vfs_init.c revision 8876
198937Sdes/*
2181111Sdes * Copyright (c) 1989, 1993
398937Sdes *	The Regents of the University of California.  All rights reserved.
498937Sdes *
598937Sdes * This code is derived from software contributed
698937Sdes * to Berkeley by John Heidemann of the UCLA Ficus project.
798937Sdes *
898937Sdes * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
998937Sdes *
1098937Sdes * Redistribution and use in source and binary forms, with or without
1198937Sdes * modification, are permitted provided that the following conditions
1298937Sdes * are met:
1398937Sdes * 1. Redistributions of source code must retain the above copyright
1498937Sdes *    notice, this list of conditions and the following disclaimer.
1598937Sdes * 2. Redistributions in binary form must reproduce the above copyright
1698937Sdes *    notice, this list of conditions and the following disclaimer in the
1798937Sdes *    documentation and/or other materials provided with the distribution.
1898937Sdes * 3. All advertising materials mentioning features or use of this software
1998937Sdes *    must display the following acknowledgement:
2098937Sdes *	This product includes software developed by the University of
2198937Sdes *	California, Berkeley and its contributors.
2298937Sdes * 4. Neither the name of the University nor the names of its contributors
2398937Sdes *    may be used to endorse or promote products derived from this software
2498937Sdes *    without specific prior written permission.
2598937Sdes *
2698937Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2798937Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2898937Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2998937Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3098937Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3198937Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3298937Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3398937Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3498937Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3598937Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3698937Sdes * SUCH DAMAGE.
37106121Sdes *
3898937Sdes *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
3998937Sdes * $Id: vfs_init.c,v 1.9 1994/10/20 00:48:28 wollman Exp $
4098937Sdes */
4198937Sdes
4298937Sdes
4398937Sdes#include <sys/param.h>
4498937Sdes#include <sys/systm.h>
4598937Sdes#include <sys/kernel.h>
4698937Sdes#include <sys/mount.h>
4798937Sdes#include <sys/time.h>
48106121Sdes#include <sys/vnode.h>
4998937Sdes#include <sys/stat.h>
5098937Sdes#include <sys/namei.h>
5198937Sdes#include <sys/ucred.h>
52106121Sdes#include <sys/buf.h>
5398937Sdes#include <sys/errno.h>
54106121Sdes#include <sys/malloc.h>
55162852Sdes#include <sys/proc.h>
5698937Sdes#include <vm/vm.h>
57162852Sdes#include <sys/sysctl.h>
58162852Sdes
5998937Sdes/*
6098937Sdes * Sigh, such primitive tools are these...
6198937Sdes */
62106121Sdes#if 0
63106121Sdes#define DODEBUG(A) A
64106121Sdes#else
65126274Sdes#define DODEBUG(A)
66126274Sdes#endif
67126274Sdes
68126274Sdesstruct vfsconf void_vfsconf;
69126274Sdes
70126274Sdesextern struct linker_set vfs_opv_descs_;
71126274Sdes#define vfs_opv_descs ((struct vnodeopv_desc **)vfs_opv_descs_.ls_items)
72126274Sdes
73126274Sdesextern struct linker_set vfs_set;
74126274Sdesstruct vfsops *vfssw[MOUNT_MAXTYPE + 1];
75126274Sdesstruct vfsconf *vfsconf[MOUNT_MAXTYPE + 1];
76126274Sdes
77126274Sdesextern struct vnodeop_desc *vfs_op_descs[];
78126274Sdes				/* and the operations they perform */
79126274Sdes/*
80126274Sdes * This code doesn't work if the defn is **vnodop_defns with cc.
81126274Sdes * The problem is because of the compiler sometimes putting in an
82126274Sdes * extra level of indirection for arrays.  It's an interesting
83126274Sdes * "feature" of C.
84126274Sdes */
85126274Sdesint vfs_opv_numops;
86126274Sdes
87106121Sdestypedef int (*PFI)(); /* the standard Pointer to a Function returning an Int */
88106121Sdes
8998937Sdes/*
9098937Sdes * A miscellaneous routine.
91106121Sdes * A generic "default" routine that just returns an error.
92106121Sdes */
93106121Sdesint
94106121Sdesvn_default_error()
95124208Sdes{
9698937Sdes
97124208Sdes	return (EOPNOTSUPP);
98124208Sdes}
99106121Sdes
10098937Sdes/*
10198937Sdes * vfs_init.c
10298937Sdes *
10398937Sdes * Allocate and fill in operations vectors.
10498937Sdes *
10598937Sdes * An undocumented feature of this approach to defining operations is that
10698937Sdes * there can be multiple entries in vfs_opv_descs for the same operations
107106121Sdes * vector. This allows third parties to extend the set of operations
108106121Sdes * supported by another layer in a binary compatibile way. For example,
109106121Sdes * assume that NFS needed to be modified to support Ficus. NFS has an entry
11098937Sdes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
111106121Sdes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
112106121Sdes * listing those new operations Ficus adds to NFS, all without modifying the
113106121Sdes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
114124208Sdes * that is a(whole)nother story.) This is a feature.
115124208Sdes */
116106121Sdesvoid
117124208Sdesvfs_opv_init(struct vnodeopv_desc **them)
118124208Sdes{
11998937Sdes	int i, j, k;
120124208Sdes	int (***opv_desc_vector_p)();
121106121Sdes	int (**opv_desc_vector)();
122124208Sdes	struct vnodeopv_entry_desc *opve_descp;
123106121Sdes
124124208Sdes	/*
125106121Sdes	 * Allocate the dynamic vectors and fill them in.
126106121Sdes	 */
127106121Sdes	for (i=0; them[i]; i++) {
128124208Sdes		opv_desc_vector_p = them[i]->opv_desc_vector_p;
129124208Sdes		/*
130124208Sdes		 * Allocate and init the vector, if it needs it.
131124208Sdes		 * Also handle backwards compatibility.
132124208Sdes		 */
133124208Sdes		if (*opv_desc_vector_p == NULL) {
134124208Sdes			/* XXX - shouldn't be M_VNODE */
135124208Sdes			MALLOC(*opv_desc_vector_p, PFI*,
136124208Sdes			       vfs_opv_numops*sizeof(PFI), M_VNODE, M_WAITOK);
137124208Sdes			bzero (*opv_desc_vector_p, vfs_opv_numops*sizeof(PFI));
138124208Sdes			DODEBUG(printf("vector at %x allocated\n",
139124208Sdes			    opv_desc_vector_p));
140106121Sdes		}
141124208Sdes		opv_desc_vector = *opv_desc_vector_p;
142124208Sdes		for (j=0; them[i]->opv_desc_ops[j].opve_op; j++) {
143124208Sdes			opve_descp = &(them[i]->opv_desc_ops[j]);
144106121Sdes
145106121Sdes			/*
146106121Sdes			 * Sanity check:  is this operation listed
147124208Sdes			 * in the list of operations?  We check this
148106121Sdes			 * by seeing if its offest is zero.  Since
149106121Sdes			 * the default routine should always be listed
15098937Sdes			 * first, it should be the only one with a zero
151106121Sdes			 * offset.  Any other operation with a zero
152106121Sdes			 * offset is probably not listed in
153106121Sdes			 * vfs_op_descs, and so is probably an error.
154106121Sdes			 *
15598937Sdes			 * A panic here means the layer programmer
156124208Sdes			 * has committed the all-too common bug
157124208Sdes			 * of adding a new operation to the layer's
158106121Sdes			 * list of vnode operations but
159106121Sdes			 * not adding the operation to the system-wide
160106121Sdes			 * list of supported operations.
161124208Sdes			 */
162106121Sdes			if (opve_descp->opve_op->vdesc_offset == 0 &&
163106121Sdes				    opve_descp->opve_op->vdesc_offset !=
164124208Sdes				    	VOFFSET(vop_default)) {
165124208Sdes				printf("operation %s not listed in %s.\n",
166106121Sdes				    opve_descp->opve_op->vdesc_name,
167106121Sdes				    "vfs_op_descs");
168106121Sdes				panic ("vfs_opv_init: bad operation");
169124208Sdes			}
170106121Sdes			/*
171106121Sdes			 * Fill in this entry.
172106121Sdes			 */
173124208Sdes			opv_desc_vector[opve_descp->opve_op->vdesc_offset] =
174124208Sdes					opve_descp->opve_impl;
175124208Sdes		}
176124208Sdes	}
177146998Sdes	/*
178124208Sdes	 * Finally, go back and replace unfilled routines
179124208Sdes	 * with their default.  (Sigh, an O(n^3) algorithm.  I
180124208Sdes	 * could make it better, but that'd be work, and n is small.)
181124208Sdes	 */
182106121Sdes	for (i = 0; them[i]; i++) {
183106121Sdes		opv_desc_vector = *(them[i]->opv_desc_vector_p);
184106121Sdes		/*
185106121Sdes		 * Force every operations vector to have a default routine.
18698937Sdes		 */
18798937Sdes		if (opv_desc_vector[VOFFSET(vop_default)]==NULL) {
188124208Sdes			panic("vfs_opv_init: operation vector without default routine.");
189124208Sdes		}
190124208Sdes		for (k = 0; k<vfs_opv_numops; k++)
191124208Sdes			if (opv_desc_vector[k] == NULL)
192124208Sdes				opv_desc_vector[k] =
193124208Sdes					opv_desc_vector[VOFFSET(vop_default)];
194124208Sdes	}
195124208Sdes}
196124208Sdes
197124208Sdes/*
198124208Sdes * Initialize known vnode operations vectors.
199124208Sdes */
200124208Sdesvoid
201124208Sdesvfs_op_init()
202124208Sdes{
203124208Sdes	int i;
204124208Sdes
205124208Sdes	DODEBUG(printf("Vnode_interface_init.\n"));
206124208Sdes	/*
207124208Sdes	 * Set all vnode vectors to a well known value.
208124208Sdes	 */
209124208Sdes	for (i = 0; vfs_opv_descs[i]; i++)
210126274Sdes		*(vfs_opv_descs[i]->opv_desc_vector_p) = NULL;
211124208Sdes	/*
212124208Sdes	 * Figure out how many ops there are by counting the table,
213106121Sdes	 * and assign each its offset.
214124208Sdes	 */
215106121Sdes	for (vfs_opv_numops = 0, i = 0; vfs_op_descs[i]; i++) {
216124208Sdes		vfs_op_descs[i]->vdesc_offset = vfs_opv_numops;
217124208Sdes		vfs_opv_numops++;
21898937Sdes	}
219106121Sdes	DODEBUG(printf ("vfs_opv_numops=%d\n", vfs_opv_numops));
220106121Sdes}
221106121Sdes
222124208Sdes/*
223124208Sdes * Routines having to do with the management of the vnode table.
22498937Sdes */
225106121Sdesextern struct vnodeops dead_vnodeops;
226124208Sdesextern struct vnodeops spec_vnodeops;
227124208Sdesextern void vclean();
228106121Sdesstruct vattr va_null;
229124208Sdes
230124208Sdes/*
231124208Sdes * Initialize the vnode structures and initialize each file system type.
232124208Sdes */
233124208Sdesvoid
234124208Sdesvfsinit()
23598937Sdes{
236124208Sdes	struct vfsops **vfsp;
237124208Sdes	struct vfsconf **vfc;
238124208Sdes	int i;
239124208Sdes
240124208Sdes	/*
241124208Sdes	 * Initialize the VFS switch table
242124208Sdes	 */
243106121Sdes	for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
244106121Sdes		vfsconf[i] = &void_vfsconf;
245124208Sdes	}
246106121Sdes
247124208Sdes	vfc = (struct vfsconf **)vfs_set.ls_items;
24898937Sdes	while(*vfc) {
249106121Sdes		vfssw[(**vfc).vfc_index] = (**vfc).vfc_vfsops;
250106121Sdes		vfsconf[(**vfc).vfc_index] = *vfc;
251106121Sdes		vfc++;
252124208Sdes	}
253106121Sdes
254124208Sdes	/*
255106121Sdes	 * Initialize the vnode table
256124208Sdes	 */
257124208Sdes	vntblinit();
258106121Sdes	/*
259124208Sdes	 * Initialize the vnode name cache
260124208Sdes	 */
261106121Sdes	nchinit();
262106121Sdes	/*
263106121Sdes	 * Build vnode operation vectors.
264124208Sdes	 */
265124208Sdes	vfs_op_init();
266124208Sdes	vfs_opv_init(vfs_opv_descs);   /* finish the job */
267124208Sdes	/*
268124208Sdes	 * Initialize each file system type.
269124208Sdes	 */
270124208Sdes	vattr_null(&va_null);
271124208Sdes	for (vfsp = &vfssw[0]; vfsp <= &vfssw[MOUNT_MAXTYPE]; vfsp++) {
272124208Sdes		if (*vfsp == NULL)
273124208Sdes			continue;
274124208Sdes		(*(*vfsp)->vfs_init)();
275124208Sdes	}
276124208Sdes}
277124208Sdes
27898937Sdes/*
279124208Sdes * kernel related system variables.
280124208Sdes */
281124208Sdesint
28298937Sdesfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
283124208Sdes	int *name;
284124208Sdes	u_int namelen;
285124208Sdes	void *oldp;
286106121Sdes	size_t *oldlenp;
287124208Sdes	void *newp;
288124208Sdes	size_t newlen;
289124208Sdes	struct proc *p;
290124208Sdes{
291106121Sdes	int i;
292124208Sdes	int error;
293124208Sdes	int buflen = *oldlenp;
294124208Sdes	caddr_t where = oldp, start = oldp;
295106121Sdes
296124208Sdes	switch (name[0]) {
297124208Sdes	case FS_VFSCONF:
298124208Sdes		if (namelen != 1) return ENOTDIR;
299106121Sdes
300124208Sdes		if (oldp == NULL) {
301124208Sdes			*oldlenp = (MOUNT_MAXTYPE+1) * sizeof(struct vfsconf);
302124208Sdes			return 0;
303124208Sdes		}
304124208Sdes		if (newp) {
305124208Sdes			return EINVAL;
306124208Sdes		}
307124208Sdes
308124208Sdes		for(i = 0; i < MOUNT_MAXTYPE + 1; i++) {
309124208Sdes			if(buflen < sizeof *vfsconf[i]) {
310124208Sdes				*oldlenp = where - start;
311124208Sdes				return ENOMEM;
312124208Sdes			}
313124208Sdes
314124208Sdes			error = copyout(vfsconf[i], where, sizeof *vfsconf[i]);
315124208Sdes			if(error)
316124208Sdes				return error;
317124208Sdes			where += sizeof *vfsconf[i];
318124208Sdes			buflen -= sizeof *vfsconf[i];
319124208Sdes		}
320124208Sdes		*oldlenp = where - start;
321124208Sdes		return 0;
322124208Sdes
323124208Sdes	default:
324124208Sdes		if(namelen < 1) return EINVAL;
325106121Sdes
326124208Sdes		i = name[0];
327124208Sdes
328124208Sdes		if(i <= MOUNT_MAXTYPE
329124208Sdes		   && vfssw[i]
330124208Sdes		   && vfssw[i]->vfs_sysctl) {
331106121Sdes			return vfssw[i]->vfs_sysctl(name + 1, namelen - 1,
332124208Sdes						    oldp, oldlenp,
333124208Sdes						    newp, newlen, p);
334106121Sdes		}
335124208Sdes
336124208Sdes		return (EOPNOTSUPP);
337124208Sdes	}
338124208Sdes	/* NOTREACHED */
339124208Sdes}
340124208Sdes
341124208Sdes/*
342124208Sdes * This goop is here to support a loadable NFS module... grumble...
343124208Sdes */
344124208Sdesvoid (*lease_check) __P((struct vnode *, struct proc *, struct ucred *, int))
345124208Sdes     = 0;
346124208Sdesvoid (*lease_updatetime) __P((int))
347124208Sdes     = 0;
348124208Sdes
349124208Sdes