[PATCH v2 1/2] btrfs: clear BTRFS_ORDERED_TRUNCATED on non-truncated split head

From: Ziheng Wei

Date: Sat Dec 05 2026 - 02:23:33 EST


btrfs_split_ordered_extent() passes ordered->flags to
alloc_ordered_extent(), so the split-off head ordered extent inherits
BTRFS_ORDERED_TRUNCATED with new->truncated_len initialized to (u64)-1.

When ordered->truncated_len > len, the split-off head [file_offset,
file_offset + len) is completely before the truncation offset, yet
BTRFS_ORDERED_TRUNCATED was left set on new with new->truncated_len
still equal to (u64)-1.

When ordered->truncated_len == len, the else branch set
new->truncated_len = len while keeping BTRFS_ORDERED_TRUNCATED set.
btrfs_finish_one_ordered(new) then computed unwritten_start =
start + len = end + 1 > end and hit WARN_ON(end < start) in
btrfs_drop_extent_map_range().

Clear BTRFS_ORDERED_TRUNCATED on new and subtract len from
ordered->truncated_len whenever ordered->truncated_len >= len, so only
a genuinely truncated split head keeps BTRFS_ORDERED_TRUNCATED set.

Found while testing a zoned partial-truncation reproducer in QEMU+KASAN.

Fixes: 52b1fdca23ac ("btrfs: handle completed ordered extents in btrfs_split_ordered_extent")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
---
fs/btrfs/ordered-data.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index df74c75d6..1df12fee7 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -1309,7 +1309,8 @@ struct btrfs_ordered_extent *btrfs_split_ordered_extent(
}

if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags)) {
- if (ordered->truncated_len > len) {
+ if (ordered->truncated_len >= len) {
+ clear_bit(BTRFS_ORDERED_TRUNCATED, &new->flags);
ordered->truncated_len -= len;
} else {
new->truncated_len = ordered->truncated_len;
--
2.43.0