Skip to content

Commit 1e9fd53

Browse files
committed
jbd: Fix a race between checkpointing code and journal_get_write_access()
The following race can happen: CPU1 CPU2 checkpointing code checks the buffer, adds it to an array for writeback do_get_write_access() ... lock_buffer() unlock_buffer() flush_batch() submits the buffer for IO __jbd_journal_file_buffer() So a buffer under writeout is returned from do_get_write_access(). Since the filesystem code relies on the fact that journaled buffers cannot be written out, it does not take the buffer lock and so it can modify buffer while it is under writeout. That can lead to a filesystem corruption if we crash at the right moment. The similar problem can happen with the journal_get_create_access() path. We fix the problem by clearing the buffer dirty bit under buffer_lock even if the buffer is on BJ_None list. Actually, we clear the dirty bit regardless the list the buffer is in and warn about the fact if the buffer is already journalled. Thanks for spotting the problem goes to dingdinghua <dingdinghua85@gmail.com>. Reported-by: dingdinghua <dingdinghua85@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 9eaaa2d commit 1e9fd53

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

‎fs/jbd/transaction.c‎

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -489,34 +489,15 @@ void journal_unlock_updates (journal_t *journal)
489489
wake_up(&journal->j_wait_transaction_locked);
490490
}
491491

492-
/*
493-
* Report any unexpected dirty buffers which turn up. Normally those
494-
* indicate an error, but they can occur if the user is running (say)
495-
* tune2fs to modify the live filesystem, so we need the option of
496-
* continuing as gracefully as possible. #
497-
*
498-
* The caller should already hold the journal lock and
499-
* j_list_lock spinlock: most callers will need those anyway
500-
* in order to probe the buffer's journaling state safely.
501-
*/
502-
static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
492+
static void warn_dirty_buffer(struct buffer_head *bh)
503493
{
504-
int jlist;
505-
506-
/* If this buffer is one which might reasonably be dirty
507-
* --- ie. data, or not part of this journal --- then
508-
* we're OK to leave it alone, but otherwise we need to
509-
* move the dirty bit to the journal's own internal
510-
* JBDDirty bit. */
511-
jlist = jh->b_jlist;
494+
char b[BDEVNAME_SIZE];
512495

513-
if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
514-
jlist == BJ_Shadow || jlist == BJ_Forget) {
515-
struct buffer_head *bh = jh2bh(jh);
516-
517-
if (test_clear_buffer_dirty(bh))
518-
set_buffer_jbddirty(bh);
519-
}
496+
printk(KERN_WARNING
497+
"JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
498+
"There's a risk of filesystem corruption in case of system "
499+
"crash.\n",
500+
bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
520501
}
521502

522503
/*
@@ -583,14 +564,16 @@ do_get_write_access(handle_t *handle, struct journal_head *jh,
583564
if (jh->b_next_transaction)
584565
J_ASSERT_JH(jh, jh->b_next_transaction ==
585566
transaction);
567+
warn_dirty_buffer(bh);
586568
}
587569
/*
588570
* In any case we need to clean the dirty flag and we must
589571
* do it under the buffer lock to be sure we don't race
590572
* with running write-out.
591573
*/
592-
JBUFFER_TRACE(jh, "Unexpected dirty buffer");
593-
jbd_unexpected_dirty_buffer(jh);
574+
JBUFFER_TRACE(jh, "Journalling dirty buffer");
575+
clear_buffer_dirty(bh);
576+
set_buffer_jbddirty(bh);
594577
}
595578

596579
unlock_buffer(bh);
@@ -826,6 +809,15 @@ int journal_get_create_access(handle_t *handle, struct buffer_head *bh)
826809
J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
827810

828811
if (jh->b_transaction == NULL) {
812+
/*
813+
* Previous journal_forget() could have left the buffer
814+
* with jbddirty bit set because it was being committed. When
815+
* the commit finished, we've filed the buffer for
816+
* checkpointing and marked it dirty. Now we are reallocating
817+
* the buffer so the transaction freeing it must have
818+
* committed and so it's safe to clear the dirty bit.
819+
*/
820+
clear_buffer_dirty(jh2bh(jh));
829821
jh->b_transaction = transaction;
830822

831823
/* first access by this transaction */
@@ -1782,8 +1774,13 @@ static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
17821774

17831775
if (jh->b_cp_transaction) {
17841776
JBUFFER_TRACE(jh, "on running+cp transaction");
1777+
/*
1778+
* We don't want to write the buffer anymore, clear the
1779+
* bit so that we don't confuse checks in
1780+
* __journal_file_buffer
1781+
*/
1782+
clear_buffer_dirty(bh);
17851783
__journal_file_buffer(jh, transaction, BJ_Forget);
1786-
clear_buffer_jbddirty(bh);
17871784
may_free = 0;
17881785
} else {
17891786
JBUFFER_TRACE(jh, "on running transaction");
@@ -2041,12 +2038,17 @@ void __journal_file_buffer(struct journal_head *jh,
20412038
if (jh->b_transaction && jh->b_jlist == jlist)
20422039
return;
20432040

2044-
/* The following list of buffer states needs to be consistent
2045-
* with __jbd_unexpected_dirty_buffer()'s handling of dirty
2046-
* state. */
2047-
20482041
if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
20492042
jlist == BJ_Shadow || jlist == BJ_Forget) {
2043+
/*
2044+
* For metadata buffers, we track dirty bit in buffer_jbddirty
2045+
* instead of buffer_dirty. We should not see a dirty bit set
2046+
* here because we clear it in do_get_write_access but e.g.
2047+
* tune2fs can modify the sb and set the dirty bit at any time
2048+
* so we try to gracefully handle that.
2049+
*/
2050+
if (buffer_dirty(bh))
2051+
warn_dirty_buffer(bh);
20502052
if (test_clear_buffer_dirty(bh) ||
20512053
test_clear_buffer_jbddirty(bh))
20522054
was_dirty = 1;

0 commit comments

Comments
 (0)