2 * linux/fs/transaction.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- 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 * Generic filesystem transaction handling code; part of the ext2fs
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
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>
26 #include <linux/timer.h>
27 #include <linux/smp_lock.h>
30 extern spinlock_t journal_datalist_lock;
33 * get_transaction: obtain a new transaction_t object.
35 * Simply allocate and initialise a new transaction. Create it in
36 * RUNNING state and add it to the current journal (which should not
37 * have an existing running transaction: we only make a new transaction
38 * once we have started to commit the old one).
41 * The journal MUST be locked. We don't perform atomic mallocs on the
42 * new transaction and we can't block without protecting against other
43 * processes trying to touch the journal while it is in transition.
46 static transaction_t * get_transaction (journal_t * journal, int is_try)
48 transaction_t * transaction;
50 transaction = jbd_kmalloc (sizeof (transaction_t), GFP_NOFS);
54 memset (transaction, 0, sizeof (transaction_t));
56 transaction->t_journal = journal;
57 transaction->t_state = T_RUNNING;
58 transaction->t_tid = journal->j_transaction_sequence++;
59 transaction->t_expires = jiffies + journal->j_commit_interval;
60 INIT_LIST_HEAD(&transaction->t_jcb);
62 if (journal->j_commit_interval) {
63 /* Set up the commit timer for the new transaction. */
64 J_ASSERT (!journal->j_commit_timer_active);
65 journal->j_commit_timer_active = 1;
66 journal->j_commit_timer->expires = transaction->t_expires;
67 add_timer(journal->j_commit_timer);
70 J_ASSERT (journal->j_running_transaction == NULL);
71 journal->j_running_transaction = transaction;
79 * A handle_t is an object which represents a single atomic update to a
80 * filesystem, and which tracks all of the modifications which form part
85 * start_this_handle: Given a handle, deal with any locking or stalling
86 * needed to make sure that there is enough journal space for the handle
87 * to begin. Attach the handle to a transaction and set up the
88 * transaction's buffer credits.
91 static int start_this_handle(journal_t *journal, handle_t *handle)
93 transaction_t *transaction;
95 int nblocks = handle->h_buffer_credits;
97 if (nblocks > journal->j_max_transaction_buffers) {
98 jbd_debug(1, "JBD: %s wants too many credits (%d > %d)\n",
99 current->comm, nblocks,
100 journal->j_max_transaction_buffers);
104 jbd_debug(3, "New handle %p going live.\n", handle);
108 lock_journal(journal);
112 if (is_journal_aborted(journal) ||
113 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
114 unlock_journal(journal);
118 /* Wait on the journal's transaction barrier if necessary */
119 if (journal->j_barrier_count) {
120 unlock_journal(journal);
121 sleep_on(&journal->j_wait_transaction_locked);
125 if (!journal->j_running_transaction)
126 get_transaction(journal, 0);
128 J_ASSERT(journal->j_running_transaction);
130 transaction = journal->j_running_transaction;
132 /* If the current transaction is locked down for commit, wait
133 * for the lock to be released. */
135 if (transaction->t_state == T_LOCKED) {
136 unlock_journal(journal);
137 jbd_debug(3, "Handle %p stalling...\n", handle);
138 sleep_on(&journal->j_wait_transaction_locked);
142 /* If there is not enough space left in the log to write all
143 * potential buffers requested by this operation, we need to
144 * stall pending a log checkpoint to free some more log
147 needed = transaction->t_outstanding_credits + nblocks;
149 if (needed > journal->j_max_transaction_buffers) {
150 /* If the current transaction is already too large, then
151 * start to commit it: we can then go back and attach
152 * this handle to a new transaction. */
154 jbd_debug(2, "Handle %p starting new commit...\n", handle);
155 log_start_commit(journal, transaction);
156 unlock_journal(journal);
157 sleep_on(&journal->j_wait_transaction_locked);
158 lock_journal(journal);
163 * The commit code assumes that it can get enough log space
164 * without forcing a checkpoint. This is *critical* for
165 * correctness: a checkpoint of a buffer which is also
166 * associated with a committing transaction creates a deadlock,
167 * so commit simply cannot force through checkpoints.
169 * We must therefore ensure the necessary space in the journal
170 * *before* starting to dirty potentially checkpointed buffers
171 * in the new transaction.
173 * The worst part is, any transaction currently committing can
174 * reduce the free space arbitrarily. Be careful to account for
175 * those buffers when checkpointing.
179 * @@@ AKPM: This seems rather over-defensive. We're giving commit
180 * a _lot_ of headroom: 1/4 of the journal plus the size of
181 * the committing transaction. Really, we only need to give it
182 * committing_transaction->t_outstanding_credits plus "enough" for
183 * the log control blocks.
184 * Also, this test is inconsitent with the matching one in
187 needed = journal->j_max_transaction_buffers;
188 if (journal->j_committing_transaction)
189 needed += journal->j_committing_transaction->
190 t_outstanding_credits;
192 if (log_space_left(journal) < needed) {
193 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
194 log_wait_for_space(journal, needed);
198 /* OK, account for the buffers that this operation expects to
199 * use and add the handle to the running transaction. */
201 handle->h_transaction = transaction;
202 transaction->t_outstanding_credits += nblocks;
203 transaction->t_updates++;
204 transaction->t_handle_count++;
205 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
206 handle, nblocks, transaction->t_outstanding_credits,
207 log_space_left(journal));
209 unlock_journal(journal);
214 /* Allocate a new handle. This should probably be in a slab... */
215 static handle_t *new_handle(int nblocks)
217 handle_t *handle = jbd_kmalloc(sizeof (handle_t), GFP_NOFS);
220 memset(handle, 0, sizeof (handle_t));
221 handle->h_buffer_credits = nblocks;
223 INIT_LIST_HEAD(&handle->h_jcb);
229 * handle_t *journal_start() - Obtain a new handle.
230 * @journal: Journal to start transaction on.
231 * @nblocks: number of block buffer we might modify
233 * We make sure that the transaction can guarantee at least nblocks of
234 * modified buffers in the log. We block until the log can guarantee
237 * This function is visible to journal users (like ext3fs), so is not
238 * called with the journal already locked.
240 * Return a pointer to a newly allocated handle, or NULL on failure
242 handle_t *journal_start(journal_t *journal, int nblocks)
244 handle_t *handle = journal_current_handle();
248 return ERR_PTR(-EROFS);
251 J_ASSERT(handle->h_transaction->t_journal == journal);
256 handle = new_handle(nblocks);
258 return ERR_PTR(-ENOMEM);
260 current->journal_info = handle;
262 err = start_this_handle(journal, handle);
265 current->journal_info = NULL;
273 * Return zero on success
275 static int try_start_this_handle(journal_t *journal, handle_t *handle)
277 transaction_t *transaction;
279 int nblocks = handle->h_buffer_credits;
282 jbd_debug(3, "New handle %p maybe going live.\n", handle);
284 lock_journal(journal);
286 if (is_journal_aborted(journal) ||
287 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
292 if (journal->j_barrier_count)
295 if (!journal->j_running_transaction && get_transaction(journal, 1) == 0)
298 transaction = journal->j_running_transaction;
299 if (transaction->t_state == T_LOCKED)
302 needed = transaction->t_outstanding_credits + nblocks;
303 /* We could run log_start_commit here */
304 if (needed > journal->j_max_transaction_buffers)
307 needed = journal->j_max_transaction_buffers;
308 if (journal->j_committing_transaction)
309 needed += journal->j_committing_transaction->
310 t_outstanding_credits;
312 if (log_space_left(journal) < needed)
315 handle->h_transaction = transaction;
316 transaction->t_outstanding_credits += nblocks;
317 transaction->t_updates++;
318 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
319 handle, nblocks, transaction->t_outstanding_credits,
320 log_space_left(journal));
321 unlock_journal(journal);
325 unlock_journal(journal);
332 * handle_t *journal_try_start() - Don't block, but try and get a handle
333 * @journal: Journal to start transaction on.
334 * @nblocks: number of block buffer we might modify
336 * Try to start a handle, but non-blockingly. If we weren't able
337 * to, return an ERR_PTR value.
339 handle_t *journal_try_start(journal_t *journal, int nblocks)
341 handle_t *handle = journal_current_handle();
345 return ERR_PTR(-EROFS);
348 jbd_debug(4, "h_ref %d -> %d\n",
351 J_ASSERT(handle->h_transaction->t_journal == journal);
352 if (is_handle_aborted(handle))
353 return ERR_PTR(-EIO);
357 jbd_debug(4, "no current transaction\n");
360 if (is_journal_aborted(journal))
361 return ERR_PTR(-EIO);
363 handle = new_handle(nblocks);
365 return ERR_PTR(-ENOMEM);
367 current->journal_info = handle;
369 err = try_start_this_handle(journal, handle);
372 current->journal_info = NULL;
380 * int journal_extend() - extend buffer credits.
381 * @handle: handle to 'extend'
382 * @nblocks: nr blocks to try to extend by.
384 * Some transactions, such as large extends and truncates, can be done
385 * atomically all at once or in several stages. The operation requests
386 * a credit for a number of buffer modications in advance, but can
387 * extend its credit if it needs more.
389 * journal_extend tries to give the running handle more buffer credits.
390 * It does not guarantee that allocation - this is a best-effort only.
391 * The calling process MUST be able to deal cleanly with a failure to
394 * Return 0 on success, non-zero on failure.
396 * return code < 0 implies an error
397 * return code > 0 implies normal transaction-full status.
399 int journal_extend (handle_t *handle, int nblocks)
401 transaction_t *transaction = handle->h_transaction;
402 journal_t *journal = transaction->t_journal;
406 lock_journal (journal);
409 if (is_handle_aborted(handle))
414 /* Don't extend a locked-down transaction! */
415 if (handle->h_transaction->t_state != T_RUNNING) {
416 jbd_debug(3, "denied handle %p %d blocks: "
417 "transaction not running\n", handle, nblocks);
421 wanted = transaction->t_outstanding_credits + nblocks;
423 if (wanted > journal->j_max_transaction_buffers) {
424 jbd_debug(3, "denied handle %p %d blocks: "
425 "transaction too large\n", handle, nblocks);
429 if (wanted > log_space_left(journal)) {
430 jbd_debug(3, "denied handle %p %d blocks: "
431 "insufficient log space\n", handle, nblocks);
435 handle->h_buffer_credits += nblocks;
436 transaction->t_outstanding_credits += nblocks;
439 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
442 unlock_journal (journal);
448 * int journal_restart() - restart a handle .
449 * @handle: handle to restart
450 * @nblocks: nr credits requested
452 * Restart a handle for a multi-transaction filesystem
455 * If the journal_extend() call above fails to grant new buffer credits
456 * to a running handle, a call to journal_restart will commit the
457 * handle's transaction so far and reattach the handle to a new
458 * transaction capabable of guaranteeing the requested number of
462 int journal_restart(handle_t *handle, int nblocks)
464 transaction_t *transaction = handle->h_transaction;
465 journal_t *journal = transaction->t_journal;
468 /* If we've had an abort of any type, don't even think about
469 * actually doing the restart! */
470 if (is_handle_aborted(handle))
473 /* First unlink the handle from its current transaction, and
474 * start the commit on that. */
476 J_ASSERT (transaction->t_updates > 0);
477 J_ASSERT (journal_current_handle() == handle);
479 transaction->t_outstanding_credits -= handle->h_buffer_credits;
480 transaction->t_updates--;
482 if (!transaction->t_updates)
483 wake_up(&journal->j_wait_updates);
485 jbd_debug(2, "restarting handle %p\n", handle);
486 log_start_commit(journal, transaction);
488 handle->h_buffer_credits = nblocks;
489 ret = start_this_handle(journal, handle);
495 * void journal_lock_updates () - establish a transaction barrier.
496 * @journal: Journal to establish a barrier on.
498 * This locks out any further updates from being started, and blocks
499 * until all existing updates have completed, returning only once the
500 * journal is in a quiescent state with no updates running.
502 * The journal lock should not be held on entry.
504 void journal_lock_updates (journal_t *journal)
506 lock_journal(journal);
507 ++journal->j_barrier_count;
509 /* Wait until there are no running updates */
511 transaction_t *transaction = journal->j_running_transaction;
514 if (!transaction->t_updates)
517 unlock_journal(journal);
518 sleep_on(&journal->j_wait_updates);
519 lock_journal(journal);
522 unlock_journal(journal);
524 /* We have now established a barrier against other normal
525 * updates, but we also need to barrier against other
526 * journal_lock_updates() calls to make sure that we serialise
527 * special journal-locked operations too. */
528 down(&journal->j_barrier);
532 * void journal_unlock_updates (journal_t* journal) - release barrier
533 * @journal: Journal to release the barrier on.
535 * Release a transaction barrier obtained with journal_lock_updates().
537 * Should be called without the journal lock held.
539 void journal_unlock_updates (journal_t *journal)
541 lock_journal(journal);
543 J_ASSERT (journal->j_barrier_count != 0);
545 up(&journal->j_barrier);
546 --journal->j_barrier_count;
547 wake_up(&journal->j_wait_transaction_locked);
548 unlock_journal(journal);
552 * if the buffer is already part of the current transaction, then there
553 * is nothing we need to do. if it is already part of a prior
554 * transaction which we are still committing to disk, then we need to
555 * make sure that we do not overwrite the old copy: we do copy-out to
556 * preserve the copy going to disk. we also account the buffer against
557 * the handle's metadata buffer credits (unless the buffer is already
558 * part of the transaction, that is).
561 do_get_write_access(handle_t *handle, struct journal_head *jh, int force_copy)
563 struct buffer_head *bh;
564 transaction_t *transaction = handle->h_transaction;
565 journal_t *journal = transaction->t_journal;
567 char *frozen_buffer = NULL;
571 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
573 JBUFFER_TRACE(jh, "entry");
577 /* @@@ Need to check for errors here at some point. */
580 * AKPM: we have replaced all the lock_journal_bh_wait() stuff with a
581 * simple lock_journal(). This code here will care for locked buffers.
583 locked = test_and_set_bit(BH_Lock, &bh->b_state);
585 /* We can't reliably test the buffer state if we found
586 * it already locked, so just wait for the lock and
588 unlock_journal(journal);
589 __wait_on_buffer(bh);
590 lock_journal(journal);
594 /* We now hold the buffer lock so it is safe to query the buffer
595 * state. Is the buffer dirty?
597 * If so, there are two possibilities. The buffer may be
598 * non-journaled, and undergoing a quite legitimate writeback.
599 * Otherwise, it is journaled, and we don't expect dirty buffers
600 * in that state (the buffers should be marked JBD_Dirty
601 * instead.) So either the IO is being done under our own
602 * control and this is a bug, or it's a third party IO such as
603 * dump(8) (which may leave the buffer scheduled for read ---
604 * ie. locked but not dirty) or tune2fs (which may actually have
605 * the buffer dirtied, ugh.) */
607 if (buffer_dirty(bh)) {
608 spin_lock(&journal_datalist_lock);
609 /* First question: is this buffer already part of the
610 * current transaction or the existing committing
612 if (jh->b_transaction) {
613 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
614 jh->b_transaction == journal->j_committing_transaction);
615 if (jh->b_next_transaction)
616 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
617 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
618 jbd_unexpected_dirty_buffer(jh);
620 spin_unlock(&journal_datalist_lock);
626 if (is_handle_aborted(handle))
630 spin_lock(&journal_datalist_lock);
632 /* The buffer is already part of this transaction if
633 * b_transaction or b_next_transaction points to it. */
635 if (jh->b_transaction == transaction ||
636 jh->b_next_transaction == transaction)
639 /* If there is already a copy-out version of this buffer, then
640 * we don't need to make another one. */
642 if (jh->b_frozen_data) {
643 JBUFFER_TRACE(jh, "has frozen data");
644 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
645 jh->b_next_transaction = transaction;
647 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
648 handle->h_buffer_credits--;
652 /* Is there data here we need to preserve? */
654 if (jh->b_transaction && jh->b_transaction != transaction) {
655 JBUFFER_TRACE(jh, "owned by older transaction");
656 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
657 J_ASSERT_JH(jh, jh->b_transaction ==
658 journal->j_committing_transaction);
660 /* There is one case we have to be very careful about.
661 * If the committing transaction is currently writing
662 * this buffer out to disk and has NOT made a copy-out,
663 * then we cannot modify the buffer contents at all
664 * right now. The essence of copy-out is that it is the
665 * extra copy, not the primary copy, which gets
666 * journaled. If the primary copy is already going to
667 * disk then we cannot do copy-out here. */
669 if (jh->b_jlist == BJ_Shadow) {
670 JBUFFER_TRACE(jh, "on shadow: sleep");
671 spin_unlock(&journal_datalist_lock);
672 unlock_journal(journal);
673 /* commit wakes up all shadow buffers after IO */
674 wait_event(jh2bh(jh)->b_wait,
675 jh->b_jlist != BJ_Shadow);
676 lock_journal(journal);
680 /* Only do the copy if the currently-owning transaction
681 * still needs it. If it is on the Forget list, the
682 * committing transaction is past that stage. The
683 * buffer had better remain locked during the kmalloc,
684 * but that should be true --- we hold the journal lock
685 * still and the buffer is already on the BUF_JOURNAL
686 * list so won't be flushed.
688 * Subtle point, though: if this is a get_undo_access,
689 * then we will be relying on the frozen_data to contain
690 * the new value of the committed_data record after the
691 * transaction, so we HAVE to force the frozen_data copy
694 if (jh->b_jlist != BJ_Forget || force_copy) {
695 JBUFFER_TRACE(jh, "generate frozen data");
696 if (!frozen_buffer) {
697 JBUFFER_TRACE(jh, "allocate memory for buffer");
698 spin_unlock(&journal_datalist_lock);
699 unlock_journal(journal);
700 frozen_buffer = jbd_kmalloc(jh2bh(jh)->b_size,
702 lock_journal(journal);
703 if (!frozen_buffer) {
705 "%s: OOM for frozen_buffer\n",
707 JBUFFER_TRACE(jh, "oom!");
709 spin_lock(&journal_datalist_lock);
715 jh->b_frozen_data = frozen_buffer;
716 frozen_buffer = NULL;
719 jh->b_next_transaction = transaction;
722 J_ASSERT(handle->h_buffer_credits > 0);
723 handle->h_buffer_credits--;
725 /* Finally, if the buffer is not journaled right now, we need to
726 * make sure it doesn't get written to disk before the caller
727 * actually commits the new data. */
729 if (!jh->b_transaction) {
730 JBUFFER_TRACE(jh, "no transaction");
731 J_ASSERT_JH(jh, !jh->b_next_transaction);
732 jh->b_transaction = transaction;
733 JBUFFER_TRACE(jh, "file as BJ_Reserved");
734 __journal_file_buffer(jh, transaction, BJ_Reserved);
738 spin_unlock(&journal_datalist_lock);
744 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
745 "Possible IO failure.\n");
746 page = jh2bh(jh)->b_page;
747 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
749 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
754 /* If we are about to journal a buffer, then any revoke pending
755 on it is no longer valid. */
756 journal_cancel_revoke(handle, jh);
760 kfree(frozen_buffer);
762 JBUFFER_TRACE(jh, "exit");
767 * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
768 * @handle: transaction to add buffer modifications to
769 * @bh: bh to be used for metadata writes
771 * Returns an error code or 0 on success.
773 * In full data journalling mode the buffer may be of type BJ_AsyncData,
774 * because we're write()ing a buffer which is also part of a shared mapping.
777 int journal_get_write_access (handle_t *handle, struct buffer_head *bh)
779 transaction_t *transaction = handle->h_transaction;
780 journal_t *journal = transaction->t_journal;
781 struct journal_head *jh = journal_add_journal_head(bh);
784 /* We do not want to get caught playing with fields which the
785 * log thread also manipulates. Make sure that the buffer
786 * completes any outstanding IO before proceeding. */
787 lock_journal(journal);
788 rc = do_get_write_access(handle, jh, 0);
789 journal_unlock_journal_head(jh);
790 unlock_journal(journal);
796 * When the user wants to journal a newly created buffer_head
797 * (ie. getblk() returned a new buffer and we are going to populate it
798 * manually rather than reading off disk), then we need to keep the
799 * buffer_head locked until it has been completely filled with new
800 * data. In this case, we should be able to make the assertion that
801 * the bh is not already part of an existing transaction.
803 * The buffer should already be locked by the caller by this point.
804 * There is no lock ranking violation: it was a newly created,
805 * unlocked buffer beforehand. */
808 * int journal_get_create_access () - notify intent to use newly created bh
809 * @handle: ransaction to new buffer to
812 * Call this if you create a new bh.
814 int journal_get_create_access (handle_t *handle, struct buffer_head *bh)
816 transaction_t *transaction = handle->h_transaction;
817 journal_t *journal = transaction->t_journal;
818 struct journal_head *jh = journal_add_journal_head(bh);
821 jbd_debug(5, "journal_head %p\n", jh);
822 lock_journal(journal);
824 if (is_handle_aborted(handle))
828 JBUFFER_TRACE(jh, "entry");
829 /* The buffer may already belong to this transaction due to
830 * pre-zeroing in the filesystem's new_block code. It may also
831 * be on the previous, committing transaction's lists, but it
832 * HAS to be in Forget state in that case: the transaction must
833 * have deleted the buffer for it to be reused here. */
834 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
835 jh->b_transaction == NULL ||
836 (jh->b_transaction == journal->j_committing_transaction &&
837 jh->b_jlist == BJ_Forget)));
839 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
840 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
842 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
843 handle->h_buffer_credits--;
845 spin_lock(&journal_datalist_lock);
846 if (jh->b_transaction == NULL) {
847 jh->b_transaction = transaction;
848 JBUFFER_TRACE(jh, "file as BJ_Reserved");
849 __journal_file_buffer(jh, transaction, BJ_Reserved);
850 JBUFFER_TRACE(jh, "refile");
851 refile_buffer(jh2bh(jh));
852 } else if (jh->b_transaction == journal->j_committing_transaction) {
853 JBUFFER_TRACE(jh, "set next transaction");
854 jh->b_next_transaction = transaction;
856 spin_unlock(&journal_datalist_lock);
859 * akpm: I added this. ext3_alloc_branch can pick up new indirect
860 * blocks which contain freed but then revoked metadata. We need
861 * to cancel the revoke in case we end up freeing it yet again
862 * and the reallocating as data - this would cause a second revoke,
863 * which hits an assertion error.
865 JBUFFER_TRACE(jh, "cancelling revoke");
866 journal_cancel_revoke(handle, jh);
867 journal_unlock_journal_head(jh);
869 unlock_journal(journal);
876 * int journal_get_undo_access() - Notify intent to modify metadata with non-rewindable consequences
877 * @handle: transaction
878 * @bh: buffer to undo
880 * Sometimes there is a need to distinguish between metadata which has
881 * been committed to disk and that which has not. The ext3fs code uses
882 * this for freeing and allocating space, we have to make sure that we
883 * do not reuse freed space until the deallocation has been committed,
884 * since if we overwrote that space we would make the delete
885 * un-rewindable in case of a crash.
887 * To deal with that, journal_get_undo_access requests write access to a
888 * buffer for parts of non-rewindable operations such as delete
889 * operations on the bitmaps. The journaling code must keep a copy of
890 * the buffer's contents prior to the undo_access call until such time
891 * as we know that the buffer has definitely been committed to disk.
893 * We never need to know which transaction the committed data is part
894 * of, buffers touched here are guaranteed to be dirtied later and so
895 * will be committed to a new transaction in due course, at which point
896 * we can discard the old committed data pointer.
898 * Returns error number or 0 on success.
900 int journal_get_undo_access (handle_t *handle, struct buffer_head *bh)
902 journal_t *journal = handle->h_transaction->t_journal;
904 struct journal_head *jh = journal_add_journal_head(bh);
906 JBUFFER_TRACE(jh, "entry");
907 lock_journal(journal);
909 /* Do this first --- it can drop the journal lock, so we want to
910 * make sure that obtaining the committed_data is done
911 * atomically wrt. completion of any outstanding commits. */
912 err = do_get_write_access (handle, jh, 1);
916 if (!jh->b_committed_data) {
917 /* Copy out the current buffer contents into the
918 * preserved, committed copy. */
919 JBUFFER_TRACE(jh, "generate b_committed data");
920 jh->b_committed_data = jbd_kmalloc(jh2bh(jh)->b_size,
922 if (!jh->b_committed_data) {
923 printk(KERN_EMERG "%s: No memory for committed data!\n",
929 memcpy (jh->b_committed_data, jh2bh(jh)->b_data,
935 J_ASSERT_JH(jh, jh->b_committed_data);
936 journal_unlock_journal_head(jh);
937 unlock_journal(journal);
942 * int journal_dirty_data() - mark a buffer as containing dirty data which needs to be flushed before we can commit the current transaction.
943 * @handle: transaction
944 * @bh: bufferhead to mark
947 * The buffer is placed on the transaction's data list and is marked as
948 * belonging to the transaction.
950 * If `async' is set then the writebask will be initiated by the caller
951 * using submit_bh -> end_buffer_io_async. We put the buffer onto
954 * Returns error number or 0 on success.
956 int journal_dirty_data (handle_t *handle, struct buffer_head *bh, int async)
959 * journal_dirty_data() can be called via page_launder->ext3_writepage
960 * by kswapd. So it cannot block. Happily, there's nothing here
961 * which needs lock_journal if `async' is set.
963 * When the buffer is on the current transaction we freely move it
964 * between BJ_AsyncData and BJ_SyncData according to who tried to
965 * change its state last.
967 journal_t *journal = handle->h_transaction->t_journal;
969 int wanted_jlist = async ? BJ_AsyncData : BJ_SyncData;
970 struct journal_head *jh;
972 if (is_handle_aborted(handle))
975 jh = journal_add_journal_head(bh);
976 JBUFFER_TRACE(jh, "entry");
979 * The buffer could *already* be dirty. Writeout can start
982 jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
985 * What if the buffer is already part of a running transaction?
987 * There are two cases:
988 * 1) It is part of the current running transaction. Refile it,
989 * just in case we have allocated it as metadata, deallocated
990 * it, then reallocated it as data.
991 * 2) It is part of the previous, still-committing transaction.
992 * If all we want to do is to guarantee that the buffer will be
993 * written to disk before this new transaction commits, then
994 * being sure that the *previous* transaction has this same
995 * property is sufficient for us! Just leave it on its old
998 * In case (2), the buffer must not already exist as metadata
999 * --- that would violate write ordering (a transaction is free
1000 * to write its data at any point, even before the previous
1001 * committing transaction has committed). The caller must
1002 * never, ever allow this to happen: there's nothing we can do
1003 * about it in this layer.
1005 spin_lock(&journal_datalist_lock);
1006 if (jh->b_transaction) {
1007 JBUFFER_TRACE(jh, "has transaction");
1008 if (jh->b_transaction != handle->h_transaction) {
1009 JBUFFER_TRACE(jh, "belongs to older transaction");
1010 J_ASSERT_JH(jh, jh->b_transaction ==
1011 journal->j_committing_transaction);
1013 /* @@@ IS THIS TRUE ? */
1015 * Not any more. Scenario: someone does a write()
1016 * in data=journal mode. The buffer's transaction has
1017 * moved into commit. Then someone does another
1018 * write() to the file. We do the frozen data copyout
1019 * and set b_next_transaction to point to j_running_t.
1020 * And while we're in that state, someone does a
1021 * writepage() in an attempt to pageout the same area
1022 * of the file via a shared mapping. At present that
1023 * calls journal_dirty_data(), and we get right here.
1024 * It may be too late to journal the data. Simply
1025 * falling through to the next test will suffice: the
1026 * data will be dirty and wil be checkpointed. The
1027 * ordering comments in the next comment block still
1030 //J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1033 * If we're journalling data, and this buffer was
1034 * subject to a write(), it could be metadata, forget
1035 * or shadow against the committing transaction. Now,
1036 * someone has dirtied the same darn page via a mapping
1037 * and it is being writepage()'d.
1038 * We *could* just steal the page from commit, with some
1039 * fancy locking there. Instead, we just skip it -
1040 * don't tie the page's buffers to the new transaction
1042 * Implication: if we crash before the writepage() data
1043 * is written into the filesystem, recovery will replay
1046 if (jh->b_jlist != BJ_None &&
1047 jh->b_jlist != BJ_SyncData &&
1048 jh->b_jlist != BJ_AsyncData) {
1049 JBUFFER_TRACE(jh, "Not stealing");
1054 * This buffer may be undergoing writeout in commit. We
1055 * can't return from here and let the caller dirty it
1056 * again because that can cause the write-out loop in
1057 * commit to never terminate.
1059 if (!async && buffer_dirty(bh)) {
1060 atomic_inc(&bh->b_count);
1061 spin_unlock(&journal_datalist_lock);
1063 ll_rw_block(WRITE, 1, &bh);
1065 spin_lock(&journal_datalist_lock);
1066 /* The buffer may become locked again at any
1067 time if it is redirtied */
1070 /* journal_clean_data_list() may have got there first */
1071 if (jh->b_transaction != NULL) {
1072 JBUFFER_TRACE(jh, "unfile from commit");
1073 __journal_unfile_buffer(jh);
1074 jh->b_transaction = NULL;
1076 /* The buffer will be refiled below */
1080 * Special case --- the buffer might actually have been
1081 * allocated and then immediately deallocated in the previous,
1082 * committing transaction, so might still be left on that
1083 * transaction's metadata lists.
1085 if (jh->b_jlist != wanted_jlist) {
1086 JBUFFER_TRACE(jh, "not on correct data list: unfile");
1087 J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
1088 __journal_unfile_buffer(jh);
1089 jh->b_transaction = NULL;
1090 JBUFFER_TRACE(jh, "file as data");
1091 __journal_file_buffer(jh, handle->h_transaction,
1095 JBUFFER_TRACE(jh, "not on a transaction");
1096 __journal_file_buffer(jh, handle->h_transaction, wanted_jlist);
1099 spin_unlock(&journal_datalist_lock);
1101 BUFFER_TRACE(bh, "brelse");
1104 JBUFFER_TRACE(jh, "exit");
1105 journal_unlock_journal_head(jh);
1110 * int journal_dirty_metadata() - mark a buffer as containing dirty metadata
1111 * @handle: transaction to add buffer to.
1112 * @bh: buffer to mark
1114 * mark dirty metadata which needs to be journaled as part of the current transaction.
1116 * The buffer is placed on the transaction's metadata list and is marked
1117 * as belonging to the transaction.
1119 * Returns error number or 0 on success.
1121 int journal_dirty_metadata (handle_t *handle, struct buffer_head *bh)
1124 * Special care needs to be taken if the buffer already belongs to the
1125 * current committing transaction (in which case we should have frozen
1126 * data present for that commit). In that case, we don't relink the
1127 * buffer: that only gets done when the old transaction finally
1128 * completes its commit.
1131 transaction_t *transaction = handle->h_transaction;
1132 journal_t *journal = transaction->t_journal;
1133 struct journal_head *jh = bh2jh(bh);
1135 jbd_debug(5, "journal_head %p\n", jh);
1136 JBUFFER_TRACE(jh, "entry");
1137 lock_journal(journal);
1138 if (is_handle_aborted(handle))
1141 spin_lock(&journal_datalist_lock);
1142 set_bit(BH_JBDDirty, &bh->b_state);
1144 J_ASSERT_JH(jh, jh->b_transaction != NULL);
1147 * Metadata already on the current transaction list doesn't
1148 * need to be filed. Metadata on another transaction's list must
1149 * be committing, and will be refiled once the commit completes:
1150 * leave it alone for now.
1153 if (jh->b_transaction != transaction) {
1154 JBUFFER_TRACE(jh, "already on other transaction");
1155 J_ASSERT_JH(jh, jh->b_transaction ==
1156 journal->j_committing_transaction);
1157 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1158 /* And this case is illegal: we can't reuse another
1159 * transaction's data buffer, ever. */
1160 /* FIXME: writepage() should be journalled */
1161 J_ASSERT_JH(jh, jh->b_jlist != BJ_SyncData);
1165 /* That test should have eliminated the following case: */
1166 J_ASSERT_JH(jh, jh->b_frozen_data == 0);
1168 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1169 __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1172 spin_unlock(&journal_datalist_lock);
1173 JBUFFER_TRACE(jh, "exit");
1175 unlock_journal(journal);
1181 * journal_release_buffer: undo a get_write_access without any buffer
1182 * updates, if the update decided in the end that it didn't need access.
1184 * journal_get_write_access() can block, so it is quite possible for a
1185 * journaling component to decide after the write access is returned
1186 * that global state has changed and the update is no longer required. */
1188 void journal_release_buffer (handle_t *handle, struct buffer_head *bh)
1190 transaction_t *transaction = handle->h_transaction;
1191 journal_t *journal = transaction->t_journal;
1192 struct journal_head *jh = bh2jh(bh);
1194 lock_journal(journal);
1195 JBUFFER_TRACE(jh, "entry");
1197 /* If the buffer is reserved but not modified by this
1198 * transaction, then it is safe to release it. In all other
1199 * cases, just leave the buffer as it is. */
1201 spin_lock(&journal_datalist_lock);
1202 if (jh->b_jlist == BJ_Reserved && jh->b_transaction == transaction &&
1203 !buffer_jdirty(jh2bh(jh))) {
1204 JBUFFER_TRACE(jh, "unused: refiling it");
1205 handle->h_buffer_credits++;
1206 __journal_refile_buffer(jh);
1208 spin_unlock(&journal_datalist_lock);
1210 JBUFFER_TRACE(jh, "exit");
1211 unlock_journal(journal);
1216 * void journal_forget() - bforget() for potentially-journaled buffers.
1217 * @handle: transaction handle
1218 * @bh: bh to 'forget'
1220 * We can only do the bforget if there are no commits pending against the
1221 * buffer. If the buffer is dirty in the current running transaction we
1222 * can safely unlink it.
1224 * bh may not be a journalled buffer at all - it may be a non-JBD
1225 * buffer which came off the hashtable. Check for this.
1227 * Decrements bh->b_count by one.
1229 * Allow this call even if the handle has aborted --- it may be part of
1230 * the caller's cleanup after an abort.
1232 void journal_forget (handle_t *handle, struct buffer_head *bh)
1234 transaction_t *transaction = handle->h_transaction;
1235 journal_t *journal = transaction->t_journal;
1236 struct journal_head *jh;
1238 BUFFER_TRACE(bh, "entry");
1240 lock_journal(journal);
1241 spin_lock(&journal_datalist_lock);
1243 if (!buffer_jbd(bh))
1247 if (jh->b_transaction == handle->h_transaction) {
1248 J_ASSERT_JH(jh, !jh->b_frozen_data);
1250 /* If we are forgetting a buffer which is already part
1251 * of this transaction, then we can just drop it from
1252 * the transaction immediately. */
1253 clear_bit(BH_Dirty, &bh->b_state);
1254 clear_bit(BH_JBDDirty, &bh->b_state);
1256 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1257 J_ASSERT_JH(jh, !jh->b_committed_data);
1259 __journal_unfile_buffer(jh);
1260 jh->b_transaction = 0;
1263 * We are no longer going to journal this buffer.
1264 * However, the commit of this transaction is still
1265 * important to the buffer: the delete that we are now
1266 * processing might obsolete an old log entry, so by
1267 * committing, we can satisfy the buffer's checkpoint.
1269 * So, if we have a checkpoint on the buffer, we should
1270 * now refile the buffer on our BJ_Forget list so that
1271 * we know to remove the checkpoint after we commit.
1274 if (jh->b_cp_transaction) {
1275 __journal_file_buffer(jh, transaction, BJ_Forget);
1277 __journal_remove_journal_head(bh);
1279 if (!buffer_jbd(bh)) {
1280 spin_unlock(&journal_datalist_lock);
1281 unlock_journal(journal);
1287 } else if (jh->b_transaction) {
1288 J_ASSERT_JH(jh, (jh->b_transaction ==
1289 journal->j_committing_transaction));
1290 /* However, if the buffer is still owned by a prior
1291 * (committing) transaction, we can't drop it yet... */
1292 JBUFFER_TRACE(jh, "belongs to older transaction");
1293 /* ... but we CAN drop it from the new transaction if we
1294 * have also modified it since the original commit. */
1296 if (jh->b_next_transaction) {
1297 J_ASSERT(jh->b_next_transaction == transaction);
1298 jh->b_next_transaction = NULL;
1303 spin_unlock(&journal_datalist_lock);
1304 unlock_journal(journal);
1311 * journal_sync_buffer: flush a potentially-journaled buffer to disk.
1313 * Used for O_SYNC filesystem operations. If the buffer is journaled,
1314 * we need to complete the O_SYNC by waiting for the transaction to
1315 * complete. It is an error to call journal_sync_buffer before
1319 void journal_sync_buffer(struct buffer_head *bh)
1321 transaction_t *transaction;
1324 struct journal_head *jh;
1326 /* If the buffer isn't journaled, this is easy: just sync it to
1328 BUFFER_TRACE(bh, "entry");
1330 spin_lock(&journal_datalist_lock);
1331 if (!buffer_jbd(bh)) {
1332 spin_unlock(&journal_datalist_lock);
1336 if (jh->b_transaction == NULL) {
1337 /* If the buffer has already been journaled, then this
1339 if (jh->b_cp_transaction == NULL) {
1340 spin_unlock(&journal_datalist_lock);
1343 atomic_inc(&bh->b_count);
1344 spin_unlock(&journal_datalist_lock);
1345 ll_rw_block (WRITE, 1, &bh);
1351 /* Otherwise, just wait until the transaction is synced to disk. */
1352 transaction = jh->b_transaction;
1353 journal = transaction->t_journal;
1354 sequence = transaction->t_tid;
1355 spin_unlock(&journal_datalist_lock);
1357 jbd_debug(2, "requesting commit for jh %p\n", jh);
1358 log_start_commit (journal, transaction);
1360 while (tid_gt(sequence, journal->j_commit_sequence)) {
1361 wake_up(&journal->j_wait_done_commit);
1362 sleep_on(&journal->j_wait_done_commit);
1364 JBUFFER_TRACE(jh, "exit");
1371 * Register a callback function for this handle. The function will be
1372 * called when the transaction that this handle is part of has been
1373 * committed to disk with the original callback data struct and the
1374 * error status of the journal as parameters. There is no guarantee of
1375 * ordering between handles within a single transaction, nor between
1376 * callbacks registered on the same handle.
1378 * The caller is responsible for allocating the journal_callback struct.
1379 * This is to allow the caller to add as much extra data to the callback
1380 * as needed, but reduce the overhead of multiple allocations. The caller
1381 * allocated struct must start with a struct journal_callback at offset 0,
1382 * and has the caller-specific data afterwards.
1384 void journal_callback_set(handle_t *handle,
1385 void (*func)(struct journal_callback *jcb, int error),
1386 struct journal_callback *jcb)
1388 list_add_tail(&jcb->jcb_list, &handle->h_jcb);
1389 jcb->jcb_func = func;
1393 * int journal_stop() - complete a transaction
1394 * @handle: tranaction to complete.
1396 * All done for a particular handle.
1398 * There is not much action needed here. We just return any remaining
1399 * buffer credits to the transaction and remove the handle. The only
1400 * complication is that we need to start a commit operation if the
1401 * filesystem is marked for synchronous update.
1403 * journal_stop itself will not usually return an error, but it may
1404 * do so in unusual circumstances. In particular, expect it to
1405 * return -EIO if a journal_abort has been executed since the
1406 * transaction began.
1408 int journal_stop(handle_t *handle)
1410 transaction_t *transaction = handle->h_transaction;
1411 journal_t *journal = transaction->t_journal;
1412 int old_handle_count, err;
1417 J_ASSERT (transaction->t_updates > 0);
1418 J_ASSERT (journal_current_handle() == handle);
1420 if (is_handle_aborted(handle))
1425 if (--handle->h_ref > 0) {
1426 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1431 jbd_debug(4, "Handle %p going down\n", handle);
1434 * Implement synchronous transaction batching. If the handle
1435 * was synchronous, don't force a commit immediately. Let's
1436 * yield and let another thread piggyback onto this transaction.
1437 * Keep doing that while new threads continue to arrive.
1438 * It doesn't cost much - we're about to run a commit and sleep
1439 * on IO anyway. Speeds up many-threaded, many-dir operations
1442 if (handle->h_sync) {
1444 old_handle_count = transaction->t_handle_count;
1446 } while (old_handle_count != transaction->t_handle_count);
1449 current->journal_info = NULL;
1450 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1451 transaction->t_updates--;
1452 if (!transaction->t_updates) {
1453 wake_up(&journal->j_wait_updates);
1454 if (journal->j_barrier_count)
1455 wake_up(&journal->j_wait_transaction_locked);
1458 /* Move callbacks from the handle to the transaction. */
1459 list_splice(&handle->h_jcb, &transaction->t_jcb);
1462 * If the handle is marked SYNC, we need to set another commit
1463 * going! We also want to force a commit if the current
1464 * transaction is occupying too much of the log, or if the
1465 * transaction is too old now.
1467 if (handle->h_sync ||
1468 transaction->t_outstanding_credits >
1469 journal->j_max_transaction_buffers ||
1470 (journal->j_commit_interval &&
1471 time_after_eq(jiffies, transaction->t_expires))) {
1472 /* Do this even for aborted journals: an abort still
1473 * completes the commit thread, it just doesn't write
1474 * anything to disk. */
1475 tid_t tid = transaction->t_tid;
1477 jbd_debug(2, "transaction too old, requesting commit for "
1478 "handle %p\n", handle);
1479 /* This is non-blocking */
1480 log_start_commit(journal, transaction);
1483 * Special case: JFS_SYNC synchronous updates require us
1484 * to wait for the commit to complete.
1486 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1487 log_wait_commit(journal, tid);
1493 /**int journal_force_commit() - force any uncommitted transactions
1494 * @journal: journal to force
1496 * For synchronous operations: force any uncommitted transactions
1497 * to disk. May seem kludgy, but it reuses all the handle batching
1498 * code in a very simple manner.
1500 int journal_force_commit(journal_t *journal)
1506 handle = journal_start(journal, 1);
1507 if (IS_ERR(handle)) {
1508 ret = PTR_ERR(handle);
1512 journal_stop(handle);
1520 * List management code snippets: various functions for manipulating the
1521 * transaction buffer lists.
1526 * Append a buffer to a transaction list, given the transaction's list head
1528 * journal_datalist_lock is held.
1532 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1535 jh->b_tnext = jh->b_tprev = jh;
1538 /* Insert at the tail of the list to preserve order */
1539 struct journal_head *first = *list, *last = first->b_tprev;
1541 jh->b_tnext = first;
1542 last->b_tnext = first->b_tprev = jh;
1547 * Remove a buffer from a transaction list, given the transaction's list
1550 * Called with journal_datalist_lock held, and the journal may not
1555 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1558 *list = jh->b_tnext;
1562 jh->b_tprev->b_tnext = jh->b_tnext;
1563 jh->b_tnext->b_tprev = jh->b_tprev;
1567 * Remove a buffer from the appropriate transaction list.
1569 * Note that this function can *change* the value of
1570 * bh->b_transaction->t_sync_datalist, t_async_datalist, t_buffers, t_forget,
1571 * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
1572 * is holding onto a copy of one of thee pointers, it could go bad.
1573 * Generally the caller needs to re-read the pointer from the transaction_t.
1575 * If bh->b_jlist is BJ_SyncData or BJ_AsyncData then we may have been called
1576 * via journal_try_to_free_buffer() or journal_clean_data_list(). In that
1577 * case, journal_datalist_lock will be held, and the journal may not be locked.
1579 void __journal_unfile_buffer(struct journal_head *jh)
1581 struct journal_head **list = 0;
1582 transaction_t * transaction;
1584 assert_spin_locked(&journal_datalist_lock);
1585 transaction = jh->b_transaction;
1587 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1589 if (jh->b_jlist != BJ_None)
1590 J_ASSERT_JH(jh, transaction != 0);
1592 switch (jh->b_jlist) {
1596 list = &transaction->t_sync_datalist;
1599 list = &transaction->t_async_datalist;
1602 transaction->t_nr_buffers--;
1603 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1604 list = &transaction->t_buffers;
1607 list = &transaction->t_forget;
1610 list = &transaction->t_iobuf_list;
1613 list = &transaction->t_shadow_list;
1616 list = &transaction->t_log_list;
1619 list = &transaction->t_reserved_list;
1623 __blist_del_buffer(list, jh);
1624 jh->b_jlist = BJ_None;
1625 if (test_and_clear_bit(BH_JBDDirty, &jh2bh(jh)->b_state)) {
1626 set_bit(BH_Dirty, &jh2bh(jh)->b_state);
1630 void journal_unfile_buffer(struct journal_head *jh)
1632 spin_lock(&journal_datalist_lock);
1633 __journal_unfile_buffer(jh);
1634 spin_unlock(&journal_datalist_lock);
1638 * Called from journal_try_to_free_buffers(). The journal is not
1639 * locked. lru_list_lock is not held.
1641 * Here we see why journal_datalist_lock is global and not per-journal.
1642 * We cannot get back to this buffer's journal pointer without locking
1643 * out journal_clean_data_list() in some manner.
1645 * One could use journal_datalist_lock to get unracy access to a
1648 * Called with journal_datalist_lock held.
1650 * Returns non-zero iff we were able to free the journal_head.
1652 static int __journal_try_to_free_buffer(struct buffer_head *bh,
1653 int *locked_or_dirty)
1655 struct journal_head *jh;
1657 assert_spin_locked(&journal_datalist_lock);
1661 if (buffer_locked(bh) || buffer_dirty(bh)) {
1662 *locked_or_dirty = 1;
1666 if (!buffer_uptodate(bh))
1669 if (jh->b_next_transaction != 0)
1672 if (jh->b_transaction != 0 && jh->b_cp_transaction == 0) {
1673 if (jh->b_jlist == BJ_SyncData || jh->b_jlist==BJ_AsyncData) {
1674 /* A written-back ordered data buffer */
1675 JBUFFER_TRACE(jh, "release data");
1676 __journal_unfile_buffer(jh);
1677 jh->b_transaction = 0;
1678 __journal_remove_journal_head(bh);
1682 else if (jh->b_cp_transaction != 0 && jh->b_transaction == 0) {
1683 /* written-back checkpointed metadata buffer */
1684 if (jh->b_jlist == BJ_None) {
1685 JBUFFER_TRACE(jh, "remove from checkpoint list");
1686 __journal_remove_checkpoint(jh);
1687 __journal_remove_journal_head(bh);
1691 return !buffer_jbd(bh);
1697 void debug_page(struct page *p)
1699 struct buffer_head *bh;
1703 printk(KERN_ERR "%s: page index:%lu count:%d flags:%lx\n", __FUNCTION__,
1704 p->index, atomic_read(&p->count), p->flags);
1707 printk(KERN_ERR "%s: bh b_next:%p blocknr:%lu b_list:%u state:%lx\n",
1708 __FUNCTION__, bh->b_next, bh->b_blocknr, bh->b_list,
1710 bh = bh->b_this_page;
1716 * int journal_try_to_free_buffers() - try to free page buffers.
1717 * @journal: journal for operation
1718 * @page: to try and free
1719 * @gfp_mask: 'IO' mode for try_to_free_buffers()
1722 * For all the buffers on this page,
1723 * if they are fully written out ordered data, move them onto BUF_CLEAN
1724 * so try_to_free_buffers() can reap them.
1726 * This function returns non-zero if we wish try_to_free_buffers()
1727 * to be called. We do this if the page is releasable by try_to_free_buffers().
1728 * We also do it if the page has locked or dirty buffers and the caller wants
1729 * us to perform sync or async writeout.
1731 int journal_try_to_free_buffers(journal_t *journal,
1732 struct page *page, int gfp_mask)
1735 * journal_try_to_free_buffers(). For all the buffers on this page,
1736 * if they are fully written out ordered data, move them onto BUF_CLEAN
1737 * so try_to_free_buffers() can reap them. Called with lru_list_lock
1738 * not held. Does its own locking.
1740 * This complicates JBD locking somewhat. We aren't protected by the
1741 * BKL here. We wish to remove the buffer from its committing or
1742 * running transaction's ->t_datalist via __journal_unfile_buffer.
1744 * This may *change* the value of transaction_t->t_datalist, so anyone
1745 * who looks at t_datalist needs to lock against this function.
1747 * Even worse, someone may be doing a journal_dirty_data on this
1748 * buffer. So we need to lock against that. journal_dirty_data()
1749 * will come out of the lock with the buffer dirty, which makes it
1750 * ineligible for release here.
1752 * Who else is affected by this? hmm... Really the only contender
1753 * is do_get_write_access() - it could be looking at the buffer while
1754 * journal_try_to_free_buffer() is changing its state. But that
1755 * cannot happen because we never reallocate freed data as metadata
1756 * while the data is part of a transaction. Yes?
1759 struct buffer_head *bh;
1760 struct buffer_head *tmp;
1761 int locked_or_dirty = 0;
1764 J_ASSERT(PageLocked(page));
1768 spin_lock(&journal_datalist_lock);
1770 struct buffer_head *p = tmp;
1772 if (unlikely(!tmp)) {
1777 tmp = tmp->b_this_page;
1779 if (!__journal_try_to_free_buffer(p, &locked_or_dirty))
1781 } while (tmp != bh);
1782 spin_unlock(&journal_datalist_lock);
1784 if (!(gfp_mask & (__GFP_IO|__GFP_WAIT)))
1786 if (!locked_or_dirty)
1789 * The VM wants us to do writeout, or to block on IO, or both.
1790 * So we allow try_to_free_buffers to be called even if the page
1791 * still has journalled buffers.
1799 * This buffer is no longer needed. If it is on an older transaction's
1800 * checkpoint list we need to record it on this transaction's forget list
1801 * to pin this buffer (and hence its checkpointing transaction) down until
1802 * this transaction commits. If the buffer isn't on a checkpoint list, we
1804 * Returns non-zero if JBD no longer has an interest in the buffer.
1806 static int dispose_buffer(struct journal_head *jh,
1807 transaction_t *transaction)
1810 struct buffer_head *bh = jh2bh(jh);
1812 spin_lock(&journal_datalist_lock);
1813 __journal_unfile_buffer(jh);
1814 jh->b_transaction = 0;
1816 if (jh->b_cp_transaction) {
1817 JBUFFER_TRACE(jh, "on running+cp transaction");
1818 __journal_file_buffer(jh, transaction, BJ_Forget);
1819 clear_bit(BH_JBDDirty, &bh->b_state);
1822 JBUFFER_TRACE(jh, "on running transaction");
1823 __journal_remove_journal_head(bh);
1826 spin_unlock(&journal_datalist_lock);
1833 * This code is tricky. It has a number of cases to deal with.
1835 * There are two invariants which this code relies on:
1837 * i_size must be updated on disk before we start calling flushpage on the
1840 * This is done in ext3 by defining an ext3_setattr method which
1841 * updates i_size before truncate gets going. By maintaining this
1842 * invariant, we can be sure that it is safe to throw away any buffers
1843 * attached to the current transaction: once the transaction commits,
1844 * we know that the data will not be needed.
1846 * Note however that we can *not* throw away data belonging to the
1847 * previous, committing transaction!
1849 * Any disk blocks which *are* part of the previous, committing
1850 * transaction (and which therefore cannot be discarded immediately) are
1851 * not going to be reused in the new running transaction
1853 * The bitmap committed_data images guarantee this: any block which is
1854 * allocated in one transaction and removed in the next will be marked
1855 * as in-use in the committed_data bitmap, so cannot be reused until
1856 * the next transaction to delete the block commits. This means that
1857 * leaving committing buffers dirty is quite safe: the disk blocks
1858 * cannot be reallocated to a different file and so buffer aliasing is
1862 * The above applies mainly to ordered data mode. In writeback mode we
1863 * don't make guarantees about the order in which data hits disk --- in
1864 * particular we don't guarantee that new dirty data is flushed before
1865 * transaction commit --- so it is always safe just to discard data
1866 * immediately in that mode. --sct
1870 * The journal_unmap_buffer helper function returns zero if the buffer
1871 * concerned remains pinned as an anonymous buffer belonging to an older
1874 * We're outside-transaction here. Either or both of j_running_transaction
1875 * and j_committing_transaction may be NULL.
1877 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1879 transaction_t *transaction;
1880 struct journal_head *jh;
1883 BUFFER_TRACE(bh, "entry");
1885 if (!buffer_mapped(bh))
1888 /* It is safe to proceed here without the
1889 * journal_datalist_spinlock because the buffers cannot be
1890 * stolen by try_to_free_buffers as long as we are holding the
1891 * page lock. --sct */
1893 if (!buffer_jbd(bh))
1897 transaction = jh->b_transaction;
1898 if (transaction == NULL) {
1899 /* First case: not on any transaction. If it
1900 * has no checkpoint link, then we can zap it:
1901 * it's a writeback-mode buffer so we don't care
1902 * if it hits disk safely. */
1903 if (!jh->b_cp_transaction) {
1904 JBUFFER_TRACE(jh, "not on any transaction: zap");
1908 if (!buffer_dirty(bh)) {
1909 /* bdflush has written it. We can drop it now */
1913 /* OK, it must be in the journal but still not
1914 * written fully to disk: it's metadata or
1915 * journaled data... */
1917 if (journal->j_running_transaction) {
1918 /* ... and once the current transaction has
1919 * committed, the buffer won't be needed any
1921 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1922 return dispose_buffer(jh,
1923 journal->j_running_transaction);
1925 /* There is no currently-running transaction. So the
1926 * orphan record which we wrote for this file must have
1927 * passed into commit. We must attach this buffer to
1928 * the committing transaction, if it exists. */
1929 if (journal->j_committing_transaction) {
1930 JBUFFER_TRACE(jh, "give to committing trans");
1931 return dispose_buffer(jh,
1932 journal->j_committing_transaction);
1934 /* The orphan record's transaction has
1935 * committed. We can cleanse this buffer */
1936 clear_bit(BH_JBDDirty, &bh->b_state);
1940 } else if (transaction == journal->j_committing_transaction) {
1941 /* If it is committing, we simply cannot touch it. We
1942 * can remove it's next_transaction pointer from the
1943 * running transaction if that is set, but nothing
1945 JBUFFER_TRACE(jh, "on committing transaction");
1946 set_bit(BH_Freed, &bh->b_state);
1947 if (jh->b_next_transaction) {
1948 J_ASSERT(jh->b_next_transaction ==
1949 journal->j_running_transaction);
1950 jh->b_next_transaction = NULL;
1954 /* Good, the buffer belongs to the running transaction.
1955 * We are writing our own transaction's data, not any
1956 * previous one's, so it is safe to throw it away
1957 * (remember that we expect the filesystem to have set
1958 * i_size already for this truncate so recovery will not
1959 * expose the disk blocks we are discarding here.) */
1960 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1961 may_free = dispose_buffer(jh, transaction);
1965 if (buffer_dirty(bh))
1966 mark_buffer_clean(bh);
1967 J_ASSERT_BH(bh, !buffer_jdirty(bh));
1968 clear_bit(BH_Uptodate, &bh->b_state);
1969 clear_bit(BH_Mapped, &bh->b_state);
1970 clear_bit(BH_Req, &bh->b_state);
1971 clear_bit(BH_New, &bh->b_state);
1976 * int journal_flushpage()
1977 * @journal: journal to use for flush...
1978 * @page: page to flush
1979 * @offset: length of page to flush.
1981 * Reap page buffers containing data after offset in page.
1983 * Return non-zero if the page's buffers were successfully reaped.
1985 int journal_flushpage(journal_t *journal,
1987 unsigned long offset)
1989 struct buffer_head *head, *bh, *next;
1990 unsigned int curr_off = 0;
1993 if (!PageLocked(page))
1998 /* We will potentially be playing with lists other than just the
1999 * data lists (especially for journaled data mode), so be
2000 * cautious in our locking. */
2001 lock_journal(journal);
2003 head = bh = page->buffers;
2005 unsigned int next_off = curr_off + bh->b_size;
2006 next = bh->b_this_page;
2008 /* AKPM: doing lock_buffer here may be overly paranoid */
2009 if (offset <= curr_off) {
2010 /* This block is wholly outside the truncation point */
2012 may_free &= journal_unmap_buffer(journal, bh);
2015 curr_off = next_off;
2018 } while (bh != head);
2020 unlock_journal(journal);
2023 if (!may_free || !try_to_free_buffers(page, 0))
2025 J_ASSERT(page->buffers == NULL);
2031 * File a buffer on the given transaction list.
2033 void __journal_file_buffer(struct journal_head *jh,
2034 transaction_t *transaction, int jlist)
2036 struct journal_head **list = 0;
2039 assert_spin_locked(&journal_datalist_lock);
2041 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2042 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2043 jh->b_transaction == 0);
2045 if (jh->b_transaction && jh->b_jlist == jlist)
2048 /* The following list of buffer states needs to be consistent
2049 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
2052 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2053 jlist == BJ_Shadow || jlist == BJ_Forget) {
2054 if (atomic_set_buffer_clean(jh2bh(jh)) ||
2055 test_and_clear_bit(BH_JBDDirty, &jh2bh(jh)->b_state))
2059 if (jh->b_transaction)
2060 __journal_unfile_buffer(jh);
2062 jh->b_transaction = transaction;
2066 J_ASSERT_JH(jh, !jh->b_committed_data);
2067 J_ASSERT_JH(jh, !jh->b_frozen_data);
2070 list = &transaction->t_sync_datalist;
2073 list = &transaction->t_async_datalist;
2076 transaction->t_nr_buffers++;
2077 list = &transaction->t_buffers;
2080 list = &transaction->t_forget;
2083 list = &transaction->t_iobuf_list;
2086 list = &transaction->t_shadow_list;
2089 list = &transaction->t_log_list;
2092 list = &transaction->t_reserved_list;
2096 __blist_add_buffer(list, jh);
2097 jh->b_jlist = jlist;
2100 set_bit(BH_JBDDirty, &jh2bh(jh)->b_state);
2103 void journal_file_buffer(struct journal_head *jh,
2104 transaction_t *transaction, int jlist)
2106 spin_lock(&journal_datalist_lock);
2107 __journal_file_buffer(jh, transaction, jlist);
2108 spin_unlock(&journal_datalist_lock);
2111 static void jbd_refile_buffer(struct buffer_head *bh)
2113 if (buffer_dirty(bh) && (bh->b_list != BUF_DIRTY))
2114 set_buffer_flushtime(bh);
2119 * Remove a buffer from its current buffer list in preparation for
2120 * dropping it from its current transaction entirely. If the buffer has
2121 * already started to be used by a subsequent transaction, refile the
2122 * buffer on that transaction's metadata list.
2125 void __journal_refile_buffer(struct journal_head *jh)
2129 assert_spin_locked(&journal_datalist_lock);
2130 /* If the buffer is now unused, just drop it. */
2131 if (jh->b_next_transaction == NULL) {
2132 __journal_unfile_buffer(jh);
2133 jh->b_transaction = NULL;
2134 /* Onto BUF_DIRTY for writeback */
2135 jbd_refile_buffer(jh2bh(jh));
2139 /* It has been modified by a later transaction: add it to the
2140 * new transaction's metadata list. */
2142 if (test_and_clear_bit(BH_JBDDirty, &jh2bh(jh)->b_state))
2145 __journal_unfile_buffer(jh);
2146 jh->b_transaction = jh->b_next_transaction;
2147 jh->b_next_transaction = NULL;
2148 __journal_file_buffer(jh, jh->b_transaction, BJ_Metadata);
2149 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2152 set_bit(BH_JBDDirty, &jh2bh(jh)->b_state);
2157 * For the unlocked version of this call, also make sure that any
2158 * hanging journal_head is cleaned up if necessary.
2160 * __journal_refile_buffer is usually called as part of a single locked
2161 * operation on a buffer_head, in which the caller is probably going to
2162 * be hooking the journal_head onto other lists. In that case it is up
2163 * to the caller to remove the journal_head if necessary. For the
2164 * unlocked journal_refile_buffer call, the caller isn't going to be
2165 * doing anything else to the buffer so we need to do the cleanup
2166 * ourselves to avoid a jh leak.
2168 * *** The journal_head may be freed by this call! ***
2170 void journal_refile_buffer(struct journal_head *jh)
2172 struct buffer_head *bh;
2174 spin_lock(&journal_datalist_lock);
2177 __journal_refile_buffer(jh);
2178 __journal_remove_journal_head(bh);
2180 spin_unlock(&journal_datalist_lock);