]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/kern/vfs_init.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / 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_root == NULL)
201                 /* return file system's root vnode */
202                 vfsops->vfs_root =      vfs_stdroot;
203         if (vfsops->vfs_quotactl == NULL)
204                 /* quota control */
205                 vfsops->vfs_quotactl =  vfs_stdquotactl;
206         if (vfsops->vfs_statfs == NULL)
207                 /* return file system's status */
208                 vfsops->vfs_statfs =    vfs_stdstatfs;
209         if (vfsops->vfs_sync == NULL)
210                 /*
211                  * flush unwritten data (nosync)
212                  * file systems can use vfs_stdsync
213                  * explicitly by setting it in the
214                  * vfsop vector.
215                  */
216                 vfsops->vfs_sync =      vfs_stdnosync;
217         if (vfsops->vfs_vget == NULL)
218                 /* convert an inode number to a vnode */
219                 vfsops->vfs_vget =      vfs_stdvget;
220         if (vfsops->vfs_fhtovp == NULL)
221                 /* turn an NFS file handle into a vnode */
222                 vfsops->vfs_fhtovp =    vfs_stdfhtovp;
223         if (vfsops->vfs_checkexp == NULL)
224                 /* check if file system is exported */
225                 vfsops->vfs_checkexp =  vfs_stdcheckexp;
226         if (vfsops->vfs_vptofh == NULL)
227                 /* turn a vnode into an NFS file handle */
228                 vfsops->vfs_vptofh =    vfs_stdvptofh;
229         if (vfsops->vfs_init == NULL)
230                 /* file system specific initialisation */
231                 vfsops->vfs_init =      vfs_stdinit;
232         if (vfsops->vfs_uninit == NULL)
233                 /* file system specific uninitialisation */
234                 vfsops->vfs_uninit =    vfs_stduninit;
235         if (vfsops->vfs_extattrctl == NULL)
236                 /* extended attribute control */
237                 vfsops->vfs_extattrctl = vfs_stdextattrctl;
238         if (vfsops->vfs_sysctl == NULL)
239                 vfsops->vfs_sysctl = vfs_stdsysctl;
240         
241         /*
242          * Call init function for this VFS...
243          */
244         (*(vfc->vfc_vfsops->vfs_init))(vfc);
245
246         return 0;
247 }
248
249
250 /* Remove registration of a filesystem type */
251 static int
252 vfs_unregister(struct vfsconf *vfc)
253 {
254         struct vfsconf *vfsp;
255         int error, i, maxtypenum;
256
257         i = vfc->vfc_typenum;
258
259         vfsp = vfs_byname(vfc->vfc_name);
260         if (vfsp == NULL)
261                 return EINVAL;
262         if (vfsp->vfc_refcount)
263                 return EBUSY;
264         if (vfc->vfc_vfsops->vfs_uninit != NULL) {
265                 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
266                 if (error)
267                         return (error);
268         }
269         TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
270         maxtypenum = VFS_GENERIC;
271         TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
272                 if (maxtypenum < vfsp->vfc_typenum)
273                         maxtypenum = vfsp->vfc_typenum;
274         maxvfsconf = maxtypenum + 1;
275         return 0;
276 }
277
278 /*
279  * Standard kernel module handling code for filesystem modules.
280  * Referenced from VFS_SET().
281  */
282 int
283 vfs_modevent(module_t mod, int type, void *data)
284 {
285         struct vfsconf *vfc;
286         int error = 0;
287
288         vfc = (struct vfsconf *)data;
289
290         switch (type) {
291         case MOD_LOAD:
292                 if (vfc)
293                         error = vfs_register(vfc);
294                 break;
295
296         case MOD_UNLOAD:
297                 if (vfc)
298                         error = vfs_unregister(vfc);
299                 break;
300         default:
301                 error = EOPNOTSUPP;
302                 break;
303         }
304         return (error);
305 }