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