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 /* Set up the commit timer for the new transaction. */
63 J_ASSERT (!journal->j_commit_timer_active);
64 journal->j_commit_timer_active = 1;
65 journal->j_commit_timer->expires = transaction->t_expires;
66 add_timer(journal->j_commit_timer);
68 J_ASSERT (journal->j_running_transaction == NULL);
69 journal->j_running_transaction = transaction;
77 * A handle_t is an object which represents a single atomic update to a
78 * filesystem, and which tracks all of the modifications which form part
83 * start_this_handle: Given a handle, deal with any locking or stalling
84 * needed to make sure that there is enough journal space for the handle
85 * to begin. Attach the handle to a transaction and set up the
86 * transaction's buffer credits.
89 static int start_this_handle(journal_t *journal, handle_t *handle)
91 transaction_t *transaction;
93 int nblocks = handle->h_buffer_credits;
95 if (nblocks > journal->j_max_transaction_buffers) {
96 jbd_debug(1, "JBD: %s wants too many credits (%d > %d)\n",
97 current->comm, nblocks,
98 journal->j_max_transaction_buffers);
102 jbd_debug(3, "New handle %p going live.\n", handle);
106 lock_journal(journal);
110 if (is_journal_aborted(journal) ||
111 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
112 unlock_journal(journal);
116 /* Wait on the journal's transaction barrier if necessary */
117 if (journal->j_barrier_count) {
118 unlock_journal(journal);
119 sleep_on(&journal->j_wait_transaction_locked);
123 if (!journal->j_running_transaction)
124 get_transaction(journal, 0);
126 J_ASSERT(journal->j_running_transaction);
128 transaction = journal->j_running_transaction;
130 /* If the current transaction is locked down for commit, wait
131 * for the lock to be released. */
133 if (transaction->t_state == T_LOCKED) {
134 unlock_journal(journal);
135 jbd_debug(3, "Handle %p stalling...\n", handle);
136 sleep_on(&journal->j_wait_transaction_locked);
140 /* If there is not enough space left in the log to write all
141 * potential buffers requested by this operation, we need to
142 * stall pending a log checkpoint to free some more log
145 needed = transaction->t_outstanding_credits + nblocks;
147 if (needed > journal->j_max_transaction_buffers) {
148 /* If the current transaction is already too large, then
149 * start to commit it: we can then go back and attach
150 * this handle to a new transaction. */
152 jbd_debug(2, "Handle %p starting new commit...\n", handle);
153 log_start_commit(journal, transaction);
154 unlock_journal(journal);
155 sleep_on(&journal->j_wait_transaction_locked);
156 lock_journal(journal);
161 * The commit code assumes that it can get enough log space
162 * without forcing a checkpoint. This is *critical* for
163 * correctness: a checkpoint of a buffer which is also
164 * associated with a committing transaction creates a deadlock,
165 * so commit simply cannot force through checkpoints.
167 * We must therefore ensure the necessary space in the journal
168 * *before* starting to dirty potentially checkpointed buffers
169 * in the new transaction.
171 * The worst part is, any transaction currently committing can
172 * reduce the free space arbitrarily. Be careful to account for
173 * those buffers when checkpointing.
177 * @@@ AKPM: This seems rather over-defensive. We're giving commit
178 * a _lot_ of headroom: 1/4 of the journal plus the size of
179 * the committing transaction. Really, we only need to give it
180 * committing_transaction->t_outstanding_credits plus "enough" for
181 * the log control blocks.
182 * Also, this test is inconsitent with the matching one in
185 needed = journal->j_max_transaction_buffers;
186 if (journal->j_committing_transaction)
187 needed += journal->j_committing_transaction->
188 t_outstanding_credits;
190 if (log_space_left(journal) < needed) {
191 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
192 log_wait_for_space(journal, needed);
196 /* OK, account for the buffers that this operation expects to
197 * use and add the handle to the running transaction. */
199 handle->h_transaction = transaction;
200 transaction->t_outstanding_credits += nblocks;
201 transaction->t_updates++;
202 transaction->t_handle_count++;
203 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
204 handle, nblocks, transaction->t_outstanding_credits,
205 log_space_left(journal));
207 unlock_journal(journal);
212 /* Allocate a new handle. This should probably be in a slab... */
213 static handle_t *new_handle(int nblocks)
215 handle_t *handle = jbd_kmalloc(sizeof (handle_t), GFP_NOFS);
218 memset(handle, 0, sizeof (handle_t));
219 handle->h_buffer_credits = nblocks;
221 INIT_LIST_HEAD(&handle->h_jcb);
227 * handle_t *journal_start() - Obtain a new handle.
228 * @journal: Journal to start transaction on.
229 * @nblocks: number of block buffer we might modify
231 * We make sure that the transaction can guarantee at least nblocks of
232 * modified buffers in the log. We block until the log can guarantee
235 * This function is visible to journal users (like ext3fs), so is not
236 * called with the journal already locked.
238 * Return a pointer to a newly allocated handle, or NULL on failure
240 handle_t *journal_start(journal_t *journal, int nblocks)
242 handle_t *handle = journal_current_handle();
246 return ERR_PTR(-EROFS);
249 J_ASSERT(handle->h_transaction->t_journal == journal);
254 handle = new_handle(nblocks);
256 return ERR_PTR(-ENOMEM);
258 current->journal_info = handle;
260 err = start_this_handle(journal, handle);
263 current->journal_info = NULL;
271 * Return zero on success
273 static int try_start_this_handle(journal_t *journal, handle_t *handle)
275 transaction_t *transaction;
277 int nblocks = handle->h_buffer_credits;
280 jbd_debug(3, "New handle %p maybe going live.\n", handle);
282 lock_journal(journal);
284 if (is_journal_aborted(journal) ||
285 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
290 if (journal->j_barrier_count)
293 if (!journal->j_running_transaction && get_transaction(journal, 1) == 0)
296 transaction = journal->j_running_transaction;
297 if (transaction->t_state == T_LOCKED)
300 needed = transaction->t_outstanding_credits + nblocks;
301 /* We could run log_start_commit here */
302 if (needed > journal->j_max_transaction_buffers)
305 needed = journal->j_max_transaction_buffers;
306 if (journal->j_committing_transaction)
307 needed += journal->j_committing_transaction->
308 t_outstanding_credits;
310 if (log_space_left(journal) < needed)
313 handle->h_transaction = transaction;
314 transaction->t_outstanding_credits += nblocks;
315 transaction->t_updates++;
316 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
317 handle, nblocks, transaction->t_outstanding_credits,
318 log_space_left(journal));
319 unlock_journal(journal);
323 unlock_journal(journal);
330 * handle_t *journal_try_start() - Don't block, but try and get a handle
331 * @journal: Journal to start transaction on.
332 * @nblocks: number of block buffer we might modify
334 * Try to start a handle, but non-blockingly. If we weren't able
335 * to, return an ERR_PTR value.
337 handle_t *journal_try_start(journal_t *journal, int nblocks)
339 handle_t *handle = journal_current_handle();
343 return ERR_PTR(-EROFS);
346 jbd_debug(4, "h_ref %d -> %d\n",
349 J_ASSERT(handle->h_transaction->t_journal == journal);
350 if (is_handle_aborted(handle))
351 return ERR_PTR(-EIO);
355 jbd_debug(4, "no current transaction\n");
358 if (is_journal_aborted(journal))
359 return ERR_PTR(-EIO);
361 handle = new_handle(nblocks);
363 return ERR_PTR(-ENOMEM);
365 current->journal_info = handle;
367 err = try_start_this_handle(journal, handle);
370 current->journal_info = NULL;
378 * int journal_extend() - extend buffer credits.
379 * @handle: handle to 'extend'
380 * @nblocks: nr blocks to try to extend by.
382 * Some transactions, such as large extends and truncates, can be done
383 * atomically all at once or in several stages. The operation requests
384 * a credit for a number of buffer modications in advance, but can
385 * extend its credit if it needs more.
387 * journal_extend tries to give the running handle more buffer credits.
388 * It does not guarantee that allocation - this is a best-effort only.
389 * The calling process MUST be able to deal cleanly with a failure to
392 * Return 0 on success, non-zero on failure.
394 * return code < 0 implies an error
395 * return code > 0 implies normal transaction-full status.
397 int journal_extend (handle_t *handle, int nblocks)
399 transaction_t *transaction = handle->h_transaction;
400 journal_t *journal = transaction->t_journal;
404 lock_journal (journal);
407 if (is_handle_aborted(handle))
412 /* Don't extend a locked-down transaction! */
413 if (handle->h_transaction->t_state != T_RUNNING) {
414 jbd_debug(3, "denied handle %p %d blocks: "
415 "transaction not running\n", handle, nblocks);
419 wanted = transaction->t_outstanding_credits + nblocks;
421 if (wanted > journal->j_max_transaction_buffers) {
422 jbd_debug(3, "denied handle %p %d blocks: "
423 "transaction too large\n", handle, nblocks);
427 if (wanted > log_space_left(journal)) {
428 jbd_debug(3, "denied handle %p %d blocks: "
429 "insufficient log space\n", handle, nblocks);
433 handle->h_buffer_credits += nblocks;
434 transaction->t_outstanding_credits += nblocks;
437 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
440 unlock_journal (journal);
446 * int journal_restart() - restart a handle .
447 * @handle: handle to restart
448 * @nblocks: nr credits requested
450 * Restart a handle for a multi-transaction filesystem
453 * If the journal_extend() call above fails to grant new buffer credits
454 * to a running handle, a call to journal_restart will commit the
455 * handle's transaction so far and reattach the handle to a new
456 * transaction capabable of guaranteeing the requested number of
460 int journal_restart(handle_t *handle, int nblocks)
462 transaction_t *transaction = handle->h_transaction;
463 journal_t *journal = transaction->t_journal;
466 /* If we've had an abort of any type, don't even think about
467 * actually doing the restart! */
468 if (is_handle_aborted(handle))
471 /* First unlink the handle from its current transaction, and
472 * start the commit on that. */
474 J_ASSERT (transaction->t_updates > 0);
475 J_ASSERT (journal_current_handle() == handle);
477 transaction->t_outstanding_credits -= handle->h_buffer_credits;
478 transaction->t_updates--;
480 if (!transaction->t_updates)
481 wake_up(&journal->j_wait_updates);
483 jbd_debug(2, "restarting handle %p\n", handle);
484 log_start_commit(journal, transaction);
486 handle->h_buffer_credits = nblocks;
487 ret = start_this_handle(journal, handle);
493 * void journal_lock_updates () - establish a transaction barrier.
494 * @journal: Journal to establish a barrier on.
496 * This locks out any further updates from being started, and blocks
497 * until all existing updates have completed, returning only once the
498 * journal is in a quiescent state with no updates running.
500 * The journal lock should not be held on entry.
502 void journal_lock_updates (journal_t *journal)
504 lock_journal(journal);
505 ++journal->j_barrier_count;
507 /* Wait until there are no running updates */
509 transaction_t *transaction = journal->j_running_transaction;
512 if (!transaction->t_updates)
515 unlock_journal(journal);
516 sleep_on(&journal->j_wait_updates);
517 lock_journal(journal);
520 unlock_journal(journal);
522 /* We have now established a barrier against other normal
523 * updates, but we also need to barrier against other
524 * journal_lock_updates() calls to make sure that we serialise
525 * special journal-locked operations too. */
526 down(&journal->j_barrier);
530 * void journal_unlock_updates (journal_t* journal) - release barrier
531 * @journal: Journal to release the barrier on.
533 * Release a transaction barrier obtained with journal_lock_updates().
535 * Should be called without the journal lock held.
537 void journal_unlock_updates (journal_t *journal)
539 lock_journal(journal);
541 J_ASSERT (journal->j_barrier_count != 0);
543 up(&journal->j_barrier);
544 --journal->j_barrier_count;
545 wake_up(&journal->j_wait_transaction_locked);
546 unlock_journal(journal);
550 * if the buffer is already part of the current transaction, then there
551 * is nothing we need to do. if it is already part of a prior
552 * transaction which we are still committing to disk, then we need to
553 * make sure that we do not overwrite the old copy: we do copy-out to
554 * preserve the copy going to disk. we also account the buffer against
555 * the handle's metadata buffer credits (unless the buffer is already
556 * part of the transaction, that is).
559 do_get_write_access(handle_t *handle, struct journal_head *jh, int force_copy)
561 struct buffer_head *bh;
562 transaction_t *transaction = handle->h_transaction;
563 journal_t *journal = transaction->t_journal;
565 char *frozen_buffer = NULL;
569 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
571 JBUFFER_TRACE(jh, "entry");
575 /* @@@ Need to check for errors here at some point. */
578 * AKPM: we have replaced all the lock_journal_bh_wait() stuff with a
579 * simple lock_journal(). This code here will care for locked buffers.
581 locked = test_and_set_bit(BH_Lock, &bh->b_state);
583 /* We can't reliably test the buffer state if we found
584 * it already locked, so just wait for the lock and
586 unlock_journal(journal);
587 __wait_on_buffer(bh);
588 lock_journal(journal);
592 /* We now hold the buffer lock so it is safe to query the buffer
593 * state. Is the buffer dirty?
595 * If so, there are two possibilities. The buffer may be
596 * non-journaled, and undergoing a quite legitimate writeback.
597 * Otherwise, it is journaled, and we don't expect dirty buffers
598 * in that state (the buffers should be marked JBD_Dirty
599 * instead.) So either the IO is being done under our own
600 * control and this is a bug, or it's a third party IO such as
601 * dump(8) (which may leave the buffer scheduled for read ---
602 * ie. locked but not dirty) or tune2fs (which may actually have
603 * the buffer dirtied, ugh.) */
605 if (buffer_dirty(bh)) {
606 spin_lock(&journal_datalist_lock);
607 /* First question: is this buffer already part of the
608 * current transaction or the existing committing
610 if (jh->b_transaction) {
611 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
612 jh->b_transaction == journal->j_committing_transaction);
613 if (jh->b_next_transaction)
614 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
615 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
616 jbd_unexpected_dirty_buffer(jh);
618 spin_unlock(&journal_datalist_lock);
624 if (is_handle_aborted(handle))
628 spin_lock(&journal_datalist_lock);
630 /* The buffer is already part of this transaction if
631 * b_transaction or b_next_transaction points to it. */
633 if (jh->b_transaction == transaction ||
634 jh->b_next_transaction == transaction)
637 /* If there is already a copy-out version of this buffer, then
638 * we don't need to make another one. */
640 if (jh->b_frozen_data) {
641 JBUFFER_TRACE(jh, "has frozen data");
642 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
643 jh->b_next_transaction = transaction;
645 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
646 handle->h_buffer_credits--;
650 /* Is there data here we need to preserve? */
652 if (jh->b_transaction && jh->b_transaction != transaction) {
653 JBUFFER_TRACE(jh, "owned by older transaction");
654 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
655 J_ASSERT_JH(jh, jh->b_transaction ==
656 journal->j_committing_transaction);
658 /* There is one case we have to be very careful about.
659 * If the committing transaction is currently writing
660 * this buffer out to disk and has NOT made a copy-out,
661 * then we cannot modify the buffer contents at all
662 * right now. The essence of copy-out is that it is the
663 * extra copy, not the primary copy, which gets
664 * journaled. If the primary copy is already going to
665 * disk then we cannot do copy-out here. */
667 if (jh->b_jlist == BJ_Shadow) {
668 JBUFFER_TRACE(jh, "on shadow: sleep");
669 spin_unlock(&journal_datalist_lock);
670 unlock_journal(journal);
671 /* commit wakes up all shadow buffers after IO */
672 wait_event(jh2bh(jh)->b_wait,
673 jh->b_jlist != BJ_Shadow);
674 lock_journal(journal);
678 /* Only do the copy if the currently-owning transaction
679 * still needs it. If it is on the Forget list, the
680 * committing transaction is past that stage. The
681 * buffer had better remain locked during the kmalloc,
682 * but that should be true --- we hold the journal lock
683 * still and the buffer is already on the BUF_JOURNAL
684 * list so won't be flushed.
686 * Subtle point, though: if this is a get_undo_access,
687 * then we will be relying on the frozen_data to contain
688 * the new value of the committed_data record after the
689 * transaction, so we HAVE to force the frozen_data copy
692 if (jh->b_jlist != BJ_Forget || force_copy) {
693 JBUFFER_TRACE(jh, "generate frozen data");
694 if (!frozen_buffer) {
695 JBUFFER_TRACE(jh, "allocate memory for buffer");
696 spin_unlock(&journal_datalist_lock);
697 unlock_journal(journal);
698 frozen_buffer = jbd_kmalloc(jh2bh(jh)->b_size,
700 lock_journal(journal);
701 if (!frozen_buffer) {
703 "%s: OOM for frozen_buffer\n",
705 JBUFFER_TRACE(jh, "oom!");
707 spin_lock(&journal_datalist_lock);
713 jh->b_frozen_data = frozen_buffer;
714 frozen_buffer = NULL;
717 jh->b_next_transaction = transaction;
720 J_ASSERT(handle->h_buffer_credits > 0);
721 handle->h_buffer_credits--;
723 /* Finally, if the buffer is not journaled right now, we need to
724 * make sure it doesn't get written to disk before the caller
725 * actually commits the new data. */
727 if (!jh->b_transaction) {
728 JBUFFER_TRACE(jh, "no transaction");
729 J_ASSERT_JH(jh, !jh->b_next_transaction);
730 jh->b_transaction = transaction;
731 JBUFFER_TRACE(jh, "file as BJ_Reserved");
732 __journal_file_buffer(jh, transaction, BJ_Reserved);
736 spin_unlock(&journal_datalist_lock);
742 J_ASSERT_JH(jh, buffer_uptodate(jh2bh(jh)));
743 page = jh2bh(jh)->b_page;
744 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
746 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
751 /* If we are about to journal a buffer, then any revoke pending
752 on it is no longer valid. */
753 journal_cancel_revoke(handle, jh);
757 kfree(frozen_buffer);
759 JBUFFER_TRACE(jh, "exit");
764 * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
765 * @handle: transaction to add buffer modifications to
766 * @bh: bh to be used for metadata writes
768 * Returns an error code or 0 on success.
770 * In full data journalling mode the buffer may be of type BJ_AsyncData,
771 * because we're write()ing a buffer which is also part of a shared mapping.
774 int journal_get_write_access (handle_t *handle, struct buffer_head *bh)
776 transaction_t *transaction = handle->h_transaction;
777 journal_t *journal = transaction->t_journal;
778 struct journal_head *jh = journal_add_journal_head(bh);
781 /* We do not want to get caught playing with fields which the
782 * log thread also manipulates. Make sure that the buffer
783 * completes any outstanding IO before proceeding. */
784 lock_journal(journal);
785 rc = do_get_write_access(handle, jh, 0);
786 journal_unlock_journal_head(jh);
787 unlock_journal(journal);
793 * When the user wants to journal a newly created buffer_head
794 * (ie. getblk() returned a new buffer and we are going to populate it
795 * manually rather than reading off disk), then we need to keep the
796 * buffer_head locked until it has been completely filled with new
797 * data. In this case, we should be able to make the assertion that
798 * the bh is not already part of an existing transaction.
800 * The buffer should already be locked by the caller by this point.
801 * There is no lock ranking violation: it was a newly created,
802 * unlocked buffer beforehand. */
805 * int journal_get_create_access () - notify intent to use newly created bh
806 * @handle: ransaction to new buffer to
809 * Call this if you create a new bh.
811 int journal_get_create_access (handle_t *handle, struct buffer_head *bh)
813 transaction_t *transaction = handle->h_transaction;
814 journal_t *journal = transaction->t_journal;
815 struct journal_head *jh = journal_add_journal_head(bh);
818 jbd_debug(5, "journal_head %p\n", jh);
819 lock_journal(journal);
821 if (is_handle_aborted(handle))
825 JBUFFER_TRACE(jh, "entry");
826 /* The buffer may already belong to this transaction due to
827 * pre-zeroing in the filesystem's new_block code. It may also
828 * be on the previous, committing transaction's lists, but it
829 * HAS to be in Forget state in that case: the transaction must
830 * have deleted the buffer for it to be reused here. */
831 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
832 jh->b_transaction == NULL ||
833 (jh->b_transaction == journal->j_committing_transaction &&
834 jh->b_jlist == BJ_Forget)));
836 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
837 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
839 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
840 handle->h_buffer_credits--;
842 spin_lock(&journal_datalist_lock);
843 if (jh->b_transaction == NULL) {
844 jh->b_transaction = transaction;
845 JBUFFER_TRACE(jh, "file as BJ_Reserved");
846 __journal_file_buffer(jh, transaction, BJ_Reserved);
847 JBUFFER_TRACE(jh, "refile");
848 refile_buffer(jh2bh(jh));
849 } else if (jh->b_transaction == journal->j_committing_transaction) {
850 JBUFFER_TRACE(jh, "set next transaction");
851 jh->b_next_transaction = transaction;
853 spin_unlock(&journal_datalist_lock);
856 * akpm: I added this. ext3_alloc_branch can pick up new indirect
857 * blocks which contain freed but then revoked metadata. We need
858 * to cancel the revoke in case we end up freeing it yet again
859 * and the reallocating as data - this would cause a second revoke,
860 * which hits an assertion error.
862 JBUFFER_TRACE(jh, "cancelling revoke");
863 journal_cancel_revoke(handle, jh);
864 journal_unlock_journal_head(jh);
866 unlock_journal(journal);
873 * int journal_get_undo_access() - Notify intent to modify metadata with non-rewindable consequences
874 * @handle: transaction
875 * @bh: buffer to undo
877 * Sometimes there is a need to distinguish between metadata which has
878 * been committed to disk and that which has not. The ext3fs code uses
879 * this for freeing and allocating space, we have to make sure that we
880 * do not reuse freed space until the deallocation has been committed,
881 * since if we overwrote that space we would make the delete
882 * un-rewindable in case of a crash.
884 * To deal with that, journal_get_undo_access requests write access to a
885 * buffer for parts of non-rewindable operations such as delete
886 * operations on the bitmaps. The journaling code must keep a copy of
887 * the buffer's contents prior to the undo_access call until such time
888 * as we know that the buffer has definitely been committed to disk.
890 * We never need to know which transaction the committed data is part
891 * of, buffers touched here are guaranteed to be dirtied later and so
892 * will be committed to a new transaction in due course, at which point
893 * we can discard the old committed data pointer.
895 * Returns error number or 0 on success.
897 int journal_get_undo_access (handle_t *handle, struct buffer_head *bh)
899 journal_t *journal = handle->h_transaction->t_journal;
901 struct journal_head *jh = journal_add_journal_head(bh);
903 JBUFFER_TRACE(jh, "entry");
904 lock_journal(journal);
906 /* Do this first --- it can drop the journal lock, so we want to
907 * make sure that obtaining the committed_data is done
908 * atomically wrt. completion of any outstanding commits. */
909 err = do_get_write_access (handle, jh, 1);
913 if (!jh->b_committed_data) {
914 /* Copy out the current buffer contents into the
915 * preserved, committed copy. */
916 JBUFFER_TRACE(jh, "generate b_committed data");
917 jh->b_committed_data = jbd_kmalloc(jh2bh(jh)->b_size,
919 if (!jh->b_committed_data) {
920 printk(KERN_EMERG "%s: No memory for committed data!\n",
926 memcpy (jh->b_committed_data, jh2bh(jh)->b_data,
932 J_ASSERT_JH(jh, jh->b_committed_data);
933 journal_unlock_journal_head(jh);
934 unlock_journal(journal);
939 * int journal_dirty_data() - mark a buffer as containing dirty data which needs to be flushed before we can commit the current transaction.
940 * @handle: transaction
941 * @bh: bufferhead to mark
944 * The buffer is placed on the transaction's data list and is marked as
945 * belonging to the transaction.
947 * If `async' is set then the writebask will be initiated by the caller
948 * using submit_bh -> end_buffer_io_async. We put the buffer onto
951 * Returns error number or 0 on success.
953 int journal_dirty_data (handle_t *handle, struct buffer_head *bh, int async)
956 * journal_dirty_data() can be called via page_launder->ext3_writepage
957 * by kswapd. So it cannot block. Happily, there's nothing here
958 * which needs lock_journal if `async' is set.
960 * When the buffer is on the current transaction we freely move it
961 * between BJ_AsyncData and BJ_SyncData according to who tried to
962 * change its state last.
964 journal_t *journal = handle->h_transaction->t_journal;
966 int wanted_jlist = async ? BJ_AsyncData : BJ_SyncData;
967 struct journal_head *jh;
969 if (is_handle_aborted(handle))
972 jh = journal_add_journal_head(bh);
973 JBUFFER_TRACE(jh, "entry");
976 * The buffer could *already* be dirty. Writeout can start
979 jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
982 * What if the buffer is already part of a running transaction?
984 * There are two cases:
985 * 1) It is part of the current running transaction. Refile it,
986 * just in case we have allocated it as metadata, deallocated
987 * it, then reallocated it as data.
988 * 2) It is part of the previous, still-committing transaction.
989 * If all we want to do is to guarantee that the buffer will be
990 * written to disk before this new transaction commits, then
991 * being sure that the *previous* transaction has this same
992 * property is sufficient for us! Just leave it on its old
995 * In case (2), the buffer must not already exist as metadata
996 * --- that would violate write ordering (a transaction is free
997 * to write its data at any point, even before the previous
998 * committing transaction has committed). The caller must
999 * never, ever allow this to happen: there's nothing we can do
1000 * about it in this layer.
1002 spin_lock(&journal_datalist_lock);
1003 if (jh->b_transaction) {
1004 JBUFFER_TRACE(jh, "has transaction");
1005 if (jh->b_transaction != handle->h_transaction) {
1006 JBUFFER_TRACE(jh, "belongs to older transaction");
1007 J_ASSERT_JH(jh, jh->b_transaction ==
1008 journal->j_committing_transaction);
1010 /* @@@ IS THIS TRUE ? */
1012 * Not any more. Scenario: someone does a write()
1013 * in data=journal mode. The buffer's transaction has
1014 * moved into commit. Then someone does another
1015 * write() to the file. We do the frozen data copyout
1016 * and set b_next_transaction to point to j_running_t.
1017 * And while we're in that state, someone does a
1018 * writepage() in an attempt to pageout the same area
1019 * of the file via a shared mapping. At present that
1020 * calls journal_dirty_data(), and we get right here.
1021 * It may be too late to journal the data. Simply
1022 * falling through to the next test will suffice: the
1023 * data will be dirty and wil be checkpointed. The
1024 * ordering comments in the next comment block still
1027 //J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1030 * If we're journalling data, and this buffer was
1031 * subject to a write(), it could be metadata, forget
1032 * or shadow against the committing transaction. Now,
1033 * someone has dirtied the same darn page via a mapping
1034 * and it is being writepage()'d.
1035 * We *could* just steal the page from commit, with some
1036 * fancy locking there. Instead, we just skip it -
1037 * don't tie the page's buffers to the new transaction
1039 * Implication: if we crash before the writepage() data
1040 * is written into the filesystem, recovery will replay
1043 if (jh->b_jlist != BJ_None &&
1044 jh->b_jlist != BJ_SyncData &&
1045 jh->b_jlist != BJ_AsyncData) {
1046 JBUFFER_TRACE(jh, "Not stealing");
1051 * This buffer may be undergoing writeout in commit. We
1052 * can't return from here and let the caller dirty it
1053 * again because that can cause the write-out loop in
1054 * commit to never terminate.
1056 if (!async && buffer_dirty(bh)) {
1057 atomic_inc(&bh->b_count);
1058 spin_unlock(&journal_datalist_lock);
1060 ll_rw_block(WRITE, 1, &bh);
1062 spin_lock(&journal_datalist_lock);
1063 /* The buffer may become locked again at any
1064 time if it is redirtied */
1067 /* journal_clean_data_list() may have got there first */
1068 if (jh->b_transaction != NULL) {
1069 JBUFFER_TRACE(jh, "unfile from commit");
1070 __journal_unfile_buffer(jh);
1071 jh->b_transaction = NULL;
1073 /* The buffer will be refiled below */
1077 * Special case --- the buffer might actually have been
1078 * allocated and then immediately deallocated in the previous,
1079 * committing transaction, so might still be left on that
1080 * transaction's metadata lists.
1082 if (jh->b_jlist != wanted_jlist) {
1083 JBUFFER_TRACE(jh, "not on correct data list: unfile");
1084 J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
1085 __journal_unfile_buffer(jh);
1086 jh->b_transaction = NULL;
1087 JBUFFER_TRACE(jh, "file as data");
1088 __journal_file_buffer(jh, handle->h_transaction,
1092 JBUFFER_TRACE(jh, "not on a transaction");
1093 __journal_file_buffer(jh, handle->h_transaction, wanted_jlist);
1096 spin_unlock(&journal_datalist_lock);
1098 BUFFER_TRACE(bh, "brelse");
1101 JBUFFER_TRACE(jh, "exit");
1102 journal_unlock_journal_head(jh);
1107 * int journal_dirty_metadata() - mark a buffer as containing dirty metadata
1108 * @handle: transaction to add buffer to.
1109 * @bh: buffer to mark
1111 * mark dirty metadata which needs to be journaled as part of the current transaction.
1113 * The buffer is placed on the transaction's metadata list and is marked
1114 * as belonging to the transaction.
1116 * Returns error number or 0 on success.
1118 int journal_dirty_metadata (handle_t *handle, struct buffer_head *bh)
1121 * Special care needs to be taken if the buffer already belongs to the
1122 * current committing transaction (in which case we should have frozen
1123 * data present for that commit). In that case, we don't relink the
1124 * buffer: that only gets done when the old transaction finally
1125 * completes its commit.
1128 transaction_t *transaction = handle->h_transaction;
1129 journal_t *journal = transaction->t_journal;
1130 struct journal_head *jh = bh2jh(bh);
1132 jbd_debug(5, "journal_head %p\n", jh);
1133 JBUFFER_TRACE(jh, "entry");
1134 lock_journal(journal);
1135 if (is_handle_aborted(handle))
1138 spin_lock(&journal_datalist_lock);
1139 set_bit(BH_JBDDirty, &bh->b_state);
1140 set_buffer_flushtime(bh);
1142 J_ASSERT_JH(jh, jh->b_transaction != NULL);
1145 * Metadata already on the current transaction list doesn't
1146 * need to be filed. Metadata on another transaction's list must
1147 * be committing, and will be refiled once the commit completes:
1148 * leave it alone for now.
1151 if (jh->b_transaction != transaction) {
1152 JBUFFER_TRACE(jh, "already on other transaction");
1153 J_ASSERT_JH(jh, jh->b_transaction ==
1154 journal->j_committing_transaction);
1155 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1156 /* And this case is illegal: we can't reuse another
1157 * transaction's data buffer, ever. */
1158 /* FIXME: writepage() should be journalled */
1159 J_ASSERT_JH(jh, jh->b_jlist != BJ_SyncData);
1163 /* That test should have eliminated the following case: */
1164 J_ASSERT_JH(jh, jh->b_frozen_data == 0);
1166 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1167 __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1170 spin_unlock(&journal_datalist_lock);
1171 JBUFFER_TRACE(jh, "exit");
1173 unlock_journal(journal);
1179 * journal_release_buffer: undo a get_write_access without any buffer
1180 * updates, if the update decided in the end that it didn't need access.
1182 * journal_get_write_access() can block, so it is quite possible for a
1183 * journaling component to decide after the write access is returned
1184 * that global state has changed and the update is no longer required. */
1186 void journal_release_buffer (handle_t *handle, struct buffer_head *bh)
1188 transaction_t *transaction = handle->h_transaction;
1189 journal_t *journal = transaction->t_journal;
1190 struct journal_head *jh = bh2jh(bh);
1192 lock_journal(journal);
1193 JBUFFER_TRACE(jh, "entry");
1195 /* If the buffer is reserved but not modified by this
1196 * transaction, then it is safe to release it. In all other
1197 * cases, just leave the buffer as it is. */
1199 spin_lock(&journal_datalist_lock);
1200 if (jh->b_jlist == BJ_Reserved && jh->b_transaction == transaction &&
1201 !buffer_jdirty(jh2bh(jh))) {
1202 JBUFFER_TRACE(jh, "unused: refiling it");
1203 handle->h_buffer_credits++;
1204 __journal_refile_buffer(jh);
1206 spin_unlock(&journal_datalist_lock);
1208 JBUFFER_TRACE(jh, "exit");
1209 unlock_journal(journal);
1214 * void journal_forget() - bforget() for potentially-journaled buffers.
1215 * @handle: transaction handle
1216 * @bh: bh to 'forget'
1218 * We can only do the bforget if there are no commits pending against the
1219 * buffer. If the buffer is dirty in the current running transaction we
1220 * can safely unlink it.
1222 * bh may not be a journalled buffer at all - it may be a non-JBD
1223 * buffer which came off the hashtable. Check for this.
1225 * Decrements bh->b_count by one.
1227 * Allow this call even if the handle has aborted --- it may be part of
1228 * the caller's cleanup after an abort.
1230 void journal_forget (handle_t *handle, struct buffer_head *bh)
1232 transaction_t *transaction = handle->h_transaction;
1233 journal_t *journal = transaction->t_journal;
1234 struct journal_head *jh;
1236 BUFFER_TRACE(bh, "entry");
1238 lock_journal(journal);
1239 spin_lock(&journal_datalist_lock);
1241 if (!buffer_jbd(bh))
1245 if (jh->b_transaction == handle->h_transaction) {
1246 J_ASSERT_JH(jh, !jh->b_frozen_data);
1248 /* If we are forgetting a buffer which is already part
1249 * of this transaction, then we can just drop it from
1250 * the transaction immediately. */
1251 clear_bit(BH_Dirty, &bh->b_state);
1252 clear_bit(BH_JBDDirty, &bh->b_state);
1254 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1255 J_ASSERT_JH(jh, !jh->b_committed_data);
1257 __journal_unfile_buffer(jh);
1258 jh->b_transaction = 0;
1261 * We are no longer going to journal this buffer.
1262 * However, the commit of this transaction is still
1263 * important to the buffer: the delete that we are now
1264 * processing might obsolete an old log entry, so by
1265 * committing, we can satisfy the buffer's checkpoint.
1267 * So, if we have a checkpoint on the buffer, we should
1268 * now refile the buffer on our BJ_Forget list so that
1269 * we know to remove the checkpoint after we commit.
1272 if (jh->b_cp_transaction) {
1273 __journal_file_buffer(jh, transaction, BJ_Forget);
1275 __journal_remove_journal_head(bh);
1277 if (!buffer_jbd(bh)) {
1278 spin_unlock(&journal_datalist_lock);
1279 unlock_journal(journal);
1285 } else if (jh->b_transaction) {
1286 J_ASSERT_JH(jh, (jh->b_transaction ==
1287 journal->j_committing_transaction));
1288 /* However, if the buffer is still owned by a prior
1289 * (committing) transaction, we can't drop it yet... */
1290 JBUFFER_TRACE(jh, "belongs to older transaction");
1291 /* ... but we CAN drop it from the new transaction if we
1292 * have also modified it since the original commit. */
1294 if (jh->b_next_transaction) {
1295 J_ASSERT(jh->b_next_transaction == transaction);
1296 jh->b_next_transaction = NULL;
1301 spin_unlock(&journal_datalist_lock);
1302 unlock_journal(journal);
1309 * journal_sync_buffer: flush a potentially-journaled buffer to disk.
1311 * Used for O_SYNC filesystem operations. If the buffer is journaled,
1312 * we need to complete the O_SYNC by waiting for the transaction to
1313 * complete. It is an error to call journal_sync_buffer before
1317 void journal_sync_buffer(struct buffer_head *bh)
1319 transaction_t *transaction;
1322 struct journal_head *jh;
1324 /* If the buffer isn't journaled, this is easy: just sync it to
1326 BUFFER_TRACE(bh, "entry");
1328 spin_lock(&journal_datalist_lock);
1329 if (!buffer_jbd(bh)) {
1330 spin_unlock(&journal_datalist_lock);
1334 if (jh->b_transaction == NULL) {
1335 /* If the buffer has already been journaled, then this
1337 if (jh->b_cp_transaction == NULL) {
1338 spin_unlock(&journal_datalist_lock);
1341 atomic_inc(&bh->b_count);
1342 spin_unlock(&journal_datalist_lock);
1343 ll_rw_block (WRITE, 1, &bh);
1349 /* Otherwise, just wait until the transaction is synced to disk. */
1350 transaction = jh->b_transaction;
1351 journal = transaction->t_journal;
1352 sequence = transaction->t_tid;
1353 spin_unlock(&journal_datalist_lock);
1355 jbd_debug(2, "requesting commit for jh %p\n", jh);
1356 log_start_commit (journal, transaction);
1358 while (tid_gt(sequence, journal->j_commit_sequence)) {
1359 wake_up(&journal->j_wait_done_commit);
1360 sleep_on(&journal->j_wait_done_commit);
1362 JBUFFER_TRACE(jh, "exit");
1369 * Register a callback function for this handle. The function will be
1370 * called when the transaction that this handle is part of has been
1371 * committed to disk with the original callback data struct and the
1372 * error status of the journal as parameters. There is no guarantee of
1373 * ordering between handles within a single transaction, nor between
1374 * callbacks registered on the same handle.
1376 * The caller is responsible for allocating the journal_callback struct.
1377 * This is to allow the caller to add as much extra data to the callback
1378 * as needed, but reduce the overhead of multiple allocations. The caller
1379 * allocated struct must start with a struct journal_callback at offset 0,
1380 * and has the caller-specific data afterwards.
1382 void journal_callback_set(handle_t *handle,
1383 void (*func)(struct journal_callback *jcb, int error),
1384 struct journal_callback *jcb)
1386 list_add_tail(&jcb->jcb_list, &handle->h_jcb);
1387 jcb->jcb_func = func;
1391 * int journal_stop() - complete a transaction
1392 * @handle: tranaction to complete.
1394 * All done for a particular handle.
1396 * There is not much action needed here. We just return any remaining
1397 * buffer credits to the transaction and remove the handle. The only
1398 * complication is that we need to start a commit operation if the
1399 * filesystem is marked for synchronous update.
1401 * journal_stop itself will not usually return an error, but it may
1402 * do so in unusual circumstances. In particular, expect it to
1403 * return -EIO if a journal_abort has been executed since the
1404 * transaction began.
1406 int journal_stop(handle_t *handle)
1408 transaction_t *transaction = handle->h_transaction;
1409 journal_t *journal = transaction->t_journal;
1410 int old_handle_count, err;
1415 J_ASSERT (transaction->t_updates > 0);
1416 J_ASSERT (journal_current_handle() == handle);
1418 if (is_handle_aborted(handle))
1423 if (--handle->h_ref > 0) {
1424 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1429 jbd_debug(4, "Handle %p going down\n", handle);
1432 * Implement synchronous transaction batching. If the handle
1433 * was synchronous, don't force a commit immediately. Let's
1434 * yield and let another thread piggyback onto this transaction.
1435 * Keep doing that while new threads continue to arrive.
1436 * It doesn't cost much - we're about to run a commit and sleep
1437 * on IO anyway. Speeds up many-threaded, many-dir operations
1440 if (handle->h_sync) {
1442 old_handle_count = transaction->t_handle_count;
1444 } while (old_handle_count != transaction->t_handle_count);
1447 current->journal_info = NULL;
1448 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1449 transaction->t_updates--;
1450 if (!transaction->t_updates) {
1451 wake_up(&journal->j_wait_updates);
1452 if (journal->j_barrier_count)
1453 wake_up(&journal->j_wait_transaction_locked);
1456 /* Move callbacks from the handle to the transaction. */
1457 list_splice(&handle->h_jcb, &transaction->t_jcb);
1460 * If the handle is marked SYNC, we need to set another commit
1461 * going! We also want to force a commit if the current
1462 * transaction is occupying too much of the log, or if the
1463 * transaction is too old now.
1465 if (handle->h_sync ||
1466 transaction->t_outstanding_credits >
1467 journal->j_max_transaction_buffers ||
1468 time_after_eq(jiffies, transaction->t_expires)) {
1469 /* Do this even for aborted journals: an abort still
1470 * completes the commit thread, it just doesn't write
1471 * anything to disk. */
1472 tid_t tid = transaction->t_tid;
1474 jbd_debug(2, "transaction too old, requesting commit for "
1475 "handle %p\n", handle);
1476 /* This is non-blocking */
1477 log_start_commit(journal, transaction);
1480 * Special case: JFS_SYNC synchronous updates require us
1481 * to wait for the commit to complete.
1483 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1484 log_wait_commit(journal, tid);
1490 /**int journal_force_commit() - force any uncommitted transactions
1491 * @journal: journal to force
1493 * For synchronous operations: force any uncommitted transactions
1494 * to disk. May seem kludgy, but it reuses all the handle batching
1495 * code in a very simple manner.
1497 int journal_force_commit(journal_t *journal)
1503 handle = journal_start(journal, 1);
1504 if (IS_ERR(handle)) {
1505 ret = PTR_ERR(handle);
1509 journal_stop(handle);
1517 * List management code snippets: various functions for manipulating the
1518 * transaction buffer lists.
1523 * Append a buffer to a transaction list, given the transaction's list head
1525 * journal_datalist_lock is held.
1529 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1532 jh->b_tnext = jh->b_tprev = jh;
1535 /* Insert at the tail of the list to preserve order */
1536 struct journal_head *first = *list, *last = first->b_tprev;
1538 jh->b_tnext = first;
1539 last->b_tnext = first->b_tprev = jh;
1544 * Remove a buffer from a transaction list, given the transaction's list
1547 * Called with journal_datalist_lock held, and the journal may not
1552 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1555 *list = jh->b_tnext;
1559 jh->b_tprev->b_tnext = jh->b_tnext;
1560 jh->b_tnext->b_tprev = jh->b_tprev;
1564 * Remove a buffer from the appropriate transaction list.
1566 * Note that this function can *change* the value of
1567 * bh->b_transaction->t_sync_datalist, t_async_datalist, t_buffers, t_forget,
1568 * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
1569 * is holding onto a copy of one of thee pointers, it could go bad.
1570 * Generally the caller needs to re-read the pointer from the transaction_t.
1572 * If bh->b_jlist is BJ_SyncData or BJ_AsyncData then we may have been called
1573 * via journal_try_to_free_buffer() or journal_clean_data_list(). In that
1574 * case, journal_datalist_lock will be held, and the journal may not be locked.
1576 void __journal_unfile_buffer(struct journal_head *jh)
1578 struct journal_head **list = 0;
1579 transaction_t * transaction;
1581 assert_spin_locked(&journal_datalist_lock);
1582 transaction = jh->b_transaction;
1585 J_ASSERT (current->lock_depth >= 0);
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);
1699 * int journal_try_to_free_buffers() - try to free page buffers.
1700 * @journal: journal for operation
1701 * @page: to try and free
1702 * @gfp_mask: 'IO' mode for try_to_free_buffers()
1705 * For all the buffers on this page,
1706 * if they are fully written out ordered data, move them onto BUF_CLEAN
1707 * so try_to_free_buffers() can reap them.
1709 * This function returns non-zero if we wish try_to_free_buffers()
1710 * to be called. We do this if the page is releasable by try_to_free_buffers().
1711 * We also do it if the page has locked or dirty buffers and the caller wants
1712 * us to perform sync or async writeout.
1714 int journal_try_to_free_buffers(journal_t *journal,
1715 struct page *page, int gfp_mask)
1718 * journal_try_to_free_buffers(). For all the buffers on this page,
1719 * if they are fully written out ordered data, move them onto BUF_CLEAN
1720 * so try_to_free_buffers() can reap them. Called with lru_list_lock
1721 * not held. Does its own locking.
1723 * This complicates JBD locking somewhat. We aren't protected by the
1724 * BKL here. We wish to remove the buffer from its committing or
1725 * running transaction's ->t_datalist via __journal_unfile_buffer.
1727 * This may *change* the value of transaction_t->t_datalist, so anyone
1728 * who looks at t_datalist needs to lock against this function.
1730 * Even worse, someone may be doing a journal_dirty_data on this
1731 * buffer. So we need to lock against that. journal_dirty_data()
1732 * will come out of the lock with the buffer dirty, which makes it
1733 * ineligible for release here.
1735 * Who else is affected by this? hmm... Really the only contender
1736 * is do_get_write_access() - it could be looking at the buffer while
1737 * journal_try_to_free_buffer() is changing its state. But that
1738 * cannot happen because we never reallocate freed data as metadata
1739 * while the data is part of a transaction. Yes?
1742 struct buffer_head *bh;
1743 struct buffer_head *tmp;
1744 int locked_or_dirty = 0;
1747 J_ASSERT(PageLocked(page));
1751 spin_lock(&journal_datalist_lock);
1753 struct buffer_head *p = tmp;
1755 tmp = tmp->b_this_page;
1757 if (!__journal_try_to_free_buffer(p, &locked_or_dirty))
1759 } while (tmp != bh);
1760 spin_unlock(&journal_datalist_lock);
1762 if (!(gfp_mask & (__GFP_IO|__GFP_WAIT)))
1764 if (!locked_or_dirty)
1767 * The VM wants us to do writeout, or to block on IO, or both.
1768 * So we allow try_to_free_buffers to be called even if the page
1769 * still has journalled buffers.
1777 * This buffer is no longer needed. If it is on an older transaction's
1778 * checkpoint list we need to record it on this transaction's forget list
1779 * to pin this buffer (and hence its checkpointing transaction) down until
1780 * this transaction commits. If the buffer isn't on a checkpoint list, we
1782 * Returns non-zero if JBD no longer has an interest in the buffer.
1784 static int dispose_buffer(struct journal_head *jh,
1785 transaction_t *transaction)
1788 struct buffer_head *bh = jh2bh(jh);
1790 spin_lock(&journal_datalist_lock);
1791 __journal_unfile_buffer(jh);
1792 jh->b_transaction = 0;
1794 if (jh->b_cp_transaction) {
1795 JBUFFER_TRACE(jh, "on running+cp transaction");
1796 __journal_file_buffer(jh, transaction, BJ_Forget);
1797 clear_bit(BH_JBDDirty, &bh->b_state);
1800 JBUFFER_TRACE(jh, "on running transaction");
1801 __journal_remove_journal_head(bh);
1804 spin_unlock(&journal_datalist_lock);
1811 * This code is tricky. It has a number of cases to deal with.
1813 * There are two invariants which this code relies on:
1815 * i_size must be updated on disk before we start calling flushpage on the
1818 * This is done in ext3 by defining an ext3_setattr method which
1819 * updates i_size before truncate gets going. By maintaining this
1820 * invariant, we can be sure that it is safe to throw away any buffers
1821 * attached to the current transaction: once the transaction commits,
1822 * we know that the data will not be needed.
1824 * Note however that we can *not* throw away data belonging to the
1825 * previous, committing transaction!
1827 * Any disk blocks which *are* part of the previous, committing
1828 * transaction (and which therefore cannot be discarded immediately) are
1829 * not going to be reused in the new running transaction
1831 * The bitmap committed_data images guarantee this: any block which is
1832 * allocated in one transaction and removed in the next will be marked
1833 * as in-use in the committed_data bitmap, so cannot be reused until
1834 * the next transaction to delete the block commits. This means that
1835 * leaving committing buffers dirty is quite safe: the disk blocks
1836 * cannot be reallocated to a different file and so buffer aliasing is
1840 * The above applies mainly to ordered data mode. In writeback mode we
1841 * don't make guarantees about the order in which data hits disk --- in
1842 * particular we don't guarantee that new dirty data is flushed before
1843 * transaction commit --- so it is always safe just to discard data
1844 * immediately in that mode. --sct
1848 * The journal_unmap_buffer helper function returns zero if the buffer
1849 * concerned remains pinned as an anonymous buffer belonging to an older
1852 * We're outside-transaction here. Either or both of j_running_transaction
1853 * and j_committing_transaction may be NULL.
1855 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1857 transaction_t *transaction;
1858 struct journal_head *jh;
1861 BUFFER_TRACE(bh, "entry");
1863 if (!buffer_mapped(bh))
1866 /* It is safe to proceed here without the
1867 * journal_datalist_spinlock because the buffers cannot be
1868 * stolen by try_to_free_buffers as long as we are holding the
1869 * page lock. --sct */
1871 if (!buffer_jbd(bh))
1875 transaction = jh->b_transaction;
1876 if (transaction == NULL) {
1877 /* First case: not on any transaction. If it
1878 * has no checkpoint link, then we can zap it:
1879 * it's a writeback-mode buffer so we don't care
1880 * if it hits disk safely. */
1881 if (!jh->b_cp_transaction) {
1882 JBUFFER_TRACE(jh, "not on any transaction: zap");
1886 if (!buffer_dirty(bh)) {
1887 /* bdflush has written it. We can drop it now */
1891 /* OK, it must be in the journal but still not
1892 * written fully to disk: it's metadata or
1893 * journaled data... */
1895 if (journal->j_running_transaction) {
1896 /* ... and once the current transaction has
1897 * committed, the buffer won't be needed any
1899 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1900 return dispose_buffer(jh,
1901 journal->j_running_transaction);
1903 /* There is no currently-running transaction. So the
1904 * orphan record which we wrote for this file must have
1905 * passed into commit. We must attach this buffer to
1906 * the committing transaction, if it exists. */
1907 if (journal->j_committing_transaction) {
1908 JBUFFER_TRACE(jh, "give to committing trans");
1909 return dispose_buffer(jh,
1910 journal->j_committing_transaction);
1912 /* The orphan record's transaction has
1913 * committed. We can cleanse this buffer */
1914 clear_bit(BH_JBDDirty, &bh->b_state);
1918 } else if (transaction == journal->j_committing_transaction) {
1919 /* If it is committing, we simply cannot touch it. We
1920 * can remove it's next_transaction pointer from the
1921 * running transaction if that is set, but nothing
1923 JBUFFER_TRACE(jh, "on committing transaction");
1924 set_bit(BH_Freed, &bh->b_state);
1925 if (jh->b_next_transaction) {
1926 J_ASSERT(jh->b_next_transaction ==
1927 journal->j_running_transaction);
1928 jh->b_next_transaction = NULL;
1932 /* Good, the buffer belongs to the running transaction.
1933 * We are writing our own transaction's data, not any
1934 * previous one's, so it is safe to throw it away
1935 * (remember that we expect the filesystem to have set
1936 * i_size already for this truncate so recovery will not
1937 * expose the disk blocks we are discarding here.) */
1938 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1939 may_free = dispose_buffer(jh, transaction);
1943 if (buffer_dirty(bh))
1944 mark_buffer_clean(bh);
1945 J_ASSERT_BH(bh, !buffer_jdirty(bh));
1946 clear_bit(BH_Uptodate, &bh->b_state);
1947 clear_bit(BH_Mapped, &bh->b_state);
1948 clear_bit(BH_Req, &bh->b_state);
1949 clear_bit(BH_New, &bh->b_state);
1954 * int journal_flushpage()
1955 * @journal: journal to use for flush...
1956 * @page: page to flush
1957 * @offset: length of page to flush.
1959 * Reap page buffers containing data after offset in page.
1961 * Return non-zero if the page's buffers were successfully reaped.
1963 int journal_flushpage(journal_t *journal,
1965 unsigned long offset)
1967 struct buffer_head *head, *bh, *next;
1968 unsigned int curr_off = 0;
1971 if (!PageLocked(page))
1976 /* We will potentially be playing with lists other than just the
1977 * data lists (especially for journaled data mode), so be
1978 * cautious in our locking. */
1979 lock_journal(journal);
1981 head = bh = page->buffers;
1983 unsigned int next_off = curr_off + bh->b_size;
1984 next = bh->b_this_page;
1986 /* AKPM: doing lock_buffer here may be overly paranoid */
1987 if (offset <= curr_off) {
1988 /* This block is wholly outside the truncation point */
1990 may_free &= journal_unmap_buffer(journal, bh);
1993 curr_off = next_off;
1996 } while (bh != head);
1998 unlock_journal(journal);
2001 if (!may_free || !try_to_free_buffers(page, 0))
2003 J_ASSERT(page->buffers == NULL);
2009 * File a buffer on the given transaction list.
2011 void __journal_file_buffer(struct journal_head *jh,
2012 transaction_t *transaction, int jlist)
2014 struct journal_head **list = 0;
2017 assert_spin_locked(&journal_datalist_lock);
2020 J_ASSERT (current->lock_depth >= 0);
2022 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2023 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2024 jh->b_transaction == 0);
2026 if (jh->b_transaction && jh->b_jlist == jlist)
2029 /* The following list of buffer states needs to be consistent
2030 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
2033 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2034 jlist == BJ_Shadow || jlist == BJ_Forget) {
2035 if (atomic_set_buffer_clean(jh2bh(jh)) ||
2036 test_and_clear_bit(BH_JBDDirty, &jh2bh(jh)->b_state))
2040 if (jh->b_transaction)
2041 __journal_unfile_buffer(jh);
2043 jh->b_transaction = transaction;
2047 J_ASSERT_JH(jh, !jh->b_committed_data);
2048 J_ASSERT_JH(jh, !jh->b_frozen_data);
2051 list = &transaction->t_sync_datalist;
2054 list = &transaction->t_async_datalist;
2057 transaction->t_nr_buffers++;
2058 list = &transaction->t_buffers;
2061 list = &transaction->t_forget;
2064 list = &transaction->t_iobuf_list;
2067 list = &transaction->t_shadow_list;
2070 list = &transaction->t_log_list;
2073 list = &transaction->t_reserved_list;
2077 __blist_add_buffer(list, jh);
2078 jh->b_jlist = jlist;
2081 set_bit(BH_JBDDirty, &jh2bh(jh)->b_state);
2084 void journal_file_buffer(struct journal_head *jh,
2085 transaction_t *transaction, int jlist)
2087 spin_lock(&journal_datalist_lock);
2088 __journal_file_buffer(jh, transaction, jlist);
2089 spin_unlock(&journal_datalist_lock);
2093 * Remove a buffer from its current buffer list in preparation for
2094 * dropping it from its current transaction entirely. If the buffer has
2095 * already started to be used by a subsequent transaction, refile the
2096 * buffer on that transaction's metadata list.
2099 void __journal_refile_buffer(struct journal_head *jh)
2103 assert_spin_locked(&journal_datalist_lock);
2105 J_ASSERT_JH(jh, current->lock_depth >= 0);
2107 /* If the buffer is now unused, just drop it. */
2108 if (jh->b_next_transaction == NULL) {
2109 __journal_unfile_buffer(jh);
2110 jh->b_transaction = NULL;
2111 /* Onto BUF_DIRTY for writeback */
2112 refile_buffer(jh2bh(jh));
2116 /* It has been modified by a later transaction: add it to the
2117 * new transaction's metadata list. */
2119 if (test_and_clear_bit(BH_JBDDirty, &jh2bh(jh)->b_state))
2122 __journal_unfile_buffer(jh);
2123 jh->b_transaction = jh->b_next_transaction;
2124 jh->b_next_transaction = NULL;
2125 __journal_file_buffer(jh, jh->b_transaction, BJ_Metadata);
2126 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2129 set_bit(BH_JBDDirty, &jh2bh(jh)->b_state);
2134 * For the unlocked version of this call, also make sure that any
2135 * hanging journal_head is cleaned up if necessary.
2137 * __journal_refile_buffer is usually called as part of a single locked
2138 * operation on a buffer_head, in which the caller is probably going to
2139 * be hooking the journal_head onto other lists. In that case it is up
2140 * to the caller to remove the journal_head if necessary. For the
2141 * unlocked journal_refile_buffer call, the caller isn't going to be
2142 * doing anything else to the buffer so we need to do the cleanup
2143 * ourselves to avoid a jh leak.
2145 * *** The journal_head may be freed by this call! ***
2147 void journal_refile_buffer(struct journal_head *jh)
2149 struct buffer_head *bh;
2151 spin_lock(&journal_datalist_lock);
2154 __journal_refile_buffer(jh);
2155 __journal_remove_journal_head(bh);
2157 spin_unlock(&journal_datalist_lock);