ext4: prevent inline data creation on extent-based inodes
PATCH 1/1 ext4: prevent inline data creation on extent-based inodes
ext4_prepare_inline_data() only checks EXT4_STATE_MAY_INLINE_DATA before creating inline data. However, after ext4_convert_inline_data() converts an inode to use extents, a concurrent write can race and re-create inline data via ext4_create_inline_data(), which re-sets MAY_INLINE_DATA. This leads to an inconsistent state where an extent-based inode has inline data, causing BUG_ON in ext4_do_writepages() when dirty mmap pages exist.
The race window is between ext4_convert_inline_data() clearing MAY_INLINE_DATA and the concurrent write checking it:
page_mkwrite: write:
ext4_convert_inline_data() ext4_da_write_begin()
ext4_has_inline_data = false MAY_INLINE_DATA set (from iget)
clear MAY_INLINE_DATA ext4_prepare_inline_data()
return 0 ext4_create_inline_data() success!
block_page_mkwrite() success re-sets MAY_INLINE_DATA!
folio dirty, PTE writable
writepages:
ext4_has_inline_data = TRUE MAY_INLINE_DATA = TRUE dirty page exists -> BUG_ON!
Fix this by adding a check for EXT4_INODE_EXTENTS in ext4_prepare_inline_data(). Once an inode has been converted to use extents, it must never go back to inline data regardless of the MAY_INLINE_DATA state.
---
An alternative (or complementary) approach would be to convert inline data at mmap_prepare time (ext4_file_mmap_prepare) rather than at page_mkwrite time. This eliminates the race window entirely by converting before the mapping is established, making both this fix and commit 7b4cc9787fe3 unnecessary.
fs/ext4/inline.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/ext4/inline.c +7 -0
@@ -418,6 +418,13 @@ static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,return -ENOSPC ;ext4_write_lock_xattr (inode, &no_expand); + +/* Once converted to extents, never go back to inline data. */ +if (ext4_test_inode_flag (inode,EXT4_INODE_EXTENTS )) { +ext4_write_unlock_xattr (inode, &no_expand); +return -ENOSPC ; + } +/* * ei->i_inline_size may have changed since the initial check * if other xattrs were added. Recalculate to ensure
Signature · 1 line
2.43.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 34 lines · On 7/2/26 09:38, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] The patch completely breaks the inline data feature for all new files and directories on extents-enabled filesystems.Pre-existing issues:
- [High] The patch fails to resolve the data corruption race condition on filesystems where theextentsfeature is disabled, leaving indirect-mapped files vulnerable. --fs/ext4/inline.c static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode, · line 423
Quoted 11 lines
fs/ext4/inline.c +3
-0@@ -418,6 +418,13 @@ static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,return -ENOSPC ;ext4_write_lock_xattr (inode, &no_expand); + +/* Once converted to extents, never go back to inline data. */ +if (ext4_test_inode_flag (inode,EXT4_INODE_EXTENTS )) {[Severity: High]
Does this completely disable inline data for all newly created files and directories on extents-enabled filesystems?Looking at
__ext4_new_inode(), it appears newly created inodes inherently have theEXT4_INODE_EXTENTSflag set:fs/ext4/ialloc.c:__ext4_new_inode() { ... ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); ... }
Quoted 4 lines
If
ext4_prepare_inline_data()is called during a write or directory creation for a new inode, wouldn't this new check evaluate to true and unconditionally abort the inline data creation by returning-ENOSPC?
My apologies, my analysis was incorrect, and I am currently working on a new fix.
Please ignore this version.