btrfs: rescue: allow ibadroots to skip bad extent tree when reading block group items
PATCH 1/1 btrfs: rescue: allow ibadroots to skip bad extent tree when reading block group items
When extent tree gets corrupted, normally it's not extent tree root, but one toasted tree leaf/node.
In that case, rescue=ibadroots mount option won't help as it can only handle the extent tree root corruption.
This patch will enhance the behavior by:
- Allow fill_dummy_bgs() to ignore -EEXIST error
This means we may have some block group items read from disk, but then hit some error halfway.
- Fallback to fill_dummy_bgs() if any error gets hit in btrfs_read_block_groups()
Of course, this still needs rescue=ibadroots mount option.
With that, rescue=ibadroots can handle extent tree corruption more gracefully and allow a better recover chance.
--- fs/btrfs/block-group.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +17 -3
@@ -2105,11 +2105,16 @@ static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) bg->used = em->len; bg->flags = map->type; ret =btrfs_add_block_group_cache (fs_info, bg); -if (ret) { +/* +* We may have some block groups filled already, thus ignore +* the -EEXIST error. +*/ +if (ret && ret != -EEXIST ) {btrfs_remove_free_space_cache (bg);btrfs_put_block_group (bg);break ; } + ret =0 ;btrfs_update_space_info (fs_info, bg->flags, em->len, em->len,0 ,0 , &space_info); bg->space_info = space_info;
@@ -2139,8 +2144,10 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) key.offset =0 ; key.type =BTRFS_BLOCK_GROUP_ITEM_KEY ; path =btrfs_alloc_path (); -if (!path) -return -ENOMEM ; +if (!path) { + ret = -ENOMEM ; + goto error; + } cache_gen =btrfs_super_cache_generation (info->super_copy); if (btrfs_test_opt(info, SPACE_CACHE) &&
@@ -2212,6 +2219,13 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) ret =check_chunk_block_group_mappings (info); error:btrfs_free_path (path); +/* +* Either we have no extent_root (already implies IGNOREBADROOTS), or +* we hit error and have IGNOREBADROOTS, then we can try to use dummy +* bgs. +*/ +if (!info->extent_root || (ret &&btrfs_test_opt (info,IGNOREBADROOTS ))) + ret =fill_dummy_bgs (info);return ret; }
Signature · 1 line
2.32.0
Quoted 53 lines · Qu Wenruo
When extent tree gets corrupted, normally it's not extent tree root, but one toasted tree leaf/node.
In that case, rescue=ibadroots mount option won't help as it can only handle the extent tree root corruption.
This patch will enhance the behavior by:
- Allow
fill_dummy_bgs()to ignore-EEXISTerrorThis means we may have some block group items read from disk, but then hit some error halfway.
- Fallback to
fill_dummy_bgs()if any error gets hit inbtrfs_read_block_groups()Of course, this still needs rescue=ibadroots mount option.
With that, rescue=ibadroots can handle extent tree corruption more gracefully and allow a better recover chance.
Reported-by Zhenyu Wu <[email protected]>Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/block-group.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)fs/btrfs/block-group.c +6
-1@@ -2105,11 +2105,16 @@ static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) bg->used = em->len; bg->flags = map->type; ret =btrfs_add_block_group_cache (fs_info, bg); -if (ret) { +/* +* We may have some block groups filled already, thus ignore +* the -EEXIST error. +*/ +if (ret && ret != -EEXIST ) {btrfs_remove_free_space_cache (bg);btrfs_put_block_group (bg);break ; } + ret =0 ;btrfs_update_space_info (fs_info, bg->flags, em->len, em->len,0 ,0 , &space_info); bg->space_info = space_info;
fs/btrfs/block-group.c int btrfs_read_block_groups(struct btrfs_fs_info *info) · line 2150
Quoted 9 lines
fs/btrfs/block-group.c +4
-2@@ -2139,8 +2144,10 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) key.offset =0 ; key.type =BTRFS_BLOCK_GROUP_ITEM_KEY ; path =btrfs_alloc_path (); -if (!path) -return -ENOMEM ; +if (!path) { + ret = -ENOMEM ; + goto error; + }
Return -ENOMEM; is correct as alloc failure is not part of the
corruption?
Thanks, Anand
Quoted 17 lines
fs/btrfs/block-group.c +7
-0cache_gen =btrfs_super_cache_generation (info->super_copy); if (btrfs_test_opt(info, SPACE_CACHE) &&@@ -2212,6 +2219,13 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) ret =check_chunk_block_group_mappings (info); error:btrfs_free_path (path); +/* +* Either we have no extent_root (already implies IGNOREBADROOTS), or +* we hit error and have IGNOREBADROOTS, then we can try to use dummy +* bgs. +*/ +if (!info->extent_root || (ret &&btrfs_test_opt (info,IGNOREBADROOTS ))) + ret =fill_dummy_bgs (info);return ret; }
Quoted 54 lines · Anand Jain
Quoted 51 lines · Qu Wenruo
When extent tree gets corrupted, normally it's not extent tree root, but one toasted tree leaf/node.
In that case, rescue=ibadroots mount option won't help as it can only handle the extent tree root corruption.
This patch will enhance the behavior by:
- Allow
fill_dummy_bgs()to ignore-EEXISTerrorThis means we may have some block group items read from disk, but then hit some error halfway.
- Fallback to
fill_dummy_bgs()if any error gets hit inbtrfs_read_block_groups()Of course, this still needs rescue=ibadroots mount option.
With that, rescue=ibadroots can handle extent tree corruption more gracefully and allow a better recover chance.
Reported-by Zhenyu Wu <[email protected]>Signed-off-by Qu Wenruo <[email protected]>---
fs/btrfs/block-group.c| 20 +++++++++++++++++---1 file changed, 17 insertions(+), 3 deletions(-)fs/btrfs/block-group.c +6
-1@@ -2105,11 +2105,16 @@ static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) bg->used = em->len; bg->flags = map->type; ret = btrfs_add_block_group_cache(fs_info, bg); - if (ret) { + /* + * We may have some block groups filled already, thus ignore + * the -EEXIST error. + */ + if (ret && ret != -EEXIST) { btrfs_remove_free_space_cache(bg); btrfs_put_block_group(bg); break; } + ret = 0; btrfs_update_space_info(fs_info, bg->flags, em->len, em->len, 0, 0, &space_info);
bg->space_info=space_info;
Quoted 14 lines
fs/btrfs/block-group.c int btrfs_read_block_groups(struct · line 2151
Quoted 11 lines
fs/btrfs/block-group.c +4
-2@@ -2139,8 +2144,10 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) key.offset = 0; key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; path = btrfs_alloc_path(); - if (!path) - return -ENOMEM; + if (!path) { + ret = -ENOMEM; + goto error; + }Return
-ENOMEM; is correct as alloc failure is not part of the
corruption?
Oh, right. In that case we shouldn't try to fill dummy eb, especially it will also fail with -ENOMEM anyway.
Thanks,
Qu
Quoted 24 lines
Thanks, Anand
Quoted 20 lines
fs/btrfs/block-group.c +0
-0cache_gen = btrfs_super_cache_generation(info->super_copy);if (
btrfs_test_opt(info, SPACE_CACHE)&&fs/btrfs/block-group.c +0
-0@@ -2212,6 +2219,13 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info) ret = check_chunk_block_group_mappings(info);error:
btrfs_free_path(path);
+ /*
+ * Either we have noextent_root(already implies IGNOREBADROOTS), or
+ * we hit error and have IGNOREBADROOTS, then we can try to use dummy
+ * bgs.
+ */
+ if (!info->extent_root || (ret &&btrfs_test_opt(info, IGNOREBADROOTS)))
+ ret =fill_dummy_bgs(info);
return ret;
}