Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Kyrylo Biryuk <illofspeed@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Naohiro Aota <naohiro.aota@wdc.com>
Subject: [BUG] btrfs: zoned: ENOSPC during fsync commit forces filesystem read-only
Date: Sat,  5 Dec 2026 12:01:17 +0100	[thread overview]
Message-ID: <20261205110118.3543-1-illofspeed@gmail.com> (raw)

Hello,

On a freshly formatted 2 GiB zoned btrfs (null_blk), a loop that
creates and fsyncs 4 KiB files reports ENOSPC at zero-based index 6857
(the 6858th file, about 27 MiB of file data). In the rc4 trace below,
the fsync task hits a warning in btrfs_remove_from_free_space_tree()
while running delayed refs during a transaction commit. btrfs reports
-ENOSPC at free-space-tree.c:869, aborts the transaction and forces the
filesystem read-only. The abort dump shows 3.75 MiB used and 1.37 GiB
zone_unusable in the primary metadata space_info.

Tested:
- v7.4-rc5 (7.4.0-rc5-srm5: official rc5 tarball, only a LOCALVERSION
  suffix), with the btrfs module from that kernel build; the kernel
  was not tainted before the first warning. Three runs with the
  reproducer below: each fails at index 6857 at the same warning site
  (free-space-tree.c:869, same offset as in the rc4 trace), and after
  the failure metadata total_bytes=1543503872, bytes_used=3932160 and
  bytes_zone_unusable=1472462848. The script took 19, 14 and 15 s.
- v7.4-rc4 (7.4.0-rc4-srm), three runs: same failing index and the
  same three sysfs values. Here btrfs was built separately as an
  unmodified rc4 baseline (taints O and E: out-of-tree, unsigned); I
  do not have an exact source commit or a module srcversion for that
  build. The trace and the df samples below are from these rc4 runs.
- Not tested on btrfs for-next, on a non-zoned device, or with other
  mkfs options.
- btrfs-progs v6.14. QEMU guest (Q35).

Device: one null_blk device, host-managed zoned, memory backed,
2 GiB, 32 zones of 64 MiB (zone capacity 64 MiB), no conventional
zones, max open zones 16, no active zone limit, 4096-byte blocks.

mkfs:

    mkfs.btrfs -f -O zoned -d single -m single -n 16384 /dev/wcz0

Features from mkfs: extref, skinny-metadata, no-holes,
free-space-tree, zoned. Sector size 4096. Mount options: none
(/proc/mounts shows rw,relatime,ssd,space_cache=v2,subvolid=5,subvol=/).

The reproducer, repro22.sh, is at the end of this mail. It is for root
in a fresh disposable VM with no existing wcz0 or /mnt/repro22. It
creates the null_blk device through configfs, runs the mkfs above
(formatting the test device), attempts to clear dmesg, mounts the file
system, and by default creates up to 10000 files of 4 KiB with one
fsync each. It then prints "btrfs filesystem df", the metadata sysfs
values and dmesg.

Selected metadata samples from "btrfs filesystem df". The index is the
last completed file, counting from zero:

    index  total      used       zone_unusable
      500  128.00MiB  448.00KiB   76.80MiB
     2500  512.00MiB    1.45MiB  489.22MiB
     5000    1.06GiB    2.78MiB    1.01GiB
     6500    1.38GiB    3.55MiB    1.35GiB

In three runs on rc4 with COUNT=10000, the loop reported "[Errno 28] No
space left on device" at index 6857. The sampled metadata df values,
the aborted transaction ID (6872), and the metadata sysfs values after
the failure matched across the runs; timestamps, pids, CPU and fsids
differed. Mount message to first WARNING was 14.1, 20.0 and 13.4
seconds, respectively. The rc5 runs (see Tested) gave the same failing
index and the same sysfs values; I did not compare their df samples.

At abort, primary METADATA is marked full but reports 56688640 bytes
free. Used + reserved + zone_unusable equals 22 zones; free + may_use
equals one zone. These are space_info totals; I do not have
per-block-group measurements showing how those bytes are distributed.
The sysfs values read after the failure, total_bytes=1543503872,
bytes_used=3932160 and bytes_zone_unusable=1472462848, have
bytes_zone_unusable 114688 bytes above the abort dump (1472348160), so
they were read a little later. The difference equals reserved=114688
in the dump.

