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