1 /* $NetBSD: vfs_init.c,v 1.45 2009/10/05 04:20:13 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.45 2009/10/05 04:20:13 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 (req != KAUTH_REQ_SYSTEM_MOUNT_GET)) 380 return result; 381 382 result = KAUTH_RESULT_ALLOW; 383 384 return result; 385 } 386 387 /* 388 * Initialize the vnode structures and initialize each file system type. 389 */ 390 void 391 vfsinit(void) 392 { 393 394 /* 395 * Attach sysctl nodes 396 */ 397 sysctl_vfs_setup(); 398 399 /* 400 * Initialize the namei pathname buffer pool and cache. 401 */ 402 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl", 403 NULL, IPL_NONE, NULL, NULL, NULL); 404 KASSERT(pnbuf_cache != NULL); 405 406 /* 407 * Initialize the vnode table 408 */ 409 vntblinit(); 410 411 /* 412 * Initialize the vnode name cache 413 */ 414 nchinit(); 415 416 #ifdef DEBUG 417 /* 418 * Check the list of vnode operations. 419 */ 420 vfs_op_check(); 421 #endif 422 423 /* 424 * Initialize the special vnode operations. 425 */ 426 vfs_opv_init(vfs_special_vnodeopv_descs); 427 428 /* 429 * Initialise generic dirhash. 430 */ 431 dirhash_init(); 432 433 /* 434 * Initialise VFS hooks. 435 */ 436 vfs_hooks_init(); 437 438 mount_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM, 439 mount_listener_cb, NULL); 440 441 /* 442 * Establish each file system which was statically 443 * included in the kernel. 444 */ 445 module_init_class(MODULE_CLASS_VFS); 446 } 447 448 /* 449 * Drop a reference to a file system type. 450 */ 451 void 452 vfs_delref(struct vfsops *vfs) 453 { 454 455 mutex_enter(&vfs_list_lock); 456 vfs->vfs_refcount--; 457 mutex_exit(&vfs_list_lock); 458 } 459 460 /* 461 * Establish a file system and initialize it. 462 */ 463 int 464 vfs_attach(struct vfsops *vfs) 465 { 466 struct vfsops *v; 467 int error = 0; 468 469 mutex_enter(&vfs_list_lock); 470 471 /* 472 * Make sure this file system doesn't already exist. 473 */ 474 LIST_FOREACH(v, &vfs_list, vfs_list) { 475 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) { 476 error = EEXIST; 477 goto out; 478 } 479 } 480 481 /* 482 * Initialize the vnode operations for this file system. 483 */ 484 vfs_opv_init(vfs->vfs_opv_descs); 485 486 /* 487 * Now initialize the file system itself. 488 */ 489 (*vfs->vfs_init)(); 490 491 /* 492 * ...and link it into the kernel's list. 493 */ 494 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list); 495 496 /* 497 * Sanity: make sure the reference count is 0. 498 */ 499 vfs->vfs_refcount = 0; 500 out: 501 mutex_exit(&vfs_list_lock); 502 return (error); 503 } 504 505 /* 506 * Remove a file system from the kernel. 507 */ 508 int 509 vfs_detach(struct vfsops *vfs) 510 { 511 struct vfsops *v; 512 int error = 0; 513 514 mutex_enter(&vfs_list_lock); 515 516 /* 517 * Make sure no one is using the filesystem. 518 */ 519 if (vfs->vfs_refcount != 0) { 520 error = EBUSY; 521 goto out; 522 } 523 524 /* 525 * ...and remove it from the kernel's list. 526 */ 527 LIST_FOREACH(v, &vfs_list, vfs_list) { 528 if (v == vfs) { 529 LIST_REMOVE(v, vfs_list); 530 break; 531 } 532 } 533 534 if (v == NULL) { 535 error = ESRCH; 536 goto out; 537 } 538 539 /* 540 * Now run the file system-specific cleanups. 541 */ 542 (*vfs->vfs_done)(); 543 544 /* 545 * Free the vnode operations vector. 546 */ 547 vfs_opv_free(vfs->vfs_opv_descs); 548 out: 549 mutex_exit(&vfs_list_lock); 550 return (error); 551 } 552 553 void 554 vfs_reinit(void) 555 { 556 struct vfsops *vfs; 557 558 mutex_enter(&vfs_list_lock); 559 LIST_FOREACH(vfs, &vfs_list, vfs_list) { 560 if (vfs->vfs_reinit) { 561 vfs->vfs_refcount++; 562 mutex_exit(&vfs_list_lock); 563 (*vfs->vfs_reinit)(); 564 mutex_enter(&vfs_list_lock); 565 vfs->vfs_refcount--; 566 } 567 } 568 mutex_exit(&vfs_list_lock); 569 } 570