[PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion
From: Arbab Haider
Date: Thu Nov 26 2026 - 02:08:41 EST
ext4_ext_migrate() and ext4_ind_migrate() are invoked under i_rwsem alone
(neither filemap_invalidate_lock nor xattr_sem is held), and
ext4_ext_migrate() checks EXT4_INODE_EXTENTS and ext4_has_inline_data()
before acquiring ext4_writepages_down_write(inode->i_sb).
This allows two races against concurrent inline-data conversion:
1. Against ext4_page_mkwrite() -> ext4_convert_inline_data_nolock()
rollback: ext4_create_inline_data() clears EXT4_INODE_EXTENTS before
setting EXT4_INODE_INLINE_DATA while leaving the extent header from
ext4_ext_tree_init() in ei->i_data[0]. ext4_ext_migrate() can observe
both flags clear, interpret ei->i_data[0] as a direct block pointer,
and persist both EXT4_INODE_EXTENTS and EXT4_INODE_INLINE_DATA on disk.
Similarly, ext4_ind_migrate() can observe transiently set
EXT4_INODE_EXTENTS during conversion and clear it concurrently.
2. Against ext4_do_writepages() on a delalloc inline-data inode: writeback
can convert the inode (setting EXT4_INODE_EXTENTS, clearing
EXT4_INODE_INLINE_DATA, and allocating block 0 into ei->i_data[])
between ext4_ext_migrate()'s EXT4_INODE_EXTENTS and
ext4_has_inline_data() checks, causing ext4_ext_migrate() to interpret
the extent header and extent entry in ei->i_data[] as indirect blocks.
Hold filemap_invalidate_lock(inode->i_mapping) across ext4_ext_migrate()
and ext4_ind_migrate() to exclude ext4_page_mkwrite(), and acquire
ext4_writepages_down_write(inode->i_sb) before checking EXT4_INODE_EXTENTS
and ext4_has_inline_data() in ext4_ext_migrate() to exclude concurrent
writeback.
Signed-off-by: Arbab Haider <arbabhaider649@xxxxxxxxx>
---
fs/ext4/migrate.c | 33 ++++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index 807bd3034..528623310 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -433,22 +433,25 @@ int ext4_ext_migrate(struct inode *inode)
uid_t owner[2];
int alloc_ctx;
+ filemap_invalidate_lock(inode->i_mapping);
+ alloc_ctx = ext4_writepages_down_write(inode->i_sb);
+
/*
* If the filesystem does not support extents, or the inode
* already is extent-based, error out.
*/
if (!ext4_has_feature_extents(inode->i_sb) ||
ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
- ext4_has_inline_data(inode))
- return -EINVAL;
+ ext4_has_inline_data(inode)) {
+ retval = -EINVAL;
+ goto out_unlock;
+ }
if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
/*
* don't migrate fast symlink
*/
- return retval;
-
- alloc_ctx = ext4_writepages_down_write(inode->i_sb);
+ goto out_unlock;
/*
* Worst case we can touch the allocation bitmaps and a block
@@ -613,6 +616,7 @@ int ext4_ext_migrate(struct inode *inode)
out_unlock:
ext4_writepages_up_write(inode->i_sb, alloc_ctx);
iput(tmp_inode);
+ filemap_invalidate_unlock(inode->i_mapping);
return retval;
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_migrate);
@@ -634,14 +638,19 @@ int ext4_ind_migrate(struct inode *inode)
int ret, ret2 = 0;
int alloc_ctx;
+ filemap_invalidate_lock(inode->i_mapping);
+
if (!ext4_has_feature_extents(inode->i_sb) ||
- (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
- return -EINVAL;
+ (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
+ ret = -EINVAL;
+ goto out_invalidate;
+ }
- if (ext4_has_feature_bigalloc(inode->i_sb))
- return -EOPNOTSUPP;
- if (ext4_inode_buffered_iomap(inode))
- return -EOPNOTSUPP;
+ if (ext4_has_feature_bigalloc(inode->i_sb) ||
+ ext4_inode_buffered_iomap(inode)) {
+ ret = -EOPNOTSUPP;
+ goto out_invalidate;
+ }
/*
* In order to get correct extent info, force all delayed allocation
@@ -702,6 +711,8 @@ int ext4_ind_migrate(struct inode *inode)
ext4_journal_stop(handle);
out_unlock:
ext4_writepages_up_write(inode->i_sb, alloc_ctx);
+out_invalidate:
+ filemap_invalidate_unlock(inode->i_mapping);
return ret;
}
EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ind_migrate);
--
2.53.0