Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/fs/jbd/commit.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -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> |
| 18 | #include <linux/jbd.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Default IO end handler for temporary BJ_IO buffer_heads. |
| 26 | */ |
| 27 | static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate) |
| 28 | { |
| 29 | BUFFER_TRACE(bh, ""); |
| 30 | if (uptodate) |
| 31 | set_buffer_uptodate(bh); |
| 32 | else |
| 33 | clear_buffer_uptodate(bh); |
| 34 | unlock_buffer(bh); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * When an ext3-ordered file is truncated, it is possible that many pages are |
| 39 | * not sucessfully freed, because they are attached to a committing transaction. |
| 40 | * After the transaction commits, these pages are left on the LRU, with no |
| 41 | * ->mapping, and with attached buffers. These pages are trivially reclaimable |
| 42 | * by the VM, but their apparent absence upsets the VM accounting, and it makes |
| 43 | * the numbers in /proc/meminfo look odd. |
| 44 | * |
| 45 | * So here, we have a buffer which has just come off the forget list. Look to |
| 46 | * see if we can strip all buffers from the backing page. |
| 47 | * |
| 48 | * Called under lock_journal(), and possibly under journal_datalist_lock. The |
| 49 | * caller provided us with a ref against the buffer, and we drop that here. |
| 50 | */ |
| 51 | static void release_buffer_page(struct buffer_head *bh) |
| 52 | { |
| 53 | struct page *page; |
| 54 | |
| 55 | if (buffer_dirty(bh)) |
| 56 | goto nope; |
| 57 | if (atomic_read(&bh->b_count) != 1) |
| 58 | goto nope; |
| 59 | page = bh->b_page; |
| 60 | if (!page) |
| 61 | goto nope; |
| 62 | if (page->mapping) |
| 63 | goto nope; |
| 64 | |
| 65 | /* OK, it's a truncated page */ |
| 66 | if (TestSetPageLocked(page)) |
| 67 | goto nope; |
| 68 | |
| 69 | page_cache_get(page); |
| 70 | __brelse(bh); |
| 71 | try_to_free_buffers(page); |
| 72 | unlock_page(page); |
| 73 | page_cache_release(page); |
| 74 | return; |
| 75 | |
| 76 | nope: |
| 77 | __brelse(bh); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Try to acquire jbd_lock_bh_state() against the buffer, when j_list_lock is |
| 82 | * held. For ranking reasons we must trylock. If we lose, schedule away and |
| 83 | * return 0. j_list_lock is dropped in this case. |
| 84 | */ |
| 85 | static int inverted_lock(journal_t *journal, struct buffer_head *bh) |
| 86 | { |
| 87 | if (!jbd_trylock_bh_state(bh)) { |
| 88 | spin_unlock(&journal->j_list_lock); |
| 89 | schedule(); |
| 90 | return 0; |
| 91 | } |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | /* Done it all: now write the commit record. We should have |
| 96 | * cleaned up our previous buffers by now, so if we are in abort |
| 97 | * mode we can now just skip the rest of the journal write |
| 98 | * entirely. |
| 99 | * |
| 100 | * Returns 1 if the journal needs to be aborted or 0 on success |
| 101 | */ |
| 102 | static int journal_write_commit_record(journal_t *journal, |
| 103 | transaction_t *commit_transaction) |
| 104 | { |
| 105 | struct journal_head *descriptor; |
| 106 | struct buffer_head *bh; |
Jan Kara | 5315217 | 2008-02-01 08:26:46 -0500 | [diff] [blame] | 107 | journal_header_t *header; |
| 108 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | int barrier_done = 0; |
| 110 | |
| 111 | if (is_journal_aborted(journal)) |
| 112 | return 0; |
| 113 | |
| 114 | descriptor = journal_get_descriptor_buffer(journal); |
| 115 | if (!descriptor) |
| 116 | return 1; |
| 117 | |
| 118 | bh = jh2bh(descriptor); |
| 119 | |
Jan Kara | 5315217 | 2008-02-01 08:26:46 -0500 | [diff] [blame] | 120 | header = (journal_header_t *)(bh->b_data); |
| 121 | header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER); |
| 122 | header->h_blocktype = cpu_to_be32(JFS_COMMIT_BLOCK); |
| 123 | header->h_sequence = cpu_to_be32(commit_transaction->t_tid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | JBUFFER_TRACE(descriptor, "write commit block"); |
| 126 | set_buffer_dirty(bh); |
| 127 | if (journal->j_flags & JFS_BARRIER) { |
| 128 | set_buffer_ordered(bh); |
| 129 | barrier_done = 1; |
| 130 | } |
| 131 | ret = sync_dirty_buffer(bh); |
Neil Brown | 28ae094 | 2008-02-08 04:22:13 -0800 | [diff] [blame] | 132 | if (barrier_done) |
| 133 | clear_buffer_ordered(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* is it possible for another commit to fail at roughly |
| 135 | * the same time as this one? If so, we don't want to |
| 136 | * trust the barrier flag in the super, but instead want |
| 137 | * to remember if we sent a barrier request |
| 138 | */ |
| 139 | if (ret == -EOPNOTSUPP && barrier_done) { |
| 140 | char b[BDEVNAME_SIZE]; |
| 141 | |
| 142 | printk(KERN_WARNING |
| 143 | "JBD: barrier-based sync failed on %s - " |
| 144 | "disabling barriers\n", |
| 145 | bdevname(journal->j_dev, b)); |
| 146 | spin_lock(&journal->j_state_lock); |
| 147 | journal->j_flags &= ~JFS_BARRIER; |
| 148 | spin_unlock(&journal->j_state_lock); |
| 149 | |
| 150 | /* And try again, without the barrier */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | set_buffer_uptodate(bh); |
| 152 | set_buffer_dirty(bh); |
| 153 | ret = sync_dirty_buffer(bh); |
| 154 | } |
| 155 | put_bh(bh); /* One for getblk() */ |
| 156 | journal_put_journal_head(descriptor); |
| 157 | |
| 158 | return (ret == -EIO); |
| 159 | } |
| 160 | |
Jan Kara | 3998b93 | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 161 | static void journal_do_submit_data(struct buffer_head **wbuf, int bufs) |
| 162 | { |
| 163 | int i; |
| 164 | |
| 165 | for (i = 0; i < bufs; i++) { |
| 166 | wbuf[i]->b_end_io = end_buffer_write_sync; |
| 167 | /* We use-up our safety reference in submit_bh() */ |
| 168 | submit_bh(WRITE, wbuf[i]); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Submit all the data buffers to disk |
| 174 | */ |
| 175 | static void journal_submit_data_buffers(journal_t *journal, |
| 176 | transaction_t *commit_transaction) |
| 177 | { |
| 178 | struct journal_head *jh; |
| 179 | struct buffer_head *bh; |
| 180 | int locked; |
| 181 | int bufs = 0; |
| 182 | struct buffer_head **wbuf = journal->j_wbuf; |
| 183 | |
| 184 | /* |
| 185 | * Whenever we unlock the journal and sleep, things can get added |
| 186 | * onto ->t_sync_datalist, so we have to keep looping back to |
| 187 | * write_out_data until we *know* that the list is empty. |
| 188 | * |
| 189 | * Cleanup any flushed data buffers from the data list. Even in |
| 190 | * abort mode, we want to flush this out as soon as possible. |
| 191 | */ |
| 192 | write_out_data: |
| 193 | cond_resched(); |
| 194 | spin_lock(&journal->j_list_lock); |
| 195 | |
| 196 | while (commit_transaction->t_sync_datalist) { |
| 197 | jh = commit_transaction->t_sync_datalist; |
| 198 | bh = jh2bh(jh); |
| 199 | locked = 0; |
| 200 | |
| 201 | /* Get reference just to make sure buffer does not disappear |
| 202 | * when we are forced to drop various locks */ |
| 203 | get_bh(bh); |
| 204 | /* If the buffer is dirty, we need to submit IO and hence |
| 205 | * we need the buffer lock. We try to lock the buffer without |
| 206 | * blocking. If we fail, we need to drop j_list_lock and do |
| 207 | * blocking lock_buffer(). |
| 208 | */ |
| 209 | if (buffer_dirty(bh)) { |
| 210 | if (test_set_buffer_locked(bh)) { |
| 211 | BUFFER_TRACE(bh, "needs blocking lock"); |
| 212 | spin_unlock(&journal->j_list_lock); |
| 213 | /* Write out all data to prevent deadlocks */ |
| 214 | journal_do_submit_data(wbuf, bufs); |
| 215 | bufs = 0; |
| 216 | lock_buffer(bh); |
| 217 | spin_lock(&journal->j_list_lock); |
| 218 | } |
| 219 | locked = 1; |
| 220 | } |
| 221 | /* We have to get bh_state lock. Again out of order, sigh. */ |
| 222 | if (!inverted_lock(journal, bh)) { |
| 223 | jbd_lock_bh_state(bh); |
| 224 | spin_lock(&journal->j_list_lock); |
| 225 | } |
| 226 | /* Someone already cleaned up the buffer? */ |
| 227 | if (!buffer_jbd(bh) |
| 228 | || jh->b_transaction != commit_transaction |
| 229 | || jh->b_jlist != BJ_SyncData) { |
| 230 | jbd_unlock_bh_state(bh); |
| 231 | if (locked) |
| 232 | unlock_buffer(bh); |
| 233 | BUFFER_TRACE(bh, "already cleaned up"); |
| 234 | put_bh(bh); |
| 235 | continue; |
| 236 | } |
| 237 | if (locked && test_clear_buffer_dirty(bh)) { |
| 238 | BUFFER_TRACE(bh, "needs writeout, adding to array"); |
| 239 | wbuf[bufs++] = bh; |
| 240 | __journal_file_buffer(jh, commit_transaction, |
| 241 | BJ_Locked); |
| 242 | jbd_unlock_bh_state(bh); |
| 243 | if (bufs == journal->j_wbufsize) { |
| 244 | spin_unlock(&journal->j_list_lock); |
| 245 | journal_do_submit_data(wbuf, bufs); |
| 246 | bufs = 0; |
| 247 | goto write_out_data; |
| 248 | } |
Hisashi Hifumi | 6f5a9da | 2006-12-22 01:11:50 -0800 | [diff] [blame] | 249 | } else if (!locked && buffer_locked(bh)) { |
| 250 | __journal_file_buffer(jh, commit_transaction, |
| 251 | BJ_Locked); |
| 252 | jbd_unlock_bh_state(bh); |
| 253 | put_bh(bh); |
| 254 | } else { |
Jan Kara | 3998b93 | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 255 | BUFFER_TRACE(bh, "writeout complete: unfile"); |
| 256 | __journal_unfile_buffer(jh); |
| 257 | jbd_unlock_bh_state(bh); |
| 258 | if (locked) |
| 259 | unlock_buffer(bh); |
| 260 | journal_remove_journal_head(bh); |
| 261 | /* Once for our safety reference, once for |
| 262 | * journal_remove_journal_head() */ |
| 263 | put_bh(bh); |
| 264 | put_bh(bh); |
| 265 | } |
| 266 | |
Nick Piggin | 95c354f | 2008-01-30 13:31:20 +0100 | [diff] [blame] | 267 | if (need_resched() || spin_needbreak(&journal->j_list_lock)) { |
Jan Kara | 3998b93 | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 268 | spin_unlock(&journal->j_list_lock); |
| 269 | goto write_out_data; |
| 270 | } |
| 271 | } |
| 272 | spin_unlock(&journal->j_list_lock); |
| 273 | journal_do_submit_data(wbuf, bufs); |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* |
| 277 | * journal_commit_transaction |
| 278 | * |
| 279 | * The primary function for committing a transaction to the log. This |
| 280 | * function is called by the journal thread to begin a complete commit. |
| 281 | */ |
| 282 | void journal_commit_transaction(journal_t *journal) |
| 283 | { |
| 284 | transaction_t *commit_transaction; |
| 285 | struct journal_head *jh, *new_jh, *descriptor; |
| 286 | struct buffer_head **wbuf = journal->j_wbuf; |
| 287 | int bufs; |
| 288 | int flags; |
| 289 | int err; |
| 290 | unsigned long blocknr; |
| 291 | char *tagp = NULL; |
| 292 | journal_header_t *header; |
| 293 | journal_block_tag_t *tag = NULL; |
| 294 | int space_left = 0; |
| 295 | int first_tag = 0; |
| 296 | int tag_flag; |
| 297 | int i; |
| 298 | |
| 299 | /* |
| 300 | * First job: lock down the current transaction and wait for |
| 301 | * all outstanding updates to complete. |
| 302 | */ |
| 303 | |
| 304 | #ifdef COMMIT_STATS |
| 305 | spin_lock(&journal->j_list_lock); |
| 306 | summarise_journal_usage(journal); |
| 307 | spin_unlock(&journal->j_list_lock); |
| 308 | #endif |
| 309 | |
| 310 | /* Do we need to erase the effects of a prior journal_flush? */ |
| 311 | if (journal->j_flags & JFS_FLUSHED) { |
| 312 | jbd_debug(3, "super block updated\n"); |
| 313 | journal_update_superblock(journal, 1); |
| 314 | } else { |
| 315 | jbd_debug(3, "superblock not updated\n"); |
| 316 | } |
| 317 | |
| 318 | J_ASSERT(journal->j_running_transaction != NULL); |
| 319 | J_ASSERT(journal->j_committing_transaction == NULL); |
| 320 | |
| 321 | commit_transaction = journal->j_running_transaction; |
| 322 | J_ASSERT(commit_transaction->t_state == T_RUNNING); |
| 323 | |
| 324 | jbd_debug(1, "JBD: starting commit of transaction %d\n", |
| 325 | commit_transaction->t_tid); |
| 326 | |
| 327 | spin_lock(&journal->j_state_lock); |
| 328 | commit_transaction->t_state = T_LOCKED; |
| 329 | |
| 330 | spin_lock(&commit_transaction->t_handle_lock); |
| 331 | while (commit_transaction->t_updates) { |
| 332 | DEFINE_WAIT(wait); |
| 333 | |
| 334 | prepare_to_wait(&journal->j_wait_updates, &wait, |
| 335 | TASK_UNINTERRUPTIBLE); |
| 336 | if (commit_transaction->t_updates) { |
| 337 | spin_unlock(&commit_transaction->t_handle_lock); |
| 338 | spin_unlock(&journal->j_state_lock); |
| 339 | schedule(); |
| 340 | spin_lock(&journal->j_state_lock); |
| 341 | spin_lock(&commit_transaction->t_handle_lock); |
| 342 | } |
| 343 | finish_wait(&journal->j_wait_updates, &wait); |
| 344 | } |
| 345 | spin_unlock(&commit_transaction->t_handle_lock); |
| 346 | |
| 347 | J_ASSERT (commit_transaction->t_outstanding_credits <= |
| 348 | journal->j_max_transaction_buffers); |
| 349 | |
| 350 | /* |
| 351 | * First thing we are allowed to do is to discard any remaining |
| 352 | * BJ_Reserved buffers. Note, it is _not_ permissible to assume |
| 353 | * that there are no such buffers: if a large filesystem |
| 354 | * operation like a truncate needs to split itself over multiple |
| 355 | * transactions, then it may try to do a journal_restart() while |
| 356 | * there are still BJ_Reserved buffers outstanding. These must |
| 357 | * be released cleanly from the current transaction. |
| 358 | * |
| 359 | * In this case, the filesystem must still reserve write access |
| 360 | * again before modifying the buffer in the new transaction, but |
| 361 | * we do not require it to remember exactly which old buffers it |
| 362 | * has reserved. This is consistent with the existing behaviour |
| 363 | * that multiple journal_get_write_access() calls to the same |
| 364 | * buffer are perfectly permissable. |
| 365 | */ |
| 366 | while (commit_transaction->t_reserved_list) { |
| 367 | jh = commit_transaction->t_reserved_list; |
| 368 | JBUFFER_TRACE(jh, "reserved, unused: refile"); |
| 369 | /* |
| 370 | * A journal_get_undo_access()+journal_release_buffer() may |
| 371 | * leave undo-committed data. |
| 372 | */ |
| 373 | if (jh->b_committed_data) { |
| 374 | struct buffer_head *bh = jh2bh(jh); |
| 375 | |
| 376 | jbd_lock_bh_state(bh); |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 377 | jbd_free(jh->b_committed_data, bh->b_size); |
Jesper Juhl | f99d49a | 2005-11-07 01:01:34 -0800 | [diff] [blame] | 378 | jh->b_committed_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | jbd_unlock_bh_state(bh); |
| 380 | } |
| 381 | journal_refile_buffer(journal, jh); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Now try to drop any written-back buffers from the journal's |
| 386 | * checkpoint lists. We do this *before* commit because it potentially |
| 387 | * frees some memory |
| 388 | */ |
| 389 | spin_lock(&journal->j_list_lock); |
| 390 | __journal_clean_checkpoint_list(journal); |
| 391 | spin_unlock(&journal->j_list_lock); |
| 392 | |
| 393 | jbd_debug (3, "JBD: commit phase 1\n"); |
| 394 | |
| 395 | /* |
| 396 | * Switch to a new revoke table. |
| 397 | */ |
| 398 | journal_switch_revoke_table(journal); |
| 399 | |
| 400 | commit_transaction->t_state = T_FLUSH; |
| 401 | journal->j_committing_transaction = commit_transaction; |
| 402 | journal->j_running_transaction = NULL; |
| 403 | commit_transaction->t_log_start = journal->j_head; |
| 404 | wake_up(&journal->j_wait_transaction_locked); |
| 405 | spin_unlock(&journal->j_state_lock); |
| 406 | |
| 407 | jbd_debug (3, "JBD: commit phase 2\n"); |
| 408 | |
| 409 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | * Now start flushing things to disk, in the order they appear |
| 411 | * on the transaction lists. Data blocks go first. |
| 412 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | err = 0; |
Jan Kara | 3998b93 | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 414 | journal_submit_data_buffers(journal, commit_transaction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * Wait for all previously submitted IO to complete. |
| 418 | */ |
Jan Kara | 3998b93 | 2006-09-25 23:30:53 -0700 | [diff] [blame] | 419 | spin_lock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | while (commit_transaction->t_locked_list) { |
| 421 | struct buffer_head *bh; |
| 422 | |
| 423 | jh = commit_transaction->t_locked_list->b_tprev; |
| 424 | bh = jh2bh(jh); |
| 425 | get_bh(bh); |
| 426 | if (buffer_locked(bh)) { |
| 427 | spin_unlock(&journal->j_list_lock); |
| 428 | wait_on_buffer(bh); |
| 429 | if (unlikely(!buffer_uptodate(bh))) |
| 430 | err = -EIO; |
| 431 | spin_lock(&journal->j_list_lock); |
| 432 | } |
| 433 | if (!inverted_lock(journal, bh)) { |
| 434 | put_bh(bh); |
| 435 | spin_lock(&journal->j_list_lock); |
| 436 | continue; |
| 437 | } |
| 438 | if (buffer_jbd(bh) && jh->b_jlist == BJ_Locked) { |
| 439 | __journal_unfile_buffer(jh); |
| 440 | jbd_unlock_bh_state(bh); |
| 441 | journal_remove_journal_head(bh); |
| 442 | put_bh(bh); |
| 443 | } else { |
| 444 | jbd_unlock_bh_state(bh); |
| 445 | } |
| 446 | put_bh(bh); |
| 447 | cond_resched_lock(&journal->j_list_lock); |
| 448 | } |
| 449 | spin_unlock(&journal->j_list_lock); |
| 450 | |
| 451 | if (err) |
Jan Kara | 7a266e7 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 452 | journal_abort(journal, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | journal_write_revoke_records(journal, commit_transaction); |
| 455 | |
| 456 | jbd_debug(3, "JBD: commit phase 2\n"); |
| 457 | |
| 458 | /* |
| 459 | * If we found any dirty or locked buffers, then we should have |
| 460 | * looped back up to the write_out_data label. If there weren't |
| 461 | * any then journal_clean_data_list should have wiped the list |
| 462 | * clean by now, so check that it is in fact empty. |
| 463 | */ |
| 464 | J_ASSERT (commit_transaction->t_sync_datalist == NULL); |
| 465 | |
| 466 | jbd_debug (3, "JBD: commit phase 3\n"); |
| 467 | |
| 468 | /* |
| 469 | * Way to go: we have now written out all of the data for a |
| 470 | * transaction! Now comes the tricky part: we need to write out |
| 471 | * metadata. Loop over the transaction's entire buffer list: |
| 472 | */ |
| 473 | commit_transaction->t_state = T_COMMIT; |
| 474 | |
Josef Bacik | 5b9a499 | 2008-04-28 02:16:12 -0700 | [diff] [blame^] | 475 | J_ASSERT(commit_transaction->t_nr_buffers <= |
| 476 | commit_transaction->t_outstanding_credits); |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | descriptor = NULL; |
| 479 | bufs = 0; |
| 480 | while (commit_transaction->t_buffers) { |
| 481 | |
| 482 | /* Find the next buffer to be journaled... */ |
| 483 | |
| 484 | jh = commit_transaction->t_buffers; |
| 485 | |
| 486 | /* If we're in abort mode, we just un-journal the buffer and |
| 487 | release it for background writing. */ |
| 488 | |
| 489 | if (is_journal_aborted(journal)) { |
| 490 | JBUFFER_TRACE(jh, "journal is aborting: refile"); |
| 491 | journal_refile_buffer(journal, jh); |
| 492 | /* If that was the last one, we need to clean up |
| 493 | * any descriptor buffers which may have been |
| 494 | * already allocated, even if we are now |
| 495 | * aborting. */ |
| 496 | if (!commit_transaction->t_buffers) |
| 497 | goto start_journal_io; |
| 498 | continue; |
| 499 | } |
| 500 | |
| 501 | /* Make sure we have a descriptor block in which to |
| 502 | record the metadata buffer. */ |
| 503 | |
| 504 | if (!descriptor) { |
| 505 | struct buffer_head *bh; |
| 506 | |
| 507 | J_ASSERT (bufs == 0); |
| 508 | |
| 509 | jbd_debug(4, "JBD: get descriptor\n"); |
| 510 | |
| 511 | descriptor = journal_get_descriptor_buffer(journal); |
| 512 | if (!descriptor) { |
Jan Kara | 7a266e7 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 513 | journal_abort(journal, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | continue; |
| 515 | } |
| 516 | |
| 517 | bh = jh2bh(descriptor); |
| 518 | jbd_debug(4, "JBD: got buffer %llu (%p)\n", |
| 519 | (unsigned long long)bh->b_blocknr, bh->b_data); |
| 520 | header = (journal_header_t *)&bh->b_data[0]; |
| 521 | header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER); |
| 522 | header->h_blocktype = cpu_to_be32(JFS_DESCRIPTOR_BLOCK); |
| 523 | header->h_sequence = cpu_to_be32(commit_transaction->t_tid); |
| 524 | |
| 525 | tagp = &bh->b_data[sizeof(journal_header_t)]; |
| 526 | space_left = bh->b_size - sizeof(journal_header_t); |
| 527 | first_tag = 1; |
| 528 | set_buffer_jwrite(bh); |
| 529 | set_buffer_dirty(bh); |
| 530 | wbuf[bufs++] = bh; |
| 531 | |
| 532 | /* Record it so that we can wait for IO |
| 533 | completion later */ |
| 534 | BUFFER_TRACE(bh, "ph3: file as descriptor"); |
| 535 | journal_file_buffer(descriptor, commit_transaction, |
| 536 | BJ_LogCtl); |
| 537 | } |
| 538 | |
| 539 | /* Where is the buffer to be written? */ |
| 540 | |
| 541 | err = journal_next_log_block(journal, &blocknr); |
| 542 | /* If the block mapping failed, just abandon the buffer |
| 543 | and repeat this loop: we'll fall into the |
| 544 | refile-on-abort condition above. */ |
| 545 | if (err) { |
Jan Kara | 7a266e7 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 546 | journal_abort(journal, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | continue; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * start_this_handle() uses t_outstanding_credits to determine |
| 552 | * the free space in the log, but this counter is changed |
| 553 | * by journal_next_log_block() also. |
| 554 | */ |
| 555 | commit_transaction->t_outstanding_credits--; |
| 556 | |
| 557 | /* Bump b_count to prevent truncate from stumbling over |
| 558 | the shadowed buffer! @@@ This can go if we ever get |
| 559 | rid of the BJ_IO/BJ_Shadow pairing of buffers. */ |
| 560 | atomic_inc(&jh2bh(jh)->b_count); |
| 561 | |
| 562 | /* Make a temporary IO buffer with which to write it out |
| 563 | (this will requeue both the metadata buffer and the |
| 564 | temporary IO buffer). new_bh goes on BJ_IO*/ |
| 565 | |
| 566 | set_bit(BH_JWrite, &jh2bh(jh)->b_state); |
| 567 | /* |
| 568 | * akpm: journal_write_metadata_buffer() sets |
| 569 | * new_bh->b_transaction to commit_transaction. |
| 570 | * We need to clean this up before we release new_bh |
| 571 | * (which is of type BJ_IO) |
| 572 | */ |
| 573 | JBUFFER_TRACE(jh, "ph3: write metadata"); |
| 574 | flags = journal_write_metadata_buffer(commit_transaction, |
| 575 | jh, &new_jh, blocknr); |
| 576 | set_bit(BH_JWrite, &jh2bh(new_jh)->b_state); |
| 577 | wbuf[bufs++] = jh2bh(new_jh); |
| 578 | |
| 579 | /* Record the new block's tag in the current descriptor |
| 580 | buffer */ |
| 581 | |
| 582 | tag_flag = 0; |
| 583 | if (flags & 1) |
| 584 | tag_flag |= JFS_FLAG_ESCAPE; |
| 585 | if (!first_tag) |
| 586 | tag_flag |= JFS_FLAG_SAME_UUID; |
| 587 | |
| 588 | tag = (journal_block_tag_t *) tagp; |
| 589 | tag->t_blocknr = cpu_to_be32(jh2bh(jh)->b_blocknr); |
| 590 | tag->t_flags = cpu_to_be32(tag_flag); |
| 591 | tagp += sizeof(journal_block_tag_t); |
| 592 | space_left -= sizeof(journal_block_tag_t); |
| 593 | |
| 594 | if (first_tag) { |
| 595 | memcpy (tagp, journal->j_uuid, 16); |
| 596 | tagp += 16; |
| 597 | space_left -= 16; |
| 598 | first_tag = 0; |
| 599 | } |
| 600 | |
| 601 | /* If there's no more to do, or if the descriptor is full, |
| 602 | let the IO rip! */ |
| 603 | |
| 604 | if (bufs == journal->j_wbufsize || |
| 605 | commit_transaction->t_buffers == NULL || |
| 606 | space_left < sizeof(journal_block_tag_t) + 16) { |
| 607 | |
| 608 | jbd_debug(4, "JBD: Submit %d IOs\n", bufs); |
| 609 | |
| 610 | /* Write an end-of-descriptor marker before |
| 611 | submitting the IOs. "tag" still points to |
| 612 | the last tag we set up. */ |
| 613 | |
| 614 | tag->t_flags |= cpu_to_be32(JFS_FLAG_LAST_TAG); |
| 615 | |
| 616 | start_journal_io: |
| 617 | for (i = 0; i < bufs; i++) { |
| 618 | struct buffer_head *bh = wbuf[i]; |
| 619 | lock_buffer(bh); |
| 620 | clear_buffer_dirty(bh); |
| 621 | set_buffer_uptodate(bh); |
| 622 | bh->b_end_io = journal_end_buffer_io_sync; |
| 623 | submit_bh(WRITE, bh); |
| 624 | } |
| 625 | cond_resched(); |
| 626 | |
| 627 | /* Force a new descriptor to be generated next |
| 628 | time round the loop. */ |
| 629 | descriptor = NULL; |
| 630 | bufs = 0; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* Lo and behold: we have just managed to send a transaction to |
| 635 | the log. Before we can commit it, wait for the IO so far to |
| 636 | complete. Control buffers being written are on the |
| 637 | transaction's t_log_list queue, and metadata buffers are on |
| 638 | the t_iobuf_list queue. |
| 639 | |
| 640 | Wait for the buffers in reverse order. That way we are |
| 641 | less likely to be woken up until all IOs have completed, and |
| 642 | so we incur less scheduling load. |
| 643 | */ |
| 644 | |
| 645 | jbd_debug(3, "JBD: commit phase 4\n"); |
| 646 | |
| 647 | /* |
| 648 | * akpm: these are BJ_IO, and j_list_lock is not needed. |
| 649 | * See __journal_try_to_free_buffer. |
| 650 | */ |
| 651 | wait_for_iobuf: |
| 652 | while (commit_transaction->t_iobuf_list != NULL) { |
| 653 | struct buffer_head *bh; |
| 654 | |
| 655 | jh = commit_transaction->t_iobuf_list->b_tprev; |
| 656 | bh = jh2bh(jh); |
| 657 | if (buffer_locked(bh)) { |
| 658 | wait_on_buffer(bh); |
| 659 | goto wait_for_iobuf; |
| 660 | } |
| 661 | if (cond_resched()) |
| 662 | goto wait_for_iobuf; |
| 663 | |
| 664 | if (unlikely(!buffer_uptodate(bh))) |
| 665 | err = -EIO; |
| 666 | |
| 667 | clear_buffer_jwrite(bh); |
| 668 | |
| 669 | JBUFFER_TRACE(jh, "ph4: unfile after journal write"); |
| 670 | journal_unfile_buffer(journal, jh); |
| 671 | |
| 672 | /* |
| 673 | * ->t_iobuf_list should contain only dummy buffer_heads |
| 674 | * which were created by journal_write_metadata_buffer(). |
| 675 | */ |
| 676 | BUFFER_TRACE(bh, "dumping temporary bh"); |
| 677 | journal_put_journal_head(jh); |
| 678 | __brelse(bh); |
| 679 | J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0); |
| 680 | free_buffer_head(bh); |
| 681 | |
| 682 | /* We also have to unlock and free the corresponding |
| 683 | shadowed buffer */ |
| 684 | jh = commit_transaction->t_shadow_list->b_tprev; |
| 685 | bh = jh2bh(jh); |
| 686 | clear_bit(BH_JWrite, &bh->b_state); |
| 687 | J_ASSERT_BH(bh, buffer_jbddirty(bh)); |
| 688 | |
| 689 | /* The metadata is now released for reuse, but we need |
| 690 | to remember it against this transaction so that when |
| 691 | we finally commit, we can do any checkpointing |
| 692 | required. */ |
| 693 | JBUFFER_TRACE(jh, "file as BJ_Forget"); |
| 694 | journal_file_buffer(jh, commit_transaction, BJ_Forget); |
| 695 | /* Wake up any transactions which were waiting for this |
| 696 | IO to complete */ |
| 697 | wake_up_bit(&bh->b_state, BH_Unshadow); |
| 698 | JBUFFER_TRACE(jh, "brelse shadowed buffer"); |
| 699 | __brelse(bh); |
| 700 | } |
| 701 | |
| 702 | J_ASSERT (commit_transaction->t_shadow_list == NULL); |
| 703 | |
| 704 | jbd_debug(3, "JBD: commit phase 5\n"); |
| 705 | |
| 706 | /* Here we wait for the revoke record and descriptor record buffers */ |
| 707 | wait_for_ctlbuf: |
| 708 | while (commit_transaction->t_log_list != NULL) { |
| 709 | struct buffer_head *bh; |
| 710 | |
| 711 | jh = commit_transaction->t_log_list->b_tprev; |
| 712 | bh = jh2bh(jh); |
| 713 | if (buffer_locked(bh)) { |
| 714 | wait_on_buffer(bh); |
| 715 | goto wait_for_ctlbuf; |
| 716 | } |
| 717 | if (cond_resched()) |
| 718 | goto wait_for_ctlbuf; |
| 719 | |
| 720 | if (unlikely(!buffer_uptodate(bh))) |
| 721 | err = -EIO; |
| 722 | |
| 723 | BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile"); |
| 724 | clear_buffer_jwrite(bh); |
| 725 | journal_unfile_buffer(journal, jh); |
| 726 | journal_put_journal_head(jh); |
| 727 | __brelse(bh); /* One for getblk */ |
| 728 | /* AKPM: bforget here */ |
| 729 | } |
| 730 | |
| 731 | jbd_debug(3, "JBD: commit phase 6\n"); |
| 732 | |
| 733 | if (journal_write_commit_record(journal, commit_transaction)) |
| 734 | err = -EIO; |
| 735 | |
| 736 | if (err) |
Jan Kara | 7a266e7 | 2007-10-18 23:39:22 -0700 | [diff] [blame] | 737 | journal_abort(journal, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
| 739 | /* End of a transaction! Finally, we can do checkpoint |
| 740 | processing: any buffers committed as a result of this |
| 741 | transaction can be removed from any checkpoint list it was on |
| 742 | before. */ |
| 743 | |
| 744 | jbd_debug(3, "JBD: commit phase 7\n"); |
| 745 | |
| 746 | J_ASSERT(commit_transaction->t_sync_datalist == NULL); |
| 747 | J_ASSERT(commit_transaction->t_buffers == NULL); |
| 748 | J_ASSERT(commit_transaction->t_checkpoint_list == NULL); |
| 749 | J_ASSERT(commit_transaction->t_iobuf_list == NULL); |
| 750 | J_ASSERT(commit_transaction->t_shadow_list == NULL); |
| 751 | J_ASSERT(commit_transaction->t_log_list == NULL); |
| 752 | |
| 753 | restart_loop: |
Jan Kara | e6c9f5c | 2005-09-06 15:19:09 -0700 | [diff] [blame] | 754 | /* |
| 755 | * As there are other places (journal_unmap_buffer()) adding buffers |
| 756 | * to this list we have to be careful and hold the j_list_lock. |
| 757 | */ |
| 758 | spin_lock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | while (commit_transaction->t_forget) { |
| 760 | transaction_t *cp_transaction; |
| 761 | struct buffer_head *bh; |
| 762 | |
| 763 | jh = commit_transaction->t_forget; |
Jan Kara | e6c9f5c | 2005-09-06 15:19:09 -0700 | [diff] [blame] | 764 | spin_unlock(&journal->j_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | bh = jh2bh(jh); |
| 766 | jbd_lock_bh_state(bh); |
| 767 | J_ASSERT_JH(jh, jh->b_transaction == commit_transaction || |
| 768 | jh->b_transaction == journal->j_running_transaction); |
| 769 | |
| 770 | /* |
| 771 | * If there is undo-protected committed data against |
| 772 | * this buffer, then we can remove it now. If it is a |
| 773 | * buffer needing such protection, the old frozen_data |
| 774 | * field now points to a committed version of the |
| 775 | * buffer, so rotate that field to the new committed |
| 776 | * data. |
| 777 | * |
| 778 | * Otherwise, we can just throw away the frozen data now. |
| 779 | */ |
| 780 | if (jh->b_committed_data) { |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 781 | jbd_free(jh->b_committed_data, bh->b_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | jh->b_committed_data = NULL; |
| 783 | if (jh->b_frozen_data) { |
| 784 | jh->b_committed_data = jh->b_frozen_data; |
| 785 | jh->b_frozen_data = NULL; |
| 786 | } |
| 787 | } else if (jh->b_frozen_data) { |
Mingming Cao | c089d49 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 788 | jbd_free(jh->b_frozen_data, bh->b_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | jh->b_frozen_data = NULL; |
| 790 | } |
| 791 | |
| 792 | spin_lock(&journal->j_list_lock); |
| 793 | cp_transaction = jh->b_cp_transaction; |
| 794 | if (cp_transaction) { |
| 795 | JBUFFER_TRACE(jh, "remove from old cp transaction"); |
| 796 | __journal_remove_checkpoint(jh); |
| 797 | } |
| 798 | |
| 799 | /* Only re-checkpoint the buffer_head if it is marked |
| 800 | * dirty. If the buffer was added to the BJ_Forget list |
| 801 | * by journal_forget, it may no longer be dirty and |
| 802 | * there's no point in keeping a checkpoint record for |
| 803 | * it. */ |
| 804 | |
| 805 | /* A buffer which has been freed while still being |
| 806 | * journaled by a previous transaction may end up still |
| 807 | * being dirty here, but we want to avoid writing back |
| 808 | * that buffer in the future now that the last use has |
| 809 | * been committed. That's not only a performance gain, |
| 810 | * it also stops aliasing problems if the buffer is left |
| 811 | * behind for writeback and gets reallocated for another |
| 812 | * use in a different page. */ |
| 813 | if (buffer_freed(bh)) { |
| 814 | clear_buffer_freed(bh); |
| 815 | clear_buffer_jbddirty(bh); |
| 816 | } |
| 817 | |
| 818 | if (buffer_jbddirty(bh)) { |
| 819 | JBUFFER_TRACE(jh, "add to new checkpointing trans"); |
| 820 | __journal_insert_checkpoint(jh, commit_transaction); |
| 821 | JBUFFER_TRACE(jh, "refile for checkpoint writeback"); |
| 822 | __journal_refile_buffer(jh); |
| 823 | jbd_unlock_bh_state(bh); |
| 824 | } else { |
| 825 | J_ASSERT_BH(bh, !buffer_dirty(bh)); |
Jan Kara | 9ada734 | 2006-06-23 02:05:25 -0700 | [diff] [blame] | 826 | /* The buffer on BJ_Forget list and not jbddirty means |
| 827 | * it has been freed by this transaction and hence it |
| 828 | * could not have been reallocated until this |
| 829 | * transaction has committed. *BUT* it could be |
| 830 | * reallocated once we have written all the data to |
| 831 | * disk and before we process the buffer on BJ_Forget |
| 832 | * list. */ |
| 833 | JBUFFER_TRACE(jh, "refile or unfile freed buffer"); |
| 834 | __journal_refile_buffer(jh); |
| 835 | if (!jh->b_transaction) { |
| 836 | jbd_unlock_bh_state(bh); |
| 837 | /* needs a brelse */ |
| 838 | journal_remove_journal_head(bh); |
| 839 | release_buffer_page(bh); |
| 840 | } else |
| 841 | jbd_unlock_bh_state(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
Jan Kara | e6c9f5c | 2005-09-06 15:19:09 -0700 | [diff] [blame] | 843 | cond_resched_lock(&journal->j_list_lock); |
| 844 | } |
| 845 | spin_unlock(&journal->j_list_lock); |
| 846 | /* |
Jan Kara | d4beaf4 | 2007-12-04 23:45:27 -0800 | [diff] [blame] | 847 | * This is a bit sleazy. We use j_list_lock to protect transition |
| 848 | * of a transaction into T_FINISHED state and calling |
| 849 | * __journal_drop_transaction(). Otherwise we could race with |
| 850 | * other checkpointing code processing the transaction... |
Jan Kara | e6c9f5c | 2005-09-06 15:19:09 -0700 | [diff] [blame] | 851 | */ |
| 852 | spin_lock(&journal->j_state_lock); |
| 853 | spin_lock(&journal->j_list_lock); |
| 854 | /* |
| 855 | * Now recheck if some buffers did not get attached to the transaction |
| 856 | * while the lock was dropped... |
| 857 | */ |
| 858 | if (commit_transaction->t_forget) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | spin_unlock(&journal->j_list_lock); |
Jan Kara | e6c9f5c | 2005-09-06 15:19:09 -0700 | [diff] [blame] | 860 | spin_unlock(&journal->j_state_lock); |
| 861 | goto restart_loop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /* Done with this transaction! */ |
| 865 | |
| 866 | jbd_debug(3, "JBD: commit phase 8\n"); |
| 867 | |
| 868 | J_ASSERT(commit_transaction->t_state == T_COMMIT); |
| 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | commit_transaction->t_state = T_FINISHED; |
| 871 | J_ASSERT(commit_transaction == journal->j_committing_transaction); |
| 872 | journal->j_commit_sequence = commit_transaction->t_tid; |
| 873 | journal->j_committing_transaction = NULL; |
| 874 | spin_unlock(&journal->j_state_lock); |
| 875 | |
Jan Kara | fe28e42 | 2007-07-15 23:37:18 -0700 | [diff] [blame] | 876 | if (commit_transaction->t_checkpoint_list == NULL && |
| 877 | commit_transaction->t_checkpoint_io_list == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | __journal_drop_transaction(journal, commit_transaction); |
| 879 | } else { |
| 880 | if (journal->j_checkpoint_transactions == NULL) { |
| 881 | journal->j_checkpoint_transactions = commit_transaction; |
| 882 | commit_transaction->t_cpnext = commit_transaction; |
| 883 | commit_transaction->t_cpprev = commit_transaction; |
| 884 | } else { |
| 885 | commit_transaction->t_cpnext = |
| 886 | journal->j_checkpoint_transactions; |
| 887 | commit_transaction->t_cpprev = |
| 888 | commit_transaction->t_cpnext->t_cpprev; |
| 889 | commit_transaction->t_cpnext->t_cpprev = |
| 890 | commit_transaction; |
| 891 | commit_transaction->t_cpprev->t_cpnext = |
| 892 | commit_transaction; |
| 893 | } |
| 894 | } |
| 895 | spin_unlock(&journal->j_list_lock); |
| 896 | |
| 897 | jbd_debug(1, "JBD: commit %d complete, head %d\n", |
| 898 | journal->j_commit_sequence, journal->j_tail_sequence); |
| 899 | |
| 900 | wake_up(&journal->j_wait_done_commit); |
| 901 | } |