vfs_mountroot.c revision 288091
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: head/sys/kern/vfs_mountroot.c 288091 2015-09-22 03:34:07Z bdrewery $");
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
82213365Smarcel * from the first one that suceeds.
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 *);
91214006Smarcel
92213365Smarcel/*
93214006Smarcel * The vnode of the system's root (/ in the filesystem, without chroot
94214006Smarcel * active.)
95213365Smarcel */
96214006Smarcelstruct vnode *rootvnode;
97213365Smarcel
98287107Strasz/*
99287107Strasz * Mount of the system's /dev.
100287107Strasz */
101287107Straszstruct mount *rootdevmp;
102287107Strasz
103214006Smarcelchar *rootdevnames[2] = {NULL, NULL};
104213365Smarcel
105267351Smavstruct mtx root_holds_mtx;
106267351SmavMTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
107267351Smav
108213365Smarcelstruct root_hold_token {
109213365Smarcel	const char			*who;
110213365Smarcel	LIST_ENTRY(root_hold_token)	list;
111213365Smarcel};
112213365Smarcel
113213365Smarcelstatic LIST_HEAD(, root_hold_token)	root_holds =
114213365Smarcel    LIST_HEAD_INITIALIZER(root_holds);
115213365Smarcel
116214006Smarcelenum action {
117214006Smarcel	A_CONTINUE,
118214006Smarcel	A_PANIC,
119214006Smarcel	A_REBOOT,
120214006Smarcel	A_RETRY
121214006Smarcel};
122214006Smarcel
123214006Smarcelstatic enum action root_mount_onfail = A_CONTINUE;
124214006Smarcel
125214006Smarcelstatic int root_mount_mddev;
126213365Smarcelstatic int root_mount_complete;
127213365Smarcel
128214006Smarcel/* By default wait up to 3 seconds for devices to appear. */
129214006Smarcelstatic int root_mount_timeout = 3;
130253910SmarcelTUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
131214006Smarcel
132213365Smarcelstruct root_hold_token *
133213365Smarcelroot_mount_hold(const char *identifier)
134213365Smarcel{
135213365Smarcel	struct root_hold_token *h;
136213365Smarcel
137213365Smarcel	if (root_mounted())
138213365Smarcel		return (NULL);
139213365Smarcel
140213365Smarcel	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
141213365Smarcel	h->who = identifier;
142267351Smav	mtx_lock(&root_holds_mtx);
143213365Smarcel	LIST_INSERT_HEAD(&root_holds, h, list);
144267351Smav	mtx_unlock(&root_holds_mtx);
145213365Smarcel	return (h);
146213365Smarcel}
147213365Smarcel
148213365Smarcelvoid
149213365Smarcelroot_mount_rel(struct root_hold_token *h)
150213365Smarcel{
151213365Smarcel
152213365Smarcel	if (h == NULL)
153213365Smarcel		return;
154267351Smav	mtx_lock(&root_holds_mtx);
155213365Smarcel	LIST_REMOVE(h, list);
156213365Smarcel	wakeup(&root_holds);
157267351Smav	mtx_unlock(&root_holds_mtx);
158213365Smarcel	free(h, M_DEVBUF);
159213365Smarcel}
160213365Smarcel
161213365Smarcelint
162213365Smarcelroot_mounted(void)
163213365Smarcel{
164213365Smarcel
165213365Smarcel	/* No mutex is acquired here because int stores are atomic. */
166213365Smarcel	return (root_mount_complete);
167213365Smarcel}
168213365Smarcel
169213365Smarcelvoid
170213365Smarcelroot_mount_wait(void)
171213365Smarcel{
172213365Smarcel
173213365Smarcel	/*
174213365Smarcel	 * Panic on an obvious deadlock - the function can't be called from
175213365Smarcel	 * a thread which is doing the whole SYSINIT stuff.
176213365Smarcel	 */
177213365Smarcel	KASSERT(curthread->td_proc->p_pid != 0,
178213365Smarcel	    ("root_mount_wait: cannot be called from the swapper thread"));
179267351Smav	mtx_lock(&root_holds_mtx);
180213365Smarcel	while (!root_mount_complete) {
181267351Smav		msleep(&root_mount_complete, &root_holds_mtx, PZERO, "rootwait",
182213365Smarcel		    hz);
183213365Smarcel	}
184267351Smav	mtx_unlock(&root_holds_mtx);
185213365Smarcel}
186213365Smarcel
187213365Smarcelstatic void
188213365Smarcelset_rootvnode(void)
189213365Smarcel{
190213365Smarcel	struct proc *p;
191213365Smarcel
192213365Smarcel	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
193213365Smarcel		panic("Cannot find root vnode");
194213365Smarcel
195213365Smarcel	VOP_UNLOCK(rootvnode, 0);
196213365Smarcel
197213365Smarcel	p = curthread->td_proc;
198213365Smarcel	FILEDESC_XLOCK(p->p_fd);
199213365Smarcel
200213365Smarcel	if (p->p_fd->fd_cdir != NULL)
201213365Smarcel		vrele(p->p_fd->fd_cdir);
202213365Smarcel	p->p_fd->fd_cdir = rootvnode;
203213365Smarcel	VREF(rootvnode);
204213365Smarcel
205213365Smarcel	if (p->p_fd->fd_rdir != NULL)
206213365Smarcel		vrele(p->p_fd->fd_rdir);
207213365Smarcel	p->p_fd->fd_rdir = rootvnode;
208213365Smarcel	VREF(rootvnode);
209213365Smarcel
210213365Smarcel	FILEDESC_XUNLOCK(p->p_fd);
211213365Smarcel}
212213365Smarcel
213214006Smarcelstatic int
214214006Smarcelvfs_mountroot_devfs(struct thread *td, struct mount **mpp)
215213365Smarcel{
216213365Smarcel	struct vfsoptlist *opts;
217213365Smarcel	struct vfsconf *vfsp;
218214006Smarcel	struct mount *mp;
219213365Smarcel	int error;
220213365Smarcel
221214006Smarcel	*mpp = NULL;
222214006Smarcel
223287964Strasz	if (rootdevmp != NULL) {
224287964Strasz		/*
225287964Strasz		 * Already have /dev; this happens during rerooting.
226287964Strasz		 */
227287964Strasz		error = vfs_busy(rootdevmp, 0);
228287964Strasz		if (error != 0)
229287964Strasz			return (error);
230287964Strasz		*mpp = rootdevmp;
231287964Strasz	} else {
232287964Strasz		vfsp = vfs_byname("devfs");
233287964Strasz		KASSERT(vfsp != NULL, ("Could not find devfs by name"));
234287964Strasz		if (vfsp == NULL)
235287964Strasz			return (ENOENT);
236213365Smarcel
237287964Strasz		mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
238213365Smarcel
239287964Strasz		error = VFS_MOUNT(mp);
240287964Strasz		KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
241287964Strasz		if (error)
242287964Strasz			return (error);
243213365Smarcel
244287964Strasz		opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
245287964Strasz		TAILQ_INIT(opts);
246287964Strasz		mp->mnt_opt = opts;
247213365Smarcel
248287964Strasz		mtx_lock(&mountlist_mtx);
249287964Strasz		TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
250287964Strasz		mtx_unlock(&mountlist_mtx);
251213365Smarcel
252287964Strasz		*mpp = mp;
253287964Strasz		rootdevmp = mp;
254287964Strasz	}
255287964Strasz
256213365Smarcel	set_rootvnode();
257213365Smarcel
258274476Skib	error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
259213365Smarcel	if (error)
260213365Smarcel		printf("kern_symlink /dev -> / returns %d\n", error);
261214006Smarcel
262214006Smarcel	return (error);
263213365Smarcel}
264213365Smarcel
265288091Sbdrewerystatic void
266214006Smarcelvfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
267213365Smarcel{
268213365Smarcel	struct nameidata nd;
269214006Smarcel	struct mount *mporoot, *mpnroot;
270214006Smarcel	struct vnode *vp, *vporoot, *vpdevfs;
271214006Smarcel	char *fspath;
272213365Smarcel	int error;
273213365Smarcel
274214006Smarcel	mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
275214006Smarcel
276214006Smarcel	/* Shuffle the mountlist. */
277213365Smarcel	mtx_lock(&mountlist_mtx);
278214006Smarcel	mporoot = TAILQ_FIRST(&mountlist);
279214006Smarcel	TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
280214006Smarcel	if (mporoot != mpdevfs) {
281214006Smarcel		TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
282214006Smarcel		TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
283214006Smarcel	}
284214006Smarcel	TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
285213365Smarcel	mtx_unlock(&mountlist_mtx);
286213365Smarcel
287214006Smarcel	cache_purgevfs(mporoot);
288214006Smarcel	if (mporoot != mpdevfs)
289214006Smarcel		cache_purgevfs(mpdevfs);
290213365Smarcel
291214006Smarcel	VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot);
292214006Smarcel
293214006Smarcel	VI_LOCK(vporoot);
294214006Smarcel	vporoot->v_iflag &= ~VI_MOUNT;
295214006Smarcel	VI_UNLOCK(vporoot);
296214006Smarcel	vporoot->v_mountedhere = NULL;
297214006Smarcel	mporoot->mnt_flag &= ~MNT_ROOTFS;
298214006Smarcel	mporoot->mnt_vnodecovered = NULL;
299214006Smarcel	vput(vporoot);
300214006Smarcel
301214006Smarcel	/* Set up the new rootvnode, and purge the cache */
302214006Smarcel	mpnroot->mnt_vnodecovered = NULL;
303213365Smarcel	set_rootvnode();
304213365Smarcel	cache_purgevfs(rootvnode->v_mount);
305213365Smarcel
306214006Smarcel	if (mporoot != mpdevfs) {
307214006Smarcel		/* Remount old root under /.mount or /mnt */
308214006Smarcel		fspath = "/.mount";
309214006Smarcel		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
310214006Smarcel		    fspath, td);
311214006Smarcel		error = namei(&nd);
312214006Smarcel		if (error) {
313214006Smarcel			NDFREE(&nd, NDF_ONLY_PNBUF);
314214006Smarcel			fspath = "/mnt";
315214006Smarcel			NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
316214006Smarcel			    fspath, td);
317214006Smarcel			error = namei(&nd);
318214006Smarcel		}
319214006Smarcel		if (!error) {
320214006Smarcel			vp = nd.ni_vp;
321214006Smarcel			error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
322214006Smarcel			if (!error)
323214006Smarcel				error = vinvalbuf(vp, V_SAVE, 0, 0);
324214006Smarcel			if (!error) {
325214006Smarcel				cache_purge(vp);
326214006Smarcel				mporoot->mnt_vnodecovered = vp;
327214006Smarcel				vp->v_mountedhere = mporoot;
328214006Smarcel				strlcpy(mporoot->mnt_stat.f_mntonname,
329214006Smarcel				    fspath, MNAMELEN);
330214006Smarcel				VOP_UNLOCK(vp, 0);
331214006Smarcel			} else
332214006Smarcel				vput(vp);
333214006Smarcel		}
334214006Smarcel		NDFREE(&nd, NDF_ONLY_PNBUF);
335214006Smarcel
336214006Smarcel		if (error && bootverbose)
337214006Smarcel			printf("mountroot: unable to remount previous root "
338214006Smarcel			    "under /.mount or /mnt (error %d).\n", error);
339214006Smarcel	}
340214006Smarcel
341214006Smarcel	/* Remount devfs under /dev */
342213365Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
343213365Smarcel	error = namei(&nd);
344214006Smarcel	if (!error) {
345214006Smarcel		vp = nd.ni_vp;
346214006Smarcel		error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
347214006Smarcel		if (!error)
348214006Smarcel			error = vinvalbuf(vp, V_SAVE, 0, 0);
349214006Smarcel		if (!error) {
350214006Smarcel			vpdevfs = mpdevfs->mnt_vnodecovered;
351214006Smarcel			if (vpdevfs != NULL) {
352214006Smarcel				cache_purge(vpdevfs);
353214006Smarcel				vpdevfs->v_mountedhere = NULL;
354214006Smarcel				vrele(vpdevfs);
355214006Smarcel			}
356214006Smarcel			mpdevfs->mnt_vnodecovered = vp;
357214006Smarcel			vp->v_mountedhere = mpdevfs;
358214006Smarcel			VOP_UNLOCK(vp, 0);
359214006Smarcel		} else
360214006Smarcel			vput(vp);
361213365Smarcel	}
362214006Smarcel	if (error && bootverbose)
363214006Smarcel		printf("mountroot: unable to remount devfs under /dev "
364214006Smarcel		    "(error %d).\n", error);
365213365Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
366214006Smarcel
367214006Smarcel	if (mporoot == mpdevfs) {
368214006Smarcel		vfs_unbusy(mpdevfs);
369214006Smarcel		/* Unlink the no longer needed /dev/dev -> / symlink */
370274476Skib		error = kern_unlinkat(td, AT_FDCWD, "/dev/dev",
371274476Skib		    UIO_SYSSPACE, 0);
372214006Smarcel		if (error && bootverbose)
373214006Smarcel			printf("mountroot: unable to unlink /dev/dev "
374214006Smarcel			    "(error %d)\n", error);
375213365Smarcel	}
376214006Smarcel}
377214006Smarcel
378214006Smarcel/*
379214006Smarcel * Configuration parser.
380214006Smarcel */
381214006Smarcel
382214006Smarcel/* Parser character classes. */
383214006Smarcel#define	CC_WHITESPACE		-1
384214006Smarcel#define	CC_NONWHITESPACE	-2
385214006Smarcel
386214006Smarcel/* Parse errors. */
387214006Smarcel#define	PE_EOF			-1
388214006Smarcel#define	PE_EOL			-2
389214006Smarcel
390214006Smarcelstatic __inline int
391214006Smarcelparse_peek(char **conf)
392214006Smarcel{
393214006Smarcel
394214006Smarcel	return (**conf);
395214006Smarcel}
396214006Smarcel
397214006Smarcelstatic __inline void
398214006Smarcelparse_poke(char **conf, int c)
399214006Smarcel{
400214006Smarcel
401214006Smarcel	**conf = c;
402214006Smarcel}
403214006Smarcel
404214006Smarcelstatic __inline void
405214006Smarcelparse_advance(char **conf)
406214006Smarcel{
407214006Smarcel
408214006Smarcel	(*conf)++;
409214006Smarcel}
410214006Smarcel
411214006Smarcelstatic int
412214006Smarcelparse_skipto(char **conf, int mc)
413214006Smarcel{
414214006Smarcel	int c, match;
415214006Smarcel
416214006Smarcel	while (1) {
417214006Smarcel		c = parse_peek(conf);
418214006Smarcel		if (c == 0)
419214006Smarcel			return (PE_EOF);
420214006Smarcel		switch (mc) {
421214006Smarcel		case CC_WHITESPACE:
422214006Smarcel			match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
423214006Smarcel			break;
424214006Smarcel		case CC_NONWHITESPACE:
425214006Smarcel			if (c == '\n')
426214006Smarcel				return (PE_EOL);
427214006Smarcel			match = (c != ' ' && c != '\t') ? 1 : 0;
428214006Smarcel			break;
429214006Smarcel		default:
430214006Smarcel			match = (c == mc) ? 1 : 0;
431214006Smarcel			break;
432214006Smarcel		}
433214006Smarcel		if (match)
434214006Smarcel			break;
435214006Smarcel		parse_advance(conf);
436213365Smarcel	}
437214006Smarcel	return (0);
438214006Smarcel}
439213365Smarcel
440214006Smarcelstatic int
441214006Smarcelparse_token(char **conf, char **tok)
442214006Smarcel{
443214006Smarcel	char *p;
444214006Smarcel	size_t len;
445214006Smarcel	int error;
446214006Smarcel
447214006Smarcel	*tok = NULL;
448214006Smarcel	error = parse_skipto(conf, CC_NONWHITESPACE);
449213365Smarcel	if (error)
450214006Smarcel		return (error);
451214006Smarcel	p = *conf;
452214006Smarcel	error = parse_skipto(conf, CC_WHITESPACE);
453214006Smarcel	len = *conf - p;
454214006Smarcel	*tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
455214006Smarcel	bcopy(p, *tok, len);
456214006Smarcel	return (0);
457213365Smarcel}
458213365Smarcel
459214006Smarcelstatic void
460214006Smarcelparse_dir_ask_printenv(const char *var)
461213365Smarcel{
462214006Smarcel	char *val;
463213365Smarcel
464273174Sdavide	val = kern_getenv(var);
465214006Smarcel	if (val != NULL) {
466214006Smarcel		printf("  %s=%s\n", var, val);
467214006Smarcel		freeenv(val);
468214006Smarcel	}
469214006Smarcel}
470213365Smarcel
471214006Smarcelstatic int
472214006Smarcelparse_dir_ask(char **conf)
473214006Smarcel{
474214006Smarcel	char name[80];
475214006Smarcel	char *mnt;
476214006Smarcel	int error;
477213365Smarcel
478214006Smarcel	printf("\nLoader variables:\n");
479214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom");
480214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom.options");
481213365Smarcel
482214006Smarcel	printf("\nManual root filesystem specification:\n");
483214006Smarcel	printf("  <fstype>:<device> [options]\n");
484214006Smarcel	printf("      Mount <device> using filesystem <fstype>\n");
485214006Smarcel	printf("      and with the specified (optional) option list.\n");
486214006Smarcel	printf("\n");
487214006Smarcel	printf("    eg. ufs:/dev/da0s1a\n");
488214006Smarcel	printf("        zfs:tank\n");
489214006Smarcel	printf("        cd9660:/dev/acd0 ro\n");
490214006Smarcel	printf("          (which is equivalent to: ");
491214006Smarcel	printf("mount -t cd9660 -o ro /dev/acd0 /)\n");
492214006Smarcel	printf("\n");
493214006Smarcel	printf("  ?               List valid disk boot devices\n");
494214006Smarcel	printf("  .               Yield 1 second (for background tasks)\n");
495214006Smarcel	printf("  <empty line>    Abort manual input\n");
496214006Smarcel
497226673Smarcel	do {
498226673Smarcel		error = EINVAL;
499226673Smarcel		printf("\nmountroot> ");
500228634Savg		cngets(name, sizeof(name), GETS_ECHO);
501226673Smarcel		if (name[0] == '\0')
502226673Smarcel			break;
503226673Smarcel		if (name[0] == '?' && name[1] == '\0') {
504226673Smarcel			printf("\nList of GEOM managed disk devices:\n  ");
505226673Smarcel			g_dev_print();
506226673Smarcel			continue;
507226673Smarcel		}
508226673Smarcel		if (name[0] == '.' && name[1] == '\0') {
509226673Smarcel			pause("rmask", hz);
510226673Smarcel			continue;
511226673Smarcel		}
512226673Smarcel		mnt = name;
513226673Smarcel		error = parse_mount(&mnt);
514226673Smarcel		if (error == -1)
515226673Smarcel			printf("Invalid file system specification.\n");
516226673Smarcel	} while (error != 0);
517226673Smarcel
518214006Smarcel	return (error);
519214006Smarcel}
520213365Smarcel
521214006Smarcelstatic int
522214006Smarcelparse_dir_md(char **conf)
523214006Smarcel{
524214006Smarcel	struct stat sb;
525214006Smarcel	struct thread *td;
526214006Smarcel	struct md_ioctl *mdio;
527214006Smarcel	char *path, *tok;
528214006Smarcel	int error, fd, len;
529213365Smarcel
530214006Smarcel	td = curthread;
531214006Smarcel
532214006Smarcel	error = parse_token(conf, &tok);
533214006Smarcel	if (error)
534214006Smarcel		return (error);
535214006Smarcel
536214006Smarcel	len = strlen(tok);
537214006Smarcel	mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
538214006Smarcel	path = (void *)(mdio + 1);
539214006Smarcel	bcopy(tok, path, len);
540214006Smarcel	free(tok, M_TEMP);
541214006Smarcel
542214006Smarcel	/* Get file status. */
543274476Skib	error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL);
544214006Smarcel	if (error)
545214006Smarcel		goto out;
546214006Smarcel
547214006Smarcel	/* Open /dev/mdctl so that we can attach/detach. */
548274476Skib	error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
549274476Skib	    O_RDWR, 0);
550214006Smarcel	if (error)
551214006Smarcel		goto out;
552214006Smarcel
553214006Smarcel	fd = td->td_retval[0];
554214006Smarcel	mdio->md_version = MDIOVERSION;
555214006Smarcel	mdio->md_type = MD_VNODE;
556214006Smarcel
557214006Smarcel	if (root_mount_mddev != -1) {
558214006Smarcel		mdio->md_unit = root_mount_mddev;
559214006Smarcel		DROP_GIANT();
560214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
561214006Smarcel		PICKUP_GIANT();
562214006Smarcel		/* Ignore errors. We don't care. */
563214006Smarcel		root_mount_mddev = -1;
564214006Smarcel	}
565214006Smarcel
566214006Smarcel	mdio->md_file = (void *)(mdio + 1);
567214006Smarcel	mdio->md_options = MD_AUTOUNIT | MD_READONLY;
568214006Smarcel	mdio->md_mediasize = sb.st_size;
569214006Smarcel	mdio->md_unit = 0;
570214006Smarcel	DROP_GIANT();
571214006Smarcel	error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
572214006Smarcel	PICKUP_GIANT();
573214006Smarcel	if (error)
574214006Smarcel		goto out;
575214006Smarcel
576214006Smarcel	if (mdio->md_unit > 9) {
577214006Smarcel		printf("rootmount: too many md units\n");
578214006Smarcel		mdio->md_file = NULL;
579214006Smarcel		mdio->md_options = 0;
580214006Smarcel		mdio->md_mediasize = 0;
581214006Smarcel		DROP_GIANT();
582214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
583214006Smarcel		PICKUP_GIANT();
584214006Smarcel		/* Ignore errors. We don't care. */
585214006Smarcel		error = ERANGE;
586214006Smarcel		goto out;
587214006Smarcel	}
588214006Smarcel
589214006Smarcel	root_mount_mddev = mdio->md_unit;
590214006Smarcel	printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
591214006Smarcel
592214006Smarcel	error = kern_close(td, fd);
593214006Smarcel
594214006Smarcel out:
595214006Smarcel	free(mdio, M_TEMP);
596214006Smarcel	return (error);
597214006Smarcel}
598214006Smarcel
599214006Smarcelstatic int
600214006Smarcelparse_dir_onfail(char **conf)
601214006Smarcel{
602214006Smarcel	char *action;
603214006Smarcel	int error;
604214006Smarcel
605214006Smarcel	error = parse_token(conf, &action);
606214006Smarcel	if (error)
607214006Smarcel		return (error);
608214006Smarcel
609214006Smarcel	if (!strcmp(action, "continue"))
610214006Smarcel		root_mount_onfail = A_CONTINUE;
611214006Smarcel	else if (!strcmp(action, "panic"))
612214006Smarcel		root_mount_onfail = A_PANIC;
613214006Smarcel	else if (!strcmp(action, "reboot"))
614214006Smarcel		root_mount_onfail = A_REBOOT;
615214006Smarcel	else if (!strcmp(action, "retry"))
616214006Smarcel		root_mount_onfail = A_RETRY;
617214006Smarcel	else {
618214006Smarcel		printf("rootmount: %s: unknown action\n", action);
619214006Smarcel		error = EINVAL;
620214006Smarcel	}
621214006Smarcel
622214006Smarcel	free(action, M_TEMP);
623214006Smarcel	return (0);
624214006Smarcel}
625214006Smarcel
626214006Smarcelstatic int
627214006Smarcelparse_dir_timeout(char **conf)
628214006Smarcel{
629214006Smarcel	char *tok, *endtok;
630214006Smarcel	long secs;
631214006Smarcel	int error;
632214006Smarcel
633214006Smarcel	error = parse_token(conf, &tok);
634214006Smarcel	if (error)
635214006Smarcel		return (error);
636214006Smarcel
637214006Smarcel	secs = strtol(tok, &endtok, 0);
638214006Smarcel	error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
639214006Smarcel	if (!error)
640214006Smarcel		root_mount_timeout = secs;
641214006Smarcel	free(tok, M_TEMP);
642214006Smarcel	return (error);
643214006Smarcel}
644214006Smarcel
645214006Smarcelstatic int
646214006Smarcelparse_directive(char **conf)
647214006Smarcel{
648214006Smarcel	char *dir;
649214006Smarcel	int error;
650214006Smarcel
651214006Smarcel	error = parse_token(conf, &dir);
652214006Smarcel	if (error)
653214006Smarcel		return (error);
654214006Smarcel
655214006Smarcel	if (strcmp(dir, ".ask") == 0)
656214006Smarcel		error = parse_dir_ask(conf);
657214006Smarcel	else if (strcmp(dir, ".md") == 0)
658214006Smarcel		error = parse_dir_md(conf);
659214006Smarcel	else if (strcmp(dir, ".onfail") == 0)
660214006Smarcel		error = parse_dir_onfail(conf);
661214006Smarcel	else if (strcmp(dir, ".timeout") == 0)
662214006Smarcel		error = parse_dir_timeout(conf);
663214006Smarcel	else {
664214006Smarcel		printf("mountroot: invalid directive `%s'\n", dir);
665214006Smarcel		/* Ignore the rest of the line. */
666214006Smarcel		(void)parse_skipto(conf, '\n');
667214006Smarcel		error = EINVAL;
668214006Smarcel	}
669214006Smarcel	free(dir, M_TEMP);
670214006Smarcel	return (error);
671214006Smarcel}
672214006Smarcel
673214006Smarcelstatic int
674214006Smarcelparse_mount_dev_present(const char *dev)
675214006Smarcel{
676214006Smarcel	struct nameidata nd;
677214006Smarcel	int error;
678214006Smarcel
679214006Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
680214006Smarcel	error = namei(&nd);
681214006Smarcel	if (!error)
682214006Smarcel		vput(nd.ni_vp);
683214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
684214006Smarcel	return (error != 0) ? 0 : 1;
685214006Smarcel}
686214006Smarcel
687243868Skib#define	ERRMSGL	255
688214006Smarcelstatic int
689214006Smarcelparse_mount(char **conf)
690214006Smarcel{
691243868Skib	char *errmsg;
692214006Smarcel	struct mntarg *ma;
693214006Smarcel	char *dev, *fs, *opts, *tok;
694214006Smarcel	int delay, error, timeout;
695214006Smarcel
696214006Smarcel	error = parse_token(conf, &tok);
697214006Smarcel	if (error)
698214006Smarcel		return (error);
699214006Smarcel	fs = tok;
700214006Smarcel	error = parse_skipto(&tok, ':');
701214006Smarcel	if (error) {
702214006Smarcel		free(fs, M_TEMP);
703214006Smarcel		return (error);
704214006Smarcel	}
705214006Smarcel	parse_poke(&tok, '\0');
706214006Smarcel	parse_advance(&tok);
707214006Smarcel	dev = tok;
708214006Smarcel
709214006Smarcel	if (root_mount_mddev != -1) {
710214006Smarcel		/* Handle substitution for the md unit number. */
711214006Smarcel		tok = strstr(dev, "md#");
712214006Smarcel		if (tok != NULL)
713214006Smarcel			tok[2] = '0' + root_mount_mddev;
714214006Smarcel	}
715214006Smarcel
716214006Smarcel	/* Parse options. */
717214006Smarcel	error = parse_token(conf, &tok);
718214006Smarcel	opts = (error == 0) ? tok : NULL;
719214006Smarcel
720214006Smarcel	printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
721214006Smarcel	    (opts != NULL) ? opts : "");
722214006Smarcel
723243868Skib	errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
724214006Smarcel
725214006Smarcel	if (vfs_byname(fs) == NULL) {
726255412Sdelphij		strlcpy(errmsg, "unknown file system", ERRMSGL);
727214006Smarcel		error = ENOENT;
728214006Smarcel		goto out;
729214006Smarcel	}
730214006Smarcel
731253847Sian	if (strcmp(fs, "zfs") != 0 && strstr(fs, "nfs") == NULL &&
732253847Sian	    dev[0] != '\0' && !parse_mount_dev_present(dev)) {
733214006Smarcel		printf("mountroot: waiting for device %s ...\n", dev);
734214006Smarcel		delay = hz / 10;
735214006Smarcel		timeout = root_mount_timeout * hz;
736214006Smarcel		do {
737214006Smarcel			pause("rmdev", delay);
738214006Smarcel			timeout -= delay;
739214006Smarcel		} while (timeout > 0 && !parse_mount_dev_present(dev));
740214006Smarcel		if (timeout <= 0) {
741214006Smarcel			error = ENODEV;
742214006Smarcel			goto out;
743214006Smarcel		}
744214006Smarcel	}
745214006Smarcel
746214006Smarcel	ma = NULL;
747214006Smarcel	ma = mount_arg(ma, "fstype", fs, -1);
748214006Smarcel	ma = mount_arg(ma, "fspath", "/", -1);
749214006Smarcel	ma = mount_arg(ma, "from", dev, -1);
750243868Skib	ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
751214006Smarcel	ma = mount_arg(ma, "ro", NULL, 0);
752214006Smarcel	ma = parse_mountroot_options(ma, opts);
753214006Smarcel	error = kernel_mount(ma, MNT_ROOTFS);
754214006Smarcel
755214006Smarcel out:
756214006Smarcel	if (error) {
757214006Smarcel		printf("Mounting from %s:%s failed with error %d",
758214006Smarcel		    fs, dev, error);
759214006Smarcel		if (errmsg[0] != '\0')
760214006Smarcel			printf(": %s", errmsg);
761214006Smarcel		printf(".\n");
762214006Smarcel	}
763214006Smarcel	free(fs, M_TEMP);
764243868Skib	free(errmsg, M_TEMP);
765214006Smarcel	if (opts != NULL)
766214006Smarcel		free(opts, M_TEMP);
767214006Smarcel	/* kernel_mount can return -1 on error. */
768214006Smarcel	return ((error < 0) ? EDOOFUS : error);
769214006Smarcel}
770243868Skib#undef ERRMSGL
771214006Smarcel
772214006Smarcelstatic int
773214006Smarcelvfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
774214006Smarcel{
775214006Smarcel	struct mount *mp;
776214006Smarcel	char *conf;
777214006Smarcel	int error;
778214006Smarcel
779214006Smarcel	root_mount_mddev = -1;
780214006Smarcel
781214006Smarcelretry:
782214006Smarcel	conf = sbuf_data(sb);
783214006Smarcel	mp = TAILQ_NEXT(mpdevfs, mnt_list);
784214006Smarcel	error = (mp == NULL) ? 0 : EDOOFUS;
785214006Smarcel	root_mount_onfail = A_CONTINUE;
786214006Smarcel	while (mp == NULL) {
787214006Smarcel		error = parse_skipto(&conf, CC_NONWHITESPACE);
788214006Smarcel		if (error == PE_EOL) {
789214006Smarcel			parse_advance(&conf);
790214006Smarcel			continue;
791214006Smarcel		}
792214006Smarcel		if (error < 0)
793214006Smarcel			break;
794214006Smarcel		switch (parse_peek(&conf)) {
795214006Smarcel		case '#':
796214006Smarcel			error = parse_skipto(&conf, '\n');
797214006Smarcel			break;
798214006Smarcel		case '.':
799214006Smarcel			error = parse_directive(&conf);
800214006Smarcel			break;
801214006Smarcel		default:
802214006Smarcel			error = parse_mount(&conf);
803287190Smarcel			if (error == -1) {
804287190Smarcel				printf("mountroot: invalid file system "
805287190Smarcel				    "specification.\n");
806287190Smarcel				error = 0;
807287190Smarcel			}
808214006Smarcel			break;
809214006Smarcel		}
810214006Smarcel		if (error < 0)
811214006Smarcel			break;
812214006Smarcel		/* Ignore any trailing garbage on the line. */
813214006Smarcel		if (parse_peek(&conf) != '\n') {
814214006Smarcel			printf("mountroot: advancing to next directive...\n");
815214006Smarcel			(void)parse_skipto(&conf, '\n');
816214006Smarcel		}
817214006Smarcel		mp = TAILQ_NEXT(mpdevfs, mnt_list);
818214006Smarcel	}
819214006Smarcel	if (mp != NULL)
820214006Smarcel		return (0);
821214006Smarcel
822213365Smarcel	/*
823214006Smarcel	 * We failed to mount (a new) root.
824213365Smarcel	 */
825214006Smarcel	switch (root_mount_onfail) {
826214006Smarcel	case A_CONTINUE:
827214006Smarcel		break;
828214006Smarcel	case A_PANIC:
829214006Smarcel		panic("mountroot: unable to (re-)mount root.");
830214006Smarcel		/* NOTREACHED */
831214006Smarcel	case A_RETRY:
832214006Smarcel		goto retry;
833214006Smarcel	case A_REBOOT:
834214006Smarcel		kern_reboot(RB_NOSYNC);
835214006Smarcel		/* NOTREACHED */
836213365Smarcel	}
837213365Smarcel
838214006Smarcel	return (error);
839214006Smarcel}
840214006Smarcel
841214006Smarcelstatic void
842214006Smarcelvfs_mountroot_conf0(struct sbuf *sb)
843214006Smarcel{
844214006Smarcel	char *s, *tok, *mnt, *opt;
845214006Smarcel	int error;
846214006Smarcel
847214006Smarcel	sbuf_printf(sb, ".onfail panic\n");
848214006Smarcel	sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
849214006Smarcel	if (boothowto & RB_ASKNAME)
850214006Smarcel		sbuf_printf(sb, ".ask\n");
851214006Smarcel#ifdef ROOTDEVNAME
852214006Smarcel	if (boothowto & RB_DFLTROOT)
853214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
854214006Smarcel#endif
855213365Smarcel	if (boothowto & RB_CDROM) {
856217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
857214006Smarcel		sbuf_printf(sb, ".timeout 0\n");
858217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/acd0 ro\n");
859214006Smarcel		sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
860214006Smarcel	}
861273174Sdavide	s = kern_getenv("vfs.root.mountfrom");
862214006Smarcel	if (s != NULL) {
863273174Sdavide		opt = kern_getenv("vfs.root.mountfrom.options");
864214006Smarcel		tok = s;
865214006Smarcel		error = parse_token(&tok, &mnt);
866214006Smarcel		while (!error) {
867214006Smarcel			sbuf_printf(sb, "%s %s\n", mnt,
868214006Smarcel			    (opt != NULL) ? opt : "");
869214006Smarcel			free(mnt, M_TEMP);
870214006Smarcel			error = parse_token(&tok, &mnt);
871213365Smarcel		}
872214006Smarcel		if (opt != NULL)
873214006Smarcel			freeenv(opt);
874214006Smarcel		freeenv(s);
875213365Smarcel	}
876214006Smarcel	if (rootdevnames[0] != NULL)
877214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[0]);
878214006Smarcel	if (rootdevnames[1] != NULL)
879214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[1]);
880214006Smarcel#ifdef ROOTDEVNAME
881214006Smarcel	if (!(boothowto & RB_DFLTROOT))
882214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
883214006Smarcel#endif
884214006Smarcel	if (!(boothowto & RB_ASKNAME))
885214006Smarcel		sbuf_printf(sb, ".ask\n");
886214006Smarcel}
887213365Smarcel
888214006Smarcelstatic int
889214006Smarcelvfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
890214006Smarcel{
891214006Smarcel	static char buf[128];
892214006Smarcel	struct nameidata nd;
893214006Smarcel	off_t ofs;
894231949Skib	ssize_t resid;
895241896Skib	int error, flags, len;
896214006Smarcel
897241896Skib	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
898214006Smarcel	flags = FREAD;
899214006Smarcel	error = vn_open(&nd, &flags, 0, NULL);
900214006Smarcel	if (error)
901214006Smarcel		return (error);
902214006Smarcel
903214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
904214006Smarcel	ofs = 0;
905214006Smarcel	len = sizeof(buf) - 1;
906214006Smarcel	while (1) {
907214006Smarcel		error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
908214006Smarcel		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
909214006Smarcel		    NOCRED, &resid, td);
910214006Smarcel		if (error)
911214006Smarcel			break;
912214006Smarcel		if (resid == len)
913214006Smarcel			break;
914214006Smarcel		buf[len - resid] = 0;
915214006Smarcel		sbuf_printf(sb, "%s", buf);
916214006Smarcel		ofs += len - resid;
917214006Smarcel	}
918214006Smarcel
919214006Smarcel	VOP_UNLOCK(nd.ni_vp, 0);
920214006Smarcel	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
921214006Smarcel	return (error);
922214006Smarcel}
923214006Smarcel
924214006Smarcelstatic void
925214006Smarcelvfs_mountroot_wait(void)
926214006Smarcel{
927214006Smarcel	struct root_hold_token *h;
928214006Smarcel	struct timeval lastfail;
929214006Smarcel	int curfail;
930214006Smarcel
931214006Smarcel	curfail = 0;
932214006Smarcel	while (1) {
933214006Smarcel		DROP_GIANT();
934214006Smarcel		g_waitidle();
935214006Smarcel		PICKUP_GIANT();
936267351Smav		mtx_lock(&root_holds_mtx);
937214006Smarcel		if (LIST_EMPTY(&root_holds)) {
938267351Smav			mtx_unlock(&root_holds_mtx);
939214006Smarcel			break;
940214006Smarcel		}
941214006Smarcel		if (ppsratecheck(&lastfail, &curfail, 1)) {
942214006Smarcel			printf("Root mount waiting for:");
943214006Smarcel			LIST_FOREACH(h, &root_holds, list)
944214006Smarcel				printf(" %s", h->who);
945214006Smarcel			printf("\n");
946214006Smarcel		}
947267351Smav		msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
948214006Smarcel		    hz);
949214006Smarcel	}
950214006Smarcel}
951214006Smarcel
952214006Smarcelvoid
953214006Smarcelvfs_mountroot(void)
954214006Smarcel{
955214006Smarcel	struct mount *mp;
956214006Smarcel	struct sbuf *sb;
957214006Smarcel	struct thread *td;
958214006Smarcel	time_t timebase;
959214006Smarcel	int error;
960214006Smarcel
961214006Smarcel	td = curthread;
962214006Smarcel
963214006Smarcel	vfs_mountroot_wait();
964214006Smarcel
965214006Smarcel	sb = sbuf_new_auto();
966214006Smarcel	vfs_mountroot_conf0(sb);
967214006Smarcel	sbuf_finish(sb);
968214006Smarcel
969214006Smarcel	error = vfs_mountroot_devfs(td, &mp);
970214006Smarcel	while (!error) {
971214006Smarcel		error = vfs_mountroot_parse(sb, mp);
972214006Smarcel		if (!error) {
973288091Sbdrewery			vfs_mountroot_shuffle(td, mp);
974288091Sbdrewery			sbuf_clear(sb);
975288091Sbdrewery			error = vfs_mountroot_readconf(td, sb);
976288091Sbdrewery			sbuf_finish(sb);
977213365Smarcel		}
978213365Smarcel	}
979213365Smarcel
980214006Smarcel	sbuf_delete(sb);
981213365Smarcel
982213365Smarcel	/*
983214006Smarcel	 * Iterate over all currently mounted file systems and use
984214006Smarcel	 * the time stamp found to check and/or initialize the RTC.
985214006Smarcel	 * Call inittodr() only once and pass it the largest of the
986214006Smarcel	 * timestamps we encounter.
987213365Smarcel	 */
988214006Smarcel	timebase = 0;
989214006Smarcel	mtx_lock(&mountlist_mtx);
990214006Smarcel	mp = TAILQ_FIRST(&mountlist);
991214006Smarcel	while (mp != NULL) {
992214006Smarcel		if (mp->mnt_time > timebase)
993214006Smarcel			timebase = mp->mnt_time;
994214006Smarcel		mp = TAILQ_NEXT(mp, mnt_list);
995214006Smarcel	}
996214006Smarcel	mtx_unlock(&mountlist_mtx);
997214006Smarcel	inittodr(timebase);
998213365Smarcel
999214006Smarcel	/* Keep prison0's root in sync with the global rootvnode. */
1000214006Smarcel	mtx_lock(&prison0.pr_mtx);
1001214006Smarcel	prison0.pr_root = rootvnode;
1002214006Smarcel	vref(prison0.pr_root);
1003214006Smarcel	mtx_unlock(&prison0.pr_mtx);
1004213365Smarcel
1005267351Smav	mtx_lock(&root_holds_mtx);
1006214006Smarcel	atomic_store_rel_int(&root_mount_complete, 1);
1007214006Smarcel	wakeup(&root_mount_complete);
1008267351Smav	mtx_unlock(&root_holds_mtx);
1009248645Savg
1010248645Savg	EVENTHANDLER_INVOKE(mountroot);
1011213365Smarcel}
1012213365Smarcel
1013213365Smarcelstatic struct mntarg *
1014213365Smarcelparse_mountroot_options(struct mntarg *ma, const char *options)
1015213365Smarcel{
1016213365Smarcel	char *p;
1017213365Smarcel	char *name, *name_arg;
1018213365Smarcel	char *val, *val_arg;
1019213365Smarcel	char *opts;
1020213365Smarcel
1021213365Smarcel	if (options == NULL || options[0] == '\0')
1022213365Smarcel		return (ma);
1023213365Smarcel
1024213365Smarcel	p = opts = strdup(options, M_MOUNT);
1025213365Smarcel	if (opts == NULL) {
1026213365Smarcel		return (ma);
1027214006Smarcel	}
1028213365Smarcel
1029213365Smarcel	while((name = strsep(&p, ",")) != NULL) {
1030213365Smarcel		if (name[0] == '\0')
1031213365Smarcel			break;
1032213365Smarcel
1033213365Smarcel		val = strchr(name, '=');
1034213365Smarcel		if (val != NULL) {
1035213365Smarcel			*val = '\0';
1036213365Smarcel			++val;
1037213365Smarcel		}
1038213365Smarcel		if( strcmp(name, "rw") == 0 ||
1039213365Smarcel		    strcmp(name, "noro") == 0) {
1040213365Smarcel			/*
1041213365Smarcel			 * The first time we mount the root file system,
1042213365Smarcel			 * we need to mount 'ro', so We need to ignore
1043213365Smarcel			 * 'rw' and 'noro' mount options.
1044213365Smarcel			 */
1045213365Smarcel			continue;
1046213365Smarcel		}
1047213365Smarcel		name_arg = strdup(name, M_MOUNT);
1048213365Smarcel		val_arg = NULL;
1049214006Smarcel		if (val != NULL)
1050213365Smarcel			val_arg = strdup(val, M_MOUNT);
1051213365Smarcel
1052213365Smarcel		ma = mount_arg(ma, name_arg, val_arg,
1053213365Smarcel		    (val_arg != NULL ? -1 : 0));
1054213365Smarcel	}
1055213365Smarcel	free(opts, M_MOUNT);
1056213365Smarcel	return (ma);
1057213365Smarcel}
1058