xref: /freebsd/sys/kern/vfs_init.c (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
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 
51 
52 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
53 
54 /*
55  * The highest defined VFS number.
56  */
57 int maxvfsconf = VFS_GENERIC + 1;
58 
59 /*
60  * Single-linked list of configured VFSes.
61  * New entries are added/deleted by vfs_register()/vfs_unregister()
62  */
63 struct vfsconf *vfsconf;
64 
65 /*
66  * vfs_init.c
67  *
68  * Allocate and fill in operations vectors.
69  *
70  * An undocumented feature of this approach to defining operations is that
71  * there can be multiple entries in vfs_opv_descs for the same operations
72  * vector. This allows third parties to extend the set of operations
73  * supported by another layer in a binary compatibile way. For example,
74  * assume that NFS needed to be modified to support Ficus. NFS has an entry
75  * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
76  * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
77  * listing those new operations Ficus adds to NFS, all without modifying the
78  * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
79  * that is a(whole)nother story.) This is a feature.
80  */
81 
82 /* Table of known vnodeop vectors (list of VFS vnode vectors) */
83 static const struct vnodeopv_desc **vnodeopv_descs;
84 static int vnodeopv_num;
85 
86 /* Table of known descs (list of vnode op handlers "vop_access_desc") */
87 static struct vnodeop_desc **vfs_op_descs;
88 /* Reference counts for vfs_op_descs */
89 static int *vfs_op_desc_refs;
90 /* Number of descriptions */
91 static int num_op_descs;
92 /* Number of entries in each description */
93 static int vfs_opv_numops = 64;
94 
95 /* Allow this number to be tuned at boot */
96 TUNABLE_INT("vfs.opv_numops", &vfs_opv_numops);
97 SYSCTL_INT(_vfs, OID_AUTO, opv_numops, CTLFLAG_RD, &vfs_opv_numops,
98 	0, "Maximum number of operations in vop_t vector");
99 
100 static int int_cmp(const void *a, const void *b);
101 
102 static int
103 int_cmp(const void *a, const void *b)
104 {
105 	return(*(const int *)a - *(const int *)b);
106 }
107 
108 /*
109  * Recalculate the operations vector/description (those parts of it that can
110  * be recalculated, that is.)
111  * Always allocate operations vector large enough to hold vfs_opv_numops
112  * entries. The vector is never freed or deallocated once it is initialized,
113  * so that vnodes might safely reference it through their v_op pointer without
114  * vector changing suddenly from under them.
115  */
116 static void
117 vfs_opv_recalc(void)
118 {
119 	int i, j, k;
120 	int *vfs_op_offsets;
121 	vop_t ***opv_desc_vector_p;
122 	vop_t **opv_desc_vector;
123 	struct vnodeopv_entry_desc *opve_descp;
124 	const struct vnodeopv_desc *opv;
125 
126 	if (vfs_op_descs == NULL)
127 		panic("vfs_opv_recalc called with null vfs_op_descs");
128 
129 	/*
130 	 * Allocate and initialize temporary array to store
131 	 * offsets. Sort it to put all uninitialized entries
132 	 * first and to make holes in existing offset sequence
133 	 * detectable.
134 	 */
135 	MALLOC(vfs_op_offsets, int *,
136 		num_op_descs * sizeof(int), M_TEMP, M_WAITOK);
137 	if (vfs_op_offsets == NULL)
138 		panic("vfs_opv_recalc: no memory");
139 	for (i = 0; i < num_op_descs; i++)
140 		vfs_op_offsets[i] = vfs_op_descs[i]->vdesc_offset;
141 	qsort(vfs_op_offsets, num_op_descs, sizeof(int), int_cmp);
142 
143 	/*
144 	 * Run through and make sure all known descs have an offset.
145 	 * Use vfs_op_offsets to locate holes in offset sequence and
146 	 * reuse them.
147 	 * vop_default_desc is hardwired at offset 1, and offset 0
148 	 * is a panic sanity check.
149 	 */
150 	j = 1; k = 1;
151 	for (i = 0; i < num_op_descs; i++) {
152 		if (vfs_op_descs[i]->vdesc_offset != 0)
153 			continue;
154 		/*
155 		 * Look at two adjacent entries vfs_op_offsets[j - 1] and
156 		 * vfs_op_offsets[j] and see if we can fit a new offset
157 		 * number in between. If not, look at the next pair until
158 		 * hole is found or the end of the vfs_op_offsets vector is
159 		 * reached. j has been initialized to 1 above so that
160 		 * referencing (j-1)-th element is safe and the loop will
161 		 * never execute if num_op_descs is 1. For each new value s
162 		 * of i the j loop pick up from where previous iteration has
163 		 * left off. When the last hole has been consumed or if no
164 		 * hole has been found, we will start allocating new numbers
165 		 * starting from the biggest already available offset + 1.
166 		 */
167 		for (; j < num_op_descs; j++) {
168 			if (vfs_op_offsets[j - 1] < k && vfs_op_offsets[j] > k)
169 				break;
170 			k = vfs_op_offsets[j] + 1;
171 		}
172 		vfs_op_descs[i]->vdesc_offset = k++;
173 	}
174 	FREE(vfs_op_offsets, M_TEMP);
175 
176 	/* Panic if new vops will cause vector overflow */
177 	if (k > vfs_opv_numops)
178 		panic("VFS: Ran out of vop_t vector entries. %d entries required, only %d available.\n", k, vfs_opv_numops);
179 
180 	/*
181 	 * Allocate and fill in the vectors
182 	 */
183 	for (i = 0; i < vnodeopv_num; i++) {
184 		opv = vnodeopv_descs[i];
185 		opv_desc_vector_p = opv->opv_desc_vector_p;
186 		if (*opv_desc_vector_p == NULL)
187 			MALLOC(*opv_desc_vector_p, vop_t **,
188 				vfs_opv_numops * sizeof(vop_t *), M_VNODE,
189 				M_WAITOK | M_ZERO);
190 		if (*opv_desc_vector_p == NULL)
191 			panic("no memory for vop_t ** vector");
192 
193 		/* Fill in, with slot 0 being to return EOPNOTSUPP */
194 		opv_desc_vector = *opv_desc_vector_p;
195 		opv_desc_vector[0] = (vop_t *)vop_eopnotsupp;
196 		for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
197 			opve_descp = &(opv->opv_desc_ops[j]);
198 			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
199 				opve_descp->opve_impl;
200 		}
201 
202 		/* Replace unfilled routines with their default (slot 1). */
203 		opv_desc_vector = *(opv->opv_desc_vector_p);
204 		if (opv_desc_vector[1] == NULL)
205 			panic("vfs_opv_recalc: vector without a default.");
206 		for (j = 0; j < vfs_opv_numops; j++)
207 			if (opv_desc_vector[j] == NULL)
208 				opv_desc_vector[j] = opv_desc_vector[1];
209 	}
210 }
211 
212 /* Add a set of vnode operations (a description) to the table above. */
213 void
214 vfs_add_vnodeops(const void *data)
215 {
216 	const struct vnodeopv_desc *opv;
217 	const struct vnodeopv_desc **newopv;
218 	struct vnodeop_desc **newop;
219 	int *newref;
220 	vop_t **opv_desc_vector;
221 	struct vnodeop_desc *desc;
222 	int i, j;
223 
224 	opv = (const struct vnodeopv_desc *)data;
225 	MALLOC(newopv, const struct vnodeopv_desc **,
226 	       (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
227 	if (newopv == NULL)
228 		panic("vfs_add_vnodeops: no memory");
229 	if (vnodeopv_descs) {
230 		bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
231 		FREE(vnodeopv_descs, M_VNODE);
232 	}
233 	newopv[vnodeopv_num] = opv;
234 	vnodeopv_descs = newopv;
235 	vnodeopv_num++;
236 
237 	/* See if we have turned up a new vnode op desc */
238 	opv_desc_vector = *(opv->opv_desc_vector_p);
239 	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
240 		for (j = 0; j < num_op_descs; j++) {
241 			if (desc == vfs_op_descs[j]) {
242 				/* found it, increase reference count */
243 				vfs_op_desc_refs[j]++;
244 				break;
245 			}
246 		}
247 		if (j == num_op_descs) {
248 			/* not found, new entry */
249 			MALLOC(newop, struct vnodeop_desc **,
250 			       (num_op_descs + 1) * sizeof(*newop),
251 			       M_VNODE, M_WAITOK);
252 			if (newop == NULL)
253 				panic("vfs_add_vnodeops: no memory for desc");
254 			/* new reference count (for unload) */
255 			MALLOC(newref, int *,
256 				(num_op_descs + 1) * sizeof(*newref),
257 				M_VNODE, M_WAITOK);
258 			if (newref == NULL)
259 				panic("vfs_add_vnodeops: no memory for refs");
260 			if (vfs_op_descs) {
261 				bcopy(vfs_op_descs, newop,
262 					num_op_descs * sizeof(*newop));
263 				FREE(vfs_op_descs, M_VNODE);
264 			}
265 			if (vfs_op_desc_refs) {
266 				bcopy(vfs_op_desc_refs, newref,
267 					num_op_descs * sizeof(*newref));
268 				FREE(vfs_op_desc_refs, M_VNODE);
269 			}
270 			newop[num_op_descs] = desc;
271 			newref[num_op_descs] = 1;
272 			vfs_op_descs = newop;
273 			vfs_op_desc_refs = newref;
274 			num_op_descs++;
275 		}
276 	}
277 	vfs_opv_recalc();
278 }
279 
280 /* Remove a vnode type from the vnode description table above. */
281 void
282 vfs_rm_vnodeops(const void *data)
283 {
284 	const struct vnodeopv_desc *opv;
285 	const struct vnodeopv_desc **newopv;
286 	struct vnodeop_desc **newop;
287 	int *newref;
288 	vop_t **opv_desc_vector;
289 	struct vnodeop_desc *desc;
290 	int i, j, k;
291 
292 	opv = (const struct vnodeopv_desc *)data;
293 	/* Lower ref counts on descs in the table and release if zero */
294 	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
295 		for (j = 0; j < num_op_descs; j++) {
296 			if (desc == vfs_op_descs[j]) {
297 				/* found it, decrease reference count */
298 				vfs_op_desc_refs[j]--;
299 				break;
300 			}
301 		}
302 		for (j = 0; j < num_op_descs; j++) {
303 			if (vfs_op_desc_refs[j] > 0)
304 				continue;
305 			if (vfs_op_desc_refs[j] < 0)
306 				panic("vfs_remove_vnodeops: negative refcnt");
307 			/* Entry is going away - replace it with defaultop */
308 			for (k = 0; k < vnodeopv_num; k++) {
309 				opv_desc_vector =
310 					*(vnodeopv_descs[k]->opv_desc_vector_p);
311 				if (opv_desc_vector != NULL)
312 					opv_desc_vector[desc->vdesc_offset] =
313 						opv_desc_vector[1];
314 			}
315 			MALLOC(newop, struct vnodeop_desc **,
316 			       (num_op_descs - 1) * sizeof(*newop),
317 			       M_VNODE, M_WAITOK);
318 			if (newop == NULL)
319 				panic("vfs_remove_vnodeops: no memory for desc");
320 			/* new reference count (for unload) */
321 			MALLOC(newref, int *,
322 				(num_op_descs - 1) * sizeof(*newref),
323 				M_VNODE, M_WAITOK);
324 			if (newref == NULL)
325 				panic("vfs_remove_vnodeops: no memory for refs");
326 			for (k = j; k < (num_op_descs - 1); k++) {
327 				vfs_op_descs[k] = vfs_op_descs[k + 1];
328 				vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
329 			}
330 			bcopy(vfs_op_descs, newop,
331 				(num_op_descs - 1) * sizeof(*newop));
332 			bcopy(vfs_op_desc_refs, newref,
333 				(num_op_descs - 1) * sizeof(*newref));
334 			FREE(vfs_op_descs, M_VNODE);
335 			FREE(vfs_op_desc_refs, M_VNODE);
336 			vfs_op_descs = newop;
337 			vfs_op_desc_refs = newref;
338 			num_op_descs--;
339 		}
340 	}
341 
342 	for (i = 0; i < vnodeopv_num; i++) {
343 		if (vnodeopv_descs[i] == opv) {
344 			for (j = i; j < (vnodeopv_num - 1); j++)
345 				vnodeopv_descs[j] = vnodeopv_descs[j + 1];
346 			break;
347 		}
348 	}
349 	if (i == vnodeopv_num)
350 		panic("vfs_remove_vnodeops: opv not found");
351 	opv_desc_vector = *(opv->opv_desc_vector_p);
352 	if (opv_desc_vector != NULL)
353 		FREE(opv_desc_vector, M_VNODE);
354 	MALLOC(newopv, const struct vnodeopv_desc **,
355 	       (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
356 	if (newopv == NULL)
357 		panic("vfs_remove_vnodeops: no memory");
358 	bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
359 	FREE(vnodeopv_descs, M_VNODE);
360 	vnodeopv_descs = newopv;
361 	vnodeopv_num--;
362 
363 	vfs_opv_recalc();
364 }
365 
366 /*
367  * Routines having to do with the management of the vnode table.
368  */
369 struct vattr va_null;
370 
371 /*
372  * Initialize the vnode structures and initialize each file system type.
373  */
374 /* ARGSUSED*/
375 static void
376 vfsinit(void *dummy)
377 {
378 
379 	vattr_null(&va_null);
380 }
381 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
382 
383 /* Register a new file system type in the global table */
384 int
385 vfs_register(struct vfsconf *vfc)
386 {
387 	struct sysctl_oid *oidp;
388 	struct vfsconf *vfsp;
389 
390 	vfsp = NULL;
391 	if (vfsconf)
392 		for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
393 			if (strcmp(vfc->vfc_name, vfsp->vfc_name) == 0)
394 				return EEXIST;
395 
396 	vfc->vfc_typenum = maxvfsconf++;
397 	if (vfsp)
398 		vfsp->vfc_next = vfc;
399 	else
400 		vfsconf = vfc;
401 	vfc->vfc_next = NULL;
402 
403 	/*
404 	 * If this filesystem has a sysctl node under vfs
405 	 * (i.e. vfs.xxfs), then change the oid number of that node to
406 	 * match the filesystem's type number.  This allows user code
407 	 * which uses the type number to read sysctl variables defined
408 	 * by the filesystem to continue working. Since the oids are
409 	 * in a sorted list, we need to make sure the order is
410 	 * preserved by re-registering the oid after modifying its
411 	 * number.
412 	 */
413 	SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
414 		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
415 			sysctl_unregister_oid(oidp);
416 			oidp->oid_number = vfc->vfc_typenum;
417 			sysctl_register_oid(oidp);
418 		}
419 
420 	/*
421 	 * Call init function for this VFS...
422 	 */
423 	(*(vfc->vfc_vfsops->vfs_init))(vfc);
424 
425 	return 0;
426 }
427 
428 
429 /* Remove registration of a file system type */
430 int
431 vfs_unregister(struct vfsconf *vfc)
432 {
433 	struct vfsconf *vfsp, *prev_vfsp;
434 	int error, i, maxtypenum;
435 
436 	i = vfc->vfc_typenum;
437 
438 	prev_vfsp = NULL;
439 	for (vfsp = vfsconf; vfsp;
440 			prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
441 		if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
442 			break;
443 	}
444 	if (vfsp == NULL)
445 		return EINVAL;
446 	if (vfsp->vfc_refcount)
447 		return EBUSY;
448 	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
449 		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
450 		if (error)
451 			return (error);
452 	}
453 	if (prev_vfsp)
454 		prev_vfsp->vfc_next = vfsp->vfc_next;
455 	else
456 		vfsconf = vfsp->vfc_next;
457 	maxtypenum = VFS_GENERIC;
458 	for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
459 		if (maxtypenum < vfsp->vfc_typenum)
460 			maxtypenum = vfsp->vfc_typenum;
461 	maxvfsconf = maxtypenum + 1;
462 	return 0;
463 }
464 
465 /*
466  * Standard kernel module handling code for file system modules.
467  * Referenced from VFS_SET().
468  */
469 int
470 vfs_modevent(module_t mod, int type, void *data)
471 {
472 	struct vfsconf *vfc;
473 	int error = 0;
474 
475 	vfc = (struct vfsconf *)data;
476 
477 	switch (type) {
478 	case MOD_LOAD:
479 		if (vfc)
480 			error = vfs_register(vfc);
481 		break;
482 
483 	case MOD_UNLOAD:
484 		if (vfc)
485 			error = vfs_unregister(vfc);
486 		break;
487 	default:	/* including MOD_SHUTDOWN */
488 		break;
489 	}
490 	return (error);
491 }
492