xref: /freebsd/sys/kern/vfs_init.c (revision 952d112864d8008aa87278a30a539d888a8493cd)
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  * $Id: vfs_init.c,v 1.24 1997/02/22 09:39:32 peter Exp $
40  */
41 
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/time.h>
48 #include <sys/vnode.h>
49 #include <sys/stat.h>
50 #include <sys/namei.h>
51 #include <sys/ucred.h>
52 #include <sys/buf.h>
53 #include <sys/errno.h>
54 #include <sys/malloc.h>
55 #include <sys/proc.h>
56 
57 static void	vfs_op_init __P((void));
58 
59 static void vfsinit __P((void *));
60 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
61 
62 /*
63  * Sigh, such primitive tools are these...
64  */
65 #if 0
66 #define DODEBUG(A) A
67 #else
68 #define DODEBUG(A)
69 #endif
70 
71 struct vfsconf void_vfsconf;
72 
73 extern struct linker_set vfs_opv_descs_;
74 #define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
75 
76 extern struct linker_set vfs_set;
77 
78 extern struct vnodeop_desc *vfs_op_descs[];
79 				/* and the operations they perform */
80 /*
81  * This code doesn't work if the defn is **vnodop_defns with cc.
82  * The problem is because of the compiler sometimes putting in an
83  * extra level of indirection for arrays.  It's an interesting
84  * "feature" of C.
85  */
86 static int vfs_opv_numops;
87 
88 /*
89  * A miscellaneous routine.
90  * A generic "default" routine that just returns an error.
91  */
92 int
93 vn_default_error()
94 {
95 
96 	return (EOPNOTSUPP);
97 }
98 
99 /*
100  * vfs_init.c
101  *
102  * Allocate and fill in operations vectors.
103  *
104  * An undocumented feature of this approach to defining operations is that
105  * there can be multiple entries in vfs_opv_descs for the same operations
106  * vector. This allows third parties to extend the set of operations
107  * supported by another layer in a binary compatibile way. For example,
108  * assume that NFS needed to be modified to support Ficus. NFS has an entry
109  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
110  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
111  * listing those new operations Ficus adds to NFS, all without modifying the
112  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
113  * that is a(whole)nother story.) This is a feature.
114  */
115 void
116 vfs_opv_init(struct vnodeopv_desc **them)
117 {
118 	int i, j, k;
119 	vop_t ***opv_desc_vector_p;
120 	vop_t **opv_desc_vector;
121 	struct vnodeopv_entry_desc *opve_descp;
122 
123 	/*
124 	 * Allocate the dynamic vectors and fill them in.
125 	 */
126 	for (i=0; them[i]; i++) {
127 		opv_desc_vector_p = them[i]->opv_desc_vector_p;
128 		/*
129 		 * Allocate and init the vector, if it needs it.
130 		 * Also handle backwards compatibility.
131 		 */
132 		if (*opv_desc_vector_p == NULL) {
133 			/* XXX - shouldn't be M_VNODE */
134 			MALLOC(*opv_desc_vector_p, vop_t **,
135 			       vfs_opv_numops * sizeof(vop_t *), M_VNODE,
136 			       M_WAITOK);
137 			bzero(*opv_desc_vector_p,
138 			      vfs_opv_numops * sizeof(vop_t *));
139 			DODEBUG(printf("vector at %x allocated\n",
140 			    opv_desc_vector_p));
141 		}
142 		opv_desc_vector = *opv_desc_vector_p;
143 		for (j=0; them[i]->opv_desc_ops[j].opve_op; j++) {
144 			opve_descp = &(them[i]->opv_desc_ops[j]);
145 
146 			/*
147 			 * Sanity check:  is this operation listed
148 			 * in the list of operations?  We check this
149 			 * by seeing if its offest is zero.  Since
150 			 * the default routine should always be listed
151 			 * first, it should be the only one with a zero
152 			 * offset.  Any other operation with a zero
153 			 * offset is probably not listed in
154 			 * vfs_op_descs, and so is probably an error.
155 			 *
156 			 * A panic here means the layer programmer
157 			 * has committed the all-too common bug
158 			 * of adding a new operation to the layer's
159 			 * list of vnode operations but
160 			 * not adding the operation to the system-wide
161 			 * list of supported operations.
162 			 */
163 			if (opve_descp->opve_op->vdesc_offset == 0 &&
164 				    opve_descp->opve_op->vdesc_offset !=
165 				    	VOFFSET(vop_default)) {
166 				printf("operation %s not listed in %s.\n",
167 				    opve_descp->opve_op->vdesc_name,
168 				    "vfs_op_descs");
169 				panic ("vfs_opv_init: bad operation");
170 			}
171 			/*
172 			 * Fill in this entry.
173 			 */
174 			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
175 					opve_descp->opve_impl;
176 		}
177 	}
178 	/*
179 	 * Finally, go back and replace unfilled routines
180 	 * with their default.  (Sigh, an O(n^3) algorithm.  I
181 	 * could make it better, but that'd be work, and n is small.)
182 	 */
183 	for (i = 0; them[i]; i++) {
184 		opv_desc_vector = *(them[i]->opv_desc_vector_p);
185 		/*
186 		 * Force every operations vector to have a default routine.
187 		 */
188 		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
189 			panic("vfs_opv_init: operation vector without default routine.");
190 		}
191 		for (k = 0; k<vfs_opv_numops; k++)
192 			if (opv_desc_vector[k] == NULL)
193 				opv_desc_vector[k] =
194 					opv_desc_vector[VOFFSET(vop_default)];
195 	}
196 }
197 
198 /*
199  * Initialize known vnode operations vectors.
200  */
201 static void
202 vfs_op_init()
203 {
204 	int i;
205 
206 	DODEBUG(printf("Vnode_interface_init.\n"));
207 	/*
208 	 * Set all vnode vectors to a well known value.
209 	 */
210 	for (i = 0; vfs_opv_descs[i]; i++)
211 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
212 	/*
213 	 * Figure out how many ops there are by counting the table,
214 	 * and assign each its offset.
215 	 */
216 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
217 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
218 		vfs_opv_numops++;
219 	}
220 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
221 }
222 
223 /*
224  * Routines having to do with the management of the vnode table.
225  */
226 extern struct vnodeops dead_vnodeops;
227 extern struct vnodeops spec_vnodeops;
228 struct vattr va_null;
229 
230 /*
231  * Initialize the vnode structures and initialize each file system type.
232  */
233 /* ARGSUSED*/
234 static void
235 vfsinit(dummy)
236 	void *dummy;
237 {
238 	struct vfsconf **vfc;
239 	int maxtypenum;
240 
241 	/*
242 	 * Initialize the vnode table
243 	 */
244 	vntblinit();
245 	/*
246 	 * Initialize the vnode name cache
247 	 */
248 	nchinit();
249 	/*
250 	 * Build vnode operation vectors.
251 	 */
252 	vfs_op_init();
253 	vfs_opv_init(vfs_opv_descs);   /* finish the job */
254 	/*
255 	 * Initialize each file system type.
256 	 */
257 	vattr_null(&va_null);
258 	maxtypenum = 0;
259 	vfc = (struct vfsconf **)vfs_set.ls_items;
260 	vfsconf = *vfc;		/* simulate Lite2 vfsconf array */
261 	while (*vfc) {
262 		struct vfsconf *vfsp = *vfc;
263 
264 		vfc++;
265 		vfsp->vfc_next = *vfc;
266 		if (maxtypenum <= vfsp->vfc_typenum)
267 			maxtypenum = vfsp->vfc_typenum + 1;
268 		(*vfsp->vfc_vfsops->vfs_init)(vfsp);
269 	}
270 	/* next vfc_typenum to be used */
271 	maxvfsconf = maxtypenum;
272 }
273 
274 /*
275  * kernel related system variables.
276  */
277 
278 /*
279  * This goop is here to support a loadable NFS module... grumble...
280  */
281 int (*lease_check_hook) __P((struct vop_lease_args *))
282      = 0;
283 void (*lease_updatetime) __P((int))
284      = 0;
285 
286 int
287 lease_check(ap)
288 	struct vop_lease_args /* {
289 		struct vnode *a_vp;
290 		struct proc *a_p;
291 		struct ucred *a_cred;
292 		int a_flag;
293 	} */ *ap;
294 {
295     if (lease_check_hook)
296 	return (*lease_check_hook)(ap);
297     else
298 	return 0;
299 }
300