2 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_pseudofs.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
43 #include <fs/pseudofs/pseudofs.h>
44 #include <fs/pseudofs/pseudofs_internal.h>
47 * Initialize fileno bitmap
50 pfs_fileno_init(struct pfs_info *pi)
54 up = new_unrhdr(3, INT_MAX, &pi->pi_mutex);
55 mtx_lock(&pi->pi_mutex);
57 pi->pi_root->pn_fileno = 2;
58 mtx_unlock(&pi->pi_mutex);
62 * Tear down fileno bitmap
65 pfs_fileno_uninit(struct pfs_info *pi)
69 mtx_lock(&pi->pi_mutex);
74 mtx_unlock(&pi->pi_mutex);
80 * Allocate a file number
83 pfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn)
85 /* make sure our parent has a file number */
86 if (pn->pn_parent && !pn->pn_parent->pn_fileno)
87 pfs_fileno_alloc(pi, pn->pn_parent);
89 switch (pn->pn_type) {
95 pn->pn_fileno = alloc_unr(pi->pi_unrhdr);
98 KASSERT(pn->pn_parent != NULL,
99 ("pfstype_this node has no parent"));
100 pn->pn_fileno = pn->pn_parent->pn_fileno;
103 KASSERT(pn->pn_parent != NULL,
104 ("pfstype_parent node has no parent"));
105 if (pn->pn_parent == pi->pi_root) {
106 pn->pn_fileno = pn->pn_parent->pn_fileno;
109 KASSERT(pn->pn_parent->pn_parent != NULL,
110 ("pfstype_parent node has no grandparent"));
111 pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
115 ("pfs_fileno_alloc() called for pfstype_none node"));
120 printf("pfs_fileno_alloc(): %s: ", pi->pi_name);
122 if (pn->pn_parent->pn_parent) {
123 printf("%s/", pn->pn_parent->pn_parent->pn_name);
125 printf("%s/", pn->pn_parent->pn_name);
127 printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);
132 * Release a file number
135 pfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn)
137 switch (pn->pn_type) {
141 case pfstype_symlink:
142 case pfstype_procdir:
143 free_unr(pi->pi_unrhdr, pn->pn_fileno);
147 /* ignore these, as they don't "own" their file number */
151 ("pfs_fileno_free() called for pfstype_none node"));