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