Skip to contentSkip to conversation

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

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

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH v2 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.

---
Changelog:
v2:
- Don't try to fill with dummy block groups when we hit ENOMEM

---
 fs/btrfs/block-group.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +13 -1
index 5bd76a45037e..240e6ec8bc31 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;
@@ -2212,6 +2217,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 58 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.

---
Changelog:
v2:
- Don't try to fill with dummy block groups when we hit ENOMEM

---
  fs/btrfs/block-group.c | 14 +++++++++++++-
  1 file changed, 13 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +6 -1
index 5bd76a45037e..240e6ec8bc31 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 2225

Quoted 9 lines
fs/btrfs/block-group.c +6 -0
@@ -2212,6 +2217,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)))

I missed this part before, my bad.

info->extent_root can not be NULL here, which is checked at the
beginning of the function.

2138 if (!info->extent_root)
2139 return fill_dummy_bgs(info);

So should be

       if (ret && btrfs_test_opt(info, IGNOREBADROOTS)) {
		info->extent_root = NULL;
		ret = fill_dummy_bgs(info);
	}

Thanks, Anand

Quoted 3 lines
fs/btrfs/block-group.c +1 -0
+		ret = fill_dummy_bgs(info);
  	return ret;
  }
#2 lore raw
Quoted 77 lines · Anand Jain
Quoted 64 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.

---
Changelog:
v2:
- Don't try to fill with dummy block groups when we hit ENOMEM
---
  fs/btrfs/block-group.c | 14 +++++++++++++-

  1 file changed, 13 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +6 -1
index 5bd76a45037e..240e6ec8bc31 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 +0 -0
@@ -2212,6 +2217,13 @@ int btrfs_read_block_groups(struct
 btrfs_fs_info *info)
       ret = check_chunk_block_group_mappings(info);

  error:
      btrfs_free_path(path);
+    /*

Quoted 8 lines

+     * 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)))

  I missed this part before, my bad.

Quoted 5 lines

 info->extent_root can not be NULL here, which is checked at the
beginning of the function.

2138         if (!info->extent_root)
2139                 return fill_dummy_bgs(info);

Oh, you're right.

Quoted 4 lines

   So should be
       if (ret && btrfs_test_opt(info, IGNOREBADROOTS)) { info->extent_root = NULL;

But this is not needed.

We can have extent_root if only some extent tree nodes/leaves get corrupted.

Thanks,
Qu

Quoted 12 lines

        ret = fill_dummy_bgs(info);
    }

Thanks, Anand

Quoted 3 lines

+        ret = fill_dummy_bgs(info);
      return ret;
  }

#3 lore raw
Quoted 89 lines · Qu Wenruo
Quoted 77 lines · Anand Jain
Quoted 64 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.

---
Changelog:
v2:
- Don't try to fill with dummy block groups when we hit ENOMEM
---
  fs/btrfs/block-group.c | 14 +++++++++++++-

  1 file changed, 13 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +6 -1
index 5bd76a45037e..240e6ec8bc31 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 +0 -0
@@ -2212,6 +2217,13 @@ int btrfs_read_block_groups(struct
 btrfs_fs_info *info)
       ret = check_chunk_block_group_mappings(info);

  error:
      btrfs_free_path(path);
+    /*

Quoted 8 lines

+     * 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)))

   I missed this part before, my bad.

Quoted 5 lines

  info->extent_root can not be NULL here, which is checked at the beginning of the function.

2138         if (!info->extent_root)
2139                 return fill_dummy_bgs(info);

Oh, you're right.

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

    So should be
        if (ret && btrfs_test_opt(info, IGNOREBADROOTS)) { info->extent_root = NULL;

But this is not needed.

We can have extent_root if only some extent tree nodes/leaves get corrupted.

In fact, if here we manually set extent_root to NULL, we will leak some extent buffers.

Thanks,
Qu

Quoted 16 lines

Thanks,
Qu

Quoted 12 lines

         ret = fill_dummy_bgs(info);
     }

Thanks, Anand

Quoted 3 lines

+        ret = fill_dummy_bgs(info);
      return ret;
  }

#4 lore raw
Quoted 96 lines · Qu Wenruo
Quoted 93 lines · Qu Wenruo
Quoted 77 lines · Anand Jain
Quoted 64 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.

---
Changelog:
v2:
- Don't try to fill with dummy block groups when we hit ENOMEM
---
  fs/btrfs/block-group.c | 14 +++++++++++++-

  1 file changed, 13 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +6 -1
index 5bd76a45037e..240e6ec8bc31 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 +0 -0
@@ -2212,6 +2217,13 @@ int btrfs_read_block_groups(struct
 btrfs_fs_info *info)
       ret = check_chunk_block_group_mappings(info);

  error:
      btrfs_free_path(path);
+    /*

Quoted 8 lines

+     * 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)))

   I missed this part before, my bad.

Quoted 5 lines

  info->extent_root can not be NULL here, which is checked at the beginning of the function.

2138         if (!info->extent_root)
2139                 return fill_dummy_bgs(info);

Oh, you're right.

Quoted 4 lines

    So should be
        if (ret && btrfs_test_opt(info, IGNOREBADROOTS)) { info->extent_root = NULL;

Quoted 7 lines
Quoted 4 lines

But this is not needed.

We can have extent_root if only some extent tree nodes/leaves get corrupted.

In fact, if here we manually set extent_root to NULL, we will leak some extent buffers.

Right. After I replied, I thought so. Thanks for verifying.

-Anand

Quoted 19 lines

Thanks,
Qu

Quoted 16 lines

Thanks,
Qu

Quoted 12 lines

         ret = fill_dummy_bgs(info);
     }

Thanks, Anand

Quoted 3 lines

+        ret = fill_dummy_bgs(info);
      return ret;
  }