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