Linux EXT4 FS development
 help / color / mirror / Atom feed
From: Arbab Haider <arbabhaider649@gmail.com>
To: tytso@mit.edu
Cc: adilger.kernel@dilger.ca, libaokun@linux.alibaba.com,
	jack@suse.cz, ojaswin@linux.ibm.com, ritesh.list@gmail.com,
	yi.zhang@huawei.com, yebin10@huawei.com,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion
Date: Thu, 26 Nov 2026 12:07:49 +0500	[thread overview]
Message-ID: <20261126070748.272109-7-arbabhaider649@gmail.com> (raw)
In-Reply-To: <20261126070748.272109-2-arbabhaider649@gmail.com>

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@gmail.com>
---
 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


  parent reply	other threads:[~2026-11-26  7:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  7:07 [PATCH v4 0/5] ext4: fix inline data conversion races Arbab Haider
2026-11-26  7:07 ` [PATCH v4 1/5] ext4: stub the nonatomic buffer lookup in inode KUnit tests Arbab Haider
2026-11-26  7:07 ` [PATCH v4 2/5] ext4: fix delayed-allocation inline data conversion races Arbab Haider
2026-11-26  7:07 ` [PATCH v4 3/5] ext4: keep EXT4_STATE_MAY_INLINE_DATA set during inline conversion Arbab Haider
2026-11-26  7:07 ` [PATCH v4 4/5] ext4: re-check inline data under filemap_invalidate_lock in swap_inode_boot_loader Arbab Haider
2026-11-26  7:07 ` Arbab Haider [this message]
2026-11-26  7:32 ` [PATCH v4 0/5] ext4: fix inline data conversion races sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126070748.272109-7-arbabhaider649@gmail.com \
    --to=arbabhaider649@gmail.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=tytso@mit.edu \
    --cc=yebin10@huawei.com \
    --cc=yi.zhang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox