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