1 /* $NetBSD: vfs_init.c,v 1.44 2009/05/03 21:25:44 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.44 2009/05/03 21:25:44 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 87 /* 88 * Sigh, such primitive tools are these... 89 */ 90 #if 0 91 #define DODEBUG(A) A 92 #else 93 #define DODEBUG(A) 94 #endif 95 96 /* 97 * The global list of vnode operations. 98 */ 99 extern const struct vnodeop_desc * const vfs_op_descs[]; 100 101 /* 102 * These vnodeopv_descs are listed here because they are not 103 * associated with any particular file system, and thus cannot 104 * be initialized by vfs_attach(). 105 */ 106 extern const struct vnodeopv_desc dead_vnodeop_opv_desc; 107 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc; 108 extern const struct vnodeopv_desc spec_vnodeop_opv_desc; 109 extern const struct vnodeopv_desc sync_vnodeop_opv_desc; 110 111 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = { 112 &dead_vnodeop_opv_desc, 113 &fifo_vnodeop_opv_desc, 114 &spec_vnodeop_opv_desc, 115 &sync_vnodeop_opv_desc, 116 NULL, 117 }; 118 119 struct vfs_list_head vfs_list = /* vfs list */ 120 LIST_HEAD_INITIALIZER(vfs_list); 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 extern int vfs_magiclinks; 151 152 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 153 CTLFLAG_PERMANENT, 154 CTLTYPE_NODE, "vfs", NULL, 155 NULL, 0, NULL, 0, 156 CTL_VFS, CTL_EOL); 157 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 158 CTLFLAG_PERMANENT, 159 CTLTYPE_NODE, "generic", 160 SYSCTL_DESCR("Non-specific vfs related information"), 161 NULL, 0, NULL, 0, 162 CTL_VFS, VFS_GENERIC, CTL_EOL); 163 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 164 CTLFLAG_PERMANENT, 165 CTLTYPE_STRING, "fstypes", 166 SYSCTL_DESCR("List of file systems present"), 167 sysctl_vfs_generic_fstypes, 0, NULL, 0, 168 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL); 169 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL, 170 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 171 CTLTYPE_INT, "magiclinks", 172 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"), 173 NULL, 0, &vfs_magiclinks, 0, 174 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL); 175 } 176 177 178 /* 179 * vfs_init.c 180 * 181 * Allocate and fill in operations vectors. 182 * 183 * An undocumented feature of this approach to defining operations is that 184 * there can be multiple entries in vfs_opv_descs for the same operations 185 * vector. This allows third parties to extend the set of operations 186 * supported by another layer in a binary compatibile way. For example, 187 * assume that NFS needed to be modified to support Ficus. NFS has an entry 188 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 189 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 190 * listing those new operations Ficus adds to NFS, all without modifying the 191 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 192 * that is a(whole)nother story.) This is a feature. 193 */ 194 195 /* 196 * Init the vector, if it needs it. 197 * Also handle backwards compatibility. 198 */ 199 static void 200 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc) 201 { 202 int (**opv_desc_vector)(void *); 203 const struct vnodeopv_entry_desc *opve_descp; 204 205 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p); 206 207 for (opve_descp = vfs_opv_desc->opv_desc_ops; 208 opve_descp->opve_op; 209 opve_descp++) { 210 /* 211 * Sanity check: is this operation listed 212 * in the list of operations? We check this 213 * by seeing if its offset is zero. Since 214 * the default routine should always be listed 215 * first, it should be the only one with a zero 216 * offset. Any other operation with a zero 217 * offset is probably not listed in 218 * vfs_op_descs, and so is probably an error. 219 * 220 * A panic here means the layer programmer 221 * has committed the all-too common bug 222 * of adding a new operation to the layer's 223 * list of vnode operations but 224 * not adding the operation to the system-wide 225 * list of supported operations. 226 */ 227 if (opve_descp->opve_op->vdesc_offset == 0 && 228 opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) { 229 printf("operation %s not listed in %s.\n", 230 opve_descp->opve_op->vdesc_name, "vfs_op_descs"); 231 panic ("vfs_opv_init: bad operation"); 232 } 233 234 /* 235 * Fill in this entry. 236 */ 237 opv_desc_vector[opve_descp->opve_op->vdesc_offset] = 238 opve_descp->opve_impl; 239 } 240 } 241 242 static void 243 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc) 244 { 245 int j; 246 int (**opv_desc_vector)(void *); 247 248 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p); 249 250 /* 251 * Force every operations vector to have a default routine. 252 */ 253 if (opv_desc_vector[VOFFSET(vop_default)] == NULL) 254 panic("vfs_opv_init: operation vector without default routine."); 255 256 for (j = 0; j < VNODE_OPS_COUNT; j++) 257 if (opv_desc_vector[j] == NULL) 258 opv_desc_vector[j] = 259 opv_desc_vector[VOFFSET(vop_default)]; 260 } 261 262 void 263 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp) 264 { 265 int (**opv_desc_vector)(void *); 266 int i; 267 268 /* 269 * Allocate the vectors. 270 */ 271 for (i = 0; vopvdpp[i] != NULL; i++) { 272 opv_desc_vector = 273 kmem_alloc(VNODE_OPS_COUNT * sizeof(PFI), KM_SLEEP); 274 memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI)); 275 *(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector; 276 DODEBUG(printf("vector at %p allocated\n", 277 opv_desc_vector_p)); 278 } 279 280 /* 281 * ...and fill them in. 282 */ 283 for (i = 0; vopvdpp[i] != NULL; i++) 284 vfs_opv_init_explicit(vopvdpp[i]); 285 286 /* 287 * Finally, go back and replace unfilled routines 288 * with their default. 289 */ 290 for (i = 0; vopvdpp[i] != NULL; i++) 291 vfs_opv_init_default(vopvdpp[i]); 292 } 293 294 void 295 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp) 296 { 297 int i; 298 299 /* 300 * Free the vectors allocated in vfs_opv_init(). 301 */ 302 for (i = 0; vopvdpp[i] != NULL; i++) { 303 kmem_free(*(vopvdpp[i]->opv_desc_vector_p), 304 VNODE_OPS_COUNT * sizeof(PFI)); 305 *(vopvdpp[i]->opv_desc_vector_p) = NULL; 306 } 307 } 308 309 #ifdef DEBUG 310 static void 311 vfs_op_check(void) 312 { 313 int i; 314 315 DODEBUG(printf("Vnode_interface_init.\n")); 316 317 /* 318 * Check offset of each op. 319 */ 320 for (i = 0; vfs_op_descs[i]; i++) { 321 if (vfs_op_descs[i]->vdesc_offset != i) 322 panic("vfs_op_check: vfs_op_desc[] offset mismatch"); 323 } 324 325 if (i != VNODE_OPS_COUNT) { 326 panic("vfs_op_check: vnode ops count mismatch (%d != %d)", 327 i, VNODE_OPS_COUNT); 328 } 329 330 DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT)); 331 } 332 #endif /* DEBUG */ 333 334 /* 335 * Initialize the vnode structures and initialize each file system type. 336 */ 337 void 338 vfsinit(void) 339 { 340 341 /* 342 * Attach sysctl nodes 343 */ 344 sysctl_vfs_setup(); 345 346 /* 347 * Initialize the namei pathname buffer pool and cache. 348 */ 349 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl", 350 NULL, IPL_NONE, NULL, NULL, NULL); 351 KASSERT(pnbuf_cache != NULL); 352 353 /* 354 * Initialize the vnode table 355 */ 356 vntblinit(); 357 358 /* 359 * Initialize the vnode name cache 360 */ 361 nchinit(); 362 363 #ifdef DEBUG 364 /* 365 * Check the list of vnode operations. 366 */ 367 vfs_op_check(); 368 #endif 369 370 /* 371 * Initialize the special vnode operations. 372 */ 373 vfs_opv_init(vfs_special_vnodeopv_descs); 374 375 /* 376 * Initialise generic dirhash. 377 */ 378 dirhash_init(); 379 380 /* 381 * Initialise VFS hooks. 382 */ 383 vfs_hooks_init(); 384 385 /* 386 * Establish each file system which was statically 387 * included in the kernel. 388 */ 389 module_init_class(MODULE_CLASS_VFS); 390 } 391 392 /* 393 * Drop a reference to a file system type. 394 */ 395 void 396 vfs_delref(struct vfsops *vfs) 397 { 398 399 mutex_enter(&vfs_list_lock); 400 vfs->vfs_refcount--; 401 mutex_exit(&vfs_list_lock); 402 } 403 404 /* 405 * Establish a file system and initialize it. 406 */ 407 int 408 vfs_attach(struct vfsops *vfs) 409 { 410 struct vfsops *v; 411 int error = 0; 412 413 mutex_enter(&vfs_list_lock); 414 415 /* 416 * Make sure this file system doesn't already exist. 417 */ 418 LIST_FOREACH(v, &vfs_list, vfs_list) { 419 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) { 420 error = EEXIST; 421 goto out; 422 } 423 } 424 425 /* 426 * Initialize the vnode operations for this file system. 427 */ 428 vfs_opv_init(vfs->vfs_opv_descs); 429 430 /* 431 * Now initialize the file system itself. 432 */ 433 (*vfs->vfs_init)(); 434 435 /* 436 * ...and link it into the kernel's list. 437 */ 438 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list); 439 440 /* 441 * Sanity: make sure the reference count is 0. 442 */ 443 vfs->vfs_refcount = 0; 444 out: 445 mutex_exit(&vfs_list_lock); 446 return (error); 447 } 448 449 /* 450 * Remove a file system from the kernel. 451 */ 452 int 453 vfs_detach(struct vfsops *vfs) 454 { 455 struct vfsops *v; 456 int error = 0; 457 458 mutex_enter(&vfs_list_lock); 459 460 /* 461 * Make sure no one is using the filesystem. 462 */ 463 if (vfs->vfs_refcount != 0) { 464 error = EBUSY; 465 goto out; 466 } 467 468 /* 469 * ...and remove it from the kernel's list. 470 */ 471 LIST_FOREACH(v, &vfs_list, vfs_list) { 472 if (v == vfs) { 473 LIST_REMOVE(v, vfs_list); 474 break; 475 } 476 } 477 478 if (v == NULL) { 479 error = ESRCH; 480 goto out; 481 } 482 483 /* 484 * Now run the file system-specific cleanups. 485 */ 486 (*vfs->vfs_done)(); 487 488 /* 489 * Free the vnode operations vector. 490 */ 491 vfs_opv_free(vfs->vfs_opv_descs); 492 out: 493 mutex_exit(&vfs_list_lock); 494 return (error); 495 } 496 497 void 498 vfs_reinit(void) 499 { 500 struct vfsops *vfs; 501 502 mutex_enter(&vfs_list_lock); 503 LIST_FOREACH(vfs, &vfs_list, vfs_list) { 504 if (vfs->vfs_reinit) { 505 vfs->vfs_refcount++; 506 mutex_exit(&vfs_list_lock); 507 (*vfs->vfs_reinit)(); 508 mutex_enter(&vfs_list_lock); 509 vfs->vfs_refcount--; 510 } 511 } 512 mutex_exit(&vfs_list_lock); 513 } 514