vfs_init.c revision 159590
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 159590 2006-06-13 21:36:23Z 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 118138350Sphk /* Only load modules for root (very important!). */ 119138350Sphk *error = suser(td); 120138350Sphk if (*error) 121138350Sphk return (NULL); 122138350Sphk *error = securelevel_gt(td->td_ucred, 0); 123138350Sphk if (*error) 124138350Sphk return (NULL); 125159590Sjhb *error = kern_kldload(td, fstype, &fileid); 126138350Sphk if (*error) 127138350Sphk return (NULL); 128159590Sjhb 129138350Sphk /* Look up again to see if the VFS was loaded. */ 130138350Sphk vfsp = vfs_byname(fstype); 131138350Sphk if (vfsp == NULL) { 132159590Sjhb (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); 133138350Sphk *error = ENODEV; 134138350Sphk return (NULL); 135138350Sphk } 136138350Sphk return (vfsp); 137138350Sphk} 138138350Sphk 139138350Sphk 14096755Strhodes/* Register a new filesystem type in the global table */ 141141634Sphkstatic int 14241056Spetervfs_register(struct vfsconf *vfc) 14340435Speter{ 14444549Sdfr struct sysctl_oid *oidp; 145116271Sphk struct vfsops *vfsops; 146135279Sphk static int once; 147135279Sphk 148135279Sphk if (!once) { 149135279Sphk vattr_null(&va_null); 150135279Sphk once = 1; 151135279Sphk } 152116271Sphk 153132902Sphk if (vfc->vfc_version != VFS_VERSION) { 154132902Sphk printf("ERROR: filesystem %s, unsupported ABI version %x\n", 155132902Sphk vfc->vfc_name, vfc->vfc_version); 156132902Sphk return (EINVAL); 157132902Sphk } 158132710Sphk if (vfs_byname(vfc->vfc_name) != NULL) 159132710Sphk return EEXIST; 16040435Speter 16140435Speter vfc->vfc_typenum = maxvfsconf++; 162132710Sphk TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 16340435Speter 16440435Speter /* 16544549Sdfr * If this filesystem has a sysctl node under vfs 16644549Sdfr * (i.e. vfs.xxfs), then change the oid number of that node to 16744549Sdfr * match the filesystem's type number. This allows user code 16844549Sdfr * which uses the type number to read sysctl variables defined 16944549Sdfr * by the filesystem to continue working. Since the oids are 17044549Sdfr * in a sorted list, we need to make sure the order is 17144549Sdfr * preserved by re-registering the oid after modifying its 17244549Sdfr * number. 17344549Sdfr */ 17472012Sphk SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) 17544549Sdfr if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 17644549Sdfr sysctl_unregister_oid(oidp); 17744549Sdfr oidp->oid_number = vfc->vfc_typenum; 17844549Sdfr sysctl_register_oid(oidp); 17944549Sdfr } 18044549Sdfr 18144549Sdfr /* 182116271Sphk * Initialise unused ``struct vfsops'' fields, to use 183116271Sphk * the vfs_std*() functions. Note, we need the mount 184116271Sphk * and unmount operations, at the least. The check 185116271Sphk * for vfsops available is just a debugging aid. 186116271Sphk */ 187116271Sphk KASSERT(vfc->vfc_vfsops != NULL, 188116271Sphk ("Filesystem %s has no vfsops", vfc->vfc_name)); 189116271Sphk /* 190116271Sphk * Check the mount and unmount operations. 191116271Sphk */ 192116271Sphk vfsops = vfc->vfc_vfsops; 193138509Sphk KASSERT(vfsops->vfs_mount != NULL, 194138509Sphk ("Filesystem %s has no mount op", vfc->vfc_name)); 195116271Sphk KASSERT(vfsops->vfs_unmount != NULL, 196116271Sphk ("Filesystem %s has no unmount op", vfc->vfc_name)); 197116271Sphk 198116271Sphk if (vfsops->vfs_root == NULL) 199116271Sphk /* return file system's root vnode */ 200116271Sphk vfsops->vfs_root = vfs_stdroot; 201116271Sphk if (vfsops->vfs_quotactl == NULL) 202116271Sphk /* quota control */ 203116271Sphk vfsops->vfs_quotactl = vfs_stdquotactl; 204116271Sphk if (vfsops->vfs_statfs == NULL) 205116271Sphk /* return file system's status */ 206116271Sphk vfsops->vfs_statfs = vfs_stdstatfs; 207116271Sphk if (vfsops->vfs_sync == NULL) 208116271Sphk /* 209116271Sphk * flush unwritten data (nosync) 210116271Sphk * file systems can use vfs_stdsync 211116271Sphk * explicitly by setting it in the 212116271Sphk * vfsop vector. 213116271Sphk */ 214116271Sphk vfsops->vfs_sync = vfs_stdnosync; 215116271Sphk if (vfsops->vfs_vget == NULL) 216116271Sphk /* convert an inode number to a vnode */ 217116271Sphk vfsops->vfs_vget = vfs_stdvget; 218116271Sphk if (vfsops->vfs_fhtovp == NULL) 219116271Sphk /* turn an NFS file handle into a vnode */ 220116271Sphk vfsops->vfs_fhtovp = vfs_stdfhtovp; 221116271Sphk if (vfsops->vfs_checkexp == NULL) 222116271Sphk /* check if file system is exported */ 223116271Sphk vfsops->vfs_checkexp = vfs_stdcheckexp; 224116271Sphk if (vfsops->vfs_vptofh == NULL) 225116271Sphk /* turn a vnode into an NFS file handle */ 226116271Sphk vfsops->vfs_vptofh = vfs_stdvptofh; 227116271Sphk if (vfsops->vfs_init == NULL) 228116271Sphk /* file system specific initialisation */ 229116271Sphk vfsops->vfs_init = vfs_stdinit; 230116271Sphk if (vfsops->vfs_uninit == NULL) 231116271Sphk /* file system specific uninitialisation */ 232116271Sphk vfsops->vfs_uninit = vfs_stduninit; 233116271Sphk if (vfsops->vfs_extattrctl == NULL) 234116271Sphk /* extended attribute control */ 235116271Sphk vfsops->vfs_extattrctl = vfs_stdextattrctl; 236131733Salfred if (vfsops->vfs_sysctl == NULL) 237131733Salfred vfsops->vfs_sysctl = vfs_stdsysctl; 238116271Sphk 239116271Sphk /* 24040435Speter * Call init function for this VFS... 24140435Speter */ 24240435Speter (*(vfc->vfc_vfsops->vfs_init))(vfc); 24340435Speter 24440435Speter return 0; 2451541Srgrimes} 2462946Swollman 24740435Speter 24896755Strhodes/* Remove registration of a filesystem type */ 249141634Sphkstatic int 25041056Spetervfs_unregister(struct vfsconf *vfc) 25140435Speter{ 252132710Sphk struct vfsconf *vfsp; 25340435Speter int error, i, maxtypenum; 25440435Speter 25540435Speter i = vfc->vfc_typenum; 25640435Speter 257132710Sphk vfsp = vfs_byname(vfc->vfc_name); 25840435Speter if (vfsp == NULL) 25940435Speter return EINVAL; 26040435Speter if (vfsp->vfc_refcount) 26140435Speter return EBUSY; 26240435Speter if (vfc->vfc_vfsops->vfs_uninit != NULL) { 26340435Speter error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 26440435Speter if (error) 26540435Speter return (error); 26640435Speter } 267132710Sphk TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 26840435Speter maxtypenum = VFS_GENERIC; 269132710Sphk TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 27040435Speter if (maxtypenum < vfsp->vfc_typenum) 27140435Speter maxtypenum = vfsp->vfc_typenum; 27240435Speter maxvfsconf = maxtypenum + 1; 27340435Speter return 0; 27440435Speter} 27541056Speter 27691690Seivind/* 27796755Strhodes * Standard kernel module handling code for filesystem modules. 27891690Seivind * Referenced from VFS_SET(). 27991690Seivind */ 28041056Speterint 28141170Sbdevfs_modevent(module_t mod, int type, void *data) 28241056Speter{ 28341056Speter struct vfsconf *vfc; 28441056Speter int error = 0; 28541056Speter 28641056Speter vfc = (struct vfsconf *)data; 28741056Speter 28841056Speter switch (type) { 28941056Speter case MOD_LOAD: 29041056Speter if (vfc) 29141056Speter error = vfs_register(vfc); 29241056Speter break; 29341056Speter 29441056Speter case MOD_UNLOAD: 29541056Speter if (vfc) 29641056Speter error = vfs_unregister(vfc); 29741056Speter break; 298132199Sphk default: 299132199Sphk error = EOPNOTSUPP; 30041056Speter break; 30141056Speter } 30241056Speter return (error); 30341056Speter} 304