1139804Simp/*-
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed
61541Srgrimes * to Berkeley by John Heidemann of the UCLA Ficus project.
71541Srgrimes *
81541Srgrimes * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *	@(#)vfs_init.c	8.3 (Berkeley) 1/4/94
351541Srgrimes */
361541Srgrimes
37116182Sobrien#include <sys/cdefs.h>
38116182Sobrien__FBSDID("$FreeBSD$");
391541Srgrimes
401541Srgrimes#include <sys/param.h>
412112Swollman#include <sys/systm.h>
42225537Srmacklem#include <sys/fnv_hash.h>
432946Swollman#include <sys/kernel.h>
44138350Sphk#include <sys/linker.h>
451541Srgrimes#include <sys/mount.h>
46138350Sphk#include <sys/proc.h>
47269457Skib#include <sys/sx.h>
48159590Sjhb#include <sys/syscallsubr.h>
4938869Sbde#include <sys/sysctl.h>
501541Srgrimes#include <sys/vnode.h>
511541Srgrimes#include <sys/malloc.h>
521541Srgrimes
53141634Sphkstatic int	vfs_register(struct vfsconf *);
54141634Sphkstatic int	vfs_unregister(struct vfsconf *);
5512577Sbde
5630354SphkMALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
5730354Sphk
5810358Sjulian/*
5969664Speter * The highest defined VFS number.
6029653Sdyson */
6169664Speterint maxvfsconf = VFS_GENERIC + 1;
6291690Seivind
6391690Seivind/*
6491690Seivind * Single-linked list of configured VFSes.
6591690Seivind * New entries are added/deleted by vfs_register()/vfs_unregister()
6691690Seivind */
67132710Sphkstruct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
68269457Skibstruct sx vfsconf_sx;
69269457SkibSX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
7052780Smsmith
7152780Smsmith/*
72225537Srmacklem * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
73225537Srmacklem * calculation on vfc_name, so that it doesn't change when file systems are
74225537Srmacklem * loaded in a different order. This will avoid the NFS server file handles from
75225537Srmacklem * changing for file systems that use vfc_typenum in their fsid.
76225537Srmacklem */
77225537Srmacklemstatic int	vfs_typenumhash = 1;
78225537SrmacklemSYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
79225537Srmacklem    "Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
80225537Srmacklem    "change when file systems are loaded in a different order.");
81225537Srmacklem
82225537Srmacklem/*
83135279Sphk * A Zen vnode attribute structure.
84135279Sphk *
85135279Sphk * Initialized when the first filesystem registers by vfs_register().
86135279Sphk */
87135279Sphkstruct vattr va_null;
88135279Sphk
89135279Sphk/*
901541Srgrimes * vfs_init.c
911541Srgrimes *
921541Srgrimes * Allocate and fill in operations vectors.
931541Srgrimes *
941541Srgrimes * An undocumented feature of this approach to defining operations is that
951541Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations
961541Srgrimes * vector. This allows third parties to extend the set of operations
971541Srgrimes * supported by another layer in a binary compatibile way. For example,
981541Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry
991541Srgrimes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
1001541Srgrimes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
1011541Srgrimes * listing those new operations Ficus adds to NFS, all without modifying the
1021541Srgrimes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
1031541Srgrimes * that is a(whole)nother story.) This is a feature.
1041541Srgrimes */
10541056Speter
106138290Sphk/*
107138290Sphk * Routines having to do with the management of the vnode table.
108138290Sphk */
10941056Speter
110269457Skibstatic struct vfsconf *
111269457Skibvfs_byname_locked(const char *name)
112132710Sphk{
113132710Sphk	struct vfsconf *vfsp;
114132710Sphk
115269457Skib	sx_assert(&vfsconf_sx, SA_LOCKED);
116138497Sphk	if (!strcmp(name, "ffs"))
117138497Sphk		name = "ufs";
118269457Skib	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
119132710Sphk		if (!strcmp(name, vfsp->vfc_name))
120132710Sphk			return (vfsp);
121269457Skib	}
122132710Sphk	return (NULL);
123132710Sphk}
124132710Sphk
125138350Sphkstruct vfsconf *
126269457Skibvfs_byname(const char *name)
127269457Skib{
128269457Skib	struct vfsconf *vfsp;
129269457Skib
130269457Skib	vfsconf_slock();
131269457Skib	vfsp = vfs_byname_locked(name);
132269457Skib	vfsconf_sunlock();
133269457Skib	return (vfsp);
134269457Skib}
135269457Skib
136269457Skibstruct vfsconf *
137138350Sphkvfs_byname_kld(const char *fstype, struct thread *td, int *error)
138138350Sphk{
139138350Sphk	struct vfsconf *vfsp;
140247071Sjamie	int fileid, loaded;
141138350Sphk
142138350Sphk	vfsp = vfs_byname(fstype);
143138350Sphk	if (vfsp != NULL)
144138350Sphk		return (vfsp);
145138350Sphk
146159956Sjhb	/* Try to load the respective module. */
147159590Sjhb	*error = kern_kldload(td, fstype, &fileid);
148247071Sjamie	loaded = (*error == 0);
149247071Sjamie	if (*error == EEXIST)
150247071Sjamie		*error = 0;
151138350Sphk	if (*error)
152138350Sphk		return (NULL);
153159590Sjhb
154138350Sphk	/* Look up again to see if the VFS was loaded. */
155138350Sphk	vfsp = vfs_byname(fstype);
156138350Sphk	if (vfsp == NULL) {
157247071Sjamie		if (loaded)
158247071Sjamie			(void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
159138350Sphk		*error = ENODEV;
160138350Sphk		return (NULL);
161138350Sphk	}
162138350Sphk	return (vfsp);
163138350Sphk}
164138350Sphk
165138350Sphk
16696755Strhodes/* Register a new filesystem type in the global table */
167141634Sphkstatic int
16841056Spetervfs_register(struct vfsconf *vfc)
16940435Speter{
17044549Sdfr	struct sysctl_oid *oidp;
171116271Sphk	struct vfsops *vfsops;
172135279Sphk	static int once;
173225537Srmacklem	struct vfsconf *tvfc;
174225537Srmacklem	uint32_t hashval;
175225537Srmacklem	int secondpass;
176135279Sphk
177135279Sphk	if (!once) {
178135279Sphk		vattr_null(&va_null);
179135279Sphk		once = 1;
180135279Sphk	}
181116271Sphk
182132902Sphk	if (vfc->vfc_version != VFS_VERSION) {
183132902Sphk		printf("ERROR: filesystem %s, unsupported ABI version %x\n",
184132902Sphk		    vfc->vfc_name, vfc->vfc_version);
185132902Sphk		return (EINVAL);
186132902Sphk	}
187269457Skib	vfsconf_lock();
188269457Skib	if (vfs_byname_locked(vfc->vfc_name) != NULL) {
189269457Skib		vfsconf_unlock();
190241896Skib		return (EEXIST);
191269457Skib	}
19240435Speter
193225537Srmacklem	if (vfs_typenumhash != 0) {
194225537Srmacklem		/*
195225537Srmacklem		 * Calculate a hash on vfc_name to use for vfc_typenum. Unless
196225537Srmacklem		 * all of 1<->255 are assigned, it is limited to 8bits since
197225537Srmacklem		 * that is what ZFS uses from vfc_typenum and is also the
198225537Srmacklem		 * preferred range for vfs_getnewfsid().
199225537Srmacklem		 */
200225537Srmacklem		hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
201225537Srmacklem		hashval &= 0xff;
202225537Srmacklem		secondpass = 0;
203225537Srmacklem		do {
204225537Srmacklem			/* Look for and fix any collision. */
205225537Srmacklem			TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
206225537Srmacklem				if (hashval == tvfc->vfc_typenum) {
207225537Srmacklem					if (hashval == 255 && secondpass == 0) {
208225537Srmacklem						hashval = 1;
209225537Srmacklem						secondpass = 1;
210225537Srmacklem					} else
211225537Srmacklem						hashval++;
212225537Srmacklem					break;
213225537Srmacklem				}
214225537Srmacklem			}
215225537Srmacklem		} while (tvfc != NULL);
216225537Srmacklem		vfc->vfc_typenum = hashval;
217225537Srmacklem		if (vfc->vfc_typenum >= maxvfsconf)
218225537Srmacklem			maxvfsconf = vfc->vfc_typenum + 1;
219225537Srmacklem	} else
220225537Srmacklem		vfc->vfc_typenum = maxvfsconf++;
221132710Sphk	TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
22240435Speter
22340435Speter	/*
224116271Sphk	 * Initialise unused ``struct vfsops'' fields, to use
225116271Sphk	 * the vfs_std*() functions.  Note, we need the mount
226116271Sphk	 * and unmount operations, at the least.  The check
227116271Sphk	 * for vfsops available is just a debugging aid.
228116271Sphk	 */
229116271Sphk	KASSERT(vfc->vfc_vfsops != NULL,
230116271Sphk	    ("Filesystem %s has no vfsops", vfc->vfc_name));
231116271Sphk	/*
232116271Sphk	 * Check the mount and unmount operations.
233116271Sphk	 */
234116271Sphk	vfsops = vfc->vfc_vfsops;
235138509Sphk	KASSERT(vfsops->vfs_mount != NULL,
236138509Sphk	    ("Filesystem %s has no mount op", vfc->vfc_name));
237116271Sphk	KASSERT(vfsops->vfs_unmount != NULL,
238116271Sphk	    ("Filesystem %s has no unmount op", vfc->vfc_name));
239116271Sphk
240116271Sphk	if (vfsops->vfs_root == NULL)
241116271Sphk		/* return file system's root vnode */
242116271Sphk		vfsops->vfs_root =	vfs_stdroot;
243116271Sphk	if (vfsops->vfs_quotactl == NULL)
244116271Sphk		/* quota control */
245116271Sphk		vfsops->vfs_quotactl =	vfs_stdquotactl;
246116271Sphk	if (vfsops->vfs_statfs == NULL)
247116271Sphk		/* return file system's status */
248116271Sphk		vfsops->vfs_statfs =	vfs_stdstatfs;
249116271Sphk	if (vfsops->vfs_sync == NULL)
250116271Sphk		/*
251116271Sphk		 * flush unwritten data (nosync)
252116271Sphk		 * file systems can use vfs_stdsync
253116271Sphk		 * explicitly by setting it in the
254116271Sphk		 * vfsop vector.
255116271Sphk		 */
256116271Sphk		vfsops->vfs_sync =	vfs_stdnosync;
257116271Sphk	if (vfsops->vfs_vget == NULL)
258116271Sphk		/* convert an inode number to a vnode */
259116271Sphk		vfsops->vfs_vget =	vfs_stdvget;
260116271Sphk	if (vfsops->vfs_fhtovp == NULL)
261116271Sphk		/* turn an NFS file handle into a vnode */
262116271Sphk		vfsops->vfs_fhtovp =	vfs_stdfhtovp;
263116271Sphk	if (vfsops->vfs_checkexp == NULL)
264116271Sphk		/* check if file system is exported */
265116271Sphk		vfsops->vfs_checkexp =	vfs_stdcheckexp;
266116271Sphk	if (vfsops->vfs_init == NULL)
267116271Sphk		/* file system specific initialisation */
268116271Sphk		vfsops->vfs_init =	vfs_stdinit;
269116271Sphk	if (vfsops->vfs_uninit == NULL)
270116271Sphk		/* file system specific uninitialisation */
271116271Sphk		vfsops->vfs_uninit =	vfs_stduninit;
272116271Sphk	if (vfsops->vfs_extattrctl == NULL)
273116271Sphk		/* extended attribute control */
274116271Sphk		vfsops->vfs_extattrctl = vfs_stdextattrctl;
275131733Salfred	if (vfsops->vfs_sysctl == NULL)
276131733Salfred		vfsops->vfs_sysctl = vfs_stdsysctl;
277116271Sphk
278116271Sphk	/*
27940435Speter	 * Call init function for this VFS...
28040435Speter	 */
28140435Speter	(*(vfc->vfc_vfsops->vfs_init))(vfc);
282269457Skib	vfsconf_unlock();
28340435Speter
284269457Skib	/*
285269457Skib	 * If this filesystem has a sysctl node under vfs
286269457Skib	 * (i.e. vfs.xxfs), then change the oid number of that node to
287269457Skib	 * match the filesystem's type number.  This allows user code
288269457Skib	 * which uses the type number to read sysctl variables defined
289269457Skib	 * by the filesystem to continue working. Since the oids are
290269457Skib	 * in a sorted list, we need to make sure the order is
291269457Skib	 * preserved by re-registering the oid after modifying its
292269457Skib	 * number.
293269457Skib	 */
294287835Smjg	sysctl_wlock();
295269457Skib	SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) {
296269457Skib		if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
297269457Skib			sysctl_unregister_oid(oidp);
298269457Skib			oidp->oid_number = vfc->vfc_typenum;
299269457Skib			sysctl_register_oid(oidp);
300269457Skib			break;
301269457Skib		}
302269457Skib	}
303287835Smjg	sysctl_wunlock();
304269457Skib
305269457Skib	return (0);
3061541Srgrimes}
3072946Swollman
30840435Speter
30996755Strhodes/* Remove registration of a filesystem type */
310141634Sphkstatic int
31141056Spetervfs_unregister(struct vfsconf *vfc)
31240435Speter{
313132710Sphk	struct vfsconf *vfsp;
314283735Skib	int error, maxtypenum;
31540435Speter
316269457Skib	vfsconf_lock();
317269457Skib	vfsp = vfs_byname_locked(vfc->vfc_name);
318269457Skib	if (vfsp == NULL) {
319269457Skib		vfsconf_unlock();
320269457Skib		return (EINVAL);
321269457Skib	}
322269457Skib	if (vfsp->vfc_refcount != 0) {
323269457Skib		vfsconf_unlock();
324269457Skib		return (EBUSY);
325269457Skib	}
32640435Speter	if (vfc->vfc_vfsops->vfs_uninit != NULL) {
32740435Speter		error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
328269457Skib		if (error != 0) {
329269457Skib			vfsconf_unlock();
33040435Speter			return (error);
331269457Skib		}
33240435Speter	}
333132710Sphk	TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
33440435Speter	maxtypenum = VFS_GENERIC;
335132710Sphk	TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
33640435Speter		if (maxtypenum < vfsp->vfc_typenum)
33740435Speter			maxtypenum = vfsp->vfc_typenum;
33840435Speter	maxvfsconf = maxtypenum + 1;
339269457Skib	vfsconf_unlock();
340269457Skib	return (0);
34140435Speter}
34241056Speter
34391690Seivind/*
34496755Strhodes * Standard kernel module handling code for filesystem modules.
34591690Seivind * Referenced from VFS_SET().
34691690Seivind */
34741056Speterint
34841170Sbdevfs_modevent(module_t mod, int type, void *data)
34941056Speter{
35041056Speter	struct vfsconf *vfc;
35141056Speter	int error = 0;
35241056Speter
35341056Speter	vfc = (struct vfsconf *)data;
35441056Speter
35541056Speter	switch (type) {
35641056Speter	case MOD_LOAD:
35741056Speter		if (vfc)
35841056Speter			error = vfs_register(vfc);
35941056Speter		break;
36041056Speter
36141056Speter	case MOD_UNLOAD:
36241056Speter		if (vfc)
36341056Speter			error = vfs_unregister(vfc);
36441056Speter		break;
365132199Sphk	default:
366132199Sphk		error = EOPNOTSUPP;
36741056Speter		break;
36841056Speter	}
36941056Speter	return (error);
37041056Speter}
371