vfs_mount.c revision 94936
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 94936 2002-04-17 13:06:36Z mux $
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 "opt_rootdevname.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/lock.h>
51#include <sys/mutex.h>
52#include <sys/vnode.h>
53#include <sys/mount.h>
54#include <sys/malloc.h>
55#include <sys/reboot.h>
56#include <sys/diskslice.h>
57#include <sys/disklabel.h>
58#include <sys/conf.h>
59#include <sys/cons.h>
60#include <sys/proc.h>
61
62#include "opt_ddb.h"
63
64#ifdef DDB
65#include <ddb/ddb.h>
66#endif
67
68#include <paths.h>
69
70MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
71
72#define ROOTNAME	"root_device"
73
74/*
75 * The vnode of the system's root (/ in the filesystem, without chroot
76 * active.)
77 */
78struct vnode	*rootvnode;
79
80/*
81 * The root specifiers we will try if RB_CDROM is specified.
82 */
83static char *cdrom_rootdevnames[] = {
84	"cd9660:cd0a",
85	"cd9660:acd0a",
86	"cd9660:wcd0a",
87	NULL
88};
89
90static int	vfs_mountroot_try(char *mountfrom);
91static int	vfs_mountroot_ask(void);
92static void	gets(char *cp);
93
94/* legacy find-root code */
95char		*rootdevnames[2] = {NULL, NULL};
96static int	setrootbyname(char *name);
97dev_t		rootdev = NODEV;
98
99/*
100 * Find and mount the root filesystem
101 */
102void
103vfs_mountroot(void *foo __unused)
104{
105	char		*cp;
106	int		i, error;
107
108	/*
109	 * The root filesystem information is compiled in, and we are
110	 * booted with instructions to use it.
111	 */
112#ifdef ROOTDEVNAME
113	if ((boothowto & RB_DFLTROOT) &&
114	    !vfs_mountroot_try(ROOTDEVNAME))
115		return;
116#endif
117	/*
118	 * We are booted with instructions to prompt for the root filesystem,
119	 * or to use the compiled-in default when it doesn't exist.
120	 */
121	if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
122		if (!vfs_mountroot_ask())
123			return;
124	}
125
126	/*
127	 * We've been given the generic "use CDROM as root" flag.  This is
128	 * necessary because one media may be used in many different
129	 * devices, so we need to search for them.
130	 */
131	if (boothowto & RB_CDROM) {
132		for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
133			if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
134				return;
135		}
136	}
137
138	/*
139	 * Try to use the value read by the loader from /etc/fstab, or
140	 * supplied via some other means.  This is the preferred
141	 * mechanism.
142	 */
143	if ((cp = getenv("vfs.root.mountfrom")) != NULL) {
144		error = vfs_mountroot_try(cp);
145		freeenv(cp);
146		if (!error)
147			return;
148	}
149
150	/*
151	 * Try values that may have been computed by the machine-dependant
152	 * legacy code.
153	 */
154	if (!vfs_mountroot_try(rootdevnames[0]))
155		return;
156	if (!vfs_mountroot_try(rootdevnames[1]))
157		return;
158
159	/*
160	 * If we have a compiled-in default, and haven't already tried it, try
161	 * it now.
162	 */
163#ifdef ROOTDEVNAME
164	if (!(boothowto & RB_DFLTROOT))
165		if (!vfs_mountroot_try(ROOTDEVNAME))
166			return;
167#endif
168
169	/*
170	 * Everything so far has failed, prompt on the console if we haven't
171	 * already tried that.
172	 */
173	if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
174		return;
175	panic("Root mount failed, startup aborted.");
176}
177
178/*
179 * Mount (mountfrom) as the root filesystem.
180 */
181static int
182vfs_mountroot_try(char *mountfrom)
183{
184        struct mount	*mp;
185	char		*vfsname, *path;
186	int		error;
187	char		patt[32];
188	int		s;
189
190	vfsname = NULL;
191	path    = NULL;
192	mp      = NULL;
193	error   = EINVAL;
194
195	if (mountfrom == NULL)
196		return(error);		/* don't complain */
197
198	s = splcam();			/* Overkill, but annoying without it */
199	printf("Mounting root from %s\n", mountfrom);
200	splx(s);
201
202	/* parse vfs name and path */
203	vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
204	path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
205	vfsname[0] = path[0] = 0;
206	sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
207	if (sscanf(mountfrom, patt, vfsname, path) < 1)
208		goto done;
209
210	/* allocate a root mount */
211	error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
212				   &mp);
213	if (error != 0) {
214		printf("Can't allocate root mount for filesystem '%s': %d\n",
215		       vfsname, error);
216		goto done;
217	}
218	mp->mnt_flag |= MNT_ROOTFS;
219
220	/* do our best to set rootdev */
221	if ((path[0] != 0) && setrootbyname(path))
222		printf("setrootbyname failed\n");
223
224	/* If the root device is a type "memory disk", mount RW */
225	if (rootdev != NODEV && devsw(rootdev) &&
226	    (devsw(rootdev)->d_flags & D_MEMDISK))
227		mp->mnt_flag &= ~MNT_RDONLY;
228
229	/*
230	 * Set the mount path to be something useful, because the
231	 * filesystem code isn't responsible now for initialising
232	 * f_mntonname unless they want to override the default
233	 * (which is `path'.)
234	 */
235	strncpy(mp->mnt_stat.f_mntonname, "/", MNAMELEN);
236
237	error = VFS_MOUNT(mp, NULL, NULL, NULL, curthread);
238
239done:
240	if (vfsname != NULL)
241		free(vfsname, M_MOUNT);
242	if (path != NULL)
243		free(path, M_MOUNT);
244	if (error != 0) {
245		if (mp != NULL) {
246			vfs_unbusy(mp, curthread);
247			free(mp, M_MOUNT);
248		}
249		printf("Root mount failed: %d\n", error);
250	} else {
251
252		/* register with list of mounted filesystems */
253		mtx_lock(&mountlist_mtx);
254		TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
255		mtx_unlock(&mountlist_mtx);
256
257		/* sanity check system clock against root filesystem timestamp */
258		inittodr(mp->mnt_time);
259		vfs_unbusy(mp, curthread);
260	}
261	return(error);
262}
263
264/*
265 * Spin prompting on the console for a suitable root filesystem
266 */
267static int
268vfs_mountroot_ask(void)
269{
270	char name[128];
271	int i;
272	dev_t dev;
273
274	for(;;) {
275		printf("\nManual root filesystem specification:\n");
276		printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
277#if defined(__i386__) || defined(__ia64__)
278		printf("                       eg. ufs:da0s1a\n");
279#else
280		printf("                       eg. ufs:da0a\n");
281#endif
282		printf("  ?                  List valid disk boot devices\n");
283		printf("  <empty line>       Abort manual input\n");
284		printf("\nmountroot> ");
285		gets(name);
286		if (name[0] == 0)
287			return(1);
288		if (name[0] == '?') {
289			printf("Possibly valid devices for 'ufs' root:\n");
290			for (i = 0; i < NUMCDEVSW; i++) {
291				dev = makedev(i, 0);
292				if (devsw(dev) != NULL)
293					printf(" \"%s\"", devsw(dev)->d_name);
294			}
295			printf("\n");
296			continue;
297		}
298		if (!vfs_mountroot_try(name))
299			return(0);
300	}
301}
302
303/*
304 * Local helper function for vfs_mountroot_ask.
305 */
306static void
307gets(char *cp)
308{
309	char *lp;
310	int c;
311
312	lp = cp;
313	for (;;) {
314		printf("%c", c = cngetc() & 0177);
315		switch (c) {
316		case -1:
317		case '\n':
318		case '\r':
319			*lp++ = '\0';
320			return;
321		case '\b':
322		case '\177':
323			if (lp > cp) {
324				printf(" \b");
325				lp--;
326			}
327			continue;
328		case '#':
329			lp--;
330			if (lp < cp)
331				lp = cp;
332			continue;
333		case '@':
334		case 'u' & 037:
335			lp = cp;
336			printf("%c", '\n');
337			continue;
338		default:
339			*lp++ = c;
340		}
341	}
342}
343
344/*
345 * Convert a given name to the dev_t of the disk-like device
346 * it refers to.
347 */
348dev_t
349getdiskbyname(char *name) {
350	char *cp;
351	dev_t dev;
352
353	cp = name;
354	if (!bcmp(cp, "/dev/", 5))
355		cp += 5;
356
357	dev = NODEV;
358	EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev);
359	return (dev);
360}
361
362/*
363 * Set rootdev to match (name), given that we expect it to
364 * refer to a disk-like device.
365 */
366static int
367setrootbyname(char *name)
368{
369	dev_t diskdev;
370
371	diskdev = getdiskbyname(name);
372	if (diskdev != NODEV) {
373		rootdev = diskdev;
374		return (0);
375	}
376
377	return (1);
378}
379
380/* Show the dev_t for a disk specified by name */
381#ifdef DDB
382DB_SHOW_COMMAND(disk, db_getdiskbyname)
383{
384	dev_t dev;
385
386	if (modif[0] == '\0') {
387		db_error("usage: show disk/devicename");
388		return;
389	}
390	dev = getdiskbyname(modif);
391	if (dev != NODEV)
392		db_printf("dev_t = %p\n", dev);
393	else
394		db_printf("No disk device matched.\n");
395}
396#endif
397