1 /* $NetBSD: vfs_mount.c,v 1.67 2017/08/21 09:00:21 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 1997-2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * (c) UNIX System Laboratories, Inc. 37 * All or some portions of this file are derived from material licensed 38 * to the University of California by American Telephone and Telegraph 39 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 40 * the permission of UNIX System Laboratories, Inc. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.67 2017/08/21 09:00:21 hannken Exp $"); 71 72 #include <sys/param.h> 73 #include <sys/kernel.h> 74 75 #include <sys/atomic.h> 76 #include <sys/buf.h> 77 #include <sys/conf.h> 78 #include <sys/fcntl.h> 79 #include <sys/filedesc.h> 80 #include <sys/device.h> 81 #include <sys/kauth.h> 82 #include <sys/kmem.h> 83 #include <sys/module.h> 84 #include <sys/mount.h> 85 #include <sys/fstrans.h> 86 #include <sys/namei.h> 87 #include <sys/extattr.h> 88 #include <sys/syscallargs.h> 89 #include <sys/sysctl.h> 90 #include <sys/systm.h> 91 #include <sys/vfs_syscalls.h> 92 #include <sys/vnode_impl.h> 93 94 #include <miscfs/genfs/genfs.h> 95 #include <miscfs/specfs/specdev.h> 96 97 enum mountlist_type { 98 ME_MOUNT, 99 ME_MARKER 100 }; 101 struct mountlist_entry { 102 TAILQ_ENTRY(mountlist_entry) me_list; /* Mount list. */ 103 struct mount *me_mount; /* Actual mount if ME_MOUNT, 104 current mount else. */ 105 enum mountlist_type me_type; /* Mount or marker. */ 106 }; 107 struct mount_iterator { 108 struct mountlist_entry mi_entry; 109 }; 110 111 static struct vnode *vfs_vnode_iterator_next1(struct vnode_iterator *, 112 bool (*)(void *, struct vnode *), void *, bool); 113 114 /* Root filesystem. */ 115 vnode_t * rootvnode; 116 117 /* Mounted filesystem list. */ 118 static TAILQ_HEAD(mountlist, mountlist_entry) mountlist; 119 static kmutex_t mountlist_lock; 120 int vnode_offset_next_by_lru /* XXX: ugly hack for pstat.c */ 121 = offsetof(vnode_impl_t, vi_lrulist.tqe_next); 122 123 kmutex_t mntvnode_lock; 124 kmutex_t vfs_list_lock; 125 126 static specificdata_domain_t mount_specificdata_domain; 127 static kmutex_t mntid_lock; 128 129 static kmutex_t mountgen_lock; 130 static uint64_t mountgen; 131 132 void 133 vfs_mount_sysinit(void) 134 { 135 136 TAILQ_INIT(&mountlist); 137 mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE); 138 mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE); 139 mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE); 140 141 mount_specificdata_domain = specificdata_domain_create(); 142 mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE); 143 mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE); 144 mountgen = 0; 145 } 146 147 struct mount * 148 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp) 149 { 150 struct mount *mp; 151 int error __diagused; 152 extern struct vfsops dead_vfsops; 153 154 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP); 155 mp->mnt_op = vfsops; 156 mp->mnt_refcnt = 1; 157 TAILQ_INIT(&mp->mnt_vnodelist); 158 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE); 159 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE); 160 mp->mnt_vnodecovered = vp; 161 mount_initspecific(mp); 162 if (vfsops != &dead_vfsops) { 163 error = fstrans_mount(mp); 164 KASSERT(error == 0); 165 } 166 167 mutex_enter(&mountgen_lock); 168 mp->mnt_gen = mountgen++; 169 mutex_exit(&mountgen_lock); 170 171 return mp; 172 } 173 174 /* 175 * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and 176 * initialize a mount structure for it. 177 * 178 * Devname is usually updated by mount(8) after booting. 179 */ 180 int 181 vfs_rootmountalloc(const char *fstypename, const char *devname, 182 struct mount **mpp) 183 { 184 struct vfsops *vfsp = NULL; 185 struct mount *mp; 186 int error __diagused; 187 188 mutex_enter(&vfs_list_lock); 189 LIST_FOREACH(vfsp, &vfs_list, vfs_list) 190 if (!strncmp(vfsp->vfs_name, fstypename, 191 sizeof(mp->mnt_stat.f_fstypename))) 192 break; 193 if (vfsp == NULL) { 194 mutex_exit(&vfs_list_lock); 195 return (ENODEV); 196 } 197 vfsp->vfs_refcount++; 198 mutex_exit(&vfs_list_lock); 199 200 if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL) 201 return ENOMEM; 202 error = vfs_busy(mp); 203 KASSERT(error == 0); 204 mp->mnt_flag = MNT_RDONLY; 205 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, 206 sizeof(mp->mnt_stat.f_fstypename)); 207 mp->mnt_stat.f_mntonname[0] = '/'; 208 mp->mnt_stat.f_mntonname[1] = '\0'; 209 mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] = 210 '\0'; 211 (void)copystr(devname, mp->mnt_stat.f_mntfromname, 212 sizeof(mp->mnt_stat.f_mntfromname) - 1, 0); 213 *mpp = mp; 214 return 0; 215 } 216 217 /* 218 * vfs_getnewfsid: get a new unique fsid. 219 */ 220 void 221 vfs_getnewfsid(struct mount *mp) 222 { 223 static u_short xxxfs_mntid; 224 fsid_t tfsid; 225 int mtype; 226 227 mutex_enter(&mntid_lock); 228 mtype = makefstype(mp->mnt_op->vfs_name); 229 mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0); 230 mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype; 231 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 232 if (xxxfs_mntid == 0) 233 ++xxxfs_mntid; 234 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid); 235 tfsid.__fsid_val[1] = mtype; 236 while (vfs_getvfs(&tfsid)) { 237 tfsid.__fsid_val[0]++; 238 xxxfs_mntid++; 239 } 240 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0]; 241 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 242 mutex_exit(&mntid_lock); 243 } 244 245 /* 246 * Lookup a mount point by filesystem identifier. 247 * 248 * XXX Needs to add a reference to the mount point. 249 */ 250 struct mount * 251 vfs_getvfs(fsid_t *fsid) 252 { 253 mount_iterator_t *iter; 254 struct mount *mp; 255 256 mountlist_iterator_init(&iter); 257 while ((mp = mountlist_iterator_next(iter)) != NULL) { 258 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] && 259 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) { 260 mountlist_iterator_destroy(iter); 261 return mp; 262 } 263 } 264 mountlist_iterator_destroy(iter); 265 return NULL; 266 } 267 268 /* 269 * Take a reference to a mount structure. 270 */ 271 void 272 vfs_ref(struct mount *mp) 273 { 274 275 KASSERT(mp->mnt_refcnt > 0 || mutex_owned(&mountlist_lock)); 276 277 atomic_inc_uint(&mp->mnt_refcnt); 278 } 279 280 /* 281 * Drop a reference to a mount structure, freeing if the last reference. 282 */ 283 void 284 vfs_rele(struct mount *mp) 285 { 286 287 if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) { 288 return; 289 } 290 291 /* 292 * Nothing else has visibility of the mount: we can now 293 * free the data structures. 294 */ 295 KASSERT(mp->mnt_refcnt == 0); 296 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 297 mutex_destroy(&mp->mnt_updating); 298 mutex_destroy(&mp->mnt_renamelock); 299 if (mp->mnt_op != NULL) { 300 vfs_delref(mp->mnt_op); 301 } 302 kmem_free(mp, sizeof(*mp)); 303 } 304 305 /* 306 * Mark a mount point as busy, and gain a new reference to it. Used to 307 * prevent the file system from being unmounted during critical sections. 308 * 309 * vfs_busy can be called multiple times and by multiple threads 310 * and must be accompanied by the same number of vfs_unbusy calls. 311 * 312 * => The caller must hold a pre-existing reference to the mount. 313 * => Will fail if the file system is being unmounted, or is unmounted. 314 */ 315 static inline int 316 _vfs_busy(struct mount *mp, bool wait) 317 { 318 319 KASSERT(mp->mnt_refcnt > 0); 320 321 if (wait) { 322 fstrans_start(mp); 323 } else { 324 if (fstrans_start_nowait(mp)) 325 return EBUSY; 326 } 327 if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) { 328 fstrans_done(mp); 329 return ENOENT; 330 } 331 vfs_ref(mp); 332 return 0; 333 } 334 335 int 336 vfs_busy(struct mount *mp) 337 { 338 339 return _vfs_busy(mp, true); 340 } 341 342 int 343 vfs_trybusy(struct mount *mp) 344 { 345 346 return _vfs_busy(mp, false); 347 } 348 349 /* 350 * Unbusy a busy filesystem. 351 * 352 * Every successful vfs_busy() call must be undone by a vfs_unbusy() call. 353 */ 354 void 355 vfs_unbusy(struct mount *mp) 356 { 357 358 KASSERT(mp->mnt_refcnt > 0); 359 360 fstrans_done(mp); 361 vfs_rele(mp); 362 } 363 364 struct vnode_iterator { 365 vnode_impl_t vi_vnode; 366 }; 367 368 void 369 vfs_vnode_iterator_init(struct mount *mp, struct vnode_iterator **vnip) 370 { 371 vnode_t *vp; 372 vnode_impl_t *vip; 373 374 vp = vnalloc_marker(mp); 375 vip = VNODE_TO_VIMPL(vp); 376 377 mutex_enter(&mntvnode_lock); 378 TAILQ_INSERT_HEAD(&mp->mnt_vnodelist, vip, vi_mntvnodes); 379 vp->v_usecount = 1; 380 mutex_exit(&mntvnode_lock); 381 382 *vnip = (struct vnode_iterator *)vip; 383 } 384 385 void 386 vfs_vnode_iterator_destroy(struct vnode_iterator *vni) 387 { 388 vnode_impl_t *mvip = &vni->vi_vnode; 389 vnode_t *mvp = VIMPL_TO_VNODE(mvip); 390 391 mutex_enter(&mntvnode_lock); 392 KASSERT(vnis_marker(mvp)); 393 if (mvp->v_usecount != 0) { 394 TAILQ_REMOVE(&mvp->v_mount->mnt_vnodelist, mvip, vi_mntvnodes); 395 mvp->v_usecount = 0; 396 } 397 mutex_exit(&mntvnode_lock); 398 vnfree_marker(mvp); 399 } 400 401 static struct vnode * 402 vfs_vnode_iterator_next1(struct vnode_iterator *vni, 403 bool (*f)(void *, struct vnode *), void *cl, bool do_wait) 404 { 405 vnode_impl_t *mvip = &vni->vi_vnode; 406 struct mount *mp = VIMPL_TO_VNODE(mvip)->v_mount; 407 vnode_t *vp; 408 vnode_impl_t *vip; 409 int error; 410 411 KASSERT(vnis_marker(VIMPL_TO_VNODE(mvip))); 412 413 do { 414 mutex_enter(&mntvnode_lock); 415 vip = TAILQ_NEXT(mvip, vi_mntvnodes); 416 TAILQ_REMOVE(&mp->mnt_vnodelist, mvip, vi_mntvnodes); 417 VIMPL_TO_VNODE(mvip)->v_usecount = 0; 418 again: 419 vp = VIMPL_TO_VNODE(vip); 420 if (vp == NULL) { 421 mutex_exit(&mntvnode_lock); 422 return NULL; 423 } 424 mutex_enter(vp->v_interlock); 425 if (vnis_marker(vp) || 426 vdead_check(vp, (do_wait ? 0 : VDEAD_NOWAIT)) || 427 (f && !(*f)(cl, vp))) { 428 mutex_exit(vp->v_interlock); 429 vip = TAILQ_NEXT(vip, vi_mntvnodes); 430 goto again; 431 } 432 433 TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vip, mvip, vi_mntvnodes); 434 VIMPL_TO_VNODE(mvip)->v_usecount = 1; 435 mutex_exit(&mntvnode_lock); 436 error = vcache_vget(vp); 437 KASSERT(error == 0 || error == ENOENT); 438 } while (error != 0); 439 440 return vp; 441 } 442 443 struct vnode * 444 vfs_vnode_iterator_next(struct vnode_iterator *vni, 445 bool (*f)(void *, struct vnode *), void *cl) 446 { 447 448 return vfs_vnode_iterator_next1(vni, f, cl, false); 449 } 450 451 /* 452 * Move a vnode from one mount queue to another. 453 */ 454 void 455 vfs_insmntque(vnode_t *vp, struct mount *mp) 456 { 457 vnode_impl_t *vip = VNODE_TO_VIMPL(vp); 458 struct mount *omp; 459 460 KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 || 461 vp->v_tag == VT_VFS); 462 463 mutex_enter(&mntvnode_lock); 464 /* 465 * Delete from old mount point vnode list, if on one. 466 */ 467 if ((omp = vp->v_mount) != NULL) 468 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vip, vi_mntvnodes); 469 /* 470 * Insert into list of vnodes for the new mount point, if 471 * available. The caller must take a reference on the mount 472 * structure and donate to the vnode. 473 */ 474 if ((vp->v_mount = mp) != NULL) 475 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vip, vi_mntvnodes); 476 mutex_exit(&mntvnode_lock); 477 478 if (omp != NULL) { 479 /* Release reference to old mount. */ 480 vfs_rele(omp); 481 } 482 } 483 484 /* 485 * Remove any vnodes in the vnode table belonging to mount point mp. 486 * 487 * If FORCECLOSE is not specified, there should not be any active ones, 488 * return error if any are found (nb: this is a user error, not a 489 * system error). If FORCECLOSE is specified, detach any active vnodes 490 * that are found. 491 * 492 * If WRITECLOSE is set, only flush out regular file vnodes open for 493 * writing. 494 * 495 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 496 */ 497 #ifdef DEBUG 498 int busyprt = 0; /* print out busy vnodes */ 499 struct ctldebug debug1 = { "busyprt", &busyprt }; 500 #endif 501 502 static vnode_t * 503 vflushnext(struct vnode_iterator *marker, int *when) 504 { 505 if (hardclock_ticks > *when) { 506 yield(); 507 *when = hardclock_ticks + hz / 10; 508 } 509 return vfs_vnode_iterator_next1(marker, NULL, NULL, true); 510 } 511 512 /* 513 * Flush one vnode. Referenced on entry, unreferenced on return. 514 */ 515 static int 516 vflush_one(vnode_t *vp, vnode_t *skipvp, int flags) 517 { 518 int error; 519 struct vattr vattr; 520 521 if (vp == skipvp || 522 ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM))) { 523 vrele(vp); 524 return 0; 525 } 526 /* 527 * If WRITECLOSE is set, only flush out regular file 528 * vnodes open for writing or open and unlinked. 529 */ 530 if ((flags & WRITECLOSE)) { 531 if (vp->v_type != VREG) { 532 vrele(vp); 533 return 0; 534 } 535 error = vn_lock(vp, LK_EXCLUSIVE); 536 if (error) { 537 KASSERT(error == ENOENT); 538 vrele(vp); 539 return 0; 540 } 541 error = VOP_FSYNC(vp, curlwp->l_cred, FSYNC_WAIT, 0, 0); 542 if (error == 0) 543 error = VOP_GETATTR(vp, &vattr, curlwp->l_cred); 544 VOP_UNLOCK(vp); 545 if (error) { 546 vrele(vp); 547 return error; 548 } 549 if (vp->v_writecount == 0 && vattr.va_nlink > 0) { 550 vrele(vp); 551 return 0; 552 } 553 } 554 /* 555 * First try to recycle the vnode. 556 */ 557 if (vrecycle(vp)) 558 return 0; 559 /* 560 * If FORCECLOSE is set, forcibly close the vnode. 561 * For block or character devices, revert to an 562 * anonymous device. For all other files, just 563 * kill them. 564 */ 565 if (flags & FORCECLOSE) { 566 if (vp->v_usecount > 1 && 567 (vp->v_type == VBLK || vp->v_type == VCHR)) 568 vcache_make_anon(vp); 569 else 570 vgone(vp); 571 return 0; 572 } 573 vrele(vp); 574 return EBUSY; 575 } 576 577 int 578 vflush(struct mount *mp, vnode_t *skipvp, int flags) 579 { 580 vnode_t *vp; 581 struct vnode_iterator *marker; 582 int busy, error, when, retries = 2; 583 584 do { 585 busy = error = when = 0; 586 587 /* 588 * First, flush out any vnode references from the 589 * deferred vrele list. 590 */ 591 vrele_flush(mp); 592 593 vfs_vnode_iterator_init(mp, &marker); 594 595 while ((vp = vflushnext(marker, &when)) != NULL) { 596 error = vflush_one(vp, skipvp, flags); 597 if (error == EBUSY) { 598 error = 0; 599 busy++; 600 #ifdef DEBUG 601 if (busyprt && retries == 0) 602 vprint("vflush: busy vnode", vp); 603 #endif 604 } else if (error != 0) { 605 break; 606 } 607 } 608 609 vfs_vnode_iterator_destroy(marker); 610 } while (error == 0 && busy > 0 && retries-- > 0); 611 612 if (error) 613 return error; 614 if (busy) 615 return EBUSY; 616 return 0; 617 } 618 619 /* 620 * Mount a file system. 621 */ 622 623 /* 624 * Scan all active processes to see if any of them have a current or root 625 * directory onto which the new filesystem has just been mounted. If so, 626 * replace them with the new mount point. 627 */ 628 static void 629 mount_checkdirs(vnode_t *olddp) 630 { 631 vnode_t *newdp, *rele1, *rele2; 632 struct cwdinfo *cwdi; 633 struct proc *p; 634 bool retry; 635 636 if (olddp->v_usecount == 1) { 637 return; 638 } 639 if (VFS_ROOT(olddp->v_mountedhere, &newdp)) 640 panic("mount: lost mount"); 641 642 do { 643 retry = false; 644 mutex_enter(proc_lock); 645 PROCLIST_FOREACH(p, &allproc) { 646 if ((cwdi = p->p_cwdi) == NULL) 647 continue; 648 /* 649 * Cannot change to the old directory any more, 650 * so even if we see a stale value it is not a 651 * problem. 652 */ 653 if (cwdi->cwdi_cdir != olddp && 654 cwdi->cwdi_rdir != olddp) 655 continue; 656 retry = true; 657 rele1 = NULL; 658 rele2 = NULL; 659 atomic_inc_uint(&cwdi->cwdi_refcnt); 660 mutex_exit(proc_lock); 661 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 662 if (cwdi->cwdi_cdir == olddp) { 663 rele1 = cwdi->cwdi_cdir; 664 vref(newdp); 665 cwdi->cwdi_cdir = newdp; 666 } 667 if (cwdi->cwdi_rdir == olddp) { 668 rele2 = cwdi->cwdi_rdir; 669 vref(newdp); 670 cwdi->cwdi_rdir = newdp; 671 } 672 rw_exit(&cwdi->cwdi_lock); 673 cwdfree(cwdi); 674 if (rele1 != NULL) 675 vrele(rele1); 676 if (rele2 != NULL) 677 vrele(rele2); 678 mutex_enter(proc_lock); 679 break; 680 } 681 mutex_exit(proc_lock); 682 } while (retry); 683 684 if (rootvnode == olddp) { 685 vrele(rootvnode); 686 vref(newdp); 687 rootvnode = newdp; 688 } 689 vput(newdp); 690 } 691 692 /* 693 * Start extended attributes 694 */ 695 static int 696 start_extattr(struct mount *mp) 697 { 698 int error; 699 700 error = VFS_EXTATTRCTL(mp, EXTATTR_CMD_START, NULL, 0, NULL); 701 if (error) 702 printf("%s: failed to start extattr: error = %d\n", 703 mp->mnt_stat.f_mntonname, error); 704 705 return error; 706 } 707 708 int 709 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops, 710 const char *path, int flags, void *data, size_t *data_len) 711 { 712 vnode_t *vp = *vpp; 713 struct mount *mp; 714 struct pathbuf *pb; 715 struct nameidata nd; 716 int error; 717 718 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 719 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data); 720 if (error) { 721 vfs_delref(vfsops); 722 return error; 723 } 724 725 /* Cannot make a non-dir a mount-point (from here anyway). */ 726 if (vp->v_type != VDIR) { 727 vfs_delref(vfsops); 728 return ENOTDIR; 729 } 730 731 if (flags & MNT_EXPORTED) { 732 vfs_delref(vfsops); 733 return EINVAL; 734 } 735 736 if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) { 737 vfs_delref(vfsops); 738 return ENOMEM; 739 } 740 741 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred); 742 743 /* 744 * The underlying file system may refuse the mount for 745 * various reasons. Allow the user to force it to happen. 746 * 747 * Set the mount level flags. 748 */ 749 mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE); 750 751 mutex_enter(&mp->mnt_updating); 752 error = VFS_MOUNT(mp, path, data, data_len); 753 mp->mnt_flag &= ~MNT_OP_FLAGS; 754 755 if (error != 0) 756 goto err_unmounted; 757 758 /* 759 * Validate and prepare the mount point. 760 */ 761 error = pathbuf_copyin(path, &pb); 762 if (error != 0) { 763 goto err_mounted; 764 } 765 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 766 error = namei(&nd); 767 pathbuf_destroy(pb); 768 if (error != 0) { 769 goto err_mounted; 770 } 771 if (nd.ni_vp != vp) { 772 vput(nd.ni_vp); 773 error = EINVAL; 774 goto err_mounted; 775 } 776 if (vp->v_mountedhere != NULL) { 777 vput(nd.ni_vp); 778 error = EBUSY; 779 goto err_mounted; 780 } 781 error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0); 782 if (error != 0) { 783 vput(nd.ni_vp); 784 goto err_mounted; 785 } 786 787 /* 788 * Put the new filesystem on the mount list after root. 789 */ 790 cache_purge(vp); 791 mp->mnt_iflag &= ~IMNT_WANTRDWR; 792 793 mountlist_append(mp); 794 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 795 vfs_syncer_add_to_worklist(mp); 796 vp->v_mountedhere = mp; 797 vput(nd.ni_vp); 798 799 mount_checkdirs(vp); 800 mutex_exit(&mp->mnt_updating); 801 802 /* Hold an additional reference to the mount across VFS_START(). */ 803 vfs_ref(mp); 804 (void) VFS_STATVFS(mp, &mp->mnt_stat); 805 error = VFS_START(mp, 0); 806 if (error) { 807 vrele(vp); 808 } else if (flags & MNT_EXTATTR) { 809 (void)start_extattr(mp); 810 } 811 /* Drop reference held for VFS_START(). */ 812 vfs_rele(mp); 813 *vpp = NULL; 814 return error; 815 816 err_mounted: 817 if (VFS_UNMOUNT(mp, MNT_FORCE) != 0) 818 panic("Unmounting fresh file system failed"); 819 820 err_unmounted: 821 vp->v_mountedhere = NULL; 822 mutex_exit(&mp->mnt_updating); 823 fstrans_unmount(mp); 824 vfs_rele(mp); 825 826 return error; 827 } 828 829 /* 830 * Do the actual file system unmount. File system is assumed to have 831 * been locked by the caller. 832 * 833 * => Caller hold reference to the mount, explicitly for dounmount(). 834 */ 835 int 836 dounmount(struct mount *mp, int flags, struct lwp *l) 837 { 838 mount_iterator_t *iter; 839 struct mount *cmp; 840 vnode_t *coveredvp; 841 int error, async, used_syncer, used_extattr; 842 843 #if NVERIEXEC > 0 844 error = veriexec_unmountchk(mp); 845 if (error) 846 return (error); 847 #endif /* NVERIEXEC > 0 */ 848 849 /* 850 * No unmount below layered mounts. 851 */ 852 mountlist_iterator_init(&iter); 853 while ((cmp = mountlist_iterator_next(iter)) != NULL) { 854 if (cmp->mnt_lower == mp) { 855 mountlist_iterator_destroy(iter); 856 return EBUSY; 857 } 858 } 859 mountlist_iterator_destroy(iter); 860 861 error = vfs_suspend(mp, 0); 862 if (error) { 863 return error; 864 } 865 866 KASSERT((mp->mnt_iflag & IMNT_GONE) == 0); 867 868 used_syncer = (mp->mnt_iflag & IMNT_ONWORKLIST) != 0; 869 used_extattr = mp->mnt_flag & MNT_EXTATTR; 870 871 mp->mnt_iflag |= IMNT_UNMOUNT; 872 mutex_enter(&mp->mnt_updating); 873 async = mp->mnt_flag & MNT_ASYNC; 874 mp->mnt_flag &= ~MNT_ASYNC; 875 cache_purgevfs(mp); /* remove cache entries for this file sys */ 876 if (used_syncer) 877 vfs_syncer_remove_from_worklist(mp); 878 error = 0; 879 if (((mp->mnt_flag & MNT_RDONLY) == 0) && ((flags & MNT_FORCE) == 0)) { 880 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred); 881 } 882 if (error == 0 || (flags & MNT_FORCE)) { 883 error = VFS_UNMOUNT(mp, flags); 884 } 885 if (error) { 886 mp->mnt_iflag &= ~IMNT_UNMOUNT; 887 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 888 vfs_syncer_add_to_worklist(mp); 889 mp->mnt_flag |= async; 890 mutex_exit(&mp->mnt_updating); 891 vfs_resume(mp); 892 if (used_extattr) { 893 if (start_extattr(mp) != 0) 894 mp->mnt_flag &= ~MNT_EXTATTR; 895 else 896 mp->mnt_flag |= MNT_EXTATTR; 897 } 898 return (error); 899 } 900 mutex_exit(&mp->mnt_updating); 901 902 /* 903 * mark filesystem as gone to prevent further umounts 904 * after mnt_umounting lock is gone, this also prevents 905 * vfs_busy() from succeeding. 906 */ 907 mp->mnt_iflag |= IMNT_GONE; 908 vfs_resume(mp); 909 910 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) { 911 vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY); 912 coveredvp->v_mountedhere = NULL; 913 VOP_UNLOCK(coveredvp); 914 } 915 mountlist_remove(mp); 916 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL) 917 panic("unmount: dangling vnode"); 918 vfs_hooks_unmount(mp); 919 920 fstrans_unmount(mp); 921 vfs_rele(mp); /* reference from mount() */ 922 if (coveredvp != NULLVP) { 923 vrele(coveredvp); 924 } 925 return (0); 926 } 927 928 /* 929 * Unmount all file systems. 930 * We traverse the list in reverse order under the assumption that doing so 931 * will avoid needing to worry about dependencies. 932 */ 933 bool 934 vfs_unmountall(struct lwp *l) 935 { 936 937 printf("unmounting file systems...\n"); 938 return vfs_unmountall1(l, true, true); 939 } 940 941 static void 942 vfs_unmount_print(struct mount *mp, const char *pfx) 943 { 944 945 aprint_verbose("%sunmounted %s on %s type %s\n", pfx, 946 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname, 947 mp->mnt_stat.f_fstypename); 948 } 949 950 /* 951 * Return the mount with the highest generation less than "gen". 952 */ 953 static struct mount * 954 vfs_unmount_next(uint64_t gen) 955 { 956 mount_iterator_t *iter; 957 struct mount *mp, *nmp; 958 959 nmp = NULL; 960 961 mountlist_iterator_init(&iter); 962 while ((mp = mountlist_iterator_next(iter)) != NULL) { 963 if ((nmp == NULL || mp->mnt_gen > nmp->mnt_gen) && 964 mp->mnt_gen < gen) { 965 if (nmp != NULL) 966 vfs_rele(nmp); 967 nmp = mp; 968 vfs_ref(nmp); 969 } 970 } 971 mountlist_iterator_destroy(iter); 972 973 return nmp; 974 } 975 976 bool 977 vfs_unmount_forceone(struct lwp *l) 978 { 979 struct mount *mp; 980 int error; 981 982 mp = vfs_unmount_next(mountgen); 983 if (mp == NULL) { 984 return false; 985 } 986 987 #ifdef DEBUG 988 printf("forcefully unmounting %s (%s)...\n", 989 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 990 #endif 991 if ((error = dounmount(mp, MNT_FORCE, l)) == 0) { 992 vfs_unmount_print(mp, "forcefully "); 993 return true; 994 } else { 995 vfs_rele(mp); 996 } 997 998 #ifdef DEBUG 999 printf("forceful unmount of %s failed with error %d\n", 1000 mp->mnt_stat.f_mntonname, error); 1001 #endif 1002 1003 return false; 1004 } 1005 1006 bool 1007 vfs_unmountall1(struct lwp *l, bool force, bool verbose) 1008 { 1009 struct mount *mp; 1010 bool any_error = false, progress = false; 1011 uint64_t gen; 1012 int error; 1013 1014 gen = mountgen; 1015 for (;;) { 1016 mp = vfs_unmount_next(gen); 1017 if (mp == NULL) 1018 break; 1019 gen = mp->mnt_gen; 1020 1021 #ifdef DEBUG 1022 printf("unmounting %p %s (%s)...\n", 1023 (void *)mp, mp->mnt_stat.f_mntonname, 1024 mp->mnt_stat.f_mntfromname); 1025 #endif 1026 if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) { 1027 vfs_unmount_print(mp, ""); 1028 progress = true; 1029 } else { 1030 vfs_rele(mp); 1031 if (verbose) { 1032 printf("unmount of %s failed with error %d\n", 1033 mp->mnt_stat.f_mntonname, error); 1034 } 1035 any_error = true; 1036 } 1037 } 1038 if (verbose) { 1039 printf("unmounting done\n"); 1040 } 1041 if (any_error && verbose) { 1042 printf("WARNING: some file systems would not unmount\n"); 1043 } 1044 return progress; 1045 } 1046 1047 void 1048 vfs_sync_all(struct lwp *l) 1049 { 1050 printf("syncing disks... "); 1051 1052 /* remove user processes from run queue */ 1053 suspendsched(); 1054 (void)spl0(); 1055 1056 /* avoid coming back this way again if we panic. */ 1057 doing_shutdown = 1; 1058 1059 do_sys_sync(l); 1060 1061 /* Wait for sync to finish. */ 1062 if (buf_syncwait() != 0) { 1063 #if defined(DDB) && defined(DEBUG_HALT_BUSY) 1064 Debugger(); 1065 #endif 1066 printf("giving up\n"); 1067 return; 1068 } else 1069 printf("done\n"); 1070 } 1071 1072 /* 1073 * Sync and unmount file systems before shutting down. 1074 */ 1075 void 1076 vfs_shutdown(void) 1077 { 1078 lwp_t *l = curlwp; 1079 1080 vfs_sync_all(l); 1081 1082 /* 1083 * If we have paniced - do not make the situation potentially 1084 * worse by unmounting the file systems. 1085 */ 1086 if (panicstr != NULL) { 1087 return; 1088 } 1089 1090 /* Unmount file systems. */ 1091 vfs_unmountall(l); 1092 } 1093 1094 /* 1095 * Print a list of supported file system types (used by vfs_mountroot) 1096 */ 1097 static void 1098 vfs_print_fstypes(void) 1099 { 1100 struct vfsops *v; 1101 int cnt = 0; 1102 1103 mutex_enter(&vfs_list_lock); 1104 LIST_FOREACH(v, &vfs_list, vfs_list) 1105 ++cnt; 1106 mutex_exit(&vfs_list_lock); 1107 1108 if (cnt == 0) { 1109 printf("WARNING: No file system modules have been loaded.\n"); 1110 return; 1111 } 1112 1113 printf("Supported file systems:"); 1114 mutex_enter(&vfs_list_lock); 1115 LIST_FOREACH(v, &vfs_list, vfs_list) { 1116 printf(" %s", v->vfs_name); 1117 } 1118 mutex_exit(&vfs_list_lock); 1119 printf("\n"); 1120 } 1121 1122 /* 1123 * Mount the root file system. If the operator didn't specify a 1124 * file system to use, try all possible file systems until one 1125 * succeeds. 1126 */ 1127 int 1128 vfs_mountroot(void) 1129 { 1130 struct vfsops *v; 1131 int error = ENODEV; 1132 1133 if (root_device == NULL) 1134 panic("vfs_mountroot: root device unknown"); 1135 1136 switch (device_class(root_device)) { 1137 case DV_IFNET: 1138 if (rootdev != NODEV) 1139 panic("vfs_mountroot: rootdev set for DV_IFNET " 1140 "(0x%llx -> %llu,%llu)", 1141 (unsigned long long)rootdev, 1142 (unsigned long long)major(rootdev), 1143 (unsigned long long)minor(rootdev)); 1144 break; 1145 1146 case DV_DISK: 1147 if (rootdev == NODEV) 1148 panic("vfs_mountroot: rootdev not set for DV_DISK"); 1149 if (bdevvp(rootdev, &rootvp)) 1150 panic("vfs_mountroot: can't get vnode for rootdev"); 1151 error = VOP_OPEN(rootvp, FREAD, FSCRED); 1152 if (error) { 1153 printf("vfs_mountroot: can't open root device\n"); 1154 return (error); 1155 } 1156 break; 1157 1158 case DV_VIRTUAL: 1159 break; 1160 1161 default: 1162 printf("%s: inappropriate for root file system\n", 1163 device_xname(root_device)); 1164 return (ENODEV); 1165 } 1166 1167 /* 1168 * If user specified a root fs type, use it. Make sure the 1169 * specified type exists and has a mount_root() 1170 */ 1171 if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) { 1172 v = vfs_getopsbyname(rootfstype); 1173 error = EFTYPE; 1174 if (v != NULL) { 1175 if (v->vfs_mountroot != NULL) { 1176 error = (v->vfs_mountroot)(); 1177 } 1178 v->vfs_refcount--; 1179 } 1180 goto done; 1181 } 1182 1183 /* 1184 * Try each file system currently configured into the kernel. 1185 */ 1186 mutex_enter(&vfs_list_lock); 1187 LIST_FOREACH(v, &vfs_list, vfs_list) { 1188 if (v->vfs_mountroot == NULL) 1189 continue; 1190 #ifdef DEBUG 1191 aprint_normal("mountroot: trying %s...\n", v->vfs_name); 1192 #endif 1193 v->vfs_refcount++; 1194 mutex_exit(&vfs_list_lock); 1195 error = (*v->vfs_mountroot)(); 1196 mutex_enter(&vfs_list_lock); 1197 v->vfs_refcount--; 1198 if (!error) { 1199 aprint_normal("root file system type: %s\n", 1200 v->vfs_name); 1201 break; 1202 } 1203 } 1204 mutex_exit(&vfs_list_lock); 1205 1206 if (v == NULL) { 1207 vfs_print_fstypes(); 1208 printf("no file system for %s", device_xname(root_device)); 1209 if (device_class(root_device) == DV_DISK) 1210 printf(" (dev 0x%llx)", (unsigned long long)rootdev); 1211 printf("\n"); 1212 error = EFTYPE; 1213 } 1214 1215 done: 1216 if (error && device_class(root_device) == DV_DISK) { 1217 VOP_CLOSE(rootvp, FREAD, FSCRED); 1218 vrele(rootvp); 1219 } 1220 if (error == 0) { 1221 mount_iterator_t *iter; 1222 struct mount *mp; 1223 extern struct cwdinfo cwdi0; 1224 1225 mountlist_iterator_init(&iter); 1226 mp = mountlist_iterator_next(iter); 1227 KASSERT(mp != NULL); 1228 mountlist_iterator_destroy(iter); 1229 1230 mp->mnt_flag |= MNT_ROOTFS; 1231 mp->mnt_op->vfs_refcount++; 1232 1233 /* 1234 * Get the vnode for '/'. Set cwdi0.cwdi_cdir to 1235 * reference it. 1236 */ 1237 error = VFS_ROOT(mp, &rootvnode); 1238 if (error) 1239 panic("cannot find root vnode, error=%d", error); 1240 cwdi0.cwdi_cdir = rootvnode; 1241 vref(cwdi0.cwdi_cdir); 1242 VOP_UNLOCK(rootvnode); 1243 cwdi0.cwdi_rdir = NULL; 1244 1245 /* 1246 * Now that root is mounted, we can fixup initproc's CWD 1247 * info. All other processes are kthreads, which merely 1248 * share proc0's CWD info. 1249 */ 1250 initproc->p_cwdi->cwdi_cdir = rootvnode; 1251 vref(initproc->p_cwdi->cwdi_cdir); 1252 initproc->p_cwdi->cwdi_rdir = NULL; 1253 /* 1254 * Enable loading of modules from the filesystem 1255 */ 1256 module_load_vfs_init(); 1257 1258 } 1259 return (error); 1260 } 1261 1262 /* 1263 * mount_specific_key_create -- 1264 * Create a key for subsystem mount-specific data. 1265 */ 1266 int 1267 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 1268 { 1269 1270 return specificdata_key_create(mount_specificdata_domain, keyp, dtor); 1271 } 1272 1273 /* 1274 * mount_specific_key_delete -- 1275 * Delete a key for subsystem mount-specific data. 1276 */ 1277 void 1278 mount_specific_key_delete(specificdata_key_t key) 1279 { 1280 1281 specificdata_key_delete(mount_specificdata_domain, key); 1282 } 1283 1284 /* 1285 * mount_initspecific -- 1286 * Initialize a mount's specificdata container. 1287 */ 1288 void 1289 mount_initspecific(struct mount *mp) 1290 { 1291 int error __diagused; 1292 1293 error = specificdata_init(mount_specificdata_domain, 1294 &mp->mnt_specdataref); 1295 KASSERT(error == 0); 1296 } 1297 1298 /* 1299 * mount_finispecific -- 1300 * Finalize a mount's specificdata container. 1301 */ 1302 void 1303 mount_finispecific(struct mount *mp) 1304 { 1305 1306 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 1307 } 1308 1309 /* 1310 * mount_getspecific -- 1311 * Return mount-specific data corresponding to the specified key. 1312 */ 1313 void * 1314 mount_getspecific(struct mount *mp, specificdata_key_t key) 1315 { 1316 1317 return specificdata_getspecific(mount_specificdata_domain, 1318 &mp->mnt_specdataref, key); 1319 } 1320 1321 /* 1322 * mount_setspecific -- 1323 * Set mount-specific data corresponding to the specified key. 1324 */ 1325 void 1326 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data) 1327 { 1328 1329 specificdata_setspecific(mount_specificdata_domain, 1330 &mp->mnt_specdataref, key, data); 1331 } 1332 1333 /* 1334 * Check to see if a filesystem is mounted on a block device. 1335 */ 1336 int 1337 vfs_mountedon(vnode_t *vp) 1338 { 1339 vnode_t *vq; 1340 int error = 0; 1341 1342 if (vp->v_type != VBLK) 1343 return ENOTBLK; 1344 if (spec_node_getmountedfs(vp) != NULL) 1345 return EBUSY; 1346 if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) { 1347 if (spec_node_getmountedfs(vq) != NULL) 1348 error = EBUSY; 1349 vrele(vq); 1350 } 1351 1352 return error; 1353 } 1354 1355 /* 1356 * Check if a device pointed to by vp is mounted. 1357 * 1358 * Returns: 1359 * EINVAL if it's not a disk 1360 * EBUSY if it's a disk and mounted 1361 * 0 if it's a disk and not mounted 1362 */ 1363 int 1364 rawdev_mounted(vnode_t *vp, vnode_t **bvpp) 1365 { 1366 vnode_t *bvp; 1367 dev_t dev; 1368 int d_type; 1369 1370 bvp = NULL; 1371 d_type = D_OTHER; 1372 1373 if (iskmemvp(vp)) 1374 return EINVAL; 1375 1376 switch (vp->v_type) { 1377 case VCHR: { 1378 const struct cdevsw *cdev; 1379 1380 dev = vp->v_rdev; 1381 cdev = cdevsw_lookup(dev); 1382 if (cdev != NULL) { 1383 dev_t blkdev; 1384 1385 blkdev = devsw_chr2blk(dev); 1386 if (blkdev != NODEV) { 1387 if (vfinddev(blkdev, VBLK, &bvp) != 0) { 1388 d_type = (cdev->d_flag & D_TYPEMASK); 1389 /* XXX: what if bvp disappears? */ 1390 vrele(bvp); 1391 } 1392 } 1393 } 1394 1395 break; 1396 } 1397 1398 case VBLK: { 1399 const struct bdevsw *bdev; 1400 1401 dev = vp->v_rdev; 1402 bdev = bdevsw_lookup(dev); 1403 if (bdev != NULL) 1404 d_type = (bdev->d_flag & D_TYPEMASK); 1405 1406 bvp = vp; 1407 1408 break; 1409 } 1410 1411 default: 1412 break; 1413 } 1414 1415 if (d_type != D_DISK) 1416 return EINVAL; 1417 1418 if (bvpp != NULL) 1419 *bvpp = bvp; 1420 1421 /* 1422 * XXX: This is bogus. We should be failing the request 1423 * XXX: not only if this specific slice is mounted, but 1424 * XXX: if it's on a disk with any other mounted slice. 1425 */ 1426 if (vfs_mountedon(bvp)) 1427 return EBUSY; 1428 1429 return 0; 1430 } 1431 1432 /* 1433 * Make a 'unique' number from a mount type name. 1434 */ 1435 long 1436 makefstype(const char *type) 1437 { 1438 long rv; 1439 1440 for (rv = 0; *type; type++) { 1441 rv <<= 2; 1442 rv ^= *type; 1443 } 1444 return rv; 1445 } 1446 1447 static struct mountlist_entry * 1448 mountlist_alloc(enum mountlist_type type, struct mount *mp) 1449 { 1450 struct mountlist_entry *me; 1451 1452 me = kmem_zalloc(sizeof(*me), KM_SLEEP); 1453 me->me_mount = mp; 1454 me->me_type = type; 1455 1456 return me; 1457 } 1458 1459 static void 1460 mountlist_free(struct mountlist_entry *me) 1461 { 1462 1463 kmem_free(me, sizeof(*me)); 1464 } 1465 1466 void 1467 mountlist_iterator_init(mount_iterator_t **mip) 1468 { 1469 struct mountlist_entry *me; 1470 1471 me = mountlist_alloc(ME_MARKER, NULL); 1472 mutex_enter(&mountlist_lock); 1473 TAILQ_INSERT_HEAD(&mountlist, me, me_list); 1474 mutex_exit(&mountlist_lock); 1475 *mip = (mount_iterator_t *)me; 1476 } 1477 1478 void 1479 mountlist_iterator_destroy(mount_iterator_t *mi) 1480 { 1481 struct mountlist_entry *marker = &mi->mi_entry; 1482 1483 if (marker->me_mount != NULL) 1484 vfs_unbusy(marker->me_mount); 1485 1486 mutex_enter(&mountlist_lock); 1487 TAILQ_REMOVE(&mountlist, marker, me_list); 1488 mutex_exit(&mountlist_lock); 1489 1490 mountlist_free(marker); 1491 1492 } 1493 1494 /* 1495 * Return the next mount or NULL for this iterator. 1496 * Mark it busy on success. 1497 */ 1498 static inline struct mount * 1499 _mountlist_iterator_next(mount_iterator_t *mi, bool wait) 1500 { 1501 struct mountlist_entry *me, *marker = &mi->mi_entry; 1502 struct mount *mp; 1503 int error; 1504 1505 if (marker->me_mount != NULL) { 1506 vfs_unbusy(marker->me_mount); 1507 marker->me_mount = NULL; 1508 } 1509 1510 mutex_enter(&mountlist_lock); 1511 for (;;) { 1512 KASSERT(marker->me_type == ME_MARKER); 1513 1514 me = TAILQ_NEXT(marker, me_list); 1515 if (me == NULL) { 1516 /* End of list: keep marker and return. */ 1517 mutex_exit(&mountlist_lock); 1518 return NULL; 1519 } 1520 TAILQ_REMOVE(&mountlist, marker, me_list); 1521 TAILQ_INSERT_AFTER(&mountlist, me, marker, me_list); 1522 1523 /* Skip other markers. */ 1524 if (me->me_type != ME_MOUNT) 1525 continue; 1526 1527 /* Take an initial reference for vfs_busy() below. */ 1528 mp = me->me_mount; 1529 KASSERT(mp != NULL); 1530 vfs_ref(mp); 1531 mutex_exit(&mountlist_lock); 1532 1533 /* Try to mark this mount busy and return on success. */ 1534 if (wait) 1535 error = vfs_busy(mp); 1536 else 1537 error = vfs_trybusy(mp); 1538 if (error == 0) { 1539 vfs_rele(mp); 1540 marker->me_mount = mp; 1541 return mp; 1542 } 1543 vfs_rele(mp); 1544 mutex_enter(&mountlist_lock); 1545 } 1546 } 1547 1548 struct mount * 1549 mountlist_iterator_next(mount_iterator_t *mi) 1550 { 1551 1552 return _mountlist_iterator_next(mi, true); 1553 } 1554 1555 struct mount * 1556 mountlist_iterator_trynext(mount_iterator_t *mi) 1557 { 1558 1559 return _mountlist_iterator_next(mi, false); 1560 } 1561 1562 /* 1563 * Attach new mount to the end of the mount list. 1564 */ 1565 void 1566 mountlist_append(struct mount *mp) 1567 { 1568 struct mountlist_entry *me; 1569 1570 me = mountlist_alloc(ME_MOUNT, mp); 1571 mutex_enter(&mountlist_lock); 1572 TAILQ_INSERT_TAIL(&mountlist, me, me_list); 1573 mutex_exit(&mountlist_lock); 1574 } 1575 1576 /* 1577 * Remove mount from mount list. 1578 */void 1579 mountlist_remove(struct mount *mp) 1580 { 1581 struct mountlist_entry *me; 1582 1583 mutex_enter(&mountlist_lock); 1584 TAILQ_FOREACH(me, &mountlist, me_list) 1585 if (me->me_type == ME_MOUNT && me->me_mount == mp) 1586 break; 1587 KASSERT(me != NULL); 1588 TAILQ_REMOVE(&mountlist, me, me_list); 1589 mutex_exit(&mountlist_lock); 1590 mountlist_free(me); 1591 } 1592 1593 /* 1594 * Unlocked variant to traverse the mountlist. 1595 * To be used from DDB only. 1596 */ 1597 struct mount * 1598 _mountlist_next(struct mount *mp) 1599 { 1600 struct mountlist_entry *me; 1601 1602 if (mp == NULL) { 1603 me = TAILQ_FIRST(&mountlist); 1604 } else { 1605 TAILQ_FOREACH(me, &mountlist, me_list) 1606 if (me->me_type == ME_MOUNT && me->me_mount == mp) 1607 break; 1608 if (me != NULL) 1609 me = TAILQ_NEXT(me, me_list); 1610 } 1611 1612 while (me != NULL && me->me_type != ME_MOUNT) 1613 me = TAILQ_NEXT(me, me_list); 1614 1615 return (me ? me->me_mount : NULL); 1616 } 1617