xref: /netbsd-src/sys/kern/vfs_init.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: vfs_init.c,v 1.51 2020/05/16 18:31:50 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1989, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * This code is derived from software contributed
38  * to Berkeley by John Heidemann of the UCLA Ficus project.
39  *
40  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)vfs_init.c	8.5 (Berkeley) 5/11/95
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: vfs_init.c,v 1.51 2020/05/16 18:31:50 christos Exp $");
71 
72 #include <sys/param.h>
73 #include <sys/mount.h>
74 #include <sys/time.h>
75 #include <sys/vnode.h>
76 #include <sys/stat.h>
77 #include <sys/namei.h>
78 #include <sys/ucred.h>
79 #include <sys/buf.h>
80 #include <sys/errno.h>
81 #include <sys/kmem.h>
82 #include <sys/systm.h>
83 #include <sys/module.h>
84 #include <sys/dirhash.h>
85 #include <sys/sysctl.h>
86 #include <sys/kauth.h>
87 
88 /*
89  * Sigh, such primitive tools are these...
90  */
91 #if 0
92 #define DODEBUG(A) A
93 #else
94 #define DODEBUG(A)
95 #endif
96 
97 pool_cache_t pnbuf_cache;
98 
99 /*
100  * The global list of vnode operations.
101  */
102 extern const struct vnodeop_desc * const vfs_op_descs[];
103 
104 /*
105  * These vnodeopv_descs are listed here because they are not
106  * associated with any particular file system, and thus cannot
107  * be initialized by vfs_attach().
108  */
109 extern const struct vnodeopv_desc dead_vnodeop_opv_desc;
110 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc;
111 extern const struct vnodeopv_desc spec_vnodeop_opv_desc;
112 
113 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
114 	&dead_vnodeop_opv_desc,
115 	&fifo_vnodeop_opv_desc,
116 	&spec_vnodeop_opv_desc,
117 	NULL,
118 };
119 
120 struct vfs_list_head vfs_list =			/* vfs list */
121     LIST_HEAD_INITIALIZER(vfs_list);
122 
123 static kauth_listener_t mount_listener;
124 
125 /*
126  * This code doesn't work if the defn is **vnodop_defns with cc.
127  * The problem is because of the compiler sometimes putting in an
128  * extra level of indirection for arrays.  It's an interesting
129  * "feature" of C.
130  */
131 typedef int (*PFI)(void *);
132 
133 /*
134  * A miscellaneous routine.
135  * A generic "default" routine that just returns an error.
136  */
137 /*ARGSUSED*/
138 int
139 vn_default_error(void *v)
140 {
141 
142 	return (EOPNOTSUPP);
143 }
144 
145 static struct sysctllog *vfs_sysctllog;
146 
147 /*
148  * Top level filesystem related information gathering.
149  */
150 static void
151 sysctl_vfs_setup(void)
152 {
153 	extern int vfs_magiclinks;
154 
155 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
156 		       CTLFLAG_PERMANENT,
157 		       CTLTYPE_NODE, "generic",
158 		       SYSCTL_DESCR("Non-specific vfs related information"),
159 		       NULL, 0, NULL, 0,
160 		       CTL_VFS, VFS_GENERIC, CTL_EOL);
161 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
162 		       CTLFLAG_PERMANENT,
163 		       CTLTYPE_STRING, "fstypes",
164 		       SYSCTL_DESCR("List of file systems present"),
165 		       sysctl_vfs_generic_fstypes, 0, NULL, 0,
166 		       CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
167 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
168 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
169 		       CTLTYPE_INT, "magiclinks",
170 		       SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"),
171 		       NULL, 0, &vfs_magiclinks, 0,
172 		       CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL);
173 }
174 
175 
176 /*
177  * vfs_init.c
178  *
179  * Allocate and fill in operations vectors.
180  *
181  * An undocumented feature of this approach to defining operations is that
182  * there can be multiple entries in vfs_opv_descs for the same operations
183  * vector. This allows third parties to extend the set of operations
184  * supported by another layer in a binary compatibile way. For example,
185  * assume that NFS needed to be modified to support Ficus. NFS has an entry
186  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
187  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
188  * listing those new operations Ficus adds to NFS, all without modifying the
189  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
190  * that is a(whole)nother story.) This is a feature.
191  */
192 
193 /*
194  * Init the vector, if it needs it.
195  * Also handle backwards compatibility.
196  */
197 static void
198 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc)
199 {
200 	int (**opv_desc_vector)(void *);
201 	const struct vnodeopv_entry_desc *opve_descp;
202 
203 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
204 
205 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
206 	     opve_descp->opve_op;
207 	     opve_descp++) {
208 		/*
209 		 * Sanity check:  is this operation listed
210 		 * in the list of operations?  We check this
211 		 * by seeing if its offset is zero.  Since
212 		 * the default routine should always be listed
213 		 * first, it should be the only one with a zero
214 		 * offset.  Any other operation with a zero
215 		 * offset is probably not listed in
216 		 * vfs_op_descs, and so is probably an error.
217 		 *
218 		 * A panic here means the layer programmer
219 		 * has committed the all-too common bug
220 		 * of adding a new operation to the layer's
221 		 * list of vnode operations but
222 		 * not adding the operation to the system-wide
223 		 * list of supported operations.
224 		 */
225 		if (opve_descp->opve_op->vdesc_offset == 0 &&
226 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
227 			printf("operation %s not listed in %s.\n",
228 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
229 			panic ("vfs_opv_init: bad operation");
230 		}
231 
232 		/*
233 		 * Fill in this entry.
234 		 */
235 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
236 		    opve_descp->opve_impl;
237 	}
238 }
239 
240 static void
241 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc)
242 {
243 	int j;
244 	int (**opv_desc_vector)(void *);
245 
246 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
247 
248 	/*
249 	 * Force every operations vector to have a default routine.
250 	 */
251 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
252 		panic("vfs_opv_init: operation vector without default routine.");
253 
254 	for (j = 0; j < VNODE_OPS_COUNT; j++)
255 		if (opv_desc_vector[j] == NULL)
256 			opv_desc_vector[j] =
257 			    opv_desc_vector[VOFFSET(vop_default)];
258 }
259 
260 void
261 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp)
262 {
263 	int (**opv_desc_vector)(void *);
264 	int i;
265 
266 	/*
267 	 * Allocate the vectors.
268 	 */
269 	for (i = 0; vopvdpp[i] != NULL; i++) {
270 		opv_desc_vector =
271 		    kmem_alloc(VNODE_OPS_COUNT * sizeof(PFI), KM_SLEEP);
272 		memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
273 		*(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
274 		DODEBUG(printf("vector at %p allocated\n",
275 		    opv_desc_vector_p));
276 	}
277 
278 	/*
279 	 * ...and fill them in.
280 	 */
281 	for (i = 0; vopvdpp[i] != NULL; i++)
282 		vfs_opv_init_explicit(vopvdpp[i]);
283 
284 	/*
285 	 * Finally, go back and replace unfilled routines
286 	 * with their default.
287 	 */
288 	for (i = 0; vopvdpp[i] != NULL; i++)
289 		vfs_opv_init_default(vopvdpp[i]);
290 }
291 
292 void
293 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
294 {
295 	int i;
296 
297 	/*
298 	 * Free the vectors allocated in vfs_opv_init().
299 	 */
300 	for (i = 0; vopvdpp[i] != NULL; i++) {
301 		kmem_free(*(vopvdpp[i]->opv_desc_vector_p),
302 		    VNODE_OPS_COUNT * sizeof(PFI));
303 		*(vopvdpp[i]->opv_desc_vector_p) = NULL;
304 	}
305 }
306 
307 #ifdef DEBUG
308 static void
309 vfs_op_check(void)
310 {
311 	int i;
312 
313 	DODEBUG(printf("Vnode_interface_init.\n"));
314 
315 	/*
316 	 * Check offset of each op.
317 	 */
318 	for (i = 0; vfs_op_descs[i]; i++) {
319 		if (vfs_op_descs[i]->vdesc_offset != i)
320 			panic("vfs_op_check: vfs_op_desc[] offset mismatch");
321 	}
322 
323 	if (i != VNODE_OPS_COUNT) {
324 		panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
325 			i, VNODE_OPS_COUNT);
326 	}
327 
328 	DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
329 }
330 #endif /* DEBUG */
331 
332 /*
333  * Common routine to check if an unprivileged mount is allowed.
334  *
335  * We export just this part (i.e., without the access control) so that if a
336  * secmodel wants to implement finer grained user mounts it can do so without
337  * copying too much code. More elaborate policies (i.e., specific users allowed
338  * to also create devices and/or introduce set-id binaries, or export
339  * file-systems) will require a different implementation.
340  *
341  * This routine is intended to be called from listener context, and as such
342  * does not take credentials as an argument.
343  */
344 int
345 usermount_common_policy(struct mount *mp, u_long flags)
346 {
347 
348 	/* No exporting if unprivileged. */
349 	if (flags & MNT_EXPORTED)
350 		return EPERM;
351 
352 	/* Must have 'nosuid' and 'nodev'. */
353 	if ((flags & MNT_NODEV) == 0 || (flags & MNT_NOSUID) == 0)
354 		return EPERM;
355 
356 	/* Retain 'noexec'. */
357 	if ((mp->mnt_flag & MNT_NOEXEC) && (flags & MNT_NOEXEC) == 0)
358 		return EPERM;
359 
360 	return 0;
361 }
362 
363 static int
364 mount_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
365     void *arg0, void *arg1, void *arg2, void *arg3)
366 {
367 	int result;
368 	enum kauth_system_req req;
369 
370 	result = KAUTH_RESULT_DEFER;
371 	req = (enum kauth_system_req)(uintptr_t)(uintptr_t)arg0;
372 
373 	if (action != KAUTH_SYSTEM_MOUNT)
374 		return result;
375 
376 	if (req == KAUTH_REQ_SYSTEM_MOUNT_GET)
377 		result = KAUTH_RESULT_ALLOW;
378 	else if (req == KAUTH_REQ_SYSTEM_MOUNT_DEVICE) {
379 		vnode_t *devvp = arg2;
380 		accmode_t accmode = (accmode_t)(unsigned long)arg3;
381 		int error;
382 
383 		error = VOP_ACCESS(devvp, accmode, cred);
384 		if (!error)
385 			result = KAUTH_RESULT_ALLOW;
386 	}
387 
388 	return result;
389 }
390 
391 /*
392  * Initialize the vnode structures and initialize each file system type.
393  */
394 void
395 vfsinit(void)
396 {
397 
398 	/*
399 	 * Attach sysctl nodes
400 	 */
401 	sysctl_vfs_setup();
402 
403 	/*
404 	 * Initialize the namei pathname buffer pool and cache.
405 	 */
406 	pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
407 	    NULL, IPL_NONE, NULL, NULL, NULL);
408 	KASSERT(pnbuf_cache != NULL);
409 
410 	/*
411 	 * Initialize the vnode table
412 	 */
413 	vntblinit();
414 
415 	/*
416 	 * Initialize the vnode name cache
417 	 */
418 	nchinit();
419 
420 #ifdef DEBUG
421 	/*
422 	 * Check the list of vnode operations.
423 	 */
424 	vfs_op_check();
425 #endif
426 
427 	/*
428 	 * Initialize the special vnode operations.
429 	 */
430 	vfs_opv_init(vfs_special_vnodeopv_descs);
431 
432 	/*
433 	 * Initialise generic dirhash.
434 	 */
435 	dirhash_init();
436 
437 	/*
438 	 * Initialise VFS hooks.
439 	 */
440 	vfs_hooks_init();
441 
442 	mount_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
443 	    mount_listener_cb, NULL);
444 
445 	/*
446 	 * Establish each file system which was statically
447 	 * included in the kernel.
448 	 */
449 	module_init_class(MODULE_CLASS_VFS);
450 }
451 
452 /*
453  * Drop a reference to a file system type.
454  */
455 void
456 vfs_delref(struct vfsops *vfs)
457 {
458 
459 	mutex_enter(&vfs_list_lock);
460 	vfs->vfs_refcount--;
461 	mutex_exit(&vfs_list_lock);
462 }
463 
464 /*
465  * Establish a file system and initialize it.
466  */
467 int
468 vfs_attach(struct vfsops *vfs)
469 {
470 	struct vfsops *v;
471 	int error = 0;
472 
473 	mutex_enter(&vfs_list_lock);
474 
475 	/*
476 	 * Make sure this file system doesn't already exist.
477 	 */
478 	LIST_FOREACH(v, &vfs_list, vfs_list) {
479 		if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
480 			error = EEXIST;
481 			goto out;
482 		}
483 	}
484 
485 	/*
486 	 * Initialize the vnode operations for this file system.
487 	 */
488 	vfs_opv_init(vfs->vfs_opv_descs);
489 
490 	/*
491 	 * Now initialize the file system itself.
492 	 */
493 	(*vfs->vfs_init)();
494 
495 	/*
496 	 * ...and link it into the kernel's list.
497 	 */
498 	LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
499 
500 	/*
501 	 * Sanity: make sure the reference count is 0.
502 	 */
503 	vfs->vfs_refcount = 0;
504  out:
505 	mutex_exit(&vfs_list_lock);
506 	return (error);
507 }
508 
509 /*
510  * Remove a file system from the kernel.
511  */
512 int
513 vfs_detach(struct vfsops *vfs)
514 {
515 	struct vfsops *v;
516 	int error = 0;
517 
518 	mutex_enter(&vfs_list_lock);
519 
520 	/*
521 	 * Make sure no one is using the filesystem.
522 	 */
523 	if (vfs->vfs_refcount != 0) {
524 		error = EBUSY;
525 		goto out;
526 	}
527 
528 	/*
529 	 * ...and remove it from the kernel's list.
530 	 */
531 	LIST_FOREACH(v, &vfs_list, vfs_list) {
532 		if (v == vfs) {
533 			LIST_REMOVE(v, vfs_list);
534 			break;
535 		}
536 	}
537 
538 	if (v == NULL) {
539 		error = ESRCH;
540 		goto out;
541 	}
542 
543 	/*
544 	 * Now run the file system-specific cleanups.
545 	 */
546 	(*vfs->vfs_done)();
547 
548 	/*
549 	 * Free the vnode operations vector.
550 	 */
551 	vfs_opv_free(vfs->vfs_opv_descs);
552  out:
553  	mutex_exit(&vfs_list_lock);
554 	return (error);
555 }
556 
557 void
558 vfs_reinit(void)
559 {
560 	struct vfsops *vfs;
561 
562 	mutex_enter(&vfs_list_lock);
563 	LIST_FOREACH(vfs, &vfs_list, vfs_list) {
564 		if (vfs->vfs_reinit) {
565 			vfs->vfs_refcount++;
566 			mutex_exit(&vfs_list_lock);
567 			(*vfs->vfs_reinit)();
568 			mutex_enter(&vfs_list_lock);
569 			vfs->vfs_refcount--;
570 		}
571 	}
572 	mutex_exit(&vfs_list_lock);
573 }
574