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.
29 * Called with j_list_lock held.
32 static inline void __buffer_unlink(struct journal_head
*jh
)
34 transaction_t
*transaction
;
36 transaction
= jh
->b_cp_transaction
;
37 jh
->b_cp_transaction
= NULL
;
39 jh
->b_cpnext
->b_cpprev
= jh
->b_cpprev
;
40 jh
->b_cpprev
->b_cpnext
= jh
->b_cpnext
;
41 if (transaction
->t_checkpoint_list
== jh
)
42 transaction
->t_checkpoint_list
= jh
->b_cpnext
;
43 if (transaction
->t_checkpoint_list
== jh
)
44 transaction
->t_checkpoint_list
= NULL
;
48 * Try to release a checkpointed buffer from its transaction.
49 * Returns 1 if we released it.
50 * Requires j_list_lock
51 * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
53 static int __try_to_free_cp_buf(struct journal_head
*jh
)
56 struct buffer_head
*bh
= jh2bh(jh
);
58 if (jh
->b_jlist
== BJ_None
&& !buffer_locked(bh
) && !buffer_dirty(bh
)) {
59 JBUFFER_TRACE(jh
, "remove from checkpoint list");
60 __journal_remove_checkpoint(jh
);
61 jbd_unlock_bh_state(bh
);
62 journal_remove_journal_head(bh
);
63 BUFFER_TRACE(bh
, "release");
67 jbd_unlock_bh_state(bh
);
73 * __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 __log_wait_for_space(journal_t
*journal
)
81 assert_spin_locked(&journal
->j_state_lock
);
83 nblocks
= jbd_space_needed(journal
);
84 while (__log_space_left(journal
) < nblocks
) {
85 if (journal
->j_flags
& JFS_ABORT
)
87 spin_unlock(&journal
->j_state_lock
);
88 down(&journal
->j_checkpoint_sem
);
91 * Test again, another process may have checkpointed while we
92 * were waiting for the checkpoint lock
94 spin_lock(&journal
->j_state_lock
);
95 nblocks
= jbd_space_needed(journal
);
96 if (__log_space_left(journal
) < nblocks
) {
97 spin_unlock(&journal
->j_state_lock
);
98 log_do_checkpoint(journal
);
99 spin_lock(&journal
->j_state_lock
);
101 up(&journal
->j_checkpoint_sem
);
106 * We were unable to perform jbd_trylock_bh_state() inside j_list_lock.
107 * The caller must restart a list walk. Wait for someone else to run
108 * jbd_unlock_bh_state().
110 static void jbd_sync_bh(journal_t
*journal
, struct buffer_head
*bh
)
113 spin_unlock(&journal
->j_list_lock
);
114 jbd_lock_bh_state(bh
);
115 jbd_unlock_bh_state(bh
);
120 * Clean up a transaction's checkpoint list.
122 * We wait for any pending IO to complete and make sure any clean
123 * buffers are removed from the transaction.
125 * Return 1 if we performed any actions which might have destroyed the
126 * checkpoint. (journal_remove_checkpoint() deletes the transaction when
127 * the last checkpoint buffer is cleansed)
129 * Called with j_list_lock held.
131 static int __cleanup_transaction(journal_t
*journal
, transaction_t
*transaction
)
133 struct journal_head
*jh
, *next_jh
, *last_jh
;
134 struct buffer_head
*bh
;
137 assert_spin_locked(&journal
->j_list_lock
);
138 jh
= transaction
->t_checkpoint_list
;
142 last_jh
= jh
->b_cpprev
;
147 if (buffer_locked(bh
)) {
148 atomic_inc(&bh
->b_count
);
149 spin_unlock(&journal
->j_list_lock
);
151 /* the journal_head may have gone by now */
152 BUFFER_TRACE(bh
, "brelse");
160 if (!jbd_trylock_bh_state(bh
)) {
161 jbd_sync_bh(journal
, bh
);
165 if (jh
->b_transaction
!= NULL
) {
166 transaction_t
*t
= jh
->b_transaction
;
167 tid_t tid
= t
->t_tid
;
169 spin_unlock(&journal
->j_list_lock
);
170 jbd_unlock_bh_state(bh
);
171 log_start_commit(journal
, tid
);
172 log_wait_commit(journal
, tid
);
177 * AKPM: I think the buffer_jbddirty test is redundant - it
178 * shouldn't have NULL b_transaction?
180 next_jh
= jh
->b_cpnext
;
181 if (!buffer_dirty(bh
) && !buffer_jbddirty(bh
)) {
182 BUFFER_TRACE(bh
, "remove from checkpoint");
183 __journal_remove_checkpoint(jh
);
184 jbd_unlock_bh_state(bh
);
185 journal_remove_journal_head(bh
);
189 jbd_unlock_bh_state(bh
);
192 } while (jh
!= last_jh
);
196 spin_lock(&journal
->j_list_lock
);
203 __flush_batch(journal_t
*journal
, struct buffer_head
**bhs
, int *batch_count
)
207 spin_unlock(&journal
->j_list_lock
);
208 ll_rw_block(WRITE
, *batch_count
, bhs
);
209 spin_lock(&journal
->j_list_lock
);
210 for (i
= 0; i
< *batch_count
; i
++) {
211 struct buffer_head
*bh
= bhs
[i
];
212 clear_buffer_jwrite(bh
);
213 BUFFER_TRACE(bh
, "brelse");
220 * Try to flush one buffer from the checkpoint list to disk.
222 * Return 1 if something happened which requires us to abort the current
223 * scan of the checkpoint list.
225 * Called with j_list_lock held.
226 * Called under jbd_lock_bh_state(jh2bh(jh)), and drops it
228 static int __flush_buffer(journal_t
*journal
, struct journal_head
*jh
,
229 struct buffer_head
**bhs
, int *batch_count
,
232 struct buffer_head
*bh
= jh2bh(jh
);
235 if (buffer_dirty(bh
) && !buffer_locked(bh
) && jh
->b_jlist
== BJ_None
) {
236 J_ASSERT_JH(jh
, jh
->b_transaction
== NULL
);
239 * Important: we are about to write the buffer, and
240 * possibly block, while still holding the journal lock.
241 * We cannot afford to let the transaction logic start
242 * messing around with this buffer before we write it to
243 * disk, as that would break recoverability.
245 BUFFER_TRACE(bh
, "queue");
247 J_ASSERT_BH(bh
, !buffer_jwrite(bh
));
248 set_buffer_jwrite(bh
);
249 bhs
[*batch_count
] = bh
;
250 jbd_unlock_bh_state(bh
);
252 if (*batch_count
== NR_BATCH
) {
253 __flush_batch(journal
, bhs
, batch_count
);
258 if (jh
->b_cpnext
== jh
) {
259 /* We may be about to drop the transaction. Tell the
260 * caller that the lists have changed.
264 if (__try_to_free_cp_buf(jh
)) {
273 * Perform an actual checkpoint. We don't write out only enough to
274 * satisfy the current blocked requests: rather we submit a reasonably
275 * sized chunk of the outstanding data to disk at once for
276 * efficiency. __log_wait_for_space() will retry if we didn't free enough.
278 * However, we _do_ take into account the amount requested so that once
279 * the IO has been queued, we can return as soon as enough of it has
282 * The journal should be locked before calling this function.
284 int log_do_checkpoint(journal_t
*journal
)
288 struct buffer_head
*bhs
[NR_BATCH
];
290 jbd_debug(1, "Start checkpoint\n");
293 * First thing: if there are any transactions in the log which
294 * don't need checkpointing, just eliminate them from the
295 * journal straight away.
297 result
= cleanup_journal_tail(journal
);
298 jbd_debug(1, "cleanup_journal_tail returned %d\n", result
);
303 * OK, we need to start writing disk blocks. Try to free up a
304 * quarter of the log in a single checkpoint if we can.
307 * AKPM: check this code. I had a feeling a while back that it
308 * degenerates into a busy loop at unmount time.
310 spin_lock(&journal
->j_list_lock
);
311 while (journal
->j_checkpoint_transactions
) {
312 transaction_t
*transaction
;
313 struct journal_head
*jh
, *last_jh
, *next_jh
;
315 int cleanup_ret
, retry
= 0;
318 transaction
= journal
->j_checkpoint_transactions
;
319 this_tid
= transaction
->t_tid
;
320 jh
= transaction
->t_checkpoint_list
;
321 last_jh
= jh
->b_cpprev
;
324 struct buffer_head
*bh
;
327 next_jh
= jh
->b_cpnext
;
329 if (!jbd_trylock_bh_state(bh
)) {
330 jbd_sync_bh(journal
, bh
);
331 spin_lock(&journal
->j_list_lock
);
335 retry
= __flush_buffer(journal
, jh
, bhs
, &batch_count
, &drop_count
);
336 } while (jh
!= last_jh
&& !retry
);
339 __flush_batch(journal
, bhs
, &batch_count
);
342 * If someone cleaned up this transaction while we slept, we're
345 if (journal
->j_checkpoint_transactions
!= transaction
)
350 * Maybe it's a new transaction, but it fell at the same
353 if (transaction
->t_tid
!= this_tid
)
356 * We have walked the whole transaction list without
357 * finding anything to write to disk. We had better be
358 * able to make some progress or we are in trouble.
360 cleanup_ret
= __cleanup_transaction(journal
, transaction
);
361 J_ASSERT(drop_count
!= 0 || cleanup_ret
!= 0);
362 if (journal
->j_checkpoint_transactions
!= transaction
)
365 spin_unlock(&journal
->j_list_lock
);
366 result
= cleanup_journal_tail(journal
);
374 * Check the list of checkpoint transactions for the journal to see if
375 * we have already got rid of any since the last update of the log tail
376 * in the journal superblock. If so, we can instantly roll the
377 * superblock forward to remove those transactions from the log.
379 * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
381 * Called with the journal lock held.
383 * This is the only part of the journaling code which really needs to be
384 * aware of transaction aborts. Checkpointing involves writing to the
385 * main filesystem area rather than to the journal, so it can proceed
386 * even in abort state, but we must not update the journal superblock if
387 * we have an abort error outstanding.
390 int cleanup_journal_tail(journal_t
*journal
)
392 transaction_t
* transaction
;
394 unsigned long blocknr
, freed
;
396 /* OK, work out the oldest transaction remaining in the log, and
397 * the log block it starts at.
399 * If the log is now empty, we need to work out which is the
400 * next transaction ID we will write, and where it will
403 spin_lock(&journal
->j_state_lock
);
404 spin_lock(&journal
->j_list_lock
);
405 transaction
= journal
->j_checkpoint_transactions
;
407 first_tid
= transaction
->t_tid
;
408 blocknr
= transaction
->t_log_start
;
409 } else if ((transaction
= journal
->j_committing_transaction
) != NULL
) {
410 first_tid
= transaction
->t_tid
;
411 blocknr
= transaction
->t_log_start
;
412 } else if ((transaction
= journal
->j_running_transaction
) != NULL
) {
413 first_tid
= transaction
->t_tid
;
414 blocknr
= journal
->j_head
;
416 first_tid
= journal
->j_transaction_sequence
;
417 blocknr
= journal
->j_head
;
419 spin_unlock(&journal
->j_list_lock
);
420 J_ASSERT(blocknr
!= 0);
422 /* If the oldest pinned transaction is at the tail of the log
423 already then there's not much we can do right now. */
424 if (journal
->j_tail_sequence
== first_tid
) {
425 spin_unlock(&journal
->j_state_lock
);
429 /* OK, update the superblock to recover the freed space.
430 * Physical blocks come first: have we wrapped beyond the end of
432 freed
= blocknr
- journal
->j_tail
;
433 if (blocknr
< journal
->j_tail
)
434 freed
= freed
+ journal
->j_last
- journal
->j_first
;
437 "Cleaning journal tail from %d to %d (offset %lu), "
439 journal
->j_tail_sequence
, first_tid
, blocknr
, freed
);
441 journal
->j_free
+= freed
;
442 journal
->j_tail_sequence
= first_tid
;
443 journal
->j_tail
= blocknr
;
444 spin_unlock(&journal
->j_state_lock
);
445 if (!(journal
->j_flags
& JFS_ABORT
))
446 journal_update_superblock(journal
, 1);
451 /* Checkpoint list management */
454 * journal_clean_checkpoint_list
456 * Find all the written-back checkpoint buffers in the journal and release them.
458 * Called with the journal locked.
459 * Called with j_list_lock held.
460 * Returns number of bufers reaped (for debug)
463 int __journal_clean_checkpoint_list(journal_t
*journal
)
465 transaction_t
*transaction
, *last_transaction
, *next_transaction
;
468 transaction
= journal
->j_checkpoint_transactions
;
469 if (transaction
== 0)
472 last_transaction
= transaction
->t_cpprev
;
473 next_transaction
= transaction
;
475 struct journal_head
*jh
;
477 transaction
= next_transaction
;
478 next_transaction
= transaction
->t_cpnext
;
479 jh
= transaction
->t_checkpoint_list
;
481 struct journal_head
*last_jh
= jh
->b_cpprev
;
482 struct journal_head
*next_jh
= jh
;
486 next_jh
= jh
->b_cpnext
;
487 /* Use trylock because of the ranknig */
488 if (jbd_trylock_bh_state(jh2bh(jh
)))
489 ret
+= __try_to_free_cp_buf(jh
);
490 } while (jh
!= last_jh
);
492 } while (transaction
!= last_transaction
);
498 * journal_remove_checkpoint: called after a buffer has been committed
499 * to disk (either by being write-back flushed to disk, or being
500 * committed to the log).
502 * We cannot safely clean a transaction out of the log until all of the
503 * buffer updates committed in that transaction have safely been stored
504 * elsewhere on disk. To achieve this, all of the buffers in a
505 * transaction need to be maintained on the transaction's checkpoint
506 * list until they have been rewritten, at which point this function is
507 * called to remove the buffer from the existing transaction's
510 * This function is called with the journal locked.
511 * This function is called with j_list_lock held.
514 void __journal_remove_checkpoint(struct journal_head
*jh
)
516 transaction_t
*transaction
;
519 JBUFFER_TRACE(jh
, "entry");
521 if ((transaction
= jh
->b_cp_transaction
) == NULL
) {
522 JBUFFER_TRACE(jh
, "not on transaction");
525 journal
= transaction
->t_journal
;
529 if (transaction
->t_checkpoint_list
!= NULL
)
531 JBUFFER_TRACE(jh
, "transaction has no more buffers");
534 * There is one special case to worry about: if we have just pulled the
535 * buffer off a committing transaction's forget list, then even if the
536 * checkpoint list is empty, the transaction obviously cannot be
539 * The locking here around j_committing_transaction is a bit sleazy.
540 * See the comment at the end of journal_commit_transaction().
542 if (transaction
== journal
->j_committing_transaction
) {
543 JBUFFER_TRACE(jh
, "belongs to committing transaction");
547 /* OK, that was the last buffer for the transaction: we can now
548 safely remove this transaction from the log */
550 __journal_drop_transaction(journal
, transaction
);
552 /* Just in case anybody was waiting for more transactions to be
554 wake_up(&journal
->j_wait_logspace
);
556 JBUFFER_TRACE(jh
, "exit");
560 * journal_insert_checkpoint: put a committed buffer onto a checkpoint
561 * list so that we know when it is safe to clean the transaction out of
564 * Called with the journal locked.
565 * Called with j_list_lock held.
567 void __journal_insert_checkpoint(struct journal_head
*jh
,
568 transaction_t
*transaction
)
570 JBUFFER_TRACE(jh
, "entry");
571 J_ASSERT_JH(jh
, buffer_dirty(jh2bh(jh
)) || buffer_jbddirty(jh2bh(jh
)));
572 J_ASSERT_JH(jh
, jh
->b_cp_transaction
== NULL
);
574 jh
->b_cp_transaction
= transaction
;
576 if (!transaction
->t_checkpoint_list
) {
577 jh
->b_cpnext
= jh
->b_cpprev
= jh
;
579 jh
->b_cpnext
= transaction
->t_checkpoint_list
;
580 jh
->b_cpprev
= transaction
->t_checkpoint_list
->b_cpprev
;
581 jh
->b_cpprev
->b_cpnext
= jh
;
582 jh
->b_cpnext
->b_cpprev
= jh
;
584 transaction
->t_checkpoint_list
= jh
;
588 * We've finished with this transaction structure: adios...
590 * The transaction must have no links except for the checkpoint by this
593 * Called with the journal locked.
594 * Called with j_list_lock held.
597 void __journal_drop_transaction(journal_t
*journal
, transaction_t
*transaction
)
599 assert_spin_locked(&journal
->j_list_lock
);
600 if (transaction
->t_cpnext
) {
601 transaction
->t_cpnext
->t_cpprev
= transaction
->t_cpprev
;
602 transaction
->t_cpprev
->t_cpnext
= transaction
->t_cpnext
;
603 if (journal
->j_checkpoint_transactions
== transaction
)
604 journal
->j_checkpoint_transactions
=
605 transaction
->t_cpnext
;
606 if (journal
->j_checkpoint_transactions
== transaction
)
607 journal
->j_checkpoint_transactions
= NULL
;
610 J_ASSERT(transaction
->t_state
== T_FINISHED
);
611 J_ASSERT(transaction
->t_buffers
== NULL
);
612 J_ASSERT(transaction
->t_sync_datalist
== NULL
);
613 J_ASSERT(transaction
->t_forget
== NULL
);
614 J_ASSERT(transaction
->t_iobuf_list
== NULL
);
615 J_ASSERT(transaction
->t_shadow_list
== NULL
);
616 J_ASSERT(transaction
->t_log_list
== NULL
);
617 J_ASSERT(transaction
->t_checkpoint_list
== NULL
);
618 J_ASSERT(transaction
->t_updates
== 0);
619 J_ASSERT(list_empty(&transaction
->t_jcb
));
620 J_ASSERT(journal
->j_committing_transaction
!= transaction
);
621 J_ASSERT(journal
->j_running_transaction
!= transaction
);
623 jbd_debug(1, "Dropping transaction %d, all done\n", transaction
->t_tid
);