Home | History | Annotate | Line # | Download | only in kern
vfs_init.c revision 1.12
      1 /*	$NetBSD: vfs_init.c,v 1.12 1998/06/22 22:01:04 sommerfe Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1989, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * This code is derived from software contributed
     45  * to Berkeley by John Heidemann of the UCLA Ficus project.
     46  *
     47  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  * 3. All advertising materials mentioning features or use of this software
     58  *    must display the following acknowledgement:
     59  *	This product includes software developed by the University of
     60  *	California, Berkeley and its contributors.
     61  * 4. Neither the name of the University nor the names of its contributors
     62  *    may be used to endorse or promote products derived from this software
     63  *    without specific prior written permission.
     64  *
     65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     75  * SUCH DAMAGE.
     76  *
     77  *	@(#)vfs_init.c	8.5 (Berkeley) 5/11/95
     78  */
     79 
     80 #include "opt_fifo.h"
     81 
     82 #include <sys/param.h>
     83 #include <sys/mount.h>
     84 #include <sys/time.h>
     85 #include <sys/vnode.h>
     86 #include <sys/stat.h>
     87 #include <sys/namei.h>
     88 #include <sys/ucred.h>
     89 #include <sys/buf.h>
     90 #include <sys/errno.h>
     91 #include <sys/malloc.h>
     92 #include <sys/systm.h>
     93 
     94 /*
     95  * Sigh, such primitive tools are these...
     96  */
     97 #if 0
     98 #define DODEBUG(A) A
     99 #else
    100 #define DODEBUG(A)
    101 #endif
    102 
    103 /*
    104  * The global list of vnode operations.
    105  */
    106 extern struct vnodeop_desc *vfs_op_descs[];
    107 
    108 /*
    109  * These vnodeopv_descs are listed here because they are not
    110  * associated with any particular file system, and thus cannot
    111  * be initialized by vfs_attach().
    112  */
    113 extern struct vnodeopv_desc dead_vnodeop_opv_desc;
    114 #ifdef FIFO
    115 extern struct vnodeopv_desc fifo_vnodeop_opv_desc;
    116 #endif
    117 extern struct vnodeopv_desc spec_vnodeop_opv_desc;
    118 
    119 struct vnodeopv_desc *vfs_special_vnodeopv_descs[] = {
    120 	&dead_vnodeop_opv_desc,
    121 #ifdef FIFO
    122 	&fifo_vnodeop_opv_desc,
    123 #endif
    124 	&spec_vnodeop_opv_desc,
    125 	NULL,
    126 };
    127 
    128 /*
    129  * This code doesn't work if the defn is **vnodop_defns with cc.
    130  * The problem is because of the compiler sometimes putting in an
    131  * extra level of indirection for arrays.  It's an interesting
    132  * "feature" of C.
    133  */
    134 int vfs_opv_numops;
    135 
    136 typedef int (*PFI) __P((void *));
    137 
    138 void vfs_opv_init_explicit __P((struct vnodeopv_desc *));
    139 void vfs_opv_init_default __P((struct vnodeopv_desc *));
    140 void vfs_op_init __P((void));
    141 
    142 /*
    143  * A miscellaneous routine.
    144  * A generic "default" routine that just returns an error.
    145  */
    146 /*ARGSUSED*/
    147 int
    148 vn_default_error(v)
    149 	void *v;
    150 {
    151 
    152 	return (EOPNOTSUPP);
    153 }
    154 
    155 /*
    156  * vfs_init.c
    157  *
    158  * Allocate and fill in operations vectors.
    159  *
    160  * An undocumented feature of this approach to defining operations is that
    161  * there can be multiple entries in vfs_opv_descs for the same operations
    162  * vector. This allows third parties to extend the set of operations
    163  * supported by another layer in a binary compatibile way. For example,
    164  * assume that NFS needed to be modified to support Ficus. NFS has an entry
    165  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
    166  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
    167  * listing those new operations Ficus adds to NFS, all without modifying the
    168  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
    169  * that is a(whole)nother story.) This is a feature.
    170  */
    171 
    172 /*
    173  * Init the vector, if it needs it.
    174  * Also handle backwards compatibility.
    175  */
    176 void
    177 vfs_opv_init_explicit(vfs_opv_desc)
    178 	struct vnodeopv_desc *vfs_opv_desc;
    179 {
    180 	int (**opv_desc_vector) __P((void *));
    181 	struct vnodeopv_entry_desc *opve_descp;
    182 
    183 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
    184 
    185 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
    186 	     opve_descp->opve_op;
    187 	     opve_descp++) {
    188 		/*
    189 		 * Sanity check:  is this operation listed
    190 		 * in the list of operations?  We check this
    191 		 * by seeing if its offest is zero.  Since
    192 		 * the default routine should always be listed
    193 		 * first, it should be the only one with a zero
    194 		 * offset.  Any other operation with a zero
    195 		 * offset is probably not listed in
    196 		 * vfs_op_descs, and so is probably an error.
    197 		 *
    198 		 * A panic here means the layer programmer
    199 		 * has committed the all-too common bug
    200 		 * of adding a new operation to the layer's
    201 		 * list of vnode operations but
    202 		 * not adding the operation to the system-wide
    203 		 * list of supported operations.
    204 		 */
    205 		if (opve_descp->opve_op->vdesc_offset == 0 &&
    206 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
    207 			printf("operation %s not listed in %s.\n",
    208 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
    209 			panic ("vfs_opv_init: bad operation");
    210 		}
    211 
    212 		/*
    213 		 * Fill in this entry.
    214 		 */
    215 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
    216 		    opve_descp->opve_impl;
    217 	}
    218 }
    219 
    220 void
    221 vfs_opv_init_default(vfs_opv_desc)
    222 	struct vnodeopv_desc *vfs_opv_desc;
    223 {
    224 	int j;
    225 	int (**opv_desc_vector) __P((void *));
    226 
    227 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
    228 
    229 	/*
    230 	 * Force every operations vector to have a default routine.
    231 	 */
    232 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
    233 		panic("vfs_opv_init: operation vector without default routine.");
    234 
    235 	for (j = 0; j < vfs_opv_numops; j++)
    236 		if (opv_desc_vector[j] == NULL)
    237 			opv_desc_vector[j] =
    238 			    opv_desc_vector[VOFFSET(vop_default)];
    239 }
    240 
    241 void
    242 vfs_opv_init(vopvdpp)
    243 	struct vnodeopv_desc **vopvdpp;
    244 {
    245 	int (**opv_desc_vector) __P((void *));
    246 	int i;
    247 
    248 	/*
    249 	 * Allocate the vectors.
    250 	 */
    251 	for (i = 0; vopvdpp[i] != NULL; i++) {
    252 		/* XXX - shouldn't be M_VNODE */
    253 		opv_desc_vector =
    254 		    malloc(vfs_opv_numops * sizeof(PFI), M_VNODE, M_WAITOK);
    255 		bzero(opv_desc_vector, vfs_opv_numops * sizeof(PFI));
    256 		*(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
    257 		DODEBUG(printf("vector at %p allocated\n",
    258 		    opv_desc_vector_p));
    259 	}
    260 
    261 	/*
    262 	 * ...and fill them in.
    263 	 */
    264 	for (i = 0; vopvdpp[i] != NULL; i++)
    265 		vfs_opv_init_explicit(vopvdpp[i]);
    266 
    267 	/*
    268 	 * Finally, go back and replace unfilled routines
    269 	 * with their default.
    270 	 */
    271 	for (i = 0; vopvdpp[i] != NULL; i++)
    272 		vfs_opv_init_default(vopvdpp[i]);
    273 }
    274 
    275 void
    276 vfs_opv_free(vopvdpp)
    277 	struct vnodeopv_desc **vopvdpp;
    278 {
    279 	int i;
    280 
    281 	/*
    282 	 * Free the vectors allocated in vfs_opv_init().
    283 	 */
    284 	for (i = 0; vopvdpp[i] != NULL; i++) {
    285 		/* XXX - shouldn't be M_VNODE */
    286 		free(*(vopvdpp[i]->opv_desc_vector_p), M_VNODE);
    287 		*(vopvdpp[i]->opv_desc_vector_p) = NULL;
    288 	}
    289 }
    290 
    291 void
    292 vfs_op_init()
    293 {
    294 	int i;
    295 
    296 	DODEBUG(printf("Vnode_interface_init.\n"));
    297 	/*
    298 	 * Figure out how many ops there are by counting the table,
    299 	 * and assign each its offset.
    300 	 */
    301 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
    302 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
    303 		vfs_opv_numops++;
    304 	}
    305 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
    306 }
    307 
    308 /*
    309  * Routines having to do with the management of the vnode table.
    310  */
    311 struct vattr va_null;
    312 
    313 /*
    314  * Initialize the vnode structures and initialize each file system type.
    315  */
    316 void
    317 vfsinit()
    318 {
    319 	extern struct vfsops *vfs_list_initial[];
    320 	int i;
    321 
    322 	/*
    323 	 * Initialize the vnode table
    324 	 */
    325 	vntblinit();
    326 
    327 	/*
    328 	 * Initialize the vnode name cache
    329 	 */
    330 	nchinit();
    331 
    332 	/*
    333 	 * Initialize the list of vnode operations.
    334 	 */
    335 	vfs_op_init();
    336 
    337 	/*
    338 	 * Initialize the special vnode operations.
    339 	 */
    340 	vfs_opv_init(vfs_special_vnodeopv_descs);
    341 
    342 	/*
    343 	 * Establish each file system which was statically
    344 	 * included in the kernel.
    345 	 */
    346 	vattr_null(&va_null);
    347 	for (i = 0; vfs_list_initial[i] != NULL; i++) {
    348 		if (vfs_attach(vfs_list_initial[i])) {
    349 			printf("multiple `%s' file systems",
    350 			    vfs_list_initial[i]->vfs_name);
    351 			panic("vfsinit");
    352 		}
    353 	}
    354 }
    355