1 // SPDX-License-Identifier: GPL-2.0+
3 * linux/fs/jbd2/checkpoint.c
5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
7 * Copyright 1999 Red Hat Software --- All Rights Reserved
9 * Checkpoint routines for the generic filesystem journaling code.
10 * Part of the ext2fs journaling system.
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
17 #include <linux/time.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>
26 * Unlink a buffer from a transaction checkpoint list.
28 * Called with j_list_lock held.
30 static inline void __buffer_unlink_first(struct journal_head *jh)
32 transaction_t *transaction = jh->b_cp_transaction;
34 jh->b_cpnext->b_cpprev = jh->b_cpprev;
35 jh->b_cpprev->b_cpnext = jh->b_cpnext;
36 if (transaction->t_checkpoint_list == jh) {
37 transaction->t_checkpoint_list = jh->b_cpnext;
38 if (transaction->t_checkpoint_list == jh)
39 transaction->t_checkpoint_list = NULL;
44 * Unlink a buffer from a transaction checkpoint(io) list.
46 * Called with j_list_lock held.
48 static inline void __buffer_unlink(struct journal_head *jh)
50 transaction_t *transaction = jh->b_cp_transaction;
52 __buffer_unlink_first(jh);
53 if (transaction->t_checkpoint_io_list == jh) {
54 transaction->t_checkpoint_io_list = jh->b_cpnext;
55 if (transaction->t_checkpoint_io_list == jh)
56 transaction->t_checkpoint_io_list = NULL;
61 * Check a checkpoint buffer could be release or not.
63 * Requires j_list_lock
65 static inline bool __cp_buffer_busy(struct journal_head *jh)
67 struct buffer_head *bh = jh2bh(jh);
69 return (jh->b_transaction || buffer_locked(bh) || buffer_dirty(bh));
73 * __jbd2_log_wait_for_space: wait until there is space in the journal.
75 * Called under j-state_lock *only*. It will be unlocked if we have to wait
76 * for a checkpoint to free up some space in the log.
78 void __jbd2_log_wait_for_space(journal_t *journal)
79 __acquires(&journal->j_state_lock)
80 __releases(&journal->j_state_lock)
82 int nblocks, space_left;
83 /* assert_spin_locked(&journal->j_state_lock); */
85 nblocks = journal->j_max_transaction_buffers;
86 while (jbd2_log_space_left(journal) < nblocks) {
87 write_unlock(&journal->j_state_lock);
88 mutex_lock_io(&journal->j_checkpoint_mutex);
91 * Test again, another process may have checkpointed while we
92 * were waiting for the checkpoint lock. If there are no
93 * transactions ready to be checkpointed, try to recover
94 * journal space by calling cleanup_journal_tail(), and if
95 * that doesn't work, by waiting for the currently committing
96 * transaction to complete. If there is absolutely no way
97 * to make progress, this is either a BUG or corrupted
98 * filesystem, so abort the journal and leave a stack
99 * trace for forensic evidence.
101 write_lock(&journal->j_state_lock);
102 if (journal->j_flags & JBD2_ABORT) {
103 mutex_unlock(&journal->j_checkpoint_mutex);
106 spin_lock(&journal->j_list_lock);
107 space_left = jbd2_log_space_left(journal);
108 if (space_left < nblocks) {
109 int chkpt = journal->j_checkpoint_transactions != NULL;
112 if (journal->j_committing_transaction)
113 tid = journal->j_committing_transaction->t_tid;
114 spin_unlock(&journal->j_list_lock);
115 write_unlock(&journal->j_state_lock);
117 jbd2_log_do_checkpoint(journal);
118 } else if (jbd2_cleanup_journal_tail(journal) == 0) {
119 /* We were able to recover space; yay! */
123 * jbd2_journal_commit_transaction() may want
124 * to take the checkpoint_mutex if JBD2_FLUSHED
125 * is set. So we need to temporarily drop it.
127 mutex_unlock(&journal->j_checkpoint_mutex);
128 jbd2_log_wait_commit(journal, tid);
129 write_lock(&journal->j_state_lock);
132 printk(KERN_ERR "%s: needed %d blocks and "
133 "only had %d space available\n",
134 __func__, nblocks, space_left);
135 printk(KERN_ERR "%s: no way to get more "
136 "journal space in %s\n", __func__,
139 jbd2_journal_abort(journal, -EIO);
141 write_lock(&journal->j_state_lock);
143 spin_unlock(&journal->j_list_lock);
145 mutex_unlock(&journal->j_checkpoint_mutex);
150 __flush_batch(journal_t *journal, int *batch_count)
153 struct blk_plug plug;
155 blk_start_plug(&plug);
156 for (i = 0; i < *batch_count; i++)
157 write_dirty_buffer(journal->j_chkpt_bhs[i], REQ_SYNC);
158 blk_finish_plug(&plug);
160 for (i = 0; i < *batch_count; i++) {
161 struct buffer_head *bh = journal->j_chkpt_bhs[i];
162 BUFFER_TRACE(bh, "brelse");
164 journal->j_chkpt_bhs[i] = NULL;
170 * Perform an actual checkpoint. We take the first transaction on the
171 * list of transactions to be checkpointed and send all its buffers
172 * to disk. We submit larger chunks of data at once.
174 * The journal should be locked before calling this function.
175 * Called with j_checkpoint_mutex held.
177 int jbd2_log_do_checkpoint(journal_t *journal)
179 struct journal_head *jh;
180 struct buffer_head *bh;
181 transaction_t *transaction;
183 int result, batch_count = 0;
185 jbd2_debug(1, "Start checkpoint\n");
188 * First thing: if there are any transactions in the log which
189 * don't need checkpointing, just eliminate them from the
190 * journal straight away.
192 result = jbd2_cleanup_journal_tail(journal);
193 trace_jbd2_checkpoint(journal, result);
194 jbd2_debug(1, "cleanup_journal_tail returned %d\n", result);
199 * OK, we need to start writing disk blocks. Take one transaction
202 spin_lock(&journal->j_list_lock);
203 if (!journal->j_checkpoint_transactions)
205 transaction = journal->j_checkpoint_transactions;
206 if (transaction->t_chp_stats.cs_chp_time == 0)
207 transaction->t_chp_stats.cs_chp_time = jiffies;
208 this_tid = transaction->t_tid;
211 * If someone cleaned up this transaction while we slept, we're
212 * done (maybe it's a new transaction, but it fell at the same
215 if (journal->j_checkpoint_transactions != transaction ||
216 transaction->t_tid != this_tid)
219 /* checkpoint all of the transaction's buffers */
220 while (transaction->t_checkpoint_list) {
221 jh = transaction->t_checkpoint_list;
225 * The buffer may be writing back, or flushing out in the
226 * last couple of cycles, or re-adding into a new transaction,
227 * need to check it again until it's unlocked.
229 if (buffer_locked(bh)) {
231 spin_unlock(&journal->j_list_lock);
233 /* the journal_head may have gone by now */
234 BUFFER_TRACE(bh, "brelse");
238 if (jh->b_transaction != NULL) {
239 transaction_t *t = jh->b_transaction;
240 tid_t tid = t->t_tid;
242 transaction->t_chp_stats.cs_forced_to_close++;
243 spin_unlock(&journal->j_list_lock);
244 if (unlikely(journal->j_flags & JBD2_UNMOUNT))
246 * The journal thread is dead; so
247 * starting and waiting for a commit
248 * to finish will cause us to wait for
249 * a _very_ long time.
252 "JBD2: %s: Waiting for Godot: block %llu\n",
253 journal->j_devname, (unsigned long long) bh->b_blocknr);
256 __flush_batch(journal, &batch_count);
257 jbd2_log_start_commit(journal, tid);
259 * jbd2_journal_commit_transaction() may want
260 * to take the checkpoint_mutex if JBD2_FLUSHED
261 * is set, jbd2_update_log_tail() called by
262 * jbd2_journal_commit_transaction() may also take
263 * checkpoint_mutex. So we need to temporarily
266 mutex_unlock(&journal->j_checkpoint_mutex);
267 jbd2_log_wait_commit(journal, tid);
268 mutex_lock_io(&journal->j_checkpoint_mutex);
269 spin_lock(&journal->j_list_lock);
272 if (!buffer_dirty(bh)) {
273 BUFFER_TRACE(bh, "remove from checkpoint");
275 * If the transaction was released or the checkpoint
276 * list was empty, we're done.
278 if (__jbd2_journal_remove_checkpoint(jh) ||
279 !transaction->t_checkpoint_list)
283 * We are about to write the buffer, it could be
284 * raced by some other transaction shrink or buffer
285 * re-log logic once we release the j_list_lock,
286 * leave it on the checkpoint list and check status
287 * again to make sure it's clean.
289 BUFFER_TRACE(bh, "queue");
291 J_ASSERT_BH(bh, !buffer_jwrite(bh));
292 journal->j_chkpt_bhs[batch_count++] = bh;
293 transaction->t_chp_stats.cs_written++;
294 transaction->t_checkpoint_list = jh->b_cpnext;
297 if ((batch_count == JBD2_NR_BATCH) ||
298 need_resched() || spin_needbreak(&journal->j_list_lock) ||
299 jh2bh(transaction->t_checkpoint_list) == journal->j_chkpt_bhs[0])
300 goto unlock_and_flush;
305 spin_unlock(&journal->j_list_lock);
308 __flush_batch(journal, &batch_count);
309 spin_lock(&journal->j_list_lock);
314 spin_unlock(&journal->j_list_lock);
315 result = jbd2_cleanup_journal_tail(journal);
317 return (result < 0) ? result : 0;
321 * Check the list of checkpoint transactions for the journal to see if
322 * we have already got rid of any since the last update of the log tail
323 * in the journal superblock. If so, we can instantly roll the
324 * superblock forward to remove those transactions from the log.
326 * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
328 * Called with the journal lock held.
330 * This is the only part of the journaling code which really needs to be
331 * aware of transaction aborts. Checkpointing involves writing to the
332 * main filesystem area rather than to the journal, so it can proceed
333 * even in abort state, but we must not update the super block if
334 * checkpointing may have failed. Otherwise, we would lose some metadata
335 * buffers which should be written-back to the filesystem.
338 int jbd2_cleanup_journal_tail(journal_t *journal)
341 unsigned long blocknr;
343 if (is_journal_aborted(journal))
346 if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
348 J_ASSERT(blocknr != 0);
351 * We need to make sure that any blocks that were recently written out
352 * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
353 * we drop the transactions from the journal. It's unlikely this will
354 * be necessary, especially with an appropriately sized journal, but we
355 * need this to guarantee correctness. Fortunately
356 * jbd2_cleanup_journal_tail() doesn't get called all that often.
358 if (journal->j_flags & JBD2_BARRIER)
359 blkdev_issue_flush(journal->j_fs_dev);
361 return __jbd2_update_log_tail(journal, first_tid, blocknr);
365 /* Checkpoint list management */
368 * journal_clean_one_cp_list
370 * Find all the written-back checkpoint buffers in the given list and
371 * release them. If 'destroy' is set, clean all buffers unconditionally.
373 * Called with j_list_lock held.
374 * Returns 1 if we freed the transaction, 0 otherwise.
376 static int journal_clean_one_cp_list(struct journal_head *jh, bool destroy)
378 struct journal_head *last_jh;
379 struct journal_head *next_jh = jh;
384 last_jh = jh->b_cpprev;
387 next_jh = jh->b_cpnext;
389 if (!destroy && __cp_buffer_busy(jh))
392 if (__jbd2_journal_remove_checkpoint(jh))
395 * This function only frees up some memory
396 * if possible so we dont have an obligation
397 * to finish processing. Bail out if preemption
402 } while (jh != last_jh);
408 * journal_shrink_one_cp_list
410 * Find 'nr_to_scan' written-back checkpoint buffers in the given list
411 * and try to release them. If the whole transaction is released, set
412 * the 'released' parameter. Return the number of released checkpointed
415 * Called with j_list_lock held.
417 static unsigned long journal_shrink_one_cp_list(struct journal_head *jh,
418 unsigned long *nr_to_scan,
421 struct journal_head *last_jh;
422 struct journal_head *next_jh = jh;
423 unsigned long nr_freed = 0;
426 if (!jh || *nr_to_scan == 0)
429 last_jh = jh->b_cpprev;
432 next_jh = jh->b_cpnext;
435 if (__cp_buffer_busy(jh))
439 ret = __jbd2_journal_remove_checkpoint(jh);
447 } while (jh != last_jh && *nr_to_scan);
453 * jbd2_journal_shrink_checkpoint_list
455 * Find 'nr_to_scan' written-back checkpoint buffers in the journal
456 * and try to release them. Return the number of released checkpointed
459 * Called with j_list_lock held.
461 unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
462 unsigned long *nr_to_scan)
464 transaction_t *transaction, *last_transaction, *next_transaction;
466 tid_t first_tid = 0, last_tid = 0, next_tid = 0;
468 unsigned long nr_freed = 0;
469 unsigned long nr_scanned = *nr_to_scan;
472 spin_lock(&journal->j_list_lock);
473 if (!journal->j_checkpoint_transactions) {
474 spin_unlock(&journal->j_list_lock);
479 * Get next shrink transaction, resume previous scan or start
480 * over again. If some others do checkpoint and drop transaction
481 * from the checkpoint list, we ignore saved j_shrink_transaction
482 * and start over unconditionally.
484 if (journal->j_shrink_transaction)
485 transaction = journal->j_shrink_transaction;
487 transaction = journal->j_checkpoint_transactions;
490 first_tid = transaction->t_tid;
491 last_transaction = journal->j_checkpoint_transactions->t_cpprev;
492 next_transaction = transaction;
493 last_tid = last_transaction->t_tid;
495 transaction = next_transaction;
496 next_transaction = transaction->t_cpnext;
497 tid = transaction->t_tid;
500 nr_freed += journal_shrink_one_cp_list(transaction->t_checkpoint_list,
501 nr_to_scan, &released);
502 if (*nr_to_scan == 0)
504 if (need_resched() || spin_needbreak(&journal->j_list_lock))
509 nr_freed += journal_shrink_one_cp_list(transaction->t_checkpoint_io_list,
510 nr_to_scan, &released);
511 if (*nr_to_scan == 0)
513 if (need_resched() || spin_needbreak(&journal->j_list_lock))
515 } while (transaction != last_transaction);
517 if (transaction != last_transaction) {
518 journal->j_shrink_transaction = next_transaction;
519 next_tid = next_transaction->t_tid;
521 journal->j_shrink_transaction = NULL;
525 spin_unlock(&journal->j_list_lock);
528 if (*nr_to_scan && next_tid)
531 nr_scanned -= *nr_to_scan;
532 trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
533 nr_freed, nr_scanned, next_tid);
539 * journal_clean_checkpoint_list
541 * Find all the written-back checkpoint buffers in the journal and release them.
542 * If 'destroy' is set, release all buffers unconditionally.
544 * Called with j_list_lock held.
546 void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
548 transaction_t *transaction, *last_transaction, *next_transaction;
551 transaction = journal->j_checkpoint_transactions;
555 last_transaction = transaction->t_cpprev;
556 next_transaction = transaction;
558 transaction = next_transaction;
559 next_transaction = transaction->t_cpnext;
560 ret = journal_clean_one_cp_list(transaction->t_checkpoint_list,
563 * This function only frees up some memory if possible so we
564 * dont have an obligation to finish processing. Bail out if
565 * preemption requested:
572 * It is essential that we are as careful as in the case of
573 * t_checkpoint_list with removing the buffer from the list as
574 * we can possibly see not yet submitted buffers on io_list
576 ret = journal_clean_one_cp_list(transaction->
577 t_checkpoint_io_list, destroy);
581 * Stop scanning if we couldn't free the transaction. This
582 * avoids pointless scanning of transactions which still
583 * weren't checkpointed.
587 } while (transaction != last_transaction);
591 * Remove buffers from all checkpoint lists as journal is aborted and we just
592 * need to free memory
594 void jbd2_journal_destroy_checkpoint(journal_t *journal)
597 * We loop because __jbd2_journal_clean_checkpoint_list() may abort
598 * early due to a need of rescheduling.
601 spin_lock(&journal->j_list_lock);
602 if (!journal->j_checkpoint_transactions) {
603 spin_unlock(&journal->j_list_lock);
606 __jbd2_journal_clean_checkpoint_list(journal, true);
607 spin_unlock(&journal->j_list_lock);
613 * journal_remove_checkpoint: called after a buffer has been committed
614 * to disk (either by being write-back flushed to disk, or being
615 * committed to the log).
617 * We cannot safely clean a transaction out of the log until all of the
618 * buffer updates committed in that transaction have safely been stored
619 * elsewhere on disk. To achieve this, all of the buffers in a
620 * transaction need to be maintained on the transaction's checkpoint
621 * lists until they have been rewritten, at which point this function is
622 * called to remove the buffer from the existing transaction's
625 * The function returns 1 if it frees the transaction, 0 otherwise.
626 * The function can free jh and bh.
628 * This function is called with j_list_lock held.
630 int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
632 struct transaction_chp_stats_s *stats;
633 transaction_t *transaction;
635 struct buffer_head *bh = jh2bh(jh);
637 JBUFFER_TRACE(jh, "entry");
639 transaction = jh->b_cp_transaction;
641 JBUFFER_TRACE(jh, "not on transaction");
644 journal = transaction->t_journal;
646 JBUFFER_TRACE(jh, "removing from transaction");
649 * If we have failed to write the buffer out to disk, the filesystem
650 * may become inconsistent. We cannot abort the journal here since
651 * we hold j_list_lock and we have to be careful about races with
652 * jbd2_journal_destroy(). So mark the writeback IO error in the
653 * journal here and we abort the journal later from a better context.
655 if (buffer_write_io_error(bh))
656 set_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags);
659 jh->b_cp_transaction = NULL;
660 percpu_counter_dec(&journal->j_checkpoint_jh_count);
661 jbd2_journal_put_journal_head(jh);
663 /* Is this transaction empty? */
664 if (transaction->t_checkpoint_list || transaction->t_checkpoint_io_list)
668 * There is one special case to worry about: if we have just pulled the
669 * buffer off a running or committing transaction's checkpoing list,
670 * then even if the checkpoint list is empty, the transaction obviously
673 * The locking here around t_state is a bit sleazy.
674 * See the comment at the end of jbd2_journal_commit_transaction().
676 if (transaction->t_state != T_FINISHED)
680 * OK, that was the last buffer for the transaction, we can now
681 * safely remove this transaction from the log.
683 stats = &transaction->t_chp_stats;
684 if (stats->cs_chp_time)
685 stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
687 trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
688 transaction->t_tid, stats);
690 __jbd2_journal_drop_transaction(journal, transaction);
691 jbd2_journal_free_transaction(transaction);
696 * journal_insert_checkpoint: put a committed buffer onto a checkpoint
697 * list so that we know when it is safe to clean the transaction out of
700 * Called with the journal locked.
701 * Called with j_list_lock held.
703 void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
704 transaction_t *transaction)
706 JBUFFER_TRACE(jh, "entry");
707 J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
708 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
710 /* Get reference for checkpointing transaction */
711 jbd2_journal_grab_journal_head(jh2bh(jh));
712 jh->b_cp_transaction = transaction;
714 if (!transaction->t_checkpoint_list) {
715 jh->b_cpnext = jh->b_cpprev = jh;
717 jh->b_cpnext = transaction->t_checkpoint_list;
718 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
719 jh->b_cpprev->b_cpnext = jh;
720 jh->b_cpnext->b_cpprev = jh;
722 transaction->t_checkpoint_list = jh;
723 percpu_counter_inc(&transaction->t_journal->j_checkpoint_jh_count);
727 * We've finished with this transaction structure: adios...
729 * The transaction must have no links except for the checkpoint by this
732 * Called with the journal locked.
733 * Called with j_list_lock held.
736 void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
738 assert_spin_locked(&journal->j_list_lock);
740 journal->j_shrink_transaction = NULL;
741 if (transaction->t_cpnext) {
742 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
743 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
744 if (journal->j_checkpoint_transactions == transaction)
745 journal->j_checkpoint_transactions =
746 transaction->t_cpnext;
747 if (journal->j_checkpoint_transactions == transaction)
748 journal->j_checkpoint_transactions = NULL;
751 J_ASSERT(transaction->t_state == T_FINISHED);
752 J_ASSERT(transaction->t_buffers == NULL);
753 J_ASSERT(transaction->t_forget == NULL);
754 J_ASSERT(transaction->t_shadow_list == NULL);
755 J_ASSERT(transaction->t_checkpoint_list == NULL);
756 J_ASSERT(transaction->t_checkpoint_io_list == NULL);
757 J_ASSERT(atomic_read(&transaction->t_updates) == 0);
758 J_ASSERT(journal->j_committing_transaction != transaction);
759 J_ASSERT(journal->j_running_transaction != transaction);
761 trace_jbd2_drop_transaction(journal, transaction);
763 jbd2_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);