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 --- 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