git: 6479bd1b7d34 - main - stand/zfs: Refactor zfs_get_bootenv

From: Warner Losh <imp_at_FreeBSD.org>
Date: Mon, 01 May 2023 21:04:13 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=6479bd1b7d347e42012fab64b595b0cf091e13f4

commit 6479bd1b7d347e42012fab64b595b0cf091e13f4
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-05-01 15:26:51 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-05-01 21:02:53 +0000

    stand/zfs: Refactor zfs_get_bootenv
    
    Create a new interface to zfs_get_bootenv called zfs_get_bootenv_spa
    which takes a spa instead of a void * (effectively a devdesc *). Use
    that in zfs_get_bootenv.
    
    Sponsored by:           Netflix
    Reviewed by:            tsoome, kevans
    Differential Revision:  https://reviews.freebsd.org/D39409
---
 stand/libsa/zfs/zfs.c     | 27 ++-------------------------
 stand/libsa/zfs/zfsimpl.c | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c
index 4a4d3cf26843..17f213fe833c 100644
--- a/stand/libsa/zfs/zfs.c
+++ b/stand/libsa/zfs/zfs.c
@@ -798,35 +798,12 @@ zfs_probe_partition(void *arg, const char *partname,
 int
 zfs_get_bootenv(void *vdev, nvlist_t **benvp)
 {
-	struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
-	nvlist_t *benv = NULL;
-	vdev_t *vd;
 	spa_t *spa;
 
-	if (dev->dd.d_dev->dv_type != DEVT_ZFS)
-		return (ENOTSUP);
-
-	if ((spa = spa_find_by_dev(dev)) == NULL)
+	if ((spa = spa_find_by_dev((struct zfs_devdesc *)vdev)) == NULL)
 		return (ENXIO);
 
-	if (spa->spa_bootenv == NULL) {
-		STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
-		    v_childlink) {
-			benv = vdev_read_bootenv(vd);
-
-			if (benv != NULL)
-				break;
-		}
-		spa->spa_bootenv = benv;
-	} else {
-		benv = spa->spa_bootenv;
-	}
-
-	if (benv == NULL)
-		return (ENOENT);
-
-	*benvp = benv;
-	return (0);
+	return (zfs_get_bootenv_spa(spa, benvp));
 }
 
 /*
diff --git a/stand/libsa/zfs/zfsimpl.c b/stand/libsa/zfs/zfsimpl.c
index 29e8bb82b6e2..40a1448a0c9f 100644
--- a/stand/libsa/zfs/zfsimpl.c
+++ b/stand/libsa/zfs/zfsimpl.c
@@ -3853,3 +3853,33 @@ done:
 		free(entry);
 	return (rc);
 }
+
+/*
+ * Return either a cached copy of the bootenv, or read each of the vdev children
+ * looking for the bootenv. Cache what's found and return the results. Returns 0
+ * when benvp is filled in, and some errno when not.
+ */
+static int
+zfs_get_bootenv_spa(spa_t *spa, nvlist_t **benvp)
+{
+	vdev_t *vd;
+	nvlist_t *benv = NULL;
+
+	if (spa->spa_bootenv == NULL) {
+		STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children,
+		    v_childlink) {
+			benv = vdev_read_bootenv(vd);
+
+			if (benv != NULL)
+				break;
+		}
+		spa->spa_bootenv = benv;
+	}
+	benv = spa->spa_bootenv;
+
+	if (benv == NULL)
+		return (ENOENT);
+
+	*benvp = benv;
+	return (0);
+}