Deleted Added
full compact
vfs_mountroot.c (226673) vfs_mountroot.c (228634)
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 226673 2011-10-23 20:03:33Z marcel $");
41__FBSDID("$FreeBSD: head/sys/kern/vfs_mountroot.c 228634 2011-12-17 15:26:34Z avg $");
42
43#include <sys/param.h>
44#include <sys/conf.h>
42
43#include <sys/param.h>
44#include <sys/conf.h>
45#include <sys/cons.h>
45#include <sys/fcntl.h>
46#include <sys/jail.h>
47#include <sys/kernel.h>
46#include <sys/fcntl.h>
47#include <sys/jail.h>
48#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>
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 do {
487 error = EINVAL;
488 printf("\nmountroot> ");
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
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 do {
487 error = EINVAL;
488 printf("\nmountroot> ");
489 gets(name, sizeof(name), GETS_ECHO);
489 cngets(name, sizeof(name), GETS_ECHO);
490 if (name[0] == '\0')
491 break;
492 if (name[0] == '?' && name[1] == '\0') {
493 printf("\nList of GEOM managed disk devices:\n ");
494 g_dev_print();
495 continue;
496 }
497 if (name[0] == '.' && name[1] == '\0') {
498 pause("rmask", hz);
499 continue;
500 }
501 mnt = name;
502 error = parse_mount(&mnt);
503 if (error == -1)
504 printf("Invalid file system specification.\n");
505 } while (error != 0);
506
507 return (error);
508}
509
510static int
511parse_dir_md(char **conf)
512{
513 struct stat sb;
514 struct thread *td;
515 struct md_ioctl *mdio;
516 char *path, *tok;
517 int error, fd, len;
518
519 td = curthread;
520
521 error = parse_token(conf, &tok);
522 if (error)
523 return (error);
524
525 len = strlen(tok);
526 mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
527 path = (void *)(mdio + 1);
528 bcopy(tok, path, len);
529 free(tok, M_TEMP);
530
531 /* Get file status. */
532 error = kern_stat(td, path, UIO_SYSSPACE, &sb);
533 if (error)
534 goto out;
535
536 /* Open /dev/mdctl so that we can attach/detach. */
537 error = kern_open(td, "/dev/" MDCTL_NAME, UIO_SYSSPACE, O_RDWR, 0);
538 if (error)
539 goto out;
540
541 fd = td->td_retval[0];
542 mdio->md_version = MDIOVERSION;
543 mdio->md_type = MD_VNODE;
544
545 if (root_mount_mddev != -1) {
546 mdio->md_unit = root_mount_mddev;
547 DROP_GIANT();
548 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
549 PICKUP_GIANT();
550 /* Ignore errors. We don't care. */
551 root_mount_mddev = -1;
552 }
553
554 mdio->md_file = (void *)(mdio + 1);
555 mdio->md_options = MD_AUTOUNIT | MD_READONLY;
556 mdio->md_mediasize = sb.st_size;
557 mdio->md_unit = 0;
558 DROP_GIANT();
559 error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
560 PICKUP_GIANT();
561 if (error)
562 goto out;
563
564 if (mdio->md_unit > 9) {
565 printf("rootmount: too many md units\n");
566 mdio->md_file = NULL;
567 mdio->md_options = 0;
568 mdio->md_mediasize = 0;
569 DROP_GIANT();
570 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
571 PICKUP_GIANT();
572 /* Ignore errors. We don't care. */
573 error = ERANGE;
574 goto out;
575 }
576
577 root_mount_mddev = mdio->md_unit;
578 printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
579
580 error = kern_close(td, fd);
581
582 out:
583 free(mdio, M_TEMP);
584 return (error);
585}
586
587static int
588parse_dir_onfail(char **conf)
589{
590 char *action;
591 int error;
592
593 error = parse_token(conf, &action);
594 if (error)
595 return (error);
596
597 if (!strcmp(action, "continue"))
598 root_mount_onfail = A_CONTINUE;
599 else if (!strcmp(action, "panic"))
600 root_mount_onfail = A_PANIC;
601 else if (!strcmp(action, "reboot"))
602 root_mount_onfail = A_REBOOT;
603 else if (!strcmp(action, "retry"))
604 root_mount_onfail = A_RETRY;
605 else {
606 printf("rootmount: %s: unknown action\n", action);
607 error = EINVAL;
608 }
609
610 free(action, M_TEMP);
611 return (0);
612}
613
614static int
615parse_dir_timeout(char **conf)
616{
617 char *tok, *endtok;
618 long secs;
619 int error;
620
621 error = parse_token(conf, &tok);
622 if (error)
623 return (error);
624
625 secs = strtol(tok, &endtok, 0);
626 error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
627 if (!error)
628 root_mount_timeout = secs;
629 free(tok, M_TEMP);
630 return (error);
631}
632
633static int
634parse_directive(char **conf)
635{
636 char *dir;
637 int error;
638
639 error = parse_token(conf, &dir);
640 if (error)
641 return (error);
642
643 if (strcmp(dir, ".ask") == 0)
644 error = parse_dir_ask(conf);
645 else if (strcmp(dir, ".md") == 0)
646 error = parse_dir_md(conf);
647 else if (strcmp(dir, ".onfail") == 0)
648 error = parse_dir_onfail(conf);
649 else if (strcmp(dir, ".timeout") == 0)
650 error = parse_dir_timeout(conf);
651 else {
652 printf("mountroot: invalid directive `%s'\n", dir);
653 /* Ignore the rest of the line. */
654 (void)parse_skipto(conf, '\n');
655 error = EINVAL;
656 }
657 free(dir, M_TEMP);
658 return (error);
659}
660
661static int
662parse_mount_dev_present(const char *dev)
663{
664 struct nameidata nd;
665 int error;
666
667 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
668 error = namei(&nd);
669 if (!error)
670 vput(nd.ni_vp);
671 NDFREE(&nd, NDF_ONLY_PNBUF);
672 return (error != 0) ? 0 : 1;
673}
674
675static int
676parse_mount(char **conf)
677{
678 char errmsg[255];
679 struct mntarg *ma;
680 char *dev, *fs, *opts, *tok;
681 int delay, error, timeout;
682
683 error = parse_token(conf, &tok);
684 if (error)
685 return (error);
686 fs = tok;
687 error = parse_skipto(&tok, ':');
688 if (error) {
689 free(fs, M_TEMP);
690 return (error);
691 }
692 parse_poke(&tok, '\0');
693 parse_advance(&tok);
694 dev = tok;
695
696 if (root_mount_mddev != -1) {
697 /* Handle substitution for the md unit number. */
698 tok = strstr(dev, "md#");
699 if (tok != NULL)
700 tok[2] = '0' + root_mount_mddev;
701 }
702
703 /* Parse options. */
704 error = parse_token(conf, &tok);
705 opts = (error == 0) ? tok : NULL;
706
707 printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
708 (opts != NULL) ? opts : "");
709
710 bzero(errmsg, sizeof(errmsg));
711
712 if (vfs_byname(fs) == NULL) {
713 strlcpy(errmsg, "unknown file system", sizeof(errmsg));
714 error = ENOENT;
715 goto out;
716 }
717
718 if (strcmp(fs, "zfs") != 0 && dev[0] != '\0' &&
719 !parse_mount_dev_present(dev)) {
720 printf("mountroot: waiting for device %s ...\n", dev);
721 delay = hz / 10;
722 timeout = root_mount_timeout * hz;
723 do {
724 pause("rmdev", delay);
725 timeout -= delay;
726 } while (timeout > 0 && !parse_mount_dev_present(dev));
727 if (timeout <= 0) {
728 error = ENODEV;
729 goto out;
730 }
731 }
732
733 ma = NULL;
734 ma = mount_arg(ma, "fstype", fs, -1);
735 ma = mount_arg(ma, "fspath", "/", -1);
736 ma = mount_arg(ma, "from", dev, -1);
737 ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
738 ma = mount_arg(ma, "ro", NULL, 0);
739 ma = parse_mountroot_options(ma, opts);
740 error = kernel_mount(ma, MNT_ROOTFS);
741
742 out:
743 if (error) {
744 printf("Mounting from %s:%s failed with error %d",
745 fs, dev, error);
746 if (errmsg[0] != '\0')
747 printf(": %s", errmsg);
748 printf(".\n");
749 }
750 free(fs, M_TEMP);
751 if (opts != NULL)
752 free(opts, M_TEMP);
753 /* kernel_mount can return -1 on error. */
754 return ((error < 0) ? EDOOFUS : error);
755}
756
757static int
758vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
759{
760 struct mount *mp;
761 char *conf;
762 int error;
763
764 root_mount_mddev = -1;
765
766retry:
767 conf = sbuf_data(sb);
768 mp = TAILQ_NEXT(mpdevfs, mnt_list);
769 error = (mp == NULL) ? 0 : EDOOFUS;
770 root_mount_onfail = A_CONTINUE;
771 while (mp == NULL) {
772 error = parse_skipto(&conf, CC_NONWHITESPACE);
773 if (error == PE_EOL) {
774 parse_advance(&conf);
775 continue;
776 }
777 if (error < 0)
778 break;
779 switch (parse_peek(&conf)) {
780 case '#':
781 error = parse_skipto(&conf, '\n');
782 break;
783 case '.':
784 error = parse_directive(&conf);
785 break;
786 default:
787 error = parse_mount(&conf);
788 break;
789 }
790 if (error < 0)
791 break;
792 /* Ignore any trailing garbage on the line. */
793 if (parse_peek(&conf) != '\n') {
794 printf("mountroot: advancing to next directive...\n");
795 (void)parse_skipto(&conf, '\n');
796 }
797 mp = TAILQ_NEXT(mpdevfs, mnt_list);
798 }
799 if (mp != NULL)
800 return (0);
801
802 /*
803 * We failed to mount (a new) root.
804 */
805 switch (root_mount_onfail) {
806 case A_CONTINUE:
807 break;
808 case A_PANIC:
809 panic("mountroot: unable to (re-)mount root.");
810 /* NOTREACHED */
811 case A_RETRY:
812 goto retry;
813 case A_REBOOT:
814 kern_reboot(RB_NOSYNC);
815 /* NOTREACHED */
816 }
817
818 return (error);
819}
820
821static void
822vfs_mountroot_conf0(struct sbuf *sb)
823{
824 char *s, *tok, *mnt, *opt;
825 int error;
826
827 sbuf_printf(sb, ".onfail panic\n");
828 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
829 if (boothowto & RB_ASKNAME)
830 sbuf_printf(sb, ".ask\n");
831#ifdef ROOTDEVNAME
832 if (boothowto & RB_DFLTROOT)
833 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
834#endif
835 if (boothowto & RB_CDROM) {
836 sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
837 sbuf_printf(sb, ".timeout 0\n");
838 sbuf_printf(sb, "cd9660:/dev/acd0 ro\n");
839 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
840 }
841 s = getenv("vfs.root.mountfrom");
842 if (s != NULL) {
843 opt = getenv("vfs.root.mountfrom.options");
844 tok = s;
845 error = parse_token(&tok, &mnt);
846 while (!error) {
847 sbuf_printf(sb, "%s %s\n", mnt,
848 (opt != NULL) ? opt : "");
849 free(mnt, M_TEMP);
850 error = parse_token(&tok, &mnt);
851 }
852 if (opt != NULL)
853 freeenv(opt);
854 freeenv(s);
855 }
856 if (rootdevnames[0] != NULL)
857 sbuf_printf(sb, "%s\n", rootdevnames[0]);
858 if (rootdevnames[1] != NULL)
859 sbuf_printf(sb, "%s\n", rootdevnames[1]);
860#ifdef ROOTDEVNAME
861 if (!(boothowto & RB_DFLTROOT))
862 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
863#endif
864 if (!(boothowto & RB_ASKNAME))
865 sbuf_printf(sb, ".ask\n");
866}
867
868static int
869vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
870{
871 static char buf[128];
872 struct nameidata nd;
873 off_t ofs;
874 int error, flags;
875 int len, resid;
876 int vfslocked;
877
878 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE,
879 "/.mount.conf", td);
880 flags = FREAD;
881 error = vn_open(&nd, &flags, 0, NULL);
882 if (error)
883 return (error);
884
885 vfslocked = NDHASGIANT(&nd);
886 NDFREE(&nd, NDF_ONLY_PNBUF);
887 ofs = 0;
888 len = sizeof(buf) - 1;
889 while (1) {
890 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
891 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
892 NOCRED, &resid, td);
893 if (error)
894 break;
895 if (resid == len)
896 break;
897 buf[len - resid] = 0;
898 sbuf_printf(sb, "%s", buf);
899 ofs += len - resid;
900 }
901
902 VOP_UNLOCK(nd.ni_vp, 0);
903 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
904 VFS_UNLOCK_GIANT(vfslocked);
905 return (error);
906}
907
908static void
909vfs_mountroot_wait(void)
910{
911 struct root_hold_token *h;
912 struct timeval lastfail;
913 int curfail;
914
915 curfail = 0;
916 while (1) {
917 DROP_GIANT();
918 g_waitidle();
919 PICKUP_GIANT();
920 mtx_lock(&mountlist_mtx);
921 if (LIST_EMPTY(&root_holds)) {
922 mtx_unlock(&mountlist_mtx);
923 break;
924 }
925 if (ppsratecheck(&lastfail, &curfail, 1)) {
926 printf("Root mount waiting for:");
927 LIST_FOREACH(h, &root_holds, list)
928 printf(" %s", h->who);
929 printf("\n");
930 }
931 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
932 hz);
933 }
934}
935
936void
937vfs_mountroot(void)
938{
939 struct mount *mp;
940 struct sbuf *sb;
941 struct thread *td;
942 time_t timebase;
943 int error;
944
945 td = curthread;
946
947 vfs_mountroot_wait();
948
949 sb = sbuf_new_auto();
950 vfs_mountroot_conf0(sb);
951 sbuf_finish(sb);
952
953 error = vfs_mountroot_devfs(td, &mp);
954 while (!error) {
955 error = vfs_mountroot_parse(sb, mp);
956 if (!error) {
957 error = vfs_mountroot_shuffle(td, mp);
958 if (!error) {
959 sbuf_clear(sb);
960 error = vfs_mountroot_readconf(td, sb);
961 sbuf_finish(sb);
962 }
963 }
964 }
965
966 sbuf_delete(sb);
967
968 /*
969 * Iterate over all currently mounted file systems and use
970 * the time stamp found to check and/or initialize the RTC.
971 * Call inittodr() only once and pass it the largest of the
972 * timestamps we encounter.
973 */
974 timebase = 0;
975 mtx_lock(&mountlist_mtx);
976 mp = TAILQ_FIRST(&mountlist);
977 while (mp != NULL) {
978 if (mp->mnt_time > timebase)
979 timebase = mp->mnt_time;
980 mp = TAILQ_NEXT(mp, mnt_list);
981 }
982 mtx_unlock(&mountlist_mtx);
983 inittodr(timebase);
984
985 /* Keep prison0's root in sync with the global rootvnode. */
986 mtx_lock(&prison0.pr_mtx);
987 prison0.pr_root = rootvnode;
988 vref(prison0.pr_root);
989 mtx_unlock(&prison0.pr_mtx);
990
991 mtx_lock(&mountlist_mtx);
992 atomic_store_rel_int(&root_mount_complete, 1);
993 wakeup(&root_mount_complete);
994 mtx_unlock(&mountlist_mtx);
995}
996
997static struct mntarg *
998parse_mountroot_options(struct mntarg *ma, const char *options)
999{
1000 char *p;
1001 char *name, *name_arg;
1002 char *val, *val_arg;
1003 char *opts;
1004
1005 if (options == NULL || options[0] == '\0')
1006 return (ma);
1007
1008 p = opts = strdup(options, M_MOUNT);
1009 if (opts == NULL) {
1010 return (ma);
1011 }
1012
1013 while((name = strsep(&p, ",")) != NULL) {
1014 if (name[0] == '\0')
1015 break;
1016
1017 val = strchr(name, '=');
1018 if (val != NULL) {
1019 *val = '\0';
1020 ++val;
1021 }
1022 if( strcmp(name, "rw") == 0 ||
1023 strcmp(name, "noro") == 0) {
1024 /*
1025 * The first time we mount the root file system,
1026 * we need to mount 'ro', so We need to ignore
1027 * 'rw' and 'noro' mount options.
1028 */
1029 continue;
1030 }
1031 name_arg = strdup(name, M_MOUNT);
1032 val_arg = NULL;
1033 if (val != NULL)
1034 val_arg = strdup(val, M_MOUNT);
1035
1036 ma = mount_arg(ma, name_arg, val_arg,
1037 (val_arg != NULL ? -1 : 0));
1038 }
1039 free(opts, M_MOUNT);
1040 return (ma);
1041}
490 if (name[0] == '\0')
491 break;
492 if (name[0] == '?' && name[1] == '\0') {
493 printf("\nList of GEOM managed disk devices:\n ");
494 g_dev_print();
495 continue;
496 }
497 if (name[0] == '.' && name[1] == '\0') {
498 pause("rmask", hz);
499 continue;
500 }
501 mnt = name;
502 error = parse_mount(&mnt);
503 if (error == -1)
504 printf("Invalid file system specification.\n");
505 } while (error != 0);
506
507 return (error);
508}
509
510static int
511parse_dir_md(char **conf)
512{
513 struct stat sb;
514 struct thread *td;
515 struct md_ioctl *mdio;
516 char *path, *tok;
517 int error, fd, len;
518
519 td = curthread;
520
521 error = parse_token(conf, &tok);
522 if (error)
523 return (error);
524
525 len = strlen(tok);
526 mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
527 path = (void *)(mdio + 1);
528 bcopy(tok, path, len);
529 free(tok, M_TEMP);
530
531 /* Get file status. */
532 error = kern_stat(td, path, UIO_SYSSPACE, &sb);
533 if (error)
534 goto out;
535
536 /* Open /dev/mdctl so that we can attach/detach. */
537 error = kern_open(td, "/dev/" MDCTL_NAME, UIO_SYSSPACE, O_RDWR, 0);
538 if (error)
539 goto out;
540
541 fd = td->td_retval[0];
542 mdio->md_version = MDIOVERSION;
543 mdio->md_type = MD_VNODE;
544
545 if (root_mount_mddev != -1) {
546 mdio->md_unit = root_mount_mddev;
547 DROP_GIANT();
548 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
549 PICKUP_GIANT();
550 /* Ignore errors. We don't care. */
551 root_mount_mddev = -1;
552 }
553
554 mdio->md_file = (void *)(mdio + 1);
555 mdio->md_options = MD_AUTOUNIT | MD_READONLY;
556 mdio->md_mediasize = sb.st_size;
557 mdio->md_unit = 0;
558 DROP_GIANT();
559 error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
560 PICKUP_GIANT();
561 if (error)
562 goto out;
563
564 if (mdio->md_unit > 9) {
565 printf("rootmount: too many md units\n");
566 mdio->md_file = NULL;
567 mdio->md_options = 0;
568 mdio->md_mediasize = 0;
569 DROP_GIANT();
570 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
571 PICKUP_GIANT();
572 /* Ignore errors. We don't care. */
573 error = ERANGE;
574 goto out;
575 }
576
577 root_mount_mddev = mdio->md_unit;
578 printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
579
580 error = kern_close(td, fd);
581
582 out:
583 free(mdio, M_TEMP);
584 return (error);
585}
586
587static int
588parse_dir_onfail(char **conf)
589{
590 char *action;
591 int error;
592
593 error = parse_token(conf, &action);
594 if (error)
595 return (error);
596
597 if (!strcmp(action, "continue"))
598 root_mount_onfail = A_CONTINUE;
599 else if (!strcmp(action, "panic"))
600 root_mount_onfail = A_PANIC;
601 else if (!strcmp(action, "reboot"))
602 root_mount_onfail = A_REBOOT;
603 else if (!strcmp(action, "retry"))
604 root_mount_onfail = A_RETRY;
605 else {
606 printf("rootmount: %s: unknown action\n", action);
607 error = EINVAL;
608 }
609
610 free(action, M_TEMP);
611 return (0);
612}
613
614static int
615parse_dir_timeout(char **conf)
616{
617 char *tok, *endtok;
618 long secs;
619 int error;
620
621 error = parse_token(conf, &tok);
622 if (error)
623 return (error);
624
625 secs = strtol(tok, &endtok, 0);
626 error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
627 if (!error)
628 root_mount_timeout = secs;
629 free(tok, M_TEMP);
630 return (error);
631}
632
633static int
634parse_directive(char **conf)
635{
636 char *dir;
637 int error;
638
639 error = parse_token(conf, &dir);
640 if (error)
641 return (error);
642
643 if (strcmp(dir, ".ask") == 0)
644 error = parse_dir_ask(conf);
645 else if (strcmp(dir, ".md") == 0)
646 error = parse_dir_md(conf);
647 else if (strcmp(dir, ".onfail") == 0)
648 error = parse_dir_onfail(conf);
649 else if (strcmp(dir, ".timeout") == 0)
650 error = parse_dir_timeout(conf);
651 else {
652 printf("mountroot: invalid directive `%s'\n", dir);
653 /* Ignore the rest of the line. */
654 (void)parse_skipto(conf, '\n');
655 error = EINVAL;
656 }
657 free(dir, M_TEMP);
658 return (error);
659}
660
661static int
662parse_mount_dev_present(const char *dev)
663{
664 struct nameidata nd;
665 int error;
666
667 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
668 error = namei(&nd);
669 if (!error)
670 vput(nd.ni_vp);
671 NDFREE(&nd, NDF_ONLY_PNBUF);
672 return (error != 0) ? 0 : 1;
673}
674
675static int
676parse_mount(char **conf)
677{
678 char errmsg[255];
679 struct mntarg *ma;
680 char *dev, *fs, *opts, *tok;
681 int delay, error, timeout;
682
683 error = parse_token(conf, &tok);
684 if (error)
685 return (error);
686 fs = tok;
687 error = parse_skipto(&tok, ':');
688 if (error) {
689 free(fs, M_TEMP);
690 return (error);
691 }
692 parse_poke(&tok, '\0');
693 parse_advance(&tok);
694 dev = tok;
695
696 if (root_mount_mddev != -1) {
697 /* Handle substitution for the md unit number. */
698 tok = strstr(dev, "md#");
699 if (tok != NULL)
700 tok[2] = '0' + root_mount_mddev;
701 }
702
703 /* Parse options. */
704 error = parse_token(conf, &tok);
705 opts = (error == 0) ? tok : NULL;
706
707 printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
708 (opts != NULL) ? opts : "");
709
710 bzero(errmsg, sizeof(errmsg));
711
712 if (vfs_byname(fs) == NULL) {
713 strlcpy(errmsg, "unknown file system", sizeof(errmsg));
714 error = ENOENT;
715 goto out;
716 }
717
718 if (strcmp(fs, "zfs") != 0 && dev[0] != '\0' &&
719 !parse_mount_dev_present(dev)) {
720 printf("mountroot: waiting for device %s ...\n", dev);
721 delay = hz / 10;
722 timeout = root_mount_timeout * hz;
723 do {
724 pause("rmdev", delay);
725 timeout -= delay;
726 } while (timeout > 0 && !parse_mount_dev_present(dev));
727 if (timeout <= 0) {
728 error = ENODEV;
729 goto out;
730 }
731 }
732
733 ma = NULL;
734 ma = mount_arg(ma, "fstype", fs, -1);
735 ma = mount_arg(ma, "fspath", "/", -1);
736 ma = mount_arg(ma, "from", dev, -1);
737 ma = mount_arg(ma, "errmsg", errmsg, sizeof(errmsg));
738 ma = mount_arg(ma, "ro", NULL, 0);
739 ma = parse_mountroot_options(ma, opts);
740 error = kernel_mount(ma, MNT_ROOTFS);
741
742 out:
743 if (error) {
744 printf("Mounting from %s:%s failed with error %d",
745 fs, dev, error);
746 if (errmsg[0] != '\0')
747 printf(": %s", errmsg);
748 printf(".\n");
749 }
750 free(fs, M_TEMP);
751 if (opts != NULL)
752 free(opts, M_TEMP);
753 /* kernel_mount can return -1 on error. */
754 return ((error < 0) ? EDOOFUS : error);
755}
756
757static int
758vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
759{
760 struct mount *mp;
761 char *conf;
762 int error;
763
764 root_mount_mddev = -1;
765
766retry:
767 conf = sbuf_data(sb);
768 mp = TAILQ_NEXT(mpdevfs, mnt_list);
769 error = (mp == NULL) ? 0 : EDOOFUS;
770 root_mount_onfail = A_CONTINUE;
771 while (mp == NULL) {
772 error = parse_skipto(&conf, CC_NONWHITESPACE);
773 if (error == PE_EOL) {
774 parse_advance(&conf);
775 continue;
776 }
777 if (error < 0)
778 break;
779 switch (parse_peek(&conf)) {
780 case '#':
781 error = parse_skipto(&conf, '\n');
782 break;
783 case '.':
784 error = parse_directive(&conf);
785 break;
786 default:
787 error = parse_mount(&conf);
788 break;
789 }
790 if (error < 0)
791 break;
792 /* Ignore any trailing garbage on the line. */
793 if (parse_peek(&conf) != '\n') {
794 printf("mountroot: advancing to next directive...\n");
795 (void)parse_skipto(&conf, '\n');
796 }
797 mp = TAILQ_NEXT(mpdevfs, mnt_list);
798 }
799 if (mp != NULL)
800 return (0);
801
802 /*
803 * We failed to mount (a new) root.
804 */
805 switch (root_mount_onfail) {
806 case A_CONTINUE:
807 break;
808 case A_PANIC:
809 panic("mountroot: unable to (re-)mount root.");
810 /* NOTREACHED */
811 case A_RETRY:
812 goto retry;
813 case A_REBOOT:
814 kern_reboot(RB_NOSYNC);
815 /* NOTREACHED */
816 }
817
818 return (error);
819}
820
821static void
822vfs_mountroot_conf0(struct sbuf *sb)
823{
824 char *s, *tok, *mnt, *opt;
825 int error;
826
827 sbuf_printf(sb, ".onfail panic\n");
828 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
829 if (boothowto & RB_ASKNAME)
830 sbuf_printf(sb, ".ask\n");
831#ifdef ROOTDEVNAME
832 if (boothowto & RB_DFLTROOT)
833 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
834#endif
835 if (boothowto & RB_CDROM) {
836 sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
837 sbuf_printf(sb, ".timeout 0\n");
838 sbuf_printf(sb, "cd9660:/dev/acd0 ro\n");
839 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
840 }
841 s = getenv("vfs.root.mountfrom");
842 if (s != NULL) {
843 opt = getenv("vfs.root.mountfrom.options");
844 tok = s;
845 error = parse_token(&tok, &mnt);
846 while (!error) {
847 sbuf_printf(sb, "%s %s\n", mnt,
848 (opt != NULL) ? opt : "");
849 free(mnt, M_TEMP);
850 error = parse_token(&tok, &mnt);
851 }
852 if (opt != NULL)
853 freeenv(opt);
854 freeenv(s);
855 }
856 if (rootdevnames[0] != NULL)
857 sbuf_printf(sb, "%s\n", rootdevnames[0]);
858 if (rootdevnames[1] != NULL)
859 sbuf_printf(sb, "%s\n", rootdevnames[1]);
860#ifdef ROOTDEVNAME
861 if (!(boothowto & RB_DFLTROOT))
862 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
863#endif
864 if (!(boothowto & RB_ASKNAME))
865 sbuf_printf(sb, ".ask\n");
866}
867
868static int
869vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
870{
871 static char buf[128];
872 struct nameidata nd;
873 off_t ofs;
874 int error, flags;
875 int len, resid;
876 int vfslocked;
877
878 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE,
879 "/.mount.conf", td);
880 flags = FREAD;
881 error = vn_open(&nd, &flags, 0, NULL);
882 if (error)
883 return (error);
884
885 vfslocked = NDHASGIANT(&nd);
886 NDFREE(&nd, NDF_ONLY_PNBUF);
887 ofs = 0;
888 len = sizeof(buf) - 1;
889 while (1) {
890 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
891 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
892 NOCRED, &resid, td);
893 if (error)
894 break;
895 if (resid == len)
896 break;
897 buf[len - resid] = 0;
898 sbuf_printf(sb, "%s", buf);
899 ofs += len - resid;
900 }
901
902 VOP_UNLOCK(nd.ni_vp, 0);
903 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
904 VFS_UNLOCK_GIANT(vfslocked);
905 return (error);
906}
907
908static void
909vfs_mountroot_wait(void)
910{
911 struct root_hold_token *h;
912 struct timeval lastfail;
913 int curfail;
914
915 curfail = 0;
916 while (1) {
917 DROP_GIANT();
918 g_waitidle();
919 PICKUP_GIANT();
920 mtx_lock(&mountlist_mtx);
921 if (LIST_EMPTY(&root_holds)) {
922 mtx_unlock(&mountlist_mtx);
923 break;
924 }
925 if (ppsratecheck(&lastfail, &curfail, 1)) {
926 printf("Root mount waiting for:");
927 LIST_FOREACH(h, &root_holds, list)
928 printf(" %s", h->who);
929 printf("\n");
930 }
931 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold",
932 hz);
933 }
934}
935
936void
937vfs_mountroot(void)
938{
939 struct mount *mp;
940 struct sbuf *sb;
941 struct thread *td;
942 time_t timebase;
943 int error;
944
945 td = curthread;
946
947 vfs_mountroot_wait();
948
949 sb = sbuf_new_auto();
950 vfs_mountroot_conf0(sb);
951 sbuf_finish(sb);
952
953 error = vfs_mountroot_devfs(td, &mp);
954 while (!error) {
955 error = vfs_mountroot_parse(sb, mp);
956 if (!error) {
957 error = vfs_mountroot_shuffle(td, mp);
958 if (!error) {
959 sbuf_clear(sb);
960 error = vfs_mountroot_readconf(td, sb);
961 sbuf_finish(sb);
962 }
963 }
964 }
965
966 sbuf_delete(sb);
967
968 /*
969 * Iterate over all currently mounted file systems and use
970 * the time stamp found to check and/or initialize the RTC.
971 * Call inittodr() only once and pass it the largest of the
972 * timestamps we encounter.
973 */
974 timebase = 0;
975 mtx_lock(&mountlist_mtx);
976 mp = TAILQ_FIRST(&mountlist);
977 while (mp != NULL) {
978 if (mp->mnt_time > timebase)
979 timebase = mp->mnt_time;
980 mp = TAILQ_NEXT(mp, mnt_list);
981 }
982 mtx_unlock(&mountlist_mtx);
983 inittodr(timebase);
984
985 /* Keep prison0's root in sync with the global rootvnode. */
986 mtx_lock(&prison0.pr_mtx);
987 prison0.pr_root = rootvnode;
988 vref(prison0.pr_root);
989 mtx_unlock(&prison0.pr_mtx);
990
991 mtx_lock(&mountlist_mtx);
992 atomic_store_rel_int(&root_mount_complete, 1);
993 wakeup(&root_mount_complete);
994 mtx_unlock(&mountlist_mtx);
995}
996
997static struct mntarg *
998parse_mountroot_options(struct mntarg *ma, const char *options)
999{
1000 char *p;
1001 char *name, *name_arg;
1002 char *val, *val_arg;
1003 char *opts;
1004
1005 if (options == NULL || options[0] == '\0')
1006 return (ma);
1007
1008 p = opts = strdup(options, M_MOUNT);
1009 if (opts == NULL) {
1010 return (ma);
1011 }
1012
1013 while((name = strsep(&p, ",")) != NULL) {
1014 if (name[0] == '\0')
1015 break;
1016
1017 val = strchr(name, '=');
1018 if (val != NULL) {
1019 *val = '\0';
1020 ++val;
1021 }
1022 if( strcmp(name, "rw") == 0 ||
1023 strcmp(name, "noro") == 0) {
1024 /*
1025 * The first time we mount the root file system,
1026 * we need to mount 'ro', so We need to ignore
1027 * 'rw' and 'noro' mount options.
1028 */
1029 continue;
1030 }
1031 name_arg = strdup(name, M_MOUNT);
1032 val_arg = NULL;
1033 if (val != NULL)
1034 val_arg = strdup(val, M_MOUNT);
1035
1036 ma = mount_arg(ma, name_arg, val_arg,
1037 (val_arg != NULL ? -1 : 0));
1038 }
1039 free(opts, M_MOUNT);
1040 return (ma);
1041}