Skip to contentSkip to conversation

btrfs: add check in remove_range_from_remap_tree() for a NULL block group

PATCH In review Mark Harmstone 2 participants 2 messages Feb 19 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: add check in remove_range_from_remap_tree() for a NULL block group

Add a check in remove_range_from_remap_tree() after we call btrfs_lookup_block_group(), to check if it is NULL. This shouldn't happen, but if it does we at least get an error rather than a segfault.

This fixes a bug introduced in the patch "btrfs: handle deletions from remapped block group" in for-next.

---
 fs/btrfs/relocation.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/relocation.c +3 -0
index f2abc5d625c1..679e551707f5 100644
@@ -6003,6 +6003,9 @@ static int remove_range_from_remap_tree(struct btrfs_trans_handle *trans,
 		struct btrfs_block_group *dest_bg;
 
 		dest_bg = btrfs_lookup_block_group(fs_info, new_addr);
+		if (!dest_bg)
+			return -EUCLEAN;
+
 		adjust_block_group_remap_bytes(trans, dest_bg, -overlap_length);
 		btrfs_put_block_group(dest_bg);
 		ret = btrfs_add_to_free_space_tree(trans,
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Mark Harmstone

Add a check in remove_range_from_remap_tree() after we call btrfs_lookup_block_group(), to check if it is NULL. This shouldn't happen, but if it does we at least get an error rather than a segfault.

This fixes a bug introduced in the patch "btrfs: handle deletions from remapped block group" in for-next.

Same comment as for the other patch, that change is already in Linus' tree, so we can and should add here a:

Fixes 979e1dc3d69e ("btrfs: handle deletions from remapped block group")
Quoted 13 lines
---
 fs/btrfs/relocation.c | 3 +++
 1 file changed, 3 insertions(+)
fs/btrfs/relocation.c +0 -0
index f2abc5d625c1..679e551707f5 100644

fs/btrfs/relocation.c static int remove_range_from_remap_tree(struct btrfs_trans_handle *trans, · line 6007

Quoted 5 lines
fs/btrfs/relocation.c +2 -0
@@ -6003,6 +6003,9 @@ static int remove_range_from_remap_tree(struct btrfs_trans_handle *trans,
                struct btrfs_block_group *dest_bg;
 
                dest_bg = btrfs_lookup_block_group(fs_info, new_addr);
+               if (!dest_bg)
+                       return -EUCLEAN;

Since this is a EUCLEAN and highly unexpected, we can use unlikely here, see: https://btrfs.readthedocs.io/en/latest/dev/Development-notes.html

With those two changes:

Thanks.

Quoted 8 lines
fs/btrfs/relocation.c +1 -1
+
                adjust_block_group_remap_bytes(trans, dest_bg, -overlap_length);
                btrfs_put_block_group(dest_bg);
                ret = btrfs_add_to_free_space_tree(trans,
--

2.52.0