Skip to contentSkip to conversation

btrfs: subpage: reject tree blocks which are not node size aligned

PATCH In review Qu Wenruo 2 participants 2 messages Apr 23, 2025 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: subpage: reject tree blocks which are not node size aligned

When btrfs subpage support (fs block < page size) is introduced, a subpage btrfs will only reject tree blocks which cross page boundaries.

This used to be a compromise to simplify the tree block handling and still allows subpage cases to read some old converted btrfses which do not have proper chunk alignment done.

But in practice, if we have the following unaligned tree block on a 64K page sized system:

  0                           32K           44K             60K  64K
  |                                         |///////////////|    |

Although btrfs has no problem reading the tree block at [44K, 60K), if extent allocator is allocating another tree block, it may choose the range [60K, 74K), as extent allocator has no awareness if it's a subpage metadata request.

Then we got -EINVAL from the following sequence:

btrfs_alloc_tree_block()

 |- btrfs_reserve_extent()
 |  Which returned range [60K, 74K)
 |- btrfs_init_new_buffer()

|- btrfs_find_create_tree_block()
|- alloc_extent_buffer()
|- check_eb_alignment()
Which returned -EINVAL, because the range crosses the page boundary.

This situation will not solve itself and should mostly mark the fs read-only.

Thankfully we didn't really get such reports in the real world because:

- The original unaligned tree block is only caused by older btrfs-convert
It's before the btrfs-convert rework done in v4.6, where converted btrfs can have metadata block groups which are not aligned to node size nor stripe size (64K).

But after btrfs-progs v4.6, all chunks allocated will be stripe (64K) aligned, thus no more such problem.

Considering how old the fix is (v4.x), meanwhile subpage support for btrfs is only introduced in v5.15, it should be safe to reject those unaligned tree blocks.

---
 fs/btrfs/extent_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +2 -2
index 152bf042eb0f..6f2c468cf741 100644
@@ -3070,9 +3070,9 @@ static bool check_eb_alignment(struct btrfs_fs_info *fs_info, u64 start)
 	}
 
 	if (fs_info->nodesize < PAGE_SIZE &&
-	    offset_in_page(start) + fs_info->nodesize > PAGE_SIZE) {
+	    !IS_ALIGNED(start, fs_info->nodesize)) {
 		btrfs_err(fs_info,
-		"tree block crosses page boundary, start %llu nodesize %u",
+		"tree block is not node size aligned, start %llu nodesize %u",
 			  start, fs_info->nodesize);
 		return true;
 	}
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 41 lines · Qu Wenruo

When btrfs subpage support (fs block < page size) is introduced, a subpage btrfs will only reject tree blocks which cross page boundaries.

This used to be a compromise to simplify the tree block handling and still allows subpage cases to read some old converted btrfses which do not have proper chunk alignment done.

But in practice, if we have the following unaligned tree block on a 64K page sized system:

  0                           32K           44K             60K  64K
  |                                         |///////////////|    |

Although btrfs has no problem reading the tree block at [44K, 60K), if extent allocator is allocating another tree block, it may choose the range [60K, 74K), as extent allocator has no awareness if it's a subpage metadata request.

Then we got -EINVAL from the following sequence:

btrfs_alloc_tree_block()

 |- btrfs_reserve_extent()
 |  Which returned range [60K, 74K)
 |- btrfs_init_new_buffer()

|- btrfs_find_create_tree_block()
|- alloc_extent_buffer()
|- check_eb_alignment()
Which returned -EINVAL, because the range crosses the page boundary.

This situation will not solve itself and should mostly mark the fs read-only.

Thankfully we didn't really get such reports in the real world because:

- The original unaligned tree block is only caused by older btrfs-convert
It's before the btrfs-convert rework done in v4.6, where converted btrfs can have metadata block groups which are not aligned to node size nor stripe size (64K).

Quoted 6 lines

But after btrfs-progs v4.6, all chunks allocated will be stripe (64K) aligned, thus no more such problem.

Considering how old the fix is (v4.x), meanwhile subpage support for btrfs is only introduced in v5.15, it should be safe to reject those unaligned tree blocks.

I hope so, 4.6 was almost 10 years ago.

Quoted 1 line