]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/pseudofs/pseudofs.c
Don't attempt to recurse lockmgr, it doesn't like it.
[FreeBSD/FreeBSD.git] / sys / fs / pseudofs / pseudofs.c
1 /*-
2  * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_pseudofs.h"
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mount.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/sbuf.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
46
47 #include <fs/pseudofs/pseudofs.h>
48 #include <fs/pseudofs/pseudofs_internal.h>
49
50 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");
51
52 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0,
53     "pseudofs");
54
55 #if PFS_FSNAMELEN != MFSNAMELEN
56 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
57 #endif
58
59 /*
60  * Add a node to a directory
61  */
62 static int
63 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node)
64 {
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__));
73
74         /* XXX should check for duplicate names etc. */
75
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);
85
86         return (0);
87 }
88
89 /*
90  * Add . and .. to a directory
91  */
92 static int
93 _pfs_fixup_dir(struct pfs_node *parent)
94 {
95         struct pfs_node *dir;
96
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;
101
102         if (_pfs_add_node(parent, dir) != 0) {
103                 FREE(dir, M_PFSNODES);
104                 return (-1);
105         }
106
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;
111
112         if (_pfs_add_node(parent, dir) != 0) {
113                 FREE(dir, M_PFSNODES);
114                 return (-1);
115         }
116
117         return (0);
118 }
119
120 /*
121  * Create a directory
122  */
123 struct pfs_node *
124 pfs_create_dir(struct pfs_node *parent, const char *name,
125                pfs_attr_t attr, pfs_vis_t vis, int flags)
126 {
127         struct pfs_node *dir;
128
129         KASSERT(strlen(name) < PFS_NAMELEN,
130             ("%s(): node name is too long", __func__));
131
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;
136         dir->pn_attr = attr;
137         dir->pn_vis = vis;
138         dir->pn_flags = flags;
139
140         if (_pfs_add_node(parent, dir) != 0) {
141                 FREE(dir, M_PFSNODES);
142                 return (NULL);
143         }
144
145         if (_pfs_fixup_dir(dir) != 0) {
146                 pfs_destroy(dir);
147                 return (NULL);
148         }
149
150         return (dir);
151 }
152
153 /*
154  * Create a file
155  */
156 struct pfs_node *
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)
159 {
160         struct pfs_node *node;
161
162         KASSERT(strlen(name) < PFS_NAMELEN,
163             ("%s(): node name is too long", __func__));
164
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;
171         node->pn_vis = vis;
172         node->pn_flags = flags;
173
174         if (_pfs_add_node(parent, node) != 0) {
175                 FREE(node, M_PFSNODES);
176                 return (NULL);
177         }
178
179         return (node);
180 }
181
182 /*
183  * Create a symlink
184  */
185 struct pfs_node *
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)
188 {
189         struct pfs_node *node;
190
191         node = pfs_create_file(parent, name, fill, attr, vis, flags);
192         if (node == NULL)
193                 return (NULL);
194         node->pn_type = pfstype_symlink;
195         return (node);
196 }
197
198 /*
199  * Locate a node by name
200  */
201 struct pfs_node *
202 pfs_find_node(struct pfs_node *parent, const char *name)
203 {
204         struct pfs_node *node;
205
206         for (node = parent->pn_nodes; node != NULL; node = node->pn_next)
207                 if (strcmp(node->pn_name, name) == 0)
208                         return (node);
209         return (NULL);
210 }
211
212 /*
213  * Destroy a node or a tree of nodes
214  */
215 int
216 pfs_destroy(struct pfs_node *node)
217 {
218         struct pfs_node *parent, *rover;
219
220         KASSERT(node != NULL,
221             ("%s(): node is NULL", __func__));
222         KASSERT(node->pn_info != NULL,
223             ("%s(): node has no pn_info", __func__));
224
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);
231
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;
239                 } else {
240                         rover = parent->pn_nodes;
241                         while (rover->pn_next != NULL) {
242                                 if (rover->pn_next == node) {
243                                         rover->pn_next = node->pn_next;
244                                         break;
245                                 }
246                                 rover = rover->pn_next;
247                         }
248                 }
249                 mtx_unlock(&node->pn_info->pi_mutex);
250         }
251
252         /* revoke vnodes and release memory */
253         pfs_disable(node);
254         FREE(node, M_PFSNODES);
255
256         return (0);
257 }
258
259 /*
260  * Mount a pseudofs instance
261  */
262 int
263 pfs_mount(struct pfs_info *pi, struct mount *mp, struct thread *td)
264 {
265         struct statfs *sbp;
266
267         if (mp->mnt_flag & MNT_UPDATE)
268                 return (EOPNOTSUPP);
269
270         mp->mnt_flag |= MNT_LOCAL;
271         mp->mnt_data = (qaddr_t)pi;
272         vfs_getnewfsid(mp);
273
274         sbp = &mp->mnt_stat;
275         vfs_mountedfrom(mp, pi->pi_name);
276         sbp->f_bsize = PAGE_SIZE;
277         sbp->f_iosize = PAGE_SIZE;
278         sbp->f_blocks = 1;
279         sbp->f_bfree = 0;
280         sbp->f_bavail = 0;
281         sbp->f_files = 1;
282         sbp->f_ffree = 0;
283
284         return (0);
285 }
286
287 /*
288  * Unmount a pseudofs instance
289  */
290 int
291 pfs_unmount(struct mount *mp, int mntflags, struct thread *td)
292 {
293         struct pfs_info *pi;
294         int error;
295
296         pi = (struct pfs_info *)mp->mnt_data;
297
298         /* XXX do stuff with pi... */
299
300         error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0, td);
301         return (error);
302 }
303
304 /*
305  * Return a root vnode
306  */
307 int
308 pfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
309 {
310         struct pfs_info *pi;
311
312         pi = (struct pfs_info *)mp->mnt_data;
313         return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID);
314 }
315
316 /*
317  * Return filesystem stats
318  */
319 int
320 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
321 {
322         /* no-op:  always called with mp->mnt_stat */
323         return (0);
324 }
325
326 /*
327  * Initialize a pseudofs instance
328  */
329 int
330 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
331 {
332         struct pfs_node *root;
333         int error;
334
335         mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF);
336
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] = '/';
342         root->pn_info = pi;
343         if (_pfs_fixup_dir(root) != 0) {
344                 FREE(root, M_PFSNODES);
345                 return (ENODEV); /* XXX not really the right errno */
346         }
347         pi->pi_root = root;
348
349         /* construct file hierarchy */
350         error = (pi->pi_init)(pi, vfc);
351         if (error) {
352                 pfs_destroy(root);
353                 pi->pi_root = NULL;
354                 mtx_destroy(&pi->pi_mutex);
355                 return (error);
356         }
357
358         pfs_fileno_init(pi);
359         if (bootverbose)
360                 printf("%s registered\n", pi->pi_name);
361         return (0);
362 }
363
364 /*
365  * Destroy a pseudofs instance
366  */
367 int
368 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
369 {
370         int error;
371
372         pfs_fileno_uninit(pi);
373         pfs_destroy(pi->pi_root);
374         pi->pi_root = NULL;
375         mtx_destroy(&pi->pi_mutex);
376         if (bootverbose)
377                 printf("%s unregistered\n", pi->pi_name);
378         error = (pi->pi_uninit)(pi, vfc);
379         return (error);
380 }
381
382 /*
383  * Handle load / unload events
384  */
385 static int
386 pfs_modevent(module_t mod, int evt, void *arg)
387 {
388         switch (evt) {
389         case MOD_LOAD:
390                 pfs_vncache_load();
391                 break;
392         case MOD_UNLOAD:
393         case MOD_SHUTDOWN:
394                 pfs_vncache_unload();
395                 break;
396         default:
397                 return EOPNOTSUPP;
398                 break;
399         }
400         return 0;
401 }
402
403 /*
404  * Module declaration
405  */
406 static moduledata_t pseudofs_data = {
407         "pseudofs",
408         pfs_modevent,
409         NULL
410 };
411 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
412 MODULE_VERSION(pseudofs, 1);