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