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