1 /*- 2 * Copyright (C) 2014 Nathan Whitehorn 3 * All rights reserved. 4 * Copyright 2022 Netflix, Inc 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/disk.h> 29 #include <stdarg.h> 30 #include <paths.h> 31 #include "host_syscall.h" 32 #include "kboot.h" 33 #include "bootstrap.h" 34 #ifdef LOADER_ZFS_SUPPORT 35 #include "libzfs.h" 36 #include <sys/zfs_bootenv.h> 37 #endif 38 39 static int hostdisk_init(void); 40 static int hostdisk_strategy(void *devdata, int flag, daddr_t dblk, 41 size_t size, char *buf, size_t *rsize); 42 static int hostdisk_open(struct open_file *f, ...); 43 static int hostdisk_close(struct open_file *f); 44 static int hostdisk_ioctl(struct open_file *f, u_long cmd, void *data); 45 static int hostdisk_print(int verbose); 46 static char *hostdisk_fmtdev(struct devdesc *vdev); 47 static bool hostdisk_match(struct devsw *devsw, const char *devspec); 48 static int hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path); 49 50 struct devsw hostdisk = { 51 .dv_name = "/dev", 52 .dv_type = DEVT_HOSTDISK, 53 .dv_init = hostdisk_init, 54 .dv_strategy = hostdisk_strategy, 55 .dv_open = hostdisk_open, 56 .dv_close = hostdisk_close, 57 .dv_ioctl = hostdisk_ioctl, 58 .dv_print = hostdisk_print, 59 .dv_cleanup = nullsys, 60 .dv_fmtdev = hostdisk_fmtdev, 61 .dv_match = hostdisk_match, 62 .dv_parsedev = hostdisk_parsedev, 63 }; 64 65 /* 66 * We need to walk through the /sys/block directories looking for 67 * block devices that we can use. 68 */ 69 #define SYSBLK "/sys/block" 70 71 #define HOSTDISK_MIN_SIZE (16ul << 20) /* 16MB */ 72 73 typedef STAILQ_HEAD(, hdinfo) hdinfo_list_t; 74 typedef struct hdinfo { 75 STAILQ_ENTRY(hdinfo) hd_link; /* link in device list */ 76 hdinfo_list_t hd_children; 77 struct hdinfo *hd_parent; 78 const char *hd_dev; 79 uint64_t hd_size; /* In bytes */ 80 uint64_t hd_sectors; 81 uint64_t hd_sectorsize; 82 int hd_flags; 83 #define HDF_HAS_ZPOOL 1 /* We found a zpool here and uuid valid */ 84 uint64_t hd_zfs_uuid; 85 } hdinfo_t; 86 87 #define dev2hd(d) ((hdinfo_t *)d->d_opendata) 88 #define hd_name(hd) ((hd->hd_dev + 5)) 89 90 static hdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo); 91 92 typedef bool fef_cb_t(struct host_dirent64 *, void *); 93 #define FEF_RECURSIVE 1 94 95 static bool 96 foreach_file(const char *dir, fef_cb_t cb, void *argp, u_int flags) 97 { 98 char dents[2048]; 99 int fd, dentsize; 100 struct host_dirent64 *dent; 101 102 fd = host_open(dir, O_RDONLY, 0); 103 if (fd < 0) { 104 printf("Can't open %s\n", dir);/* XXX */ 105 return (false); 106 } 107 while (1) { 108 dentsize = host_getdents64(fd, dents, sizeof(dents)); 109 if (dentsize <= 0) 110 break; 111 for (dent = (struct host_dirent64 *)dents; 112 (char *)dent < dents + dentsize; 113 dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) { 114 if (!cb(dent, argp)) 115 break; 116 } 117 } 118 host_close(fd); 119 return (true); 120 } 121 122 static void 123 hostdisk_add_part(hdinfo_t *hd, const char *drv, uint64_t secs) 124 { 125 hdinfo_t *md; 126 char *dev; 127 128 printf("hd %s adding %s %ju\n", hd->hd_dev, drv, (uintmax_t)secs); 129 if ((md = calloc(1, sizeof(*md))) == NULL) 130 return; 131 if (asprintf(&dev, "/dev/%s", drv) == -1) { 132 printf("hostdisk: no memory\n"); 133 free(md); 134 return; 135 } 136 md->hd_dev = dev; 137 md->hd_sectors = secs; 138 md->hd_sectorsize = hd->hd_sectorsize; 139 md->hd_size = md->hd_sectors * md->hd_sectorsize; 140 md->hd_parent = hd; 141 STAILQ_INSERT_TAIL(&hd->hd_children, md, hd_link); 142 } 143 144 static bool 145 hostdisk_one_part(struct host_dirent64 *dent, void *argp) 146 { 147 hdinfo_t *hd = argp; 148 char szfn[1024]; 149 uint64_t sz; 150 151 /* Need to skip /dev/ at start of hd_name */ 152 if (strncmp(dent->d_name, hd_name(hd), strlen(hd_name(hd))) != 0) 153 return (true); 154 /* Find out how big this is -- no size not a disk */ 155 snprintf(szfn, sizeof(szfn), "%s/%s/%s/size", SYSBLK, 156 hd_name(hd), dent->d_name); 157 if (!file2u64(szfn, &sz)) 158 return true; 159 hostdisk_add_part(hd, dent->d_name, sz); 160 return true; 161 } 162 163 static void 164 hostdisk_add_parts(hdinfo_t *hd) 165 { 166 char fn[1024]; 167 168 snprintf(fn, sizeof(fn), "%s/%s", SYSBLK, hd_name(hd)); 169 foreach_file(fn, hostdisk_one_part, hd, 0); 170 } 171 172 static void 173 hostdisk_add_drive(const char *drv, uint64_t secs) 174 { 175 hdinfo_t *hd = NULL; 176 char *dev = NULL; 177 char fn[1024]; 178 179 if ((hd = calloc(1, sizeof(*hd))) == NULL) 180 return; 181 if (asprintf(&dev, "/dev/%s", drv) == -1) { 182 printf("hostdisk: no memory\n"); 183 free(hd); 184 return; 185 } 186 hd->hd_dev = dev; 187 hd->hd_sectors = secs; 188 snprintf(fn, sizeof(fn), "%s/%s/queue/hw_sector_size", 189 SYSBLK, drv); 190 if (!file2u64(fn, &hd->hd_sectorsize)) 191 goto err; 192 hd->hd_size = hd->hd_sectors * hd->hd_sectorsize; 193 if (hd->hd_size < HOSTDISK_MIN_SIZE) 194 goto err; 195 hd->hd_flags = 0; 196 STAILQ_INIT(&hd->hd_children); 197 printf("/dev/%s: %ju %ju %ju\n", 198 drv, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize); 199 STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link); 200 hostdisk_add_parts(hd); 201 return; 202 err: 203 free(dev); 204 free(hd); 205 return; 206 } 207 208 /* Find a disk / partition by its filename */ 209 210 static hdinfo_t * 211 hostdisk_find(const char *fn) 212 { 213 hdinfo_t *hd, *md; 214 215 STAILQ_FOREACH(hd, &hdinfo, hd_link) { 216 if (strcmp(hd->hd_dev, fn) == 0) 217 return (hd); 218 STAILQ_FOREACH(md, &hd->hd_children, hd_link) { 219 if (strcmp(md->hd_dev, fn) == 0) 220 return (md); 221 } 222 } 223 return (NULL); 224 } 225 226 227 static bool 228 hostdisk_one_disk(struct host_dirent64 *dent, void *argp __unused) 229 { 230 char szfn[1024]; 231 uint64_t sz; 232 233 /* 234 * Skip . and .. 235 */ 236 if (strcmp(dent->d_name, ".") == 0 || 237 strcmp(dent->d_name, "..") == 0) 238 return (true); 239 240 /* Find out how big this is -- no size not a disk */ 241 snprintf(szfn, sizeof(szfn), "%s/%s/size", SYSBLK, 242 dent->d_name); 243 if (!file2u64(szfn, &sz)) 244 return (true); 245 hostdisk_add_drive(dent->d_name, sz); 246 return (true); 247 } 248 249 static void 250 hostdisk_fake_one_disk(char *override) 251 { 252 hdinfo_t *hd = NULL; 253 struct host_kstat sb; 254 255 if (host_stat(override, &sb) != 0) 256 return; 257 if (!HOST_S_ISREG(sb.st_mode)) 258 return; 259 if (sb.st_size == 0) 260 return; 261 if ((hd = calloc(1, sizeof(*hd))) == NULL) 262 return; 263 if ((hd->hd_dev = strdup(override)) == NULL) 264 goto err; 265 hd->hd_size = sb.st_size; 266 hd->hd_sectorsize = 512; /* XXX configurable? */ 267 hd->hd_sectors = hd->hd_size / hd->hd_sectorsize; 268 if (hd->hd_size < HOSTDISK_MIN_SIZE) 269 goto err; 270 hd->hd_flags = 0; 271 STAILQ_INIT(&hd->hd_children); 272 printf("%s: %ju %ju %ju\n", 273 hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize); 274 STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link); 275 return; 276 err: 277 free(__DECONST(void *, hd->hd_dev)); 278 free(hd); 279 } 280 281 static void 282 hostdisk_find_block_devices(void) 283 { 284 char *override; 285 286 override=getenv("hostdisk_override"); 287 if (override != NULL) 288 hostdisk_fake_one_disk(override); 289 else 290 foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0); 291 } 292 293 static int 294 hostdisk_init(void) 295 { 296 hostdisk_find_block_devices(); 297 298 return (0); 299 } 300 301 static int 302 hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, 303 char *buf, size_t *rsize) 304 { 305 struct devdesc *desc = devdata; 306 daddr_t pos; 307 int n; 308 int64_t off; 309 uint64_t res; 310 uint32_t posl, posh; 311 312 pos = dblk * 512; 313 314 posl = pos & 0xffffffffu; 315 posh = (pos >> 32) & 0xffffffffu; 316 if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) { 317 printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n", 318 desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off); 319 return (EIO); 320 } 321 n = host_read(desc->d_unit, buf, size); 322 323 if (n < 0) 324 return (EIO); 325 326 *rsize = n; 327 return (0); 328 } 329 330 static int 331 hostdisk_open(struct open_file *f, ...) 332 { 333 struct devdesc *desc; 334 const char *fn; 335 va_list vl; 336 337 va_start(vl, f); 338 desc = va_arg(vl, struct devdesc *); 339 va_end(vl); 340 341 fn = dev2hd(desc)->hd_dev; 342 desc->d_unit = host_open(fn, O_RDONLY, 0); 343 if (desc->d_unit <= 0) { 344 printf("hostdisk_open: couldn't open %s: %d\n", fn, errno); 345 return (ENOENT); 346 } 347 348 return (0); 349 } 350 351 static int 352 hostdisk_close(struct open_file *f) 353 { 354 struct devdesc *desc = f->f_devdata; 355 356 host_close(desc->d_unit); 357 return (0); 358 } 359 360 static int 361 hostdisk_ioctl(struct open_file *f, u_long cmd, void *data) 362 { 363 struct devdesc *desc = f->f_devdata; 364 hdinfo_t *hd = dev2hd(desc); 365 366 switch (cmd) { 367 case DIOCGSECTORSIZE: 368 *(u_int *)data = hd->hd_sectorsize; 369 break; 370 case DIOCGMEDIASIZE: 371 *(uint64_t *)data = hd->hd_size; 372 break; 373 default: 374 return (ENOTTY); 375 } 376 return (0); 377 } 378 379 static int 380 hostdisk_print(int verbose) 381 { 382 char line[80]; 383 hdinfo_t *hd, *md; 384 int ret = 0; 385 386 printf("%s devices:", hostdisk.dv_name); 387 if (pager_output("\n") != 0) 388 return (1); 389 390 STAILQ_FOREACH(hd, &hdinfo, hd_link) { 391 snprintf(line, sizeof(line), 392 " %s: %ju X %ju: %ju bytes\n", 393 hd->hd_dev, 394 (uintmax_t)hd->hd_sectors, 395 (uintmax_t)hd->hd_sectorsize, 396 (uintmax_t)hd->hd_size); 397 if ((ret = pager_output(line)) != 0) 398 break; 399 STAILQ_FOREACH(md, &hd->hd_children, hd_link) { 400 snprintf(line, sizeof(line), 401 " %s: %ju X %ju: %ju bytes\n", 402 md->hd_dev, 403 (uintmax_t)md->hd_sectors, 404 (uintmax_t)md->hd_sectorsize, 405 (uintmax_t)md->hd_size); 406 if ((ret = pager_output(line)) != 0) 407 goto done; 408 } 409 } 410 411 done: 412 return (ret); 413 } 414 415 static char * 416 hostdisk_fmtdev(struct devdesc *vdev) 417 { 418 static char name[DEV_DEVLEN]; 419 420 snprintf(name, sizeof(name), "%s:", dev2hd(vdev)->hd_dev); 421 return (name); 422 } 423 424 static bool 425 hostdisk_match(struct devsw *devsw, const char *devspec) 426 { 427 hdinfo_t *hd; 428 const char *colon; 429 char *cp; 430 431 colon = strchr(devspec, ':'); 432 if (colon == NULL) 433 return false; 434 cp = strdup(devspec); 435 cp[colon - devspec] = '\0'; 436 hd = hostdisk_find(cp); 437 free(cp); 438 return (hd != NULL); 439 } 440 441 static int 442 hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path) 443 { 444 const char *cp; 445 struct devdesc *dev; 446 hdinfo_t *hd; 447 int len; 448 char *fn; 449 450 /* Must have a : in it */ 451 cp = strchr(devspec, ':'); 452 if (cp == NULL) 453 return (EINVAL); 454 /* XXX Stat the /dev or defer error handling to open(2) call? */ 455 if (path != NULL) 456 *path = cp + 1; 457 len = cp - devspec; 458 fn = strdup(devspec); 459 fn[len] = '\0'; 460 hd = hostdisk_find(fn); 461 if (hd == NULL) { 462 printf("Can't find hdinfo for %s\n", fn); 463 free(fn); 464 return (EINVAL); 465 } 466 free(fn); 467 dev = malloc(sizeof(*dev)); 468 if (dev == NULL) 469 return (ENOMEM); 470 dev->d_unit = 0; 471 dev->d_dev = &hostdisk; 472 dev->d_opendata = hd; 473 *idev = dev; 474 return (0); 475 } 476 477 /* XXX refactor */ 478 static bool 479 sanity_check_currdev(void) 480 { 481 struct stat st; 482 483 return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 || 484 #ifdef PATH_BOOTABLE_TOKEN 485 stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */ 486 #endif 487 stat(PATH_KERNEL, &st) == 0); 488 } 489 490 static const char * 491 hostdisk_try_one(hdinfo_t *hd) 492 { 493 char *fn; 494 495 if (asprintf(&fn, "%s:", hd->hd_dev) == -1) 496 return (NULL); 497 set_currdev(fn); 498 printf("Trying %s\n", fn); 499 if (sanity_check_currdev()) 500 return (fn); 501 printf("Failed %s\n", fn); 502 free(fn); 503 return (NULL); 504 } 505 506 const char * 507 hostdisk_gen_probe(void) 508 { 509 hdinfo_t *hd, *md; 510 const char *rv = NULL; 511 512 STAILQ_FOREACH(hd, &hdinfo, hd_link) { 513 /* try whole disk */ 514 if (hd->hd_flags & HDF_HAS_ZPOOL) 515 continue; 516 rv = hostdisk_try_one(hd); 517 if (rv != NULL) 518 return (rv); 519 520 /* try all partitions */ 521 STAILQ_FOREACH(md, &hd->hd_children, hd_link) { 522 if (md->hd_flags & HDF_HAS_ZPOOL) 523 continue; 524 rv = hostdisk_try_one(md); 525 if (rv != NULL) 526 return (rv); 527 } 528 } 529 return (NULL); 530 } 531 532 #ifdef LOADER_ZFS_SUPPORT 533 static bool 534 hostdisk_zfs_check_one(hdinfo_t *hd) 535 { 536 char *fn; 537 bool found = false; 538 uint64_t pool_uuid; 539 540 if (asprintf(&fn, "%s:", hd->hd_dev) == -1) 541 return (false); 542 pool_uuid = 0; 543 zfs_probe_dev(fn, &pool_uuid, false); 544 if (pool_uuid != 0) { 545 found = true; 546 hd->hd_flags |= HDF_HAS_ZPOOL; 547 hd->hd_zfs_uuid = pool_uuid; 548 } 549 free(fn); 550 551 return (found); 552 } 553 554 void 555 hostdisk_zfs_probe(void) 556 { 557 hdinfo_t *hd, *md; 558 559 STAILQ_FOREACH(hd, &hdinfo, hd_link) { 560 if (hostdisk_zfs_check_one(hd)) 561 continue; 562 STAILQ_FOREACH(md, &hd->hd_children, hd_link) { 563 hostdisk_zfs_check_one(md); 564 } 565 } 566 } 567 568 /* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */ 569 static bool 570 probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev) 571 { 572 char *devname; 573 struct zfs_devdesc currdev; 574 bool bootable; 575 576 currdev.dd.d_dev = &zfs_dev; 577 currdev.dd.d_unit = 0; 578 currdev.pool_guid = pool_guid; 579 currdev.root_guid = root_guid; 580 devname = devformat(&currdev.dd); 581 if (setcurrdev) 582 set_currdev(devname); 583 584 bootable = sanity_check_currdev(); 585 if (bootable) { 586 char buf[VDEV_PAD_SIZE]; 587 588 if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) { 589 printf("zfs bootonce: %s\n", buf); 590 if (setcurrdev) 591 set_currdev(buf); 592 setenv("zfs-bootonce", buf, 1); 593 } 594 (void)zfs_attach_nvstore(&currdev); 595 init_zfs_boot_options(devname); 596 } 597 return (bootable); 598 } 599 600 static bool 601 hostdisk_zfs_try_default(hdinfo_t *hd) 602 { 603 return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true)); 604 } 605 606 bool 607 hostdisk_zfs_find_default(void) 608 { 609 hdinfo_t *hd, *md; 610 611 STAILQ_FOREACH(hd, &hdinfo, hd_link) { 612 if (hd->hd_flags & HDF_HAS_ZPOOL) { 613 if (hostdisk_zfs_try_default(hd)) 614 return (true); 615 continue; 616 } 617 STAILQ_FOREACH(md, &hd->hd_children, hd_link) { 618 if (md->hd_flags & HDF_HAS_ZPOOL) { 619 if (hostdisk_zfs_try_default(md)) 620 return (true); 621 } 622 } 623 } 624 return (false); 625 } 626 #endif 627