]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_init.c
Fix some unused variables.
[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  * 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$
40  */
41
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/sysctl.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <vm/vm_zone.h>
51
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 vfsconf *vfsconf;
65
66 /*
67  * vfs_init.c
68  *
69  * Allocate and fill in operations vectors.
70  *
71  * An undocumented feature of this approach to defining operations is that
72  * there can be multiple entries in vfs_opv_descs for the same operations
73  * vector. This allows third parties to extend the set of operations
74  * supported by another layer in a binary compatibile way. For example,
75  * assume that NFS needed to be modified to support Ficus. NFS has an entry
76  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
77  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
78  * listing those new operations Ficus adds to NFS, all without modifying the
79  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
80  * that is a(whole)nother story.) This is a feature.
81  */
82
83 /* Table of known vnodeop vectors (list of VFS vnode vectors) */
84 static const struct vnodeopv_desc **vnodeopv_descs;
85 static int vnodeopv_num;
86
87 /* Table of known descs (list of vnode op handlers "vop_access_desc") */
88 static struct vnodeop_desc **vfs_op_descs;
89 /* Reference counts for vfs_op_descs */
90 static int *vfs_op_desc_refs;
91 /* Number of descriptions */
92 static int num_op_descs;
93 /* Number of entries in each description */
94 static int vfs_opv_numops;
95
96 /*
97  * Recalculate the operations vector/description (those parts of it that can
98  * be recalculated, that is.)
99  * XXX It may be preferable to replace this function with an invariant check
100  * and a set of functions that should keep the table invariant.
101  */
102 static void
103 vfs_opv_recalc(void)
104 {
105         int i, j;
106         vop_t ***opv_desc_vector_p;
107         vop_t **opv_desc_vector;
108         struct vnodeopv_entry_desc *opve_descp;
109         const struct vnodeopv_desc *opv;
110
111         if (vfs_op_descs == NULL)
112                 panic("vfs_opv_recalc called with null vfs_op_descs");
113
114         /*
115          * Run through and make sure all known descs have an offset
116          *
117          * vop_default_desc is hardwired at offset 1, and offset 0
118          * is a panic sanity check.
119          */
120         vfs_opv_numops = 0;
121         for (i = 0; i < num_op_descs; i++)
122                 if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1))
123                         vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1;
124         for (i = 0; i < num_op_descs; i++)
125                 if (vfs_op_descs[i]->vdesc_offset == 0)
126                         vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++;
127         /*
128          * Allocate and fill in the vectors
129          */
130         for (i = 0; i < vnodeopv_num; i++) {
131                 opv = vnodeopv_descs[i];
132                 opv_desc_vector_p = opv->opv_desc_vector_p;
133                 if (*opv_desc_vector_p)
134                         FREE(*opv_desc_vector_p, M_VNODE);
135                 MALLOC(*opv_desc_vector_p, vop_t **,
136                         vfs_opv_numops * sizeof(vop_t *), M_VNODE,
137                         M_WAITOK | M_ZERO);
138                 if (*opv_desc_vector_p == NULL)
139                         panic("no memory for vop_t ** vector");
140
141                 /* Fill in, with slot 0 being to return EOPNOTSUPP */
142                 opv_desc_vector = *opv_desc_vector_p;
143                 opv_desc_vector[0] = (vop_t *)vop_eopnotsupp;
144                 for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
145                         opve_descp = &(opv->opv_desc_ops[j]);
146                         opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
147                                 opve_descp->opve_impl;
148                 }
149
150                 /* Replace unfilled routines with their default (slot 1). */
151                 opv_desc_vector = *(opv->opv_desc_vector_p);
152                 if (opv_desc_vector[1] == NULL)
153                         panic("vfs_opv_recalc: vector without a default.");
154                 for (j = 0; j < vfs_opv_numops; j++)
155                         if (opv_desc_vector[j] == NULL)
156                                 opv_desc_vector[j] = opv_desc_vector[1];
157         }
158 }
159
160 /* Add a set of vnode operations (a description) to the table above. */
161 void
162 vfs_add_vnodeops(const void *data)
163 {
164         const struct vnodeopv_desc *opv;
165         const struct vnodeopv_desc **newopv;
166         struct vnodeop_desc **newop;
167         int *newref;
168         vop_t **opv_desc_vector;
169         struct vnodeop_desc *desc;
170         int i, j;
171
172         opv = (const struct vnodeopv_desc *)data;
173         MALLOC(newopv, const struct vnodeopv_desc **,
174                (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
175         if (newopv == NULL)
176                 panic("vfs_add_vnodeops: no memory");
177         if (vnodeopv_descs) {
178                 bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
179                 FREE(vnodeopv_descs, M_VNODE);
180         }
181         newopv[vnodeopv_num] = opv;
182         vnodeopv_descs = newopv;
183         vnodeopv_num++;
184
185         /* See if we have turned up a new vnode op desc */
186         opv_desc_vector = *(opv->opv_desc_vector_p);
187         for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
188                 for (j = 0; j < num_op_descs; j++) {
189                         if (desc == vfs_op_descs[j]) {
190                                 /* found it, increase reference count */
191                                 vfs_op_desc_refs[j]++;
192                                 break;
193                         }
194                 }
195                 if (j == num_op_descs) {
196                         /* not found, new entry */
197                         MALLOC(newop, struct vnodeop_desc **,
198                                (num_op_descs + 1) * sizeof(*newop),
199                                M_VNODE, M_WAITOK);
200                         if (newop == NULL)
201                                 panic("vfs_add_vnodeops: no memory for desc");
202                         /* new reference count (for unload) */
203                         MALLOC(newref, int *,
204                                 (num_op_descs + 1) * sizeof(*newref),
205                                 M_VNODE, M_WAITOK);
206                         if (newref == NULL)
207                                 panic("vfs_add_vnodeops: no memory for refs");
208                         if (vfs_op_descs) {
209                                 bcopy(vfs_op_descs, newop,
210                                         num_op_descs * sizeof(*newop));
211                                 FREE(vfs_op_descs, M_VNODE);
212                         }
213                         if (vfs_op_desc_refs) {
214                                 bcopy(vfs_op_desc_refs, newref,
215                                         num_op_descs * sizeof(*newref));
216                                 FREE(vfs_op_desc_refs, M_VNODE);
217                         }
218                         newop[num_op_descs] = desc;
219                         newref[num_op_descs] = 1;
220                         vfs_op_descs = newop;
221                         vfs_op_desc_refs = newref;
222                         num_op_descs++;
223                 }
224         }
225         vfs_opv_recalc();
226 }
227
228 /* Remove a vnode type from the vnode description table above. */
229 void
230 vfs_rm_vnodeops(const void *data)
231 {
232         const struct vnodeopv_desc *opv;
233         const struct vnodeopv_desc **newopv;
234         struct vnodeop_desc **newop;
235         int *newref;
236         vop_t **opv_desc_vector;
237         struct vnodeop_desc *desc;
238         int i, j, k;
239
240         opv = (const struct vnodeopv_desc *)data;
241         /* Lower ref counts on descs in the table and release if zero */
242         opv_desc_vector = *(opv->opv_desc_vector_p);
243         for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
244                 for (j = 0; j < num_op_descs; j++) {
245                         if (desc == vfs_op_descs[j]) {
246                                 /* found it, decrease reference count */
247                                 vfs_op_desc_refs[j]--;
248                                 break;
249                         }
250                 }
251                 for (j = 0; j < num_op_descs; j++) {
252                         if (vfs_op_desc_refs[j] > 0)
253                                 continue;
254                         if (vfs_op_desc_refs[j] < 0)
255                                 panic("vfs_remove_vnodeops: negative refcnt");
256                         MALLOC(newop, struct vnodeop_desc **,
257                                (num_op_descs - 1) * sizeof(*newop),
258                                M_VNODE, M_WAITOK);
259                         if (newop == NULL)
260                                 panic("vfs_remove_vnodeops: no memory for desc");
261                         /* new reference count (for unload) */
262                         MALLOC(newref, int *,
263                                 (num_op_descs - 1) * sizeof(*newref),
264                                 M_VNODE, M_WAITOK);
265                         if (newref == NULL)
266                                 panic("vfs_remove_vnodeops: no memory for refs");
267                         for (k = j; k < (num_op_descs - 1); k++) {
268                                 vfs_op_descs[k] = vfs_op_descs[k + 1];
269                                 vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
270                         }
271                         bcopy(vfs_op_descs, newop,
272                                 (num_op_descs - 1) * sizeof(*newop));
273                         bcopy(vfs_op_desc_refs, newref,
274                                 (num_op_descs - 1) * sizeof(*newref));
275                         FREE(vfs_op_descs, M_VNODE);
276                         FREE(vfs_op_desc_refs, M_VNODE);
277                         vfs_op_descs = newop;
278                         vfs_op_desc_refs = newref;
279                         num_op_descs--;
280                 }
281         }
282
283         for (i = 0; i < vnodeopv_num; i++) {
284                 if (vnodeopv_descs[i] == opv) {
285                         for (j = i; j < (vnodeopv_num - 1); j++)
286                                 vnodeopv_descs[j] = vnodeopv_descs[j + 1];
287                         break;
288                 }
289         }
290         if (i == vnodeopv_num)
291                 panic("vfs_remove_vnodeops: opv not found");
292         MALLOC(newopv, const struct vnodeopv_desc **,
293                (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
294         if (newopv == NULL)
295                 panic("vfs_remove_vnodeops: no memory");
296         bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
297         FREE(vnodeopv_descs, M_VNODE);
298         vnodeopv_descs = newopv;
299         vnodeopv_num--;
300
301         vfs_opv_recalc();
302 }
303
304 /*
305  * Routines having to do with the management of the vnode table.
306  */
307 struct vattr va_null;
308
309 /*
310  * Initialize the vnode structures and initialize each file system type.
311  */
312 /* ARGSUSED*/
313 static void
314 vfsinit(void *dummy)
315 {
316
317         vattr_null(&va_null);
318 }
319 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
320
321 /* Register a new file system type in the global table */
322 int
323 vfs_register(struct vfsconf *vfc)
324 {
325         struct sysctl_oid *oidp;
326         struct vfsconf *vfsp;
327
328         vfsp = NULL;
329         if (vfsconf)
330                 for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
331                         if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
332                                 return EEXIST;
333
334         vfc->vfc_typenum = maxvfsconf++;
335         if (vfsp)
336                 vfsp->vfc_next = vfc;
337         else
338                 vfsconf = vfc;
339         vfc->vfc_next = NULL;
340
341         /*
342          * If this filesystem has a sysctl node under vfs
343          * (i.e. vfs.xxfs), then change the oid number of that node to 
344          * match the filesystem's type number.  This allows user code
345          * which uses the type number to read sysctl variables defined
346          * by the filesystem to continue working. Since the oids are
347          * in a sorted list, we need to make sure the order is
348          * preserved by re-registering the oid after modifying its
349          * number.
350          */
351         SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
352                 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
353                         sysctl_unregister_oid(oidp);
354                         oidp->oid_number = vfc->vfc_typenum;
355                         sysctl_register_oid(oidp);
356                 }
357
358         /*
359          * Call init function for this VFS...
360          */
361         (*(vfc->vfc_vfsops->vfs_init))(vfc);
362
363         return 0;
364 }
365
366
367 /* Remove registration of a file system type */
368 int
369 vfs_unregister(struct vfsconf *vfc)
370 {
371         struct vfsconf *vfsp, *prev_vfsp;
372         int error, i, maxtypenum;
373
374         i = vfc->vfc_typenum;
375
376         prev_vfsp = NULL;
377         for (vfsp = vfsconf; vfsp;
378                         prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
379                 if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
380                         break;
381         }
382         if (vfsp == NULL)
383                 return EINVAL;
384         if (vfsp->vfc_refcount)
385                 return EBUSY;
386         if (vfc->vfc_vfsops->vfs_uninit != NULL) {
387                 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
388                 if (error)
389                         return (error);
390         }
391         if (prev_vfsp)
392                 prev_vfsp->vfc_next = vfsp->vfc_next;
393         else
394                 vfsconf = vfsp->vfc_next;
395         maxtypenum = VFS_GENERIC;
396         for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
397                 if (maxtypenum < vfsp->vfc_typenum)
398                         maxtypenum = vfsp->vfc_typenum;
399         maxvfsconf = maxtypenum + 1;
400         return 0;
401 }
402
403 /*
404  * Standard kernel module handling code for file system modules.
405  * Referenced from VFS_SET().
406  */
407 int
408 vfs_modevent(module_t mod, int type, void *data)
409 {
410         struct vfsconf *vfc;
411         int error = 0;
412
413         vfc = (struct vfsconf *)data;
414
415         switch (type) {
416         case MOD_LOAD:
417                 if (vfc)
418                         error = vfs_register(vfc);
419                 break;
420
421         case MOD_UNLOAD:
422                 if (vfc)
423                         error = vfs_unregister(vfc);
424                 break;
425         default:        /* including MOD_SHUTDOWN */
426                 break;
427         }
428         return (error);
429 }