[PATCH v2 0/2] btrfs: zoned: fix partial truncation in btrfs_finish_ordered_zoned()
From: Ziheng Wei
Date: Sat Dec 05 2026 - 02:18:45 EST
On Fri, Dec 04, 2026 at 02:20:49PM +0000, Johannes Thumshirn wrote:
> This is quite a tricky part. As you already used an LLM to do this, can
> you write a reproducer to test your fix?
Thank you for pushing for a reproducer -- running it in QEMU
(KASAN + LOCKDEP + BTRFS_DEBUG + BTRFS_ASSERT) on a zoned null_blk
device turned out to be essential:
1. It reproduced the v1 bug within < 0.1s on unpatched v7.4-rc2
(97d73c825122): WARN_ON_ONCE(!block_group) in
btrfs_zone_finish_endio() and repeated
"Unable to find block group for 3" / "for 4" errors from reading past
the end of ordered->csum_list.
2. It empirically confirmed that v1 (stopping at list_is_last() without
splitting the written prefix) hangs by leaving
ordered->disk_num_bytes oversized, as noted in v1 review.
3. After switching to splitting the written prefix with
btrfs_zoned_split_ordered(), the reproducer caught a second bug in
btrfs_split_ordered_extent() (patch 1/2): when
ordered->truncated_len >= len, BTRFS_ORDERED_TRUNCATED was left set
on the non-truncated head, triggering WARN_ON(end < start) in
btrfs_drop_extent_map_range() (when == len) and leaving
new->truncated_len == (u64)-1 (when > len).
Reproducer
==========
Kernel cmdline (32 x 32 MiB sequential zones on null_blk):
null_blk.nr_devices=1 null_blk.zoned=1 null_blk.zone_size=32 \
null_blk.zone_nr_conv=0 null_blk.gb=1 null_blk.memory_backed=1 \
null_blk.bs=4096
Setup (-o commit=1 only shortens the default 30s commit interval so
active zones finish quickly; default mount also reproduces on unpatched
and passes on v2):
mkfs.btrfs -f -O zoned -m single -d single /dev/nullb0
mount -o commit=1 /dev/nullb0 /mnt
gcc -O2 -Wall -Wextra -pthread -o repro repro.c
./repro /mnt/testfile
umount /mnt
btrfs check --check-data-csum /dev/nullb0
repro.c:
#define _GNU_SOURCE
#include <fcntl.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
static int g_fd;
static volatile char *g_map;
static atomic_int g_stop;
static __thread sigjmp_buf g_jmp;
static void sigbus_handler(int sig)
{
(void)sig;
siglongjmp(g_jmp, 1);
}
static void *mmap_writer(void *arg)
{
volatile unsigned char v = 1;
(void)arg;
while (!atomic_load(&g_stop)) {
if (sigsetjmp(g_jmp, 1) == 0) {
for (int i = 0; i < 16; i++)
g_map[i * 4096] = v;
v++;
}
}
return NULL;
}
static void *flusher(void *arg)
{
(void)arg;
while (!atomic_load(&g_stop))
sync_file_range(g_fd, 0, 65536, SYNC_FILE_RANGE_WRITE);
return NULL;
}
static void *truncator(void *arg)
{
char buf[65536];
(void)arg;
memset(buf, 0x5a, sizeof(buf));
while (!atomic_load(&g_stop)) {
if (pwrite(g_fd, buf, sizeof(buf), 0) < 0)
continue;
if (ftruncate(g_fd, 4096) < 0)
continue;
}
return NULL;
}
int main(int argc, char **argv)
{
const char *path = argc > 1 ? argv[1] : "/mnt/testfile";
struct sigaction sa = { .sa_handler = sigbus_handler };
pthread_t t1, t2, t3, t4;
sigaction(SIGBUS, &sa, NULL);
g_fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
if (g_fd < 0 || ftruncate(g_fd, 65536) < 0) {
perror("open/ftruncate");
return 1;
}
g_map = mmap(NULL, 65536, PROT_READ | PROT_WRITE, MAP_SHARED,
g_fd, 0);
if (g_map == MAP_FAILED) {
perror("mmap");
return 1;
}
pthread_create(&t1, NULL, truncator, NULL);
pthread_create(&t2, NULL, mmap_writer, NULL);
pthread_create(&t3, NULL, flusher, NULL);
pthread_create(&t4, NULL, flusher, NULL);
sleep(8); /* adjust duration as needed */
atomic_store(&g_stop, 1);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
pthread_join(t4, NULL);
munmap((void *)g_map, 65536);
close(g_fd);
printf("repro: done\n");
return 0;
}
Verification Results (QEMU x86_64, KASAN + LOCKDEP + BTRFS_DEBUG)
=================================================================
- Unpatched 97d73c825122:
Fails in < 0.1s with WARN at extent_map.c:820, WARN at
btrfs_zone_finish_endio(), and repeated
"BTRFS error (device nullb0): Unable to find block group for 3/4".
- v1 patch (list_is_last only):
Hangs until timeout.
- Patch 2/2 alone (without patch 1/2):
No block-group errors or hangs, btrfs check passes, but hits
WARN_ON(end < start) at fs/btrfs/extent_map.c:820.
- Patch 1/2 + 2/2 (this v2 series):
0 warnings, 0 KASAN/LOCKDEP/BTRFS errors, clean ro remount readback,
and btrfs check --check-data-csum reports 0 errors across:
* default mount (45s run across default 30s commit) and -o commit=1
* -o commit=1,nodatasum
* multi-file concurrent writer + 8K sliced sync_file_range +
aligned and unaligned ftruncate sizes {0, 100, 4097, 12345,
28672, 4096} (covering out-of-order zone appends and all split
truncation branches)
* qgroups enabled (btrfs quota enable + quota check [7/7] clean)
- Note on compress=zstd:
Under -o commit=1,compress=zstd, the multi-file stress variant hit
"root 5 inode 257 errors 400, nbytes wrong" identically on both
unpatched 97d73c825122 and the patched kernel (a separate
pre-existing issue).
v1: https://lore.kernel.org/r/20261204031023.1758536-1-zihengw03@xxxxxxx
Ziheng Wei (2):
btrfs: clear BTRFS_ORDERED_TRUNCATED on non-truncated split head
btrfs: zoned: split partially truncated ordered extents at endio
fs/btrfs/ordered-data.c | 3 ++-
fs/btrfs/zoned.c | 50 ++++++++++++++++++++++++++++++++++---------------
2 files changed, 37 insertions(+), 16 deletions(-)
base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
--
2.43.0