Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking
@ 2026-11-30 23:05 Xing Ma
  2026-11-30 23:05 ` [PATCH v3 1/2] ext4: fall back to full commit for unlinked or orphan inodes in fast commit Xing Ma
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Xing Ma @ 2026-11-30 23:05 UTC (permalink / raw)
  To: linux-ext4
  Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, Baokun Li,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Harshad Shirwadkar

This 2-patch series fixes two crash-recovery bugs in ext4 fast commit
on tytso/ext4.git dev (aceadbccd428f):

1. Patch 1 ("ext4: fall back to full commit for unlinked or orphan
   inodes in fast commit"):
   Fast commit does not log orphan list updates.  Snapshotting an
   unlinked open inode (i_nlink == 0) emits EXT4_FC_TAG_INODE with
   i_links_count == 0, which causes ext4_fc_replay_inode() to fail
   ext4_iget() with -ESTALE -> -EFSCORRUPTED and abort journal recovery.
   Snapshotting a mid-truncate orphan inode logs shrunk i_size with
   unfreed extents.  Fall back cleanly to a full JBD2 commit via
   -ECANCELED, and add KUnit coverage in fast_commit-test.c.

2. Patch 2 ("ext4: track truncated block range in ext4_truncate() for
   fast commit"):
   ext4_fc_track_range() for truncation is only called in
   ext4_truncate_down(), missing same-size ftruncate(fd, oldsize)
   dropping FALLOC_FL_KEEP_SIZE post-EOF blocks, cross-transaction
   ext4_truncate() handles, and handle restarts in
   ext4_ext_remove_space() / ext4_ind_truncate().  Insert an
   EXTENT_STATUS_HOLE entry for [last_block, EXT_MAX_BLOCKS - 1] in
   ext4_ext_truncate() (when fast commit is enabled and the inode is
   still linked) and call ext4_fc_track_range() in ext4_truncate().

Changes since v2 (addressing Sashiko AI review):
- Patch 1: unchanged (no issues found).
- Patch 2:
  * In ext4_ext_truncate(), insert EXTENT_STATUS_HOLE covering
    [last_block, EXT_MAX_BLOCKS - 1] after ext4_ext_remove_space()
    succeeds (when JOURNAL_FAST_COMMIT is enabled and i_nlink != 0),
    matching ext4_punch_hole().  Without this hole entry,
    ext4_fc_snapshot_inode_data() hits EXT4_FC_SNAP_ERR_ES_MISS and
    falls back to a full commit (even on a no-op same-size ftruncate()
    + fsync()).
  * Track [last_block, EXT_MAX_BLOCKS - 1] in ext4_truncate() (matching
    ext4_ext_truncate()'s exact last_block calculation) guarded by
    last_block < EXT_MAX_BLOCKS.

Changes since v1 (addressing Sashiko AI review):
- Patch 1: unchanged (no issues found).
- Patch 2:
  * Move ext4_fc_track_range() after `if (err) goto out_stop;` and
    guard it with `!ext4_handle_is_aborted(handle)`.
  * Call ext4_fc_track_range() for both extent-based and indirect-mapped
    inodes; indirect-mapped inodes have no hole entry inserted and
    continue to fall back safely to a full commit.

Testing on tytso/ext4.git dev (aceadbccd428f, -O fast_commit):
- Patch 1: xfstests generic/764 fails 100% before ("Structure needs
  cleaning" / "JBD2: journal recovery failed") and passes after;
  KUnit ext4_fast_commit_test 11/11 PASS.
- Patch 2: verified via /proc/fs/ext4/<dev>/fc_info that same-size
  ftruncate() (both dropping 64K FALLOC_FL_KEEP_SIZE post-EOF blocks
  and no-op same-size ftruncate(), warm and cold cache), truncate-to-0,
  and shrinking truncate all commit via fast commit (commits++,
  ineligible unchanged, es_miss == 0) and survive dm-flakey crash
  recovery with expected block counts and clean e2fsck -fn.

Xing Ma (2):
  ext4: fall back to full commit for unlinked or orphan inodes in fast
    commit
  ext4: track truncated block range in ext4_truncate() for fast commit

 fs/ext4/extents.c          |  5 +++++
 fs/ext4/fast_commit-test.c | 18 ++++++++++++++++++
 fs/ext4/fast_commit.c      | 17 +++++++++++++++++
 fs/ext4/inode.c            | 10 ++++++++++
 4 files changed, 50 insertions(+)


base-commit: aceadbccd428fb8e6593b27a3091d961f80225d1
-- 
2.39.5

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

* [PATCH v3 1/2] ext4: fall back to full commit for unlinked or orphan inodes in fast commit
  2026-11-30 23:05 [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking Xing Ma
@ 2026-11-30 23:05 ` Xing Ma
  2026-11-30 23:06 ` [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for " Xing Ma
  2026-11-30 23:28 ` [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Xing Ma @ 2026-11-30 23:05 UTC (permalink / raw)
  To: linux-ext4
  Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, Baokun Li,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Harshad Shirwadkar,
	stable

Fast commit does not log orphan list updates (s_last_orphan or orphan
file).  When a fast commit snapshots a tracked inode that is unlinked
or on the orphan list, crash recovery fails in two ways:

1. Unlinked open inode (open+unlink, overwrite rename over an open
   file, or O_TMPFILE): the inode has i_nlink == 0 and stays on s_fc_q
   until closed.  Emitting EXT4_FC_TAG_INODE with i_links_count == 0
   causes ext4_fc_replay_inode() to reload it via ext4_iget() without
   EXT4_ORPHAN_FS, turning -ESTALE into -EFSCORRUPTED and aborting
   journal recovery ("JBD2: journal recovery failed").  This is
   deterministically reproduced by xfstests generic/764 with
   -O fast_commit.

2. Mid-truncate orphan inode: ext4_truncate_down() adds the inode to
   the orphan list, shrinks i_size/i_disksize, and stops its handle
   before ext4_truncate() frees blocks.  Snapshotting the inode in
   this window logs the shrunk i_size with unfreed extents and no
   orphan entry, causing e2fsck to report "end of extent exceeds
   allowed value".

Return -ECANCELED from ext4_fc_snapshot_inode() when i_nlink == 0,
ext4_inode_orphan_tracked(inode) is true, or raw i_links_count == 0 so
ext4_fc_commit() falls back to a full JBD2 commit.  Normal unlinks of
closed files and completed truncates remain fast-commit eligible.

Also add KUnit coverage in test_fc_snapshot_inode_and_ranges() for
i_nlink == 0 and EXT4_STATE_ORPHAN_FILE fallback.

Fixes: 8016e29f4362 ("ext4: fast commit recovery path")
Cc: stable@vger.kernel.org
Signed-off-by: Xing Ma <maxing.lan@bytedance.com>
---
 fs/ext4/fast_commit-test.c | 18 ++++++++++++++++++
 fs/ext4/fast_commit.c      | 17 +++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/fs/ext4/fast_commit-test.c b/fs/ext4/fast_commit-test.c
index 07cb9c043cd2c..2d342cabb4cf7 100644
--- a/fs/ext4/fast_commit-test.c
+++ b/fs/ext4/fast_commit-test.c
@@ -1752,6 +1752,24 @@ static void test_fc_snapshot_inode_and_ranges(struct kunit *test)
 			  ext4_test_inode_state(file2, EXT4_STATE_FC_COMMITTING));
 	KUNIT_EXPECT_NOT_NULL(test, EXT4_I(file2)->i_fc_snap);
 	kvfree(inodes);
