2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed
8 * to Berkeley by John Heidemann of the UCLA Ficus project.
10 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fnv_hash.h>
46 #include <sys/kernel.h>
47 #include <sys/linker.h>
48 #include <sys/mount.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
56 static int vfs_register(struct vfsconf *);
57 static int vfs_unregister(struct vfsconf *);
59 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
62 * The highest defined VFS number.
64 int maxvfsconf = VFS_GENERIC + 1;
67 * Single-linked list of configured VFSes.
68 * New entries are added/deleted by vfs_register()/vfs_unregister()
70 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
72 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
75 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
76 * calculation on vfc_name, so that it doesn't change when file systems are
77 * loaded in a different order. This will avoid the NFS server file handles from
78 * changing for file systems that use vfc_typenum in their fsid.
80 static int vfs_typenumhash = 1;
81 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
82 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
83 "change when file systems are loaded in a different order.");
86 * A Zen vnode attribute structure.
88 * Initialized when the first filesystem registers by vfs_register().
95 * Allocate and fill in operations vectors.
97 * An undocumented feature of this approach to defining operations is that
98 * there can be multiple entries in vfs_opv_descs for the same operations
99 * vector. This allows third parties to extend the set of operations
100 * supported by another layer in a binary compatibile way. For example,
101 * assume that NFS needed to be modified to support Ficus. NFS has an entry
102 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
103 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
104 * listing those new operations Ficus adds to NFS, all without modifying the
105 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
106 * that is a(whole)nother story.) This is a feature.
110 * Routines having to do with the management of the vnode table.
113 static struct vfsconf *
114 vfs_byname_locked(const char *name)
116 struct vfsconf *vfsp;
118 sx_assert(&vfsconf_sx, SA_LOCKED);
119 if (!strcmp(name, "ffs"))
121 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
122 if (!strcmp(name, vfsp->vfc_name))
129 vfs_byname(const char *name)
131 struct vfsconf *vfsp;
134 vfsp = vfs_byname_locked(name);
140 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
142 struct vfsconf *vfsp;
145 vfsp = vfs_byname(fstype);
149 /* Try to load the respective module. */
150 *error = kern_kldload(td, fstype, &fileid);
151 loaded = (*error == 0);
152 if (*error == EEXIST)
157 /* Look up again to see if the VFS was loaded. */
158 vfsp = vfs_byname(fstype);
161 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
169 vfs_mount_sigdefer(struct mount *mp)
173 TSRAW(curthread, TS_ENTER, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
174 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
175 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_mount)(mp);
176 sigallowstop(prev_stops);
177 TSRAW(curthread, TS_EXIT, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
182 vfs_unmount_sigdefer(struct mount *mp, int mntflags)
186 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
187 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unmount)(mp, mntflags);
188 sigallowstop(prev_stops);
193 vfs_root_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
197 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
198 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_root)(mp, flags, vpp);
199 sigallowstop(prev_stops);
204 vfs_cachedroot_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
208 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
209 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_cachedroot)(mp, flags, vpp);
210 sigallowstop(prev_stops);
215 vfs_quotactl_sigdefer(struct mount *mp, int cmd, uid_t uid, void *arg)
219 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
220 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_quotactl)(mp, cmd, uid, arg);
221 sigallowstop(prev_stops);
226 vfs_statfs_sigdefer(struct mount *mp, struct statfs *sbp)
230 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
231 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_statfs)(mp, sbp);
232 sigallowstop(prev_stops);
237 vfs_sync_sigdefer(struct mount *mp, int waitfor)
241 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
242 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sync)(mp, waitfor);
243 sigallowstop(prev_stops);
248 vfs_vget_sigdefer(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
252 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
253 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_vget)(mp, ino, flags, vpp);
254 sigallowstop(prev_stops);
259 vfs_fhtovp_sigdefer(struct mount *mp, struct fid *fidp, int flags,
264 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
265 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_fhtovp)(mp, fidp, flags, vpp);
266 sigallowstop(prev_stops);
271 vfs_checkexp_sigdefer(struct mount *mp, struct sockaddr *nam, uint64_t *exflg,
272 struct ucred **credp, int *numsecflavors, int *secflavors)
276 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
277 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_checkexp)(mp, nam, exflg, credp,
278 numsecflavors, secflavors);
279 sigallowstop(prev_stops);
284 vfs_extattrctl_sigdefer(struct mount *mp, int cmd, struct vnode *filename_vp,
285 int attrnamespace, const char *attrname)
289 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
290 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_extattrctl)(mp, cmd,
291 filename_vp, attrnamespace, attrname);
292 sigallowstop(prev_stops);
297 vfs_sysctl_sigdefer(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
301 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
302 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sysctl)(mp, op, req);
303 sigallowstop(prev_stops);
308 vfs_susp_clean_sigdefer(struct mount *mp)
312 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean == NULL)
314 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
315 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean)(mp);
316 sigallowstop(prev_stops);
320 vfs_reclaim_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
324 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp == NULL)
326 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
327 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp)(mp, vp);
328 sigallowstop(prev_stops);
332 vfs_unlink_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
336 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp == NULL)
338 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
339 (*(mp)->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp)(mp, vp);
340 sigallowstop(prev_stops);
344 vfs_purge_sigdefer(struct mount *mp)
348 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
349 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_purge)(mp);
350 sigallowstop(prev_stops);
353 static struct vfsops vfsops_sigdefer = {
354 .vfs_mount = vfs_mount_sigdefer,
355 .vfs_unmount = vfs_unmount_sigdefer,
356 .vfs_root = vfs_root_sigdefer,
357 .vfs_cachedroot = vfs_cachedroot_sigdefer,
358 .vfs_quotactl = vfs_quotactl_sigdefer,
359 .vfs_statfs = vfs_statfs_sigdefer,
360 .vfs_sync = vfs_sync_sigdefer,
361 .vfs_vget = vfs_vget_sigdefer,
362 .vfs_fhtovp = vfs_fhtovp_sigdefer,
363 .vfs_checkexp = vfs_checkexp_sigdefer,
364 .vfs_extattrctl = vfs_extattrctl_sigdefer,
365 .vfs_sysctl = vfs_sysctl_sigdefer,
366 .vfs_susp_clean = vfs_susp_clean_sigdefer,
367 .vfs_reclaim_lowervp = vfs_reclaim_lowervp_sigdefer,
368 .vfs_unlink_lowervp = vfs_unlink_lowervp_sigdefer,
369 .vfs_purge = vfs_purge_sigdefer,
373 /* Register a new filesystem type in the global table */
375 vfs_register(struct vfsconf *vfc)
377 struct sysctl_oid *oidp;
378 struct vfsops *vfsops;
380 struct vfsconf *tvfc;
385 vattr_null(&va_null);
389 if (vfc->vfc_version != VFS_VERSION) {
390 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
391 vfc->vfc_name, vfc->vfc_version);
395 if (vfs_byname_locked(vfc->vfc_name) != NULL) {
400 if (vfs_typenumhash != 0) {
402 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
403 * all of 1<->255 are assigned, it is limited to 8bits since
404 * that is what ZFS uses from vfc_typenum and is also the
405 * preferred range for vfs_getnewfsid().
407 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
411 /* Look for and fix any collision. */
412 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
413 if (hashval == tvfc->vfc_typenum) {
414 if (hashval == 255 && secondpass == 0) {
422 } while (tvfc != NULL);
423 vfc->vfc_typenum = hashval;
424 if (vfc->vfc_typenum >= maxvfsconf)
425 maxvfsconf = vfc->vfc_typenum + 1;
427 vfc->vfc_typenum = maxvfsconf++;
428 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
431 * Initialise unused ``struct vfsops'' fields, to use
432 * the vfs_std*() functions. Note, we need the mount
433 * and unmount operations, at the least. The check
434 * for vfsops available is just a debugging aid.
436 KASSERT(vfc->vfc_vfsops != NULL,
437 ("Filesystem %s has no vfsops", vfc->vfc_name));
439 * Check the mount and unmount operations.
441 vfsops = vfc->vfc_vfsops;
442 KASSERT(vfsops->vfs_mount != NULL,
443 ("Filesystem %s has no mount op", vfc->vfc_name));
444 KASSERT(vfsops->vfs_unmount != NULL,
445 ("Filesystem %s has no unmount op", vfc->vfc_name));
447 if (vfsops->vfs_root == NULL)
448 /* return file system's root vnode */
449 vfsops->vfs_root = vfs_stdroot;
450 if (vfsops->vfs_quotactl == NULL)
452 vfsops->vfs_quotactl = vfs_stdquotactl;
453 if (vfsops->vfs_statfs == NULL)
454 /* return file system's status */
455 vfsops->vfs_statfs = vfs_stdstatfs;
456 if (vfsops->vfs_sync == NULL)
458 * flush unwritten data (nosync)
459 * file systems can use vfs_stdsync
460 * explicitly by setting it in the
463 vfsops->vfs_sync = vfs_stdnosync;
464 if (vfsops->vfs_vget == NULL)
465 /* convert an inode number to a vnode */
466 vfsops->vfs_vget = vfs_stdvget;
467 if (vfsops->vfs_fhtovp == NULL)
468 /* turn an NFS file handle into a vnode */
469 vfsops->vfs_fhtovp = vfs_stdfhtovp;
470 if (vfsops->vfs_checkexp == NULL)
471 /* check if file system is exported */
472 vfsops->vfs_checkexp = vfs_stdcheckexp;
473 if (vfsops->vfs_init == NULL)
474 /* file system specific initialisation */
475 vfsops->vfs_init = vfs_stdinit;
476 if (vfsops->vfs_uninit == NULL)
477 /* file system specific uninitialisation */
478 vfsops->vfs_uninit = vfs_stduninit;
479 if (vfsops->vfs_extattrctl == NULL)
480 /* extended attribute control */
481 vfsops->vfs_extattrctl = vfs_stdextattrctl;
482 if (vfsops->vfs_sysctl == NULL)
483 vfsops->vfs_sysctl = vfs_stdsysctl;
485 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
486 vfc->vfc_vfsops_sd = vfc->vfc_vfsops;
487 vfc->vfc_vfsops = &vfsops_sigdefer;
490 if (vfc->vfc_flags & VFCF_JAIL)
494 * Call init function for this VFS...
496 if ((vfc->vfc_flags & VFCF_SBDRY) != 0)
497 vfc->vfc_vfsops_sd->vfs_init(vfc);
499 vfc->vfc_vfsops->vfs_init(vfc);
503 * If this filesystem has a sysctl node under vfs
504 * (i.e. vfs.xxfs), then change the oid number of that node to
505 * match the filesystem's type number. This allows user code
506 * which uses the type number to read sysctl variables defined
507 * by the filesystem to continue working. Since the oids are
508 * in a sorted list, we need to make sure the order is
509 * preserved by re-registering the oid after modifying its
513 SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) {
514 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
515 sysctl_unregister_oid(oidp);
516 oidp->oid_number = vfc->vfc_typenum;
517 sysctl_register_oid(oidp);
526 /* Remove registration of a filesystem type */
528 vfs_unregister(struct vfsconf *vfc)
530 struct vfsconf *vfsp;
531 int error, maxtypenum;
534 vfsp = vfs_byname_locked(vfc->vfc_name);
539 if (vfsp->vfc_refcount != 0) {
544 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
545 if (vfc->vfc_vfsops_sd->vfs_uninit != NULL)
546 error = vfc->vfc_vfsops_sd->vfs_uninit(vfsp);
548 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
549 error = vfc->vfc_vfsops->vfs_uninit(vfsp);
556 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
557 maxtypenum = VFS_GENERIC;
558 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
559 if (maxtypenum < vfsp->vfc_typenum)
560 maxtypenum = vfsp->vfc_typenum;
561 maxvfsconf = maxtypenum + 1;
567 * Standard kernel module handling code for filesystem modules.
568 * Referenced from VFS_SET().
571 vfs_modevent(module_t mod, int type, void *data)
576 vfc = (struct vfsconf *)data;
581 error = vfs_register(vfc);
586 error = vfs_unregister(vfc);