The DATA, METADATA and SYSTEM totals in the dump account for 1, 23
and 1 zone-equivalents respectively. Zones 0 and 1 are reserved for
the superblock log. In these runs I did not measure metadata-treelog
or data-reloc, or collect reclaim counters (reclaim_count,
reclaim_errors). I do not know how the other five zones were
accounted for or whether reclaim ran before the abort.

Run 1 on rc4, from the warning through the final Btrfs error (the
earlier lines are the mount messages; module list trimmed):

    [   28.173043] ------------[ cut here ]------------
    [   28.173236] WARNING: free-space-tree.c:869 at btrfs_remove_from_free_space_tree+0x2df/0x470 [btrfs], CPU#6: python3/884
    [   28.175290] Modules linked in: null_blk btrfs(OE) [other modules trimmed, none with taint flags] [last unloaded: btrfs]
    [   28.181554] CPU: 6 UID: 0 PID: 884 Comm: python3 Tainted: G           OE       7.4.0-rc4-srm #srm PREEMPT(lazy)
    [   28.182013] Tainted: [O]=OOT_MODULE, [E]=UNSIGNED_MODULE
    [   28.182391] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-0-gb52ca86e094d-prebuilt.qemu.org 04/01/2014
    [   28.183752] RIP: 0010:btrfs_remove_from_free_space_tree+0x2df/0x470 [btrfs]
    [   28.184734] Code: 05 80 0a 00 00 f0 48 0f ba 28 02 0f 82 5a 05 03 00 41 8d 44 24 1e 83 f8 19 77 0f ba 01 00 04 02 48 0f a3 c2 0f 82 23 05 03 00 <0f> 0b e9 1c 05 03 00 4c 89 fe 31 c9 48 89 ea 31 ff e8 5b e2 ff ff
    [   28.186123] RSP: 0018:ffffcb57819978c0 EFLAGS: 00010292
    [   28.186392] RAX: 0000000000000002 RBX: ffff8a3cfbe0bd10 RCX: 0000000000000000
    [   28.186728] RDX: 0000000002040001 RSI: 0000000000000000 RDI: ffff8a3b7b00d980
    [   28.187088] RBP: ffff8a3ac49fb700 R08: 0000000000000000 R09: 00000000ffffffe4
    [   28.187998] R10: ffff8a3ac65bf000 R11: 00000000ffffffff R12: 00000000ffffffe4
    [   28.188917] R13: 0000000015ac9000 R14: 0000000000001000 R15: ffff8a3b7b00d800
    [   28.189845] FS:  00007f5793abb780(0000) GS:ffff8a40a03a7000(0000) knlGS:0000000000000000
    [   28.190701] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
    [   28.190994] CR2: 00007f6416ec816c CR3: 0000000103fec001 CR4: 0000000000f72ef0
    [   28.191331] PKRU: 55555554
    [   28.191496] Call Trace:
    [   28.191655]  <TASK>
    [   28.191930]  ? alloc_reserved_file_extent+0x1e0/0x300 [btrfs]
    [   28.192535]  alloc_reserved_extent+0x1f/0xe0 [btrfs]
    [   28.193235]  __btrfs_run_delayed_refs+0x539/0x1120 [btrfs]
    [   28.194127]  btrfs_run_delayed_refs+0x3d/0xe0 [btrfs]
    [   28.194915]  btrfs_commit_transaction+0x68/0xeb0 [btrfs]
    [   28.195720]  btrfs_sync_file+0x46a/0x590 [btrfs]
    [   28.196283]  do_fsync+0x50/0xc0
    [   28.196470]  ? kmem_cache_free+0x251/0x3c0
    [   28.196711]  __x64_sys_fsync+0x13/0x20
    [   28.196917]  do_syscall_64+0xc4/0x570
    [   28.197125]  ? do_file_open+0xe4/0x1a0
    [   28.197332]  ? do_sys_openat2+0x7a/0xe0
    [   28.197542]  ? __x64_sys_openat+0x54/0xa0
    [   28.197757]  ? do_syscall_64+0x101/0x570
    [   28.197972]  ? do_sys_openat2+0x7a/0xe0
    [   28.198378]  ? __x64_sys_openat+0x54/0xa0
    [   28.198966]  ? do_syscall_64+0x101/0x570
    [   28.199550]  ? __x64_sys_close+0x3d/0x80
    [   28.200209]  ? do_syscall_64+0x101/0x570
    [   28.200789]  ? do_syscall_64+0x101/0x570
    [   28.201363]  ? do_syscall_64+0x278/0x570
    [   28.201633]  ? do_syscall_64+0x101/0x570
    [   28.201845]  ? do_syscall_64+0x35/0x570
    [   28.202052]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
    [   28.202308] RIP: 0033:0x7f5793b4f3a7
    [   28.202520] Code: 48 89 fa 4c 89 df e8 78 b3 00 00 8b 93 08 03 00 00 59 5e 48 83 f8 fc 74 1a 5b c3 0f 1f 84 00 00 00 00 00 48 8b 44 24 10 0f 05 <5b> c3 0f 1f 80 00 00 00 00 83 e2 39 83 fa 08 75 de e8 23 ff ff ff
    [   28.203307] RSP: 002b:00007fff6b14ac40 EFLAGS: 00000202 ORIG_RAX: 000000000000004a
    [   28.204282] RAX: ffffffffffffffda RBX: 00007f5793abb780 RCX: 00007f5793b4f3a7
    [   28.205207] RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000003
    [   28.206128] RBP: 0000000000000003 R08: 0000000000000000 R09: 0000000000000000
    [   28.207098] R10: 0000000000000000 R11: 0000000000000202 R12: 00007f5793a59300
    [   28.208029] R13: 0000000000a84590 R14: 0000000000a84590 R15: 00007f57938fb088
    [   28.208391]  </TASK>
    [   28.208536] ---[ end trace 0000000000000000 ]---
    [   28.208944] BTRFS error (device wcz0 state A): Transaction 6872 aborted (-ENOSPC)
    [   28.209005] BTRFS info (device wcz0 state A): dumping space info:
    [   28.209058] BTRFS info (device wcz0 state A): space_info DATA (sub-group id 0) has 39018496 free, is not full
    [   28.209113] BTRFS info (device wcz0 state A): space_info total=67108864, used=28086272, pinned=0, reserved=4096, may_use=0, readonly=0 zone_unusable=0
    [   28.209168] BTRFS info (device wcz0 state A): space_info METADATA (sub-group id 0) has 56688640 free, is full
    [   28.209223] BTRFS info (device wcz0 state A): space_info total=1543503872, used=3932160, pinned=0, reserved=114688, may_use=10420224, readonly=0 zone_unusable=1472348160
    [   28.209283] BTRFS info (device wcz0 state A): space_info SYSTEM (sub-group id 0) has 66469888 free, is not full
    [   28.209337] BTRFS info (device wcz0 state A): space_info total=67108864, used=16384, pinned=0, reserved=0, may_use=0, readonly=0 zone_unusable=622592
    [   28.209394] BTRFS info (device wcz0 state A): global_block_rsv: size 5767168 reserved 5767168
    [   28.209449] BTRFS info (device wcz0 state A): trans_block_rsv: size 0 reserved 0
    [   28.209503] BTRFS info (device wcz0 state A): chunk_block_rsv: size 0 reserved 0
    [   28.209557] BTRFS info (device wcz0 state A): remap_block_rsv: size 0 reserved 0
    [   28.209611] BTRFS info (device wcz0 state A): delayed_block_rsv: size 524288 reserved 524288
    [   28.209665] BTRFS info (device wcz0 state A): delayed_refs_rsv: size 7864320 reserved 4128768
    [   28.209720] BTRFS: error (device wcz0 state A) in btrfs_remove_from_free_space_tree:869: errno=-28 No space left
    [   28.209777] BTRFS info (device wcz0 state EA): forced readonly
    [   28.209830] BTRFS error (device wcz0 state EA): failed to run delayed ref for logical 363630592 num_bytes 4096 type 178 action 1 ref_mod 1: -28
    [   28.209886] BTRFS: error (device wcz0 state EA) in btrfs_run_delayed_refs:2248: errno=-28 No space left

