vfs_init.c revision 12577
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.15 1995/12/02 17:10:53 bde 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
59extern void	vfs_op_init __P((void));
60
61static void vfsinit __P((void *));
62SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
63
64void vfs_op_init __P((void));
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
75struct vfsconf void_vfsconf;
76
77extern struct linker_set vfs_opv_descs_;
78#define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
79
80extern struct linker_set vfs_set;
81struct vfsops *vfssw[MOUNT_MAXTYPE + 1];
82struct vfsconf *vfsconf[MOUNT_MAXTYPE + 1];
83
84extern 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 */
92int vfs_opv_numops;
93
94/*
95 * A miscellaneous routine.
96 * A generic "default" routine that just returns an error.
97 */
98int
99vn_default_error()
100{
101
102	return (EOPNOTSUPP);
103}
104
105/*
106 * vfs_init.c
107 *
108 * Allocate and fill in operations vectors.
109 *
110 * An undocumented feature of this approach to defining operations is that
111 * there can be multiple entries in vfs_opv_descs for the same operations
112 * vector. This allows third parties to extend the set of operations
113 * supported by another layer in a binary compatibile way. For example,
114 * assume that NFS needed to be modified to support Ficus. NFS has an entry
115 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
116 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
117 * listing those new operations Ficus adds to NFS, all without modifying the
118 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
119 * that is a(whole)nother story.) This is a feature.
120 */
121void
122vfs_opv_init(struct vnodeopv_desc **them)
123{
124	int i, j, k;
125	vop_t ***opv_desc_vector_p;
126	vop_t **opv_desc_vector;
127	struct vnodeopv_entry_desc *opve_descp;
128
129	/*
130	 * Allocate the dynamic vectors and fill them in.
131	 */
132	for (i=0; them[i]; i++) {
133		opv_desc_vector_p = them[i]->opv_desc_vector_p;
134		/*
135		 * Allocate and init the vector, if it needs it.
136		 * Also handle backwards compatibility.
137		 */
138		if (*opv_desc_vector_p == NULL) {
139			/* XXX - shouldn't be M_VNODE */
140			MALLOC(*opv_desc_vector_p, vop_t **,
141			       vfs_opv_numops * sizeof(vop_t *), M_VNODE,
142			       M_WAITOK);
143			bzero(*opv_desc_vector_p,
144			      vfs_opv_numops * sizeof(vop_t *));
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 */
207void
208vfs_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 */
232extern struct vnodeops dead_vnodeops;
233extern struct vnodeops spec_vnodeops;
234struct vattr va_null;
235
236/*
237 * Initialize the vnode structures and initialize each file system type.
238 */
239/* ARGSUSED*/
240static void
241vfsinit(dummy)
242	void *dummy;
243{
244	struct vfsops **vfsp;
245	struct vfsconf **vfc;
246	int i;
247
248	/*
249	 * Initialize the VFS switch table
250	 */
251	for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
252		vfsconf[i] = &void_vfsconf;
253	}
254
255	vfc = (struct vfsconf **)vfs_set.ls_items;
256	while(*vfc) {
257		vfssw[(**vfc).vfc_index] = (**vfc).vfc_vfsops;
258		vfsconf[(**vfc).vfc_index] = *vfc;
259		vfc++;
260	}
261
262	/*
263	 * Initialize the vnode table
264	 */
265	vntblinit();
266	/*
267	 * Initialize the vnode name cache
268	 */
269	nchinit();
270	/*
271	 * Build vnode operation vectors.
272	 */
273	vfs_op_init();
274	vfs_opv_init(vfs_opv_descs);   /* finish the job */
275	/*
276	 * Initialize each file system type.
277	 */
278	vattr_null(&va_null);
279	for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
280		if (*vfsp == NULL)
281			continue;
282		(*(*vfsp)->vfs_init)();
283	}
284}
285
286/*
287 * kernel related system variables.
288 */
289
290static int
291sysctl_fs_vfsconf SYSCTL_HANDLER_ARGS
292{
293	int i, error;
294
295	if (req->newptr)
296		return EINVAL;
297	for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
298		error = SYSCTL_OUT(req, vfsconf[i], sizeof *vfsconf[i]);
299		if(error)
300			return error;
301	}
302	return (error);
303
304}
305
306SYSCTL_PROC(_fs, FS_VFSCONF, vfsconf, CTLTYPE_OPAQUE|CTLFLAG_RD,
307	0, 0, sysctl_fs_vfsconf, "");
308
309/*
310 * This goop is here to support a loadable NFS module... grumble...
311 */
312void (*lease_check) __P((struct vnode *, struct proc *, struct ucred *, int))
313     = 0;
314void (*lease_updatetime) __P((int))
315     = 0;
316
317