WARNING in btrfs_destroy_inode (4)

Status: moderation: reported on 2026/12/01 08:00
Subsystems: btrfs
Labels: prio:normal
[Documentation on labels]
Reported-by: syzbot+072d1d32645aa70ddb32@syzkaller.appspotmail.com
First crash: 2h04m, last: 2h04m
✨ AI cause bisection: introduced by (AI job) :
commit 13141df705b7145e46b88b1147d67d7a43f2d9e8
Author: Qu Wenruo <wqu@suse.com>
Date: Mon Jul 28 08:27:56 2025 +0000

  btrfs: make nocow_one_range() to do cleanup on error

✨ AI-generated root cause summary:

In fs/btrfs/inode.c:nocow_one_range(), when writeback encounters an error (such as btrfs_reloc_clone_csums() returning -EIO), the error path calls btrfs_cleanup_ordered_extents() followed by extent_clear_unlock_delalloc(). Because btrfs_cleanup_ordered_extents() asynchronously queues ordered extent completion to the endio worker pool and btrfs_finish_one_ordered() bypasses extent range locking on BTRFS_ORDERED_IOERR, btrfs_remove_ordered_extent() can execute before extent_clear_unlock_delalloc() clears the EXTENT_DELALLOC bit. When this race occurs, btrfs_remove_ordered_extent() sees outstanding_extents >= 1, recalculates a non-zero reservation size, and releases only excess bytes; subsequently, extent_clear_unlock_delalloc() clears EXTENT_DELALLOC without passing EXTENT_CLEAR_META_RESV, decrementing outstanding_extents without releasing the retained metadata reservation. When the inode is evicted and destroyed, btrfs_destroy_inode() hits WARN_ON(inode->block_rsv.reserved) due to the leaked reservation bytes.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 19cc08c1a6a6:

Technical Breakdown and Root Cause Analysis

1. Context and Setup

During block group relocation on a mixed block-group Btrfs filesystem (flags data|metadata, nodesize == sectorsize == 4096), data is relocated via the relocation data inode rc->data_inode (inode 258 on data_reloc_root).

When pages in the relocation cluster are prepared for writeback in relocate_one_folio() (fs/btrfs/relocation.c):

  1. btrfs_delalloc_reserve_metadata() reserves metadata for the dirty range (12,288 bytes = 3 sectors).
  2. For mixed block groups with nodesize = 4096, calc_inode_reservations() calculates:
    • Extents needed: nr_extents = 1
    • Checksum leaves: csum_leaves = 1
    • Inode update: btrfs_calc_metadata_size(fs_info, 1) = 4096 * 8 * 1 = 32768 (0x8000)
    • Metadata insert: btrfs_calc_insert_metadata_size(fs_info, nr_extents + csum_leaves) = 4096 * 8 * 2 * 2 = 131072 (0x20000)
    • Total reserved: 0x20000 + 0x8000 = 0x28000 (163,840 bytes).
  3. The reserved bytes are added to inode->block_rsv.reserved and inode->block_rsv.size, and inode->outstanding_extents is set to 1.

2. Writeback and the Defect in nocow_one_range()

Writeback initiates via btrfs_wait_ordered_range() -> run_delalloc_nocow() -> nocow_one_range() (fs/btrfs/inode.c:1883):

  1. nocow_one_range() calls btrfs_alloc_ordered_extent() (fs/btrfs/ordered-data.c:300), which increments inode->outstanding_extents from 1 to 2.
  2. For the relocation inode, btrfs_reloc_clone_csums(ordered) is called. In this bug report, because the transaction previously aborted with -EIO (in __btrfs_update_delayed_inode), btrfs_reloc_clone_csums() returns -EIO.
  3. nocow_one_range() jumps to error: (fs/btrfs/inode.c:1934):
    error:
        btrfs_cleanup_ordered_extents(inode, file_pos, len);
        extent_clear_unlock_delalloc(inode, file_pos, end, locked_folio, cached,
                                     EXTENT_LOCKED | EXTENT_DELALLOC |
                                     EXTENT_CLEAR_DATA_RESV,
                                     PAGE_UNLOCK | PAGE_START_WRITEBACK |
                                     PAGE_END_WRITEBACK);
    

3. Accounting Desynchronization and Metadata Leak

