vfs_init.c revision 159956
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 159956 2006-06-26 18:33:32Z jhb $"); 391541Srgrimes 401541Srgrimes#include <sys/param.h> 412112Swollman#include <sys/systm.h> 422946Swollman#include <sys/kernel.h> 43138350Sphk#include <sys/linker.h> 441541Srgrimes#include <sys/mount.h> 45138350Sphk#include <sys/proc.h> 46159590Sjhb#include <sys/syscallsubr.h> 4738869Sbde#include <sys/sysctl.h> 481541Srgrimes#include <sys/vnode.h> 491541Srgrimes#include <sys/malloc.h> 501541Srgrimes 51141634Sphkstatic int vfs_register(struct vfsconf *); 52141634Sphkstatic int vfs_unregister(struct vfsconf *); 5312577Sbde 5430354SphkMALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 5530354Sphk 5610358Sjulian/* 5769664Speter * The highest defined VFS number. 5829653Sdyson */ 5969664Speterint maxvfsconf = VFS_GENERIC + 1; 6091690Seivind 6191690Seivind/* 6291690Seivind * Single-linked list of configured VFSes. 6391690Seivind * New entries are added/deleted by vfs_register()/vfs_unregister() 6491690Seivind */ 65132710Sphkstruct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf); 6652780Smsmith 6752780Smsmith/* 68135279Sphk * A Zen vnode attribute structure. 69135279Sphk * 70135279Sphk * Initialized when the first filesystem registers by vfs_register(). 71135279Sphk */ 72135279Sphkstruct vattr va_null; 73135279Sphk 74135279Sphk/* 751541Srgrimes * vfs_init.c 761541Srgrimes * 771541Srgrimes * Allocate and fill in operations vectors. 781541Srgrimes * 791541Srgrimes * An undocumented feature of this approach to defining operations is that 801541Srgrimes * there can be multiple entries in vfs_opv_descs for the same operations 811541Srgrimes * vector. This allows third parties to extend the set of operations 821541Srgrimes * supported by another layer in a binary compatibile way. For example, 831541Srgrimes * assume that NFS needed to be modified to support Ficus. NFS has an entry 841541Srgrimes * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 851541Srgrimes * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 861541Srgrimes * listing those new operations Ficus adds to NFS, all without modifying the 871541Srgrimes * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 881541Srgrimes * that is a(whole)nother story.) This is a feature. 891541Srgrimes */ 9041056Speter 91138290Sphk/* 92138290Sphk * Routines having to do with the management of the vnode table. 93138290Sphk */ 9441056Speter 95132710Sphkstruct vfsconf * 96132710Sphkvfs_byname(const char *name) 97132710Sphk{ 98132710Sphk struct vfsconf *vfsp; 99132710Sphk 100138497Sphk if (!strcmp(name, "ffs")) 101138497Sphk name = "ufs"; 102132710Sphk TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 103132710Sphk if (!strcmp(name, vfsp->vfc_name)) 104132710Sphk return (vfsp); 105132710Sphk return (NULL); 106132710Sphk} 107132710Sphk 108138350Sphkstruct vfsconf * 109138350Sphkvfs_byname_kld(const char *fstype, struct thread *td, int *error) 110138350Sphk{ 111138350Sphk struct vfsconf *vfsp; 112159590Sjhb int fileid; 113138350Sphk 114138350Sphk vfsp = vfs_byname(fstype); 115138350Sphk if (vfsp != NULL) 116138350Sphk return (vfsp); 117138350Sphk 118159956Sjhb /* Try to load the respective module. */ 119159590Sjhb *error = kern_kldload(td, fstype, &fileid); 120138350Sphk if (*error) 121138350Sphk return (NULL); 122159590Sjhb 123138350Sphk /* Look up again to see if the VFS was loaded. */ 124138350Sphk vfsp = vfs_byname(fstype); 125138350Sphk if (vfsp == NULL) { 126159590Sjhb (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); 127138350Sphk *error = ENODEV; 128138350Sphk return (NULL); 129138350Sphk } 130138350Sphk return (vfsp); 131138350Sphk} 132138350Sphk 133138350Sphk 13496755Strhodes/* Register a new filesystem type in the global table */ 135141634Sphkstatic int 13641056Spetervfs_register(struct vfsconf *vfc) 13740435Speter{ 13844549Sdfr struct sysctl_oid *oidp; 139116271Sphk struct vfsops *vfsops; 140135279Sphk static int once; 141135279Sphk 142135279Sphk if (!once) { 143135279Sphk vattr_null(&va_null); 144135279Sphk once = 1; 145135279Sphk } 146116271Sphk 147132902Sphk if (vfc->vfc_version != VFS_VERSION) { 148132902Sphk printf("ERROR: filesystem %s, unsupported ABI version %x\n", 149132902Sphk vfc->vfc_name, vfc->vfc_version); 150132902Sphk return (EINVAL); 151132902Sphk } 152132710Sphk if (vfs_byname(vfc->vfc_name) != NULL) 153132710Sphk return EEXIST; 15440435Speter 15540435Speter vfc->vfc_typenum = maxvfsconf++; 156132710Sphk TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 15740435Speter 15840435Speter /* 15944549Sdfr * If this filesystem has a sysctl node under vfs 16044549Sdfr * (i.e. vfs.xxfs), then change the oid number of that node to 16144549Sdfr * match the filesystem's type number. This allows user code 16244549Sdfr * which uses the type number to read sysctl variables defined 16344549Sdfr * by the filesystem to continue working. Since the oids are 16444549Sdfr * in a sorted list, we need to make sure the order is 16544549Sdfr * preserved by re-registering the oid after modifying its 16644549Sdfr * number. 16744549Sdfr */ 16872012Sphk SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) 16944549Sdfr if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 17044549Sdfr sysctl_unregister_oid(oidp); 17144549Sdfr oidp->oid_number = vfc->vfc_typenum; 17244549Sdfr sysctl_register_oid(oidp); 17344549Sdfr } 17444549Sdfr 17544549Sdfr /* 176116271Sphk * Initialise unused ``struct vfsops'' fields, to use 177116271Sphk * the vfs_std*() functions. Note, we need the mount 178116271Sphk * and unmount operations, at the least. The check 179116271Sphk * for vfsops available is just a debugging aid. 180116271Sphk */ 181116271Sphk KASSERT(vfc->vfc_vfsops != NULL, 182116271Sphk ("Filesystem %s has no vfsops", vfc->vfc_name)); 183116271Sphk /* 184116271Sphk * Check the mount and unmount operations. 185116271Sphk */ 186116271Sphk vfsops = vfc->vfc_vfsops; 187138509Sphk KASSERT(vfsops->vfs_mount != NULL, 188138509Sphk ("Filesystem %s has no mount op", vfc->vfc_name)); 189116271Sphk KASSERT(vfsops->vfs_unmount != NULL, 190116271Sphk ("Filesystem %s has no unmount op", vfc->vfc_name)); 191116271Sphk 192116271Sphk if (vfsops->vfs_root == NULL) 193116271Sphk /* return file system's root vnode */ 194116271Sphk vfsops->vfs_root = vfs_stdroot; 195116271Sphk if (vfsops->vfs_quotactl == NULL) 196116271Sphk /* quota control */ 197116271Sphk vfsops->vfs_quotactl = vfs_stdquotactl; 198116271Sphk if (vfsops->vfs_statfs == NULL) 199116271Sphk /* return file system's status */ 200116271Sphk vfsops->vfs_statfs = vfs_stdstatfs; 201116271Sphk if (vfsops->vfs_sync == NULL) 202116271Sphk /* 203116271Sphk * flush unwritten data (nosync) 204116271Sphk * file systems can use vfs_stdsync 205116271Sphk * explicitly by setting it in the 206116271Sphk * vfsop vector. 207116271Sphk */ 208116271Sphk vfsops->vfs_sync = vfs_stdnosync; 209116271Sphk if (vfsops->vfs_vget == NULL) 210116271Sphk /* convert an inode number to a vnode */ 211116271Sphk vfsops->vfs_vget = vfs_stdvget; 212116271Sphk if (vfsops->vfs_fhtovp == NULL) 213116271Sphk /* turn an NFS file handle into a vnode */ 214116271Sphk vfsops->vfs_fhtovp = vfs_stdfhtovp; 215116271Sphk if (vfsops->vfs_checkexp == NULL) 216116271Sphk /* check if file system is exported */ 217116271Sphk vfsops->vfs_checkexp = vfs_stdcheckexp; 218116271Sphk if (vfsops->vfs_vptofh == NULL) 219116271Sphk /* turn a vnode into an NFS file handle */ 220116271Sphk vfsops->vfs_vptofh = vfs_stdvptofh; 221116271Sphk if (vfsops->vfs_init == NULL) 222116271Sphk /* file system specific initialisation */ 223116271Sphk vfsops->vfs_init = vfs_stdinit; 224116271Sphk if (vfsops->vfs_uninit == NULL) 225116271Sphk /* file system specific uninitialisation */ 226116271Sphk vfsops->vfs_uninit = vfs_stduninit; 227116271Sphk if (vfsops->vfs_extattrctl == NULL) 228116271Sphk /* extended attribute control */ 229116271Sphk vfsops->vfs_extattrctl = vfs_stdextattrctl; 230131733Salfred if (vfsops->vfs_sysctl == NULL) 231131733Salfred vfsops->vfs_sysctl = vfs_stdsysctl; 232116271Sphk 233116271Sphk /* 23440435Speter * Call init function for this VFS... 23540435Speter */ 23640435Speter (*(vfc->vfc_vfsops->vfs_init))(vfc); 23740435Speter 23840435Speter return 0; 2391541Srgrimes} 2402946Swollman 24140435Speter 24296755Strhodes/* Remove registration of a filesystem type */ 243141634Sphkstatic int 24441056Spetervfs_unregister(struct vfsconf *vfc) 24540435Speter{ 246132710Sphk struct vfsconf *vfsp; 24740435Speter int error, i, maxtypenum; 24840435Speter 24940435Speter i = vfc->vfc_typenum; 25040435Speter 251132710Sphk vfsp = vfs_byname(vfc->vfc_name); 25240435Speter if (vfsp == NULL) 25340435Speter return EINVAL; 25440435Speter if (vfsp->vfc_refcount) 25540435Speter return EBUSY; 25640435Speter if (vfc->vfc_vfsops->vfs_uninit != NULL) { 25740435Speter error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 25840435Speter if (error) 25940435Speter return (error); 26040435Speter } 261132710Sphk TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 26240435Speter maxtypenum = VFS_GENERIC; 263132710Sphk TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 26440435Speter if (maxtypenum < vfsp->vfc_typenum) 26540435Speter maxtypenum = vfsp->vfc_typenum; 26640435Speter maxvfsconf = maxtypenum + 1; 26740435Speter return 0; 26840435Speter} 26941056Speter 27091690Seivind/* 27196755Strhodes * Standard kernel module handling code for filesystem modules. 27291690Seivind * Referenced from VFS_SET(). 27391690Seivind */ 27441056Speterint 27541170Sbdevfs_modevent(module_t mod, int type, void *data) 27641056Speter{ 27741056Speter struct vfsconf *vfc; 27841056Speter int error = 0; 27941056Speter 28041056Speter vfc = (struct vfsconf *)data; 28141056Speter 28241056Speter switch (type) { 28341056Speter case MOD_LOAD: 28441056Speter if (vfc) 28541056Speter error = vfs_register(vfc); 28641056Speter break; 28741056Speter 28841056Speter case MOD_UNLOAD: 28941056Speter if (vfc) 29041056Speter error = vfs_unregister(vfc); 29141056Speter break; 292132199Sphk default: 293132199Sphk error = EOPNOTSUPP; 29441056Speter break; 29541056Speter } 29641056Speter return (error); 29741056Speter} 298