xref: /freebsd/sys/kern/vfs_init.c (revision be8bae67002503baff6bb9fb77fbe6b80f035c33)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed
8  * to Berkeley by John Heidemann of the UCLA Ficus project.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/fnv_hash.h>
38 #include <sys/jail.h>
39 #include <sys/kernel.h>
40 #include <sys/linker.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/sx.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
48 
49 static int	vfs_register(struct vfsconf *);
50 static int	vfs_unregister(struct vfsconf *);
51 
52 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
53 
54 /*
55  * The highest defined VFS number.
56  */
57 int maxvfsconf = VFS_GENERIC + 1;
58 
59 /*
60  * Single-linked list of configured VFSes.
61  * New entries are added/deleted by vfs_register()/vfs_unregister()
62  */
63 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
64 struct sx vfsconf_sx;
65 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
66 
67 /*
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.
72  */
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.");
77 
78 /*
79  * A Zen vnode attribute structure.
80  *
81  * Initialized when the first filesystem registers by vfs_register().
82  */
83 struct vattr va_null;
84 
85 /*
86  * vfs_init.c
87  *
88  * Allocate and fill in operations vectors.
89  *
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.
100  */
101 
102 /*
103  * Routines having to do with the management of the vnode table.
104  */
105 
106 void
vfs_unref_vfsconf(struct vfsconf * vfsp)107 vfs_unref_vfsconf(struct vfsconf *vfsp)
108 {
109 	vfsconf_lock();
110 	KASSERT(vfsp->vfc_refcount > 0,
111 	    ("vfs %p refcount underflow %d", vfsp, vfsp->vfc_refcount));
112 	vfsp->vfc_refcount--;
113 	vfsconf_unlock();
114 }
115 
116 static struct vfsconf *
vfs_byname_locked(const char * name)117 vfs_byname_locked(const char *name)
118 {
119 	struct vfsconf *vfsp;
120 
121 	sx_assert(&vfsconf_sx, SA_LOCKED);
122 	if (!strcmp(name, "ffs"))
123 		name = "ufs";
124 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
125 		if (!strcmp(name, vfsp->vfc_name))
126 			return (vfsp);
127 	}
128 	return (NULL);
129 }
130 
131 struct vfsconf *
vfs_byname(const char * name)132 vfs_byname(const char *name)
133 {
134 	struct vfsconf *vfsp;
135 
136 	vfsconf_lock();
137 	vfsp = vfs_byname_locked(name);
138 	if (vfsp != NULL)
139 		vfsp->vfc_refcount++;
140 	vfsconf_unlock();
141 	return (vfsp);
142 }
143 
144 struct vfsconf *
vfs_byname_kld(const char * fstype,struct thread * td,int * error)145 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
146 {
147 	struct vfsconf *vfsp;
148 	int fileid, loaded;
149 
150 	vfsp = vfs_byname(fstype);
151 	if (vfsp != NULL)
152 		return (vfsp);
153 
154 	/* Try to load the respective module. */
155 	*error = kern_kldload(td, fstype, &fileid);
156 	loaded = (*error == 0);
157 	if (*error == EEXIST)
158 		*error = 0;
159 	if (*error) {
160 		*error = ENODEV;
161 		return (NULL);
162 	}
163 
164 	/* Look up again to see if the VFS was loaded. */
165 	vfsp = vfs_byname(fstype);
166 	if (vfsp == NULL) {
167 		if (loaded)
168 			(void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
169 		*error = ENODEV;
170 		return (NULL);
171 	}
172 	return (vfsp);
173 }
174 
175 static int
vfs_mount_sigdefer(struct mount * mp)176 vfs_mount_sigdefer(struct mount *mp)
177 {
178 	int prev_stops, rc;
179 
180 	TSRAW(curthread, TS_ENTER, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
181 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
182 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_mount)(mp);
183 	sigallowstop(prev_stops);
184 	TSRAW(curthread, TS_EXIT, "VFS_MOUNT", mp->mnt_vfc->vfc_name);
185 	return (rc);
186 }
187 
188 static int
vfs_unmount_sigdefer(struct mount * mp,int mntflags)189 vfs_unmount_sigdefer(struct mount *mp, int mntflags)
190 {
191 	int prev_stops, rc;
192 
193 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
194 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unmount)(mp, mntflags);
195 	sigallowstop(prev_stops);
196 	return (rc);
197 }
198 
199 static int
vfs_root_sigdefer(struct mount * mp,int flags,struct vnode ** vpp)200 vfs_root_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
201 {
202 	int prev_stops, rc;
203 
204 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
205 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_root)(mp, flags, vpp);
206 	sigallowstop(prev_stops);
207 	return (rc);
208 }
209 
210 static int
vfs_cachedroot_sigdefer(struct mount * mp,int flags,struct vnode ** vpp)211 vfs_cachedroot_sigdefer(struct mount *mp, int flags, struct vnode **vpp)
212 {
213 	int prev_stops, rc;
214 
215 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
216 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_cachedroot)(mp, flags, vpp);
217 	sigallowstop(prev_stops);
218 	return (rc);
219 }
220 
221 static int
vfs_quotactl_sigdefer(struct mount * mp,int cmd,uid_t uid,void * arg,bool * mp_busy)222 vfs_quotactl_sigdefer(struct mount *mp, int cmd, uid_t uid, void *arg,
223     bool *mp_busy)
224 {
225 	int prev_stops, rc;
226 
227 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
228 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_quotactl)(mp, cmd, uid, arg,
229 	    mp_busy);
230 	sigallowstop(prev_stops);
231 	return (rc);
232 }
233 
234 static int
vfs_statfs_sigdefer(struct mount * mp,struct statfs * sbp)235 vfs_statfs_sigdefer(struct mount *mp, struct statfs *sbp)
236 {
237 	int prev_stops, rc;
238 
239 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
240 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_statfs)(mp, sbp);
241 	sigallowstop(prev_stops);
242 	return (rc);
243 }
244 
245 static int
vfs_sync_sigdefer(struct mount * mp,int waitfor)246 vfs_sync_sigdefer(struct mount *mp, int waitfor)
247 {
248 	int prev_stops, rc;
249 
250 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
251 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sync)(mp, waitfor);
252 	sigallowstop(prev_stops);
253 	return (rc);
254 }
255 
256 static int
vfs_vget_sigdefer(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp)257 vfs_vget_sigdefer(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
258 {
259 	int prev_stops, rc;
260 
261 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
262 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_vget)(mp, ino, flags, vpp);
263 	sigallowstop(prev_stops);
264 	return (rc);
265 }
266 
267 static int
vfs_fhtovp_sigdefer(struct mount * mp,struct fid * fidp,int flags,struct vnode ** vpp)268 vfs_fhtovp_sigdefer(struct mount *mp, struct fid *fidp, int flags,
269     struct vnode **vpp)
270 {
271 	int prev_stops, rc;
272 
273 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
274 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_fhtovp)(mp, fidp, flags, vpp);
275 	sigallowstop(prev_stops);
276 	return (rc);
277 }
278 
279 static int
vfs_checkexp_sigdefer(struct mount * mp,struct sockaddr * nam,uint64_t * exflg,struct ucred ** credp,int * numsecflavors,int * secflavors)280 vfs_checkexp_sigdefer(struct mount *mp, struct sockaddr *nam, uint64_t *exflg,
281     struct ucred **credp, int *numsecflavors, int *secflavors)
282 {
283 	int prev_stops, rc;
284 
285 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
286 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_checkexp)(mp, nam, exflg, credp,
287 	    numsecflavors, secflavors);
288 	sigallowstop(prev_stops);
289 	return (rc);
290 }
291 
292 static int
vfs_extattrctl_sigdefer(struct mount * mp,int cmd,struct vnode * filename_vp,int attrnamespace,const char * attrname)293 vfs_extattrctl_sigdefer(struct mount *mp, int cmd, struct vnode *filename_vp,
294     int attrnamespace, const char *attrname)
295 {
296 	int prev_stops, rc;
297 
298 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
299 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_extattrctl)(mp, cmd,
300 	    filename_vp, attrnamespace, attrname);
301 	sigallowstop(prev_stops);
302 	return (rc);
303 }
304 
305 static int
vfs_sysctl_sigdefer(struct mount * mp,fsctlop_t op,struct sysctl_req * req)306 vfs_sysctl_sigdefer(struct mount *mp, fsctlop_t op, struct sysctl_req *req)
307 {
308 	int prev_stops, rc;
309 
310 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
311 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sysctl)(mp, op, req);
312 	sigallowstop(prev_stops);
313 	return (rc);
314 }
315 
316 static void
vfs_susp_clean_sigdefer(struct mount * mp)317 vfs_susp_clean_sigdefer(struct mount *mp)
318 {
319 	int prev_stops;
320 
321 	if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean == NULL)
322 		return;
323 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
324 	(*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean)(mp);
325 	sigallowstop(prev_stops);
326 }
327 
328 static void
vfs_reclaim_lowervp_sigdefer(struct mount * mp,struct vnode * vp)329 vfs_reclaim_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
330 {
331 	int prev_stops;
332 
333 	if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp == NULL)
334 		return;
335 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
336 	(*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp)(mp, vp);
337 	sigallowstop(prev_stops);
338 }
339 
340 static void
vfs_unlink_lowervp_sigdefer(struct mount * mp,struct vnode * vp)341 vfs_unlink_lowervp_sigdefer(struct mount *mp, struct vnode *vp)
342 {
343 	int prev_stops;
344 
345 	if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp == NULL)
346 		return;
347 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
348 	(*(mp)->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp)(mp, vp);
349 	sigallowstop(prev_stops);
350 }
351 
352 static void
vfs_purge_sigdefer(struct mount * mp)353 vfs_purge_sigdefer(struct mount *mp)
354 {
355 	int prev_stops;
356 
357 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
358 	(*mp->mnt_vfc->vfc_vfsops_sd->vfs_purge)(mp);
359 	sigallowstop(prev_stops);
360 }
361 
362 static int
vfs_report_lockf_sigdefer(struct mount * mp,struct sbuf * sb)363 vfs_report_lockf_sigdefer(struct mount *mp, struct sbuf *sb)
364 {
365 	int prev_stops, rc;
366 
367 	prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT);
368 	rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_report_lockf)(mp, sb);
369 	sigallowstop(prev_stops);
370 	return (rc);
371 }
372 
373 static struct vfsops vfsops_sigdefer = {
374 	.vfs_mount =		vfs_mount_sigdefer,
375 	.vfs_unmount =		vfs_unmount_sigdefer,
376 	.vfs_root =		vfs_root_sigdefer,
377 	.vfs_cachedroot =	vfs_cachedroot_sigdefer,
378 	.vfs_quotactl =		vfs_quotactl_sigdefer,
379 	.vfs_statfs =		vfs_statfs_sigdefer,
380 	.vfs_sync =		vfs_sync_sigdefer,
381 	.vfs_vget =		vfs_vget_sigdefer,
382 	.vfs_fhtovp =		vfs_fhtovp_sigdefer,
383 	.vfs_checkexp =		vfs_checkexp_sigdefer,
384 	.vfs_extattrctl =	vfs_extattrctl_sigdefer,
385 	.vfs_sysctl =		vfs_sysctl_sigdefer,
386 	.vfs_susp_clean =	vfs_susp_clean_sigdefer,
387 	.vfs_reclaim_lowervp =	vfs_reclaim_lowervp_sigdefer,
388 	.vfs_unlink_lowervp =	vfs_unlink_lowervp_sigdefer,
389 	.vfs_purge =		vfs_purge_sigdefer,
390 	.vfs_report_lockf =	vfs_report_lockf_sigdefer,
391 };
392 
393 /* Register a new filesystem type in the global table */
394 static int
vfs_register(struct vfsconf * vfc)395 vfs_register(struct vfsconf *vfc)
396 {
397 	struct sysctl_oid *oidp;
398 	struct vfsops *vfsops;
399 	static int once;
400 	struct vfsconf *tvfc;
401 	uint32_t hashval;
402 	int secondpass;
403 
404 	if (!once) {
405 		vattr_null(&va_null);
406 		once = 1;
407 	}
408 
409 	if (vfc->vfc_version != VFS_VERSION) {
410 		printf("ERROR: filesystem %s, unsupported ABI version %x\n",
411 		    vfc->vfc_name, vfc->vfc_version);
412 		return (EINVAL);
413 	}
414 	vfsconf_lock();
415 	if (vfs_byname_locked(vfc->vfc_name) != NULL) {
416 		vfsconf_unlock();
417 		return (EEXIST);
418 	}
419 
420 	if (vfs_typenumhash != 0) {
421 		/*
422 		 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
423 		 * all of 1<->255 are assigned, it is limited to 8bits since
424 		 * that is what ZFS uses from vfc_typenum and is also the
425 		 * preferred range for vfs_getnewfsid().
426 		 */
427 		hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
428 		hashval &= 0xff;
429 		secondpass = 0;
430 		do {
431 			/* Look for and fix any collision. */
432 			TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
433 				if (hashval == tvfc->vfc_typenum) {
434 					if (hashval == 255 && secondpass == 0) {
435 						hashval = 1;
436 						secondpass = 1;
437 					} else
438 						hashval++;
439 					break;
440 				}
441 			}
442 		} while (tvfc != NULL);
443 		vfc->vfc_typenum = hashval;
444 		if (vfc->vfc_typenum >= maxvfsconf)
445 			maxvfsconf = vfc->vfc_typenum + 1;
446 	} else
447 		vfc->vfc_typenum = maxvfsconf++;
448 	TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
449 
450 	/*
451 	 * Initialise unused ``struct vfsops'' fields, to use
452 	 * the vfs_std*() functions.  Note, we need the mount
453 	 * and unmount operations, at the least.  The check
454 	 * for vfsops available is just a debugging aid.
455 	 */
456 	KASSERT(vfc->vfc_vfsops != NULL,
457 	    ("Filesystem %s has no vfsops", vfc->vfc_name));
458 	/*
459 	 * Check the mount and unmount operations.
460 	 */
461 	vfsops = vfc->vfc_vfsops;
462 	KASSERT(vfsops->vfs_mount != NULL,
463 	    ("Filesystem %s has no mount op", vfc->vfc_name));
464 	KASSERT(vfsops->vfs_unmount != NULL,
465 	    ("Filesystem %s has no unmount op", vfc->vfc_name));
466 
467 	if (vfsops->vfs_root == NULL)
468 		/* return file system's root vnode */
469 		vfsops->vfs_root =	vfs_stdroot;
470 	if (vfsops->vfs_quotactl == NULL)
471 		/* quota control */
472 		vfsops->vfs_quotactl =	vfs_stdquotactl;
473 	if (vfsops->vfs_statfs == NULL)
474 		/* return file system's status */
475 		vfsops->vfs_statfs =	vfs_stdstatfs;
476 	if (vfsops->vfs_sync == NULL)
477 		/*
478 		 * flush unwritten data (nosync)
479 		 * file systems can use vfs_stdsync
480 		 * explicitly by setting it in the
481 		 * vfsop vector.
482 		 */
483 		vfsops->vfs_sync =	vfs_stdnosync;
484 	if (vfsops->vfs_vget == NULL)
485 		/* convert an inode number to a vnode */
486 		vfsops->vfs_vget =	vfs_stdvget;
487 	if (vfsops->vfs_fhtovp == NULL)
488 		/* turn an NFS file handle into a vnode */
489 		vfsops->vfs_fhtovp =	vfs_stdfhtovp;
490 	if (vfsops->vfs_checkexp == NULL)
491 		/* check if file system is exported */
492 		vfsops->vfs_checkexp =	vfs_stdcheckexp;
493 	if (vfsops->vfs_init == NULL)
494 		/* file system specific initialisation */
495 		vfsops->vfs_init =	vfs_stdinit;
496 	if (vfsops->vfs_uninit == NULL)
497 		/* file system specific uninitialisation */
498 		vfsops->vfs_uninit =	vfs_stduninit;
499 	if (vfsops->vfs_extattrctl == NULL)
500 		/* extended attribute control */
501 		vfsops->vfs_extattrctl = vfs_stdextattrctl;
502 	if (vfsops->vfs_sysctl == NULL)
503 		vfsops->vfs_sysctl = vfs_stdsysctl;
504 	if (vfsops->vfs_report_lockf == NULL)
505 		vfsops->vfs_report_lockf = vfs_report_lockf;
506 
507 	if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
508 		vfc->vfc_vfsops_sd = vfc->vfc_vfsops;
509 		vfc->vfc_vfsops = &vfsops_sigdefer;
510 	}
511 
512 	if (vfc->vfc_flags & VFCF_JAIL)
513 		prison_add_vfs(vfc);
514 
515 	/*
516 	 * Call init function for this VFS...
517 	 */
518 	if ((vfc->vfc_flags & VFCF_SBDRY) != 0)
519 		vfc->vfc_vfsops_sd->vfs_init(vfc);
520 	else
521 		vfc->vfc_vfsops->vfs_init(vfc);
522 	vfsconf_unlock();
523 
524 	/*
525 	 * If this filesystem has a sysctl node under vfs
526 	 * (i.e. vfs.xxfs), then change the oid number of that node to
527 	 * match the filesystem's type number.  This allows user code
528 	 * which uses the type number to read sysctl variables defined
529 	 * by the filesystem to continue working. Since the oids are
530 	 * in a sorted list, we need to make sure the order is
531 	 * preserved by re-registering the oid after modifying its
532 	 * number.
533 	 */
534 	sysctl_wlock();
535 	RB_FOREACH(oidp, sysctl_oid_list, SYSCTL_CHILDREN(&sysctl___vfs)) {
536 		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
537 			sysctl_unregister_oid(oidp);
538 			oidp->oid_number = vfc->vfc_typenum;
539 			sysctl_register_oid(oidp);
540 			break;
541 		}
542 	}
543 	sysctl_wunlock();
544 
545 	return (0);
546 }
547 
548 /* Remove registration of a filesystem type */
549 static int
vfs_unregister(struct vfsconf * vfc)550 vfs_unregister(struct vfsconf *vfc)
551 {
552 	struct vfsconf *vfsp;
553 	int error, maxtypenum;
554 
555 	vfsconf_lock();
556 	vfsp = vfs_byname_locked(vfc->vfc_name);
557 	if (vfsp == NULL) {
558 		vfsconf_unlock();
559 		return (EINVAL);
560 	}
561 	if (vfsp->vfc_refcount != 0) {
562 		vfsconf_unlock();
563 		return (EBUSY);
564 	}
565 	error = 0;
566 	if ((vfc->vfc_flags & VFCF_SBDRY) != 0) {
567 		if (vfc->vfc_vfsops_sd->vfs_uninit != NULL)
568 			error = vfc->vfc_vfsops_sd->vfs_uninit(vfsp);
569 	} else {
570 		if (vfc->vfc_vfsops->vfs_uninit != NULL)
571 			error = vfc->vfc_vfsops->vfs_uninit(vfsp);
572 	}
573 	if (error != 0) {
574 		vfsconf_unlock();
575 		return (error);
576 	}
577 	TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
578 	maxtypenum = VFS_GENERIC;
579 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
580 		if (maxtypenum < vfsp->vfc_typenum)
581 			maxtypenum = vfsp->vfc_typenum;
582 	maxvfsconf = maxtypenum + 1;
583 	vfsconf_unlock();
584 	return (0);
585 }
586 
587 /*
588  * Standard kernel module handling code for filesystem modules.
589  * Referenced from VFS_SET().
590  */
591 int
vfs_modevent(module_t mod,int type,void * data)592 vfs_modevent(module_t mod, int type, void *data)
593 {
594 	struct vfsconf *vfc;
595 	int error = 0;
596 
597 	vfc = (struct vfsconf *)data;
598 
599 	switch (type) {
600 	case MOD_LOAD:
601 		if (vfc)
602 			error = vfs_register(vfc);
603 		break;
604 
605 	case MOD_UNLOAD:
606 		if (vfc)
607 			error = vfs_unregister(vfc);
608 		break;
609 	default:
610 		error = EOPNOTSUPP;
611 		break;
612 	}
613 	return (error);
614 }
615