vfs_mountroot.c revision 253910
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 253910 2013-08-03 04:25:25Z marcel $");
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
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;
122253910SmarcelTUNABLE_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 __inline int
393214006Smarcelparse_isspace(int c)
394214006Smarcel{
395214006Smarcel
396214006Smarcel	return ((c == ' ' || c == '\t' || c == '\n') ? 1 : 0);
397214006Smarcel}
398214006Smarcel
399214006Smarcelstatic int
400214006Smarcelparse_skipto(char **conf, int mc)
401214006Smarcel{
402214006Smarcel	int c, match;
403214006Smarcel
404214006Smarcel	while (1) {
405214006Smarcel		c = parse_peek(conf);
406214006Smarcel		if (c == 0)
407214006Smarcel			return (PE_EOF);
408214006Smarcel		switch (mc) {
409214006Smarcel		case CC_WHITESPACE:
410214006Smarcel			match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
411214006Smarcel			break;
412214006Smarcel		case CC_NONWHITESPACE:
413214006Smarcel			if (c == '\n')
414214006Smarcel				return (PE_EOL);
415214006Smarcel			match = (c != ' ' && c != '\t') ? 1 : 0;
416214006Smarcel			break;
417214006Smarcel		default:
418214006Smarcel			match = (c == mc) ? 1 : 0;
419214006Smarcel			break;
420214006Smarcel		}
421214006Smarcel		if (match)
422214006Smarcel			break;
423214006Smarcel		parse_advance(conf);
424213365Smarcel	}
425214006Smarcel	return (0);
426214006Smarcel}
427213365Smarcel
428214006Smarcelstatic int
429214006Smarcelparse_token(char **conf, char **tok)
430214006Smarcel{
431214006Smarcel	char *p;
432214006Smarcel	size_t len;
433214006Smarcel	int error;
434214006Smarcel
435214006Smarcel	*tok = NULL;
436214006Smarcel	error = parse_skipto(conf, CC_NONWHITESPACE);
437213365Smarcel	if (error)
438214006Smarcel		return (error);
439214006Smarcel	p = *conf;
440214006Smarcel	error = parse_skipto(conf, CC_WHITESPACE);
441214006Smarcel	len = *conf - p;
442214006Smarcel	*tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
443214006Smarcel	bcopy(p, *tok, len);
444214006Smarcel	return (0);
445213365Smarcel}
446213365Smarcel
447214006Smarcelstatic void
448214006Smarcelparse_dir_ask_printenv(const char *var)
449213365Smarcel{
450214006Smarcel	char *val;
451213365Smarcel
452214006Smarcel	val = getenv(var);
453214006Smarcel	if (val != NULL) {
454214006Smarcel		printf("  %s=%s\n", var, val);
455214006Smarcel		freeenv(val);
456214006Smarcel	}
457214006Smarcel}
458213365Smarcel
459214006Smarcelstatic int
460214006Smarcelparse_dir_ask(char **conf)
461214006Smarcel{
462214006Smarcel	char name[80];
463214006Smarcel	char *mnt;
464214006Smarcel	int error;
465213365Smarcel
466214006Smarcel	printf("\nLoader variables:\n");
467214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom");
468214006Smarcel	parse_dir_ask_printenv("vfs.root.mountfrom.options");
469213365Smarcel
470214006Smarcel	printf("\nManual root filesystem specification:\n");
471214006Smarcel	printf("  <fstype>:<device> [options]\n");
472214006Smarcel	printf("      Mount <device> using filesystem <fstype>\n");
473214006Smarcel	printf("      and with the specified (optional) option list.\n");
474214006Smarcel	printf("\n");
475214006Smarcel	printf("    eg. ufs:/dev/da0s1a\n");
476214006Smarcel	printf("        zfs:tank\n");
477214006Smarcel	printf("        cd9660:/dev/acd0 ro\n");
478214006Smarcel	printf("          (which is equivalent to: ");
479214006Smarcel	printf("mount -t cd9660 -o ro /dev/acd0 /)\n");
480214006Smarcel	printf("\n");
481214006Smarcel	printf("  ?               List valid disk boot devices\n");
482214006Smarcel	printf("  .               Yield 1 second (for background tasks)\n");
483214006Smarcel	printf("  <empty line>    Abort manual input\n");
484214006Smarcel
485226673Smarcel	do {
486226673Smarcel		error = EINVAL;
487226673Smarcel		printf("\nmountroot> ");
488228634Savg		cngets(name, sizeof(name), GETS_ECHO);
489226673Smarcel		if (name[0] == '\0')
490226673Smarcel			break;
491226673Smarcel		if (name[0] == '?' && name[1] == '\0') {
492226673Smarcel			printf("\nList of GEOM managed disk devices:\n  ");
493226673Smarcel			g_dev_print();
494226673Smarcel			continue;
495226673Smarcel		}
496226673Smarcel		if (name[0] == '.' && name[1] == '\0') {
497226673Smarcel			pause("rmask", hz);
498226673Smarcel			continue;
499226673Smarcel		}
500226673Smarcel		mnt = name;
501226673Smarcel		error = parse_mount(&mnt);
502226673Smarcel		if (error == -1)
503226673Smarcel			printf("Invalid file system specification.\n");
504226673Smarcel	} while (error != 0);
505226673Smarcel
506214006Smarcel	return (error);
507214006Smarcel}
508213365Smarcel
509214006Smarcelstatic int
510214006Smarcelparse_dir_md(char **conf)
511214006Smarcel{
512214006Smarcel	struct stat sb;
513214006Smarcel	struct thread *td;
514214006Smarcel	struct md_ioctl *mdio;
515214006Smarcel	char *path, *tok;
516214006Smarcel	int error, fd, len;
517213365Smarcel
518214006Smarcel	td = curthread;
519214006Smarcel
520214006Smarcel	error = parse_token(conf, &tok);
521214006Smarcel	if (error)
522214006Smarcel		return (error);
523214006Smarcel
524214006Smarcel	len = strlen(tok);
525214006Smarcel	mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
526214006Smarcel	path = (void *)(mdio + 1);
527214006Smarcel	bcopy(tok, path, len);
528214006Smarcel	free(tok, M_TEMP);
529214006Smarcel
530214006Smarcel	/* Get file status. */
531214006Smarcel	error = kern_stat(td, path, UIO_SYSSPACE, &sb);
532214006Smarcel	if (error)
533214006Smarcel		goto out;
534214006Smarcel
535214006Smarcel	/* Open /dev/mdctl so that we can attach/detach. */
536214006Smarcel	error = kern_open(td, "/dev/" MDCTL_NAME, UIO_SYSSPACE, O_RDWR, 0);
537214006Smarcel	if (error)
538214006Smarcel		goto out;
539214006Smarcel
540214006Smarcel	fd = td->td_retval[0];
541214006Smarcel	mdio->md_version = MDIOVERSION;
542214006Smarcel	mdio->md_type = MD_VNODE;
543214006Smarcel
544214006Smarcel	if (root_mount_mddev != -1) {
545214006Smarcel		mdio->md_unit = root_mount_mddev;
546214006Smarcel		DROP_GIANT();
547214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
548214006Smarcel		PICKUP_GIANT();
549214006Smarcel		/* Ignore errors. We don't care. */
550214006Smarcel		root_mount_mddev = -1;
551214006Smarcel	}
552214006Smarcel
553214006Smarcel	mdio->md_file = (void *)(mdio + 1);
554214006Smarcel	mdio->md_options = MD_AUTOUNIT | MD_READONLY;
555214006Smarcel	mdio->md_mediasize = sb.st_size;
556214006Smarcel	mdio->md_unit = 0;
557214006Smarcel	DROP_GIANT();
558214006Smarcel	error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
559214006Smarcel	PICKUP_GIANT();
560214006Smarcel	if (error)
561214006Smarcel		goto out;
562214006Smarcel
563214006Smarcel	if (mdio->md_unit > 9) {
564214006Smarcel		printf("rootmount: too many md units\n");
565214006Smarcel		mdio->md_file = NULL;
566214006Smarcel		mdio->md_options = 0;
567214006Smarcel		mdio->md_mediasize = 0;
568214006Smarcel		DROP_GIANT();
569214006Smarcel		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
570214006Smarcel		PICKUP_GIANT();
571214006Smarcel		/* Ignore errors. We don't care. */
572214006Smarcel		error = ERANGE;
573214006Smarcel		goto out;
574214006Smarcel	}
575214006Smarcel
576214006Smarcel	root_mount_mddev = mdio->md_unit;
577214006Smarcel	printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
578214006Smarcel
579214006Smarcel	error = kern_close(td, fd);
580214006Smarcel
581214006Smarcel out:
582214006Smarcel	free(mdio, M_TEMP);
583214006Smarcel	return (error);
584214006Smarcel}
585214006Smarcel
586214006Smarcelstatic int
587214006Smarcelparse_dir_onfail(char **conf)
588214006Smarcel{
589214006Smarcel	char *action;
590214006Smarcel	int error;
591214006Smarcel
592214006Smarcel	error = parse_token(conf, &action);
593214006Smarcel	if (error)
594214006Smarcel		return (error);
595214006Smarcel
596214006Smarcel	if (!strcmp(action, "continue"))
597214006Smarcel		root_mount_onfail = A_CONTINUE;
598214006Smarcel	else if (!strcmp(action, "panic"))
599214006Smarcel		root_mount_onfail = A_PANIC;
600214006Smarcel	else if (!strcmp(action, "reboot"))
601214006Smarcel		root_mount_onfail = A_REBOOT;
602214006Smarcel	else if (!strcmp(action, "retry"))
603214006Smarcel		root_mount_onfail = A_RETRY;
604214006Smarcel	else {
605214006Smarcel		printf("rootmount: %s: unknown action\n", action);
606214006Smarcel		error = EINVAL;
607214006Smarcel	}
608214006Smarcel
609214006Smarcel	free(action, M_TEMP);
610214006Smarcel	return (0);
611214006Smarcel}
612214006Smarcel
613214006Smarcelstatic int
614214006Smarcelparse_dir_timeout(char **conf)
615214006Smarcel{
616214006Smarcel	char *tok, *endtok;
617214006Smarcel	long secs;
618214006Smarcel	int error;
619214006Smarcel
620214006Smarcel	error = parse_token(conf, &tok);
621214006Smarcel	if (error)
622214006Smarcel		return (error);
623214006Smarcel
624214006Smarcel	secs = strtol(tok, &endtok, 0);
625214006Smarcel	error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
626214006Smarcel	if (!error)
627214006Smarcel		root_mount_timeout = secs;
628214006Smarcel	free(tok, M_TEMP);
629214006Smarcel	return (error);
630214006Smarcel}
631214006Smarcel
632214006Smarcelstatic int
633214006Smarcelparse_directive(char **conf)
634214006Smarcel{
635214006Smarcel	char *dir;
636214006Smarcel	int error;
637214006Smarcel
638214006Smarcel	error = parse_token(conf, &dir);
639214006Smarcel	if (error)
640214006Smarcel		return (error);
641214006Smarcel
642214006Smarcel	if (strcmp(dir, ".ask") == 0)
643214006Smarcel		error = parse_dir_ask(conf);
644214006Smarcel	else if (strcmp(dir, ".md") == 0)
645214006Smarcel		error = parse_dir_md(conf);
646214006Smarcel	else if (strcmp(dir, ".onfail") == 0)
647214006Smarcel		error = parse_dir_onfail(conf);
648214006Smarcel	else if (strcmp(dir, ".timeout") == 0)
649214006Smarcel		error = parse_dir_timeout(conf);
650214006Smarcel	else {
651214006Smarcel		printf("mountroot: invalid directive `%s'\n", dir);
652214006Smarcel		/* Ignore the rest of the line. */
653214006Smarcel		(void)parse_skipto(conf, '\n');
654214006Smarcel		error = EINVAL;
655214006Smarcel	}
656214006Smarcel	free(dir, M_TEMP);
657214006Smarcel	return (error);
658214006Smarcel}
659214006Smarcel
660214006Smarcelstatic int
661214006Smarcelparse_mount_dev_present(const char *dev)
662214006Smarcel{
663214006Smarcel	struct nameidata nd;
664214006Smarcel	int error;
665214006Smarcel
666214006Smarcel	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
667214006Smarcel	error = namei(&nd);
668214006Smarcel	if (!error)
669214006Smarcel		vput(nd.ni_vp);
670214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
671214006Smarcel	return (error != 0) ? 0 : 1;
672214006Smarcel}
673214006Smarcel
674243868Skib#define	ERRMSGL	255
675214006Smarcelstatic int
676214006Smarcelparse_mount(char **conf)
677214006Smarcel{
678243868Skib	char *errmsg;
679214006Smarcel	struct mntarg *ma;
680214006Smarcel	char *dev, *fs, *opts, *tok;
681214006Smarcel	int delay, error, timeout;
682214006Smarcel
683214006Smarcel	error = parse_token(conf, &tok);
684214006Smarcel	if (error)
685214006Smarcel		return (error);
686214006Smarcel	fs = tok;
687214006Smarcel	error = parse_skipto(&tok, ':');
688214006Smarcel	if (error) {
689214006Smarcel		free(fs, M_TEMP);
690214006Smarcel		return (error);
691214006Smarcel	}
692214006Smarcel	parse_poke(&tok, '\0');
693214006Smarcel	parse_advance(&tok);
694214006Smarcel	dev = tok;
695214006Smarcel
696214006Smarcel	if (root_mount_mddev != -1) {
697214006Smarcel		/* Handle substitution for the md unit number. */
698214006Smarcel		tok = strstr(dev, "md#");
699214006Smarcel		if (tok != NULL)
700214006Smarcel			tok[2] = '0' + root_mount_mddev;
701214006Smarcel	}
702214006Smarcel
703214006Smarcel	/* Parse options. */
704214006Smarcel	error = parse_token(conf, &tok);
705214006Smarcel	opts = (error == 0) ? tok : NULL;
706214006Smarcel
707214006Smarcel	printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
708214006Smarcel	    (opts != NULL) ? opts : "");
709214006Smarcel
710243868Skib	errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
711214006Smarcel
712214006Smarcel	if (vfs_byname(fs) == NULL) {
713214006Smarcel		strlcpy(errmsg, "unknown file system", sizeof(errmsg));
714214006Smarcel		error = ENOENT;
715214006Smarcel		goto out;
716214006Smarcel	}
717214006Smarcel
718253847Sian	if (strcmp(fs, "zfs") != 0 && strstr(fs, "nfs") == NULL &&
719253847Sian	    dev[0] != '\0' && !parse_mount_dev_present(dev)) {
720214006Smarcel		printf("mountroot: waiting for device %s ...\n", dev);
721214006Smarcel		delay = hz / 10;
722214006Smarcel		timeout = root_mount_timeout * hz;
723214006Smarcel		do {
724214006Smarcel			pause("rmdev", delay);
725214006Smarcel			timeout -= delay;
726214006Smarcel		} while (timeout > 0 && !parse_mount_dev_present(dev));
727214006Smarcel		if (timeout <= 0) {
728214006Smarcel			error = ENODEV;
729214006Smarcel			goto out;
730214006Smarcel		}
731214006Smarcel	}
732214006Smarcel
733214006Smarcel	ma = NULL;
734214006Smarcel	ma = mount_arg(ma, "fstype", fs, -1);
735214006Smarcel	ma = mount_arg(ma, "fspath", "/", -1);
736214006Smarcel	ma = mount_arg(ma, "from", dev, -1);
737243868Skib	ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
738214006Smarcel	ma = mount_arg(ma, "ro", NULL, 0);
739214006Smarcel	ma = parse_mountroot_options(ma, opts);
740214006Smarcel	error = kernel_mount(ma, MNT_ROOTFS);
741214006Smarcel
742214006Smarcel out:
743214006Smarcel	if (error) {
744214006Smarcel		printf("Mounting from %s:%s failed with error %d",
745214006Smarcel		    fs, dev, error);
746214006Smarcel		if (errmsg[0] != '\0')
747214006Smarcel			printf(": %s", errmsg);
748214006Smarcel		printf(".\n");
749214006Smarcel	}
750214006Smarcel	free(fs, M_TEMP);
751243868Skib	free(errmsg, M_TEMP);
752214006Smarcel	if (opts != NULL)
753214006Smarcel		free(opts, M_TEMP);
754214006Smarcel	/* kernel_mount can return -1 on error. */
755214006Smarcel	return ((error < 0) ? EDOOFUS : error);
756214006Smarcel}
757243868Skib#undef ERRMSGL
758214006Smarcel
759214006Smarcelstatic int
760214006Smarcelvfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
761214006Smarcel{
762214006Smarcel	struct mount *mp;
763214006Smarcel	char *conf;
764214006Smarcel	int error;
765214006Smarcel
766214006Smarcel	root_mount_mddev = -1;
767214006Smarcel
768214006Smarcelretry:
769214006Smarcel	conf = sbuf_data(sb);
770214006Smarcel	mp = TAILQ_NEXT(mpdevfs, mnt_list);
771214006Smarcel	error = (mp == NULL) ? 0 : EDOOFUS;
772214006Smarcel	root_mount_onfail = A_CONTINUE;
773214006Smarcel	while (mp == NULL) {
774214006Smarcel		error = parse_skipto(&conf, CC_NONWHITESPACE);
775214006Smarcel		if (error == PE_EOL) {
776214006Smarcel			parse_advance(&conf);
777214006Smarcel			continue;
778214006Smarcel		}
779214006Smarcel		if (error < 0)
780214006Smarcel			break;
781214006Smarcel		switch (parse_peek(&conf)) {
782214006Smarcel		case '#':
783214006Smarcel			error = parse_skipto(&conf, '\n');
784214006Smarcel			break;
785214006Smarcel		case '.':
786214006Smarcel			error = parse_directive(&conf);
787214006Smarcel			break;
788214006Smarcel		default:
789214006Smarcel			error = parse_mount(&conf);
790214006Smarcel			break;
791214006Smarcel		}
792214006Smarcel		if (error < 0)
793214006Smarcel			break;
794214006Smarcel		/* Ignore any trailing garbage on the line. */
795214006Smarcel		if (parse_peek(&conf) != '\n') {
796214006Smarcel			printf("mountroot: advancing to next directive...\n");
797214006Smarcel			(void)parse_skipto(&conf, '\n');
798214006Smarcel		}
799214006Smarcel		mp = TAILQ_NEXT(mpdevfs, mnt_list);
800214006Smarcel	}
801214006Smarcel	if (mp != NULL)
802214006Smarcel		return (0);
803214006Smarcel
804213365Smarcel	/*
805214006Smarcel	 * We failed to mount (a new) root.
806213365Smarcel	 */
807214006Smarcel	switch (root_mount_onfail) {
808214006Smarcel	case A_CONTINUE:
809214006Smarcel		break;
810214006Smarcel	case A_PANIC:
811214006Smarcel		panic("mountroot: unable to (re-)mount root.");
812214006Smarcel		/* NOTREACHED */
813214006Smarcel	case A_RETRY:
814214006Smarcel		goto retry;
815214006Smarcel	case A_REBOOT:
816214006Smarcel		kern_reboot(RB_NOSYNC);
817214006Smarcel		/* NOTREACHED */
818213365Smarcel	}
819213365Smarcel
820214006Smarcel	return (error);
821214006Smarcel}
822214006Smarcel
823214006Smarcelstatic void
824214006Smarcelvfs_mountroot_conf0(struct sbuf *sb)
825214006Smarcel{
826214006Smarcel	char *s, *tok, *mnt, *opt;
827214006Smarcel	int error;
828214006Smarcel
829214006Smarcel	sbuf_printf(sb, ".onfail panic\n");
830214006Smarcel	sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
831214006Smarcel	if (boothowto & RB_ASKNAME)
832214006Smarcel		sbuf_printf(sb, ".ask\n");
833214006Smarcel#ifdef ROOTDEVNAME
834214006Smarcel	if (boothowto & RB_DFLTROOT)
835214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
836214006Smarcel#endif
837213365Smarcel	if (boothowto & RB_CDROM) {
838217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
839214006Smarcel		sbuf_printf(sb, ".timeout 0\n");
840217163Snwhitehorn		sbuf_printf(sb, "cd9660:/dev/acd0 ro\n");
841214006Smarcel		sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
842214006Smarcel	}
843214006Smarcel	s = getenv("vfs.root.mountfrom");
844214006Smarcel	if (s != NULL) {
845214006Smarcel		opt = getenv("vfs.root.mountfrom.options");
846214006Smarcel		tok = s;
847214006Smarcel		error = parse_token(&tok, &mnt);
848214006Smarcel		while (!error) {
849214006Smarcel			sbuf_printf(sb, "%s %s\n", mnt,
850214006Smarcel			    (opt != NULL) ? opt : "");
851214006Smarcel			free(mnt, M_TEMP);
852214006Smarcel			error = parse_token(&tok, &mnt);
853213365Smarcel		}
854214006Smarcel		if (opt != NULL)
855214006Smarcel			freeenv(opt);
856214006Smarcel		freeenv(s);
857213365Smarcel	}
858214006Smarcel	if (rootdevnames[0] != NULL)
859214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[0]);
860214006Smarcel	if (rootdevnames[1] != NULL)
861214006Smarcel		sbuf_printf(sb, "%s\n", rootdevnames[1]);
862214006Smarcel#ifdef ROOTDEVNAME
863214006Smarcel	if (!(boothowto & RB_DFLTROOT))
864214006Smarcel		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
865214006Smarcel#endif
866214006Smarcel	if (!(boothowto & RB_ASKNAME))
867214006Smarcel		sbuf_printf(sb, ".ask\n");
868214006Smarcel}
869213365Smarcel
870214006Smarcelstatic int
871214006Smarcelvfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
872214006Smarcel{
873214006Smarcel	static char buf[128];
874214006Smarcel	struct nameidata nd;
875214006Smarcel	off_t ofs;
876231949Skib	ssize_t resid;
877241896Skib	int error, flags, len;
878214006Smarcel
879241896Skib	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
880214006Smarcel	flags = FREAD;
881214006Smarcel	error = vn_open(&nd, &flags, 0, NULL);
882214006Smarcel	if (error)
883214006Smarcel		return (error);
884214006Smarcel
885214006Smarcel	NDFREE(&nd, NDF_ONLY_PNBUF);
886214006Smarcel	ofs = 0;
887214006Smarcel	len = sizeof(buf) - 1;
888214006Smarcel	while (1) {
889214006Smarcel		error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
890214006Smarcel		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
891214006Smarcel		    NOCRED, &resid, td);
892214006Smarcel		if (error)
893214006Smarcel			break;
894214006Smarcel		if (resid == len)
895214006Smarcel			break;
896214006Smarcel		buf[len - resid] = 0;
897214006Smarcel		sbuf_printf(sb, "%s", buf);
898214006Smarcel		ofs += len - resid;
899214006Smarcel	}
900214006Smarcel
901214006Smarcel	VOP_UNLOCK(nd.ni_vp, 0);
902214006Smarcel	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
903214006Smarcel	return (error);
904214006Smarcel}
905214006Smarcel
906214006Smarcelstatic void
907214006Smarcelvfs_mountroot_wait(void)
908214006Smarcel{
909214006Smarcel	struct root_hold_token *h;
910214006Smarcel	struct timeval lastfail;
911214006Smarcel	int curfail;
912214006Smarcel
913214006Smarcel	curfail = 0;
914214006Smarcel	while (1) {
915214006Smarcel		DROP_GIANT();
916214006Smarcel		g_waitidle();
917214006Smarcel		PICKUP_GIANT();
918214006Smarcel		mtx_lock(&mountlist_mtx);
919214006Smarcel		if (LIST_EMPTY(&root_holds)) {
920214006Smarcel			mtx_unlock(&mountlist_mtx);
921214006Smarcel			break;
922214006Smarcel		}
923214006Smarcel		if (ppsratecheck(&lastfail, &curfail, 1)) {
924214006Smarcel			printf("Root mount waiting for:");
925214006Smarcel			LIST_FOREACH(h, &root_holds, list)
926214006Smarcel				printf(" %s", h->who);
927214006Smarcel			printf("\n");
928214006Smarcel		}
929214006Smarcel		msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
930214006Smarcel		    hz);
931214006Smarcel	}
932214006Smarcel}
933214006Smarcel
934214006Smarcelvoid
935214006Smarcelvfs_mountroot(void)
936214006Smarcel{
937214006Smarcel	struct mount *mp;
938214006Smarcel	struct sbuf *sb;
939214006Smarcel	struct thread *td;
940214006Smarcel	time_t timebase;
941214006Smarcel	int error;
942214006Smarcel
943214006Smarcel	td = curthread;
944214006Smarcel
945214006Smarcel	vfs_mountroot_wait();
946214006Smarcel
947214006Smarcel	sb = sbuf_new_auto();
948214006Smarcel	vfs_mountroot_conf0(sb);
949214006Smarcel	sbuf_finish(sb);
950214006Smarcel
951214006Smarcel	error = vfs_mountroot_devfs(td, &mp);
952214006Smarcel	while (!error) {
953214006Smarcel		error = vfs_mountroot_parse(sb, mp);
954214006Smarcel		if (!error) {
955214006Smarcel			error = vfs_mountroot_shuffle(td, mp);
956214006Smarcel			if (!error) {
957214006Smarcel				sbuf_clear(sb);
958214006Smarcel				error = vfs_mountroot_readconf(td, sb);
959214006Smarcel				sbuf_finish(sb);
960213365Smarcel			}
961213365Smarcel		}
962213365Smarcel	}
963213365Smarcel
964214006Smarcel	sbuf_delete(sb);
965213365Smarcel
966213365Smarcel	/*
967214006Smarcel	 * Iterate over all currently mounted file systems and use
968214006Smarcel	 * the time stamp found to check and/or initialize the RTC.
969214006Smarcel	 * Call inittodr() only once and pass it the largest of the
970214006Smarcel	 * timestamps we encounter.
971213365Smarcel	 */
972214006Smarcel	timebase = 0;
973214006Smarcel	mtx_lock(&mountlist_mtx);
974214006Smarcel	mp = TAILQ_FIRST(&mountlist);
975214006Smarcel	while (mp != NULL) {
976214006Smarcel		if (mp->mnt_time > timebase)
977214006Smarcel			timebase = mp->mnt_time;
978214006Smarcel		mp = TAILQ_NEXT(mp, mnt_list);
979214006Smarcel	}
980214006Smarcel	mtx_unlock(&mountlist_mtx);
981214006Smarcel	inittodr(timebase);
982213365Smarcel
983214006Smarcel	/* Keep prison0's root in sync with the global rootvnode. */
984214006Smarcel	mtx_lock(&prison0.pr_mtx);
985214006Smarcel	prison0.pr_root = rootvnode;
986214006Smarcel	vref(prison0.pr_root);
987214006Smarcel	mtx_unlock(&prison0.pr_mtx);
988213365Smarcel
989214006Smarcel	mtx_lock(&mountlist_mtx);
990214006Smarcel	atomic_store_rel_int(&root_mount_complete, 1);
991214006Smarcel	wakeup(&root_mount_complete);
992214006Smarcel	mtx_unlock(&mountlist_mtx);
993248645Savg
994248645Savg	EVENTHANDLER_INVOKE(mountroot);
995213365Smarcel}
996213365Smarcel
997213365Smarcelstatic struct mntarg *
998213365Smarcelparse_mountroot_options(struct mntarg *ma, const char *options)
999213365Smarcel{
1000213365Smarcel	char *p;
1001213365Smarcel	char *name, *name_arg;
1002213365Smarcel	char *val, *val_arg;
1003213365Smarcel	char *opts;
1004213365Smarcel
1005213365Smarcel	if (options == NULL || options[0] == '\0')
1006213365Smarcel		return (ma);
1007213365Smarcel
1008213365Smarcel	p = opts = strdup(options, M_MOUNT);
1009213365Smarcel	if (opts == NULL) {
1010213365Smarcel		return (ma);
1011214006Smarcel	}
1012213365Smarcel
1013213365Smarcel	while((name = strsep(&p, ",")) != NULL) {
1014213365Smarcel		if (name[0] == '\0')
1015213365Smarcel			break;
1016213365Smarcel
1017213365Smarcel		val = strchr(name, '=');
1018213365Smarcel		if (val != NULL) {
1019213365Smarcel			*val = '\0';
1020213365Smarcel			++val;
1021213365Smarcel		}
1022213365Smarcel		if( strcmp(name, "rw") == 0 ||
1023213365Smarcel		    strcmp(name, "noro") == 0) {
1024213365Smarcel			/*
1025213365Smarcel			 * The first time we mount the root file system,
1026213365Smarcel			 * we need to mount 'ro', so We need to ignore
1027213365Smarcel			 * 'rw' and 'noro' mount options.
1028213365Smarcel			 */
1029213365Smarcel			continue;
1030213365Smarcel		}
1031213365Smarcel		name_arg = strdup(name, M_MOUNT);
1032213365Smarcel		val_arg = NULL;
1033214006Smarcel		if (val != NULL)
1034213365Smarcel			val_arg = strdup(val, M_MOUNT);
1035213365Smarcel
1036213365Smarcel		ma = mount_arg(ma, name_arg, val_arg,
1037213365Smarcel		    (val_arg != NULL ? -1 : 0));
1038213365Smarcel	}
1039213365Smarcel	free(opts, M_MOUNT);
1040213365Smarcel	return (ma);
1041213365Smarcel}
1042