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,
57 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0,
58 "enable tracing of pseudofs vnode operations");
61 #if PFS_FSNAMELEN != MFSNAMELEN
62 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
66 * Allocate and initialize a node
68 static struct pfs_node *
69 pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type)
73 KASSERT(strlen(name) < PFS_NAMELEN,
74 ("%s(): node name is too long", __func__));
76 pn = malloc(sizeof *pn,
77 M_PFSNODES, M_WAITOK|M_ZERO);
78 mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK);
79 strlcpy(pn->pn_name, name, sizeof pn->pn_name);
86 * Add a node to a directory
89 pfs_add_node(struct pfs_node *parent, struct pfs_node *pn)
92 struct pfs_node *iter;
95 KASSERT(parent != NULL,
96 ("%s(): parent is NULL", __func__));
97 KASSERT(pn->pn_parent == NULL,
98 ("%s(): node already has a parent", __func__));
99 KASSERT(parent->pn_info != NULL,
100 ("%s(): parent has no pn_info", __func__));
101 KASSERT(parent->pn_type == pfstype_dir ||
102 parent->pn_type == pfstype_procdir ||
103 parent->pn_type == pfstype_root,
104 ("%s(): parent is not a directory", __func__));
107 /* XXX no locking! */
108 if (pn->pn_type == pfstype_procdir)
109 for (iter = parent; iter != NULL; iter = iter->pn_parent)
110 KASSERT(iter->pn_type != pfstype_procdir,
111 ("%s(): nested process directories", __func__));
112 for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) {
113 KASSERT(strcmp(pn->pn_name, iter->pn_name) != 0,
114 ("%s(): homonymous siblings", __func__));
115 if (pn->pn_type == pfstype_procdir)
116 KASSERT(iter->pn_type != pfstype_procdir,
117 ("%s(): sibling process directories", __func__));
121 pn->pn_parent = parent;
122 pfs_fileno_alloc(pn);
125 pn->pn_next = parent->pn_nodes;
126 if ((parent->pn_flags & PFS_PROCDEP) != 0)
127 pn->pn_flags |= PFS_PROCDEP;
128 parent->pn_nodes = pn;
133 * Detach a node from its aprent
136 pfs_detach_node(struct pfs_node *pn)
138 struct pfs_node *parent = pn->pn_parent;
139 struct pfs_node **iter;
141 KASSERT(parent != NULL, ("%s(): node has no parent", __func__));
142 KASSERT(parent->pn_info == pn->pn_info,
143 ("%s(): parent has different pn_info", __func__));
146 iter = &parent->pn_nodes;
147 while (*iter != NULL) {
152 iter = &(*iter)->pn_next;
154 pn->pn_parent = NULL;
159 * Add . and .. to a directory
162 pfs_fixup_dir(struct pfs_node *parent)
166 pn = pfs_alloc_node(parent->pn_info, ".", pfstype_this);
167 pfs_add_node(parent, pn);
168 pn = pfs_alloc_node(parent->pn_info, "..", pfstype_parent);
169 pfs_add_node(parent, pn);
176 pfs_create_dir(struct pfs_node *parent, const char *name,
177 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
182 pn = pfs_alloc_node(parent->pn_info, name,
183 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir);
186 pn->pn_destroy = destroy;
187 pn->pn_flags = flags;
188 pfs_add_node(parent, pn);
198 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
199 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
204 pn = pfs_alloc_node(parent->pn_info, name, pfstype_file);
208 pn->pn_destroy = destroy;
209 pn->pn_flags = flags;
210 pfs_add_node(parent, pn);
219 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
220 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
225 pn = pfs_alloc_node(parent->pn_info, name, pfstype_symlink);
229 pn->pn_destroy = destroy;
230 pn->pn_flags = flags;
231 pfs_add_node(parent, pn);
237 * Locate a node by name
240 pfs_find_node(struct pfs_node *parent, const char *name)
245 for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next)
246 if (strcmp(pn->pn_name, name) == 0)
253 * Destroy a node and all its descendants. If the node to be destroyed
254 * has a parent, the parent's mutex must be held.
257 pfs_destroy(struct pfs_node *pn)
259 struct pfs_node *iter;
262 ("%s(): node is NULL", __func__));
263 KASSERT(pn->pn_info != NULL,
264 ("%s(): node has no pn_info", __func__));
269 /* destroy children */
270 if (pn->pn_type == pfstype_dir ||
271 pn->pn_type == pfstype_procdir ||
272 pn->pn_type == pfstype_root) {
274 while (pn->pn_nodes != NULL) {
276 pn->pn_nodes = iter->pn_next;
277 iter->pn_parent = NULL;
285 /* revoke vnodes and fileno */
288 /* callback to free any private resources */
289 if (pn->pn_destroy != NULL)
292 /* destroy the node */
294 mtx_destroy(&pn->pn_mutex);
295 free(pn, M_PFSNODES);
301 * Mount a pseudofs instance
304 pfs_mount(struct pfs_info *pi, struct mount *mp)
308 if (mp->mnt_flag & MNT_UPDATE)
312 mp->mnt_flag |= MNT_LOCAL;
318 vfs_mountedfrom(mp, pi->pi_name);
319 sbp->f_bsize = PAGE_SIZE;
320 sbp->f_iosize = PAGE_SIZE;
331 * Compatibility shim for old mount(2) system call
334 pfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
338 error = kernel_mount(ma, flags);
343 * Unmount a pseudofs instance
346 pfs_unmount(struct mount *mp, int mntflags)
350 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
356 * Return a root vnode
359 pfs_root(struct mount *mp, int flags, struct vnode **vpp)
363 pi = (struct pfs_info *)mp->mnt_data;
364 return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID));
368 * Return filesystem stats
371 pfs_statfs(struct mount *mp, struct statfs *sbp)
373 /* no-op: always called with mp->mnt_stat */
378 * Initialize a pseudofs instance
381 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
383 struct pfs_node *root;
388 /* set up the root directory */
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 pfs_destroy(pi->pi_root);
417 pfs_fileno_uninit(pi);
419 printf("%s unregistered\n", pi->pi_name);
420 error = (pi->pi_uninit)(pi, vfc);
425 * Handle load / unload events
428 pfs_modevent(module_t mod, int evt, void *arg)
436 pfs_vncache_unload();
448 static moduledata_t pseudofs_data = {
453 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
454 MODULE_VERSION(pseudofs, 1);