From nobody Tue Jan 24 22:14:29 2023 X-Original-To: dev-commits-src-branches@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 4P1h8j73P9z3bgwC; Tue, 24 Jan 2023 22:14:29 +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 4P1h8j584Rz3LX2; Tue, 24 Jan 2023 22:14:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674598469; 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=WqtuEgcLUxv/64l5jTGgHrbuIGg3rLSzdDpHvlVCIYs=; b=HmO1ROPRIGZaupfRJLyCUp7//ZifmzQymAStQaBtbXTtBGODdL+qStgB0n48dWx5oOn+U5 8JX0dEY4VQRSsQdB6N0dONpWU8IMaRY7ZEVKR2YGzQCdB3qwCLU99cdHMjuX0p0oUZQ2Il oq1rn0T1H6BIvpacmfED+SPiOTpPk+mh0D9DO+b4EtETvM0CZdAEnV2S8Zfahhxh3t6+t/ c/HvDoZf3OFXl6cTBqyWeS8KwMJabKoNFko7Hh5C0eg9akwBcEZDcF74vC8vxwInYAAfqq zWCgx2V9eOE2Phx+I+UT/Q2sNHkjUpk9pH7g+clYD1vXpnmUIH4iX9xyGoRBFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674598469; 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=WqtuEgcLUxv/64l5jTGgHrbuIGg3rLSzdDpHvlVCIYs=; b=Obod/3C7ZFj7k6UKz+xb0YIOCoKlPs6C/UZBw8+lymWYRSoExETN5OpbCFDkGoxhz+riIY yL+zxwHLJPNJk02qhzjiQOatC0ttvnyVdoy2yHXgD6G09bMIzlqOrhfg2soMKg2xxPfHOQ E8/3JlkzUBNeAeNGAW5toWwDvpCAR1ENZcBX2aJn+hfZOFzUI7eU/m2bH4I+SEFrjtDuH9 RNHHodaRruWZ02E1QTMyphyX/mau9eeDw9Nmtdk5FmzySEbtIr3mbfSHpzM3Uj7Wszv2To HjBMwXF9iMwilHHZs/5uZnf0maxmeD6oge8Gt3TFDvf/rsVU7LsnFk7aSgzaTA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674598469; a=rsa-sha256; cv=none; b=nDBkgMlJWqQdIViFN3o6KNC82t+7iiMy/9xbi2vAlgkJfTe4x1q8C57PdXtd8ds22GjqtS a77+WZGwwfdAKreUaT8ZG3TuebrxALrql5e04w7y7fdijf7D6ek6A1YzYX447+3mjCt497 IGD0kdpBcOQvcdluSr2E2CawuhFdgS1BQfcPWLKSXskw3aVYUtu1HEJycsm5iOstuqL05U SHKu60e8IuqQJ3vkbnGl4jZCNPpaaeqN9HsJBt0i2BNR8KUyAJkAok1G+0aoUovQelu4Kr eeV8spVCVhLMQQOndlMv/HaXEaw7O+8/Ra3W0YsJFWdwhsxEWBpyxLadiU5FOg== 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 4P1h8j3ysTzQhN; Tue, 24 Jan 2023 22:14:29 +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 30OMETJI089887; Tue, 24 Jan 2023 22:14:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30OMETSo089886; Tue, 24 Jan 2023 22:14:29 GMT (envelope-from git) Date: Tue, 24 Jan 2023 22:14:29 GMT Message-Id: <202301242214.30OMETSo089886@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: 85ab3bb9897c - stable/13 - kboot: Add ZFS support to hostdisk List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/13 X-Git-Reftype: branch X-Git-Commit: 85ab3bb9897c7a9edaa4322bff770b496f50cc91 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=85ab3bb9897c7a9edaa4322bff770b496f50cc91 commit 85ab3bb9897c7a9edaa4322bff770b496f50cc91 Author: Warner Losh AuthorDate: 2023-01-13 21:20:30 +0000 Commit: Warner Losh CommitDate: 2023-01-24 21:49:46 +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 (cherry picked from commit cc82c650a72fcdde255b023703a9b214a81254ec) --- 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);