2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed
6 * to Berkeley by John Heidemann of the UCLA Ficus project.
8 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94
39 * $FreeBSD: src/sys/kern/vfs_init.c,v 1.49 1999/12/12 16:30:34 peter Exp $
40 * $DragonFly: src/sys/kern/vfs_init.c,v 1.2 2003/06/17 04:28:42 dillon Exp $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mount.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 #include <vm/vm_zone.h>
54 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
59 struct vm_zone *namei_zone;
62 * vfs_init() will set maxvfsconf
63 * to the highest defined type number.
66 struct vfsconf *vfsconf;
71 * Allocate and fill in operations vectors.
73 * An undocumented feature of this approach to defining operations is that
74 * there can be multiple entries in vfs_opv_descs for the same operations
75 * vector. This allows third parties to extend the set of operations
76 * supported by another layer in a binary compatibile way. For example,
77 * assume that NFS needed to be modified to support Ficus. NFS has an entry
78 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
79 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
80 * listing those new operations Ficus adds to NFS, all without modifying the
81 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
82 * that is a(whole)nother story.) This is a feature.
85 /* Table of known vnodeop vectors (list of VFS vnode vectors) */
86 static const struct vnodeopv_desc **vnodeopv_descs;
87 static int vnodeopv_num;
89 /* Table of known descs (list of vnode op handlers "vop_access_desc") */
90 static struct vnodeop_desc **vfs_op_descs;
91 static int *vfs_op_desc_refs; /* reference counts */
92 static int num_op_descs;
93 static int vfs_opv_numops;
99 vop_t ***opv_desc_vector_p;
100 vop_t **opv_desc_vector;
101 struct vnodeopv_entry_desc *opve_descp;
102 const struct vnodeopv_desc *opv;
104 if (vfs_op_descs == NULL)
105 panic("vfs_opv_recalc called with null vfs_op_descs");
108 * Run through and make sure all known descs have an offset
110 * vop_default_desc is hardwired at offset 1, and offset 0
111 * is a panic sanity check.
114 for (i = 0; i < num_op_descs; i++)
115 if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1))
116 vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1;
117 for (i = 0; i < num_op_descs; i++)
118 if (vfs_op_descs[i]->vdesc_offset == 0)
119 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++;
121 * Allocate and fill in the vectors
123 for (i = 0; i < vnodeopv_num; i++) {
124 opv = vnodeopv_descs[i];
125 opv_desc_vector_p = opv->opv_desc_vector_p;
126 if (*opv_desc_vector_p)
127 FREE(*opv_desc_vector_p, M_VNODE);
128 MALLOC(*opv_desc_vector_p, vop_t **,
129 vfs_opv_numops * sizeof(vop_t *), M_VNODE, M_WAITOK);
130 if (*opv_desc_vector_p == NULL)
131 panic("no memory for vop_t ** vector");
132 bzero(*opv_desc_vector_p, vfs_opv_numops * sizeof(vop_t *));
134 /* Fill in, with slot 0 being panic */
135 opv_desc_vector = *opv_desc_vector_p;
136 opv_desc_vector[0] = (vop_t *)vop_panic;
137 for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
138 opve_descp = &(opv->opv_desc_ops[j]);
139 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
140 opve_descp->opve_impl;
143 /* Replace unfilled routines with their default (slot 1). */
144 opv_desc_vector = *(opv->opv_desc_vector_p);
145 if (opv_desc_vector[1] == NULL)
146 panic("vfs_opv_recalc: vector without a default.");
147 for (j = 0; j < vfs_opv_numops; j++)
148 if (opv_desc_vector[j] == NULL)
149 opv_desc_vector[j] = opv_desc_vector[1];
154 vfs_add_vnodeops(const void *data)
156 const struct vnodeopv_desc *opv;
157 const struct vnodeopv_desc **newopv;
158 struct vnodeop_desc **newop;
160 vop_t **opv_desc_vector;
161 struct vnodeop_desc *desc;
164 opv = (const struct vnodeopv_desc *)data;
165 MALLOC(newopv, const struct vnodeopv_desc **,
166 (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
168 panic("vfs_add_vnodeops: no memory");
169 if (vnodeopv_descs) {
170 bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
171 FREE(vnodeopv_descs, M_VNODE);
173 newopv[vnodeopv_num] = opv;
174 vnodeopv_descs = newopv;
177 /* See if we have turned up a new vnode op desc */
178 opv_desc_vector = *(opv->opv_desc_vector_p);
179 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
180 for (j = 0; j < num_op_descs; j++) {
181 if (desc == vfs_op_descs[j]) {
182 /* found it, increase reference count */
183 vfs_op_desc_refs[j]++;
187 if (j == num_op_descs) {
188 /* not found, new entry */
189 MALLOC(newop, struct vnodeop_desc **,
190 (num_op_descs + 1) * sizeof(*newop),
193 panic("vfs_add_vnodeops: no memory for desc");
194 /* new reference count (for unload) */
195 MALLOC(newref, int *,
196 (num_op_descs + 1) * sizeof(*newref),
199 panic("vfs_add_vnodeops: no memory for refs");
201 bcopy(vfs_op_descs, newop,
202 num_op_descs * sizeof(*newop));
203 FREE(vfs_op_descs, M_VNODE);
205 if (vfs_op_desc_refs) {
206 bcopy(vfs_op_desc_refs, newref,
207 num_op_descs * sizeof(*newref));
208 FREE(vfs_op_desc_refs, M_VNODE);
210 newop[num_op_descs] = desc;
211 newref[num_op_descs] = 1;
212 vfs_op_descs = newop;
213 vfs_op_desc_refs = newref;
221 vfs_rm_vnodeops(const void *data)
223 const struct vnodeopv_desc *opv;
224 const struct vnodeopv_desc **newopv;
225 struct vnodeop_desc **newop;
227 vop_t **opv_desc_vector;
228 struct vnodeop_desc *desc;
231 opv = (const struct vnodeopv_desc *)data;
232 /* Lower ref counts on descs in the table and release if zero */
233 opv_desc_vector = *(opv->opv_desc_vector_p);
234 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
235 for (j = 0; j < num_op_descs; j++) {
236 if (desc == vfs_op_descs[j]) {
237 /* found it, decrease reference count */
238 vfs_op_desc_refs[j]--;
242 for (j = 0; j < num_op_descs; j++) {
243 if (vfs_op_desc_refs[j] > 0)
245 if (vfs_op_desc_refs[j] < 0)
246 panic("vfs_remove_vnodeops: negative refcnt");
247 MALLOC(newop, struct vnodeop_desc **,
248 (num_op_descs - 1) * sizeof(*newop),
251 panic("vfs_remove_vnodeops: no memory for desc");
252 /* new reference count (for unload) */
253 MALLOC(newref, int *,
254 (num_op_descs - 1) * sizeof(*newref),
257 panic("vfs_remove_vnodeops: no memory for refs");
258 for (k = j; k < (num_op_descs - 1); k++) {
259 vfs_op_descs[k] = vfs_op_descs[k + 1];
260 vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
262 bcopy(vfs_op_descs, newop,
263 (num_op_descs - 1) * sizeof(*newop));
264 bcopy(vfs_op_desc_refs, newref,
265 (num_op_descs - 1) * sizeof(*newref));
266 FREE(vfs_op_descs, M_VNODE);
267 FREE(vfs_op_desc_refs, M_VNODE);
268 vfs_op_descs = newop;
269 vfs_op_desc_refs = newref;
274 for (i = 0; i < vnodeopv_num; i++) {
275 if (vnodeopv_descs[i] == opv) {
276 for (j = i; j < (vnodeopv_num - 1); j++)
277 vnodeopv_descs[j] = vnodeopv_descs[j + 1];
281 if (i == vnodeopv_num)
282 panic("vfs_remove_vnodeops: opv not found");
283 MALLOC(newopv, const struct vnodeopv_desc **,
284 (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
286 panic("vfs_remove_vnodeops: no memory");
287 bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
288 FREE(vnodeopv_descs, M_VNODE);
289 vnodeopv_descs = newopv;
296 * Routines having to do with the management of the vnode table.
298 struct vattr va_null;
301 * Initialize the vnode structures and initialize each file system type.
308 namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
311 * Initialize the vnode table
315 * Initialize the vnode name cache
319 * Initialize each file system type.
320 * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF).
322 vattr_null(&va_null);
323 maxvfsconf = VFS_GENERIC + 1;
325 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
328 vfs_register(struct vfsconf *vfc)
330 struct sysctl_oid *oidp;
331 struct vfsconf *vfsp;
335 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
336 if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
339 vfc->vfc_typenum = maxvfsconf++;
341 vfsp->vfc_next = vfc;
344 vfc->vfc_next = NULL;
347 * If this filesystem has a sysctl node under vfs
348 * (i.e. vfs.xxfs), then change the oid number of that node to
349 * match the filesystem's type number. This allows user code
350 * which uses the type number to read sysctl variables defined
351 * by the filesystem to continue working. Since the oids are
352 * in a sorted list, we need to make sure the order is
353 * preserved by re-registering the oid after modifying its
356 for (oidp = SLIST_FIRST(&sysctl__vfs_children); oidp;
357 oidp = SLIST_NEXT(oidp, oid_link))
358 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
359 sysctl_unregister_oid(oidp);
360 oidp->oid_number = vfc->vfc_typenum;
361 sysctl_register_oid(oidp);
365 * Call init function for this VFS...
367 (*(vfc->vfc_vfsops->vfs_init))(vfc);
374 vfs_unregister(struct vfsconf *vfc)
376 struct vfsconf *vfsp, *prev_vfsp;
377 int error, i, maxtypenum;
379 i = vfc->vfc_typenum;
382 for (vfsp = vfsconf; vfsp;
383 prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
384 if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
389 if (vfsp->vfc_refcount)
391 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
392 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
397 prev_vfsp->vfc_next = vfsp->vfc_next;
399 vfsconf = vfsp->vfc_next;
400 maxtypenum = VFS_GENERIC;
401 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
402 if (maxtypenum < vfsp->vfc_typenum)
403 maxtypenum = vfsp->vfc_typenum;
404 maxvfsconf = maxtypenum + 1;
409 vfs_modevent(module_t mod, int type, void *data)
414 vfc = (struct vfsconf *)data;
419 error = vfs_register(vfc);
424 error = vfs_unregister(vfc);
426 default: /* including MOD_SHUTDOWN */