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