vfs_init.c revision 8876
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1989, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed
61573Srgrimes * to Berkeley by John Heidemann of the UCLA Ficus project.
71573Srgrimes *
81573Srgrimes * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
91573Srgrimes *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
181573Srgrimes * 3. All advertising materials mentioning features or use of this software
191573Srgrimes *    must display the following acknowledgement:
201573Srgrimes *	This product includes software developed by the University of
211573Srgrimes *	California, Berkeley and its contributors.
221573Srgrimes * 4. Neither the name of the University nor the names of its contributors
231573Srgrimes *    may be used to endorse or promote products derived from this software
241573Srgrimes *    without specific prior written permission.
251573Srgrimes *
261573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3350476Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35171195Sscf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361573Srgrimes * SUCH DAMAGE.
371573Srgrimes *
381573Srgrimes *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
391573Srgrimes * $Id: vfs_init.c,v 1.9 1994/10/20 00:48:28 wollman Exp $
401573Srgrimes */
411573Srgrimes
421573Srgrimes
431573Srgrimes#include <sys/param.h>
4459460Sphantom#include <sys/systm.h>
4559460Sphantom#include <sys/kernel.h>
461573Srgrimes#include <sys/mount.h>
4783206Sasmodai#include <sys/time.h>
481573Srgrimes#include <sys/vnode.h>
491573Srgrimes#include <sys/stat.h>
501573Srgrimes#include <sys/namei.h>
511573Srgrimes#include <sys/ucred.h>
521573Srgrimes#include <sys/buf.h>
53171195Sscf#include <sys/errno.h>
54171195Sscf#include <sys/malloc.h>
551573Srgrimes#include <sys/proc.h>
561573Srgrimes#include <vm/vm.h>
571573Srgrimes#include <sys/sysctl.h>
581573Srgrimes
591573Srgrimes/*
601573Srgrimes * Sigh, such primitive tools are these...
611573Srgrimes */
621573Srgrimes#if 0
631573Srgrimes#define DODEBUG(A) A
64108040Sru#else
65163262Strhodes#define DODEBUG(A)
66163262Strhodes#endif
67163262Strhodes
68163262Strhodesstruct vfsconf void_vfsconf;
691573Srgrimes
701573Srgrimesextern struct linker_set vfs_opv_descs_;
711573Srgrimes#define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
721573Srgrimes
73108040Sruextern struct linker_set vfs_set;
741573Srgrimesstruct vfsops *vfssw[MOUNT_MAXTYPE + 1];
751573Srgrimesstruct vfsconf *vfsconf[MOUNT_MAXTYPE + 1];
76108040Sru
771573Srgrimesextern struct vnodeop_desc *vfs_op_descs[];
781573Srgrimes				/* and the operations they perform */
79108040Sru/*
801573Srgrimes * This code doesn't work if the defn is **vnodop_defns with cc.
81108040Sru * The problem is because of the compiler sometimes putting in an
821573Srgrimes * extra level of indirection for arrays.  It's an interesting
83108040Sru * "feature" of C.
84163108Strhodes */
851573Srgrimesint vfs_opv_numops;
861573Srgrimes
87108040Srutypedef int (*PFI)(); /* the standard Pointer to a Function returning an Int */
881573Srgrimes
891573Srgrimes/*
901573Srgrimes * A miscellaneous routine.
91171195Sscf * A generic "default" routine that just returns an error.
92171195Sscf */
93171195Sscfint
94171195Sscfvn_default_error()
95171195Sscf{
96171195Sscf
97171195Sscf	return (EOPNOTSUPP);
98171195Sscf}
99171195Sscf
100171195Sscf/*
101171195Sscf * vfs_init.c
102171195Sscf *
1031573Srgrimes * Allocate and fill in operations vectors.
1041573Srgrimes *
1051573Srgrimes * An undocumented feature of this approach to defining operations is that
1061573Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations
1071573Srgrimes * vector. This allows third parties to extend the set of operations
1081573Srgrimes * supported by another layer in a binary compatibile way. For example,
1091573Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry
110200195Sscf * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
111200195Sscf * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
112200195Sscf * listing those new operations Ficus adds to NFS, all without modifying the
113200195Sscf * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
114200195Sscf * that is a(whole)nother story.) This is a feature.
115200195Sscf */
116200195Sscfvoid
117200195Sscfvfs_opv_init(struct vnodeopv_desc **them)
118200195Sscf{
1191573Srgrimes	int i, j, k;
120131728Shmp	int (***opv_desc_vector_p)();
121131728Shmp	int (**opv_desc_vector)();
122131754Sru	struct vnodeopv_entry_desc *opve_descp;
123131754Sru
124131754Sru	/*
125131728Shmp	 * Allocate the dynamic vectors and fill them in.
126131728Shmp	 */
127131728Shmp	for (i=0; them[i]; i++) {
128131728Shmp		opv_desc_vector_p = them[i]->opv_desc_vector_p;
129131728Shmp		/*
130131728Shmp		 * Allocate and init the vector, if it needs it.
131171195Sscf		 * Also handle backwards compatibility.
1321573Srgrimes		 */
13360075Sphantom		if (*opv_desc_vector_p == NULL) {
134171195Sscf			/* XXX - shouldn't be M_VNODE */
1351573Srgrimes			MALLOC(*opv_desc_vector_p, PFI*,
136171195Sscf			       vfs_opv_numops*sizeof(PFI), M_VNODE, M_WAITOK);
1371573Srgrimes			bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
1381573Srgrimes			DODEBUG(printf("vector at %x allocated\n",
139171195Sscf			    opv_desc_vector_p));
140171195Sscf		}
141171195Sscf		opv_desc_vector = *opv_desc_vector_p;
142171195Sscf		for (j=0; them[i]->opv_desc_ops[j].opve_op; j++) {
143171195Sscf			opve_descp = &(them[i]->opv_desc_ops[j]);
144171195Sscf
145171195Sscf			/*
146171195Sscf			 * Sanity check:  is this operation listed
147171195Sscf			 * in the list of operations?  We check this
148171195Sscf			 * by seeing if its offest is zero.  Since
1491573Srgrimes			 * the default routine should always be listed
150171195Sscf			 * first, it should be the only one with a zero
151171195Sscf			 * offset.  Any other operation with a zero
152171195Sscf			 * offset is probably not listed in
153171195Sscf			 * vfs_op_descs, and so is probably an error.
154171195Sscf			 *
155235575Sgjb			 * A panic here means the layer programmer
156235575Sgjb			 * has committed the all-too common bug
157171195Sscf			 * of adding a new operation to the layer's
158171195Sscf			 * list of vnode operations but
159171195Sscf			 * not adding the operation to the system-wide
160171195Sscf			 * list of supported operations.
161171195Sscf			 */
162171195Sscf			if (opve_descp->opve_op->vdesc_offset == 0 &&
163171195Sscf				    opve_descp->opve_op->vdesc_offset !=
164171195Sscf				    	VOFFSET(vop_default)) {
165171195Sscf				printf("operation %s not listed in %s.\n",
166171195Sscf				    opve_descp->opve_op->vdesc_name,
167171195Sscf				    "vfs_op_descs");
168171195Sscf				panic ("vfs_opv_init: bad operation");
169171195Sscf			}
170171195Sscf			/*
1711573Srgrimes			 * Fill in this entry.
1721573Srgrimes			 */
1731573Srgrimes			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
1741573Srgrimes					opve_descp->opve_impl;
1751573Srgrimes		}
1761573Srgrimes	}
1771573Srgrimes	/*
1781573Srgrimes	 * Finally, go back and replace unfilled routines
1791573Srgrimes	 * with their default.  (Sigh, an O(n^3) algorithm.  I
1801573Srgrimes	 * could make it better, but that'd be work, and n is small.)
1811573Srgrimes	 */
18273088Sru	for (i = 0; them[i]; i++) {
183171195Sscf		opv_desc_vector = *(them[i]->opv_desc_vector_p);
184171195Sscf		/*
185171195Sscf		 * Force every operations vector to have a default routine.
186171195Sscf		 */
187171195Sscf		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
188171195Sscf			panic("vfs_opv_init: operation vector without default routine.");
189171195Sscf		}
190140505Sru		for (k = 0; k<vfs_opv_numops; k++)
191140505Sru			if (opv_desc_vector[k] == NULL)
192140505Sru				opv_desc_vector[k] =
193140505Sru					opv_desc_vector[VOFFSET(vop_default)];
194140505Sru	}
195140505Sru}
196140505Sru
197140505Sru/*
198140505Sru * Initialize known vnode operations vectors.
199140505Sru */
200140505Sruvoid
201171195Sscfvfs_op_init()
202171195Sscf{
203171195Sscf	int i;
204171195Sscf
205237216Seadler	DODEBUG(printf("Vnode_interface_init.\n"));
206171195Sscf	/*
207171195Sscf	 * Set all vnode vectors to a well known value.
208171195Sscf	 */
209171195Sscf	for (i = 0; vfs_opv_descs[i]; i++)
210171195Sscf		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
211171195Sscf	/*
212171195Sscf	 * Figure out how many ops there are by counting the table,
213171195Sscf	 * and assign each its offset.
21456652Sarchie	 */
21556652Sarchie	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
21656652Sarchie		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
217171195Sscf		vfs_opv_numops++;
218108040Sru	}
219171195Sscf	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
220108040Sru}
221131504Sru
222131504Sru/*
22368716Sru * Routines having to do with the management of the vnode table.
224171195Sscf */
22556652Sarchieextern struct vnodeops dead_vnodeops;
226108040Sruextern struct vnodeops spec_vnodeops;
22756652Sarchieextern void vclean();
228131504Srustruct vattr va_null;
229131504Sru
23056652Sarchie/*
231 * Initialize the vnode structures and initialize each file system type.
232 */
233void
234vfsinit()
235{
236	struct vfsops **vfsp;
237	struct vfsconf **vfc;
238	int i;
239
240	/*
241	 * Initialize the VFS switch table
242	 */
243	for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
244		vfsconf[i] = &void_vfsconf;
245	}
246
247	vfc = (struct vfsconf **)vfs_set.ls_items;
248	while(*vfc) {
249		vfssw[(**vfc).vfc_index] = (**vfc).vfc_vfsops;
250		vfsconf[(**vfc).vfc_index] = *vfc;
251		vfc++;
252	}
253
254	/*
255	 * Initialize the vnode table
256	 */
257	vntblinit();
258	/*
259	 * Initialize the vnode name cache
260	 */
261	nchinit();
262	/*
263	 * Build vnode operation vectors.
264	 */
265	vfs_op_init();
266	vfs_opv_init(vfs_opv_descs);   /* finish the job */
267	/*
268	 * Initialize each file system type.
269	 */
270	vattr_null(&va_null);
271	for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
272		if (*vfsp == NULL)
273			continue;
274		(*(*vfsp)->vfs_init)();
275	}
276}
277
278/*
279 * kernel related system variables.
280 */
281int
282fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
283	int *name;
284	u_int namelen;
285	void *oldp;
286	size_t *oldlenp;
287	void *newp;
288	size_t newlen;
289	struct proc *p;
290{
291	int i;
292	int error;
293	int buflen = *oldlenp;
294	caddr_t where = oldp, start = oldp;
295
296	switch (name[0]) {
297	case FS_VFSCONF:
298		if (namelen != 1) return ENOTDIR;
299
300		if (oldp == NULL) {
301			*oldlenp = (MOUNT_MAXTYPE+1) * sizeof(struct vfsconf);
302			return 0;
303		}
304		if (newp) {
305			return EINVAL;
306		}
307
308		for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
309			if(buflen < sizeof *vfsconf[i]) {
310				*oldlenp = where - start;
311				return ENOMEM;
312			}
313
314			error = copyout(vfsconf[i], where, sizeof *vfsconf[i]);
315			if(error)
316				return error;
317			where += sizeof *vfsconf[i];
318			buflen -= sizeof *vfsconf[i];
319		}
320		*oldlenp = where - start;
321		return 0;
322
323	default:
324		if(namelen < 1) return EINVAL;
325
326		i = name[0];
327
328		if(i <= MOUNT_MAXTYPE
329		   && vfssw[i]
330		   && vfssw[i]->vfs_sysctl) {
331			return vfssw[i]->vfs_sysctl(name + 1, namelen - 1,
332						    oldp, oldlenp,
333						    newp, newlen, p);
334		}
335
336		return (EOPNOTSUPP);
337	}
338	/* NOTREACHED */
339}
340
341/*
342 * This goop is here to support a loadable NFS module... grumble...
343 */
344void (*lease_check) __P((struct vnode *, struct proc *, struct ucred *, int))
345     = 0;
346void (*lease_updatetime) __P((int))
347     = 0;
348
349