]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
jbd2: avoid infinite loop when destroying aborted journal
authorJan Kara <jack@suse.com>
Tue, 28 Jul 2015 18:57:14 +0000 (14:57 -0400)
committerZefan Li <lizefan@huawei.com>
Thu, 22 Oct 2015 01:20:08 +0000 (09:20 +0800)
commit 841df7df196237ea63233f0f9eaa41db53afd70f upstream.

Commit 6f6a6fda2945 "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: Eryu Guan <guaneryu@gmail.com>
Tested-by: Eryu Guan <guaneryu@gmail.com>
Fixes: 6f6a6fda294506dfe0e3e0a253bb2d2923f28f0a
Signed-off-by: Jan Kara <jack@suse.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
[lizf: Backported to 3.4: adjust context]
Signed-off-by: Zefan Li <lizefan@huawei.com>
fs/jbd2/checkpoint.c
fs/jbd2/commit.c
fs/jbd2/journal.c
include/linux/jbd2.h

index 6bb52859cb866a12e804453c77d3f0a301fe327b..4fd78565988dae3018f7eb83bbf9919f62fa8857 100644 (file)
@@ -467,14 +467,14 @@ int jbd2_cleanup_journal_tail(journal_t *journal)
  * journal_clean_one_cp_list
  *
  * 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 the journal locked.
  * Called with j_list_lock held.
  * Returns number of buffers reaped (for debug)
  */
 
-static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
+static int journal_clean_one_cp_list(struct journal_head *jh, int *released, bool destroy)
 {
        struct journal_head *last_jh;
        struct journal_head *next_jh = jh;
@@ -488,7 +488,10 @@ static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
        do {
                jh = next_jh;
                next_jh = jh->b_cpnext;
-               ret = __try_to_free_cp_buf(jh);
+               if (!destroy)
+                       ret = __try_to_free_cp_buf(jh);
+               else
+                       ret = __jbd2_journal_remove_checkpoint(jh) + 1;
                if (ret) {
                        freed++;
                        if (ret == 2) {
@@ -514,12 +517,14 @@ static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
  *
  * Find all the written-back checkpoint buffers in the journal and release them.
  *
+ * If 'destroy' is set, release all buffers unconditionally.
+ *
  * Called with the journal locked.
  * Called with j_list_lock held.
  * Returns number of buffers reaped (for debug)
  */
 
-int __jbd2_journal_clean_checkpoint_list(journal_t *journal)
+int __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
 {
        transaction_t *transaction, *last_transaction, *next_transaction;
        int ret = 0;
@@ -535,7 +540,7 @@ int __jbd2_journal_clean_checkpoint_list(journal_t *journal)
                transaction = next_transaction;
                next_transaction = transaction->t_cpnext;
                ret += journal_clean_one_cp_list(transaction->
-                               t_checkpoint_list, &released);
+                               t_checkpoint_list, &released, destroy);
                /*
                 * This function only frees up some memory if possible so we
                 * dont have an obligation to finish processing. Bail out if
@@ -551,7 +556,7 @@ int __jbd2_journal_clean_checkpoint_list(journal_t *journal)
                 * we can possibly see not yet submitted buffers on io_list
                 */
                ret += journal_clean_one_cp_list(transaction->
-                               t_checkpoint_io_list, &released);
+                               t_checkpoint_io_list, &released, destroy);
                if (need_resched())
                        goto out;
        } while (transaction != last_transaction);
@@ -559,6 +564,28 @@ out:
        return ret;
 }
 
+/*
+ * 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
  * to disk (either by being write-back flushed to disk, or being
index a0dcbd62b180775f70657526f2d624323ffc2aeb..259f28dfc652acf6dc5044a1c4f7e85e6ff9c06b 100644 (file)
@@ -438,7 +438,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
         * frees some memory
         */
        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);
 
        jbd_debug(3, "JBD2: commit phase 1\n");
index ad64b94ac7f16e162d21fe04fb365505d5402708..a3279442bf304c0b1defa2b46a5d8f4fca23441c 100644 (file)
@@ -1591,8 +1591,17 @@ int jbd2_journal_destroy(journal_t *journal)
        while (journal->j_checkpoint_transactions != NULL) {
                spin_unlock(&journal->j_list_lock);
                mutex_lock(&journal->j_checkpoint_mutex);
-               jbd2_log_do_checkpoint(journal);
+               err = jbd2_log_do_checkpoint(journal);
                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);
        }
 
index 129bca491acfdc8ae8342897d09d5ff91de71596..2179d78b6ea645081a2974cecbf522c384ffe4dd 100644 (file)
@@ -981,8 +981,9 @@ void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block);
 extern void jbd2_journal_commit_transaction(journal_t *);
 
 /* Checkpoint list management */
-int __jbd2_journal_clean_checkpoint_list(journal_t *journal);
+int __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy);
 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 *);