[PATCH v2 2/2] btrfs: zoned: split partially truncated ordered extents at endio
From: Ziheng Wei
Date: Sat Dec 05 2026 - 02:18:55 EST
In btrfs_finish_ordered_zoned(), the early return for
BTRFS_ORDERED_TRUNCATED only handles fully truncated ordered extents
(ordered->truncated_len == 0). When an ordered extent is partially
truncated (0 < ordered->truncated_len < ordered->disk_num_bytes),
sectors at or beyond i_size are marked finished without submitting bios
or adding btrfs_ordered_sum entries to ordered->csum_list.
The loop then compares the accumulated csum length against
ordered->disk_num_bytes and advances sum with list_next_entry(sum, list)
without checking whether sum is already the last entry on
ordered->csum_list. This walks onto the list head inside struct
btrfs_ordered_extent, reading garbage logical addresses and triggering
WARN_ON_ONCE(!block_group) in btrfs_zone_finish_endio() as well as
"Unable to find block group" errors.
Simply stopping the loop at list_is_last() and falling through to
btrfs_rewrite_logical_zoned() is also insufficient because it leaves
ordered->disk_num_bytes larger than the zone-append bytes actually
written, breaking zoned accounting and causing hangs in testing.
Stop advancing past the last entry on ordered->csum_list, and when
len < ordered->disk_num_bytes split off the written len bytes via
btrfs_zoned_split_ordered() so only [logical, logical + len) is
registered while the unwritten tail (truncated_len == 0) is dropped and
freed in btrfs_finish_one_ordered(). Compressed ordered extents are
submitted whole so their csum_list always covers disk_num_bytes and
never enters this split path.
Also move nodatasum dummy-csum freeing into a helper called from
btrfs_zoned_split_ordered() before btrfs_finish_one_ordered(new), which
avoids passing dummy csums into add_pending_csums() on both the new
partial-truncation split and existing non-contiguous splits.
Found by LLM-assisted static analysis; reproduced and verified in
QEMU (KASAN + LOCKDEP + BTRFS_DEBUG + BTRFS_ASSERT) on a zoned null_blk
device across default, commit=1, nodatasum, and qgroups mounts followed
by btrfs check --check-data-csum.
Fixes: 71df088c1cc0 ("btrfs: defer splitting of ordered extents until I/O completion")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
v1 -> v2:
- Split off the written len bytes via btrfs_zoned_split_ordered() when
exiting the loop with len < ordered->disk_num_bytes, instead of
falling through with the oversized pre-truncation disk_num_bytes
(flagged in v1 review and confirmed to hang in QEMU testing).
- Move nodatasum dummy-csum cleanup into btrfs_zoned_split_ordered() so
split-off ordered extents free dummy csums before finishing.
- Added patch 1/2 to fix BTRFS_ORDERED_TRUNCATED handling in
btrfs_split_ordered_extent() (found when testing the reproducer).
- v1: https://lore.kernel.org/r/20261204031023.1758536-1-zihengw03@xxxxxxx
fs/btrfs/zoned.c | 50 +++++++++++++++++++++++++++++++++---------------
1 file changed, 35 insertions(+), 15 deletions(-)
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index bdadc704c..3bc090cf3 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2111,6 +2111,28 @@ static void btrfs_rewrite_logical_zoned(struct btrfs_ordered_extent *ordered,
write_unlock(&em_tree->lock);
}
+static void btrfs_zoned_free_dummy_csums(struct btrfs_ordered_extent *ordered)
+{
+ struct btrfs_inode *inode = ordered->inode;
+ struct btrfs_fs_info *fs_info = inode->root->fs_info;
+ struct btrfs_ordered_sum *sum;
+
+ /*
+ * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
+ * were allocated by btrfs_alloc_dummy_sum only to record the logical
+ * addresses and don't contain actual checksums. We thus must free them
+ * here so that we don't attempt to log the csums later.
+ */
+ if ((inode->flags & BTRFS_INODE_NODATASUM) ||
+ test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state)) {
+ while ((sum = list_first_entry_or_null(&ordered->csum_list,
+ typeof(*sum), list))) {
+ list_del(&sum->list);
+ kfree(sum);
+ }
+ }
+}
+
static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
u64 logical, u64 len)
{
@@ -2125,6 +2147,7 @@ static bool btrfs_zoned_split_ordered(struct btrfs_ordered_extent *ordered,
if (IS_ERR(new))
return false;
new->disk_bytenr = logical;
+ btrfs_zoned_free_dummy_csums(new);
btrfs_finish_one_ordered(new);
return true;
}
@@ -2158,7 +2181,8 @@ void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
logical = sum->logical;
len = sum->len;
- while (len < ordered->disk_num_bytes) {
+ while (len < ordered->disk_num_bytes &&
+ !list_is_last(&sum->list, &ordered->csum_list)) {
sum = list_next_entry(sum, list);
if (sum->logical == logical + len) {
len += sum->len;
@@ -2173,24 +2197,20 @@ void btrfs_finish_ordered_zoned(struct btrfs_ordered_extent *ordered)
len = sum->len;
}
+ if (len < ordered->disk_num_bytes) {
+ if (!btrfs_zoned_split_ordered(ordered, logical, len)) {
+ btrfs_mark_ordered_extent_error(ordered);
+ btrfs_err(fs_info,
+ "failed to split partially truncated ordered extent");
+ }
+ goto out;
+ }
+
if (ordered->disk_bytenr != logical)
btrfs_rewrite_logical_zoned(ordered, logical);
out:
- /*
- * If we end up here for nodatasum I/O, the btrfs_ordered_sum structures
- * were allocated by btrfs_alloc_dummy_sum only to record the logical
- * addresses and don't contain actual checksums. We thus must free them
- * here so that we don't attempt to log the csums later.
- */
- if ((inode->flags & BTRFS_INODE_NODATASUM) ||
- test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state)) {
- while ((sum = list_first_entry_or_null(&ordered->csum_list,
- typeof(*sum), list))) {
- list_del(&sum->list);
- kfree(sum);
- }
- }
+ btrfs_zoned_free_dummy_csums(ordered);
}
static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
--
2.43.0