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