vfs_init.c revision 12158
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed
61541Srgrimes * to Berkeley by John Heidemann of the UCLA Ficus project.
71541Srgrimes *
81541Srgrimes * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
381541Srgrimes *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
3912158Sbde * $Id: vfs_init.c,v 1.12 1995/09/09 18:10:16 davidg Exp $
401541Srgrimes */
411541Srgrimes
421541Srgrimes
431541Srgrimes#include <sys/param.h>
442112Swollman#include <sys/systm.h>
452946Swollman#include <sys/kernel.h>
461541Srgrimes#include <sys/mount.h>
471541Srgrimes#include <sys/time.h>
481541Srgrimes#include <sys/vnode.h>
491541Srgrimes#include <sys/stat.h>
501541Srgrimes#include <sys/namei.h>
511541Srgrimes#include <sys/ucred.h>
521541Srgrimes#include <sys/buf.h>
531541Srgrimes#include <sys/errno.h>
541541Srgrimes#include <sys/malloc.h>
552946Swollman#include <sys/proc.h>
562946Swollman#include <vm/vm.h>
572946Swollman#include <sys/sysctl.h>
581541Srgrimes
591541Srgrimes/*
6010358Sjulian * System initialization
6110358Sjulian */
6210358Sjulian
6310653Sdgstatic void vfsinit __P((void *));
6410358SjulianSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
6510358Sjulian
6610358Sjulian/*
671541Srgrimes * Sigh, such primitive tools are these...
681541Srgrimes */
691541Srgrimes#if 0
701541Srgrimes#define DODEBUG(A) A
711541Srgrimes#else
721541Srgrimes#define DODEBUG(A)
731541Srgrimes#endif
741541Srgrimes
752946Swollmanstruct vfsconf void_vfsconf;
762946Swollman
772946Swollmanextern struct linker_set vfs_opv_descs_;
782946Swollman#define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
792946Swollman
802946Swollmanextern struct linker_set vfs_set;
812946Swollmanstruct vfsops *vfssw[MOUNT_MAXTYPE + 1];
822946Swollmanstruct vfsconf *vfsconf[MOUNT_MAXTYPE + 1];
832946Swollman
841541Srgrimesextern struct vnodeop_desc *vfs_op_descs[];
851541Srgrimes				/* and the operations they perform */
861541Srgrimes/*
871541Srgrimes * This code doesn't work if the defn is **vnodop_defns with cc.
881541Srgrimes * The problem is because of the compiler sometimes putting in an
891541Srgrimes * extra level of indirection for arrays.  It's an interesting
901541Srgrimes * "feature" of C.
911541Srgrimes */
921541Srgrimesint vfs_opv_numops;
931541Srgrimes
941541Srgrimes/*
951541Srgrimes * A miscellaneous routine.
961541Srgrimes * A generic "default" routine that just returns an error.
971541Srgrimes */
981541Srgrimesint
991541Srgrimesvn_default_error()
1001541Srgrimes{
1011541Srgrimes
1021541Srgrimes	return (EOPNOTSUPP);
1031541Srgrimes}
1041541Srgrimes
1051541Srgrimes/*
1061541Srgrimes * vfs_init.c
1071541Srgrimes *
1081541Srgrimes * Allocate and fill in operations vectors.
1091541Srgrimes *
1101541Srgrimes * An undocumented feature of this approach to defining operations is that
1111541Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations
1121541Srgrimes * vector. This allows third parties to extend the set of operations
1131541Srgrimes * supported by another layer in a binary compatibile way. For example,
1141541Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry
1151541Srgrimes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
1161541Srgrimes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
1171541Srgrimes * listing those new operations Ficus adds to NFS, all without modifying the
1181541Srgrimes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
1191541Srgrimes * that is a(whole)nother story.) This is a feature.
1201541Srgrimes */
1211541Srgrimesvoid
1222946Swollmanvfs_opv_init(struct vnodeopv_desc **them)
1231541Srgrimes{
1241541Srgrimes	int i, j, k;
12512158Sbde	vop_t ***opv_desc_vector_p;
12612158Sbde	vop_t **opv_desc_vector;
1271541Srgrimes	struct vnodeopv_entry_desc *opve_descp;
1281541Srgrimes
1291541Srgrimes	/*
1301541Srgrimes	 * Allocate the dynamic vectors and fill them in.
1311541Srgrimes	 */
1322946Swollman	for (i=0; them[i]; i++) {
1332946Swollman		opv_desc_vector_p = them[i]->opv_desc_vector_p;
1341541Srgrimes		/*
1351541Srgrimes		 * Allocate and init the vector, if it needs it.
1361541Srgrimes		 * Also handle backwards compatibility.
1371541Srgrimes		 */
1381541Srgrimes		if (*opv_desc_vector_p == NULL) {
1391541Srgrimes			/* XXX - shouldn't be M_VNODE */
14012158Sbde			MALLOC(*opv_desc_vector_p, vop_t **,
14112158Sbde			       vfs_opv_numops * sizeof(vop_t *), M_VNODE,
14212158Sbde			       M_WAITOK);
14312158Sbde			bzero(*opv_desc_vector_p,
14412158Sbde			      vfs_opv_numops * sizeof(vop_t *));
1451541Srgrimes			DODEBUG(printf("vector at %x allocated\n",
1461541Srgrimes			    opv_desc_vector_p));
1471541Srgrimes		}
1481541Srgrimes		opv_desc_vector = *opv_desc_vector_p;
1492946Swollman		for (j=0; them[i]->opv_desc_ops[j].opve_op; j++) {
1502946Swollman			opve_descp = &(them[i]->opv_desc_ops[j]);
1511541Srgrimes
1521541Srgrimes			/*
1531541Srgrimes			 * Sanity check:  is this operation listed
1541541Srgrimes			 * in the list of operations?  We check this
1551541Srgrimes			 * by seeing if its offest is zero.  Since
1561541Srgrimes			 * the default routine should always be listed
1571541Srgrimes			 * first, it should be the only one with a zero
1581541Srgrimes			 * offset.  Any other operation with a zero
1591541Srgrimes			 * offset is probably not listed in
1601541Srgrimes			 * vfs_op_descs, and so is probably an error.
1611541Srgrimes			 *
1621541Srgrimes			 * A panic here means the layer programmer
1631541Srgrimes			 * has committed the all-too common bug
1641541Srgrimes			 * of adding a new operation to the layer's
1651541Srgrimes			 * list of vnode operations but
1661541Srgrimes			 * not adding the operation to the system-wide
1671541Srgrimes			 * list of supported operations.
1681541Srgrimes			 */
1691541Srgrimes			if (opve_descp->opve_op->vdesc_offset == 0 &&
1701541Srgrimes				    opve_descp->opve_op->vdesc_offset !=
1711541Srgrimes				    	VOFFSET(vop_default)) {
1721541Srgrimes				printf("operation %s not listed in %s.\n",
1731541Srgrimes				    opve_descp->opve_op->vdesc_name,
1741541Srgrimes				    "vfs_op_descs");
1751541Srgrimes				panic ("vfs_opv_init: bad operation");
1761541Srgrimes			}
1771541Srgrimes			/*
1781541Srgrimes			 * Fill in this entry.
1791541Srgrimes			 */
1801541Srgrimes			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
1811541Srgrimes					opve_descp->opve_impl;
1821541Srgrimes		}
1831541Srgrimes	}
1841541Srgrimes	/*
1851541Srgrimes	 * Finally, go back and replace unfilled routines
1861541Srgrimes	 * with their default.  (Sigh, an O(n^3) algorithm.  I
1871541Srgrimes	 * could make it better, but that'd be work, and n is small.)
1881541Srgrimes	 */
1892946Swollman	for (i = 0; them[i]; i++) {
1902946Swollman		opv_desc_vector = *(them[i]->opv_desc_vector_p);
1911541Srgrimes		/*
1921541Srgrimes		 * Force every operations vector to have a default routine.
1931541Srgrimes		 */
1941541Srgrimes		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
1951541Srgrimes			panic("vfs_opv_init: operation vector without default routine.");
1961541Srgrimes		}
1971541Srgrimes		for (k = 0; k<vfs_opv_numops; k++)
1981541Srgrimes			if (opv_desc_vector[k] == NULL)
1998876Srgrimes				opv_desc_vector[k] =
2001541Srgrimes					opv_desc_vector[VOFFSET(vop_default)];
2011541Srgrimes	}
2021541Srgrimes}
2031541Srgrimes
2041541Srgrimes/*
2051541Srgrimes * Initialize known vnode operations vectors.
2061541Srgrimes */
2071541Srgrimesvoid
2081541Srgrimesvfs_op_init()
2091541Srgrimes{
2101541Srgrimes	int i;
2111541Srgrimes
2121541Srgrimes	DODEBUG(printf("Vnode_interface_init.\n"));
2131541Srgrimes	/*
2141541Srgrimes	 * Set all vnode vectors to a well known value.
2151541Srgrimes	 */
2161541Srgrimes	for (i = 0; vfs_opv_descs[i]; i++)
2171541Srgrimes		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
2181541Srgrimes	/*
2191541Srgrimes	 * Figure out how many ops there are by counting the table,
2201541Srgrimes	 * and assign each its offset.
2211541Srgrimes	 */
2221541Srgrimes	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
2231541Srgrimes		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
2241541Srgrimes		vfs_opv_numops++;
2251541Srgrimes	}
2261541Srgrimes	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
2271541Srgrimes}
2281541Srgrimes
2291541Srgrimes/*
2301541Srgrimes * Routines having to do with the management of the vnode table.
2311541Srgrimes */
2321541Srgrimesextern struct vnodeops dead_vnodeops;
2331541Srgrimesextern struct vnodeops spec_vnodeops;
2341541Srgrimesextern void vclean();
2351541Srgrimesstruct vattr va_null;
2361541Srgrimes
2371541Srgrimes/*
2381541Srgrimes * Initialize the vnode structures and initialize each file system type.
2391541Srgrimes */
24010358Sjulian/* ARGSUSED*/
24110358Sjulianstatic void
24210653Sdgvfsinit(udata)
24310653Sdg	void *udata;		/* not used*/
2441541Srgrimes{
2451541Srgrimes	struct vfsops **vfsp;
2462946Swollman	struct vfsconf **vfc;
2472946Swollman	int i;
2481541Srgrimes
2491541Srgrimes	/*
2502946Swollman	 * Initialize the VFS switch table
2512946Swollman	 */
2522946Swollman	for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
2532946Swollman		vfsconf[i] = &void_vfsconf;
2542946Swollman	}
2552946Swollman
2562946Swollman	vfc = (struct vfsconf **)vfs_set.ls_items;
2572946Swollman	while(*vfc) {
2582946Swollman		vfssw[(**vfc).vfc_index] = (**vfc).vfc_vfsops;
2592946Swollman		vfsconf[(**vfc).vfc_index] = *vfc;
2602946Swollman		vfc++;
2612946Swollman	}
2628876Srgrimes
2632946Swollman	/*
2641541Srgrimes	 * Initialize the vnode table
2651541Srgrimes	 */
2661541Srgrimes	vntblinit();
2671541Srgrimes	/*
2681541Srgrimes	 * Initialize the vnode name cache
2691541Srgrimes	 */
2701541Srgrimes	nchinit();
2711541Srgrimes	/*
2721541Srgrimes	 * Build vnode operation vectors.
2731541Srgrimes	 */
2741541Srgrimes	vfs_op_init();
2752946Swollman	vfs_opv_init(vfs_opv_descs);   /* finish the job */
2761541Srgrimes	/*
2771541Srgrimes	 * Initialize each file system type.
2781541Srgrimes	 */
2791541Srgrimes	vattr_null(&va_null);
2801541Srgrimes	for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
2811541Srgrimes		if (*vfsp == NULL)
2821541Srgrimes			continue;
2831541Srgrimes		(*(*vfsp)->vfs_init)();
2841541Srgrimes	}
2851541Srgrimes}
2862946Swollman
2872946Swollman/*
2882946Swollman * kernel related system variables.
2892946Swollman */
2902946Swollmanint
2912946Swollmanfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
2922946Swollman	int *name;
2932946Swollman	u_int namelen;
2942946Swollman	void *oldp;
2952946Swollman	size_t *oldlenp;
2962946Swollman	void *newp;
2972946Swollman	size_t newlen;
2982946Swollman	struct proc *p;
2992946Swollman{
3002946Swollman	int i;
3012946Swollman	int error;
3022946Swollman	int buflen = *oldlenp;
3032962Swollman	caddr_t where = oldp, start = oldp;
3042946Swollman
3052946Swollman	switch (name[0]) {
3062946Swollman	case FS_VFSCONF:
3072946Swollman		if (namelen != 1) return ENOTDIR;
3082946Swollman
3092946Swollman		if (oldp == NULL) {
3102946Swollman			*oldlenp = (MOUNT_MAXTYPE+1) * sizeof(struct vfsconf);
3112946Swollman			return 0;
3122946Swollman		}
3132946Swollman		if (newp) {
3142946Swollman			return EINVAL;
3152946Swollman		}
3162946Swollman
3172946Swollman		for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
3182946Swollman			if(buflen < sizeof *vfsconf[i]) {
3192946Swollman				*oldlenp = where - start;
3202946Swollman				return ENOMEM;
3212946Swollman			}
3222946Swollman
3233441Sphk			error = copyout(vfsconf[i], where, sizeof *vfsconf[i]);
3243441Sphk			if(error)
3252946Swollman				return error;
3262946Swollman			where += sizeof *vfsconf[i];
3272946Swollman			buflen -= sizeof *vfsconf[i];
3282946Swollman		}
3292946Swollman		*oldlenp = where - start;
3302946Swollman		return 0;
3318876Srgrimes
3322946Swollman	default:
3333731Swollman		if(namelen < 1) return EINVAL;
3343731Swollman
3353731Swollman		i = name[0];
3363731Swollman
3373731Swollman		if(i <= MOUNT_MAXTYPE
3383731Swollman		   && vfssw[i]
3393731Swollman		   && vfssw[i]->vfs_sysctl) {
3403731Swollman			return vfssw[i]->vfs_sysctl(name + 1, namelen - 1,
3413731Swollman						    oldp, oldlenp,
3423731Swollman						    newp, newlen, p);
3433731Swollman		}
3443731Swollman
3452946Swollman		return (EOPNOTSUPP);
3462946Swollman	}
3472946Swollman	/* NOTREACHED */
3482946Swollman}
3492946Swollman
3502997Swollman/*
3512997Swollman * This goop is here to support a loadable NFS module... grumble...
3522997Swollman */
3532997Swollmanvoid (*lease_check) __P((struct vnode *, struct proc *, struct ucred *, int))
3542997Swollman     = 0;
3552997Swollmanvoid (*lease_updatetime) __P((int))
3562997Swollman     = 0;
3572997Swollman
358