Skip to content

Commit 21dbbb1

Browse files
fdmananakdave
authored andcommitted
btrfs: fix clearing of BTRFS_FS_RELOC_RUNNING if relocation already running
When starting relocation, at reloc_chunk_start(), if we happen to find the flag BTRFS_FS_RELOC_RUNNING is already set we return an error (-EINPROGRESS) to the callers, however the callers call reloc_chunk_end() which will clear the flag BTRFS_FS_RELOC_RUNNING, which is wrong since relocation was started by another task and still running. Finding the BTRFS_FS_RELOC_RUNNING flag already set is an unexpected scenario, but still our current behaviour is not correct. Fix this by never calling reloc_chunk_end() if reloc_chunk_start() has returned an error, which is what logically makes sense, since the general widespread pattern is to have end functions called only if the counterpart start functions succeeded. This requires changing reloc_chunk_start() to clear BTRFS_FS_RELOC_RUNNING if there's a pending cancel request. Fixes: 907d271 ("btrfs: add cancellable chunk relocation support") 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 ace33eb commit 21dbbb1

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

fs/btrfs/relocation.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,6 +3780,7 @@ static noinline_for_stack struct inode *create_reloc_inode(
37803780
/*
37813781
* Mark start of chunk relocation that is cancellable. Check if the cancellation
37823782
* has been requested meanwhile and don't start in that case.
3783+
* NOTE: if this returns an error, reloc_chunk_end() must not be called.
37833784
*
37843785
* Return:
37853786
* 0 success
@@ -3796,10 +3797,8 @@ static int reloc_chunk_start(struct btrfs_fs_info *fs_info)
37963797

37973798
if (atomic_read(&fs_info->reloc_cancel_req) > 0) {
37983799
btrfs_info(fs_info, "chunk relocation canceled on start");
3799-
/*
3800-
* On cancel, clear all requests but let the caller mark
3801-
* the end after cleanup operations.
3802-
*/
3800+
/* On cancel, clear all requests. */
3801+
clear_and_wake_up_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags);
38033802
atomic_set(&fs_info->reloc_cancel_req, 0);
38043803
return -ECANCELED;
38053804
}
@@ -3808,9 +3807,11 @@ static int reloc_chunk_start(struct btrfs_fs_info *fs_info)
38083807

38093808
/*
38103809
* Mark end of chunk relocation that is cancellable and wake any waiters.
3810+
* NOTE: call only if a previous call to reloc_chunk_start() succeeded.
38113811
*/
38123812
static void reloc_chunk_end(struct btrfs_fs_info *fs_info)
38133813
{
3814+
ASSERT(test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags));
38143815
/* Requested after start, clear bit first so any waiters can continue */
38153816
if (atomic_read(&fs_info->reloc_cancel_req) > 0)
38163817
btrfs_info(fs_info, "chunk relocation canceled during operation");
@@ -4023,9 +4024,9 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
40234024
if (err && rw)
40244025
btrfs_dec_block_group_ro(rc->block_group);
40254026
iput(rc->data_inode);
4027+
reloc_chunk_end(fs_info);
40264028
out_put_bg:
40274029
btrfs_put_block_group(bg);
4028-
reloc_chunk_end(fs_info);
40294030
free_reloc_control(rc);
40304031
return err;
40314032
}
@@ -4208,8 +4209,8 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
42084209
ret = ret2;
42094210
out_unset:
42104211
unset_reloc_control(rc);
4211-
out_end:
42124212
reloc_chunk_end(fs_info);
4213+
out_end:
42134214
free_reloc_control(rc);
42144215
out:
42154216
free_reloc_roots(&reloc_roots);

0 commit comments

Comments
 (0)