1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001 Dag-Erling Smørgrav 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 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 */ 51 void 52 pfs_fileno_init(struct pfs_info *pi) 53 { 54 55 mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF); 56 pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex); 57 } 58 59 /* 60 * Tear down fileno bitmap 61 */ 62 void 63 pfs_fileno_uninit(struct pfs_info *pi) 64 { 65 66 delete_unrhdr(pi->pi_unrhdr); 67 pi->pi_unrhdr = NULL; 68 mtx_destroy(&pi->pi_mutex); 69 } 70 71 /* 72 * Allocate a file number 73 */ 74 void 75 pfs_fileno_alloc(struct pfs_node *pn) 76 { 77 78 if (pn->pn_parent) 79 PFS_TRACE(("%s/%s", pn->pn_parent->pn_name, pn->pn_name)); 80 else 81 PFS_TRACE(("%s", pn->pn_name)); 82 pfs_assert_not_owned(pn); 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 ("%s(): pfstype_this node has no parent", __func__)); 98 pn->pn_fileno = pn->pn_parent->pn_fileno; 99 break; 100 case pfstype_parent: 101 KASSERT(pn->pn_parent != NULL, 102 ("%s(): pfstype_parent node has no parent", __func__)); 103 if (pn->pn_parent->pn_type == pfstype_root) { 104 pn->pn_fileno = pn->pn_parent->pn_fileno; 105 break; 106 } 107 KASSERT(pn->pn_parent->pn_parent != NULL, 108 ("%s(): pfstype_parent node has no grandparent", __func__)); 109 pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno; 110 break; 111 case pfstype_none: 112 KASSERT(0, 113 ("%s(): pfstype_none node", __func__)); 114 break; 115 } 116 117 #if 0 118 printf("%s(): %s: ", __func__, 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 */ 132 void 133 pfs_fileno_free(struct pfs_node *pn) 134 { 135 136 pfs_assert_not_owned(pn); 137 138 switch (pn->pn_type) { 139 case pfstype_root: 140 /* not allocated from unrhdr */ 141 return; 142 case pfstype_dir: 143 case pfstype_file: 144 case pfstype_symlink: 145 case pfstype_procdir: 146 free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno); 147 break; 148 case pfstype_this: 149 case pfstype_parent: 150 /* ignore these, as they don't "own" their file number */ 151 break; 152 case pfstype_none: 153 KASSERT(0, 154 ("pfs_fileno_free() called for pfstype_none node")); 155 break; 156 } 157 } 158