1213365Smarcel/*-
2214006Smarcel * Copyright (c) 2010 Marcel Moolenaar
3213365Smarcel * Copyright (c) 1999-2004 Poul-Henning Kamp
4213365Smarcel * Copyright (c) 1999 Michael Smith
5213365Smarcel * Copyright (c) 1989, 1993
6213365Smarcel *      The Regents of the University of California.  All rights reserved.
7213365Smarcel * (c) UNIX System Laboratories, Inc.
8213365Smarcel * All or some portions of this file are derived from material licensed
9213365Smarcel * to the University of California by American Telephone and Telegraph
10213365Smarcel * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11213365Smarcel * the permission of UNIX System Laboratories, Inc.
12213365Smarcel *
13213365Smarcel * Redistribution and use in source and binary forms, with or without
14213365Smarcel * modification, are permitted provided that the following conditions
15213365Smarcel * are met:
16213365Smarcel * 1. Redistributions of source code must retain the above copyright
17213365Smarcel *    notice, this list of conditions and the following disclaimer.
18213365Smarcel * 2. Redistributions in binary form must reproduce the above copyright
19213365Smarcel *    notice, this list of conditions and the following disclaimer in the
20213365Smarcel *    documentation and/or other materials provided with the distribution.
21213365Smarcel * 4. Neither the name of the University nor the names of its contributors
22213365Smarcel *    may be used to endorse or promote products derived from this software
23213365Smarcel *    without specific prior written permission.
24213365Smarcel *
25213365Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26213365Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27213365Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28213365Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29213365Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30213365Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31213365Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32213365Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33213365Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34213365Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35213365Smarcel * SUCH DAMAGE.
36213365Smarcel */
37213365Smarcel
38214006Smarcel#include "opt_rootdevname.h"
39214006Smarcel
40213365Smarcel#include <sys/cdefs.h>
41213365Smarcel__FBSDID("$FreeBSD: releng/11.0/sys/kern/vfs_mountroot.c 299523 2016-05-12 07:38:10Z trasz $");
42213365Smarcel
43213365Smarcel#include <sys/param.h>
44213365Smarcel#include <sys/conf.h>
45228634Savg#include <sys/cons.h>
46213365Smarcel#include <sys/fcntl.h>
47213365Smarcel#include <sys/jail.h>
48213365Smarcel#include <sys/kernel.h>
49213365Smarcel#include <sys/malloc.h>
50214006Smarcel#include <sys/mdioctl.h>
51213365Smarcel#include <sys/mount.h>
52213365Smarcel#include <sys/mutex.h>
53213365Smarcel#include <sys/namei.h>
54213365Smarcel#include <sys/priv.h>
55213365Smarcel#include <sys/proc.h>
56213365Smarcel#include <sys/filedesc.h>
57213365Smarcel#include <sys/reboot.h>
58223919Sae#include <sys/sbuf.h>
59214006Smarcel#include <sys/stat.h>
60213365Smarcel#include <sys/syscallsubr.h>
61213365Smarcel#include <sys/sysproto.h>
62213365Smarcel#include <sys/sx.h>
63213365Smarcel#include <sys/sysctl.h>
64213365Smarcel#include <sys/sysent.h>
65213365Smarcel#include <sys/systm.h>
66213365Smarcel#include <sys/vnode.h>
67213365Smarcel
68213365Smarcel#include <geom/geom.h>
69213365Smarcel
70213365Smarcel/*
71213365Smarcel * The root filesystem is detailed in the kernel environment variable
72213365Smarcel * vfs.root.mountfrom, which is expected to be in the general format
73213365Smarcel *
74213365Smarcel * <vfsname>:[<path>][	<vfsname>:[<path>] ...]
75213365Smarcel * vfsname   := the name of a VFS known to the kernel and capable
76213365Smarcel *              of being mounted as root
77213365Smarcel * path      := disk device name or other data used by the filesystem
78213365Smarcel *              to locate its physical store
79213365Smarcel *
80213365Smarcel * If the environment variable vfs.root.mountfrom is a space separated list,
81213365Smarcel * each list element is tried in turn and the root filesystem will be mounted
82298819Spfg * from the first one that succeeds.
83213365Smarcel *
84213365Smarcel * The environment variable vfs.root.mountfrom.options is a comma delimited
85213365Smarcel * set of string mount options.  These mount options must be parseable
86213365Smarcel * by nmount() in the kernel.
87213365Smarcel */
88213365Smarcel
89214006Smarcelstatic int parse_mount(char **);
90214006Smarcelstatic struct mntarg *parse_mountroot_options(struct mntarg *, const char *);
91290197Straszstatic int sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS);
92297190Straszstatic void vfs_mountroot_wait(void);
93290196Straszstatic int vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev);
94214006Smarcel
95213365Smarcel/*
96214006Smarcel * The vnode of the system's root (/ in the filesystem, without chroot
97214006Smarcel * active.)
98213365Smarcel */
99214006Smarcelstruct vnode *rootvnode;
100213365Smarcel
101287107Strasz/*
102287107Strasz * Mount of the system's /dev.
103287107Strasz */
104287107Straszstruct mount *rootdevmp;
105287107Strasz
106214006Smarcelchar *rootdevnames[2] = {NULL, NULL};
107213365Smarcel
108267351Smavstruct mtx root_holds_mtx;
109267351SmavMTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
110267351Smav
111213365Smarcelstruct root_hold_token {
112213365Smarcel	const char			*who;
113213365Smarcel	LIST_ENTRY(root_hold_token)	list;
114213365Smarcel};
115213365Smarcel
116213365Smarcelstatic LIST_HEAD(, root_hold_token)	root_holds =
117213365Smarcel    LIST_HEAD_INITIALIZER(root_holds);
118213365Smarcel
119214006Smarcelenum action {
120214006Smarcel	A_CONTINUE,
121214006Smarcel	A_PANIC,
122214006Smarcel	A_REBOOT,
123214006Smarcel	A_RETRY
124214006Smarcel};
125214006Smarcel
126214006Smarcelstatic enum action root_mount_onfail = A_CONTINUE;
127214006Smarcel
128214006Smarcelstatic int root_mount_mddev;
129213365Smarcelstatic int root_mount_complete;
130213365Smarcel
131214006Smarcel/* By default wait up to 3 seconds for devices to appear. */
132214006Smarcelstatic int root_mount_timeout = 3;
133253910SmarcelTUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
134214006Smarcel
135290197StraszSYSCTL_PROC(_vfs, OID_AUTO, root_mount_hold,
136290197Strasz    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
137290197Strasz    NULL, 0, sysctl_vfs_root_mount_hold, "A",
138290197Strasz    "List of root mount hold tokens");
139290197Strasz
140290197Straszstatic int
141290197Straszsysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS)
142290197Strasz{
143290197Strasz	struct sbuf sb;
144290197Strasz	struct root_hold_token *h;
145290197Strasz	int error;
146290197Strasz
147290197Strasz	sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
148290197Strasz
149290197Strasz	mtx_lock(&root_holds_mtx);
150290197Strasz	LIST_FOREACH(h, &root_holds, list) {
151290197Strasz		if (h != LIST_FIRST(&root_holds))
152290197Strasz			sbuf_putc(&sb, ' ');
153290197Strasz		sbuf_printf(&sb, "%s", h->who);
154290197Strasz	}
155290197Strasz	mtx_unlock(&root_holds_mtx);
156290197Strasz
157290197Strasz	error = sbuf_finish(&sb);
158290197Strasz	if (error == 0)
159290197Strasz		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
160290197Strasz	sbuf_delete(&sb);
161290197Strasz	return (error);
162290197Strasz}
163290197Strasz
164213365Smarcelstruct root_hold_token *
165213365Smarcelroot_mount_hold(const char *identifier)
166213365Smarcel{
167213365Smarcel	struct root_hold_token *h;
168213365Smarcel
169213365Smarcel	if (root_mounted())
170213365Smarcel		return (NULL);
171213365Smarcel
172213365Smarcel	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
173213365Smarcel	h->who = identifier;
174267351Smav	mtx_lock(&root_holds_mtx);
175213365Smarcel	LIST_INSERT_HEAD(&root_holds, h, list);
176267351Smav	mtx_unlock(&root_holds_mtx);
177213365Smarcel	return (h);
178213365Smarcel}
179213365Smarcel
180213365Smarcelvoid
181213365Smarcelroot_mount_rel(struct root_hold_token *h)
182213365Smarcel{
183213365Smarcel
184213365Smarcel	if (h == NULL)
185213365Smarcel		return;
186267351Smav	mtx_lock(&root_holds_mtx);
187213365Smarcel	LIST_REMOVE(h, list);
188213365Smarcel	wakeup(&root_holds);
189267351Smav	mtx_unlock(&root_holds_mtx);
190213365Smarcel	free(h, M_DEVBUF);
191213365Smarcel}
192213365Smarcel
193213365Smarcelint
194213365Smarcelroot_mounted(void)
195213365Smarcel{
196213365Smarcel
197213365Smarcel	/* No mutex is acquired here because int stores are atomic. */
198213365Smarcel	return (root_mount_complete);
199213365Smarcel}
200213365Smarcel
201213365Smarcelstatic void
202213365Smarcelset_rootvnode(void)
203213365Smarcel{
204213365Smarcel	struct proc *p;
205213365Smarcel
206213365Smarcel	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
207213365Smarcel		panic("Cannot find root vnode");
208213365Smarcel
209213365Smarcel	VOP_UNLOCK(rootvnode, 0);
210213365Smarcel
211213365Smarcel	p = curthread->td_proc;
212213365Smarcel	FILEDESC_XLOCK(p->p_fd);
213213365Smarcel
214213365Smarcel	if (p->p_fd->fd_cdir != NULL)
215213365Smarcel		vrele(p->p_fd->fd_cdir);
216213365Smarcel	p->p_fd->fd_cdir = rootvnode;
217213365Smarcel	VREF(rootvnode);
218213365Smarcel
219213365Smarcel	if (p->p_fd->fd_rdir != NULL)
220213365Smarcel		vrele(p->p_fd->fd_rdir);
221213365Smarcel	p->p_fd->fd_rdir = rootvnode;
222213365Smarcel	VREF(rootvnode);
223213365Smarcel
224213365Smarcel	FILEDESC_XUNLOCK(p->p_fd);
225213365Smarcel}
226213365Smarcel
227214006Smarcelstatic int
228214006Smarcelvfs_mountroot_devfs(struct thread *td, struct mount **mpp)
229213365Smarcel{
230213365Smarcel	struct vfsoptlist *opts;
231213365Smarcel	struct vfsconf *vfsp;
232214006Smarcel	struct mount *mp;
233213365Smarcel	int error;
234213365Smarcel
235214006Smarcel	*mpp = NULL;
236214006Smarcel
237287964Strasz	if (rootdevmp != NULL) {
238287964Strasz		/*
239287964Strasz		 * Already have /dev; this happens during rerooting.
240287964Strasz		 */
241287964Strasz		error = vfs_busy(rootdevmp, 0);
242287964Strasz		if (error != 0)
243287964Strasz			return (error);
244287964Strasz		*mpp = rootdevmp;
245287964Strasz	} else {
246287964Strasz		vfsp = vfs_byname("devfs");
247287964Strasz		KASSERT(vfsp != NULL, ("Could not find devfs by name"));
248287964Strasz		if (vfsp == NULL)
249287964Strasz			return (ENOENT);
250213365Smarcel
251287964Strasz		mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
252213365Smarcel
253287964Strasz		error = VFS_MOUNT(mp);
254287964Strasz		KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
255287964Strasz		if (error)
256287964Strasz			return (error);
257213365Smarcel
258287964Strasz		opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
259287964Strasz		TAILQ_INIT(opts);
260287964Strasz		mp->mnt_opt = opts;
261213365Smarcel
262287964Strasz		mtx_lock(&mountlist_mtx);
263287964Strasz		TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
264287964Strasz		mtx_unlock(&mountlist_mtx);
265213365Smarcel
266287964Strasz		*mpp = mp;
267287964Strasz		rootdevmp = mp;
268287964Strasz	}
269287964Strasz
270213365Smarcel	set_rootvnode();
271213365Smarcel
272274476Skib	error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
273213365Smarcel	if (error)
274213365Smarcel		printf("kern_symlink /dev -> / returns %d\n", error);
275214006Smarcel
276214006Smarcel	return (error);
277213365Smarcel}
278213365Smarcel
279288091Sbdrewerystatic void
280214006Smarcelvfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
281213365Smarcel{
282213365Smarcel	struct nameidata nd;
283214006Smarcel	struct mount *mporoot, *mpnroot;
284214006Smarcel	struct vnode *vp, *vporoot, *vpdevfs;
285214006Smarcel	char *fspath;
286213365Smarcel	int error;
287213365Smarcel
288214006Smarcel	mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
289214006Smarcel
290214006Smarcel	/* Shuffle the mountlist. */
291213365Smarcel	mtx_lock(&mountlist_mtx);
292214006Smarcel	mporoot = TAILQ_FIRST(&mountlist);
293214006Smarcel	TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
294214006Smarcel	if (mporoot != mpdevfs) {
295214006Smarcel		TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
296214006Smarcel		TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
297214006Smarcel	}
298214006Smarcel	TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
299213365Smarcel	mtx_unlock(&mountlist_mtx);
300213365Smarcel
301214006Smarcel	cache_purgevfs(mporoot);
302214006Smarcel	if (mporoot != mpdevfs)
303214006Smarcel		cache_purgevfs(mpdevfs);
304213365Smarcel
305214006Smarcel	VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot);
306214006Smarcel
307214006Smarcel	VI_LOCK(vporoot);
308214006Smarcel	vporoot->v_iflag &= ~VI_MOUNT;
309214006Smarcel	VI_UNLOCK(vporoot);
310214006Smarcel	vporoot->v_mountedhere = NULL;
311214006Smarcel	mporoot->mnt_flag &= ~MNT_ROOTFS;
312214006Smarcel	mporoot->mnt_vnodecovered = NULL;
313214006Smarcel	vput(vporoot);
314214006Smarcel
315214006Smarcel	/* Set up the new rootvnode, and purge the cache */
316214006Smarcel	mpnroot->mnt_vnodecovered = NULL;
317213365Smarcel	set_rootvnode();
318213365Smarcel	cache_purgevfs(rootvnode->v_mount);
319213365Smarcel
320214006Smarcel	if (mporoot != mpdevfs) {
321214006Smarcel		/* Remount old root under /.mount or /mnt */
322214006Smarcel		fspath = "/.mount";
323214006Smarcel		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
324214006Smarcel		    fspath, td);
325214006Smarcel		error = namei(&nd);
326214006Smarcel		if (error) {
327214006Smarcel			NDFREE(&nd, NDF_ONLY_PNBUF);
328214006Smarcel			fspath = "/mnt";
329214006Smarcel			NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
330214006Smarcel			    fspath, td);
331214006Smarcel			error = namei(&nd);
332214006Smarcel		}
333214006Smarcel		if (!error) {
334214006Smarcel			vp = nd.ni_vp;
335214006Smarcel			error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
336214006Smarcel			if (!error)
337214006Smarcel				error = vinvalbuf(vp, V_SAVE, 0, 0);
338214006Smarcel			if (!error) {
339214006Smarcel				cache_purge(vp);
340214006Smarcel				mporoot->mnt_vnodecovered = vp;
341214006Smarcel				vp->v_mountedhere = mporoot;
342214006Smarcel				strlcpy(mporoot->mnt_stat.f_mntonname,
343214006Smarcel				    fspath, MNAMELEN);
344214006Smarcel				VOP_UNLOCK(vp, 0);
345214006Smarcel			} else
346214006Smarcel				vput(vp);
347214006Smarcel		}
348214006Smarcel		NDFREE(&nd, NDF_ONLY_PNBUF);
349214006Smarcel
350299523Strasz		if (error)
351214006Smarcel			printf("mountroot: unable to remount previous root "
352299523Strasz			    "under /.mount or /mnt (error %d)\n", error);
353214006Smarcel	}
354214006Smarcel
355214006Smarcel	/* Remount devfs under /dev */
356213365Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
357213365Smarcel	error = namei(&nd);
358214006Smarcel	if (!error) {
359214006Smarcel		vp = nd.ni_vp;
360214006Smarcel		error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
361214006Smarcel		if (!error)
362214006Smarcel			error = vinvalbuf(vp, V_SAVE, 0, 0);
363214006Smarcel		if (!error) {
364214006Smarcel			vpdevfs = mpdevfs->mnt_vnodecovered;
365214006Smarcel			if (vpdevfs != NULL) {
366214006Smarcel				cache_purge(vpdevfs);
367214006Smarcel				vpdevfs->v_mountedhere = NULL;
368214006Smarcel				vrele(vpdevfs);
369214006Smarcel			}
370214006Smarcel			mpdevfs->mnt_vnodecovered = vp;
371214006Smarcel			vp->v_mountedhere = mpdevfs;
372214006Smarcel			VOP_UNLOCK(vp, 0);
373214006Smarcel		} else
374214006Smarcel			vput(vp);
375213365Smarcel	}
376299523Strasz	if (error)
377214006Smarcel		printf("mountroot: unable to remount devfs under /dev "
378299523Strasz		    "(error %d)\n", error);
379213365Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
380214006Smarcel
381214006Smarcel	if (mporoot == mpdevfs) {
382214006Smarcel		vfs_unbusy(mpdevfs);
383214006Smarcel		/* Unlink the no longer needed /dev/dev -> / symlink */
384274476Skib		error = kern_unlinkat(td, AT_FDCWD, "/dev/dev",
385274476Skib		    UIO_SYSSPACE, 0);
386299523Strasz		if (error)
387214006Smarcel			printf("mountroot: unable to unlink /dev/dev "
388214006Smarcel			    "(error %d)\n", error);
389213365Smarcel	}
390214006Smarcel}
391214006Smarcel
392214006Smarcel/*
393214006Smarcel * Configuration parser.
394214006Smarcel */
395214006Smarcel
396214006Smarcel/* Parser character classes. */
397214006Smarcel#define	CC_WHITESPACE		-1
398214006Smarcel#define	CC_NONWHITESPACE	-2
399214006Smarcel
400214006Smarcel/* Parse errors. */
401214006Smarcel#define	PE_EOF			-1
402214006Smarcel#define	PE_EOL			-2
403214006Smarcel
404214006Smarcelstatic __inline int
405214006Smarcelparse_peek(char **conf)
406214006Smarcel{
407214006Smarcel
408214006Smarcel	return (**conf);
409214006Smarcel}
410214006Smarcel
411214006Smarcelstatic __inline void
412214006Smarcelparse_poke(char **conf, int c)
413214006Smarcel{
414214006Smarcel
415214006Smarcel	**conf = c;
416214006Smarcel}
417214006Smarcel
418214006Smarcelstatic __inline void
419214006Smarcelparse_advance(char **conf)
420214006Smarcel{
421214006Smarcel
422214006Smarcel	(*conf)++;
423214006Smarcel}
424214006Smarcel
425214006Smarcelstatic int
426214006Smarcelparse_skipto(char **conf, int mc)
427214006Smarcel{
428214006Smarcel	int c, match;
429214006Smarcel
430214006Smarcel	while (1) {
431214006Smarcel		c = parse_peek(conf);
432214006Smarcel		if (c == 0)
433214006Smarcel			return (PE_EOF);
434214006Smarcel		switch (mc) {
435214006Smarcel		case CC_WHITESPACE:
436214006Smarcel			match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
437214006Smarcel			break;
438214006Smarcel		case CC_NONWHITESPACE:
439214006Smarcel			if (c == '\n')
440214006Smarcel				return (PE_EOL);
441214006Smarcel			match = (c != ' ' && c != '\t') ? 1 : 0;
442214006Smarcel			break;
443214006Smarcel		default:
444214006Smarcel			match = (c == mc) ? 1 : 0;
445214006Smarcel			break;
446214006Smarcel		}
447214006Smarcel		if (match)
448214006Smarcel			break;
449214006Smarcel		parse_advance(conf);
450213365Smarcel	}
451214006Smarcel	return (0);
452214006Smarcel}
453213365Smarcel
454214006Smarcelstatic int
455214006Smarcelparse_token(char **conf, char **tok)
456214006Smarcel{
457214006Smarcel	char *p;
458214006Smarcel	size_t len;
459214006Smarcel	int error;
460214006Smarcel
461214006Smarcel	*tok = NULL;
462214006Smarcel	error = parse_skipto(conf, CC_NONWHITESPACE);
463213365Smarcel	if (error)
464214006Smarcel		return (error);
465214006Smarcel	p = *conf;
466214006Smarcel	error = parse_skipto(conf, CC_WHITESPACE);
467214006Smarcel	len = *conf - p;
468214006Smarcel	*tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
469214006Smarcel	bcopy(p, *tok, len);
470214006Smarcel	return (0);
471213365Smarcel}
472213365Smarcel
473214006Smarcelstatic void
474214006Smarcelparse_dir_ask_printenv(const char *var)
475213365Smarcel{
476214006Smarcel	char *val;
477213365Smarcel
478273174Sdavide	val = kern_getenv(var);
479214006Smarcel	if (val != NULL) {
480214006Smarcel		printf("  %s=%s\n", var, val);
481214006Smarcel		freeenv(val);
482214006Smarcel	}
483214006Smarcel}
484213365Smarcel
485214006Smarcelstatic int
486214006Smarcelparse_dir_ask(char **conf)
487214006Smarcel{
488214006Smarcel	char name[80];
489214006Smarcel	char *mnt;
490214006Smarcel	int error;
491213365Smarcel
492297190Strasz	vfs_mountroot_wait();
493297190Strasz
494214006Smarcel	printf("\nLoader variables:\n");
495214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom");
496214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom.options");
497213365Smarcel
498214006Smarcel	printf("\nManual root filesystem specification:\n");
499214006Smarcel	printf("  <fstype>:<device> [options]\n");
500214006Smarcel	printf("      Mount <device> using filesystem <fstype>\n");
501214006Smarcel	printf("      and with the specified (optional) option list.\n");
502214006Smarcel	printf("\n");
503214006Smarcel	printf("    eg. ufs:/dev/da0s1a\n");
504214006Smarcel	printf("        zfs:tank\n");
505289449Sngie	printf("        cd9660:/dev/cd0 ro\n");
506214006Smarcel	printf("          (which is equivalent to: ");
507289449Sngie	printf("mount -t cd9660 -o ro /dev/cd0 /)\n");
508214006Smarcel	printf("\n");
509214006Smarcel	printf("  ?               List valid disk boot devices\n");
510214006Smarcel	printf("  .               Yield 1 second (for background tasks)\n");
511214006Smarcel	printf("  <empty line>    Abort manual input\n");
512214006Smarcel
513226673Smarcel	do {
514226673Smarcel		error = EINVAL;
515226673Smarcel		printf("\nmountroot> ");
516228634Savg		cngets(name, sizeof(name), GETS_ECHO);
517226673Smarcel		if (name[0] == '\0')
518226673Smarcel			break;
519226673Smarcel		if (name[0] == '?' && name[1] == '\0') {
520226673Smarcel			printf("\nList of GEOM managed disk devices:\n  ");
521226673Smarcel			g_dev_print();
522226673Smarcel			continue;
523226673Smarcel		}
524226673Smarcel		if (name[0] == '.' && name[1] == '\0') {
525226673Smarcel			pause("rmask", hz);
526226673Smarcel			continue;
527226673Smarcel		}
528226673Smarcel		mnt = name;
529226673Smarcel		error = parse_mount(&mnt);
530226673Smarcel		if (error == -1)
531226673Smarcel			printf("Invalid file system specification.\n");
532226673Smarcel	} while (error != 0);
533226673Smarcel
534214006Smarcel	return (error);
535214006Smarcel}
536213365Smarcel
537214006Smarcelstatic int
538214006Smarcelparse_dir_md(char **conf)
539214006Smarcel{
540214006Smarcel	struct stat sb;
541214006Smarcel	struct thread *td;
542214006Smarcel	struct md_ioctl *mdio;
543214006Smarcel	char *path, *tok;
544214006Smarcel	int error, fd, len;
545213365Smarcel
546214006Smarcel	td = curthread;
547214006Smarcel
548214006Smarcel	error = parse_token(conf, &tok);
549214006Smarcel	if (error)
550214006Smarcel		return (error);
551214006Smarcel
552214006Smarcel	len = strlen(tok);
553214006Smarcel	mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
554214006Smarcel	path = (void *)(mdio + 1);
555214006Smarcel	bcopy(tok, path, len);
556214006Smarcel	free(tok, M_TEMP);
557214006Smarcel
558214006Smarcel	/* Get file status. */
559274476Skib	error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL);
560214006Smarcel	if (error)
561214006Smarcel		goto out;
562214006Smarcel
563214006Smarcel	/* Open /dev/mdctl so that we can attach/detach. */
564274476Skib	error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
565274476Skib	    O_RDWR, 0);
566214006Smarcel	if (error)
567214006Smarcel		goto out;
568214006Smarcel
569214006Smarcel	fd = td->td_retval[0];
570214006Smarcel	mdio->md_version = MDIOVERSION;
571214006Smarcel	mdio->md_type = MD_VNODE;
572214006Smarcel
573214006Smarcel	if (root_mount_mddev != -1) {
574214006Smarcel		mdio->md_unit = root_mount_mddev;
575214006Smarcel		DROP_GIANT();
576214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
577214006Smarcel		PICKUP_GIANT();
578214006Smarcel		/* Ignore errors. We don't care. */
579214006Smarcel		root_mount_mddev = -1;
580214006Smarcel	}
581214006Smarcel
582214006Smarcel	mdio->md_file = (void *)(mdio + 1);
583214006Smarcel	mdio->md_options = MD_AUTOUNIT | MD_READONLY;
584214006Smarcel	mdio->md_mediasize = sb.st_size;
585214006Smarcel	mdio->md_unit = 0;
586214006Smarcel	DROP_GIANT();
587214006Smarcel	error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
588214006Smarcel	PICKUP_GIANT();
589214006Smarcel	if (error)
590214006Smarcel		goto out;
591214006Smarcel
592214006Smarcel	if (mdio->md_unit > 9) {
593214006Smarcel		printf("rootmount: too many md units\n");
594214006Smarcel		mdio->md_file = NULL;
595214006Smarcel		mdio->md_options = 0;
596214006Smarcel		mdio->md_mediasize = 0;
597214006Smarcel		DROP_GIANT();
598214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
599214006Smarcel		PICKUP_GIANT();
600214006Smarcel		/* Ignore errors. We don't care. */
601214006Smarcel		error = ERANGE;
602214006Smarcel		goto out;
603214006Smarcel	}
604214006Smarcel
605214006Smarcel	root_mount_mddev = mdio->md_unit;
606214006Smarcel	printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
607214006Smarcel
608214006Smarcel	error = kern_close(td, fd);
609214006Smarcel
610214006Smarcel out:
611214006Smarcel	free(mdio, M_TEMP);
612214006Smarcel	return (error);
613214006Smarcel}
614214006Smarcel
615214006Smarcelstatic int
616214006Smarcelparse_dir_onfail(char **conf)
617214006Smarcel{
618214006Smarcel	char *action;
619214006Smarcel	int error;
620214006Smarcel
621214006Smarcel	error = parse_token(conf, &action);
622214006Smarcel	if (error)
623214006Smarcel		return (error);
624214006Smarcel
625214006Smarcel	if (!strcmp(action, "continue"))
626214006Smarcel		root_mount_onfail = A_CONTINUE;
627214006Smarcel	else if (!strcmp(action, "panic"))
628214006Smarcel		root_mount_onfail = A_PANIC;
629214006Smarcel	else if (!strcmp(action, "reboot"))
630214006Smarcel		root_mount_onfail = A_REBOOT;
631214006Smarcel	else if (!strcmp(action, "retry"))
632214006Smarcel		root_mount_onfail = A_RETRY;
633214006Smarcel	else {
634214006Smarcel		printf("rootmount: %s: unknown action\n", action);
635214006Smarcel		error = EINVAL;
636214006Smarcel	}
637214006Smarcel
638214006Smarcel	free(action, M_TEMP);
639214006Smarcel	return (0);
640214006Smarcel}
641214006Smarcel
642214006Smarcelstatic int
643214006Smarcelparse_dir_timeout(char **conf)
644214006Smarcel{
645214006Smarcel	char *tok, *endtok;
646214006Smarcel	long secs;
647214006Smarcel	int error;
648214006Smarcel
649214006Smarcel	error = parse_token(conf, &tok);
650214006Smarcel	if (error)
651214006Smarcel		return (error);
652214006Smarcel
653214006Smarcel	secs = strtol(tok, &endtok, 0);
654214006Smarcel	error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
655214006Smarcel	if (!error)
656214006Smarcel		root_mount_timeout = secs;
657214006Smarcel	free(tok, M_TEMP);
658214006Smarcel	return (error);
659214006Smarcel}
660214006Smarcel
661214006Smarcelstatic int
662214006Smarcelparse_directive(char **conf)
663214006Smarcel{
664214006Smarcel	char *dir;
665214006Smarcel	int error;
666214006Smarcel
667214006Smarcel	error = parse_token(conf, &dir);
668214006Smarcel	if (error)
669214006Smarcel		return (error);
670214006Smarcel
671214006Smarcel	if (strcmp(dir, ".ask") == 0)
672214006Smarcel		error = parse_dir_ask(conf);
673214006Smarcel	else if (strcmp(dir, ".md") == 0)
674214006Smarcel		error = parse_dir_md(conf);
675214006Smarcel	else if (strcmp(dir, ".onfail") == 0)
676214006Smarcel		error = parse_dir_onfail(conf);
677214006Smarcel	else if (strcmp(dir, ".timeout") == 0)
678214006Smarcel		error = parse_dir_timeout(conf);
679214006Smarcel	else {
680214006Smarcel		printf("mountroot: invalid directive `%s'\n", dir);
681214006Smarcel		/* Ignore the rest of the line. */
682214006Smarcel		(void)parse_skipto(conf, '\n');
683214006Smarcel		error = EINVAL;
684214006Smarcel	}
685214006Smarcel	free(dir, M_TEMP);
686214006Smarcel	return (error);
687214006Smarcel}
688214006Smarcel
689214006Smarcelstatic int
690214006Smarcelparse_mount_dev_present(const char *dev)
691214006Smarcel{
692214006Smarcel	struct nameidata nd;
693214006Smarcel	int error;
694214006Smarcel
695214006Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
696214006Smarcel	error = namei(&nd);
697214006Smarcel	if (!error)
698214006Smarcel		vput(nd.ni_vp);
699214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
700214006Smarcel	return (error != 0) ? 0 : 1;
701214006Smarcel}
702214006Smarcel
703243868Skib#define	ERRMSGL	255
704214006Smarcelstatic int
705214006Smarcelparse_mount(char **conf)
706214006Smarcel{
707243868Skib	char *errmsg;
708214006Smarcel	struct mntarg *ma;
709214006Smarcel	char *dev, *fs, *opts, *tok;
710290196Strasz	int error;
711214006Smarcel
712214006Smarcel	error = parse_token(conf, &tok);
713214006Smarcel	if (error)
714214006Smarcel		return (error);
715214006Smarcel	fs = tok;
716214006Smarcel	error = parse_skipto(&tok, ':');
717214006Smarcel	if (error) {
718214006Smarcel		free(fs, M_TEMP);
719214006Smarcel		return (error);
720214006Smarcel	}
721214006Smarcel	parse_poke(&tok, '\0');
722214006Smarcel	parse_advance(&tok);
723214006Smarcel	dev = tok;
724214006Smarcel
725214006Smarcel	if (root_mount_mddev != -1) {
726214006Smarcel		/* Handle substitution for the md unit number. */
727214006Smarcel		tok = strstr(dev, "md#");
728214006Smarcel		if (tok != NULL)
729214006Smarcel			tok[2] = '0' + root_mount_mddev;
730214006Smarcel	}
731214006Smarcel
732214006Smarcel	/* Parse options. */
733214006Smarcel	error = parse_token(conf, &tok);
734214006Smarcel	opts = (error == 0) ? tok : NULL;
735214006Smarcel
736214006Smarcel	printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
737214006Smarcel	    (opts != NULL) ? opts : "");
738214006Smarcel
739243868Skib	errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
740214006Smarcel
741214006Smarcel	if (vfs_byname(fs) == NULL) {
742255412Sdelphij		strlcpy(errmsg, "unknown file system", ERRMSGL);
743214006Smarcel		error = ENOENT;
744214006Smarcel		goto out;
745214006Smarcel	}
746214006Smarcel
747290196Strasz	error = vfs_mountroot_wait_if_neccessary(fs, dev);
748290196Strasz	if (error != 0)
749290196Strasz		goto out;
750214006Smarcel
751214006Smarcel	ma = NULL;
752214006Smarcel	ma = mount_arg(ma, "fstype", fs, -1);
753214006Smarcel	ma = mount_arg(ma, "fspath", "/", -1);
754214006Smarcel	ma = mount_arg(ma, "from", dev, -1);
755243868Skib	ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
756214006Smarcel	ma = mount_arg(ma, "ro", NULL, 0);
757214006Smarcel	ma = parse_mountroot_options(ma, opts);
758214006Smarcel	error = kernel_mount(ma, MNT_ROOTFS);
759214006Smarcel
760214006Smarcel out:
761214006Smarcel	if (error) {
762214006Smarcel		printf("Mounting from %s:%s failed with error %d",
763214006Smarcel		    fs, dev, error);
764214006Smarcel		if (errmsg[0] != '\0')
765214006Smarcel			printf(": %s", errmsg);
766214006Smarcel		printf(".\n");
767214006Smarcel	}
768214006Smarcel	free(fs, M_TEMP);
769243868Skib	free(errmsg, M_TEMP);
770214006Smarcel	if (opts != NULL)
771214006Smarcel		free(opts, M_TEMP);
772214006Smarcel	/* kernel_mount can return -1 on error. */
773214006Smarcel	return ((error < 0) ? EDOOFUS : error);
774214006Smarcel}
775243868Skib#undef ERRMSGL
776214006Smarcel
777214006Smarcelstatic int
778214006Smarcelvfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
779214006Smarcel{
780214006Smarcel	struct mount *mp;
781214006Smarcel	char *conf;
782214006Smarcel	int error;
783214006Smarcel
784214006Smarcel	root_mount_mddev = -1;
785214006Smarcel
786214006Smarcelretry:
787214006Smarcel	conf = sbuf_data(sb);
788214006Smarcel	mp = TAILQ_NEXT(mpdevfs, mnt_list);
789214006Smarcel	error = (mp == NULL) ? 0 : EDOOFUS;
790214006Smarcel	root_mount_onfail = A_CONTINUE;
791214006Smarcel	while (mp == NULL) {
792214006Smarcel		error = parse_skipto(&conf, CC_NONWHITESPACE);
793214006Smarcel		if (error == PE_EOL) {
794214006Smarcel			parse_advance(&conf);
795214006Smarcel			continue;
796214006Smarcel		}
797214006Smarcel		if (error < 0)
798214006Smarcel			break;
799214006Smarcel		switch (parse_peek(&conf)) {
800214006Smarcel		case '#':
801214006Smarcel			error = parse_skipto(&conf, '\n');
802214006Smarcel			break;
803214006Smarcel		case '.':
804214006Smarcel			error = parse_directive(&conf);
805214006Smarcel			break;
806214006Smarcel		default:
807214006Smarcel			error = parse_mount(&conf);
808287190Smarcel			if (error == -1) {
809287190Smarcel				printf("mountroot: invalid file system "
810287190Smarcel				    "specification.\n");
811287190Smarcel				error = 0;
812287190Smarcel			}
813214006Smarcel			break;
814214006Smarcel		}
815214006Smarcel		if (error < 0)
816214006Smarcel			break;
817214006Smarcel		/* Ignore any trailing garbage on the line. */
818214006Smarcel		if (parse_peek(&conf) != '\n') {
819214006Smarcel			printf("mountroot: advancing to next directive...\n");
820214006Smarcel			(void)parse_skipto(&conf, '\n');
821214006Smarcel		}
822214006Smarcel		mp = TAILQ_NEXT(mpdevfs, mnt_list);
823214006Smarcel	}
824214006Smarcel	if (mp != NULL)
825214006Smarcel		return (0);
826214006Smarcel
827213365Smarcel	/*
828214006Smarcel	 * We failed to mount (a new) root.
829213365Smarcel	 */
830214006Smarcel	switch (root_mount_onfail) {
831214006Smarcel	case A_CONTINUE:
832214006Smarcel		break;
833214006Smarcel	case A_PANIC:
834214006Smarcel		panic("mountroot: unable to (re-)mount root.");
835214006Smarcel		/* NOTREACHED */
836214006Smarcel	case A_RETRY:
837214006Smarcel		goto retry;
838214006Smarcel	case A_REBOOT:
839214006Smarcel		kern_reboot(RB_NOSYNC);
840214006Smarcel		/* NOTREACHED */
841213365Smarcel	}
842213365Smarcel
843214006Smarcel	return (error);
844214006Smarcel}
845214006Smarcel
846214006Smarcelstatic void
847214006Smarcelvfs_mountroot_conf0(struct sbuf *sb)
848214006Smarcel{
849214006Smarcel	char *s, *tok, *mnt, *opt;
850214006Smarcel	int error;
851214006Smarcel
852214006Smarcel	sbuf_printf(sb, ".onfail panic\n");
853214006Smarcel	sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
854214006Smarcel	if (boothowto & RB_ASKNAME)
855214006Smarcel		sbuf_printf(sb, ".ask\n");
856214006Smarcel#ifdef ROOTDEVNAME
857214006Smarcel	if (boothowto & RB_DFLTROOT)
858214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
859214006Smarcel#endif
860213365Smarcel	if (boothowto & RB_CDROM) {
861217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
862214006Smarcel		sbuf_printf(sb, ".timeout 0\n");
863289449Sngie		sbuf_printf(sb, "cd9660:/dev/cd1 ro\n");
864214006Smarcel		sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
865214006Smarcel	}
866273174Sdavide	s = kern_getenv("vfs.root.mountfrom");
867214006Smarcel	if (s != NULL) {
868273174Sdavide		opt = kern_getenv("vfs.root.mountfrom.options");
869214006Smarcel		tok = s;
870214006Smarcel		error = parse_token(&tok, &mnt);
871214006Smarcel		while (!error) {
872214006Smarcel			sbuf_printf(sb, "%s %s\n", mnt,
873214006Smarcel			    (opt != NULL) ? opt : "");
874214006Smarcel			free(mnt, M_TEMP);
875214006Smarcel			error = parse_token(&tok, &mnt);
876213365Smarcel		}
877214006Smarcel		if (opt != NULL)
878214006Smarcel			freeenv(opt);
879214006Smarcel		freeenv(s);
880213365Smarcel	}
881214006Smarcel	if (rootdevnames[0] != NULL)
882214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[0]);
883214006Smarcel	if (rootdevnames[1] != NULL)
884214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[1]);
885214006Smarcel#ifdef ROOTDEVNAME
886214006Smarcel	if (!(boothowto & RB_DFLTROOT))
887214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
888214006Smarcel#endif
889214006Smarcel	if (!(boothowto & RB_ASKNAME))
890214006Smarcel		sbuf_printf(sb, ".ask\n");
891214006Smarcel}
892213365Smarcel
893214006Smarcelstatic int
894214006Smarcelvfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
895214006Smarcel{
896214006Smarcel	static char buf[128];
897214006Smarcel	struct nameidata nd;
898214006Smarcel	off_t ofs;
899231949Skib	ssize_t resid;
900241896Skib	int error, flags, len;
901214006Smarcel
902241896Skib	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
903214006Smarcel	flags = FREAD;
904214006Smarcel	error = vn_open(&nd, &flags, 0, NULL);
905214006Smarcel	if (error)
906214006Smarcel		return (error);
907214006Smarcel
908214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
909214006Smarcel	ofs = 0;
910214006Smarcel	len = sizeof(buf) - 1;
911214006Smarcel	while (1) {
912214006Smarcel		error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
913214006Smarcel		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
914214006Smarcel		    NOCRED, &resid, td);
915214006Smarcel		if (error)
916214006Smarcel			break;
917214006Smarcel		if (resid == len)
918214006Smarcel			break;
919214006Smarcel		buf[len - resid] = 0;
920214006Smarcel		sbuf_printf(sb, "%s", buf);
921214006Smarcel		ofs += len - resid;
922214006Smarcel	}
923214006Smarcel
924214006Smarcel	VOP_UNLOCK(nd.ni_vp, 0);
925214006Smarcel	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
926214006Smarcel	return (error);
927214006Smarcel}
928214006Smarcel
929214006Smarcelstatic void
930214006Smarcelvfs_mountroot_wait(void)
931214006Smarcel{
932214006Smarcel	struct root_hold_token *h;
933214006Smarcel	struct timeval lastfail;
934214006Smarcel	int curfail;
935214006Smarcel
936214006Smarcel	curfail = 0;
937214006Smarcel	while (1) {
938214006Smarcel		DROP_GIANT();
939214006Smarcel		g_waitidle();
940214006Smarcel		PICKUP_GIANT();
941267351Smav		mtx_lock(&root_holds_mtx);
942214006Smarcel		if (LIST_EMPTY(&root_holds)) {
943267351Smav			mtx_unlock(&root_holds_mtx);
944214006Smarcel			break;
945214006Smarcel		}
946214006Smarcel		if (ppsratecheck(&lastfail, &curfail, 1)) {
947214006Smarcel			printf("Root mount waiting for:");
948214006Smarcel			LIST_FOREACH(h, &root_holds, list)
949214006Smarcel				printf(" %s", h->who);
950214006Smarcel			printf("\n");
951214006Smarcel		}
952267351Smav		msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
953214006Smarcel		    hz);
954214006Smarcel	}
955214006Smarcel}
956214006Smarcel
957290196Straszstatic int
958290196Straszvfs_mountroot_wait_if_neccessary(const char *fs, const char *dev)
959290196Strasz{
960290196Strasz	int delay, timeout;
961290196Strasz
962290196Strasz	/*
963290196Strasz	 * In case of ZFS and NFS we don't have a way to wait for
964290196Strasz	 * specific device.
965290196Strasz	 */
966290196Strasz	if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL ||
967290196Strasz	    dev[0] == '\0') {
968290196Strasz		vfs_mountroot_wait();
969290196Strasz		return (0);
970290196Strasz	}
971290196Strasz
972290196Strasz	/*
973290196Strasz	 * Otherwise, no point in waiting if the device is already there.
974290196Strasz	 * Note that we must wait for GEOM to finish reconfiguring itself,
975290196Strasz	 * eg for geom_part(4) to finish tasting.
976290196Strasz	 */
977290196Strasz	DROP_GIANT();
978290196Strasz	g_waitidle();
979290196Strasz	PICKUP_GIANT();
980290196Strasz	if (parse_mount_dev_present(dev))
981290196Strasz		return (0);
982290196Strasz
983290196Strasz	/*
984290196Strasz	 * No luck.  Let's wait.  This code looks weird, but it's that way
985290196Strasz	 * to behave exactly as it used to work before.
986290196Strasz	 */
987290196Strasz	vfs_mountroot_wait();
988290196Strasz	printf("mountroot: waiting for device %s...\n", dev);
989290196Strasz	delay = hz / 10;
990290196Strasz	timeout = root_mount_timeout * hz;
991290196Strasz	do {
992290196Strasz		pause("rmdev", delay);
993290196Strasz		timeout -= delay;
994290196Strasz	} while (timeout > 0 && !parse_mount_dev_present(dev));
995290196Strasz
996290196Strasz	if (timeout <= 0)
997290196Strasz		return (ENODEV);
998290196Strasz
999290196Strasz	return (0);
1000290196Strasz}
1001290196Strasz
1002214006Smarcelvoid
1003214006Smarcelvfs_mountroot(void)
1004214006Smarcel{
1005214006Smarcel	struct mount *mp;
1006214006Smarcel	struct sbuf *sb;
1007214006Smarcel	struct thread *td;
1008214006Smarcel	time_t timebase;
1009214006Smarcel	int error;
1010214006Smarcel
1011214006Smarcel	td = curthread;
1012214006Smarcel
1013214006Smarcel	sb = sbuf_new_auto();
1014214006Smarcel	vfs_mountroot_conf0(sb);
1015214006Smarcel	sbuf_finish(sb);
1016214006Smarcel
1017214006Smarcel	error = vfs_mountroot_devfs(td, &mp);
1018214006Smarcel	while (!error) {
1019214006Smarcel		error = vfs_mountroot_parse(sb, mp);
1020214006Smarcel		if (!error) {
1021288091Sbdrewery			vfs_mountroot_shuffle(td, mp);
1022288091Sbdrewery			sbuf_clear(sb);
1023288091Sbdrewery			error = vfs_mountroot_readconf(td, sb);
1024288091Sbdrewery			sbuf_finish(sb);
1025213365Smarcel		}
1026213365Smarcel	}
1027213365Smarcel
1028214006Smarcel	sbuf_delete(sb);
1029213365Smarcel
1030213365Smarcel	/*
1031214006Smarcel	 * Iterate over all currently mounted file systems and use
1032214006Smarcel	 * the time stamp found to check and/or initialize the RTC.
1033214006Smarcel	 * Call inittodr() only once and pass it the largest of the
1034214006Smarcel	 * timestamps we encounter.
1035213365Smarcel	 */
1036214006Smarcel	timebase = 0;
1037214006Smarcel	mtx_lock(&mountlist_mtx);
1038214006Smarcel	mp = TAILQ_FIRST(&mountlist);
1039214006Smarcel	while (mp != NULL) {
1040214006Smarcel		if (mp->mnt_time > timebase)
1041214006Smarcel			timebase = mp->mnt_time;
1042214006Smarcel		mp = TAILQ_NEXT(mp, mnt_list);
1043214006Smarcel	}
1044214006Smarcel	mtx_unlock(&mountlist_mtx);
1045214006Smarcel	inittodr(timebase);
1046213365Smarcel
1047214006Smarcel	/* Keep prison0's root in sync with the global rootvnode. */
1048214006Smarcel	mtx_lock(&prison0.pr_mtx);
1049214006Smarcel	prison0.pr_root = rootvnode;
1050214006Smarcel	vref(prison0.pr_root);
1051214006Smarcel	mtx_unlock(&prison0.pr_mtx);
1052213365Smarcel
1053267351Smav	mtx_lock(&root_holds_mtx);
1054214006Smarcel	atomic_store_rel_int(&root_mount_complete, 1);
1055214006Smarcel	wakeup(&root_mount_complete);
1056267351Smav	mtx_unlock(&root_holds_mtx);
1057248645Savg
1058248645Savg	EVENTHANDLER_INVOKE(mountroot);
1059213365Smarcel}
1060213365Smarcel
1061213365Smarcelstatic struct mntarg *
1062213365Smarcelparse_mountroot_options(struct mntarg *ma, const char *options)
1063213365Smarcel{
1064213365Smarcel	char *p;
1065213365Smarcel	char *name, *name_arg;
1066213365Smarcel	char *val, *val_arg;
1067213365Smarcel	char *opts;
1068213365Smarcel
1069213365Smarcel	if (options == NULL || options[0] == '\0')
1070213365Smarcel		return (ma);
1071213365Smarcel
1072213365Smarcel	p = opts = strdup(options, M_MOUNT);
1073213365Smarcel	if (opts == NULL) {
1074213365Smarcel		return (ma);
1075214006Smarcel	}
1076213365Smarcel
1077213365Smarcel	while((name = strsep(&p, ",")) != NULL) {
1078213365Smarcel		if (name[0] == '\0')
1079213365Smarcel			break;
1080213365Smarcel
1081213365Smarcel		val = strchr(name, '=');
1082213365Smarcel		if (val != NULL) {
1083213365Smarcel			*val = '\0';
1084213365Smarcel			++val;
1085213365Smarcel		}
1086213365Smarcel		if( strcmp(name, "rw") == 0 ||
1087213365Smarcel		    strcmp(name, "noro") == 0) {
1088213365Smarcel			/*
1089213365Smarcel			 * The first time we mount the root file system,
1090213365Smarcel			 * we need to mount 'ro', so We need to ignore
1091213365Smarcel			 * 'rw' and 'noro' mount options.
1092213365Smarcel			 */
1093213365Smarcel			continue;
1094213365Smarcel		}
1095213365Smarcel		name_arg = strdup(name, M_MOUNT);
1096213365Smarcel		val_arg = NULL;
1097214006Smarcel		if (val != NULL)
1098213365Smarcel			val_arg = strdup(val, M_MOUNT);
1099213365Smarcel
1100213365Smarcel		ma = mount_arg(ma, name_arg, val_arg,
1101213365Smarcel		    (val_arg != NULL ? -1 : 0));
1102213365Smarcel	}
1103213365Smarcel	free(opts, M_MOUNT);
1104213365Smarcel	return (ma);
1105213365Smarcel}
1106