Root Cause Analysis
1. Context and Problem Statement
When performing a buffered write to an ext4 inode with inline data enabled (EXT4_STATE_MAY_INLINE_DATA), the write path begins in generic_perform_write(), which invokes a_ops->write_begin() (ext4_da_write_begin() or ext4_write_begin()) followed by a_ops->write_end() (ext4_da_write_end() or ext4_write_end()).
For inline data writes:
2. Credit Consumption During Inline Data Preparation
Immediately after starting the handle, ext4_generic_write_inline_data() calls:
ret = ext4_prepare_inline_data(handle, inode, pos + len);
In ext4_prepare_inline_data() (fs/ext4/inline.c:406), if the inline data entry needs to be created or expanded to accommodate pos + len, it calls ext4_create_inline_data() or ext4_update_inline_data(). Both functions modify the inode and call:
error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
ext4_mark_iloc_dirty() calls ext4_handle_dirty_metadata(handle, inode, iloc->bh) -> jbd2_journal_dirty_metadata(handle, bh). Because iloc.bh (the inode table block) is being modified for the first time in this handle:
if (jh->b_modified == 0) {
...
jh->b_modified = 1;
handle->h_total_credits--;
}
This consumes the single credit requested at handle start, leaving jbd2_handle_buffer_credits(handle) == 0.
(Note: Even if ext4_prepare_inline_data() did not expand inline storage, if any data is copied in a short write 0 < copied < len, ext4_write_inline_data_end() invokes mark_inode_dirty(inode) -> ext4_dirty_inode(), which joins the existing handle without adding credits and dirties iloc.bh, likewise exhausting the single credit).
3. Short/Failed Userspace Copy Triggers Orphan Addition
In generic_perform_write(), copy_folio_from_iter_atomic() attempts to copy user data. When the userspace buffer is invalid or causes a page fault (as observed in the syzkaller reproducer register state where RSI = 0x0, a NULL pointer, for a write length RDX = 0x15), the copy fails or is truncated (copied == 0 < len).
Next, generic_perform_write() calls a_ops->write_end(), which calls ext4_write_inline_data_end() (fs/ext4/inline.c:793). Because copied < len, inode->i_size is not extended to pos + len, satisfying:
if (pos + len > inode->i_size && ext4_can_truncate(inode))
ext4_orphan_add(handle, inode);
Unlike regular block writes in ext4_write_begin() (fs/ext4/inode.c:1423-1428), which specifically account for failure by reserving extra credits:
/*
* Reserve one block more for addition to orphan list in case
* we allocate blocks but write fails for some reason
*/
needed_blocks = ext4_chunk_trans_extent(inode, ext4_journal_blocks_per_folio(inode)) + 1;
ext4_generic_write_inline_data() only reserved 1 credit.
4. Buffer Credit Underflow in JBD2
ext4_orphan_add() attempts to add the inode to the orphan tracking mechanism:
- If the
orphan_file feature is enabled (sbi->s_orphan_info.of_blocks != 0), it calls ext4_orphan_file_add(handle, inode) (fs/ext4/orphan.c:14), which records the inode in an orphan file block (oi->of_binfo[i].ob_bh) and marks it dirty via ext4_handle_dirty_metadata().
- If using the traditional superblock orphan list, it marks the superblock buffer (
sbi->s_sbh) dirty via ext4_handle_dirty_metadata().
Both orphan paths require dirtying a completely different metadata block from iloc.bh. When ext4_handle_dirty_metadata() calls jbd2_journal_dirty_metadata(handle, bh) (fs/jbd2/transaction.c:1598):
if (jh->b_modified == 0) {
if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) {
ret = -ENOSPC;
goto out_unlock_bh;
}
...
}
Because jbd2_handle_buffer_credits(handle) is 0, the WARN_ON_ONCE triggers, and -ENOSPC is returned. In __ext4_handle_dirty_metadata() (fs/ext4/ext4_jbd2.c:394-396), receiving this error causes the journal handle to be aborted (ext4_journal_abort_handle()) and reports a filesystem error via ext4_error_inode().