xref: /freebsd/sys/kern/vfs_init.c (revision a14a0223ae1b172e96dd2a1d849e22026a98b692)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed
6  * to Berkeley by John Heidemann of the UCLA Ficus project.
7  *
8  * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
39  * $FreeBSD$
40  */
41 
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/sysctl.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <vm/vm_zone.h>
51 
52 
53 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
54 
55 /*
56  * XXX this bloat just exands the sysctl__vfs linker set a little so that
57  * we can attach sysctls for VFS modules without expanding the linker set.
58  * Currently (1998/09/06), only one VFS uses sysctls, so 2 extra linker
59  * set slots are more than sufficient.
60  */
61 static int mod_xx;
62 SYSCTL_INT(_vfs, OID_AUTO, mod0, CTLFLAG_RD, &mod_xx, 0, "");
63 SYSCTL_INT(_vfs, OID_AUTO, mod1, CTLFLAG_RD, &mod_xx, 0, "");
64 
65 /*
66  * Zone for namei
67  */
68 struct vm_zone *namei_zone;
69 
70 /*
71  * vfs_init() will set maxvfsconf
72  * to the highest defined type number.
73  */
74 int maxvfsconf;
75 struct vfsconf *vfsconf;
76 
77 /*
78  * vfs_init.c
79  *
80  * Allocate and fill in operations vectors.
81  *
82  * An undocumented feature of this approach to defining operations is that
83  * there can be multiple entries in vfs_opv_descs for the same operations
84  * vector. This allows third parties to extend the set of operations
85  * supported by another layer in a binary compatibile way. For example,
86  * assume that NFS needed to be modified to support Ficus. NFS has an entry
87  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
88  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
89  * listing those new operations Ficus adds to NFS, all without modifying the
90  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
91  * that is a(whole)nother story.) This is a feature.
92  */
93 
94 /* Table of known vnodeop vectors (list of VFS vnode vectors) */
95 static const struct vnodeopv_desc **vnodeopv_descs;
96 static int vnodeopv_num;
97 
98 /* Table of known descs (list of vnode op handlers "vop_access_desc") */
99 static struct vnodeop_desc **vfs_op_descs;
100 static int *vfs_op_desc_refs;			/* reference counts */
101 static int num_op_descs;
102 static int vfs_opv_numops;
103 
104 static void
105 vfs_opv_recalc(void)
106 {
107 	int i, j;
108 	vop_t ***opv_desc_vector_p;
109 	vop_t **opv_desc_vector;
110 	struct vnodeopv_entry_desc *opve_descp;
111 	const struct vnodeopv_desc *opv;
112 
113 	if (vfs_op_descs == NULL)
114 		panic("vfs_opv_recalc called with null vfs_op_descs");
115 
116 	/*
117 	 * Run through and make sure all known descs have an offset
118 	 *
119 	 * vop_default_desc is hardwired at offset 1, and offset 0
120 	 * is a panic sanity check.
121 	 */
122 	vfs_opv_numops = 0;
123 	for (i = 0; i < num_op_descs; i++)
124 		if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1))
125 			vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1;
126 	for (i = 0; i < num_op_descs; i++)
127 		if (vfs_op_descs[i]->vdesc_offset == 0)
128 			vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++;
129 	/*
130 	 * Allocate and fill in the vectors
131 	 */
132 	for (i = 0; i < vnodeopv_num; i++) {
133 		opv = vnodeopv_descs[i];
134 		opv_desc_vector_p = opv->opv_desc_vector_p;
135 		if (*opv_desc_vector_p)
136 			FREE(*opv_desc_vector_p, M_VNODE);
137 		MALLOC(*opv_desc_vector_p, vop_t **,
138 		       vfs_opv_numops * sizeof(vop_t *), M_VNODE, M_WAITOK);
139 		if (*opv_desc_vector_p == NULL)
140 			panic("no memory for vop_t ** vector");
141 		bzero(*opv_desc_vector_p, vfs_opv_numops * sizeof(vop_t *));
142 
143 		/* Fill in, with slot 0 being panic */
144 		opv_desc_vector = *opv_desc_vector_p;
145 		opv_desc_vector[0] = (vop_t *)vop_panic;
146 		for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
147 			opve_descp = &(opv->opv_desc_ops[j]);
148 			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
149 				opve_descp->opve_impl;
150 		}
151 
152 		/* Replace unfilled routines with their default (slot 1). */
153 		opv_desc_vector = *(opv->opv_desc_vector_p);
154 		if (opv_desc_vector[1] == NULL)
155 			panic("vfs_opv_recalc: vector without a default.");
156 		for (j = 0; j < vfs_opv_numops; j++)
157 			if (opv_desc_vector[j] == NULL)
158 				opv_desc_vector[j] = opv_desc_vector[1];
159 	}
160 }
161 
162 void
163 vfs_add_vnodeops(const void *data)
164 {
165 	const struct vnodeopv_desc *opv;
166 	const struct vnodeopv_desc **newopv;
167 	struct vnodeop_desc **newop;
168 	int *newref;
169 	vop_t **opv_desc_vector;
170 	struct vnodeop_desc *desc;
171 	int i, j;
172 
173 	opv = (const struct vnodeopv_desc *)data;
174 	MALLOC(newopv, const struct vnodeopv_desc **,
175 	       (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
176 	if (newopv == NULL)
177 		panic("vfs_add_vnodeops: no memory");
178 	if (vnodeopv_descs) {
179 		bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
180 		FREE(vnodeopv_descs, M_VNODE);
181 	}
182 	newopv[vnodeopv_num] = opv;
183 	vnodeopv_descs = newopv;
184 	vnodeopv_num++;
185 
186 	/* See if we have turned up a new vnode op desc */
187 	opv_desc_vector = *(opv->opv_desc_vector_p);
188 	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
189 		for (j = 0; j < num_op_descs; j++) {
190 			if (desc == vfs_op_descs[j]) {
191 				/* found it, increase reference count */
192 				vfs_op_desc_refs[j]++;
193 				break;
194 			}
195 		}
196 		if (j == num_op_descs) {
197 			/* not found, new entry */
198 			MALLOC(newop, struct vnodeop_desc **,
199 			       (num_op_descs + 1) * sizeof(*newop),
200 			       M_VNODE, M_WAITOK);
201 			if (newop == NULL)
202 				panic("vfs_add_vnodeops: no memory for desc");
203 			/* new reference count (for unload) */
204 			MALLOC(newref, int *,
205 				(num_op_descs + 1) * sizeof(*newref),
206 				M_VNODE, M_WAITOK);
207 			if (newref == NULL)
208 				panic("vfs_add_vnodeops: no memory for refs");
209 			if (vfs_op_descs) {
210 				bcopy(vfs_op_descs, newop,
211 					num_op_descs * sizeof(*newop));
212 				FREE(vfs_op_descs, M_VNODE);
213 			}
214 			if (vfs_op_desc_refs) {
215 				bcopy(vfs_op_desc_refs, newref,
216 					num_op_descs * sizeof(*newref));
217 				FREE(vfs_op_desc_refs, M_VNODE);
218 			}
219 			newop[num_op_descs] = desc;
220 			newref[num_op_descs] = 1;
221 			vfs_op_descs = newop;
222 			vfs_op_desc_refs = newref;
223 			num_op_descs++;
224 		}
225 	}
226 	vfs_opv_recalc();
227 }
228 
229 void
230 vfs_rm_vnodeops(const void *data)
231 {
232 	const struct vnodeopv_desc *opv;
233 	const struct vnodeopv_desc **newopv;
234 	struct vnodeop_desc **newop;
235 	int *newref;
236 	vop_t **opv_desc_vector;
237 	struct vnodeop_desc *desc;
238 	int i, j, k;
239 
240 	opv = (const struct vnodeopv_desc *)data;
241 	/* Lower ref counts on descs in the table and release if zero */
242 	opv_desc_vector = *(opv->opv_desc_vector_p);
243 	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
244 		for (j = 0; j < num_op_descs; j++) {
245 			if (desc == vfs_op_descs[j]) {
246 				/* found it, decrease reference count */
247 				vfs_op_desc_refs[j]--;
248 				break;
249 			}
250 		}
251 		for (j = 0; j < num_op_descs; j++) {
252 			if (vfs_op_desc_refs[j] > 0)
253 				continue;
254 			if (vfs_op_desc_refs[j] < 0)
255 				panic("vfs_remove_vnodeops: negative refcnt");
256 			MALLOC(newop, struct vnodeop_desc **,
257 			       (num_op_descs - 1) * sizeof(*newop),
258 			       M_VNODE, M_WAITOK);
259 			if (newop == NULL)
260 				panic("vfs_remove_vnodeops: no memory for desc");
261 			/* new reference count (for unload) */
262 			MALLOC(newref, int *,
263 				(num_op_descs - 1) * sizeof(*newref),
264 				M_VNODE, M_WAITOK);
265 			if (newref == NULL)
266 				panic("vfs_remove_vnodeops: no memory for refs");
267 			for (k = j; k < (num_op_descs - 1); k++) {
268 				vfs_op_descs[k] = vfs_op_descs[k + 1];
269 				vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
270 			}
271 			bcopy(vfs_op_descs, newop,
272 				(num_op_descs - 1) * sizeof(*newop));
273 			bcopy(vfs_op_desc_refs, newref,
274 				(num_op_descs - 1) * sizeof(*newref));
275 			FREE(vfs_op_descs, M_VNODE);
276 			FREE(vfs_op_desc_refs, M_VNODE);
277 			vfs_op_descs = newop;
278 			vfs_op_desc_refs = newref;
279 			num_op_descs--;
280 		}
281 	}
282 
283 	for (i = 0; i < vnodeopv_num; i++) {
284 		if (vnodeopv_descs[i] == opv) {
285 			for (j = i; j < (vnodeopv_num - 1); j++)
286 				vnodeopv_descs[j] = vnodeopv_descs[j + 1];
287 			break;
288 		}
289 	}
290 	if (i == vnodeopv_num)
291 		panic("vfs_remove_vnodeops: opv not found");
292 	MALLOC(newopv, const struct vnodeopv_desc **,
293 	       (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
294 	if (newopv == NULL)
295 		panic("vfs_remove_vnodeops: no memory");
296 	bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
297 	FREE(vnodeopv_descs, M_VNODE);
298 	vnodeopv_descs = newopv;
299 	vnodeopv_num--;
300 
301 	vfs_opv_recalc();
302 }
303 
304 /*
305  * Routines having to do with the management of the vnode table.
306  */
307 struct vattr va_null;
308 
309 /*
310  * Initialize the vnode structures and initialize each file system type.
311  */
312 /* ARGSUSED*/
313 static void
314 vfsinit(void *dummy)
315 {
316 
317 	namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
318 
319 	/*
320 	 * Initialize the vnode table
321 	 */
322 	vntblinit();
323 	/*
324 	 * Initialize the vnode name cache
325 	 */
326 	nchinit();
327 	/*
328 	 * Initialize each file system type.
329 	 * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF).
330 	 */
331 	vattr_null(&va_null);
332 	maxvfsconf = VFS_GENERIC + 1;
333 }
334 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
335 
336 int
337 vfs_register(struct vfsconf *vfc)
338 {
339 	struct sysctl_oid *oidp;
340 	struct vfsconf *vfsp;
341 
342 	vfsp = NULL;
343 	if (vfsconf)
344 		for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
345 			if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
346 				return EEXIST;
347 
348 	vfc->vfc_typenum = maxvfsconf++;
349 	if (vfsp)
350 		vfsp->vfc_next = vfc;
351 	else
352 		vfsconf = vfc;
353 	vfc->vfc_next = NULL;
354 
355 	/*
356 	 * If this filesystem has a sysctl node under vfs
357 	 * (i.e. vfs.xxfs), then change the oid number of that node to
358 	 * match the filesystem's type number.  This allows user code
359 	 * which uses the type number to read sysctl variables defined
360 	 * by the filesystem to continue working. Since the oids are
361 	 * in a sorted list, we need to make sure the order is
362 	 * preserved by re-registering the oid after modifying its
363 	 * number.
364 	 */
365 	for (oidp = SLIST_FIRST(&sysctl__vfs_children); oidp;
366 	     oidp = SLIST_NEXT(oidp, oid_link))
367 		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
368 			sysctl_unregister_oid(oidp);
369 			oidp->oid_number = vfc->vfc_typenum;
370 			sysctl_register_oid(oidp);
371 		}
372 
373 	/*
374 	 * Call init function for this VFS...
375 	 */
376 	(*(vfc->vfc_vfsops->vfs_init))(vfc);
377 
378 	return 0;
379 }
380 
381 
382 int
383 vfs_unregister(struct vfsconf *vfc)
384 {
385 	struct vfsconf *vfsp, *prev_vfsp;
386 	int error, i, maxtypenum;
387 
388 	i = vfc->vfc_typenum;
389 
390 	prev_vfsp = NULL;
391 	for (vfsp = vfsconf; vfsp;
392 			prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
393 		if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
394 			break;
395 	}
396 	if (vfsp == NULL)
397 		return EINVAL;
398 	if (vfsp->vfc_refcount)
399 		return EBUSY;
400 	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
401 		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
402 		if (error)
403 			return (error);
404 	}
405 	if (prev_vfsp)
406 		prev_vfsp->vfc_next = vfsp->vfc_next;
407 	else
408 		vfsconf = vfsp->vfc_next;
409 	maxtypenum = VFS_GENERIC;
410 	for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
411 		if (maxtypenum < vfsp->vfc_typenum)
412 			maxtypenum = vfsp->vfc_typenum;
413 	maxvfsconf = maxtypenum + 1;
414 	return 0;
415 }
416 
417 int
418 vfs_modevent(module_t mod, int type, void *data)
419 {
420 	struct vfsconf *vfc;
421 	int error = 0;
422 
423 	vfc = (struct vfsconf *)data;
424 
425 	switch (type) {
426 	case MOD_LOAD:
427 		if (vfc)
428 			error = vfs_register(vfc);
429 		break;
430 
431 	case MOD_UNLOAD:
432 		if (vfc)
433 			error = vfs_unregister(vfc);
434 		break;
435 	default:	/* including MOD_SHUTDOWN */
436 		break;
437 	}
438 	return (error);
439 }
440