Revert "kobject: don't block for each kobject_uevent".
[linux-2.6/mini2440.git] / fs / jbd2 / transaction.c
blob996ffda06bf306fde630e833f91332b745107689
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 * jbd2_journal_try_to_free_buffers() could race with
1552 * jbd2_journal_commit_transaction(). The later might still hold the
1553 * reference count to the buffers when inspecting them on
1554 * t_syncdata_list or t_locked_list.
1556 * jbd2_journal_try_to_free_buffers() will call this function to
1557 * wait for the current transaction to finish syncing data buffers, before
1558 * try to free that buffer.
1560 * Called with journal->j_state_lock hold.
1562 static void jbd2_journal_wait_for_transaction_sync_data(journal_t *journal)
1564 transaction_t *transaction;
1565 tid_t tid;
1567 spin_lock(&journal->j_state_lock);
1568 transaction = journal->j_committing_transaction;
1570 if (!transaction) {
1571 spin_unlock(&journal->j_state_lock);
1572 return;
1575 tid = transaction->t_tid;
1576 spin_unlock(&journal->j_state_lock);
1577 jbd2_log_wait_commit(journal, tid);
1581 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1582 * @journal: journal for operation
1583 * @page: to try and free
1584 * @gfp_mask: we use the mask to detect how hard should we try to release
1585 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1586 * release the buffers.
1589 * For all the buffers on this page,
1590 * if they are fully written out ordered data, move them onto BUF_CLEAN
1591 * so try_to_free_buffers() can reap them.
1593 * This function returns non-zero if we wish try_to_free_buffers()
1594 * to be called. We do this if the page is releasable by try_to_free_buffers().
1595 * We also do it if the page has locked or dirty buffers and the caller wants
1596 * us to perform sync or async writeout.
1598 * This complicates JBD locking somewhat. We aren't protected by the
1599 * BKL here. We wish to remove the buffer from its committing or
1600 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1602 * This may *change* the value of transaction_t->t_datalist, so anyone
1603 * who looks at t_datalist needs to lock against this function.
1605 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1606 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1607 * will come out of the lock with the buffer dirty, which makes it
1608 * ineligible for release here.
1610 * Who else is affected by this? hmm... Really the only contender
1611 * is do_get_write_access() - it could be looking at the buffer while
1612 * journal_try_to_free_buffer() is changing its state. But that
1613 * cannot happen because we never reallocate freed data as metadata
1614 * while the data is part of a transaction. Yes?
1616 * Return 0 on failure, 1 on success
1618 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1619 struct page *page, gfp_t gfp_mask)
1621 struct buffer_head *head;
1622 struct buffer_head *bh;
1623 int ret = 0;
1625 J_ASSERT(PageLocked(page));
1627 head = page_buffers(page);
1628 bh = head;
1629 do {
1630 struct journal_head *jh;
1633 * We take our own ref against the journal_head here to avoid
1634 * having to add tons of locking around each instance of
1635 * jbd2_journal_remove_journal_head() and
1636 * jbd2_journal_put_journal_head().
1638 jh = jbd2_journal_grab_journal_head(bh);
1639 if (!jh)
1640 continue;
1642 jbd_lock_bh_state(bh);
1643 __journal_try_to_free_buffer(journal, bh);
1644 jbd2_journal_put_journal_head(jh);
1645 jbd_unlock_bh_state(bh);
1646 if (buffer_jbd(bh))
1647 goto busy;
1648 } while ((bh = bh->b_this_page) != head);
1650 ret = try_to_free_buffers(page);
1653 * There are a number of places where jbd2_journal_try_to_free_buffers()
1654 * could race with jbd2_journal_commit_transaction(), the later still
1655 * holds the reference to the buffers to free while processing them.
1656 * try_to_free_buffers() failed to free those buffers. Some of the
1657 * caller of releasepage() request page buffers to be dropped, otherwise
1658 * treat the fail-to-free as errors (such as generic_file_direct_IO())
1660 * So, if the caller of try_to_release_page() wants the synchronous
1661 * behaviour(i.e make sure buffers are dropped upon return),
1662 * let's wait for the current transaction to finish flush of
1663 * dirty data buffers, then try to free those buffers again,
1664 * with the journal locked.
1666 if (ret == 0 && (gfp_mask & __GFP_WAIT) && (gfp_mask & __GFP_FS)) {
1667 jbd2_journal_wait_for_transaction_sync_data(journal);
1668 ret = try_to_free_buffers(page);
1671 busy:
1672 return ret;
1676 * This buffer is no longer needed. If it is on an older transaction's
1677 * checkpoint list we need to record it on this transaction's forget list
1678 * to pin this buffer (and hence its checkpointing transaction) down until
1679 * this transaction commits. If the buffer isn't on a checkpoint list, we
1680 * release it.
1681 * Returns non-zero if JBD no longer has an interest in the buffer.
1683 * Called under j_list_lock.
1685 * Called under jbd_lock_bh_state(bh).
1687 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1689 int may_free = 1;
1690 struct buffer_head *bh = jh2bh(jh);
1692 __jbd2_journal_unfile_buffer(jh);
1694 if (jh->b_cp_transaction) {
1695 JBUFFER_TRACE(jh, "on running+cp transaction");
1696 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1697 clear_buffer_jbddirty(bh);
1698 may_free = 0;
1699 } else {
1700 JBUFFER_TRACE(jh, "on running transaction");
1701 jbd2_journal_remove_journal_head(bh);
1702 __brelse(bh);
1704 return may_free;
1708 * jbd2_journal_invalidatepage
1710 * This code is tricky. It has a number of cases to deal with.
1712 * There are two invariants which this code relies on:
1714 * i_size must be updated on disk before we start calling invalidatepage on the
1715 * data.
1717 * This is done in ext3 by defining an ext3_setattr method which
1718 * updates i_size before truncate gets going. By maintaining this
1719 * invariant, we can be sure that it is safe to throw away any buffers
1720 * attached to the current transaction: once the transaction commits,
1721 * we know that the data will not be needed.
1723 * Note however that we can *not* throw away data belonging to the
1724 * previous, committing transaction!
1726 * Any disk blocks which *are* part of the previous, committing
1727 * transaction (and which therefore cannot be discarded immediately) are
1728 * not going to be reused in the new running transaction
1730 * The bitmap committed_data images guarantee this: any block which is
1731 * allocated in one transaction and removed in the next will be marked
1732 * as in-use in the committed_data bitmap, so cannot be reused until
1733 * the next transaction to delete the block commits. This means that
1734 * leaving committing buffers dirty is quite safe: the disk blocks
1735 * cannot be reallocated to a different file and so buffer aliasing is
1736 * not possible.
1739 * The above applies mainly to ordered data mode. In writeback mode we
1740 * don't make guarantees about the order in which data hits disk --- in
1741 * particular we don't guarantee that new dirty data is flushed before
1742 * transaction commit --- so it is always safe just to discard data
1743 * immediately in that mode. --sct
1747 * The journal_unmap_buffer helper function returns zero if the buffer
1748 * concerned remains pinned as an anonymous buffer belonging to an older
1749 * transaction.
1751 * We're outside-transaction here. Either or both of j_running_transaction
1752 * and j_committing_transaction may be NULL.
1754 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1756 transaction_t *transaction;
1757 struct journal_head *jh;
1758 int may_free = 1;
1759 int ret;
1761 BUFFER_TRACE(bh, "entry");
1764 * It is safe to proceed here without the j_list_lock because the
1765 * buffers cannot be stolen by try_to_free_buffers as long as we are
1766 * holding the page lock. --sct
1769 if (!buffer_jbd(bh))
1770 goto zap_buffer_unlocked;
1772 /* OK, we have data buffer in journaled mode */
1773 spin_lock(&journal->j_state_lock);
1774 jbd_lock_bh_state(bh);
1775 spin_lock(&journal->j_list_lock);
1777 jh = jbd2_journal_grab_journal_head(bh);
1778 if (!jh)
1779 goto zap_buffer_no_jh;
1781 transaction = jh->b_transaction;
1782 if (transaction == NULL) {
1783 /* First case: not on any transaction. If it
1784 * has no checkpoint link, then we can zap it:
1785 * it's a writeback-mode buffer so we don't care
1786 * if it hits disk safely. */
1787 if (!jh->b_cp_transaction) {
1788 JBUFFER_TRACE(jh, "not on any transaction: zap");
1789 goto zap_buffer;
1792 if (!buffer_dirty(bh)) {
1793 /* bdflush has written it. We can drop it now */
1794 goto zap_buffer;
1797 /* OK, it must be in the journal but still not
1798 * written fully to disk: it's metadata or
1799 * journaled data... */
1801 if (journal->j_running_transaction) {
1802 /* ... and once the current transaction has
1803 * committed, the buffer won't be needed any
1804 * longer. */
1805 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1806 ret = __dispose_buffer(jh,
1807 journal->j_running_transaction);
1808 jbd2_journal_put_journal_head(jh);
1809 spin_unlock(&journal->j_list_lock);
1810 jbd_unlock_bh_state(bh);
1811 spin_unlock(&journal->j_state_lock);
1812 return ret;
1813 } else {
1814 /* There is no currently-running transaction. So the
1815 * orphan record which we wrote for this file must have
1816 * passed into commit. We must attach this buffer to
1817 * the committing transaction, if it exists. */
1818 if (journal->j_committing_transaction) {
1819 JBUFFER_TRACE(jh, "give to committing trans");
1820 ret = __dispose_buffer(jh,
1821 journal->j_committing_transaction);
1822 jbd2_journal_put_journal_head(jh);
1823 spin_unlock(&journal->j_list_lock);
1824 jbd_unlock_bh_state(bh);
1825 spin_unlock(&journal->j_state_lock);
1826 return ret;
1827 } else {
1828 /* The orphan record's transaction has
1829 * committed. We can cleanse this buffer */
1830 clear_buffer_jbddirty(bh);
1831 goto zap_buffer;
1834 } else if (transaction == journal->j_committing_transaction) {
1835 JBUFFER_TRACE(jh, "on committing transaction");
1837 * If it is committing, we simply cannot touch it. We
1838 * can remove it's next_transaction pointer from the
1839 * running transaction if that is set, but nothing
1840 * else. */
1841 set_buffer_freed(bh);
1842 if (jh->b_next_transaction) {
1843 J_ASSERT(jh->b_next_transaction ==
1844 journal->j_running_transaction);
1845 jh->b_next_transaction = NULL;
1847 jbd2_journal_put_journal_head(jh);
1848 spin_unlock(&journal->j_list_lock);
1849 jbd_unlock_bh_state(bh);
1850 spin_unlock(&journal->j_state_lock);
1851 return 0;
1852 } else {
1853 /* Good, the buffer belongs to the running transaction.
1854 * We are writing our own transaction's data, not any
1855 * previous one's, so it is safe to throw it away
1856 * (remember that we expect the filesystem to have set
1857 * i_size already for this truncate so recovery will not
1858 * expose the disk blocks we are discarding here.) */
1859 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1860 JBUFFER_TRACE(jh, "on running transaction");
1861 may_free = __dispose_buffer(jh, transaction);
1864 zap_buffer:
1865 jbd2_journal_put_journal_head(jh);
1866 zap_buffer_no_jh:
1867 spin_unlock(&journal->j_list_lock);
1868 jbd_unlock_bh_state(bh);
1869 spin_unlock(&journal->j_state_lock);
1870 zap_buffer_unlocked:
1871 clear_buffer_dirty(bh);
1872 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1873 clear_buffer_mapped(bh);
1874 clear_buffer_req(bh);
1875 clear_buffer_new(bh);
1876 bh->b_bdev = NULL;
1877 return may_free;
1881 * void jbd2_journal_invalidatepage()
1882 * @journal: journal to use for flush...
1883 * @page: page to flush
1884 * @offset: length of page to invalidate.
1886 * Reap page buffers containing data after offset in page.
1889 void jbd2_journal_invalidatepage(journal_t *journal,
1890 struct page *page,
1891 unsigned long offset)
1893 struct buffer_head *head, *bh, *next;
1894 unsigned int curr_off = 0;
1895 int may_free = 1;
1897 if (!PageLocked(page))
1898 BUG();
1899 if (!page_has_buffers(page))
1900 return;
1902 /* We will potentially be playing with lists other than just the
1903 * data lists (especially for journaled data mode), so be
1904 * cautious in our locking. */
1906 head = bh = page_buffers(page);
1907 do {
1908 unsigned int next_off = curr_off + bh->b_size;
1909 next = bh->b_this_page;
1911 if (offset <= curr_off) {
1912 /* This block is wholly outside the truncation point */
1913 lock_buffer(bh);
1914 may_free &= journal_unmap_buffer(journal, bh);
1915 unlock_buffer(bh);
1917 curr_off = next_off;
1918 bh = next;
1920 } while (bh != head);
1922 if (!offset) {
1923 if (may_free && try_to_free_buffers(page))
1924 J_ASSERT(!page_has_buffers(page));
1929 * File a buffer on the given transaction list.
1931 void __jbd2_journal_file_buffer(struct journal_head *jh,
1932 transaction_t *transaction, int jlist)
1934 struct journal_head **list = NULL;
1935 int was_dirty = 0;
1936 struct buffer_head *bh = jh2bh(jh);
1938 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1939 assert_spin_locked(&transaction->t_journal->j_list_lock);
1941 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1942 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1943 jh->b_transaction == NULL);
1945 if (jh->b_transaction && jh->b_jlist == jlist)
1946 return;
1948 /* The following list of buffer states needs to be consistent
1949 * with __jbd_unexpected_dirty_buffer()'s handling of dirty
1950 * state. */
1952 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1953 jlist == BJ_Shadow || jlist == BJ_Forget) {
1954 if (test_clear_buffer_dirty(bh) ||
1955 test_clear_buffer_jbddirty(bh))
1956 was_dirty = 1;
1959 if (jh->b_transaction)
1960 __jbd2_journal_temp_unlink_buffer(jh);
1961 jh->b_transaction = transaction;
1963 switch (jlist) {
1964 case BJ_None:
1965 J_ASSERT_JH(jh, !jh->b_committed_data);
1966 J_ASSERT_JH(jh, !jh->b_frozen_data);
1967 return;
1968 case BJ_Metadata:
1969 transaction->t_nr_buffers++;
1970 list = &transaction->t_buffers;
1971 break;
1972 case BJ_Forget:
1973 list = &transaction->t_forget;
1974 break;
1975 case BJ_IO:
1976 list = &transaction->t_iobuf_list;
1977 break;
1978 case BJ_Shadow:
1979 list = &transaction->t_shadow_list;
1980 break;
1981 case BJ_LogCtl:
1982 list = &transaction->t_log_list;
1983 break;
1984 case BJ_Reserved:
1985 list = &transaction->t_reserved_list;
1986 break;
1989 __blist_add_buffer(list, jh);
1990 jh->b_jlist = jlist;
1992 if (was_dirty)
1993 set_buffer_jbddirty(bh);
1996 void jbd2_journal_file_buffer(struct journal_head *jh,
1997 transaction_t *transaction, int jlist)
1999 jbd_lock_bh_state(jh2bh(jh));
2000 spin_lock(&transaction->t_journal->j_list_lock);
2001 __jbd2_journal_file_buffer(jh, transaction, jlist);
2002 spin_unlock(&transaction->t_journal->j_list_lock);
2003 jbd_unlock_bh_state(jh2bh(jh));
2007 * Remove a buffer from its current buffer list in preparation for
2008 * dropping it from its current transaction entirely. If the buffer has
2009 * already started to be used by a subsequent transaction, refile the
2010 * buffer on that transaction's metadata list.
2012 * Called under journal->j_list_lock
2014 * Called under jbd_lock_bh_state(jh2bh(jh))
2016 void __jbd2_journal_refile_buffer(struct journal_head *jh)
2018 int was_dirty;
2019 struct buffer_head *bh = jh2bh(jh);
2021 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2022 if (jh->b_transaction)
2023 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2025 /* If the buffer is now unused, just drop it. */
2026 if (jh->b_next_transaction == NULL) {
2027 __jbd2_journal_unfile_buffer(jh);
2028 return;
2032 * It has been modified by a later transaction: add it to the new
2033 * transaction's metadata list.
2036 was_dirty = test_clear_buffer_jbddirty(bh);
2037 __jbd2_journal_temp_unlink_buffer(jh);
2038 jh->b_transaction = jh->b_next_transaction;
2039 jh->b_next_transaction = NULL;
2040 __jbd2_journal_file_buffer(jh, jh->b_transaction,
2041 jh->b_modified ? BJ_Metadata : BJ_Reserved);
2042 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2044 if (was_dirty)
2045 set_buffer_jbddirty(bh);
2049 * For the unlocked version of this call, also make sure that any
2050 * hanging journal_head is cleaned up if necessary.
2052 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2053 * operation on a buffer_head, in which the caller is probably going to
2054 * be hooking the journal_head onto other lists. In that case it is up
2055 * to the caller to remove the journal_head if necessary. For the
2056 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2057 * doing anything else to the buffer so we need to do the cleanup
2058 * ourselves to avoid a jh leak.
2060 * *** The journal_head may be freed by this call! ***
2062 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2064 struct buffer_head *bh = jh2bh(jh);
2066 jbd_lock_bh_state(bh);
2067 spin_lock(&journal->j_list_lock);
2069 __jbd2_journal_refile_buffer(jh);
2070 jbd_unlock_bh_state(bh);
2071 jbd2_journal_remove_journal_head(bh);
2073 spin_unlock(&journal->j_list_lock);
2074 __brelse(bh);
2078 * File inode in the inode list of the handle's transaction
2080 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2082 transaction_t *transaction = handle->h_transaction;
2083 journal_t *journal = transaction->t_journal;
2085 if (is_handle_aborted(handle))
2086 return -EIO;
2088 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2089 transaction->t_tid);
2092 * First check whether inode isn't already on the transaction's
2093 * lists without taking the lock. Note that this check is safe
2094 * without the lock as we cannot race with somebody removing inode
2095 * from the transaction. The reason is that we remove inode from the
2096 * transaction only in journal_release_jbd_inode() and when we commit
2097 * the transaction. We are guarded from the first case by holding
2098 * a reference to the inode. We are safe against the second case
2099 * because if jinode->i_transaction == transaction, commit code
2100 * cannot touch the transaction because we hold reference to it,
2101 * and if jinode->i_next_transaction == transaction, commit code
2102 * will only file the inode where we want it.
2104 if (jinode->i_transaction == transaction ||
2105 jinode->i_next_transaction == transaction)
2106 return 0;
2108 spin_lock(&journal->j_list_lock);
2110 if (jinode->i_transaction == transaction ||
2111 jinode->i_next_transaction == transaction)
2112 goto done;
2114 /* On some different transaction's list - should be
2115 * the committing one */
2116 if (jinode->i_transaction) {
2117 J_ASSERT(jinode->i_next_transaction == NULL);
2118 J_ASSERT(jinode->i_transaction ==
2119 journal->j_committing_transaction);
2120 jinode->i_next_transaction = transaction;
2121 goto done;
2123 /* Not on any transaction list... */
2124 J_ASSERT(!jinode->i_next_transaction);
2125 jinode->i_transaction = transaction;
2126 list_add(&jinode->i_list, &transaction->t_inode_list);
2127 done:
2128 spin_unlock(&journal->j_list_lock);
2130 return 0;
2134 * File truncate and transaction commit interact with each other in a
2135 * non-trivial way. If a transaction writing data block A is
2136 * committing, we cannot discard the data by truncate until we have
2137 * written them. Otherwise if we crashed after the transaction with
2138 * write has committed but before the transaction with truncate has
2139 * committed, we could see stale data in block A. This function is a
2140 * helper to solve this problem. It starts writeout of the truncated
2141 * part in case it is in the committing transaction.
2143 * Filesystem code must call this function when inode is journaled in
2144 * ordered mode before truncation happens and after the inode has been
2145 * placed on orphan list with the new inode size. The second condition
2146 * avoids the race that someone writes new data and we start
2147 * committing the transaction after this function has been called but
2148 * before a transaction for truncate is started (and furthermore it
2149 * allows us to optimize the case where the addition to orphan list
2150 * happens in the same transaction as write --- we don't have to write
2151 * any data in such case).
2153 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2154 struct jbd2_inode *jinode,
2155 loff_t new_size)
2157 transaction_t *inode_trans, *commit_trans;
2158 int ret = 0;
2160 /* This is a quick check to avoid locking if not necessary */
2161 if (!jinode->i_transaction)
2162 goto out;
2163 /* Locks are here just to force reading of recent values, it is
2164 * enough that the transaction was not committing before we started
2165 * a transaction adding the inode to orphan list */
2166 spin_lock(&journal->j_state_lock);
2167 commit_trans = journal->j_committing_transaction;
2168 spin_unlock(&journal->j_state_lock);
2169 spin_lock(&journal->j_list_lock);
2170 inode_trans = jinode->i_transaction;
2171 spin_unlock(&journal->j_list_lock);
2172 if (inode_trans == commit_trans) {
2173 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2174 new_size, LLONG_MAX);
2175 if (ret)
2176 jbd2_journal_abort(journal, ret);
2178 out:
2179 return ret;