1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed 8 * to Berkeley by John Heidemann of the UCLA Ficus project. 9 * 10 * Source: * @(#)i405_init.c 2.10 92/04/27 UCLA Ficus project 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_init.c 8.3 (Berkeley) 1/4/94 37 */ 38 39 #include <sys/cdefs.h> 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/fnv_hash.h> 43 #include <sys/jail.h> 44 #include <sys/kernel.h> 45 #include <sys/linker.h> 46 #include <sys/mount.h> 47 #include <sys/proc.h> 48 #include <sys/sx.h> 49 #include <sys/syscallsubr.h> 50 #include <sys/sysctl.h> 51 #include <sys/vnode.h> 52 #include <sys/malloc.h> 53 54 static int vfs_register(struct vfsconf *); 55 static int vfs_unregister(struct vfsconf *); 56 57 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes"); 58 59 /* 60 * The highest defined VFS number. 61 */ 62 int maxvfsconf = VFS_GENERIC + 1; 63 64 /* 65 * Single-linked list of configured VFSes. 66 * New entries are added/deleted by vfs_register()/vfs_unregister() 67 */ 68 struct vfsconfhead vfsconf = TAILQ_HEAD_INITIALIZER(vfsconf); 69 struct sx vfsconf_sx; 70 SX_SYSINIT(vfsconf, &vfsconf_sx, "vfsconf"); 71 72 /* 73 * Loader.conf variable vfs.typenumhash enables setting vfc_typenum using a hash 74 * calculation on vfc_name, so that it doesn't change when file systems are 75 * loaded in a different order. This will avoid the NFS server file handles from 76 * changing for file systems that use vfc_typenum in their fsid. 77 */ 78 static int vfs_typenumhash = 1; 79 SYSCTL_INT(_vfs, OID_AUTO, typenumhash, CTLFLAG_RDTUN, &vfs_typenumhash, 0, 80 "Set vfc_typenum using a hash calculation on vfc_name, so that it does not" 81 " change when file systems are loaded in a different order."); 82 83 /* 84 * A Zen vnode attribute structure. 85 * 86 * Initialized when the first filesystem registers by vfs_register(). 87 */ 88 struct vattr va_null; 89 90 /* 91 * vfs_init.c 92 * 93 * Allocate and fill in operations vectors. 94 * 95 * An undocumented feature of this approach to defining operations is that 96 * there can be multiple entries in vfs_opv_descs for the same operations 97 * vector. This allows third parties to extend the set of operations 98 * supported by another layer in a binary compatibile way. For example, 99 * assume that NFS needed to be modified to support Ficus. NFS has an entry 100 * (probably nfs_vnopdeop_decls) declaring all the operations NFS supports by 101 * default. Ficus could add another entry (ficus_nfs_vnodeop_decl_entensions) 102 * listing those new operations Ficus adds to NFS, all without modifying the 103 * NFS code. (Of couse, the OTW NFS protocol still needs to be munged, but 104 * that is a(whole)nother story.) This is a feature. 105 */ 106 107 /* 108 * Routines having to do with the management of the vnode table. 109 */ 110 111 static struct vfsconf * 112 vfs_byname_locked(const char *name) 113 { 114 struct vfsconf *vfsp; 115 116 sx_assert(&vfsconf_sx, SA_LOCKED); 117 if (!strcmp(name, "ffs")) 118 name = "ufs"; 119 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) { 120 if (!strcmp(name, vfsp->vfc_name)) 121 return (vfsp); 122 } 123 return (NULL); 124 } 125 126 struct vfsconf * 127 vfs_byname(const char *name) 128 { 129 struct vfsconf *vfsp; 130 131 vfsconf_slock(); 132 vfsp = vfs_byname_locked(name); 133 vfsconf_sunlock(); 134 return (vfsp); 135 } 136 137 struct vfsconf * 138 vfs_byname_kld(const char *fstype, struct thread *td, int *error) 139 { 140 struct vfsconf *vfsp; 141 int fileid, loaded; 142 143 vfsp = vfs_byname(fstype); 144 if (vfsp != NULL) 145 return (vfsp); 146 147 /* Try to load the respective module. */ 148 *error = kern_kldload(td, fstype, &fileid); 149 loaded = (*error == 0); 150 if (*error == EEXIST) 151 *error = 0; 152 if (*error) 153 return (NULL); 154 155 /* Look up again to see if the VFS was loaded. */ 156 vfsp = vfs_byname(fstype); 157 if (vfsp == NULL) { 158 if (loaded) 159 (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); 160 *error = ENODEV; 161 return (NULL); 162 } 163 return (vfsp); 164 } 165 166 static int 167 vfs_mount_sigdefer(struct mount *mp) 168 { 169 int prev_stops, rc; 170 171 TSRAW(curthread, TS_ENTER, "VFS_MOUNT", mp->mnt_vfc->vfc_name); 172 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 173 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_mount)(mp); 174 sigallowstop(prev_stops); 175 TSRAW(curthread, TS_EXIT, "VFS_MOUNT", mp->mnt_vfc->vfc_name); 176 return (rc); 177 } 178 179 static int 180 vfs_unmount_sigdefer(struct mount *mp, int mntflags) 181 { 182 int prev_stops, rc; 183 184 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 185 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unmount)(mp, mntflags); 186 sigallowstop(prev_stops); 187 return (rc); 188 } 189 190 static int 191 vfs_root_sigdefer(struct mount *mp, int flags, struct vnode **vpp) 192 { 193 int prev_stops, rc; 194 195 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 196 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_root)(mp, flags, vpp); 197 sigallowstop(prev_stops); 198 return (rc); 199 } 200 201 static int 202 vfs_cachedroot_sigdefer(struct mount *mp, int flags, struct vnode **vpp) 203 { 204 int prev_stops, rc; 205 206 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 207 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_cachedroot)(mp, flags, vpp); 208 sigallowstop(prev_stops); 209 return (rc); 210 } 211 212 static int 213 vfs_quotactl_sigdefer(struct mount *mp, int cmd, uid_t uid, void *arg, 214 bool *mp_busy) 215 { 216 int prev_stops, rc; 217 218 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 219 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_quotactl)(mp, cmd, uid, arg, 220 mp_busy); 221 sigallowstop(prev_stops); 222 return (rc); 223 } 224 225 static int 226 vfs_statfs_sigdefer(struct mount *mp, struct statfs *sbp) 227 { 228 int prev_stops, rc; 229 230 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 231 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_statfs)(mp, sbp); 232 sigallowstop(prev_stops); 233 return (rc); 234 } 235 236 static int 237 vfs_sync_sigdefer(struct mount *mp, int waitfor) 238 { 239 int prev_stops, rc; 240 241 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 242 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sync)(mp, waitfor); 243 sigallowstop(prev_stops); 244 return (rc); 245 } 246 247 static int 248 vfs_vget_sigdefer(struct mount *mp, ino_t ino, int flags, struct vnode **vpp) 249 { 250 int prev_stops, rc; 251 252 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 253 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_vget)(mp, ino, flags, vpp); 254 sigallowstop(prev_stops); 255 return (rc); 256 } 257 258 static int 259 vfs_fhtovp_sigdefer(struct mount *mp, struct fid *fidp, int flags, 260 struct vnode **vpp) 261 { 262 int prev_stops, rc; 263 264 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 265 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_fhtovp)(mp, fidp, flags, vpp); 266 sigallowstop(prev_stops); 267 return (rc); 268 } 269 270 static int 271 vfs_checkexp_sigdefer(struct mount *mp, struct sockaddr *nam, uint64_t *exflg, 272 struct ucred **credp, int *numsecflavors, int *secflavors) 273 { 274 int prev_stops, rc; 275 276 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 277 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_checkexp)(mp, nam, exflg, credp, 278 numsecflavors, secflavors); 279 sigallowstop(prev_stops); 280 return (rc); 281 } 282 283 static int 284 vfs_extattrctl_sigdefer(struct mount *mp, int cmd, struct vnode *filename_vp, 285 int attrnamespace, const char *attrname) 286 { 287 int prev_stops, rc; 288 289 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 290 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_extattrctl)(mp, cmd, 291 filename_vp, attrnamespace, attrname); 292 sigallowstop(prev_stops); 293 return (rc); 294 } 295 296 static int 297 vfs_sysctl_sigdefer(struct mount *mp, fsctlop_t op, struct sysctl_req *req) 298 { 299 int prev_stops, rc; 300 301 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 302 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_sysctl)(mp, op, req); 303 sigallowstop(prev_stops); 304 return (rc); 305 } 306 307 static void 308 vfs_susp_clean_sigdefer(struct mount *mp) 309 { 310 int prev_stops; 311 312 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean == NULL) 313 return; 314 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 315 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_susp_clean)(mp); 316 sigallowstop(prev_stops); 317 } 318 319 static void 320 vfs_reclaim_lowervp_sigdefer(struct mount *mp, struct vnode *vp) 321 { 322 int prev_stops; 323 324 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp == NULL) 325 return; 326 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 327 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_reclaim_lowervp)(mp, vp); 328 sigallowstop(prev_stops); 329 } 330 331 static void 332 vfs_unlink_lowervp_sigdefer(struct mount *mp, struct vnode *vp) 333 { 334 int prev_stops; 335 336 if (*mp->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp == NULL) 337 return; 338 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 339 (*(mp)->mnt_vfc->vfc_vfsops_sd->vfs_unlink_lowervp)(mp, vp); 340 sigallowstop(prev_stops); 341 } 342 343 static void 344 vfs_purge_sigdefer(struct mount *mp) 345 { 346 int prev_stops; 347 348 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 349 (*mp->mnt_vfc->vfc_vfsops_sd->vfs_purge)(mp); 350 sigallowstop(prev_stops); 351 } 352 353 static int 354 vfs_report_lockf_sigdefer(struct mount *mp, struct sbuf *sb) 355 { 356 int prev_stops, rc; 357 358 prev_stops = sigdeferstop(SIGDEFERSTOP_SILENT); 359 rc = (*mp->mnt_vfc->vfc_vfsops_sd->vfs_report_lockf)(mp, sb); 360 sigallowstop(prev_stops); 361 return (rc); 362 } 363 364 static struct vfsops vfsops_sigdefer = { 365 .vfs_mount = vfs_mount_sigdefer, 366 .vfs_unmount = vfs_unmount_sigdefer, 367 .vfs_root = vfs_root_sigdefer, 368 .vfs_cachedroot = vfs_cachedroot_sigdefer, 369 .vfs_quotactl = vfs_quotactl_sigdefer, 370 .vfs_statfs = vfs_statfs_sigdefer, 371 .vfs_sync = vfs_sync_sigdefer, 372 .vfs_vget = vfs_vget_sigdefer, 373 .vfs_fhtovp = vfs_fhtovp_sigdefer, 374 .vfs_checkexp = vfs_checkexp_sigdefer, 375 .vfs_extattrctl = vfs_extattrctl_sigdefer, 376 .vfs_sysctl = vfs_sysctl_sigdefer, 377 .vfs_susp_clean = vfs_susp_clean_sigdefer, 378 .vfs_reclaim_lowervp = vfs_reclaim_lowervp_sigdefer, 379 .vfs_unlink_lowervp = vfs_unlink_lowervp_sigdefer, 380 .vfs_purge = vfs_purge_sigdefer, 381 .vfs_report_lockf = vfs_report_lockf_sigdefer, 382 }; 383 384 /* Register a new filesystem type in the global table */ 385 static int 386 vfs_register(struct vfsconf *vfc) 387 { 388 struct sysctl_oid *oidp; 389 struct vfsops *vfsops; 390 static int once; 391 struct vfsconf *tvfc; 392 uint32_t hashval; 393 int secondpass; 394 395 if (!once) { 396 vattr_null(&va_null); 397 once = 1; 398 } 399 400 if (vfc->vfc_version != VFS_VERSION) { 401 printf("ERROR: filesystem %s, unsupported ABI version %x\n", 402 vfc->vfc_name, vfc->vfc_version); 403 return (EINVAL); 404 } 405 vfsconf_lock(); 406 if (vfs_byname_locked(vfc->vfc_name) != NULL) { 407 vfsconf_unlock(); 408 return (EEXIST); 409 } 410 411 if (vfs_typenumhash != 0) { 412 /* 413 * Calculate a hash on vfc_name to use for vfc_typenum. Unless 414 * all of 1<->255 are assigned, it is limited to 8bits since 415 * that is what ZFS uses from vfc_typenum and is also the 416 * preferred range for vfs_getnewfsid(). 417 */ 418 hashval = fnv_32_str(vfc->vfc_name, FNV1_32_INIT); 419 hashval &= 0xff; 420 secondpass = 0; 421 do { 422 /* Look for and fix any collision. */ 423 TAILQ_FOREACH(tvfc, &vfsconf, vfc_list) { 424 if (hashval == tvfc->vfc_typenum) { 425 if (hashval == 255 && secondpass == 0) { 426 hashval = 1; 427 secondpass = 1; 428 } else 429 hashval++; 430 break; 431 } 432 } 433 } while (tvfc != NULL); 434 vfc->vfc_typenum = hashval; 435 if (vfc->vfc_typenum >= maxvfsconf) 436 maxvfsconf = vfc->vfc_typenum + 1; 437 } else 438 vfc->vfc_typenum = maxvfsconf++; 439 TAILQ_INSERT_TAIL(&vfsconf, vfc, vfc_list); 440 441 /* 442 * Initialise unused ``struct vfsops'' fields, to use 443 * the vfs_std*() functions. Note, we need the mount 444 * and unmount operations, at the least. The check 445 * for vfsops available is just a debugging aid. 446 */ 447 KASSERT(vfc->vfc_vfsops != NULL, 448 ("Filesystem %s has no vfsops", vfc->vfc_name)); 449 /* 450 * Check the mount and unmount operations. 451 */ 452 vfsops = vfc->vfc_vfsops; 453 KASSERT(vfsops->vfs_mount != NULL, 454 ("Filesystem %s has no mount op", vfc->vfc_name)); 455 KASSERT(vfsops->vfs_unmount != NULL, 456 ("Filesystem %s has no unmount op", vfc->vfc_name)); 457 458 if (vfsops->vfs_root == NULL) 459 /* return file system's root vnode */ 460 vfsops->vfs_root = vfs_stdroot; 461 if (vfsops->vfs_quotactl == NULL) 462 /* quota control */ 463 vfsops->vfs_quotactl = vfs_stdquotactl; 464 if (vfsops->vfs_statfs == NULL) 465 /* return file system's status */ 466 vfsops->vfs_statfs = vfs_stdstatfs; 467 if (vfsops->vfs_sync == NULL) 468 /* 469 * flush unwritten data (nosync) 470 * file systems can use vfs_stdsync 471 * explicitly by setting it in the 472 * vfsop vector. 473 */ 474 vfsops->vfs_sync = vfs_stdnosync; 475 if (vfsops->vfs_vget == NULL) 476 /* convert an inode number to a vnode */ 477 vfsops->vfs_vget = vfs_stdvget; 478 if (vfsops->vfs_fhtovp == NULL) 479 /* turn an NFS file handle into a vnode */ 480 vfsops->vfs_fhtovp = vfs_stdfhtovp; 481 if (vfsops->vfs_checkexp == NULL) 482 /* check if file system is exported */ 483 vfsops->vfs_checkexp = vfs_stdcheckexp; 484 if (vfsops->vfs_init == NULL) 485 /* file system specific initialisation */ 486 vfsops->vfs_init = vfs_stdinit; 487 if (vfsops->vfs_uninit == NULL) 488 /* file system specific uninitialisation */ 489 vfsops->vfs_uninit = vfs_stduninit; 490 if (vfsops->vfs_extattrctl == NULL) 491 /* extended attribute control */ 492 vfsops->vfs_extattrctl = vfs_stdextattrctl; 493 if (vfsops->vfs_sysctl == NULL) 494 vfsops->vfs_sysctl = vfs_stdsysctl; 495 if (vfsops->vfs_report_lockf == NULL) 496 vfsops->vfs_report_lockf = vfs_report_lockf; 497 498 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) { 499 vfc->vfc_vfsops_sd = vfc->vfc_vfsops; 500 vfc->vfc_vfsops = &vfsops_sigdefer; 501 } 502 503 if (vfc->vfc_flags & VFCF_JAIL) 504 prison_add_vfs(vfc); 505 506 /* 507 * Call init function for this VFS... 508 */ 509 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) 510 vfc->vfc_vfsops_sd->vfs_init(vfc); 511 else 512 vfc->vfc_vfsops->vfs_init(vfc); 513 vfsconf_unlock(); 514 515 /* 516 * If this filesystem has a sysctl node under vfs 517 * (i.e. vfs.xxfs), then change the oid number of that node to 518 * match the filesystem's type number. This allows user code 519 * which uses the type number to read sysctl variables defined 520 * by the filesystem to continue working. Since the oids are 521 * in a sorted list, we need to make sure the order is 522 * preserved by re-registering the oid after modifying its 523 * number. 524 */ 525 sysctl_wlock(); 526 RB_FOREACH(oidp, sysctl_oid_list, SYSCTL_CHILDREN(&sysctl___vfs)) { 527 if (strcmp(oidp->oid_name, vfc->vfc_name) == 0) { 528 sysctl_unregister_oid(oidp); 529 oidp->oid_number = vfc->vfc_typenum; 530 sysctl_register_oid(oidp); 531 break; 532 } 533 } 534 sysctl_wunlock(); 535 536 return (0); 537 } 538 539 /* Remove registration of a filesystem type */ 540 static int 541 vfs_unregister(struct vfsconf *vfc) 542 { 543 struct vfsconf *vfsp; 544 int error, maxtypenum; 545 546 vfsconf_lock(); 547 vfsp = vfs_byname_locked(vfc->vfc_name); 548 if (vfsp == NULL) { 549 vfsconf_unlock(); 550 return (EINVAL); 551 } 552 if (vfsp->vfc_refcount != 0) { 553 vfsconf_unlock(); 554 return (EBUSY); 555 } 556 error = 0; 557 if ((vfc->vfc_flags & VFCF_SBDRY) != 0) { 558 if (vfc->vfc_vfsops_sd->vfs_uninit != NULL) 559 error = vfc->vfc_vfsops_sd->vfs_uninit(vfsp); 560 } else { 561 if (vfc->vfc_vfsops->vfs_uninit != NULL) 562 error = vfc->vfc_vfsops->vfs_uninit(vfsp); 563 } 564 if (error != 0) { 565 vfsconf_unlock(); 566 return (error); 567 } 568 TAILQ_REMOVE(&vfsconf, vfsp, vfc_list); 569 maxtypenum = VFS_GENERIC; 570 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) 571 if (maxtypenum < vfsp->vfc_typenum) 572 maxtypenum = vfsp->vfc_typenum; 573 maxvfsconf = maxtypenum + 1; 574 vfsconf_unlock(); 575 return (0); 576 } 577 578 /* 579 * Standard kernel module handling code for filesystem modules. 580 * Referenced from VFS_SET(). 581 */ 582 int 583 vfs_modevent(module_t mod, int type, void *data) 584 { 585 struct vfsconf *vfc; 586 int error = 0; 587 588 vfc = (struct vfsconf *)data; 589 590 switch (type) { 591 case MOD_LOAD: 592 if (vfc) 593 error = vfs_register(vfc); 594 break; 595 596 case MOD_UNLOAD: 597 if (vfc) 598 error = vfs_unregister(vfc); 599 break; 600 default: 601 error = EOPNOTSUPP; 602 break; 603 } 604 return (error); 605 } 606