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