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