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