Is the transaction abort expected under this kind of zoned metadata
pressure? I would have expected an operation to fail with ENOSPC
without forcing the filesystem read-only. The stack shows a full
commit in the fsync task at the warning. In one further rc5 run with
a line added to print /sys/fs/btrfs/<fsid>/generation every 500
files, the generation was 9 at index 0, 509 at index 500 and 6514 at
index 6500, about one per file, so the earlier fsyncs appear to
commit a transaction each as well. That run failed at the same index
and gave the same four df samples as the table above. I do not know
why delayed refs could not finish this commit.

I have not found the cause. I can send the full logs of the rc4 and
rc5 runs.

Disclosure: I used an LLM assistant to help run the tests and to draft
this report. I checked the numbers against the logs.

Kyrylo Biryuk

==== repro22.sh ====
#!/bin/bash
# repro22.sh - small-file fsync load on one zoned btrfs (null_blk, 2 GiB, 32 x 64 MiB zones).
# Root only, for a throw-away test VM. It loads null_blk, creates and removes one
# zoned device, and mounts a file system on it at /mnt/repro22.
# Needs: null_blk with configfs, btrfs-progs, python3 (3.9 or newer), util-linux.
set -u

DEV_NAME=wcz0
DEV=/dev/$DEV_NAME
CFG=/sys/kernel/config/nullb/$DEV_NAME
MNT=/mnt/repro22
COUNT=${COUNT:-10000}

