2 * linux/fs/jbd/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/time.h>
22 #include <linux/jbd.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
27 #include <linux/highmem.h>
28 #include <linux/hrtimer.h>
30 static void __journal_temp_unlink_buffer(struct journal_head *jh);
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.
45 * Called under j_state_lock
48 static transaction_t *
49 get_transaction(journal_t *journal, transaction_t *transaction)
51 transaction->t_journal = journal;
52 transaction->t_state = T_RUNNING;
53 transaction->t_start_time = ktime_get();
54 transaction->t_tid = journal->j_transaction_sequence++;
55 transaction->t_expires = jiffies + journal->j_commit_interval;
56 spin_lock_init(&transaction->t_handle_lock);
58 /* Set up the commit timer for the new transaction. */
59 journal->j_commit_timer.expires = round_jiffies(transaction->t_expires);
60 add_timer(&journal->j_commit_timer);
62 J_ASSERT(journal->j_running_transaction == NULL);
63 journal->j_running_transaction = transaction;
71 * A handle_t is an object which represents a single atomic update to a
72 * filesystem, and which tracks all of the modifications which form part
77 * start_this_handle: Given a handle, deal with any locking or stalling
78 * needed to make sure that there is enough journal space for the handle
79 * to begin. Attach the handle to a transaction and set up the
80 * transaction's buffer credits.
83 static int start_this_handle(journal_t *journal, handle_t *handle)
85 transaction_t *transaction;
87 int nblocks = handle->h_buffer_credits;
88 transaction_t *new_transaction = NULL;
91 if (nblocks > journal->j_max_transaction_buffers) {
92 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
93 current->comm, nblocks,
94 journal->j_max_transaction_buffers);
100 if (!journal->j_running_transaction) {
101 new_transaction = kzalloc(sizeof(*new_transaction),
102 GFP_NOFS|__GFP_NOFAIL);
103 if (!new_transaction) {
109 jbd_debug(3, "New handle %p going live.\n", handle);
114 * We need to hold j_state_lock until t_updates has been incremented,
115 * for proper journal barrier handling
117 spin_lock(&journal->j_state_lock);
119 if (is_journal_aborted(journal) ||
120 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
121 spin_unlock(&journal->j_state_lock);
126 /* Wait on the journal's transaction barrier if necessary */
127 if (journal->j_barrier_count) {
128 spin_unlock(&journal->j_state_lock);
129 wait_event(journal->j_wait_transaction_locked,
130 journal->j_barrier_count == 0);
134 if (!journal->j_running_transaction) {
135 if (!new_transaction) {
136 spin_unlock(&journal->j_state_lock);
137 goto alloc_transaction;
139 get_transaction(journal, new_transaction);
140 new_transaction = NULL;
143 transaction = journal->j_running_transaction;
146 * If the current transaction is locked down for commit, wait for the
147 * lock to be released.
149 if (transaction->t_state == T_LOCKED) {
152 prepare_to_wait(&journal->j_wait_transaction_locked,
153 &wait, TASK_UNINTERRUPTIBLE);
154 spin_unlock(&journal->j_state_lock);
156 finish_wait(&journal->j_wait_transaction_locked, &wait);
161 * If there is not enough space left in the log to write all potential
162 * buffers requested by this operation, we need to stall pending a log
163 * checkpoint to free some more log space.
165 spin_lock(&transaction->t_handle_lock);
166 needed = transaction->t_outstanding_credits + nblocks;
168 if (needed > journal->j_max_transaction_buffers) {
170 * If the current transaction is already too large, then start
171 * to commit it: we can then go back and attach this handle to
176 jbd_debug(2, "Handle %p starting new commit...\n", handle);
177 spin_unlock(&transaction->t_handle_lock);
178 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
179 TASK_UNINTERRUPTIBLE);
180 __log_start_commit(journal, transaction->t_tid);
181 spin_unlock(&journal->j_state_lock);
183 finish_wait(&journal->j_wait_transaction_locked, &wait);
188 * The commit code assumes that it can get enough log space
189 * without forcing a checkpoint. This is *critical* for
190 * correctness: a checkpoint of a buffer which is also
191 * associated with a committing transaction creates a deadlock,
192 * so commit simply cannot force through checkpoints.
194 * We must therefore ensure the necessary space in the journal
195 * *before* starting to dirty potentially checkpointed buffers
196 * in the new transaction.
198 * The worst part is, any transaction currently committing can
199 * reduce the free space arbitrarily. Be careful to account for
200 * those buffers when checkpointing.
204 * @@@ AKPM: This seems rather over-defensive. We're giving commit
205 * a _lot_ of headroom: 1/4 of the journal plus the size of
206 * the committing transaction. Really, we only need to give it
207 * committing_transaction->t_outstanding_credits plus "enough" for
208 * the log control blocks.
209 * Also, this test is inconsitent with the matching one in
212 if (__log_space_left(journal) < jbd_space_needed(journal)) {
213 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
214 spin_unlock(&transaction->t_handle_lock);
215 __log_wait_for_space(journal);
219 /* OK, account for the buffers that this operation expects to
220 * use and add the handle to the running transaction. */
222 handle->h_transaction = transaction;
223 transaction->t_outstanding_credits += nblocks;
224 transaction->t_updates++;
225 transaction->t_handle_count++;
226 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
227 handle, nblocks, transaction->t_outstanding_credits,
228 __log_space_left(journal));
229 spin_unlock(&transaction->t_handle_lock);
230 spin_unlock(&journal->j_state_lock);
232 if (unlikely(new_transaction)) /* It's usually NULL */
233 kfree(new_transaction);
237 static struct lock_class_key jbd_handle_key;
239 /* Allocate a new handle. This should probably be in a slab... */
240 static handle_t *new_handle(int nblocks)
242 handle_t *handle = jbd_alloc_handle(GFP_NOFS);
245 memset(handle, 0, sizeof(*handle));
246 handle->h_buffer_credits = nblocks;
249 lockdep_init_map(&handle->h_lockdep_map, "jbd_handle", &jbd_handle_key, 0);
255 * handle_t *journal_start() - Obtain a new handle.
256 * @journal: Journal to start transaction on.
257 * @nblocks: number of block buffer we might modify
259 * We make sure that the transaction can guarantee at least nblocks of
260 * modified buffers in the log. We block until the log can guarantee
263 * This function is visible to journal users (like ext3fs), so is not
264 * called with the journal already locked.
266 * Return a pointer to a newly allocated handle, or NULL on failure
268 handle_t *journal_start(journal_t *journal, int nblocks)
270 handle_t *handle = journal_current_handle();
274 return ERR_PTR(-EROFS);
277 J_ASSERT(handle->h_transaction->t_journal == journal);
282 handle = new_handle(nblocks);
284 return ERR_PTR(-ENOMEM);
286 current->journal_info = handle;
288 err = start_this_handle(journal, handle);
290 jbd_free_handle(handle);
291 current->journal_info = NULL;
292 handle = ERR_PTR(err);
296 lock_map_acquire(&handle->h_lockdep_map);
303 * int journal_extend() - extend buffer credits.
304 * @handle: handle to 'extend'
305 * @nblocks: nr blocks to try to extend by.
307 * Some transactions, such as large extends and truncates, can be done
308 * atomically all at once or in several stages. The operation requests
309 * a credit for a number of buffer modications in advance, but can
310 * extend its credit if it needs more.
312 * journal_extend tries to give the running handle more buffer credits.
313 * It does not guarantee that allocation - this is a best-effort only.
314 * The calling process MUST be able to deal cleanly with a failure to
317 * Return 0 on success, non-zero on failure.
319 * return code < 0 implies an error
320 * return code > 0 implies normal transaction-full status.
322 int journal_extend(handle_t *handle, int nblocks)
324 transaction_t *transaction = handle->h_transaction;
325 journal_t *journal = transaction->t_journal;
330 if (is_handle_aborted(handle))
335 spin_lock(&journal->j_state_lock);
337 /* Don't extend a locked-down transaction! */
338 if (handle->h_transaction->t_state != T_RUNNING) {
339 jbd_debug(3, "denied handle %p %d blocks: "
340 "transaction not running\n", handle, nblocks);
344 spin_lock(&transaction->t_handle_lock);
345 wanted = transaction->t_outstanding_credits + nblocks;
347 if (wanted > journal->j_max_transaction_buffers) {
348 jbd_debug(3, "denied handle %p %d blocks: "
349 "transaction too large\n", handle, nblocks);
353 if (wanted > __log_space_left(journal)) {
354 jbd_debug(3, "denied handle %p %d blocks: "
355 "insufficient log space\n", handle, nblocks);
359 handle->h_buffer_credits += nblocks;
360 transaction->t_outstanding_credits += nblocks;
363 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
365 spin_unlock(&transaction->t_handle_lock);
367 spin_unlock(&journal->j_state_lock);
374 * int journal_restart() - restart a handle.
375 * @handle: handle to restart
376 * @nblocks: nr credits requested
378 * Restart a handle for a multi-transaction filesystem
381 * If the journal_extend() call above fails to grant new buffer credits
382 * to a running handle, a call to journal_restart will commit the
383 * handle's transaction so far and reattach the handle to a new
384 * transaction capabable of guaranteeing the requested number of
388 int journal_restart(handle_t *handle, int nblocks)
390 transaction_t *transaction = handle->h_transaction;
391 journal_t *journal = transaction->t_journal;
394 /* If we've had an abort of any type, don't even think about
395 * actually doing the restart! */
396 if (is_handle_aborted(handle))
400 * First unlink the handle from its current transaction, and start the
403 J_ASSERT(transaction->t_updates > 0);
404 J_ASSERT(journal_current_handle() == handle);
406 spin_lock(&journal->j_state_lock);
407 spin_lock(&transaction->t_handle_lock);
408 transaction->t_outstanding_credits -= handle->h_buffer_credits;
409 transaction->t_updates--;
411 if (!transaction->t_updates)
412 wake_up(&journal->j_wait_updates);
413 spin_unlock(&transaction->t_handle_lock);
415 jbd_debug(2, "restarting handle %p\n", handle);
416 __log_start_commit(journal, transaction->t_tid);
417 spin_unlock(&journal->j_state_lock);
419 handle->h_buffer_credits = nblocks;
420 ret = start_this_handle(journal, handle);
426 * void journal_lock_updates () - establish a transaction barrier.
427 * @journal: Journal to establish a barrier on.
429 * This locks out any further updates from being started, and blocks
430 * until all existing updates have completed, returning only once the
431 * journal is in a quiescent state with no updates running.
433 * The journal lock should not be held on entry.
435 void journal_lock_updates(journal_t *journal)
439 spin_lock(&journal->j_state_lock);
440 ++journal->j_barrier_count;
442 /* Wait until there are no running updates */
444 transaction_t *transaction = journal->j_running_transaction;
449 spin_lock(&transaction->t_handle_lock);
450 if (!transaction->t_updates) {
451 spin_unlock(&transaction->t_handle_lock);
454 prepare_to_wait(&journal->j_wait_updates, &wait,
455 TASK_UNINTERRUPTIBLE);
456 spin_unlock(&transaction->t_handle_lock);
457 spin_unlock(&journal->j_state_lock);
459 finish_wait(&journal->j_wait_updates, &wait);
460 spin_lock(&journal->j_state_lock);
462 spin_unlock(&journal->j_state_lock);
465 * We have now established a barrier against other normal updates, but
466 * we also need to barrier against other journal_lock_updates() calls
467 * to make sure that we serialise special journal-locked operations
470 mutex_lock(&journal->j_barrier);
474 * void journal_unlock_updates (journal_t* journal) - release barrier
475 * @journal: Journal to release the barrier on.
477 * Release a transaction barrier obtained with journal_lock_updates().
479 * Should be called without the journal lock held.
481 void journal_unlock_updates (journal_t *journal)
483 J_ASSERT(journal->j_barrier_count != 0);
485 mutex_unlock(&journal->j_barrier);
486 spin_lock(&journal->j_state_lock);
487 --journal->j_barrier_count;
488 spin_unlock(&journal->j_state_lock);
489 wake_up(&journal->j_wait_transaction_locked);
493 * Report any unexpected dirty buffers which turn up. Normally those
494 * indicate an error, but they can occur if the user is running (say)
495 * tune2fs to modify the live filesystem, so we need the option of
496 * continuing as gracefully as possible. #
498 * The caller should already hold the journal lock and
499 * j_list_lock spinlock: most callers will need those anyway
500 * in order to probe the buffer's journaling state safely.
502 static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
506 /* If this buffer is one which might reasonably be dirty
507 * --- ie. data, or not part of this journal --- then
508 * we're OK to leave it alone, but otherwise we need to
509 * move the dirty bit to the journal's own internal
513 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
514 jlist == BJ_Shadow || jlist == BJ_Forget) {
515 struct buffer_head *bh = jh2bh(jh);
517 if (test_clear_buffer_dirty(bh))
518 set_buffer_jbddirty(bh);
523 * If the buffer is already part of the current transaction, then there
524 * is nothing we need to do. If it is already part of a prior
525 * transaction which we are still committing to disk, then we need to
526 * make sure that we do not overwrite the old copy: we do copy-out to
527 * preserve the copy going to disk. We also account the buffer against
528 * the handle's metadata buffer credits (unless the buffer is already
529 * part of the transaction, that is).
533 do_get_write_access(handle_t *handle, struct journal_head *jh,
536 struct buffer_head *bh;
537 transaction_t *transaction;
540 char *frozen_buffer = NULL;
543 if (is_handle_aborted(handle))
546 transaction = handle->h_transaction;
547 journal = transaction->t_journal;
549 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
551 JBUFFER_TRACE(jh, "entry");
555 /* @@@ Need to check for errors here at some point. */
558 jbd_lock_bh_state(bh);
560 /* We now hold the buffer lock so it is safe to query the buffer
561 * state. Is the buffer dirty?
563 * If so, there are two possibilities. The buffer may be
564 * non-journaled, and undergoing a quite legitimate writeback.
565 * Otherwise, it is journaled, and we don't expect dirty buffers
566 * in that state (the buffers should be marked JBD_Dirty
567 * instead.) So either the IO is being done under our own
568 * control and this is a bug, or it's a third party IO such as
569 * dump(8) (which may leave the buffer scheduled for read ---
570 * ie. locked but not dirty) or tune2fs (which may actually have
571 * the buffer dirtied, ugh.) */
573 if (buffer_dirty(bh)) {
575 * First question: is this buffer already part of the current
576 * transaction or the existing committing transaction?
578 if (jh->b_transaction) {
580 jh->b_transaction == transaction ||
582 journal->j_committing_transaction);
583 if (jh->b_next_transaction)
584 J_ASSERT_JH(jh, jh->b_next_transaction ==
588 * In any case we need to clean the dirty flag and we must
589 * do it under the buffer lock to be sure we don't race
590 * with running write-out.
592 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
593 jbd_unexpected_dirty_buffer(jh);
599 if (is_handle_aborted(handle)) {
600 jbd_unlock_bh_state(bh);
606 * The buffer is already part of this transaction if b_transaction or
607 * b_next_transaction points to it
609 if (jh->b_transaction == transaction ||
610 jh->b_next_transaction == transaction)
614 * this is the first time this transaction is touching this buffer,
615 * reset the modified flag
620 * If there is already a copy-out version of this buffer, then we don't
621 * need to make another one
623 if (jh->b_frozen_data) {
624 JBUFFER_TRACE(jh, "has frozen data");
625 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
626 jh->b_next_transaction = transaction;
630 /* Is there data here we need to preserve? */
632 if (jh->b_transaction && jh->b_transaction != transaction) {
633 JBUFFER_TRACE(jh, "owned by older transaction");
634 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
635 J_ASSERT_JH(jh, jh->b_transaction ==
636 journal->j_committing_transaction);
638 /* There is one case we have to be very careful about.
639 * If the committing transaction is currently writing
640 * this buffer out to disk and has NOT made a copy-out,
641 * then we cannot modify the buffer contents at all
642 * right now. The essence of copy-out is that it is the
643 * extra copy, not the primary copy, which gets
644 * journaled. If the primary copy is already going to
645 * disk then we cannot do copy-out here. */
647 if (jh->b_jlist == BJ_Shadow) {
648 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
649 wait_queue_head_t *wqh;
651 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
653 JBUFFER_TRACE(jh, "on shadow: sleep");
654 jbd_unlock_bh_state(bh);
655 /* commit wakes up all shadow buffers after IO */
657 prepare_to_wait(wqh, &wait.wait,
658 TASK_UNINTERRUPTIBLE);
659 if (jh->b_jlist != BJ_Shadow)
663 finish_wait(wqh, &wait.wait);
667 /* Only do the copy if the currently-owning transaction
668 * still needs it. If it is on the Forget list, the
669 * committing transaction is past that stage. The
670 * buffer had better remain locked during the kmalloc,
671 * but that should be true --- we hold the journal lock
672 * still and the buffer is already on the BUF_JOURNAL
673 * list so won't be flushed.
675 * Subtle point, though: if this is a get_undo_access,
676 * then we will be relying on the frozen_data to contain
677 * the new value of the committed_data record after the
678 * transaction, so we HAVE to force the frozen_data copy
681 if (jh->b_jlist != BJ_Forget || force_copy) {
682 JBUFFER_TRACE(jh, "generate frozen data");
683 if (!frozen_buffer) {
684 JBUFFER_TRACE(jh, "allocate memory for buffer");
685 jbd_unlock_bh_state(bh);
687 jbd_alloc(jh2bh(jh)->b_size,
689 if (!frozen_buffer) {
691 "%s: OOM for frozen_buffer\n",
693 JBUFFER_TRACE(jh, "oom!");
695 jbd_lock_bh_state(bh);
700 jh->b_frozen_data = frozen_buffer;
701 frozen_buffer = NULL;
704 jh->b_next_transaction = transaction;
709 * Finally, if the buffer is not journaled right now, we need to make
710 * sure it doesn't get written to disk before the caller actually
711 * commits the new data
713 if (!jh->b_transaction) {
714 JBUFFER_TRACE(jh, "no transaction");
715 J_ASSERT_JH(jh, !jh->b_next_transaction);
716 jh->b_transaction = transaction;
717 JBUFFER_TRACE(jh, "file as BJ_Reserved");
718 spin_lock(&journal->j_list_lock);
719 __journal_file_buffer(jh, transaction, BJ_Reserved);
720 spin_unlock(&journal->j_list_lock);
729 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
730 "Possible IO failure.\n");
731 page = jh2bh(jh)->b_page;
732 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
733 source = kmap_atomic(page, KM_USER0);
734 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
735 kunmap_atomic(source, KM_USER0);
737 jbd_unlock_bh_state(bh);
740 * If we are about to journal a buffer, then any revoke pending on it is
743 journal_cancel_revoke(handle, jh);
746 if (unlikely(frozen_buffer)) /* It's usually NULL */
747 jbd_free(frozen_buffer, bh->b_size);
749 JBUFFER_TRACE(jh, "exit");
754 * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
755 * @handle: transaction to add buffer modifications to
756 * @bh: bh to be used for metadata writes
757 * @credits: variable that will receive credits for the buffer
759 * Returns an error code or 0 on success.
761 * In full data journalling mode the buffer may be of type BJ_AsyncData,
762 * because we're write()ing a buffer which is also part of a shared mapping.
765 int journal_get_write_access(handle_t *handle, struct buffer_head *bh)
767 struct journal_head *jh = journal_add_journal_head(bh);
770 /* We do not want to get caught playing with fields which the
771 * log thread also manipulates. Make sure that the buffer
772 * completes any outstanding IO before proceeding. */
773 rc = do_get_write_access(handle, jh, 0);
774 journal_put_journal_head(jh);
780 * When the user wants to journal a newly created buffer_head
781 * (ie. getblk() returned a new buffer and we are going to populate it
782 * manually rather than reading off disk), then we need to keep the
783 * buffer_head locked until it has been completely filled with new
784 * data. In this case, we should be able to make the assertion that
785 * the bh is not already part of an existing transaction.
787 * The buffer should already be locked by the caller by this point.
788 * There is no lock ranking violation: it was a newly created,
789 * unlocked buffer beforehand. */
792 * int journal_get_create_access () - notify intent to use newly created bh
793 * @handle: transaction to new buffer to
796 * Call this if you create a new bh.
798 int journal_get_create_access(handle_t *handle, struct buffer_head *bh)
800 transaction_t *transaction = handle->h_transaction;
801 journal_t *journal = transaction->t_journal;
802 struct journal_head *jh = journal_add_journal_head(bh);
805 jbd_debug(5, "journal_head %p\n", jh);
807 if (is_handle_aborted(handle))
811 JBUFFER_TRACE(jh, "entry");
813 * The buffer may already belong to this transaction due to pre-zeroing
814 * in the filesystem's new_block code. It may also be on the previous,
815 * committing transaction's lists, but it HAS to be in Forget state in
816 * that case: the transaction must have deleted the buffer for it to be
819 jbd_lock_bh_state(bh);
820 spin_lock(&journal->j_list_lock);
821 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
822 jh->b_transaction == NULL ||
823 (jh->b_transaction == journal->j_committing_transaction &&
824 jh->b_jlist == BJ_Forget)));
826 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
827 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
829 if (jh->b_transaction == NULL) {
830 jh->b_transaction = transaction;
832 /* first access by this transaction */
835 JBUFFER_TRACE(jh, "file as BJ_Reserved");
836 __journal_file_buffer(jh, transaction, BJ_Reserved);
837 } else if (jh->b_transaction == journal->j_committing_transaction) {
838 /* first access by this transaction */
841 JBUFFER_TRACE(jh, "set next transaction");
842 jh->b_next_transaction = transaction;
844 spin_unlock(&journal->j_list_lock);
845 jbd_unlock_bh_state(bh);
848 * akpm: I added this. ext3_alloc_branch can pick up new indirect
849 * blocks which contain freed but then revoked metadata. We need
850 * to cancel the revoke in case we end up freeing it yet again
851 * and the reallocating as data - this would cause a second revoke,
852 * which hits an assertion error.
854 JBUFFER_TRACE(jh, "cancelling revoke");
855 journal_cancel_revoke(handle, jh);
856 journal_put_journal_head(jh);
862 * int journal_get_undo_access() - Notify intent to modify metadata with non-rewindable consequences
863 * @handle: transaction
864 * @bh: buffer to undo
866 * Sometimes there is a need to distinguish between metadata which has
867 * been committed to disk and that which has not. The ext3fs code uses
868 * this for freeing and allocating space, we have to make sure that we
869 * do not reuse freed space until the deallocation has been committed,
870 * since if we overwrote that space we would make the delete
871 * un-rewindable in case of a crash.
873 * To deal with that, journal_get_undo_access requests write access to a
874 * buffer for parts of non-rewindable operations such as delete
875 * operations on the bitmaps. The journaling code must keep a copy of
876 * the buffer's contents prior to the undo_access call until such time
877 * as we know that the buffer has definitely been committed to disk.
879 * We never need to know which transaction the committed data is part
880 * of, buffers touched here are guaranteed to be dirtied later and so
881 * will be committed to a new transaction in due course, at which point
882 * we can discard the old committed data pointer.
884 * Returns error number or 0 on success.
886 int journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
889 struct journal_head *jh = journal_add_journal_head(bh);
890 char *committed_data = NULL;
892 JBUFFER_TRACE(jh, "entry");
895 * Do this first --- it can drop the journal lock, so we want to
896 * make sure that obtaining the committed_data is done
897 * atomically wrt. completion of any outstanding commits.
899 err = do_get_write_access(handle, jh, 1);
904 if (!jh->b_committed_data) {
905 committed_data = jbd_alloc(jh2bh(jh)->b_size, GFP_NOFS);
906 if (!committed_data) {
907 printk(KERN_EMERG "%s: No memory for committed data\n",
914 jbd_lock_bh_state(bh);
915 if (!jh->b_committed_data) {
916 /* Copy out the current buffer contents into the
917 * preserved, committed copy. */
918 JBUFFER_TRACE(jh, "generate b_committed data");
919 if (!committed_data) {
920 jbd_unlock_bh_state(bh);
924 jh->b_committed_data = committed_data;
925 committed_data = NULL;
926 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
928 jbd_unlock_bh_state(bh);
930 journal_put_journal_head(jh);
931 if (unlikely(committed_data))
932 jbd_free(committed_data, bh->b_size);
937 * int journal_dirty_data() - mark a buffer as containing dirty data to be flushed
938 * @handle: transaction
939 * @bh: bufferhead to mark
942 * Mark a buffer as containing dirty data which needs to be flushed before
943 * we can commit the current transaction.
945 * The buffer is placed on the transaction's data list and is marked as
946 * belonging to the transaction.
948 * Returns error number or 0 on success.
950 * journal_dirty_data() can be called via page_launder->ext3_writepage
953 int journal_dirty_data(handle_t *handle, struct buffer_head *bh)
955 journal_t *journal = handle->h_transaction->t_journal;
957 struct journal_head *jh;
960 if (is_handle_aborted(handle))
963 jh = journal_add_journal_head(bh);
964 JBUFFER_TRACE(jh, "entry");
967 * The buffer could *already* be dirty. Writeout can start
970 jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
973 * What if the buffer is already part of a running transaction?
975 * There are two cases:
976 * 1) It is part of the current running transaction. Refile it,
977 * just in case we have allocated it as metadata, deallocated
978 * it, then reallocated it as data.
979 * 2) It is part of the previous, still-committing transaction.
980 * If all we want to do is to guarantee that the buffer will be
981 * written to disk before this new transaction commits, then
982 * being sure that the *previous* transaction has this same
983 * property is sufficient for us! Just leave it on its old
986 * In case (2), the buffer must not already exist as metadata
987 * --- that would violate write ordering (a transaction is free
988 * to write its data at any point, even before the previous
989 * committing transaction has committed). The caller must
990 * never, ever allow this to happen: there's nothing we can do
991 * about it in this layer.
993 jbd_lock_bh_state(bh);
994 spin_lock(&journal->j_list_lock);
996 /* Now that we have bh_state locked, are we really still mapped? */
997 if (!buffer_mapped(bh)) {
998 JBUFFER_TRACE(jh, "unmapped buffer, bailing out");
1002 if (jh->b_transaction) {
1003 JBUFFER_TRACE(jh, "has transaction");
1004 if (jh->b_transaction != handle->h_transaction) {
1005 JBUFFER_TRACE(jh, "belongs to older transaction");
1006 J_ASSERT_JH(jh, jh->b_transaction ==
1007 journal->j_committing_transaction);
1009 /* @@@ IS THIS TRUE ? */
1011 * Not any more. Scenario: someone does a write()
1012 * in data=journal mode. The buffer's transaction has
1013 * moved into commit. Then someone does another
1014 * write() to the file. We do the frozen data copyout
1015 * and set b_next_transaction to point to j_running_t.
1016 * And while we're in that state, someone does a
1017 * writepage() in an attempt to pageout the same area
1018 * of the file via a shared mapping. At present that
1019 * calls journal_dirty_data(), and we get right here.
1020 * It may be too late to journal the data. Simply
1021 * falling through to the next test will suffice: the
1022 * data will be dirty and wil be checkpointed. The
1023 * ordering comments in the next comment block still
1026 //J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1029 * If we're journalling data, and this buffer was
1030 * subject to a write(), it could be metadata, forget
1031 * or shadow against the committing transaction. Now,
1032 * someone has dirtied the same darn page via a mapping
1033 * and it is being writepage()'d.
1034 * We *could* just steal the page from commit, with some
1035 * fancy locking there. Instead, we just skip it -
1036 * don't tie the page's buffers to the new transaction
1038 * Implication: if we crash before the writepage() data
1039 * is written into the filesystem, recovery will replay
1042 if (jh->b_jlist != BJ_None &&
1043 jh->b_jlist != BJ_SyncData &&
1044 jh->b_jlist != BJ_Locked) {
1045 JBUFFER_TRACE(jh, "Not stealing");
1050 * This buffer may be undergoing writeout in commit. We
1051 * can't return from here and let the caller dirty it
1052 * again because that can cause the write-out loop in
1053 * commit to never terminate.
1055 if (buffer_dirty(bh)) {
1057 spin_unlock(&journal->j_list_lock);
1058 jbd_unlock_bh_state(bh);
1060 sync_dirty_buffer(bh);
1061 jbd_lock_bh_state(bh);
1062 spin_lock(&journal->j_list_lock);
1063 /* Since we dropped the lock... */
1064 if (!buffer_mapped(bh)) {
1065 JBUFFER_TRACE(jh, "buffer got unmapped");
1068 /* The buffer may become locked again at any
1069 time if it is redirtied */
1073 * We cannot remove the buffer with io error from the
1074 * committing transaction, because otherwise it would
1075 * miss the error and the commit would not abort.
1077 if (unlikely(!buffer_uptodate(bh))) {
1082 if (jh->b_transaction != NULL) {
1083 JBUFFER_TRACE(jh, "unfile from commit");
1084 __journal_temp_unlink_buffer(jh);
1085 /* It still points to the committing
1086 * transaction; move it to this one so
1087 * that the refile assert checks are
1089 jh->b_transaction = handle->h_transaction;
1091 /* The buffer will be refiled below */
1095 * Special case --- the buffer might actually have been
1096 * allocated and then immediately deallocated in the previous,
1097 * committing transaction, so might still be left on that
1098 * transaction's metadata lists.
1100 if (jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_Locked) {
1101 JBUFFER_TRACE(jh, "not on correct data list: unfile");
1102 J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
1103 __journal_temp_unlink_buffer(jh);
1104 jh->b_transaction = handle->h_transaction;
1105 JBUFFER_TRACE(jh, "file as data");
1106 __journal_file_buffer(jh, handle->h_transaction,
1110 JBUFFER_TRACE(jh, "not on a transaction");
1111 __journal_file_buffer(jh, handle->h_transaction, BJ_SyncData);
1114 spin_unlock(&journal->j_list_lock);
1115 jbd_unlock_bh_state(bh);
1117 BUFFER_TRACE(bh, "brelse");
1120 JBUFFER_TRACE(jh, "exit");
1121 journal_put_journal_head(jh);
1126 * int journal_dirty_metadata() - mark a buffer as containing dirty metadata
1127 * @handle: transaction to add buffer to.
1128 * @bh: buffer to mark
1130 * Mark dirty metadata which needs to be journaled as part of the current
1133 * The buffer is placed on the transaction's metadata list and is marked
1134 * as belonging to the transaction.
1136 * Returns error number or 0 on success.
1138 * Special care needs to be taken if the buffer already belongs to the
1139 * current committing transaction (in which case we should have frozen
1140 * data present for that commit). In that case, we don't relink the
1141 * buffer: that only gets done when the old transaction finally
1142 * completes its commit.
1144 int journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1146 transaction_t *transaction = handle->h_transaction;
1147 journal_t *journal = transaction->t_journal;
1148 struct journal_head *jh = bh2jh(bh);
1150 jbd_debug(5, "journal_head %p\n", jh);
1151 JBUFFER_TRACE(jh, "entry");
1152 if (is_handle_aborted(handle))
1155 jbd_lock_bh_state(bh);
1157 if (jh->b_modified == 0) {
1159 * This buffer's got modified and becoming part
1160 * of the transaction. This needs to be done
1161 * once a transaction -bzzz
1164 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1165 handle->h_buffer_credits--;
1169 * fastpath, to avoid expensive locking. If this buffer is already
1170 * on the running transaction's metadata list there is nothing to do.
1171 * Nobody can take it off again because there is a handle open.
1172 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1173 * result in this test being false, so we go in and take the locks.
1175 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1176 JBUFFER_TRACE(jh, "fastpath");
1177 J_ASSERT_JH(jh, jh->b_transaction ==
1178 journal->j_running_transaction);
1182 set_buffer_jbddirty(bh);
1185 * Metadata already on the current transaction list doesn't
1186 * need to be filed. Metadata on another transaction's list must
1187 * be committing, and will be refiled once the commit completes:
1188 * leave it alone for now.
1190 if (jh->b_transaction != transaction) {
1191 JBUFFER_TRACE(jh, "already on other transaction");
1192 J_ASSERT_JH(jh, jh->b_transaction ==
1193 journal->j_committing_transaction);
1194 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1195 /* And this case is illegal: we can't reuse another
1196 * transaction's data buffer, ever. */
1200 /* That test should have eliminated the following case: */
1201 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1203 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1204 spin_lock(&journal->j_list_lock);
1205 __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1206 spin_unlock(&journal->j_list_lock);
1208 jbd_unlock_bh_state(bh);
1210 JBUFFER_TRACE(jh, "exit");
1215 * journal_release_buffer: undo a get_write_access without any buffer
1216 * updates, if the update decided in the end that it didn't need access.
1220 journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1222 BUFFER_TRACE(bh, "entry");
1226 * void journal_forget() - bforget() for potentially-journaled buffers.
1227 * @handle: transaction handle
1228 * @bh: bh to 'forget'
1230 * We can only do the bforget if there are no commits pending against the
1231 * buffer. If the buffer is dirty in the current running transaction we
1232 * can safely unlink it.
1234 * bh may not be a journalled buffer at all - it may be a non-JBD
1235 * buffer which came off the hashtable. Check for this.
1237 * Decrements bh->b_count by one.
1239 * Allow this call even if the handle has aborted --- it may be part of
1240 * the caller's cleanup after an abort.
1242 int journal_forget (handle_t *handle, struct buffer_head *bh)
1244 transaction_t *transaction = handle->h_transaction;
1245 journal_t *journal = transaction->t_journal;
1246 struct journal_head *jh;
1247 int drop_reserve = 0;
1249 int was_modified = 0;
1251 BUFFER_TRACE(bh, "entry");
1253 jbd_lock_bh_state(bh);
1254 spin_lock(&journal->j_list_lock);
1256 if (!buffer_jbd(bh))
1260 /* Critical error: attempting to delete a bitmap buffer, maybe?
1261 * Don't do any jbd operations, and return an error. */
1262 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1263 "inconsistent data on disk")) {
1268 /* keep track of wether or not this transaction modified us */
1269 was_modified = jh->b_modified;
1272 * The buffer's going from the transaction, we must drop
1273 * all references -bzzz
1277 if (jh->b_transaction == handle->h_transaction) {
1278 J_ASSERT_JH(jh, !jh->b_frozen_data);
1280 /* If we are forgetting a buffer which is already part
1281 * of this transaction, then we can just drop it from
1282 * the transaction immediately. */
1283 clear_buffer_dirty(bh);
1284 clear_buffer_jbddirty(bh);
1286 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1289 * we only want to drop a reference if this transaction
1290 * modified the buffer
1296 * We are no longer going to journal this buffer.
1297 * However, the commit of this transaction is still
1298 * important to the buffer: the delete that we are now
1299 * processing might obsolete an old log entry, so by
1300 * committing, we can satisfy the buffer's checkpoint.
1302 * So, if we have a checkpoint on the buffer, we should
1303 * now refile the buffer on our BJ_Forget list so that
1304 * we know to remove the checkpoint after we commit.
1307 if (jh->b_cp_transaction) {
1308 __journal_temp_unlink_buffer(jh);
1309 __journal_file_buffer(jh, transaction, BJ_Forget);
1311 __journal_unfile_buffer(jh);
1312 journal_remove_journal_head(bh);
1314 if (!buffer_jbd(bh)) {
1315 spin_unlock(&journal->j_list_lock);
1316 jbd_unlock_bh_state(bh);
1321 } else if (jh->b_transaction) {
1322 J_ASSERT_JH(jh, (jh->b_transaction ==
1323 journal->j_committing_transaction));
1324 /* However, if the buffer is still owned by a prior
1325 * (committing) transaction, we can't drop it yet... */
1326 JBUFFER_TRACE(jh, "belongs to older transaction");
1327 /* ... but we CAN drop it from the new transaction if we
1328 * have also modified it since the original commit. */
1330 if (jh->b_next_transaction) {
1331 J_ASSERT(jh->b_next_transaction == transaction);
1332 jh->b_next_transaction = NULL;
1335 * only drop a reference if this transaction modified
1344 spin_unlock(&journal->j_list_lock);
1345 jbd_unlock_bh_state(bh);
1349 /* no need to reserve log space for this block -bzzz */
1350 handle->h_buffer_credits++;
1356 * int journal_stop() - complete a transaction
1357 * @handle: tranaction to complete.
1359 * All done for a particular handle.
1361 * There is not much action needed here. We just return any remaining
1362 * buffer credits to the transaction and remove the handle. The only
1363 * complication is that we need to start a commit operation if the
1364 * filesystem is marked for synchronous update.
1366 * journal_stop itself will not usually return an error, but it may
1367 * do so in unusual circumstances. In particular, expect it to
1368 * return -EIO if a journal_abort has been executed since the
1369 * transaction began.
1371 int journal_stop(handle_t *handle)
1373 transaction_t *transaction = handle->h_transaction;
1374 journal_t *journal = transaction->t_journal;
1378 J_ASSERT(journal_current_handle() == handle);
1380 if (is_handle_aborted(handle))
1383 J_ASSERT(transaction->t_updates > 0);
1387 if (--handle->h_ref > 0) {
1388 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1393 jbd_debug(4, "Handle %p going down\n", handle);
1396 * Implement synchronous transaction batching. If the handle
1397 * was synchronous, don't force a commit immediately. Let's
1398 * yield and let another thread piggyback onto this transaction.
1399 * Keep doing that while new threads continue to arrive.
1400 * It doesn't cost much - we're about to run a commit and sleep
1401 * on IO anyway. Speeds up many-threaded, many-dir operations
1404 * We try and optimize the sleep time against what the underlying disk
1405 * can do, instead of having a static sleep time. This is usefull for
1406 * the case where our storage is so fast that it is more optimal to go
1407 * ahead and force a flush and wait for the transaction to be committed
1408 * than it is to wait for an arbitrary amount of time for new writers to
1409 * join the transaction. We acheive this by measuring how long it takes
1410 * to commit a transaction, and compare it with how long this
1411 * transaction has been running, and if run time < commit time then we
1412 * sleep for the delta and commit. This greatly helps super fast disks
1413 * that would see slowdowns as more threads started doing fsyncs.
1415 * But don't do this if this process was the most recent one to
1416 * perform a synchronous write. We do this to detect the case where a
1417 * single process is doing a stream of sync writes. No point in waiting
1418 * for joiners in that case.
1421 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1422 u64 commit_time, trans_time;
1424 journal->j_last_sync_writer = pid;
1426 spin_lock(&journal->j_state_lock);
1427 commit_time = journal->j_average_commit_time;
1428 spin_unlock(&journal->j_state_lock);
1430 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1431 transaction->t_start_time));
1433 commit_time = min_t(u64, commit_time,
1434 1000*jiffies_to_usecs(1));
1436 if (trans_time < commit_time) {
1437 ktime_t expires = ktime_add_ns(ktime_get(),
1439 set_current_state(TASK_UNINTERRUPTIBLE);
1440 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1444 current->journal_info = NULL;
1445 spin_lock(&journal->j_state_lock);
1446 spin_lock(&transaction->t_handle_lock);
1447 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1448 transaction->t_updates--;
1449 if (!transaction->t_updates) {
1450 wake_up(&journal->j_wait_updates);
1451 if (journal->j_barrier_count)
1452 wake_up(&journal->j_wait_transaction_locked);
1456 * If the handle is marked SYNC, we need to set another commit
1457 * going! We also want to force a commit if the current
1458 * transaction is occupying too much of the log, or if the
1459 * transaction is too old now.
1461 if (handle->h_sync ||
1462 transaction->t_outstanding_credits >
1463 journal->j_max_transaction_buffers ||
1464 time_after_eq(jiffies, transaction->t_expires)) {
1465 /* Do this even for aborted journals: an abort still
1466 * completes the commit thread, it just doesn't write
1467 * anything to disk. */
1468 tid_t tid = transaction->t_tid;
1470 spin_unlock(&transaction->t_handle_lock);
1471 jbd_debug(2, "transaction too old, requesting commit for "
1472 "handle %p\n", handle);
1473 /* This is non-blocking */
1474 __log_start_commit(journal, transaction->t_tid);
1475 spin_unlock(&journal->j_state_lock);
1478 * Special case: JFS_SYNC synchronous updates require us
1479 * to wait for the commit to complete.
1481 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1482 err = log_wait_commit(journal, tid);
1484 spin_unlock(&transaction->t_handle_lock);
1485 spin_unlock(&journal->j_state_lock);
1488 lock_map_release(&handle->h_lockdep_map);
1490 jbd_free_handle(handle);
1495 * int journal_force_commit() - force any uncommitted transactions
1496 * @journal: journal to force
1498 * For synchronous operations: force any uncommitted transactions
1499 * to disk. May seem kludgy, but it reuses all the handle batching
1500 * code in a very simple manner.
1502 int journal_force_commit(journal_t *journal)
1507 handle = journal_start(journal, 1);
1508 if (IS_ERR(handle)) {
1509 ret = PTR_ERR(handle);
1512 ret = journal_stop(handle);
1519 * List management code snippets: various functions for manipulating the
1520 * transaction buffer lists.
1525 * Append a buffer to a transaction list, given the transaction's list head
1528 * j_list_lock is held.
1530 * jbd_lock_bh_state(jh2bh(jh)) is held.
1534 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1537 jh->b_tnext = jh->b_tprev = jh;
1540 /* Insert at the tail of the list to preserve order */
1541 struct journal_head *first = *list, *last = first->b_tprev;
1543 jh->b_tnext = first;
1544 last->b_tnext = first->b_tprev = jh;
1549 * Remove a buffer from a transaction list, given the transaction's list
1552 * Called with j_list_lock held, and the journal may not be locked.
1554 * jbd_lock_bh_state(jh2bh(jh)) is held.
1558 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1561 *list = jh->b_tnext;
1565 jh->b_tprev->b_tnext = jh->b_tnext;
1566 jh->b_tnext->b_tprev = jh->b_tprev;
1570 * Remove a buffer from the appropriate transaction list.
1572 * Note that this function can *change* the value of
1573 * bh->b_transaction->t_sync_datalist, t_buffers, t_forget,
1574 * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
1575 * is holding onto a copy of one of thee pointers, it could go bad.
1576 * Generally the caller needs to re-read the pointer from the transaction_t.
1578 * Called under j_list_lock. The journal may not be locked.
1580 static void __journal_temp_unlink_buffer(struct journal_head *jh)
1582 struct journal_head **list = NULL;
1583 transaction_t *transaction;
1584 struct buffer_head *bh = jh2bh(jh);
1586 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1587 transaction = jh->b_transaction;
1589 assert_spin_locked(&transaction->t_journal->j_list_lock);
1591 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1592 if (jh->b_jlist != BJ_None)
1593 J_ASSERT_JH(jh, transaction != NULL);
1595 switch (jh->b_jlist) {
1599 list = &transaction->t_sync_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;
1622 list = &transaction->t_locked_list;
1626 __blist_del_buffer(list, jh);
1627 jh->b_jlist = BJ_None;
1628 if (test_clear_buffer_jbddirty(bh))
1629 mark_buffer_dirty(bh); /* Expose it to the VM */
1632 void __journal_unfile_buffer(struct journal_head *jh)
1634 __journal_temp_unlink_buffer(jh);
1635 jh->b_transaction = NULL;
1638 void journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1640 jbd_lock_bh_state(jh2bh(jh));
1641 spin_lock(&journal->j_list_lock);
1642 __journal_unfile_buffer(jh);
1643 spin_unlock(&journal->j_list_lock);
1644 jbd_unlock_bh_state(jh2bh(jh));
1648 * Called from journal_try_to_free_buffers().
1650 * Called under jbd_lock_bh_state(bh)
1653 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1655 struct journal_head *jh;
1659 if (buffer_locked(bh) || buffer_dirty(bh))
1662 if (jh->b_next_transaction != NULL)
1665 spin_lock(&journal->j_list_lock);
1666 if (jh->b_transaction != NULL && jh->b_cp_transaction == NULL) {
1667 if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) {
1668 /* A written-back ordered data buffer */
1669 JBUFFER_TRACE(jh, "release data");
1670 __journal_unfile_buffer(jh);
1671 journal_remove_journal_head(bh);
1674 } else if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1675 /* written-back checkpointed metadata buffer */
1676 if (jh->b_jlist == BJ_None) {
1677 JBUFFER_TRACE(jh, "remove from checkpoint list");
1678 __journal_remove_checkpoint(jh);
1679 journal_remove_journal_head(bh);
1683 spin_unlock(&journal->j_list_lock);
1689 * journal_try_to_free_buffers() could race with journal_commit_transaction()
1690 * The latter might still hold the a count on buffers when inspecting
1691 * them on t_syncdata_list or t_locked_list.
1693 * journal_try_to_free_buffers() will call this function to
1694 * wait for the current transaction to finish syncing data buffers, before
1695 * tryinf to free that buffer.
1697 * Called with journal->j_state_lock held.
1699 static void journal_wait_for_transaction_sync_data(journal_t *journal)
1701 transaction_t *transaction = NULL;
1704 spin_lock(&journal->j_state_lock);
1705 transaction = journal->j_committing_transaction;
1708 spin_unlock(&journal->j_state_lock);
1712 tid = transaction->t_tid;
1713 spin_unlock(&journal->j_state_lock);
1714 log_wait_commit(journal, tid);
1718 * int journal_try_to_free_buffers() - try to free page buffers.
1719 * @journal: journal for operation
1720 * @page: to try and free
1721 * @gfp_mask: we use the mask to detect how hard should we try to release
1722 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1723 * release the buffers.
1726 * For all the buffers on this page,
1727 * if they are fully written out ordered data, move them onto BUF_CLEAN
1728 * so try_to_free_buffers() can reap them.
1730 * This function returns non-zero if we wish try_to_free_buffers()
1731 * to be called. We do this if the page is releasable by try_to_free_buffers().
1732 * We also do it if the page has locked or dirty buffers and the caller wants
1733 * us to perform sync or async writeout.
1735 * This complicates JBD locking somewhat. We aren't protected by the
1736 * BKL here. We wish to remove the buffer from its committing or
1737 * running transaction's ->t_datalist via __journal_unfile_buffer.
1739 * This may *change* the value of transaction_t->t_datalist, so anyone
1740 * who looks at t_datalist needs to lock against this function.
1742 * Even worse, someone may be doing a journal_dirty_data on this
1743 * buffer. So we need to lock against that. journal_dirty_data()
1744 * will come out of the lock with the buffer dirty, which makes it
1745 * ineligible for release here.
1747 * Who else is affected by this? hmm... Really the only contender
1748 * is do_get_write_access() - it could be looking at the buffer while
1749 * journal_try_to_free_buffer() is changing its state. But that
1750 * cannot happen because we never reallocate freed data as metadata
1751 * while the data is part of a transaction. Yes?
1753 * Return 0 on failure, 1 on success
1755 int journal_try_to_free_buffers(journal_t *journal,
1756 struct page *page, gfp_t gfp_mask)
1758 struct buffer_head *head;
1759 struct buffer_head *bh;
1762 J_ASSERT(PageLocked(page));
1764 head = page_buffers(page);
1767 struct journal_head *jh;
1770 * We take our own ref against the journal_head here to avoid
1771 * having to add tons of locking around each instance of
1772 * journal_remove_journal_head() and journal_put_journal_head().
1774 jh = journal_grab_journal_head(bh);
1778 jbd_lock_bh_state(bh);
1779 __journal_try_to_free_buffer(journal, bh);
1780 journal_put_journal_head(jh);
1781 jbd_unlock_bh_state(bh);
1784 } while ((bh = bh->b_this_page) != head);
1786 ret = try_to_free_buffers(page);
1789 * There are a number of places where journal_try_to_free_buffers()
1790 * could race with journal_commit_transaction(), the later still
1791 * holds the reference to the buffers to free while processing them.
1792 * try_to_free_buffers() failed to free those buffers. Some of the
1793 * caller of releasepage() request page buffers to be dropped, otherwise
1794 * treat the fail-to-free as errors (such as generic_file_direct_IO())
1796 * So, if the caller of try_to_release_page() wants the synchronous
1797 * behaviour(i.e make sure buffers are dropped upon return),
1798 * let's wait for the current transaction to finish flush of
1799 * dirty data buffers, then try to free those buffers again,
1800 * with the journal locked.
1802 if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
1803 journal_wait_for_transaction_sync_data(journal);
1804 ret = try_to_free_buffers(page);
1812 * This buffer is no longer needed. If it is on an older transaction's
1813 * checkpoint list we need to record it on this transaction's forget list
1814 * to pin this buffer (and hence its checkpointing transaction) down until
1815 * this transaction commits. If the buffer isn't on a checkpoint list, we
1817 * Returns non-zero if JBD no longer has an interest in the buffer.
1819 * Called under j_list_lock.
1821 * Called under jbd_lock_bh_state(bh).
1823 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1826 struct buffer_head *bh = jh2bh(jh);
1828 __journal_unfile_buffer(jh);
1830 if (jh->b_cp_transaction) {
1831 JBUFFER_TRACE(jh, "on running+cp transaction");
1832 __journal_file_buffer(jh, transaction, BJ_Forget);
1833 clear_buffer_jbddirty(bh);
1836 JBUFFER_TRACE(jh, "on running transaction");
1837 journal_remove_journal_head(bh);
1844 * journal_invalidatepage
1846 * This code is tricky. It has a number of cases to deal with.
1848 * There are two invariants which this code relies on:
1850 * i_size must be updated on disk before we start calling invalidatepage on the
1853 * This is done in ext3 by defining an ext3_setattr method which
1854 * updates i_size before truncate gets going. By maintaining this
1855 * invariant, we can be sure that it is safe to throw away any buffers
1856 * attached to the current transaction: once the transaction commits,
1857 * we know that the data will not be needed.
1859 * Note however that we can *not* throw away data belonging to the
1860 * previous, committing transaction!
1862 * Any disk blocks which *are* part of the previous, committing
1863 * transaction (and which therefore cannot be discarded immediately) are
1864 * not going to be reused in the new running transaction
1866 * The bitmap committed_data images guarantee this: any block which is
1867 * allocated in one transaction and removed in the next will be marked
1868 * as in-use in the committed_data bitmap, so cannot be reused until
1869 * the next transaction to delete the block commits. This means that
1870 * leaving committing buffers dirty is quite safe: the disk blocks
1871 * cannot be reallocated to a different file and so buffer aliasing is
1875 * The above applies mainly to ordered data mode. In writeback mode we
1876 * don't make guarantees about the order in which data hits disk --- in
1877 * particular we don't guarantee that new dirty data is flushed before
1878 * transaction commit --- so it is always safe just to discard data
1879 * immediately in that mode. --sct
1883 * The journal_unmap_buffer helper function returns zero if the buffer
1884 * concerned remains pinned as an anonymous buffer belonging to an older
1887 * We're outside-transaction here. Either or both of j_running_transaction
1888 * and j_committing_transaction may be NULL.
1890 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1892 transaction_t *transaction;
1893 struct journal_head *jh;
1897 BUFFER_TRACE(bh, "entry");
1900 * It is safe to proceed here without the j_list_lock because the
1901 * buffers cannot be stolen by try_to_free_buffers as long as we are
1902 * holding the page lock. --sct
1905 if (!buffer_jbd(bh))
1906 goto zap_buffer_unlocked;
1908 spin_lock(&journal->j_state_lock);
1909 jbd_lock_bh_state(bh);
1910 spin_lock(&journal->j_list_lock);
1912 jh = journal_grab_journal_head(bh);
1914 goto zap_buffer_no_jh;
1916 transaction = jh->b_transaction;
1917 if (transaction == NULL) {
1918 /* First case: not on any transaction. If it
1919 * has no checkpoint link, then we can zap it:
1920 * it's a writeback-mode buffer so we don't care
1921 * if it hits disk safely. */
1922 if (!jh->b_cp_transaction) {
1923 JBUFFER_TRACE(jh, "not on any transaction: zap");
1927 if (!buffer_dirty(bh)) {
1928 /* bdflush has written it. We can drop it now */
1932 /* OK, it must be in the journal but still not
1933 * written fully to disk: it's metadata or
1934 * journaled data... */
1936 if (journal->j_running_transaction) {
1937 /* ... and once the current transaction has
1938 * committed, the buffer won't be needed any
1940 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1941 ret = __dispose_buffer(jh,
1942 journal->j_running_transaction);
1943 journal_put_journal_head(jh);
1944 spin_unlock(&journal->j_list_lock);
1945 jbd_unlock_bh_state(bh);
1946 spin_unlock(&journal->j_state_lock);
1949 /* There is no currently-running transaction. So the
1950 * orphan record which we wrote for this file must have
1951 * passed into commit. We must attach this buffer to
1952 * the committing transaction, if it exists. */
1953 if (journal->j_committing_transaction) {
1954 JBUFFER_TRACE(jh, "give to committing trans");
1955 ret = __dispose_buffer(jh,
1956 journal->j_committing_transaction);
1957 journal_put_journal_head(jh);
1958 spin_unlock(&journal->j_list_lock);
1959 jbd_unlock_bh_state(bh);
1960 spin_unlock(&journal->j_state_lock);
1963 /* The orphan record's transaction has
1964 * committed. We can cleanse this buffer */
1965 clear_buffer_jbddirty(bh);
1969 } else if (transaction == journal->j_committing_transaction) {
1970 JBUFFER_TRACE(jh, "on committing transaction");
1971 if (jh->b_jlist == BJ_Locked) {
1973 * The buffer is on the committing transaction's locked
1974 * list. We have the buffer locked, so I/O has
1975 * completed. So we can nail the buffer now.
1977 may_free = __dispose_buffer(jh, transaction);
1981 * If it is committing, we simply cannot touch it. We
1982 * can remove it's next_transaction pointer from the
1983 * running transaction if that is set, but nothing
1985 set_buffer_freed(bh);
1986 if (jh->b_next_transaction) {
1987 J_ASSERT(jh->b_next_transaction ==
1988 journal->j_running_transaction);
1989 jh->b_next_transaction = NULL;
1991 journal_put_journal_head(jh);
1992 spin_unlock(&journal->j_list_lock);
1993 jbd_unlock_bh_state(bh);
1994 spin_unlock(&journal->j_state_lock);
1997 /* Good, the buffer belongs to the running transaction.
1998 * We are writing our own transaction's data, not any
1999 * previous one's, so it is safe to throw it away
2000 * (remember that we expect the filesystem to have set
2001 * i_size already for this truncate so recovery will not
2002 * expose the disk blocks we are discarding here.) */
2003 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
2004 JBUFFER_TRACE(jh, "on running transaction");
2005 may_free = __dispose_buffer(jh, transaction);
2009 journal_put_journal_head(jh);
2011 spin_unlock(&journal->j_list_lock);
2012 jbd_unlock_bh_state(bh);
2013 spin_unlock(&journal->j_state_lock);
2014 zap_buffer_unlocked:
2015 clear_buffer_dirty(bh);
2016 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
2017 clear_buffer_mapped(bh);
2018 clear_buffer_req(bh);
2019 clear_buffer_new(bh);
2025 * void journal_invalidatepage() - invalidate a journal page
2026 * @journal: journal to use for flush
2027 * @page: page to flush
2028 * @offset: length of page to invalidate.
2030 * Reap page buffers containing data after offset in page.
2032 void journal_invalidatepage(journal_t *journal,
2034 unsigned long offset)
2036 struct buffer_head *head, *bh, *next;
2037 unsigned int curr_off = 0;
2040 if (!PageLocked(page))
2042 if (!page_has_buffers(page))
2045 /* We will potentially be playing with lists other than just the
2046 * data lists (especially for journaled data mode), so be
2047 * cautious in our locking. */
2049 head = bh = page_buffers(page);
2051 unsigned int next_off = curr_off + bh->b_size;
2052 next = bh->b_this_page;
2054 if (offset <= curr_off) {
2055 /* This block is wholly outside the truncation point */
2057 may_free &= journal_unmap_buffer(journal, bh);
2060 curr_off = next_off;
2063 } while (bh != head);
2066 if (may_free && try_to_free_buffers(page))
2067 J_ASSERT(!page_has_buffers(page));
2072 * File a buffer on the given transaction list.
2074 void __journal_file_buffer(struct journal_head *jh,
2075 transaction_t *transaction, int jlist)
2077 struct journal_head **list = NULL;
2079 struct buffer_head *bh = jh2bh(jh);
2081 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2082 assert_spin_locked(&transaction->t_journal->j_list_lock);
2084 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
2085 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
2086 jh->b_transaction == NULL);
2088 if (jh->b_transaction && jh->b_jlist == jlist)
2091 /* The following list of buffer states needs to be consistent
2092 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
2095 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
2096 jlist == BJ_Shadow || jlist == BJ_Forget) {
2097 if (test_clear_buffer_dirty(bh) ||
2098 test_clear_buffer_jbddirty(bh))
2102 if (jh->b_transaction)
2103 __journal_temp_unlink_buffer(jh);
2104 jh->b_transaction = transaction;
2108 J_ASSERT_JH(jh, !jh->b_committed_data);
2109 J_ASSERT_JH(jh, !jh->b_frozen_data);
2112 list = &transaction->t_sync_datalist;
2115 transaction->t_nr_buffers++;
2116 list = &transaction->t_buffers;
2119 list = &transaction->t_forget;
2122 list = &transaction->t_iobuf_list;
2125 list = &transaction->t_shadow_list;
2128 list = &transaction->t_log_list;
2131 list = &transaction->t_reserved_list;
2134 list = &transaction->t_locked_list;
2138 __blist_add_buffer(list, jh);
2139 jh->b_jlist = jlist;
2142 set_buffer_jbddirty(bh);
2145 void journal_file_buffer(struct journal_head *jh,
2146 transaction_t *transaction, int jlist)
2148 jbd_lock_bh_state(jh2bh(jh));
2149 spin_lock(&transaction->t_journal->j_list_lock);
2150 __journal_file_buffer(jh, transaction, jlist);
2151 spin_unlock(&transaction->t_journal->j_list_lock);
2152 jbd_unlock_bh_state(jh2bh(jh));
2156 * Remove a buffer from its current buffer list in preparation for
2157 * dropping it from its current transaction entirely. If the buffer has
2158 * already started to be used by a subsequent transaction, refile the
2159 * buffer on that transaction's metadata list.
2161 * Called under journal->j_list_lock
2163 * Called under jbd_lock_bh_state(jh2bh(jh))
2165 void __journal_refile_buffer(struct journal_head *jh)
2168 struct buffer_head *bh = jh2bh(jh);
2170 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2171 if (jh->b_transaction)
2172 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2174 /* If the buffer is now unused, just drop it. */
2175 if (jh->b_next_transaction == NULL) {
2176 __journal_unfile_buffer(jh);
2181 * It has been modified by a later transaction: add it to the new
2182 * transaction's metadata list.
2185 was_dirty = test_clear_buffer_jbddirty(bh);
2186 __journal_temp_unlink_buffer(jh);
2187 jh->b_transaction = jh->b_next_transaction;
2188 jh->b_next_transaction = NULL;
2189 __journal_file_buffer(jh, jh->b_transaction,
2190 jh->b_modified ? BJ_Metadata : BJ_Reserved);
2191 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2194 set_buffer_jbddirty(bh);
2198 * For the unlocked version of this call, also make sure that any
2199 * hanging journal_head is cleaned up if necessary.
2201 * __journal_refile_buffer is usually called as part of a single locked
2202 * operation on a buffer_head, in which the caller is probably going to
2203 * be hooking the journal_head onto other lists. In that case it is up
2204 * to the caller to remove the journal_head if necessary. For the
2205 * unlocked journal_refile_buffer call, the caller isn't going to be
2206 * doing anything else to the buffer so we need to do the cleanup
2207 * ourselves to avoid a jh leak.
2209 * *** The journal_head may be freed by this call! ***
2211 void journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2213 struct buffer_head *bh = jh2bh(jh);
2215 jbd_lock_bh_state(bh);
2216 spin_lock(&journal->j_list_lock);
2218 __journal_refile_buffer(jh);
2219 jbd_unlock_bh_state(bh);
2220 journal_remove_journal_head(bh);
2222 spin_unlock(&journal->j_list_lock);