1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2010 Marcel Moolenaar 5 * Copyright (c) 1999-2004 Poul-Henning Kamp 6 * Copyright (c) 1999 Michael Smith 7 * Copyright (c) 1989, 1993 8 * The Regents of the University of California. All rights reserved. 9 * (c) UNIX System Laboratories, Inc. 10 * All or some portions of this file are derived from material licensed 11 * to the University of California by American Telephone and Telegraph 12 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 13 * the permission of UNIX System Laboratories, Inc. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #include "opt_rootdevname.h" 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/conf.h> 47 #include <sys/cons.h> 48 #include <sys/eventhandler.h> 49 #include <sys/fcntl.h> 50 #include <sys/jail.h> 51 #include <sys/kernel.h> 52 #include <sys/malloc.h> 53 #include <sys/mdioctl.h> 54 #include <sys/mount.h> 55 #include <sys/mutex.h> 56 #include <sys/namei.h> 57 #include <sys/priv.h> 58 #include <sys/proc.h> 59 #include <sys/filedesc.h> 60 #include <sys/reboot.h> 61 #include <sys/sbuf.h> 62 #include <sys/stat.h> 63 #include <sys/syscallsubr.h> 64 #include <sys/sysproto.h> 65 #include <sys/sx.h> 66 #include <sys/sysctl.h> 67 #include <sys/sysent.h> 68 #include <sys/systm.h> 69 #include <sys/vnode.h> 70 71 #include <geom/geom.h> 72 73 /* 74 * The root filesystem is detailed in the kernel environment variable 75 * vfs.root.mountfrom, which is expected to be in the general format 76 * 77 * <vfsname>:[<path>][ <vfsname>:[<path>] ...] 78 * vfsname := the name of a VFS known to the kernel and capable 79 * of being mounted as root 80 * path := disk device name or other data used by the filesystem 81 * to locate its physical store 82 * 83 * If the environment variable vfs.root.mountfrom is a space separated list, 84 * each list element is tried in turn and the root filesystem will be mounted 85 * from the first one that succeeds. 86 * 87 * The environment variable vfs.root.mountfrom.options is a comma delimited 88 * set of string mount options. These mount options must be parseable 89 * by nmount() in the kernel. 90 */ 91 92 static int parse_mount(char **); 93 static struct mntarg *parse_mountroot_options(struct mntarg *, const char *); 94 static int sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS); 95 static void vfs_mountroot_wait(void); 96 static int vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev); 97 98 /* 99 * The vnode of the system's root (/ in the filesystem, without chroot 100 * active.) 101 */ 102 struct vnode *rootvnode; 103 104 /* 105 * Mount of the system's /dev. 106 */ 107 struct mount *rootdevmp; 108 109 char *rootdevnames[2] = {NULL, NULL}; 110 111 struct mtx root_holds_mtx; 112 MTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF); 113 114 struct root_hold_token { 115 const char *who; 116 LIST_ENTRY(root_hold_token) list; 117 }; 118 119 static LIST_HEAD(, root_hold_token) root_holds = 120 LIST_HEAD_INITIALIZER(root_holds); 121 122 enum action { 123 A_CONTINUE, 124 A_PANIC, 125 A_REBOOT, 126 A_RETRY 127 }; 128 129 static enum action root_mount_onfail = A_CONTINUE; 130 131 static int root_mount_mddev; 132 static int root_mount_complete; 133 134 /* By default wait up to 3 seconds for devices to appear. */ 135 static int root_mount_timeout = 3; 136 TUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout); 137 138 static int root_mount_always_wait = 0; 139 SYSCTL_INT(_vfs, OID_AUTO, root_mount_always_wait, CTLFLAG_RDTUN, 140 &root_mount_always_wait, 0, 141 "Wait for root mount holds even if the root device already exists"); 142 143 SYSCTL_PROC(_vfs, OID_AUTO, root_mount_hold, 144 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 145 NULL, 0, sysctl_vfs_root_mount_hold, "A", 146 "List of root mount hold tokens"); 147 148 static int 149 sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS) 150 { 151 struct sbuf sb; 152 struct root_hold_token *h; 153 int error; 154 155 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL); 156 157 mtx_lock(&root_holds_mtx); 158 LIST_FOREACH(h, &root_holds, list) { 159 if (h != LIST_FIRST(&root_holds)) 160 sbuf_putc(&sb, ' '); 161 sbuf_printf(&sb, "%s", h->who); 162 } 163 mtx_unlock(&root_holds_mtx); 164 165 error = sbuf_finish(&sb); 166 if (error == 0) 167 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 168 sbuf_delete(&sb); 169 return (error); 170 } 171 172 struct root_hold_token * 173 root_mount_hold(const char *identifier) 174 { 175 struct root_hold_token *h; 176 177 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK); 178 h->who = identifier; 179 mtx_lock(&root_holds_mtx); 180 TSHOLD("root mount"); 181 LIST_INSERT_HEAD(&root_holds, h, list); 182 mtx_unlock(&root_holds_mtx); 183 return (h); 184 } 185 186 void 187 root_mount_rel(struct root_hold_token *h) 188 { 189 190 if (h == NULL) 191 return; 192 193 mtx_lock(&root_holds_mtx); 194 LIST_REMOVE(h, list); 195 TSRELEASE("root mount"); 196 wakeup(&root_holds); 197 mtx_unlock(&root_holds_mtx); 198 free(h, M_DEVBUF); 199 } 200 201 int 202 root_mounted(void) 203 { 204 205 /* No mutex is acquired here because int stores are atomic. */ 206 return (root_mount_complete); 207 } 208 209 static void 210 set_rootvnode(void) 211 { 212 struct proc *p; 213 214 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode)) 215 panic("set_rootvnode: Cannot find root vnode"); 216 217 VOP_UNLOCK(rootvnode, 0); 218 219 p = curthread->td_proc; 220 FILEDESC_XLOCK(p->p_fd); 221 222 if (p->p_fd->fd_cdir != NULL) 223 vrele(p->p_fd->fd_cdir); 224 p->p_fd->fd_cdir = rootvnode; 225 VREF(rootvnode); 226 227 if (p->p_fd->fd_rdir != NULL) 228 vrele(p->p_fd->fd_rdir); 229 p->p_fd->fd_rdir = rootvnode; 230 VREF(rootvnode); 231 232 FILEDESC_XUNLOCK(p->p_fd); 233 } 234 235 static int 236 vfs_mountroot_devfs(struct thread *td, struct mount **mpp) 237 { 238 struct vfsoptlist *opts; 239 struct vfsconf *vfsp; 240 struct mount *mp; 241 int error; 242 243 *mpp = NULL; 244 245 if (rootdevmp != NULL) { 246 /* 247 * Already have /dev; this happens during rerooting. 248 */ 249 error = vfs_busy(rootdevmp, 0); 250 if (error != 0) 251 return (error); 252 *mpp = rootdevmp; 253 } else { 254 vfsp = vfs_byname("devfs"); 255 KASSERT(vfsp != NULL, ("Could not find devfs by name")); 256 if (vfsp == NULL) 257 return (ENOENT); 258 259 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred); 260 261 error = VFS_MOUNT(mp); 262 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 263 if (error) 264 return (error); 265 266 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 267 TAILQ_INIT(opts); 268 mp->mnt_opt = opts; 269 270 mtx_lock(&mountlist_mtx); 271 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 272 mtx_unlock(&mountlist_mtx); 273 274 *mpp = mp; 275 rootdevmp = mp; 276 vfs_op_exit(mp); 277 } 278 279 set_rootvnode(); 280 281 error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE); 282 if (error) 283 printf("kern_symlink /dev -> / returns %d\n", error); 284 285 return (error); 286 } 287 288 static void 289 vfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs) 290 { 291 struct nameidata nd; 292 struct mount *mporoot, *mpnroot; 293 struct vnode *vp, *vporoot, *vpdevfs; 294 char *fspath; 295 int error; 296 297 mpnroot = TAILQ_NEXT(mpdevfs, mnt_list); 298 299 /* Shuffle the mountlist. */ 300 mtx_lock(&mountlist_mtx); 301 mporoot = TAILQ_FIRST(&mountlist); 302 TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list); 303 if (mporoot != mpdevfs) { 304 TAILQ_REMOVE(&mountlist, mpnroot, mnt_list); 305 TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list); 306 } 307 TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list); 308 mtx_unlock(&mountlist_mtx); 309 310 cache_purgevfs(mporoot, true); 311 if (mporoot != mpdevfs) 312 cache_purgevfs(mpdevfs, true); 313 314 if (VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot)) 315 panic("vfs_mountroot_shuffle: Cannot find root vnode"); 316 317 VI_LOCK(vporoot); 318 vporoot->v_iflag &= ~VI_MOUNT; 319 VI_UNLOCK(vporoot); 320 vporoot->v_mountedhere = NULL; 321 mporoot->mnt_flag &= ~MNT_ROOTFS; 322 mporoot->mnt_vnodecovered = NULL; 323 vput(vporoot); 324 325 /* Set up the new rootvnode, and purge the cache */ 326 mpnroot->mnt_vnodecovered = NULL; 327 set_rootvnode(); 328 cache_purgevfs(rootvnode->v_mount, true); 329 330 if (mporoot != mpdevfs) { 331 /* Remount old root under /.mount or /mnt */ 332 fspath = "/.mount"; 333 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 334 fspath, td); 335 error = namei(&nd); 336 if (error) { 337 NDFREE(&nd, NDF_ONLY_PNBUF); 338 fspath = "/mnt"; 339 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 340 fspath, td); 341 error = namei(&nd); 342 } 343 if (!error) { 344 vp = nd.ni_vp; 345 error = (vp->v_type == VDIR) ? 0 : ENOTDIR; 346 if (!error) 347 error = vinvalbuf(vp, V_SAVE, 0, 0); 348 if (!error) { 349 cache_purge(vp); 350 mporoot->mnt_vnodecovered = vp; 351 vp->v_mountedhere = mporoot; 352 strlcpy(mporoot->mnt_stat.f_mntonname, 353 fspath, MNAMELEN); 354 VOP_UNLOCK(vp, 0); 355 } else 356 vput(vp); 357 } 358 NDFREE(&nd, NDF_ONLY_PNBUF); 359 360 if (error) 361 printf("mountroot: unable to remount previous root " 362 "under /.mount or /mnt (error %d)\n", error); 363 } 364 365 /* Remount devfs under /dev */ 366 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 367 error = namei(&nd); 368 if (!error) { 369 vp = nd.ni_vp; 370 error = (vp->v_type == VDIR) ? 0 : ENOTDIR; 371 if (!error) 372 error = vinvalbuf(vp, V_SAVE, 0, 0); 373 if (!error) { 374 vpdevfs = mpdevfs->mnt_vnodecovered; 375 if (vpdevfs != NULL) { 376 cache_purge(vpdevfs); 377 vpdevfs->v_mountedhere = NULL; 378 vrele(vpdevfs); 379 } 380 mpdevfs->mnt_vnodecovered = vp; 381 vp->v_mountedhere = mpdevfs; 382 VOP_UNLOCK(vp, 0); 383 } else 384 vput(vp); 385 } 386 if (error) 387 printf("mountroot: unable to remount devfs under /dev " 388 "(error %d)\n", error); 389 NDFREE(&nd, NDF_ONLY_PNBUF); 390 391 if (mporoot == mpdevfs) { 392 vfs_unbusy(mpdevfs); 393 /* Unlink the no longer needed /dev/dev -> / symlink */ 394 error = kern_funlinkat(td, AT_FDCWD, "/dev/dev", FD_NONE, 395 UIO_SYSSPACE, 0, 0); 396 if (error) 397 printf("mountroot: unable to unlink /dev/dev " 398 "(error %d)\n", error); 399 } 400 } 401 402 /* 403 * Configuration parser. 404 */ 405 406 /* Parser character classes. */ 407 #define CC_WHITESPACE -1 408 #define CC_NONWHITESPACE -2 409 410 /* Parse errors. */ 411 #define PE_EOF -1 412 #define PE_EOL -2 413 414 static __inline int 415 parse_peek(char **conf) 416 { 417 418 return (**conf); 419 } 420 421 static __inline void 422 parse_poke(char **conf, int c) 423 { 424 425 **conf = c; 426 } 427 428 static __inline void 429 parse_advance(char **conf) 430 { 431 432 (*conf)++; 433 } 434 435 static int 436 parse_skipto(char **conf, int mc) 437 { 438 int c, match; 439 440 while (1) { 441 c = parse_peek(conf); 442 if (c == 0) 443 return (PE_EOF); 444 switch (mc) { 445 case CC_WHITESPACE: 446 match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0; 447 break; 448 case CC_NONWHITESPACE: 449 if (c == '\n') 450 return (PE_EOL); 451 match = (c != ' ' && c != '\t') ? 1 : 0; 452 break; 453 default: 454 match = (c == mc) ? 1 : 0; 455 break; 456 } 457 if (match) 458 break; 459 parse_advance(conf); 460 } 461 return (0); 462 } 463 464 static int 465 parse_token(char **conf, char **tok) 466 { 467 char *p; 468 size_t len; 469 int error; 470 471 *tok = NULL; 472 error = parse_skipto(conf, CC_NONWHITESPACE); 473 if (error) 474 return (error); 475 p = *conf; 476 error = parse_skipto(conf, CC_WHITESPACE); 477 len = *conf - p; 478 *tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO); 479 bcopy(p, *tok, len); 480 return (0); 481 } 482 483 static void 484 parse_dir_ask_printenv(const char *var) 485 { 486 char *val; 487 488 val = kern_getenv(var); 489 if (val != NULL) { 490 printf(" %s=%s\n", var, val); 491 freeenv(val); 492 } 493 } 494 495 static int 496 parse_dir_ask(char **conf) 497 { 498 char name[80]; 499 char *mnt; 500 int error; 501 502 vfs_mountroot_wait(); 503 504 printf("\nLoader variables:\n"); 505 parse_dir_ask_printenv("vfs.root.mountfrom"); 506 parse_dir_ask_printenv("vfs.root.mountfrom.options"); 507 508 printf("\nManual root filesystem specification:\n"); 509 printf(" <fstype>:<device> [options]\n"); 510 printf(" Mount <device> using filesystem <fstype>\n"); 511 printf(" and with the specified (optional) option list.\n"); 512 printf("\n"); 513 printf(" eg. ufs:/dev/da0s1a\n"); 514 printf(" zfs:zroot/ROOT/default\n"); 515 printf(" cd9660:/dev/cd0 ro\n"); 516 printf(" (which is equivalent to: "); 517 printf("mount -t cd9660 -o ro /dev/cd0 /)\n"); 518 printf("\n"); 519 printf(" ? List valid disk boot devices\n"); 520 printf(" . Yield 1 second (for background tasks)\n"); 521 printf(" <empty line> Abort manual input\n"); 522 523 do { 524 error = EINVAL; 525 printf("\nmountroot> "); 526 cngets(name, sizeof(name), GETS_ECHO); 527 if (name[0] == '\0') 528 break; 529 if (name[0] == '?' && name[1] == '\0') { 530 printf("\nList of GEOM managed disk devices:\n "); 531 g_dev_print(); 532 continue; 533 } 534 if (name[0] == '.' && name[1] == '\0') { 535 pause("rmask", hz); 536 continue; 537 } 538 mnt = name; 539 error = parse_mount(&mnt); 540 if (error == -1) 541 printf("Invalid file system specification.\n"); 542 } while (error != 0); 543 544 return (error); 545 } 546 547 static int 548 parse_dir_md(char **conf) 549 { 550 struct stat sb; 551 struct thread *td; 552 struct md_ioctl *mdio; 553 char *path, *tok; 554 int error, fd, len; 555 556 td = curthread; 557 558 error = parse_token(conf, &tok); 559 if (error) 560 return (error); 561 562 len = strlen(tok); 563 mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO); 564 path = (void *)(mdio + 1); 565 bcopy(tok, path, len); 566 free(tok, M_TEMP); 567 568 /* Get file status. */ 569 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL); 570 if (error) 571 goto out; 572 573 /* Open /dev/mdctl so that we can attach/detach. */ 574 error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE, 575 O_RDWR, 0); 576 if (error) 577 goto out; 578 579 fd = td->td_retval[0]; 580 mdio->md_version = MDIOVERSION; 581 mdio->md_type = MD_VNODE; 582 583 if (root_mount_mddev != -1) { 584 mdio->md_unit = root_mount_mddev; 585 (void)kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio); 586 /* Ignore errors. We don't care. */ 587 root_mount_mddev = -1; 588 } 589 590 mdio->md_file = (void *)(mdio + 1); 591 mdio->md_options = MD_AUTOUNIT | MD_READONLY; 592 mdio->md_mediasize = sb.st_size; 593 mdio->md_unit = 0; 594 error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio); 595 if (error) 596 goto out; 597 598 if (mdio->md_unit > 9) { 599 printf("rootmount: too many md units\n"); 600 mdio->md_file = NULL; 601 mdio->md_options = 0; 602 mdio->md_mediasize = 0; 603 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio); 604 /* Ignore errors. We don't care. */ 605 error = ERANGE; 606 goto out; 607 } 608 609 root_mount_mddev = mdio->md_unit; 610 printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file); 611 612 error = kern_close(td, fd); 613 614 out: 615 free(mdio, M_TEMP); 616 return (error); 617 } 618 619 static int 620 parse_dir_onfail(char **conf) 621 { 622 char *action; 623 int error; 624 625 error = parse_token(conf, &action); 626 if (error) 627 return (error); 628 629 if (!strcmp(action, "continue")) 630 root_mount_onfail = A_CONTINUE; 631 else if (!strcmp(action, "panic")) 632 root_mount_onfail = A_PANIC; 633 else if (!strcmp(action, "reboot")) 634 root_mount_onfail = A_REBOOT; 635 else if (!strcmp(action, "retry")) 636 root_mount_onfail = A_RETRY; 637 else { 638 printf("rootmount: %s: unknown action\n", action); 639 error = EINVAL; 640 } 641 642 free(action, M_TEMP); 643 return (0); 644 } 645 646 static int 647 parse_dir_timeout(char **conf) 648 { 649 char *tok, *endtok; 650 long secs; 651 int error; 652 653 error = parse_token(conf, &tok); 654 if (error) 655 return (error); 656 657 secs = strtol(tok, &endtok, 0); 658 error = (secs < 0 || *endtok != '\0') ? EINVAL : 0; 659 if (!error) 660 root_mount_timeout = secs; 661 free(tok, M_TEMP); 662 return (error); 663 } 664 665 static int 666 parse_directive(char **conf) 667 { 668 char *dir; 669 int error; 670 671 error = parse_token(conf, &dir); 672 if (error) 673 return (error); 674 675 if (strcmp(dir, ".ask") == 0) 676 error = parse_dir_ask(conf); 677 else if (strcmp(dir, ".md") == 0) 678 error = parse_dir_md(conf); 679 else if (strcmp(dir, ".onfail") == 0) 680 error = parse_dir_onfail(conf); 681 else if (strcmp(dir, ".timeout") == 0) 682 error = parse_dir_timeout(conf); 683 else { 684 printf("mountroot: invalid directive `%s'\n", dir); 685 /* Ignore the rest of the line. */ 686 (void)parse_skipto(conf, '\n'); 687 error = EINVAL; 688 } 689 free(dir, M_TEMP); 690 return (error); 691 } 692 693 static int 694 parse_mount_dev_present(const char *dev) 695 { 696 struct nameidata nd; 697 int error; 698 699 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread); 700 error = namei(&nd); 701 if (!error) 702 vput(nd.ni_vp); 703 NDFREE(&nd, NDF_ONLY_PNBUF); 704 return (error != 0) ? 0 : 1; 705 } 706 707 #define ERRMSGL 255 708 static int 709 parse_mount(char **conf) 710 { 711 char *errmsg; 712 struct mntarg *ma; 713 char *dev, *fs, *opts, *tok; 714 int delay, error, timeout; 715 716 error = parse_token(conf, &tok); 717 if (error) 718 return (error); 719 fs = tok; 720 error = parse_skipto(&tok, ':'); 721 if (error) { 722 free(fs, M_TEMP); 723 return (error); 724 } 725 parse_poke(&tok, '\0'); 726 parse_advance(&tok); 727 dev = tok; 728 729 if (root_mount_mddev != -1) { 730 /* Handle substitution for the md unit number. */ 731 tok = strstr(dev, "md#"); 732 if (tok != NULL) 733 tok[2] = '0' + root_mount_mddev; 734 } 735 736 /* Parse options. */ 737 error = parse_token(conf, &tok); 738 opts = (error == 0) ? tok : NULL; 739 740 printf("Trying to mount root from %s:%s [%s]...\n", fs, dev, 741 (opts != NULL) ? opts : ""); 742 743 errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO); 744 745 if (vfs_byname(fs) == NULL) { 746 strlcpy(errmsg, "unknown file system", ERRMSGL); 747 error = ENOENT; 748 goto out; 749 } 750 751 error = vfs_mountroot_wait_if_neccessary(fs, dev); 752 if (error != 0) 753 goto out; 754 755 delay = hz / 10; 756 timeout = root_mount_timeout * hz; 757 758 for (;;) { 759 ma = NULL; 760 ma = mount_arg(ma, "fstype", fs, -1); 761 ma = mount_arg(ma, "fspath", "/", -1); 762 ma = mount_arg(ma, "from", dev, -1); 763 ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL); 764 ma = mount_arg(ma, "ro", NULL, 0); 765 ma = parse_mountroot_options(ma, opts); 766 767 error = kernel_mount(ma, MNT_ROOTFS); 768 if (error == 0 || timeout <= 0) 769 break; 770 771 if (root_mount_timeout * hz == timeout || 772 (bootverbose && timeout % hz == 0)) { 773 printf("Mounting from %s:%s failed with error %d; " 774 "retrying for %d more second%s\n", fs, dev, error, 775 timeout / hz, (timeout / hz > 1) ? "s" : ""); 776 } 777 pause("rmretry", delay); 778 timeout -= delay; 779 } 780 out: 781 if (error) { 782 printf("Mounting from %s:%s failed with error %d", 783 fs, dev, error); 784 if (errmsg[0] != '\0') 785 printf(": %s", errmsg); 786 printf(".\n"); 787 } 788 free(fs, M_TEMP); 789 free(errmsg, M_TEMP); 790 if (opts != NULL) 791 free(opts, M_TEMP); 792 /* kernel_mount can return -1 on error. */ 793 return ((error < 0) ? EDOOFUS : error); 794 } 795 #undef ERRMSGL 796 797 static int 798 vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs) 799 { 800 struct mount *mp; 801 char *conf; 802 int error; 803 804 root_mount_mddev = -1; 805 806 retry: 807 conf = sbuf_data(sb); 808 mp = TAILQ_NEXT(mpdevfs, mnt_list); 809 error = (mp == NULL) ? 0 : EDOOFUS; 810 root_mount_onfail = A_CONTINUE; 811 while (mp == NULL) { 812 error = parse_skipto(&conf, CC_NONWHITESPACE); 813 if (error == PE_EOL) { 814 parse_advance(&conf); 815 continue; 816 } 817 if (error < 0) 818 break; 819 switch (parse_peek(&conf)) { 820 case '#': 821 error = parse_skipto(&conf, '\n'); 822 break; 823 case '.': 824 error = parse_directive(&conf); 825 break; 826 default: 827 error = parse_mount(&conf); 828 if (error == -1) { 829 printf("mountroot: invalid file system " 830 "specification.\n"); 831 error = 0; 832 } 833 break; 834 } 835 if (error < 0) 836 break; 837 /* Ignore any trailing garbage on the line. */ 838 if (parse_peek(&conf) != '\n') { 839 printf("mountroot: advancing to next directive...\n"); 840 (void)parse_skipto(&conf, '\n'); 841 } 842 mp = TAILQ_NEXT(mpdevfs, mnt_list); 843 } 844 if (mp != NULL) 845 return (0); 846 847 /* 848 * We failed to mount (a new) root. 849 */ 850 switch (root_mount_onfail) { 851 case A_CONTINUE: 852 break; 853 case A_PANIC: 854 panic("mountroot: unable to (re-)mount root."); 855 /* NOTREACHED */ 856 case A_RETRY: 857 goto retry; 858 case A_REBOOT: 859 kern_reboot(RB_NOSYNC); 860 /* NOTREACHED */ 861 } 862 863 return (error); 864 } 865 866 static void 867 vfs_mountroot_conf0(struct sbuf *sb) 868 { 869 char *s, *tok, *mnt, *opt; 870 int error; 871 872 sbuf_printf(sb, ".onfail panic\n"); 873 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout); 874 if (boothowto & RB_ASKNAME) 875 sbuf_printf(sb, ".ask\n"); 876 #ifdef ROOTDEVNAME 877 if (boothowto & RB_DFLTROOT) 878 sbuf_printf(sb, "%s\n", ROOTDEVNAME); 879 #endif 880 if (boothowto & RB_CDROM) { 881 sbuf_printf(sb, "cd9660:/dev/cd0 ro\n"); 882 sbuf_printf(sb, ".timeout 0\n"); 883 sbuf_printf(sb, "cd9660:/dev/cd1 ro\n"); 884 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout); 885 } 886 s = kern_getenv("vfs.root.mountfrom"); 887 if (s != NULL) { 888 opt = kern_getenv("vfs.root.mountfrom.options"); 889 tok = s; 890 error = parse_token(&tok, &mnt); 891 while (!error) { 892 sbuf_printf(sb, "%s %s\n", mnt, 893 (opt != NULL) ? opt : ""); 894 free(mnt, M_TEMP); 895 error = parse_token(&tok, &mnt); 896 } 897 if (opt != NULL) 898 freeenv(opt); 899 freeenv(s); 900 } 901 if (rootdevnames[0] != NULL) 902 sbuf_printf(sb, "%s\n", rootdevnames[0]); 903 if (rootdevnames[1] != NULL) 904 sbuf_printf(sb, "%s\n", rootdevnames[1]); 905 #ifdef ROOTDEVNAME 906 if (!(boothowto & RB_DFLTROOT)) 907 sbuf_printf(sb, "%s\n", ROOTDEVNAME); 908 #endif 909 if (!(boothowto & RB_ASKNAME)) 910 sbuf_printf(sb, ".ask\n"); 911 } 912 913 static int 914 vfs_mountroot_readconf(struct thread *td, struct sbuf *sb) 915 { 916 static char buf[128]; 917 struct nameidata nd; 918 off_t ofs; 919 ssize_t resid; 920 int error, flags, len; 921 922 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td); 923 flags = FREAD; 924 error = vn_open(&nd, &flags, 0, NULL); 925 if (error) 926 return (error); 927 928 NDFREE(&nd, NDF_ONLY_PNBUF); 929 ofs = 0; 930 len = sizeof(buf) - 1; 931 while (1) { 932 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs, 933 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 934 NOCRED, &resid, td); 935 if (error) 936 break; 937 if (resid == len) 938 break; 939 buf[len - resid] = 0; 940 sbuf_printf(sb, "%s", buf); 941 ofs += len - resid; 942 } 943 944 VOP_UNLOCK(nd.ni_vp, 0); 945 vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 946 return (error); 947 } 948 949 static void 950 vfs_mountroot_wait(void) 951 { 952 struct root_hold_token *h; 953 struct timeval lastfail; 954 int curfail; 955 956 TSENTER(); 957 958 curfail = 0; 959 while (1) { 960 g_waitidle(); 961 mtx_lock(&root_holds_mtx); 962 if (LIST_EMPTY(&root_holds)) { 963 mtx_unlock(&root_holds_mtx); 964 break; 965 } 966 if (ppsratecheck(&lastfail, &curfail, 1)) { 967 printf("Root mount waiting for:"); 968 LIST_FOREACH(h, &root_holds, list) 969 printf(" %s", h->who); 970 printf("\n"); 971 } 972 TSWAIT("root mount"); 973 msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold", 974 hz); 975 TSUNWAIT("root mount"); 976 } 977 978 TSEXIT(); 979 } 980 981 static int 982 vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev) 983 { 984 int delay, timeout; 985 986 /* 987 * In case of ZFS and NFS we don't have a way to wait for 988 * specific device. Also do the wait if the user forced that 989 * behaviour by setting vfs.root_mount_always_wait=1. 990 */ 991 if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL || 992 dev[0] == '\0' || root_mount_always_wait != 0) { 993 vfs_mountroot_wait(); 994 return (0); 995 } 996 997 /* 998 * Otherwise, no point in waiting if the device is already there. 999 * Note that we must wait for GEOM to finish reconfiguring itself, 1000 * eg for geom_part(4) to finish tasting. 1001 */ 1002 g_waitidle(); 1003 if (parse_mount_dev_present(dev)) 1004 return (0); 1005 1006 /* 1007 * No luck. Let's wait. This code looks weird, but it's that way 1008 * to behave exactly as it used to work before. 1009 */ 1010 vfs_mountroot_wait(); 1011 printf("mountroot: waiting for device %s...\n", dev); 1012 delay = hz / 10; 1013 timeout = root_mount_timeout * hz; 1014 do { 1015 pause("rmdev", delay); 1016 timeout -= delay; 1017 } while (timeout > 0 && !parse_mount_dev_present(dev)); 1018 1019 if (timeout <= 0) 1020 return (ENODEV); 1021 1022 return (0); 1023 } 1024 1025 void 1026 vfs_mountroot(void) 1027 { 1028 struct mount *mp; 1029 struct sbuf *sb; 1030 struct thread *td; 1031 time_t timebase; 1032 int error; 1033 1034 mtx_assert(&Giant, MA_NOTOWNED); 1035 1036 TSENTER(); 1037 1038 td = curthread; 1039 1040 sb = sbuf_new_auto(); 1041 vfs_mountroot_conf0(sb); 1042 sbuf_finish(sb); 1043 1044 error = vfs_mountroot_devfs(td, &mp); 1045 while (!error) { 1046 error = vfs_mountroot_parse(sb, mp); 1047 if (!error) { 1048 vfs_mountroot_shuffle(td, mp); 1049 sbuf_clear(sb); 1050 error = vfs_mountroot_readconf(td, sb); 1051 sbuf_finish(sb); 1052 } 1053 } 1054 1055 sbuf_delete(sb); 1056 1057 /* 1058 * Iterate over all currently mounted file systems and use 1059 * the time stamp found to check and/or initialize the RTC. 1060 * Call inittodr() only once and pass it the largest of the 1061 * timestamps we encounter. 1062 */ 1063 timebase = 0; 1064 mtx_lock(&mountlist_mtx); 1065 mp = TAILQ_FIRST(&mountlist); 1066 while (mp != NULL) { 1067 if (mp->mnt_time > timebase) 1068 timebase = mp->mnt_time; 1069 mp = TAILQ_NEXT(mp, mnt_list); 1070 } 1071 mtx_unlock(&mountlist_mtx); 1072 inittodr(timebase); 1073 1074 /* Keep prison0's root in sync with the global rootvnode. */ 1075 mtx_lock(&prison0.pr_mtx); 1076 prison0.pr_root = rootvnode; 1077 vref(prison0.pr_root); 1078 mtx_unlock(&prison0.pr_mtx); 1079 1080 mtx_lock(&root_holds_mtx); 1081 atomic_store_rel_int(&root_mount_complete, 1); 1082 wakeup(&root_mount_complete); 1083 mtx_unlock(&root_holds_mtx); 1084 1085 EVENTHANDLER_INVOKE(mountroot); 1086 1087 TSEXIT(); 1088 } 1089 1090 static struct mntarg * 1091 parse_mountroot_options(struct mntarg *ma, const char *options) 1092 { 1093 char *p; 1094 char *name, *name_arg; 1095 char *val, *val_arg; 1096 char *opts; 1097 1098 if (options == NULL || options[0] == '\0') 1099 return (ma); 1100 1101 p = opts = strdup(options, M_MOUNT); 1102 if (opts == NULL) { 1103 return (ma); 1104 } 1105 1106 while((name = strsep(&p, ",")) != NULL) { 1107 if (name[0] == '\0') 1108 break; 1109 1110 val = strchr(name, '='); 1111 if (val != NULL) { 1112 *val = '\0'; 1113 ++val; 1114 } 1115 if( strcmp(name, "rw") == 0 || 1116 strcmp(name, "noro") == 0) { 1117 /* 1118 * The first time we mount the root file system, 1119 * we need to mount 'ro', so We need to ignore 1120 * 'rw' and 'noro' mount options. 1121 */ 1122 continue; 1123 } 1124 name_arg = strdup(name, M_MOUNT); 1125 val_arg = NULL; 1126 if (val != NULL) 1127 val_arg = strdup(val, M_MOUNT); 1128 1129 ma = mount_arg(ma, name_arg, val_arg, 1130 (val_arg != NULL ? -1 : 0)); 1131 } 1132 free(opts, M_MOUNT); 1133 return (ma); 1134 } 1135