git: 0e78599a5b58 - stable/13 - kboot: Enhance hostdisk

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:13:36 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=0e78599a5b58684eae89f7bfb24da599c2ad8ec1

commit 0e78599a5b58684eae89f7bfb24da599c2ad8ec1
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-12-02 18:28:08 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:41 +0000

    kboot: Enhance hostdisk
    
    Added missing functionality to allow us to boot off of things like
    /dev/nvme0n1p2 successfully. And to list all available devices and
    partitions with 'lsdev'.
    
    Sponsored by:           Netflix
    
    (cherry picked from commit 7685e8d97a22ae4b58243d6ef67569ea5bcbc988)
---
 stand/kboot/hostdisk.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 185 insertions(+), 7 deletions(-)

diff --git a/stand/kboot/hostdisk.c b/stand/kboot/hostdisk.c
index 852785497989..d83161f5bfee 100644
--- a/stand/kboot/hostdisk.c
+++ b/stand/kboot/hostdisk.c
@@ -53,9 +53,163 @@ struct devsw hostdisk = {
 	.dv_fmtdev = hostdisk_fmtdev,
 };
 
+/*
+ * We need to walk through the /sys/block directories looking for
+ * block devices that we can use.
+ */
+#define SYSBLK "/sys/block"
+
+typedef STAILQ_HEAD(, hdinfo) hdinfo_list_t;
+typedef struct hdinfo {
+	STAILQ_ENTRY(hdinfo)	hd_link;	/* link in device list */
+	hdinfo_list_t	hd_children;
+	struct hdinfo	*hd_parent;
+	const char	*hd_name;
+	uint64_t	hd_size;
+	uint64_t	hd_sectors;
+	uint64_t	hd_sectorsize;
+	int		hd_flags;
+} hdinfo_t;
+
+static hdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
+
+typedef bool fef_cb_t(struct host_dirent64 *, void *);
+#define FEF_RECURSIVE 1
+
+static bool
+foreach_file(const char *dir, fef_cb_t cb, void *argp, u_int flags)
+{
+	char dents[2048];
+	int fd, dentsize;
+	struct host_dirent64 *dent;
+
+	fd = host_open(dir, O_RDONLY, 0);
+	if (fd < 0) {
+		printf("Can't open %s\n", dir);/* XXX */
+		return (false);
+	}
+	while (1) {
+		dentsize = host_getdents64(fd, dents, sizeof(dents));
+		if (dentsize <= 0)
+			break;
+		for (dent = (struct host_dirent64 *)dents;
+		     (char *)dent < dents + dentsize;
+		     dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) {
+			if (!cb(dent, argp))
+				break;
+		}
+	}
+	host_close(fd);
+	return (true);
+}
+
+static void
+hostdisk_add_part(struct hdinfo *hd, const char *drv, uint64_t secs)
+{
+	struct hdinfo *md;
+
+	printf("hd %s adding %s %ju\n", hd->hd_name, drv, (uintmax_t)secs);
+	if ((md = calloc(1, sizeof(*md))) == NULL)
+		return;
+	md->hd_name = strdup(drv);
+	md->hd_sectors = secs;
+	md->hd_sectorsize = hd->hd_sectorsize;
+	md->hd_size = md->hd_sectors * md->hd_sectorsize;
+	md->hd_parent = hd;
+	STAILQ_INSERT_TAIL(&hd->hd_children, md, hd_link);
+}
+
+static bool
+hostdisk_one_part(struct host_dirent64 *dent, void *argp)
+{
+	struct hdinfo *hd = argp;
+	char szfn[1024];
+	uint64_t sz;
+
+	if (strncmp(dent->d_name, hd->hd_name, strlen(hd->hd_name)) != 0)
+		return (true);
+	/* Find out how big this is -- no size not a disk */
+	snprintf(szfn, sizeof(szfn), "%s/%s/%s/size", SYSBLK,
+	    hd->hd_name, dent->d_name);
+	if (!file2u64(szfn, &sz))
+		return true;
+	hostdisk_add_part(hd, dent->d_name, sz);
+	return true;
+}
+
+static void
+hostdisk_add_parts(struct hdinfo *hd)
+{
+	char fn[1024];
+
+	snprintf(fn, sizeof(fn), "%s/%s", SYSBLK, hd->hd_name);
+	foreach_file(fn, hostdisk_one_part, hd, 0);
+}
+
+static void
+hostdisk_add_drive(const char *drv, uint64_t secs)
+{
+	struct hdinfo *hd;
+	char fn[1024];
+
+	if ((hd = calloc(1, sizeof(*hd))) == NULL)
+		return;
+	hd->hd_name = strdup(drv);
+	hd->hd_sectors = secs;
+	snprintf(fn, sizeof(fn), "%s/%s/queue/hw_sector_size",
+	    SYSBLK, drv);
+	if (!file2u64(fn, &hd->hd_sectorsize))
+		goto err;
+	hd->hd_size = hd->hd_sectors * hd->hd_sectorsize;
+	hd->hd_flags = 0;
+	STAILQ_INIT(&hd->hd_children);
+	printf("/dev/%s: %ju %ju %ju\n",
+	    drv, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
+	STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
+	hostdisk_add_parts(hd);
+	return;
+err:
+	free(hd);
+	return;
+}
+
+static bool
+hostdisk_one_disk(struct host_dirent64 *dent, void *argp __unused)
+{
+	char szfn[1024];
+	uint64_t sz;
+
+	/*
+	 * Skip . and ..
+	 */
+	if (strcmp(dent->d_name, ".") == 0 ||
+	    strcmp(dent->d_name, "..") == 0)
+		return (true);
+
+	/* Find out how big this is -- no size not a disk */
+	snprintf(szfn, sizeof(szfn), "%s/%s/size", SYSBLK,
+	    dent->d_name);
+	if (!file2u64(szfn, &sz))
+		return (true);
+	hostdisk_add_drive(dent->d_name, sz);
+	return (true);
+}
+
+static void
+hostdisk_find_block_devices(void)
+{
+	/*
+	 * Start here XXX open SYSBLK, walk through all directories, keep the
+	 * ones that return a size and a 'block' device when we 'stat' it. Try
+	 * to avoid partitions and only do raw devices.
+	 */
+	foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
+}
+
 static int
 hostdisk_init(void)
 {
+	hostdisk_find_block_devices();
 
 	return (0);
 }
@@ -67,15 +221,17 @@ hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
 	struct devdesc *desc = devdata;
 	daddr_t pos;
 	int n;
+	int64_t off;
 	uint64_t res;
 	uint32_t posl, posh;
 
 	pos = dblk * 512;
 
-	posl = pos & 0xffffffff;
-	posh = (pos >> 32) & 0xffffffff;
-	if (host_llseek(desc->d_unit, posh, posl, &res, 0) < 0) {
-		printf("Seek error\n");
+	posl = pos & 0xffffffffu;
+	posh = (pos >> 32) & 0xffffffffu;
+	if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) {
+		printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n",
+		    desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off);
 		return (EIO);
 	}
 	n = host_read(desc->d_unit, buf, size);
