Skip to content

Commit e4d82f1

Browse files
fdmananakdave
authored andcommitted
btrfs: use single return value variable in btrfs_relocate_block_group()
We are using 'ret' and 'err' variables to track return values and errors, which is pattern that is error prone and we had quite some bugs due to this pattern in the past. Simplify this and use a single variable, named 'ret', to track errors and the return value. Also rename the variable 'rw' to 'bg_is_ro' which is more meaningful name, and change its type from int to bool. Reviewed-by: Boris Burkov <[email protected]> Reviewed-by: Johannes Thumshirn <[email protected]> Reviewed-by: Qu Wenruo <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 21dbbb1 commit e4d82f1

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

fs/btrfs/relocation.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,8 +3882,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
38823882
struct inode *inode;
38833883
struct btrfs_path *path;
38843884
int ret;
3885-
int rw = 0;
3886-
int err = 0;
3885+
bool bg_is_ro = false;
38873886

38883887
/*
38893888
* This only gets set if we had a half-deleted snapshot on mount. We
@@ -3925,24 +3924,20 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
39253924
}
39263925

39273926
ret = reloc_chunk_start(fs_info);
3928-
if (ret < 0) {
3929-
err = ret;
3927+
if (ret < 0)
39303928
goto out_put_bg;
3931-
}
39323929

39333930
rc->extent_root = extent_root;
39343931
rc->block_group = bg;
39353932

39363933
ret = btrfs_inc_block_group_ro(rc->block_group, true);
3937-
if (ret) {
3938-
err = ret;
3934+
if (ret)
39393935
goto out;
3940-
}
3941-
rw = 1;
3936+
bg_is_ro = true;
39423937

39433938
path = btrfs_alloc_path();
39443939
if (!path) {
3945-
err = -ENOMEM;
3940+
ret = -ENOMEM;
39463941
goto out;
39473942
}
39483943

@@ -3954,14 +3949,12 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
39543949
else
39553950
ret = PTR_ERR(inode);
39563951

3957-
if (ret && ret != -ENOENT) {
3958-
err = ret;
3952+
if (ret && ret != -ENOENT)
39593953
goto out;
3960-
}
39613954

39623955
rc->data_inode = create_reloc_inode(rc->block_group);
39633956
if (IS_ERR(rc->data_inode)) {
3964-
err = PTR_ERR(rc->data_inode);
3957+
ret = PTR_ERR(rc->data_inode);
39653958
rc->data_inode = NULL;
39663959
goto out;
39673960
}
@@ -3982,8 +3975,6 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
39823975
mutex_lock(&fs_info->cleaner_mutex);
39833976
ret = relocate_block_group(rc);
39843977
mutex_unlock(&fs_info->cleaner_mutex);
3985-
if (ret < 0)
3986-
err = ret;
39873978

39883979
finishes_stage = rc->stage;
39893980
/*
@@ -3996,16 +3987,18 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
39963987
* out of the loop if we hit an error.
39973988
*/
39983989
if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
3999-
ret = btrfs_wait_ordered_range(BTRFS_I(rc->data_inode), 0,
4000-
(u64)-1);
4001-
if (ret)
4002-
err = ret;
3990+
int wb_ret;
3991+
3992+
wb_ret = btrfs_wait_ordered_range(BTRFS_I(rc->data_inode), 0,
3993+
(u64)-1);
3994+
if (wb_ret && ret == 0)
3995+
ret = wb_ret;
40033996
invalidate_mapping_pages(rc->data_inode->i_mapping,
40043997
0, -1);
40053998
rc->stage = UPDATE_DATA_PTRS;
40063999
}
40074000

4008-
if (err < 0)
4001+
if (ret < 0)
40094002
goto out;
40104003

40114004
if (rc->extents_found == 0)
@@ -4021,14 +4014,14 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
40214014
WARN_ON(rc->block_group->reserved > 0);
40224015
WARN_ON(rc->block_group->used > 0);
40234016
out:
4024-
if (err && rw)
4017+
if (ret && bg_is_ro)
40254018
btrfs_dec_block_group_ro(rc->block_group);
40264019
iput(rc->data_inode);
40274020
reloc_chunk_end(fs_info);
40284021
out_put_bg:
40294022
btrfs_put_block_group(bg);
40304023
free_reloc_control(rc);
4031-
return err;
4024+
return ret;
40324025
}
40334026

40344027
static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)

0 commit comments

Comments
 (0)