vfs_init.c revision 41056
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 * $Id: vfs_init.c,v 1.38 1998/11/04 03:18:10 peter Exp $
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
53MALLOC_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 */
61extern struct linker_set sysctl__vfs;
62static int mod_xx;
63SYSCTL_INT(_vfs, OID_AUTO, mod0, CTLFLAG_RD, &mod_xx, 0, "");
64SYSCTL_INT(_vfs, OID_AUTO, mod1, CTLFLAG_RD, &mod_xx, 0, "");
65
66/*
67 * Zone for namei
68 */
69struct vm_zone *namei_zone;
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/* Table of known vnodeop vectors (list of VFS vnode vectors) */
89static struct vnodeopv_desc **vnodeopv_descs;
90static int vnodeopv_num;
91
92/* Table of known descs (list of vnode op handlers "vop_access_desc") */
93static struct vnodeop_desc **vfs_op_descs;
94static int *vfs_op_desc_refs;			/* reference counts */
95static int num_op_descs;
96static int vfs_opv_numops;
97
98static void
99vfs_opv_recalc(void)
100{
101	int i, j;
102	vop_t ***opv_desc_vector_p;
103	vop_t **opv_desc_vector;
104	struct vnodeopv_entry_desc *opve_descp;
105	struct vnodeopv_desc *opv;
106
107	if (vfs_op_descs == NULL)
108		panic("vfs_opv_recalc called with null vfs_op_descs");
109
110	/*
111	 * Run through and make sure all known descs have an offset
112	 *
113	 * vop_default_desc is hardwired at offset 1, and offset 0
114	 * is a panic sanity check.
115	 */
116	vfs_opv_numops = 0;
117	for (i = 0; i < num_op_descs; i++)
118		if (vfs_opv_numops < (vfs_op_descs[i]->vdesc_offset + 1))
119			vfs_opv_numops = vfs_op_descs[i]->vdesc_offset + 1;
120	for (i = 0; i < num_op_descs; i++)
121		if (vfs_op_descs[i]->vdesc_offset == 0)
122			vfs_op_descs[i]->vdesc_offset = vfs_opv_numops++;
123	/*
124	 * Allocate and fill in the vectors
125	 */
126	for (i = 0; i < vnodeopv_num; i++) {
127		opv = vnodeopv_descs[i];
128		opv_desc_vector_p = opv->opv_desc_vector_p;
129		if (*opv_desc_vector_p)
130			FREE(*opv_desc_vector_p, M_VNODE);
131		MALLOC(*opv_desc_vector_p, vop_t **,
132		       vfs_opv_numops * sizeof(vop_t *), M_VNODE, M_WAITOK);
133		if (*opv_desc_vector_p == NULL)
134			panic("no memory for vop_t ** vector");
135		bzero(*opv_desc_vector_p, vfs_opv_numops * sizeof(vop_t *));
136
137		/* Fill in, with slot 0 being panic */
138		opv_desc_vector = *opv_desc_vector_p;
139		opv_desc_vector[0] = (vop_t *)vop_panic;
140		for (j = 0; opv->opv_desc_ops[j].opve_op; j++) {
141			opve_descp = &(opv->opv_desc_ops[j]);
142			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
143				opve_descp->opve_impl;
144		}
145
146		/* Replace unfilled routines with their default (slot 1). */
147		opv_desc_vector = *(opv->opv_desc_vector_p);
148		if (opv_desc_vector[1] == NULL)
149			panic("vfs_opv_recalc: vector without a default.");
150		for (j = 0; j < vfs_opv_numops; j++)
151			if (opv_desc_vector[j] == NULL)
152				opv_desc_vector[j] = opv_desc_vector[1];
153	}
154}
155
156void
157vfs_add_vnodeops(void *data)
158{
159	struct vnodeopv_desc *opv;
160	struct vnodeopv_desc **newopv;
161	struct vnodeop_desc **newop;
162	int *newref;
163	vop_t **opv_desc_vector;
164	struct vnodeop_desc *desc;
165	struct vnodeopv_entry_desc *opve_descp;
166	int i, j, k;
167
168	opv = (struct vnodeopv_desc *)data;
169	MALLOC(newopv, struct vnodeopv_desc **,
170	       (vnodeopv_num + 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
171	if (newopv == NULL)
172		panic("vfs_add_vnodeops: no memory");
173	if (vnodeopv_descs) {
174		bcopy(vnodeopv_descs, newopv, vnodeopv_num * sizeof(*newopv));
175		FREE(vnodeopv_descs, M_VNODE);
176	}
177	newopv[vnodeopv_num] = opv;
178	vnodeopv_descs = newopv;
179	vnodeopv_num++;
180
181	/* See if we have turned up a new vnode op desc */
182	opv_desc_vector = *(opv->opv_desc_vector_p);
183	for (i = 0; (desc = opv->opv_desc_ops[i].opve_op); i++) {
184		for (j = 0; j < num_op_descs; j++) {
185			if (desc == vfs_op_descs[j]) {
186				/* found it, increase reference count */
187				vfs_op_desc_refs[j]++;
188				break;
189			}
190		}
191		if (j == num_op_descs) {
192			/* not found, new entry */
193			MALLOC(newop, struct vnodeop_desc **,
194			       (num_op_descs + 1) * sizeof(*newop),
195			       M_VNODE, M_WAITOK);
196			if (newop == NULL)
197				panic("vfs_add_vnodeops: no memory for desc");
198			/* new reference count (for unload) */
199			MALLOC(newref, int *,
200				(num_op_descs + 1) * sizeof(*newref),
201				M_VNODE, M_WAITOK);
202			if (newref == NULL)
203				panic("vfs_add_vnodeops: no memory for refs");
204			if (vfs_op_descs) {
205				bcopy(vfs_op_descs, newop,
206					num_op_descs * sizeof(*newop));
207				FREE(vfs_op_descs, M_VNODE);
208			}
209			if (vfs_op_desc_refs) {
210				bcopy(vfs_op_desc_refs, newref,
211					num_op_descs * sizeof(*newref));
212				FREE(vfs_op_desc_refs, M_VNODE);
213			}
214			newop[num_op_descs] = desc;
215			newref[num_op_descs] = 1;
216			vfs_op_descs = newop;
217			vfs_op_desc_refs = newref;
218			num_op_descs++;
219		}
220	}
221	vfs_opv_recalc();
222}
223
224void
225vfs_rm_vnodeops(void *data)
226{
227	struct vnodeopv_desc *opv;
228	struct vnodeopv_desc **newopv;
229	struct vnodeop_desc **newop;
230	int *newref;
231	vop_t **opv_desc_vector;
232	struct vnodeop_desc *desc;
233	struct vnodeopv_entry_desc *opve_descp;
234	int i, j, k;
235
236	opv = (struct vnodeopv_desc *)data;
237	/* Lower ref counts on descs in the table and release if zero */
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, decrease reference count */
243				vfs_op_desc_refs[j]--;
244				break;
245			}
246		}
247		for (j = 0; j < num_op_descs; j++) {
248			if (vfs_op_desc_refs[j] > 0)
249				continue;
250			if (vfs_op_desc_refs[j] < 0)
251				panic("vfs_remove_vnodeops: negative refcnt");
252			MALLOC(newop, struct vnodeop_desc **,
253			       (num_op_descs - 1) * sizeof(*newop),
254			       M_VNODE, M_WAITOK);
255			if (newop == NULL)
256				panic("vfs_remove_vnodeops: no memory for desc");
257			/* new reference count (for unload) */
258			MALLOC(newref, int *,
259				(num_op_descs - 1) * sizeof(*newref),
260				M_VNODE, M_WAITOK);
261			if (newref == NULL)
262				panic("vfs_remove_vnodeops: no memory for refs");
263			for (k = j; k < (num_op_descs - 1); k++) {
264				vfs_op_descs[k] = vfs_op_descs[k + 1];
265				vfs_op_desc_refs[k] = vfs_op_desc_refs[k + 1];
266			}
267			bcopy(vfs_op_descs, newop,
268				(num_op_descs - 1) * sizeof(*newop));
269			bcopy(vfs_op_desc_refs, newref,
270				(num_op_descs - 1) * sizeof(*newref));
271			FREE(vfs_op_descs, M_VNODE);
272			FREE(vfs_op_desc_refs, M_VNODE);
273			vfs_op_descs = newop;
274			vfs_op_desc_refs = newref;
275			num_op_descs--;
276		}
277	}
278
279	for (i = 0; i < vnodeopv_num; i++) {
280		if (vnodeopv_descs[i] == opv) {
281			for (j = i; j < (vnodeopv_num - 1); j++)
282				vnodeopv_descs[j] = vnodeopv_descs[j + 1];
283			break;
284		}
285	}
286	if (i == vnodeopv_num)
287		panic("vfs_remove_vnodeops: opv not found");
288	MALLOC(newopv, struct vnodeopv_desc **,
289	       (vnodeopv_num - 1) * sizeof(*newopv), M_VNODE, M_WAITOK);
290	if (newopv == NULL)
291		panic("vfs_remove_vnodeops: no memory");
292	bcopy(vnodeopv_descs, newopv, (vnodeopv_num - 1) * sizeof(*newopv));
293	FREE(vnodeopv_descs, M_VNODE);
294	vnodeopv_descs = newopv;
295	vnodeopv_num--;
296
297	vfs_opv_recalc();
298}
299
300/*
301 * Routines having to do with the management of the vnode table.
302 */
303struct vattr va_null;
304
305/*
306 * Initialize the vnode structures and initialize each file system type.
307 */
308/* ARGSUSED*/
309static void
310vfsinit(void *dummy)
311{
312
313	namei_zone = zinit("NAMEI", MAXPATHLEN, 0, 0, 2);
314
315	/*
316	 * Initialize the vnode table
317	 */
318	vntblinit();
319	/*
320	 * Initialize the vnode name cache
321	 */
322	nchinit();
323	/*
324	 * Initialize each file system type.
325	 * Vfs type numbers must be distinct from VFS_GENERIC (and VFS_VFSCONF).
326	 */
327	vattr_null(&va_null);
328	maxvfsconf = VFS_GENERIC + 1;
329}
330SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vfsinit, NULL)
331
332int
333vfs_register(struct vfsconf *vfc)
334{
335	struct linker_set *l;
336	struct sysctl_oid **oidpp;
337	struct vfsconf *vfsp;
338	int i, exists;
339
340	vfsp = NULL;
341	l = &sysctl__vfs;
342	if (vfsconf)
343		for (vfsp = vfsconf; vfsp->vfc_next; vfsp = vfsp->vfc_next)
344			if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
345				return EEXIST;
346
347	vfc->vfc_typenum = maxvfsconf++;
348	if (vfc->vfc_vfsops->vfs_oid != NULL) {
349		/*
350		 * Attach the oid to the "vfs" node of the sysctl tree if
351		 * it isn't already there (it will be there for statically
352		 * configured vfs's).
353		 */
354		exists = 0;
355		for (i = l->ls_length,
356		    oidpp = (struct sysctl_oid **)l->ls_items;
357		    i-- != 0; oidpp++)
358			if (*oidpp == vfc->vfc_vfsops->vfs_oid) {
359				exists = 1;
360				break;
361			}
362		if (exists == 0)
363			for (i = l->ls_length,
364			    oidpp = (struct sysctl_oid **)l->ls_items;
365			    i-- != 0; oidpp++) {
366				if (*oidpp == NULL ||
367				    *oidpp == &sysctl___vfs_mod0 ||
368				    *oidpp == &sysctl___vfs_mod1) {
369					*oidpp = vfc->vfc_vfsops->vfs_oid;
370					break;
371				}
372			}
373
374		vfc->vfc_vfsops->vfs_oid->oid_number = vfc->vfc_typenum;
375		sysctl_order_all();
376	}
377	if (vfsp)
378		vfsp->vfc_next = vfc;
379	else
380		vfsconf = vfc;
381	vfc->vfc_next = NULL;
382
383	/*
384	 * Call init function for this VFS...
385	 */
386	(*(vfc->vfc_vfsops->vfs_init))(vfc);
387
388	return 0;
389}
390
391
392int
393vfs_unregister(struct vfsconf *vfc)
394{
395	struct linker_set *l;
396	struct sysctl_oid **oidpp;
397	struct vfsconf *vfsp, *prev_vfsp;
398	int error, i, maxtypenum;
399
400	i = vfc->vfc_typenum;
401
402	prev_vfsp = NULL;
403	for (vfsp = vfsconf; vfsp;
404			prev_vfsp = vfsp, vfsp = vfsp->vfc_next) {
405		if (!strcmp(vfc->vfc_name, vfsp->vfc_name))
406			break;
407	}
408	if (vfsp == NULL)
409		return EINVAL;
410	if (vfsp->vfc_refcount)
411		return EBUSY;
412	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
413		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
414		if (error)
415			return (error);
416	}
417	if (prev_vfsp)
418		prev_vfsp->vfc_next = vfsp->vfc_next;
419	else
420		vfsconf = vfsp->vfc_next;
421	if (vfsp->vfc_vfsops->vfs_oid != NULL) {
422		l = &sysctl__vfs;
423		for (i = l->ls_length,
424		    oidpp = (struct sysctl_oid **)l->ls_items;
425		    i--; oidpp++) {
426			if (*oidpp == vfsp->vfc_vfsops->vfs_oid) {
427				*oidpp = NULL;
428				sysctl_order_all();
429				break;
430			}
431		}
432	}
433	maxtypenum = VFS_GENERIC;
434	for (vfsp = vfsconf; vfsp != NULL; vfsp = vfsp->vfc_next)
435		if (maxtypenum < vfsp->vfc_typenum)
436			maxtypenum = vfsp->vfc_typenum;
437	maxvfsconf = maxtypenum + 1;
438	return 0;
439}
440
441int
442vfs_modevent(module_t mod, modeventtype_t type, void *data)
443{
444	struct vfsconf *vfc;
445	struct vnodeopv_desc *opv;
446	int i;
447	int error = 0;
448
449	vfc = (struct vfsconf *)data;
450
451	switch (type) {
452	case MOD_LOAD:
453		if (vfc)
454			error = vfs_register(vfc);
455		break;
456
457	case MOD_UNLOAD:
458		if (vfc)
459			error = vfs_unregister(vfc);
460		break;
461	default:	/* including MOD_SHUTDOWN */
462		break;
463	}
464	return (error);
465}
466