Skip to content

Commit 0e26c5d

Browse files
gamanakislundman
authored andcommitted
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 openzfs#16929
1 parent 327a383 commit 0e26c5d

File tree

2 files changed

+80
-51
lines changed

2 files changed

+80
-51
lines changed

lib/libzfs/libzfs_sendrecv.c

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,66 +3393,78 @@ created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
33933393
*/
33943394
static int
33953395
recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3396-
nvlist_t *stream_nv)
3396+
nvlist_t *stream_nv, avl_tree_t *stream_avl)
33973397
{
33983398
int err;
33993399
nvpair_t *fselem = NULL;
3400-
nvlist_t *stream_fss;
3400+
nvlist_t *local_nv;
3401+
avl_tree_t *local_avl;
3402+
boolean_t recursive;
3403+
3404+
recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3405+
ENOENT);
34013406

3402-
stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3407+
/* Using top_zfs, gather the nvlists for all local filesystems. */
3408+
if ((err = gather_nvlist(hdl, top_zfs, NULL, NULL,
3409+
recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3410+
B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3411+
return (err);
34033412

3404-
while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3413+
/*
3414+
* Go through the nvlists of the local filesystems and check for
3415+
* encryption roots.
3416+
*/
3417+
while ((fselem = nvlist_next_nvpair(local_nv, fselem)) != NULL) {
34053418
zfs_handle_t *zhp = NULL;
34063419
uint64_t crypt;
3407-
nvlist_t *snaps, *props, *stream_nvfs = NULL;
3408-
nvpair_t *snapel = NULL;
3420+
nvlist_t *stream_props, *snaps, *stream_nvfs = NULL,
3421+
*nvfs = NULL;
34093422
boolean_t is_encroot, is_clone, stream_encroot;
3410-
char *cp;
3411-
const char *stream_keylocation = NULL;
3423+
const char *stream_keylocation = NULL, *fsname;
34123424
char keylocation[MAXNAMELEN];
3413-
char fsname[ZFS_MAX_DATASET_NAME_LEN];
3414-
3415-
keylocation[0] = '\0';
3416-
stream_nvfs = fnvpair_value_nvlist(fselem);
3417-
snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3418-
props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3419-
stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3420-
3421-
/* find a snapshot from the stream that exists locally */
3422-
err = ENOENT;
3423-
while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3424-
uint64_t guid;
3425-
3426-
guid = fnvpair_value_uint64(snapel);
3427-
err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3428-
fsname);
3429-
if (err == 0)
3430-
break;
3431-
}
3432-
3433-
if (err != 0)
3434-
continue;
3435-
3436-
cp = strchr(fsname, '@');
3437-
if (cp != NULL)
3438-
*cp = '\0';
3425+
nvpair_t *snapelem;
34393426

3427+
nvfs = fnvpair_value_nvlist(fselem);
3428+
snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3429+
fsname = fnvlist_lookup_string(nvfs, "name");
34403430
zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
34413431
if (zhp == NULL) {
34423432
err = ENOENT;
34433433
goto error;
34443434
}
34453435

3446-
crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3447-
is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3448-
(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3449-
34503436
/* we don't need to do anything for unencrypted datasets */
3437+
crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
34513438
if (crypt == ZIO_CRYPT_OFF) {
34523439
zfs_close(zhp);
34533440
continue;
34543441
}
34553442

3443+
is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3444+
(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3445+
keylocation[0] = '\0';
3446+
3447+
/*
3448+
* Go through the snapshots of the local filesystem and find
3449+
* the stream's filesystem.
3450+
*/
3451+
for (snapelem = nvlist_next_nvpair(snaps, NULL);
3452+
snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3453+
uint64_t thisguid;
3454+
3455+
thisguid = fnvpair_value_uint64(snapelem);
3456+
stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3457+
3458+
if (stream_nvfs != NULL)
3459+
break;
3460+
}
3461+
3462+
if (stream_nvfs == NULL)
3463+
continue;
3464+
3465+
stream_props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3466+
stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3467+
34563468
/*
34573469
* If the dataset is flagged as an encryption root, was not
34583470
* received as a clone and is not currently an encryption root,
@@ -3468,7 +3480,7 @@ recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
34683480
}
34693481
}
34703482

3471-
stream_keylocation = fnvlist_lookup_string(props,
3483+
stream_keylocation = fnvlist_lookup_string(stream_props,
34723484
zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
34733485

34743486
/*
@@ -3535,14 +3547,14 @@ recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
35353547
boolean_t needagain, progress, recursive;
35363548
const char *s1, *s2;
35373549

3550+
if (flags->dryrun)
3551+
return (0);
3552+
35383553
fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
35393554

35403555
recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
35413556
ENOENT);
35423557

3543-
if (flags->dryrun)
3544-
return (0);
3545-
35463558
again:
35473559
needagain = progress = B_FALSE;
35483560

@@ -4016,9 +4028,9 @@ zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
40164028
stream_nv, stream_avl, NULL);
40174029
}
40184030

4019-
if (raw && softerr == 0 && *top_zfs != NULL) {
4031+
if (raw && *top_zfs != NULL && !flags->dryrun) {
40204032
softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
4021-
stream_nv);
4033+
stream_nv, stream_avl);
40224034
}
40234035

40244036
out:

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

Lines changed: 24 additions & 7 deletions
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)