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