+
+	/*
+	 * 8) Unlinked (i_nlink == 0) or orphan-tracked inodes force fallback
+	 *    via -ECANCELED.
+	 */
+	clear_nlink(file1);
+	KUNIT_EXPECT_EQ(test,
+			ext4_fc_snapshot_inode_test(file1, 0, &nr_ranges,
+						    &snap_err),
+			-ECANCELED);
+	set_nlink(file1, 1);
+
+	ext4_set_inode_state(file1, EXT4_STATE_ORPHAN_FILE);
+	KUNIT_EXPECT_EQ(test,
+			ext4_fc_snapshot_inode_test(file1, 0, &nr_ranges,
+						    &snap_err),
+			-ECANCELED);
+	ext4_clear_inode_state(file1, EXT4_STATE_ORPHAN_FILE);
 }
 
 /* =========================================================================
diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index 9beb423b208f8..fd98f7b54fc0f 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -1119,6 +1119,18 @@ static int ext4_fc_snapshot_inode(struct inode *inode,
 	int ret;
 	int alloc_ctx;
 
+	/*
+	 * Fast commit does not log orphan list updates, and
+	 * ext4_fc_replay_inode() reloads replayed inodes with
+	 * EXT4_IGET_NORMAL, which rejects i_nlink == 0 inodes with
+	 * -ESTALE (causing journal recovery to abort with -EFSCORRUPTED).
+	 * Fall back to a full commit whenever a tracked inode is
+	 * currently unlinked or on the orphan list (e.g., open-unlinked,
+	 * rename-overwritten while open, O_TMPFILE, or mid-truncate).
+	 */
+	if (inode->i_nlink == 0 || ext4_inode_orphan_tracked(inode))
+		return -ECANCELED;
+
 	ret = ext4_get_inode_loc_noio(inode, &iloc);
 	if (ret) {
 		atomic64_inc(&stats->snap_fail_inode_loc);
@@ -1126,6 +1138,11 @@ static int ext4_fc_snapshot_inode(struct inode *inode,
 		return ret;
 	}
 
+	if (le16_to_cpu(ext4_raw_inode(&iloc)->i_links_count) == 0) {
+		brelse(iloc.bh);
+		return -ECANCELED;
+	}
+
 	if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA))
 		inode_len = EXT4_INODE_SIZE(inode->i_sb);
 	else if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE)
