vfs_mount.c revision 171450
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 171450 2007-07-14 21:18:19Z rodrigc $"); 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 "suid", 141 "exec", 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_init_mount(mp); 492 mac_create_mount(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_destroy_mount(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, "noclusterw") == 0) 648 fsflags |= MNT_NOCLUSTERW; 649 else if (strcmp(opt->name, "noexec") == 0) 650 fsflags |= MNT_NOEXEC; 651 else if (strcmp(opt->name, "nosuid") == 0) 652 fsflags |= MNT_NOSUID; 653 else if (strcmp(opt->name, "nosymfollow") == 0) 654 fsflags |= MNT_NOSYMFOLLOW; 655 else if (strcmp(opt->name, "noro") == 0) { 656 fsflags &= ~MNT_RDONLY; 657 has_noro = 1; 658 } 659 else if (strcmp(opt->name, "rw") == 0) { 660 fsflags &= ~MNT_RDONLY; 661 has_rw = 1; 662 } 663 else if (strcmp(opt->name, "ro") == 0 || 664 strcmp(opt->name, "rdonly") == 0) 665 fsflags |= MNT_RDONLY; 666 else if (strcmp(opt->name, "snapshot") == 0) 667 fsflags |= MNT_SNAPSHOT; 668 else if (strcmp(opt->name, "suiddir") == 0) 669 fsflags |= MNT_SUIDDIR; 670 else if (strcmp(opt->name, "sync") == 0) 671 fsflags |= MNT_SYNCHRONOUS; 672 else if (strcmp(opt->name, "union") == 0) 673 fsflags |= MNT_UNION; 674 } 675 676 /* 677 * If "rw" was specified as a mount option, and we 678 * are trying to update a mount-point from "ro" to "rw", 679 * we need a mount option "noro", since in vfs_mergeopts(), 680 * "noro" will cancel "ro", but "rw" will not do anything. 681 */ 682 if (has_rw && !has_noro) { 683 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 684 noro_opt->name = strdup("noro", M_MOUNT); 685 noro_opt->value = NULL; 686 noro_opt->len = 0; 687 TAILQ_INSERT_TAIL(optlist, noro_opt, link); 688 } 689 690 /* 691 * Be ultra-paranoid about making sure the type and fspath 692 * variables will fit in our mp buffers, including the 693 * terminating NUL. 694 */ 695 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) { 696 error = ENAMETOOLONG; 697 goto bail; 698 } 699 700 mtx_lock(&Giant); 701 error = vfs_domount(td, fstype, fspath, fsflags, optlist); 702 mtx_unlock(&Giant); 703bail: 704 /* copyout the errmsg */ 705 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt) 706 && errmsg_len > 0 && errmsg != NULL) { 707 if (fsoptions->uio_segflg == UIO_SYSSPACE) { 708 bcopy(errmsg, 709 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 710 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 711 } else { 712 copyout(errmsg, 713 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 714 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 715 } 716 } 717 718 if (error != 0) 719 vfs_freeopts(optlist); 720 return (error); 721} 722 723/* 724 * Old mount API. 725 */ 726#ifndef _SYS_SYSPROTO_H_ 727struct mount_args { 728 char *type; 729 char *path; 730 int flags; 731 caddr_t data; 732}; 733#endif 734/* ARGSUSED */ 735int 736mount(td, uap) 737 struct thread *td; 738 struct mount_args /* { 739 char *type; 740 char *path; 741 int flags; 742 caddr_t data; 743 } */ *uap; 744{ 745 char *fstype; 746 struct vfsconf *vfsp = NULL; 747 struct mntarg *ma = NULL; 748 int error; 749 750 AUDIT_ARG(fflags, uap->flags); 751 752 /* Kick out MNT_ROOTFS early as it is legal internally */ 753 uap->flags &= ~MNT_ROOTFS; 754 755 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 756 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 757 if (error) { 758 free(fstype, M_TEMP); 759 return (error); 760 } 761 762 AUDIT_ARG(text, fstype); 763 mtx_lock(&Giant); 764 vfsp = vfs_byname_kld(fstype, td, &error); 765 free(fstype, M_TEMP); 766 if (vfsp == NULL) { 767 mtx_unlock(&Giant); 768 return (ENOENT); 769 } 770 if (vfsp->vfc_vfsops->vfs_cmount == NULL) { 771 mtx_unlock(&Giant); 772 return (EOPNOTSUPP); 773 } 774 775 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN); 776 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 777 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro"); 778 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid"); 779 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec"); 780 781 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td); 782 mtx_unlock(&Giant); 783 return (error); 784} 785 786 787/* 788 * vfs_domount(): actually attempt a filesystem mount. 789 */ 790static int 791vfs_domount( 792 struct thread *td, /* Calling thread. */ 793 const char *fstype, /* Filesystem type. */ 794 char *fspath, /* Mount path. */ 795 int fsflags, /* Flags common to all filesystems. */ 796 void *fsdata /* Options local to the filesystem. */ 797 ) 798{ 799 struct vnode *vp; 800 struct mount *mp; 801 struct vfsconf *vfsp; 802 struct export_args export; 803 int error, flag = 0; 804 struct vattr va; 805 struct nameidata nd; 806 807 mtx_assert(&Giant, MA_OWNED); 808 /* 809 * Be ultra-paranoid about making sure the type and fspath 810 * variables will fit in our mp buffers, including the 811 * terminating NUL. 812 */ 813 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 814 return (ENAMETOOLONG); 815 816 if (jailed(td->td_ucred) || usermount == 0) { 817 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0) 818 return (error); 819 } 820 821 /* 822 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 823 */ 824 if (fsflags & MNT_EXPORTED) { 825 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED); 826 if (error) 827 return (error); 828 } 829 if (fsflags & MNT_SUIDDIR) { 830 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR); 831 if (error) 832 return (error); 833 } 834 /* 835 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users. 836 */ 837 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) { 838 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0) 839 fsflags |= MNT_NOSUID | MNT_USER; 840 } 841 842 /* Load KLDs before we lock the covered vnode to avoid reversals. */ 843 vfsp = NULL; 844 if ((fsflags & MNT_UPDATE) == 0) { 845 /* Don't try to load KLDs if we're mounting the root. */ 846 if (fsflags & MNT_ROOTFS) 847 vfsp = vfs_byname(fstype); 848 else 849 vfsp = vfs_byname_kld(fstype, td, &error); 850 if (vfsp == NULL) 851 return (ENODEV); 852 if (jailed(td->td_ucred) && !(vfsp->vfc_flags & VFCF_JAIL)) 853 return (EPERM); 854 } 855 /* 856 * Get vnode to be covered 857 */ 858 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE, 859 fspath, td); 860 if ((error = namei(&nd)) != 0) 861 return (error); 862 NDFREE(&nd, NDF_ONLY_PNBUF); 863 vp = nd.ni_vp; 864 if (fsflags & MNT_UPDATE) { 865 if ((vp->v_vflag & VV_ROOT) == 0) { 866 vput(vp); 867 return (EINVAL); 868 } 869 mp = vp->v_mount; 870 MNT_ILOCK(mp); 871 flag = mp->mnt_flag; 872 /* 873 * We only allow the filesystem to be reloaded if it 874 * is currently mounted read-only. 875 */ 876 if ((fsflags & MNT_RELOAD) && 877 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 878 MNT_IUNLOCK(mp); 879 vput(vp); 880 return (EOPNOTSUPP); /* Needs translation */ 881 } 882 MNT_IUNLOCK(mp); 883 /* 884 * Only privileged root, or (if MNT_USER is set) the user that 885 * did the original mount is permitted to update it. 886 */ 887 error = vfs_suser(mp, td); 888 if (error) { 889 vput(vp); 890 return (error); 891 } 892 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 893 vput(vp); 894 return (EBUSY); 895 } 896 VI_LOCK(vp); 897 if ((vp->v_iflag & VI_MOUNT) != 0 || 898 vp->v_mountedhere != NULL) { 899 VI_UNLOCK(vp); 900 vfs_unbusy(mp, td); 901 vput(vp); 902 return (EBUSY); 903 } 904 vp->v_iflag |= VI_MOUNT; 905 VI_UNLOCK(vp); 906 MNT_ILOCK(mp); 907 mp->mnt_flag |= fsflags & 908 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS); 909 MNT_IUNLOCK(mp); 910 VOP_UNLOCK(vp, 0, td); 911 mp->mnt_optnew = fsdata; 912 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 913 } else { 914 /* 915 * If the user is not root, ensure that they own the directory 916 * onto which we are attempting to mount. 917 */ 918 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 919 if (error) { 920 vput(vp); 921 return (error); 922 } 923 if (va.va_uid != td->td_ucred->cr_uid) { 924 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 925 0); 926 if (error) { 927 vput(vp); 928 return (error); 929 } 930 } 931 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 932 if (error != 0) { 933 vput(vp); 934 return (error); 935 } 936 if (vp->v_type != VDIR) { 937 vput(vp); 938 return (ENOTDIR); 939 } 940 VI_LOCK(vp); 941 if ((vp->v_iflag & VI_MOUNT) != 0 || 942 vp->v_mountedhere != NULL) { 943 VI_UNLOCK(vp); 944 vput(vp); 945 return (EBUSY); 946 } 947 vp->v_iflag |= VI_MOUNT; 948 VI_UNLOCK(vp); 949 950 /* 951 * Allocate and initialize the filesystem. 952 */ 953 mp = vfs_mount_alloc(vp, vfsp, fspath, td); 954 VOP_UNLOCK(vp, 0, td); 955 956 /* XXXMAC: pass to vfs_mount_alloc? */ 957 mp->mnt_optnew = fsdata; 958 } 959 960 /* 961 * Set the mount level flags. 962 */ 963 MNT_ILOCK(mp); 964 mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) | 965 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS | 966 MNT_RDONLY)); 967 if ((mp->mnt_flag & MNT_ASYNC) == 0) 968 mp->mnt_kern_flag &= ~MNTK_ASYNC; 969 MNT_IUNLOCK(mp); 970 /* 971 * Mount the filesystem. 972 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 973 * get. No freeing of cn_pnbuf. 974 */ 975 error = VFS_MOUNT(mp, td); 976 977 /* 978 * Process the export option only if we are 979 * updating mount options. 980 */ 981 if (!error && (fsflags & MNT_UPDATE)) { 982 if (vfs_copyopt(mp->mnt_optnew, "export", &export, 983 sizeof(export)) == 0) 984 error = vfs_export(mp, &export); 985 } 986 987 if (!error) { 988 if (mp->mnt_opt != NULL) 989 vfs_freeopts(mp->mnt_opt); 990 mp->mnt_opt = mp->mnt_optnew; 991 (void)VFS_STATFS(mp, &mp->mnt_stat, td); 992 } 993 /* 994 * Prevent external consumers of mount options from reading 995 * mnt_optnew. 996 */ 997 mp->mnt_optnew = NULL; 998 if (mp->mnt_flag & MNT_UPDATE) { 999 MNT_ILOCK(mp); 1000 if (error) 1001 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | 1002 (flag & ~MNT_QUOTA); 1003 else 1004 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | 1005 MNT_FORCE | MNT_SNAPSHOT); 1006 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 1007 mp->mnt_kern_flag |= MNTK_ASYNC; 1008 else 1009 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1010 MNT_IUNLOCK(mp); 1011 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 1012 if (mp->mnt_syncer == NULL) 1013 error = vfs_allocate_syncvnode(mp); 1014 } else { 1015 if (mp->mnt_syncer != NULL) 1016 vrele(mp->mnt_syncer); 1017 mp->mnt_syncer = NULL; 1018 } 1019 vfs_unbusy(mp, td); 1020 VI_LOCK(vp); 1021 vp->v_iflag &= ~VI_MOUNT; 1022 VI_UNLOCK(vp); 1023 vrele(vp); 1024 return (error); 1025 } 1026 MNT_ILOCK(mp); 1027 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 1028 mp->mnt_kern_flag |= MNTK_ASYNC; 1029 else 1030 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1031 MNT_IUNLOCK(mp); 1032 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1033 /* 1034 * Put the new filesystem on the mount list after root. 1035 */ 1036 cache_purge(vp); 1037 if (!error) { 1038 struct vnode *newdp; 1039 1040 VI_LOCK(vp); 1041 vp->v_iflag &= ~VI_MOUNT; 1042 VI_UNLOCK(vp); 1043 vp->v_mountedhere = mp; 1044 mtx_lock(&mountlist_mtx); 1045 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1046 mtx_unlock(&mountlist_mtx); 1047 vfs_event_signal(NULL, VQ_MOUNT, 0); 1048 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td)) 1049 panic("mount: lost mount"); 1050 mountcheckdirs(vp, newdp); 1051 vput(newdp); 1052 VOP_UNLOCK(vp, 0, td); 1053 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1054 error = vfs_allocate_syncvnode(mp); 1055 vfs_unbusy(mp, td); 1056 if (error) 1057 vrele(vp); 1058 } else { 1059 VI_LOCK(vp); 1060 vp->v_iflag &= ~VI_MOUNT; 1061 VI_UNLOCK(vp); 1062 vfs_unbusy(mp, td); 1063 vfs_mount_destroy(mp); 1064 vput(vp); 1065 } 1066 return (error); 1067} 1068 1069/* 1070 * Unmount a filesystem. 1071 * 1072 * Note: unmount takes a path to the vnode mounted on as argument, not 1073 * special file (as before). 1074 */ 1075#ifndef _SYS_SYSPROTO_H_ 1076struct unmount_args { 1077 char *path; 1078 int flags; 1079}; 1080#endif 1081/* ARGSUSED */ 1082int 1083unmount(td, uap) 1084 struct thread *td; 1085 register struct unmount_args /* { 1086 char *path; 1087 int flags; 1088 } */ *uap; 1089{ 1090 struct mount *mp; 1091 char *pathbuf; 1092 int error, id0, id1; 1093 1094 if (jailed(td->td_ucred) || usermount == 0) { 1095 error = priv_check(td, PRIV_VFS_UNMOUNT); 1096 if (error) 1097 return (error); 1098 } 1099 1100 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1101 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 1102 if (error) { 1103 free(pathbuf, M_TEMP); 1104 return (error); 1105 } 1106 AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1); 1107 mtx_lock(&Giant); 1108 if (uap->flags & MNT_BYFSID) { 1109 /* Decode the filesystem ID. */ 1110 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 1111 mtx_unlock(&Giant); 1112 free(pathbuf, M_TEMP); 1113 return (EINVAL); 1114 } 1115 1116 mtx_lock(&mountlist_mtx); 1117 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1118 if (mp->mnt_stat.f_fsid.val[0] == id0 && 1119 mp->mnt_stat.f_fsid.val[1] == id1) 1120 break; 1121 } 1122 mtx_unlock(&mountlist_mtx); 1123 } else { 1124 mtx_lock(&mountlist_mtx); 1125 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1126 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) 1127 break; 1128 } 1129 mtx_unlock(&mountlist_mtx); 1130 } 1131 free(pathbuf, M_TEMP); 1132 if (mp == NULL) { 1133 /* 1134 * Previously we returned ENOENT for a nonexistent path and 1135 * EINVAL for a non-mountpoint. We cannot tell these apart 1136 * now, so in the !MNT_BYFSID case return the more likely 1137 * EINVAL for compatibility. 1138 */ 1139 mtx_unlock(&Giant); 1140 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 1141 } 1142 1143 /* 1144 * Don't allow unmounting the root filesystem. 1145 */ 1146 if (mp->mnt_flag & MNT_ROOTFS) { 1147 mtx_unlock(&Giant); 1148 return (EINVAL); 1149 } 1150 error = dounmount(mp, uap->flags, td); 1151 mtx_unlock(&Giant); 1152 return (error); 1153} 1154 1155/* 1156 * Do the actual filesystem unmount. 1157 */ 1158int 1159dounmount(mp, flags, td) 1160 struct mount *mp; 1161 int flags; 1162 struct thread *td; 1163{ 1164 struct vnode *coveredvp, *fsrootvp; 1165 int error; 1166 int async_flag; 1167 int mnt_gen_r; 1168 1169 mtx_assert(&Giant, MA_OWNED); 1170 1171 if ((coveredvp = mp->mnt_vnodecovered) != NULL) { 1172 mnt_gen_r = mp->mnt_gen; 1173 VI_LOCK(coveredvp); 1174 vholdl(coveredvp); 1175 vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, td); 1176 vdrop(coveredvp); 1177 /* 1178 * Check for mp being unmounted while waiting for the 1179 * covered vnode lock. 1180 */ 1181 if (coveredvp->v_mountedhere != mp || 1182 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) { 1183 VOP_UNLOCK(coveredvp, 0, td); 1184 return (EBUSY); 1185 } 1186 } 1187 /* 1188 * Only privileged root, or (if MNT_USER is set) the user that did the 1189 * original mount is permitted to unmount this filesystem. 1190 */ 1191 error = vfs_suser(mp, td); 1192 if (error) { 1193 if (coveredvp) 1194 VOP_UNLOCK(coveredvp, 0, td); 1195 return (error); 1196 } 1197 1198 MNT_ILOCK(mp); 1199 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 1200 MNT_IUNLOCK(mp); 1201 if (coveredvp) 1202 VOP_UNLOCK(coveredvp, 0, td); 1203 return (EBUSY); 1204 } 1205 mp->mnt_kern_flag |= MNTK_UNMOUNT; 1206 /* Allow filesystems to detect that a forced unmount is in progress. */ 1207 if (flags & MNT_FORCE) 1208 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 1209 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 1210 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td); 1211 if (error) { 1212 MNT_ILOCK(mp); 1213 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1214 if (mp->mnt_kern_flag & MNTK_MWAIT) 1215 wakeup(mp); 1216 MNT_IUNLOCK(mp); 1217 if (coveredvp) 1218 VOP_UNLOCK(coveredvp, 0, td); 1219 return (error); 1220 } 1221 vn_start_write(NULL, &mp, V_WAIT); 1222 1223 if (mp->mnt_flag & MNT_EXPUBLIC) 1224 vfs_setpublicfs(NULL, NULL, NULL); 1225 1226 vfs_msync(mp, MNT_WAIT); 1227 MNT_ILOCK(mp); 1228 async_flag = mp->mnt_flag & MNT_ASYNC; 1229 mp->mnt_flag &= ~MNT_ASYNC; 1230 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1231 MNT_IUNLOCK(mp); 1232 cache_purgevfs(mp); /* remove cache entries for this file sys */ 1233 if (mp->mnt_syncer != NULL) 1234 vrele(mp->mnt_syncer); 1235 /* 1236 * For forced unmounts, move process cdir/rdir refs on the fs root 1237 * vnode to the covered vnode. For non-forced unmounts we want 1238 * such references to cause an EBUSY error. 1239 */ 1240 if ((flags & MNT_FORCE) && 1241 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 1242 if (mp->mnt_vnodecovered != NULL) 1243 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered); 1244 if (fsrootvp == rootvnode) { 1245 vrele(rootvnode); 1246 rootvnode = NULL; 1247 } 1248 vput(fsrootvp); 1249 } 1250 if (((mp->mnt_flag & MNT_RDONLY) || 1251 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) || 1252 (flags & MNT_FORCE)) { 1253 error = VFS_UNMOUNT(mp, flags, td); 1254 } 1255 vn_finished_write(mp); 1256 if (error) { 1257 /* Undo cdir/rdir and rootvnode changes made above. */ 1258 if ((flags & MNT_FORCE) && 1259 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 1260 if (mp->mnt_vnodecovered != NULL) 1261 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp); 1262 if (rootvnode == NULL) { 1263 rootvnode = fsrootvp; 1264 vref(rootvnode); 1265 } 1266 vput(fsrootvp); 1267 } 1268 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 1269 (void) vfs_allocate_syncvnode(mp); 1270 MNT_ILOCK(mp); 1271 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1272 mp->mnt_flag |= async_flag; 1273 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 1274 mp->mnt_kern_flag |= MNTK_ASYNC; 1275 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 1276 if (mp->mnt_kern_flag & MNTK_MWAIT) 1277 wakeup(mp); 1278 MNT_IUNLOCK(mp); 1279 if (coveredvp) 1280 VOP_UNLOCK(coveredvp, 0, td); 1281 return (error); 1282 } 1283 mtx_lock(&mountlist_mtx); 1284 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1285 mtx_unlock(&mountlist_mtx); 1286 if (coveredvp != NULL) { 1287 coveredvp->v_mountedhere = NULL; 1288 vput(coveredvp); 1289 } 1290 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 1291 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 1292 vfs_mount_destroy(mp); 1293 return (0); 1294} 1295 1296/* 1297 * --------------------------------------------------------------------- 1298 * Mounting of root filesystem 1299 * 1300 */ 1301 1302struct root_hold_token { 1303 const char *who; 1304 LIST_ENTRY(root_hold_token) list; 1305}; 1306 1307static LIST_HEAD(, root_hold_token) root_holds = 1308 LIST_HEAD_INITIALIZER(&root_holds); 1309 1310static int root_mount_complete; 1311 1312/* 1313 * Hold root mount. 1314 */ 1315struct root_hold_token * 1316root_mount_hold(const char *identifier) 1317{ 1318 struct root_hold_token *h; 1319 1320 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK); 1321 h->who = identifier; 1322 mtx_lock(&mountlist_mtx); 1323 LIST_INSERT_HEAD(&root_holds, h, list); 1324 mtx_unlock(&mountlist_mtx); 1325 return (h); 1326} 1327 1328/* 1329 * Release root mount. 1330 */ 1331void 1332root_mount_rel(struct root_hold_token *h) 1333{ 1334 1335 mtx_lock(&mountlist_mtx); 1336 LIST_REMOVE(h, list); 1337 wakeup(&root_holds); 1338 mtx_unlock(&mountlist_mtx); 1339 free(h, M_DEVBUF); 1340} 1341 1342/* 1343 * Wait for all subsystems to release root mount. 1344 */ 1345static void 1346root_mount_prepare(void) 1347{ 1348 struct root_hold_token *h; 1349 1350 for (;;) { 1351 DROP_GIANT(); 1352 g_waitidle(); 1353 PICKUP_GIANT(); 1354 mtx_lock(&mountlist_mtx); 1355 if (LIST_EMPTY(&root_holds)) { 1356 mtx_unlock(&mountlist_mtx); 1357 break; 1358 } 1359 printf("Root mount waiting for:"); 1360 LIST_FOREACH(h, &root_holds, list) 1361 printf(" %s", h->who); 1362 printf("\n"); 1363 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold", 1364 hz); 1365 } 1366} 1367 1368/* 1369 * Root was mounted, share the good news. 1370 */ 1371static void 1372root_mount_done(void) 1373{ 1374 1375 /* 1376 * Use a mutex to prevent the wakeup being missed and waiting for 1377 * an extra 1 second sleep. 1378 */ 1379 mtx_lock(&mountlist_mtx); 1380 root_mount_complete = 1; 1381 wakeup(&root_mount_complete); 1382 mtx_unlock(&mountlist_mtx); 1383} 1384 1385/* 1386 * Return true if root is already mounted. 1387 */ 1388int 1389root_mounted(void) 1390{ 1391 1392 /* No mutex is acquired here because int stores are atomic. */ 1393 return (root_mount_complete); 1394} 1395 1396/* 1397 * Wait until root is mounted. 1398 */ 1399void 1400root_mount_wait(void) 1401{ 1402 1403 /* 1404 * Panic on an obvious deadlock - the function can't be called from 1405 * a thread which is doing the whole SYSINIT stuff. 1406 */ 1407 KASSERT(curthread->td_proc->p_pid != 0, 1408 ("root_mount_wait: cannot be called from the swapper thread")); 1409 mtx_lock(&mountlist_mtx); 1410 while (!root_mount_complete) { 1411 msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait", 1412 hz); 1413 } 1414 mtx_unlock(&mountlist_mtx); 1415} 1416 1417static void 1418set_rootvnode(struct thread *td) 1419{ 1420 struct proc *p; 1421 1422 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td)) 1423 panic("Cannot find root vnode"); 1424 1425 p = td->td_proc; 1426 FILEDESC_SLOCK(p->p_fd); 1427 1428 if (p->p_fd->fd_cdir != NULL) 1429 vrele(p->p_fd->fd_cdir); 1430 p->p_fd->fd_cdir = rootvnode; 1431 VREF(rootvnode); 1432 1433 if (p->p_fd->fd_rdir != NULL) 1434 vrele(p->p_fd->fd_rdir); 1435 p->p_fd->fd_rdir = rootvnode; 1436 VREF(rootvnode); 1437 1438 FILEDESC_SUNLOCK(p->p_fd); 1439 1440 VOP_UNLOCK(rootvnode, 0, td); 1441} 1442 1443/* 1444 * Mount /devfs as our root filesystem, but do not put it on the mountlist 1445 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup. 1446 */ 1447 1448static void 1449devfs_first(void) 1450{ 1451 struct thread *td = curthread; 1452 struct vfsoptlist *opts; 1453 struct vfsconf *vfsp; 1454 struct mount *mp = NULL; 1455 int error; 1456 1457 vfsp = vfs_byname("devfs"); 1458 KASSERT(vfsp != NULL, ("Could not find devfs by name")); 1459 if (vfsp == NULL) 1460 return; 1461 1462 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td); 1463 1464 error = VFS_MOUNT(mp, td); 1465 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 1466 if (error) 1467 return; 1468 1469 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 1470 TAILQ_INIT(opts); 1471 mp->mnt_opt = opts; 1472 1473 mtx_lock(&mountlist_mtx); 1474 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1475 mtx_unlock(&mountlist_mtx); 1476 1477 set_rootvnode(td); 1478 1479 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE); 1480 if (error) 1481 printf("kern_symlink /dev -> / returns %d\n", error); 1482} 1483 1484/* 1485 * Surgically move our devfs to be mounted on /dev. 1486 */ 1487 1488static void 1489devfs_fixup(struct thread *td) 1490{ 1491 struct nameidata nd; 1492 int error; 1493 struct vnode *vp, *dvp; 1494 struct mount *mp; 1495 1496 /* Remove our devfs mount from the mountlist and purge the cache */ 1497 mtx_lock(&mountlist_mtx); 1498 mp = TAILQ_FIRST(&mountlist); 1499 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1500 mtx_unlock(&mountlist_mtx); 1501 cache_purgevfs(mp); 1502 1503 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td); 1504 VI_LOCK(dvp); 1505 dvp->v_iflag &= ~VI_MOUNT; 1506 dvp->v_mountedhere = NULL; 1507 VI_UNLOCK(dvp); 1508 1509 /* Set up the real rootvnode, and purge the cache */ 1510 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL; 1511 set_rootvnode(td); 1512 cache_purgevfs(rootvnode->v_mount); 1513 1514 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 1515 error = namei(&nd); 1516 if (error) { 1517 printf("Lookup of /dev for devfs, error: %d\n", error); 1518 return; 1519 } 1520 NDFREE(&nd, NDF_ONLY_PNBUF); 1521 vp = nd.ni_vp; 1522 if (vp->v_type != VDIR) { 1523 vput(vp); 1524 } 1525 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 1526 if (error) { 1527 vput(vp); 1528 } 1529 cache_purge(vp); 1530 mp->mnt_vnodecovered = vp; 1531 vp->v_mountedhere = mp; 1532 mtx_lock(&mountlist_mtx); 1533 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1534 mtx_unlock(&mountlist_mtx); 1535 VOP_UNLOCK(vp, 0, td); 1536 vput(dvp); 1537 vfs_unbusy(mp, td); 1538 1539 /* Unlink the no longer needed /dev/dev -> / symlink */ 1540 kern_unlink(td, "/dev/dev", UIO_SYSSPACE); 1541} 1542 1543/* 1544 * Report errors during filesystem mounting. 1545 */ 1546void 1547vfs_mount_error(struct mount *mp, const char *fmt, ...) 1548{ 1549 struct vfsoptlist *moptlist = mp->mnt_optnew; 1550 va_list ap; 1551 int error, len; 1552 char *errmsg; 1553 1554 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len); 1555 if (error || errmsg == NULL || len <= 0) 1556 return; 1557 1558 va_start(ap, fmt); 1559 vsnprintf(errmsg, (size_t)len, fmt, ap); 1560 va_end(ap); 1561} 1562 1563/* 1564 * Find and mount the root filesystem 1565 */ 1566void 1567vfs_mountroot(void) 1568{ 1569 char *cp; 1570 int error, i, asked = 0; 1571 1572 root_mount_prepare(); 1573 1574 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), 1575 NULL, NULL, mount_init, mount_fini, 1576 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1577 devfs_first(); 1578 1579 /* 1580 * We are booted with instructions to prompt for the root filesystem. 1581 */ 1582 if (boothowto & RB_ASKNAME) { 1583 if (!vfs_mountroot_ask()) 1584 goto mounted; 1585 asked = 1; 1586 } 1587 1588 /* 1589 * The root filesystem information is compiled in, and we are 1590 * booted with instructions to use it. 1591 */ 1592 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) { 1593 if (!vfs_mountroot_try(ctrootdevname)) 1594 goto mounted; 1595 ctrootdevname = NULL; 1596 } 1597 1598 /* 1599 * We've been given the generic "use CDROM as root" flag. This is 1600 * necessary because one media may be used in many different 1601 * devices, so we need to search for them. 1602 */ 1603 if (boothowto & RB_CDROM) { 1604 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1605 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1606 goto mounted; 1607 } 1608 } 1609 1610 /* 1611 * Try to use the value read by the loader from /etc/fstab, or 1612 * supplied via some other means. This is the preferred 1613 * mechanism. 1614 */ 1615 cp = getenv("vfs.root.mountfrom"); 1616 if (cp != NULL) { 1617 error = vfs_mountroot_try(cp); 1618 freeenv(cp); 1619 if (!error) 1620 goto mounted; 1621 } 1622 1623 /* 1624 * Try values that may have been computed by code during boot 1625 */ 1626 if (!vfs_mountroot_try(rootdevnames[0])) 1627 goto mounted; 1628 if (!vfs_mountroot_try(rootdevnames[1])) 1629 goto mounted; 1630 1631 /* 1632 * If we (still) have a compiled-in default, try it. 1633 */ 1634 if (ctrootdevname != NULL) 1635 if (!vfs_mountroot_try(ctrootdevname)) 1636 goto mounted; 1637 /* 1638 * Everything so far has failed, prompt on the console if we haven't 1639 * already tried that. 1640 */ 1641 if (!asked) 1642 if (!vfs_mountroot_ask()) 1643 goto mounted; 1644 1645 panic("Root mount failed, startup aborted."); 1646 1647mounted: 1648 root_mount_done(); 1649} 1650 1651/* 1652 * Mount (mountfrom) as the root filesystem. 1653 */ 1654static int 1655vfs_mountroot_try(const char *mountfrom) 1656{ 1657 struct mount *mp; 1658 char *vfsname, *path; 1659 time_t timebase; 1660 int error; 1661 char patt[32]; 1662 1663 vfsname = NULL; 1664 path = NULL; 1665 mp = NULL; 1666 error = EINVAL; 1667 1668 if (mountfrom == NULL) 1669 return (error); /* don't complain */ 1670 printf("Trying to mount root from %s\n", mountfrom); 1671 1672 /* parse vfs name and path */ 1673 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1674 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1675 vfsname[0] = path[0] = 0; 1676 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1677 if (sscanf(mountfrom, patt, vfsname, path) < 1) 1678 goto out; 1679 1680 if (path[0] == '\0') 1681 strcpy(path, ROOTNAME); 1682 1683 error = kernel_vmount( 1684 MNT_RDONLY | MNT_ROOTFS, 1685 "fstype", vfsname, 1686 "fspath", "/", 1687 "from", path, 1688 NULL); 1689 if (error == 0) { 1690 /* 1691 * We mount devfs prior to mounting the / FS, so the first 1692 * entry will typically be devfs. 1693 */ 1694 mp = TAILQ_FIRST(&mountlist); 1695 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__)); 1696 1697 /* 1698 * Iterate over all currently mounted file systems and use 1699 * the time stamp found to check and/or initialize the RTC. 1700 * Typically devfs has no time stamp and the only other FS 1701 * is the actual / FS. 1702 * Call inittodr() only once and pass it the largest of the 1703 * timestamps we encounter. 1704 */ 1705 timebase = 0; 1706 do { 1707 if (mp->mnt_time > timebase) 1708 timebase = mp->mnt_time; 1709 mp = TAILQ_NEXT(mp, mnt_list); 1710 } while (mp != NULL); 1711 inittodr(timebase); 1712 1713 devfs_fixup(curthread); 1714 } 1715out: 1716 free(path, M_MOUNT); 1717 free(vfsname, M_MOUNT); 1718 return (error); 1719} 1720 1721/* 1722 * --------------------------------------------------------------------- 1723 * Interactive root filesystem selection code. 1724 */ 1725 1726static int 1727vfs_mountroot_ask(void) 1728{ 1729 char name[128]; 1730 1731 for(;;) { 1732 printf("\nManual root filesystem specification:\n"); 1733 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1734#if defined(__amd64__) || defined(__i386__) || defined(__ia64__) 1735 printf(" eg. ufs:da0s1a\n"); 1736#else 1737 printf(" eg. ufs:/dev/da0a\n"); 1738#endif 1739 printf(" ? List valid disk boot devices\n"); 1740 printf(" <empty line> Abort manual input\n"); 1741 printf("\nmountroot> "); 1742 gets(name, sizeof(name), 1); 1743 if (name[0] == '\0') 1744 return (1); 1745 if (name[0] == '?') { 1746 printf("\nList of GEOM managed disk devices:\n "); 1747 g_dev_print(); 1748 continue; 1749 } 1750 if (!vfs_mountroot_try(name)) 1751 return (0); 1752 } 1753} 1754 1755/* 1756 * --------------------------------------------------------------------- 1757 * Functions for querying mount options/arguments from filesystems. 1758 */ 1759 1760/* 1761 * Check that no unknown options are given 1762 */ 1763int 1764vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1765{ 1766 struct vfsopt *opt; 1767 const char **t, *p; 1768 1769 TAILQ_FOREACH(opt, opts, link) { 1770 p = opt->name; 1771 if (p[0] == 'n' && p[1] == 'o') 1772 p += 2; 1773 for(t = global_opts; *t != NULL; t++) 1774 if (!strcmp(*t, p)) 1775 break; 1776 if (*t != NULL) 1777 continue; 1778 for(t = legal; *t != NULL; t++) 1779 if (!strcmp(*t, p)) 1780 break; 1781 if (*t != NULL) 1782 continue; 1783 printf("mount option <%s> is unknown\n", p); 1784 return (EINVAL); 1785 } 1786 return (0); 1787} 1788 1789/* 1790 * Get a mount option by its name. 1791 * 1792 * Return 0 if the option was found, ENOENT otherwise. 1793 * If len is non-NULL it will be filled with the length 1794 * of the option. If buf is non-NULL, it will be filled 1795 * with the address of the option. 1796 */ 1797int 1798vfs_getopt(opts, name, buf, len) 1799 struct vfsoptlist *opts; 1800 const char *name; 1801 void **buf; 1802 int *len; 1803{ 1804 struct vfsopt *opt; 1805 1806 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1807 1808 TAILQ_FOREACH(opt, opts, link) { 1809 if (strcmp(name, opt->name) == 0) { 1810 if (len != NULL) 1811 *len = opt->len; 1812 if (buf != NULL) 1813 *buf = opt->value; 1814 return (0); 1815 } 1816 } 1817 return (ENOENT); 1818} 1819 1820static int 1821vfs_getopt_pos(struct vfsoptlist *opts, const char *name) 1822{ 1823 struct vfsopt *opt; 1824 int i; 1825 1826 if (opts == NULL) 1827 return (-1); 1828 1829 i = 0; 1830 TAILQ_FOREACH(opt, opts, link) { 1831 if (strcmp(name, opt->name) == 0) 1832 return (i); 1833 ++i; 1834 } 1835 return (-1); 1836} 1837 1838char * 1839vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1840{ 1841 struct vfsopt *opt; 1842 1843 *error = 0; 1844 TAILQ_FOREACH(opt, opts, link) { 1845 if (strcmp(name, opt->name) != 0) 1846 continue; 1847 if (((char *)opt->value)[opt->len - 1] != '\0') { 1848 *error = EINVAL; 1849 return (NULL); 1850 } 1851 return (opt->value); 1852 } 1853 *error = ENOENT; 1854 return (NULL); 1855} 1856 1857int 1858vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val) 1859{ 1860 struct vfsopt *opt; 1861 1862 TAILQ_FOREACH(opt, opts, link) { 1863 if (strcmp(name, opt->name) == 0) { 1864 if (w != NULL) 1865 *w |= val; 1866 return (1); 1867 } 1868 } 1869 if (w != NULL) 1870 *w &= ~val; 1871 return (0); 1872} 1873 1874int 1875vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1876{ 1877 va_list ap; 1878 struct vfsopt *opt; 1879 int ret; 1880 1881 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1882 1883 TAILQ_FOREACH(opt, opts, link) { 1884 if (strcmp(name, opt->name) != 0) 1885 continue; 1886 if (((char *)opt->value)[opt->len - 1] != '\0') 1887 return (0); 1888 va_start(ap, fmt); 1889 ret = vsscanf(opt->value, fmt, ap); 1890 va_end(ap); 1891 return (ret); 1892 } 1893 return (0); 1894} 1895 1896/* 1897 * Find and copy a mount option. 1898 * 1899 * The size of the buffer has to be specified 1900 * in len, if it is not the same length as the 1901 * mount option, EINVAL is returned. 1902 * Returns ENOENT if the option is not found. 1903 */ 1904int 1905vfs_copyopt(opts, name, dest, len) 1906 struct vfsoptlist *opts; 1907 const char *name; 1908 void *dest; 1909 int len; 1910{ 1911 struct vfsopt *opt; 1912 1913 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 1914 1915 TAILQ_FOREACH(opt, opts, link) { 1916 if (strcmp(name, opt->name) == 0) { 1917 if (len != opt->len) 1918 return (EINVAL); 1919 bcopy(opt->value, dest, opt->len); 1920 return (0); 1921 } 1922 } 1923 return (ENOENT); 1924} 1925 1926/* 1927 * This is a helper function for filesystems to traverse their 1928 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h 1929 */ 1930 1931struct vnode * 1932__mnt_vnode_next(struct vnode **mvp, struct mount *mp) 1933{ 1934 struct vnode *vp; 1935 1936 mtx_assert(MNT_MTX(mp), MA_OWNED); 1937 1938 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 1939 vp = TAILQ_NEXT(*mvp, v_nmntvnodes); 1940 while (vp != NULL && vp->v_type == VMARKER) 1941 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1942 1943 /* Check if we are done */ 1944 if (vp == NULL) { 1945 __mnt_vnode_markerfree(mvp, mp); 1946 return (NULL); 1947 } 1948 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 1949 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 1950 return (vp); 1951} 1952 1953struct vnode * 1954__mnt_vnode_first(struct vnode **mvp, struct mount *mp) 1955{ 1956 struct vnode *vp; 1957 1958 mtx_assert(MNT_MTX(mp), MA_OWNED); 1959 1960 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1961 while (vp != NULL && vp->v_type == VMARKER) 1962 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1963 1964 /* Check if we are done */ 1965 if (vp == NULL) { 1966 *mvp = NULL; 1967 return (NULL); 1968 } 1969 mp->mnt_holdcnt++; 1970 MNT_IUNLOCK(mp); 1971 *mvp = (struct vnode *) malloc(sizeof(struct vnode), 1972 M_VNODE_MARKER, 1973 M_WAITOK | M_ZERO); 1974 MNT_ILOCK(mp); 1975 (*mvp)->v_type = VMARKER; 1976 1977 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1978 while (vp != NULL && vp->v_type == VMARKER) 1979 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1980 1981 /* Check if we are done */ 1982 if (vp == NULL) { 1983 MNT_IUNLOCK(mp); 1984 free(*mvp, M_VNODE_MARKER); 1985 MNT_ILOCK(mp); 1986 *mvp = NULL; 1987 mp->mnt_holdcnt--; 1988 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0) 1989 wakeup(&mp->mnt_holdcnt); 1990 return (NULL); 1991 } 1992 mp->mnt_markercnt++; 1993 (*mvp)->v_mount = mp; 1994 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 1995 return (vp); 1996} 1997 1998 1999void 2000__mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp) 2001{ 2002 2003 if (*mvp == NULL) 2004 return; 2005 2006 mtx_assert(MNT_MTX(mp), MA_OWNED); 2007 2008 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 2009 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 2010 MNT_IUNLOCK(mp); 2011 free(*mvp, M_VNODE_MARKER); 2012 MNT_ILOCK(mp); 2013 *mvp = NULL; 2014 2015 mp->mnt_markercnt--; 2016 mp->mnt_holdcnt--; 2017 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0) 2018 wakeup(&mp->mnt_holdcnt); 2019} 2020 2021 2022int 2023__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 2024{ 2025 int error; 2026 2027 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td); 2028 if (sbp != &mp->mnt_stat) 2029 *sbp = mp->mnt_stat; 2030 return (error); 2031} 2032 2033void 2034vfs_mountedfrom(struct mount *mp, const char *from) 2035{ 2036 2037 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 2038 strlcpy(mp->mnt_stat.f_mntfromname, from, 2039 sizeof mp->mnt_stat.f_mntfromname); 2040} 2041 2042/* 2043 * --------------------------------------------------------------------- 2044 * This is the api for building mount args and mounting filesystems from 2045 * inside the kernel. 2046 * 2047 * The API works by accumulation of individual args. First error is 2048 * latched. 2049 * 2050 * XXX: should be documented in new manpage kernel_mount(9) 2051 */ 2052 2053/* A memory allocation which must be freed when we are done */ 2054struct mntaarg { 2055 SLIST_ENTRY(mntaarg) next; 2056}; 2057 2058/* The header for the mount arguments */ 2059struct mntarg { 2060 struct iovec *v; 2061 int len; 2062 int error; 2063 SLIST_HEAD(, mntaarg) list; 2064}; 2065 2066/* 2067 * Add a boolean argument. 2068 * 2069 * flag is the boolean value. 2070 * name must start with "no". 2071 */ 2072struct mntarg * 2073mount_argb(struct mntarg *ma, int flag, const char *name) 2074{ 2075 2076 KASSERT(name[0] == 'n' && name[1] == 'o', 2077 ("mount_argb(...,%s): name must start with 'no'", name)); 2078 2079 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 2080} 2081 2082/* 2083 * Add an argument printf style 2084 */ 2085struct mntarg * 2086mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 2087{ 2088 va_list ap; 2089 struct mntaarg *maa; 2090 struct sbuf *sb; 2091 int len; 2092 2093 if (ma == NULL) { 2094 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2095 SLIST_INIT(&ma->list); 2096 } 2097 if (ma->error) 2098 return (ma); 2099 2100 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2101 M_MOUNT, M_WAITOK); 2102 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2103 ma->v[ma->len].iov_len = strlen(name) + 1; 2104 ma->len++; 2105 2106 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 2107 va_start(ap, fmt); 2108 sbuf_vprintf(sb, fmt, ap); 2109 va_end(ap); 2110 sbuf_finish(sb); 2111 len = sbuf_len(sb) + 1; 2112 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2113 SLIST_INSERT_HEAD(&ma->list, maa, next); 2114 bcopy(sbuf_data(sb), maa + 1, len); 2115 sbuf_delete(sb); 2116 2117 ma->v[ma->len].iov_base = maa + 1; 2118 ma->v[ma->len].iov_len = len; 2119 ma->len++; 2120 2121 return (ma); 2122} 2123 2124/* 2125 * Add an argument which is a userland string. 2126 */ 2127struct mntarg * 2128mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 2129{ 2130 struct mntaarg *maa; 2131 char *tbuf; 2132 2133 if (val == NULL) 2134 return (ma); 2135 if (ma == NULL) { 2136 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2137 SLIST_INIT(&ma->list); 2138 } 2139 if (ma->error) 2140 return (ma); 2141 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2142 SLIST_INSERT_HEAD(&ma->list, maa, next); 2143 tbuf = (void *)(maa + 1); 2144 ma->error = copyinstr(val, tbuf, len, NULL); 2145 return (mount_arg(ma, name, tbuf, -1)); 2146} 2147 2148/* 2149 * Plain argument. 2150 * 2151 * If length is -1, use printf. 2152 */ 2153struct mntarg * 2154mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 2155{ 2156 2157 if (ma == NULL) { 2158 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2159 SLIST_INIT(&ma->list); 2160 } 2161 if (ma->error) 2162 return (ma); 2163 2164 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2165 M_MOUNT, M_WAITOK); 2166 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2167 ma->v[ma->len].iov_len = strlen(name) + 1; 2168 ma->len++; 2169 2170 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 2171 if (len < 0) 2172 ma->v[ma->len].iov_len = strlen(val) + 1; 2173 else 2174 ma->v[ma->len].iov_len = len; 2175 ma->len++; 2176 return (ma); 2177} 2178 2179/* 2180 * Free a mntarg structure 2181 */ 2182static void 2183free_mntarg(struct mntarg *ma) 2184{ 2185 struct mntaarg *maa; 2186 2187 while (!SLIST_EMPTY(&ma->list)) { 2188 maa = SLIST_FIRST(&ma->list); 2189 SLIST_REMOVE_HEAD(&ma->list, next); 2190 free(maa, M_MOUNT); 2191 } 2192 free(ma->v, M_MOUNT); 2193 free(ma, M_MOUNT); 2194} 2195 2196/* 2197 * Mount a filesystem 2198 */ 2199int 2200kernel_mount(struct mntarg *ma, int flags) 2201{ 2202 struct uio auio; 2203 int error; 2204 2205 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 2206 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 2207 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 2208 2209 auio.uio_iov = ma->v; 2210 auio.uio_iovcnt = ma->len; 2211 auio.uio_segflg = UIO_SYSSPACE; 2212 2213 error = ma->error; 2214 if (!error) 2215 error = vfs_donmount(curthread, flags, &auio); 2216 free_mntarg(ma); 2217 return (error); 2218} 2219 2220/* 2221 * A printflike function to mount a filesystem. 2222 */ 2223int 2224kernel_vmount(int flags, ...) 2225{ 2226 struct mntarg *ma = NULL; 2227 va_list ap; 2228 const char *cp; 2229 const void *vp; 2230 int error; 2231 2232 va_start(ap, flags); 2233 for (;;) { 2234 cp = va_arg(ap, const char *); 2235 if (cp == NULL) 2236 break; 2237 vp = va_arg(ap, const void *); 2238 ma = mount_arg(ma, cp, vp, -1); 2239 } 2240 va_end(ap); 2241 2242 error = kernel_mount(ma, flags); 2243 return (error); 2244} 2245