xref: /openbsd-src/sys/kern/vfs_init.c (revision c4071fd13883b3f74b90a411bbb39755a785aeaa)
1 /*	$OpenBSD: vfs_init.c,v 1.12 2002/03/14 01:27:06 millert 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)(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)(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 		opv_desc_vector = malloc(vfs_opv_numops * sizeof(PFI),
126 		    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; opve_descp++) {
135 		/*
136 		 * Sanity check:  is this operation listed
137 		 * in the list of operations?  We check this
138 		 * by seeing if its offest is zero.  Since
139 		 * the default routine should always be listed
140 		 * first, it should be the only one with a zero
141 		 * offset.  Any other operation with a zero
142 		 * offset is probably not listed in
143 		 * vfs_op_descs, and so is probably an error.
144 		 *
145 		 * A panic here means the layer programmer
146 		 * has committed the all-too common bug
147 		 * of adding a new operation to the layer's
148 		 * list of vnode operations but
149 		 * not adding the operation to the system-wide
150 		 * list of supported operations.
151 		 */
152 		if (opve_descp->opve_op->vdesc_offset == 0 &&
153 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
154 			printf("operation %s not listed in %s.\n",
155 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
156 			panic ("vfs_opv_init: bad operation");
157 		}
158 
159 		/*
160 		 * Fill in this entry.
161 		 */
162 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
163 		    opve_descp->opve_impl;
164 	}
165 }
166 
167 void
168 vfs_opv_init_default(vfs_opv_desc)
169 	struct vnodeopv_desc *vfs_opv_desc;
170 {
171 	int j;
172 	int (**opv_desc_vector)(void *);
173 
174 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
175 
176 	/*
177 	 * Force every operations vector to have a default routine.
178 	 */
179 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
180 		panic("vfs_opv_init: operation vector without default routine.");
181 
182 	for (j = 0; j < vfs_opv_numops; j++)
183 		if (opv_desc_vector[j] == NULL)
184 			opv_desc_vector[j] =
185 			    opv_desc_vector[VOFFSET(vop_default)];
186 }
187 
188 void
189 vfs_opv_init()
190 {
191 	int i;
192 
193 	/*
194 	 * Allocate the dynamic vectors and fill them in.
195 	 */
196 	for (i = 0; vfs_opv_descs[i]; i++)
197 		vfs_opv_init_explicit(vfs_opv_descs[i]);
198 
199 	/*
200 	 * Finally, go back and replace unfilled routines
201 	 * with their default.
202 	 */
203 	for (i = 0; vfs_opv_descs[i]; i++)
204 		vfs_opv_init_default(vfs_opv_descs[i]);
205 }
206 
207 /*
208  * Initialize known vnode operations vectors.
209  */
210 void
211 vfs_op_init()
212 {
213 	int i;
214 
215 	DODEBUG(printf("Vnode_interface_init.\n"));
216 	/*
217 	 * Set all vnode vectors to a well known value.
218 	 */
219 	for (i = 0; vfs_opv_descs[i]; i++)
220 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
221 	/*
222 	 * Figure out how many ops there are by counting the table,
223 	 * and assign each its offset.
224 	 */
225 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
226 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
227 		vfs_opv_numops++;
228 	}
229 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
230 }
231 
232 /*
233  * Routines having to do with the management of the vnode table.
234  */
235 extern struct vnodeops dead_vnodeops;
236 extern struct vnodeops spec_vnodeops;
237 struct vattr va_null;
238 
239 /*
240  * Initialize the vnode structures and initialize each file system type.
241  */
242 void
243 vfsinit()
244 {
245 	int i;
246 	struct vfsconf *vfsconflist;
247 	int vfsconflistlen;
248 
249 	/*
250 	 * Initialize the vnode table
251 	 */
252 	vntblinit();
253 	/*
254 	 * Initialize the vnode name cache
255 	 */
256 	nchinit();
257 	/*
258 	 * Build vnode operation vectors.
259 	 */
260 	vfs_op_init();
261 	vfs_opv_init();   /* finish the job */
262 	/*
263 	 * Initialize each file system type.
264 	 */
265 	vattr_null(&va_null);
266 
267 	/*
268 	 * Stop using vfsconf and maxvfsconf as a temporary storage,
269 	 * set them to their correct values now.
270 	 */
271 	vfsconflist = vfsconf;
272 	vfsconflistlen = maxvfsconf;
273 	vfsconf = NULL;
274 	maxvfsconf = 0;
275 
276 	for (i = 0; i < vfsconflistlen; i++)
277 		vfs_register(&vfsconflist[i]);
278 }
279