Skip to contentSkip to conversation

btrfs: rescue: allow ibadroots to skip bad extent tree when reading block group items

PATCH Superseded Qu Wenruo 2 participants 3 messages Jul 15, 2021 Btrfs file system

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

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
index 5bd76a45037e..33086b882fe8 100644
@@ -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
#1 lore raw
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 -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 +6 -1
index 5bd76a45037e..33086b882fe8 100644
@@ -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 -0
  	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;
  }
 
author
#2 lore raw
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 -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 +6 -1
index 5bd76a45037e..33086b882fe8 100644
@@ -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 -0
       cache_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 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;
  }