ocfs2: Add CoW support for xattr.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd2 / transaction.c
blob494501edba6bcf0ce39c129848d5862177e1d38b
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 if (handle->h_sync)
1319 transaction->t_synchronous_commit = 1;
1320 current->journal_info = NULL;
1321 spin_lock(&journal->j_state_lock);
1322 spin_lock(&transaction->t_handle_lock);
1323 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1324 transaction->t_updates--;
1325 if (!transaction->t_updates) {
1326 wake_up(&journal->j_wait_updates);
1327 if (journal->j_barrier_count)
1328 wake_up(&journal->j_wait_transaction_locked);
1332 * If the handle is marked SYNC, we need to set another commit
1333 * going! We also want to force a commit if the current
1334 * transaction is occupying too much of the log, or if the
1335 * transaction is too old now.
1337 if (handle->h_sync ||
1338 transaction->t_outstanding_credits >
1339 journal->j_max_transaction_buffers ||
1340 time_after_eq(jiffies, transaction->t_expires)) {
1341 /* Do this even for aborted journals: an abort still
1342 * completes the commit thread, it just doesn't write
1343 * anything to disk. */
1344 tid_t tid = transaction->t_tid;
1346 spin_unlock(&transaction->t_handle_lock);
1347 jbd_debug(2, "transaction too old, requesting commit for "
1348 "handle %p\n", handle);
1349 /* This is non-blocking */
1350 __jbd2_log_start_commit(journal, transaction->t_tid);
1351 spin_unlock(&journal->j_state_lock);
1354 * Special case: JBD2_SYNC synchronous updates require us
1355 * to wait for the commit to complete.
1357 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1358 err = jbd2_log_wait_commit(journal, tid);
1359 } else {
1360 spin_unlock(&transaction->t_handle_lock);
1361 spin_unlock(&journal->j_state_lock);
1364 lock_map_release(&handle->h_lockdep_map);
1366 jbd2_free_handle(handle);
1367 return err;
1371 * int jbd2_journal_force_commit() - force any uncommitted transactions
1372 * @journal: journal to force
1374 * For synchronous operations: force any uncommitted transactions
1375 * to disk. May seem kludgy, but it reuses all the handle batching
1376 * code in a very simple manner.
1378 int jbd2_journal_force_commit(journal_t *journal)
1380 handle_t *handle;
1381 int ret;
1383 handle = jbd2_journal_start(journal, 1);
1384 if (IS_ERR(handle)) {
1385 ret = PTR_ERR(handle);
1386 } else {
1387 handle->h_sync = 1;
1388 ret = jbd2_journal_stop(handle);
1390 return ret;
1395 * List management code snippets: various functions for manipulating the
1396 * transaction buffer lists.
1401 * Append a buffer to a transaction list, given the transaction's list head
1402 * pointer.
1404 * j_list_lock is held.
1406 * jbd_lock_bh_state(jh2bh(jh)) is held.
1409 static inline void
1410 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1412 if (!*list) {
1413 jh->b_tnext = jh->b_tprev = jh;
1414 *list = jh;
1415 } else {
1416 /* Insert at the tail of the list to preserve order */
1417 struct journal_head *first = *list, *last = first->b_tprev;
1418 jh->b_tprev = last;
1419 jh->b_tnext = first;
1420 last->b_tnext = first->b_tprev = jh;
1425 * Remove a buffer from a transaction list, given the transaction's list
1426 * head pointer.
1428 * Called with j_list_lock held, and the journal may not be locked.
1430 * jbd_lock_bh_state(jh2bh(jh)) is held.
1433 static inline void
1434 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1436 if (*list == jh) {
1437 *list = jh->b_tnext;
1438 if (*list == jh)
1439 *list = NULL;
1441 jh->b_tprev->b_tnext = jh->b_tnext;
1442 jh->b_tnext->b_tprev = jh->b_tprev;
1446 * Remove a buffer from the appropriate transaction list.
1448 * Note that this function can *change* the value of
1449 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1450 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1451 * of these pointers, it could go bad. Generally the caller needs to re-read
1452 * the pointer from the transaction_t.
1454 * Called under j_list_lock. The journal may not be locked.
1456 void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1458 struct journal_head **list = NULL;
1459 transaction_t *transaction;
1460 struct buffer_head *bh = jh2bh(jh);
1462 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1463 transaction = jh->b_transaction;
1464 if (transaction)
1465 assert_spin_locked(&transaction->t_journal->j_list_lock);
1467 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1468 if (jh->b_jlist != BJ_None)
1469 J_ASSERT_JH(jh, transaction != NULL);
1471 switch (jh->b_jlist) {
1472 case BJ_None:
1473 return;
1474 case BJ_Metadata:
1475 transaction->t_nr_buffers--;
1476 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1477 list = &transaction->t_buffers;
1478 break;
1479 case BJ_Forget:
1480 list = &transaction->t_forget;
1481 break;
1482 case BJ_IO:
1483 list = &transaction->t_iobuf_list;
1484 break;
1485 case BJ_Shadow:
1486 list = &transaction->t_shadow_list;
1487 break;
1488 case BJ_LogCtl:
1489 list = &transaction->t_log_list;
1490 break;
1491 case BJ_Reserved:
1492 list = &transaction->t_reserved_list;
1493 break;
1496 __blist_del_buffer(list, jh);
1497 jh->b_jlist = BJ_None;
1498 if (test_clear_buffer_jbddirty(bh))
1499 mark_buffer_dirty(bh); /* Expose it to the VM */
1502 void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1504 __jbd2_journal_temp_unlink_buffer(jh);
1505 jh->b_transaction = NULL;
1508 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1510 jbd_lock_bh_state(jh2bh(jh));
1511 spin_lock(&journal->j_list_lock);
1512 __jbd2_journal_unfile_buffer(jh);
1513 spin_unlock(&journal->j_list_lock);
1514 jbd_unlock_bh_state(jh2bh(jh));
1518 * Called from jbd2_journal_try_to_free_buffers().
1520 * Called under jbd_lock_bh_state(bh)
1522 static void
1523 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1525 struct journal_head *jh;
1527 jh = bh2jh(bh);
1529 if (buffer_locked(bh) || buffer_dirty(bh))
1530 goto out;
1532 if (jh->b_next_transaction != NULL)
1533 goto out;
1535 spin_lock(&journal->j_list_lock);
1536 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1537 /* written-back checkpointed metadata buffer */
1538 if (jh->b_jlist == BJ_None) {
1539 JBUFFER_TRACE(jh, "remove from checkpoint list");
1540 __jbd2_journal_remove_checkpoint(jh);
1541 jbd2_journal_remove_journal_head(bh);
1542 __brelse(bh);
1545 spin_unlock(&journal->j_list_lock);
1546 out:
1547 return;
1551 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1552 * @journal: journal for operation
1553 * @page: to try and free
1554 * @gfp_mask: we use the mask to detect how hard should we try to release
1555 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1556 * release the buffers.
1559 * For all the buffers on this page,
1560 * if they are fully written out ordered data, move them onto BUF_CLEAN
1561 * so try_to_free_buffers() can reap them.
1563 * This function returns non-zero if we wish try_to_free_buffers()
1564 * to be called. We do this if the page is releasable by try_to_free_buffers().
1565 * We also do it if the page has locked or dirty buffers and the caller wants
1566 * us to perform sync or async writeout.
1568 * This complicates JBD locking somewhat. We aren't protected by the
1569 * BKL here. We wish to remove the buffer from its committing or
1570 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1572 * This may *change* the value of transaction_t->t_datalist, so anyone
1573 * who looks at t_datalist needs to lock against this function.
1575 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1576 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1577 * will come out of the lock with the buffer dirty, which makes it
1578 * ineligible for release here.
1580 * Who else is affected by this? hmm... Really the only contender
1581 * is do_get_write_access() - it could be looking at the buffer while
1582 * journal_try_to_free_buffer() is changing its state. But that
1583 * cannot happen because we never reallocate freed data as metadata
1584 * while the data is part of a transaction. Yes?
1586 * Return 0 on failure, 1 on success
1588 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1589 struct page *page, gfp_t gfp_mask)
1591 struct buffer_head *head;
1592 struct buffer_head *bh;
1593 int ret = 0;
1595 J_ASSERT(PageLocked(page));
1597 head = page_buffers(page);
1598 bh = head;
1599 do {
1600 struct journal_head *jh;
1603 * We take our own ref against the journal_head here to avoid
1604 * having to add tons of locking around each instance of
1605 * jbd2_journal_remove_journal_head() and
1606 * jbd2_journal_put_journal_head().
1608 jh = jbd2_journal_grab_journal_head(bh);
1609 if (!jh)
1610 continue;
1612 jbd_lock_bh_state(bh);
1613 __journal_try_to_free_buffer(journal, bh);
1614 jbd2_journal_put_journal_head(jh);
1615 jbd_unlock_bh_state(bh);
1616 if (buffer_jbd(bh))
1617 goto busy;
1618 } while ((bh = bh->b_this_page) != head);
1620 ret = try_to_free_buffers(page);
1622 busy:
1623 return ret;
1627 * This buffer is no longer needed. If it is on an older transaction's
1628 * checkpoint list we need to record it on this transaction's forget list
1629 * to pin this buffer (and hence its checkpointing transaction) down until
1630 * this transaction commits. If the buffer isn't on a checkpoint list, we
1631 * release it.
1632 * Returns non-zero if JBD no longer has an interest in the buffer.
1634 * Called under j_list_lock.
1636 * Called under jbd_lock_bh_state(bh).
1638 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1640 int may_free = 1;
1641 struct buffer_head *bh = jh2bh(jh);
1643 __jbd2_journal_unfile_buffer(jh);
1645 if (jh->b_cp_transaction) {
1646 JBUFFER_TRACE(jh, "on running+cp transaction");
1647 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1648 clear_buffer_jbddirty(bh);
1649 may_free = 0;
1650 } else {
1651 JBUFFER_TRACE(jh, "on running transaction");
1652 jbd2_journal_remove_journal_head(bh);
1653 __brelse(bh);
1655 return may_free;
1659 * jbd2_journal_invalidatepage
1661 * This code is tricky. It has a number of cases to deal with.
1663 * There are two invariants which this code relies on:
1665 * i_size must be updated on disk before we start calling invalidatepage on the
1666 * data.
1668 * This is done in ext3 by defining an ext3_setattr method which
1669 * updates i_size before truncate gets going. By maintaining this
1670 * invariant, we can be sure that it is safe to throw away any buffers
1671 * attached to the current transaction: once the transaction commits,
1672 * we know that the data will not be needed.
1674 * Note however that we can *not* throw away data belonging to the
1675 * previous, committing transaction!
1677 * Any disk blocks which *are* part of the previous, committing
1678 * transaction (and which therefore cannot be discarded immediately) are
1679 * not going to be reused in the new running transaction
1681 * The bitmap committed_data images guarantee this: any block which is
1682 * allocated in one transaction and removed in the next will be marked
1683 * as in-use in the committed_data bitmap, so cannot be reused until
1684 * the next transaction to delete the block commits. This means that
1685 * leaving committing buffers dirty is quite safe: the disk blocks
1686 * cannot be reallocated to a different file and so buffer aliasing is
1687 * not possible.
1690 * The above applies mainly to ordered data mode. In writeback mode we
1691 * don't make guarantees about the order in which data hits disk --- in
1692 * particular we don't guarantee that new dirty data is flushed before
1693 * transaction commit --- so it is always safe just to discard data
1694 * immediately in that mode. --sct
1698 * The journal_unmap_buffer helper function returns zero if the buffer
1699 * concerned remains pinned as an anonymous buffer belonging to an older
1700 * transaction.
1702 * We're outside-transaction here. Either or both of j_running_transaction
1703 * and j_committing_transaction may be NULL.
1705 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1707 transaction_t *transaction;
1708 struct journal_head *jh;
1709 int may_free = 1;
1710 int ret;
1712 BUFFER_TRACE(bh, "entry");
1715 * It is safe to proceed here without the j_list_lock because the
1716 * buffers cannot be stolen by try_to_free_buffers as long as we are
1717 * holding the page lock. --sct
1720 if (!buffer_jbd(bh))
1721 goto zap_buffer_unlocked;
1723 /* OK, we have data buffer in journaled mode */
1724 spin_lock(&journal->j_state_lock);
1725 jbd_lock_bh_state(bh);
1726 spin_lock(&journal->j_list_lock);
1728 jh = jbd2_journal_grab_journal_head(bh);
1729 if (!jh)
1730 goto zap_buffer_no_jh;
1732 transaction = jh->b_transaction;
1733 if (transaction == NULL) {
1734 /* First case: not on any transaction. If it
1735 * has no checkpoint link, then we can zap it:
1736 * it's a writeback-mode buffer so we don't care
1737 * if it hits disk safely. */
1738 if (!jh->b_cp_transaction) {
1739 JBUFFER_TRACE(jh, "not on any transaction: zap");
1740 goto zap_buffer;
1743 if (!buffer_dirty(bh)) {
1744 /* bdflush has written it. We can drop it now */
1745 goto zap_buffer;
1748 /* OK, it must be in the journal but still not
1749 * written fully to disk: it's metadata or
1750 * journaled data... */
1752 if (journal->j_running_transaction) {
1753 /* ... and once the current transaction has
1754 * committed, the buffer won't be needed any
1755 * longer. */
1756 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1757 ret = __dispose_buffer(jh,
1758 journal->j_running_transaction);
1759 jbd2_journal_put_journal_head(jh);
1760 spin_unlock(&journal->j_list_lock);
1761 jbd_unlock_bh_state(bh);
1762 spin_unlock(&journal->j_state_lock);
1763 return ret;
1764 } else {
1765 /* There is no currently-running transaction. So the
1766 * orphan record which we wrote for this file must have
1767 * passed into commit. We must attach this buffer to
1768 * the committing transaction, if it exists. */
1769 if (journal->j_committing_transaction) {
1770 JBUFFER_TRACE(jh, "give to committing trans");
1771 ret = __dispose_buffer(jh,
1772 journal->j_committing_transaction);
1773 jbd2_journal_put_journal_head(jh);
1774 spin_unlock(&journal->j_list_lock);
1775 jbd_unlock_bh_state(bh);
1776 spin_unlock(&journal->j_state_lock);
1777 return ret;
1778 } else {
1779 /* The orphan record's transaction has
1780 * committed. We can cleanse this buffer */
1781 clear_buffer_jbddirty(bh);
1782 goto zap_buffer;
1785 } else if (transaction == journal->j_committing_transaction) {
1786 JBUFFER_TRACE(jh, "on committing transaction");
1788 * If it is committing, we simply cannot touch it. We
1789 * can remove it's next_transaction pointer from the
1790 * running transaction if that is set, but nothing
1791 * else. */
1792 set_buffer_freed(bh);
1793 if (jh->b_next_transaction) {
1794 J_ASSERT(jh->b_next_transaction ==
1795 journal->j_running_transaction);
1796 jh->b_next_transaction = NULL;
1798 jbd2_journal_put_journal_head(jh);
1799 spin_unlock(&journal->j_list_lock);
1800 jbd_unlock_bh_state(bh);
1801 spin_unlock(&journal->j_state_lock);
1802 return 0;
1803 } else {
1804 /* Good, the buffer belongs to the running transaction.
1805 * We are writing our own transaction's data, not any
1806 * previous one's, so it is safe to throw it away
1807 * (remember that we expect the filesystem to have set
1808 * i_size already for this truncate so recovery will not
1809 * expose the disk blocks we are discarding here.) */
1810 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1811 JBUFFER_TRACE(jh, "on running transaction");
1812 may_free = __dispose_buffer(jh, transaction);
1815 zap_buffer:
1816 jbd2_journal_put_journal_head(jh);
1817 zap_buffer_no_jh:
1818 spin_unlock(&journal->j_list_lock);
1819 jbd_unlock_bh_state(bh);
1820 spin_unlock(&journal->j_state_lock);
1821 zap_buffer_unlocked:
1822 clear_buffer_dirty(bh);
1823 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1824 clear_buffer_mapped(bh);
1825 clear_buffer_req(bh);
1826 clear_buffer_new(bh);
1827 bh->b_bdev = NULL;
1828 return may_free;
1832 * void jbd2_journal_invalidatepage()
1833 * @journal: journal to use for flush...
1834 * @page: page to flush
1835 * @offset: length of page to invalidate.
1837 * Reap page buffers containing data after offset in page.
1840 void jbd2_journal_invalidatepage(journal_t *journal,
1841 struct page *page,
1842 unsigned long offset)
1844 struct buffer_head *head, *bh, *next;
1845 unsigned int curr_off = 0;
1846 int may_free = 1;
1848 if (!PageLocked(page))
1849 BUG();
1850 if (!page_has_buffers(page))
1851 return;
1853 /* We will potentially be playing with lists other than just the
1854 * data lists (especially for journaled data mode), so be
1855 * cautious in our locking. */
1857 head = bh = page_buffers(page);
1858 do {
1859 unsigned int next_off = curr_off + bh->b_size;
1860 next = bh->b_this_page;
1862 if (offset <= curr_off) {
1863 /* This block is wholly outside the truncation point */
1864 lock_buffer(bh);
1865 may_free &= journal_unmap_buffer(journal, bh);
1866 unlock_buffer(bh);
1868 curr_off = next_off;
1869 bh = next;
1871 } while (bh != head);
1873 if (!offset) {
1874 if (may_free && try_to_free_buffers(page))
1875 J_ASSERT(!page_has_buffers(page));
1880 * File a buffer on the given transaction list.
1882 void __jbd2_journal_file_buffer(struct journal_head *jh,
1883 transaction_t *transaction, int jlist)
1885 struct journal_head **list = NULL;
1886 int was_dirty = 0;
1887 struct buffer_head *bh = jh2bh(jh);
1889 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1890 assert_spin_locked(&transaction->t_journal->j_list_lock);
1892 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1893 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1894 jh->b_transaction == NULL);
1896 if (jh->b_transaction && jh->b_jlist == jlist)
1897 return;
1899 /* The following list of buffer states needs to be consistent
1900 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
1901 * state. */
1903 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1904 jlist == BJ_Shadow || jlist == BJ_Forget) {
1905 if (test_clear_buffer_dirty(bh) ||
1906 test_clear_buffer_jbddirty(bh))
1907 was_dirty = 1;
1910 if (jh->b_transaction)
1911 __jbd2_journal_temp_unlink_buffer(jh);
1912 jh->b_transaction = transaction;
1914 switch (jlist) {
1915 case BJ_None:
1916 J_ASSERT_JH(jh, !jh->b_committed_data);
1917 J_ASSERT_JH(jh, !jh->b_frozen_data);
1918 return;
1919 case BJ_Metadata:
1920 transaction->t_nr_buffers++;
1921 list = &transaction->t_buffers;
1922 break;
1923 case BJ_Forget:
1924 list = &transaction->t_forget;
1925 break;
1926 case BJ_IO:
1927 list = &transaction->t_iobuf_list;
1928 break;
1929 case BJ_Shadow:
1930 list = &transaction->t_shadow_list;
1931 break;
1932 case BJ_LogCtl:
1933 list = &transaction->t_log_list;
1934 break;
1935 case BJ_Reserved:
1936 list = &transaction->t_reserved_list;
1937 break;
1940 __blist_add_buffer(list, jh);
1941 jh->b_jlist = jlist;
1943 if (was_dirty)
1944 set_buffer_jbddirty(bh);
1947 void jbd2_journal_file_buffer(struct journal_head *jh,
1948 transaction_t *transaction, int jlist)
1950 jbd_lock_bh_state(jh2bh(jh));
1951 spin_lock(&transaction->t_journal->j_list_lock);
1952 __jbd2_journal_file_buffer(jh, transaction, jlist);
1953 spin_unlock(&transaction->t_journal->j_list_lock);
1954 jbd_unlock_bh_state(jh2bh(jh));
1958 * Remove a buffer from its current buffer list in preparation for
1959 * dropping it from its current transaction entirely. If the buffer has
1960 * already started to be used by a subsequent transaction, refile the
1961 * buffer on that transaction's metadata list.
1963 * Called under journal->j_list_lock
1965 * Called under jbd_lock_bh_state(jh2bh(jh))
1967 void __jbd2_journal_refile_buffer(struct journal_head *jh)
1969 int was_dirty;
1970 struct buffer_head *bh = jh2bh(jh);
1972 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1973 if (jh->b_transaction)
1974 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
1976 /* If the buffer is now unused, just drop it. */
1977 if (jh->b_next_transaction == NULL) {
1978 __jbd2_journal_unfile_buffer(jh);
1979 return;
1983 * It has been modified by a later transaction: add it to the new
1984 * transaction's metadata list.
1987 was_dirty = test_clear_buffer_jbddirty(bh);
1988 __jbd2_journal_temp_unlink_buffer(jh);
1989 jh->b_transaction = jh->b_next_transaction;
1990 jh->b_next_transaction = NULL;
1991 __jbd2_journal_file_buffer(jh, jh->b_transaction,
1992 jh->b_modified ? BJ_Metadata : BJ_Reserved);
1993 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
1995 if (was_dirty)
1996 set_buffer_jbddirty(bh);
2000 * For the unlocked version of this call, also make sure that any
2001 * hanging journal_head is cleaned up if necessary.
2003 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2004 * operation on a buffer_head, in which the caller is probably going to
2005 * be hooking the journal_head onto other lists. In that case it is up
2006 * to the caller to remove the journal_head if necessary. For the
2007 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2008 * doing anything else to the buffer so we need to do the cleanup
2009 * ourselves to avoid a jh leak.
2011 * *** The journal_head may be freed by this call! ***
2013 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2015 struct buffer_head *bh = jh2bh(jh);
2017 jbd_lock_bh_state(bh);
2018 spin_lock(&journal->j_list_lock);
2020 __jbd2_journal_refile_buffer(jh);
2021 jbd_unlock_bh_state(bh);
2022 jbd2_journal_remove_journal_head(bh);
2024 spin_unlock(&journal->j_list_lock);
2025 __brelse(bh);
2029 * File inode in the inode list of the handle's transaction
2031 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2033 transaction_t *transaction = handle->h_transaction;
2034 journal_t *journal = transaction->t_journal;
2036 if (is_handle_aborted(handle))
2037 return -EIO;
2039 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2040 transaction->t_tid);
2043 * First check whether inode isn't already on the transaction's
2044 * lists without taking the lock. Note that this check is safe
2045 * without the lock as we cannot race with somebody removing inode
2046 * from the transaction. The reason is that we remove inode from the
2047 * transaction only in journal_release_jbd_inode() and when we commit
2048 * the transaction. We are guarded from the first case by holding
2049 * a reference to the inode. We are safe against the second case
2050 * because if jinode->i_transaction == transaction, commit code
2051 * cannot touch the transaction because we hold reference to it,
2052 * and if jinode->i_next_transaction == transaction, commit code
2053 * will only file the inode where we want it.
2055 if (jinode->i_transaction == transaction ||
2056 jinode->i_next_transaction == transaction)
2057 return 0;
2059 spin_lock(&journal->j_list_lock);
2061 if (jinode->i_transaction == transaction ||
2062 jinode->i_next_transaction == transaction)
2063 goto done;
2065 /* On some different transaction's list - should be
2066 * the committing one */
2067 if (jinode->i_transaction) {
2068 J_ASSERT(jinode->i_next_transaction == NULL);
2069 J_ASSERT(jinode->i_transaction ==
2070 journal->j_committing_transaction);
2071 jinode->i_next_transaction = transaction;
2072 goto done;
2074 /* Not on any transaction list... */
2075 J_ASSERT(!jinode->i_next_transaction);
2076 jinode->i_transaction = transaction;
2077 list_add(&jinode->i_list, &transaction->t_inode_list);
2078 done:
2079 spin_unlock(&journal->j_list_lock);
2081 return 0;
2085 * File truncate and transaction commit interact with each other in a
2086 * non-trivial way. If a transaction writing data block A is
2087 * committing, we cannot discard the data by truncate until we have
2088 * written them. Otherwise if we crashed after the transaction with
2089 * write has committed but before the transaction with truncate has
2090 * committed, we could see stale data in block A. This function is a
2091 * helper to solve this problem. It starts writeout of the truncated
2092 * part in case it is in the committing transaction.
2094 * Filesystem code must call this function when inode is journaled in
2095 * ordered mode before truncation happens and after the inode has been
2096 * placed on orphan list with the new inode size. The second condition
2097 * avoids the race that someone writes new data and we start
2098 * committing the transaction after this function has been called but
2099 * before a transaction for truncate is started (and furthermore it
2100 * allows us to optimize the case where the addition to orphan list
2101 * happens in the same transaction as write --- we don't have to write
2102 * any data in such case).
2104 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2105 struct jbd2_inode *jinode,
2106 loff_t new_size)
2108 transaction_t *inode_trans, *commit_trans;
2109 int ret = 0;
2111 /* This is a quick check to avoid locking if not necessary */
2112 if (!jinode->i_transaction)
2113 goto out;
2114 /* Locks are here just to force reading of recent values, it is
2115 * enough that the transaction was not committing before we started
2116 * a transaction adding the inode to orphan list */
2117 spin_lock(&journal->j_state_lock);
2118 commit_trans = journal->j_committing_transaction;
2119 spin_unlock(&journal->j_state_lock);
2120 spin_lock(&journal->j_list_lock);
2121 inode_trans = jinode->i_transaction;
2122 spin_unlock(&journal->j_list_lock);
2123 if (inode_trans == commit_trans) {
2124 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2125 new_size, LLONG_MAX);
2126 if (ret)
2127 jbd2_journal_abort(journal, ret);
2129 out:
2130 return ret;