2 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_pseudofs.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mount.h>
41 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
47 #include <fs/pseudofs/pseudofs.h>
48 #include <fs/pseudofs/pseudofs_internal.h>
50 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");
52 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0,
56 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0,
57 "enable tracing of pseudofs vnode operations");
59 #if PFS_FSNAMELEN != MFSNAMELEN
60 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
64 * Allocate and initialize a node
66 static struct pfs_node *
67 pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type)
71 KASSERT(strlen(name) < PFS_NAMELEN,
72 ("%s(): node name is too long", __func__));
74 pn = malloc(sizeof *pn,
75 M_PFSNODES, M_WAITOK|M_ZERO);
76 mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK);
77 strlcpy(pn->pn_name, name, sizeof pn->pn_name);
84 * Add a node to a directory
87 pfs_add_node(struct pfs_node *parent, struct pfs_node *pn)
90 struct pfs_node *iter;
93 KASSERT(parent != NULL,
94 ("%s(): parent is NULL", __func__));
95 KASSERT(pn->pn_parent == NULL,
96 ("%s(): node already has a parent", __func__));
97 KASSERT(parent->pn_info != NULL,
98 ("%s(): parent has no pn_info", __func__));
99 KASSERT(parent->pn_type == pfstype_dir ||
100 parent->pn_type == pfstype_procdir ||
101 parent->pn_type == pfstype_root,
102 ("%s(): parent is not a directory", __func__));
105 /* XXX no locking! */
106 if (pn->pn_type == pfstype_procdir)
107 for (iter = parent; iter != NULL; iter = iter->pn_parent)
108 KASSERT(iter->pn_type != pfstype_procdir,
109 ("%s(): nested process directories", __func__));
110 for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) {
111 KASSERT(strcmp(pn->pn_name, iter->pn_name) != 0,
112 ("%s(): homonymous siblings", __func__));
113 if (pn->pn_type == pfstype_procdir)
114 KASSERT(iter->pn_type != pfstype_procdir,
115 ("%s(): sibling process directories", __func__));
119 pn->pn_parent = parent;
120 pfs_fileno_alloc(pn);
123 pn->pn_next = parent->pn_nodes;
124 if ((parent->pn_flags & PFS_PROCDEP) != 0)
125 pn->pn_flags |= PFS_PROCDEP;
126 parent->pn_nodes = pn;
131 * Detach a node from its aprent
134 pfs_detach_node(struct pfs_node *pn)
136 struct pfs_node *parent = pn->pn_parent;
137 struct pfs_node **iter;
139 KASSERT(parent != NULL, ("%s(): node has no parent", __func__));
140 KASSERT(parent->pn_info == pn->pn_info,
141 ("%s(): parent has different pn_info", __func__));
144 iter = &parent->pn_nodes;
145 while (*iter != NULL) {
150 iter = &(*iter)->pn_next;
152 pn->pn_parent = NULL;
157 * Add . and .. to a directory
160 pfs_fixup_dir(struct pfs_node *parent)
164 pn = pfs_alloc_node(parent->pn_info, ".", pfstype_this);
165 pfs_add_node(parent, pn);
166 pn = pfs_alloc_node(parent->pn_info, "..", pfstype_parent);
167 pfs_add_node(parent, pn);
174 pfs_create_dir(struct pfs_node *parent, const char *name,
175 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
180 pn = pfs_alloc_node(parent->pn_info, name,
181 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir);
184 pn->pn_destroy = destroy;
185 pn->pn_flags = flags;
186 pfs_add_node(parent, pn);
196 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
197 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
202 pn = pfs_alloc_node(parent->pn_info, name, pfstype_file);
206 pn->pn_destroy = destroy;
207 pn->pn_flags = flags;
208 pfs_add_node(parent, pn);
217 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
218 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
223 pn = pfs_alloc_node(parent->pn_info, name, pfstype_symlink);
227 pn->pn_destroy = destroy;
228 pn->pn_flags = flags;
229 pfs_add_node(parent, pn);
235 * Locate a node by name
238 pfs_find_node(struct pfs_node *parent, const char *name)
243 for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next)
244 if (strcmp(pn->pn_name, name) == 0)
251 * Destroy a node and all its descendants. If the node to be destroyed
252 * has a parent, the parent's mutex must be held.
255 pfs_destroy(struct pfs_node *pn)
257 struct pfs_node *iter;
260 ("%s(): node is NULL", __func__));
261 KASSERT(pn->pn_info != NULL,
262 ("%s(): node has no pn_info", __func__));
267 /* destroy children */
268 if (pn->pn_type == pfstype_dir ||
269 pn->pn_type == pfstype_procdir ||
270 pn->pn_type == pfstype_root) {
272 while (pn->pn_nodes != NULL) {
274 pn->pn_nodes = iter->pn_next;
275 iter->pn_parent = NULL;
283 /* revoke vnodes and fileno */
286 /* callback to free any private resources */
287 if (pn->pn_destroy != NULL)
290 /* destroy the node */
292 mtx_destroy(&pn->pn_mutex);
293 free(pn, M_PFSNODES);
299 * Mount a pseudofs instance
302 pfs_mount(struct pfs_info *pi, struct mount *mp)
306 if (mp->mnt_flag & MNT_UPDATE)
310 mp->mnt_flag |= MNT_LOCAL;
316 vfs_mountedfrom(mp, pi->pi_name);
317 sbp->f_bsize = PAGE_SIZE;
318 sbp->f_iosize = PAGE_SIZE;
329 * Compatibility shim for old mount(2) system call
332 pfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
336 error = kernel_mount(ma, flags);
341 * Unmount a pseudofs instance
344 pfs_unmount(struct mount *mp, int mntflags)
348 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
354 * Return a root vnode
357 pfs_root(struct mount *mp, int flags, struct vnode **vpp)
361 pi = (struct pfs_info *)mp->mnt_data;
362 return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID));
366 * Return filesystem stats
369 pfs_statfs(struct mount *mp, struct statfs *sbp)
371 /* no-op: always called with mp->mnt_stat */
376 * Initialize a pseudofs instance
379 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
381 struct pfs_node *root;
384 mtx_assert(&Giant, MA_OWNED);
388 /* set up the root diretory */
389 root = pfs_alloc_node(pi, "/", pfstype_root);
391 pfs_fileno_alloc(root);
394 /* construct file hierarchy */
395 error = (pi->pi_init)(pi, vfc);
403 printf("%s registered\n", pi->pi_name);
408 * Destroy a pseudofs instance
411 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
415 mtx_assert(&Giant, MA_OWNED);
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);