Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Xing Ma" <maxing.lan@bytedance.com>
To: <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>,
	 "Andreas Dilger" <adilger.kernel@dilger.ca>,
	"Jan Kara" <jack@suse.cz>,
	 "Baokun Li" <libaokun@linux.alibaba.com>,
	 "Ojaswin Mujoo" <ojaswin@linux.ibm.com>,
	 "Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	 "Zhang Yi" <yi.zhang@huawei.com>,
	 "Harshad Shirwadkar" <harshadshirwadkar@gmail.com>,
	 "Xin Yin" <yinxin.x@bytedance.com>, <stable@vger.kernel.org>
Subject: [PATCH v2 3/3] ext4: keep tracking newer transactions while older tid is fc-ineligible
Date: Tue, 01 Dec 2026 01:37:45 +0800	[thread overview]
Message-ID: <20261201013742.1562967-4-maxing.lan@bytedance.com> (raw)
In-Reply-To: <20261201013742.1562967-1-maxing.lan@bytedance.com>

While an ineligible transaction T (with s_fc_ineligible_tid == T) is
performing a full JBD2 commit, EXT4_MF_FC_INELIGIBLE remains set until
ext4_fc_cleanup(journal, full=1, T) runs at the end of that commit.
Meanwhile, JBD2 already allows new handles to run in transaction T + 1.

Because ext4_fc_track_{inode,range,create,link,unlink}() and
ext4_rename() check only EXT4_MF_FC_INELIGIBLE without comparing
handle->h_transaction->t_tid against s_fc_ineligible_tid, updates in
T + 1 silently skip fast commit tracking.  Once T's full commit finishes
and clears EXT4_MF_FC_INELIGIBLE, a subsequent fsync() in T + 1 proceeds
with a fast commit that omits some or all of T + 1's updates:

  - If both range and inode tracking were skipped, fsync() writes an
    empty fast commit and silently loses operations such as truncate.
  - If range tracking was skipped before cleanup cleared the flag and
    inode tracking ran afterward, fast commit replays the shrunk inode
    without DEL_RANGE, leaving stale extents beyond EOF ("end of extent
    exceeds allowed value" in e2fsck).

In addition, ext4_fc_cleanup() cleared EXT4_MF_FC_INELIGIBLE even when
full == 0, which allows jbd2_fc_end_commit_fallback() to clear the flag
before the fallback full commit has even started.

Introduce ext4_fc_handle_eligible() and use it in all fast commit
tracking entry points (including ext4_rename()).  On the slow path when
EXT4_MF_FC_INELIGIBLE is set, re-check under s_fc_lock (serializing
with ext4_fc_mark_ineligible() and ext4_fc_cleanup() so a cleared
s_fc_ineligible_tid == 0 is never passed to tid_gt()) and continue
tracking handles whose TID is newer than s_fc_ineligible_tid.  Also
inline ext4_fc_eligible() into ext4_fc_eligible_test() to avoid an
unused-function warning when CONFIG_EXT4_KUNIT_TESTS=n, and only clear
EXT4_MF_FC_INELIGIBLE in ext4_fc_cleanup() when full is set.

Fixes: e85c81ba8859 ("ext4: fast commit may not fallback for ineligible commit")
Cc: stable@vger.kernel.org
Signed-off-by: Xing Ma <maxing.lan@bytedance.com>
---
 fs/ext4/ext4.h        |  1 +
 fs/ext4/fast_commit.c | 47 ++++++++++++++++++++++++++++++++++---------
 fs/ext4/namei.c       |  4 +---
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index fb595277f2c1c..f7ecd7925e8e3 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3144,6 +3144,7 @@ void ext4_fc_init(struct super_block *sb, journal_t *journal);
 void ext4_fc_init_inode(struct inode *inode);
 void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
 			 ext4_lblk_t end);
+bool ext4_fc_handle_eligible(struct super_block *sb, handle_t *handle);
 void __ext4_fc_track_unlink(handle_t *handle, struct inode *inode,
 	struct dentry *dentry);
 void __ext4_fc_track_link(handle_t *handle, struct inode *inode,
diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c
index f02dca2c9a3a1..6af6c1d37e4b4 100644
--- a/fs/ext4/fast_commit.c
+++ b/fs/ext4/fast_commit.c
@@ -229,10 +229,36 @@ static bool ext4_fc_disabled(struct super_block *sb)
 		(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY));
 }
 
