2 * SPDX-License-Identifier: BSD-3-Clause
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.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
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.
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
40 #include "opt_rootdevname.h"
42 #include <sys/param.h>
45 #include <sys/eventhandler.h>
46 #include <sys/fcntl.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mdioctl.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/namei.h>
56 #include <sys/filedesc.h>
57 #include <sys/reboot.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysproto.h>
63 #include <sys/sysctl.h>
64 #include <sys/systm.h>
65 #include <sys/vnode.h>
67 #include <geom/geom.h>
70 * The root filesystem is detailed in the kernel environment variable
71 * vfs.root.mountfrom, which is expected to be in the general format
73 * <vfsname>:[<path>][ <vfsname>:[<path>] ...]
74 * vfsname := the name of a VFS known to the kernel and capable
75 * of being mounted as root
76 * path := disk device name or other data used by the filesystem
77 * to locate its physical store
79 * If the environment variable vfs.root.mountfrom is a space separated list,
80 * each list element is tried in turn and the root filesystem will be mounted
81 * from the first one that succeeds.
83 * The environment variable vfs.root.mountfrom.options is a comma delimited
84 * set of string mount options. These mount options must be parseable
85 * by nmount() in the kernel.
88 static int parse_mount(char **);
89 static struct mntarg *parse_mountroot_options(struct mntarg *, const char *);
90 static int sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS);
91 static void vfs_mountroot_wait(void);
92 static int vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev);
95 * The vnode of the system's root (/ in the filesystem, without chroot
98 struct vnode *rootvnode;
101 * Mount of the system's /dev.
103 struct mount *rootdevmp;
105 char *rootdevnames[2] = {NULL, NULL};
107 struct mtx root_holds_mtx;
108 MTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
110 static TAILQ_HEAD(, root_hold_token) root_holds =
111 TAILQ_HEAD_INITIALIZER(root_holds);
126 static enum action root_mount_onfail = A_CONTINUE;
128 static int root_mount_mddev;
129 static int root_mount_complete;
131 /* By default wait up to 3 seconds for devices to appear. */
132 static int root_mount_timeout = 3;
133 TUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
135 static int root_mount_always_wait = 0;
136 SYSCTL_INT(_vfs, OID_AUTO, root_mount_always_wait, CTLFLAG_RDTUN,
137 &root_mount_always_wait, 0,
138 "Wait for root mount holds even if the root device already exists");
140 SYSCTL_PROC(_vfs, OID_AUTO, root_mount_hold,
141 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
142 NULL, 0, sysctl_vfs_root_mount_hold, "A",
143 "List of root mount hold tokens");
146 sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS)
149 struct root_hold_token *h;
152 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
154 mtx_lock(&root_holds_mtx);
155 TAILQ_FOREACH(h, &root_holds, list) {
156 if (h != TAILQ_FIRST(&root_holds))
158 sbuf_printf(&sb, "%s", h->who);
160 mtx_unlock(&root_holds_mtx);
162 error = sbuf_finish(&sb);
164 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
169 struct root_hold_token *
170 root_mount_hold(const char *identifier)
172 struct root_hold_token *h;
174 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
177 mtx_lock(&root_holds_mtx);
178 TSHOLD("root mount");
179 TAILQ_INSERT_TAIL(&root_holds, h, list);
180 mtx_unlock(&root_holds_mtx);
185 root_mount_hold_token(const char *identifier, struct root_hold_token *h)
188 struct root_hold_token *t;
193 mtx_lock(&root_holds_mtx);
195 TAILQ_FOREACH(t, &root_holds, list) {
197 panic("Duplicate mount hold by '%s' on %p",
202 TSHOLD("root mount");
203 TAILQ_INSERT_TAIL(&root_holds, h, list);
204 mtx_unlock(&root_holds_mtx);
208 root_mount_rel(struct root_hold_token *h)
211 if (h == NULL || h->flags == RH_FREE)
214 mtx_lock(&root_holds_mtx);
215 TAILQ_REMOVE(&root_holds, h, list);
216 TSRELEASE("root mount");
218 mtx_unlock(&root_holds_mtx);
219 if (h->flags == RH_ALLOC) {
229 /* No mutex is acquired here because int stores are atomic. */
230 return (root_mount_complete);
237 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
238 panic("set_rootvnode: Cannot find root vnode");
240 VOP_UNLOCK(rootvnode);
246 vfs_mountroot_devfs(struct thread *td, struct mount **mpp)
248 struct vfsoptlist *opts;
249 struct vfsconf *vfsp;
255 if (rootdevmp != NULL) {
257 * Already have /dev; this happens during rerooting.
259 error = vfs_busy(rootdevmp, 0);
264 vfsp = vfs_byname("devfs");
265 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
269 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
271 error = VFS_MOUNT(mp);
272 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
276 error = VFS_STATFS(mp, &mp->mnt_stat);
277 KASSERT(error == 0, ("VFS_STATFS(devfs) failed %d", error));
281 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
285 mtx_lock(&mountlist_mtx);
286 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
287 mtx_unlock(&mountlist_mtx);
296 error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
298 printf("kern_symlink /dev -> / returns %d\n", error);
304 vfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
307 struct mount *mporoot, *mpnroot;
308 struct vnode *vp, *vporoot, *vpdevfs;
312 mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
314 /* Shuffle the mountlist. */
315 mtx_lock(&mountlist_mtx);
316 mporoot = TAILQ_FIRST(&mountlist);
317 TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
318 if (mporoot != mpdevfs) {
319 TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
320 TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
322 TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
323 mtx_unlock(&mountlist_mtx);
325 cache_purgevfs(mporoot);
326 if (mporoot != mpdevfs)
327 cache_purgevfs(mpdevfs);
329 if (VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot))
330 panic("vfs_mountroot_shuffle: Cannot find root vnode");
333 vporoot->v_iflag &= ~VI_MOUNT;
334 vn_irflag_unset_locked(vporoot, VIRF_MOUNTPOINT);
335 vporoot->v_mountedhere = NULL;
337 mporoot->mnt_flag &= ~MNT_ROOTFS;
338 mporoot->mnt_vnodecovered = NULL;
341 /* Set up the new rootvnode, and purge the cache */
342 mpnroot->mnt_vnodecovered = NULL;
344 cache_purgevfs(rootvnode->v_mount);
346 if (mporoot != mpdevfs) {
347 /* Remount old root under /.mount or /mnt */
349 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath);
353 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
360 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
362 error = vinvalbuf(vp, V_SAVE, 0, 0);
366 mporoot->mnt_vnodecovered = vp;
367 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
368 vp->v_mountedhere = mporoot;
369 strlcpy(mporoot->mnt_stat.f_mntonname,
378 printf("mountroot: unable to remount previous root "
379 "under /.mount or /mnt (error %d)\n", error);
382 /* Remount devfs under /dev */
383 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev");
388 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
390 error = vinvalbuf(vp, V_SAVE, 0, 0);
392 vpdevfs = mpdevfs->mnt_vnodecovered;
393 if (vpdevfs != NULL) {
394 cache_purge(vpdevfs);
396 vn_irflag_unset_locked(vpdevfs, VIRF_MOUNTPOINT);
397 vpdevfs->v_mountedhere = NULL;
402 mpdevfs->mnt_vnodecovered = vp;
403 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
404 vp->v_mountedhere = mpdevfs;
411 printf("mountroot: unable to remount devfs under /dev "
412 "(error %d)\n", error);
414 if (mporoot == mpdevfs) {
416 /* Unlink the no longer needed /dev/dev -> / symlink */
417 error = kern_funlinkat(td, AT_FDCWD, "/dev/dev", FD_NONE,
420 printf("mountroot: unable to unlink /dev/dev "
421 "(error %d)\n", error);
426 * Configuration parser.
429 /* Parser character classes. */
430 #define CC_WHITESPACE -1
431 #define CC_NONWHITESPACE -2
438 parse_peek(char **conf)
445 parse_poke(char **conf, int c)
452 parse_advance(char **conf)
459 parse_skipto(char **conf, int mc)
464 c = parse_peek(conf);
469 match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
471 case CC_NONWHITESPACE:
474 match = (c != ' ' && c != '\t') ? 1 : 0;
477 match = (c == mc) ? 1 : 0;
488 parse_token(char **conf, char **tok)
495 error = parse_skipto(conf, CC_NONWHITESPACE);
499 error = parse_skipto(conf, CC_WHITESPACE);
501 *tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
507 parse_dir_ask_printenv(const char *var)
511 val = kern_getenv(var);
513 printf(" %s=%s\n", var, val);
519 parse_dir_ask(char **conf)
525 vfs_mountroot_wait();
527 printf("\nLoader variables:\n");
528 parse_dir_ask_printenv("vfs.root.mountfrom");
529 parse_dir_ask_printenv("vfs.root.mountfrom.options");
531 printf("\nManual root filesystem specification:\n");
532 printf(" <fstype>:<device> [options]\n");
533 printf(" Mount <device> using filesystem <fstype>\n");
534 printf(" and with the specified (optional) option list.\n");
536 printf(" eg. ufs:/dev/da0s1a\n");
537 printf(" zfs:zroot/ROOT/default\n");
538 printf(" cd9660:/dev/cd0 ro\n");
539 printf(" (which is equivalent to: ");
540 printf("mount -t cd9660 -o ro /dev/cd0 /)\n");
542 printf(" ? List valid disk boot devices\n");
543 printf(" . Yield 1 second (for background tasks)\n");
544 printf(" <empty line> Abort manual input\n");
548 printf("\nmountroot> ");
549 cngets(name, sizeof(name), GETS_ECHO);
552 if (name[0] == '?' && name[1] == '\0') {
553 printf("\nList of GEOM managed disk devices:\n ");
557 if (name[0] == '.' && name[1] == '\0') {
562 error = parse_mount(&mnt);
564 printf("Invalid file system specification.\n");
565 } while (error != 0);
571 parse_dir_md(char **conf)
575 struct md_ioctl *mdio;
582 error = parse_token(conf, &tok);
587 mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
588 path = (void *)(mdio + 1);
589 bcopy(tok, path, len);
592 /* Get file status. */
593 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb);
597 /* Open /dev/mdctl so that we can attach/detach. */
598 error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
603 fd = td->td_retval[0];
604 mdio->md_version = MDIOVERSION;
605 mdio->md_type = MD_VNODE;
607 if (root_mount_mddev != -1) {
608 mdio->md_unit = root_mount_mddev;
609 (void)kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
610 /* Ignore errors. We don't care. */
611 root_mount_mddev = -1;
614 mdio->md_file = (void *)(mdio + 1);
615 mdio->md_options = MD_AUTOUNIT | MD_READONLY;
616 mdio->md_mediasize = sb.st_size;
618 error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
622 if (mdio->md_unit > 9) {
623 printf("rootmount: too many md units\n");
624 mdio->md_file = NULL;
625 mdio->md_options = 0;
626 mdio->md_mediasize = 0;
627 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
628 /* Ignore errors. We don't care. */
633 root_mount_mddev = mdio->md_unit;
634 printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
638 (void)kern_close(td, fd);
644 parse_dir_onfail(char **conf)
649 error = parse_token(conf, &action);
653 if (!strcmp(action, "continue"))
654 root_mount_onfail = A_CONTINUE;
655 else if (!strcmp(action, "panic"))
656 root_mount_onfail = A_PANIC;
657 else if (!strcmp(action, "reboot"))
658 root_mount_onfail = A_REBOOT;
659 else if (!strcmp(action, "retry"))
660 root_mount_onfail = A_RETRY;
662 printf("rootmount: %s: unknown action\n", action);
666 free(action, M_TEMP);
671 parse_dir_timeout(char **conf)
677 error = parse_token(conf, &tok);
681 secs = strtol(tok, &endtok, 0);
682 error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
684 root_mount_timeout = secs;
690 parse_directive(char **conf)
695 error = parse_token(conf, &dir);
699 if (strcmp(dir, ".ask") == 0)
700 error = parse_dir_ask(conf);
701 else if (strcmp(dir, ".md") == 0)
702 error = parse_dir_md(conf);
703 else if (strcmp(dir, ".onfail") == 0)
704 error = parse_dir_onfail(conf);
705 else if (strcmp(dir, ".timeout") == 0)
706 error = parse_dir_timeout(conf);
708 printf("mountroot: invalid directive `%s'\n", dir);
709 /* Ignore the rest of the line. */
710 (void)parse_skipto(conf, '\n');
718 parse_mount_dev_present(const char *dev)
723 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, dev);
734 parse_mount(char **conf)
738 char *dev, *fs, *opts, *tok;
739 int delay, error, timeout;
741 error = parse_token(conf, &tok);
745 error = parse_skipto(&tok, ':');
750 parse_poke(&tok, '\0');
754 if (root_mount_mddev != -1) {
755 /* Handle substitution for the md unit number. */
756 tok = strstr(dev, "md#");
758 tok[2] = '0' + root_mount_mddev;
762 error = parse_token(conf, &tok);
763 opts = (error == 0) ? tok : NULL;
765 printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
766 (opts != NULL) ? opts : "");
768 errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
770 if (vfs_byname(fs) == NULL) {
771 strlcpy(errmsg, "unknown file system", ERRMSGL);
776 error = vfs_mountroot_wait_if_neccessary(fs, dev);
781 timeout = root_mount_timeout * hz;
785 ma = mount_arg(ma, "fstype", fs, -1);
786 ma = mount_arg(ma, "fspath", "/", -1);
787 ma = mount_arg(ma, "from", dev, -1);
788 ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
789 ma = mount_arg(ma, "ro", NULL, 0);
790 ma = parse_mountroot_options(ma, opts);
792 error = kernel_mount(ma, MNT_ROOTFS);
793 if (error == 0 || error == EILSEQ || timeout <= 0)
796 if (root_mount_timeout * hz == timeout ||
797 (bootverbose && timeout % hz == 0)) {
798 printf("Mounting from %s:%s failed with error %d; "
799 "retrying for %d more second%s\n", fs, dev, error,
800 timeout / hz, (timeout / hz > 1) ? "s" : "");
802 pause("rmretry", delay);
807 printf("Mounting from %s:%s failed with error %d",
809 if (errmsg[0] != '\0')
810 printf(": %s", errmsg);
814 free(errmsg, M_TEMP);
817 /* kernel_mount can return -1 on error. */
818 return ((error < 0) ? EDOOFUS : error);
823 vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
829 root_mount_mddev = -1;
832 conf = sbuf_data(sb);
833 mp = TAILQ_NEXT(mpdevfs, mnt_list);
834 error = (mp == NULL) ? 0 : EDOOFUS;
835 root_mount_onfail = A_CONTINUE;
837 error = parse_skipto(&conf, CC_NONWHITESPACE);
838 if (error == PE_EOL) {
839 parse_advance(&conf);
844 switch (parse_peek(&conf)) {
846 error = parse_skipto(&conf, '\n');
849 error = parse_directive(&conf);
852 error = parse_mount(&conf);
854 printf("mountroot: invalid file system "
862 /* Ignore any trailing garbage on the line. */
863 if (parse_peek(&conf) != '\n') {
864 printf("mountroot: advancing to next directive...\n");
865 (void)parse_skipto(&conf, '\n');
867 mp = TAILQ_NEXT(mpdevfs, mnt_list);
873 * We failed to mount (a new) root.
875 switch (root_mount_onfail) {
879 panic("mountroot: unable to (re-)mount root.");
884 kern_reboot(RB_NOSYNC);
892 vfs_mountroot_conf0(struct sbuf *sb)
894 char *s, *tok, *mnt, *opt;
897 sbuf_cat(sb, ".onfail panic\n");
898 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
899 if (boothowto & RB_ASKNAME)
900 sbuf_cat(sb, ".ask\n");
902 if (boothowto & RB_DFLTROOT)
903 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
905 if (boothowto & RB_CDROM) {
906 sbuf_cat(sb, "cd9660:/dev/cd0 ro\n");
907 sbuf_cat(sb, ".timeout 0\n");
908 sbuf_cat(sb, "cd9660:/dev/cd1 ro\n");
909 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
911 s = kern_getenv("vfs.root.mountfrom");
913 opt = kern_getenv("vfs.root.mountfrom.options");
915 error = parse_token(&tok, &mnt);
917 sbuf_printf(sb, "%s %s\n", mnt,
918 (opt != NULL) ? opt : "");
920 error = parse_token(&tok, &mnt);
926 if (rootdevnames[0] != NULL)
927 sbuf_printf(sb, "%s\n", rootdevnames[0]);
928 if (rootdevnames[1] != NULL)
929 sbuf_printf(sb, "%s\n", rootdevnames[1]);
931 if (!(boothowto & RB_DFLTROOT))
932 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
934 if (!(boothowto & RB_ASKNAME))
935 sbuf_cat(sb, ".ask\n");
939 vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
941 static char buf[128];
945 int error, flags, len;
947 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf");
949 error = vn_open(&nd, &flags, 0, NULL);
955 len = sizeof(buf) - 1;
957 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
958 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
964 buf[len - resid] = 0;
965 sbuf_printf(sb, "%s", buf);
969 VOP_UNLOCK(nd.ni_vp);
970 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
975 vfs_mountroot_wait(void)
977 struct root_hold_token *h;
979 struct timeval lastfail;
986 eventratecheck(&lastfail, &curfail, 1);
990 mtx_lock(&root_holds_mtx);
991 if (TAILQ_EMPTY(&root_holds)) {
992 mtx_unlock(&root_holds_mtx);
995 if (eventratecheck(&lastfail, &curfail, 1)) {
996 printf("Root mount waiting for:");
997 TAILQ_FOREACH(h, &root_holds, list)
998 printf(" %s", h->who);
1001 TSWAIT("root mount");
1002 msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
1004 TSUNWAIT("root mount");
1012 vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev)
1017 * In case of ZFS and NFS we don't have a way to wait for
1018 * specific device. Also do the wait if the user forced that
1019 * behaviour by setting vfs.root_mount_always_wait=1.
1021 if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL ||
1022 dev[0] == '\0' || root_mount_always_wait != 0) {
1023 vfs_mountroot_wait();
1028 * Otherwise, no point in waiting if the device is already there.
1029 * Note that we must wait for GEOM to finish reconfiguring itself,
1030 * eg for geom_part(4) to finish tasting.
1032 g_waitidle(curthread);
1033 if (parse_mount_dev_present(dev))
1037 * No luck. Let's wait. This code looks weird, but it's that way
1038 * to behave exactly as it used to work before.
1040 vfs_mountroot_wait();
1041 if (parse_mount_dev_present(dev))
1043 printf("mountroot: waiting for device %s...\n", dev);
1045 timeout = root_mount_timeout * hz;
1047 pause("rmdev", delay);
1049 } while (timeout > 0 && !parse_mount_dev_present(dev));
1066 mtx_assert(&Giant, MA_NOTOWNED);
1072 sb = sbuf_new_auto();
1073 vfs_mountroot_conf0(sb);
1076 error = vfs_mountroot_devfs(td, &mp);
1078 error = vfs_mountroot_parse(sb, mp);
1080 vfs_mountroot_shuffle(td, mp);
1082 error = vfs_mountroot_readconf(td, sb);
1090 * Iterate over all currently mounted file systems and use
1091 * the time stamp found to check and/or initialize the RTC.
1092 * Call inittodr() only once and pass it the largest of the
1093 * timestamps we encounter.
1096 mtx_lock(&mountlist_mtx);
1097 mp = TAILQ_FIRST(&mountlist);
1098 while (mp != NULL) {
1099 if (mp->mnt_time > timebase)
1100 timebase = mp->mnt_time;
1101 mp = TAILQ_NEXT(mp, mnt_list);
1103 mtx_unlock(&mountlist_mtx);
1106 /* Keep prison0's root in sync with the global rootvnode. */
1107 mtx_lock(&prison0.pr_mtx);
1108 prison0.pr_root = rootvnode;
1109 vref(prison0.pr_root);
1110 mtx_unlock(&prison0.pr_mtx);
1112 mtx_lock(&root_holds_mtx);
1113 atomic_store_rel_int(&root_mount_complete, 1);
1114 wakeup(&root_mount_complete);
1115 mtx_unlock(&root_holds_mtx);
1117 EVENTHANDLER_INVOKE(mountroot);
1122 static struct mntarg *
1123 parse_mountroot_options(struct mntarg *ma, const char *options)
1126 char *name, *name_arg;
1127 char *val, *val_arg;
1130 if (options == NULL || options[0] == '\0')
1133 p = opts = strdup(options, M_MOUNT);
1138 while((name = strsep(&p, ",")) != NULL) {
1139 if (name[0] == '\0')
1142 val = strchr(name, '=');
1147 if (strcmp(name, "rw") == 0 || strcmp(name, "noro") == 0) {
1149 * The first time we mount the root file system,
1150 * we need to mount 'ro', so We need to ignore
1151 * 'rw' and 'noro' mount options.
1155 name_arg = strdup(name, M_MOUNT);
1158 val_arg = strdup(val, M_MOUNT);
1160 ma = mount_arg(ma, name_arg, val_arg,
1161 (val_arg != NULL ? -1 : 0));
1163 free(opts, M_MOUNT);