1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1999-2004 Poul-Henning Kamp 5 * Copyright (c) 1999 Michael Smith 6 * Copyright (c) 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/param.h> 43 #include <sys/conf.h> 44 #include <sys/smp.h> 45 #include <sys/devctl.h> 46 #include <sys/eventhandler.h> 47 #include <sys/fcntl.h> 48 #include <sys/jail.h> 49 #include <sys/kernel.h> 50 #include <sys/ktr.h> 51 #include <sys/libkern.h> 52 #include <sys/limits.h> 53 #include <sys/malloc.h> 54 #include <sys/mount.h> 55 #include <sys/mutex.h> 56 #include <sys/namei.h> 57 #include <sys/priv.h> 58 #include <sys/proc.h> 59 #include <sys/filedesc.h> 60 #include <sys/reboot.h> 61 #include <sys/sbuf.h> 62 #include <sys/syscallsubr.h> 63 #include <sys/sysproto.h> 64 #include <sys/sx.h> 65 #include <sys/sysctl.h> 66 #include <sys/systm.h> 67 #include <sys/taskqueue.h> 68 #include <sys/vnode.h> 69 #include <vm/uma.h> 70 71 #include <geom/geom.h> 72 73 #include <machine/stdarg.h> 74 75 #include <security/audit/audit.h> 76 #include <security/mac/mac_framework.h> 77 78 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64) 79 80 static int vfs_domount(struct thread *td, const char *fstype, char *fspath, 81 uint64_t fsflags, struct vfsoptlist **optlist); 82 static void free_mntarg(struct mntarg *ma); 83 84 static int usermount = 0; 85 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 86 "Unprivileged users may mount and unmount file systems"); 87 88 static bool default_autoro = false; 89 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0, 90 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified"); 91 92 static bool recursive_forced_unmount = false; 93 SYSCTL_BOOL(_vfs, OID_AUTO, recursive_forced_unmount, CTLFLAG_RW, 94 &recursive_forced_unmount, 0, "Recursively unmount stacked upper mounts" 95 " when a file system is forcibly unmounted"); 96 97 static SYSCTL_NODE(_vfs, OID_AUTO, deferred_unmount, 98 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "deferred unmount controls"); 99 100 static unsigned int deferred_unmount_retry_limit = 10; 101 SYSCTL_UINT(_vfs_deferred_unmount, OID_AUTO, retry_limit, CTLFLAG_RW, 102 &deferred_unmount_retry_limit, 0, 103 "Maximum number of retries for deferred unmount failure"); 104 105 static int deferred_unmount_retry_delay_hz; 106 SYSCTL_INT(_vfs_deferred_unmount, OID_AUTO, retry_delay_hz, CTLFLAG_RW, 107 &deferred_unmount_retry_delay_hz, 0, 108 "Delay in units of [1/kern.hz]s when retrying a failed deferred unmount"); 109 110 static int deferred_unmount_total_retries = 0; 111 SYSCTL_INT(_vfs_deferred_unmount, OID_AUTO, total_retries, CTLFLAG_RD, 112 &deferred_unmount_total_retries, 0, 113 "Total number of retried deferred unmounts"); 114 115 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 116 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure"); 117 static uma_zone_t mount_zone; 118 119 /* List of mounted filesystems. */ 120 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 121 122 /* For any iteration/modification of mountlist */ 123 struct mtx_padalign __exclusive_cache_line mountlist_mtx; 124 125 EVENTHANDLER_LIST_DEFINE(vfs_mounted); 126 EVENTHANDLER_LIST_DEFINE(vfs_unmounted); 127 128 static void vfs_deferred_unmount(void *arg, int pending); 129 static struct timeout_task deferred_unmount_task; 130 static struct mtx deferred_unmount_lock; 131 MTX_SYSINIT(deferred_unmount, &deferred_unmount_lock, "deferred_unmount", 132 MTX_DEF); 133 static STAILQ_HEAD(, mount) deferred_unmount_list = 134 STAILQ_HEAD_INITIALIZER(deferred_unmount_list); 135 TASKQUEUE_DEFINE_THREAD(deferred_unmount); 136 137 static void mount_devctl_event(const char *type, struct mount *mp, bool donew); 138 139 /* 140 * Global opts, taken by all filesystems 141 */ 142 static const char *global_opts[] = { 143 "errmsg", 144 "fstype", 145 "fspath", 146 "ro", 147 "rw", 148 "nosuid", 149 "noexec", 150 NULL 151 }; 152 153 static int 154 mount_init(void *mem, int size, int flags) 155 { 156 struct mount *mp; 157 158 mp = (struct mount *)mem; 159 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF); 160 mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF); 161 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0); 162 mp->mnt_pcpu = uma_zalloc_pcpu(pcpu_zone_16, M_WAITOK | M_ZERO); 163 mp->mnt_ref = 0; 164 mp->mnt_vfs_ops = 1; 165 mp->mnt_rootvnode = NULL; 166 return (0); 167 } 168 169 static void 170 mount_fini(void *mem, int size) 171 { 172 struct mount *mp; 173 174 mp = (struct mount *)mem; 175 uma_zfree_pcpu(pcpu_zone_16, mp->mnt_pcpu); 176 lockdestroy(&mp->mnt_explock); 177 mtx_destroy(&mp->mnt_listmtx); 178 mtx_destroy(&mp->mnt_mtx); 179 } 180 181 static void 182 vfs_mount_init(void *dummy __unused) 183 { 184 TIMEOUT_TASK_INIT(taskqueue_deferred_unmount, &deferred_unmount_task, 185 0, vfs_deferred_unmount, NULL); 186 deferred_unmount_retry_delay_hz = hz; 187 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL, 188 NULL, mount_init, mount_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE); 189 mtx_init(&mountlist_mtx, "mountlist", NULL, MTX_DEF); 190 } 191 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL); 192 193 /* 194 * --------------------------------------------------------------------- 195 * Functions for building and sanitizing the mount options 196 */ 197 198 /* Remove one mount option. */ 199 static void 200 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt) 201 { 202 203 TAILQ_REMOVE(opts, opt, link); 204 free(opt->name, M_MOUNT); 205 if (opt->value != NULL) 206 free(opt->value, M_MOUNT); 207 free(opt, M_MOUNT); 208 } 209 210 /* Release all resources related to the mount options. */ 211 void 212 vfs_freeopts(struct vfsoptlist *opts) 213 { 214 struct vfsopt *opt; 215 216 while (!TAILQ_EMPTY(opts)) { 217 opt = TAILQ_FIRST(opts); 218 vfs_freeopt(opts, opt); 219 } 220 free(opts, M_MOUNT); 221 } 222 223 void 224 vfs_deleteopt(struct vfsoptlist *opts, const char *name) 225 { 226 struct vfsopt *opt, *temp; 227 228 if (opts == NULL) 229 return; 230 TAILQ_FOREACH_SAFE(opt, opts, link, temp) { 231 if (strcmp(opt->name, name) == 0) 232 vfs_freeopt(opts, opt); 233 } 234 } 235 236 static int 237 vfs_isopt_ro(const char *opt) 238 { 239 240 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 || 241 strcmp(opt, "norw") == 0) 242 return (1); 243 return (0); 244 } 245 246 static int 247 vfs_isopt_rw(const char *opt) 248 { 249 250 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0) 251 return (1); 252 return (0); 253 } 254 255 /* 256 * Check if options are equal (with or without the "no" prefix). 257 */ 258 static int 259 vfs_equalopts(const char *opt1, const char *opt2) 260 { 261 char *p; 262 263 /* "opt" vs. "opt" or "noopt" vs. "noopt" */ 264 if (strcmp(opt1, opt2) == 0) 265 return (1); 266 /* "noopt" vs. "opt" */ 267 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 268 return (1); 269 /* "opt" vs. "noopt" */ 270 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 271 return (1); 272 while ((p = strchr(opt1, '.')) != NULL && 273 !strncmp(opt1, opt2, ++p - opt1)) { 274 opt2 += p - opt1; 275 opt1 = p; 276 /* "foo.noopt" vs. "foo.opt" */ 277 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 278 return (1); 279 /* "foo.opt" vs. "foo.noopt" */ 280 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 281 return (1); 282 } 283 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */ 284 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) && 285 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2))) 286 return (1); 287 return (0); 288 } 289 290 /* 291 * If a mount option is specified several times, 292 * (with or without the "no" prefix) only keep 293 * the last occurrence of it. 294 */ 295 static void 296 vfs_sanitizeopts(struct vfsoptlist *opts) 297 { 298 struct vfsopt *opt, *opt2, *tmp; 299 300 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) { 301 opt2 = TAILQ_PREV(opt, vfsoptlist, link); 302 while (opt2 != NULL) { 303 if (vfs_equalopts(opt->name, opt2->name)) { 304 tmp = TAILQ_PREV(opt2, vfsoptlist, link); 305 vfs_freeopt(opts, opt2); 306 opt2 = tmp; 307 } else { 308 opt2 = TAILQ_PREV(opt2, vfsoptlist, link); 309 } 310 } 311 } 312 } 313 314 /* 315 * Build a linked list of mount options from a struct uio. 316 */ 317 int 318 vfs_buildopts(struct uio *auio, struct vfsoptlist **options) 319 { 320 struct vfsoptlist *opts; 321 struct vfsopt *opt; 322 size_t memused, namelen, optlen; 323 unsigned int i, iovcnt; 324 int error; 325 326 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 327 TAILQ_INIT(opts); 328 memused = 0; 329 iovcnt = auio->uio_iovcnt; 330 for (i = 0; i < iovcnt; i += 2) { 331 namelen = auio->uio_iov[i].iov_len; 332 optlen = auio->uio_iov[i + 1].iov_len; 333 memused += sizeof(struct vfsopt) + optlen + namelen; 334 /* 335 * Avoid consuming too much memory, and attempts to overflow 336 * memused. 337 */ 338 if (memused > VFS_MOUNTARG_SIZE_MAX || 339 optlen > VFS_MOUNTARG_SIZE_MAX || 340 namelen > VFS_MOUNTARG_SIZE_MAX) { 341 error = EINVAL; 342 goto bad; 343 } 344 345 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 346 opt->name = malloc(namelen, M_MOUNT, M_WAITOK); 347 opt->value = NULL; 348 opt->len = 0; 349 opt->pos = i / 2; 350 opt->seen = 0; 351 352 /* 353 * Do this early, so jumps to "bad" will free the current 354 * option. 355 */ 356 TAILQ_INSERT_TAIL(opts, opt, link); 357 358 if (auio->uio_segflg == UIO_SYSSPACE) { 359 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen); 360 } else { 361 error = copyin(auio->uio_iov[i].iov_base, opt->name, 362 namelen); 363 if (error) 364 goto bad; 365 } 366 /* Ensure names are null-terminated strings. */ 367 if (namelen == 0 || opt->name[namelen - 1] != '\0') { 368 error = EINVAL; 369 goto bad; 370 } 371 if (optlen != 0) { 372 opt->len = optlen; 373 opt->value = malloc(optlen, M_MOUNT, M_WAITOK); 374 if (auio->uio_segflg == UIO_SYSSPACE) { 375 bcopy(auio->uio_iov[i + 1].iov_base, opt->value, 376 optlen); 377 } else { 378 error = copyin(auio->uio_iov[i + 1].iov_base, 379 opt->value, optlen); 380 if (error) 381 goto bad; 382 } 383 } 384 } 385 vfs_sanitizeopts(opts); 386 *options = opts; 387 return (0); 388 bad: 389 vfs_freeopts(opts); 390 return (error); 391 } 392 393 /* 394 * Merge the old mount options with the new ones passed 395 * in the MNT_UPDATE case. 396 * 397 * XXX: This function will keep a "nofoo" option in the new 398 * options. E.g, if the option's canonical name is "foo", 399 * "nofoo" ends up in the mount point's active options. 400 */ 401 static void 402 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts) 403 { 404 struct vfsopt *opt, *new; 405 406 TAILQ_FOREACH(opt, oldopts, link) { 407 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 408 new->name = strdup(opt->name, M_MOUNT); 409 if (opt->len != 0) { 410 new->value = malloc(opt->len, M_MOUNT, M_WAITOK); 411 bcopy(opt->value, new->value, opt->len); 412 } else 413 new->value = NULL; 414 new->len = opt->len; 415 new->seen = opt->seen; 416 TAILQ_INSERT_HEAD(toopts, new, link); 417 } 418 vfs_sanitizeopts(toopts); 419 } 420 421 /* 422 * Mount a filesystem. 423 */ 424 #ifndef _SYS_SYSPROTO_H_ 425 struct nmount_args { 426 struct iovec *iovp; 427 unsigned int iovcnt; 428 int flags; 429 }; 430 #endif 431 int 432 sys_nmount(struct thread *td, struct nmount_args *uap) 433 { 434 struct uio *auio; 435 int error; 436 u_int iovcnt; 437 uint64_t flags; 438 439 /* 440 * Mount flags are now 64-bits. On 32-bit archtectures only 441 * 32-bits are passed in, but from here on everything handles 442 * 64-bit flags correctly. 443 */ 444 flags = uap->flags; 445 446 AUDIT_ARG_FFLAGS(flags); 447 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__, 448 uap->iovp, uap->iovcnt, flags); 449 450 /* 451 * Filter out MNT_ROOTFS. We do not want clients of nmount() in 452 * userspace to set this flag, but we must filter it out if we want 453 * MNT_UPDATE on the root file system to work. 454 * MNT_ROOTFS should only be set by the kernel when mounting its 455 * root file system. 456 */ 457 flags &= ~MNT_ROOTFS; 458 459 iovcnt = uap->iovcnt; 460 /* 461 * Check that we have an even number of iovec's 462 * and that we have at least two options. 463 */ 464 if ((iovcnt & 1) || (iovcnt < 4)) { 465 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__, 466 uap->iovcnt); 467 return (EINVAL); 468 } 469 470 error = copyinuio(uap->iovp, iovcnt, &auio); 471 if (error) { 472 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno", 473 __func__, error); 474 return (error); 475 } 476 error = vfs_donmount(td, flags, auio); 477 478 free(auio, M_IOV); 479 return (error); 480 } 481 482 /* 483 * --------------------------------------------------------------------- 484 * Various utility functions 485 */ 486 487 /* 488 * Get a reference on a mount point from a vnode. 489 * 490 * The vnode is allowed to be passed unlocked and race against dooming. Note in 491 * such case there are no guarantees the referenced mount point will still be 492 * associated with it after the function returns. 493 */ 494 struct mount * 495 vfs_ref_from_vp(struct vnode *vp) 496 { 497 struct mount *mp; 498 struct mount_pcpu *mpcpu; 499 500 mp = atomic_load_ptr(&vp->v_mount); 501 if (__predict_false(mp == NULL)) { 502 return (mp); 503 } 504 if (vfs_op_thread_enter(mp, mpcpu)) { 505 if (__predict_true(mp == vp->v_mount)) { 506 vfs_mp_count_add_pcpu(mpcpu, ref, 1); 507 vfs_op_thread_exit(mp, mpcpu); 508 } else { 509 vfs_op_thread_exit(mp, mpcpu); 510 mp = NULL; 511 } 512 } else { 513 MNT_ILOCK(mp); 514 if (mp == vp->v_mount) { 515 MNT_REF(mp); 516 MNT_IUNLOCK(mp); 517 } else { 518 MNT_IUNLOCK(mp); 519 mp = NULL; 520 } 521 } 522 return (mp); 523 } 524 525 void 526 vfs_ref(struct mount *mp) 527 { 528 struct mount_pcpu *mpcpu; 529 530 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 531 if (vfs_op_thread_enter(mp, mpcpu)) { 532 vfs_mp_count_add_pcpu(mpcpu, ref, 1); 533 vfs_op_thread_exit(mp, mpcpu); 534 return; 535 } 536 537 MNT_ILOCK(mp); 538 MNT_REF(mp); 539 MNT_IUNLOCK(mp); 540 } 541 542 /* 543 * Register ump as an upper mount of the mount associated with 544 * vnode vp. This registration will be tracked through 545 * mount_upper_node upper, which should be allocated by the 546 * caller and stored in per-mount data associated with mp. 547 * 548 * If successful, this function will return the mount associated 549 * with vp, and will ensure that it cannot be unmounted until 550 * ump has been unregistered as one of its upper mounts. 551 * 552 * Upon failure this function will return NULL. 553 */ 554 struct mount * 555 vfs_register_upper_from_vp(struct vnode *vp, struct mount *ump, 556 struct mount_upper_node *upper) 557 { 558 struct mount *mp; 559 560 mp = atomic_load_ptr(&vp->v_mount); 561 if (mp == NULL) 562 return (NULL); 563 MNT_ILOCK(mp); 564 if (mp != vp->v_mount || 565 ((mp->mnt_kern_flag & (MNTK_UNMOUNT | MNTK_RECURSE)) != 0)) { 566 MNT_IUNLOCK(mp); 567 return (NULL); 568 } 569 KASSERT(ump != mp, ("upper and lower mounts are identical")); 570 upper->mp = ump; 571 MNT_REF(mp); 572 TAILQ_INSERT_TAIL(&mp->mnt_uppers, upper, mnt_upper_link); 573 MNT_IUNLOCK(mp); 574 return (mp); 575 } 576 577 /* 578 * Register upper mount ump to receive vnode unlink/reclaim 579 * notifications from lower mount mp. This registration will 580 * be tracked through mount_upper_node upper, which should be 581 * allocated by the caller and stored in per-mount data 582 * associated with mp. 583 * 584 * ump must already be registered as an upper mount of mp 585 * through a call to vfs_register_upper_from_vp(). 586 */ 587 void 588 vfs_register_for_notification(struct mount *mp, struct mount *ump, 589 struct mount_upper_node *upper) 590 { 591 upper->mp = ump; 592 MNT_ILOCK(mp); 593 TAILQ_INSERT_TAIL(&mp->mnt_notify, upper, mnt_upper_link); 594 MNT_IUNLOCK(mp); 595 } 596 597 static void 598 vfs_drain_upper_locked(struct mount *mp) 599 { 600 mtx_assert(MNT_MTX(mp), MA_OWNED); 601 while (mp->mnt_upper_pending != 0) { 602 mp->mnt_kern_flag |= MNTK_UPPER_WAITER; 603 msleep(&mp->mnt_uppers, MNT_MTX(mp), 0, "mntupw", 0); 604 } 605 } 606 607 /* 608 * Undo a previous call to vfs_register_for_notification(). 609 * The mount represented by upper must be currently registered 610 * as an upper mount for mp. 611 */ 612 void 613 vfs_unregister_for_notification(struct mount *mp, 614 struct mount_upper_node *upper) 615 { 616 MNT_ILOCK(mp); 617 vfs_drain_upper_locked(mp); 618 TAILQ_REMOVE(&mp->mnt_notify, upper, mnt_upper_link); 619 MNT_IUNLOCK(mp); 620 } 621 622 /* 623 * Undo a previous call to vfs_register_upper_from_vp(). 624 * This must be done before mp can be unmounted. 625 */ 626 void 627 vfs_unregister_upper(struct mount *mp, struct mount_upper_node *upper) 628 { 629 MNT_ILOCK(mp); 630 KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0, 631 ("registered upper with pending unmount")); 632 vfs_drain_upper_locked(mp); 633 TAILQ_REMOVE(&mp->mnt_uppers, upper, mnt_upper_link); 634 if ((mp->mnt_kern_flag & MNTK_TASKQUEUE_WAITER) != 0 && 635 TAILQ_EMPTY(&mp->mnt_uppers)) { 636 mp->mnt_kern_flag &= ~MNTK_TASKQUEUE_WAITER; 637 wakeup(&mp->mnt_taskqueue_link); 638 } 639 MNT_REL(mp); 640 MNT_IUNLOCK(mp); 641 } 642 643 void 644 vfs_rel(struct mount *mp) 645 { 646 struct mount_pcpu *mpcpu; 647 648 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 649 if (vfs_op_thread_enter(mp, mpcpu)) { 650 vfs_mp_count_sub_pcpu(mpcpu, ref, 1); 651 vfs_op_thread_exit(mp, mpcpu); 652 return; 653 } 654 655 MNT_ILOCK(mp); 656 MNT_REL(mp); 657 MNT_IUNLOCK(mp); 658 } 659 660 /* 661 * Allocate and initialize the mount point struct. 662 */ 663 struct mount * 664 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath, 665 struct ucred *cred) 666 { 667 struct mount *mp; 668 669 mp = uma_zalloc(mount_zone, M_WAITOK); 670 bzero(&mp->mnt_startzero, 671 __rangeof(struct mount, mnt_startzero, mnt_endzero)); 672 mp->mnt_kern_flag = 0; 673 mp->mnt_flag = 0; 674 mp->mnt_rootvnode = NULL; 675 mp->mnt_vnodecovered = NULL; 676 mp->mnt_op = NULL; 677 mp->mnt_vfc = NULL; 678 TAILQ_INIT(&mp->mnt_nvnodelist); 679 mp->mnt_nvnodelistsize = 0; 680 TAILQ_INIT(&mp->mnt_lazyvnodelist); 681 mp->mnt_lazyvnodelistsize = 0; 682 MPPASS(mp->mnt_ref == 0 && mp->mnt_lockref == 0 && 683 mp->mnt_writeopcount == 0, mp); 684 MPASSERT(mp->mnt_vfs_ops == 1, mp, 685 ("vfs_ops should be 1 but %d found", mp->mnt_vfs_ops)); 686 (void) vfs_busy(mp, MBF_NOWAIT); 687 atomic_add_acq_int(&vfsp->vfc_refcount, 1); 688 mp->mnt_op = vfsp->vfc_vfsops; 689 mp->mnt_vfc = vfsp; 690 mp->mnt_stat.f_type = vfsp->vfc_typenum; 691 mp->mnt_gen++; 692 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 693 mp->mnt_vnodecovered = vp; 694 mp->mnt_cred = crdup(cred); 695 mp->mnt_stat.f_owner = cred->cr_uid; 696 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 697 mp->mnt_iosize_max = DFLTPHYS; 698 #ifdef MAC 699 mac_mount_init(mp); 700 mac_mount_create(cred, mp); 701 #endif 702 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0); 703 mp->mnt_upper_pending = 0; 704 TAILQ_INIT(&mp->mnt_uppers); 705 TAILQ_INIT(&mp->mnt_notify); 706 mp->mnt_taskqueue_flags = 0; 707 mp->mnt_unmount_retries = 0; 708 return (mp); 709 } 710 711 /* 712 * Destroy the mount struct previously allocated by vfs_mount_alloc(). 713 */ 714 void 715 vfs_mount_destroy(struct mount *mp) 716 { 717 718 MPPASS(mp->mnt_vfs_ops != 0, mp); 719 720 vfs_assert_mount_counters(mp); 721 722 MNT_ILOCK(mp); 723 mp->mnt_kern_flag |= MNTK_REFEXPIRE; 724 if (mp->mnt_kern_flag & MNTK_MWAIT) { 725 mp->mnt_kern_flag &= ~MNTK_MWAIT; 726 wakeup(mp); 727 } 728 while (mp->mnt_ref) 729 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0); 730 KASSERT(mp->mnt_ref == 0, 731 ("%s: invalid refcount in the drain path @ %s:%d", __func__, 732 __FILE__, __LINE__)); 733 MPPASS(mp->mnt_writeopcount == 0, mp); 734 MPPASS(mp->mnt_secondary_writes == 0, mp); 735 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1); 736 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) { 737 struct vnode *vp; 738 739 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) 740 vn_printf(vp, "dangling vnode "); 741 panic("unmount: dangling vnode"); 742 } 743 KASSERT(mp->mnt_upper_pending == 0, ("mnt_upper_pending")); 744 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers")); 745 KASSERT(TAILQ_EMPTY(&mp->mnt_notify), ("mnt_notify")); 746 MPPASS(mp->mnt_nvnodelistsize == 0, mp); 747 MPPASS(mp->mnt_lazyvnodelistsize == 0, mp); 748 MPPASS(mp->mnt_lockref == 0, mp); 749 MNT_IUNLOCK(mp); 750 751 MPASSERT(mp->mnt_vfs_ops == 1, mp, 752 ("vfs_ops should be 1 but %d found", mp->mnt_vfs_ops)); 753 754 MPASSERT(mp->mnt_rootvnode == NULL, mp, 755 ("mount point still has a root vnode %p", mp->mnt_rootvnode)); 756 757 if (mp->mnt_vnodecovered != NULL) 758 vrele(mp->mnt_vnodecovered); 759 #ifdef MAC 760 mac_mount_destroy(mp); 761 #endif 762 if (mp->mnt_opt != NULL) 763 vfs_freeopts(mp->mnt_opt); 764 crfree(mp->mnt_cred); 765 uma_zfree(mount_zone, mp); 766 } 767 768 static bool 769 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error) 770 { 771 /* This is an upgrade of an exisiting mount. */ 772 if ((fsflags & MNT_UPDATE) != 0) 773 return (false); 774 /* This is already an R/O mount. */ 775 if ((fsflags & MNT_RDONLY) != 0) 776 return (false); 777 778 switch (error) { 779 case ENODEV: /* generic, geom, ... */ 780 case EACCES: /* cam/scsi, ... */ 781 case EROFS: /* md, mmcsd, ... */ 782 /* 783 * These errors can be returned by the storage layer to signal 784 * that the media is read-only. No harm in the R/O mount 785 * attempt if the error was returned for some other reason. 786 */ 787 return (true); 788 default: 789 return (false); 790 } 791 } 792 793 int 794 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions) 795 { 796 struct vfsoptlist *optlist; 797 struct vfsopt *opt, *tmp_opt; 798 char *fstype, *fspath, *errmsg; 799 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos; 800 bool autoro; 801 802 errmsg = fspath = NULL; 803 errmsg_len = fspathlen = 0; 804 errmsg_pos = -1; 805 autoro = default_autoro; 806 807 error = vfs_buildopts(fsoptions, &optlist); 808 if (error) 809 return (error); 810 811 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0) 812 errmsg_pos = vfs_getopt_pos(optlist, "errmsg"); 813 814 /* 815 * We need these two options before the others, 816 * and they are mandatory for any filesystem. 817 * Ensure they are NUL terminated as well. 818 */ 819 fstypelen = 0; 820 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 821 if (error || fstypelen <= 0 || fstype[fstypelen - 1] != '\0') { 822 error = EINVAL; 823 if (errmsg != NULL) 824 strncpy(errmsg, "Invalid fstype", errmsg_len); 825 goto bail; 826 } 827 fspathlen = 0; 828 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 829 if (error || fspathlen <= 0 || fspath[fspathlen - 1] != '\0') { 830 error = EINVAL; 831 if (errmsg != NULL) 832 strncpy(errmsg, "Invalid fspath", errmsg_len); 833 goto bail; 834 } 835 836 /* 837 * We need to see if we have the "update" option 838 * before we call vfs_domount(), since vfs_domount() has special 839 * logic based on MNT_UPDATE. This is very important 840 * when we want to update the root filesystem. 841 */ 842 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) { 843 int do_freeopt = 0; 844 845 if (strcmp(opt->name, "update") == 0) { 846 fsflags |= MNT_UPDATE; 847 do_freeopt = 1; 848 } 849 else if (strcmp(opt->name, "async") == 0) 850 fsflags |= MNT_ASYNC; 851 else if (strcmp(opt->name, "force") == 0) { 852 fsflags |= MNT_FORCE; 853 do_freeopt = 1; 854 } 855 else if (strcmp(opt->name, "reload") == 0) { 856 fsflags |= MNT_RELOAD; 857 do_freeopt = 1; 858 } 859 else if (strcmp(opt->name, "multilabel") == 0) 860 fsflags |= MNT_MULTILABEL; 861 else if (strcmp(opt->name, "noasync") == 0) 862 fsflags &= ~MNT_ASYNC; 863 else if (strcmp(opt->name, "noatime") == 0) 864 fsflags |= MNT_NOATIME; 865 else if (strcmp(opt->name, "atime") == 0) { 866 free(opt->name, M_MOUNT); 867 opt->name = strdup("nonoatime", M_MOUNT); 868 } 869 else if (strcmp(opt->name, "noclusterr") == 0) 870 fsflags |= MNT_NOCLUSTERR; 871 else if (strcmp(opt->name, "clusterr") == 0) { 872 free(opt->name, M_MOUNT); 873 opt->name = strdup("nonoclusterr", M_MOUNT); 874 } 875 else if (strcmp(opt->name, "noclusterw") == 0) 876 fsflags |= MNT_NOCLUSTERW; 877 else if (strcmp(opt->name, "clusterw") == 0) { 878 free(opt->name, M_MOUNT); 879 opt->name = strdup("nonoclusterw", M_MOUNT); 880 } 881 else if (strcmp(opt->name, "noexec") == 0) 882 fsflags |= MNT_NOEXEC; 883 else if (strcmp(opt->name, "exec") == 0) { 884 free(opt->name, M_MOUNT); 885 opt->name = strdup("nonoexec", M_MOUNT); 886 } 887 else if (strcmp(opt->name, "nosuid") == 0) 888 fsflags |= MNT_NOSUID; 889 else if (strcmp(opt->name, "suid") == 0) { 890 free(opt->name, M_MOUNT); 891 opt->name = strdup("nonosuid", M_MOUNT); 892 } 893 else if (strcmp(opt->name, "nosymfollow") == 0) 894 fsflags |= MNT_NOSYMFOLLOW; 895 else if (strcmp(opt->name, "symfollow") == 0) { 896 free(opt->name, M_MOUNT); 897 opt->name = strdup("nonosymfollow", M_MOUNT); 898 } 899 else if (strcmp(opt->name, "noro") == 0) { 900 fsflags &= ~MNT_RDONLY; 901 autoro = false; 902 } 903 else if (strcmp(opt->name, "rw") == 0) { 904 fsflags &= ~MNT_RDONLY; 905 autoro = false; 906 } 907 else if (strcmp(opt->name, "ro") == 0) { 908 fsflags |= MNT_RDONLY; 909 autoro = false; 910 } 911 else if (strcmp(opt->name, "rdonly") == 0) { 912 free(opt->name, M_MOUNT); 913 opt->name = strdup("ro", M_MOUNT); 914 fsflags |= MNT_RDONLY; 915 autoro = false; 916 } 917 else if (strcmp(opt->name, "autoro") == 0) { 918 do_freeopt = 1; 919 autoro = true; 920 } 921 else if (strcmp(opt->name, "suiddir") == 0) 922 fsflags |= MNT_SUIDDIR; 923 else if (strcmp(opt->name, "sync") == 0) 924 fsflags |= MNT_SYNCHRONOUS; 925 else if (strcmp(opt->name, "union") == 0) 926 fsflags |= MNT_UNION; 927 else if (strcmp(opt->name, "automounted") == 0) { 928 fsflags |= MNT_AUTOMOUNTED; 929 do_freeopt = 1; 930 } else if (strcmp(opt->name, "nocover") == 0) { 931 fsflags |= MNT_NOCOVER; 932 do_freeopt = 1; 933 } else if (strcmp(opt->name, "cover") == 0) { 934 fsflags &= ~MNT_NOCOVER; 935 do_freeopt = 1; 936 } else if (strcmp(opt->name, "emptydir") == 0) { 937 fsflags |= MNT_EMPTYDIR; 938 do_freeopt = 1; 939 } else if (strcmp(opt->name, "noemptydir") == 0) { 940 fsflags &= ~MNT_EMPTYDIR; 941 do_freeopt = 1; 942 } 943 if (do_freeopt) 944 vfs_freeopt(optlist, opt); 945 } 946 947 /* 948 * Be ultra-paranoid about making sure the type and fspath 949 * variables will fit in our mp buffers, including the 950 * terminating NUL. 951 */ 952 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) { 953 error = ENAMETOOLONG; 954 goto bail; 955 } 956 957 error = vfs_domount(td, fstype, fspath, fsflags, &optlist); 958 if (error == ENOENT) { 959 error = EINVAL; 960 if (errmsg != NULL) 961 strncpy(errmsg, "Invalid fstype", errmsg_len); 962 goto bail; 963 } 964 965 /* 966 * See if we can mount in the read-only mode if the error code suggests 967 * that it could be possible and the mount options allow for that. 968 * Never try it if "[no]{ro|rw}" has been explicitly requested and not 969 * overridden by "autoro". 970 */ 971 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) { 972 printf("%s: R/W mount failed, possibly R/O media," 973 " trying R/O mount\n", __func__); 974 fsflags |= MNT_RDONLY; 975 error = vfs_domount(td, fstype, fspath, fsflags, &optlist); 976 } 977 bail: 978 /* copyout the errmsg */ 979 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt) 980 && errmsg_len > 0 && errmsg != NULL) { 981 if (fsoptions->uio_segflg == UIO_SYSSPACE) { 982 bcopy(errmsg, 983 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 984 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 985 } else { 986 copyout(errmsg, 987 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 988 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 989 } 990 } 991 992 if (optlist != NULL) 993 vfs_freeopts(optlist); 994 return (error); 995 } 996 997 /* 998 * Old mount API. 999 */ 1000 #ifndef _SYS_SYSPROTO_H_ 1001 struct mount_args { 1002 char *type; 1003 char *path; 1004 int flags; 1005 caddr_t data; 1006 }; 1007 #endif 1008 /* ARGSUSED */ 1009 int 1010 sys_mount(struct thread *td, struct mount_args *uap) 1011 { 1012 char *fstype; 1013 struct vfsconf *vfsp = NULL; 1014 struct mntarg *ma = NULL; 1015 uint64_t flags; 1016 int error; 1017 1018 /* 1019 * Mount flags are now 64-bits. On 32-bit architectures only 1020 * 32-bits are passed in, but from here on everything handles 1021 * 64-bit flags correctly. 1022 */ 1023 flags = uap->flags; 1024 1025 AUDIT_ARG_FFLAGS(flags); 1026 1027 /* 1028 * Filter out MNT_ROOTFS. We do not want clients of mount() in 1029 * userspace to set this flag, but we must filter it out if we want 1030 * MNT_UPDATE on the root file system to work. 1031 * MNT_ROOTFS should only be set by the kernel when mounting its 1032 * root file system. 1033 */ 1034 flags &= ~MNT_ROOTFS; 1035 1036 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 1037 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 1038 if (error) { 1039 free(fstype, M_TEMP); 1040 return (error); 1041 } 1042 1043 AUDIT_ARG_TEXT(fstype); 1044 vfsp = vfs_byname_kld(fstype, td, &error); 1045 free(fstype, M_TEMP); 1046 if (vfsp == NULL) 1047 return (ENOENT); 1048 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 && 1049 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) || 1050 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 && 1051 vfsp->vfc_vfsops->vfs_cmount == NULL)) 1052 return (EOPNOTSUPP); 1053 1054 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN); 1055 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 1056 ma = mount_argb(ma, flags & MNT_RDONLY, "noro"); 1057 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid"); 1058 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec"); 1059 1060 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0) 1061 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags)); 1062 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags)); 1063 } 1064 1065 /* 1066 * vfs_domount_first(): first file system mount (not update) 1067 */ 1068 static int 1069 vfs_domount_first( 1070 struct thread *td, /* Calling thread. */ 1071 struct vfsconf *vfsp, /* File system type. */ 1072 char *fspath, /* Mount path. */ 1073 struct vnode *vp, /* Vnode to be covered. */ 1074 uint64_t fsflags, /* Flags common to all filesystems. */ 1075 struct vfsoptlist **optlist /* Options local to the filesystem. */ 1076 ) 1077 { 1078 struct vattr va; 1079 struct mount *mp; 1080 struct vnode *newdp, *rootvp; 1081 int error, error1; 1082 bool unmounted; 1083 1084 ASSERT_VOP_ELOCKED(vp, __func__); 1085 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here")); 1086 1087 /* 1088 * If the jail of the calling thread lacks permission for this type of 1089 * file system, or is trying to cover its own root, deny immediately. 1090 */ 1091 if (jailed(td->td_ucred) && (!prison_allow(td->td_ucred, 1092 vfsp->vfc_prison_flag) || vp == td->td_ucred->cr_prison->pr_root)) { 1093 vput(vp); 1094 return (EPERM); 1095 } 1096 1097 /* 1098 * If the user is not root, ensure that they own the directory 1099 * onto which we are attempting to mount. 1100 */ 1101 error = VOP_GETATTR(vp, &va, td->td_ucred); 1102 if (error == 0 && va.va_uid != td->td_ucred->cr_uid) 1103 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN); 1104 if (error == 0) 1105 error = vinvalbuf(vp, V_SAVE, 0, 0); 1106 if (error == 0 && vp->v_type != VDIR) 1107 error = ENOTDIR; 1108 if (error == 0 && (fsflags & MNT_EMPTYDIR) != 0) 1109 error = vfs_emptydir(vp); 1110 if (error == 0) { 1111 VI_LOCK(vp); 1112 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL) 1113 vp->v_iflag |= VI_MOUNT; 1114 else 1115 error = EBUSY; 1116 VI_UNLOCK(vp); 1117 } 1118 if (error != 0) { 1119 vput(vp); 1120 return (error); 1121 } 1122 vn_seqc_write_begin(vp); 1123 VOP_UNLOCK(vp); 1124 1125 /* Allocate and initialize the filesystem. */ 1126 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred); 1127 /* XXXMAC: pass to vfs_mount_alloc? */ 1128 mp->mnt_optnew = *optlist; 1129 /* Set the mount level flags. */ 1130 mp->mnt_flag = (fsflags & 1131 (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY | MNT_FORCE)); 1132 1133 /* 1134 * Mount the filesystem. 1135 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 1136 * get. No freeing of cn_pnbuf. 1137 */ 1138 error1 = 0; 1139 unmounted = true; 1140 if ((error = VFS_MOUNT(mp)) != 0 || 1141 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 || 1142 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) { 1143 rootvp = NULL; 1144 if (error1 != 0) { 1145 MPASS(error == 0); 1146 rootvp = vfs_cache_root_clear(mp); 1147 if (rootvp != NULL) { 1148 vhold(rootvp); 1149 vrele(rootvp); 1150 } 1151 (void)vn_start_write(NULL, &mp, V_WAIT); 1152 MNT_ILOCK(mp); 1153 mp->mnt_kern_flag |= MNTK_UNMOUNT | MNTK_UNMOUNTF; 1154 MNT_IUNLOCK(mp); 1155 VFS_PURGE(mp); 1156 error = VFS_UNMOUNT(mp, 0); 1157 vn_finished_write(mp); 1158 if (error != 0) { 1159 printf( 1160 "failed post-mount (%d): rollback unmount returned %d\n", 1161 error1, error); 1162 unmounted = false; 1163 } 1164 error = error1; 1165 } 1166 vfs_unbusy(mp); 1167 mp->mnt_vnodecovered = NULL; 1168 if (unmounted) { 1169 /* XXXKIB wait for mnt_lockref drain? */ 1170 vfs_mount_destroy(mp); 1171 } 1172 VI_LOCK(vp); 1173 vp->v_iflag &= ~VI_MOUNT; 1174 VI_UNLOCK(vp); 1175 if (rootvp != NULL) { 1176 vn_seqc_write_end(rootvp); 1177 vdrop(rootvp); 1178 } 1179 vn_seqc_write_end(vp); 1180 vrele(vp); 1181 return (error); 1182 } 1183 vn_seqc_write_begin(newdp); 1184 VOP_UNLOCK(newdp); 1185 1186 if (mp->mnt_opt != NULL) 1187 vfs_freeopts(mp->mnt_opt); 1188 mp->mnt_opt = mp->mnt_optnew; 1189 *optlist = NULL; 1190 1191 /* 1192 * Prevent external consumers of mount options from reading mnt_optnew. 1193 */ 1194 mp->mnt_optnew = NULL; 1195 1196 MNT_ILOCK(mp); 1197 if ((mp->mnt_flag & MNT_ASYNC) != 0 && 1198 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0) 1199 mp->mnt_kern_flag |= MNTK_ASYNC; 1200 else 1201 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1202 MNT_IUNLOCK(mp); 1203 1204 VI_LOCK(vp); 1205 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT); 1206 vp->v_mountedhere = mp; 1207 VI_UNLOCK(vp); 1208 cache_purge(vp); 1209 1210 /* 1211 * We need to lock both vnodes. 1212 * 1213 * Use vn_lock_pair to avoid establishing an ordering between vnodes 1214 * from different filesystems. 1215 */ 1216 vn_lock_pair(vp, false, newdp, false); 1217 1218 VI_LOCK(vp); 1219 vp->v_iflag &= ~VI_MOUNT; 1220 VI_UNLOCK(vp); 1221 /* Place the new filesystem at the end of the mount list. */ 1222 mtx_lock(&mountlist_mtx); 1223 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1224 mtx_unlock(&mountlist_mtx); 1225 vfs_event_signal(NULL, VQ_MOUNT, 0); 1226 VOP_UNLOCK(vp); 1227 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td); 1228 VOP_UNLOCK(newdp); 1229 mount_devctl_event("MOUNT", mp, false); 1230 mountcheckdirs(vp, newdp); 1231 vn_seqc_write_end(vp); 1232 vn_seqc_write_end(newdp); 1233 vrele(newdp); 1234 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1235 vfs_allocate_syncvnode(mp); 1236 vfs_op_exit(mp); 1237 vfs_unbusy(mp); 1238 return (0); 1239 } 1240 1241 /* 1242 * vfs_domount_update(): update of mounted file system 1243 */ 1244 static int 1245 vfs_domount_update( 1246 struct thread *td, /* Calling thread. */ 1247 struct vnode *vp, /* Mount point vnode. */ 1248 uint64_t fsflags, /* Flags common to all filesystems. */ 1249 struct vfsoptlist **optlist /* Options local to the filesystem. */ 1250 ) 1251 { 1252 struct export_args export; 1253 struct o2export_args o2export; 1254 struct vnode *rootvp; 1255 void *bufp; 1256 struct mount *mp; 1257 int error, export_error, i, len; 1258 uint64_t flag; 1259 gid_t *grps; 1260 1261 ASSERT_VOP_ELOCKED(vp, __func__); 1262 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here")); 1263 mp = vp->v_mount; 1264 1265 if ((vp->v_vflag & VV_ROOT) == 0) { 1266 if (vfs_copyopt(*optlist, "export", &export, sizeof(export)) 1267 == 0) 1268 error = EXDEV; 1269 else 1270 error = EINVAL; 1271 vput(vp); 1272 return (error); 1273 } 1274 1275 /* 1276 * We only allow the filesystem to be reloaded if it 1277 * is currently mounted read-only. 1278 */ 1279 flag = mp->mnt_flag; 1280 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) { 1281 vput(vp); 1282 return (EOPNOTSUPP); /* Needs translation */ 1283 } 1284 /* 1285 * Only privileged root, or (if MNT_USER is set) the user that 1286 * did the original mount is permitted to update it. 1287 */ 1288 error = vfs_suser(mp, td); 1289 if (error != 0) { 1290 vput(vp); 1291 return (error); 1292 } 1293 if (vfs_busy(mp, MBF_NOWAIT)) { 1294 vput(vp); 1295 return (EBUSY); 1296 } 1297 VI_LOCK(vp); 1298 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) { 1299 VI_UNLOCK(vp); 1300 vfs_unbusy(mp); 1301 vput(vp); 1302 return (EBUSY); 1303 } 1304 vp->v_iflag |= VI_MOUNT; 1305 VI_UNLOCK(vp); 1306 VOP_UNLOCK(vp); 1307 1308 vfs_op_enter(mp); 1309 vn_seqc_write_begin(vp); 1310 1311 rootvp = NULL; 1312 MNT_ILOCK(mp); 1313 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) { 1314 MNT_IUNLOCK(mp); 1315 error = EBUSY; 1316 goto end; 1317 } 1318 mp->mnt_flag &= ~MNT_UPDATEMASK; 1319 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | 1320 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY); 1321 if ((mp->mnt_flag & MNT_ASYNC) == 0) 1322 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1323 rootvp = vfs_cache_root_clear(mp); 1324 MNT_IUNLOCK(mp); 1325 mp->mnt_optnew = *optlist; 1326 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 1327 1328 /* 1329 * Mount the filesystem. 1330 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 1331 * get. No freeing of cn_pnbuf. 1332 */ 1333 error = VFS_MOUNT(mp); 1334 1335 export_error = 0; 1336 /* Process the export option. */ 1337 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp, 1338 &len) == 0) { 1339 /* Assume that there is only 1 ABI for each length. */ 1340 switch (len) { 1341 case (sizeof(struct oexport_args)): 1342 bzero(&o2export, sizeof(o2export)); 1343 /* FALLTHROUGH */ 1344 case (sizeof(o2export)): 1345 bcopy(bufp, &o2export, len); 1346 export.ex_flags = (uint64_t)o2export.ex_flags; 1347 export.ex_root = o2export.ex_root; 1348 export.ex_uid = o2export.ex_anon.cr_uid; 1349 export.ex_groups = NULL; 1350 export.ex_ngroups = o2export.ex_anon.cr_ngroups; 1351 if (export.ex_ngroups > 0) { 1352 if (export.ex_ngroups <= XU_NGROUPS) { 1353 export.ex_groups = malloc( 1354 export.ex_ngroups * sizeof(gid_t), 1355 M_TEMP, M_WAITOK); 1356 for (i = 0; i < export.ex_ngroups; i++) 1357 export.ex_groups[i] = 1358 o2export.ex_anon.cr_groups[i]; 1359 } else 1360 export_error = EINVAL; 1361 } else if (export.ex_ngroups < 0) 1362 export_error = EINVAL; 1363 export.ex_addr = o2export.ex_addr; 1364 export.ex_addrlen = o2export.ex_addrlen; 1365 export.ex_mask = o2export.ex_mask; 1366 export.ex_masklen = o2export.ex_masklen; 1367 export.ex_indexfile = o2export.ex_indexfile; 1368 export.ex_numsecflavors = o2export.ex_numsecflavors; 1369 if (export.ex_numsecflavors < MAXSECFLAVORS) { 1370 for (i = 0; i < export.ex_numsecflavors; i++) 1371 export.ex_secflavors[i] = 1372 o2export.ex_secflavors[i]; 1373 } else 1374 export_error = EINVAL; 1375 if (export_error == 0) 1376 export_error = vfs_export(mp, &export); 1377 free(export.ex_groups, M_TEMP); 1378 break; 1379 case (sizeof(export)): 1380 bcopy(bufp, &export, len); 1381 grps = NULL; 1382 if (export.ex_ngroups > 0) { 1383 if (export.ex_ngroups <= NGROUPS_MAX) { 1384 grps = malloc(export.ex_ngroups * 1385 sizeof(gid_t), M_TEMP, M_WAITOK); 1386 export_error = copyin(export.ex_groups, 1387 grps, export.ex_ngroups * 1388 sizeof(gid_t)); 1389 if (export_error == 0) 1390 export.ex_groups = grps; 1391 } else 1392 export_error = EINVAL; 1393 } else if (export.ex_ngroups == 0) 1394 export.ex_groups = NULL; 1395 else 1396 export_error = EINVAL; 1397 if (export_error == 0) 1398 export_error = vfs_export(mp, &export); 1399 free(grps, M_TEMP); 1400 break; 1401 default: 1402 export_error = EINVAL; 1403 break; 1404 } 1405 } 1406 1407 MNT_ILOCK(mp); 1408 if (error == 0) { 1409 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | 1410 MNT_SNAPSHOT); 1411 } else { 1412 /* 1413 * If we fail, restore old mount flags. MNT_QUOTA is special, 1414 * because it is not part of MNT_UPDATEMASK, but it could have 1415 * changed in the meantime if quotactl(2) was called. 1416 * All in all we want current value of MNT_QUOTA, not the old 1417 * one. 1418 */ 1419 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA); 1420 } 1421 if ((mp->mnt_flag & MNT_ASYNC) != 0 && 1422 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0) 1423 mp->mnt_kern_flag |= MNTK_ASYNC; 1424 else 1425 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1426 MNT_IUNLOCK(mp); 1427 1428 if (error != 0) 1429 goto end; 1430 1431 mount_devctl_event("REMOUNT", mp, true); 1432 if (mp->mnt_opt != NULL) 1433 vfs_freeopts(mp->mnt_opt); 1434 mp->mnt_opt = mp->mnt_optnew; 1435 *optlist = NULL; 1436 (void)VFS_STATFS(mp, &mp->mnt_stat); 1437 /* 1438 * Prevent external consumers of mount options from reading 1439 * mnt_optnew. 1440 */ 1441 mp->mnt_optnew = NULL; 1442 1443 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1444 vfs_allocate_syncvnode(mp); 1445 else 1446 vfs_deallocate_syncvnode(mp); 1447 end: 1448 vfs_op_exit(mp); 1449 if (rootvp != NULL) { 1450 vn_seqc_write_end(rootvp); 1451 vrele(rootvp); 1452 } 1453 vn_seqc_write_end(vp); 1454 vfs_unbusy(mp); 1455 VI_LOCK(vp); 1456 vp->v_iflag &= ~VI_MOUNT; 1457 VI_UNLOCK(vp); 1458 vrele(vp); 1459 return (error != 0 ? error : export_error); 1460 } 1461 1462 /* 1463 * vfs_domount(): actually attempt a filesystem mount. 1464 */ 1465 static int 1466 vfs_domount( 1467 struct thread *td, /* Calling thread. */ 1468 const char *fstype, /* Filesystem type. */ 1469 char *fspath, /* Mount path. */ 1470 uint64_t fsflags, /* Flags common to all filesystems. */ 1471 struct vfsoptlist **optlist /* Options local to the filesystem. */ 1472 ) 1473 { 1474 struct vfsconf *vfsp; 1475 struct nameidata nd; 1476 struct vnode *vp; 1477 char *pathbuf; 1478 int error; 1479 1480 /* 1481 * Be ultra-paranoid about making sure the type and fspath 1482 * variables will fit in our mp buffers, including the 1483 * terminating NUL. 1484 */ 1485 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 1486 return (ENAMETOOLONG); 1487 1488 if (jailed(td->td_ucred) || usermount == 0) { 1489 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0) 1490 return (error); 1491 } 1492 1493 /* 1494 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 1495 */ 1496 if (fsflags & MNT_EXPORTED) { 1497 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED); 1498 if (error) 1499 return (error); 1500 } 1501 if (fsflags & MNT_SUIDDIR) { 1502 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR); 1503 if (error) 1504 return (error); 1505 } 1506 /* 1507 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users. 1508 */ 1509 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) { 1510 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0) 1511 fsflags |= MNT_NOSUID | MNT_USER; 1512 } 1513 1514 /* Load KLDs before we lock the covered vnode to avoid reversals. */ 1515 vfsp = NULL; 1516 if ((fsflags & MNT_UPDATE) == 0) { 1517 /* Don't try to load KLDs if we're mounting the root. */ 1518 if (fsflags & MNT_ROOTFS) { 1519 if ((vfsp = vfs_byname(fstype)) == NULL) 1520 return (ENODEV); 1521 } else { 1522 if ((vfsp = vfs_byname_kld(fstype, td, &error)) == NULL) 1523 return (error); 1524 } 1525 } 1526 1527 /* 1528 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE. 1529 */ 1530 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE, 1531 fspath); 1532 error = namei(&nd); 1533 if (error != 0) 1534 return (error); 1535 NDFREE_PNBUF(&nd); 1536 vp = nd.ni_vp; 1537 if ((fsflags & MNT_UPDATE) == 0) { 1538 if ((vp->v_vflag & VV_ROOT) != 0 && 1539 (fsflags & MNT_NOCOVER) != 0) { 1540 vput(vp); 1541 return (EBUSY); 1542 } 1543 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1544 strcpy(pathbuf, fspath); 1545 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN); 1546 if (error == 0) { 1547 error = vfs_domount_first(td, vfsp, pathbuf, vp, 1548 fsflags, optlist); 1549 } 1550 free(pathbuf, M_TEMP); 1551 } else 1552 error = vfs_domount_update(td, vp, fsflags, optlist); 1553 1554 return (error); 1555 } 1556 1557 /* 1558 * Unmount a filesystem. 1559 * 1560 * Note: unmount takes a path to the vnode mounted on as argument, not 1561 * special file (as before). 1562 */ 1563 #ifndef _SYS_SYSPROTO_H_ 1564 struct unmount_args { 1565 char *path; 1566 int flags; 1567 }; 1568 #endif 1569 /* ARGSUSED */ 1570 int 1571 sys_unmount(struct thread *td, struct unmount_args *uap) 1572 { 1573 1574 return (kern_unmount(td, uap->path, uap->flags)); 1575 } 1576 1577 int 1578 kern_unmount(struct thread *td, const char *path, int flags) 1579 { 1580 struct nameidata nd; 1581 struct mount *mp; 1582 char *fsidbuf, *pathbuf; 1583 fsid_t fsid; 1584 int error; 1585 1586 AUDIT_ARG_VALUE(flags); 1587 if (jailed(td->td_ucred) || usermount == 0) { 1588 error = priv_check(td, PRIV_VFS_UNMOUNT); 1589 if (error) 1590 return (error); 1591 } 1592 1593 if (flags & MNT_BYFSID) { 1594 fsidbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1595 error = copyinstr(path, fsidbuf, MNAMELEN, NULL); 1596 if (error) { 1597 free(fsidbuf, M_TEMP); 1598 return (error); 1599 } 1600 1601 AUDIT_ARG_TEXT(fsidbuf); 1602 /* Decode the filesystem ID. */ 1603 if (sscanf(fsidbuf, "FSID:%d:%d", &fsid.val[0], &fsid.val[1]) != 2) { 1604 free(fsidbuf, M_TEMP); 1605 return (EINVAL); 1606 } 1607 1608 mp = vfs_getvfs(&fsid); 1609 free(fsidbuf, M_TEMP); 1610 if (mp == NULL) { 1611 return (ENOENT); 1612 } 1613 } else { 1614 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1615 error = copyinstr(path, pathbuf, MNAMELEN, NULL); 1616 if (error) { 1617 free(pathbuf, M_TEMP); 1618 return (error); 1619 } 1620 1621 /* 1622 * Try to find global path for path argument. 1623 */ 1624 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 1625 UIO_SYSSPACE, pathbuf); 1626 if (namei(&nd) == 0) { 1627 NDFREE_PNBUF(&nd); 1628 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf, 1629 MNAMELEN); 1630 if (error == 0) 1631 vput(nd.ni_vp); 1632 } 1633 mtx_lock(&mountlist_mtx); 1634 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1635 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) { 1636 vfs_ref(mp); 1637 break; 1638 } 1639 } 1640 mtx_unlock(&mountlist_mtx); 1641 free(pathbuf, M_TEMP); 1642 if (mp == NULL) { 1643 /* 1644 * Previously we returned ENOENT for a nonexistent path and 1645 * EINVAL for a non-mountpoint. We cannot tell these apart 1646 * now, so in the !MNT_BYFSID case return the more likely 1647 * EINVAL for compatibility. 1648 */ 1649 return (EINVAL); 1650 } 1651 } 1652 1653 /* 1654 * Don't allow unmounting the root filesystem. 1655 */ 1656 if (mp->mnt_flag & MNT_ROOTFS) { 1657 vfs_rel(mp); 1658 return (EINVAL); 1659 } 1660 error = dounmount(mp, flags, td); 1661 return (error); 1662 } 1663 1664 /* 1665 * Return error if any of the vnodes, ignoring the root vnode 1666 * and the syncer vnode, have non-zero usecount. 1667 * 1668 * This function is purely advisory - it can return false positives 1669 * and negatives. 1670 */ 1671 static int 1672 vfs_check_usecounts(struct mount *mp) 1673 { 1674 struct vnode *vp, *mvp; 1675 1676 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 1677 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON && 1678 vp->v_usecount != 0) { 1679 VI_UNLOCK(vp); 1680 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 1681 return (EBUSY); 1682 } 1683 VI_UNLOCK(vp); 1684 } 1685 1686 return (0); 1687 } 1688 1689 static void 1690 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags) 1691 { 1692 1693 mtx_assert(MNT_MTX(mp), MA_OWNED); 1694 mp->mnt_kern_flag &= ~mntkflags; 1695 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) { 1696 mp->mnt_kern_flag &= ~MNTK_MWAIT; 1697 wakeup(mp); 1698 } 1699 vfs_op_exit_locked(mp); 1700 MNT_IUNLOCK(mp); 1701 if (coveredvp != NULL) { 1702 VOP_UNLOCK(coveredvp); 1703 vdrop(coveredvp); 1704 } 1705 vn_finished_write(mp); 1706 vfs_rel(mp); 1707 } 1708 1709 /* 1710 * There are various reference counters associated with the mount point. 1711 * Normally it is permitted to modify them without taking the mnt ilock, 1712 * but this behavior can be temporarily disabled if stable value is needed 1713 * or callers are expected to block (e.g. to not allow new users during 1714 * forced unmount). 1715 */ 1716 void 1717 vfs_op_enter(struct mount *mp) 1718 { 1719 struct mount_pcpu *mpcpu; 1720 int cpu; 1721 1722 MNT_ILOCK(mp); 1723 mp->mnt_vfs_ops++; 1724 if (mp->mnt_vfs_ops > 1) { 1725 MNT_IUNLOCK(mp); 1726 return; 1727 } 1728 vfs_op_barrier_wait(mp); 1729 CPU_FOREACH(cpu) { 1730 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1731 1732 mp->mnt_ref += mpcpu->mntp_ref; 1733 mpcpu->mntp_ref = 0; 1734 1735 mp->mnt_lockref += mpcpu->mntp_lockref; 1736 mpcpu->mntp_lockref = 0; 1737 1738 mp->mnt_writeopcount += mpcpu->mntp_writeopcount; 1739 mpcpu->mntp_writeopcount = 0; 1740 } 1741 MPASSERT(mp->mnt_ref > 0 && mp->mnt_lockref >= 0 && 1742 mp->mnt_writeopcount >= 0, mp, 1743 ("invalid count(s): ref %d lockref %d writeopcount %d", 1744 mp->mnt_ref, mp->mnt_lockref, mp->mnt_writeopcount)); 1745 MNT_IUNLOCK(mp); 1746 vfs_assert_mount_counters(mp); 1747 } 1748 1749 void 1750 vfs_op_exit_locked(struct mount *mp) 1751 { 1752 1753 mtx_assert(MNT_MTX(mp), MA_OWNED); 1754 1755 MPASSERT(mp->mnt_vfs_ops > 0, mp, 1756 ("invalid vfs_ops count %d", mp->mnt_vfs_ops)); 1757 MPASSERT(mp->mnt_vfs_ops > 1 || 1758 (mp->mnt_kern_flag & (MNTK_UNMOUNT | MNTK_SUSPEND)) == 0, mp, 1759 ("vfs_ops too low %d in unmount or suspend", mp->mnt_vfs_ops)); 1760 mp->mnt_vfs_ops--; 1761 } 1762 1763 void 1764 vfs_op_exit(struct mount *mp) 1765 { 1766 1767 MNT_ILOCK(mp); 1768 vfs_op_exit_locked(mp); 1769 MNT_IUNLOCK(mp); 1770 } 1771 1772 struct vfs_op_barrier_ipi { 1773 struct mount *mp; 1774 struct smp_rendezvous_cpus_retry_arg srcra; 1775 }; 1776 1777 static void 1778 vfs_op_action_func(void *arg) 1779 { 1780 struct vfs_op_barrier_ipi *vfsopipi; 1781 struct mount *mp; 1782 1783 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra); 1784 mp = vfsopipi->mp; 1785 1786 if (!vfs_op_thread_entered(mp)) 1787 smp_rendezvous_cpus_done(arg); 1788 } 1789 1790 static void 1791 vfs_op_wait_func(void *arg, int cpu) 1792 { 1793 struct vfs_op_barrier_ipi *vfsopipi; 1794 struct mount *mp; 1795 struct mount_pcpu *mpcpu; 1796 1797 vfsopipi = __containerof(arg, struct vfs_op_barrier_ipi, srcra); 1798 mp = vfsopipi->mp; 1799 1800 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1801 while (atomic_load_int(&mpcpu->mntp_thread_in_ops)) 1802 cpu_spinwait(); 1803 } 1804 1805 void 1806 vfs_op_barrier_wait(struct mount *mp) 1807 { 1808 struct vfs_op_barrier_ipi vfsopipi; 1809 1810 vfsopipi.mp = mp; 1811 1812 smp_rendezvous_cpus_retry(all_cpus, 1813 smp_no_rendezvous_barrier, 1814 vfs_op_action_func, 1815 smp_no_rendezvous_barrier, 1816 vfs_op_wait_func, 1817 &vfsopipi.srcra); 1818 } 1819 1820 #ifdef DIAGNOSTIC 1821 void 1822 vfs_assert_mount_counters(struct mount *mp) 1823 { 1824 struct mount_pcpu *mpcpu; 1825 int cpu; 1826 1827 if (mp->mnt_vfs_ops == 0) 1828 return; 1829 1830 CPU_FOREACH(cpu) { 1831 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1832 if (mpcpu->mntp_ref != 0 || 1833 mpcpu->mntp_lockref != 0 || 1834 mpcpu->mntp_writeopcount != 0) 1835 vfs_dump_mount_counters(mp); 1836 } 1837 } 1838 1839 void 1840 vfs_dump_mount_counters(struct mount *mp) 1841 { 1842 struct mount_pcpu *mpcpu; 1843 int ref, lockref, writeopcount; 1844 int cpu; 1845 1846 printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops); 1847 1848 printf(" ref : "); 1849 ref = mp->mnt_ref; 1850 CPU_FOREACH(cpu) { 1851 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1852 printf("%d ", mpcpu->mntp_ref); 1853 ref += mpcpu->mntp_ref; 1854 } 1855 printf("\n"); 1856 printf(" lockref : "); 1857 lockref = mp->mnt_lockref; 1858 CPU_FOREACH(cpu) { 1859 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1860 printf("%d ", mpcpu->mntp_lockref); 1861 lockref += mpcpu->mntp_lockref; 1862 } 1863 printf("\n"); 1864 printf("writeopcount: "); 1865 writeopcount = mp->mnt_writeopcount; 1866 CPU_FOREACH(cpu) { 1867 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1868 printf("%d ", mpcpu->mntp_writeopcount); 1869 writeopcount += mpcpu->mntp_writeopcount; 1870 } 1871 printf("\n"); 1872 1873 printf("counter struct total\n"); 1874 printf("ref %-5d %-5d\n", mp->mnt_ref, ref); 1875 printf("lockref %-5d %-5d\n", mp->mnt_lockref, lockref); 1876 printf("writeopcount %-5d %-5d\n", mp->mnt_writeopcount, writeopcount); 1877 1878 panic("invalid counts on struct mount"); 1879 } 1880 #endif 1881 1882 int 1883 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which) 1884 { 1885 struct mount_pcpu *mpcpu; 1886 int cpu, sum; 1887 1888 switch (which) { 1889 case MNT_COUNT_REF: 1890 sum = mp->mnt_ref; 1891 break; 1892 case MNT_COUNT_LOCKREF: 1893 sum = mp->mnt_lockref; 1894 break; 1895 case MNT_COUNT_WRITEOPCOUNT: 1896 sum = mp->mnt_writeopcount; 1897 break; 1898 } 1899 1900 CPU_FOREACH(cpu) { 1901 mpcpu = vfs_mount_pcpu_remote(mp, cpu); 1902 switch (which) { 1903 case MNT_COUNT_REF: 1904 sum += mpcpu->mntp_ref; 1905 break; 1906 case MNT_COUNT_LOCKREF: 1907 sum += mpcpu->mntp_lockref; 1908 break; 1909 case MNT_COUNT_WRITEOPCOUNT: 1910 sum += mpcpu->mntp_writeopcount; 1911 break; 1912 } 1913 } 1914 return (sum); 1915 } 1916 1917 static bool 1918 deferred_unmount_enqueue(struct mount *mp, uint64_t flags, bool requeue, 1919 int timeout_ticks) 1920 { 1921 bool enqueued; 1922 1923 enqueued = false; 1924 mtx_lock(&deferred_unmount_lock); 1925 if ((mp->mnt_taskqueue_flags & MNT_DEFERRED) == 0 || requeue) { 1926 mp->mnt_taskqueue_flags = flags | MNT_DEFERRED; 1927 STAILQ_INSERT_TAIL(&deferred_unmount_list, mp, 1928 mnt_taskqueue_link); 1929 enqueued = true; 1930 } 1931 mtx_unlock(&deferred_unmount_lock); 1932 1933 if (enqueued) { 1934 taskqueue_enqueue_timeout(taskqueue_deferred_unmount, 1935 &deferred_unmount_task, timeout_ticks); 1936 } 1937 1938 return (enqueued); 1939 } 1940 1941 /* 1942 * Taskqueue handler for processing async/recursive unmounts 1943 */ 1944 static void 1945 vfs_deferred_unmount(void *argi __unused, int pending __unused) 1946 { 1947 STAILQ_HEAD(, mount) local_unmounts; 1948 uint64_t flags; 1949 struct mount *mp, *tmp; 1950 int error; 1951 unsigned int retries; 1952 bool unmounted; 1953 1954 STAILQ_INIT(&local_unmounts); 1955 mtx_lock(&deferred_unmount_lock); 1956 STAILQ_CONCAT(&local_unmounts, &deferred_unmount_list); 1957 mtx_unlock(&deferred_unmount_lock); 1958 1959 STAILQ_FOREACH_SAFE(mp, &local_unmounts, mnt_taskqueue_link, tmp) { 1960 flags = mp->mnt_taskqueue_flags; 1961 KASSERT((flags & MNT_DEFERRED) != 0, 1962 ("taskqueue unmount without MNT_DEFERRED")); 1963 error = dounmount(mp, flags, curthread); 1964 if (error != 0) { 1965 MNT_ILOCK(mp); 1966 unmounted = ((mp->mnt_kern_flag & MNTK_REFEXPIRE) != 0); 1967 MNT_IUNLOCK(mp); 1968 1969 /* 1970 * The deferred unmount thread is the only thread that 1971 * modifies the retry counts, so locking/atomics aren't 1972 * needed here. 1973 */ 1974 retries = (mp->mnt_unmount_retries)++; 1975 deferred_unmount_total_retries++; 1976 if (!unmounted && retries < deferred_unmount_retry_limit) { 1977 deferred_unmount_enqueue(mp, flags, true, 1978 -deferred_unmount_retry_delay_hz); 1979 } else { 1980 if (retries >= deferred_unmount_retry_limit) { 1981 printf("giving up on deferred unmount " 1982 "of %s after %d retries, error %d\n", 1983 mp->mnt_stat.f_mntonname, retries, error); 1984 } 1985 vfs_rel(mp); 1986 } 1987 } 1988 } 1989 } 1990 1991 /* 1992 * Do the actual filesystem unmount. 1993 */ 1994 int 1995 dounmount(struct mount *mp, uint64_t flags, struct thread *td) 1996 { 1997 struct mount_upper_node *upper; 1998 struct vnode *coveredvp, *rootvp; 1999 int error; 2000 uint64_t async_flag; 2001 int mnt_gen_r; 2002 unsigned int retries; 2003 2004 KASSERT((flags & MNT_DEFERRED) == 0 || 2005 (flags & (MNT_RECURSE | MNT_FORCE)) == (MNT_RECURSE | MNT_FORCE), 2006 ("MNT_DEFERRED requires MNT_RECURSE | MNT_FORCE")); 2007 2008 /* 2009 * If the caller has explicitly requested the unmount to be handled by 2010 * the taskqueue and we're not already in taskqueue context, queue 2011 * up the unmount request and exit. This is done prior to any 2012 * credential checks; MNT_DEFERRED should be used only for kernel- 2013 * initiated unmounts and will therefore be processed with the 2014 * (kernel) credentials of the taskqueue thread. Still, callers 2015 * should be sure this is the behavior they want. 2016 */ 2017 if ((flags & MNT_DEFERRED) != 0 && 2018 taskqueue_member(taskqueue_deferred_unmount, curthread) == 0) { 2019 if (!deferred_unmount_enqueue(mp, flags, false, 0)) 2020 vfs_rel(mp); 2021 return (EINPROGRESS); 2022 } 2023 2024 /* 2025 * Only privileged root, or (if MNT_USER is set) the user that did the 2026 * original mount is permitted to unmount this filesystem. 2027 * This check should be made prior to queueing up any recursive 2028 * unmounts of upper filesystems. Those unmounts will be executed 2029 * with kernel thread credentials and are expected to succeed, so 2030 * we must at least ensure the originating context has sufficient 2031 * privilege to unmount the base filesystem before proceeding with 2032 * the uppers. 2033 */ 2034 error = vfs_suser(mp, td); 2035 if (error != 0) { 2036 KASSERT((flags & MNT_DEFERRED) == 0, 2037 ("taskqueue unmount with insufficient privilege")); 2038 vfs_rel(mp); 2039 return (error); 2040 } 2041 2042 if (recursive_forced_unmount && ((flags & MNT_FORCE) != 0)) 2043 flags |= MNT_RECURSE; 2044 2045 if ((flags & MNT_RECURSE) != 0) { 2046 KASSERT((flags & MNT_FORCE) != 0, 2047 ("MNT_RECURSE requires MNT_FORCE")); 2048 2049 MNT_ILOCK(mp); 2050 /* 2051 * Set MNTK_RECURSE to prevent new upper mounts from being 2052 * added, and note that an operation on the uppers list is in 2053 * progress. This will ensure that unregistration from the 2054 * uppers list, and therefore any pending unmount of the upper 2055 * FS, can't complete until after we finish walking the list. 2056 */ 2057 mp->mnt_kern_flag |= MNTK_RECURSE; 2058 mp->mnt_upper_pending++; 2059 TAILQ_FOREACH(upper, &mp->mnt_uppers, mnt_upper_link) { 2060 retries = upper->mp->mnt_unmount_retries; 2061 if (retries > deferred_unmount_retry_limit) { 2062 error = EBUSY; 2063 continue; 2064 } 2065 MNT_IUNLOCK(mp); 2066 2067 vfs_ref(upper->mp); 2068 if (!deferred_unmount_enqueue(upper->mp, flags, 2069 false, 0)) 2070 vfs_rel(upper->mp); 2071 MNT_ILOCK(mp); 2072 } 2073 mp->mnt_upper_pending--; 2074 if ((mp->mnt_kern_flag & MNTK_UPPER_WAITER) != 0 && 2075 mp->mnt_upper_pending == 0) { 2076 mp->mnt_kern_flag &= ~MNTK_UPPER_WAITER; 2077 wakeup(&mp->mnt_uppers); 2078 } 2079 2080 /* 2081 * If we're not on the taskqueue, wait until the uppers list 2082 * is drained before proceeding with unmount. Otherwise, if 2083 * we are on the taskqueue and there are still pending uppers, 2084 * just re-enqueue on the end of the taskqueue. 2085 */ 2086 if ((flags & MNT_DEFERRED) == 0) { 2087 while (error == 0 && !TAILQ_EMPTY(&mp->mnt_uppers)) { 2088 mp->mnt_kern_flag |= MNTK_TASKQUEUE_WAITER; 2089 error = msleep(&mp->mnt_taskqueue_link, 2090 MNT_MTX(mp), PCATCH, "umntqw", 0); 2091 } 2092 if (error != 0) { 2093 MNT_REL(mp); 2094 MNT_IUNLOCK(mp); 2095 return (error); 2096 } 2097 } else if (!TAILQ_EMPTY(&mp->mnt_uppers)) { 2098 MNT_IUNLOCK(mp); 2099 if (error == 0) 2100 deferred_unmount_enqueue(mp, flags, true, 0); 2101 return (error); 2102 } 2103 MNT_IUNLOCK(mp); 2104 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers not empty")); 2105 } 2106 2107 /* Allow the taskqueue to safely re-enqueue on failure */ 2108 if ((flags & MNT_DEFERRED) != 0) 2109 vfs_ref(mp); 2110 2111 if ((coveredvp = mp->mnt_vnodecovered) != NULL) { 2112 mnt_gen_r = mp->mnt_gen; 2113 VI_LOCK(coveredvp); 2114 vholdl(coveredvp); 2115 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 2116 /* 2117 * Check for mp being unmounted while waiting for the 2118 * covered vnode lock. 2119 */ 2120 if (coveredvp->v_mountedhere != mp || 2121 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) { 2122 VOP_UNLOCK(coveredvp); 2123 vdrop(coveredvp); 2124 vfs_rel(mp); 2125 return (EBUSY); 2126 } 2127 } 2128 2129 vfs_op_enter(mp); 2130 2131 vn_start_write(NULL, &mp, V_WAIT); 2132 MNT_ILOCK(mp); 2133 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 || 2134 (mp->mnt_flag & MNT_UPDATE) != 0 || 2135 !TAILQ_EMPTY(&mp->mnt_uppers)) { 2136 dounmount_cleanup(mp, coveredvp, 0); 2137 return (EBUSY); 2138 } 2139 mp->mnt_kern_flag |= MNTK_UNMOUNT; 2140 rootvp = vfs_cache_root_clear(mp); 2141 if (coveredvp != NULL) 2142 vn_seqc_write_begin(coveredvp); 2143 if (flags & MNT_NONBUSY) { 2144 MNT_IUNLOCK(mp); 2145 error = vfs_check_usecounts(mp); 2146 MNT_ILOCK(mp); 2147 if (error != 0) { 2148 vn_seqc_write_end(coveredvp); 2149 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT); 2150 if (rootvp != NULL) { 2151 vn_seqc_write_end(rootvp); 2152 vrele(rootvp); 2153 } 2154 return (error); 2155 } 2156 } 2157 /* Allow filesystems to detect that a forced unmount is in progress. */ 2158 if (flags & MNT_FORCE) { 2159 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 2160 MNT_IUNLOCK(mp); 2161 /* 2162 * Must be done after setting MNTK_UNMOUNTF and before 2163 * waiting for mnt_lockref to become 0. 2164 */ 2165 VFS_PURGE(mp); 2166 MNT_ILOCK(mp); 2167 } 2168 error = 0; 2169 if (mp->mnt_lockref) { 2170 mp->mnt_kern_flag |= MNTK_DRAINING; 2171 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS, 2172 "mount drain", 0); 2173 } 2174 MNT_IUNLOCK(mp); 2175 KASSERT(mp->mnt_lockref == 0, 2176 ("%s: invalid lock refcount in the drain path @ %s:%d", 2177 __func__, __FILE__, __LINE__)); 2178 KASSERT(error == 0, 2179 ("%s: invalid return value for msleep in the drain path @ %s:%d", 2180 __func__, __FILE__, __LINE__)); 2181 2182 /* 2183 * We want to keep the vnode around so that we can vn_seqc_write_end 2184 * after we are done with unmount. Downgrade our reference to a mere 2185 * hold count so that we don't interefere with anything. 2186 */ 2187 if (rootvp != NULL) { 2188 vhold(rootvp); 2189 vrele(rootvp); 2190 } 2191 2192 if (mp->mnt_flag & MNT_EXPUBLIC) 2193 vfs_setpublicfs(NULL, NULL, NULL); 2194 2195 vfs_periodic(mp, MNT_WAIT); 2196 MNT_ILOCK(mp); 2197 async_flag = mp->mnt_flag & MNT_ASYNC; 2198 mp->mnt_flag &= ~MNT_ASYNC; 2199 mp->mnt_kern_flag &= ~MNTK_ASYNC; 2200 MNT_IUNLOCK(mp); 2201 vfs_deallocate_syncvnode(mp); 2202 error = VFS_UNMOUNT(mp, flags); 2203 vn_finished_write(mp); 2204 vfs_rel(mp); 2205 /* 2206 * If we failed to flush the dirty blocks for this mount point, 2207 * undo all the cdir/rdir and rootvnode changes we made above. 2208 * Unless we failed to do so because the device is reporting that 2209 * it doesn't exist anymore. 2210 */ 2211 if (error && error != ENXIO) { 2212 MNT_ILOCK(mp); 2213 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 2214 MNT_IUNLOCK(mp); 2215 vfs_allocate_syncvnode(mp); 2216 MNT_ILOCK(mp); 2217 } 2218 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 2219 mp->mnt_flag |= async_flag; 2220 if ((mp->mnt_flag & MNT_ASYNC) != 0 && 2221 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0) 2222 mp->mnt_kern_flag |= MNTK_ASYNC; 2223 if (mp->mnt_kern_flag & MNTK_MWAIT) { 2224 mp->mnt_kern_flag &= ~MNTK_MWAIT; 2225 wakeup(mp); 2226 } 2227 vfs_op_exit_locked(mp); 2228 MNT_IUNLOCK(mp); 2229 if (coveredvp) { 2230 vn_seqc_write_end(coveredvp); 2231 VOP_UNLOCK(coveredvp); 2232 vdrop(coveredvp); 2233 } 2234 if (rootvp != NULL) { 2235 vn_seqc_write_end(rootvp); 2236 vdrop(rootvp); 2237 } 2238 return (error); 2239 } 2240 2241 mtx_lock(&mountlist_mtx); 2242 TAILQ_REMOVE(&mountlist, mp, mnt_list); 2243 mtx_unlock(&mountlist_mtx); 2244 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td); 2245 if (coveredvp != NULL) { 2246 VI_LOCK(coveredvp); 2247 vn_irflag_unset_locked(coveredvp, VIRF_MOUNTPOINT); 2248 coveredvp->v_mountedhere = NULL; 2249 vn_seqc_write_end_locked(coveredvp); 2250 VI_UNLOCK(coveredvp); 2251 VOP_UNLOCK(coveredvp); 2252 vdrop(coveredvp); 2253 } 2254 mount_devctl_event("UNMOUNT", mp, false); 2255 if (rootvp != NULL) { 2256 vn_seqc_write_end(rootvp); 2257 vdrop(rootvp); 2258 } 2259 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 2260 if (rootvnode != NULL && mp == rootvnode->v_mount) { 2261 vrele(rootvnode); 2262 rootvnode = NULL; 2263 } 2264 if (mp == rootdevmp) 2265 rootdevmp = NULL; 2266 if ((flags & MNT_DEFERRED) != 0) 2267 vfs_rel(mp); 2268 vfs_mount_destroy(mp); 2269 return (0); 2270 } 2271 2272 /* 2273 * Report errors during filesystem mounting. 2274 */ 2275 void 2276 vfs_mount_error(struct mount *mp, const char *fmt, ...) 2277 { 2278 struct vfsoptlist *moptlist = mp->mnt_optnew; 2279 va_list ap; 2280 int error, len; 2281 char *errmsg; 2282 2283 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len); 2284 if (error || errmsg == NULL || len <= 0) 2285 return; 2286 2287 va_start(ap, fmt); 2288 vsnprintf(errmsg, (size_t)len, fmt, ap); 2289 va_end(ap); 2290 } 2291 2292 void 2293 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...) 2294 { 2295 va_list ap; 2296 int error, len; 2297 char *errmsg; 2298 2299 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len); 2300 if (error || errmsg == NULL || len <= 0) 2301 return; 2302 2303 va_start(ap, fmt); 2304 vsnprintf(errmsg, (size_t)len, fmt, ap); 2305 va_end(ap); 2306 } 2307 2308 /* 2309 * --------------------------------------------------------------------- 2310 * Functions for querying mount options/arguments from filesystems. 2311 */ 2312 2313 /* 2314 * Check that no unknown options are given 2315 */ 2316 int 2317 vfs_filteropt(struct vfsoptlist *opts, const char **legal) 2318 { 2319 struct vfsopt *opt; 2320 char errmsg[255]; 2321 const char **t, *p, *q; 2322 int ret = 0; 2323 2324 TAILQ_FOREACH(opt, opts, link) { 2325 p = opt->name; 2326 q = NULL; 2327 if (p[0] == 'n' && p[1] == 'o') 2328 q = p + 2; 2329 for(t = global_opts; *t != NULL; t++) { 2330 if (strcmp(*t, p) == 0) 2331 break; 2332 if (q != NULL) { 2333 if (strcmp(*t, q) == 0) 2334 break; 2335 } 2336 } 2337 if (*t != NULL) 2338 continue; 2339 for(t = legal; *t != NULL; t++) { 2340 if (strcmp(*t, p) == 0) 2341 break; 2342 if (q != NULL) { 2343 if (strcmp(*t, q) == 0) 2344 break; 2345 } 2346 } 2347 if (*t != NULL) 2348 continue; 2349 snprintf(errmsg, sizeof(errmsg), 2350 "mount option <%s> is unknown", p); 2351 ret = EINVAL; 2352 } 2353 if (ret != 0) { 2354 TAILQ_FOREACH(opt, opts, link) { 2355 if (strcmp(opt->name, "errmsg") == 0) { 2356 strncpy((char *)opt->value, errmsg, opt->len); 2357 break; 2358 } 2359 } 2360 if (opt == NULL) 2361 printf("%s\n", errmsg); 2362 } 2363 return (ret); 2364 } 2365 2366 /* 2367 * Get a mount option by its name. 2368 * 2369 * Return 0 if the option was found, ENOENT otherwise. 2370 * If len is non-NULL it will be filled with the length 2371 * of the option. If buf is non-NULL, it will be filled 2372 * with the address of the option. 2373 */ 2374 int 2375 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len) 2376 { 2377 struct vfsopt *opt; 2378 2379 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 2380 2381 TAILQ_FOREACH(opt, opts, link) { 2382 if (strcmp(name, opt->name) == 0) { 2383 opt->seen = 1; 2384 if (len != NULL) 2385 *len = opt->len; 2386 if (buf != NULL) 2387 *buf = opt->value; 2388 return (0); 2389 } 2390 } 2391 return (ENOENT); 2392 } 2393 2394 int 2395 vfs_getopt_pos(struct vfsoptlist *opts, const char *name) 2396 { 2397 struct vfsopt *opt; 2398 2399 if (opts == NULL) 2400 return (-1); 2401 2402 TAILQ_FOREACH(opt, opts, link) { 2403 if (strcmp(name, opt->name) == 0) { 2404 opt->seen = 1; 2405 return (opt->pos); 2406 } 2407 } 2408 return (-1); 2409 } 2410 2411 int 2412 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value) 2413 { 2414 char *opt_value, *vtp; 2415 quad_t iv; 2416 int error, opt_len; 2417 2418 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len); 2419 if (error != 0) 2420 return (error); 2421 if (opt_len == 0 || opt_value == NULL) 2422 return (EINVAL); 2423 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0') 2424 return (EINVAL); 2425 iv = strtoq(opt_value, &vtp, 0); 2426 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0')) 2427 return (EINVAL); 2428 if (iv < 0) 2429 return (EINVAL); 2430 switch (vtp[0]) { 2431 case 't': case 'T': 2432 iv *= 1024; 2433 /* FALLTHROUGH */ 2434 case 'g': case 'G': 2435 iv *= 1024; 2436 /* FALLTHROUGH */ 2437 case 'm': case 'M': 2438 iv *= 1024; 2439 /* FALLTHROUGH */ 2440 case 'k': case 'K': 2441 iv *= 1024; 2442 case '\0': 2443 break; 2444 default: 2445 return (EINVAL); 2446 } 2447 *value = iv; 2448 2449 return (0); 2450 } 2451 2452 char * 2453 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 2454 { 2455 struct vfsopt *opt; 2456 2457 *error = 0; 2458 TAILQ_FOREACH(opt, opts, link) { 2459 if (strcmp(name, opt->name) != 0) 2460 continue; 2461 opt->seen = 1; 2462 if (opt->len == 0 || 2463 ((char *)opt->value)[opt->len - 1] != '\0') { 2464 *error = EINVAL; 2465 return (NULL); 2466 } 2467 return (opt->value); 2468 } 2469 *error = ENOENT; 2470 return (NULL); 2471 } 2472 2473 int 2474 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w, 2475 uint64_t val) 2476 { 2477 struct vfsopt *opt; 2478 2479 TAILQ_FOREACH(opt, opts, link) { 2480 if (strcmp(name, opt->name) == 0) { 2481 opt->seen = 1; 2482 if (w != NULL) 2483 *w |= val; 2484 return (1); 2485 } 2486 } 2487 if (w != NULL) 2488 *w &= ~val; 2489 return (0); 2490 } 2491 2492 int 2493 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 2494 { 2495 va_list ap; 2496 struct vfsopt *opt; 2497 int ret; 2498 2499 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 2500 2501 TAILQ_FOREACH(opt, opts, link) { 2502 if (strcmp(name, opt->name) != 0) 2503 continue; 2504 opt->seen = 1; 2505 if (opt->len == 0 || opt->value == NULL) 2506 return (0); 2507 if (((char *)opt->value)[opt->len - 1] != '\0') 2508 return (0); 2509 va_start(ap, fmt); 2510 ret = vsscanf(opt->value, fmt, ap); 2511 va_end(ap); 2512 return (ret); 2513 } 2514 return (0); 2515 } 2516 2517 int 2518 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len) 2519 { 2520 struct vfsopt *opt; 2521 2522 TAILQ_FOREACH(opt, opts, link) { 2523 if (strcmp(name, opt->name) != 0) 2524 continue; 2525 opt->seen = 1; 2526 if (opt->value == NULL) 2527 opt->len = len; 2528 else { 2529 if (opt->len != len) 2530 return (EINVAL); 2531 bcopy(value, opt->value, len); 2532 } 2533 return (0); 2534 } 2535 return (ENOENT); 2536 } 2537 2538 int 2539 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len) 2540 { 2541 struct vfsopt *opt; 2542 2543 TAILQ_FOREACH(opt, opts, link) { 2544 if (strcmp(name, opt->name) != 0) 2545 continue; 2546 opt->seen = 1; 2547 if (opt->value == NULL) 2548 opt->len = len; 2549 else { 2550 if (opt->len < len) 2551 return (EINVAL); 2552 opt->len = len; 2553 bcopy(value, opt->value, len); 2554 } 2555 return (0); 2556 } 2557 return (ENOENT); 2558 } 2559 2560 int 2561 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value) 2562 { 2563 struct vfsopt *opt; 2564 2565 TAILQ_FOREACH(opt, opts, link) { 2566 if (strcmp(name, opt->name) != 0) 2567 continue; 2568 opt->seen = 1; 2569 if (opt->value == NULL) 2570 opt->len = strlen(value) + 1; 2571 else if (strlcpy(opt->value, value, opt->len) >= opt->len) 2572 return (EINVAL); 2573 return (0); 2574 } 2575 return (ENOENT); 2576 } 2577 2578 /* 2579 * Find and copy a mount option. 2580 * 2581 * The size of the buffer has to be specified 2582 * in len, if it is not the same length as the 2583 * mount option, EINVAL is returned. 2584 * Returns ENOENT if the option is not found. 2585 */ 2586 int 2587 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len) 2588 { 2589 struct vfsopt *opt; 2590 2591 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 2592 2593 TAILQ_FOREACH(opt, opts, link) { 2594 if (strcmp(name, opt->name) == 0) { 2595 opt->seen = 1; 2596 if (len != opt->len) 2597 return (EINVAL); 2598 bcopy(opt->value, dest, opt->len); 2599 return (0); 2600 } 2601 } 2602 return (ENOENT); 2603 } 2604 2605 int 2606 __vfs_statfs(struct mount *mp, struct statfs *sbp) 2607 { 2608 /* 2609 * Filesystems only fill in part of the structure for updates, we 2610 * have to read the entirety first to get all content. 2611 */ 2612 if (sbp != &mp->mnt_stat) 2613 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp)); 2614 2615 /* 2616 * Set these in case the underlying filesystem fails to do so. 2617 */ 2618 sbp->f_version = STATFS_VERSION; 2619 sbp->f_namemax = NAME_MAX; 2620 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 2621 sbp->f_nvnodelistsize = mp->mnt_nvnodelistsize; 2622 2623 return (mp->mnt_op->vfs_statfs(mp, sbp)); 2624 } 2625 2626 void 2627 vfs_mountedfrom(struct mount *mp, const char *from) 2628 { 2629 2630 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 2631 strlcpy(mp->mnt_stat.f_mntfromname, from, 2632 sizeof mp->mnt_stat.f_mntfromname); 2633 } 2634 2635 /* 2636 * --------------------------------------------------------------------- 2637 * This is the api for building mount args and mounting filesystems from 2638 * inside the kernel. 2639 * 2640 * The API works by accumulation of individual args. First error is 2641 * latched. 2642 * 2643 * XXX: should be documented in new manpage kernel_mount(9) 2644 */ 2645 2646 /* A memory allocation which must be freed when we are done */ 2647 struct mntaarg { 2648 SLIST_ENTRY(mntaarg) next; 2649 }; 2650 2651 /* The header for the mount arguments */ 2652 struct mntarg { 2653 struct iovec *v; 2654 int len; 2655 int error; 2656 SLIST_HEAD(, mntaarg) list; 2657 }; 2658 2659 /* 2660 * Add a boolean argument. 2661 * 2662 * flag is the boolean value. 2663 * name must start with "no". 2664 */ 2665 struct mntarg * 2666 mount_argb(struct mntarg *ma, int flag, const char *name) 2667 { 2668 2669 KASSERT(name[0] == 'n' && name[1] == 'o', 2670 ("mount_argb(...,%s): name must start with 'no'", name)); 2671 2672 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 2673 } 2674 2675 /* 2676 * Add an argument printf style 2677 */ 2678 struct mntarg * 2679 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 2680 { 2681 va_list ap; 2682 struct mntaarg *maa; 2683 struct sbuf *sb; 2684 int len; 2685 2686 if (ma == NULL) { 2687 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2688 SLIST_INIT(&ma->list); 2689 } 2690 if (ma->error) 2691 return (ma); 2692 2693 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2694 M_MOUNT, M_WAITOK); 2695 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2696 ma->v[ma->len].iov_len = strlen(name) + 1; 2697 ma->len++; 2698 2699 sb = sbuf_new_auto(); 2700 va_start(ap, fmt); 2701 sbuf_vprintf(sb, fmt, ap); 2702 va_end(ap); 2703 sbuf_finish(sb); 2704 len = sbuf_len(sb) + 1; 2705 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2706 SLIST_INSERT_HEAD(&ma->list, maa, next); 2707 bcopy(sbuf_data(sb), maa + 1, len); 2708 sbuf_delete(sb); 2709 2710 ma->v[ma->len].iov_base = maa + 1; 2711 ma->v[ma->len].iov_len = len; 2712 ma->len++; 2713 2714 return (ma); 2715 } 2716 2717 /* 2718 * Add an argument which is a userland string. 2719 */ 2720 struct mntarg * 2721 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 2722 { 2723 struct mntaarg *maa; 2724 char *tbuf; 2725 2726 if (val == NULL) 2727 return (ma); 2728 if (ma == NULL) { 2729 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2730 SLIST_INIT(&ma->list); 2731 } 2732 if (ma->error) 2733 return (ma); 2734 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2735 SLIST_INSERT_HEAD(&ma->list, maa, next); 2736 tbuf = (void *)(maa + 1); 2737 ma->error = copyinstr(val, tbuf, len, NULL); 2738 return (mount_arg(ma, name, tbuf, -1)); 2739 } 2740 2741 /* 2742 * Plain argument. 2743 * 2744 * If length is -1, treat value as a C string. 2745 */ 2746 struct mntarg * 2747 mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 2748 { 2749 2750 if (ma == NULL) { 2751 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2752 SLIST_INIT(&ma->list); 2753 } 2754 if (ma->error) 2755 return (ma); 2756 2757 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2758 M_MOUNT, M_WAITOK); 2759 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2760 ma->v[ma->len].iov_len = strlen(name) + 1; 2761 ma->len++; 2762 2763 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 2764 if (len < 0) 2765 ma->v[ma->len].iov_len = strlen(val) + 1; 2766 else 2767 ma->v[ma->len].iov_len = len; 2768 ma->len++; 2769 return (ma); 2770 } 2771 2772 /* 2773 * Free a mntarg structure 2774 */ 2775 static void 2776 free_mntarg(struct mntarg *ma) 2777 { 2778 struct mntaarg *maa; 2779 2780 while (!SLIST_EMPTY(&ma->list)) { 2781 maa = SLIST_FIRST(&ma->list); 2782 SLIST_REMOVE_HEAD(&ma->list, next); 2783 free(maa, M_MOUNT); 2784 } 2785 free(ma->v, M_MOUNT); 2786 free(ma, M_MOUNT); 2787 } 2788 2789 /* 2790 * Mount a filesystem 2791 */ 2792 int 2793 kernel_mount(struct mntarg *ma, uint64_t flags) 2794 { 2795 struct uio auio; 2796 int error; 2797 2798 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 2799 KASSERT(ma->error != 0 || ma->v != NULL, ("kernel_mount NULL ma->v")); 2800 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 2801 2802 error = ma->error; 2803 if (error == 0) { 2804 auio.uio_iov = ma->v; 2805 auio.uio_iovcnt = ma->len; 2806 auio.uio_segflg = UIO_SYSSPACE; 2807 error = vfs_donmount(curthread, flags, &auio); 2808 } 2809 free_mntarg(ma); 2810 return (error); 2811 } 2812 2813 /* Map from mount options to printable formats. */ 2814 static struct mntoptnames optnames[] = { 2815 MNTOPT_NAMES 2816 }; 2817 2818 #define DEVCTL_LEN 1024 2819 static void 2820 mount_devctl_event(const char *type, struct mount *mp, bool donew) 2821 { 2822 const uint8_t *cp; 2823 struct mntoptnames *fp; 2824 struct sbuf sb; 2825 struct statfs *sfp = &mp->mnt_stat; 2826 char *buf; 2827 2828 buf = malloc(DEVCTL_LEN, M_MOUNT, M_NOWAIT); 2829 if (buf == NULL) 2830 return; 2831 sbuf_new(&sb, buf, DEVCTL_LEN, SBUF_FIXEDLEN); 2832 sbuf_cpy(&sb, "mount-point=\""); 2833 devctl_safe_quote_sb(&sb, sfp->f_mntonname); 2834 sbuf_cat(&sb, "\" mount-dev=\""); 2835 devctl_safe_quote_sb(&sb, sfp->f_mntfromname); 2836 sbuf_cat(&sb, "\" mount-type=\""); 2837 devctl_safe_quote_sb(&sb, sfp->f_fstypename); 2838 sbuf_cat(&sb, "\" fsid=0x"); 2839 cp = (const uint8_t *)&sfp->f_fsid.val[0]; 2840 for (int i = 0; i < sizeof(sfp->f_fsid); i++) 2841 sbuf_printf(&sb, "%02x", cp[i]); 2842 sbuf_printf(&sb, " owner=%u flags=\"", sfp->f_owner); 2843 for (fp = optnames; fp->o_opt != 0; fp++) { 2844 if ((mp->mnt_flag & fp->o_opt) != 0) { 2845 sbuf_cat(&sb, fp->o_name); 2846 sbuf_putc(&sb, ';'); 2847 } 2848 } 2849 sbuf_putc(&sb, '"'); 2850 sbuf_finish(&sb); 2851 2852 /* 2853 * Options are not published because the form of the options depends on 2854 * the file system and may include binary data. In addition, they don't 2855 * necessarily provide enough useful information to be actionable when 2856 * devd processes them. 2857 */ 2858 2859 if (sbuf_error(&sb) == 0) 2860 devctl_notify("VFS", "FS", type, sbuf_data(&sb)); 2861 sbuf_delete(&sb); 2862 free(buf, M_MOUNT); 2863 } 2864 2865 /* 2866 * Force remount specified mount point to read-only. The argument 2867 * must be busied to avoid parallel unmount attempts. 2868 * 2869 * Intended use is to prevent further writes if some metadata 2870 * inconsistency is detected. Note that the function still flushes 2871 * all cached metadata and data for the mount point, which might be 2872 * not always suitable. 2873 */ 2874 int 2875 vfs_remount_ro(struct mount *mp) 2876 { 2877 struct vfsoptlist *opts; 2878 struct vfsopt *opt; 2879 struct vnode *vp_covered, *rootvp; 2880 int error; 2881 2882 KASSERT(mp->mnt_lockref > 0, 2883 ("vfs_remount_ro: mp %p is not busied", mp)); 2884 KASSERT((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0, 2885 ("vfs_remount_ro: mp %p is being unmounted (and busy?)", mp)); 2886 2887 rootvp = NULL; 2888 vp_covered = mp->mnt_vnodecovered; 2889 error = vget(vp_covered, LK_EXCLUSIVE | LK_NOWAIT); 2890 if (error != 0) 2891 return (error); 2892 VI_LOCK(vp_covered); 2893 if ((vp_covered->v_iflag & VI_MOUNT) != 0) { 2894 VI_UNLOCK(vp_covered); 2895 vput(vp_covered); 2896 return (EBUSY); 2897 } 2898 vp_covered->v_iflag |= VI_MOUNT; 2899 VI_UNLOCK(vp_covered); 2900 vfs_op_enter(mp); 2901 vn_seqc_write_begin(vp_covered); 2902 2903 MNT_ILOCK(mp); 2904 if ((mp->mnt_flag & MNT_RDONLY) != 0) { 2905 MNT_IUNLOCK(mp); 2906 error = EBUSY; 2907 goto out; 2908 } 2909 mp->mnt_flag |= MNT_UPDATE | MNT_FORCE | MNT_RDONLY; 2910 rootvp = vfs_cache_root_clear(mp); 2911 MNT_IUNLOCK(mp); 2912 2913 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK | M_ZERO); 2914 TAILQ_INIT(opts); 2915 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK | M_ZERO); 2916 opt->name = strdup("ro", M_MOUNT); 2917 opt->value = NULL; 2918 TAILQ_INSERT_TAIL(opts, opt, link); 2919 vfs_mergeopts(opts, mp->mnt_opt); 2920 mp->mnt_optnew = opts; 2921 2922 error = VFS_MOUNT(mp); 2923 2924 if (error == 0) { 2925 MNT_ILOCK(mp); 2926 mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE); 2927 MNT_IUNLOCK(mp); 2928 vfs_deallocate_syncvnode(mp); 2929 if (mp->mnt_opt != NULL) 2930 vfs_freeopts(mp->mnt_opt); 2931 mp->mnt_opt = mp->mnt_optnew; 2932 } else { 2933 MNT_ILOCK(mp); 2934 mp->mnt_flag &= ~(MNT_UPDATE | MNT_FORCE | MNT_RDONLY); 2935 MNT_IUNLOCK(mp); 2936 vfs_freeopts(mp->mnt_optnew); 2937 } 2938 mp->mnt_optnew = NULL; 2939 2940 out: 2941 vfs_op_exit(mp); 2942 VI_LOCK(vp_covered); 2943 vp_covered->v_iflag &= ~VI_MOUNT; 2944 VI_UNLOCK(vp_covered); 2945 vput(vp_covered); 2946 vn_seqc_write_end(vp_covered); 2947 if (rootvp != NULL) { 2948 vn_seqc_write_end(rootvp); 2949 vrele(rootvp); 2950 } 2951 return (error); 2952 } 2953 2954 /* 2955 * Suspend write operations on all local writeable filesystems. Does 2956 * full sync of them in the process. 2957 * 2958 * Iterate over the mount points in reverse order, suspending most 2959 * recently mounted filesystems first. It handles a case where a 2960 * filesystem mounted from a md(4) vnode-backed device should be 2961 * suspended before the filesystem that owns the vnode. 2962 */ 2963 void 2964 suspend_all_fs(void) 2965 { 2966 struct mount *mp; 2967 int error; 2968 2969 mtx_lock(&mountlist_mtx); 2970 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 2971 error = vfs_busy(mp, MBF_MNTLSTLOCK | MBF_NOWAIT); 2972 if (error != 0) 2973 continue; 2974 if ((mp->mnt_flag & (MNT_RDONLY | MNT_LOCAL)) != MNT_LOCAL || 2975 (mp->mnt_kern_flag & MNTK_SUSPEND) != 0) { 2976 mtx_lock(&mountlist_mtx); 2977 vfs_unbusy(mp); 2978 continue; 2979 } 2980 error = vfs_write_suspend(mp, 0); 2981 if (error == 0) { 2982 MNT_ILOCK(mp); 2983 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0); 2984 mp->mnt_kern_flag |= MNTK_SUSPEND_ALL; 2985 MNT_IUNLOCK(mp); 2986 mtx_lock(&mountlist_mtx); 2987 } else { 2988 printf("suspend of %s failed, error %d\n", 2989 mp->mnt_stat.f_mntonname, error); 2990 mtx_lock(&mountlist_mtx); 2991 vfs_unbusy(mp); 2992 } 2993 } 2994 mtx_unlock(&mountlist_mtx); 2995 } 2996 2997 void 2998 resume_all_fs(void) 2999 { 3000 struct mount *mp; 3001 3002 mtx_lock(&mountlist_mtx); 3003 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 3004 if ((mp->mnt_kern_flag & MNTK_SUSPEND_ALL) == 0) 3005 continue; 3006 mtx_unlock(&mountlist_mtx); 3007 MNT_ILOCK(mp); 3008 MPASS((mp->mnt_kern_flag & MNTK_SUSPEND) != 0); 3009 mp->mnt_kern_flag &= ~MNTK_SUSPEND_ALL; 3010 MNT_IUNLOCK(mp); 3011 vfs_write_resume(mp, 0); 3012 mtx_lock(&mountlist_mtx); 3013 vfs_unbusy(mp); 3014 } 3015 mtx_unlock(&mountlist_mtx); 3016 } 3017