vfs_init.c revision 132710
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 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/vfs_init.c 132710 2004-07-27 22:32:01Z phk $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/mount.h>
44#include <sys/sysctl.h>
45#include <sys/vnode.h>
46#include <sys/malloc.h>
47
48
49MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
50
51/*
52 * The highest defined VFS number.
53 */
54int maxvfsconf = VFS_GENERIC + 1;
55
56/*
57 * Single-linked list of configured VFSes.
58 * New entries are added/deleted by vfs_register()/vfs_unregister()
59 */
60struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
61
62/*
63 * vfs_init.c
64 *
65 * Allocate and fill in operations vectors.
66 *
67 * An undocumented feature of this approach to defining operations is that
68 * there can be multiple entries in vfs_opv_descs for the same operations
69 * vector. This allows third parties to extend the set of operations
70 * supported by another layer in a binary compatibile way. For example,
71 * assume that NFS needed to be modified to support Ficus. NFS has an entry
72 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
73 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
74 * listing those new operations Ficus adds to NFS, all without modifying the
75 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
76 * that is a(whole)nother story.) This is a feature.
77 */
78
79/* Table of known vnodeop vectors (list of VFS vnode vectors) */
80static const struct vnodeopv_desc **vnodeopv_descs;
81static int vnodeopv_num;
82
83/* Table of known descs (list of vnode op handlers "vop_access_desc") */
84static struct vnodeop_desc **vfs_op_descs;
85/* Reference counts for vfs_op_descs */
86static int *vfs_op_desc_refs;
87/* Number of descriptions */
88static int num_op_descs;
89/* Number of entries in each description */
90static int vfs_opv_numops = 64;
91
92/* Allow this number to be tuned at boot */
93TUNABLE_INT("vfs.opv_numops", &vfs_opv_numops);
94SYSCTL_INT(_vfs, OID_AUTO, opv_numops, CTLFLAG_RDTUN, &vfs_opv_numops,
95	0, "Maximum number of operations in vop_t vector");
96
97static int int_cmp(const void *a, const void *b);
98
99static int
100int_cmp(const void *a, const void *b)
101{
102	return(*(const int *)a - *(const int *)b);
103}
104
105/*
106 * Recalculate the operations vector/description (those parts of it that can
107 * be recalculated, that is.)
108 * Always allocate operations vector large enough to hold vfs_opv_numops
109 * entries. The vector is never freed or deallocated once it is initialized,
110 * so that vnodes might safely reference it through their v_op pointer without
111 * vector changing suddenly from under them.
112 */
113static void
114vfs_opv_recalc(void)
115{
116	int i, j, k;
117	int *vfs_op_offsets;
118	vop_t ***opv_desc_vector_p;
119	vop_t **opv_desc_vector;
120	struct vnodeopv_entry_desc *opve_descp;
121	const struct vnodeopv_desc *opv;
122
123	if (vfs_op_descs == NULL)
124		panic("vfs_opv_recalc called with null vfs_op_descs");
125
126	/*
127	 * Allocate and initialize temporary array to store
128	 * offsets. Sort it to put all uninitialized entries
129	 * first and to make holes in existing offset sequence
130	 * detectable.
131	 */
132	MALLOC(vfs_op_offsets, int *,
133		num_op_descs * sizeof(int), M_TEMP, M_WAITOK);
134	if (vfs_op_offsets == NULL)
135		panic("vfs_opv_recalc: no memory");
136	for (i = 0; i < num_op_descs; i++)
137		vfs_op_offsets[i] = vfs_op_descs[i]->vdesc_offset;
138	qsort(vfs_op_offsets, num_op_descs, sizeof(int), int_cmp);
139
140	/*
141	 * Run through and make sure all known descs have an offset.
142	 * Use vfs_op_offsets to locate holes in offset sequence and
143	 * reuse them.
144	 * vop_default_desc is hardwired at offset 1, and offset 0
145	 * is a panic sanity check.
146	 */
147	j = 1; k = 1;
148	for (i = 0; i < num_op_descs; i++) {
149		if (vfs_op_descs[i]->vdesc_offset != 0)
150			continue;
151		/*
152		 * Look at two adjacent entries vfs_op_offsets[j - 1] and
153		 * vfs_op_offsets[j] and see if we can fit a new offset
154		 * number in between. If not, look at the next pair until
155		 * hole is found or the end of the vfs_op_offsets vector is
156		 * reached. j has been initialized to 1 above so that
157		 * referencing (j-1)-th element is safe and the loop will
158		 * never execute if num_op_descs is 1. For each new value s
159		 * of i the j loop pick up from where previous iteration has
160		 * left off. When the last hole has been consumed or if no
161		 * hole has been found, we will start allocating new numbers
162		 * starting from the biggest already available offset + 1.
163		 */
164		for (; j < num_op_descs; j++) {
165			if (vfs_op_offsets[j - 1] < k && vfs_op_offsets[j] > k)
166				break;
167			k = vfs_op_offsets[j] + 1;
168		}
169		vfs_op_descs[i]->vdesc_offset = k++;
170	}
171	FREE(vfs_op_offsets, M_TEMP);
172
173	/* Panic if new vops will cause vector overflow */
174	if (k > vfs_opv_numops)
175		panic("VFS: Ran out of vop_t vector entries. %d entries required, only %d available.\n", k, vfs_opv_numops);
176
177	/*
178	 * Allocate and fill in the vectors
179	 */
180	for (i = 0; i < vnodeopv_num; i++) {
181		opv = vnodeopv_descs[i];
182		opv_desc_vector_p = opv->opv_desc_vector_p;
183		if (*opv_desc_vector_p == NULL)
184			MALLOC(*opv_desc_vector_p, vop_t **,
185				vfs_opv_numops * sizeof(vop_t *), M_VNODE,
186				M_WAITOK | M_ZERO);
187
188		/* Fill in, with slot 0 being to return EOPNOTSUPP */
189		opv_desc_vector = *opv_desc_vector_p;
190		opv_desc_vector[0] = (vop_t *)vop_eopnotsupp;
191		for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
192			opve_descp = &(opv->opv_desc_ops[j]);
193			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
194				opve_descp->opve_impl;
195		}
196
197		/* Replace unfilled routines with their default (slot 1). */
198		opv_desc_vector = *(opv->opv_desc_vector_p);
199		if (opv_desc_vector[1] == NULL)
200			panic("vfs_opv_recalc: vector without a default.");
201		for (j = 0; j < vfs_opv_numops; j++)
202			if (opv_desc_vector[j] == NULL)
203				opv_desc_vector[j] = opv_desc_vector[1];
204	}
205}
206
207/* Add a set of vnode operations (a description) to the table above. */
208void
209vfs_add_vnodeops(const void *data)
210{
211	const struct vnodeopv_desc *opv;
212	const struct vnodeopv_desc **newopv;
213	struct vnodeop_desc **newop;
214	int *newref;
215	struct vnodeop_desc *desc;
216	int i, j;
217
218	opv = (const struct vnodeopv_desc *)data;
219	MALLOC(newopv, const struct vnodeopv_desc **,
220	       (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
221	if (vnodeopv_descs) {
222		bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
223		FREE(vnodeopv_descs, M_VNODE);
224	}
225	newopv[vnodeopv_num] = opv;
226	vnodeopv_descs = newopv;
227	vnodeopv_num++;
228
229	/* See if we have turned up a new vnode op desc */
230	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
231		for (j = 0; j < num_op_descs; j++) {
232			if (desc == vfs_op_descs[j]) {
233				/* found it, increase reference count */
234				vfs_op_desc_refs[j]++;
235				break;
236			}
237		}
238		if (j == num_op_descs) {
239			/* not found, new entry */
240			MALLOC(newop, struct vnodeop_desc **,
241			       (num_op_descs + 1) * sizeof(*newop),
242			       M_VNODE, M_WAITOK);
243			/* new reference count (for unload) */
244			MALLOC(newref, int *,
245				(num_op_descs + 1) * sizeof(*newref),
246				M_VNODE, M_WAITOK);
247			if (vfs_op_descs) {
248				bcopy(vfs_op_descs, newop,
249					num_op_descs * sizeof(*newop));
250				FREE(vfs_op_descs, M_VNODE);
251			}
252			if (vfs_op_desc_refs) {
253				bcopy(vfs_op_desc_refs, newref,
254					num_op_descs * sizeof(*newref));
255				FREE(vfs_op_desc_refs, M_VNODE);
256			}
257			newop[num_op_descs] = desc;
258			newref[num_op_descs] = 1;
259			vfs_op_descs = newop;
260			vfs_op_desc_refs = newref;
261			num_op_descs++;
262		}
263	}
264	vfs_opv_recalc();
265}
266
267/* Remove a vnode type from the vnode description table above. */
268void
269vfs_rm_vnodeops(const void *data)
270{
271	const struct vnodeopv_desc *opv;
272	const struct vnodeopv_desc **newopv;
273	struct vnodeop_desc **newop;
274	int *newref;
275	vop_t **opv_desc_vector;
276	struct vnodeop_desc *desc;
277	int i, j, k;
278
279	opv = (const struct vnodeopv_desc *)data;
280	/* Lower ref counts on descs in the table and release if zero */
281	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
282		for (j = 0; j < num_op_descs; j++) {
283			if (desc == vfs_op_descs[j]) {
284				/* found it, decrease reference count */
285				vfs_op_desc_refs[j]--;
286				break;
287			}
288		}
289		for (j = 0; j < num_op_descs; j++) {
290			if (vfs_op_desc_refs[j] > 0)
291				continue;
292			if (vfs_op_desc_refs[j] < 0)
293				panic("vfs_remove_vnodeops: negative refcnt");
294			/* Entry is going away - replace it with defaultop */
295			for (k = 0; k < vnodeopv_num; k++) {
296				opv_desc_vector =
297					*(vnodeopv_descs[k]->opv_desc_vector_p);
298				if (opv_desc_vector != NULL)
299					opv_desc_vector[desc->vdesc_offset] =
300						opv_desc_vector[1];
301			}
302			MALLOC(newop, struct vnodeop_desc **,
303			       (num_op_descs - 1) * sizeof(*newop),
304			       M_VNODE, M_WAITOK);
305			/* new reference count (for unload) */
306			MALLOC(newref, int *,
307				(num_op_descs - 1) * sizeof(*newref),
308				M_VNODE, M_WAITOK);
309			for (k = j; k < (num_op_descs - 1); k++) {
310				vfs_op_descs[k] = vfs_op_descs[k + 1];
311				vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
312			}
313			bcopy(vfs_op_descs, newop,
314				(num_op_descs - 1) * sizeof(*newop));
315			bcopy(vfs_op_desc_refs, newref,
316				(num_op_descs - 1) * sizeof(*newref));
317			FREE(vfs_op_descs, M_VNODE);
318			FREE(vfs_op_desc_refs, M_VNODE);
319			vfs_op_descs = newop;
320			vfs_op_desc_refs = newref;
321			num_op_descs--;
322		}
323	}
324
325	for (i = 0; i < vnodeopv_num; i++) {
326		if (vnodeopv_descs[i] == opv) {
327			for (j = i; j < (vnodeopv_num - 1); j++)
328				vnodeopv_descs[j] = vnodeopv_descs[j + 1];
329			break;
330		}
331	}
332	if (i == vnodeopv_num)
333		panic("vfs_remove_vnodeops: opv not found");
334	opv_desc_vector = *(opv->opv_desc_vector_p);
335	if (opv_desc_vector != NULL)
336		FREE(opv_desc_vector, M_VNODE);
337	MALLOC(newopv, const struct vnodeopv_desc **,
338	       (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
339	bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
340	FREE(vnodeopv_descs, M_VNODE);
341	vnodeopv_descs = newopv;
342	vnodeopv_num--;
343
344	vfs_opv_recalc();
345}
346
347/*
348 * Routines having to do with the management of the vnode table.
349 */
350struct vattr va_null;
351
352struct vfsconf *
353vfs_byname(const char *name)
354{
355	struct vfsconf *vfsp;
356
357	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
358		if (!strcmp(name, vfsp->vfc_name))
359			return (vfsp);
360	return (NULL);
361}
362
363/*
364 * Initialize the vnode structures and initialize each filesystem type.
365 */
366/* ARGSUSED*/
367static void
368vfsinit(void *dummy)
369{
370
371	vattr_null(&va_null);
372}
373SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
374
375/* Register a new filesystem type in the global table */
376int
377vfs_register(struct vfsconf *vfc)
378{
379	struct sysctl_oid *oidp;
380	struct vfsops *vfsops;
381
382	if (vfs_byname(vfc->vfc_name) != NULL)
383		return EEXIST;
384
385	vfc->vfc_typenum = maxvfsconf++;
386	TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
387
388	/*
389	 * If this filesystem has a sysctl node under vfs
390	 * (i.e. vfs.xxfs), then change the oid number of that node to
391	 * match the filesystem's type number.  This allows user code
392	 * which uses the type number to read sysctl variables defined
393	 * by the filesystem to continue working. Since the oids are
394	 * in a sorted list, we need to make sure the order is
395	 * preserved by re-registering the oid after modifying its
396	 * number.
397	 */
398	SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link)
399		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
400			sysctl_unregister_oid(oidp);
401			oidp->oid_number = vfc->vfc_typenum;
402			sysctl_register_oid(oidp);
403		}
404
405	/*
406	 * Initialise unused ``struct vfsops'' fields, to use
407	 * the vfs_std*() functions.  Note, we need the mount
408	 * and unmount operations, at the least.  The check
409	 * for vfsops available is just a debugging aid.
410	 */
411	KASSERT(vfc->vfc_vfsops != NULL,
412	    ("Filesystem %s has no vfsops", vfc->vfc_name));
413	/*
414	 * Check the mount and unmount operations.
415	 */
416	vfsops = vfc->vfc_vfsops;
417	KASSERT(vfsops->vfs_mount != NULL || vfsops->vfs_nmount != NULL,
418	    ("Filesystem %s has no (n)mount op", vfc->vfc_name));
419	KASSERT(vfsops->vfs_unmount != NULL,
420	    ("Filesystem %s has no unmount op", vfc->vfc_name));
421
422	if (vfsops->vfs_start == NULL)
423		/* make a file system operational */
424		vfsops->vfs_start =	vfs_stdstart;
425	if (vfsops->vfs_root == NULL)
426		/* return file system's root vnode */
427		vfsops->vfs_root =	vfs_stdroot;
428	if (vfsops->vfs_quotactl == NULL)
429		/* quota control */
430		vfsops->vfs_quotactl =	vfs_stdquotactl;
431	if (vfsops->vfs_statfs == NULL)
432		/* return file system's status */
433		vfsops->vfs_statfs =	vfs_stdstatfs;
434	if (vfsops->vfs_sync == NULL)
435		/*
436		 * flush unwritten data (nosync)
437		 * file systems can use vfs_stdsync
438		 * explicitly by setting it in the
439		 * vfsop vector.
440		 */
441		vfsops->vfs_sync =	vfs_stdnosync;
442	if (vfsops->vfs_vget == NULL)
443		/* convert an inode number to a vnode */
444		vfsops->vfs_vget =	vfs_stdvget;
445	if (vfsops->vfs_fhtovp == NULL)
446		/* turn an NFS file handle into a vnode */
447		vfsops->vfs_fhtovp =	vfs_stdfhtovp;
448	if (vfsops->vfs_checkexp == NULL)
449		/* check if file system is exported */
450		vfsops->vfs_checkexp =	vfs_stdcheckexp;
451	if (vfsops->vfs_vptofh == NULL)
452		/* turn a vnode into an NFS file handle */
453		vfsops->vfs_vptofh =	vfs_stdvptofh;
454	if (vfsops->vfs_init == NULL)
455		/* file system specific initialisation */
456		vfsops->vfs_init =	vfs_stdinit;
457	if (vfsops->vfs_uninit == NULL)
458		/* file system specific uninitialisation */
459		vfsops->vfs_uninit =	vfs_stduninit;
460	if (vfsops->vfs_extattrctl == NULL)
461		/* extended attribute control */
462		vfsops->vfs_extattrctl = vfs_stdextattrctl;
463	if (vfsops->vfs_sysctl == NULL)
464		vfsops->vfs_sysctl = vfs_stdsysctl;
465
466	/*
467	 * Call init function for this VFS...
468	 */
469	(*(vfc->vfc_vfsops->vfs_init))(vfc);
470
471	return 0;
472}
473
474
475/* Remove registration of a filesystem type */
476int
477vfs_unregister(struct vfsconf *vfc)
478{
479	struct vfsconf *vfsp;
480	int error, i, maxtypenum;
481
482	i = vfc->vfc_typenum;
483
484	vfsp = vfs_byname(vfc->vfc_name);
485	if (vfsp == NULL)
486		return EINVAL;
487	if (vfsp->vfc_refcount)
488		return EBUSY;
489	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
490		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
491		if (error)
492			return (error);
493	}
494	TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
495	maxtypenum = VFS_GENERIC;
496	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
497		if (maxtypenum < vfsp->vfc_typenum)
498			maxtypenum = vfsp->vfc_typenum;
499	maxvfsconf = maxtypenum + 1;
500	return 0;
501}
502
503/*
504 * Standard kernel module handling code for filesystem modules.
505 * Referenced from VFS_SET().
506 */
507int
508vfs_modevent(module_t mod, int type, void *data)
509{
510	struct vfsconf *vfc;
511	int error = 0;
512
513	vfc = (struct vfsconf *)data;
514
515	switch (type) {
516	case MOD_LOAD:
517		if (vfc)
518			error = vfs_register(vfc);
519		break;
520
521	case MOD_UNLOAD:
522		if (vfc)
523			error = vfs_unregister(vfc);
524		break;
525	default:
526		error = EOPNOTSUPP;
527		break;
528	}
529	return (error);
530}
531