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$"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/fnv_hash.h> 43 #include <sys/kernel.h> 44 #include <sys/linker.h> 45 #include <sys/mount.h> 46 #include <sys/proc.h> 47 #include <sys/sx.h> 48 #include <sys/syscallsubr.h> 49 #include <sys/sysctl.h> 50 #include <sys/vnode.h> 51 #include <sys/malloc.h> 52 53 static int vfs_register(struct vfsconf *); 54 static int vfs_unregister(struct vfsconf *); 55 56 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 57 58 /* 59 * The highest defined VFS number. 60 */ 61 int maxvfsconf = VFS_GENERIC + 1; 62 63 /* 64 * Single-linked list of configured VFSes. 65 * New entries are added/deleted by vfs_register()/vfs_unregister() 66 */ 67 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf); 68 struct sx vfsconf_sx; 69 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf"); 70 71 /* 72 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash 73 * calculation on vfc_name, so that it doesn't change when file systems are 74 * loaded in a different order. This will avoid the NFS server file handles from 75 * changing for file systems that use vfc_typenum in their fsid. 76 */ 77 static int vfs_typenumhash = 1; 78 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0, 79 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not" 80 "change when file systems are loaded in a different order."); 81 82 /* 83 * A Zen vnode attribute structure. 84 * 85 * Initialized when the first filesystem registers by vfs_register(). 86 */ 87 struct vattr va_null; 88 89 /* 90 * vfs_init.c 91 * 92 * Allocate and fill in operations vectors. 93 * 94 * An undocumented feature of this approach to defining operations is that 95 * there can be multiple entries in vfs_opv_descs for the same operations 96 * vector. This allows third parties to extend the set of operations 97 * supported by another layer in a binary compatibile way. For example, 98 * assume that NFS needed to be modified to support Ficus. NFS has an entry 99 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 100 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 101 * listing those new operations Ficus adds to NFS, all without modifying the 102 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 103 * that is a(whole)nother story.) This is a feature. 104 */ 105 106 /* 107 * Routines having to do with the management of the vnode table. 108 */ 109 110 static struct vfsconf * 111 vfs_byname_locked(const char *name) 112 { 113 struct vfsconf *vfsp; 114 115 sx_assert(&vfsconf_sx, SA_LOCKED); 116 if (!strcmp(name, "ffs")) 117 name = "ufs"; 118 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 119 if (!strcmp(name, vfsp->vfc_name)) 120 return (vfsp); 121 } 122 return (NULL); 123 } 124 125 struct vfsconf * 126 vfs_byname(const char *name) 127 { 128 struct vfsconf *vfsp; 129 130 vfsconf_slock(); 131 vfsp = vfs_byname_locked(name); 132 vfsconf_sunlock(); 133 return (vfsp); 134 } 135 136 struct vfsconf * 137 vfs_byname_kld(const char *fstype, struct thread *td, int *error) 138 { 139 struct vfsconf *vfsp; 140 int fileid, loaded; 141 142 vfsp = vfs_byname(fstype); 143 if (vfsp != NULL) 144 return (vfsp); 145 146 /* Try to load the respective module. */ 147 *error = kern_kldload(td, fstype, &fileid); 148 loaded = (*error == 0); 149 if (*error == EEXIST) 150 *error = 0; 151 if (*error) 152 return (NULL); 153 154 /* Look up again to see if the VFS was loaded. */ 155 vfsp = vfs_byname(fstype); 156 if (vfsp == NULL) { 157 if (loaded) 158 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); 159 *error = ENODEV; 160 return (NULL); 161 } 162 return (vfsp); 163 } 164 165 166 /* Register a new filesystem type in the global table */ 167 static int 168 vfs_register(struct vfsconf *vfc) 169 { 170 struct sysctl_oid *oidp; 171 struct vfsops *vfsops; 172 static int once; 173 struct vfsconf *tvfc; 174 uint32_t hashval; 175 int secondpass; 176 177 if (!once) { 178 vattr_null(&va_null); 179 once = 1; 180 } 181 182 if (vfc->vfc_version != VFS_VERSION) { 183 printf("ERROR: filesystem %s, unsupported ABI version %x\n", 184 vfc->vfc_name, vfc->vfc_version); 185 return (EINVAL); 186 } 187 vfsconf_lock(); 188 if (vfs_byname_locked(vfc->vfc_name) != NULL) { 189 vfsconf_unlock(); 190 return (EEXIST); 191 } 192 193 if (vfs_typenumhash != 0) { 194 /* 195 * Calculate a hash on vfc_name to use for vfc_typenum. Unless 196 * all of 1<->255 are assigned, it is limited to 8bits since 197 * that is what ZFS uses from vfc_typenum and is also the 198 * preferred range for vfs_getnewfsid(). 199 */ 200 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT); 201 hashval &= 0xff; 202 secondpass = 0; 203 do { 204 /* Look for and fix any collision. */ 205 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) { 206 if (hashval == tvfc->vfc_typenum) { 207 if (hashval == 255 && secondpass == 0) { 208 hashval = 1; 209 secondpass = 1; 210 } else 211 hashval++; 212 break; 213 } 214 } 215 } while (tvfc != NULL); 216 vfc->vfc_typenum = hashval; 217 if (vfc->vfc_typenum >= maxvfsconf) 218 maxvfsconf = vfc->vfc_typenum + 1; 219 } else 220 vfc->vfc_typenum = maxvfsconf++; 221 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 222 223 /* 224 * Initialise unused ``struct vfsops'' fields, to use 225 * the vfs_std*() functions. Note, we need the mount 226 * and unmount operations, at the least. The check 227 * for vfsops available is just a debugging aid. 228 */ 229 KASSERT(vfc->vfc_vfsops != NULL, 230 ("Filesystem %s has no vfsops", vfc->vfc_name)); 231 /* 232 * Check the mount and unmount operations. 233 */ 234 vfsops = vfc->vfc_vfsops; 235 KASSERT(vfsops->vfs_mount != NULL, 236 ("Filesystem %s has no mount op", vfc->vfc_name)); 237 KASSERT(vfsops->vfs_unmount != NULL, 238 ("Filesystem %s has no unmount op", vfc->vfc_name)); 239 240 if (vfsops->vfs_root == NULL) 241 /* return file system's root vnode */ 242 vfsops->vfs_root = vfs_stdroot; 243 if (vfsops->vfs_quotactl == NULL) 244 /* quota control */ 245 vfsops->vfs_quotactl = vfs_stdquotactl; 246 if (vfsops->vfs_statfs == NULL) 247 /* return file system's status */ 248 vfsops->vfs_statfs = vfs_stdstatfs; 249 if (vfsops->vfs_sync == NULL) 250 /* 251 * flush unwritten data (nosync) 252 * file systems can use vfs_stdsync 253 * explicitly by setting it in the 254 * vfsop vector. 255 */ 256 vfsops->vfs_sync = vfs_stdnosync; 257 if (vfsops->vfs_vget == NULL) 258 /* convert an inode number to a vnode */ 259 vfsops->vfs_vget = vfs_stdvget; 260 if (vfsops->vfs_fhtovp == NULL) 261 /* turn an NFS file handle into a vnode */ 262 vfsops->vfs_fhtovp = vfs_stdfhtovp; 263 if (vfsops->vfs_checkexp == NULL) 264 /* check if file system is exported */ 265 vfsops->vfs_checkexp = vfs_stdcheckexp; 266 if (vfsops->vfs_init == NULL) 267 /* file system specific initialisation */ 268 vfsops->vfs_init = vfs_stdinit; 269 if (vfsops->vfs_uninit == NULL) 270 /* file system specific uninitialisation */ 271 vfsops->vfs_uninit = vfs_stduninit; 272 if (vfsops->vfs_extattrctl == NULL) 273 /* extended attribute control */ 274 vfsops->vfs_extattrctl = vfs_stdextattrctl; 275 if (vfsops->vfs_sysctl == NULL) 276 vfsops->vfs_sysctl = vfs_stdsysctl; 277 278 /* 279 * Call init function for this VFS... 280 */ 281 (*(vfc->vfc_vfsops->vfs_init))(vfc); 282 vfsconf_unlock(); 283 284 /* 285 * If this filesystem has a sysctl node under vfs 286 * (i.e. vfs.xxfs), then change the oid number of that node to 287 * match the filesystem's type number. This allows user code 288 * which uses the type number to read sysctl variables defined 289 * by the filesystem to continue working. Since the oids are 290 * in a sorted list, we need to make sure the order is 291 * preserved by re-registering the oid after modifying its 292 * number. 293 */ 294 sysctl_xlock(); 295 SLIST_FOREACH(oidp, SYSCTL_CHILDREN(&sysctl___vfs), oid_link) { 296 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 297 sysctl_unregister_oid(oidp); 298 oidp->oid_number = vfc->vfc_typenum; 299 sysctl_register_oid(oidp); 300 break; 301 } 302 } 303 sysctl_xunlock(); 304 305 return (0); 306 } 307 308 309 /* Remove registration of a filesystem type */ 310 static int 311 vfs_unregister(struct vfsconf *vfc) 312 { 313 struct vfsconf *vfsp; 314 int error, i, maxtypenum; 315 316 i = vfc->vfc_typenum; 317 318 vfsconf_lock(); 319 vfsp = vfs_byname_locked(vfc->vfc_name); 320 if (vfsp == NULL) { 321 vfsconf_unlock(); 322 return (EINVAL); 323 } 324 if (vfsp->vfc_refcount != 0) { 325 vfsconf_unlock(); 326 return (EBUSY); 327 } 328 if (vfc->vfc_vfsops->vfs_uninit != NULL) { 329 error = (*vfc->vfc_vfsops->vfs_uninit)(vfsp); 330 if (error != 0) { 331 vfsconf_unlock(); 332 return (error); 333 } 334 } 335 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 336 maxtypenum = VFS_GENERIC; 337 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 338 if (maxtypenum < vfsp->vfc_typenum) 339 maxtypenum = vfsp->vfc_typenum; 340 maxvfsconf = maxtypenum + 1; 341 vfsconf_unlock(); 342 return (0); 343 } 344 345 /* 346 * Standard kernel module handling code for filesystem modules. 347 * Referenced from VFS_SET(). 348 */ 349 int 350 vfs_modevent(module_t mod, int type, void *data) 351 { 352 struct vfsconf *vfc; 353 int error = 0; 354 355 vfc = (struct vfsconf *)data; 356 357 switch (type) { 358 case MOD_LOAD: 359 if (vfc) 360 error = vfs_register(vfc); 361 break; 362 363 case MOD_UNLOAD: 364 if (vfc) 365 error = vfs_unregister(vfc); 366 break; 367 default: 368 error = EOPNOTSUPP; 369 break; 370 } 371 return (error); 372 } 373