xref: /freebsd/sys/kern/vfs_init.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
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.11 1995/08/28 09:18:55 julian 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 #include <vm/vm.h>
57 #include <sys/sysctl.h>
58 
59 /*
60  * System initialization
61  */
62 
63 static void vfsinit __P((void *));
64 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
65 
66 /*
67  * Sigh, such primitive tools are these...
68  */
69 #if 0
70 #define DODEBUG(A) A
71 #else
72 #define DODEBUG(A)
73 #endif
74 
75 struct vfsconf void_vfsconf;
76 
77 extern struct linker_set vfs_opv_descs_;
78 #define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
79 
80 extern struct linker_set vfs_set;
81 struct vfsops *vfssw[MOUNT_MAXTYPE + 1];
82 struct vfsconf *vfsconf[MOUNT_MAXTYPE + 1];
83 
84 extern struct vnodeop_desc *vfs_op_descs[];
85 				/* and the operations they perform */
86 /*
87  * This code doesn't work if the defn is **vnodop_defns with cc.
88  * The problem is because of the compiler sometimes putting in an
89  * extra level of indirection for arrays.  It's an interesting
90  * "feature" of C.
91  */
92 int vfs_opv_numops;
93 
94 typedef int (*PFI)(); /* the standard Pointer to a Function returning an Int */
95 
96 /*
97  * A miscellaneous routine.
98  * A generic "default" routine that just returns an error.
99  */
100 int
101 vn_default_error()
102 {
103 
104 	return (EOPNOTSUPP);
105 }
106 
107 /*
108  * vfs_init.c
109  *
110  * Allocate and fill in operations vectors.
111  *
112  * An undocumented feature of this approach to defining operations is that
113  * there can be multiple entries in vfs_opv_descs for the same operations
114  * vector. This allows third parties to extend the set of operations
115  * supported by another layer in a binary compatibile way. For example,
116  * assume that NFS needed to be modified to support Ficus. NFS has an entry
117  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
118  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
119  * listing those new operations Ficus adds to NFS, all without modifying the
120  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
121  * that is a(whole)nother story.) This is a feature.
122  */
123 void
124 vfs_opv_init(struct vnodeopv_desc **them)
125 {
126 	int i, j, k;
127 	int (***opv_desc_vector_p)();
128 	int (**opv_desc_vector)();
129 	struct vnodeopv_entry_desc *opve_descp;
130 
131 	/*
132 	 * Allocate the dynamic vectors and fill them in.
133 	 */
134 	for (i=0; them[i]; i++) {
135 		opv_desc_vector_p = them[i]->opv_desc_vector_p;
136 		/*
137 		 * Allocate and init the vector, if it needs it.
138 		 * Also handle backwards compatibility.
139 		 */
140 		if (*opv_desc_vector_p == NULL) {
141 			/* XXX - shouldn't be M_VNODE */
142 			MALLOC(*opv_desc_vector_p, PFI*,
143 			       vfs_opv_numops*sizeof(PFI), M_VNODE, M_WAITOK);
144 			bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
145 			DODEBUG(printf("vector at %x allocated\n",
146 			    opv_desc_vector_p));
147 		}
148 		opv_desc_vector = *opv_desc_vector_p;
149 		for (j=0; them[i]->opv_desc_ops[j].opve_op; j++) {
150 			opve_descp = &(them[i]->opv_desc_ops[j]);
151 
152 			/*
153 			 * Sanity check:  is this operation listed
154 			 * in the list of operations?  We check this
155 			 * by seeing if its offest is zero.  Since
156 			 * the default routine should always be listed
157 			 * first, it should be the only one with a zero
158 			 * offset.  Any other operation with a zero
159 			 * offset is probably not listed in
160 			 * vfs_op_descs, and so is probably an error.
161 			 *
162 			 * A panic here means the layer programmer
163 			 * has committed the all-too common bug
164 			 * of adding a new operation to the layer's
165 			 * list of vnode operations but
166 			 * not adding the operation to the system-wide
167 			 * list of supported operations.
168 			 */
169 			if (opve_descp->opve_op->vdesc_offset == 0 &&
170 				    opve_descp->opve_op->vdesc_offset !=
171 				    	VOFFSET(vop_default)) {
172 				printf("operation %s not listed in %s.\n",
173 				    opve_descp->opve_op->vdesc_name,
174 				    "vfs_op_descs");
175 				panic ("vfs_opv_init: bad operation");
176 			}
177 			/*
178 			 * Fill in this entry.
179 			 */
180 			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
181 					opve_descp->opve_impl;
182 		}
183 	}
184 	/*
185 	 * Finally, go back and replace unfilled routines
186 	 * with their default.  (Sigh, an O(n^3) algorithm.  I
187 	 * could make it better, but that'd be work, and n is small.)
188 	 */
189 	for (i = 0; them[i]; i++) {
190 		opv_desc_vector = *(them[i]->opv_desc_vector_p);
191 		/*
192 		 * Force every operations vector to have a default routine.
193 		 */
194 		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
195 			panic("vfs_opv_init: operation vector without default routine.");
196 		}
197 		for (k = 0; k<vfs_opv_numops; k++)
198 			if (opv_desc_vector[k] == NULL)
199 				opv_desc_vector[k] =
200 					opv_desc_vector[VOFFSET(vop_default)];
201 	}
202 }
203 
204 /*
205  * Initialize known vnode operations vectors.
206  */
207 void
208 vfs_op_init()
209 {
210 	int i;
211 
212 	DODEBUG(printf("Vnode_interface_init.\n"));
213 	/*
214 	 * Set all vnode vectors to a well known value.
215 	 */
216 	for (i = 0; vfs_opv_descs[i]; i++)
217 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
218 	/*
219 	 * Figure out how many ops there are by counting the table,
220 	 * and assign each its offset.
221 	 */
222 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
223 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
224 		vfs_opv_numops++;
225 	}
226 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
227 }
228 
229 /*
230  * Routines having to do with the management of the vnode table.
231  */
232 extern struct vnodeops dead_vnodeops;
233 extern struct vnodeops spec_vnodeops;
234 extern void vclean();
235 struct vattr va_null;
236 
237 /*
238  * Initialize the vnode structures and initialize each file system type.
239  */
240 /* ARGSUSED*/
241 static void
242 vfsinit(udata)
243 	void *udata;		/* not used*/
244 {
245 	struct vfsops **vfsp;
246 	struct vfsconf **vfc;
247 	int i;
248 
249 	/*
250 	 * Initialize the VFS switch table
251 	 */
252 	for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
253 		vfsconf[i] = &void_vfsconf;
254 	}
255 
256 	vfc = (struct vfsconf **)vfs_set.ls_items;
257 	while(*vfc) {
258 		vfssw[(**vfc).vfc_index] = (**vfc).vfc_vfsops;
259 		vfsconf[(**vfc).vfc_index] = *vfc;
260 		vfc++;
261 	}
262 
263 	/*
264 	 * Initialize the vnode table
265 	 */
266 	vntblinit();
267 	/*
268 	 * Initialize the vnode name cache
269 	 */
270 	nchinit();
271 	/*
272 	 * Build vnode operation vectors.
273 	 */
274 	vfs_op_init();
275 	vfs_opv_init(vfs_opv_descs);   /* finish the job */
276 	/*
277 	 * Initialize each file system type.
278 	 */
279 	vattr_null(&va_null);
280 	for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
281 		if (*vfsp == NULL)
282 			continue;
283 		(*(*vfsp)->vfs_init)();
284 	}
285 }
286 
287 /*
288  * kernel related system variables.
289  */
290 int
291 fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
292 	int *name;
293 	u_int namelen;
294 	void *oldp;
295 	size_t *oldlenp;
296 	void *newp;
297 	size_t newlen;
298 	struct proc *p;
299 {
300 	int i;
301 	int error;
302 	int buflen = *oldlenp;
303 	caddr_t where = oldp, start = oldp;
304 
305 	switch (name[0]) {
306 	case FS_VFSCONF:
307 		if (namelen != 1) return ENOTDIR;
308 
309 		if (oldp == NULL) {
310 			*oldlenp = (MOUNT_MAXTYPE+1) * sizeof(struct vfsconf);
311 			return 0;
312 		}
313 		if (newp) {
314 			return EINVAL;
315 		}
316 
317 		for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
318 			if(buflen < sizeof *vfsconf[i]) {
319 				*oldlenp = where - start;
320 				return ENOMEM;
321 			}
322 
323 			error = copyout(vfsconf[i], where, sizeof *vfsconf[i]);
324 			if(error)
325 				return error;
326 			where += sizeof *vfsconf[i];
327 			buflen -= sizeof *vfsconf[i];
328 		}
329 		*oldlenp = where - start;
330 		return 0;
331 
332 	default:
333 		if(namelen < 1) return EINVAL;
334 
335 		i = name[0];
336 
337 		if(i <= MOUNT_MAXTYPE
338 		   && vfssw[i]
339 		   && vfssw[i]->vfs_sysctl) {
340 			return vfssw[i]->vfs_sysctl(name + 1, namelen - 1,
341 						    oldp, oldlenp,
342 						    newp, newlen, p);
343 		}
344 
345 		return (EOPNOTSUPP);
346 	}
347 	/* NOTREACHED */
348 }
349 
350 /*
351  * This goop is here to support a loadable NFS module... grumble...
352  */
353 void (*lease_check) __P((struct vnode *, struct proc *, struct ucred *, int))
354      = 0;
355 void (*lease_updatetime) __P((int))
356      = 0;
357 
358