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,
55 #if PFS_FSNAMELEN != MFSNAMELEN
56 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
60 * Add a node to a directory
63 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node)
65 KASSERT(parent != NULL,
66 ("%s(): parent is NULL", __func__));
67 KASSERT(parent->pn_info != NULL,
68 ("%s(): parent has no pn_info", __func__));
69 KASSERT(parent->pn_type == pfstype_dir ||
70 parent->pn_type == pfstype_procdir ||
71 parent->pn_type == pfstype_root,
72 ("%s(): parent is not a directory", __func__));
74 /* XXX should check for duplicate names etc. */
76 mtx_lock(&parent->pn_info->pi_mutex);
77 node->pn_info = parent->pn_info;
78 node->pn_parent = parent;
79 node->pn_next = parent->pn_nodes;
80 parent->pn_nodes = node;
81 /* Propagate flag to all child nodes (and thus their vnodes) */
82 if ((parent->pn_flags & PFS_PROCDEP) != 0)
83 node->pn_flags |= PFS_PROCDEP;
84 mtx_unlock(&parent->pn_info->pi_mutex);
90 * Add . and .. to a directory
93 _pfs_fixup_dir(struct pfs_node *parent)
97 MALLOC(dir, struct pfs_node *, sizeof *dir,
98 M_PFSNODES, M_WAITOK|M_ZERO);
99 dir->pn_name[0] = '.';
100 dir->pn_type = pfstype_this;
102 if (_pfs_add_node(parent, dir) != 0) {
103 FREE(dir, M_PFSNODES);
107 MALLOC(dir, struct pfs_node *, sizeof *dir,
108 M_PFSNODES, M_WAITOK|M_ZERO);
109 dir->pn_name[0] = dir->pn_name[1] = '.';
110 dir->pn_type = pfstype_parent;
112 if (_pfs_add_node(parent, dir) != 0) {
113 FREE(dir, M_PFSNODES);
124 pfs_create_dir(struct pfs_node *parent, const char *name,
125 pfs_attr_t attr, pfs_vis_t vis, int flags)
127 struct pfs_node *dir;
129 KASSERT(strlen(name) < PFS_NAMELEN,
130 ("%s(): node name is too long", __func__));
132 MALLOC(dir, struct pfs_node *, sizeof *dir,
133 M_PFSNODES, M_WAITOK|M_ZERO);
134 strcpy(dir->pn_name, name);
135 dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir;
138 dir->pn_flags = flags;
140 if (_pfs_add_node(parent, dir) != 0) {
141 FREE(dir, M_PFSNODES);
145 if (_pfs_fixup_dir(dir) != 0) {
157 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
158 pfs_attr_t attr, pfs_vis_t vis, int flags)
160 struct pfs_node *node;
162 KASSERT(strlen(name) < PFS_NAMELEN,
163 ("%s(): node name is too long", __func__));
165 MALLOC(node, struct pfs_node *, sizeof *node,
166 M_PFSNODES, M_WAITOK|M_ZERO);
167 strcpy(node->pn_name, name);
168 node->pn_type = pfstype_file;
169 node->pn_func = fill;
170 node->pn_attr = attr;
172 node->pn_flags = flags;
174 if (_pfs_add_node(parent, node) != 0) {
175 FREE(node, M_PFSNODES);
186 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
187 pfs_attr_t attr, pfs_vis_t vis, int flags)
189 struct pfs_node *node;
191 node = pfs_create_file(parent, name, fill, attr, vis, flags);
194 node->pn_type = pfstype_symlink;
199 * Locate a node by name
202 pfs_find_node(struct pfs_node *parent, const char *name)
204 struct pfs_node *node;
206 for (node = parent->pn_nodes; node != NULL; node = node->pn_next)
207 if (strcmp(node->pn_name, name) == 0)
213 * Destroy a node or a tree of nodes
216 pfs_destroy(struct pfs_node *node)
218 struct pfs_node *parent, *rover;
220 KASSERT(node != NULL,
221 ("%s(): node is NULL", __func__));
222 KASSERT(node->pn_info != NULL,
223 ("%s(): node has no pn_info", __func__));
225 /* destroy children */
226 if (node->pn_type == pfstype_dir ||
227 node->pn_type == pfstype_procdir ||
228 node->pn_type == pfstype_root)
229 while (node->pn_nodes != NULL)
230 pfs_destroy(node->pn_nodes);
232 /* unlink from parent */
233 if ((parent = node->pn_parent) != NULL) {
234 KASSERT(parent->pn_info == node->pn_info,
235 ("%s(): parent has different pn_info", __func__));
236 mtx_lock(&node->pn_info->pi_mutex);
237 if (parent->pn_nodes == node) {
238 parent->pn_nodes = node->pn_next;
240 rover = parent->pn_nodes;
241 while (rover->pn_next != NULL) {
242 if (rover->pn_next == node) {
243 rover->pn_next = node->pn_next;
246 rover = rover->pn_next;
249 mtx_unlock(&node->pn_info->pi_mutex);
252 /* revoke vnodes and release memory */
254 FREE(node, M_PFSNODES);
260 * Mount a pseudofs instance
263 pfs_mount(struct pfs_info *pi, struct mount *mp, struct thread *td)
267 if (mp->mnt_flag & MNT_UPDATE)
270 mp->mnt_flag |= MNT_LOCAL;
271 mp->mnt_data = (qaddr_t)pi;
275 vfs_mountedfrom(mp, pi->pi_name);
276 sbp->f_bsize = PAGE_SIZE;
277 sbp->f_iosize = PAGE_SIZE;
288 * Unmount a pseudofs instance
291 pfs_unmount(struct mount *mp, int mntflags, struct thread *td)
296 pi = (struct pfs_info *)mp->mnt_data;
298 /* XXX do stuff with pi... */
300 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, td);
305 * Return a root vnode
308 pfs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
312 pi = (struct pfs_info *)mp->mnt_data;
313 return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID);
317 * Return filesystem stats
320 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
322 /* no-op: always called with mp->mnt_stat */
327 * Initialize a pseudofs instance
330 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
332 struct pfs_node *root;
335 mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF);
337 /* set up the root diretory */
338 MALLOC(root, struct pfs_node *, sizeof *root,
339 M_PFSNODES, M_WAITOK|M_ZERO);
340 root->pn_type = pfstype_root;
341 root->pn_name[0] = '/';
343 if (_pfs_fixup_dir(root) != 0) {
344 FREE(root, M_PFSNODES);
345 return (ENODEV); /* XXX not really the right errno */
349 /* construct file hierarchy */
350 error = (pi->pi_init)(pi, vfc);
354 mtx_destroy(&pi->pi_mutex);
360 printf("%s registered\n", pi->pi_name);
365 * Destroy a pseudofs instance
368 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
372 pfs_fileno_uninit(pi);
373 pfs_destroy(pi->pi_root);
375 mtx_destroy(&pi->pi_mutex);
377 printf("%s unregistered\n", pi->pi_name);
378 error = (pi->pi_uninit)(pi, vfc);
383 * Handle load / unload events
386 pfs_modevent(module_t mod, int evt, void *arg)
394 pfs_vncache_unload();
406 static moduledata_t pseudofs_data = {
411 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
412 MODULE_VERSION(pseudofs, 1);