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 * $DragonFly: src/sys/kern/vfs_conf.c,v 1.25 2007/01/09 22:36:00 tgen Exp $ 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> 60 #include <sys/device.h> 61 #include <sys/namecache.h> 62 #include <sys/paths.h> 63 #include <sys/thread2.h> 64 65 #include "opt_ddb.h" 66 #ifdef DDB 67 #include <ddb/ddb.h> 68 #endif 69 70 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 71 72 #define ROOTNAME "root_device" 73 74 struct vnode *rootvnode; 75 struct nchandle rootnch; 76 77 /* 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'. 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. 88 */ 89 static char *cdrom_rootdevnames[] = { 90 "cd9660:cd0c", 91 "cd9660:acd0c", 92 "cd9660:cd1c", 93 "cd9660:acd1c", 94 "cd9660:acd0", 95 "cd9660:acd1", 96 NULL 97 }; 98 99 static void vfs_mountroot(void *junk); 100 static int vfs_mountroot_try(const char *mountfrom); 101 static int vfs_mountroot_ask(void); 102 static void gets(char *cp); 103 104 /* legacy find-root code */ 105 char *rootdevnames[2] = {NULL, NULL}; 106 static int setrootbyname(char *name); 107 108 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL); 109 110 /* 111 * Find and mount the root filesystem 112 */ 113 static void 114 vfs_mountroot(void *junk) 115 { 116 int i; 117 cdev_t save_rootdev = rootdev; 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 */ 154 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom"))) 155 return; 156 157 /* 158 * If a vfs set rootdev, try it (XXX VINUM HACK!) 159 */ 160 if (save_rootdev != NOCDEV) { 161 rootdev = save_rootdev; 162 if (!vfs_mountroot_try("")) 163 return; 164 } 165 166 /* 167 * Try values that may have been computed by the machine-dependant 168 * legacy code. 169 */ 170 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0])) 171 return; 172 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1])) 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 */ 197 static int 198 vfs_mountroot_try(const char *mountfrom) 199 { 200 struct mount *mp; 201 char *vfsname, *devname; 202 int error; 203 char patt[32]; 204 205 vfsname = NULL; 206 devname = NULL; 207 mp = NULL; 208 error = EINVAL; 209 210 if (mountfrom == NULL) 211 return(error); /* don't complain */ 212 213 crit_enter(); 214 kprintf("Mounting root from %s\n", mountfrom); 215 crit_exit(); 216 217 /* parse vfs name and devname */ 218 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 219 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK); 220 vfsname[0] = devname[0] = 0; 221 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 222 if (ksscanf(mountfrom, patt, vfsname, devname) < 1) 223 goto done; 224 225 /* allocate a root mount */ 226 error = vfs_rootmountalloc(vfsname, 227 devname[0] != 0 ? devname : ROOTNAME, &mp); 228 if (error != 0) { 229 kprintf("Can't allocate root mount for filesystem '%s': %d\n", 230 vfsname, error); 231 goto done; 232 } 233 mp->mnt_flag |= MNT_ROOTFS; 234 235 /* do our best to set rootdev */ 236 if ((devname[0] != 0) && setrootbyname(devname)) 237 kprintf("setrootbyname failed\n"); 238 239 /* If the root device is a type "memory disk", mount RW */ 240 if (rootdev != NOCDEV && dev_is_good(rootdev) && 241 (dev_dflags(rootdev) & D_MEMDISK)) { 242 mp->mnt_flag &= ~MNT_RDONLY; 243 } 244 245 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred); 246 247 done: 248 if (vfsname != NULL) 249 kfree(vfsname, M_MOUNT); 250 if (devname != NULL) 251 kfree(devname, M_MOUNT); 252 if (error != 0) { 253 if (mp != NULL) { 254 vfs_unbusy(mp); 255 kfree(mp, M_MOUNT); 256 } 257 kprintf("Root mount failed: %d\n", error); 258 } else { 259 /* register with list of mounted filesystems */ 260 mountlist_insert(mp, MNTINS_FIRST); 261 262 /* sanity check system clock against root fs timestamp */ 263 inittodr(mp->mnt_time); 264 vfs_unbusy(mp); 265 } 266 return(error); 267 } 268 269 /* 270 * Spin prompting on the console for a suitable root filesystem 271 */ 272 static int 273 vfs_mountroot_ask(void) 274 { 275 char name[128]; 276 int i; 277 cdev_t dev; 278 279 for(;;) { 280 kprintf("\nManual root filesystem specification:\n"); 281 kprintf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 282 kprintf(" eg. ufs:da0s1a\n"); 283 kprintf(" ? List valid disk boot devices\n"); 284 kprintf(" <empty line> Abort manual input\n"); 285 kprintf("\nmountroot> "); 286 gets(name); 287 if (name[0] == 0) 288 return(1); 289 if (name[0] == '?') { 290 kprintf("Possibly valid devices for 'ufs' root:\n"); 291 for (i = 0; i < NUMCDEVSW; i++) { 292 dev = udev2dev(makeudev(i, 0), 0); 293 if (dev_is_good(dev)) 294 kprintf(" \"%s\"", dev_dname(dev)); 295 } 296 kprintf("\n"); 297 continue; 298 } 299 if (!vfs_mountroot_try(name)) 300 return(0); 301 } 302 } 303 304 static void 305 gets(char *cp) 306 { 307 char *lp; 308 int c; 309 310 lp = cp; 311 for (;;) { 312 kprintf("%c", c = cngetc() & 0177); 313 switch (c) { 314 case -1: 315 case '\n': 316 case '\r': 317 *lp++ = '\0'; 318 return; 319 case '\b': 320 case '\177': 321 if (lp > cp) { 322 kprintf(" \b"); 323 lp--; 324 } 325 continue; 326 case '#': 327 lp--; 328 if (lp < cp) 329 lp = cp; 330 continue; 331 case '@': 332 case 'u' & 037: 333 lp = cp; 334 kprintf("%c", '\n'); 335 continue; 336 default: 337 *lp++ = c; 338 } 339 } 340 } 341 342 /* 343 * Convert a given name to the cdev_t of the disk-like device 344 * it refers to. 345 */ 346 cdev_t 347 kgetdiskbyname(const char *name) 348 { 349 char *cp; 350 int nlen; 351 int cd, unit, slice, part; 352 cdev_t dev; 353 cdev_t rdev; 354 355 /* 356 * Get the base name of the device 357 */ 358 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0) 359 name += sizeof(__SYS_PATH_DEV) - 1; 360 cp = __DECONST(char *, name); 361 while (*cp == '/') 362 ++cp; 363 while (*cp >= 'a' && *cp <= 'z') 364 ++cp; 365 if (cp == name) { 366 kprintf("missing device name\n"); 367 return (NOCDEV); 368 } 369 nlen = cp - name; 370 371 /* 372 * Get the unit. 373 */ 374 unit = strtol(cp, &cp, 10); 375 if (name + nlen == (const char *)cp || unit < 0 || unit > DKMAXUNIT) { 376 kprintf("bad unit: %d\n", unit); 377 return (NOCDEV); 378 } 379 380 /* 381 * Get the slice. Note that if no partition or partition 'a' is 382 * specified, and no slice is specified, we will try both 'ad0a' 383 * (which is what you get when slice is 0), and also 'ad0' (the 384 * whole-disk partition, slice == 1). 385 */ 386 if (*cp == 's') { 387 slice = cp[1] - '0'; 388 if (slice < 1 || slice > 9) { 389 kprintf("bad slice number\n"); 390 return(NOCDEV); 391 } 392 ++slice; /* slice #1 starts at 2 */ 393 cp += 2; 394 } else { 395 slice = 0; 396 } 397 398 /* 399 * Get the partition. 400 */ 401 if (*cp >= 'a' && *cp <= 'p') { 402 part = *cp - 'a'; 403 ++cp; 404 } else { 405 part = 0; 406 } 407 408 if (*cp != '\0') { 409 kprintf("junk after name\n"); 410 return (NOCDEV); 411 } 412 413 /* 414 * Locate the device 415 */ 416 for (cd = 0; cd < NUMCDEVSW; cd++) { 417 const char *dname; 418 419 dev = udev2dev(makeudev(cd, dkmakeminor(unit, slice, part)), 0); 420 if (dev_is_good(dev) && (dname = dev_dname(dev)) != NULL) { 421 if (strlen(dname) == nlen && 422 strncmp(dname, name, nlen) == 0) { 423 break; 424 } 425 } 426 } 427 if (cd == NUMCDEVSW) { 428 kprintf("no such device '%*.*s'\n", nlen, nlen, name); 429 return (NOCDEV); 430 } 431 432 /* 433 * FOUND DEVICE 434 */ 435 rdev = make_sub_dev(dev, dkmakeminor(unit, slice, part)); 436 return(rdev); 437 } 438 439 /* 440 * Set rootdev to match (name), given that we expect it to 441 * refer to a disk-like device. 442 */ 443 static int 444 setrootbyname(char *name) 445 { 446 cdev_t diskdev; 447 448 diskdev = kgetdiskbyname(name); 449 if (diskdev != NOCDEV) { 450 rootdev = diskdev; 451 return (0); 452 } 453 454 return (1); 455 } 456 457 #ifdef DDB 458 DB_SHOW_COMMAND(disk, db_getdiskbyname) 459 { 460 cdev_t dev; 461 462 if (modif[0] == '\0') { 463 db_error("usage: show disk/devicename"); 464 return; 465 } 466 dev = kgetdiskbyname(modif); 467 if (dev != NOCDEV) 468 db_printf("cdev_t = %p\n", dev); 469 else 470 db_printf("No disk device matched.\n"); 471 } 472 #endif 473