2 * linux/fs/jbd2/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/jbd2.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>
29 #include <linux/backing-dev.h>
30 #include <linux/module.h>
32 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
35 * jbd2_get_transaction: obtain a new transaction_t object.
37 * Simply allocate and initialise a new transaction. Create it in
38 * RUNNING state and add it to the current journal (which should not
39 * have an existing running transaction: we only make a new transaction
40 * once we have started to commit the old one).
43 * The journal MUST be locked. We don't perform atomic mallocs on the
44 * new transaction and we can't block without protecting against other
45 * processes trying to touch the journal while it is in transition.
49 static transaction_t *
50 jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
54 transaction->t_start_time = ktime_get();
55 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
58 atomic_set(&transaction->t_updates, 0);
59 atomic_set(&transaction->t_outstanding_credits, 0);
60 INIT_LIST_HEAD(&transaction->t_inode_list);
61 INIT_LIST_HEAD(&transaction->t_private_list);
63 /* Set up the commit timer for the new transaction. */
64 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
65 add_timer(&journal->j_commit_timer);
67 J_ASSERT(journal->j_running_transaction == NULL);
68 journal->j_running_transaction = transaction;
69 transaction->t_max_wait = 0;
70 transaction->t_start = jiffies;
78 * A handle_t is an object which represents a single atomic update to a
79 * filesystem, and which tracks all of the modifications which form part
84 * start_this_handle: Given a handle, deal with any locking or stalling
85 * needed to make sure that there is enough journal space for the handle
86 * to begin. Attach the handle to a transaction and set up the
87 * transaction's buffer credits.
90 static int start_this_handle(journal_t *journal, handle_t *handle,
93 transaction_t *transaction;
95 int nblocks = handle->h_buffer_credits;
96 transaction_t *new_transaction = NULL;
97 unsigned long ts = jiffies;
99 if (nblocks > journal->j_max_transaction_buffers) {
100 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
101 current->comm, nblocks,
102 journal->j_max_transaction_buffers);
107 if (!journal->j_running_transaction) {
108 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
109 if (!new_transaction) {
111 * If __GFP_FS is not present, then we may be
112 * being called from inside the fs writeback
113 * layer, so we MUST NOT fail. Since
114 * __GFP_NOFAIL is going away, we will arrange
115 * to retry the allocation ourselves.
117 if ((gfp_mask & __GFP_FS) == 0) {
118 congestion_wait(BLK_RW_ASYNC, HZ/50);
119 goto alloc_transaction;
125 jbd_debug(3, "New handle %p going live.\n", handle);
130 * We need to hold j_state_lock until t_updates has been incremented,
131 * for proper journal barrier handling
133 spin_lock(&journal->j_state_lock);
135 if (is_journal_aborted(journal) ||
136 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
137 spin_unlock(&journal->j_state_lock);
138 kfree(new_transaction);
142 /* Wait on the journal's transaction barrier if necessary */
143 if (journal->j_barrier_count) {
144 spin_unlock(&journal->j_state_lock);
145 wait_event(journal->j_wait_transaction_locked,
146 journal->j_barrier_count == 0);
150 if (!journal->j_running_transaction) {
151 if (!new_transaction) {
152 spin_unlock(&journal->j_state_lock);
153 goto alloc_transaction;
155 jbd2_get_transaction(journal, new_transaction);
156 new_transaction = NULL;
159 transaction = journal->j_running_transaction;
162 * If the current transaction is locked down for commit, wait for the
163 * lock to be released.
165 if (transaction->t_state == T_LOCKED) {
168 prepare_to_wait(&journal->j_wait_transaction_locked,
169 &wait, TASK_UNINTERRUPTIBLE);
170 spin_unlock(&journal->j_state_lock);
172 finish_wait(&journal->j_wait_transaction_locked, &wait);
177 * If there is not enough space left in the log to write all potential
178 * buffers requested by this operation, we need to stall pending a log
179 * checkpoint to free some more log space.
181 spin_lock(&transaction->t_handle_lock);
182 needed = atomic_read(&transaction->t_outstanding_credits) + nblocks;
184 if (needed > journal->j_max_transaction_buffers) {
186 * If the current transaction is already too large, then start
187 * to commit it: we can then go back and attach this handle to
192 jbd_debug(2, "Handle %p starting new commit...\n", handle);
193 spin_unlock(&transaction->t_handle_lock);
194 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
195 TASK_UNINTERRUPTIBLE);
196 __jbd2_log_start_commit(journal, transaction->t_tid);
197 spin_unlock(&journal->j_state_lock);
199 finish_wait(&journal->j_wait_transaction_locked, &wait);
204 * The commit code assumes that it can get enough log space
205 * without forcing a checkpoint. This is *critical* for
206 * correctness: a checkpoint of a buffer which is also
207 * associated with a committing transaction creates a deadlock,
208 * so commit simply cannot force through checkpoints.
210 * We must therefore ensure the necessary space in the journal
211 * *before* starting to dirty potentially checkpointed buffers
212 * in the new transaction.
214 * The worst part is, any transaction currently committing can
215 * reduce the free space arbitrarily. Be careful to account for
216 * those buffers when checkpointing.
220 * @@@ AKPM: This seems rather over-defensive. We're giving commit
221 * a _lot_ of headroom: 1/4 of the journal plus the size of
222 * the committing transaction. Really, we only need to give it
223 * committing_transaction->t_outstanding_credits plus "enough" for
224 * the log control blocks.
225 * Also, this test is inconsitent with the matching one in
226 * jbd2_journal_extend().
228 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
229 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
230 spin_unlock(&transaction->t_handle_lock);
231 __jbd2_log_wait_for_space(journal);
235 /* OK, account for the buffers that this operation expects to
236 * use and add the handle to the running transaction. */
238 if (time_after(transaction->t_start, ts)) {
239 ts = jbd2_time_diff(ts, transaction->t_start);
240 if (ts > transaction->t_max_wait)
241 transaction->t_max_wait = ts;
244 handle->h_transaction = transaction;
245 atomic_add(nblocks, &transaction->t_outstanding_credits);
246 atomic_inc(&transaction->t_updates);
247 transaction->t_handle_count++;
248 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
250 atomic_read(&transaction->t_outstanding_credits),
251 __jbd2_log_space_left(journal));
252 spin_unlock(&transaction->t_handle_lock);
253 spin_unlock(&journal->j_state_lock);
255 lock_map_acquire(&handle->h_lockdep_map);
256 kfree(new_transaction);
260 static struct lock_class_key jbd2_handle_key;
262 /* Allocate a new handle. This should probably be in a slab... */
263 static handle_t *new_handle(int nblocks)
265 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
268 memset(handle, 0, sizeof(*handle));
269 handle->h_buffer_credits = nblocks;
272 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
273 &jbd2_handle_key, 0);
279 * handle_t *jbd2_journal_start() - Obtain a new handle.
280 * @journal: Journal to start transaction on.
281 * @nblocks: number of block buffer we might modify
283 * We make sure that the transaction can guarantee at least nblocks of
284 * modified buffers in the log. We block until the log can guarantee
287 * This function is visible to journal users (like ext3fs), so is not
288 * called with the journal already locked.
290 * Return a pointer to a newly allocated handle, or NULL on failure
292 handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
294 handle_t *handle = journal_current_handle();
298 return ERR_PTR(-EROFS);
301 J_ASSERT(handle->h_transaction->t_journal == journal);
306 handle = new_handle(nblocks);
308 return ERR_PTR(-ENOMEM);
310 current->journal_info = handle;
312 err = start_this_handle(journal, handle, gfp_mask);
314 jbd2_free_handle(handle);
315 current->journal_info = NULL;
316 handle = ERR_PTR(err);
322 EXPORT_SYMBOL(jbd2__journal_start);
325 handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
327 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
329 EXPORT_SYMBOL(jbd2_journal_start);
333 * int jbd2_journal_extend() - extend buffer credits.
334 * @handle: handle to 'extend'
335 * @nblocks: nr blocks to try to extend by.
337 * Some transactions, such as large extends and truncates, can be done
338 * atomically all at once or in several stages. The operation requests
339 * a credit for a number of buffer modications in advance, but can
340 * extend its credit if it needs more.
342 * jbd2_journal_extend tries to give the running handle more buffer credits.
343 * It does not guarantee that allocation - this is a best-effort only.
344 * The calling process MUST be able to deal cleanly with a failure to
347 * Return 0 on success, non-zero on failure.
349 * return code < 0 implies an error
350 * return code > 0 implies normal transaction-full status.
352 int jbd2_journal_extend(handle_t *handle, int nblocks)
354 transaction_t *transaction = handle->h_transaction;
355 journal_t *journal = transaction->t_journal;
360 if (is_handle_aborted(handle))
365 spin_lock(&journal->j_state_lock);
367 /* Don't extend a locked-down transaction! */
368 if (handle->h_transaction->t_state != T_RUNNING) {
369 jbd_debug(3, "denied handle %p %d blocks: "
370 "transaction not running\n", handle, nblocks);
374 spin_lock(&transaction->t_handle_lock);
375 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
377 if (wanted > journal->j_max_transaction_buffers) {
378 jbd_debug(3, "denied handle %p %d blocks: "
379 "transaction too large\n", handle, nblocks);
383 if (wanted > __jbd2_log_space_left(journal)) {
384 jbd_debug(3, "denied handle %p %d blocks: "
385 "insufficient log space\n", handle, nblocks);
389 handle->h_buffer_credits += nblocks;
390 atomic_add(nblocks, &transaction->t_outstanding_credits);
393 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
395 spin_unlock(&transaction->t_handle_lock);
397 spin_unlock(&journal->j_state_lock);
404 * int jbd2_journal_restart() - restart a handle .
405 * @handle: handle to restart
406 * @nblocks: nr credits requested
408 * Restart a handle for a multi-transaction filesystem
411 * If the jbd2_journal_extend() call above fails to grant new buffer credits
412 * to a running handle, a call to jbd2_journal_restart will commit the
413 * handle's transaction so far and reattach the handle to a new
414 * transaction capabable of guaranteeing the requested number of
417 int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
419 transaction_t *transaction = handle->h_transaction;
420 journal_t *journal = transaction->t_journal;
423 /* If we've had an abort of any type, don't even think about
424 * actually doing the restart! */
425 if (is_handle_aborted(handle))
429 * First unlink the handle from its current transaction, and start the
432 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
433 J_ASSERT(journal_current_handle() == handle);
435 spin_lock(&journal->j_state_lock);
436 spin_lock(&transaction->t_handle_lock);
437 atomic_sub(handle->h_buffer_credits,
438 &transaction->t_outstanding_credits);
439 if (atomic_dec_and_test(&transaction->t_updates))
440 wake_up(&journal->j_wait_updates);
441 spin_unlock(&transaction->t_handle_lock);
443 jbd_debug(2, "restarting handle %p\n", handle);
444 __jbd2_log_start_commit(journal, transaction->t_tid);
445 spin_unlock(&journal->j_state_lock);
447 lock_map_release(&handle->h_lockdep_map);
448 handle->h_buffer_credits = nblocks;
449 ret = start_this_handle(journal, handle, gfp_mask);
452 EXPORT_SYMBOL(jbd2__journal_restart);
455 int jbd2_journal_restart(handle_t *handle, int nblocks)
457 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
459 EXPORT_SYMBOL(jbd2_journal_restart);
462 * void jbd2_journal_lock_updates () - establish a transaction barrier.
463 * @journal: Journal to establish a barrier on.
465 * This locks out any further updates from being started, and blocks
466 * until all existing updates have completed, returning only once the
467 * journal is in a quiescent state with no updates running.
469 * The journal lock should not be held on entry.
471 void jbd2_journal_lock_updates(journal_t *journal)
475 spin_lock(&journal->j_state_lock);
476 ++journal->j_barrier_count;
478 /* Wait until there are no running updates */
480 transaction_t *transaction = journal->j_running_transaction;
485 spin_lock(&transaction->t_handle_lock);
486 if (!atomic_read(&transaction->t_updates)) {
487 spin_unlock(&transaction->t_handle_lock);
490 prepare_to_wait(&journal->j_wait_updates, &wait,
491 TASK_UNINTERRUPTIBLE);
492 spin_unlock(&transaction->t_handle_lock);
493 spin_unlock(&journal->j_state_lock);
495 finish_wait(&journal->j_wait_updates, &wait);
496 spin_lock(&journal->j_state_lock);
498 spin_unlock(&journal->j_state_lock);
501 * We have now established a barrier against other normal updates, but
502 * we also need to barrier against other jbd2_journal_lock_updates() calls
503 * to make sure that we serialise special journal-locked operations
506 mutex_lock(&journal->j_barrier);
510 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
511 * @journal: Journal to release the barrier on.
513 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
515 * Should be called without the journal lock held.
517 void jbd2_journal_unlock_updates (journal_t *journal)
519 J_ASSERT(journal->j_barrier_count != 0);
521 mutex_unlock(&journal->j_barrier);
522 spin_lock(&journal->j_state_lock);
523 --journal->j_barrier_count;
524 spin_unlock(&journal->j_state_lock);
525 wake_up(&journal->j_wait_transaction_locked);
528 static void warn_dirty_buffer(struct buffer_head *bh)
530 char b[BDEVNAME_SIZE];
533 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
534 "There's a risk of filesystem corruption in case of system "
536 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
540 * If the buffer is already part of the current transaction, then there
541 * is nothing we need to do. If it is already part of a prior
542 * transaction which we are still committing to disk, then we need to
543 * make sure that we do not overwrite the old copy: we do copy-out to
544 * preserve the copy going to disk. We also account the buffer against
545 * the handle's metadata buffer credits (unless the buffer is already
546 * part of the transaction, that is).
550 do_get_write_access(handle_t *handle, struct journal_head *jh,
553 struct buffer_head *bh;
554 transaction_t *transaction;
557 char *frozen_buffer = NULL;
560 if (is_handle_aborted(handle))
563 transaction = handle->h_transaction;
564 journal = transaction->t_journal;
566 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
568 JBUFFER_TRACE(jh, "entry");
572 /* @@@ Need to check for errors here at some point. */
575 jbd_lock_bh_state(bh);
577 /* We now hold the buffer lock so it is safe to query the buffer
578 * state. Is the buffer dirty?
580 * If so, there are two possibilities. The buffer may be
581 * non-journaled, and undergoing a quite legitimate writeback.
582 * Otherwise, it is journaled, and we don't expect dirty buffers
583 * in that state (the buffers should be marked JBD_Dirty
584 * instead.) So either the IO is being done under our own
585 * control and this is a bug, or it's a third party IO such as
586 * dump(8) (which may leave the buffer scheduled for read ---
587 * ie. locked but not dirty) or tune2fs (which may actually have
588 * the buffer dirtied, ugh.) */
590 if (buffer_dirty(bh)) {
592 * First question: is this buffer already part of the current
593 * transaction or the existing committing transaction?
595 if (jh->b_transaction) {
597 jh->b_transaction == transaction ||
599 journal->j_committing_transaction);
600 if (jh->b_next_transaction)
601 J_ASSERT_JH(jh, jh->b_next_transaction ==
603 warn_dirty_buffer(bh);
606 * In any case we need to clean the dirty flag and we must
607 * do it under the buffer lock to be sure we don't race
608 * with running write-out.
610 JBUFFER_TRACE(jh, "Journalling dirty buffer");
611 clear_buffer_dirty(bh);
612 set_buffer_jbddirty(bh);
618 if (is_handle_aborted(handle)) {
619 jbd_unlock_bh_state(bh);
625 * The buffer is already part of this transaction if b_transaction or
626 * b_next_transaction points to it
628 if (jh->b_transaction == transaction ||
629 jh->b_next_transaction == transaction)
633 * this is the first time this transaction is touching this buffer,
634 * reset the modified flag
639 * If there is already a copy-out version of this buffer, then we don't
640 * need to make another one
642 if (jh->b_frozen_data) {
643 JBUFFER_TRACE(jh, "has frozen data");
644 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
645 jh->b_next_transaction = transaction;
649 /* Is there data here we need to preserve? */
651 if (jh->b_transaction && jh->b_transaction != transaction) {
652 JBUFFER_TRACE(jh, "owned by older transaction");
653 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
654 J_ASSERT_JH(jh, jh->b_transaction ==
655 journal->j_committing_transaction);
657 /* There is one case we have to be very careful about.
658 * If the committing transaction is currently writing
659 * this buffer out to disk and has NOT made a copy-out,
660 * then we cannot modify the buffer contents at all
661 * right now. The essence of copy-out is that it is the
662 * extra copy, not the primary copy, which gets
663 * journaled. If the primary copy is already going to
664 * disk then we cannot do copy-out here. */
666 if (jh->b_jlist == BJ_Shadow) {
667 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
668 wait_queue_head_t *wqh;
670 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
672 JBUFFER_TRACE(jh, "on shadow: sleep");
673 jbd_unlock_bh_state(bh);
674 /* commit wakes up all shadow buffers after IO */
676 prepare_to_wait(wqh, &wait.wait,
677 TASK_UNINTERRUPTIBLE);
678 if (jh->b_jlist != BJ_Shadow)
682 finish_wait(wqh, &wait.wait);
686 /* Only do the copy if the currently-owning transaction
687 * still needs it. If it is on the Forget list, the
688 * committing transaction is past that stage. The
689 * buffer had better remain locked during the kmalloc,
690 * but that should be true --- we hold the journal lock
691 * still and the buffer is already on the BUF_JOURNAL
692 * list so won't be flushed.
694 * Subtle point, though: if this is a get_undo_access,
695 * then we will be relying on the frozen_data to contain
696 * the new value of the committed_data record after the
697 * transaction, so we HAVE to force the frozen_data copy
700 if (jh->b_jlist != BJ_Forget || force_copy) {
701 JBUFFER_TRACE(jh, "generate frozen data");
702 if (!frozen_buffer) {
703 JBUFFER_TRACE(jh, "allocate memory for buffer");
704 jbd_unlock_bh_state(bh);
706 jbd2_alloc(jh2bh(jh)->b_size,
708 if (!frozen_buffer) {
710 "%s: OOM for frozen_buffer\n",
712 JBUFFER_TRACE(jh, "oom!");
714 jbd_lock_bh_state(bh);
719 jh->b_frozen_data = frozen_buffer;
720 frozen_buffer = NULL;
723 jh->b_next_transaction = transaction;
728 * Finally, if the buffer is not journaled right now, we need to make
729 * sure it doesn't get written to disk before the caller actually
730 * commits the new data
732 if (!jh->b_transaction) {
733 JBUFFER_TRACE(jh, "no transaction");
734 J_ASSERT_JH(jh, !jh->b_next_transaction);
735 jh->b_transaction = transaction;
736 JBUFFER_TRACE(jh, "file as BJ_Reserved");
737 spin_lock(&journal->j_list_lock);
738 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
739 spin_unlock(&journal->j_list_lock);
748 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
749 "Possible IO failure.\n");
750 page = jh2bh(jh)->b_page;
751 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
752 source = kmap_atomic(page, KM_USER0);
753 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
754 kunmap_atomic(source, KM_USER0);
757 * Now that the frozen data is saved off, we need to store
758 * any matching triggers.
760 jh->b_frozen_triggers = jh->b_triggers;
762 jbd_unlock_bh_state(bh);
765 * If we are about to journal a buffer, then any revoke pending on it is
768 jbd2_journal_cancel_revoke(handle, jh);
771 if (unlikely(frozen_buffer)) /* It's usually NULL */
772 jbd2_free(frozen_buffer, bh->b_size);
774 JBUFFER_TRACE(jh, "exit");
779 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
780 * @handle: transaction to add buffer modifications to
781 * @bh: bh to be used for metadata writes
782 * @credits: variable that will receive credits for the buffer
784 * Returns an error code or 0 on success.
786 * In full data journalling mode the buffer may be of type BJ_AsyncData,
787 * because we're write()ing a buffer which is also part of a shared mapping.
790 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
792 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
795 /* We do not want to get caught playing with fields which the
796 * log thread also manipulates. Make sure that the buffer
797 * completes any outstanding IO before proceeding. */
798 rc = do_get_write_access(handle, jh, 0);
799 jbd2_journal_put_journal_head(jh);
805 * When the user wants to journal a newly created buffer_head
806 * (ie. getblk() returned a new buffer and we are going to populate it
807 * manually rather than reading off disk), then we need to keep the
808 * buffer_head locked until it has been completely filled with new
809 * data. In this case, we should be able to make the assertion that
810 * the bh is not already part of an existing transaction.
812 * The buffer should already be locked by the caller by this point.
813 * There is no lock ranking violation: it was a newly created,
814 * unlocked buffer beforehand. */
817 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
818 * @handle: transaction to new buffer to
821 * Call this if you create a new bh.
823 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
825 transaction_t *transaction = handle->h_transaction;
826 journal_t *journal = transaction->t_journal;
827 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
830 jbd_debug(5, "journal_head %p\n", jh);
832 if (is_handle_aborted(handle))
836 JBUFFER_TRACE(jh, "entry");
838 * The buffer may already belong to this transaction due to pre-zeroing
839 * in the filesystem's new_block code. It may also be on the previous,
840 * committing transaction's lists, but it HAS to be in Forget state in
841 * that case: the transaction must have deleted the buffer for it to be
844 jbd_lock_bh_state(bh);
845 spin_lock(&journal->j_list_lock);
846 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
847 jh->b_transaction == NULL ||
848 (jh->b_transaction == journal->j_committing_transaction &&
849 jh->b_jlist == BJ_Forget)));
851 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
852 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
854 if (jh->b_transaction == NULL) {
856 * Previous jbd2_journal_forget() could have left the buffer
857 * with jbddirty bit set because it was being committed. When
858 * the commit finished, we've filed the buffer for
859 * checkpointing and marked it dirty. Now we are reallocating
860 * the buffer so the transaction freeing it must have
861 * committed and so it's safe to clear the dirty bit.
863 clear_buffer_dirty(jh2bh(jh));
864 jh->b_transaction = transaction;
866 /* first access by this transaction */
869 JBUFFER_TRACE(jh, "file as BJ_Reserved");
870 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
871 } else if (jh->b_transaction == journal->j_committing_transaction) {
872 /* first access by this transaction */
875 JBUFFER_TRACE(jh, "set next transaction");
876 jh->b_next_transaction = transaction;
878 spin_unlock(&journal->j_list_lock);
879 jbd_unlock_bh_state(bh);
882 * akpm: I added this. ext3_alloc_branch can pick up new indirect
883 * blocks which contain freed but then revoked metadata. We need
884 * to cancel the revoke in case we end up freeing it yet again
885 * and the reallocating as data - this would cause a second revoke,
886 * which hits an assertion error.
888 JBUFFER_TRACE(jh, "cancelling revoke");
889 jbd2_journal_cancel_revoke(handle, jh);
890 jbd2_journal_put_journal_head(jh);
896 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
897 * non-rewindable consequences
898 * @handle: transaction
899 * @bh: buffer to undo
900 * @credits: store the number of taken credits here (if not NULL)
902 * Sometimes there is a need to distinguish between metadata which has
903 * been committed to disk and that which has not. The ext3fs code uses
904 * this for freeing and allocating space, we have to make sure that we
905 * do not reuse freed space until the deallocation has been committed,
906 * since if we overwrote that space we would make the delete
907 * un-rewindable in case of a crash.
909 * To deal with that, jbd2_journal_get_undo_access requests write access to a
910 * buffer for parts of non-rewindable operations such as delete
911 * operations on the bitmaps. The journaling code must keep a copy of
912 * the buffer's contents prior to the undo_access call until such time
913 * as we know that the buffer has definitely been committed to disk.
915 * We never need to know which transaction the committed data is part
916 * of, buffers touched here are guaranteed to be dirtied later and so
917 * will be committed to a new transaction in due course, at which point
918 * we can discard the old committed data pointer.
920 * Returns error number or 0 on success.
922 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
925 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
926 char *committed_data = NULL;
928 JBUFFER_TRACE(jh, "entry");
931 * Do this first --- it can drop the journal lock, so we want to
932 * make sure that obtaining the committed_data is done
933 * atomically wrt. completion of any outstanding commits.
935 err = do_get_write_access(handle, jh, 1);
940 if (!jh->b_committed_data) {
941 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
942 if (!committed_data) {
943 printk(KERN_EMERG "%s: No memory for committed data\n",
950 jbd_lock_bh_state(bh);
951 if (!jh->b_committed_data) {
952 /* Copy out the current buffer contents into the
953 * preserved, committed copy. */
954 JBUFFER_TRACE(jh, "generate b_committed data");
955 if (!committed_data) {
956 jbd_unlock_bh_state(bh);
960 jh->b_committed_data = committed_data;
961 committed_data = NULL;
962 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
964 jbd_unlock_bh_state(bh);
966 jbd2_journal_put_journal_head(jh);
967 if (unlikely(committed_data))
968 jbd2_free(committed_data, bh->b_size);
973 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
974 * @bh: buffer to trigger on
975 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
977 * Set any triggers on this journal_head. This is always safe, because
978 * triggers for a committing buffer will be saved off, and triggers for
979 * a running transaction will match the buffer in that transaction.
981 * Call with NULL to clear the triggers.
983 void jbd2_journal_set_triggers(struct buffer_head *bh,
984 struct jbd2_buffer_trigger_type *type)
986 struct journal_head *jh = bh2jh(bh);
988 jh->b_triggers = type;
991 void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
992 struct jbd2_buffer_trigger_type *triggers)
994 struct buffer_head *bh = jh2bh(jh);
996 if (!triggers || !triggers->t_commit)
999 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
1002 void jbd2_buffer_abort_trigger(struct journal_head *jh,
1003 struct jbd2_buffer_trigger_type *triggers)
1005 if (!triggers || !triggers->t_abort)
1008 triggers->t_abort(triggers, jh2bh(jh));
1014 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
1015 * @handle: transaction to add buffer to.
1016 * @bh: buffer to mark
1018 * mark dirty metadata which needs to be journaled as part of the current
1021 * The buffer is placed on the transaction's metadata list and is marked
1022 * as belonging to the transaction.
1024 * Returns error number or 0 on success.
1026 * Special care needs to be taken if the buffer already belongs to the
1027 * current committing transaction (in which case we should have frozen
1028 * data present for that commit). In that case, we don't relink the
1029 * buffer: that only gets done when the old transaction finally
1030 * completes its commit.
1032 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1034 transaction_t *transaction = handle->h_transaction;
1035 journal_t *journal = transaction->t_journal;
1036 struct journal_head *jh = bh2jh(bh);
1038 jbd_debug(5, "journal_head %p\n", jh);
1039 JBUFFER_TRACE(jh, "entry");
1040 if (is_handle_aborted(handle))
1043 jbd_lock_bh_state(bh);
1045 if (jh->b_modified == 0) {
1047 * This buffer's got modified and becoming part
1048 * of the transaction. This needs to be done
1049 * once a transaction -bzzz
1052 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1053 handle->h_buffer_credits--;
1057 * fastpath, to avoid expensive locking. If this buffer is already
1058 * on the running transaction's metadata list there is nothing to do.
1059 * Nobody can take it off again because there is a handle open.
1060 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1061 * result in this test being false, so we go in and take the locks.
1063 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1064 JBUFFER_TRACE(jh, "fastpath");
1065 J_ASSERT_JH(jh, jh->b_transaction ==
1066 journal->j_running_transaction);
1070 set_buffer_jbddirty(bh);
1073 * Metadata already on the current transaction list doesn't
1074 * need to be filed. Metadata on another transaction's list must
1075 * be committing, and will be refiled once the commit completes:
1076 * leave it alone for now.
1078 if (jh->b_transaction != transaction) {
1079 JBUFFER_TRACE(jh, "already on other transaction");
1080 J_ASSERT_JH(jh, jh->b_transaction ==
1081 journal->j_committing_transaction);
1082 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1083 /* And this case is illegal: we can't reuse another
1084 * transaction's data buffer, ever. */
1088 /* That test should have eliminated the following case: */
1089 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1091 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1092 spin_lock(&journal->j_list_lock);
1093 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1094 spin_unlock(&journal->j_list_lock);
1096 jbd_unlock_bh_state(bh);
1098 JBUFFER_TRACE(jh, "exit");
1103 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
1104 * updates, if the update decided in the end that it didn't need access.
1108 jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1110 BUFFER_TRACE(bh, "entry");
1114 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1115 * @handle: transaction handle
1116 * @bh: bh to 'forget'
1118 * We can only do the bforget if there are no commits pending against the
1119 * buffer. If the buffer is dirty in the current running transaction we
1120 * can safely unlink it.
1122 * bh may not be a journalled buffer at all - it may be a non-JBD
1123 * buffer which came off the hashtable. Check for this.
1125 * Decrements bh->b_count by one.
1127 * Allow this call even if the handle has aborted --- it may be part of
1128 * the caller's cleanup after an abort.
1130 int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1132 transaction_t *transaction = handle->h_transaction;
1133 journal_t *journal = transaction->t_journal;
1134 struct journal_head *jh;
1135 int drop_reserve = 0;
1137 int was_modified = 0;
1139 BUFFER_TRACE(bh, "entry");
1141 jbd_lock_bh_state(bh);
1142 spin_lock(&journal->j_list_lock);
1144 if (!buffer_jbd(bh))
1148 /* Critical error: attempting to delete a bitmap buffer, maybe?
1149 * Don't do any jbd operations, and return an error. */
1150 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1151 "inconsistent data on disk")) {
1156 /* keep track of wether or not this transaction modified us */
1157 was_modified = jh->b_modified;
1160 * The buffer's going from the transaction, we must drop
1161 * all references -bzzz
1165 if (jh->b_transaction == handle->h_transaction) {
1166 J_ASSERT_JH(jh, !jh->b_frozen_data);
1168 /* If we are forgetting a buffer which is already part
1169 * of this transaction, then we can just drop it from
1170 * the transaction immediately. */
1171 clear_buffer_dirty(bh);
1172 clear_buffer_jbddirty(bh);
1174 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1177 * we only want to drop a reference if this transaction
1178 * modified the buffer
1184 * We are no longer going to journal this buffer.
1185 * However, the commit of this transaction is still
1186 * important to the buffer: the delete that we are now
1187 * processing might obsolete an old log entry, so by
1188 * committing, we can satisfy the buffer's checkpoint.
1190 * So, if we have a checkpoint on the buffer, we should
1191 * now refile the buffer on our BJ_Forget list so that
1192 * we know to remove the checkpoint after we commit.
1195 if (jh->b_cp_transaction) {
1196 __jbd2_journal_temp_unlink_buffer(jh);
1197 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1199 __jbd2_journal_unfile_buffer(jh);
1200 jbd2_journal_remove_journal_head(bh);
1202 if (!buffer_jbd(bh)) {
1203 spin_unlock(&journal->j_list_lock);
1204 jbd_unlock_bh_state(bh);
1209 } else if (jh->b_transaction) {
1210 J_ASSERT_JH(jh, (jh->b_transaction ==
1211 journal->j_committing_transaction));
1212 /* However, if the buffer is still owned by a prior
1213 * (committing) transaction, we can't drop it yet... */
1214 JBUFFER_TRACE(jh, "belongs to older transaction");
1215 /* ... but we CAN drop it from the new transaction if we
1216 * have also modified it since the original commit. */
1218 if (jh->b_next_transaction) {
1219 J_ASSERT(jh->b_next_transaction == transaction);
1220 jh->b_next_transaction = NULL;
1223 * only drop a reference if this transaction modified
1232 spin_unlock(&journal->j_list_lock);
1233 jbd_unlock_bh_state(bh);
1237 /* no need to reserve log space for this block -bzzz */
1238 handle->h_buffer_credits++;
1244 * int jbd2_journal_stop() - complete a transaction
1245 * @handle: tranaction to complete.
1247 * All done for a particular handle.
1249 * There is not much action needed here. We just return any remaining
1250 * buffer credits to the transaction and remove the handle. The only
1251 * complication is that we need to start a commit operation if the
1252 * filesystem is marked for synchronous update.
1254 * jbd2_journal_stop itself will not usually return an error, but it may
1255 * do so in unusual circumstances. In particular, expect it to
1256 * return -EIO if a jbd2_journal_abort has been executed since the
1257 * transaction began.
1259 int jbd2_journal_stop(handle_t *handle)
1261 transaction_t *transaction = handle->h_transaction;
1262 journal_t *journal = transaction->t_journal;
1263 int err, wait_for_commit = 0;
1267 J_ASSERT(journal_current_handle() == handle);
1269 if (is_handle_aborted(handle))
1272 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
1276 if (--handle->h_ref > 0) {
1277 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1282 jbd_debug(4, "Handle %p going down\n", handle);
1285 * Implement synchronous transaction batching. If the handle
1286 * was synchronous, don't force a commit immediately. Let's
1287 * yield and let another thread piggyback onto this
1288 * transaction. Keep doing that while new threads continue to
1289 * arrive. It doesn't cost much - we're about to run a commit
1290 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1291 * operations by 30x or more...
1293 * We try and optimize the sleep time against what the
1294 * underlying disk can do, instead of having a static sleep
1295 * time. This is useful for the case where our storage is so
1296 * fast that it is more optimal to go ahead and force a flush
1297 * and wait for the transaction to be committed than it is to
1298 * wait for an arbitrary amount of time for new writers to
1299 * join the transaction. We achieve this by measuring how
1300 * long it takes to commit a transaction, and compare it with
1301 * how long this transaction has been running, and if run time
1302 * < commit time then we sleep for the delta and commit. This
1303 * greatly helps super fast disks that would see slowdowns as
1304 * more threads started doing fsyncs.
1306 * But don't do this if this process was the most recent one
1307 * to perform a synchronous write. We do this to detect the
1308 * case where a single process is doing a stream of sync
1309 * writes. No point in waiting for joiners in that case.
1312 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1313 u64 commit_time, trans_time;
1315 journal->j_last_sync_writer = pid;
1317 spin_lock(&journal->j_state_lock);
1318 commit_time = journal->j_average_commit_time;
1319 spin_unlock(&journal->j_state_lock);
1321 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1322 transaction->t_start_time));
1324 commit_time = max_t(u64, commit_time,
1325 1000*journal->j_min_batch_time);
1326 commit_time = min_t(u64, commit_time,
1327 1000*journal->j_max_batch_time);
1329 if (trans_time < commit_time) {
1330 ktime_t expires = ktime_add_ns(ktime_get(),
1332 set_current_state(TASK_UNINTERRUPTIBLE);
1333 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1338 transaction->t_synchronous_commit = 1;
1339 current->journal_info = NULL;
1340 atomic_sub(handle->h_buffer_credits,
1341 &transaction->t_outstanding_credits);
1344 * If the handle is marked SYNC, we need to set another commit
1345 * going! We also want to force a commit if the current
1346 * transaction is occupying too much of the log, or if the
1347 * transaction is too old now.
1349 if (handle->h_sync ||
1350 (atomic_read(&transaction->t_outstanding_credits) >
1351 journal->j_max_transaction_buffers) ||
1352 time_after_eq(jiffies, transaction->t_expires)) {
1353 /* Do this even for aborted journals: an abort still
1354 * completes the commit thread, it just doesn't write
1355 * anything to disk. */
1357 jbd_debug(2, "transaction too old, requesting commit for "
1358 "handle %p\n", handle);
1359 /* This is non-blocking */
1360 jbd2_log_start_commit(journal, transaction->t_tid);
1363 * Special case: JBD2_SYNC synchronous updates require us
1364 * to wait for the commit to complete.
1366 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1367 wait_for_commit = 1;
1371 * Once we drop t_updates, if it goes to zero the transaction
1372 * could start commiting on us and eventually disappear. So
1373 * once we do this, we must not dereference transaction
1376 tid = transaction->t_tid;
1377 if (atomic_dec_and_test(&transaction->t_updates)) {
1378 wake_up(&journal->j_wait_updates);
1379 if (journal->j_barrier_count)
1380 wake_up(&journal->j_wait_transaction_locked);
1383 if (wait_for_commit)
1384 err = jbd2_log_wait_commit(journal, tid);
1386 lock_map_release(&handle->h_lockdep_map);
1388 jbd2_free_handle(handle);
1393 * int jbd2_journal_force_commit() - force any uncommitted transactions
1394 * @journal: journal to force
1396 * For synchronous operations: force any uncommitted transactions
1397 * to disk. May seem kludgy, but it reuses all the handle batching
1398 * code in a very simple manner.
1400 int jbd2_journal_force_commit(journal_t *journal)
1405 handle = jbd2_journal_start(journal, 1);
1406 if (IS_ERR(handle)) {
1407 ret = PTR_ERR(handle);
1410 ret = jbd2_journal_stop(handle);
1417 * List management code snippets: various functions for manipulating the
1418 * transaction buffer lists.
1423 * Append a buffer to a transaction list, given the transaction's list head
1426 * j_list_lock is held.
1428 * jbd_lock_bh_state(jh2bh(jh)) is held.
1432 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1435 jh->b_tnext = jh->b_tprev = jh;
1438 /* Insert at the tail of the list to preserve order */
1439 struct journal_head *first = *list, *last = first->b_tprev;
1441 jh->b_tnext = first;
1442 last->b_tnext = first->b_tprev = jh;
1447 * Remove a buffer from a transaction list, given the transaction's list
1450 * Called with j_list_lock held, and the journal may not be locked.
1452 * jbd_lock_bh_state(jh2bh(jh)) is held.
1456 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1459 *list = jh->b_tnext;
1463 jh->b_tprev->b_tnext = jh->b_tnext;
1464 jh->b_tnext->b_tprev = jh->b_tprev;
1468 * Remove a buffer from the appropriate transaction list.
1470 * Note that this function can *change* the value of
1471 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1472 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1473 * of these pointers, it could go bad. Generally the caller needs to re-read
1474 * the pointer from the transaction_t.
1476 * Called under j_list_lock. The journal may not be locked.
1478 void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1480 struct journal_head **list = NULL;
1481 transaction_t *transaction;
1482 struct buffer_head *bh = jh2bh(jh);
1484 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1485 transaction = jh->b_transaction;
1487 assert_spin_locked(&transaction->t_journal->j_list_lock);
1489 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1490 if (jh->b_jlist != BJ_None)
1491 J_ASSERT_JH(jh, transaction != NULL);
1493 switch (jh->b_jlist) {
1497 transaction->t_nr_buffers--;
1498 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1499 list = &transaction->t_buffers;
1502 list = &transaction->t_forget;
1505 list = &transaction->t_iobuf_list;
1508 list = &transaction->t_shadow_list;
1511 list = &transaction->t_log_list;
1514 list = &transaction->t_reserved_list;
1518 __blist_del_buffer(list, jh);
1519 jh->b_jlist = BJ_None;
1520 if (test_clear_buffer_jbddirty(bh))
1521 mark_buffer_dirty(bh); /* Expose it to the VM */
1524 void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1526 __jbd2_journal_temp_unlink_buffer(jh);
1527 jh->b_transaction = NULL;
1530 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1532 jbd_lock_bh_state(jh2bh(jh));
1533 spin_lock(&journal->j_list_lock);
1534 __jbd2_journal_unfile_buffer(jh);
1535 spin_unlock(&journal->j_list_lock);
1536 jbd_unlock_bh_state(jh2bh(jh));
1540 * Called from jbd2_journal_try_to_free_buffers().
1542 * Called under jbd_lock_bh_state(bh)
1545 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1547 struct journal_head *jh;
1551 if (buffer_locked(bh) || buffer_dirty(bh))
1554 if (jh->b_next_transaction != NULL)
1557 spin_lock(&journal->j_list_lock);
1558 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1559 /* written-back checkpointed metadata buffer */
1560 if (jh->b_jlist == BJ_None) {
1561 JBUFFER_TRACE(jh, "remove from checkpoint list");
1562 __jbd2_journal_remove_checkpoint(jh);
1563 jbd2_journal_remove_journal_head(bh);
1567 spin_unlock(&journal->j_list_lock);
1573 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1574 * @journal: journal for operation
1575 * @page: to try and free
1576 * @gfp_mask: we use the mask to detect how hard should we try to release
1577 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1578 * release the buffers.
1581 * For all the buffers on this page,
1582 * if they are fully written out ordered data, move them onto BUF_CLEAN
1583 * so try_to_free_buffers() can reap them.
1585 * This function returns non-zero if we wish try_to_free_buffers()
1586 * to be called. We do this if the page is releasable by try_to_free_buffers().
1587 * We also do it if the page has locked or dirty buffers and the caller wants
1588 * us to perform sync or async writeout.
1590 * This complicates JBD locking somewhat. We aren't protected by the
1591 * BKL here. We wish to remove the buffer from its committing or
1592 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1594 * This may *change* the value of transaction_t->t_datalist, so anyone
1595 * who looks at t_datalist needs to lock against this function.
1597 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1598 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1599 * will come out of the lock with the buffer dirty, which makes it
1600 * ineligible for release here.
1602 * Who else is affected by this? hmm... Really the only contender
1603 * is do_get_write_access() - it could be looking at the buffer while
1604 * journal_try_to_free_buffer() is changing its state. But that
1605 * cannot happen because we never reallocate freed data as metadata
1606 * while the data is part of a transaction. Yes?
1608 * Return 0 on failure, 1 on success
1610 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1611 struct page *page, gfp_t gfp_mask)
1613 struct buffer_head *head;
1614 struct buffer_head *bh;
1617 J_ASSERT(PageLocked(page));
1619 head = page_buffers(page);
1622 struct journal_head *jh;
1625 * We take our own ref against the journal_head here to avoid
1626 * having to add tons of locking around each instance of
1627 * jbd2_journal_remove_journal_head() and
1628 * jbd2_journal_put_journal_head().
1630 jh = jbd2_journal_grab_journal_head(bh);
1634 jbd_lock_bh_state(bh);
1635 __journal_try_to_free_buffer(journal, bh);
1636 jbd2_journal_put_journal_head(jh);
1637 jbd_unlock_bh_state(bh);
1640 } while ((bh = bh->b_this_page) != head);
1642 ret = try_to_free_buffers(page);
1649 * This buffer is no longer needed. If it is on an older transaction's
1650 * checkpoint list we need to record it on this transaction's forget list
1651 * to pin this buffer (and hence its checkpointing transaction) down until
1652 * this transaction commits. If the buffer isn't on a checkpoint list, we
1654 * Returns non-zero if JBD no longer has an interest in the buffer.
1656 * Called under j_list_lock.
1658 * Called under jbd_lock_bh_state(bh).
1660 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1663 struct buffer_head *bh = jh2bh(jh);
1665 __jbd2_journal_unfile_buffer(jh);
1667 if (jh->b_cp_transaction) {
1668 JBUFFER_TRACE(jh, "on running+cp transaction");
1670 * We don't want to write the buffer anymore, clear the
1671 * bit so that we don't confuse checks in
1672 * __journal_file_buffer
1674 clear_buffer_dirty(bh);
1675 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1678 JBUFFER_TRACE(jh, "on running transaction");
1679 jbd2_journal_remove_journal_head(bh);
1686 * jbd2_journal_invalidatepage
1688 * This code is tricky. It has a number of cases to deal with.
1690 * There are two invariants which this code relies on:
1692 * i_size must be updated on disk before we start calling invalidatepage on the
1695 * This is done in ext3 by defining an ext3_setattr method which
1696 * updates i_size before truncate gets going. By maintaining this
1697 * invariant, we can be sure that it is safe to throw away any buffers
1698 * attached to the current transaction: once the transaction commits,
1699 * we know that the data will not be needed.
1701 * Note however that we can *not* throw away data belonging to the
1702 * previous, committing transaction!
1704 * Any disk blocks which *are* part of the previous, committing
1705 * transaction (and which therefore cannot be discarded immediately) are
1706 * not going to be reused in the new running transaction
1708 * The bitmap committed_data images guarantee this: any block which is
1709 * allocated in one transaction and removed in the next will be marked
1710 * as in-use in the committed_data bitmap, so cannot be reused until
1711 * the next transaction to delete the block commits. This means that
1712 * leaving committing buffers dirty is quite safe: the disk blocks
1713 * cannot be reallocated to a different file and so buffer aliasing is
1717 * The above applies mainly to ordered data mode. In writeback mode we
1718 * don't make guarantees about the order in which data hits disk --- in
1719 * particular we don't guarantee that new dirty data is flushed before
1720 * transaction commit --- so it is always safe just to discard data
1721 * immediately in that mode. --sct
1725 * The journal_unmap_buffer helper function returns zero if the buffer
1726 * concerned remains pinned as an anonymous buffer belonging to an older
1729 * We're outside-transaction here. Either or both of j_running_transaction
1730 * and j_committing_transaction may be NULL.
1732 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1734 transaction_t *transaction;
1735 struct journal_head *jh;
1739 BUFFER_TRACE(bh, "entry");
1742 * It is safe to proceed here without the j_list_lock because the
1743 * buffers cannot be stolen by try_to_free_buffers as long as we are
1744 * holding the page lock. --sct
1747 if (!buffer_jbd(bh))
1748 goto zap_buffer_unlocked;
1750 /* OK, we have data buffer in journaled mode */
1751 spin_lock(&journal->j_state_lock);
1752 jbd_lock_bh_state(bh);
1753 spin_lock(&journal->j_list_lock);
1755 jh = jbd2_journal_grab_journal_head(bh);
1757 goto zap_buffer_no_jh;
1760 * We cannot remove the buffer from checkpoint lists until the
1761 * transaction adding inode to orphan list (let's call it T)
1762 * is committed. Otherwise if the transaction changing the
1763 * buffer would be cleaned from the journal before T is
1764 * committed, a crash will cause that the correct contents of
1765 * the buffer will be lost. On the other hand we have to
1766 * clear the buffer dirty bit at latest at the moment when the
1767 * transaction marking the buffer as freed in the filesystem
1768 * structures is committed because from that moment on the
1769 * buffer can be reallocated and used by a different page.
1770 * Since the block hasn't been freed yet but the inode has
1771 * already been added to orphan list, it is safe for us to add
1772 * the buffer to BJ_Forget list of the newest transaction.
1774 transaction = jh->b_transaction;
1775 if (transaction == NULL) {
1776 /* First case: not on any transaction. If it
1777 * has no checkpoint link, then we can zap it:
1778 * it's a writeback-mode buffer so we don't care
1779 * if it hits disk safely. */
1780 if (!jh->b_cp_transaction) {
1781 JBUFFER_TRACE(jh, "not on any transaction: zap");
1785 if (!buffer_dirty(bh)) {
1786 /* bdflush has written it. We can drop it now */
1790 /* OK, it must be in the journal but still not
1791 * written fully to disk: it's metadata or
1792 * journaled data... */
1794 if (journal->j_running_transaction) {
1795 /* ... and once the current transaction has
1796 * committed, the buffer won't be needed any
1798 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1799 ret = __dispose_buffer(jh,
1800 journal->j_running_transaction);
1801 jbd2_journal_put_journal_head(jh);
1802 spin_unlock(&journal->j_list_lock);
1803 jbd_unlock_bh_state(bh);
1804 spin_unlock(&journal->j_state_lock);
1807 /* There is no currently-running transaction. So the
1808 * orphan record which we wrote for this file must have
1809 * passed into commit. We must attach this buffer to
1810 * the committing transaction, if it exists. */
1811 if (journal->j_committing_transaction) {
1812 JBUFFER_TRACE(jh, "give to committing trans");
1813 ret = __dispose_buffer(jh,
1814 journal->j_committing_transaction);
1815 jbd2_journal_put_journal_head(jh);
1816 spin_unlock(&journal->j_list_lock);
1817 jbd_unlock_bh_state(bh);
1818 spin_unlock(&journal->j_state_lock);
1821 /* The orphan record's transaction has
1822 * committed. We can cleanse this buffer */
1823 clear_buffer_jbddirty(bh);
1827 } else if (transaction == journal->j_committing_transaction) {
1828 JBUFFER_TRACE(jh, "on committing transaction");
1830 * The buffer is committing, we simply cannot touch
1831 * it. So we just set j_next_transaction to the
1832 * running transaction (if there is one) and mark
1833 * buffer as freed so that commit code knows it should
1834 * clear dirty bits when it is done with the buffer.
1836 set_buffer_freed(bh);
1837 if (journal->j_running_transaction && buffer_jbddirty(bh))
1838 jh->b_next_transaction = journal->j_running_transaction;
1839 jbd2_journal_put_journal_head(jh);
1840 spin_unlock(&journal->j_list_lock);
1841 jbd_unlock_bh_state(bh);
1842 spin_unlock(&journal->j_state_lock);
1845 /* Good, the buffer belongs to the running transaction.
1846 * We are writing our own transaction's data, not any
1847 * previous one's, so it is safe to throw it away
1848 * (remember that we expect the filesystem to have set
1849 * i_size already for this truncate so recovery will not
1850 * expose the disk blocks we are discarding here.) */
1851 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1852 JBUFFER_TRACE(jh, "on running transaction");
1853 may_free = __dispose_buffer(jh, transaction);
1857 jbd2_journal_put_journal_head(jh);
1859 spin_unlock(&journal->j_list_lock);
1860 jbd_unlock_bh_state(bh);
1861 spin_unlock(&journal->j_state_lock);
1862 zap_buffer_unlocked:
1863 clear_buffer_dirty(bh);
1864 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1865 clear_buffer_mapped(bh);
1866 clear_buffer_req(bh);
1867 clear_buffer_new(bh);
1873 * void jbd2_journal_invalidatepage()
1874 * @journal: journal to use for flush...
1875 * @page: page to flush
1876 * @offset: length of page to invalidate.
1878 * Reap page buffers containing data after offset in page.
1881 void jbd2_journal_invalidatepage(journal_t *journal,
1883 unsigned long offset)
1885 struct buffer_head *head, *bh, *next;
1886 unsigned int curr_off = 0;
1889 if (!PageLocked(page))
1891 if (!page_has_buffers(page))
1894 /* We will potentially be playing with lists other than just the
1895 * data lists (especially for journaled data mode), so be
1896 * cautious in our locking. */
1898 head = bh = page_buffers(page);
1900 unsigned int next_off = curr_off + bh->b_size;
1901 next = bh->b_this_page;
1903 if (offset <= curr_off) {
1904 /* This block is wholly outside the truncation point */
1906 may_free &= journal_unmap_buffer(journal, bh);
1909 curr_off = next_off;
1912 } while (bh != head);
1915 if (may_free && try_to_free_buffers(page))
1916 J_ASSERT(!page_has_buffers(page));
1921 * File a buffer on the given transaction list.
1923 void __jbd2_journal_file_buffer(struct journal_head *jh,
1924 transaction_t *transaction, int jlist)
1926 struct journal_head **list = NULL;
1928 struct buffer_head *bh = jh2bh(jh);
1930 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1931 assert_spin_locked(&transaction->t_journal->j_list_lock);
1933 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1934 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1935 jh->b_transaction == NULL);
1937 if (jh->b_transaction && jh->b_jlist == jlist)
1940 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1941 jlist == BJ_Shadow || jlist == BJ_Forget) {
1943 * For metadata buffers, we track dirty bit in buffer_jbddirty
1944 * instead of buffer_dirty. We should not see a dirty bit set
1945 * here because we clear it in do_get_write_access but e.g.
1946 * tune2fs can modify the sb and set the dirty bit at any time
1947 * so we try to gracefully handle that.
1949 if (buffer_dirty(bh))
1950 warn_dirty_buffer(bh);
1951 if (test_clear_buffer_dirty(bh) ||
1952 test_clear_buffer_jbddirty(bh))
1956 if (jh->b_transaction)
1957 __jbd2_journal_temp_unlink_buffer(jh);
1958 jh->b_transaction = transaction;
1962 J_ASSERT_JH(jh, !jh->b_committed_data);
1963 J_ASSERT_JH(jh, !jh->b_frozen_data);
1966 transaction->t_nr_buffers++;
1967 list = &transaction->t_buffers;
1970 list = &transaction->t_forget;
1973 list = &transaction->t_iobuf_list;
1976 list = &transaction->t_shadow_list;
1979 list = &transaction->t_log_list;
1982 list = &transaction->t_reserved_list;
1986 __blist_add_buffer(list, jh);
1987 jh->b_jlist = jlist;
1990 set_buffer_jbddirty(bh);
1993 void jbd2_journal_file_buffer(struct journal_head *jh,
1994 transaction_t *transaction, int jlist)
1996 jbd_lock_bh_state(jh2bh(jh));
1997 spin_lock(&transaction->t_journal->j_list_lock);
1998 __jbd2_journal_file_buffer(jh, transaction, jlist);
1999 spin_unlock(&transaction->t_journal->j_list_lock);
2000 jbd_unlock_bh_state(jh2bh(jh));
2004 * Remove a buffer from its current buffer list in preparation for
2005 * dropping it from its current transaction entirely. If the buffer has
2006 * already started to be used by a subsequent transaction, refile the
2007 * buffer on that transaction's metadata list.
2009 * Called under journal->j_list_lock
2011 * Called under jbd_lock_bh_state(jh2bh(jh))
2013 void __jbd2_journal_refile_buffer(struct journal_head *jh)
2015 int was_dirty, jlist;
2016 struct buffer_head *bh = jh2bh(jh);
2018 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2019 if (jh->b_transaction)
2020 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2022 /* If the buffer is now unused, just drop it. */
2023 if (jh->b_next_transaction == NULL) {
2024 __jbd2_journal_unfile_buffer(jh);
2029 * It has been modified by a later transaction: add it to the new
2030 * transaction's metadata list.
2033 was_dirty = test_clear_buffer_jbddirty(bh);
2034 __jbd2_journal_temp_unlink_buffer(jh);
2035 jh->b_transaction = jh->b_next_transaction;
2036 jh->b_next_transaction = NULL;
2037 if (buffer_freed(bh))
2039 else if (jh->b_modified)
2040 jlist = BJ_Metadata;
2042 jlist = BJ_Reserved;
2043 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2044 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2047 set_buffer_jbddirty(bh);
2051 * For the unlocked version of this call, also make sure that any
2052 * hanging journal_head is cleaned up if necessary.
2054 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2055 * operation on a buffer_head, in which the caller is probably going to
2056 * be hooking the journal_head onto other lists. In that case it is up
2057 * to the caller to remove the journal_head if necessary. For the
2058 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2059 * doing anything else to the buffer so we need to do the cleanup
2060 * ourselves to avoid a jh leak.
2062 * *** The journal_head may be freed by this call! ***
2064 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2066 struct buffer_head *bh = jh2bh(jh);
2068 jbd_lock_bh_state(bh);
2069 spin_lock(&journal->j_list_lock);
2071 __jbd2_journal_refile_buffer(jh);
2072 jbd_unlock_bh_state(bh);
2073 jbd2_journal_remove_journal_head(bh);
2075 spin_unlock(&journal->j_list_lock);
2080 * File inode in the inode list of the handle's transaction
2082 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2084 transaction_t *transaction = handle->h_transaction;
2085 journal_t *journal = transaction->t_journal;
2087 if (is_handle_aborted(handle))
2090 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2091 transaction->t_tid);
2094 * First check whether inode isn't already on the transaction's
2095 * lists without taking the lock. Note that this check is safe
2096 * without the lock as we cannot race with somebody removing inode
2097 * from the transaction. The reason is that we remove inode from the
2098 * transaction only in journal_release_jbd_inode() and when we commit
2099 * the transaction. We are guarded from the first case by holding
2100 * a reference to the inode. We are safe against the second case
2101 * because if jinode->i_transaction == transaction, commit code
2102 * cannot touch the transaction because we hold reference to it,
2103 * and if jinode->i_next_transaction == transaction, commit code
2104 * will only file the inode where we want it.
2106 if (jinode->i_transaction == transaction ||
2107 jinode->i_next_transaction == transaction)
2110 spin_lock(&journal->j_list_lock);
2112 if (jinode->i_transaction == transaction ||
2113 jinode->i_next_transaction == transaction)
2116 /* On some different transaction's list - should be
2117 * the committing one */
2118 if (jinode->i_transaction) {
2119 J_ASSERT(jinode->i_next_transaction == NULL);
2120 J_ASSERT(jinode->i_transaction ==
2121 journal->j_committing_transaction);
2122 jinode->i_next_transaction = transaction;
2125 /* Not on any transaction list... */
2126 J_ASSERT(!jinode->i_next_transaction);
2127 jinode->i_transaction = transaction;
2128 list_add(&jinode->i_list, &transaction->t_inode_list);
2130 spin_unlock(&journal->j_list_lock);
2136 * File truncate and transaction commit interact with each other in a
2137 * non-trivial way. If a transaction writing data block A is
2138 * committing, we cannot discard the data by truncate until we have
2139 * written them. Otherwise if we crashed after the transaction with
2140 * write has committed but before the transaction with truncate has
2141 * committed, we could see stale data in block A. This function is a
2142 * helper to solve this problem. It starts writeout of the truncated
2143 * part in case it is in the committing transaction.
2145 * Filesystem code must call this function when inode is journaled in
2146 * ordered mode before truncation happens and after the inode has been
2147 * placed on orphan list with the new inode size. The second condition
2148 * avoids the race that someone writes new data and we start
2149 * committing the transaction after this function has been called but
2150 * before a transaction for truncate is started (and furthermore it
2151 * allows us to optimize the case where the addition to orphan list
2152 * happens in the same transaction as write --- we don't have to write
2153 * any data in such case).
2155 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2156 struct jbd2_inode *jinode,
2159 transaction_t *inode_trans, *commit_trans;
2162 /* This is a quick check to avoid locking if not necessary */
2163 if (!jinode->i_transaction)
2165 /* Locks are here just to force reading of recent values, it is
2166 * enough that the transaction was not committing before we started
2167 * a transaction adding the inode to orphan list */
2168 spin_lock(&journal->j_state_lock);
2169 commit_trans = journal->j_committing_transaction;
2170 spin_unlock(&journal->j_state_lock);
2171 spin_lock(&journal->j_list_lock);
2172 inode_trans = jinode->i_transaction;
2173 spin_unlock(&journal->j_list_lock);
2174 if (inode_trans == commit_trans) {
2175 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2176 new_size, LLONG_MAX);
2178 jbd2_journal_abort(journal, ret);