vfs_init.c revision 138350
1/* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed 6 * to Berkeley by John Heidemann of the UCLA Ficus project. 7 * 8 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/kern/vfs_init.c 138350 2004-12-03 16:11:01Z phk $"); 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/linker.h> 44#include <sys/mount.h> 45#include <sys/proc.h> 46#include <sys/sysctl.h> 47#include <sys/vnode.h> 48#include <sys/malloc.h> 49 50 51MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 52 53/* 54 * The highest defined VFS number. 55 */ 56int maxvfsconf = VFS_GENERIC + 1; 57 58/* 59 * Single-linked list of configured VFSes. 60 * New entries are added/deleted by vfs_register()/vfs_unregister() 61 */ 62struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf); 63 64/* 65 * A Zen vnode attribute structure. 66 * 67 * Initialized when the first filesystem registers by vfs_register(). 68 */ 69struct vattr va_null; 70 71/* 72 * vfs_init.c 73 * 74 * Allocate and fill in operations vectors. 75 * 76 * An undocumented feature of this approach to defining operations is that 77 * there can be multiple entries in vfs_opv_descs for the same operations 78 * vector. This allows third parties to extend the set of operations 79 * supported by another layer in a binary compatibile way. For example, 80 * assume that NFS needed to be modified to support Ficus. NFS has an entry 81 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 82 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 83 * listing those new operations Ficus adds to NFS, all without modifying the 84 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 85 * that is a(whole)nother story.) This is a feature. 86 */ 87 88/* 89 * Routines having to do with the management of the vnode table. 90 */ 91 92/* 93 * XXX: hack alert 94 */ 95int 96vcall(struct vnode *vp, u_int off, void *ap) 97{ 98 struct vop_vector *vop = vp->v_op; 99 vop_bypass_t **bpt; 100 int rc; 101 102 for(;;) { 103 bpt = (void *)((u_char *)vop + off); 104 if (vop != NULL && *bpt == NULL && vop->vop_bypass == NULL) { 105 vop = vop->vop_default; 106 continue; 107 } 108 break; 109 } 110 KASSERT(vop != NULL, ("No VCALL(%p...)", vp)); 111 if (*bpt != NULL) 112 rc = (*bpt)(ap); 113 else 114 rc = vop->vop_bypass(ap); 115 return (rc); 116} 117 118struct vfsconf * 119vfs_byname(const char *name) 120{ 121 struct vfsconf *vfsp; 122 123 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 124 if (!strcmp(name, vfsp->vfc_name)) 125 return (vfsp); 126 return (NULL); 127} 128 129struct vfsconf * 130vfs_byname_kld(const char *fstype, struct thread *td, int *error) 131{ 132 struct vfsconf *vfsp; 133 linker_file_t lf; 134 135 vfsp = vfs_byname(fstype); 136 if (vfsp != NULL) 137 return (vfsp); 138 139 /* Only load modules for root (very important!). */ 140 *error = suser(td); 141 if (*error) 142 return (NULL); 143 *error = securelevel_gt(td->td_ucred, 0); 144 if (*error) 145 return (NULL); 146 *error = linker_load_module(NULL, fstype, NULL, NULL, &lf); 147 if (lf == NULL) 148 *error = ENODEV; 149 if (*error) 150 return (NULL); 151 lf->userrefs++; 152 /* Look up again to see if the VFS was loaded. */ 153 vfsp = vfs_byname(fstype); 154 if (vfsp == NULL) { 155 lf->userrefs--; 156 linker_file_unload(lf, LINKER_UNLOAD_FORCE); 157 *error = ENODEV; 158 return (NULL); 159 } 160 return (vfsp); 161} 162 163 164/* Register a new filesystem type in the global table */ 165int 166vfs_register(struct vfsconf *vfc) 167{ 168 struct sysctl_oid *oidp; 169 struct vfsops *vfsops; 170 static int once; 171 172 if (!once) { 173 vattr_null(&va_null); 174 once = 1; 175 } 176 177 if (vfc->vfc_version != VFS_VERSION) { 178 printf("ERROR: filesystem %s, unsupported ABI version %x\n", 179 vfc->vfc_name, vfc->vfc_version); 180 return (EINVAL); 181 } 182 if (vfs_byname(vfc->vfc_name) != NULL) 183 return EEXIST; 184 185 vfc->vfc_typenum = maxvfsconf++; 186 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 187 188 /* 189 * If this filesystem has a sysctl node under vfs 190 * (i.e. vfs.xxfs), then change the oid number of that node to 191 * match the filesystem's type number. This allows user code 192 * which uses the type number to read sysctl variables defined 193 * by the filesystem to continue working. Since the oids are 194 * in a sorted list, we need to make sure the order is 195 * preserved by re-registering the oid after modifying its 196 * number. 197 */ 198 SLIST_FOREACH(oidp, &sysctl__vfs_children, oid_link) 199 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 200 sysctl_unregister_oid(oidp); 201 oidp->oid_number = vfc->vfc_typenum; 202 sysctl_register_oid(oidp); 203 } 204 205 /* 206 * Initialise unused ``struct vfsops'' fields, to use 207 * the vfs_std*() functions. Note, we need the mount 208 * and unmount operations, at the least. The check 209 * for vfsops available is just a debugging aid. 210 */ 211 KASSERT(vfc->vfc_vfsops != NULL, 212 ("Filesystem %s has no vfsops", vfc->vfc_name)); 213 /* 214 * Check the mount and unmount operations. 215 */ 216 vfsops = vfc->vfc_vfsops; 217 KASSERT(vfsops->vfs_mount != NULL || vfsops->vfs_omount != NULL, 218 ("Filesystem %s has no (o)mount op", vfc->vfc_name)); 219 KASSERT(vfsops->vfs_unmount != NULL, 220 ("Filesystem %s has no unmount op", vfc->vfc_name)); 221 222 if (vfsops->vfs_start == NULL) 223 /* make a file system operational */ 224 vfsops->vfs_start = vfs_stdstart; 225 if (vfsops->vfs_root == NULL) 226 /* return file system's root vnode */ 227 vfsops->vfs_root = vfs_stdroot; 228 if (vfsops->vfs_quotactl == NULL) 229 /* quota control */ 230 vfsops->vfs_quotactl = vfs_stdquotactl; 231 if (vfsops->vfs_statfs == NULL) 232 /* return file system's status */ 233 vfsops->vfs_statfs = vfs_stdstatfs; 234 if (vfsops->vfs_sync == NULL) 235 /* 236 * flush unwritten data (nosync) 237 * file systems can use vfs_stdsync 238 * explicitly by setting it in the 239 * vfsop vector. 240 */ 241 vfsops->vfs_sync = vfs_stdnosync; 242 if (vfsops->vfs_vget == NULL) 243 /* convert an inode number to a vnode */ 244 vfsops->vfs_vget = vfs_stdvget; 245 if (vfsops->vfs_fhtovp == NULL) 246 /* turn an NFS file handle into a vnode */ 247 vfsops->vfs_fhtovp = vfs_stdfhtovp; 248 if (vfsops->vfs_checkexp == NULL) 249 /* check if file system is exported */ 250 vfsops->vfs_checkexp = vfs_stdcheckexp; 251 if (vfsops->vfs_vptofh == NULL) 252 /* turn a vnode into an NFS file handle */ 253 vfsops->vfs_vptofh = vfs_stdvptofh; 254 if (vfsops->vfs_init == NULL) 255 /* file system specific initialisation */ 256 vfsops->vfs_init = vfs_stdinit; 257 if (vfsops->vfs_uninit == NULL) 258 /* file system specific uninitialisation */ 259 vfsops->vfs_uninit = vfs_stduninit; 260 if (vfsops->vfs_extattrctl == NULL) 261 /* extended attribute control */ 262 vfsops->vfs_extattrctl = vfs_stdextattrctl; 263 if (vfsops->vfs_sysctl == NULL) 264 vfsops->vfs_sysctl = vfs_stdsysctl; 265 266 /* 267 * Call init function for this VFS... 268 */ 269 (*(vfc->vfc_vfsops->vfs_init))(vfc); 270 271 return 0; 272} 273 274 275/* Remove registration of a filesystem type */ 276int 277vfs_unregister(struct vfsconf *vfc) 278{ 279 struct vfsconf *vfsp; 280 int error, i, maxtypenum; 281 282 i = vfc->vfc_typenum; 283 284 vfsp = vfs_byname(vfc->vfc_name); 285 if (vfsp == NULL) 286 return EINVAL; 287 if (vfsp->vfc_refcount) 288 return EBUSY; 289 if (vfc->vfc_vfsops->vfs_uninit != NULL) { 290 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 291 if (error) 292 return (error); 293 } 294 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 295 maxtypenum = VFS_GENERIC; 296 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 297 if (maxtypenum < vfsp->vfc_typenum) 298 maxtypenum = vfsp->vfc_typenum; 299 maxvfsconf = maxtypenum + 1; 300 return 0; 301} 302 303/* 304 * Standard kernel module handling code for filesystem modules. 305 * Referenced from VFS_SET(). 306 */ 307int 308vfs_modevent(module_t mod, int type, void *data) 309{ 310 struct vfsconf *vfc; 311 int error = 0; 312 313 vfc = (struct vfsconf *)data; 314 315 switch (type) { 316 case MOD_LOAD: 317 if (vfc) 318 error = vfs_register(vfc); 319 break; 320 321 case MOD_UNLOAD: 322 if (vfc) 323 error = vfs_unregister(vfc); 324 break; 325 default: 326 error = EOPNOTSUPP; 327 break; 328 } 329 return (error); 330} 331