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