jbd2: protect all log tail updates with j_checkpoint_mutex

There are some log tail updates that are not protected by j_checkpoint_mutex.
Some of these are harmless because they happen during startup or shutdown but
updates in jbd2_journal_commit_transaction() and jbd2_journal_flush() can
really race with other log tail updates (e.g. someone doing
jbd2_journal_flush() with someone running jbd2_cleanup_journal_tail()). So
protect all log tail updates with j_checkpoint_mutex.

Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
This commit is contained in:
Jan Kara 2012-03-13 15:43:04 -04:00 committed by Theodore Ts'o
parent 24bcc89c7e
commit a78bb11d7a
2 changed files with 18 additions and 3 deletions

View File

@ -340,7 +340,9 @@ void jbd2_journal_commit_transaction(journal_t *journal)
/* Do we need to erase the effects of a prior jbd2_journal_flush? */ /* Do we need to erase the effects of a prior jbd2_journal_flush? */
if (journal->j_flags & JBD2_FLUSHED) { if (journal->j_flags & JBD2_FLUSHED) {
jbd_debug(3, "super block updated\n"); jbd_debug(3, "super block updated\n");
mutex_lock(&journal->j_checkpoint_mutex);
jbd2_journal_update_sb_log_tail(journal); jbd2_journal_update_sb_log_tail(journal);
mutex_unlock(&journal->j_checkpoint_mutex);
} else { } else {
jbd_debug(3, "superblock not updated\n"); jbd_debug(3, "superblock not updated\n");
} }

View File

@ -1123,8 +1123,11 @@ static int journal_reset(journal_t *journal)
journal->j_errno); journal->j_errno);
journal->j_flags |= JBD2_FLUSHED; journal->j_flags |= JBD2_FLUSHED;
} else { } else {
/* Lock here to make assertions happy... */
mutex_lock(&journal->j_checkpoint_mutex);
/* Add the dynamic fields and write it to disk. */ /* Add the dynamic fields and write it to disk. */
jbd2_journal_update_sb_log_tail(journal); jbd2_journal_update_sb_log_tail(journal);
mutex_unlock(&journal->j_checkpoint_mutex);
} }
return jbd2_journal_start_thread(journal); return jbd2_journal_start_thread(journal);
} }
@ -1173,6 +1176,7 @@ void jbd2_journal_update_sb_log_tail(journal_t *journal)
{ {
journal_superblock_t *sb = journal->j_superblock; journal_superblock_t *sb = journal->j_superblock;
BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
read_lock(&journal->j_state_lock); read_lock(&journal->j_state_lock);
jbd_debug(1, "JBD2: updating superblock (start %ld, seq %d)\n", jbd_debug(1, "JBD2: updating superblock (start %ld, seq %d)\n",
journal->j_tail, journal->j_tail_sequence); journal->j_tail, journal->j_tail_sequence);
@ -1201,6 +1205,7 @@ static void jbd2_mark_journal_empty(journal_t *journal)
{ {
journal_superblock_t *sb = journal->j_superblock; journal_superblock_t *sb = journal->j_superblock;
BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
read_lock(&journal->j_state_lock); read_lock(&journal->j_state_lock);
jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n", jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
journal->j_tail_sequence); journal->j_tail_sequence);
@ -1434,9 +1439,11 @@ int jbd2_journal_destroy(journal_t *journal)
spin_unlock(&journal->j_list_lock); spin_unlock(&journal->j_list_lock);
if (journal->j_sb_buffer) { if (journal->j_sb_buffer) {
if (!is_journal_aborted(journal)) if (!is_journal_aborted(journal)) {
mutex_lock(&journal->j_checkpoint_mutex);
jbd2_mark_journal_empty(journal); jbd2_mark_journal_empty(journal);
else mutex_unlock(&journal->j_checkpoint_mutex);
} else
err = -EIO; err = -EIO;
brelse(journal->j_sb_buffer); brelse(journal->j_sb_buffer);
} }
@ -1630,6 +1637,7 @@ int jbd2_journal_flush(journal_t *journal)
if (is_journal_aborted(journal)) if (is_journal_aborted(journal))
return -EIO; return -EIO;
mutex_lock(&journal->j_checkpoint_mutex);
jbd2_cleanup_journal_tail(journal); jbd2_cleanup_journal_tail(journal);
/* Finally, mark the journal as really needing no recovery. /* Finally, mark the journal as really needing no recovery.
@ -1638,6 +1646,7 @@ int jbd2_journal_flush(journal_t *journal)
* commits of data to the journal will restore the current * commits of data to the journal will restore the current
* s_start value. */ * s_start value. */
jbd2_mark_journal_empty(journal); jbd2_mark_journal_empty(journal);
mutex_unlock(&journal->j_checkpoint_mutex);
write_lock(&journal->j_state_lock); write_lock(&journal->j_state_lock);
J_ASSERT(!journal->j_running_transaction); J_ASSERT(!journal->j_running_transaction);
J_ASSERT(!journal->j_committing_transaction); J_ASSERT(!journal->j_committing_transaction);
@ -1678,8 +1687,12 @@ int jbd2_journal_wipe(journal_t *journal, int write)
write ? "Clearing" : "Ignoring"); write ? "Clearing" : "Ignoring");
err = jbd2_journal_skip_recovery(journal); err = jbd2_journal_skip_recovery(journal);
if (write) if (write) {
/* Lock to make assertions happy... */
mutex_lock(&journal->j_checkpoint_mutex);
jbd2_mark_journal_empty(journal); jbd2_mark_journal_empty(journal);
mutex_unlock(&journal->j_checkpoint_mutex);
}
no_recovery: no_recovery:
return err; return err;