xref: /openbsd-src/sys/kern/vfs_init.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: vfs_init.c,v 1.9 1999/02/19 18:02:48 art Exp $	*/
2 /*	$NetBSD: vfs_init.c,v 1.6 1996/02/09 19:00:58 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed
9  * to Berkeley by John Heidemann of the UCLA Ficus project.
10  *
11  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
42  */
43 
44 
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/time.h>
48 #include <sys/vnode.h>
49 #include <sys/stat.h>
50 #include <sys/namei.h>
51 #include <sys/ucred.h>
52 #include <sys/buf.h>
53 #include <sys/errno.h>
54 #include <sys/malloc.h>
55 #include <sys/systm.h>
56 
57 /*
58  * Sigh, such primitive tools are these...
59  */
60 #if 0
61 #define DODEBUG(A) A
62 #else
63 #define DODEBUG(A)
64 #endif
65 
66 extern struct vnodeopv_desc *vfs_opv_descs[];
67 				/* a list of lists of vnodeops defns */
68 extern struct vnodeop_desc *vfs_op_descs[];
69 				/* and the operations they perform */
70 /*
71  * This code doesn't work if the defn is **vnodop_defns with cc.
72  * The problem is because of the compiler sometimes putting in an
73  * extra level of indirection for arrays.  It's an interesting
74  * "feature" of C.
75  */
76 int vfs_opv_numops;
77 
78 typedef int (*PFI) __P((void *));
79 
80 /*
81  * A miscellaneous routine.
82  * A generic "default" routine that just returns an error.
83  */
84 /*ARGSUSED*/
85 int
86 vn_default_error(v)
87 	void *v;
88 {
89 
90 	return (EOPNOTSUPP);
91 }
92 
93 /*
94  * vfs_init.c
95  *
96  * Allocate and fill in operations vectors.
97  *
98  * An undocumented feature of this approach to defining operations is that
99  * there can be multiple entries in vfs_opv_descs for the same operations
100  * vector. This allows third parties to extend the set of operations
101  * supported by another layer in a binary compatibile way. For example,
102  * assume that NFS needed to be modified to support Ficus. NFS has an entry
103  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
104  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
105  * listing those new operations Ficus adds to NFS, all without modifying the
106  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
107  * that is a(whole)nother story.) This is a feature.
108  */
109 
110 /*
111  * Allocate and init the vector, if it needs it.
112  * Also handle backwards compatibility.
113  */
114 void
115 vfs_opv_init_explicit(vfs_opv_desc)
116 	struct vnodeopv_desc *vfs_opv_desc;
117 {
118 	int (**opv_desc_vector) __P((void *));
119 	struct vnodeopv_entry_desc *opve_descp;
120 
121 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
122 
123 	if (opv_desc_vector == NULL) {
124 		/* XXX - shouldn't be M_VNODE */
125 		MALLOC(opv_desc_vector, PFI *,
126 		    vfs_opv_numops * sizeof(PFI), M_VNODE, M_WAITOK);
127 		bzero(opv_desc_vector, vfs_opv_numops * sizeof(PFI));
128 		*(vfs_opv_desc->opv_desc_vector_p) = opv_desc_vector;
129 		DODEBUG(printf("vector at %p allocated\n",
130 		    opv_desc_vector_p));
131 	}
132 
133 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
134 	     opve_descp->opve_op;
135 	     opve_descp++) {
136 		/*
137 		 * Sanity check:  is this operation listed
138 		 * in the list of operations?  We check this
139 		 * by seeing if its offest is zero.  Since
140 		 * the default routine should always be listed
141 		 * first, it should be the only one with a zero
142 		 * offset.  Any other operation with a zero
143 		 * offset is probably not listed in
144 		 * vfs_op_descs, and so is probably an error.
145 		 *
146 		 * A panic here means the layer programmer
147 		 * has committed the all-too common bug
148 		 * of adding a new operation to the layer's
149 		 * list of vnode operations but
150 		 * not adding the operation to the system-wide
151 		 * list of supported operations.
152 		 */
153 		if (opve_descp->opve_op->vdesc_offset == 0 &&
154 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
155 			printf("operation %s not listed in %s.\n",
156 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
157 			panic ("vfs_opv_init: bad operation");
158 		}
159 
160 		/*
161 		 * Fill in this entry.
162 		 */
163 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
164 		    opve_descp->opve_impl;
165 	}
166 }
167 
168 void
169 vfs_opv_init_default(vfs_opv_desc)
170 	struct vnodeopv_desc *vfs_opv_desc;
171 {
172 	int j;
173 	int (**opv_desc_vector) __P((void *));
174 
175 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
176 
177 	/*
178 	 * Force every operations vector to have a default routine.
179 	 */
180 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
181 		panic("vfs_opv_init: operation vector without default routine.");
182 
183 	for (j = 0; j < vfs_opv_numops; j++)
184 		if (opv_desc_vector[j] == NULL)
185 			opv_desc_vector[j] =
186 			    opv_desc_vector[VOFFSET(vop_default)];
187 }
188 
189 void
190 vfs_opv_init()
191 {
192 	int i;
193 
194 	/*
195 	 * Allocate the dynamic vectors and fill them in.
196 	 */
197 	for (i = 0; vfs_opv_descs[i]; i++)
198 		vfs_opv_init_explicit(vfs_opv_descs[i]);
199 
200 	/*
201 	 * Finally, go back and replace unfilled routines
202 	 * with their default.
203 	 */
204 	for (i = 0; vfs_opv_descs[i]; i++)
205 		vfs_opv_init_default(vfs_opv_descs[i]);
206 }
207 
208 /*
209  * Initialize known vnode operations vectors.
210  */
211 void
212 vfs_op_init()
213 {
214 	int i;
215 
216 	DODEBUG(printf("Vnode_interface_init.\n"));
217 	/*
218 	 * Set all vnode vectors to a well known value.
219 	 */
220 	for (i = 0; vfs_opv_descs[i]; i++)
221 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
222 	/*
223 	 * Figure out how many ops there are by counting the table,
224 	 * and assign each its offset.
225 	 */
226 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
227 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
228 		vfs_opv_numops++;
229 	}
230 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
231 }
232 
233 /*
234  * Routines having to do with the management of the vnode table.
235  */
236 extern struct vnodeops dead_vnodeops;
237 extern struct vnodeops spec_vnodeops;
238 struct vattr va_null;
239 
240 /*
241  * Initialize the vnode structures and initialize each file system type.
242  */
243 void
244 vfsinit()
245 {
246 	int i;
247 	struct vfsconf *vfsconflist;
248 	int vfsconflistlen;
249 
250 	/*
251 	 * Initialize the vnode table
252 	 */
253 	vntblinit();
254 	/*
255 	 * Initialize the vnode name cache
256 	 */
257 	nchinit();
258 	/*
259 	 * Build vnode operation vectors.
260 	 */
261 	vfs_op_init();
262 	vfs_opv_init();   /* finish the job */
263 	/*
264 	 * Initialize each file system type.
265 	 */
266 	vattr_null(&va_null);
267 
268 	/*
269 	 * Stop using vfsconf and maxvfsconf as a temporary storage,
270 	 * set them to their correct values now.
271 	 */
272 	vfsconflist = vfsconf;
273 	vfsconflistlen = maxvfsconf;
274 	vfsconf = NULL;
275 	maxvfsconf = 0;
276 
277 	for (i = 0; i < vfsconflistlen; i++)
278 		vfs_register(&vfsconflist[i]);
279 }
280