VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   57 Tags
f51667685749e (Theodore Ts'o              2017-12-17 22:00:59 -0500    1) // SPDX-License-Identifier: GPL-2.0+
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    2) /*
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700    3)  * linux/fs/jbd2/commit.c
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    4)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    5)  * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    6)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    7)  * Copyright 1998 Red Hat corp --- All Rights Reserved
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    8)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700    9)  * Journal commit routines for the generic filesystem journaling code;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   10)  * part of the ext2fs journaling system.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   11)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   12) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   13) #include <linux/time.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   14) #include <linux/fs.h>
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700   15) #include <linux/jbd2.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   16) #include <linux/errno.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   17) #include <linux/slab.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   18) #include <linux/mm.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   19) #include <linux/pagemap.h>
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500   20) #include <linux/jiffies.h>
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500   21) #include <linux/crc32.h>
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400   22) #include <linux/writeback.h>
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400   23) #include <linux/backing-dev.h>
fd98496f467b3 (Theodore Ts'o              2009-01-05 21:34:13 -0500   24) #include <linux/bio.h>
0e3d2a6313d03 (Theodore Ts'o              2009-09-11 09:30:12 -0400   25) #include <linux/blkdev.h>
39e3ac2599a5f (Brian King                 2010-10-27 21:25:12 -0400   26) #include <linux/bitops.h>
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400   27) #include <trace/events/jbd2.h>
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   28) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   29) /*
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   30)  * IO end handler for temporary buffer_heads handling writes to the journal.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   31)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   32) static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   33) {
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   34) 	struct buffer_head *orig_bh = bh->b_private;
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   35) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   36) 	BUFFER_TRACE(bh, "");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   37) 	if (uptodate)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   38) 		set_buffer_uptodate(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   39) 	else
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   40) 		clear_buffer_uptodate(bh);
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   41) 	if (orig_bh) {
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   42) 		clear_bit_unlock(BH_Shadow, &orig_bh->b_state);
4e857c58efeb9 (Peter Zijlstra             2014-03-17 18:06:10 +0100   43) 		smp_mb__after_atomic();
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   44) 		wake_up_bit(&orig_bh->b_state, BH_Shadow);
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400   45) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   46) 	unlock_buffer(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   47) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   48) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   49) /*
87c89c232c8f7 (Jan Kara                   2008-07-11 19:27:31 -0400   50)  * When an ext4 file is truncated, it is possible that some pages are not
87c89c232c8f7 (Jan Kara                   2008-07-11 19:27:31 -0400   51)  * successfully freed, because they are attached to a committing transaction.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   52)  * After the transaction commits, these pages are left on the LRU, with no
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   53)  * ->mapping, and with attached buffers.  These pages are trivially reclaimable
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   54)  * by the VM, but their apparent absence upsets the VM accounting, and it makes
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   55)  * the numbers in /proc/meminfo look odd.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   56)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   57)  * So here, we have a buffer which has just come off the forget list.  Look to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   58)  * see if we can strip all buffers from the backing page.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   59)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   60)  * Called under lock_journal(), and possibly under journal_datalist_lock.  The
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   61)  * caller provided us with a ref against the buffer, and we drop that here.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   62)  */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   63) static void release_buffer_page(struct buffer_head *bh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   64) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   65) 	struct page *page;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   66) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   67) 	if (buffer_dirty(bh))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   68) 		goto nope;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   69) 	if (atomic_read(&bh->b_count) != 1)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   70) 		goto nope;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   71) 	page = bh->b_page;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   72) 	if (!page)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   73) 		goto nope;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   74) 	if (page->mapping)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   75) 		goto nope;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   76) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   77) 	/* OK, it's a truncated page */
529ae9aaa0837 (Nicholas Piggin            2008-08-02 12:01:03 +0200   78) 	if (!trylock_page(page))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   79) 		goto nope;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   80) 
09cbfeaf1a5a6 (Kirill A. Shutemov         2016-04-01 15:29:47 +0300   81) 	get_page(page);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   82) 	__brelse(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   83) 	try_to_free_buffers(page);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   84) 	unlock_page(page);
09cbfeaf1a5a6 (Kirill A. Shutemov         2016-04-01 15:29:47 +0300   85) 	put_page(page);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   86) 	return;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   87) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   88) nope:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   89) 	__brelse(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   90) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700   91) 
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400   92) static void jbd2_commit_block_csum_set(journal_t *j, struct buffer_head *bh)
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400   93) {
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400   94) 	struct commit_header *h;
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400   95) 	__u32 csum;
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400   96) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400   97) 	if (!jbd2_journal_has_csum_v2or3(j))
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400   98) 		return;
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400   99) 
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  100) 	h = (struct commit_header *)(bh->b_data);
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400  101) 	h->h_chksum_type = 0;
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400  102) 	h->h_chksum_size = 0;
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400  103) 	h->h_chksum[0] = 0;
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  104) 	csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400  105) 	h->h_chksum[0] = cpu_to_be32(csum);
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400  106) }
1f56c5890e3e8 (Darrick J. Wong            2012-05-27 08:10:25 -0400  107) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  108) /*
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  109)  * Done it all: now submit the commit record.  We should have
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  110)  * cleaned up our previous buffers by now, so if we are in abort
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  111)  * mode we can now just skip the rest of the journal write
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  112)  * entirely.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  113)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  114)  * Returns 1 if the journal needs to be aborted or 0 on success
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  115)  */
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  116) static int journal_submit_commit_record(journal_t *journal,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  117) 					transaction_t *commit_transaction,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  118) 					struct buffer_head **cbh,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  119) 					__u32 crc32_sum)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  120) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  121) 	struct commit_header *tmp;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  122) 	struct buffer_head *bh;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  123) 	int ret;
b42d1d6b5b789 (Arnd Bergmann              2018-07-29 15:51:47 -0400  124) 	struct timespec64 now;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  125) 
6cba611e600de (Zhang Huan                 2011-04-05 19:16:20 -0400  126) 	*cbh = NULL;
6cba611e600de (Zhang Huan                 2011-04-05 19:16:20 -0400  127) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  128) 	if (is_journal_aborted(journal))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  129) 		return 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  130) 
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500  131) 	bh = jbd2_journal_get_descriptor_buffer(commit_transaction,
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500  132) 						JBD2_COMMIT_BLOCK);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  133) 	if (!bh)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  134) 		return 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  135) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  136) 	tmp = (struct commit_header *)bh->b_data;
b42d1d6b5b789 (Arnd Bergmann              2018-07-29 15:51:47 -0400  137) 	ktime_get_coarse_real_ts64(&now);
736603ab29750 (Theodore Ts'o              2008-07-11 19:27:31 -0400  138) 	tmp->h_commit_sec = cpu_to_be64(now.tv_sec);
736603ab29750 (Theodore Ts'o              2008-07-11 19:27:31 -0400  139) 	tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  140) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  141) 	if (jbd2_has_feature_checksum(journal)) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  142) 		tmp->h_chksum_type 	= JBD2_CRC32_CHKSUM;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  143) 		tmp->h_chksum_size 	= JBD2_CRC32_CHKSUM_SIZE;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  144) 		tmp->h_chksum[0] 	= cpu_to_be32(crc32_sum);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  145) 	}
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  146) 	jbd2_commit_block_csum_set(journal, bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  147) 
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  148) 	BUFFER_TRACE(bh, "submit commit block");
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  149) 	lock_buffer(bh);
45a90bfd90c12 (Theodore Ts'o              2008-10-06 12:04:02 -0400  150) 	clear_buffer_dirty(bh);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  151) 	set_buffer_uptodate(bh);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  152) 	bh->b_end_io = journal_end_buffer_io_sync;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  153) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  154) 	if (journal->j_flags & JBD2_BARRIER &&
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  155) 	    !jbd2_has_feature_async_commit(journal))
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600  156) 		ret = submit_bh(REQ_OP_WRITE,
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600  157) 			REQ_SYNC | REQ_PREFLUSH | REQ_FUA, bh);
9c35575bbe6b1 (Christoph Hellwig          2010-08-18 05:29:17 -0400  158) 	else
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600  159) 		ret = submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
9c35575bbe6b1 (Christoph Hellwig          2010-08-18 05:29:17 -0400  160) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  161) 	*cbh = bh;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  162) 	return ret;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  163) }
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  164) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  165) /*
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  166)  * This function along with journal_submit_commit_record
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  167)  * allows to write the commit record asynchronously.
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  168)  */
fd98496f467b3 (Theodore Ts'o              2009-01-05 21:34:13 -0500  169) static int journal_wait_on_commit_record(journal_t *journal,
fd98496f467b3 (Theodore Ts'o              2009-01-05 21:34:13 -0500  170) 					 struct buffer_head *bh)
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  171) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  172) 	int ret = 0;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  173) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  174) 	clear_buffer_dirty(bh);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  175) 	wait_on_buffer(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  176) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  177) 	if (unlikely(!buffer_uptodate(bh)))
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  178) 		ret = -EIO;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  179) 	put_bh(bh);            /* One for getblk() */
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  180) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  181) 	return ret;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  182) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  183) 
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  184) /*
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  185)  * write the filemap data using writepage() address_space_operations.
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  186)  * We don't do block allocation here even for delalloc. We don't
a49773064bc22 (Liu Song                   2019-05-30 15:15:57 -0400  187)  * use writepages() because with delayed allocation we may be doing
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  188)  * block allocation in writepages().
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  189)  */
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  190) int jbd2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  191) {
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  192) 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  193) 	struct writeback_control wbc = {
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  194) 		.sync_mode =  WB_SYNC_ALL,
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  195) 		.nr_to_write = mapping->nrpages * 2,
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  196) 		.range_start = jinode->i_dirty_start,
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  197) 		.range_end = jinode->i_dirty_end,
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  198) 	};
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  199) 
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  200) 	/*
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  201) 	 * submit the inode data buffers. We use writepage
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  202) 	 * instead of writepages. Because writepages can do
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  203) 	 * block allocation with delalloc. We need to write
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  204) 	 * only allocated blocks here.
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  205) 	 */
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  206) 	return generic_writepages(mapping, &wbc);
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  207) }
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  208) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  209) /* Send all the data buffers related to an inode */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  210) int jbd2_submit_inode_data(struct jbd2_inode *jinode)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  211) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  212) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  213) 	if (!jinode || !(jinode->i_flags & JI_WRITE_DATA))
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  214) 		return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  215) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  216) 	trace_jbd2_submit_inode_data(jinode->i_vfs_inode);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  217) 	return jbd2_journal_submit_inode_data_buffers(jinode);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  218) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  219) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  220) EXPORT_SYMBOL(jbd2_submit_inode_data);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  221) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  222) int jbd2_wait_inode_data(journal_t *journal, struct jbd2_inode *jinode)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  223) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  224) 	if (!jinode || !(jinode->i_flags & JI_WAIT_DATA) ||
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  225) 		!jinode->i_vfs_inode || !jinode->i_vfs_inode->i_mapping)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  226) 		return 0;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  227) 	return filemap_fdatawait_range_keep_errors(
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  228) 		jinode->i_vfs_inode->i_mapping, jinode->i_dirty_start,
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  229) 		jinode->i_dirty_end);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  230) }
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  231) EXPORT_SYMBOL(jbd2_wait_inode_data);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  232) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  233) /*
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  234)  * Submit all the data buffers of inode associated with the transaction to
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  235)  * disk.
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  236)  *
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  237)  * We are in a committing transaction. Therefore no new inode can be added to
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  238)  * our inode list. We use JI_COMMIT_RUNNING flag to protect inode we currently
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  239)  * operate on from being released while we write out pages.
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  240)  */
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  241) static int journal_submit_data_buffers(journal_t *journal,
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  242) 		transaction_t *commit_transaction)
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  243) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  244) 	struct jbd2_inode *jinode;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  245) 	int err, ret = 0;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  246) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  247) 	spin_lock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  248) 	list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
41617e1a8dec9 (Jan Kara                   2016-04-24 00:56:07 -0400  249) 		if (!(jinode->i_flags & JI_WRITE_DATA))
41617e1a8dec9 (Jan Kara                   2016-04-24 00:56:07 -0400  250) 			continue;
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500  251) 		jinode->i_flags |= JI_COMMIT_RUNNING;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  252) 		spin_unlock(&journal->j_list_lock);
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  253) 		/* submit the inode data buffers. */
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400  254) 		trace_jbd2_submit_inode_data(jinode->i_vfs_inode);
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  255) 		if (journal->j_submit_inode_data_buffers) {
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  256) 			err = journal->j_submit_inode_data_buffers(jinode);
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  257) 			if (!ret)
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  258) 				ret = err;
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  259) 		}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  260) 		spin_lock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  261) 		J_ASSERT(jinode->i_transaction == commit_transaction);
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500  262) 		jinode->i_flags &= ~JI_COMMIT_RUNNING;
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500  263) 		smp_mb();
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  264) 		wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  265) 	}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  266) 	spin_unlock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  267) 	return ret;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  268) }
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  269) 
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  270) int jbd2_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  271) {
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  272) 	struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  273) 
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  274) 	return filemap_fdatawait_range_keep_errors(mapping,
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  275) 						   jinode->i_dirty_start,
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  276) 						   jinode->i_dirty_end);
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  277) }
aa3c0c61f62d6 (Mauricio Faria de Oliveira 2020-10-05 21:48:38 -0300  278) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  279) /*
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  280)  * Wait for data submitted for writeout, refile inodes to proper
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  281)  * transaction if needed.
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  282)  *
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  283)  */
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  284) static int journal_finish_inode_data_buffers(journal_t *journal,
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  285) 		transaction_t *commit_transaction)
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  286) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  287) 	struct jbd2_inode *jinode, *next_i;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  288) 	int err, ret = 0;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  289) 
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  290) 	/* For locking, see the comment in journal_submit_data_buffers() */
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  291) 	spin_lock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  292) 	list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
41617e1a8dec9 (Jan Kara                   2016-04-24 00:56:07 -0400  293) 		if (!(jinode->i_flags & JI_WAIT_DATA))
41617e1a8dec9 (Jan Kara                   2016-04-24 00:56:07 -0400  294) 			continue;
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500  295) 		jinode->i_flags |= JI_COMMIT_RUNNING;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  296) 		spin_unlock(&journal->j_list_lock);
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  297) 		/* wait for the inode data buffers writeout. */
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  298) 		if (journal->j_finish_inode_data_buffers) {
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  299) 			err = journal->j_finish_inode_data_buffers(jinode);
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  300) 			if (!ret)
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  301) 				ret = err;
342af94ec6c02 (Mauricio Faria de Oliveira 2020-10-05 21:48:39 -0300  302) 		}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  303) 		spin_lock(&journal->j_list_lock);
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500  304) 		jinode->i_flags &= ~JI_COMMIT_RUNNING;
cb0d9d47a39de (Jan Kara                   2016-02-22 23:20:30 -0500  305) 		smp_mb();
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  306) 		wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  307) 	}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  308) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  309) 	/* Now refile inode to proper lists */
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  310) 	list_for_each_entry_safe(jinode, next_i,
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  311) 				 &commit_transaction->t_inode_list, i_list) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  312) 		list_del(&jinode->i_list);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  313) 		if (jinode->i_next_transaction) {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  314) 			jinode->i_transaction = jinode->i_next_transaction;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  315) 			jinode->i_next_transaction = NULL;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  316) 			list_add(&jinode->i_list,
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  317) 				&jinode->i_transaction->t_inode_list);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  318) 		} else {
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  319) 			jinode->i_transaction = NULL;
6ba0e7dc64a5a (Ross Zwisler               2019-06-20 17:24:56 -0400  320) 			jinode->i_dirty_start = 0;
6ba0e7dc64a5a (Ross Zwisler               2019-06-20 17:24:56 -0400  321) 			jinode->i_dirty_end = 0;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  322) 		}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  323) 	}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  324) 	spin_unlock(&journal->j_list_lock);
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  325) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  326) 	return ret;
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  327) }
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  328) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  329) static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  330) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  331) 	struct page *page = bh->b_page;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  332) 	char *addr;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  333) 	__u32 checksum;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  334) 
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  335) 	addr = kmap_atomic(page);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  336) 	checksum = crc32_be(crc32_sum,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  337) 		(void *)(addr + offset_in_page(bh->b_data)), bh->b_size);
303a8f2afc7ba (Cong Wang                  2011-11-25 23:14:31 +0800  338) 	kunmap_atomic(addr);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  339) 
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  340) 	return checksum;
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  341) }
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  342) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  343) static void write_tag_block(journal_t *j, journal_block_tag_t *tag,
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700  344) 				   unsigned long long block)
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  345) {
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  346) 	tag->t_blocknr = cpu_to_be32(block & (u32)~0);
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  347) 	if (jbd2_has_feature_64bit(j))
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  348) 		tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  349) }
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  350) 
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  351) static void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  352) 				    struct buffer_head *bh, __u32 sequence)
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  353) {
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  354) 	journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  355) 	struct page *page = bh->b_page;
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  356) 	__u8 *addr;
eee06c5678449 (Darrick J. Wong            2013-05-28 07:31:59 -0400  357) 	__u32 csum32;
18a6ea1e5cc88 (Darrick J. Wong            2013-08-28 14:59:58 -0400  358) 	__be32 seq;
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  359) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  360) 	if (!jbd2_journal_has_csum_v2or3(j))
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  361) 		return;
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  362) 
18a6ea1e5cc88 (Darrick J. Wong            2013-08-28 14:59:58 -0400  363) 	seq = cpu_to_be32(sequence);
906adea1536fe (Cong Wang                  2012-06-23 11:24:48 +0800  364) 	addr = kmap_atomic(page);
18a6ea1e5cc88 (Darrick J. Wong            2013-08-28 14:59:58 -0400  365) 	csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
eee06c5678449 (Darrick J. Wong            2013-05-28 07:31:59 -0400  366) 	csum32 = jbd2_chksum(j, csum32, addr + offset_in_page(bh->b_data),
eee06c5678449 (Darrick J. Wong            2013-05-28 07:31:59 -0400  367) 			     bh->b_size);
906adea1536fe (Cong Wang                  2012-06-23 11:24:48 +0800  368) 	kunmap_atomic(addr);
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  369) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  370) 	if (jbd2_has_feature_csum3(j))
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  371) 		tag3->t_checksum = cpu_to_be32(csum32);
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  372) 	else
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  373) 		tag->t_checksum = cpu_to_be16(csum32);
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  374) }
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  375) /*
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  376)  * jbd2_journal_commit_transaction
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  377)  *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  378)  * The primary function for committing a transaction to the log.  This
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  379)  * function is called by the journal thread to begin a complete commit.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  380)  */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  381) void jbd2_journal_commit_transaction(journal_t *journal)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  382) {
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500  383) 	struct transaction_stats_s stats;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  384) 	transaction_t *commit_transaction;
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  385) 	struct journal_head *jh;
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  386) 	struct buffer_head *descriptor;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  387) 	struct buffer_head **wbuf = journal->j_wbuf;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  388) 	int bufs;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  389) 	int flags;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  390) 	int err;
18eba7aae080d (Mingming Cao               2006-10-11 01:21:13 -0700  391) 	unsigned long long blocknr;
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500  392) 	ktime_t start_time;
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500  393) 	u64 commit_time;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  394) 	char *tagp = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  395) 	journal_block_tag_t *tag = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  396) 	int space_left = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  397) 	int first_tag = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  398) 	int tag_flag;
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400  399) 	int i;
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  400) 	int tag_bytes = journal_tag_bytes(journal);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  401) 	struct buffer_head *cbh = NULL; /* For transactional checksums */
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  402) 	__u32 crc32_sum = ~0;
82f04ab47e1d9 (Jens Axboe                 2011-03-17 11:01:52 +0100  403) 	struct blk_plug plug;
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  404) 	/* Tail of the journal */
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  405) 	unsigned long first_block;
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  406) 	tid_t first_tid;
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  407) 	int update_tail;
3caa487f53f65 (Darrick J. Wong            2012-05-27 08:10:22 -0400  408) 	int csum_size = 0;
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  409) 	LIST_HEAD(io_bufs);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  410) 	LIST_HEAD(log_bufs);
3caa487f53f65 (Darrick J. Wong            2012-05-27 08:10:22 -0400  411) 
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  412) 	if (jbd2_journal_has_csum_v2or3(journal))
3caa487f53f65 (Darrick J. Wong            2012-05-27 08:10:22 -0400  413) 		csum_size = sizeof(struct jbd2_journal_block_tail);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  414) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  415) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  416) 	 * First job: lock down the current transaction and wait for
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  417) 	 * all outstanding updates to complete.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  418) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  419) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  420) 	/* Do we need to erase the effects of a prior jbd2_journal_flush? */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  421) 	if (journal->j_flags & JBD2_FLUSHED) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  422) 		jbd_debug(3, "super block updated\n");
6fa7aa50b2c48 (Tejun Heo                  2016-10-28 12:58:12 -0400  423) 		mutex_lock_io(&journal->j_checkpoint_mutex);
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  424) 		/*
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  425) 		 * We hold j_checkpoint_mutex so tail cannot change under us.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  426) 		 * We don't need any special data guarantees for writing sb
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  427) 		 * since journal is empty and it is ok for write to be
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  428) 		 * flushed only with transaction commit.
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  429) 		 */
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  430) 		jbd2_journal_update_sb_log_tail(journal,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  431) 						journal->j_tail_sequence,
79feb521a4470 (Jan Kara                   2012-03-13 22:22:54 -0400  432) 						journal->j_tail,
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600  433) 						REQ_SYNC);
a78bb11d7acd5 (Jan Kara                   2012-03-13 15:43:04 -0400  434) 		mutex_unlock(&journal->j_checkpoint_mutex);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  435) 	} else {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  436) 		jbd_debug(3, "superblock not updated\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  437) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  438) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  439) 	J_ASSERT(journal->j_running_transaction != NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  440) 	J_ASSERT(journal->j_committing_transaction == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  441) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  442) 	write_lock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  443) 	journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  444) 	while (journal->j_flags & JBD2_FAST_COMMIT_ONGOING) {
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  445) 		DEFINE_WAIT(wait);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  446) 
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  447) 		prepare_to_wait(&journal->j_fc_wait, &wait,
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  448) 				TASK_UNINTERRUPTIBLE);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  449) 		write_unlock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  450) 		schedule();
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  451) 		write_lock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  452) 		finish_wait(&journal->j_fc_wait, &wait);
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  453) 		/*
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  454) 		 * TODO: by blocking fast commits here, we are increasing
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  455) 		 * fsync() latency slightly. Strictly speaking, we don't need
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  456) 		 * to block fast commits until the transaction enters T_FLUSH
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  457) 		 * state. So an optimization is possible where we block new fast
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  458) 		 * commits here and wait for existing ones to complete
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  459) 		 * just before we enter T_FLUSH. That way, the existing fast
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  460) 		 * commits and this full commit can proceed parallely.
cc80586a57f70 (Harshad Shirwadkar         2020-11-05 19:58:59 -0800  461) 		 */
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  462) 	}
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  463) 	write_unlock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  464) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  465) 	commit_transaction = journal->j_running_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  466) 
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400  467) 	trace_jbd2_start_commit(journal, commit_transaction);
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  468) 	jbd_debug(1, "JBD2: starting commit of transaction %d\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  469) 			commit_transaction->t_tid);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  470) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  471) 	write_lock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700  472) 	journal->j_fc_off = 0;
3ca841c106fd6 (Paul Gortmaker             2013-06-12 22:46:35 -0400  473) 	J_ASSERT(commit_transaction->t_state == T_RUNNING);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  474) 	commit_transaction->t_state = T_LOCKED;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  475) 
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400  476) 	trace_jbd2_commit_locking(journal, commit_transaction);
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  477) 	stats.run.rs_wait = commit_transaction->t_max_wait;
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500  478) 	stats.run.rs_request_delay = 0;
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  479) 	stats.run.rs_locked = jiffies;
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500  480) 	if (commit_transaction->t_requested)
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500  481) 		stats.run.rs_request_delay =
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500  482) 			jbd2_time_diff(commit_transaction->t_requested,
9fff24aa2c5c5 (Theodore Ts'o              2013-02-06 22:30:23 -0500  483) 				       stats.run.rs_locked);
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  484) 	stats.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  485) 					      stats.run.rs_locked);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500  486) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  487) 	spin_lock(&commit_transaction->t_handle_lock);
a51dca9cd3bb4 (Theodore Ts'o              2010-08-02 08:43:25 -0400  488) 	while (atomic_read(&commit_transaction->t_updates)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  489) 		DEFINE_WAIT(wait);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  490) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  491) 		prepare_to_wait(&journal->j_wait_updates, &wait,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  492) 					TASK_UNINTERRUPTIBLE);
a51dca9cd3bb4 (Theodore Ts'o              2010-08-02 08:43:25 -0400  493) 		if (atomic_read(&commit_transaction->t_updates)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  494) 			spin_unlock(&commit_transaction->t_handle_lock);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  495) 			write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  496) 			schedule();
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  497) 			write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  498) 			spin_lock(&commit_transaction->t_handle_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  499) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  500) 		finish_wait(&journal->j_wait_updates, &wait);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  501) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  502) 	spin_unlock(&commit_transaction->t_handle_lock);
96f1e09745750 (Jan Kara                   2018-12-03 23:16:07 -0500  503) 	commit_transaction->t_state = T_SWITCH;
96f1e09745750 (Jan Kara                   2018-12-03 23:16:07 -0500  504) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  505) 
a51dca9cd3bb4 (Theodore Ts'o              2010-08-02 08:43:25 -0400  506) 	J_ASSERT (atomic_read(&commit_transaction->t_outstanding_credits) <=
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  507) 			journal->j_max_transaction_buffers);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  508) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  509) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  510) 	 * First thing we are allowed to do is to discard any remaining
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  511) 	 * BJ_Reserved buffers.  Note, it is _not_ permissible to assume
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  512) 	 * that there are no such buffers: if a large filesystem
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  513) 	 * operation like a truncate needs to split itself over multiple
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  514) 	 * transactions, then it may try to do a jbd2_journal_restart() while
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  515) 	 * there are still BJ_Reserved buffers outstanding.  These must
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  516) 	 * be released cleanly from the current transaction.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  517) 	 *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  518) 	 * In this case, the filesystem must still reserve write access
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  519) 	 * again before modifying the buffer in the new transaction, but
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  520) 	 * we do not require it to remember exactly which old buffers it
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  521) 	 * has reserved.  This is consistent with the existing behaviour
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  522) 	 * that multiple jbd2_journal_get_write_access() calls to the same
25985edcedea6 (Lucas De Marchi            2011-03-30 22:57:33 -0300  523) 	 * buffer are perfectly permissible.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  524) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  525) 	while (commit_transaction->t_reserved_list) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  526) 		jh = commit_transaction->t_reserved_list;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  527) 		JBUFFER_TRACE(jh, "reserved, unused: refile");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  528) 		/*
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  529) 		 * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  530) 		 * leave undo-committed data.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  531) 		 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  532) 		if (jh->b_committed_data) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  533) 			struct buffer_head *bh = jh2bh(jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  534) 
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  535) 			spin_lock(&jh->b_state_lock);
af1e76d6b3f37 (Mingming Cao               2007-10-16 18:38:25 -0400  536) 			jbd2_free(jh->b_committed_data, bh->b_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  537) 			jh->b_committed_data = NULL;
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  538) 			spin_unlock(&jh->b_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  539) 		}
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  540) 		jbd2_journal_refile_buffer(journal, jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  541) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  542) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  543) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  544) 	 * Now try to drop any written-back buffers from the journal's
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  545) 	 * checkpoint lists.  We do this *before* commit because it potentially
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  546) 	 * frees some memory
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  547) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  548) 	spin_lock(&journal->j_list_lock);
841df7df19623 (Jan Kara                   2015-07-28 14:57:14 -0400  549) 	__jbd2_journal_clean_checkpoint_list(journal, false);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  550) 	spin_unlock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  551) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  552) 	jbd_debug(3, "JBD2: commit phase 1\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  553) 
1ba37268cd19e (Yongqiang Yang             2011-12-28 17:46:46 -0500  554) 	/*
1ba37268cd19e (Yongqiang Yang             2011-12-28 17:46:46 -0500  555) 	 * Clear revoked flag to reflect there is no revoked buffers
1ba37268cd19e (Yongqiang Yang             2011-12-28 17:46:46 -0500  556) 	 * in the next transaction which is going to be started.
1ba37268cd19e (Yongqiang Yang             2011-12-28 17:46:46 -0500  557) 	 */
1ba37268cd19e (Yongqiang Yang             2011-12-28 17:46:46 -0500  558) 	jbd2_clear_buffer_revoked_flags(journal);
1ba37268cd19e (Yongqiang Yang             2011-12-28 17:46:46 -0500  559) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  560) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  561) 	 * Switch to a new revoke table.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  562) 	 */
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  563) 	jbd2_journal_switch_revoke_table(journal);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  564) 
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400  565) 	/*
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400  566) 	 * Reserved credits cannot be claimed anymore, free them
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400  567) 	 */
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400  568) 	atomic_sub(atomic_read(&journal->j_reserved_credits),
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400  569) 		   &commit_transaction->t_outstanding_credits);
8f7d89f36829b (Jan Kara                   2013-06-04 12:35:11 -0400  570) 
96f1e09745750 (Jan Kara                   2018-12-03 23:16:07 -0500  571) 	write_lock(&journal->j_state_lock);
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400  572) 	trace_jbd2_commit_flushing(journal, commit_transaction);
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  573) 	stats.run.rs_flushing = jiffies;
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  574) 	stats.run.rs_locked = jbd2_time_diff(stats.run.rs_locked,
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  575) 					     stats.run.rs_flushing);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500  576) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  577) 	commit_transaction->t_state = T_FLUSH;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  578) 	journal->j_committing_transaction = commit_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  579) 	journal->j_running_transaction = NULL;
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500  580) 	start_time = ktime_get();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  581) 	commit_transaction->t_log_start = journal->j_head;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  582) 	wake_up(&journal->j_wait_transaction_locked);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  583) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  584) 
cfc7bc896f452 (Paul Gortmaker             2013-06-12 22:56:35 -0400  585) 	jbd_debug(3, "JBD2: commit phase 2a\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  586) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  587) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  588) 	 * Now start flushing things to disk, in the order they appear
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  589) 	 * on the transaction lists.  Data blocks go first.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  590) 	 */
cd1aac32923a9 (Aneesh Kumar K.V           2008-07-11 19:27:31 -0400  591) 	err = journal_submit_data_buffers(journal, commit_transaction);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  592) 	if (err)
a7fa2baf8e2a6 (Jan Kara                   2007-10-16 18:38:25 -0400  593) 		jbd2_journal_abort(journal, err);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  594) 
82f04ab47e1d9 (Jens Axboe                 2011-03-17 11:01:52 +0100  595) 	blk_start_plug(&plug);
9bcf976cb8b86 (Jan Kara                   2016-02-22 23:07:30 -0500  596) 	jbd2_journal_write_revoke_records(commit_transaction, &log_bufs);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  597) 
cfc7bc896f452 (Paul Gortmaker             2013-06-12 22:56:35 -0400  598) 	jbd_debug(3, "JBD2: commit phase 2b\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  599) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  600) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  601) 	 * Way to go: we have now written out all of the data for a
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  602) 	 * transaction!  Now comes the tricky part: we need to write out
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  603) 	 * metadata.  Loop over the transaction's entire buffer list:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  604) 	 */
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  605) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  606) 	commit_transaction->t_state = T_COMMIT;
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400  607) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  608) 
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400  609) 	trace_jbd2_commit_logging(journal, commit_transaction);
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  610) 	stats.run.rs_logging = jiffies;
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  611) 	stats.run.rs_flushing = jbd2_time_diff(stats.run.rs_flushing,
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  612) 					       stats.run.rs_logging);
9f356e5a4f120 (Jan Kara                   2019-11-05 17:44:24 +0100  613) 	stats.run.rs_blocks = commit_transaction->t_nr_buffers;
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400  614) 	stats.run.rs_blocks_logged = 0;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500  615) 
1dfc3220d9633 (Josef Bacik                2008-04-17 10:38:59 -0400  616) 	J_ASSERT(commit_transaction->t_nr_buffers <=
a51dca9cd3bb4 (Theodore Ts'o              2010-08-02 08:43:25 -0400  617) 		 atomic_read(&commit_transaction->t_outstanding_credits));
1dfc3220d9633 (Josef Bacik                2008-04-17 10:38:59 -0400  618) 
87c89c232c8f7 (Jan Kara                   2008-07-11 19:27:31 -0400  619) 	err = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  620) 	bufs = 0;
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  621) 	descriptor = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  622) 	while (commit_transaction->t_buffers) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  623) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  624) 		/* Find the next buffer to be journaled... */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  625) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  626) 		jh = commit_transaction->t_buffers;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  627) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  628) 		/* If we're in abort mode, we just un-journal the buffer and
7ad7445f60fe4 (Hidehiro Kawai             2008-10-10 20:29:31 -0400  629) 		   release it. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  630) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  631) 		if (is_journal_aborted(journal)) {
7ad7445f60fe4 (Hidehiro Kawai             2008-10-10 20:29:31 -0400  632) 			clear_buffer_jbddirty(jh2bh(jh));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  633) 			JBUFFER_TRACE(jh, "journal is aborting: refile");
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  634) 			jbd2_buffer_abort_trigger(jh,
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  635) 						  jh->b_frozen_data ?
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  636) 						  jh->b_frozen_triggers :
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  637) 						  jh->b_triggers);
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  638) 			jbd2_journal_refile_buffer(journal, jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  639) 			/* If that was the last one, we need to clean up
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  640) 			 * any descriptor buffers which may have been
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  641) 			 * already allocated, even if we are now
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  642) 			 * aborting. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  643) 			if (!commit_transaction->t_buffers)
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  644) 				goto start_journal_io;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  645) 			continue;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  646) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  647) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  648) 		/* Make sure we have a descriptor block in which to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  649) 		   record the metadata buffer. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  650) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  651) 		if (!descriptor) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  652) 			J_ASSERT (bufs == 0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  653) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  654) 			jbd_debug(4, "JBD2: get descriptor\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  655) 
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500  656) 			descriptor = jbd2_journal_get_descriptor_buffer(
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500  657) 							commit_transaction,
32ab671599a89 (Jan Kara                   2016-02-22 23:17:15 -0500  658) 							JBD2_DESCRIPTOR_BLOCK);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  659) 			if (!descriptor) {
a7fa2baf8e2a6 (Jan Kara                   2007-10-16 18:38:25 -0400  660) 				jbd2_journal_abort(journal, -EIO);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  661) 				continue;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  662) 			}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  663) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  664) 			jbd_debug(4, "JBD2: got buffer %llu (%p)\n",
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  665) 				(unsigned long long)descriptor->b_blocknr,
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  666) 				descriptor->b_data);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  667) 			tagp = &descriptor->b_data[sizeof(journal_header_t)];
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  668) 			space_left = descriptor->b_size -
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  669) 						sizeof(journal_header_t);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  670) 			first_tag = 1;
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  671) 			set_buffer_jwrite(descriptor);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  672) 			set_buffer_dirty(descriptor);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  673) 			wbuf[bufs++] = descriptor;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  674) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  675) 			/* Record it so that we can wait for IO
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  676)                            completion later */
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  677) 			BUFFER_TRACE(descriptor, "ph3: file as descriptor");
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  678) 			jbd2_file_log_bh(&log_bufs, descriptor);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  679) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  680) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  681) 		/* Where is the buffer to be written? */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  682) 
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  683) 		err = jbd2_journal_next_log_block(journal, &blocknr);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  684) 		/* If the block mapping failed, just abandon the buffer
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  685) 		   and repeat this loop: we'll fall into the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  686) 		   refile-on-abort condition above. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  687) 		if (err) {
a7fa2baf8e2a6 (Jan Kara                   2007-10-16 18:38:25 -0400  688) 			jbd2_journal_abort(journal, err);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  689) 			continue;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  690) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  691) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  692) 		/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  693) 		 * start_this_handle() uses t_outstanding_credits to determine
0db4588945364 (Jan Kara                   2019-11-05 17:44:08 +0100  694) 		 * the free space in the log.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  695) 		 */
a51dca9cd3bb4 (Theodore Ts'o              2010-08-02 08:43:25 -0400  696) 		atomic_dec(&commit_transaction->t_outstanding_credits);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  697) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  698) 		/* Bump b_count to prevent truncate from stumbling over
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  699)                    the shadowed buffer!  @@@ This can go if we ever get
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  700)                    rid of the shadow pairing of buffers. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  701) 		atomic_inc(&jh2bh(jh)->b_count);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  702) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  703) 		/*
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  704) 		 * Make a temporary IO buffer with which to write it out
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  705) 		 * (this will requeue the metadata buffer to BJ_Shadow).
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  706) 		 */
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  707) 		set_bit(BH_JWrite, &jh2bh(jh)->b_state);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  708) 		JBUFFER_TRACE(jh, "ph3: write metadata");
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  709) 		flags = jbd2_journal_write_metadata_buffer(commit_transaction,
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  710) 						jh, &wbuf[bufs], blocknr);
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  711) 		if (flags < 0) {
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  712) 			jbd2_journal_abort(journal, flags);
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  713) 			continue;
e6ec116b67f46 (Theodore Ts'o              2009-12-01 09:04:42 -0500  714) 		}
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  715) 		jbd2_file_log_bh(&io_bufs, wbuf[bufs]);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  716) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  717) 		/* Record the new block's tag in the current descriptor
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  718)                    buffer */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  719) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  720) 		tag_flag = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  721) 		if (flags & 1)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  722) 			tag_flag |= JBD2_FLAG_ESCAPE;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  723) 		if (!first_tag)
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  724) 			tag_flag |= JBD2_FLAG_SAME_UUID;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  725) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  726) 		tag = (journal_block_tag_t *) tagp;
db9ee220361de (Darrick J. Wong            2014-08-27 18:40:07 -0400  727) 		write_tag_block(journal, tag, jh2bh(jh)->b_blocknr);
8f888ef846d44 (Darrick J. Wong            2012-05-22 22:43:41 -0400  728) 		tag->t_flags = cpu_to_be16(tag_flag);
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  729) 		jbd2_block_tag_csum_set(journal, tag, wbuf[bufs],
c390087591dcb (Darrick J. Wong            2012-05-27 08:12:12 -0400  730) 					commit_transaction->t_tid);
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  731) 		tagp += tag_bytes;
b517bea1c74e4 (Zach Brown                 2006-10-11 01:21:08 -0700  732) 		space_left -= tag_bytes;
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  733) 		bufs++;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  734) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  735) 		if (first_tag) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  736) 			memcpy (tagp, journal->j_uuid, 16);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  737) 			tagp += 16;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  738) 			space_left -= 16;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  739) 			first_tag = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  740) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  741) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  742) 		/* If there's no more to do, or if the descriptor is full,
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  743) 		   let the IO rip! */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  744) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  745) 		if (bufs == journal->j_wbufsize ||
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  746) 		    commit_transaction->t_buffers == NULL ||
3caa487f53f65 (Darrick J. Wong            2012-05-27 08:10:22 -0400  747) 		    space_left < tag_bytes + 16 + csum_size) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  748) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  749) 			jbd_debug(4, "JBD2: Submit %d IOs\n", bufs);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  750) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  751) 			/* Write an end-of-descriptor marker before
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  752)                            submitting the IOs.  "tag" still points to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  753)                            the last tag we set up. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  754) 
8f888ef846d44 (Darrick J. Wong            2012-05-22 22:43:41 -0400  755) 			tag->t_flags |= cpu_to_be16(JBD2_FLAG_LAST_TAG);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  756) start_journal_io:
6e876c3dd205d (luojiajun                  2019-03-01 00:30:00 -0500  757) 			if (descriptor)
6e876c3dd205d (luojiajun                  2019-03-01 00:30:00 -0500  758) 				jbd2_descriptor_block_csum_set(journal,
6e876c3dd205d (luojiajun                  2019-03-01 00:30:00 -0500  759) 							descriptor);
6e876c3dd205d (luojiajun                  2019-03-01 00:30:00 -0500  760) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  761) 			for (i = 0; i < bufs; i++) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  762) 				struct buffer_head *bh = wbuf[i];
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  763) 				/*
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  764) 				 * Compute checksum.
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  765) 				 */
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  766) 				if (jbd2_has_feature_checksum(journal)) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  767) 					crc32_sum =
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  768) 					    jbd2_checksum_data(crc32_sum, bh);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  769) 				}
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  770) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  771) 				lock_buffer(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  772) 				clear_buffer_dirty(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  773) 				set_buffer_uptodate(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  774) 				bh->b_end_io = journal_end_buffer_io_sync;
70fd76140a6cb (Christoph Hellwig          2016-11-01 07:40:10 -0600  775) 				submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  776) 			}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  777) 			cond_resched();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  778) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  779) 			/* Force a new descriptor to be generated next
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  780)                            time round the loop. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  781) 			descriptor = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  782) 			bufs = 0;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  783) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  784) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  785) 
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  786) 	err = journal_finish_inode_data_buffers(journal, commit_transaction);
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  787) 	if (err) {
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  788) 		printk(KERN_WARNING
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  789) 			"JBD2: Detected IO errors while flushing file data "
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  790) 		       "on %s\n", journal->j_devname);
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  791) 		if (journal->j_flags & JBD2_ABORT_ON_SYNCDATA_ERR)
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  792) 			jbd2_journal_abort(journal, err);
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  793) 		err = 0;
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  794) 	}
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  795) 
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  796) 	/*
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  797) 	 * Get current oldest transaction in the log before we issue flush
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  798) 	 * to the filesystem device. After the flush we can be sure that
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  799) 	 * blocks of all older transactions are checkpointed to persistent
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  800) 	 * storage and we will be safe to update journal start in the
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  801) 	 * superblock with the numbers we get here.
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  802) 	 */
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  803) 	update_tail =
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  804) 		jbd2_journal_get_log_tail(journal, &first_tid, &first_block);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  805) 
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  806) 	write_lock(&journal->j_state_lock);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  807) 	if (update_tail) {
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  808) 		long freed = first_block - journal->j_tail;
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  809) 
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  810) 		if (first_block < journal->j_tail)
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  811) 			freed += journal->j_last - journal->j_first;
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  812) 		/* Update tail only if we free significant amount of space */
ede7dc7fa0af6 (Harshad Shirwadkar         2020-11-05 19:58:54 -0800  813) 		if (freed < jbd2_journal_get_max_txn_bufs(journal))
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  814) 			update_tail = 0;
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  815) 	}
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  816) 	J_ASSERT(commit_transaction->t_state == T_COMMIT);
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  817) 	commit_transaction->t_state = T_COMMIT_DFLUSH;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  818) 	write_unlock(&journal->j_state_lock);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  819) 
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  820) 	/* 
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  821) 	 * If the journal is not located on the file system device,
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  822) 	 * then we must flush the file system device before we issue
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  823) 	 * the commit record
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  824) 	 */
81be12c8179c1 (Jan Kara                   2011-05-24 11:52:40 -0400  825) 	if (commit_transaction->t_need_data_flush &&
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  826) 	    (journal->j_fs_dev != journal->j_dev) &&
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  827) 	    (journal->j_flags & JBD2_BARRIER))
c6bf3f0e25f4c (Christoph Hellwig          2021-01-26 15:52:35 +0100  828) 		blkdev_issue_flush(journal->j_fs_dev);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  829) 
cc3e1bea5d876 (Theodore Ts'o              2009-12-23 06:52:08 -0500  830) 	/* Done it all: now write the commit record asynchronously. */
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  831) 	if (jbd2_has_feature_async_commit(journal)) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  832) 		err = journal_submit_commit_record(journal, commit_transaction,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  833) 						 &cbh, crc32_sum);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  834) 		if (err)
d0a186e0d3e7a (zhangyi (F)                2019-12-04 20:46:11 +0800  835) 			jbd2_journal_abort(journal, err);
e9e34f4e8f421 (Hidehiro Kawai             2008-07-31 22:26:04 -0400  836) 	}
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  837) 
82f04ab47e1d9 (Jens Axboe                 2011-03-17 11:01:52 +0100  838) 	blk_finish_plug(&plug);
82f04ab47e1d9 (Jens Axboe                 2011-03-17 11:01:52 +0100  839) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  840) 	/* Lo and behold: we have just managed to send a transaction to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  841)            the log.  Before we can commit it, wait for the IO so far to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  842)            complete.  Control buffers being written are on the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  843)            transaction's t_log_list queue, and metadata buffers are on
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  844)            the io_bufs list.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  845) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  846) 	   Wait for the buffers in reverse order.  That way we are
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  847) 	   less likely to be woken up until all IOs have completed, and
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  848) 	   so we incur less scheduling load.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  849) 	*/
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  850) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  851) 	jbd_debug(3, "JBD2: commit phase 3\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  852) 
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  853) 	while (!list_empty(&io_bufs)) {
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  854) 		struct buffer_head *bh = list_entry(io_bufs.prev,
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  855) 						    struct buffer_head,
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  856) 						    b_assoc_buffers);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  857) 
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  858) 		wait_on_buffer(bh);
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  859) 		cond_resched();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  860) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  861) 		if (unlikely(!buffer_uptodate(bh)))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  862) 			err = -EIO;
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  863) 		jbd2_unfile_log_bh(bh);
015c603306820 (Jan Kara                   2019-11-05 17:44:19 +0100  864) 		stats.run.rs_blocks_logged++;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  865) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  866) 		/*
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  867) 		 * The list contains temporary buffer heads created by
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  868) 		 * jbd2_journal_write_metadata_buffer().
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  869) 		 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  870) 		BUFFER_TRACE(bh, "dumping temporary bh");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  871) 		__brelse(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  872) 		J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  873) 		free_buffer_head(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  874) 
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  875) 		/* We also have to refile the corresponding shadowed buffer */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  876) 		jh = commit_transaction->t_shadow_list->b_tprev;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  877) 		bh = jh2bh(jh);
f5113effc2a2e (Jan Kara                   2013-06-04 12:01:45 -0400  878) 		clear_buffer_jwrite(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  879) 		J_ASSERT_BH(bh, buffer_jbddirty(bh));
b34090e5e22a0 (Jan Kara                   2013-06-04 12:08:56 -0400  880) 		J_ASSERT_BH(bh, !buffer_shadow(bh));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  881) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  882) 		/* The metadata is now released for reuse, but we need
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  883)                    to remember it against this transaction so that when
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  884)                    we finally commit, we can do any checkpointing
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  885)                    required. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  886) 		JBUFFER_TRACE(jh, "file as BJ_Forget");
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700  887) 		jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  888) 		JBUFFER_TRACE(jh, "brelse shadowed buffer");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  889) 		__brelse(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  890) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  891) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  892) 	J_ASSERT (commit_transaction->t_shadow_list == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  893) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  894) 	jbd_debug(3, "JBD2: commit phase 4\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  895) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  896) 	/* Here we wait for the revoke record and descriptor record buffers */
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  897) 	while (!list_empty(&log_bufs)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  898) 		struct buffer_head *bh;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  899) 
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  900) 		bh = list_entry(log_bufs.prev, struct buffer_head, b_assoc_buffers);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  901) 		wait_on_buffer(bh);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  902) 		cond_resched();
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  903) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  904) 		if (unlikely(!buffer_uptodate(bh)))
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  905) 			err = -EIO;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  906) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  907) 		BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  908) 		clear_buffer_jwrite(bh);
e5a120aeb57f4 (Jan Kara                   2013-06-04 12:06:01 -0400  909) 		jbd2_unfile_log_bh(bh);
015c603306820 (Jan Kara                   2019-11-05 17:44:19 +0100  910) 		stats.run.rs_blocks_logged++;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  911) 		__brelse(bh);		/* One for getblk */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  912) 		/* AKPM: bforget here */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  913) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  914) 
77e841de8abac (Hidehiro Kawai             2008-10-12 16:39:16 -0400  915) 	if (err)
77e841de8abac (Hidehiro Kawai             2008-10-12 16:39:16 -0400  916) 		jbd2_journal_abort(journal, err);
77e841de8abac (Hidehiro Kawai             2008-10-12 16:39:16 -0400  917) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  918) 	jbd_debug(3, "JBD2: commit phase 5\n");
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  919) 	write_lock(&journal->j_state_lock);
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  920) 	J_ASSERT(commit_transaction->t_state == T_COMMIT_DFLUSH);
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  921) 	commit_transaction->t_state = T_COMMIT_JFLUSH;
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400  922) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  923) 
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  924) 	if (!jbd2_has_feature_async_commit(journal)) {
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  925) 		err = journal_submit_commit_record(journal, commit_transaction,
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  926) 						&cbh, crc32_sum);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  927) 		if (err)
d0a186e0d3e7a (zhangyi (F)                2019-12-04 20:46:11 +0800  928) 			jbd2_journal_abort(journal, err);
818d276ceb83a (Girish Shilamkar           2008-01-28 23:58:27 -0500  929) 	}
6cba611e600de (Zhang Huan                 2011-04-05 19:16:20 -0400  930) 	if (cbh)
fd98496f467b3 (Theodore Ts'o              2009-01-05 21:34:13 -0500  931) 		err = journal_wait_on_commit_record(journal, cbh);
015c603306820 (Jan Kara                   2019-11-05 17:44:19 +0100  932) 	stats.run.rs_blocks_logged++;
56316a0d28f25 (Darrick J. Wong            2015-10-17 16:18:45 -0400  933) 	if (jbd2_has_feature_async_commit(journal) &&
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  934) 	    journal->j_flags & JBD2_BARRIER) {
c6bf3f0e25f4c (Christoph Hellwig          2021-01-26 15:52:35 +0100  935) 		blkdev_issue_flush(journal->j_dev);
f73bee49855fe (Jan Kara                   2010-08-18 15:56:56 +0200  936) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  937) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  938) 	if (err)
a7fa2baf8e2a6 (Jan Kara                   2007-10-16 18:38:25 -0400  939) 		jbd2_journal_abort(journal, err);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  940) 
9f356e5a4f120 (Jan Kara                   2019-11-05 17:44:24 +0100  941) 	WARN_ON_ONCE(
9f356e5a4f120 (Jan Kara                   2019-11-05 17:44:24 +0100  942) 		atomic_read(&commit_transaction->t_outstanding_credits) < 0);
9f356e5a4f120 (Jan Kara                   2019-11-05 17:44:24 +0100  943) 
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  944) 	/*
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  945) 	 * Now disk caches for filesystem device are flushed so we are safe to
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  946) 	 * erase checkpointed transactions from the log by updating journal
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  947) 	 * superblock.
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  948) 	 */
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  949) 	if (update_tail)
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  950) 		jbd2_update_log_tail(journal, first_tid, first_block);
3339578f05787 (Jan Kara                   2012-03-13 22:45:38 -0400  951) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  952) 	/* End of a transaction!  Finally, we can do checkpoint
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  953)            processing: any buffers committed as a result of this
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  954)            transaction can be removed from any checkpoint list it was on
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  955)            before. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  956) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400  957) 	jbd_debug(3, "JBD2: commit phase 6\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  958) 
c851ed5401737 (Jan Kara                   2008-07-11 19:27:31 -0400  959) 	J_ASSERT(list_empty(&commit_transaction->t_inode_list));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  960) 	J_ASSERT(commit_transaction->t_buffers == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  961) 	J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  962) 	J_ASSERT(commit_transaction->t_shadow_list == NULL);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  963) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  964) restart_loop:
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  965) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  966) 	 * As there are other places (journal_unmap_buffer()) adding buffers
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  967) 	 * to this list we have to be careful and hold the j_list_lock.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  968) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  969) 	spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  970) 	while (commit_transaction->t_forget) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  971) 		transaction_t *cp_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  972) 		struct buffer_head *bh;
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400  973) 		int try_to_free = 0;
93108ebb848df (Jan Kara                   2019-08-09 14:42:29 +0200  974) 		bool drop_ref;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  975) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  976) 		jh = commit_transaction->t_forget;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  977) 		spin_unlock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  978) 		bh = jh2bh(jh);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400  979) 		/*
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400  980) 		 * Get a reference so that bh cannot be freed before we are
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400  981) 		 * done with it.
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400  982) 		 */
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400  983) 		get_bh(bh);
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200  984) 		spin_lock(&jh->b_state_lock);
23e2af3518fac (dingdinghua                2010-02-24 12:11:20 -0500  985) 		J_ASSERT_JH(jh,	jh->b_transaction == commit_transaction);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  986) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  987) 		/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  988) 		 * If there is undo-protected committed data against
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  989) 		 * this buffer, then we can remove it now.  If it is a
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  990) 		 * buffer needing such protection, the old frozen_data
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  991) 		 * field now points to a committed version of the
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  992) 		 * buffer, so rotate that field to the new committed
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  993) 		 * data.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  994) 		 *
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  995) 		 * Otherwise, we can just throw away the frozen data now.
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  996) 		 *
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  997) 		 * We also know that the frozen data has already fired
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700  998) 		 * its triggers if they exist, so we can clear that too.
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700  999) 		 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1000) 		if (jh->b_committed_data) {
af1e76d6b3f37 (Mingming Cao               2007-10-16 18:38:25 -0400 1001) 			jbd2_free(jh->b_committed_data, bh->b_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1002) 			jh->b_committed_data = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1003) 			if (jh->b_frozen_data) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1004) 				jh->b_committed_data = jh->b_frozen_data;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1005) 				jh->b_frozen_data = NULL;
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700 1006) 				jh->b_frozen_triggers = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1007) 			}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1008) 		} else if (jh->b_frozen_data) {
af1e76d6b3f37 (Mingming Cao               2007-10-16 18:38:25 -0400 1009) 			jbd2_free(jh->b_frozen_data, bh->b_size);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1010) 			jh->b_frozen_data = NULL;
e06c8227fd94e (Joel Becker                2008-09-11 15:35:47 -0700 1011) 			jh->b_frozen_triggers = NULL;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1012) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1013) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1014) 		spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1015) 		cp_transaction = jh->b_cp_transaction;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1016) 		if (cp_transaction) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1017) 			JBUFFER_TRACE(jh, "remove from old cp transaction");
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1018) 			cp_transaction->t_chp_stats.cs_dropped++;
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1019) 			__jbd2_journal_remove_checkpoint(jh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1020) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1021) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1022) 		/* Only re-checkpoint the buffer_head if it is marked
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1023) 		 * dirty.  If the buffer was added to the BJ_Forget list
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1024) 		 * by jbd2_journal_forget, it may no longer be dirty and
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1025) 		 * there's no point in keeping a checkpoint record for
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1026) 		 * it. */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1027) 
b794e7a6ebfbd (Jan Kara                   2012-09-26 23:11:13 -0400 1028) 		/*
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1029) 		 * A buffer which has been freed while still being journaled
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1030) 		 * by a previous transaction, refile the buffer to BJ_Forget of
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1031) 		 * the running transaction. If the just committed transaction
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1032) 		 * contains "add to orphan" operation, we can completely
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1033) 		 * invalidate the buffer now. We are rather through in that
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1034) 		 * since the buffer may be still accessible when blocksize <
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1035) 		 * pagesize and it is attached to the last partial page.
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1036) 		 */
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1037) 		if (buffer_freed(bh) && !jh->b_next_transaction) {
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1038) 			struct address_space *mapping;
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1039) 
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1040) 			clear_buffer_freed(bh);
6a66a7ded12ba (zhangyi (F)                2020-02-13 14:38:20 +0800 1041) 			clear_buffer_jbddirty(bh);
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1042) 
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1043) 			/*
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1044) 			 * Block device buffers need to stay mapped all the
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1045) 			 * time, so it is enough to clear buffer_jbddirty and
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1046) 			 * buffer_freed bits. For the file mapping buffers (i.e.
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1047) 			 * journalled data) we need to unmap buffer and clear
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1048) 			 * more bits. We also need to be careful about the check
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1049) 			 * because the data page mapping can get cleared under
780f66e59231f (zhangyi (F)                2020-02-17 19:27:06 +0800 1050) 			 * our hands. Note that if mapping == NULL, we don't
780f66e59231f (zhangyi (F)                2020-02-17 19:27:06 +0800 1051) 			 * need to make buffer unmapped because the page is
780f66e59231f (zhangyi (F)                2020-02-17 19:27:06 +0800 1052) 			 * already detached from the mapping and buffers cannot
780f66e59231f (zhangyi (F)                2020-02-17 19:27:06 +0800 1053) 			 * get reused.
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1054) 			 */
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1055) 			mapping = READ_ONCE(bh->b_page->mapping);
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1056) 			if (mapping && !sb_is_blkdev_sb(mapping->host->i_sb)) {
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1057) 				clear_buffer_mapped(bh);
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1058) 				clear_buffer_new(bh);
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1059) 				clear_buffer_req(bh);
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1060) 				bh->b_bdev = NULL;
c96dceeabf765 (zhangyi (F)                2020-02-13 14:38:21 +0800 1061) 			}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1062) 		}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1063) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1064) 		if (buffer_jbddirty(bh)) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1065) 			JBUFFER_TRACE(jh, "add to new checkpointing trans");
f7f4bccb72984 (Mingming Cao               2006-10-11 01:20:59 -0700 1066) 			__jbd2_journal_insert_checkpoint(jh, commit_transaction);
7ad7445f60fe4 (Hidehiro Kawai             2008-10-10 20:29:31 -0400 1067) 			if (is_journal_aborted(journal))
7ad7445f60fe4 (Hidehiro Kawai             2008-10-10 20:29:31 -0400 1068) 				clear_buffer_jbddirty(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1069) 		} else {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1070) 			J_ASSERT_BH(bh, !buffer_dirty(bh));
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1071) 			/*
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1072) 			 * The buffer on BJ_Forget list and not jbddirty means
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1073) 			 * it has been freed by this transaction and hence it
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1074) 			 * could not have been reallocated until this
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1075) 			 * transaction has committed. *BUT* it could be
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1076) 			 * reallocated once we have written all the data to
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1077) 			 * disk and before we process the buffer on BJ_Forget
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1078) 			 * list.
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1079) 			 */
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1080) 			if (!jh->b_next_transaction)
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1081) 				try_to_free = 1;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1082) 		}
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1083) 		JBUFFER_TRACE(jh, "refile or unfile buffer");
93108ebb848df (Jan Kara                   2019-08-09 14:42:29 +0200 1084) 		drop_ref = __jbd2_journal_refile_buffer(jh);
464170647b564 (Thomas Gleixner            2019-08-09 14:42:32 +0200 1085) 		spin_unlock(&jh->b_state_lock);
93108ebb848df (Jan Kara                   2019-08-09 14:42:29 +0200 1086) 		if (drop_ref)
93108ebb848df (Jan Kara                   2019-08-09 14:42:29 +0200 1087) 			jbd2_journal_put_journal_head(jh);
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1088) 		if (try_to_free)
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1089) 			release_buffer_page(bh);	/* Drops bh reference */
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1090) 		else
de1b794130b13 (Jan Kara                   2011-06-13 15:38:22 -0400 1091) 			__brelse(bh);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1092) 		cond_resched_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1093) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1094) 	spin_unlock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1095) 	/*
f5a7a6b0d9b6a (Jan Kara                   2008-01-28 23:58:27 -0500 1096) 	 * This is a bit sleazy.  We use j_list_lock to protect transition
f5a7a6b0d9b6a (Jan Kara                   2008-01-28 23:58:27 -0500 1097) 	 * of a transaction into T_FINISHED state and calling
f5a7a6b0d9b6a (Jan Kara                   2008-01-28 23:58:27 -0500 1098) 	 * __jbd2_journal_drop_transaction(). Otherwise we could race with
f5a7a6b0d9b6a (Jan Kara                   2008-01-28 23:58:27 -0500 1099) 	 * other checkpointing code processing the transaction...
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1100) 	 */
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 1101) 	write_lock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1102) 	spin_lock(&journal->j_list_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1103) 	/*
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1104) 	 * Now recheck if some buffers did not get attached to the transaction
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1105) 	 * while the lock was dropped...
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1106) 	 */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1107) 	if (commit_transaction->t_forget) {
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1108) 		spin_unlock(&journal->j_list_lock);
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 1109) 		write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1110) 		goto restart_loop;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1111) 	}
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1112) 
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1113) 	/* Add the transaction to the checkpoint list
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1114) 	 * __journal_remove_checkpoint() can not destroy transaction
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1115) 	 * under us because it is not marked as T_FINISHED yet */
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1116) 	if (journal->j_checkpoint_transactions == NULL) {
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1117) 		journal->j_checkpoint_transactions = commit_transaction;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1118) 		commit_transaction->t_cpnext = commit_transaction;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1119) 		commit_transaction->t_cpprev = commit_transaction;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1120) 	} else {
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1121) 		commit_transaction->t_cpnext =
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1122) 			journal->j_checkpoint_transactions;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1123) 		commit_transaction->t_cpprev =
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1124) 			commit_transaction->t_cpnext->t_cpprev;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1125) 		commit_transaction->t_cpnext->t_cpprev =
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1126) 			commit_transaction;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1127) 		commit_transaction->t_cpprev->t_cpnext =
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1128) 				commit_transaction;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1129) 	}
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1130) 	spin_unlock(&journal->j_list_lock);
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1131) 
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1132) 	/* Done with this transaction! */
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1133) 
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1134) 	jbd_debug(3, "JBD2: commit phase 7\n");
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1135) 
bbd2be3691072 (Jan Kara                   2011-05-24 11:59:18 -0400 1136) 	J_ASSERT(commit_transaction->t_state == T_COMMIT_JFLUSH);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1137) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1138) 	commit_transaction->t_start = jiffies;
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1139) 	stats.run.rs_logging = jbd2_time_diff(stats.run.rs_logging,
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1140) 					      commit_transaction->t_start);
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1141) 
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1142) 	/*
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1143) 	 * File the transaction statistics
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1144) 	 */
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1145) 	stats.ts_tid = commit_transaction->t_tid;
8dd420466c7bf (Theodore Ts'o              2010-08-03 21:38:29 -0400 1146) 	stats.run.rs_handle_count =
8dd420466c7bf (Theodore Ts'o              2010-08-03 21:38:29 -0400 1147) 		atomic_read(&commit_transaction->t_handle_count);
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1148) 	trace_jbd2_run_stats(journal->j_fs_dev->bd_dev,
bf6993276f74d (Theodore Ts'o              2009-09-30 00:32:06 -0400 1149) 			     commit_transaction->t_tid, &stats.run);
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1150) 	stats.ts_requested = (commit_transaction->t_requested) ? 1 : 0;
8e85fb3f305b2 (Johann Lombardi            2008-01-28 23:58:27 -0500 1151) 
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1152) 	commit_transaction->t_state = T_COMMIT_CALLBACK;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1153) 	J_ASSERT(commit_transaction == journal->j_committing_transaction);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1154) 	journal->j_commit_sequence = commit_transaction->t_tid;
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1155) 	journal->j_committing_transaction = NULL;
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1156) 	commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1157) 
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1158) 	/*
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1159) 	 * weight the commit time higher than the average time so we don't
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1160) 	 * react too strongly to vast changes in the commit time
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1161) 	 */
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1162) 	if (likely(journal->j_average_commit_time))
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1163) 		journal->j_average_commit_time = (commit_time +
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1164) 				journal->j_average_commit_time*3) / 4;
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1165) 	else
e07f7183a486c (Josef Bacik                2008-11-26 01:14:26 -0500 1166) 		journal->j_average_commit_time = commit_time;
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1167) 
a931da6ac9331 (Theodore Ts'o              2010-08-03 21:35:12 -0400 1168) 	write_unlock(&journal->j_state_lock);
6c20ec850360b (Theodore Ts'o              2008-10-28 21:08:20 -0400 1169) 
fb68407b0d9ef (Aneesh Kumar K.V           2008-11-06 17:50:21 -0500 1170) 	if (journal->j_commit_callback)
fb68407b0d9ef (Aneesh Kumar K.V           2008-11-06 17:50:21 -0500 1171) 		journal->j_commit_callback(journal, commit_transaction);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1172) 	if (journal->j_fc_cleanup_callback)
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1173) 		journal->j_fc_cleanup_callback(journal, 1);
fb68407b0d9ef (Aneesh Kumar K.V           2008-11-06 17:50:21 -0500 1174) 
879c5e6b7cb4c (Theodore Ts'o              2009-06-17 11:47:48 -0400 1175) 	trace_jbd2_end_commit(journal, commit_transaction);
f2a44523b20f3 (Eryu Guan                  2011-11-01 19:09:18 -0400 1176) 	jbd_debug(1, "JBD2: commit %d complete, head %d\n",
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1177) 		  journal->j_commit_sequence, journal->j_tail_sequence);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1178) 
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1179) 	write_lock(&journal->j_state_lock);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1180) 	journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1181) 	journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1182) 	spin_lock(&journal->j_list_lock);
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1183) 	commit_transaction->t_state = T_FINISHED;
d4e839d4a9dc3 (Theodore Ts'o              2014-03-08 22:34:10 -0500 1184) 	/* Check if the transaction can be dropped now that we are finished */
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1185) 	if (commit_transaction->t_checkpoint_list == NULL &&
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1186) 	    commit_transaction->t_checkpoint_io_list == NULL) {
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1187) 		__jbd2_journal_drop_transaction(journal, commit_transaction);
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1188) 		jbd2_journal_free_transaction(commit_transaction);
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1189) 	}
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1190) 	spin_unlock(&journal->j_list_lock);
794446c694651 (Dmitry Monakhov            2013-04-03 22:06:52 -0400 1191) 	write_unlock(&journal->j_state_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1192) 	wake_up(&journal->j_wait_done_commit);
ff780b91efe90 (Harshad Shirwadkar         2020-10-15 13:37:56 -0700 1193) 	wake_up(&journal->j_fc_wait);
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1194) 
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1195) 	/*
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1196) 	 * Calculate overall stats
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1197) 	 */
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1198) 	spin_lock(&journal->j_history_lock);
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1199) 	journal->j_stats.ts_tid++;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1200) 	journal->j_stats.ts_requested += stats.ts_requested;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1201) 	journal->j_stats.run.rs_wait += stats.run.rs_wait;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1202) 	journal->j_stats.run.rs_request_delay += stats.run.rs_request_delay;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1203) 	journal->j_stats.run.rs_running += stats.run.rs_running;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1204) 	journal->j_stats.run.rs_locked += stats.run.rs_locked;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1205) 	journal->j_stats.run.rs_flushing += stats.run.rs_flushing;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1206) 	journal->j_stats.run.rs_logging += stats.run.rs_logging;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1207) 	journal->j_stats.run.rs_handle_count += stats.run.rs_handle_count;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1208) 	journal->j_stats.run.rs_blocks += stats.run.rs_blocks;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1209) 	journal->j_stats.run.rs_blocks_logged += stats.run.rs_blocks_logged;
42cf3452d5f5b (Theodore Ts'o              2014-03-08 19:51:16 -0500 1210) 	spin_unlock(&journal->j_history_lock);
470decc613ab2 (Dave Kleikamp              2006-10-11 01:20:57 -0700 1211) }