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