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