pseudofs_fileno.c revision 168637
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 29143592Sdes#include <sys/cdefs.h> 30143592Sdes__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_fileno.c 168637 2007-04-11 22:40:57Z des $"); 31143592Sdes 32143592Sdes#include "opt_pseudofs.h" 33143592Sdes 3475295Sdes#include <sys/param.h> 3575295Sdes#include <sys/kernel.h> 3675295Sdes#include <sys/systm.h> 37114216Skan#include <sys/limits.h> 3878073Sdes#include <sys/lock.h> 3975295Sdes#include <sys/malloc.h> 4077965Sdes#include <sys/mutex.h> 4175295Sdes#include <sys/sysctl.h> 42168637Sdes#include <sys/systm.h> 4375295Sdes 4475295Sdes#include <fs/pseudofs/pseudofs.h> 4575295Sdes#include <fs/pseudofs/pseudofs_internal.h> 4675295Sdes 4775295Sdes/* 4875295Sdes * Initialize fileno bitmap 4975295Sdes */ 5075295Sdesvoid 5175295Sdespfs_fileno_init(struct pfs_info *pi) 5275295Sdes{ 53143841Sphk struct unrhdr *up; 5497940Sdes 55143841Sphk up = new_unrhdr(3, INT_MAX, &pi->pi_mutex); 5675295Sdes mtx_lock(&pi->pi_mutex); 57143841Sphk pi->pi_unrhdr = up; 5875295Sdes mtx_unlock(&pi->pi_mutex); 5975295Sdes} 6075295Sdes 6175295Sdes/* 6275295Sdes * Tear down fileno bitmap 6375295Sdes */ 6475295Sdesvoid 6575295Sdespfs_fileno_uninit(struct pfs_info *pi) 6675295Sdes{ 67143841Sphk struct unrhdr *up; 6897940Sdes 6975295Sdes mtx_lock(&pi->pi_mutex); 7097940Sdes 71143841Sphk up = pi->pi_unrhdr; 72143841Sphk pi->pi_unrhdr = NULL; 7397940Sdes 7475295Sdes mtx_unlock(&pi->pi_mutex); 7575295Sdes 76143841Sphk delete_unrhdr(up); 7775295Sdes} 7875295Sdes 7975295Sdes/* 8075295Sdes * Allocate a file number 8175295Sdes */ 8275295Sdesvoid 8375295Sdespfs_fileno_alloc(struct pfs_info *pi, struct pfs_node *pn) 8475295Sdes{ 85168637Sdes /* pi is not really necessary as it can be derived */ 86168637Sdes KASSERT(pi == pn->pn_info, ("pn / pi mismatch")); 87168637Sdes 8875295Sdes /* make sure our parent has a file number */ 8975295Sdes if (pn->pn_parent && !pn->pn_parent->pn_fileno) 9075295Sdes pfs_fileno_alloc(pi, pn->pn_parent); 9197940Sdes 9275295Sdes switch (pn->pn_type) { 9375295Sdes case pfstype_root: 94168637Sdes /* root must always be 2 */ 95168637Sdes pn->pn_fileno = 2; 96168637Sdes break; 9775295Sdes case pfstype_dir: 9875295Sdes case pfstype_file: 9975295Sdes case pfstype_symlink: 10077998Sdes case pfstype_procdir: 101143841Sphk pn->pn_fileno = alloc_unr(pi->pi_unrhdr); 10275295Sdes break; 10375295Sdes case pfstype_this: 10475295Sdes KASSERT(pn->pn_parent != NULL, 10575295Sdes ("pfstype_this node has no parent")); 10675295Sdes pn->pn_fileno = pn->pn_parent->pn_fileno; 10775295Sdes break; 10875295Sdes case pfstype_parent: 10975295Sdes KASSERT(pn->pn_parent != NULL, 11075295Sdes ("pfstype_parent node has no parent")); 11175295Sdes if (pn->pn_parent == pi->pi_root) { 11275295Sdes pn->pn_fileno = pn->pn_parent->pn_fileno; 11375295Sdes break; 11475295Sdes } 11575295Sdes KASSERT(pn->pn_parent->pn_parent != NULL, 11675295Sdes ("pfstype_parent node has no grandparent")); 11775295Sdes pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno; 11875295Sdes break; 11975295Sdes case pfstype_none: 12077998Sdes KASSERT(0, 12175295Sdes ("pfs_fileno_alloc() called for pfstype_none node")); 12275295Sdes break; 12397940Sdes } 12477998Sdes 12577998Sdes#if 0 12675295Sdes printf("pfs_fileno_alloc(): %s: ", pi->pi_name); 12775295Sdes if (pn->pn_parent) { 12875295Sdes if (pn->pn_parent->pn_parent) { 12975295Sdes printf("%s/", pn->pn_parent->pn_parent->pn_name); 13075295Sdes } 13175295Sdes printf("%s/", pn->pn_parent->pn_name); 13275295Sdes } 13375295Sdes printf("%s -> %d\n", pn->pn_name, pn->pn_fileno); 13477998Sdes#endif 13575295Sdes} 13675295Sdes 13775295Sdes/* 13875295Sdes * Release a file number 13975295Sdes */ 14075295Sdesvoid 14175295Sdespfs_fileno_free(struct pfs_info *pi, struct pfs_node *pn) 14275295Sdes{ 143168637Sdes /* pi is not really necessary as it can be derived */ 144168637Sdes KASSERT(pi == pn->pn_info, ("pn / pi mismatch")); 145168637Sdes 14675295Sdes switch (pn->pn_type) { 14775295Sdes case pfstype_root: 148168637Sdes /* not allocated from unrhdr */ 149168637Sdes return; 15075295Sdes case pfstype_dir: 15175295Sdes case pfstype_file: 15275295Sdes case pfstype_symlink: 15377998Sdes case pfstype_procdir: 154143841Sphk free_unr(pi->pi_unrhdr, pn->pn_fileno); 15575295Sdes break; 15675295Sdes case pfstype_this: 15775295Sdes case pfstype_parent: 15875295Sdes /* ignore these, as they don't "own" their file number */ 15975295Sdes break; 16075295Sdes case pfstype_none: 16177998Sdes KASSERT(0, 16275295Sdes ("pfs_fileno_free() called for pfstype_none node")); 16375295Sdes break; 16475295Sdes } 16575295Sdes} 166