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: releng/9.0/sys/kern/vfs_init.c 225537 2011-09-13 21:01:26Z rmacklem $");
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/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51
52 static int vfs_register(struct vfsconf *);
53 static int vfs_unregister(struct vfsconf *);
54
55 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
56
57 /*
58 * The highest defined VFS number.
59 */
60 int maxvfsconf = VFS_GENERIC + 1;
61
62 /*
63 * Single-linked list of configured VFSes.
64 * New entries are added/deleted by vfs_register()/vfs_unregister()
65 */
66 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
67
68 /*
69 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
70 * calculation on vfc_name, so that it doesn't change when file systems are
71 * loaded in a different order. This will avoid the NFS server file handles from
72 * changing for file systems that use vfc_typenum in their fsid.
73 */
74 static int vfs_typenumhash = 1;
75 TUNABLE_INT("vfs.typenumhash", &vfs_typenumhash);
76 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
77 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
78 "change when file systems are loaded in a different order.");
79
80 /*
81 * A Zen vnode attribute structure.
82 *
83 * Initialized when the first filesystem registers by vfs_register().
84 */
85 struct vattr va_null;
86
87 /*
88 * vfs_init.c
89 *
90 * Allocate and fill in operations vectors.
91 *
92 * An undocumented feature of this approach to defining operations is that
93 * there can be multiple entries in vfs_opv_descs for the same operations
94 * vector. This allows third parties to extend the set of operations
95 * supported by another layer in a binary compatibile way. For example,
96 * assume that NFS needed to be modified to support Ficus. NFS has an entry
97 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
98 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
99 * listing those new operations Ficus adds to NFS, all without modifying the
100 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
101 * that is a(whole)nother story.) This is a feature.
102 */
103
104 /*
105 * Routines having to do with the management of the vnode table.
106 */
107
108 struct vfsconf *
109 vfs_byname(const char *name)
110 {
111 struct vfsconf *vfsp;
112
113 if (!strcmp(name, "ffs"))
114 name = "ufs";
115 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
116 if (!strcmp(name, vfsp->vfc_name))
117 return (vfsp);
118 return (NULL);
119 }
120
121 struct vfsconf *
122 vfs_byname_kld(const char *fstype, struct thread *td, int *error)
123 {
124 struct vfsconf *vfsp;
125 int fileid;
126
127 vfsp = vfs_byname(fstype);
128 if (vfsp != NULL)
129 return (vfsp);
130
131 /* Try to load the respective module. */
132 *error = kern_kldload(td, fstype, &fileid);
133 if (*error)
134 return (NULL);
135
136 /* Look up again to see if the VFS was loaded. */
137 vfsp = vfs_byname(fstype);
138 if (vfsp == NULL) {
139 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
140 *error = ENODEV;
141 return (NULL);
142 }
143 return (vfsp);
144 }
145
146
147 /* Register a new filesystem type in the global table */
148 static int
149 vfs_register(struct vfsconf *vfc)
150 {
151 struct sysctl_oid *oidp;
152 struct vfsops *vfsops;
153 static int once;
154 struct vfsconf *tvfc;
155 uint32_t hashval;
156 int secondpass;
157
158 if (!once) {
159 vattr_null(&va_null);
160 once = 1;
161 }
162
163 if (vfc->vfc_version != VFS_VERSION) {
164 printf("ERROR: filesystem %s, unsupported ABI version %x\n",
165 vfc->vfc_name, vfc->vfc_version);
166 return (EINVAL);
167 }
168 if (vfs_byname(vfc->vfc_name) != NULL)
169 return EEXIST;
170
171 if (vfs_typenumhash != 0) {
172 /*
173 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
174 * all of 1<->255 are assigned, it is limited to 8bits since
175 * that is what ZFS uses from vfc_typenum and is also the
176 * preferred range for vfs_getnewfsid().
177 */
178 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
179 hashval &= 0xff;
180 secondpass = 0;
181 do {
182 /* Look for and fix any collision. */
183 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
184 if (hashval == tvfc->vfc_typenum) {
185 if (hashval == 255 && secondpass == 0) {
186 hashval = 1;
187 secondpass = 1;
188 } else
189 hashval++;
190 break;
191 }
192 }
193 } while (tvfc != NULL);
194 vfc->vfc_typenum = hashval;
195 if (vfc->vfc_typenum >= maxvfsconf)
196 maxvfsconf = vfc->vfc_typenum + 1;
197 } else
198 vfc->vfc_typenum = maxvfsconf++;
199 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
200
201 /*
202 * If this filesystem has a sysctl node under vfs
203 * (i.e. vfs.xxfs), then change the oid number of that node to
204 * match the filesystem's type number. This allows user code
205 * which uses the type number to read sysctl variables defined
206 * by the filesystem to continue working. Since the oids are
207 * in a sorted list, we need to make sure the order is
208 * preserved by re-registering the oid after modifying its
209 * number.
210 */
211 sysctl_lock();
212 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
213 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
214 sysctl_unregister_oid(oidp);
215 oidp->oid_number = vfc->vfc_typenum;
216 sysctl_register_oid(oidp);
217 break;
218 }
219 sysctl_unlock();
220
221 /*
222 * Initialise unused ``struct vfsops'' fields, to use
223 * the vfs_std*() functions. Note, we need the mount
224 * and unmount operations, at the least. The check
225 * for vfsops available is just a debugging aid.
226 */
227 KASSERT(vfc->vfc_vfsops != NULL,
228 ("Filesystem %s has no vfsops", vfc->vfc_name));
229 /*
230 * Check the mount and unmount operations.
231 */
232 vfsops = vfc->vfc_vfsops;
233 KASSERT(vfsops->vfs_mount != NULL,
234 ("Filesystem %s has no mount op", vfc->vfc_name));
235 KASSERT(vfsops->vfs_unmount != NULL,
236 ("Filesystem %s has no unmount op", vfc->vfc_name));
237
238 if (vfsops->vfs_root == NULL)
239 /* return file system's root vnode */
240 vfsops->vfs_root = vfs_stdroot;
241 if (vfsops->vfs_quotactl == NULL)
242 /* quota control */
243 vfsops->vfs_quotactl = vfs_stdquotactl;
244 if (vfsops->vfs_statfs == NULL)
245 /* return file system's status */
246 vfsops->vfs_statfs = vfs_stdstatfs;
247 if (vfsops->vfs_sync == NULL)
248 /*
249 * flush unwritten data (nosync)
250 * file systems can use vfs_stdsync
251 * explicitly by setting it in the
252 * vfsop vector.
253 */
254 vfsops->vfs_sync = vfs_stdnosync;
255 if (vfsops->vfs_vget == NULL)
256 /* convert an inode number to a vnode */
257 vfsops->vfs_vget = vfs_stdvget;
258 if (vfsops->vfs_fhtovp == NULL)
259 /* turn an NFS file handle into a vnode */
260 vfsops->vfs_fhtovp = vfs_stdfhtovp;
261 if (vfsops->vfs_checkexp == NULL)
262 /* check if file system is exported */
263 vfsops->vfs_checkexp = vfs_stdcheckexp;
264 if (vfsops->vfs_init == NULL)
265 /* file system specific initialisation */
266 vfsops->vfs_init = vfs_stdinit;
267 if (vfsops->vfs_uninit == NULL)
268 /* file system specific uninitialisation */
269 vfsops->vfs_uninit = vfs_stduninit;
270 if (vfsops->vfs_extattrctl == NULL)
271 /* extended attribute control */
272 vfsops->vfs_extattrctl = vfs_stdextattrctl;
273 if (vfsops->vfs_sysctl == NULL)
274 vfsops->vfs_sysctl = vfs_stdsysctl;
275
276 /*
277 * Call init function for this VFS...
278 */
279 (*(vfc->vfc_vfsops->vfs_init))(vfc);
280
281 return 0;
282 }
283
284
285 /* Remove registration of a filesystem type */
286 static int
287 vfs_unregister(struct vfsconf *vfc)
288 {
289 struct vfsconf *vfsp;
290 int error, i, maxtypenum;
291
292 i = vfc->vfc_typenum;
293
294 vfsp = vfs_byname(vfc->vfc_name);
295 if (vfsp == NULL)
296 return EINVAL;
297 if (vfsp->vfc_refcount)
298 return EBUSY;
299 if (vfc->vfc_vfsops->vfs_uninit != NULL) {
300 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
301 if (error)
302 return (error);
303 }
304 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
305 maxtypenum = VFS_GENERIC;
306 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
307 if (maxtypenum < vfsp->vfc_typenum)
308 maxtypenum = vfsp->vfc_typenum;
309 maxvfsconf = maxtypenum + 1;
310 return 0;
311 }
312
313 /*
314 * Standard kernel module handling code for filesystem modules.
315 * Referenced from VFS_SET().
316 */
317 int
318 vfs_modevent(module_t mod, int type, void *data)
319 {
320 struct vfsconf *vfc;
321 int error = 0;
322
323 vfc = (struct vfsconf *)data;
324
325 switch (type) {
326 case MOD_LOAD:
327 if (vfc)
328 error = vfs_register(vfc);
329 break;
330
331 case MOD_UNLOAD:
332 if (vfc)
333 error = vfs_unregister(vfc);
334 break;
335 default:
336 error = EOPNOTSUPP;
337 break;
338 }
339 return (error);
340 }
Cache object: ad01c94c3b3cf435b1e11e4e00b6a155
|