Rename struct specinfo into struct cdev. Add a new typedef 'cdev_t' for cdev
[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 $
efda3bd0 29 * $DragonFly: src/sys/kern/vfs_conf.c,v 1.18 2006/09/05 00:55:45 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;
21739618 75struct namecache *rootncp;
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'.
984263bc
MD
81 */
82static char *cdrom_rootdevnames[] = {
6fcb48f2
MD
83 "cd9660:cd0c",
84 "cd9660:acd0c",
85 "cd9660:cd1c",
86 "cd9660:acd1c",
984263bc
MD
87 NULL
88};
89
90static void vfs_mountroot(void *junk);
6b98993e 91static int vfs_mountroot_try(const char *mountfrom);
984263bc
MD
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{
6b98993e
MD
107 int i;
108 dev_t save_rootdev = rootdev;
984263bc
MD
109
110 /*
111 * The root filesystem information is compiled in, and we are
112 * booted with instructions to use it.
113 */
114#ifdef ROOTDEVNAME
115 if ((boothowto & RB_DFLTROOT) &&
116 !vfs_mountroot_try(ROOTDEVNAME))
117 return;
118#endif
119 /*
120 * We are booted with instructions to prompt for the root filesystem,
121 * or to use the compiled-in default when it doesn't exist.
122 */
123 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
124 if (!vfs_mountroot_ask())
125 return;
126 }
127
128 /*
129 * We've been given the generic "use CDROM as root" flag. This is
130 * necessary because one media may be used in many different
131 * devices, so we need to search for them.
132 */
133 if (boothowto & RB_CDROM) {
134 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
135 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
136 return;
137 }
138 }
139
140 /*
141 * Try to use the value read by the loader from /etc/fstab, or
142 * supplied via some other means. This is the preferred
143 * mechanism.
144 */
bc01a404 145 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
984263bc
MD
146 return;
147
6b98993e
MD
148 /*
149 * If a vfs set rootdev, try it (XXX VINUM HACK!)
150 */
151 if (save_rootdev != NODEV) {
152 rootdev = save_rootdev;
153 if (!vfs_mountroot_try(""))
154 return;
155 }
156
984263bc
MD
157 /*
158 * Try values that may have been computed by the machine-dependant
159 * legacy code.
160 */
6b98993e 161 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
984263bc 162 return;
6b98993e 163 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
984263bc
MD
164 return;
165
166 /*
167 * If we have a compiled-in default, and haven't already tried it, try
168 * it now.
169 */
170#ifdef ROOTDEVNAME
171 if (!(boothowto & RB_DFLTROOT))
172 if (!vfs_mountroot_try(ROOTDEVNAME))
173 return;
174#endif
175
176 /*
177 * Everything so far has failed, prompt on the console if we haven't
178 * already tried that.
179 */
180 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
181 return;
182 panic("Root mount failed, startup aborted.");
183}
184
185/*
186 * Mount (mountfrom) as the root filesystem.
187 */
188static int
6b98993e 189vfs_mountroot_try(const char *mountfrom)
984263bc
MD
190{
191 struct mount *mp;
6b98993e 192 char *vfsname, *devname;
984263bc
MD
193 int error;
194 char patt[32];
984263bc
MD
195
196 vfsname = NULL;
6b98993e 197 devname = NULL;
984263bc
MD
198 mp = NULL;
199 error = EINVAL;
200
201 if (mountfrom == NULL)
202 return(error); /* don't complain */
203
e43a034f 204 crit_enter();
984263bc 205 printf("Mounting root from %s\n", mountfrom);
e43a034f 206 crit_exit();
984263bc 207
6b98993e 208 /* parse vfs name and devname */
efda3bd0
MD
209 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
210 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK);
6b98993e 211 vfsname[0] = devname[0] = 0;
984263bc 212 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
6b98993e 213 if (sscanf(mountfrom, patt, vfsname, devname) < 1)
984263bc
MD
214 goto done;
215
216 /* allocate a root mount */
6b98993e
MD
217 error = vfs_rootmountalloc(vfsname,
218 devname[0] != 0 ? devname : ROOTNAME, &mp);
984263bc
MD
219 if (error != 0) {
220 printf("Can't allocate root mount for filesystem '%s': %d\n",
221 vfsname, error);
222 goto done;
223 }
224 mp->mnt_flag |= MNT_ROOTFS;
225
226 /* do our best to set rootdev */
6b98993e 227 if ((devname[0] != 0) && setrootbyname(devname))
984263bc
MD
228 printf("setrootbyname failed\n");
229
230 /* If the root device is a type "memory disk", mount RW */
e4c9c0c8 231 if (rootdev != NODEV && dev_is_good(rootdev) &&
335dda38 232 (dev_dflags(rootdev) & D_MEMDISK)) {
984263bc 233 mp->mnt_flag &= ~MNT_RDONLY;
335dda38 234 }
984263bc 235
acde96db 236 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
984263bc
MD
237
238done:
239 if (vfsname != NULL)
efda3bd0 240 kfree(vfsname, M_MOUNT);
6b98993e 241 if (devname != NULL)
efda3bd0 242 kfree(devname, M_MOUNT);
984263bc
MD
243 if (error != 0) {
244 if (mp != NULL) {
f9642f56 245 vfs_unbusy(mp);
efda3bd0 246 kfree(mp, M_MOUNT);
984263bc
MD
247 }
248 printf("Root mount failed: %d\n", error);
249 } else {
984263bc 250 /* register with list of mounted filesystems */
861905fb 251 mountlist_insert(mp, MNTINS_FIRST);
984263bc 252
8a8d5d85 253 /* sanity check system clock against root fs timestamp */
984263bc 254 inittodr(mp->mnt_time);
f9642f56 255 vfs_unbusy(mp);
984263bc
MD
256 }
257 return(error);
258}
259
260/*
261 * Spin prompting on the console for a suitable root filesystem
262 */
263static int
264vfs_mountroot_ask(void)
265{
266 char name[128];
267 int i;
268 dev_t dev;
269
270 for(;;) {
271 printf("\nManual root filesystem specification:\n");
272 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
6b98993e 273 printf(" eg. ufs:da0s1a\n");
984263bc
MD
274 printf(" ? List valid disk boot devices\n");
275 printf(" <empty line> Abort manual input\n");
276 printf("\nmountroot> ");
277 gets(name);
278 if (name[0] == 0)
279 return(1);
280 if (name[0] == '?') {
281 printf("Possibly valid devices for 'ufs' root:\n");
282 for (i = 0; i < NUMCDEVSW; i++) {
e4c9c0c8
MD
283 dev = udev2dev(makeudev(i, 0), 0);
284 if (dev_is_good(dev))
335dda38 285 printf(" \"%s\"", dev_dname(dev));
984263bc
MD
286 }
287 printf("\n");
288 continue;
289 }
290 if (!vfs_mountroot_try(name))
291 return(0);
292 }
293}
294
295static void
296gets(char *cp)
297{
298 char *lp;
299 int c;
300
301 lp = cp;
302 for (;;) {
303 printf("%c", c = cngetc() & 0177);
304 switch (c) {
305 case -1:
306 case '\n':
307 case '\r':
308 *lp++ = '\0';
309 return;
310 case '\b':
311 case '\177':
312 if (lp > cp) {
313 printf(" \b");
314 lp--;
315 }
316 continue;
317 case '#':
318 lp--;
319 if (lp < cp)
320 lp = cp;
321 continue;
322 case '@':
323 case 'u' & 037:
324 lp = cp;
325 printf("%c", '\n');
326 continue;
327 default:
328 *lp++ = c;
329 }
330 }
331}
332
333/*
334 * Convert a given name to the dev_t of the disk-like device
335 * it refers to.
336 */
337dev_t
bc01a404 338kgetdiskbyname(const char *name)
e4c9c0c8 339{
984263bc 340 char *cp;
6b98993e 341 int nlen;
984263bc
MD
342 int cd, unit, slice, part;
343 dev_t dev;
e4c9c0c8 344 dev_t rdev;
984263bc 345
6b98993e
MD
346 /*
347 * Get the base name of the device
348 */
1f2de5d4
MD
349 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
350 name += sizeof(__SYS_PATH_DEV) - 1;
6b98993e
MD
351 cp = __DECONST(char *, name);
352 while (*cp == '/')
353 ++cp;
354 while (*cp >= 'a' && *cp <= 'z')
355 ++cp;
984263bc
MD
356 if (cp == name) {
357 printf("missing device name\n");
358 return (NODEV);
359 }
6b98993e
MD
360 nlen = cp - name;
361
362 /*
363 * Get the unit.
364 */
365 unit = strtol(cp, &cp, 10);
366 if (name + nlen == (const char *)cp || unit < 0 || unit > DKMAXUNIT) {
367 printf("bad unit: %d\n", unit);
984263bc
MD
368 return (NODEV);
369 }
6b98993e
MD
370
371 /*
372 * Get the slice. Note that if no partition or partition 'a' is
373 * specified, and no slice is specified, we will try both 'ad0a'
374 * (which is what you get when slice is 0), and also 'ad0' (the
375 * whole-disk partition, slice == 1).
376 */
377 if (*cp == 's') {
378 slice = cp[1] - '0';
379 if (slice < 1 || slice > 9) {
380 printf("bad slice number\n");
381 return(NODEV);
382 }
383 ++slice; /* slice #1 starts at 2 */
984263bc 384 cp += 2;
6b98993e
MD
385 } else {
386 slice = 0;
984263bc 387 }
6b98993e
MD
388
389 /*
390 * Get the partition.
391 */
392 if (*cp >= 'a' && *cp <= 'p') {
984263bc 393 part = *cp - 'a';
6b98993e
MD
394 ++cp;
395 } else {
396 part = 0;
984263bc 397 }
6b98993e 398
984263bc
MD
399 if (*cp != '\0') {
400 printf("junk after name\n");
401 return (NODEV);
402 }
6b98993e
MD
403
404 /*
405 * Locate the device
406 */
407 for (cd = 0; cd < NUMCDEVSW; cd++) {
408 const char *dname;
409
410 dev = udev2dev(makeudev(cd, dkmakeminor(unit, slice, part)), 0);
411 if (dev_is_good(dev) && (dname = dev_dname(dev)) != NULL) {
412 if (strlen(dname) == nlen &&
413 strncmp(dname, name, nlen) == 0) {
414 break;
415 }
416 }
417 }
418 if (cd == NUMCDEVSW) {
419 printf("no such device '%*.*s'\n", nlen, nlen, name);
420 return (NODEV);
421 }
422
423 /*
424 * FOUND DEVICE
425 */
e4c9c0c8
MD
426 rdev = make_sub_dev(dev, dkmakeminor(unit, slice, part));
427 return(rdev);
984263bc
MD
428}
429
430/*
431 * Set rootdev to match (name), given that we expect it to
432 * refer to a disk-like device.
433 */
434static int
435setrootbyname(char *name)
436{
437 dev_t diskdev;
438
bc01a404 439 diskdev = kgetdiskbyname(name);
984263bc
MD
440 if (diskdev != NODEV) {
441 rootdev = diskdev;
442 return (0);
443 }
444
445 return (1);
446}
447
448#ifdef DDB
449DB_SHOW_COMMAND(disk, db_getdiskbyname)
450{
451 dev_t dev;
452
453 if (modif[0] == '\0') {
454 db_error("usage: show disk/devicename");
455 return;
456 }
bc01a404 457 dev = kgetdiskbyname(modif);
984263bc
MD
458 if (dev != NODEV)
459 db_printf("dev_t = %p\n", dev);
460 else
461 db_printf("No disk device matched.\n");
462}
463#endif