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