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