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$"); 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> 41168720Sdes#include <sys/proc.h> 4275295Sdes#include <sys/sysctl.h> 43168637Sdes#include <sys/systm.h> 4475295Sdes 4575295Sdes#include <fs/pseudofs/pseudofs.h> 4675295Sdes#include <fs/pseudofs/pseudofs_internal.h> 4775295Sdes 4875295Sdes/* 4975295Sdes * Initialize fileno bitmap 5075295Sdes */ 5175295Sdesvoid 5275295Sdespfs_fileno_init(struct pfs_info *pi) 5375295Sdes{ 5497940Sdes 55168720Sdes mtx_assert(&Giant, MA_OWNED); 56168720Sdes mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF); 57168720Sdes pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex); 5875295Sdes} 5975295Sdes 6075295Sdes/* 6175295Sdes * Tear down fileno bitmap 6275295Sdes */ 6375295Sdesvoid 6475295Sdespfs_fileno_uninit(struct pfs_info *pi) 6575295Sdes{ 6697940Sdes 67168720Sdes mtx_assert(&Giant, MA_OWNED); 68168720Sdes delete_unrhdr(pi->pi_unrhdr); 69143841Sphk pi->pi_unrhdr = NULL; 70168720Sdes mtx_destroy(&pi->pi_mutex); 7175295Sdes} 7275295Sdes 7375295Sdes/* 7475295Sdes * Allocate a file number 7575295Sdes */ 7675295Sdesvoid 77168720Sdespfs_fileno_alloc(struct pfs_node *pn) 7875295Sdes{ 79168637Sdes 80168764Sdes if (pn->pn_parent) 81168764Sdes PFS_TRACE(("%s/%s", pn->pn_parent->pn_name, pn->pn_name)); 82168764Sdes else 83168764Sdes PFS_TRACE(("%s", pn->pn_name)); 84168764Sdes pfs_assert_not_owned(pn); 8597940Sdes 8675295Sdes switch (pn->pn_type) { 8775295Sdes case pfstype_root: 88168637Sdes /* root must always be 2 */ 89168637Sdes pn->pn_fileno = 2; 90168637Sdes break; 9175295Sdes case pfstype_dir: 9275295Sdes case pfstype_file: 9375295Sdes case pfstype_symlink: 9477998Sdes case pfstype_procdir: 95168720Sdes pn->pn_fileno = alloc_unr(pn->pn_info->pi_unrhdr); 9675295Sdes break; 9775295Sdes case pfstype_this: 9875295Sdes KASSERT(pn->pn_parent != NULL, 99168764Sdes ("%s(): pfstype_this node has no parent", __func__)); 10075295Sdes pn->pn_fileno = pn->pn_parent->pn_fileno; 10175295Sdes break; 10275295Sdes case pfstype_parent: 10375295Sdes KASSERT(pn->pn_parent != NULL, 104168764Sdes ("%s(): pfstype_parent node has no parent", __func__)); 105168764Sdes if (pn->pn_parent->pn_type == pfstype_root) { 10675295Sdes pn->pn_fileno = pn->pn_parent->pn_fileno; 10775295Sdes break; 10875295Sdes } 10975295Sdes KASSERT(pn->pn_parent->pn_parent != NULL, 110168764Sdes ("%s(): pfstype_parent node has no grandparent", __func__)); 11175295Sdes pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno; 11275295Sdes break; 11375295Sdes case pfstype_none: 11477998Sdes KASSERT(0, 115168764Sdes ("%s(): pfstype_none node", __func__)); 11675295Sdes break; 11797940Sdes } 11877998Sdes 11977998Sdes#if 0 120168764Sdes printf("%s(): %s: ", __func__, pn->pn_info->pi_name); 12175295Sdes if (pn->pn_parent) { 12275295Sdes if (pn->pn_parent->pn_parent) { 12375295Sdes printf("%s/", pn->pn_parent->pn_parent->pn_name); 12475295Sdes } 12575295Sdes printf("%s/", pn->pn_parent->pn_name); 12675295Sdes } 12775295Sdes printf("%s -> %d\n", pn->pn_name, pn->pn_fileno); 12877998Sdes#endif 12975295Sdes} 13075295Sdes 13175295Sdes/* 13275295Sdes * Release a file number 13375295Sdes */ 13475295Sdesvoid 135168720Sdespfs_fileno_free(struct pfs_node *pn) 13675295Sdes{ 137168637Sdes 138168764Sdes pfs_assert_not_owned(pn); 139168764Sdes 14075295Sdes switch (pn->pn_type) { 14175295Sdes case pfstype_root: 142168637Sdes /* not allocated from unrhdr */ 143168637Sdes return; 14475295Sdes case pfstype_dir: 14575295Sdes case pfstype_file: 14675295Sdes case pfstype_symlink: 14777998Sdes case pfstype_procdir: 148168720Sdes free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno); 14975295Sdes break; 15075295Sdes case pfstype_this: 15175295Sdes case pfstype_parent: 15275295Sdes /* ignore these, as they don't "own" their file number */ 15375295Sdes break; 15475295Sdes case pfstype_none: 15577998Sdes KASSERT(0, 15675295Sdes ("pfs_fileno_free() called for pfstype_none node")); 15775295Sdes break; 15875295Sdes } 15975295Sdes} 160