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/disk.h> 61 #include <sys/namecache.h> 62 #include <sys/paths.h> 63 #include <sys/thread2.h> 64 #include <sys/nlookup.h> 65 #include <vfs/devfs/devfs.h> 66 67 #include "opt_ddb.h" 68 #ifdef DDB 69 #include <ddb/ddb.h> 70 #endif 71 72 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 73 74 #define ROOTNAME "root_device" 75 76 struct vnode *rootvnode; 77 struct nchandle rootnch; 78 79 /* 80 * The root specifiers we will try if RB_CDROM is specified. Note that 81 * the ATA driver will accept acd*a and acd*c, but the SCSI driver 82 * will only accept cd*c, so use 'c'. 83 * 84 * XXX TGEN NATA and, presumably, 'old'ATA will also accept the device name 85 * without any fake partition, since the major & minor are identical for all 86 * three (acd*, acd*a and acd*c). However, due to an as-of-yet undiscovered 87 * bug, acd0c ends up with minor 2 when using NATA and booting cold. Since 88 * NATA's acd_open() is unable to fulfill mounts on such 'ghost' cdevs, acd0 89 * and acd1 have been added to the list of CD-ROM root device names. 90 */ 91 static char *cdrom_rootdevnames[] = { 92 "cd9660:cd0c", 93 "cd9660:acd0c", 94 "cd9660:cd1c", 95 "cd9660:acd1c", 96 "cd9660:acd0", 97 "cd9660:acd1", 98 NULL 99 }; 100 101 int vfs_mountroot_devfs(void); 102 static void vfs_mountroot(void *junk); 103 static int vfs_mountroot_try(const char *mountfrom); 104 static int vfs_mountroot_ask(void); 105 static int getline(char *cp, int limit); 106 107 /* legacy find-root code */ 108 char *rootdevnames[2] = {NULL, NULL}; 109 static int setrootbyname(char *name); 110 111 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL); 112 113 /* 114 * Find and mount the root filesystem 115 */ 116 static void 117 vfs_mountroot(void *junk) 118 { 119 int i; 120 cdev_t save_rootdev = rootdev; 121 122 /* 123 * Make sure all disk devices created so far have also been probed, 124 * and also make sure that the newly created device nodes for 125 * probed disks are ready, too. 126 */ 127 disk_config(NULL); 128 devfs_config(NULL); 129 130 /* 131 * The root filesystem information is compiled in, and we are 132 * booted with instructions to use it. 133 */ 134 #ifdef ROOTDEVNAME 135 if ((boothowto & RB_DFLTROOT) && 136 !vfs_mountroot_try(ROOTDEVNAME)) 137 return; 138 #endif 139 /* 140 * We are booted with instructions to prompt for the root filesystem, 141 * or to use the compiled-in default when it doesn't exist. 142 */ 143 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) { 144 if (!vfs_mountroot_ask()) 145 return; 146 } 147 148 /* 149 * We've been given the generic "use CDROM as root" flag. This is 150 * necessary because one media may be used in many different 151 * devices, so we need to search for them. 152 */ 153 if (boothowto & RB_CDROM) { 154 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 155 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 156 return; 157 } 158 } 159 160 /* 161 * Try to use the value read by the loader from /etc/fstab, or 162 * supplied via some other means. This is the preferred 163 * mechanism. 164 */ 165 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom"))) 166 return; 167 168 /* 169 * If a vfs set rootdev, try it (XXX VINUM HACK!) 170 */ 171 if (save_rootdev != NULL) { 172 rootdev = save_rootdev; 173 if (!vfs_mountroot_try("")) 174 return; 175 } 176 177 /* 178 * Try values that may have been computed by the machine-dependant 179 * legacy code. 180 */ 181 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0])) 182 return; 183 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1])) 184 return; 185 186 /* 187 * If we have a compiled-in default, and haven't already tried it, try 188 * it now. 189 */ 190 #ifdef ROOTDEVNAME 191 if (!(boothowto & RB_DFLTROOT)) 192 if (!vfs_mountroot_try(ROOTDEVNAME)) 193 return; 194 #endif 195 196 /* 197 * Everything so far has failed, prompt on the console if we haven't 198 * already tried that. 199 */ 200 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask()) 201 return; 202 panic("Root mount failed, startup aborted."); 203 } 204 205 206 int 207 vfs_mountroot_devfs(void) 208 { 209 struct vnode *vp; 210 struct nchandle nch; 211 struct nlookupdata nd; 212 struct mount *mp; 213 struct vfsconf *vfsp; 214 int error; 215 struct ucred *cred = proc0.p_ucred; 216 217 /* 218 * Lookup the requested path and extract the nch and vnode. 219 */ 220 error = nlookup_init_raw(&nd, 221 "/dev", UIO_SYSSPACE, NLC_FOLLOW, 222 cred, &rootnch); 223 224 if (error == 0) { 225 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup_init is ok...\n"); 226 if ((error = nlookup(&nd)) == 0) { 227 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup is ok...\n"); 228 if (nd.nl_nch.ncp->nc_vp == NULL) { 229 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup: simply not found\n"); 230 error = ENOENT; 231 } 232 } 233 } 234 if (error) { 235 nlookup_done(&nd); 236 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup failed, error: %d\n", error); 237 return (error); 238 } 239 240 /* 241 * Extract the locked+refd ncp and cleanup the nd structure 242 */ 243 nch = nd.nl_nch; 244 cache_zero(&nd.nl_nch); 245 nlookup_done(&nd); 246 247 /* 248 * now we have the locked ref'd nch and unreferenced vnode. 249 */ 250 vp = nch.ncp->nc_vp; 251 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) { 252 cache_put(&nch); 253 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vget failed\n"); 254 return (error); 255 } 256 cache_unlock(&nch); 257 258 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) { 259 cache_drop(&nch); 260 vput(vp); 261 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vinvalbuf failed\n"); 262 return (error); 263 } 264 if (vp->v_type != VDIR) { 265 cache_drop(&nch); 266 vput(vp); 267 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vp is not VDIR\n"); 268 return (ENOTDIR); 269 } 270 271 vfsp = vfsconf_find_by_name("devfs"); 272 vp->v_flag |= VMOUNT; 273 274 /* 275 * Allocate and initialize the filesystem. 276 */ 277 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK); 278 TAILQ_INIT(&mp->mnt_nvnodelist); 279 TAILQ_INIT(&mp->mnt_reservedvnlist); 280 TAILQ_INIT(&mp->mnt_jlist); 281 mp->mnt_nvnodelistsize = 0; 282 lockinit(&mp->mnt_lock, "vfslock", 0, 0); 283 vfs_busy(mp, LK_NOWAIT); 284 mp->mnt_op = vfsp->vfc_vfsops; 285 mp->mnt_vfc = vfsp; 286 vfsp->vfc_refcount++; 287 mp->mnt_stat.f_type = vfsp->vfc_typenum; 288 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 289 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 290 mp->mnt_stat.f_owner = cred->cr_uid; 291 mp->mnt_iosize_max = DFLTPHYS; 292 vn_unlock(vp); 293 294 /* 295 * Mount the filesystem. 296 */ 297 error = VFS_MOUNT(mp, "/dev", NULL, cred); 298 299 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 300 301 /* 302 * Put the new filesystem on the mount list after root. The mount 303 * point gets its own mnt_ncmountpt (unless the VFS already set one 304 * up) which represents the root of the mount. The lookup code 305 * detects the mount point going forward and checks the root of 306 * the mount going backwards. 307 * 308 * It is not necessary to invalidate or purge the vnode underneath 309 * because elements under the mount will be given their own glue 310 * namecache record. 311 */ 312 if (!error) { 313 if (mp->mnt_ncmountpt.ncp == NULL) { 314 /* 315 * allocate, then unlock, but leave the ref intact 316 */ 317 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL); 318 cache_unlock(&mp->mnt_ncmountpt); 319 } 320 mp->mnt_ncmounton = nch; /* inherits ref */ 321 nch.ncp->nc_flag |= NCF_ISMOUNTPT; 322 323 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */ 324 vp->v_flag &= ~VMOUNT; 325 mountlist_insert(mp, MNTINS_LAST); 326 vn_unlock(vp); 327 //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt); 328 error = vfs_allocate_syncvnode(mp); 329 if (error) { 330 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n"); 331 } 332 vfs_unbusy(mp); 333 error = VFS_START(mp, 0); 334 vrele(vp); 335 } else { 336 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); 337 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); 338 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); 339 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); 340 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); 341 vp->v_flag &= ~VMOUNT; 342 mp->mnt_vfc->vfc_refcount--; 343 vfs_unbusy(mp); 344 kfree(mp, M_MOUNT); 345 cache_drop(&nch); 346 vput(vp); 347 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n"); 348 } 349 350 devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error); 351 return (error); 352 } 353 354 355 /* 356 * Mount (mountfrom) as the root filesystem. 357 */ 358 static int 359 vfs_mountroot_try(const char *mountfrom) 360 { 361 struct mount *mp, *mp2; 362 char *vfsname, *devname; 363 int error; 364 char patt[32]; 365 366 vfsname = NULL; 367 devname = NULL; 368 mp = NULL; 369 mp2 = NULL; 370 error = EINVAL; 371 372 if (mountfrom == NULL) 373 return(error); /* don't complain */ 374 375 crit_enter(); 376 kprintf("Mounting root from %s\n", mountfrom); 377 crit_exit(); 378 379 /* parse vfs name and devname */ 380 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 381 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK); 382 vfsname[0] = devname[0] = 0; 383 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 384 if (ksscanf(mountfrom, patt, vfsname, devname) < 1) 385 goto done; 386 387 /* allocate a root mount */ 388 error = vfs_rootmountalloc(vfsname, 389 devname[0] != 0 ? devname : ROOTNAME, &mp); 390 if (error != 0) { 391 kprintf("Can't allocate root mount for filesystem '%s': %d\n", 392 vfsname, error); 393 goto done; 394 } 395 mp->mnt_flag |= MNT_ROOTFS; 396 397 /* do our best to set rootdev */ 398 if ((devname[0] != 0) && setrootbyname(devname)) 399 kprintf("setrootbyname failed\n"); 400 401 /* If the root device is a type "memory disk", mount RW */ 402 if (rootdev != NULL && dev_is_good(rootdev) && 403 (dev_dflags(rootdev) & D_MEMDISK)) { 404 mp->mnt_flag &= ~MNT_RDONLY; 405 } 406 407 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred); 408 409 if (!error) { 410 //kprintf("Trying vfs_mountroot_devfs!\n"); 411 //vfs_mountroot_devfs(); 412 } 413 414 done: 415 if (vfsname != NULL) 416 kfree(vfsname, M_MOUNT); 417 if (devname != NULL) 418 kfree(devname, M_MOUNT); 419 if (error == 0) { 420 /* register with list of mounted filesystems */ 421 mountlist_insert(mp, MNTINS_FIRST); 422 423 /* sanity check system clock against root fs timestamp */ 424 inittodr(mp->mnt_time); 425 vfs_unbusy(mp); 426 if (mp->mnt_syncer == NULL) { 427 error = vfs_allocate_syncvnode(mp); 428 if (error) 429 kprintf("Warning: no syncer vp for root!\n"); 430 error = 0; 431 } 432 } else { 433 if (mp != NULL) { 434 vfs_unbusy(mp); 435 kfree(mp, M_MOUNT); 436 } 437 kprintf("Root mount failed: %d\n", error); 438 } 439 return(error); 440 } 441 442 443 static void vfs_mountroot_ask_callback(cdev_t); 444 445 /* 446 * Spin prompting on the console for a suitable root filesystem 447 */ 448 449 static int 450 vfs_mountroot_ask(void) 451 { 452 char name[128]; 453 int llimit = 100; 454 455 kprintf("\nManual root filesystem specification:\n"); 456 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n"); 457 kprintf(" ? List valid disk boot devices\n"); 458 kprintf(" panic Just panic\n"); 459 kprintf(" abort Abort manual input\n"); 460 while (llimit--) { 461 kprintf("\nmountroot> "); 462 463 if (getline(name, 128) < 0) 464 break; 465 if (name[0] == 0) { 466 ; 467 } else if (name[0] == '?') { 468 kprintf("Possibly valid devices for root FS:\n"); 469 //enumerate all disk devices 470 devfs_scan_callback(vfs_mountroot_ask_callback); 471 kprintf("\n"); 472 continue; 473 } else if (strcmp(name, "panic") == 0) { 474 panic("panic from console"); 475 } else if (strcmp(name, "abort") == 0) { 476 break; 477 } else if (vfs_mountroot_try(name) == 0) { 478 return(0); 479 } 480 } 481 return(1); 482 } 483 484 485 static void 486 vfs_mountroot_ask_callback(cdev_t dev) 487 { 488 if (dev_is_good(dev) && (dev_dflags(dev) & D_DISK)) 489 kprintf(" \"%s\" ", dev->si_name); 490 } 491 492 493 static int 494 getline(char *cp, int limit) 495 { 496 char *lp; 497 int c; 498 499 lp = cp; 500 for (;;) { 501 c = cngetc(); 502 503 switch (c) { 504 case -1: 505 return(-1); 506 case '\n': 507 case '\r': 508 kprintf("\n"); 509 *lp++ = '\0'; 510 return(0); 511 case '\b': 512 case '\177': 513 if (lp > cp) { 514 kprintf("\b \b"); 515 lp--; 516 } else { 517 kprintf("%c", 7); 518 } 519 continue; 520 case '#': 521 kprintf("#"); 522 lp--; 523 if (lp < cp) 524 lp = cp; 525 continue; 526 case '@': 527 case 'u' & 037: 528 lp = cp; 529 kprintf("%c", '\n'); 530 continue; 531 default: 532 if (lp - cp >= limit - 1) { 533 kprintf("%c", 7); 534 } else { 535 kprintf("%c", c); 536 *lp++ = c; 537 } 538 continue; 539 } 540 } 541 } 542 543 /* 544 * Convert a given name to the cdev_t of the disk-like device 545 * it refers to. 546 */ 547 struct kdbn_info { 548 const char *name; 549 int nlen; 550 int minor; 551 cdev_t dev; 552 }; 553 554 555 cdev_t 556 kgetdiskbyname(const char *name) 557 { 558 char *cp; 559 int nlen; 560 int unit, slice, part; 561 cdev_t rdev; 562 563 /* 564 * Get the base name of the device 565 */ 566 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0) 567 name += sizeof(__SYS_PATH_DEV) - 1; 568 cp = __DECONST(char *, name); 569 while (*cp == '/') 570 ++cp; 571 while (*cp >= 'a' && *cp <= 'z') 572 ++cp; 573 if (cp == name) { 574 kprintf("missing device name\n"); 575 return (NULL); 576 } 577 nlen = cp - name; 578 579 /* 580 * Get the unit. 581 */ 582 unit = strtol(cp, &cp, 10); 583 if (name + nlen == (const char *)cp || unit < 0 || unit >= DKMAXUNITS) { 584 kprintf("bad unit: %d\n", unit); 585 return (NULL); 586 } 587 588 /* 589 * Get the slice. Note that if no partition or partition 'a' is 590 * specified, and no slice is specified, we will try both 'ad0a' 591 * (which is what you get when slice is 0), and also 'ad0' (the 592 * whole-disk partition, slice == 1). 593 */ 594 if (*cp == 's') { 595 slice = cp[1] - '0'; 596 if (slice >= 1) 597 ++slice; 598 cp += 2; 599 } else { 600 slice = 0; 601 } 602 603 /* 604 * Get the partition. 605 */ 606 if (*cp >= 'a' && *cp <= 'p') { 607 part = *cp - 'a'; 608 ++cp; 609 } else { 610 part = 0; 611 } 612 613 if (*cp != '\0') { 614 kprintf("junk after name: %s\n", cp); 615 return (NULL); 616 } 617 618 /* 619 * Locate the device 620 */ 621 rdev = devfs_find_device_by_name(name); 622 if (rdev == NULL) { 623 kprintf("no disk named '%s'\n", name); 624 } 625 /* 626 * FOUND DEVICE 627 */ 628 return(rdev); 629 } 630 631 /* 632 * Set rootdev to match (name), given that we expect it to 633 * refer to a disk-like device. 634 */ 635 static int 636 setrootbyname(char *name) 637 { 638 cdev_t diskdev; 639 640 diskdev = kgetdiskbyname(name); 641 if (diskdev != NULL) { 642 rootdev = diskdev; 643 return (0); 644 } 645 /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is 646 * found by fails to mount and the second one isn't found, mountroot_try 647 * doesn't try again with the first one 648 */ 649 rootdev = NULL; 650 return (1); 651 } 652 653 #ifdef DDB 654 DB_SHOW_COMMAND(disk, db_getdiskbyname) 655 { 656 cdev_t dev; 657 658 if (modif[0] == '\0') { 659 db_error("usage: show disk/devicename"); 660 return; 661 } 662 dev = kgetdiskbyname(modif); 663 if (dev != NULL) 664 db_printf("cdev_t = %p\n", dev); 665 else 666 db_printf("No disk device matched.\n"); 667 } 668 #endif 669