VFS messaging/interfacing work stage 2/99. This stage retools the vnode ops
[dragonfly.git] / sys / kern / vfs_init.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *
35  * Copyright (c) 1989, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * This code is derived from software contributed
39  * to Berkeley by John Heidemann of the UCLA Ficus project.
40  *
41  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *      This product includes software developed by the University of
54  *      California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *      @(#)vfs_init.c  8.3 (Berkeley) 1/4/94
72  * $FreeBSD: src/sys/kern/vfs_init.c,v 1.59 2002/04/30 18:44:32 dillon Exp $
73  * $DragonFly: src/sys/kern/vfs_init.c,v 1.6 2004/08/17 18:57:32 dillon Exp $
74  */
75 /*
76  * Manage vnode VOP operations vectors
77  */
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
81 #include <sys/mount.h>
82 #include <sys/sysctl.h>
83 #include <sys/vnode.h>
84 #include <sys/malloc.h>
85 #include <vm/vm_zone.h>
86
87 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
88
89 /*
90  * Zone for namei
91  */
92 struct vm_zone *namei_zone;
93
94 /*
95  * vfs_init() will set maxvfsconf
96  * to the highest defined type number.
97  */
98 int maxvfsconf;
99 struct vfsconf *vfsconf;
100
101 static TAILQ_HEAD(, vnodeopv_node) vnodeopv_list;
102 static void vfs_recalc_vnodeops(void);
103
104 /*
105  * Add a vnode operations (vnops) vector to the global list.
106  */
107 void
108 vfs_add_vnodeops_sysinit(const void *data)
109 {
110         const struct vnodeopv_desc *vdesc = data;
111
112         vfs_add_vnodeops(vdesc->opv_desc_vector, vdesc->opv_desc_ops);
113 }
114
115 /*
116  * Unlink previously added vnode operations vector.
117  */
118 void
119 vfs_rm_vnodeops_sysinit(const void *data)
120 {
121         const struct vnodeopv_desc *vdesc = data;
122
123         vfs_rm_vnodeops(vdesc->opv_desc_vector);
124 }
125
126 void
127 vfs_add_vnodeops(struct vop_ops **vops_pp, struct vnodeopv_entry_desc *descs)
128 {
129         struct vnodeopv_node *node;
130         struct vop_ops *ops;
131
132         node = malloc(sizeof(*node), M_VNODE, M_ZERO|M_WAITOK);
133         if ((ops = *vops_pp) == NULL) {
134                 ops = malloc(sizeof(struct vop_ops), M_VNODE, M_ZERO|M_WAITOK);
135                 *vops_pp = ops;
136         }
137         node->ops = ops;
138         node->descs = descs;
139         ++ops->vv_refs;
140         TAILQ_INSERT_TAIL(&vnodeopv_list, node, entry);
141         vfs_recalc_vnodeops();
142 }
143
144 void
145 vfs_rm_vnodeops(struct vop_ops **vops_pp)
146 {
147         struct vop_ops *ops = *vops_pp;
148         struct vnodeopv_node *node;
149
150         if (ops == NULL)
151                 return;
152
153         TAILQ_FOREACH(node, &vnodeopv_list, entry) {
154                 if (node->ops == ops)
155                         break;
156         }
157         if (node == NULL) {
158                 printf("vfs_rm_vnodeops: unable to find ops: %p\n", ops);
159                 return;
160         }
161         TAILQ_REMOVE(&vnodeopv_list, node, entry);
162         free(node, M_VNODE);
163         KKASSERT(ops != NULL && ops->vv_refs > 0);
164         if (--ops->vv_refs == 0) {
165                 *vops_pp = NULL;
166                 free(ops, M_VNODE);
167         }
168         vfs_recalc_vnodeops();
169 }
170
171 /*
172  * Recalculate VFS operations vectors
173  */
174 static void
175 vfs_recalc_vnodeops(void)
176 {
177         struct vnodeopv_node *node;
178         struct vnodeopv_entry_desc *desc;
179         struct vop_ops *ops;
180         struct vop_ops *vnew;
181         int off;
182
183         /*
184          * Because vop_ops may be active we can't just blow them away, we
185          * have to generate new vop_ops and then copy them into the running
186          * vop_ops.  Any missing entries will be assigned to the default
187          * entry.  If the default entry itself is missing it will be assigned
188          * to vop_eopnotsupp.
189          */
190         TAILQ_FOREACH(node, &vnodeopv_list, entry) {
191                 ops = node->ops;
192                 if ((vnew = ops->vv_new) == NULL) {
193                         vnew = malloc(sizeof(struct vop_ops),
194                                         M_VNODE, M_ZERO|M_WAITOK);
195                         ops->vv_new = vnew;
196                         vnew->vop_default = vop_eopnotsupp;
197                 }
198                 for (desc = node->descs; desc->opve_op; ++desc) {
199                         off = desc->opve_op->vdesc_offset;
200                         *(void **)((char *)vnew + off) = desc->opve_func;
201                 }
202                 for (off = __offsetof(struct vop_ops, vop_ops_first_field);
203                      off <= __offsetof(struct vop_ops, vop_ops_last_field);
204                      off += sizeof(void **)
205                 ) {
206                         if (*(void **)((char *)vnew + off) == NULL)
207                             *(void **)((char *)vnew + off) = vnew->vop_default;
208                 }
209         }
210
211         /*
212          * Copy the temporary ops into the running configuration and then
213          * delete them.
214          */
215         TAILQ_FOREACH(node, &vnodeopv_list, entry) {
216                 ops = node->ops;
217                 if ((vnew = ops->vv_new) == NULL)
218                         continue;
219                 for (off = __offsetof(struct vop_ops, vop_ops_first_field);
220                      off <= __offsetof(struct vop_ops, vop_ops_last_field);
221                      off += sizeof(void **)
222                 ) {
223                         *(void **)((char *)ops + off) = 
224                                 *(void **)((char *)vnew + off);
225                 }
226                 ops->vv_new = NULL;
227                 free(vnew, M_VNODE);
228         }
229 }
230
231 /*
232  * Routines having to do with the management of the vnode table.
233  */
234 struct vattr va_null;
235
236 /*
237  * Initialize the vnode structures and initialize each file system type.
238  */
239 /* ARGSUSED*/
240 static void
241 vfsinit(void *dummy)
242 {
243         TAILQ_INIT(&vnodeopv_list);
244         namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
245
246         /*
247          * Initialize the vnode table
248          */
249         vntblinit();
250         /*
251          * Initialize the vnode name cache
252          */
253         nchinit();
254         /*
255          * Initialize each file system type.
256          * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF).
257          */
258         vattr_null(&va_null);
259         maxvfsconf = VFS_GENERIC + 1;
260 }
261 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
262
263 /*
264  * Register a VFS.
265  *
266  * After doing general initialisation, this function will
267  * call the filesystem specific initialisation vector op,
268  * i.e. vfsops->vfs_init().
269  */
270 int
271 vfs_register(struct vfsconf *vfc)
272 {
273         struct sysctl_oid *oidp;
274         struct vfsconf *vfsp;
275
276         vfsp = NULL;
277         if (vfsconf)
278                 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
279                         if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
280                                 return EEXIST;
281
282         vfc->vfc_typenum = maxvfsconf++;
283         if (vfsp)
284                 vfsp->vfc_next = vfc;
285         else
286                 vfsconf = vfc;
287         vfc->vfc_next = NULL;
288
289         /*
290          * If this filesystem has a sysctl node under vfs
291          * (i.e. vfs.xxfs), then change the oid number of that node to 
292          * match the filesystem's type number.  This allows user code
293          * which uses the type number to read sysctl variables defined
294          * by the filesystem to continue working. Since the oids are
295          * in a sorted list, we need to make sure the order is
296          * preserved by re-registering the oid after modifying its
297          * number.
298          */
299         SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
300                 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
301                         sysctl_unregister_oid(oidp);
302                         oidp->oid_number = vfc->vfc_typenum;
303                         sysctl_register_oid(oidp);
304                 }
305
306         /*
307          * Call init function for this VFS...
308          */
309         (*(vfc->vfc_vfsops->vfs_init))(vfc);
310
311         return 0;
312 }
313
314
315 /*
316  * Remove previously registered VFS.
317  *
318  * After doing general de-registration like removing sysctl
319  * nodes etc, it will call the filesystem specific vector
320  * op, i.e. vfsops->vfs_uninit().
321  * 
322  */
323 int
324 vfs_unregister(struct vfsconf *vfc)
325 {
326         struct vfsconf *vfsp, *prev_vfsp;
327         int error, i, maxtypenum;
328
329         i = vfc->vfc_typenum;
330
331         prev_vfsp = NULL;
332         for (vfsp = vfsconf; vfsp;
333                         prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
334                 if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
335                         break;
336         }
337         if (vfsp == NULL)
338                 return EINVAL;
339         if (vfsp->vfc_refcount)
340                 return EBUSY;
341         if (vfc->vfc_vfsops->vfs_uninit != NULL) {
342                 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
343                 if (error)
344                         return (error);
345         }
346         if (prev_vfsp)
347                 prev_vfsp->vfc_next = vfsp->vfc_next;
348         else
349                 vfsconf = vfsp->vfc_next;
350         maxtypenum = VFS_GENERIC;
351         for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
352                 if (maxtypenum < vfsp->vfc_typenum)
353                         maxtypenum = vfsp->vfc_typenum;
354         maxvfsconf = maxtypenum + 1;
355         return 0;
356 }
357
358 int
359 vfs_modevent(module_t mod, int type, void *data)
360 {
361         struct vfsconf *vfc;
362         int error = 0;
363
364         vfc = (struct vfsconf *)data;
365
366         switch (type) {
367         case MOD_LOAD:
368                 if (vfc)
369                         error = vfs_register(vfc);
370                 break;
371
372         case MOD_UNLOAD:
373                 if (vfc)
374                         error = vfs_unregister(vfc);
375                 break;
376         default:        /* including MOD_SHUTDOWN */
377                 break;
378         }
379         return (error);
380 }