Home | History | Annotate | Line # | Download | only in kern
vfs_init.c revision 1.60
      1 /*	$NetBSD: vfs_init.c,v 1.60 2023/04/29 10:07:13 riastradh 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.60 2023/04/29 10:07:13 riastradh Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/types.h>
     74 
     75 #include <sys/buf.h>
     76 #include <sys/dirhash.h>
     77 #include <sys/errno.h>
     78 #include <sys/kauth.h>
     79 #include <sys/kmem.h>
     80 #include <sys/module.h>
     81 #include <sys/mount.h>
     82 #include <sys/namei.h>
     83 #include <sys/stat.h>
     84 #include <sys/sysctl.h>
     85 #include <sys/systm.h>
     86 #include <sys/time.h>
     87 #include <sys/ucred.h>
     88 #include <sys/vnode.h>
     89 
     90 #include <miscfs/deadfs/deadfs.h>
     91 #include <miscfs/fifofs/fifo.h>
     92 #include <miscfs/specfs/specdev.h>
     93 
     94 /*
     95  * Sigh, such primitive tools are these...
     96  */
     97 #if 0
     98 #define DODEBUG(A) A
     99 #else
    100 #define DODEBUG(A)
    101 #endif
    102 
    103 pool_cache_t pnbuf_cache;
    104 
    105 /*
    106  * These vnodeopv_descs are listed here because they are not
    107  * associated with any particular file system, and thus cannot
    108  * be initialized by vfs_attach().
    109  */
    110 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
    111 	&dead_vnodeop_opv_desc,
    112 	&fifo_vnodeop_opv_desc,
    113 	&spec_vnodeop_opv_desc,
    114 	NULL,
    115 };
    116 
    117 struct vfs_list_head vfs_list =			/* vfs list */
    118     LIST_HEAD_INITIALIZER(vfs_list);
    119 
    120 static kauth_listener_t mount_listener;
    121 
    122 /*
    123  * This code doesn't work if the defn is **vnodop_defns with cc.
    124  * The problem is because of the compiler sometimes putting in an
    125  * extra level of indirection for arrays.  It's an interesting
    126  * "feature" of C.
    127  */
    128 typedef int (*PFI)(void *);
    129 
    130 /*
    131  * A miscellaneous routine.
    132  * A generic "default" routine that just returns an error.
    133  */
    134 /*ARGSUSED*/
    135 int
    136 vn_default_error(void *v)
    137 {
    138 
    139 	return (EOPNOTSUPP);
    140 }
    141 
    142 static struct sysctllog *vfs_sysctllog;
    143 
    144 /*
    145  * Top level filesystem related information gathering.
    146  */
    147 static void
    148 sysctl_vfs_setup(void)
    149 {
    150 
    151 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
    152 		       CTLFLAG_PERMANENT,
    153 		       CTLTYPE_NODE, "generic",
    154 		       SYSCTL_DESCR("Non-specific vfs related information"),
    155 		       NULL, 0, NULL, 0,
    156 		       CTL_VFS, VFS_GENERIC, CTL_EOL);
    157 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
    158 		       CTLFLAG_PERMANENT,
    159 		       CTLTYPE_STRING, "fstypes",
    160 		       SYSCTL_DESCR("List of file systems present"),
    161 		       sysctl_vfs_generic_fstypes, 0, NULL, 0,
    162 		       CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
    163 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
    164 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    165 		       CTLTYPE_INT, "magiclinks",
    166 		       SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"),
    167 		       NULL, 0, &vfs_magiclinks, 0,
    168 		       CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL);
    169 	sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
    170 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    171 			CTLTYPE_INT, "timestamp_precision",
    172 			SYSCTL_DESCR("File timestamp precision"),
    173 			NULL, 0, &vfs_timestamp_precision, 0,
    174 			CTL_VFS, VFS_GENERIC, VFS_TIMESTAMP_PRECISION,
    175 			CTL_EOL);
    176 }
    177 
    178 
    179 /*
    180  * vfs_init.c
    181  *
    182  * Allocate and fill in operations vectors.
    183  *
    184  * An undocumented feature of this approach to defining operations is that
    185  * there can be multiple entries in vfs_opv_descs for the same operations
    186  * vector. This allows third parties to extend the set of operations
    187  * supported by another layer in a binary compatibile way. For example,
    188  * assume that NFS needed to be modified to support Ficus. NFS has an entry
    189  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
    190  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
    191  * listing those new operations Ficus adds to NFS, all without modifying the
    192  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
    193  * that is a(whole)nother story.) This is a feature.
    194  */
    195 
    196 /*
    197  * Init the vector, if it needs it.
    198  * Also handle backwards compatibility.
    199  */
    200 static void
    201 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc)
    202 {
    203 	int (**opv_desc_vector)(void *);
    204 	const struct vnodeopv_entry_desc *opve_descp;
    205 
    206 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
    207 
    208 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
    209 	     opve_descp->opve_op;
    210 	     opve_descp++) {
    211 		/*
    212 		 * Sanity check:  is this operation listed
    213 		 * in the list of operations?  We check this
    214 		 * by seeing if its offset is zero.  Since
    215 		 * the default routine should always be listed
    216 		 * first, it should be the only one with a zero
    217 		 * offset.  Any other operation with a zero
    218 		 * offset is probably not listed in
    219 		 * vfs_op_descs, and so is probably an error.
    220 		 *
    221 		 * A panic here means the layer programmer
    222 		 * has committed the all-too common bug
    223 		 * of adding a new operation to the layer's
    224 		 * list of vnode operations but
    225 		 * not adding the operation to the system-wide
    226 		 * list of supported operations.
    227 		 */
    228 		if (opve_descp->opve_op->vdesc_offset == 0 &&
    229 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
    230 			printf("operation %s not listed in %s.\n",
    231 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
    232 			panic ("vfs_opv_init: bad operation");
    233 		}
    234 
    235 		/*
    236 		 * Fill in this entry.
    237 		 */
    238 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
    239 		    opve_descp->opve_impl;
    240 	}
    241 }
    242 
    243 static void
    244 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc)
    245 {
    246 	int j;
    247 	int (**opv_desc_vector)(void *);
    248 
    249 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
    250 
    251 	/*
    252 	 * Force every operations vector to have a default routine.
    253 	 */
    254 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
    255 		panic("vfs_opv_init: operation vector without default routine.");
    256 
    257 	for (j = 0; j < VNODE_OPS_COUNT; j++)
    258 		if (opv_desc_vector[j] == NULL)
    259 			opv_desc_vector[j] =
    260 			    opv_desc_vector[VOFFSET(vop_default)];
    261 }
    262 
    263 void
    264 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp)
    265 {
    266 	int (**opv_desc_vector)(void *);
    267 	int i;
    268 
    269 	/*
    270 	 * Allocate the vectors.
    271 	 */
    272 	for (i = 0; vopvdpp[i] != NULL; i++) {
    273 		opv_desc_vector =
    274 		    kmem_alloc(VNODE_OPS_COUNT * sizeof(PFI), KM_SLEEP);
    275 		memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
    276 		*(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
    277 		DODEBUG(printf("vector at %p allocated\n",
    278 		    opv_desc_vector_p));
    279 	}
    280 
    281 	/*
    282 	 * ...and fill them in.
    283 	 */
    284 	for (i = 0; vopvdpp[i] != NULL; i++)
    285 		vfs_opv_init_explicit(vopvdpp[i]);
    286 
    287 	/*
    288 	 * Finally, go back and replace unfilled routines
    289 	 * with their default.
    290 	 */
    291 	for (i = 0; vopvdpp[i] != NULL; i++)
    292 		vfs_opv_init_default(vopvdpp[i]);
    293 }
    294 
    295 void
    296 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
    297 {
    298 	int i;
    299 
    300 	/*
    301 	 * Free the vectors allocated in vfs_opv_init().
    302 	 */
    303 	for (i = 0; vopvdpp[i] != NULL; i++) {
    304 		kmem_free(*(vopvdpp[i]->opv_desc_vector_p),
    305 		    VNODE_OPS_COUNT * sizeof(PFI));
    306 		*(vopvdpp[i]->opv_desc_vector_p) = NULL;
    307 	}
    308 }
    309 
    310 #ifdef DEBUG
    311 static void
    312 vfs_op_check(void)
    313 {
    314 	int i;
    315 
    316 	DODEBUG(printf("Vnode_interface_init.\n"));
    317 
    318 	/*
    319 	 * Check offset of each op.
    320 	 */
    321 	for (i = 0; vfs_op_descs[i]; i++) {
    322 		if (vfs_op_descs[i]->vdesc_offset != i)
    323 			panic("vfs_op_check: vfs_op_desc[] offset mismatch");
    324 	}
    325 
    326 	if (i != VNODE_OPS_COUNT) {
    327 		panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
    328 			i, VNODE_OPS_COUNT);
    329 	}
    330 
    331 	DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
    332 }
    333 #endif /* DEBUG */
    334 
    335 /*
    336  * Common routine to check if an unprivileged mount is allowed.
    337  *
    338  * We export just this part (i.e., without the access control) so that if a
    339  * secmodel wants to implement finer grained user mounts it can do so without
    340  * copying too much code. More elaborate policies (i.e., specific users allowed
    341  * to also create devices and/or introduce set-id binaries, or export
    342  * file-systems) will require a different implementation.
    343  *
    344  * This routine is intended to be called from listener context, and as such
    345  * does not take credentials as an argument.
    346  */
    347 int
    348 usermount_common_policy(struct mount *mp, u_long flags)
    349 {
    350 
    351 	/* No exporting if unprivileged. */
    352 	if (flags & MNT_EXPORTED)
    353 		return EPERM;
    354 
    355 	/* Must have 'nosuid' and 'nodev'. */
    356 	if ((flags & MNT_NODEV) == 0 || (flags & MNT_NOSUID) == 0)
    357 		return EPERM;
    358 
    359 	/* Retain 'noexec'. */
    360 	if ((mp->mnt_flag & MNT_NOEXEC) && (flags & MNT_NOEXEC) == 0)
    361 		return EPERM;
    362 
    363 	return 0;
    364 }
    365 
    366 static int
    367 mount_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    368     void *arg0, void *arg1, void *arg2, void *arg3)
    369 {
    370 	int result;
    371 	enum kauth_system_req req;
    372 
    373 	result = KAUTH_RESULT_DEFER;
    374 	req = (enum kauth_system_req)(uintptr_t)(uintptr_t)arg0;
    375 
    376 	if (action != KAUTH_SYSTEM_MOUNT)
    377 		return result;
    378 
    379 	if (req == KAUTH_REQ_SYSTEM_MOUNT_GET)
    380 		result = KAUTH_RESULT_ALLOW;
    381 	else if (req == KAUTH_REQ_SYSTEM_MOUNT_DEVICE) {
    382 		vnode_t *devvp = arg2;
    383 		accmode_t accmode = (accmode_t)(unsigned long)arg3;
    384 		int error;
    385 
    386 		error = VOP_ACCESS(devvp, accmode, cred);
    387 		if (!error)
    388 			result = KAUTH_RESULT_ALLOW;
    389 	}
    390 
    391 	return result;
    392 }
    393 
    394 /*
    395  * Initialize the vnode structures and initialize each file system type.
    396  */
    397 void
    398 vfsinit(void)
    399 {
    400 
    401 	/*
    402 	 * Attach sysctl nodes
    403 	 */
    404 	sysctl_vfs_setup();
    405 
    406 	/*
    407 	 * Initialize the namei pathname buffer pool and cache.
    408 	 */
    409 	pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    410 	    NULL, IPL_NONE, NULL, NULL, NULL);
    411 	KASSERT(pnbuf_cache != NULL);
    412 
    413 	/*
    414 	 * Initialize the vnode table
    415 	 */
    416 	vntblinit();
    417 
    418 	/*
    419 	 * Initialize the vnode name cache
    420 	 */
    421 	nchinit();
    422 
    423 #ifdef DEBUG
    424 	/*
    425 	 * Check the list of vnode operations.
    426 	 */
    427 	vfs_op_check();
    428 #endif
    429 
    430 	/*
    431 	 * Initialize the special vnode operations.
    432 	 */
    433 	vfs_opv_init(vfs_special_vnodeopv_descs);
    434 
    435 	/*
    436 	 * Initialise generic dirhash.
    437 	 */
    438 	dirhash_init();
    439 
    440 	/*
    441 	 * Initialise VFS hooks.
    442 	 */
    443 	vfs_hooks_init();
    444 
    445 	mount_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
    446 	    mount_listener_cb, NULL);
    447 
    448 	/*
    449 	 * Establish each file system which was statically
    450 	 * included in the kernel.
    451 	 */
    452 	module_init_class(MODULE_CLASS_VFS);
    453 
    454 	/*
    455 	 * Initialize EVFILT_FS for kqueue.
    456 	 */
    457 	vfs_evfilt_fs_init();
    458 }
    459 
    460 /*
    461  * Drop a reference to a file system type.
    462  */
    463 void
    464 vfs_delref(struct vfsops *vfs)
    465 {
    466 
    467 	mutex_enter(&vfs_list_lock);
    468 	vfs->vfs_refcount--;
    469 	mutex_exit(&vfs_list_lock);
    470 }
    471 
    472 /*
    473  * Establish a file system and initialize it.
    474  */
    475 int
    476 vfs_attach(struct vfsops *vfs)
    477 {
    478 	struct vfsops *v;
    479 	int error = 0;
    480 
    481 	mutex_enter(&vfs_list_lock);
    482 
    483 	/*
    484 	 * Make sure this file system doesn't already exist.
    485 	 */
    486 	LIST_FOREACH(v, &vfs_list, vfs_list) {
    487 		if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
    488 			error = EEXIST;
    489 			goto out;
    490 		}
    491 	}
    492 
    493 	/*
    494 	 * Initialize the vnode operations for this file system.
    495 	 */
    496 	vfs_opv_init(vfs->vfs_opv_descs);
    497 
    498 	/*
    499 	 * Now initialize the file system itself.
    500 	 */
    501 	(*vfs->vfs_init)();
    502 
    503 	/*
    504 	 * ...and link it into the kernel's list.
    505 	 */
    506 	LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
    507 
    508 	/*
    509 	 * Sanity: make sure the reference count is 0.
    510 	 */
    511 	vfs->vfs_refcount = 0;
    512  out:
    513 	mutex_exit(&vfs_list_lock);
    514 	return (error);
    515 }
    516 
    517 /*
    518  * Remove a file system from the kernel.
    519  */
    520 int
    521 vfs_detach(struct vfsops *vfs)
    522 {
    523 	struct vfsops *v;
    524 	int error = 0;
    525 
    526 	mutex_enter(&vfs_list_lock);
    527 
    528 	/*
    529 	 * Make sure no one is using the filesystem.
    530 	 */
    531 	if (vfs->vfs_refcount != 0) {
    532 		error = EBUSY;
    533 		goto out;
    534 	}
    535 
    536 	/*
    537 	 * ...and remove it from the kernel's list.
    538 	 */
    539 	LIST_FOREACH(v, &vfs_list, vfs_list) {
    540 		if (v == vfs) {
    541 			LIST_REMOVE(v, vfs_list);
    542 			break;
    543 		}
    544 	}
    545 
    546 	if (v == NULL) {
    547 		error = ESRCH;
    548 		goto out;
    549 	}
    550 
    551 	/*
    552 	 * Now run the file system-specific cleanups.
    553 	 */
    554 	(*vfs->vfs_done)();
    555 
    556 	/*
    557 	 * Free the vnode operations vector.
    558 	 */
    559 	vfs_opv_free(vfs->vfs_opv_descs);
    560  out:
    561  	mutex_exit(&vfs_list_lock);
    562 	return (error);
    563 }
    564 
    565 void
    566 vfs_reinit(void)
    567 {
    568 	struct vfsops *vfs;
    569 
    570 	mutex_enter(&vfs_list_lock);
    571 	LIST_FOREACH(vfs, &vfs_list, vfs_list) {
    572 		if (vfs->vfs_reinit) {
    573 			vfs->vfs_refcount++;
    574 			mutex_exit(&vfs_list_lock);
    575 			(*vfs->vfs_reinit)();
    576 			mutex_enter(&vfs_list_lock);
    577 			vfs->vfs_refcount--;
    578 		}
    579 	}
    580 	mutex_exit(&vfs_list_lock);
    581 }
    582