-- 
2.39.5

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

* [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for fast commit
  2026-11-30 23:05 [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking Xing Ma
  2026-11-30 23:05 ` [PATCH v3 1/2] ext4: fall back to full commit for unlinked or orphan inodes in fast commit Xing Ma
@ 2026-11-30 23:06 ` Xing Ma
  2026-11-30 23:28   ` sashiko-bot
  2026-11-30 23:28 ` [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Xing Ma @ 2026-11-30 23:06 UTC (permalink / raw)
  To: linux-ext4
  Cc: Theodore Ts'o, Andreas Dilger, Jan Kara, Baokun Li,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Harshad Shirwadkar,
	stable

Truncation range tracking (ext4_fc_track_range()) is currently only
called in ext4_truncate_down(), which misses three cases where
ext4_truncate() frees extents:

1. Same-size ftruncate(fd, oldsize) dropping FALLOC_FL_KEEP_SIZE
   preallocated blocks beyond EOF jumps straight to ext4_truncate()
   without calling ext4_fc_track_range().
2. If a commit occurs between ext4_truncate_down() stopping its handle
   and ext4_truncate() starting a new handle in transaction T+1,
   ext4_fc_track_inode() resets i_fc_lblk_len to 0 and freed extents
   are not tracked.
3. If ext4_ext_remove_space() or ext4_ind_truncate() restarts the
   journal handle onto a new transaction, earlier tracking belonged to
   the previous transaction.

In all three cases, a subsequent fast commit omits EXT4_FC_TAG_DEL_RANGE
and resurrects freed extents (or emits an indirect-mapped inode without
falling back to a full commit) after crash recovery.  Case 1 reproduces
deterministically on -O fast_commit:

  xfs_io -f -c "pwrite -S 0xaa 0 4k" -c "falloc -k 4k 64k" mnt/foo
  sync
  xfs_io -c "truncate 4k" -c "fsync" mnt/foo
  # crash (dm-flakey drop_writes) and remount:
  # stat -c %b mnt/foo returns 136 instead of 8

Furthermore, unlike ext4_punch_hole(), ext4_ext_truncate() removes
extent status entries for [last_block, EXT_MAX_BLOCKS - 1] without
inserting an EXTENT_STATUS_HOLE entry.  Tracking that range without
populating the hole entry causes ext4_fc_snapshot_inode_data() to fail
with EXT4_FC_SNAP_ERR_ES_MISS and fall back to a full commit (even on a
no-op same-size ftruncate() + fsync()).

In ext4_ext_truncate(), insert an EXTENT_STATUS_HOLE entry covering
[last_block, EXT_MAX_BLOCKS - 1] after ext4_ext_remove_space() succeeds
when JOURNAL_FAST_COMMIT is enabled and the inode is still linked.  In
ext4_truncate(), call ext4_fc_track_range() for [last_block,
EXT_MAX_BLOCKS - 1] after checking err and !ext4_handle_is_aborted().
Extent-based inodes then emit EXT4_FC_TAG_DEL_RANGE cleanly via fast
commit, while indirect-mapped inodes have no hole entry inserted and
continue to fall back safely to a full commit.

Fixes: aa75f4d3daae ("ext4: main fast-commit commit path")
Cc: stable@vger.kernel.org
Signed-off-by: Xing Ma <maxing.lan@bytedance.com>
---
 fs/ext4/extents.c |  5 +++++
 fs/ext4/inode.c   | 10 ++++++++++
 2 files changed, 15 insertions(+)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index b65b0261ee5c3..a47b56e3cbe9c 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4608,6 +4608,11 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode)
 		memalloc_retry_wait(GFP_ATOMIC);
 		goto retry_remove_space;
 	}
+	if (!err && inode->i_nlink && test_opt2(sb, JOURNAL_FAST_COMMIT) &&
+	    last_block < EXT_MAX_BLOCKS)
+		ext4_es_insert_extent(inode, last_block,
+				      EXT_MAX_BLOCKS - last_block, ~0,
+				      EXTENT_STATUS_HOLE, 0);
 	return err;
 }
 
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 07a62d8569d31..78823397d4245 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5608,6 +5608,16 @@ int ext4_truncate(struct inode *inode)
 	if (err)
 		goto out_stop;
 
