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}() 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() so tracking continues for any handle whose transaction TID is newer than s_fc_ineligible_tid, 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/fast_commit.c | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/fs/ext4/fast_commit.c b/fs/ext4/fast_commit.c index e03a829e2a10a..27e54ffa4ae5e 100644 --- a/fs/ext4/fast_commit.c +++ b/fs/ext4/fast_commit.c @@ -235,6 +235,31 @@ static bool ext4_fc_eligible(struct super_block *sb) !(ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE)); } +static bool ext4_fc_handle_eligible(struct super_block *sb, handle_t *handle) +{ + struct ext4_sb_info *sbi = EXT4_SB(sb); + + 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. Continue tracking handles whose TID is newer than + * s_fc_ineligible_tid so the next transaction is completely tracked + * once the older full commit clears EXT4_MF_FC_INELIGIBLE. + * + * Pairs with smp_wmb() in ext4_fc_mark_ineligible() and + * ext4_fc_cleanup(). + */ + smp_rmb(); + return tid_gt(handle->h_transaction->t_tid, + READ_ONCE(sbi->s_fc_ineligible_tid)); +} + /* * Wait for an inode fast-commit state bit to clear while dropping the * fast-commit lock around schedule(). @@ -371,7 +396,9 @@ void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handl alloc_ctx = ext4_fc_lock(sb); is_ineligible = ext4_test_mount_flag(sb, EXT4_MF_FC_INELIGIBLE); if (has_transaction && (!is_ineligible || tid_gt(tid, sbi->s_fc_ineligible_tid))) - sbi->s_fc_ineligible_tid = tid; + WRITE_ONCE(sbi->s_fc_ineligible_tid, tid); + /* Pairs with smp_rmb() in ext4_fc_handle_eligible(). */ + smp_wmb(); ext4_set_mount_flag(sb, EXT4_MF_FC_INELIGIBLE); ext4_fc_unlock(sb, alloc_ctx); WARN_ON(reason >= EXT4_FC_REASON_MAX); @@ -513,7 +540,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); } @@ -534,7 +561,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); } @@ -556,7 +583,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); } @@ -585,7 +612,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; /* @@ -645,7 +672,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)) { @@ -1782,9 +1809,11 @@ 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)) { - sbi->s_fc_ineligible_tid = 0; + if (full && tid_geq(tid, sbi->s_fc_ineligible_tid)) { ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE); + /* Pairs with smp_rmb() in ext4_fc_handle_eligible(). */ + smp_wmb(); + WRITE_ONCE(sbi->s_fc_ineligible_tid, 0); } if (full) -- 2.39.5