@@ -98,7 +254,6 @@ hostdisk_open(struct open_file *f, ...)
 	va_end(vl);
 
 	desc->d_unit = host_open(desc->d_opendata, O_RDONLY, 0);
-
 	if (desc->d_unit <= 0) {
 		printf("hostdisk_open: couldn't open %s: %d\n",
 		    (char *)desc->d_opendata, desc->d_unit);
@@ -128,13 +283,36 @@ static int
 hostdisk_print(int verbose)
 {
 	char line[80];
+	hdinfo_t *hd, *md;
+	int ret = 0;
 
 	printf("%s devices:", hostdisk.dv_name);
 	if (pager_output("\n") != 0)
 		return (1);
 
-	snprintf(line, sizeof(line), "    /dev%d:   Host disk\n", 0);
-	return (pager_output(line));
+	STAILQ_FOREACH(hd, &hdinfo, hd_link) {
+		snprintf(line, sizeof(line),
+		    "   /dev/%s: %ju X %ju: %ju bytes\n",
+		    hd->hd_name,
+		    (uintmax_t)hd->hd_sectors,
+		    (uintmax_t)hd->hd_sectorsize,
+		    (uintmax_t)hd->hd_size);
+		if ((ret = pager_output(line)) != 0)
+			break;
+		STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
+			snprintf(line, sizeof(line),
+			    "     /dev/%s: %ju X %ju: %ju bytes\n",
+			    md->hd_name,
+			    (uintmax_t)md->hd_sectors,
+			    (uintmax_t)md->hd_sectorsize,
+			    (uintmax_t)md->hd_size);
+			if ((ret = pager_output(line)) != 0)
+				goto done;
+		}
+	}
+
+done:
+	return (ret);
 }
 
 static char *