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/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
48 #include <sys/fcntl.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>
58 #include <sys/filedesc.h>
59 #include <sys/reboot.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysproto.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/systm.h>
68 #include <sys/vnode.h>
70 #include <geom/geom.h>
73 * The root filesystem is detailed in the kernel environment variable
74 * vfs.root.mountfrom, which is expected to be in the general format
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
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.
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.
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);
98 * The vnode of the system's root (/ in the filesystem, without chroot
101 struct vnode *rootvnode;
104 * Mount of the system's /dev.
106 struct mount *rootdevmp;
108 char *rootdevnames[2] = {NULL, NULL};
110 struct mtx root_holds_mtx;
111 MTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
113 struct root_hold_token {
115 LIST_ENTRY(root_hold_token) list;
118 static LIST_HEAD(, root_hold_token) root_holds =
119 LIST_HEAD_INITIALIZER(root_holds);
128 static enum action root_mount_onfail = A_CONTINUE;
130 static int root_mount_mddev;
131 static int root_mount_complete;
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);
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");
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");
148 sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS)
151 struct root_hold_token *h;
154 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
156 mtx_lock(&root_holds_mtx);
157 LIST_FOREACH(h, &root_holds, list) {
158 if (h != LIST_FIRST(&root_holds))
160 sbuf_printf(&sb, "%s", h->who);
162 mtx_unlock(&root_holds_mtx);
164 error = sbuf_finish(&sb);
166 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
171 struct root_hold_token *
172 root_mount_hold(const char *identifier)
174 struct root_hold_token *h;
176 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
178 mtx_lock(&root_holds_mtx);
179 TSHOLD("root mount");
180 LIST_INSERT_HEAD(&root_holds, h, list);
181 mtx_unlock(&root_holds_mtx);
186 root_mount_rel(struct root_hold_token *h)
192 mtx_lock(&root_holds_mtx);
193 LIST_REMOVE(h, list);
194 TSRELEASE("root mount");
196 mtx_unlock(&root_holds_mtx);
204 /* No mutex is acquired here because int stores are atomic. */
205 return (root_mount_complete);
213 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
214 panic("Cannot find root vnode");
216 VOP_UNLOCK(rootvnode, 0);
218 p = curthread->td_proc;
219 FILEDESC_XLOCK(p->p_fd);
221 if (p->p_fd->fd_cdir != NULL)
222 vrele(p->p_fd->fd_cdir);
223 p->p_fd->fd_cdir = rootvnode;
226 if (p->p_fd->fd_rdir != NULL)
227 vrele(p->p_fd->fd_rdir);
228 p->p_fd->fd_rdir = rootvnode;
231 FILEDESC_XUNLOCK(p->p_fd);
235 vfs_mountroot_devfs(struct thread *td, struct mount **mpp)
237 struct vfsoptlist *opts;
238 struct vfsconf *vfsp;
244 if (rootdevmp != NULL) {
246 * Already have /dev; this happens during rerooting.
248 error = vfs_busy(rootdevmp, 0);
253 vfsp = vfs_byname("devfs");
254 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
258 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
260 error = VFS_MOUNT(mp);
261 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
265 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
269 mtx_lock(&mountlist_mtx);
270 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
271 mtx_unlock(&mountlist_mtx);
279 error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
281 printf("kern_symlink /dev -> / returns %d\n", error);
287 vfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
290 struct mount *mporoot, *mpnroot;
291 struct vnode *vp, *vporoot, *vpdevfs;
295 mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
297 /* Shuffle the mountlist. */
298 mtx_lock(&mountlist_mtx);
299 mporoot = TAILQ_FIRST(&mountlist);
300 TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
301 if (mporoot != mpdevfs) {
302 TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
303 TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
305 TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
306 mtx_unlock(&mountlist_mtx);
308 cache_purgevfs(mporoot, true);
309 if (mporoot != mpdevfs)
310 cache_purgevfs(mpdevfs, true);
312 VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot);
315 vporoot->v_iflag &= ~VI_MOUNT;
317 vporoot->v_mountedhere = NULL;
318 mporoot->mnt_flag &= ~MNT_ROOTFS;
319 mporoot->mnt_vnodecovered = NULL;
322 /* Set up the new rootvnode, and purge the cache */
323 mpnroot->mnt_vnodecovered = NULL;
325 cache_purgevfs(rootvnode->v_mount, true);
327 if (mporoot != mpdevfs) {
328 /* Remount old root under /.mount or /mnt */
330 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
334 NDFREE(&nd, NDF_ONLY_PNBUF);
336 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
342 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
344 error = vinvalbuf(vp, V_SAVE, 0, 0);
347 mporoot->mnt_vnodecovered = vp;
348 vp->v_mountedhere = mporoot;
349 strlcpy(mporoot->mnt_stat.f_mntonname,
355 NDFREE(&nd, NDF_ONLY_PNBUF);
358 printf("mountroot: unable to remount previous root "
359 "under /.mount or /mnt (error %d)\n", error);
362 /* Remount devfs under /dev */
363 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
367 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
369 error = vinvalbuf(vp, V_SAVE, 0, 0);
371 vpdevfs = mpdevfs->mnt_vnodecovered;
372 if (vpdevfs != NULL) {
373 cache_purge(vpdevfs);
374 vpdevfs->v_mountedhere = NULL;
377 mpdevfs->mnt_vnodecovered = vp;
378 vp->v_mountedhere = mpdevfs;
384 printf("mountroot: unable to remount devfs under /dev "
385 "(error %d)\n", error);
386 NDFREE(&nd, NDF_ONLY_PNBUF);
388 if (mporoot == mpdevfs) {
390 /* Unlink the no longer needed /dev/dev -> / symlink */
391 error = kern_unlinkat(td, AT_FDCWD, "/dev/dev",
394 printf("mountroot: unable to unlink /dev/dev "
395 "(error %d)\n", error);
400 * Configuration parser.
403 /* Parser character classes. */
404 #define CC_WHITESPACE -1
405 #define CC_NONWHITESPACE -2
412 parse_peek(char **conf)
419 parse_poke(char **conf, int c)
426 parse_advance(char **conf)
433 parse_skipto(char **conf, int mc)
438 c = parse_peek(conf);
443 match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
445 case CC_NONWHITESPACE:
448 match = (c != ' ' && c != '\t') ? 1 : 0;
451 match = (c == mc) ? 1 : 0;
462 parse_token(char **conf, char **tok)
469 error = parse_skipto(conf, CC_NONWHITESPACE);
473 error = parse_skipto(conf, CC_WHITESPACE);
475 *tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
481 parse_dir_ask_printenv(const char *var)
485 val = kern_getenv(var);
487 printf(" %s=%s\n", var, val);
493 parse_dir_ask(char **conf)
499 vfs_mountroot_wait();
501 printf("\nLoader variables:\n");
502 parse_dir_ask_printenv("vfs.root.mountfrom");
503 parse_dir_ask_printenv("vfs.root.mountfrom.options");
505 printf("\nManual root filesystem specification:\n");
506 printf(" <fstype>:<device> [options]\n");
507 printf(" Mount <device> using filesystem <fstype>\n");
508 printf(" and with the specified (optional) option list.\n");
510 printf(" eg. ufs:/dev/da0s1a\n");
511 printf(" zfs:zroot/ROOT/default\n");
512 printf(" cd9660:/dev/cd0 ro\n");
513 printf(" (which is equivalent to: ");
514 printf("mount -t cd9660 -o ro /dev/cd0 /)\n");
516 printf(" ? List valid disk boot devices\n");
517 printf(" . Yield 1 second (for background tasks)\n");
518 printf(" <empty line> Abort manual input\n");
522 printf("\nmountroot> ");
523 cngets(name, sizeof(name), GETS_ECHO);
526 if (name[0] == '?' && name[1] == '\0') {
527 printf("\nList of GEOM managed disk devices:\n ");
531 if (name[0] == '.' && name[1] == '\0') {
536 error = parse_mount(&mnt);
538 printf("Invalid file system specification.\n");
539 } while (error != 0);
545 parse_dir_md(char **conf)
549 struct md_ioctl *mdio;
555 error = parse_token(conf, &tok);
560 mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
561 path = (void *)(mdio + 1);
562 bcopy(tok, path, len);
565 /* Get file status. */
566 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL);
570 /* Open /dev/mdctl so that we can attach/detach. */
571 error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
576 fd = td->td_retval[0];
577 mdio->md_version = MDIOVERSION;
578 mdio->md_type = MD_VNODE;
580 if (root_mount_mddev != -1) {
581 mdio->md_unit = root_mount_mddev;
582 (void)kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
583 /* Ignore errors. We don't care. */
584 root_mount_mddev = -1;
587 mdio->md_file = (void *)(mdio + 1);
588 mdio->md_options = MD_AUTOUNIT | MD_READONLY;
589 mdio->md_mediasize = sb.st_size;
591 error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
595 if (mdio->md_unit > 9) {
596 printf("rootmount: too many md units\n");
597 mdio->md_file = NULL;
598 mdio->md_options = 0;
599 mdio->md_mediasize = 0;
600 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
601 /* Ignore errors. We don't care. */
606 root_mount_mddev = mdio->md_unit;
607 printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
609 error = kern_close(td, fd);
617 parse_dir_onfail(char **conf)
622 error = parse_token(conf, &action);
626 if (!strcmp(action, "continue"))
627 root_mount_onfail = A_CONTINUE;
628 else if (!strcmp(action, "panic"))
629 root_mount_onfail = A_PANIC;
630 else if (!strcmp(action, "reboot"))
631 root_mount_onfail = A_REBOOT;
632 else if (!strcmp(action, "retry"))
633 root_mount_onfail = A_RETRY;
635 printf("rootmount: %s: unknown action\n", action);
639 free(action, M_TEMP);
644 parse_dir_timeout(char **conf)
650 error = parse_token(conf, &tok);
654 secs = strtol(tok, &endtok, 0);
655 error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
657 root_mount_timeout = secs;
663 parse_directive(char **conf)
668 error = parse_token(conf, &dir);
672 if (strcmp(dir, ".ask") == 0)
673 error = parse_dir_ask(conf);
674 else if (strcmp(dir, ".md") == 0)
675 error = parse_dir_md(conf);
676 else if (strcmp(dir, ".onfail") == 0)
677 error = parse_dir_onfail(conf);
678 else if (strcmp(dir, ".timeout") == 0)
679 error = parse_dir_timeout(conf);
681 printf("mountroot: invalid directive `%s'\n", dir);
682 /* Ignore the rest of the line. */
683 (void)parse_skipto(conf, '\n');
691 parse_mount_dev_present(const char *dev)
696 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
700 NDFREE(&nd, NDF_ONLY_PNBUF);
701 return (error != 0) ? 0 : 1;
706 parse_mount(char **conf)
710 char *dev, *fs, *opts, *tok;
711 int delay, error, timeout;
713 error = parse_token(conf, &tok);
717 error = parse_skipto(&tok, ':');
722 parse_poke(&tok, '\0');
726 if (root_mount_mddev != -1) {
727 /* Handle substitution for the md unit number. */
728 tok = strstr(dev, "md#");
730 tok[2] = '0' + root_mount_mddev;
734 error = parse_token(conf, &tok);
735 opts = (error == 0) ? tok : NULL;
737 printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
738 (opts != NULL) ? opts : "");
740 errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
742 if (vfs_byname(fs) == NULL) {
743 strlcpy(errmsg, "unknown file system", ERRMSGL);
748 error = vfs_mountroot_wait_if_neccessary(fs, dev);
753 timeout = root_mount_timeout * hz;
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);
764 error = kernel_mount(ma, MNT_ROOTFS);
765 if (error == 0 || timeout <= 0)
768 if (root_mount_timeout * hz == timeout ||
769 (bootverbose && timeout % hz == 0)) {
770 printf("Mounting from %s:%s failed with error %d; "
771 "retrying for %d more second%s\n", fs, dev, error,
772 timeout / hz, (timeout / hz > 1) ? "s" : "");
774 pause("rmretry", delay);
779 printf("Mounting from %s:%s failed with error %d",
781 if (errmsg[0] != '\0')
782 printf(": %s", errmsg);
786 free(errmsg, M_TEMP);
789 /* kernel_mount can return -1 on error. */
790 return ((error < 0) ? EDOOFUS : error);
795 vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
801 root_mount_mddev = -1;
804 conf = sbuf_data(sb);
805 mp = TAILQ_NEXT(mpdevfs, mnt_list);
806 error = (mp == NULL) ? 0 : EDOOFUS;
807 root_mount_onfail = A_CONTINUE;
809 error = parse_skipto(&conf, CC_NONWHITESPACE);
810 if (error == PE_EOL) {
811 parse_advance(&conf);
816 switch (parse_peek(&conf)) {
818 error = parse_skipto(&conf, '\n');
821 error = parse_directive(&conf);
824 error = parse_mount(&conf);
826 printf("mountroot: invalid file system "
834 /* Ignore any trailing garbage on the line. */
835 if (parse_peek(&conf) != '\n') {
836 printf("mountroot: advancing to next directive...\n");
837 (void)parse_skipto(&conf, '\n');
839 mp = TAILQ_NEXT(mpdevfs, mnt_list);
845 * We failed to mount (a new) root.
847 switch (root_mount_onfail) {
851 panic("mountroot: unable to (re-)mount root.");
856 kern_reboot(RB_NOSYNC);
864 vfs_mountroot_conf0(struct sbuf *sb)
866 char *s, *tok, *mnt, *opt;
869 sbuf_printf(sb, ".onfail panic\n");
870 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
871 if (boothowto & RB_ASKNAME)
872 sbuf_printf(sb, ".ask\n");
874 if (boothowto & RB_DFLTROOT)
875 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
877 if (boothowto & RB_CDROM) {
878 sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
879 sbuf_printf(sb, ".timeout 0\n");
880 sbuf_printf(sb, "cd9660:/dev/cd1 ro\n");
881 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
883 s = kern_getenv("vfs.root.mountfrom");
885 opt = kern_getenv("vfs.root.mountfrom.options");
887 error = parse_token(&tok, &mnt);
889 sbuf_printf(sb, "%s %s\n", mnt,
890 (opt != NULL) ? opt : "");
892 error = parse_token(&tok, &mnt);
898 if (rootdevnames[0] != NULL)
899 sbuf_printf(sb, "%s\n", rootdevnames[0]);
900 if (rootdevnames[1] != NULL)
901 sbuf_printf(sb, "%s\n", rootdevnames[1]);
903 if (!(boothowto & RB_DFLTROOT))
904 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
906 if (!(boothowto & RB_ASKNAME))
907 sbuf_printf(sb, ".ask\n");
911 vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
913 static char buf[128];
917 int error, flags, len;
919 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
921 error = vn_open(&nd, &flags, 0, NULL);
925 NDFREE(&nd, NDF_ONLY_PNBUF);
927 len = sizeof(buf) - 1;
929 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
930 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
936 buf[len - resid] = 0;
937 sbuf_printf(sb, "%s", buf);
941 VOP_UNLOCK(nd.ni_vp, 0);
942 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
947 vfs_mountroot_wait(void)
949 struct root_hold_token *h;
950 struct timeval lastfail;
958 mtx_lock(&root_holds_mtx);
959 if (LIST_EMPTY(&root_holds)) {
960 mtx_unlock(&root_holds_mtx);
963 if (ppsratecheck(&lastfail, &curfail, 1)) {
964 printf("Root mount waiting for:");
965 LIST_FOREACH(h, &root_holds, list)
966 printf(" %s", h->who);
969 TSWAIT("root mount");
970 msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
972 TSUNWAIT("root mount");
979 vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev)
984 * In case of ZFS and NFS we don't have a way to wait for
985 * specific device. Also do the wait if the user forced that
986 * behaviour by setting vfs.root_mount_always_wait=1.
988 if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL ||
989 dev[0] == '\0' || root_mount_always_wait != 0) {
990 vfs_mountroot_wait();
995 * Otherwise, no point in waiting if the device is already there.
996 * Note that we must wait for GEOM to finish reconfiguring itself,
997 * eg for geom_part(4) to finish tasting.
1000 if (parse_mount_dev_present(dev))
1004 * No luck. Let's wait. This code looks weird, but it's that way
1005 * to behave exactly as it used to work before.
1007 vfs_mountroot_wait();
1008 printf("mountroot: waiting for device %s...\n", dev);
1010 timeout = root_mount_timeout * hz;
1012 pause("rmdev", delay);
1014 } while (timeout > 0 && !parse_mount_dev_present(dev));
1031 mtx_assert(&Giant, MA_NOTOWNED);
1037 sb = sbuf_new_auto();
1038 vfs_mountroot_conf0(sb);
1041 error = vfs_mountroot_devfs(td, &mp);
1043 error = vfs_mountroot_parse(sb, mp);
1045 vfs_mountroot_shuffle(td, mp);
1047 error = vfs_mountroot_readconf(td, sb);
1055 * Iterate over all currently mounted file systems and use
1056 * the time stamp found to check and/or initialize the RTC.
1057 * Call inittodr() only once and pass it the largest of the
1058 * timestamps we encounter.
1061 mtx_lock(&mountlist_mtx);
1062 mp = TAILQ_FIRST(&mountlist);
1063 while (mp != NULL) {
1064 if (mp->mnt_time > timebase)
1065 timebase = mp->mnt_time;
1066 mp = TAILQ_NEXT(mp, mnt_list);
1068 mtx_unlock(&mountlist_mtx);
1071 /* Keep prison0's root in sync with the global rootvnode. */
1072 mtx_lock(&prison0.pr_mtx);
1073 prison0.pr_root = rootvnode;
1074 vref(prison0.pr_root);
1075 mtx_unlock(&prison0.pr_mtx);
1077 mtx_lock(&root_holds_mtx);
1078 atomic_store_rel_int(&root_mount_complete, 1);
1079 wakeup(&root_mount_complete);
1080 mtx_unlock(&root_holds_mtx);
1082 EVENTHANDLER_INVOKE(mountroot);
1087 static struct mntarg *
1088 parse_mountroot_options(struct mntarg *ma, const char *options)
1091 char *name, *name_arg;
1092 char *val, *val_arg;
1095 if (options == NULL || options[0] == '\0')
1098 p = opts = strdup(options, M_MOUNT);
1103 while((name = strsep(&p, ",")) != NULL) {
1104 if (name[0] == '\0')
1107 val = strchr(name, '=');
1112 if( strcmp(name, "rw") == 0 ||
1113 strcmp(name, "noro") == 0) {
1115 * The first time we mount the root file system,
1116 * we need to mount 'ro', so We need to ignore
1117 * 'rw' and 'noro' mount options.
1121 name_arg = strdup(name, M_MOUNT);
1124 val_arg = strdup(val, M_MOUNT);
1126 ma = mount_arg(ma, name_arg, val_arg,
1127 (val_arg != NULL ? -1 : 0));
1129 free(opts, M_MOUNT);