Skip to contentSkip to conversation

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

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

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH v3 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 v3:
- Remove a dead condition
The empty fs_info->extent_root case has already been handled.

---
 fs/btrfs/block-group.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +14 -1
index 5bd76a45037e..9bc68515bc4a 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,14 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
 	ret = check_chunk_block_group_mappings(info);
 error:
 	btrfs_free_path(path);
+	/*
+	 * We hit some error reading the extent tree, and have rescue=ibadroots
+	 * mount option.
+	 * Try to fill using dummy block groups so that the user can continue
+	 * to mount and grab their data.
+	 */
+	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
+		ret = fill_dummy_bgs(info);
 	return ret;
 }
 
Signature · 1 line
2.32.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 16 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()

Quoted 8 lines

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.

Thanks, Anand

Quoted 49 lines

---
Changelog:
v2:
- Don't try to fill with dummy block groups when we hit ENOMEM v3:
- Remove a dead condition
The empty fs_info->extent_root case has already been handled.

---
  fs/btrfs/block-group.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +14 -1
index 5bd76a45037e..9bc68515bc4a 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,14 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
  	ret = check_chunk_block_group_mappings(info);
  error:
  	btrfs_free_path(path);
+	/*
+	 * We hit some error reading the extent tree, and have rescue=ibadroots
+	 * mount option.
+	 * Try to fill using dummy block groups so that the user can continue
+	 * to mount and grab their data.
+	 */
+	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
+		ret = fill_dummy_bgs(info);
  	return ret;
  }
 
#2 lore raw
In reply to Qu Wenruo
Quoted 50 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 v3:
- Remove a dead condition
The empty fs_info->extent_root case has already been handled.

---
 fs/btrfs/block-group.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +2 -1
index 5bd76a45037e..9bc68515bc4a 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

fs/btrfs/block-group.c static int fill_dummy_bgs(struct btrfs_fs_info *fs_info) · line 2116

Quoted 7 lines
fs/btrfs/block-group.c +3 -0
+		 * the -EEXIST error.
+		 */
+		if (ret && ret != -EEXIST) {
 			btrfs_remove_free_space_cache(bg);
 			btrfs_put_block_group(bg);
 			break;
 		}

So we continue to link_block_group() bellow even -EEXIST. The new allocated bg will be inserted into &space_info->block_groups[index]. Then while calling close_ctree(), it only frees bgs not allocated by fill_dummy_bgs(). The bgs still exist in &space_info->block_groups[index]. Memory leaks!

--
Su

Quoted 22 lines
fs/btrfs/block-group.c +3 -0
+		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,14 @@ int btrfs_read_block_groups(struct 
 btrfs_fs_info *info)
 	ret = check_chunk_block_group_mappings(info);
 error:
 	btrfs_free_path(path);
+	/*
+	 * We hit some error reading the extent tree, and have 

rescue=ibadroots

fs/btrfs/block-group.c +2 -0
+	 * mount option.
+	 * Try to fill using dummy block groups so that the user can 

continue

fs/btrfs/block-group.c +4 -0
+	 * to mount and grab their data.
+	 */
+	if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
+		ret = fill_dummy_bgs(info);
 	return ret;
 }
#3 lore raw
Quoted 55 lines · Su Yue
Quoted 52 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 v3:
- Remove a dead condition
  The empty fs_info->extent_root case has already been handled.
---
 fs/btrfs/block-group.c | 15 ++++++++++++++-

 1 file changed, 14 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +5 -1
index 5bd76a45037e..9bc68515bc4a 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);
Quoted 8 lines

fs/btrfs/block-group.c static int fill_dummy_bgs(struct btrfs_fs_info · line 2118

Quoted 3 lines
fs/btrfs/block-group.c +0 -0
              break;
          }
 

So we continue to link_block_group() bellow even -EEXIST. The new allocated bg will be inserted into &space_info->block_groups[index]. Then while calling close_ctree(), it only frees bgs not allocated by fill_dummy_bgs(). The bgs still exist in &space_info->block_groups[index]. Memory leaks!

Right, when -EEXIST is hit, we should skip to next chunk, not continuing the remaining works.

Thanks,
Qu

Quoted 25 lines

--
Su

Quoted 21 lines
fs/btrfs/block-group.c +1 -0
+        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,14 @@ int btrfs_read_block_groups(struct
 btrfs_fs_info *info)
      ret = check_chunk_block_group_mappings(info);

 error:
     btrfs_free_path(path);
+    /*
+     * We hit some error reading the extent tree, and have rescue=ibadroots
+     * mount option.
+     * Try to fill using dummy block groups so that the user can continue
+     * to mount and grab their data.
+     */
+    if (ret && btrfs_test_opt(info, IGNOREBADROOTS))
+        ret = fill_dummy_bgs(info);
     return ret;
 }