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: releng/6.4/sys/fs/pseudofs/pseudofs.c 170191 2007-06-01 20:56:18Z des $");
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 node->pn_info = parent->pn_info;
77 node->pn_parent = parent;
78 node->pn_next = parent->pn_nodes;
79 parent->pn_nodes = node;
80 /* Propagate flag to all child nodes (and thus their vnodes) */
81 if ((parent->pn_flags & PFS_PROCDEP) != 0)
82 node->pn_flags |= PFS_PROCDEP;
83
84 return (0);
85 }
86
87 /*
88 * Add . and .. to a directory
89 */
90 static int
91 _pfs_fixup_dir(struct pfs_node *parent)
92 {
93 struct pfs_node *dir;
94
95 MALLOC(dir, struct pfs_node *, sizeof *dir,
96 M_PFSNODES, M_WAITOK|M_ZERO);
97 dir->pn_name[0] = '.';
98 dir->pn_type = pfstype_this;
99
100 if (_pfs_add_node(parent, dir) != 0) {
101 FREE(dir, M_PFSNODES);
102 return (-1);
103 }
104
105 MALLOC(dir, struct pfs_node *, sizeof *dir,
106 M_PFSNODES, M_WAITOK|M_ZERO);
107 dir->pn_name[0] = dir->pn_name[1] = '.';
108 dir->pn_type = pfstype_parent;
109
110 if (_pfs_add_node(parent, dir) != 0) {
111 FREE(dir, M_PFSNODES);
112 return (-1);
113 }
114
115 return (0);
116 }
117
118 /*
119 * Create a directory
120 */
121 struct pfs_node *
122 pfs_create_dir(struct pfs_node *parent, const char *name,
123 pfs_attr_t attr, pfs_vis_t vis, int flags)
124 {
125 struct pfs_node *dir;
126
127 KASSERT(strlen(name) < PFS_NAMELEN,
128 ("%s(): node name is too long", __func__));
129
130 MALLOC(dir, struct pfs_node *, sizeof *dir,
131 M_PFSNODES, M_WAITOK|M_ZERO);
132 strcpy(dir->pn_name, name);
133 dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir;
134 dir->pn_attr = attr;
135 dir->pn_vis = vis;
136 dir->pn_flags = flags;
137
138 if (_pfs_add_node(parent, dir) != 0) {
139 FREE(dir, M_PFSNODES);
140 return (NULL);
141 }
142
143 if (_pfs_fixup_dir(dir) != 0) {
144 pfs_destroy(dir);
145 return (NULL);
146 }
147
148 return (dir);
149 }
150
151 /*
152 * Create a file
153 */
154 struct pfs_node *
155 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
156 pfs_attr_t attr, pfs_vis_t vis, int flags)
157 {
158 struct pfs_node *node;
159
160 KASSERT(strlen(name) < PFS_NAMELEN,
161 ("%s(): node name is too long", __func__));
162
163 MALLOC(node, struct pfs_node *, sizeof *node,
164 M_PFSNODES, M_WAITOK|M_ZERO);
165 strcpy(node->pn_name, name);
166 node->pn_type = pfstype_file;
167 node->pn_func = fill;
168 node->pn_attr = attr;
169 node->pn_vis = vis;
170 node->pn_flags = flags;
171
172 if (_pfs_add_node(parent, node) != 0) {
173 FREE(node, M_PFSNODES);
174 return (NULL);
175 }
176
177 return (node);
178 }
179
180 /*
181 * Create a symlink
182 */
183 struct pfs_node *
184 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
185 pfs_attr_t attr, pfs_vis_t vis, int flags)
186 {
187 struct pfs_node *node;
188
189 node = pfs_create_file(parent, name, fill, attr, vis, flags);
190 if (node == NULL)
191 return (NULL);
192 node->pn_type = pfstype_symlink;
193 return (node);
194 }
195
196 /*
197 * Locate a node by name
198 */
199 struct pfs_node *
200 pfs_find_node(struct pfs_node *parent, const char *name)
201 {
202 struct pfs_node *node;
203
204 for (node = parent->pn_nodes; node != NULL; node = node->pn_next)
205 if (strcmp(node->pn_name, name) == 0)
206 break;
207 return (node);
208 }
209
210 /*
211 * Destroy a node or a tree of nodes
212 */
213 int
214 pfs_destroy(struct pfs_node *node)
215 {
216 struct pfs_node *parent, **rover;
217
218 KASSERT(node != NULL,
219 ("%s(): node is NULL", __func__));
220 KASSERT(node->pn_info != NULL,
221 ("%s(): node has no pn_info", __func__));
222
223 /* destroy children */
224 if (node->pn_type == pfstype_dir ||
225 node->pn_type == pfstype_procdir ||
226 node->pn_type == pfstype_root)
227 while (node->pn_nodes != NULL)
228 pfs_destroy(node->pn_nodes);
229
230 /* unlink from parent */
231 if ((parent = node->pn_parent) != NULL) {
232 KASSERT(parent->pn_info == node->pn_info,
233 ("%s(): parent has different pn_info", __func__));
234 rover = &parent->pn_nodes;
235 while (*rover != NULL) {
236 if (*rover == node) {
237 *rover = node->pn_next;
238 break;
239 }
240 rover = &(*rover)->pn_next;
241 }
242 }
243
244 /* revoke fileno and vnodes and release memory */
245 if (node->pn_fileno)
246 pfs_fileno_free(node);
247 pfs_purge(node);
248 FREE(node, M_PFSNODES);
249
250 return (0);
251 }
252
253 /*
254 * Mount a pseudofs instance
255 */
256 int
257 pfs_mount(struct pfs_info *pi, struct mount *mp, struct thread *td)
258 {
259 struct statfs *sbp;
260
261 if (mp->mnt_flag & MNT_UPDATE)
262 return (EOPNOTSUPP);
263
264 MNT_ILOCK(mp);
265 mp->mnt_flag |= MNT_LOCAL;
266 #if 0
267 /* not quite ready for this yet */
268 mp->mnt_kern_flag |= MNTK_MPSAFE;
269 #endif
270 MNT_IUNLOCK(mp);
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_assert(&Giant, MA_OWNED);
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 return (error);
355 }
356
357 pfs_fileno_init(pi);
358 if (bootverbose)
359 printf("%s registered\n", pi->pi_name);
360 return (0);
361 }
362
363 /*
364 * Destroy a pseudofs instance
365 */
366 int
367 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
368 {
369 int error;
370
371 mtx_assert(&Giant, MA_OWNED);
372
373 pfs_destroy(pi->pi_root);
374 pi->pi_root = NULL;
375 pfs_fileno_uninit(pi);
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);
Cache object: 82963faeccd6705cb83279bd7bbb52dc
|