isci: unify phy data structures
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd2 / transaction.c
blob3eec82d32fd4c6886fdf823e7d3690ca2e665162
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>
29 #include <linux/backing-dev.h>
30 #include <linux/module.h>
32 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
35 * jbd2_get_transaction: obtain a new transaction_t object.
37 * Simply allocate and initialise a new transaction. Create it in
38 * RUNNING state and add it to the current journal (which should not
39 * have an existing running transaction: we only make a new transaction
40 * once we have started to commit the old one).
42 * Preconditions:
43 * The journal MUST be locked. We don't perform atomic mallocs on the
44 * new transaction and we can't block without protecting against other
45 * processes trying to touch the journal while it is in transition.
49 static transaction_t *
50 jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
52 transaction->t_journal = journal;
53 transaction->t_state = T_RUNNING;
54 transaction->t_start_time = ktime_get();
55 transaction->t_tid = journal->j_transaction_sequence++;
56 transaction->t_expires = jiffies + journal->j_commit_interval;
57 spin_lock_init(&transaction->t_handle_lock);
58 atomic_set(&transaction->t_updates, 0);
59 atomic_set(&transaction->t_outstanding_credits, 0);
60 atomic_set(&transaction->t_handle_count, 0);
61 INIT_LIST_HEAD(&transaction->t_inode_list);
62 INIT_LIST_HEAD(&transaction->t_private_list);
64 /* Set up the commit timer for the new transaction. */
65 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires);
66 add_timer(&journal->j_commit_timer);
68 J_ASSERT(journal->j_running_transaction == NULL);
69 journal->j_running_transaction = transaction;
70 transaction->t_max_wait = 0;
71 transaction->t_start = jiffies;
73 return transaction;
77 * Handle management.
79 * A handle_t is an object which represents a single atomic update to a
80 * filesystem, and which tracks all of the modifications which form part
81 * of that one update.
85 * Update transaction's maximum wait time, if debugging is enabled.
87 * In order for t_max_wait to be reliable, it must be protected by a
88 * lock. But doing so will mean that start_this_handle() can not be
89 * run in parallel on SMP systems, which limits our scalability. So
90 * unless debugging is enabled, we no longer update t_max_wait, which
91 * means that maximum wait time reported by the jbd2_run_stats
92 * tracepoint will always be zero.
94 static inline void update_t_max_wait(transaction_t *transaction,
95 unsigned long ts)
97 #ifdef CONFIG_JBD2_DEBUG
98 if (jbd2_journal_enable_debug &&
99 time_after(transaction->t_start, ts)) {
100 ts = jbd2_time_diff(ts, transaction->t_start);
101 spin_lock(&transaction->t_handle_lock);
102 if (ts > transaction->t_max_wait)
103 transaction->t_max_wait = ts;
104 spin_unlock(&transaction->t_handle_lock);
106 #endif
110 * start_this_handle: Given a handle, deal with any locking or stalling
111 * needed to make sure that there is enough journal space for the handle
112 * to begin. Attach the handle to a transaction and set up the
113 * transaction's buffer credits.
116 static int start_this_handle(journal_t *journal, handle_t *handle,
117 int gfp_mask)
119 transaction_t *transaction, *new_transaction = NULL;
120 tid_t tid;
121 int needed, need_to_start;
122 int nblocks = handle->h_buffer_credits;
123 unsigned long ts = jiffies;
125 if (nblocks > journal->j_max_transaction_buffers) {
126 printk(KERN_ERR "JBD: %s wants too many credits (%d > %d)\n",
127 current->comm, nblocks,
128 journal->j_max_transaction_buffers);
129 return -ENOSPC;
132 alloc_transaction:
133 if (!journal->j_running_transaction) {
134 new_transaction = kzalloc(sizeof(*new_transaction), gfp_mask);
135 if (!new_transaction) {
137 * If __GFP_FS is not present, then we may be
138 * being called from inside the fs writeback
139 * layer, so we MUST NOT fail. Since
140 * __GFP_NOFAIL is going away, we will arrange
141 * to retry the allocation ourselves.
143 if ((gfp_mask & __GFP_FS) == 0) {
144 congestion_wait(BLK_RW_ASYNC, HZ/50);
145 goto alloc_transaction;
147 return -ENOMEM;
151 jbd_debug(3, "New handle %p going live.\n", handle);
154 * We need to hold j_state_lock until t_updates has been incremented,
155 * for proper journal barrier handling
157 repeat:
158 read_lock(&journal->j_state_lock);
159 BUG_ON(journal->j_flags & JBD2_UNMOUNT);
160 if (is_journal_aborted(journal) ||
161 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) {
162 read_unlock(&journal->j_state_lock);
163 kfree(new_transaction);
164 return -EROFS;
167 /* Wait on the journal's transaction barrier if necessary */
168 if (journal->j_barrier_count) {
169 read_unlock(&journal->j_state_lock);
170 wait_event(journal->j_wait_transaction_locked,
171 journal->j_barrier_count == 0);
172 goto repeat;
175 if (!journal->j_running_transaction) {
176 read_unlock(&journal->j_state_lock);
177 if (!new_transaction)
178 goto alloc_transaction;
179 write_lock(&journal->j_state_lock);
180 if (!journal->j_running_transaction) {
181 jbd2_get_transaction(journal, new_transaction);
182 new_transaction = NULL;
184 write_unlock(&journal->j_state_lock);
185 goto repeat;
188 transaction = journal->j_running_transaction;
191 * If the current transaction is locked down for commit, wait for the
192 * lock to be released.
194 if (transaction->t_state == T_LOCKED) {
195 DEFINE_WAIT(wait);
197 prepare_to_wait(&journal->j_wait_transaction_locked,
198 &wait, TASK_UNINTERRUPTIBLE);
199 read_unlock(&journal->j_state_lock);
200 schedule();
201 finish_wait(&journal->j_wait_transaction_locked, &wait);
202 goto repeat;
206 * If there is not enough space left in the log to write all potential
207 * buffers requested by this operation, we need to stall pending a log
208 * checkpoint to free some more log space.
210 needed = atomic_add_return(nblocks,
211 &transaction->t_outstanding_credits);
213 if (needed > journal->j_max_transaction_buffers) {
215 * If the current transaction is already too large, then start
216 * to commit it: we can then go back and attach this handle to
217 * a new transaction.
219 DEFINE_WAIT(wait);
221 jbd_debug(2, "Handle %p starting new commit...\n", handle);
222 atomic_sub(nblocks, &transaction->t_outstanding_credits);
223 prepare_to_wait(&journal->j_wait_transaction_locked, &wait,
224 TASK_UNINTERRUPTIBLE);
225 tid = transaction->t_tid;
226 need_to_start = !tid_geq(journal->j_commit_request, tid);
227 read_unlock(&journal->j_state_lock);
228 if (need_to_start)
229 jbd2_log_start_commit(journal, tid);
230 schedule();
231 finish_wait(&journal->j_wait_transaction_locked, &wait);
232 goto repeat;
236 * The commit code assumes that it can get enough log space
237 * without forcing a checkpoint. This is *critical* for
238 * correctness: a checkpoint of a buffer which is also
239 * associated with a committing transaction creates a deadlock,
240 * so commit simply cannot force through checkpoints.
242 * We must therefore ensure the necessary space in the journal
243 * *before* starting to dirty potentially checkpointed buffers
244 * in the new transaction.
246 * The worst part is, any transaction currently committing can
247 * reduce the free space arbitrarily. Be careful to account for
248 * those buffers when checkpointing.
252 * @@@ AKPM: This seems rather over-defensive. We're giving commit
253 * a _lot_ of headroom: 1/4 of the journal plus the size of
254 * the committing transaction. Really, we only need to give it
255 * committing_transaction->t_outstanding_credits plus "enough" for
256 * the log control blocks.
257 * Also, this test is inconsistent with the matching one in
258 * jbd2_journal_extend().
260 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal)) {
261 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle);
262 atomic_sub(nblocks, &transaction->t_outstanding_credits);
263 read_unlock(&journal->j_state_lock);
264 write_lock(&journal->j_state_lock);
265 if (__jbd2_log_space_left(journal) < jbd_space_needed(journal))
266 __jbd2_log_wait_for_space(journal);
267 write_unlock(&journal->j_state_lock);
268 goto repeat;
271 /* OK, account for the buffers that this operation expects to
272 * use and add the handle to the running transaction.
274 update_t_max_wait(transaction, ts);
275 handle->h_transaction = transaction;
276 atomic_inc(&transaction->t_updates);
277 atomic_inc(&transaction->t_handle_count);
278 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
279 handle, nblocks,
280 atomic_read(&transaction->t_outstanding_credits),
281 __jbd2_log_space_left(journal));
282 read_unlock(&journal->j_state_lock);
284 lock_map_acquire(&handle->h_lockdep_map);
285 kfree(new_transaction);
286 return 0;
289 static struct lock_class_key jbd2_handle_key;
291 /* Allocate a new handle. This should probably be in a slab... */
292 static handle_t *new_handle(int nblocks)
294 handle_t *handle = jbd2_alloc_handle(GFP_NOFS);
295 if (!handle)
296 return NULL;
297 memset(handle, 0, sizeof(*handle));
298 handle->h_buffer_credits = nblocks;
299 handle->h_ref = 1;
301 lockdep_init_map(&handle->h_lockdep_map, "jbd2_handle",
302 &jbd2_handle_key, 0);
304 return handle;
308 * handle_t *jbd2_journal_start() - Obtain a new handle.
309 * @journal: Journal to start transaction on.
310 * @nblocks: number of block buffer we might modify
312 * We make sure that the transaction can guarantee at least nblocks of
313 * modified buffers in the log. We block until the log can guarantee
314 * that much space.
316 * This function is visible to journal users (like ext3fs), so is not
317 * called with the journal already locked.
319 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
320 * on failure.
322 handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int gfp_mask)
324 handle_t *handle = journal_current_handle();
325 int err;
327 if (!journal)
328 return ERR_PTR(-EROFS);
330 if (handle) {
331 J_ASSERT(handle->h_transaction->t_journal == journal);
332 handle->h_ref++;
333 return handle;
336 handle = new_handle(nblocks);
337 if (!handle)
338 return ERR_PTR(-ENOMEM);
340 current->journal_info = handle;
342 err = start_this_handle(journal, handle, gfp_mask);
343 if (err < 0) {
344 jbd2_free_handle(handle);
345 current->journal_info = NULL;
346 handle = ERR_PTR(err);
348 return handle;
350 EXPORT_SYMBOL(jbd2__journal_start);
353 handle_t *jbd2_journal_start(journal_t *journal, int nblocks)
355 return jbd2__journal_start(journal, nblocks, GFP_NOFS);
357 EXPORT_SYMBOL(jbd2_journal_start);
361 * int jbd2_journal_extend() - extend buffer credits.
362 * @handle: handle to 'extend'
363 * @nblocks: nr blocks to try to extend by.
365 * Some transactions, such as large extends and truncates, can be done
366 * atomically all at once or in several stages. The operation requests
367 * a credit for a number of buffer modications in advance, but can
368 * extend its credit if it needs more.
370 * jbd2_journal_extend tries to give the running handle more buffer credits.
371 * It does not guarantee that allocation - this is a best-effort only.
372 * The calling process MUST be able to deal cleanly with a failure to
373 * extend here.
375 * Return 0 on success, non-zero on failure.
377 * return code < 0 implies an error
378 * return code > 0 implies normal transaction-full status.
380 int jbd2_journal_extend(handle_t *handle, int nblocks)
382 transaction_t *transaction = handle->h_transaction;
383 journal_t *journal = transaction->t_journal;
384 int result;
385 int wanted;
387 result = -EIO;
388 if (is_handle_aborted(handle))
389 goto out;
391 result = 1;
393 read_lock(&journal->j_state_lock);
395 /* Don't extend a locked-down transaction! */
396 if (handle->h_transaction->t_state != T_RUNNING) {
397 jbd_debug(3, "denied handle %p %d blocks: "
398 "transaction not running\n", handle, nblocks);
399 goto error_out;
402 spin_lock(&transaction->t_handle_lock);
403 wanted = atomic_read(&transaction->t_outstanding_credits) + nblocks;
405 if (wanted > journal->j_max_transaction_buffers) {
406 jbd_debug(3, "denied handle %p %d blocks: "
407 "transaction too large\n", handle, nblocks);
408 goto unlock;
411 if (wanted > __jbd2_log_space_left(journal)) {
412 jbd_debug(3, "denied handle %p %d blocks: "
413 "insufficient log space\n", handle, nblocks);
414 goto unlock;
417 handle->h_buffer_credits += nblocks;
418 atomic_add(nblocks, &transaction->t_outstanding_credits);
419 result = 0;
421 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks);
422 unlock:
423 spin_unlock(&transaction->t_handle_lock);
424 error_out:
425 read_unlock(&journal->j_state_lock);
426 out:
427 return result;
432 * int jbd2_journal_restart() - restart a handle .
433 * @handle: handle to restart
434 * @nblocks: nr credits requested
436 * Restart a handle for a multi-transaction filesystem
437 * operation.
439 * If the jbd2_journal_extend() call above fails to grant new buffer credits
440 * to a running handle, a call to jbd2_journal_restart will commit the
441 * handle's transaction so far and reattach the handle to a new
442 * transaction capabable of guaranteeing the requested number of
443 * credits.
445 int jbd2__journal_restart(handle_t *handle, int nblocks, int gfp_mask)
447 transaction_t *transaction = handle->h_transaction;
448 journal_t *journal = transaction->t_journal;
449 tid_t tid;
450 int need_to_start, ret;
452 /* If we've had an abort of any type, don't even think about
453 * actually doing the restart! */
454 if (is_handle_aborted(handle))
455 return 0;
458 * First unlink the handle from its current transaction, and start the
459 * commit on that.
461 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
462 J_ASSERT(journal_current_handle() == handle);
464 read_lock(&journal->j_state_lock);
465 spin_lock(&transaction->t_handle_lock);
466 atomic_sub(handle->h_buffer_credits,
467 &transaction->t_outstanding_credits);
468 if (atomic_dec_and_test(&transaction->t_updates))
469 wake_up(&journal->j_wait_updates);
470 spin_unlock(&transaction->t_handle_lock);
472 jbd_debug(2, "restarting handle %p\n", handle);
473 tid = transaction->t_tid;
474 need_to_start = !tid_geq(journal->j_commit_request, tid);
475 read_unlock(&journal->j_state_lock);
476 if (need_to_start)
477 jbd2_log_start_commit(journal, tid);
479 lock_map_release(&handle->h_lockdep_map);
480 handle->h_buffer_credits = nblocks;
481 ret = start_this_handle(journal, handle, gfp_mask);
482 return ret;
484 EXPORT_SYMBOL(jbd2__journal_restart);
487 int jbd2_journal_restart(handle_t *handle, int nblocks)
489 return jbd2__journal_restart(handle, nblocks, GFP_NOFS);
491 EXPORT_SYMBOL(jbd2_journal_restart);
494 * void jbd2_journal_lock_updates () - establish a transaction barrier.
495 * @journal: Journal to establish a barrier on.
497 * This locks out any further updates from being started, and blocks
498 * until all existing updates have completed, returning only once the
499 * journal is in a quiescent state with no updates running.
501 * The journal lock should not be held on entry.
503 void jbd2_journal_lock_updates(journal_t *journal)
505 DEFINE_WAIT(wait);
507 write_lock(&journal->j_state_lock);
508 ++journal->j_barrier_count;
510 /* Wait until there are no running updates */
511 while (1) {
512 transaction_t *transaction = journal->j_running_transaction;
514 if (!transaction)
515 break;
517 spin_lock(&transaction->t_handle_lock);
518 if (!atomic_read(&transaction->t_updates)) {
519 spin_unlock(&transaction->t_handle_lock);
520 break;
522 prepare_to_wait(&journal->j_wait_updates, &wait,
523 TASK_UNINTERRUPTIBLE);
524 spin_unlock(&transaction->t_handle_lock);
525 write_unlock(&journal->j_state_lock);
526 schedule();
527 finish_wait(&journal->j_wait_updates, &wait);
528 write_lock(&journal->j_state_lock);
530 write_unlock(&journal->j_state_lock);
533 * We have now established a barrier against other normal updates, but
534 * we also need to barrier against other jbd2_journal_lock_updates() calls
535 * to make sure that we serialise special journal-locked operations
536 * too.
538 mutex_lock(&journal->j_barrier);
542 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier
543 * @journal: Journal to release the barrier on.
545 * Release a transaction barrier obtained with jbd2_journal_lock_updates().
547 * Should be called without the journal lock held.
549 void jbd2_journal_unlock_updates (journal_t *journal)
551 J_ASSERT(journal->j_barrier_count != 0);
553 mutex_unlock(&journal->j_barrier);
554 write_lock(&journal->j_state_lock);
555 --journal->j_barrier_count;
556 write_unlock(&journal->j_state_lock);
557 wake_up(&journal->j_wait_transaction_locked);
560 static void warn_dirty_buffer(struct buffer_head *bh)
562 char b[BDEVNAME_SIZE];
564 printk(KERN_WARNING
565 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
566 "There's a risk of filesystem corruption in case of system "
567 "crash.\n",
568 bdevname(bh->b_bdev, b), (unsigned long long)bh->b_blocknr);
572 * If the buffer is already part of the current transaction, then there
573 * is nothing we need to do. If it is already part of a prior
574 * transaction which we are still committing to disk, then we need to
575 * make sure that we do not overwrite the old copy: we do copy-out to
576 * preserve the copy going to disk. We also account the buffer against
577 * the handle's metadata buffer credits (unless the buffer is already
578 * part of the transaction, that is).
581 static int
582 do_get_write_access(handle_t *handle, struct journal_head *jh,
583 int force_copy)
585 struct buffer_head *bh;
586 transaction_t *transaction;
587 journal_t *journal;
588 int error;
589 char *frozen_buffer = NULL;
590 int need_copy = 0;
592 if (is_handle_aborted(handle))
593 return -EROFS;
595 transaction = handle->h_transaction;
596 journal = transaction->t_journal;
598 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy);
600 JBUFFER_TRACE(jh, "entry");
601 repeat:
602 bh = jh2bh(jh);
604 /* @@@ Need to check for errors here at some point. */
606 lock_buffer(bh);
607 jbd_lock_bh_state(bh);
609 /* We now hold the buffer lock so it is safe to query the buffer
610 * state. Is the buffer dirty?
612 * If so, there are two possibilities. The buffer may be
613 * non-journaled, and undergoing a quite legitimate writeback.
614 * Otherwise, it is journaled, and we don't expect dirty buffers
615 * in that state (the buffers should be marked JBD_Dirty
616 * instead.) So either the IO is being done under our own
617 * control and this is a bug, or it's a third party IO such as
618 * dump(8) (which may leave the buffer scheduled for read ---
619 * ie. locked but not dirty) or tune2fs (which may actually have
620 * the buffer dirtied, ugh.) */
622 if (buffer_dirty(bh)) {
624 * First question: is this buffer already part of the current
625 * transaction or the existing committing transaction?
627 if (jh->b_transaction) {
628 J_ASSERT_JH(jh,
629 jh->b_transaction == transaction ||
630 jh->b_transaction ==
631 journal->j_committing_transaction);
632 if (jh->b_next_transaction)
633 J_ASSERT_JH(jh, jh->b_next_transaction ==
634 transaction);
635 warn_dirty_buffer(bh);
638 * In any case we need to clean the dirty flag and we must
639 * do it under the buffer lock to be sure we don't race
640 * with running write-out.
642 JBUFFER_TRACE(jh, "Journalling dirty buffer");
643 clear_buffer_dirty(bh);
644 set_buffer_jbddirty(bh);
647 unlock_buffer(bh);
649 error = -EROFS;
650 if (is_handle_aborted(handle)) {
651 jbd_unlock_bh_state(bh);
652 goto out;
654 error = 0;
657 * The buffer is already part of this transaction if b_transaction or
658 * b_next_transaction points to it
660 if (jh->b_transaction == transaction ||
661 jh->b_next_transaction == transaction)
662 goto done;
665 * this is the first time this transaction is touching this buffer,
666 * reset the modified flag
668 jh->b_modified = 0;
671 * If there is already a copy-out version of this buffer, then we don't
672 * need to make another one
674 if (jh->b_frozen_data) {
675 JBUFFER_TRACE(jh, "has frozen data");
676 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
677 jh->b_next_transaction = transaction;
678 goto done;
681 /* Is there data here we need to preserve? */
683 if (jh->b_transaction && jh->b_transaction != transaction) {
684 JBUFFER_TRACE(jh, "owned by older transaction");
685 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
686 J_ASSERT_JH(jh, jh->b_transaction ==
687 journal->j_committing_transaction);
689 /* There is one case we have to be very careful about.
690 * If the committing transaction is currently writing
691 * this buffer out to disk and has NOT made a copy-out,
692 * then we cannot modify the buffer contents at all
693 * right now. The essence of copy-out is that it is the
694 * extra copy, not the primary copy, which gets
695 * journaled. If the primary copy is already going to
696 * disk then we cannot do copy-out here. */
698 if (jh->b_jlist == BJ_Shadow) {
699 DEFINE_WAIT_BIT(wait, &bh->b_state, BH_Unshadow);
700 wait_queue_head_t *wqh;
702 wqh = bit_waitqueue(&bh->b_state, BH_Unshadow);
704 JBUFFER_TRACE(jh, "on shadow: sleep");
705 jbd_unlock_bh_state(bh);
706 /* commit wakes up all shadow buffers after IO */
707 for ( ; ; ) {
708 prepare_to_wait(wqh, &wait.wait,
709 TASK_UNINTERRUPTIBLE);
710 if (jh->b_jlist != BJ_Shadow)
711 break;
712 schedule();
714 finish_wait(wqh, &wait.wait);
715 goto repeat;
718 /* Only do the copy if the currently-owning transaction
719 * still needs it. If it is on the Forget list, the
720 * committing transaction is past that stage. The
721 * buffer had better remain locked during the kmalloc,
722 * but that should be true --- we hold the journal lock
723 * still and the buffer is already on the BUF_JOURNAL
724 * list so won't be flushed.
726 * Subtle point, though: if this is a get_undo_access,
727 * then we will be relying on the frozen_data to contain
728 * the new value of the committed_data record after the
729 * transaction, so we HAVE to force the frozen_data copy
730 * in that case. */
732 if (jh->b_jlist != BJ_Forget || force_copy) {
733 JBUFFER_TRACE(jh, "generate frozen data");
734 if (!frozen_buffer) {
735 JBUFFER_TRACE(jh, "allocate memory for buffer");
736 jbd_unlock_bh_state(bh);
737 frozen_buffer =
738 jbd2_alloc(jh2bh(jh)->b_size,
739 GFP_NOFS);
740 if (!frozen_buffer) {
741 printk(KERN_EMERG
742 "%s: OOM for frozen_buffer\n",
743 __func__);
744 JBUFFER_TRACE(jh, "oom!");
745 error = -ENOMEM;
746 jbd_lock_bh_state(bh);
747 goto done;
749 goto repeat;
751 jh->b_frozen_data = frozen_buffer;
752 frozen_buffer = NULL;
753 need_copy = 1;
755 jh->b_next_transaction = transaction;
760 * Finally, if the buffer is not journaled right now, we need to make
761 * sure it doesn't get written to disk before the caller actually
762 * commits the new data
764 if (!jh->b_transaction) {
765 JBUFFER_TRACE(jh, "no transaction");
766 J_ASSERT_JH(jh, !jh->b_next_transaction);
767 jh->b_transaction = transaction;
768 JBUFFER_TRACE(jh, "file as BJ_Reserved");
769 spin_lock(&journal->j_list_lock);
770 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
771 spin_unlock(&journal->j_list_lock);
774 done:
775 if (need_copy) {
776 struct page *page;
777 int offset;
778 char *source;
780 J_EXPECT_JH(jh, buffer_uptodate(jh2bh(jh)),
781 "Possible IO failure.\n");
782 page = jh2bh(jh)->b_page;
783 offset = offset_in_page(jh2bh(jh)->b_data);
784 source = kmap_atomic(page, KM_USER0);
785 /* Fire data frozen trigger just before we copy the data */
786 jbd2_buffer_frozen_trigger(jh, source + offset,
787 jh->b_triggers);
788 memcpy(jh->b_frozen_data, source+offset, jh2bh(jh)->b_size);
789 kunmap_atomic(source, KM_USER0);
792 * Now that the frozen data is saved off, we need to store
793 * any matching triggers.
795 jh->b_frozen_triggers = jh->b_triggers;
797 jbd_unlock_bh_state(bh);
800 * If we are about to journal a buffer, then any revoke pending on it is
801 * no longer valid
803 jbd2_journal_cancel_revoke(handle, jh);
805 out:
806 if (unlikely(frozen_buffer)) /* It's usually NULL */
807 jbd2_free(frozen_buffer, bh->b_size);
809 JBUFFER_TRACE(jh, "exit");
810 return error;
814 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
815 * @handle: transaction to add buffer modifications to
816 * @bh: bh to be used for metadata writes
817 * @credits: variable that will receive credits for the buffer
819 * Returns an error code or 0 on success.
821 * In full data journalling mode the buffer may be of type BJ_AsyncData,
822 * because we're write()ing a buffer which is also part of a shared mapping.
825 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh)
827 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
828 int rc;
830 /* We do not want to get caught playing with fields which the
831 * log thread also manipulates. Make sure that the buffer
832 * completes any outstanding IO before proceeding. */
833 rc = do_get_write_access(handle, jh, 0);
834 jbd2_journal_put_journal_head(jh);
835 return rc;
840 * When the user wants to journal a newly created buffer_head
841 * (ie. getblk() returned a new buffer and we are going to populate it
842 * manually rather than reading off disk), then we need to keep the
843 * buffer_head locked until it has been completely filled with new
844 * data. In this case, we should be able to make the assertion that
845 * the bh is not already part of an existing transaction.
847 * The buffer should already be locked by the caller by this point.
848 * There is no lock ranking violation: it was a newly created,
849 * unlocked buffer beforehand. */
852 * int jbd2_journal_get_create_access () - notify intent to use newly created bh
853 * @handle: transaction to new buffer to
854 * @bh: new buffer.
856 * Call this if you create a new bh.
858 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh)
860 transaction_t *transaction = handle->h_transaction;
861 journal_t *journal = transaction->t_journal;
862 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
863 int err;
865 jbd_debug(5, "journal_head %p\n", jh);
866 err = -EROFS;
867 if (is_handle_aborted(handle))
868 goto out;
869 err = 0;
871 JBUFFER_TRACE(jh, "entry");
873 * The buffer may already belong to this transaction due to pre-zeroing
874 * in the filesystem's new_block code. It may also be on the previous,
875 * committing transaction's lists, but it HAS to be in Forget state in
876 * that case: the transaction must have deleted the buffer for it to be
877 * reused here.
879 jbd_lock_bh_state(bh);
880 spin_lock(&journal->j_list_lock);
881 J_ASSERT_JH(jh, (jh->b_transaction == transaction ||
882 jh->b_transaction == NULL ||
883 (jh->b_transaction == journal->j_committing_transaction &&
884 jh->b_jlist == BJ_Forget)));
886 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
887 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh)));
889 if (jh->b_transaction == NULL) {
891 * Previous jbd2_journal_forget() could have left the buffer
892 * with jbddirty bit set because it was being committed. When
893 * the commit finished, we've filed the buffer for
894 * checkpointing and marked it dirty. Now we are reallocating
895 * the buffer so the transaction freeing it must have
896 * committed and so it's safe to clear the dirty bit.
898 clear_buffer_dirty(jh2bh(jh));
899 jh->b_transaction = transaction;
901 /* first access by this transaction */
902 jh->b_modified = 0;
904 JBUFFER_TRACE(jh, "file as BJ_Reserved");
905 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved);
906 } else if (jh->b_transaction == journal->j_committing_transaction) {
907 /* first access by this transaction */
908 jh->b_modified = 0;
910 JBUFFER_TRACE(jh, "set next transaction");
911 jh->b_next_transaction = transaction;
913 spin_unlock(&journal->j_list_lock);
914 jbd_unlock_bh_state(bh);
917 * akpm: I added this. ext3_alloc_branch can pick up new indirect
918 * blocks which contain freed but then revoked metadata. We need
919 * to cancel the revoke in case we end up freeing it yet again
920 * and the reallocating as data - this would cause a second revoke,
921 * which hits an assertion error.
923 JBUFFER_TRACE(jh, "cancelling revoke");
924 jbd2_journal_cancel_revoke(handle, jh);
925 out:
926 jbd2_journal_put_journal_head(jh);
927 return err;
931 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with
932 * non-rewindable consequences
933 * @handle: transaction
934 * @bh: buffer to undo
935 * @credits: store the number of taken credits here (if not NULL)
937 * Sometimes there is a need to distinguish between metadata which has
938 * been committed to disk and that which has not. The ext3fs code uses
939 * this for freeing and allocating space, we have to make sure that we
940 * do not reuse freed space until the deallocation has been committed,
941 * since if we overwrote that space we would make the delete
942 * un-rewindable in case of a crash.
944 * To deal with that, jbd2_journal_get_undo_access requests write access to a
945 * buffer for parts of non-rewindable operations such as delete
946 * operations on the bitmaps. The journaling code must keep a copy of
947 * the buffer's contents prior to the undo_access call until such time
948 * as we know that the buffer has definitely been committed to disk.
950 * We never need to know which transaction the committed data is part
951 * of, buffers touched here are guaranteed to be dirtied later and so
952 * will be committed to a new transaction in due course, at which point
953 * we can discard the old committed data pointer.
955 * Returns error number or 0 on success.
957 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh)
959 int err;
960 struct journal_head *jh = jbd2_journal_add_journal_head(bh);
961 char *committed_data = NULL;
963 JBUFFER_TRACE(jh, "entry");
966 * Do this first --- it can drop the journal lock, so we want to
967 * make sure that obtaining the committed_data is done
968 * atomically wrt. completion of any outstanding commits.
970 err = do_get_write_access(handle, jh, 1);
971 if (err)
972 goto out;
974 repeat:
975 if (!jh->b_committed_data) {
976 committed_data = jbd2_alloc(jh2bh(jh)->b_size, GFP_NOFS);
977 if (!committed_data) {
978 printk(KERN_EMERG "%s: No memory for committed data\n",
979 __func__);
980 err = -ENOMEM;
981 goto out;
985 jbd_lock_bh_state(bh);
986 if (!jh->b_committed_data) {
987 /* Copy out the current buffer contents into the
988 * preserved, committed copy. */
989 JBUFFER_TRACE(jh, "generate b_committed data");
990 if (!committed_data) {
991 jbd_unlock_bh_state(bh);
992 goto repeat;
995 jh->b_committed_data = committed_data;
996 committed_data = NULL;
997 memcpy(jh->b_committed_data, bh->b_data, bh->b_size);
999 jbd_unlock_bh_state(bh);
1000 out:
1001 jbd2_journal_put_journal_head(jh);
1002 if (unlikely(committed_data))
1003 jbd2_free(committed_data, bh->b_size);
1004 return err;
1008 * void jbd2_journal_set_triggers() - Add triggers for commit writeout
1009 * @bh: buffer to trigger on
1010 * @type: struct jbd2_buffer_trigger_type containing the trigger(s).
1012 * Set any triggers on this journal_head. This is always safe, because
1013 * triggers for a committing buffer will be saved off, and triggers for
1014 * a running transaction will match the buffer in that transaction.
1016 * Call with NULL to clear the triggers.
1018 void jbd2_journal_set_triggers(struct buffer_head *bh,
1019 struct jbd2_buffer_trigger_type *type)
1021 struct journal_head *jh = bh2jh(bh);
1023 jh->b_triggers = type;
1026 void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data,
1027 struct jbd2_buffer_trigger_type *triggers)
1029 struct buffer_head *bh = jh2bh(jh);
1031 if (!triggers || !triggers->t_frozen)
1032 return;
1034 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size);
1037 void jbd2_buffer_abort_trigger(struct journal_head *jh,
1038 struct jbd2_buffer_trigger_type *triggers)
1040 if (!triggers || !triggers->t_abort)
1041 return;
1043 triggers->t_abort(triggers, jh2bh(jh));
1049 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata
1050 * @handle: transaction to add buffer to.
1051 * @bh: buffer to mark
1053 * mark dirty metadata which needs to be journaled as part of the current
1054 * transaction.
1056 * The buffer is placed on the transaction's metadata list and is marked
1057 * as belonging to the transaction.
1059 * Returns error number or 0 on success.
1061 * Special care needs to be taken if the buffer already belongs to the
1062 * current committing transaction (in which case we should have frozen
1063 * data present for that commit). In that case, we don't relink the
1064 * buffer: that only gets done when the old transaction finally
1065 * completes its commit.
1067 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh)
1069 transaction_t *transaction = handle->h_transaction;
1070 journal_t *journal = transaction->t_journal;
1071 struct journal_head *jh = bh2jh(bh);
1073 jbd_debug(5, "journal_head %p\n", jh);
1074 JBUFFER_TRACE(jh, "entry");
1075 if (is_handle_aborted(handle))
1076 goto out;
1078 jbd_lock_bh_state(bh);
1080 if (jh->b_modified == 0) {
1082 * This buffer's got modified and becoming part
1083 * of the transaction. This needs to be done
1084 * once a transaction -bzzz
1086 jh->b_modified = 1;
1087 J_ASSERT_JH(jh, handle->h_buffer_credits > 0);
1088 handle->h_buffer_credits--;
1092 * fastpath, to avoid expensive locking. If this buffer is already
1093 * on the running transaction's metadata list there is nothing to do.
1094 * Nobody can take it off again because there is a handle open.
1095 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1096 * result in this test being false, so we go in and take the locks.
1098 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) {
1099 JBUFFER_TRACE(jh, "fastpath");
1100 J_ASSERT_JH(jh, jh->b_transaction ==
1101 journal->j_running_transaction);
1102 goto out_unlock_bh;
1105 set_buffer_jbddirty(bh);
1108 * Metadata already on the current transaction list doesn't
1109 * need to be filed. Metadata on another transaction's list must
1110 * be committing, and will be refiled once the commit completes:
1111 * leave it alone for now.
1113 if (jh->b_transaction != transaction) {
1114 JBUFFER_TRACE(jh, "already on other transaction");
1115 J_ASSERT_JH(jh, jh->b_transaction ==
1116 journal->j_committing_transaction);
1117 J_ASSERT_JH(jh, jh->b_next_transaction == transaction);
1118 /* And this case is illegal: we can't reuse another
1119 * transaction's data buffer, ever. */
1120 goto out_unlock_bh;
1123 /* That test should have eliminated the following case: */
1124 J_ASSERT_JH(jh, jh->b_frozen_data == NULL);
1126 JBUFFER_TRACE(jh, "file as BJ_Metadata");
1127 spin_lock(&journal->j_list_lock);
1128 __jbd2_journal_file_buffer(jh, handle->h_transaction, BJ_Metadata);
1129 spin_unlock(&journal->j_list_lock);
1130 out_unlock_bh:
1131 jbd_unlock_bh_state(bh);
1132 out:
1133 JBUFFER_TRACE(jh, "exit");
1134 return 0;
1138 * jbd2_journal_release_buffer: undo a get_write_access without any buffer
1139 * updates, if the update decided in the end that it didn't need access.
1142 void
1143 jbd2_journal_release_buffer(handle_t *handle, struct buffer_head *bh)
1145 BUFFER_TRACE(bh, "entry");
1149 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers.
1150 * @handle: transaction handle
1151 * @bh: bh to 'forget'
1153 * We can only do the bforget if there are no commits pending against the
1154 * buffer. If the buffer is dirty in the current running transaction we
1155 * can safely unlink it.
1157 * bh may not be a journalled buffer at all - it may be a non-JBD
1158 * buffer which came off the hashtable. Check for this.
1160 * Decrements bh->b_count by one.
1162 * Allow this call even if the handle has aborted --- it may be part of
1163 * the caller's cleanup after an abort.
1165 int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh)
1167 transaction_t *transaction = handle->h_transaction;
1168 journal_t *journal = transaction->t_journal;
1169 struct journal_head *jh;
1170 int drop_reserve = 0;
1171 int err = 0;
1172 int was_modified = 0;
1174 BUFFER_TRACE(bh, "entry");
1176 jbd_lock_bh_state(bh);
1177 spin_lock(&journal->j_list_lock);
1179 if (!buffer_jbd(bh))
1180 goto not_jbd;
1181 jh = bh2jh(bh);
1183 /* Critical error: attempting to delete a bitmap buffer, maybe?
1184 * Don't do any jbd operations, and return an error. */
1185 if (!J_EXPECT_JH(jh, !jh->b_committed_data,
1186 "inconsistent data on disk")) {
1187 err = -EIO;
1188 goto not_jbd;
1191 /* keep track of wether or not this transaction modified us */
1192 was_modified = jh->b_modified;
1195 * The buffer's going from the transaction, we must drop
1196 * all references -bzzz
1198 jh->b_modified = 0;
1200 if (jh->b_transaction == handle->h_transaction) {
1201 J_ASSERT_JH(jh, !jh->b_frozen_data);
1203 /* If we are forgetting a buffer which is already part
1204 * of this transaction, then we can just drop it from
1205 * the transaction immediately. */
1206 clear_buffer_dirty(bh);
1207 clear_buffer_jbddirty(bh);
1209 JBUFFER_TRACE(jh, "belongs to current transaction: unfile");
1212 * we only want to drop a reference if this transaction
1213 * modified the buffer
1215 if (was_modified)
1216 drop_reserve = 1;
1219 * We are no longer going to journal this buffer.
1220 * However, the commit of this transaction is still
1221 * important to the buffer: the delete that we are now
1222 * processing might obsolete an old log entry, so by
1223 * committing, we can satisfy the buffer's checkpoint.
1225 * So, if we have a checkpoint on the buffer, we should
1226 * now refile the buffer on our BJ_Forget list so that
1227 * we know to remove the checkpoint after we commit.
1230 if (jh->b_cp_transaction) {
1231 __jbd2_journal_temp_unlink_buffer(jh);
1232 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1233 } else {
1234 __jbd2_journal_unfile_buffer(jh);
1235 jbd2_journal_remove_journal_head(bh);
1236 __brelse(bh);
1237 if (!buffer_jbd(bh)) {
1238 spin_unlock(&journal->j_list_lock);
1239 jbd_unlock_bh_state(bh);
1240 __bforget(bh);
1241 goto drop;
1244 } else if (jh->b_transaction) {
1245 J_ASSERT_JH(jh, (jh->b_transaction ==
1246 journal->j_committing_transaction));
1247 /* However, if the buffer is still owned by a prior
1248 * (committing) transaction, we can't drop it yet... */
1249 JBUFFER_TRACE(jh, "belongs to older transaction");
1250 /* ... but we CAN drop it from the new transaction if we
1251 * have also modified it since the original commit. */
1253 if (jh->b_next_transaction) {
1254 J_ASSERT(jh->b_next_transaction == transaction);
1255 jh->b_next_transaction = NULL;
1258 * only drop a reference if this transaction modified
1259 * the buffer
1261 if (was_modified)
1262 drop_reserve = 1;
1266 not_jbd:
1267 spin_unlock(&journal->j_list_lock);
1268 jbd_unlock_bh_state(bh);
1269 __brelse(bh);
1270 drop:
1271 if (drop_reserve) {
1272 /* no need to reserve log space for this block -bzzz */
1273 handle->h_buffer_credits++;
1275 return err;
1279 * int jbd2_journal_stop() - complete a transaction
1280 * @handle: tranaction to complete.
1282 * All done for a particular handle.
1284 * There is not much action needed here. We just return any remaining
1285 * buffer credits to the transaction and remove the handle. The only
1286 * complication is that we need to start a commit operation if the
1287 * filesystem is marked for synchronous update.
1289 * jbd2_journal_stop itself will not usually return an error, but it may
1290 * do so in unusual circumstances. In particular, expect it to
1291 * return -EIO if a jbd2_journal_abort has been executed since the
1292 * transaction began.
1294 int jbd2_journal_stop(handle_t *handle)
1296 transaction_t *transaction = handle->h_transaction;
1297 journal_t *journal = transaction->t_journal;
1298 int err, wait_for_commit = 0;
1299 tid_t tid;
1300 pid_t pid;
1302 J_ASSERT(journal_current_handle() == handle);
1304 if (is_handle_aborted(handle))
1305 err = -EIO;
1306 else {
1307 J_ASSERT(atomic_read(&transaction->t_updates) > 0);
1308 err = 0;
1311 if (--handle->h_ref > 0) {
1312 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1,
1313 handle->h_ref);
1314 return err;
1317 jbd_debug(4, "Handle %p going down\n", handle);
1320 * Implement synchronous transaction batching. If the handle
1321 * was synchronous, don't force a commit immediately. Let's
1322 * yield and let another thread piggyback onto this
1323 * transaction. Keep doing that while new threads continue to
1324 * arrive. It doesn't cost much - we're about to run a commit
1325 * and sleep on IO anyway. Speeds up many-threaded, many-dir
1326 * operations by 30x or more...
1328 * We try and optimize the sleep time against what the
1329 * underlying disk can do, instead of having a static sleep
1330 * time. This is useful for the case where our storage is so
1331 * fast that it is more optimal to go ahead and force a flush
1332 * and wait for the transaction to be committed than it is to
1333 * wait for an arbitrary amount of time for new writers to
1334 * join the transaction. We achieve this by measuring how
1335 * long it takes to commit a transaction, and compare it with
1336 * how long this transaction has been running, and if run time
1337 * < commit time then we sleep for the delta and commit. This
1338 * greatly helps super fast disks that would see slowdowns as
1339 * more threads started doing fsyncs.
1341 * But don't do this if this process was the most recent one
1342 * to perform a synchronous write. We do this to detect the
1343 * case where a single process is doing a stream of sync
1344 * writes. No point in waiting for joiners in that case.
1346 pid = current->pid;
1347 if (handle->h_sync && journal->j_last_sync_writer != pid) {
1348 u64 commit_time, trans_time;
1350 journal->j_last_sync_writer = pid;
1352 read_lock(&journal->j_state_lock);
1353 commit_time = journal->j_average_commit_time;
1354 read_unlock(&journal->j_state_lock);
1356 trans_time = ktime_to_ns(ktime_sub(ktime_get(),
1357 transaction->t_start_time));
1359 commit_time = max_t(u64, commit_time,
1360 1000*journal->j_min_batch_time);
1361 commit_time = min_t(u64, commit_time,
1362 1000*journal->j_max_batch_time);
1364 if (trans_time < commit_time) {
1365 ktime_t expires = ktime_add_ns(ktime_get(),
1366 commit_time);
1367 set_current_state(TASK_UNINTERRUPTIBLE);
1368 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
1372 if (handle->h_sync)
1373 transaction->t_synchronous_commit = 1;
1374 current->journal_info = NULL;
1375 atomic_sub(handle->h_buffer_credits,
1376 &transaction->t_outstanding_credits);
1379 * If the handle is marked SYNC, we need to set another commit
1380 * going! We also want to force a commit if the current
1381 * transaction is occupying too much of the log, or if the
1382 * transaction is too old now.
1384 if (handle->h_sync ||
1385 (atomic_read(&transaction->t_outstanding_credits) >
1386 journal->j_max_transaction_buffers) ||
1387 time_after_eq(jiffies, transaction->t_expires)) {
1388 /* Do this even for aborted journals: an abort still
1389 * completes the commit thread, it just doesn't write
1390 * anything to disk. */
1392 jbd_debug(2, "transaction too old, requesting commit for "
1393 "handle %p\n", handle);
1394 /* This is non-blocking */
1395 jbd2_log_start_commit(journal, transaction->t_tid);
1398 * Special case: JBD2_SYNC synchronous updates require us
1399 * to wait for the commit to complete.
1401 if (handle->h_sync && !(current->flags & PF_MEMALLOC))
1402 wait_for_commit = 1;
1406 * Once we drop t_updates, if it goes to zero the transaction
1407 * could start committing on us and eventually disappear. So
1408 * once we do this, we must not dereference transaction
1409 * pointer again.
1411 tid = transaction->t_tid;
1412 if (atomic_dec_and_test(&transaction->t_updates)) {
1413 wake_up(&journal->j_wait_updates);
1414 if (journal->j_barrier_count)
1415 wake_up(&journal->j_wait_transaction_locked);
1418 if (wait_for_commit)
1419 err = jbd2_log_wait_commit(journal, tid);
1421 lock_map_release(&handle->h_lockdep_map);
1423 jbd2_free_handle(handle);
1424 return err;
1428 * int jbd2_journal_force_commit() - force any uncommitted transactions
1429 * @journal: journal to force
1431 * For synchronous operations: force any uncommitted transactions
1432 * to disk. May seem kludgy, but it reuses all the handle batching
1433 * code in a very simple manner.
1435 int jbd2_journal_force_commit(journal_t *journal)
1437 handle_t *handle;
1438 int ret;
1440 handle = jbd2_journal_start(journal, 1);
1441 if (IS_ERR(handle)) {
1442 ret = PTR_ERR(handle);
1443 } else {
1444 handle->h_sync = 1;
1445 ret = jbd2_journal_stop(handle);
1447 return ret;
1452 * List management code snippets: various functions for manipulating the
1453 * transaction buffer lists.
1458 * Append a buffer to a transaction list, given the transaction's list head
1459 * pointer.
1461 * j_list_lock is held.
1463 * jbd_lock_bh_state(jh2bh(jh)) is held.
1466 static inline void
1467 __blist_add_buffer(struct journal_head **list, struct journal_head *jh)
1469 if (!*list) {
1470 jh->b_tnext = jh->b_tprev = jh;
1471 *list = jh;
1472 } else {
1473 /* Insert at the tail of the list to preserve order */
1474 struct journal_head *first = *list, *last = first->b_tprev;
1475 jh->b_tprev = last;
1476 jh->b_tnext = first;
1477 last->b_tnext = first->b_tprev = jh;
1482 * Remove a buffer from a transaction list, given the transaction's list
1483 * head pointer.
1485 * Called with j_list_lock held, and the journal may not be locked.
1487 * jbd_lock_bh_state(jh2bh(jh)) is held.
1490 static inline void
1491 __blist_del_buffer(struct journal_head **list, struct journal_head *jh)
1493 if (*list == jh) {
1494 *list = jh->b_tnext;
1495 if (*list == jh)
1496 *list = NULL;
1498 jh->b_tprev->b_tnext = jh->b_tnext;
1499 jh->b_tnext->b_tprev = jh->b_tprev;
1503 * Remove a buffer from the appropriate transaction list.
1505 * Note that this function can *change* the value of
1506 * bh->b_transaction->t_buffers, t_forget, t_iobuf_list, t_shadow_list,
1507 * t_log_list or t_reserved_list. If the caller is holding onto a copy of one
1508 * of these pointers, it could go bad. Generally the caller needs to re-read
1509 * the pointer from the transaction_t.
1511 * Called under j_list_lock. The journal may not be locked.
1513 void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh)
1515 struct journal_head **list = NULL;
1516 transaction_t *transaction;
1517 struct buffer_head *bh = jh2bh(jh);
1519 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1520 transaction = jh->b_transaction;
1521 if (transaction)
1522 assert_spin_locked(&transaction->t_journal->j_list_lock);
1524 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1525 if (jh->b_jlist != BJ_None)
1526 J_ASSERT_JH(jh, transaction != NULL);
1528 switch (jh->b_jlist) {
1529 case BJ_None:
1530 return;
1531 case BJ_Metadata:
1532 transaction->t_nr_buffers--;
1533 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0);
1534 list = &transaction->t_buffers;
1535 break;
1536 case BJ_Forget:
1537 list = &transaction->t_forget;
1538 break;
1539 case BJ_IO:
1540 list = &transaction->t_iobuf_list;
1541 break;
1542 case BJ_Shadow:
1543 list = &transaction->t_shadow_list;
1544 break;
1545 case BJ_LogCtl:
1546 list = &transaction->t_log_list;
1547 break;
1548 case BJ_Reserved:
1549 list = &transaction->t_reserved_list;
1550 break;
1553 __blist_del_buffer(list, jh);
1554 jh->b_jlist = BJ_None;
1555 if (test_clear_buffer_jbddirty(bh))
1556 mark_buffer_dirty(bh); /* Expose it to the VM */
1559 void __jbd2_journal_unfile_buffer(struct journal_head *jh)
1561 __jbd2_journal_temp_unlink_buffer(jh);
1562 jh->b_transaction = NULL;
1565 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh)
1567 jbd_lock_bh_state(jh2bh(jh));
1568 spin_lock(&journal->j_list_lock);
1569 __jbd2_journal_unfile_buffer(jh);
1570 spin_unlock(&journal->j_list_lock);
1571 jbd_unlock_bh_state(jh2bh(jh));
1575 * Called from jbd2_journal_try_to_free_buffers().
1577 * Called under jbd_lock_bh_state(bh)
1579 static void
1580 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh)
1582 struct journal_head *jh;
1584 jh = bh2jh(bh);
1586 if (buffer_locked(bh) || buffer_dirty(bh))
1587 goto out;
1589 if (jh->b_next_transaction != NULL)
1590 goto out;
1592 spin_lock(&journal->j_list_lock);
1593 if (jh->b_cp_transaction != NULL && jh->b_transaction == NULL) {
1594 /* written-back checkpointed metadata buffer */
1595 if (jh->b_jlist == BJ_None) {
1596 JBUFFER_TRACE(jh, "remove from checkpoint list");
1597 __jbd2_journal_remove_checkpoint(jh);
1598 jbd2_journal_remove_journal_head(bh);
1599 __brelse(bh);
1602 spin_unlock(&journal->j_list_lock);
1603 out:
1604 return;
1608 * int jbd2_journal_try_to_free_buffers() - try to free page buffers.
1609 * @journal: journal for operation
1610 * @page: to try and free
1611 * @gfp_mask: we use the mask to detect how hard should we try to release
1612 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1613 * release the buffers.
1616 * For all the buffers on this page,
1617 * if they are fully written out ordered data, move them onto BUF_CLEAN
1618 * so try_to_free_buffers() can reap them.
1620 * This function returns non-zero if we wish try_to_free_buffers()
1621 * to be called. We do this if the page is releasable by try_to_free_buffers().
1622 * We also do it if the page has locked or dirty buffers and the caller wants
1623 * us to perform sync or async writeout.
1625 * This complicates JBD locking somewhat. We aren't protected by the
1626 * BKL here. We wish to remove the buffer from its committing or
1627 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer.
1629 * This may *change* the value of transaction_t->t_datalist, so anyone
1630 * who looks at t_datalist needs to lock against this function.
1632 * Even worse, someone may be doing a jbd2_journal_dirty_data on this
1633 * buffer. So we need to lock against that. jbd2_journal_dirty_data()
1634 * will come out of the lock with the buffer dirty, which makes it
1635 * ineligible for release here.
1637 * Who else is affected by this? hmm... Really the only contender
1638 * is do_get_write_access() - it could be looking at the buffer while
1639 * journal_try_to_free_buffer() is changing its state. But that
1640 * cannot happen because we never reallocate freed data as metadata
1641 * while the data is part of a transaction. Yes?
1643 * Return 0 on failure, 1 on success
1645 int jbd2_journal_try_to_free_buffers(journal_t *journal,
1646 struct page *page, gfp_t gfp_mask)
1648 struct buffer_head *head;
1649 struct buffer_head *bh;
1650 int ret = 0;
1652 J_ASSERT(PageLocked(page));
1654 head = page_buffers(page);
1655 bh = head;
1656 do {
1657 struct journal_head *jh;
1660 * We take our own ref against the journal_head here to avoid
1661 * having to add tons of locking around each instance of
1662 * jbd2_journal_remove_journal_head() and
1663 * jbd2_journal_put_journal_head().
1665 jh = jbd2_journal_grab_journal_head(bh);
1666 if (!jh)
1667 continue;
1669 jbd_lock_bh_state(bh);
1670 __journal_try_to_free_buffer(journal, bh);
1671 jbd2_journal_put_journal_head(jh);
1672 jbd_unlock_bh_state(bh);
1673 if (buffer_jbd(bh))
1674 goto busy;
1675 } while ((bh = bh->b_this_page) != head);
1677 ret = try_to_free_buffers(page);
1679 busy:
1680 return ret;
1684 * This buffer is no longer needed. If it is on an older transaction's
1685 * checkpoint list we need to record it on this transaction's forget list
1686 * to pin this buffer (and hence its checkpointing transaction) down until
1687 * this transaction commits. If the buffer isn't on a checkpoint list, we
1688 * release it.
1689 * Returns non-zero if JBD no longer has an interest in the buffer.
1691 * Called under j_list_lock.
1693 * Called under jbd_lock_bh_state(bh).
1695 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction)
1697 int may_free = 1;
1698 struct buffer_head *bh = jh2bh(jh);
1700 __jbd2_journal_unfile_buffer(jh);
1702 if (jh->b_cp_transaction) {
1703 JBUFFER_TRACE(jh, "on running+cp transaction");
1705 * We don't want to write the buffer anymore, clear the
1706 * bit so that we don't confuse checks in
1707 * __journal_file_buffer
1709 clear_buffer_dirty(bh);
1710 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget);
1711 may_free = 0;
1712 } else {
1713 JBUFFER_TRACE(jh, "on running transaction");
1714 jbd2_journal_remove_journal_head(bh);
1715 __brelse(bh);
1717 return may_free;
1721 * jbd2_journal_invalidatepage
1723 * This code is tricky. It has a number of cases to deal with.
1725 * There are two invariants which this code relies on:
1727 * i_size must be updated on disk before we start calling invalidatepage on the
1728 * data.
1730 * This is done in ext3 by defining an ext3_setattr method which
1731 * updates i_size before truncate gets going. By maintaining this
1732 * invariant, we can be sure that it is safe to throw away any buffers
1733 * attached to the current transaction: once the transaction commits,
1734 * we know that the data will not be needed.
1736 * Note however that we can *not* throw away data belonging to the
1737 * previous, committing transaction!
1739 * Any disk blocks which *are* part of the previous, committing
1740 * transaction (and which therefore cannot be discarded immediately) are
1741 * not going to be reused in the new running transaction
1743 * The bitmap committed_data images guarantee this: any block which is
1744 * allocated in one transaction and removed in the next will be marked
1745 * as in-use in the committed_data bitmap, so cannot be reused until
1746 * the next transaction to delete the block commits. This means that
1747 * leaving committing buffers dirty is quite safe: the disk blocks
1748 * cannot be reallocated to a different file and so buffer aliasing is
1749 * not possible.
1752 * The above applies mainly to ordered data mode. In writeback mode we
1753 * don't make guarantees about the order in which data hits disk --- in
1754 * particular we don't guarantee that new dirty data is flushed before
1755 * transaction commit --- so it is always safe just to discard data
1756 * immediately in that mode. --sct
1760 * The journal_unmap_buffer helper function returns zero if the buffer
1761 * concerned remains pinned as an anonymous buffer belonging to an older
1762 * transaction.
1764 * We're outside-transaction here. Either or both of j_running_transaction
1765 * and j_committing_transaction may be NULL.
1767 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh)
1769 transaction_t *transaction;
1770 struct journal_head *jh;
1771 int may_free = 1;
1772 int ret;
1774 BUFFER_TRACE(bh, "entry");
1777 * It is safe to proceed here without the j_list_lock because the
1778 * buffers cannot be stolen by try_to_free_buffers as long as we are
1779 * holding the page lock. --sct
1782 if (!buffer_jbd(bh))
1783 goto zap_buffer_unlocked;
1785 /* OK, we have data buffer in journaled mode */
1786 write_lock(&journal->j_state_lock);
1787 jbd_lock_bh_state(bh);
1788 spin_lock(&journal->j_list_lock);
1790 jh = jbd2_journal_grab_journal_head(bh);
1791 if (!jh)
1792 goto zap_buffer_no_jh;
1795 * We cannot remove the buffer from checkpoint lists until the
1796 * transaction adding inode to orphan list (let's call it T)
1797 * is committed. Otherwise if the transaction changing the
1798 * buffer would be cleaned from the journal before T is
1799 * committed, a crash will cause that the correct contents of
1800 * the buffer will be lost. On the other hand we have to
1801 * clear the buffer dirty bit at latest at the moment when the
1802 * transaction marking the buffer as freed in the filesystem
1803 * structures is committed because from that moment on the
1804 * buffer can be reallocated and used by a different page.
1805 * Since the block hasn't been freed yet but the inode has
1806 * already been added to orphan list, it is safe for us to add
1807 * the buffer to BJ_Forget list of the newest transaction.
1809 transaction = jh->b_transaction;
1810 if (transaction == NULL) {
1811 /* First case: not on any transaction. If it
1812 * has no checkpoint link, then we can zap it:
1813 * it's a writeback-mode buffer so we don't care
1814 * if it hits disk safely. */
1815 if (!jh->b_cp_transaction) {
1816 JBUFFER_TRACE(jh, "not on any transaction: zap");
1817 goto zap_buffer;
1820 if (!buffer_dirty(bh)) {
1821 /* bdflush has written it. We can drop it now */
1822 goto zap_buffer;
1825 /* OK, it must be in the journal but still not
1826 * written fully to disk: it's metadata or
1827 * journaled data... */
1829 if (journal->j_running_transaction) {
1830 /* ... and once the current transaction has
1831 * committed, the buffer won't be needed any
1832 * longer. */
1833 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget");
1834 ret = __dispose_buffer(jh,
1835 journal->j_running_transaction);
1836 jbd2_journal_put_journal_head(jh);
1837 spin_unlock(&journal->j_list_lock);
1838 jbd_unlock_bh_state(bh);
1839 write_unlock(&journal->j_state_lock);
1840 return ret;
1841 } else {
1842 /* There is no currently-running transaction. So the
1843 * orphan record which we wrote for this file must have
1844 * passed into commit. We must attach this buffer to
1845 * the committing transaction, if it exists. */
1846 if (journal->j_committing_transaction) {
1847 JBUFFER_TRACE(jh, "give to committing trans");
1848 ret = __dispose_buffer(jh,
1849 journal->j_committing_transaction);
1850 jbd2_journal_put_journal_head(jh);
1851 spin_unlock(&journal->j_list_lock);
1852 jbd_unlock_bh_state(bh);
1853 write_unlock(&journal->j_state_lock);
1854 return ret;
1855 } else {
1856 /* The orphan record's transaction has
1857 * committed. We can cleanse this buffer */
1858 clear_buffer_jbddirty(bh);
1859 goto zap_buffer;
1862 } else if (transaction == journal->j_committing_transaction) {
1863 JBUFFER_TRACE(jh, "on committing transaction");
1865 * The buffer is committing, we simply cannot touch
1866 * it. So we just set j_next_transaction to the
1867 * running transaction (if there is one) and mark
1868 * buffer as freed so that commit code knows it should
1869 * clear dirty bits when it is done with the buffer.
1871 set_buffer_freed(bh);
1872 if (journal->j_running_transaction && buffer_jbddirty(bh))
1873 jh->b_next_transaction = journal->j_running_transaction;
1874 jbd2_journal_put_journal_head(jh);
1875 spin_unlock(&journal->j_list_lock);
1876 jbd_unlock_bh_state(bh);
1877 write_unlock(&journal->j_state_lock);
1878 return 0;
1879 } else {
1880 /* Good, the buffer belongs to the running transaction.
1881 * We are writing our own transaction's data, not any
1882 * previous one's, so it is safe to throw it away
1883 * (remember that we expect the filesystem to have set
1884 * i_size already for this truncate so recovery will not
1885 * expose the disk blocks we are discarding here.) */
1886 J_ASSERT_JH(jh, transaction == journal->j_running_transaction);
1887 JBUFFER_TRACE(jh, "on running transaction");
1888 may_free = __dispose_buffer(jh, transaction);
1891 zap_buffer:
1892 jbd2_journal_put_journal_head(jh);
1893 zap_buffer_no_jh:
1894 spin_unlock(&journal->j_list_lock);
1895 jbd_unlock_bh_state(bh);
1896 write_unlock(&journal->j_state_lock);
1897 zap_buffer_unlocked:
1898 clear_buffer_dirty(bh);
1899 J_ASSERT_BH(bh, !buffer_jbddirty(bh));
1900 clear_buffer_mapped(bh);
1901 clear_buffer_req(bh);
1902 clear_buffer_new(bh);
1903 bh->b_bdev = NULL;
1904 return may_free;
1908 * void jbd2_journal_invalidatepage()
1909 * @journal: journal to use for flush...
1910 * @page: page to flush
1911 * @offset: length of page to invalidate.
1913 * Reap page buffers containing data after offset in page.
1916 void jbd2_journal_invalidatepage(journal_t *journal,
1917 struct page *page,
1918 unsigned long offset)
1920 struct buffer_head *head, *bh, *next;
1921 unsigned int curr_off = 0;
1922 int may_free = 1;
1924 if (!PageLocked(page))
1925 BUG();
1926 if (!page_has_buffers(page))
1927 return;
1929 /* We will potentially be playing with lists other than just the
1930 * data lists (especially for journaled data mode), so be
1931 * cautious in our locking. */
1933 head = bh = page_buffers(page);
1934 do {
1935 unsigned int next_off = curr_off + bh->b_size;
1936 next = bh->b_this_page;
1938 if (offset <= curr_off) {
1939 /* This block is wholly outside the truncation point */
1940 lock_buffer(bh);
1941 may_free &= journal_unmap_buffer(journal, bh);
1942 unlock_buffer(bh);
1944 curr_off = next_off;
1945 bh = next;
1947 } while (bh != head);
1949 if (!offset) {
1950 if (may_free && try_to_free_buffers(page))
1951 J_ASSERT(!page_has_buffers(page));
1956 * File a buffer on the given transaction list.
1958 void __jbd2_journal_file_buffer(struct journal_head *jh,
1959 transaction_t *transaction, int jlist)
1961 struct journal_head **list = NULL;
1962 int was_dirty = 0;
1963 struct buffer_head *bh = jh2bh(jh);
1965 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
1966 assert_spin_locked(&transaction->t_journal->j_list_lock);
1968 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types);
1969 J_ASSERT_JH(jh, jh->b_transaction == transaction ||
1970 jh->b_transaction == NULL);
1972 if (jh->b_transaction && jh->b_jlist == jlist)
1973 return;
1975 if (jlist == BJ_Metadata || jlist == BJ_Reserved ||
1976 jlist == BJ_Shadow || jlist == BJ_Forget) {
1978 * For metadata buffers, we track dirty bit in buffer_jbddirty
1979 * instead of buffer_dirty. We should not see a dirty bit set
1980 * here because we clear it in do_get_write_access but e.g.
1981 * tune2fs can modify the sb and set the dirty bit at any time
1982 * so we try to gracefully handle that.
1984 if (buffer_dirty(bh))
1985 warn_dirty_buffer(bh);
1986 if (test_clear_buffer_dirty(bh) ||
1987 test_clear_buffer_jbddirty(bh))
1988 was_dirty = 1;
1991 if (jh->b_transaction)
1992 __jbd2_journal_temp_unlink_buffer(jh);
1993 jh->b_transaction = transaction;
1995 switch (jlist) {
1996 case BJ_None:
1997 J_ASSERT_JH(jh, !jh->b_committed_data);
1998 J_ASSERT_JH(jh, !jh->b_frozen_data);
1999 return;
2000 case BJ_Metadata:
2001 transaction->t_nr_buffers++;
2002 list = &transaction->t_buffers;
2003 break;
2004 case BJ_Forget:
2005 list = &transaction->t_forget;
2006 break;
2007 case BJ_IO:
2008 list = &transaction->t_iobuf_list;
2009 break;
2010 case BJ_Shadow:
2011 list = &transaction->t_shadow_list;
2012 break;
2013 case BJ_LogCtl:
2014 list = &transaction->t_log_list;
2015 break;
2016 case BJ_Reserved:
2017 list = &transaction->t_reserved_list;
2018 break;
2021 __blist_add_buffer(list, jh);
2022 jh->b_jlist = jlist;
2024 if (was_dirty)
2025 set_buffer_jbddirty(bh);
2028 void jbd2_journal_file_buffer(struct journal_head *jh,
2029 transaction_t *transaction, int jlist)
2031 jbd_lock_bh_state(jh2bh(jh));
2032 spin_lock(&transaction->t_journal->j_list_lock);
2033 __jbd2_journal_file_buffer(jh, transaction, jlist);
2034 spin_unlock(&transaction->t_journal->j_list_lock);
2035 jbd_unlock_bh_state(jh2bh(jh));
2039 * Remove a buffer from its current buffer list in preparation for
2040 * dropping it from its current transaction entirely. If the buffer has
2041 * already started to be used by a subsequent transaction, refile the
2042 * buffer on that transaction's metadata list.
2044 * Called under journal->j_list_lock
2046 * Called under jbd_lock_bh_state(jh2bh(jh))
2048 void __jbd2_journal_refile_buffer(struct journal_head *jh)
2050 int was_dirty, jlist;
2051 struct buffer_head *bh = jh2bh(jh);
2053 J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh));
2054 if (jh->b_transaction)
2055 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock);
2057 /* If the buffer is now unused, just drop it. */
2058 if (jh->b_next_transaction == NULL) {
2059 __jbd2_journal_unfile_buffer(jh);
2060 return;
2064 * It has been modified by a later transaction: add it to the new
2065 * transaction's metadata list.
2068 was_dirty = test_clear_buffer_jbddirty(bh);
2069 __jbd2_journal_temp_unlink_buffer(jh);
2070 jh->b_transaction = jh->b_next_transaction;
2071 jh->b_next_transaction = NULL;
2072 if (buffer_freed(bh))
2073 jlist = BJ_Forget;
2074 else if (jh->b_modified)
2075 jlist = BJ_Metadata;
2076 else
2077 jlist = BJ_Reserved;
2078 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist);
2079 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING);
2081 if (was_dirty)
2082 set_buffer_jbddirty(bh);
2086 * For the unlocked version of this call, also make sure that any
2087 * hanging journal_head is cleaned up if necessary.
2089 * __jbd2_journal_refile_buffer is usually called as part of a single locked
2090 * operation on a buffer_head, in which the caller is probably going to
2091 * be hooking the journal_head onto other lists. In that case it is up
2092 * to the caller to remove the journal_head if necessary. For the
2093 * unlocked jbd2_journal_refile_buffer call, the caller isn't going to be
2094 * doing anything else to the buffer so we need to do the cleanup
2095 * ourselves to avoid a jh leak.
2097 * *** The journal_head may be freed by this call! ***
2099 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh)
2101 struct buffer_head *bh = jh2bh(jh);
2103 jbd_lock_bh_state(bh);
2104 spin_lock(&journal->j_list_lock);
2106 __jbd2_journal_refile_buffer(jh);
2107 jbd_unlock_bh_state(bh);
2108 jbd2_journal_remove_journal_head(bh);
2110 spin_unlock(&journal->j_list_lock);
2111 __brelse(bh);
2115 * File inode in the inode list of the handle's transaction
2117 int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode)
2119 transaction_t *transaction = handle->h_transaction;
2120 journal_t *journal = transaction->t_journal;
2122 if (is_handle_aborted(handle))
2123 return -EIO;
2125 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino,
2126 transaction->t_tid);
2129 * First check whether inode isn't already on the transaction's
2130 * lists without taking the lock. Note that this check is safe
2131 * without the lock as we cannot race with somebody removing inode
2132 * from the transaction. The reason is that we remove inode from the
2133 * transaction only in journal_release_jbd_inode() and when we commit
2134 * the transaction. We are guarded from the first case by holding
2135 * a reference to the inode. We are safe against the second case
2136 * because if jinode->i_transaction == transaction, commit code
2137 * cannot touch the transaction because we hold reference to it,
2138 * and if jinode->i_next_transaction == transaction, commit code
2139 * will only file the inode where we want it.
2141 if (jinode->i_transaction == transaction ||
2142 jinode->i_next_transaction == transaction)
2143 return 0;
2145 spin_lock(&journal->j_list_lock);
2147 if (jinode->i_transaction == transaction ||
2148 jinode->i_next_transaction == transaction)
2149 goto done;
2152 * We only ever set this variable to 1 so the test is safe. Since
2153 * t_need_data_flush is likely to be set, we do the test to save some
2154 * cacheline bouncing
2156 if (!transaction->t_need_data_flush)
2157 transaction->t_need_data_flush = 1;
2158 /* On some different transaction's list - should be
2159 * the committing one */
2160 if (jinode->i_transaction) {
2161 J_ASSERT(jinode->i_next_transaction == NULL);
2162 J_ASSERT(jinode->i_transaction ==
2163 journal->j_committing_transaction);
2164 jinode->i_next_transaction = transaction;
2165 goto done;
2167 /* Not on any transaction list... */
2168 J_ASSERT(!jinode->i_next_transaction);
2169 jinode->i_transaction = transaction;
2170 list_add(&jinode->i_list, &transaction->t_inode_list);
2171 done:
2172 spin_unlock(&journal->j_list_lock);
2174 return 0;
2178 * File truncate and transaction commit interact with each other in a
2179 * non-trivial way. If a transaction writing data block A is
2180 * committing, we cannot discard the data by truncate until we have
2181 * written them. Otherwise if we crashed after the transaction with
2182 * write has committed but before the transaction with truncate has
2183 * committed, we could see stale data in block A. This function is a
2184 * helper to solve this problem. It starts writeout of the truncated
2185 * part in case it is in the committing transaction.
2187 * Filesystem code must call this function when inode is journaled in
2188 * ordered mode before truncation happens and after the inode has been
2189 * placed on orphan list with the new inode size. The second condition
2190 * avoids the race that someone writes new data and we start
2191 * committing the transaction after this function has been called but
2192 * before a transaction for truncate is started (and furthermore it
2193 * allows us to optimize the case where the addition to orphan list
2194 * happens in the same transaction as write --- we don't have to write
2195 * any data in such case).
2197 int jbd2_journal_begin_ordered_truncate(journal_t *journal,
2198 struct jbd2_inode *jinode,
2199 loff_t new_size)
2201 transaction_t *inode_trans, *commit_trans;
2202 int ret = 0;
2204 /* This is a quick check to avoid locking if not necessary */
2205 if (!jinode->i_transaction)
2206 goto out;
2207 /* Locks are here just to force reading of recent values, it is
2208 * enough that the transaction was not committing before we started
2209 * a transaction adding the inode to orphan list */
2210 read_lock(&journal->j_state_lock);
2211 commit_trans = journal->j_committing_transaction;
2212 read_unlock(&journal->j_state_lock);
2213 spin_lock(&journal->j_list_lock);
2214 inode_trans = jinode->i_transaction;
2215 spin_unlock(&journal->j_list_lock);
2216 if (inode_trans == commit_trans) {
2217 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
2218 new_size, LLONG_MAX);
2219 if (ret)
2220 jbd2_journal_abort(journal, ret);
2222 out:
2223 return ret;