Do not free the old VFS vector when recomputing the vectors. If a module
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)vfs_init.c  8.3 (Berkeley) 1/4/94
39  * $FreeBSD: src/sys/kern/vfs_init.c,v 1.49 1999/12/12 16:30:34 peter Exp $
40  * $DragonFly: src/sys/kern/vfs_init.c,v 1.3 2004/03/15 06:10:51 dillon Exp $
41  */
42
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mount.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/malloc.h>
51 #include <vm/vm_zone.h>
52
53
54 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
55
56 /*
57  * Zone for namei
58  */
59 struct vm_zone *namei_zone;
60
61 /*
62  * vfs_init() will set maxvfsconf
63  * to the highest defined type number.
64  */
65 int maxvfsconf;
66 struct vfsconf *vfsconf;
67
68 /*
69  * vfs_init.c
70  *
71  * Allocate and fill in operations vectors.
72  *
73  * An undocumented feature of this approach to defining operations is that
74  * there can be multiple entries in vfs_opv_descs for the same operations
75  * vector. This allows third parties to extend the set of operations
76  * supported by another layer in a binary compatibile way. For example,
77  * assume that NFS needed to be modified to support Ficus. NFS has an entry
78  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
79  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
80  * listing those new operations Ficus adds to NFS, all without modifying the
81  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
82  * that is a(whole)nother story.) This is a feature.
83  */
84
85 /* Table of known vnodeop vectors (list of VFS vnode vectors) */
86 static const struct vnodeopv_desc **vnodeopv_descs;
87 static int vnodeopv_num;
88
89 /* Table of known descs (list of vnode op handlers "vop_access_desc") */
90 static struct vnodeop_desc **vfs_op_descs;
91 static int *vfs_op_desc_refs;                   /* reference counts */
92 static int num_op_descs;
93 static int vfs_opv_numops;
94
95 static void
96 vfs_opv_recalc(void)
97 {
98         int i, j;
99         vop_t ***opv_desc_vector_p;
100         vop_t **opv_desc_vector;
101         struct vnodeopv_entry_desc *opve_descp;
102         const struct vnodeopv_desc *opv;
103
104         if (vfs_op_descs == NULL)
105                 panic("vfs_opv_recalc called with null vfs_op_descs");
106
107         /*
108          * Run through and make sure all known descs have an offset
109          *
110          * vop_default_desc is hardwired at offset 1, and offset 0
111          * is a panic sanity check.
112          */
113         vfs_opv_numops = 0;
114         for (i = 0; i < num_op_descs; i++)
115                 if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1))
116                         vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1;
117         for (i = 0; i < num_op_descs; i++)
118                 if (vfs_op_descs[i]->vdesc_offset == 0)
119                         vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++;
120         /*
121          * Allocate and fill in the vectors
122          */
123         for (i = 0; i < vnodeopv_num; i++) {
124                 opv = vnodeopv_descs[i];
125                 opv_desc_vector_p = opv->opv_desc_vector_p;
126 #if 0
127                 /* old vector may be in-use by vnodes */
128                 /* XXX fixme, pre-allocate enough vectors? */
129                 if (*opv_desc_vector_p)
130                         FREE(*opv_desc_vector_p, M_VNODE);
131 #endif
132                 MALLOC(*opv_desc_vector_p, vop_t **,
133                        vfs_opv_numops * sizeof(vop_t *), M_VNODE, M_WAITOK);
134                 if (*opv_desc_vector_p == NULL)
135                         panic("no memory for vop_t ** vector");
136                 bzero(*opv_desc_vector_p, vfs_opv_numops * sizeof(vop_t *));
137
138                 /* Fill in, with slot 0 being panic */
139                 opv_desc_vector = *opv_desc_vector_p;
140                 opv_desc_vector[0] = (vop_t *)vop_panic;
141                 for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
142                         opve_descp = &(opv->opv_desc_ops[j]);
143                         opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
144                                 opve_descp->opve_impl;
145                 }
146
147                 /* Replace unfilled routines with their default (slot 1). */
148                 opv_desc_vector = *(opv->opv_desc_vector_p);
149                 if (opv_desc_vector[1] == NULL)
150                         panic("vfs_opv_recalc: vector without a default.");
151                 for (j = 0; j < vfs_opv_numops; j++)
152                         if (opv_desc_vector[j] == NULL)
153                                 opv_desc_vector[j] = opv_desc_vector[1];
154         }
155 }
156
157 void
158 vfs_add_vnodeops(const void *data)
159 {
160         const struct vnodeopv_desc *opv;
161         const struct vnodeopv_desc **newopv;
162         struct vnodeop_desc **newop;
163         int *newref;
164         vop_t **opv_desc_vector;
165         struct vnodeop_desc *desc;
166         int i, j;
167
168         opv = (const struct vnodeopv_desc *)data;
169         MALLOC(newopv, const struct vnodeopv_desc **,
170                (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
171         if (newopv == NULL)
172                 panic("vfs_add_vnodeops: no memory");
173         if (vnodeopv_descs) {
174                 bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
175                 FREE(vnodeopv_descs, M_VNODE);
176         }
177         newopv[vnodeopv_num] = opv;
178         vnodeopv_descs = newopv;
179         vnodeopv_num++;
180
181         /* See if we have turned up a new vnode op desc */
182         opv_desc_vector = *(opv->opv_desc_vector_p);
183         for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
184                 for (j = 0; j < num_op_descs; j++) {
185                         if (desc == vfs_op_descs[j]) {
186                                 /* found it, increase reference count */
187                                 vfs_op_desc_refs[j]++;
188                                 break;
189                         }
190                 }
191                 if (j == num_op_descs) {
192                         /* not found, new entry */
193                         MALLOC(newop, struct vnodeop_desc **,
194                                (num_op_descs + 1) * sizeof(*newop),
195                                M_VNODE, M_WAITOK);
196                         if (newop == NULL)
197                                 panic("vfs_add_vnodeops: no memory for desc");
198                         /* new reference count (for unload) */
199                         MALLOC(newref, int *,
200                                 (num_op_descs + 1) * sizeof(*newref),
201                                 M_VNODE, M_WAITOK);
202                         if (newref == NULL)
203                                 panic("vfs_add_vnodeops: no memory for refs");
204                         if (vfs_op_descs) {
205                                 bcopy(vfs_op_descs, newop,
206                                         num_op_descs * sizeof(*newop));
207                                 FREE(vfs_op_descs, M_VNODE);
208                         }
209                         if (vfs_op_desc_refs) {
210                                 bcopy(vfs_op_desc_refs, newref,
211                                         num_op_descs * sizeof(*newref));
212                                 FREE(vfs_op_desc_refs, M_VNODE);
213                         }
214                         newop[num_op_descs] = desc;
215                         newref[num_op_descs] = 1;
216                         vfs_op_descs = newop;
217                         vfs_op_desc_refs = newref;
218                         num_op_descs++;
219                 }
220         }
221         vfs_opv_recalc();
222 }
223
224 void
225 vfs_rm_vnodeops(const void *data)
226 {
227         const struct vnodeopv_desc *opv;
228         const struct vnodeopv_desc **newopv;
229         struct vnodeop_desc **newop;
230         int *newref;
231         vop_t **opv_desc_vector;
232         struct vnodeop_desc *desc;
233         int i, j, k;
234
235         opv = (const struct vnodeopv_desc *)data;
236         /* Lower ref counts on descs in the table and release if zero */
237         opv_desc_vector = *(opv->opv_desc_vector_p);
238         for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
239                 for (j = 0; j < num_op_descs; j++) {
240                         if (desc == vfs_op_descs[j]) {
241                                 /* found it, decrease reference count */
242                                 vfs_op_desc_refs[j]--;
243                                 break;
244                         }
245                 }
246                 for (j = 0; j < num_op_descs; j++) {
247                         if (vfs_op_desc_refs[j] > 0)
248                                 continue;
249                         if (vfs_op_desc_refs[j] < 0)
250                                 panic("vfs_remove_vnodeops: negative refcnt");
251                         MALLOC(newop, struct vnodeop_desc **,
252                                (num_op_descs - 1) * sizeof(*newop),
253                                M_VNODE, M_WAITOK);
254                         if (newop == NULL)
255                                 panic("vfs_remove_vnodeops: no memory for desc");
256                         /* new reference count (for unload) */
257                         MALLOC(newref, int *,
258                                 (num_op_descs - 1) * sizeof(*newref),
259                                 M_VNODE, M_WAITOK);
260                         if (newref == NULL)
261                                 panic("vfs_remove_vnodeops: no memory for refs");
262                         for (k = j; k < (num_op_descs - 1); k++) {
263                                 vfs_op_descs[k] = vfs_op_descs[k + 1];
264                                 vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
265                         }
266                         bcopy(vfs_op_descs, newop,
267                                 (num_op_descs - 1) * sizeof(*newop));
268                         bcopy(vfs_op_desc_refs, newref,
269                                 (num_op_descs - 1) * sizeof(*newref));
270                         FREE(vfs_op_descs, M_VNODE);
271                         FREE(vfs_op_desc_refs, M_VNODE);
272                         vfs_op_descs = newop;
273                         vfs_op_desc_refs = newref;
274                         num_op_descs--;
275                 }
276         }
277
278         for (i = 0; i < vnodeopv_num; i++) {
279                 if (vnodeopv_descs[i] == opv) {
280                         for (j = i; j < (vnodeopv_num - 1); j++)
281                                 vnodeopv_descs[j] = vnodeopv_descs[j + 1];
282                         break;
283                 }
284         }
285         if (i == vnodeopv_num)
286                 panic("vfs_remove_vnodeops: opv not found");
287         MALLOC(newopv, const struct vnodeopv_desc **,
288                (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
289         if (newopv == NULL)
290                 panic("vfs_remove_vnodeops: no memory");
291         bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
292         FREE(vnodeopv_descs, M_VNODE);
293         vnodeopv_descs = newopv;
294         vnodeopv_num--;
295
296         vfs_opv_recalc();
297 }
298
299 /*
300  * Routines having to do with the management of the vnode table.
301  */
302 struct vattr va_null;
303
304 /*
305  * Initialize the vnode structures and initialize each file system type.
306  */
307 /* ARGSUSED*/
308 static void
309 vfsinit(void *dummy)
310 {
311
312         namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
313
314         /*
315          * Initialize the vnode table
316          */
317         vntblinit();
318         /*
319          * Initialize the vnode name cache
320          */
321         nchinit();
322         /*
323          * Initialize each file system type.
324          * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF).
325          */
326         vattr_null(&va_null);
327         maxvfsconf = VFS_GENERIC + 1;
328 }
329 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
330
331 int
332 vfs_register(struct vfsconf *vfc)
333 {
334         struct sysctl_oid *oidp;
335         struct vfsconf *vfsp;
336
337         vfsp = NULL;
338         if (vfsconf)
339                 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
340                         if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
341                                 return EEXIST;
342
343         vfc->vfc_typenum = maxvfsconf++;
344         if (vfsp)
345                 vfsp->vfc_next = vfc;
346         else
347                 vfsconf = vfc;
348         vfc->vfc_next = NULL;
349
350         /*
351          * If this filesystem has a sysctl node under vfs
352          * (i.e. vfs.xxfs), then change the oid number of that node to 
353          * match the filesystem's type number.  This allows user code
354          * which uses the type number to read sysctl variables defined
355          * by the filesystem to continue working. Since the oids are
356          * in a sorted list, we need to make sure the order is
357          * preserved by re-registering the oid after modifying its
358          * number.
359          */
360         for (oidp = SLIST_FIRST(&sysctl__vfs_children); oidp;
361              oidp = SLIST_NEXT(oidp, oid_link))
362                 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
363                         sysctl_unregister_oid(oidp);
364                         oidp->oid_number = vfc->vfc_typenum;
365                         sysctl_register_oid(oidp);
366                 }
367
368         /*
369          * Call init function for this VFS...
370          */
371         (*(vfc->vfc_vfsops->vfs_init))(vfc);
372
373         return 0;
374 }
375
376
377 int
378 vfs_unregister(struct vfsconf *vfc)
379 {
380         struct vfsconf *vfsp, *prev_vfsp;
381         int error, i, maxtypenum;
382
383         i = vfc->vfc_typenum;
384
385         prev_vfsp = NULL;
386         for (vfsp = vfsconf; vfsp;
387                         prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
388                 if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
389                         break;
390         }
391         if (vfsp == NULL)
392                 return EINVAL;
393         if (vfsp->vfc_refcount)
394                 return EBUSY;
395         if (vfc->vfc_vfsops->vfs_uninit != NULL) {
396                 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
397                 if (error)
398                         return (error);
399         }
400         if (prev_vfsp)
401                 prev_vfsp->vfc_next = vfsp->vfc_next;
402         else
403                 vfsconf = vfsp->vfc_next;
404         maxtypenum = VFS_GENERIC;
405         for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
406                 if (maxtypenum < vfsp->vfc_typenum)
407                         maxtypenum = vfsp->vfc_typenum;
408         maxvfsconf = maxtypenum + 1;
409         return 0;
410 }
411
412 int
413 vfs_modevent(module_t mod, int type, void *data)
414 {
415         struct vfsconf *vfc;
416         int error = 0;
417
418         vfc = (struct vfsconf *)data;
419
420         switch (type) {
421         case MOD_LOAD:
422                 if (vfc)
423                         error = vfs_register(vfc);
424                 break;
425
426         case MOD_UNLOAD:
427                 if (vfc)
428                         error = vfs_unregister(vfc);
429                 break;
430         default:        /* including MOD_SHUTDOWN */
431                 break;
432         }
433         return (error);
434 }