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 * Try to release a checkpointed buffer from its transaction.
62 * Returns 1 if we released it and 2 if we also released the
65 * Requires j_list_lock
67 static int __try_to_free_cp_buf(struct journal_head *jh)
70 struct buffer_head *bh = jh2bh(jh);
72 if (!jh->b_transaction && !buffer_locked(bh) && !buffer_dirty(bh)) {
73 JBUFFER_TRACE(jh, "remove from checkpoint list");
74 ret = __jbd2_journal_remove_checkpoint(jh) + 1;
80 * __jbd2_log_wait_for_space: wait until there is space in the journal.
82 * Called under j-state_lock *only*. It will be unlocked if we have to wait
83 * for a checkpoint to free up some space in the log.
85 void __jbd2_log_wait_for_space(journal_t *journal)
86 __acquires(&journal->j_state_lock)
87 __releases(&journal->j_state_lock)
89 int nblocks, space_left;
90 /* assert_spin_locked(&journal->j_state_lock); */
92 nblocks = journal->j_max_transaction_buffers;
93 while (jbd2_log_space_left(journal) < nblocks) {
94 write_unlock(&journal->j_state_lock);
95 mutex_lock_io(&journal->j_checkpoint_mutex);
98 * Test again, another process may have checkpointed while we
99 * were waiting for the checkpoint lock. If there are no
100 * transactions ready to be checkpointed, try to recover
101 * journal space by calling cleanup_journal_tail(), and if
102 * that doesn't work, by waiting for the currently committing
103 * transaction to complete. If there is absolutely no way
104 * to make progress, this is either a BUG or corrupted
105 * filesystem, so abort the journal and leave a stack
106 * trace for forensic evidence.
108 write_lock(&journal->j_state_lock);
109 if (journal->j_flags & JBD2_ABORT) {
110 mutex_unlock(&journal->j_checkpoint_mutex);
113 spin_lock(&journal->j_list_lock);
114 space_left = jbd2_log_space_left(journal);
115 if (space_left < nblocks) {
116 int chkpt = journal->j_checkpoint_transactions != NULL;
119 if (journal->j_committing_transaction)
120 tid = journal->j_committing_transaction->t_tid;
121 spin_unlock(&journal->j_list_lock);
122 write_unlock(&journal->j_state_lock);
124 jbd2_log_do_checkpoint(journal);
125 } else if (jbd2_cleanup_journal_tail(journal) == 0) {
126 /* We were able to recover space; yay! */
130 * jbd2_journal_commit_transaction() may want
131 * to take the checkpoint_mutex if JBD2_FLUSHED
132 * is set. So we need to temporarily drop it.
134 mutex_unlock(&journal->j_checkpoint_mutex);
135 jbd2_log_wait_commit(journal, tid);
136 write_lock(&journal->j_state_lock);
139 printk(KERN_ERR "%s: needed %d blocks and "
140 "only had %d space available\n",
141 __func__, nblocks, space_left);
142 printk(KERN_ERR "%s: no way to get more "
143 "journal space in %s\n", __func__,
146 jbd2_journal_abort(journal, -EIO);
148 write_lock(&journal->j_state_lock);
150 spin_unlock(&journal->j_list_lock);
152 mutex_unlock(&journal->j_checkpoint_mutex);
157 __flush_batch(journal_t *journal, int *batch_count)
160 struct blk_plug plug;
162 blk_start_plug(&plug);
163 for (i = 0; i < *batch_count; i++)
164 write_dirty_buffer(journal->j_chkpt_bhs[i], REQ_SYNC);
165 blk_finish_plug(&plug);
167 for (i = 0; i < *batch_count; i++) {
168 struct buffer_head *bh = journal->j_chkpt_bhs[i];
169 BUFFER_TRACE(bh, "brelse");
171 journal->j_chkpt_bhs[i] = NULL;
177 * Perform an actual checkpoint. We take the first transaction on the
178 * list of transactions to be checkpointed and send all its buffers
179 * to disk. We submit larger chunks of data at once.
181 * The journal should be locked before calling this function.
182 * Called with j_checkpoint_mutex held.
184 int jbd2_log_do_checkpoint(journal_t *journal)
186 struct journal_head *jh;
187 struct buffer_head *bh;
188 transaction_t *transaction;
190 int result, batch_count = 0;
192 jbd_debug(1, "Start checkpoint\n");
195 * First thing: if there are any transactions in the log which
196 * don't need checkpointing, just eliminate them from the
197 * journal straight away.
199 result = jbd2_cleanup_journal_tail(journal);
200 trace_jbd2_checkpoint(journal, result);
201 jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
206 * OK, we need to start writing disk blocks. Take one transaction
209 spin_lock(&journal->j_list_lock);
210 if (!journal->j_checkpoint_transactions)
212 transaction = journal->j_checkpoint_transactions;
213 if (transaction->t_chp_stats.cs_chp_time == 0)
214 transaction->t_chp_stats.cs_chp_time = jiffies;
215 this_tid = transaction->t_tid;
218 * If someone cleaned up this transaction while we slept, we're
219 * done (maybe it's a new transaction, but it fell at the same
222 if (journal->j_checkpoint_transactions != transaction ||
223 transaction->t_tid != this_tid)
226 /* checkpoint all of the transaction's buffers */
227 while (transaction->t_checkpoint_list) {
228 jh = transaction->t_checkpoint_list;
231 if (jh->b_transaction != NULL) {
232 transaction_t *t = jh->b_transaction;
233 tid_t tid = t->t_tid;
235 transaction->t_chp_stats.cs_forced_to_close++;
236 spin_unlock(&journal->j_list_lock);
237 if (unlikely(journal->j_flags & JBD2_UNMOUNT))
239 * The journal thread is dead; so
240 * starting and waiting for a commit
241 * to finish will cause us to wait for
242 * a _very_ long time.
245 "JBD2: %s: Waiting for Godot: block %llu\n",
246 journal->j_devname, (unsigned long long) bh->b_blocknr);
249 __flush_batch(journal, &batch_count);
250 jbd2_log_start_commit(journal, tid);
252 * jbd2_journal_commit_transaction() may want
253 * to take the checkpoint_mutex if JBD2_FLUSHED
254 * is set, jbd2_update_log_tail() called by
255 * jbd2_journal_commit_transaction() may also take
256 * checkpoint_mutex. So we need to temporarily
259 mutex_unlock(&journal->j_checkpoint_mutex);
260 jbd2_log_wait_commit(journal, tid);
261 mutex_lock_io(&journal->j_checkpoint_mutex);
262 spin_lock(&journal->j_list_lock);
265 if (!trylock_buffer(bh)) {
267 * The buffer is locked, it may be writing back, or
268 * flushing out in the last couple of cycles, or
269 * re-adding into a new transaction, need to check
270 * it again until it's unlocked.
273 spin_unlock(&journal->j_list_lock);
275 /* the journal_head may have gone by now */
276 BUFFER_TRACE(bh, "brelse");
279 } else if (!buffer_dirty(bh)) {
281 BUFFER_TRACE(bh, "remove from checkpoint");
283 * If the transaction was released or the checkpoint
284 * list was empty, we're done.
286 if (__jbd2_journal_remove_checkpoint(jh) ||
287 !transaction->t_checkpoint_list)
292 * We are about to write the buffer, it could be
293 * raced by some other transaction shrink or buffer
294 * re-log logic once we release the j_list_lock,
295 * leave it on the checkpoint list and check status
296 * again to make sure it's clean.
298 BUFFER_TRACE(bh, "queue");
300 J_ASSERT_BH(bh, !buffer_jwrite(bh));
301 journal->j_chkpt_bhs[batch_count++] = bh;
302 transaction->t_chp_stats.cs_written++;
303 transaction->t_checkpoint_list = jh->b_cpnext;
306 if ((batch_count == JBD2_NR_BATCH) ||
307 need_resched() || spin_needbreak(&journal->j_list_lock) ||
308 jh2bh(transaction->t_checkpoint_list) == journal->j_chkpt_bhs[0])
309 goto unlock_and_flush;
314 spin_unlock(&journal->j_list_lock);
317 __flush_batch(journal, &batch_count);
318 spin_lock(&journal->j_list_lock);
323 spin_unlock(&journal->j_list_lock);
324 result = jbd2_cleanup_journal_tail(journal);
326 return (result < 0) ? result : 0;
330 * Check the list of checkpoint transactions for the journal to see if
331 * we have already got rid of any since the last update of the log tail
332 * in the journal superblock. If so, we can instantly roll the
333 * superblock forward to remove those transactions from the log.
335 * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
337 * Called with the journal lock held.
339 * This is the only part of the journaling code which really needs to be
340 * aware of transaction aborts. Checkpointing involves writing to the
341 * main filesystem area rather than to the journal, so it can proceed
342 * even in abort state, but we must not update the super block if
343 * checkpointing may have failed. Otherwise, we would lose some metadata
344 * buffers which should be written-back to the filesystem.
347 int jbd2_cleanup_journal_tail(journal_t *journal)
350 unsigned long blocknr;
352 if (is_journal_aborted(journal))
355 if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
357 J_ASSERT(blocknr != 0);
360 * We need to make sure that any blocks that were recently written out
361 * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
362 * we drop the transactions from the journal. It's unlikely this will
363 * be necessary, especially with an appropriately sized journal, but we
364 * need this to guarantee correctness. Fortunately
365 * jbd2_cleanup_journal_tail() doesn't get called all that often.
367 if (journal->j_flags & JBD2_BARRIER)
368 blkdev_issue_flush(journal->j_fs_dev, GFP_NOFS);
370 return __jbd2_update_log_tail(journal, first_tid, blocknr);
374 /* Checkpoint list management */
377 * journal_clean_one_cp_list
379 * Find all the written-back checkpoint buffers in the given list and
380 * release them. If 'destroy' is set, clean all buffers unconditionally.
382 * Called with j_list_lock held.
383 * Returns 1 if we freed the transaction, 0 otherwise.
385 static int journal_clean_one_cp_list(struct journal_head *jh, bool destroy)
387 struct journal_head *last_jh;
388 struct journal_head *next_jh = jh;
394 last_jh = jh->b_cpprev;
397 next_jh = jh->b_cpnext;
399 ret = __try_to_free_cp_buf(jh);
401 ret = __jbd2_journal_remove_checkpoint(jh) + 1;
407 * This function only frees up some memory
408 * if possible so we dont have an obligation
409 * to finish processing. Bail out if preemption
414 } while (jh != last_jh);
420 * journal_clean_checkpoint_list
422 * Find all the written-back checkpoint buffers in the journal and release them.
423 * If 'destroy' is set, release all buffers unconditionally.
425 * Called with j_list_lock held.
427 void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
429 transaction_t *transaction, *last_transaction, *next_transaction;
432 transaction = journal->j_checkpoint_transactions;
436 last_transaction = transaction->t_cpprev;
437 next_transaction = transaction;
439 transaction = next_transaction;
440 next_transaction = transaction->t_cpnext;
441 ret = journal_clean_one_cp_list(transaction->t_checkpoint_list,
444 * This function only frees up some memory if possible so we
445 * dont have an obligation to finish processing. Bail out if
446 * preemption requested:
453 * It is essential that we are as careful as in the case of
454 * t_checkpoint_list with removing the buffer from the list as
455 * we can possibly see not yet submitted buffers on io_list
457 ret = journal_clean_one_cp_list(transaction->
458 t_checkpoint_io_list, destroy);
462 * Stop scanning if we couldn't free the transaction. This
463 * avoids pointless scanning of transactions which still
464 * weren't checkpointed.
468 } while (transaction != last_transaction);
472 * Remove buffers from all checkpoint lists as journal is aborted and we just
473 * need to free memory
475 void jbd2_journal_destroy_checkpoint(journal_t *journal)
478 * We loop because __jbd2_journal_clean_checkpoint_list() may abort
479 * early due to a need of rescheduling.
482 spin_lock(&journal->j_list_lock);
483 if (!journal->j_checkpoint_transactions) {
484 spin_unlock(&journal->j_list_lock);
487 __jbd2_journal_clean_checkpoint_list(journal, true);
488 spin_unlock(&journal->j_list_lock);
494 * journal_remove_checkpoint: called after a buffer has been committed
495 * to disk (either by being write-back flushed to disk, or being
496 * committed to the log).
498 * We cannot safely clean a transaction out of the log until all of the
499 * buffer updates committed in that transaction have safely been stored
500 * elsewhere on disk. To achieve this, all of the buffers in a
501 * transaction need to be maintained on the transaction's checkpoint
502 * lists until they have been rewritten, at which point this function is
503 * called to remove the buffer from the existing transaction's
506 * The function returns 1 if it frees the transaction, 0 otherwise.
507 * The function can free jh and bh.
509 * This function is called with j_list_lock held.
511 int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
513 struct transaction_chp_stats_s *stats;
514 transaction_t *transaction;
518 JBUFFER_TRACE(jh, "entry");
520 if ((transaction = jh->b_cp_transaction) == NULL) {
521 JBUFFER_TRACE(jh, "not on transaction");
524 journal = transaction->t_journal;
526 JBUFFER_TRACE(jh, "removing from transaction");
528 jh->b_cp_transaction = NULL;
529 jbd2_journal_put_journal_head(jh);
531 if (transaction->t_checkpoint_list != NULL ||
532 transaction->t_checkpoint_io_list != NULL)
536 * There is one special case to worry about: if we have just pulled the
537 * buffer off a running or committing transaction's checkpoing list,
538 * then even if the checkpoint list is empty, the transaction obviously
541 * The locking here around t_state is a bit sleazy.
542 * See the comment at the end of jbd2_journal_commit_transaction().
544 if (transaction->t_state != T_FINISHED)
547 /* OK, that was the last buffer for the transaction: we can now
548 safely remove this transaction from the log */
549 stats = &transaction->t_chp_stats;
550 if (stats->cs_chp_time)
551 stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
553 trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
554 transaction->t_tid, stats);
556 __jbd2_journal_drop_transaction(journal, transaction);
557 jbd2_journal_free_transaction(transaction);
564 * journal_insert_checkpoint: put a committed buffer onto a checkpoint
565 * list so that we know when it is safe to clean the transaction out of
568 * Called with the journal locked.
569 * Called with j_list_lock held.
571 void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
572 transaction_t *transaction)
574 JBUFFER_TRACE(jh, "entry");
575 J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
576 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
578 /* Get reference for checkpointing transaction */
579 jbd2_journal_grab_journal_head(jh2bh(jh));
580 jh->b_cp_transaction = transaction;
582 if (!transaction->t_checkpoint_list) {
583 jh->b_cpnext = jh->b_cpprev = jh;
585 jh->b_cpnext = transaction->t_checkpoint_list;
586 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
587 jh->b_cpprev->b_cpnext = jh;
588 jh->b_cpnext->b_cpprev = jh;
590 transaction->t_checkpoint_list = jh;
594 * We've finished with this transaction structure: adios...
596 * The transaction must have no links except for the checkpoint by this
599 * Called with the journal locked.
600 * Called with j_list_lock held.
603 void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
605 assert_spin_locked(&journal->j_list_lock);
606 if (transaction->t_cpnext) {
607 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
608 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
609 if (journal->j_checkpoint_transactions == transaction)
610 journal->j_checkpoint_transactions =
611 transaction->t_cpnext;
612 if (journal->j_checkpoint_transactions == transaction)
613 journal->j_checkpoint_transactions = NULL;
616 J_ASSERT(transaction->t_state == T_FINISHED);
617 J_ASSERT(transaction->t_buffers == NULL);
618 J_ASSERT(transaction->t_forget == NULL);
619 J_ASSERT(transaction->t_shadow_list == NULL);
620 J_ASSERT(transaction->t_checkpoint_list == NULL);
621 J_ASSERT(transaction->t_checkpoint_io_list == NULL);
622 J_ASSERT(atomic_read(&transaction->t_updates) == 0);
623 J_ASSERT(journal->j_committing_transaction != transaction);
624 J_ASSERT(journal->j_running_transaction != transaction);
626 trace_jbd2_drop_transaction(journal, transaction);
628 jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);