2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed
39 * to Berkeley by John Heidemann of the UCLA Ficus project.
41 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94
72 * $FreeBSD: src/sys/kern/vfs_init.c,v 1.59 2002/04/30 18:44:32 dillon Exp $
73 * $DragonFly: src/sys/kern/vfs_init.c,v 1.5 2004/08/13 17:51:09 dillon Exp $
76 * Manage vnode VOP operations vectors
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/mount.h>
82 #include <sys/sysctl.h>
83 #include <sys/vnode.h>
84 #include <sys/malloc.h>
85 #include <vm/vm_zone.h>
87 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
92 struct vm_zone *namei_zone;
95 * vfs_init() will set maxvfsconf
96 * to the highest defined type number.
99 struct vfsconf *vfsconf;
101 static TAILQ_HEAD(, vnodeopv_node) vnodeopv_list;
102 static void vfs_recalc_vnodeops(void);
105 * Add a vnode operations (vnops) vector to the global list.
108 vfs_add_vnodeops(const void *data)
110 struct vnodeopv_node *node;
111 struct vop_ops *vops;
113 node = malloc(sizeof(struct vnodeopv_node), M_VNODE, M_ZERO|M_WAITOK);
115 if ((vops = *node->vdesc->opv_desc_vector) == NULL) {
116 vops = malloc(sizeof(struct vop_ops), M_VNODE, M_ZERO|M_WAITOK);
117 *node->vdesc->opv_desc_vector = vops;
120 TAILQ_INSERT_TAIL(&vnodeopv_list, node, entry);
121 vfs_recalc_vnodeops();
125 * Unlink previously added vnode operations vector.
128 vfs_rm_vnodeops(const void *data)
130 struct vnodeopv_node *node;
131 struct vop_ops *vops;
133 TAILQ_FOREACH(node, &vnodeopv_list, entry) {
134 if ((const void *)node->vdesc == data)
138 printf("vfs_rm_vnodeops: unable to find vnodeopv_desc: %p\n",
142 TAILQ_REMOVE(&vnodeopv_list, node, entry);
143 vops = *node->vdesc->opv_desc_vector;
144 KKASSERT(vops != NULL && vops->vv_refs > 0);
145 if (--vops->vv_refs == 0) {
146 *node->vdesc->opv_desc_vector = NULL;
150 vfs_recalc_vnodeops();
154 * Recalculate VFS operations vectors
157 vfs_recalc_vnodeops(void)
159 struct vnodeopv_node *node;
160 struct vnodeopv_entry_desc *desc;
161 struct vop_ops *vops;
162 struct vop_ops *vnew;
166 * Because vop_ops may be active we can't just blow them away, we
167 * have to generate new vop_ops and then copy them into the running
168 * vop_ops. Any missing entries will be assigned to the default
169 * entry. If the default entry itself is missing it will be assigned
172 TAILQ_FOREACH(node, &vnodeopv_list, entry) {
173 vops = *node->vdesc->opv_desc_vector;
174 if ((vnew = vops->vv_new) == NULL) {
175 vnew = malloc(sizeof(struct vop_ops),
176 M_VNODE, M_ZERO|M_WAITOK);
178 vnew->vop_default = vop_eopnotsupp;
180 for (desc = node->vdesc->opv_desc_ops; desc->opve_op; ++desc) {
181 off = desc->opve_op->vdesc_offset;
182 *(void **)((char *)vnew + off) = desc->opve_func;
184 for (off = __offsetof(struct vop_ops, vop_ops_first_field);
185 off <= __offsetof(struct vop_ops, vop_ops_last_field);
186 off += sizeof(void **)
188 if (*(void **)((char *)vnew + off) == NULL)
189 *(void **)((char *)vnew + off) = vnew->vop_default;
194 * Copy the temporary vops into the running configuration and then
197 TAILQ_FOREACH(node, &vnodeopv_list, entry) {
198 vops = *node->vdesc->opv_desc_vector;
199 if ((vnew = vops->vv_new) == NULL)
201 for (off = __offsetof(struct vop_ops, vop_ops_first_field);
202 off <= __offsetof(struct vop_ops, vop_ops_last_field);
203 off += sizeof(void **)
205 *(void **)((char *)vops + off) =
206 *(void **)((char *)vnew + off);
214 * Routines having to do with the management of the vnode table.
216 struct vattr va_null;
219 * Initialize the vnode structures and initialize each file system type.
225 TAILQ_INIT(&vnodeopv_list);
226 namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
229 * Initialize the vnode table
233 * Initialize the vnode name cache
237 * Initialize each file system type.
238 * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF).
240 vattr_null(&va_null);
241 maxvfsconf = VFS_GENERIC + 1;
243 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
248 * After doing general initialisation, this function will
249 * call the filesystem specific initialisation vector op,
250 * i.e. vfsops->vfs_init().
253 vfs_register(struct vfsconf *vfc)
255 struct sysctl_oid *oidp;
256 struct vfsconf *vfsp;
260 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
261 if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
264 vfc->vfc_typenum = maxvfsconf++;
266 vfsp->vfc_next = vfc;
269 vfc->vfc_next = NULL;
272 * If this filesystem has a sysctl node under vfs
273 * (i.e. vfs.xxfs), then change the oid number of that node to
274 * match the filesystem's type number. This allows user code
275 * which uses the type number to read sysctl variables defined
276 * by the filesystem to continue working. Since the oids are
277 * in a sorted list, we need to make sure the order is
278 * preserved by re-registering the oid after modifying its
281 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
282 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
283 sysctl_unregister_oid(oidp);
284 oidp->oid_number = vfc->vfc_typenum;
285 sysctl_register_oid(oidp);
289 * Call init function for this VFS...
291 (*(vfc->vfc_vfsops->vfs_init))(vfc);
298 * Remove previously registered VFS.
300 * After doing general de-registration like removing sysctl
301 * nodes etc, it will call the filesystem specific vector
302 * op, i.e. vfsops->vfs_uninit().
306 vfs_unregister(struct vfsconf *vfc)
308 struct vfsconf *vfsp, *prev_vfsp;
309 int error, i, maxtypenum;
311 i = vfc->vfc_typenum;
314 for (vfsp = vfsconf; vfsp;
315 prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
316 if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
321 if (vfsp->vfc_refcount)
323 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
324 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
329 prev_vfsp->vfc_next = vfsp->vfc_next;
331 vfsconf = vfsp->vfc_next;
332 maxtypenum = VFS_GENERIC;
333 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
334 if (maxtypenum < vfsp->vfc_typenum)
335 maxtypenum = vfsp->vfc_typenum;
336 maxvfsconf = maxtypenum + 1;
341 vfs_modevent(module_t mod, int type, void *data)
346 vfc = (struct vfsconf *)data;
351 error = vfs_register(vfc);
356 error = vfs_unregister(vfc);
358 default: /* including MOD_SHUTDOWN */