No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / vfs_init.c
blobf290e54121c49c6bc55a665e270b70f902494d20
1 /* $NetBSD: vfs_init.c,v 1.44 2009/05/03 21:25:44 elad Exp $ */
3 /*-
4 * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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.
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed
38 * to Berkeley by John Heidemann of the UCLA Ficus project.
40 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
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.
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.
66 * @(#)vfs_init.c 8.5 (Berkeley) 5/11/95
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: vfs_init.c,v 1.44 2009/05/03 21:25:44 elad Exp $");
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 #include <sys/kauth.h>
89 * Sigh, such primitive tools are these...
91 #if 0
92 #define DODEBUG(A) A
93 #else
94 #define DODEBUG(A)
95 #endif
98 * The global list of vnode operations.
100 extern const struct vnodeop_desc * const vfs_op_descs[];
103 * These vnodeopv_descs are listed here because they are not
104 * associated with any particular file system, and thus cannot
105 * be initialized by vfs_attach().
107 extern const struct vnodeopv_desc dead_vnodeop_opv_desc;
108 extern const struct vnodeopv_desc fifo_vnodeop_opv_desc;
109 extern const struct vnodeopv_desc spec_vnodeop_opv_desc;
110 extern const struct vnodeopv_desc sync_vnodeop_opv_desc;
112 const struct vnodeopv_desc * const vfs_special_vnodeopv_descs[] = {
113 &dead_vnodeop_opv_desc,
114 &fifo_vnodeop_opv_desc,
115 &spec_vnodeop_opv_desc,
116 &sync_vnodeop_opv_desc,
117 NULL,
120 struct vfs_list_head vfs_list = /* vfs list */
121 LIST_HEAD_INITIALIZER(vfs_list);
123 static kauth_listener_t mount_listener;
126 * This code doesn't work if the defn is **vnodop_defns with cc.
127 * The problem is because of the compiler sometimes putting in an
128 * extra level of indirection for arrays. It's an interesting
129 * "feature" of C.
131 typedef int (*PFI)(void *);
134 * A miscellaneous routine.
135 * A generic "default" routine that just returns an error.
137 /*ARGSUSED*/
139 vn_default_error(void *v)
142 return (EOPNOTSUPP);
145 static struct sysctllog *vfs_sysctllog;
148 * Top level filesystem related information gathering.
150 static void
151 sysctl_vfs_setup(void)
153 extern int vfs_magiclinks;
155 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
156 CTLFLAG_PERMANENT,
157 CTLTYPE_NODE, "vfs", NULL,
158 NULL, 0, NULL, 0,
159 CTL_VFS, CTL_EOL);
160 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
161 CTLFLAG_PERMANENT,
162 CTLTYPE_NODE, "generic",
163 SYSCTL_DESCR("Non-specific vfs related information"),
164 NULL, 0, NULL, 0,
165 CTL_VFS, VFS_GENERIC, CTL_EOL);
166 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
167 CTLFLAG_PERMANENT,
168 CTLTYPE_STRING, "fstypes",
169 SYSCTL_DESCR("List of file systems present"),
170 sysctl_vfs_generic_fstypes, 0, NULL, 0,
171 CTL_VFS, VFS_GENERIC, CTL_CREATE, CTL_EOL);
172 sysctl_createv(&vfs_sysctllog, 0, NULL, NULL,
173 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
174 CTLTYPE_INT, "magiclinks",
175 SYSCTL_DESCR("Whether \"magic\" symlinks are expanded"),
176 NULL, 0, &vfs_magiclinks, 0,
177 CTL_VFS, VFS_GENERIC, VFS_MAGICLINKS, CTL_EOL);
182 * vfs_init.c
184 * Allocate and fill in operations vectors.
186 * An undocumented feature of this approach to defining operations is that
187 * there can be multiple entries in vfs_opv_descs for the same operations
188 * vector. This allows third parties to extend the set of operations
189 * supported by another layer in a binary compatibile way. For example,
190 * assume that NFS needed to be modified to support Ficus. NFS has an entry
191 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
192 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
193 * listing those new operations Ficus adds to NFS, all without modifying the
194 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
195 * that is a(whole)nother story.) This is a feature.
199 * Init the vector, if it needs it.
200 * Also handle backwards compatibility.
202 static void
203 vfs_opv_init_explicit(const struct vnodeopv_desc *vfs_opv_desc)
205 int (**opv_desc_vector)(void *);
206 const struct vnodeopv_entry_desc *opve_descp;
208 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
210 for (opve_descp = vfs_opv_desc->opv_desc_ops;
211 opve_descp->opve_op;
212 opve_descp++) {
214 * Sanity check: is this operation listed
215 * in the list of operations? We check this
216 * by seeing if its offset is zero. Since
217 * the default routine should always be listed
218 * first, it should be the only one with a zero
219 * offset. Any other operation with a zero
220 * offset is probably not listed in
221 * vfs_op_descs, and so is probably an error.
223 * A panic here means the layer programmer
224 * has committed the all-too common bug
225 * of adding a new operation to the layer's
226 * list of vnode operations but
227 * not adding the operation to the system-wide
228 * list of supported operations.
230 if (opve_descp->opve_op->vdesc_offset == 0 &&
231 opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
232 printf("operation %s not listed in %s.\n",
233 opve_descp->opve_op->vdesc_name, "vfs_op_descs");
234 panic ("vfs_opv_init: bad operation");
238 * Fill in this entry.
240 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
241 opve_descp->opve_impl;
245 static void
246 vfs_opv_init_default(const struct vnodeopv_desc *vfs_opv_desc)
248 int j;
249 int (**opv_desc_vector)(void *);
251 opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
254 * Force every operations vector to have a default routine.
256 if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
257 panic("vfs_opv_init: operation vector without default routine.");
259 for (j = 0; j < VNODE_OPS_COUNT; j++)
260 if (opv_desc_vector[j] == NULL)
261 opv_desc_vector[j] =
262 opv_desc_vector[VOFFSET(vop_default)];
265 void
266 vfs_opv_init(const struct vnodeopv_desc * const *vopvdpp)
268 int (**opv_desc_vector)(void *);
269 int i;
272 * Allocate the vectors.
274 for (i = 0; vopvdpp[i] != NULL; i++) {
275 opv_desc_vector =
276 kmem_alloc(VNODE_OPS_COUNT * sizeof(PFI), KM_SLEEP);
277 memset(opv_desc_vector, 0, VNODE_OPS_COUNT * sizeof(PFI));
278 *(vopvdpp[i]->opv_desc_vector_p) = opv_desc_vector;
279 DODEBUG(printf("vector at %p allocated\n",
280 opv_desc_vector_p));
284 * ...and fill them in.
286 for (i = 0; vopvdpp[i] != NULL; i++)
287 vfs_opv_init_explicit(vopvdpp[i]);
290 * Finally, go back and replace unfilled routines
291 * with their default.
293 for (i = 0; vopvdpp[i] != NULL; i++)
294 vfs_opv_init_default(vopvdpp[i]);
297 void
298 vfs_opv_free(const struct vnodeopv_desc * const *vopvdpp)
300 int i;
303 * Free the vectors allocated in vfs_opv_init().
305 for (i = 0; vopvdpp[i] != NULL; i++) {
306 kmem_free(*(vopvdpp[i]->opv_desc_vector_p),
307 VNODE_OPS_COUNT * sizeof(PFI));
308 *(vopvdpp[i]->opv_desc_vector_p) = NULL;
312 #ifdef DEBUG
313 static void
314 vfs_op_check(void)
316 int i;
318 DODEBUG(printf("Vnode_interface_init.\n"));
321 * Check offset of each op.
323 for (i = 0; vfs_op_descs[i]; i++) {
324 if (vfs_op_descs[i]->vdesc_offset != i)
325 panic("vfs_op_check: vfs_op_desc[] offset mismatch");
328 if (i != VNODE_OPS_COUNT) {
329 panic("vfs_op_check: vnode ops count mismatch (%d != %d)",
330 i, VNODE_OPS_COUNT);
333 DODEBUG(printf ("vfs_opv_numops=%d\n", VNODE_OPS_COUNT));
335 #endif /* DEBUG */
338 * Common routine to check if an unprivileged mount is allowed.
340 * We export just this part (i.e., without the access control) so that if a
341 * secmodel wants to implement finer grained user mounts it can do so without
342 * copying too much code. More elaborate policies (i.e., specific users allowed
343 * to also create devices and/or introduce set-id binaries, or export
344 * file-systems) will require a different implementation.
346 * This routine is intended to be called from listener context, and as such
347 * does not take credentials as an argument.
350 usermount_common_policy(struct mount *mp, u_long flags)
353 /* No exporting if unprivileged. */
354 if (flags & MNT_EXPORTED)
355 return EPERM;
357 /* Must have 'nosuid' and 'nodev'. */
358 if ((flags & MNT_NODEV) == 0 || (flags & MNT_NOSUID) == 0)
359 return EPERM;
361 /* Retain 'noexec'. */
362 if ((mp->mnt_flag & MNT_NOEXEC) && (flags & MNT_NOEXEC) == 0)
363 return EPERM;
365 return 0;
368 static int
369 mount_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
370 void *arg0, void *arg1, void *arg2, void *arg3)
372 int result;
373 enum kauth_system_req req;
375 result = KAUTH_RESULT_DEFER;
376 req = (enum kauth_system_req)arg0;
378 if ((action != KAUTH_SYSTEM_MOUNT) ||
379 (req != KAUTH_REQ_SYSTEM_MOUNT_GET))
380 return result;
382 result = KAUTH_RESULT_ALLOW;
384 return result;
388 * Initialize the vnode structures and initialize each file system type.
390 void
391 vfsinit(void)
395 * Attach sysctl nodes
397 sysctl_vfs_setup();
400 * Initialize the namei pathname buffer pool and cache.
402 pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
403 NULL, IPL_NONE, NULL, NULL, NULL);
404 KASSERT(pnbuf_cache != NULL);
407 * Initialize the vnode table
409 vntblinit();
412 * Initialize the vnode name cache
414 nchinit();
416 #ifdef DEBUG
418 * Check the list of vnode operations.
420 vfs_op_check();
421 #endif
424 * Initialize the special vnode operations.
426 vfs_opv_init(vfs_special_vnodeopv_descs);
429 * Initialise generic dirhash.
431 dirhash_init();
434 * Initialise VFS hooks.
436 vfs_hooks_init();
438 mount_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM,
439 mount_listener_cb, NULL);
442 * Establish each file system which was statically
443 * included in the kernel.
445 module_init_class(MODULE_CLASS_VFS);
449 * Drop a reference to a file system type.
451 void
452 vfs_delref(struct vfsops *vfs)
455 mutex_enter(&vfs_list_lock);
456 vfs->vfs_refcount--;
457 mutex_exit(&vfs_list_lock);
461 * Establish a file system and initialize it.
464 vfs_attach(struct vfsops *vfs)
466 struct vfsops *v;
467 int error = 0;
469 mutex_enter(&vfs_list_lock);
472 * Make sure this file system doesn't already exist.
474 LIST_FOREACH(v, &vfs_list, vfs_list) {
475 if (strcmp(vfs->vfs_name, v->vfs_name) == 0) {
476 error = EEXIST;
477 goto out;
482 * Initialize the vnode operations for this file system.
484 vfs_opv_init(vfs->vfs_opv_descs);
487 * Now initialize the file system itself.
489 (*vfs->vfs_init)();
492 * ...and link it into the kernel's list.
494 LIST_INSERT_HEAD(&vfs_list, vfs, vfs_list);
497 * Sanity: make sure the reference count is 0.
499 vfs->vfs_refcount = 0;
500 out:
501 mutex_exit(&vfs_list_lock);
502 return (error);
506 * Remove a file system from the kernel.
509 vfs_detach(struct vfsops *vfs)
511 struct vfsops *v;
512 int error = 0;
514 mutex_enter(&vfs_list_lock);
517 * Make sure no one is using the filesystem.
519 if (vfs->vfs_refcount != 0) {
520 error = EBUSY;
521 goto out;
525 * ...and remove it from the kernel's list.
527 LIST_FOREACH(v, &vfs_list, vfs_list) {
528 if (v == vfs) {
529 LIST_REMOVE(v, vfs_list);
530 break;
534 if (v == NULL) {
535 error = ESRCH;
536 goto out;
540 * Now run the file system-specific cleanups.
542 (*vfs->vfs_done)();
545 * Free the vnode operations vector.
547 vfs_opv_free(vfs->vfs_opv_descs);
548 out:
549 mutex_exit(&vfs_list_lock);
550 return (error);
553 void
554 vfs_reinit(void)
556 struct vfsops *vfs;
558 mutex_enter(&vfs_list_lock);
559 LIST_FOREACH(vfs, &vfs_list, vfs_list) {
560 if (vfs->vfs_reinit) {
561 vfs->vfs_refcount++;
562 mutex_exit(&vfs_list_lock);
563 (*vfs->vfs_reinit)();
564 mutex_enter(&vfs_list_lock);
565 vfs->vfs_refcount--;
568 mutex_exit(&vfs_list_lock);