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