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