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