Commit b64119b5 authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba

Btrfs: remove unnecessary condition in btrfs_clone() to avoid too much nesting

The bulk of the work done when cloning extents, at ioctl.c:btrfs_clone(),
is done inside an if statement that checks if the found key has the type
BTRFS_EXTENT_DATA_KEY. That if statement is redundant however, because
btrfs_search_slot() always leaves us in a leaf slot that points to a key
that is always greater then or equals to the search key, and our search
key here has that type, and because we bail out before that if statement
if the key at the given leaf slot is greater then BTRFS_EXTENT_DATA_KEY.

Therefore just remove that if statement, not only because it is useless
but mostly because it increases the nesting/indentation level in this
function which is quite big and makes things a bit awkward whenever I need
to fix something that requires changing btrfs_clone() (and it has been
like that for many years already).
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 559ca6ea
......@@ -3516,6 +3516,14 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
while (1) {
u64 next_key_min_offset = key.offset + 1;
struct btrfs_file_extent_item *extent;
int type;
u32 size;
struct btrfs_key new_key;
u64 disko = 0, diskl = 0;
u64 datao = 0, datal = 0;
u8 comp;
u64 drop_start;
/*
* note the key will change type as we walk through the
......@@ -3556,15 +3564,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
key.objectid != btrfs_ino(BTRFS_I(src)))
break;
if (key.type == BTRFS_EXTENT_DATA_KEY) {
struct btrfs_file_extent_item *extent;
int type;
u32 size;
struct btrfs_key new_key;
u64 disko = 0, diskl = 0;
u64 datao = 0, datal = 0;
u8 comp;
u64 drop_start;
ASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
extent = btrfs_item_ptr(leaf, slot,
struct btrfs_file_extent_item);
......@@ -3572,23 +3572,19 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
type = btrfs_file_extent_type(leaf, extent);
if (type == BTRFS_FILE_EXTENT_REG ||
type == BTRFS_FILE_EXTENT_PREALLOC) {
disko = btrfs_file_extent_disk_bytenr(leaf,
extent);
diskl = btrfs_file_extent_disk_num_bytes(leaf,
extent);
disko = btrfs_file_extent_disk_bytenr(leaf, extent);
diskl = btrfs_file_extent_disk_num_bytes(leaf, extent);
datao = btrfs_file_extent_offset(leaf, extent);
datal = btrfs_file_extent_num_bytes(leaf,
extent);
datal = btrfs_file_extent_num_bytes(leaf, extent);
} else if (type == BTRFS_FILE_EXTENT_INLINE) {
/* take upper bound, may be compressed */
datal = btrfs_file_extent_ram_bytes(leaf,
extent);
/* Take upper bound, may be compressed */
datal = btrfs_file_extent_ram_bytes(leaf, extent);
}
/*
* The first search might have left us at an extent
* item that ends before our target range's start, can
* happen if we have holes and NO_HOLES feature enabled.
* The first search might have left us at an extent item that
* ends before our target range's start, can happen if we have
* holes and NO_HOLES feature enabled.
*/
if (key.offset + datal <= off) {
path->slots[0]++;
......@@ -3598,8 +3594,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
}
next_key_min_offset = key.offset + datal;
size = btrfs_item_size_nr(leaf, slot);
read_extent_buffer(leaf, buf,
btrfs_item_ptr_offset(leaf, slot),
read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, slot),
size);
btrfs_release_path(path);
......@@ -3613,11 +3608,10 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
new_key.offset = destoff;
/*
* Deal with a hole that doesn't have an extent item
* that represents it (NO_HOLES feature enabled).
* This hole is either in the middle of the cloning
* range or at the beginning (fully overlaps it or
* partially overlaps it).
* Deal with a hole that doesn't have an extent item that
* represents it (NO_HOLES feature enabled).
* This hole is either in the middle of the cloning range or at
* the beginning (fully overlaps it or partially overlaps it).
*/
if (new_key.offset != last_dest_end)
drop_start = last_dest_end;
......@@ -3633,11 +3627,11 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
* | ------------- extent ------------- |
*/
/* subtract range b */
/* Subtract range b */
if (key.offset + datal > off + len)
datal = off + len - key.offset;
/* subtract range a */
/* Subtract range a */
if (off > key.offset) {
datao += off - key.offset;
datal -= off - key.offset;
......@@ -3676,12 +3670,10 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
datal -= skip + trim;
/*
* If our extent is inline, we know we will drop
* or adjust at most 1 extent item in the
* destination root.
* If our extent is inline, we know we will drop or
* adjust at most 1 extent item in the destination root.
*
* 1 - adjusting old extent (we may have to
* split it)
* 1 - adjusting old extent (we may have to split it)
* 1 - add new extent
* 1 - inode update
*/
......@@ -3691,16 +3683,12 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
goto out;
}
ret = clone_copy_inline_extent(inode,
trans, path,
&new_key,
drop_start,
datal,
skip, size, buf);
ret = clone_copy_inline_extent(inode, trans, path,
&new_key, drop_start,
datal, skip, size, buf);
if (ret) {
if (ret != -EOPNOTSUPP)
btrfs_abort_transaction(trans,
ret);
btrfs_abort_transaction(trans, ret);
btrfs_end_transaction(trans);
goto out;
}
......@@ -3710,15 +3698,13 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
last_dest_end = ALIGN(new_key.offset + datal,
fs_info->sectorsize);
ret = clone_finish_inode_update(trans, inode,
last_dest_end,
destoff, olen,
no_time_update);
ret = clone_finish_inode_update(trans, inode, last_dest_end,
destoff, olen, no_time_update);
if (ret)
goto out;
if (new_key.offset + datal >= destoff + len)
break;
}
btrfs_release_path(path);
key.offset = next_key_min_offset;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment