xref: /netbsd-src/sys/kern/vfs_init.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: vfs_init.c,v 1.18 2001/01/22 12:17:41 jdolecek 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  * 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 <sys/param.h>
81 #include <sys/mount.h>
82 #include <sys/time.h>
83 #include <sys/vnode.h>
84 #include <sys/stat.h>
85 #include <sys/namei.h>
86 #include <sys/ucred.h>
87 #include <sys/buf.h>
88 #include <sys/errno.h>
89 #include <sys/malloc.h>
90 #include <sys/systm.h>
91 
92 /*
93  * Sigh, such primitive tools are these...
94  */
95 #if 0
96 #define DODEBUG(A) A
97 #else
98 #define DODEBUG(A)
99 #endif
100 
101 /*
102  * The global list of vnode operations.
103  */
104 extern const struct vnodeop_desc * const vfs_op_descs[];
105 
106 /*
107  * These vnodeopv_descs are listed here because they are not
108  * associated with any particular file system, and thus cannot
109  * be initialized by vfs_attach().
110  */
111 extern const struct vnodeopv_desc dead_vnodeop_opv_desc;
112 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc;
113 extern const struct vnodeopv_desc spec_vnodeop_opv_desc;
114 extern const struct vnodeopv_desc sync_vnodeop_opv_desc;
115 
116 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
117 	&dead_vnodeop_opv_desc,
118 	&fifo_vnodeop_opv_desc,
119 	&spec_vnodeop_opv_desc,
120 	&sync_vnodeop_opv_desc,
121 	NULL,
122 };
123 
124 /*
125  * This code doesn't work if the defn is **vnodop_defns with cc.
126  * The problem is because of the compiler sometimes putting in an
127  * extra level of indirection for arrays.  It's an interesting
128  * "feature" of C.
129  */
130 typedef int (*PFI) __P((void *));
131 
132 static void vfs_opv_init_explicit __P((const struct vnodeopv_desc *));
133 static void vfs_opv_init_default __P((const struct vnodeopv_desc *));
134 #ifdef DEBUG
135 static void vfs_op_check __P((void));
136 #endif
137 
138 /*
139  * A miscellaneous routine.
140  * A generic "default" routine that just returns an error.
141  */
142 /*ARGSUSED*/
143 int
144 vn_default_error(v)
145 	void *v;
146 {
147 
148 	return (EOPNOTSUPP);
149 }
150 
151 /*
152  * vfs_init.c
153  *
154  * Allocate and fill in operations vectors.
155  *
156  * An undocumented feature of this approach to defining operations is that
157  * there can be multiple entries in vfs_opv_descs for the same operations
158  * vector. This allows third parties to extend the set of operations
159  * supported by another layer in a binary compatibile way. For example,
160  * assume that NFS needed to be modified to support Ficus. NFS has an entry
161  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
162  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
163  * listing those new operations Ficus adds to NFS, all without modifying the
164  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
165  * that is a(whole)nother story.) This is a feature.
166  */
167 
168 /*
169  * Init the vector, if it needs it.
170  * Also handle backwards compatibility.
171  */
172 static void
173 vfs_opv_init_explicit(vfs_opv_desc)
174 	const struct vnodeopv_desc *vfs_opv_desc;
175 {
176 	int (**opv_desc_vector) __P((void *));
177 	const struct vnodeopv_entry_desc *opve_descp;
178 
179 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
180 
181 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
182 	     opve_descp->opve_op;
183 	     opve_descp++) {
184 		/*
185 		 * Sanity check:  is this operation listed
186 		 * in the list of operations?  We check this
187 		 * by seeing if its offest is zero.  Since
188 		 * the default routine should always be listed
189 		 * first, it should be the only one with a zero
190 		 * offset.  Any other operation with a zero
191 		 * offset is probably not listed in
192 		 * vfs_op_descs, and so is probably an error.
193 		 *
194 		 * A panic here means the layer programmer
195 		 * has committed the all-too common bug
196 		 * of adding a new operation to the layer's
197 		 * list of vnode operations but
198 		 * not adding the operation to the system-wide
199 		 * list of supported operations.
200 		 */
201 		if (opve_descp->opve_op->vdesc_offset == 0 &&
202 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
203 			printf("operation %s not listed in %s.\n",
204 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
205 			panic ("vfs_opv_init: bad operation");
206 		}
207 
208 		/*
209 		 * Fill in this entry.
210 		 */
211 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
212 		    opve_descp->opve_impl;
213 	}
214 }
215 
216 static void
217 vfs_opv_init_default(vfs_opv_desc)
218 	const struct vnodeopv_desc *vfs_opv_desc;
219 {
220 	int j;
221 	int (**opv_desc_vector) __P((void *));
222 
223 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
224 
225 	/*
226 	 * Force every operations vector to have a default routine.
227 	 */
228 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
229 		panic("vfs_opv_init: operation vector without default routine.");
230 
231 	for (j = 0; j < VNODE_OPS_COUNT; j++)
232 		if (opv_desc_vector[j] == NULL)
233 			opv_desc_vector[j] =
234 			    opv_desc_vector[VOFFSET(vop_default)];
235 }
236 
237 void
238 vfs_opv_init(vopvdpp)
239 	const struct vnodeopv_desc * const *vopvdpp;
240 {
241 	int (**opv_desc_vector) __P((void *));
242 	int i;
243 
244 	/*
245 	 * Allocate the vectors.
246 	 */
247 	for (i = 0; vopvdpp[i] != NULL; i++) {
248 		/* XXX - shouldn't be M_VNODE */
249 		opv_desc_vector =
250 		    malloc(VNODE_OPS_COUNT * sizeof(PFI), M_VNODE, M_WAITOK);
251 		memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
252 		*(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
253 		DODEBUG(printf("vector at %p allocated\n",
254 		    opv_desc_vector_p));
255 	}
256 
257 	/*
258 	 * ...and fill them in.
259 	 */
260 	for (i = 0; vopvdpp[i] != NULL; i++)
261 		vfs_opv_init_explicit(vopvdpp[i]);
262 
263 	/*
264 	 * Finally, go back and replace unfilled routines
265 	 * with their default.
266 	 */
267 	for (i = 0; vopvdpp[i] != NULL; i++)
268 		vfs_opv_init_default(vopvdpp[i]);
269 }
270 
271 void
272 vfs_opv_free(vopvdpp)
273 	const struct vnodeopv_desc * const *vopvdpp;
274 {
275 	int i;
276 
277 	/*
278 	 * Free the vectors allocated in vfs_opv_init().
279 	 */
280 	for (i = 0; vopvdpp[i] != NULL; i++) {
281 		/* XXX - shouldn't be M_VNODE */
282 		free(*(vopvdpp[i]->opv_desc_vector_p), M_VNODE);
283 		*(vopvdpp[i]->opv_desc_vector_p) = NULL;
284 	}
285 }
286 
287 #ifdef DEBUG
288 static void
289 vfs_op_check()
290 {
291 	int i;
292 
293 	DODEBUG(printf("Vnode_interface_init.\n"));
294 
295 	/*
296 	 * Check offset of each op.
297 	 */
298 	for (i = 0; vfs_op_descs[i]; i++) {
299 		if (vfs_op_descs[i]->vdesc_offset != i)
300 			panic("vfs_op_check: vfs_op_desc[] offset mismatch");
301 	}
302 
303 	if (i != VNODE_OPS_COUNT) {
304 		panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
305 			i, VNODE_OPS_COUNT);
306 	}
307 
308 	DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
309 }
310 #endif /* DEBUG */
311 
312 /*
313  * Routines having to do with the management of the vnode table.
314  */
315 struct vattr va_null;
316 
317 /*
318  * Initialize the vnode structures and initialize each file system type.
319  */
320 void
321 vfsinit()
322 {
323 	extern struct vfsops *vfs_list_initial[];
324 	int i;
325 
326 	/*
327 	 * Initialize the namei pathname buffer pool.
328 	 */
329 	pool_init(&pnbuf_pool, MAXPATHLEN, 0, 0, 0, "pnbufpl",
330 	     0, pool_page_alloc_nointr,  pool_page_free_nointr, M_NAMEI);
331 
332 	/*
333 	 * Initialize the vnode table
334 	 */
335 	vntblinit();
336 
337 	/*
338 	 * Initialize the vnode name cache
339 	 */
340 	nchinit();
341 
342 #ifdef DEBUG
343 	/*
344 	 * Check the list of vnode operations.
345 	 */
346 	vfs_op_check();
347 #endif
348 
349 	/*
350 	 * Initialize the special vnode operations.
351 	 */
352 	vfs_opv_init(vfs_special_vnodeopv_descs);
353 
354 	/*
355 	 * Establish each file system which was statically
356 	 * included in the kernel.
357 	 */
358 	vattr_null(&va_null);
359 	for (i = 0; vfs_list_initial[i] != NULL; i++) {
360 		if (vfs_attach(vfs_list_initial[i])) {
361 			printf("multiple `%s' file systems",
362 			    vfs_list_initial[i]->vfs_name);
363 			panic("vfsinit");
364 		}
365 	}
366 }
367