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