Skip to content

Commit 020d5b7

Browse files
fdmananamasoncl
authored andcommitted
Btrfs: fix race between scrub and block group deletion
Scrub can race with the cleaner kthread deleting block groups that are unused (and with relocation too) leading to a failure with error -EINVAL that gets returned to user space. The following diagram illustrates how it happens: CPU 1 CPU 2 cleaner kthread btrfs_delete_unused_bgs() gets block group X from fs_info->unused_bgs sets block group to RO btrfs_remove_chunk(bg X) deletes device extents scrub_enumerate_chunks() searches device tree using its commit root finds device extent for block group X gets block group X from the tree fs_info->block_group_cache_tree (via btrfs_lookup_block_group()) sets bg X to RO (again) btrfs_remove_block_group(bg X) deletes block group from fs_info->block_group_cache_tree removes extent map from fs_info->mapping_tree scrub_chunk(offset X) searches fs_info->mapping_tree for extent map starting at offset X --> doesn't find any such extent map --> returns -EINVAL and scrub errors out to userspace with -EINVAL Fix this by dealing with an extent map lookup failure as an indicator of block group deletion. Issue reproduced with fstest btrfs/071. Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent 31388ab commit 020d5b7

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

fs/btrfs/scrub.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,7 +3432,9 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
34323432
static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
34333433
struct btrfs_device *scrub_dev,
34343434
u64 chunk_offset, u64 length,
3435-
u64 dev_offset, int is_dev_replace)
3435+
u64 dev_offset,
3436+
struct btrfs_block_group_cache *cache,
3437+
int is_dev_replace)
34363438
{
34373439
struct btrfs_mapping_tree *map_tree =
34383440
&sctx->dev_root->fs_info->mapping_tree;
@@ -3445,8 +3447,18 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
34453447
em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
34463448
read_unlock(&map_tree->map_tree.lock);
34473449

3448-
if (!em)
3449-
return -EINVAL;
3450+
if (!em) {
3451+
/*
3452+
* Might have been an unused block group deleted by the cleaner
3453+
* kthread or relocation.
3454+
*/
3455+
spin_lock(&cache->lock);
3456+
if (!cache->removed)
3457+
ret = -EINVAL;
3458+
spin_unlock(&cache->lock);
3459+
3460+
return ret;
3461+
}
34503462

34513463
map = (struct map_lookup *)em->bdev;
34523464
if (em->start != chunk_offset)
@@ -3592,7 +3604,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
35923604
dev_replace->cursor_left = found_key.offset;
35933605
dev_replace->item_needs_writeback = 1;
35943606
ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length,
3595-
found_key.offset, is_dev_replace);
3607+
found_key.offset, cache, is_dev_replace);
35963608

35973609
/*
35983610
* flush, submit all pending read and write bios, afterwards

0 commit comments

Comments
 (0)