LCOV - code coverage report
Current view: top level - lkbce/fs/jbd - checkpoint.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 359 0.0 %
Date: 2017-01-25 Functions: 0 16 0.0 %

          Line data    Source code
       1             : /*
       2             :  * linux/fs/jbd/checkpoint.c
       3             :  *
       4             :  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
       5             :  *
       6             :  * Copyright 1999 Red Hat Software --- All Rights Reserved
       7             :  *
       8             :  * This file is part of the Linux kernel and is made available under
       9             :  * the terms of the GNU General Public License, version 2, or at your
      10             :  * option, any later version, incorporated herein by reference.
      11             :  *
      12             :  * Checkpoint routines for the generic filesystem journaling code.
      13             :  * Part of the ext2fs journaling system.
      14             :  *
      15             :  * Checkpointing is the process of ensuring that a section of the log is
      16             :  * committed fully to disk, so that that portion of the log can be
      17             :  * reused.
      18             :  */
      19             : 
      20             : #include <linux/time.h>
      21             : #include <linux/fs.h>
      22             : #include <linux/jbd.h>
      23             : #include <linux/errno.h>
      24             : #include <linux/slab.h>
      25             : 
      26             : /*
      27             :  * Unlink a buffer from a transaction checkpoint list.
      28             :  *
      29             :  * Called with j_list_lock held.
      30             :  */
      31             : static inline void __buffer_unlink_first(struct journal_head *jh)
      32             : {
      33           0 :         transaction_t *transaction = jh->b_cp_transaction;
      34             : 
      35           0 :         jh->b_cpnext->b_cpprev = jh->b_cpprev;
      36           0 :         jh->b_cpprev->b_cpnext = jh->b_cpnext;
      37           0 :         if (transaction->t_checkpoint_list == jh) {
      38           0 :                 transaction->t_checkpoint_list = jh->b_cpnext;
      39           0 :                 if (transaction->t_checkpoint_list == jh)
      40           0 :                         transaction->t_checkpoint_list = NULL;
      41             :         }
      42           0 : }
      43             : 
      44             : /*
      45             :  * Unlink a buffer from a transaction checkpoint(io) list.
      46             :  *
      47             :  * Called with j_list_lock held.
      48             :  */
      49             : static inline void __buffer_unlink(struct journal_head *jh)
      50             : {
      51           0 :         transaction_t *transaction = jh->b_cp_transaction;
      52             : 
      53           0 :         __buffer_unlink_first(jh);
      54           0 :         if (transaction->t_checkpoint_io_list == jh) {
      55           0 :                 transaction->t_checkpoint_io_list = jh->b_cpnext;
      56           0 :                 if (transaction->t_checkpoint_io_list == jh)
      57           0 :                         transaction->t_checkpoint_io_list = NULL;
      58             :         }
      59           0 : }
      60             : 
      61             : /*
      62             :  * Move a buffer from the checkpoint list to the checkpoint io list
      63             :  *
      64             :  * Called with j_list_lock held
      65             :  */
      66             : static inline void __buffer_relink_io(struct journal_head *jh)
      67             : {
      68           0 :         transaction_t *transaction = jh->b_cp_transaction;
      69           0 : 
      70           0 :         __buffer_unlink_first(jh);
      71             : 
      72           0 :         if (!transaction->t_checkpoint_io_list) {
      73           0 :                 jh->b_cpnext = jh->b_cpprev = jh;
      74             :         } else {
      75           0 :                 jh->b_cpnext = transaction->t_checkpoint_io_list;
      76           0 :                 jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;
      77           0 :                 jh->b_cpprev->b_cpnext = jh;
      78           0 :                 jh->b_cpnext->b_cpprev = jh;
      79             :         }
      80           0 :         transaction->t_checkpoint_io_list = jh;
      81           0 : }
      82             : 
      83             : /*
      84             :  * Try to release a checkpointed buffer from its transaction.
      85             :  * Returns 1 if we released it and 2 if we also released the
      86             :  * whole transaction.
      87             :  *
      88             :  * Requires j_list_lock
      89             :  * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
      90             :  */
      91             : static int __try_to_free_cp_buf(struct journal_head *jh)
      92             : {
      93           0 :         int ret = 0;
      94           0 :         struct buffer_head *bh = jh2bh(jh);
      95           0 : 
      96           0 :         if (jh->b_jlist == BJ_None && !buffer_locked(bh) &&
      97           0 :             !buffer_dirty(bh) && !buffer_write_io_error(bh)) {
      98           0 :                 JBUFFER_TRACE(jh, "remove from checkpoint list");
      99           0 :                 ret = __journal_remove_checkpoint(jh) + 1;
     100           0 :                 jbd_unlock_bh_state(bh);
     101           0 :                 journal_remove_journal_head(bh);
     102             :                 BUFFER_TRACE(bh, "release");
     103           0 :                 __brelse(bh);
     104             :         } else {
     105           0 :                 jbd_unlock_bh_state(bh);
     106             :         }
     107           0 :         return ret;
     108             : }
     109             : 
     110             : /*
     111             :  * __log_wait_for_space: wait until there is space in the journal.
     112             :  *
     113             :  * Called under j-state_lock *only*.  It will be unlocked if we have to wait
     114             :  * for a checkpoint to free up some space in the log.
     115             :  */
     116             : void __log_wait_for_space(journal_t *journal)
     117             : {
     118           0 :         int nblocks, space_left;
     119           0 :         assert_spin_locked(&journal->j_state_lock);
     120           0 : 
     121           0 :         nblocks = jbd_space_needed(journal);
     122           0 :         while (__log_space_left(journal) < nblocks) {
     123           0 :                 if (journal->j_flags & JFS_ABORT)
     124           0 :                         return;
     125           0 :                 spin_unlock(&journal->j_state_lock);
     126           0 :                 mutex_lock(&journal->j_checkpoint_mutex);
     127             : 
     128             :                 /*
     129           0 :                  * Test again, another process may have checkpointed while we
     130             :                  * were waiting for the checkpoint lock. If there are no
     131             :                  * transactions ready to be checkpointed, try to recover
     132             :                  * journal space by calling cleanup_journal_tail(), and if
     133             :                  * that doesn't work, by waiting for the currently committing
     134             :                  * transaction to complete.  If there is absolutely no way
     135             :                  * to make progress, this is either a BUG or corrupted
     136             :                  * filesystem, so abort the journal and leave a stack
     137             :                  * trace for forensic evidence.
     138             :                  */
     139           0 :                 spin_lock(&journal->j_state_lock);
     140           0 :                 spin_lock(&journal->j_list_lock);
     141           0 :                 nblocks = jbd_space_needed(journal);
     142           0 :                 space_left = __log_space_left(journal);
     143           0 :                 if (space_left < nblocks) {
     144           0 :                         int chkpt = journal->j_checkpoint_transactions != NULL;
     145           0 :                         tid_t tid = 0;
     146             : 
     147           0 :                         if (journal->j_committing_transaction)
     148           0 :                                 tid = journal->j_committing_transaction->t_tid;
     149           0 :                         spin_unlock(&journal->j_list_lock);
     150           0 :                         spin_unlock(&journal->j_state_lock);
     151           0 :                         if (chkpt) {
     152           0 :                                 log_do_checkpoint(journal);
     153           0 :                         } else if (cleanup_journal_tail(journal) == 0) {
     154             :                                 /* We were able to recover space; yay! */
     155             :                                 ;
     156           0 :                         } else if (tid) {
     157           0 :                                 log_wait_commit(journal, tid);
     158             :                         } else {
     159           0 :                                 printk(KERN_ERR "%s: needed %d blocks and "
     160             :                                        "only had %d space available\n",
     161             :                                        __func__, nblocks, space_left);
     162           0 :                                 printk(KERN_ERR "%s: no way to get more "
     163             :                                        "journal space\n", __func__);
     164           0 :                                 WARN_ON(1);
     165           0 :                                 journal_abort(journal, 0);
     166             :                         }
     167           0 :                         spin_lock(&journal->j_state_lock);
     168             :                 } else {
     169           0 :                         spin_unlock(&journal->j_list_lock);
     170             :                 }
     171           0 :                 mutex_unlock(&journal->j_checkpoint_mutex);
     172             :         }
     173           0 : }
     174             : 
     175             : /*
     176             :  * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
     177             :  * The caller must restart a list walk.  Wait for someone else to run
     178             :  * jbd_unlock_bh_state().
     179             :  */
     180             : static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)
     181             :         __releases(journal->j_list_lock)
     182             : {
     183           0 :         get_bh(bh);
     184           0 :         spin_unlock(&journal->j_list_lock);
     185           0 :         jbd_lock_bh_state(bh);
     186           0 :         jbd_unlock_bh_state(bh);
     187           0 :         put_bh(bh);
     188           0 : }
     189             : 
     190             : /*
     191             :  * Clean up transaction's list of buffers submitted for io.
     192             :  * We wait for any pending IO to complete and remove any clean
     193             :  * buffers. Note that we take the buffers in the opposite ordering
     194             :  * from the one in which they were submitted for IO.
     195             :  *
     196             :  * Return 0 on success, and return <0 if some buffers have failed
     197             :  * to be written out.
     198             :  *
     199             :  * Called with j_list_lock held.
     200             :  */
     201             : static int __wait_cp_io(journal_t *journal, transaction_t *transaction)
     202             : {
     203           0 :         struct journal_head *jh;
     204           0 :         struct buffer_head *bh;
     205           0 :         tid_t this_tid;
     206           0 :         int released = 0;
     207           0 :         int ret = 0;
     208           0 : 
     209           0 :         this_tid = transaction->t_tid;
     210           0 : restart:
     211           0 :         /* Did somebody clean up the transaction in the meanwhile? */
     212           0 :         if (journal->j_checkpoint_transactions != transaction ||
     213             :                         transaction->t_tid != this_tid)
     214           0 :                 return ret;
     215           0 :         while (!released && transaction->t_checkpoint_io_list) {
     216           0 :                 jh = transaction->t_checkpoint_io_list;
     217           0 :                 bh = jh2bh(jh);
     218           0 :                 if (!jbd_trylock_bh_state(bh)) {
     219           0 :                         jbd_sync_bh(journal, bh);
     220           0 :                         spin_lock(&journal->j_list_lock);
     221           0 :                         goto restart;
     222             :                 }
     223           0 :                 if (buffer_locked(bh)) {
     224           0 :                         atomic_inc(&bh->b_count);
     225           0 :                         spin_unlock(&journal->j_list_lock);
     226           0 :                         jbd_unlock_bh_state(bh);
     227           0 :                         wait_on_buffer(bh);
     228             :                         /* the journal_head may have gone by now */
     229             :                         BUFFER_TRACE(bh, "brelse");
     230           0 :                         __brelse(bh);
     231           0 :                         spin_lock(&journal->j_list_lock);
     232           0 :                         goto restart;
     233             :                 }
     234           0 :                 if (unlikely(buffer_write_io_error(bh)))
     235           0 :                         ret = -EIO;
     236             : 
     237             :                 /*
     238             :                  * Now in whatever state the buffer currently is, we know that
     239             :                  * it has been written out and so we can drop it from the list
     240             :                  */
     241           0 :                 released = __journal_remove_checkpoint(jh);
     242           0 :                 jbd_unlock_bh_state(bh);
     243           0 :                 journal_remove_journal_head(bh);
     244           0 :                 __brelse(bh);
     245             :         }
     246           0 : 
     247           0 :         return ret;
     248             : }
     249             : 
     250             : #define NR_BATCH        64
     251             : 
     252             : static void
     253             : __flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count)
     254             : {
     255           0 :         int i;
     256           0 : 
     257           0 :         ll_rw_block(SWRITE, *batch_count, bhs);
     258           0 :         for (i = 0; i < *batch_count; i++) {
     259           0 :                 struct buffer_head *bh = bhs[i];
     260           0 :                 clear_buffer_jwrite(bh);
     261             :                 BUFFER_TRACE(bh, "brelse");
     262           0 :                 __brelse(bh);
     263             :         }
     264           0 :         *batch_count = 0;
     265           0 : }
     266             : 
     267             : /*
     268             :  * Try to flush one buffer from the checkpoint list to disk.
     269             :  *
     270             :  * Return 1 if something happened which requires us to abort the current
     271             :  * scan of the checkpoint list.  Return <0 if the buffer has failed to
     272             :  * be written out.
     273             :  *
     274             :  * Called with j_list_lock held and drops it if 1 is returned
     275             :  * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
     276             :  */
     277             : static int __process_buffer(journal_t *journal, struct journal_head *jh,
     278             :                         struct buffer_head **bhs, int *batch_count)
     279             : {
     280           0 :         struct buffer_head *bh = jh2bh(jh);
     281           0 :         int ret = 0;
     282           0 : 
     283           0 :         if (buffer_locked(bh)) {
     284           0 :                 atomic_inc(&bh->b_count);
     285           0 :                 spin_unlock(&journal->j_list_lock);
     286           0 :                 jbd_unlock_bh_state(bh);
     287           0 :                 wait_on_buffer(bh);
     288           0 :                 /* the journal_head may have gone by now */
     289           0 :                 BUFFER_TRACE(bh, "brelse");
     290           0 :                 __brelse(bh);
     291           0 :                 ret = 1;
     292           0 :         } else if (jh->b_transaction != NULL) {
     293           0 :                 transaction_t *t = jh->b_transaction;
     294           0 :                 tid_t tid = t->t_tid;
     295             : 
     296           0 :                 spin_unlock(&journal->j_list_lock);
     297           0 :                 jbd_unlock_bh_state(bh);
     298           0 :                 log_start_commit(journal, tid);
     299           0 :                 log_wait_commit(journal, tid);
     300           0 :                 ret = 1;
     301           0 :         } else if (!buffer_dirty(bh)) {
     302           0 :                 ret = 1;
     303           0 :                 if (unlikely(buffer_write_io_error(bh)))
     304           0 :                         ret = -EIO;
     305           0 :                 J_ASSERT_JH(jh, !buffer_jbddirty(bh));
     306             :                 BUFFER_TRACE(bh, "remove from checkpoint");
     307           0 :                 __journal_remove_checkpoint(jh);
     308           0 :                 spin_unlock(&journal->j_list_lock);
     309           0 :                 jbd_unlock_bh_state(bh);
     310           0 :                 journal_remove_journal_head(bh);
     311           0 :                 __brelse(bh);
     312             :         } else {
     313             :                 /*
     314             :                  * Important: we are about to write the buffer, and
     315             :                  * possibly block, while still holding the journal lock.
     316             :                  * We cannot afford to let the transaction logic start
     317             :                  * messing around with this buffer before we write it to
     318             :                  * disk, as that would break recoverability.
     319             :                  */
     320             :                 BUFFER_TRACE(bh, "queue");
     321           0 :                 get_bh(bh);
     322           0 :                 J_ASSERT_BH(bh, !buffer_jwrite(bh));
     323           0 :                 set_buffer_jwrite(bh);
     324           0 :                 bhs[*batch_count] = bh;
     325           0 :                 __buffer_relink_io(jh);
     326           0 :                 jbd_unlock_bh_state(bh);
     327           0 :                 (*batch_count)++;
     328           0 :                 if (*batch_count == NR_BATCH) {
     329           0 :                         spin_unlock(&journal->j_list_lock);
     330           0 :                         __flush_batch(journal, bhs, batch_count);
     331           0 :                         ret = 1;
     332             :                 }
     333             :         }
     334           0 :         return ret;
     335             : }
     336             : 
     337             : /*
     338             :  * Perform an actual checkpoint. We take the first transaction on the
     339             :  * list of transactions to be checkpointed and send all its buffers
     340             :  * to disk. We submit larger chunks of data at once.
     341             :  *
     342             :  * The journal should be locked before calling this function.
     343             :  * Called with j_checkpoint_mutex held.
     344             :  */
     345             : int log_do_checkpoint(journal_t *journal)
     346             : {
     347           0 :         transaction_t *transaction;
     348           0 :         tid_t this_tid;
     349           0 :         int result;
     350           0 : 
     351           0 :         jbd_debug(1, "Start checkpoint\n");
     352           0 : 
     353           0 :         /*
     354           0 :          * First thing: if there are any transactions in the log which
     355           0 :          * don't need checkpointing, just eliminate them from the
     356           0 :          * journal straight away.
     357           0 :          */
     358           0 :         result = cleanup_journal_tail(journal);
     359             :         jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
     360           0 :         if (result <= 0)
     361           0 :                 return result;
     362             : 
     363             :         /*
     364             :          * OK, we need to start writing disk blocks.  Take one transaction
     365             :          * and write it.
     366             :          */
     367           0 :         result = 0;
     368           0 :         spin_lock(&journal->j_list_lock);
     369           0 :         if (!journal->j_checkpoint_transactions)
     370           0 :                 goto out;
     371           0 :         transaction = journal->j_checkpoint_transactions;
     372           0 :         this_tid = transaction->t_tid;
     373           0 : restart:
     374             :         /*
     375             :          * If someone cleaned up this transaction while we slept, we're
     376             :          * done (maybe it's a new transaction, but it fell at the same
     377             :          * address).
     378             :          */
     379           0 :         if (journal->j_checkpoint_transactions == transaction &&
     380             :                         transaction->t_tid == this_tid) {
     381           0 :                 int batch_count = 0;
     382             :                 struct buffer_head *bhs[NR_BATCH];
     383             :                 struct journal_head *jh;
     384           0 :                 int retry = 0, err;
     385             : 
     386           0 :                 while (!retry && transaction->t_checkpoint_list) {
     387           0 :                         struct buffer_head *bh;
     388           0 : 
     389           0 :                         jh = transaction->t_checkpoint_list;
     390           0 :                         bh = jh2bh(jh);
     391           0 :                         if (!jbd_trylock_bh_state(bh)) {
     392           0 :                                 jbd_sync_bh(journal, bh);
     393           0 :                                 retry = 1;
     394           0 :                                 break;
     395             :                         }
     396           0 :                         retry = __process_buffer(journal, jh, bhs,&batch_count);
     397           0 :                         if (retry < 0 && !result)
     398           0 :                                 result = retry;
     399           0 :                         if (!retry && (need_resched() ||
     400             :                                 spin_needbreak(&journal->j_list_lock))) {
     401           0 :                                 spin_unlock(&journal->j_list_lock);
     402           0 :                                 retry = 1;
     403           0 :                                 break;
     404             :                         }
     405             :                 }
     406             : 
     407           0 :                 if (batch_count) {
     408           0 :                         if (!retry) {
     409           0 :                                 spin_unlock(&journal->j_list_lock);
     410           0 :                                 retry = 1;
     411           0 :                         }
     412           0 :                         __flush_batch(journal, bhs, &batch_count);
     413             :                 }
     414             : 
     415           0 :                 if (retry) {
     416           0 :                         spin_lock(&journal->j_list_lock);
     417           0 :                         goto restart;
     418             :                 }
     419             :                 /*
     420             :                  * Now we have cleaned up the first transaction's checkpoint
     421             :                  * list. Let's clean up the second one
     422             :                  */
     423           0 :                 err = __wait_cp_io(journal, transaction);
     424           0 :                 if (!result)
     425           0 :                         result = err;
     426             :         }
     427             : out:
     428           0 :         spin_unlock(&journal->j_list_lock);
     429           0 :         if (result < 0)
     430           0 :                 journal_abort(journal, result);
     431             :         else
     432           0 :                 result = cleanup_journal_tail(journal);
     433             : 
     434           0 :         return (result < 0) ? result : 0;
     435             : }
     436             : 
     437             : /*
     438             :  * Check the list of checkpoint transactions for the journal to see if
     439             :  * we have already got rid of any since the last update of the log tail
     440             :  * in the journal superblock.  If so, we can instantly roll the
     441             :  * superblock forward to remove those transactions from the log.
     442             :  *
     443             :  * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
     444             :  *
     445             :  * Called with the journal lock held.
     446             :  *
     447             :  * This is the only part of the journaling code which really needs to be
     448             :  * aware of transaction aborts.  Checkpointing involves writing to the
     449             :  * main filesystem area rather than to the journal, so it can proceed
     450             :  * even in abort state, but we must not update the super block if
     451             :  * checkpointing may have failed.  Otherwise, we would lose some metadata
     452             :  * buffers which should be written-back to the filesystem.
     453             :  */
     454             : 
     455             : int cleanup_journal_tail(journal_t *journal)
     456             : {
     457           0 :         transaction_t * transaction;
     458           0 :         tid_t           first_tid;
     459           0 :         unsigned int    blocknr, freed;
     460           0 : 
     461           0 :         if (is_journal_aborted(journal))
     462           0 :                 return 1;
     463             : 
     464             :         /* OK, work out the oldest transaction remaining in the log, and
     465             :          * the log block it starts at.
     466             :          *
     467             :          * If the log is now empty, we need to work out which is the
     468             :          * next transaction ID we will write, and where it will
     469             :          * start. */
     470             : 
     471           0 :         spin_lock(&journal->j_state_lock);
     472           0 :         spin_lock(&journal->j_list_lock);
     473           0 :         transaction = journal->j_checkpoint_transactions;
     474           0 :         if (transaction) {
     475           0 :                 first_tid = transaction->t_tid;
     476           0 :                 blocknr = transaction->t_log_start;
     477           0 :         } else if ((transaction = journal->j_committing_transaction) != NULL) {
     478           0 :                 first_tid = transaction->t_tid;
     479           0 :                 blocknr = transaction->t_log_start;
     480           0 :         } else if ((transaction = journal->j_running_transaction) != NULL) {
     481           0 :                 first_tid = transaction->t_tid;
     482           0 :                 blocknr = journal->j_head;
     483             :         } else {
     484           0 :                 first_tid = journal->j_transaction_sequence;
     485           0 :                 blocknr = journal->j_head;
     486             :         }
     487           0 :         spin_unlock(&journal->j_list_lock);
     488           0 :         J_ASSERT(blocknr != 0);
     489             : 
     490             :         /* If the oldest pinned transaction is at the tail of the log
     491             :            already then there's not much we can do right now. */
     492           0 :         if (journal->j_tail_sequence == first_tid) {
     493           0 :                 spin_unlock(&journal->j_state_lock);
     494           0 :                 return 1;
     495             :         }
     496             : 
     497             :         /* OK, update the superblock to recover the freed space.
     498             :          * Physical blocks come first: have we wrapped beyond the end of
     499             :          * the log?  */
     500           0 :         freed = blocknr - journal->j_tail;
     501           0 :         if (blocknr < journal->j_tail)
     502           0 :                 freed = freed + journal->j_last - journal->j_first;
     503             : 
     504             :         jbd_debug(1,
     505             :                   "Cleaning journal tail from %d to %d (offset %u), "
     506             :                   "freeing %u\n",
     507             :                   journal->j_tail_sequence, first_tid, blocknr, freed);
     508             : 
     509           0 :         journal->j_free += freed;
     510           0 :         journal->j_tail_sequence = first_tid;
     511           0 :         journal->j_tail = blocknr;
     512           0 :         spin_unlock(&journal->j_state_lock);
     513           0 :         if (!(journal->j_flags & JFS_ABORT))
     514           0 :                 journal_update_superblock(journal, 1);
     515           0 :         return 0;
     516             : }
     517             : 
     518             : 
     519             : /* Checkpoint list management */
     520             : 
     521             : /*
     522             :  * journal_clean_one_cp_list
     523             :  *
     524             :  * Find all the written-back checkpoint buffers in the given list and release them.
     525             :  *
     526             :  * Called with the journal locked.
     527             :  * Called with j_list_lock held.
     528             :  * Returns number of bufers reaped (for debug)
     529             :  */
     530             : 
     531             : static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
     532             : {
     533           0 :         struct journal_head *last_jh;
     534           0 :         struct journal_head *next_jh = jh;
     535           0 :         int ret, freed = 0;
     536           0 : 
     537           0 :         *released = 0;
     538           0 :         if (!jh)
     539           0 :                 return 0;
     540             : 
     541           0 :         last_jh = jh->b_cpprev;
     542           0 :         do {
     543           0 :                 jh = next_jh;
     544           0 :                 next_jh = jh->b_cpnext;
     545             :                 /* Use trylock because of the ranking */
     546           0 :                 if (jbd_trylock_bh_state(jh2bh(jh))) {
     547           0 :                         ret = __try_to_free_cp_buf(jh);
     548           0 :                         if (ret) {
     549           0 :                                 freed++;
     550           0 :                                 if (ret == 2) {
     551           0 :                                         *released = 1;
     552           0 :                                         return freed;
     553             :                                 }
     554             :                         }
     555             :                 }
     556             :                 /*
     557             :                  * This function only frees up some memory
     558             :                  * if possible so we dont have an obligation
     559             :                  * to finish processing. Bail out if preemption
     560             :                  * requested:
     561             :                  */
     562           0 :                 if (need_resched())
     563           0 :                         return freed;
     564           0 :         } while (jh != last_jh);
     565             : 
     566           0 :         return freed;
     567             : }
     568             : 
     569             : /*
     570             :  * journal_clean_checkpoint_list
     571             :  *
     572             :  * Find all the written-back checkpoint buffers in the journal and release them.
     573             :  *
     574             :  * Called with the journal locked.
     575             :  * Called with j_list_lock held.
     576             :  * Returns number of buffers reaped (for debug)
     577             :  */
     578             : 
     579             : int __journal_clean_checkpoint_list(journal_t *journal)
     580             : {
     581           0 :         transaction_t *transaction, *last_transaction, *next_transaction;
     582           0 :         int ret = 0;
     583           0 :         int released;
     584           0 : 
     585           0 :         transaction = journal->j_checkpoint_transactions;
     586           0 :         if (!transaction)
     587           0 :                 goto out;
     588           0 : 
     589           0 :         last_transaction = transaction->t_cpprev;
     590           0 :         next_transaction = transaction;
     591           0 :         do {
     592           0 :                 transaction = next_transaction;
     593           0 :                 next_transaction = transaction->t_cpnext;
     594           0 :                 ret += journal_clean_one_cp_list(transaction->
     595             :                                 t_checkpoint_list, &released);
     596             :                 /*
     597             :                  * This function only frees up some memory if possible so we
     598             :                  * dont have an obligation to finish processing. Bail out if
     599             :                  * preemption requested:
     600             :                  */
     601           0 :                 if (need_resched())
     602           0 :                         goto out;
     603           0 :                 if (released)
     604           0 :                         continue;
     605             :                 /*
     606             :                  * It is essential that we are as careful as in the case of
     607             :                  * t_checkpoint_list with removing the buffer from the list as
     608             :                  * we can possibly see not yet submitted buffers on io_list
     609             :                  */
     610           0 :                 ret += journal_clean_one_cp_list(transaction->
     611             :                                 t_checkpoint_io_list, &released);
     612           0 :                 if (need_resched())
     613           0 :                         goto out;
     614           0 :         } while (transaction != last_transaction);
     615             : out:
     616           0 :         return ret;
     617           0 : }
     618             : 
     619             : /*
     620             :  * journal_remove_checkpoint: called after a buffer has been committed
     621           0 :  * to disk (either by being write-back flushed to disk, or being
     622             :  * committed to the log).
     623             :  *
     624             :  * We cannot safely clean a transaction out of the log until all of the
     625             :  * buffer updates committed in that transaction have safely been stored
     626             :  * elsewhere on disk.  To achieve this, all of the buffers in a
     627             :  * transaction need to be maintained on the transaction's checkpoint
     628             :  * lists until they have been rewritten, at which point this function is
     629             :  * called to remove the buffer from the existing transaction's
     630             :  * checkpoint lists.
     631             :  *
     632             :  * The function returns 1 if it frees the transaction, 0 otherwise.
     633             :  *
     634             :  * This function is called with the journal locked.
     635             :  * This function is called with j_list_lock held.
     636             :  * This function is called with jbd_lock_bh_state(jh2bh(jh))
     637             :  */
     638             : 
     639             : int __journal_remove_checkpoint(struct journal_head *jh)
     640             : {
     641           0 :         transaction_t *transaction;
     642           0 :         journal_t *journal;
     643           0 :         int ret = 0;
     644             : 
     645             :         JBUFFER_TRACE(jh, "entry");
     646             : 
     647           0 :         if ((transaction = jh->b_cp_transaction) == NULL) {
     648             :                 JBUFFER_TRACE(jh, "not on transaction");
     649           0 :                 goto out;
     650             :         }
     651           0 :         journal = transaction->t_journal;
     652             : 
     653           0 :         __buffer_unlink(jh);
     654           0 :         jh->b_cp_transaction = NULL;
     655             : 
     656           0 :         if (transaction->t_checkpoint_list != NULL ||
     657             :             transaction->t_checkpoint_io_list != NULL)
     658           0 :                 goto out;
     659             :         JBUFFER_TRACE(jh, "transaction has no more buffers");
     660             : 
     661             :         /*
     662             :          * There is one special case to worry about: if we have just pulled the
     663             :          * buffer off a running or committing transaction's checkpoing list,
     664             :          * then even if the checkpoint list is empty, the transaction obviously
     665             :          * cannot be dropped!
     666             :          *
     667             :          * The locking here around t_state is a bit sleazy.
     668             :          * See the comment at the end of journal_commit_transaction().
     669             :          */
     670           0 :         if (transaction->t_state != T_FINISHED) {
     671             :                 JBUFFER_TRACE(jh, "belongs to running/committing transaction");
     672           0 :                 goto out;
     673             :         }
     674             : 
     675             :         /* OK, that was the last buffer for the transaction: we can now
     676             :            safely remove this transaction from the log */
     677             : 
     678           0 :         __journal_drop_transaction(journal, transaction);
     679             : 
     680             :         /* Just in case anybody was waiting for more transactions to be
     681             :            checkpointed... */
     682           0 :         wake_up(&journal->j_wait_logspace);
     683           0 :         ret = 1;
     684             : out:
     685           0 :         JBUFFER_TRACE(jh, "exit");
     686           0 :         return ret;
     687             : }
     688             : 
     689             : /*
     690             :  * journal_insert_checkpoint: put a committed buffer onto a checkpoint
     691             :  * list so that we know when it is safe to clean the transaction out of
     692             :  * the log.
     693             :  *
     694             :  * Called with the journal locked.
     695             :  * Called with j_list_lock held.
     696             :  */
     697             : void __journal_insert_checkpoint(struct journal_head *jh,
     698             :                                transaction_t *transaction)
     699           0 : {
     700           0 :         JBUFFER_TRACE(jh, "entry");
     701           0 :         J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
     702           0 :         J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
     703           0 : 
     704           0 :         jh->b_cp_transaction = transaction;
     705           0 : 
     706           0 :         if (!transaction->t_checkpoint_list) {
     707           0 :                 jh->b_cpnext = jh->b_cpprev = jh;
     708             :         } else {
     709           0 :                 jh->b_cpnext = transaction->t_checkpoint_list;
     710           0 :                 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
     711           0 :                 jh->b_cpprev->b_cpnext = jh;
     712           0 :                 jh->b_cpnext->b_cpprev = jh;
     713             :         }
     714           0 :         transaction->t_checkpoint_list = jh;
     715           0 : }
     716             : 
     717             : /*
     718             :  * We've finished with this transaction structure: adios...
     719             :  *
     720             :  * The transaction must have no links except for the checkpoint by this
     721             :  * point.
     722             :  *
     723             :  * Called with the journal locked.
     724             :  * Called with j_list_lock held.
     725             :  */
     726             : 
     727             : void __journal_drop_transaction(journal_t *journal, transaction_t *transaction)
     728             : {
     729           0 :         assert_spin_locked(&journal->j_list_lock);
     730           0 :         if (transaction->t_cpnext) {
     731           0 :                 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
     732           0 :                 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
     733           0 :                 if (journal->j_checkpoint_transactions == transaction)
     734           0 :                         journal->j_checkpoint_transactions =
     735           0 :                                 transaction->t_cpnext;
     736           0 :                 if (journal->j_checkpoint_transactions == transaction)
     737           0 :                         journal->j_checkpoint_transactions = NULL;
     738           0 :         }
     739           0 : 
     740           0 :         J_ASSERT(transaction->t_state == T_FINISHED);
     741           0 :         J_ASSERT(transaction->t_buffers == NULL);
     742           0 :         J_ASSERT(transaction->t_sync_datalist == NULL);
     743           0 :         J_ASSERT(transaction->t_forget == NULL);
     744           0 :         J_ASSERT(transaction->t_iobuf_list == NULL);
     745           0 :         J_ASSERT(transaction->t_shadow_list == NULL);
     746           0 :         J_ASSERT(transaction->t_log_list == NULL);
     747           0 :         J_ASSERT(transaction->t_checkpoint_list == NULL);
     748           0 :         J_ASSERT(transaction->t_checkpoint_io_list == NULL);
     749           0 :         J_ASSERT(transaction->t_updates == 0);
     750           0 :         J_ASSERT(journal->j_committing_transaction != transaction);
     751           0 :         J_ASSERT(journal->j_running_transaction != transaction);
     752             : 
     753             :         jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
     754           0 :         kfree(transaction);
     755           0 : }

Generated by: LCOV version 1.10