xref: /openbsd-src/sys/kern/vfs_init.c (revision 152d075113ea9c9416253f124537cdd8e8d918a6)
1 /*	$OpenBSD: vfs_init.c,v 1.15 2003/07/24 22:00:24 mickey 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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
38  */
39 
40 
41 #include <sys/param.h>
42 #include <sys/mount.h>
43 #include <sys/time.h>
44 #include <sys/vnode.h>
45 #include <sys/stat.h>
46 #include <sys/namei.h>
47 #include <sys/ucred.h>
48 #include <sys/buf.h>
49 #include <sys/errno.h>
50 #include <sys/malloc.h>
51 #include <sys/systm.h>
52 
53 /*
54  * Sigh, such primitive tools are these...
55  */
56 #if 0
57 #define DODEBUG(A) A
58 #else
59 #define DODEBUG(A)
60 #endif
61 
62 extern struct vnodeopv_desc *vfs_opv_descs[];
63 				/* a list of lists of vnodeops defns */
64 extern struct vnodeop_desc *vfs_op_descs[];
65 				/* and the operations they perform */
66 /*
67  * This code doesn't work if the defn is **vnodop_defns with cc.
68  * The problem is because of the compiler sometimes putting in an
69  * extra level of indirection for arrays.  It's an interesting
70  * "feature" of C.
71  */
72 int vfs_opv_numops;
73 
74 typedef int (*PFI)(void *);
75 
76 /*
77  * A miscellaneous routine.
78  * A generic "default" routine that just returns an error.
79  */
80 /*ARGSUSED*/
81 int
82 vn_default_error(v)
83 	void *v;
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)(void *);
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 		opv_desc_vector = malloc(vfs_opv_numops * sizeof(PFI),
122 		    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));
127 	}
128 
129 	for (opve_descp = vfs_opv_desc->opv_desc_ops;
130 	    opve_descp->opve_op; opve_descp++) {
131 		/*
132 		 * Sanity check:  is this operation listed
133 		 * in the list of operations?  We check this
134 		 * by seeing if its offest is zero.  Since
135 		 * the default routine should always be listed
136 		 * first, it should be the only one with a zero
137 		 * offset.  Any other operation with a zero
138 		 * offset is probably not listed in
139 		 * vfs_op_descs, and so is probably an error.
140 		 *
141 		 * A panic here means the layer programmer
142 		 * has committed the all-too common bug
143 		 * of adding a new operation to the layer's
144 		 * list of vnode operations but
145 		 * not adding the operation to the system-wide
146 		 * list of supported operations.
147 		 */
148 		if (opve_descp->opve_op->vdesc_offset == 0 &&
149 		    opve_descp->opve_op->vdesc_offset != VOFFSET(vop_default)) {
150 			printf("operation %s not listed in %s.\n",
151 			    opve_descp->opve_op->vdesc_name, "vfs_op_descs");
152 			panic ("vfs_opv_init: bad operation");
153 		}
154 
155 		/*
156 		 * Fill in this entry.
157 		 */
158 		opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
159 		    opve_descp->opve_impl;
160 	}
161 }
162 
163 void
164 vfs_opv_init_default(vfs_opv_desc)
165 	struct vnodeopv_desc *vfs_opv_desc;
166 {
167 	int j;
168 	int (**opv_desc_vector)(void *);
169 
170 	opv_desc_vector = *(vfs_opv_desc->opv_desc_vector_p);
171 
172 	/*
173 	 * Force every operations vector to have a default routine.
174 	 */
175 	if (opv_desc_vector[VOFFSET(vop_default)] == NULL)
176 		panic("vfs_opv_init: operation vector without default routine.");
177 
178 	for (j = 0; j < vfs_opv_numops; j++)
179 		if (opv_desc_vector[j] == NULL)
180 			opv_desc_vector[j] =
181 			    opv_desc_vector[VOFFSET(vop_default)];
182 }
183 
184 void
185 vfs_opv_init()
186 {
187 	int i;
188 
189 	/*
190 	 * Allocate the dynamic vectors and fill them in.
191 	 */
192 	for (i = 0; vfs_opv_descs[i]; i++)
193 		vfs_opv_init_explicit(vfs_opv_descs[i]);
194 
195 	/*
196 	 * Finally, go back and replace unfilled routines
197 	 * with their default.
198 	 */
199 	for (i = 0; vfs_opv_descs[i]; i++)
200 		vfs_opv_init_default(vfs_opv_descs[i]);
201 }
202 
203 /*
204  * Initialize known vnode operations vectors.
205  */
206 void
207 vfs_op_init()
208 {
209 	int i;
210 
211 	DODEBUG(printf("Vnode_interface_init.\n"));
212 	/*
213 	 * Set all vnode vectors to a well known value.
214 	 */
215 	for (i = 0; vfs_opv_descs[i]; i++)
216 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
217 	/*
218 	 * Figure out how many ops there are by counting the table,
219 	 * and assign each its offset.
220 	 */
221 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
222 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
223 		vfs_opv_numops++;
224 	}
225 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
226 }
227 
228 /*
229  * Routines having to do with the management of the vnode table.
230  */
231 struct vattr va_null;
232 
233 /*
234  * Initialize the vnode structures and initialize each file system type.
235  */
236 void
237 vfsinit()
238 {
239 	int i;
240 	struct vfsconf *vfsconflist;
241 	int vfsconflistlen;
242 
243 	/*
244 	 * Initialize the vnode table
245 	 */
246 	vntblinit();
247 	/*
248 	 * Initialize the vnode name cache
249 	 */
250 	nchinit();
251 	/*
252 	 * Build vnode operation vectors.
253 	 */
254 	vfs_op_init();
255 	vfs_opv_init();   /* finish the job */
256 	/*
257 	 * Initialize each file system type.
258 	 */
259 	vattr_null(&va_null);
260 
261 	/*
262 	 * Stop using vfsconf and maxvfsconf as a temporary storage,
263 	 * set them to their correct values now.
264 	 */
265 	vfsconflist = vfsconf;
266 	vfsconflistlen = maxvfsconf;
267 	vfsconf = NULL;
268 	maxvfsconf = 0;
269 
270 	for (i = 0; i < vfsconflistlen; i++)
271 		vfs_register(&vfsconflist[i]);
272 }
273