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); 222cc3e1beaSTheodore Ts'o commit_transaction->t_flushed_data_blocks = 1; 22339e3ac25SBrian King clear_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 22439e3ac25SBrian King smp_mb__after_clear_bit(); 225c851ed54SJan Kara wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); 226c851ed54SJan Kara } 227c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 228c851ed54SJan Kara return ret; 229c851ed54SJan Kara } 230c851ed54SJan Kara 231c851ed54SJan Kara /* 232c851ed54SJan Kara * Wait for data submitted for writeout, refile inodes to proper 233c851ed54SJan Kara * transaction if needed. 234c851ed54SJan Kara * 235c851ed54SJan Kara */ 236c851ed54SJan Kara static int journal_finish_inode_data_buffers(journal_t *journal, 237c851ed54SJan Kara transaction_t *commit_transaction) 238c851ed54SJan Kara { 239c851ed54SJan Kara struct jbd2_inode *jinode, *next_i; 240c851ed54SJan Kara int err, ret = 0; 241c851ed54SJan Kara 242cd1aac32SAneesh Kumar K.V /* For locking, see the comment in journal_submit_data_buffers() */ 243c851ed54SJan Kara spin_lock(&journal->j_list_lock); 244c851ed54SJan Kara list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { 24539e3ac25SBrian King set_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 246c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 247c851ed54SJan Kara err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping); 248e9e34f4eSHidehiro Kawai if (err) { 249e9e34f4eSHidehiro Kawai /* 250e9e34f4eSHidehiro Kawai * Because AS_EIO is cleared by 25194004ed7SChristoph Hellwig * filemap_fdatawait_range(), set it again so 252e9e34f4eSHidehiro Kawai * that user process can get -EIO from fsync(). 253e9e34f4eSHidehiro Kawai */ 254e9e34f4eSHidehiro Kawai set_bit(AS_EIO, 255e9e34f4eSHidehiro Kawai &jinode->i_vfs_inode->i_mapping->flags); 256e9e34f4eSHidehiro Kawai 257c851ed54SJan Kara if (!ret) 258c851ed54SJan Kara ret = err; 259e9e34f4eSHidehiro Kawai } 260c851ed54SJan Kara spin_lock(&journal->j_list_lock); 26139e3ac25SBrian King clear_bit(__JI_COMMIT_RUNNING, &jinode->i_flags); 26239e3ac25SBrian King smp_mb__after_clear_bit(); 263c851ed54SJan Kara wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); 264c851ed54SJan Kara } 265c851ed54SJan Kara 266c851ed54SJan Kara /* Now refile inode to proper lists */ 267c851ed54SJan Kara list_for_each_entry_safe(jinode, next_i, 268c851ed54SJan Kara &commit_transaction->t_inode_list, i_list) { 269c851ed54SJan Kara list_del(&jinode->i_list); 270c851ed54SJan Kara if (jinode->i_next_transaction) { 271c851ed54SJan Kara jinode->i_transaction = jinode->i_next_transaction; 272c851ed54SJan Kara jinode->i_next_transaction = NULL; 273c851ed54SJan Kara list_add(&jinode->i_list, 274c851ed54SJan Kara &jinode->i_transaction->t_inode_list); 275c851ed54SJan Kara } else { 276c851ed54SJan Kara jinode->i_transaction = NULL; 277c851ed54SJan Kara } 278c851ed54SJan Kara } 279c851ed54SJan Kara spin_unlock(&journal->j_list_lock); 280c851ed54SJan Kara 281c851ed54SJan Kara return ret; 282c851ed54SJan Kara } 283c851ed54SJan Kara 284818d276cSGirish Shilamkar static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh) 285818d276cSGirish Shilamkar { 286818d276cSGirish Shilamkar struct page *page = bh->b_page; 287818d276cSGirish Shilamkar char *addr; 288818d276cSGirish Shilamkar __u32 checksum; 289818d276cSGirish Shilamkar 290818d276cSGirish Shilamkar addr = kmap_atomic(page, KM_USER0); 291818d276cSGirish Shilamkar checksum = crc32_be(crc32_sum, 292818d276cSGirish Shilamkar (void *)(addr + offset_in_page(bh->b_data)), bh->b_size); 293818d276cSGirish Shilamkar kunmap_atomic(addr, KM_USER0); 294818d276cSGirish Shilamkar 295818d276cSGirish Shilamkar return checksum; 296818d276cSGirish Shilamkar } 297818d276cSGirish Shilamkar 298818d276cSGirish Shilamkar static void write_tag_block(int tag_bytes, journal_block_tag_t *tag, 29918eba7aaSMingming Cao unsigned long long block) 300b517bea1SZach Brown { 301b517bea1SZach Brown tag->t_blocknr = cpu_to_be32(block & (u32)~0); 302cd02ff0bSMingming Cao if (tag_bytes > JBD2_TAG_SIZE32) 303b517bea1SZach Brown tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1); 304b517bea1SZach Brown } 305b517bea1SZach Brown 306470decc6SDave Kleikamp /* 307f7f4bccbSMingming Cao * jbd2_journal_commit_transaction 308470decc6SDave Kleikamp * 309470decc6SDave Kleikamp * The primary function for committing a transaction to the log. This 310470decc6SDave Kleikamp * function is called by the journal thread to begin a complete commit. 311470decc6SDave Kleikamp */ 312f7f4bccbSMingming Cao void jbd2_journal_commit_transaction(journal_t *journal) 313470decc6SDave Kleikamp { 3148e85fb3fSJohann Lombardi struct transaction_stats_s stats; 315470decc6SDave Kleikamp transaction_t *commit_transaction; 316470decc6SDave Kleikamp struct journal_head *jh, *new_jh, *descriptor; 317470decc6SDave Kleikamp struct buffer_head **wbuf = journal->j_wbuf; 318470decc6SDave Kleikamp int bufs; 319470decc6SDave Kleikamp int flags; 320470decc6SDave Kleikamp int err; 32118eba7aaSMingming Cao unsigned long long blocknr; 322e07f7183SJosef Bacik ktime_t start_time; 323e07f7183SJosef Bacik u64 commit_time; 324470decc6SDave Kleikamp char *tagp = NULL; 325470decc6SDave Kleikamp journal_header_t *header; 326470decc6SDave Kleikamp journal_block_tag_t *tag = NULL; 327470decc6SDave Kleikamp int space_left = 0; 328470decc6SDave Kleikamp int first_tag = 0; 329470decc6SDave Kleikamp int tag_flag; 330fb68407bSAneesh Kumar K.V int i, to_free = 0; 331b517bea1SZach Brown int tag_bytes = journal_tag_bytes(journal); 332818d276cSGirish Shilamkar struct buffer_head *cbh = NULL; /* For transactional checksums */ 333818d276cSGirish Shilamkar __u32 crc32_sum = ~0; 33482f04ab4SJens Axboe struct blk_plug plug; 335470decc6SDave Kleikamp 336470decc6SDave Kleikamp /* 337470decc6SDave Kleikamp * First job: lock down the current transaction and wait for 338470decc6SDave Kleikamp * all outstanding updates to complete. 339470decc6SDave Kleikamp */ 340470decc6SDave Kleikamp 341470decc6SDave Kleikamp #ifdef COMMIT_STATS 342470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 343470decc6SDave Kleikamp summarise_journal_usage(journal); 344470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 345470decc6SDave Kleikamp #endif 346470decc6SDave Kleikamp 347f7f4bccbSMingming Cao /* Do we need to erase the effects of a prior jbd2_journal_flush? */ 348f7f4bccbSMingming Cao if (journal->j_flags & JBD2_FLUSHED) { 349470decc6SDave Kleikamp jbd_debug(3, "super block updated\n"); 350f7f4bccbSMingming Cao jbd2_journal_update_superblock(journal, 1); 351470decc6SDave Kleikamp } else { 352470decc6SDave Kleikamp jbd_debug(3, "superblock not updated\n"); 353470decc6SDave Kleikamp } 354470decc6SDave Kleikamp 355470decc6SDave Kleikamp J_ASSERT(journal->j_running_transaction != NULL); 356470decc6SDave Kleikamp J_ASSERT(journal->j_committing_transaction == NULL); 357470decc6SDave Kleikamp 358470decc6SDave Kleikamp commit_transaction = journal->j_running_transaction; 359470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_state == T_RUNNING); 360470decc6SDave Kleikamp 361879c5e6bSTheodore Ts'o trace_jbd2_start_commit(journal, commit_transaction); 362470decc6SDave Kleikamp jbd_debug(1, "JBD: starting commit of transaction %d\n", 363470decc6SDave Kleikamp commit_transaction->t_tid); 364470decc6SDave Kleikamp 365a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 366470decc6SDave Kleikamp commit_transaction->t_state = T_LOCKED; 367470decc6SDave Kleikamp 368879c5e6bSTheodore Ts'o trace_jbd2_commit_locking(journal, commit_transaction); 369bf699327STheodore Ts'o stats.run.rs_wait = commit_transaction->t_max_wait; 370bf699327STheodore Ts'o stats.run.rs_locked = jiffies; 371bf699327STheodore Ts'o stats.run.rs_running = jbd2_time_diff(commit_transaction->t_start, 372bf699327STheodore Ts'o stats.run.rs_locked); 3738e85fb3fSJohann Lombardi 374470decc6SDave Kleikamp spin_lock(&commit_transaction->t_handle_lock); 375a51dca9cSTheodore Ts'o while (atomic_read(&commit_transaction->t_updates)) { 376470decc6SDave Kleikamp DEFINE_WAIT(wait); 377470decc6SDave Kleikamp 378470decc6SDave Kleikamp prepare_to_wait(&journal->j_wait_updates, &wait, 379470decc6SDave Kleikamp TASK_UNINTERRUPTIBLE); 380a51dca9cSTheodore Ts'o if (atomic_read(&commit_transaction->t_updates)) { 381470decc6SDave Kleikamp spin_unlock(&commit_transaction->t_handle_lock); 382a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 383470decc6SDave Kleikamp schedule(); 384a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 385470decc6SDave Kleikamp spin_lock(&commit_transaction->t_handle_lock); 386470decc6SDave Kleikamp } 387470decc6SDave Kleikamp finish_wait(&journal->j_wait_updates, &wait); 388470decc6SDave Kleikamp } 389470decc6SDave Kleikamp spin_unlock(&commit_transaction->t_handle_lock); 390470decc6SDave Kleikamp 391a51dca9cSTheodore Ts'o J_ASSERT (atomic_read(&commit_transaction->t_outstanding_credits) <= 392470decc6SDave Kleikamp journal->j_max_transaction_buffers); 393470decc6SDave Kleikamp 394470decc6SDave Kleikamp /* 395470decc6SDave Kleikamp * First thing we are allowed to do is to discard any remaining 396470decc6SDave Kleikamp * BJ_Reserved buffers. Note, it is _not_ permissible to assume 397470decc6SDave Kleikamp * that there are no such buffers: if a large filesystem 398470decc6SDave Kleikamp * operation like a truncate needs to split itself over multiple 399f7f4bccbSMingming Cao * transactions, then it may try to do a jbd2_journal_restart() while 400470decc6SDave Kleikamp * there are still BJ_Reserved buffers outstanding. These must 401470decc6SDave Kleikamp * be released cleanly from the current transaction. 402470decc6SDave Kleikamp * 403470decc6SDave Kleikamp * In this case, the filesystem must still reserve write access 404470decc6SDave Kleikamp * again before modifying the buffer in the new transaction, but 405470decc6SDave Kleikamp * we do not require it to remember exactly which old buffers it 406470decc6SDave Kleikamp * has reserved. This is consistent with the existing behaviour 407f7f4bccbSMingming Cao * that multiple jbd2_journal_get_write_access() calls to the same 40825985edcSLucas De Marchi * buffer are perfectly permissible. 409470decc6SDave Kleikamp */ 410470decc6SDave Kleikamp while (commit_transaction->t_reserved_list) { 411470decc6SDave Kleikamp jh = commit_transaction->t_reserved_list; 412470decc6SDave Kleikamp JBUFFER_TRACE(jh, "reserved, unused: refile"); 413470decc6SDave Kleikamp /* 414f7f4bccbSMingming Cao * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may 415470decc6SDave Kleikamp * leave undo-committed data. 416470decc6SDave Kleikamp */ 417470decc6SDave Kleikamp if (jh->b_committed_data) { 418470decc6SDave Kleikamp struct buffer_head *bh = jh2bh(jh); 419470decc6SDave Kleikamp 420470decc6SDave Kleikamp jbd_lock_bh_state(bh); 421af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 422470decc6SDave Kleikamp jh->b_committed_data = NULL; 423470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 424470decc6SDave Kleikamp } 425f7f4bccbSMingming Cao jbd2_journal_refile_buffer(journal, jh); 426470decc6SDave Kleikamp } 427470decc6SDave Kleikamp 428470decc6SDave Kleikamp /* 429470decc6SDave Kleikamp * Now try to drop any written-back buffers from the journal's 430470decc6SDave Kleikamp * checkpoint lists. We do this *before* commit because it potentially 431470decc6SDave Kleikamp * frees some memory 432470decc6SDave Kleikamp */ 433470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 434f7f4bccbSMingming Cao __jbd2_journal_clean_checkpoint_list(journal); 435470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 436470decc6SDave Kleikamp 437470decc6SDave Kleikamp jbd_debug (3, "JBD: commit phase 1\n"); 438470decc6SDave Kleikamp 439470decc6SDave Kleikamp /* 440470decc6SDave Kleikamp * Switch to a new revoke table. 441470decc6SDave Kleikamp */ 442f7f4bccbSMingming Cao jbd2_journal_switch_revoke_table(journal); 443470decc6SDave Kleikamp 444879c5e6bSTheodore Ts'o trace_jbd2_commit_flushing(journal, commit_transaction); 445bf699327STheodore Ts'o stats.run.rs_flushing = jiffies; 446bf699327STheodore Ts'o stats.run.rs_locked = jbd2_time_diff(stats.run.rs_locked, 447bf699327STheodore Ts'o stats.run.rs_flushing); 4488e85fb3fSJohann Lombardi 449470decc6SDave Kleikamp commit_transaction->t_state = T_FLUSH; 450470decc6SDave Kleikamp journal->j_committing_transaction = commit_transaction; 451470decc6SDave Kleikamp journal->j_running_transaction = NULL; 452e07f7183SJosef Bacik start_time = ktime_get(); 453470decc6SDave Kleikamp commit_transaction->t_log_start = journal->j_head; 454470decc6SDave Kleikamp wake_up(&journal->j_wait_transaction_locked); 455a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 456470decc6SDave Kleikamp 457470decc6SDave Kleikamp jbd_debug (3, "JBD: commit phase 2\n"); 458470decc6SDave Kleikamp 459470decc6SDave Kleikamp /* 460470decc6SDave Kleikamp * Now start flushing things to disk, in the order they appear 461470decc6SDave Kleikamp * on the transaction lists. Data blocks go first. 462470decc6SDave Kleikamp */ 463cd1aac32SAneesh Kumar K.V err = journal_submit_data_buffers(journal, commit_transaction); 464c851ed54SJan Kara if (err) 465c851ed54SJan Kara jbd2_journal_abort(journal, err); 466470decc6SDave Kleikamp 46782f04ab4SJens Axboe blk_start_plug(&plug); 46867c457a8STheodore Ts'o jbd2_journal_write_revoke_records(journal, commit_transaction, 46982f04ab4SJens Axboe WRITE_SYNC); 47082f04ab4SJens Axboe blk_finish_plug(&plug); 471470decc6SDave Kleikamp 472470decc6SDave Kleikamp jbd_debug(3, "JBD: commit phase 2\n"); 473470decc6SDave Kleikamp 474470decc6SDave Kleikamp /* 475470decc6SDave Kleikamp * Way to go: we have now written out all of the data for a 476470decc6SDave Kleikamp * transaction! Now comes the tricky part: we need to write out 477470decc6SDave Kleikamp * metadata. Loop over the transaction's entire buffer list: 478470decc6SDave Kleikamp */ 479a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 480470decc6SDave Kleikamp commit_transaction->t_state = T_COMMIT; 481a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 482470decc6SDave Kleikamp 483879c5e6bSTheodore Ts'o trace_jbd2_commit_logging(journal, commit_transaction); 484bf699327STheodore Ts'o stats.run.rs_logging = jiffies; 485bf699327STheodore Ts'o stats.run.rs_flushing = jbd2_time_diff(stats.run.rs_flushing, 486bf699327STheodore Ts'o stats.run.rs_logging); 487a51dca9cSTheodore Ts'o stats.run.rs_blocks = 488a51dca9cSTheodore Ts'o atomic_read(&commit_transaction->t_outstanding_credits); 489bf699327STheodore Ts'o stats.run.rs_blocks_logged = 0; 4908e85fb3fSJohann Lombardi 4911dfc3220SJosef Bacik J_ASSERT(commit_transaction->t_nr_buffers <= 492a51dca9cSTheodore Ts'o atomic_read(&commit_transaction->t_outstanding_credits)); 4931dfc3220SJosef Bacik 49487c89c23SJan Kara err = 0; 495470decc6SDave Kleikamp descriptor = NULL; 496470decc6SDave Kleikamp bufs = 0; 49782f04ab4SJens Axboe blk_start_plug(&plug); 498470decc6SDave Kleikamp while (commit_transaction->t_buffers) { 499470decc6SDave Kleikamp 500470decc6SDave Kleikamp /* Find the next buffer to be journaled... */ 501470decc6SDave Kleikamp 502470decc6SDave Kleikamp jh = commit_transaction->t_buffers; 503470decc6SDave Kleikamp 504470decc6SDave Kleikamp /* If we're in abort mode, we just un-journal the buffer and 5057ad7445fSHidehiro Kawai release it. */ 506470decc6SDave Kleikamp 507470decc6SDave Kleikamp if (is_journal_aborted(journal)) { 5087ad7445fSHidehiro Kawai clear_buffer_jbddirty(jh2bh(jh)); 509470decc6SDave Kleikamp JBUFFER_TRACE(jh, "journal is aborting: refile"); 510e06c8227SJoel Becker jbd2_buffer_abort_trigger(jh, 511e06c8227SJoel Becker jh->b_frozen_data ? 512e06c8227SJoel Becker jh->b_frozen_triggers : 513e06c8227SJoel Becker jh->b_triggers); 514f7f4bccbSMingming Cao jbd2_journal_refile_buffer(journal, jh); 515470decc6SDave Kleikamp /* If that was the last one, we need to clean up 516470decc6SDave Kleikamp * any descriptor buffers which may have been 517470decc6SDave Kleikamp * already allocated, even if we are now 518470decc6SDave Kleikamp * aborting. */ 519470decc6SDave Kleikamp if (!commit_transaction->t_buffers) 520470decc6SDave Kleikamp goto start_journal_io; 521470decc6SDave Kleikamp continue; 522470decc6SDave Kleikamp } 523470decc6SDave Kleikamp 524470decc6SDave Kleikamp /* Make sure we have a descriptor block in which to 525470decc6SDave Kleikamp record the metadata buffer. */ 526470decc6SDave Kleikamp 527470decc6SDave Kleikamp if (!descriptor) { 528470decc6SDave Kleikamp struct buffer_head *bh; 529470decc6SDave Kleikamp 530470decc6SDave Kleikamp J_ASSERT (bufs == 0); 531470decc6SDave Kleikamp 532470decc6SDave Kleikamp jbd_debug(4, "JBD: get descriptor\n"); 533470decc6SDave Kleikamp 534f7f4bccbSMingming Cao descriptor = jbd2_journal_get_descriptor_buffer(journal); 535470decc6SDave Kleikamp if (!descriptor) { 536a7fa2bafSJan Kara jbd2_journal_abort(journal, -EIO); 537470decc6SDave Kleikamp continue; 538470decc6SDave Kleikamp } 539470decc6SDave Kleikamp 540470decc6SDave Kleikamp bh = jh2bh(descriptor); 541470decc6SDave Kleikamp jbd_debug(4, "JBD: got buffer %llu (%p)\n", 542470decc6SDave Kleikamp (unsigned long long)bh->b_blocknr, bh->b_data); 543470decc6SDave Kleikamp header = (journal_header_t *)&bh->b_data[0]; 544f7f4bccbSMingming Cao header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 545f7f4bccbSMingming Cao header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK); 546470decc6SDave Kleikamp header->h_sequence = cpu_to_be32(commit_transaction->t_tid); 547470decc6SDave Kleikamp 548470decc6SDave Kleikamp tagp = &bh->b_data[sizeof(journal_header_t)]; 549470decc6SDave Kleikamp space_left = bh->b_size - sizeof(journal_header_t); 550470decc6SDave Kleikamp first_tag = 1; 551470decc6SDave Kleikamp set_buffer_jwrite(bh); 552470decc6SDave Kleikamp set_buffer_dirty(bh); 553470decc6SDave Kleikamp wbuf[bufs++] = bh; 554470decc6SDave Kleikamp 555470decc6SDave Kleikamp /* Record it so that we can wait for IO 556470decc6SDave Kleikamp completion later */ 557470decc6SDave Kleikamp BUFFER_TRACE(bh, "ph3: file as descriptor"); 558f7f4bccbSMingming Cao jbd2_journal_file_buffer(descriptor, commit_transaction, 559470decc6SDave Kleikamp BJ_LogCtl); 560470decc6SDave Kleikamp } 561470decc6SDave Kleikamp 562470decc6SDave Kleikamp /* Where is the buffer to be written? */ 563470decc6SDave Kleikamp 564f7f4bccbSMingming Cao err = jbd2_journal_next_log_block(journal, &blocknr); 565470decc6SDave Kleikamp /* If the block mapping failed, just abandon the buffer 566470decc6SDave Kleikamp and repeat this loop: we'll fall into the 567470decc6SDave Kleikamp refile-on-abort condition above. */ 568470decc6SDave Kleikamp if (err) { 569a7fa2bafSJan Kara jbd2_journal_abort(journal, err); 570470decc6SDave Kleikamp continue; 571470decc6SDave Kleikamp } 572470decc6SDave Kleikamp 573470decc6SDave Kleikamp /* 574470decc6SDave Kleikamp * start_this_handle() uses t_outstanding_credits to determine 575470decc6SDave Kleikamp * the free space in the log, but this counter is changed 576f7f4bccbSMingming Cao * by jbd2_journal_next_log_block() also. 577470decc6SDave Kleikamp */ 578a51dca9cSTheodore Ts'o atomic_dec(&commit_transaction->t_outstanding_credits); 579470decc6SDave Kleikamp 580470decc6SDave Kleikamp /* Bump b_count to prevent truncate from stumbling over 581470decc6SDave Kleikamp the shadowed buffer! @@@ This can go if we ever get 582470decc6SDave Kleikamp rid of the BJ_IO/BJ_Shadow pairing of buffers. */ 583470decc6SDave Kleikamp atomic_inc(&jh2bh(jh)->b_count); 584470decc6SDave Kleikamp 585470decc6SDave Kleikamp /* Make a temporary IO buffer with which to write it out 586470decc6SDave Kleikamp (this will requeue both the metadata buffer and the 587470decc6SDave Kleikamp temporary IO buffer). new_bh goes on BJ_IO*/ 588470decc6SDave Kleikamp 589470decc6SDave Kleikamp set_bit(BH_JWrite, &jh2bh(jh)->b_state); 590470decc6SDave Kleikamp /* 591f7f4bccbSMingming Cao * akpm: jbd2_journal_write_metadata_buffer() sets 592470decc6SDave Kleikamp * new_bh->b_transaction to commit_transaction. 593470decc6SDave Kleikamp * We need to clean this up before we release new_bh 594470decc6SDave Kleikamp * (which is of type BJ_IO) 595470decc6SDave Kleikamp */ 596470decc6SDave Kleikamp JBUFFER_TRACE(jh, "ph3: write metadata"); 597f7f4bccbSMingming Cao flags = jbd2_journal_write_metadata_buffer(commit_transaction, 598470decc6SDave Kleikamp jh, &new_jh, blocknr); 599e6ec116bSTheodore Ts'o if (flags < 0) { 600e6ec116bSTheodore Ts'o jbd2_journal_abort(journal, flags); 601e6ec116bSTheodore Ts'o continue; 602e6ec116bSTheodore Ts'o } 603470decc6SDave Kleikamp set_bit(BH_JWrite, &jh2bh(new_jh)->b_state); 604470decc6SDave Kleikamp wbuf[bufs++] = jh2bh(new_jh); 605470decc6SDave Kleikamp 606470decc6SDave Kleikamp /* Record the new block's tag in the current descriptor 607470decc6SDave Kleikamp buffer */ 608470decc6SDave Kleikamp 609470decc6SDave Kleikamp tag_flag = 0; 610470decc6SDave Kleikamp if (flags & 1) 611f7f4bccbSMingming Cao tag_flag |= JBD2_FLAG_ESCAPE; 612470decc6SDave Kleikamp if (!first_tag) 613f7f4bccbSMingming Cao tag_flag |= JBD2_FLAG_SAME_UUID; 614470decc6SDave Kleikamp 615470decc6SDave Kleikamp tag = (journal_block_tag_t *) tagp; 616b517bea1SZach Brown write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr); 617470decc6SDave Kleikamp tag->t_flags = cpu_to_be32(tag_flag); 618b517bea1SZach Brown tagp += tag_bytes; 619b517bea1SZach Brown space_left -= tag_bytes; 620470decc6SDave Kleikamp 621470decc6SDave Kleikamp if (first_tag) { 622470decc6SDave Kleikamp memcpy (tagp, journal->j_uuid, 16); 623470decc6SDave Kleikamp tagp += 16; 624470decc6SDave Kleikamp space_left -= 16; 625470decc6SDave Kleikamp first_tag = 0; 626470decc6SDave Kleikamp } 627470decc6SDave Kleikamp 628470decc6SDave Kleikamp /* If there's no more to do, or if the descriptor is full, 629470decc6SDave Kleikamp let the IO rip! */ 630470decc6SDave Kleikamp 631470decc6SDave Kleikamp if (bufs == journal->j_wbufsize || 632470decc6SDave Kleikamp commit_transaction->t_buffers == NULL || 633b517bea1SZach Brown space_left < tag_bytes + 16) { 634470decc6SDave Kleikamp 635470decc6SDave Kleikamp jbd_debug(4, "JBD: Submit %d IOs\n", bufs); 636470decc6SDave Kleikamp 637470decc6SDave Kleikamp /* Write an end-of-descriptor marker before 638470decc6SDave Kleikamp submitting the IOs. "tag" still points to 639470decc6SDave Kleikamp the last tag we set up. */ 640470decc6SDave Kleikamp 641f7f4bccbSMingming Cao tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG); 642470decc6SDave Kleikamp 643470decc6SDave Kleikamp start_journal_io: 644470decc6SDave Kleikamp for (i = 0; i < bufs; i++) { 645470decc6SDave Kleikamp struct buffer_head *bh = wbuf[i]; 646818d276cSGirish Shilamkar /* 647818d276cSGirish Shilamkar * Compute checksum. 648818d276cSGirish Shilamkar */ 649818d276cSGirish Shilamkar if (JBD2_HAS_COMPAT_FEATURE(journal, 650818d276cSGirish Shilamkar JBD2_FEATURE_COMPAT_CHECKSUM)) { 651818d276cSGirish Shilamkar crc32_sum = 652818d276cSGirish Shilamkar jbd2_checksum_data(crc32_sum, bh); 653818d276cSGirish Shilamkar } 654818d276cSGirish Shilamkar 655470decc6SDave Kleikamp lock_buffer(bh); 656470decc6SDave Kleikamp clear_buffer_dirty(bh); 657470decc6SDave Kleikamp set_buffer_uptodate(bh); 658470decc6SDave Kleikamp bh->b_end_io = journal_end_buffer_io_sync; 65982f04ab4SJens Axboe submit_bh(WRITE_SYNC, bh); 660470decc6SDave Kleikamp } 661470decc6SDave Kleikamp cond_resched(); 662bf699327STheodore Ts'o stats.run.rs_blocks_logged += bufs; 663470decc6SDave Kleikamp 664470decc6SDave Kleikamp /* Force a new descriptor to be generated next 665470decc6SDave Kleikamp time round the loop. */ 666470decc6SDave Kleikamp descriptor = NULL; 667470decc6SDave Kleikamp bufs = 0; 668470decc6SDave Kleikamp } 669470decc6SDave Kleikamp } 670470decc6SDave Kleikamp 671c851ed54SJan Kara err = journal_finish_inode_data_buffers(journal, commit_transaction); 672e9e34f4eSHidehiro Kawai if (err) { 673e9e34f4eSHidehiro Kawai printk(KERN_WARNING 674e9e34f4eSHidehiro Kawai "JBD2: Detected IO errors while flushing file data " 67505496769STheodore Ts'o "on %s\n", journal->j_devname); 6765bf5683aSHidehiro Kawai if (journal->j_flags & JBD2_ABORT_ON_SYNCDATA_ERR) 6775bf5683aSHidehiro Kawai jbd2_journal_abort(journal, err); 678e9e34f4eSHidehiro Kawai err = 0; 679e9e34f4eSHidehiro Kawai } 680c851ed54SJan Kara 681818d276cSGirish Shilamkar /* 682818d276cSGirish Shilamkar * If the journal is not located on the file system device, 683818d276cSGirish Shilamkar * then we must flush the file system device before we issue 684818d276cSGirish Shilamkar * the commit record 685818d276cSGirish Shilamkar */ 686818d276cSGirish Shilamkar if (commit_transaction->t_flushed_data_blocks && 687818d276cSGirish Shilamkar (journal->j_fs_dev != journal->j_dev) && 688818d276cSGirish Shilamkar (journal->j_flags & JBD2_BARRIER)) 689dd3932edSChristoph Hellwig blkdev_issue_flush(journal->j_fs_dev, GFP_KERNEL, NULL); 690818d276cSGirish Shilamkar 691818d276cSGirish Shilamkar /* Done it all: now write the commit record asynchronously. */ 692818d276cSGirish Shilamkar if (JBD2_HAS_INCOMPAT_FEATURE(journal, 693818d276cSGirish Shilamkar JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { 694818d276cSGirish Shilamkar err = journal_submit_commit_record(journal, commit_transaction, 695470decc6SDave Kleikamp &cbh, crc32_sum); 696470decc6SDave Kleikamp if (err) 697470decc6SDave Kleikamp __jbd2_journal_abort_hard(journal); 698470decc6SDave Kleikamp } 699470decc6SDave Kleikamp 70082f04ab4SJens Axboe blk_finish_plug(&plug); 70182f04ab4SJens Axboe 702470decc6SDave Kleikamp /* Lo and behold: we have just managed to send a transaction to 703470decc6SDave Kleikamp the log. Before we can commit it, wait for the IO so far to 704470decc6SDave Kleikamp complete. Control buffers being written are on the 705470decc6SDave Kleikamp transaction's t_log_list queue, and metadata buffers are on 706470decc6SDave Kleikamp the t_iobuf_list queue. 707470decc6SDave Kleikamp 708470decc6SDave Kleikamp Wait for the buffers in reverse order. That way we are 709470decc6SDave Kleikamp less likely to be woken up until all IOs have completed, and 710470decc6SDave Kleikamp so we incur less scheduling load. 711470decc6SDave Kleikamp */ 712470decc6SDave Kleikamp 713470decc6SDave Kleikamp jbd_debug(3, "JBD: commit phase 3\n"); 714470decc6SDave Kleikamp 715470decc6SDave Kleikamp /* 716470decc6SDave Kleikamp * akpm: these are BJ_IO, and j_list_lock is not needed. 717470decc6SDave Kleikamp * See __journal_try_to_free_buffer. 718470decc6SDave Kleikamp */ 719470decc6SDave Kleikamp wait_for_iobuf: 720470decc6SDave Kleikamp while (commit_transaction->t_iobuf_list != NULL) { 721470decc6SDave Kleikamp struct buffer_head *bh; 722470decc6SDave Kleikamp 723470decc6SDave Kleikamp jh = commit_transaction->t_iobuf_list->b_tprev; 724470decc6SDave Kleikamp bh = jh2bh(jh); 725470decc6SDave Kleikamp if (buffer_locked(bh)) { 726470decc6SDave Kleikamp wait_on_buffer(bh); 727470decc6SDave Kleikamp goto wait_for_iobuf; 728470decc6SDave Kleikamp } 729470decc6SDave Kleikamp if (cond_resched()) 730470decc6SDave Kleikamp goto wait_for_iobuf; 731470decc6SDave Kleikamp 732470decc6SDave Kleikamp if (unlikely(!buffer_uptodate(bh))) 733470decc6SDave Kleikamp err = -EIO; 734470decc6SDave Kleikamp 735470decc6SDave Kleikamp clear_buffer_jwrite(bh); 736470decc6SDave Kleikamp 737470decc6SDave Kleikamp JBUFFER_TRACE(jh, "ph4: unfile after journal write"); 738f7f4bccbSMingming Cao jbd2_journal_unfile_buffer(journal, jh); 739470decc6SDave Kleikamp 740470decc6SDave Kleikamp /* 741470decc6SDave Kleikamp * ->t_iobuf_list should contain only dummy buffer_heads 742f7f4bccbSMingming Cao * which were created by jbd2_journal_write_metadata_buffer(). 743470decc6SDave Kleikamp */ 744470decc6SDave Kleikamp BUFFER_TRACE(bh, "dumping temporary bh"); 745f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 746470decc6SDave Kleikamp __brelse(bh); 747470decc6SDave Kleikamp J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0); 748470decc6SDave Kleikamp free_buffer_head(bh); 749470decc6SDave Kleikamp 750470decc6SDave Kleikamp /* We also have to unlock and free the corresponding 751470decc6SDave Kleikamp shadowed buffer */ 752470decc6SDave Kleikamp jh = commit_transaction->t_shadow_list->b_tprev; 753470decc6SDave Kleikamp bh = jh2bh(jh); 754470decc6SDave Kleikamp clear_bit(BH_JWrite, &bh->b_state); 755470decc6SDave Kleikamp J_ASSERT_BH(bh, buffer_jbddirty(bh)); 756470decc6SDave Kleikamp 757470decc6SDave Kleikamp /* The metadata is now released for reuse, but we need 758470decc6SDave Kleikamp to remember it against this transaction so that when 759470decc6SDave Kleikamp we finally commit, we can do any checkpointing 760470decc6SDave Kleikamp required. */ 761470decc6SDave Kleikamp JBUFFER_TRACE(jh, "file as BJ_Forget"); 762f7f4bccbSMingming Cao jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget); 763*229309caSJan Kara /* 764*229309caSJan Kara * Wake up any transactions which were waiting for this IO to 765*229309caSJan Kara * complete. The barrier must be here so that changes by 766*229309caSJan Kara * jbd2_journal_file_buffer() take effect before wake_up_bit() 767*229309caSJan Kara * does the waitqueue check. 768*229309caSJan Kara */ 769*229309caSJan Kara smp_mb(); 770470decc6SDave Kleikamp wake_up_bit(&bh->b_state, BH_Unshadow); 771470decc6SDave Kleikamp JBUFFER_TRACE(jh, "brelse shadowed buffer"); 772470decc6SDave Kleikamp __brelse(bh); 773470decc6SDave Kleikamp } 774470decc6SDave Kleikamp 775470decc6SDave Kleikamp J_ASSERT (commit_transaction->t_shadow_list == NULL); 776470decc6SDave Kleikamp 77787c89c23SJan Kara jbd_debug(3, "JBD: commit phase 4\n"); 778470decc6SDave Kleikamp 779470decc6SDave Kleikamp /* Here we wait for the revoke record and descriptor record buffers */ 780470decc6SDave Kleikamp wait_for_ctlbuf: 781470decc6SDave Kleikamp while (commit_transaction->t_log_list != NULL) { 782470decc6SDave Kleikamp struct buffer_head *bh; 783470decc6SDave Kleikamp 784470decc6SDave Kleikamp jh = commit_transaction->t_log_list->b_tprev; 785470decc6SDave Kleikamp bh = jh2bh(jh); 786470decc6SDave Kleikamp if (buffer_locked(bh)) { 787470decc6SDave Kleikamp wait_on_buffer(bh); 788470decc6SDave Kleikamp goto wait_for_ctlbuf; 789470decc6SDave Kleikamp } 790470decc6SDave Kleikamp if (cond_resched()) 791470decc6SDave Kleikamp goto wait_for_ctlbuf; 792470decc6SDave Kleikamp 793470decc6SDave Kleikamp if (unlikely(!buffer_uptodate(bh))) 794470decc6SDave Kleikamp err = -EIO; 795470decc6SDave Kleikamp 796470decc6SDave Kleikamp BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile"); 797470decc6SDave Kleikamp clear_buffer_jwrite(bh); 798f7f4bccbSMingming Cao jbd2_journal_unfile_buffer(journal, jh); 799f7f4bccbSMingming Cao jbd2_journal_put_journal_head(jh); 800470decc6SDave Kleikamp __brelse(bh); /* One for getblk */ 801470decc6SDave Kleikamp /* AKPM: bforget here */ 802470decc6SDave Kleikamp } 803470decc6SDave Kleikamp 80477e841deSHidehiro Kawai if (err) 80577e841deSHidehiro Kawai jbd2_journal_abort(journal, err); 80677e841deSHidehiro Kawai 80787c89c23SJan Kara jbd_debug(3, "JBD: commit phase 5\n"); 808470decc6SDave Kleikamp 809818d276cSGirish Shilamkar if (!JBD2_HAS_INCOMPAT_FEATURE(journal, 810818d276cSGirish Shilamkar JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { 811818d276cSGirish Shilamkar err = journal_submit_commit_record(journal, commit_transaction, 812818d276cSGirish Shilamkar &cbh, crc32_sum); 813818d276cSGirish Shilamkar if (err) 814818d276cSGirish Shilamkar __jbd2_journal_abort_hard(journal); 815818d276cSGirish Shilamkar } 8166cba611eSZhang Huan if (cbh) 817fd98496fSTheodore Ts'o err = journal_wait_on_commit_record(journal, cbh); 818f73bee49SJan Kara if (JBD2_HAS_INCOMPAT_FEATURE(journal, 819f73bee49SJan Kara JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT) && 820f73bee49SJan Kara journal->j_flags & JBD2_BARRIER) { 821dd3932edSChristoph Hellwig blkdev_issue_flush(journal->j_dev, GFP_KERNEL, NULL); 822f73bee49SJan Kara } 823470decc6SDave Kleikamp 824470decc6SDave Kleikamp if (err) 825a7fa2bafSJan Kara jbd2_journal_abort(journal, err); 826470decc6SDave Kleikamp 827470decc6SDave Kleikamp /* End of a transaction! Finally, we can do checkpoint 828470decc6SDave Kleikamp processing: any buffers committed as a result of this 829470decc6SDave Kleikamp transaction can be removed from any checkpoint list it was on 830470decc6SDave Kleikamp before. */ 831470decc6SDave Kleikamp 83287c89c23SJan Kara jbd_debug(3, "JBD: commit phase 6\n"); 833470decc6SDave Kleikamp 834c851ed54SJan Kara J_ASSERT(list_empty(&commit_transaction->t_inode_list)); 835470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_buffers == NULL); 836470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_checkpoint_list == NULL); 837470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_iobuf_list == NULL); 838470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_shadow_list == NULL); 839470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_log_list == NULL); 840470decc6SDave Kleikamp 841470decc6SDave Kleikamp restart_loop: 842470decc6SDave Kleikamp /* 843470decc6SDave Kleikamp * As there are other places (journal_unmap_buffer()) adding buffers 844470decc6SDave Kleikamp * to this list we have to be careful and hold the j_list_lock. 845470decc6SDave Kleikamp */ 846470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 847470decc6SDave Kleikamp while (commit_transaction->t_forget) { 848470decc6SDave Kleikamp transaction_t *cp_transaction; 849470decc6SDave Kleikamp struct buffer_head *bh; 850470decc6SDave Kleikamp 851470decc6SDave Kleikamp jh = commit_transaction->t_forget; 852470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 853470decc6SDave Kleikamp bh = jh2bh(jh); 854470decc6SDave Kleikamp jbd_lock_bh_state(bh); 85523e2af35Sdingdinghua J_ASSERT_JH(jh, jh->b_transaction == commit_transaction); 856470decc6SDave Kleikamp 857470decc6SDave Kleikamp /* 858470decc6SDave Kleikamp * If there is undo-protected committed data against 859470decc6SDave Kleikamp * this buffer, then we can remove it now. If it is a 860470decc6SDave Kleikamp * buffer needing such protection, the old frozen_data 861470decc6SDave Kleikamp * field now points to a committed version of the 862470decc6SDave Kleikamp * buffer, so rotate that field to the new committed 863470decc6SDave Kleikamp * data. 864470decc6SDave Kleikamp * 865470decc6SDave Kleikamp * Otherwise, we can just throw away the frozen data now. 866e06c8227SJoel Becker * 867e06c8227SJoel Becker * We also know that the frozen data has already fired 868e06c8227SJoel Becker * its triggers if they exist, so we can clear that too. 869470decc6SDave Kleikamp */ 870470decc6SDave Kleikamp if (jh->b_committed_data) { 871af1e76d6SMingming Cao jbd2_free(jh->b_committed_data, bh->b_size); 872470decc6SDave Kleikamp jh->b_committed_data = NULL; 873470decc6SDave Kleikamp if (jh->b_frozen_data) { 874470decc6SDave Kleikamp jh->b_committed_data = jh->b_frozen_data; 875470decc6SDave Kleikamp jh->b_frozen_data = NULL; 876e06c8227SJoel Becker jh->b_frozen_triggers = NULL; 877470decc6SDave Kleikamp } 878470decc6SDave Kleikamp } else if (jh->b_frozen_data) { 879af1e76d6SMingming Cao jbd2_free(jh->b_frozen_data, bh->b_size); 880470decc6SDave Kleikamp jh->b_frozen_data = NULL; 881e06c8227SJoel Becker jh->b_frozen_triggers = NULL; 882470decc6SDave Kleikamp } 883470decc6SDave Kleikamp 884470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 885470decc6SDave Kleikamp cp_transaction = jh->b_cp_transaction; 886470decc6SDave Kleikamp if (cp_transaction) { 887470decc6SDave Kleikamp JBUFFER_TRACE(jh, "remove from old cp transaction"); 8888e85fb3fSJohann Lombardi cp_transaction->t_chp_stats.cs_dropped++; 889f7f4bccbSMingming Cao __jbd2_journal_remove_checkpoint(jh); 890470decc6SDave Kleikamp } 891470decc6SDave Kleikamp 892470decc6SDave Kleikamp /* Only re-checkpoint the buffer_head if it is marked 893470decc6SDave Kleikamp * dirty. If the buffer was added to the BJ_Forget list 894f7f4bccbSMingming Cao * by jbd2_journal_forget, it may no longer be dirty and 895470decc6SDave Kleikamp * there's no point in keeping a checkpoint record for 896470decc6SDave Kleikamp * it. */ 897470decc6SDave Kleikamp 898470decc6SDave Kleikamp /* A buffer which has been freed while still being 899470decc6SDave Kleikamp * journaled by a previous transaction may end up still 900470decc6SDave Kleikamp * being dirty here, but we want to avoid writing back 901ba869023Sdingdinghua * that buffer in the future after the "add to orphan" 902ba869023Sdingdinghua * operation been committed, That's not only a performance 903ba869023Sdingdinghua * gain, it also stops aliasing problems if the buffer is 904ba869023Sdingdinghua * left behind for writeback and gets reallocated for another 905470decc6SDave Kleikamp * use in a different page. */ 906ba869023Sdingdinghua if (buffer_freed(bh) && !jh->b_next_transaction) { 907470decc6SDave Kleikamp clear_buffer_freed(bh); 908470decc6SDave Kleikamp clear_buffer_jbddirty(bh); 909470decc6SDave Kleikamp } 910470decc6SDave Kleikamp 911470decc6SDave Kleikamp if (buffer_jbddirty(bh)) { 912470decc6SDave Kleikamp JBUFFER_TRACE(jh, "add to new checkpointing trans"); 913f7f4bccbSMingming Cao __jbd2_journal_insert_checkpoint(jh, commit_transaction); 9147ad7445fSHidehiro Kawai if (is_journal_aborted(journal)) 9157ad7445fSHidehiro Kawai clear_buffer_jbddirty(bh); 916470decc6SDave Kleikamp JBUFFER_TRACE(jh, "refile for checkpoint writeback"); 917f7f4bccbSMingming Cao __jbd2_journal_refile_buffer(jh); 918470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 919470decc6SDave Kleikamp } else { 920470decc6SDave Kleikamp J_ASSERT_BH(bh, !buffer_dirty(bh)); 921470decc6SDave Kleikamp /* The buffer on BJ_Forget list and not jbddirty means 922470decc6SDave Kleikamp * it has been freed by this transaction and hence it 923470decc6SDave Kleikamp * could not have been reallocated until this 924470decc6SDave Kleikamp * transaction has committed. *BUT* it could be 925470decc6SDave Kleikamp * reallocated once we have written all the data to 926470decc6SDave Kleikamp * disk and before we process the buffer on BJ_Forget 927470decc6SDave Kleikamp * list. */ 928470decc6SDave Kleikamp JBUFFER_TRACE(jh, "refile or unfile freed buffer"); 929f7f4bccbSMingming Cao __jbd2_journal_refile_buffer(jh); 930470decc6SDave Kleikamp if (!jh->b_transaction) { 931470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 932470decc6SDave Kleikamp /* needs a brelse */ 933f7f4bccbSMingming Cao jbd2_journal_remove_journal_head(bh); 934470decc6SDave Kleikamp release_buffer_page(bh); 935470decc6SDave Kleikamp } else 936470decc6SDave Kleikamp jbd_unlock_bh_state(bh); 937470decc6SDave Kleikamp } 938470decc6SDave Kleikamp cond_resched_lock(&journal->j_list_lock); 939470decc6SDave Kleikamp } 940470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 941470decc6SDave Kleikamp /* 942f5a7a6b0SJan Kara * This is a bit sleazy. We use j_list_lock to protect transition 943f5a7a6b0SJan Kara * of a transaction into T_FINISHED state and calling 944f5a7a6b0SJan Kara * __jbd2_journal_drop_transaction(). Otherwise we could race with 945f5a7a6b0SJan Kara * other checkpointing code processing the transaction... 946470decc6SDave Kleikamp */ 947a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 948470decc6SDave Kleikamp spin_lock(&journal->j_list_lock); 949470decc6SDave Kleikamp /* 950470decc6SDave Kleikamp * Now recheck if some buffers did not get attached to the transaction 951470decc6SDave Kleikamp * while the lock was dropped... 952470decc6SDave Kleikamp */ 953470decc6SDave Kleikamp if (commit_transaction->t_forget) { 954470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 955a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 956470decc6SDave Kleikamp goto restart_loop; 957470decc6SDave Kleikamp } 958470decc6SDave Kleikamp 959470decc6SDave Kleikamp /* Done with this transaction! */ 960470decc6SDave Kleikamp 96187c89c23SJan Kara jbd_debug(3, "JBD: commit phase 7\n"); 962470decc6SDave Kleikamp 963470decc6SDave Kleikamp J_ASSERT(commit_transaction->t_state == T_COMMIT); 964470decc6SDave Kleikamp 9658e85fb3fSJohann Lombardi commit_transaction->t_start = jiffies; 966bf699327STheodore Ts'o stats.run.rs_logging = jbd2_time_diff(stats.run.rs_logging, 9678e85fb3fSJohann Lombardi commit_transaction->t_start); 9688e85fb3fSJohann Lombardi 9698e85fb3fSJohann Lombardi /* 970bf699327STheodore Ts'o * File the transaction statistics 9718e85fb3fSJohann Lombardi */ 9728e85fb3fSJohann Lombardi stats.ts_tid = commit_transaction->t_tid; 9738dd42046STheodore Ts'o stats.run.rs_handle_count = 9748dd42046STheodore Ts'o atomic_read(&commit_transaction->t_handle_count); 975bf699327STheodore Ts'o trace_jbd2_run_stats(journal->j_fs_dev->bd_dev, 976bf699327STheodore Ts'o commit_transaction->t_tid, &stats.run); 9778e85fb3fSJohann Lombardi 9788e85fb3fSJohann Lombardi /* 9798e85fb3fSJohann Lombardi * Calculate overall stats 9808e85fb3fSJohann Lombardi */ 981bf699327STheodore Ts'o spin_lock(&journal->j_history_lock); 9828e85fb3fSJohann Lombardi journal->j_stats.ts_tid++; 983bf699327STheodore Ts'o journal->j_stats.run.rs_wait += stats.run.rs_wait; 984bf699327STheodore Ts'o journal->j_stats.run.rs_running += stats.run.rs_running; 985bf699327STheodore Ts'o journal->j_stats.run.rs_locked += stats.run.rs_locked; 986bf699327STheodore Ts'o journal->j_stats.run.rs_flushing += stats.run.rs_flushing; 987bf699327STheodore Ts'o journal->j_stats.run.rs_logging += stats.run.rs_logging; 988bf699327STheodore Ts'o journal->j_stats.run.rs_handle_count += stats.run.rs_handle_count; 989bf699327STheodore Ts'o journal->j_stats.run.rs_blocks += stats.run.rs_blocks; 990bf699327STheodore Ts'o journal->j_stats.run.rs_blocks_logged += stats.run.rs_blocks_logged; 9918e85fb3fSJohann Lombardi spin_unlock(&journal->j_history_lock); 9928e85fb3fSJohann Lombardi 993470decc6SDave Kleikamp commit_transaction->t_state = T_FINISHED; 994470decc6SDave Kleikamp J_ASSERT(commit_transaction == journal->j_committing_transaction); 995470decc6SDave Kleikamp journal->j_commit_sequence = commit_transaction->t_tid; 996470decc6SDave Kleikamp journal->j_committing_transaction = NULL; 997e07f7183SJosef Bacik commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time)); 998470decc6SDave Kleikamp 999e07f7183SJosef Bacik /* 1000e07f7183SJosef Bacik * weight the commit time higher than the average time so we don't 1001e07f7183SJosef Bacik * react too strongly to vast changes in the commit time 1002e07f7183SJosef Bacik */ 1003e07f7183SJosef Bacik if (likely(journal->j_average_commit_time)) 1004e07f7183SJosef Bacik journal->j_average_commit_time = (commit_time + 1005e07f7183SJosef Bacik journal->j_average_commit_time*3) / 4; 1006e07f7183SJosef Bacik else 1007e07f7183SJosef Bacik journal->j_average_commit_time = commit_time; 1008a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 10096c20ec85STheodore Ts'o 1010f89b7795SJan Kara if (commit_transaction->t_checkpoint_list == NULL && 1011f89b7795SJan Kara commit_transaction->t_checkpoint_io_list == NULL) { 1012f7f4bccbSMingming Cao __jbd2_journal_drop_transaction(journal, commit_transaction); 1013fb68407bSAneesh Kumar K.V to_free = 1; 1014470decc6SDave Kleikamp } else { 1015470decc6SDave Kleikamp if (journal->j_checkpoint_transactions == NULL) { 1016470decc6SDave Kleikamp journal->j_checkpoint_transactions = commit_transaction; 1017470decc6SDave Kleikamp commit_transaction->t_cpnext = commit_transaction; 1018470decc6SDave Kleikamp commit_transaction->t_cpprev = commit_transaction; 1019470decc6SDave Kleikamp } else { 1020470decc6SDave Kleikamp commit_transaction->t_cpnext = 1021470decc6SDave Kleikamp journal->j_checkpoint_transactions; 1022470decc6SDave Kleikamp commit_transaction->t_cpprev = 1023470decc6SDave Kleikamp commit_transaction->t_cpnext->t_cpprev; 1024470decc6SDave Kleikamp commit_transaction->t_cpnext->t_cpprev = 1025470decc6SDave Kleikamp commit_transaction; 1026470decc6SDave Kleikamp commit_transaction->t_cpprev->t_cpnext = 1027470decc6SDave Kleikamp commit_transaction; 1028470decc6SDave Kleikamp } 1029470decc6SDave Kleikamp } 1030470decc6SDave Kleikamp spin_unlock(&journal->j_list_lock); 1031470decc6SDave Kleikamp 1032fb68407bSAneesh Kumar K.V if (journal->j_commit_callback) 1033fb68407bSAneesh Kumar K.V journal->j_commit_callback(journal, commit_transaction); 1034fb68407bSAneesh Kumar K.V 1035879c5e6bSTheodore Ts'o trace_jbd2_end_commit(journal, commit_transaction); 1036470decc6SDave Kleikamp jbd_debug(1, "JBD: commit %d complete, head %d\n", 1037470decc6SDave Kleikamp journal->j_commit_sequence, journal->j_tail_sequence); 1038fb68407bSAneesh Kumar K.V if (to_free) 1039fb68407bSAneesh Kumar K.V kfree(commit_transaction); 1040470decc6SDave Kleikamp 1041470decc6SDave Kleikamp wake_up(&journal->j_wait_done_commit); 1042470decc6SDave Kleikamp } 1043