Skip to content

Commit aac4507

Browse files
zhangyi089tytso
authored andcommitted
jbd2: add a missing data flush during file and fs synchronization
When the filesystem performs file or filesystem synchronization (e.g., ext4_sync_file()), it queries the journal to determine whether to flush the file device through jbd2_trans_will_send_data_barrier(). If the target transaction has not started committing, it assumes that the journal will submit the flush command, allowing the filesystem to bypass a redundant flush command. However, this assumption is not always valid. If the journal is not located on the filesystem device, the journal commit thread will not submit the flush command unless the variable ->t_need_data_flush is set to 1. Consequently, the flush may be missed, and data may be lost following a power failure or system crash, even if the synchronization appears to succeed. Unfortunately, we cannot determine with certainty whether the target transaction will flush to the filesystem device before it commits. However, if it has not started committing, it must be the running transaction. Therefore, fix it by always set its t_need_data_flush to 1, ensuring that the committing thread will flush the filesystem device. Fixes: bbd2be3 ("jbd2: Add function jbd2_trans_will_send_data_barrier()") Signed-off-by: Zhang Yi <yi.zhang@huawei.com> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://patch.msgid.link/20241206111327.4171337-1-yi.zhang@huaweicloud.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent f87d3af commit aac4507

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

fs/jbd2/journal.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
603603
int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
604604
{
605605
int ret = 0;
606-
transaction_t *commit_trans;
606+
transaction_t *commit_trans, *running_trans;
607607

608608
if (!(journal->j_flags & JBD2_BARRIER))
609609
return 0;
@@ -613,6 +613,16 @@ int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
613613
goto out;
614614
commit_trans = journal->j_committing_transaction;
615615
if (!commit_trans || commit_trans->t_tid != tid) {
616+
running_trans = journal->j_running_transaction;
617+
/*
618+
* The query transaction hasn't started committing,
619+
* it must still be running.
620+
*/
621+
if (WARN_ON_ONCE(!running_trans ||
622+
running_trans->t_tid != tid))
623+
goto out;
624+
625+
running_trans->t_need_data_flush = 1;
616626
ret = 1;
617627
goto out;
618628
}

0 commit comments

Comments
 (0)