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