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(struct host_dirent64 *dent, void *argp)
255 char *override_dir = argp;
258 struct host_kstat sb;
261 * We only do regular files. Each one is treated as a disk image
262 * accessible via /dev/${dent->d_name}.
264 if (dent->d_type != HOST_DT_REG && dent->d_type != HOST_DT_LNK)
266 if (asprintf(&fn, "%s/%s", override_dir, dent->d_name) == -1)
268 if (host_stat(fn, &sb) != 0)
270 if (!HOST_S_ISREG(sb.st_mode))
274 if ((hd = calloc(1, sizeof(*hd))) == NULL)
277 hd->hd_size = sb.st_size;
278 hd->hd_sectorsize = 512; /* XXX configurable? */
279 hd->hd_sectors = hd->hd_size / hd->hd_sectorsize;
280 if (hd->hd_size < HOSTDISK_MIN_SIZE)
283 STAILQ_INIT(&hd->hd_children);
284 printf("%s: %ju %ju %ju\n",
285 hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
286 STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
287 /* XXX no partiions? -- is that OK? */
296 hostdisk_find_block_devices(void)
300 override=getenv("hostdisk_override");
301 if (override != NULL)
302 foreach_file(override, hostdisk_fake_one_disk, override, 0);
304 foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
310 hostdisk_find_block_devices();
316 hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
317 char *buf, size_t *rsize)
319 struct devdesc *desc = devdata;
328 posl = pos & 0xffffffffu;
329 posh = (pos >> 32) & 0xffffffffu;
330 if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) {
331 printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n",
332 desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off);
335 n = host_read(desc->d_unit, buf, size);
345 hostdisk_open(struct open_file *f, ...)
347 struct devdesc *desc;
352 desc = va_arg(vl, struct devdesc *);
355 fn = dev2hd(desc)->hd_dev;
356 desc->d_unit = host_open(fn, O_RDONLY, 0);
357 if (desc->d_unit <= 0) {
358 printf("hostdisk_open: couldn't open %s: %d\n", fn, errno);
366 hostdisk_close(struct open_file *f)
368 struct devdesc *desc = f->f_devdata;
370 host_close(desc->d_unit);
375 hostdisk_ioctl(struct open_file *f, u_long cmd, void *data)
377 struct devdesc *desc = f->f_devdata;
378 hdinfo_t *hd = dev2hd(desc);
381 case DIOCGSECTORSIZE:
382 *(u_int *)data = hd->hd_sectorsize;
385 *(uint64_t *)data = hd->hd_size;
394 hostdisk_print(int verbose)
400 printf("%s devices:", hostdisk.dv_name);
401 if (pager_output("\n") != 0)
404 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
405 snprintf(line, sizeof(line),
406 " %s: %ju X %ju: %ju bytes\n",
408 (uintmax_t)hd->hd_sectors,
409 (uintmax_t)hd->hd_sectorsize,
410 (uintmax_t)hd->hd_size);
411 if ((ret = pager_output(line)) != 0)
413 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
414 snprintf(line, sizeof(line),
415 " %s: %ju X %ju: %ju bytes\n",
417 (uintmax_t)md->hd_sectors,
418 (uintmax_t)md->hd_sectorsize,
419 (uintmax_t)md->hd_size);
420 if ((ret = pager_output(line)) != 0)
430 hostdisk_fmtdev(struct devdesc *vdev)
433 return ((char *)hd_name(dev2hd(vdev)));
437 hostdisk_match(struct devsw *devsw, const char *devspec)
443 colon = strchr(devspec, ':');
446 cp = strdup(devspec);
447 cp[colon - devspec] = '\0';
448 hd = hostdisk_find(cp);
454 hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
462 /* Must have a : in it */
463 cp = strchr(devspec, ':');
466 /* XXX Stat the /dev or defer error handling to open(2) call? */
470 fn = strdup(devspec);
472 hd = hostdisk_find(fn);
474 printf("Can't find hdinfo for %s\n", fn);
479 dev = malloc(sizeof(*dev));
483 dev->d_dev = &hostdisk;
484 dev->d_opendata = hd;
489 #ifdef LOADER_ZFS_SUPPORT
491 hostdisk_zfs_check_one(hdinfo_t *hd)
497 if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
500 zfs_probe_dev(fn, &pool_uuid, false);
501 if (pool_uuid != 0) {
503 hd->hd_flags |= HDF_HAS_ZPOOL;
504 hd->hd_zfs_uuid = pool_uuid;
512 hostdisk_zfs_probe(void)
516 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
517 if (hostdisk_zfs_check_one(hd))
519 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
520 hostdisk_zfs_check_one(md);
527 sanity_check_currdev(void)
531 return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 ||
532 #ifdef PATH_BOOTABLE_TOKEN
533 stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */
535 stat(PATH_KERNEL, &st) == 0);
538 /* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */
540 probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev)
543 struct zfs_devdesc currdev;
547 currdev.dd.d_dev = &zfs_dev;
548 currdev.dd.d_unit = 0;
549 currdev.pool_guid = pool_guid;
550 currdev.root_guid = root_guid;
551 devname = devformat(&currdev.dd);
553 set_currdev(devname);
555 bootable = sanity_check_currdev();
557 buf = malloc(VDEV_PAD_SIZE);
559 if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf,
560 VDEV_PAD_SIZE) == 0) {
561 printf("zfs bootonce: %s\n", buf);
564 setenv("zfs-bootonce", buf, 1);
567 (void)zfs_attach_nvstore(&currdev);
569 init_zfs_boot_options(devname);
575 hostdisk_zfs_try_default(hdinfo_t *hd)
577 return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true));
581 hostdisk_zfs_find_default(void)
585 STAILQ_FOREACH(hd, &hdinfo, hd_link) {
586 if (hd->hd_flags & HDF_HAS_ZPOOL) {
587 if (hostdisk_zfs_try_default(hd))
591 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
592 if (md->hd_flags & HDF_HAS_ZPOOL) {
593 if (hostdisk_zfs_try_default(md))