xref: /netbsd-src/sys/kern/vfs_init.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: vfs_init.c,v 1.42 2008/12/07 20:58:46 pooka 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.42 2008/12/07 20:58:46 pooka 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 #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 		/* XXX - shouldn't be M_VNODE */
281 		opv_desc_vector =
282 		    malloc(VNODE_OPS_COUNT * sizeof(PFI), M_VNODE, M_WAITOK);
283 		memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
284 		*(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
285 		DODEBUG(printf("vector at %p allocated\n",
286 		    opv_desc_vector_p));
287 	}
288 
289 	/*
290 	 * ...and fill them in.
291 	 */
292 	for (i = 0; vopvdpp[i] != NULL; i++)
293 		vfs_opv_init_explicit(vopvdpp[i]);
294 
295 	/*
296 	 * Finally, go back and replace unfilled routines
297 	 * with their default.
298 	 */
299 	for (i = 0; vopvdpp[i] != NULL; i++)
300 		vfs_opv_init_default(vopvdpp[i]);
301 }
302 
303 void
304 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
305 {
306 	int i;
307 
308 	/*
309 	 * Free the vectors allocated in vfs_opv_init().
310 	 */
311 	for (i = 0; vopvdpp[i] != NULL; i++) {
312 		/* XXX - shouldn't be M_VNODE */
313 		free(*(vopvdpp[i]->opv_desc_vector_p), M_VNODE);
314 		*(vopvdpp[i]->opv_desc_vector_p) = NULL;
315 	}
316 }
317 
318 #ifdef DEBUG
319 static void
320 vfs_op_check(void)
321 {
322 	int i;
323 
324 	DODEBUG(printf("Vnode_interface_init.\n"));
325 
326 	/*
327 	 * Check offset of each op.
328 	 */
329 	for (i = 0; vfs_op_descs[i]; i++) {
330 		if (vfs_op_descs[i]->vdesc_offset != i)
331 			panic("vfs_op_check: vfs_op_desc[] offset mismatch");
332 	}
333 
334 	if (i != VNODE_OPS_COUNT) {
335 		panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
336 			i, VNODE_OPS_COUNT);
337 	}
338 
339 	DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
340 }
341 #endif /* DEBUG */
342 
343 /*
344  * Initialize the vnode structures and initialize each file system type.
345  */
346 void
347 vfsinit(void)
348 {
349 
350 	/*
351 	 * Attach sysctl nodes
352 	 */
353 	sysctl_vfs_setup();
354 
355 	/*
356 	 * Initialize the namei pathname buffer pool and cache.
357 	 */
358 	pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
359 	    NULL, IPL_NONE, NULL, NULL, NULL);
360 	KASSERT(pnbuf_cache != NULL);
361 
362 	/*
363 	 * Initialize the vnode table
364 	 */
365 	vntblinit();
366 
367 	/*
368 	 * Initialize the vnode name cache
369 	 */
370 	nchinit();
371 
372 #ifdef DEBUG
373 	/*
374 	 * Check the list of vnode operations.
375 	 */
376 	vfs_op_check();
377 #endif
378 
379 	/*
380 	 * Initialize the special vnode operations.
381 	 */
382 	vfs_opv_init(vfs_special_vnodeopv_descs);
383 
384 	/*
385 	 * Initialise generic dirhash.
386 	 */
387 	dirhash_init();
388 
389 	/*
390 	 * Initialise VFS hooks.
391 	 */
392 	vfs_hooks_init();
393 
394 	/*
395 	 * Establish each file system which was statically
396 	 * included in the kernel.
397 	 */
398 	module_init_class(MODULE_CLASS_VFS);
399 }
400 
401 /*
402  * Drop a reference to a file system type.
403  */
404 void
405 vfs_delref(struct vfsops *vfs)
406 {
407 
408 	mutex_enter(&vfs_list_lock);
409 	vfs->vfs_refcount--;
410 	mutex_exit(&vfs_list_lock);
411 }
412 
413 /*
414  * Establish a file system and initialize it.
415  */
416 int
417 vfs_attach(struct vfsops *vfs)
418 {
419 	struct vfsops *v;
420 	int error = 0;
421 
422 	mutex_enter(&vfs_list_lock);
423 
424 	/*
425 	 * Make sure this file system doesn't already exist.
426 	 */
427 	LIST_FOREACH(v, &vfs_list, vfs_list) {
428 		if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
429 			error = EEXIST;
430 			goto out;
431 		}
432 	}
433 
434 	/*
435 	 * Initialize the vnode operations for this file system.
436 	 */
437 	vfs_opv_init(vfs->vfs_opv_descs);
438 
439 	/*
440 	 * Now initialize the file system itself.
441 	 */
442 	(*vfs->vfs_init)();
443 
444 	/*
445 	 * ...and link it into the kernel's list.
446 	 */
447 	LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
448 
449 	/*
450 	 * Sanity: make sure the reference count is 0.
451 	 */
452 	vfs->vfs_refcount = 0;
453  out:
454 	mutex_exit(&vfs_list_lock);
455 	return (error);
456 }
457 
458 /*
459  * Remove a file system from the kernel.
460  */
461 int
462 vfs_detach(struct vfsops *vfs)
463 {
464 	struct vfsops *v;
465 	int error = 0;
466 
467 	mutex_enter(&vfs_list_lock);
468 
469 	/*
470 	 * Make sure no one is using the filesystem.
471 	 */
472 	if (vfs->vfs_refcount != 0) {
473 		error = EBUSY;
474 		goto out;
475 	}
476 
477 	/*
478 	 * ...and remove it from the kernel's list.
479 	 */
480 	LIST_FOREACH(v, &vfs_list, vfs_list) {
481 		if (v == vfs) {
482 			LIST_REMOVE(v, vfs_list);
483 			break;
484 		}
485 	}
486 
487 	if (v == NULL) {
488 		error = ESRCH;
489 		goto out;
490 	}
491 
492 	/*
493 	 * Now run the file system-specific cleanups.
494 	 */
495 	(*vfs->vfs_done)();
496 
497 	/*
498 	 * Free the vnode operations vector.
499 	 */
500 	vfs_opv_free(vfs->vfs_opv_descs);
501  out:
502  	mutex_exit(&vfs_list_lock);
503 	return (error);
504 }
505 
506 void
507 vfs_reinit(void)
508 {
509 	struct vfsops *vfs;
510 
511 	mutex_enter(&vfs_list_lock);
512 	LIST_FOREACH(vfs, &vfs_list, vfs_list) {
513 		if (vfs->vfs_reinit) {
514 			vfs->vfs_refcount++;
515 			mutex_exit(&vfs_list_lock);
516 			(*vfs->vfs_reinit)();
517 			mutex_enter(&vfs_list_lock);
518 			vfs->vfs_refcount--;
519 		}
520 	}
521 	mutex_exit(&vfs_list_lock);
522 }
523