Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH v4 0/5] ext4: fix inline data conversion races
@ 2026-11-26  7:07 Arbab Haider
  2026-11-26  7:07 ` [PATCH v4 1/5] ext4: stub the nonatomic buffer lookup in inode KUnit tests Arbab Haider
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Arbab Haider @ 2026-11-26  7:07 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	yebin10, linux-ext4, linux-kernel

This series addresses ext4 inline-data conversion races. Patch 1 fixes
the KUnit inode fixture so it passes on the base. Patch 2 fixes the
delayed-allocation handoff to writeback, carries the stable tags, and
can be applied independently. Patch 3 keeps synchronous conversion
state visible and serializes callers that inspect it. Patches 4 and 5
cover boot-loader swap and inode migration, respectively.

Booted each cumulative patch in QEMU x86-64 with KASAN, UBSAN and
lockdep. All five source trees passed all 22 ext4 KUnit suites. The
fixture-only tree also verifies that the two inode test failures on
the original base are fixed independently of the runtime patches.

The complete series also passed 555 ext4 KUnit tests in an arm64 QEMU
guest with lockdep and DEBUG_ATOMIC_SLEEP enabled. LKMM models of the
locked writeback handoff and rollback observation both excluded the
invalid state.
The arm64 guest also passed persistence and truncate checks on a 1 KiB
journaled filesystem and a 4 KiB filesystem without a journal.

The full series passed inline-to-block conversion, remount and data
comparison, and truncate-to-zero checks with delalloc, nodelalloc,
no journal, data journaling, 1 KiB blocks, bigalloc, fast commit and
indirect mappings. The indirect-mapping run also checked sparse reads,
SEEK_DATA/SEEK_HOLE, FIEMAP and direct reads. The boot-loader swap fix
rejected an inline file and completed a swap round trip on a disposable
image.
e2fsck -fn passed on all test images. Sashiko --no-ai passed patch
extraction and application.

Changes since v3:
- Rebase onto 4dce97c6c265 and split the change into five patches.
- Fix the nonatomic buffer lookup stub in the inode KUnit fixture.
- Publish the delayed-allocation handoff under xattr_sem and check it
  under the same write lock used to remove inline data. Return -EIO on
  an incomplete handoff and propagate inline removal and journal errors.
- Finish the delayed-allocation handoff on truncate-to-zero and on a
  failed write at size zero. Extend the inline KUnit coverage.
- Keep MAY_INLINE_DATA set through synchronous conversion and serialize
  callers that inspect inline state, preserving sparse-file readahead.
- Serialize boot-loader swap and inode migration against conversion.

Link: https://lore.kernel.org/linux-ext4/20261123172856.100887-2-arbabhaider649@gmail.com/

Arbab Haider (5):
  ext4: stub the nonatomic buffer lookup in inode KUnit tests
  ext4: fix delayed-allocation inline data conversion races
  ext4: keep EXT4_STATE_MAY_INLINE_DATA set during inline conversion
  ext4: re-check inline data under filemap_invalidate_lock in
    swap_inode_boot_loader
  ext4: serialize inode migration against concurrent inline data
    conversion

 fs/ext4/file.c        |  73 ++++++++++++++++++++------
 fs/ext4/inline-test.c |  17 +++++-
 fs/ext4/inline.c      | 118 +++++++++++++++++++++++++++++++++---------
 fs/ext4/inode-test.c  |   2 +
 fs/ext4/inode.c       |  71 ++++++++++++++++++-------
 fs/ext4/ioctl.c       |   4 ++
 fs/ext4/migrate.c     |  33 ++++++++----
 fs/ext4/readpage.c    |  15 ++++--
 8 files changed, 260 insertions(+), 73 deletions(-)


base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
-- 
2.53.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/5] ext4: stub the nonatomic buffer lookup in inode KUnit tests
  2026-11-26  7:07 [PATCH v4 0/5] ext4: fix inline data conversion races Arbab Haider
@ 2026-11-26  7:07 ` Arbab Haider
  2026-11-26  7:07 ` [PATCH v4 2/5] ext4: fix delayed-allocation inline data conversion races Arbab Haider
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Arbab Haider @ 2026-11-26  7:07 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	yebin10, linux-ext4, linux-kernel

ext4_get_inode_loc_noio() uses ext4_sb_find_get_block_nonatomic(),
but the inode test fixture only stubs the atomic buffer lookup.
The real lookup cannot find the fixture's synthetic inode table
buffers, so it returns -EAGAIN even for the cached test buffer.
ext4_sync_inode_metadata() then treats the same cache miss as success
and never observes the synthetic buffer's write error.

Register the existing fake-cache lookup stub for the nonatomic
helper as well.

Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/ext4/inode-test.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/ext4/inode-test.c b/fs/ext4/inode-test.c
index ab1ca5982..529cab24d 100644
--- a/fs/ext4/inode-test.c
+++ b/fs/ext4/inode-test.c
@@ -1149,6 +1149,8 @@ static struct inode_test_ctx *inode_test_setup(struct kunit *test)
 				   inode_test_sb_getblk_stub);
 	kunit_activate_static_stub(test, ext4_sb_find_get_block,
 				   inode_test_sb_find_get_block_stub);
+	kunit_activate_static_stub(test, ext4_sb_find_get_block_nonatomic,
+				   inode_test_sb_find_get_block_stub);
 	kunit_activate_static_stub(test, ext4_sb_getblk_unmovable,
 				   inode_test_sb_getblk_unmovable_stub);
 	kunit_activate_static_stub(test, ext4_ext_getblk,
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 2/5] ext4: fix delayed-allocation inline data conversion races
  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 ` Arbab Haider
  2026-11-26  7:07 ` [PATCH v4 3/5] ext4: keep EXT4_STATE_MAY_INLINE_DATA set during inline conversion Arbab Haider
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Arbab Haider @ 2026-11-26  7:07 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	yebin10, linux-ext4, linux-kernel, stable

Commit ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages") attempted to
prevent ext4_convert_inline_data() from racing with delayed-allocation
inline data conversion by calling filemap_flush() when
EXT4_STATE_MAY_INLINE_DATA is already cleared.

However, several races remain in the delayed-allocation inline handoff:

1. filemap_flush() is asynchronous (WB_SYNC_NONE) and can return 0 while
   folio 0 is locked by a concurrent buffered write and
   ext4_has_inline_data(inode) is still true.  ext4_convert_inline_data()
   then proceeds to ext4_convert_inline_data_nolock() even though
   EXT4_STATE_MAY_INLINE_DATA is already cleared (meaning
   ext4_da_convert_inline_data_to_extent() has already moved the data into
   dirty folio 0), either copying stale on-disk inline data or rolling
   back via ext4_restore_inline_data() and re-setting
   EXT4_STATE_MAY_INLINE_DATA while writeback is pending, triggering:

     kernel BUG at fs/ext4/inode.c:3028!
     RIP: 0010:ext4_do_writepages+0x25c0/0x39a0
     Call Trace:
      ext4_writepages+0x285/0x500
      do_writepages+0x205/0x520
      filemap_writeback+0x1a6/0x250
      file_write_and_wait_range+0x8c/0x150
      ext4_sync_file+0x2cf/0x950
      ext4_buffered_write_iter+0x428/0x5d0
      ext4_file_write_iter+0x7cb/0x1450

2. In ext4_da_convert_inline_data_to_extent(), folio_mark_dirty(folio)
   was called before ext4_clear_inode_state(EXT4_STATE_MAY_INLINE_DATA),
   and ext4_do_writepages() tested EXT4_STATE_MAY_INLINE_DATA before
   acquiring xattr_sem.  On concurrent or weakly ordered CPUs, writeback
   can observe PAGECACHE_TAG_DIRTY with EXT4_STATE_MAY_INLINE_DATA still
   set and hit BUG_ON() in ext4_do_writepages().

3. When an inode already in delayed allocation
   (!EXT4_STATE_MAY_INLINE_DATA) is truncated to i_size == 0 in
   ext4_inline_data_truncate() or drops a failed write at i_size == 0 in
   ext4_da_truncate_failed_write(), dirty folio 0 and delayed extents are
   dropped while EXT4_INODE_INLINE_DATA remains set with
   !EXT4_STATE_MAY_INLINE_DATA and no dirty pages.  Also, when truncated
   to i_size == inline_size, ext4_inline_data_truncate() skipped removing
   delayed extents beyond i_size because ext4_es_remove_extent() was
   inside if (i_size < inline_size).

Fix the delayed-allocation handoff:

- In ext4_da_convert_inline_data_to_extent(), hold xattr_sem for write
  and clear EXT4_STATE_MAY_INLINE_DATA before marking folio 0 dirty, and
  in writeback validate the handoff and remove inline data under the
  same xattr_sem write lock. Return an error if the handoff is
  incomplete or removing inline data fails.
- In ext4_convert_inline_data(), serialize the initial and post-writeback
  state checks under down_read(&xattr_sem) (with a lockless fast path when
  neither EXT4_INODE_INLINE_DATA nor EXT4_STATE_MAY_INLINE_DATA is set),
  use synchronous filemap_write_and_wait() when
  !EXT4_STATE_MAY_INLINE_DATA, return ext4_emergency_state() or -EIO if
  ext4_has_inline_data(inode) still holds after writeback, and re-check
  EXT4_STATE_MAY_INLINE_DATA under ext4_write_lock_xattr() with a retry.
- In ext4_inline_data_truncate(), finish destroying inline data via
  ext4_destroy_inline_data_nolock() when !EXT4_STATE_MAY_INLINE_DATA and
  inode->i_size == 0, and remove delayed extents beyond i_size even when
  i_size == inline_size.  In ext4_da_truncate_failed_write(), call
  ext4_truncate() when inode->i_size == 0 with EXT4_INODE_INLINE_DATA set
  and !EXT4_STATE_MAY_INLINE_DATA (falling back to ext4_es_remove_extent()
  if ext4_truncate() fails).
- Update test_convert_inline_data_and_rollback() in fs/ext4/inline-test.c
  to match the new -EIO behavior when !EXT4_STATE_MAY_INLINE_DATA remains
  inline after writeback, and also cover normal conversion with
  EXT4_STATE_MAY_INLINE_DATA set.

Fixes: ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages")
Cc: stable@vger.kernel.org
Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/ext4/inline-test.c |  17 ++++++-
 fs/ext4/inline.c      | 104 ++++++++++++++++++++++++++++++++----------
 fs/ext4/inode.c       |  18 ++++++--
 3 files changed, 111 insertions(+), 28 deletions(-)

diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
index 0198ce831..006c5b3ea 100644
--- a/fs/ext4/inline-test.c
+++ b/fs/ext4/inline-test.c
@@ -717,6 +717,11 @@ static void test_write_and_destroy_inline_data(struct kunit *test)
 	KUNIT_ASSERT_FALSE(test, IS_ERR(handle));
 	ret = ext4_get_inode_loc(inode, &iloc);
 	KUNIT_ASSERT_EQ(test, ret, 0);
+	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	ret = ext4_destroy_inline_data(handle, inode);
+	KUNIT_EXPECT_EQ(test, ret, -EIO);
+	KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
+	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 	ret = ext4_destroy_inline_data(handle, inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
@@ -741,6 +746,7 @@ static void test_write_and_destroy_inline_data(struct kunit *test)
 
 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
 	KUNIT_ASSERT_FALSE(test, IS_ERR(handle));
+	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 	ret = ext4_destroy_inline_data(handle, inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
@@ -1139,14 +1145,23 @@ static void test_convert_inline_data_and_rollback(struct kunit *test)
 
 	/*
 	 * 2. Inline inode without EXT4_STATE_MAY_INLINE_DATA -> calls
-	 * filemap_flush() and finishes converting inline data if still inline.
+	 * filemap_write_and_wait() and returns -EIO if still inline instead of
+	 * falling through to ext4_convert_inline_data_nolock(). With
+	 * EXT4_STATE_MAY_INLINE_DATA set, converts inline data to an extent.
 	 */
 	ret = create_inline_file(inode, buf, sizeof(buf));
 	KUNIT_ASSERT_EQ(test, ret, 0);
 	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 	ret = ext4_convert_inline_data(inode);
+	KUNIT_EXPECT_EQ(test, ret, -EIO);
+	KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
+
+	ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	ret = ext4_convert_inline_data(inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
+	KUNIT_EXPECT_FALSE(test,
+			   ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA));
 	KUNIT_EXPECT_MEMEQ(test, ctx->data_block_buf, buf, sizeof(buf));
 
 	/*
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index acdc65ce4..df50b0483 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -921,7 +921,7 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
 	if (IS_ERR(folio))
 		return PTR_ERR(folio);
 
-	down_read(&EXT4_I(inode)->xattr_sem);
+	down_write(&EXT4_I(inode)->xattr_sem);
 	if (!ext4_has_inline_data(inode)) {
 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 		goto out;
@@ -938,7 +938,7 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
 	ret = ext4_block_write_begin(NULL, folio, 0, inline_size,
 				     ext4_da_get_block_prep);
 	if (ret) {
-		up_read(&EXT4_I(inode)->xattr_sem);
+		up_write(&EXT4_I(inode)->xattr_sem);
 		folio_unlock(folio);
 		folio_put(folio);
 		ext4_truncate_failed_write(inode);
@@ -946,12 +946,19 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
 	}
 
 	clear_buffer_new(folio_buffers(folio));
+	/*
+	 * Clear EXT4_STATE_MAY_INLINE_DATA before marking folio 0 dirty so
+	 * concurrent writeback cannot see PAGECACHE_TAG_DIRTY while
+	 * EXT4_STATE_MAY_INLINE_DATA is still set. Holding xattr_sem for
+	 * write prevents ext4_convert_inline_data() from observing the
+	 * cleared state before folio 0 is dirtied.
+	 */
+	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 	folio_mark_dirty(folio);
 	folio_mark_uptodate(folio);
-	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 
 out:
-	up_read(&EXT4_I(inode)->xattr_sem);
+	up_write(&EXT4_I(inode)->xattr_sem);
 	if (folio) {
 		folio_unlock(folio);
 		folio_put(folio);
@@ -1842,10 +1849,18 @@ bool empty_inline_dir(struct inode *dir, int *has_inline_data)
 
 int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
 {
-	int ret, no_expand;
+	int ret = 0, no_expand;
 
 	ext4_write_lock_xattr(inode, &no_expand);
+	if (!ext4_has_inline_data(inode))
+		goto out;
+	/* Only writeback of a completed delayed-allocation handoff may proceed. */
+	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		ret = -EIO;
+		goto out;
+	}
 	ret = ext4_destroy_inline_data_nolock(handle, inode);
+out:
 	ext4_write_unlock_xattr(inode, &no_expand);
 
 	return ret;
@@ -1914,6 +1929,25 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
 	if ((err = ext4_orphan_add(handle, inode)) != 0)
 		goto out;
 
+	/*
+	 * If the file was already moved to delayed allocation
+	 * (!EXT4_STATE_MAY_INLINE_DATA) and is now truncated to zero,
+	 * truncate_pagecache() has already dropped dirty folio 0. Finish
+	 * converting the empty inode out of inline data here instead of
+	 * leaving EXT4_INODE_INLINE_DATA set with !EXT4_STATE_MAY_INLINE_DATA
+	 * and no dirty pages.
+	 */
+	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
+	    inode->i_size == 0) {
+		down_write(&EXT4_I(inode)->i_data_sem);
+		EXT4_I(inode)->i_disksize = 0;
+		ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
+		up_write(&EXT4_I(inode)->i_data_sem);
+
+		err = ext4_destroy_inline_data_nolock(handle, inode);
+		goto out;
+	}
+
 	if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
 		goto out;
 
@@ -1922,20 +1956,19 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
 	inline_size = ext4_get_inline_size(inode);
 	EXT4_I(inode)->i_disksize = i_size;
 
-	if (i_size < inline_size) {
-		/*
-		 * if there's inline data to truncate and this file was
-		 * converted to extents after that inline data was written,
-		 * the extent status cache must be cleared to avoid leaving
-		 * behind stale delayed allocated extent entries
-		 */
-		if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
-			ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, i_size);
+	/*
+	 * If this file was moved to delayed allocation
+	 * (!EXT4_STATE_MAY_INLINE_DATA), remove delayed extents beyond the
+	 * new i_size while keeping block 0 if i_size > 0 (folio 0 is still
+	 * dirty in the page cache).
+	 */
+	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, inode->i_size);
 
-			ext4_es_remove_extent(inode, lblk,
-					      EXT_MAX_BLOCKS - lblk);
-		}
+		ext4_es_remove_extent(inode, lblk, EXT_MAX_BLOCKS - lblk);
+	}
 
+	if (i_size < inline_size) {
 		/* Clear the content in the xattr space. */
 		if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
 			if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
@@ -2005,22 +2038,40 @@ int ext4_convert_inline_data(struct inode *inode)
 	handle_t *handle;
 	struct ext4_iloc iloc;
 
+retry:
+	if (!ext4_has_inline_data(inode) &&
+	    !ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
+		return 0;
+
+	down_read(&EXT4_I(inode)->xattr_sem);
 	if (!ext4_has_inline_data(inode)) {
 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		up_read(&EXT4_I(inode)->xattr_sem);
 		return 0;
-	} else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+	}
+	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		up_read(&EXT4_I(inode)->xattr_sem);
 		/*
 		 * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
 		 * cleared. This means we are in the middle of moving of
-		 * inline data to delay allocated block. Just force writeout
-		 * here to finish conversion.
+		 * inline data to delay allocated block. Force synchronous
+		 * writeout here to finish conversion.
 		 */
-		error = filemap_flush(inode->i_mapping);
+		error = filemap_write_and_wait(inode->i_mapping);
 		if (error)
 			return error;
-		if (!ext4_has_inline_data(inode))
+		down_read(&EXT4_I(inode)->xattr_sem);
+		if (!ext4_has_inline_data(inode)) {
+			up_read(&EXT4_I(inode)->xattr_sem);
 			return 0;
+		}
+		up_read(&EXT4_I(inode)->xattr_sem);
+		error = ext4_emergency_state(inode->i_sb);
+		if (error)
+			return error;
+		return -EIO;
 	}
+	up_read(&EXT4_I(inode)->xattr_sem);
 
 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
 
@@ -2036,8 +2087,15 @@ int ext4_convert_inline_data(struct inode *inode)
 	}
 
 	ext4_write_lock_xattr(inode, &no_expand);
-	if (ext4_has_inline_data(inode))
+	if (ext4_has_inline_data(inode)) {
+		if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+			ext4_write_unlock_xattr(inode, &no_expand);
+			ext4_journal_stop(handle);
+			brelse(iloc.bh);
+			goto retry;
+		}
 		error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
+	}
 	ext4_write_unlock_xattr(inode, &no_expand);
 	ext4_journal_stop(handle);
 out_free:
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 07a62d856..f27aeae02 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3019,16 +3019,20 @@ static int ext4_do_writepages(struct mpage_da_data *mpd)
 	 * we'd better clear the inline data here.
 	 */
 	if (ext4_has_inline_data(inode)) {
+		int stop_err;
+
 		/* Just inode will be modified... */
 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
 		if (IS_ERR(handle)) {
 			ret = PTR_ERR(handle);
 			goto out_writepages;
 		}
-		BUG_ON(ext4_test_inode_state(inode,
-				EXT4_STATE_MAY_INLINE_DATA));
-		ext4_destroy_inline_data(handle, inode);
-		ext4_journal_stop(handle);
+		ret = ext4_destroy_inline_data(handle, inode);
+		stop_err = ext4_journal_stop(handle);
+		if (!ret)
+			ret = stop_err;
+		if (ret)
+			goto out_writepages;
 	}
 
 	/*
@@ -3437,9 +3441,15 @@ static void ext4_da_truncate_failed_write(struct inode *inode)
 
 	filemap_invalidate_lock(mapping);
 	truncate_inode_pages(mapping, inode->i_size);
+	if (inode->i_size == 0 && ext4_has_inline_data(inode) &&
+	    !ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		if (!ext4_truncate(inode))
+			goto out_unlock;
+	}
 	down_write(&EXT4_I(inode)->i_data_sem);
 	ext4_es_remove_extent(inode, lblk, EXT_MAX_BLOCKS - lblk);
 	up_write(&EXT4_I(inode)->i_data_sem);
+out_unlock:
 	filemap_invalidate_unlock(mapping);
 }
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 3/5] ext4: keep EXT4_STATE_MAY_INLINE_DATA set during inline conversion
  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 ` 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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Arbab Haider @ 2026-11-26  7:07 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	yebin10, linux-ext4, linux-kernel

