2 * Copyright (C) 2014 Nathan Whitehorn
4 * Copyright 2022 Netflix, Inc
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
34 #include "host_syscall.h"
36 #include "bootstrap.h"
37 #ifdef LOADER_ZFS_SUPPORT
39 #include <sys/zfs_bootenv.h>
42 static int hostdisk_init(void);
43 static int hostdisk_strategy(void *devdata, int flag, daddr_t dblk,
44 size_t size, char *buf, size_t *rsize);
45 static int hostdisk_open(struct open_file *f, ...);
46 static int hostdisk_close(struct open_file *f);
47 static int hostdisk_ioctl(struct open_file *f, u_long cmd, void *data);
48 static int hostdisk_print(int verbose);
49 static char *hostdisk_fmtdev(struct devdesc *vdev);
50 static bool hostdisk_match(struct devsw *devsw, const char *devspec);
51 static int hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path);
53 struct devsw hostdisk = {
55 .dv_type = DEVT_HOSTDISK,
56 .dv_init = hostdisk_init,
57 .dv_strategy = hostdisk_strategy,
58 .dv_open = hostdisk_open,
59 .dv_close = hostdisk_close,
60 .dv_ioctl = hostdisk_ioctl,
61 .dv_print = hostdisk_print,
62 .dv_cleanup = nullsys,
63 .dv_fmtdev = hostdisk_fmtdev,
64 .dv_match = hostdisk_match,
65 .dv_parsedev = hostdisk_parsedev,
69 * We need to walk through the /sys/block directories looking for
70 * block devices that we can use.
72 #define SYSBLK "/sys/block"
74 #define HOSTDISK_MIN_SIZE (16ul << 20) /* 16MB */
76 typedef STAILQ_HEAD(, hdinfo) hdinfo_list_t;
77 typedef struct hdinfo {
78 STAILQ_ENTRY(hdinfo) hd_link; /* link in device list */
79 hdinfo_list_t hd_children;
80 struct hdinfo *hd_parent;
82 uint64_t hd_size; /* In bytes */
84 uint64_t hd_sectorsize;
86 #define HDF_HAS_ZPOOL 1 /* We found a zpool here and uuid valid */
90 #define dev2hd(d) ((hdinfo_t *)d->d_opendata)
91 #define hd_name(hd) ((hd->hd_dev + 5))
93 static hdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
95 typedef bool fef_cb_t(struct host_dirent64 *, void *);
96 #define FEF_RECURSIVE 1
99 foreach_file(const char *dir, fef_cb_t cb, void *argp, u_int flags)
103 struct host_dirent64 *dent;
105 fd = host_open(dir, O_RDONLY, 0);
107 printf("Can't open %s\n", dir);/* XXX */
111 dentsize = host_getdents64(fd, dents, sizeof(dents));
114 for (dent = (struct host_dirent64 *)dents;
115 (char *)dent < dents + dentsize;
116 dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) {
126 hostdisk_add_part(hdinfo_t *hd, const char *drv, uint64_t secs)
131 printf("hd %s adding %s %ju\n", hd->hd_dev, drv, (uintmax_t)secs);
132 if ((md = calloc(1, sizeof(*md))) == NULL)
134 if (asprintf(&dev, "/dev/%s", drv) == -1) {
135 printf("hostdisk: no memory\n");
140 md->hd_sectors = secs;
141 md->hd_sectorsize = hd->hd_sectorsize;
142 md->hd_size = md->hd_sectors * md->hd_sectorsize;
144 STAILQ_INSERT_TAIL(&hd->hd_children, md, hd_link);
148 hostdisk_one_part(struct host_dirent64 *dent, void *argp)
154 /* Need to skip /dev/ at start of hd_name */
155 if (strncmp(dent->d_name, hd_name(hd), strlen(hd_name(hd))) != 0)
157 /* Find out how big this is -- no size not a disk */
158 snprintf(szfn, sizeof(szfn), "%s/%s/%s/size", SYSBLK,
159 hd_name(hd), dent->d_name);
160 if (!file2u64(szfn, &sz))
162 hostdisk_add_part(hd, dent->d_name, sz);
167 hostdisk_add_parts(hdinfo_t *hd)
171 snprintf(fn, sizeof(fn), "%s/%s", SYSBLK, hd_name(hd));
172 foreach_file(fn, hostdisk_one_part, hd, 0);
176 hostdisk_add_drive(const char *drv, uint64_t secs)
182 if ((hd = calloc(1, sizeof(*hd))) == NULL)
184 if (asprintf(&dev, "/dev/%s", drv) == -1) {
185 printf("hostdisk: no memory\n");
190 hd->hd_sectors = secs;
191 snprintf(fn, sizeof(fn), "%s/%s/queue/hw_sector_size",
193 if (!file2u64(fn, &hd->hd_sectorsize))
195 hd->hd_size = hd->hd_sectors * hd->hd_sectorsize;
196 if (hd->hd_size < HOSTDISK_MIN_SIZE)
199 STAILQ_INIT(&hd->hd_children);
200 printf("/dev/%s: %ju %ju %ju\n",
201 drv, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
202 STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
203 hostdisk_add_parts(hd);
211 /* Find a disk / partition by its filename */
214 hostdisk_find(const char *fn)
218 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
219 if (strcmp(hd->hd_dev, fn) == 0)
221 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
222 if (strcmp(md->hd_dev, fn) == 0)
231 hostdisk_one_disk(struct host_dirent64 *dent, void *argp __unused)
239 if (strcmp(dent->d_name, ".") == 0 ||
240 strcmp(dent->d_name, "..") == 0)
243 /* Find out how big this is -- no size not a disk */
244 snprintf(szfn, sizeof(szfn), "%s/%s/size", SYSBLK,
246 if (!file2u64(szfn, &sz))
248 hostdisk_add_drive(dent->d_name, sz);
253 hostdisk_fake_one_disk(char *override)
256 struct host_kstat sb;
258 if (host_stat(override, &sb) != 0)
260 if (!HOST_S_ISREG(sb.st_mode))
264 if ((hd = calloc(1, sizeof(*hd))) == NULL)
266 if ((hd->hd_dev = strdup(override)) == NULL)
268 hd->hd_size = sb.st_size;
269 hd->hd_sectorsize = 512; /* XXX configurable? */
270 hd->hd_sectors = hd->hd_size / hd->hd_sectorsize;
271 if (hd->hd_size < HOSTDISK_MIN_SIZE)
274 STAILQ_INIT(&hd->hd_children);
275 printf("%s: %ju %ju %ju\n",
276 hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
277 STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
280 free(__DECONST(void *, hd->hd_dev));
285 hostdisk_find_block_devices(void)
289 override=getenv("hostdisk_override");
290 if (override != NULL)
291 hostdisk_fake_one_disk(override);
293 foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
299 hostdisk_find_block_devices();
305 hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
306 char *buf, size_t *rsize)
308 struct devdesc *desc = devdata;
317 posl = pos & 0xffffffffu;
318 posh = (pos >> 32) & 0xffffffffu;
319 if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) {
320 printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n",
321 desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off);
324 n = host_read(desc->d_unit, buf, size);
334 hostdisk_open(struct open_file *f, ...)
336 struct devdesc *desc;
341 desc = va_arg(vl, struct devdesc *);
344 fn = dev2hd(desc)->hd_dev;
345 desc->d_unit = host_open(fn, O_RDONLY, 0);
346 if (desc->d_unit <= 0) {
347 printf("hostdisk_open: couldn't open %s: %d\n", fn, errno);
355 hostdisk_close(struct open_file *f)
357 struct devdesc *desc = f->f_devdata;
359 host_close(desc->d_unit);
364 hostdisk_ioctl(struct open_file *f, u_long cmd, void *data)
366 struct devdesc *desc = f->f_devdata;
367 hdinfo_t *hd = dev2hd(desc);
370 case DIOCGSECTORSIZE:
371 *(u_int *)data = hd->hd_sectorsize;
374 *(uint64_t *)data = hd->hd_size;
383 hostdisk_print(int verbose)
389 printf("%s devices:", hostdisk.dv_name);
390 if (pager_output("\n") != 0)
393 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
394 snprintf(line, sizeof(line),
395 " %s: %ju X %ju: %ju bytes\n",
397 (uintmax_t)hd->hd_sectors,
398 (uintmax_t)hd->hd_sectorsize,
399 (uintmax_t)hd->hd_size);
400 if ((ret = pager_output(line)) != 0)
402 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
403 snprintf(line, sizeof(line),
404 " %s: %ju X %ju: %ju bytes\n",
406 (uintmax_t)md->hd_sectors,
407 (uintmax_t)md->hd_sectorsize,
408 (uintmax_t)md->hd_size);
409 if ((ret = pager_output(line)) != 0)
419 hostdisk_fmtdev(struct devdesc *vdev)
421 static char name[DEV_DEVLEN];
423 snprintf(name, sizeof(name), "%s:", dev2hd(vdev)->hd_dev);
428 hostdisk_match(struct devsw *devsw, const char *devspec)
434 colon = strchr(devspec, ':');
437 cp = strdup(devspec);
438 cp[colon - devspec] = '\0';
439 hd = hostdisk_find(cp);
445 hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
453 /* Must have a : in it */
454 cp = strchr(devspec, ':');
457 /* XXX Stat the /dev or defer error handling to open(2) call? */
461 fn = strdup(devspec);
463 hd = hostdisk_find(fn);
465 printf("Can't find hdinfo for %s\n", fn);
470 dev = malloc(sizeof(*dev));
474 dev->d_dev = &hostdisk;
475 dev->d_opendata = hd;
482 sanity_check_currdev(void)
486 return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 ||
487 #ifdef PATH_BOOTABLE_TOKEN
488 stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */
490 stat(PATH_KERNEL, &st) == 0);
494 hostdisk_try_one(hdinfo_t *hd)
498 if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
501 printf("Trying %s\n", fn);
502 if (sanity_check_currdev())
504 printf("Failed %s\n", fn);
510 hostdisk_gen_probe(void)
513 const char *rv = NULL;
515 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
517 if (hd->hd_flags & HDF_HAS_ZPOOL)
519 rv = hostdisk_try_one(hd);
523 /* try all partitions */
524 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
525 if (md->hd_flags & HDF_HAS_ZPOOL)
527 rv = hostdisk_try_one(md);
535 #ifdef LOADER_ZFS_SUPPORT
537 hostdisk_zfs_check_one(hdinfo_t *hd)
543 if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
546 zfs_probe_dev(fn, &pool_uuid, false);
547 if (pool_uuid != 0) {
549 hd->hd_flags |= HDF_HAS_ZPOOL;
550 hd->hd_zfs_uuid = pool_uuid;
558 hostdisk_zfs_probe(void)
562 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
563 if (hostdisk_zfs_check_one(hd))
565 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
566 hostdisk_zfs_check_one(md);
571 /* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */
573 probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev)
576 struct zfs_devdesc currdev;
579 currdev.dd.d_dev = &zfs_dev;
580 currdev.dd.d_unit = 0;
581 currdev.pool_guid = pool_guid;
582 currdev.root_guid = root_guid;
583 devname = devformat(&currdev.dd);
585 set_currdev(devname);
587 bootable = sanity_check_currdev();
589 char buf[VDEV_PAD_SIZE];
591 if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) {
592 printf("zfs bootonce: %s\n", buf);
595 setenv("zfs-bootonce", buf, 1);
597 (void)zfs_attach_nvstore(&currdev);
598 init_zfs_boot_options(devname);
604 hostdisk_zfs_try_default(hdinfo_t *hd)
606 return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true));
610 hostdisk_zfs_find_default(void)
614 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
615 if (hd->hd_flags & HDF_HAS_ZPOOL) {
616 if (hostdisk_zfs_try_default(hd))
620 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
621 if (md->hd_flags & HDF_HAS_ZPOOL) {
622 if (hostdisk_zfs_try_default(md))