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