+	if (!ext4_handle_is_aborted(handle)) {
+		ext4_lblk_t last_block;
+
+		last_block = (inode->i_size + inode->i_sb->s_blocksize - 1)
+				>> inode->i_blkbits;
+		if (last_block < EXT_MAX_BLOCKS)
+			ext4_fc_track_range(handle, inode, last_block,
+					    EXT_MAX_BLOCKS - 1);
+	}
+
 	if (IS_SYNC(inode))
 		ext4_handle_sync(handle);
 
-- 
2.39.5

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

* Re: [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for fast commit
  2026-11-30 23:06 ` [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for " Xing Ma
@ 2026-11-30 23:28   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-30 23:28 UTC (permalink / raw)
  To: Xing Ma, linux-ext4; +Cc: tytso

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ext4: range mismatch between ext4_truncate_down() and
  ext4_ext_truncate()
--

commit e0b0a96d89503d8b723098577fe6474607943c18
Author: Xing Ma <maxing.lan@bytedance.com>

ext4: track truncated block range in ext4_truncate() for fast commit

This commit inserts an EXTENT_STATUS_HOLE entry in ext4_ext_truncate() and
calls ext4_fc_track_range() in ext4_truncate() so that truncated ranges are
tracked for fast commit.

> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
> index b65b0261ee5c3..a47b56e3cbe9c 100644
> --- a/fs/ext4/extents.c
> +++ b/fs/ext4/extents.c
> @@ -4608,6 +4608,11 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode)
>  		memalloc_retry_wait(GFP_ATOMIC);
>  		goto retry_remove_space;
>  	}
> +	if (!err && inode->i_nlink && test_opt2(sb, JOURNAL_FAST_COMMIT) &&
> +	    last_block < EXT_MAX_BLOCKS)
> +		ext4_es_insert_extent(inode, last_block,
> +				      EXT_MAX_BLOCKS - last_block, ~0,
> +				      EXTENT_STATUS_HOLE, 0);
>  	return err;
>  }
>  
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 07a62d8569d31..78823397d4245 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -5608,6 +5608,16 @@ int ext4_truncate(struct inode *inode)
>  	if (err)
>  		goto out_stop;
>  
> +	if (!ext4_handle_is_aborted(handle)) {
> +		ext4_lblk_t last_block;
> +
> +		last_block = (inode->i_size + inode->i_sb->s_blocksize - 1)
> +				>> inode->i_blkbits;
> +		if (last_block < EXT_MAX_BLOCKS)
> +			ext4_fc_track_range(handle, inode, last_block,
> +					    EXT_MAX_BLOCKS - 1);
> +	}

[Severity: Medium]
When shrinking a file to a non-zero size, doesn't ext4_truncate_down() still
leave i_fc_lblk_start set to last_block - 1 while ext4_ext_truncate() only
inserts the EXTENT_STATUS_HOLE entry starting at last_block?

In ext4_truncate_down(), start_lblk is computed before calling
ext4_truncate():

fs/ext4/inode.c:ext4_truncate_down() {
    ...
	start_lblk = newsize > 0 ? (newsize - 1) >> inode->i_blkbits : 0;
	ext4_fc_track_range(handle, inode, start_lblk, EXT_MAX_BLOCKS - 1);
    ...
}

For any newsize > 0, start_lblk is last_block - 1 (for example, truncating to
a block-aligned 4K gives start_lblk = 0 and last_block = 1).  When both
handles run in the same transaction, __track_range() merges the two ranges
with min(ei->i_fc_lblk_start, __arg->start), keeping i_fc_lblk_start at
last_block - 1.

If block last_block - 1 is not already cached in i_es_tree (such as on a
block-aligned truncate where ext4_block_zero_eof() is skipped and its extent
was not queried or was reclaimed), ext4_fc_snapshot_inode_data() starts
scanning at last_block - 1, hits EXT4_FC_SNAP_ERR_ES_MISS, and falls back to
a full journal commit.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201070558.4017023-1-maxing.lan@bytedance.com?part=2

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

* Re: [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking
  2026-11-30 23:05 [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking Xing Ma
  2026-11-30 23:05 ` [PATCH v3 1/2] ext4: fall back to full commit for unlinked or orphan inodes in fast commit Xing Ma
  2026-11-30 23:06 ` [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for " Xing Ma
@ 2026-11-30 23:28 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-30 23:28 UTC (permalink / raw)
  To: Xing Ma, linux-ext4; +Cc: tytso

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("ext4: track truncated block range in ext4_truncate()..."): 1
  potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201070558.4017023-1-maxing.lan@bytedance.com

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

end of thread, other threads:[~2026-11-30 23:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 23:05 [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking Xing Ma
2026-11-30 23:05 ` [PATCH v3 1/2] ext4: fall back to full commit for unlinked or orphan inodes in fast commit Xing Ma
2026-11-30 23:06 ` [PATCH v3 2/2] ext4: track truncated block range in ext4_truncate() for " Xing Ma
2026-11-30 23:28   ` sashiko-bot
2026-11-30 23:28 ` [PATCH v3 0/2] ext4: fix fast commit orphan inode replay and truncate range tracking sashiko-bot

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