[ "$(id -u)" -eq 0 ] || { echo "run as root" >&2; exit 1; }
[ ! -e "$CFG" ] || { echo "$CFG already exists" >&2; exit 1; }
[ ! -e "$MNT" ] || { echo "$MNT already exists" >&2; exit 1; }

cleanup() {
    cd /
    mountpoint -q "$MNT" 2>/dev/null && umount "$MNT"
    rmdir "$MNT" 2>/dev/null
    if [ -d "$CFG" ]; then
        echo 0 > "$CFG/power" 2>/dev/null
        rmdir "$CFG" 2>/dev/null
    fi
}
trap cleanup EXIT

# Device: zoned null_blk through configfs.
modprobe null_blk nr_devices=0 || exit 1
mountpoint -q /sys/kernel/config || mount -t configfs none /sys/kernel/config
mkdir "$CFG" || exit 1
set_attr() { echo "$2" > "$CFG/$1" || { echo "cannot set $1" >&2; exit 1; }; }
set_attr size 2048
set_attr blocksize 4096
set_attr memory_backed 1
set_attr cache_size 64
set_attr fua 1
set_attr mbps 0
set_attr zoned 1
set_attr zone_size 64
set_attr zone_capacity 64
set_attr zone_nr_conv 0
set_attr zone_max_open 16
set_attr zone_max_active 0
set_attr power 1
udevadm settle 2>/dev/null
[ -b "$DEV" ] || { echo "$DEV did not appear" >&2; exit 1; }

dmesg -C 2>/dev/null || true
mkfs.btrfs -f -O zoned -d single -m single -n 16384 "$DEV" || exit 1
mkdir "$MNT" && mount "$DEV" "$MNT" || exit 1
uname -r
cat /sys/module/btrfs/srcversion 2>/dev/null
grep " $MNT " /proc/mounts

# Workload: COUNT files of 4 KiB, one fsync each.
python3 -I - "$MNT" "$COUNT" <<'EOF'
import os, random, subprocess, sys
mnt, count = sys.argv[1], int(sys.argv[2])
d = mnt + "/d"
os.mkdir(d)
try:
    for i in range(count):
        fd = os.open("%s/file-%08d" % (d, i), os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0o644)
        os.write(fd, random.Random(1 + i).randbytes(4096))
        os.fsync(fd)
        os.close(fd)
        if i % 500 == 0:
            print("files:", i, flush=True)
            subprocess.run("btrfs filesystem df %s | grep Metadata" % mnt, shell=True)
    print("done:", count, "files")
except OSError as e:
    print("failed at file %d: %s" % (i, e))
EOF

echo "== btrfs filesystem df"
btrfs filesystem df "$MNT"

echo "== metadata space_info (sysfs)"
UUID=$(blkid -s UUID -o value "$DEV")
for f in total_bytes bytes_used bytes_zone_unusable; do
    printf '%s=%s\n' "$f" "$(cat /sys/fs/btrfs/$UUID/allocation/metadata/$f)"
done

echo "== dmesg"
dmesg

                 reply	other threads:[~2026-12-05 11:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205110118.3543-1-illofspeed@gmail.com \
    --to=illofspeed@gmail.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=naohiro.aota@wdc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox