xref: /freebsd/sys/kern/vfs_init.c (revision 0e5c6bd43646821dc676a6db03b7175f2668ec88)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed
8df8bae1dSRodney W. Grimes  * to Berkeley by John Heidemann of the UCLA Ficus project.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39677b542eSDavid E. O'Brien #include <sys/cdefs.h>
40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43f23b4c91SGarrett Wollman #include <sys/systm.h>
444d30adc4SRick Macklem #include <sys/fnv_hash.h>
45*0e5c6bd4SJamie Gritton #include <sys/jail.h>
46c901836cSGarrett Wollman #include <sys/kernel.h>
4732ba8e93SPoul-Henning Kamp #include <sys/linker.h>
48df8bae1dSRodney W. Grimes #include <sys/mount.h>
4932ba8e93SPoul-Henning Kamp #include <sys/proc.h>
50168f4ee0SKonstantin Belousov #include <sys/sx.h>
51edd32c2dSJohn Baldwin #include <sys/syscallsubr.h>
52e99ea9ecSBruce Evans #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes #include <sys/vnode.h>
54df8bae1dSRodney W. Grimes #include <sys/malloc.h>
55df8bae1dSRodney W. Grimes 
56ebbfc2f8SPoul-Henning Kamp static int	vfs_register(struct vfsconf *);
57ebbfc2f8SPoul-Henning Kamp static int	vfs_unregister(struct vfsconf *);
582b14f991SJulian Elischer 
59a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
60a1c995b6SPoul-Henning Kamp 
612b14f991SJulian Elischer /*
62138e514cSPeter Wemm  * The highest defined VFS number.
6399448ed1SJohn Dyson  */
64138e514cSPeter Wemm int maxvfsconf = VFS_GENERIC + 1;
65eb8e6d52SEivind Eklund 
66eb8e6d52SEivind Eklund /*
67eb8e6d52SEivind Eklund  * Single-linked list of configured VFSes.
68eb8e6d52SEivind Eklund  * New entries are added/deleted by vfs_register()/vfs_unregister()
69eb8e6d52SEivind Eklund  */
703dfe213eSPoul-Henning Kamp struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
71168f4ee0SKonstantin Belousov struct sx vfsconf_sx;
72168f4ee0SKonstantin Belousov SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
73b676e48cSMike Smith 
74b676e48cSMike Smith /*
754d30adc4SRick Macklem  * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
764d30adc4SRick Macklem  * calculation on vfc_name, so that it doesn't change when file systems are
774d30adc4SRick Macklem  * loaded in a different order. This will avoid the NFS server file handles from
784d30adc4SRick Macklem  * changing for file systems that use vfc_typenum in their fsid.
794d30adc4SRick Macklem  */
804d30adc4SRick Macklem static int	vfs_typenumhash = 1;
814d30adc4SRick Macklem SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
824d30adc4SRick Macklem     "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
834d30adc4SRick Macklem     "change when file systems are loaded in a different order.");
844d30adc4SRick Macklem 
854d30adc4SRick Macklem /*
86273350adSPoul-Henning Kamp  * A Zen vnode attribute structure.
87273350adSPoul-Henning Kamp  *
88273350adSPoul-Henning Kamp  * Initialized when the first filesystem registers by vfs_register().
89273350adSPoul-Henning Kamp  */
90273350adSPoul-Henning Kamp struct vattr va_null;
91273350adSPoul-Henning Kamp 
92273350adSPoul-Henning Kamp /*
93df8bae1dSRodney W. Grimes  * vfs_init.c
94df8bae1dSRodney W. Grimes  *
95df8bae1dSRodney W. Grimes  * Allocate and fill in operations vectors.
96df8bae1dSRodney W. Grimes  *
97df8bae1dSRodney W. Grimes  * An undocumented feature of this approach to defining operations is that
98df8bae1dSRodney W. Grimes  * there can be multiple entries in vfs_opv_descs for the same operations
99df8bae1dSRodney W. Grimes  * vector. This allows third parties to extend the set of operations
100df8bae1dSRodney W. Grimes  * supported by another layer in a binary compatibile way. For example,
101df8bae1dSRodney W. Grimes  * assume that NFS needed to be modified to support Ficus. NFS has an entry
102df8bae1dSRodney W. Grimes  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
103df8bae1dSRodney W. Grimes  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
104df8bae1dSRodney W. Grimes  * listing those new operations Ficus adds to NFS, all without modifying the
105df8bae1dSRodney W. Grimes  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
106df8bae1dSRodney W. Grimes  * that is a(whole)nother story.) This is a feature.
107df8bae1dSRodney W. Grimes  */
1084e61198eSPeter Wemm 
109df8bae1dSRodney W. Grimes /*
110df8bae1dSRodney W. Grimes  * Routines having to do with the management of the vnode table.
111df8bae1dSRodney W. Grimes  */
112df8bae1dSRodney W. Grimes 
113168f4ee0SKonstantin Belousov static struct vfsconf *
114168f4ee0SKonstantin Belousov vfs_byname_locked(const char *name)
115168f4ee0SKonstantin Belousov {
116168f4ee0SKonstantin Belousov 	struct vfsconf *vfsp;
117168f4ee0SKonstantin Belousov 
118168f4ee0SKonstantin Belousov 	sx_assert(&vfsconf_sx, SA_LOCKED);
119168f4ee0SKonstantin Belousov 	if (!strcmp(name, "ffs"))
120168f4ee0SKonstantin Belousov 		name = "ufs";
121168f4ee0SKonstantin Belousov 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
122168f4ee0SKonstantin Belousov 		if (!strcmp(name, vfsp->vfc_name))
123168f4ee0SKonstantin Belousov 			return (vfsp);
124168f4ee0SKonstantin Belousov 	}
125168f4ee0SKonstantin Belousov 	return (NULL);
126168f4ee0SKonstantin Belousov }
127168f4ee0SKonstantin Belousov 
1283dfe213eSPoul-Henning Kamp struct vfsconf *
1293dfe213eSPoul-Henning Kamp vfs_byname(const char *name)
1303dfe213eSPoul-Henning Kamp {
1313dfe213eSPoul-Henning Kamp 	struct vfsconf *vfsp;
1323dfe213eSPoul-Henning Kamp 
133168f4ee0SKonstantin Belousov 	vfsconf_slock();
134168f4ee0SKonstantin Belousov 	vfsp = vfs_byname_locked(name);
135168f4ee0SKonstantin Belousov 	vfsconf_sunlock();
1363dfe213eSPoul-Henning Kamp 	return (vfsp);
1373dfe213eSPoul-Henning Kamp }
1383dfe213eSPoul-Henning Kamp 
13932ba8e93SPoul-Henning Kamp struct vfsconf *
14032ba8e93SPoul-Henning Kamp vfs_byname_kld(const char *fstype, struct thread *td, int *error)
14132ba8e93SPoul-Henning Kamp {
14232ba8e93SPoul-Henning Kamp 	struct vfsconf *vfsp;
143ffc72591SJamie Gritton 	int fileid, loaded;
14432ba8e93SPoul-Henning Kamp 
14532ba8e93SPoul-Henning Kamp 	vfsp = vfs_byname(fstype);
14632ba8e93SPoul-Henning Kamp 	if (vfsp != NULL)
14732ba8e93SPoul-Henning Kamp 		return (vfsp);
14832ba8e93SPoul-Henning Kamp 
149322fb40cSJohn Baldwin 	/* Try to load the respective module. */
150edd32c2dSJohn Baldwin 	*error = kern_kldload(td, fstype, &fileid);
151ffc72591SJamie Gritton 	loaded = (*error == 0);
152ffc72591SJamie Gritton 	if (*error == EEXIST)
153ffc72591SJamie Gritton 		*error = 0;
15432ba8e93SPoul-Henning Kamp 	if (*error)
15532ba8e93SPoul-Henning Kamp 		return (NULL);
156edd32c2dSJohn Baldwin 
15732ba8e93SPoul-Henning Kamp 	/* Look up again to see if the VFS was loaded. */
15832ba8e93SPoul-Henning Kamp 	vfsp = vfs_byname(fstype);
15932ba8e93SPoul-Henning Kamp 	if (vfsp == NULL) {
160ffc72591SJamie Gritton 		if (loaded)
161edd32c2dSJohn Baldwin 			(void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
16232ba8e93SPoul-Henning Kamp 		*error = ENODEV;
16332ba8e93SPoul-Henning Kamp 		return (NULL);
16432ba8e93SPoul-Henning Kamp 	}
16532ba8e93SPoul-Henning Kamp 	return (vfsp);
16632ba8e93SPoul-Henning Kamp }
16732ba8e93SPoul-Henning Kamp 
16832ba8e93SPoul-Henning Kamp 
169eb8e6d52SEivind Eklund /* Register a new filesystem type in the global table */
170ebbfc2f8SPoul-Henning Kamp static int
1714e61198eSPeter Wemm vfs_register(struct vfsconf *vfc)
172aa855a59SPeter Wemm {
173a199ed3cSDoug Rabson 	struct sysctl_oid *oidp;
1747652131bSPoul-Henning Kamp 	struct vfsops *vfsops;
175273350adSPoul-Henning Kamp 	static int once;
1764d30adc4SRick Macklem 	struct vfsconf *tvfc;
1774d30adc4SRick Macklem 	uint32_t hashval;
1784d30adc4SRick Macklem 	int secondpass;
179273350adSPoul-Henning Kamp 
180273350adSPoul-Henning Kamp 	if (!once) {
181273350adSPoul-Henning Kamp 		vattr_null(&va_null);
182273350adSPoul-Henning Kamp 		once = 1;
183273350adSPoul-Henning Kamp 	}
1847652131bSPoul-Henning Kamp 
1855e8c582aSPoul-Henning Kamp 	if (vfc->vfc_version != VFS_VERSION) {
1865e8c582aSPoul-Henning Kamp 		printf("ERROR: filesystem %s, unsupported ABI version %x\n",
1875e8c582aSPoul-Henning Kamp 		    vfc->vfc_name, vfc->vfc_version);
1885e8c582aSPoul-Henning Kamp 		return (EINVAL);
1895e8c582aSPoul-Henning Kamp 	}
190168f4ee0SKonstantin Belousov 	vfsconf_lock();
191168f4ee0SKonstantin Belousov 	if (vfs_byname_locked(vfc->vfc_name) != NULL) {
192168f4ee0SKonstantin Belousov 		vfsconf_unlock();
1935050aa86SKonstantin Belousov 		return (EEXIST);
194168f4ee0SKonstantin Belousov 	}
195aa855a59SPeter Wemm 
1964d30adc4SRick Macklem 	if (vfs_typenumhash != 0) {
1974d30adc4SRick Macklem 		/*
1984d30adc4SRick Macklem 		 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
1994d30adc4SRick Macklem 		 * all of 1<->255 are assigned, it is limited to 8bits since
2004d30adc4SRick Macklem 		 * that is what ZFS uses from vfc_typenum and is also the
2014d30adc4SRick Macklem 		 * preferred range for vfs_getnewfsid().
2024d30adc4SRick Macklem 		 */
2034d30adc4SRick Macklem 		hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
2044d30adc4SRick Macklem 		hashval &= 0xff;
2054d30adc4SRick Macklem 		secondpass = 0;
2064d30adc4SRick Macklem 		do {
2074d30adc4SRick Macklem 			/* Look for and fix any collision. */
2084d30adc4SRick Macklem 			TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
2094d30adc4SRick Macklem 				if (hashval == tvfc->vfc_typenum) {
2104d30adc4SRick Macklem 					if (hashval == 255 && secondpass == 0) {
2114d30adc4SRick Macklem 						hashval = 1;
2124d30adc4SRick Macklem 						secondpass = 1;
2134d30adc4SRick Macklem 					} else
2144d30adc4SRick Macklem 						hashval++;
2154d30adc4SRick Macklem 					break;
2164d30adc4SRick Macklem 				}
2174d30adc4SRick Macklem 			}
2184d30adc4SRick Macklem 		} while (tvfc != NULL);
2194d30adc4SRick Macklem 		vfc->vfc_typenum = hashval;
2204d30adc4SRick Macklem 		if (vfc->vfc_typenum >= maxvfsconf)
2214d30adc4SRick Macklem 			maxvfsconf = vfc->vfc_typenum + 1;
2224d30adc4SRick Macklem 	} else
223aa855a59SPeter Wemm 		vfc->vfc_typenum = maxvfsconf++;
2243dfe213eSPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
225aa855a59SPeter Wemm 
226aa855a59SPeter Wemm 	/*
2277652131bSPoul-Henning Kamp 	 * Initialise unused ``struct vfsops'' fields, to use
2287652131bSPoul-Henning Kamp 	 * the vfs_std*() functions.  Note, we need the mount
2297652131bSPoul-Henning Kamp 	 * and unmount operations, at the least.  The check
2307652131bSPoul-Henning Kamp 	 * for vfsops available is just a debugging aid.
2317652131bSPoul-Henning Kamp 	 */
2327652131bSPoul-Henning Kamp 	KASSERT(vfc->vfc_vfsops != NULL,
2337652131bSPoul-Henning Kamp 	    ("Filesystem %s has no vfsops", vfc->vfc_name));
2347652131bSPoul-Henning Kamp 	/*
2357652131bSPoul-Henning Kamp 	 * Check the mount and unmount operations.
2367652131bSPoul-Henning Kamp 	 */
2377652131bSPoul-Henning Kamp 	vfsops = vfc->vfc_vfsops;
23820a92a18SPoul-Henning Kamp 	KASSERT(vfsops->vfs_mount != NULL,
23920a92a18SPoul-Henning Kamp 	    ("Filesystem %s has no mount op", vfc->vfc_name));
2407652131bSPoul-Henning Kamp 	KASSERT(vfsops->vfs_unmount != NULL,
2417652131bSPoul-Henning Kamp 	    ("Filesystem %s has no unmount op", vfc->vfc_name));
2427652131bSPoul-Henning Kamp 
2437652131bSPoul-Henning Kamp 	if (vfsops->vfs_root == NULL)
2447652131bSPoul-Henning Kamp 		/* return file system's root vnode */
2457652131bSPoul-Henning Kamp 		vfsops->vfs_root =	vfs_stdroot;
2467652131bSPoul-Henning Kamp 	if (vfsops->vfs_quotactl == NULL)
2477652131bSPoul-Henning Kamp 		/* quota control */
2487652131bSPoul-Henning Kamp 		vfsops->vfs_quotactl =	vfs_stdquotactl;
2497652131bSPoul-Henning Kamp 	if (vfsops->vfs_statfs == NULL)
2507652131bSPoul-Henning Kamp 		/* return file system's status */
2517652131bSPoul-Henning Kamp 		vfsops->vfs_statfs =	vfs_stdstatfs;
2527652131bSPoul-Henning Kamp 	if (vfsops->vfs_sync == NULL)
2537652131bSPoul-Henning Kamp 		/*
2547652131bSPoul-Henning Kamp 		 * flush unwritten data (nosync)
2557652131bSPoul-Henning Kamp 		 * file systems can use vfs_stdsync
2567652131bSPoul-Henning Kamp 		 * explicitly by setting it in the
2577652131bSPoul-Henning Kamp 		 * vfsop vector.
2587652131bSPoul-Henning Kamp 		 */
2597652131bSPoul-Henning Kamp 		vfsops->vfs_sync =	vfs_stdnosync;
2607652131bSPoul-Henning Kamp 	if (vfsops->vfs_vget == NULL)
2617652131bSPoul-Henning Kamp 		/* convert an inode number to a vnode */
2627652131bSPoul-Henning Kamp 		vfsops->vfs_vget =	vfs_stdvget;
2637652131bSPoul-Henning Kamp 	if (vfsops->vfs_fhtovp == NULL)
2647652131bSPoul-Henning Kamp 		/* turn an NFS file handle into a vnode */
2657652131bSPoul-Henning Kamp 		vfsops->vfs_fhtovp =	vfs_stdfhtovp;
2667652131bSPoul-Henning Kamp 	if (vfsops->vfs_checkexp == NULL)
2677652131bSPoul-Henning Kamp 		/* check if file system is exported */
2687652131bSPoul-Henning Kamp 		vfsops->vfs_checkexp =	vfs_stdcheckexp;
2697652131bSPoul-Henning Kamp 	if (vfsops->vfs_init == NULL)
2707652131bSPoul-Henning Kamp 		/* file system specific initialisation */
2717652131bSPoul-Henning Kamp 		vfsops->vfs_init =	vfs_stdinit;
2727652131bSPoul-Henning Kamp 	if (vfsops->vfs_uninit == NULL)
2737652131bSPoul-Henning Kamp 		/* file system specific uninitialisation */
2747652131bSPoul-Henning Kamp 		vfsops->vfs_uninit =	vfs_stduninit;
2757652131bSPoul-Henning Kamp 	if (vfsops->vfs_extattrctl == NULL)
2767652131bSPoul-Henning Kamp 		/* extended attribute control */
2777652131bSPoul-Henning Kamp 		vfsops->vfs_extattrctl = vfs_stdextattrctl;
27881d16e2dSAlfred Perlstein 	if (vfsops->vfs_sysctl == NULL)
27981d16e2dSAlfred Perlstein 		vfsops->vfs_sysctl = vfs_stdsysctl;
2807652131bSPoul-Henning Kamp 
281*0e5c6bd4SJamie Gritton 	if (vfc->vfc_flags & VFCF_JAIL)
282*0e5c6bd4SJamie Gritton 		prison_add_vfs(vfc);
283*0e5c6bd4SJamie Gritton 
2847652131bSPoul-Henning Kamp 	/*
285aa855a59SPeter Wemm 	 * Call init function for this VFS...
286aa855a59SPeter Wemm 	 */
287aa855a59SPeter Wemm 	(*(vfc->vfc_vfsops->vfs_init))(vfc);
288168f4ee0SKonstantin Belousov 	vfsconf_unlock();
289aa855a59SPeter Wemm 
290168f4ee0SKonstantin Belousov 	/*
291168f4ee0SKonstantin Belousov 	 * If this filesystem has a sysctl node under vfs
292168f4ee0SKonstantin Belousov 	 * (i.e. vfs.xxfs), then change the oid number of that node to
293168f4ee0SKonstantin Belousov 	 * match the filesystem's type number.  This allows user code
294168f4ee0SKonstantin Belousov 	 * which uses the type number to read sysctl variables defined
295168f4ee0SKonstantin Belousov 	 * by the filesystem to continue working. Since the oids are
296168f4ee0SKonstantin Belousov 	 * in a sorted list, we need to make sure the order is
297168f4ee0SKonstantin Belousov 	 * preserved by re-registering the oid after modifying its
298168f4ee0SKonstantin Belousov 	 * number.
299168f4ee0SKonstantin Belousov 	 */
3007665e341SMateusz Guzik 	sysctl_wlock();
301168f4ee0SKonstantin Belousov 	SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) {
302168f4ee0SKonstantin Belousov 		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
303168f4ee0SKonstantin Belousov 			sysctl_unregister_oid(oidp);
304168f4ee0SKonstantin Belousov 			oidp->oid_number = vfc->vfc_typenum;
305168f4ee0SKonstantin Belousov 			sysctl_register_oid(oidp);
306168f4ee0SKonstantin Belousov 			break;
307168f4ee0SKonstantin Belousov 		}
308168f4ee0SKonstantin Belousov 	}
3097665e341SMateusz Guzik 	sysctl_wunlock();
310168f4ee0SKonstantin Belousov 
311168f4ee0SKonstantin Belousov 	return (0);
312aa855a59SPeter Wemm }
313aa855a59SPeter Wemm 
314aa855a59SPeter Wemm 
315eb8e6d52SEivind Eklund /* Remove registration of a filesystem type */
316ebbfc2f8SPoul-Henning Kamp static int
3174e61198eSPeter Wemm vfs_unregister(struct vfsconf *vfc)
318aa855a59SPeter Wemm {
3193dfe213eSPoul-Henning Kamp 	struct vfsconf *vfsp;
32069baeadcSKonstantin Belousov 	int error, maxtypenum;
321aa855a59SPeter Wemm 
322168f4ee0SKonstantin Belousov 	vfsconf_lock();
323168f4ee0SKonstantin Belousov 	vfsp = vfs_byname_locked(vfc->vfc_name);
324168f4ee0SKonstantin Belousov 	if (vfsp == NULL) {
325168f4ee0SKonstantin Belousov 		vfsconf_unlock();
326168f4ee0SKonstantin Belousov 		return (EINVAL);
327168f4ee0SKonstantin Belousov 	}
328168f4ee0SKonstantin Belousov 	if (vfsp->vfc_refcount != 0) {
329168f4ee0SKonstantin Belousov 		vfsconf_unlock();
330168f4ee0SKonstantin Belousov 		return (EBUSY);
331168f4ee0SKonstantin Belousov 	}
332aa855a59SPeter Wemm 	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
333aa855a59SPeter Wemm 		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
334168f4ee0SKonstantin Belousov 		if (error != 0) {
335168f4ee0SKonstantin Belousov 			vfsconf_unlock();
336aa855a59SPeter Wemm 			return (error);
337aa855a59SPeter Wemm 		}
338168f4ee0SKonstantin Belousov 	}
3393dfe213eSPoul-Henning Kamp 	TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
340aa855a59SPeter Wemm 	maxtypenum = VFS_GENERIC;
3413dfe213eSPoul-Henning Kamp 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
342aa855a59SPeter Wemm 		if (maxtypenum < vfsp->vfc_typenum)
343aa855a59SPeter Wemm 			maxtypenum = vfsp->vfc_typenum;
344aa855a59SPeter Wemm 	maxvfsconf = maxtypenum + 1;
345168f4ee0SKonstantin Belousov 	vfsconf_unlock();
346168f4ee0SKonstantin Belousov 	return (0);
347aa855a59SPeter Wemm }
3484e61198eSPeter Wemm 
349eb8e6d52SEivind Eklund /*
350eb8e6d52SEivind Eklund  * Standard kernel module handling code for filesystem modules.
351eb8e6d52SEivind Eklund  * Referenced from VFS_SET().
352eb8e6d52SEivind Eklund  */
3534e61198eSPeter Wemm int
3544ae860afSBruce Evans vfs_modevent(module_t mod, int type, void *data)
3554e61198eSPeter Wemm {
3564e61198eSPeter Wemm 	struct vfsconf *vfc;
3574e61198eSPeter Wemm 	int error = 0;
3584e61198eSPeter Wemm 
3594e61198eSPeter Wemm 	vfc = (struct vfsconf *)data;
3604e61198eSPeter Wemm 
3614e61198eSPeter Wemm 	switch (type) {
3624e61198eSPeter Wemm 	case MOD_LOAD:
3634e61198eSPeter Wemm 		if (vfc)
3644e61198eSPeter Wemm 			error = vfs_register(vfc);
3654e61198eSPeter Wemm 		break;
3664e61198eSPeter Wemm 
3674e61198eSPeter Wemm 	case MOD_UNLOAD:
3684e61198eSPeter Wemm 		if (vfc)
3694e61198eSPeter Wemm 			error = vfs_unregister(vfc);
3704e61198eSPeter Wemm 		break;
3713e019deaSPoul-Henning Kamp 	default:
3723e019deaSPoul-Henning Kamp 		error = EOPNOTSUPP;
3734e61198eSPeter Wemm 		break;
3744e61198eSPeter Wemm 	}
3754e61198eSPeter Wemm 	return (error);
3764e61198eSPeter Wemm }
377