vfs_mount.c revision 52886
1/*-
2 * Copyright (c) 1999 Michael Smith
3 * All rights reserved.
4 * Copyright (c) 1999 Poul-Henning Kamp
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$FreeBSD: head/sys/kern/vfs_mount.c 52886 1999-11-05 02:45:50Z msmith $
29 */
30
31/*
32 * Locate and mount the root filesystem.
33 *
34 * The root filesystem is detailed in the kernel environment variable
35 * vfs.root.mountfrom, which is expected to be in the general format
36 *
37 * <vfsname>:[<path>]
38 * vfsname   := the name of a VFS known to the kernel and capable
39 *              of being mounted as root
40 * path      := disk device name or other data used by the filesystem
41 *              to locate its physical store
42 *
43 */
44
45#include <sys/param.h>
46#include <sys/kernel.h>
47#include <sys/systm.h>
48#include <sys/proc.h>
49#include <sys/vnode.h>
50#include <sys/mount.h>
51#include <sys/malloc.h>
52#include <sys/reboot.h>
53#include <sys/diskslice.h>
54#include <sys/disklabel.h>
55#include <sys/conf.h>
56#include <sys/cons.h>
57
58MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
59
60#define ROOTNAME	"root_device"
61
62struct vnode	*rootvnode;
63
64/*
65 * The root specifiers we will try if RB_CDROM is specified.
66 */
67static char *cdrom_rootdevnames[] = {
68	"cd9660:cd0a",
69	"cd9660:acd0a",
70	"cd9660:wcd0a",
71	NULL
72};
73
74static void	vfs_mountroot(void *junk);
75static int	vfs_mountroot_try(char *mountfrom);
76static int	vfs_mountroot_ask(void);
77static void	gets(char *cp);
78
79/* legacy find-root code */
80char		*rootdevnames[2] = {NULL, NULL};
81static int	setrootbyname(char *name);
82
83SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
84
85/*
86 * Find and mount the root filesystem
87 */
88static void
89vfs_mountroot(void *junk)
90{
91	int		i;
92
93	/*
94	 * The root filesystem information is compiled in, and we are
95	 * booted with instructions to use it.
96	 */
97#ifdef ROOTDEVNAME
98	if ((boothowto & RB_DFLTROOT) &&
99	    !vfs_mountroot_try(ROOTDEVNAME))
100		return;
101#endif
102	/*
103	 * We are booted with instructions to prompt for the root filesystem,
104	 * or to use the compiled-in default when it doesn't exist.
105	 */
106	if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
107		if (!vfs_mountroot_ask())
108			return;
109	}
110
111	/*
112	 * We've been given the generic "use CDROM as root" flag.  This is
113	 * necessary because one media may be used in many different
114	 * devices, so we need to search for them.
115	 */
116	if (boothowto & RB_CDROM) {
117		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
118			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
119				return;
120		}
121	}
122
123	/*
124	 * Try to use the value read by the loader from /etc/fstab, or
125	 * supplied via some other means.  This is the preferred
126	 * mechanism.
127	 */
128	if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
129		return;
130
131	/*
132	 * Try values that may have been computed by the machine-dependant
133	 * legacy code.
134	 */
135	if (!vfs_mountroot_try(rootdevnames[0]))
136		return;
137	if (!vfs_mountroot_try(rootdevnames[1]))
138		return;
139
140	/*
141	 * If we have a compiled-in default, and haven't already tried it, try
142	 * it now.
143	 */
144#ifdef ROOTDEVNAME
145	if (!(boothowto & RB_DFLTROOT))
146		if (!vfs_mountroot_try(ROOTDEVNAME))
147			return;
148#endif
149
150	/*
151	 * Everything so far has failed, prompt on the console if we haven't
152	 * already tried that.
153	 */
154	if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
155		return;
156	panic("Root mount failed, startup aborted.");
157}
158
159/*
160 * Mount (mountfrom) as the root filesystem.
161 */
162static int
163vfs_mountroot_try(char *mountfrom)
164{
165        struct mount	*mp;
166	char		*vfsname, *path;
167	int		error;
168	char		patt[32];
169
170	vfsname = path = mp = NULL;
171	error = EINVAL;
172
173	if (mountfrom == NULL)
174		goto done;
175
176	printf("Mounting root from %s\n", mountfrom);
177
178	/* parse vfs name and path */
179	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
180	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
181	vfsname[0] = path[0] = 0;
182	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
183	if (sscanf(mountfrom, patt, vfsname, path) < 1)
184		goto done;
185
186	/* allocate a root mount */
187	if ((error = vfs_rootmountalloc(vfsname, ROOTNAME, &mp))) {
188		printf("Can't allocate root mount for filesystem '%s': %d\n",
189		       vfsname, error);
190		goto done;
191	}
192	mp->mnt_flag |= MNT_ROOTFS;
193
194	/* do our best to set rootdev */
195	if ((path[0] != 0) && setrootbyname(path))
196		printf("setrootbyname failed\n");
197
198	strcpy(mp->mnt_stat.f_mntfromname, path);
199	error = VFS_MOUNT(mp, NULL, NULL, NULL, curproc);
200
201done:
202	free(vfsname, M_MOUNT);
203	free(path, M_MOUNT);
204	if (error != 0) {
205		if (mp != NULL)
206			free(mp, M_MOUNT);
207		printf("Root mount failed: %d\n", error);
208	} else {
209
210		/* register with list of mounted filesystems */
211		simple_lock(&mountlist_slock);
212		CIRCLEQ_INSERT_HEAD(&mountlist, mp, mnt_list);
213		simple_unlock(&mountlist_slock);
214
215		/* sanity check system clock against root filesystem timestamp */
216		inittodr(mp->mnt_time);
217	}
218	if (mp != NULL)
219		vfs_unbusy(mp, curproc);
220	return(error);
221}
222
223/*
224 * Spin prompting on the console for a suitable root filesystem
225 */
226static int
227vfs_mountroot_ask(void)
228{
229	char name[128];
230	int i;
231	dev_t dev;
232
233	for(;;) {
234		printf("\nManual root filesystem specification:\n");
235		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
236		printf("                       eg. ufs:/dev/da0s1a\n");
237		printf("  ?                  List valid disk boot devices\n");
238		printf("  <empty line>       Abort manual input\n");
239		printf("\n>>> ");
240		gets(name);
241		if (name[0] == 0)
242			return(1);
243		if (name[0] == '?') {
244			printf("Possibly valid devices for 'ufs' root:\n");
245			for (i = 0; i < NUMCDEVSW; i++) {
246				dev = makebdev(i, 0);
247				if (devsw(dev) != NULL)
248					printf(" \"%s\"", devsw(dev)->d_name);
249			}
250			printf("\n");
251			continue;
252		}
253		if (!vfs_mountroot_try(name))
254			return(0);
255	}
256}
257
258static void
259gets(char *cp)
260{
261	char *lp;
262	int c;
263
264	lp = cp;
265	for (;;) {
266		printf("%c", c = cngetc() & 0177);
267		switch (c) {
268		case -1:
269		case '\n':
270		case '\r':
271			*lp++ = '\0';
272			return;
273		case '\b':
274		case '\177':
275			if (lp > cp) {
276				printf(" \b");
277				lp--;
278			}
279			continue;
280		case '#':
281			lp--;
282			if (lp < cp)
283				lp = cp;
284			continue;
285		case '@':
286		case 'u' & 037:
287			lp = cp;
288			printf("%c", '\n');
289			continue;
290		default:
291			*lp++ = c;
292		}
293	}
294}
295
296/*
297 * Set rootdev to match (name), given that we expect it to
298 * refer to a disk-like device.
299 */
300static int
301setrootbyname(char *name)
302{
303	char *cp;
304	int bd, unit, slice, part;
305	dev_t dev;
306
307	slice = 0;
308	part = 0;
309	cp = rindex(name, '/');
310	if (cp != NULL) {
311		name = cp + 1;
312	}
313	cp = name;
314	while (cp != '\0' && (*cp < '0' || *cp > '9'))
315		cp++;
316	if (cp == name) {
317		printf("missing device name\n");
318		return(1);
319	}
320	if (*cp == '\0') {
321		printf("missing unit number\n");
322		return(1);
323	}
324	unit = *cp - '0';
325	*cp++ = '\0';
326	for (bd = 0; bd < NUMCDEVSW; bd++) {
327		dev = makebdev(bd, 0);
328		if (devsw(dev) != NULL &&
329		    strcmp(devsw(dev)->d_name, name) == 0)
330			goto gotit;
331	}
332	printf("no such device '%s'\n", name);
333	return (2);
334gotit:
335	while (*cp >= '0' && *cp <= '9')
336		unit += 10 * unit + *cp++ - '0';
337	if (*cp == 's' && cp[1] >= '0' && cp[1] <= '9') {
338		slice = cp[1] - '0' + 1;
339		cp += 2;
340	}
341	if (*cp >= 'a' && *cp <= 'h') {
342		part = *cp - 'a';
343		cp++;
344	}
345	if (*cp != '\0') {
346		printf("junk after name\n");
347		return (1);
348	}
349	rootdev = makebdev(bd, dkmakeminor(unit, slice, part));
350	return 0;
351}
352
353