2 * linux/fs/jbd/transaction.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Generic filesystem transaction handling code; part of the ext2fs
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
20 #include <linux/time.h>
22 #include <linux/jbd.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
27 #include <linux/highmem.h>
29 static void __journal_temp_unlink_buffer(struct journal_head *jh);
32 * get_transaction: obtain a new transaction_t object.
34 * Simply allocate and initialise a new transaction. Create it in
35 * RUNNING state and add it to the current journal (which should not
36 * have an existing running transaction: we only make a new transaction
37 * once we have started to commit the old one).
40 * The journal MUST be locked. We don't perform atomic mallocs on the
41 * new transaction and we can't block without protecting against other
42 * processes trying to touch the journal while it is in transition.
44 * Called under j_state_lock
47 static transaction_t *
48 get_transaction(journal_t *journal, transaction_t *transaction)
50 transaction->t_journal = journal;
51 transaction->t_state = T_RUNNING;
52 transaction->t_tid = journal->j_transaction_sequence++;
53 transaction->t_expires = jiffies + journal->j_commit_interval;
54 spin_lock_init(&transaction->t_handle_lock);
56 /* Set up the commit timer for the new transaction. */
57 journal->j_commit_timer.expires = round_jiffies(transaction->t_expires);
58 add_timer(&journal->j_commit_timer);
60 J_ASSERT(journal->j_running_transaction == NULL);
61 journal->j_running_transaction = transaction;
69 * A handle_t is an object which represents a single atomic update to a
70 * filesystem, and which tracks all of the modifications which form part
75 * start_this_handle: Given a handle, deal with any locking or stalling
76 * needed to make sure that there is enough journal space for the handle
77 * to begin. Attach the handle to a transaction and set up the
78 * transaction's buffer credits.
81 static int start_this_handle(journal_t *journal, handle_t *handle)
83 transaction_t *transaction;
85 int nblocks = handle->h_buffer_credits;
86 transaction_t *new_transaction = NULL;
89 if (nblocks > journal->j_max_transaction_buffers) {
90 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
91 current->comm, nblocks,
92 journal->j_max_transaction_buffers);
98 if (!journal->j_running_transaction) {
99 new_transaction = jbd_kmalloc(sizeof(*new_transaction),
101 if (!new_transaction) {
105 memset(new_transaction, 0, sizeof(*new_transaction));
108 jbd_debug(3, "New handle %p going live.\n", handle);
113 * We need to hold j_state_lock until t_updates has been incremented,
114 * for proper journal barrier handling
116 spin_lock(&journal->j_state_lock);
118 if (is_journal_aborted(journal) ||
119 (journal->j_errno != 0 && !(journal->j_flags & JFS_ACK_ERR))) {
120 spin_unlock(&journal->j_state_lock);
125 /* Wait on the journal's transaction barrier if necessary */
126 if (journal->j_barrier_count) {
127 spin_unlock(&journal->j_state_lock);
128 wait_event(journal->j_wait_transaction_locked,
129 journal->j_barrier_count == 0);
133 if (!journal->j_running_transaction) {
134 if (!new_transaction) {
135 spin_unlock(&journal->j_state_lock);
136 goto alloc_transaction;
138 get_transaction(journal, new_transaction);
139 new_transaction = NULL;
142 transaction = journal->j_running_transaction;
145 * If the current transaction is locked down for commit, wait for the
146 * lock to be released.
148 if (transaction->t_state == T_LOCKED) {
151 prepare_to_wait(&journal->j_wait_transaction_locked,
152 &wait, TASK_UNINTERRUPTIBLE);
153 spin_unlock(&journal->j_state_lock);
155 finish_wait(&journal->j_wait_transaction_locked, &wait);
160 * If there is not enough space left in the log to write all potential
161 * buffers requested by this operation, we need to stall pending a log
162 * checkpoint to free some more log space.
164 spin_lock(&transaction->t_handle_lock);
165 needed = transaction->t_outstanding_credits + nblocks;
167 if (needed > journal->j_max_transaction_buffers) {
169 * If the current transaction is already too large, then start
170 * to commit it: we can then go back and attach this handle to
175 jbd_debug(2, "Handle %p starting new commit...\n", handle);
176 spin_unlock(&transaction->t_handle_lock);
177 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
178 TASK_UNINTERRUPTIBLE);
179 __log_start_commit(journal, transaction->t_tid);
180 spin_unlock(&journal->j_state_lock);
182 finish_wait(&journal->j_wait_transaction_locked, &wait);
187 * The commit code assumes that it can get enough log space
188 * without forcing a checkpoint. This is *critical* for
189 * correctness: a checkpoint of a buffer which is also
190 * associated with a committing transaction creates a deadlock,
191 * so commit simply cannot force through checkpoints.
193 * We must therefore ensure the necessary space in the journal
194 * *before* starting to dirty potentially checkpointed buffers
195 * in the new transaction.
197 * The worst part is, any transaction currently committing can
198 * reduce the free space arbitrarily. Be careful to account for
199 * those buffers when checkpointing.
203 * @@@ AKPM: This seems rather over-defensive. We're giving commit
204 * a _lot_ of headroom: 1/4 of the journal plus the size of
205 * the committing transaction. Really, we only need to give it
206 * committing_transaction->t_outstanding_credits plus "enough" for
207 * the log control blocks.
208 * Also, this test is inconsitent with the matching one in
211 if (__log_space_left(journal) < jbd_space_needed(journal)) {
212 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
213 spin_unlock(&transaction->t_handle_lock);
214 __log_wait_for_space(journal);
218 /* OK, account for the buffers that this operation expects to
219 * use and add the handle to the running transaction. */
221 handle->h_transaction = transaction;
222 transaction->t_outstanding_credits += nblocks;
223 transaction->t_updates++;
224 transaction->t_handle_count++;
225 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
226 handle, nblocks, transaction->t_outstanding_credits,
227 __log_space_left(journal));
228 spin_unlock(&transaction->t_handle_lock);
229 spin_unlock(&journal->j_state_lock);
231 if (unlikely(new_transaction)) /* It's usually NULL */
232 kfree(new_transaction);
236 static struct lock_class_key jbd_handle_key;
238 /* Allocate a new handle. This should probably be in a slab... */
239 static handle_t *new_handle(int nblocks)
241 handle_t *handle = jbd_alloc_handle(GFP_NOFS);
244 memset(handle, 0, sizeof(*handle));
245 handle->h_buffer_credits = nblocks;
248 lockdep_init_map(&handle->h_lockdep_map, "jbd_handle", &jbd_handle_key, 0);
254 * handle_t *journal_start() - Obtain a new handle.
255 * @journal: Journal to start transaction on.
256 * @nblocks: number of block buffer we might modify
258 * We make sure that the transaction can guarantee at least nblocks of
259 * modified buffers in the log. We block until the log can guarantee
262 * This function is visible to journal users (like ext3fs), so is not
263 * called with the journal already locked.
265 * Return a pointer to a newly allocated handle, or NULL on failure
267 handle_t *journal_start(journal_t *journal, int nblocks)
269 handle_t *handle = journal_current_handle();
273 return ERR_PTR(-EROFS);
276 J_ASSERT(handle->h_transaction->t_journal == journal);
281 handle = new_handle(nblocks);
283 return ERR_PTR(-ENOMEM);
285 current->journal_info = handle;
287 err = start_this_handle(journal, handle);
289 jbd_free_handle(handle);
290 current->journal_info = NULL;
291 handle = ERR_PTR(err);
294 lock_acquire(&handle->h_lockdep_map, 0, 0, 0, 2, _THIS_IP_);
300 * int journal_extend() - extend buffer credits.
301 * @handle: handle to 'extend'
302 * @nblocks: nr blocks to try to extend by.
304 * Some transactions, such as large extends and truncates, can be done
305 * atomically all at once or in several stages. The operation requests
306 * a credit for a number of buffer modications in advance, but can
307 * extend its credit if it needs more.
309 * journal_extend tries to give the running handle more buffer credits.
310 * It does not guarantee that allocation - this is a best-effort only.
311 * The calling process MUST be able to deal cleanly with a failure to
314 * Return 0 on success, non-zero on failure.
316 * return code < 0 implies an error
317 * return code > 0 implies normal transaction-full status.
319 int journal_extend(handle_t *handle, int nblocks)
321 transaction_t *transaction = handle->h_transaction;
322 journal_t *journal = transaction->t_journal;
327 if (is_handle_aborted(handle))
332 spin_lock(&journal->j_state_lock);
334 /* Don't extend a locked-down transaction! */
335 if (handle->h_transaction->t_state != T_RUNNING) {
336 jbd_debug(3, "denied handle %p %d blocks: "
337 "transaction not running\n", handle, nblocks);
341 spin_lock(&transaction->t_handle_lock);
342 wanted = transaction->t_outstanding_credits + nblocks;
344 if (wanted > journal->j_max_transaction_buffers) {
345 jbd_debug(3, "denied handle %p %d blocks: "
346 "transaction too large\n", handle, nblocks);
350 if (wanted > __log_space_left(journal)) {
351 jbd_debug(3, "denied handle %p %d blocks: "
352 "insufficient log space\n", handle, nblocks);
356 handle->h_buffer_credits += nblocks;
357 transaction->t_outstanding_credits += nblocks;
360 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
362 spin_unlock(&transaction->t_handle_lock);
364 spin_unlock(&journal->j_state_lock);
371 * int journal_restart() - restart a handle .
372 * @handle: handle to restart
373 * @nblocks: nr credits requested
375 * Restart a handle for a multi-transaction filesystem
378 * If the journal_extend() call above fails to grant new buffer credits
379 * to a running handle, a call to journal_restart will commit the
380 * handle's transaction so far and reattach the handle to a new
381 * transaction capabable of guaranteeing the requested number of
385 int journal_restart(handle_t *handle, int nblocks)
387 transaction_t *transaction = handle->h_transaction;
388 journal_t *journal = transaction->t_journal;
391 /* If we've had an abort of any type, don't even think about
392 * actually doing the restart! */
393 if (is_handle_aborted(handle))
397 * First unlink the handle from its current transaction, and start the
400 J_ASSERT(transaction->t_updates > 0);
401 J_ASSERT(journal_current_handle() == handle);
403 spin_lock(&journal->j_state_lock);
404 spin_lock(&transaction->t_handle_lock);
405 transaction->t_outstanding_credits -= handle->h_buffer_credits;
406 transaction->t_updates--;
408 if (!transaction->t_updates)
409 wake_up(&journal->j_wait_updates);
410 spin_unlock(&transaction->t_handle_lock);
412 jbd_debug(2, "restarting handle %p\n", handle);
413 __log_start_commit(journal, transaction->t_tid);
414 spin_unlock(&journal->j_state_lock);
416 handle->h_buffer_credits = nblocks;
417 ret = start_this_handle(journal, handle);
423 * void journal_lock_updates () - establish a transaction barrier.
424 * @journal: Journal to establish a barrier on.
426 * This locks out any further updates from being started, and blocks
427 * until all existing updates have completed, returning only once the
428 * journal is in a quiescent state with no updates running.
430 * The journal lock should not be held on entry.
432 void journal_lock_updates(journal_t *journal)
436 spin_lock(&journal->j_state_lock);
437 ++journal->j_barrier_count;
439 /* Wait until there are no running updates */
441 transaction_t *transaction = journal->j_running_transaction;
446 spin_lock(&transaction->t_handle_lock);
447 if (!transaction->t_updates) {
448 spin_unlock(&transaction->t_handle_lock);
451 prepare_to_wait(&journal->j_wait_updates, &wait,
452 TASK_UNINTERRUPTIBLE);
453 spin_unlock(&transaction->t_handle_lock);
454 spin_unlock(&journal->j_state_lock);
456 finish_wait(&journal->j_wait_updates, &wait);
457 spin_lock(&journal->j_state_lock);
459 spin_unlock(&journal->j_state_lock);
462 * We have now established a barrier against other normal updates, but
463 * we also need to barrier against other journal_lock_updates() calls
464 * to make sure that we serialise special journal-locked operations
467 mutex_lock(&journal->j_barrier);
471 * void journal_unlock_updates (journal_t* journal) - release barrier
472 * @journal: Journal to release the barrier on.
474 * Release a transaction barrier obtained with journal_lock_updates().
476 * Should be called without the journal lock held.
478 void journal_unlock_updates (journal_t *journal)
480 J_ASSERT(journal->j_barrier_count != 0);
482 mutex_unlock(&journal->j_barrier);
483 spin_lock(&journal->j_state_lock);
484 --journal->j_barrier_count;
485 spin_unlock(&journal->j_state_lock);
486 wake_up(&journal->j_wait_transaction_locked);
490 * Report any unexpected dirty buffers which turn up. Normally those
491 * indicate an error, but they can occur if the user is running (say)
492 * tune2fs to modify the live filesystem, so we need the option of
493 * continuing as gracefully as possible. #
495 * The caller should already hold the journal lock and
496 * j_list_lock spinlock: most callers will need those anyway
497 * in order to probe the buffer's journaling state safely.
499 static void jbd_unexpected_dirty_buffer(struct journal_head *jh)
503 /* If this buffer is one which might reasonably be dirty
504 * --- ie. data, or not part of this journal --- then
505 * we're OK to leave it alone, but otherwise we need to
506 * move the dirty bit to the journal's own internal
510 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
511 jlist == BJ_Shadow || jlist == BJ_Forget) {
512 struct buffer_head *bh = jh2bh(jh);
514 if (test_clear_buffer_dirty(bh))
515 set_buffer_jbddirty(bh);
520 * If the buffer is already part of the current transaction, then there
521 * is nothing we need to do. If it is already part of a prior
522 * transaction which we are still committing to disk, then we need to
523 * make sure that we do not overwrite the old copy: we do copy-out to
524 * preserve the copy going to disk. We also account the buffer against
525 * the handle's metadata buffer credits (unless the buffer is already
526 * part of the transaction, that is).
530 do_get_write_access(handle_t *handle, struct journal_head *jh,
533 struct buffer_head *bh;
534 transaction_t *transaction;
537 char *frozen_buffer = NULL;
540 if (is_handle_aborted(handle))
543 transaction = handle->h_transaction;
544 journal = transaction->t_journal;
546 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
548 JBUFFER_TRACE(jh, "entry");
552 /* @@@ Need to check for errors here at some point. */
555 jbd_lock_bh_state(bh);
557 /* We now hold the buffer lock so it is safe to query the buffer
558 * state. Is the buffer dirty?
560 * If so, there are two possibilities. The buffer may be
561 * non-journaled, and undergoing a quite legitimate writeback.
562 * Otherwise, it is journaled, and we don't expect dirty buffers
563 * in that state (the buffers should be marked JBD_Dirty
564 * instead.) So either the IO is being done under our own
565 * control and this is a bug, or it's a third party IO such as
566 * dump(8) (which may leave the buffer scheduled for read ---
567 * ie. locked but not dirty) or tune2fs (which may actually have
568 * the buffer dirtied, ugh.) */
570 if (buffer_dirty(bh)) {
572 * First question: is this buffer already part of the current
573 * transaction or the existing committing transaction?
575 if (jh->b_transaction) {
577 jh->b_transaction == transaction ||
579 journal->j_committing_transaction);
580 if (jh->b_next_transaction)
581 J_ASSERT_JH(jh, jh->b_next_transaction ==
585 * In any case we need to clean the dirty flag and we must
586 * do it under the buffer lock to be sure we don't race
587 * with running write-out.
589 JBUFFER_TRACE(jh, "Unexpected dirty buffer");
590 jbd_unexpected_dirty_buffer(jh);
596 if (is_handle_aborted(handle)) {
597 jbd_unlock_bh_state(bh);
603 * The buffer is already part of this transaction if b_transaction or
604 * b_next_transaction points to it
606 if (jh->b_transaction == transaction ||
607 jh->b_next_transaction == transaction)
611 * If there is already a copy-out version of this buffer, then we don't
612 * need to make another one
614 if (jh->b_frozen_data) {
615 JBUFFER_TRACE(jh, "has frozen data");
616 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
617 jh->b_next_transaction = transaction;
621 /* Is there data here we need to preserve? */
623 if (jh->b_transaction && jh->b_transaction != transaction) {
624 JBUFFER_TRACE(jh, "owned by older transaction");
625 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
626 J_ASSERT_JH(jh, jh->b_transaction ==
627 journal->j_committing_transaction);
629 /* There is one case we have to be very careful about.
630 * If the committing transaction is currently writing
631 * this buffer out to disk and has NOT made a copy-out,
632 * then we cannot modify the buffer contents at all
633 * right now. The essence of copy-out is that it is the
634 * extra copy, not the primary copy, which gets
635 * journaled. If the primary copy is already going to
636 * disk then we cannot do copy-out here. */
638 if (jh->b_jlist == BJ_Shadow) {
639 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
640 wait_queue_head_t *wqh;
642 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
644 JBUFFER_TRACE(jh, "on shadow: sleep");
645 jbd_unlock_bh_state(bh);
646 /* commit wakes up all shadow buffers after IO */
648 prepare_to_wait(wqh, &wait.wait,
649 TASK_UNINTERRUPTIBLE);
650 if (jh->b_jlist != BJ_Shadow)
654 finish_wait(wqh, &wait.wait);
658 /* Only do the copy if the currently-owning transaction
659 * still needs it. If it is on the Forget list, the
660 * committing transaction is past that stage. The
661 * buffer had better remain locked during the kmalloc,
662 * but that should be true --- we hold the journal lock
663 * still and the buffer is already on the BUF_JOURNAL
664 * list so won't be flushed.
666 * Subtle point, though: if this is a get_undo_access,
667 * then we will be relying on the frozen_data to contain
668 * the new value of the committed_data record after the
669 * transaction, so we HAVE to force the frozen_data copy
672 if (jh->b_jlist != BJ_Forget || force_copy) {
673 JBUFFER_TRACE(jh, "generate frozen data");
674 if (!frozen_buffer) {
675 JBUFFER_TRACE(jh, "allocate memory for buffer");
676 jbd_unlock_bh_state(bh);
678 jbd_slab_alloc(jh2bh(jh)->b_size,
680 if (!frozen_buffer) {
682 "%s: OOM for frozen_buffer\n",
684 JBUFFER_TRACE(jh, "oom!");
686 jbd_lock_bh_state(bh);
691 jh->b_frozen_data = frozen_buffer;
692 frozen_buffer = NULL;
695 jh->b_next_transaction = transaction;
700 * Finally, if the buffer is not journaled right now, we need to make
701 * sure it doesn't get written to disk before the caller actually
702 * commits the new data
704 if (!jh->b_transaction) {
705 JBUFFER_TRACE(jh, "no transaction");
706 J_ASSERT_JH(jh, !jh->b_next_transaction);
707 jh->b_transaction = transaction;
708 JBUFFER_TRACE(jh, "file as BJ_Reserved");
709 spin_lock(&journal->j_list_lock);
710 __journal_file_buffer(jh, transaction, BJ_Reserved);
711 spin_unlock(&journal->j_list_lock);
720 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
721 "Possible IO failure.\n");
722 page = jh2bh(jh)->b_page;
723 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
724 source = kmap_atomic(page, KM_USER0);
725 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
726 kunmap_atomic(source, KM_USER0);
728 jbd_unlock_bh_state(bh);
731 * If we are about to journal a buffer, then any revoke pending on it is
734 journal_cancel_revoke(handle, jh);
737 if (unlikely(frozen_buffer)) /* It's usually NULL */
738 jbd_slab_free(frozen_buffer, bh->b_size);
740 JBUFFER_TRACE(jh, "exit");
745 * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
746 * @handle: transaction to add buffer modifications to
747 * @bh: bh to be used for metadata writes
748 * @credits: variable that will receive credits for the buffer
750 * Returns an error code or 0 on success.
752 * In full data journalling mode the buffer may be of type BJ_AsyncData,
753 * because we're write()ing a buffer which is also part of a shared mapping.
756 int journal_get_write_access(handle_t *handle, struct buffer_head *bh)
758 struct journal_head *jh = journal_add_journal_head(bh);
761 /* We do not want to get caught playing with fields which the
762 * log thread also manipulates. Make sure that the buffer
763 * completes any outstanding IO before proceeding. */
764 rc = do_get_write_access(handle, jh, 0);
765 journal_put_journal_head(jh);
771 * When the user wants to journal a newly created buffer_head
772 * (ie. getblk() returned a new buffer and we are going to populate it
773 * manually rather than reading off disk), then we need to keep the
774 * buffer_head locked until it has been completely filled with new
775 * data. In this case, we should be able to make the assertion that
776 * the bh is not already part of an existing transaction.
778 * The buffer should already be locked by the caller by this point.
779 * There is no lock ranking violation: it was a newly created,
780 * unlocked buffer beforehand. */
783 * int journal_get_create_access () - notify intent to use newly created bh
784 * @handle: transaction to new buffer to
787 * Call this if you create a new bh.
789 int journal_get_create_access(handle_t *handle, struct buffer_head *bh)
791 transaction_t *transaction = handle->h_transaction;
792 journal_t *journal = transaction->t_journal;
793 struct journal_head *jh = journal_add_journal_head(bh);
796 jbd_debug(5, "journal_head %p\n", jh);
798 if (is_handle_aborted(handle))
802 JBUFFER_TRACE(jh, "entry");
804 * The buffer may already belong to this transaction due to pre-zeroing
805 * in the filesystem's new_block code. It may also be on the previous,
806 * committing transaction's lists, but it HAS to be in Forget state in
807 * that case: the transaction must have deleted the buffer for it to be
810 jbd_lock_bh_state(bh);
811 spin_lock(&journal->j_list_lock);
812 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
813 jh->b_transaction == NULL ||
814 (jh->b_transaction == journal->j_committing_transaction &&
815 jh->b_jlist == BJ_Forget)));
817 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
818 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
820 if (jh->b_transaction == NULL) {
821 jh->b_transaction = transaction;
822 JBUFFER_TRACE(jh, "file as BJ_Reserved");
823 __journal_file_buffer(jh, transaction, BJ_Reserved);
824 } else if (jh->b_transaction == journal->j_committing_transaction) {
825 JBUFFER_TRACE(jh, "set next transaction");
826 jh->b_next_transaction = transaction;
828 spin_unlock(&journal->j_list_lock);
829 jbd_unlock_bh_state(bh);
832 * akpm: I added this. ext3_alloc_branch can pick up new indirect
833 * blocks which contain freed but then revoked metadata. We need
834 * to cancel the revoke in case we end up freeing it yet again
835 * and the reallocating as data - this would cause a second revoke,
836 * which hits an assertion error.
838 JBUFFER_TRACE(jh, "cancelling revoke");
839 journal_cancel_revoke(handle, jh);
840 journal_put_journal_head(jh);
846 * int journal_get_undo_access() - Notify intent to modify metadata with
847 * non-rewindable consequences
848 * @handle: transaction
849 * @bh: buffer to undo
850 * @credits: store the number of taken credits here (if not NULL)
852 * Sometimes there is a need to distinguish between metadata which has
853 * been committed to disk and that which has not. The ext3fs code uses
854 * this for freeing and allocating space, we have to make sure that we
855 * do not reuse freed space until the deallocation has been committed,
856 * since if we overwrote that space we would make the delete
857 * un-rewindable in case of a crash.
859 * To deal with that, journal_get_undo_access requests write access to a
860 * buffer for parts of non-rewindable operations such as delete
861 * operations on the bitmaps. The journaling code must keep a copy of
862 * the buffer's contents prior to the undo_access call until such time
863 * as we know that the buffer has definitely been committed to disk.
865 * We never need to know which transaction the committed data is part
866 * of, buffers touched here are guaranteed to be dirtied later and so
867 * will be committed to a new transaction in due course, at which point
868 * we can discard the old committed data pointer.
870 * Returns error number or 0 on success.
872 int journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
875 struct journal_head *jh = journal_add_journal_head(bh);
876 char *committed_data = NULL;
878 JBUFFER_TRACE(jh, "entry");
881 * Do this first --- it can drop the journal lock, so we want to
882 * make sure that obtaining the committed_data is done
883 * atomically wrt. completion of any outstanding commits.
885 err = do_get_write_access(handle, jh, 1);
890 if (!jh->b_committed_data) {
891 committed_data = jbd_slab_alloc(jh2bh(jh)->b_size, GFP_NOFS);
892 if (!committed_data) {
893 printk(KERN_EMERG "%s: No memory for committed data\n",
900 jbd_lock_bh_state(bh);
901 if (!jh->b_committed_data) {
902 /* Copy out the current buffer contents into the
903 * preserved, committed copy. */
904 JBUFFER_TRACE(jh, "generate b_committed data");
905 if (!committed_data) {
906 jbd_unlock_bh_state(bh);
910 jh->b_committed_data = committed_data;
911 committed_data = NULL;
912 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
914 jbd_unlock_bh_state(bh);
916 journal_put_journal_head(jh);
917 if (unlikely(committed_data))
918 jbd_slab_free(committed_data, bh->b_size);
923 * int journal_dirty_data() - mark a buffer as containing dirty data which
924 * needs to be flushed before we can commit the
925 * current transaction.
926 * @handle: transaction
927 * @bh: bufferhead to mark
929 * The buffer is placed on the transaction's data list and is marked as
930 * belonging to the transaction.
932 * Returns error number or 0 on success.
934 * journal_dirty_data() can be called via page_launder->ext3_writepage
937 int journal_dirty_data(handle_t *handle, struct buffer_head *bh)
939 journal_t *journal = handle->h_transaction->t_journal;
941 struct journal_head *jh;
943 if (is_handle_aborted(handle))
946 jh = journal_add_journal_head(bh);
947 JBUFFER_TRACE(jh, "entry");
950 * The buffer could *already* be dirty. Writeout can start
953 jbd_debug(4, "jh: %p, tid:%d\n", jh, handle->h_transaction->t_tid);
956 * What if the buffer is already part of a running transaction?
958 * There are two cases:
959 * 1) It is part of the current running transaction. Refile it,
960 * just in case we have allocated it as metadata, deallocated
961 * it, then reallocated it as data.
962 * 2) It is part of the previous, still-committing transaction.
963 * If all we want to do is to guarantee that the buffer will be
964 * written to disk before this new transaction commits, then
965 * being sure that the *previous* transaction has this same
966 * property is sufficient for us! Just leave it on its old
969 * In case (2), the buffer must not already exist as metadata
970 * --- that would violate write ordering (a transaction is free
971 * to write its data at any point, even before the previous
972 * committing transaction has committed). The caller must
973 * never, ever allow this to happen: there's nothing we can do
974 * about it in this layer.
976 jbd_lock_bh_state(bh);
977 spin_lock(&journal->j_list_lock);
979 /* Now that we have bh_state locked, are we really still mapped? */
980 if (!buffer_mapped(bh)) {
981 JBUFFER_TRACE(jh, "unmapped buffer, bailing out");
985 if (jh->b_transaction) {
986 JBUFFER_TRACE(jh, "has transaction");
987 if (jh->b_transaction != handle->h_transaction) {
988 JBUFFER_TRACE(jh, "belongs to older transaction");
989 J_ASSERT_JH(jh, jh->b_transaction ==
990 journal->j_committing_transaction);
992 /* @@@ IS THIS TRUE ? */
994 * Not any more. Scenario: someone does a write()
995 * in data=journal mode. The buffer's transaction has
996 * moved into commit. Then someone does another
997 * write() to the file. We do the frozen data copyout
998 * and set b_next_transaction to point to j_running_t.
999 * And while we're in that state, someone does a
1000 * writepage() in an attempt to pageout the same area
1001 * of the file via a shared mapping. At present that
1002 * calls journal_dirty_data(), and we get right here.
1003 * It may be too late to journal the data. Simply
1004 * falling through to the next test will suffice: the
1005 * data will be dirty and wil be checkpointed. The
1006 * ordering comments in the next comment block still
1009 //J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1012 * If we're journalling data, and this buffer was
1013 * subject to a write(), it could be metadata, forget
1014 * or shadow against the committing transaction. Now,
1015 * someone has dirtied the same darn page via a mapping
1016 * and it is being writepage()'d.
1017 * We *could* just steal the page from commit, with some
1018 * fancy locking there. Instead, we just skip it -
1019 * don't tie the page's buffers to the new transaction
1021 * Implication: if we crash before the writepage() data
1022 * is written into the filesystem, recovery will replay
1025 if (jh->b_jlist != BJ_None &&
1026 jh->b_jlist != BJ_SyncData &&
1027 jh->b_jlist != BJ_Locked) {
1028 JBUFFER_TRACE(jh, "Not stealing");
1033 * This buffer may be undergoing writeout in commit. We
1034 * can't return from here and let the caller dirty it
1035 * again because that can cause the write-out loop in
1036 * commit to never terminate.
1038 if (buffer_dirty(bh)) {
1040 spin_unlock(&journal->j_list_lock);
1041 jbd_unlock_bh_state(bh);
1043 sync_dirty_buffer(bh);
1044 jbd_lock_bh_state(bh);
1045 spin_lock(&journal->j_list_lock);
1046 /* Since we dropped the lock... */
1047 if (!buffer_mapped(bh)) {
1048 JBUFFER_TRACE(jh, "buffer got unmapped");
1051 /* The buffer may become locked again at any
1052 time if it is redirtied */
1055 /* journal_clean_data_list() may have got there first */
1056 if (jh->b_transaction != NULL) {
1057 JBUFFER_TRACE(jh, "unfile from commit");
1058 __journal_temp_unlink_buffer(jh);
1059 /* It still points to the committing
1060 * transaction; move it to this one so
1061 * that the refile assert checks are
1063 jh->b_transaction = handle->h_transaction;
1065 /* The buffer will be refiled below */
1069 * Special case --- the buffer might actually have been
1070 * allocated and then immediately deallocated in the previous,
1071 * committing transaction, so might still be left on that
1072 * transaction's metadata lists.
1074 if (jh->b_jlist != BJ_SyncData && jh->b_jlist != BJ_Locked) {
1075 JBUFFER_TRACE(jh, "not on correct data list: unfile");
1076 J_ASSERT_JH(jh, jh->b_jlist != BJ_Shadow);
1077 __journal_temp_unlink_buffer(jh);
1078 jh->b_transaction = handle->h_transaction;
1079 JBUFFER_TRACE(jh, "file as data");
1080 __journal_file_buffer(jh, handle->h_transaction,
1084 JBUFFER_TRACE(jh, "not on a transaction");
1085 __journal_file_buffer(jh, handle->h_transaction, BJ_SyncData);
1088 spin_unlock(&journal->j_list_lock);
1089 jbd_unlock_bh_state(bh);
1091 BUFFER_TRACE(bh, "brelse");
1094 JBUFFER_TRACE(jh, "exit");
1095 journal_put_journal_head(jh);
1100 * int journal_dirty_metadata() - mark a buffer as containing dirty metadata
1101 * @handle: transaction to add buffer to.
1102 * @bh: buffer to mark
1104 * mark dirty metadata which needs to be journaled as part of the current
1107 * The buffer is placed on the transaction's metadata list and is marked
1108 * as belonging to the transaction.
1110 * Returns error number or 0 on success.
1112 * Special care needs to be taken if the buffer already belongs to the
1113 * current committing transaction (in which case we should have frozen
1114 * data present for that commit). In that case, we don't relink the
1115 * buffer: that only gets done when the old transaction finally
1116 * completes its commit.
1118 int journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1120 transaction_t *transaction = handle->h_transaction;
1121 journal_t *journal = transaction->t_journal;
1122 struct journal_head *jh = bh2jh(bh);
1124 jbd_debug(5, "journal_head %p\n", jh);
1125 JBUFFER_TRACE(jh, "entry");
1126 if (is_handle_aborted(handle))
1129 jbd_lock_bh_state(bh);
1131 if (jh->b_modified == 0) {
1133 * This buffer's got modified and becoming part
1134 * of the transaction. This needs to be done
1135 * once a transaction -bzzz
1138 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1139 handle->h_buffer_credits--;
1143 * fastpath, to avoid expensive locking. If this buffer is already
1144 * on the running transaction's metadata list there is nothing to do.
1145 * Nobody can take it off again because there is a handle open.
1146 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1147 * result in this test being false, so we go in and take the locks.
1149 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1150 JBUFFER_TRACE(jh, "fastpath");
1151 J_ASSERT_JH(jh, jh->b_transaction ==
1152 journal->j_running_transaction);
1156 set_buffer_jbddirty(bh);
1159 * Metadata already on the current transaction list doesn't
1160 * need to be filed. Metadata on another transaction's list must
1161 * be committing, and will be refiled once the commit completes:
1162 * leave it alone for now.
1164 if (jh->b_transaction != transaction) {
1165 JBUFFER_TRACE(jh, "already on other transaction");
1166 J_ASSERT_JH(jh, jh->b_transaction ==
1167 journal->j_committing_transaction);
1168 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1169 /* And this case is illegal: we can't reuse another
1170 * transaction's data buffer, ever. */
1174 /* That test should have eliminated the following case: */
1175 J_ASSERT_JH(jh, jh->b_frozen_data == 0);
1177 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1178 spin_lock(&journal->j_list_lock);
1179 __journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1180 spin_unlock(&journal->j_list_lock);
1182 jbd_unlock_bh_state(bh);
1184 JBUFFER_TRACE(jh, "exit");
1189 * journal_release_buffer: undo a get_write_access without any buffer
1190 * updates, if the update decided in the end that it didn't need access.
1194 journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1196 BUFFER_TRACE(bh, "entry");
1200 * void journal_forget() - bforget() for potentially-journaled buffers.
1201 * @handle: transaction handle
1202 * @bh: bh to 'forget'
1204 * We can only do the bforget if there are no commits pending against the
1205 * buffer. If the buffer is dirty in the current running transaction we
1206 * can safely unlink it.
1208 * bh may not be a journalled buffer at all - it may be a non-JBD
1209 * buffer which came off the hashtable. Check for this.
1211 * Decrements bh->b_count by one.
1213 * Allow this call even if the handle has aborted --- it may be part of
1214 * the caller's cleanup after an abort.
1216 int journal_forget (handle_t *handle, struct buffer_head *bh)
1218 transaction_t *transaction = handle->h_transaction;
1219 journal_t *journal = transaction->t_journal;
1220 struct journal_head *jh;
1221 int drop_reserve = 0;
1224 BUFFER_TRACE(bh, "entry");
1226 jbd_lock_bh_state(bh);
1227 spin_lock(&journal->j_list_lock);
1229 if (!buffer_jbd(bh))
1233 /* Critical error: attempting to delete a bitmap buffer, maybe?
1234 * Don't do any jbd operations, and return an error. */
1235 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1236 "inconsistent data on disk")) {
1242 * The buffer's going from the transaction, we must drop
1243 * all references -bzzz
1247 if (jh->b_transaction == handle->h_transaction) {
1248 J_ASSERT_JH(jh, !jh->b_frozen_data);
1250 /* If we are forgetting a buffer which is already part
1251 * of this transaction, then we can just drop it from
1252 * the transaction immediately. */
1253 clear_buffer_dirty(bh);
1254 clear_buffer_jbddirty(bh);
1256 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1261 * We are no longer going to journal this buffer.
1262 * However, the commit of this transaction is still
1263 * important to the buffer: the delete that we are now
1264 * processing might obsolete an old log entry, so by
1265 * committing, we can satisfy the buffer's checkpoint.
1267 * So, if we have a checkpoint on the buffer, we should
1268 * now refile the buffer on our BJ_Forget list so that
1269 * we know to remove the checkpoint after we commit.
1272 if (jh->b_cp_transaction) {
1273 __journal_temp_unlink_buffer(jh);
1274 __journal_file_buffer(jh, transaction, BJ_Forget);
1276 __journal_unfile_buffer(jh);
1277 journal_remove_journal_head(bh);
1279 if (!buffer_jbd(bh)) {
1280 spin_unlock(&journal->j_list_lock);
1281 jbd_unlock_bh_state(bh);
1286 } else if (jh->b_transaction) {
1287 J_ASSERT_JH(jh, (jh->b_transaction ==
1288 journal->j_committing_transaction));
1289 /* However, if the buffer is still owned by a prior
1290 * (committing) transaction, we can't drop it yet... */
1291 JBUFFER_TRACE(jh, "belongs to older transaction");
1292 /* ... but we CAN drop it from the new transaction if we
1293 * have also modified it since the original commit. */
1295 if (jh->b_next_transaction) {
1296 J_ASSERT(jh->b_next_transaction == transaction);
1297 jh->b_next_transaction = NULL;
1303 spin_unlock(&journal->j_list_lock);
1304 jbd_unlock_bh_state(bh);
1308 /* no need to reserve log space for this block -bzzz */
1309 handle->h_buffer_credits++;
1315 * int journal_stop() - complete a transaction
1316 * @handle: tranaction to complete.
1318 * All done for a particular handle.
1320 * There is not much action needed here. We just return any remaining
1321 * buffer credits to the transaction and remove the handle. The only
1322 * complication is that we need to start a commit operation if the
1323 * filesystem is marked for synchronous update.
1325 * journal_stop itself will not usually return an error, but it may
1326 * do so in unusual circumstances. In particular, expect it to
1327 * return -EIO if a journal_abort has been executed since the
1328 * transaction began.
1330 int journal_stop(handle_t *handle)
1332 transaction_t *transaction = handle->h_transaction;
1333 journal_t *journal = transaction->t_journal;
1334 int old_handle_count, err;
1337 J_ASSERT(journal_current_handle() == handle);
1339 if (is_handle_aborted(handle))
1342 J_ASSERT(transaction->t_updates > 0);
1346 if (--handle->h_ref > 0) {
1347 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1352 jbd_debug(4, "Handle %p going down\n", handle);
1355 * Implement synchronous transaction batching. If the handle
1356 * was synchronous, don't force a commit immediately. Let's
1357 * yield and let another thread piggyback onto this transaction.
1358 * Keep doing that while new threads continue to arrive.
1359 * It doesn't cost much - we're about to run a commit and sleep
1360 * on IO anyway. Speeds up many-threaded, many-dir operations
1363 * But don't do this if this process was the most recent one to
1364 * perform a synchronous write. We do this to detect the case where a
1365 * single process is doing a stream of sync writes. No point in waiting
1366 * for joiners in that case.
1369 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1370 journal->j_last_sync_writer = pid;
1372 old_handle_count = transaction->t_handle_count;
1373 schedule_timeout_uninterruptible(1);
1374 } while (old_handle_count != transaction->t_handle_count);
1377 current->journal_info = NULL;
1378 spin_lock(&journal->j_state_lock);
1379 spin_lock(&transaction->t_handle_lock);
1380 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1381 transaction->t_updates--;
1382 if (!transaction->t_updates) {
1383 wake_up(&journal->j_wait_updates);
1384 if (journal->j_barrier_count)
1385 wake_up(&journal->j_wait_transaction_locked);
1389 * If the handle is marked SYNC, we need to set another commit
1390 * going! We also want to force a commit if the current
1391 * transaction is occupying too much of the log, or if the
1392 * transaction is too old now.
1394 if (handle->h_sync ||
1395 transaction->t_outstanding_credits >
1396 journal->j_max_transaction_buffers ||
1397 time_after_eq(jiffies, transaction->t_expires)) {
1398 /* Do this even for aborted journals: an abort still
1399 * completes the commit thread, it just doesn't write
1400 * anything to disk. */
1401 tid_t tid = transaction->t_tid;
1403 spin_unlock(&transaction->t_handle_lock);
1404 jbd_debug(2, "transaction too old, requesting commit for "
1405 "handle %p\n", handle);
1406 /* This is non-blocking */
1407 __log_start_commit(journal, transaction->t_tid);
1408 spin_unlock(&journal->j_state_lock);
1411 * Special case: JFS_SYNC synchronous updates require us
1412 * to wait for the commit to complete.
1414 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1415 err = log_wait_commit(journal, tid);
1417 spin_unlock(&transaction->t_handle_lock);
1418 spin_unlock(&journal->j_state_lock);
1421 lock_release(&handle->h_lockdep_map, 1, _THIS_IP_);
1423 jbd_free_handle(handle);
1427 /**int journal_force_commit() - force any uncommitted transactions
1428 * @journal: journal to force
1430 * For synchronous operations: force any uncommitted transactions
1431 * to disk. May seem kludgy, but it reuses all the handle batching
1432 * code in a very simple manner.
1434 int journal_force_commit(journal_t *journal)
1439 handle = journal_start(journal, 1);
1440 if (IS_ERR(handle)) {
1441 ret = PTR_ERR(handle);
1444 ret = journal_stop(handle);
1451 * List management code snippets: various functions for manipulating the
1452 * transaction buffer lists.
1457 * Append a buffer to a transaction list, given the transaction's list head
1460 * j_list_lock is held.
1462 * jbd_lock_bh_state(jh2bh(jh)) is held.
1466 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1469 jh->b_tnext = jh->b_tprev = jh;
1472 /* Insert at the tail of the list to preserve order */
1473 struct journal_head *first = *list, *last = first->b_tprev;
1475 jh->b_tnext = first;
1476 last->b_tnext = first->b_tprev = jh;
1481 * Remove a buffer from a transaction list, given the transaction's list
1484 * Called with j_list_lock held, and the journal may not be locked.
1486 * jbd_lock_bh_state(jh2bh(jh)) is held.
1490 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1493 *list = jh->b_tnext;
1497 jh->b_tprev->b_tnext = jh->b_tnext;
1498 jh->b_tnext->b_tprev = jh->b_tprev;
1502 * Remove a buffer from the appropriate transaction list.
1504 * Note that this function can *change* the value of
1505 * bh->b_transaction->t_sync_datalist, t_buffers, t_forget,
1506 * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
1507 * is holding onto a copy of one of thee pointers, it could go bad.
1508 * Generally the caller needs to re-read the pointer from the transaction_t.
1510 * Called under j_list_lock. The journal may not be locked.
1512 static void __journal_temp_unlink_buffer(struct journal_head *jh)
1514 struct journal_head **list = NULL;
1515 transaction_t *transaction;
1516 struct buffer_head *bh = jh2bh(jh);
1518 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1519 transaction = jh->b_transaction;
1521 assert_spin_locked(&transaction->t_journal->j_list_lock);
1523 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1524 if (jh->b_jlist != BJ_None)
1525 J_ASSERT_JH(jh, transaction != 0);
1527 switch (jh->b_jlist) {
1531 list = &transaction->t_sync_datalist;
1534 transaction->t_nr_buffers--;
1535 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1536 list = &transaction->t_buffers;
1539 list = &transaction->t_forget;
1542 list = &transaction->t_iobuf_list;
1545 list = &transaction->t_shadow_list;
1548 list = &transaction->t_log_list;
1551 list = &transaction->t_reserved_list;
1554 list = &transaction->t_locked_list;
1558 __blist_del_buffer(list, jh);
1559 jh->b_jlist = BJ_None;
1560 if (test_clear_buffer_jbddirty(bh))
1561 mark_buffer_dirty(bh); /* Expose it to the VM */
1564 void __journal_unfile_buffer(struct journal_head *jh)
1566 __journal_temp_unlink_buffer(jh);
1567 jh->b_transaction = NULL;
1570 void journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1572 jbd_lock_bh_state(jh2bh(jh));
1573 spin_lock(&journal->j_list_lock);
1574 __journal_unfile_buffer(jh);
1575 spin_unlock(&journal->j_list_lock);
1576 jbd_unlock_bh_state(jh2bh(jh));
1580 * Called from journal_try_to_free_buffers().
1582 * Called under jbd_lock_bh_state(bh)
1585 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1587 struct journal_head *jh;
1591 if (buffer_locked(bh) || buffer_dirty(bh))
1594 if (jh->b_next_transaction != 0)
1597 spin_lock(&journal->j_list_lock);
1598 if (jh->b_transaction != 0 && jh->b_cp_transaction == 0) {
1599 if (jh->b_jlist == BJ_SyncData || jh->b_jlist == BJ_Locked) {
1600 /* A written-back ordered data buffer */
1601 JBUFFER_TRACE(jh, "release data");
1602 __journal_unfile_buffer(jh);
1603 journal_remove_journal_head(bh);
1606 } else if (jh->b_cp_transaction != 0 && jh->b_transaction == 0) {
1607 /* written-back checkpointed metadata buffer */
1608 if (jh->b_jlist == BJ_None) {
1609 JBUFFER_TRACE(jh, "remove from checkpoint list");
1610 __journal_remove_checkpoint(jh);
1611 journal_remove_journal_head(bh);
1615 spin_unlock(&journal->j_list_lock);
1622 * int journal_try_to_free_buffers() - try to free page buffers.
1623 * @journal: journal for operation
1624 * @page: to try and free
1625 * @unused_gfp_mask: unused
1628 * For all the buffers on this page,
1629 * if they are fully written out ordered data, move them onto BUF_CLEAN
1630 * so try_to_free_buffers() can reap them.
1632 * This function returns non-zero if we wish try_to_free_buffers()
1633 * to be called. We do this if the page is releasable by try_to_free_buffers().
1634 * We also do it if the page has locked or dirty buffers and the caller wants
1635 * us to perform sync or async writeout.
1637 * This complicates JBD locking somewhat. We aren't protected by the
1638 * BKL here. We wish to remove the buffer from its committing or
1639 * running transaction's ->t_datalist via __journal_unfile_buffer.
1641 * This may *change* the value of transaction_t->t_datalist, so anyone
1642 * who looks at t_datalist needs to lock against this function.
1644 * Even worse, someone may be doing a journal_dirty_data on this
1645 * buffer. So we need to lock against that. journal_dirty_data()
1646 * will come out of the lock with the buffer dirty, which makes it
1647 * ineligible for release here.
1649 * Who else is affected by this? hmm... Really the only contender
1650 * is do_get_write_access() - it could be looking at the buffer while
1651 * journal_try_to_free_buffer() is changing its state. But that
1652 * cannot happen because we never reallocate freed data as metadata
1653 * while the data is part of a transaction. Yes?
1655 int journal_try_to_free_buffers(journal_t *journal,
1656 struct page *page, gfp_t unused_gfp_mask)
1658 struct buffer_head *head;
1659 struct buffer_head *bh;
1662 J_ASSERT(PageLocked(page));
1664 head = page_buffers(page);
1667 struct journal_head *jh;
1670 * We take our own ref against the journal_head here to avoid
1671 * having to add tons of locking around each instance of
1672 * journal_remove_journal_head() and journal_put_journal_head().
1674 jh = journal_grab_journal_head(bh);
1678 jbd_lock_bh_state(bh);
1679 __journal_try_to_free_buffer(journal, bh);
1680 journal_put_journal_head(jh);
1681 jbd_unlock_bh_state(bh);
1684 } while ((bh = bh->b_this_page) != head);
1685 ret = try_to_free_buffers(page);
1691 * This buffer is no longer needed. If it is on an older transaction's
1692 * checkpoint list we need to record it on this transaction's forget list
1693 * to pin this buffer (and hence its checkpointing transaction) down until
1694 * this transaction commits. If the buffer isn't on a checkpoint list, we
1696 * Returns non-zero if JBD no longer has an interest in the buffer.
1698 * Called under j_list_lock.
1700 * Called under jbd_lock_bh_state(bh).
1702 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1705 struct buffer_head *bh = jh2bh(jh);
1707 __journal_unfile_buffer(jh);
1709 if (jh->b_cp_transaction) {
1710 JBUFFER_TRACE(jh, "on running+cp transaction");
1711 __journal_file_buffer(jh, transaction, BJ_Forget);
1712 clear_buffer_jbddirty(bh);
1715 JBUFFER_TRACE(jh, "on running transaction");
1716 journal_remove_journal_head(bh);
1723 * journal_invalidatepage
1725 * This code is tricky. It has a number of cases to deal with.
1727 * There are two invariants which this code relies on:
1729 * i_size must be updated on disk before we start calling invalidatepage on the
1732 * This is done in ext3 by defining an ext3_setattr method which
1733 * updates i_size before truncate gets going. By maintaining this
1734 * invariant, we can be sure that it is safe to throw away any buffers
1735 * attached to the current transaction: once the transaction commits,
1736 * we know that the data will not be needed.
1738 * Note however that we can *not* throw away data belonging to the
1739 * previous, committing transaction!
1741 * Any disk blocks which *are* part of the previous, committing
1742 * transaction (and which therefore cannot be discarded immediately) are
1743 * not going to be reused in the new running transaction
1745 * The bitmap committed_data images guarantee this: any block which is
1746 * allocated in one transaction and removed in the next will be marked
1747 * as in-use in the committed_data bitmap, so cannot be reused until
1748 * the next transaction to delete the block commits. This means that
1749 * leaving committing buffers dirty is quite safe: the disk blocks
1750 * cannot be reallocated to a different file and so buffer aliasing is
1754 * The above applies mainly to ordered data mode. In writeback mode we
1755 * don't make guarantees about the order in which data hits disk --- in
1756 * particular we don't guarantee that new dirty data is flushed before
1757 * transaction commit --- so it is always safe just to discard data
1758 * immediately in that mode. --sct
1762 * The journal_unmap_buffer helper function returns zero if the buffer
1763 * concerned remains pinned as an anonymous buffer belonging to an older
1766 * We're outside-transaction here. Either or both of j_running_transaction
1767 * and j_committing_transaction may be NULL.
1769 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1771 transaction_t *transaction;
1772 struct journal_head *jh;
1776 BUFFER_TRACE(bh, "entry");
1779 * It is safe to proceed here without the j_list_lock because the
1780 * buffers cannot be stolen by try_to_free_buffers as long as we are
1781 * holding the page lock. --sct
1784 if (!buffer_jbd(bh))
1785 goto zap_buffer_unlocked;
1787 spin_lock(&journal->j_state_lock);
1788 jbd_lock_bh_state(bh);
1789 spin_lock(&journal->j_list_lock);
1791 jh = journal_grab_journal_head(bh);
1793 goto zap_buffer_no_jh;
1795 transaction = jh->b_transaction;
1796 if (transaction == NULL) {
1797 /* First case: not on any transaction. If it
1798 * has no checkpoint link, then we can zap it:
1799 * it's a writeback-mode buffer so we don't care
1800 * if it hits disk safely. */
1801 if (!jh->b_cp_transaction) {
1802 JBUFFER_TRACE(jh, "not on any transaction: zap");
1806 if (!buffer_dirty(bh)) {
1807 /* bdflush has written it. We can drop it now */
1811 /* OK, it must be in the journal but still not
1812 * written fully to disk: it's metadata or
1813 * journaled data... */
1815 if (journal->j_running_transaction) {
1816 /* ... and once the current transaction has
1817 * committed, the buffer won't be needed any
1819 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1820 ret = __dispose_buffer(jh,
1821 journal->j_running_transaction);
1822 journal_put_journal_head(jh);
1823 spin_unlock(&journal->j_list_lock);
1824 jbd_unlock_bh_state(bh);
1825 spin_unlock(&journal->j_state_lock);
1828 /* There is no currently-running transaction. So the
1829 * orphan record which we wrote for this file must have
1830 * passed into commit. We must attach this buffer to
1831 * the committing transaction, if it exists. */
1832 if (journal->j_committing_transaction) {
1833 JBUFFER_TRACE(jh, "give to committing trans");
1834 ret = __dispose_buffer(jh,
1835 journal->j_committing_transaction);
1836 journal_put_journal_head(jh);
1837 spin_unlock(&journal->j_list_lock);
1838 jbd_unlock_bh_state(bh);
1839 spin_unlock(&journal->j_state_lock);
1842 /* The orphan record's transaction has
1843 * committed. We can cleanse this buffer */
1844 clear_buffer_jbddirty(bh);
1848 } else if (transaction == journal->j_committing_transaction) {
1849 JBUFFER_TRACE(jh, "on committing transaction");
1850 if (jh->b_jlist == BJ_Locked) {
1852 * The buffer is on the committing transaction's locked
1853 * list. We have the buffer locked, so I/O has
1854 * completed. So we can nail the buffer now.
1856 may_free = __dispose_buffer(jh, transaction);
1860 * If it is committing, we simply cannot touch it. We
1861 * can remove it's next_transaction pointer from the
1862 * running transaction if that is set, but nothing
1864 set_buffer_freed(bh);
1865 if (jh->b_next_transaction) {
1866 J_ASSERT(jh->b_next_transaction ==
1867 journal->j_running_transaction);
1868 jh->b_next_transaction = NULL;
1870 journal_put_journal_head(jh);
1871 spin_unlock(&journal->j_list_lock);
1872 jbd_unlock_bh_state(bh);
1873 spin_unlock(&journal->j_state_lock);
1876 /* Good, the buffer belongs to the running transaction.
1877 * We are writing our own transaction's data, not any
1878 * previous one's, so it is safe to throw it away
1879 * (remember that we expect the filesystem to have set
1880 * i_size already for this truncate so recovery will not
1881 * expose the disk blocks we are discarding here.) */
1882 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1883 JBUFFER_TRACE(jh, "on running transaction");
1884 may_free = __dispose_buffer(jh, transaction);
1888 journal_put_journal_head(jh);
1890 spin_unlock(&journal->j_list_lock);
1891 jbd_unlock_bh_state(bh);
1892 spin_unlock(&journal->j_state_lock);
1893 zap_buffer_unlocked:
1894 clear_buffer_dirty(bh);
1895 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1896 clear_buffer_mapped(bh);
1897 clear_buffer_req(bh);
1898 clear_buffer_new(bh);
1904 * void journal_invalidatepage()
1905 * @journal: journal to use for flush...
1906 * @page: page to flush
1907 * @offset: length of page to invalidate.
1909 * Reap page buffers containing data after offset in page.
1912 void journal_invalidatepage(journal_t *journal,
1914 unsigned long offset)
1916 struct buffer_head *head, *bh, *next;
1917 unsigned int curr_off = 0;
1920 if (!PageLocked(page))
1922 if (!page_has_buffers(page))
1925 /* We will potentially be playing with lists other than just the
1926 * data lists (especially for journaled data mode), so be
1927 * cautious in our locking. */
1929 head = bh = page_buffers(page);
1931 unsigned int next_off = curr_off + bh->b_size;
1932 next = bh->b_this_page;
1934 if (offset <= curr_off) {
1935 /* This block is wholly outside the truncation point */
1937 may_free &= journal_unmap_buffer(journal, bh);
1940 curr_off = next_off;
1943 } while (bh != head);
1946 if (may_free && try_to_free_buffers(page))
1947 J_ASSERT(!page_has_buffers(page));
1952 * File a buffer on the given transaction list.
1954 void __journal_file_buffer(struct journal_head *jh,
1955 transaction_t *transaction, int jlist)
1957 struct journal_head **list = NULL;
1959 struct buffer_head *bh = jh2bh(jh);
1961 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1962 assert_spin_locked(&transaction->t_journal->j_list_lock);
1964 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1965 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1966 jh->b_transaction == 0);
1968 if (jh->b_transaction && jh->b_jlist == jlist)
1971 /* The following list of buffer states needs to be consistent
1972 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
1975 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1976 jlist == BJ_Shadow || jlist == BJ_Forget) {
1977 if (test_clear_buffer_dirty(bh) ||
1978 test_clear_buffer_jbddirty(bh))
1982 if (jh->b_transaction)
1983 __journal_temp_unlink_buffer(jh);
1984 jh->b_transaction = transaction;
1988 J_ASSERT_JH(jh, !jh->b_committed_data);
1989 J_ASSERT_JH(jh, !jh->b_frozen_data);
1992 list = &transaction->t_sync_datalist;
1995 transaction->t_nr_buffers++;
1996 list = &transaction->t_buffers;
1999 list = &transaction->t_forget;
2002 list = &transaction->t_iobuf_list;
2005 list = &transaction->t_shadow_list;
2008 list = &transaction->t_log_list;
2011 list = &transaction->t_reserved_list;
2014 list = &transaction->t_locked_list;
2018 __blist_add_buffer(list, jh);
2019 jh->b_jlist = jlist;
2022 set_buffer_jbddirty(bh);
2025 void journal_file_buffer(struct journal_head *jh,
2026 transaction_t *transaction, int jlist)
2028 jbd_lock_bh_state(jh2bh(jh));
2029 spin_lock(&transaction->t_journal->j_list_lock);
2030 __journal_file_buffer(jh, transaction, jlist);
2031 spin_unlock(&transaction->t_journal->j_list_lock);
2032 jbd_unlock_bh_state(jh2bh(jh));
2036 * Remove a buffer from its current buffer list in preparation for
2037 * dropping it from its current transaction entirely. If the buffer has
2038 * already started to be used by a subsequent transaction, refile the
2039 * buffer on that transaction's metadata list.
2041 * Called under journal->j_list_lock
2043 * Called under jbd_lock_bh_state(jh2bh(jh))
2045 void __journal_refile_buffer(struct journal_head *jh)
2048 struct buffer_head *bh = jh2bh(jh);
2050 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2051 if (jh->b_transaction)
2052 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2054 /* If the buffer is now unused, just drop it. */
2055 if (jh->b_next_transaction == NULL) {
2056 __journal_unfile_buffer(jh);
2061 * It has been modified by a later transaction: add it to the new
2062 * transaction's metadata list.
2065 was_dirty = test_clear_buffer_jbddirty(bh);
2066 __journal_temp_unlink_buffer(jh);
2067 jh->b_transaction = jh->b_next_transaction;
2068 jh->b_next_transaction = NULL;
2069 __journal_file_buffer(jh, jh->b_transaction,
2070 was_dirty ? BJ_Metadata : BJ_Reserved);
2071 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2074 set_buffer_jbddirty(bh);
2078 * For the unlocked version of this call, also make sure that any
2079 * hanging journal_head is cleaned up if necessary.
2081 * __journal_refile_buffer is usually called as part of a single locked
2082 * operation on a buffer_head, in which the caller is probably going to
2083 * be hooking the journal_head onto other lists. In that case it is up
2084 * to the caller to remove the journal_head if necessary. For the
2085 * unlocked journal_refile_buffer call, the caller isn't going to be
2086 * doing anything else to the buffer so we need to do the cleanup
2087 * ourselves to avoid a jh leak.
2089 * *** The journal_head may be freed by this call! ***
2091 void journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2093 struct buffer_head *bh = jh2bh(jh);
2095 jbd_lock_bh_state(bh);
2096 spin_lock(&journal->j_list_lock);
2098 __journal_refile_buffer(jh);
2099 jbd_unlock_bh_state(bh);
2100 journal_remove_journal_head(bh);
2102 spin_unlock(&journal->j_list_lock);