From 9d5f662f10524afd73de319ecf60cd85975801d5 Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Mon, 13 Sep 2021 15:10:56 +0000 Subject: [PATCH] libzfs_sendrecv: Refactor find_redact_book Factor out get_bookmarks, find_redact_pair, and get_redact_complete helper functions to improve the readability of find_redact_book. Reviewed-by: Brian Behlendorf Signed-off-by: Ryan Moeller Closes #12967 --- lib/libzfs/libzfs_sendrecv.c | 79 ++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/lib/libzfs/libzfs_sendrecv.c b/lib/libzfs/libzfs_sendrecv.c index e034b4378991..4b660158e6e1 100644 --- a/lib/libzfs/libzfs_sendrecv.c +++ b/lib/libzfs/libzfs_sendrecv.c @@ -1557,6 +1557,53 @@ redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1, return (B_TRUE); } +static int +get_bookmarks(const char *path, nvlist_t **bmarksp) +{ + nvlist_t *props = fnvlist_alloc(); + int error; + + fnvlist_add_boolean(props, "redact_complete"); + fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); + error = lzc_get_bookmarks(path, props, bmarksp); + fnvlist_free(props); + return (error); +} + +static nvpair_t * +find_redact_pair(nvlist_t *bmarks, const uint64_t *redact_snap_guids, + int num_redact_snaps) +{ + nvpair_t *pair; + + for (pair = nvlist_next_nvpair(bmarks, NULL); pair; + pair = nvlist_next_nvpair(bmarks, pair)) { + + nvlist_t *bmark = fnvpair_value_nvlist(pair); + nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, + zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); + uint_t len = 0; + uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist, + ZPROP_VALUE, &len); + if (redact_snaps_equal(redact_snap_guids, + num_redact_snaps, bmarksnaps, len)) { + break; + } + } + return (pair); +} + +static boolean_t +get_redact_complete(nvpair_t *pair) +{ + nvlist_t *bmark = fnvpair_value_nvlist(pair); + nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete"); + boolean_t complete = fnvlist_lookup_boolean_value(vallist, + ZPROP_VALUE); + + return (complete); +} + /* * Check that the list of redaction snapshots in the bookmark matches the send * we're resuming, and return whether or not it's complete. @@ -1570,17 +1617,12 @@ find_redact_book(libzfs_handle_t *hdl, const char *path, char **bookname) { char errbuf[1024]; - int error = 0; - nvlist_t *props = fnvlist_alloc(); nvlist_t *bmarks; (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, "cannot resume send")); - fnvlist_add_boolean(props, "redact_complete"); - fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); - error = lzc_get_bookmarks(path, props, &bmarks); - fnvlist_free(props); + int error = get_bookmarks(path, &bmarks); if (error != 0) { if (error == ESRCH) { zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, @@ -1594,39 +1636,22 @@ find_redact_book(libzfs_handle_t *hdl, const char *path, } return (zfs_error(hdl, EZFS_BADPROP, errbuf)); } - nvpair_t *pair; - for (pair = nvlist_next_nvpair(bmarks, NULL); pair; - pair = nvlist_next_nvpair(bmarks, pair)) { - - nvlist_t *bmark = fnvpair_value_nvlist(pair); - nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, - zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS)); - uint_t len = 0; - uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist, - ZPROP_VALUE, &len); - if (redact_snaps_equal(redact_snap_guids, - num_redact_snaps, bmarksnaps, len)) { - break; - } - } + nvpair_t *pair = find_redact_pair(bmarks, redact_snap_guids, + num_redact_snaps); if (pair == NULL) { fnvlist_free(bmarks); zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no appropriate redaction bookmark exists")); return (zfs_error(hdl, EZFS_BADPROP, errbuf)); } - char *name = nvpair_name(pair); - nvlist_t *bmark = fnvpair_value_nvlist(pair); - nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete"); - boolean_t complete = fnvlist_lookup_boolean_value(vallist, - ZPROP_VALUE); + boolean_t complete = get_redact_complete(pair); if (!complete) { fnvlist_free(bmarks); zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "incomplete redaction bookmark provided")); return (zfs_error(hdl, EZFS_BADPROP, errbuf)); } - *bookname = strndup(name, ZFS_MAX_DATASET_NAME_LEN); + *bookname = strndup(nvpair_name(pair), ZFS_MAX_DATASET_NAME_LEN); ASSERT3P(*bookname, !=, NULL); fnvlist_free(bmarks); return (0);