vfs_init.c revision 241896
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: head/sys/kern/vfs_init.c 241896 2012-10-22 17:50:54Z kib $"); 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> 47159590Sjhb#include <sys/syscallsubr.h> 4838869Sbde#include <sys/sysctl.h> 491541Srgrimes#include <sys/vnode.h> 501541Srgrimes#include <sys/malloc.h> 511541Srgrimes 52141634Sphkstatic int vfs_register(struct vfsconf *); 53141634Sphkstatic int vfs_unregister(struct vfsconf *); 5412577Sbde 5530354SphkMALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 5630354Sphk 5710358Sjulian/* 5869664Speter * The highest defined VFS number. 5929653Sdyson */ 6069664Speterint maxvfsconf = VFS_GENERIC + 1; 6191690Seivind 6291690Seivind/* 6391690Seivind * Single-linked list of configured VFSes. 6491690Seivind * New entries are added/deleted by vfs_register()/vfs_unregister() 6591690Seivind */ 66132710Sphkstruct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf); 6752780Smsmith 6852780Smsmith/* 69225537Srmacklem * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash 70225537Srmacklem * calculation on vfc_name, so that it doesn't change when file systems are 71225537Srmacklem * loaded in a different order. This will avoid the NFS server file handles from 72225537Srmacklem * changing for file systems that use vfc_typenum in their fsid. 73225537Srmacklem */ 74225537Srmacklemstatic int vfs_typenumhash = 1; 75225537SrmacklemTUNABLE_INT("vfs.typenumhash", &vfs_typenumhash); 76225537SrmacklemSYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0, 77225537Srmacklem "Set vfc_typenum using a hash calculation on vfc_name, so that it does not" 78225537Srmacklem "change when file systems are loaded in a different order."); 79225537Srmacklem 80225537Srmacklem/* 81135279Sphk * A Zen vnode attribute structure. 82135279Sphk * 83135279Sphk * Initialized when the first filesystem registers by vfs_register(). 84135279Sphk */ 85135279Sphkstruct vattr va_null; 86135279Sphk 87135279Sphk/* 881541Srgrimes * vfs_init.c 891541Srgrimes * 901541Srgrimes * Allocate and fill in operations vectors. 911541Srgrimes * 921541Srgrimes * An undocumented feature of this approach to defining operations is that 931541Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations 941541Srgrimes * vector. This allows third parties to extend the set of operations 951541Srgrimes * supported by another layer in a binary compatibile way. For example, 961541Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry 971541Srgrimes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 981541Srgrimes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 991541Srgrimes * listing those new operations Ficus adds to NFS, all without modifying the 1001541Srgrimes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 1011541Srgrimes * that is a(whole)nother story.) This is a feature. 1021541Srgrimes */ 10341056Speter 104138290Sphk/* 105138290Sphk * Routines having to do with the management of the vnode table. 106138290Sphk */ 10741056Speter 108132710Sphkstruct vfsconf * 109132710Sphkvfs_byname(const char *name) 110132710Sphk{ 111132710Sphk struct vfsconf *vfsp; 112132710Sphk 113138497Sphk if (!strcmp(name, "ffs")) 114138497Sphk name = "ufs"; 115132710Sphk TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 116132710Sphk if (!strcmp(name, vfsp->vfc_name)) 117132710Sphk return (vfsp); 118132710Sphk return (NULL); 119132710Sphk} 120132710Sphk 121138350Sphkstruct vfsconf * 122138350Sphkvfs_byname_kld(const char *fstype, struct thread *td, int *error) 123138350Sphk{ 124138350Sphk struct vfsconf *vfsp; 125159590Sjhb int fileid; 126138350Sphk 127138350Sphk vfsp = vfs_byname(fstype); 128138350Sphk if (vfsp != NULL) 129138350Sphk return (vfsp); 130138350Sphk 131159956Sjhb /* Try to load the respective module. */ 132159590Sjhb *error = kern_kldload(td, fstype, &fileid); 133138350Sphk if (*error) 134138350Sphk return (NULL); 135159590Sjhb 136138350Sphk /* Look up again to see if the VFS was loaded. */ 137138350Sphk vfsp = vfs_byname(fstype); 138138350Sphk if (vfsp == NULL) { 139159590Sjhb (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); 140138350Sphk *error = ENODEV; 141138350Sphk return (NULL); 142138350Sphk } 143138350Sphk return (vfsp); 144138350Sphk} 145138350Sphk 146138350Sphk 14796755Strhodes/* Register a new filesystem type in the global table */ 148141634Sphkstatic int 14941056Spetervfs_register(struct vfsconf *vfc) 15040435Speter{ 15144549Sdfr struct sysctl_oid *oidp; 152116271Sphk struct vfsops *vfsops; 153135279Sphk static int once; 154225537Srmacklem struct vfsconf *tvfc; 155225537Srmacklem uint32_t hashval; 156225537Srmacklem int secondpass; 157135279Sphk 158135279Sphk if (!once) { 159135279Sphk vattr_null(&va_null); 160135279Sphk once = 1; 161135279Sphk } 162116271Sphk 163132902Sphk if (vfc->vfc_version != VFS_VERSION) { 164132902Sphk printf("ERROR: filesystem %s, unsupported ABI version %x\n", 165132902Sphk vfc->vfc_name, vfc->vfc_version); 166132902Sphk return (EINVAL); 167132902Sphk } 168132710Sphk if (vfs_byname(vfc->vfc_name) != NULL) 169241896Skib return (EEXIST); 17040435Speter 171225537Srmacklem if (vfs_typenumhash != 0) { 172225537Srmacklem /* 173225537Srmacklem * Calculate a hash on vfc_name to use for vfc_typenum. Unless 174225537Srmacklem * all of 1<->255 are assigned, it is limited to 8bits since 175225537Srmacklem * that is what ZFS uses from vfc_typenum and is also the 176225537Srmacklem * preferred range for vfs_getnewfsid(). 177225537Srmacklem */ 178225537Srmacklem hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT); 179225537Srmacklem hashval &= 0xff; 180225537Srmacklem secondpass = 0; 181225537Srmacklem do { 182225537Srmacklem /* Look for and fix any collision. */ 183225537Srmacklem TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) { 184225537Srmacklem if (hashval == tvfc->vfc_typenum) { 185225537Srmacklem if (hashval == 255 && secondpass == 0) { 186225537Srmacklem hashval = 1; 187225537Srmacklem secondpass = 1; 188225537Srmacklem } else 189225537Srmacklem hashval++; 190225537Srmacklem break; 191225537Srmacklem } 192225537Srmacklem } 193225537Srmacklem } while (tvfc != NULL); 194225537Srmacklem vfc->vfc_typenum = hashval; 195225537Srmacklem if (vfc->vfc_typenum >= maxvfsconf) 196225537Srmacklem maxvfsconf = vfc->vfc_typenum + 1; 197225537Srmacklem } else 198225537Srmacklem vfc->vfc_typenum = maxvfsconf++; 199132710Sphk TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 20040435Speter 20140435Speter /* 20244549Sdfr * If this filesystem has a sysctl node under vfs 20344549Sdfr * (i.e. vfs.xxfs), then change the oid number of that node to 20444549Sdfr * match the filesystem's type number. This allows user code 20544549Sdfr * which uses the type number to read sysctl variables defined 20644549Sdfr * by the filesystem to continue working. Since the oids are 20744549Sdfr * in a sorted list, we need to make sure the order is 20844549Sdfr * preserved by re-registering the oid after modifying its 20944549Sdfr * number. 21044549Sdfr */ 211188232Sjhb sysctl_lock(); 21272012Sphk SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) 21344549Sdfr if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 21444549Sdfr sysctl_unregister_oid(oidp); 21544549Sdfr oidp->oid_number = vfc->vfc_typenum; 21644549Sdfr sysctl_register_oid(oidp); 217188232Sjhb break; 21844549Sdfr } 219188232Sjhb sysctl_unlock(); 22044549Sdfr 22144549Sdfr /* 222116271Sphk * Initialise unused ``struct vfsops'' fields, to use 223116271Sphk * the vfs_std*() functions. Note, we need the mount 224116271Sphk * and unmount operations, at the least. The check 225116271Sphk * for vfsops available is just a debugging aid. 226116271Sphk */ 227116271Sphk KASSERT(vfc->vfc_vfsops != NULL, 228116271Sphk ("Filesystem %s has no vfsops", vfc->vfc_name)); 229116271Sphk /* 230116271Sphk * Check the mount and unmount operations. 231116271Sphk */ 232116271Sphk vfsops = vfc->vfc_vfsops; 233138509Sphk KASSERT(vfsops->vfs_mount != NULL, 234138509Sphk ("Filesystem %s has no mount op", vfc->vfc_name)); 235116271Sphk KASSERT(vfsops->vfs_unmount != NULL, 236116271Sphk ("Filesystem %s has no unmount op", vfc->vfc_name)); 237116271Sphk 238116271Sphk if (vfsops->vfs_root == NULL) 239116271Sphk /* return file system's root vnode */ 240116271Sphk vfsops->vfs_root = vfs_stdroot; 241116271Sphk if (vfsops->vfs_quotactl == NULL) 242116271Sphk /* quota control */ 243116271Sphk vfsops->vfs_quotactl = vfs_stdquotactl; 244116271Sphk if (vfsops->vfs_statfs == NULL) 245116271Sphk /* return file system's status */ 246116271Sphk vfsops->vfs_statfs = vfs_stdstatfs; 247116271Sphk if (vfsops->vfs_sync == NULL) 248116271Sphk /* 249116271Sphk * flush unwritten data (nosync) 250116271Sphk * file systems can use vfs_stdsync 251116271Sphk * explicitly by setting it in the 252116271Sphk * vfsop vector. 253116271Sphk */ 254116271Sphk vfsops->vfs_sync = vfs_stdnosync; 255116271Sphk if (vfsops->vfs_vget == NULL) 256116271Sphk /* convert an inode number to a vnode */ 257116271Sphk vfsops->vfs_vget = vfs_stdvget; 258116271Sphk if (vfsops->vfs_fhtovp == NULL) 259116271Sphk /* turn an NFS file handle into a vnode */ 260116271Sphk vfsops->vfs_fhtovp = vfs_stdfhtovp; 261116271Sphk if (vfsops->vfs_checkexp == NULL) 262116271Sphk /* check if file system is exported */ 263116271Sphk vfsops->vfs_checkexp = vfs_stdcheckexp; 264116271Sphk if (vfsops->vfs_init == NULL) 265116271Sphk /* file system specific initialisation */ 266116271Sphk vfsops->vfs_init = vfs_stdinit; 267116271Sphk if (vfsops->vfs_uninit == NULL) 268116271Sphk /* file system specific uninitialisation */ 269116271Sphk vfsops->vfs_uninit = vfs_stduninit; 270116271Sphk if (vfsops->vfs_extattrctl == NULL) 271116271Sphk /* extended attribute control */ 272116271Sphk vfsops->vfs_extattrctl = vfs_stdextattrctl; 273131733Salfred if (vfsops->vfs_sysctl == NULL) 274131733Salfred vfsops->vfs_sysctl = vfs_stdsysctl; 275116271Sphk 276116271Sphk /* 27740435Speter * Call init function for this VFS... 27840435Speter */ 27940435Speter (*(vfc->vfc_vfsops->vfs_init))(vfc); 28040435Speter 28140435Speter return 0; 2821541Srgrimes} 2832946Swollman 28440435Speter 28596755Strhodes/* Remove registration of a filesystem type */ 286141634Sphkstatic int 28741056Spetervfs_unregister(struct vfsconf *vfc) 28840435Speter{ 289132710Sphk struct vfsconf *vfsp; 29040435Speter int error, i, maxtypenum; 29140435Speter 29240435Speter i = vfc->vfc_typenum; 29340435Speter 294132710Sphk vfsp = vfs_byname(vfc->vfc_name); 29540435Speter if (vfsp == NULL) 29640435Speter return EINVAL; 29740435Speter if (vfsp->vfc_refcount) 29840435Speter return EBUSY; 29940435Speter if (vfc->vfc_vfsops->vfs_uninit != NULL) { 30040435Speter error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 30140435Speter if (error) 30240435Speter return (error); 30340435Speter } 304132710Sphk TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 30540435Speter maxtypenum = VFS_GENERIC; 306132710Sphk TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 30740435Speter if (maxtypenum < vfsp->vfc_typenum) 30840435Speter maxtypenum = vfsp->vfc_typenum; 30940435Speter maxvfsconf = maxtypenum + 1; 31040435Speter return 0; 31140435Speter} 31241056Speter 31391690Seivind/* 31496755Strhodes * Standard kernel module handling code for filesystem modules. 31591690Seivind * Referenced from VFS_SET(). 31691690Seivind */ 31741056Speterint 31841170Sbdevfs_modevent(module_t mod, int type, void *data) 31941056Speter{ 32041056Speter struct vfsconf *vfc; 32141056Speter int error = 0; 32241056Speter 32341056Speter vfc = (struct vfsconf *)data; 32441056Speter 32541056Speter switch (type) { 32641056Speter case MOD_LOAD: 32741056Speter if (vfc) 32841056Speter error = vfs_register(vfc); 32941056Speter break; 33041056Speter 33141056Speter case MOD_UNLOAD: 33241056Speter if (vfc) 33341056Speter error = vfs_unregister(vfc); 33441056Speter break; 335132199Sphk default: 336132199Sphk error = EOPNOTSUPP; 33741056Speter break; 33841056Speter } 33941056Speter return (error); 34041056Speter} 341