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>
30 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
33 * jbd2_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.
47 static transaction_t *
48 jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
50 transaction->t_journal = journal;
51 transaction->t_state = T_RUNNING;
52 transaction->t_start_time = ktime_get();
53 transaction->t_tid = journal->j_transaction_sequence++;
54 transaction->t_expires = jiffies + journal->j_commit_interval;
55 spin_lock_init(&transaction->t_handle_lock);
56 INIT_LIST_HEAD(&transaction->t_inode_list);
57 INIT_LIST_HEAD(&transaction->t_private_list);
59 /* Set up the commit timer for the new transaction. */
60 journal->j_commit_timer.expires = round_jiffies(transaction->t_expires);
61 add_timer(&journal->j_commit_timer);
63 J_ASSERT(journal->j_running_transaction == NULL);
64 journal->j_running_transaction = transaction;
65 transaction->t_max_wait = 0;
66 transaction->t_start = jiffies;
74 * A handle_t is an object which represents a single atomic update to a
75 * filesystem, and which tracks all of the modifications which form part
80 * start_this_handle: Given a handle, deal with any locking or stalling
81 * needed to make sure that there is enough journal space for the handle
82 * to begin. Attach the handle to a transaction and set up the
83 * transaction's buffer credits.
86 static int start_this_handle(journal_t *journal, handle_t *handle)
88 transaction_t *transaction;
90 int nblocks = handle->h_buffer_credits;
91 transaction_t *new_transaction = NULL;
93 unsigned long ts = jiffies;
95 if (nblocks > journal->j_max_transaction_buffers) {
96 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
97 current->comm, nblocks,
98 journal->j_max_transaction_buffers);
104 if (!journal->j_running_transaction) {
105 new_transaction = kzalloc(sizeof(*new_transaction),
106 GFP_NOFS|__GFP_NOFAIL);
107 if (!new_transaction) {
113 jbd_debug(3, "New handle %p going live.\n", handle);
118 * We need to hold j_state_lock until t_updates has been incremented,
119 * for proper journal barrier handling
121 spin_lock(&journal->j_state_lock);
123 if (is_journal_aborted(journal) ||
124 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
125 spin_unlock(&journal->j_state_lock);
130 /* Wait on the journal's transaction barrier if necessary */
131 if (journal->j_barrier_count) {
132 spin_unlock(&journal->j_state_lock);
133 wait_event(journal->j_wait_transaction_locked,
134 journal->j_barrier_count == 0);
138 if (!journal->j_running_transaction) {
139 if (!new_transaction) {
140 spin_unlock(&journal->j_state_lock);
141 goto alloc_transaction;
143 jbd2_get_transaction(journal, new_transaction);
144 new_transaction = NULL;
147 transaction = journal->j_running_transaction;
150 * If the current transaction is locked down for commit, wait for the
151 * lock to be released.
153 if (transaction->t_state == T_LOCKED) {
156 prepare_to_wait(&journal->j_wait_transaction_locked,
157 &wait, TASK_UNINTERRUPTIBLE);
158 spin_unlock(&journal->j_state_lock);
160 finish_wait(&journal->j_wait_transaction_locked, &wait);
165 * If there is not enough space left in the log to write all potential
166 * buffers requested by this operation, we need to stall pending a log
167 * checkpoint to free some more log space.
169 spin_lock(&transaction->t_handle_lock);
170 needed = transaction->t_outstanding_credits + nblocks;
172 if (needed > journal->j_max_transaction_buffers) {
174 * If the current transaction is already too large, then start
175 * to commit it: we can then go back and attach this handle to
180 jbd_debug(2, "Handle %p starting new commit...\n", handle);
181 spin_unlock(&transaction->t_handle_lock);
182 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
183 TASK_UNINTERRUPTIBLE);
184 __jbd2_log_start_commit(journal, transaction->t_tid);
185 spin_unlock(&journal->j_state_lock);
187 finish_wait(&journal->j_wait_transaction_locked, &wait);
192 * The commit code assumes that it can get enough log space
193 * without forcing a checkpoint. This is *critical* for
194 * correctness: a checkpoint of a buffer which is also
195 * associated with a committing transaction creates a deadlock,
196 * so commit simply cannot force through checkpoints.
198 * We must therefore ensure the necessary space in the journal
199 * *before* starting to dirty potentially checkpointed buffers
200 * in the new transaction.
202 * The worst part is, any transaction currently committing can
203 * reduce the free space arbitrarily. Be careful to account for
204 * those buffers when checkpointing.
208 * @@@ AKPM: This seems rather over-defensive. We're giving commit
209 * a _lot_ of headroom: 1/4 of the journal plus the size of
210 * the committing transaction. Really, we only need to give it
211 * committing_transaction->t_outstanding_credits plus "enough" for
212 * the log control blocks.
213 * Also, this test is inconsitent with the matching one in
214 * jbd2_journal_extend().
216 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
217 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
218 spin_unlock(&transaction->t_handle_lock);
219 __jbd2_log_wait_for_space(journal);
223 /* OK, account for the buffers that this operation expects to
224 * use and add the handle to the running transaction. */
226 if (time_after(transaction->t_start, ts)) {
227 ts = jbd2_time_diff(ts, transaction->t_start);
228 if (ts > transaction->t_max_wait)
229 transaction->t_max_wait = ts;
232 handle->h_transaction = transaction;
233 transaction->t_outstanding_credits += nblocks;
234 transaction->t_updates++;
235 transaction->t_handle_count++;
236 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
237 handle, nblocks, transaction->t_outstanding_credits,
238 __jbd2_log_space_left(journal));
239 spin_unlock(&transaction->t_handle_lock);
240 spin_unlock(&journal->j_state_lock);
242 if (unlikely(new_transaction)) /* It's usually NULL */
243 kfree(new_transaction);
247 static struct lock_class_key jbd2_handle_key;
249 /* Allocate a new handle. This should probably be in a slab... */
250 static handle_t *new_handle(int nblocks)
252 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
255 memset(handle, 0, sizeof(*handle));
256 handle->h_buffer_credits = nblocks;
259 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
260 &jbd2_handle_key, 0);
266 * handle_t *jbd2_journal_start() - Obtain a new handle.
267 * @journal: Journal to start transaction on.
268 * @nblocks: number of block buffer we might modify
270 * We make sure that the transaction can guarantee at least nblocks of
271 * modified buffers in the log. We block until the log can guarantee
274 * This function is visible to journal users (like ext3fs), so is not
275 * called with the journal already locked.
277 * Return a pointer to a newly allocated handle, or NULL on failure
279 handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
281 handle_t *handle = journal_current_handle();
285 return ERR_PTR(-EROFS);
288 J_ASSERT(handle->h_transaction->t_journal == journal);
293 handle = new_handle(nblocks);
295 return ERR_PTR(-ENOMEM);
297 current->journal_info = handle;
299 err = start_this_handle(journal, handle);
301 jbd2_free_handle(handle);
302 current->journal_info = NULL;
303 handle = ERR_PTR(err);
307 lock_map_acquire(&handle->h_lockdep_map);
313 * int jbd2_journal_extend() - extend buffer credits.
314 * @handle: handle to 'extend'
315 * @nblocks: nr blocks to try to extend by.
317 * Some transactions, such as large extends and truncates, can be done
318 * atomically all at once or in several stages. The operation requests
319 * a credit for a number of buffer modications in advance, but can
320 * extend its credit if it needs more.
322 * jbd2_journal_extend tries to give the running handle more buffer credits.
323 * It does not guarantee that allocation - this is a best-effort only.
324 * The calling process MUST be able to deal cleanly with a failure to
327 * Return 0 on success, non-zero on failure.
329 * return code < 0 implies an error
330 * return code > 0 implies normal transaction-full status.
332 int jbd2_journal_extend(handle_t *handle, int nblocks)
334 transaction_t *transaction = handle->h_transaction;
335 journal_t *journal = transaction->t_journal;
340 if (is_handle_aborted(handle))
345 spin_lock(&journal->j_state_lock);
347 /* Don't extend a locked-down transaction! */
348 if (handle->h_transaction->t_state != T_RUNNING) {
349 jbd_debug(3, "denied handle %p %d blocks: "
350 "transaction not running\n", handle, nblocks);
354 spin_lock(&transaction->t_handle_lock);
355 wanted = transaction->t_outstanding_credits + nblocks;
357 if (wanted > journal->j_max_transaction_buffers) {
358 jbd_debug(3, "denied handle %p %d blocks: "
359 "transaction too large\n", handle, nblocks);
363 if (wanted > __jbd2_log_space_left(journal)) {
364 jbd_debug(3, "denied handle %p %d blocks: "
365 "insufficient log space\n", handle, nblocks);
369 handle->h_buffer_credits += nblocks;
370 transaction->t_outstanding_credits += nblocks;
373 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
375 spin_unlock(&transaction->t_handle_lock);
377 spin_unlock(&journal->j_state_lock);
384 * int jbd2_journal_restart() - restart a handle .
385 * @handle: handle to restart
386 * @nblocks: nr credits requested
388 * Restart a handle for a multi-transaction filesystem
391 * If the jbd2_journal_extend() call above fails to grant new buffer credits
392 * to a running handle, a call to jbd2_journal_restart will commit the
393 * handle's transaction so far and reattach the handle to a new
394 * transaction capabable of guaranteeing the requested number of
398 int jbd2_journal_restart(handle_t *handle, int nblocks)
400 transaction_t *transaction = handle->h_transaction;
401 journal_t *journal = transaction->t_journal;
404 /* If we've had an abort of any type, don't even think about
405 * actually doing the restart! */
406 if (is_handle_aborted(handle))
410 * First unlink the handle from its current transaction, and start the
413 J_ASSERT(transaction->t_updates > 0);
414 J_ASSERT(journal_current_handle() == handle);
416 spin_lock(&journal->j_state_lock);
417 spin_lock(&transaction->t_handle_lock);
418 transaction->t_outstanding_credits -= handle->h_buffer_credits;
419 transaction->t_updates--;
421 if (!transaction->t_updates)
422 wake_up(&journal->j_wait_updates);
423 spin_unlock(&transaction->t_handle_lock);
425 jbd_debug(2, "restarting handle %p\n", handle);
426 __jbd2_log_start_commit(journal, transaction->t_tid);
427 spin_unlock(&journal->j_state_lock);
429 handle->h_buffer_credits = nblocks;
430 ret = start_this_handle(journal, handle);
436 * void jbd2_journal_lock_updates () - establish a transaction barrier.
437 * @journal: Journal to establish a barrier on.
439 * This locks out any further updates from being started, and blocks
440 * until all existing updates have completed, returning only once the
441 * journal is in a quiescent state with no updates running.
443 * The journal lock should not be held on entry.
445 void jbd2_journal_lock_updates(journal_t *journal)
449 spin_lock(&journal->j_state_lock);
450 ++journal->j_barrier_count;
452 /* Wait until there are no running updates */
454 transaction_t *transaction = journal->j_running_transaction;
459 spin_lock(&transaction->t_handle_lock);
460 if (!transaction->t_updates) {
461 spin_unlock(&transaction->t_handle_lock);
464 prepare_to_wait(&journal->j_wait_updates, &wait,
465 TASK_UNINTERRUPTIBLE);
466 spin_unlock(&transaction->t_handle_lock);
467 spin_unlock(&journal->j_state_lock);
469 finish_wait(&journal->j_wait_updates, &wait);
470 spin_lock(&journal->j_state_lock);
472 spin_unlock(&journal->j_state_lock);
475 * We have now established a barrier against other normal updates, but
476 * we also need to barrier against other jbd2_journal_lock_updates() calls
477 * to make sure that we serialise special journal-locked operations
480 mutex_lock(&journal->j_barrier);
484 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
485 * @journal: Journal to release the barrier on.
487 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
489 * Should be called without the journal lock held.
491 void jbd2_journal_unlock_updates (journal_t *journal)
493 J_ASSERT(journal->j_barrier_count != 0);
495 mutex_unlock(&journal->j_barrier);
496 spin_lock(&journal->j_state_lock);
497 --journal->j_barrier_count;
498 spin_unlock(&journal->j_state_lock);
499 wake_up(&journal->j_wait_transaction_locked);
503 * Report any unexpected dirty buffers which turn up. Normally those
504 * indicate an error, but they can occur if the user is running (say)
505 * tune2fs to modify the live filesystem, so we need the option of
506 * continuing as gracefully as possible. #
508 * The caller should already hold the journal lock and
509 * j_list_lock spinlock: most callers will need those anyway
510 * in order to probe the buffer's journaling state safely.
512 static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
516 /* If this buffer is one which might reasonably be dirty
517 * --- ie. data, or not part of this journal --- then
518 * we're OK to leave it alone, but otherwise we need to
519 * move the dirty bit to the journal's own internal
523 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
524 jlist == BJ_Shadow || jlist == BJ_Forget) {
525 struct buffer_head *bh = jh2bh(jh);
527 if (test_clear_buffer_dirty(bh))
528 set_buffer_jbddirty(bh);
533 * If the buffer is already part of the current transaction, then there
534 * is nothing we need to do. If it is already part of a prior
535 * transaction which we are still committing to disk, then we need to
536 * make sure that we do not overwrite the old copy: we do copy-out to
537 * preserve the copy going to disk. We also account the buffer against
538 * the handle's metadata buffer credits (unless the buffer is already
539 * part of the transaction, that is).
543 do_get_write_access(handle_t *handle, struct journal_head *jh,
546 struct buffer_head *bh;
547 transaction_t *transaction;
550 char *frozen_buffer = NULL;
553 if (is_handle_aborted(handle))
556 transaction = handle->h_transaction;
557 journal = transaction->t_journal;
559 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
561 JBUFFER_TRACE(jh, "entry");
565 /* @@@ Need to check for errors here at some point. */
568 jbd_lock_bh_state(bh);
570 /* We now hold the buffer lock so it is safe to query the buffer
571 * state. Is the buffer dirty?
573 * If so, there are two possibilities. The buffer may be
574 * non-journaled, and undergoing a quite legitimate writeback.
575 * Otherwise, it is journaled, and we don't expect dirty buffers
576 * in that state (the buffers should be marked JBD_Dirty
577 * instead.) So either the IO is being done under our own
578 * control and this is a bug, or it's a third party IO such as
579 * dump(8) (which may leave the buffer scheduled for read ---
580 * ie. locked but not dirty) or tune2fs (which may actually have
581 * the buffer dirtied, ugh.) */
583 if (buffer_dirty(bh)) {
585 * First question: is this buffer already part of the current
586 * transaction or the existing committing transaction?
588 if (jh->b_transaction) {
590 jh->b_transaction == transaction ||
592 journal->j_committing_transaction);
593 if (jh->b_next_transaction)
594 J_ASSERT_JH(jh, jh->b_next_transaction ==
598 * In any case we need to clean the dirty flag and we must
599 * do it under the buffer lock to be sure we don't race
600 * with running write-out.
602 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
603 jbd_unexpected_dirty_buffer(jh);
609 if (is_handle_aborted(handle)) {
610 jbd_unlock_bh_state(bh);
616 * The buffer is already part of this transaction if b_transaction or
617 * b_next_transaction points to it
619 if (jh->b_transaction == transaction ||
620 jh->b_next_transaction == transaction)
624 * this is the first time this transaction is touching this buffer,
625 * reset the modified flag
630 * If there is already a copy-out version of this buffer, then we don't
631 * need to make another one
633 if (jh->b_frozen_data) {
634 JBUFFER_TRACE(jh, "has frozen data");
635 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
636 jh->b_next_transaction = transaction;
640 /* Is there data here we need to preserve? */
642 if (jh->b_transaction && jh->b_transaction != transaction) {
643 JBUFFER_TRACE(jh, "owned by older transaction");
644 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
645 J_ASSERT_JH(jh, jh->b_transaction ==
646 journal->j_committing_transaction);
648 /* There is one case we have to be very careful about.
649 * If the committing transaction is currently writing
650 * this buffer out to disk and has NOT made a copy-out,
651 * then we cannot modify the buffer contents at all
652 * right now. The essence of copy-out is that it is the
653 * extra copy, not the primary copy, which gets
654 * journaled. If the primary copy is already going to
655 * disk then we cannot do copy-out here. */
657 if (jh->b_jlist == BJ_Shadow) {
658 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
659 wait_queue_head_t *wqh;
661 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
663 JBUFFER_TRACE(jh, "on shadow: sleep");
664 jbd_unlock_bh_state(bh);
665 /* commit wakes up all shadow buffers after IO */
667 prepare_to_wait(wqh, &wait.wait,
668 TASK_UNINTERRUPTIBLE);
669 if (jh->b_jlist != BJ_Shadow)
673 finish_wait(wqh, &wait.wait);
677 /* Only do the copy if the currently-owning transaction
678 * still needs it. If it is on the Forget list, the
679 * committing transaction is past that stage. The
680 * buffer had better remain locked during the kmalloc,
681 * but that should be true --- we hold the journal lock
682 * still and the buffer is already on the BUF_JOURNAL
683 * list so won't be flushed.
685 * Subtle point, though: if this is a get_undo_access,
686 * then we will be relying on the frozen_data to contain
687 * the new value of the committed_data record after the
688 * transaction, so we HAVE to force the frozen_data copy
691 if (jh->b_jlist != BJ_Forget || force_copy) {
692 JBUFFER_TRACE(jh, "generate frozen data");
693 if (!frozen_buffer) {
694 JBUFFER_TRACE(jh, "allocate memory for buffer");
695 jbd_unlock_bh_state(bh);
697 jbd2_alloc(jh2bh(jh)->b_size,
699 if (!frozen_buffer) {
701 "%s: OOM for frozen_buffer\n",
703 JBUFFER_TRACE(jh, "oom!");
705 jbd_lock_bh_state(bh);
710 jh->b_frozen_data = frozen_buffer;
711 frozen_buffer = NULL;
714 jh->b_next_transaction = transaction;
719 * Finally, if the buffer is not journaled right now, we need to make
720 * sure it doesn't get written to disk before the caller actually
721 * commits the new data
723 if (!jh->b_transaction) {
724 JBUFFER_TRACE(jh, "no transaction");
725 J_ASSERT_JH(jh, !jh->b_next_transaction);
726 jh->b_transaction = transaction;
727 JBUFFER_TRACE(jh, "file as BJ_Reserved");
728 spin_lock(&journal->j_list_lock);
729 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
730 spin_unlock(&journal->j_list_lock);
739 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
740 "Possible IO failure.\n");
741 page = jh2bh(jh)->b_page;
742 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
743 source = kmap_atomic(page, KM_USER0);
744 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
745 kunmap_atomic(source, KM_USER0);
747 jbd_unlock_bh_state(bh);
750 * If we are about to journal a buffer, then any revoke pending on it is
753 jbd2_journal_cancel_revoke(handle, jh);
756 if (unlikely(frozen_buffer)) /* It's usually NULL */
757 jbd2_free(frozen_buffer, bh->b_size);
759 JBUFFER_TRACE(jh, "exit");
764 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
765 * @handle: transaction to add buffer modifications to
766 * @bh: bh to be used for metadata writes
767 * @credits: variable that will receive credits for the buffer
769 * Returns an error code or 0 on success.
771 * In full data journalling mode the buffer may be of type BJ_AsyncData,
772 * because we're write()ing a buffer which is also part of a shared mapping.
775 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
777 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
780 /* We do not want to get caught playing with fields which the
781 * log thread also manipulates. Make sure that the buffer
782 * completes any outstanding IO before proceeding. */
783 rc = do_get_write_access(handle, jh, 0);
784 jbd2_journal_put_journal_head(jh);
790 * When the user wants to journal a newly created buffer_head
791 * (ie. getblk() returned a new buffer and we are going to populate it
792 * manually rather than reading off disk), then we need to keep the
793 * buffer_head locked until it has been completely filled with new
794 * data. In this case, we should be able to make the assertion that
795 * the bh is not already part of an existing transaction.
797 * The buffer should already be locked by the caller by this point.
798 * There is no lock ranking violation: it was a newly created,
799 * unlocked buffer beforehand. */
802 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
803 * @handle: transaction to new buffer to
806 * Call this if you create a new bh.
808 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
810 transaction_t *transaction = handle->h_transaction;
811 journal_t *journal = transaction->t_journal;
812 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
815 jbd_debug(5, "journal_head %p\n", jh);
817 if (is_handle_aborted(handle))
821 JBUFFER_TRACE(jh, "entry");
823 * The buffer may already belong to this transaction due to pre-zeroing
824 * in the filesystem's new_block code. It may also be on the previous,
825 * committing transaction's lists, but it HAS to be in Forget state in
826 * that case: the transaction must have deleted the buffer for it to be
829 jbd_lock_bh_state(bh);
830 spin_lock(&journal->j_list_lock);
831 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
832 jh->b_transaction == NULL ||
833 (jh->b_transaction == journal->j_committing_transaction &&
834 jh->b_jlist == BJ_Forget)));
836 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
837 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
839 if (jh->b_transaction == NULL) {
840 jh->b_transaction = transaction;
842 /* first access by this transaction */
845 JBUFFER_TRACE(jh, "file as BJ_Reserved");
846 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
847 } else if (jh->b_transaction == journal->j_committing_transaction) {
848 /* first access by this transaction */
851 JBUFFER_TRACE(jh, "set next transaction");
852 jh->b_next_transaction = transaction;
854 spin_unlock(&journal->j_list_lock);
855 jbd_unlock_bh_state(bh);
858 * akpm: I added this. ext3_alloc_branch can pick up new indirect
859 * blocks which contain freed but then revoked metadata. We need
860 * to cancel the revoke in case we end up freeing it yet again
861 * and the reallocating as data - this would cause a second revoke,
862 * which hits an assertion error.
864 JBUFFER_TRACE(jh, "cancelling revoke");
865 jbd2_journal_cancel_revoke(handle, jh);
866 jbd2_journal_put_journal_head(jh);
872 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
873 * non-rewindable consequences
874 * @handle: transaction
875 * @bh: buffer to undo
876 * @credits: store the number of taken credits here (if not NULL)
878 * Sometimes there is a need to distinguish between metadata which has
879 * been committed to disk and that which has not. The ext3fs code uses
880 * this for freeing and allocating space, we have to make sure that we
881 * do not reuse freed space until the deallocation has been committed,
882 * since if we overwrote that space we would make the delete
883 * un-rewindable in case of a crash.
885 * To deal with that, jbd2_journal_get_undo_access requests write access to a
886 * buffer for parts of non-rewindable operations such as delete
887 * operations on the bitmaps. The journaling code must keep a copy of
888 * the buffer's contents prior to the undo_access call until such time
889 * as we know that the buffer has definitely been committed to disk.
891 * We never need to know which transaction the committed data is part
892 * of, buffers touched here are guaranteed to be dirtied later and so
893 * will be committed to a new transaction in due course, at which point
894 * we can discard the old committed data pointer.
896 * Returns error number or 0 on success.
898 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
901 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
902 char *committed_data = NULL;
904 JBUFFER_TRACE(jh, "entry");
907 * Do this first --- it can drop the journal lock, so we want to
908 * make sure that obtaining the committed_data is done
909 * atomically wrt. completion of any outstanding commits.
911 err = do_get_write_access(handle, jh, 1);
916 if (!jh->b_committed_data) {
917 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
918 if (!committed_data) {
919 printk(KERN_EMERG "%s: No memory for committed data\n",
926 jbd_lock_bh_state(bh);
927 if (!jh->b_committed_data) {
928 /* Copy out the current buffer contents into the
929 * preserved, committed copy. */
930 JBUFFER_TRACE(jh, "generate b_committed data");
931 if (!committed_data) {
932 jbd_unlock_bh_state(bh);
936 jh->b_committed_data = committed_data;
937 committed_data = NULL;
938 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
940 jbd_unlock_bh_state(bh);
942 jbd2_journal_put_journal_head(jh);
943 if (unlikely(committed_data))
944 jbd2_free(committed_data, bh->b_size);
949 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
950 * @handle: transaction to add buffer to.
951 * @bh: buffer to mark
953 * mark dirty metadata which needs to be journaled as part of the current
956 * The buffer is placed on the transaction's metadata list and is marked
957 * as belonging to the transaction.
959 * Returns error number or 0 on success.
961 * Special care needs to be taken if the buffer already belongs to the
962 * current committing transaction (in which case we should have frozen
963 * data present for that commit). In that case, we don't relink the
964 * buffer: that only gets done when the old transaction finally
965 * completes its commit.
967 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
969 transaction_t *transaction = handle->h_transaction;
970 journal_t *journal = transaction->t_journal;
971 struct journal_head *jh = bh2jh(bh);
973 jbd_debug(5, "journal_head %p\n", jh);
974 JBUFFER_TRACE(jh, "entry");
975 if (is_handle_aborted(handle))
978 jbd_lock_bh_state(bh);
980 if (jh->b_modified == 0) {
982 * This buffer's got modified and becoming part
983 * of the transaction. This needs to be done
984 * once a transaction -bzzz
987 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
988 handle->h_buffer_credits--;
992 * fastpath, to avoid expensive locking. If this buffer is already
993 * on the running transaction's metadata list there is nothing to do.
994 * Nobody can take it off again because there is a handle open.
995 * I _think_ we're OK here with SMP barriers - a mistaken decision will
996 * result in this test being false, so we go in and take the locks.
998 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
999 JBUFFER_TRACE(jh, "fastpath");
1000 J_ASSERT_JH(jh, jh->b_transaction ==
1001 journal->j_running_transaction);
1005 set_buffer_jbddirty(bh);
1008 * Metadata already on the current transaction list doesn't
1009 * need to be filed. Metadata on another transaction's list must
1010 * be committing, and will be refiled once the commit completes:
1011 * leave it alone for now.
1013 if (jh->b_transaction != transaction) {
1014 JBUFFER_TRACE(jh, "already on other transaction");
1015 J_ASSERT_JH(jh, jh->b_transaction ==
1016 journal->j_committing_transaction);
1017 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1018 /* And this case is illegal: we can't reuse another
1019 * transaction's data buffer, ever. */
1023 /* That test should have eliminated the following case: */
1024 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1026 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1027 spin_lock(&journal->j_list_lock);
1028 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1029 spin_unlock(&journal->j_list_lock);
1031 jbd_unlock_bh_state(bh);
1033 JBUFFER_TRACE(jh, "exit");
1038 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
1039 * updates, if the update decided in the end that it didn't need access.
1043 jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1045 BUFFER_TRACE(bh, "entry");
1049 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1050 * @handle: transaction handle
1051 * @bh: bh to 'forget'
1053 * We can only do the bforget if there are no commits pending against the
1054 * buffer. If the buffer is dirty in the current running transaction we
1055 * can safely unlink it.
1057 * bh may not be a journalled buffer at all - it may be a non-JBD
1058 * buffer which came off the hashtable. Check for this.
1060 * Decrements bh->b_count by one.
1062 * Allow this call even if the handle has aborted --- it may be part of
1063 * the caller's cleanup after an abort.
1065 int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1067 transaction_t *transaction = handle->h_transaction;
1068 journal_t *journal = transaction->t_journal;
1069 struct journal_head *jh;
1070 int drop_reserve = 0;
1072 int was_modified = 0;
1074 BUFFER_TRACE(bh, "entry");
1076 jbd_lock_bh_state(bh);
1077 spin_lock(&journal->j_list_lock);
1079 if (!buffer_jbd(bh))
1083 /* Critical error: attempting to delete a bitmap buffer, maybe?
1084 * Don't do any jbd operations, and return an error. */
1085 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1086 "inconsistent data on disk")) {
1091 /* keep track of wether or not this transaction modified us */
1092 was_modified = jh->b_modified;
1095 * The buffer's going from the transaction, we must drop
1096 * all references -bzzz
1100 if (jh->b_transaction == handle->h_transaction) {
1101 J_ASSERT_JH(jh, !jh->b_frozen_data);
1103 /* If we are forgetting a buffer which is already part
1104 * of this transaction, then we can just drop it from
1105 * the transaction immediately. */
1106 clear_buffer_dirty(bh);
1107 clear_buffer_jbddirty(bh);
1109 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1112 * we only want to drop a reference if this transaction
1113 * modified the buffer
1119 * We are no longer going to journal this buffer.
1120 * However, the commit of this transaction is still
1121 * important to the buffer: the delete that we are now
1122 * processing might obsolete an old log entry, so by
1123 * committing, we can satisfy the buffer's checkpoint.
1125 * So, if we have a checkpoint on the buffer, we should
1126 * now refile the buffer on our BJ_Forget list so that
1127 * we know to remove the checkpoint after we commit.
1130 if (jh->b_cp_transaction) {
1131 __jbd2_journal_temp_unlink_buffer(jh);
1132 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1134 __jbd2_journal_unfile_buffer(jh);
1135 jbd2_journal_remove_journal_head(bh);
1137 if (!buffer_jbd(bh)) {
1138 spin_unlock(&journal->j_list_lock);
1139 jbd_unlock_bh_state(bh);
1144 } else if (jh->b_transaction) {
1145 J_ASSERT_JH(jh, (jh->b_transaction ==
1146 journal->j_committing_transaction));
1147 /* However, if the buffer is still owned by a prior
1148 * (committing) transaction, we can't drop it yet... */
1149 JBUFFER_TRACE(jh, "belongs to older transaction");
1150 /* ... but we CAN drop it from the new transaction if we
1151 * have also modified it since the original commit. */
1153 if (jh->b_next_transaction) {
1154 J_ASSERT(jh->b_next_transaction == transaction);
1155 jh->b_next_transaction = NULL;
1158 * only drop a reference if this transaction modified
1167 spin_unlock(&journal->j_list_lock);
1168 jbd_unlock_bh_state(bh);
1172 /* no need to reserve log space for this block -bzzz */
1173 handle->h_buffer_credits++;
1179 * int jbd2_journal_stop() - complete a transaction
1180 * @handle: tranaction to complete.
1182 * All done for a particular handle.
1184 * There is not much action needed here. We just return any remaining
1185 * buffer credits to the transaction and remove the handle. The only
1186 * complication is that we need to start a commit operation if the
1187 * filesystem is marked for synchronous update.
1189 * jbd2_journal_stop itself will not usually return an error, but it may
1190 * do so in unusual circumstances. In particular, expect it to
1191 * return -EIO if a jbd2_journal_abort has been executed since the
1192 * transaction began.
1194 int jbd2_journal_stop(handle_t *handle)
1196 transaction_t *transaction = handle->h_transaction;
1197 journal_t *journal = transaction->t_journal;
1201 J_ASSERT(journal_current_handle() == handle);
1203 if (is_handle_aborted(handle))
1206 J_ASSERT(transaction->t_updates > 0);
1210 if (--handle->h_ref > 0) {
1211 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1216 jbd_debug(4, "Handle %p going down\n", handle);
1219 * Implement synchronous transaction batching. If the handle
1220 * was synchronous, don't force a commit immediately. Let's
1221 * yield and let another thread piggyback onto this
1222 * transaction. Keep doing that while new threads continue to
1223 * arrive. It doesn't cost much - we're about to run a commit
1224 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1225 * operations by 30x or more...
1227 * We try and optimize the sleep time against what the
1228 * underlying disk can do, instead of having a static sleep
1229 * time. This is useful for the case where our storage is so
1230 * fast that it is more optimal to go ahead and force a flush
1231 * and wait for the transaction to be committed than it is to
1232 * wait for an arbitrary amount of time for new writers to
1233 * join the transaction. We achieve this by measuring how
1234 * long it takes to commit a transaction, and compare it with
1235 * how long this transaction has been running, and if run time
1236 * < commit time then we sleep for the delta and commit. This
1237 * greatly helps super fast disks that would see slowdowns as
1238 * more threads started doing fsyncs.
1240 * But don't do this if this process was the most recent one
1241 * to perform a synchronous write. We do this to detect the
1242 * case where a single process is doing a stream of sync
1243 * writes. No point in waiting for joiners in that case.
1246 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1247 u64 commit_time, trans_time;
1249 journal->j_last_sync_writer = pid;
1251 spin_lock(&journal->j_state_lock);
1252 commit_time = journal->j_average_commit_time;
1253 spin_unlock(&journal->j_state_lock);
1255 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1256 transaction->t_start_time));
1258 commit_time = min_t(u64, commit_time,
1259 1000*jiffies_to_usecs(1));
1261 if (trans_time < commit_time) {
1262 ktime_t expires = ktime_add_ns(ktime_get(),
1264 set_current_state(TASK_UNINTERRUPTIBLE);
1265 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1269 current->journal_info = NULL;
1270 spin_lock(&journal->j_state_lock);
1271 spin_lock(&transaction->t_handle_lock);
1272 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1273 transaction->t_updates--;
1274 if (!transaction->t_updates) {
1275 wake_up(&journal->j_wait_updates);
1276 if (journal->j_barrier_count)
1277 wake_up(&journal->j_wait_transaction_locked);
1281 * If the handle is marked SYNC, we need to set another commit
1282 * going! We also want to force a commit if the current
1283 * transaction is occupying too much of the log, or if the
1284 * transaction is too old now.
1286 if (handle->h_sync ||
1287 transaction->t_outstanding_credits >
1288 journal->j_max_transaction_buffers ||
1289 time_after_eq(jiffies, transaction->t_expires)) {
1290 /* Do this even for aborted journals: an abort still
1291 * completes the commit thread, it just doesn't write
1292 * anything to disk. */
1293 tid_t tid = transaction->t_tid;
1295 spin_unlock(&transaction->t_handle_lock);
1296 jbd_debug(2, "transaction too old, requesting commit for "
1297 "handle %p\n", handle);
1298 /* This is non-blocking */
1299 __jbd2_log_start_commit(journal, transaction->t_tid);
1300 spin_unlock(&journal->j_state_lock);
1303 * Special case: JBD2_SYNC synchronous updates require us
1304 * to wait for the commit to complete.
1306 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1307 err = jbd2_log_wait_commit(journal, tid);
1309 spin_unlock(&transaction->t_handle_lock);
1310 spin_unlock(&journal->j_state_lock);
1313 lock_map_release(&handle->h_lockdep_map);
1315 jbd2_free_handle(handle);
1320 * int jbd2_journal_force_commit() - force any uncommitted transactions
1321 * @journal: journal to force
1323 * For synchronous operations: force any uncommitted transactions
1324 * to disk. May seem kludgy, but it reuses all the handle batching
1325 * code in a very simple manner.
1327 int jbd2_journal_force_commit(journal_t *journal)
1332 handle = jbd2_journal_start(journal, 1);
1333 if (IS_ERR(handle)) {
1334 ret = PTR_ERR(handle);
1337 ret = jbd2_journal_stop(handle);
1344 * List management code snippets: various functions for manipulating the
1345 * transaction buffer lists.
1350 * Append a buffer to a transaction list, given the transaction's list head
1353 * j_list_lock is held.
1355 * jbd_lock_bh_state(jh2bh(jh)) is held.
1359 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1362 jh->b_tnext = jh->b_tprev = jh;
1365 /* Insert at the tail of the list to preserve order */
1366 struct journal_head *first = *list, *last = first->b_tprev;
1368 jh->b_tnext = first;
1369 last->b_tnext = first->b_tprev = jh;
1374 * Remove a buffer from a transaction list, given the transaction's list
1377 * Called with j_list_lock held, and the journal may not be locked.
1379 * jbd_lock_bh_state(jh2bh(jh)) is held.
1383 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1386 *list = jh->b_tnext;
1390 jh->b_tprev->b_tnext = jh->b_tnext;
1391 jh->b_tnext->b_tprev = jh->b_tprev;
1395 * Remove a buffer from the appropriate transaction list.
1397 * Note that this function can *change* the value of
1398 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1399 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1400 * of these pointers, it could go bad. Generally the caller needs to re-read
1401 * the pointer from the transaction_t.
1403 * Called under j_list_lock. The journal may not be locked.
1405 void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1407 struct journal_head **list = NULL;
1408 transaction_t *transaction;
1409 struct buffer_head *bh = jh2bh(jh);
1411 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1412 transaction = jh->b_transaction;
1414 assert_spin_locked(&transaction->t_journal->j_list_lock);
1416 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1417 if (jh->b_jlist != BJ_None)
1418 J_ASSERT_JH(jh, transaction != NULL);
1420 switch (jh->b_jlist) {
1424 transaction->t_nr_buffers--;
1425 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1426 list = &transaction->t_buffers;
1429 list = &transaction->t_forget;
1432 list = &transaction->t_iobuf_list;
1435 list = &transaction->t_shadow_list;
1438 list = &transaction->t_log_list;
1441 list = &transaction->t_reserved_list;
1445 __blist_del_buffer(list, jh);
1446 jh->b_jlist = BJ_None;
1447 if (test_clear_buffer_jbddirty(bh))
1448 mark_buffer_dirty(bh); /* Expose it to the VM */
1451 void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1453 __jbd2_journal_temp_unlink_buffer(jh);
1454 jh->b_transaction = NULL;
1457 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1459 jbd_lock_bh_state(jh2bh(jh));
1460 spin_lock(&journal->j_list_lock);
1461 __jbd2_journal_unfile_buffer(jh);
1462 spin_unlock(&journal->j_list_lock);
1463 jbd_unlock_bh_state(jh2bh(jh));
1467 * Called from jbd2_journal_try_to_free_buffers().
1469 * Called under jbd_lock_bh_state(bh)
1472 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1474 struct journal_head *jh;
1478 if (buffer_locked(bh) || buffer_dirty(bh))
1481 if (jh->b_next_transaction != NULL)
1484 spin_lock(&journal->j_list_lock);
1485 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1486 /* written-back checkpointed metadata buffer */
1487 if (jh->b_jlist == BJ_None) {
1488 JBUFFER_TRACE(jh, "remove from checkpoint list");
1489 __jbd2_journal_remove_checkpoint(jh);
1490 jbd2_journal_remove_journal_head(bh);
1494 spin_unlock(&journal->j_list_lock);
1500 * jbd2_journal_try_to_free_buffers() could race with
1501 * jbd2_journal_commit_transaction(). The later might still hold the
1502 * reference count to the buffers when inspecting them on
1503 * t_syncdata_list or t_locked_list.
1505 * jbd2_journal_try_to_free_buffers() will call this function to
1506 * wait for the current transaction to finish syncing data buffers, before
1507 * try to free that buffer.
1509 * Called with journal->j_state_lock hold.
1511 static void jbd2_journal_wait_for_transaction_sync_data(journal_t *journal)
1513 transaction_t *transaction;
1516 spin_lock(&journal->j_state_lock);
1517 transaction = journal->j_committing_transaction;
1520 spin_unlock(&journal->j_state_lock);
1524 tid = transaction->t_tid;
1525 spin_unlock(&journal->j_state_lock);
1526 jbd2_log_wait_commit(journal, tid);
1530 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1531 * @journal: journal for operation
1532 * @page: to try and free
1533 * @gfp_mask: we use the mask to detect how hard should we try to release
1534 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1535 * release the buffers.
1538 * For all the buffers on this page,
1539 * if they are fully written out ordered data, move them onto BUF_CLEAN
1540 * so try_to_free_buffers() can reap them.
1542 * This function returns non-zero if we wish try_to_free_buffers()
1543 * to be called. We do this if the page is releasable by try_to_free_buffers().
1544 * We also do it if the page has locked or dirty buffers and the caller wants
1545 * us to perform sync or async writeout.
1547 * This complicates JBD locking somewhat. We aren't protected by the
1548 * BKL here. We wish to remove the buffer from its committing or
1549 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1551 * This may *change* the value of transaction_t->t_datalist, so anyone
1552 * who looks at t_datalist needs to lock against this function.
1554 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1555 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1556 * will come out of the lock with the buffer dirty, which makes it
1557 * ineligible for release here.
1559 * Who else is affected by this? hmm... Really the only contender
1560 * is do_get_write_access() - it could be looking at the buffer while
1561 * journal_try_to_free_buffer() is changing its state. But that
1562 * cannot happen because we never reallocate freed data as metadata
1563 * while the data is part of a transaction. Yes?
1565 * Return 0 on failure, 1 on success
1567 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1568 struct page *page, gfp_t gfp_mask)
1570 struct buffer_head *head;
1571 struct buffer_head *bh;
1574 J_ASSERT(PageLocked(page));
1576 head = page_buffers(page);
1579 struct journal_head *jh;
1582 * We take our own ref against the journal_head here to avoid
1583 * having to add tons of locking around each instance of
1584 * jbd2_journal_remove_journal_head() and
1585 * jbd2_journal_put_journal_head().
1587 jh = jbd2_journal_grab_journal_head(bh);
1591 jbd_lock_bh_state(bh);
1592 __journal_try_to_free_buffer(journal, bh);
1593 jbd2_journal_put_journal_head(jh);
1594 jbd_unlock_bh_state(bh);
1597 } while ((bh = bh->b_this_page) != head);
1599 ret = try_to_free_buffers(page);
1602 * There are a number of places where jbd2_journal_try_to_free_buffers()
1603 * could race with jbd2_journal_commit_transaction(), the later still
1604 * holds the reference to the buffers to free while processing them.
1605 * try_to_free_buffers() failed to free those buffers. Some of the
1606 * caller of releasepage() request page buffers to be dropped, otherwise
1607 * treat the fail-to-free as errors (such as generic_file_direct_IO())
1609 * So, if the caller of try_to_release_page() wants the synchronous
1610 * behaviour(i.e make sure buffers are dropped upon return),
1611 * let's wait for the current transaction to finish flush of
1612 * dirty data buffers, then try to free those buffers again,
1613 * with the journal locked.
1615 if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
1616 jbd2_journal_wait_for_transaction_sync_data(journal);
1617 ret = try_to_free_buffers(page);
1625 * This buffer is no longer needed. If it is on an older transaction's
1626 * checkpoint list we need to record it on this transaction's forget list
1627 * to pin this buffer (and hence its checkpointing transaction) down until
1628 * this transaction commits. If the buffer isn't on a checkpoint list, we
1630 * Returns non-zero if JBD no longer has an interest in the buffer.
1632 * Called under j_list_lock.
1634 * Called under jbd_lock_bh_state(bh).
1636 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1639 struct buffer_head *bh = jh2bh(jh);
1641 __jbd2_journal_unfile_buffer(jh);
1643 if (jh->b_cp_transaction) {
1644 JBUFFER_TRACE(jh, "on running+cp transaction");
1645 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1646 clear_buffer_jbddirty(bh);
1649 JBUFFER_TRACE(jh, "on running transaction");
1650 jbd2_journal_remove_journal_head(bh);
1657 * jbd2_journal_invalidatepage
1659 * This code is tricky. It has a number of cases to deal with.
1661 * There are two invariants which this code relies on:
1663 * i_size must be updated on disk before we start calling invalidatepage on the
1666 * This is done in ext3 by defining an ext3_setattr method which
1667 * updates i_size before truncate gets going. By maintaining this
1668 * invariant, we can be sure that it is safe to throw away any buffers
1669 * attached to the current transaction: once the transaction commits,
1670 * we know that the data will not be needed.
1672 * Note however that we can *not* throw away data belonging to the
1673 * previous, committing transaction!
1675 * Any disk blocks which *are* part of the previous, committing
1676 * transaction (and which therefore cannot be discarded immediately) are
1677 * not going to be reused in the new running transaction
1679 * The bitmap committed_data images guarantee this: any block which is
1680 * allocated in one transaction and removed in the next will be marked
1681 * as in-use in the committed_data bitmap, so cannot be reused until
1682 * the next transaction to delete the block commits. This means that
1683 * leaving committing buffers dirty is quite safe: the disk blocks
1684 * cannot be reallocated to a different file and so buffer aliasing is
1688 * The above applies mainly to ordered data mode. In writeback mode we
1689 * don't make guarantees about the order in which data hits disk --- in
1690 * particular we don't guarantee that new dirty data is flushed before
1691 * transaction commit --- so it is always safe just to discard data
1692 * immediately in that mode. --sct
1696 * The journal_unmap_buffer helper function returns zero if the buffer
1697 * concerned remains pinned as an anonymous buffer belonging to an older
1700 * We're outside-transaction here. Either or both of j_running_transaction
1701 * and j_committing_transaction may be NULL.
1703 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1705 transaction_t *transaction;
1706 struct journal_head *jh;
1710 BUFFER_TRACE(bh, "entry");
1713 * It is safe to proceed here without the j_list_lock because the
1714 * buffers cannot be stolen by try_to_free_buffers as long as we are
1715 * holding the page lock. --sct
1718 if (!buffer_jbd(bh))
1719 goto zap_buffer_unlocked;
1721 /* OK, we have data buffer in journaled mode */
1722 spin_lock(&journal->j_state_lock);
1723 jbd_lock_bh_state(bh);
1724 spin_lock(&journal->j_list_lock);
1726 jh = jbd2_journal_grab_journal_head(bh);
1728 goto zap_buffer_no_jh;
1730 transaction = jh->b_transaction;
1731 if (transaction == NULL) {
1732 /* First case: not on any transaction. If it
1733 * has no checkpoint link, then we can zap it:
1734 * it's a writeback-mode buffer so we don't care
1735 * if it hits disk safely. */
1736 if (!jh->b_cp_transaction) {
1737 JBUFFER_TRACE(jh, "not on any transaction: zap");
1741 if (!buffer_dirty(bh)) {
1742 /* bdflush has written it. We can drop it now */
1746 /* OK, it must be in the journal but still not
1747 * written fully to disk: it's metadata or
1748 * journaled data... */
1750 if (journal->j_running_transaction) {
1751 /* ... and once the current transaction has
1752 * committed, the buffer won't be needed any
1754 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1755 ret = __dispose_buffer(jh,
1756 journal->j_running_transaction);
1757 jbd2_journal_put_journal_head(jh);
1758 spin_unlock(&journal->j_list_lock);
1759 jbd_unlock_bh_state(bh);
1760 spin_unlock(&journal->j_state_lock);
1763 /* There is no currently-running transaction. So the
1764 * orphan record which we wrote for this file must have
1765 * passed into commit. We must attach this buffer to
1766 * the committing transaction, if it exists. */
1767 if (journal->j_committing_transaction) {
1768 JBUFFER_TRACE(jh, "give to committing trans");
1769 ret = __dispose_buffer(jh,
1770 journal->j_committing_transaction);
1771 jbd2_journal_put_journal_head(jh);
1772 spin_unlock(&journal->j_list_lock);
1773 jbd_unlock_bh_state(bh);
1774 spin_unlock(&journal->j_state_lock);
1777 /* The orphan record's transaction has
1778 * committed. We can cleanse this buffer */
1779 clear_buffer_jbddirty(bh);
1783 } else if (transaction == journal->j_committing_transaction) {
1784 JBUFFER_TRACE(jh, "on committing transaction");
1786 * If it is committing, we simply cannot touch it. We
1787 * can remove it's next_transaction pointer from the
1788 * running transaction if that is set, but nothing
1790 set_buffer_freed(bh);
1791 if (jh->b_next_transaction) {
1792 J_ASSERT(jh->b_next_transaction ==
1793 journal->j_running_transaction);
1794 jh->b_next_transaction = NULL;
1796 jbd2_journal_put_journal_head(jh);
1797 spin_unlock(&journal->j_list_lock);
1798 jbd_unlock_bh_state(bh);
1799 spin_unlock(&journal->j_state_lock);
1802 /* Good, the buffer belongs to the running transaction.
1803 * We are writing our own transaction's data, not any
1804 * previous one's, so it is safe to throw it away
1805 * (remember that we expect the filesystem to have set
1806 * i_size already for this truncate so recovery will not
1807 * expose the disk blocks we are discarding here.) */
1808 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1809 JBUFFER_TRACE(jh, "on running transaction");
1810 may_free = __dispose_buffer(jh, transaction);
1814 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);
1819 zap_buffer_unlocked:
1820 clear_buffer_dirty(bh);
1821 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1822 clear_buffer_mapped(bh);
1823 clear_buffer_req(bh);
1824 clear_buffer_new(bh);
1830 * void jbd2_journal_invalidatepage()
1831 * @journal: journal to use for flush...
1832 * @page: page to flush
1833 * @offset: length of page to invalidate.
1835 * Reap page buffers containing data after offset in page.
1838 void jbd2_journal_invalidatepage(journal_t *journal,
1840 unsigned long offset)
1842 struct buffer_head *head, *bh, *next;
1843 unsigned int curr_off = 0;
1846 if (!PageLocked(page))
1848 if (!page_has_buffers(page))
1851 /* We will potentially be playing with lists other than just the
1852 * data lists (especially for journaled data mode), so be
1853 * cautious in our locking. */
1855 head = bh = page_buffers(page);
1857 unsigned int next_off = curr_off + bh->b_size;
1858 next = bh->b_this_page;
1860 if (offset <= curr_off) {
1861 /* This block is wholly outside the truncation point */
1863 may_free &= journal_unmap_buffer(journal, bh);
1866 curr_off = next_off;
1869 } while (bh != head);
1872 if (may_free && try_to_free_buffers(page))
1873 J_ASSERT(!page_has_buffers(page));
1878 * File a buffer on the given transaction list.
1880 void __jbd2_journal_file_buffer(struct journal_head *jh,
1881 transaction_t *transaction, int jlist)
1883 struct journal_head **list = NULL;
1885 struct buffer_head *bh = jh2bh(jh);
1887 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1888 assert_spin_locked(&transaction->t_journal->j_list_lock);
1890 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1891 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1892 jh->b_transaction == NULL);
1894 if (jh->b_transaction && jh->b_jlist == jlist)
1897 /* The following list of buffer states needs to be consistent
1898 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
1901 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1902 jlist == BJ_Shadow || jlist == BJ_Forget) {
1903 if (test_clear_buffer_dirty(bh) ||
1904 test_clear_buffer_jbddirty(bh))
1908 if (jh->b_transaction)
1909 __jbd2_journal_temp_unlink_buffer(jh);
1910 jh->b_transaction = transaction;
1914 J_ASSERT_JH(jh, !jh->b_committed_data);
1915 J_ASSERT_JH(jh, !jh->b_frozen_data);
1918 transaction->t_nr_buffers++;
1919 list = &transaction->t_buffers;
1922 list = &transaction->t_forget;
1925 list = &transaction->t_iobuf_list;
1928 list = &transaction->t_shadow_list;
1931 list = &transaction->t_log_list;
1934 list = &transaction->t_reserved_list;
1938 __blist_add_buffer(list, jh);
1939 jh->b_jlist = jlist;
1942 set_buffer_jbddirty(bh);
1945 void jbd2_journal_file_buffer(struct journal_head *jh,
1946 transaction_t *transaction, int jlist)
1948 jbd_lock_bh_state(jh2bh(jh));
1949 spin_lock(&transaction->t_journal->j_list_lock);
1950 __jbd2_journal_file_buffer(jh, transaction, jlist);
1951 spin_unlock(&transaction->t_journal->j_list_lock);
1952 jbd_unlock_bh_state(jh2bh(jh));
1956 * Remove a buffer from its current buffer list in preparation for
1957 * dropping it from its current transaction entirely. If the buffer has
1958 * already started to be used by a subsequent transaction, refile the
1959 * buffer on that transaction's metadata list.
1961 * Called under journal->j_list_lock
1963 * Called under jbd_lock_bh_state(jh2bh(jh))
1965 void __jbd2_journal_refile_buffer(struct journal_head *jh)
1968 struct buffer_head *bh = jh2bh(jh);
1970 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1971 if (jh->b_transaction)
1972 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
1974 /* If the buffer is now unused, just drop it. */
1975 if (jh->b_next_transaction == NULL) {
1976 __jbd2_journal_unfile_buffer(jh);
1981 * It has been modified by a later transaction: add it to the new
1982 * transaction's metadata list.
1985 was_dirty = test_clear_buffer_jbddirty(bh);
1986 __jbd2_journal_temp_unlink_buffer(jh);
1987 jh->b_transaction = jh->b_next_transaction;
1988 jh->b_next_transaction = NULL;
1989 __jbd2_journal_file_buffer(jh, jh->b_transaction,
1990 jh->b_modified ? BJ_Metadata : BJ_Reserved);
1991 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
1994 set_buffer_jbddirty(bh);
1998 * For the unlocked version of this call, also make sure that any
1999 * hanging journal_head is cleaned up if necessary.
2001 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2002 * operation on a buffer_head, in which the caller is probably going to
2003 * be hooking the journal_head onto other lists. In that case it is up
2004 * to the caller to remove the journal_head if necessary. For the
2005 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2006 * doing anything else to the buffer so we need to do the cleanup
2007 * ourselves to avoid a jh leak.
2009 * *** The journal_head may be freed by this call! ***
2011 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2013 struct buffer_head *bh = jh2bh(jh);
2015 jbd_lock_bh_state(bh);
2016 spin_lock(&journal->j_list_lock);
2018 __jbd2_journal_refile_buffer(jh);
2019 jbd_unlock_bh_state(bh);
2020 jbd2_journal_remove_journal_head(bh);
2022 spin_unlock(&journal->j_list_lock);
2027 * File inode in the inode list of the handle's transaction
2029 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2031 transaction_t *transaction = handle->h_transaction;
2032 journal_t *journal = transaction->t_journal;
2034 if (is_handle_aborted(handle))
2037 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2038 transaction->t_tid);
2041 * First check whether inode isn't already on the transaction's
2042 * lists without taking the lock. Note that this check is safe
2043 * without the lock as we cannot race with somebody removing inode
2044 * from the transaction. The reason is that we remove inode from the
2045 * transaction only in journal_release_jbd_inode() and when we commit
2046 * the transaction. We are guarded from the first case by holding
2047 * a reference to the inode. We are safe against the second case
2048 * because if jinode->i_transaction == transaction, commit code
2049 * cannot touch the transaction because we hold reference to it,
2050 * and if jinode->i_next_transaction == transaction, commit code
2051 * will only file the inode where we want it.
2053 if (jinode->i_transaction == transaction ||
2054 jinode->i_next_transaction == transaction)
2057 spin_lock(&journal->j_list_lock);
2059 if (jinode->i_transaction == transaction ||
2060 jinode->i_next_transaction == transaction)
2063 /* On some different transaction's list - should be
2064 * the committing one */
2065 if (jinode->i_transaction) {
2066 J_ASSERT(jinode->i_next_transaction == NULL);
2067 J_ASSERT(jinode->i_transaction ==
2068 journal->j_committing_transaction);
2069 jinode->i_next_transaction = transaction;
2072 /* Not on any transaction list... */
2073 J_ASSERT(!jinode->i_next_transaction);
2074 jinode->i_transaction = transaction;
2075 list_add(&jinode->i_list, &transaction->t_inode_list);
2077 spin_unlock(&journal->j_list_lock);
2083 * This function must be called when inode is journaled in ordered mode
2084 * before truncation happens. It starts writeout of truncated part in
2085 * case it is in the committing transaction so that we stand to ordered
2086 * mode consistency guarantees.
2088 int jbd2_journal_begin_ordered_truncate(struct jbd2_inode *inode,
2092 transaction_t *commit_trans;
2095 if (!inode->i_transaction && !inode->i_next_transaction)
2097 journal = inode->i_transaction->t_journal;
2098 spin_lock(&journal->j_state_lock);
2099 commit_trans = journal->j_committing_transaction;
2100 spin_unlock(&journal->j_state_lock);
2101 if (inode->i_transaction == commit_trans) {
2102 ret = filemap_fdatawrite_range(inode->i_vfs_inode->i_mapping,
2103 new_size, LLONG_MAX);
2105 jbd2_journal_abort(journal, ret);