Two compounding bugs exist in this error cleanup sequence:

  • Asynchronous Ordered Extent Cleanup vs. Delalloc Extent Clearing: btrfs_cleanup_ordered_extents() calls btrfs_mark_ordered_io_finished(), which marks the ordered extent as finished with error (BTRFS_ORDERED_IOERR) and calls btrfs_queue_ordered_fn(entry), dispatching completion to endio_write_workers asynchronously. In btrfs_finish_one_ordered() (fs/btrfs/inode.c:3178), checking test_bit(BTRFS_ORDERED_IOERR) causes an immediate goto out;, bypassing btrfs_lock_extent_bits(). Consequently, btrfs_remove_ordered_extent() executes in the worker thread without waiting for extent_clear_unlock_delalloc() to complete in the submitting thread:

    1. btrfs_remove_ordered_extent() decrements inode->outstanding_extents by 1 (leaving it at 1).
    2. It then calls btrfs_delalloc_release_metadata(inode, release, true).
    3. btrfs_calculate_inode_block_rsv_size() sees inode->outstanding_extents == 1, recalculates block_rsv->size based on 1 remaining extent, and sets block_rsv->size to a non-zero value.
    4. btrfs_inode_rsv_release() calls btrfs_block_rsv_release(..., 0, ...), which only releases bytes exceeding block_rsv->size, clamping block_rsv->reserved = block_rsv->size.
  • Missing EXTENT_CLEAR_META_RESV in extent_clear_unlock_delalloc(): Submitting thread then runs extent_clear_unlock_delalloc() with EXTENT_DELALLOC, but intentionally omits EXTENT_CLEAR_META_RESV under the assumption that ordered extent completion will release the metadata. In btrfs_clear_delalloc_extent() (fs/btrfs/inode.c:2635):

    spin_lock(&inode->lock);
    btrfs_mod_outstanding_extents(inode, -num_extents);
    spin_unlock(&inode->lock);
    
    if (bits & EXTENT_CLEAR_META_RESV)
        btrfs_delalloc_release_metadata(inode, len, true);
    

    inode->outstanding_extents is decremented from 1 to 0, but because EXTENT_CLEAR_META_RESV is absent, btrfs_delalloc_release_metadata() is never invoked. The reservation bytes retained in inode->block_rsv.reserved (0x28000) are permanently orphaned.

    (Note: Even if failure occurs earlier before an ordered extent is allocatedβ€”e.g., failure in btrfs_create_io_em() or btrfs_alloc_ordered_extent()β€”error: similarly clears EXTENT_DELALLOC without EXTENT_CLEAR_META_RESV, leaking the entire reservation since no ordered extent exists to release it.)

4. Eviction Splat

When btrfs_relocate_block_group() terminates on error, it invokes iput(rc->data_inode). With no references remaining, evict() -> destroy_inode() -> btrfs_destroy_inode() runs synchronously. In btrfs_destroy_inode() (fs/btrfs/inode.c:8096):

WARN_ON(inode->block_rsv.reserved);

Because inode->block_rsv.reserved still holds 0x28000 (163,840 bytes), the warning triggers (and, with panic_on_warn=1 enabled, halts the system).

▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
0973296c bisect-cause IsRelease: ❌ ❓ WARNING in btrfs_destroy_inode (4) 2026/12/01 07:09 2026/12/01 07:09 2026/12/01 08:20 ba8e13b2
e7aca4ce assessment-security DenialOfService: βœ… Exploitable: ❌ FilesystemTrigger: βœ… FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ WARNING in btrfs_destroy_inode (4) 2026/12/01 06:55 2026/12/01 06:56 2026/12/01 07:59 ba8e13b2
▶ ▼ Similar bugs (3)
Kernel Title Rank πŸ›ˆ Repro Cause bisect Fix bisect Count Last Reported Patched Status
upstream WARNING in btrfs_destroy_inode (2) btrfs -1 1 813d 809d 0/30 auto-obsoleted due to no activity on 2024/12/18 00:50
upstream WARNING in btrfs_destroy_inode btrfs -1 1 1501d 1501d 0/30 auto-obsoleted due to no activity on 2023/02/18 09:16
upstream WARNING in btrfs_destroy_inode (3) prio:high btrfs -1 syz, C 81 80d 400d 0/30 auto-obsoleted due to no activity on 2026/11/20 20:35

