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
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/sysctl.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
52 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
55 * The highest defined VFS number.
57 int maxvfsconf = VFS_GENERIC + 1;
60 * Single-linked list of configured VFSes.
61 * New entries are added/deleted by vfs_register()/vfs_unregister()
63 struct vfsconf *vfsconf;
68 * Allocate and fill in operations vectors.
70 * An undocumented feature of this approach to defining operations is that
71 * there can be multiple entries in vfs_opv_descs for the same operations
72 * vector. This allows third parties to extend the set of operations
73 * supported by another layer in a binary compatibile way. For example,
74 * assume that NFS needed to be modified to support Ficus. NFS has an entry
75 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
76 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
77 * listing those new operations Ficus adds to NFS, all without modifying the
78 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
79 * that is a(whole)nother story.) This is a feature.
82 /* Table of known vnodeop vectors (list of VFS vnode vectors) */
83 static const struct vnodeopv_desc **vnodeopv_descs;
84 static int vnodeopv_num;
86 /* Table of known descs (list of vnode op handlers "vop_access_desc") */
87 static struct vnodeop_desc **vfs_op_descs;
88 /* Reference counts for vfs_op_descs */
89 static int *vfs_op_desc_refs;
90 /* Number of descriptions */
91 static int num_op_descs;
92 /* Number of entries in each description */
93 static int vfs_opv_numops;
96 * Recalculate the operations vector/description (those parts of it that can
97 * be recalculated, that is.)
98 * XXX It may be preferable to replace this function with an invariant check
99 * and a set of functions that should keep the table invariant.
105 vop_t ***opv_desc_vector_p;
106 vop_t **opv_desc_vector;
107 struct vnodeopv_entry_desc *opve_descp;
108 const struct vnodeopv_desc *opv;
110 if (vfs_op_descs == NULL)
111 panic("vfs_opv_recalc called with null vfs_op_descs");
114 * Run through and make sure all known descs have an offset
116 * vop_default_desc is hardwired at offset 1, and offset 0
117 * is a panic sanity check.
120 for (i = 0; i < num_op_descs; i++)
121 if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1))
122 vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1;
123 for (i = 0; i < num_op_descs; i++)
124 if (vfs_op_descs[i]->vdesc_offset == 0)
125 vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++;
127 * Allocate and fill in the vectors
129 for (i = 0; i < vnodeopv_num; i++) {
130 opv = vnodeopv_descs[i];
131 opv_desc_vector_p = opv->opv_desc_vector_p;
132 if (*opv_desc_vector_p)
133 FREE(*opv_desc_vector_p, M_VNODE);
134 MALLOC(*opv_desc_vector_p, vop_t **,
135 vfs_opv_numops * sizeof(vop_t *), M_VNODE,
137 if (*opv_desc_vector_p == NULL)
138 panic("no memory for vop_t ** vector");
140 /* Fill in, with slot 0 being to return EOPNOTSUPP */
141 opv_desc_vector = *opv_desc_vector_p;
142 opv_desc_vector[0] = (vop_t *)vop_eopnotsupp;
143 for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
144 opve_descp = &(opv->opv_desc_ops[j]);
145 opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
146 opve_descp->opve_impl;
149 /* Replace unfilled routines with their default (slot 1). */
150 opv_desc_vector = *(opv->opv_desc_vector_p);
151 if (opv_desc_vector[1] == NULL)
152 panic("vfs_opv_recalc: vector without a default.");
153 for (j = 0; j < vfs_opv_numops; j++)
154 if (opv_desc_vector[j] == NULL)
155 opv_desc_vector[j] = opv_desc_vector[1];
159 /* Add a set of vnode operations (a description) to the table above. */
161 vfs_add_vnodeops(const void *data)
163 const struct vnodeopv_desc *opv;
164 const struct vnodeopv_desc **newopv;
165 struct vnodeop_desc **newop;
167 vop_t **opv_desc_vector;
168 struct vnodeop_desc *desc;
171 opv = (const struct vnodeopv_desc *)data;
172 MALLOC(newopv, const struct vnodeopv_desc **,
173 (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
175 panic("vfs_add_vnodeops: no memory");
176 if (vnodeopv_descs) {
177 bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
178 FREE(vnodeopv_descs, M_VNODE);
180 newopv[vnodeopv_num] = opv;
181 vnodeopv_descs = newopv;
184 /* See if we have turned up a new vnode op desc */
185 opv_desc_vector = *(opv->opv_desc_vector_p);
186 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
187 for (j = 0; j < num_op_descs; j++) {
188 if (desc == vfs_op_descs[j]) {
189 /* found it, increase reference count */
190 vfs_op_desc_refs[j]++;
194 if (j == num_op_descs) {
195 /* not found, new entry */
196 MALLOC(newop, struct vnodeop_desc **,
197 (num_op_descs + 1) * sizeof(*newop),
200 panic("vfs_add_vnodeops: no memory for desc");
201 /* new reference count (for unload) */
202 MALLOC(newref, int *,
203 (num_op_descs + 1) * sizeof(*newref),
206 panic("vfs_add_vnodeops: no memory for refs");
208 bcopy(vfs_op_descs, newop,
209 num_op_descs * sizeof(*newop));
210 FREE(vfs_op_descs, M_VNODE);
212 if (vfs_op_desc_refs) {
213 bcopy(vfs_op_desc_refs, newref,
214 num_op_descs * sizeof(*newref));
215 FREE(vfs_op_desc_refs, M_VNODE);
217 newop[num_op_descs] = desc;
218 newref[num_op_descs] = 1;
219 vfs_op_descs = newop;
220 vfs_op_desc_refs = newref;
227 /* Remove a vnode type from the vnode description table above. */
229 vfs_rm_vnodeops(const void *data)
231 const struct vnodeopv_desc *opv;
232 const struct vnodeopv_desc **newopv;
233 struct vnodeop_desc **newop;
235 vop_t **opv_desc_vector;
236 struct vnodeop_desc *desc;
239 opv = (const struct vnodeopv_desc *)data;
240 /* Lower ref counts on descs in the table and release if zero */
241 opv_desc_vector = *(opv->opv_desc_vector_p);
242 for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
243 for (j = 0; j < num_op_descs; j++) {
244 if (desc == vfs_op_descs[j]) {
245 /* found it, decrease reference count */
246 vfs_op_desc_refs[j]--;
250 for (j = 0; j < num_op_descs; j++) {
251 if (vfs_op_desc_refs[j] > 0)
253 if (vfs_op_desc_refs[j] < 0)
254 panic("vfs_remove_vnodeops: negative refcnt");
255 MALLOC(newop, struct vnodeop_desc **,
256 (num_op_descs - 1) * sizeof(*newop),
259 panic("vfs_remove_vnodeops: no memory for desc");
260 /* new reference count (for unload) */
261 MALLOC(newref, int *,
262 (num_op_descs - 1) * sizeof(*newref),
265 panic("vfs_remove_vnodeops: no memory for refs");
266 for (k = j; k < (num_op_descs - 1); k++) {
267 vfs_op_descs[k] = vfs_op_descs[k + 1];
268 vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
270 bcopy(vfs_op_descs, newop,
271 (num_op_descs - 1) * sizeof(*newop));
272 bcopy(vfs_op_desc_refs, newref,
273 (num_op_descs - 1) * sizeof(*newref));
274 FREE(vfs_op_descs, M_VNODE);
275 FREE(vfs_op_desc_refs, M_VNODE);
276 vfs_op_descs = newop;
277 vfs_op_desc_refs = newref;
282 for (i = 0; i < vnodeopv_num; i++) {
283 if (vnodeopv_descs[i] == opv) {
284 for (j = i; j < (vnodeopv_num - 1); j++)
285 vnodeopv_descs[j] = vnodeopv_descs[j + 1];
289 if (i == vnodeopv_num)
290 panic("vfs_remove_vnodeops: opv not found");
291 MALLOC(newopv, const struct vnodeopv_desc **,
292 (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
294 panic("vfs_remove_vnodeops: no memory");
295 bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
296 FREE(vnodeopv_descs, M_VNODE);
297 vnodeopv_descs = newopv;
304 * Routines having to do with the management of the vnode table.
306 struct vattr va_null;
309 * Initialize the vnode structures and initialize each file system type.
316 vattr_null(&va_null);
318 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
320 /* Register a new file system type in the global table */
322 vfs_register(struct vfsconf *vfc)
324 struct sysctl_oid *oidp;
325 struct vfsconf *vfsp;
329 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
330 if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
333 vfc->vfc_typenum = maxvfsconf++;
335 vfsp->vfc_next = vfc;
338 vfc->vfc_next = NULL;
341 * If this filesystem has a sysctl node under vfs
342 * (i.e. vfs.xxfs), then change the oid number of that node to
343 * match the filesystem's type number. This allows user code
344 * which uses the type number to read sysctl variables defined
345 * by the filesystem to continue working. Since the oids are
346 * in a sorted list, we need to make sure the order is
347 * preserved by re-registering the oid after modifying its
350 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
351 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
352 sysctl_unregister_oid(oidp);
353 oidp->oid_number = vfc->vfc_typenum;
354 sysctl_register_oid(oidp);
358 * Call init function for this VFS...
360 (*(vfc->vfc_vfsops->vfs_init))(vfc);
366 /* Remove registration of a file system type */
368 vfs_unregister(struct vfsconf *vfc)
370 struct vfsconf *vfsp, *prev_vfsp;
371 int error, i, maxtypenum;
373 i = vfc->vfc_typenum;
376 for (vfsp = vfsconf; vfsp;
377 prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
378 if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
383 if (vfsp->vfc_refcount)
385 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
386 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
391 prev_vfsp->vfc_next = vfsp->vfc_next;
393 vfsconf = vfsp->vfc_next;
394 maxtypenum = VFS_GENERIC;
395 for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
396 if (maxtypenum < vfsp->vfc_typenum)
397 maxtypenum = vfsp->vfc_typenum;
398 maxvfsconf = maxtypenum + 1;
403 * Standard kernel module handling code for file system modules.
404 * Referenced from VFS_SET().
407 vfs_modevent(module_t mod, int type, void *data)
412 vfc = (struct vfsconf *)data;
417 error = vfs_register(vfc);
422 error = vfs_unregister(vfc);
424 default: /* including MOD_SHUTDOWN */