btrfs-progs: convert: simplify create_image_file_range()

The logic at the beginning of this function to handle reserved ranges
was pretty complex and hard to follow. By refactoring it to use the
existing intersect_with_reserved() function, we can remove most of the
comparisons and boolean operators while preserving the exact same logic.

This change is only for readability. It does not change the logic itself
at all.

Author: Thomas Hebb <tommyhebb@gmail.com>
Pull-request: #494
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Thomas Hebb 2022-06-26 15:38:40 -07:00 committed by David Sterba
parent 9509b85449
commit 86bd027377

View File

@ -213,9 +213,9 @@ static int create_image_file_range(struct btrfs_trans_handle *trans,
{
struct cache_extent *cache;
struct btrfs_block_group *bg_cache;
const struct simple_range *reserved;
u64 len = *ret_len;
u64 disk_bytenr;
int i;
int ret;
u32 datacsum = convert_flags & CONVERT_FLAG_DATACSUM;
@ -237,9 +237,8 @@ static int create_image_file_range(struct btrfs_trans_handle *trans,
* Or we will insert a hole into current image file, and later
* migrate block will fail as there is already a file extent.
*/
for (i = 0; i < ARRAY_SIZE(btrfs_reserved_ranges); i++) {
const struct simple_range *reserved = &btrfs_reserved_ranges[i];
reserved = intersect_with_reserved(bytenr, len);
if (reserved) {
/*
* |-- reserved --|
* |-- range --|
@ -248,7 +247,7 @@ static int create_image_file_range(struct btrfs_trans_handle *trans,
* |-- range --|
* Skip to reserved range end
*/
if (bytenr >= reserved->start && bytenr < range_end(reserved)) {
if (bytenr >= reserved->start) {
*ret_len = range_end(reserved) - bytenr;
return 0;
}
@ -261,11 +260,7 @@ static int create_image_file_range(struct btrfs_trans_handle *trans,
* |------- range -------|
* Leading part may still create a file extent
*/
if (bytenr < reserved->start &&
bytenr + len > reserved->start) {
len = min_t(u64, len, reserved->start - bytenr);
break;
}
len = min_t(u64, len, reserved->start - bytenr);
}
/* Check if we are going to insert regular file extent, or hole */