FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_init.c
1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed
6 * to Berkeley by John Heidemann of the UCLA Ficus project.
7 *
8 * Source: * @(#)i405_init.c 2.10 92/04/27 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 * 4. 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 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fnv_hash.h>
43 #include <sys/kernel.h>
44 #include <sys/linker.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/sx.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52
53 static int vfs_register(struct vfsconf *);
54 static int vfs_unregister(struct vfsconf *);
55
56 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
57
58 /*
59 * The highest defined VFS number.
60 */
61 int maxvfsconf = VFS_GENERIC + 1;
62
63 /*
64 * Single-linked list of configured VFSes.
65 * New entries are added/deleted by vfs_register()/vfs_unregister()
66 */
67 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
68 struct sx vfsconf_sx;
69 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
70
71 /*
72 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
73 * calculation on vfc_name, so that it doesn't change when file systems are
74 * loaded in a different order. This will avoid the NFS server file handles from
75 * changing for file systems that use vfc_typenum in their fsid.
76 */
77 static int vfs_typenumhash = 1;
78 TUNABLE_INT("vfs.typenumhash", &vfs_typenumhash);
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.");
82
83 /*
84 * A Zen vnode attribute structure.
85 *
86 * Initialized when the first filesystem registers by vfs_register().
87 */
88 struct vattr va_null;
89
90 /*
91 * vfs_init.c
92 *
93 * Allocate and fill in operations vectors.
94 *
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.
105 */
106
107 /*
108 * Routines having to do with the management of the vnode table.
109 */
110
111 static struct vfsconf *
112 vfs_byname_locked(const char *name)
113 {
114 struct vfsconf *vfsp;
115
116 sx_assert(&vfsconf_sx, SA_LOCKED);
117 if (!strcmp(name, "ffs"))
118 name = "ufs";
119 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
120 if (!strcmp(name, vfsp->vfc_name))
121 return (vfsp);
122 }
123 return (NULL);
124 }
125
126 struct vfsconf *
127 vfs_byname(const char *name)
128 {
129 struct vfsconf *vfsp;
130
131 vfsconf_slock();
132 vfsp = vfs_byname_locked(name);
133 vfsconf_sunlock();
134 return (vfsp);
135 }
136
137 struct vfsconf *
138 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
139 {
140 struct vfsconf *vfsp;
141 int fileid, loaded;
142
143 vfsp = vfs_byname(fstype);
144 if (vfsp != NULL)
145 return (vfsp);
146
147 /* Try to load the respective module. */
148 *error = kern_kldload(td, fstype, &fileid);
149 loaded = (*error == 0);
150 if (*error == EEXIST)
151 *error = 0;
152 if (*error)
153 return (NULL);
154
155 /* Look up again to see if the VFS was loaded. */
156 vfsp = vfs_byname(fstype);
157 if (vfsp == NULL) {
158 if (loaded)
159 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
160 *error = ENODEV;
161 return (NULL);
162 }
163 return (vfsp);
164 }
165
166
167 /* Register a new filesystem type in the global table */
168 static int
169 vfs_register(struct vfsconf *vfc)
170 {
171 struct sysctl_oid *oidp;
172 struct vfsops *vfsops;
173 static int once;
174 struct vfsconf *tvfc;
175 uint32_t hashval;
176 int secondpass;
177
178 if (!once) {
179 vattr_null(&va_null);
180 once = 1;
181 }
182
183 if (vfc->vfc_version != VFS_VERSION) {
184 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
185 vfc->vfc_name, vfc->vfc_version);
186 return (EINVAL);
187 }
188 vfsconf_lock();
189 if (vfs_byname_locked(vfc->vfc_name) != NULL) {
190 vfsconf_unlock();
191 return (EEXIST);
192 }
193
194 if (vfs_typenumhash != 0) {
195 /*
196 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
197 * all of 1<->255 are assigned, it is limited to 8bits since
198 * that is what ZFS uses from vfc_typenum and is also the
199 * preferred range for vfs_getnewfsid().
200 */
201 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
202 hashval &= 0xff;
203 secondpass = 0;
204 do {
205 /* Look for and fix any collision. */
206 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
207 if (hashval == tvfc->vfc_typenum) {
208 if (hashval == 255 && secondpass == 0) {
209 hashval = 1;
210 secondpass = 1;
211 } else
212 hashval++;
213 break;
214 }
215 }
216 } while (tvfc != NULL);
217 vfc->vfc_typenum = hashval;
218 if (vfc->vfc_typenum >= maxvfsconf)
219 maxvfsconf = vfc->vfc_typenum + 1;
220 } else
221 vfc->vfc_typenum = maxvfsconf++;
222 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
223
224 /*
225 * Initialise unused ``struct vfsops'' fields, to use
226 * the vfs_std*() functions. Note, we need the mount
227 * and unmount operations, at the least. The check
228 * for vfsops available is just a debugging aid.
229 */
230 KASSERT(vfc->vfc_vfsops != NULL,
231 ("Filesystem %s has no vfsops", vfc->vfc_name));
232 /*
233 * Check the mount and unmount operations.
234 */
235 vfsops = vfc->vfc_vfsops;
236 KASSERT(vfsops->vfs_mount != NULL,
237 ("Filesystem %s has no mount op", vfc->vfc_name));
238 KASSERT(vfsops->vfs_unmount != NULL,
239 ("Filesystem %s has no unmount op", vfc->vfc_name));
240
241 if (vfsops->vfs_root == NULL)
242 /* return file system's root vnode */
243 vfsops->vfs_root = vfs_stdroot;
244 if (vfsops->vfs_quotactl == NULL)
245 /* quota control */
246 vfsops->vfs_quotactl = vfs_stdquotactl;
247 if (vfsops->vfs_statfs == NULL)
248 /* return file system's status */
249 vfsops->vfs_statfs = vfs_stdstatfs;
250 if (vfsops->vfs_sync == NULL)
251 /*
252 * flush unwritten data (nosync)
253 * file systems can use vfs_stdsync
254 * explicitly by setting it in the
255 * vfsop vector.
256 */
257 vfsops->vfs_sync = vfs_stdnosync;
258 if (vfsops->vfs_vget == NULL)
259 /* convert an inode number to a vnode */
260 vfsops->vfs_vget = vfs_stdvget;
261 if (vfsops->vfs_fhtovp == NULL)
262 /* turn an NFS file handle into a vnode */
263 vfsops->vfs_fhtovp = vfs_stdfhtovp;
264 if (vfsops->vfs_checkexp == NULL)
265 /* check if file system is exported */
266 vfsops->vfs_checkexp = vfs_stdcheckexp;
267 if (vfsops->vfs_init == NULL)
268 /* file system specific initialisation */
269 vfsops->vfs_init = vfs_stdinit;
270 if (vfsops->vfs_uninit == NULL)
271 /* file system specific uninitialisation */
272 vfsops->vfs_uninit = vfs_stduninit;
273 if (vfsops->vfs_extattrctl == NULL)
274 /* extended attribute control */
275 vfsops->vfs_extattrctl = vfs_stdextattrctl;
276 if (vfsops->vfs_sysctl == NULL)
277 vfsops->vfs_sysctl = vfs_stdsysctl;
278
279 /*
280 * Call init function for this VFS...
281 */
282 (*(vfc->vfc_vfsops->vfs_init))(vfc);
283 vfsconf_unlock();
284
285 /*
286 * If this filesystem has a sysctl node under vfs
287 * (i.e. vfs.xxfs), then change the oid number of that node to
288 * match the filesystem's type number. This allows user code
289 * which uses the type number to read sysctl variables defined
290 * by the filesystem to continue working. Since the oids are
291 * in a sorted list, we need to make sure the order is
292 * preserved by re-registering the oid after modifying its
293 * number.
294 */
295 sysctl_lock();
296 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) {
297 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
298 sysctl_unregister_oid(oidp);
299 oidp->oid_number = vfc->vfc_typenum;
300 sysctl_register_oid(oidp);
301 break;
302 }
303 }
304 sysctl_unlock();
305
306 return (0);
307 }
308
309
310 /* Remove registration of a filesystem type */
311 static int
312 vfs_unregister(struct vfsconf *vfc)
313 {
314 struct vfsconf *vfsp;
315 int error, maxtypenum;
316
317 vfsconf_lock();
318 vfsp = vfs_byname_locked(vfc->vfc_name);
319 if (vfsp == NULL) {
320 vfsconf_unlock();
321 return (EINVAL);
322 }
323 if (vfsp->vfc_refcount != 0) {
324 vfsconf_unlock();
325 return (EBUSY);
326 }
327 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
328 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
329 if (error != 0) {
330 vfsconf_unlock();
331 return (error);
332 }
333 }
334 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
335 maxtypenum = VFS_GENERIC;
336 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
337 if (maxtypenum < vfsp->vfc_typenum)
338 maxtypenum = vfsp->vfc_typenum;
339 maxvfsconf = maxtypenum + 1;
340 vfsconf_unlock();
341 return (0);
342 }
343
344 /*
345 * Standard kernel module handling code for filesystem modules.
346 * Referenced from VFS_SET().
347 */
348 int
349 vfs_modevent(module_t mod, int type, void *data)
350 {
351 struct vfsconf *vfc;
352 int error = 0;
353
354 vfc = (struct vfsconf *)data;
355
356 switch (type) {
357 case MOD_LOAD:
358 if (vfc)
359 error = vfs_register(vfc);
360 break;
361
362 case MOD_UNLOAD:
363 if (vfc)
364 error = vfs_unregister(vfc);
365 break;
366 default:
367 error = EOPNOTSUPP;
368 break;
369 }
370 return (error);
371 }
Cache object: 8eafc6bfa7c1e4ea764ebc1b09e812ec
|