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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/linker.h>
44 #include <sys/mount.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
50 static int vfs_register(struct vfsconf *);
51 static int vfs_unregister(struct vfsconf *);
53 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
56 * The highest defined VFS number.
58 int maxvfsconf = VFS_GENERIC + 1;
61 * Single-linked list of configured VFSes.
62 * New entries are added/deleted by vfs_register()/vfs_unregister()
64 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
67 * A Zen vnode attribute structure.
69 * Initialized when the first filesystem registers by vfs_register().
76 * Allocate and fill in operations vectors.
78 * An undocumented feature of this approach to defining operations is that
79 * there can be multiple entries in vfs_opv_descs for the same operations
80 * vector. This allows third parties to extend the set of operations
81 * supported by another layer in a binary compatibile way. For example,
82 * assume that NFS needed to be modified to support Ficus. NFS has an entry
83 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
84 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
85 * listing those new operations Ficus adds to NFS, all without modifying the
86 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
87 * that is a(whole)nother story.) This is a feature.
91 * Routines having to do with the management of the vnode table.
95 vfs_byname(const char *name)
99 if (!strcmp(name, "ffs"))
101 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
102 if (!strcmp(name, vfsp->vfc_name))
108 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
110 struct vfsconf *vfsp;
113 vfsp = vfs_byname(fstype);
117 /* Only load modules for root (very important!). */
121 *error = securelevel_gt(td->td_ucred, 0);
124 *error = linker_load_module(NULL, fstype, NULL, NULL, &lf);
130 /* Look up again to see if the VFS was loaded. */
131 vfsp = vfs_byname(fstype);
134 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
142 /* Register a new filesystem type in the global table */
144 vfs_register(struct vfsconf *vfc)
146 struct sysctl_oid *oidp;
147 struct vfsops *vfsops;
151 vattr_null(&va_null);
155 if (vfc->vfc_version != VFS_VERSION) {
156 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
157 vfc->vfc_name, vfc->vfc_version);
160 if (vfs_byname(vfc->vfc_name) != NULL)
163 vfc->vfc_typenum = maxvfsconf++;
164 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
167 * If this filesystem has a sysctl node under vfs
168 * (i.e. vfs.xxfs), then change the oid number of that node to
169 * match the filesystem's type number. This allows user code
170 * which uses the type number to read sysctl variables defined
171 * by the filesystem to continue working. Since the oids are
172 * in a sorted list, we need to make sure the order is
173 * preserved by re-registering the oid after modifying its
176 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
177 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
178 sysctl_unregister_oid(oidp);
179 oidp->oid_number = vfc->vfc_typenum;
180 sysctl_register_oid(oidp);
184 * Initialise unused ``struct vfsops'' fields, to use
185 * the vfs_std*() functions. Note, we need the mount
186 * and unmount operations, at the least. The check
187 * for vfsops available is just a debugging aid.
189 KASSERT(vfc->vfc_vfsops != NULL,
190 ("Filesystem %s has no vfsops", vfc->vfc_name));
192 * Check the mount and unmount operations.
194 vfsops = vfc->vfc_vfsops;
195 KASSERT(vfsops->vfs_mount != NULL,
196 ("Filesystem %s has no mount op", vfc->vfc_name));
197 KASSERT(vfsops->vfs_unmount != NULL,
198 ("Filesystem %s has no unmount op", vfc->vfc_name));
200 if (vfsops->vfs_root == NULL)
201 /* return file system's root vnode */
202 vfsops->vfs_root = vfs_stdroot;
203 if (vfsops->vfs_quotactl == NULL)
205 vfsops->vfs_quotactl = vfs_stdquotactl;
206 if (vfsops->vfs_statfs == NULL)
207 /* return file system's status */
208 vfsops->vfs_statfs = vfs_stdstatfs;
209 if (vfsops->vfs_sync == NULL)
211 * flush unwritten data (nosync)
212 * file systems can use vfs_stdsync
213 * explicitly by setting it in the
216 vfsops->vfs_sync = vfs_stdnosync;
217 if (vfsops->vfs_vget == NULL)
218 /* convert an inode number to a vnode */
219 vfsops->vfs_vget = vfs_stdvget;
220 if (vfsops->vfs_fhtovp == NULL)
221 /* turn an NFS file handle into a vnode */
222 vfsops->vfs_fhtovp = vfs_stdfhtovp;
223 if (vfsops->vfs_checkexp == NULL)
224 /* check if file system is exported */
225 vfsops->vfs_checkexp = vfs_stdcheckexp;
226 if (vfsops->vfs_vptofh == NULL)
227 /* turn a vnode into an NFS file handle */
228 vfsops->vfs_vptofh = vfs_stdvptofh;
229 if (vfsops->vfs_init == NULL)
230 /* file system specific initialisation */
231 vfsops->vfs_init = vfs_stdinit;
232 if (vfsops->vfs_uninit == NULL)
233 /* file system specific uninitialisation */
234 vfsops->vfs_uninit = vfs_stduninit;
235 if (vfsops->vfs_extattrctl == NULL)
236 /* extended attribute control */
237 vfsops->vfs_extattrctl = vfs_stdextattrctl;
238 if (vfsops->vfs_sysctl == NULL)
239 vfsops->vfs_sysctl = vfs_stdsysctl;
242 * Call init function for this VFS...
244 (*(vfc->vfc_vfsops->vfs_init))(vfc);
250 /* Remove registration of a filesystem type */
252 vfs_unregister(struct vfsconf *vfc)
254 struct vfsconf *vfsp;
255 int error, i, maxtypenum;
257 i = vfc->vfc_typenum;
259 vfsp = vfs_byname(vfc->vfc_name);
262 if (vfsp->vfc_refcount)
264 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
265 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
269 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
270 maxtypenum = VFS_GENERIC;
271 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
272 if (maxtypenum < vfsp->vfc_typenum)
273 maxtypenum = vfsp->vfc_typenum;
274 maxvfsconf = maxtypenum + 1;
279 * Standard kernel module handling code for filesystem modules.
280 * Referenced from VFS_SET().
283 vfs_modevent(module_t mod, int type, void *data)
288 vfc = (struct vfsconf *)data;
293 error = vfs_register(vfc);
298 error = vfs_unregister(vfc);