From nobody Fri Jan 13 21:24:49 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NtvZT5YVKz2qkYF; Fri, 13 Jan 2023 21:24:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NtvZT3wsXz40rs; Fri, 13 Jan 2023 21:24:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+ALIEJ8ch8XiaWSAnIsPaJGNdRPUn5vI48DIgfbAXaI=; b=i2261HO/3ysLF0RtmerZxZyG2mKZALwq+B2iBFyxOxtjjxjaChdRGopRIfQ882WcVXxUrN gS27giDWEF7boj3mAfTxhkhsztaWuVAKt6mh11tPhd4BiBpNj/JPrJMU21XifjOdax2Uhn O+cPRbQ7Yn5b2+ywJIWjPyGVDM+YQXBPI3aStkd5M/CYDOttDE/a3i1efNXktAJdT+gvpN TkAlGCdIaeGmtpBksBcsUL2/EwnUTQwEqPa9iEvulx8dkFsPOmHkShOpyia4lAz0ED73HS qCzZHgxpQL7TRX7Wf4bLbFfNwRJPke9HvpJ2LJEhgSlFiBDs4X/M3UPh2r7Adw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+ALIEJ8ch8XiaWSAnIsPaJGNdRPUn5vI48DIgfbAXaI=; b=eny7ppWUmS/mbUT4VrHlxPjJEsrMxeaeLfSrWPqZUbtn5CDZYEmG7Uwl0H049T3jVw+pvf Wf+iLEPRQg1TFx3QR1emOvz3jQ9xbu/VYyyJ4JE/VViIGdR2gVAubY9+q3PFXMP+watjWU 95cXbO4DPAPD5//B9NLPm5ufu8i5BqZl55xFsnVY3tQAsuf+CxgKpyLH/9XbRtOYHFHMOQ wsTZG+Rg+TjKidc6Mr2dzMqQNPxRP7ohMZ8W4NKr/6s0NFAePw5j6QTg4fRMFucqCQz+M7 DZCJOOAc1TOVv2y2ATWdjebPb4b6Q94JAsxXhdE7c1VVfQKmQlf8pebynkjxbw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673645089; a=rsa-sha256; cv=none; b=jbPincaWy8meNnQ+2cLc9rEkyW64O6xdyaPa82mYaQv24050oKF1v1lAxGo97x9HQVXvV2 hLHhuRIkZxmm+AlIEm3r7+NPHRubcz/pSiOH1jYr1mi4FLQsx/3vaeXN2Mh2PFHUjr/nuS PLEAGR9KNiuWeN/9gBwjHWBvTljYwdNmilLVIQIh2dq+BbsONphmFG/rwH55QP5sJXdlni xQj2+O/fW1+gKJOtvMHzeEw7fbuQXGIyqq57IH1DphYfrI+L8bryyGiM0nL+oOl5fHNXrH lmBo6VDvIXF2+bejUpcGk2hXT+DstADxdtNk2Fv6pNanfgamX7BpZ613nknI/g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NtvZT2QLpzMv5; Fri, 13 Jan 2023 21:24:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30DLOnqQ040778; Fri, 13 Jan 2023 21:24:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30DLOnlc040777; Fri, 13 Jan 2023 21:24:49 GMT (envelope-from git) Date: Fri, 13 Jan 2023 21:24:49 GMT Message-Id: <202301132124.30DLOnlc040777@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: cc82c650a72f - main - kboot: Add ZFS support to hostdisk List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cc82c650a72fcdde255b023703a9b214a81254ec Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=cc82c650a72fcdde255b023703a9b214a81254ec commit cc82c650a72fcdde255b023703a9b214a81254ec Author: Warner Losh AuthorDate: 2023-01-13 21:20:30 +0000 Commit: Warner Losh CommitDate: 2023-01-13 21:22:38 +0000 kboot: Add ZFS support to hostdisk Add helper function to walk through the disk drives we've found to look for zpools. main.c will still need to call this because the loader hasn't implemented a good way to 'taste' drives for zpools and/or GELI partitions (mostly because there's no generic list of candidate devices). Sponsored by: Netflix Reviewed by: kevans Differential Revision: https://reviews.freebsd.org/D38007 --- stand/kboot/hostdisk.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ stand/kboot/kboot.h | 7 ++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/stand/kboot/hostdisk.c b/stand/kboot/hostdisk.c index cbe8faabca96..a96c38c21182 100644 --- a/stand/kboot/hostdisk.c +++ b/stand/kboot/hostdisk.c @@ -30,8 +30,14 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include "host_syscall.h" #include "kboot.h" +#include "bootstrap.h" +#ifdef LOADER_ZFS_SUPPORT +#include "libzfs.h" +#include +#endif static int hostdisk_init(void); static int hostdisk_strategy(void *devdata, int flag, daddr_t dblk, @@ -75,6 +81,8 @@ typedef struct hdinfo { uint64_t hd_sectors; uint64_t hd_sectorsize; int hd_flags; +#define HDF_HAS_ZPOOL 1 /* We found a zpool here and uuid valid */ + uint64_t hd_zfs_uuid; } hdinfo_t; #define dev2hd(d) ((hdinfo_t *)d->d_opendata) @@ -416,3 +424,117 @@ hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path) *idev = dev; return (0); } + +#ifdef LOADER_ZFS_SUPPORT +static bool +hostdisk_zfs_check_one(hdinfo_t *hd) +{ + char *fn; + bool found = false; + uint64_t pool_uuid; + + if (asprintf(&fn, "%s:", hd->hd_dev) == -1) + return (false); + pool_uuid = 0; + zfs_probe_dev(fn, &pool_uuid, false); + if (pool_uuid != 0) { + found = true; + hd->hd_flags |= HDF_HAS_ZPOOL; + hd->hd_zfs_uuid = pool_uuid; + } + free(fn); + + return (found); +} + +void +hostdisk_zfs_probe(void) +{ + hdinfo_t *hd, *md; + + STAILQ_FOREACH(hd, &hdinfo, hd_link) { + if (hostdisk_zfs_check_one(hd)) + continue; + STAILQ_FOREACH(md, &hd->hd_children, hd_link) { + hostdisk_zfs_check_one(md); + } + } +} + +/* XXX refactor */ +static bool +sanity_check_currdev(void) +{ + struct stat st; + + return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 || +#ifdef PATH_BOOTABLE_TOKEN + stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */ +#endif + stat(PATH_KERNEL, &st) == 0); +} + +/* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */ +static bool +probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev) +{ + char *devname; + struct zfs_devdesc currdev; + char *buf = NULL; + bool bootable; + + currdev.dd.d_dev = &zfs_dev; + currdev.dd.d_unit = 0; + currdev.pool_guid = pool_guid; + currdev.root_guid = root_guid; + devname = devformat(&currdev.dd); + if (setcurrdev) + set_currdev(devname); + + bootable = sanity_check_currdev(); + if (bootable) { + buf = malloc(VDEV_PAD_SIZE); + if (buf != NULL) { + if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf, + VDEV_PAD_SIZE) == 0) { + printf("zfs bootonce: %s\n", buf); + if (setcurrdev) + set_currdev(buf); + setenv("zfs-bootonce", buf, 1); + } + free(buf); + (void)zfs_attach_nvstore(&currdev); + } + init_zfs_boot_options(devname); + } + return (bootable); +} + +static bool +hostdisk_zfs_try_default(hdinfo_t *hd) +{ + return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true)); +} + +bool +hostdisk_zfs_find_default(void) +{ + hdinfo_t *hd, *md; + + STAILQ_FOREACH(hd, &hdinfo, hd_link) { + if (hd->hd_flags & HDF_HAS_ZPOOL) { + if (hostdisk_zfs_try_default(hd)) + return (true); + continue; + } + STAILQ_FOREACH(md, &hd->hd_children, hd_link) { + if (md->hd_flags & HDF_HAS_ZPOOL) { + if (hostdisk_zfs_try_default(md)) + return (true); + } + } + } + return (false); +} + +#endif diff --git a/stand/kboot/kboot.h b/stand/kboot/kboot.h index 72be7299995b..58cbedff67a1 100644 --- a/stand/kboot/kboot.h +++ b/stand/kboot/kboot.h @@ -14,14 +14,17 @@ vm_offset_t acpi_rsdp(void); void do_init(void); -extern const char *hostfs_root; - /* Per-platform fdt fixup */ void fdt_arch_fixups(void *fdtp); uint64_t kboot_get_phys_load_segment(void); uint8_t kboot_get_kernel_machine_bits(void); +/* hostdisk.c */ +extern const char *hostfs_root; +void hostdisk_zfs_probe(void); +bool hostdisk_zfs_find_default(void); + /* util.c */ bool file2str(const char *fn, char *buffer, size_t buflen); bool file2u64(const char *fn, uint64_t *val);