1 /* $NetBSD: vfs_init.c,v 1.46 2012/03/13 18:40:55 elad 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.46 2012/03/13 18:40:55 elad 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 /* 98 * The global list of vnode operations. 99 */ 100 extern const struct vnodeop_desc * const vfs_op_descs[]; 101 102 /* 103 * These vnodeopv_descs are listed here because they are not 104 * associated with any particular file system, and thus cannot 105 * be initialized by vfs_attach(). 106 */ 107 extern const struct vnodeopv_desc dead_vnodeop_opv_desc; 108 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc; 109 extern const struct vnodeopv_desc spec_vnodeop_opv_desc; 110 extern const struct vnodeopv_desc sync_vnodeop_opv_desc; 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 &sync_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, "vfs", NULL, 158 NULL, 0, NULL, 0, 159 CTL_VFS, CTL_EOL); 160 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 161 CTLFLAG_PERMANENT, 162 CTLTYPE_NODE, "generic", 163 SYSCTL_DESCR("Non-specific vfs related information"), 164 NULL, 0, NULL, 0, 165 CTL_VFS, VFS_GENERIC, CTL_EOL); 166 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 167 CTLFLAG_PERMANENT, 168 CTLTYPE_STRING, "fstypes", 169 SYSCTL_DESCR("List of file systems present"), 170 sysctl_vfs_generic_fstypes, 0, NULL, 0, 171 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL); 172 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 173 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 174 CTLTYPE_INT, "magiclinks", 175 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"), 176 NULL, 0, &vfs_magiclinks, 0, 177 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, 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)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 mode_t access_mode = (mode_t)(unsigned long)arg3; 386 int error; 387 388 error = VOP_ACCESS(devvp, access_mode, 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 namei pathname buffer pool and cache. 410 */ 411 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl", 412 NULL, IPL_NONE, NULL, NULL, NULL); 413 KASSERT(pnbuf_cache != NULL); 414 415 /* 416 * Initialize the vnode table 417 */ 418 vntblinit(); 419 420 /* 421 * Initialize the vnode name cache 422 */ 423 nchinit(); 424 425 #ifdef DEBUG 426 /* 427 * Check the list of vnode operations. 428 */ 429 vfs_op_check(); 430 #endif 431 432 /* 433 * Initialize the special vnode operations. 434 */ 435 vfs_opv_init(vfs_special_vnodeopv_descs); 436 437 /* 438 * Initialise generic dirhash. 439 */ 440 dirhash_init(); 441 442 /* 443 * Initialise VFS hooks. 444 */ 445 vfs_hooks_init(); 446 447 mount_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM, 448 mount_listener_cb, NULL); 449 450 /* 451 * Establish each file system which was statically 452 * included in the kernel. 453 */ 454 module_init_class(MODULE_CLASS_VFS); 455 } 456 457 /* 458 * Drop a reference to a file system type. 459 */ 460 void 461 vfs_delref(struct vfsops *vfs) 462 { 463 464 mutex_enter(&vfs_list_lock); 465 vfs->vfs_refcount--; 466 mutex_exit(&vfs_list_lock); 467 } 468 469 /* 470 * Establish a file system and initialize it. 471 */ 472 int 473 vfs_attach(struct vfsops *vfs) 474 { 475 struct vfsops *v; 476 int error = 0; 477 478 mutex_enter(&vfs_list_lock); 479 480 /* 481 * Make sure this file system doesn't already exist. 482 */ 483 LIST_FOREACH(v, &vfs_list, vfs_list) { 484 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) { 485 error = EEXIST; 486 goto out; 487 } 488 } 489 490 /* 491 * Initialize the vnode operations for this file system. 492 */ 493 vfs_opv_init(vfs->vfs_opv_descs); 494 495 /* 496 * Now initialize the file system itself. 497 */ 498 (*vfs->vfs_init)(); 499 500 /* 501 * ...and link it into the kernel's list. 502 */ 503 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list); 504 505 /* 506 * Sanity: make sure the reference count is 0. 507 */ 508 vfs->vfs_refcount = 0; 509 out: 510 mutex_exit(&vfs_list_lock); 511 return (error); 512 } 513 514 /* 515 * Remove a file system from the kernel. 516 */ 517 int 518 vfs_detach(struct vfsops *vfs) 519 { 520 struct vfsops *v; 521 int error = 0; 522 523 mutex_enter(&vfs_list_lock); 524 525 /* 526 * Make sure no one is using the filesystem. 527 */ 528 if (vfs->vfs_refcount != 0) { 529 error = EBUSY; 530 goto out; 531 } 532 533 /* 534 * ...and remove it from the kernel's list. 535 */ 536 LIST_FOREACH(v, &vfs_list, vfs_list) { 537 if (v == vfs) { 538 LIST_REMOVE(v, vfs_list); 539 break; 540 } 541 } 542 543 if (v == NULL) { 544 error = ESRCH; 545 goto out; 546 } 547 548 /* 549 * Now run the file system-specific cleanups. 550 */ 551 (*vfs->vfs_done)(); 552 553 /* 554 * Free the vnode operations vector. 555 */ 556 vfs_opv_free(vfs->vfs_opv_descs); 557 out: 558 mutex_exit(&vfs_list_lock); 559 return (error); 560 } 561 562 void 563 vfs_reinit(void) 564 { 565 struct vfsops *vfs; 566 567 mutex_enter(&vfs_list_lock); 568 LIST_FOREACH(vfs, &vfs_list, vfs_list) { 569 if (vfs->vfs_reinit) { 570 vfs->vfs_refcount++; 571 mutex_exit(&vfs_list_lock); 572 (*vfs->vfs_reinit)(); 573 mutex_enter(&vfs_list_lock); 574 vfs->vfs_refcount--; 575 } 576 } 577 mutex_exit(&vfs_list_lock); 578 } 579