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