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 * 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. 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
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/fnv_hash.h>
39 #include <sys/kernel.h>
40 #include <sys/linker.h>
41 #include <sys/mount.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
49 static int vfs_register(struct vfsconf *);
50 static int vfs_unregister(struct vfsconf *);
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 vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
65 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
68 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
69 * calculation on vfc_name, so that it doesn't change when file systems are
70 * loaded in a different order. This will avoid the NFS server file handles from
71 * changing for file systems that use vfc_typenum in their fsid.
73 static int vfs_typenumhash = 1;
74 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
75 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
76 " change when file systems are loaded in a different order.");
79 * A Zen vnode attribute structure.
81 * Initialized when the first filesystem registers by vfs_register().
88 * Allocate and fill in operations vectors.
90 * An undocumented feature of this approach to defining operations is that
91 * there can be multiple entries in vfs_opv_descs for the same operations
92 * vector. This allows third parties to extend the set of operations
93 * supported by another layer in a binary compatibile way. For example,
94 * assume that NFS needed to be modified to support Ficus. NFS has an entry
95 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
96 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
97 * listing those new operations Ficus adds to NFS, all without modifying the
98 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
99 * that is a(whole)nother story.) This is a feature.
103 * Routines having to do with the management of the vnode table.
106 static struct vfsconf *
107 vfs_byname_locked(const char *name)
109 struct vfsconf *vfsp;
111 sx_assert(&vfsconf_sx, SA_LOCKED);
112 if (!strcmp(name, "ffs"))
114 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
115 if (!strcmp(name, vfsp->vfc_name))
122 vfs_byname(const char *name)
124 struct vfsconf *vfsp;
127 vfsp = vfs_byname_locked(name);
133 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
135 struct vfsconf *vfsp;
138 vfsp = vfs_byname(fstype);
142 /* Try to load the respective module. */
143 *error = kern_kldload(td, fstype, &fileid);
144 loaded = (*error == 0);
145 if (*error == EEXIST)
152 /* Look up again to see if the VFS was loaded. */
153 vfsp = vfs_byname(fstype);
156 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
164 vfs_mount_sigdefer(struct mount *mp)
168 TSRAW(curthread, TS_ENTER, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
169 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
170 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_mount)(mp);
171 sigallowstop(prev_stops);
172 TSRAW(curthread, TS_EXIT, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
177 vfs_unmount_sigdefer(struct mount *mp, int mntflags)
181 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
182 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unmount)(mp, mntflags);
183 sigallowstop(prev_stops);
188 vfs_root_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
192 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
193 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_root)(mp, flags, vpp);
194 sigallowstop(prev_stops);
199 vfs_cachedroot_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
203 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
204 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_cachedroot)(mp, flags, vpp);
205 sigallowstop(prev_stops);
210 vfs_quotactl_sigdefer(struct mount *mp, int cmd, uid_t uid, void *arg,
215 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
216 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_quotactl)(mp, cmd, uid, arg,
218 sigallowstop(prev_stops);
223 vfs_statfs_sigdefer(struct mount *mp, struct statfs *sbp)
227 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
228 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_statfs)(mp, sbp);
229 sigallowstop(prev_stops);
234 vfs_sync_sigdefer(struct mount *mp, int waitfor)
238 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
239 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sync)(mp, waitfor);
240 sigallowstop(prev_stops);
245 vfs_vget_sigdefer(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
249 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
250 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_vget)(mp, ino, flags, vpp);
251 sigallowstop(prev_stops);
256 vfs_fhtovp_sigdefer(struct mount *mp, struct fid *fidp, int flags,
261 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
262 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_fhtovp)(mp, fidp, flags, vpp);
263 sigallowstop(prev_stops);
268 vfs_checkexp_sigdefer(struct mount *mp, struct sockaddr *nam, uint64_t *exflg,
269 struct ucred **credp, int *numsecflavors, int *secflavors)
273 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
274 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_checkexp)(mp, nam, exflg, credp,
275 numsecflavors, secflavors);
276 sigallowstop(prev_stops);
281 vfs_extattrctl_sigdefer(struct mount *mp, int cmd, struct vnode *filename_vp,
282 int attrnamespace, const char *attrname)
286 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
287 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_extattrctl)(mp, cmd,
288 filename_vp, attrnamespace, attrname);
289 sigallowstop(prev_stops);
294 vfs_sysctl_sigdefer(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
298 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
299 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sysctl)(mp, op, req);
300 sigallowstop(prev_stops);
305 vfs_susp_clean_sigdefer(struct mount *mp)
309 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean == NULL)
311 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
312 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean)(mp);
313 sigallowstop(prev_stops);
317 vfs_reclaim_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
321 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp == NULL)
323 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
324 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp)(mp, vp);
325 sigallowstop(prev_stops);
329 vfs_unlink_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
333 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp == NULL)
335 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
336 (*(mp)->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp)(mp, vp);
337 sigallowstop(prev_stops);
341 vfs_purge_sigdefer(struct mount *mp)
345 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
346 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_purge)(mp);
347 sigallowstop(prev_stops);
351 vfs_report_lockf_sigdefer(struct mount *mp, struct sbuf *sb)
355 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
356 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_report_lockf)(mp, sb);
357 sigallowstop(prev_stops);
361 static struct vfsops vfsops_sigdefer = {
362 .vfs_mount = vfs_mount_sigdefer,
363 .vfs_unmount = vfs_unmount_sigdefer,
364 .vfs_root = vfs_root_sigdefer,
365 .vfs_cachedroot = vfs_cachedroot_sigdefer,
366 .vfs_quotactl = vfs_quotactl_sigdefer,
367 .vfs_statfs = vfs_statfs_sigdefer,
368 .vfs_sync = vfs_sync_sigdefer,
369 .vfs_vget = vfs_vget_sigdefer,
370 .vfs_fhtovp = vfs_fhtovp_sigdefer,
371 .vfs_checkexp = vfs_checkexp_sigdefer,
372 .vfs_extattrctl = vfs_extattrctl_sigdefer,
373 .vfs_sysctl = vfs_sysctl_sigdefer,
374 .vfs_susp_clean = vfs_susp_clean_sigdefer,
375 .vfs_reclaim_lowervp = vfs_reclaim_lowervp_sigdefer,
376 .vfs_unlink_lowervp = vfs_unlink_lowervp_sigdefer,
377 .vfs_purge = vfs_purge_sigdefer,
378 .vfs_report_lockf = vfs_report_lockf_sigdefer,
381 /* Register a new filesystem type in the global table */
383 vfs_register(struct vfsconf *vfc)
385 struct sysctl_oid *oidp;
386 struct vfsops *vfsops;
388 struct vfsconf *tvfc;
393 vattr_null(&va_null);
397 if (vfc->vfc_version != VFS_VERSION) {
398 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
399 vfc->vfc_name, vfc->vfc_version);
403 if (vfs_byname_locked(vfc->vfc_name) != NULL) {
408 if (vfs_typenumhash != 0) {
410 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
411 * all of 1<->255 are assigned, it is limited to 8bits since
412 * that is what ZFS uses from vfc_typenum and is also the
413 * preferred range for vfs_getnewfsid().
415 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
419 /* Look for and fix any collision. */
420 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
421 if (hashval == tvfc->vfc_typenum) {
422 if (hashval == 255 && secondpass == 0) {
430 } while (tvfc != NULL);
431 vfc->vfc_typenum = hashval;
432 if (vfc->vfc_typenum >= maxvfsconf)
433 maxvfsconf = vfc->vfc_typenum + 1;
435 vfc->vfc_typenum = maxvfsconf++;
436 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
439 * Initialise unused ``struct vfsops'' fields, to use
440 * the vfs_std*() functions. Note, we need the mount
441 * and unmount operations, at the least. The check
442 * for vfsops available is just a debugging aid.
444 KASSERT(vfc->vfc_vfsops != NULL,
445 ("Filesystem %s has no vfsops", vfc->vfc_name));
447 * Check the mount and unmount operations.
449 vfsops = vfc->vfc_vfsops;
450 KASSERT(vfsops->vfs_mount != NULL,
451 ("Filesystem %s has no mount op", vfc->vfc_name));
452 KASSERT(vfsops->vfs_unmount != NULL,
453 ("Filesystem %s has no unmount op", vfc->vfc_name));
455 if (vfsops->vfs_root == NULL)
456 /* return file system's root vnode */
457 vfsops->vfs_root = vfs_stdroot;
458 if (vfsops->vfs_quotactl == NULL)
460 vfsops->vfs_quotactl = vfs_stdquotactl;
461 if (vfsops->vfs_statfs == NULL)
462 /* return file system's status */
463 vfsops->vfs_statfs = vfs_stdstatfs;
464 if (vfsops->vfs_sync == NULL)
466 * flush unwritten data (nosync)
467 * file systems can use vfs_stdsync
468 * explicitly by setting it in the
471 vfsops->vfs_sync = vfs_stdnosync;
472 if (vfsops->vfs_vget == NULL)
473 /* convert an inode number to a vnode */
474 vfsops->vfs_vget = vfs_stdvget;
475 if (vfsops->vfs_fhtovp == NULL)
476 /* turn an NFS file handle into a vnode */
477 vfsops->vfs_fhtovp = vfs_stdfhtovp;
478 if (vfsops->vfs_checkexp == NULL)
479 /* check if file system is exported */
480 vfsops->vfs_checkexp = vfs_stdcheckexp;
481 if (vfsops->vfs_init == NULL)
482 /* file system specific initialisation */
483 vfsops->vfs_init = vfs_stdinit;
484 if (vfsops->vfs_uninit == NULL)
485 /* file system specific uninitialisation */
486 vfsops->vfs_uninit = vfs_stduninit;
487 if (vfsops->vfs_extattrctl == NULL)
488 /* extended attribute control */
489 vfsops->vfs_extattrctl = vfs_stdextattrctl;
490 if (vfsops->vfs_sysctl == NULL)
491 vfsops->vfs_sysctl = vfs_stdsysctl;
492 if (vfsops->vfs_report_lockf == NULL)
493 vfsops->vfs_report_lockf = vfs_report_lockf;
495 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
496 vfc->vfc_vfsops_sd = vfc->vfc_vfsops;
497 vfc->vfc_vfsops = &vfsops_sigdefer;
500 if (vfc->vfc_flags & VFCF_JAIL)
504 * Call init function for this VFS...
506 if ((vfc->vfc_flags & VFCF_SBDRY) != 0)
507 vfc->vfc_vfsops_sd->vfs_init(vfc);
509 vfc->vfc_vfsops->vfs_init(vfc);
513 * If this filesystem has a sysctl node under vfs
514 * (i.e. vfs.xxfs), then change the oid number of that node to
515 * match the filesystem's type number. This allows user code
516 * which uses the type number to read sysctl variables defined
517 * by the filesystem to continue working. Since the oids are
518 * in a sorted list, we need to make sure the order is
519 * preserved by re-registering the oid after modifying its
523 RB_FOREACH(oidp, sysctl_oid_list, SYSCTL_CHILDREN(&sysctl___vfs)) {
524 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
525 sysctl_unregister_oid(oidp);
526 oidp->oid_number = vfc->vfc_typenum;
527 sysctl_register_oid(oidp);
536 /* Remove registration of a filesystem type */
538 vfs_unregister(struct vfsconf *vfc)
540 struct vfsconf *vfsp;
541 int error, maxtypenum;
544 vfsp = vfs_byname_locked(vfc->vfc_name);
549 if (vfsp->vfc_refcount != 0) {
554 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
555 if (vfc->vfc_vfsops_sd->vfs_uninit != NULL)
556 error = vfc->vfc_vfsops_sd->vfs_uninit(vfsp);
558 if (vfc->vfc_vfsops->vfs_uninit != NULL)
559 error = vfc->vfc_vfsops->vfs_uninit(vfsp);
565 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
566 maxtypenum = VFS_GENERIC;
567 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
568 if (maxtypenum < vfsp->vfc_typenum)
569 maxtypenum = vfsp->vfc_typenum;
570 maxvfsconf = maxtypenum + 1;
576 * Standard kernel module handling code for filesystem modules.
577 * Referenced from VFS_SET().
580 vfs_modevent(module_t mod, int type, void *data)
585 vfc = (struct vfsconf *)data;
590 error = vfs_register(vfc);
595 error = vfs_unregister(vfc);