vfs_init.c revision 1.41.2.1 1 /* $NetBSD: vfs_init.c,v 1.41.2.1 2009/01/19 13:19:40 skrll 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.41.2.1 2009/01/19 13:19:40 skrll 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/kmem.h>
82 #include <sys/systm.h>
83 #include <sys/module.h>
84 #include <sys/dirhash.h>
85 #include <sys/sysctl.h>
86
87 /*
88 * Sigh, such primitive tools are these...
89 */
90 #if 0
91 #define DODEBUG(A) A
92 #else
93 #define DODEBUG(A)
94 #endif
95
96 /*
97 * The global list of vnode operations.
98 */
99 extern const struct vnodeop_desc * const vfs_op_descs[];
100
101 /*
102 * These vnodeopv_descs are listed here because they are not
103 * associated with any particular file system, and thus cannot
104 * be initialized by vfs_attach().
105 */
106 extern const struct vnodeopv_desc dead_vnodeop_opv_desc;
107 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc;
108 extern const struct vnodeopv_desc spec_vnodeop_opv_desc;
109 extern const struct vnodeopv_desc sync_vnodeop_opv_desc;
110
111 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
112 &dead_vnodeop_opv_desc,
113 &fifo_vnodeop_opv_desc,
114 &spec_vnodeop_opv_desc,
115 &sync_vnodeop_opv_desc,
116 NULL,
117 };
118
119 struct vfs_list_head vfs_list = /* vfs list */
120 LIST_HEAD_INITIALIZER(vfs_list);
121
122 /*
123 * This code doesn't work if the defn is **vnodop_defns with cc.
124 * The problem is because of the compiler sometimes putting in an
125 * extra level of indirection for arrays. It's an interesting
126 * "feature" of C.
127 */
128 typedef int (*PFI)(void *);
129
130 /*
131 * A miscellaneous routine.
132 * A generic "default" routine that just returns an error.
133 */
134 /*ARGSUSED*/
135 int
136 vn_default_error(void *v)
137 {
138
139 return (EOPNOTSUPP);
140 }
141
142 static struct sysctllog *vfs_sysctllog;
143
144 /*
145 * Top level filesystem related information gathering.
146 */
147 static void
148 sysctl_vfs_setup(void)
149 {
150 extern int dovfsusermount;
151 extern int vfs_magiclinks;
152
153 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
154 CTLFLAG_PERMANENT,
155 CTLTYPE_NODE, "vfs", NULL,
156 NULL, 0, NULL, 0,
157 CTL_VFS, CTL_EOL);
158 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
159 CTLFLAG_PERMANENT,
160 CTLTYPE_NODE, "generic",
161 SYSCTL_DESCR("Non-specific vfs related information"),
162 NULL, 0, NULL, 0,
163 CTL_VFS, VFS_GENERIC, CTL_EOL);
164 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
165 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
166 CTLTYPE_INT, "usermount",
167 SYSCTL_DESCR("Whether unprivileged users may mount "
168 "filesystems"),
169 NULL, 0, &dovfsusermount, 0,
170 CTL_VFS, VFS_GENERIC, VFS_USERMOUNT, CTL_EOL);
171 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
172 CTLFLAG_PERMANENT,
173 CTLTYPE_STRING, "fstypes",
174 SYSCTL_DESCR("List of file systems present"),
175 sysctl_vfs_generic_fstypes, 0, NULL, 0,
176 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
177 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
178 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
179 CTLTYPE_INT, "magiclinks",
180 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"),
181 NULL, 0, &vfs_magiclinks, 0,
182 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL);
183 }
184
185
186 /*
187 * vfs_init.c
188 *
189 * Allocate and fill in operations vectors.
190 *
191 * An undocumented feature of this approach to defining operations is that
192 * there can be multiple entries in vfs_opv_descs for the same operations
193 * vector. This allows third parties to extend the set of operations
194 * supported by another layer in a binary compatibile way. For example,
195 * assume that NFS needed to be modified to support Ficus. NFS has an entry
196 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
197 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
198 * listing those new operations Ficus adds to NFS, all without modifying the
199 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
200 * that is a(whole)nother story.) This is a feature.
201 */
202
203 /*
204 * Init the vector, if it needs it.
205 * Also handle backwards compatibility.
206 */
207 static void
208 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc)
209 {
210 int (**opv_desc_vector)(void *);
211 const struct vnodeopv_entry_desc *opve_descp;
212
213 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
214
215 for (opve_descp = vfs_opv_desc->opv_desc_ops;
216 opve_descp->opve_op;
217 opve_descp++) {
218 /*
219 * Sanity check: is this operation listed
220 * in the list of operations? We check this
221 * by seeing if its offset is zero. Since
222 * the default routine should always be listed
223 * first, it should be the only one with a zero
224 * offset. Any other operation with a zero
225 * offset is probably not listed in
226 * vfs_op_descs, and so is probably an error.
227 *
228 * A panic here means the layer programmer
229 * has committed the all-too common bug
230 * of adding a new operation to the layer's
231 * list of vnode operations but
232 * not adding the operation to the system-wide
233 * list of supported operations.
234 */
235 if (opve_descp->opve_op->vdesc_offset == 0 &&
236 opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
237 printf("operation %s not listed in %s.\n",
238 opve_descp->opve_op->vdesc_name, "vfs_op_descs");
239 panic ("vfs_opv_init: bad operation");
240 }
241
242 /*
243 * Fill in this entry.
244 */
245 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
246 opve_descp->opve_impl;
247 }
248 }
249
250 static void
251 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc)
252 {
253 int j;
254 int (**opv_desc_vector)(void *);
255
256 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
257
258 /*
259 * Force every operations vector to have a default routine.
260 */
261 if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
262 panic("vfs_opv_init: operation vector without default routine.");
263
264 for (j = 0; j < VNODE_OPS_COUNT; j++)
265 if (opv_desc_vector[j] == NULL)
266 opv_desc_vector[j] =
267 opv_desc_vector[VOFFSET(vop_default)];
268 }
269
270 void
271 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp)
272 {
273 int (**opv_desc_vector)(void *);
274 int i;
275
276 /*
277 * Allocate the vectors.
278 */
279 for (i = 0; vopvdpp[i] != NULL; i++) {
280 opv_desc_vector =
281 kmem_alloc(VNODE_OPS_COUNT * sizeof(PFI), KM_SLEEP);
282 memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
283 *(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
284 DODEBUG(printf("vector at %p allocated\n",
285 opv_desc_vector_p));
286 }
287
288 /*
289 * ...and fill them in.
290 */
291 for (i = 0; vopvdpp[i] != NULL; i++)
292 vfs_opv_init_explicit(vopvdpp[i]);
293
294 /*
295 * Finally, go back and replace unfilled routines
296 * with their default.
297 */
298 for (i = 0; vopvdpp[i] != NULL; i++)
299 vfs_opv_init_default(vopvdpp[i]);
300 }
301
302 void
303 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
304 {
305 int i;
306
307 /*
308 * Free the vectors allocated in vfs_opv_init().
309 */
310 for (i = 0; vopvdpp[i] != NULL; i++) {
311 kmem_free(*(vopvdpp[i]->opv_desc_vector_p),
312 VNODE_OPS_COUNT * sizeof(PFI));
313 *(vopvdpp[i]->opv_desc_vector_p) = NULL;
314 }
315 }
316
317 #ifdef DEBUG
318 static void
319 vfs_op_check(void)
320 {
321 int i;
322
323 DODEBUG(printf("Vnode_interface_init.\n"));
324
325 /*
326 * Check offset of each op.
327 */
328 for (i = 0; vfs_op_descs[i]; i++) {
329 if (vfs_op_descs[i]->vdesc_offset != i)
330 panic("vfs_op_check: vfs_op_desc[] offset mismatch");
331 }
332
333 if (i != VNODE_OPS_COUNT) {
334 panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
335 i, VNODE_OPS_COUNT);
336 }
337
338 DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
339 }
340 #endif /* DEBUG */
341
342 /*
343 * Initialize the vnode structures and initialize each file system type.
344 */
345 void
346 vfsinit(void)
347 {
348
349 /*
350 * Attach sysctl nodes
351 */
352 sysctl_vfs_setup();
353
354 /*
355 * Initialize the namei pathname buffer pool and cache.
356 */
357 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
358 NULL, IPL_NONE, NULL, NULL, NULL);
359 KASSERT(pnbuf_cache != NULL);
360
361 /*
362 * Initialize the vnode table
363 */
364 vntblinit();
365
366 /*
367 * Initialize the vnode name cache
368 */
369 nchinit();
370
371 #ifdef DEBUG
372 /*
373 * Check the list of vnode operations.
374 */
375 vfs_op_check();
376 #endif
377
378 /*
379 * Initialize the special vnode operations.
380 */
381 vfs_opv_init(vfs_special_vnodeopv_descs);
382
383 /*
384 * Initialise generic dirhash.
385 */
386 dirhash_init();
387
388 /*
389 * Initialise VFS hooks.
390 */
391 vfs_hooks_init();
392
393 /*
394 * Establish each file system which was statically
395 * included in the kernel.
396 */
397 module_init_class(MODULE_CLASS_VFS);
398 }
399
400 /*
401 * Drop a reference to a file system type.
402 */
403 void
404 vfs_delref(struct vfsops *vfs)
405 {
406
407 mutex_enter(&vfs_list_lock);
408 vfs->vfs_refcount--;
409 mutex_exit(&vfs_list_lock);
410 }
411
412 /*
413 * Establish a file system and initialize it.
414 */
415 int
416 vfs_attach(struct vfsops *vfs)
417 {
418 struct vfsops *v;
419 int error = 0;
420
421 mutex_enter(&vfs_list_lock);
422
423 /*
424 * Make sure this file system doesn't already exist.
425 */
426 LIST_FOREACH(v, &vfs_list, vfs_list) {
427 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
428 error = EEXIST;
429 goto out;
430 }
431 }
432
433 /*
434 * Initialize the vnode operations for this file system.
435 */
436 vfs_opv_init(vfs->vfs_opv_descs);
437
438 /*
439 * Now initialize the file system itself.
440 */
441 (*vfs->vfs_init)();
442
443 /*
444 * ...and link it into the kernel's list.
445 */
446 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
447
448 /*
449 * Sanity: make sure the reference count is 0.
450 */
451 vfs->vfs_refcount = 0;
452 out:
453 mutex_exit(&vfs_list_lock);
454 return (error);
455 }
456
457 /*
458 * Remove a file system from the kernel.
459 */
460 int
461 vfs_detach(struct vfsops *vfs)
462 {
463 struct vfsops *v;
464 int error = 0;
465
466 mutex_enter(&vfs_list_lock);
467
468 /*
469 * Make sure no one is using the filesystem.
470 */
471 if (vfs->vfs_refcount != 0) {
472 error = EBUSY;
473 goto out;
474 }
475
476 /*
477 * ...and remove it from the kernel's list.
478 */
479 LIST_FOREACH(v, &vfs_list, vfs_list) {
480 if (v == vfs) {
481 LIST_REMOVE(v, vfs_list);
482 break;
483 }
484 }
485
486 if (v == NULL) {
487 error = ESRCH;
488 goto out;
489 }
490
491 /*
492 * Now run the file system-specific cleanups.
493 */
494 (*vfs->vfs_done)();
495
496 /*
497 * Free the vnode operations vector.
498 */
499 vfs_opv_free(vfs->vfs_opv_descs);
500 out:
501 mutex_exit(&vfs_list_lock);
502 return (error);
503 }
504
505 void
506 vfs_reinit(void)
507 {
508 struct vfsops *vfs;
509
510 mutex_enter(&vfs_list_lock);
511 LIST_FOREACH(vfs, &vfs_list, vfs_list) {
512 if (vfs->vfs_reinit) {
513 vfs->vfs_refcount++;
514 mutex_exit(&vfs_list_lock);
515 (*vfs->vfs_reinit)();
516 mutex_enter(&vfs_list_lock);
517 vfs->vfs_refcount--;
518 }
519 }
520 mutex_exit(&vfs_list_lock);
521 }
522