pseudofs_fileno.c (143592) | pseudofs_fileno.c (143841) |
---|---|
1/*- 2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 13 unchanged lines hidden (view full) --- 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. 27 */ 28 29#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 13 unchanged lines hidden (view full) --- 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. 27 */ 28 29#include <sys/cdefs.h> |
30__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_fileno.c 143592 2005-03-14 15:54:11Z des $"); | 30__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_fileno.c 143841 2005-03-19 08:22:36Z phk $"); |
31 32#include "opt_pseudofs.h" 33 34#include <sys/param.h> 35#include <sys/kernel.h> 36#include <sys/systm.h> 37#include <sys/limits.h> 38#include <sys/lock.h> 39#include <sys/malloc.h> 40#include <sys/mutex.h> 41#include <sys/sysctl.h> 42 43#include <fs/pseudofs/pseudofs.h> 44#include <fs/pseudofs/pseudofs_internal.h> 45 | 31 32#include "opt_pseudofs.h" 33 34#include <sys/param.h> 35#include <sys/kernel.h> 36#include <sys/systm.h> 37#include <sys/limits.h> 38#include <sys/lock.h> 39#include <sys/malloc.h> 40#include <sys/mutex.h> 41#include <sys/sysctl.h> 42 43#include <fs/pseudofs/pseudofs.h> 44#include <fs/pseudofs/pseudofs_internal.h> 45 |
46static MALLOC_DEFINE(M_PFSFILENO, "pfs_fileno", "pseudofs fileno bitmap"); 47 48static struct mtx pfs_fileno_mutex; 49 50#define PFS_BITMAP_SIZE 4096 51#define PFS_SLOT_BITS (int)(sizeof(unsigned int) * CHAR_BIT) 52#define PFS_BITMAP_BITS (PFS_BITMAP_SIZE * PFS_SLOT_BITS) 53struct pfs_bitmap { 54 u_int32_t pb_offset; 55 int pb_used; 56 unsigned int pb_bitmap[PFS_BITMAP_SIZE]; 57 struct pfs_bitmap *pb_next; 58}; 59 | |
60/* | 46/* |
61 * Initialization 62 */ 63void 64pfs_fileno_load(void) 65{ 66 mtx_init(&pfs_fileno_mutex, "pseudofs_fileno", NULL, MTX_DEF); 67} 68 69/* 70 * Teardown 71 */ 72void 73pfs_fileno_unload(void) 74{ 75 mtx_destroy(&pfs_fileno_mutex); 76} 77 78/* | |
79 * Initialize fileno bitmap 80 */ 81void 82pfs_fileno_init(struct pfs_info *pi) 83{ | 47 * Initialize fileno bitmap 48 */ 49void 50pfs_fileno_init(struct pfs_info *pi) 51{ |
84 struct pfs_bitmap *pb; | 52 struct unrhdr *up; |
85 | 53 |
86 MALLOC(pb, struct pfs_bitmap *, sizeof *pb, 87 M_PFSFILENO, M_WAITOK|M_ZERO); 88 | 54 up = new_unrhdr(3, INT_MAX, &pi->pi_mutex); |
89 mtx_lock(&pi->pi_mutex); | 55 mtx_lock(&pi->pi_mutex); |
90 91 pb->pb_bitmap[0] = 07; 92 pb->pb_used = 3; 93 pi->pi_bitmap = pb; | 56 pi->pi_unrhdr = up; |
94 pi->pi_root->pn_fileno = 2; | 57 pi->pi_root->pn_fileno = 2; |
95 | |
96 mtx_unlock(&pi->pi_mutex); 97} 98 99/* 100 * Tear down fileno bitmap 101 */ 102void 103pfs_fileno_uninit(struct pfs_info *pi) 104{ | 58 mtx_unlock(&pi->pi_mutex); 59} 60 61/* 62 * Tear down fileno bitmap 63 */ 64void 65pfs_fileno_uninit(struct pfs_info *pi) 66{ |
105 struct pfs_bitmap *pb, *npb; 106 int used; | 67 struct unrhdr *up; |
107 108 mtx_lock(&pi->pi_mutex); 109 | 68 69 mtx_lock(&pi->pi_mutex); 70 |
110 pb = pi->pi_bitmap; 111 pi->pi_bitmap = NULL; | 71 up = pi->pi_unrhdr; 72 pi->pi_unrhdr = NULL; |
112 113 mtx_unlock(&pi->pi_mutex); 114 | 73 74 mtx_unlock(&pi->pi_mutex); 75 |
115 for (used = 0; pb; pb = npb) { 116 npb = pb->pb_next; 117 used += pb->pb_used; 118 FREE(pb, M_PFSFILENO); 119 } 120#if 0 121 /* we currently don't reclaim filenos */ 122 if (used > 2) 123 printf("WARNING: %d file numbers still in use\n", used); 124#endif | 76 delete_unrhdr(up); |
125} 126 127/* | 77} 78 79/* |
128 * Get the next available file number 129 */ 130static u_int32_t 131pfs_get_fileno(struct pfs_info *pi) 132{ 133 struct pfs_bitmap *pb, *ppb; 134 u_int32_t fileno; 135 unsigned int *p; 136 int i; 137 138 mtx_lock(&pi->pi_mutex); 139 140 /* look for the first page with free bits */ 141 for (ppb = NULL, pb = pi->pi_bitmap; pb; ppb = pb, pb = pb->pb_next) 142 if (pb->pb_used != PFS_BITMAP_BITS) 143 break; 144 145 /* out of pages? */ 146 if (pb == NULL) { 147 mtx_unlock(&pi->pi_mutex); 148 MALLOC(pb, struct pfs_bitmap *, sizeof *pb, 149 M_PFSFILENO, M_WAITOK|M_ZERO); 150 mtx_lock(&pi->pi_mutex); 151 /* protect against possible race */ 152 while (ppb->pb_next) 153 ppb = ppb->pb_next; 154 pb->pb_offset = ppb->pb_offset + PFS_BITMAP_BITS; 155 ppb->pb_next = pb; 156 } 157 158 /* find the first free slot */ 159 for (i = 0; i < PFS_BITMAP_SIZE; ++i) 160 if (pb->pb_bitmap[i] != UINT_MAX) 161 break; 162 163 /* find the first available bit and flip it */ 164 fileno = pb->pb_offset + i * PFS_SLOT_BITS; 165 p = &pb->pb_bitmap[i]; 166 for (i = 0; i < PFS_SLOT_BITS; ++i, ++fileno) 167 if ((*p & (unsigned int)(1 << i)) == 0) 168 break; 169 KASSERT(i < PFS_SLOT_BITS, 170 ("slot has free bits, yet doesn't")); 171 *p |= (unsigned int)(1 << i); 172 ++pb->pb_used; 173 174 mtx_unlock(&pi->pi_mutex); 175 176 return fileno; 177} 178 179/* 180 * Free a file number 181 */ 182static void 183pfs_free_fileno(struct pfs_info *pi, u_int32_t fileno) 184{ 185 struct pfs_bitmap *pb; 186 unsigned int *p; 187 int i; 188 189 mtx_lock(&pi->pi_mutex); 190 191 /* find the right page */ 192 for (pb = pi->pi_bitmap; 193 pb && fileno >= PFS_BITMAP_BITS; 194 pb = pb->pb_next, fileno -= PFS_BITMAP_BITS) 195 /* nothing */ ; 196 KASSERT(pb, 197 ("fileno isn't in any bitmap")); 198 199 /* find the right bit in the right slot and flip it */ 200 p = &pb->pb_bitmap[fileno / PFS_SLOT_BITS]; 201 i = fileno % PFS_SLOT_BITS; 202 KASSERT(*p & (unsigned int)(1 << i), 203 ("fileno is already free")); 204 *p &= ~((unsigned int)(1 << i)); 205 --pb->pb_used; 206 207 mtx_unlock(&pi->pi_mutex); 208 printf("pfs_free_fileno(): reclaimed %d\n", fileno); 209} 210 211/* | |
212 * Allocate a file number 213 */ 214void 215pfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn) 216{ 217 /* make sure our parent has a file number */ 218 if (pn->pn_parent && !pn->pn_parent->pn_fileno) 219 pfs_fileno_alloc(pi, pn->pn_parent); 220 221 switch (pn->pn_type) { 222 case pfstype_root: 223 case pfstype_dir: 224 case pfstype_file: 225 case pfstype_symlink: 226 case pfstype_procdir: | 80 * Allocate a file number 81 */ 82void 83pfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn) 84{ 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); 88 89 switch (pn->pn_type) { 90 case pfstype_root: 91 case pfstype_dir: 92 case pfstype_file: 93 case pfstype_symlink: 94 case pfstype_procdir: |
227 pn->pn_fileno = pfs_get_fileno(pi); | 95 pn->pn_fileno = alloc_unr(pi->pi_unrhdr); |
228 break; 229 case pfstype_this: 230 KASSERT(pn->pn_parent != NULL, 231 ("pfstype_this node has no parent")); 232 pn->pn_fileno = pn->pn_parent->pn_fileno; 233 break; 234 case pfstype_parent: 235 KASSERT(pn->pn_parent != NULL, --- 31 unchanged lines hidden (view full) --- 267pfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn) 268{ 269 switch (pn->pn_type) { 270 case pfstype_root: 271 case pfstype_dir: 272 case pfstype_file: 273 case pfstype_symlink: 274 case pfstype_procdir: | 96 break; 97 case pfstype_this: 98 KASSERT(pn->pn_parent != NULL, 99 ("pfstype_this node has no parent")); 100 pn->pn_fileno = pn->pn_parent->pn_fileno; 101 break; 102 case pfstype_parent: 103 KASSERT(pn->pn_parent != NULL, --- 31 unchanged lines hidden (view full) --- 135pfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn) 136{ 137 switch (pn->pn_type) { 138 case pfstype_root: 139 case pfstype_dir: 140 case pfstype_file: 141 case pfstype_symlink: 142 case pfstype_procdir: |
275 pfs_free_fileno(pi, pn->pn_fileno); | 143 free_unr(pi->pi_unrhdr, pn->pn_fileno); |
276 break; 277 case pfstype_this: 278 case pfstype_parent: 279 /* ignore these, as they don't "own" their file number */ 280 break; 281 case pfstype_none: 282 KASSERT(0, 283 ("pfs_fileno_free() called for pfstype_none node")); 284 break; 285 } 286} | 144 break; 145 case pfstype_this: 146 case pfstype_parent: 147 /* ignore these, as they don't "own" their file number */ 148 break; 149 case pfstype_none: 150 KASSERT(0, 151 ("pfs_fileno_free() called for pfstype_none node")); 152 break; 153 } 154} |