xref: /openbsd-src/sys/kern/vfs_init.c (revision df930be708d50e9715f173caa26ffe1b7599b157)
1 /*	$NetBSD: vfs_init.c,v 1.4 1995/03/19 23:45:01 mycroft 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 
55 /*
56  * Sigh, such primitive tools are these...
57  */
58 #if 0
59 #define DODEBUG(A) A
60 #else
61 #define DODEBUG(A)
62 #endif
63 
64 extern struct vnodeopv_desc *vfs_opv_descs[];
65 				/* a list of lists of vnodeops defns */
66 extern struct vnodeop_desc *vfs_op_descs[];
67 				/* and the operations they perform */
68 /*
69  * This code doesn't work if the defn is **vnodop_defns with cc.
70  * The problem is because of the compiler sometimes putting in an
71  * extra level of indirection for arrays.  It's an interesting
72  * "feature" of C.
73  */
74 int vfs_opv_numops;
75 
76 typedef (*PFI)();   /* the standard Pointer to a Function returning an Int */
77 
78 /*
79  * A miscellaneous routine.
80  * A generic "default" routine that just returns an error.
81  */
82 int
83 vn_default_error()
84 {
85 
86 	return (EOPNOTSUPP);
87 }
88 
89 /*
90  * vfs_init.c
91  *
92  * Allocate and fill in operations vectors.
93  *
94  * An undocumented feature of this approach to defining operations is that
95  * there can be multiple entries in vfs_opv_descs for the same operations
96  * vector. This allows third parties to extend the set of operations
97  * supported by another layer in a binary compatibile way. For example,
98  * assume that NFS needed to be modified to support Ficus. NFS has an entry
99  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
100  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
101  * listing those new operations Ficus adds to NFS, all without modifying the
102  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
103  * that is a(whole)nother story.) This is a feature.
104  */
105 
106 /*
107  * Allocate and init the vector, if it needs it.
108  * Also handle backwards compatibility.
109  */
110 void
111 vfs_opv_init_explicit(vfs_opv_desc)
112 	struct vnodeopv_desc *vfs_opv_desc;
113 {
114 	int (**opv_desc_vector)();
115 	struct vnodeopv_entry_desc *opve_descp;
116 
117 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
118 
119 	if (opv_desc_vector == NULL) {
120 		/* XXX - shouldn't be M_VNODE */
121 		MALLOC(opv_desc_vector, PFI *,
122 		    vfs_opv_numops * sizeof(PFI), M_VNODE, M_WAITOK);
123 		bzero(opv_desc_vector, vfs_opv_numops * sizeof(PFI));
124 		*(vfs_opv_desc->opv_desc_vector_p) = opv_desc_vector;
125 		DODEBUG(printf("vector at %p allocated\n",
126 		    opv_desc_vector_p));
127 	}
128 
129 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
130 	     opve_descp->opve_op;
131 	     opve_descp++) {
132 		/*
133 		 * Sanity check:  is this operation listed
134 		 * in the list of operations?  We check this
135 		 * by seeing if its offest is zero.  Since
136 		 * the default routine should always be listed
137 		 * first, it should be the only one with a zero
138 		 * offset.  Any other operation with a zero
139 		 * offset is probably not listed in
140 		 * vfs_op_descs, and so is probably an error.
141 		 *
142 		 * A panic here means the layer programmer
143 		 * has committed the all-too common bug
144 		 * of adding a new operation to the layer's
145 		 * list of vnode operations but
146 		 * not adding the operation to the system-wide
147 		 * list of supported operations.
148 		 */
149 		if (opve_descp->opve_op->vdesc_offset == 0 &&
150 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
151 			printf("operation %s not listed in %s.\n",
152 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
153 			panic ("vfs_opv_init: bad operation");
154 		}
155 
156 		/*
157 		 * Fill in this entry.
158 		 */
159 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
160 		    opve_descp->opve_impl;
161 	}
162 }
163 
164 void
165 vfs_opv_init_default(vfs_opv_desc)
166 	struct vnodeopv_desc *vfs_opv_desc;
167 {
168 	int j;
169 	int (**opv_desc_vector)();
170 	struct vnodeopv_entry_desc *opve_descp;
171 
172 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
173 
174 	/*
175 	 * Force every operations vector to have a default routine.
176 	 */
177 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
178 		panic("vfs_opv_init: operation vector without default routine.");
179 
180 	for (j = 0; j < vfs_opv_numops; j++)
181 		if (opv_desc_vector[j] == NULL)
182 			opv_desc_vector[j] =
183 			    opv_desc_vector[VOFFSET(vop_default)];
184 }
185 
186 void
187 vfs_opv_init()
188 {
189 	int i;
190 
191 	/*
192 	 * Allocate the dynamic vectors and fill them in.
193 	 */
194 	for (i = 0; vfs_opv_descs[i]; i++)
195 		vfs_opv_init_explicit(vfs_opv_descs[i]);
196 
197 	/*
198 	 * Finally, go back and replace unfilled routines
199 	 * with their default.
200 	 */
201 	for (i = 0; vfs_opv_descs[i]; i++)
202 		vfs_opv_init_default(vfs_opv_descs[i]);
203 }
204 
205 /*
206  * Initialize known vnode operations vectors.
207  */
208 void
209 vfs_op_init()
210 {
211 	int i;
212 
213 	DODEBUG(printf("Vnode_interface_init.\n"));
214 	/*
215 	 * Set all vnode vectors to a well known value.
216 	 */
217 	for (i = 0; vfs_opv_descs[i]; i++)
218 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
219 	/*
220 	 * Figure out how many ops there are by counting the table,
221 	 * and assign each its offset.
222 	 */
223 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
224 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
225 		vfs_opv_numops++;
226 	}
227 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
228 }
229 
230 /*
231  * Routines having to do with the management of the vnode table.
232  */
233 extern struct vnodeops dead_vnodeops;
234 extern struct vnodeops spec_vnodeops;
235 extern void vclean();
236 struct vattr va_null;
237 
238 /*
239  * Initialize the vnode structures and initialize each file system type.
240  */
241 vfsinit()
242 {
243 	struct vfsops **vfsp;
244 
245 	/*
246 	 * Initialize the vnode table
247 	 */
248 	vntblinit();
249 	/*
250 	 * Initialize the vnode name cache
251 	 */
252 	nchinit();
253 	/*
254 	 * Build vnode operation vectors.
255 	 */
256 	vfs_op_init();
257 	vfs_opv_init();   /* finish the job */
258 	/*
259 	 * Initialize each file system type.
260 	 */
261 	vattr_null(&va_null);
262 	for (vfsp = &vfssw[0]; vfsp < &vfssw[nvfssw]; vfsp++) {
263 		if (*vfsp == NULL)
264 			continue;
265 		(*(*vfsp)->vfs_init)();
266 	}
267 }
268