Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 2 | * linux/fs/jbd2/commit.c |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 3 | * |
| 4 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 |
| 5 | * |
| 6 | * Copyright 1998 Red Hat corp --- All Rights Reserved |
| 7 | * |
| 8 | * This file is part of the Linux kernel and is made available under |
| 9 | * the terms of the GNU General Public License, version 2, or at your |
| 10 | * option, any later version, incorporated herein by reference. |
| 11 | * |
| 12 | * Journal commit routines for the generic filesystem journaling code; |
| 13 | * part of the ext2fs journaling system. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/time.h> |
| 17 | #include <linux/fs.h> |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 18 | #include <linux/jbd2.h> |
Theodore Ts'o | ede86cc | 2008-10-05 20:50:06 -0400 | [diff] [blame] | 19 | #include <linux/marker.h> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/pagemap.h> |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 24 | #include <linux/jiffies.h> |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 25 | #include <linux/crc32.h> |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 26 | #include <linux/writeback.h> |
| 27 | #include <linux/backing-dev.h> |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Default IO end handler for temporary BJ_IO buffer_heads. |
| 31 | */ |
| 32 | static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate) |
| 33 | { |
| 34 | BUFFER_TRACE(bh, ""); |
| 35 | if (uptodate) |
| 36 | set_buffer_uptodate(bh); |
| 37 | else |
| 38 | clear_buffer_uptodate(bh); |
| 39 | unlock_buffer(bh); |
| 40 | } |
| 41 | |
| 42 | /* |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 43 | * When an ext4 file is truncated, it is possible that some pages are not |
| 44 | * successfully freed, because they are attached to a committing transaction. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 45 | * After the transaction commits, these pages are left on the LRU, with no |
| 46 | * ->mapping, and with attached buffers. These pages are trivially reclaimable |
| 47 | * by the VM, but their apparent absence upsets the VM accounting, and it makes |
| 48 | * the numbers in /proc/meminfo look odd. |
| 49 | * |
| 50 | * So here, we have a buffer which has just come off the forget list. Look to |
| 51 | * see if we can strip all buffers from the backing page. |
| 52 | * |
| 53 | * Called under lock_journal(), and possibly under journal_datalist_lock. The |
| 54 | * caller provided us with a ref against the buffer, and we drop that here. |
| 55 | */ |
| 56 | static void release_buffer_page(struct buffer_head *bh) |
| 57 | { |
| 58 | struct page *page; |
| 59 | |
| 60 | if (buffer_dirty(bh)) |
| 61 | goto nope; |
| 62 | if (atomic_read(&bh->b_count) != 1) |
| 63 | goto nope; |
| 64 | page = bh->b_page; |
| 65 | if (!page) |
| 66 | goto nope; |
| 67 | if (page->mapping) |
| 68 | goto nope; |
| 69 | |
| 70 | /* OK, it's a truncated page */ |
Nick Piggin | 529ae9a | 2008-08-02 12:01:03 +0200 | [diff] [blame] | 71 | if (!trylock_page(page)) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 72 | goto nope; |
| 73 | |
| 74 | page_cache_get(page); |
| 75 | __brelse(bh); |
| 76 | try_to_free_buffers(page); |
| 77 | unlock_page(page); |
| 78 | page_cache_release(page); |
| 79 | return; |
| 80 | |
| 81 | nope: |
| 82 | __brelse(bh); |
| 83 | } |
| 84 | |
| 85 | /* |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 86 | * Done it all: now submit the commit record. We should have |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 87 | * cleaned up our previous buffers by now, so if we are in abort |
| 88 | * mode we can now just skip the rest of the journal write |
| 89 | * entirely. |
| 90 | * |
| 91 | * Returns 1 if the journal needs to be aborted or 0 on success |
| 92 | */ |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 93 | static int journal_submit_commit_record(journal_t *journal, |
| 94 | transaction_t *commit_transaction, |
| 95 | struct buffer_head **cbh, |
| 96 | __u32 crc32_sum) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 97 | { |
| 98 | struct journal_head *descriptor; |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 99 | struct commit_header *tmp; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 100 | struct buffer_head *bh; |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 101 | int ret; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 102 | int barrier_done = 0; |
Theodore Ts'o | 736603a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 103 | struct timespec now = current_kernel_time(); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 104 | |
| 105 | if (is_journal_aborted(journal)) |
| 106 | return 0; |
| 107 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 108 | descriptor = jbd2_journal_get_descriptor_buffer(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 109 | if (!descriptor) |
| 110 | return 1; |
| 111 | |
| 112 | bh = jh2bh(descriptor); |
| 113 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 114 | tmp = (struct commit_header *)bh->b_data; |
| 115 | tmp->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); |
| 116 | tmp->h_blocktype = cpu_to_be32(JBD2_COMMIT_BLOCK); |
| 117 | tmp->h_sequence = cpu_to_be32(commit_transaction->t_tid); |
Theodore Ts'o | 736603a | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 118 | tmp->h_commit_sec = cpu_to_be64(now.tv_sec); |
| 119 | tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec); |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 120 | |
| 121 | if (JBD2_HAS_COMPAT_FEATURE(journal, |
| 122 | JBD2_FEATURE_COMPAT_CHECKSUM)) { |
| 123 | tmp->h_chksum_type = JBD2_CRC32_CHKSUM; |
| 124 | tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE; |
| 125 | tmp->h_chksum[0] = cpu_to_be32(crc32_sum); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 128 | JBUFFER_TRACE(descriptor, "submit commit block"); |
| 129 | lock_buffer(bh); |
Theodore Ts'o | 45a90bf | 2008-10-06 12:04:02 -0400 | [diff] [blame] | 130 | clear_buffer_dirty(bh); |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 131 | set_buffer_uptodate(bh); |
| 132 | bh->b_end_io = journal_end_buffer_io_sync; |
| 133 | |
| 134 | if (journal->j_flags & JBD2_BARRIER && |
Aneesh Kumar K.V | 4d60517 | 2008-02-05 10:56:15 -0500 | [diff] [blame] | 135 | !JBD2_HAS_INCOMPAT_FEATURE(journal, |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 136 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 137 | set_buffer_ordered(bh); |
| 138 | barrier_done = 1; |
| 139 | } |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 140 | ret = submit_bh(WRITE, bh); |
Dave Kleikamp | c4e35e07 | 2008-02-10 01:09:32 -0500 | [diff] [blame] | 141 | if (barrier_done) |
| 142 | clear_buffer_ordered(bh); |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 143 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 144 | /* is it possible for another commit to fail at roughly |
| 145 | * the same time as this one? If so, we don't want to |
| 146 | * trust the barrier flag in the super, but instead want |
| 147 | * to remember if we sent a barrier request |
| 148 | */ |
| 149 | if (ret == -EOPNOTSUPP && barrier_done) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 150 | printk(KERN_WARNING |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 151 | "JBD: barrier-based sync failed on %s - " |
| 152 | "disabling barriers\n", journal->j_devname); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 153 | spin_lock(&journal->j_state_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 154 | journal->j_flags &= ~JBD2_BARRIER; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 155 | spin_unlock(&journal->j_state_lock); |
| 156 | |
| 157 | /* And try again, without the barrier */ |
Theodore Ts'o | 034772b | 2008-06-03 22:31:11 -0400 | [diff] [blame] | 158 | lock_buffer(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 159 | set_buffer_uptodate(bh); |
Theodore Ts'o | 45a90bf | 2008-10-06 12:04:02 -0400 | [diff] [blame] | 160 | clear_buffer_dirty(bh); |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 161 | ret = submit_bh(WRITE, bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 162 | } |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 163 | *cbh = bh; |
| 164 | return ret; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 167 | /* |
| 168 | * This function along with journal_submit_commit_record |
| 169 | * allows to write the commit record asynchronously. |
| 170 | */ |
| 171 | static int journal_wait_on_commit_record(struct buffer_head *bh) |
| 172 | { |
| 173 | int ret = 0; |
| 174 | |
| 175 | clear_buffer_dirty(bh); |
| 176 | wait_on_buffer(bh); |
| 177 | |
| 178 | if (unlikely(!buffer_uptodate(bh))) |
| 179 | ret = -EIO; |
| 180 | put_bh(bh); /* One for getblk() */ |
| 181 | jbd2_journal_put_journal_head(bh2jh(bh)); |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /* |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 187 | * write the filemap data using writepage() address_space_operations. |
| 188 | * We don't do block allocation here even for delalloc. We don't |
| 189 | * use writepages() because with dealyed allocation we may be doing |
| 190 | * block allocation in writepages(). |
| 191 | */ |
| 192 | static int journal_submit_inode_data_buffers(struct address_space *mapping) |
| 193 | { |
| 194 | int ret; |
| 195 | struct writeback_control wbc = { |
| 196 | .sync_mode = WB_SYNC_ALL, |
| 197 | .nr_to_write = mapping->nrpages * 2, |
| 198 | .range_start = 0, |
| 199 | .range_end = i_size_read(mapping->host), |
| 200 | .for_writepages = 1, |
| 201 | }; |
| 202 | |
| 203 | ret = generic_writepages(mapping, &wbc); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | /* |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 208 | * Submit all the data buffers of inode associated with the transaction to |
| 209 | * disk. |
| 210 | * |
| 211 | * We are in a committing transaction. Therefore no new inode can be added to |
| 212 | * our inode list. We use JI_COMMIT_RUNNING flag to protect inode we currently |
| 213 | * operate on from being released while we write out pages. |
| 214 | */ |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 215 | static int journal_submit_data_buffers(journal_t *journal, |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 216 | transaction_t *commit_transaction) |
| 217 | { |
| 218 | struct jbd2_inode *jinode; |
| 219 | int err, ret = 0; |
| 220 | struct address_space *mapping; |
| 221 | |
| 222 | spin_lock(&journal->j_list_lock); |
| 223 | list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { |
| 224 | mapping = jinode->i_vfs_inode->i_mapping; |
| 225 | jinode->i_flags |= JI_COMMIT_RUNNING; |
| 226 | spin_unlock(&journal->j_list_lock); |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 227 | /* |
| 228 | * submit the inode data buffers. We use writepage |
| 229 | * instead of writepages. Because writepages can do |
| 230 | * block allocation with delalloc. We need to write |
| 231 | * only allocated blocks here. |
| 232 | */ |
| 233 | err = journal_submit_inode_data_buffers(mapping); |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 234 | if (!ret) |
| 235 | ret = err; |
| 236 | spin_lock(&journal->j_list_lock); |
| 237 | J_ASSERT(jinode->i_transaction == commit_transaction); |
| 238 | jinode->i_flags &= ~JI_COMMIT_RUNNING; |
| 239 | wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); |
| 240 | } |
| 241 | spin_unlock(&journal->j_list_lock); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Wait for data submitted for writeout, refile inodes to proper |
| 247 | * transaction if needed. |
| 248 | * |
| 249 | */ |
| 250 | static int journal_finish_inode_data_buffers(journal_t *journal, |
| 251 | transaction_t *commit_transaction) |
| 252 | { |
| 253 | struct jbd2_inode *jinode, *next_i; |
| 254 | int err, ret = 0; |
| 255 | |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 256 | /* For locking, see the comment in journal_submit_data_buffers() */ |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 257 | spin_lock(&journal->j_list_lock); |
| 258 | list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) { |
| 259 | jinode->i_flags |= JI_COMMIT_RUNNING; |
| 260 | spin_unlock(&journal->j_list_lock); |
| 261 | err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping); |
Hidehiro Kawai | e9e34f4 | 2008-07-31 22:26:04 -0400 | [diff] [blame] | 262 | if (err) { |
| 263 | /* |
| 264 | * Because AS_EIO is cleared by |
| 265 | * wait_on_page_writeback_range(), set it again so |
| 266 | * that user process can get -EIO from fsync(). |
| 267 | */ |
| 268 | set_bit(AS_EIO, |
| 269 | &jinode->i_vfs_inode->i_mapping->flags); |
| 270 | |
| 271 | if (!ret) |
| 272 | ret = err; |
| 273 | } |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 274 | spin_lock(&journal->j_list_lock); |
| 275 | jinode->i_flags &= ~JI_COMMIT_RUNNING; |
| 276 | wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING); |
| 277 | } |
| 278 | |
| 279 | /* Now refile inode to proper lists */ |
| 280 | list_for_each_entry_safe(jinode, next_i, |
| 281 | &commit_transaction->t_inode_list, i_list) { |
| 282 | list_del(&jinode->i_list); |
| 283 | if (jinode->i_next_transaction) { |
| 284 | jinode->i_transaction = jinode->i_next_transaction; |
| 285 | jinode->i_next_transaction = NULL; |
| 286 | list_add(&jinode->i_list, |
| 287 | &jinode->i_transaction->t_inode_list); |
| 288 | } else { |
| 289 | jinode->i_transaction = NULL; |
| 290 | } |
| 291 | } |
| 292 | spin_unlock(&journal->j_list_lock); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 297 | static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh) |
| 298 | { |
| 299 | struct page *page = bh->b_page; |
| 300 | char *addr; |
| 301 | __u32 checksum; |
| 302 | |
| 303 | addr = kmap_atomic(page, KM_USER0); |
| 304 | checksum = crc32_be(crc32_sum, |
| 305 | (void *)(addr + offset_in_page(bh->b_data)), bh->b_size); |
| 306 | kunmap_atomic(addr, KM_USER0); |
| 307 | |
| 308 | return checksum; |
| 309 | } |
| 310 | |
| 311 | static void write_tag_block(int tag_bytes, journal_block_tag_t *tag, |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 312 | unsigned long long block) |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 313 | { |
| 314 | tag->t_blocknr = cpu_to_be32(block & (u32)~0); |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 315 | if (tag_bytes > JBD2_TAG_SIZE32) |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 316 | tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1); |
| 317 | } |
| 318 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 319 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 320 | * jbd2_journal_commit_transaction |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 321 | * |
| 322 | * The primary function for committing a transaction to the log. This |
| 323 | * function is called by the journal thread to begin a complete commit. |
| 324 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 325 | void jbd2_journal_commit_transaction(journal_t *journal) |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 326 | { |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 327 | struct transaction_stats_s stats; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 328 | transaction_t *commit_transaction; |
| 329 | struct journal_head *jh, *new_jh, *descriptor; |
| 330 | struct buffer_head **wbuf = journal->j_wbuf; |
| 331 | int bufs; |
| 332 | int flags; |
| 333 | int err; |
Mingming Cao | 18eba7a | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 334 | unsigned long long blocknr; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 335 | char *tagp = NULL; |
| 336 | journal_header_t *header; |
| 337 | journal_block_tag_t *tag = NULL; |
| 338 | int space_left = 0; |
| 339 | int first_tag = 0; |
| 340 | int tag_flag; |
| 341 | int i; |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 342 | int tag_bytes = journal_tag_bytes(journal); |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 343 | struct buffer_head *cbh = NULL; /* For transactional checksums */ |
| 344 | __u32 crc32_sum = ~0; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * First job: lock down the current transaction and wait for |
| 348 | * all outstanding updates to complete. |
| 349 | */ |
| 350 | |
| 351 | #ifdef COMMIT_STATS |
| 352 | spin_lock(&journal->j_list_lock); |
| 353 | summarise_journal_usage(journal); |
| 354 | spin_unlock(&journal->j_list_lock); |
| 355 | #endif |
| 356 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 357 | /* Do we need to erase the effects of a prior jbd2_journal_flush? */ |
| 358 | if (journal->j_flags & JBD2_FLUSHED) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 359 | jbd_debug(3, "super block updated\n"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 360 | jbd2_journal_update_superblock(journal, 1); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 361 | } else { |
| 362 | jbd_debug(3, "superblock not updated\n"); |
| 363 | } |
| 364 | |
| 365 | J_ASSERT(journal->j_running_transaction != NULL); |
| 366 | J_ASSERT(journal->j_committing_transaction == NULL); |
| 367 | |
| 368 | commit_transaction = journal->j_running_transaction; |
| 369 | J_ASSERT(commit_transaction->t_state == T_RUNNING); |
| 370 | |
Theodore Ts'o | ede86cc | 2008-10-05 20:50:06 -0400 | [diff] [blame] | 371 | trace_mark(jbd2_start_commit, "dev %s transaction %d", |
| 372 | journal->j_devname, commit_transaction->t_tid); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 373 | jbd_debug(1, "JBD: starting commit of transaction %d\n", |
| 374 | commit_transaction->t_tid); |
| 375 | |
| 376 | spin_lock(&journal->j_state_lock); |
| 377 | commit_transaction->t_state = T_LOCKED; |
| 378 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 379 | stats.u.run.rs_wait = commit_transaction->t_max_wait; |
| 380 | stats.u.run.rs_locked = jiffies; |
| 381 | stats.u.run.rs_running = jbd2_time_diff(commit_transaction->t_start, |
| 382 | stats.u.run.rs_locked); |
| 383 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 384 | spin_lock(&commit_transaction->t_handle_lock); |
| 385 | while (commit_transaction->t_updates) { |
| 386 | DEFINE_WAIT(wait); |
| 387 | |
| 388 | prepare_to_wait(&journal->j_wait_updates, &wait, |
| 389 | TASK_UNINTERRUPTIBLE); |
| 390 | if (commit_transaction->t_updates) { |
| 391 | spin_unlock(&commit_transaction->t_handle_lock); |
| 392 | spin_unlock(&journal->j_state_lock); |
| 393 | schedule(); |
| 394 | spin_lock(&journal->j_state_lock); |
| 395 | spin_lock(&commit_transaction->t_handle_lock); |
| 396 | } |
| 397 | finish_wait(&journal->j_wait_updates, &wait); |
| 398 | } |
| 399 | spin_unlock(&commit_transaction->t_handle_lock); |
| 400 | |
| 401 | J_ASSERT (commit_transaction->t_outstanding_credits <= |
| 402 | journal->j_max_transaction_buffers); |
| 403 | |
| 404 | /* |
| 405 | * First thing we are allowed to do is to discard any remaining |
| 406 | * BJ_Reserved buffers. Note, it is _not_ permissible to assume |
| 407 | * that there are no such buffers: if a large filesystem |
| 408 | * operation like a truncate needs to split itself over multiple |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 409 | * transactions, then it may try to do a jbd2_journal_restart() while |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 410 | * there are still BJ_Reserved buffers outstanding. These must |
| 411 | * be released cleanly from the current transaction. |
| 412 | * |
| 413 | * In this case, the filesystem must still reserve write access |
| 414 | * again before modifying the buffer in the new transaction, but |
| 415 | * we do not require it to remember exactly which old buffers it |
| 416 | * has reserved. This is consistent with the existing behaviour |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 417 | * that multiple jbd2_journal_get_write_access() calls to the same |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 418 | * buffer are perfectly permissable. |
| 419 | */ |
| 420 | while (commit_transaction->t_reserved_list) { |
| 421 | jh = commit_transaction->t_reserved_list; |
| 422 | JBUFFER_TRACE(jh, "reserved, unused: refile"); |
| 423 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 424 | * A jbd2_journal_get_undo_access()+jbd2_journal_release_buffer() may |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 425 | * leave undo-committed data. |
| 426 | */ |
| 427 | if (jh->b_committed_data) { |
| 428 | struct buffer_head *bh = jh2bh(jh); |
| 429 | |
| 430 | jbd_lock_bh_state(bh); |
Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 431 | jbd2_free(jh->b_committed_data, bh->b_size); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 432 | jh->b_committed_data = NULL; |
| 433 | jbd_unlock_bh_state(bh); |
| 434 | } |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 435 | jbd2_journal_refile_buffer(journal, jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Now try to drop any written-back buffers from the journal's |
| 440 | * checkpoint lists. We do this *before* commit because it potentially |
| 441 | * frees some memory |
| 442 | */ |
| 443 | spin_lock(&journal->j_list_lock); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 444 | __jbd2_journal_clean_checkpoint_list(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 445 | spin_unlock(&journal->j_list_lock); |
| 446 | |
| 447 | jbd_debug (3, "JBD: commit phase 1\n"); |
| 448 | |
| 449 | /* |
| 450 | * Switch to a new revoke table. |
| 451 | */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 452 | jbd2_journal_switch_revoke_table(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 453 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 454 | stats.u.run.rs_flushing = jiffies; |
| 455 | stats.u.run.rs_locked = jbd2_time_diff(stats.u.run.rs_locked, |
| 456 | stats.u.run.rs_flushing); |
| 457 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 458 | commit_transaction->t_state = T_FLUSH; |
| 459 | journal->j_committing_transaction = commit_transaction; |
| 460 | journal->j_running_transaction = NULL; |
| 461 | commit_transaction->t_log_start = journal->j_head; |
| 462 | wake_up(&journal->j_wait_transaction_locked); |
| 463 | spin_unlock(&journal->j_state_lock); |
| 464 | |
| 465 | jbd_debug (3, "JBD: commit phase 2\n"); |
| 466 | |
| 467 | /* |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 468 | * Now start flushing things to disk, in the order they appear |
| 469 | * on the transaction lists. Data blocks go first. |
| 470 | */ |
Aneesh Kumar K.V | cd1aac3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 471 | err = journal_submit_data_buffers(journal, commit_transaction); |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 472 | if (err) |
| 473 | jbd2_journal_abort(journal, err); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 474 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 475 | jbd2_journal_write_revoke_records(journal, commit_transaction); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 476 | |
| 477 | jbd_debug(3, "JBD: commit phase 2\n"); |
| 478 | |
| 479 | /* |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 480 | * Way to go: we have now written out all of the data for a |
| 481 | * transaction! Now comes the tricky part: we need to write out |
| 482 | * metadata. Loop over the transaction's entire buffer list: |
| 483 | */ |
Mingming Cao | 02c471c | 2008-05-15 14:46:17 -0400 | [diff] [blame] | 484 | spin_lock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 485 | commit_transaction->t_state = T_COMMIT; |
Mingming Cao | 02c471c | 2008-05-15 14:46:17 -0400 | [diff] [blame] | 486 | spin_unlock(&journal->j_state_lock); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 487 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 488 | stats.u.run.rs_logging = jiffies; |
| 489 | stats.u.run.rs_flushing = jbd2_time_diff(stats.u.run.rs_flushing, |
| 490 | stats.u.run.rs_logging); |
| 491 | stats.u.run.rs_blocks = commit_transaction->t_outstanding_credits; |
| 492 | stats.u.run.rs_blocks_logged = 0; |
| 493 | |
Josef Bacik | 1dfc322 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 494 | J_ASSERT(commit_transaction->t_nr_buffers <= |
| 495 | commit_transaction->t_outstanding_credits); |
| 496 | |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 497 | err = 0; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 498 | descriptor = NULL; |
| 499 | bufs = 0; |
| 500 | while (commit_transaction->t_buffers) { |
| 501 | |
| 502 | /* Find the next buffer to be journaled... */ |
| 503 | |
| 504 | jh = commit_transaction->t_buffers; |
| 505 | |
| 506 | /* If we're in abort mode, we just un-journal the buffer and |
Hidehiro Kawai | 7ad7445 | 2008-10-10 20:29:31 -0400 | [diff] [blame] | 507 | release it. */ |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 508 | |
| 509 | if (is_journal_aborted(journal)) { |
Hidehiro Kawai | 7ad7445 | 2008-10-10 20:29:31 -0400 | [diff] [blame] | 510 | clear_buffer_jbddirty(jh2bh(jh)); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 511 | JBUFFER_TRACE(jh, "journal is aborting: refile"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 512 | jbd2_journal_refile_buffer(journal, jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 513 | /* If that was the last one, we need to clean up |
| 514 | * any descriptor buffers which may have been |
| 515 | * already allocated, even if we are now |
| 516 | * aborting. */ |
| 517 | if (!commit_transaction->t_buffers) |
| 518 | goto start_journal_io; |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | /* Make sure we have a descriptor block in which to |
| 523 | record the metadata buffer. */ |
| 524 | |
| 525 | if (!descriptor) { |
| 526 | struct buffer_head *bh; |
| 527 | |
| 528 | J_ASSERT (bufs == 0); |
| 529 | |
| 530 | jbd_debug(4, "JBD: get descriptor\n"); |
| 531 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 532 | descriptor = jbd2_journal_get_descriptor_buffer(journal); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 533 | if (!descriptor) { |
Jan Kara | a7fa2ba | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 534 | jbd2_journal_abort(journal, -EIO); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 535 | continue; |
| 536 | } |
| 537 | |
| 538 | bh = jh2bh(descriptor); |
| 539 | jbd_debug(4, "JBD: got buffer %llu (%p)\n", |
| 540 | (unsigned long long)bh->b_blocknr, bh->b_data); |
| 541 | header = (journal_header_t *)&bh->b_data[0]; |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 542 | header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); |
| 543 | header->h_blocktype = cpu_to_be32(JBD2_DESCRIPTOR_BLOCK); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 544 | header->h_sequence = cpu_to_be32(commit_transaction->t_tid); |
| 545 | |
| 546 | tagp = &bh->b_data[sizeof(journal_header_t)]; |
| 547 | space_left = bh->b_size - sizeof(journal_header_t); |
| 548 | first_tag = 1; |
| 549 | set_buffer_jwrite(bh); |
| 550 | set_buffer_dirty(bh); |
| 551 | wbuf[bufs++] = bh; |
| 552 | |
| 553 | /* Record it so that we can wait for IO |
| 554 | completion later */ |
| 555 | BUFFER_TRACE(bh, "ph3: file as descriptor"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 556 | jbd2_journal_file_buffer(descriptor, commit_transaction, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 557 | BJ_LogCtl); |
| 558 | } |
| 559 | |
| 560 | /* Where is the buffer to be written? */ |
| 561 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 562 | err = jbd2_journal_next_log_block(journal, &blocknr); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 563 | /* If the block mapping failed, just abandon the buffer |
| 564 | and repeat this loop: we'll fall into the |
| 565 | refile-on-abort condition above. */ |
| 566 | if (err) { |
Jan Kara | a7fa2ba | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 567 | jbd2_journal_abort(journal, err); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 568 | continue; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * start_this_handle() uses t_outstanding_credits to determine |
| 573 | * the free space in the log, but this counter is changed |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 574 | * by jbd2_journal_next_log_block() also. |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 575 | */ |
| 576 | commit_transaction->t_outstanding_credits--; |
| 577 | |
| 578 | /* Bump b_count to prevent truncate from stumbling over |
| 579 | the shadowed buffer! @@@ This can go if we ever get |
| 580 | rid of the BJ_IO/BJ_Shadow pairing of buffers. */ |
| 581 | atomic_inc(&jh2bh(jh)->b_count); |
| 582 | |
| 583 | /* Make a temporary IO buffer with which to write it out |
| 584 | (this will requeue both the metadata buffer and the |
| 585 | temporary IO buffer). new_bh goes on BJ_IO*/ |
| 586 | |
| 587 | set_bit(BH_JWrite, &jh2bh(jh)->b_state); |
| 588 | /* |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 589 | * akpm: jbd2_journal_write_metadata_buffer() sets |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 590 | * new_bh->b_transaction to commit_transaction. |
| 591 | * We need to clean this up before we release new_bh |
| 592 | * (which is of type BJ_IO) |
| 593 | */ |
| 594 | JBUFFER_TRACE(jh, "ph3: write metadata"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 595 | flags = jbd2_journal_write_metadata_buffer(commit_transaction, |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 596 | jh, &new_jh, blocknr); |
| 597 | set_bit(BH_JWrite, &jh2bh(new_jh)->b_state); |
| 598 | wbuf[bufs++] = jh2bh(new_jh); |
| 599 | |
| 600 | /* Record the new block's tag in the current descriptor |
| 601 | buffer */ |
| 602 | |
| 603 | tag_flag = 0; |
| 604 | if (flags & 1) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 605 | tag_flag |= JBD2_FLAG_ESCAPE; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 606 | if (!first_tag) |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 607 | tag_flag |= JBD2_FLAG_SAME_UUID; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 608 | |
| 609 | tag = (journal_block_tag_t *) tagp; |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 610 | write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 611 | tag->t_flags = cpu_to_be32(tag_flag); |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 612 | tagp += tag_bytes; |
| 613 | space_left -= tag_bytes; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 614 | |
| 615 | if (first_tag) { |
| 616 | memcpy (tagp, journal->j_uuid, 16); |
| 617 | tagp += 16; |
| 618 | space_left -= 16; |
| 619 | first_tag = 0; |
| 620 | } |
| 621 | |
| 622 | /* If there's no more to do, or if the descriptor is full, |
| 623 | let the IO rip! */ |
| 624 | |
| 625 | if (bufs == journal->j_wbufsize || |
| 626 | commit_transaction->t_buffers == NULL || |
Zach Brown | b517bea | 2006-10-11 01:21:08 -0700 | [diff] [blame] | 627 | space_left < tag_bytes + 16) { |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 628 | |
| 629 | jbd_debug(4, "JBD: Submit %d IOs\n", bufs); |
| 630 | |
| 631 | /* Write an end-of-descriptor marker before |
| 632 | submitting the IOs. "tag" still points to |
| 633 | the last tag we set up. */ |
| 634 | |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 635 | tag->t_flags |= cpu_to_be32(JBD2_FLAG_LAST_TAG); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 636 | |
| 637 | start_journal_io: |
| 638 | for (i = 0; i < bufs; i++) { |
| 639 | struct buffer_head *bh = wbuf[i]; |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 640 | /* |
| 641 | * Compute checksum. |
| 642 | */ |
| 643 | if (JBD2_HAS_COMPAT_FEATURE(journal, |
| 644 | JBD2_FEATURE_COMPAT_CHECKSUM)) { |
| 645 | crc32_sum = |
| 646 | jbd2_checksum_data(crc32_sum, bh); |
| 647 | } |
| 648 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 649 | lock_buffer(bh); |
| 650 | clear_buffer_dirty(bh); |
| 651 | set_buffer_uptodate(bh); |
| 652 | bh->b_end_io = journal_end_buffer_io_sync; |
| 653 | submit_bh(WRITE, bh); |
| 654 | } |
| 655 | cond_resched(); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 656 | stats.u.run.rs_blocks_logged += bufs; |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 657 | |
| 658 | /* Force a new descriptor to be generated next |
| 659 | time round the loop. */ |
| 660 | descriptor = NULL; |
| 661 | bufs = 0; |
| 662 | } |
| 663 | } |
| 664 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 665 | /* Done it all: now write the commit record asynchronously. */ |
| 666 | |
| 667 | if (JBD2_HAS_INCOMPAT_FEATURE(journal, |
| 668 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { |
| 669 | err = journal_submit_commit_record(journal, commit_transaction, |
| 670 | &cbh, crc32_sum); |
| 671 | if (err) |
| 672 | __jbd2_journal_abort_hard(journal); |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 673 | } |
| 674 | |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 675 | /* |
| 676 | * This is the right place to wait for data buffers both for ASYNC |
| 677 | * and !ASYNC commit. If commit is ASYNC, we need to wait only after |
| 678 | * the commit block went to disk (which happens above). If commit is |
| 679 | * SYNC, we need to wait for data buffers before we start writing |
| 680 | * commit block, which happens below in such setting. |
| 681 | */ |
| 682 | err = journal_finish_inode_data_buffers(journal, commit_transaction); |
Hidehiro Kawai | e9e34f4 | 2008-07-31 22:26:04 -0400 | [diff] [blame] | 683 | if (err) { |
Hidehiro Kawai | e9e34f4 | 2008-07-31 22:26:04 -0400 | [diff] [blame] | 684 | printk(KERN_WARNING |
| 685 | "JBD2: Detected IO errors while flushing file data " |
Theodore Ts'o | 0549676 | 2008-09-16 14:36:17 -0400 | [diff] [blame] | 686 | "on %s\n", journal->j_devname); |
Hidehiro Kawai | 5bf5683 | 2008-10-10 22:12:43 -0400 | [diff] [blame] | 687 | if (journal->j_flags & JBD2_ABORT_ON_SYNCDATA_ERR) |
| 688 | jbd2_journal_abort(journal, err); |
Hidehiro Kawai | e9e34f4 | 2008-07-31 22:26:04 -0400 | [diff] [blame] | 689 | err = 0; |
| 690 | } |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 691 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 692 | /* Lo and behold: we have just managed to send a transaction to |
| 693 | the log. Before we can commit it, wait for the IO so far to |
| 694 | complete. Control buffers being written are on the |
| 695 | transaction's t_log_list queue, and metadata buffers are on |
| 696 | the t_iobuf_list queue. |
| 697 | |
| 698 | Wait for the buffers in reverse order. That way we are |
| 699 | less likely to be woken up until all IOs have completed, and |
| 700 | so we incur less scheduling load. |
| 701 | */ |
| 702 | |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 703 | jbd_debug(3, "JBD: commit phase 3\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | * akpm: these are BJ_IO, and j_list_lock is not needed. |
| 707 | * See __journal_try_to_free_buffer. |
| 708 | */ |
| 709 | wait_for_iobuf: |
| 710 | while (commit_transaction->t_iobuf_list != NULL) { |
| 711 | struct buffer_head *bh; |
| 712 | |
| 713 | jh = commit_transaction->t_iobuf_list->b_tprev; |
| 714 | bh = jh2bh(jh); |
| 715 | if (buffer_locked(bh)) { |
| 716 | wait_on_buffer(bh); |
| 717 | goto wait_for_iobuf; |
| 718 | } |
| 719 | if (cond_resched()) |
| 720 | goto wait_for_iobuf; |
| 721 | |
| 722 | if (unlikely(!buffer_uptodate(bh))) |
| 723 | err = -EIO; |
| 724 | |
| 725 | clear_buffer_jwrite(bh); |
| 726 | |
| 727 | JBUFFER_TRACE(jh, "ph4: unfile after journal write"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 728 | jbd2_journal_unfile_buffer(journal, jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 729 | |
| 730 | /* |
| 731 | * ->t_iobuf_list should contain only dummy buffer_heads |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 732 | * which were created by jbd2_journal_write_metadata_buffer(). |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 733 | */ |
| 734 | BUFFER_TRACE(bh, "dumping temporary bh"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 735 | jbd2_journal_put_journal_head(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 736 | __brelse(bh); |
| 737 | J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0); |
| 738 | free_buffer_head(bh); |
| 739 | |
| 740 | /* We also have to unlock and free the corresponding |
| 741 | shadowed buffer */ |
| 742 | jh = commit_transaction->t_shadow_list->b_tprev; |
| 743 | bh = jh2bh(jh); |
| 744 | clear_bit(BH_JWrite, &bh->b_state); |
| 745 | J_ASSERT_BH(bh, buffer_jbddirty(bh)); |
| 746 | |
| 747 | /* The metadata is now released for reuse, but we need |
| 748 | to remember it against this transaction so that when |
| 749 | we finally commit, we can do any checkpointing |
| 750 | required. */ |
| 751 | JBUFFER_TRACE(jh, "file as BJ_Forget"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 752 | jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 753 | /* Wake up any transactions which were waiting for this |
| 754 | IO to complete */ |
| 755 | wake_up_bit(&bh->b_state, BH_Unshadow); |
| 756 | JBUFFER_TRACE(jh, "brelse shadowed buffer"); |
| 757 | __brelse(bh); |
| 758 | } |
| 759 | |
| 760 | J_ASSERT (commit_transaction->t_shadow_list == NULL); |
| 761 | |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 762 | jbd_debug(3, "JBD: commit phase 4\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 763 | |
| 764 | /* Here we wait for the revoke record and descriptor record buffers */ |
| 765 | wait_for_ctlbuf: |
| 766 | while (commit_transaction->t_log_list != NULL) { |
| 767 | struct buffer_head *bh; |
| 768 | |
| 769 | jh = commit_transaction->t_log_list->b_tprev; |
| 770 | bh = jh2bh(jh); |
| 771 | if (buffer_locked(bh)) { |
| 772 | wait_on_buffer(bh); |
| 773 | goto wait_for_ctlbuf; |
| 774 | } |
| 775 | if (cond_resched()) |
| 776 | goto wait_for_ctlbuf; |
| 777 | |
| 778 | if (unlikely(!buffer_uptodate(bh))) |
| 779 | err = -EIO; |
| 780 | |
| 781 | BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile"); |
| 782 | clear_buffer_jwrite(bh); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 783 | jbd2_journal_unfile_buffer(journal, jh); |
| 784 | jbd2_journal_put_journal_head(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 785 | __brelse(bh); /* One for getblk */ |
| 786 | /* AKPM: bforget here */ |
| 787 | } |
| 788 | |
Hidehiro Kawai | 77e841d | 2008-10-12 16:39:16 -0400 | [diff] [blame] | 789 | if (err) |
| 790 | jbd2_journal_abort(journal, err); |
| 791 | |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 792 | jbd_debug(3, "JBD: commit phase 5\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 793 | |
Girish Shilamkar | 818d276 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 794 | if (!JBD2_HAS_INCOMPAT_FEATURE(journal, |
| 795 | JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) { |
| 796 | err = journal_submit_commit_record(journal, commit_transaction, |
| 797 | &cbh, crc32_sum); |
| 798 | if (err) |
| 799 | __jbd2_journal_abort_hard(journal); |
| 800 | } |
Mingming Cao | b048d84 | 2008-02-05 08:52:45 -0500 | [diff] [blame] | 801 | if (!err && !is_journal_aborted(journal)) |
| 802 | err = journal_wait_on_commit_record(cbh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 803 | |
| 804 | if (err) |
Jan Kara | a7fa2ba | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 805 | jbd2_journal_abort(journal, err); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 806 | |
| 807 | /* End of a transaction! Finally, we can do checkpoint |
| 808 | processing: any buffers committed as a result of this |
| 809 | transaction can be removed from any checkpoint list it was on |
| 810 | before. */ |
| 811 | |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 812 | jbd_debug(3, "JBD: commit phase 6\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 813 | |
Jan Kara | c851ed5 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 814 | J_ASSERT(list_empty(&commit_transaction->t_inode_list)); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 815 | J_ASSERT(commit_transaction->t_buffers == NULL); |
| 816 | J_ASSERT(commit_transaction->t_checkpoint_list == NULL); |
| 817 | J_ASSERT(commit_transaction->t_iobuf_list == NULL); |
| 818 | J_ASSERT(commit_transaction->t_shadow_list == NULL); |
| 819 | J_ASSERT(commit_transaction->t_log_list == NULL); |
| 820 | |
| 821 | restart_loop: |
| 822 | /* |
| 823 | * As there are other places (journal_unmap_buffer()) adding buffers |
| 824 | * to this list we have to be careful and hold the j_list_lock. |
| 825 | */ |
| 826 | spin_lock(&journal->j_list_lock); |
| 827 | while (commit_transaction->t_forget) { |
| 828 | transaction_t *cp_transaction; |
| 829 | struct buffer_head *bh; |
| 830 | |
| 831 | jh = commit_transaction->t_forget; |
| 832 | spin_unlock(&journal->j_list_lock); |
| 833 | bh = jh2bh(jh); |
| 834 | jbd_lock_bh_state(bh); |
| 835 | J_ASSERT_JH(jh, jh->b_transaction == commit_transaction || |
| 836 | jh->b_transaction == journal->j_running_transaction); |
| 837 | |
| 838 | /* |
| 839 | * If there is undo-protected committed data against |
| 840 | * this buffer, then we can remove it now. If it is a |
| 841 | * buffer needing such protection, the old frozen_data |
| 842 | * field now points to a committed version of the |
| 843 | * buffer, so rotate that field to the new committed |
| 844 | * data. |
| 845 | * |
| 846 | * Otherwise, we can just throw away the frozen data now. |
| 847 | */ |
| 848 | if (jh->b_committed_data) { |
Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 849 | jbd2_free(jh->b_committed_data, bh->b_size); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 850 | jh->b_committed_data = NULL; |
| 851 | if (jh->b_frozen_data) { |
| 852 | jh->b_committed_data = jh->b_frozen_data; |
| 853 | jh->b_frozen_data = NULL; |
| 854 | } |
| 855 | } else if (jh->b_frozen_data) { |
Mingming Cao | af1e76d | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 856 | jbd2_free(jh->b_frozen_data, bh->b_size); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 857 | jh->b_frozen_data = NULL; |
| 858 | } |
| 859 | |
| 860 | spin_lock(&journal->j_list_lock); |
| 861 | cp_transaction = jh->b_cp_transaction; |
| 862 | if (cp_transaction) { |
| 863 | JBUFFER_TRACE(jh, "remove from old cp transaction"); |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 864 | cp_transaction->t_chp_stats.cs_dropped++; |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 865 | __jbd2_journal_remove_checkpoint(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* Only re-checkpoint the buffer_head if it is marked |
| 869 | * dirty. If the buffer was added to the BJ_Forget list |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 870 | * by jbd2_journal_forget, it may no longer be dirty and |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 871 | * there's no point in keeping a checkpoint record for |
| 872 | * it. */ |
| 873 | |
| 874 | /* A buffer which has been freed while still being |
| 875 | * journaled by a previous transaction may end up still |
| 876 | * being dirty here, but we want to avoid writing back |
| 877 | * that buffer in the future now that the last use has |
| 878 | * been committed. That's not only a performance gain, |
| 879 | * it also stops aliasing problems if the buffer is left |
| 880 | * behind for writeback and gets reallocated for another |
| 881 | * use in a different page. */ |
| 882 | if (buffer_freed(bh)) { |
| 883 | clear_buffer_freed(bh); |
| 884 | clear_buffer_jbddirty(bh); |
| 885 | } |
| 886 | |
| 887 | if (buffer_jbddirty(bh)) { |
| 888 | JBUFFER_TRACE(jh, "add to new checkpointing trans"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 889 | __jbd2_journal_insert_checkpoint(jh, commit_transaction); |
Hidehiro Kawai | 7ad7445 | 2008-10-10 20:29:31 -0400 | [diff] [blame] | 890 | if (is_journal_aborted(journal)) |
| 891 | clear_buffer_jbddirty(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 892 | JBUFFER_TRACE(jh, "refile for checkpoint writeback"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 893 | __jbd2_journal_refile_buffer(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 894 | jbd_unlock_bh_state(bh); |
| 895 | } else { |
| 896 | J_ASSERT_BH(bh, !buffer_dirty(bh)); |
| 897 | /* The buffer on BJ_Forget list and not jbddirty means |
| 898 | * it has been freed by this transaction and hence it |
| 899 | * could not have been reallocated until this |
| 900 | * transaction has committed. *BUT* it could be |
| 901 | * reallocated once we have written all the data to |
| 902 | * disk and before we process the buffer on BJ_Forget |
| 903 | * list. */ |
| 904 | JBUFFER_TRACE(jh, "refile or unfile freed buffer"); |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 905 | __jbd2_journal_refile_buffer(jh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 906 | if (!jh->b_transaction) { |
| 907 | jbd_unlock_bh_state(bh); |
| 908 | /* needs a brelse */ |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 909 | jbd2_journal_remove_journal_head(bh); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 910 | release_buffer_page(bh); |
| 911 | } else |
| 912 | jbd_unlock_bh_state(bh); |
| 913 | } |
| 914 | cond_resched_lock(&journal->j_list_lock); |
| 915 | } |
| 916 | spin_unlock(&journal->j_list_lock); |
| 917 | /* |
Jan Kara | f5a7a6b | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 918 | * This is a bit sleazy. We use j_list_lock to protect transition |
| 919 | * of a transaction into T_FINISHED state and calling |
| 920 | * __jbd2_journal_drop_transaction(). Otherwise we could race with |
| 921 | * other checkpointing code processing the transaction... |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 922 | */ |
| 923 | spin_lock(&journal->j_state_lock); |
| 924 | spin_lock(&journal->j_list_lock); |
| 925 | /* |
| 926 | * Now recheck if some buffers did not get attached to the transaction |
| 927 | * while the lock was dropped... |
| 928 | */ |
| 929 | if (commit_transaction->t_forget) { |
| 930 | spin_unlock(&journal->j_list_lock); |
| 931 | spin_unlock(&journal->j_state_lock); |
| 932 | goto restart_loop; |
| 933 | } |
| 934 | |
| 935 | /* Done with this transaction! */ |
| 936 | |
Jan Kara | 87c89c2 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 937 | jbd_debug(3, "JBD: commit phase 7\n"); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 938 | |
| 939 | J_ASSERT(commit_transaction->t_state == T_COMMIT); |
| 940 | |
Johann Lombardi | 8e85fb3 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 941 | commit_transaction->t_start = jiffies; |
| 942 | stats.u.run.rs_logging = jbd2_time_diff(stats.u.run.rs_logging, |
| 943 | commit_transaction->t_start); |
| 944 | |
| 945 | /* |
| 946 | * File the transaction for history |
| 947 | */ |
| 948 | stats.ts_type = JBD2_STATS_RUN; |
| 949 | stats.ts_tid = commit_transaction->t_tid; |
| 950 | stats.u.run.rs_handle_count = commit_transaction->t_handle_count; |
| 951 | spin_lock(&journal->j_history_lock); |
| 952 | memcpy(journal->j_history + journal->j_history_cur, &stats, |
| 953 | sizeof(stats)); |
| 954 | if (++journal->j_history_cur == journal->j_history_max) |
| 955 | journal->j_history_cur = 0; |
| 956 | |
| 957 | /* |
| 958 | * Calculate overall stats |
| 959 | */ |
| 960 | journal->j_stats.ts_tid++; |
| 961 | journal->j_stats.u.run.rs_wait += stats.u.run.rs_wait; |
| 962 | journal->j_stats.u.run.rs_running += stats.u.run.rs_running; |
| 963 | journal->j_stats.u.run.rs_locked += stats.u.run.rs_locked; |
| 964 | journal->j_stats.u.run.rs_flushing += stats.u.run.rs_flushing; |
| 965 | journal->j_stats.u.run.rs_logging += stats.u.run.rs_logging; |
| 966 | journal->j_stats.u.run.rs_handle_count += stats.u.run.rs_handle_count; |
| 967 | journal->j_stats.u.run.rs_blocks += stats.u.run.rs_blocks; |
| 968 | journal->j_stats.u.run.rs_blocks_logged += stats.u.run.rs_blocks_logged; |
| 969 | spin_unlock(&journal->j_history_lock); |
| 970 | |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 971 | commit_transaction->t_state = T_FINISHED; |
| 972 | J_ASSERT(commit_transaction == journal->j_committing_transaction); |
| 973 | journal->j_commit_sequence = commit_transaction->t_tid; |
| 974 | journal->j_committing_transaction = NULL; |
| 975 | spin_unlock(&journal->j_state_lock); |
| 976 | |
Theodore Ts'o | 6c20ec8 | 2008-10-28 21:08:20 -0400 | [diff] [blame] | 977 | if (journal->j_commit_callback) |
| 978 | journal->j_commit_callback(journal, commit_transaction); |
| 979 | |
Jan Kara | f89b779 | 2007-07-15 23:37:20 -0700 | [diff] [blame] | 980 | if (commit_transaction->t_checkpoint_list == NULL && |
| 981 | commit_transaction->t_checkpoint_io_list == NULL) { |
Mingming Cao | f7f4bcc | 2006-10-11 01:20:59 -0700 | [diff] [blame] | 982 | __jbd2_journal_drop_transaction(journal, commit_transaction); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 983 | } else { |
| 984 | if (journal->j_checkpoint_transactions == NULL) { |
| 985 | journal->j_checkpoint_transactions = commit_transaction; |
| 986 | commit_transaction->t_cpnext = commit_transaction; |
| 987 | commit_transaction->t_cpprev = commit_transaction; |
| 988 | } else { |
| 989 | commit_transaction->t_cpnext = |
| 990 | journal->j_checkpoint_transactions; |
| 991 | commit_transaction->t_cpprev = |
| 992 | commit_transaction->t_cpnext->t_cpprev; |
| 993 | commit_transaction->t_cpnext->t_cpprev = |
| 994 | commit_transaction; |
| 995 | commit_transaction->t_cpprev->t_cpnext = |
| 996 | commit_transaction; |
| 997 | } |
| 998 | } |
| 999 | spin_unlock(&journal->j_list_lock); |
| 1000 | |
Theodore Ts'o | ede86cc | 2008-10-05 20:50:06 -0400 | [diff] [blame] | 1001 | trace_mark(jbd2_end_commit, "dev %s transaction %d head %d", |
Theodore Ts'o | 6c20ec8 | 2008-10-28 21:08:20 -0400 | [diff] [blame] | 1002 | journal->j_devname, journal->j_commit_sequence, |
Theodore Ts'o | ede86cc | 2008-10-05 20:50:06 -0400 | [diff] [blame] | 1003 | journal->j_tail_sequence); |
Dave Kleikamp | 470decc | 2006-10-11 01:20:57 -0700 | [diff] [blame] | 1004 | jbd_debug(1, "JBD: commit %d complete, head %d\n", |
| 1005 | journal->j_commit_sequence, journal->j_tail_sequence); |
| 1006 | |
| 1007 | wake_up(&journal->j_wait_done_commit); |
| 1008 | } |