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