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/eventhandler.h>
49 #include <sys/fcntl.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>
59 #include <sys/filedesc.h>
60 #include <sys/reboot.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysproto.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysent.h>
68 #include <sys/systm.h>
69 #include <sys/vnode.h>
71 #include <geom/geom.h>
74 * The root filesystem is detailed in the kernel environment variable
75 * vfs.root.mountfrom, which is expected to be in the general format
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
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.
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.
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);
99 * The vnode of the system's root (/ in the filesystem, without chroot
102 struct vnode *rootvnode;
105 * Mount of the system's /dev.
107 struct mount *rootdevmp;
109 char *rootdevnames[2] = {NULL, NULL};
111 struct mtx root_holds_mtx;
112 MTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
114 static TAILQ_HEAD(, root_hold_token) root_holds =
115 TAILQ_HEAD_INITIALIZER(root_holds);
130 static enum action root_mount_onfail = A_CONTINUE;
132 static int root_mount_mddev;
133 static int root_mount_complete;
135 /* By default wait up to 3 seconds for devices to appear. */
136 static int root_mount_timeout = 3;
137 TUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
139 static int root_mount_always_wait = 0;
140 SYSCTL_INT(_vfs, OID_AUTO, root_mount_always_wait, CTLFLAG_RDTUN,
141 &root_mount_always_wait, 0,
142 "Wait for root mount holds even if the root device already exists");
144 SYSCTL_PROC(_vfs, OID_AUTO, root_mount_hold,
145 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
146 NULL, 0, sysctl_vfs_root_mount_hold, "A",
147 "List of root mount hold tokens");
150 sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS)
153 struct root_hold_token *h;
156 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
158 mtx_lock(&root_holds_mtx);
159 TAILQ_FOREACH(h, &root_holds, list) {
160 if (h != TAILQ_FIRST(&root_holds))
162 sbuf_printf(&sb, "%s", h->who);
164 mtx_unlock(&root_holds_mtx);
166 error = sbuf_finish(&sb);
168 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
173 struct root_hold_token *
174 root_mount_hold(const char *identifier)
176 struct root_hold_token *h;
178 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
181 mtx_lock(&root_holds_mtx);
182 TSHOLD("root mount");
183 TAILQ_INSERT_TAIL(&root_holds, h, list);
184 mtx_unlock(&root_holds_mtx);
189 root_mount_hold_token(const char *identifier, struct root_hold_token *h)
192 struct root_hold_token *t;
197 mtx_lock(&root_holds_mtx);
199 TAILQ_FOREACH(t, &root_holds, list) {
201 panic("Duplicate mount hold by '%s' on %p",
206 TSHOLD("root mount");
207 TAILQ_INSERT_TAIL(&root_holds, h, list);
208 mtx_unlock(&root_holds_mtx);
212 root_mount_rel(struct root_hold_token *h)
215 if (h == NULL || h->flags == RH_FREE)
218 mtx_lock(&root_holds_mtx);
219 TAILQ_REMOVE(&root_holds, h, list);
220 TSRELEASE("root mount");
222 mtx_unlock(&root_holds_mtx);
223 if (h->flags == RH_ALLOC) {
233 /* No mutex is acquired here because int stores are atomic. */
234 return (root_mount_complete);
241 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
242 panic("set_rootvnode: Cannot find root vnode");
244 VOP_UNLOCK(rootvnode);
250 vfs_mountroot_devfs(struct thread *td, struct mount **mpp)
252 struct vfsoptlist *opts;
253 struct vfsconf *vfsp;
259 if (rootdevmp != NULL) {
261 * Already have /dev; this happens during rerooting.
263 error = vfs_busy(rootdevmp, 0);
268 vfsp = vfs_byname("devfs");
269 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
273 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
275 error = VFS_MOUNT(mp);
276 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
280 error = VFS_STATFS(mp, &mp->mnt_stat);
281 KASSERT(error == 0, ("VFS_STATFS(devfs) failed %d", error));
285 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
289 mtx_lock(&mountlist_mtx);
290 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
291 mtx_unlock(&mountlist_mtx);
300 error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
302 printf("kern_symlink /dev -> / returns %d\n", error);
308 vfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
311 struct mount *mporoot, *mpnroot;
312 struct vnode *vp, *vporoot, *vpdevfs;
316 mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
318 /* Shuffle the mountlist. */
319 mtx_lock(&mountlist_mtx);
320 mporoot = TAILQ_FIRST(&mountlist);
321 TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
322 if (mporoot != mpdevfs) {
323 TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
324 TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
326 TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
327 mtx_unlock(&mountlist_mtx);
329 cache_purgevfs(mporoot);
330 if (mporoot != mpdevfs)
331 cache_purgevfs(mpdevfs);
333 if (VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot))
334 panic("vfs_mountroot_shuffle: Cannot find root vnode");
337 vporoot->v_iflag &= ~VI_MOUNT;
338 vn_irflag_unset_locked(vporoot, VIRF_MOUNTPOINT);
339 vporoot->v_mountedhere = NULL;
341 mporoot->mnt_flag &= ~MNT_ROOTFS;
342 mporoot->mnt_vnodecovered = NULL;
345 /* Set up the new rootvnode, and purge the cache */
346 mpnroot->mnt_vnodecovered = NULL;
348 cache_purgevfs(rootvnode->v_mount);
350 if (mporoot != mpdevfs) {
351 /* Remount old root under /.mount or /mnt */
353 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
357 NDFREE(&nd, NDF_ONLY_PNBUF);
359 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
365 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
367 error = vinvalbuf(vp, V_SAVE, 0, 0);
371 mporoot->mnt_vnodecovered = vp;
372 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
373 vp->v_mountedhere = mporoot;
374 strlcpy(mporoot->mnt_stat.f_mntonname,
381 NDFREE(&nd, NDF_ONLY_PNBUF);
384 printf("mountroot: unable to remount previous root "
385 "under /.mount or /mnt (error %d)\n", error);
388 /* Remount devfs under /dev */
389 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
393 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
395 error = vinvalbuf(vp, V_SAVE, 0, 0);
397 vpdevfs = mpdevfs->mnt_vnodecovered;
398 if (vpdevfs != NULL) {
399 cache_purge(vpdevfs);
401 vn_irflag_unset_locked(vpdevfs, VIRF_MOUNTPOINT);
402 vpdevfs->v_mountedhere = NULL;
407 mpdevfs->mnt_vnodecovered = vp;
408 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
409 vp->v_mountedhere = mpdevfs;
416 printf("mountroot: unable to remount devfs under /dev "
417 "(error %d)\n", error);
418 NDFREE(&nd, NDF_ONLY_PNBUF);
420 if (mporoot == mpdevfs) {
422 /* Unlink the no longer needed /dev/dev -> / symlink */
423 error = kern_funlinkat(td, AT_FDCWD, "/dev/dev", FD_NONE,
426 printf("mountroot: unable to unlink /dev/dev "
427 "(error %d)\n", error);
432 * Configuration parser.
435 /* Parser character classes. */
436 #define CC_WHITESPACE -1
437 #define CC_NONWHITESPACE -2
444 parse_peek(char **conf)
451 parse_poke(char **conf, int c)
458 parse_advance(char **conf)
465 parse_skipto(char **conf, int mc)
470 c = parse_peek(conf);
475 match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
477 case CC_NONWHITESPACE:
480 match = (c != ' ' && c != '\t') ? 1 : 0;
483 match = (c == mc) ? 1 : 0;
494 parse_token(char **conf, char **tok)
501 error = parse_skipto(conf, CC_NONWHITESPACE);
505 error = parse_skipto(conf, CC_WHITESPACE);
507 *tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
513 parse_dir_ask_printenv(const char *var)
517 val = kern_getenv(var);
519 printf(" %s=%s\n", var, val);
525 parse_dir_ask(char **conf)
531 vfs_mountroot_wait();
533 printf("\nLoader variables:\n");
534 parse_dir_ask_printenv("vfs.root.mountfrom");
535 parse_dir_ask_printenv("vfs.root.mountfrom.options");
537 printf("\nManual root filesystem specification:\n");
538 printf(" <fstype>:<device> [options]\n");
539 printf(" Mount <device> using filesystem <fstype>\n");
540 printf(" and with the specified (optional) option list.\n");
542 printf(" eg. ufs:/dev/da0s1a\n");
543 printf(" zfs:zroot/ROOT/default\n");
544 printf(" cd9660:/dev/cd0 ro\n");
545 printf(" (which is equivalent to: ");
546 printf("mount -t cd9660 -o ro /dev/cd0 /)\n");
548 printf(" ? List valid disk boot devices\n");
549 printf(" . Yield 1 second (for background tasks)\n");
550 printf(" <empty line> Abort manual input\n");
554 printf("\nmountroot> ");
555 cngets(name, sizeof(name), GETS_ECHO);
558 if (name[0] == '?' && name[1] == '\0') {
559 printf("\nList of GEOM managed disk devices:\n ");
563 if (name[0] == '.' && name[1] == '\0') {
568 error = parse_mount(&mnt);
570 printf("Invalid file system specification.\n");
571 } while (error != 0);
577 parse_dir_md(char **conf)
581 struct md_ioctl *mdio;
588 error = parse_token(conf, &tok);
593 mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
594 path = (void *)(mdio + 1);
595 bcopy(tok, path, len);
598 /* Get file status. */
599 error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL);
603 /* Open /dev/mdctl so that we can attach/detach. */
604 error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
609 fd = td->td_retval[0];
610 mdio->md_version = MDIOVERSION;
611 mdio->md_type = MD_VNODE;
613 if (root_mount_mddev != -1) {
614 mdio->md_unit = root_mount_mddev;
615 (void)kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
616 /* Ignore errors. We don't care. */
617 root_mount_mddev = -1;
620 mdio->md_file = (void *)(mdio + 1);
621 mdio->md_options = MD_AUTOUNIT | MD_READONLY;
622 mdio->md_mediasize = sb.st_size;
624 error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
628 if (mdio->md_unit > 9) {
629 printf("rootmount: too many md units\n");
630 mdio->md_file = NULL;
631 mdio->md_options = 0;
632 mdio->md_mediasize = 0;
633 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
634 /* Ignore errors. We don't care. */
639 root_mount_mddev = mdio->md_unit;
640 printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
644 (void)kern_close(td, fd);
650 parse_dir_onfail(char **conf)
655 error = parse_token(conf, &action);
659 if (!strcmp(action, "continue"))
660 root_mount_onfail = A_CONTINUE;
661 else if (!strcmp(action, "panic"))
662 root_mount_onfail = A_PANIC;
663 else if (!strcmp(action, "reboot"))
664 root_mount_onfail = A_REBOOT;
665 else if (!strcmp(action, "retry"))
666 root_mount_onfail = A_RETRY;
668 printf("rootmount: %s: unknown action\n", action);
672 free(action, M_TEMP);
677 parse_dir_timeout(char **conf)
683 error = parse_token(conf, &tok);
687 secs = strtol(tok, &endtok, 0);
688 error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
690 root_mount_timeout = secs;
696 parse_directive(char **conf)
701 error = parse_token(conf, &dir);
705 if (strcmp(dir, ".ask") == 0)
706 error = parse_dir_ask(conf);
707 else if (strcmp(dir, ".md") == 0)
708 error = parse_dir_md(conf);
709 else if (strcmp(dir, ".onfail") == 0)
710 error = parse_dir_onfail(conf);
711 else if (strcmp(dir, ".timeout") == 0)
712 error = parse_dir_timeout(conf);
714 printf("mountroot: invalid directive `%s'\n", dir);
715 /* Ignore the rest of the line. */
716 (void)parse_skipto(conf, '\n');
724 parse_mount_dev_present(const char *dev)
729 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
733 NDFREE(&nd, NDF_ONLY_PNBUF);
734 return (error != 0) ? 0 : 1;
739 parse_mount(char **conf)
743 char *dev, *fs, *opts, *tok;
744 int delay, error, timeout;
746 error = parse_token(conf, &tok);
750 error = parse_skipto(&tok, ':');
755 parse_poke(&tok, '\0');
759 if (root_mount_mddev != -1) {
760 /* Handle substitution for the md unit number. */
761 tok = strstr(dev, "md#");
763 tok[2] = '0' + root_mount_mddev;
767 error = parse_token(conf, &tok);
768 opts = (error == 0) ? tok : NULL;
770 printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
771 (opts != NULL) ? opts : "");
773 errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
775 if (vfs_byname(fs) == NULL) {
776 strlcpy(errmsg, "unknown file system", ERRMSGL);
781 error = vfs_mountroot_wait_if_neccessary(fs, dev);
786 timeout = root_mount_timeout * hz;
790 ma = mount_arg(ma, "fstype", fs, -1);
791 ma = mount_arg(ma, "fspath", "/", -1);
792 ma = mount_arg(ma, "from", dev, -1);
793 ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
794 ma = mount_arg(ma, "ro", NULL, 0);
795 ma = parse_mountroot_options(ma, opts);
797 error = kernel_mount(ma, MNT_ROOTFS);
798 if (error == 0 || timeout <= 0)
801 if (root_mount_timeout * hz == timeout ||
802 (bootverbose && timeout % hz == 0)) {
803 printf("Mounting from %s:%s failed with error %d; "
804 "retrying for %d more second%s\n", fs, dev, error,
805 timeout / hz, (timeout / hz > 1) ? "s" : "");
807 pause("rmretry", delay);
812 printf("Mounting from %s:%s failed with error %d",
814 if (errmsg[0] != '\0')
815 printf(": %s", errmsg);
819 free(errmsg, M_TEMP);
822 /* kernel_mount can return -1 on error. */
823 return ((error < 0) ? EDOOFUS : error);
828 vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
834 root_mount_mddev = -1;
837 conf = sbuf_data(sb);
838 mp = TAILQ_NEXT(mpdevfs, mnt_list);
839 error = (mp == NULL) ? 0 : EDOOFUS;
840 root_mount_onfail = A_CONTINUE;
842 error = parse_skipto(&conf, CC_NONWHITESPACE);
843 if (error == PE_EOL) {
844 parse_advance(&conf);
849 switch (parse_peek(&conf)) {
851 error = parse_skipto(&conf, '\n');
854 error = parse_directive(&conf);
857 error = parse_mount(&conf);
859 printf("mountroot: invalid file system "
867 /* Ignore any trailing garbage on the line. */
868 if (parse_peek(&conf) != '\n') {
869 printf("mountroot: advancing to next directive...\n");
870 (void)parse_skipto(&conf, '\n');
872 mp = TAILQ_NEXT(mpdevfs, mnt_list);
878 * We failed to mount (a new) root.
880 switch (root_mount_onfail) {
884 panic("mountroot: unable to (re-)mount root.");
889 kern_reboot(RB_NOSYNC);
897 vfs_mountroot_conf0(struct sbuf *sb)
899 char *s, *tok, *mnt, *opt;
902 sbuf_printf(sb, ".onfail panic\n");
903 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
904 if (boothowto & RB_ASKNAME)
905 sbuf_printf(sb, ".ask\n");
907 if (boothowto & RB_DFLTROOT)
908 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
910 if (boothowto & RB_CDROM) {
911 sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
912 sbuf_printf(sb, ".timeout 0\n");
913 sbuf_printf(sb, "cd9660:/dev/cd1 ro\n");
914 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
916 s = kern_getenv("vfs.root.mountfrom");
918 opt = kern_getenv("vfs.root.mountfrom.options");
920 error = parse_token(&tok, &mnt);
922 sbuf_printf(sb, "%s %s\n", mnt,
923 (opt != NULL) ? opt : "");
925 error = parse_token(&tok, &mnt);
931 if (rootdevnames[0] != NULL)
932 sbuf_printf(sb, "%s\n", rootdevnames[0]);
933 if (rootdevnames[1] != NULL)
934 sbuf_printf(sb, "%s\n", rootdevnames[1]);
936 if (!(boothowto & RB_DFLTROOT))
937 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
939 if (!(boothowto & RB_ASKNAME))
940 sbuf_printf(sb, ".ask\n");
944 vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
946 static char buf[128];
950 int error, flags, len;
952 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
954 error = vn_open(&nd, &flags, 0, NULL);
958 NDFREE(&nd, NDF_ONLY_PNBUF);
960 len = sizeof(buf) - 1;
962 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
963 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
969 buf[len - resid] = 0;
970 sbuf_printf(sb, "%s", buf);
974 VOP_UNLOCK(nd.ni_vp);
975 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
980 vfs_mountroot_wait(void)
982 struct root_hold_token *h;
983 struct timeval lastfail;
992 mtx_lock(&root_holds_mtx);
993 if (TAILQ_EMPTY(&root_holds)) {
994 mtx_unlock(&root_holds_mtx);
997 if (ppsratecheck(&lastfail, &curfail, 1)) {
998 printf("Root mount waiting for:");
999 TAILQ_FOREACH(h, &root_holds, list)
1000 printf(" %s", h->who);
1003 TSWAIT("root mount");
1004 msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
1006 TSUNWAIT("root mount");
1013 vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev)
1018 * In case of ZFS and NFS we don't have a way to wait for
1019 * specific device. Also do the wait if the user forced that
1020 * behaviour by setting vfs.root_mount_always_wait=1.
1022 if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL ||
1023 dev[0] == '\0' || root_mount_always_wait != 0) {
1024 vfs_mountroot_wait();
1029 * Otherwise, no point in waiting if the device is already there.
1030 * Note that we must wait for GEOM to finish reconfiguring itself,
1031 * eg for geom_part(4) to finish tasting.
1034 if (parse_mount_dev_present(dev))
1038 * No luck. Let's wait. This code looks weird, but it's that way
1039 * to behave exactly as it used to work before.
1041 vfs_mountroot_wait();
1042 printf("mountroot: waiting for device %s...\n", dev);
1044 timeout = root_mount_timeout * hz;
1046 pause("rmdev", delay);
1048 } while (timeout > 0 && !parse_mount_dev_present(dev));
1065 mtx_assert(&Giant, MA_NOTOWNED);
1071 sb = sbuf_new_auto();
1072 vfs_mountroot_conf0(sb);
1075 error = vfs_mountroot_devfs(td, &mp);
1077 error = vfs_mountroot_parse(sb, mp);
1079 vfs_mountroot_shuffle(td, mp);
1081 error = vfs_mountroot_readconf(td, sb);
1089 * Iterate over all currently mounted file systems and use
1090 * the time stamp found to check and/or initialize the RTC.
1091 * Call inittodr() only once and pass it the largest of the
1092 * timestamps we encounter.
1095 mtx_lock(&mountlist_mtx);
1096 mp = TAILQ_FIRST(&mountlist);
1097 while (mp != NULL) {
1098 if (mp->mnt_time > timebase)
1099 timebase = mp->mnt_time;
1100 mp = TAILQ_NEXT(mp, mnt_list);
1102 mtx_unlock(&mountlist_mtx);
1105 /* Keep prison0's root in sync with the global rootvnode. */
1106 mtx_lock(&prison0.pr_mtx);
1107 prison0.pr_root = rootvnode;
1108 vref(prison0.pr_root);
1109 mtx_unlock(&prison0.pr_mtx);
1111 mtx_lock(&root_holds_mtx);
1112 atomic_store_rel_int(&root_mount_complete, 1);
1113 wakeup(&root_mount_complete);
1114 mtx_unlock(&root_holds_mtx);
1116 EVENTHANDLER_INVOKE(mountroot);
1121 static struct mntarg *
1122 parse_mountroot_options(struct mntarg *ma, const char *options)
1125 char *name, *name_arg;
1126 char *val, *val_arg;
1129 if (options == NULL || options[0] == '\0')
1132 p = opts = strdup(options, M_MOUNT);
1137 while((name = strsep(&p, ",")) != NULL) {
1138 if (name[0] == '\0')
1141 val = strchr(name, '=');
1146 if( strcmp(name, "rw") == 0 ||
1147 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);