2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_pseudofs.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
49 #include <fs/pseudofs/pseudofs.h>
50 #include <fs/pseudofs/pseudofs_internal.h>
52 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");
54 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0,
59 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0,
60 "enable tracing of pseudofs vnode operations");
63 #if PFS_FSNAMELEN != MFSNAMELEN
64 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
68 * Allocate and initialize a node
70 static struct pfs_node *
71 pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type)
75 KASSERT(strlen(name) < PFS_NAMELEN,
76 ("%s(): node name is too long", __func__));
78 pn = malloc(sizeof *pn,
79 M_PFSNODES, M_WAITOK|M_ZERO);
80 mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK);
81 strlcpy(pn->pn_name, name, sizeof pn->pn_name);
88 * Add a node to a directory
91 pfs_add_node(struct pfs_node *parent, struct pfs_node *pn)
94 struct pfs_node *iter;
97 KASSERT(parent != NULL,
98 ("%s(): parent is NULL", __func__));
99 KASSERT(pn->pn_parent == NULL,
100 ("%s(): node already has a parent", __func__));
101 KASSERT(parent->pn_info != NULL,
102 ("%s(): parent has no pn_info", __func__));
103 KASSERT(parent->pn_type == pfstype_dir ||
104 parent->pn_type == pfstype_procdir ||
105 parent->pn_type == pfstype_root,
106 ("%s(): parent is not a directory", __func__));
109 /* XXX no locking! */
110 if (pn->pn_type == pfstype_procdir)
111 for (iter = parent; iter != NULL; iter = iter->pn_parent)
112 KASSERT(iter->pn_type != pfstype_procdir,
113 ("%s(): nested process directories", __func__));
114 for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) {
115 KASSERT(strcmp(pn->pn_name, iter->pn_name) != 0,
116 ("%s(): homonymous siblings", __func__));
117 if (pn->pn_type == pfstype_procdir)
118 KASSERT(iter->pn_type != pfstype_procdir,
119 ("%s(): sibling process directories", __func__));
123 pn->pn_parent = parent;
124 pfs_fileno_alloc(pn);
127 pn->pn_next = parent->pn_nodes;
128 if ((parent->pn_flags & PFS_PROCDEP) != 0)
129 pn->pn_flags |= PFS_PROCDEP;
130 parent->pn_nodes = pn;
135 * Detach a node from its aprent
138 pfs_detach_node(struct pfs_node *pn)
140 struct pfs_node *parent = pn->pn_parent;
141 struct pfs_node **iter;
143 KASSERT(parent != NULL, ("%s(): node has no parent", __func__));
144 KASSERT(parent->pn_info == pn->pn_info,
145 ("%s(): parent has different pn_info", __func__));
148 iter = &parent->pn_nodes;
149 while (*iter != NULL) {
154 iter = &(*iter)->pn_next;
156 pn->pn_parent = NULL;
161 * Add . and .. to a directory
164 pfs_fixup_dir(struct pfs_node *parent)
168 pn = pfs_alloc_node(parent->pn_info, ".", pfstype_this);
169 pfs_add_node(parent, pn);
170 pn = pfs_alloc_node(parent->pn_info, "..", pfstype_parent);
171 pfs_add_node(parent, pn);
178 pfs_create_dir(struct pfs_node *parent, const char *name,
179 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
184 pn = pfs_alloc_node(parent->pn_info, name,
185 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir);
188 pn->pn_destroy = destroy;
189 pn->pn_flags = flags;
190 pfs_add_node(parent, pn);
200 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
201 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
206 pn = pfs_alloc_node(parent->pn_info, name, pfstype_file);
210 pn->pn_destroy = destroy;
211 pn->pn_flags = flags;
212 pfs_add_node(parent, pn);
221 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
222 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
227 pn = pfs_alloc_node(parent->pn_info, name, pfstype_symlink);
231 pn->pn_destroy = destroy;
232 pn->pn_flags = flags;
233 pfs_add_node(parent, pn);
239 * Locate a node by name
242 pfs_find_node(struct pfs_node *parent, const char *name)
247 for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next)
248 if (strcmp(pn->pn_name, name) == 0)
255 * Destroy a node and all its descendants. If the node to be destroyed
256 * has a parent, the parent's mutex must be held.
259 pfs_destroy(struct pfs_node *pn)
261 struct pfs_node *iter;
264 ("%s(): node is NULL", __func__));
265 KASSERT(pn->pn_info != NULL,
266 ("%s(): node has no pn_info", __func__));
271 /* destroy children */
272 if (pn->pn_type == pfstype_dir ||
273 pn->pn_type == pfstype_procdir ||
274 pn->pn_type == pfstype_root) {
276 while (pn->pn_nodes != NULL) {
278 pn->pn_nodes = iter->pn_next;
279 iter->pn_parent = NULL;
287 /* revoke vnodes and fileno */
290 /* callback to free any private resources */
291 if (pn->pn_destroy != NULL)
294 /* destroy the node */
296 mtx_destroy(&pn->pn_mutex);
297 free(pn, M_PFSNODES);
303 * Mount a pseudofs instance
306 pfs_mount(struct pfs_info *pi, struct mount *mp)
310 if (mp->mnt_flag & MNT_UPDATE)
314 mp->mnt_flag |= MNT_LOCAL;
320 vfs_mountedfrom(mp, pi->pi_name);
321 sbp->f_bsize = PAGE_SIZE;
322 sbp->f_iosize = PAGE_SIZE;
333 * Compatibility shim for old mount(2) system call
336 pfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
340 error = kernel_mount(ma, flags);
345 * Unmount a pseudofs instance
348 pfs_unmount(struct mount *mp, int mntflags)
352 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
358 * Return a root vnode
361 pfs_root(struct mount *mp, int flags, struct vnode **vpp)
365 pi = (struct pfs_info *)mp->mnt_data;
366 return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID));
370 * Return filesystem stats
373 pfs_statfs(struct mount *mp, struct statfs *sbp)
375 /* no-op: always called with mp->mnt_stat */
380 * Initialize a pseudofs instance
383 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
385 struct pfs_node *root;
390 /* set up the root directory */
391 root = pfs_alloc_node(pi, "/", pfstype_root);
393 pfs_fileno_alloc(root);
396 /* construct file hierarchy */
397 error = (pi->pi_init)(pi, vfc);
405 printf("%s registered\n", pi->pi_name);
410 * Destroy a pseudofs instance
413 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
417 pfs_destroy(pi->pi_root);
419 pfs_fileno_uninit(pi);
421 printf("%s unregistered\n", pi->pi_name);
422 error = (pi->pi_uninit)(pi, vfc);
427 * Handle load / unload events
430 pfs_modevent(module_t mod, int evt, void *arg)
438 pfs_vncache_unload();
450 static moduledata_t pseudofs_data = {
455 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
456 MODULE_VERSION(pseudofs, 1);