Sample crash report:
loop0: detected capacity change from 0 to 32768
BTRFS: device fsid 14d642db-7b15-43e4-81e6-4b8fac6a25f8 devid 1 transid 8 /dev/loop0 (7:0) scanned by syz.0.0 (5337)
BTRFS info (device loop0): first mount of filesystem 14d642db-7b15-43e4-81e6-4b8fac6a25f8
BTRFS info (device loop0): using blake2b checksum algorithm
BTRFS info (device loop0): enabling ssd optimizations
BTRFS info (device loop0): turning on async discard
BTRFS info (device loop0): enabling free space tree
BTRFS info (device loop0): use zstd compression, level 3
BTRFS info (device loop0): disk added /dev/nullb0
BTRFS info (device loop0): balance: start -dconvert=raid0 -mconvert=raid0
BTRFS info (device loop0): relocating block group 6881280 flags data|metadata
BTRFS info (device loop0): relocating block group 5242880 flags data|metadata
BTRFS error (device loop0 state A): Transaction 15 aborted (-EIO)
BTRFS: error (device loop0 state A) in __btrfs_update_delayed_inode:1027: errno=-5 IO failure
BTRFS info (device loop0 state EA): forced readonly
BTRFS: error (device loop0 state EA) in __btrfs_run_delayed_items:1160: errno=-5 IO failure
BTRFS warning (device loop0 state EA): Skipping commit of aborted transaction.
BTRFS: error (device loop0 state EA) in cleanup_transaction:2089: errno=-5 IO failure
BTRFS error (device loop0 state EA): nocow_one_range failed, root=-9 inode=258 start=53248 len=12288: -EIO
BTRFS error (device loop0 state EA): run_delalloc_nocow failed, root=18446744073709551607 inode=258 start=53248 len=12288 cur_offset=53248 oe_cleanup=53248 oe_cleanup_len=0 untouched_start=65536 untouched_len=0: -EIO
BTRFS error (device loop0 state EA): failed to run delalloc range, root=-9 ino=258 folio=53248 submit_bitmap=0 start=53248 len=12288: -EIO
------------[ cut here ]------------
inode->block_rsv.reserved
WARNING: fs/btrfs/inode.c:8096 at btrfs_destroy_inode+0x7a6/0x8c0 fs/btrfs/inode.c:8096, CPU#0: syz.0.0/5337
Modules linked in:
CPU: 0 UID: 0 PID: 5337 Comm: syz.0.0 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
RIP: 0010:btrfs_destroy_inode+0x7a6/0x8c0 fs/btrfs/inode.c:8096
Code: 46 fd ff e9 da fe ff ff e8 47 15 9b fd 90 0f 0b 90 e9 e7 f8 ff ff e8 39 15 9b fd 90 0f 0b 90 e9 11 f9 ff ff e8 2b 15 9b fd 90 <0f> 0b 90 e9 42 f9 ff ff e8 1d 15 9b fd 90 0f 0b 90 e9 66 f9 ff ff
RSP: 0018:ffffc9000e4af8c8 EFLAGS: 00010287
RAX: ffffffff842e4b85 RBX: 0000000000028000 RCX: 0000000000100000
RDX: ffffc9000ef02000 RSI: 000000000008704f RDI: 0000000000087050
RBP: ffff8880143231e0 R08: ffff88804236c7c7 R09: 1ffff1100846d8f8
R10: dffffc0000000000 R11: ffffffff842e43e0 R12: ffff888014323448
R13: 1ffff1100286463c R14: ffff888014323570 R15: dffffc0000000000
FS:  00007fce767d26c0(0000) GS:ffff88808a9e5000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000555ab4af6168 CR3: 0000000014b66000 CR4: 0000000000352ef0
Call Trace:
 <TASK>
 destroy_inode fs/inode.c:387 [inline]
 evict+0x99e/0xc20 fs/inode.c:853
 btrfs_relocate_block_group+0x7b2/0xaf0 fs/btrfs/relocation.c:5562
 btrfs_relocate_chunk+0x114/0x810 fs/btrfs/volumes.c:3768
 __btrfs_balance+0x1b5f/0x29d0 fs/btrfs/volumes.c:4707
 btrfs_balance+0xaa6/0x1180 fs/btrfs/volumes.c:5094
 btrfs_ioctl_balance+0x3da/0x640 fs/btrfs/ioctl.c:3518
 vfs_ioctl fs/ioctl.c:52 [inline]
 __do_sys_ioctl fs/ioctl.c:598 [inline]
 __se_sys_ioctl+0xfc/0x170 fs/ioctl.c:584
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fce759a9b59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fce767d1fe8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007fce75c35fa0 RCX: 00007fce759a9b59
RDX: 0000200000011440 RSI: 00000000c4009420 RDI: 0000000000000005
RBP: 00007fce75a3a18b R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fce75c36038 R14: 00007fce75c35fa0 R15: 00007ffef2235c48
 </TASK>

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/12/01 06:55 upstream 19cc08c1a6a6 ba8e13b2 .config console log report [disk image (non-bootable)] [vmlinux] [kernel image] ci-snapshot-upstream-root WARNING in btrfs_destroy_inode
* Struck through repros no longer work on HEAD.