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.
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mount.h>
38 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
44 #include <fs/pseudofs/pseudofs.h>
45 #include <fs/pseudofs/pseudofs_internal.h>
47 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");
49 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0,
52 #if PFS_FSNAMELEN != MFSNAMELEN
53 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
57 * Add a node to a directory
60 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node)
62 KASSERT(parent != NULL,
63 ("%s(): parent is NULL", __func__));
64 KASSERT(parent->pn_info != NULL,
65 ("%s(): parent has no pn_info", __func__));
66 KASSERT(parent->pn_type == pfstype_dir ||
67 parent->pn_type == pfstype_procdir ||
68 parent->pn_type == pfstype_root,
69 ("%s(): parent is not a directory", __func__));
71 /* XXX should check for duplicate names etc. */
73 mtx_lock(&parent->pn_info->pi_mutex);
74 node->pn_info = parent->pn_info;
75 node->pn_parent = parent;
76 node->pn_next = parent->pn_nodes;
77 parent->pn_nodes = node;
78 /* Propagate flag to all child nodes (and thus their vnodes) */
79 if ((parent->pn_flags & PFS_PROCDEP) != 0)
80 node->pn_flags |= PFS_PROCDEP;
81 mtx_unlock(&parent->pn_info->pi_mutex);
87 * Add . and .. to a directory
90 _pfs_fixup_dir(struct pfs_node *parent)
94 MALLOC(dir, struct pfs_node *, sizeof *dir,
95 M_PFSNODES, M_WAITOK|M_ZERO);
96 dir->pn_name[0] = '.';
97 dir->pn_type = pfstype_this;
99 if (_pfs_add_node(parent, dir) != 0) {
100 FREE(dir, M_PFSNODES);
104 MALLOC(dir, struct pfs_node *, sizeof *dir,
105 M_PFSNODES, M_WAITOK|M_ZERO);
106 dir->pn_name[0] = dir->pn_name[1] = '.';
107 dir->pn_type = pfstype_parent;
109 if (_pfs_add_node(parent, dir) != 0) {
110 FREE(dir, M_PFSNODES);
121 pfs_create_dir(struct pfs_node *parent, const char *name,
122 pfs_attr_t attr, pfs_vis_t vis, int flags)
124 struct pfs_node *dir;
126 KASSERT(strlen(name) < PFS_NAMELEN,
127 ("%s(): node name is too long", __func__));
129 MALLOC(dir, struct pfs_node *, sizeof *dir,
130 M_PFSNODES, M_WAITOK|M_ZERO);
131 strcpy(dir->pn_name, name);
132 dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir;
135 dir->pn_flags = flags;
137 if (_pfs_add_node(parent, dir) != 0) {
138 FREE(dir, M_PFSNODES);
142 if (_pfs_fixup_dir(dir) != 0) {
154 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
155 pfs_attr_t attr, pfs_vis_t vis, int flags)
157 struct pfs_node *node;
159 KASSERT(strlen(name) < PFS_NAMELEN,
160 ("%s(): node name is too long", __func__));
162 MALLOC(node, struct pfs_node *, sizeof *node,
163 M_PFSNODES, M_WAITOK|M_ZERO);
164 strcpy(node->pn_name, name);
165 node->pn_type = pfstype_file;
166 node->pn_func = fill;
167 node->pn_attr = attr;
169 node->pn_flags = flags;
171 if (_pfs_add_node(parent, node) != 0) {
172 FREE(node, M_PFSNODES);
183 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
184 pfs_attr_t attr, pfs_vis_t vis, int flags)
186 struct pfs_node *node;
188 node = pfs_create_file(parent, name, fill, attr, vis, flags);
191 node->pn_type = pfstype_symlink;
196 * Locate a node by name
199 pfs_find_node(struct pfs_node *parent, const char *name)
201 struct pfs_node *node;
203 for (node = parent->pn_nodes; node != NULL; node = node->pn_next)
204 if (strcmp(node->pn_name, name) == 0)
210 * Destroy a node or a tree of nodes
213 pfs_destroy(struct pfs_node *node)
215 struct pfs_node *parent, *rover;
217 KASSERT(node != NULL,
218 ("%s(): node is NULL", __func__));
219 KASSERT(node->pn_info != NULL,
220 ("%s(): node has no pn_info", __func__));
222 /* destroy children */
223 if (node->pn_type == pfstype_dir ||
224 node->pn_type == pfstype_procdir ||
225 node->pn_type == pfstype_root)
226 while (node->pn_nodes != NULL)
227 pfs_destroy(node->pn_nodes);
229 /* unlink from parent */
230 if ((parent = node->pn_parent) != NULL) {
231 KASSERT(parent->pn_info == node->pn_info,
232 ("%s(): parent has different pn_info", __func__));
233 mtx_lock(&node->pn_info->pi_mutex);
234 if (parent->pn_nodes == node) {
235 parent->pn_nodes = node->pn_next;
237 rover = parent->pn_nodes;
238 while (rover->pn_next != NULL) {
239 if (rover->pn_next == node) {
240 rover->pn_next = node->pn_next;
243 rover = rover->pn_next;
246 mtx_unlock(&node->pn_info->pi_mutex);
249 /* revoke vnodes and release memory */
251 FREE(node, M_PFSNODES);
257 * Mount a pseudofs instance
260 pfs_mount(struct pfs_info *pi, struct mount *mp, struct thread *td)
264 if (mp->mnt_flag & MNT_UPDATE)
267 mp->mnt_flag |= MNT_LOCAL;
268 mp->mnt_data = (qaddr_t)pi;
272 bcopy(pi->pi_name, sbp->f_mntfromname, sizeof pi->pi_name);
273 sbp->f_bsize = PAGE_SIZE;
274 sbp->f_iosize = PAGE_SIZE;
285 * Unmount a pseudofs instance
288 pfs_unmount(struct mount *mp, int mntflags, struct thread *td)
293 pi = (struct pfs_info *)mp->mnt_data;
295 /* XXX do stuff with pi... */
297 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, td);
302 * Return a root vnode
305 pfs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
309 pi = (struct pfs_info *)mp->mnt_data;
310 return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID);
314 * Return filesystem stats
317 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
319 bcopy(&mp->mnt_stat, sbp, sizeof *sbp);
324 * Initialize a pseudofs instance
327 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
329 struct pfs_node *root;
332 mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF);
334 /* set up the root diretory */
335 MALLOC(root, struct pfs_node *, sizeof *root,
336 M_PFSNODES, M_WAITOK|M_ZERO);
337 root->pn_type = pfstype_root;
338 root->pn_name[0] = '/';
340 if (_pfs_fixup_dir(root) != 0) {
341 FREE(root, M_PFSNODES);
342 return (ENODEV); /* XXX not really the right errno */
346 /* construct file hierarchy */
347 error = (pi->pi_init)(pi, vfc);
351 mtx_destroy(&pi->pi_mutex);
357 printf("%s registered\n", pi->pi_name);
362 * Destroy a pseudofs instance
365 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
369 pfs_fileno_uninit(pi);
370 pfs_destroy(pi->pi_root);
372 mtx_destroy(&pi->pi_mutex);
374 printf("%s unregistered\n", pi->pi_name);
375 error = (pi->pi_uninit)(pi, vfc);
380 * Handle load / unload events
383 pfs_modevent(module_t mod, int evt, void *arg)
392 pfs_vncache_unload();
405 static moduledata_t pseudofs_data = {
410 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
411 MODULE_VERSION(pseudofs, 1);