xref: /freebsd/sys/fs/pseudofs/pseudofs.c (revision c27f7d6b9cf6d4ab01cb3d0972726c14e0aca146)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
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 | CTLFLAG_MPSAFE, 0,
53     "pseudofs");
54 
55 #ifdef PSEUDOFS_TRACE
56 int pfs_trace;
57 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0,
58     "enable tracing of pseudofs vnode operations");
59 #endif
60 
61 #if PFS_FSNAMELEN != MFSNAMELEN
62 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
63 #endif
64 
65 /*
66  * Allocate and initialize a node
67  */
68 static struct pfs_node *
69 pfs_alloc_node_flags(struct pfs_info *pi, const char *name, pfs_type_t type, int flags)
70 {
71 	struct pfs_node *pn;
72 	int malloc_flags;
73 	size_t len;
74 
75 	len = strlen(name);
76 	KASSERT(len < PFS_NAMELEN,
77 	    ("%s(): node name is too long", __func__));
78 	if (flags & PFS_NOWAIT)
79 		malloc_flags = M_NOWAIT | M_ZERO;
80 	else
81 		malloc_flags = M_WAITOK | M_ZERO;
82 	pn = malloc(sizeof(*pn) + len + 1, M_PFSNODES, malloc_flags);
83 	if (pn == NULL)
84 		return (NULL);
85 	mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK);
86 	memcpy(pn->pn_name, name, len);
87 	pn->pn_type = type;
88 	pn->pn_info = pi;
89 	return (pn);
90 }
91 
92 static struct pfs_node *
93 pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type)
94 {
95 	return (pfs_alloc_node_flags(pi, name, type, 0));
96 }
97 
98 /*
99  * Add a node to a directory
100  */
101 static void
102 pfs_add_node(struct pfs_node *parent, struct pfs_node *pn)
103 {
104 #ifdef INVARIANTS
105 	struct pfs_node *iter;
106 #endif
107 
108 	KASSERT(parent != NULL,
109 	    ("%s(): parent is NULL", __func__));
110 	KASSERT(pn->pn_parent == NULL,
111 	    ("%s(): node already has a parent", __func__));
112 	KASSERT(parent->pn_info != NULL,
113 	    ("%s(): parent has no pn_info", __func__));
114 	KASSERT(parent->pn_type == pfstype_dir ||
115 	    parent->pn_type == pfstype_procdir ||
116 	    parent->pn_type == pfstype_root,
117 	    ("%s(): parent is not a directory", __func__));
118 
119 #ifdef INVARIANTS
120 	/* XXX no locking! */
121 	if (pn->pn_type == pfstype_procdir)
122 		for (iter = parent; iter != NULL; iter = iter->pn_parent)
123 			KASSERT(iter->pn_type != pfstype_procdir,
124 			    ("%s(): nested process directories", __func__));
125 	for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) {
126 		KASSERT(strcmp(pn->pn_name, iter->pn_name) != 0,
127 		    ("%s(): homonymous siblings: '%s' type %d", __func__,
128 		    pn->pn_name, pn->pn_type));
129 		if (pn->pn_type == pfstype_procdir)
130 			KASSERT(iter->pn_type != pfstype_procdir,
131 			    ("%s(): sibling process directories", __func__));
132 	}
133 #endif
134 
135 	pn->pn_parent = parent;
136 	pfs_fileno_alloc(pn);
137 
138 	pfs_lock(parent);
139 	if ((parent->pn_flags & PFS_PROCDEP) != 0)
140 		pn->pn_flags |= PFS_PROCDEP;
141 	if (parent->pn_nodes == NULL) {
142 		KASSERT(parent->pn_last_node == NULL,
143 		    ("%s(): pn_last_node not NULL", __func__));
144 		parent->pn_nodes = pn;
145 		parent->pn_last_node = pn;
146 	} else {
147 		KASSERT(parent->pn_last_node != NULL,
148 		    ("%s(): pn_last_node is NULL", __func__));
149 		KASSERT(parent->pn_last_node->pn_next == NULL,
150 		    ("%s(): pn_last_node->pn_next not NULL", __func__));
151 		parent->pn_last_node->pn_next = pn;
152 		parent->pn_last_node = pn;
153 	}
154 	pfs_unlock(parent);
155 }
156 
157 /*
158  * Detach a node from its aprent
159  */
160 static void
161 pfs_detach_node(struct pfs_node *pn)
162 {
163 	struct pfs_node *node, *parent = pn->pn_parent;
164 	struct pfs_node **iter;
165 
166 	KASSERT(parent != NULL, ("%s(): node has no parent", __func__));
167 	KASSERT(parent->pn_info == pn->pn_info,
168 	    ("%s(): parent has different pn_info", __func__));
169 
170 	pfs_lock(parent);
171 	if (pn == parent->pn_last_node) {
172 		if (pn == pn->pn_nodes) {
173 			parent->pn_last_node = NULL;
174 		} else {
175 			for (node = parent->pn_nodes;
176 			    node->pn_next != pn; node = node->pn_next)
177 				continue;
178 			parent->pn_last_node = node;
179 		}
180 	}
181 	iter = &parent->pn_nodes;
182 	while (*iter != NULL) {
183 		if (*iter == pn) {
184 			*iter = pn->pn_next;
185 			break;
186 		}
187 		iter = &(*iter)->pn_next;
188 	}
189 	pn->pn_parent = NULL;
190 	pfs_unlock(parent);
191 }
192 
193 /*
194  * Add . and .. to a directory
195  */
196 static int
197 pfs_fixup_dir_flags(struct pfs_node *parent, int flags)
198 {
199 	struct pfs_node *dot, *dotdot;
200 
201 	dot = pfs_alloc_node_flags(parent->pn_info, ".", pfstype_this, flags);
202 	if (dot == NULL)
203 		return (ENOMEM);
204 	dotdot = pfs_alloc_node_flags(parent->pn_info, "..", pfstype_parent, flags);
205 	if (dotdot == NULL) {
206 		pfs_destroy(dot);
207 		return (ENOMEM);
208 	}
209 	pfs_add_node(parent, dot);
210 	pfs_add_node(parent, dotdot);
211 	return (0);
212 }
213 
214 static void
215 pfs_fixup_dir(struct pfs_node *parent)
216 {
217 
218 	pfs_fixup_dir_flags(parent, 0);
219 }
220 
221 /*
222  * Create a directory
223  */
224 struct pfs_node	*
225 pfs_create_dir(struct pfs_node *parent, const char *name,
226 	       pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
227 	       int flags)
228 {
229 	struct pfs_node *pn;
230 	int rc;
231 
232 	pn = pfs_alloc_node_flags(parent->pn_info, name,
233 			 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir, flags);
234 	if (pn == NULL)
235 		return (NULL);
236 	pn->pn_attr = attr;
237 	pn->pn_vis = vis;
238 	pn->pn_destroy = destroy;
239 	pn->pn_flags = flags;
240 	pfs_add_node(parent, pn);
241 	rc = pfs_fixup_dir_flags(pn, flags);
242 	if (rc) {
243 		pfs_destroy(pn);
244 		return (NULL);
245 	}
246 	return (pn);
247 }
248 
249 /*
250  * Create a file
251  */
252 struct pfs_node	*
253 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
254 		pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
255 		int flags)
256 {
257 	struct pfs_node *pn;
258 
259 	pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_file, flags);
260 	if (pn == NULL)
261 		return (NULL);
262 	pn->pn_fill = fill;
263 	pn->pn_attr = attr;
264 	pn->pn_vis = vis;
265 	pn->pn_destroy = destroy;
266 	pn->pn_flags = flags;
267 	pfs_add_node(parent, pn);
268 
269 	return (pn);
270 }
271 
272 /*
273  * Create a symlink
274  */
275 struct pfs_node	*
276 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
277 		pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
278 		int flags)
279 {
280 	struct pfs_node *pn;
281 
282 	pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_symlink, flags);
283 	if (pn == NULL)
284 		return (NULL);
285 	pn->pn_fill = fill;
286 	pn->pn_attr = attr;
287 	pn->pn_vis = vis;
288 	pn->pn_destroy = destroy;
289 	pn->pn_flags = flags;
290 	pfs_add_node(parent, pn);
291 
292 	return (pn);
293 }
294 
295 /*
296  * Locate a node by name
297  */
298 struct pfs_node *
299 pfs_find_node(struct pfs_node *parent, const char *name)
300 {
301 	struct pfs_node *pn;
302 
303 	pfs_lock(parent);
304 	for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next)
305 		if (strcmp(pn->pn_name, name) == 0)
306 			break;
307 	pfs_unlock(parent);
308 	return (pn);
309 }
310 
311 /*
312  * Destroy a node and all its descendants.  If the node to be destroyed
313  * has a parent, the parent's mutex must be held.
314  */
315 int
316 pfs_destroy(struct pfs_node *pn)
317 {
318 	struct pfs_node *iter;
319 
320 	KASSERT(pn != NULL,
321 	    ("%s(): node is NULL", __func__));
322 	KASSERT(pn->pn_info != NULL,
323 	    ("%s(): node has no pn_info", __func__));
324 
325 	if (pn->pn_parent)
326 		pfs_detach_node(pn);
327 
328 	/* destroy children */
329 	if (pn->pn_type == pfstype_dir ||
330 	    pn->pn_type == pfstype_procdir ||
331 	    pn->pn_type == pfstype_root) {
332 		pfs_lock(pn);
333 		while (pn->pn_nodes != NULL) {
334 			iter = pn->pn_nodes;
335 			pn->pn_nodes = iter->pn_next;
336 			iter->pn_parent = NULL;
337 			pfs_unlock(pn);
338 			pfs_destroy(iter);
339 			pfs_lock(pn);
340 		}
341 		pfs_unlock(pn);
342 	}
343 
344 	/* revoke vnodes and fileno */
345 	pfs_purge(pn);
346 
347 	/* callback to free any private resources */
348 	if (pn->pn_destroy != NULL)
349 		pn_destroy(pn);
350 
351 	/* destroy the node */
352 	pfs_fileno_free(pn);
353 	mtx_destroy(&pn->pn_mutex);
354 	free(pn, M_PFSNODES);
355 
356 	return (0);
357 }
358 
359 /*
360  * Mount a pseudofs instance
361  */
362 int
363 pfs_mount(struct pfs_info *pi, struct mount *mp)
364 {
365 	struct statfs *sbp;
366 
367 	if (mp->mnt_flag & MNT_UPDATE)
368 		return (EOPNOTSUPP);
369 
370 	MNT_ILOCK(mp);
371 	mp->mnt_flag |= MNT_LOCAL;
372 	mp->mnt_kern_flag |= MNTK_NOMSYNC;
373 	MNT_IUNLOCK(mp);
374 	mp->mnt_data = pi;
375 	vfs_getnewfsid(mp);
376 
377 	sbp = &mp->mnt_stat;
378 	vfs_mountedfrom(mp, pi->pi_name);
379 	sbp->f_bsize = PAGE_SIZE;
380 	sbp->f_iosize = PAGE_SIZE;
381 	sbp->f_blocks = 2;
382 	sbp->f_bfree = 2;
383 	sbp->f_bavail = 2;
384 	sbp->f_files = 0;
385 	sbp->f_ffree = 0;
386 
387 	return (0);
388 }
389 
390 /*
391  * Compatibility shim for old mount(2) system call
392  */
393 int
394 pfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
395 {
396 	int error;
397 
398 	error = kernel_mount(ma, flags);
399 	return (error);
400 }
401 
402 /*
403  * Unmount a pseudofs instance
404  */
405 int
406 pfs_unmount(struct mount *mp, int mntflags)
407 {
408 	int error;
409 
410 	error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0,
411 	    curthread);
412 	return (error);
413 }
414 
415 /*
416  * Return a root vnode
417  */
418 int
419 pfs_root(struct mount *mp, int flags, struct vnode **vpp)
420 {
421 	struct pfs_info *pi;
422 
423 	pi = (struct pfs_info *)mp->mnt_data;
424 	return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID));
425 }
426 
427 /*
428  * Return filesystem stats
429  */
430 int
431 pfs_statfs(struct mount *mp, struct statfs *sbp)
432 {
433 	/* no-op:  always called with mp->mnt_stat */
434 	return (0);
435 }
436 
437 /*
438  * Initialize a pseudofs instance
439  */
440 int
441 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
442 {
443 	struct pfs_node *root;
444 	int error;
445 
446 	pfs_fileno_init(pi);
447 
448 	/* set up the root directory */
449 	root = pfs_alloc_node(pi, "/", pfstype_root);
450 	pi->pi_root = root;
451 	pfs_fileno_alloc(root);
452 	pfs_fixup_dir(root);
453 
454 	/* construct file hierarchy */
455 	error = (pi->pi_init)(pi, vfc);
456 	if (error) {
457 		pfs_destroy(root);
458 		pi->pi_root = NULL;
459 		return (error);
460 	}
461 
462 	if (bootverbose)
463 		printf("%s registered\n", pi->pi_name);
464 	return (0);
465 }
466 
467 /*
468  * Destroy a pseudofs instance
469  */
470 int
471 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
472 {
473 	int error;
474 
475 	pfs_destroy(pi->pi_root);
476 	pi->pi_root = NULL;
477 	pfs_fileno_uninit(pi);
478 	if (bootverbose)
479 		printf("%s unregistered\n", pi->pi_name);
480 	error = (pi->pi_uninit)(pi, vfc);
481 	return (error);
482 }
483 
484 /*
485  * Handle load / unload events
486  */
487 static int
488 pfs_modevent(module_t mod, int evt, void *arg)
489 {
490 	switch (evt) {
491 	case MOD_LOAD:
492 		pfs_vncache_load();
493 		break;
494 	case MOD_UNLOAD:
495 	case MOD_SHUTDOWN:
496 		pfs_vncache_unload();
497 		break;
498 	default:
499 		return EOPNOTSUPP;
500 		break;
501 	}
502 	return 0;
503 }
504 
505 /*
506  * Module declaration
507  */
508 static moduledata_t pseudofs_data = {
509 	"pseudofs",
510 	pfs_modevent,
511 	NULL
512 };
513 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
514 MODULE_VERSION(pseudofs, 1);
515