xref: /freebsd/sys/kern/vfs_mountroot.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2010 Marcel Moolenaar
3  * Copyright (c) 1999-2004 Poul-Henning Kamp
4  * Copyright (c) 1999 Michael Smith
5  * Copyright (c) 1989, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include "opt_rootdevname.h"
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/conf.h>
45 #include <sys/cons.h>
46 #include <sys/fcntl.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mdioctl.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/namei.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/filedesc.h>
57 #include <sys/reboot.h>
58 #include <sys/sbuf.h>
59 #include <sys/stat.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysproto.h>
62 #include <sys/sx.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/systm.h>
66 #include <sys/vnode.h>
67 
68 #include <geom/geom.h>
69 
70 /*
71  * The root filesystem is detailed in the kernel environment variable
72  * vfs.root.mountfrom, which is expected to be in the general format
73  *
74  * <vfsname>:[<path>][	<vfsname>:[<path>] ...]
75  * vfsname   := the name of a VFS known to the kernel and capable
76  *              of being mounted as root
77  * path      := disk device name or other data used by the filesystem
78  *              to locate its physical store
79  *
80  * If the environment variable vfs.root.mountfrom is a space separated list,
81  * each list element is tried in turn and the root filesystem will be mounted
82  * from the first one that suceeds.
83  *
84  * The environment variable vfs.root.mountfrom.options is a comma delimited
85  * set of string mount options.  These mount options must be parseable
86  * by nmount() in the kernel.
87  */
88 
89 static int parse_mount(char **);
90 static struct mntarg *parse_mountroot_options(struct mntarg *, const char *);
91 static int sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS);
92 static int vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev);
93 
94 /*
95  * The vnode of the system's root (/ in the filesystem, without chroot
96  * active.)
97  */
98 struct vnode *rootvnode;
99 
100 /*
101  * Mount of the system's /dev.
102  */
103 struct mount *rootdevmp;
104 
105 char *rootdevnames[2] = {NULL, NULL};
106 
107 struct mtx root_holds_mtx;
108 MTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
109 
110 struct root_hold_token {
111 	const char			*who;
112 	LIST_ENTRY(root_hold_token)	list;
113 };
114 
115 static LIST_HEAD(, root_hold_token)	root_holds =
116     LIST_HEAD_INITIALIZER(root_holds);
117 
118 enum action {
119 	A_CONTINUE,
120 	A_PANIC,
121 	A_REBOOT,
122 	A_RETRY
123 };
124 
125 static enum action root_mount_onfail = A_CONTINUE;
126 
127 static int root_mount_mddev;
128 static int root_mount_complete;
129 
130 /* By default wait up to 3 seconds for devices to appear. */
131 static int root_mount_timeout = 3;
132 TUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
133 
134 SYSCTL_PROC(_vfs, OID_AUTO, root_mount_hold,
135     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
136     NULL, 0, sysctl_vfs_root_mount_hold, "A",
137     "List of root mount hold tokens");
138 
139 static int
140 sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS)
141 {
142 	struct sbuf sb;
143 	struct root_hold_token *h;
144 	int error;
145 
146 	sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
147 
148 	mtx_lock(&root_holds_mtx);
149 	LIST_FOREACH(h, &root_holds, list) {
150 		if (h != LIST_FIRST(&root_holds))
151 			sbuf_putc(&sb, ' ');
152 		sbuf_printf(&sb, "%s", h->who);
153 	}
154 	mtx_unlock(&root_holds_mtx);
155 
156 	error = sbuf_finish(&sb);
157 	if (error == 0)
158 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
159 	sbuf_delete(&sb);
160 	return (error);
161 }
162 
163 struct root_hold_token *
164 root_mount_hold(const char *identifier)
165 {
166 	struct root_hold_token *h;
167 
168 	if (root_mounted())
169 		return (NULL);
170 
171 	h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
172 	h->who = identifier;
173 	mtx_lock(&root_holds_mtx);
174 	LIST_INSERT_HEAD(&root_holds, h, list);
175 	mtx_unlock(&root_holds_mtx);
176 	return (h);
177 }
178 
179 void
180 root_mount_rel(struct root_hold_token *h)
181 {
182 
183 	if (h == NULL)
184 		return;
185 	mtx_lock(&root_holds_mtx);
186 	LIST_REMOVE(h, list);
187 	wakeup(&root_holds);
188 	mtx_unlock(&root_holds_mtx);
189 	free(h, M_DEVBUF);
190 }
191 
192 int
193 root_mounted(void)
194 {
195 
196 	/* No mutex is acquired here because int stores are atomic. */
197 	return (root_mount_complete);
198 }
199 
200 static void
201 set_rootvnode(void)
202 {
203 	struct proc *p;
204 
205 	if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
206 		panic("Cannot find root vnode");
207 
208 	VOP_UNLOCK(rootvnode, 0);
209 
210 	p = curthread->td_proc;
211 	FILEDESC_XLOCK(p->p_fd);
212 
213 	if (p->p_fd->fd_cdir != NULL)
214 		vrele(p->p_fd->fd_cdir);
215 	p->p_fd->fd_cdir = rootvnode;
216 	VREF(rootvnode);
217 
218 	if (p->p_fd->fd_rdir != NULL)
219 		vrele(p->p_fd->fd_rdir);
220 	p->p_fd->fd_rdir = rootvnode;
221 	VREF(rootvnode);
222 
223 	FILEDESC_XUNLOCK(p->p_fd);
224 }
225 
226 static int
227 vfs_mountroot_devfs(struct thread *td, struct mount **mpp)
228 {
229 	struct vfsoptlist *opts;
230 	struct vfsconf *vfsp;
231 	struct mount *mp;
232 	int error;
233 
234 	*mpp = NULL;
235 
236 	if (rootdevmp != NULL) {
237 		/*
238 		 * Already have /dev; this happens during rerooting.
239 		 */
240 		error = vfs_busy(rootdevmp, 0);
241 		if (error != 0)
242 			return (error);
243 		*mpp = rootdevmp;
244 	} else {
245 		vfsp = vfs_byname("devfs");
246 		KASSERT(vfsp != NULL, ("Could not find devfs by name"));
247 		if (vfsp == NULL)
248 			return (ENOENT);
249 
250 		mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
251 
252 		error = VFS_MOUNT(mp);
253 		KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
254 		if (error)
255 			return (error);
256 
257 		opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
258 		TAILQ_INIT(opts);
259 		mp->mnt_opt = opts;
260 
261 		mtx_lock(&mountlist_mtx);
262 		TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
263 		mtx_unlock(&mountlist_mtx);
264 
265 		*mpp = mp;
266 		rootdevmp = mp;
267 	}
268 
269 	set_rootvnode();
270 
271 	error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
272 	if (error)
273 		printf("kern_symlink /dev -> / returns %d\n", error);
274 
275 	return (error);
276 }
277 
278 static void
279 vfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
280 {
281 	struct nameidata nd;
282 	struct mount *mporoot, *mpnroot;
283 	struct vnode *vp, *vporoot, *vpdevfs;
284 	char *fspath;
285 	int error;
286 
287 	mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
288 
289 	/* Shuffle the mountlist. */
290 	mtx_lock(&mountlist_mtx);
291 	mporoot = TAILQ_FIRST(&mountlist);
292 	TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
293 	if (mporoot != mpdevfs) {
294 		TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
295 		TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
296 	}
297 	TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
298 	mtx_unlock(&mountlist_mtx);
299 
300 	cache_purgevfs(mporoot);
301 	if (mporoot != mpdevfs)
302 		cache_purgevfs(mpdevfs);
303 
304 	VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot);
305 
306 	VI_LOCK(vporoot);
307 	vporoot->v_iflag &= ~VI_MOUNT;
308 	VI_UNLOCK(vporoot);
309 	vporoot->v_mountedhere = NULL;
310 	mporoot->mnt_flag &= ~MNT_ROOTFS;
311 	mporoot->mnt_vnodecovered = NULL;
312 	vput(vporoot);
313 
314 	/* Set up the new rootvnode, and purge the cache */
315 	mpnroot->mnt_vnodecovered = NULL;
316 	set_rootvnode();
317 	cache_purgevfs(rootvnode->v_mount);
318 
319 	if (mporoot != mpdevfs) {
320 		/* Remount old root under /.mount or /mnt */
321 		fspath = "/.mount";
322 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
323 		    fspath, td);
324 		error = namei(&nd);
325 		if (error) {
326 			NDFREE(&nd, NDF_ONLY_PNBUF);
327 			fspath = "/mnt";
328 			NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
329 			    fspath, td);
330 			error = namei(&nd);
331 		}
332 		if (!error) {
333 			vp = nd.ni_vp;
334 			error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
335 			if (!error)
336 				error = vinvalbuf(vp, V_SAVE, 0, 0);
337 			if (!error) {
338 				cache_purge(vp);
339 				mporoot->mnt_vnodecovered = vp;
340 				vp->v_mountedhere = mporoot;
341 				strlcpy(mporoot->mnt_stat.f_mntonname,
342 				    fspath, MNAMELEN);
343 				VOP_UNLOCK(vp, 0);
344 			} else
345 				vput(vp);
346 		}
347 		NDFREE(&nd, NDF_ONLY_PNBUF);
348 
349 		if (error && bootverbose)
350 			printf("mountroot: unable to remount previous root "
351 			    "under /.mount or /mnt (error %d).\n", error);
352 	}
353 
354 	/* Remount devfs under /dev */
355 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
356 	error = namei(&nd);
357 	if (!error) {
358 		vp = nd.ni_vp;
359 		error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
360 		if (!error)
361 			error = vinvalbuf(vp, V_SAVE, 0, 0);
362 		if (!error) {
363 			vpdevfs = mpdevfs->mnt_vnodecovered;
364 			if (vpdevfs != NULL) {
365 				cache_purge(vpdevfs);
366 				vpdevfs->v_mountedhere = NULL;
367 				vrele(vpdevfs);
368 			}
369 			mpdevfs->mnt_vnodecovered = vp;
370 			vp->v_mountedhere = mpdevfs;
371 			VOP_UNLOCK(vp, 0);
372 		} else
373 			vput(vp);
374 	}
375 	if (error && bootverbose)
376 		printf("mountroot: unable to remount devfs under /dev "
377 		    "(error %d).\n", error);
378 	NDFREE(&nd, NDF_ONLY_PNBUF);
379 
380 	if (mporoot == mpdevfs) {
381 		vfs_unbusy(mpdevfs);
382 		/* Unlink the no longer needed /dev/dev -> / symlink */
383 		error = kern_unlinkat(td, AT_FDCWD, "/dev/dev",
384 		    UIO_SYSSPACE, 0);
385 		if (error && bootverbose)
386 			printf("mountroot: unable to unlink /dev/dev "
387 			    "(error %d)\n", error);
388 	}
389 }
390 
391 /*
392  * Configuration parser.
393  */
394 
395 /* Parser character classes. */
396 #define	CC_WHITESPACE		-1
397 #define	CC_NONWHITESPACE	-2
398 
399 /* Parse errors. */
400 #define	PE_EOF			-1
401 #define	PE_EOL			-2
402 
403 static __inline int
404 parse_peek(char **conf)
405 {
406 
407 	return (**conf);
408 }
409 
410 static __inline void
411 parse_poke(char **conf, int c)
412 {
413 
414 	**conf = c;
415 }
416 
417 static __inline void
418 parse_advance(char **conf)
419 {
420 
421 	(*conf)++;
422 }
423 
424 static int
425 parse_skipto(char **conf, int mc)
426 {
427 	int c, match;
428 
429 	while (1) {
430 		c = parse_peek(conf);
431 		if (c == 0)
432 			return (PE_EOF);
433 		switch (mc) {
434 		case CC_WHITESPACE:
435 			match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
436 			break;
437 		case CC_NONWHITESPACE:
438 			if (c == '\n')
439 				return (PE_EOL);
440 			match = (c != ' ' && c != '\t') ? 1 : 0;
441 			break;
442 		default:
443 			match = (c == mc) ? 1 : 0;
444 			break;
445 		}
446 		if (match)
447 			break;
448 		parse_advance(conf);
449 	}
450 	return (0);
451 }
452 
453 static int
454 parse_token(char **conf, char **tok)
455 {
456 	char *p;
457 	size_t len;
458 	int error;
459 
460 	*tok = NULL;
461 	error = parse_skipto(conf, CC_NONWHITESPACE);
462 	if (error)
463 		return (error);
464 	p = *conf;
465 	error = parse_skipto(conf, CC_WHITESPACE);
466 	len = *conf - p;
467 	*tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
468 	bcopy(p, *tok, len);
469 	return (0);
470 }
471 
472 static void
473 parse_dir_ask_printenv(const char *var)
474 {
475 	char *val;
476 
477 	val = kern_getenv(var);
478 	if (val != NULL) {
479 		printf("  %s=%s\n", var, val);
480 		freeenv(val);
481 	}
482 }
483 
484 static int
485 parse_dir_ask(char **conf)
486 {
487 	char name[80];
488 	char *mnt;
489 	int error;
490 
491 	printf("\nLoader variables:\n");
492 	parse_dir_ask_printenv("vfs.root.mountfrom");
493 	parse_dir_ask_printenv("vfs.root.mountfrom.options");
494 
495 	printf("\nManual root filesystem specification:\n");
496 	printf("  <fstype>:<device> [options]\n");
497 	printf("      Mount <device> using filesystem <fstype>\n");
498 	printf("      and with the specified (optional) option list.\n");
499 	printf("\n");
500 	printf("    eg. ufs:/dev/da0s1a\n");
501 	printf("        zfs:tank\n");
502 	printf("        cd9660:/dev/cd0 ro\n");
503 	printf("          (which is equivalent to: ");
504 	printf("mount -t cd9660 -o ro /dev/cd0 /)\n");
505 	printf("\n");
506 	printf("  ?               List valid disk boot devices\n");
507 	printf("  .               Yield 1 second (for background tasks)\n");
508 	printf("  <empty line>    Abort manual input\n");
509 
510 	do {
511 		error = EINVAL;
512 		printf("\nmountroot> ");
513 		cngets(name, sizeof(name), GETS_ECHO);
514 		if (name[0] == '\0')
515 			break;
516 		if (name[0] == '?' && name[1] == '\0') {
517 			printf("\nList of GEOM managed disk devices:\n  ");
518 			g_dev_print();
519 			continue;
520 		}
521 		if (name[0] == '.' && name[1] == '\0') {
522 			pause("rmask", hz);
523 			continue;
524 		}
525 		mnt = name;
526 		error = parse_mount(&mnt);
527 		if (error == -1)
528 			printf("Invalid file system specification.\n");
529 	} while (error != 0);
530 
531 	return (error);
532 }
533 
534 static int
535 parse_dir_md(char **conf)
536 {
537 	struct stat sb;
538 	struct thread *td;
539 	struct md_ioctl *mdio;
540 	char *path, *tok;
541 	int error, fd, len;
542 
543 	td = curthread;
544 
545 	error = parse_token(conf, &tok);
546 	if (error)
547 		return (error);
548 
549 	len = strlen(tok);
550 	mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
551 	path = (void *)(mdio + 1);
552 	bcopy(tok, path, len);
553 	free(tok, M_TEMP);
554 
555 	/* Get file status. */
556 	error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL);
557 	if (error)
558 		goto out;
559 
560 	/* Open /dev/mdctl so that we can attach/detach. */
561 	error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
562 	    O_RDWR, 0);
563 	if (error)
564 		goto out;
565 
566 	fd = td->td_retval[0];
567 	mdio->md_version = MDIOVERSION;
568 	mdio->md_type = MD_VNODE;
569 
570 	if (root_mount_mddev != -1) {
571 		mdio->md_unit = root_mount_mddev;
572 		DROP_GIANT();
573 		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
574 		PICKUP_GIANT();
575 		/* Ignore errors. We don't care. */
576 		root_mount_mddev = -1;
577 	}
578 
579 	mdio->md_file = (void *)(mdio + 1);
580 	mdio->md_options = MD_AUTOUNIT | MD_READONLY;
581 	mdio->md_mediasize = sb.st_size;
582 	mdio->md_unit = 0;
583 	DROP_GIANT();
584 	error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
585 	PICKUP_GIANT();
586 	if (error)
587 		goto out;
588 
589 	if (mdio->md_unit > 9) {
590 		printf("rootmount: too many md units\n");
591 		mdio->md_file = NULL;
592 		mdio->md_options = 0;
593 		mdio->md_mediasize = 0;
594 		DROP_GIANT();
595 		error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
596 		PICKUP_GIANT();
597 		/* Ignore errors. We don't care. */
598 		error = ERANGE;
599 		goto out;
600 	}
601 
602 	root_mount_mddev = mdio->md_unit;
603 	printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
604 
605 	error = kern_close(td, fd);
606 
607  out:
608 	free(mdio, M_TEMP);
609 	return (error);
610 }
611 
612 static int
613 parse_dir_onfail(char **conf)
614 {
615 	char *action;
616 	int error;
617 
618 	error = parse_token(conf, &action);
619 	if (error)
620 		return (error);
621 
622 	if (!strcmp(action, "continue"))
623 		root_mount_onfail = A_CONTINUE;
624 	else if (!strcmp(action, "panic"))
625 		root_mount_onfail = A_PANIC;
626 	else if (!strcmp(action, "reboot"))
627 		root_mount_onfail = A_REBOOT;
628 	else if (!strcmp(action, "retry"))
629 		root_mount_onfail = A_RETRY;
630 	else {
631 		printf("rootmount: %s: unknown action\n", action);
632 		error = EINVAL;
633 	}
634 
635 	free(action, M_TEMP);
636 	return (0);
637 }
638 
639 static int
640 parse_dir_timeout(char **conf)
641 {
642 	char *tok, *endtok;
643 	long secs;
644 	int error;
645 
646 	error = parse_token(conf, &tok);
647 	if (error)
648 		return (error);
649 
650 	secs = strtol(tok, &endtok, 0);
651 	error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
652 	if (!error)
653 		root_mount_timeout = secs;
654 	free(tok, M_TEMP);
655 	return (error);
656 }
657 
658 static int
659 parse_directive(char **conf)
660 {
661 	char *dir;
662 	int error;
663 
664 	error = parse_token(conf, &dir);
665 	if (error)
666 		return (error);
667 
668 	if (strcmp(dir, ".ask") == 0)
669 		error = parse_dir_ask(conf);
670 	else if (strcmp(dir, ".md") == 0)
671 		error = parse_dir_md(conf);
672 	else if (strcmp(dir, ".onfail") == 0)
673 		error = parse_dir_onfail(conf);
674 	else if (strcmp(dir, ".timeout") == 0)
675 		error = parse_dir_timeout(conf);
676 	else {
677 		printf("mountroot: invalid directive `%s'\n", dir);
678 		/* Ignore the rest of the line. */
679 		(void)parse_skipto(conf, '\n');
680 		error = EINVAL;
681 	}
682 	free(dir, M_TEMP);
683 	return (error);
684 }
685 
686 static int
687 parse_mount_dev_present(const char *dev)
688 {
689 	struct nameidata nd;
690 	int error;
691 
692 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
693 	error = namei(&nd);
694 	if (!error)
695 		vput(nd.ni_vp);
696 	NDFREE(&nd, NDF_ONLY_PNBUF);
697 	return (error != 0) ? 0 : 1;
698 }
699 
700 #define	ERRMSGL	255
701 static int
702 parse_mount(char **conf)
703 {
704 	char *errmsg;
705 	struct mntarg *ma;
706 	char *dev, *fs, *opts, *tok;
707 	int error;
708 
709 	error = parse_token(conf, &tok);
710 	if (error)
711 		return (error);
712 	fs = tok;
713 	error = parse_skipto(&tok, ':');
714 	if (error) {
715 		free(fs, M_TEMP);
716 		return (error);
717 	}
718 	parse_poke(&tok, '\0');
719 	parse_advance(&tok);
720 	dev = tok;
721 
722 	if (root_mount_mddev != -1) {
723 		/* Handle substitution for the md unit number. */
724 		tok = strstr(dev, "md#");
725 		if (tok != NULL)
726 			tok[2] = '0' + root_mount_mddev;
727 	}
728 
729 	/* Parse options. */
730 	error = parse_token(conf, &tok);
731 	opts = (error == 0) ? tok : NULL;
732 
733 	printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
734 	    (opts != NULL) ? opts : "");
735 
736 	errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
737 
738 	if (vfs_byname(fs) == NULL) {
739 		strlcpy(errmsg, "unknown file system", ERRMSGL);
740 		error = ENOENT;
741 		goto out;
742 	}
743 
744 	error = vfs_mountroot_wait_if_neccessary(fs, dev);
745 	if (error != 0)
746 		goto out;
747 
748 	ma = NULL;
749 	ma = mount_arg(ma, "fstype", fs, -1);
750 	ma = mount_arg(ma, "fspath", "/", -1);
751 	ma = mount_arg(ma, "from", dev, -1);
752 	ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
753 	ma = mount_arg(ma, "ro", NULL, 0);
754 	ma = parse_mountroot_options(ma, opts);
755 	error = kernel_mount(ma, MNT_ROOTFS);
756 
757  out:
758 	if (error) {
759 		printf("Mounting from %s:%s failed with error %d",
760 		    fs, dev, error);
761 		if (errmsg[0] != '\0')
762 			printf(": %s", errmsg);
763 		printf(".\n");
764 	}
765 	free(fs, M_TEMP);
766 	free(errmsg, M_TEMP);
767 	if (opts != NULL)
768 		free(opts, M_TEMP);
769 	/* kernel_mount can return -1 on error. */
770 	return ((error < 0) ? EDOOFUS : error);
771 }
772 #undef ERRMSGL
773 
774 static int
775 vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
776 {
777 	struct mount *mp;
778 	char *conf;
779 	int error;
780 
781 	root_mount_mddev = -1;
782 
783 retry:
784 	conf = sbuf_data(sb);
785 	mp = TAILQ_NEXT(mpdevfs, mnt_list);
786 	error = (mp == NULL) ? 0 : EDOOFUS;
787 	root_mount_onfail = A_CONTINUE;
788 	while (mp == NULL) {
789 		error = parse_skipto(&conf, CC_NONWHITESPACE);
790 		if (error == PE_EOL) {
791 			parse_advance(&conf);
792 			continue;
793 		}
794 		if (error < 0)
795 			break;
796 		switch (parse_peek(&conf)) {
797 		case '#':
798 			error = parse_skipto(&conf, '\n');
799 			break;
800 		case '.':
801 			error = parse_directive(&conf);
802 			break;
803 		default:
804 			error = parse_mount(&conf);
805 			if (error == -1) {
806 				printf("mountroot: invalid file system "
807 				    "specification.\n");
808 				error = 0;
809 			}
810 			break;
811 		}
812 		if (error < 0)
813 			break;
814 		/* Ignore any trailing garbage on the line. */
815 		if (parse_peek(&conf) != '\n') {
816 			printf("mountroot: advancing to next directive...\n");
817 			(void)parse_skipto(&conf, '\n');
818 		}
819 		mp = TAILQ_NEXT(mpdevfs, mnt_list);
820 	}
821 	if (mp != NULL)
822 		return (0);
823 
824 	/*
825 	 * We failed to mount (a new) root.
826 	 */
827 	switch (root_mount_onfail) {
828 	case A_CONTINUE:
829 		break;
830 	case A_PANIC:
831 		panic("mountroot: unable to (re-)mount root.");
832 		/* NOTREACHED */
833 	case A_RETRY:
834 		goto retry;
835 	case A_REBOOT:
836 		kern_reboot(RB_NOSYNC);
837 		/* NOTREACHED */
838 	}
839 
840 	return (error);
841 }
842 
843 static void
844 vfs_mountroot_conf0(struct sbuf *sb)
845 {
846 	char *s, *tok, *mnt, *opt;
847 	int error;
848 
849 	sbuf_printf(sb, ".onfail panic\n");
850 	sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
851 	if (boothowto & RB_ASKNAME)
852 		sbuf_printf(sb, ".ask\n");
853 #ifdef ROOTDEVNAME
854 	if (boothowto & RB_DFLTROOT)
855 		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
856 #endif
857 	if (boothowto & RB_CDROM) {
858 		sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
859 		sbuf_printf(sb, ".timeout 0\n");
860 		sbuf_printf(sb, "cd9660:/dev/cd1 ro\n");
861 		sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
862 	}
863 	s = kern_getenv("vfs.root.mountfrom");
864 	if (s != NULL) {
865 		opt = kern_getenv("vfs.root.mountfrom.options");
866 		tok = s;
867 		error = parse_token(&tok, &mnt);
868 		while (!error) {
869 			sbuf_printf(sb, "%s %s\n", mnt,
870 			    (opt != NULL) ? opt : "");
871 			free(mnt, M_TEMP);
872 			error = parse_token(&tok, &mnt);
873 		}
874 		if (opt != NULL)
875 			freeenv(opt);
876 		freeenv(s);
877 	}
878 	if (rootdevnames[0] != NULL)
879 		sbuf_printf(sb, "%s\n", rootdevnames[0]);
880 	if (rootdevnames[1] != NULL)
881 		sbuf_printf(sb, "%s\n", rootdevnames[1]);
882 #ifdef ROOTDEVNAME
883 	if (!(boothowto & RB_DFLTROOT))
884 		sbuf_printf(sb, "%s\n", ROOTDEVNAME);
885 #endif
886 	if (!(boothowto & RB_ASKNAME))
887 		sbuf_printf(sb, ".ask\n");
888 }
889 
890 static int
891 vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
892 {
893 	static char buf[128];
894 	struct nameidata nd;
895 	off_t ofs;
896 	ssize_t resid;
897 	int error, flags, len;
898 
899 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
900 	flags = FREAD;
901 	error = vn_open(&nd, &flags, 0, NULL);
902 	if (error)
903 		return (error);
904 
905 	NDFREE(&nd, NDF_ONLY_PNBUF);
906 	ofs = 0;
907 	len = sizeof(buf) - 1;
908 	while (1) {
909 		error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
910 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
911 		    NOCRED, &resid, td);
912 		if (error)
913 			break;
914 		if (resid == len)
915 			break;
916 		buf[len - resid] = 0;
917 		sbuf_printf(sb, "%s", buf);
918 		ofs += len - resid;
919 	}
920 
921 	VOP_UNLOCK(nd.ni_vp, 0);
922 	vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
923 	return (error);
924 }
925 
926 static void
927 vfs_mountroot_wait(void)
928 {
929 	struct root_hold_token *h;
930 	struct timeval lastfail;
931 	int curfail;
932 
933 	curfail = 0;
934 	while (1) {
935 		DROP_GIANT();
936 		g_waitidle();
937 		PICKUP_GIANT();
938 		mtx_lock(&root_holds_mtx);
939 		if (LIST_EMPTY(&root_holds)) {
940 			mtx_unlock(&root_holds_mtx);
941 			break;
942 		}
943 		if (ppsratecheck(&lastfail, &curfail, 1)) {
944 			printf("Root mount waiting for:");
945 			LIST_FOREACH(h, &root_holds, list)
946 				printf(" %s", h->who);
947 			printf("\n");
948 		}
949 		msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
950 		    hz);
951 	}
952 }
953 
954 static int
955 vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev)
956 {
957 	int delay, timeout;
958 
959 	/*
960 	 * In case of ZFS and NFS we don't have a way to wait for
961 	 * specific device.
962 	 */
963 	if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL ||
964 	    dev[0] == '\0') {
965 		vfs_mountroot_wait();
966 		return (0);
967 	}
968 
969 	/*
970 	 * Otherwise, no point in waiting if the device is already there.
971 	 * Note that we must wait for GEOM to finish reconfiguring itself,
972 	 * eg for geom_part(4) to finish tasting.
973 	 */
974 	DROP_GIANT();
975 	g_waitidle();
976 	PICKUP_GIANT();
977 	if (parse_mount_dev_present(dev))
978 		return (0);
979 
980 	/*
981 	 * No luck.  Let's wait.  This code looks weird, but it's that way
982 	 * to behave exactly as it used to work before.
983 	 */
984 	vfs_mountroot_wait();
985 	printf("mountroot: waiting for device %s...\n", dev);
986 	delay = hz / 10;
987 	timeout = root_mount_timeout * hz;
988 	do {
989 		pause("rmdev", delay);
990 		timeout -= delay;
991 	} while (timeout > 0 && !parse_mount_dev_present(dev));
992 
993 	if (timeout <= 0)
994 		return (ENODEV);
995 
996 	return (0);
997 }
998 
999 void
1000 vfs_mountroot(void)
1001 {
1002 	struct mount *mp;
1003 	struct sbuf *sb;
1004 	struct thread *td;
1005 	time_t timebase;
1006 	int error;
1007 
1008 	td = curthread;
1009 
1010 	sb = sbuf_new_auto();
1011 	vfs_mountroot_conf0(sb);
1012 	sbuf_finish(sb);
1013 
1014 	error = vfs_mountroot_devfs(td, &mp);
1015 	while (!error) {
1016 		error = vfs_mountroot_parse(sb, mp);
1017 		if (!error) {
1018 			vfs_mountroot_shuffle(td, mp);
1019 			sbuf_clear(sb);
1020 			error = vfs_mountroot_readconf(td, sb);
1021 			sbuf_finish(sb);
1022 		}
1023 	}
1024 
1025 	sbuf_delete(sb);
1026 
1027 	/*
1028 	 * Iterate over all currently mounted file systems and use
1029 	 * the time stamp found to check and/or initialize the RTC.
1030 	 * Call inittodr() only once and pass it the largest of the
1031 	 * timestamps we encounter.
1032 	 */
1033 	timebase = 0;
1034 	mtx_lock(&mountlist_mtx);
1035 	mp = TAILQ_FIRST(&mountlist);
1036 	while (mp != NULL) {
1037 		if (mp->mnt_time > timebase)
1038 			timebase = mp->mnt_time;
1039 		mp = TAILQ_NEXT(mp, mnt_list);
1040 	}
1041 	mtx_unlock(&mountlist_mtx);
1042 	inittodr(timebase);
1043 
1044 	/* Keep prison0's root in sync with the global rootvnode. */
1045 	mtx_lock(&prison0.pr_mtx);
1046 	prison0.pr_root = rootvnode;
1047 	vref(prison0.pr_root);
1048 	mtx_unlock(&prison0.pr_mtx);
1049 
1050 	mtx_lock(&root_holds_mtx);
1051 	atomic_store_rel_int(&root_mount_complete, 1);
1052 	wakeup(&root_mount_complete);
1053 	mtx_unlock(&root_holds_mtx);
1054 
1055 	EVENTHANDLER_INVOKE(mountroot);
1056 }
1057 
1058 static struct mntarg *
1059 parse_mountroot_options(struct mntarg *ma, const char *options)
1060 {
1061 	char *p;
1062 	char *name, *name_arg;
1063 	char *val, *val_arg;
1064 	char *opts;
1065 
1066 	if (options == NULL || options[0] == '\0')
1067 		return (ma);
1068 
1069 	p = opts = strdup(options, M_MOUNT);
1070 	if (opts == NULL) {
1071 		return (ma);
1072 	}
1073 
1074 	while((name = strsep(&p, ",")) != NULL) {
1075 		if (name[0] == '\0')
1076 			break;
1077 
1078 		val = strchr(name, '=');
1079 		if (val != NULL) {
1080 			*val = '\0';
1081 			++val;
1082 		}
1083 		if( strcmp(name, "rw") == 0 ||
1084 		    strcmp(name, "noro") == 0) {
1085 			/*
1086 			 * The first time we mount the root file system,
1087 			 * we need to mount 'ro', so We need to ignore
1088 			 * 'rw' and 'noro' mount options.
1089 			 */
1090 			continue;
1091 		}
1092 		name_arg = strdup(name, M_MOUNT);
1093 		val_arg = NULL;
1094 		if (val != NULL)
1095 			val_arg = strdup(val, M_MOUNT);
1096 
1097 		ma = mount_arg(ma, name_arg, val_arg,
1098 		    (val_arg != NULL ? -1 : 0));
1099 	}
1100 	free(opts, M_MOUNT);
1101 	return (ma);
1102 }
1103