Skip to content

Commit 1553e00

Browse files
fdmananakdave
authored andcommitted
btrfs: send: do not allocate memory for xattr data when checking it exists
When checking if xattrs were deleted we don't care about their data, but we are allocating memory for the data and copying it, which only wastes time and can result in an unnecessary error in case the allocation fails. So stop allocating memory and copying data by making find_xattr() and __find_xattr() skip those steps if the given data buffer is NULL. Reviewed-by: Boris Burkov <boris@bur.io> Signed-off-by: Filipe Manana <fdmanana@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent ad3b3be commit 1553e00

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

fs/btrfs/send.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,7 @@ struct find_xattr_ctx {
49434943
int found_idx;
49444944
char *found_data;
49454945
int found_data_len;
4946+
bool copy_data;
49464947
};
49474948

49484949
static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
@@ -4954,9 +4955,11 @@ static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
49544955
strncmp(name, ctx->name, name_len) == 0) {
49554956
ctx->found_idx = num;
49564957
ctx->found_data_len = data_len;
4957-
ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4958-
if (!ctx->found_data)
4959-
return -ENOMEM;
4958+
if (ctx->copy_data) {
4959+
ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4960+
if (!ctx->found_data)
4961+
return -ENOMEM;
4962+
}
49604963
return 1;
49614964
}
49624965
return 0;
@@ -4976,6 +4979,7 @@ static int find_xattr(struct btrfs_root *root,
49764979
ctx.found_idx = -1;
49774980
ctx.found_data = NULL;
49784981
ctx.found_data_len = 0;
4982+
ctx.copy_data = (data != NULL);
49794983

49804984
ret = iterate_dir_item(root, path, __find_xattr, &ctx);
49814985
if (ret < 0)
@@ -4987,7 +4991,7 @@ static int find_xattr(struct btrfs_root *root,
49874991
*data = ctx.found_data;
49884992
*data_len = ctx.found_data_len;
49894993
} else {
4990-
kfree(ctx.found_data);
4994+
ASSERT(ctx.found_data == NULL);
49914995
}
49924996
return ctx.found_idx;
49934997
}

0 commit comments

Comments
 (0)