2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed
|
|
|
|
* to Berkeley by John Heidemann of the UCLA Ficus project.
|
|
|
|
*
|
|
|
|
* Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2016-09-15 13:16:20 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-24 10:09:53 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)vfs_init.c 8.3 (Berkeley) 1/4/94
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-08-18 22:36:09 +00:00
|
|
|
#include <sys/systm.h>
|
2011-09-13 21:01:26 +00:00
|
|
|
#include <sys/fnv_hash.h>
|
1994-09-21 03:47:43 +00:00
|
|
|
#include <sys/kernel.h>
|
2004-12-03 16:11:01 +00:00
|
|
|
#include <sys/linker.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/mount.h>
|
2004-12-03 16:11:01 +00:00
|
|
|
#include <sys/proc.h>
|
2014-08-03 03:27:54 +00:00
|
|
|
#include <sys/sx.h>
|
2006-06-13 21:36:23 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
1998-09-05 17:13:28 +00:00
|
|
|
#include <sys/sysctl.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
|
2005-02-10 12:25:38 +00:00
|
|
|
static int vfs_register(struct vfsconf *);
|
|
|
|
static int vfs_unregister(struct vfsconf *);
|
1995-08-28 09:19:25 +00:00
|
|
|
|
1997-10-12 20:26:33 +00:00
|
|
|
MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
|
|
|
|
|
1997-09-21 04:24:27 +00:00
|
|
|
/*
|
2000-12-06 07:09:08 +00:00
|
|
|
* The highest defined VFS number.
|
1997-09-21 04:24:27 +00:00
|
|
|
*/
|
2000-12-06 07:09:08 +00:00
|
|
|
int maxvfsconf = VFS_GENERIC + 1;
|
2002-03-05 15:38:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Single-linked list of configured VFSes.
|
|
|
|
* New entries are added/deleted by vfs_register()/vfs_unregister()
|
|
|
|
*/
|
2004-07-27 22:32:01 +00:00
|
|
|
struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf);
|
2014-08-03 03:27:54 +00:00
|
|
|
struct sx vfsconf_sx;
|
|
|
|
SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf");
|
1999-11-01 23:54:07 +00:00
|
|
|
|
2011-09-13 21:01:26 +00:00
|
|
|
/*
|
|
|
|
* Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash
|
|
|
|
* calculation on vfc_name, so that it doesn't change when file systems are
|
|
|
|
* loaded in a different order. This will avoid the NFS server file handles from
|
|
|
|
* changing for file systems that use vfc_typenum in their fsid.
|
|
|
|
*/
|
|
|
|
static int vfs_typenumhash = 1;
|
|
|
|
SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0,
|
|
|
|
"Set vfc_typenum using a hash calculation on vfc_name, so that it does not"
|
|
|
|
"change when file systems are loaded in a different order.");
|
|
|
|
|
2004-09-15 21:42:03 +00:00
|
|
|
/*
|
|
|
|
* A Zen vnode attribute structure.
|
|
|
|
*
|
|
|
|
* Initialized when the first filesystem registers by vfs_register().
|
|
|
|
*/
|
|
|
|
struct vattr va_null;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* vfs_init.c
|
|
|
|
*
|
|
|
|
* Allocate and fill in operations vectors.
|
|
|
|
*
|
|
|
|
* An undocumented feature of this approach to defining operations is that
|
|
|
|
* there can be multiple entries in vfs_opv_descs for the same operations
|
|
|
|
* vector. This allows third parties to extend the set of operations
|
|
|
|
* supported by another layer in a binary compatibile way. For example,
|
|
|
|
* assume that NFS needed to be modified to support Ficus. NFS has an entry
|
|
|
|
* (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by
|
|
|
|
* default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions)
|
|
|
|
* listing those new operations Ficus adds to NFS, all without modifying the
|
|
|
|
* NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but
|
|
|
|
* that is a(whole)nother story.) This is a feature.
|
|
|
|
*/
|
1998-11-10 09:04:09 +00:00
|
|
|
|
2004-12-01 23:16:38 +00:00
|
|
|
/*
|
|
|
|
* Routines having to do with the management of the vnode table.
|
|
|
|
*/
|
1998-11-10 09:04:09 +00:00
|
|
|
|
2014-08-03 03:27:54 +00:00
|
|
|
static struct vfsconf *
|
|
|
|
vfs_byname_locked(const char *name)
|
2004-07-27 22:32:01 +00:00
|
|
|
{
|
|
|
|
struct vfsconf *vfsp;
|
|
|
|
|
2014-08-03 03:27:54 +00:00
|
|
|
sx_assert(&vfsconf_sx, SA_LOCKED);
|
2004-12-06 22:22:57 +00:00
|
|
|
if (!strcmp(name, "ffs"))
|
|
|
|
name = "ufs";
|
2014-08-03 03:27:54 +00:00
|
|
|
TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
|
2004-07-27 22:32:01 +00:00
|
|
|
if (!strcmp(name, vfsp->vfc_name))
|
|
|
|
return (vfsp);
|
2014-08-03 03:27:54 +00:00
|
|
|
}
|
2004-07-27 22:32:01 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2014-08-03 03:27:54 +00:00
|
|
|
struct vfsconf *
|
|
|
|
vfs_byname(const char *name)
|
|
|
|
{
|
|
|
|
struct vfsconf *vfsp;
|
|
|
|
|
|
|
|
vfsconf_slock();
|
|
|
|
vfsp = vfs_byname_locked(name);
|
|
|
|
vfsconf_sunlock();
|
|
|
|
return (vfsp);
|
|
|
|
}
|
|
|
|
|
2004-12-03 16:11:01 +00:00
|
|
|
struct vfsconf *
|
|
|
|
vfs_byname_kld(const char *fstype, struct thread *td, int *error)
|
|
|
|
{
|
|
|
|
struct vfsconf *vfsp;
|
2013-02-21 02:41:37 +00:00
|
|
|
int fileid, loaded;
|
2004-12-03 16:11:01 +00:00
|
|
|
|
|
|
|
vfsp = vfs_byname(fstype);
|
|
|
|
if (vfsp != NULL)
|
|
|
|
return (vfsp);
|
|
|
|
|
2006-06-26 18:33:32 +00:00
|
|
|
/* Try to load the respective module. */
|
2006-06-13 21:36:23 +00:00
|
|
|
*error = kern_kldload(td, fstype, &fileid);
|
2013-02-21 02:41:37 +00:00
|
|
|
loaded = (*error == 0);
|
|
|
|
if (*error == EEXIST)
|
|
|
|
*error = 0;
|
2004-12-03 16:11:01 +00:00
|
|
|
if (*error)
|
|
|
|
return (NULL);
|
2006-06-13 21:36:23 +00:00
|
|
|
|
2004-12-03 16:11:01 +00:00
|
|
|
/* Look up again to see if the VFS was loaded. */
|
|
|
|
vfsp = vfs_byname(fstype);
|
|
|
|
if (vfsp == NULL) {
|
2013-02-21 02:41:37 +00:00
|
|
|
if (loaded)
|
|
|
|
(void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE);
|
2004-12-03 16:11:01 +00:00
|
|
|
*error = ENODEV;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (vfsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-16 21:28:32 +00:00
|
|
|
/* Register a new filesystem type in the global table */
|
2005-02-10 12:25:38 +00:00
|
|
|
static int
|
1998-11-10 09:04:09 +00:00
|
|
|
vfs_register(struct vfsconf *vfc)
|
1998-10-16 03:55:01 +00:00
|
|
|
{
|
1999-03-07 16:06:41 +00:00
|
|
|
struct sysctl_oid *oidp;
|
2003-06-12 20:48:38 +00:00
|
|
|
struct vfsops *vfsops;
|
2004-09-15 21:42:03 +00:00
|
|
|
static int once;
|
2011-09-13 21:01:26 +00:00
|
|
|
struct vfsconf *tvfc;
|
|
|
|
uint32_t hashval;
|
|
|
|
int secondpass;
|
2004-09-15 21:42:03 +00:00
|
|
|
|
|
|
|
if (!once) {
|
|
|
|
vattr_null(&va_null);
|
|
|
|
once = 1;
|
|
|
|
}
|
2003-06-12 20:48:38 +00:00
|
|
|
|
2004-07-30 22:08:52 +00:00
|
|
|
if (vfc->vfc_version != VFS_VERSION) {
|
|
|
|
printf("ERROR: filesystem %s, unsupported ABI version %x\n",
|
|
|
|
vfc->vfc_name, vfc->vfc_version);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2014-08-03 03:27:54 +00:00
|
|
|
vfsconf_lock();
|
|
|
|
if (vfs_byname_locked(vfc->vfc_name) != NULL) {
|
|
|
|
vfsconf_unlock();
|
2012-10-22 17:50:54 +00:00
|
|
|
return (EEXIST);
|
2014-08-03 03:27:54 +00:00
|
|
|
}
|
1998-10-16 03:55:01 +00:00
|
|
|
|
2011-09-13 21:01:26 +00:00
|
|
|
if (vfs_typenumhash != 0) {
|
|
|
|
/*
|
|
|
|
* Calculate a hash on vfc_name to use for vfc_typenum. Unless
|
|
|
|
* all of 1<->255 are assigned, it is limited to 8bits since
|
|
|
|
* that is what ZFS uses from vfc_typenum and is also the
|
|
|
|
* preferred range for vfs_getnewfsid().
|
|
|
|
*/
|
|
|
|
hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT);
|
|
|
|
hashval &= 0xff;
|
|
|
|
secondpass = 0;
|
|
|
|
do {
|
|
|
|
/* Look for and fix any collision. */
|
|
|
|
TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) {
|
|
|
|
if (hashval == tvfc->vfc_typenum) {
|
|
|
|
if (hashval == 255 && secondpass == 0) {
|
|
|
|
hashval = 1;
|
|
|
|
secondpass = 1;
|
|
|
|
} else
|
|
|
|
hashval++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (tvfc != NULL);
|
|
|
|
vfc->vfc_typenum = hashval;
|
|
|
|
if (vfc->vfc_typenum >= maxvfsconf)
|
|
|
|
maxvfsconf = vfc->vfc_typenum + 1;
|
|
|
|
} else
|
|
|
|
vfc->vfc_typenum = maxvfsconf++;
|
2004-07-27 22:32:01 +00:00
|
|
|
TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list);
|
1998-10-16 03:55:01 +00:00
|
|
|
|
2003-06-12 20:48:38 +00:00
|
|
|
/*
|
|
|
|
* Initialise unused ``struct vfsops'' fields, to use
|
|
|
|
* the vfs_std*() functions. Note, we need the mount
|
|
|
|
* and unmount operations, at the least. The check
|
|
|
|
* for vfsops available is just a debugging aid.
|
|
|
|
*/
|
|
|
|
KASSERT(vfc->vfc_vfsops != NULL,
|
|
|
|
("Filesystem %s has no vfsops", vfc->vfc_name));
|
|
|
|
/*
|
|
|
|
* Check the mount and unmount operations.
|
|
|
|
*/
|
|
|
|
vfsops = vfc->vfc_vfsops;
|
2004-12-07 08:15:41 +00:00
|
|
|
KASSERT(vfsops->vfs_mount != NULL,
|
|
|
|
("Filesystem %s has no mount op", vfc->vfc_name));
|
2003-06-12 20:48:38 +00:00
|
|
|
KASSERT(vfsops->vfs_unmount != NULL,
|
|
|
|
("Filesystem %s has no unmount op", vfc->vfc_name));
|
|
|
|
|
|
|
|
if (vfsops->vfs_root == NULL)
|
|
|
|
/* return file system's root vnode */
|
|
|
|
vfsops->vfs_root = vfs_stdroot;
|
|
|
|
if (vfsops->vfs_quotactl == NULL)
|
|
|
|
/* quota control */
|
|
|
|
vfsops->vfs_quotactl = vfs_stdquotactl;
|
|
|
|
if (vfsops->vfs_statfs == NULL)
|
|
|
|
/* return file system's status */
|
|
|
|
vfsops->vfs_statfs = vfs_stdstatfs;
|
|
|
|
if (vfsops->vfs_sync == NULL)
|
|
|
|
/*
|
|
|
|
* flush unwritten data (nosync)
|
|
|
|
* file systems can use vfs_stdsync
|
|
|
|
* explicitly by setting it in the
|
|
|
|
* vfsop vector.
|
|
|
|
*/
|
|
|
|
vfsops->vfs_sync = vfs_stdnosync;
|
|
|
|
if (vfsops->vfs_vget == NULL)
|
|
|
|
/* convert an inode number to a vnode */
|
|
|
|
vfsops->vfs_vget = vfs_stdvget;
|
|
|
|
if (vfsops->vfs_fhtovp == NULL)
|
|
|
|
/* turn an NFS file handle into a vnode */
|
|
|
|
vfsops->vfs_fhtovp = vfs_stdfhtovp;
|
|
|
|
if (vfsops->vfs_checkexp == NULL)
|
|
|
|
/* check if file system is exported */
|
|
|
|
vfsops->vfs_checkexp = vfs_stdcheckexp;
|
|
|
|
if (vfsops->vfs_init == NULL)
|
|
|
|
/* file system specific initialisation */
|
|
|
|
vfsops->vfs_init = vfs_stdinit;
|
|
|
|
if (vfsops->vfs_uninit == NULL)
|
|
|
|
/* file system specific uninitialisation */
|
|
|
|
vfsops->vfs_uninit = vfs_stduninit;
|
|
|
|
if (vfsops->vfs_extattrctl == NULL)
|
|
|
|
/* extended attribute control */
|
|
|
|
vfsops->vfs_extattrctl = vfs_stdextattrctl;
|
2004-07-07 06:58:29 +00:00
|
|
|
if (vfsops->vfs_sysctl == NULL)
|
|
|
|
vfsops->vfs_sysctl = vfs_stdsysctl;
|
2003-06-12 20:48:38 +00:00
|
|
|
|
1998-10-16 03:55:01 +00:00
|
|
|
/*
|
|
|
|
* Call init function for this VFS...
|
|
|
|
*/
|
|
|
|
(*(vfc->vfc_vfsops->vfs_init))(vfc);
|
2014-08-03 03:27:54 +00:00
|
|
|
vfsconf_unlock();
|
1998-10-16 03:55:01 +00:00
|
|
|
|
2014-08-03 03:27:54 +00:00
|
|
|
/*
|
|
|
|
* If this filesystem has a sysctl node under vfs
|
|
|
|
* (i.e. vfs.xxfs), then change the oid number of that node to
|
|
|
|
* match the filesystem's type number. This allows user code
|
|
|
|
* which uses the type number to read sysctl variables defined
|
|
|
|
* by the filesystem to continue working. Since the oids are
|
|
|
|
* in a sorted list, we need to make sure the order is
|
|
|
|
* preserved by re-registering the oid after modifying its
|
|
|
|
* number.
|
|
|
|
*/
|
2015-09-15 23:06:56 +00:00
|
|
|
sysctl_wlock();
|
2014-08-03 03:27:54 +00:00
|
|
|
SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) {
|
|
|
|
if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) {
|
|
|
|
sysctl_unregister_oid(oidp);
|
|
|
|
oidp->oid_number = vfc->vfc_typenum;
|
|
|
|
sysctl_register_oid(oidp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 23:06:56 +00:00
|
|
|
sysctl_wunlock();
|
2014-08-03 03:27:54 +00:00
|
|
|
|
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1994-09-21 03:47:43 +00:00
|
|
|
|
1998-10-16 03:55:01 +00:00
|
|
|
|
2002-05-16 21:28:32 +00:00
|
|
|
/* Remove registration of a filesystem type */
|
2005-02-10 12:25:38 +00:00
|
|
|
static int
|
1998-11-10 09:04:09 +00:00
|
|
|
vfs_unregister(struct vfsconf *vfc)
|
1998-10-16 03:55:01 +00:00
|
|
|
{
|
2004-07-27 22:32:01 +00:00
|
|
|
struct vfsconf *vfsp;
|
2015-05-29 13:24:17 +00:00
|
|
|
int error, maxtypenum;
|
1998-10-16 03:55:01 +00:00
|
|
|
|
2014-08-03 03:27:54 +00:00
|
|
|
vfsconf_lock();
|
|
|
|
vfsp = vfs_byname_locked(vfc->vfc_name);
|
|
|
|
if (vfsp == NULL) {
|
|
|
|
vfsconf_unlock();
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
if (vfsp->vfc_refcount != 0) {
|
|
|
|
vfsconf_unlock();
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
1998-10-16 03:55:01 +00:00
|
|
|
if (vfc->vfc_vfsops->vfs_uninit != NULL) {
|
|
|
|
error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp);
|
2014-08-03 03:27:54 +00:00
|
|
|
if (error != 0) {
|
|
|
|
vfsconf_unlock();
|
1998-10-16 03:55:01 +00:00
|
|
|
return (error);
|
2014-08-03 03:27:54 +00:00
|
|
|
}
|
1998-10-16 03:55:01 +00:00
|
|
|
}
|
2004-07-27 22:32:01 +00:00
|
|
|
TAILQ_REMOVE(&vfsconf, vfsp, vfc_list);
|
1998-10-16 03:55:01 +00:00
|
|
|
maxtypenum = VFS_GENERIC;
|
2004-07-27 22:32:01 +00:00
|
|
|
TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
|
1998-10-16 03:55:01 +00:00
|
|
|
if (maxtypenum < vfsp->vfc_typenum)
|
|
|
|
maxtypenum = vfsp->vfc_typenum;
|
|
|
|
maxvfsconf = maxtypenum + 1;
|
2014-08-03 03:27:54 +00:00
|
|
|
vfsconf_unlock();
|
|
|
|
return (0);
|
1998-10-16 03:55:01 +00:00
|
|
|
}
|
1998-11-10 09:04:09 +00:00
|
|
|
|
2002-03-05 15:38:49 +00:00
|
|
|
/*
|
2002-05-16 21:28:32 +00:00
|
|
|
* Standard kernel module handling code for filesystem modules.
|
2002-03-05 15:38:49 +00:00
|
|
|
* Referenced from VFS_SET().
|
|
|
|
*/
|
1998-11-10 09:04:09 +00:00
|
|
|
int
|
1998-11-15 15:18:30 +00:00
|
|
|
vfs_modevent(module_t mod, int type, void *data)
|
1998-11-10 09:04:09 +00:00
|
|
|
{
|
|
|
|
struct vfsconf *vfc;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
vfc = (struct vfsconf *)data;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
if (vfc)
|
|
|
|
error = vfs_register(vfc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
if (vfc)
|
|
|
|
error = vfs_unregister(vfc);
|
|
|
|
break;
|
2004-07-15 08:26:07 +00:00
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
1998-11-10 09:04:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|