Path: blob/main/sys/fs/pseudofs/pseudofs_fileno.c
107528 views
/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 2001 Dag-Erling Smørgrav4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer11* in this position and unchanged.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. The name of the author may not be used to endorse or promote products16* derived from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR19* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES20* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.21* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,22* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT23* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF27* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31#include "opt_pseudofs.h"3233#include <sys/param.h>34#include <sys/kernel.h>35#include <sys/systm.h>36#include <sys/limits.h>37#include <sys/lock.h>38#include <sys/malloc.h>39#include <sys/mutex.h>40#include <sys/proc.h>41#include <sys/sysctl.h>42#include <sys/systm.h>4344#include <fs/pseudofs/pseudofs.h>45#include <fs/pseudofs/pseudofs_internal.h>4647/*48* Initialize fileno bitmap49*/50void51pfs_fileno_init(struct pfs_info *pi)52{5354mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF);55pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex);56}5758/*59* Tear down fileno bitmap60*/61void62pfs_fileno_uninit(struct pfs_info *pi)63{6465delete_unrhdr(pi->pi_unrhdr);66pi->pi_unrhdr = NULL;67mtx_destroy(&pi->pi_mutex);68}6970/*71* Allocate a file number72*/73void74pfs_fileno_alloc(struct pfs_node *pn)75{7677if (pn->pn_parent)78PFS_TRACE(("%s/%s", pn->pn_parent->pn_name, pn->pn_name));79else80PFS_TRACE(("%s", pn->pn_name));81pfs_assert_not_owned(pn);8283switch (pn->pn_type) {84case pfstype_root:85/* root must always be 2 */86pn->pn_fileno = 2;87break;88case pfstype_dir:89case pfstype_file:90case pfstype_symlink:91case pfstype_procdir:92pn->pn_fileno = alloc_unr(pn->pn_info->pi_unrhdr);93break;94case pfstype_this:95KASSERT(pn->pn_parent != NULL,96("%s(): pfstype_this node has no parent", __func__));97pn->pn_fileno = pn->pn_parent->pn_fileno;98break;99case pfstype_parent:100KASSERT(pn->pn_parent != NULL,101("%s(): pfstype_parent node has no parent", __func__));102if (pn->pn_parent->pn_type == pfstype_root) {103pn->pn_fileno = pn->pn_parent->pn_fileno;104break;105}106KASSERT(pn->pn_parent->pn_parent != NULL,107("%s(): pfstype_parent node has no grandparent", __func__));108pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;109break;110case pfstype_none:111KASSERT(0,112("%s(): pfstype_none node", __func__));113break;114}115116#if 0117printf("%s(): %s: ", __func__, pn->pn_info->pi_name);118if (pn->pn_parent) {119if (pn->pn_parent->pn_parent) {120printf("%s/", pn->pn_parent->pn_parent->pn_name);121}122printf("%s/", pn->pn_parent->pn_name);123}124printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);125#endif126}127128/*129* Release a file number130*/131void132pfs_fileno_free(struct pfs_node *pn)133{134135pfs_assert_not_owned(pn);136137switch (pn->pn_type) {138case pfstype_root:139/* not allocated from unrhdr */140return;141case pfstype_dir:142case pfstype_file:143case pfstype_symlink:144case pfstype_procdir:145free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno);146break;147case pfstype_this:148case pfstype_parent:149/* ignore these, as they don't "own" their file number */150break;151case pfstype_none:152KASSERT(0,153("pfs_fileno_free() called for pfstype_none node"));154break;155}156}157158159