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