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