sky2: Set SKY2_HW_RAM_BUFFER in sky2_init
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd2 / transaction.c
blob6213ac728f303286e15a2b6703fc90d69e2bd6ba
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);
502 static void warn_dirty_buffer(struct buffer_head *bh)
504 char b[BDEVNAME_SIZE];
506 printk(KERN_WARNING
507 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
508 "There's a risk of filesystem corruption in case of system "
509 "crash.\n",
510 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
514 * If the buffer is already part of the current transaction, then there
515 * is nothing we need to do. If it is already part of a prior
516 * transaction which we are still committing to disk, then we need to
517 * make sure that we do not overwrite the old copy: we do copy-out to
518 * preserve the copy going to disk. We also account the buffer against
519 * the handle's metadata buffer credits (unless the buffer is already
520 * part of the transaction, that is).
523 static int
524 do_get_write_access(handle_t *handle, struct journal_head *jh,
525 int force_copy)
527 struct buffer_head *bh;
528 transaction_t *transaction;
529 journal_t *journal;
530 int error;
531 char *frozen_buffer = NULL;
532 int need_copy = 0;
534 if (is_handle_aborted(handle))
535 return -EROFS;
537 transaction = handle->h_transaction;
538 journal = transaction->t_journal;
540 jbd_debug(5, "buffer_head %p, force_copy %d\n", jh, force_copy);
542 JBUFFER_TRACE(jh, "entry");
543 repeat:
544 bh = jh2bh(jh);
546 /* @@@ Need to check for errors here at some point. */
548 lock_buffer(bh);
549 jbd_lock_bh_state(bh);
551 /* We now hold the buffer lock so it is safe to query the buffer
552 * state. Is the buffer dirty?
554 * If so, there are two possibilities. The buffer may be
555 * non-journaled, and undergoing a quite legitimate writeback.
556 * Otherwise, it is journaled, and we don't expect dirty buffers
557 * in that state (the buffers should be marked JBD_Dirty
558 * instead.) So either the IO is being done under our own
559 * control and this is a bug, or it's a third party IO such as
560 * dump(8) (which may leave the buffer scheduled for read ---
561 * ie. locked but not dirty) or tune2fs (which may actually have
562 * the buffer dirtied, ugh.) */
564 if (buffer_dirty(bh)) {
566 * First question: is this buffer already part of the current
567 * transaction or the existing committing transaction?
569 if (jh->b_transaction) {
570 J_ASSERT_JH(jh,
571 jh->b_transaction == transaction ||
572 jh->b_transaction ==
573 journal->j_committing_transaction);
574 if (jh->b_next_transaction)
575 J_ASSERT_JH(jh, jh->b_next_transaction ==
576 transaction);
577 warn_dirty_buffer(bh);
580 * In any case we need to clean the dirty flag and we must
581 * do it under the buffer lock to be sure we don't race
582 * with running write-out.
584 JBUFFER_TRACE(jh, "Journalling dirty buffer");
585 clear_buffer_dirty(bh);
586 set_buffer_jbddirty(bh);
589 unlock_buffer(bh);
591 error = -EROFS;
592 if (is_handle_aborted(handle)) {
593 jbd_unlock_bh_state(bh);
594 goto out;
596 error = 0;
599 * The buffer is already part of this transaction if b_transaction or
600 * b_next_transaction points to it
602 if (jh->b_transaction == transaction ||
603 jh->b_next_transaction == transaction)
604 goto done;
607 * this is the first time this transaction is touching this buffer,
608 * reset the modified flag
610 jh->b_modified = 0;
613 * If there is already a copy-out version of this buffer, then we don't
614 * need to make another one
616 if (jh->b_frozen_data) {
617 JBUFFER_TRACE(jh, "has frozen data");
618 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
619 jh->b_next_transaction = transaction;
620 goto done;
623 /* Is there data here we need to preserve? */
625 if (jh->b_transaction && jh->b_transaction != transaction) {
626 JBUFFER_TRACE(jh, "owned by older transaction");
627 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
628 J_ASSERT_JH(jh, jh->b_transaction ==
629 journal->j_committing_transaction);
631 /* There is one case we have to be very careful about.
632 * If the committing transaction is currently writing
633 * this buffer out to disk and has NOT made a copy-out,
634 * then we cannot modify the buffer contents at all
635 * right now. The essence of copy-out is that it is the
636 * extra copy, not the primary copy, which gets
637 * journaled. If the primary copy is already going to
638 * disk then we cannot do copy-out here. */
640 if (jh->b_jlist == BJ_Shadow) {
641 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
642 wait_queue_head_t *wqh;
644 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
646 JBUFFER_TRACE(jh, "on shadow: sleep");
647 jbd_unlock_bh_state(bh);
648 /* commit wakes up all shadow buffers after IO */
649 for ( ; ; ) {
650 prepare_to_wait(wqh, &wait.wait,
651 TASK_UNINTERRUPTIBLE);
652 if (jh->b_jlist != BJ_Shadow)
653 break;
654 schedule();
656 finish_wait(wqh, &wait.wait);
657 goto repeat;
660 /* Only do the copy if the currently-owning transaction
661 * still needs it. If it is on the Forget list, the
662 * committing transaction is past that stage. The
663 * buffer had better remain locked during the kmalloc,
664 * but that should be true --- we hold the journal lock
665 * still and the buffer is already on the BUF_JOURNAL
666 * list so won't be flushed.
668 * Subtle point, though: if this is a get_undo_access,
669 * then we will be relying on the frozen_data to contain
670 * the new value of the committed_data record after the
671 * transaction, so we HAVE to force the frozen_data copy
672 * in that case. */
674 if (jh->b_jlist != BJ_Forget || force_copy) {
675 JBUFFER_TRACE(jh, "generate frozen data");
676 if (!frozen_buffer) {
677 JBUFFER_TRACE(jh, "allocate memory for buffer");
678 jbd_unlock_bh_state(bh);
679 frozen_buffer =
680 jbd2_alloc(jh2bh(jh)->b_size,
681 GFP_NOFS);
682 if (!frozen_buffer) {
683 printk(KERN_EMERG
684 "%s: OOM for frozen_buffer\n",
685 __func__);
686 JBUFFER_TRACE(jh, "oom!");
687 error = -ENOMEM;
688 jbd_lock_bh_state(bh);
689 goto done;
691 goto repeat;
693 jh->b_frozen_data = frozen_buffer;
694 frozen_buffer = NULL;
695 need_copy = 1;
697 jh->b_next_transaction = transaction;
702 * Finally, if the buffer is not journaled right now, we need to make
703 * sure it doesn't get written to disk before the caller actually
704 * commits the new data
706 if (!jh->b_transaction) {
707 JBUFFER_TRACE(jh, "no transaction");
708 J_ASSERT_JH(jh, !jh->b_next_transaction);
709 jh->b_transaction = transaction;
710 JBUFFER_TRACE(jh, "file as BJ_Reserved");
711 spin_lock(&journal->j_list_lock);
712 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
713 spin_unlock(&journal->j_list_lock);
716 done:
717 if (need_copy) {
718 struct page *page;
719 int offset;
720 char *source;
722 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
723 "Possible IO failure.\n");
724 page = jh2bh(jh)->b_page;
725 offset = ((unsigned long) jh2bh(jh)->b_data) & ~PAGE_MASK;
726 source = kmap_atomic(page, KM_USER0);
727 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
728 kunmap_atomic(source, KM_USER0);
731 * Now that the frozen data is saved off, we need to store
732 * any matching triggers.
734 jh->b_frozen_triggers = jh->b_triggers;
736 jbd_unlock_bh_state(bh);
739 * If we are about to journal a buffer, then any revoke pending on it is
740 * no longer valid
742 jbd2_journal_cancel_revoke(handle, jh);
744 out:
745 if (unlikely(frozen_buffer)) /* It's usually NULL */
746 jbd2_free(frozen_buffer, bh->b_size);
748 JBUFFER_TRACE(jh, "exit");
749 return error;
753 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
754 * @handle: transaction to add buffer modifications to
755 * @bh: bh to be used for metadata writes
756 * @credits: variable that will receive credits for the buffer
758 * Returns an error code or 0 on success.
760 * In full data journalling mode the buffer may be of type BJ_AsyncData,
761 * because we're write()ing a buffer which is also part of a shared mapping.
764 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
766 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
767 int rc;
769 /* We do not want to get caught playing with fields which the
770 * log thread also manipulates. Make sure that the buffer
771 * completes any outstanding IO before proceeding. */
772 rc = do_get_write_access(handle, jh, 0);
773 jbd2_journal_put_journal_head(jh);
774 return rc;
779 * When the user wants to journal a newly created buffer_head
780 * (ie. getblk() returned a new buffer and we are going to populate it
781 * manually rather than reading off disk), then we need to keep the
782 * buffer_head locked until it has been completely filled with new
783 * data. In this case, we should be able to make the assertion that
784 * the bh is not already part of an existing transaction.
786 * The buffer should already be locked by the caller by this point.
787 * There is no lock ranking violation: it was a newly created,
788 * unlocked buffer beforehand. */
791 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
792 * @handle: transaction to new buffer to
793 * @bh: new buffer.
795 * Call this if you create a new bh.
797 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
799 transaction_t *transaction = handle->h_transaction;
800 journal_t *journal = transaction->t_journal;
801 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
802 int err;
804 jbd_debug(5, "journal_head %p\n", jh);
805 err = -EROFS;
806 if (is_handle_aborted(handle))
807 goto out;
808 err = 0;
810 JBUFFER_TRACE(jh, "entry");
812 * The buffer may already belong to this transaction due to pre-zeroing
813 * in the filesystem's new_block code. It may also be on the previous,
814 * committing transaction's lists, but it HAS to be in Forget state in
815 * that case: the transaction must have deleted the buffer for it to be
816 * reused here.
818 jbd_lock_bh_state(bh);
819 spin_lock(&journal->j_list_lock);
820 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
821 jh->b_transaction == NULL ||
822 (jh->b_transaction == journal->j_committing_transaction &&
823 jh->b_jlist == BJ_Forget)));
825 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
826 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
828 if (jh->b_transaction == NULL) {
830 * Previous jbd2_journal_forget() could have left the buffer
831 * with jbddirty bit set because it was being committed. When
832 * the commit finished, we've filed the buffer for
833 * checkpointing and marked it dirty. Now we are reallocating
834 * the buffer so the transaction freeing it must have
835 * committed and so it's safe to clear the dirty bit.
837 clear_buffer_dirty(jh2bh(jh));
838 jh->b_transaction = transaction;
840 /* first access by this transaction */
841 jh->b_modified = 0;
843 JBUFFER_TRACE(jh, "file as BJ_Reserved");
844 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
845 } else if (jh->b_transaction == journal->j_committing_transaction) {
846 /* first access by this transaction */
847 jh->b_modified = 0;
849 JBUFFER_TRACE(jh, "set next transaction");
850 jh->b_next_transaction = transaction;
852 spin_unlock(&journal->j_list_lock);
853 jbd_unlock_bh_state(bh);
856 * akpm: I added this. ext3_alloc_branch can pick up new indirect
857 * blocks which contain freed but then revoked metadata. We need
858 * to cancel the revoke in case we end up freeing it yet again
859 * and the reallocating as data - this would cause a second revoke,
860 * which hits an assertion error.
862 JBUFFER_TRACE(jh, "cancelling revoke");
863 jbd2_journal_cancel_revoke(handle, jh);
864 jbd2_journal_put_journal_head(jh);
865 out:
866 return err;
870 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
871 * non-rewindable consequences
872 * @handle: transaction
873 * @bh: buffer to undo
874 * @credits: store the number of taken credits here (if not NULL)
876 * Sometimes there is a need to distinguish between metadata which has
877 * been committed to disk and that which has not. The ext3fs code uses
878 * this for freeing and allocating space, we have to make sure that we
879 * do not reuse freed space until the deallocation has been committed,
880 * since if we overwrote that space we would make the delete
881 * un-rewindable in case of a crash.
883 * To deal with that, jbd2_journal_get_undo_access requests write access to a
884 * buffer for parts of non-rewindable operations such as delete
885 * operations on the bitmaps. The journaling code must keep a copy of
886 * the buffer's contents prior to the undo_access call until such time
887 * as we know that the buffer has definitely been committed to disk.
889 * We never need to know which transaction the committed data is part
890 * of, buffers touched here are guaranteed to be dirtied later and so
891 * will be committed to a new transaction in due course, at which point
892 * we can discard the old committed data pointer.
894 * Returns error number or 0 on success.
896 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
898 int err;
899 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
900 char *committed_data = NULL;
902 JBUFFER_TRACE(jh, "entry");
905 * Do this first --- it can drop the journal lock, so we want to
906 * make sure that obtaining the committed_data is done
907 * atomically wrt. completion of any outstanding commits.
909 err = do_get_write_access(handle, jh, 1);
910 if (err)
911 goto out;
913 repeat:
914 if (!jh->b_committed_data) {
915 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
916 if (!committed_data) {
917 printk(KERN_EMERG "%s: No memory for committed data\n",
918 __func__);
919 err = -ENOMEM;
920 goto out;
924 jbd_lock_bh_state(bh);
925 if (!jh->b_committed_data) {
926 /* Copy out the current buffer contents into the
927 * preserved, committed copy. */
928 JBUFFER_TRACE(jh, "generate b_committed data");
929 if (!committed_data) {
930 jbd_unlock_bh_state(bh);
931 goto repeat;
934 jh->b_committed_data = committed_data;
935 committed_data = NULL;
936 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
938 jbd_unlock_bh_state(bh);
939 out:
940 jbd2_journal_put_journal_head(jh);
941 if (unlikely(committed_data))
942 jbd2_free(committed_data, bh->b_size);
943 return err;
947 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
948 * @bh: buffer to trigger on
949 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
951 * Set any triggers on this journal_head. This is always safe, because
952 * triggers for a committing buffer will be saved off, and triggers for
953 * a running transaction will match the buffer in that transaction.
955 * Call with NULL to clear the triggers.
957 void jbd2_journal_set_triggers(struct buffer_head *bh,
958 struct jbd2_buffer_trigger_type *type)
960 struct journal_head *jh = bh2jh(bh);
962 jh->b_triggers = type;
965 void jbd2_buffer_commit_trigger(struct journal_head *jh, void *mapped_data,
966 struct jbd2_buffer_trigger_type *triggers)
968 struct buffer_head *bh = jh2bh(jh);
970 if (!triggers || !triggers->t_commit)
971 return;
973 triggers->t_commit(triggers, bh, mapped_data, bh->b_size);
976 void jbd2_buffer_abort_trigger(struct journal_head *jh,
977 struct jbd2_buffer_trigger_type *triggers)
979 if (!triggers || !triggers->t_abort)
980 return;
982 triggers->t_abort(triggers, jh2bh(jh));
988 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
989 * @handle: transaction to add buffer to.
990 * @bh: buffer to mark
992 * mark dirty metadata which needs to be journaled as part of the current
993 * transaction.
995 * The buffer is placed on the transaction's metadata list and is marked
996 * as belonging to the transaction.
998 * Returns error number or 0 on success.
1000 * Special care needs to be taken if the buffer already belongs to the
1001 * current committing transaction (in which case we should have frozen
1002 * data present for that commit). In that case, we don't relink the
1003 * buffer: that only gets done when the old transaction finally
1004 * completes its commit.
1006 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1008 transaction_t *transaction = handle->h_transaction;
1009 journal_t *journal = transaction->t_journal;
1010 struct journal_head *jh = bh2jh(bh);
1012 jbd_debug(5, "journal_head %p\n", jh);
1013 JBUFFER_TRACE(jh, "entry");
1014 if (is_handle_aborted(handle))
1015 goto out;
1017 jbd_lock_bh_state(bh);
1019 if (jh->b_modified == 0) {
1021 * This buffer's got modified and becoming part
1022 * of the transaction. This needs to be done
1023 * once a transaction -bzzz
1025 jh->b_modified = 1;
1026 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1027 handle->h_buffer_credits--;
1031 * fastpath, to avoid expensive locking. If this buffer is already
1032 * on the running transaction's metadata list there is nothing to do.
1033 * Nobody can take it off again because there is a handle open.
1034 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1035 * result in this test being false, so we go in and take the locks.
1037 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1038 JBUFFER_TRACE(jh, "fastpath");
1039 J_ASSERT_JH(jh, jh->b_transaction ==
1040 journal->j_running_transaction);
1041 goto out_unlock_bh;
1044 set_buffer_jbddirty(bh);
1047 * Metadata already on the current transaction list doesn't
1048 * need to be filed. Metadata on another transaction's list must
1049 * be committing, and will be refiled once the commit completes:
1050 * leave it alone for now.
1052 if (jh->b_transaction != transaction) {
1053 JBUFFER_TRACE(jh, "already on other transaction");
1054 J_ASSERT_JH(jh, jh->b_transaction ==
1055 journal->j_committing_transaction);
1056 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1057 /* And this case is illegal: we can't reuse another
1058 * transaction's data buffer, ever. */
1059 goto out_unlock_bh;
1062 /* That test should have eliminated the following case: */
1063 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1065 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1066 spin_lock(&journal->j_list_lock);
1067 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1068 spin_unlock(&journal->j_list_lock);
1069 out_unlock_bh:
1070 jbd_unlock_bh_state(bh);
1071 out:
1072 JBUFFER_TRACE(jh, "exit");
1073 return 0;
1077 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
1078 * updates, if the update decided in the end that it didn't need access.
1081 void
1082 jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1084 BUFFER_TRACE(bh, "entry");
1088 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1089 * @handle: transaction handle
1090 * @bh: bh to 'forget'
1092 * We can only do the bforget if there are no commits pending against the
1093 * buffer. If the buffer is dirty in the current running transaction we
1094 * can safely unlink it.
1096 * bh may not be a journalled buffer at all - it may be a non-JBD
1097 * buffer which came off the hashtable. Check for this.
1099 * Decrements bh->b_count by one.
1101 * Allow this call even if the handle has aborted --- it may be part of
1102 * the caller's cleanup after an abort.
1104 int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1106 transaction_t *transaction = handle->h_transaction;
1107 journal_t *journal = transaction->t_journal;
1108 struct journal_head *jh;
1109 int drop_reserve = 0;
1110 int err = 0;
1111 int was_modified = 0;
1113 BUFFER_TRACE(bh, "entry");
1115 jbd_lock_bh_state(bh);
1116 spin_lock(&journal->j_list_lock);
1118 if (!buffer_jbd(bh))
1119 goto not_jbd;
1120 jh = bh2jh(bh);
1122 /* Critical error: attempting to delete a bitmap buffer, maybe?
1123 * Don't do any jbd operations, and return an error. */
1124 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1125 "inconsistent data on disk")) {
1126 err = -EIO;
1127 goto not_jbd;
1130 /* keep track of wether or not this transaction modified us */
1131 was_modified = jh->b_modified;
1134 * The buffer's going from the transaction, we must drop
1135 * all references -bzzz
1137 jh->b_modified = 0;
1139 if (jh->b_transaction == handle->h_transaction) {
1140 J_ASSERT_JH(jh, !jh->b_frozen_data);
1142 /* If we are forgetting a buffer which is already part
1143 * of this transaction, then we can just drop it from
1144 * the transaction immediately. */
1145 clear_buffer_dirty(bh);
1146 clear_buffer_jbddirty(bh);
1148 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1151 * we only want to drop a reference if this transaction
1152 * modified the buffer
1154 if (was_modified)
1155 drop_reserve = 1;
1158 * We are no longer going to journal this buffer.
1159 * However, the commit of this transaction is still
1160 * important to the buffer: the delete that we are now
1161 * processing might obsolete an old log entry, so by
1162 * committing, we can satisfy the buffer's checkpoint.
1164 * So, if we have a checkpoint on the buffer, we should
1165 * now refile the buffer on our BJ_Forget list so that
1166 * we know to remove the checkpoint after we commit.
1169 if (jh->b_cp_transaction) {
1170 __jbd2_journal_temp_unlink_buffer(jh);
1171 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1172 } else {
1173 __jbd2_journal_unfile_buffer(jh);
1174 jbd2_journal_remove_journal_head(bh);
1175 __brelse(bh);
1176 if (!buffer_jbd(bh)) {
1177 spin_unlock(&journal->j_list_lock);
1178 jbd_unlock_bh_state(bh);
1179 __bforget(bh);
1180 goto drop;
1183 } else if (jh->b_transaction) {
1184 J_ASSERT_JH(jh, (jh->b_transaction ==
1185 journal->j_committing_transaction));
1186 /* However, if the buffer is still owned by a prior
1187 * (committing) transaction, we can't drop it yet... */
1188 JBUFFER_TRACE(jh, "belongs to older transaction");
1189 /* ... but we CAN drop it from the new transaction if we
1190 * have also modified it since the original commit. */
1192 if (jh->b_next_transaction) {
1193 J_ASSERT(jh->b_next_transaction == transaction);
1194 jh->b_next_transaction = NULL;
1197 * only drop a reference if this transaction modified
1198 * the buffer
1200 if (was_modified)
1201 drop_reserve = 1;
1205 not_jbd:
1206 spin_unlock(&journal->j_list_lock);
1207 jbd_unlock_bh_state(bh);
1208 __brelse(bh);
1209 drop:
1210 if (drop_reserve) {
1211 /* no need to reserve log space for this block -bzzz */
1212 handle->h_buffer_credits++;
1214 return err;
1218 * int jbd2_journal_stop() - complete a transaction
1219 * @handle: tranaction to complete.
1221 * All done for a particular handle.
1223 * There is not much action needed here. We just return any remaining
1224 * buffer credits to the transaction and remove the handle. The only
1225 * complication is that we need to start a commit operation if the
1226 * filesystem is marked for synchronous update.
1228 * jbd2_journal_stop itself will not usually return an error, but it may
1229 * do so in unusual circumstances. In particular, expect it to
1230 * return -EIO if a jbd2_journal_abort has been executed since the
1231 * transaction began.
1233 int jbd2_journal_stop(handle_t *handle)
1235 transaction_t *transaction = handle->h_transaction;
1236 journal_t *journal = transaction->t_journal;
1237 int err;
1238 pid_t pid;
1240 J_ASSERT(journal_current_handle() == handle);
1242 if (is_handle_aborted(handle))
1243 err = -EIO;
1244 else {
1245 J_ASSERT(transaction->t_updates > 0);
1246 err = 0;
1249 if (--handle->h_ref > 0) {
1250 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1251 handle->h_ref);
1252 return err;
1255 jbd_debug(4, "Handle %p going down\n", handle);
1258 * Implement synchronous transaction batching. If the handle
1259 * was synchronous, don't force a commit immediately. Let's
1260 * yield and let another thread piggyback onto this
1261 * transaction. Keep doing that while new threads continue to
1262 * arrive. It doesn't cost much - we're about to run a commit
1263 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1264 * operations by 30x or more...
1266 * We try and optimize the sleep time against what the
1267 * underlying disk can do, instead of having a static sleep
1268 * time. This is useful for the case where our storage is so
1269 * fast that it is more optimal to go ahead and force a flush
1270 * and wait for the transaction to be committed than it is to
1271 * wait for an arbitrary amount of time for new writers to
1272 * join the transaction. We achieve this by measuring how
1273 * long it takes to commit a transaction, and compare it with
1274 * how long this transaction has been running, and if run time
1275 * < commit time then we sleep for the delta and commit. This
1276 * greatly helps super fast disks that would see slowdowns as
1277 * more threads started doing fsyncs.
1279 * But don't do this if this process was the most recent one
1280 * to perform a synchronous write. We do this to detect the
1281 * case where a single process is doing a stream of sync
1282 * writes. No point in waiting for joiners in that case.
1284 pid = current->pid;
1285 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1286 u64 commit_time, trans_time;
1288 journal->j_last_sync_writer = pid;
1290 spin_lock(&journal->j_state_lock);
1291 commit_time = journal->j_average_commit_time;
1292 spin_unlock(&journal->j_state_lock);
1294 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1295 transaction->t_start_time));
1297 commit_time = max_t(u64, commit_time,
1298 1000*journal->j_min_batch_time);
1299 commit_time = min_t(u64, commit_time,
1300 1000*journal->j_max_batch_time);
1302 if (trans_time < commit_time) {
1303 ktime_t expires = ktime_add_ns(ktime_get(),
1304 commit_time);
1305 set_current_state(TASK_UNINTERRUPTIBLE);
1306 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1310 if (handle->h_sync)
1311 transaction->t_synchronous_commit = 1;
1312 current->journal_info = NULL;
1313 spin_lock(&journal->j_state_lock);
1314 spin_lock(&transaction->t_handle_lock);
1315 transaction->t_outstanding_credits -= handle->h_buffer_credits;
1316 transaction->t_updates--;
1317 if (!transaction->t_updates) {
1318 wake_up(&journal->j_wait_updates);
1319 if (journal->j_barrier_count)
1320 wake_up(&journal->j_wait_transaction_locked);
1324 * If the handle is marked SYNC, we need to set another commit
1325 * going! We also want to force a commit if the current
1326 * transaction is occupying too much of the log, or if the
1327 * transaction is too old now.
1329 if (handle->h_sync ||
1330 transaction->t_outstanding_credits >
1331 journal->j_max_transaction_buffers ||
1332 time_after_eq(jiffies, transaction->t_expires)) {
1333 /* Do this even for aborted journals: an abort still
1334 * completes the commit thread, it just doesn't write
1335 * anything to disk. */
1336 tid_t tid = transaction->t_tid;
1338 spin_unlock(&transaction->t_handle_lock);
1339 jbd_debug(2, "transaction too old, requesting commit for "
1340 "handle %p\n", handle);
1341 /* This is non-blocking */
1342 __jbd2_log_start_commit(journal, transaction->t_tid);
1343 spin_unlock(&journal->j_state_lock);
1346 * Special case: JBD2_SYNC synchronous updates require us
1347 * to wait for the commit to complete.
1349 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1350 err = jbd2_log_wait_commit(journal, tid);
1351 } else {
1352 spin_unlock(&transaction->t_handle_lock);
1353 spin_unlock(&journal->j_state_lock);
1356 lock_map_release(&handle->h_lockdep_map);
1358 jbd2_free_handle(handle);
1359 return err;
1363 * int jbd2_journal_force_commit() - force any uncommitted transactions
1364 * @journal: journal to force
1366 * For synchronous operations: force any uncommitted transactions
1367 * to disk. May seem kludgy, but it reuses all the handle batching
1368 * code in a very simple manner.
1370 int jbd2_journal_force_commit(journal_t *journal)
1372 handle_t *handle;
1373 int ret;
1375 handle = jbd2_journal_start(journal, 1);
1376 if (IS_ERR(handle)) {
1377 ret = PTR_ERR(handle);
1378 } else {
1379 handle->h_sync = 1;
1380 ret = jbd2_journal_stop(handle);
1382 return ret;
1387 * List management code snippets: various functions for manipulating the
1388 * transaction buffer lists.
1393 * Append a buffer to a transaction list, given the transaction's list head
1394 * pointer.
1396 * j_list_lock is held.
1398 * jbd_lock_bh_state(jh2bh(jh)) is held.
1401 static inline void
1402 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1404 if (!*list) {
1405 jh->b_tnext = jh->b_tprev = jh;
1406 *list = jh;
1407 } else {
1408 /* Insert at the tail of the list to preserve order */
1409 struct journal_head *first = *list, *last = first->b_tprev;
1410 jh->b_tprev = last;
1411 jh->b_tnext = first;
1412 last->b_tnext = first->b_tprev = jh;
1417 * Remove a buffer from a transaction list, given the transaction's list
1418 * head pointer.
1420 * Called with j_list_lock held, and the journal may not be locked.
1422 * jbd_lock_bh_state(jh2bh(jh)) is held.
1425 static inline void
1426 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1428 if (*list == jh) {
1429 *list = jh->b_tnext;
1430 if (*list == jh)
1431 *list = NULL;
1433 jh->b_tprev->b_tnext = jh->b_tnext;
1434 jh->b_tnext->b_tprev = jh->b_tprev;
1438 * Remove a buffer from the appropriate transaction list.
1440 * Note that this function can *change* the value of
1441 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1442 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1443 * of these pointers, it could go bad. Generally the caller needs to re-read
1444 * the pointer from the transaction_t.
1446 * Called under j_list_lock. The journal may not be locked.
1448 void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1450 struct journal_head **list = NULL;
1451 transaction_t *transaction;
1452 struct buffer_head *bh = jh2bh(jh);
1454 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1455 transaction = jh->b_transaction;
1456 if (transaction)
1457 assert_spin_locked(&transaction->t_journal->j_list_lock);
1459 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1460 if (jh->b_jlist != BJ_None)
1461 J_ASSERT_JH(jh, transaction != NULL);
1463 switch (jh->b_jlist) {
1464 case BJ_None:
1465 return;
1466 case BJ_Metadata:
1467 transaction->t_nr_buffers--;
1468 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1469 list = &transaction->t_buffers;
1470 break;
1471 case BJ_Forget:
1472 list = &transaction->t_forget;
1473 break;
1474 case BJ_IO:
1475 list = &transaction->t_iobuf_list;
1476 break;
1477 case BJ_Shadow:
1478 list = &transaction->t_shadow_list;
1479 break;
1480 case BJ_LogCtl:
1481 list = &transaction->t_log_list;
1482 break;
1483 case BJ_Reserved:
1484 list = &transaction->t_reserved_list;
1485 break;
1488 __blist_del_buffer(list, jh);
1489 jh->b_jlist = BJ_None;
1490 if (test_clear_buffer_jbddirty(bh))
1491 mark_buffer_dirty(bh); /* Expose it to the VM */
1494 void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1496 __jbd2_journal_temp_unlink_buffer(jh);
1497 jh->b_transaction = NULL;
1500 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1502 jbd_lock_bh_state(jh2bh(jh));
1503 spin_lock(&journal->j_list_lock);
1504 __jbd2_journal_unfile_buffer(jh);
1505 spin_unlock(&journal->j_list_lock);
1506 jbd_unlock_bh_state(jh2bh(jh));
1510 * Called from jbd2_journal_try_to_free_buffers().
1512 * Called under jbd_lock_bh_state(bh)
1514 static void
1515 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1517 struct journal_head *jh;
1519 jh = bh2jh(bh);
1521 if (buffer_locked(bh) || buffer_dirty(bh))
1522 goto out;
1524 if (jh->b_next_transaction != NULL)
1525 goto out;
1527 spin_lock(&journal->j_list_lock);
1528 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1529 /* written-back checkpointed metadata buffer */
1530 if (jh->b_jlist == BJ_None) {
1531 JBUFFER_TRACE(jh, "remove from checkpoint list");
1532 __jbd2_journal_remove_checkpoint(jh);
1533 jbd2_journal_remove_journal_head(bh);
1534 __brelse(bh);
1537 spin_unlock(&journal->j_list_lock);
1538 out:
1539 return;
1543 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1544 * @journal: journal for operation
1545 * @page: to try and free
1546 * @gfp_mask: we use the mask to detect how hard should we try to release
1547 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1548 * release the buffers.
1551 * For all the buffers on this page,
1552 * if they are fully written out ordered data, move them onto BUF_CLEAN
1553 * so try_to_free_buffers() can reap them.
1555 * This function returns non-zero if we wish try_to_free_buffers()
1556 * to be called. We do this if the page is releasable by try_to_free_buffers().
1557 * We also do it if the page has locked or dirty buffers and the caller wants
1558 * us to perform sync or async writeout.
1560 * This complicates JBD locking somewhat. We aren't protected by the
1561 * BKL here. We wish to remove the buffer from its committing or
1562 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1564 * This may *change* the value of transaction_t->t_datalist, so anyone
1565 * who looks at t_datalist needs to lock against this function.
1567 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1568 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1569 * will come out of the lock with the buffer dirty, which makes it
1570 * ineligible for release here.
1572 * Who else is affected by this? hmm... Really the only contender
1573 * is do_get_write_access() - it could be looking at the buffer while
1574 * journal_try_to_free_buffer() is changing its state. But that
1575 * cannot happen because we never reallocate freed data as metadata
1576 * while the data is part of a transaction. Yes?
1578 * Return 0 on failure, 1 on success
1580 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1581 struct page *page, gfp_t gfp_mask)
1583 struct buffer_head *head;
1584 struct buffer_head *bh;
1585 int ret = 0;
1587 J_ASSERT(PageLocked(page));
1589 head = page_buffers(page);
1590 bh = head;
1591 do {
1592 struct journal_head *jh;
1595 * We take our own ref against the journal_head here to avoid
1596 * having to add tons of locking around each instance of
1597 * jbd2_journal_remove_journal_head() and
1598 * jbd2_journal_put_journal_head().
1600 jh = jbd2_journal_grab_journal_head(bh);
1601 if (!jh)
1602 continue;
1604 jbd_lock_bh_state(bh);
1605 __journal_try_to_free_buffer(journal, bh);
1606 jbd2_journal_put_journal_head(jh);
1607 jbd_unlock_bh_state(bh);
1608 if (buffer_jbd(bh))
1609 goto busy;
1610 } while ((bh = bh->b_this_page) != head);
1612 ret = try_to_free_buffers(page);
1614 busy:
1615 return ret;
1619 * This buffer is no longer needed. If it is on an older transaction's
1620 * checkpoint list we need to record it on this transaction's forget list
1621 * to pin this buffer (and hence its checkpointing transaction) down until
1622 * this transaction commits. If the buffer isn't on a checkpoint list, we
1623 * release it.
1624 * Returns non-zero if JBD no longer has an interest in the buffer.
1626 * Called under j_list_lock.
1628 * Called under jbd_lock_bh_state(bh).
1630 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1632 int may_free = 1;
1633 struct buffer_head *bh = jh2bh(jh);
1635 __jbd2_journal_unfile_buffer(jh);
1637 if (jh->b_cp_transaction) {
1638 JBUFFER_TRACE(jh, "on running+cp transaction");
1640 * We don't want to write the buffer anymore, clear the
1641 * bit so that we don't confuse checks in
1642 * __journal_file_buffer
1644 clear_buffer_dirty(bh);
1645 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1646 may_free = 0;
1647 } else {
1648 JBUFFER_TRACE(jh, "on running transaction");
1649 jbd2_journal_remove_journal_head(bh);
1650 __brelse(bh);
1652 return may_free;
1656 * jbd2_journal_invalidatepage
1658 * This code is tricky. It has a number of cases to deal with.
1660 * There are two invariants which this code relies on:
1662 * i_size must be updated on disk before we start calling invalidatepage on the
1663 * data.
1665 * This is done in ext3 by defining an ext3_setattr method which
1666 * updates i_size before truncate gets going. By maintaining this
1667 * invariant, we can be sure that it is safe to throw away any buffers
1668 * attached to the current transaction: once the transaction commits,
1669 * we know that the data will not be needed.
1671 * Note however that we can *not* throw away data belonging to the
1672 * previous, committing transaction!
1674 * Any disk blocks which *are* part of the previous, committing
1675 * transaction (and which therefore cannot be discarded immediately) are
1676 * not going to be reused in the new running transaction
1678 * The bitmap committed_data images guarantee this: any block which is
1679 * allocated in one transaction and removed in the next will be marked
1680 * as in-use in the committed_data bitmap, so cannot be reused until
1681 * the next transaction to delete the block commits. This means that
1682 * leaving committing buffers dirty is quite safe: the disk blocks
1683 * cannot be reallocated to a different file and so buffer aliasing is
1684 * not possible.
1687 * The above applies mainly to ordered data mode. In writeback mode we
1688 * don't make guarantees about the order in which data hits disk --- in
1689 * particular we don't guarantee that new dirty data is flushed before
1690 * transaction commit --- so it is always safe just to discard data
1691 * immediately in that mode. --sct
1695 * The journal_unmap_buffer helper function returns zero if the buffer
1696 * concerned remains pinned as an anonymous buffer belonging to an older
1697 * transaction.
1699 * We're outside-transaction here. Either or both of j_running_transaction
1700 * and j_committing_transaction may be NULL.
1702 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1704 transaction_t *transaction;
1705 struct journal_head *jh;
1706 int may_free = 1;
1707 int ret;
1709 BUFFER_TRACE(bh, "entry");
1712 * It is safe to proceed here without the j_list_lock because the
1713 * buffers cannot be stolen by try_to_free_buffers as long as we are
1714 * holding the page lock. --sct
1717 if (!buffer_jbd(bh))
1718 goto zap_buffer_unlocked;
1720 /* OK, we have data buffer in journaled mode */
1721 spin_lock(&journal->j_state_lock);
1722 jbd_lock_bh_state(bh);
1723 spin_lock(&journal->j_list_lock);
1725 jh = jbd2_journal_grab_journal_head(bh);
1726 if (!jh)
1727 goto zap_buffer_no_jh;
1729 transaction = jh->b_transaction;
1730 if (transaction == NULL) {
1731 /* First case: not on any transaction. If it
1732 * has no checkpoint link, then we can zap it:
1733 * it's a writeback-mode buffer so we don't care
1734 * if it hits disk safely. */
1735 if (!jh->b_cp_transaction) {
1736 JBUFFER_TRACE(jh, "not on any transaction: zap");
1737 goto zap_buffer;
1740 if (!buffer_dirty(bh)) {
1741 /* bdflush has written it. We can drop it now */
1742 goto zap_buffer;
1745 /* OK, it must be in the journal but still not
1746 * written fully to disk: it's metadata or
1747 * journaled data... */
1749 if (journal->j_running_transaction) {
1750 /* ... and once the current transaction has
1751 * committed, the buffer won't be needed any
1752 * longer. */
1753 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1754 ret = __dispose_buffer(jh,
1755 journal->j_running_transaction);
1756 jbd2_journal_put_journal_head(jh);
1757 spin_unlock(&journal->j_list_lock);
1758 jbd_unlock_bh_state(bh);
1759 spin_unlock(&journal->j_state_lock);
1760 return ret;
1761 } else {
1762 /* There is no currently-running transaction. So the
1763 * orphan record which we wrote for this file must have
1764 * passed into commit. We must attach this buffer to
1765 * the committing transaction, if it exists. */
1766 if (journal->j_committing_transaction) {
1767 JBUFFER_TRACE(jh, "give to committing trans");
1768 ret = __dispose_buffer(jh,
1769 journal->j_committing_transaction);
1770 jbd2_journal_put_journal_head(jh);
1771 spin_unlock(&journal->j_list_lock);
1772 jbd_unlock_bh_state(bh);
1773 spin_unlock(&journal->j_state_lock);
1774 return ret;
1775 } else {
1776 /* The orphan record's transaction has
1777 * committed. We can cleanse this buffer */
1778 clear_buffer_jbddirty(bh);
1779 goto zap_buffer;
1782 } else if (transaction == journal->j_committing_transaction) {
1783 JBUFFER_TRACE(jh, "on committing transaction");
1785 * If it is committing, we simply cannot touch it. We
1786 * can remove it's next_transaction pointer from the
1787 * running transaction if that is set, but nothing
1788 * else. */
1789 set_buffer_freed(bh);
1790 if (jh->b_next_transaction) {
1791 J_ASSERT(jh->b_next_transaction ==
1792 journal->j_running_transaction);
1793 jh->b_next_transaction = NULL;
1795 jbd2_journal_put_journal_head(jh);
1796 spin_unlock(&journal->j_list_lock);
1797 jbd_unlock_bh_state(bh);
1798 spin_unlock(&journal->j_state_lock);
1799 return 0;
1800 } else {
1801 /* Good, the buffer belongs to the running transaction.
1802 * We are writing our own transaction's data, not any
1803 * previous one's, so it is safe to throw it away
1804 * (remember that we expect the filesystem to have set
1805 * i_size already for this truncate so recovery will not
1806 * expose the disk blocks we are discarding here.) */
1807 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1808 JBUFFER_TRACE(jh, "on running transaction");
1809 may_free = __dispose_buffer(jh, transaction);
1812 zap_buffer:
1813 jbd2_journal_put_journal_head(jh);
1814 zap_buffer_no_jh:
1815 spin_unlock(&journal->j_list_lock);
1816 jbd_unlock_bh_state(bh);
1817 spin_unlock(&journal->j_state_lock);
1818 zap_buffer_unlocked:
1819 clear_buffer_dirty(bh);
1820 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1821 clear_buffer_mapped(bh);
1822 clear_buffer_req(bh);
1823 clear_buffer_new(bh);
1824 bh->b_bdev = NULL;
1825 return may_free;
1829 * void jbd2_journal_invalidatepage()
1830 * @journal: journal to use for flush...
1831 * @page: page to flush
1832 * @offset: length of page to invalidate.
1834 * Reap page buffers containing data after offset in page.
1837 void jbd2_journal_invalidatepage(journal_t *journal,
1838 struct page *page,
1839 unsigned long offset)
1841 struct buffer_head *head, *bh, *next;
1842 unsigned int curr_off = 0;
1843 int may_free = 1;
1845 if (!PageLocked(page))
1846 BUG();
1847 if (!page_has_buffers(page))
1848 return;
1850 /* We will potentially be playing with lists other than just the
1851 * data lists (especially for journaled data mode), so be
1852 * cautious in our locking. */
1854 head = bh = page_buffers(page);
1855 do {
1856 unsigned int next_off = curr_off + bh->b_size;
1857 next = bh->b_this_page;
1859 if (offset <= curr_off) {
1860 /* This block is wholly outside the truncation point */
1861 lock_buffer(bh);
1862 may_free &= journal_unmap_buffer(journal, bh);
1863 unlock_buffer(bh);
1865 curr_off = next_off;
1866 bh = next;
1868 } while (bh != head);
1870 if (!offset) {
1871 if (may_free && try_to_free_buffers(page))
1872 J_ASSERT(!page_has_buffers(page));
1877 * File a buffer on the given transaction list.
1879 void __jbd2_journal_file_buffer(struct journal_head *jh,
1880 transaction_t *transaction, int jlist)
1882 struct journal_head **list = NULL;
1883 int was_dirty = 0;
1884 struct buffer_head *bh = jh2bh(jh);
1886 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1887 assert_spin_locked(&transaction->t_journal->j_list_lock);
1889 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1890 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1891 jh->b_transaction == NULL);
1893 if (jh->b_transaction && jh->b_jlist == jlist)
1894 return;
1896 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1897 jlist == BJ_Shadow || jlist == BJ_Forget) {
1899 * For metadata buffers, we track dirty bit in buffer_jbddirty
1900 * instead of buffer_dirty. We should not see a dirty bit set
1901 * here because we clear it in do_get_write_access but e.g.
1902 * tune2fs can modify the sb and set the dirty bit at any time
1903 * so we try to gracefully handle that.
1905 if (buffer_dirty(bh))
1906 warn_dirty_buffer(bh);
1907 if (test_clear_buffer_dirty(bh) ||
1908 test_clear_buffer_jbddirty(bh))
1909 was_dirty = 1;
1912 if (jh->b_transaction)
1913 __jbd2_journal_temp_unlink_buffer(jh);
1914 jh->b_transaction = transaction;
1916 switch (jlist) {
1917 case BJ_None:
1918 J_ASSERT_JH(jh, !jh->b_committed_data);
1919 J_ASSERT_JH(jh, !jh->b_frozen_data);
1920 return;
1921 case BJ_Metadata:
1922 transaction->t_nr_buffers++;
1923 list = &transaction->t_buffers;
1924 break;
1925 case BJ_Forget:
1926 list = &transaction->t_forget;
1927 break;
1928 case BJ_IO:
1929 list = &transaction->t_iobuf_list;
1930 break;
1931 case BJ_Shadow:
1932 list = &transaction->t_shadow_list;
1933 break;
1934 case BJ_LogCtl:
1935 list = &transaction->t_log_list;
1936 break;
1937 case BJ_Reserved:
1938 list = &transaction->t_reserved_list;
1939 break;
1942 __blist_add_buffer(list, jh);
1943 jh->b_jlist = jlist;
1945 if (was_dirty)
1946 set_buffer_jbddirty(bh);
1949 void jbd2_journal_file_buffer(struct journal_head *jh,
1950 transaction_t *transaction, int jlist)
1952 jbd_lock_bh_state(jh2bh(jh));
1953 spin_lock(&transaction->t_journal->j_list_lock);
1954 __jbd2_journal_file_buffer(jh, transaction, jlist);
1955 spin_unlock(&transaction->t_journal->j_list_lock);
1956 jbd_unlock_bh_state(jh2bh(jh));
1960 * Remove a buffer from its current buffer list in preparation for
1961 * dropping it from its current transaction entirely. If the buffer has
1962 * already started to be used by a subsequent transaction, refile the
1963 * buffer on that transaction's metadata list.
1965 * Called under journal->j_list_lock
1967 * Called under jbd_lock_bh_state(jh2bh(jh))
1969 void __jbd2_journal_refile_buffer(struct journal_head *jh)
1971 int was_dirty;
1972 struct buffer_head *bh = jh2bh(jh);
1974 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1975 if (jh->b_transaction)
1976 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
1978 /* If the buffer is now unused, just drop it. */
1979 if (jh->b_next_transaction == NULL) {
1980 __jbd2_journal_unfile_buffer(jh);
1981 return;
1985 * It has been modified by a later transaction: add it to the new
1986 * transaction's metadata list.
1989 was_dirty = test_clear_buffer_jbddirty(bh);
1990 __jbd2_journal_temp_unlink_buffer(jh);
1991 jh->b_transaction = jh->b_next_transaction;
1992 jh->b_next_transaction = NULL;
1993 __jbd2_journal_file_buffer(jh, jh->b_transaction,
1994 jh->b_modified ? BJ_Metadata : BJ_Reserved);
1995 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
1997 if (was_dirty)
1998 set_buffer_jbddirty(bh);
2002 * For the unlocked version of this call, also make sure that any
2003 * hanging journal_head is cleaned up if necessary.
2005 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2006 * operation on a buffer_head, in which the caller is probably going to
2007 * be hooking the journal_head onto other lists. In that case it is up
2008 * to the caller to remove the journal_head if necessary. For the
2009 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2010 * doing anything else to the buffer so we need to do the cleanup
2011 * ourselves to avoid a jh leak.
2013 * *** The journal_head may be freed by this call! ***
2015 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2017 struct buffer_head *bh = jh2bh(jh);
2019 jbd_lock_bh_state(bh);
2020 spin_lock(&journal->j_list_lock);
2022 __jbd2_journal_refile_buffer(jh);
2023 jbd_unlock_bh_state(bh);
2024 jbd2_journal_remove_journal_head(bh);
2026 spin_unlock(&journal->j_list_lock);
2027 __brelse(bh);
2031 * File inode in the inode list of the handle's transaction
2033 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2035 transaction_t *transaction = handle->h_transaction;
2036 journal_t *journal = transaction->t_journal;
2038 if (is_handle_aborted(handle))
2039 return -EIO;
2041 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2042 transaction->t_tid);
2045 * First check whether inode isn't already on the transaction's
2046 * lists without taking the lock. Note that this check is safe
2047 * without the lock as we cannot race with somebody removing inode
2048 * from the transaction. The reason is that we remove inode from the
2049 * transaction only in journal_release_jbd_inode() and when we commit
2050 * the transaction. We are guarded from the first case by holding
2051 * a reference to the inode. We are safe against the second case
2052 * because if jinode->i_transaction == transaction, commit code
2053 * cannot touch the transaction because we hold reference to it,
2054 * and if jinode->i_next_transaction == transaction, commit code
2055 * will only file the inode where we want it.
2057 if (jinode->i_transaction == transaction ||
2058 jinode->i_next_transaction == transaction)
2059 return 0;
2061 spin_lock(&journal->j_list_lock);
2063 if (jinode->i_transaction == transaction ||
2064 jinode->i_next_transaction == transaction)
2065 goto done;
2067 /* On some different transaction's list - should be
2068 * the committing one */
2069 if (jinode->i_transaction) {
2070 J_ASSERT(jinode->i_next_transaction == NULL);
2071 J_ASSERT(jinode->i_transaction ==
2072 journal->j_committing_transaction);
2073 jinode->i_next_transaction = transaction;
2074 goto done;
2076 /* Not on any transaction list... */
2077 J_ASSERT(!jinode->i_next_transaction);
2078 jinode->i_transaction = transaction;
2079 list_add(&jinode->i_list, &transaction->t_inode_list);
2080 done:
2081 spin_unlock(&journal->j_list_lock);
2083 return 0;
2087 * File truncate and transaction commit interact with each other in a
2088 * non-trivial way. If a transaction writing data block A is
2089 * committing, we cannot discard the data by truncate until we have
2090 * written them. Otherwise if we crashed after the transaction with
2091 * write has committed but before the transaction with truncate has
2092 * committed, we could see stale data in block A. This function is a
2093 * helper to solve this problem. It starts writeout of the truncated
2094 * part in case it is in the committing transaction.
2096 * Filesystem code must call this function when inode is journaled in
2097 * ordered mode before truncation happens and after the inode has been
2098 * placed on orphan list with the new inode size. The second condition
2099 * avoids the race that someone writes new data and we start
2100 * committing the transaction after this function has been called but
2101 * before a transaction for truncate is started (and furthermore it
2102 * allows us to optimize the case where the addition to orphan list
2103 * happens in the same transaction as write --- we don't have to write
2104 * any data in such case).
2106 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2107 struct jbd2_inode *jinode,
2108 loff_t new_size)
2110 transaction_t *inode_trans, *commit_trans;
2111 int ret = 0;
2113 /* This is a quick check to avoid locking if not necessary */
2114 if (!jinode->i_transaction)
2115 goto out;
2116 /* Locks are here just to force reading of recent values, it is
2117 * enough that the transaction was not committing before we started
2118 * a transaction adding the inode to orphan list */
2119 spin_lock(&journal->j_state_lock);
2120 commit_trans = journal->j_committing_transaction;
2121 spin_unlock(&journal->j_state_lock);
2122 spin_lock(&journal->j_list_lock);
2123 inode_trans = jinode->i_transaction;
2124 spin_unlock(&journal->j_list_lock);
2125 if (inode_trans == commit_trans) {
2126 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2127 new_size, LLONG_MAX);
2128 if (ret)
2129 jbd2_journal_abort(journal, ret);
2131 out:
2132 return ret;