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