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$");
42213365Smarcel
43213365Smarcel#include <sys/param.h>
44213365Smarcel#include <sys/conf.h>
45235407Savg#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
98214006Smarcelchar *rootdevnames[2] = {NULL, NULL};
99213365Smarcel
100213365Smarcelstruct root_hold_token {
101213365Smarcel	const char			*who;
102213365Smarcel	LIST_ENTRY(root_hold_token)	list;
103213365Smarcel};
104213365Smarcel
105213365Smarcelstatic LIST_HEAD(, root_hold_token)	root_holds =
106213365Smarcel    LIST_HEAD_INITIALIZER(root_holds);
107213365Smarcel
108214006Smarcelenum action {
109214006Smarcel	A_CONTINUE,
110214006Smarcel	A_PANIC,
111214006Smarcel	A_REBOOT,
112214006Smarcel	A_RETRY
113214006Smarcel};
114214006Smarcel
115214006Smarcelstatic enum action root_mount_onfail = A_CONTINUE;
116214006Smarcel
117214006Smarcelstatic int root_mount_mddev;
118213365Smarcelstatic int root_mount_complete;
119213365Smarcel
120214006Smarcel/* By default wait up to 3 seconds for devices to appear. */
121214006Smarcelstatic int root_mount_timeout = 3;
122255054SmarcelTUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
123214006Smarcel
124213365Smarcelstruct root_hold_token *
125213365Smarcelroot_mount_hold(const char *identifier)
126213365Smarcel{
127213365Smarcel	struct root_hold_token *h;
128213365Smarcel
129213365Smarcel	if (root_mounted())
130213365Smarcel		return (NULL);
131213365Smarcel
132213365Smarcel	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
133213365Smarcel	h->who = identifier;
134213365Smarcel	mtx_lock(&mountlist_mtx);
135213365Smarcel	LIST_INSERT_HEAD(&root_holds, h, list);
136213365Smarcel	mtx_unlock(&mountlist_mtx);
137213365Smarcel	return (h);
138213365Smarcel}
139213365Smarcel
140213365Smarcelvoid
141213365Smarcelroot_mount_rel(struct root_hold_token *h)
142213365Smarcel{
143213365Smarcel
144213365Smarcel	if (h == NULL)
145213365Smarcel		return;
146213365Smarcel	mtx_lock(&mountlist_mtx);
147213365Smarcel	LIST_REMOVE(h, list);
148213365Smarcel	wakeup(&root_holds);
149213365Smarcel	mtx_unlock(&mountlist_mtx);
150213365Smarcel	free(h, M_DEVBUF);
151213365Smarcel}
152213365Smarcel
153213365Smarcelint
154213365Smarcelroot_mounted(void)
155213365Smarcel{
156213365Smarcel
157213365Smarcel	/* No mutex is acquired here because int stores are atomic. */
158213365Smarcel	return (root_mount_complete);
159213365Smarcel}
160213365Smarcel
161213365Smarcelvoid
162213365Smarcelroot_mount_wait(void)
163213365Smarcel{
164213365Smarcel
165213365Smarcel	/*
166213365Smarcel	 * Panic on an obvious deadlock - the function can't be called from
167213365Smarcel	 * a thread which is doing the whole SYSINIT stuff.
168213365Smarcel	 */
169213365Smarcel	KASSERT(curthread->td_proc->p_pid != 0,
170213365Smarcel	    ("root_mount_wait: cannot be called from the swapper thread"));
171213365Smarcel	mtx_lock(&mountlist_mtx);
172213365Smarcel	while (!root_mount_complete) {
173213365Smarcel		msleep(&root_mount_complete, &mountlist_mtx, PZERO, "rootwait",
174213365Smarcel		    hz);
175213365Smarcel	}
176213365Smarcel	mtx_unlock(&mountlist_mtx);
177213365Smarcel}
178213365Smarcel
179213365Smarcelstatic void
180213365Smarcelset_rootvnode(void)
181213365Smarcel{
182213365Smarcel	struct proc *p;
183213365Smarcel
184213365Smarcel	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
185213365Smarcel		panic("Cannot find root vnode");
186213365Smarcel
187213365Smarcel	VOP_UNLOCK(rootvnode, 0);
188213365Smarcel
189213365Smarcel	p = curthread->td_proc;
190213365Smarcel	FILEDESC_XLOCK(p->p_fd);
191213365Smarcel
192213365Smarcel	if (p->p_fd->fd_cdir != NULL)
193213365Smarcel		vrele(p->p_fd->fd_cdir);
194213365Smarcel	p->p_fd->fd_cdir = rootvnode;
195213365Smarcel	VREF(rootvnode);
196213365Smarcel
197213365Smarcel	if (p->p_fd->fd_rdir != NULL)
198213365Smarcel		vrele(p->p_fd->fd_rdir);
199213365Smarcel	p->p_fd->fd_rdir = rootvnode;
200213365Smarcel	VREF(rootvnode);
201213365Smarcel
202213365Smarcel	FILEDESC_XUNLOCK(p->p_fd);
203213365Smarcel}
204213365Smarcel
205214006Smarcelstatic int
206214006Smarcelvfs_mountroot_devfs(struct thread *td, struct mount **mpp)
207213365Smarcel{
208213365Smarcel	struct vfsoptlist *opts;
209213365Smarcel	struct vfsconf *vfsp;
210214006Smarcel	struct mount *mp;
211213365Smarcel	int error;
212213365Smarcel
213214006Smarcel	*mpp = NULL;
214214006Smarcel
215213365Smarcel	vfsp = vfs_byname("devfs");
216213365Smarcel	KASSERT(vfsp != NULL, ("Could not find devfs by name"));
217213365Smarcel	if (vfsp == NULL)
218214006Smarcel		return (ENOENT);
219213365Smarcel
220213365Smarcel	mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
221213365Smarcel
222213365Smarcel	error = VFS_MOUNT(mp);
223213365Smarcel	KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
224213365Smarcel	if (error)
225214006Smarcel		return (error);
226213365Smarcel
227213365Smarcel	opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
228213365Smarcel	TAILQ_INIT(opts);
229213365Smarcel	mp->mnt_opt = opts;
230213365Smarcel
231213365Smarcel	mtx_lock(&mountlist_mtx);
232213365Smarcel	TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
233213365Smarcel	mtx_unlock(&mountlist_mtx);
234213365Smarcel
235214006Smarcel	*mpp = mp;
236213365Smarcel	set_rootvnode();
237213365Smarcel
238213365Smarcel	error = kern_symlink(td, "/", "dev", UIO_SYSSPACE);
239213365Smarcel	if (error)
240213365Smarcel		printf("kern_symlink /dev -> / returns %d\n", error);
241214006Smarcel
242214006Smarcel	return (error);
243213365Smarcel}
244213365Smarcel
245214006Smarcelstatic int
246214006Smarcelvfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
247213365Smarcel{
248213365Smarcel	struct nameidata nd;
249214006Smarcel	struct mount *mporoot, *mpnroot;
250214006Smarcel	struct vnode *vp, *vporoot, *vpdevfs;
251214006Smarcel	char *fspath;
252213365Smarcel	int error;
253213365Smarcel
254214006Smarcel	mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
255214006Smarcel
256214006Smarcel	/* Shuffle the mountlist. */
257213365Smarcel	mtx_lock(&mountlist_mtx);
258214006Smarcel	mporoot = TAILQ_FIRST(&mountlist);
259214006Smarcel	TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
260214006Smarcel	if (mporoot != mpdevfs) {
261214006Smarcel		TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
262214006Smarcel		TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
263214006Smarcel	}
264214006Smarcel	TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
265213365Smarcel	mtx_unlock(&mountlist_mtx);
266213365Smarcel
267214006Smarcel	cache_purgevfs(mporoot);
268214006Smarcel	if (mporoot != mpdevfs)
269214006Smarcel		cache_purgevfs(mpdevfs);
270213365Smarcel
271214006Smarcel	VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot);
272214006Smarcel
273214006Smarcel	VI_LOCK(vporoot);
274214006Smarcel	vporoot->v_iflag &= ~VI_MOUNT;
275214006Smarcel	VI_UNLOCK(vporoot);
276214006Smarcel	vporoot->v_mountedhere = NULL;
277214006Smarcel	mporoot->mnt_flag &= ~MNT_ROOTFS;
278214006Smarcel	mporoot->mnt_vnodecovered = NULL;
279214006Smarcel	vput(vporoot);
280214006Smarcel
281214006Smarcel	/* Set up the new rootvnode, and purge the cache */
282214006Smarcel	mpnroot->mnt_vnodecovered = NULL;
283213365Smarcel	set_rootvnode();
284213365Smarcel	cache_purgevfs(rootvnode->v_mount);
285213365Smarcel
286214006Smarcel	if (mporoot != mpdevfs) {
287214006Smarcel		/* Remount old root under /.mount or /mnt */
288214006Smarcel		fspath = "/.mount";
289214006Smarcel		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
290214006Smarcel		    fspath, td);
291214006Smarcel		error = namei(&nd);
292214006Smarcel		if (error) {
293214006Smarcel			NDFREE(&nd, NDF_ONLY_PNBUF);
294214006Smarcel			fspath = "/mnt";
295214006Smarcel			NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
296214006Smarcel			    fspath, td);
297214006Smarcel			error = namei(&nd);
298214006Smarcel		}
299214006Smarcel		if (!error) {
300214006Smarcel			vp = nd.ni_vp;
301214006Smarcel			error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
302214006Smarcel			if (!error)
303214006Smarcel				error = vinvalbuf(vp, V_SAVE, 0, 0);
304214006Smarcel			if (!error) {
305214006Smarcel				cache_purge(vp);
306214006Smarcel				mporoot->mnt_vnodecovered = vp;
307214006Smarcel				vp->v_mountedhere = mporoot;
308214006Smarcel				strlcpy(mporoot->mnt_stat.f_mntonname,
309214006Smarcel				    fspath, MNAMELEN);
310214006Smarcel				VOP_UNLOCK(vp, 0);
311214006Smarcel			} else
312214006Smarcel				vput(vp);
313214006Smarcel		}
314214006Smarcel		NDFREE(&nd, NDF_ONLY_PNBUF);
315214006Smarcel
316214006Smarcel		if (error && bootverbose)
317214006Smarcel			printf("mountroot: unable to remount previous root "
318214006Smarcel			    "under /.mount or /mnt (error %d).\n", error);
319214006Smarcel	}
320214006Smarcel
321214006Smarcel	/* Remount devfs under /dev */
322213365Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
323213365Smarcel	error = namei(&nd);
324214006Smarcel	if (!error) {
325214006Smarcel		vp = nd.ni_vp;
326214006Smarcel		error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
327214006Smarcel		if (!error)
328214006Smarcel			error = vinvalbuf(vp, V_SAVE, 0, 0);
329214006Smarcel		if (!error) {
330214006Smarcel			vpdevfs = mpdevfs->mnt_vnodecovered;
331214006Smarcel			if (vpdevfs != NULL) {
332214006Smarcel				cache_purge(vpdevfs);
333214006Smarcel				vpdevfs->v_mountedhere = NULL;
334214006Smarcel				vrele(vpdevfs);
335214006Smarcel			}
336214006Smarcel			mpdevfs->mnt_vnodecovered = vp;
337214006Smarcel			vp->v_mountedhere = mpdevfs;
338214006Smarcel			VOP_UNLOCK(vp, 0);
339214006Smarcel		} else
340214006Smarcel			vput(vp);
341213365Smarcel	}
342214006Smarcel	if (error && bootverbose)
343214006Smarcel		printf("mountroot: unable to remount devfs under /dev "
344214006Smarcel		    "(error %d).\n", error);
345213365Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
346214006Smarcel
347214006Smarcel	if (mporoot == mpdevfs) {
348214006Smarcel		vfs_unbusy(mpdevfs);
349214006Smarcel		/* Unlink the no longer needed /dev/dev -> / symlink */
350214006Smarcel		error = kern_unlink(td, "/dev/dev", UIO_SYSSPACE);
351214006Smarcel		if (error && bootverbose)
352214006Smarcel			printf("mountroot: unable to unlink /dev/dev "
353214006Smarcel			    "(error %d)\n", error);
354213365Smarcel	}
355214006Smarcel
356214006Smarcel	return (0);
357214006Smarcel}
358214006Smarcel
359214006Smarcel/*
360214006Smarcel * Configuration parser.
361214006Smarcel */
362214006Smarcel
363214006Smarcel/* Parser character classes. */
364214006Smarcel#define	CC_WHITESPACE		-1
365214006Smarcel#define	CC_NONWHITESPACE	-2
366214006Smarcel
367214006Smarcel/* Parse errors. */
368214006Smarcel#define	PE_EOF			-1
369214006Smarcel#define	PE_EOL			-2
370214006Smarcel
371214006Smarcelstatic __inline int
372214006Smarcelparse_peek(char **conf)
373214006Smarcel{
374214006Smarcel
375214006Smarcel	return (**conf);
376214006Smarcel}
377214006Smarcel
378214006Smarcelstatic __inline void
379214006Smarcelparse_poke(char **conf, int c)
380214006Smarcel{
381214006Smarcel
382214006Smarcel	**conf = c;
383214006Smarcel}
384214006Smarcel
385214006Smarcelstatic __inline void
386214006Smarcelparse_advance(char **conf)
387214006Smarcel{
388214006Smarcel
389214006Smarcel	(*conf)++;
390214006Smarcel}
391214006Smarcel
392214006Smarcelstatic int
393214006Smarcelparse_skipto(char **conf, int mc)
394214006Smarcel{
395214006Smarcel	int c, match;
396214006Smarcel
397214006Smarcel	while (1) {
398214006Smarcel		c = parse_peek(conf);
399214006Smarcel		if (c == 0)
400214006Smarcel			return (PE_EOF);
401214006Smarcel		switch (mc) {
402214006Smarcel		case CC_WHITESPACE:
403214006Smarcel			match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
404214006Smarcel			break;
405214006Smarcel		case CC_NONWHITESPACE:
406214006Smarcel			if (c == '\n')
407214006Smarcel				return (PE_EOL);
408214006Smarcel			match = (c != ' ' && c != '\t') ? 1 : 0;
409214006Smarcel			break;
410214006Smarcel		default:
411214006Smarcel			match = (c == mc) ? 1 : 0;
412214006Smarcel			break;
413214006Smarcel		}
414214006Smarcel		if (match)
415214006Smarcel			break;
416214006Smarcel		parse_advance(conf);
417213365Smarcel	}
418214006Smarcel	return (0);
419214006Smarcel}
420213365Smarcel
421214006Smarcelstatic int
422214006Smarcelparse_token(char **conf, char **tok)
423214006Smarcel{
424214006Smarcel	char *p;
425214006Smarcel	size_t len;
426214006Smarcel	int error;
427214006Smarcel
428214006Smarcel	*tok = NULL;
429214006Smarcel	error = parse_skipto(conf, CC_NONWHITESPACE);
430213365Smarcel	if (error)
431214006Smarcel		return (error);
432214006Smarcel	p = *conf;
433214006Smarcel	error = parse_skipto(conf, CC_WHITESPACE);
434214006Smarcel	len = *conf - p;
435214006Smarcel	*tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
436214006Smarcel	bcopy(p, *tok, len);
437214006Smarcel	return (0);
438213365Smarcel}
439213365Smarcel
440214006Smarcelstatic void
441214006Smarcelparse_dir_ask_printenv(const char *var)
442213365Smarcel{
443214006Smarcel	char *val;
444213365Smarcel
445214006Smarcel	val = getenv(var);
446214006Smarcel	if (val != NULL) {
447214006Smarcel		printf("  %s=%s\n", var, val);
448214006Smarcel		freeenv(val);
449214006Smarcel	}
450214006Smarcel}
451213365Smarcel
452214006Smarcelstatic int
453214006Smarcelparse_dir_ask(char **conf)
454214006Smarcel{
455214006Smarcel	char name[80];
456214006Smarcel	char *mnt;
457214006Smarcel	int error;
458213365Smarcel
459214006Smarcel	printf("\nLoader variables:\n");
460214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom");
461214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom.options");
462213365Smarcel
463214006Smarcel	printf("\nManual root filesystem specification:\n");
464214006Smarcel	printf("  <fstype>:<device> [options]\n");
465214006Smarcel	printf("      Mount <device> using filesystem <fstype>\n");
466214006Smarcel	printf("      and with the specified (optional) option list.\n");
467214006Smarcel	printf("\n");
468214006Smarcel	printf("    eg. ufs:/dev/da0s1a\n");
469214006Smarcel	printf("        zfs:tank\n");
470214006Smarcel	printf("        cd9660:/dev/acd0 ro\n");
471214006Smarcel	printf("          (which is equivalent to: ");
472214006Smarcel	printf("mount -t cd9660 -o ro /dev/acd0 /)\n");
473214006Smarcel	printf("\n");
474214006Smarcel	printf("  ?               List valid disk boot devices\n");
475214006Smarcel	printf("  .               Yield 1 second (for background tasks)\n");
476214006Smarcel	printf("  <empty line>    Abort manual input\n");
477214006Smarcel
478226906Smarcel	do {
479226906Smarcel		error = EINVAL;
480226906Smarcel		printf("\nmountroot> ");
481235407Savg		cngets(name, sizeof(name), GETS_ECHO);
482226906Smarcel		if (name[0] == '\0')
483226906Smarcel			break;
484226906Smarcel		if (name[0] == '?' && name[1] == '\0') {
485226906Smarcel			printf("\nList of GEOM managed disk devices:\n  ");
486226906Smarcel			g_dev_print();
487226906Smarcel			continue;
488226906Smarcel		}
489226906Smarcel		if (name[0] == '.' && name[1] == '\0') {
490226906Smarcel			pause("rmask", hz);
491226906Smarcel			continue;
492226906Smarcel		}
493226906Smarcel		mnt = name;
494226906Smarcel		error = parse_mount(&mnt);
495226906Smarcel		if (error == -1)
496226906Smarcel			printf("Invalid file system specification.\n");
497226906Smarcel	} while (error != 0);
498226906Smarcel
499214006Smarcel	return (error);
500214006Smarcel}
501213365Smarcel
502214006Smarcelstatic int
503214006Smarcelparse_dir_md(char **conf)
504214006Smarcel{
505214006Smarcel	struct stat sb;
506214006Smarcel	struct thread *td;
507214006Smarcel	struct md_ioctl *mdio;
508214006Smarcel	char *path, *tok;
509214006Smarcel	int error, fd, len;
510213365Smarcel
511214006Smarcel	td = curthread;
512214006Smarcel
513214006Smarcel	error = parse_token(conf, &tok);
514214006Smarcel	if (error)
515214006Smarcel		return (error);
516214006Smarcel
517214006Smarcel	len = strlen(tok);
518214006Smarcel	mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
519214006Smarcel	path = (void *)(mdio + 1);
520214006Smarcel	bcopy(tok, path, len);
521214006Smarcel	free(tok, M_TEMP);
522214006Smarcel
523214006Smarcel	/* Get file status. */
524214006Smarcel	error = kern_stat(td, path, UIO_SYSSPACE, &sb);
525214006Smarcel	if (error)
526214006Smarcel		goto out;
527214006Smarcel
528214006Smarcel	/* Open /dev/mdctl so that we can attach/detach. */
529214006Smarcel	error = kern_open(td, "/dev/" MDCTL_NAME, UIO_SYSSPACE, O_RDWR, 0);
530214006Smarcel	if (error)
531214006Smarcel		goto out;
532214006Smarcel
533214006Smarcel	fd = td->td_retval[0];
534214006Smarcel	mdio->md_version = MDIOVERSION;
535214006Smarcel	mdio->md_type = MD_VNODE;
536214006Smarcel
537214006Smarcel	if (root_mount_mddev != -1) {
538214006Smarcel		mdio->md_unit = root_mount_mddev;
539214006Smarcel		DROP_GIANT();
540214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
541214006Smarcel		PICKUP_GIANT();
542214006Smarcel		/* Ignore errors. We don't care. */
543214006Smarcel		root_mount_mddev = -1;
544214006Smarcel	}
545214006Smarcel
546214006Smarcel	mdio->md_file = (void *)(mdio + 1);
547214006Smarcel	mdio->md_options = MD_AUTOUNIT | MD_READONLY;
548214006Smarcel	mdio->md_mediasize = sb.st_size;
549214006Smarcel	mdio->md_unit = 0;
550214006Smarcel	DROP_GIANT();
551214006Smarcel	error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
552214006Smarcel	PICKUP_GIANT();
553214006Smarcel	if (error)
554214006Smarcel		goto out;
555214006Smarcel
556214006Smarcel	if (mdio->md_unit > 9) {
557214006Smarcel		printf("rootmount: too many md units\n");
558214006Smarcel		mdio->md_file = NULL;
559214006Smarcel		mdio->md_options = 0;
560214006Smarcel		mdio->md_mediasize = 0;
561214006Smarcel		DROP_GIANT();
562214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
563214006Smarcel		PICKUP_GIANT();
564214006Smarcel		/* Ignore errors. We don't care. */
565214006Smarcel		error = ERANGE;
566214006Smarcel		goto out;
567214006Smarcel	}
568214006Smarcel
569214006Smarcel	root_mount_mddev = mdio->md_unit;
570214006Smarcel	printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
571214006Smarcel
572214006Smarcel	error = kern_close(td, fd);
573214006Smarcel
574214006Smarcel out:
575214006Smarcel	free(mdio, M_TEMP);
576214006Smarcel	return (error);
577214006Smarcel}
578214006Smarcel
579214006Smarcelstatic int
580214006Smarcelparse_dir_onfail(char **conf)
581214006Smarcel{
582214006Smarcel	char *action;
583214006Smarcel	int error;
584214006Smarcel
585214006Smarcel	error = parse_token(conf, &action);
586214006Smarcel	if (error)
587214006Smarcel		return (error);
588214006Smarcel
589214006Smarcel	if (!strcmp(action, "continue"))
590214006Smarcel		root_mount_onfail = A_CONTINUE;
591214006Smarcel	else if (!strcmp(action, "panic"))
592214006Smarcel		root_mount_onfail = A_PANIC;
593214006Smarcel	else if (!strcmp(action, "reboot"))
594214006Smarcel		root_mount_onfail = A_REBOOT;
595214006Smarcel	else if (!strcmp(action, "retry"))
596214006Smarcel		root_mount_onfail = A_RETRY;
597214006Smarcel	else {
598214006Smarcel		printf("rootmount: %s: unknown action\n", action);
599214006Smarcel		error = EINVAL;
600214006Smarcel	}
601214006Smarcel
602214006Smarcel	free(action, M_TEMP);
603214006Smarcel	return (0);
604214006Smarcel}
605214006Smarcel
606214006Smarcelstatic int
607214006Smarcelparse_dir_timeout(char **conf)
608214006Smarcel{
609214006Smarcel	char *tok, *endtok;
610214006Smarcel	long secs;
611214006Smarcel	int error;
612214006Smarcel
613214006Smarcel	error = parse_token(conf, &tok);
614214006Smarcel	if (error)
615214006Smarcel		return (error);
616214006Smarcel
617214006Smarcel	secs = strtol(tok, &endtok, 0);
618214006Smarcel	error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
619214006Smarcel	if (!error)
620214006Smarcel		root_mount_timeout = secs;
621214006Smarcel	free(tok, M_TEMP);
622214006Smarcel	return (error);
623214006Smarcel}
624214006Smarcel
625214006Smarcelstatic int
626214006Smarcelparse_directive(char **conf)
627214006Smarcel{
628214006Smarcel	char *dir;
629214006Smarcel	int error;
630214006Smarcel
631214006Smarcel	error = parse_token(conf, &dir);
632214006Smarcel	if (error)
633214006Smarcel		return (error);
634214006Smarcel
635214006Smarcel	if (strcmp(dir, ".ask") == 0)
636214006Smarcel		error = parse_dir_ask(conf);
637214006Smarcel	else if (strcmp(dir, ".md") == 0)
638214006Smarcel		error = parse_dir_md(conf);
639214006Smarcel	else if (strcmp(dir, ".onfail") == 0)
640214006Smarcel		error = parse_dir_onfail(conf);
641214006Smarcel	else if (strcmp(dir, ".timeout") == 0)
642214006Smarcel		error = parse_dir_timeout(conf);
643214006Smarcel	else {
644214006Smarcel		printf("mountroot: invalid directive `%s'\n", dir);
645214006Smarcel		/* Ignore the rest of the line. */
646214006Smarcel		(void)parse_skipto(conf, '\n');
647214006Smarcel		error = EINVAL;
648214006Smarcel	}
649214006Smarcel	free(dir, M_TEMP);
650214006Smarcel	return (error);
651214006Smarcel}
652214006Smarcel
653214006Smarcelstatic int
654214006Smarcelparse_mount_dev_present(const char *dev)
655214006Smarcel{
656214006Smarcel	struct nameidata nd;
657214006Smarcel	int error;
658214006Smarcel
659214006Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
660214006Smarcel	error = namei(&nd);
661214006Smarcel	if (!error)
662214006Smarcel		vput(nd.ni_vp);
663214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
664214006Smarcel	return (error != 0) ? 0 : 1;
665214006Smarcel}
666214006Smarcel
667244110Skib#define	ERRMSGL	255
668214006Smarcelstatic int
669214006Smarcelparse_mount(char **conf)
670214006Smarcel{
671244110Skib	char *errmsg;
672214006Smarcel	struct mntarg *ma;
673214006Smarcel	char *dev, *fs, *opts, *tok;
674214006Smarcel	int delay, error, timeout;
675214006Smarcel
676214006Smarcel	error = parse_token(conf, &tok);
677214006Smarcel	if (error)
678214006Smarcel		return (error);
679214006Smarcel	fs = tok;
680214006Smarcel	error = parse_skipto(&tok, ':');
681214006Smarcel	if (error) {
682214006Smarcel		free(fs, M_TEMP);
683214006Smarcel		return (error);
684214006Smarcel	}
685214006Smarcel	parse_poke(&tok, '\0');
686214006Smarcel	parse_advance(&tok);
687214006Smarcel	dev = tok;
688214006Smarcel
689214006Smarcel	if (root_mount_mddev != -1) {
690214006Smarcel		/* Handle substitution for the md unit number. */
691214006Smarcel		tok = strstr(dev, "md#");
692214006Smarcel		if (tok != NULL)
693214006Smarcel			tok[2] = '0' + root_mount_mddev;
694214006Smarcel	}
695214006Smarcel
696214006Smarcel	/* Parse options. */
697214006Smarcel	error = parse_token(conf, &tok);
698214006Smarcel	opts = (error == 0) ? tok : NULL;
699214006Smarcel
700214006Smarcel	printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
701214006Smarcel	    (opts != NULL) ? opts : "");
702214006Smarcel
703244110Skib	errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
704214006Smarcel
705214006Smarcel	if (vfs_byname(fs) == NULL) {
706255485Sdelphij		strlcpy(errmsg, "unknown file system", ERRMSGL);
707214006Smarcel		error = ENOENT;
708214006Smarcel		goto out;
709214006Smarcel	}
710214006Smarcel
711214067Sae	if (strcmp(fs, "zfs") != 0 && dev[0] != '\0' &&
712214067Sae	    !parse_mount_dev_present(dev)) {
713214006Smarcel		printf("mountroot: waiting for device %s ...\n", dev);
714214006Smarcel		delay = hz / 10;
715214006Smarcel		timeout = root_mount_timeout * hz;
716214006Smarcel		do {
717214006Smarcel			pause("rmdev", delay);
718214006Smarcel			timeout -= delay;
719214006Smarcel		} while (timeout > 0 && !parse_mount_dev_present(dev));
720214006Smarcel		if (timeout <= 0) {
721214006Smarcel			error = ENODEV;
722214006Smarcel			goto out;
723214006Smarcel		}
724214006Smarcel	}
725214006Smarcel
726214006Smarcel	ma = NULL;
727214006Smarcel	ma = mount_arg(ma, "fstype", fs, -1);
728214006Smarcel	ma = mount_arg(ma, "fspath", "/", -1);
729214006Smarcel	ma = mount_arg(ma, "from", dev, -1);
730244110Skib	ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
731214006Smarcel	ma = mount_arg(ma, "ro", NULL, 0);
732214006Smarcel	ma = parse_mountroot_options(ma, opts);
733214006Smarcel	error = kernel_mount(ma, MNT_ROOTFS);
734214006Smarcel
735214006Smarcel out:
736214006Smarcel	if (error) {
737214006Smarcel		printf("Mounting from %s:%s failed with error %d",
738214006Smarcel		    fs, dev, error);
739214006Smarcel		if (errmsg[0] != '\0')
740214006Smarcel			printf(": %s", errmsg);
741214006Smarcel		printf(".\n");
742214006Smarcel	}
743214006Smarcel	free(fs, M_TEMP);
744244110Skib	free(errmsg, M_TEMP);
745214006Smarcel	if (opts != NULL)
746214006Smarcel		free(opts, M_TEMP);
747214006Smarcel	/* kernel_mount can return -1 on error. */
748214006Smarcel	return ((error < 0) ? EDOOFUS : error);
749214006Smarcel}
750244110Skib#undef ERRMSGL
751214006Smarcel
752214006Smarcelstatic int
753214006Smarcelvfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
754214006Smarcel{
755214006Smarcel	struct mount *mp;
756214006Smarcel	char *conf;
757214006Smarcel	int error;
758214006Smarcel
759214006Smarcel	root_mount_mddev = -1;
760214006Smarcel
761214006Smarcelretry:
762214006Smarcel	conf = sbuf_data(sb);
763214006Smarcel	mp = TAILQ_NEXT(mpdevfs, mnt_list);
764214006Smarcel	error = (mp == NULL) ? 0 : EDOOFUS;
765214006Smarcel	root_mount_onfail = A_CONTINUE;
766214006Smarcel	while (mp == NULL) {
767214006Smarcel		error = parse_skipto(&conf, CC_NONWHITESPACE);
768214006Smarcel		if (error == PE_EOL) {
769214006Smarcel			parse_advance(&conf);
770214006Smarcel			continue;
771214006Smarcel		}
772214006Smarcel		if (error < 0)
773214006Smarcel			break;
774214006Smarcel		switch (parse_peek(&conf)) {
775214006Smarcel		case '#':
776214006Smarcel			error = parse_skipto(&conf, '\n');
777214006Smarcel			break;
778214006Smarcel		case '.':
779214006Smarcel			error = parse_directive(&conf);
780214006Smarcel			break;
781214006Smarcel		default:
782214006Smarcel			error = parse_mount(&conf);
783214006Smarcel			break;
784214006Smarcel		}
785214006Smarcel		if (error < 0)
786214006Smarcel			break;
787214006Smarcel		/* Ignore any trailing garbage on the line. */
788214006Smarcel		if (parse_peek(&conf) != '\n') {
789214006Smarcel			printf("mountroot: advancing to next directive...\n");
790214006Smarcel			(void)parse_skipto(&conf, '\n');
791214006Smarcel		}
792214006Smarcel		mp = TAILQ_NEXT(mpdevfs, mnt_list);
793214006Smarcel	}
794214006Smarcel	if (mp != NULL)
795214006Smarcel		return (0);
796214006Smarcel
797213365Smarcel	/*
798214006Smarcel	 * We failed to mount (a new) root.
799213365Smarcel	 */
800214006Smarcel	switch (root_mount_onfail) {
801214006Smarcel	case A_CONTINUE:
802214006Smarcel		break;
803214006Smarcel	case A_PANIC:
804214006Smarcel		panic("mountroot: unable to (re-)mount root.");
805214006Smarcel		/* NOTREACHED */
806214006Smarcel	case A_RETRY:
807214006Smarcel		goto retry;
808214006Smarcel	case A_REBOOT:
809214006Smarcel		kern_reboot(RB_NOSYNC);
810214006Smarcel		/* NOTREACHED */
811213365Smarcel	}
812213365Smarcel
813214006Smarcel	return (error);
814214006Smarcel}
815214006Smarcel
816214006Smarcelstatic void
817214006Smarcelvfs_mountroot_conf0(struct sbuf *sb)
818214006Smarcel{
819214006Smarcel	char *s, *tok, *mnt, *opt;
820214006Smarcel	int error;
821214006Smarcel
822214006Smarcel	sbuf_printf(sb, ".onfail panic\n");
823214006Smarcel	sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
824214006Smarcel	if (boothowto & RB_ASKNAME)
825214006Smarcel		sbuf_printf(sb, ".ask\n");
826214006Smarcel#ifdef ROOTDEVNAME
827214006Smarcel	if (boothowto & RB_DFLTROOT)
828214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
829214006Smarcel#endif
830213365Smarcel	if (boothowto & RB_CDROM) {
831217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
832214006Smarcel		sbuf_printf(sb, ".timeout 0\n");
833217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/acd0 ro\n");
834214006Smarcel		sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
835214006Smarcel	}
836214006Smarcel	s = getenv("vfs.root.mountfrom");
837214006Smarcel	if (s != NULL) {
838214006Smarcel		opt = getenv("vfs.root.mountfrom.options");
839214006Smarcel		tok = s;
840214006Smarcel		error = parse_token(&tok, &mnt);
841214006Smarcel		while (!error) {
842214006Smarcel			sbuf_printf(sb, "%s %s\n", mnt,
843214006Smarcel			    (opt != NULL) ? opt : "");
844214006Smarcel			free(mnt, M_TEMP);
845214006Smarcel			error = parse_token(&tok, &mnt);
846213365Smarcel		}
847214006Smarcel		if (opt != NULL)
848214006Smarcel			freeenv(opt);
849214006Smarcel		freeenv(s);
850213365Smarcel	}
851214006Smarcel	if (rootdevnames[0] != NULL)
852214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[0]);
853214006Smarcel	if (rootdevnames[1] != NULL)
854214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[1]);
855214006Smarcel#ifdef ROOTDEVNAME
856214006Smarcel	if (!(boothowto & RB_DFLTROOT))
857214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
858214006Smarcel#endif
859214006Smarcel	if (!(boothowto & RB_ASKNAME))
860214006Smarcel		sbuf_printf(sb, ".ask\n");
861214006Smarcel}
862213365Smarcel
863214006Smarcelstatic int
864214006Smarcelvfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
865214006Smarcel{
866214006Smarcel	static char buf[128];
867214006Smarcel	struct nameidata nd;
868214006Smarcel	off_t ofs;
869233353Skib	ssize_t resid;
870233353Skib	int error, flags, len, vfslocked;
871214006Smarcel
872214006Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE,
873214006Smarcel	    "/.mount.conf", td);
874214006Smarcel	flags = FREAD;
875214006Smarcel	error = vn_open(&nd, &flags, 0, NULL);
876214006Smarcel	if (error)
877214006Smarcel		return (error);
878214006Smarcel
879214006Smarcel	vfslocked = NDHASGIANT(&nd);
880214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
881214006Smarcel	ofs = 0;
882214006Smarcel	len = sizeof(buf) - 1;
883214006Smarcel	while (1) {
884214006Smarcel		error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
885214006Smarcel		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
886214006Smarcel		    NOCRED, &resid, td);
887214006Smarcel		if (error)
888214006Smarcel			break;
889214006Smarcel		if (resid == len)
890214006Smarcel			break;
891214006Smarcel		buf[len - resid] = 0;
892214006Smarcel		sbuf_printf(sb, "%s", buf);
893214006Smarcel		ofs += len - resid;
894214006Smarcel	}
895214006Smarcel
896214006Smarcel	VOP_UNLOCK(nd.ni_vp, 0);
897214006Smarcel	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
898214006Smarcel	VFS_UNLOCK_GIANT(vfslocked);
899214006Smarcel	return (error);
900214006Smarcel}
901214006Smarcel
902214006Smarcelstatic void
903214006Smarcelvfs_mountroot_wait(void)
904214006Smarcel{
905214006Smarcel	struct root_hold_token *h;
906214006Smarcel	struct timeval lastfail;
907214006Smarcel	int curfail;
908214006Smarcel
909214006Smarcel	curfail = 0;
910214006Smarcel	while (1) {
911214006Smarcel		DROP_GIANT();
912214006Smarcel		g_waitidle();
913214006Smarcel		PICKUP_GIANT();
914214006Smarcel		mtx_lock(&mountlist_mtx);
915214006Smarcel		if (LIST_EMPTY(&root_holds)) {
916214006Smarcel			mtx_unlock(&mountlist_mtx);
917214006Smarcel			break;
918214006Smarcel		}
919214006Smarcel		if (ppsratecheck(&lastfail, &curfail, 1)) {
920214006Smarcel			printf("Root mount waiting for:");
921214006Smarcel			LIST_FOREACH(h, &root_holds, list)
922214006Smarcel				printf(" %s", h->who);
923214006Smarcel			printf("\n");
924214006Smarcel		}
925214006Smarcel		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
926214006Smarcel		    hz);
927214006Smarcel	}
928214006Smarcel}
929214006Smarcel
930214006Smarcelvoid
931214006Smarcelvfs_mountroot(void)
932214006Smarcel{
933214006Smarcel	struct mount *mp;
934214006Smarcel	struct sbuf *sb;
935214006Smarcel	struct thread *td;
936214006Smarcel	time_t timebase;
937214006Smarcel	int error;
938214006Smarcel
939214006Smarcel	td = curthread;
940214006Smarcel
941214006Smarcel	vfs_mountroot_wait();
942214006Smarcel
943214006Smarcel	sb = sbuf_new_auto();
944214006Smarcel	vfs_mountroot_conf0(sb);
945214006Smarcel	sbuf_finish(sb);
946214006Smarcel
947214006Smarcel	error = vfs_mountroot_devfs(td, &mp);
948214006Smarcel	while (!error) {
949214006Smarcel		error = vfs_mountroot_parse(sb, mp);
950214006Smarcel		if (!error) {
951214006Smarcel			error = vfs_mountroot_shuffle(td, mp);
952214006Smarcel			if (!error) {
953214006Smarcel				sbuf_clear(sb);
954214006Smarcel				error = vfs_mountroot_readconf(td, sb);
955214006Smarcel				sbuf_finish(sb);
956213365Smarcel			}
957213365Smarcel		}
958213365Smarcel	}
959213365Smarcel
960214006Smarcel	sbuf_delete(sb);
961213365Smarcel
962213365Smarcel	/*
963214006Smarcel	 * Iterate over all currently mounted file systems and use
964214006Smarcel	 * the time stamp found to check and/or initialize the RTC.
965214006Smarcel	 * Call inittodr() only once and pass it the largest of the
966214006Smarcel	 * timestamps we encounter.
967213365Smarcel	 */
968214006Smarcel	timebase = 0;
969214006Smarcel	mtx_lock(&mountlist_mtx);
970214006Smarcel	mp = TAILQ_FIRST(&mountlist);
971214006Smarcel	while (mp != NULL) {
972214006Smarcel		if (mp->mnt_time > timebase)
973214006Smarcel			timebase = mp->mnt_time;
974214006Smarcel		mp = TAILQ_NEXT(mp, mnt_list);
975214006Smarcel	}
976214006Smarcel	mtx_unlock(&mountlist_mtx);
977214006Smarcel	inittodr(timebase);
978213365Smarcel
979214006Smarcel	/* Keep prison0's root in sync with the global rootvnode. */
980214006Smarcel	mtx_lock(&prison0.pr_mtx);
981214006Smarcel	prison0.pr_root = rootvnode;
982214006Smarcel	vref(prison0.pr_root);
983214006Smarcel	mtx_unlock(&prison0.pr_mtx);
984213365Smarcel
985214006Smarcel	mtx_lock(&mountlist_mtx);
986214006Smarcel	atomic_store_rel_int(&root_mount_complete, 1);
987214006Smarcel	wakeup(&root_mount_complete);
988214006Smarcel	mtx_unlock(&mountlist_mtx);
989252874Savg
990252874Savg	EVENTHANDLER_INVOKE(mountroot);
991213365Smarcel}
992213365Smarcel
993213365Smarcelstatic struct mntarg *
994213365Smarcelparse_mountroot_options(struct mntarg *ma, const char *options)
995213365Smarcel{
996213365Smarcel	char *p;
997213365Smarcel	char *name, *name_arg;
998213365Smarcel	char *val, *val_arg;
999213365Smarcel	char *opts;
1000213365Smarcel
1001213365Smarcel	if (options == NULL || options[0] == '\0')
1002213365Smarcel		return (ma);
1003213365Smarcel
1004213365Smarcel	p = opts = strdup(options, M_MOUNT);
1005213365Smarcel	if (opts == NULL) {
1006213365Smarcel		return (ma);
1007214006Smarcel	}
1008213365Smarcel
1009213365Smarcel	while((name = strsep(&p, ",")) != NULL) {
1010213365Smarcel		if (name[0] == '\0')
1011213365Smarcel			break;
1012213365Smarcel
1013213365Smarcel		val = strchr(name, '=');
1014213365Smarcel		if (val != NULL) {
1015213365Smarcel			*val = '\0';
1016213365Smarcel			++val;
1017213365Smarcel		}
1018213365Smarcel		if( strcmp(name, "rw") == 0 ||
1019213365Smarcel		    strcmp(name, "noro") == 0) {
1020213365Smarcel			/*
1021213365Smarcel			 * The first time we mount the root file system,
1022213365Smarcel			 * we need to mount 'ro', so We need to ignore
1023213365Smarcel			 * 'rw' and 'noro' mount options.
1024213365Smarcel			 */
1025213365Smarcel			continue;
1026213365Smarcel		}
1027213365Smarcel		name_arg = strdup(name, M_MOUNT);
1028213365Smarcel		val_arg = NULL;
1029214006Smarcel		if (val != NULL)
1030213365Smarcel			val_arg = strdup(val, M_MOUNT);
1031213365Smarcel
1032213365Smarcel		ma = mount_arg(ma, name_arg, val_arg,
1033213365Smarcel		    (val_arg != NULL ? -1 : 0));
1034213365Smarcel	}
1035213365Smarcel	free(opts, M_MOUNT);
1036213365Smarcel	return (ma);
1037213365Smarcel}
1038