Replace NOCDEV with NULL. NOCDEV was ((void *)-1) and as inherited
[games.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 $
028066b1 29 * $DragonFly: src/sys/kern/vfs_conf.c,v 1.28 2007/05/08 02:31:42 dillon Exp $
984263bc
MD
30 */
31
32/*
33 * Locate and mount the root filesystem.
34 *
35 * The root filesystem is detailed in the kernel environment variable
36 * vfs.root.mountfrom, which is expected to be in the general format
37 *
38 * <vfsname>:[<path>]
39 * vfsname := the name of a VFS known to the kernel and capable
40 * of being mounted as root
41 * path := disk device name or other data used by the filesystem
42 * to locate its physical store
43 *
44 */
45
46#include "opt_rootdevname.h"
47
48#include <sys/param.h>
49#include <sys/kernel.h>
50#include <sys/systm.h>
51#include <sys/proc.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>
335dda38 60#include <sys/device.h>
21739618 61#include <sys/namecache.h>
1f2de5d4 62#include <sys/paths.h>
e43a034f 63#include <sys/thread2.h>
984263bc
MD
64
65#include "opt_ddb.h"
66#ifdef DDB
67#include <ddb/ddb.h>
68#endif
69
70MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
71
72#define ROOTNAME "root_device"
73
74struct vnode *rootvnode;
28623bf9 75struct nchandle rootnch;
984263bc
MD
76
77/*
6fcb48f2
MD
78 * The root specifiers we will try if RB_CDROM is specified. Note that
79 * the ATA driver will accept acd*a and acd*c, but the SCSI driver
80 * will only accept cd*c, so use 'c'.
926a7bdb
TS
81 *
82 * XXX TGEN NATA and, presumably, 'old'ATA will also accept the device name
83 * without any fake partition, since the major & minor are identical for all
84 * three (acd*, acd*a and acd*c). However, due to an as-of-yet undiscovered
85 * bug, acd0c ends up with minor 2 when using NATA and booting cold. Since
86 * NATA's acd_open() is unable to fulfill mounts on such 'ghost' cdevs, acd0
87 * and acd1 have been added to the list of CD-ROM root device names.
984263bc
MD
88 */
89static char *cdrom_rootdevnames[] = {
6fcb48f2
MD
90 "cd9660:cd0c",
91 "cd9660:acd0c",
92 "cd9660:cd1c",
93 "cd9660:acd1c",
926a7bdb
TS
94 "cd9660:acd0",
95 "cd9660:acd1",
984263bc
MD
96 NULL
97};
98
99static void vfs_mountroot(void *junk);
6b98993e 100static int vfs_mountroot_try(const char *mountfrom);
984263bc 101static int vfs_mountroot_ask(void);
cee92776 102static int getline(char *cp, int limit);
984263bc
MD
103
104/* legacy find-root code */
105char *rootdevnames[2] = {NULL, NULL};
106static int setrootbyname(char *name);
107
108SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
109
110/*
111 * Find and mount the root filesystem
112 */
113static void
114vfs_mountroot(void *junk)
115{
6b98993e 116 int i;
b13267a5 117 cdev_t save_rootdev = rootdev;
984263bc
MD
118
119 /*
120 * The root filesystem information is compiled in, and we are
121 * booted with instructions to use it.
122 */
123#ifdef ROOTDEVNAME
124 if ((boothowto & RB_DFLTROOT) &&
125 !vfs_mountroot_try(ROOTDEVNAME))
126 return;
127#endif
128 /*
129 * We are booted with instructions to prompt for the root filesystem,
130 * or to use the compiled-in default when it doesn't exist.
131 */
132 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
133 if (!vfs_mountroot_ask())
134 return;
135 }
136
137 /*
138 * We've been given the generic "use CDROM as root" flag. This is
139 * necessary because one media may be used in many different
140 * devices, so we need to search for them.
141 */
142 if (boothowto & RB_CDROM) {
143 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
144 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
145 return;
146 }
147 }
148
149 /*
150 * Try to use the value read by the loader from /etc/fstab, or
151 * supplied via some other means. This is the preferred
152 * mechanism.
153 */
bc01a404 154 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
984263bc
MD
155 return;
156
6b98993e
MD
157 /*
158 * If a vfs set rootdev, try it (XXX VINUM HACK!)
159 */
028066b1 160 if (save_rootdev != NULL) {
6b98993e
MD
161 rootdev = save_rootdev;
162 if (!vfs_mountroot_try(""))
163 return;
164 }
165
984263bc
MD
166 /*
167 * Try values that may have been computed by the machine-dependant
168 * legacy code.
169 */
6b98993e 170 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
984263bc 171 return;
6b98993e 172 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
984263bc
MD
173 return;
174
175 /*
176 * If we have a compiled-in default, and haven't already tried it, try
177 * it now.
178 */
179#ifdef ROOTDEVNAME
180 if (!(boothowto & RB_DFLTROOT))
181 if (!vfs_mountroot_try(ROOTDEVNAME))
182 return;
183#endif
184
185 /*
186 * Everything so far has failed, prompt on the console if we haven't
187 * already tried that.
188 */
189 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
190 return;
191 panic("Root mount failed, startup aborted.");
192}
193
194/*
195 * Mount (mountfrom) as the root filesystem.
196 */
197static int
6b98993e 198vfs_mountroot_try(const char *mountfrom)
984263bc
MD
199{
200 struct mount *mp;
6b98993e 201 char *vfsname, *devname;
984263bc
MD
202 int error;
203 char patt[32];
984263bc
MD
204
205 vfsname = NULL;
6b98993e 206 devname = NULL;
984263bc
MD
207 mp = NULL;
208 error = EINVAL;
209
210 if (mountfrom == NULL)
211 return(error); /* don't complain */
212
e43a034f 213 crit_enter();
6ea70f76 214 kprintf("Mounting root from %s\n", mountfrom);
e43a034f 215 crit_exit();
984263bc 216
6b98993e 217 /* parse vfs name and devname */
efda3bd0
MD
218 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
219 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK);
6b98993e 220 vfsname[0] = devname[0] = 0;
f8c7a42d 221 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
e93690c2 222 if (ksscanf(mountfrom, patt, vfsname, devname) < 1)
984263bc
MD
223 goto done;
224
225 /* allocate a root mount */
6b98993e
MD
226 error = vfs_rootmountalloc(vfsname,
227 devname[0] != 0 ? devname : ROOTNAME, &mp);
984263bc 228 if (error != 0) {
6ea70f76 229 kprintf("Can't allocate root mount for filesystem '%s': %d\n",
984263bc
MD
230 vfsname, error);
231 goto done;
232 }
233 mp->mnt_flag |= MNT_ROOTFS;
234
235 /* do our best to set rootdev */
6b98993e 236 if ((devname[0] != 0) && setrootbyname(devname))
6ea70f76 237 kprintf("setrootbyname failed\n");
984263bc
MD
238
239 /* If the root device is a type "memory disk", mount RW */
028066b1 240 if (rootdev != NULL && dev_is_good(rootdev) &&
335dda38 241 (dev_dflags(rootdev) & D_MEMDISK)) {
984263bc 242 mp->mnt_flag &= ~MNT_RDONLY;
335dda38 243 }
984263bc 244
acde96db 245 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
984263bc
MD
246
247done:
248 if (vfsname != NULL)
efda3bd0 249 kfree(vfsname, M_MOUNT);
6b98993e 250 if (devname != NULL)
efda3bd0 251 kfree(devname, M_MOUNT);
984263bc
MD
252 if (error != 0) {
253 if (mp != NULL) {
f9642f56 254 vfs_unbusy(mp);
efda3bd0 255 kfree(mp, M_MOUNT);
984263bc 256 }
6ea70f76 257 kprintf("Root mount failed: %d\n", error);
984263bc 258 } else {
984263bc 259 /* register with list of mounted filesystems */
861905fb 260 mountlist_insert(mp, MNTINS_FIRST);
984263bc 261
8a8d5d85 262 /* sanity check system clock against root fs timestamp */
984263bc 263 inittodr(mp->mnt_time);
f9642f56 264 vfs_unbusy(mp);
984263bc
MD
265 }
266 return(error);
267}
268
269/*
270 * Spin prompting on the console for a suitable root filesystem
271 */
272static int
273vfs_mountroot_ask(void)
274{
275 char name[128];
276 int i;
cee92776 277 int llimit = 100;
b13267a5 278 cdev_t dev;
984263bc 279
cee92776
MD
280 kprintf("\nManual root filesystem specification:\n");
281 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n");
282 kprintf(" ? List valid disk boot devices\n");
283 kprintf(" panic Just panic\n");
284 kprintf(" abort Abort manual input\n");
285 while (llimit--) {
6ea70f76 286 kprintf("\nmountroot> ");
cee92776
MD
287
288 if (getline(name, 128) < 0)
289 break;
290 if (name[0] == 0) {
291 ;
292 } else if (name[0] == '?') {
6ea70f76 293 kprintf("Possibly valid devices for 'ufs' root:\n");
984263bc 294 for (i = 0; i < NUMCDEVSW; i++) {
e4c9c0c8
MD
295 dev = udev2dev(makeudev(i, 0), 0);
296 if (dev_is_good(dev))
6ea70f76 297 kprintf(" \"%s\"", dev_dname(dev));
984263bc 298 }
6ea70f76 299 kprintf("\n");
984263bc 300 continue;
cee92776
MD
301 } else if (strcmp(name, "panic") == 0) {
302 panic("panic from console");
303 } else if (strcmp(name, "abort") == 0) {
304 break;
305 } else if (vfs_mountroot_try(name) == 0) {
984263bc 306 return(0);
cee92776 307 }
984263bc 308 }
cee92776 309 return(1);
984263bc
MD
310}
311
cee92776
MD
312static int
313getline(char *cp, int limit)
984263bc
MD
314{
315 char *lp;
316 int c;
317
318 lp = cp;
319 for (;;) {
7f12a969
MD
320 c = cngetc();
321
984263bc
MD
322 switch (c) {
323 case -1:
cee92776 324 return(-1);
984263bc
MD
325 case '\n':
326 case '\r':
7f12a969 327 kprintf("\n");
984263bc 328 *lp++ = '\0';
cee92776 329 return(0);
984263bc
MD
330 case '\b':
331 case '\177':
332 if (lp > cp) {
7f12a969 333 kprintf("\b \b");
984263bc 334 lp--;
7f12a969
MD
335 } else {
336 kprintf("%c", 7);
984263bc
MD
337 }
338 continue;
339 case '#':
7f12a969 340 kprintf("#");
984263bc
MD
341 lp--;
342 if (lp < cp)
343 lp = cp;
344 continue;
345 case '@':
346 case 'u' & 037:
347 lp = cp;
6ea70f76 348 kprintf("%c", '\n');
984263bc
MD
349 continue;
350 default:
cee92776
MD
351 if (lp - cp >= limit - 1) {
352 kprintf("%c", 7);
353 } else {
7f12a969 354 kprintf("%c", c);
cee92776
MD
355 *lp++ = c;
356 }
357 continue;
984263bc
MD
358 }
359 }
360}
361
362/*
b13267a5 363 * Convert a given name to the cdev_t of the disk-like device
984263bc
MD
364 * it refers to.
365 */
b13267a5 366cdev_t
bc01a404 367kgetdiskbyname(const char *name)
e4c9c0c8 368{
984263bc 369 char *cp;
6b98993e 370 int nlen;
984263bc 371 int cd, unit, slice, part;
b13267a5
MD
372 cdev_t dev;
373 cdev_t rdev;
984263bc 374
6b98993e
MD
375 /*
376 * Get the base name of the device
377 */
1f2de5d4
MD
378 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
379 name += sizeof(__SYS_PATH_DEV) - 1;
6b98993e
MD
380 cp = __DECONST(char *, name);
381 while (*cp == '/')
382 ++cp;
383 while (*cp >= 'a' && *cp <= 'z')
384 ++cp;
984263bc 385 if (cp == name) {
6ea70f76 386 kprintf("missing device name\n");
028066b1 387 return (NULL);
984263bc 388 }
6b98993e
MD
389 nlen = cp - name;
390
391 /*
392 * Get the unit.
393 */
394 unit = strtol(cp, &cp, 10);
395 if (name + nlen == (const char *)cp || unit < 0 || unit > DKMAXUNIT) {
6ea70f76 396 kprintf("bad unit: %d\n", unit);
028066b1 397 return (NULL);
984263bc 398 }
6b98993e
MD
399
400 /*
401 * Get the slice. Note that if no partition or partition 'a' is
402 * specified, and no slice is specified, we will try both 'ad0a'
403 * (which is what you get when slice is 0), and also 'ad0' (the
404 * whole-disk partition, slice == 1).
405 */
406 if (*cp == 's') {
407 slice = cp[1] - '0';
408 if (slice < 1 || slice > 9) {
6ea70f76 409 kprintf("bad slice number\n");
028066b1 410 return(NULL);
6b98993e
MD
411 }
412 ++slice; /* slice #1 starts at 2 */
984263bc 413 cp += 2;
6b98993e
MD
414 } else {
415 slice = 0;
984263bc 416 }
6b98993e
MD
417
418 /*
419 * Get the partition.
420 */
421 if (*cp >= 'a' && *cp <= 'p') {
984263bc 422 part = *cp - 'a';
6b98993e
MD
423 ++cp;
424 } else {
425 part = 0;
984263bc 426 }
6b98993e 427
984263bc 428 if (*cp != '\0') {
6ea70f76 429 kprintf("junk after name\n");
028066b1 430 return (NULL);
984263bc 431 }
6b98993e
MD
432
433 /*
434 * Locate the device
435 */
436 for (cd = 0; cd < NUMCDEVSW; cd++) {
437 const char *dname;
438
439 dev = udev2dev(makeudev(cd, dkmakeminor(unit, slice, part)), 0);
440 if (dev_is_good(dev) && (dname = dev_dname(dev)) != NULL) {
441 if (strlen(dname) == nlen &&
442 strncmp(dname, name, nlen) == 0) {
443 break;
444 }
445 }
446 }
447 if (cd == NUMCDEVSW) {
6ea70f76 448 kprintf("no such device '%*.*s'\n", nlen, nlen, name);
028066b1 449 return (NULL);
6b98993e
MD
450 }
451
452 /*
453 * FOUND DEVICE
454 */
e4c9c0c8
MD
455 rdev = make_sub_dev(dev, dkmakeminor(unit, slice, part));
456 return(rdev);
984263bc
MD
457}
458
459/*
460 * Set rootdev to match (name), given that we expect it to
461 * refer to a disk-like device.
462 */
463static int
464setrootbyname(char *name)
465{
b13267a5 466 cdev_t diskdev;
984263bc 467
bc01a404 468 diskdev = kgetdiskbyname(name);
028066b1 469 if (diskdev != NULL) {
984263bc
MD
470 rootdev = diskdev;
471 return (0);
472 }
473
474 return (1);
475}
476
477#ifdef DDB
478DB_SHOW_COMMAND(disk, db_getdiskbyname)
479{
b13267a5 480 cdev_t dev;
984263bc
MD
481
482 if (modif[0] == '\0') {
483 db_error("usage: show disk/devicename");
484 return;
485 }
bc01a404 486 dev = kgetdiskbyname(modif);
028066b1 487 if (dev != NULL)
b13267a5 488 db_printf("cdev_t = %p\n", dev);
984263bc
MD
489 else
490 db_printf("No disk device matched.\n");
491}
492#endif