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