xfrm: wrong hash value for temporary SA
[linux-2.6/mini2440.git] / fs / jbd2 / transaction.c
blob28ce21d8598e11f16182a973d31d7e8bef83c145
1 /*
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
13 * journaling system.
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
17 * filesystem).
20 #include <linux/time.h>
21 #include <linux/fs.h>
22 #include <linux/jbd2.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
26 #include <linux/mm.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).
40 * Preconditions:
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;
68 return transaction;
72 * Handle management.
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
76 * of that one update.
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;
89 int needed;
90 int nblocks = handle->h_buffer_credits;
91 transaction_t *new_transaction = NULL;
92 int ret = 0;
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);
99 ret = -ENOSPC;
100 goto out;
103 alloc_transaction:
104 if (!journal->j_running_transaction) {
105 new_transaction = kzalloc(sizeof(*new_transaction),
106 GFP_NOFS|__GFP_NOFAIL);
107 if (!new_transaction) {
108 ret = -ENOMEM;
109 goto out;
113 jbd_debug(3, "New handle %p going live.\n", handle);
115 repeat:
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);
122 repeat_locked:
123 if (is_journal_aborted(journal) ||
124 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
125 spin_unlock(&journal->j_state_lock);
126 ret = -EROFS;
127 goto out;
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);
135 goto repeat;
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) {
154 DEFINE_WAIT(wait);
156 prepare_to_wait(&journal->j_wait_transaction_locked,
157 &wait, TASK_UNINTERRUPTIBLE);
158 spin_unlock(&journal->j_state_lock);
159 schedule();
160 finish_wait(&journal->j_wait_transaction_locked, &wait);
161 goto repeat;
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
176 * a new transaction.
178 DEFINE_WAIT(wait);
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);
186 schedule();
187 finish_wait(&journal->j_wait_transaction_locked, &wait);
188 goto repeat;
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);
220 goto repeat_locked;
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);
241 out:
242 if (unlikely(new_transaction)) /* It's usually NULL */
243 kfree(new_transaction);
244 return ret;
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);
253 if (!handle)
254 return NULL;
255 memset(handle, 0, sizeof(*handle));
256 handle->h_buffer_credits = nblocks;
257 handle->h_ref = 1;
259 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
260 &jbd2_handle_key, 0);
262 return handle;
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
272 * that much space.
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();
282 int err;
284 if (!journal)
285 return ERR_PTR(-EROFS);
287 if (handle) {
288 J_ASSERT(handle->h_transaction->t_journal == journal);
289 handle->h_ref++;
290 return handle;
293 handle = new_handle(nblocks);
294 if (!handle)
295 return ERR_PTR(-ENOMEM);
297 current->journal_info = handle;
299 err = start_this_handle(journal, handle);
300 if (err < 0) {
301 jbd2_free_handle(handle);
302 current->journal_info = NULL;
303 handle = ERR_PTR(err);
304 goto out;
307 lock_map_acquire(&handle->h_lockdep_map);
308 out:
309 return handle;
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
325 * extend here.
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;
336 int result;
337 int wanted;
339 result = -EIO;
340 if (is_handle_aborted(handle))
341 goto out;
343 result = 1;
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);
351 goto error_out;
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);
360 goto unlock;
363 if (wanted > __jbd2_log_space_left(journal)) {
364 jbd_debug(3, "denied handle %p %d blocks: "
365 "insufficient log space\n", handle, nblocks);
366 goto unlock;
369 handle->h_buffer_credits += nblocks;
370 transaction->t_outstanding_credits += nblocks;
371 result = 0;
373 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
374 unlock:
375 spin_unlock(&transaction->t_handle_lock);
376 error_out:
377 spin_unlock(&journal->j_state_lock);
378 out:
379 return result;
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
389 * operation.
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
395 * credits.
398 int jbd2_journal_restart(handle_t *handle, int nblocks)
400 transaction_t *transaction = handle->h_transaction;
401 journal_t *journal = transaction->t_journal;
402 int ret;
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))
407 return 0;
410 * First unlink the handle from its current transaction, and start the
411 * commit on that.
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);
431 return ret;
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)
447 DEFINE_WAIT(wait);
449 spin_lock(&journal->j_state_lock);
450 ++journal->j_barrier_count;
452 /* Wait until there are no running updates */
453 while (1) {
454 transaction_t *transaction = journal->j_running_transaction;
456 if (!transaction)
457 break;
459 spin_lock(&transaction->t_handle_lock);
460 if (!transaction->t_updates) {
461 spin_unlock(&transaction->t_handle_lock);
462 break;
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);
468 schedule();
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
478 * too.
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)
514 int jlist;
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
520 * JBDDirty bit. */
521 jlist = jh->b_jlist;
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).
542 static int
543 do_get_write_access(handle_t *handle, struct journal_head *jh,
544 int force_copy)
546 struct buffer_head *bh;
547 transaction_t *transaction;
548 journal_t *journal;
549 int error;
550 char *frozen_buffer = NULL;
551 int need_copy = 0;
553 if (is_handle_aborted(handle))
554 return -EROFS;
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");
562 repeat:
563 bh = jh2bh(jh);
565 /* @@@ Need to check for errors here at some point. */
567 lock_buffer(bh);
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) {
589 J_ASSERT_JH(jh,
590 jh->b_transaction == transaction ||
591 jh->b_transaction ==
592 journal->j_committing_transaction);
593 if (jh->b_next_transaction)
594 J_ASSERT_JH(jh, jh->b_next_transaction ==
595 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);
606 unlock_buffer(bh);
608 error = -EROFS;
609 if (is_handle_aborted(handle)) {
610 jbd_unlock_bh_state(bh);
611 goto out;
613 error = 0;
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)
621 goto done;
624 * this is the first time this transaction is touching this buffer,
625 * reset the modified flag
627 jh->b_modified = 0;
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;
637 goto done;
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 */
666 for ( ; ; ) {
667 prepare_to_wait(wqh, &wait.wait,
668 TASK_UNINTERRUPTIBLE);
669 if (jh->b_jlist != BJ_Shadow)
670 break;
671 schedule();
673 finish_wait(wqh, &wait.wait);
674 goto repeat;
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
689 * in that case. */
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);
696 frozen_buffer =
697 jbd2_alloc(jh2bh(jh)->b_size,
698 GFP_NOFS);
699 if (!frozen_buffer) {
700 printk(KERN_EMERG
701 "%s: OOM for frozen_buffer\n",
702 __func__);
703 JBUFFER_TRACE(jh, "oom!");
704 error = -ENOMEM;
705 jbd_lock_bh_state(bh);
706 goto done;
708 goto repeat;
710 jh->b_frozen_data = frozen_buffer;
711 frozen_buffer = NULL;
712 need_copy = 1;
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);
733 done:
734 if (need_copy) {
735 struct page *page;
736 int offset;
737 char *source;
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);
748 * Now that the frozen data is saved off, we need to store
749 * any matching triggers.
751 jh->b_frozen_triggers = jh->b_triggers;
753 jbd_unlock_bh_state(bh);
756 * If we are about to journal a buffer, then any revoke pending on it is
757 * no longer valid
759 jbd2_journal_cancel_revoke(handle, jh);
761 out:
762 if (unlikely(frozen_buffer)) /* It's usually NULL */
763 jbd2_free(frozen_buffer, bh->b_size);
765 JBUFFER_TRACE(jh, "exit");
766 return error;
770 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
771 * @handle: transaction to add buffer modifications to
772 * @bh: bh to be used for metadata writes
773 * @credits: variable that will receive credits for the buffer
775 * Returns an error code or 0 on success.
777 * In full data journalling mode the buffer may be of type BJ_AsyncData,
778 * because we're write()ing a buffer which is also part of a shared mapping.
781 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
783 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
784 int rc;
786 /* We do not want to get caught playing with fields which the
787 * log thread also manipulates. Make sure that the buffer
788 * completes any outstanding IO before proceeding. */
789 rc = do_get_write_access(handle, jh, 0);
790 jbd2_journal_put_journal_head(jh);
791 return rc;
796 * When the user wants to journal a newly created buffer_head
797 * (ie. getblk() returned a new buffer and we are going to populate it
798 * manually rather than reading off disk), then we need to keep the
799 * buffer_head locked until it has been completely filled with new
800 * data. In this case, we should be able to make the assertion that
801 * the bh is not already part of an existing transaction.
803 * The buffer should already be locked by the caller by this point.
804 * There is no lock ranking violation: it was a newly created,
805 * unlocked buffer beforehand. */
808 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
809 * @handle: transaction to new buffer to
810 * @bh: new buffer.
812 * Call this if you create a new bh.
814 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
816 transaction_t *transaction = handle->h_transaction;
817 journal_t *journal = transaction->t_journal;
818 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
819 int err;
821 jbd_debug(5, "journal_head %p\n", jh);
822 err = -EROFS;
823 if (is_handle_aborted(handle))
824 goto out;
825 err = 0;
827 JBUFFER_TRACE(jh, "entry");
829 * The buffer may already belong to this transaction due to pre-zeroing
830 * in the filesystem's new_block code. It may also be on the previous,
831 * committing transaction's lists, but it HAS to be in Forget state in
832 * that case: the transaction must have deleted the buffer for it to be
833 * reused here.
835 jbd_lock_bh_state(bh);
836 spin_lock(&journal->j_list_lock);
837 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
838 jh->b_transaction == NULL ||
839 (jh->b_transaction == journal->j_committing_transaction &&
840 jh->b_jlist == BJ_Forget)));
842 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
843 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
845 if (jh->b_transaction == NULL) {
846 jh->b_transaction = transaction;
848 /* first access by this transaction */
849 jh->b_modified = 0;
851 JBUFFER_TRACE(jh, "file as BJ_Reserved");
852 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
853 } else if (jh->b_transaction == journal->j_committing_transaction) {
854 /* first access by this transaction */
855 jh->b_modified = 0;
857 JBUFFER_TRACE(jh, "set next transaction");
858 jh->b_next_transaction = transaction;
860 spin_unlock(&journal->j_list_lock);
861 jbd_unlock_bh_state(bh);
864 * akpm: I added this. ext3_alloc_branch can pick up new indirect
865 * blocks which contain freed but then revoked metadata. We need
866 * to cancel the revoke in case we end up freeing it yet again
867 * and the reallocating as data - this would cause a second revoke,
868 * which hits an assertion error.
870 JBUFFER_TRACE(jh, "cancelling revoke");
871 jbd2_journal_cancel_revoke(handle, jh);
872 jbd2_journal_put_journal_head(jh);
873 out:
874 return err;
878 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
879 * non-rewindable consequences
880 * @handle: transaction
881 * @bh: buffer to undo
882 * @credits: store the number of taken credits here (if not NULL)
884 * Sometimes there is a need to distinguish between metadata which has
885 * been committed to disk and that which has not. The ext3fs code uses
886 * this for freeing and allocating space, we have to make sure that we
887 * do not reuse freed space until the deallocation has been committed,
888 * since if we overwrote that space we would make the delete
889 * un-rewindable in case of a crash.
891 * To deal with that, jbd2_journal_get_undo_access requests write access to a
892 * buffer for parts of non-rewindable operations such as delete
893 * operations on the bitmaps. The journaling code must keep a copy of
894 * the buffer's contents prior to the undo_access call until such time
895 * as we know that the buffer has definitely been committed to disk.
897 * We never need to know which transaction the committed data is part
898 * of, buffers touched here are guaranteed to be dirtied later and so
899 * will be committed to a new transaction in due course, at which point
900 * we can discard the old committed data pointer.
902 * Returns error number or 0 on success.
904 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
906 int err;
907 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
908 char *committed_data = NULL;
910 JBUFFER_TRACE(jh, "entry");
913 * Do this first --- it can drop the journal lock, so we want to
914 * make sure that obtaining the committed_data is done
915 * atomically wrt. completion of any outstanding commits.
917 err = do_get_write_access(handle, jh, 1);
918 if (err)
919 goto out;
921 repeat:
922 if (!jh->b_committed_data) {
923 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
924 if (!committed_data) {
925 printk(KERN_EMERG "%s: No memory for committed data\n",
926 __func__);
927 err = -ENOMEM;
928 goto out;
932 jbd_lock_bh_state(bh);
933 if (!jh->b_committed_data) {
934 /* Copy out the current buffer contents into the
935 * preserved, committed copy. */
936 JBUFFER_TRACE(jh, "generate b_committed data");
937 if (!committed_data) {
938 jbd_unlock_bh_state(bh);
939 goto repeat;
942 jh->b_committed_data = committed_data;
943 committed_data = NULL;
944 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
946 jbd_unlock_bh_state(bh);
947 out:
948 jbd2_journal_put_journal_head(jh);
949 if (unlikely(committed_data))
950 jbd2_free(committed_data, bh->b_size);
951 return err;
955 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
956 * @bh: buffer to trigger on
957 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
959 * Set any triggers on this journal_head. This is always safe, because
960 * triggers for a committing buffer will be saved off, and triggers for
961 * a running transaction will match the buffer in that transaction.
963 * Call with NULL to clear the triggers.
965 void jbd2_journal_set_triggers(struct buffer_head *bh,
966 struct jbd2_buffer_trigger_type *type)
968 struct journal_head *jh = bh2jh(bh);
970 jh->b_triggers = type;
973 void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
974 struct jbd2_buffer_trigger_type *triggers)
976 struct buffer_head *bh = jh2bh(jh);
978 if (!triggers || !triggers->t_commit)
979 return;
981 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
984 void jbd2_buffer_abort_trigger(struct journal_head *jh,
985 struct jbd2_buffer_trigger_type *triggers)
987 if (!triggers || !triggers->t_abort)
988 return;
990 triggers->t_abort(triggers, jh2bh(jh));
996 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
997 * @handle: transaction to add buffer to.
998 * @bh: buffer to mark
1000 * mark dirty metadata which needs to be journaled as part of the current
1001 * transaction.
1003 * The buffer is placed on the transaction's metadata list and is marked
1004 * as belonging to the transaction.
1006 * Returns error number or 0 on success.
1008 * Special care needs to be taken if the buffer already belongs to the
1009 * current committing transaction (in which case we should have frozen
1010 * data present for that commit). In that case, we don't relink the
1011 * buffer: that only gets done when the old transaction finally
1012 * completes its commit.
1014 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1016 transaction_t *transaction = handle->h_transaction;
1017 journal_t *journal = transaction->t_journal;
1018 struct journal_head *jh = bh2jh(bh);
1020 jbd_debug(5, "journal_head %p\n", jh);
1021 JBUFFER_TRACE(jh, "entry");
1022 if (is_handle_aborted(handle))
1023 goto out;
1025 jbd_lock_bh_state(bh);
1027 if (jh->b_modified == 0) {
1029 * This buffer's got modified and becoming part
1030 * of the transaction. This needs to be done
1031 * once a transaction -bzzz
1033 jh->b_modified = 1;
1034 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1035 handle->h_buffer_credits--;
1039 * fastpath, to avoid expensive locking. If this buffer is already
1040 * on the running transaction's metadata list there is nothing to do.
1041 * Nobody can take it off again because there is a handle open.
1042 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1043 * result in this test being false, so we go in and take the locks.
1045 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1046 JBUFFER_TRACE(jh, "fastpath");
1047 J_ASSERT_JH(jh, jh->b_transaction ==
1048 journal->j_running_transaction);
1049 goto out_unlock_bh;
1052 set_buffer_jbddirty(bh);
1055 * Metadata already on the current transaction list doesn't
1056 * need to be filed. Metadata on another transaction's list must
1057 * be committing, and will be refiled once the commit completes:
1058 * leave it alone for now.
1060 if (jh->b_transaction != transaction) {
1061 JBUFFER_TRACE(jh, "already on other transaction");
1062 J_ASSERT_JH(jh, jh->b_transaction ==
1063 journal->j_committing_transaction);
1064 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1065 /* And this case is illegal: we can't reuse another
1066 * transaction's data buffer, ever. */
1067 goto out_unlock_bh;
1070 /* That test should have eliminated the following case: */
1071 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1073 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1074 spin_lock(&journal->j_list_lock);
1075 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1076 spin_unlock(&journal->j_list_lock);
1077 out_unlock_bh:
1078 jbd_unlock_bh_state(bh);
1079 out:
1080 JBUFFER_TRACE(jh, "exit");
1081 return 0;
1085 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
1086 * updates, if the update decided in the end that it didn't need access.
1089 void
1090 jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1092 BUFFER_TRACE(bh, "entry");
1096 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1097 * @handle: transaction handle
1098 * @bh: bh to 'forget'
1100 * We can only do the bforget if there are no commits pending against the
1101 * buffer. If the buffer is dirty in the current running transaction we
1102 * can safely unlink it.
1104 * bh may not be a journalled buffer at all - it may be a non-JBD
1105 * buffer which came off the hashtable. Check for this.
1107 * Decrements bh->b_count by one.
1109 * Allow this call even if the handle has aborted --- it may be part of
1110 * the caller's cleanup after an abort.
1112 int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1114 transaction_t *transaction = handle->h_transaction;
1115 journal_t *journal = transaction->t_journal;
1116 struct journal_head *jh;
1117 int drop_reserve = 0;
1118 int err = 0;
1119 int was_modified = 0;
1121 BUFFER_TRACE(bh, "entry");
1123 jbd_lock_bh_state(bh);
1124 spin_lock(&journal->j_list_lock);
1126 if (!buffer_jbd(bh))
1127 goto not_jbd;
1128 jh = bh2jh(bh);
1130 /* Critical error: attempting to delete a bitmap buffer, maybe?
1131 * Don't do any jbd operations, and return an error. */
1132 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1133 "inconsistent data on disk")) {
1134 err = -EIO;
1135 goto not_jbd;
1138 /* keep track of wether or not this transaction modified us */
1139 was_modified = jh->b_modified;
1142 * The buffer's going from the transaction, we must drop
1143 * all references -bzzz
1145 jh->b_modified = 0;
1147 if (jh->b_transaction == handle->h_transaction) {
1148 J_ASSERT_JH(jh, !jh->b_frozen_data);
1150 /* If we are forgetting a buffer which is already part
1151 * of this transaction, then we can just drop it from
1152 * the transaction immediately. */
1153 clear_buffer_dirty(bh);
1154 clear_buffer_jbddirty(bh);
1156 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1159 * we only want to drop a reference if this transaction
1160 * modified the buffer
1162 if (was_modified)
1163 drop_reserve = 1;
1166 * We are no longer going to journal this buffer.
1167 * However, the commit of this transaction is still
1168 * important to the buffer: the delete that we are now
1169 * processing might obsolete an old log entry, so by
1170 * committing, we can satisfy the buffer's checkpoint.
1172 * So, if we have a checkpoint on the buffer, we should
1173 * now refile the buffer on our BJ_Forget list so that
1174 * we know to remove the checkpoint after we commit.
1177 if (jh->b_cp_transaction) {
1178 __jbd2_journal_temp_unlink_buffer(jh);
1179 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1180 } else {
1181 __jbd2_journal_unfile_buffer(jh);
1182 jbd2_journal_remove_journal_head(bh);
1183 __brelse(bh);
1184 if (!buffer_jbd(bh)) {
1185 spin_unlock(&journal->j_list_lock);
1186 jbd_unlock_bh_state(bh);
1187 __bforget(bh);
1188 goto drop;
1191 } else if (jh->b_transaction) {
1192 J_ASSERT_JH(jh, (jh->b_transaction ==
1193 journal->j_committing_transaction));
1194 /* However, if the buffer is still owned by a prior
1195 * (committing) transaction, we can't drop it yet... */
1196 JBUFFER_TRACE(jh, "belongs to older transaction");
1197 /* ... but we CAN drop it from the new transaction if we
1198 * have also modified it since the original commit. */
1200 if (jh->b_next_transaction) {
1201 J_ASSERT(jh->b_next_transaction == transaction);
1202 jh->b_next_transaction = NULL;
1205 * only drop a reference if this transaction modified
1206 * the buffer
1208 if (was_modified)
1209 drop_reserve = 1;
1213 not_jbd:
1214 spin_unlock(&journal->j_list_lock);
1215 jbd_unlock_bh_state(bh);
1216 __brelse(bh);
1217 drop:
1218 if (drop_reserve) {
1219 /* no need to reserve log space for this block -bzzz */
1220 handle->h_buffer_credits++;
1222 return err;
1226 * int jbd2_journal_stop() - complete a transaction
1227 * @handle: tranaction to complete.
1229 * All done for a particular handle.
1231 * There is not much action needed here. We just return any remaining
1232 * buffer credits to the transaction and remove the handle. The only
1233 * complication is that we need to start a commit operation if the
1234 * filesystem is marked for synchronous update.
1236 * jbd2_journal_stop itself will not usually return an error, but it may
1237 * do so in unusual circumstances. In particular, expect it to
1238 * return -EIO if a jbd2_journal_abort has been executed since the
1239 * transaction began.
1241 int jbd2_journal_stop(handle_t *handle)
1243 transaction_t *transaction = handle->h_transaction;
1244 journal_t *journal = transaction->t_journal;
1245 int err;
1246 pid_t pid;
1248 J_ASSERT(journal_current_handle() == handle);
1250 if (is_handle_aborted(handle))
1251 err = -EIO;
1252 else {
1253 J_ASSERT(transaction->t_updates > 0);
1254 err = 0;
1257 if (--handle->h_ref > 0) {
1258 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1259 handle->h_ref);
1260 return err;
1263 jbd_debug(4, "Handle %p going down\n", handle);
1266 * Implement synchronous transaction batching. If the handle
1267 * was synchronous, don't force a commit immediately. Let's
1268 * yield and let another thread piggyback onto this
1269 * transaction. Keep doing that while new threads continue to
1270 * arrive. It doesn't cost much - we're about to run a commit
1271 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1272 * operations by 30x or more...
1274 * We try and optimize the sleep time against what the
1275 * underlying disk can do, instead of having a static sleep
1276 * time. This is useful for the case where our storage is so
1277 * fast that it is more optimal to go ahead and force a flush
1278 * and wait for the transaction to be committed than it is to
1279 * wait for an arbitrary amount of time for new writers to
1280 * join the transaction. We achieve this by measuring how
1281 * long it takes to commit a transaction, and compare it with
1282 * how long this transaction has been running, and if run time
1283 * < commit time then we sleep for the delta and commit. This
1284 * greatly helps super fast disks that would see slowdowns as
1285 * more threads started doing fsyncs.
1287 * But don't do this if this process was the most recent one
1288 * to perform a synchronous write. We do this to detect the
1289 * case where a single process is doing a stream of sync
1290 * writes. No point in waiting for joiners in that case.
1292 pid = current->pid;
1293 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1294 u64 commit_time, trans_time;
1296 journal->j_last_sync_writer = pid;
1298 spin_lock(&journal->j_state_lock);
1299 commit_time = journal->j_average_commit_time;
1300 spin_unlock(&journal->j_state_lock);
1302 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1303 transaction->t_start_time));
1305 commit_time = max_t(u64, commit_time,
1306 1000*journal->j_min_batch_time);
1307 commit_time = min_t(u64, commit_time,
1308 1000*journal->j_max_batch_time);
1310 if (trans_time < commit_time) {
1311 ktime_t expires = ktime_add_ns(ktime_get(),
1312 commit_time);
1313 set_current_state(TASK_UNINTERRUPTIBLE);
1314 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1318 current->journal_info = NULL;
1319 spin_lock(&journal->j_state_lock);
1320 spin_lock(&transaction->t_handle_lock);
1321 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1322 transaction->t_updates--;
1323 if (!transaction->t_updates) {
1324 wake_up(&journal->j_wait_updates);
1325 if (journal->j_barrier_count)
1326 wake_up(&journal->j_wait_transaction_locked);
1330 * If the handle is marked SYNC, we need to set another commit
1331 * going! We also want to force a commit if the current
1332 * transaction is occupying too much of the log, or if the
1333 * transaction is too old now.
1335 if (handle->h_sync ||
1336 transaction->t_outstanding_credits >
1337 journal->j_max_transaction_buffers ||
1338 time_after_eq(jiffies, transaction->t_expires)) {
1339 /* Do this even for aborted journals: an abort still
1340 * completes the commit thread, it just doesn't write
1341 * anything to disk. */
1342 tid_t tid = transaction->t_tid;
1344 spin_unlock(&transaction->t_handle_lock);
1345 jbd_debug(2, "transaction too old, requesting commit for "
1346 "handle %p\n", handle);
1347 /* This is non-blocking */
1348 __jbd2_log_start_commit(journal, transaction->t_tid);
1349 spin_unlock(&journal->j_state_lock);
1352 * Special case: JBD2_SYNC synchronous updates require us
1353 * to wait for the commit to complete.
1355 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1356 err = jbd2_log_wait_commit(journal, tid);
1357 } else {
1358 spin_unlock(&transaction->t_handle_lock);
1359 spin_unlock(&journal->j_state_lock);
1362 lock_map_release(&handle->h_lockdep_map);
1364 jbd2_free_handle(handle);
1365 return err;
1369 * int jbd2_journal_force_commit() - force any uncommitted transactions
1370 * @journal: journal to force
1372 * For synchronous operations: force any uncommitted transactions
1373 * to disk. May seem kludgy, but it reuses all the handle batching
1374 * code in a very simple manner.
1376 int jbd2_journal_force_commit(journal_t *journal)
1378 handle_t *handle;
1379 int ret;
1381 handle = jbd2_journal_start(journal, 1);
1382 if (IS_ERR(handle)) {
1383 ret = PTR_ERR(handle);
1384 } else {
1385 handle->h_sync = 1;
1386 ret = jbd2_journal_stop(handle);
1388 return ret;
1393 * List management code snippets: various functions for manipulating the
1394 * transaction buffer lists.
1399 * Append a buffer to a transaction list, given the transaction's list head
1400 * pointer.
1402 * j_list_lock is held.
1404 * jbd_lock_bh_state(jh2bh(jh)) is held.
1407 static inline void
1408 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1410 if (!*list) {
1411 jh->b_tnext = jh->b_tprev = jh;
1412 *list = jh;
1413 } else {
1414 /* Insert at the tail of the list to preserve order */
1415 struct journal_head *first = *list, *last = first->b_tprev;
1416 jh->b_tprev = last;
1417 jh->b_tnext = first;
1418 last->b_tnext = first->b_tprev = jh;
1423 * Remove a buffer from a transaction list, given the transaction's list
1424 * head pointer.
1426 * Called with j_list_lock held, and the journal may not be locked.
1428 * jbd_lock_bh_state(jh2bh(jh)) is held.
1431 static inline void
1432 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1434 if (*list == jh) {
1435 *list = jh->b_tnext;
1436 if (*list == jh)
1437 *list = NULL;
1439 jh->b_tprev->b_tnext = jh->b_tnext;
1440 jh->b_tnext->b_tprev = jh->b_tprev;
1444 * Remove a buffer from the appropriate transaction list.
1446 * Note that this function can *change* the value of
1447 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1448 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1449 * of these pointers, it could go bad. Generally the caller needs to re-read
1450 * the pointer from the transaction_t.
1452 * Called under j_list_lock. The journal may not be locked.
1454 void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1456 struct journal_head **list = NULL;
1457 transaction_t *transaction;
1458 struct buffer_head *bh = jh2bh(jh);
1460 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1461 transaction = jh->b_transaction;
1462 if (transaction)
1463 assert_spin_locked(&transaction->t_journal->j_list_lock);
1465 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1466 if (jh->b_jlist != BJ_None)
1467 J_ASSERT_JH(jh, transaction != NULL);
1469 switch (jh->b_jlist) {
1470 case BJ_None:
1471 return;
1472 case BJ_Metadata:
1473 transaction->t_nr_buffers--;
1474 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1475 list = &transaction->t_buffers;
1476 break;
1477 case BJ_Forget:
1478 list = &transaction->t_forget;
1479 break;
1480 case BJ_IO:
1481 list = &transaction->t_iobuf_list;
1482 break;
1483 case BJ_Shadow:
1484 list = &transaction->t_shadow_list;
1485 break;
1486 case BJ_LogCtl:
1487 list = &transaction->t_log_list;
1488 break;
1489 case BJ_Reserved:
1490 list = &transaction->t_reserved_list;
1491 break;
1494 __blist_del_buffer(list, jh);
1495 jh->b_jlist = BJ_None;
1496 if (test_clear_buffer_jbddirty(bh))
1497 mark_buffer_dirty(bh); /* Expose it to the VM */
1500 void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1502 __jbd2_journal_temp_unlink_buffer(jh);
1503 jh->b_transaction = NULL;
1506 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1508 jbd_lock_bh_state(jh2bh(jh));
1509 spin_lock(&journal->j_list_lock);
1510 __jbd2_journal_unfile_buffer(jh);
1511 spin_unlock(&journal->j_list_lock);
1512 jbd_unlock_bh_state(jh2bh(jh));
1516 * Called from jbd2_journal_try_to_free_buffers().
1518 * Called under jbd_lock_bh_state(bh)
1520 static void
1521 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1523 struct journal_head *jh;
1525 jh = bh2jh(bh);
1527 if (buffer_locked(bh) || buffer_dirty(bh))
1528 goto out;
1530 if (jh->b_next_transaction != NULL)
1531 goto out;
1533 spin_lock(&journal->j_list_lock);
1534 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1535 /* written-back checkpointed metadata buffer */
1536 if (jh->b_jlist == BJ_None) {
1537 JBUFFER_TRACE(jh, "remove from checkpoint list");
1538 __jbd2_journal_remove_checkpoint(jh);
1539 jbd2_journal_remove_journal_head(bh);
1540 __brelse(bh);
1543 spin_unlock(&journal->j_list_lock);
1544 out:
1545 return;
1549 * jbd2_journal_try_to_free_buffers() could race with
1550 * jbd2_journal_commit_transaction(). The later might still hold the
1551 * reference count to the buffers when inspecting them on
1552 * t_syncdata_list or t_locked_list.
1554 * jbd2_journal_try_to_free_buffers() will call this function to
1555 * wait for the current transaction to finish syncing data buffers, before
1556 * try to free that buffer.
1558 * Called with journal->j_state_lock hold.
1560 static void jbd2_journal_wait_for_transaction_sync_data(journal_t *journal)
1562 transaction_t *transaction;
1563 tid_t tid;
1565 spin_lock(&journal->j_state_lock);
1566 transaction = journal->j_committing_transaction;
1568 if (!transaction) {
1569 spin_unlock(&journal->j_state_lock);
1570 return;
1573 tid = transaction->t_tid;
1574 spin_unlock(&journal->j_state_lock);
1575 jbd2_log_wait_commit(journal, tid);
1579 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1580 * @journal: journal for operation
1581 * @page: to try and free
1582 * @gfp_mask: we use the mask to detect how hard should we try to release
1583 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1584 * release the buffers.
1587 * For all the buffers on this page,
1588 * if they are fully written out ordered data, move them onto BUF_CLEAN
1589 * so try_to_free_buffers() can reap them.
1591 * This function returns non-zero if we wish try_to_free_buffers()
1592 * to be called. We do this if the page is releasable by try_to_free_buffers().
1593 * We also do it if the page has locked or dirty buffers and the caller wants
1594 * us to perform sync or async writeout.
1596 * This complicates JBD locking somewhat. We aren't protected by the
1597 * BKL here. We wish to remove the buffer from its committing or
1598 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1600 * This may *change* the value of transaction_t->t_datalist, so anyone
1601 * who looks at t_datalist needs to lock against this function.
1603 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1604 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1605 * will come out of the lock with the buffer dirty, which makes it
1606 * ineligible for release here.
1608 * Who else is affected by this? hmm... Really the only contender
1609 * is do_get_write_access() - it could be looking at the buffer while
1610 * journal_try_to_free_buffer() is changing its state. But that
1611 * cannot happen because we never reallocate freed data as metadata
1612 * while the data is part of a transaction. Yes?
1614 * Return 0 on failure, 1 on success
1616 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1617 struct page *page, gfp_t gfp_mask)
1619 struct buffer_head *head;
1620 struct buffer_head *bh;
1621 int ret = 0;
1623 J_ASSERT(PageLocked(page));
1625 head = page_buffers(page);
1626 bh = head;
1627 do {
1628 struct journal_head *jh;
1631 * We take our own ref against the journal_head here to avoid
1632 * having to add tons of locking around each instance of
1633 * jbd2_journal_remove_journal_head() and
1634 * jbd2_journal_put_journal_head().
1636 jh = jbd2_journal_grab_journal_head(bh);
1637 if (!jh)
1638 continue;
1640 jbd_lock_bh_state(bh);
1641 __journal_try_to_free_buffer(journal, bh);
1642 jbd2_journal_put_journal_head(jh);
1643 jbd_unlock_bh_state(bh);
1644 if (buffer_jbd(bh))
1645 goto busy;
1646 } while ((bh = bh->b_this_page) != head);
1648 ret = try_to_free_buffers(page);
1651 * There are a number of places where jbd2_journal_try_to_free_buffers()
1652 * could race with jbd2_journal_commit_transaction(), the later still
1653 * holds the reference to the buffers to free while processing them.
1654 * try_to_free_buffers() failed to free those buffers. Some of the
1655 * caller of releasepage() request page buffers to be dropped, otherwise
1656 * treat the fail-to-free as errors (such as generic_file_direct_IO())
1658 * So, if the caller of try_to_release_page() wants the synchronous
1659 * behaviour(i.e make sure buffers are dropped upon return),
1660 * let's wait for the current transaction to finish flush of
1661 * dirty data buffers, then try to free those buffers again,
1662 * with the journal locked.
1664 if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
1665 jbd2_journal_wait_for_transaction_sync_data(journal);
1666 ret = try_to_free_buffers(page);
1669 busy:
1670 return ret;
1674 * This buffer is no longer needed. If it is on an older transaction's
1675 * checkpoint list we need to record it on this transaction's forget list
1676 * to pin this buffer (and hence its checkpointing transaction) down until
1677 * this transaction commits. If the buffer isn't on a checkpoint list, we
1678 * release it.
1679 * Returns non-zero if JBD no longer has an interest in the buffer.
1681 * Called under j_list_lock.
1683 * Called under jbd_lock_bh_state(bh).
1685 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1687 int may_free = 1;
1688 struct buffer_head *bh = jh2bh(jh);
1690 __jbd2_journal_unfile_buffer(jh);
1692 if (jh->b_cp_transaction) {
1693 JBUFFER_TRACE(jh, "on running+cp transaction");
1694 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1695 clear_buffer_jbddirty(bh);
1696 may_free = 0;
1697 } else {
1698 JBUFFER_TRACE(jh, "on running transaction");
1699 jbd2_journal_remove_journal_head(bh);
1700 __brelse(bh);
1702 return may_free;
1706 * jbd2_journal_invalidatepage
1708 * This code is tricky. It has a number of cases to deal with.
1710 * There are two invariants which this code relies on:
1712 * i_size must be updated on disk before we start calling invalidatepage on the
1713 * data.
1715 * This is done in ext3 by defining an ext3_setattr method which
1716 * updates i_size before truncate gets going. By maintaining this
1717 * invariant, we can be sure that it is safe to throw away any buffers
1718 * attached to the current transaction: once the transaction commits,
1719 * we know that the data will not be needed.
1721 * Note however that we can *not* throw away data belonging to the
1722 * previous, committing transaction!
1724 * Any disk blocks which *are* part of the previous, committing
1725 * transaction (and which therefore cannot be discarded immediately) are
1726 * not going to be reused in the new running transaction
1728 * The bitmap committed_data images guarantee this: any block which is
1729 * allocated in one transaction and removed in the next will be marked
1730 * as in-use in the committed_data bitmap, so cannot be reused until
1731 * the next transaction to delete the block commits. This means that
1732 * leaving committing buffers dirty is quite safe: the disk blocks
1733 * cannot be reallocated to a different file and so buffer aliasing is
1734 * not possible.
1737 * The above applies mainly to ordered data mode. In writeback mode we
1738 * don't make guarantees about the order in which data hits disk --- in
1739 * particular we don't guarantee that new dirty data is flushed before
1740 * transaction commit --- so it is always safe just to discard data
1741 * immediately in that mode. --sct
1745 * The journal_unmap_buffer helper function returns zero if the buffer
1746 * concerned remains pinned as an anonymous buffer belonging to an older
1747 * transaction.
1749 * We're outside-transaction here. Either or both of j_running_transaction
1750 * and j_committing_transaction may be NULL.
1752 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1754 transaction_t *transaction;
1755 struct journal_head *jh;
1756 int may_free = 1;
1757 int ret;
1759 BUFFER_TRACE(bh, "entry");
1762 * It is safe to proceed here without the j_list_lock because the
1763 * buffers cannot be stolen by try_to_free_buffers as long as we are
1764 * holding the page lock. --sct
1767 if (!buffer_jbd(bh))
1768 goto zap_buffer_unlocked;
1770 /* OK, we have data buffer in journaled mode */
1771 spin_lock(&journal->j_state_lock);
1772 jbd_lock_bh_state(bh);
1773 spin_lock(&journal->j_list_lock);
1775 jh = jbd2_journal_grab_journal_head(bh);
1776 if (!jh)
1777 goto zap_buffer_no_jh;
1779 transaction = jh->b_transaction;
1780 if (transaction == NULL) {
1781 /* First case: not on any transaction. If it
1782 * has no checkpoint link, then we can zap it:
1783 * it's a writeback-mode buffer so we don't care
1784 * if it hits disk safely. */
1785 if (!jh->b_cp_transaction) {
1786 JBUFFER_TRACE(jh, "not on any transaction: zap");
1787 goto zap_buffer;
1790 if (!buffer_dirty(bh)) {
1791 /* bdflush has written it. We can drop it now */
1792 goto zap_buffer;
1795 /* OK, it must be in the journal but still not
1796 * written fully to disk: it's metadata or
1797 * journaled data... */
1799 if (journal->j_running_transaction) {
1800 /* ... and once the current transaction has
1801 * committed, the buffer won't be needed any
1802 * longer. */
1803 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1804 ret = __dispose_buffer(jh,
1805 journal->j_running_transaction);
1806 jbd2_journal_put_journal_head(jh);
1807 spin_unlock(&journal->j_list_lock);
1808 jbd_unlock_bh_state(bh);
1809 spin_unlock(&journal->j_state_lock);
1810 return ret;
1811 } else {
1812 /* There is no currently-running transaction. So the
1813 * orphan record which we wrote for this file must have
1814 * passed into commit. We must attach this buffer to
1815 * the committing transaction, if it exists. */
1816 if (journal->j_committing_transaction) {
1817 JBUFFER_TRACE(jh, "give to committing trans");
1818 ret = __dispose_buffer(jh,
1819 journal->j_committing_transaction);
1820 jbd2_journal_put_journal_head(jh);
1821 spin_unlock(&journal->j_list_lock);
1822 jbd_unlock_bh_state(bh);
1823 spin_unlock(&journal->j_state_lock);
1824 return ret;
1825 } else {
1826 /* The orphan record's transaction has
1827 * committed. We can cleanse this buffer */
1828 clear_buffer_jbddirty(bh);
1829 goto zap_buffer;
1832 } else if (transaction == journal->j_committing_transaction) {
1833 JBUFFER_TRACE(jh, "on committing transaction");
1835 * If it is committing, we simply cannot touch it. We
1836 * can remove it's next_transaction pointer from the
1837 * running transaction if that is set, but nothing
1838 * else. */
1839 set_buffer_freed(bh);
1840 if (jh->b_next_transaction) {
1841 J_ASSERT(jh->b_next_transaction ==
1842 journal->j_running_transaction);
1843 jh->b_next_transaction = NULL;
1845 jbd2_journal_put_journal_head(jh);
1846 spin_unlock(&journal->j_list_lock);
1847 jbd_unlock_bh_state(bh);
1848 spin_unlock(&journal->j_state_lock);
1849 return 0;
1850 } else {
1851 /* Good, the buffer belongs to the running transaction.
1852 * We are writing our own transaction's data, not any
1853 * previous one's, so it is safe to throw it away
1854 * (remember that we expect the filesystem to have set
1855 * i_size already for this truncate so recovery will not
1856 * expose the disk blocks we are discarding here.) */
1857 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1858 JBUFFER_TRACE(jh, "on running transaction");
1859 may_free = __dispose_buffer(jh, transaction);
1862 zap_buffer:
1863 jbd2_journal_put_journal_head(jh);
1864 zap_buffer_no_jh:
1865 spin_unlock(&journal->j_list_lock);
1866 jbd_unlock_bh_state(bh);
1867 spin_unlock(&journal->j_state_lock);
1868 zap_buffer_unlocked:
1869 clear_buffer_dirty(bh);
1870 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1871 clear_buffer_mapped(bh);
1872 clear_buffer_req(bh);
1873 clear_buffer_new(bh);
1874 bh->b_bdev = NULL;
1875 return may_free;
1879 * void jbd2_journal_invalidatepage()
1880 * @journal: journal to use for flush...
1881 * @page: page to flush
1882 * @offset: length of page to invalidate.
1884 * Reap page buffers containing data after offset in page.
1887 void jbd2_journal_invalidatepage(journal_t *journal,
1888 struct page *page,
1889 unsigned long offset)
1891 struct buffer_head *head, *bh, *next;
1892 unsigned int curr_off = 0;
1893 int may_free = 1;
1895 if (!PageLocked(page))
1896 BUG();
1897 if (!page_has_buffers(page))
1898 return;
1900 /* We will potentially be playing with lists other than just the
1901 * data lists (especially for journaled data mode), so be
1902 * cautious in our locking. */
1904 head = bh = page_buffers(page);
1905 do {
1906 unsigned int next_off = curr_off + bh->b_size;
1907 next = bh->b_this_page;
1909 if (offset <= curr_off) {
1910 /* This block is wholly outside the truncation point */
1911 lock_buffer(bh);
1912 may_free &= journal_unmap_buffer(journal, bh);
1913 unlock_buffer(bh);
1915 curr_off = next_off;
1916 bh = next;
1918 } while (bh != head);
1920 if (!offset) {
1921 if (may_free && try_to_free_buffers(page))
1922 J_ASSERT(!page_has_buffers(page));
1927 * File a buffer on the given transaction list.
1929 void __jbd2_journal_file_buffer(struct journal_head *jh,
1930 transaction_t *transaction, int jlist)
1932 struct journal_head **list = NULL;
1933 int was_dirty = 0;
1934 struct buffer_head *bh = jh2bh(jh);
1936 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1937 assert_spin_locked(&transaction->t_journal->j_list_lock);
1939 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1940 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1941 jh->b_transaction == NULL);
1943 if (jh->b_transaction && jh->b_jlist == jlist)
1944 return;
1946 /* The following list of buffer states needs to be consistent
1947 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
1948 * state. */
1950 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1951 jlist == BJ_Shadow || jlist == BJ_Forget) {
1952 if (test_clear_buffer_dirty(bh) ||
1953 test_clear_buffer_jbddirty(bh))
1954 was_dirty = 1;
1957 if (jh->b_transaction)
1958 __jbd2_journal_temp_unlink_buffer(jh);
1959 jh->b_transaction = transaction;
1961 switch (jlist) {
1962 case BJ_None:
1963 J_ASSERT_JH(jh, !jh->b_committed_data);
1964 J_ASSERT_JH(jh, !jh->b_frozen_data);
1965 return;
1966 case BJ_Metadata:
1967 transaction->t_nr_buffers++;
1968 list = &transaction->t_buffers;
1969 break;
1970 case BJ_Forget:
1971 list = &transaction->t_forget;
1972 break;
1973 case BJ_IO:
1974 list = &transaction->t_iobuf_list;
1975 break;
1976 case BJ_Shadow:
1977 list = &transaction->t_shadow_list;
1978 break;
1979 case BJ_LogCtl:
1980 list = &transaction->t_log_list;
1981 break;
1982 case BJ_Reserved:
1983 list = &transaction->t_reserved_list;
1984 break;
1987 __blist_add_buffer(list, jh);
1988 jh->b_jlist = jlist;
1990 if (was_dirty)
1991 set_buffer_jbddirty(bh);
1994 void jbd2_journal_file_buffer(struct journal_head *jh,
1995 transaction_t *transaction, int jlist)
1997 jbd_lock_bh_state(jh2bh(jh));
1998 spin_lock(&transaction->t_journal->j_list_lock);
1999 __jbd2_journal_file_buffer(jh, transaction, jlist);
2000 spin_unlock(&transaction->t_journal->j_list_lock);
2001 jbd_unlock_bh_state(jh2bh(jh));
2005 * Remove a buffer from its current buffer list in preparation for
2006 * dropping it from its current transaction entirely. If the buffer has
2007 * already started to be used by a subsequent transaction, refile the
2008 * buffer on that transaction's metadata list.
2010 * Called under journal->j_list_lock
2012 * Called under jbd_lock_bh_state(jh2bh(jh))
2014 void __jbd2_journal_refile_buffer(struct journal_head *jh)
2016 int was_dirty;
2017 struct buffer_head *bh = jh2bh(jh);
2019 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2020 if (jh->b_transaction)
2021 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2023 /* If the buffer is now unused, just drop it. */
2024 if (jh->b_next_transaction == NULL) {
2025 __jbd2_journal_unfile_buffer(jh);
2026 return;
2030 * It has been modified by a later transaction: add it to the new
2031 * transaction's metadata list.
2034 was_dirty = test_clear_buffer_jbddirty(bh);
2035 __jbd2_journal_temp_unlink_buffer(jh);
2036 jh->b_transaction = jh->b_next_transaction;
2037 jh->b_next_transaction = NULL;
2038 __jbd2_journal_file_buffer(jh, jh->b_transaction,
2039 jh->b_modified ? BJ_Metadata : BJ_Reserved);
2040 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2042 if (was_dirty)
2043 set_buffer_jbddirty(bh);
2047 * For the unlocked version of this call, also make sure that any
2048 * hanging journal_head is cleaned up if necessary.
2050 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2051 * operation on a buffer_head, in which the caller is probably going to
2052 * be hooking the journal_head onto other lists. In that case it is up
2053 * to the caller to remove the journal_head if necessary. For the
2054 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2055 * doing anything else to the buffer so we need to do the cleanup
2056 * ourselves to avoid a jh leak.
2058 * *** The journal_head may be freed by this call! ***
2060 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2062 struct buffer_head *bh = jh2bh(jh);
2064 jbd_lock_bh_state(bh);
2065 spin_lock(&journal->j_list_lock);
2067 __jbd2_journal_refile_buffer(jh);
2068 jbd_unlock_bh_state(bh);
2069 jbd2_journal_remove_journal_head(bh);
2071 spin_unlock(&journal->j_list_lock);
2072 __brelse(bh);
2076 * File inode in the inode list of the handle's transaction
2078 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2080 transaction_t *transaction = handle->h_transaction;
2081 journal_t *journal = transaction->t_journal;
2083 if (is_handle_aborted(handle))
2084 return -EIO;
2086 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2087 transaction->t_tid);
2090 * First check whether inode isn't already on the transaction's
2091 * lists without taking the lock. Note that this check is safe
2092 * without the lock as we cannot race with somebody removing inode
2093 * from the transaction. The reason is that we remove inode from the
2094 * transaction only in journal_release_jbd_inode() and when we commit
2095 * the transaction. We are guarded from the first case by holding
2096 * a reference to the inode. We are safe against the second case
2097 * because if jinode->i_transaction == transaction, commit code
2098 * cannot touch the transaction because we hold reference to it,
2099 * and if jinode->i_next_transaction == transaction, commit code
2100 * will only file the inode where we want it.
2102 if (jinode->i_transaction == transaction ||
2103 jinode->i_next_transaction == transaction)
2104 return 0;
2106 spin_lock(&journal->j_list_lock);
2108 if (jinode->i_transaction == transaction ||
2109 jinode->i_next_transaction == transaction)
2110 goto done;
2112 /* On some different transaction's list - should be
2113 * the committing one */
2114 if (jinode->i_transaction) {
2115 J_ASSERT(jinode->i_next_transaction == NULL);
2116 J_ASSERT(jinode->i_transaction ==
2117 journal->j_committing_transaction);
2118 jinode->i_next_transaction = transaction;
2119 goto done;
2121 /* Not on any transaction list... */
2122 J_ASSERT(!jinode->i_next_transaction);
2123 jinode->i_transaction = transaction;
2124 list_add(&jinode->i_list, &transaction->t_inode_list);
2125 done:
2126 spin_unlock(&journal->j_list_lock);
2128 return 0;
2132 * File truncate and transaction commit interact with each other in a
2133 * non-trivial way. If a transaction writing data block A is
2134 * committing, we cannot discard the data by truncate until we have
2135 * written them. Otherwise if we crashed after the transaction with
2136 * write has committed but before the transaction with truncate has
2137 * committed, we could see stale data in block A. This function is a
2138 * helper to solve this problem. It starts writeout of the truncated
2139 * part in case it is in the committing transaction.
2141 * Filesystem code must call this function when inode is journaled in
2142 * ordered mode before truncation happens and after the inode has been
2143 * placed on orphan list with the new inode size. The second condition
2144 * avoids the race that someone writes new data and we start
2145 * committing the transaction after this function has been called but
2146 * before a transaction for truncate is started (and furthermore it
2147 * allows us to optimize the case where the addition to orphan list
2148 * happens in the same transaction as write --- we don't have to write
2149 * any data in such case).
2151 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2152 struct jbd2_inode *jinode,
2153 loff_t new_size)
2155 transaction_t *inode_trans, *commit_trans;
2156 int ret = 0;
2158 /* This is a quick check to avoid locking if not necessary */
2159 if (!jinode->i_transaction)
2160 goto out;
2161 /* Locks are here just to force reading of recent values, it is
2162 * enough that the transaction was not committing before we started
2163 * a transaction adding the inode to orphan list */
2164 spin_lock(&journal->j_state_lock);
2165 commit_trans = journal->j_committing_transaction;
2166 spin_unlock(&journal->j_state_lock);
2167 spin_lock(&journal->j_list_lock);
2168 inode_trans = jinode->i_transaction;
2169 spin_unlock(&journal->j_list_lock);
2170 if (inode_trans == commit_trans) {
2171 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2172 new_size, LLONG_MAX);
2173 if (ret)
2174 jbd2_journal_abort(journal, ret);
2176 out:
2177 return ret;