In ext4_convert_inline_data_nolock(), ext4_destroy_inline_data_nolock()
cleared both EXT4_INODE_INLINE_DATA and EXT4_STATE_MAY_INLINE_DATA and
dropped i_data_sem before ext4_map_blocks() allocated and initialized
block 0.  When ext4_page_mkwrite() invokes ext4_convert_inline_data()
without holding i_rwsem, two classes of races occur during that window:

1. A concurrent ext4_da_write_begin() sees !EXT4_STATE_MAY_INLINE_DATA,
   bypasses xattr_sem, and dirties folio 0 before ext4_map_blocks() fails
   (e.g. -ENOSPC) and ext4_restore_inline_data() restores
   EXT4_INODE_INLINE_DATA and EXT4_STATE_MAY_INLINE_DATA, triggering
   BUG_ON(ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) in
   ext4_do_writepages().
2. Callers that check ext4_has_inline_data() without xattr_sem while
   holding only i_rwsem or folio_lock can observe the transiently cleared
   EXT4_INODE_INLINE_DATA flag before block 0 is initialized or rollback
   completes:
   - ext4_convert_inline_data_to_extent() and ext4_prepare_inline_data()
     can misroute writes or recreate inline data after conversion.
   - ext4_dio_read_iter() and ext4_dio_write_iter() can bypass the
     buffered I/O fallback, clear EXT4_STATE_MAY_INLINE_DATA after a
     rollback, or hit WARN_ON_ONCE(ext4_has_inline_data()) in
     ext4_iomap_begin().
   - ext4_setattr() can skip ext4_convert_inline_data() and persist
     i_size > i_inline_size after rollback.
   - ext4_block_zero_eof() can enter ext4_block_zero_range(), failing
     with -ERANGE, aliasing JBD2 buffers in data=journal mode, or
     dirtying folio 0 across rollback.
   - ext4_bmap() can hit WARN_ON_ONCE(ext4_has_inline_data()) in
     ext4_iomap_begin().
   - ext4_iomap_begin_report() (FIEMAP / SEEK_DATA / SEEK_HOLE) can read
     ei->i_data[] as indirect block pointers after rollback, triggering
     EXT4_ERROR_INODE().
   - ext4_read_folio() and ext4_readahead() can cache a zeroed folio 0 or
     read stale on-disk block contents.

