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