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