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