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