LCOV - code coverage report
Current view: top level - fs/jbd2 - checkpoint.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 282 299 94.3 %
Date: 2023-07-31 20:08:34 Functions: 13 13 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0+
       2             : /*
       3             :  * linux/fs/jbd2/checkpoint.c
       4             :  *
       5             :  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
       6             :  *
       7             :  * Copyright 1999 Red Hat Software --- All Rights Reserved
       8             :  *
       9             :  * Checkpoint routines for the generic filesystem journaling code.
      10             :  * Part of the ext2fs journaling system.
      11             :  *
      12             :  * Checkpointing is the process of ensuring that a section of the log is
      13             :  * committed fully to disk, so that that portion of the log can be
      14             :  * reused.
      15             :  */
      16             : 
      17             : #include <linux/time.h>
      18             : #include <linux/fs.h>
      19             : #include <linux/jbd2.h>
      20             : #include <linux/errno.h>
      21             : #include <linux/slab.h>
      22             : #include <linux/blkdev.h>
      23             : #include <trace/events/jbd2.h>
      24             : 
      25             : /*
      26             :  * Unlink a buffer from a transaction checkpoint list.
      27             :  *
      28             :  * Called with j_list_lock held.
      29             :  */
      30     3704044 : static inline void __buffer_unlink(struct journal_head *jh)
      31             : {
      32     3704044 :         transaction_t *transaction = jh->b_cp_transaction;
      33             : 
      34     3704044 :         jh->b_cpnext->b_cpprev = jh->b_cpprev;
      35     3704044 :         jh->b_cpprev->b_cpnext = jh->b_cpnext;
      36     3704044 :         if (transaction->t_checkpoint_list == jh) {
      37     2231771 :                 transaction->t_checkpoint_list = jh->b_cpnext;
      38     2231771 :                 if (transaction->t_checkpoint_list == jh)
      39      204562 :                         transaction->t_checkpoint_list = NULL;
      40             :         }
      41     3704044 : }
      42             : 
      43             : /*
      44             :  * Check a checkpoint buffer could be release or not.
      45             :  *
      46             :  * Requires j_list_lock
      47             :  */
      48             : static inline bool __cp_buffer_busy(struct journal_head *jh)
      49             : {
      50             :         struct buffer_head *bh = jh2bh(jh);
      51             : 
      52             :         return (jh->b_transaction || buffer_locked(bh) || buffer_dirty(bh));
      53             : }
      54             : 
      55             : /*
      56             :  * __jbd2_log_wait_for_space: wait until there is space in the journal.
      57             :  *
      58             :  * Called under j-state_lock *only*.  It will be unlocked if we have to wait
      59             :  * for a checkpoint to free up some space in the log.
      60             :  */
      61      239223 : void __jbd2_log_wait_for_space(journal_t *journal)
      62             : __acquires(&journal->j_state_lock)
      63             : __releases(&journal->j_state_lock)
      64             : {
      65      239223 :         int nblocks, space_left;
      66             :         /* assert_spin_locked(&journal->j_state_lock); */
      67             : 
      68      239223 :         nblocks = journal->j_max_transaction_buffers;
      69      844162 :         while (jbd2_log_space_left(journal) < nblocks) {
      70      242727 :                 write_unlock(&journal->j_state_lock);
      71      242709 :                 mutex_lock_io(&journal->j_checkpoint_mutex);
      72             : 
      73             :                 /*
      74             :                  * Test again, another process may have checkpointed while we
      75             :                  * were waiting for the checkpoint lock. If there are no
      76             :                  * transactions ready to be checkpointed, try to recover
      77             :                  * journal space by calling cleanup_journal_tail(), and if
      78             :                  * that doesn't work, by waiting for the currently committing
      79             :                  * transaction to complete.  If there is absolutely no way
      80             :                  * to make progress, this is either a BUG or corrupted
      81             :                  * filesystem, so abort the journal and leave a stack
      82             :                  * trace for forensic evidence.
      83             :                  */
      84      242727 :                 write_lock(&journal->j_state_lock);
      85      242727 :                 if (journal->j_flags & JBD2_ABORT) {
      86           0 :                         mutex_unlock(&journal->j_checkpoint_mutex);
      87           0 :                         return;
      88             :                 }
      89      242727 :                 spin_lock(&journal->j_list_lock);
      90      242727 :                 space_left = jbd2_log_space_left(journal);
      91      242727 :                 if (space_left < nblocks) {
      92       39635 :                         int chkpt = journal->j_checkpoint_transactions != NULL;
      93       39635 :                         tid_t tid = 0;
      94             : 
      95       39635 :                         if (journal->j_committing_transaction)
      96       38754 :                                 tid = journal->j_committing_transaction->t_tid;
      97       39635 :                         spin_unlock(&journal->j_list_lock);
      98       39635 :                         write_unlock(&journal->j_state_lock);
      99       39635 :                         if (chkpt) {
     100       39634 :                                 jbd2_log_do_checkpoint(journal);
     101           1 :                         } else if (jbd2_cleanup_journal_tail(journal) == 0) {
     102             :                                 /* We were able to recover space; yay! */
     103             :                                 ;
     104           0 :                         } else if (tid) {
     105             :                                 /*
     106             :                                  * jbd2_journal_commit_transaction() may want
     107             :                                  * to take the checkpoint_mutex if JBD2_FLUSHED
     108             :                                  * is set.  So we need to temporarily drop it.
     109             :                                  */
     110           0 :                                 mutex_unlock(&journal->j_checkpoint_mutex);
     111           0 :                                 jbd2_log_wait_commit(journal, tid);
     112           0 :                                 write_lock(&journal->j_state_lock);
     113           0 :                                 continue;
     114             :                         } else {
     115           0 :                                 printk(KERN_ERR "%s: needed %d blocks and "
     116             :                                        "only had %d space available\n",
     117             :                                        __func__, nblocks, space_left);
     118           0 :                                 printk(KERN_ERR "%s: no way to get more "
     119             :                                        "journal space in %s\n", __func__,
     120             :                                        journal->j_devname);
     121           0 :                                 WARN_ON(1);
     122           0 :                                 jbd2_journal_abort(journal, -EIO);
     123             :                         }
     124       39635 :                         write_lock(&journal->j_state_lock);
     125             :                 } else {
     126      203092 :                         spin_unlock(&journal->j_list_lock);
     127             :                 }
     128      242727 :                 mutex_unlock(&journal->j_checkpoint_mutex);
     129             :         }
     130             : }
     131             : 
     132             : static void
     133       10270 : __flush_batch(journal_t *journal, int *batch_count)
     134             : {
     135       10270 :         int i;
     136       10270 :         struct blk_plug plug;
     137             : 
     138       10270 :         blk_start_plug(&plug);
     139      226811 :         for (i = 0; i < *batch_count; i++)
     140      206271 :                 write_dirty_buffer(journal->j_chkpt_bhs[i], REQ_SYNC);
     141       10270 :         blk_finish_plug(&plug);
     142             : 
     143      226811 :         for (i = 0; i < *batch_count; i++) {
     144      206271 :                 struct buffer_head *bh = journal->j_chkpt_bhs[i];
     145      206271 :                 BUFFER_TRACE(bh, "brelse");
     146      206271 :                 __brelse(bh);
     147      206271 :                 journal->j_chkpt_bhs[i] = NULL;
     148             :         }
     149       10270 :         *batch_count = 0;
     150       10270 : }
     151             : 
     152             : /*
     153             :  * Perform an actual checkpoint. We take the first transaction on the
     154             :  * list of transactions to be checkpointed and send all its buffers
     155             :  * to disk. We submit larger chunks of data at once.
     156             :  *
     157             :  * The journal should be locked before calling this function.
     158             :  * Called with j_checkpoint_mutex held.
     159             :  */
     160       42439 : int jbd2_log_do_checkpoint(journal_t *journal)
     161             : {
     162       42439 :         struct journal_head     *jh;
     163       42439 :         struct buffer_head      *bh;
     164       42439 :         transaction_t           *transaction;
     165       42439 :         tid_t                   this_tid;
     166       42439 :         int                     result, batch_count = 0;
     167             : 
     168       42439 :         jbd2_debug(1, "Start checkpoint\n");
     169             : 
     170             :         /*
     171             :          * First thing: if there are any transactions in the log which
     172             :          * don't need checkpointing, just eliminate them from the
     173             :          * journal straight away.
     174             :          */
     175       42439 :         result = jbd2_cleanup_journal_tail(journal);
     176       42439 :         trace_jbd2_checkpoint(journal, result);
     177       42439 :         jbd2_debug(1, "cleanup_journal_tail returned %d\n", result);
     178       42439 :         if (result <= 0)
     179             :                 return result;
     180             : 
     181             :         /*
     182             :          * OK, we need to start writing disk blocks.  Take one transaction
     183             :          * and write it.
     184             :          */
     185       41895 :         spin_lock(&journal->j_list_lock);
     186       41895 :         if (!journal->j_checkpoint_transactions)
     187           0 :                 goto out;
     188       41895 :         transaction = journal->j_checkpoint_transactions;
     189       41895 :         if (transaction->t_chp_stats.cs_chp_time == 0)
     190        7685 :                 transaction->t_chp_stats.cs_chp_time = jiffies;
     191       41895 :         this_tid = transaction->t_tid;
     192             : restart:
     193             :         /*
     194             :          * If someone cleaned up this transaction while we slept, we're
     195             :          * done (maybe it's a new transaction, but it fell at the same
     196             :          * address).
     197             :          */
     198      100269 :         if (journal->j_checkpoint_transactions != transaction ||
     199       66022 :             transaction->t_tid != this_tid)
     200       34247 :                 goto out;
     201             : 
     202             :         /* checkpoint all of the transaction's buffers */
     203      595483 :         while (transaction->t_checkpoint_list) {
     204      595483 :                 jh = transaction->t_checkpoint_list;
     205      595483 :                 bh = jh2bh(jh);
     206             : 
     207      595483 :                 if (jh->b_transaction != NULL) {
     208       34840 :                         transaction_t *t = jh->b_transaction;
     209       34840 :                         tid_t tid = t->t_tid;
     210             : 
     211       34840 :                         transaction->t_chp_stats.cs_forced_to_close++;
     212       34840 :                         spin_unlock(&journal->j_list_lock);
     213       34840 :                         if (unlikely(journal->j_flags & JBD2_UNMOUNT))
     214             :                                 /*
     215             :                                  * The journal thread is dead; so
     216             :                                  * starting and waiting for a commit
     217             :                                  * to finish will cause us to wait for
     218             :                                  * a _very_ long time.
     219             :                                  */
     220           0 :                                 printk(KERN_ERR
     221             :                 "JBD2: %s: Waiting for Godot: block %llu\n",
     222             :                 journal->j_devname, (unsigned long long) bh->b_blocknr);
     223             : 
     224       34840 :                         if (batch_count)
     225         606 :                                 __flush_batch(journal, &batch_count);
     226       34840 :                         jbd2_log_start_commit(journal, tid);
     227             :                         /*
     228             :                          * jbd2_journal_commit_transaction() may want
     229             :                          * to take the checkpoint_mutex if JBD2_FLUSHED
     230             :                          * is set, jbd2_update_log_tail() called by
     231             :                          * jbd2_journal_commit_transaction() may also take
     232             :                          * checkpoint_mutex.  So we need to temporarily
     233             :                          * drop it.
     234             :                          */
     235       34840 :                         mutex_unlock(&journal->j_checkpoint_mutex);
     236       34838 :                         jbd2_log_wait_commit(journal, tid);
     237       34796 :                         mutex_lock_io(&journal->j_checkpoint_mutex);
     238       34840 :                         spin_lock(&journal->j_list_lock);
     239       34840 :                         goto restart;
     240             :                 }
     241      560643 :                 if (!trylock_buffer(bh)) {
     242             :                         /*
     243             :                          * The buffer is locked, it may be writing back, or
     244             :                          * flushing out in the last couple of cycles, or
     245             :                          * re-adding into a new transaction, need to check
     246             :                          * it again until it's unlocked.
     247             :                          */
     248       13218 :                         get_bh(bh);
     249       13218 :                         spin_unlock(&journal->j_list_lock);
     250       13218 :                         wait_on_buffer(bh);
     251             :                         /* the journal_head may have gone by now */
     252       13218 :                         BUFFER_TRACE(bh, "brelse");
     253       13218 :                         __brelse(bh);
     254       13218 :                         goto retry;
     255     1094850 :                 } else if (!buffer_dirty(bh)) {
     256      341154 :                         unlock_buffer(bh);
     257      341154 :                         BUFFER_TRACE(bh, "remove from checkpoint");
     258             :                         /*
     259             :                          * If the transaction was released or the checkpoint
     260             :                          * list was empty, we're done.
     261             :                          */
     262      341154 :                         if (__jbd2_journal_remove_checkpoint(jh) ||
     263      333506 :                             !transaction->t_checkpoint_list)
     264        7648 :                                 goto out;
     265             :                 } else {
     266      206271 :                         unlock_buffer(bh);
     267             :                         /*
     268             :                          * We are about to write the buffer, it could be
     269             :                          * raced by some other transaction shrink or buffer
     270             :                          * re-log logic once we release the j_list_lock,
     271             :                          * leave it on the checkpoint list and check status
     272             :                          * again to make sure it's clean.
     273             :                          */
     274      206271 :                         BUFFER_TRACE(bh, "queue");
     275      206271 :                         get_bh(bh);
     276      412542 :                         J_ASSERT_BH(bh, !buffer_jwrite(bh));
     277      206271 :                         journal->j_chkpt_bhs[batch_count++] = bh;
     278      206271 :                         transaction->t_chp_stats.cs_written++;
     279      206271 :                         transaction->t_checkpoint_list = jh->b_cpnext;
     280             :                 }
     281             : 
     282      539777 :                 if ((batch_count == JBD2_NR_BATCH) ||
     283      538568 :                     need_resched() || spin_needbreak(&journal->j_list_lock) ||
     284      533966 :                     jh2bh(transaction->t_checkpoint_list) == journal->j_chkpt_bhs[0])
     285       10316 :                         goto unlock_and_flush;
     286             :         }
     287             : 
     288           0 :         if (batch_count) {
     289           0 :                 unlock_and_flush:
     290       10316 :                         spin_unlock(&journal->j_list_lock);
     291       23534 :                 retry:
     292       23534 :                         if (batch_count)
     293        9664 :                                 __flush_batch(journal, &batch_count);
     294       23534 :                         spin_lock(&journal->j_list_lock);
     295       23534 :                         goto restart;
     296             :         }
     297             : 
     298           0 : out:
     299       41895 :         spin_unlock(&journal->j_list_lock);
     300       41895 :         result = jbd2_cleanup_journal_tail(journal);
     301             : 
     302       41895 :         return (result < 0) ? result : 0;
     303             : }
     304             : 
     305             : /*
     306             :  * Check the list of checkpoint transactions for the journal to see if
     307             :  * we have already got rid of any since the last update of the log tail
     308             :  * in the journal superblock.  If so, we can instantly roll the
     309             :  * superblock forward to remove those transactions from the log.
     310             :  *
     311             :  * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
     312             :  *
     313             :  * Called with the journal lock held.
     314             :  *
     315             :  * This is the only part of the journaling code which really needs to be
     316             :  * aware of transaction aborts.  Checkpointing involves writing to the
     317             :  * main filesystem area rather than to the journal, so it can proceed
     318             :  * even in abort state, but we must not update the super block if
     319             :  * checkpointing may have failed.  Otherwise, we would lose some metadata
     320             :  * buffers which should be written-back to the filesystem.
     321             :  */
     322             : 
     323       84811 : int jbd2_cleanup_journal_tail(journal_t *journal)
     324             : {
     325       84811 :         tid_t           first_tid;
     326       84811 :         unsigned long   blocknr;
     327             : 
     328       84811 :         if (is_journal_aborted(journal))
     329             :                 return -EIO;
     330             : 
     331       84696 :         if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
     332             :                 return 1;
     333        8118 :         J_ASSERT(blocknr != 0);
     334             : 
     335             :         /*
     336             :          * We need to make sure that any blocks that were recently written out
     337             :          * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
     338             :          * we drop the transactions from the journal. It's unlikely this will
     339             :          * be necessary, especially with an appropriately sized journal, but we
     340             :          * need this to guarantee correctness.  Fortunately
     341             :          * jbd2_cleanup_journal_tail() doesn't get called all that often.
     342             :          */
     343        8118 :         if (journal->j_flags & JBD2_BARRIER)
     344        8118 :                 blkdev_issue_flush(journal->j_fs_dev);
     345             : 
     346        8118 :         return __jbd2_update_log_tail(journal, first_tid, blocknr);
     347             : }
     348             : 
     349             : 
     350             : /* Checkpoint list management */
     351             : 
     352             : /*
     353             :  * journal_shrink_one_cp_list
     354             :  *
     355             :  * Find all the written-back checkpoint buffers in the given list
     356             :  * and try to release them. If the whole transaction is released, set
     357             :  * the 'released' parameter. Return the number of released checkpointed
     358             :  * buffers.
     359             :  *
     360             :  * Called with j_list_lock held.
     361             :  */
     362      248102 : static unsigned long journal_shrink_one_cp_list(struct journal_head *jh,
     363             :                                                 bool destroy, bool *released)
     364             : {
     365      248102 :         struct journal_head *last_jh;
     366      248102 :         struct journal_head *next_jh = jh;
     367      248102 :         unsigned long nr_freed = 0;
     368      248102 :         int ret;
     369             : 
     370      248102 :         *released = false;
     371      248102 :         if (!jh)
     372             :                 return 0;
     373             : 
     374      248102 :         last_jh = jh->b_cpprev;
     375     3940608 :         do {
     376     3940608 :                 jh = next_jh;
     377     3940608 :                 next_jh = jh->b_cpnext;
     378             : 
     379     3940608 :                 if (destroy) {
     380       22565 :                         ret = __jbd2_journal_remove_checkpoint(jh);
     381             :                 } else {
     382     3918043 :                         ret = jbd2_journal_try_remove_checkpoint(jh);
     383     3918043 :                         if (ret < 0)
     384     1903984 :                                 continue;
     385             :                 }
     386             : 
     387     2036624 :                 nr_freed++;
     388     2036624 :                 if (ret) {
     389      102385 :                         *released = true;
     390      102385 :                         break;
     391             :                 }
     392             : 
     393     1934239 :                 if (need_resched())
     394             :                         break;
     395     3838167 :         } while (jh != last_jh);
     396             : 
     397             :         return nr_freed;
     398             : }
     399             : 
     400             : /*
     401             :  * jbd2_journal_shrink_checkpoint_list
     402             :  *
     403             :  * Find 'nr_to_scan' written-back checkpoint buffers in the journal
     404             :  * and try to release them. Return the number of released checkpointed
     405             :  * buffers.
     406             :  *
     407             :  * Called with j_list_lock held.
     408             :  */
     409          51 : unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
     410             :                                                   unsigned long *nr_to_scan)
     411             : {
     412          51 :         transaction_t *transaction, *last_transaction, *next_transaction;
     413          51 :         bool __maybe_unused released;
     414          51 :         tid_t first_tid = 0, last_tid = 0, next_tid = 0;
     415          51 :         tid_t tid = 0;
     416          51 :         unsigned long nr_freed = 0;
     417          51 :         unsigned long freed;
     418             : 
     419          51 : again:
     420          51 :         spin_lock(&journal->j_list_lock);
     421          51 :         if (!journal->j_checkpoint_transactions) {
     422          38 :                 spin_unlock(&journal->j_list_lock);
     423          38 :                 goto out;
     424             :         }
     425             : 
     426             :         /*
     427             :          * Get next shrink transaction, resume previous scan or start
     428             :          * over again. If some others do checkpoint and drop transaction
     429             :          * from the checkpoint list, we ignore saved j_shrink_transaction
     430             :          * and start over unconditionally.
     431             :          */
     432          13 :         if (journal->j_shrink_transaction)
     433             :                 transaction = journal->j_shrink_transaction;
     434             :         else
     435           7 :                 transaction = journal->j_checkpoint_transactions;
     436             : 
     437          13 :         if (!first_tid)
     438          13 :                 first_tid = transaction->t_tid;
     439          13 :         last_transaction = journal->j_checkpoint_transactions->t_cpprev;
     440          13 :         next_transaction = transaction;
     441          13 :         last_tid = last_transaction->t_tid;
     442          22 :         do {
     443          22 :                 transaction = next_transaction;
     444          22 :                 next_transaction = transaction->t_cpnext;
     445          22 :                 tid = transaction->t_tid;
     446             : 
     447          22 :                 freed = journal_shrink_one_cp_list(transaction->t_checkpoint_list,
     448             :                                                    false, &released);
     449          22 :                 nr_freed += freed;
     450          22 :                 (*nr_to_scan) -= min(*nr_to_scan, freed);
     451          22 :                 if (*nr_to_scan == 0)
     452             :                         break;
     453          15 :                 if (need_resched() || spin_needbreak(&journal->j_list_lock))
     454             :                         break;
     455          15 :         } while (transaction != last_transaction);
     456             : 
     457          13 :         if (transaction != last_transaction) {
     458           6 :                 journal->j_shrink_transaction = next_transaction;
     459           6 :                 next_tid = next_transaction->t_tid;
     460             :         } else {
     461           7 :                 journal->j_shrink_transaction = NULL;
     462           7 :                 next_tid = 0;
     463             :         }
     464             : 
     465          13 :         spin_unlock(&journal->j_list_lock);
     466          13 :         cond_resched();
     467             : 
     468          13 :         if (*nr_to_scan && next_tid)
     469           0 :                 goto again;
     470          13 : out:
     471          51 :         trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
     472             :                                           nr_freed, next_tid);
     473             : 
     474          51 :         return nr_freed;
     475             : }
     476             : 
     477             : /*
     478             :  * journal_clean_checkpoint_list
     479             :  *
     480             :  * Find all the written-back checkpoint buffers in the journal and release them.
     481             :  * If 'destroy' is set, release all buffers unconditionally.
     482             :  *
     483             :  * Called with j_list_lock held.
     484             :  */
     485      207120 : void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
     486             : {
     487      207120 :         transaction_t *transaction, *last_transaction, *next_transaction;
     488      207120 :         bool released;
     489             : 
     490      207120 :         transaction = journal->j_checkpoint_transactions;
     491      207120 :         if (!transaction)
     492      147411 :                 return;
     493             : 
     494      205429 :         last_transaction = transaction->t_cpprev;
     495      205429 :         next_transaction = transaction;
     496      248080 :         do {
     497      248080 :                 transaction = next_transaction;
     498      248080 :                 next_transaction = transaction->t_cpnext;
     499      248080 :                 journal_shrink_one_cp_list(transaction->t_checkpoint_list,
     500             :                                            destroy, &released);
     501             :                 /*
     502             :                  * This function only frees up some memory if possible so we
     503             :                  * dont have an obligation to finish processing. Bail out if
     504             :                  * preemption requested:
     505             :                  */
     506      248080 :                 if (need_resched())
     507             :                         return;
     508             :                 /*
     509             :                  * Stop scanning if we couldn't free the transaction. This
     510             :                  * avoids pointless scanning of transactions which still
     511             :                  * weren't checkpointed.
     512             :                  */
     513      248015 :                 if (!released)
     514             :                         return;
     515      102360 :         } while (transaction != last_transaction);
     516             : }
     517             : 
     518             : /*
     519             :  * Remove buffers from all checkpoint lists as journal is aborted and we just
     520             :  * need to free memory
     521             :  */
     522         119 : void jbd2_journal_destroy_checkpoint(journal_t *journal)
     523             : {
     524             :         /*
     525             :          * We loop because __jbd2_journal_clean_checkpoint_list() may abort
     526             :          * early due to a need of rescheduling.
     527             :          */
     528         129 :         while (1) {
     529         248 :                 spin_lock(&journal->j_list_lock);
     530         248 :                 if (!journal->j_checkpoint_transactions) {
     531         119 :                         spin_unlock(&journal->j_list_lock);
     532         119 :                         break;
     533             :                 }
     534         129 :                 __jbd2_journal_clean_checkpoint_list(journal, true);
     535         129 :                 spin_unlock(&journal->j_list_lock);
     536         129 :                 cond_resched();
     537             :         }
     538         119 : }
     539             : 
     540             : /*
     541             :  * journal_remove_checkpoint: called after a buffer has been committed
     542             :  * to disk (either by being write-back flushed to disk, or being
     543             :  * committed to the log).
     544             :  *
     545             :  * We cannot safely clean a transaction out of the log until all of the
     546             :  * buffer updates committed in that transaction have safely been stored
     547             :  * elsewhere on disk.  To achieve this, all of the buffers in a
     548             :  * transaction need to be maintained on the transaction's checkpoint
     549             :  * lists until they have been rewritten, at which point this function is
     550             :  * called to remove the buffer from the existing transaction's
     551             :  * checkpoint lists.
     552             :  *
     553             :  * The function returns 1 if it frees the transaction, 0 otherwise.
     554             :  * The function can free jh and bh.
     555             :  *
     556             :  * This function is called with j_list_lock held.
     557             :  */
     558     3704044 : int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
     559             : {
     560     3704044 :         struct transaction_chp_stats_s *stats;
     561     3704044 :         transaction_t *transaction;
     562     3704044 :         journal_t *journal;
     563     3704044 :         struct buffer_head *bh = jh2bh(jh);
     564             : 
     565     3704044 :         JBUFFER_TRACE(jh, "entry");
     566             : 
     567     3704044 :         transaction = jh->b_cp_transaction;
     568     3704044 :         if (!transaction) {
     569             :                 JBUFFER_TRACE(jh, "not on transaction");
     570             :                 return 0;
     571             :         }
     572     3704044 :         journal = transaction->t_journal;
     573             : 
     574     3704044 :         JBUFFER_TRACE(jh, "removing from transaction");
     575             : 
     576             :         /*
     577             :          * If we have failed to write the buffer out to disk, the filesystem
     578             :          * may become inconsistent. We cannot abort the journal here since
     579             :          * we hold j_list_lock and we have to be careful about races with
     580             :          * jbd2_journal_destroy(). So mark the writeback IO error in the
     581             :          * journal here and we abort the journal later from a better context.
     582             :          */
     583     7408088 :         if (buffer_write_io_error(bh))
     584          17 :                 set_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags);
     585             : 
     586     3704044 :         __buffer_unlink(jh);
     587     3704044 :         jh->b_cp_transaction = NULL;
     588     3704044 :         percpu_counter_dec(&journal->j_checkpoint_jh_count);
     589     3704044 :         jbd2_journal_put_journal_head(jh);
     590             : 
     591             :         /* Is this transaction empty? */
     592     3704044 :         if (transaction->t_checkpoint_list)
     593             :                 return 0;
     594             : 
     595             :         /*
     596             :          * There is one special case to worry about: if we have just pulled the
     597             :          * buffer off a running or committing transaction's checkpoing list,
     598             :          * then even if the checkpoint list is empty, the transaction obviously
     599             :          * cannot be dropped!
     600             :          *
     601             :          * The locking here around t_state is a bit sleazy.
     602             :          * See the comment at the end of jbd2_journal_commit_transaction().
     603             :          */
     604      204562 :         if (transaction->t_state != T_FINISHED)
     605             :                 return 0;
     606             : 
     607             :         /*
     608             :          * OK, that was the last buffer for the transaction, we can now
     609             :          * safely remove this transaction from the log.
     610             :          */
     611      204562 :         stats = &transaction->t_chp_stats;
     612      204562 :         if (stats->cs_chp_time)
     613       15370 :                 stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
     614             :                                                     jiffies);
     615      204562 :         trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
     616             :                                     transaction->t_tid, stats);
     617             : 
     618      204562 :         __jbd2_journal_drop_transaction(journal, transaction);
     619      204562 :         jbd2_journal_free_transaction(transaction);
     620      204562 :         return 1;
     621             : }
     622             : 
     623             : /*
     624             :  * Check the checkpoint buffer and try to remove it from the checkpoint
     625             :  * list if it's clean. Returns -EBUSY if it is not clean, returns 1 if
     626             :  * it frees the transaction, 0 otherwise.
     627             :  *
     628             :  * This function is called with j_list_lock held.
     629             :  */
     630     4001986 : int jbd2_journal_try_remove_checkpoint(struct journal_head *jh)
     631             : {
     632     4001986 :         struct buffer_head *bh = jh2bh(jh);
     633             : 
     634     4001986 :         if (!trylock_buffer(bh))
     635             :                 return -EBUSY;
     636     7839068 :         if (buffer_dirty(bh)) {
     637     1904450 :                 unlock_buffer(bh);
     638     1904450 :                 return -EBUSY;
     639             :         }
     640     2015084 :         unlock_buffer(bh);
     641             : 
     642             :         /*
     643             :          * Buffer is clean and the IO has finished (we held the buffer
     644             :          * lock) so the checkpoint is done. We can safely remove the
     645             :          * buffer from this transaction.
     646             :          */
     647     2015084 :         JBUFFER_TRACE(jh, "remove from checkpoint list");
     648     2015084 :         return __jbd2_journal_remove_checkpoint(jh);
     649             : }
     650             : 
     651             : /*
     652             :  * journal_insert_checkpoint: put a committed buffer onto a checkpoint
     653             :  * list so that we know when it is safe to clean the transaction out of
     654             :  * the log.
     655             :  *
     656             :  * Called with the journal locked.
     657             :  * Called with j_list_lock held.
     658             :  */
     659     3703898 : void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
     660             :                                transaction_t *transaction)
     661             : {
     662     3703898 :         JBUFFER_TRACE(jh, "entry");
     663    11111694 :         J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
     664     3703898 :         J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
     665             : 
     666             :         /* Get reference for checkpointing transaction */
     667     3703898 :         jbd2_journal_grab_journal_head(jh2bh(jh));
     668     3703898 :         jh->b_cp_transaction = transaction;
     669             : 
     670     3703898 :         if (!transaction->t_checkpoint_list) {
     671      204561 :                 jh->b_cpnext = jh->b_cpprev = jh;
     672             :         } else {
     673     3499337 :                 jh->b_cpnext = transaction->t_checkpoint_list;
     674     3499337 :                 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
     675     3499337 :                 jh->b_cpprev->b_cpnext = jh;
     676     3499337 :                 jh->b_cpnext->b_cpprev = jh;
     677             :         }
     678     3703898 :         transaction->t_checkpoint_list = jh;
     679     3703898 :         percpu_counter_inc(&transaction->t_journal->j_checkpoint_jh_count);
     680     3703898 : }
     681             : 
     682             : /*
     683             :  * We've finished with this transaction structure: adios...
     684             :  *
     685             :  * The transaction must have no links except for the checkpoint by this
     686             :  * point.
     687             :  *
     688             :  * Called with the journal locked.
     689             :  * Called with j_list_lock held.
     690             :  */
     691             : 
     692      206992 : void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
     693             : {
     694      206992 :         assert_spin_locked(&journal->j_list_lock);
     695             : 
     696      206992 :         journal->j_shrink_transaction = NULL;
     697      206992 :         if (transaction->t_cpnext) {
     698      206992 :                 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
     699      206992 :                 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
     700      206992 :                 if (journal->j_checkpoint_transactions == transaction)
     701      110092 :                         journal->j_checkpoint_transactions =
     702             :                                 transaction->t_cpnext;
     703      206992 :                 if (journal->j_checkpoint_transactions == transaction)
     704       61286 :                         journal->j_checkpoint_transactions = NULL;
     705             :         }
     706             : 
     707      206992 :         J_ASSERT(transaction->t_state == T_FINISHED);
     708      206992 :         J_ASSERT(transaction->t_buffers == NULL);
     709      206992 :         J_ASSERT(transaction->t_forget == NULL);
     710      206992 :         J_ASSERT(transaction->t_shadow_list == NULL);
     711      206992 :         J_ASSERT(transaction->t_checkpoint_list == NULL);
     712      206992 :         J_ASSERT(atomic_read(&transaction->t_updates) == 0);
     713      206992 :         J_ASSERT(journal->j_committing_transaction != transaction);
     714      206992 :         J_ASSERT(journal->j_running_transaction != transaction);
     715             : 
     716      206992 :         trace_jbd2_drop_transaction(journal, transaction);
     717             : 
     718      206992 :         jbd2_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
     719      206992 : }

Generated by: LCOV version 1.14