Skip to content

Commit c2458ba

Browse files
authored
optimize recv_fix_encryption_hierarchy()
recv_fix_encryption_hierarchy() in its present state goes through all stream filesystems, and for each one traverses the snapshots in order to find one that exists locally. This happens by calling guid_to_name() for each snapshot, which iterates through all children of the filesystem. This results in CPU utilization of 100% for several minutes (for ~1000 filesystems on a Ryzen 4350G) for 1 thread at the end of a raw receive (-w, regardless whether encrypted or not, dryrun or not). Fix this by following a different logic: using the top_fs name, call gather_nvlist() to gather the nvlists for all local filesystems. For each one filesystem, go through the snapshots to find the corresponding stream's filesystem (since we know the snapshots guid and can search with it in stream_avl for the stream's fs). Then go on to fix the encryption roots and locations as in its present state. Avoiding guid_to_name() iteratively makes recv_fix_encryption_hierarchy() significantly faster (from several minutes to seconds for ~1000 filesystems on a Ryzen 4350G). Another problem is the following: in case we have promoted a clone of the filesystem outside the top filesystem specified in zfs send, zfs receive does not fail but returns an error: recv_incremental_replication() fails to find its origin and errors out with needagain=1. This results in recv_fix_hierarchy() not being called which may render some children of the top fs not mountable since their encryption root was not updated. To circumvent this make recv_incremental_replication() silently ignore this error. Reviewed-by: Alexander Motin <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: George Amanakis <[email protected]> Closes #16929
1 parent 88020b9 commit c2458ba

File tree

2 files changed

+80
-51
lines changed

2 files changed

+80
-51
lines changed

lib/libzfs/libzfs_sendrecv.c

+56-44
Original file line numberDiff line numberDiff line change
@@ -3376,66 +3376,78 @@ 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;
33823382
nvpair_t *fselem = NULL;
3383-
nvlist_t *stream_fss;
3383+
nvlist_t *local_nv;
3384+
avl_tree_t *local_avl;
3385+
boolean_t recursive;
3386+
3387+
recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3388+
ENOENT);
33843389

