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