2 * linux/fs/jbd/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
15 * This file manages transactions (compound commits managed by the
16 * journaling code) and handles (individual atomic operations by the
20 #include <linux/time.h>
22 #include <linux/jbd.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
27 #include <linux/highmem.h>
28 #include <linux/hrtimer.h>
29 #include <linux/backing-dev.h>
31 static void __journal_temp_unlink_buffer(struct journal_head
*jh
);
34 * get_transaction: obtain a new transaction_t object.
36 * Simply allocate and initialise a new transaction. Create it in
37 * RUNNING state and add it to the current journal (which should not
38 * have an existing running transaction: we only make a new transaction
39 * once we have started to commit the old one).
42 * The journal MUST be locked. We don't perform atomic mallocs on the
43 * new transaction and we can't block without protecting against other
44 * processes trying to touch the journal while it is in transition.
46 * Called under j_state_lock
49 static transaction_t
*
50 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
);
59 /* Set up the commit timer for the new transaction. */
60 journal
->j_commit_timer
.expires
=
61 round_jiffies_up(transaction
->t_expires
);
62 add_timer(&journal
->j_commit_timer
);
64 J_ASSERT(journal
->j_running_transaction
== NULL
);
65 journal
->j_running_transaction
= transaction
;
73 * A handle_t is an object which represents a single atomic update to a
74 * filesystem, and which tracks all of the modifications which form part
79 * start_this_handle: Given a handle, deal with any locking or stalling
80 * needed to make sure that there is enough journal space for the handle
81 * to begin. Attach the handle to a transaction and set up the
82 * transaction's buffer credits.
85 static int start_this_handle(journal_t
*journal
, handle_t
*handle
)
87 transaction_t
*transaction
;
89 int nblocks
= handle
->h_buffer_credits
;
90 transaction_t
*new_transaction
= NULL
;
93 if (nblocks
> journal
->j_max_transaction_buffers
) {
94 printk(KERN_ERR
"JBD: %s wants too many credits (%d > %d)\n",
95 current
->comm
, nblocks
,
96 journal
->j_max_transaction_buffers
);
102 if (!journal
->j_running_transaction
) {
103 new_transaction
= kzalloc(sizeof(*new_transaction
), GFP_NOFS
);
104 if (!new_transaction
) {
105 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
106 goto alloc_transaction
;
110 jbd_debug(3, "New handle %p going live.\n", handle
);
115 * We need to hold j_state_lock until t_updates has been incremented,
116 * for proper journal barrier handling
118 spin_lock(&journal
->j_state_lock
);
120 if (is_journal_aborted(journal
) ||
121 (journal
->j_errno
!= 0 && !(journal
->j_flags
& JFS_ACK_ERR
))) {
122 spin_unlock(&journal
->j_state_lock
);
127 /* Wait on the journal's transaction barrier if necessary */
128 if (journal
->j_barrier_count
) {
129 spin_unlock(&journal
->j_state_lock
);
130 wait_event(journal
->j_wait_transaction_locked
,
131 journal
->j_barrier_count
== 0);
135 if (!journal
->j_running_transaction
) {
136 if (!new_transaction
) {
137 spin_unlock(&journal
->j_state_lock
);
138 goto alloc_transaction
;
140 get_transaction(journal
, new_transaction
);
141 new_transaction
= NULL
;
144 transaction
= journal
->j_running_transaction
;
147 * If the current transaction is locked down for commit, wait for the
148 * lock to be released.
150 if (transaction
->t_state
== T_LOCKED
) {
153 prepare_to_wait(&journal
->j_wait_transaction_locked
,
154 &wait
, TASK_UNINTERRUPTIBLE
);
155 spin_unlock(&journal
->j_state_lock
);
157 finish_wait(&journal
->j_wait_transaction_locked
, &wait
);
162 * If there is not enough space left in the log to write all potential
163 * buffers requested by this operation, we need to stall pending a log
164 * checkpoint to free some more log space.
166 spin_lock(&transaction
->t_handle_lock
);
167 needed
= transaction
->t_outstanding_credits
+ nblocks
;
169 if (needed
> journal
->j_max_transaction_buffers
) {
171 * If the current transaction is already too large, then start
172 * to commit it: we can then go back and attach this handle to
177 jbd_debug(2, "Handle %p starting new commit...\n", handle
);
178 spin_unlock(&transaction
->t_handle_lock
);
179 prepare_to_wait(&journal
->j_wait_transaction_locked
, &wait
,
180 TASK_UNINTERRUPTIBLE
);
181 __log_start_commit(journal
, transaction
->t_tid
);
182 spin_unlock(&journal
->j_state_lock
);
184 finish_wait(&journal
->j_wait_transaction_locked
, &wait
);
189 * The commit code assumes that it can get enough log space
190 * without forcing a checkpoint. This is *critical* for
191 * correctness: a checkpoint of a buffer which is also
192 * associated with a committing transaction creates a deadlock,
193 * so commit simply cannot force through checkpoints.
195 * We must therefore ensure the necessary space in the journal
196 * *before* starting to dirty potentially checkpointed buffers
197 * in the new transaction.
199 * The worst part is, any transaction currently committing can
200 * reduce the free space arbitrarily. Be careful to account for
201 * those buffers when checkpointing.
205 * @@@ AKPM: This seems rather over-defensive. We're giving commit
206 * a _lot_ of headroom: 1/4 of the journal plus the size of
207 * the committing transaction. Really, we only need to give it
208 * committing_transaction->t_outstanding_credits plus "enough" for
209 * the log control blocks.
210 * Also, this test is inconsistent with the matching one in
213 if (__log_space_left(journal
) < jbd_space_needed(journal
)) {
214 jbd_debug(2, "Handle %p waiting for checkpoint...\n", handle
);
215 spin_unlock(&transaction
->t_handle_lock
);
216 __log_wait_for_space(journal
);
220 /* OK, account for the buffers that this operation expects to
221 * use and add the handle to the running transaction. */
223 handle
->h_transaction
= transaction
;
224 transaction
->t_outstanding_credits
+= nblocks
;
225 transaction
->t_updates
++;
226 transaction
->t_handle_count
++;
227 jbd_debug(4, "Handle %p given %d credits (total %d, free %d)\n",
228 handle
, nblocks
, transaction
->t_outstanding_credits
,
229 __log_space_left(journal
));
230 spin_unlock(&transaction
->t_handle_lock
);
231 spin_unlock(&journal
->j_state_lock
);
233 lock_map_acquire(&handle
->h_lockdep_map
);
235 if (unlikely(new_transaction
)) /* It's usually NULL */
236 kfree(new_transaction
);
240 static struct lock_class_key jbd_handle_key
;
242 /* Allocate a new handle. This should probably be in a slab... */
243 static handle_t
*new_handle(int nblocks
)
245 handle_t
*handle
= jbd_alloc_handle(GFP_NOFS
);
248 handle
->h_buffer_credits
= nblocks
;
251 lockdep_init_map(&handle
->h_lockdep_map
, "jbd_handle", &jbd_handle_key
, 0);
257 * handle_t *journal_start() - Obtain a new handle.
258 * @journal: Journal to start transaction on.
259 * @nblocks: number of block buffer we might modify
261 * We make sure that the transaction can guarantee at least nblocks of
262 * modified buffers in the log. We block until the log can guarantee
265 * This function is visible to journal users (like ext3fs), so is not
266 * called with the journal already locked.
268 * Return a pointer to a newly allocated handle, or an ERR_PTR() value
271 handle_t
*journal_start(journal_t
*journal
, int nblocks
)
273 handle_t
*handle
= journal_current_handle();
277 return ERR_PTR(-EROFS
);
280 J_ASSERT(handle
->h_transaction
->t_journal
== journal
);
285 handle
= new_handle(nblocks
);
287 return ERR_PTR(-ENOMEM
);
289 current
->journal_info
= handle
;
291 err
= start_this_handle(journal
, handle
);
293 jbd_free_handle(handle
);
294 current
->journal_info
= NULL
;
295 handle
= ERR_PTR(err
);
301 * int journal_extend() - extend buffer credits.
302 * @handle: handle to 'extend'
303 * @nblocks: nr blocks to try to extend by.
305 * Some transactions, such as large extends and truncates, can be done
306 * atomically all at once or in several stages. The operation requests
307 * a credit for a number of buffer modications in advance, but can
308 * extend its credit if it needs more.
310 * journal_extend tries to give the running handle more buffer credits.
311 * It does not guarantee that allocation - this is a best-effort only.
312 * The calling process MUST be able to deal cleanly with a failure to
315 * Return 0 on success, non-zero on failure.
317 * return code < 0 implies an error
318 * return code > 0 implies normal transaction-full status.
320 int journal_extend(handle_t
*handle
, int nblocks
)
322 transaction_t
*transaction
= handle
->h_transaction
;
323 journal_t
*journal
= transaction
->t_journal
;
328 if (is_handle_aborted(handle
))
333 spin_lock(&journal
->j_state_lock
);
335 /* Don't extend a locked-down transaction! */
336 if (handle
->h_transaction
->t_state
!= T_RUNNING
) {
337 jbd_debug(3, "denied handle %p %d blocks: "
338 "transaction not running\n", handle
, nblocks
);
342 spin_lock(&transaction
->t_handle_lock
);
343 wanted
= transaction
->t_outstanding_credits
+ nblocks
;
345 if (wanted
> journal
->j_max_transaction_buffers
) {
346 jbd_debug(3, "denied handle %p %d blocks: "
347 "transaction too large\n", handle
, nblocks
);
351 if (wanted
> __log_space_left(journal
)) {
352 jbd_debug(3, "denied handle %p %d blocks: "
353 "insufficient log space\n", handle
, nblocks
);
357 handle
->h_buffer_credits
+= nblocks
;
358 transaction
->t_outstanding_credits
+= nblocks
;
361 jbd_debug(3, "extended handle %p by %d\n", handle
, nblocks
);
363 spin_unlock(&transaction
->t_handle_lock
);
365 spin_unlock(&journal
->j_state_lock
);
372 * int journal_restart() - restart a handle.
373 * @handle: handle to restart
374 * @nblocks: nr credits requested
376 * Restart a handle for a multi-transaction filesystem
379 * If the journal_extend() call above fails to grant new buffer credits
380 * to a running handle, a call to journal_restart will commit the
381 * handle's transaction so far and reattach the handle to a new
382 * transaction capabable of guaranteeing the requested number of
386 int journal_restart(handle_t
*handle
, int nblocks
)
388 transaction_t
*transaction
= handle
->h_transaction
;
389 journal_t
*journal
= transaction
->t_journal
;
392 /* If we've had an abort of any type, don't even think about
393 * actually doing the restart! */
394 if (is_handle_aborted(handle
))
398 * First unlink the handle from its current transaction, and start the
401 J_ASSERT(transaction
->t_updates
> 0);
402 J_ASSERT(journal_current_handle() == handle
);
404 spin_lock(&journal
->j_state_lock
);
405 spin_lock(&transaction
->t_handle_lock
);
406 transaction
->t_outstanding_credits
-= handle
->h_buffer_credits
;
407 transaction
->t_updates
--;
409 if (!transaction
->t_updates
)
410 wake_up(&journal
->j_wait_updates
);
411 spin_unlock(&transaction
->t_handle_lock
);
413 jbd_debug(2, "restarting handle %p\n", handle
);
414 __log_start_commit(journal
, transaction
->t_tid
);
415 spin_unlock(&journal
->j_state_lock
);
417 lock_map_release(&handle
->h_lockdep_map
);
418 handle
->h_buffer_credits
= nblocks
;
419 ret
= start_this_handle(journal
, handle
);
425 * void journal_lock_updates () - establish a transaction barrier.
426 * @journal: Journal to establish a barrier on.
428 * This locks out any further updates from being started, and blocks until all
429 * existing updates have completed, returning only once the journal is in a
430 * quiescent state with no updates running.
432 * We do not use simple mutex for synchronization as there are syscalls which
433 * want to return with filesystem locked and that trips up lockdep. Also
434 * hibernate needs to lock filesystem but locked mutex then blocks hibernation.
435 * Since locking filesystem is rare operation, we use simple counter and
436 * waitqueue for locking.
438 void journal_lock_updates(journal_t
*journal
)
443 /* Wait for previous locked operation to finish */
444 wait_event(journal
->j_wait_transaction_locked
,
445 journal
->j_barrier_count
== 0);
447 spin_lock(&journal
->j_state_lock
);
449 * Check reliably under the lock whether we are the ones winning the race
450 * and locking the journal
452 if (journal
->j_barrier_count
> 0) {
453 spin_unlock(&journal
->j_state_lock
);
456 ++journal
->j_barrier_count
;
458 /* Wait until there are no running updates */
460 transaction_t
*transaction
= journal
->j_running_transaction
;
465 spin_lock(&transaction
->t_handle_lock
);
466 if (!transaction
->t_updates
) {
467 spin_unlock(&transaction
->t_handle_lock
);
470 prepare_to_wait(&journal
->j_wait_updates
, &wait
,
471 TASK_UNINTERRUPTIBLE
);
472 spin_unlock(&transaction
->t_handle_lock
);
473 spin_unlock(&journal
->j_state_lock
);
475 finish_wait(&journal
->j_wait_updates
, &wait
);
476 spin_lock(&journal
->j_state_lock
);
478 spin_unlock(&journal
->j_state_lock
);
482 * void journal_unlock_updates (journal_t* journal) - release barrier
483 * @journal: Journal to release the barrier on.
485 * Release a transaction barrier obtained with journal_lock_updates().
487 void journal_unlock_updates (journal_t
*journal
)
489 J_ASSERT(journal
->j_barrier_count
!= 0);
491 spin_lock(&journal
->j_state_lock
);
492 --journal
->j_barrier_count
;
493 spin_unlock(&journal
->j_state_lock
);
494 wake_up(&journal
->j_wait_transaction_locked
);
497 static void warn_dirty_buffer(struct buffer_head
*bh
)
499 char b
[BDEVNAME_SIZE
];
502 "JBD: Spotted dirty metadata buffer (dev = %s, blocknr = %llu). "
503 "There's a risk of filesystem corruption in case of system "
505 bdevname(bh
->b_bdev
, b
), (unsigned long long)bh
->b_blocknr
);
509 * If the buffer is already part of the current transaction, then there
510 * is nothing we need to do. If it is already part of a prior
511 * transaction which we are still committing to disk, then we need to
512 * make sure that we do not overwrite the old copy: we do copy-out to
513 * preserve the copy going to disk. We also account the buffer against
514 * the handle's metadata buffer credits (unless the buffer is already
515 * part of the transaction, that is).
519 do_get_write_access(handle_t
*handle
, struct journal_head
*jh
,
522 struct buffer_head
*bh
;
523 transaction_t
*transaction
;
526 char *frozen_buffer
= NULL
;
529 if (is_handle_aborted(handle
))
532 transaction
= handle
->h_transaction
;
533 journal
= transaction
->t_journal
;
535 jbd_debug(5, "journal_head %p, force_copy %d\n", jh
, force_copy
);
537 JBUFFER_TRACE(jh
, "entry");
541 /* @@@ Need to check for errors here at some point. */
544 jbd_lock_bh_state(bh
);
546 /* We now hold the buffer lock so it is safe to query the buffer
547 * state. Is the buffer dirty?
549 * If so, there are two possibilities. The buffer may be
550 * non-journaled, and undergoing a quite legitimate writeback.
551 * Otherwise, it is journaled, and we don't expect dirty buffers
552 * in that state (the buffers should be marked JBD_Dirty
553 * instead.) So either the IO is being done under our own
554 * control and this is a bug, or it's a third party IO such as
555 * dump(8) (which may leave the buffer scheduled for read ---
556 * ie. locked but not dirty) or tune2fs (which may actually have
557 * the buffer dirtied, ugh.) */
559 if (buffer_dirty(bh
)) {
561 * First question: is this buffer already part of the current
562 * transaction or the existing committing transaction?
564 if (jh
->b_transaction
) {
566 jh
->b_transaction
== transaction
||
568 journal
->j_committing_transaction
);
569 if (jh
->b_next_transaction
)
570 J_ASSERT_JH(jh
, jh
->b_next_transaction
==
572 warn_dirty_buffer(bh
);
575 * In any case we need to clean the dirty flag and we must
576 * do it under the buffer lock to be sure we don't race
577 * with running write-out.
579 JBUFFER_TRACE(jh
, "Journalling dirty buffer");
580 clear_buffer_dirty(bh
);
581 set_buffer_jbddirty(bh
);
587 if (is_handle_aborted(handle
)) {
588 jbd_unlock_bh_state(bh
);
594 * The buffer is already part of this transaction if b_transaction or
595 * b_next_transaction points to it
597 if (jh
->b_transaction
== transaction
||
598 jh
->b_next_transaction
== transaction
)
602 * this is the first time this transaction is touching this buffer,
603 * reset the modified flag
608 * If there is already a copy-out version of this buffer, then we don't
609 * need to make another one
611 if (jh
->b_frozen_data
) {
612 JBUFFER_TRACE(jh
, "has frozen data");
613 J_ASSERT_JH(jh
, jh
->b_next_transaction
== NULL
);
614 jh
->b_next_transaction
= transaction
;
618 /* Is there data here we need to preserve? */
620 if (jh
->b_transaction
&& jh
->b_transaction
!= transaction
) {
621 JBUFFER_TRACE(jh
, "owned by older transaction");
622 J_ASSERT_JH(jh
, jh
->b_next_transaction
== NULL
);
623 J_ASSERT_JH(jh
, jh
->b_transaction
==
624 journal
->j_committing_transaction
);
626 /* There is one case we have to be very careful about.
627 * If the committing transaction is currently writing
628 * this buffer out to disk and has NOT made a copy-out,
629 * then we cannot modify the buffer contents at all
630 * right now. The essence of copy-out is that it is the
631 * extra copy, not the primary copy, which gets
632 * journaled. If the primary copy is already going to
633 * disk then we cannot do copy-out here. */
635 if (jh
->b_jlist
== BJ_Shadow
) {
636 DEFINE_WAIT_BIT(wait
, &bh
->b_state
, BH_Unshadow
);
637 wait_queue_head_t
*wqh
;
639 wqh
= bit_waitqueue(&bh
->b_state
, BH_Unshadow
);
641 JBUFFER_TRACE(jh
, "on shadow: sleep");
642 jbd_unlock_bh_state(bh
);
643 /* commit wakes up all shadow buffers after IO */
645 prepare_to_wait(wqh
, &wait
.wait
,
646 TASK_UNINTERRUPTIBLE
);
647 if (jh
->b_jlist
!= BJ_Shadow
)
651 finish_wait(wqh
, &wait
.wait
);
655 /* Only do the copy if the currently-owning transaction
656 * still needs it. If it is on the Forget list, the
657 * committing transaction is past that stage. The
658 * buffer had better remain locked during the kmalloc,
659 * but that should be true --- we hold the journal lock
660 * still and the buffer is already on the BUF_JOURNAL
661 * list so won't be flushed.
663 * Subtle point, though: if this is a get_undo_access,
664 * then we will be relying on the frozen_data to contain
665 * the new value of the committed_data record after the
666 * transaction, so we HAVE to force the frozen_data copy
669 if (jh
->b_jlist
!= BJ_Forget
|| force_copy
) {
670 JBUFFER_TRACE(jh
, "generate frozen data");
671 if (!frozen_buffer
) {
672 JBUFFER_TRACE(jh
, "allocate memory for buffer");
673 jbd_unlock_bh_state(bh
);
675 jbd_alloc(jh2bh(jh
)->b_size
,
677 if (!frozen_buffer
) {
679 "%s: OOM for frozen_buffer\n",
681 JBUFFER_TRACE(jh
, "oom!");
683 jbd_lock_bh_state(bh
);
688 jh
->b_frozen_data
= frozen_buffer
;
689 frozen_buffer
= NULL
;
692 jh
->b_next_transaction
= transaction
;
697 * Finally, if the buffer is not journaled right now, we need to make
698 * sure it doesn't get written to disk before the caller actually
699 * commits the new data
701 if (!jh
->b_transaction
) {
702 JBUFFER_TRACE(jh
, "no transaction");
703 J_ASSERT_JH(jh
, !jh
->b_next_transaction
);
704 JBUFFER_TRACE(jh
, "file as BJ_Reserved");
705 spin_lock(&journal
->j_list_lock
);
706 __journal_file_buffer(jh
, transaction
, BJ_Reserved
);
707 spin_unlock(&journal
->j_list_lock
);
716 J_EXPECT_JH(jh
, buffer_uptodate(jh2bh(jh
)),
717 "Possible IO failure.\n");
718 page
= jh2bh(jh
)->b_page
;
719 offset
= offset_in_page(jh2bh(jh
)->b_data
);
720 source
= kmap_atomic(page
);
721 memcpy(jh
->b_frozen_data
, source
+offset
, jh2bh(jh
)->b_size
);
722 kunmap_atomic(source
);
724 jbd_unlock_bh_state(bh
);
727 * If we are about to journal a buffer, then any revoke pending on it is
730 journal_cancel_revoke(handle
, jh
);
733 if (unlikely(frozen_buffer
)) /* It's usually NULL */
734 jbd_free(frozen_buffer
, bh
->b_size
);
736 JBUFFER_TRACE(jh
, "exit");
741 * int journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update.
742 * @handle: transaction to add buffer modifications to
743 * @bh: bh to be used for metadata writes
745 * Returns an error code or 0 on success.
747 * In full data journalling mode the buffer may be of type BJ_AsyncData,
748 * because we're write()ing a buffer which is also part of a shared mapping.
751 int journal_get_write_access(handle_t
*handle
, struct buffer_head
*bh
)
753 struct journal_head
*jh
= journal_add_journal_head(bh
);
756 /* We do not want to get caught playing with fields which the
757 * log thread also manipulates. Make sure that the buffer
758 * completes any outstanding IO before proceeding. */
759 rc
= do_get_write_access(handle
, jh
, 0);
760 journal_put_journal_head(jh
);
766 * When the user wants to journal a newly created buffer_head
767 * (ie. getblk() returned a new buffer and we are going to populate it
768 * manually rather than reading off disk), then we need to keep the
769 * buffer_head locked until it has been completely filled with new
770 * data. In this case, we should be able to make the assertion that
771 * the bh is not already part of an existing transaction.
773 * The buffer should already be locked by the caller by this point.
774 * There is no lock ranking violation: it was a newly created,
775 * unlocked buffer beforehand. */
778 * int journal_get_create_access () - notify intent to use newly created bh
779 * @handle: transaction to new buffer to
782 * Call this if you create a new bh.
784 int journal_get_create_access(handle_t
*handle
, struct buffer_head
*bh
)
786 transaction_t
*transaction
= handle
->h_transaction
;
787 journal_t
*journal
= transaction
->t_journal
;
788 struct journal_head
*jh
= journal_add_journal_head(bh
);
791 jbd_debug(5, "journal_head %p\n", jh
);
793 if (is_handle_aborted(handle
))
797 JBUFFER_TRACE(jh
, "entry");
799 * The buffer may already belong to this transaction due to pre-zeroing
800 * in the filesystem's new_block code. It may also be on the previous,
801 * committing transaction's lists, but it HAS to be in Forget state in
802 * that case: the transaction must have deleted the buffer for it to be
805 jbd_lock_bh_state(bh
);
806 spin_lock(&journal
->j_list_lock
);
807 J_ASSERT_JH(jh
, (jh
->b_transaction
== transaction
||
808 jh
->b_transaction
== NULL
||
809 (jh
->b_transaction
== journal
->j_committing_transaction
&&
810 jh
->b_jlist
== BJ_Forget
)));
812 J_ASSERT_JH(jh
, jh
->b_next_transaction
== NULL
);
813 J_ASSERT_JH(jh
, buffer_locked(jh2bh(jh
)));
815 if (jh
->b_transaction
== NULL
) {
817 * Previous journal_forget() could have left the buffer
818 * with jbddirty bit set because it was being committed. When
819 * the commit finished, we've filed the buffer for
820 * checkpointing and marked it dirty. Now we are reallocating
821 * the buffer so the transaction freeing it must have
822 * committed and so it's safe to clear the dirty bit.
824 clear_buffer_dirty(jh2bh(jh
));
826 /* first access by this transaction */
829 JBUFFER_TRACE(jh
, "file as BJ_Reserved");
830 __journal_file_buffer(jh
, transaction
, BJ_Reserved
);
831 } else if (jh
->b_transaction
== journal
->j_committing_transaction
) {
832 /* first access by this transaction */
835 JBUFFER_TRACE(jh
, "set next transaction");
836 jh
->b_next_transaction
= transaction
;
838 spin_unlock(&journal
->j_list_lock
);
839 jbd_unlock_bh_state(bh
);
842 * akpm: I added this. ext3_alloc_branch can pick up new indirect
843 * blocks which contain freed but then revoked metadata. We need
844 * to cancel the revoke in case we end up freeing it yet again
845 * and the reallocating as data - this would cause a second revoke,
846 * which hits an assertion error.
848 JBUFFER_TRACE(jh
, "cancelling revoke");
849 journal_cancel_revoke(handle
, jh
);
851 journal_put_journal_head(jh
);
856 * int journal_get_undo_access() - Notify intent to modify metadata with non-rewindable consequences
857 * @handle: transaction
858 * @bh: buffer to undo
860 * Sometimes there is a need to distinguish between metadata which has
861 * been committed to disk and that which has not. The ext3fs code uses
862 * this for freeing and allocating space, we have to make sure that we
863 * do not reuse freed space until the deallocation has been committed,
864 * since if we overwrote that space we would make the delete
865 * un-rewindable in case of a crash.
867 * To deal with that, journal_get_undo_access requests write access to a
868 * buffer for parts of non-rewindable operations such as delete
869 * operations on the bitmaps. The journaling code must keep a copy of
870 * the buffer's contents prior to the undo_access call until such time
871 * as we know that the buffer has definitely been committed to disk.
873 * We never need to know which transaction the committed data is part
874 * of, buffers touched here are guaranteed to be dirtied later and so
875 * will be committed to a new transaction in due course, at which point
876 * we can discard the old committed data pointer.
878 * Returns error number or 0 on success.
880 int journal_get_undo_access(handle_t
*handle
, struct buffer_head
*bh
)
883 struct journal_head
*jh
= journal_add_journal_head(bh
);
884 char *committed_data
= NULL
;
886 JBUFFER_TRACE(jh
, "entry");
889 * Do this first --- it can drop the journal lock, so we want to
890 * make sure that obtaining the committed_data is done
891 * atomically wrt. completion of any outstanding commits.
893 err
= do_get_write_access(handle
, jh
, 1);
898 if (!jh
->b_committed_data
) {
899 committed_data
= jbd_alloc(jh2bh(jh
)->b_size
, GFP_NOFS
);
900 if (!committed_data
) {
901 printk(KERN_EMERG
"%s: No memory for committed data\n",
908 jbd_lock_bh_state(bh
);
909 if (!jh
->b_committed_data
) {
910 /* Copy out the current buffer contents into the
911 * preserved, committed copy. */
912 JBUFFER_TRACE(jh
, "generate b_committed data");
913 if (!committed_data
) {
914 jbd_unlock_bh_state(bh
);
918 jh
->b_committed_data
= committed_data
;
919 committed_data
= NULL
;
920 memcpy(jh
->b_committed_data
, bh
->b_data
, bh
->b_size
);
922 jbd_unlock_bh_state(bh
);
924 journal_put_journal_head(jh
);
925 if (unlikely(committed_data
))
926 jbd_free(committed_data
, bh
->b_size
);
931 * int journal_dirty_data() - mark a buffer as containing dirty data to be flushed
932 * @handle: transaction
933 * @bh: bufferhead to mark
936 * Mark a buffer as containing dirty data which needs to be flushed before
937 * we can commit the current transaction.
939 * The buffer is placed on the transaction's data list and is marked as
940 * belonging to the transaction.
942 * Returns error number or 0 on success.
944 * journal_dirty_data() can be called via page_launder->ext3_writepage
947 int journal_dirty_data(handle_t
*handle
, struct buffer_head
*bh
)
949 journal_t
*journal
= handle
->h_transaction
->t_journal
;
951 struct journal_head
*jh
;
954 if (is_handle_aborted(handle
))
957 jh
= journal_add_journal_head(bh
);
958 JBUFFER_TRACE(jh
, "entry");
961 * The buffer could *already* be dirty. Writeout can start
964 jbd_debug(4, "jh: %p, tid:%d\n", jh
, handle
->h_transaction
->t_tid
);
967 * What if the buffer is already part of a running transaction?
969 * There are two cases:
970 * 1) It is part of the current running transaction. Refile it,
971 * just in case we have allocated it as metadata, deallocated
972 * it, then reallocated it as data.
973 * 2) It is part of the previous, still-committing transaction.
974 * If all we want to do is to guarantee that the buffer will be
975 * written to disk before this new transaction commits, then
976 * being sure that the *previous* transaction has this same
977 * property is sufficient for us! Just leave it on its old
980 * In case (2), the buffer must not already exist as metadata
981 * --- that would violate write ordering (a transaction is free
982 * to write its data at any point, even before the previous
983 * committing transaction has committed). The caller must
984 * never, ever allow this to happen: there's nothing we can do
985 * about it in this layer.
987 jbd_lock_bh_state(bh
);
988 spin_lock(&journal
->j_list_lock
);
990 /* Now that we have bh_state locked, are we really still mapped? */
991 if (!buffer_mapped(bh
)) {
992 JBUFFER_TRACE(jh
, "unmapped buffer, bailing out");
996 if (jh
->b_transaction
) {
997 JBUFFER_TRACE(jh
, "has transaction");
998 if (jh
->b_transaction
!= handle
->h_transaction
) {
999 JBUFFER_TRACE(jh
, "belongs to older transaction");
1000 J_ASSERT_JH(jh
, jh
->b_transaction
==
1001 journal
->j_committing_transaction
);
1003 /* @@@ IS THIS TRUE ? */
1005 * Not any more. Scenario: someone does a write()
1006 * in data=journal mode. The buffer's transaction has
1007 * moved into commit. Then someone does another
1008 * write() to the file. We do the frozen data copyout
1009 * and set b_next_transaction to point to j_running_t.
1010 * And while we're in that state, someone does a
1011 * writepage() in an attempt to pageout the same area
1012 * of the file via a shared mapping. At present that
1013 * calls journal_dirty_data(), and we get right here.
1014 * It may be too late to journal the data. Simply
1015 * falling through to the next test will suffice: the
1016 * data will be dirty and wil be checkpointed. The
1017 * ordering comments in the next comment block still
1020 //J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1023 * If we're journalling data, and this buffer was
1024 * subject to a write(), it could be metadata, forget
1025 * or shadow against the committing transaction. Now,
1026 * someone has dirtied the same darn page via a mapping
1027 * and it is being writepage()'d.
1028 * We *could* just steal the page from commit, with some
1029 * fancy locking there. Instead, we just skip it -
1030 * don't tie the page's buffers to the new transaction
1032 * Implication: if we crash before the writepage() data
1033 * is written into the filesystem, recovery will replay
1036 if (jh
->b_jlist
!= BJ_None
&&
1037 jh
->b_jlist
!= BJ_SyncData
&&
1038 jh
->b_jlist
!= BJ_Locked
) {
1039 JBUFFER_TRACE(jh
, "Not stealing");
1044 * This buffer may be undergoing writeout in commit. We
1045 * can't return from here and let the caller dirty it
1046 * again because that can cause the write-out loop in
1047 * commit to never terminate.
1049 if (buffer_dirty(bh
)) {
1051 spin_unlock(&journal
->j_list_lock
);
1052 jbd_unlock_bh_state(bh
);
1054 sync_dirty_buffer(bh
);
1055 jbd_lock_bh_state(bh
);
1056 spin_lock(&journal
->j_list_lock
);
1057 /* Since we dropped the lock... */
1058 if (!buffer_mapped(bh
)) {
1059 JBUFFER_TRACE(jh
, "buffer got unmapped");
1062 /* The buffer may become locked again at any
1063 time if it is redirtied */
1067 * We cannot remove the buffer with io error from the
1068 * committing transaction, because otherwise it would
1069 * miss the error and the commit would not abort.
1071 if (unlikely(!buffer_uptodate(bh
))) {
1075 /* We might have slept so buffer could be refiled now */
1076 if (jh
->b_transaction
!= NULL
&&
1077 jh
->b_transaction
!= handle
->h_transaction
) {
1078 JBUFFER_TRACE(jh
, "unfile from commit");
1079 __journal_temp_unlink_buffer(jh
);
1080 /* It still points to the committing
1081 * transaction; move it to this one so
1082 * that the refile assert checks are
1084 jh
->b_transaction
= handle
->h_transaction
;
1086 /* The buffer will be refiled below */
1090 * Special case --- the buffer might actually have been
1091 * allocated and then immediately deallocated in the previous,
1092 * committing transaction, so might still be left on that
1093 * transaction's metadata lists.
1095 if (jh
->b_jlist
!= BJ_SyncData
&& jh
->b_jlist
!= BJ_Locked
) {
1096 JBUFFER_TRACE(jh
, "not on correct data list: unfile");
1097 J_ASSERT_JH(jh
, jh
->b_jlist
!= BJ_Shadow
);
1098 JBUFFER_TRACE(jh
, "file as data");
1099 __journal_file_buffer(jh
, handle
->h_transaction
,
1103 JBUFFER_TRACE(jh
, "not on a transaction");
1104 __journal_file_buffer(jh
, handle
->h_transaction
, BJ_SyncData
);
1107 spin_unlock(&journal
->j_list_lock
);
1108 jbd_unlock_bh_state(bh
);
1110 BUFFER_TRACE(bh
, "brelse");
1113 JBUFFER_TRACE(jh
, "exit");
1114 journal_put_journal_head(jh
);
1119 * int journal_dirty_metadata() - mark a buffer as containing dirty metadata
1120 * @handle: transaction to add buffer to.
1121 * @bh: buffer to mark
1123 * Mark dirty metadata which needs to be journaled as part of the current
1126 * The buffer is placed on the transaction's metadata list and is marked
1127 * as belonging to the transaction.
1129 * Returns error number or 0 on success.
1131 * Special care needs to be taken if the buffer already belongs to the
1132 * current committing transaction (in which case we should have frozen
1133 * data present for that commit). In that case, we don't relink the
1134 * buffer: that only gets done when the old transaction finally
1135 * completes its commit.
1137 int journal_dirty_metadata(handle_t
*handle
, struct buffer_head
*bh
)
1139 transaction_t
*transaction
= handle
->h_transaction
;
1140 journal_t
*journal
= transaction
->t_journal
;
1141 struct journal_head
*jh
= bh2jh(bh
);
1143 jbd_debug(5, "journal_head %p\n", jh
);
1144 JBUFFER_TRACE(jh
, "entry");
1145 if (is_handle_aborted(handle
))
1148 jbd_lock_bh_state(bh
);
1150 if (jh
->b_modified
== 0) {
1152 * This buffer's got modified and becoming part
1153 * of the transaction. This needs to be done
1154 * once a transaction -bzzz
1157 J_ASSERT_JH(jh
, handle
->h_buffer_credits
> 0);
1158 handle
->h_buffer_credits
--;
1162 * fastpath, to avoid expensive locking. If this buffer is already
1163 * on the running transaction's metadata list there is nothing to do.
1164 * Nobody can take it off again because there is a handle open.
1165 * I _think_ we're OK here with SMP barriers - a mistaken decision will
1166 * result in this test being false, so we go in and take the locks.
1168 if (jh
->b_transaction
== transaction
&& jh
->b_jlist
== BJ_Metadata
) {
1169 JBUFFER_TRACE(jh
, "fastpath");
1170 J_ASSERT_JH(jh
, jh
->b_transaction
==
1171 journal
->j_running_transaction
);
1175 set_buffer_jbddirty(bh
);
1178 * Metadata already on the current transaction list doesn't
1179 * need to be filed. Metadata on another transaction's list must
1180 * be committing, and will be refiled once the commit completes:
1181 * leave it alone for now.
1183 if (jh
->b_transaction
!= transaction
) {
1184 JBUFFER_TRACE(jh
, "already on other transaction");
1185 J_ASSERT_JH(jh
, jh
->b_transaction
==
1186 journal
->j_committing_transaction
);
1187 J_ASSERT_JH(jh
, jh
->b_next_transaction
== transaction
);
1188 /* And this case is illegal: we can't reuse another
1189 * transaction's data buffer, ever. */
1193 /* That test should have eliminated the following case: */
1194 J_ASSERT_JH(jh
, jh
->b_frozen_data
== NULL
);
1196 JBUFFER_TRACE(jh
, "file as BJ_Metadata");
1197 spin_lock(&journal
->j_list_lock
);
1198 __journal_file_buffer(jh
, handle
->h_transaction
, BJ_Metadata
);
1199 spin_unlock(&journal
->j_list_lock
);
1201 jbd_unlock_bh_state(bh
);
1203 JBUFFER_TRACE(jh
, "exit");
1208 * journal_release_buffer: undo a get_write_access without any buffer
1209 * updates, if the update decided in the end that it didn't need access.
1213 journal_release_buffer(handle_t
*handle
, struct buffer_head
*bh
)
1215 BUFFER_TRACE(bh
, "entry");
1219 * void journal_forget() - bforget() for potentially-journaled buffers.
1220 * @handle: transaction handle
1221 * @bh: bh to 'forget'
1223 * We can only do the bforget if there are no commits pending against the
1224 * buffer. If the buffer is dirty in the current running transaction we
1225 * can safely unlink it.
1227 * bh may not be a journalled buffer at all - it may be a non-JBD
1228 * buffer which came off the hashtable. Check for this.
1230 * Decrements bh->b_count by one.
1232 * Allow this call even if the handle has aborted --- it may be part of
1233 * the caller's cleanup after an abort.
1235 int journal_forget (handle_t
*handle
, struct buffer_head
*bh
)
1237 transaction_t
*transaction
= handle
->h_transaction
;
1238 journal_t
*journal
= transaction
->t_journal
;
1239 struct journal_head
*jh
;
1240 int drop_reserve
= 0;
1242 int was_modified
= 0;
1244 BUFFER_TRACE(bh
, "entry");
1246 jbd_lock_bh_state(bh
);
1247 spin_lock(&journal
->j_list_lock
);
1249 if (!buffer_jbd(bh
))
1253 /* Critical error: attempting to delete a bitmap buffer, maybe?
1254 * Don't do any jbd operations, and return an error. */
1255 if (!J_EXPECT_JH(jh
, !jh
->b_committed_data
,
1256 "inconsistent data on disk")) {
1261 /* keep track of whether or not this transaction modified us */
1262 was_modified
= jh
->b_modified
;
1265 * The buffer's going from the transaction, we must drop
1266 * all references -bzzz
1270 if (jh
->b_transaction
== handle
->h_transaction
) {
1271 J_ASSERT_JH(jh
, !jh
->b_frozen_data
);
1273 /* If we are forgetting a buffer which is already part
1274 * of this transaction, then we can just drop it from
1275 * the transaction immediately. */
1276 clear_buffer_dirty(bh
);
1277 clear_buffer_jbddirty(bh
);
1279 JBUFFER_TRACE(jh
, "belongs to current transaction: unfile");
1282 * we only want to drop a reference if this transaction
1283 * modified the buffer
1289 * We are no longer going to journal this buffer.
1290 * However, the commit of this transaction is still
1291 * important to the buffer: the delete that we are now
1292 * processing might obsolete an old log entry, so by
1293 * committing, we can satisfy the buffer's checkpoint.
1295 * So, if we have a checkpoint on the buffer, we should
1296 * now refile the buffer on our BJ_Forget list so that
1297 * we know to remove the checkpoint after we commit.
1300 if (jh
->b_cp_transaction
) {
1301 __journal_temp_unlink_buffer(jh
);
1302 __journal_file_buffer(jh
, transaction
, BJ_Forget
);
1304 __journal_unfile_buffer(jh
);
1305 if (!buffer_jbd(bh
)) {
1306 spin_unlock(&journal
->j_list_lock
);
1307 jbd_unlock_bh_state(bh
);
1312 } else if (jh
->b_transaction
) {
1313 J_ASSERT_JH(jh
, (jh
->b_transaction
==
1314 journal
->j_committing_transaction
));
1315 /* However, if the buffer is still owned by a prior
1316 * (committing) transaction, we can't drop it yet... */
1317 JBUFFER_TRACE(jh
, "belongs to older transaction");
1318 /* ... but we CAN drop it from the new transaction if we
1319 * have also modified it since the original commit. */
1321 if (jh
->b_next_transaction
) {
1322 J_ASSERT(jh
->b_next_transaction
== transaction
);
1323 jh
->b_next_transaction
= NULL
;
1326 * only drop a reference if this transaction modified
1335 spin_unlock(&journal
->j_list_lock
);
1336 jbd_unlock_bh_state(bh
);
1340 /* no need to reserve log space for this block -bzzz */
1341 handle
->h_buffer_credits
++;
1347 * int journal_stop() - complete a transaction
1348 * @handle: tranaction to complete.
1350 * All done for a particular handle.
1352 * There is not much action needed here. We just return any remaining
1353 * buffer credits to the transaction and remove the handle. The only
1354 * complication is that we need to start a commit operation if the
1355 * filesystem is marked for synchronous update.
1357 * journal_stop itself will not usually return an error, but it may
1358 * do so in unusual circumstances. In particular, expect it to
1359 * return -EIO if a journal_abort has been executed since the
1360 * transaction began.
1362 int journal_stop(handle_t
*handle
)
1364 transaction_t
*transaction
= handle
->h_transaction
;
1365 journal_t
*journal
= transaction
->t_journal
;
1369 J_ASSERT(journal_current_handle() == handle
);
1371 if (is_handle_aborted(handle
))
1374 J_ASSERT(transaction
->t_updates
> 0);
1378 if (--handle
->h_ref
> 0) {
1379 jbd_debug(4, "h_ref %d -> %d\n", handle
->h_ref
+ 1,
1384 jbd_debug(4, "Handle %p going down\n", handle
);
1387 * Implement synchronous transaction batching. If the handle
1388 * was synchronous, don't force a commit immediately. Let's
1389 * yield and let another thread piggyback onto this transaction.
1390 * Keep doing that while new threads continue to arrive.
1391 * It doesn't cost much - we're about to run a commit and sleep
1392 * on IO anyway. Speeds up many-threaded, many-dir operations
1395 * We try and optimize the sleep time against what the underlying disk
1396 * can do, instead of having a static sleep time. This is useful for
1397 * the case where our storage is so fast that it is more optimal to go
1398 * ahead and force a flush and wait for the transaction to be committed
1399 * than it is to wait for an arbitrary amount of time for new writers to
1400 * join the transaction. We achieve this by measuring how long it takes
1401 * to commit a transaction, and compare it with how long this
1402 * transaction has been running, and if run time < commit time then we
1403 * sleep for the delta and commit. This greatly helps super fast disks
1404 * that would see slowdowns as more threads started doing fsyncs.
1406 * But don't do this if this process was the most recent one to
1407 * perform a synchronous write. We do this to detect the case where a
1408 * single process is doing a stream of sync writes. No point in waiting
1409 * for joiners in that case.
1412 if (handle
->h_sync
&& journal
->j_last_sync_writer
!= pid
) {
1413 u64 commit_time
, trans_time
;
1415 journal
->j_last_sync_writer
= pid
;
1417 spin_lock(&journal
->j_state_lock
);
1418 commit_time
= journal
->j_average_commit_time
;
1419 spin_unlock(&journal
->j_state_lock
);
1421 trans_time
= ktime_to_ns(ktime_sub(ktime_get(),
1422 transaction
->t_start_time
));
1424 commit_time
= min_t(u64
, commit_time
,
1425 1000*jiffies_to_usecs(1));
1427 if (trans_time
< commit_time
) {
1428 ktime_t expires
= ktime_add_ns(ktime_get(),
1430 set_current_state(TASK_UNINTERRUPTIBLE
);
1431 schedule_hrtimeout(&expires
, HRTIMER_MODE_ABS
);
1435 current
->journal_info
= NULL
;
1436 spin_lock(&journal
->j_state_lock
);
1437 spin_lock(&transaction
->t_handle_lock
);
1438 transaction
->t_outstanding_credits
-= handle
->h_buffer_credits
;
1439 transaction
->t_updates
--;
1440 if (!transaction
->t_updates
) {
1441 wake_up(&journal
->j_wait_updates
);
1442 if (journal
->j_barrier_count
)
1443 wake_up(&journal
->j_wait_transaction_locked
);
1447 * If the handle is marked SYNC, we need to set another commit
1448 * going! We also want to force a commit if the current
1449 * transaction is occupying too much of the log, or if the
1450 * transaction is too old now.
1452 if (handle
->h_sync
||
1453 transaction
->t_outstanding_credits
>
1454 journal
->j_max_transaction_buffers
||
1455 time_after_eq(jiffies
, transaction
->t_expires
)) {
1456 /* Do this even for aborted journals: an abort still
1457 * completes the commit thread, it just doesn't write
1458 * anything to disk. */
1459 tid_t tid
= transaction
->t_tid
;
1461 spin_unlock(&transaction
->t_handle_lock
);
1462 jbd_debug(2, "transaction too old, requesting commit for "
1463 "handle %p\n", handle
);
1464 /* This is non-blocking */
1465 __log_start_commit(journal
, transaction
->t_tid
);
1466 spin_unlock(&journal
->j_state_lock
);
1469 * Special case: JFS_SYNC synchronous updates require us
1470 * to wait for the commit to complete.
1472 if (handle
->h_sync
&& !(current
->flags
& PF_MEMALLOC
))
1473 err
= log_wait_commit(journal
, tid
);
1475 spin_unlock(&transaction
->t_handle_lock
);
1476 spin_unlock(&journal
->j_state_lock
);
1479 lock_map_release(&handle
->h_lockdep_map
);
1481 jbd_free_handle(handle
);
1486 * int journal_force_commit() - force any uncommitted transactions
1487 * @journal: journal to force
1489 * For synchronous operations: force any uncommitted transactions
1490 * to disk. May seem kludgy, but it reuses all the handle batching
1491 * code in a very simple manner.
1493 int journal_force_commit(journal_t
*journal
)
1498 handle
= journal_start(journal
, 1);
1499 if (IS_ERR(handle
)) {
1500 ret
= PTR_ERR(handle
);
1503 ret
= journal_stop(handle
);
1510 * List management code snippets: various functions for manipulating the
1511 * transaction buffer lists.
1516 * Append a buffer to a transaction list, given the transaction's list head
1519 * j_list_lock is held.
1521 * jbd_lock_bh_state(jh2bh(jh)) is held.
1525 __blist_add_buffer(struct journal_head
**list
, struct journal_head
*jh
)
1528 jh
->b_tnext
= jh
->b_tprev
= jh
;
1531 /* Insert at the tail of the list to preserve order */
1532 struct journal_head
*first
= *list
, *last
= first
->b_tprev
;
1534 jh
->b_tnext
= first
;
1535 last
->b_tnext
= first
->b_tprev
= jh
;
1540 * Remove a buffer from a transaction list, given the transaction's list
1543 * Called with j_list_lock held, and the journal may not be locked.
1545 * jbd_lock_bh_state(jh2bh(jh)) is held.
1549 __blist_del_buffer(struct journal_head
**list
, struct journal_head
*jh
)
1552 *list
= jh
->b_tnext
;
1556 jh
->b_tprev
->b_tnext
= jh
->b_tnext
;
1557 jh
->b_tnext
->b_tprev
= jh
->b_tprev
;
1561 * Remove a buffer from the appropriate transaction list.
1563 * Note that this function can *change* the value of
1564 * bh->b_transaction->t_sync_datalist, t_buffers, t_forget,
1565 * t_iobuf_list, t_shadow_list, t_log_list or t_reserved_list. If the caller
1566 * is holding onto a copy of one of thee pointers, it could go bad.
1567 * Generally the caller needs to re-read the pointer from the transaction_t.
1569 * Called under j_list_lock. The journal may not be locked.
1571 static void __journal_temp_unlink_buffer(struct journal_head
*jh
)
1573 struct journal_head
**list
= NULL
;
1574 transaction_t
*transaction
;
1575 struct buffer_head
*bh
= jh2bh(jh
);
1577 J_ASSERT_JH(jh
, jbd_is_locked_bh_state(bh
));
1578 transaction
= jh
->b_transaction
;
1580 assert_spin_locked(&transaction
->t_journal
->j_list_lock
);
1582 J_ASSERT_JH(jh
, jh
->b_jlist
< BJ_Types
);
1583 if (jh
->b_jlist
!= BJ_None
)
1584 J_ASSERT_JH(jh
, transaction
!= NULL
);
1586 switch (jh
->b_jlist
) {
1590 list
= &transaction
->t_sync_datalist
;
1593 transaction
->t_nr_buffers
--;
1594 J_ASSERT_JH(jh
, transaction
->t_nr_buffers
>= 0);
1595 list
= &transaction
->t_buffers
;
1598 list
= &transaction
->t_forget
;
1601 list
= &transaction
->t_iobuf_list
;
1604 list
= &transaction
->t_shadow_list
;
1607 list
= &transaction
->t_log_list
;
1610 list
= &transaction
->t_reserved_list
;
1613 list
= &transaction
->t_locked_list
;
1617 __blist_del_buffer(list
, jh
);
1618 jh
->b_jlist
= BJ_None
;
1619 if (test_clear_buffer_jbddirty(bh
))
1620 mark_buffer_dirty(bh
); /* Expose it to the VM */
1624 * Remove buffer from all transactions.
1626 * Called with bh_state lock and j_list_lock
1628 * jh and bh may be already freed when this function returns.
1630 void __journal_unfile_buffer(struct journal_head
*jh
)
1632 __journal_temp_unlink_buffer(jh
);
1633 jh
->b_transaction
= NULL
;
1634 journal_put_journal_head(jh
);
1637 void journal_unfile_buffer(journal_t
*journal
, struct journal_head
*jh
)
1639 struct buffer_head
*bh
= jh2bh(jh
);
1641 /* Get reference so that buffer cannot be freed before we unlock it */
1643 jbd_lock_bh_state(bh
);
1644 spin_lock(&journal
->j_list_lock
);
1645 __journal_unfile_buffer(jh
);
1646 spin_unlock(&journal
->j_list_lock
);
1647 jbd_unlock_bh_state(bh
);
1652 * Called from journal_try_to_free_buffers().
1654 * Called under jbd_lock_bh_state(bh)
1657 __journal_try_to_free_buffer(journal_t
*journal
, struct buffer_head
*bh
)
1659 struct journal_head
*jh
;
1663 if (buffer_locked(bh
) || buffer_dirty(bh
))
1666 if (jh
->b_next_transaction
!= NULL
)
1669 spin_lock(&journal
->j_list_lock
);
1670 if (jh
->b_transaction
!= NULL
&& jh
->b_cp_transaction
== NULL
) {
1671 if (jh
->b_jlist
== BJ_SyncData
|| jh
->b_jlist
== BJ_Locked
) {
1672 /* A written-back ordered data buffer */
1673 JBUFFER_TRACE(jh
, "release data");
1674 __journal_unfile_buffer(jh
);
1676 } else if (jh
->b_cp_transaction
!= NULL
&& jh
->b_transaction
== NULL
) {
1677 /* written-back checkpointed metadata buffer */
1678 if (jh
->b_jlist
== BJ_None
) {
1679 JBUFFER_TRACE(jh
, "remove from checkpoint list");
1680 __journal_remove_checkpoint(jh
);
1683 spin_unlock(&journal
->j_list_lock
);
1689 * int journal_try_to_free_buffers() - try to free page buffers.
1690 * @journal: journal for operation
1691 * @page: to try and free
1692 * @gfp_mask: we use the mask to detect how hard should we try to release
1693 * buffers. If __GFP_WAIT and __GFP_FS is set, we wait for commit code to
1694 * release the buffers.
1697 * For all the buffers on this page,
1698 * if they are fully written out ordered data, move them onto BUF_CLEAN
1699 * so try_to_free_buffers() can reap them.
1701 * This function returns non-zero if we wish try_to_free_buffers()
1702 * to be called. We do this if the page is releasable by try_to_free_buffers().
1703 * We also do it if the page has locked or dirty buffers and the caller wants
1704 * us to perform sync or async writeout.
1706 * This complicates JBD locking somewhat. We aren't protected by the
1707 * BKL here. We wish to remove the buffer from its committing or
1708 * running transaction's ->t_datalist via __journal_unfile_buffer.
1710 * This may *change* the value of transaction_t->t_datalist, so anyone
1711 * who looks at t_datalist needs to lock against this function.
1713 * Even worse, someone may be doing a journal_dirty_data on this
1714 * buffer. So we need to lock against that. journal_dirty_data()
1715 * will come out of the lock with the buffer dirty, which makes it
1716 * ineligible for release here.
1718 * Who else is affected by this? hmm... Really the only contender
1719 * is do_get_write_access() - it could be looking at the buffer while
1720 * journal_try_to_free_buffer() is changing its state. But that
1721 * cannot happen because we never reallocate freed data as metadata
1722 * while the data is part of a transaction. Yes?
1724 * Return 0 on failure, 1 on success
1726 int journal_try_to_free_buffers(journal_t
*journal
,
1727 struct page
*page
, gfp_t gfp_mask
)
1729 struct buffer_head
*head
;
1730 struct buffer_head
*bh
;
1733 J_ASSERT(PageLocked(page
));
1735 head
= page_buffers(page
);
1738 struct journal_head
*jh
;
1741 * We take our own ref against the journal_head here to avoid
1742 * having to add tons of locking around each instance of
1743 * journal_put_journal_head().
1745 jh
= journal_grab_journal_head(bh
);
1749 jbd_lock_bh_state(bh
);
1750 __journal_try_to_free_buffer(journal
, bh
);
1751 journal_put_journal_head(jh
);
1752 jbd_unlock_bh_state(bh
);
1755 } while ((bh
= bh
->b_this_page
) != head
);
1757 ret
= try_to_free_buffers(page
);
1764 * This buffer is no longer needed. If it is on an older transaction's
1765 * checkpoint list we need to record it on this transaction's forget list
1766 * to pin this buffer (and hence its checkpointing transaction) down until
1767 * this transaction commits. If the buffer isn't on a checkpoint list, we
1769 * Returns non-zero if JBD no longer has an interest in the buffer.
1771 * Called under j_list_lock.
1773 * Called under jbd_lock_bh_state(bh).
1775 static int __dispose_buffer(struct journal_head
*jh
, transaction_t
*transaction
)
1778 struct buffer_head
*bh
= jh2bh(jh
);
1780 if (jh
->b_cp_transaction
) {
1781 JBUFFER_TRACE(jh
, "on running+cp transaction");
1782 __journal_temp_unlink_buffer(jh
);
1784 * We don't want to write the buffer anymore, clear the
1785 * bit so that we don't confuse checks in
1786 * __journal_file_buffer
1788 clear_buffer_dirty(bh
);
1789 __journal_file_buffer(jh
, transaction
, BJ_Forget
);
1792 JBUFFER_TRACE(jh
, "on running transaction");
1793 __journal_unfile_buffer(jh
);
1799 * journal_invalidatepage
1801 * This code is tricky. It has a number of cases to deal with.
1803 * There are two invariants which this code relies on:
1805 * i_size must be updated on disk before we start calling invalidatepage on the
1808 * This is done in ext3 by defining an ext3_setattr method which
1809 * updates i_size before truncate gets going. By maintaining this
1810 * invariant, we can be sure that it is safe to throw away any buffers
1811 * attached to the current transaction: once the transaction commits,
1812 * we know that the data will not be needed.
1814 * Note however that we can *not* throw away data belonging to the
1815 * previous, committing transaction!
1817 * Any disk blocks which *are* part of the previous, committing
1818 * transaction (and which therefore cannot be discarded immediately) are
1819 * not going to be reused in the new running transaction
1821 * The bitmap committed_data images guarantee this: any block which is
1822 * allocated in one transaction and removed in the next will be marked
1823 * as in-use in the committed_data bitmap, so cannot be reused until
1824 * the next transaction to delete the block commits. This means that
1825 * leaving committing buffers dirty is quite safe: the disk blocks
1826 * cannot be reallocated to a different file and so buffer aliasing is
1830 * The above applies mainly to ordered data mode. In writeback mode we
1831 * don't make guarantees about the order in which data hits disk --- in
1832 * particular we don't guarantee that new dirty data is flushed before
1833 * transaction commit --- so it is always safe just to discard data
1834 * immediately in that mode. --sct
1838 * The journal_unmap_buffer helper function returns zero if the buffer
1839 * concerned remains pinned as an anonymous buffer belonging to an older
1842 * We're outside-transaction here. Either or both of j_running_transaction
1843 * and j_committing_transaction may be NULL.
1845 static int journal_unmap_buffer(journal_t
*journal
, struct buffer_head
*bh
,
1848 transaction_t
*transaction
;
1849 struct journal_head
*jh
;
1852 BUFFER_TRACE(bh
, "entry");
1856 * It is safe to proceed here without the j_list_lock because the
1857 * buffers cannot be stolen by try_to_free_buffers as long as we are
1858 * holding the page lock. --sct
1861 if (!buffer_jbd(bh
))
1862 goto zap_buffer_unlocked
;
1864 spin_lock(&journal
->j_state_lock
);
1865 jbd_lock_bh_state(bh
);
1866 spin_lock(&journal
->j_list_lock
);
1868 jh
= journal_grab_journal_head(bh
);
1870 goto zap_buffer_no_jh
;
1873 * We cannot remove the buffer from checkpoint lists until the
1874 * transaction adding inode to orphan list (let's call it T)
1875 * is committed. Otherwise if the transaction changing the
1876 * buffer would be cleaned from the journal before T is
1877 * committed, a crash will cause that the correct contents of
1878 * the buffer will be lost. On the other hand we have to
1879 * clear the buffer dirty bit at latest at the moment when the
1880 * transaction marking the buffer as freed in the filesystem
1881 * structures is committed because from that moment on the
1882 * block can be reallocated and used by a different page.
1883 * Since the block hasn't been freed yet but the inode has
1884 * already been added to orphan list, it is safe for us to add
1885 * the buffer to BJ_Forget list of the newest transaction.
1887 * Also we have to clear buffer_mapped flag of a truncated buffer
1888 * because the buffer_head may be attached to the page straddling
1889 * i_size (can happen only when blocksize < pagesize) and thus the
1890 * buffer_head can be reused when the file is extended again. So we end
1891 * up keeping around invalidated buffers attached to transactions'
1892 * BJ_Forget list just to stop checkpointing code from cleaning up
1893 * the transaction this buffer was modified in.
1895 transaction
= jh
->b_transaction
;
1896 if (transaction
== NULL
) {
1897 /* First case: not on any transaction. If it
1898 * has no checkpoint link, then we can zap it:
1899 * it's a writeback-mode buffer so we don't care
1900 * if it hits disk safely. */
1901 if (!jh
->b_cp_transaction
) {
1902 JBUFFER_TRACE(jh
, "not on any transaction: zap");
1906 if (!buffer_dirty(bh
)) {
1907 /* bdflush has written it. We can drop it now */
1911 /* OK, it must be in the journal but still not
1912 * written fully to disk: it's metadata or
1913 * journaled data... */
1915 if (journal
->j_running_transaction
) {
1916 /* ... and once the current transaction has
1917 * committed, the buffer won't be needed any
1919 JBUFFER_TRACE(jh
, "checkpointed: add to BJ_Forget");
1920 may_free
= __dispose_buffer(jh
,
1921 journal
->j_running_transaction
);
1924 /* There is no currently-running transaction. So the
1925 * orphan record which we wrote for this file must have
1926 * passed into commit. We must attach this buffer to
1927 * the committing transaction, if it exists. */
1928 if (journal
->j_committing_transaction
) {
1929 JBUFFER_TRACE(jh
, "give to committing trans");
1930 may_free
= __dispose_buffer(jh
,
1931 journal
->j_committing_transaction
);
1934 /* The orphan record's transaction has
1935 * committed. We can cleanse this buffer */
1936 clear_buffer_jbddirty(bh
);
1940 } else if (transaction
== journal
->j_committing_transaction
) {
1941 JBUFFER_TRACE(jh
, "on committing transaction");
1942 if (jh
->b_jlist
== BJ_Locked
) {
1944 * The buffer is on the committing transaction's locked
1945 * list. We have the buffer locked, so I/O has
1946 * completed. So we can nail the buffer now.
1948 may_free
= __dispose_buffer(jh
, transaction
);
1952 * The buffer is committing, we simply cannot touch
1953 * it. If the page is straddling i_size we have to wait
1954 * for commit and try again.
1957 tid_t tid
= journal
->j_committing_transaction
->t_tid
;
1959 journal_put_journal_head(jh
);
1960 spin_unlock(&journal
->j_list_lock
);
1961 jbd_unlock_bh_state(bh
);
1962 spin_unlock(&journal
->j_state_lock
);
1964 log_wait_commit(journal
, tid
);
1969 * OK, buffer won't be reachable after truncate. We just set
1970 * j_next_transaction to the running transaction (if there is
1971 * one) and mark buffer as freed so that commit code knows it
1972 * should clear dirty bits when it is done with the buffer.
1974 set_buffer_freed(bh
);
1975 if (journal
->j_running_transaction
&& buffer_jbddirty(bh
))
1976 jh
->b_next_transaction
= journal
->j_running_transaction
;
1977 journal_put_journal_head(jh
);
1978 spin_unlock(&journal
->j_list_lock
);
1979 jbd_unlock_bh_state(bh
);
1980 spin_unlock(&journal
->j_state_lock
);
1983 /* Good, the buffer belongs to the running transaction.
1984 * We are writing our own transaction's data, not any
1985 * previous one's, so it is safe to throw it away
1986 * (remember that we expect the filesystem to have set
1987 * i_size already for this truncate so recovery will not
1988 * expose the disk blocks we are discarding here.) */
1989 J_ASSERT_JH(jh
, transaction
== journal
->j_running_transaction
);
1990 JBUFFER_TRACE(jh
, "on running transaction");
1991 may_free
= __dispose_buffer(jh
, transaction
);
1996 * This is tricky. Although the buffer is truncated, it may be reused
1997 * if blocksize < pagesize and it is attached to the page straddling
1998 * EOF. Since the buffer might have been added to BJ_Forget list of the
1999 * running transaction, journal_get_write_access() won't clear
2000 * b_modified and credit accounting gets confused. So clear b_modified
2003 journal_put_journal_head(jh
);
2005 spin_unlock(&journal
->j_list_lock
);
2006 jbd_unlock_bh_state(bh
);
2007 spin_unlock(&journal
->j_state_lock
);
2008 zap_buffer_unlocked
:
2009 clear_buffer_dirty(bh
);
2010 J_ASSERT_BH(bh
, !buffer_jbddirty(bh
));
2011 clear_buffer_mapped(bh
);
2012 clear_buffer_req(bh
);
2013 clear_buffer_new(bh
);
2019 * void journal_invalidatepage() - invalidate a journal page
2020 * @journal: journal to use for flush
2021 * @page: page to flush
2022 * @offset: offset of the range to invalidate
2023 * @length: length of the range to invalidate
2025 * Reap page buffers containing data in specified range in page.
2027 void journal_invalidatepage(journal_t
*journal
,
2029 unsigned int offset
,
2030 unsigned int length
)
2032 struct buffer_head
*head
, *bh
, *next
;
2033 unsigned int stop
= offset
+ length
;
2034 unsigned int curr_off
= 0;
2035 int partial_page
= (offset
|| length
< PAGE_CACHE_SIZE
);
2038 if (!PageLocked(page
))
2040 if (!page_has_buffers(page
))
2043 BUG_ON(stop
> PAGE_CACHE_SIZE
|| stop
< length
);
2045 /* We will potentially be playing with lists other than just the
2046 * data lists (especially for journaled data mode), so be
2047 * cautious in our locking. */
2049 head
= bh
= page_buffers(page
);
2051 unsigned int next_off
= curr_off
+ bh
->b_size
;
2052 next
= bh
->b_this_page
;
2054 if (next_off
> stop
)
2057 if (offset
<= curr_off
) {
2058 /* This block is wholly outside the truncation point */
2060 may_free
&= journal_unmap_buffer(journal
, bh
,
2064 curr_off
= next_off
;
2067 } while (bh
!= head
);
2069 if (!partial_page
) {
2070 if (may_free
&& try_to_free_buffers(page
))
2071 J_ASSERT(!page_has_buffers(page
));
2076 * File a buffer on the given transaction list.
2078 void __journal_file_buffer(struct journal_head
*jh
,
2079 transaction_t
*transaction
, int jlist
)
2081 struct journal_head
**list
= NULL
;
2083 struct buffer_head
*bh
= jh2bh(jh
);
2085 J_ASSERT_JH(jh
, jbd_is_locked_bh_state(bh
));
2086 assert_spin_locked(&transaction
->t_journal
->j_list_lock
);
2088 J_ASSERT_JH(jh
, jh
->b_jlist
< BJ_Types
);
2089 J_ASSERT_JH(jh
, jh
->b_transaction
== transaction
||
2090 jh
->b_transaction
== NULL
);
2092 if (jh
->b_transaction
&& jh
->b_jlist
== jlist
)
2095 if (jlist
== BJ_Metadata
|| jlist
== BJ_Reserved
||
2096 jlist
== BJ_Shadow
|| jlist
== BJ_Forget
) {
2098 * For metadata buffers, we track dirty bit in buffer_jbddirty
2099 * instead of buffer_dirty. We should not see a dirty bit set
2100 * here because we clear it in do_get_write_access but e.g.
2101 * tune2fs can modify the sb and set the dirty bit at any time
2102 * so we try to gracefully handle that.
2104 if (buffer_dirty(bh
))
2105 warn_dirty_buffer(bh
);
2106 if (test_clear_buffer_dirty(bh
) ||
2107 test_clear_buffer_jbddirty(bh
))
2111 if (jh
->b_transaction
)
2112 __journal_temp_unlink_buffer(jh
);
2114 journal_grab_journal_head(bh
);
2115 jh
->b_transaction
= transaction
;
2119 J_ASSERT_JH(jh
, !jh
->b_committed_data
);
2120 J_ASSERT_JH(jh
, !jh
->b_frozen_data
);
2123 list
= &transaction
->t_sync_datalist
;
2126 transaction
->t_nr_buffers
++;
2127 list
= &transaction
->t_buffers
;
2130 list
= &transaction
->t_forget
;
2133 list
= &transaction
->t_iobuf_list
;
2136 list
= &transaction
->t_shadow_list
;
2139 list
= &transaction
->t_log_list
;
2142 list
= &transaction
->t_reserved_list
;
2145 list
= &transaction
->t_locked_list
;
2149 __blist_add_buffer(list
, jh
);
2150 jh
->b_jlist
= jlist
;
2153 set_buffer_jbddirty(bh
);
2156 void journal_file_buffer(struct journal_head
*jh
,
2157 transaction_t
*transaction
, int jlist
)
2159 jbd_lock_bh_state(jh2bh(jh
));
2160 spin_lock(&transaction
->t_journal
->j_list_lock
);
2161 __journal_file_buffer(jh
, transaction
, jlist
);
2162 spin_unlock(&transaction
->t_journal
->j_list_lock
);
2163 jbd_unlock_bh_state(jh2bh(jh
));
2167 * Remove a buffer from its current buffer list in preparation for
2168 * dropping it from its current transaction entirely. If the buffer has
2169 * already started to be used by a subsequent transaction, refile the
2170 * buffer on that transaction's metadata list.
2172 * Called under j_list_lock
2173 * Called under jbd_lock_bh_state(jh2bh(jh))
2175 * jh and bh may be already free when this function returns
2177 void __journal_refile_buffer(struct journal_head
*jh
)
2179 int was_dirty
, jlist
;
2180 struct buffer_head
*bh
= jh2bh(jh
);
2182 J_ASSERT_JH(jh
, jbd_is_locked_bh_state(bh
));
2183 if (jh
->b_transaction
)
2184 assert_spin_locked(&jh
->b_transaction
->t_journal
->j_list_lock
);
2186 /* If the buffer is now unused, just drop it. */
2187 if (jh
->b_next_transaction
== NULL
) {
2188 __journal_unfile_buffer(jh
);
2193 * It has been modified by a later transaction: add it to the new
2194 * transaction's metadata list.
2197 was_dirty
= test_clear_buffer_jbddirty(bh
);
2198 __journal_temp_unlink_buffer(jh
);
2200 * We set b_transaction here because b_next_transaction will inherit
2201 * our jh reference and thus __journal_file_buffer() must not take a
2204 jh
->b_transaction
= jh
->b_next_transaction
;
2205 jh
->b_next_transaction
= NULL
;
2206 if (buffer_freed(bh
))
2208 else if (jh
->b_modified
)
2209 jlist
= BJ_Metadata
;
2211 jlist
= BJ_Reserved
;
2212 __journal_file_buffer(jh
, jh
->b_transaction
, jlist
);
2213 J_ASSERT_JH(jh
, jh
->b_transaction
->t_state
== T_RUNNING
);
2216 set_buffer_jbddirty(bh
);
2220 * __journal_refile_buffer() with necessary locking added. We take our bh
2221 * reference so that we can safely unlock bh.
2223 * The jh and bh may be freed by this call.
2225 void journal_refile_buffer(journal_t
*journal
, struct journal_head
*jh
)
2227 struct buffer_head
*bh
= jh2bh(jh
);
2229 /* Get reference so that buffer cannot be freed before we unlock it */
2231 jbd_lock_bh_state(bh
);
2232 spin_lock(&journal
->j_list_lock
);
2233 __journal_refile_buffer(jh
);
2234 jbd_unlock_bh_state(bh
);
2235 spin_unlock(&journal
->j_list_lock
);