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