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