2 * linux/fs/jbd/commit.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 * Journal commit routines for the generic filesystem journaling code;
13 * part of the ext2fs journaling system.
16 #include <linux/time.h>
18 #include <linux/jbd.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
22 #include <linux/pagemap.h>
25 * Default IO end handler for temporary BJ_IO buffer_heads.
27 static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
31 set_buffer_uptodate(bh);
33 clear_buffer_uptodate(bh);
38 * When an ext3-ordered file is truncated, it is possible that many pages are
39 * not sucessfully freed, because they are attached to a committing transaction.
40 * After the transaction commits, these pages are left on the LRU, with no
41 * ->mapping, and with attached buffers. These pages are trivially reclaimable
42 * by the VM, but their apparent absence upsets the VM accounting, and it makes
43 * the numbers in /proc/meminfo look odd.
45 * So here, we have a buffer which has just come off the forget list. Look to
46 * see if we can strip all buffers from the backing page.
48 * Called under lock_journal(), and possibly under journal_datalist_lock. The
49 * caller provided us with a ref against the buffer, and we drop that here.
51 static void release_buffer_page(struct buffer_head *bh)
57 if (atomic_read(&bh->b_count) != 1)
65 /* OK, it's a truncated page */
66 if (TestSetPageLocked(page))
71 try_to_free_buffers(page);
73 page_cache_release(page);
81 * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is
82 * held. For ranking reasons we must trylock. If we lose, schedule away and
83 * return 0. j_list_lock is dropped in this case.
85 static int inverted_lock(journal_t *journal, struct buffer_head *bh)
87 if (!jbd_trylock_bh_state(bh)) {
88 spin_unlock(&journal->j_list_lock);
95 /* Done it all: now write the commit record. We should have
96 * cleaned up our previous buffers by now, so if we are in abort
97 * mode we can now just skip the rest of the journal write
100 * Returns 1 if the journal needs to be aborted or 0 on success
102 static int journal_write_commit_record(journal_t *journal,
103 transaction_t *commit_transaction)
105 struct journal_head *descriptor;
106 struct buffer_head *bh;
108 int barrier_done = 0;
110 if (is_journal_aborted(journal))
113 descriptor = journal_get_descriptor_buffer(journal);
117 bh = jh2bh(descriptor);
119 /* AKPM: buglet - add `i' to tmp! */
120 for (i = 0; i < bh->b_size; i += 512) {
121 journal_header_t *tmp = (journal_header_t*)bh->b_data;
122 tmp->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
123 tmp->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK);
124 tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
127 JBUFFER_TRACE(descriptor, "write commit block");
128 set_buffer_dirty(bh);
129 if (journal->j_flags & JFS_BARRIER) {
130 set_buffer_ordered(bh);
133 ret = sync_dirty_buffer(bh);
134 /* is it possible for another commit to fail at roughly
135 * the same time as this one? If so, we don't want to
136 * trust the barrier flag in the super, but instead want
137 * to remember if we sent a barrier request
139 if (ret == -EOPNOTSUPP && barrier_done) {
140 char b[BDEVNAME_SIZE];
143 "JBD: barrier-based sync failed on %s - "
144 "disabling barriers\n",
145 bdevname(journal->j_dev, b));
146 spin_lock(&journal->j_state_lock);
147 journal->j_flags &= ~JFS_BARRIER;
148 spin_unlock(&journal->j_state_lock);
150 /* And try again, without the barrier */
151 clear_buffer_ordered(bh);
152 set_buffer_uptodate(bh);
153 set_buffer_dirty(bh);
154 ret = sync_dirty_buffer(bh);
156 put_bh(bh); /* One for getblk() */
157 journal_put_journal_head(descriptor);
159 return (ret == -EIO);
162 static void journal_do_submit_data(struct buffer_head **wbuf, int bufs)
166 for (i = 0; i < bufs; i++) {
167 wbuf[i]->b_end_io = end_buffer_write_sync;
168 /* We use-up our safety reference in submit_bh() */
169 submit_bh(WRITE, wbuf[i]);
174 * Submit all the data buffers to disk
176 static void journal_submit_data_buffers(journal_t *journal,
177 transaction_t *commit_transaction)
179 struct journal_head *jh;
180 struct buffer_head *bh;
183 struct buffer_head **wbuf = journal->j_wbuf;
186 * Whenever we unlock the journal and sleep, things can get added
187 * onto ->t_sync_datalist, so we have to keep looping back to
188 * write_out_data until we *know* that the list is empty.
190 * Cleanup any flushed data buffers from the data list. Even in
191 * abort mode, we want to flush this out as soon as possible.
195 spin_lock(&journal->j_list_lock);
197 while (commit_transaction->t_sync_datalist) {
198 jh = commit_transaction->t_sync_datalist;
202 /* Get reference just to make sure buffer does not disappear
203 * when we are forced to drop various locks */
205 /* If the buffer is dirty, we need to submit IO and hence
206 * we need the buffer lock. We try to lock the buffer without
207 * blocking. If we fail, we need to drop j_list_lock and do
208 * blocking lock_buffer().
210 if (buffer_dirty(bh)) {
211 if (test_set_buffer_locked(bh)) {
212 BUFFER_TRACE(bh, "needs blocking lock");
213 spin_unlock(&journal->j_list_lock);
214 /* Write out all data to prevent deadlocks */
215 journal_do_submit_data(wbuf, bufs);
218 spin_lock(&journal->j_list_lock);
222 /* We have to get bh_state lock. Again out of order, sigh. */
223 if (!inverted_lock(journal, bh)) {
224 jbd_lock_bh_state(bh);
225 spin_lock(&journal->j_list_lock);
227 /* Someone already cleaned up the buffer? */
229 || jh->b_transaction != commit_transaction
230 || jh->b_jlist != BJ_SyncData) {
231 jbd_unlock_bh_state(bh);
234 BUFFER_TRACE(bh, "already cleaned up");
238 if (locked && test_clear_buffer_dirty(bh)) {
239 BUFFER_TRACE(bh, "needs writeout, adding to array");
241 __journal_file_buffer(jh, commit_transaction,
243 jbd_unlock_bh_state(bh);
244 if (bufs == journal->j_wbufsize) {
245 spin_unlock(&journal->j_list_lock);
246 journal_do_submit_data(wbuf, bufs);
250 } else if (!locked && buffer_locked(bh)) {
251 __journal_file_buffer(jh, commit_transaction,
253 jbd_unlock_bh_state(bh);
256 BUFFER_TRACE(bh, "writeout complete: unfile");
257 __journal_unfile_buffer(jh);
258 jbd_unlock_bh_state(bh);
261 journal_remove_journal_head(bh);
262 /* Once for our safety reference, once for
263 * journal_remove_journal_head() */
268 if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
269 spin_unlock(&journal->j_list_lock);
273 spin_unlock(&journal->j_list_lock);
274 journal_do_submit_data(wbuf, bufs);
278 * journal_commit_transaction
280 * The primary function for committing a transaction to the log. This
281 * function is called by the journal thread to begin a complete commit.
283 void journal_commit_transaction(journal_t *journal)
285 transaction_t *commit_transaction;
286 struct journal_head *jh, *new_jh, *descriptor;
287 struct buffer_head **wbuf = journal->j_wbuf;
291 unsigned long blocknr;
293 journal_header_t *header;
294 journal_block_tag_t *tag = NULL;
301 * First job: lock down the current transaction and wait for
302 * all outstanding updates to complete.
306 spin_lock(&journal->j_list_lock);
307 summarise_journal_usage(journal);
308 spin_unlock(&journal->j_list_lock);
311 /* Do we need to erase the effects of a prior journal_flush? */
312 if (journal->j_flags & JFS_FLUSHED) {
313 jbd_debug(3, "super block updated\n");
314 journal_update_superblock(journal, 1);
316 jbd_debug(3, "superblock not updated\n");
319 J_ASSERT(journal->j_running_transaction != NULL);
320 J_ASSERT(journal->j_committing_transaction == NULL);
322 commit_transaction = journal->j_running_transaction;
323 J_ASSERT(commit_transaction->t_state == T_RUNNING);
325 jbd_debug(1, "JBD: starting commit of transaction %d\n",
326 commit_transaction->t_tid);
328 spin_lock(&journal->j_state_lock);
329 commit_transaction->t_state = T_LOCKED;
331 spin_lock(&commit_transaction->t_handle_lock);
332 while (commit_transaction->t_updates) {
335 prepare_to_wait(&journal->j_wait_updates, &wait,
336 TASK_UNINTERRUPTIBLE);
337 if (commit_transaction->t_updates) {
338 spin_unlock(&commit_transaction->t_handle_lock);
339 spin_unlock(&journal->j_state_lock);
341 spin_lock(&journal->j_state_lock);
342 spin_lock(&commit_transaction->t_handle_lock);
344 finish_wait(&journal->j_wait_updates, &wait);
346 spin_unlock(&commit_transaction->t_handle_lock);
348 J_ASSERT (commit_transaction->t_outstanding_credits <=
349 journal->j_max_transaction_buffers);
352 * First thing we are allowed to do is to discard any remaining
353 * BJ_Reserved buffers. Note, it is _not_ permissible to assume
354 * that there are no such buffers: if a large filesystem
355 * operation like a truncate needs to split itself over multiple
356 * transactions, then it may try to do a journal_restart() while
357 * there are still BJ_Reserved buffers outstanding. These must
358 * be released cleanly from the current transaction.
360 * In this case, the filesystem must still reserve write access
361 * again before modifying the buffer in the new transaction, but
362 * we do not require it to remember exactly which old buffers it
363 * has reserved. This is consistent with the existing behaviour
364 * that multiple journal_get_write_access() calls to the same
365 * buffer are perfectly permissable.
367 while (commit_transaction->t_reserved_list) {
368 jh = commit_transaction->t_reserved_list;
369 JBUFFER_TRACE(jh, "reserved, unused: refile");
371 * A journal_get_undo_access()+journal_release_buffer() may
372 * leave undo-committed data.
374 if (jh->b_committed_data) {
375 struct buffer_head *bh = jh2bh(jh);
377 jbd_lock_bh_state(bh);
378 jbd_free(jh->b_committed_data, bh->b_size);
379 jh->b_committed_data = NULL;
380 jbd_unlock_bh_state(bh);
382 journal_refile_buffer(journal, jh);
386 * Now try to drop any written-back buffers from the journal's
387 * checkpoint lists. We do this *before* commit because it potentially
390 spin_lock(&journal->j_list_lock);
391 __journal_clean_checkpoint_list(journal);
392 spin_unlock(&journal->j_list_lock);
394 jbd_debug (3, "JBD: commit phase 1\n");
397 * Switch to a new revoke table.
399 journal_switch_revoke_table(journal);
401 commit_transaction->t_state = T_FLUSH;
402 journal->j_committing_transaction = commit_transaction;
403 journal->j_running_transaction = NULL;
404 commit_transaction->t_log_start = journal->j_head;
405 wake_up(&journal->j_wait_transaction_locked);
406 spin_unlock(&journal->j_state_lock);
408 jbd_debug (3, "JBD: commit phase 2\n");
411 * First, drop modified flag: all accesses to the buffers
412 * will be tracked for a new trasaction only -bzzz
414 spin_lock(&journal->j_list_lock);
415 if (commit_transaction->t_buffers) {
416 new_jh = jh = commit_transaction->t_buffers->b_tnext;
418 J_ASSERT_JH(new_jh, new_jh->b_modified == 1 ||
419 new_jh->b_modified == 0);
420 new_jh->b_modified = 0;
421 new_jh = new_jh->b_tnext;
422 } while (new_jh != jh);
424 spin_unlock(&journal->j_list_lock);
427 * Now start flushing things to disk, in the order they appear
428 * on the transaction lists. Data blocks go first.
431 journal_submit_data_buffers(journal, commit_transaction);
434 * Wait for all previously submitted IO to complete.
436 spin_lock(&journal->j_list_lock);
437 while (commit_transaction->t_locked_list) {
438 struct buffer_head *bh;
440 jh = commit_transaction->t_locked_list->b_tprev;
443 if (buffer_locked(bh)) {
444 spin_unlock(&journal->j_list_lock);
446 if (unlikely(!buffer_uptodate(bh)))
448 spin_lock(&journal->j_list_lock);
450 if (!inverted_lock(journal, bh)) {
452 spin_lock(&journal->j_list_lock);
455 if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) {
456 __journal_unfile_buffer(jh);
457 jbd_unlock_bh_state(bh);
458 journal_remove_journal_head(bh);
461 jbd_unlock_bh_state(bh);
464 cond_resched_lock(&journal->j_list_lock);
466 spin_unlock(&journal->j_list_lock);
469 journal_abort(journal, err);
471 journal_write_revoke_records(journal, commit_transaction);
473 jbd_debug(3, "JBD: commit phase 2\n");
476 * If we found any dirty or locked buffers, then we should have
477 * looped back up to the write_out_data label. If there weren't
478 * any then journal_clean_data_list should have wiped the list
479 * clean by now, so check that it is in fact empty.
481 J_ASSERT (commit_transaction->t_sync_datalist == NULL);
483 jbd_debug (3, "JBD: commit phase 3\n");
486 * Way to go: we have now written out all of the data for a
487 * transaction! Now comes the tricky part: we need to write out
488 * metadata. Loop over the transaction's entire buffer list:
490 commit_transaction->t_state = T_COMMIT;
494 while (commit_transaction->t_buffers) {
496 /* Find the next buffer to be journaled... */
498 jh = commit_transaction->t_buffers;
500 /* If we're in abort mode, we just un-journal the buffer and
501 release it for background writing. */
503 if (is_journal_aborted(journal)) {
504 JBUFFER_TRACE(jh, "journal is aborting: refile");
505 journal_refile_buffer(journal, jh);
506 /* If that was the last one, we need to clean up
507 * any descriptor buffers which may have been
508 * already allocated, even if we are now
510 if (!commit_transaction->t_buffers)
511 goto start_journal_io;
515 /* Make sure we have a descriptor block in which to
516 record the metadata buffer. */
519 struct buffer_head *bh;
521 J_ASSERT (bufs == 0);
523 jbd_debug(4, "JBD: get descriptor\n");
525 descriptor = journal_get_descriptor_buffer(journal);
527 journal_abort(journal, -EIO);
531 bh = jh2bh(descriptor);
532 jbd_debug(4, "JBD: got buffer %llu (%p)\n",
533 (unsigned long long)bh->b_blocknr, bh->b_data);
534 header = (journal_header_t *)&bh->b_data[0];
535 header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
536 header->h_blocktype = cpu_to_be32(JFS_DESCRIPTOR_BLOCK);
537 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
539 tagp = &bh->b_data[sizeof(journal_header_t)];
540 space_left = bh->b_size - sizeof(journal_header_t);
542 set_buffer_jwrite(bh);
543 set_buffer_dirty(bh);
546 /* Record it so that we can wait for IO
548 BUFFER_TRACE(bh, "ph3: file as descriptor");
549 journal_file_buffer(descriptor, commit_transaction,
553 /* Where is the buffer to be written? */
555 err = journal_next_log_block(journal, &blocknr);
556 /* If the block mapping failed, just abandon the buffer
557 and repeat this loop: we'll fall into the
558 refile-on-abort condition above. */
560 journal_abort(journal, err);
565 * start_this_handle() uses t_outstanding_credits to determine
566 * the free space in the log, but this counter is changed
567 * by journal_next_log_block() also.
569 commit_transaction->t_outstanding_credits--;
571 /* Bump b_count to prevent truncate from stumbling over
572 the shadowed buffer! @@@ This can go if we ever get
573 rid of the BJ_IO/BJ_Shadow pairing of buffers. */
574 atomic_inc(&jh2bh(jh)->b_count);
576 /* Make a temporary IO buffer with which to write it out
577 (this will requeue both the metadata buffer and the
578 temporary IO buffer). new_bh goes on BJ_IO*/
580 set_bit(BH_JWrite, &jh2bh(jh)->b_state);
582 * akpm: journal_write_metadata_buffer() sets
583 * new_bh->b_transaction to commit_transaction.
584 * We need to clean this up before we release new_bh
585 * (which is of type BJ_IO)
587 JBUFFER_TRACE(jh, "ph3: write metadata");
588 flags = journal_write_metadata_buffer(commit_transaction,
589 jh, &new_jh, blocknr);
590 set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
591 wbuf[bufs++] = jh2bh(new_jh);
593 /* Record the new block's tag in the current descriptor
598 tag_flag |= JFS_FLAG_ESCAPE;
600 tag_flag |= JFS_FLAG_SAME_UUID;
602 tag = (journal_block_tag_t *) tagp;
603 tag->t_blocknr = cpu_to_be32(jh2bh(jh)->b_blocknr);
604 tag->t_flags = cpu_to_be32(tag_flag);
605 tagp += sizeof(journal_block_tag_t);
606 space_left -= sizeof(journal_block_tag_t);
609 memcpy (tagp, journal->j_uuid, 16);
615 /* If there's no more to do, or if the descriptor is full,
618 if (bufs == journal->j_wbufsize ||
619 commit_transaction->t_buffers == NULL ||
620 space_left < sizeof(journal_block_tag_t) + 16) {
622 jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
624 /* Write an end-of-descriptor marker before
625 submitting the IOs. "tag" still points to
626 the last tag we set up. */
628 tag->t_flags |= cpu_to_be32(JFS_FLAG_LAST_TAG);
631 for (i = 0; i < bufs; i++) {
632 struct buffer_head *bh = wbuf[i];
634 clear_buffer_dirty(bh);
635 set_buffer_uptodate(bh);
636 bh->b_end_io = journal_end_buffer_io_sync;
637 submit_bh(WRITE, bh);
641 /* Force a new descriptor to be generated next
642 time round the loop. */
648 /* Lo and behold: we have just managed to send a transaction to
649 the log. Before we can commit it, wait for the IO so far to
650 complete. Control buffers being written are on the
651 transaction's t_log_list queue, and metadata buffers are on
652 the t_iobuf_list queue.
654 Wait for the buffers in reverse order. That way we are
655 less likely to be woken up until all IOs have completed, and
656 so we incur less scheduling load.
659 jbd_debug(3, "JBD: commit phase 4\n");
662 * akpm: these are BJ_IO, and j_list_lock is not needed.
663 * See __journal_try_to_free_buffer.
666 while (commit_transaction->t_iobuf_list != NULL) {
667 struct buffer_head *bh;
669 jh = commit_transaction->t_iobuf_list->b_tprev;
671 if (buffer_locked(bh)) {
678 if (unlikely(!buffer_uptodate(bh)))
681 clear_buffer_jwrite(bh);
683 JBUFFER_TRACE(jh, "ph4: unfile after journal write");
684 journal_unfile_buffer(journal, jh);
687 * ->t_iobuf_list should contain only dummy buffer_heads
688 * which were created by journal_write_metadata_buffer().
690 BUFFER_TRACE(bh, "dumping temporary bh");
691 journal_put_journal_head(jh);
693 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
694 free_buffer_head(bh);
696 /* We also have to unlock and free the corresponding
698 jh = commit_transaction->t_shadow_list->b_tprev;
700 clear_bit(BH_JWrite, &bh->b_state);
701 J_ASSERT_BH(bh, buffer_jbddirty(bh));
703 /* The metadata is now released for reuse, but we need
704 to remember it against this transaction so that when
705 we finally commit, we can do any checkpointing
707 JBUFFER_TRACE(jh, "file as BJ_Forget");
708 journal_file_buffer(jh, commit_transaction, BJ_Forget);
709 /* Wake up any transactions which were waiting for this
711 wake_up_bit(&bh->b_state, BH_Unshadow);
712 JBUFFER_TRACE(jh, "brelse shadowed buffer");
716 J_ASSERT (commit_transaction->t_shadow_list == NULL);
718 jbd_debug(3, "JBD: commit phase 5\n");
720 /* Here we wait for the revoke record and descriptor record buffers */
722 while (commit_transaction->t_log_list != NULL) {
723 struct buffer_head *bh;
725 jh = commit_transaction->t_log_list->b_tprev;
727 if (buffer_locked(bh)) {
729 goto wait_for_ctlbuf;
732 goto wait_for_ctlbuf;
734 if (unlikely(!buffer_uptodate(bh)))
737 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
738 clear_buffer_jwrite(bh);
739 journal_unfile_buffer(journal, jh);
740 journal_put_journal_head(jh);
741 __brelse(bh); /* One for getblk */
742 /* AKPM: bforget here */
745 jbd_debug(3, "JBD: commit phase 6\n");
747 if (journal_write_commit_record(journal, commit_transaction))
751 journal_abort(journal, err);
753 /* End of a transaction! Finally, we can do checkpoint
754 processing: any buffers committed as a result of this
755 transaction can be removed from any checkpoint list it was on
758 jbd_debug(3, "JBD: commit phase 7\n");
760 J_ASSERT(commit_transaction->t_sync_datalist == NULL);
761 J_ASSERT(commit_transaction->t_buffers == NULL);
762 J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
763 J_ASSERT(commit_transaction->t_iobuf_list == NULL);
764 J_ASSERT(commit_transaction->t_shadow_list == NULL);
765 J_ASSERT(commit_transaction->t_log_list == NULL);
769 * As there are other places (journal_unmap_buffer()) adding buffers
770 * to this list we have to be careful and hold the j_list_lock.
772 spin_lock(&journal->j_list_lock);
773 while (commit_transaction->t_forget) {
774 transaction_t *cp_transaction;
775 struct buffer_head *bh;
777 jh = commit_transaction->t_forget;
778 spin_unlock(&journal->j_list_lock);
780 jbd_lock_bh_state(bh);
781 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
782 jh->b_transaction == journal->j_running_transaction);
785 * If there is undo-protected committed data against
786 * this buffer, then we can remove it now. If it is a
787 * buffer needing such protection, the old frozen_data
788 * field now points to a committed version of the
789 * buffer, so rotate that field to the new committed
792 * Otherwise, we can just throw away the frozen data now.
794 if (jh->b_committed_data) {
795 jbd_free(jh->b_committed_data, bh->b_size);
796 jh->b_committed_data = NULL;
797 if (jh->b_frozen_data) {
798 jh->b_committed_data = jh->b_frozen_data;
799 jh->b_frozen_data = NULL;
801 } else if (jh->b_frozen_data) {
802 jbd_free(jh->b_frozen_data, bh->b_size);
803 jh->b_frozen_data = NULL;
806 spin_lock(&journal->j_list_lock);
807 cp_transaction = jh->b_cp_transaction;
808 if (cp_transaction) {
809 JBUFFER_TRACE(jh, "remove from old cp transaction");
810 __journal_remove_checkpoint(jh);
813 /* Only re-checkpoint the buffer_head if it is marked
814 * dirty. If the buffer was added to the BJ_Forget list
815 * by journal_forget, it may no longer be dirty and
816 * there's no point in keeping a checkpoint record for
819 /* A buffer which has been freed while still being
820 * journaled by a previous transaction may end up still
821 * being dirty here, but we want to avoid writing back
822 * that buffer in the future now that the last use has
823 * been committed. That's not only a performance gain,
824 * it also stops aliasing problems if the buffer is left
825 * behind for writeback and gets reallocated for another
826 * use in a different page. */
827 if (buffer_freed(bh)) {
828 clear_buffer_freed(bh);
829 clear_buffer_jbddirty(bh);
832 if (buffer_jbddirty(bh)) {
833 JBUFFER_TRACE(jh, "add to new checkpointing trans");
834 __journal_insert_checkpoint(jh, commit_transaction);
835 JBUFFER_TRACE(jh, "refile for checkpoint writeback");
836 __journal_refile_buffer(jh);
837 jbd_unlock_bh_state(bh);
839 J_ASSERT_BH(bh, !buffer_dirty(bh));
840 /* The buffer on BJ_Forget list and not jbddirty means
841 * it has been freed by this transaction and hence it
842 * could not have been reallocated until this
843 * transaction has committed. *BUT* it could be
844 * reallocated once we have written all the data to
845 * disk and before we process the buffer on BJ_Forget
847 JBUFFER_TRACE(jh, "refile or unfile freed buffer");
848 __journal_refile_buffer(jh);
849 if (!jh->b_transaction) {
850 jbd_unlock_bh_state(bh);
852 journal_remove_journal_head(bh);
853 release_buffer_page(bh);
855 jbd_unlock_bh_state(bh);
857 cond_resched_lock(&journal->j_list_lock);
859 spin_unlock(&journal->j_list_lock);
861 * This is a bit sleazy. We use j_list_lock to protect transition
862 * of a transaction into T_FINISHED state and calling
863 * __journal_drop_transaction(). Otherwise we could race with
864 * other checkpointing code processing the transaction...
866 spin_lock(&journal->j_state_lock);
867 spin_lock(&journal->j_list_lock);
869 * Now recheck if some buffers did not get attached to the transaction
870 * while the lock was dropped...
872 if (commit_transaction->t_forget) {
873 spin_unlock(&journal->j_list_lock);
874 spin_unlock(&journal->j_state_lock);
878 /* Done with this transaction! */
880 jbd_debug(3, "JBD: commit phase 8\n");
882 J_ASSERT(commit_transaction->t_state == T_COMMIT);
884 commit_transaction->t_state = T_FINISHED;
885 J_ASSERT(commit_transaction == journal->j_committing_transaction);
886 journal->j_commit_sequence = commit_transaction->t_tid;
887 journal->j_committing_transaction = NULL;
888 spin_unlock(&journal->j_state_lock);
890 if (commit_transaction->t_checkpoint_list == NULL &&
891 commit_transaction->t_checkpoint_io_list == NULL) {
892 __journal_drop_transaction(journal, commit_transaction);
894 if (journal->j_checkpoint_transactions == NULL) {
895 journal->j_checkpoint_transactions = commit_transaction;
896 commit_transaction->t_cpnext = commit_transaction;
897 commit_transaction->t_cpprev = commit_transaction;
899 commit_transaction->t_cpnext =
900 journal->j_checkpoint_transactions;
901 commit_transaction->t_cpprev =
902 commit_transaction->t_cpnext->t_cpprev;
903 commit_transaction->t_cpnext->t_cpprev =
905 commit_transaction->t_cpprev->t_cpnext =
909 spin_unlock(&journal->j_list_lock);
911 jbd_debug(1, "JBD: commit %d complete, head %d\n",
912 journal->j_commit_sequence, journal->j_tail_sequence);
914 wake_up(&journal->j_wait_done_commit);