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