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