1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2001 Dag-Erling Coïdan 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 __FBSDID("$FreeBSD$");
33
34 #include "opt_pseudofs.h"
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46
47 #include <fs/pseudofs/pseudofs.h>
48 #include <fs/pseudofs/pseudofs_internal.h>
49
50 /*
51 * Initialize fileno bitmap
52 */
53 void
54 pfs_fileno_init(struct pfs_info *pi)
55 {
56
57 mtx_init(&pi->pi_mutex, "pfs_fileno", NULL, MTX_DEF);
58 pi->pi_unrhdr = new_unrhdr(3, INT_MAX / NO_PID, &pi->pi_mutex);
59 }
60
61 /*
62 * Tear down fileno bitmap
63 */
64 void
65 pfs_fileno_uninit(struct pfs_info *pi)
66 {
67
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 */
76 void
77 pfs_fileno_alloc(struct pfs_node *pn)
78 {
79
80 if (pn->pn_parent)
81 PFS_TRACE(("%s/%s", pn->pn_parent->pn_name, pn->pn_name));
82 else
83 PFS_TRACE(("%s", pn->pn_name));
84 pfs_assert_not_owned(pn);
85
86 switch (pn->pn_type) {
87 case pfstype_root:
88 /* root must always be 2 */
89 pn->pn_fileno = 2;
90 break;
91 case pfstype_dir:
92 case pfstype_file:
93 case pfstype_symlink:
94 case pfstype_procdir:
95 pn->pn_fileno = alloc_unr(pn->pn_info->pi_unrhdr);
96 break;
97 case pfstype_this:
98 KASSERT(pn->pn_parent != NULL,
99 ("%s(): pfstype_this node has no parent", __func__));
100 pn->pn_fileno = pn->pn_parent->pn_fileno;
101 break;
102 case pfstype_parent:
103 KASSERT(pn->pn_parent != NULL,
104 ("%s(): pfstype_parent node has no parent", __func__));
105 if (pn->pn_parent->pn_type == pfstype_root) {
106 pn->pn_fileno = pn->pn_parent->pn_fileno;
107 break;
108 }
109 KASSERT(pn->pn_parent->pn_parent != NULL,
110 ("%s(): pfstype_parent node has no grandparent", __func__));
111 pn->pn_fileno = pn->pn_parent->pn_parent->pn_fileno;
112 break;
113 case pfstype_none:
114 KASSERT(0,
115 ("%s(): pfstype_none node", __func__));
116 break;
117 }
118
119 #if 0
120 printf("%s(): %s: ", __func__, pn->pn_info->pi_name);
121 if (pn->pn_parent) {
122 if (pn->pn_parent->pn_parent) {
123 printf("%s/", pn->pn_parent->pn_parent->pn_name);
124 }
125 printf("%s/", pn->pn_parent->pn_name);
126 }
127 printf("%s -> %d\n", pn->pn_name, pn->pn_fileno);
128 #endif
129 }
130
131 /*
132 * Release a file number
133 */
134 void
135 pfs_fileno_free(struct pfs_node *pn)
136 {
137
138 pfs_assert_not_owned(pn);
139
140 switch (pn->pn_type) {
141 case pfstype_root:
142 /* not allocated from unrhdr */
143 return;
144 case pfstype_dir:
145 case pfstype_file:
146 case pfstype_symlink:
147 case pfstype_procdir:
148 free_unr(pn->pn_info->pi_unrhdr, pn->pn_fileno);
149 break;
150 case pfstype_this:
151 case pfstype_parent:
152 /* ignore these, as they don't "own" their file number */
153 break;
154 case pfstype_none:
155 KASSERT(0,
156 ("pfs_fileno_free() called for pfstype_none node"));
157 break;
158 }
159 }
Cache object: 4547b2a1da144f009400139f430e012b
|