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/fnv_hash.h>
43 #include <sys/kernel.h>
44 #include <sys/linker.h>
45 #include <sys/mount.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
52 static int vfs_register(struct vfsconf *);
53 static int vfs_unregister(struct vfsconf *);
55 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
58 * The highest defined VFS number.
60 int maxvfsconf = VFS_GENERIC + 1;
63 * Single-linked list of configured VFSes.
64 * New entries are added/deleted by vfs_register()/vfs_unregister()
66 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
69 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
70 * calculation on vfc_name, so that it doesn't change when file systems are
71 * loaded in a different order. This will avoid the NFS server file handles from
72 * changing for file systems that use vfc_typenum in their fsid.
74 static int vfs_typenumhash = 1;
75 TUNABLE_INT("vfs.typenumhash", &vfs_typenumhash);
76 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
77 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
78 "change when file systems are loaded in a different order.");
81 * A Zen vnode attribute structure.
83 * Initialized when the first filesystem registers by vfs_register().
90 * Allocate and fill in operations vectors.
92 * An undocumented feature of this approach to defining operations is that
93 * there can be multiple entries in vfs_opv_descs for the same operations
94 * vector. This allows third parties to extend the set of operations
95 * supported by another layer in a binary compatibile way. For example,
96 * assume that NFS needed to be modified to support Ficus. NFS has an entry
97 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
98 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
99 * listing those new operations Ficus adds to NFS, all without modifying the
100 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
101 * that is a(whole)nother story.) This is a feature.
105 * Routines having to do with the management of the vnode table.
109 vfs_byname(const char *name)
111 struct vfsconf *vfsp;
113 if (!strcmp(name, "ffs"))
115 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
116 if (!strcmp(name, vfsp->vfc_name))
122 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
124 struct vfsconf *vfsp;
127 vfsp = vfs_byname(fstype);
131 /* Try to load the respective module. */
132 *error = kern_kldload(td, fstype, &fileid);
133 loaded = (*error == 0);
134 if (*error == EEXIST)
139 /* Look up again to see if the VFS was loaded. */
140 vfsp = vfs_byname(fstype);
143 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
151 /* Register a new filesystem type in the global table */
153 vfs_register(struct vfsconf *vfc)
155 struct sysctl_oid *oidp;
156 struct vfsops *vfsops;
158 struct vfsconf *tvfc;
163 vattr_null(&va_null);
167 if (vfc->vfc_version != VFS_VERSION) {
168 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
169 vfc->vfc_name, vfc->vfc_version);
172 if (vfs_byname(vfc->vfc_name) != NULL)
175 if (vfs_typenumhash != 0) {
177 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
178 * all of 1<->255 are assigned, it is limited to 8bits since
179 * that is what ZFS uses from vfc_typenum and is also the
180 * preferred range for vfs_getnewfsid().
182 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
186 /* Look for and fix any collision. */
187 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
188 if (hashval == tvfc->vfc_typenum) {
189 if (hashval == 255 && secondpass == 0) {
197 } while (tvfc != NULL);
198 vfc->vfc_typenum = hashval;
199 if (vfc->vfc_typenum >= maxvfsconf)
200 maxvfsconf = vfc->vfc_typenum + 1;
202 vfc->vfc_typenum = maxvfsconf++;
203 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
206 * If this filesystem has a sysctl node under vfs
207 * (i.e. vfs.xxfs), then change the oid number of that node to
208 * match the filesystem's type number. This allows user code
209 * which uses the type number to read sysctl variables defined
210 * by the filesystem to continue working. Since the oids are
211 * in a sorted list, we need to make sure the order is
212 * preserved by re-registering the oid after modifying its
216 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
217 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
218 sysctl_unregister_oid(oidp);
219 oidp->oid_number = vfc->vfc_typenum;
220 sysctl_register_oid(oidp);
226 * Initialise unused ``struct vfsops'' fields, to use
227 * the vfs_std*() functions. Note, we need the mount
228 * and unmount operations, at the least. The check
229 * for vfsops available is just a debugging aid.
231 KASSERT(vfc->vfc_vfsops != NULL,
232 ("Filesystem %s has no vfsops", vfc->vfc_name));
234 * Check the mount and unmount operations.
236 vfsops = vfc->vfc_vfsops;
237 KASSERT(vfsops->vfs_mount != NULL,
238 ("Filesystem %s has no mount op", vfc->vfc_name));
239 KASSERT(vfsops->vfs_unmount != NULL,
240 ("Filesystem %s has no unmount op", vfc->vfc_name));
242 if (vfsops->vfs_root == NULL)
243 /* return file system's root vnode */
244 vfsops->vfs_root = vfs_stdroot;
245 if (vfsops->vfs_quotactl == NULL)
247 vfsops->vfs_quotactl = vfs_stdquotactl;
248 if (vfsops->vfs_statfs == NULL)
249 /* return file system's status */
250 vfsops->vfs_statfs = vfs_stdstatfs;
251 if (vfsops->vfs_sync == NULL)
253 * flush unwritten data (nosync)
254 * file systems can use vfs_stdsync
255 * explicitly by setting it in the
258 vfsops->vfs_sync = vfs_stdnosync;
259 if (vfsops->vfs_vget == NULL)
260 /* convert an inode number to a vnode */
261 vfsops->vfs_vget = vfs_stdvget;
262 if (vfsops->vfs_fhtovp == NULL)
263 /* turn an NFS file handle into a vnode */
264 vfsops->vfs_fhtovp = vfs_stdfhtovp;
265 if (vfsops->vfs_checkexp == NULL)
266 /* check if file system is exported */
267 vfsops->vfs_checkexp = vfs_stdcheckexp;
268 if (vfsops->vfs_init == NULL)
269 /* file system specific initialisation */
270 vfsops->vfs_init = vfs_stdinit;
271 if (vfsops->vfs_uninit == NULL)
272 /* file system specific uninitialisation */
273 vfsops->vfs_uninit = vfs_stduninit;
274 if (vfsops->vfs_extattrctl == NULL)
275 /* extended attribute control */
276 vfsops->vfs_extattrctl = vfs_stdextattrctl;
277 if (vfsops->vfs_sysctl == NULL)
278 vfsops->vfs_sysctl = vfs_stdsysctl;
281 * Call init function for this VFS...
283 (*(vfc->vfc_vfsops->vfs_init))(vfc);
289 /* Remove registration of a filesystem type */
291 vfs_unregister(struct vfsconf *vfc)
293 struct vfsconf *vfsp;
294 int error, i, maxtypenum;
296 i = vfc->vfc_typenum;
298 vfsp = vfs_byname(vfc->vfc_name);
301 if (vfsp->vfc_refcount)
303 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
304 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
308 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
309 maxtypenum = VFS_GENERIC;
310 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
311 if (maxtypenum < vfsp->vfc_typenum)
312 maxtypenum = vfsp->vfc_typenum;
313 maxvfsconf = maxtypenum + 1;
318 * Standard kernel module handling code for filesystem modules.
319 * Referenced from VFS_SET().
322 vfs_modevent(module_t mod, int type, void *data)
327 vfc = (struct vfsconf *)data;
332 error = vfs_register(vfc);
337 error = vfs_unregister(vfc);