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