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