pseudofs_fileno.c revision 168720
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 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. 16 * 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. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/fs/pseudofs/pseudofs_fileno.c 168720 2007-04-14 14:08:30Z des $"); 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/proc.h> 42#include <sys/sysctl.h> 43#include <sys/systm.h> 44 45#include <fs/pseudofs/pseudofs.h> 46#include <fs/pseudofs/pseudofs_internal.h> 47 48/* 49 * Initialize fileno bitmap 50 */ 51void 52pfs_fileno_init(struct pfs_info *pi) 53{ 54 55 mtx_assert(&Giant, MA_OWNED); 56 mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF); 57 pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex); 58} 59 60/* 61 * Tear down fileno bitmap 62 */ 63void 64pfs_fileno_uninit(struct pfs_info *pi) 65{ 66 67 mtx_assert(&Giant, MA_OWNED); 68 delete_unrhdr(pi->pi_unrhdr); 69 pi->pi_unrhdr = NULL; 70 mtx_destroy(&pi->pi_mutex); 71} 72 73/* 74 * Allocate a file number 75 */ 76void 77pfs_fileno_alloc(struct pfs_node *pn) 78{ 79 80 /* make sure our parent has a file number */ 81 if (pn->pn_parent && !pn->pn_parent->pn_fileno) 82 pfs_fileno_alloc(pn->pn_parent); 83 84 switch (pn->pn_type) { 85 case pfstype_root: 86 /* root must always be 2 */ 87 pn->pn_fileno = 2; 88 break; 89 case pfstype_dir: 90 case pfstype_file: 91 case pfstype_symlink: 92 case pfstype_procdir: 93 pn->pn_fileno = alloc_unr(pn->pn_info->pi_unrhdr); 94 break; 95 case pfstype_this: 96 KASSERT(pn->pn_parent != NULL, 97 ("pfstype_this node has no parent")); 98 pn->pn_fileno = pn->pn_parent->pn_fileno; 99 break; 100 case pfstype_parent: 101 KASSERT(pn->pn_parent != NULL, 102 ("pfstype_parent node has no parent")); 103 if (pn->pn_parent == pn->pn_info->pi_root) { 104 pn->pn_fileno = pn->pn_parent->pn_fileno; 105 break; 106 } 107 KASSERT(pn->pn_parent->pn_parent != NULL, 108 ("pfstype_parent node has no grandparent")); 109 pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno; 110 break; 111 case pfstype_none: 112 KASSERT(0, 113 ("pfs_fileno_alloc() called for pfstype_none node")); 114 break; 115 } 116 117#if 0 118 printf("pfs_fileno_alloc(): %s: ", pn->pn_info->pi_name); 119 if (pn->pn_parent) { 120 if (pn->pn_parent->pn_parent) { 121 printf("%s/", pn->pn_parent->pn_parent->pn_name); 122 } 123 printf("%s/", pn->pn_parent->pn_name); 124 } 125 printf("%s -> %d\n", pn->pn_name, pn->pn_fileno); 126#endif 127} 128 129/* 130 * Release a file number 131 */ 132void 133pfs_fileno_free(struct pfs_node *pn) 134{ 135 136 switch (pn->pn_type) { 137 case pfstype_root: 138 /* not allocated from unrhdr */ 139 return; 140 case pfstype_dir: 141 case pfstype_file: 142 case pfstype_symlink: 143 case pfstype_procdir: 144 free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno); 145 break; 146 case pfstype_this: 147 case pfstype_parent: 148 /* ignore these, as they don't "own" their file number */ 149 break; 150 case pfstype_none: 151 KASSERT(0, 152 ("pfs_fileno_free() called for pfstype_none node")); 153 break; 154 } 155} 156