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/sched.h>
22 #include <linux/jbd.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/locks.h>
27 extern spinlock_t journal_datalist_lock;
30 * Unlink a buffer from a transaction.
32 * Called with journal_datalist_lock held.
35 static inline void __buffer_unlink(struct journal_head *jh)
37 transaction_t *transaction;
39 transaction = jh->b_cp_transaction;
40 jh->b_cp_transaction = NULL;
42 jh->b_cpnext->b_cpprev = jh->b_cpprev;
43 jh->b_cpprev->b_cpnext = jh->b_cpnext;
44 if (transaction->t_checkpoint_list == jh)
45 transaction->t_checkpoint_list = jh->b_cpnext;
46 if (transaction->t_checkpoint_list == jh)
47 transaction->t_checkpoint_list = NULL;
51 * Try to release a checkpointed buffer from its transaction.
52 * Returns 1 if we released it.
53 * Requires journal_datalist_lock
55 static int __try_to_free_cp_buf(struct journal_head *jh)
58 struct buffer_head *bh = jh2bh(jh);
60 if (jh->b_jlist == BJ_None && !buffer_locked(bh) && !buffer_dirty(bh)) {
61 JBUFFER_TRACE(jh, "remove from checkpoint list");
62 __journal_remove_checkpoint(jh);
63 __journal_remove_journal_head(bh);
64 BUFFER_TRACE(bh, "release");
65 /* BUF_LOCKED -> BUF_CLEAN (fwiw) */
74 * log_wait_for_space: wait until there is space in the journal.
76 * Called with the journal already locked, but it will be unlocked if we have
77 * to wait for a checkpoint to free up some space in the log.
80 void log_wait_for_space(journal_t *journal, int nblocks)
82 while (log_space_left(journal) < nblocks) {
83 if (journal->j_flags & JFS_ABORT)
85 unlock_journal(journal);
86 down(&journal->j_checkpoint_sem);
87 lock_journal(journal);
89 /* Test again, another process may have checkpointed
90 * while we were waiting for the checkpoint lock */
91 if (log_space_left(journal) < nblocks) {
92 log_do_checkpoint(journal, nblocks);
94 up(&journal->j_checkpoint_sem);
99 * Clean up a transaction's checkpoint list.
101 * We wait for any pending IO to complete and make sure any clean
102 * buffers are removed from the transaction.
104 * Return 1 if we performed any actions which might have destroyed the
105 * checkpoint. (journal_remove_checkpoint() deletes the transaction when
106 * the last checkpoint buffer is cleansed)
108 * Called with the journal locked.
109 * Called with journal_datalist_lock held.
111 static int __cleanup_transaction(journal_t *journal, transaction_t *transaction)
113 struct journal_head *jh, *next_jh, *last_jh;
114 struct buffer_head *bh;
117 assert_spin_locked(&journal_datalist_lock);
118 jh = transaction->t_checkpoint_list;
122 last_jh = jh->b_cpprev;
127 if (buffer_locked(bh)) {
128 atomic_inc(&bh->b_count);
129 spin_unlock(&journal_datalist_lock);
130 unlock_journal(journal);
132 /* the journal_head may have gone by now */
133 BUFFER_TRACE(bh, "brelse");
138 if (jh->b_transaction != NULL) {
139 transaction_t *transaction = jh->b_transaction;
140 tid_t tid = transaction->t_tid;
142 spin_unlock(&journal_datalist_lock);
143 log_start_commit(journal, transaction);
144 unlock_journal(journal);
145 log_wait_commit(journal, tid);
150 * We used to test for (jh->b_list != BUF_CLEAN) here.
151 * But unmap_underlying_metadata() can place buffer onto
152 * BUF_CLEAN. Since refile_buffer() no longer takes buffers
153 * off checkpoint lists, we cope with it here
156 * AKPM: I think the buffer_jdirty test is redundant - it
157 * shouldn't have NULL b_transaction?
159 next_jh = jh->b_cpnext;
160 if (!buffer_dirty(bh) && !buffer_jdirty(bh)) {
161 BUFFER_TRACE(bh, "remove from checkpoint");
162 __journal_remove_checkpoint(jh);
163 __journal_remove_journal_head(bh);
170 } while (jh != last_jh);
174 lock_journal(journal);
175 spin_lock(&journal_datalist_lock);
181 static void __flush_batch(struct buffer_head **bhs, int *batch_count)
185 spin_unlock(&journal_datalist_lock);
186 ll_rw_block(WRITE, *batch_count, bhs);
187 run_task_queue(&tq_disk);
188 spin_lock(&journal_datalist_lock);
189 for (i = 0; i < *batch_count; i++) {
190 struct buffer_head *bh = bhs[i];
191 clear_bit(BH_JWrite, &bh->b_state);
192 BUFFER_TRACE(bh, "brelse");
199 * Try to flush one buffer from the checkpoint list to disk.
201 * Return 1 if something happened which requires us to abort the current
202 * scan of the checkpoint list.
204 * Called with journal_datalist_lock held.
206 static int __flush_buffer(journal_t *journal, struct journal_head *jh,
207 struct buffer_head **bhs, int *batch_count,
210 struct buffer_head *bh = jh2bh(jh);
213 if (buffer_dirty(bh) && !buffer_locked(bh) && jh->b_jlist == BJ_None) {
214 J_ASSERT_JH(jh, jh->b_transaction == NULL);
217 * Important: we are about to write the buffer, and
218 * possibly block, while still holding the journal lock.
219 * We cannot afford to let the transaction logic start
220 * messing around with this buffer before we write it to
221 * disk, as that would break recoverability.
223 BUFFER_TRACE(bh, "queue");
224 atomic_inc(&bh->b_count);
225 J_ASSERT_BH(bh, !test_bit(BH_JWrite, &bh->b_state));
226 set_bit(BH_JWrite, &bh->b_state);
227 bhs[*batch_count] = bh;
229 if (*batch_count == NR_BATCH) {
230 __flush_batch(bhs, batch_count);
235 if (jh->b_cpnext == jh) {
236 /* We may be about to drop the transaction. Tell the
237 * caller that the lists have changed.
241 if (__try_to_free_cp_buf(jh)) {
251 * Perform an actual checkpoint. We don't write out only enough to
252 * satisfy the current blocked requests: rather we submit a reasonably
253 * sized chunk of the outstanding data to disk at once for
254 * efficiency. log_wait_for_space() will retry if we didn't free enough.
256 * However, we _do_ take into account the amount requested so that once
257 * the IO has been queued, we can return as soon as enough of it has
260 * The journal should be locked before calling this function.
263 /* @@@ `nblocks' is unused. Should it be used? */
264 int log_do_checkpoint (journal_t *journal, int nblocks)
266 transaction_t *transaction, *last_transaction, *next_transaction;
270 struct buffer_head *bhs[NR_BATCH];
272 jbd_debug(1, "Start checkpoint\n");
275 * First thing: if there are any transactions in the log which
276 * don't need checkpointing, just eliminate them from the
277 * journal straight away.
279 result = cleanup_journal_tail(journal);
280 jbd_debug(1, "cleanup_journal_tail returned %d\n", result);
285 * OK, we need to start writing disk blocks. Try to free up a
286 * quarter of the log in a single checkpoint if we can.
289 * AKPM: check this code. I had a feeling a while back that it
290 * degenerates into a busy loop at unmount time.
292 target = (journal->j_last - journal->j_first) / 4;
294 spin_lock(&journal_datalist_lock);
296 transaction = journal->j_checkpoint_transactions;
297 if (transaction == NULL)
299 last_transaction = transaction->t_cpprev;
300 next_transaction = transaction;
303 struct journal_head *jh, *last_jh, *next_jh;
305 int cleanup_ret, retry = 0;
307 transaction = next_transaction;
308 next_transaction = transaction->t_cpnext;
309 jh = transaction->t_checkpoint_list;
310 last_jh = jh->b_cpprev;
314 next_jh = jh->b_cpnext;
315 retry = __flush_buffer(journal, jh, bhs, &batch_count,
317 } while (jh != last_jh && !retry);
319 __flush_batch(bhs, &batch_count);
325 * We have walked the whole transaction list without
326 * finding anything to write to disk. We had better be
327 * able to make some progress or we are in trouble.
329 cleanup_ret = __cleanup_transaction(journal, transaction);
330 J_ASSERT(drop_count != 0 || cleanup_ret != 0);
331 goto repeat; /* __cleanup may have dropped lock */
332 } while (transaction != last_transaction);
335 spin_unlock(&journal_datalist_lock);
336 result = cleanup_journal_tail(journal);
344 * Check the list of checkpoint transactions for the journal to see if
345 * we have already got rid of any since the last update of the log tail
346 * in the journal superblock. If so, we can instantly roll the
347 * superblock forward to remove those transactions from the log.
349 * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
351 * Called with the journal lock held.
353 * This is the only part of the journaling code which really needs to be
354 * aware of transaction aborts. Checkpointing involves writing to the
355 * main filesystem area rather than to the journal, so it can proceed
356 * even in abort state, but we must not update the journal superblock if
357 * we have an abort error outstanding.
360 int cleanup_journal_tail(journal_t *journal)
362 transaction_t * transaction;
364 unsigned long blocknr, freed;
366 /* OK, work out the oldest transaction remaining in the log, and
367 * the log block it starts at.
369 * If the log is now empty, we need to work out which is the
370 * next transaction ID we will write, and where it will
373 /* j_checkpoint_transactions needs locking */
374 spin_lock(&journal_datalist_lock);
375 transaction = journal->j_checkpoint_transactions;
377 first_tid = transaction->t_tid;
378 blocknr = transaction->t_log_start;
379 } else if ((transaction = journal->j_committing_transaction) != NULL) {
380 first_tid = transaction->t_tid;
381 blocknr = transaction->t_log_start;
382 } else if ((transaction = journal->j_running_transaction) != NULL) {
383 first_tid = transaction->t_tid;
384 blocknr = journal->j_head;
386 first_tid = journal->j_transaction_sequence;
387 blocknr = journal->j_head;
389 spin_unlock(&journal_datalist_lock);
390 J_ASSERT (blocknr != 0);
392 /* If the oldest pinned transaction is at the tail of the log
393 already then there's not much we can do right now. */
394 if (journal->j_tail_sequence == first_tid)
397 /* OK, update the superblock to recover the freed space.
398 * Physical blocks come first: have we wrapped beyond the end of
400 freed = blocknr - journal->j_tail;
401 if (blocknr < journal->j_tail)
402 freed = freed + journal->j_last - journal->j_first;
405 "Cleaning journal tail from %d to %d (offset %lu), "
407 journal->j_tail_sequence, first_tid, blocknr, freed);
409 journal->j_free += freed;
410 journal->j_tail_sequence = first_tid;
411 journal->j_tail = blocknr;
412 if (!(journal->j_flags & JFS_ABORT))
413 journal_update_superblock(journal, 1);
418 /* Checkpoint list management */
421 * journal_clean_checkpoint_list
423 * Find all the written-back checkpoint buffers in the journal and release them.
425 * Called with the journal locked.
426 * Called with journal_datalist_lock held.
427 * Returns number of bufers reaped (for debug)
430 int __journal_clean_checkpoint_list(journal_t *journal)
432 transaction_t *transaction, *last_transaction, *next_transaction;
435 transaction = journal->j_checkpoint_transactions;
436 if (transaction == 0)
439 last_transaction = transaction->t_cpprev;
440 next_transaction = transaction;
442 struct journal_head *jh;
444 transaction = next_transaction;
445 next_transaction = transaction->t_cpnext;
446 jh = transaction->t_checkpoint_list;
448 struct journal_head *last_jh = jh->b_cpprev;
449 struct journal_head *next_jh = jh;
452 next_jh = jh->b_cpnext;
453 ret += __try_to_free_cp_buf(jh);
454 } while (jh != last_jh);
456 } while (transaction != last_transaction);
462 * journal_remove_checkpoint: called after a buffer has been committed
463 * to disk (either by being write-back flushed to disk, or being
464 * committed to the log).
466 * We cannot safely clean a transaction out of the log until all of the
467 * buffer updates committed in that transaction have safely been stored
468 * elsewhere on disk. To achieve this, all of the buffers in a
469 * transaction need to be maintained on the transaction's checkpoint
470 * list until they have been rewritten, at which point this function is
471 * called to remove the buffer from the existing transaction's
474 * This function is called with the journal locked.
475 * This function is called with journal_datalist_lock held.
478 void __journal_remove_checkpoint(struct journal_head *jh)
480 transaction_t *transaction;
483 JBUFFER_TRACE(jh, "entry");
485 if ((transaction = jh->b_cp_transaction) == NULL) {
486 JBUFFER_TRACE(jh, "not on transaction");
490 journal = transaction->t_journal;
494 if (transaction->t_checkpoint_list != NULL)
496 JBUFFER_TRACE(jh, "transaction has no more buffers");
498 /* There is one special case to worry about: if we have just
499 pulled the buffer off a committing transaction's forget list,
500 then even if the checkpoint list is empty, the transaction
501 obviously cannot be dropped! */
503 if (transaction == journal->j_committing_transaction) {
504 JBUFFER_TRACE(jh, "belongs to committing transaction");
508 /* OK, that was the last buffer for the transaction: we can now
509 safely remove this transaction from the log */
511 __journal_drop_transaction(journal, transaction);
513 /* Just in case anybody was waiting for more transactions to be
515 wake_up(&journal->j_wait_logspace);
517 JBUFFER_TRACE(jh, "exit");
520 void journal_remove_checkpoint(struct journal_head *jh)
522 spin_lock(&journal_datalist_lock);
523 __journal_remove_checkpoint(jh);
524 spin_unlock(&journal_datalist_lock);
528 * journal_insert_checkpoint: put a committed buffer onto a checkpoint
529 * list so that we know when it is safe to clean the transaction out of
532 * Called with the journal locked.
533 * Called with journal_datalist_lock held.
535 void __journal_insert_checkpoint(struct journal_head *jh,
536 transaction_t *transaction)
538 JBUFFER_TRACE(jh, "entry");
539 J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jdirty(jh2bh(jh)));
540 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
542 assert_spin_locked(&journal_datalist_lock);
543 jh->b_cp_transaction = transaction;
545 if (!transaction->t_checkpoint_list) {
546 jh->b_cpnext = jh->b_cpprev = jh;
548 jh->b_cpnext = transaction->t_checkpoint_list;
549 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
550 jh->b_cpprev->b_cpnext = jh;
551 jh->b_cpnext->b_cpprev = jh;
553 transaction->t_checkpoint_list = jh;
556 void journal_insert_checkpoint(struct journal_head *jh,
557 transaction_t *transaction)
559 spin_lock(&journal_datalist_lock);
560 __journal_insert_checkpoint(jh, transaction);
561 spin_unlock(&journal_datalist_lock);
565 * We've finished with this transaction structure: adios...
567 * The transaction must have no links except for the checkpoint by this
570 * Called with the journal locked.
571 * Called with journal_datalist_lock held.
574 void __journal_drop_transaction(journal_t *journal, transaction_t *transaction)
576 assert_spin_locked(&journal_datalist_lock);
577 if (transaction->t_cpnext) {
578 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
579 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
580 if (journal->j_checkpoint_transactions == transaction)
581 journal->j_checkpoint_transactions =
582 transaction->t_cpnext;
583 if (journal->j_checkpoint_transactions == transaction)
584 journal->j_checkpoint_transactions = NULL;
587 J_ASSERT (transaction->t_ilist == NULL);
588 J_ASSERT (transaction->t_buffers == NULL);
589 J_ASSERT (transaction->t_sync_datalist == NULL);
590 J_ASSERT (transaction->t_async_datalist == NULL);
591 J_ASSERT (transaction->t_forget == NULL);
592 J_ASSERT (transaction->t_iobuf_list == NULL);
593 J_ASSERT (transaction->t_shadow_list == NULL);
594 J_ASSERT (transaction->t_log_list == NULL);
595 J_ASSERT (transaction->t_checkpoint_list == NULL);
596 J_ASSERT (transaction->t_updates == 0);
597 J_ASSERT (list_empty(&transaction->t_jcb));
599 J_ASSERT (transaction->t_journal->j_committing_transaction !=
602 jbd_debug (1, "Dropping transaction %d, all done\n",