xref: /openbsd-src/sys/kern/vfs_init.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
1 /*	$NetBSD: vfs_init.c,v 1.6 1996/02/09 19:00:58 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed
8  * to Berkeley by John Heidemann of the UCLA Ficus project.
9  *
10  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
41  */
42 
43 
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/time.h>
47 #include <sys/vnode.h>
48 #include <sys/stat.h>
49 #include <sys/namei.h>
50 #include <sys/ucred.h>
51 #include <sys/buf.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
54 #include <sys/systm.h>
55 
56 /*
57  * Sigh, such primitive tools are these...
58  */
59 #if 0
60 #define DODEBUG(A) A
61 #else
62 #define DODEBUG(A)
63 #endif
64 
65 extern struct vnodeopv_desc *vfs_opv_descs[];
66 				/* a list of lists of vnodeops defns */
67 extern struct vnodeop_desc *vfs_op_descs[];
68 				/* and the operations they perform */
69 /*
70  * This code doesn't work if the defn is **vnodop_defns with cc.
71  * The problem is because of the compiler sometimes putting in an
72  * extra level of indirection for arrays.  It's an interesting
73  * "feature" of C.
74  */
75 int vfs_opv_numops;
76 
77 typedef (*PFI) __P((void *));
78 
79 /*
80  * A miscellaneous routine.
81  * A generic "default" routine that just returns an error.
82  */
83 /*ARGSUSED*/
84 int
85 vn_default_error(v)
86 	void *v;
87 {
88 
89 	return (EOPNOTSUPP);
90 }
91 
92 /*
93  * vfs_init.c
94  *
95  * Allocate and fill in operations vectors.
96  *
97  * An undocumented feature of this approach to defining operations is that
98  * there can be multiple entries in vfs_opv_descs for the same operations
99  * vector. This allows third parties to extend the set of operations
100  * supported by another layer in a binary compatibile way. For example,
101  * assume that NFS needed to be modified to support Ficus. NFS has an entry
102  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
103  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
104  * listing those new operations Ficus adds to NFS, all without modifying the
105  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
106  * that is a(whole)nother story.) This is a feature.
107  */
108 
109 /*
110  * Allocate and init the vector, if it needs it.
111  * Also handle backwards compatibility.
112  */
113 void
114 vfs_opv_init_explicit(vfs_opv_desc)
115 	struct vnodeopv_desc *vfs_opv_desc;
116 {
117 	int (**opv_desc_vector) __P((void *));
118 	struct vnodeopv_entry_desc *opve_descp;
119 
120 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
121 
122 	if (opv_desc_vector == NULL) {
123 		/* XXX - shouldn't be M_VNODE */
124 		MALLOC(opv_desc_vector, PFI *,
125 		    vfs_opv_numops * sizeof(PFI), M_VNODE, M_WAITOK);
126 		bzero(opv_desc_vector, vfs_opv_numops * sizeof(PFI));
127 		*(vfs_opv_desc->opv_desc_vector_p) = opv_desc_vector;
128 		DODEBUG(printf("vector at %p allocated\n",
129 		    opv_desc_vector_p));
130 	}
131 
132 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
133 	     opve_descp->opve_op;
134 	     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) __P((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 	struct vfsops **vfsp;
246 
247 	/*
248 	 * Initialize the vnode table
249 	 */
250 	vntblinit();
251 	/*
252 	 * Initialize the vnode name cache
253 	 */
254 	nchinit();
255 	/*
256 	 * Build vnode operation vectors.
257 	 */
258 	vfs_op_init();
259 	vfs_opv_init();   /* finish the job */
260 	/*
261 	 * Initialize each file system type.
262 	 */
263 	vattr_null(&va_null);
264 	for (vfsp = &vfssw[0]; vfsp < &vfssw[nvfssw]; vfsp++) {
265 		if (*vfsp == NULL)
266 			continue;
267 		(*(*vfsp)->vfs_init)();
268 	}
269 }
270