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):
btrfs_delalloc_reserve_metadata() reserves metadata for the dirty range (12,288 bytes = 3 sectors).
- 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).
- 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):
nocow_one_range() calls btrfs_alloc_ordered_extent() (fs/btrfs/ordered-data.c:300), which increments inode->outstanding_extents from 1 to 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.
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:
btrfs_remove_ordered_extent() decrements inode->outstanding_extents by 1 (leaving it at 1).
- It then calls
btrfs_delalloc_release_metadata(inode, release, true).
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.
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).