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/eventhandler.h> 46 #include <sys/fcntl.h> 47 #include <sys/jail.h> 48 #include <sys/kernel.h> 49 #include <sys/ktr.h> 50 #include <sys/libkern.h> 51 #include <sys/malloc.h> 52 #include <sys/mount.h> 53 #include <sys/mutex.h> 54 #include <sys/namei.h> 55 #include <sys/priv.h> 56 #include <sys/proc.h> 57 #include <sys/filedesc.h> 58 #include <sys/reboot.h> 59 #include <sys/sbuf.h> 60 #include <sys/syscallsubr.h> 61 #include <sys/sysproto.h> 62 #include <sys/sx.h> 63 #include <sys/sysctl.h> 64 #include <sys/sysent.h> 65 #include <sys/systm.h> 66 #include <sys/vnode.h> 67 #include <vm/uma.h> 68 69 #include <geom/geom.h> 70 71 #include <machine/stdarg.h> 72 73 #include <security/audit/audit.h> 74 #include <security/mac/mac_framework.h> 75 76 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64) 77 78 static int vfs_domount(struct thread *td, const char *fstype, char *fspath, 79 uint64_t fsflags, struct vfsoptlist **optlist); 80 static void free_mntarg(struct mntarg *ma); 81 82 static int usermount = 0; 83 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 84 "Unprivileged users may mount and unmount file systems"); 85 86 static bool default_autoro = false; 87 SYSCTL_BOOL(_vfs, OID_AUTO, default_autoro, CTLFLAG_RW, &default_autoro, 0, 88 "Retry failed r/w mount as r/o if no explicit ro/rw option is specified"); 89 90 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 91 MALLOC_DEFINE(M_STATFS, "statfs", "statfs structure"); 92 static uma_zone_t mount_zone; 93 94 /* List of mounted filesystems. */ 95 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 96 97 /* For any iteration/modification of mountlist */ 98 struct mtx mountlist_mtx; 99 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF); 100 101 EVENTHANDLER_LIST_DEFINE(vfs_mounted); 102 EVENTHANDLER_LIST_DEFINE(vfs_unmounted); 103 104 /* 105 * Global opts, taken by all filesystems 106 */ 107 static const char *global_opts[] = { 108 "errmsg", 109 "fstype", 110 "fspath", 111 "ro", 112 "rw", 113 "nosuid", 114 "noexec", 115 NULL 116 }; 117 118 static int 119 mount_init(void *mem, int size, int flags) 120 { 121 struct mount *mp; 122 123 mp = (struct mount *)mem; 124 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF); 125 mtx_init(&mp->mnt_listmtx, "struct mount vlist mtx", NULL, MTX_DEF); 126 lockinit(&mp->mnt_explock, PVFS, "explock", 0, 0); 127 mp->mnt_thread_in_ops_pcpu = uma_zalloc_pcpu(pcpu_zone_int, 128 M_WAITOK | M_ZERO); 129 mp->mnt_ref_pcpu = uma_zalloc_pcpu(pcpu_zone_int, 130 M_WAITOK | M_ZERO); 131 mp->mnt_lockref_pcpu = uma_zalloc_pcpu(pcpu_zone_int, 132 M_WAITOK | M_ZERO); 133 mp->mnt_writeopcount_pcpu = uma_zalloc_pcpu(pcpu_zone_int, 134 M_WAITOK | M_ZERO); 135 mp->mnt_ref = 0; 136 mp->mnt_vfs_ops = 1; 137 return (0); 138 } 139 140 static void 141 mount_fini(void *mem, int size) 142 { 143 struct mount *mp; 144 145 mp = (struct mount *)mem; 146 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_writeopcount_pcpu); 147 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_lockref_pcpu); 148 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_ref_pcpu); 149 uma_zfree_pcpu(pcpu_zone_int, mp->mnt_thread_in_ops_pcpu); 150 lockdestroy(&mp->mnt_explock); 151 mtx_destroy(&mp->mnt_listmtx); 152 mtx_destroy(&mp->mnt_mtx); 153 } 154 155 static void 156 vfs_mount_init(void *dummy __unused) 157 { 158 159 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), NULL, 160 NULL, mount_init, mount_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 161 } 162 SYSINIT(vfs_mount, SI_SUB_VFS, SI_ORDER_ANY, vfs_mount_init, NULL); 163 164 /* 165 * --------------------------------------------------------------------- 166 * Functions for building and sanitizing the mount options 167 */ 168 169 /* Remove one mount option. */ 170 static void 171 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt) 172 { 173 174 TAILQ_REMOVE(opts, opt, link); 175 free(opt->name, M_MOUNT); 176 if (opt->value != NULL) 177 free(opt->value, M_MOUNT); 178 free(opt, M_MOUNT); 179 } 180 181 /* Release all resources related to the mount options. */ 182 void 183 vfs_freeopts(struct vfsoptlist *opts) 184 { 185 struct vfsopt *opt; 186 187 while (!TAILQ_EMPTY(opts)) { 188 opt = TAILQ_FIRST(opts); 189 vfs_freeopt(opts, opt); 190 } 191 free(opts, M_MOUNT); 192 } 193 194 void 195 vfs_deleteopt(struct vfsoptlist *opts, const char *name) 196 { 197 struct vfsopt *opt, *temp; 198 199 if (opts == NULL) 200 return; 201 TAILQ_FOREACH_SAFE(opt, opts, link, temp) { 202 if (strcmp(opt->name, name) == 0) 203 vfs_freeopt(opts, opt); 204 } 205 } 206 207 static int 208 vfs_isopt_ro(const char *opt) 209 { 210 211 if (strcmp(opt, "ro") == 0 || strcmp(opt, "rdonly") == 0 || 212 strcmp(opt, "norw") == 0) 213 return (1); 214 return (0); 215 } 216 217 static int 218 vfs_isopt_rw(const char *opt) 219 { 220 221 if (strcmp(opt, "rw") == 0 || strcmp(opt, "noro") == 0) 222 return (1); 223 return (0); 224 } 225 226 /* 227 * Check if options are equal (with or without the "no" prefix). 228 */ 229 static int 230 vfs_equalopts(const char *opt1, const char *opt2) 231 { 232 char *p; 233 234 /* "opt" vs. "opt" or "noopt" vs. "noopt" */ 235 if (strcmp(opt1, opt2) == 0) 236 return (1); 237 /* "noopt" vs. "opt" */ 238 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 239 return (1); 240 /* "opt" vs. "noopt" */ 241 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 242 return (1); 243 while ((p = strchr(opt1, '.')) != NULL && 244 !strncmp(opt1, opt2, ++p - opt1)) { 245 opt2 += p - opt1; 246 opt1 = p; 247 /* "foo.noopt" vs. "foo.opt" */ 248 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 249 return (1); 250 /* "foo.opt" vs. "foo.noopt" */ 251 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 252 return (1); 253 } 254 /* "ro" / "rdonly" / "norw" / "rw" / "noro" */ 255 if ((vfs_isopt_ro(opt1) || vfs_isopt_rw(opt1)) && 256 (vfs_isopt_ro(opt2) || vfs_isopt_rw(opt2))) 257 return (1); 258 return (0); 259 } 260 261 /* 262 * If a mount option is specified several times, 263 * (with or without the "no" prefix) only keep 264 * the last occurrence of it. 265 */ 266 static void 267 vfs_sanitizeopts(struct vfsoptlist *opts) 268 { 269 struct vfsopt *opt, *opt2, *tmp; 270 271 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) { 272 opt2 = TAILQ_PREV(opt, vfsoptlist, link); 273 while (opt2 != NULL) { 274 if (vfs_equalopts(opt->name, opt2->name)) { 275 tmp = TAILQ_PREV(opt2, vfsoptlist, link); 276 vfs_freeopt(opts, opt2); 277 opt2 = tmp; 278 } else { 279 opt2 = TAILQ_PREV(opt2, vfsoptlist, link); 280 } 281 } 282 } 283 } 284 285 /* 286 * Build a linked list of mount options from a struct uio. 287 */ 288 int 289 vfs_buildopts(struct uio *auio, struct vfsoptlist **options) 290 { 291 struct vfsoptlist *opts; 292 struct vfsopt *opt; 293 size_t memused, namelen, optlen; 294 unsigned int i, iovcnt; 295 int error; 296 297 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 298 TAILQ_INIT(opts); 299 memused = 0; 300 iovcnt = auio->uio_iovcnt; 301 for (i = 0; i < iovcnt; i += 2) { 302 namelen = auio->uio_iov[i].iov_len; 303 optlen = auio->uio_iov[i + 1].iov_len; 304 memused += sizeof(struct vfsopt) + optlen + namelen; 305 /* 306 * Avoid consuming too much memory, and attempts to overflow 307 * memused. 308 */ 309 if (memused > VFS_MOUNTARG_SIZE_MAX || 310 optlen > VFS_MOUNTARG_SIZE_MAX || 311 namelen > VFS_MOUNTARG_SIZE_MAX) { 312 error = EINVAL; 313 goto bad; 314 } 315 316 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 317 opt->name = malloc(namelen, M_MOUNT, M_WAITOK); 318 opt->value = NULL; 319 opt->len = 0; 320 opt->pos = i / 2; 321 opt->seen = 0; 322 323 /* 324 * Do this early, so jumps to "bad" will free the current 325 * option. 326 */ 327 TAILQ_INSERT_TAIL(opts, opt, link); 328 329 if (auio->uio_segflg == UIO_SYSSPACE) { 330 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen); 331 } else { 332 error = copyin(auio->uio_iov[i].iov_base, opt->name, 333 namelen); 334 if (error) 335 goto bad; 336 } 337 /* Ensure names are null-terminated strings. */ 338 if (namelen == 0 || opt->name[namelen - 1] != '\0') { 339 error = EINVAL; 340 goto bad; 341 } 342 if (optlen != 0) { 343 opt->len = optlen; 344 opt->value = malloc(optlen, M_MOUNT, M_WAITOK); 345 if (auio->uio_segflg == UIO_SYSSPACE) { 346 bcopy(auio->uio_iov[i + 1].iov_base, opt->value, 347 optlen); 348 } else { 349 error = copyin(auio->uio_iov[i + 1].iov_base, 350 opt->value, optlen); 351 if (error) 352 goto bad; 353 } 354 } 355 } 356 vfs_sanitizeopts(opts); 357 *options = opts; 358 return (0); 359 bad: 360 vfs_freeopts(opts); 361 return (error); 362 } 363 364 /* 365 * Merge the old mount options with the new ones passed 366 * in the MNT_UPDATE case. 367 * 368 * XXX: This function will keep a "nofoo" option in the new 369 * options. E.g, if the option's canonical name is "foo", 370 * "nofoo" ends up in the mount point's active options. 371 */ 372 static void 373 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *oldopts) 374 { 375 struct vfsopt *opt, *new; 376 377 TAILQ_FOREACH(opt, oldopts, link) { 378 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 379 new->name = strdup(opt->name, M_MOUNT); 380 if (opt->len != 0) { 381 new->value = malloc(opt->len, M_MOUNT, M_WAITOK); 382 bcopy(opt->value, new->value, opt->len); 383 } else 384 new->value = NULL; 385 new->len = opt->len; 386 new->seen = opt->seen; 387 TAILQ_INSERT_HEAD(toopts, new, link); 388 } 389 vfs_sanitizeopts(toopts); 390 } 391 392 /* 393 * Mount a filesystem. 394 */ 395 #ifndef _SYS_SYSPROTO_H_ 396 struct nmount_args { 397 struct iovec *iovp; 398 unsigned int iovcnt; 399 int flags; 400 }; 401 #endif 402 int 403 sys_nmount(struct thread *td, struct nmount_args *uap) 404 { 405 struct uio *auio; 406 int error; 407 u_int iovcnt; 408 uint64_t flags; 409 410 /* 411 * Mount flags are now 64-bits. On 32-bit archtectures only 412 * 32-bits are passed in, but from here on everything handles 413 * 64-bit flags correctly. 414 */ 415 flags = uap->flags; 416 417 AUDIT_ARG_FFLAGS(flags); 418 CTR4(KTR_VFS, "%s: iovp %p with iovcnt %d and flags %d", __func__, 419 uap->iovp, uap->iovcnt, flags); 420 421 /* 422 * Filter out MNT_ROOTFS. We do not want clients of nmount() in 423 * userspace to set this flag, but we must filter it out if we want 424 * MNT_UPDATE on the root file system to work. 425 * MNT_ROOTFS should only be set by the kernel when mounting its 426 * root file system. 427 */ 428 flags &= ~MNT_ROOTFS; 429 430 iovcnt = uap->iovcnt; 431 /* 432 * Check that we have an even number of iovec's 433 * and that we have at least two options. 434 */ 435 if ((iovcnt & 1) || (iovcnt < 4)) { 436 CTR2(KTR_VFS, "%s: failed for invalid iovcnt %d", __func__, 437 uap->iovcnt); 438 return (EINVAL); 439 } 440 441 error = copyinuio(uap->iovp, iovcnt, &auio); 442 if (error) { 443 CTR2(KTR_VFS, "%s: failed for invalid uio op with %d errno", 444 __func__, error); 445 return (error); 446 } 447 error = vfs_donmount(td, flags, auio); 448 449 free(auio, M_IOV); 450 return (error); 451 } 452 453 /* 454 * --------------------------------------------------------------------- 455 * Various utility functions 456 */ 457 458 void 459 vfs_ref(struct mount *mp) 460 { 461 462 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 463 if (vfs_op_thread_enter(mp)) { 464 vfs_mp_count_add_pcpu(mp, ref, 1); 465 vfs_op_thread_exit(mp); 466 return; 467 } 468 469 MNT_ILOCK(mp); 470 MNT_REF(mp); 471 MNT_IUNLOCK(mp); 472 } 473 474 void 475 vfs_rel(struct mount *mp) 476 { 477 478 CTR2(KTR_VFS, "%s: mp %p", __func__, mp); 479 if (vfs_op_thread_enter(mp)) { 480 vfs_mp_count_sub_pcpu(mp, ref, 1); 481 vfs_op_thread_exit(mp); 482 return; 483 } 484 485 MNT_ILOCK(mp); 486 MNT_REL(mp); 487 MNT_IUNLOCK(mp); 488 } 489 490 /* 491 * Allocate and initialize the mount point struct. 492 */ 493 struct mount * 494 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, const char *fspath, 495 struct ucred *cred) 496 { 497 struct mount *mp; 498 499 mp = uma_zalloc(mount_zone, M_WAITOK); 500 bzero(&mp->mnt_startzero, 501 __rangeof(struct mount, mnt_startzero, mnt_endzero)); 502 TAILQ_INIT(&mp->mnt_nvnodelist); 503 mp->mnt_nvnodelistsize = 0; 504 TAILQ_INIT(&mp->mnt_activevnodelist); 505 mp->mnt_activevnodelistsize = 0; 506 TAILQ_INIT(&mp->mnt_tmpfreevnodelist); 507 mp->mnt_tmpfreevnodelistsize = 0; 508 if (mp->mnt_ref != 0 || mp->mnt_lockref != 0 || 509 mp->mnt_writeopcount != 0) 510 panic("%s: non-zero counters on new mp %p\n", __func__, mp); 511 if (mp->mnt_vfs_ops != 1) 512 panic("%s: vfs_ops should be 1 but %d found\n", __func__, 513 mp->mnt_vfs_ops); 514 (void) vfs_busy(mp, MBF_NOWAIT); 515 atomic_add_acq_int(&vfsp->vfc_refcount, 1); 516 mp->mnt_op = vfsp->vfc_vfsops; 517 mp->mnt_vfc = vfsp; 518 mp->mnt_stat.f_type = vfsp->vfc_typenum; 519 mp->mnt_gen++; 520 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 521 mp->mnt_vnodecovered = vp; 522 mp->mnt_cred = crdup(cred); 523 mp->mnt_stat.f_owner = cred->cr_uid; 524 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 525 mp->mnt_iosize_max = DFLTPHYS; 526 #ifdef MAC 527 mac_mount_init(mp); 528 mac_mount_create(cred, mp); 529 #endif 530 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0); 531 TAILQ_INIT(&mp->mnt_uppers); 532 return (mp); 533 } 534 535 /* 536 * Destroy the mount struct previously allocated by vfs_mount_alloc(). 537 */ 538 void 539 vfs_mount_destroy(struct mount *mp) 540 { 541 542 if (mp->mnt_vfs_ops == 0) 543 panic("%s: entered with zero vfs_ops\n", __func__); 544 545 vfs_assert_mount_counters(mp); 546 547 MNT_ILOCK(mp); 548 mp->mnt_kern_flag |= MNTK_REFEXPIRE; 549 if (mp->mnt_kern_flag & MNTK_MWAIT) { 550 mp->mnt_kern_flag &= ~MNTK_MWAIT; 551 wakeup(mp); 552 } 553 while (mp->mnt_ref) 554 msleep(mp, MNT_MTX(mp), PVFS, "mntref", 0); 555 KASSERT(mp->mnt_ref == 0, 556 ("%s: invalid refcount in the drain path @ %s:%d", __func__, 557 __FILE__, __LINE__)); 558 if (mp->mnt_writeopcount != 0) 559 panic("vfs_mount_destroy: nonzero writeopcount"); 560 if (mp->mnt_secondary_writes != 0) 561 panic("vfs_mount_destroy: nonzero secondary_writes"); 562 atomic_subtract_rel_int(&mp->mnt_vfc->vfc_refcount, 1); 563 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) { 564 struct vnode *vp; 565 566 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) 567 vn_printf(vp, "dangling vnode "); 568 panic("unmount: dangling vnode"); 569 } 570 KASSERT(TAILQ_EMPTY(&mp->mnt_uppers), ("mnt_uppers")); 571 if (mp->mnt_nvnodelistsize != 0) 572 panic("vfs_mount_destroy: nonzero nvnodelistsize"); 573 if (mp->mnt_activevnodelistsize != 0) 574 panic("vfs_mount_destroy: nonzero activevnodelistsize"); 575 if (mp->mnt_tmpfreevnodelistsize != 0) 576 panic("vfs_mount_destroy: nonzero tmpfreevnodelistsize"); 577 if (mp->mnt_lockref != 0) 578 panic("vfs_mount_destroy: nonzero lock refcount"); 579 MNT_IUNLOCK(mp); 580 581 if (mp->mnt_vfs_ops != 1) 582 panic("%s: vfs_ops should be 1 but %d found\n", __func__, 583 mp->mnt_vfs_ops); 584 585 if (mp->mnt_vnodecovered != NULL) 586 vrele(mp->mnt_vnodecovered); 587 #ifdef MAC 588 mac_mount_destroy(mp); 589 #endif 590 if (mp->mnt_opt != NULL) 591 vfs_freeopts(mp->mnt_opt); 592 crfree(mp->mnt_cred); 593 uma_zfree(mount_zone, mp); 594 } 595 596 static bool 597 vfs_should_downgrade_to_ro_mount(uint64_t fsflags, int error) 598 { 599 /* This is an upgrade of an exisiting mount. */ 600 if ((fsflags & MNT_UPDATE) != 0) 601 return (false); 602 /* This is already an R/O mount. */ 603 if ((fsflags & MNT_RDONLY) != 0) 604 return (false); 605 606 switch (error) { 607 case ENODEV: /* generic, geom, ... */ 608 case EACCES: /* cam/scsi, ... */ 609 case EROFS: /* md, mmcsd, ... */ 610 /* 611 * These errors can be returned by the storage layer to signal 612 * that the media is read-only. No harm in the R/O mount 613 * attempt if the error was returned for some other reason. 614 */ 615 return (true); 616 default: 617 return (false); 618 } 619 } 620 621 int 622 vfs_donmount(struct thread *td, uint64_t fsflags, struct uio *fsoptions) 623 { 624 struct vfsoptlist *optlist; 625 struct vfsopt *opt, *tmp_opt; 626 char *fstype, *fspath, *errmsg; 627 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos; 628 bool autoro; 629 630 errmsg = fspath = NULL; 631 errmsg_len = fspathlen = 0; 632 errmsg_pos = -1; 633 autoro = default_autoro; 634 635 error = vfs_buildopts(fsoptions, &optlist); 636 if (error) 637 return (error); 638 639 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0) 640 errmsg_pos = vfs_getopt_pos(optlist, "errmsg"); 641 642 /* 643 * We need these two options before the others, 644 * and they are mandatory for any filesystem. 645 * Ensure they are NUL terminated as well. 646 */ 647 fstypelen = 0; 648 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 649 if (error || fstype[fstypelen - 1] != '\0') { 650 error = EINVAL; 651 if (errmsg != NULL) 652 strncpy(errmsg, "Invalid fstype", errmsg_len); 653 goto bail; 654 } 655 fspathlen = 0; 656 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 657 if (error || fspath[fspathlen - 1] != '\0') { 658 error = EINVAL; 659 if (errmsg != NULL) 660 strncpy(errmsg, "Invalid fspath", errmsg_len); 661 goto bail; 662 } 663 664 /* 665 * We need to see if we have the "update" option 666 * before we call vfs_domount(), since vfs_domount() has special 667 * logic based on MNT_UPDATE. This is very important 668 * when we want to update the root filesystem. 669 */ 670 TAILQ_FOREACH_SAFE(opt, optlist, link, tmp_opt) { 671 if (strcmp(opt->name, "update") == 0) { 672 fsflags |= MNT_UPDATE; 673 vfs_freeopt(optlist, opt); 674 } 675 else if (strcmp(opt->name, "async") == 0) 676 fsflags |= MNT_ASYNC; 677 else if (strcmp(opt->name, "force") == 0) { 678 fsflags |= MNT_FORCE; 679 vfs_freeopt(optlist, opt); 680 } 681 else if (strcmp(opt->name, "reload") == 0) { 682 fsflags |= MNT_RELOAD; 683 vfs_freeopt(optlist, opt); 684 } 685 else if (strcmp(opt->name, "multilabel") == 0) 686 fsflags |= MNT_MULTILABEL; 687 else if (strcmp(opt->name, "noasync") == 0) 688 fsflags &= ~MNT_ASYNC; 689 else if (strcmp(opt->name, "noatime") == 0) 690 fsflags |= MNT_NOATIME; 691 else if (strcmp(opt->name, "atime") == 0) { 692 free(opt->name, M_MOUNT); 693 opt->name = strdup("nonoatime", M_MOUNT); 694 } 695 else if (strcmp(opt->name, "noclusterr") == 0) 696 fsflags |= MNT_NOCLUSTERR; 697 else if (strcmp(opt->name, "clusterr") == 0) { 698 free(opt->name, M_MOUNT); 699 opt->name = strdup("nonoclusterr", M_MOUNT); 700 } 701 else if (strcmp(opt->name, "noclusterw") == 0) 702 fsflags |= MNT_NOCLUSTERW; 703 else if (strcmp(opt->name, "clusterw") == 0) { 704 free(opt->name, M_MOUNT); 705 opt->name = strdup("nonoclusterw", M_MOUNT); 706 } 707 else if (strcmp(opt->name, "noexec") == 0) 708 fsflags |= MNT_NOEXEC; 709 else if (strcmp(opt->name, "exec") == 0) { 710 free(opt->name, M_MOUNT); 711 opt->name = strdup("nonoexec", M_MOUNT); 712 } 713 else if (strcmp(opt->name, "nosuid") == 0) 714 fsflags |= MNT_NOSUID; 715 else if (strcmp(opt->name, "suid") == 0) { 716 free(opt->name, M_MOUNT); 717 opt->name = strdup("nonosuid", M_MOUNT); 718 } 719 else if (strcmp(opt->name, "nosymfollow") == 0) 720 fsflags |= MNT_NOSYMFOLLOW; 721 else if (strcmp(opt->name, "symfollow") == 0) { 722 free(opt->name, M_MOUNT); 723 opt->name = strdup("nonosymfollow", M_MOUNT); 724 } 725 else if (strcmp(opt->name, "noro") == 0) { 726 fsflags &= ~MNT_RDONLY; 727 autoro = false; 728 } 729 else if (strcmp(opt->name, "rw") == 0) { 730 fsflags &= ~MNT_RDONLY; 731 autoro = false; 732 } 733 else if (strcmp(opt->name, "ro") == 0) { 734 fsflags |= MNT_RDONLY; 735 autoro = false; 736 } 737 else if (strcmp(opt->name, "rdonly") == 0) { 738 free(opt->name, M_MOUNT); 739 opt->name = strdup("ro", M_MOUNT); 740 fsflags |= MNT_RDONLY; 741 autoro = false; 742 } 743 else if (strcmp(opt->name, "autoro") == 0) { 744 vfs_freeopt(optlist, opt); 745 autoro = true; 746 } 747 else if (strcmp(opt->name, "suiddir") == 0) 748 fsflags |= MNT_SUIDDIR; 749 else if (strcmp(opt->name, "sync") == 0) 750 fsflags |= MNT_SYNCHRONOUS; 751 else if (strcmp(opt->name, "union") == 0) 752 fsflags |= MNT_UNION; 753 else if (strcmp(opt->name, "automounted") == 0) { 754 fsflags |= MNT_AUTOMOUNTED; 755 vfs_freeopt(optlist, opt); 756 } 757 } 758 759 /* 760 * Be ultra-paranoid about making sure the type and fspath 761 * variables will fit in our mp buffers, including the 762 * terminating NUL. 763 */ 764 if (fstypelen > MFSNAMELEN || fspathlen > MNAMELEN) { 765 error = ENAMETOOLONG; 766 goto bail; 767 } 768 769 error = vfs_domount(td, fstype, fspath, fsflags, &optlist); 770 771 /* 772 * See if we can mount in the read-only mode if the error code suggests 773 * that it could be possible and the mount options allow for that. 774 * Never try it if "[no]{ro|rw}" has been explicitly requested and not 775 * overridden by "autoro". 776 */ 777 if (autoro && vfs_should_downgrade_to_ro_mount(fsflags, error)) { 778 printf("%s: R/W mount failed, possibly R/O media," 779 " trying R/O mount\n", __func__); 780 fsflags |= MNT_RDONLY; 781 error = vfs_domount(td, fstype, fspath, fsflags, &optlist); 782 } 783 bail: 784 /* copyout the errmsg */ 785 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt) 786 && errmsg_len > 0 && errmsg != NULL) { 787 if (fsoptions->uio_segflg == UIO_SYSSPACE) { 788 bcopy(errmsg, 789 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 790 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 791 } else { 792 copyout(errmsg, 793 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 794 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 795 } 796 } 797 798 if (optlist != NULL) 799 vfs_freeopts(optlist); 800 return (error); 801 } 802 803 /* 804 * Old mount API. 805 */ 806 #ifndef _SYS_SYSPROTO_H_ 807 struct mount_args { 808 char *type; 809 char *path; 810 int flags; 811 caddr_t data; 812 }; 813 #endif 814 /* ARGSUSED */ 815 int 816 sys_mount(struct thread *td, struct mount_args *uap) 817 { 818 char *fstype; 819 struct vfsconf *vfsp = NULL; 820 struct mntarg *ma = NULL; 821 uint64_t flags; 822 int error; 823 824 /* 825 * Mount flags are now 64-bits. On 32-bit architectures only 826 * 32-bits are passed in, but from here on everything handles 827 * 64-bit flags correctly. 828 */ 829 flags = uap->flags; 830 831 AUDIT_ARG_FFLAGS(flags); 832 833 /* 834 * Filter out MNT_ROOTFS. We do not want clients of mount() in 835 * userspace to set this flag, but we must filter it out if we want 836 * MNT_UPDATE on the root file system to work. 837 * MNT_ROOTFS should only be set by the kernel when mounting its 838 * root file system. 839 */ 840 flags &= ~MNT_ROOTFS; 841 842 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 843 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 844 if (error) { 845 free(fstype, M_TEMP); 846 return (error); 847 } 848 849 AUDIT_ARG_TEXT(fstype); 850 vfsp = vfs_byname_kld(fstype, td, &error); 851 free(fstype, M_TEMP); 852 if (vfsp == NULL) 853 return (ENOENT); 854 if (((vfsp->vfc_flags & VFCF_SBDRY) != 0 && 855 vfsp->vfc_vfsops_sd->vfs_cmount == NULL) || 856 ((vfsp->vfc_flags & VFCF_SBDRY) == 0 && 857 vfsp->vfc_vfsops->vfs_cmount == NULL)) 858 return (EOPNOTSUPP); 859 860 ma = mount_argsu(ma, "fstype", uap->type, MFSNAMELEN); 861 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 862 ma = mount_argb(ma, flags & MNT_RDONLY, "noro"); 863 ma = mount_argb(ma, !(flags & MNT_NOSUID), "nosuid"); 864 ma = mount_argb(ma, !(flags & MNT_NOEXEC), "noexec"); 865 866 if ((vfsp->vfc_flags & VFCF_SBDRY) != 0) 867 return (vfsp->vfc_vfsops_sd->vfs_cmount(ma, uap->data, flags)); 868 return (vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, flags)); 869 } 870 871 /* 872 * vfs_domount_first(): first file system mount (not update) 873 */ 874 static int 875 vfs_domount_first( 876 struct thread *td, /* Calling thread. */ 877 struct vfsconf *vfsp, /* File system type. */ 878 char *fspath, /* Mount path. */ 879 struct vnode *vp, /* Vnode to be covered. */ 880 uint64_t fsflags, /* Flags common to all filesystems. */ 881 struct vfsoptlist **optlist /* Options local to the filesystem. */ 882 ) 883 { 884 struct vattr va; 885 struct mount *mp; 886 struct vnode *newdp; 887 int error, error1; 888 889 ASSERT_VOP_ELOCKED(vp, __func__); 890 KASSERT((fsflags & MNT_UPDATE) == 0, ("MNT_UPDATE shouldn't be here")); 891 892 /* 893 * If the jail of the calling thread lacks permission for this type of 894 * file system, deny immediately. 895 */ 896 if (jailed(td->td_ucred) && !prison_allow(td->td_ucred, 897 vfsp->vfc_prison_flag)) { 898 vput(vp); 899 return (EPERM); 900 } 901 902 /* 903 * If the user is not root, ensure that they own the directory 904 * onto which we are attempting to mount. 905 */ 906 error = VOP_GETATTR(vp, &va, td->td_ucred); 907 if (error == 0 && va.va_uid != td->td_ucred->cr_uid) 908 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN); 909 if (error == 0) 910 error = vinvalbuf(vp, V_SAVE, 0, 0); 911 if (error == 0 && vp->v_type != VDIR) 912 error = ENOTDIR; 913 if (error == 0) { 914 VI_LOCK(vp); 915 if ((vp->v_iflag & VI_MOUNT) == 0 && vp->v_mountedhere == NULL) 916 vp->v_iflag |= VI_MOUNT; 917 else 918 error = EBUSY; 919 VI_UNLOCK(vp); 920 } 921 if (error != 0) { 922 vput(vp); 923 return (error); 924 } 925 VOP_UNLOCK(vp, 0); 926 927 /* Allocate and initialize the filesystem. */ 928 mp = vfs_mount_alloc(vp, vfsp, fspath, td->td_ucred); 929 /* XXXMAC: pass to vfs_mount_alloc? */ 930 mp->mnt_optnew = *optlist; 931 /* Set the mount level flags. */ 932 mp->mnt_flag = (fsflags & (MNT_UPDATEMASK | MNT_ROOTFS | MNT_RDONLY)); 933 934 /* 935 * Mount the filesystem. 936 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 937 * get. No freeing of cn_pnbuf. 938 */ 939 error1 = 0; 940 if ((error = VFS_MOUNT(mp)) != 0 || 941 (error1 = VFS_STATFS(mp, &mp->mnt_stat)) != 0 || 942 (error1 = VFS_ROOT(mp, LK_EXCLUSIVE, &newdp)) != 0) { 943 if (error1 != 0) { 944 error = error1; 945 if ((error1 = VFS_UNMOUNT(mp, 0)) != 0) 946 printf("VFS_UNMOUNT returned %d\n", error1); 947 } 948 vfs_unbusy(mp); 949 mp->mnt_vnodecovered = NULL; 950 vfs_mount_destroy(mp); 951 VI_LOCK(vp); 952 vp->v_iflag &= ~VI_MOUNT; 953 VI_UNLOCK(vp); 954 vrele(vp); 955 return (error); 956 } 957 VOP_UNLOCK(newdp, 0); 958 959 if (mp->mnt_opt != NULL) 960 vfs_freeopts(mp->mnt_opt); 961 mp->mnt_opt = mp->mnt_optnew; 962 *optlist = NULL; 963 964 /* 965 * Prevent external consumers of mount options from reading mnt_optnew. 966 */ 967 mp->mnt_optnew = NULL; 968 969 MNT_ILOCK(mp); 970 if ((mp->mnt_flag & MNT_ASYNC) != 0 && 971 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0) 972 mp->mnt_kern_flag |= MNTK_ASYNC; 973 else 974 mp->mnt_kern_flag &= ~MNTK_ASYNC; 975 MNT_IUNLOCK(mp); 976 977 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 978 cache_purge(vp); 979 VI_LOCK(vp); 980 vp->v_iflag &= ~VI_MOUNT; 981 VI_UNLOCK(vp); 982 vp->v_mountedhere = mp; 983 /* Place the new filesystem at the end of the mount list. */ 984 mtx_lock(&mountlist_mtx); 985 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 986 mtx_unlock(&mountlist_mtx); 987 vfs_event_signal(NULL, VQ_MOUNT, 0); 988 vn_lock(newdp, LK_EXCLUSIVE | LK_RETRY); 989 VOP_UNLOCK(vp, 0); 990 EVENTHANDLER_DIRECT_INVOKE(vfs_mounted, mp, newdp, td); 991 VOP_UNLOCK(newdp, 0); 992 mountcheckdirs(vp, newdp); 993 vrele(newdp); 994 if ((mp->mnt_flag & MNT_RDONLY) == 0) 995 vfs_allocate_syncvnode(mp); 996 vfs_op_exit(mp); 997 vfs_unbusy(mp); 998 return (0); 999 } 1000 1001 /* 1002 * vfs_domount_update(): update of mounted file system 1003 */ 1004 static int 1005 vfs_domount_update( 1006 struct thread *td, /* Calling thread. */ 1007 struct vnode *vp, /* Mount point vnode. */ 1008 uint64_t fsflags, /* Flags common to all filesystems. */ 1009 struct vfsoptlist **optlist /* Options local to the filesystem. */ 1010 ) 1011 { 1012 struct export_args export; 1013 void *bufp; 1014 struct mount *mp; 1015 int error, export_error, len; 1016 uint64_t flag; 1017 1018 ASSERT_VOP_ELOCKED(vp, __func__); 1019 KASSERT((fsflags & MNT_UPDATE) != 0, ("MNT_UPDATE should be here")); 1020 mp = vp->v_mount; 1021 1022 if ((vp->v_vflag & VV_ROOT) == 0) { 1023 if (vfs_copyopt(*optlist, "export", &export, sizeof(export)) 1024 == 0) 1025 error = EXDEV; 1026 else 1027 error = EINVAL; 1028 vput(vp); 1029 return (error); 1030 } 1031 1032 /* 1033 * We only allow the filesystem to be reloaded if it 1034 * is currently mounted read-only. 1035 */ 1036 flag = mp->mnt_flag; 1037 if ((fsflags & MNT_RELOAD) != 0 && (flag & MNT_RDONLY) == 0) { 1038 vput(vp); 1039 return (EOPNOTSUPP); /* Needs translation */ 1040 } 1041 /* 1042 * Only privileged root, or (if MNT_USER is set) the user that 1043 * did the original mount is permitted to update it. 1044 */ 1045 error = vfs_suser(mp, td); 1046 if (error != 0) { 1047 vput(vp); 1048 return (error); 1049 } 1050 if (vfs_busy(mp, MBF_NOWAIT)) { 1051 vput(vp); 1052 return (EBUSY); 1053 } 1054 VI_LOCK(vp); 1055 if ((vp->v_iflag & VI_MOUNT) != 0 || vp->v_mountedhere != NULL) { 1056 VI_UNLOCK(vp); 1057 vfs_unbusy(mp); 1058 vput(vp); 1059 return (EBUSY); 1060 } 1061 vp->v_iflag |= VI_MOUNT; 1062 VI_UNLOCK(vp); 1063 VOP_UNLOCK(vp, 0); 1064 1065 vfs_op_enter(mp); 1066 1067 MNT_ILOCK(mp); 1068 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0) { 1069 MNT_IUNLOCK(mp); 1070 error = EBUSY; 1071 goto end; 1072 } 1073 mp->mnt_flag &= ~MNT_UPDATEMASK; 1074 mp->mnt_flag |= fsflags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | 1075 MNT_SNAPSHOT | MNT_ROOTFS | MNT_UPDATEMASK | MNT_RDONLY); 1076 if ((mp->mnt_flag & MNT_ASYNC) == 0) 1077 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1078 MNT_IUNLOCK(mp); 1079 mp->mnt_optnew = *optlist; 1080 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 1081 1082 /* 1083 * Mount the filesystem. 1084 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 1085 * get. No freeing of cn_pnbuf. 1086 */ 1087 error = VFS_MOUNT(mp); 1088 1089 export_error = 0; 1090 /* Process the export option. */ 1091 if (error == 0 && vfs_getopt(mp->mnt_optnew, "export", &bufp, 1092 &len) == 0) { 1093 /* Assume that there is only 1 ABI for each length. */ 1094 switch (len) { 1095 case (sizeof(struct oexport_args)): 1096 bzero(&export, sizeof(export)); 1097 /* FALLTHROUGH */ 1098 case (sizeof(export)): 1099 bcopy(bufp, &export, len); 1100 export_error = vfs_export(mp, &export); 1101 break; 1102 default: 1103 export_error = EINVAL; 1104 break; 1105 } 1106 } 1107 1108 MNT_ILOCK(mp); 1109 if (error == 0) { 1110 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | 1111 MNT_SNAPSHOT); 1112 } else { 1113 /* 1114 * If we fail, restore old mount flags. MNT_QUOTA is special, 1115 * because it is not part of MNT_UPDATEMASK, but it could have 1116 * changed in the meantime if quotactl(2) was called. 1117 * All in all we want current value of MNT_QUOTA, not the old 1118 * one. 1119 */ 1120 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA); 1121 } 1122 if ((mp->mnt_flag & MNT_ASYNC) != 0 && 1123 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0) 1124 mp->mnt_kern_flag |= MNTK_ASYNC; 1125 else 1126 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1127 MNT_IUNLOCK(mp); 1128 1129 if (error != 0) 1130 goto end; 1131 1132 if (mp->mnt_opt != NULL) 1133 vfs_freeopts(mp->mnt_opt); 1134 mp->mnt_opt = mp->mnt_optnew; 1135 *optlist = NULL; 1136 (void)VFS_STATFS(mp, &mp->mnt_stat); 1137 /* 1138 * Prevent external consumers of mount options from reading 1139 * mnt_optnew. 1140 */ 1141 mp->mnt_optnew = NULL; 1142 1143 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1144 vfs_allocate_syncvnode(mp); 1145 else 1146 vfs_deallocate_syncvnode(mp); 1147 end: 1148 vfs_op_exit(mp); 1149 vfs_unbusy(mp); 1150 VI_LOCK(vp); 1151 vp->v_iflag &= ~VI_MOUNT; 1152 VI_UNLOCK(vp); 1153 vrele(vp); 1154 return (error != 0 ? error : export_error); 1155 } 1156 1157 /* 1158 * vfs_domount(): actually attempt a filesystem mount. 1159 */ 1160 static int 1161 vfs_domount( 1162 struct thread *td, /* Calling thread. */ 1163 const char *fstype, /* Filesystem type. */ 1164 char *fspath, /* Mount path. */ 1165 uint64_t fsflags, /* Flags common to all filesystems. */ 1166 struct vfsoptlist **optlist /* Options local to the filesystem. */ 1167 ) 1168 { 1169 struct vfsconf *vfsp; 1170 struct nameidata nd; 1171 struct vnode *vp; 1172 char *pathbuf; 1173 int error; 1174 1175 /* 1176 * Be ultra-paranoid about making sure the type and fspath 1177 * variables will fit in our mp buffers, including the 1178 * terminating NUL. 1179 */ 1180 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 1181 return (ENAMETOOLONG); 1182 1183 if (jailed(td->td_ucred) || usermount == 0) { 1184 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0) 1185 return (error); 1186 } 1187 1188 /* 1189 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 1190 */ 1191 if (fsflags & MNT_EXPORTED) { 1192 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED); 1193 if (error) 1194 return (error); 1195 } 1196 if (fsflags & MNT_SUIDDIR) { 1197 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR); 1198 if (error) 1199 return (error); 1200 } 1201 /* 1202 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users. 1203 */ 1204 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) { 1205 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0) 1206 fsflags |= MNT_NOSUID | MNT_USER; 1207 } 1208 1209 /* Load KLDs before we lock the covered vnode to avoid reversals. */ 1210 vfsp = NULL; 1211 if ((fsflags & MNT_UPDATE) == 0) { 1212 /* Don't try to load KLDs if we're mounting the root. */ 1213 if (fsflags & MNT_ROOTFS) 1214 vfsp = vfs_byname(fstype); 1215 else 1216 vfsp = vfs_byname_kld(fstype, td, &error); 1217 if (vfsp == NULL) 1218 return (ENODEV); 1219 } 1220 1221 /* 1222 * Get vnode to be covered or mount point's vnode in case of MNT_UPDATE. 1223 */ 1224 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 1225 UIO_SYSSPACE, fspath, td); 1226 error = namei(&nd); 1227 if (error != 0) 1228 return (error); 1229 NDFREE(&nd, NDF_ONLY_PNBUF); 1230 vp = nd.ni_vp; 1231 if ((fsflags & MNT_UPDATE) == 0) { 1232 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1233 strcpy(pathbuf, fspath); 1234 error = vn_path_to_global_path(td, vp, pathbuf, MNAMELEN); 1235 /* debug.disablefullpath == 1 results in ENODEV */ 1236 if (error == 0 || error == ENODEV) { 1237 error = vfs_domount_first(td, vfsp, pathbuf, vp, 1238 fsflags, optlist); 1239 } 1240 free(pathbuf, M_TEMP); 1241 } else 1242 error = vfs_domount_update(td, vp, fsflags, optlist); 1243 1244 return (error); 1245 } 1246 1247 /* 1248 * Unmount a filesystem. 1249 * 1250 * Note: unmount takes a path to the vnode mounted on as argument, not 1251 * special file (as before). 1252 */ 1253 #ifndef _SYS_SYSPROTO_H_ 1254 struct unmount_args { 1255 char *path; 1256 int flags; 1257 }; 1258 #endif 1259 /* ARGSUSED */ 1260 int 1261 sys_unmount(struct thread *td, struct unmount_args *uap) 1262 { 1263 struct nameidata nd; 1264 struct mount *mp; 1265 char *pathbuf; 1266 int error, id0, id1; 1267 1268 AUDIT_ARG_VALUE(uap->flags); 1269 if (jailed(td->td_ucred) || usermount == 0) { 1270 error = priv_check(td, PRIV_VFS_UNMOUNT); 1271 if (error) 1272 return (error); 1273 } 1274 1275 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1276 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 1277 if (error) { 1278 free(pathbuf, M_TEMP); 1279 return (error); 1280 } 1281 if (uap->flags & MNT_BYFSID) { 1282 AUDIT_ARG_TEXT(pathbuf); 1283 /* Decode the filesystem ID. */ 1284 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 1285 free(pathbuf, M_TEMP); 1286 return (EINVAL); 1287 } 1288 1289 mtx_lock(&mountlist_mtx); 1290 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1291 if (mp->mnt_stat.f_fsid.val[0] == id0 && 1292 mp->mnt_stat.f_fsid.val[1] == id1) { 1293 vfs_ref(mp); 1294 break; 1295 } 1296 } 1297 mtx_unlock(&mountlist_mtx); 1298 } else { 1299 /* 1300 * Try to find global path for path argument. 1301 */ 1302 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, 1303 UIO_SYSSPACE, pathbuf, td); 1304 if (namei(&nd) == 0) { 1305 NDFREE(&nd, NDF_ONLY_PNBUF); 1306 error = vn_path_to_global_path(td, nd.ni_vp, pathbuf, 1307 MNAMELEN); 1308 if (error == 0 || error == ENODEV) 1309 vput(nd.ni_vp); 1310 } 1311 mtx_lock(&mountlist_mtx); 1312 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1313 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) { 1314 vfs_ref(mp); 1315 break; 1316 } 1317 } 1318 mtx_unlock(&mountlist_mtx); 1319 } 1320 free(pathbuf, M_TEMP); 1321 if (mp == NULL) { 1322 /* 1323 * Previously we returned ENOENT for a nonexistent path and 1324 * EINVAL for a non-mountpoint. We cannot tell these apart 1325 * now, so in the !MNT_BYFSID case return the more likely 1326 * EINVAL for compatibility. 1327 */ 1328 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 1329 } 1330 1331 /* 1332 * Don't allow unmounting the root filesystem. 1333 */ 1334 if (mp->mnt_flag & MNT_ROOTFS) { 1335 vfs_rel(mp); 1336 return (EINVAL); 1337 } 1338 error = dounmount(mp, uap->flags, td); 1339 return (error); 1340 } 1341 1342 /* 1343 * Return error if any of the vnodes, ignoring the root vnode 1344 * and the syncer vnode, have non-zero usecount. 1345 * 1346 * This function is purely advisory - it can return false positives 1347 * and negatives. 1348 */ 1349 static int 1350 vfs_check_usecounts(struct mount *mp) 1351 { 1352 struct vnode *vp, *mvp; 1353 1354 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) { 1355 if ((vp->v_vflag & VV_ROOT) == 0 && vp->v_type != VNON && 1356 vp->v_usecount != 0) { 1357 VI_UNLOCK(vp); 1358 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp); 1359 return (EBUSY); 1360 } 1361 VI_UNLOCK(vp); 1362 } 1363 1364 return (0); 1365 } 1366 1367 static void 1368 dounmount_cleanup(struct mount *mp, struct vnode *coveredvp, int mntkflags) 1369 { 1370 1371 mtx_assert(MNT_MTX(mp), MA_OWNED); 1372 mp->mnt_kern_flag &= ~mntkflags; 1373 if ((mp->mnt_kern_flag & MNTK_MWAIT) != 0) { 1374 mp->mnt_kern_flag &= ~MNTK_MWAIT; 1375 wakeup(mp); 1376 } 1377 vfs_op_exit_locked(mp); 1378 MNT_IUNLOCK(mp); 1379 if (coveredvp != NULL) { 1380 VOP_UNLOCK(coveredvp, 0); 1381 vdrop(coveredvp); 1382 } 1383 vn_finished_write(mp); 1384 } 1385 1386 /* 1387 * There are various reference counters associated with the mount point. 1388 * Normally it is permitted to modify them without taking the mnt ilock, 1389 * but this behavior can be temporarily disabled if stable value is needed 1390 * or callers are expected to block (e.g. to not allow new users during 1391 * forced unmount). 1392 */ 1393 void 1394 vfs_op_enter(struct mount *mp) 1395 { 1396 int cpu; 1397 1398 MNT_ILOCK(mp); 1399 mp->mnt_vfs_ops++; 1400 if (mp->mnt_vfs_ops > 1) { 1401 MNT_IUNLOCK(mp); 1402 return; 1403 } 1404 /* 1405 * Paired with a fence in vfs_op_thread_enter(). See the comment 1406 * above it for details. 1407 */ 1408 atomic_thread_fence_seq_cst(); 1409 vfs_op_barrier_wait(mp); 1410 /* 1411 * Paired with a fence in vfs_op_thread_exit(). 1412 */ 1413 atomic_thread_fence_acq(); 1414 CPU_FOREACH(cpu) { 1415 mp->mnt_ref += 1416 zpcpu_replace_cpu(mp->mnt_ref_pcpu, 0, cpu); 1417 mp->mnt_lockref += 1418 zpcpu_replace_cpu(mp->mnt_lockref_pcpu, 0, cpu); 1419 mp->mnt_writeopcount += 1420 zpcpu_replace_cpu(mp->mnt_writeopcount_pcpu, 0, cpu); 1421 } 1422 MNT_IUNLOCK(mp); 1423 vfs_assert_mount_counters(mp); 1424 } 1425 1426 void 1427 vfs_op_exit_locked(struct mount *mp) 1428 { 1429 1430 mtx_assert(MNT_MTX(mp), MA_OWNED); 1431 1432 if (mp->mnt_vfs_ops <= 0) 1433 panic("%s: invalid vfs_ops count %d for mp %p\n", 1434 __func__, mp->mnt_vfs_ops, mp); 1435 mp->mnt_vfs_ops--; 1436 } 1437 1438 void 1439 vfs_op_exit(struct mount *mp) 1440 { 1441 1442 MNT_ILOCK(mp); 1443 vfs_op_exit_locked(mp); 1444 MNT_IUNLOCK(mp); 1445 } 1446 1447 /* 1448 * It is assumed the caller already posted at least an acquire barrier. 1449 */ 1450 void 1451 vfs_op_barrier_wait(struct mount *mp) 1452 { 1453 int *in_op; 1454 int cpu; 1455 1456 CPU_FOREACH(cpu) { 1457 in_op = zpcpu_get_cpu(mp->mnt_thread_in_ops_pcpu, cpu); 1458 while (atomic_load_int(in_op)) 1459 cpu_spinwait(); 1460 } 1461 } 1462 1463 #ifdef DIAGNOSTIC 1464 void 1465 vfs_assert_mount_counters(struct mount *mp) 1466 { 1467 int cpu; 1468 1469 if (mp->mnt_vfs_ops == 0) 1470 return; 1471 1472 CPU_FOREACH(cpu) { 1473 if (*(int *)zpcpu_get_cpu(mp->mnt_ref_pcpu, cpu) != 0 || 1474 *(int *)zpcpu_get_cpu(mp->mnt_lockref_pcpu, cpu) != 0 || 1475 *(int *)zpcpu_get_cpu(mp->mnt_writeopcount_pcpu, cpu) != 0) 1476 vfs_dump_mount_counters(mp); 1477 } 1478 } 1479 1480 void 1481 vfs_dump_mount_counters(struct mount *mp) 1482 { 1483 int cpu, *count; 1484 int ref, lockref, writeopcount; 1485 1486 printf("%s: mp %p vfs_ops %d\n", __func__, mp, mp->mnt_vfs_ops); 1487 1488 printf(" ref : "); 1489 ref = mp->mnt_ref; 1490 CPU_FOREACH(cpu) { 1491 count = zpcpu_get_cpu(mp->mnt_ref_pcpu, cpu); 1492 printf("%d ", *count); 1493 ref += *count; 1494 } 1495 printf("\n"); 1496 printf(" lockref : "); 1497 lockref = mp->mnt_lockref; 1498 CPU_FOREACH(cpu) { 1499 count = zpcpu_get_cpu(mp->mnt_lockref_pcpu, cpu); 1500 printf("%d ", *count); 1501 lockref += *count; 1502 } 1503 printf("\n"); 1504 printf("writeopcount: "); 1505 writeopcount = mp->mnt_writeopcount; 1506 CPU_FOREACH(cpu) { 1507 count = zpcpu_get_cpu(mp->mnt_writeopcount_pcpu, cpu); 1508 printf("%d ", *count); 1509 writeopcount += *count; 1510 } 1511 printf("\n"); 1512 1513 printf("counter struct total\n"); 1514 printf("ref %-5d %-5d\n", mp->mnt_ref, ref); 1515 printf("lockref %-5d %-5d\n", mp->mnt_lockref, lockref); 1516 printf("writeopcount %-5d %-5d\n", mp->mnt_writeopcount, writeopcount); 1517 1518 panic("invalid counts on struct mount"); 1519 } 1520 #endif 1521 1522 int 1523 vfs_mount_fetch_counter(struct mount *mp, enum mount_counter which) 1524 { 1525 int *base, *pcpu; 1526 int cpu, sum; 1527 1528 switch (which) { 1529 case MNT_COUNT_REF: 1530 base = &mp->mnt_ref; 1531 pcpu = mp->mnt_ref_pcpu; 1532 break; 1533 case MNT_COUNT_LOCKREF: 1534 base = &mp->mnt_lockref; 1535 pcpu = mp->mnt_lockref_pcpu; 1536 break; 1537 case MNT_COUNT_WRITEOPCOUNT: 1538 base = &mp->mnt_writeopcount; 1539 pcpu = mp->mnt_writeopcount_pcpu; 1540 break; 1541 } 1542 1543 sum = *base; 1544 CPU_FOREACH(cpu) { 1545 sum += *(int *)zpcpu_get_cpu(pcpu, cpu); 1546 } 1547 return (sum); 1548 } 1549 1550 /* 1551 * Do the actual filesystem unmount. 1552 */ 1553 int 1554 dounmount(struct mount *mp, int flags, struct thread *td) 1555 { 1556 struct vnode *coveredvp; 1557 int error; 1558 uint64_t async_flag; 1559 int mnt_gen_r; 1560 1561 if ((coveredvp = mp->mnt_vnodecovered) != NULL) { 1562 mnt_gen_r = mp->mnt_gen; 1563 VI_LOCK(coveredvp); 1564 vholdl(coveredvp); 1565 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY); 1566 /* 1567 * Check for mp being unmounted while waiting for the 1568 * covered vnode lock. 1569 */ 1570 if (coveredvp->v_mountedhere != mp || 1571 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) { 1572 VOP_UNLOCK(coveredvp, 0); 1573 vdrop(coveredvp); 1574 vfs_rel(mp); 1575 return (EBUSY); 1576 } 1577 } 1578 1579 /* 1580 * Only privileged root, or (if MNT_USER is set) the user that did the 1581 * original mount is permitted to unmount this filesystem. 1582 */ 1583 error = vfs_suser(mp, td); 1584 if (error != 0) { 1585 if (coveredvp != NULL) { 1586 VOP_UNLOCK(coveredvp, 0); 1587 vdrop(coveredvp); 1588 } 1589 vfs_rel(mp); 1590 return (error); 1591 } 1592 1593 vfs_op_enter(mp); 1594 1595 vn_start_write(NULL, &mp, V_WAIT | V_MNTREF); 1596 MNT_ILOCK(mp); 1597 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 || 1598 (mp->mnt_flag & MNT_UPDATE) != 0 || 1599 !TAILQ_EMPTY(&mp->mnt_uppers)) { 1600 dounmount_cleanup(mp, coveredvp, 0); 1601 return (EBUSY); 1602 } 1603 mp->mnt_kern_flag |= MNTK_UNMOUNT; 1604 if (flags & MNT_NONBUSY) { 1605 MNT_IUNLOCK(mp); 1606 error = vfs_check_usecounts(mp); 1607 MNT_ILOCK(mp); 1608 if (error != 0) { 1609 dounmount_cleanup(mp, coveredvp, MNTK_UNMOUNT); 1610 return (error); 1611 } 1612 } 1613 /* Allow filesystems to detect that a forced unmount is in progress. */ 1614 if (flags & MNT_FORCE) { 1615 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 1616 MNT_IUNLOCK(mp); 1617 /* 1618 * Must be done after setting MNTK_UNMOUNTF and before 1619 * waiting for mnt_lockref to become 0. 1620 */ 1621 VFS_PURGE(mp); 1622 MNT_ILOCK(mp); 1623 } 1624 error = 0; 1625 if (mp->mnt_lockref) { 1626 mp->mnt_kern_flag |= MNTK_DRAINING; 1627 error = msleep(&mp->mnt_lockref, MNT_MTX(mp), PVFS, 1628 "mount drain", 0); 1629 } 1630 MNT_IUNLOCK(mp); 1631 KASSERT(mp->mnt_lockref == 0, 1632 ("%s: invalid lock refcount in the drain path @ %s:%d", 1633 __func__, __FILE__, __LINE__)); 1634 KASSERT(error == 0, 1635 ("%s: invalid return value for msleep in the drain path @ %s:%d", 1636 __func__, __FILE__, __LINE__)); 1637 1638 if (mp->mnt_flag & MNT_EXPUBLIC) 1639 vfs_setpublicfs(NULL, NULL, NULL); 1640 1641 /* 1642 * From now, we can claim that the use reference on the 1643 * coveredvp is ours, and the ref can be released only by 1644 * successfull unmount by us, or left for later unmount 1645 * attempt. The previously acquired hold reference is no 1646 * longer needed to protect the vnode from reuse. 1647 */ 1648 if (coveredvp != NULL) 1649 vdrop(coveredvp); 1650 1651 vfs_msync(mp, MNT_WAIT); 1652 MNT_ILOCK(mp); 1653 async_flag = mp->mnt_flag & MNT_ASYNC; 1654 mp->mnt_flag &= ~MNT_ASYNC; 1655 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1656 MNT_IUNLOCK(mp); 1657 cache_purgevfs(mp, false); /* remove cache entries for this file sys */ 1658 vfs_deallocate_syncvnode(mp); 1659 if ((mp->mnt_flag & MNT_RDONLY) != 0 || (flags & MNT_FORCE) != 0 || 1660 (error = VFS_SYNC(mp, MNT_WAIT)) == 0) 1661 error = VFS_UNMOUNT(mp, flags); 1662 vn_finished_write(mp); 1663 /* 1664 * If we failed to flush the dirty blocks for this mount point, 1665 * undo all the cdir/rdir and rootvnode changes we made above. 1666 * Unless we failed to do so because the device is reporting that 1667 * it doesn't exist anymore. 1668 */ 1669 if (error && error != ENXIO) { 1670 MNT_ILOCK(mp); 1671 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 1672 MNT_IUNLOCK(mp); 1673 vfs_allocate_syncvnode(mp); 1674 MNT_ILOCK(mp); 1675 } 1676 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1677 mp->mnt_flag |= async_flag; 1678 if ((mp->mnt_flag & MNT_ASYNC) != 0 && 1679 (mp->mnt_kern_flag & MNTK_NOASYNC) == 0) 1680 mp->mnt_kern_flag |= MNTK_ASYNC; 1681 if (mp->mnt_kern_flag & MNTK_MWAIT) { 1682 mp->mnt_kern_flag &= ~MNTK_MWAIT; 1683 wakeup(mp); 1684 } 1685 vfs_op_exit_locked(mp); 1686 MNT_IUNLOCK(mp); 1687 if (coveredvp) 1688 VOP_UNLOCK(coveredvp, 0); 1689 return (error); 1690 } 1691 mtx_lock(&mountlist_mtx); 1692 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1693 mtx_unlock(&mountlist_mtx); 1694 EVENTHANDLER_DIRECT_INVOKE(vfs_unmounted, mp, td); 1695 if (coveredvp != NULL) { 1696 coveredvp->v_mountedhere = NULL; 1697 VOP_UNLOCK(coveredvp, 0); 1698 } 1699 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 1700 if (rootvnode != NULL && mp == rootvnode->v_mount) { 1701 vrele(rootvnode); 1702 rootvnode = NULL; 1703 } 1704 if (mp == rootdevmp) 1705 rootdevmp = NULL; 1706 vfs_mount_destroy(mp); 1707 return (0); 1708 } 1709 1710 /* 1711 * Report errors during filesystem mounting. 1712 */ 1713 void 1714 vfs_mount_error(struct mount *mp, const char *fmt, ...) 1715 { 1716 struct vfsoptlist *moptlist = mp->mnt_optnew; 1717 va_list ap; 1718 int error, len; 1719 char *errmsg; 1720 1721 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len); 1722 if (error || errmsg == NULL || len <= 0) 1723 return; 1724 1725 va_start(ap, fmt); 1726 vsnprintf(errmsg, (size_t)len, fmt, ap); 1727 va_end(ap); 1728 } 1729 1730 void 1731 vfs_opterror(struct vfsoptlist *opts, const char *fmt, ...) 1732 { 1733 va_list ap; 1734 int error, len; 1735 char *errmsg; 1736 1737 error = vfs_getopt(opts, "errmsg", (void **)&errmsg, &len); 1738 if (error || errmsg == NULL || len <= 0) 1739 return; 1740 1741 va_start(ap, fmt); 1742 vsnprintf(errmsg, (size_t)len, fmt, ap); 1743 va_end(ap); 1744 } 1745 1746 /* 1747 * --------------------------------------------------------------------- 1748 * Functions for querying mount options/arguments from filesystems. 1749 */ 1750 1751 /* 1752 * Check that no unknown options are given 1753 */ 1754 int 1755 vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1756 { 1757 struct vfsopt *opt; 1758 char errmsg[255]; 1759 const char **t, *p, *q; 1760 int ret = 0; 1761 1762 TAILQ_FOREACH(opt, opts, link) { 1763 p = opt->name; 1764 q = NULL; 1765 if (p[0] == 'n' && p[1] == 'o') 1766 q = p + 2; 1767 for(t = global_opts; *t != NULL; t++) { 1768 if (strcmp(*t, p) == 0) 1769 break; 1770 if (q != NULL) { 1771 if (strcmp(*t, q) == 0) 1772 break; 1773 } 1774 } 1775 if (*t != NULL) 1776 continue; 1777 for(t = legal; *t != NULL; t++) { 1778 if (strcmp(*t, p) == 0) 1779 break; 1780 if (q != NULL) { 1781 if (strcmp(*t, q) == 0) 1782 break; 1783 } 1784 } 1785 if (*t != NULL) 1786 continue; 1787 snprintf(errmsg, sizeof(errmsg), 1788 "mount option <%s> is unknown", p); 1789 ret = EINVAL; 1790 } 1791 if (ret != 0) { 1792 TAILQ_FOREACH(opt, opts, link) { 1793 if (strcmp(opt->name, "errmsg") == 0) { 1794 strncpy((char *)opt->value, errmsg, opt->len); 1795 break; 1796 } 1797 } 1798 if (opt == NULL) 1799 printf("%s\n", errmsg); 1800 } 1801 return (ret); 1802 } 1803 1804 /* 1805 * Get a mount option by its name. 1806 * 1807 * Return 0 if the option was found, ENOENT otherwise. 1808 * If len is non-NULL it will be filled with the length 1809 * of the option. If buf is non-NULL, it will be filled 1810 * with the address of the option. 1811 */ 1812 int 1813 vfs_getopt(struct vfsoptlist *opts, const char *name, void **buf, int *len) 1814 { 1815 struct vfsopt *opt; 1816 1817 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1818 1819 TAILQ_FOREACH(opt, opts, link) { 1820 if (strcmp(name, opt->name) == 0) { 1821 opt->seen = 1; 1822 if (len != NULL) 1823 *len = opt->len; 1824 if (buf != NULL) 1825 *buf = opt->value; 1826 return (0); 1827 } 1828 } 1829 return (ENOENT); 1830 } 1831 1832 int 1833 vfs_getopt_pos(struct vfsoptlist *opts, const char *name) 1834 { 1835 struct vfsopt *opt; 1836 1837 if (opts == NULL) 1838 return (-1); 1839 1840 TAILQ_FOREACH(opt, opts, link) { 1841 if (strcmp(name, opt->name) == 0) { 1842 opt->seen = 1; 1843 return (opt->pos); 1844 } 1845 } 1846 return (-1); 1847 } 1848 1849 int 1850 vfs_getopt_size(struct vfsoptlist *opts, const char *name, off_t *value) 1851 { 1852 char *opt_value, *vtp; 1853 quad_t iv; 1854 int error, opt_len; 1855 1856 error = vfs_getopt(opts, name, (void **)&opt_value, &opt_len); 1857 if (error != 0) 1858 return (error); 1859 if (opt_len == 0 || opt_value == NULL) 1860 return (EINVAL); 1861 if (opt_value[0] == '\0' || opt_value[opt_len - 1] != '\0') 1862 return (EINVAL); 1863 iv = strtoq(opt_value, &vtp, 0); 1864 if (vtp == opt_value || (vtp[0] != '\0' && vtp[1] != '\0')) 1865 return (EINVAL); 1866 if (iv < 0) 1867 return (EINVAL); 1868 switch (vtp[0]) { 1869 case 't': case 'T': 1870 iv *= 1024; 1871 /* FALLTHROUGH */ 1872 case 'g': case 'G': 1873 iv *= 1024; 1874 /* FALLTHROUGH */ 1875 case 'm': case 'M': 1876 iv *= 1024; 1877 /* FALLTHROUGH */ 1878 case 'k': case 'K': 1879 iv *= 1024; 1880 case '\0': 1881 break; 1882 default: 1883 return (EINVAL); 1884 } 1885 *value = iv; 1886 1887 return (0); 1888 } 1889 1890 char * 1891 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1892 { 1893 struct vfsopt *opt; 1894 1895 *error = 0; 1896 TAILQ_FOREACH(opt, opts, link) { 1897 if (strcmp(name, opt->name) != 0) 1898 continue; 1899 opt->seen = 1; 1900 if (opt->len == 0 || 1901 ((char *)opt->value)[opt->len - 1] != '\0') { 1902 *error = EINVAL; 1903 return (NULL); 1904 } 1905 return (opt->value); 1906 } 1907 *error = ENOENT; 1908 return (NULL); 1909 } 1910 1911 int 1912 vfs_flagopt(struct vfsoptlist *opts, const char *name, uint64_t *w, 1913 uint64_t val) 1914 { 1915 struct vfsopt *opt; 1916 1917 TAILQ_FOREACH(opt, opts, link) { 1918 if (strcmp(name, opt->name) == 0) { 1919 opt->seen = 1; 1920 if (w != NULL) 1921 *w |= val; 1922 return (1); 1923 } 1924 } 1925 if (w != NULL) 1926 *w &= ~val; 1927 return (0); 1928 } 1929 1930 int 1931 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1932 { 1933 va_list ap; 1934 struct vfsopt *opt; 1935 int ret; 1936 1937 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1938 1939 TAILQ_FOREACH(opt, opts, link) { 1940 if (strcmp(name, opt->name) != 0) 1941 continue; 1942 opt->seen = 1; 1943 if (opt->len == 0 || opt->value == NULL) 1944 return (0); 1945 if (((char *)opt->value)[opt->len - 1] != '\0') 1946 return (0); 1947 va_start(ap, fmt); 1948 ret = vsscanf(opt->value, fmt, ap); 1949 va_end(ap); 1950 return (ret); 1951 } 1952 return (0); 1953 } 1954 1955 int 1956 vfs_setopt(struct vfsoptlist *opts, const char *name, void *value, int len) 1957 { 1958 struct vfsopt *opt; 1959 1960 TAILQ_FOREACH(opt, opts, link) { 1961 if (strcmp(name, opt->name) != 0) 1962 continue; 1963 opt->seen = 1; 1964 if (opt->value == NULL) 1965 opt->len = len; 1966 else { 1967 if (opt->len != len) 1968 return (EINVAL); 1969 bcopy(value, opt->value, len); 1970 } 1971 return (0); 1972 } 1973 return (ENOENT); 1974 } 1975 1976 int 1977 vfs_setopt_part(struct vfsoptlist *opts, const char *name, void *value, int len) 1978 { 1979 struct vfsopt *opt; 1980 1981 TAILQ_FOREACH(opt, opts, link) { 1982 if (strcmp(name, opt->name) != 0) 1983 continue; 1984 opt->seen = 1; 1985 if (opt->value == NULL) 1986 opt->len = len; 1987 else { 1988 if (opt->len < len) 1989 return (EINVAL); 1990 opt->len = len; 1991 bcopy(value, opt->value, len); 1992 } 1993 return (0); 1994 } 1995 return (ENOENT); 1996 } 1997 1998 int 1999 vfs_setopts(struct vfsoptlist *opts, const char *name, const char *value) 2000 { 2001 struct vfsopt *opt; 2002 2003 TAILQ_FOREACH(opt, opts, link) { 2004 if (strcmp(name, opt->name) != 0) 2005 continue; 2006 opt->seen = 1; 2007 if (opt->value == NULL) 2008 opt->len = strlen(value) + 1; 2009 else if (strlcpy(opt->value, value, opt->len) >= opt->len) 2010 return (EINVAL); 2011 return (0); 2012 } 2013 return (ENOENT); 2014 } 2015 2016 /* 2017 * Find and copy a mount option. 2018 * 2019 * The size of the buffer has to be specified 2020 * in len, if it is not the same length as the 2021 * mount option, EINVAL is returned. 2022 * Returns ENOENT if the option is not found. 2023 */ 2024 int 2025 vfs_copyopt(struct vfsoptlist *opts, const char *name, void *dest, int len) 2026 { 2027 struct vfsopt *opt; 2028 2029 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 2030 2031 TAILQ_FOREACH(opt, opts, link) { 2032 if (strcmp(name, opt->name) == 0) { 2033 opt->seen = 1; 2034 if (len != opt->len) 2035 return (EINVAL); 2036 bcopy(opt->value, dest, opt->len); 2037 return (0); 2038 } 2039 } 2040 return (ENOENT); 2041 } 2042 2043 int 2044 __vfs_statfs(struct mount *mp, struct statfs *sbp) 2045 { 2046 2047 /* 2048 * Filesystems only fill in part of the structure for updates, we 2049 * have to read the entirety first to get all content. 2050 */ 2051 memcpy(sbp, &mp->mnt_stat, sizeof(*sbp)); 2052 2053 /* 2054 * Set these in case the underlying filesystem fails to do so. 2055 */ 2056 sbp->f_version = STATFS_VERSION; 2057 sbp->f_namemax = NAME_MAX; 2058 sbp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; 2059 2060 return (mp->mnt_op->vfs_statfs(mp, sbp)); 2061 } 2062 2063 void 2064 vfs_mountedfrom(struct mount *mp, const char *from) 2065 { 2066 2067 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 2068 strlcpy(mp->mnt_stat.f_mntfromname, from, 2069 sizeof mp->mnt_stat.f_mntfromname); 2070 } 2071 2072 /* 2073 * --------------------------------------------------------------------- 2074 * This is the api for building mount args and mounting filesystems from 2075 * inside the kernel. 2076 * 2077 * The API works by accumulation of individual args. First error is 2078 * latched. 2079 * 2080 * XXX: should be documented in new manpage kernel_mount(9) 2081 */ 2082 2083 /* A memory allocation which must be freed when we are done */ 2084 struct mntaarg { 2085 SLIST_ENTRY(mntaarg) next; 2086 }; 2087 2088 /* The header for the mount arguments */ 2089 struct mntarg { 2090 struct iovec *v; 2091 int len; 2092 int error; 2093 SLIST_HEAD(, mntaarg) list; 2094 }; 2095 2096 /* 2097 * Add a boolean argument. 2098 * 2099 * flag is the boolean value. 2100 * name must start with "no". 2101 */ 2102 struct mntarg * 2103 mount_argb(struct mntarg *ma, int flag, const char *name) 2104 { 2105 2106 KASSERT(name[0] == 'n' && name[1] == 'o', 2107 ("mount_argb(...,%s): name must start with 'no'", name)); 2108 2109 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 2110 } 2111 2112 /* 2113 * Add an argument printf style 2114 */ 2115 struct mntarg * 2116 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 2117 { 2118 va_list ap; 2119 struct mntaarg *maa; 2120 struct sbuf *sb; 2121 int len; 2122 2123 if (ma == NULL) { 2124 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2125 SLIST_INIT(&ma->list); 2126 } 2127 if (ma->error) 2128 return (ma); 2129 2130 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2131 M_MOUNT, M_WAITOK); 2132 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2133 ma->v[ma->len].iov_len = strlen(name) + 1; 2134 ma->len++; 2135 2136 sb = sbuf_new_auto(); 2137 va_start(ap, fmt); 2138 sbuf_vprintf(sb, fmt, ap); 2139 va_end(ap); 2140 sbuf_finish(sb); 2141 len = sbuf_len(sb) + 1; 2142 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2143 SLIST_INSERT_HEAD(&ma->list, maa, next); 2144 bcopy(sbuf_data(sb), maa + 1, len); 2145 sbuf_delete(sb); 2146 2147 ma->v[ma->len].iov_base = maa + 1; 2148 ma->v[ma->len].iov_len = len; 2149 ma->len++; 2150 2151 return (ma); 2152 } 2153 2154 /* 2155 * Add an argument which is a userland string. 2156 */ 2157 struct mntarg * 2158 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 2159 { 2160 struct mntaarg *maa; 2161 char *tbuf; 2162 2163 if (val == NULL) 2164 return (ma); 2165 if (ma == NULL) { 2166 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2167 SLIST_INIT(&ma->list); 2168 } 2169 if (ma->error) 2170 return (ma); 2171 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2172 SLIST_INSERT_HEAD(&ma->list, maa, next); 2173 tbuf = (void *)(maa + 1); 2174 ma->error = copyinstr(val, tbuf, len, NULL); 2175 return (mount_arg(ma, name, tbuf, -1)); 2176 } 2177 2178 /* 2179 * Plain argument. 2180 * 2181 * If length is -1, treat value as a C string. 2182 */ 2183 struct mntarg * 2184 mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 2185 { 2186 2187 if (ma == NULL) { 2188 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2189 SLIST_INIT(&ma->list); 2190 } 2191 if (ma->error) 2192 return (ma); 2193 2194 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2195 M_MOUNT, M_WAITOK); 2196 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2197 ma->v[ma->len].iov_len = strlen(name) + 1; 2198 ma->len++; 2199 2200 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 2201 if (len < 0) 2202 ma->v[ma->len].iov_len = strlen(val) + 1; 2203 else 2204 ma->v[ma->len].iov_len = len; 2205 ma->len++; 2206 return (ma); 2207 } 2208 2209 /* 2210 * Free a mntarg structure 2211 */ 2212 static void 2213 free_mntarg(struct mntarg *ma) 2214 { 2215 struct mntaarg *maa; 2216 2217 while (!SLIST_EMPTY(&ma->list)) { 2218 maa = SLIST_FIRST(&ma->list); 2219 SLIST_REMOVE_HEAD(&ma->list, next); 2220 free(maa, M_MOUNT); 2221 } 2222 free(ma->v, M_MOUNT); 2223 free(ma, M_MOUNT); 2224 } 2225 2226 /* 2227 * Mount a filesystem 2228 */ 2229 int 2230 kernel_mount(struct mntarg *ma, uint64_t flags) 2231 { 2232 struct uio auio; 2233 int error; 2234 2235 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 2236 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 2237 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 2238 2239 auio.uio_iov = ma->v; 2240 auio.uio_iovcnt = ma->len; 2241 auio.uio_segflg = UIO_SYSSPACE; 2242 2243 error = ma->error; 2244 if (!error) 2245 error = vfs_donmount(curthread, flags, &auio); 2246 free_mntarg(ma); 2247 return (error); 2248 } 2249 2250 /* 2251 * A printflike function to mount a filesystem. 2252 */ 2253 int 2254 kernel_vmount(int flags, ...) 2255 { 2256 struct mntarg *ma = NULL; 2257 va_list ap; 2258 const char *cp; 2259 const void *vp; 2260 int error; 2261 2262 va_start(ap, flags); 2263 for (;;) { 2264 cp = va_arg(ap, const char *); 2265 if (cp == NULL) 2266 break; 2267 vp = va_arg(ap, const void *); 2268 ma = mount_arg(ma, cp, vp, (vp != NULL ? -1 : 0)); 2269 } 2270 va_end(ap); 2271 2272 error = kernel_mount(ma, flags); 2273 return (error); 2274 } 2275 2276 void 2277 vfs_oexport_conv(const struct oexport_args *oexp, struct export_args *exp) 2278 { 2279 2280 bcopy(oexp, exp, sizeof(*oexp)); 2281 exp->ex_numsecflavors = 0; 2282 } 2283