Skip to content

Commit fcf3754

Browse files
zhangyi089tytso
authored andcommitted
jbd2: ensure abort the journal if detect IO error when writing original buffer back
Although we merged c044f3d ("jbd2: abort journal if free a async write error metadata buffer"), there is a race between jbd2_journal_try_to_free_buffers() and jbd2_journal_destroy(), so the jbd2_log_do_checkpoint() may still fail to detect the buffer write io error flag which may lead to filesystem inconsistency. jbd2_journal_try_to_free_buffers() ext4_put_super() jbd2_journal_destroy() __jbd2_journal_remove_checkpoint() detect buffer write error jbd2_log_do_checkpoint() jbd2_cleanup_journal_tail() <--- lead to inconsistency jbd2_journal_abort() Fix this issue by introducing a new atomic flag which only have one JBD2_CHECKPOINT_IO_ERROR bit now, and set it in __jbd2_journal_remove_checkpoint() when freeing a checkpoint buffer which has write_io_error flag. Then jbd2_journal_destroy() will detect this mark and abort the journal to prevent updating log tail. Signed-off-by: Zhang Yi <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 1866cba commit fcf3754

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

fs/jbd2/checkpoint.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
564564
struct transaction_chp_stats_s *stats;
565565
transaction_t *transaction;
566566
journal_t *journal;
567+
struct buffer_head *bh = jh2bh(jh);
567568

568569
JBUFFER_TRACE(jh, "entry");
569570

@@ -575,6 +576,17 @@ int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
575576
journal = transaction->t_journal;
576577

577578
JBUFFER_TRACE(jh, "removing from transaction");
579+
580+
/*
581+
* If we have failed to write the buffer out to disk, the filesystem
582+
* may become inconsistent. We cannot abort the journal here since
583+
* we hold j_list_lock and we have to be careful about races with
584+
* jbd2_journal_destroy(). So mark the writeback IO error in the
585+
* journal here and we abort the journal later from a better context.
586+
*/
587+
if (buffer_write_io_error(bh))
588+
set_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags);
589+
578590
__buffer_unlink(jh);
579591
jh->b_cp_transaction = NULL;
580592
jbd2_journal_put_journal_head(jh);

fs/jbd2/journal.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,10 @@ int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
16101610

16111611
if (is_journal_aborted(journal))
16121612
return -EIO;
1613+
if (test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags)) {
1614+
jbd2_journal_abort(journal, -EIO);
1615+
return -EIO;
1616+
}
16131617

16141618
BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
16151619
jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
@@ -2091,6 +2095,16 @@ int jbd2_journal_destroy(journal_t *journal)
20912095
J_ASSERT(journal->j_checkpoint_transactions == NULL);
20922096
spin_unlock(&journal->j_list_lock);
20932097

2098+
/*
2099+
* OK, all checkpoint transactions have been checked, now check the
2100+
* write out io error flag and abort the journal if some buffer failed
2101+
* to write back to the original location, otherwise the filesystem
2102+
* may become inconsistent.
2103+
*/
2104+
if (!is_journal_aborted(journal) &&
2105+
test_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags))
2106+
jbd2_journal_abort(journal, -EIO);
2107+
20942108
if (journal->j_sb_buffer) {
20952109
if (!is_journal_aborted(journal)) {
20962110
mutex_lock_io(&journal->j_checkpoint_mutex);

include/linux/jbd2.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,11 @@ struct journal_s
779779
*/
780780
unsigned long j_flags;
781781

782+
/**
783+
* @j_atomic_flags: Atomic journaling state flags.
784+
*/
785+
unsigned long j_atomic_flags;
786+
782787
/**
783788
* @j_errno:
784789
*
@@ -1375,6 +1380,12 @@ JBD2_FEATURE_INCOMPAT_FUNCS(fast_commit, FAST_COMMIT)
13751380
#define JBD2_JOURNAL_FLUSH_VALID (JBD2_JOURNAL_FLUSH_DISCARD | \
13761381
JBD2_JOURNAL_FLUSH_ZEROOUT)
13771382

1383+
/*
1384+
* Journal atomic flag definitions
1385+
*/
1386+
#define JBD2_CHECKPOINT_IO_ERROR 0x001 /* Detect io error while writing
1387+
* buffer back to disk */
1388+
13781389
/*
13791390
* Function declarations for the journaling transaction and buffer
13801391
* management

0 commit comments

Comments
 (0)