2 * linux/fs/checkpoint.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
6 * Copyright 1999 Red Hat Software --- All Rights Reserved
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.
12 * Checkpoint routines for the generic filesystem journaling code.
13 * Part of the ext2fs journaling system.
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
20 #include <linux/time.h>
22 #include <linux/jbd.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
27 * Unlink a buffer from a transaction checkpoint list.
29 * Called with j_list_lock held.
32 static void __buffer_unlink_first(struct journal_head *jh)
34 transaction_t *transaction;
36 transaction = jh->b_cp_transaction;
38 jh->b_cpnext->b_cpprev = jh->b_cpprev;
39 jh->b_cpprev->b_cpnext = jh->b_cpnext;
40 if (transaction->t_checkpoint_list == jh) {
41 transaction->t_checkpoint_list = jh->b_cpnext;
42 if (transaction->t_checkpoint_list == jh)
43 transaction->t_checkpoint_list = NULL;
48 * Unlink a buffer from a transaction checkpoint(io) list.
50 * Called with j_list_lock held.
53 static inline void __buffer_unlink(struct journal_head *jh)
55 transaction_t *transaction;
57 transaction = jh->b_cp_transaction;
59 __buffer_unlink_first(jh);
60 if (transaction->t_checkpoint_io_list == jh) {
61 transaction->t_checkpoint_io_list = jh->b_cpnext;
62 if (transaction->t_checkpoint_io_list == jh)
63 transaction->t_checkpoint_io_list = NULL;
68 * Move a buffer from the checkpoint list to the checkpoint io list
70 * Called with j_list_lock held
73 static inline void __buffer_relink_io(struct journal_head *jh)
75 transaction_t *transaction;
77 transaction = jh->b_cp_transaction;
78 __buffer_unlink_first(jh);
80 if (!transaction->t_checkpoint_io_list) {
81 jh->b_cpnext = jh->b_cpprev = jh;
83 jh->b_cpnext = transaction->t_checkpoint_io_list;
84 jh->b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;
85 jh->b_cpprev->b_cpnext = jh;
86 jh->b_cpnext->b_cpprev = jh;
88 transaction->t_checkpoint_io_list = jh;
92 * Try to release a checkpointed buffer from its transaction.
93 * Returns 1 if we released it and 2 if we also released the
96 * Requires j_list_lock
97 * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
99 static int __try_to_free_cp_buf(struct journal_head *jh)
102 struct buffer_head *bh = jh2bh(jh);
104 if (jh->b_jlist == BJ_None && !buffer_locked(bh) && !buffer_dirty(bh)) {
105 JBUFFER_TRACE(jh, "remove from checkpoint list");
106 ret = __journal_remove_checkpoint(jh) + 1;
107 jbd_unlock_bh_state(bh);
108 journal_remove_journal_head(bh);
109 BUFFER_TRACE(bh, "release");
112 jbd_unlock_bh_state(bh);
118 * __log_wait_for_space: wait until there is space in the journal.
120 * Called under j-state_lock *only*. It will be unlocked if we have to wait
121 * for a checkpoint to free up some space in the log.
123 void __log_wait_for_space(journal_t *journal)
126 assert_spin_locked(&journal->j_state_lock);
128 nblocks = jbd_space_needed(journal);
129 while (__log_space_left(journal) < nblocks) {
130 if (journal->j_flags & JFS_ABORT)
132 spin_unlock(&journal->j_state_lock);
133 down(&journal->j_checkpoint_sem);
136 * Test again, another process may have checkpointed while we
137 * were waiting for the checkpoint lock
139 spin_lock(&journal->j_state_lock);
140 nblocks = jbd_space_needed(journal);
141 if (__log_space_left(journal) < nblocks) {
142 spin_unlock(&journal->j_state_lock);
143 log_do_checkpoint(journal);
144 spin_lock(&journal->j_state_lock);
146 up(&journal->j_checkpoint_sem);
151 * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
152 * The caller must restart a list walk. Wait for someone else to run
153 * jbd_unlock_bh_state().
155 static void jbd_sync_bh(journal_t *journal, struct buffer_head *bh)
158 spin_unlock(&journal->j_list_lock);
159 jbd_lock_bh_state(bh);
160 jbd_unlock_bh_state(bh);
165 * Clean up transaction's list of buffers submitted for io.
166 * We wait for any pending IO to complete and remove any clean
167 * buffers. Note that we take the buffers in the opposite ordering
168 * from the one in which they were submitted for IO.
170 * Called with j_list_lock held.
173 static void __wait_cp_io(journal_t *journal, transaction_t *transaction)
175 struct journal_head *jh;
176 struct buffer_head *bh;
180 this_tid = transaction->t_tid;
182 /* Didn't somebody clean up the transaction in the meanwhile */
183 if (journal->j_checkpoint_transactions != transaction ||
184 transaction->t_tid != this_tid)
186 while (!released && transaction->t_checkpoint_io_list) {
187 jh = transaction->t_checkpoint_io_list;
189 if (!jbd_trylock_bh_state(bh)) {
190 jbd_sync_bh(journal, bh);
191 spin_lock(&journal->j_list_lock);
194 if (buffer_locked(bh)) {
195 atomic_inc(&bh->b_count);
196 spin_unlock(&journal->j_list_lock);
197 jbd_unlock_bh_state(bh);
199 /* the journal_head may have gone by now */
200 BUFFER_TRACE(bh, "brelse");
202 spin_lock(&journal->j_list_lock);
206 * Now in whatever state the buffer currently is, we know that
207 * it has been written out and so we can drop it from the list
209 released = __journal_remove_checkpoint(jh);
210 jbd_unlock_bh_state(bh);
217 __flush_batch(journal_t *journal, struct buffer_head **bhs, int *batch_count)
221 ll_rw_block(SWRITE, *batch_count, bhs);
222 for (i = 0; i < *batch_count; i++) {
223 struct buffer_head *bh = bhs[i];
224 clear_buffer_jwrite(bh);
225 BUFFER_TRACE(bh, "brelse");
232 * Try to flush one buffer from the checkpoint list to disk.
234 * Return 1 if something happened which requires us to abort the current
235 * scan of the checkpoint list.
237 * Called with j_list_lock held and drops it if 1 is returned
238 * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
240 static int __process_buffer(journal_t *journal, struct journal_head *jh,
241 struct buffer_head **bhs, int *batch_count)
243 struct buffer_head *bh = jh2bh(jh);
246 if (buffer_locked(bh)) {
248 spin_unlock(&journal->j_list_lock);
249 jbd_unlock_bh_state(bh);
251 /* the journal_head may have gone by now */
252 BUFFER_TRACE(bh, "brelse");
256 else if (jh->b_transaction != NULL) {
257 transaction_t *t = jh->b_transaction;
258 tid_t tid = t->t_tid;
260 spin_unlock(&journal->j_list_lock);
261 jbd_unlock_bh_state(bh);
262 log_start_commit(journal, tid);
263 log_wait_commit(journal, tid);
266 else if (!buffer_dirty(bh)) {
267 J_ASSERT_JH(jh, !buffer_jbddirty(bh));
268 BUFFER_TRACE(bh, "remove from checkpoint");
269 __journal_remove_checkpoint(jh);
270 spin_unlock(&journal->j_list_lock);
271 jbd_unlock_bh_state(bh);
272 journal_remove_journal_head(bh);
278 * Important: we are about to write the buffer, and
279 * possibly block, while still holding the journal lock.
280 * We cannot afford to let the transaction logic start
281 * messing around with this buffer before we write it to
282 * disk, as that would break recoverability.
284 BUFFER_TRACE(bh, "queue");
286 J_ASSERT_BH(bh, !buffer_jwrite(bh));
287 set_buffer_jwrite(bh);
288 bhs[*batch_count] = bh;
289 __buffer_relink_io(jh);
290 jbd_unlock_bh_state(bh);
292 if (*batch_count == NR_BATCH) {
293 spin_unlock(&journal->j_list_lock);
294 __flush_batch(journal, bhs, batch_count);
302 * Perform an actual checkpoint. We take the first transaction on the
303 * list of transactions to be checkpointed and send all its buffers
304 * to disk. We submit larger chunks of data at once.
306 * The journal should be locked before calling this function.
308 int log_do_checkpoint(journal_t *journal)
310 transaction_t *transaction;
314 jbd_debug(1, "Start checkpoint\n");
317 * First thing: if there are any transactions in the log which
318 * don't need checkpointing, just eliminate them from the
319 * journal straight away.
321 result = cleanup_journal_tail(journal);
322 jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
327 * OK, we need to start writing disk blocks. Take one transaction
330 spin_lock(&journal->j_list_lock);
331 if (!journal->j_checkpoint_transactions)
333 transaction = journal->j_checkpoint_transactions;
334 this_tid = transaction->t_tid;
337 * If someone cleaned up this transaction while we slept, we're
338 * done (maybe it's a new transaction, but it fell at the same
341 if (journal->j_checkpoint_transactions == transaction ||
342 transaction->t_tid == this_tid) {
344 struct buffer_head *bhs[NR_BATCH];
345 struct journal_head *jh;
348 while (!retry && transaction->t_checkpoint_list) {
349 struct buffer_head *bh;
351 jh = transaction->t_checkpoint_list;
353 if (!jbd_trylock_bh_state(bh)) {
354 jbd_sync_bh(journal, bh);
358 retry = __process_buffer(journal, jh, bhs,
361 lock_need_resched(&journal->j_list_lock)) {
362 spin_unlock(&journal->j_list_lock);
370 spin_unlock(&journal->j_list_lock);
373 __flush_batch(journal, bhs, &batch_count);
377 spin_lock(&journal->j_list_lock);
381 * Now we have cleaned up the first transaction's checkpoint
382 * list. Let's clean up the second one.
384 __wait_cp_io(journal, transaction);
387 spin_unlock(&journal->j_list_lock);
388 result = cleanup_journal_tail(journal);
395 * Check the list of checkpoint transactions for the journal to see if
396 * we have already got rid of any since the last update of the log tail
397 * in the journal superblock. If so, we can instantly roll the
398 * superblock forward to remove those transactions from the log.
400 * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
402 * Called with the journal lock held.
404 * This is the only part of the journaling code which really needs to be
405 * aware of transaction aborts. Checkpointing involves writing to the
406 * main filesystem area rather than to the journal, so it can proceed
407 * even in abort state, but we must not update the journal superblock if
408 * we have an abort error outstanding.
411 int cleanup_journal_tail(journal_t *journal)
413 transaction_t * transaction;
415 unsigned long blocknr, freed;
417 /* OK, work out the oldest transaction remaining in the log, and
418 * the log block it starts at.
420 * If the log is now empty, we need to work out which is the
421 * next transaction ID we will write, and where it will
424 spin_lock(&journal->j_state_lock);
425 spin_lock(&journal->j_list_lock);
426 transaction = journal->j_checkpoint_transactions;
428 first_tid = transaction->t_tid;
429 blocknr = transaction->t_log_start;
430 } else if ((transaction = journal->j_committing_transaction) != NULL) {
431 first_tid = transaction->t_tid;
432 blocknr = transaction->t_log_start;
433 } else if ((transaction = journal->j_running_transaction) != NULL) {
434 first_tid = transaction->t_tid;
435 blocknr = journal->j_head;
437 first_tid = journal->j_transaction_sequence;
438 blocknr = journal->j_head;
440 spin_unlock(&journal->j_list_lock);
441 J_ASSERT(blocknr != 0);
443 /* If the oldest pinned transaction is at the tail of the log
444 already then there's not much we can do right now. */
445 if (journal->j_tail_sequence == first_tid) {
446 spin_unlock(&journal->j_state_lock);
450 /* OK, update the superblock to recover the freed space.
451 * Physical blocks come first: have we wrapped beyond the end of
453 freed = blocknr - journal->j_tail;
454 if (blocknr < journal->j_tail)
455 freed = freed + journal->j_last - journal->j_first;
458 "Cleaning journal tail from %d to %d (offset %lu), "
460 journal->j_tail_sequence, first_tid, blocknr, freed);
462 journal->j_free += freed;
463 journal->j_tail_sequence = first_tid;
464 journal->j_tail = blocknr;
465 spin_unlock(&journal->j_state_lock);
466 if (!(journal->j_flags & JFS_ABORT))
467 journal_update_superblock(journal, 1);
472 /* Checkpoint list management */
475 * journal_clean_one_cp_list
477 * Find all the written-back checkpoint buffers in the given list and release them.
479 * Called with the journal locked.
480 * Called with j_list_lock held.
481 * Returns number of bufers reaped (for debug)
484 static int journal_clean_one_cp_list(struct journal_head *jh, int *released)
486 struct journal_head *last_jh;
487 struct journal_head *next_jh = jh;
494 last_jh = jh->b_cpprev;
497 next_jh = jh->b_cpnext;
498 /* Use trylock because of the ranking */
499 if (jbd_trylock_bh_state(jh2bh(jh))) {
500 ret = __try_to_free_cp_buf(jh);
510 * This function only frees up some memory if possible so we
511 * dont have an obligation to finish processing. Bail out if
512 * preemption requested:
516 } while (jh != last_jh);
522 * journal_clean_checkpoint_list
524 * Find all the written-back checkpoint buffers in the journal and release them.
526 * Called with the journal locked.
527 * Called with j_list_lock held.
528 * Returns number of buffers reaped (for debug)
531 int __journal_clean_checkpoint_list(journal_t *journal)
533 transaction_t *transaction, *last_transaction, *next_transaction;
534 int ret = 0, released;
536 transaction = journal->j_checkpoint_transactions;
540 last_transaction = transaction->t_cpprev;
541 next_transaction = transaction;
543 transaction = next_transaction;
544 next_transaction = transaction->t_cpnext;
545 ret += journal_clean_one_cp_list(transaction->
546 t_checkpoint_list, &released);
552 * It is essential that we are as careful as in the case of
553 * t_checkpoint_list with removing the buffer from the list as
554 * we can possibly see not yet submitted buffers on io_list
556 ret += journal_clean_one_cp_list(transaction->
557 t_checkpoint_io_list, &released);
560 } while (transaction != last_transaction);
566 * journal_remove_checkpoint: called after a buffer has been committed
567 * to disk (either by being write-back flushed to disk, or being
568 * committed to the log).
570 * We cannot safely clean a transaction out of the log until all of the
571 * buffer updates committed in that transaction have safely been stored
572 * elsewhere on disk. To achieve this, all of the buffers in a
573 * transaction need to be maintained on the transaction's checkpoint
574 * lists until they have been rewritten, at which point this function is
575 * called to remove the buffer from the existing transaction's
578 * The function returns 1 if it frees the transaction, 0 otherwise.
580 * This function is called with the journal locked.
581 * This function is called with j_list_lock held.
582 * This function is called with jbd_lock_bh_state(jh2bh(jh))
585 int __journal_remove_checkpoint(struct journal_head *jh)
587 transaction_t *transaction;
591 JBUFFER_TRACE(jh, "entry");
593 if ((transaction = jh->b_cp_transaction) == NULL) {
594 JBUFFER_TRACE(jh, "not on transaction");
597 journal = transaction->t_journal;
600 jh->b_cp_transaction = NULL;
602 if (transaction->t_checkpoint_list != NULL ||
603 transaction->t_checkpoint_io_list != NULL)
605 JBUFFER_TRACE(jh, "transaction has no more buffers");
608 * There is one special case to worry about: if we have just pulled the
609 * buffer off a committing transaction's forget list, then even if the
610 * checkpoint list is empty, the transaction obviously cannot be
613 * The locking here around j_committing_transaction is a bit sleazy.
614 * See the comment at the end of journal_commit_transaction().
616 if (transaction == journal->j_committing_transaction) {
617 JBUFFER_TRACE(jh, "belongs to committing transaction");
621 /* OK, that was the last buffer for the transaction: we can now
622 safely remove this transaction from the log */
624 __journal_drop_transaction(journal, transaction);
626 /* Just in case anybody was waiting for more transactions to be
628 wake_up(&journal->j_wait_logspace);
631 JBUFFER_TRACE(jh, "exit");
636 * journal_insert_checkpoint: put a committed buffer onto a checkpoint
637 * list so that we know when it is safe to clean the transaction out of
640 * Called with the journal locked.
641 * Called with j_list_lock held.
643 void __journal_insert_checkpoint(struct journal_head *jh,
644 transaction_t *transaction)
646 JBUFFER_TRACE(jh, "entry");
647 J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
648 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
650 jh->b_cp_transaction = transaction;
652 if (!transaction->t_checkpoint_list) {
653 jh->b_cpnext = jh->b_cpprev = jh;
655 jh->b_cpnext = transaction->t_checkpoint_list;
656 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
657 jh->b_cpprev->b_cpnext = jh;
658 jh->b_cpnext->b_cpprev = jh;
660 transaction->t_checkpoint_list = jh;
664 * We've finished with this transaction structure: adios...
666 * The transaction must have no links except for the checkpoint by this
669 * Called with the journal locked.
670 * Called with j_list_lock held.
673 void __journal_drop_transaction(journal_t *journal, transaction_t *transaction)
675 assert_spin_locked(&journal->j_list_lock);
676 if (transaction->t_cpnext) {
677 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
678 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
679 if (journal->j_checkpoint_transactions == transaction)
680 journal->j_checkpoint_transactions =
681 transaction->t_cpnext;
682 if (journal->j_checkpoint_transactions == transaction)
683 journal->j_checkpoint_transactions = NULL;
686 J_ASSERT(transaction->t_state == T_FINISHED);
687 J_ASSERT(transaction->t_buffers == NULL);
688 J_ASSERT(transaction->t_sync_datalist == NULL);
689 J_ASSERT(transaction->t_forget == NULL);
690 J_ASSERT(transaction->t_iobuf_list == NULL);
691 J_ASSERT(transaction->t_shadow_list == NULL);
692 J_ASSERT(transaction->t_log_list == NULL);
693 J_ASSERT(transaction->t_checkpoint_list == NULL);
694 J_ASSERT(transaction->t_checkpoint_io_list == NULL);
695 J_ASSERT(transaction->t_updates == 0);
696 J_ASSERT(journal->j_committing_transaction != transaction);
697 J_ASSERT(journal->j_running_transaction != transaction);
699 jbd_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);