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