Commit 26623e72 authored by Jan Kara's avatar Jan Kara Committed by Kamal Mostafa

jbd2: avoid infinite loop when destroying aborted journal

commit 841df7df upstream.

Commit 6f6a6fda "jbd2: fix ocfs2 corrupt when updating journal
superblock fails" changed jbd2_cleanup_journal_tail() to return EIO
when the journal is aborted. That makes logic in
jbd2_log_do_checkpoint() bail out which is fine, except that
jbd2_journal_destroy() expects jbd2_log_do_checkpoint() to always make
a progress in cleaning the journal. Without it jbd2_journal_destroy()
just loops in an infinite loop.

Fix jbd2_journal_destroy() to cleanup journal checkpoint lists of
jbd2_log_do_checkpoint() fails with error.
Reported-by: default avatarEryu Guan <guaneryu@gmail.com>
Tested-by: default avatarEryu Guan <guaneryu@gmail.com>
Fixes: 6f6a6fdaSigned-off-by: default avatarJan Kara <jack@suse.com>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
parent 0ad5ac41
...@@ -417,12 +417,12 @@ int jbd2_cleanup_journal_tail(journal_t *journal) ...@@ -417,12 +417,12 @@ int jbd2_cleanup_journal_tail(journal_t *journal)
* journal_clean_one_cp_list * journal_clean_one_cp_list
* *
* Find all the written-back checkpoint buffers in the given list and * Find all the written-back checkpoint buffers in the given list and
* release them. * release them. If 'destroy' is set, clean all buffers unconditionally.
* *
* Called with j_list_lock held. * Called with j_list_lock held.
* Returns 1 if we freed the transaction, 0 otherwise. * Returns 1 if we freed the transaction, 0 otherwise.
*/ */
static int journal_clean_one_cp_list(struct journal_head *jh) static int journal_clean_one_cp_list(struct journal_head *jh, bool destroy)
{ {
struct journal_head *last_jh; struct journal_head *last_jh;
struct journal_head *next_jh = jh; struct journal_head *next_jh = jh;
...@@ -436,7 +436,10 @@ static int journal_clean_one_cp_list(struct journal_head *jh) ...@@ -436,7 +436,10 @@ static int journal_clean_one_cp_list(struct journal_head *jh)
do { do {
jh = next_jh; jh = next_jh;
next_jh = jh->b_cpnext; next_jh = jh->b_cpnext;
if (!destroy)
ret = __try_to_free_cp_buf(jh); ret = __try_to_free_cp_buf(jh);
else
ret = __jbd2_journal_remove_checkpoint(jh) + 1;
if (!ret) if (!ret)
return freed; return freed;
if (ret == 2) if (ret == 2)
...@@ -459,10 +462,11 @@ static int journal_clean_one_cp_list(struct journal_head *jh) ...@@ -459,10 +462,11 @@ static int journal_clean_one_cp_list(struct journal_head *jh)
* journal_clean_checkpoint_list * journal_clean_checkpoint_list
* *
* Find all the written-back checkpoint buffers in the journal and release them. * Find all the written-back checkpoint buffers in the journal and release them.
* If 'destroy' is set, release all buffers unconditionally.
* *
* Called with j_list_lock held. * Called with j_list_lock held.
*/ */
void __jbd2_journal_clean_checkpoint_list(journal_t *journal) void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
{ {
transaction_t *transaction, *last_transaction, *next_transaction; transaction_t *transaction, *last_transaction, *next_transaction;
int ret; int ret;
...@@ -476,7 +480,8 @@ void __jbd2_journal_clean_checkpoint_list(journal_t *journal) ...@@ -476,7 +480,8 @@ void __jbd2_journal_clean_checkpoint_list(journal_t *journal)
do { do {
transaction = next_transaction; transaction = next_transaction;
next_transaction = transaction->t_cpnext; next_transaction = transaction->t_cpnext;
ret = journal_clean_one_cp_list(transaction->t_checkpoint_list); ret = journal_clean_one_cp_list(transaction->t_checkpoint_list,
destroy);
/* /*
* This function only frees up some memory if possible so we * This function only frees up some memory if possible so we
* dont have an obligation to finish processing. Bail out if * dont have an obligation to finish processing. Bail out if
...@@ -492,7 +497,7 @@ void __jbd2_journal_clean_checkpoint_list(journal_t *journal) ...@@ -492,7 +497,7 @@ void __jbd2_journal_clean_checkpoint_list(journal_t *journal)
* we can possibly see not yet submitted buffers on io_list * we can possibly see not yet submitted buffers on io_list
*/ */
ret = journal_clean_one_cp_list(transaction-> ret = journal_clean_one_cp_list(transaction->
t_checkpoint_io_list); t_checkpoint_io_list, destroy);
if (need_resched()) if (need_resched())
return; return;
/* /*
...@@ -505,6 +510,28 @@ void __jbd2_journal_clean_checkpoint_list(journal_t *journal) ...@@ -505,6 +510,28 @@ void __jbd2_journal_clean_checkpoint_list(journal_t *journal)
} while (transaction != last_transaction); } while (transaction != last_transaction);
} }
/*
* Remove buffers from all checkpoint lists as journal is aborted and we just
* need to free memory
*/
void jbd2_journal_destroy_checkpoint(journal_t *journal)
{
/*
* We loop because __jbd2_journal_clean_checkpoint_list() may abort
* early due to a need of rescheduling.
*/
while (1) {
spin_lock(&journal->j_list_lock);
if (!journal->j_checkpoint_transactions) {
spin_unlock(&journal->j_list_lock);
break;
}
__jbd2_journal_clean_checkpoint_list(journal, true);
spin_unlock(&journal->j_list_lock);
cond_resched();
}
}
/* /*
* journal_remove_checkpoint: called after a buffer has been committed * journal_remove_checkpoint: called after a buffer has been committed
* to disk (either by being write-back flushed to disk, or being * to disk (either by being write-back flushed to disk, or being
......
...@@ -510,7 +510,7 @@ void jbd2_journal_commit_transaction(journal_t *journal) ...@@ -510,7 +510,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
* frees some memory * frees some memory
*/ */
spin_lock(&journal->j_list_lock); spin_lock(&journal->j_list_lock);
__jbd2_journal_clean_checkpoint_list(journal); __jbd2_journal_clean_checkpoint_list(journal, false);
spin_unlock(&journal->j_list_lock); spin_unlock(&journal->j_list_lock);
jbd_debug(3, "JBD2: commit phase 1\n"); jbd_debug(3, "JBD2: commit phase 1\n");
......
...@@ -1708,8 +1708,17 @@ int jbd2_journal_destroy(journal_t *journal) ...@@ -1708,8 +1708,17 @@ int jbd2_journal_destroy(journal_t *journal)
while (journal->j_checkpoint_transactions != NULL) { while (journal->j_checkpoint_transactions != NULL) {
spin_unlock(&journal->j_list_lock); spin_unlock(&journal->j_list_lock);
mutex_lock(&journal->j_checkpoint_mutex); mutex_lock(&journal->j_checkpoint_mutex);
jbd2_log_do_checkpoint(journal); err = jbd2_log_do_checkpoint(journal);
mutex_unlock(&journal->j_checkpoint_mutex); mutex_unlock(&journal->j_checkpoint_mutex);
/*
* If checkpointing failed, just free the buffers to avoid
* looping forever
*/
if (err) {
jbd2_journal_destroy_checkpoint(journal);
spin_lock(&journal->j_list_lock);
break;
}
spin_lock(&journal->j_list_lock); spin_lock(&journal->j_list_lock);
} }
......
...@@ -1042,8 +1042,9 @@ void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block); ...@@ -1042,8 +1042,9 @@ void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block);
extern void jbd2_journal_commit_transaction(journal_t *); extern void jbd2_journal_commit_transaction(journal_t *);
/* Checkpoint list management */ /* Checkpoint list management */
void __jbd2_journal_clean_checkpoint_list(journal_t *journal); void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy);
int __jbd2_journal_remove_checkpoint(struct journal_head *); int __jbd2_journal_remove_checkpoint(struct journal_head *);
void jbd2_journal_destroy_checkpoint(journal_t *journal);
void __jbd2_journal_insert_checkpoint(struct journal_head *, transaction_t *); void __jbd2_journal_insert_checkpoint(struct journal_head *, transaction_t *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment