vfs_init.c revision 33181
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
3933181Seivind * $Id: vfs_init.c,v 1.31 1997/10/26 20:26:33 phk 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/vnode.h>
481541Srgrimes#include <sys/malloc.h>
4929653Sdyson#include <vm/vm_zone.h>
501541Srgrimes
5112913Sphkstatic void	vfs_op_init __P((void));
5212577Sbde
5310653Sdgstatic void vfsinit __P((void *));
5410358SjulianSYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
5510358Sjulian
5630354SphkMALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
5730354Sphk
5810358Sjulian/*
591541Srgrimes * Sigh, such primitive tools are these...
601541Srgrimes */
611541Srgrimes#if 0
621541Srgrimes#define DODEBUG(A) A
631541Srgrimes#else
641541Srgrimes#define DODEBUG(A)
651541Srgrimes#endif
661541Srgrimes
6733181Seivindstatic struct vfsconf void_vfsconf;
682946Swollman
692946Swollmanextern struct linker_set vfs_opv_descs_;
702946Swollman#define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
712946Swollman
722946Swollmanextern struct linker_set vfs_set;
732946Swollman
741541Srgrimesextern struct vnodeop_desc *vfs_op_descs[];
751541Srgrimes				/* and the operations they perform */
761541Srgrimes
771541Srgrimes/*
7829653Sdyson * Zone for namei
7929653Sdyson */
8029653Sdysonstruct vm_zone *namei_zone;
8129653Sdyson
8229653Sdyson/*
831541Srgrimes * vfs_init.c
841541Srgrimes *
851541Srgrimes * Allocate and fill in operations vectors.
861541Srgrimes *
871541Srgrimes * An undocumented feature of this approach to defining operations is that
881541Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations
891541Srgrimes * vector. This allows third parties to extend the set of operations
901541Srgrimes * supported by another layer in a binary compatibile way. For example,
911541Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry
921541Srgrimes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
931541Srgrimes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
941541Srgrimes * listing those new operations Ficus adds to NFS, all without modifying the
951541Srgrimes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
961541Srgrimes * that is a(whole)nother story.) This is a feature.
9729290Sphk *
9829290Sphk * Without an explicit reserve area, however, you must replace vnode_if.c
9929290Sphk * and vnode_if.h when you do this, or you will be derefrencing of the
10029290Sphk * end of vfs_op_descs[].  This is a flaw in the use of a structure
10129290Sphk * pointer array rather than an agregate to define vfs_op_descs.  So
10229290Sphk * it's not a very dynamic "feature".
1031541Srgrimes */
1041541Srgrimesvoid
1052946Swollmanvfs_opv_init(struct vnodeopv_desc **them)
1061541Srgrimes{
1071541Srgrimes	int i, j, k;
10812158Sbde	vop_t ***opv_desc_vector_p;
10912158Sbde	vop_t **opv_desc_vector;
1101541Srgrimes	struct vnodeopv_entry_desc *opve_descp;
1111541Srgrimes
1121541Srgrimes	/*
1131541Srgrimes	 * Allocate the dynamic vectors and fill them in.
1141541Srgrimes	 */
1152946Swollman	for (i=0; them[i]; i++) {
1162946Swollman		opv_desc_vector_p = them[i]->opv_desc_vector_p;
1171541Srgrimes		/*
1181541Srgrimes		 * Allocate and init the vector, if it needs it.
1191541Srgrimes		 * Also handle backwards compatibility.
1201541Srgrimes		 */
1211541Srgrimes		if (*opv_desc_vector_p == NULL) {
1221541Srgrimes			/* XXX - shouldn't be M_VNODE */
12312158Sbde			MALLOC(*opv_desc_vector_p, vop_t **,
12412158Sbde			       vfs_opv_numops * sizeof(vop_t *), M_VNODE,
12512158Sbde			       M_WAITOK);
12612158Sbde			bzero(*opv_desc_vector_p,
12712158Sbde			      vfs_opv_numops * sizeof(vop_t *));
1281541Srgrimes			DODEBUG(printf("vector at %x allocated\n",
1291541Srgrimes			    opv_desc_vector_p));
1301541Srgrimes		}
1311541Srgrimes		opv_desc_vector = *opv_desc_vector_p;
1322946Swollman		for (j=0; them[i]->opv_desc_ops[j].opve_op; j++) {
1332946Swollman			opve_descp = &(them[i]->opv_desc_ops[j]);
1341541Srgrimes
1351541Srgrimes			/*
1361541Srgrimes			 * Sanity check:  is this operation listed
1371541Srgrimes			 * in the list of operations?  We check this
1381541Srgrimes			 * by seeing if its offest is zero.  Since
1391541Srgrimes			 * the default routine should always be listed
1401541Srgrimes			 * first, it should be the only one with a zero
1411541Srgrimes			 * offset.  Any other operation with a zero
1421541Srgrimes			 * offset is probably not listed in
1431541Srgrimes			 * vfs_op_descs, and so is probably an error.
1441541Srgrimes			 *
1451541Srgrimes			 * A panic here means the layer programmer
1461541Srgrimes			 * has committed the all-too common bug
1471541Srgrimes			 * of adding a new operation to the layer's
1481541Srgrimes			 * list of vnode operations but
1491541Srgrimes			 * not adding the operation to the system-wide
1501541Srgrimes			 * list of supported operations.
1511541Srgrimes			 */
1521541Srgrimes			if (opve_descp->opve_op->vdesc_offset == 0 &&
1531541Srgrimes				    opve_descp->opve_op->vdesc_offset !=
1541541Srgrimes				    	VOFFSET(vop_default)) {
1551541Srgrimes				printf("operation %s not listed in %s.\n",
1561541Srgrimes				    opve_descp->opve_op->vdesc_name,
1571541Srgrimes				    "vfs_op_descs");
1581541Srgrimes				panic ("vfs_opv_init: bad operation");
1591541Srgrimes			}
1601541Srgrimes			/*
1611541Srgrimes			 * Fill in this entry.
1621541Srgrimes			 */
1631541Srgrimes			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
1641541Srgrimes					opve_descp->opve_impl;
1651541Srgrimes		}
1661541Srgrimes	}
1671541Srgrimes	/*
1681541Srgrimes	 * Finally, go back and replace unfilled routines
1691541Srgrimes	 * with their default.  (Sigh, an O(n^3) algorithm.  I
1701541Srgrimes	 * could make it better, but that'd be work, and n is small.)
1711541Srgrimes	 */
1722946Swollman	for (i = 0; them[i]; i++) {
1732946Swollman		opv_desc_vector = *(them[i]->opv_desc_vector_p);
1741541Srgrimes		/*
1751541Srgrimes		 * Force every operations vector to have a default routine.
1761541Srgrimes		 */
1771541Srgrimes		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
1781541Srgrimes			panic("vfs_opv_init: operation vector without default routine.");
1791541Srgrimes		}
1801541Srgrimes		for (k = 0; k<vfs_opv_numops; k++)
1811541Srgrimes			if (opv_desc_vector[k] == NULL)
1828876Srgrimes				opv_desc_vector[k] =
1831541Srgrimes					opv_desc_vector[VOFFSET(vop_default)];
1841541Srgrimes	}
1851541Srgrimes}
1861541Srgrimes
1871541Srgrimes/*
1881541Srgrimes * Initialize known vnode operations vectors.
1891541Srgrimes */
19012913Sphkstatic void
1911541Srgrimesvfs_op_init()
1921541Srgrimes{
1931541Srgrimes	int i;
1941541Srgrimes
1951541Srgrimes	DODEBUG(printf("Vnode_interface_init.\n"));
19629290Sphk	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
1971541Srgrimes	/*
1981541Srgrimes	 * Set all vnode vectors to a well known value.
1991541Srgrimes	 */
2001541Srgrimes	for (i = 0; vfs_opv_descs[i]; i++)
2011541Srgrimes		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
2021541Srgrimes	/*
20329290Sphk	 * assign each op to its offset
20429290Sphk	 *
20529290Sphk	 * XXX This should not be needed, but is because the per
20629290Sphk	 * XXX FS ops tables are not sorted according to the
20729290Sphk	 * XXX vnodeop_desc's offset in vfs_op_descs.  This
20829290Sphk	 * XXX is the same reason we have to take the hit for
20929290Sphk	 * XXX the static inline function calls instead of using
21029290Sphk	 * XXX simple macro references.
2111541Srgrimes	 */
21229290Sphk	for (i = 0; i < vfs_opv_numops; i++)
21329290Sphk		vfs_op_descs[i]->vdesc_offset = i;
2141541Srgrimes}
2151541Srgrimes
2161541Srgrimes/*
2171541Srgrimes * Routines having to do with the management of the vnode table.
2181541Srgrimes */
2191541Srgrimesextern struct vnodeops dead_vnodeops;
2201541Srgrimesextern struct vnodeops spec_vnodeops;
2211541Srgrimesstruct vattr va_null;
2221541Srgrimes
2231541Srgrimes/*
2241541Srgrimes * Initialize the vnode structures and initialize each file system type.
2251541Srgrimes */
22610358Sjulian/* ARGSUSED*/
22710358Sjulianstatic void
22812569Sbdevfsinit(dummy)
22912569Sbde	void *dummy;
2301541Srgrimes{
2312946Swollman	struct vfsconf **vfc;
23222521Sdyson	int maxtypenum;
2331541Srgrimes
23429653Sdyson	namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
23529653Sdyson
2361541Srgrimes	/*
2371541Srgrimes	 * Initialize the vnode table
2381541Srgrimes	 */
2391541Srgrimes	vntblinit();
2401541Srgrimes	/*
2411541Srgrimes	 * Initialize the vnode name cache
2421541Srgrimes	 */
2431541Srgrimes	nchinit();
2441541Srgrimes	/*
2451541Srgrimes	 * Build vnode operation vectors.
2461541Srgrimes	 */
2471541Srgrimes	vfs_op_init();
2482946Swollman	vfs_opv_init(vfs_opv_descs);   /* finish the job */
2491541Srgrimes	/*
2501541Srgrimes	 * Initialize each file system type.
2511541Srgrimes	 */
2521541Srgrimes	vattr_null(&va_null);
25322521Sdyson	maxtypenum = 0;
25422521Sdyson	vfc = (struct vfsconf **)vfs_set.ls_items;
25522521Sdyson	vfsconf = *vfc;		/* simulate Lite2 vfsconf array */
25622521Sdyson	while (*vfc) {
25722521Sdyson		struct vfsconf *vfsp = *vfc;
25822521Sdyson
25922521Sdyson		vfc++;
26022521Sdyson		vfsp->vfc_next = *vfc;
26122521Sdyson		if (maxtypenum <= vfsp->vfc_typenum)
26222521Sdyson			maxtypenum = vfsp->vfc_typenum + 1;
26322521Sdyson		(*vfsp->vfc_vfsops->vfs_init)(vfsp);
2641541Srgrimes	}
26522521Sdyson	/* next vfc_typenum to be used */
26622521Sdyson	maxvfsconf = maxtypenum;
2671541Srgrimes}
2682946Swollman
269