2 * linux/fs/jbd2/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/jbd2.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
22 #include <linux/pagemap.h>
23 #include <linux/jiffies.h>
24 #include <linux/crc32.h>
25 #include <linux/writeback.h>
26 #include <linux/backing-dev.h>
29 * Default IO end handler for temporary BJ_IO buffer_heads.
31 static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
35 set_buffer_uptodate(bh);
37 clear_buffer_uptodate(bh);
42 * When an ext4 file is truncated, it is possible that some pages are not
43 * successfully freed, because they are attached to a committing transaction.
44 * After the transaction commits, these pages are left on the LRU, with no
45 * ->mapping, and with attached buffers. These pages are trivially reclaimable
46 * by the VM, but their apparent absence upsets the VM accounting, and it makes
47 * the numbers in /proc/meminfo look odd.
49 * So here, we have a buffer which has just come off the forget list. Look to
50 * see if we can strip all buffers from the backing page.
52 * Called under lock_journal(), and possibly under journal_datalist_lock. The
53 * caller provided us with a ref against the buffer, and we drop that here.
55 static void release_buffer_page(struct buffer_head *bh)
61 if (atomic_read(&bh->b_count) != 1)
69 /* OK, it's a truncated page */
70 if (TestSetPageLocked(page))
75 try_to_free_buffers(page);
77 page_cache_release(page);
85 * Done it all: now submit the commit record. We should have
86 * cleaned up our previous buffers by now, so if we are in abort
87 * mode we can now just skip the rest of the journal write
90 * Returns 1 if the journal needs to be aborted or 0 on success
92 static int journal_submit_commit_record(journal_t *journal,
93 transaction_t *commit_transaction,
94 struct buffer_head **cbh,
97 struct journal_head *descriptor;
98 struct commit_header *tmp;
99 struct buffer_head *bh;
101 int barrier_done = 0;
102 struct timespec now = current_kernel_time();
104 if (is_journal_aborted(journal))
107 descriptor = jbd2_journal_get_descriptor_buffer(journal);
111 bh = jh2bh(descriptor);
113 tmp = (struct commit_header *)bh->b_data;
114 tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
115 tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK);
116 tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid);
117 tmp->h_commit_sec = cpu_to_be64(now.tv_sec);
118 tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec);
120 if (JBD2_HAS_COMPAT_FEATURE(journal,
121 JBD2_FEATURE_COMPAT_CHECKSUM)) {
122 tmp->h_chksum_type = JBD2_CRC32_CHKSUM;
123 tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE;
124 tmp->h_chksum[0] = cpu_to_be32(crc32_sum);
127 JBUFFER_TRACE(descriptor, "submit commit block");
130 set_buffer_dirty(bh);
131 set_buffer_uptodate(bh);
132 bh->b_end_io = journal_end_buffer_io_sync;
134 if (journal->j_flags & JBD2_BARRIER &&
135 !JBD2_HAS_INCOMPAT_FEATURE(journal,
136 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
137 set_buffer_ordered(bh);
140 ret = submit_bh(WRITE, bh);
142 clear_buffer_ordered(bh);
144 /* is it possible for another commit to fail at roughly
145 * the same time as this one? If so, we don't want to
146 * trust the barrier flag in the super, but instead want
147 * to remember if we sent a barrier request
149 if (ret == -EOPNOTSUPP && barrier_done) {
150 char b[BDEVNAME_SIZE];
153 "JBD: barrier-based sync failed on %s - "
154 "disabling barriers\n",
155 bdevname(journal->j_dev, b));
156 spin_lock(&journal->j_state_lock);
157 journal->j_flags &= ~JBD2_BARRIER;
158 spin_unlock(&journal->j_state_lock);
160 /* And try again, without the barrier */
162 set_buffer_uptodate(bh);
163 set_buffer_dirty(bh);
164 ret = submit_bh(WRITE, bh);
171 * This function along with journal_submit_commit_record
172 * allows to write the commit record asynchronously.
174 static int journal_wait_on_commit_record(struct buffer_head *bh)
178 clear_buffer_dirty(bh);
181 if (unlikely(!buffer_uptodate(bh)))
183 put_bh(bh); /* One for getblk() */
184 jbd2_journal_put_journal_head(bh2jh(bh));
190 * write the filemap data using writepage() address_space_operations.
191 * We don't do block allocation here even for delalloc. We don't
192 * use writepages() because with dealyed allocation we may be doing
193 * block allocation in writepages().
195 static int journal_submit_inode_data_buffers(struct address_space *mapping)
198 struct writeback_control wbc = {
199 .sync_mode = WB_SYNC_ALL,
200 .nr_to_write = mapping->nrpages * 2,
202 .range_end = i_size_read(mapping->host),
206 ret = generic_writepages(mapping, &wbc);
211 * Submit all the data buffers of inode associated with the transaction to
214 * We are in a committing transaction. Therefore no new inode can be added to
215 * our inode list. We use JI_COMMIT_RUNNING flag to protect inode we currently
216 * operate on from being released while we write out pages.
218 static int journal_submit_data_buffers(journal_t *journal,
219 transaction_t *commit_transaction)
221 struct jbd2_inode *jinode;
223 struct address_space *mapping;
225 spin_lock(&journal->j_list_lock);
226 list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
227 mapping = jinode->i_vfs_inode->i_mapping;
228 jinode->i_flags |= JI_COMMIT_RUNNING;
229 spin_unlock(&journal->j_list_lock);
231 * submit the inode data buffers. We use writepage
232 * instead of writepages. Because writepages can do
233 * block allocation with delalloc. We need to write
234 * only allocated blocks here.
236 err = journal_submit_inode_data_buffers(mapping);
239 spin_lock(&journal->j_list_lock);
240 J_ASSERT(jinode->i_transaction == commit_transaction);
241 jinode->i_flags &= ~JI_COMMIT_RUNNING;
242 wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
244 spin_unlock(&journal->j_list_lock);
249 * Wait for data submitted for writeout, refile inodes to proper
250 * transaction if needed.
253 static int journal_finish_inode_data_buffers(journal_t *journal,
254 transaction_t *commit_transaction)
256 struct jbd2_inode *jinode, *next_i;
259 /* For locking, see the comment in journal_submit_data_buffers() */
260 spin_lock(&journal->j_list_lock);
261 list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
262 jinode->i_flags |= JI_COMMIT_RUNNING;
263 spin_unlock(&journal->j_list_lock);
264 err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping);
267 spin_lock(&journal->j_list_lock);
268 jinode->i_flags &= ~JI_COMMIT_RUNNING;
269 wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
272 /* Now refile inode to proper lists */
273 list_for_each_entry_safe(jinode, next_i,
274 &commit_transaction->t_inode_list, i_list) {
275 list_del(&jinode->i_list);
276 if (jinode->i_next_transaction) {
277 jinode->i_transaction = jinode->i_next_transaction;
278 jinode->i_next_transaction = NULL;
279 list_add(&jinode->i_list,
280 &jinode->i_transaction->t_inode_list);
282 jinode->i_transaction = NULL;
285 spin_unlock(&journal->j_list_lock);
290 static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
292 struct page *page = bh->b_page;
296 addr = kmap_atomic(page, KM_USER0);
297 checksum = crc32_be(crc32_sum,
298 (void *)(addr + offset_in_page(bh->b_data)), bh->b_size);
299 kunmap_atomic(addr, KM_USER0);
304 static void write_tag_block(int tag_bytes, journal_block_tag_t *tag,
305 unsigned long long block)
307 tag->t_blocknr = cpu_to_be32(block & (u32)~0);
308 if (tag_bytes > JBD2_TAG_SIZE32)
309 tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
313 * jbd2_journal_commit_transaction
315 * The primary function for committing a transaction to the log. This
316 * function is called by the journal thread to begin a complete commit.
318 void jbd2_journal_commit_transaction(journal_t *journal)
320 struct transaction_stats_s stats;
321 transaction_t *commit_transaction;
322 struct journal_head *jh, *new_jh, *descriptor;
323 struct buffer_head **wbuf = journal->j_wbuf;
327 unsigned long long blocknr;
329 journal_header_t *header;
330 journal_block_tag_t *tag = NULL;
335 int tag_bytes = journal_tag_bytes(journal);
336 struct buffer_head *cbh = NULL; /* For transactional checksums */
337 __u32 crc32_sum = ~0;
340 * First job: lock down the current transaction and wait for
341 * all outstanding updates to complete.
345 spin_lock(&journal->j_list_lock);
346 summarise_journal_usage(journal);
347 spin_unlock(&journal->j_list_lock);
350 /* Do we need to erase the effects of a prior jbd2_journal_flush? */
351 if (journal->j_flags & JBD2_FLUSHED) {
352 jbd_debug(3, "super block updated\n");
353 jbd2_journal_update_superblock(journal, 1);
355 jbd_debug(3, "superblock not updated\n");
358 J_ASSERT(journal->j_running_transaction != NULL);
359 J_ASSERT(journal->j_committing_transaction == NULL);
361 commit_transaction = journal->j_running_transaction;
362 J_ASSERT(commit_transaction->t_state == T_RUNNING);
364 jbd_debug(1, "JBD: starting commit of transaction %d\n",
365 commit_transaction->t_tid);
367 spin_lock(&journal->j_state_lock);
368 commit_transaction->t_state = T_LOCKED;
370 stats.u.run.rs_wait = commit_transaction->t_max_wait;
371 stats.u.run.rs_locked = jiffies;
372 stats.u.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
373 stats.u.run.rs_locked);
375 spin_lock(&commit_transaction->t_handle_lock);
376 while (commit_transaction->t_updates) {
379 prepare_to_wait(&journal->j_wait_updates, &wait,
380 TASK_UNINTERRUPTIBLE);
381 if (commit_transaction->t_updates) {
382 spin_unlock(&commit_transaction->t_handle_lock);
383 spin_unlock(&journal->j_state_lock);
385 spin_lock(&journal->j_state_lock);
386 spin_lock(&commit_transaction->t_handle_lock);
388 finish_wait(&journal->j_wait_updates, &wait);
390 spin_unlock(&commit_transaction->t_handle_lock);
392 J_ASSERT (commit_transaction->t_outstanding_credits <=
393 journal->j_max_transaction_buffers);
396 * First thing we are allowed to do is to discard any remaining
397 * BJ_Reserved buffers. Note, it is _not_ permissible to assume
398 * that there are no such buffers: if a large filesystem
399 * operation like a truncate needs to split itself over multiple
400 * transactions, then it may try to do a jbd2_journal_restart() while
401 * there are still BJ_Reserved buffers outstanding. These must
402 * be released cleanly from the current transaction.
404 * In this case, the filesystem must still reserve write access
405 * again before modifying the buffer in the new transaction, but
406 * we do not require it to remember exactly which old buffers it
407 * has reserved. This is consistent with the existing behaviour
408 * that multiple jbd2_journal_get_write_access() calls to the same
409 * buffer are perfectly permissable.
411 while (commit_transaction->t_reserved_list) {
412 jh = commit_transaction->t_reserved_list;
413 JBUFFER_TRACE(jh, "reserved, unused: refile");
415 * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may
416 * leave undo-committed data.
418 if (jh->b_committed_data) {
419 struct buffer_head *bh = jh2bh(jh);
421 jbd_lock_bh_state(bh);
422 jbd2_free(jh->b_committed_data, bh->b_size);
423 jh->b_committed_data = NULL;
424 jbd_unlock_bh_state(bh);
426 jbd2_journal_refile_buffer(journal, jh);
430 * Now try to drop any written-back buffers from the journal's
431 * checkpoint lists. We do this *before* commit because it potentially
434 spin_lock(&journal->j_list_lock);
435 __jbd2_journal_clean_checkpoint_list(journal);
436 spin_unlock(&journal->j_list_lock);
438 jbd_debug (3, "JBD: commit phase 1\n");
441 * Switch to a new revoke table.
443 jbd2_journal_switch_revoke_table(journal);
445 stats.u.run.rs_flushing = jiffies;
446 stats.u.run.rs_locked = jbd2_time_diff(stats.u.run.rs_locked,
447 stats.u.run.rs_flushing);
449 commit_transaction->t_state = T_FLUSH;
450 journal->j_committing_transaction = commit_transaction;
451 journal->j_running_transaction = NULL;
452 commit_transaction->t_log_start = journal->j_head;
453 wake_up(&journal->j_wait_transaction_locked);
454 spin_unlock(&journal->j_state_lock);
456 jbd_debug (3, "JBD: commit phase 2\n");
459 * Now start flushing things to disk, in the order they appear
460 * on the transaction lists. Data blocks go first.
462 err = journal_submit_data_buffers(journal, commit_transaction);
464 jbd2_journal_abort(journal, err);
466 jbd2_journal_write_revoke_records(journal, commit_transaction);
468 jbd_debug(3, "JBD: commit phase 2\n");
471 * Way to go: we have now written out all of the data for a
472 * transaction! Now comes the tricky part: we need to write out
473 * metadata. Loop over the transaction's entire buffer list:
475 spin_lock(&journal->j_state_lock);
476 commit_transaction->t_state = T_COMMIT;
477 spin_unlock(&journal->j_state_lock);
479 stats.u.run.rs_logging = jiffies;
480 stats.u.run.rs_flushing = jbd2_time_diff(stats.u.run.rs_flushing,
481 stats.u.run.rs_logging);
482 stats.u.run.rs_blocks = commit_transaction->t_outstanding_credits;
483 stats.u.run.rs_blocks_logged = 0;
485 J_ASSERT(commit_transaction->t_nr_buffers <=
486 commit_transaction->t_outstanding_credits);
491 while (commit_transaction->t_buffers) {
493 /* Find the next buffer to be journaled... */
495 jh = commit_transaction->t_buffers;
497 /* If we're in abort mode, we just un-journal the buffer and
498 release it for background writing. */
500 if (is_journal_aborted(journal)) {
501 JBUFFER_TRACE(jh, "journal is aborting: refile");
502 jbd2_journal_refile_buffer(journal, jh);
503 /* If that was the last one, we need to clean up
504 * any descriptor buffers which may have been
505 * already allocated, even if we are now
507 if (!commit_transaction->t_buffers)
508 goto start_journal_io;
512 /* Make sure we have a descriptor block in which to
513 record the metadata buffer. */
516 struct buffer_head *bh;
518 J_ASSERT (bufs == 0);
520 jbd_debug(4, "JBD: get descriptor\n");
522 descriptor = jbd2_journal_get_descriptor_buffer(journal);
524 jbd2_journal_abort(journal, -EIO);
528 bh = jh2bh(descriptor);
529 jbd_debug(4, "JBD: got buffer %llu (%p)\n",
530 (unsigned long long)bh->b_blocknr, bh->b_data);
531 header = (journal_header_t *)&bh->b_data[0];
532 header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
533 header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK);
534 header->h_sequence = cpu_to_be32(commit_transaction->t_tid);
536 tagp = &bh->b_data[sizeof(journal_header_t)];
537 space_left = bh->b_size - sizeof(journal_header_t);
539 set_buffer_jwrite(bh);
540 set_buffer_dirty(bh);
543 /* Record it so that we can wait for IO
545 BUFFER_TRACE(bh, "ph3: file as descriptor");
546 jbd2_journal_file_buffer(descriptor, commit_transaction,
550 /* Where is the buffer to be written? */
552 err = jbd2_journal_next_log_block(journal, &blocknr);
553 /* If the block mapping failed, just abandon the buffer
554 and repeat this loop: we'll fall into the
555 refile-on-abort condition above. */
557 jbd2_journal_abort(journal, err);
562 * start_this_handle() uses t_outstanding_credits to determine
563 * the free space in the log, but this counter is changed
564 * by jbd2_journal_next_log_block() also.
566 commit_transaction->t_outstanding_credits--;
568 /* Bump b_count to prevent truncate from stumbling over
569 the shadowed buffer! @@@ This can go if we ever get
570 rid of the BJ_IO/BJ_Shadow pairing of buffers. */
571 atomic_inc(&jh2bh(jh)->b_count);
573 /* Make a temporary IO buffer with which to write it out
574 (this will requeue both the metadata buffer and the
575 temporary IO buffer). new_bh goes on BJ_IO*/
577 set_bit(BH_JWrite, &jh2bh(jh)->b_state);
579 * akpm: jbd2_journal_write_metadata_buffer() sets
580 * new_bh->b_transaction to commit_transaction.
581 * We need to clean this up before we release new_bh
582 * (which is of type BJ_IO)
584 JBUFFER_TRACE(jh, "ph3: write metadata");
585 flags = jbd2_journal_write_metadata_buffer(commit_transaction,
586 jh, &new_jh, blocknr);
587 set_bit(BH_JWrite, &jh2bh(new_jh)->b_state);
588 wbuf[bufs++] = jh2bh(new_jh);
590 /* Record the new block's tag in the current descriptor
595 tag_flag |= JBD2_FLAG_ESCAPE;
597 tag_flag |= JBD2_FLAG_SAME_UUID;
599 tag = (journal_block_tag_t *) tagp;
600 write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
601 tag->t_flags = cpu_to_be32(tag_flag);
603 space_left -= tag_bytes;
606 memcpy (tagp, journal->j_uuid, 16);
612 /* If there's no more to do, or if the descriptor is full,
615 if (bufs == journal->j_wbufsize ||
616 commit_transaction->t_buffers == NULL ||
617 space_left < tag_bytes + 16) {
619 jbd_debug(4, "JBD: Submit %d IOs\n", bufs);
621 /* Write an end-of-descriptor marker before
622 submitting the IOs. "tag" still points to
623 the last tag we set up. */
625 tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG);
628 for (i = 0; i < bufs; i++) {
629 struct buffer_head *bh = wbuf[i];
633 if (JBD2_HAS_COMPAT_FEATURE(journal,
634 JBD2_FEATURE_COMPAT_CHECKSUM)) {
636 jbd2_checksum_data(crc32_sum, bh);
640 clear_buffer_dirty(bh);
641 set_buffer_uptodate(bh);
642 bh->b_end_io = journal_end_buffer_io_sync;
643 submit_bh(WRITE, bh);
646 stats.u.run.rs_blocks_logged += bufs;
648 /* Force a new descriptor to be generated next
649 time round the loop. */
655 /* Done it all: now write the commit record asynchronously. */
657 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
658 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
659 err = journal_submit_commit_record(journal, commit_transaction,
662 __jbd2_journal_abort_hard(journal);
666 * This is the right place to wait for data buffers both for ASYNC
667 * and !ASYNC commit. If commit is ASYNC, we need to wait only after
668 * the commit block went to disk (which happens above). If commit is
669 * SYNC, we need to wait for data buffers before we start writing
670 * commit block, which happens below in such setting.
672 err = journal_finish_inode_data_buffers(journal, commit_transaction);
674 jbd2_journal_abort(journal, err);
676 /* Lo and behold: we have just managed to send a transaction to
677 the log. Before we can commit it, wait for the IO so far to
678 complete. Control buffers being written are on the
679 transaction's t_log_list queue, and metadata buffers are on
680 the t_iobuf_list queue.
682 Wait for the buffers in reverse order. That way we are
683 less likely to be woken up until all IOs have completed, and
684 so we incur less scheduling load.
687 jbd_debug(3, "JBD: commit phase 3\n");
690 * akpm: these are BJ_IO, and j_list_lock is not needed.
691 * See __journal_try_to_free_buffer.
694 while (commit_transaction->t_iobuf_list != NULL) {
695 struct buffer_head *bh;
697 jh = commit_transaction->t_iobuf_list->b_tprev;
699 if (buffer_locked(bh)) {
706 if (unlikely(!buffer_uptodate(bh)))
709 clear_buffer_jwrite(bh);
711 JBUFFER_TRACE(jh, "ph4: unfile after journal write");
712 jbd2_journal_unfile_buffer(journal, jh);
715 * ->t_iobuf_list should contain only dummy buffer_heads
716 * which were created by jbd2_journal_write_metadata_buffer().
718 BUFFER_TRACE(bh, "dumping temporary bh");
719 jbd2_journal_put_journal_head(jh);
721 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
722 free_buffer_head(bh);
724 /* We also have to unlock and free the corresponding
726 jh = commit_transaction->t_shadow_list->b_tprev;
728 clear_bit(BH_JWrite, &bh->b_state);
729 J_ASSERT_BH(bh, buffer_jbddirty(bh));
731 /* The metadata is now released for reuse, but we need
732 to remember it against this transaction so that when
733 we finally commit, we can do any checkpointing
735 JBUFFER_TRACE(jh, "file as BJ_Forget");
736 jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget);
737 /* Wake up any transactions which were waiting for this
739 wake_up_bit(&bh->b_state, BH_Unshadow);
740 JBUFFER_TRACE(jh, "brelse shadowed buffer");
744 J_ASSERT (commit_transaction->t_shadow_list == NULL);
746 jbd_debug(3, "JBD: commit phase 4\n");
748 /* Here we wait for the revoke record and descriptor record buffers */
750 while (commit_transaction->t_log_list != NULL) {
751 struct buffer_head *bh;
753 jh = commit_transaction->t_log_list->b_tprev;
755 if (buffer_locked(bh)) {
757 goto wait_for_ctlbuf;
760 goto wait_for_ctlbuf;
762 if (unlikely(!buffer_uptodate(bh)))
765 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
766 clear_buffer_jwrite(bh);
767 jbd2_journal_unfile_buffer(journal, jh);
768 jbd2_journal_put_journal_head(jh);
769 __brelse(bh); /* One for getblk */
770 /* AKPM: bforget here */
773 jbd_debug(3, "JBD: commit phase 5\n");
775 if (!JBD2_HAS_INCOMPAT_FEATURE(journal,
776 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
777 err = journal_submit_commit_record(journal, commit_transaction,
780 __jbd2_journal_abort_hard(journal);
782 if (!err && !is_journal_aborted(journal))
783 err = journal_wait_on_commit_record(cbh);
786 jbd2_journal_abort(journal, err);
788 /* End of a transaction! Finally, we can do checkpoint
789 processing: any buffers committed as a result of this
790 transaction can be removed from any checkpoint list it was on
793 jbd_debug(3, "JBD: commit phase 6\n");
795 J_ASSERT(list_empty(&commit_transaction->t_inode_list));
796 J_ASSERT(commit_transaction->t_buffers == NULL);
797 J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
798 J_ASSERT(commit_transaction->t_iobuf_list == NULL);
799 J_ASSERT(commit_transaction->t_shadow_list == NULL);
800 J_ASSERT(commit_transaction->t_log_list == NULL);
804 * As there are other places (journal_unmap_buffer()) adding buffers
805 * to this list we have to be careful and hold the j_list_lock.
807 spin_lock(&journal->j_list_lock);
808 while (commit_transaction->t_forget) {
809 transaction_t *cp_transaction;
810 struct buffer_head *bh;
812 jh = commit_transaction->t_forget;
813 spin_unlock(&journal->j_list_lock);
815 jbd_lock_bh_state(bh);
816 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction ||
817 jh->b_transaction == journal->j_running_transaction);
820 * If there is undo-protected committed data against
821 * this buffer, then we can remove it now. If it is a
822 * buffer needing such protection, the old frozen_data
823 * field now points to a committed version of the
824 * buffer, so rotate that field to the new committed
827 * Otherwise, we can just throw away the frozen data now.
829 if (jh->b_committed_data) {
830 jbd2_free(jh->b_committed_data, bh->b_size);
831 jh->b_committed_data = NULL;
832 if (jh->b_frozen_data) {
833 jh->b_committed_data = jh->b_frozen_data;
834 jh->b_frozen_data = NULL;
836 } else if (jh->b_frozen_data) {
837 jbd2_free(jh->b_frozen_data, bh->b_size);
838 jh->b_frozen_data = NULL;
841 spin_lock(&journal->j_list_lock);
842 cp_transaction = jh->b_cp_transaction;
843 if (cp_transaction) {
844 JBUFFER_TRACE(jh, "remove from old cp transaction");
845 cp_transaction->t_chp_stats.cs_dropped++;
846 __jbd2_journal_remove_checkpoint(jh);
849 /* Only re-checkpoint the buffer_head if it is marked
850 * dirty. If the buffer was added to the BJ_Forget list
851 * by jbd2_journal_forget, it may no longer be dirty and
852 * there's no point in keeping a checkpoint record for
855 /* A buffer which has been freed while still being
856 * journaled by a previous transaction may end up still
857 * being dirty here, but we want to avoid writing back
858 * that buffer in the future now that the last use has
859 * been committed. That's not only a performance gain,
860 * it also stops aliasing problems if the buffer is left
861 * behind for writeback and gets reallocated for another
862 * use in a different page. */
863 if (buffer_freed(bh)) {
864 clear_buffer_freed(bh);
865 clear_buffer_jbddirty(bh);
868 if (buffer_jbddirty(bh)) {
869 JBUFFER_TRACE(jh, "add to new checkpointing trans");
870 __jbd2_journal_insert_checkpoint(jh, commit_transaction);
871 JBUFFER_TRACE(jh, "refile for checkpoint writeback");
872 __jbd2_journal_refile_buffer(jh);
873 jbd_unlock_bh_state(bh);
875 J_ASSERT_BH(bh, !buffer_dirty(bh));
876 /* The buffer on BJ_Forget list and not jbddirty means
877 * it has been freed by this transaction and hence it
878 * could not have been reallocated until this
879 * transaction has committed. *BUT* it could be
880 * reallocated once we have written all the data to
881 * disk and before we process the buffer on BJ_Forget
883 JBUFFER_TRACE(jh, "refile or unfile freed buffer");
884 __jbd2_journal_refile_buffer(jh);
885 if (!jh->b_transaction) {
886 jbd_unlock_bh_state(bh);
888 jbd2_journal_remove_journal_head(bh);
889 release_buffer_page(bh);
891 jbd_unlock_bh_state(bh);
893 cond_resched_lock(&journal->j_list_lock);
895 spin_unlock(&journal->j_list_lock);
897 * This is a bit sleazy. We use j_list_lock to protect transition
898 * of a transaction into T_FINISHED state and calling
899 * __jbd2_journal_drop_transaction(). Otherwise we could race with
900 * other checkpointing code processing the transaction...
902 spin_lock(&journal->j_state_lock);
903 spin_lock(&journal->j_list_lock);
905 * Now recheck if some buffers did not get attached to the transaction
906 * while the lock was dropped...
908 if (commit_transaction->t_forget) {
909 spin_unlock(&journal->j_list_lock);
910 spin_unlock(&journal->j_state_lock);
914 /* Done with this transaction! */
916 jbd_debug(3, "JBD: commit phase 7\n");
918 J_ASSERT(commit_transaction->t_state == T_COMMIT);
920 commit_transaction->t_start = jiffies;
921 stats.u.run.rs_logging = jbd2_time_diff(stats.u.run.rs_logging,
922 commit_transaction->t_start);
925 * File the transaction for history
927 stats.ts_type = JBD2_STATS_RUN;
928 stats.ts_tid = commit_transaction->t_tid;
929 stats.u.run.rs_handle_count = commit_transaction->t_handle_count;
930 spin_lock(&journal->j_history_lock);
931 memcpy(journal->j_history + journal->j_history_cur, &stats,
933 if (++journal->j_history_cur == journal->j_history_max)
934 journal->j_history_cur = 0;
937 * Calculate overall stats
939 journal->j_stats.ts_tid++;
940 journal->j_stats.u.run.rs_wait += stats.u.run.rs_wait;
941 journal->j_stats.u.run.rs_running += stats.u.run.rs_running;
942 journal->j_stats.u.run.rs_locked += stats.u.run.rs_locked;
943 journal->j_stats.u.run.rs_flushing += stats.u.run.rs_flushing;
944 journal->j_stats.u.run.rs_logging += stats.u.run.rs_logging;
945 journal->j_stats.u.run.rs_handle_count += stats.u.run.rs_handle_count;
946 journal->j_stats.u.run.rs_blocks += stats.u.run.rs_blocks;
947 journal->j_stats.u.run.rs_blocks_logged += stats.u.run.rs_blocks_logged;
948 spin_unlock(&journal->j_history_lock);
950 commit_transaction->t_state = T_FINISHED;
951 J_ASSERT(commit_transaction == journal->j_committing_transaction);
952 journal->j_commit_sequence = commit_transaction->t_tid;
953 journal->j_committing_transaction = NULL;
954 spin_unlock(&journal->j_state_lock);
956 if (commit_transaction->t_checkpoint_list == NULL &&
957 commit_transaction->t_checkpoint_io_list == NULL) {
958 __jbd2_journal_drop_transaction(journal, commit_transaction);
960 if (journal->j_checkpoint_transactions == NULL) {
961 journal->j_checkpoint_transactions = commit_transaction;
962 commit_transaction->t_cpnext = commit_transaction;
963 commit_transaction->t_cpprev = commit_transaction;
965 commit_transaction->t_cpnext =
966 journal->j_checkpoint_transactions;
967 commit_transaction->t_cpprev =
968 commit_transaction->t_cpnext->t_cpprev;
969 commit_transaction->t_cpnext->t_cpprev =
971 commit_transaction->t_cpprev->t_cpnext =
975 spin_unlock(&journal->j_list_lock);
977 jbd_debug(1, "JBD: commit %d complete, head %d\n",
978 journal->j_commit_sequence, journal->j_tail_sequence);
980 wake_up(&journal->j_wait_done_commit);