vfs_init.c revision 1.37.10.1 1 /* $NetBSD: vfs_init.c,v 1.37.10.1 2008/05/16 02:25:28 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2008 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed
38 * to Berkeley by John Heidemann of the UCLA Ficus project.
39 *
40 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: vfs_init.c,v 1.37.10.1 2008/05/16 02:25:28 yamt Exp $");
71
72 #include <sys/param.h>
73 #include <sys/mount.h>
74 #include <sys/time.h>
75 #include <sys/vnode.h>
76 #include <sys/stat.h>
77 #include <sys/namei.h>
78 #include <sys/ucred.h>
79 #include <sys/buf.h>
80 #include <sys/errno.h>
81 #include <sys/malloc.h>
82 #include <sys/systm.h>
83 #include <sys/module.h>
84
85 /*
86 * Sigh, such primitive tools are these...
87 */
88 #if 0
89 #define DODEBUG(A) A
90 #else
91 #define DODEBUG(A)
92 #endif
93
94 /*
95 * The global list of vnode operations.
96 */
97 extern const struct vnodeop_desc * const vfs_op_descs[];
98
99 /*
100 * These vnodeopv_descs are listed here because they are not
101 * associated with any particular file system, and thus cannot
102 * be initialized by vfs_attach().
103 */
104 extern const struct vnodeopv_desc dead_vnodeop_opv_desc;
105 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc;
106 extern const struct vnodeopv_desc spec_vnodeop_opv_desc;
107 extern const struct vnodeopv_desc sync_vnodeop_opv_desc;
108
109 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
110 &dead_vnodeop_opv_desc,
111 &fifo_vnodeop_opv_desc,
112 &spec_vnodeop_opv_desc,
113 &sync_vnodeop_opv_desc,
114 NULL,
115 };
116
117 struct vfs_list_head vfs_list = /* vfs list */
118 LIST_HEAD_INITIALIZER(vfs_list);
119
120 /*
121 * This code doesn't work if the defn is **vnodop_defns with cc.
122 * The problem is because of the compiler sometimes putting in an
123 * extra level of indirection for arrays. It's an interesting
124 * "feature" of C.
125 */
126 typedef int (*PFI)(void *);
127
128 /*
129 * A miscellaneous routine.
130 * A generic "default" routine that just returns an error.
131 */
132 /*ARGSUSED*/
133 int
134 vn_default_error(void *v)
135 {
136
137 return (EOPNOTSUPP);
138 }
139
140 /*
141 * vfs_init.c
142 *
143 * Allocate and fill in operations vectors.
144 *
145 * An undocumented feature of this approach to defining operations is that
146 * there can be multiple entries in vfs_opv_descs for the same operations
147 * vector. This allows third parties to extend the set of operations
148 * supported by another layer in a binary compatibile way. For example,
149 * assume that NFS needed to be modified to support Ficus. NFS has an entry
150 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
151 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
152 * listing those new operations Ficus adds to NFS, all without modifying the
153 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
154 * that is a(whole)nother story.) This is a feature.
155 */
156
157 /*
158 * Init the vector, if it needs it.
159 * Also handle backwards compatibility.
160 */
161 static void
162 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc)
163 {
164 int (**opv_desc_vector)(void *);
165 const struct vnodeopv_entry_desc *opve_descp;
166
167 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
168
169 for (opve_descp = vfs_opv_desc->opv_desc_ops;
170 opve_descp->opve_op;
171 opve_descp++) {
172 /*
173 * Sanity check: is this operation listed
174 * in the list of operations? We check this
175 * by seeing if its offset is zero. Since
176 * the default routine should always be listed
177 * first, it should be the only one with a zero
178 * offset. Any other operation with a zero
179 * offset is probably not listed in
180 * vfs_op_descs, and so is probably an error.
181 *
182 * A panic here means the layer programmer
183 * has committed the all-too common bug
184 * of adding a new operation to the layer's
185 * list of vnode operations but
186 * not adding the operation to the system-wide
187 * list of supported operations.
188 */
189 if (opve_descp->opve_op->vdesc_offset == 0 &&
190 opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
191 printf("operation %s not listed in %s.\n",
192 opve_descp->opve_op->vdesc_name, "vfs_op_descs");
193 panic ("vfs_opv_init: bad operation");
194 }
195
196 /*
197 * Fill in this entry.
198 */
199 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
200 opve_descp->opve_impl;
201 }
202 }
203
204 static void
205 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc)
206 {
207 int j;
208 int (**opv_desc_vector)(void *);
209
210 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
211
212 /*
213 * Force every operations vector to have a default routine.
214 */
215 if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
216 panic("vfs_opv_init: operation vector without default routine.");
217
218 for (j = 0; j < VNODE_OPS_COUNT; j++)
219 if (opv_desc_vector[j] == NULL)
220 opv_desc_vector[j] =
221 opv_desc_vector[VOFFSET(vop_default)];
222 }
223
224 void
225 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp)
226 {
227 int (**opv_desc_vector)(void *);
228 int i;
229
230 /*
231 * Allocate the vectors.
232 */
233 for (i = 0; vopvdpp[i] != NULL; i++) {
234 /* XXX - shouldn't be M_VNODE */
235 opv_desc_vector =
236 malloc(VNODE_OPS_COUNT * sizeof(PFI), M_VNODE, M_WAITOK);
237 memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
238 *(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
239 DODEBUG(printf("vector at %p allocated\n",
240 opv_desc_vector_p));
241 }
242
243 /*
244 * ...and fill them in.
245 */
246 for (i = 0; vopvdpp[i] != NULL; i++)
247 vfs_opv_init_explicit(vopvdpp[i]);
248
249 /*
250 * Finally, go back and replace unfilled routines
251 * with their default.
252 */
253 for (i = 0; vopvdpp[i] != NULL; i++)
254 vfs_opv_init_default(vopvdpp[i]);
255 }
256
257 void
258 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
259 {
260 int i;
261
262 /*
263 * Free the vectors allocated in vfs_opv_init().
264 */
265 for (i = 0; vopvdpp[i] != NULL; i++) {
266 /* XXX - shouldn't be M_VNODE */
267 free(*(vopvdpp[i]->opv_desc_vector_p), M_VNODE);
268 *(vopvdpp[i]->opv_desc_vector_p) = NULL;
269 }
270 }
271
272 #ifdef DEBUG
273 static void
274 vfs_op_check(void)
275 {
276 int i;
277
278 DODEBUG(printf("Vnode_interface_init.\n"));
279
280 /*
281 * Check offset of each op.
282 */
283 for (i = 0; vfs_op_descs[i]; i++) {
284 if (vfs_op_descs[i]->vdesc_offset != i)
285 panic("vfs_op_check: vfs_op_desc[] offset mismatch");
286 }
287
288 if (i != VNODE_OPS_COUNT) {
289 panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
290 i, VNODE_OPS_COUNT);
291 }
292
293 DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
294 }
295 #endif /* DEBUG */
296
297 /*
298 * Initialize the vnode structures and initialize each file system type.
299 */
300 void
301 vfsinit(void)
302 {
303
304 /*
305 * Initialize the namei pathname buffer pool and cache.
306 */
307 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
308 NULL, IPL_NONE, NULL, NULL, NULL);
309 KASSERT(pnbuf_cache != NULL);
310
311 /*
312 * Initialize the vnode table
313 */
314 vntblinit();
315
316 /*
317 * Initialize the vnode name cache
318 */
319 nchinit();
320
321 #ifdef DEBUG
322 /*
323 * Check the list of vnode operations.
324 */
325 vfs_op_check();
326 #endif
327
328 /*
329 * Initialize the special vnode operations.
330 */
331 vfs_opv_init(vfs_special_vnodeopv_descs);
332
333 /*
334 * Initialise VFS hooks.
335 */
336 vfs_hooks_init();
337
338 /*
339 * Establish each file system which was statically
340 * included in the kernel.
341 */
342 module_init_class(MODULE_CLASS_VFS);
343 }
344
345 /*
346 * Drop a reference to a file system type.
347 */
348 void
349 vfs_delref(struct vfsops *vfs)
350 {
351
352 mutex_enter(&vfs_list_lock);
353 vfs->vfs_refcount--;
354 mutex_exit(&vfs_list_lock);
355 }
356
357 /*
358 * Establish a file system and initialize it.
359 */
360 int
361 vfs_attach(struct vfsops *vfs)
362 {
363 struct vfsops *v;
364 int error = 0;
365
366 mutex_enter(&vfs_list_lock);
367
368 /*
369 * Make sure this file system doesn't already exist.
370 */
371 LIST_FOREACH(v, &vfs_list, vfs_list) {
372 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
373 error = EEXIST;
374 goto out;
375 }
376 }
377
378 /*
379 * Initialize the vnode operations for this file system.
380 */
381 vfs_opv_init(vfs->vfs_opv_descs);
382
383 /*
384 * Now initialize the file system itself.
385 */
386 (*vfs->vfs_init)();
387
388 /*
389 * ...and link it into the kernel's list.
390 */
391 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
392
393 /*
394 * Sanity: make sure the reference count is 0.
395 */
396 vfs->vfs_refcount = 0;
397 out:
398 mutex_exit(&vfs_list_lock);
399 return (error);
400 }
401
402 /*
403 * Remove a file system from the kernel.
404 */
405 int
406 vfs_detach(struct vfsops *vfs)
407 {
408 struct vfsops *v;
409 int error = 0;
410
411 mutex_enter(&vfs_list_lock);
412
413 /*
414 * Make sure no one is using the filesystem.
415 */
416 if (vfs->vfs_refcount != 0) {
417 error = EBUSY;
418 goto out;
419 }
420
421 /*
422 * ...and remove it from the kernel's list.
423 */
424 LIST_FOREACH(v, &vfs_list, vfs_list) {
425 if (v == vfs) {
426 LIST_REMOVE(v, vfs_list);
427 break;
428 }
429 }
430
431 if (v == NULL) {
432 error = ESRCH;
433 goto out;
434 }
435
436 /*
437 * Now run the file system-specific cleanups.
438 */
439 (*vfs->vfs_done)();
440
441 /*
442 * Free the vnode operations vector.
443 */
444 vfs_opv_free(vfs->vfs_opv_descs);
445 out:
446 mutex_exit(&vfs_list_lock);
447 return (error);
448 }
449
450 void
451 vfs_reinit(void)
452 {
453 struct vfsops *vfs;
454
455 mutex_enter(&vfs_list_lock);
456 LIST_FOREACH(vfs, &vfs_list, vfs_list) {
457 if (vfs->vfs_reinit) {
458 vfs->vfs_refcount++;
459 mutex_exit(&vfs_list_lock);
460 (*vfs->vfs_reinit)();
461 mutex_enter(&vfs_list_lock);
462 vfs->vfs_refcount--;
463 }
464 }
465 mutex_exit(&vfs_list_lock);
466 }
467