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.
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
40 #include <fs/pseudofs/pseudofs.h>
41 #include <fs/pseudofs/pseudofs_internal.h>
43 static MALLOC_DEFINE(M_PFSFILENO, "pfs_fileno", "pseudofs fileno bitmap");
45 static struct mtx pfs_fileno_mutex;
47 #define PFS_BITMAP_SIZE 4096
48 #define PFS_SLOT_BITS (int)(sizeof(unsigned int) * CHAR_BIT)
49 #define PFS_BITMAP_BITS (PFS_BITMAP_SIZE * PFS_SLOT_BITS)
53 unsigned int pb_bitmap[PFS_BITMAP_SIZE];
54 struct pfs_bitmap *pb_next;
63 mtx_init(&pfs_fileno_mutex, "pseudofs_fileno", NULL, MTX_DEF);
70 pfs_fileno_unload(void)
72 mtx_destroy(&pfs_fileno_mutex);
76 * Initialize fileno bitmap
79 pfs_fileno_init(struct pfs_info *pi)
81 struct pfs_bitmap *pb;
83 MALLOC(pb, struct pfs_bitmap *, sizeof *pb,
84 M_PFSFILENO, M_WAITOK|M_ZERO);
86 mtx_lock(&pi->pi_mutex);
88 pb->pb_bitmap[0] = 07;
91 pi->pi_root->pn_fileno = 2;
93 mtx_unlock(&pi->pi_mutex);
97 * Tear down fileno bitmap
100 pfs_fileno_uninit(struct pfs_info *pi)
102 struct pfs_bitmap *pb, *npb;
105 mtx_lock(&pi->pi_mutex);
108 pi->pi_bitmap = NULL;
110 mtx_unlock(&pi->pi_mutex);
112 for (used = 0; pb; pb = npb) {
115 FREE(pb, M_PFSFILENO);
118 /* we currently don't reclaim filenos */
120 printf("WARNING: %d file numbers still in use\n", used);
125 * Get the next available file number
128 pfs_get_fileno(struct pfs_info *pi)
130 struct pfs_bitmap *pb, *ppb;
135 mtx_lock(&pi->pi_mutex);
137 /* look for the first page with free bits */
138 for (ppb = NULL, pb = pi->pi_bitmap; pb; ppb = pb, pb = pb->pb_next)
139 if (pb->pb_used != PFS_BITMAP_BITS)
144 mtx_unlock(&pi->pi_mutex);
145 MALLOC(pb, struct pfs_bitmap *, sizeof *pb,
146 M_PFSFILENO, M_WAITOK|M_ZERO);
147 mtx_lock(&pi->pi_mutex);
148 /* protect against possible race */
151 pb->pb_offset = ppb->pb_offset + PFS_BITMAP_BITS;
155 /* find the first free slot */
156 for (i = 0; i < PFS_BITMAP_SIZE; ++i)
157 if (pb->pb_bitmap[i] != UINT_MAX)
160 /* find the first available bit and flip it */
161 fileno = pb->pb_offset + i * PFS_SLOT_BITS;
162 p = &pb->pb_bitmap[i];
163 for (i = 0; i < PFS_SLOT_BITS; ++i, ++fileno)
164 if ((*p & (unsigned int)(1 << i)) == 0)
166 KASSERT(i < PFS_SLOT_BITS,
167 ("slot has free bits, yet doesn't"));
168 *p |= (unsigned int)(1 << i);
171 mtx_unlock(&pi->pi_mutex);
180 pfs_free_fileno(struct pfs_info *pi, u_int32_t fileno)
182 struct pfs_bitmap *pb;
186 mtx_lock(&pi->pi_mutex);
188 /* find the right page */
189 for (pb = pi->pi_bitmap;
190 pb && fileno >= PFS_BITMAP_BITS;
191 pb = pb->pb_next, fileno -= PFS_BITMAP_BITS)
194 ("fileno isn't in any bitmap"));
196 /* find the right bit in the right slot and flip it */
197 p = &pb->pb_bitmap[fileno / PFS_SLOT_BITS];
198 i = fileno % PFS_SLOT_BITS;
199 KASSERT(*p & (unsigned int)(1 << i),
200 ("fileno is already free"));
201 *p &= ~((unsigned int)(1 << i));
204 mtx_unlock(&pi->pi_mutex);
205 printf("pfs_free_fileno(): reclaimed %d\n", fileno);
209 * Allocate a file number
212 pfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn)
214 /* make sure our parent has a file number */
215 if (pn->pn_parent && !pn->pn_parent->pn_fileno)
216 pfs_fileno_alloc(pi, pn->pn_parent);
218 switch (pn->pn_type) {
222 case pfstype_symlink:
223 case pfstype_procdir:
224 pn->pn_fileno = pfs_get_fileno(pi);
227 KASSERT(pn->pn_parent != NULL,
228 ("pfstype_this node has no parent"));
229 pn->pn_fileno = pn->pn_parent->pn_fileno;
232 KASSERT(pn->pn_parent != NULL,
233 ("pfstype_parent node has no parent"));
234 if (pn->pn_parent == pi->pi_root) {
235 pn->pn_fileno = pn->pn_parent->pn_fileno;
238 KASSERT(pn->pn_parent->pn_parent != NULL,
239 ("pfstype_parent node has no grandparent"));
240 pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
244 ("pfs_fileno_alloc() called for pfstype_none node"));
249 printf("pfs_fileno_alloc(): %s: ", pi->pi_name);
251 if (pn->pn_parent->pn_parent) {
252 printf("%s/", pn->pn_parent->pn_parent->pn_name);
254 printf("%s/", pn->pn_parent->pn_name);
256 printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);
261 * Release a file number
264 pfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn)
266 switch (pn->pn_type) {
270 case pfstype_symlink:
271 case pfstype_procdir:
272 pfs_free_fileno(pi, pn->pn_fileno);
276 /* ignore these, as they don't "own" their file number */
280 ("pfs_fileno_free() called for pfstype_none node"));