xref: /freebsd/sys/kern/vfs_init.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
39  */
40 
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/time.h>
45 #include <sys/vnode.h>
46 #include <sys/stat.h>
47 #include <sys/namei.h>
48 #include <sys/ucred.h>
49 #include <sys/buf.h>
50 #include <sys/errno.h>
51 #include <sys/malloc.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 (*PFI)();   /* the standard Pointer to a Function returning an Int */
75 
76 /*
77  * A miscellaneous routine.
78  * A generic "default" routine that just returns an error.
79  */
80 int
81 vn_default_error()
82 {
83 
84 	return (EOPNOTSUPP);
85 }
86 
87 /*
88  * vfs_init.c
89  *
90  * Allocate and fill in operations vectors.
91  *
92  * An undocumented feature of this approach to defining operations is that
93  * there can be multiple entries in vfs_opv_descs for the same operations
94  * vector. This allows third parties to extend the set of operations
95  * supported by another layer in a binary compatibile way. For example,
96  * assume that NFS needed to be modified to support Ficus. NFS has an entry
97  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
98  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
99  * listing those new operations Ficus adds to NFS, all without modifying the
100  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
101  * that is a(whole)nother story.) This is a feature.
102  */
103 void
104 vfs_opv_init()
105 {
106 	int i, j, k;
107 	int (***opv_desc_vector_p)();
108 	int (**opv_desc_vector)();
109 	struct vnodeopv_entry_desc *opve_descp;
110 
111 	/*
112 	 * Allocate the dynamic vectors and fill them in.
113 	 */
114 	for (i=0; vfs_opv_descs[i]; i++) {
115 		opv_desc_vector_p = vfs_opv_descs[i]->opv_desc_vector_p;
116 		/*
117 		 * Allocate and init the vector, if it needs it.
118 		 * Also handle backwards compatibility.
119 		 */
120 		if (*opv_desc_vector_p == NULL) {
121 			/* XXX - shouldn't be M_VNODE */
122 			MALLOC(*opv_desc_vector_p, PFI*,
123 			       vfs_opv_numops*sizeof(PFI), M_VNODE, M_WAITOK);
124 			bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
125 			DODEBUG(printf("vector at %x allocated\n",
126 			    opv_desc_vector_p));
127 		}
128 		opv_desc_vector = *opv_desc_vector_p;
129 		for (j=0; vfs_opv_descs[i]->opv_desc_ops[j].opve_op; j++) {
130 			opve_descp = &(vfs_opv_descs[i]->opv_desc_ops[j]);
131 
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 !=
151 				    	VOFFSET(vop_default)) {
152 				printf("operation %s not listed in %s.\n",
153 				    opve_descp->opve_op->vdesc_name,
154 				    "vfs_op_descs");
155 				panic ("vfs_opv_init: bad operation");
156 			}
157 			/*
158 			 * Fill in this entry.
159 			 */
160 			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
161 					opve_descp->opve_impl;
162 		}
163 	}
164 	/*
165 	 * Finally, go back and replace unfilled routines
166 	 * with their default.  (Sigh, an O(n^3) algorithm.  I
167 	 * could make it better, but that'd be work, and n is small.)
168 	 */
169 	for (i = 0; vfs_opv_descs[i]; i++) {
170 		opv_desc_vector = *(vfs_opv_descs[i]->opv_desc_vector_p);
171 		/*
172 		 * Force every operations vector to have a default routine.
173 		 */
174 		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
175 			panic("vfs_opv_init: operation vector without default routine.");
176 		}
177 		for (k = 0; k<vfs_opv_numops; k++)
178 			if (opv_desc_vector[k] == NULL)
179 				opv_desc_vector[k] =
180 					opv_desc_vector[VOFFSET(vop_default)];
181 	}
182 }
183 
184 /*
185  * Initialize known vnode operations vectors.
186  */
187 void
188 vfs_op_init()
189 {
190 	int i;
191 
192 	DODEBUG(printf("Vnode_interface_init.\n"));
193 	/*
194 	 * Set all vnode vectors to a well known value.
195 	 */
196 	for (i = 0; vfs_opv_descs[i]; i++)
197 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
198 	/*
199 	 * Figure out how many ops there are by counting the table,
200 	 * and assign each its offset.
201 	 */
202 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
203 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
204 		vfs_opv_numops++;
205 	}
206 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
207 }
208 
209 /*
210  * Routines having to do with the management of the vnode table.
211  */
212 extern struct vnodeops dead_vnodeops;
213 extern struct vnodeops spec_vnodeops;
214 extern void vclean();
215 struct vattr va_null;
216 
217 /*
218  * Initialize the vnode structures and initialize each file system type.
219  */
220 void
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[MOUNT_MAXTYPE]; vfsp++) {
243 		if (*vfsp == NULL)
244 			continue;
245 		(*(*vfsp)->vfs_init)();
246 	}
247 }
248