pseudofs_fileno.c revision 93818
175295Sdes/*- 275295Sdes * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 375295Sdes * All rights reserved. 475295Sdes * 575295Sdes * Redistribution and use in source and binary forms, with or without 675295Sdes * modification, are permitted provided that the following conditions 775295Sdes * are met: 875295Sdes * 1. Redistributions of source code must retain the above copyright 975295Sdes * notice, this list of conditions and the following disclaimer 1075295Sdes * in this position and unchanged. 1175295Sdes * 2. Redistributions in binary form must reproduce the above copyright 1275295Sdes * notice, this list of conditions and the following disclaimer in the 1375295Sdes * documentation and/or other materials provided with the distribution. 1475295Sdes * 3. The name of the author may not be used to endorse or promote products 1575295Sdes * derived from this software without specific prior written permission. 1675295Sdes * 1775295Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1875295Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1975295Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2075295Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2175295Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2275295Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2375295Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2475295Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2575295Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2675295Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2775295Sdes * 2875295Sdes * $FreeBSD: head/sys/fs/pseudofs/pseudofs_fileno.c 93818 2002-04-04 21:03:38Z jhb $ 2975295Sdes */ 3075295Sdes 3175295Sdes#include <sys/param.h> 3275295Sdes#include <sys/kernel.h> 3375295Sdes#include <sys/systm.h> 3478073Sdes#include <sys/lock.h> 3575295Sdes#include <sys/malloc.h> 3677965Sdes#include <sys/mutex.h> 3775295Sdes#include <sys/sysctl.h> 3875295Sdes 3975295Sdes#include <machine/limits.h> 4075295Sdes 4175295Sdes#include <fs/pseudofs/pseudofs.h> 4275295Sdes#include <fs/pseudofs/pseudofs_internal.h> 4375295Sdes 4477998Sdesstatic MALLOC_DEFINE(M_PFSFILENO, "pfs_fileno", "pseudofs fileno bitmap"); 4575295Sdes 4675295Sdesstatic struct mtx pfs_fileno_mutex; 4775295Sdes 4875295Sdes#define PFS_BITMAP_SIZE 4096 4977998Sdes#define PFS_SLOT_BITS (int)(sizeof(unsigned int) * CHAR_BIT) 5075295Sdes#define PFS_BITMAP_BITS (PFS_BITMAP_SIZE * PFS_SLOT_BITS) 5175295Sdesstruct pfs_bitmap { 5275295Sdes u_int32_t pb_offset; 5375295Sdes int pb_used; 5475295Sdes unsigned int pb_bitmap[PFS_BITMAP_SIZE]; 5575295Sdes struct pfs_bitmap *pb_next; 5675295Sdes}; 5775295Sdes 5875295Sdes/* 5975295Sdes * Initialization 6075295Sdes */ 6175295Sdesvoid 6275295Sdespfs_fileno_load(void) 6375295Sdes{ 6493818Sjhb mtx_init(&pfs_fileno_mutex, "pseudofs_fileno", NULL, MTX_DEF); 6575295Sdes} 6675295Sdes 6775295Sdes/* 6875295Sdes * Teardown 6975295Sdes */ 7075295Sdesvoid 7175295Sdespfs_fileno_unload(void) 7275295Sdes{ 7375295Sdes mtx_destroy(&pfs_fileno_mutex); 7475295Sdes} 7575295Sdes 7675295Sdes/* 7775295Sdes * Initialize fileno bitmap 7875295Sdes */ 7975295Sdesvoid 8075295Sdespfs_fileno_init(struct pfs_info *pi) 8175295Sdes{ 8275295Sdes struct pfs_bitmap *pb; 8375295Sdes 8475295Sdes MALLOC(pb, struct pfs_bitmap *, sizeof *pb, 8575295Sdes M_PFSFILENO, M_WAITOK|M_ZERO); 8675295Sdes 8775295Sdes mtx_lock(&pi->pi_mutex); 8875295Sdes 8975295Sdes pb->pb_bitmap[0] = 07; 9075295Sdes pb->pb_used = 3; 9175295Sdes pi->pi_bitmap = pb; 9275295Sdes pi->pi_root->pn_fileno = 2; 9375295Sdes 9475295Sdes mtx_unlock(&pi->pi_mutex); 9575295Sdes} 9675295Sdes 9775295Sdes/* 9875295Sdes * Tear down fileno bitmap 9975295Sdes */ 10075295Sdesvoid 10175295Sdespfs_fileno_uninit(struct pfs_info *pi) 10275295Sdes{ 10375295Sdes struct pfs_bitmap *pb, *npb; 10475295Sdes int used; 10575295Sdes 10675295Sdes mtx_lock(&pi->pi_mutex); 10775295Sdes 10875295Sdes pb = pi->pi_bitmap; 10975295Sdes pi->pi_bitmap = NULL; 11075295Sdes 11175295Sdes mtx_unlock(&pi->pi_mutex); 11275295Sdes 11375295Sdes for (used = 0; pb; pb = npb) { 11475295Sdes npb = pb->pb_next; 11575295Sdes used += pb->pb_used; 11675295Sdes FREE(pb, M_PFSFILENO); 11775295Sdes } 11877998Sdes#if 0 11977998Sdes /* we currently don't reclaim filenos */ 12075295Sdes if (used > 2) 12175295Sdes printf("WARNING: %d file numbers still in use\n", used); 12277998Sdes#endif 12375295Sdes} 12475295Sdes 12575295Sdes/* 12675295Sdes * Get the next available file number 12775295Sdes */ 12875295Sdesstatic u_int32_t 12975295Sdespfs_get_fileno(struct pfs_info *pi) 13075295Sdes{ 13175295Sdes struct pfs_bitmap *pb, *ppb; 13275295Sdes u_int32_t fileno; 13375295Sdes unsigned int *p; 13475295Sdes int i; 13575295Sdes 13675295Sdes mtx_lock(&pi->pi_mutex); 13775295Sdes 13875295Sdes /* look for the first page with free bits */ 13975295Sdes for (ppb = NULL, pb = pi->pi_bitmap; pb; ppb = pb, pb = pb->pb_next) 14075295Sdes if (pb->pb_used != PFS_BITMAP_BITS) 14175295Sdes break; 14275295Sdes 14375295Sdes /* out of pages? */ 14475295Sdes if (pb == NULL) { 14575295Sdes mtx_unlock(&pi->pi_mutex); 14675295Sdes MALLOC(pb, struct pfs_bitmap *, sizeof *pb, 14775295Sdes M_PFSFILENO, M_WAITOK|M_ZERO); 14875295Sdes mtx_lock(&pi->pi_mutex); 14975295Sdes /* protect against possible race */ 15075295Sdes while (ppb->pb_next) 15175295Sdes ppb = ppb->pb_next; 15275295Sdes pb->pb_offset = ppb->pb_offset + PFS_BITMAP_BITS; 15375295Sdes ppb->pb_next = pb; 15475295Sdes } 15575295Sdes 15675295Sdes /* find the first free slot */ 15775295Sdes for (i = 0; i < PFS_BITMAP_SIZE; ++i) 15875295Sdes if (pb->pb_bitmap[i] != UINT_MAX) 15975295Sdes break; 16075295Sdes 16175295Sdes /* find the first available bit and flip it */ 16275295Sdes fileno = pb->pb_offset + i * PFS_SLOT_BITS; 16375295Sdes p = &pb->pb_bitmap[i]; 16475295Sdes for (i = 0; i < PFS_SLOT_BITS; ++i, ++fileno) 16575295Sdes if ((*p & (unsigned int)(1 << i)) == 0) 16675295Sdes break; 16775295Sdes KASSERT(i < PFS_SLOT_BITS, 16875295Sdes ("slot has free bits, yet doesn't")); 16975295Sdes *p |= (unsigned int)(1 << i); 17075295Sdes ++pb->pb_used; 17175295Sdes 17275295Sdes mtx_unlock(&pi->pi_mutex); 17375295Sdes 17475295Sdes return fileno; 17575295Sdes} 17675295Sdes 17775295Sdes/* 17875295Sdes * Free a file number 17975295Sdes */ 18075295Sdesstatic void 18175295Sdespfs_free_fileno(struct pfs_info *pi, u_int32_t fileno) 18275295Sdes{ 18375295Sdes struct pfs_bitmap *pb; 18475295Sdes unsigned int *p; 18575295Sdes int i; 18675295Sdes 18775295Sdes mtx_lock(&pi->pi_mutex); 18875295Sdes 18975295Sdes /* find the right page */ 19075295Sdes for (pb = pi->pi_bitmap; 19175295Sdes pb && fileno >= PFS_BITMAP_BITS; 19275295Sdes pb = pb->pb_next, fileno -= PFS_BITMAP_BITS) 19375295Sdes /* nothing */ ; 19475295Sdes KASSERT(pb, 19575295Sdes ("fileno isn't in any bitmap")); 19675295Sdes 19775295Sdes /* find the right bit in the right slot and flip it */ 19875295Sdes p = &pb->pb_bitmap[fileno / PFS_SLOT_BITS]; 19975295Sdes i = fileno % PFS_SLOT_BITS; 20075295Sdes KASSERT(*p & (unsigned int)(1 << i), 20175295Sdes ("fileno is already free")); 20275295Sdes *p &= ~((unsigned int)(1 << i)); 20375295Sdes --pb->pb_used; 20475295Sdes 20575295Sdes mtx_unlock(&pi->pi_mutex); 20677998Sdes printf("pfs_free_fileno(): reclaimed %d\n", fileno); 20775295Sdes} 20875295Sdes 20975295Sdes/* 21075295Sdes * Allocate a file number 21175295Sdes */ 21275295Sdesvoid 21375295Sdespfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn) 21475295Sdes{ 21575295Sdes /* make sure our parent has a file number */ 21675295Sdes if (pn->pn_parent && !pn->pn_parent->pn_fileno) 21775295Sdes pfs_fileno_alloc(pi, pn->pn_parent); 21875295Sdes 21975295Sdes switch (pn->pn_type) { 22075295Sdes case pfstype_root: 22175295Sdes case pfstype_dir: 22275295Sdes case pfstype_file: 22375295Sdes case pfstype_symlink: 22477998Sdes case pfstype_procdir: 22575295Sdes pn->pn_fileno = pfs_get_fileno(pi); 22675295Sdes break; 22775295Sdes case pfstype_this: 22875295Sdes KASSERT(pn->pn_parent != NULL, 22975295Sdes ("pfstype_this node has no parent")); 23075295Sdes pn->pn_fileno = pn->pn_parent->pn_fileno; 23175295Sdes break; 23275295Sdes case pfstype_parent: 23375295Sdes KASSERT(pn->pn_parent != NULL, 23475295Sdes ("pfstype_parent node has no parent")); 23575295Sdes if (pn->pn_parent == pi->pi_root) { 23675295Sdes pn->pn_fileno = pn->pn_parent->pn_fileno; 23775295Sdes break; 23875295Sdes } 23975295Sdes KASSERT(pn->pn_parent->pn_parent != NULL, 24075295Sdes ("pfstype_parent node has no grandparent")); 24175295Sdes pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno; 24275295Sdes break; 24375295Sdes case pfstype_none: 24477998Sdes KASSERT(0, 24575295Sdes ("pfs_fileno_alloc() called for pfstype_none node")); 24675295Sdes break; 24775295Sdes } 24877998Sdes 24977998Sdes#if 0 25075295Sdes printf("pfs_fileno_alloc(): %s: ", pi->pi_name); 25175295Sdes if (pn->pn_parent) { 25275295Sdes if (pn->pn_parent->pn_parent) { 25375295Sdes printf("%s/", pn->pn_parent->pn_parent->pn_name); 25475295Sdes } 25575295Sdes printf("%s/", pn->pn_parent->pn_name); 25675295Sdes } 25775295Sdes printf("%s -> %d\n", pn->pn_name, pn->pn_fileno); 25877998Sdes#endif 25975295Sdes} 26075295Sdes 26175295Sdes/* 26275295Sdes * Release a file number 26375295Sdes */ 26475295Sdesvoid 26575295Sdespfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn) 26675295Sdes{ 26775295Sdes switch (pn->pn_type) { 26875295Sdes case pfstype_root: 26975295Sdes case pfstype_dir: 27075295Sdes case pfstype_file: 27175295Sdes case pfstype_symlink: 27277998Sdes case pfstype_procdir: 27375295Sdes pfs_free_fileno(pi, pn->pn_fileno); 27475295Sdes break; 27575295Sdes case pfstype_this: 27675295Sdes case pfstype_parent: 27775295Sdes /* ignore these, as they don't "own" their file number */ 27875295Sdes break; 27975295Sdes case pfstype_none: 28077998Sdes KASSERT(0, 28175295Sdes ("pfs_fileno_free() called for pfstype_none node")); 28275295Sdes break; 28375295Sdes } 28475295Sdes} 285