xref: /freebsd/sys/kern/vfs_init.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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 
169 /* Register a new filesystem type in the global table */
170 static int
171 vfs_register(struct vfsconf *vfc)
172 {
173 	struct sysctl_oid *oidp;
174 	struct vfsops *vfsops;
175 	static int once;
176 	struct vfsconf *tvfc;
177 	uint32_t hashval;
178 	int secondpass;
179 
180 	if (!once) {
181 		vattr_null(&va_null);
182 		once = 1;
183 	}
184 
185 	if (vfc->vfc_version != VFS_VERSION) {
186 		printf("ERROR: filesystem %s, unsupported ABI version %x\n",
187 		    vfc->vfc_name, vfc->vfc_version);
188 		return (EINVAL);
189 	}
190 	vfsconf_lock();
191 	if (vfs_byname_locked(vfc->vfc_name) != NULL) {
192 		vfsconf_unlock();
193 		return (EEXIST);
194 	}
195 
196 	if (vfs_typenumhash != 0) {
197 		/*
198 		 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
199 		 * all of 1<->255 are assigned, it is limited to 8bits since
200 		 * that is what ZFS uses from vfc_typenum and is also the
201 		 * preferred range for vfs_getnewfsid().
202 		 */
203 		hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
204 		hashval &= 0xff;
205 		secondpass = 0;
206 		do {
207 			/* Look for and fix any collision. */
208 			TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
209 				if (hashval == tvfc->vfc_typenum) {
210 					if (hashval == 255 && secondpass == 0) {
211 						hashval = 1;
212 						secondpass = 1;
213 					} else
214 						hashval++;
215 					break;
216 				}
217 			}
218 		} while (tvfc != NULL);
219 		vfc->vfc_typenum = hashval;
220 		if (vfc->vfc_typenum >= maxvfsconf)
221 			maxvfsconf = vfc->vfc_typenum + 1;
222 	} else
223 		vfc->vfc_typenum = maxvfsconf++;
224 	TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
225 
226 	/*
227 	 * Initialise unused ``struct vfsops'' fields, to use
228 	 * the vfs_std*() functions.  Note, we need the mount
229 	 * and unmount operations, at the least.  The check
230 	 * for vfsops available is just a debugging aid.
231 	 */
232 	KASSERT(vfc->vfc_vfsops != NULL,
233 	    ("Filesystem %s has no vfsops", vfc->vfc_name));
234 	/*
235 	 * Check the mount and unmount operations.
236 	 */
237 	vfsops = vfc->vfc_vfsops;
238 	KASSERT(vfsops->vfs_mount != NULL,
239 	    ("Filesystem %s has no mount op", vfc->vfc_name));
240 	KASSERT(vfsops->vfs_unmount != NULL,
241 	    ("Filesystem %s has no unmount op", vfc->vfc_name));
242 
243 	if (vfsops->vfs_root == NULL)
244 		/* return file system's root vnode */
245 		vfsops->vfs_root =	vfs_stdroot;
246 	if (vfsops->vfs_quotactl == NULL)
247 		/* quota control */
248 		vfsops->vfs_quotactl =	vfs_stdquotactl;
249 	if (vfsops->vfs_statfs == NULL)
250 		/* return file system's status */
251 		vfsops->vfs_statfs =	vfs_stdstatfs;
252 	if (vfsops->vfs_sync == NULL)
253 		/*
254 		 * flush unwritten data (nosync)
255 		 * file systems can use vfs_stdsync
256 		 * explicitly by setting it in the
257 		 * vfsop vector.
258 		 */
259 		vfsops->vfs_sync =	vfs_stdnosync;
260 	if (vfsops->vfs_vget == NULL)
261 		/* convert an inode number to a vnode */
262 		vfsops->vfs_vget =	vfs_stdvget;
263 	if (vfsops->vfs_fhtovp == NULL)
264 		/* turn an NFS file handle into a vnode */
265 		vfsops->vfs_fhtovp =	vfs_stdfhtovp;
266 	if (vfsops->vfs_checkexp == NULL)
267 		/* check if file system is exported */
268 		vfsops->vfs_checkexp =	vfs_stdcheckexp;
269 	if (vfsops->vfs_init == NULL)
270 		/* file system specific initialisation */
271 		vfsops->vfs_init =	vfs_stdinit;
272 	if (vfsops->vfs_uninit == NULL)
273 		/* file system specific uninitialisation */
274 		vfsops->vfs_uninit =	vfs_stduninit;
275 	if (vfsops->vfs_extattrctl == NULL)
276 		/* extended attribute control */
277 		vfsops->vfs_extattrctl = vfs_stdextattrctl;
278 	if (vfsops->vfs_sysctl == NULL)
279 		vfsops->vfs_sysctl = vfs_stdsysctl;
280 
281 	if (vfc->vfc_flags & VFCF_JAIL)
282 		prison_add_vfs(vfc);
283 
284 	/*
285 	 * Call init function for this VFS...
286 	 */
287 	(*(vfc->vfc_vfsops->vfs_init))(vfc);
288 	vfsconf_unlock();
289 
290 	/*
291 	 * If this filesystem has a sysctl node under vfs
292 	 * (i.e. vfs.xxfs), then change the oid number of that node to
293 	 * match the filesystem's type number.  This allows user code
294 	 * which uses the type number to read sysctl variables defined
295 	 * by the filesystem to continue working. Since the oids are
296 	 * in a sorted list, we need to make sure the order is
297 	 * preserved by re-registering the oid after modifying its
298 	 * number.
299 	 */
300 	sysctl_wlock();
301 	SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) {
302 		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
303 			sysctl_unregister_oid(oidp);
304 			oidp->oid_number = vfc->vfc_typenum;
305 			sysctl_register_oid(oidp);
306 			break;
307 		}
308 	}
309 	sysctl_wunlock();
310 
311 	return (0);
312 }
313 
314 
315 /* Remove registration of a filesystem type */
316 static int
317 vfs_unregister(struct vfsconf *vfc)
318 {
319 	struct vfsconf *vfsp;
320 	int error, maxtypenum;
321 
322 	vfsconf_lock();
323 	vfsp = vfs_byname_locked(vfc->vfc_name);
324 	if (vfsp == NULL) {
325 		vfsconf_unlock();
326 		return (EINVAL);
327 	}
328 	if (vfsp->vfc_refcount != 0) {
329 		vfsconf_unlock();
330 		return (EBUSY);
331 	}
332 	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
333 		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
334 		if (error != 0) {
335 			vfsconf_unlock();
336 			return (error);
337 		}
338 	}
339 	TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
340 	maxtypenum = VFS_GENERIC;
341 	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
342 		if (maxtypenum < vfsp->vfc_typenum)
343 			maxtypenum = vfsp->vfc_typenum;
344 	maxvfsconf = maxtypenum + 1;
345 	vfsconf_unlock();
346 	return (0);
347 }
348 
349 /*
350  * Standard kernel module handling code for filesystem modules.
351  * Referenced from VFS_SET().
352  */
353 int
354 vfs_modevent(module_t mod, int type, void *data)
355 {
356 	struct vfsconf *vfc;
357 	int error = 0;
358 
359 	vfc = (struct vfsconf *)data;
360 
361 	switch (type) {
362 	case MOD_LOAD:
363 		if (vfc)
364 			error = vfs_register(vfc);
365 		break;
366 
367 	case MOD_UNLOAD:
368 		if (vfc)
369 			error = vfs_unregister(vfc);
370 		break;
371 	default:
372 		error = EOPNOTSUPP;
373 		break;
374 	}
375 	return (error);
376 }
377