-static bool ext4_fc_eligible(struct super_block *sb)
+bool ext4_fc_handle_eligible(struct super_block *sb, handle_t *handle)
 {
-	return !ext4_fc_disabled(sb) &&
-		!(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE));
+	struct ext4_sb_info *sbi = EXT4_SB(sb);
+	bool eligible;
+	int alloc_ctx;
+
+	if (ext4_fc_disabled(sb))
+		return false;
+	if (!ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))
+		return true;
+	if (!handle || !handle->h_transaction)
+		return false;
+
+	/*
+	 * EXT4_MF_FC_INELIGIBLE may still be set for an older committing
+	 * transaction while new handles are already modifying the next running
+	 * transaction. Re-check under s_fc_lock (which serializes both
+	 * ext4_fc_mark_ineligible() and ext4_fc_cleanup()) so we never compare
+	 * against a cleared s_fc_ineligible_tid == 0, and continue tracking
+	 * handles whose TID is newer than s_fc_ineligible_tid.
+	 */
+	alloc_ctx = ext4_fc_lock(sb);
+	if (!ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))
+		eligible = true;
+	else
+		eligible = tid_gt(handle->h_transaction->t_tid,
+				  sbi->s_fc_ineligible_tid);
+	ext4_fc_unlock(sb, alloc_ctx);
+
+	return eligible;
 }
 
 /*
@@ -522,7 +548,7 @@ void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry)
 {
 	struct inode *inode = d_inode(dentry);
 
-	if (ext4_fc_eligible(inode->i_sb))
+	if (ext4_fc_handle_eligible(inode->i_sb, handle))
 		__ext4_fc_track_unlink(handle, inode, dentry);
 }
 
@@ -543,7 +569,7 @@ void __ext4_fc_track_link(handle_t *handle,
 void ext4_fc_track_link(handle_t *handle, struct inode *inode,
 			struct dentry *dentry)
 {
-	if (ext4_fc_eligible(inode->i_sb))
+	if (ext4_fc_handle_eligible(inode->i_sb, handle))
 		__ext4_fc_track_link(handle, inode, dentry);
 }
 
@@ -565,7 +591,7 @@ void ext4_fc_track_create(handle_t *handle, struct dentry *dentry)
 {
 	struct inode *inode = d_inode(dentry);
 
-	if (ext4_fc_eligible(inode->i_sb))
+	if (ext4_fc_handle_eligible(inode->i_sb, handle))
 		__ext4_fc_track_create(handle, inode, dentry);
 }
 
@@ -594,7 +620,7 @@ void ext4_fc_track_inode(handle_t *handle, struct inode *inode)
 		return;
 	}
 
-	if (!ext4_fc_eligible(inode->i_sb))
+	if (!ext4_fc_handle_eligible(inode->i_sb, handle))
 		return;
 
 	/*
@@ -654,7 +680,7 @@ void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t star
 	if (S_ISDIR(inode->i_mode))
 		return;
 
-	if (!ext4_fc_eligible(inode->i_sb))
+	if (!ext4_fc_handle_eligible(inode->i_sb, handle))
 		return;
 
 	if (ext4_has_inline_data(inode)) {
@@ -1785,7 +1811,7 @@ static void ext4_fc_cleanup(journal_t *journal, int full, tid_t tid)
 	list_splice_init(&sbi->s_fc_q[FC_Q_STAGING],
 				&sbi->s_fc_q[FC_Q_MAIN]);
 
-	if (tid_geq(tid, sbi->s_fc_ineligible_tid)) {
+	if (full && tid_geq(tid, sbi->s_fc_ineligible_tid)) {
 		sbi->s_fc_ineligible_tid = 0;
 		ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
 	}
@@ -2860,7 +2886,8 @@ EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fc_disabled_test);
 
 bool ext4_fc_eligible_test(struct super_block *sb)
 {
-	return ext4_fc_eligible(sb);
+	return !ext4_fc_disabled(sb) &&
+		!(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE));
 }
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_fc_eligible_test);
 
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f4..85942dde3421a 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -3865,9 +3865,7 @@ static int ext4_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
 
 		if (new.inode)
 			ext4_fc_track_unlink(handle, new.dentry);
-		if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
-		    !(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
-		    !(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE))) {
+		if (ext4_fc_handle_eligible(sb, handle)) {
 			__ext4_fc_track_link(handle, old.inode, new.dentry);
 			__ext4_fc_track_unlink(handle, old.inode, old.dentry);
 			if (whiteout)
-- 
2.39.5

  parent reply	other threads:[~2026-11-30 17:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 17:37 [PATCH v2 0/3] ext4: fix concurrent fast commit durability and tracking races Xing Ma
2026-11-30 17:37 ` [PATCH v2 1/3] jbd2, ext4: clear commit ongoing flags under s_fc_lock in ext4_fc_cleanup() Xing Ma
2026-11-30 17:37 ` [PATCH v2 2/3] ext4: do not skip fast commit for updates queued during ongoing commit Xing Ma
2026-11-30 17:37 ` Xing Ma [this message]
2026-11-30 17:52 ` [PATCH v2 0/3] ext4: fix concurrent fast commit durability and tracking 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=20261201013742.1562967-4-maxing.lan@bytedance.com \
    --to=maxing.lan@bytedance.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=harshadshirwadkar@gmail.com \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=tytso@mit.edu \
    --cc=yi.zhang@huawei.com \
    --cc=yinxin.x@bytedance.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