Keep EXT4_STATE_MAY_INLINE_DATA set during
ext4_convert_inline_data_nolock() until ext4_map_blocks() and block
initialization succeed (clearing it on unrecoverable failure in
ext4_destroy_inline_data_nolock() / ext4_restore_inline_data(), and
clearing it on success in ext4_convert_inline_data_to_extent()).
Serialize the inline-data checks with xattr_sem in
ext4_convert_inline_data_to_extent(), ext4_prepare_inline_data(),
ext4_dio_read_iter(), ext4_dio_write_iter() (respecting IOCB_NOWAIT),
ext4_setattr(), ext4_block_zero_eof(), ext4_bmap(), and ext4_readahead()
(preserving lockless fast paths on converted inodes and preserving
readahead for sparse files extended from empty inodes), and check
EXT4_STATE_MAY_INLINE_DATA alongside ext4_has_inline_data() in
ext4_iomap_begin_report() and ext4_read_folio().

Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/ext4/file.c     | 73 ++++++++++++++++++++++++++++++++++++----------
 fs/ext4/inline.c   | 14 ++++++++-
 fs/ext4/inode.c    | 53 ++++++++++++++++++++++++---------
 fs/ext4/readpage.c | 15 ++++++++--
 4 files changed, 121 insertions(+), 34 deletions(-)

diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index cfb22d0d5..81412b38e 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -70,6 +70,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
 	ssize_t ret;
 	struct inode *inode = file_inode(iocb->ki_filp);
+	bool use_dio, check_inline;
 
 	if (iocb->ki_flags & IOCB_NOWAIT) {
 		if (!inode_trylock_shared(inode))
@@ -78,7 +79,23 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 		inode_lock_shared(inode);
 	}
 
-	if (!ext4_should_use_dio(iocb, to)) {
+	check_inline = ext4_has_inline_data(inode) ||
+		ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	if (check_inline) {
+		if (iocb->ki_flags & IOCB_NOWAIT) {
+			if (!down_read_trylock(&EXT4_I(inode)->xattr_sem)) {
+				inode_unlock_shared(inode);
+				return -EAGAIN;
+			}
+		} else {
+			down_read(&EXT4_I(inode)->xattr_sem);
+		}
+	}
+	use_dio = ext4_should_use_dio(iocb, to);
+	if (check_inline)
+		up_read(&EXT4_I(inode)->xattr_sem);
+
+	if (!use_dio) {
 		inode_unlock_shared(inode);
 		/*
 		 * Fallback to buffered I/O if the operation being performed on
@@ -633,22 +650,46 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	}
 
 	/* Fallback to buffered I/O if the inode does not support direct I/O. */
-	if (!ext4_should_use_dio(iocb, from)) {
-		if (ilock_shared)
-			inode_unlock_shared(inode);
-		else
-			inode_unlock(inode);
-		return ext4_buffered_write_iter(iocb, from);
-	}
+	{
+		bool check_inline = ext4_has_inline_data(inode) ||
+			ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+
+		if (check_inline) {
+			if (iocb->ki_flags & IOCB_NOWAIT) {
+				if (!down_read_trylock(&EXT4_I(inode)->xattr_sem)) {
+					if (ilock_shared)
+						inode_unlock_shared(inode);
+					else
+						inode_unlock(inode);
+					return -EAGAIN;
+				}
+			} else {
+				down_read(&EXT4_I(inode)->xattr_sem);
+			}
+		}
+		if (!ext4_should_use_dio(iocb, from)) {
+			if (check_inline)
+				up_read(&EXT4_I(inode)->xattr_sem);
+			if (ilock_shared)
+				inode_unlock_shared(inode);
+			else
+				inode_unlock(inode);
+			return ext4_buffered_write_iter(iocb, from);
+		}
 
-	/*
-	 * Prevent inline data from being created since we are going to allocate
-	 * blocks for DIO. We know the inode does not currently have inline data
-	 * because ext4_should_use_dio() checked for it, but we have to clear
-	 * the state flag before the write checks because a lock cycle could
-	 * introduce races with other writers.
-	 */
-	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		/*
+		 * Prevent inline data from being created since we are going to
+		 * allocate blocks for DIO. We know the inode does not currently
+		 * have inline data because ext4_should_use_dio() checked for it
+		 * under xattr_sem, but we have to clear the state flag before
+		 * releasing xattr_sem and running the write checks because a
+		 * lock cycle could introduce races with other writers or inline
+		 * data rollback.
+		 */
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		if (check_inline)
+			up_read(&EXT4_I(inode)->xattr_sem);
+	}
 
 	ret = ext4_dio_write_checks(iocb, from, &ilock_shared, &extend,
 				    &dio_flags);
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index df50b0483..9270a2f4c 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -428,6 +428,10 @@ static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
 		return -ENOSPC;
 
 	ext4_write_lock_xattr(inode, &no_expand);
+	if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		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
@@ -507,7 +511,8 @@ static int ext4_destroy_inline_data_nolock(handle_t *handle,
 
 	EXT4_I(inode)->i_inline_off = 0;
 	EXT4_I(inode)->i_inline_size = 0;
-	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	if (error)
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 out:
 	brelse(is.iloc.bh);
 	if (error == -ENODATA)
@@ -595,14 +600,17 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
 	unsigned from, to;
 	struct ext4_iloc iloc;
 
+	down_read(&EXT4_I(inode)->xattr_sem);
 	if (!ext4_has_inline_data(inode)) {
 		/*
 		 * clear the flag so that no new write
 		 * will trap here again.
 		 */
 		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+		up_read(&EXT4_I(inode)->xattr_sem);
 		return 0;
 	}
+	up_read(&EXT4_I(inode)->xattr_sem);
 
 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
 
@@ -647,6 +655,7 @@ static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
 	ret = ext4_destroy_inline_data_nolock(handle, inode);
 	if (ret)
 		goto out;
+	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 
 	if (ext4_should_dioread_nolock(inode)) {
 		ret = ext4_block_write_begin(handle, folio, from, to,
@@ -1114,6 +1123,7 @@ static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
 		ext4_msg(inode->i_sb, KERN_EMERG,
 			"error restoring inline_data for inode -- potential data loss! (inode %llu, error %d)",
 			inode->i_ino, ret);
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 		return;
 	}
 	ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
@@ -1214,6 +1224,8 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,
 out_restore:
 	if (error)
 		ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
+	else
+		ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
 
 out:
 	brelse(data_bh);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index f27aeae02..f5d0d7939 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -3613,8 +3613,16 @@ static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
 	/*
 	 * We can get here for an inline file via the FIBMAP ioctl
 	 */
-	if (ext4_has_inline_data(inode))
-		goto out;
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		bool has_inline;
+
+		down_read(&EXT4_I(inode)->xattr_sem);
+		has_inline = ext4_has_inline_data(inode);
+		up_read(&EXT4_I(inode)->xattr_sem);
+		if (has_inline)
+			goto out;
+	}
 
 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
 	    (test_opt(inode->i_sb, DELALLOC) ||
@@ -4117,7 +4125,8 @@ static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
 	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
 		return -EINVAL;
 
-	if (ext4_has_inline_data(inode)) {
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
 		ret = ext4_inline_data_iomap(inode, iomap);
 		if (ret != -EAGAIN) {
 			if (ret == 0 && offset >= iomap->length)
@@ -5104,13 +5113,21 @@ int ext4_block_zero_eof(struct inode *inode, loff_t from, loff_t end)
 	if (!offset || from >= end)
 		return 0;
 	/*
-	 * Inline data has no tail block to zero out.  Note that a race with
-	 * ext4_page_mkwrite() converting inline data to an extent without
-	 * holding i_rwsem is safe, as that path zeroes the full block before
-	 * copying in the inline data.
+	 * Inline data has no tail block to zero out.  Serialize against a
+	 * concurrent ext4_page_mkwrite() inline data conversion so we do not
+	 * observe transiently cleared EXT4_INODE_INLINE_DATA before block 0 is
+	 * initialized or rolled back.
 	 */
-	if (ext4_has_inline_data(inode))
-		return 0;
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		bool has_inline;
+
+		down_read(&EXT4_I(inode)->xattr_sem);
+		has_inline = ext4_has_inline_data(inode);
+		up_read(&EXT4_I(inode)->xattr_sem);
+		if (has_inline)
+			return 0;
+	}
 	/* If we are processing an encrypted inode during orphan list handling */
 	if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
 		return 0;
@@ -7246,11 +7263,19 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 * convert to extent-based storage first to prevent inconsistent
 		 * state (inline flag set but size exceeds inline capacity).
 		 */
-		if (ext4_has_inline_data(inode) &&
-		    attr->ia_size > EXT4_I(inode)->i_inline_size) {
-			error = ext4_convert_inline_data(inode);
-			if (error)
-				return error;
+		if (ext4_has_inline_data(inode) ||
+		    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+			bool convert_inline;
+
+			down_read(&EXT4_I(inode)->xattr_sem);
+			convert_inline = ext4_has_inline_data(inode) &&
+				attr->ia_size > EXT4_I(inode)->i_inline_size;
+			up_read(&EXT4_I(inode)->xattr_sem);
+			if (convert_inline) {
+				error = ext4_convert_inline_data(inode);
+				if (error)
+					return error;
+			}
 		}
 
 		if (shrink) {
diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c
index c7b6cdb2e..4ebe985c6 100644
--- a/fs/ext4/readpage.c
+++ b/fs/ext4/readpage.c
@@ -326,7 +326,8 @@ int ext4_read_folio(struct file *file, struct folio *folio)
 
 	trace_ext4_read_folio(inode, folio);
 
-	if (ext4_has_inline_data(inode)) {
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
 		ret = ext4_readpage_inline(inode, folio);
 		if (ret != -EAGAIN)
 			return ret;
@@ -345,8 +346,16 @@ void ext4_readahead(struct readahead_control *rac)
 	struct fsverity_info *vi = NULL;
 
 	/* If the file has inline data, no need to do readahead. */
-	if (ext4_has_inline_data(inode))
-		return;
+	if (ext4_has_inline_data(inode) ||
+	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
+		bool has_inline;
+
+		down_read(&EXT4_I(inode)->xattr_sem);
+		has_inline = ext4_has_inline_data(inode);
+		up_read(&EXT4_I(inode)->xattr_sem);
+		if (has_inline)
+			return;
+	}
 
 	if (readahead_index(rac) < DIV_ROUND_UP(inode->i_size, PAGE_SIZE))
 		vi = fsverity_get_info(inode);
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 4/5] ext4: re-check inline data under filemap_invalidate_lock in swap_inode_boot_loader
  2026-11-26  7:07 [PATCH v4 0/5] ext4: fix inline data conversion races Arbab Haider
                   ` (2 preceding siblings ...)
  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 ` Arbab Haider
  2026-11-26  7:07 ` [PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion Arbab Haider
  2026-11-26  7:32 ` [PATCH v4 0/5] ext4: fix inline data conversion races sashiko-bot
  5 siblings, 0 replies; 7+ messages in thread
From: Arbab Haider @ 2026-11-26  7:07 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	yebin10, linux-ext4, linux-kernel

swap_inode_boot_loader() checks ext4_has_inline_data(inode) while holding
only i_rwsem (via lock_two_nondirectories()), before acquiring
filemap_invalidate_lock(inode->i_mapping).

A concurrent ext4_page_mkwrite() -> ext4_convert_inline_data_nolock()
(which holds filemap_invalidate_lock_shared, not i_rwsem) transiently
clears EXT4_INODE_INLINE_DATA before ext4_map_blocks().  If
swap_inode_boot_loader() observes !ext4_has_inline_data(inode), waits on
filemap_invalidate_lock(), and ext4_convert_inline_data_nolock() then
rolls back via ext4_restore_inline_data() on -ENOSPC,
swap_inode_boot_loader() proceeds to swap_inode_data() on the still-inline
inode.  Because EXT4_INLINE_DATA_FL is not in EXT4_FL_SHOULD_SWAP, this
swaps raw inline data in ei->i_data with EXT4_BOOT_LOADER_INO's extent
header and corrupts both inodes on disk.

Re-check ext4_has_inline_data(inode) after acquiring
filemap_invalidate_lock(inode->i_mapping).

Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
---
 fs/ext4/ioctl.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 83630cd40..e0c53d62d 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -419,6 +419,10 @@ static long swap_inode_boot_loader(struct super_block *sb,
 	}
 
 	filemap_invalidate_lock(inode->i_mapping);
+	if (ext4_has_inline_data(inode)) {
+		err = -EINVAL;
+		goto err_out;
+	}
 	err = filemap_write_and_wait(inode->i_mapping);
 	if (err)
 		goto err_out;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion
  2026-11-26  7:07 [PATCH v4 0/5] ext4: fix inline data conversion races Arbab Haider
                   ` (3 preceding siblings ...)
  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
  2026-11-26  7:32 ` [PATCH v4 0/5] ext4: fix inline data conversion races sashiko-bot
  5 siblings, 0 replies; 7+ messages in thread
From: Arbab Haider @ 2026-11-26  7:07 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	yebin10, linux-ext4, linux-kernel

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/5] ext4: fix inline data conversion races
  2026-11-26  7:07 [PATCH v4 0/5] ext4: fix inline data conversion races Arbab Haider
                   ` (4 preceding siblings ...)
  2026-11-26  7:07 ` [PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion Arbab Haider
@ 2026-11-26  7:32 ` sashiko-bot
  5 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-26  7:32 UTC (permalink / raw)
  To: Arbab Haider; +Cc: linux-ext4, tytso

Sashiko has reviewed this 5-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126070748.272109-2-arbabhaider649@gmail.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-26  7:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v4 5/5] ext4: serialize inode migration against concurrent inline data conversion Arbab Haider
2026-11-26  7:32 ` [PATCH v4 0/5] ext4: fix inline data conversion races sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox