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