1470decc6SDave Kleikamp /* 2f7f4bccbSMingming Cao * linux/fs/jbd2/commit.c 3470decc6SDave Kleikamp * 4470decc6SDave Kleikamp * Written by Stephen C. Tweedie <[email protected]>, 1998 5470decc6SDave Kleikamp * 6470decc6SDave Kleikamp * Copyright 1998 Red Hat corp --- All Rights Reserved 7470decc6SDave Kleikamp * 8470decc6SDave Kleikamp * This file is part of the Linux kernel and is made available under 9470decc6SDave Kleikamp * the terms of the GNU General Public License, version 2, or at your 10470decc6SDave Kleikamp * option, any later version, incorporated herein by reference. 11470decc6SDave Kleikamp * 12470decc6SDave Kleikamp * Journal commit routines for the generic filesystem journaling code; 13470decc6SDave Kleikamp * part of the ext2fs journaling system. 14470decc6SDave Kleikamp */ 15470decc6SDave Kleikamp 16470decc6SDave Kleikamp #include <linux/time.h> 17470decc6SDave Kleikamp #include <linux/fs.h> 18f7f4bccbSMingming Cao #include <linux/jbd2.h> 19470decc6SDave Kleikamp #include <linux/errno.h> 20470decc6SDave Kleikamp #include <linux/slab.h> 21470decc6SDave Kleikamp #include <linux/mm.h> 22470decc6SDave Kleikamp #include <linux/pagemap.h> 238e85fb3fSJohann Lombardi #include <linux/jiffies.h> 24818d276cSGirish Shilamkar #include <linux/crc32.h> 25cd1aac32SAneesh Kumar K.V #include <linux/writeback.h> 26cd1aac32SAneesh Kumar K.V #include <linux/backing-dev.h> 27fd98496fSTheodore Ts'o #include <linux/bio.h> 280e3d2a63STheodore Ts'o #include <linux/blkdev.h> 2939e3ac25SBrian King #include <linux/bitops.h> 30879c5e6bSTheodore Ts'o #include <trace/events/jbd2.h> 3139e3ac25SBrian King #include <asm/system.h> 32470decc6SDave Kleikamp 33470decc6SDave Kleikamp /* 34470decc6SDave Kleikamp * Default IO end handler for temporary BJ_IO buffer_heads. 35470decc6SDave Kleikamp */ 36470decc6SDave Kleikamp static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate) 37470decc6SDave Kleikamp { 38470decc6SDave Kleikamp BUFFER_TRACE(bh, ""); 39470decc6SDave Kleikamp if (uptodate) 40470decc6SDave Kleikamp set_buffer_uptodate(bh); 41470decc6SDave Kleikamp else 42470decc6SDave Kleikamp clear_buffer_uptodate(bh); 43470decc6SDave Kleikamp unlock_buffer(bh); 44470decc6SDave Kleikamp } 45470decc6SDave Kleikamp 46470decc6SDave Kleikamp /* 4787c89c23SJan Kara * When an ext4 file is truncated, it is possible that some pages are not 4887c89c23SJan Kara * successfully freed, because they are attached to a committing transaction. 49470decc6SDave Kleikamp * After the transaction commits, these pages are left on the LRU, with no 50470decc6SDave Kleikamp * ->mapping, and with attached buffers. These pages are trivially reclaimable 51470decc6SDave Kleikamp * by the VM, but their apparent absence upsets the VM accounting, and it makes 52470decc6SDave Kleikamp * the numbers in /proc/meminfo look odd. 53470decc6SDave Kleikamp * 54470decc6SDave Kleikamp * So here, we have a buffer which has just come off the forget list. Look to 55470decc6SDave Kleikamp * see if we can strip all buffers from the backing page. 56470decc6SDave Kleikamp * 57470decc6SDave Kleikamp * Called under lock_journal(), and possibly under journal_datalist_lock. The 58470decc6SDave Kleikamp * caller provided us with a ref against the buffer, and we drop that here. 59470decc6SDave Kleikamp */ 60470decc6SDave Kleikamp static void release_buffer_page(struct buffer_head *bh) 61470decc6SDave Kleikamp { 62470decc6SDave Kleikamp struct page *page; 63470decc6SDave Kleikamp 64470decc6SDave Kleikamp if (buffer_dirty(bh)) 65470decc6SDave Kleikamp goto nope; 66470decc6SDave Kleikamp if (atomic_read(&bh->b_count) != 1) 67470decc6SDave Kleikamp goto nope; 68470decc6SDave Kleikamp page = bh->b_page; 69470decc6SDave Kleikamp if (!page) 70470decc6SDave Kleikamp goto nope; 71470decc6SDave Kleikamp if (page->mapping) 72470decc6SDave Kleikamp goto nope; 73470decc6SDave Kleikamp 74470decc6SDave Kleikamp /* OK, it's a truncated page */ 75529ae9aaSNick Piggin if (!trylock_page(page)) 76470decc6SDave Kleikamp goto nope; 77470decc6SDave Kleikamp 78470decc6SDave Kleikamp page_cache_get(page); 79470decc6SDave Kleikamp __brelse(bh); 80470decc6SDave Kleikamp try_to_free_buffers(page); 81470decc6SDave Kleikamp unlock_page(page); 82470decc6SDave Kleikamp page_cache_release(page); 83470decc6SDave Kleikamp return; 84470decc6SDave Kleikamp 85470decc6SDave Kleikamp nope: 86470decc6SDave Kleikamp __brelse(bh); 87470decc6SDave Kleikamp } 88470decc6SDave Kleikamp 89470decc6SDave Kleikamp /* 90818d276cSGirish Shilamkar * Done it all: now submit the commit record. We should have 91470decc6SDave Kleikamp * cleaned up our previous buffers by now, so if we are in abort 92470decc6SDave Kleikamp * mode we can now just skip the rest of the journal write 93470decc6SDave Kleikamp * entirely. 94470decc6SDave Kleikamp * 95470decc6SDave Kleikamp * Returns 1 if the journal needs to be aborted or 0 on success 96470decc6SDave Kleikamp */ 97818d276cSGirish Shilamkar static int journal_submit_commit_record(journal_t *journal, 98818d276cSGirish Shilamkar transaction_t *commit_transaction, 99818d276cSGirish Shilamkar struct buffer_head **cbh, 100818d276cSGirish Shilamkar __u32 crc32_sum) 101470decc6SDave Kleikamp { 102470decc6SDave Kleikamp struct journal_head *descriptor; 103818d276cSGirish Shilamkar struct commit_header *tmp; 104470decc6SDave Kleikamp struct buffer_head *bh; 105818d276cSGirish Shilamkar int ret; 106736603abSTheodore Ts'o struct timespec now = current_kernel_time(); 107470decc6SDave Kleikamp 1086cba611eSZhang Huan *cbh = NULL; 1096cba611eSZhang Huan 110470decc6SDave Kleikamp if (is_journal_aborted(journal)) 111470decc6SDave Kleikamp return 0; 112470decc6SDave Kleikamp 113f7f4bccbSMingming Cao descriptor = jbd2_journal_get_descriptor_buffer(journal); 114470decc6SDave Kleikamp if (!descriptor) 115470decc6SDave Kleikamp return 1; 116470decc6SDave Kleikamp 117470decc6SDave Kleikamp bh = jh2bh(descriptor); 118470decc6SDave Kleikamp 119818d276cSGirish Shilamkar tmp = (struct commit_header *)bh->b_data; 120f7f4bccbSMingming Cao tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 121f7f4bccbSMingming Cao tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK); 122470decc6SDave Kleikamp tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid); 123736603abSTheodore Ts'o tmp->h_commit_sec = cpu_to_be64(now.tv_sec); 124736603abSTheodore Ts'o tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec); 125818d276cSGirish Shilamkar 126818d276cSGirish Shilamkar if (JBD2_HAS_COMPAT_FEATURE(journal, 127818d276cSGirish Shilamkar JBD2_FEATURE_COMPAT_CHECKSUM)) { 128818d276cSGirish Shilamkar tmp->h_chksum_type = JBD2_CRC32_CHKSUM; 129818d276cSGirish Shilamkar tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE; 130818d276cSGirish Shilamkar tmp->h_chksum[0] = cpu_to_be32(crc32_sum); 131470decc6SDave Kleikamp } 132470decc6SDave Kleikamp 133818d276cSGirish Shilamkar JBUFFER_TRACE(descriptor, "submit commit block"); 134818d276cSGirish Shilamkar lock_buffer(bh); 13545a90bfdSTheodore Ts'o clear_buffer_dirty(bh); 136818d276cSGirish Shilamkar set_buffer_uptodate(bh); 137818d276cSGirish Shilamkar bh->b_end_io = journal_end_buffer_io_sync; 138818d276cSGirish Shilamkar 139818d276cSGirish Shilamkar if (journal->j_flags & JBD2_BARRIER && 1404d605179SAneesh Kumar K.V !JBD2_HAS_INCOMPAT_FEATURE(journal, 1419c35575bSChristoph Hellwig JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) 142721a9602SJens Axboe ret = submit_bh(WRITE_SYNC | WRITE_FLUSH_FUA, bh); 1439c35575bSChristoph Hellwig else 144721a9602SJens Axboe ret = submit_bh(WRITE_SYNC, bh); 145470decc6SDave Kleikamp 146818d276cSGirish Shilamkar *cbh = bh; 147818d276cSGirish Shilamkar return ret; 148818d276cSGirish Shilamkar } 149470decc6SDave Kleikamp 150818d276cSGirish Shilamkar /* 151818d276cSGirish Shilamkar * This function along with journal_submit_commit_record 152818d276cSGirish Shilamkar * allows to write the commit record asynchronously. 153818d276cSGirish Shilamkar */ 154fd98496fSTheodore Ts'o static int journal_wait_on_commit_record(journal_t *journal, 155fd98496fSTheodore Ts'o struct buffer_head *bh) 156818d276cSGirish Shilamkar { 157818d276cSGirish Shilamkar int ret = 0; 158818d276cSGirish Shilamkar 159818d276cSGirish Shilamkar clear_buffer_dirty(bh); 160818d276cSGirish Shilamkar wait_on_buffer(bh); 161818d276cSGirish Shilamkar 162818d276cSGirish Shilamkar if (unlikely(!buffer_uptodate(bh))) 163818d276cSGirish Shilamkar ret = -EIO; 164818d276cSGirish Shilamkar put_bh(bh); /* One for getblk() */ 165818d276cSGirish Shilamkar jbd2_journal_put_journal_head(bh2jh(bh)); 166818d276cSGirish Shilamkar 167818d276cSGirish Shilamkar return ret; 168818d276cSGirish Shilamkar } 169818d276cSGirish Shilamkar 170818d276cSGirish Shilamkar /* 171cd1aac32SAneesh Kumar K.V * write the filemap data using writepage() address_space_operations. 172cd1aac32SAneesh Kumar K.V * We don't do block allocation here even for delalloc. We don't 173cd1aac32SAneesh Kumar K.V * use writepages() because with dealyed allocation we may be doing 174cd1aac32SAneesh Kumar K.V * block allocation in writepages(). 175cd1aac32SAneesh Kumar K.V */ 176cd1aac32SAneesh Kumar K.V static int journal_submit_inode_data_buffers(struct address_space *mapping) 177cd1aac32SAneesh Kumar K.V { 178cd1aac32SAneesh Kumar K.V int ret; 179cd1aac32SAneesh Kumar K.V struct writeback_control wbc = { 180cd1aac32SAneesh Kumar K.V .sync_mode = WB_SYNC_ALL, 181cd1aac32SAneesh Kumar K.V .nr_to_write = mapping->nrpages * 2, 182cd1aac32SAneesh Kumar K.V .range_start = 0, 183cd1aac32SAneesh Kumar K.V .range_end = i_size_read(mapping->host), 184cd1aac32SAneesh Kumar K.V }; 185cd1aac32SAneesh Kumar K.V 186cd1aac32SAneesh Kumar K.V ret = generic_writepages(mapping, &wbc); 187cd1aac32SAneesh Kumar K.V return ret; 188cd1aac32SAneesh Kumar K.V } 189cd1aac32SAneesh Kumar K.V 190cd1aac32SAneesh Kumar K.V /* 191c851ed54SJan Kara * Submit all the data buffers of inode associated with the transaction to 192c851ed54SJan Kara * disk. 193c851ed54SJan Kara * 194c851ed54SJan Kara * We are in a committing transaction. Therefore no new inode can be added to 195c851ed54SJan Kara * our inode list. We use JI_COMMIT_RUNNING flag to protect inode we currently 196c851ed54SJan Kara * operate on from being released while we write out pages. 197c851ed54SJan Kara */ 198cd1aac32SAneesh Kumar K.V static int journal_submit_data_buffers(journal_t *journal, 199c851ed54SJan Kara transaction_t *commit_transaction) 200c851ed54SJan Kara { 201c851ed54SJan Kara struct jbd2_inode *jinode; 202c851ed54SJan Kara int err, ret = 0; 203c851ed54SJan Kara struct address_space *mapping; 204c851ed54SJan Kara 205c851ed54SJan Kara spin_lock(&journal->j_list_lock); 206c851ed54SJan Kara list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { 207c851ed54SJan Kara mapping = jinode->i_vfs_inode->i_mapping; 20839e3ac25SBrian King set_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 209c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 210cd1aac32SAneesh Kumar K.V /* 211cd1aac32SAneesh Kumar K.V * submit the inode data buffers. We use writepage 212cd1aac32SAneesh Kumar K.V * instead of writepages. Because writepages can do 213cd1aac32SAneesh Kumar K.V * block allocation with delalloc. We need to write 214cd1aac32SAneesh Kumar K.V * only allocated blocks here. 215cd1aac32SAneesh Kumar K.V */ 216879c5e6bSTheodore Ts'o trace_jbd2_submit_inode_data(jinode->i_vfs_inode); 217cd1aac32SAneesh Kumar K.V err = journal_submit_inode_data_buffers(mapping); 218c851ed54SJan Kara if (!ret) 219c851ed54SJan Kara ret = err; 220c851ed54SJan Kara spin_lock(&journal->j_list_lock); 221c851ed54SJan Kara J_ASSERT(jinode->i_transaction == commit_transaction); 22239e3ac25SBrian King clear_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 22339e3ac25SBrian King smp_mb__after_clear_bit(); 224c851ed54SJan Kara wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); 225c851ed54SJan Kara } 226c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 227c851ed54SJan Kara return ret; 228c851ed54SJan Kara } 229c851ed54SJan Kara 230c851ed54SJan Kara /* 231c851ed54SJan Kara * Wait for data submitted for writeout, refile inodes to proper 232c851ed54SJan Kara * transaction if needed. 233c851ed54SJan Kara * 234c851ed54SJan Kara */ 235c851ed54SJan Kara static int journal_finish_inode_data_buffers(journal_t *journal, 236c851ed54SJan Kara transaction_t *commit_transaction) 237c851ed54SJan Kara { 238c851ed54SJan Kara struct jbd2_inode *jinode, *next_i; 239c851ed54SJan Kara int err, ret = 0; 240c851ed54SJan Kara 241cd1aac32SAneesh Kumar K.V /* For locking, see the comment in journal_submit_data_buffers() */ 242c851ed54SJan Kara spin_lock(&journal->j_list_lock); 243c851ed54SJan Kara list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { 24439e3ac25SBrian King set_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 245c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 246c851ed54SJan Kara err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping); 247e9e34f4eSHidehiro Kawai if (err) { 248e9e34f4eSHidehiro Kawai /* 249e9e34f4eSHidehiro Kawai * Because AS_EIO is cleared by 25094004ed7SChristoph Hellwig * filemap_fdatawait_range(), set it again so 251e9e34f4eSHidehiro Kawai * that user process can get -EIO from fsync(). 252e9e34f4eSHidehiro Kawai */ 253e9e34f4eSHidehiro Kawai set_bit(AS_EIO, 254e9e34f4eSHidehiro Kawai &jinode->i_vfs_inode->i_mapping->flags); 255e9e34f4eSHidehiro Kawai 256c851ed54SJan Kara if (!ret) 257c851ed54SJan Kara ret = err; 258e9e34f4eSHidehiro Kawai } 259c851ed54SJan Kara spin_lock(&journal->j_list_lock); 26039e3ac25SBrian King clear_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 26139e3ac25SBrian King smp_mb__after_clear_bit(); 262c851ed54SJan Kara wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); 263c851ed54SJan Kara } 264c851ed54SJan Kara 265c851ed54SJan Kara /* Now refile inode to proper lists */ 266c851ed54SJan Kara list_for_each_entry_safe(jinode, next_i, 267c851ed54SJan Kara &commit_transaction->t_inode_list, i_list) { 268c851ed54SJan Kara list_del(&jinode->i_list); 269c851ed54SJan Kara if (jinode->i_next_transaction) { 270c851ed54SJan Kara jinode->i_transaction = jinode->i_next_transaction; 271c851ed54SJan Kara jinode->i_next_transaction = NULL; 272c851ed54SJan Kara list_add(&jinode->i_list, 273c851ed54SJan Kara &jinode->i_transaction->t_inode_list); 274c851ed54SJan Kara } else { 275c851ed54SJan Kara jinode->i_transaction = NULL; 276c851ed54SJan Kara } 277c851ed54SJan Kara } 278c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 279c851ed54SJan Kara 280c851ed54SJan Kara return ret; 281c851ed54SJan Kara } 282c851ed54SJan Kara 283818d276cSGirish Shilamkar static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh) 284818d276cSGirish Shilamkar { 285818d276cSGirish Shilamkar struct page *page = bh->b_page; 286818d276cSGirish Shilamkar char *addr; 287818d276cSGirish Shilamkar __u32 checksum; 288818d276cSGirish Shilamkar 289818d276cSGirish Shilamkar addr = kmap_atomic(page, KM_USER0); 290818d276cSGirish Shilamkar checksum = crc32_be(crc32_sum, 291818d276cSGirish Shilamkar (void *)(addr + offset_in_page(bh->b_data)), bh->b_size); 292818d276cSGirish Shilamkar kunmap_atomic(addr, KM_USER0); 293818d276cSGirish Shilamkar 294818d276cSGirish Shilamkar return checksum; 295818d276cSGirish Shilamkar } 296818d276cSGirish Shilamkar 297818d276cSGirish Shilamkar static void write_tag_block(int tag_bytes, journal_block_tag_t *tag, 29818eba7aaSMingming Cao unsigned long long block) 299b517bea1SZach Brown { 300b517bea1SZach Brown tag->t_blocknr = cpu_to_be32(block & (u32)~0); 301cd02ff0bSMingming Cao if (tag_bytes > JBD2_TAG_SIZE32) 302b517bea1SZach Brown tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1); 303b517bea1SZach Brown } 304b517bea1SZach Brown 305470decc6SDave Kleikamp /* 306f7f4bccbSMingming Cao * jbd2_journal_commit_transaction 307470decc6SDave Kleikamp * 308470decc6SDave Kleikamp * The primary function for committing a transaction to the log. This 309470decc6SDave Kleikamp * function is called by the journal thread to begin a complete commit. 310470decc6SDave Kleikamp */ 311f7f4bccbSMingming Cao void jbd2_journal_commit_transaction(journal_t *journal) 312470decc6SDave Kleikamp { 3138e85fb3fSJohann Lombardi struct transaction_stats_s stats; 314470decc6SDave Kleikamp transaction_t *commit_transaction; 315470decc6SDave Kleikamp struct journal_head *jh, *new_jh, *descriptor; 316470decc6SDave Kleikamp struct buffer_head **wbuf = journal->j_wbuf; 317470decc6SDave Kleikamp int bufs; 318470decc6SDave Kleikamp int flags; 319470decc6SDave Kleikamp int err; 32018eba7aaSMingming Cao unsigned long long blocknr; 321e07f7183SJosef Bacik ktime_t start_time; 322e07f7183SJosef Bacik u64 commit_time; 323470decc6SDave Kleikamp char *tagp = NULL; 324470decc6SDave Kleikamp journal_header_t *header; 325470decc6SDave Kleikamp journal_block_tag_t *tag = NULL; 326470decc6SDave Kleikamp int space_left = 0; 327470decc6SDave Kleikamp int first_tag = 0; 328470decc6SDave Kleikamp int tag_flag; 329fb68407bSAneesh Kumar K.V int i, to_free = 0; 330b517bea1SZach Brown int tag_bytes = journal_tag_bytes(journal); 331818d276cSGirish Shilamkar struct buffer_head *cbh = NULL; /* For transactional checksums */ 332818d276cSGirish Shilamkar __u32 crc32_sum = ~0; 33382f04ab4SJens Axboe struct blk_plug plug; 334470decc6SDave Kleikamp 335470decc6SDave Kleikamp /* 336470decc6SDave Kleikamp * First job: lock down the current transaction and wait for 337470decc6SDave Kleikamp * all outstanding updates to complete. 338470decc6SDave Kleikamp */ 339470decc6SDave Kleikamp 340470decc6SDave Kleikamp #ifdef COMMIT_STATS 341470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 342470decc6SDave Kleikamp summarise_journal_usage(journal); 343470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 344470decc6SDave Kleikamp #endif 345470decc6SDave Kleikamp 346f7f4bccbSMingming Cao /* Do we need to erase the effects of a prior jbd2_journal_flush? */ 347f7f4bccbSMingming Cao if (journal->j_flags & JBD2_FLUSHED) { 348470decc6SDave Kleikamp jbd_debug(3, "super block updated\n"); 349f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 350470decc6SDave Kleikamp } else { 351470decc6SDave Kleikamp jbd_debug(3, "superblock not updated\n"); 352470decc6SDave Kleikamp } 353470decc6SDave Kleikamp 354470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction != NULL); 355470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 356470decc6SDave Kleikamp 357470decc6SDave Kleikamp commit_transaction = journal->j_running_transaction; 358470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_state == T_RUNNING); 359470decc6SDave Kleikamp 360879c5e6bSTheodore Ts'o trace_jbd2_start_commit(journal, commit_transaction); 361470decc6SDave Kleikamp jbd_debug(1, "JBD: starting commit of transaction %d\n", 362470decc6SDave Kleikamp commit_transaction->t_tid); 363470decc6SDave Kleikamp 364a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 365470decc6SDave Kleikamp commit_transaction->t_state = T_LOCKED; 366470decc6SDave Kleikamp 367879c5e6bSTheodore Ts'o trace_jbd2_commit_locking(journal, commit_transaction); 368bf699327STheodore Ts'o stats.run.rs_wait = commit_transaction->t_max_wait; 369bf699327STheodore Ts'o stats.run.rs_locked = jiffies; 370bf699327STheodore Ts'o stats.run.rs_running = jbd2_time_diff(commit_transaction->t_start, 371bf699327STheodore Ts'o stats.run.rs_locked); 3728e85fb3fSJohann Lombardi 373470decc6SDave Kleikamp spin_lock(&commit_transaction->t_handle_lock); 374a51dca9cSTheodore Ts'o while (atomic_read(&commit_transaction->t_updates)) { 375470decc6SDave Kleikamp DEFINE_WAIT(wait); 376470decc6SDave Kleikamp 377470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_updates, &wait, 378470decc6SDave Kleikamp TASK_UNINTERRUPTIBLE); 379a51dca9cSTheodore Ts'o if (atomic_read(&commit_transaction->t_updates)) { 380470decc6SDave Kleikamp spin_unlock(&commit_transaction->t_handle_lock); 381a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 382470decc6SDave Kleikamp schedule(); 383a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 384470decc6SDave Kleikamp spin_lock(&commit_transaction->t_handle_lock); 385470decc6SDave Kleikamp } 386470decc6SDave Kleikamp finish_wait(&journal->j_wait_updates, &wait); 387470decc6SDave Kleikamp } 388470decc6SDave Kleikamp spin_unlock(&commit_transaction->t_handle_lock); 389470decc6SDave Kleikamp 390a51dca9cSTheodore Ts'o J_ASSERT (atomic_read(&commit_transaction->t_outstanding_credits) <= 391470decc6SDave Kleikamp journal->j_max_transaction_buffers); 392470decc6SDave Kleikamp 393470decc6SDave Kleikamp /* 394470decc6SDave Kleikamp * First thing we are allowed to do is to discard any remaining 395470decc6SDave Kleikamp * BJ_Reserved buffers. Note, it is _not_ permissible to assume 396470decc6SDave Kleikamp * that there are no such buffers: if a large filesystem 397470decc6SDave Kleikamp * operation like a truncate needs to split itself over multiple 398f7f4bccbSMingming Cao * transactions, then it may try to do a jbd2_journal_restart() while 399470decc6SDave Kleikamp * there are still BJ_Reserved buffers outstanding. These must 400470decc6SDave Kleikamp * be released cleanly from the current transaction. 401470decc6SDave Kleikamp * 402470decc6SDave Kleikamp * In this case, the filesystem must still reserve write access 403470decc6SDave Kleikamp * again before modifying the buffer in the new transaction, but 404470decc6SDave Kleikamp * we do not require it to remember exactly which old buffers it 405470decc6SDave Kleikamp * has reserved. This is consistent with the existing behaviour 406f7f4bccbSMingming Cao * that multiple jbd2_journal_get_write_access() calls to the same 40725985edcSLucas De Marchi * buffer are perfectly permissible. 408470decc6SDave Kleikamp */ 409470decc6SDave Kleikamp while (commit_transaction->t_reserved_list) { 410470decc6SDave Kleikamp jh = commit_transaction->t_reserved_list; 411470decc6SDave Kleikamp JBUFFER_TRACE(jh, "reserved, unused: refile"); 412470decc6SDave Kleikamp /* 413f7f4bccbSMingming Cao * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may 414470decc6SDave Kleikamp * leave undo-committed data. 415470decc6SDave Kleikamp */ 416470decc6SDave Kleikamp if (jh->b_committed_data) { 417470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 418470decc6SDave Kleikamp 419470decc6SDave Kleikamp jbd_lock_bh_state(bh); 420af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 421470decc6SDave Kleikamp jh->b_committed_data = NULL; 422470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 423470decc6SDave Kleikamp } 424f7f4bccbSMingming Cao jbd2_journal_refile_buffer(journal, jh); 425470decc6SDave Kleikamp } 426470decc6SDave Kleikamp 427470decc6SDave Kleikamp /* 428470decc6SDave Kleikamp * Now try to drop any written-back buffers from the journal's 429470decc6SDave Kleikamp * checkpoint lists. We do this *before* commit because it potentially 430470decc6SDave Kleikamp * frees some memory 431470decc6SDave Kleikamp */ 432470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 433f7f4bccbSMingming Cao __jbd2_journal_clean_checkpoint_list(journal); 434470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 435470decc6SDave Kleikamp 436470decc6SDave Kleikamp jbd_debug (3, "JBD: commit phase 1\n"); 437470decc6SDave Kleikamp 438470decc6SDave Kleikamp /* 439470decc6SDave Kleikamp * Switch to a new revoke table. 440470decc6SDave Kleikamp */ 441f7f4bccbSMingming Cao jbd2_journal_switch_revoke_table(journal); 442470decc6SDave Kleikamp 443879c5e6bSTheodore Ts'o trace_jbd2_commit_flushing(journal, commit_transaction); 444bf699327STheodore Ts'o stats.run.rs_flushing = jiffies; 445bf699327STheodore Ts'o stats.run.rs_locked = jbd2_time_diff(stats.run.rs_locked, 446bf699327STheodore Ts'o stats.run.rs_flushing); 4478e85fb3fSJohann Lombardi 448470decc6SDave Kleikamp commit_transaction->t_state = T_FLUSH; 449470decc6SDave Kleikamp journal->j_committing_transaction = commit_transaction; 450470decc6SDave Kleikamp journal->j_running_transaction = NULL; 451e07f7183SJosef Bacik start_time = ktime_get(); 452470decc6SDave Kleikamp commit_transaction->t_log_start = journal->j_head; 453470decc6SDave Kleikamp wake_up(&journal->j_wait_transaction_locked); 454a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 455470decc6SDave Kleikamp 456470decc6SDave Kleikamp jbd_debug (3, "JBD: commit phase 2\n"); 457470decc6SDave Kleikamp 458470decc6SDave Kleikamp /* 459470decc6SDave Kleikamp * Now start flushing things to disk, in the order they appear 460470decc6SDave Kleikamp * on the transaction lists. Data blocks go first. 461470decc6SDave Kleikamp */ 462cd1aac32SAneesh Kumar K.V err = journal_submit_data_buffers(journal, commit_transaction); 463c851ed54SJan Kara if (err) 464c851ed54SJan Kara jbd2_journal_abort(journal, err); 465470decc6SDave Kleikamp 46682f04ab4SJens Axboe blk_start_plug(&plug); 46767c457a8STheodore Ts'o jbd2_journal_write_revoke_records(journal, commit_transaction, 46882f04ab4SJens Axboe WRITE_SYNC); 46982f04ab4SJens Axboe blk_finish_plug(&plug); 470470decc6SDave Kleikamp 471470decc6SDave Kleikamp jbd_debug(3, "JBD: commit phase 2\n"); 472470decc6SDave Kleikamp 473470decc6SDave Kleikamp /* 474470decc6SDave Kleikamp * Way to go: we have now written out all of the data for a 475470decc6SDave Kleikamp * transaction! Now comes the tricky part: we need to write out 476470decc6SDave Kleikamp * metadata. Loop over the transaction's entire buffer list: 477470decc6SDave Kleikamp */ 478a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 479470decc6SDave Kleikamp commit_transaction->t_state = T_COMMIT; 480a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 481470decc6SDave Kleikamp 482879c5e6bSTheodore Ts'o trace_jbd2_commit_logging(journal, commit_transaction); 483bf699327STheodore Ts'o stats.run.rs_logging = jiffies; 484bf699327STheodore Ts'o stats.run.rs_flushing = jbd2_time_diff(stats.run.rs_flushing, 485bf699327STheodore Ts'o stats.run.rs_logging); 486a51dca9cSTheodore Ts'o stats.run.rs_blocks = 487a51dca9cSTheodore Ts'o atomic_read(&commit_transaction->t_outstanding_credits); 488bf699327STheodore Ts'o stats.run.rs_blocks_logged = 0; 4898e85fb3fSJohann Lombardi 4901dfc3220SJosef Bacik J_ASSERT(commit_transaction->t_nr_buffers <= 491a51dca9cSTheodore Ts'o atomic_read(&commit_transaction->t_outstanding_credits)); 4921dfc3220SJosef Bacik 49387c89c23SJan Kara err = 0; 494470decc6SDave Kleikamp descriptor = NULL; 495470decc6SDave Kleikamp bufs = 0; 49682f04ab4SJens Axboe blk_start_plug(&plug); 497470decc6SDave Kleikamp while (commit_transaction->t_buffers) { 498470decc6SDave Kleikamp 499470decc6SDave Kleikamp /* Find the next buffer to be journaled... */ 500470decc6SDave Kleikamp 501470decc6SDave Kleikamp jh = commit_transaction->t_buffers; 502470decc6SDave Kleikamp 503470decc6SDave Kleikamp /* If we're in abort mode, we just un-journal the buffer and 5047ad7445fSHidehiro Kawai release it. */ 505470decc6SDave Kleikamp 506470decc6SDave Kleikamp if (is_journal_aborted(journal)) { 5077ad7445fSHidehiro Kawai clear_buffer_jbddirty(jh2bh(jh)); 508470decc6SDave Kleikamp JBUFFER_TRACE(jh, "journal is aborting: refile"); 509e06c8227SJoel Becker jbd2_buffer_abort_trigger(jh, 510e06c8227SJoel Becker jh->b_frozen_data ? 511e06c8227SJoel Becker jh->b_frozen_triggers : 512e06c8227SJoel Becker jh->b_triggers); 513f7f4bccbSMingming Cao jbd2_journal_refile_buffer(journal, jh); 514470decc6SDave Kleikamp /* If that was the last one, we need to clean up 515470decc6SDave Kleikamp * any descriptor buffers which may have been 516470decc6SDave Kleikamp * already allocated, even if we are now 517470decc6SDave Kleikamp * aborting. */ 518470decc6SDave Kleikamp if (!commit_transaction->t_buffers) 519470decc6SDave Kleikamp goto start_journal_io; 520470decc6SDave Kleikamp continue; 521470decc6SDave Kleikamp } 522470decc6SDave Kleikamp 523470decc6SDave Kleikamp /* Make sure we have a descriptor block in which to 524470decc6SDave Kleikamp record the metadata buffer. */ 525470decc6SDave Kleikamp 526470decc6SDave Kleikamp if (!descriptor) { 527470decc6SDave Kleikamp struct buffer_head *bh; 528470decc6SDave Kleikamp 529470decc6SDave Kleikamp J_ASSERT (bufs == 0); 530470decc6SDave Kleikamp 531470decc6SDave Kleikamp jbd_debug(4, "JBD: get descriptor\n"); 532470decc6SDave Kleikamp 533f7f4bccbSMingming Cao descriptor = jbd2_journal_get_descriptor_buffer(journal); 534470decc6SDave Kleikamp if (!descriptor) { 535a7fa2bafSJan Kara jbd2_journal_abort(journal, -EIO); 536470decc6SDave Kleikamp continue; 537470decc6SDave Kleikamp } 538470decc6SDave Kleikamp 539470decc6SDave Kleikamp bh = jh2bh(descriptor); 540470decc6SDave Kleikamp jbd_debug(4, "JBD: got buffer %llu (%p)\n", 541470decc6SDave Kleikamp (unsigned long long)bh->b_blocknr, bh->b_data); 542470decc6SDave Kleikamp header = (journal_header_t *)&bh->b_data[0]; 543f7f4bccbSMingming Cao header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 544f7f4bccbSMingming Cao header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK); 545470decc6SDave Kleikamp header->h_sequence = cpu_to_be32(commit_transaction->t_tid); 546470decc6SDave Kleikamp 547470decc6SDave Kleikamp tagp = &bh->b_data[sizeof(journal_header_t)]; 548470decc6SDave Kleikamp space_left = bh->b_size - sizeof(journal_header_t); 549470decc6SDave Kleikamp first_tag = 1; 550470decc6SDave Kleikamp set_buffer_jwrite(bh); 551470decc6SDave Kleikamp set_buffer_dirty(bh); 552470decc6SDave Kleikamp wbuf[bufs++] = bh; 553470decc6SDave Kleikamp 554470decc6SDave Kleikamp /* Record it so that we can wait for IO 555470decc6SDave Kleikamp completion later */ 556470decc6SDave Kleikamp BUFFER_TRACE(bh, "ph3: file as descriptor"); 557f7f4bccbSMingming Cao jbd2_journal_file_buffer(descriptor, commit_transaction, 558470decc6SDave Kleikamp BJ_LogCtl); 559470decc6SDave Kleikamp } 560470decc6SDave Kleikamp 561470decc6SDave Kleikamp /* Where is the buffer to be written? */ 562470decc6SDave Kleikamp 563f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 564470decc6SDave Kleikamp /* If the block mapping failed, just abandon the buffer 565470decc6SDave Kleikamp and repeat this loop: we'll fall into the 566470decc6SDave Kleikamp refile-on-abort condition above. */ 567470decc6SDave Kleikamp if (err) { 568a7fa2bafSJan Kara jbd2_journal_abort(journal, err); 569470decc6SDave Kleikamp continue; 570470decc6SDave Kleikamp } 571470decc6SDave Kleikamp 572470decc6SDave Kleikamp /* 573470decc6SDave Kleikamp * start_this_handle() uses t_outstanding_credits to determine 574470decc6SDave Kleikamp * the free space in the log, but this counter is changed 575f7f4bccbSMingming Cao * by jbd2_journal_next_log_block() also. 576470decc6SDave Kleikamp */ 577a51dca9cSTheodore Ts'o atomic_dec(&commit_transaction->t_outstanding_credits); 578470decc6SDave Kleikamp 579470decc6SDave Kleikamp /* Bump b_count to prevent truncate from stumbling over 580470decc6SDave Kleikamp the shadowed buffer! @@@ This can go if we ever get 581470decc6SDave Kleikamp rid of the BJ_IO/BJ_Shadow pairing of buffers. */ 582470decc6SDave Kleikamp atomic_inc(&jh2bh(jh)->b_count); 583470decc6SDave Kleikamp 584470decc6SDave Kleikamp /* Make a temporary IO buffer with which to write it out 585470decc6SDave Kleikamp (this will requeue both the metadata buffer and the 586470decc6SDave Kleikamp temporary IO buffer). new_bh goes on BJ_IO*/ 587470decc6SDave Kleikamp 588470decc6SDave Kleikamp set_bit(BH_JWrite, &jh2bh(jh)->b_state); 589470decc6SDave Kleikamp /* 590f7f4bccbSMingming Cao * akpm: jbd2_journal_write_metadata_buffer() sets 591470decc6SDave Kleikamp * new_bh->b_transaction to commit_transaction. 592470decc6SDave Kleikamp * We need to clean this up before we release new_bh 593470decc6SDave Kleikamp * (which is of type BJ_IO) 594470decc6SDave Kleikamp */ 595470decc6SDave Kleikamp JBUFFER_TRACE(jh, "ph3: write metadata"); 596f7f4bccbSMingming Cao flags = jbd2_journal_write_metadata_buffer(commit_transaction, 597470decc6SDave Kleikamp jh, &new_jh, blocknr); 598e6ec116bSTheodore Ts'o if (flags < 0) { 599e6ec116bSTheodore Ts'o jbd2_journal_abort(journal, flags); 600e6ec116bSTheodore Ts'o continue; 601e6ec116bSTheodore Ts'o } 602470decc6SDave Kleikamp set_bit(BH_JWrite, &jh2bh(new_jh)->b_state); 603470decc6SDave Kleikamp wbuf[bufs++] = jh2bh(new_jh); 604470decc6SDave Kleikamp 605470decc6SDave Kleikamp /* Record the new block's tag in the current descriptor 606470decc6SDave Kleikamp buffer */ 607470decc6SDave Kleikamp 608470decc6SDave Kleikamp tag_flag = 0; 609470decc6SDave Kleikamp if (flags & 1) 610f7f4bccbSMingming Cao tag_flag |= JBD2_FLAG_ESCAPE; 611470decc6SDave Kleikamp if (!first_tag) 612f7f4bccbSMingming Cao tag_flag |= JBD2_FLAG_SAME_UUID; 613470decc6SDave Kleikamp 614470decc6SDave Kleikamp tag = (journal_block_tag_t *) tagp; 615b517bea1SZach Brown write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr); 616470decc6SDave Kleikamp tag->t_flags = cpu_to_be32(tag_flag); 617b517bea1SZach Brown tagp += tag_bytes; 618b517bea1SZach Brown space_left -= tag_bytes; 619470decc6SDave Kleikamp 620470decc6SDave Kleikamp if (first_tag) { 621470decc6SDave Kleikamp memcpy (tagp, journal->j_uuid, 16); 622470decc6SDave Kleikamp tagp += 16; 623470decc6SDave Kleikamp space_left -= 16; 624470decc6SDave Kleikamp first_tag = 0; 625470decc6SDave Kleikamp } 626470decc6SDave Kleikamp 627470decc6SDave Kleikamp /* If there's no more to do, or if the descriptor is full, 628470decc6SDave Kleikamp let the IO rip! */ 629470decc6SDave Kleikamp 630470decc6SDave Kleikamp if (bufs == journal->j_wbufsize || 631470decc6SDave Kleikamp commit_transaction->t_buffers == NULL || 632b517bea1SZach Brown space_left < tag_bytes + 16) { 633470decc6SDave Kleikamp 634470decc6SDave Kleikamp jbd_debug(4, "JBD: Submit %d IOs\n", bufs); 635470decc6SDave Kleikamp 636470decc6SDave Kleikamp /* Write an end-of-descriptor marker before 637470decc6SDave Kleikamp submitting the IOs. "tag" still points to 638470decc6SDave Kleikamp the last tag we set up. */ 639470decc6SDave Kleikamp 640f7f4bccbSMingming Cao tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG); 641470decc6SDave Kleikamp 642470decc6SDave Kleikamp start_journal_io: 643470decc6SDave Kleikamp for (i = 0; i < bufs; i++) { 644470decc6SDave Kleikamp struct buffer_head *bh = wbuf[i]; 645818d276cSGirish Shilamkar /* 646818d276cSGirish Shilamkar * Compute checksum. 647818d276cSGirish Shilamkar */ 648818d276cSGirish Shilamkar if (JBD2_HAS_COMPAT_FEATURE(journal, 649818d276cSGirish Shilamkar JBD2_FEATURE_COMPAT_CHECKSUM)) { 650818d276cSGirish Shilamkar crc32_sum = 651818d276cSGirish Shilamkar jbd2_checksum_data(crc32_sum, bh); 652818d276cSGirish Shilamkar } 653818d276cSGirish Shilamkar 654470decc6SDave Kleikamp lock_buffer(bh); 655470decc6SDave Kleikamp clear_buffer_dirty(bh); 656470decc6SDave Kleikamp set_buffer_uptodate(bh); 657470decc6SDave Kleikamp bh->b_end_io = journal_end_buffer_io_sync; 65882f04ab4SJens Axboe submit_bh(WRITE_SYNC, bh); 659470decc6SDave Kleikamp } 660470decc6SDave Kleikamp cond_resched(); 661bf699327STheodore Ts'o stats.run.rs_blocks_logged += bufs; 662470decc6SDave Kleikamp 663470decc6SDave Kleikamp /* Force a new descriptor to be generated next 664470decc6SDave Kleikamp time round the loop. */ 665470decc6SDave Kleikamp descriptor = NULL; 666470decc6SDave Kleikamp bufs = 0; 667470decc6SDave Kleikamp } 668470decc6SDave Kleikamp } 669470decc6SDave Kleikamp 670c851ed54SJan Kara err = journal_finish_inode_data_buffers(journal, commit_transaction); 671e9e34f4eSHidehiro Kawai if (err) { 672e9e34f4eSHidehiro Kawai printk(KERN_WARNING 673e9e34f4eSHidehiro Kawai "JBD2: Detected IO errors while flushing file data " 67405496769STheodore Ts'o "on %s\n", journal->j_devname); 6755bf5683aSHidehiro Kawai if (journal->j_flags & JBD2_ABORT_ON_SYNCDATA_ERR) 6765bf5683aSHidehiro Kawai jbd2_journal_abort(journal, err); 677e9e34f4eSHidehiro Kawai err = 0; 678e9e34f4eSHidehiro Kawai } 679c851ed54SJan Kara 680818d276cSGirish Shilamkar /* 681818d276cSGirish Shilamkar * If the journal is not located on the file system device, 682818d276cSGirish Shilamkar * then we must flush the file system device before we issue 683818d276cSGirish Shilamkar * the commit record 684818d276cSGirish Shilamkar */ 685*81be12c8SJan Kara if (commit_transaction->t_need_data_flush && 686818d276cSGirish Shilamkar (journal->j_fs_dev != journal->j_dev) && 687818d276cSGirish Shilamkar (journal->j_flags & JBD2_BARRIER)) 688dd3932edSChristoph Hellwig blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL); 689818d276cSGirish Shilamkar 690818d276cSGirish Shilamkar /* Done it all: now write the commit record asynchronously. */ 691818d276cSGirish Shilamkar if (JBD2_HAS_INCOMPAT_FEATURE(journal, 692818d276cSGirish Shilamkar JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { 693818d276cSGirish Shilamkar err = journal_submit_commit_record(journal, commit_transaction, 694470decc6SDave Kleikamp &cbh, crc32_sum); 695470decc6SDave Kleikamp if (err) 696470decc6SDave Kleikamp __jbd2_journal_abort_hard(journal); 697470decc6SDave Kleikamp } 698470decc6SDave Kleikamp 69982f04ab4SJens Axboe blk_finish_plug(&plug); 70082f04ab4SJens Axboe 701470decc6SDave Kleikamp /* Lo and behold: we have just managed to send a transaction to 702470decc6SDave Kleikamp the log. Before we can commit it, wait for the IO so far to 703470decc6SDave Kleikamp complete. Control buffers being written are on the 704470decc6SDave Kleikamp transaction's t_log_list queue, and metadata buffers are on 705470decc6SDave Kleikamp the t_iobuf_list queue. 706470decc6SDave Kleikamp 707470decc6SDave Kleikamp Wait for the buffers in reverse order. That way we are 708470decc6SDave Kleikamp less likely to be woken up until all IOs have completed, and 709470decc6SDave Kleikamp so we incur less scheduling load. 710470decc6SDave Kleikamp */ 711470decc6SDave Kleikamp 712470decc6SDave Kleikamp jbd_debug(3, "JBD: commit phase 3\n"); 713470decc6SDave Kleikamp 714470decc6SDave Kleikamp /* 715470decc6SDave Kleikamp * akpm: these are BJ_IO, and j_list_lock is not needed. 716470decc6SDave Kleikamp * See __journal_try_to_free_buffer. 717470decc6SDave Kleikamp */ 718470decc6SDave Kleikamp wait_for_iobuf: 719470decc6SDave Kleikamp while (commit_transaction->t_iobuf_list != NULL) { 720470decc6SDave Kleikamp struct buffer_head *bh; 721470decc6SDave Kleikamp 722470decc6SDave Kleikamp jh = commit_transaction->t_iobuf_list->b_tprev; 723470decc6SDave Kleikamp bh = jh2bh(jh); 724470decc6SDave Kleikamp if (buffer_locked(bh)) { 725470decc6SDave Kleikamp wait_on_buffer(bh); 726470decc6SDave Kleikamp goto wait_for_iobuf; 727470decc6SDave Kleikamp } 728470decc6SDave Kleikamp if (cond_resched()) 729470decc6SDave Kleikamp goto wait_for_iobuf; 730470decc6SDave Kleikamp 731470decc6SDave Kleikamp if (unlikely(!buffer_uptodate(bh))) 732470decc6SDave Kleikamp err = -EIO; 733470decc6SDave Kleikamp 734470decc6SDave Kleikamp clear_buffer_jwrite(bh); 735470decc6SDave Kleikamp 736470decc6SDave Kleikamp JBUFFER_TRACE(jh, "ph4: unfile after journal write"); 737f7f4bccbSMingming Cao jbd2_journal_unfile_buffer(journal, jh); 738470decc6SDave Kleikamp 739470decc6SDave Kleikamp /* 740470decc6SDave Kleikamp * ->t_iobuf_list should contain only dummy buffer_heads 741f7f4bccbSMingming Cao * which were created by jbd2_journal_write_metadata_buffer(). 742470decc6SDave Kleikamp */ 743470decc6SDave Kleikamp BUFFER_TRACE(bh, "dumping temporary bh"); 744f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 745470decc6SDave Kleikamp __brelse(bh); 746470decc6SDave Kleikamp J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0); 747470decc6SDave Kleikamp free_buffer_head(bh); 748470decc6SDave Kleikamp 749470decc6SDave Kleikamp /* We also have to unlock and free the corresponding 750470decc6SDave Kleikamp shadowed buffer */ 751470decc6SDave Kleikamp jh = commit_transaction->t_shadow_list->b_tprev; 752470decc6SDave Kleikamp bh = jh2bh(jh); 753470decc6SDave Kleikamp clear_bit(BH_JWrite, &bh->b_state); 754470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbddirty(bh)); 755470decc6SDave Kleikamp 756470decc6SDave Kleikamp /* The metadata is now released for reuse, but we need 757470decc6SDave Kleikamp to remember it against this transaction so that when 758470decc6SDave Kleikamp we finally commit, we can do any checkpointing 759470decc6SDave Kleikamp required. */ 760470decc6SDave Kleikamp JBUFFER_TRACE(jh, "file as BJ_Forget"); 761f7f4bccbSMingming Cao jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget); 762229309caSJan Kara /* 763229309caSJan Kara * Wake up any transactions which were waiting for this IO to 764229309caSJan Kara * complete. The barrier must be here so that changes by 765229309caSJan Kara * jbd2_journal_file_buffer() take effect before wake_up_bit() 766229309caSJan Kara * does the waitqueue check. 767229309caSJan Kara */ 768229309caSJan Kara smp_mb(); 769470decc6SDave Kleikamp wake_up_bit(&bh->b_state, BH_Unshadow); 770470decc6SDave Kleikamp JBUFFER_TRACE(jh, "brelse shadowed buffer"); 771470decc6SDave Kleikamp __brelse(bh); 772470decc6SDave Kleikamp } 773470decc6SDave Kleikamp 774470decc6SDave Kleikamp J_ASSERT (commit_transaction->t_shadow_list == NULL); 775470decc6SDave Kleikamp 77687c89c23SJan Kara jbd_debug(3, "JBD: commit phase 4\n"); 777470decc6SDave Kleikamp 778470decc6SDave Kleikamp /* Here we wait for the revoke record and descriptor record buffers */ 779470decc6SDave Kleikamp wait_for_ctlbuf: 780470decc6SDave Kleikamp while (commit_transaction->t_log_list != NULL) { 781470decc6SDave Kleikamp struct buffer_head *bh; 782470decc6SDave Kleikamp 783470decc6SDave Kleikamp jh = commit_transaction->t_log_list->b_tprev; 784470decc6SDave Kleikamp bh = jh2bh(jh); 785470decc6SDave Kleikamp if (buffer_locked(bh)) { 786470decc6SDave Kleikamp wait_on_buffer(bh); 787470decc6SDave Kleikamp goto wait_for_ctlbuf; 788470decc6SDave Kleikamp } 789470decc6SDave Kleikamp if (cond_resched()) 790470decc6SDave Kleikamp goto wait_for_ctlbuf; 791470decc6SDave Kleikamp 792470decc6SDave Kleikamp if (unlikely(!buffer_uptodate(bh))) 793470decc6SDave Kleikamp err = -EIO; 794470decc6SDave Kleikamp 795470decc6SDave Kleikamp BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile"); 796470decc6SDave Kleikamp clear_buffer_jwrite(bh); 797f7f4bccbSMingming Cao jbd2_journal_unfile_buffer(journal, jh); 798f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 799470decc6SDave Kleikamp __brelse(bh); /* One for getblk */ 800470decc6SDave Kleikamp /* AKPM: bforget here */ 801470decc6SDave Kleikamp } 802470decc6SDave Kleikamp 80377e841deSHidehiro Kawai if (err) 80477e841deSHidehiro Kawai jbd2_journal_abort(journal, err); 80577e841deSHidehiro Kawai 80687c89c23SJan Kara jbd_debug(3, "JBD: commit phase 5\n"); 807470decc6SDave Kleikamp 808818d276cSGirish Shilamkar if (!JBD2_HAS_INCOMPAT_FEATURE(journal, 809818d276cSGirish Shilamkar JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { 810818d276cSGirish Shilamkar err = journal_submit_commit_record(journal, commit_transaction, 811818d276cSGirish Shilamkar &cbh, crc32_sum); 812818d276cSGirish Shilamkar if (err) 813818d276cSGirish Shilamkar __jbd2_journal_abort_hard(journal); 814818d276cSGirish Shilamkar } 8156cba611eSZhang Huan if (cbh) 816fd98496fSTheodore Ts'o err = journal_wait_on_commit_record(journal, cbh); 817f73bee49SJan Kara if (JBD2_HAS_INCOMPAT_FEATURE(journal, 818f73bee49SJan Kara JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT) && 819f73bee49SJan Kara journal->j_flags & JBD2_BARRIER) { 820dd3932edSChristoph Hellwig blkdev_issue_flush(journal->j_dev, GFP_KERNEL, NULL); 821f73bee49SJan Kara } 822470decc6SDave Kleikamp 823470decc6SDave Kleikamp if (err) 824a7fa2bafSJan Kara jbd2_journal_abort(journal, err); 825470decc6SDave Kleikamp 826470decc6SDave Kleikamp /* End of a transaction! Finally, we can do checkpoint 827470decc6SDave Kleikamp processing: any buffers committed as a result of this 828470decc6SDave Kleikamp transaction can be removed from any checkpoint list it was on 829470decc6SDave Kleikamp before. */ 830470decc6SDave Kleikamp 83187c89c23SJan Kara jbd_debug(3, "JBD: commit phase 6\n"); 832470decc6SDave Kleikamp 833c851ed54SJan Kara J_ASSERT(list_empty(&commit_transaction->t_inode_list)); 834470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_buffers == NULL); 835470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_checkpoint_list == NULL); 836470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_iobuf_list == NULL); 837470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_shadow_list == NULL); 838470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_log_list == NULL); 839470decc6SDave Kleikamp 840470decc6SDave Kleikamp restart_loop: 841470decc6SDave Kleikamp /* 842470decc6SDave Kleikamp * As there are other places (journal_unmap_buffer()) adding buffers 843470decc6SDave Kleikamp * to this list we have to be careful and hold the j_list_lock. 844470decc6SDave Kleikamp */ 845470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 846470decc6SDave Kleikamp while (commit_transaction->t_forget) { 847470decc6SDave Kleikamp transaction_t *cp_transaction; 848470decc6SDave Kleikamp struct buffer_head *bh; 849470decc6SDave Kleikamp 850470decc6SDave Kleikamp jh = commit_transaction->t_forget; 851470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 852470decc6SDave Kleikamp bh = jh2bh(jh); 853470decc6SDave Kleikamp jbd_lock_bh_state(bh); 85423e2af35Sdingdinghua J_ASSERT_JH(jh, jh->b_transaction == commit_transaction); 855470decc6SDave Kleikamp 856470decc6SDave Kleikamp /* 857470decc6SDave Kleikamp * If there is undo-protected committed data against 858470decc6SDave Kleikamp * this buffer, then we can remove it now. If it is a 859470decc6SDave Kleikamp * buffer needing such protection, the old frozen_data 860470decc6SDave Kleikamp * field now points to a committed version of the 861470decc6SDave Kleikamp * buffer, so rotate that field to the new committed 862470decc6SDave Kleikamp * data. 863470decc6SDave Kleikamp * 864470decc6SDave Kleikamp * Otherwise, we can just throw away the frozen data now. 865e06c8227SJoel Becker * 866e06c8227SJoel Becker * We also know that the frozen data has already fired 867e06c8227SJoel Becker * its triggers if they exist, so we can clear that too. 868470decc6SDave Kleikamp */ 869470decc6SDave Kleikamp if (jh->b_committed_data) { 870af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 871470decc6SDave Kleikamp jh->b_committed_data = NULL; 872470decc6SDave Kleikamp if (jh->b_frozen_data) { 873470decc6SDave Kleikamp jh->b_committed_data = jh->b_frozen_data; 874470decc6SDave Kleikamp jh->b_frozen_data = NULL; 875e06c8227SJoel Becker jh->b_frozen_triggers = NULL; 876470decc6SDave Kleikamp } 877470decc6SDave Kleikamp } else if (jh->b_frozen_data) { 878af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 879470decc6SDave Kleikamp jh->b_frozen_data = NULL; 880e06c8227SJoel Becker jh->b_frozen_triggers = NULL; 881470decc6SDave Kleikamp } 882470decc6SDave Kleikamp 883470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 884470decc6SDave Kleikamp cp_transaction = jh->b_cp_transaction; 885470decc6SDave Kleikamp if (cp_transaction) { 886470decc6SDave Kleikamp JBUFFER_TRACE(jh, "remove from old cp transaction"); 8878e85fb3fSJohann Lombardi cp_transaction->t_chp_stats.cs_dropped++; 888f7f4bccbSMingming Cao __jbd2_journal_remove_checkpoint(jh); 889470decc6SDave Kleikamp } 890470decc6SDave Kleikamp 891470decc6SDave Kleikamp /* Only re-checkpoint the buffer_head if it is marked 892470decc6SDave Kleikamp * dirty. If the buffer was added to the BJ_Forget list 893f7f4bccbSMingming Cao * by jbd2_journal_forget, it may no longer be dirty and 894470decc6SDave Kleikamp * there's no point in keeping a checkpoint record for 895470decc6SDave Kleikamp * it. */ 896470decc6SDave Kleikamp 897470decc6SDave Kleikamp /* A buffer which has been freed while still being 898470decc6SDave Kleikamp * journaled by a previous transaction may end up still 899470decc6SDave Kleikamp * being dirty here, but we want to avoid writing back 900ba869023Sdingdinghua * that buffer in the future after the "add to orphan" 901ba869023Sdingdinghua * operation been committed, That's not only a performance 902ba869023Sdingdinghua * gain, it also stops aliasing problems if the buffer is 903ba869023Sdingdinghua * left behind for writeback and gets reallocated for another 904470decc6SDave Kleikamp * use in a different page. */ 905ba869023Sdingdinghua if (buffer_freed(bh) && !jh->b_next_transaction) { 906470decc6SDave Kleikamp clear_buffer_freed(bh); 907470decc6SDave Kleikamp clear_buffer_jbddirty(bh); 908470decc6SDave Kleikamp } 909470decc6SDave Kleikamp 910470decc6SDave Kleikamp if (buffer_jbddirty(bh)) { 911470decc6SDave Kleikamp JBUFFER_TRACE(jh, "add to new checkpointing trans"); 912f7f4bccbSMingming Cao __jbd2_journal_insert_checkpoint(jh, commit_transaction); 9137ad7445fSHidehiro Kawai if (is_journal_aborted(journal)) 9147ad7445fSHidehiro Kawai clear_buffer_jbddirty(bh); 915470decc6SDave Kleikamp JBUFFER_TRACE(jh, "refile for checkpoint writeback"); 916f7f4bccbSMingming Cao __jbd2_journal_refile_buffer(jh); 917470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 918470decc6SDave Kleikamp } else { 919470decc6SDave Kleikamp J_ASSERT_BH(bh, !buffer_dirty(bh)); 920470decc6SDave Kleikamp /* The buffer on BJ_Forget list and not jbddirty means 921470decc6SDave Kleikamp * it has been freed by this transaction and hence it 922470decc6SDave Kleikamp * could not have been reallocated until this 923470decc6SDave Kleikamp * transaction has committed. *BUT* it could be 924470decc6SDave Kleikamp * reallocated once we have written all the data to 925470decc6SDave Kleikamp * disk and before we process the buffer on BJ_Forget 926470decc6SDave Kleikamp * list. */ 927470decc6SDave Kleikamp JBUFFER_TRACE(jh, "refile or unfile freed buffer"); 928f7f4bccbSMingming Cao __jbd2_journal_refile_buffer(jh); 929470decc6SDave Kleikamp if (!jh->b_transaction) { 930470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 931470decc6SDave Kleikamp /* needs a brelse */ 932f7f4bccbSMingming Cao jbd2_journal_remove_journal_head(bh); 933470decc6SDave Kleikamp release_buffer_page(bh); 934470decc6SDave Kleikamp } else 935470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 936470decc6SDave Kleikamp } 937470decc6SDave Kleikamp cond_resched_lock(&journal->j_list_lock); 938470decc6SDave Kleikamp } 939470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 940470decc6SDave Kleikamp /* 941f5a7a6b0SJan Kara * This is a bit sleazy. We use j_list_lock to protect transition 942f5a7a6b0SJan Kara * of a transaction into T_FINISHED state and calling 943f5a7a6b0SJan Kara * __jbd2_journal_drop_transaction(). Otherwise we could race with 944f5a7a6b0SJan Kara * other checkpointing code processing the transaction... 945470decc6SDave Kleikamp */ 946a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 947470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 948470decc6SDave Kleikamp /* 949470decc6SDave Kleikamp * Now recheck if some buffers did not get attached to the transaction 950470decc6SDave Kleikamp * while the lock was dropped... 951470decc6SDave Kleikamp */ 952470decc6SDave Kleikamp if (commit_transaction->t_forget) { 953470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 954a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 955470decc6SDave Kleikamp goto restart_loop; 956470decc6SDave Kleikamp } 957470decc6SDave Kleikamp 958470decc6SDave Kleikamp /* Done with this transaction! */ 959470decc6SDave Kleikamp 96087c89c23SJan Kara jbd_debug(3, "JBD: commit phase 7\n"); 961470decc6SDave Kleikamp 962470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_state == T_COMMIT); 963470decc6SDave Kleikamp 9648e85fb3fSJohann Lombardi commit_transaction->t_start = jiffies; 965bf699327STheodore Ts'o stats.run.rs_logging = jbd2_time_diff(stats.run.rs_logging, 9668e85fb3fSJohann Lombardi commit_transaction->t_start); 9678e85fb3fSJohann Lombardi 9688e85fb3fSJohann Lombardi /* 969bf699327STheodore Ts'o * File the transaction statistics 9708e85fb3fSJohann Lombardi */ 9718e85fb3fSJohann Lombardi stats.ts_tid = commit_transaction->t_tid; 9728dd42046STheodore Ts'o stats.run.rs_handle_count = 9738dd42046STheodore Ts'o atomic_read(&commit_transaction->t_handle_count); 974bf699327STheodore Ts'o trace_jbd2_run_stats(journal->j_fs_dev->bd_dev, 975bf699327STheodore Ts'o commit_transaction->t_tid, &stats.run); 9768e85fb3fSJohann Lombardi 9778e85fb3fSJohann Lombardi /* 9788e85fb3fSJohann Lombardi * Calculate overall stats 9798e85fb3fSJohann Lombardi */ 980bf699327STheodore Ts'o spin_lock(&journal->j_history_lock); 9818e85fb3fSJohann Lombardi journal->j_stats.ts_tid++; 982bf699327STheodore Ts'o journal->j_stats.run.rs_wait += stats.run.rs_wait; 983bf699327STheodore Ts'o journal->j_stats.run.rs_running += stats.run.rs_running; 984bf699327STheodore Ts'o journal->j_stats.run.rs_locked += stats.run.rs_locked; 985bf699327STheodore Ts'o journal->j_stats.run.rs_flushing += stats.run.rs_flushing; 986bf699327STheodore Ts'o journal->j_stats.run.rs_logging += stats.run.rs_logging; 987bf699327STheodore Ts'o journal->j_stats.run.rs_handle_count += stats.run.rs_handle_count; 988bf699327STheodore Ts'o journal->j_stats.run.rs_blocks += stats.run.rs_blocks; 989bf699327STheodore Ts'o journal->j_stats.run.rs_blocks_logged += stats.run.rs_blocks_logged; 9908e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 9918e85fb3fSJohann Lombardi 992470decc6SDave Kleikamp commit_transaction->t_state = T_FINISHED; 993470decc6SDave Kleikamp J_ASSERT(commit_transaction == journal->j_committing_transaction); 994470decc6SDave Kleikamp journal->j_commit_sequence = commit_transaction->t_tid; 995470decc6SDave Kleikamp journal->j_committing_transaction = NULL; 996e07f7183SJosef Bacik commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); 997470decc6SDave Kleikamp 998e07f7183SJosef Bacik /* 999e07f7183SJosef Bacik * weight the commit time higher than the average time so we don't 1000e07f7183SJosef Bacik * react too strongly to vast changes in the commit time 1001e07f7183SJosef Bacik */ 1002e07f7183SJosef Bacik if (likely(journal->j_average_commit_time)) 1003e07f7183SJosef Bacik journal->j_average_commit_time = (commit_time + 1004e07f7183SJosef Bacik journal->j_average_commit_time*3) / 4; 1005e07f7183SJosef Bacik else 1006e07f7183SJosef Bacik journal->j_average_commit_time = commit_time; 1007a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 10086c20ec85STheodore Ts'o 1009f89b7795SJan Kara if (commit_transaction->t_checkpoint_list == NULL && 1010f89b7795SJan Kara commit_transaction->t_checkpoint_io_list == NULL) { 1011f7f4bccbSMingming Cao __jbd2_journal_drop_transaction(journal, commit_transaction); 1012fb68407bSAneesh Kumar K.V to_free = 1; 1013470decc6SDave Kleikamp } else { 1014470decc6SDave Kleikamp if (journal->j_checkpoint_transactions == NULL) { 1015470decc6SDave Kleikamp journal->j_checkpoint_transactions = commit_transaction; 1016470decc6SDave Kleikamp commit_transaction->t_cpnext = commit_transaction; 1017470decc6SDave Kleikamp commit_transaction->t_cpprev = commit_transaction; 1018470decc6SDave Kleikamp } else { 1019470decc6SDave Kleikamp commit_transaction->t_cpnext = 1020470decc6SDave Kleikamp journal->j_checkpoint_transactions; 1021470decc6SDave Kleikamp commit_transaction->t_cpprev = 1022470decc6SDave Kleikamp commit_transaction->t_cpnext->t_cpprev; 1023470decc6SDave Kleikamp commit_transaction->t_cpnext->t_cpprev = 1024470decc6SDave Kleikamp commit_transaction; 1025470decc6SDave Kleikamp commit_transaction->t_cpprev->t_cpnext = 1026470decc6SDave Kleikamp commit_transaction; 1027470decc6SDave Kleikamp } 1028470decc6SDave Kleikamp } 1029470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1030470decc6SDave Kleikamp 1031fb68407bSAneesh Kumar K.V if (journal->j_commit_callback) 1032fb68407bSAneesh Kumar K.V journal->j_commit_callback(journal, commit_transaction); 1033fb68407bSAneesh Kumar K.V 1034879c5e6bSTheodore Ts'o trace_jbd2_end_commit(journal, commit_transaction); 1035470decc6SDave Kleikamp jbd_debug(1, "JBD: commit %d complete, head %d\n", 1036470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_tail_sequence); 1037fb68407bSAneesh Kumar K.V if (to_free) 1038fb68407bSAneesh Kumar K.V kfree(commit_transaction); 1039470decc6SDave Kleikamp 1040470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 1041470decc6SDave Kleikamp } 1042