Skip to content

Commit 13ee306

Browse files
committed
speed up recv_fix_encryption_hierarchy()
Signed-off-by: George Amanakis <[email protected]>
1 parent a98286a commit 13ee306

File tree

1 file changed

+70
-28
lines changed

1 file changed

+70
-28
lines changed

lib/libzfs/libzfs_sendrecv.c

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,30 +3376,24 @@ created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
33763376
*/
33773377
static int
33783378
recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3379-
nvlist_t *stream_nv)
3379+
nvlist_t *stream_nv, avl_tree_t *stream_avl)
33803380
{
33813381
int err;
3382-
nvpair_t *fselem = NULL;
3383-
nvlist_t *stream_fss;
3382+
nvpair_t *fselem = NULL, *nextfselem;
3383+
nvlist_t *local_nv, *stream_fss;
3384+
avl_tree_t *local_avl;
3385+
boolean_t recursive;
3386+
char fsname[ZFS_MAX_DATASET_NAME_LEN], *cp;
33843387

3385-
stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3388+
recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3389+
ENOENT);
33863390

3391+
stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
33873392
while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3388-
zfs_handle_t *zhp = NULL;
3389-
uint64_t crypt;
3390-
nvlist_t *snaps, *props, *stream_nvfs = NULL;
3393+
nvlist_t *snaps, *stream_nvfs;
33913394
nvpair_t *snapel = NULL;
3392-
boolean_t is_encroot, is_clone, stream_encroot;
3393-
char *cp;
3394-
const char *stream_keylocation = NULL;
3395-
char keylocation[MAXNAMELEN];
3396-
char fsname[ZFS_MAX_DATASET_NAME_LEN];
3397-
3398-
keylocation[0] = '\0';
33993395
stream_nvfs = fnvpair_value_nvlist(fselem);
34003396
snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3401-
props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3402-
stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
34033397

34043398
/* find a snapshot from the stream that exists locally */
34053399
err = ENOENT;
@@ -3413,14 +3407,40 @@ recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
34133407
break;
34143408
}
34153409

3416-
if (err != 0)
3417-
continue;
3410+
if (err == 0)
3411+
break;
3412+
}
34183413

3419-
cp = strchr(fsname, '@');
3420-
if (cp != NULL)
3421-
*cp = '\0';
3414+
if (err != 0)
3415+
return (0);
34223416

3423-
zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3417+
cp = strchr(fsname, '@');
3418+
if (cp != NULL)
3419+
*cp = '\0';
3420+
3421+
if ((err = gather_nvlist(hdl, fsname, NULL, NULL,
3422+
recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3423+
B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3424+
return (err);
3425+
3426+
for (fselem = nvlist_next_nvpair(local_nv, NULL); fselem;
3427+
fselem = nextfselem) {
3428+
zfs_handle_t *zhp = NULL;
3429+
uint64_t crypt;
3430+
nvlist_t *stream_props, *snaps, *stream_nvfs = NULL,
3431+
*nvfs = NULL;
3432+
boolean_t is_encroot, is_clone, stream_encroot;
3433+
const char *stream_keylocation = NULL, *fs;
3434+
char keylocation[MAXNAMELEN];
3435+
nvpair_t *snapelem;
3436+
3437+
nextfselem = nvlist_next_nvpair(local_nv, fselem);
3438+
3439+
nvfs = fnvpair_value_nvlist(fselem);
3440+
snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3441+
fs = fnvlist_lookup_string(nvfs, "name");
3442+
(void) printf("fs: %s\n", fs);
3443+
zhp = zfs_open(hdl, fs, ZFS_TYPE_DATASET);
34243444
if (zhp == NULL) {
34253445
err = ENOENT;
34263446
goto error;
@@ -3436,6 +3456,28 @@ recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
34363456
continue;
34373457
}
34383458

3459+
keylocation[0] = '\0';
3460+
3461+
/*
3462+
* First find the stream's fs
3463+
*/
3464+
for (snapelem = nvlist_next_nvpair(snaps, NULL);
3465+
snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3466+
uint64_t thisguid;
3467+
3468+
thisguid = fnvpair_value_uint64(snapelem);
3469+
stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3470+
3471+
if (stream_nvfs != NULL)
3472+
break;
3473+
}
3474+
3475+
if (stream_nvfs == NULL)
3476+
continue;
3477+
3478+
stream_props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3479+
stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3480+
34393481
/*
34403482
* If the dataset is flagged as an encryption root, was not
34413483
* received as a clone and is not currently an encryption root,
@@ -3451,7 +3493,7 @@ recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
34513493
}
34523494
}
34533495

3454-
stream_keylocation = fnvlist_lookup_string(props,
3496+
stream_keylocation = fnvlist_lookup_string(stream_props,
34553497
zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
34563498

34573499
/*
@@ -3518,14 +3560,14 @@ recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
35183560
boolean_t needagain, progress, recursive;
35193561
const char *s1, *s2;
35203562

3563+
if (flags->dryrun)
3564+
return (0);
3565+
35213566
fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
35223567

35233568
recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
35243569
ENOENT);
35253570

3526-
if (flags->dryrun)
3527-
return (0);
3528-
35293571
again:
35303572
needagain = progress = B_FALSE;
35313573

@@ -3999,9 +4041,9 @@ zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
39994041
stream_nv, stream_avl, NULL);
40004042
}
40014043

4002-
if (raw && softerr == 0 && *top_zfs != NULL) {
4044+
if (raw && softerr == 0 && *top_zfs != NULL && !flags->dryrun) {
40034045
softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
4004-
stream_nv);
4046+
stream_nv, stream_avl);
40054047
}
40064048

40074049
out:

0 commit comments

Comments
 (0)