1 /* $NetBSD: vfs_mount.c,v 1.26 2014/02/27 13:00:06 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.26 2014/02/27 13:00:06 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/namei.h> 86 #include <sys/extattr.h> 87 #include <sys/syscallargs.h> 88 #include <sys/sysctl.h> 89 #include <sys/systm.h> 90 #include <sys/vfs_syscalls.h> 91 #include <sys/vnode.h> 92 93 #include <miscfs/genfs/genfs.h> 94 #include <miscfs/syncfs/syncfs.h> 95 #include <miscfs/specfs/specdev.h> 96 97 /* Root filesystem and device. */ 98 vnode_t * rootvnode; 99 device_t root_device; 100 101 /* Mounted filesystem list. */ 102 struct mntlist mountlist; 103 kmutex_t mountlist_lock; 104 105 kmutex_t mntvnode_lock; 106 kmutex_t vfs_list_lock; 107 108 static specificdata_domain_t mount_specificdata_domain; 109 static kmutex_t mntid_lock; 110 111 static kmutex_t mountgen_lock; 112 static uint64_t mountgen; 113 114 void 115 vfs_mount_sysinit(void) 116 { 117 118 TAILQ_INIT(&mountlist); 119 mutex_init(&mountlist_lock, MUTEX_DEFAULT, IPL_NONE); 120 mutex_init(&mntvnode_lock, MUTEX_DEFAULT, IPL_NONE); 121 mutex_init(&vfs_list_lock, MUTEX_DEFAULT, IPL_NONE); 122 123 mount_specificdata_domain = specificdata_domain_create(); 124 mutex_init(&mntid_lock, MUTEX_DEFAULT, IPL_NONE); 125 mutex_init(&mountgen_lock, MUTEX_DEFAULT, IPL_NONE); 126 mountgen = 0; 127 } 128 129 struct mount * 130 vfs_mountalloc(struct vfsops *vfsops, vnode_t *vp) 131 { 132 struct mount *mp; 133 int error __diagused; 134 135 mp = kmem_zalloc(sizeof(*mp), KM_SLEEP); 136 if (mp == NULL) 137 return NULL; 138 139 mp->mnt_op = vfsops; 140 mp->mnt_refcnt = 1; 141 TAILQ_INIT(&mp->mnt_vnodelist); 142 mutex_init(&mp->mnt_unmounting, MUTEX_DEFAULT, IPL_NONE); 143 mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE); 144 mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE); 145 error = vfs_busy(mp, NULL); 146 KASSERT(error == 0); 147 mp->mnt_vnodecovered = vp; 148 mount_initspecific(mp); 149 150 mutex_enter(&mountgen_lock); 151 mp->mnt_gen = mountgen++; 152 mutex_exit(&mountgen_lock); 153 154 return mp; 155 } 156 157 /* 158 * vfs_rootmountalloc: lookup a filesystem type, and if found allocate and 159 * initialize a mount structure for it. 160 * 161 * Devname is usually updated by mount(8) after booting. 162 */ 163 int 164 vfs_rootmountalloc(const char *fstypename, const char *devname, 165 struct mount **mpp) 166 { 167 struct vfsops *vfsp = NULL; 168 struct mount *mp; 169 170 mutex_enter(&vfs_list_lock); 171 LIST_FOREACH(vfsp, &vfs_list, vfs_list) 172 if (!strncmp(vfsp->vfs_name, fstypename, 173 sizeof(mp->mnt_stat.f_fstypename))) 174 break; 175 if (vfsp == NULL) { 176 mutex_exit(&vfs_list_lock); 177 return (ENODEV); 178 } 179 vfsp->vfs_refcount++; 180 mutex_exit(&vfs_list_lock); 181 182 if ((mp = vfs_mountalloc(vfsp, NULL)) == NULL) 183 return ENOMEM; 184 mp->mnt_flag = MNT_RDONLY; 185 (void)strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfs_name, 186 sizeof(mp->mnt_stat.f_fstypename)); 187 mp->mnt_stat.f_mntonname[0] = '/'; 188 mp->mnt_stat.f_mntonname[1] = '\0'; 189 mp->mnt_stat.f_mntfromname[sizeof(mp->mnt_stat.f_mntfromname) - 1] = 190 '\0'; 191 (void)copystr(devname, mp->mnt_stat.f_mntfromname, 192 sizeof(mp->mnt_stat.f_mntfromname) - 1, 0); 193 *mpp = mp; 194 return 0; 195 } 196 197 /* 198 * vfs_getnewfsid: get a new unique fsid. 199 */ 200 void 201 vfs_getnewfsid(struct mount *mp) 202 { 203 static u_short xxxfs_mntid; 204 fsid_t tfsid; 205 int mtype; 206 207 mutex_enter(&mntid_lock); 208 mtype = makefstype(mp->mnt_op->vfs_name); 209 mp->mnt_stat.f_fsidx.__fsid_val[0] = makedev(mtype, 0); 210 mp->mnt_stat.f_fsidx.__fsid_val[1] = mtype; 211 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 212 if (xxxfs_mntid == 0) 213 ++xxxfs_mntid; 214 tfsid.__fsid_val[0] = makedev(mtype & 0xff, xxxfs_mntid); 215 tfsid.__fsid_val[1] = mtype; 216 if (!TAILQ_EMPTY(&mountlist)) { 217 while (vfs_getvfs(&tfsid)) { 218 tfsid.__fsid_val[0]++; 219 xxxfs_mntid++; 220 } 221 } 222 mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0]; 223 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 224 mutex_exit(&mntid_lock); 225 } 226 227 /* 228 * Lookup a mount point by filesystem identifier. 229 * 230 * XXX Needs to add a reference to the mount point. 231 */ 232 struct mount * 233 vfs_getvfs(fsid_t *fsid) 234 { 235 struct mount *mp; 236 237 mutex_enter(&mountlist_lock); 238 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 239 if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] && 240 mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) { 241 mutex_exit(&mountlist_lock); 242 return (mp); 243 } 244 } 245 mutex_exit(&mountlist_lock); 246 return NULL; 247 } 248 249 /* 250 * Drop a reference to a mount structure, freeing if the last reference. 251 */ 252 void 253 vfs_destroy(struct mount *mp) 254 { 255 256 if (__predict_true((int)atomic_dec_uint_nv(&mp->mnt_refcnt) > 0)) { 257 return; 258 } 259 260 /* 261 * Nothing else has visibility of the mount: we can now 262 * free the data structures. 263 */ 264 KASSERT(mp->mnt_refcnt == 0); 265 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 266 mutex_destroy(&mp->mnt_unmounting); 267 mutex_destroy(&mp->mnt_updating); 268 mutex_destroy(&mp->mnt_renamelock); 269 if (mp->mnt_op != NULL) { 270 vfs_delref(mp->mnt_op); 271 } 272 kmem_free(mp, sizeof(*mp)); 273 } 274 275 /* 276 * Mark a mount point as busy, and gain a new reference to it. Used to 277 * prevent the file system from being unmounted during critical sections. 278 * 279 * vfs_busy can be called multiple times and by multiple threads 280 * and must be accompanied by the same number of vfs_unbusy calls. 281 * 282 * => The caller must hold a pre-existing reference to the mount. 283 * => Will fail if the file system is being unmounted, or is unmounted. 284 */ 285 int 286 vfs_busy(struct mount *mp, struct mount **nextp) 287 { 288 289 KASSERT(mp->mnt_refcnt > 0); 290 291 mutex_enter(&mp->mnt_unmounting); 292 if (__predict_false((mp->mnt_iflag & IMNT_GONE) != 0)) { 293 mutex_exit(&mp->mnt_unmounting); 294 if (nextp != NULL) { 295 KASSERT(mutex_owned(&mountlist_lock)); 296 *nextp = TAILQ_NEXT(mp, mnt_list); 297 } 298 return ENOENT; 299 } 300 ++mp->mnt_busynest; 301 KASSERT(mp->mnt_busynest != 0); 302 mutex_exit(&mp->mnt_unmounting); 303 if (nextp != NULL) { 304 mutex_exit(&mountlist_lock); 305 } 306 atomic_inc_uint(&mp->mnt_refcnt); 307 return 0; 308 } 309 310 /* 311 * Unbusy a busy filesystem. 312 * 313 * Every successful vfs_busy() call must be undone by a vfs_unbusy() call. 314 * 315 * => If keepref is true, preserve reference added by vfs_busy(). 316 * => If nextp != NULL, acquire mountlist_lock. 317 */ 318 void 319 vfs_unbusy(struct mount *mp, bool keepref, struct mount **nextp) 320 { 321 322 KASSERT(mp->mnt_refcnt > 0); 323 324 if (nextp != NULL) { 325 mutex_enter(&mountlist_lock); 326 } 327 mutex_enter(&mp->mnt_unmounting); 328 KASSERT(mp->mnt_busynest != 0); 329 mp->mnt_busynest--; 330 mutex_exit(&mp->mnt_unmounting); 331 if (!keepref) { 332 vfs_destroy(mp); 333 } 334 if (nextp != NULL) { 335 KASSERT(mutex_owned(&mountlist_lock)); 336 *nextp = TAILQ_NEXT(mp, mnt_list); 337 } 338 } 339 340 /* 341 * Insert a marker vnode into a mount's vnode list, after the 342 * specified vnode. mntvnode_lock must be held. 343 */ 344 void 345 vmark(vnode_t *mvp, vnode_t *vp) 346 { 347 struct mount *mp = mvp->v_mount; 348 349 KASSERT(mutex_owned(&mntvnode_lock)); 350 KASSERT((mvp->v_iflag & VI_MARKER) != 0); 351 KASSERT(vp->v_mount == mp); 352 353 TAILQ_INSERT_AFTER(&mp->mnt_vnodelist, vp, mvp, v_mntvnodes); 354 } 355 356 /* 357 * Remove a marker vnode from a mount's vnode list, and return 358 * a pointer to the next vnode in the list. mntvnode_lock must 359 * be held. 360 */ 361 vnode_t * 362 vunmark(vnode_t *mvp) 363 { 364 struct mount *mp = mvp->v_mount; 365 vnode_t *vp; 366 367 KASSERT(mutex_owned(&mntvnode_lock)); 368 KASSERT((mvp->v_iflag & VI_MARKER) != 0); 369 370 vp = TAILQ_NEXT(mvp, v_mntvnodes); 371 TAILQ_REMOVE(&mp->mnt_vnodelist, mvp, v_mntvnodes); 372 373 KASSERT(vp == NULL || vp->v_mount == mp); 374 375 return vp; 376 } 377 378 /* 379 * Move a vnode from one mount queue to another. 380 */ 381 void 382 vfs_insmntque(vnode_t *vp, struct mount *mp) 383 { 384 struct mount *omp; 385 386 KASSERT(mp == NULL || (mp->mnt_iflag & IMNT_UNMOUNT) == 0 || 387 vp->v_tag == VT_VFS); 388 389 mutex_enter(&mntvnode_lock); 390 /* 391 * Delete from old mount point vnode list, if on one. 392 */ 393 if ((omp = vp->v_mount) != NULL) 394 TAILQ_REMOVE(&vp->v_mount->mnt_vnodelist, vp, v_mntvnodes); 395 /* 396 * Insert into list of vnodes for the new mount point, if 397 * available. The caller must take a reference on the mount 398 * structure and donate to the vnode. 399 */ 400 if ((vp->v_mount = mp) != NULL) 401 TAILQ_INSERT_TAIL(&mp->mnt_vnodelist, vp, v_mntvnodes); 402 mutex_exit(&mntvnode_lock); 403 404 if (omp != NULL) { 405 /* Release reference to old mount. */ 406 vfs_destroy(omp); 407 } 408 } 409 410 /* 411 * Remove any vnodes in the vnode table belonging to mount point mp. 412 * 413 * If FORCECLOSE is not specified, there should not be any active ones, 414 * return error if any are found (nb: this is a user error, not a 415 * system error). If FORCECLOSE is specified, detach any active vnodes 416 * that are found. 417 * 418 * If WRITECLOSE is set, only flush out regular file vnodes open for 419 * writing. 420 * 421 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped. 422 */ 423 #ifdef DEBUG 424 int busyprt = 0; /* print out busy vnodes */ 425 struct ctldebug debug1 = { "busyprt", &busyprt }; 426 #endif 427 428 static vnode_t * 429 vflushnext(vnode_t *mvp, int *when) 430 { 431 432 if (hardclock_ticks > *when) { 433 mutex_exit(&mntvnode_lock); 434 yield(); 435 mutex_enter(&mntvnode_lock); 436 *when = hardclock_ticks + hz / 10; 437 } 438 return vunmark(mvp); 439 } 440 441 int 442 vflush(struct mount *mp, vnode_t *skipvp, int flags) 443 { 444 vnode_t *vp, *mvp; 445 int busy = 0, when = 0; 446 447 /* First, flush out any vnode references from vrele_list. */ 448 vrele_flush(); 449 450 /* Allocate a marker vnode. */ 451 mvp = vnalloc(mp); 452 453 /* 454 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone() 455 * and vclean() are called. 456 */ 457 mutex_enter(&mntvnode_lock); 458 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); 459 vp != NULL; 460 vp = vflushnext(mvp, &when)) { 461 vmark(mvp, vp); 462 if (vp->v_mount != mp || vismarker(vp)) 463 continue; 464 /* 465 * Skip over a selected vnode. 466 */ 467 if (vp == skipvp) 468 continue; 469 /* 470 * First try to recycle the vnode. 471 */ 472 if (vrecycle(vp, &mntvnode_lock)) { 473 mutex_enter(&mntvnode_lock); 474 continue; 475 } 476 mutex_enter(vp->v_interlock); 477 /* 478 * Ignore clean but still referenced vnodes. 479 */ 480 if ((vp->v_iflag & VI_CLEAN) != 0) { 481 mutex_exit(vp->v_interlock); 482 continue; 483 } 484 /* 485 * Skip over a vnodes marked VSYSTEM. 486 */ 487 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) { 488 mutex_exit(vp->v_interlock); 489 continue; 490 } 491 /* 492 * If WRITECLOSE is set, only flush out regular file 493 * vnodes open for writing. 494 */ 495 if ((flags & WRITECLOSE) && 496 (vp->v_writecount == 0 || vp->v_type != VREG)) { 497 mutex_exit(vp->v_interlock); 498 continue; 499 } 500 /* 501 * If FORCECLOSE is set, forcibly close the vnode. 502 * For block or character devices, revert to an 503 * anonymous device. For all other files, just 504 * kill them. 505 */ 506 if (flags & FORCECLOSE) { 507 mutex_exit(&mntvnode_lock); 508 if (vget(vp, 0) == 0) 509 vgone(vp); 510 mutex_enter(&mntvnode_lock); 511 continue; 512 } 513 #ifdef DEBUG 514 if (busyprt) 515 vprint("vflush: busy vnode", vp); 516 #endif 517 mutex_exit(vp->v_interlock); 518 busy++; 519 } 520 mutex_exit(&mntvnode_lock); 521 vnfree(mvp); 522 if (busy) 523 return (EBUSY); 524 return (0); 525 } 526 527 /* 528 * Mount a file system. 529 */ 530 531 /* 532 * Scan all active processes to see if any of them have a current or root 533 * directory onto which the new filesystem has just been mounted. If so, 534 * replace them with the new mount point. 535 */ 536 static void 537 mount_checkdirs(vnode_t *olddp) 538 { 539 vnode_t *newdp, *rele1, *rele2; 540 struct cwdinfo *cwdi; 541 struct proc *p; 542 bool retry; 543 544 if (olddp->v_usecount == 1) { 545 return; 546 } 547 if (VFS_ROOT(olddp->v_mountedhere, &newdp)) 548 panic("mount: lost mount"); 549 550 do { 551 retry = false; 552 mutex_enter(proc_lock); 553 PROCLIST_FOREACH(p, &allproc) { 554 if ((cwdi = p->p_cwdi) == NULL) 555 continue; 556 /* 557 * Cannot change to the old directory any more, 558 * so even if we see a stale value it is not a 559 * problem. 560 */ 561 if (cwdi->cwdi_cdir != olddp && 562 cwdi->cwdi_rdir != olddp) 563 continue; 564 retry = true; 565 rele1 = NULL; 566 rele2 = NULL; 567 atomic_inc_uint(&cwdi->cwdi_refcnt); 568 mutex_exit(proc_lock); 569 rw_enter(&cwdi->cwdi_lock, RW_WRITER); 570 if (cwdi->cwdi_cdir == olddp) { 571 rele1 = cwdi->cwdi_cdir; 572 vref(newdp); 573 cwdi->cwdi_cdir = newdp; 574 } 575 if (cwdi->cwdi_rdir == olddp) { 576 rele2 = cwdi->cwdi_rdir; 577 vref(newdp); 578 cwdi->cwdi_rdir = newdp; 579 } 580 rw_exit(&cwdi->cwdi_lock); 581 cwdfree(cwdi); 582 if (rele1 != NULL) 583 vrele(rele1); 584 if (rele2 != NULL) 585 vrele(rele2); 586 mutex_enter(proc_lock); 587 break; 588 } 589 mutex_exit(proc_lock); 590 } while (retry); 591 592 if (rootvnode == olddp) { 593 vrele(rootvnode); 594 vref(newdp); 595 rootvnode = newdp; 596 } 597 vput(newdp); 598 } 599 600 int 601 mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops, 602 const char *path, int flags, void *data, size_t *data_len) 603 { 604 vnode_t *vp = *vpp; 605 struct mount *mp; 606 struct pathbuf *pb; 607 struct nameidata nd; 608 int error; 609 610 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 611 KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data); 612 if (error) { 613 vfs_delref(vfsops); 614 return error; 615 } 616 617 /* Cannot make a non-dir a mount-point (from here anyway). */ 618 if (vp->v_type != VDIR) { 619 vfs_delref(vfsops); 620 return ENOTDIR; 621 } 622 623 if (flags & MNT_EXPORTED) { 624 vfs_delref(vfsops); 625 return EINVAL; 626 } 627 628 if ((mp = vfs_mountalloc(vfsops, vp)) == NULL) { 629 vfs_delref(vfsops); 630 return ENOMEM; 631 } 632 633 mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred); 634 635 /* 636 * The underlying file system may refuse the mount for 637 * various reasons. Allow the user to force it to happen. 638 * 639 * Set the mount level flags. 640 */ 641 mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE); 642 643 mutex_enter(&mp->mnt_updating); 644 error = VFS_MOUNT(mp, path, data, data_len); 645 mp->mnt_flag &= ~MNT_OP_FLAGS; 646 647 if (error != 0) 648 goto err_unmounted; 649 650 /* 651 * Validate and prepare the mount point. 652 */ 653 error = pathbuf_copyin(path, &pb); 654 if (error != 0) { 655 goto err_mounted; 656 } 657 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 658 error = namei(&nd); 659 pathbuf_destroy(pb); 660 if (error != 0) { 661 goto err_mounted; 662 } 663 if (nd.ni_vp != vp) { 664 vput(nd.ni_vp); 665 error = EINVAL; 666 goto err_mounted; 667 } 668 if (vp->v_mountedhere != NULL) { 669 vput(nd.ni_vp); 670 error = EBUSY; 671 goto err_mounted; 672 } 673 error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0); 674 if (error != 0) { 675 vput(nd.ni_vp); 676 goto err_mounted; 677 } 678 679 /* 680 * Put the new filesystem on the mount list after root. 681 */ 682 cache_purge(vp); 683 mp->mnt_iflag &= ~IMNT_WANTRDWR; 684 685 mutex_enter(&mountlist_lock); 686 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 687 mutex_exit(&mountlist_lock); 688 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 689 error = vfs_allocate_syncvnode(mp); 690 if (error == 0) 691 vp->v_mountedhere = mp; 692 vput(nd.ni_vp); 693 if (error != 0) 694 goto err_onmountlist; 695 696 mount_checkdirs(vp); 697 mutex_exit(&mp->mnt_updating); 698 699 /* Hold an additional reference to the mount across VFS_START(). */ 700 vfs_unbusy(mp, true, NULL); 701 (void) VFS_STATVFS(mp, &mp->mnt_stat); 702 error = VFS_START(mp, 0); 703 if (error) { 704 vrele(vp); 705 } else if (flags & MNT_EXTATTR) { 706 error = VFS_EXTATTRCTL(vp->v_mountedhere, 707 EXTATTR_CMD_START, NULL, 0, NULL); 708 if (error) 709 printf("%s: failed to start extattr: error = %d\n", 710 vp->v_mountedhere->mnt_stat.f_mntonname, error); 711 } 712 /* Drop reference held for VFS_START(). */ 713 vfs_destroy(mp); 714 *vpp = NULL; 715 return error; 716 717 err_onmountlist: 718 mutex_enter(&mountlist_lock); 719 TAILQ_REMOVE(&mountlist, mp, mnt_list); 720 mp->mnt_iflag |= IMNT_GONE; 721 mutex_exit(&mountlist_lock); 722 723 err_mounted: 724 if (VFS_UNMOUNT(mp, MNT_FORCE) != 0) 725 panic("Unmounting fresh file system failed"); 726 727 err_unmounted: 728 vp->v_mountedhere = NULL; 729 mutex_exit(&mp->mnt_updating); 730 vfs_unbusy(mp, false, NULL); 731 vfs_destroy(mp); 732 733 return error; 734 } 735 736 /* 737 * Do the actual file system unmount. File system is assumed to have 738 * been locked by the caller. 739 * 740 * => Caller hold reference to the mount, explicitly for dounmount(). 741 */ 742 int 743 dounmount(struct mount *mp, int flags, struct lwp *l) 744 { 745 vnode_t *coveredvp; 746 int error, async, used_syncer; 747 748 #if NVERIEXEC > 0 749 error = veriexec_unmountchk(mp); 750 if (error) 751 return (error); 752 #endif /* NVERIEXEC > 0 */ 753 754 /* 755 * XXX Freeze syncer. Must do this before locking the 756 * mount point. See dounmount() for details. 757 */ 758 mutex_enter(&syncer_mutex); 759 760 /* 761 * Abort unmount attempt when the filesystem is in use 762 */ 763 mutex_enter(&mp->mnt_unmounting); 764 if (mp->mnt_busynest != 0) { 765 mutex_exit(&mp->mnt_unmounting); 766 mutex_exit(&syncer_mutex); 767 return EBUSY; 768 } 769 770 /* 771 * Abort unmount attempt when the filesystem is not mounted 772 */ 773 if ((mp->mnt_iflag & IMNT_GONE) != 0) { 774 mutex_exit(&mp->mnt_unmounting); 775 mutex_exit(&syncer_mutex); 776 return ENOENT; 777 } 778 779 used_syncer = (mp->mnt_syncer != NULL); 780 781 /* 782 * XXX Syncer must be frozen when we get here. This should really 783 * be done on a per-mountpoint basis, but the syncer doesn't work 784 * like that. 785 * 786 * The caller of dounmount() must acquire syncer_mutex because 787 * the syncer itself acquires locks in syncer_mutex -> vfs_busy 788 * order, and we must preserve that order to avoid deadlock. 789 * 790 * So, if the file system did not use the syncer, now is 791 * the time to release the syncer_mutex. 792 */ 793 if (used_syncer == 0) { 794 mutex_exit(&syncer_mutex); 795 } 796 mp->mnt_iflag |= IMNT_UNMOUNT; 797 mutex_enter(&mp->mnt_updating); 798 async = mp->mnt_flag & MNT_ASYNC; 799 mp->mnt_flag &= ~MNT_ASYNC; 800 cache_purgevfs(mp); /* remove cache entries for this file sys */ 801 if (mp->mnt_syncer != NULL) 802 vfs_deallocate_syncvnode(mp); 803 error = 0; 804 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 805 error = VFS_SYNC(mp, MNT_WAIT, l->l_cred); 806 } 807 if (error == 0 || (flags & MNT_FORCE)) { 808 error = VFS_UNMOUNT(mp, flags); 809 } 810 if (error) { 811 mp->mnt_iflag &= ~IMNT_UNMOUNT; 812 mutex_exit(&mp->mnt_unmounting); 813 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) 814 (void) vfs_allocate_syncvnode(mp); 815 mp->mnt_flag |= async; 816 mutex_exit(&mp->mnt_updating); 817 if (used_syncer) 818 mutex_exit(&syncer_mutex); 819 return (error); 820 } 821 mutex_exit(&mp->mnt_updating); 822 823 /* 824 * release mnt_umounting lock here, because other code calls 825 * vfs_busy() while holding the mountlist_lock. 826 * 827 * mark filesystem as gone to prevent further umounts 828 * after mnt_umounting lock is gone, this also prevents 829 * vfs_busy() from succeeding. 830 */ 831 mp->mnt_iflag |= IMNT_GONE; 832 mutex_exit(&mp->mnt_unmounting); 833 834 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) { 835 vn_lock(coveredvp, LK_EXCLUSIVE | LK_RETRY); 836 coveredvp->v_mountedhere = NULL; 837 VOP_UNLOCK(coveredvp); 838 } 839 mutex_enter(&mountlist_lock); 840 TAILQ_REMOVE(&mountlist, mp, mnt_list); 841 mutex_exit(&mountlist_lock); 842 if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL) 843 panic("unmount: dangling vnode"); 844 if (used_syncer) 845 mutex_exit(&syncer_mutex); 846 vfs_hooks_unmount(mp); 847 848 vfs_destroy(mp); /* reference from mount() */ 849 if (coveredvp != NULLVP) { 850 vrele(coveredvp); 851 } 852 return (0); 853 } 854 855 /* 856 * Unmount all file systems. 857 * We traverse the list in reverse order under the assumption that doing so 858 * will avoid needing to worry about dependencies. 859 */ 860 bool 861 vfs_unmountall(struct lwp *l) 862 { 863 864 printf("unmounting file systems...\n"); 865 return vfs_unmountall1(l, true, true); 866 } 867 868 static void 869 vfs_unmount_print(struct mount *mp, const char *pfx) 870 { 871 872 aprint_verbose("%sunmounted %s on %s type %s\n", pfx, 873 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname, 874 mp->mnt_stat.f_fstypename); 875 } 876 877 bool 878 vfs_unmount_forceone(struct lwp *l) 879 { 880 struct mount *mp, *nmp; 881 int error; 882 883 nmp = NULL; 884 885 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 886 if (nmp == NULL || mp->mnt_gen > nmp->mnt_gen) { 887 nmp = mp; 888 } 889 } 890 if (nmp == NULL) { 891 return false; 892 } 893 894 #ifdef DEBUG 895 printf("forcefully unmounting %s (%s)...\n", 896 nmp->mnt_stat.f_mntonname, nmp->mnt_stat.f_mntfromname); 897 #endif 898 atomic_inc_uint(&nmp->mnt_refcnt); 899 if ((error = dounmount(nmp, MNT_FORCE, l)) == 0) { 900 vfs_unmount_print(nmp, "forcefully "); 901 return true; 902 } else { 903 vfs_destroy(nmp); 904 } 905 906 #ifdef DEBUG 907 printf("forceful unmount of %s failed with error %d\n", 908 nmp->mnt_stat.f_mntonname, error); 909 #endif 910 911 return false; 912 } 913 914 bool 915 vfs_unmountall1(struct lwp *l, bool force, bool verbose) 916 { 917 struct mount *mp, *nmp; 918 bool any_error = false, progress = false; 919 int error; 920 921 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, nmp) { 922 #ifdef DEBUG 923 printf("unmounting %p %s (%s)...\n", 924 (void *)mp, mp->mnt_stat.f_mntonname, 925 mp->mnt_stat.f_mntfromname); 926 #endif 927 atomic_inc_uint(&mp->mnt_refcnt); 928 if ((error = dounmount(mp, force ? MNT_FORCE : 0, l)) == 0) { 929 vfs_unmount_print(mp, ""); 930 progress = true; 931 } else { 932 vfs_destroy(mp); 933 if (verbose) { 934 printf("unmount of %s failed with error %d\n", 935 mp->mnt_stat.f_mntonname, error); 936 } 937 any_error = true; 938 } 939 } 940 if (verbose) { 941 printf("unmounting done\n"); 942 } 943 if (any_error && verbose) { 944 printf("WARNING: some file systems would not unmount\n"); 945 } 946 return progress; 947 } 948 949 void 950 vfs_sync_all(struct lwp *l) 951 { 952 printf("syncing disks... "); 953 954 /* remove user processes from run queue */ 955 suspendsched(); 956 (void)spl0(); 957 958 /* avoid coming back this way again if we panic. */ 959 doing_shutdown = 1; 960 961 do_sys_sync(l); 962 963 /* Wait for sync to finish. */ 964 if (buf_syncwait() != 0) { 965 #if defined(DDB) && defined(DEBUG_HALT_BUSY) 966 Debugger(); 967 #endif 968 printf("giving up\n"); 969 return; 970 } else 971 printf("done\n"); 972 } 973 974 /* 975 * Sync and unmount file systems before shutting down. 976 */ 977 void 978 vfs_shutdown(void) 979 { 980 lwp_t *l = curlwp; 981 982 vfs_sync_all(l); 983 984 /* 985 * If we have paniced - do not make the situation potentially 986 * worse by unmounting the file systems. 987 */ 988 if (panicstr != NULL) { 989 return; 990 } 991 992 /* Unmount file systems. */ 993 vfs_unmountall(l); 994 } 995 996 /* 997 * Print a list of supported file system types (used by vfs_mountroot) 998 */ 999 static void 1000 vfs_print_fstypes(void) 1001 { 1002 struct vfsops *v; 1003 int cnt = 0; 1004 1005 mutex_enter(&vfs_list_lock); 1006 LIST_FOREACH(v, &vfs_list, vfs_list) 1007 ++cnt; 1008 mutex_exit(&vfs_list_lock); 1009 1010 if (cnt == 0) { 1011 printf("WARNING: No file system modules have been loaded.\n"); 1012 return; 1013 } 1014 1015 printf("Supported file systems:"); 1016 mutex_enter(&vfs_list_lock); 1017 LIST_FOREACH(v, &vfs_list, vfs_list) { 1018 printf(" %s", v->vfs_name); 1019 } 1020 mutex_exit(&vfs_list_lock); 1021 printf("\n"); 1022 } 1023 1024 /* 1025 * Mount the root file system. If the operator didn't specify a 1026 * file system to use, try all possible file systems until one 1027 * succeeds. 1028 */ 1029 int 1030 vfs_mountroot(void) 1031 { 1032 struct vfsops *v; 1033 int error = ENODEV; 1034 1035 if (root_device == NULL) 1036 panic("vfs_mountroot: root device unknown"); 1037 1038 switch (device_class(root_device)) { 1039 case DV_IFNET: 1040 if (rootdev != NODEV) 1041 panic("vfs_mountroot: rootdev set for DV_IFNET " 1042 "(0x%llx -> %llu,%llu)", 1043 (unsigned long long)rootdev, 1044 (unsigned long long)major(rootdev), 1045 (unsigned long long)minor(rootdev)); 1046 break; 1047 1048 case DV_DISK: 1049 if (rootdev == NODEV) 1050 panic("vfs_mountroot: rootdev not set for DV_DISK"); 1051 if (bdevvp(rootdev, &rootvp)) 1052 panic("vfs_mountroot: can't get vnode for rootdev"); 1053 error = VOP_OPEN(rootvp, FREAD, FSCRED); 1054 if (error) { 1055 printf("vfs_mountroot: can't open root device\n"); 1056 return (error); 1057 } 1058 break; 1059 1060 case DV_VIRTUAL: 1061 break; 1062 1063 default: 1064 printf("%s: inappropriate for root file system\n", 1065 device_xname(root_device)); 1066 return (ENODEV); 1067 } 1068 1069 /* 1070 * If user specified a root fs type, use it. Make sure the 1071 * specified type exists and has a mount_root() 1072 */ 1073 if (strcmp(rootfstype, ROOT_FSTYPE_ANY) != 0) { 1074 v = vfs_getopsbyname(rootfstype); 1075 error = EFTYPE; 1076 if (v != NULL) { 1077 if (v->vfs_mountroot != NULL) { 1078 error = (v->vfs_mountroot)(); 1079 } 1080 v->vfs_refcount--; 1081 } 1082 goto done; 1083 } 1084 1085 /* 1086 * Try each file system currently configured into the kernel. 1087 */ 1088 mutex_enter(&vfs_list_lock); 1089 LIST_FOREACH(v, &vfs_list, vfs_list) { 1090 if (v->vfs_mountroot == NULL) 1091 continue; 1092 #ifdef DEBUG 1093 aprint_normal("mountroot: trying %s...\n", v->vfs_name); 1094 #endif 1095 v->vfs_refcount++; 1096 mutex_exit(&vfs_list_lock); 1097 error = (*v->vfs_mountroot)(); 1098 mutex_enter(&vfs_list_lock); 1099 v->vfs_refcount--; 1100 if (!error) { 1101 aprint_normal("root file system type: %s\n", 1102 v->vfs_name); 1103 break; 1104 } 1105 } 1106 mutex_exit(&vfs_list_lock); 1107 1108 if (v == NULL) { 1109 vfs_print_fstypes(); 1110 printf("no file system for %s", device_xname(root_device)); 1111 if (device_class(root_device) == DV_DISK) 1112 printf(" (dev 0x%llx)", (unsigned long long)rootdev); 1113 printf("\n"); 1114 error = EFTYPE; 1115 } 1116 1117 done: 1118 if (error && device_class(root_device) == DV_DISK) { 1119 VOP_CLOSE(rootvp, FREAD, FSCRED); 1120 vrele(rootvp); 1121 } 1122 if (error == 0) { 1123 struct mount *mp; 1124 extern struct cwdinfo cwdi0; 1125 1126 mp = TAILQ_FIRST(&mountlist); 1127 mp->mnt_flag |= MNT_ROOTFS; 1128 mp->mnt_op->vfs_refcount++; 1129 1130 /* 1131 * Get the vnode for '/'. Set cwdi0.cwdi_cdir to 1132 * reference it. 1133 */ 1134 error = VFS_ROOT(mp, &rootvnode); 1135 if (error) 1136 panic("cannot find root vnode, error=%d", error); 1137 cwdi0.cwdi_cdir = rootvnode; 1138 vref(cwdi0.cwdi_cdir); 1139 VOP_UNLOCK(rootvnode); 1140 cwdi0.cwdi_rdir = NULL; 1141 1142 /* 1143 * Now that root is mounted, we can fixup initproc's CWD 1144 * info. All other processes are kthreads, which merely 1145 * share proc0's CWD info. 1146 */ 1147 initproc->p_cwdi->cwdi_cdir = rootvnode; 1148 vref(initproc->p_cwdi->cwdi_cdir); 1149 initproc->p_cwdi->cwdi_rdir = NULL; 1150 /* 1151 * Enable loading of modules from the filesystem 1152 */ 1153 module_load_vfs_init(); 1154 1155 } 1156 return (error); 1157 } 1158 1159 /* 1160 * mount_specific_key_create -- 1161 * Create a key for subsystem mount-specific data. 1162 */ 1163 int 1164 mount_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor) 1165 { 1166 1167 return specificdata_key_create(mount_specificdata_domain, keyp, dtor); 1168 } 1169 1170 /* 1171 * mount_specific_key_delete -- 1172 * Delete a key for subsystem mount-specific data. 1173 */ 1174 void 1175 mount_specific_key_delete(specificdata_key_t key) 1176 { 1177 1178 specificdata_key_delete(mount_specificdata_domain, key); 1179 } 1180 1181 /* 1182 * mount_initspecific -- 1183 * Initialize a mount's specificdata container. 1184 */ 1185 void 1186 mount_initspecific(struct mount *mp) 1187 { 1188 int error __diagused; 1189 1190 error = specificdata_init(mount_specificdata_domain, 1191 &mp->mnt_specdataref); 1192 KASSERT(error == 0); 1193 } 1194 1195 /* 1196 * mount_finispecific -- 1197 * Finalize a mount's specificdata container. 1198 */ 1199 void 1200 mount_finispecific(struct mount *mp) 1201 { 1202 1203 specificdata_fini(mount_specificdata_domain, &mp->mnt_specdataref); 1204 } 1205 1206 /* 1207 * mount_getspecific -- 1208 * Return mount-specific data corresponding to the specified key. 1209 */ 1210 void * 1211 mount_getspecific(struct mount *mp, specificdata_key_t key) 1212 { 1213 1214 return specificdata_getspecific(mount_specificdata_domain, 1215 &mp->mnt_specdataref, key); 1216 } 1217 1218 /* 1219 * mount_setspecific -- 1220 * Set mount-specific data corresponding to the specified key. 1221 */ 1222 void 1223 mount_setspecific(struct mount *mp, specificdata_key_t key, void *data) 1224 { 1225 1226 specificdata_setspecific(mount_specificdata_domain, 1227 &mp->mnt_specdataref, key, data); 1228 } 1229 1230 /* 1231 * Check to see if a filesystem is mounted on a block device. 1232 */ 1233 int 1234 vfs_mountedon(vnode_t *vp) 1235 { 1236 vnode_t *vq; 1237 int error = 0; 1238 1239 if (vp->v_type != VBLK) 1240 return ENOTBLK; 1241 if (spec_node_getmountedfs(vp) != NULL) 1242 return EBUSY; 1243 if (spec_node_lookup_by_dev(vp->v_type, vp->v_rdev, &vq) == 0) { 1244 if (spec_node_getmountedfs(vq) != NULL) 1245 error = EBUSY; 1246 vrele(vq); 1247 } 1248 1249 return error; 1250 } 1251 1252 /* 1253 * Check if a device pointed to by vp is mounted. 1254 * 1255 * Returns: 1256 * EINVAL if it's not a disk 1257 * EBUSY if it's a disk and mounted 1258 * 0 if it's a disk and not mounted 1259 */ 1260 int 1261 rawdev_mounted(vnode_t *vp, vnode_t **bvpp) 1262 { 1263 vnode_t *bvp; 1264 dev_t dev; 1265 int d_type; 1266 1267 bvp = NULL; 1268 d_type = D_OTHER; 1269 1270 if (iskmemvp(vp)) 1271 return EINVAL; 1272 1273 switch (vp->v_type) { 1274 case VCHR: { 1275 const struct cdevsw *cdev; 1276 1277 dev = vp->v_rdev; 1278 cdev = cdevsw_lookup(dev); 1279 if (cdev != NULL) { 1280 dev_t blkdev; 1281 1282 blkdev = devsw_chr2blk(dev); 1283 if (blkdev != NODEV) { 1284 if (vfinddev(blkdev, VBLK, &bvp) != 0) { 1285 d_type = (cdev->d_flag & D_TYPEMASK); 1286 /* XXX: what if bvp disappears? */ 1287 vrele(bvp); 1288 } 1289 } 1290 } 1291 1292 break; 1293 } 1294 1295 case VBLK: { 1296 const struct bdevsw *bdev; 1297 1298 dev = vp->v_rdev; 1299 bdev = bdevsw_lookup(dev); 1300 if (bdev != NULL) 1301 d_type = (bdev->d_flag & D_TYPEMASK); 1302 1303 bvp = vp; 1304 1305 break; 1306 } 1307 1308 default: 1309 break; 1310 } 1311 1312 if (d_type != D_DISK) 1313 return EINVAL; 1314 1315 if (bvpp != NULL) 1316 *bvpp = bvp; 1317 1318 /* 1319 * XXX: This is bogus. We should be failing the request 1320 * XXX: not only if this specific slice is mounted, but 1321 * XXX: if it's on a disk with any other mounted slice. 1322 */ 1323 if (vfs_mountedon(bvp)) 1324 return EBUSY; 1325 1326 return 0; 1327 } 1328 1329 /* 1330 * Make a 'unique' number from a mount type name. 1331 */ 1332 long 1333 makefstype(const char *type) 1334 { 1335 long rv; 1336 1337 for (rv = 0; *type; type++) { 1338 rv <<= 2; 1339 rv ^= *type; 1340 } 1341 return rv; 1342 } 1343 1344 void 1345 mountlist_append(struct mount *mp) 1346 { 1347 mutex_enter(&mountlist_lock); 1348 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1349 mutex_exit(&mountlist_lock); 1350 } 1351