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 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fnv_hash.h>
44 #include <sys/kernel.h>
45 #include <sys/linker.h>
46 #include <sys/mount.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
54 static int vfs_register(struct vfsconf *);
55 static int vfs_unregister(struct vfsconf *);
57 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
60 * The highest defined VFS number.
62 int maxvfsconf = VFS_GENERIC + 1;
65 * Single-linked list of configured VFSes.
66 * New entries are added/deleted by vfs_register()/vfs_unregister()
68 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
70 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
73 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
74 * calculation on vfc_name, so that it doesn't change when file systems are
75 * loaded in a different order. This will avoid the NFS server file handles from
76 * changing for file systems that use vfc_typenum in their fsid.
78 static int vfs_typenumhash = 1;
79 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
80 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
81 "change when file systems are loaded in a different order.");
84 * A Zen vnode attribute structure.
86 * Initialized when the first filesystem registers by vfs_register().
93 * Allocate and fill in operations vectors.
95 * An undocumented feature of this approach to defining operations is that
96 * there can be multiple entries in vfs_opv_descs for the same operations
97 * vector. This allows third parties to extend the set of operations
98 * supported by another layer in a binary compatibile way. For example,
99 * assume that NFS needed to be modified to support Ficus. NFS has an entry
100 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
101 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
102 * listing those new operations Ficus adds to NFS, all without modifying the
103 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
104 * that is a(whole)nother story.) This is a feature.
108 * Routines having to do with the management of the vnode table.
111 static struct vfsconf *
112 vfs_byname_locked(const char *name)
114 struct vfsconf *vfsp;
116 sx_assert(&vfsconf_sx, SA_LOCKED);
117 if (!strcmp(name, "ffs"))
119 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
120 if (!strcmp(name, vfsp->vfc_name))
127 vfs_byname(const char *name)
129 struct vfsconf *vfsp;
132 vfsp = vfs_byname_locked(name);
138 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
140 struct vfsconf *vfsp;
143 vfsp = vfs_byname(fstype);
147 /* Try to load the respective module. */
148 *error = kern_kldload(td, fstype, &fileid);
149 loaded = (*error == 0);
150 if (*error == EEXIST)
155 /* Look up again to see if the VFS was loaded. */
156 vfsp = vfs_byname(fstype);
159 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
167 vfs_mount_sigdefer(struct mount *mp)
171 TSRAW(curthread, TS_ENTER, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
172 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
173 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_mount)(mp);
174 sigallowstop(prev_stops);
175 TSRAW(curthread, TS_EXIT, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
180 vfs_unmount_sigdefer(struct mount *mp, int mntflags)
184 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
185 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unmount)(mp, mntflags);
186 sigallowstop(prev_stops);
191 vfs_root_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
195 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
196 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_root)(mp, flags, vpp);
197 sigallowstop(prev_stops);
202 vfs_cachedroot_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
206 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
207 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_cachedroot)(mp, flags, vpp);
208 sigallowstop(prev_stops);
213 vfs_quotactl_sigdefer(struct mount *mp, int cmd, uid_t uid, void *arg)
217 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
218 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_quotactl)(mp, cmd, uid, arg);
219 sigallowstop(prev_stops);
224 vfs_statfs_sigdefer(struct mount *mp, struct statfs *sbp)
228 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
229 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_statfs)(mp, sbp);
230 sigallowstop(prev_stops);
235 vfs_sync_sigdefer(struct mount *mp, int waitfor)
239 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
240 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sync)(mp, waitfor);
241 sigallowstop(prev_stops);
246 vfs_vget_sigdefer(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
250 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
251 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_vget)(mp, ino, flags, vpp);
252 sigallowstop(prev_stops);
257 vfs_fhtovp_sigdefer(struct mount *mp, struct fid *fidp, int flags,
262 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
263 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_fhtovp)(mp, fidp, flags, vpp);
264 sigallowstop(prev_stops);
269 vfs_checkexp_sigdefer(struct mount *mp, struct sockaddr *nam, uint64_t *exflg,
270 struct ucred **credp, int *numsecflavors, int *secflavors)
274 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
275 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_checkexp)(mp, nam, exflg, credp,
276 numsecflavors, secflavors);
277 sigallowstop(prev_stops);
282 vfs_extattrctl_sigdefer(struct mount *mp, int cmd, struct vnode *filename_vp,
283 int attrnamespace, const char *attrname)
287 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
288 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_extattrctl)(mp, cmd,
289 filename_vp, attrnamespace, attrname);
290 sigallowstop(prev_stops);
295 vfs_sysctl_sigdefer(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
299 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
300 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sysctl)(mp, op, req);
301 sigallowstop(prev_stops);
306 vfs_susp_clean_sigdefer(struct mount *mp)
310 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean == NULL)
312 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
313 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean)(mp);
314 sigallowstop(prev_stops);
318 vfs_reclaim_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
322 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp == NULL)
324 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
325 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp)(mp, vp);
326 sigallowstop(prev_stops);
330 vfs_unlink_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
334 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp == NULL)
336 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
337 (*(mp)->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp)(mp, vp);
338 sigallowstop(prev_stops);
342 vfs_purge_sigdefer(struct mount *mp)
346 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
347 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_purge)(mp);
348 sigallowstop(prev_stops);
352 vfs_report_lockf_sigdefer(struct mount *mp, struct sbuf *sb)
356 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
357 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_report_lockf)(mp, sb);
358 sigallowstop(prev_stops);
362 static struct vfsops vfsops_sigdefer = {
363 .vfs_mount = vfs_mount_sigdefer,
364 .vfs_unmount = vfs_unmount_sigdefer,
365 .vfs_root = vfs_root_sigdefer,
366 .vfs_cachedroot = vfs_cachedroot_sigdefer,
367 .vfs_quotactl = vfs_quotactl_sigdefer,
368 .vfs_statfs = vfs_statfs_sigdefer,
369 .vfs_sync = vfs_sync_sigdefer,
370 .vfs_vget = vfs_vget_sigdefer,
371 .vfs_fhtovp = vfs_fhtovp_sigdefer,
372 .vfs_checkexp = vfs_checkexp_sigdefer,
373 .vfs_extattrctl = vfs_extattrctl_sigdefer,
374 .vfs_sysctl = vfs_sysctl_sigdefer,
375 .vfs_susp_clean = vfs_susp_clean_sigdefer,
376 .vfs_reclaim_lowervp = vfs_reclaim_lowervp_sigdefer,
377 .vfs_unlink_lowervp = vfs_unlink_lowervp_sigdefer,
378 .vfs_purge = vfs_purge_sigdefer,
379 .vfs_report_lockf = vfs_report_lockf_sigdefer,
382 /* Register a new filesystem type in the global table */
384 vfs_register(struct vfsconf *vfc)
386 struct sysctl_oid *oidp;
387 struct vfsops *vfsops;
389 struct vfsconf *tvfc;
394 vattr_null(&va_null);
398 if (vfc->vfc_version != VFS_VERSION) {
399 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
400 vfc->vfc_name, vfc->vfc_version);
404 if (vfs_byname_locked(vfc->vfc_name) != NULL) {
409 if (vfs_typenumhash != 0) {
411 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
412 * all of 1<->255 are assigned, it is limited to 8bits since
413 * that is what ZFS uses from vfc_typenum and is also the
414 * preferred range for vfs_getnewfsid().
416 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
420 /* Look for and fix any collision. */
421 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
422 if (hashval == tvfc->vfc_typenum) {
423 if (hashval == 255 && secondpass == 0) {
431 } while (tvfc != NULL);
432 vfc->vfc_typenum = hashval;
433 if (vfc->vfc_typenum >= maxvfsconf)
434 maxvfsconf = vfc->vfc_typenum + 1;
436 vfc->vfc_typenum = maxvfsconf++;
437 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
440 * Initialise unused ``struct vfsops'' fields, to use
441 * the vfs_std*() functions. Note, we need the mount
442 * and unmount operations, at the least. The check
443 * for vfsops available is just a debugging aid.
445 KASSERT(vfc->vfc_vfsops != NULL,
446 ("Filesystem %s has no vfsops", vfc->vfc_name));
448 * Check the mount and unmount operations.
450 vfsops = vfc->vfc_vfsops;
451 KASSERT(vfsops->vfs_mount != NULL,
452 ("Filesystem %s has no mount op", vfc->vfc_name));
453 KASSERT(vfsops->vfs_unmount != NULL,
454 ("Filesystem %s has no unmount op", vfc->vfc_name));
456 if (vfsops->vfs_root == NULL)
457 /* return file system's root vnode */
458 vfsops->vfs_root = vfs_stdroot;
459 if (vfsops->vfs_quotactl == NULL)
461 vfsops->vfs_quotactl = vfs_stdquotactl;
462 if (vfsops->vfs_statfs == NULL)
463 /* return file system's status */
464 vfsops->vfs_statfs = vfs_stdstatfs;
465 if (vfsops->vfs_sync == NULL)
467 * flush unwritten data (nosync)
468 * file systems can use vfs_stdsync
469 * explicitly by setting it in the
472 vfsops->vfs_sync = vfs_stdnosync;
473 if (vfsops->vfs_vget == NULL)
474 /* convert an inode number to a vnode */
475 vfsops->vfs_vget = vfs_stdvget;
476 if (vfsops->vfs_fhtovp == NULL)
477 /* turn an NFS file handle into a vnode */
478 vfsops->vfs_fhtovp = vfs_stdfhtovp;
479 if (vfsops->vfs_checkexp == NULL)
480 /* check if file system is exported */
481 vfsops->vfs_checkexp = vfs_stdcheckexp;
482 if (vfsops->vfs_init == NULL)
483 /* file system specific initialisation */
484 vfsops->vfs_init = vfs_stdinit;
485 if (vfsops->vfs_uninit == NULL)
486 /* file system specific uninitialisation */
487 vfsops->vfs_uninit = vfs_stduninit;
488 if (vfsops->vfs_extattrctl == NULL)
489 /* extended attribute control */
490 vfsops->vfs_extattrctl = vfs_stdextattrctl;
491 if (vfsops->vfs_sysctl == NULL)
492 vfsops->vfs_sysctl = vfs_stdsysctl;
493 if (vfsops->vfs_report_lockf == NULL)
494 vfsops->vfs_report_lockf = vfs_report_lockf;
496 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
497 vfc->vfc_vfsops_sd = vfc->vfc_vfsops;
498 vfc->vfc_vfsops = &vfsops_sigdefer;
501 if (vfc->vfc_flags & VFCF_JAIL)
505 * Call init function for this VFS...
507 if ((vfc->vfc_flags & VFCF_SBDRY) != 0)
508 vfc->vfc_vfsops_sd->vfs_init(vfc);
510 vfc->vfc_vfsops->vfs_init(vfc);
514 * If this filesystem has a sysctl node under vfs
515 * (i.e. vfs.xxfs), then change the oid number of that node to
516 * match the filesystem's type number. This allows user code
517 * which uses the type number to read sysctl variables defined
518 * by the filesystem to continue working. Since the oids are
519 * in a sorted list, we need to make sure the order is
520 * preserved by re-registering the oid after modifying its
524 SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) {
525 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
526 sysctl_unregister_oid(oidp);
527 oidp->oid_number = vfc->vfc_typenum;
528 sysctl_register_oid(oidp);
537 /* Remove registration of a filesystem type */
539 vfs_unregister(struct vfsconf *vfc)
541 struct vfsconf *vfsp;
542 int error, maxtypenum;
545 vfsp = vfs_byname_locked(vfc->vfc_name);
550 if (vfsp->vfc_refcount != 0) {
555 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
556 if (vfc->vfc_vfsops_sd->vfs_uninit != NULL)
557 error = vfc->vfc_vfsops_sd->vfs_uninit(vfsp);
559 if (vfc->vfc_vfsops->vfs_uninit != NULL)
560 error = vfc->vfc_vfsops->vfs_uninit(vfsp);
566 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
567 maxtypenum = VFS_GENERIC;
568 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
569 if (maxtypenum < vfsp->vfc_typenum)
570 maxtypenum = vfsp->vfc_typenum;
571 maxvfsconf = maxtypenum + 1;
577 * Standard kernel module handling code for filesystem modules.
578 * Referenced from VFS_SET().
581 vfs_modevent(module_t mod, int type, void *data)
586 vfc = (struct vfsconf *)data;
591 error = vfs_register(vfc);
596 error = vfs_unregister(vfc);