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> 47270095Skib#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); 68270095Skibstruct sx vfsconf_sx; 69270095SkibSX_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; 78225537SrmacklemTUNABLE_INT("vfs.typenumhash", &vfs_typenumhash); 79225537SrmacklemSYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0, 80225537Srmacklem "Set vfc_typenum using a hash calculation on vfc_name, so that it does not" 81225537Srmacklem "change when file systems are loaded in a different order."); 82225537Srmacklem 83225537Srmacklem/* 84135279Sphk * A Zen vnode attribute structure. 85135279Sphk * 86135279Sphk * Initialized when the first filesystem registers by vfs_register(). 87135279Sphk */ 88135279Sphkstruct vattr va_null; 89135279Sphk 90135279Sphk/* 911541Srgrimes * vfs_init.c 921541Srgrimes * 931541Srgrimes * Allocate and fill in operations vectors. 941541Srgrimes * 951541Srgrimes * An undocumented feature of this approach to defining operations is that 961541Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations 971541Srgrimes * vector. This allows third parties to extend the set of operations 981541Srgrimes * supported by another layer in a binary compatibile way. For example, 991541Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry 1001541Srgrimes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 1011541Srgrimes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 1021541Srgrimes * listing those new operations Ficus adds to NFS, all without modifying the 1031541Srgrimes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 1041541Srgrimes * that is a(whole)nother story.) This is a feature. 1051541Srgrimes */ 10641056Speter 107138290Sphk/* 108138290Sphk * Routines having to do with the management of the vnode table. 109138290Sphk */ 11041056Speter 111270095Skibstatic struct vfsconf * 112270095Skibvfs_byname_locked(const char *name) 113132710Sphk{ 114132710Sphk struct vfsconf *vfsp; 115132710Sphk 116270095Skib sx_assert(&vfsconf_sx, SA_LOCKED); 117138497Sphk if (!strcmp(name, "ffs")) 118138497Sphk name = "ufs"; 119270095Skib TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 120132710Sphk if (!strcmp(name, vfsp->vfc_name)) 121132710Sphk return (vfsp); 122270095Skib } 123132710Sphk return (NULL); 124132710Sphk} 125132710Sphk 126138350Sphkstruct vfsconf * 127270095Skibvfs_byname(const char *name) 128270095Skib{ 129270095Skib struct vfsconf *vfsp; 130270095Skib 131270095Skib vfsconf_slock(); 132270095Skib vfsp = vfs_byname_locked(name); 133270095Skib vfsconf_sunlock(); 134270095Skib return (vfsp); 135270095Skib} 136270095Skib 137270095Skibstruct vfsconf * 138138350Sphkvfs_byname_kld(const char *fstype, struct thread *td, int *error) 139138350Sphk{ 140138350Sphk struct vfsconf *vfsp; 141247071Sjamie int fileid, loaded; 142138350Sphk 143138350Sphk vfsp = vfs_byname(fstype); 144138350Sphk if (vfsp != NULL) 145138350Sphk return (vfsp); 146138350Sphk 147159956Sjhb /* Try to load the respective module. */ 148159590Sjhb *error = kern_kldload(td, fstype, &fileid); 149247071Sjamie loaded = (*error == 0); 150247071Sjamie if (*error == EEXIST) 151247071Sjamie *error = 0; 152138350Sphk if (*error) 153138350Sphk return (NULL); 154159590Sjhb 155138350Sphk /* Look up again to see if the VFS was loaded. */ 156138350Sphk vfsp = vfs_byname(fstype); 157138350Sphk if (vfsp == NULL) { 158247071Sjamie if (loaded) 159247071Sjamie (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); 160138350Sphk *error = ENODEV; 161138350Sphk return (NULL); 162138350Sphk } 163138350Sphk return (vfsp); 164138350Sphk} 165138350Sphk 166138350Sphk 16796755Strhodes/* Register a new filesystem type in the global table */ 168141634Sphkstatic int 16941056Spetervfs_register(struct vfsconf *vfc) 17040435Speter{ 17144549Sdfr struct sysctl_oid *oidp; 172116271Sphk struct vfsops *vfsops; 173135279Sphk static int once; 174225537Srmacklem struct vfsconf *tvfc; 175225537Srmacklem uint32_t hashval; 176225537Srmacklem int secondpass; 177135279Sphk 178135279Sphk if (!once) { 179135279Sphk vattr_null(&va_null); 180135279Sphk once = 1; 181135279Sphk } 182116271Sphk 183132902Sphk if (vfc->vfc_version != VFS_VERSION) { 184132902Sphk printf("ERROR: filesystem %s, unsupported ABI version %x\n", 185132902Sphk vfc->vfc_name, vfc->vfc_version); 186132902Sphk return (EINVAL); 187132902Sphk } 188270095Skib vfsconf_lock(); 189270095Skib if (vfs_byname_locked(vfc->vfc_name) != NULL) { 190270095Skib vfsconf_unlock(); 191241896Skib return (EEXIST); 192270095Skib } 19340435Speter 194225537Srmacklem if (vfs_typenumhash != 0) { 195225537Srmacklem /* 196225537Srmacklem * Calculate a hash on vfc_name to use for vfc_typenum. Unless 197225537Srmacklem * all of 1<->255 are assigned, it is limited to 8bits since 198225537Srmacklem * that is what ZFS uses from vfc_typenum and is also the 199225537Srmacklem * preferred range for vfs_getnewfsid(). 200225537Srmacklem */ 201225537Srmacklem hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT); 202225537Srmacklem hashval &= 0xff; 203225537Srmacklem secondpass = 0; 204225537Srmacklem do { 205225537Srmacklem /* Look for and fix any collision. */ 206225537Srmacklem TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) { 207225537Srmacklem if (hashval == tvfc->vfc_typenum) { 208225537Srmacklem if (hashval == 255 && secondpass == 0) { 209225537Srmacklem hashval = 1; 210225537Srmacklem secondpass = 1; 211225537Srmacklem } else 212225537Srmacklem hashval++; 213225537Srmacklem break; 214225537Srmacklem } 215225537Srmacklem } 216225537Srmacklem } while (tvfc != NULL); 217225537Srmacklem vfc->vfc_typenum = hashval; 218225537Srmacklem if (vfc->vfc_typenum >= maxvfsconf) 219225537Srmacklem maxvfsconf = vfc->vfc_typenum + 1; 220225537Srmacklem } else 221225537Srmacklem vfc->vfc_typenum = maxvfsconf++; 222132710Sphk TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 22340435Speter 22440435Speter /* 225116271Sphk * Initialise unused ``struct vfsops'' fields, to use 226116271Sphk * the vfs_std*() functions. Note, we need the mount 227116271Sphk * and unmount operations, at the least. The check 228116271Sphk * for vfsops available is just a debugging aid. 229116271Sphk */ 230116271Sphk KASSERT(vfc->vfc_vfsops != NULL, 231116271Sphk ("Filesystem %s has no vfsops", vfc->vfc_name)); 232116271Sphk /* 233116271Sphk * Check the mount and unmount operations. 234116271Sphk */ 235116271Sphk vfsops = vfc->vfc_vfsops; 236138509Sphk KASSERT(vfsops->vfs_mount != NULL, 237138509Sphk ("Filesystem %s has no mount op", vfc->vfc_name)); 238116271Sphk KASSERT(vfsops->vfs_unmount != NULL, 239116271Sphk ("Filesystem %s has no unmount op", vfc->vfc_name)); 240116271Sphk 241116271Sphk if (vfsops->vfs_root == NULL) 242116271Sphk /* return file system's root vnode */ 243116271Sphk vfsops->vfs_root = vfs_stdroot; 244116271Sphk if (vfsops->vfs_quotactl == NULL) 245116271Sphk /* quota control */ 246116271Sphk vfsops->vfs_quotactl = vfs_stdquotactl; 247116271Sphk if (vfsops->vfs_statfs == NULL) 248116271Sphk /* return file system's status */ 249116271Sphk vfsops->vfs_statfs = vfs_stdstatfs; 250116271Sphk if (vfsops->vfs_sync == NULL) 251116271Sphk /* 252116271Sphk * flush unwritten data (nosync) 253116271Sphk * file systems can use vfs_stdsync 254116271Sphk * explicitly by setting it in the 255116271Sphk * vfsop vector. 256116271Sphk */ 257116271Sphk vfsops->vfs_sync = vfs_stdnosync; 258116271Sphk if (vfsops->vfs_vget == NULL) 259116271Sphk /* convert an inode number to a vnode */ 260116271Sphk vfsops->vfs_vget = vfs_stdvget; 261116271Sphk if (vfsops->vfs_fhtovp == NULL) 262116271Sphk /* turn an NFS file handle into a vnode */ 263116271Sphk vfsops->vfs_fhtovp = vfs_stdfhtovp; 264116271Sphk if (vfsops->vfs_checkexp == NULL) 265116271Sphk /* check if file system is exported */ 266116271Sphk vfsops->vfs_checkexp = vfs_stdcheckexp; 267116271Sphk if (vfsops->vfs_init == NULL) 268116271Sphk /* file system specific initialisation */ 269116271Sphk vfsops->vfs_init = vfs_stdinit; 270116271Sphk if (vfsops->vfs_uninit == NULL) 271116271Sphk /* file system specific uninitialisation */ 272116271Sphk vfsops->vfs_uninit = vfs_stduninit; 273116271Sphk if (vfsops->vfs_extattrctl == NULL) 274116271Sphk /* extended attribute control */ 275116271Sphk vfsops->vfs_extattrctl = vfs_stdextattrctl; 276131733Salfred if (vfsops->vfs_sysctl == NULL) 277131733Salfred vfsops->vfs_sysctl = vfs_stdsysctl; 278116271Sphk 279116271Sphk /* 28040435Speter * Call init function for this VFS... 28140435Speter */ 28240435Speter (*(vfc->vfc_vfsops->vfs_init))(vfc); 283270095Skib vfsconf_unlock(); 28440435Speter 285270095Skib /* 286270095Skib * If this filesystem has a sysctl node under vfs 287270095Skib * (i.e. vfs.xxfs), then change the oid number of that node to 288270095Skib * match the filesystem's type number. This allows user code 289270095Skib * which uses the type number to read sysctl variables defined 290270095Skib * by the filesystem to continue working. Since the oids are 291270095Skib * in a sorted list, we need to make sure the order is 292270095Skib * preserved by re-registering the oid after modifying its 293270095Skib * number. 294270095Skib */ 295270095Skib sysctl_lock(); 296270095Skib SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) { 297270095Skib if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 298270095Skib sysctl_unregister_oid(oidp); 299270095Skib oidp->oid_number = vfc->vfc_typenum; 300270095Skib sysctl_register_oid(oidp); 301270095Skib break; 302270095Skib } 303270095Skib } 304270095Skib sysctl_unlock(); 305270095Skib 306270095Skib return (0); 3071541Srgrimes} 3082946Swollman 30940435Speter 31096755Strhodes/* Remove registration of a filesystem type */ 311141634Sphkstatic int 31241056Spetervfs_unregister(struct vfsconf *vfc) 31340435Speter{ 314132710Sphk struct vfsconf *vfsp; 315284021Skib int error, maxtypenum; 31640435Speter 317270095Skib vfsconf_lock(); 318270095Skib vfsp = vfs_byname_locked(vfc->vfc_name); 319270095Skib if (vfsp == NULL) { 320270095Skib vfsconf_unlock(); 321270095Skib return (EINVAL); 322270095Skib } 323270095Skib if (vfsp->vfc_refcount != 0) { 324270095Skib vfsconf_unlock(); 325270095Skib return (EBUSY); 326270095Skib } 32740435Speter if (vfc->vfc_vfsops->vfs_uninit != NULL) { 32840435Speter error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 329270095Skib if (error != 0) { 330270095Skib vfsconf_unlock(); 33140435Speter return (error); 332270095Skib } 33340435Speter } 334132710Sphk TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 33540435Speter maxtypenum = VFS_GENERIC; 336132710Sphk TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 33740435Speter if (maxtypenum < vfsp->vfc_typenum) 33840435Speter maxtypenum = vfsp->vfc_typenum; 33940435Speter maxvfsconf = maxtypenum + 1; 340270095Skib vfsconf_unlock(); 341270095Skib return (0); 34240435Speter} 34341056Speter 34491690Seivind/* 34596755Strhodes * Standard kernel module handling code for filesystem modules. 34691690Seivind * Referenced from VFS_SET(). 34791690Seivind */ 34841056Speterint 34941170Sbdevfs_modevent(module_t mod, int type, void *data) 35041056Speter{ 35141056Speter struct vfsconf *vfc; 35241056Speter int error = 0; 35341056Speter 35441056Speter vfc = (struct vfsconf *)data; 35541056Speter 35641056Speter switch (type) { 35741056Speter case MOD_LOAD: 35841056Speter if (vfc) 35941056Speter error = vfs_register(vfc); 36041056Speter break; 36141056Speter 36241056Speter case MOD_UNLOAD: 36341056Speter if (vfc) 36441056Speter error = vfs_unregister(vfc); 36541056Speter break; 366132199Sphk default: 367132199Sphk error = EOPNOTSUPP; 36841056Speter break; 36941056Speter } 37041056Speter return (error); 37141056Speter} 372