3385-
stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3390+
/* Using top_zfs, gather the nvlists for all local filesystems. */
3391+
if ((err = gather_nvlist(hdl, top_zfs, NULL, NULL,
3392+
recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3393+
B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3394+
return (err);
33863395

3387-
while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3396+
/*
3397+
* Go through the nvlists of the local filesystems and check for
3398+
* encryption roots.
3399+
*/
3400+
while ((fselem = nvlist_next_nvpair(local_nv, fselem)) != NULL) {
33883401
zfs_handle_t *zhp = NULL;
33893402
uint64_t crypt;
3390-
nvlist_t *snaps, *props, *stream_nvfs = NULL;
3391-
nvpair_t *snapel = NULL;
3403+
nvlist_t *stream_props, *snaps, *stream_nvfs = NULL,
3404+
*nvfs = NULL;
33923405
boolean_t is_encroot, is_clone, stream_encroot;
3393-
char *cp;
3394-
const char *stream_keylocation = NULL;
3406+
const char *stream_keylocation = NULL, *fsname;
33953407
char keylocation[MAXNAMELEN];
3396-
char fsname[ZFS_MAX_DATASET_NAME_LEN];
3397-
3398-
keylocation[0] = '\0';
3399-
stream_nvfs = fnvpair_value_nvlist(fselem);
3400-
snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3401-
props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3402-
stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3403-
3404-
/* find a snapshot from the stream that exists locally */
3405-
err = ENOENT;
3406-
while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3407-
uint64_t guid;
3408-
3409-
guid = fnvpair_value_uint64(snapel);
3410-
err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3411-
fsname);
3412-
if (err == 0)
3413-
break;
3414-
}
3415-
3416-
if (err != 0)
3417-
continue;
3418-
3419-
cp = strchr(fsname, '@');
3420-
if (cp != NULL)
3421-
*cp = '\0';
3408+
nvpair_t *snapelem;
34223409

3410+
nvfs = fnvpair_value_nvlist(fselem);
3411+
snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3412+
fsname = fnvlist_lookup_string(nvfs, "name");
34233413
zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
34243414
if (zhp == NULL) {
34253415
err = ENOENT;
34263416
goto error;
34273417
}
34283418

3429-
crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3430-
is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3431-
(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3432-
34333419
/* we don't need to do anything for unencrypted datasets */
3420+
crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
34343421
if (crypt == ZIO_CRYPT_OFF) {
34353422
zfs_close(zhp);
34363423
continue;
34373424
}
34383425

3426+
is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3427+
(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3428+
keylocation[0] = '\0';
3429+
3430+
/*
3431+
* Go through the snapshots of the local filesystem and find
3432+
* the stream's filesystem.
3433+
*/
3434+
for (snapelem = nvlist_next_nvpair(snaps, NULL);
3435+
snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3436+
uint64_t thisguid;
3437+
3438+
thisguid = fnvpair_value_uint64(snapelem);
3439+
stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3440+
3441+
if (stream_nvfs != NULL)
3442+
break;
3443+
}
3444+
3445+
if (stream_nvfs == NULL)
3446+
continue;
3447+
3448+
stream_props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3449+
stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3450+
34393451
/*
34403452
* If the dataset is flagged as an encryption root, was not
34413453
* received as a clone and is not currently an encryption root,
@@ -3451,7 +3463,7 @@ recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
34513463
}
34523464
}
34533465

3454-
stream_keylocation = fnvlist_lookup_string(props,
3466+
stream_keylocation = fnvlist_lookup_string(stream_props,
34553467
zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
34563468

34573469
/*
@@ -3518,14 +3530,14 @@ recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
35183530
boolean_t needagain, progress, recursive;
35193531
const char *s1, *s2;
35203532

3533+
if (flags->dryrun)
3534+
return (0);
3535+
35213536
fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
35223537

35233538
recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
35243539
ENOENT);
35253540

3526-
if (flags->dryrun)
3527-
return (0);
3528-
35293541
again:
35303542
needagain = progress = B_FALSE;
35313543

@@ -3999,9 +4011,9 @@ zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
39994011
stream_nv, stream_avl, NULL);
40004012
}
40014013

4002-
if (raw && softerr == 0 && *top_zfs != NULL) {
4014+
if (raw && *top_zfs != NULL && !flags->dryrun) {
40034015
softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
4004-
stream_nv);
4016+
stream_nv, stream_avl);
40054017
}
40064018

40074019
out:

tests/zfs-tests/tests/functional/rsend/send_encrypted_hierarchy.ksh

+24-7
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ log_must eval "zfs receive -d -F $POOL2 < $BACKDIR/fs-before-R"
6161
dstds=$(get_dst_ds $POOL/$FS $POOL2)
6262
log_must cmp_ds_subs $POOL/$FS $dstds
6363

64-
log_must verify_encryption_root $POOL/$FS $POOL/$FS
65-
log_must verify_keylocation $POOL/$FS "prompt"
66-
log_must verify_origin $POOL/$FS "-"
64+
log_must verify_encryption_root $POOL2/$FS $POOL2/$FS
65+
log_must verify_keylocation $POOL2/$FS "prompt"
66+
log_must verify_origin $POOL2/$FS "-"
6767

68-
log_must verify_encryption_root $POOL/clone $POOL/$FS
69-
log_must verify_keylocation $POOL/clone "none"
70-
log_must verify_origin $POOL/clone "$POOL/$FS@snap"
68+
log_must verify_encryption_root $POOL2/clone $POOL2/$FS
69+
log_must verify_keylocation $POOL2/clone "none"
70+
log_must verify_origin $POOL2/clone "$POOL2/$FS@snap"
7171

7272
log_must verify_encryption_root $POOL/$FS/child $POOL/$FS
73-
log_must verify_keylocation $POOL/$FS/child "none"
73+
log_must verify_encryption_root $POOL2/$FS/child $POOL2/$FS
74+
log_must verify_keylocation $POOL2/$FS/child "none"
7475

7576
# Alter the hierarchy and re-send
7677
log_must eval "echo $PASSPHRASE1 | zfs change-key -o keyformat=passphrase" \
@@ -93,4 +94,20 @@ log_must verify_origin $POOL/clone "-"
9394
log_must verify_encryption_root $POOL/$FS/child $POOL/$FS/child
9495
log_must verify_keylocation $POOL/$FS/child "prompt"
9596

97+
log_must verify_encryption_root $POOL2 "-"
98+
log_must verify_encryption_root $POOL2/clone $POOL2/clone
99+
log_must verify_encryption_root $POOL2/$FS $POOL2/clone
100+
log_must verify_encryption_root $POOL2/$FS/child $POOL2/$FS/child
101+
102+
log_must verify_keylocation $POOL2 "none"
103+
log_must verify_keylocation $POOL2/clone "prompt"
104+
log_must verify_keylocation $POOL2/$FS "none"
105+
log_must verify_keylocation $POOL2/$FS/child "prompt"
106+
107+
log_must verify_origin $POOL2 "-"
108+
log_must verify_origin $POOL2/clone "-"
109+
log_must verify_origin $POOL2/$FS "$POOL2/clone@snap"
110+
log_must verify_origin $POOL2/$FS/child "-"
111+
log_must zfs list
112+
96113
log_pass "Raw recursive sends preserve filesystem structure."

0 commit comments

Comments
 (0)