xref: /freebsd/sys/kern/vfs_init.c (revision df8bae1de4b67ccf57f4afebd4e2bf258c38910d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed
6df8bae1dSRodney W. Grimes  * to Berkeley by John Heidemann of the UCLA Ficus project.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/mount.h>
44df8bae1dSRodney W. Grimes #include <sys/time.h>
45df8bae1dSRodney W. Grimes #include <sys/vnode.h>
46df8bae1dSRodney W. Grimes #include <sys/stat.h>
47df8bae1dSRodney W. Grimes #include <sys/namei.h>
48df8bae1dSRodney W. Grimes #include <sys/ucred.h>
49df8bae1dSRodney W. Grimes #include <sys/buf.h>
50df8bae1dSRodney W. Grimes #include <sys/errno.h>
51df8bae1dSRodney W. Grimes #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes /*
54df8bae1dSRodney W. Grimes  * Sigh, such primitive tools are these...
55df8bae1dSRodney W. Grimes  */
56df8bae1dSRodney W. Grimes #if 0
57df8bae1dSRodney W. Grimes #define DODEBUG(A) A
58df8bae1dSRodney W. Grimes #else
59df8bae1dSRodney W. Grimes #define DODEBUG(A)
60df8bae1dSRodney W. Grimes #endif
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes extern struct vnodeopv_desc *vfs_opv_descs[];
63df8bae1dSRodney W. Grimes 				/* a list of lists of vnodeops defns */
64df8bae1dSRodney W. Grimes extern struct vnodeop_desc *vfs_op_descs[];
65df8bae1dSRodney W. Grimes 				/* and the operations they perform */
66df8bae1dSRodney W. Grimes /*
67df8bae1dSRodney W. Grimes  * This code doesn't work if the defn is **vnodop_defns with cc.
68df8bae1dSRodney W. Grimes  * The problem is because of the compiler sometimes putting in an
69df8bae1dSRodney W. Grimes  * extra level of indirection for arrays.  It's an interesting
70df8bae1dSRodney W. Grimes  * "feature" of C.
71df8bae1dSRodney W. Grimes  */
72df8bae1dSRodney W. Grimes int vfs_opv_numops;
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes typedef (*PFI)();   /* the standard Pointer to a Function returning an Int */
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes /*
77df8bae1dSRodney W. Grimes  * A miscellaneous routine.
78df8bae1dSRodney W. Grimes  * A generic "default" routine that just returns an error.
79df8bae1dSRodney W. Grimes  */
80df8bae1dSRodney W. Grimes int
81df8bae1dSRodney W. Grimes vn_default_error()
82df8bae1dSRodney W. Grimes {
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes 	return (EOPNOTSUPP);
85df8bae1dSRodney W. Grimes }
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * vfs_init.c
89df8bae1dSRodney W. Grimes  *
90df8bae1dSRodney W. Grimes  * Allocate and fill in operations vectors.
91df8bae1dSRodney W. Grimes  *
92df8bae1dSRodney W. Grimes  * An undocumented feature of this approach to defining operations is that
93df8bae1dSRodney W. Grimes  * there can be multiple entries in vfs_opv_descs for the same operations
94df8bae1dSRodney W. Grimes  * vector. This allows third parties to extend the set of operations
95df8bae1dSRodney W. Grimes  * supported by another layer in a binary compatibile way. For example,
96df8bae1dSRodney W. Grimes  * assume that NFS needed to be modified to support Ficus. NFS has an entry
97df8bae1dSRodney W. Grimes  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
98df8bae1dSRodney W. Grimes  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
99df8bae1dSRodney W. Grimes  * listing those new operations Ficus adds to NFS, all without modifying the
100df8bae1dSRodney W. Grimes  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
101df8bae1dSRodney W. Grimes  * that is a(whole)nother story.) This is a feature.
102df8bae1dSRodney W. Grimes  */
103df8bae1dSRodney W. Grimes void
104df8bae1dSRodney W. Grimes vfs_opv_init()
105df8bae1dSRodney W. Grimes {
106df8bae1dSRodney W. Grimes 	int i, j, k;
107df8bae1dSRodney W. Grimes 	int (***opv_desc_vector_p)();
108df8bae1dSRodney W. Grimes 	int (**opv_desc_vector)();
109df8bae1dSRodney W. Grimes 	struct vnodeopv_entry_desc *opve_descp;
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	/*
112df8bae1dSRodney W. Grimes 	 * Allocate the dynamic vectors and fill them in.
113df8bae1dSRodney W. Grimes 	 */
114df8bae1dSRodney W. Grimes 	for (i=0; vfs_opv_descs[i]; i++) {
115df8bae1dSRodney W. Grimes 		opv_desc_vector_p = vfs_opv_descs[i]->opv_desc_vector_p;
116df8bae1dSRodney W. Grimes 		/*
117df8bae1dSRodney W. Grimes 		 * Allocate and init the vector, if it needs it.
118df8bae1dSRodney W. Grimes 		 * Also handle backwards compatibility.
119df8bae1dSRodney W. Grimes 		 */
120df8bae1dSRodney W. Grimes 		if (*opv_desc_vector_p == NULL) {
121df8bae1dSRodney W. Grimes 			/* XXX - shouldn't be M_VNODE */
122df8bae1dSRodney W. Grimes 			MALLOC(*opv_desc_vector_p, PFI*,
123df8bae1dSRodney W. Grimes 			       vfs_opv_numops*sizeof(PFI), M_VNODE, M_WAITOK);
124df8bae1dSRodney W. Grimes 			bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
125df8bae1dSRodney W. Grimes 			DODEBUG(printf("vector at %x allocated\n",
126df8bae1dSRodney W. Grimes 			    opv_desc_vector_p));
127df8bae1dSRodney W. Grimes 		}
128df8bae1dSRodney W. Grimes 		opv_desc_vector = *opv_desc_vector_p;
129df8bae1dSRodney W. Grimes 		for (j=0; vfs_opv_descs[i]->opv_desc_ops[j].opve_op; j++) {
130df8bae1dSRodney W. Grimes 			opve_descp = &(vfs_opv_descs[i]->opv_desc_ops[j]);
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes 			/*
133df8bae1dSRodney W. Grimes 			 * Sanity check:  is this operation listed
134df8bae1dSRodney W. Grimes 			 * in the list of operations?  We check this
135df8bae1dSRodney W. Grimes 			 * by seeing if its offest is zero.  Since
136df8bae1dSRodney W. Grimes 			 * the default routine should always be listed
137df8bae1dSRodney W. Grimes 			 * first, it should be the only one with a zero
138df8bae1dSRodney W. Grimes 			 * offset.  Any other operation with a zero
139df8bae1dSRodney W. Grimes 			 * offset is probably not listed in
140df8bae1dSRodney W. Grimes 			 * vfs_op_descs, and so is probably an error.
141df8bae1dSRodney W. Grimes 			 *
142df8bae1dSRodney W. Grimes 			 * A panic here means the layer programmer
143df8bae1dSRodney W. Grimes 			 * has committed the all-too common bug
144df8bae1dSRodney W. Grimes 			 * of adding a new operation to the layer's
145df8bae1dSRodney W. Grimes 			 * list of vnode operations but
146df8bae1dSRodney W. Grimes 			 * not adding the operation to the system-wide
147df8bae1dSRodney W. Grimes 			 * list of supported operations.
148df8bae1dSRodney W. Grimes 			 */
149df8bae1dSRodney W. Grimes 			if (opve_descp->opve_op->vdesc_offset == 0 &&
150df8bae1dSRodney W. Grimes 				    opve_descp->opve_op->vdesc_offset !=
151df8bae1dSRodney W. Grimes 				    	VOFFSET(vop_default)) {
152df8bae1dSRodney W. Grimes 				printf("operation %s not listed in %s.\n",
153df8bae1dSRodney W. Grimes 				    opve_descp->opve_op->vdesc_name,
154df8bae1dSRodney W. Grimes 				    "vfs_op_descs");
155df8bae1dSRodney W. Grimes 				panic ("vfs_opv_init: bad operation");
156df8bae1dSRodney W. Grimes 			}
157df8bae1dSRodney W. Grimes 			/*
158df8bae1dSRodney W. Grimes 			 * Fill in this entry.
159df8bae1dSRodney W. Grimes 			 */
160df8bae1dSRodney W. Grimes 			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
161df8bae1dSRodney W. Grimes 					opve_descp->opve_impl;
162df8bae1dSRodney W. Grimes 		}
163df8bae1dSRodney W. Grimes 	}
164df8bae1dSRodney W. Grimes 	/*
165df8bae1dSRodney W. Grimes 	 * Finally, go back and replace unfilled routines
166df8bae1dSRodney W. Grimes 	 * with their default.  (Sigh, an O(n^3) algorithm.  I
167df8bae1dSRodney W. Grimes 	 * could make it better, but that'd be work, and n is small.)
168df8bae1dSRodney W. Grimes 	 */
169df8bae1dSRodney W. Grimes 	for (i = 0; vfs_opv_descs[i]; i++) {
170df8bae1dSRodney W. Grimes 		opv_desc_vector = *(vfs_opv_descs[i]->opv_desc_vector_p);
171df8bae1dSRodney W. Grimes 		/*
172df8bae1dSRodney W. Grimes 		 * Force every operations vector to have a default routine.
173df8bae1dSRodney W. Grimes 		 */
174df8bae1dSRodney W. Grimes 		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
175df8bae1dSRodney W. Grimes 			panic("vfs_opv_init: operation vector without default routine.");
176df8bae1dSRodney W. Grimes 		}
177df8bae1dSRodney W. Grimes 		for (k = 0; k<vfs_opv_numops; k++)
178df8bae1dSRodney W. Grimes 			if (opv_desc_vector[k] == NULL)
179df8bae1dSRodney W. Grimes 				opv_desc_vector[k] =
180df8bae1dSRodney W. Grimes 					opv_desc_vector[VOFFSET(vop_default)];
181df8bae1dSRodney W. Grimes 	}
182df8bae1dSRodney W. Grimes }
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes /*
185df8bae1dSRodney W. Grimes  * Initialize known vnode operations vectors.
186df8bae1dSRodney W. Grimes  */
187df8bae1dSRodney W. Grimes void
188df8bae1dSRodney W. Grimes vfs_op_init()
189df8bae1dSRodney W. Grimes {
190df8bae1dSRodney W. Grimes 	int i;
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes 	DODEBUG(printf("Vnode_interface_init.\n"));
193df8bae1dSRodney W. Grimes 	/*
194df8bae1dSRodney W. Grimes 	 * Set all vnode vectors to a well known value.
195df8bae1dSRodney W. Grimes 	 */
196df8bae1dSRodney W. Grimes 	for (i = 0; vfs_opv_descs[i]; i++)
197df8bae1dSRodney W. Grimes 		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
198df8bae1dSRodney W. Grimes 	/*
199df8bae1dSRodney W. Grimes 	 * Figure out how many ops there are by counting the table,
200df8bae1dSRodney W. Grimes 	 * and assign each its offset.
201df8bae1dSRodney W. Grimes 	 */
202df8bae1dSRodney W. Grimes 	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
203df8bae1dSRodney W. Grimes 		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
204df8bae1dSRodney W. Grimes 		vfs_opv_numops++;
205df8bae1dSRodney W. Grimes 	}
206df8bae1dSRodney W. Grimes 	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  * Routines having to do with the management of the vnode table.
211df8bae1dSRodney W. Grimes  */
212df8bae1dSRodney W. Grimes extern struct vnodeops dead_vnodeops;
213df8bae1dSRodney W. Grimes extern struct vnodeops spec_vnodeops;
214df8bae1dSRodney W. Grimes extern void vclean();
215df8bae1dSRodney W. Grimes struct vattr va_null;
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes /*
218df8bae1dSRodney W. Grimes  * Initialize the vnode structures and initialize each file system type.
219df8bae1dSRodney W. Grimes  */
220df8bae1dSRodney W. Grimes vfsinit()
221df8bae1dSRodney W. Grimes {
222df8bae1dSRodney W. Grimes 	struct vfsops **vfsp;
223df8bae1dSRodney W. Grimes 
224df8bae1dSRodney W. Grimes 	/*
225df8bae1dSRodney W. Grimes 	 * Initialize the vnode table
226df8bae1dSRodney W. Grimes 	 */
227df8bae1dSRodney W. Grimes 	vntblinit();
228df8bae1dSRodney W. Grimes 	/*
229df8bae1dSRodney W. Grimes 	 * Initialize the vnode name cache
230df8bae1dSRodney W. Grimes 	 */
231df8bae1dSRodney W. Grimes 	nchinit();
232df8bae1dSRodney W. Grimes 	/*
233df8bae1dSRodney W. Grimes 	 * Build vnode operation vectors.
234df8bae1dSRodney W. Grimes 	 */
235df8bae1dSRodney W. Grimes 	vfs_op_init();
236df8bae1dSRodney W. Grimes 	vfs_opv_init();   /* finish the job */
237df8bae1dSRodney W. Grimes 	/*
238df8bae1dSRodney W. Grimes 	 * Initialize each file system type.
239df8bae1dSRodney W. Grimes 	 */
240df8bae1dSRodney W. Grimes 	vattr_null(&va_null);
241df8bae1dSRodney W. Grimes 	for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
242df8bae1dSRodney W. Grimes 		if (*vfsp == NULL)
243df8bae1dSRodney W. Grimes 			continue;
244df8bae1dSRodney W. Grimes 		(*(*vfsp)->vfs_init)();
245df8bae1dSRodney W. Grimes 	}
246df8bae1dSRodney W. Grimes }
247