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