1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * linux/fs/jbd2/transaction.c 4 * 5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 6 * 7 * Copyright 1998 Red Hat corp --- All Rights Reserved 8 * 9 * Generic filesystem transaction handling code; part of the ext2fs 10 * journaling system. 11 * 12 * This file manages transactions (compound commits managed by the 13 * journaling code) and handles (individual atomic operations by the 14 * filesystem). 15 */ 16 17 #include <linux/time.h> 18 #include <linux/fs.h> 19 #include <linux/jbd2.h> 20 #include <linux/errno.h> 21 #include <linux/slab.h> 22 #include <linux/timer.h> 23 #include <linux/mm.h> 24 #include <linux/highmem.h> 25 #include <linux/hrtimer.h> 26 #include <linux/backing-dev.h> 27 #include <linux/bug.h> 28 #include <linux/module.h> 29 #include <linux/sched/mm.h> 30 31 #include <trace/events/jbd2.h> 32 33 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh); 34 static void __jbd2_journal_unfile_buffer(struct journal_head *jh); 35 36 static struct kmem_cache *transaction_cache; 37 int __init jbd2_journal_init_transaction_cache(void) 38 { 39 J_ASSERT(!transaction_cache); 40 transaction_cache = kmem_cache_create("jbd2_transaction_s", 41 sizeof(transaction_t), 42 0, 43 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY, 44 NULL); 45 if (!transaction_cache) { 46 pr_emerg("JBD2: failed to create transaction cache\n"); 47 return -ENOMEM; 48 } 49 return 0; 50 } 51 52 void jbd2_journal_destroy_transaction_cache(void) 53 { 54 kmem_cache_destroy(transaction_cache); 55 transaction_cache = NULL; 56 } 57 58 void jbd2_journal_free_transaction(transaction_t *transaction) 59 { 60 if (unlikely(ZERO_OR_NULL_PTR(transaction))) 61 return; 62 kmem_cache_free(transaction_cache, transaction); 63 } 64 65 /* 66 * Base amount of descriptor blocks we reserve for each transaction. 67 */ 68 static int jbd2_descriptor_blocks_per_trans(journal_t *journal) 69 { 70 int tag_space = journal->j_blocksize - sizeof(journal_header_t); 71 int tags_per_block; 72 73 /* Subtract UUID */ 74 tag_space -= 16; 75 if (jbd2_journal_has_csum_v2or3(journal)) 76 tag_space -= sizeof(struct jbd2_journal_block_tail); 77 /* Commit code leaves a slack space of 16 bytes at the end of block */ 78 tags_per_block = (tag_space - 16) / journal_tag_bytes(journal); 79 /* 80 * Revoke descriptors are accounted separately so we need to reserve 81 * space for commit block and normal transaction descriptor blocks. 82 */ 83 return 1 + DIV_ROUND_UP(journal->j_max_transaction_buffers, 84 tags_per_block); 85 } 86 87 /* 88 * jbd2_get_transaction: obtain a new transaction_t object. 89 * 90 * Simply initialise a new transaction. Initialize it in 91 * RUNNING state and add it to the current journal (which should not 92 * have an existing running transaction: we only make a new transaction 93 * once we have started to commit the old one). 94 * 95 * Preconditions: 96 * The journal MUST be locked. We don't perform atomic mallocs on the 97 * new transaction and we can't block without protecting against other 98 * processes trying to touch the journal while it is in transition. 99 * 100 */ 101 102 static void jbd2_get_transaction(journal_t *journal, 103 transaction_t *transaction) 104 { 105 transaction->t_journal = journal; 106 transaction->t_state = T_RUNNING; 107 transaction->t_start_time = ktime_get(); 108 transaction->t_tid = journal->j_transaction_sequence++; 109 transaction->t_expires = jiffies + journal->j_commit_interval; 110 spin_lock_init(&transaction->t_handle_lock); 111 atomic_set(&transaction->t_updates, 0); 112 atomic_set(&transaction->t_outstanding_credits, 113 jbd2_descriptor_blocks_per_trans(journal) + 114 atomic_read(&journal->j_reserved_credits)); 115 atomic_set(&transaction->t_outstanding_revokes, 0); 116 atomic_set(&transaction->t_handle_count, 0); 117 INIT_LIST_HEAD(&transaction->t_inode_list); 118 INIT_LIST_HEAD(&transaction->t_private_list); 119 120 /* Set up the commit timer for the new transaction. */ 121 journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires); 122 add_timer(&journal->j_commit_timer); 123 124 J_ASSERT(journal->j_running_transaction == NULL); 125 journal->j_running_transaction = transaction; 126 transaction->t_max_wait = 0; 127 transaction->t_start = jiffies; 128 transaction->t_requested = 0; 129 } 130 131 /* 132 * Handle management. 133 * 134 * A handle_t is an object which represents a single atomic update to a 135 * filesystem, and which tracks all of the modifications which form part 136 * of that one update. 137 */ 138 139 /* 140 * Update transaction's maximum wait time, if debugging is enabled. 141 * 142 * In order for t_max_wait to be reliable, it must be protected by a 143 * lock. But doing so will mean that start_this_handle() can not be 144 * run in parallel on SMP systems, which limits our scalability. So 145 * unless debugging is enabled, we no longer update t_max_wait, which 146 * means that maximum wait time reported by the jbd2_run_stats 147 * tracepoint will always be zero. 148 */ 149 static inline void update_t_max_wait(transaction_t *transaction, 150 unsigned long ts) 151 { 152 #ifdef CONFIG_JBD2_DEBUG 153 if (jbd2_journal_enable_debug && 154 time_after(transaction->t_start, ts)) { 155 ts = jbd2_time_diff(ts, transaction->t_start); 156 spin_lock(&transaction->t_handle_lock); 157 if (ts > transaction->t_max_wait) 158 transaction->t_max_wait = ts; 159 spin_unlock(&transaction->t_handle_lock); 160 } 161 #endif 162 } 163 164 /* 165 * Wait until running transaction passes to T_FLUSH state and new transaction 166 * can thus be started. Also starts the commit if needed. The function expects 167 * running transaction to exist and releases j_state_lock. 168 */ 169 static void wait_transaction_locked(journal_t *journal) 170 __releases(journal->j_state_lock) 171 { 172 DEFINE_WAIT(wait); 173 int need_to_start; 174 tid_t tid = journal->j_running_transaction->t_tid; 175 176 prepare_to_wait(&journal->j_wait_transaction_locked, &wait, 177 TASK_UNINTERRUPTIBLE); 178 need_to_start = !tid_geq(journal->j_commit_request, tid); 179 read_unlock(&journal->j_state_lock); 180 if (need_to_start) 181 jbd2_log_start_commit(journal, tid); 182 jbd2_might_wait_for_commit(journal); 183 schedule(); 184 finish_wait(&journal->j_wait_transaction_locked, &wait); 185 } 186 187 /* 188 * Wait until running transaction transitions from T_SWITCH to T_FLUSH 189 * state and new transaction can thus be started. The function releases 190 * j_state_lock. 191 */ 192 static void wait_transaction_switching(journal_t *journal) 193 __releases(journal->j_state_lock) 194 { 195 DEFINE_WAIT(wait); 196 197 if (WARN_ON(!journal->j_running_transaction || 198 journal->j_running_transaction->t_state != T_SWITCH)) { 199 read_unlock(&journal->j_state_lock); 200 return; 201 } 202 prepare_to_wait(&journal->j_wait_transaction_locked, &wait, 203 TASK_UNINTERRUPTIBLE); 204 read_unlock(&journal->j_state_lock); 205 /* 206 * We don't call jbd2_might_wait_for_commit() here as there's no 207 * waiting for outstanding handles happening anymore in T_SWITCH state 208 * and handling of reserved handles actually relies on that for 209 * correctness. 210 */ 211 schedule(); 212 finish_wait(&journal->j_wait_transaction_locked, &wait); 213 } 214 215 static void sub_reserved_credits(journal_t *journal, int blocks) 216 { 217 atomic_sub(blocks, &journal->j_reserved_credits); 218 wake_up(&journal->j_wait_reserved); 219 } 220 221 /* 222 * Wait until we can add credits for handle to the running transaction. Called 223 * with j_state_lock held for reading. Returns 0 if handle joined the running 224 * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and 225 * caller must retry. 226 */ 227 static int add_transaction_credits(journal_t *journal, int blocks, 228 int rsv_blocks) 229 { 230 transaction_t *t = journal->j_running_transaction; 231 int needed; 232 int total = blocks + rsv_blocks; 233 234 /* 235 * If the current transaction is locked down for commit, wait 236 * for the lock to be released. 237 */ 238 if (t->t_state != T_RUNNING) { 239 WARN_ON_ONCE(t->t_state >= T_FLUSH); 240 wait_transaction_locked(journal); 241 return 1; 242 } 243 244 /* 245 * If there is not enough space left in the log to write all 246 * potential buffers requested by this operation, we need to 247 * stall pending a log checkpoint to free some more log space. 248 */ 249 needed = atomic_add_return(total, &t->t_outstanding_credits); 250 if (needed > journal->j_max_transaction_buffers) { 251 /* 252 * If the current transaction is already too large, 253 * then start to commit it: we can then go back and 254 * attach this handle to a new transaction. 255 */ 256 atomic_sub(total, &t->t_outstanding_credits); 257 258 /* 259 * Is the number of reserved credits in the current transaction too 260 * big to fit this handle? Wait until reserved credits are freed. 261 */ 262 if (atomic_read(&journal->j_reserved_credits) + total > 263 journal->j_max_transaction_buffers) { 264 read_unlock(&journal->j_state_lock); 265 jbd2_might_wait_for_commit(journal); 266 wait_event(journal->j_wait_reserved, 267 atomic_read(&journal->j_reserved_credits) + total <= 268 journal->j_max_transaction_buffers); 269 return 1; 270 } 271 272 wait_transaction_locked(journal); 273 return 1; 274 } 275 276 /* 277 * The commit code assumes that it can get enough log space 278 * without forcing a checkpoint. This is *critical* for 279 * correctness: a checkpoint of a buffer which is also 280 * associated with a committing transaction creates a deadlock, 281 * so commit simply cannot force through checkpoints. 282 * 283 * We must therefore ensure the necessary space in the journal 284 * *before* starting to dirty potentially checkpointed buffers 285 * in the new transaction. 286 */ 287 if (jbd2_log_space_left(journal) < journal->j_max_transaction_buffers) { 288 atomic_sub(total, &t->t_outstanding_credits); 289 read_unlock(&journal->j_state_lock); 290 jbd2_might_wait_for_commit(journal); 291 write_lock(&journal->j_state_lock); 292 if (jbd2_log_space_left(journal) < 293 journal->j_max_transaction_buffers) 294 __jbd2_log_wait_for_space(journal); 295 write_unlock(&journal->j_state_lock); 296 return 1; 297 } 298 299 /* No reservation? We are done... */ 300 if (!rsv_blocks) 301 return 0; 302 303 needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits); 304 /* We allow at most half of a transaction to be reserved */ 305 if (needed > journal->j_max_transaction_buffers / 2) { 306 sub_reserved_credits(journal, rsv_blocks); 307 atomic_sub(total, &t->t_outstanding_credits); 308 read_unlock(&journal->j_state_lock); 309 jbd2_might_wait_for_commit(journal); 310 wait_event(journal->j_wait_reserved, 311 atomic_read(&journal->j_reserved_credits) + rsv_blocks 312 <= journal->j_max_transaction_buffers / 2); 313 return 1; 314 } 315 return 0; 316 } 317 318 /* 319 * start_this_handle: Given a handle, deal with any locking or stalling 320 * needed to make sure that there is enough journal space for the handle 321 * to begin. Attach the handle to a transaction and set up the 322 * transaction's buffer credits. 323 */ 324 325 static int start_this_handle(journal_t *journal, handle_t *handle, 326 gfp_t gfp_mask) 327 { 328 transaction_t *transaction, *new_transaction = NULL; 329 int blocks = handle->h_total_credits; 330 int rsv_blocks = 0; 331 unsigned long ts = jiffies; 332 333 if (handle->h_rsv_handle) 334 rsv_blocks = handle->h_rsv_handle->h_total_credits; 335 336 /* 337 * Limit the number of reserved credits to 1/2 of maximum transaction 338 * size and limit the number of total credits to not exceed maximum 339 * transaction size per operation. 340 */ 341 if ((rsv_blocks > journal->j_max_transaction_buffers / 2) || 342 (rsv_blocks + blocks > journal->j_max_transaction_buffers)) { 343 printk(KERN_ERR "JBD2: %s wants too many credits " 344 "credits:%d rsv_credits:%d max:%d\n", 345 current->comm, blocks, rsv_blocks, 346 journal->j_max_transaction_buffers); 347 WARN_ON(1); 348 return -ENOSPC; 349 } 350 351 alloc_transaction: 352 if (!journal->j_running_transaction) { 353 /* 354 * If __GFP_FS is not present, then we may be being called from 355 * inside the fs writeback layer, so we MUST NOT fail. 356 */ 357 if ((gfp_mask & __GFP_FS) == 0) 358 gfp_mask |= __GFP_NOFAIL; 359 new_transaction = kmem_cache_zalloc(transaction_cache, 360 gfp_mask); 361 if (!new_transaction) 362 return -ENOMEM; 363 } 364 365 jbd_debug(3, "New handle %p going live.\n", handle); 366 367 /* 368 * We need to hold j_state_lock until t_updates has been incremented, 369 * for proper journal barrier handling 370 */ 371 repeat: 372 read_lock(&journal->j_state_lock); 373 BUG_ON(journal->j_flags & JBD2_UNMOUNT); 374 if (is_journal_aborted(journal) || 375 (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) { 376 read_unlock(&journal->j_state_lock); 377 jbd2_journal_free_transaction(new_transaction); 378 return -EROFS; 379 } 380 381 /* 382 * Wait on the journal's transaction barrier if necessary. Specifically 383 * we allow reserved handles to proceed because otherwise commit could 384 * deadlock on page writeback not being able to complete. 385 */ 386 if (!handle->h_reserved && journal->j_barrier_count) { 387 read_unlock(&journal->j_state_lock); 388 wait_event(journal->j_wait_transaction_locked, 389 journal->j_barrier_count == 0); 390 goto repeat; 391 } 392 393 if (!journal->j_running_transaction) { 394 read_unlock(&journal->j_state_lock); 395 if (!new_transaction) 396 goto alloc_transaction; 397 write_lock(&journal->j_state_lock); 398 if (!journal->j_running_transaction && 399 (handle->h_reserved || !journal->j_barrier_count)) { 400 jbd2_get_transaction(journal, new_transaction); 401 new_transaction = NULL; 402 } 403 write_unlock(&journal->j_state_lock); 404 goto repeat; 405 } 406 407 transaction = journal->j_running_transaction; 408 409 if (!handle->h_reserved) { 410 /* We may have dropped j_state_lock - restart in that case */ 411 if (add_transaction_credits(journal, blocks, rsv_blocks)) 412 goto repeat; 413 } else { 414 /* 415 * We have handle reserved so we are allowed to join T_LOCKED 416 * transaction and we don't have to check for transaction size 417 * and journal space. But we still have to wait while running 418 * transaction is being switched to a committing one as it 419 * won't wait for any handles anymore. 420 */ 421 if (transaction->t_state == T_SWITCH) { 422 wait_transaction_switching(journal); 423 goto repeat; 424 } 425 sub_reserved_credits(journal, blocks); 426 handle->h_reserved = 0; 427 } 428 429 /* OK, account for the buffers that this operation expects to 430 * use and add the handle to the running transaction. 431 */ 432 update_t_max_wait(transaction, ts); 433 handle->h_transaction = transaction; 434 handle->h_requested_credits = blocks; 435 handle->h_revoke_credits_requested = handle->h_revoke_credits; 436 handle->h_start_jiffies = jiffies; 437 atomic_inc(&transaction->t_updates); 438 atomic_inc(&transaction->t_handle_count); 439 jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n", 440 handle, blocks, 441 atomic_read(&transaction->t_outstanding_credits), 442 jbd2_log_space_left(journal)); 443 read_unlock(&journal->j_state_lock); 444 current->journal_info = handle; 445 446 rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_); 447 jbd2_journal_free_transaction(new_transaction); 448 /* 449 * Ensure that no allocations done while the transaction is open are 450 * going to recurse back to the fs layer. 451 */ 452 handle->saved_alloc_context = memalloc_nofs_save(); 453 return 0; 454 } 455 456 /* Allocate a new handle. This should probably be in a slab... */ 457 static handle_t *new_handle(int nblocks) 458 { 459 handle_t *handle = jbd2_alloc_handle(GFP_NOFS); 460 if (!handle) 461 return NULL; 462 handle->h_total_credits = nblocks; 463 handle->h_ref = 1; 464 465 return handle; 466 } 467 468 handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks, 469 int revoke_records, gfp_t gfp_mask, 470 unsigned int type, unsigned int line_no) 471 { 472 handle_t *handle = journal_current_handle(); 473 int err; 474 475 if (!journal) 476 return ERR_PTR(-EROFS); 477 478 if (handle) { 479 J_ASSERT(handle->h_transaction->t_journal == journal); 480 handle->h_ref++; 481 return handle; 482 } 483 484 nblocks += DIV_ROUND_UP(revoke_records, 485 journal->j_revoke_records_per_block); 486 handle = new_handle(nblocks); 487 if (!handle) 488 return ERR_PTR(-ENOMEM); 489 if (rsv_blocks) { 490 handle_t *rsv_handle; 491 492 rsv_handle = new_handle(rsv_blocks); 493 if (!rsv_handle) { 494 jbd2_free_handle(handle); 495 return ERR_PTR(-ENOMEM); 496 } 497 rsv_handle->h_reserved = 1; 498 rsv_handle->h_journal = journal; 499 handle->h_rsv_handle = rsv_handle; 500 } 501 handle->h_revoke_credits = revoke_records; 502 503 err = start_this_handle(journal, handle, gfp_mask); 504 if (err < 0) { 505 if (handle->h_rsv_handle) 506 jbd2_free_handle(handle->h_rsv_handle); 507 jbd2_free_handle(handle); 508 return ERR_PTR(err); 509 } 510 handle->h_type = type; 511 handle->h_line_no = line_no; 512 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev, 513 handle->h_transaction->t_tid, type, 514 line_no, nblocks); 515 516 return handle; 517 } 518 EXPORT_SYMBOL(jbd2__journal_start); 519 520 521 /** 522 * handle_t *jbd2_journal_start() - Obtain a new handle. 523 * @journal: Journal to start transaction on. 524 * @nblocks: number of block buffer we might modify 525 * 526 * We make sure that the transaction can guarantee at least nblocks of 527 * modified buffers in the log. We block until the log can guarantee 528 * that much space. Additionally, if rsv_blocks > 0, we also create another 529 * handle with rsv_blocks reserved blocks in the journal. This handle is 530 * stored in h_rsv_handle. It is not attached to any particular transaction 531 * and thus doesn't block transaction commit. If the caller uses this reserved 532 * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop() 533 * on the parent handle will dispose the reserved one. Reserved handle has to 534 * be converted to a normal handle using jbd2_journal_start_reserved() before 535 * it can be used. 536 * 537 * Return a pointer to a newly allocated handle, or an ERR_PTR() value 538 * on failure. 539 */ 540 handle_t *jbd2_journal_start(journal_t *journal, int nblocks) 541 { 542 return jbd2__journal_start(journal, nblocks, 0, 0, GFP_NOFS, 0, 0); 543 } 544 EXPORT_SYMBOL(jbd2_journal_start); 545 546 static void __jbd2_journal_unreserve_handle(handle_t *handle, transaction_t *t) 547 { 548 journal_t *journal = handle->h_journal; 549 550 WARN_ON(!handle->h_reserved); 551 sub_reserved_credits(journal, handle->h_total_credits); 552 if (t) 553 atomic_sub(handle->h_total_credits, &t->t_outstanding_credits); 554 } 555 556 void jbd2_journal_free_reserved(handle_t *handle) 557 { 558 journal_t *journal = handle->h_journal; 559 560 /* Get j_state_lock to pin running transaction if it exists */ 561 read_lock(&journal->j_state_lock); 562 __jbd2_journal_unreserve_handle(handle, journal->j_running_transaction); 563 read_unlock(&journal->j_state_lock); 564 jbd2_free_handle(handle); 565 } 566 EXPORT_SYMBOL(jbd2_journal_free_reserved); 567 568 /** 569 * int jbd2_journal_start_reserved() - start reserved handle 570 * @handle: handle to start 571 * @type: for handle statistics 572 * @line_no: for handle statistics 573 * 574 * Start handle that has been previously reserved with jbd2_journal_reserve(). 575 * This attaches @handle to the running transaction (or creates one if there's 576 * not transaction running). Unlike jbd2_journal_start() this function cannot 577 * block on journal commit, checkpointing, or similar stuff. It can block on 578 * memory allocation or frozen journal though. 579 * 580 * Return 0 on success, non-zero on error - handle is freed in that case. 581 */ 582 int jbd2_journal_start_reserved(handle_t *handle, unsigned int type, 583 unsigned int line_no) 584 { 585 journal_t *journal = handle->h_journal; 586 int ret = -EIO; 587 588 if (WARN_ON(!handle->h_reserved)) { 589 /* Someone passed in normal handle? Just stop it. */ 590 jbd2_journal_stop(handle); 591 return ret; 592 } 593 /* 594 * Usefulness of mixing of reserved and unreserved handles is 595 * questionable. So far nobody seems to need it so just error out. 596 */ 597 if (WARN_ON(current->journal_info)) { 598 jbd2_journal_free_reserved(handle); 599 return ret; 600 } 601 602 handle->h_journal = NULL; 603 /* 604 * GFP_NOFS is here because callers are likely from writeback or 605 * similarly constrained call sites 606 */ 607 ret = start_this_handle(journal, handle, GFP_NOFS); 608 if (ret < 0) { 609 handle->h_journal = journal; 610 jbd2_journal_free_reserved(handle); 611 return ret; 612 } 613 handle->h_type = type; 614 handle->h_line_no = line_no; 615 trace_jbd2_handle_start(journal->j_fs_dev->bd_dev, 616 handle->h_transaction->t_tid, type, 617 line_no, handle->h_total_credits); 618 return 0; 619 } 620 EXPORT_SYMBOL(jbd2_journal_start_reserved); 621 622 /** 623 * int jbd2_journal_extend() - extend buffer credits. 624 * @handle: handle to 'extend' 625 * @nblocks: nr blocks to try to extend by. 626 * @revoke_records: number of revoke records to try to extend by. 627 * 628 * Some transactions, such as large extends and truncates, can be done 629 * atomically all at once or in several stages. The operation requests 630 * a credit for a number of buffer modifications in advance, but can 631 * extend its credit if it needs more. 632 * 633 * jbd2_journal_extend tries to give the running handle more buffer credits. 634 * It does not guarantee that allocation - this is a best-effort only. 635 * The calling process MUST be able to deal cleanly with a failure to 636 * extend here. 637 * 638 * Return 0 on success, non-zero on failure. 639 * 640 * return code < 0 implies an error 641 * return code > 0 implies normal transaction-full status. 642 */ 643 int jbd2_journal_extend(handle_t *handle, int nblocks, int revoke_records) 644 { 645 transaction_t *transaction = handle->h_transaction; 646 journal_t *journal; 647 int result; 648 int wanted; 649 650 if (is_handle_aborted(handle)) 651 return -EROFS; 652 journal = transaction->t_journal; 653 654 result = 1; 655 656 read_lock(&journal->j_state_lock); 657 658 /* Don't extend a locked-down transaction! */ 659 if (transaction->t_state != T_RUNNING) { 660 jbd_debug(3, "denied handle %p %d blocks: " 661 "transaction not running\n", handle, nblocks); 662 goto error_out; 663 } 664 665 nblocks += DIV_ROUND_UP( 666 handle->h_revoke_credits_requested + revoke_records, 667 journal->j_revoke_records_per_block) - 668 DIV_ROUND_UP( 669 handle->h_revoke_credits_requested, 670 journal->j_revoke_records_per_block); 671 spin_lock(&transaction->t_handle_lock); 672 wanted = atomic_add_return(nblocks, 673 &transaction->t_outstanding_credits); 674 675 if (wanted > journal->j_max_transaction_buffers) { 676 jbd_debug(3, "denied handle %p %d blocks: " 677 "transaction too large\n", handle, nblocks); 678 atomic_sub(nblocks, &transaction->t_outstanding_credits); 679 goto unlock; 680 } 681 682 trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev, 683 transaction->t_tid, 684 handle->h_type, handle->h_line_no, 685 handle->h_total_credits, 686 nblocks); 687 688 handle->h_total_credits += nblocks; 689 handle->h_requested_credits += nblocks; 690 handle->h_revoke_credits += revoke_records; 691 handle->h_revoke_credits_requested += revoke_records; 692 result = 0; 693 694 jbd_debug(3, "extended handle %p by %d\n", handle, nblocks); 695 unlock: 696 spin_unlock(&transaction->t_handle_lock); 697 error_out: 698 read_unlock(&journal->j_state_lock); 699 return result; 700 } 701 702 static void stop_this_handle(handle_t *handle) 703 { 704 transaction_t *transaction = handle->h_transaction; 705 journal_t *journal = transaction->t_journal; 706 int revokes; 707 708 J_ASSERT(journal_current_handle() == handle); 709 J_ASSERT(atomic_read(&transaction->t_updates) > 0); 710 current->journal_info = NULL; 711 /* 712 * Subtract necessary revoke descriptor blocks from handle credits. We 713 * take care to account only for revoke descriptor blocks the 714 * transaction will really need as large sequences of transactions with 715 * small numbers of revokes are relatively common. 716 */ 717 revokes = handle->h_revoke_credits_requested - handle->h_revoke_credits; 718 if (revokes) { 719 int t_revokes, revoke_descriptors; 720 int rr_per_blk = journal->j_revoke_records_per_block; 721 722 WARN_ON_ONCE(DIV_ROUND_UP(revokes, rr_per_blk) 723 > handle->h_total_credits); 724 t_revokes = atomic_add_return(revokes, 725 &transaction->t_outstanding_revokes); 726 revoke_descriptors = 727 DIV_ROUND_UP(t_revokes, rr_per_blk) - 728 DIV_ROUND_UP(t_revokes - revokes, rr_per_blk); 729 handle->h_total_credits -= revoke_descriptors; 730 } 731 atomic_sub(handle->h_total_credits, 732 &transaction->t_outstanding_credits); 733 if (handle->h_rsv_handle) 734 __jbd2_journal_unreserve_handle(handle->h_rsv_handle, 735 transaction); 736 if (atomic_dec_and_test(&transaction->t_updates)) 737 wake_up(&journal->j_wait_updates); 738 739 rwsem_release(&journal->j_trans_commit_map, _THIS_IP_); 740 /* 741 * Scope of the GFP_NOFS context is over here and so we can restore the 742 * original alloc context. 743 */ 744 memalloc_nofs_restore(handle->saved_alloc_context); 745 } 746 747 /** 748 * int jbd2_journal_restart() - restart a handle . 749 * @handle: handle to restart 750 * @nblocks: nr credits requested 751 * @revoke_records: number of revoke record credits requested 752 * @gfp_mask: memory allocation flags (for start_this_handle) 753 * 754 * Restart a handle for a multi-transaction filesystem 755 * operation. 756 * 757 * If the jbd2_journal_extend() call above fails to grant new buffer credits 758 * to a running handle, a call to jbd2_journal_restart will commit the 759 * handle's transaction so far and reattach the handle to a new 760 * transaction capable of guaranteeing the requested number of 761 * credits. We preserve reserved handle if there's any attached to the 762 * passed in handle. 763 */ 764 int jbd2__journal_restart(handle_t *handle, int nblocks, int revoke_records, 765 gfp_t gfp_mask) 766 { 767 transaction_t *transaction = handle->h_transaction; 768 journal_t *journal; 769 tid_t tid; 770 int need_to_start; 771 int ret; 772 773 /* If we've had an abort of any type, don't even think about 774 * actually doing the restart! */ 775 if (is_handle_aborted(handle)) 776 return 0; 777 journal = transaction->t_journal; 778 tid = transaction->t_tid; 779 780 /* 781 * First unlink the handle from its current transaction, and start the 782 * commit on that. 783 */ 784 jbd_debug(2, "restarting handle %p\n", handle); 785 stop_this_handle(handle); 786 handle->h_transaction = NULL; 787 788 /* 789 * TODO: If we use READ_ONCE / WRITE_ONCE for j_commit_request we can 790 * get rid of pointless j_state_lock traffic like this. 791 */ 792 read_lock(&journal->j_state_lock); 793 need_to_start = !tid_geq(journal->j_commit_request, tid); 794 read_unlock(&journal->j_state_lock); 795 if (need_to_start) 796 jbd2_log_start_commit(journal, tid); 797 handle->h_total_credits = nblocks + 798 DIV_ROUND_UP(revoke_records, 799 journal->j_revoke_records_per_block); 800 handle->h_revoke_credits = revoke_records; 801 ret = start_this_handle(journal, handle, gfp_mask); 802 trace_jbd2_handle_restart(journal->j_fs_dev->bd_dev, 803 ret ? 0 : handle->h_transaction->t_tid, 804 handle->h_type, handle->h_line_no, 805 handle->h_total_credits); 806 return ret; 807 } 808 EXPORT_SYMBOL(jbd2__journal_restart); 809 810 811 int jbd2_journal_restart(handle_t *handle, int nblocks) 812 { 813 return jbd2__journal_restart(handle, nblocks, 0, GFP_NOFS); 814 } 815 EXPORT_SYMBOL(jbd2_journal_restart); 816 817 /** 818 * void jbd2_journal_lock_updates () - establish a transaction barrier. 819 * @journal: Journal to establish a barrier on. 820 * 821 * This locks out any further updates from being started, and blocks 822 * until all existing updates have completed, returning only once the 823 * journal is in a quiescent state with no updates running. 824 * 825 * The journal lock should not be held on entry. 826 */ 827 void jbd2_journal_lock_updates(journal_t *journal) 828 { 829 DEFINE_WAIT(wait); 830 831 jbd2_might_wait_for_commit(journal); 832 833 write_lock(&journal->j_state_lock); 834 ++journal->j_barrier_count; 835 836 /* Wait until there are no reserved handles */ 837 if (atomic_read(&journal->j_reserved_credits)) { 838 write_unlock(&journal->j_state_lock); 839 wait_event(journal->j_wait_reserved, 840 atomic_read(&journal->j_reserved_credits) == 0); 841 write_lock(&journal->j_state_lock); 842 } 843 844 /* Wait until there are no running updates */ 845 while (1) { 846 transaction_t *transaction = journal->j_running_transaction; 847 848 if (!transaction) 849 break; 850 851 spin_lock(&transaction->t_handle_lock); 852 prepare_to_wait(&journal->j_wait_updates, &wait, 853 TASK_UNINTERRUPTIBLE); 854 if (!atomic_read(&transaction->t_updates)) { 855 spin_unlock(&transaction->t_handle_lock); 856 finish_wait(&journal->j_wait_updates, &wait); 857 break; 858 } 859 spin_unlock(&transaction->t_handle_lock); 860 write_unlock(&journal->j_state_lock); 861 schedule(); 862 finish_wait(&journal->j_wait_updates, &wait); 863 write_lock(&journal->j_state_lock); 864 } 865 write_unlock(&journal->j_state_lock); 866 867 /* 868 * We have now established a barrier against other normal updates, but 869 * we also need to barrier against other jbd2_journal_lock_updates() calls 870 * to make sure that we serialise special journal-locked operations 871 * too. 872 */ 873 mutex_lock(&journal->j_barrier); 874 } 875 876 /** 877 * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier 878 * @journal: Journal to release the barrier on. 879 * 880 * Release a transaction barrier obtained with jbd2_journal_lock_updates(). 881 * 882 * Should be called without the journal lock held. 883 */ 884 void jbd2_journal_unlock_updates (journal_t *journal) 885 { 886 J_ASSERT(journal->j_barrier_count != 0); 887 888 mutex_unlock(&journal->j_barrier); 889 write_lock(&journal->j_state_lock); 890 --journal->j_barrier_count; 891 write_unlock(&journal->j_state_lock); 892 wake_up(&journal->j_wait_transaction_locked); 893 } 894 895 static void warn_dirty_buffer(struct buffer_head *bh) 896 { 897 printk(KERN_WARNING 898 "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). " 899 "There's a risk of filesystem corruption in case of system " 900 "crash.\n", 901 bh->b_bdev, (unsigned long long)bh->b_blocknr); 902 } 903 904 /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */ 905 static void jbd2_freeze_jh_data(struct journal_head *jh) 906 { 907 struct page *page; 908 int offset; 909 char *source; 910 struct buffer_head *bh = jh2bh(jh); 911 912 J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n"); 913 page = bh->b_page; 914 offset = offset_in_page(bh->b_data); 915 source = kmap_atomic(page); 916 /* Fire data frozen trigger just before we copy the data */ 917 jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers); 918 memcpy(jh->b_frozen_data, source + offset, bh->b_size); 919 kunmap_atomic(source); 920 921 /* 922 * Now that the frozen data is saved off, we need to store any matching 923 * triggers. 924 */ 925 jh->b_frozen_triggers = jh->b_triggers; 926 } 927 928 /* 929 * If the buffer is already part of the current transaction, then there 930 * is nothing we need to do. If it is already part of a prior 931 * transaction which we are still committing to disk, then we need to 932 * make sure that we do not overwrite the old copy: we do copy-out to 933 * preserve the copy going to disk. We also account the buffer against 934 * the handle's metadata buffer credits (unless the buffer is already 935 * part of the transaction, that is). 936 * 937 */ 938 static int 939 do_get_write_access(handle_t *handle, struct journal_head *jh, 940 int force_copy) 941 { 942 struct buffer_head *bh; 943 transaction_t *transaction = handle->h_transaction; 944 journal_t *journal; 945 int error; 946 char *frozen_buffer = NULL; 947 unsigned long start_lock, time_lock; 948 949 journal = transaction->t_journal; 950 951 jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy); 952 953 JBUFFER_TRACE(jh, "entry"); 954 repeat: 955 bh = jh2bh(jh); 956 957 /* @@@ Need to check for errors here at some point. */ 958 959 start_lock = jiffies; 960 lock_buffer(bh); 961 spin_lock(&jh->b_state_lock); 962 963 /* If it takes too long to lock the buffer, trace it */ 964 time_lock = jbd2_time_diff(start_lock, jiffies); 965 if (time_lock > HZ/10) 966 trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev, 967 jiffies_to_msecs(time_lock)); 968 969 /* We now hold the buffer lock so it is safe to query the buffer 970 * state. Is the buffer dirty? 971 * 972 * If so, there are two possibilities. The buffer may be 973 * non-journaled, and undergoing a quite legitimate writeback. 974 * Otherwise, it is journaled, and we don't expect dirty buffers 975 * in that state (the buffers should be marked JBD_Dirty 976 * instead.) So either the IO is being done under our own 977 * control and this is a bug, or it's a third party IO such as 978 * dump(8) (which may leave the buffer scheduled for read --- 979 * ie. locked but not dirty) or tune2fs (which may actually have 980 * the buffer dirtied, ugh.) */ 981 982 if (buffer_dirty(bh)) { 983 /* 984 * First question: is this buffer already part of the current 985 * transaction or the existing committing transaction? 986 */ 987 if (jh->b_transaction) { 988 J_ASSERT_JH(jh, 989 jh->b_transaction == transaction || 990 jh->b_transaction == 991 journal->j_committing_transaction); 992 if (jh->b_next_transaction) 993 J_ASSERT_JH(jh, jh->b_next_transaction == 994 transaction); 995 warn_dirty_buffer(bh); 996 } 997 /* 998 * In any case we need to clean the dirty flag and we must 999 * do it under the buffer lock to be sure we don't race 1000 * with running write-out. 1001 */ 1002 JBUFFER_TRACE(jh, "Journalling dirty buffer"); 1003 clear_buffer_dirty(bh); 1004 set_buffer_jbddirty(bh); 1005 } 1006 1007 unlock_buffer(bh); 1008 1009 error = -EROFS; 1010 if (is_handle_aborted(handle)) { 1011 spin_unlock(&jh->b_state_lock); 1012 goto out; 1013 } 1014 error = 0; 1015 1016 /* 1017 * The buffer is already part of this transaction if b_transaction or 1018 * b_next_transaction points to it 1019 */ 1020 if (jh->b_transaction == transaction || 1021 jh->b_next_transaction == transaction) 1022 goto done; 1023 1024 /* 1025 * this is the first time this transaction is touching this buffer, 1026 * reset the modified flag 1027 */ 1028 jh->b_modified = 0; 1029 1030 /* 1031 * If the buffer is not journaled right now, we need to make sure it 1032 * doesn't get written to disk before the caller actually commits the 1033 * new data 1034 */ 1035 if (!jh->b_transaction) { 1036 JBUFFER_TRACE(jh, "no transaction"); 1037 J_ASSERT_JH(jh, !jh->b_next_transaction); 1038 JBUFFER_TRACE(jh, "file as BJ_Reserved"); 1039 /* 1040 * Make sure all stores to jh (b_modified, b_frozen_data) are 1041 * visible before attaching it to the running transaction. 1042 * Paired with barrier in jbd2_write_access_granted() 1043 */ 1044 smp_wmb(); 1045 spin_lock(&journal->j_list_lock); 1046 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); 1047 spin_unlock(&journal->j_list_lock); 1048 goto done; 1049 } 1050 /* 1051 * If there is already a copy-out version of this buffer, then we don't 1052 * need to make another one 1053 */ 1054 if (jh->b_frozen_data) { 1055 JBUFFER_TRACE(jh, "has frozen data"); 1056 J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 1057 goto attach_next; 1058 } 1059 1060 JBUFFER_TRACE(jh, "owned by older transaction"); 1061 J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 1062 J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction); 1063 1064 /* 1065 * There is one case we have to be very careful about. If the 1066 * committing transaction is currently writing this buffer out to disk 1067 * and has NOT made a copy-out, then we cannot modify the buffer 1068 * contents at all right now. The essence of copy-out is that it is 1069 * the extra copy, not the primary copy, which gets journaled. If the 1070 * primary copy is already going to disk then we cannot do copy-out 1071 * here. 1072 */ 1073 if (buffer_shadow(bh)) { 1074 JBUFFER_TRACE(jh, "on shadow: sleep"); 1075 spin_unlock(&jh->b_state_lock); 1076 wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE); 1077 goto repeat; 1078 } 1079 1080 /* 1081 * Only do the copy if the currently-owning transaction still needs it. 1082 * If buffer isn't on BJ_Metadata list, the committing transaction is 1083 * past that stage (here we use the fact that BH_Shadow is set under 1084 * bh_state lock together with refiling to BJ_Shadow list and at this 1085 * point we know the buffer doesn't have BH_Shadow set). 1086 * 1087 * Subtle point, though: if this is a get_undo_access, then we will be 1088 * relying on the frozen_data to contain the new value of the 1089 * committed_data record after the transaction, so we HAVE to force the 1090 * frozen_data copy in that case. 1091 */ 1092 if (jh->b_jlist == BJ_Metadata || force_copy) { 1093 JBUFFER_TRACE(jh, "generate frozen data"); 1094 if (!frozen_buffer) { 1095 JBUFFER_TRACE(jh, "allocate memory for buffer"); 1096 spin_unlock(&jh->b_state_lock); 1097 frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size, 1098 GFP_NOFS | __GFP_NOFAIL); 1099 goto repeat; 1100 } 1101 jh->b_frozen_data = frozen_buffer; 1102 frozen_buffer = NULL; 1103 jbd2_freeze_jh_data(jh); 1104 } 1105 attach_next: 1106 /* 1107 * Make sure all stores to jh (b_modified, b_frozen_data) are visible 1108 * before attaching it to the running transaction. Paired with barrier 1109 * in jbd2_write_access_granted() 1110 */ 1111 smp_wmb(); 1112 jh->b_next_transaction = transaction; 1113 1114 done: 1115 spin_unlock(&jh->b_state_lock); 1116 1117 /* 1118 * If we are about to journal a buffer, then any revoke pending on it is 1119 * no longer valid 1120 */ 1121 jbd2_journal_cancel_revoke(handle, jh); 1122 1123 out: 1124 if (unlikely(frozen_buffer)) /* It's usually NULL */ 1125 jbd2_free(frozen_buffer, bh->b_size); 1126 1127 JBUFFER_TRACE(jh, "exit"); 1128 return error; 1129 } 1130 1131 /* Fast check whether buffer is already attached to the required transaction */ 1132 static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh, 1133 bool undo) 1134 { 1135 struct journal_head *jh; 1136 bool ret = false; 1137 1138 /* Dirty buffers require special handling... */ 1139 if (buffer_dirty(bh)) 1140 return false; 1141 1142 /* 1143 * RCU protects us from dereferencing freed pages. So the checks we do 1144 * are guaranteed not to oops. However the jh slab object can get freed 1145 * & reallocated while we work with it. So we have to be careful. When 1146 * we see jh attached to the running transaction, we know it must stay 1147 * so until the transaction is committed. Thus jh won't be freed and 1148 * will be attached to the same bh while we run. However it can 1149 * happen jh gets freed, reallocated, and attached to the transaction 1150 * just after we get pointer to it from bh. So we have to be careful 1151 * and recheck jh still belongs to our bh before we return success. 1152 */ 1153 rcu_read_lock(); 1154 if (!buffer_jbd(bh)) 1155 goto out; 1156 /* This should be bh2jh() but that doesn't work with inline functions */ 1157 jh = READ_ONCE(bh->b_private); 1158 if (!jh) 1159 goto out; 1160 /* For undo access buffer must have data copied */ 1161 if (undo && !jh->b_committed_data) 1162 goto out; 1163 if (READ_ONCE(jh->b_transaction) != handle->h_transaction && 1164 READ_ONCE(jh->b_next_transaction) != handle->h_transaction) 1165 goto out; 1166 /* 1167 * There are two reasons for the barrier here: 1168 * 1) Make sure to fetch b_bh after we did previous checks so that we 1169 * detect when jh went through free, realloc, attach to transaction 1170 * while we were checking. Paired with implicit barrier in that path. 1171 * 2) So that access to bh done after jbd2_write_access_granted() 1172 * doesn't get reordered and see inconsistent state of concurrent 1173 * do_get_write_access(). 1174 */ 1175 smp_mb(); 1176 if (unlikely(jh->b_bh != bh)) 1177 goto out; 1178 ret = true; 1179 out: 1180 rcu_read_unlock(); 1181 return ret; 1182 } 1183 1184 /** 1185 * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update. 1186 * @handle: transaction to add buffer modifications to 1187 * @bh: bh to be used for metadata writes 1188 * 1189 * Returns: error code or 0 on success. 1190 * 1191 * In full data journalling mode the buffer may be of type BJ_AsyncData, 1192 * because we're ``write()ing`` a buffer which is also part of a shared mapping. 1193 */ 1194 1195 int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh) 1196 { 1197 struct journal_head *jh; 1198 int rc; 1199 1200 if (is_handle_aborted(handle)) 1201 return -EROFS; 1202 1203 if (jbd2_write_access_granted(handle, bh, false)) 1204 return 0; 1205 1206 jh = jbd2_journal_add_journal_head(bh); 1207 /* We do not want to get caught playing with fields which the 1208 * log thread also manipulates. Make sure that the buffer 1209 * completes any outstanding IO before proceeding. */ 1210 rc = do_get_write_access(handle, jh, 0); 1211 jbd2_journal_put_journal_head(jh); 1212 return rc; 1213 } 1214 1215 1216 /* 1217 * When the user wants to journal a newly created buffer_head 1218 * (ie. getblk() returned a new buffer and we are going to populate it 1219 * manually rather than reading off disk), then we need to keep the 1220 * buffer_head locked until it has been completely filled with new 1221 * data. In this case, we should be able to make the assertion that 1222 * the bh is not already part of an existing transaction. 1223 * 1224 * The buffer should already be locked by the caller by this point. 1225 * There is no lock ranking violation: it was a newly created, 1226 * unlocked buffer beforehand. */ 1227 1228 /** 1229 * int jbd2_journal_get_create_access () - notify intent to use newly created bh 1230 * @handle: transaction to new buffer to 1231 * @bh: new buffer. 1232 * 1233 * Call this if you create a new bh. 1234 */ 1235 int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh) 1236 { 1237 transaction_t *transaction = handle->h_transaction; 1238 journal_t *journal; 1239 struct journal_head *jh = jbd2_journal_add_journal_head(bh); 1240 int err; 1241 1242 jbd_debug(5, "journal_head %p\n", jh); 1243 err = -EROFS; 1244 if (is_handle_aborted(handle)) 1245 goto out; 1246 journal = transaction->t_journal; 1247 err = 0; 1248 1249 JBUFFER_TRACE(jh, "entry"); 1250 /* 1251 * The buffer may already belong to this transaction due to pre-zeroing 1252 * in the filesystem's new_block code. It may also be on the previous, 1253 * committing transaction's lists, but it HAS to be in Forget state in 1254 * that case: the transaction must have deleted the buffer for it to be 1255 * reused here. 1256 */ 1257 spin_lock(&jh->b_state_lock); 1258 J_ASSERT_JH(jh, (jh->b_transaction == transaction || 1259 jh->b_transaction == NULL || 1260 (jh->b_transaction == journal->j_committing_transaction && 1261 jh->b_jlist == BJ_Forget))); 1262 1263 J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 1264 J_ASSERT_JH(jh, buffer_locked(jh2bh(jh))); 1265 1266 if (jh->b_transaction == NULL) { 1267 /* 1268 * Previous jbd2_journal_forget() could have left the buffer 1269 * with jbddirty bit set because it was being committed. When 1270 * the commit finished, we've filed the buffer for 1271 * checkpointing and marked it dirty. Now we are reallocating 1272 * the buffer so the transaction freeing it must have 1273 * committed and so it's safe to clear the dirty bit. 1274 */ 1275 clear_buffer_dirty(jh2bh(jh)); 1276 /* first access by this transaction */ 1277 jh->b_modified = 0; 1278 1279 JBUFFER_TRACE(jh, "file as BJ_Reserved"); 1280 spin_lock(&journal->j_list_lock); 1281 __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); 1282 spin_unlock(&journal->j_list_lock); 1283 } else if (jh->b_transaction == journal->j_committing_transaction) { 1284 /* first access by this transaction */ 1285 jh->b_modified = 0; 1286 1287 JBUFFER_TRACE(jh, "set next transaction"); 1288 spin_lock(&journal->j_list_lock); 1289 jh->b_next_transaction = transaction; 1290 spin_unlock(&journal->j_list_lock); 1291 } 1292 spin_unlock(&jh->b_state_lock); 1293 1294 /* 1295 * akpm: I added this. ext3_alloc_branch can pick up new indirect 1296 * blocks which contain freed but then revoked metadata. We need 1297 * to cancel the revoke in case we end up freeing it yet again 1298 * and the reallocating as data - this would cause a second revoke, 1299 * which hits an assertion error. 1300 */ 1301 JBUFFER_TRACE(jh, "cancelling revoke"); 1302 jbd2_journal_cancel_revoke(handle, jh); 1303 out: 1304 jbd2_journal_put_journal_head(jh); 1305 return err; 1306 } 1307 1308 /** 1309 * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with 1310 * non-rewindable consequences 1311 * @handle: transaction 1312 * @bh: buffer to undo 1313 * 1314 * Sometimes there is a need to distinguish between metadata which has 1315 * been committed to disk and that which has not. The ext3fs code uses 1316 * this for freeing and allocating space, we have to make sure that we 1317 * do not reuse freed space until the deallocation has been committed, 1318 * since if we overwrote that space we would make the delete 1319 * un-rewindable in case of a crash. 1320 * 1321 * To deal with that, jbd2_journal_get_undo_access requests write access to a 1322 * buffer for parts of non-rewindable operations such as delete 1323 * operations on the bitmaps. The journaling code must keep a copy of 1324 * the buffer's contents prior to the undo_access call until such time 1325 * as we know that the buffer has definitely been committed to disk. 1326 * 1327 * We never need to know which transaction the committed data is part 1328 * of, buffers touched here are guaranteed to be dirtied later and so 1329 * will be committed to a new transaction in due course, at which point 1330 * we can discard the old committed data pointer. 1331 * 1332 * Returns error number or 0 on success. 1333 */ 1334 int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh) 1335 { 1336 int err; 1337 struct journal_head *jh; 1338 char *committed_data = NULL; 1339 1340 if (is_handle_aborted(handle)) 1341 return -EROFS; 1342 1343 if (jbd2_write_access_granted(handle, bh, true)) 1344 return 0; 1345 1346 jh = jbd2_journal_add_journal_head(bh); 1347 JBUFFER_TRACE(jh, "entry"); 1348 1349 /* 1350 * Do this first --- it can drop the journal lock, so we want to 1351 * make sure that obtaining the committed_data is done 1352 * atomically wrt. completion of any outstanding commits. 1353 */ 1354 err = do_get_write_access(handle, jh, 1); 1355 if (err) 1356 goto out; 1357 1358 repeat: 1359 if (!jh->b_committed_data) 1360 committed_data = jbd2_alloc(jh2bh(jh)->b_size, 1361 GFP_NOFS|__GFP_NOFAIL); 1362 1363 spin_lock(&jh->b_state_lock); 1364 if (!jh->b_committed_data) { 1365 /* Copy out the current buffer contents into the 1366 * preserved, committed copy. */ 1367 JBUFFER_TRACE(jh, "generate b_committed data"); 1368 if (!committed_data) { 1369 spin_unlock(&jh->b_state_lock); 1370 goto repeat; 1371 } 1372 1373 jh->b_committed_data = committed_data; 1374 committed_data = NULL; 1375 memcpy(jh->b_committed_data, bh->b_data, bh->b_size); 1376 } 1377 spin_unlock(&jh->b_state_lock); 1378 out: 1379 jbd2_journal_put_journal_head(jh); 1380 if (unlikely(committed_data)) 1381 jbd2_free(committed_data, bh->b_size); 1382 return err; 1383 } 1384 1385 /** 1386 * void jbd2_journal_set_triggers() - Add triggers for commit writeout 1387 * @bh: buffer to trigger on 1388 * @type: struct jbd2_buffer_trigger_type containing the trigger(s). 1389 * 1390 * Set any triggers on this journal_head. This is always safe, because 1391 * triggers for a committing buffer will be saved off, and triggers for 1392 * a running transaction will match the buffer in that transaction. 1393 * 1394 * Call with NULL to clear the triggers. 1395 */ 1396 void jbd2_journal_set_triggers(struct buffer_head *bh, 1397 struct jbd2_buffer_trigger_type *type) 1398 { 1399 struct journal_head *jh = jbd2_journal_grab_journal_head(bh); 1400 1401 if (WARN_ON(!jh)) 1402 return; 1403 jh->b_triggers = type; 1404 jbd2_journal_put_journal_head(jh); 1405 } 1406 1407 void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data, 1408 struct jbd2_buffer_trigger_type *triggers) 1409 { 1410 struct buffer_head *bh = jh2bh(jh); 1411 1412 if (!triggers || !triggers->t_frozen) 1413 return; 1414 1415 triggers->t_frozen(triggers, bh, mapped_data, bh->b_size); 1416 } 1417 1418 void jbd2_buffer_abort_trigger(struct journal_head *jh, 1419 struct jbd2_buffer_trigger_type *triggers) 1420 { 1421 if (!triggers || !triggers->t_abort) 1422 return; 1423 1424 triggers->t_abort(triggers, jh2bh(jh)); 1425 } 1426 1427 /** 1428 * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata 1429 * @handle: transaction to add buffer to. 1430 * @bh: buffer to mark 1431 * 1432 * mark dirty metadata which needs to be journaled as part of the current 1433 * transaction. 1434 * 1435 * The buffer must have previously had jbd2_journal_get_write_access() 1436 * called so that it has a valid journal_head attached to the buffer 1437 * head. 1438 * 1439 * The buffer is placed on the transaction's metadata list and is marked 1440 * as belonging to the transaction. 1441 * 1442 * Returns error number or 0 on success. 1443 * 1444 * Special care needs to be taken if the buffer already belongs to the 1445 * current committing transaction (in which case we should have frozen 1446 * data present for that commit). In that case, we don't relink the 1447 * buffer: that only gets done when the old transaction finally 1448 * completes its commit. 1449 */ 1450 int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh) 1451 { 1452 transaction_t *transaction = handle->h_transaction; 1453 journal_t *journal; 1454 struct journal_head *jh; 1455 int ret = 0; 1456 1457 if (is_handle_aborted(handle)) 1458 return -EROFS; 1459 if (!buffer_jbd(bh)) 1460 return -EUCLEAN; 1461 1462 /* 1463 * We don't grab jh reference here since the buffer must be part 1464 * of the running transaction. 1465 */ 1466 jh = bh2jh(bh); 1467 jbd_debug(5, "journal_head %p\n", jh); 1468 JBUFFER_TRACE(jh, "entry"); 1469 1470 /* 1471 * This and the following assertions are unreliable since we may see jh 1472 * in inconsistent state unless we grab bh_state lock. But this is 1473 * crucial to catch bugs so let's do a reliable check until the 1474 * lockless handling is fully proven. 1475 */ 1476 if (jh->b_transaction != transaction && 1477 jh->b_next_transaction != transaction) { 1478 spin_lock(&jh->b_state_lock); 1479 J_ASSERT_JH(jh, jh->b_transaction == transaction || 1480 jh->b_next_transaction == transaction); 1481 spin_unlock(&jh->b_state_lock); 1482 } 1483 if (jh->b_modified == 1) { 1484 /* If it's in our transaction it must be in BJ_Metadata list. */ 1485 if (jh->b_transaction == transaction && 1486 jh->b_jlist != BJ_Metadata) { 1487 spin_lock(&jh->b_state_lock); 1488 if (jh->b_transaction == transaction && 1489 jh->b_jlist != BJ_Metadata) 1490 pr_err("JBD2: assertion failure: h_type=%u " 1491 "h_line_no=%u block_no=%llu jlist=%u\n", 1492 handle->h_type, handle->h_line_no, 1493 (unsigned long long) bh->b_blocknr, 1494 jh->b_jlist); 1495 J_ASSERT_JH(jh, jh->b_transaction != transaction || 1496 jh->b_jlist == BJ_Metadata); 1497 spin_unlock(&jh->b_state_lock); 1498 } 1499 goto out; 1500 } 1501 1502 journal = transaction->t_journal; 1503 spin_lock(&jh->b_state_lock); 1504 1505 if (jh->b_modified == 0) { 1506 /* 1507 * This buffer's got modified and becoming part 1508 * of the transaction. This needs to be done 1509 * once a transaction -bzzz 1510 */ 1511 if (WARN_ON_ONCE(jbd2_handle_buffer_credits(handle) <= 0)) { 1512 ret = -ENOSPC; 1513 goto out_unlock_bh; 1514 } 1515 jh->b_modified = 1; 1516 handle->h_total_credits--; 1517 } 1518 1519 /* 1520 * fastpath, to avoid expensive locking. If this buffer is already 1521 * on the running transaction's metadata list there is nothing to do. 1522 * Nobody can take it off again because there is a handle open. 1523 * I _think_ we're OK here with SMP barriers - a mistaken decision will 1524 * result in this test being false, so we go in and take the locks. 1525 */ 1526 if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) { 1527 JBUFFER_TRACE(jh, "fastpath"); 1528 if (unlikely(jh->b_transaction != 1529 journal->j_running_transaction)) { 1530 printk(KERN_ERR "JBD2: %s: " 1531 "jh->b_transaction (%llu, %p, %u) != " 1532 "journal->j_running_transaction (%p, %u)\n", 1533 journal->j_devname, 1534 (unsigned long long) bh->b_blocknr, 1535 jh->b_transaction, 1536 jh->b_transaction ? jh->b_transaction->t_tid : 0, 1537 journal->j_running_transaction, 1538 journal->j_running_transaction ? 1539 journal->j_running_transaction->t_tid : 0); 1540 ret = -EINVAL; 1541 } 1542 goto out_unlock_bh; 1543 } 1544 1545 set_buffer_jbddirty(bh); 1546 1547 /* 1548 * Metadata already on the current transaction list doesn't 1549 * need to be filed. Metadata on another transaction's list must 1550 * be committing, and will be refiled once the commit completes: 1551 * leave it alone for now. 1552 */ 1553 if (jh->b_transaction != transaction) { 1554 JBUFFER_TRACE(jh, "already on other transaction"); 1555 if (unlikely(((jh->b_transaction != 1556 journal->j_committing_transaction)) || 1557 (jh->b_next_transaction != transaction))) { 1558 printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: " 1559 "bad jh for block %llu: " 1560 "transaction (%p, %u), " 1561 "jh->b_transaction (%p, %u), " 1562 "jh->b_next_transaction (%p, %u), jlist %u\n", 1563 journal->j_devname, 1564 (unsigned long long) bh->b_blocknr, 1565 transaction, transaction->t_tid, 1566 jh->b_transaction, 1567 jh->b_transaction ? 1568 jh->b_transaction->t_tid : 0, 1569 jh->b_next_transaction, 1570 jh->b_next_transaction ? 1571 jh->b_next_transaction->t_tid : 0, 1572 jh->b_jlist); 1573 WARN_ON(1); 1574 ret = -EINVAL; 1575 } 1576 /* And this case is illegal: we can't reuse another 1577 * transaction's data buffer, ever. */ 1578 goto out_unlock_bh; 1579 } 1580 1581 /* That test should have eliminated the following case: */ 1582 J_ASSERT_JH(jh, jh->b_frozen_data == NULL); 1583 1584 JBUFFER_TRACE(jh, "file as BJ_Metadata"); 1585 spin_lock(&journal->j_list_lock); 1586 __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata); 1587 spin_unlock(&journal->j_list_lock); 1588 out_unlock_bh: 1589 spin_unlock(&jh->b_state_lock); 1590 out: 1591 JBUFFER_TRACE(jh, "exit"); 1592 return ret; 1593 } 1594 1595 /** 1596 * void jbd2_journal_forget() - bforget() for potentially-journaled buffers. 1597 * @handle: transaction handle 1598 * @bh: bh to 'forget' 1599 * 1600 * We can only do the bforget if there are no commits pending against the 1601 * buffer. If the buffer is dirty in the current running transaction we 1602 * can safely unlink it. 1603 * 1604 * bh may not be a journalled buffer at all - it may be a non-JBD 1605 * buffer which came off the hashtable. Check for this. 1606 * 1607 * Decrements bh->b_count by one. 1608 * 1609 * Allow this call even if the handle has aborted --- it may be part of 1610 * the caller's cleanup after an abort. 1611 */ 1612 int jbd2_journal_forget(handle_t *handle, struct buffer_head *bh) 1613 { 1614 transaction_t *transaction = handle->h_transaction; 1615 journal_t *journal; 1616 struct journal_head *jh; 1617 int drop_reserve = 0; 1618 int err = 0; 1619 int was_modified = 0; 1620 1621 if (is_handle_aborted(handle)) 1622 return -EROFS; 1623 journal = transaction->t_journal; 1624 1625 BUFFER_TRACE(bh, "entry"); 1626 1627 jh = jbd2_journal_grab_journal_head(bh); 1628 if (!jh) { 1629 __bforget(bh); 1630 return 0; 1631 } 1632 1633 spin_lock(&jh->b_state_lock); 1634 1635 /* Critical error: attempting to delete a bitmap buffer, maybe? 1636 * Don't do any jbd operations, and return an error. */ 1637 if (!J_EXPECT_JH(jh, !jh->b_committed_data, 1638 "inconsistent data on disk")) { 1639 err = -EIO; 1640 goto drop; 1641 } 1642 1643 /* keep track of whether or not this transaction modified us */ 1644 was_modified = jh->b_modified; 1645 1646 /* 1647 * The buffer's going from the transaction, we must drop 1648 * all references -bzzz 1649 */ 1650 jh->b_modified = 0; 1651 1652 if (jh->b_transaction == transaction) { 1653 J_ASSERT_JH(jh, !jh->b_frozen_data); 1654 1655 /* If we are forgetting a buffer which is already part 1656 * of this transaction, then we can just drop it from 1657 * the transaction immediately. */ 1658 clear_buffer_dirty(bh); 1659 clear_buffer_jbddirty(bh); 1660 1661 JBUFFER_TRACE(jh, "belongs to current transaction: unfile"); 1662 1663 /* 1664 * we only want to drop a reference if this transaction 1665 * modified the buffer 1666 */ 1667 if (was_modified) 1668 drop_reserve = 1; 1669 1670 /* 1671 * We are no longer going to journal this buffer. 1672 * However, the commit of this transaction is still 1673 * important to the buffer: the delete that we are now 1674 * processing might obsolete an old log entry, so by 1675 * committing, we can satisfy the buffer's checkpoint. 1676 * 1677 * So, if we have a checkpoint on the buffer, we should 1678 * now refile the buffer on our BJ_Forget list so that 1679 * we know to remove the checkpoint after we commit. 1680 */ 1681 1682 spin_lock(&journal->j_list_lock); 1683 if (jh->b_cp_transaction) { 1684 __jbd2_journal_temp_unlink_buffer(jh); 1685 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); 1686 } else { 1687 __jbd2_journal_unfile_buffer(jh); 1688 jbd2_journal_put_journal_head(jh); 1689 } 1690 spin_unlock(&journal->j_list_lock); 1691 } else if (jh->b_transaction) { 1692 J_ASSERT_JH(jh, (jh->b_transaction == 1693 journal->j_committing_transaction)); 1694 /* However, if the buffer is still owned by a prior 1695 * (committing) transaction, we can't drop it yet... */ 1696 JBUFFER_TRACE(jh, "belongs to older transaction"); 1697 /* ... but we CAN drop it from the new transaction through 1698 * marking the buffer as freed and set j_next_transaction to 1699 * the new transaction, so that not only the commit code 1700 * knows it should clear dirty bits when it is done with the 1701 * buffer, but also the buffer can be checkpointed only 1702 * after the new transaction commits. */ 1703 1704 set_buffer_freed(bh); 1705 1706 if (!jh->b_next_transaction) { 1707 spin_lock(&journal->j_list_lock); 1708 jh->b_next_transaction = transaction; 1709 spin_unlock(&journal->j_list_lock); 1710 } else { 1711 J_ASSERT(jh->b_next_transaction == transaction); 1712 1713 /* 1714 * only drop a reference if this transaction modified 1715 * the buffer 1716 */ 1717 if (was_modified) 1718 drop_reserve = 1; 1719 } 1720 } else { 1721 /* 1722 * Finally, if the buffer is not belongs to any 1723 * transaction, we can just drop it now if it has no 1724 * checkpoint. 1725 */ 1726 spin_lock(&journal->j_list_lock); 1727 if (!jh->b_cp_transaction) { 1728 JBUFFER_TRACE(jh, "belongs to none transaction"); 1729 spin_unlock(&journal->j_list_lock); 1730 goto drop; 1731 } 1732 1733 /* 1734 * Otherwise, if the buffer has been written to disk, 1735 * it is safe to remove the checkpoint and drop it. 1736 */ 1737 if (!buffer_dirty(bh)) { 1738 __jbd2_journal_remove_checkpoint(jh); 1739 spin_unlock(&journal->j_list_lock); 1740 goto drop; 1741 } 1742 1743 /* 1744 * The buffer is still not written to disk, we should 1745 * attach this buffer to current transaction so that the 1746 * buffer can be checkpointed only after the current 1747 * transaction commits. 1748 */ 1749 clear_buffer_dirty(bh); 1750 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); 1751 spin_unlock(&journal->j_list_lock); 1752 } 1753 drop: 1754 __brelse(bh); 1755 spin_unlock(&jh->b_state_lock); 1756 jbd2_journal_put_journal_head(jh); 1757 if (drop_reserve) { 1758 /* no need to reserve log space for this block -bzzz */ 1759 handle->h_total_credits++; 1760 } 1761 return err; 1762 } 1763 1764 /** 1765 * int jbd2_journal_stop() - complete a transaction 1766 * @handle: transaction to complete. 1767 * 1768 * All done for a particular handle. 1769 * 1770 * There is not much action needed here. We just return any remaining 1771 * buffer credits to the transaction and remove the handle. The only 1772 * complication is that we need to start a commit operation if the 1773 * filesystem is marked for synchronous update. 1774 * 1775 * jbd2_journal_stop itself will not usually return an error, but it may 1776 * do so in unusual circumstances. In particular, expect it to 1777 * return -EIO if a jbd2_journal_abort has been executed since the 1778 * transaction began. 1779 */ 1780 int jbd2_journal_stop(handle_t *handle) 1781 { 1782 transaction_t *transaction = handle->h_transaction; 1783 journal_t *journal; 1784 int err = 0, wait_for_commit = 0; 1785 tid_t tid; 1786 pid_t pid; 1787 1788 if (--handle->h_ref > 0) { 1789 jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1, 1790 handle->h_ref); 1791 if (is_handle_aborted(handle)) 1792 return -EIO; 1793 return 0; 1794 } 1795 if (!transaction) { 1796 /* 1797 * Handle is already detached from the transaction so there is 1798 * nothing to do other than free the handle. 1799 */ 1800 memalloc_nofs_restore(handle->saved_alloc_context); 1801 goto free_and_exit; 1802 } 1803 journal = transaction->t_journal; 1804 tid = transaction->t_tid; 1805 1806 if (is_handle_aborted(handle)) 1807 err = -EIO; 1808 1809 jbd_debug(4, "Handle %p going down\n", handle); 1810 trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev, 1811 tid, handle->h_type, handle->h_line_no, 1812 jiffies - handle->h_start_jiffies, 1813 handle->h_sync, handle->h_requested_credits, 1814 (handle->h_requested_credits - 1815 handle->h_total_credits)); 1816 1817 /* 1818 * Implement synchronous transaction batching. If the handle 1819 * was synchronous, don't force a commit immediately. Let's 1820 * yield and let another thread piggyback onto this 1821 * transaction. Keep doing that while new threads continue to 1822 * arrive. It doesn't cost much - we're about to run a commit 1823 * and sleep on IO anyway. Speeds up many-threaded, many-dir 1824 * operations by 30x or more... 1825 * 1826 * We try and optimize the sleep time against what the 1827 * underlying disk can do, instead of having a static sleep 1828 * time. This is useful for the case where our storage is so 1829 * fast that it is more optimal to go ahead and force a flush 1830 * and wait for the transaction to be committed than it is to 1831 * wait for an arbitrary amount of time for new writers to 1832 * join the transaction. We achieve this by measuring how 1833 * long it takes to commit a transaction, and compare it with 1834 * how long this transaction has been running, and if run time 1835 * < commit time then we sleep for the delta and commit. This 1836 * greatly helps super fast disks that would see slowdowns as 1837 * more threads started doing fsyncs. 1838 * 1839 * But don't do this if this process was the most recent one 1840 * to perform a synchronous write. We do this to detect the 1841 * case where a single process is doing a stream of sync 1842 * writes. No point in waiting for joiners in that case. 1843 * 1844 * Setting max_batch_time to 0 disables this completely. 1845 */ 1846 pid = current->pid; 1847 if (handle->h_sync && journal->j_last_sync_writer != pid && 1848 journal->j_max_batch_time) { 1849 u64 commit_time, trans_time; 1850 1851 journal->j_last_sync_writer = pid; 1852 1853 read_lock(&journal->j_state_lock); 1854 commit_time = journal->j_average_commit_time; 1855 read_unlock(&journal->j_state_lock); 1856 1857 trans_time = ktime_to_ns(ktime_sub(ktime_get(), 1858 transaction->t_start_time)); 1859 1860 commit_time = max_t(u64, commit_time, 1861 1000*journal->j_min_batch_time); 1862 commit_time = min_t(u64, commit_time, 1863 1000*journal->j_max_batch_time); 1864 1865 if (trans_time < commit_time) { 1866 ktime_t expires = ktime_add_ns(ktime_get(), 1867 commit_time); 1868 set_current_state(TASK_UNINTERRUPTIBLE); 1869 schedule_hrtimeout(&expires, HRTIMER_MODE_ABS); 1870 } 1871 } 1872 1873 if (handle->h_sync) 1874 transaction->t_synchronous_commit = 1; 1875 1876 /* 1877 * If the handle is marked SYNC, we need to set another commit 1878 * going! We also want to force a commit if the transaction is too 1879 * old now. 1880 */ 1881 if (handle->h_sync || 1882 time_after_eq(jiffies, transaction->t_expires)) { 1883 /* Do this even for aborted journals: an abort still 1884 * completes the commit thread, it just doesn't write 1885 * anything to disk. */ 1886 1887 jbd_debug(2, "transaction too old, requesting commit for " 1888 "handle %p\n", handle); 1889 /* This is non-blocking */ 1890 jbd2_log_start_commit(journal, tid); 1891 1892 /* 1893 * Special case: JBD2_SYNC synchronous updates require us 1894 * to wait for the commit to complete. 1895 */ 1896 if (handle->h_sync && !(current->flags & PF_MEMALLOC)) 1897 wait_for_commit = 1; 1898 } 1899 1900 /* 1901 * Once stop_this_handle() drops t_updates, the transaction could start 1902 * committing on us and eventually disappear. So we must not 1903 * dereference transaction pointer again after calling 1904 * stop_this_handle(). 1905 */ 1906 stop_this_handle(handle); 1907 1908 if (wait_for_commit) 1909 err = jbd2_log_wait_commit(journal, tid); 1910 1911 free_and_exit: 1912 if (handle->h_rsv_handle) 1913 jbd2_free_handle(handle->h_rsv_handle); 1914 jbd2_free_handle(handle); 1915 return err; 1916 } 1917 1918 /* 1919 * 1920 * List management code snippets: various functions for manipulating the 1921 * transaction buffer lists. 1922 * 1923 */ 1924 1925 /* 1926 * Append a buffer to a transaction list, given the transaction's list head 1927 * pointer. 1928 * 1929 * j_list_lock is held. 1930 * 1931 * jh->b_state_lock is held. 1932 */ 1933 1934 static inline void 1935 __blist_add_buffer(struct journal_head **list, struct journal_head *jh) 1936 { 1937 if (!*list) { 1938 jh->b_tnext = jh->b_tprev = jh; 1939 *list = jh; 1940 } else { 1941 /* Insert at the tail of the list to preserve order */ 1942 struct journal_head *first = *list, *last = first->b_tprev; 1943 jh->b_tprev = last; 1944 jh->b_tnext = first; 1945 last->b_tnext = first->b_tprev = jh; 1946 } 1947 } 1948 1949 /* 1950 * Remove a buffer from a transaction list, given the transaction's list 1951 * head pointer. 1952 * 1953 * Called with j_list_lock held, and the journal may not be locked. 1954 * 1955 * jh->b_state_lock is held. 1956 */ 1957 1958 static inline void 1959 __blist_del_buffer(struct journal_head **list, struct journal_head *jh) 1960 { 1961 if (*list == jh) { 1962 *list = jh->b_tnext; 1963 if (*list == jh) 1964 *list = NULL; 1965 } 1966 jh->b_tprev->b_tnext = jh->b_tnext; 1967 jh->b_tnext->b_tprev = jh->b_tprev; 1968 } 1969 1970 /* 1971 * Remove a buffer from the appropriate transaction list. 1972 * 1973 * Note that this function can *change* the value of 1974 * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or 1975 * t_reserved_list. If the caller is holding onto a copy of one of these 1976 * pointers, it could go bad. Generally the caller needs to re-read the 1977 * pointer from the transaction_t. 1978 * 1979 * Called under j_list_lock. 1980 */ 1981 static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh) 1982 { 1983 struct journal_head **list = NULL; 1984 transaction_t *transaction; 1985 struct buffer_head *bh = jh2bh(jh); 1986 1987 lockdep_assert_held(&jh->b_state_lock); 1988 transaction = jh->b_transaction; 1989 if (transaction) 1990 assert_spin_locked(&transaction->t_journal->j_list_lock); 1991 1992 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); 1993 if (jh->b_jlist != BJ_None) 1994 J_ASSERT_JH(jh, transaction != NULL); 1995 1996 switch (jh->b_jlist) { 1997 case BJ_None: 1998 return; 1999 case BJ_Metadata: 2000 transaction->t_nr_buffers--; 2001 J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0); 2002 list = &transaction->t_buffers; 2003 break; 2004 case BJ_Forget: 2005 list = &transaction->t_forget; 2006 break; 2007 case BJ_Shadow: 2008 list = &transaction->t_shadow_list; 2009 break; 2010 case BJ_Reserved: 2011 list = &transaction->t_reserved_list; 2012 break; 2013 } 2014 2015 __blist_del_buffer(list, jh); 2016 jh->b_jlist = BJ_None; 2017 if (transaction && is_journal_aborted(transaction->t_journal)) 2018 clear_buffer_jbddirty(bh); 2019 else if (test_clear_buffer_jbddirty(bh)) 2020 mark_buffer_dirty(bh); /* Expose it to the VM */ 2021 } 2022 2023 /* 2024 * Remove buffer from all transactions. The caller is responsible for dropping 2025 * the jh reference that belonged to the transaction. 2026 * 2027 * Called with bh_state lock and j_list_lock 2028 */ 2029 static void __jbd2_journal_unfile_buffer(struct journal_head *jh) 2030 { 2031 J_ASSERT_JH(jh, jh->b_transaction != NULL); 2032 J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 2033 2034 __jbd2_journal_temp_unlink_buffer(jh); 2035 jh->b_transaction = NULL; 2036 } 2037 2038 void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh) 2039 { 2040 struct buffer_head *bh = jh2bh(jh); 2041 2042 /* Get reference so that buffer cannot be freed before we unlock it */ 2043 get_bh(bh); 2044 spin_lock(&jh->b_state_lock); 2045 spin_lock(&journal->j_list_lock); 2046 __jbd2_journal_unfile_buffer(jh); 2047 spin_unlock(&journal->j_list_lock); 2048 spin_unlock(&jh->b_state_lock); 2049 jbd2_journal_put_journal_head(jh); 2050 __brelse(bh); 2051 } 2052 2053 /* 2054 * Called from jbd2_journal_try_to_free_buffers(). 2055 * 2056 * Called under jh->b_state_lock 2057 */ 2058 static void 2059 __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh) 2060 { 2061 struct journal_head *jh; 2062 2063 jh = bh2jh(bh); 2064 2065 if (buffer_locked(bh) || buffer_dirty(bh)) 2066 goto out; 2067 2068 if (jh->b_next_transaction != NULL || jh->b_transaction != NULL) 2069 goto out; 2070 2071 spin_lock(&journal->j_list_lock); 2072 if (jh->b_cp_transaction != NULL) { 2073 /* written-back checkpointed metadata buffer */ 2074 JBUFFER_TRACE(jh, "remove from checkpoint list"); 2075 __jbd2_journal_remove_checkpoint(jh); 2076 } 2077 spin_unlock(&journal->j_list_lock); 2078 out: 2079 return; 2080 } 2081 2082 /** 2083 * int jbd2_journal_try_to_free_buffers() - try to free page buffers. 2084 * @journal: journal for operation 2085 * @page: to try and free 2086 * 2087 * For all the buffers on this page, 2088 * if they are fully written out ordered data, move them onto BUF_CLEAN 2089 * so try_to_free_buffers() can reap them. 2090 * 2091 * This function returns non-zero if we wish try_to_free_buffers() 2092 * to be called. We do this if the page is releasable by try_to_free_buffers(). 2093 * We also do it if the page has locked or dirty buffers and the caller wants 2094 * us to perform sync or async writeout. 2095 * 2096 * This complicates JBD locking somewhat. We aren't protected by the 2097 * BKL here. We wish to remove the buffer from its committing or 2098 * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer. 2099 * 2100 * This may *change* the value of transaction_t->t_datalist, so anyone 2101 * who looks at t_datalist needs to lock against this function. 2102 * 2103 * Even worse, someone may be doing a jbd2_journal_dirty_data on this 2104 * buffer. So we need to lock against that. jbd2_journal_dirty_data() 2105 * will come out of the lock with the buffer dirty, which makes it 2106 * ineligible for release here. 2107 * 2108 * Who else is affected by this? hmm... Really the only contender 2109 * is do_get_write_access() - it could be looking at the buffer while 2110 * journal_try_to_free_buffer() is changing its state. But that 2111 * cannot happen because we never reallocate freed data as metadata 2112 * while the data is part of a transaction. Yes? 2113 * 2114 * Return 0 on failure, 1 on success 2115 */ 2116 int jbd2_journal_try_to_free_buffers(journal_t *journal, struct page *page) 2117 { 2118 struct buffer_head *head; 2119 struct buffer_head *bh; 2120 bool has_write_io_error = false; 2121 int ret = 0; 2122 2123 J_ASSERT(PageLocked(page)); 2124 2125 head = page_buffers(page); 2126 bh = head; 2127 do { 2128 struct journal_head *jh; 2129 2130 /* 2131 * We take our own ref against the journal_head here to avoid 2132 * having to add tons of locking around each instance of 2133 * jbd2_journal_put_journal_head(). 2134 */ 2135 jh = jbd2_journal_grab_journal_head(bh); 2136 if (!jh) 2137 continue; 2138 2139 spin_lock(&jh->b_state_lock); 2140 __journal_try_to_free_buffer(journal, bh); 2141 spin_unlock(&jh->b_state_lock); 2142 jbd2_journal_put_journal_head(jh); 2143 if (buffer_jbd(bh)) 2144 goto busy; 2145 2146 /* 2147 * If we free a metadata buffer which has been failed to 2148 * write out, the jbd2 checkpoint procedure will not detect 2149 * this failure and may lead to filesystem inconsistency 2150 * after cleanup journal tail. 2151 */ 2152 if (buffer_write_io_error(bh)) { 2153 pr_err("JBD2: Error while async write back metadata bh %llu.", 2154 (unsigned long long)bh->b_blocknr); 2155 has_write_io_error = true; 2156 } 2157 } while ((bh = bh->b_this_page) != head); 2158 2159 ret = try_to_free_buffers(page); 2160 2161 busy: 2162 if (has_write_io_error) 2163 jbd2_journal_abort(journal, -EIO); 2164 2165 return ret; 2166 } 2167 2168 /* 2169 * This buffer is no longer needed. If it is on an older transaction's 2170 * checkpoint list we need to record it on this transaction's forget list 2171 * to pin this buffer (and hence its checkpointing transaction) down until 2172 * this transaction commits. If the buffer isn't on a checkpoint list, we 2173 * release it. 2174 * Returns non-zero if JBD no longer has an interest in the buffer. 2175 * 2176 * Called under j_list_lock. 2177 * 2178 * Called under jh->b_state_lock. 2179 */ 2180 static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction) 2181 { 2182 int may_free = 1; 2183 struct buffer_head *bh = jh2bh(jh); 2184 2185 if (jh->b_cp_transaction) { 2186 JBUFFER_TRACE(jh, "on running+cp transaction"); 2187 __jbd2_journal_temp_unlink_buffer(jh); 2188 /* 2189 * We don't want to write the buffer anymore, clear the 2190 * bit so that we don't confuse checks in 2191 * __journal_file_buffer 2192 */ 2193 clear_buffer_dirty(bh); 2194 __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); 2195 may_free = 0; 2196 } else { 2197 JBUFFER_TRACE(jh, "on running transaction"); 2198 __jbd2_journal_unfile_buffer(jh); 2199 jbd2_journal_put_journal_head(jh); 2200 } 2201 return may_free; 2202 } 2203 2204 /* 2205 * jbd2_journal_invalidatepage 2206 * 2207 * This code is tricky. It has a number of cases to deal with. 2208 * 2209 * There are two invariants which this code relies on: 2210 * 2211 * i_size must be updated on disk before we start calling invalidatepage on the 2212 * data. 2213 * 2214 * This is done in ext3 by defining an ext3_setattr method which 2215 * updates i_size before truncate gets going. By maintaining this 2216 * invariant, we can be sure that it is safe to throw away any buffers 2217 * attached to the current transaction: once the transaction commits, 2218 * we know that the data will not be needed. 2219 * 2220 * Note however that we can *not* throw away data belonging to the 2221 * previous, committing transaction! 2222 * 2223 * Any disk blocks which *are* part of the previous, committing 2224 * transaction (and which therefore cannot be discarded immediately) are 2225 * not going to be reused in the new running transaction 2226 * 2227 * The bitmap committed_data images guarantee this: any block which is 2228 * allocated in one transaction and removed in the next will be marked 2229 * as in-use in the committed_data bitmap, so cannot be reused until 2230 * the next transaction to delete the block commits. This means that 2231 * leaving committing buffers dirty is quite safe: the disk blocks 2232 * cannot be reallocated to a different file and so buffer aliasing is 2233 * not possible. 2234 * 2235 * 2236 * The above applies mainly to ordered data mode. In writeback mode we 2237 * don't make guarantees about the order in which data hits disk --- in 2238 * particular we don't guarantee that new dirty data is flushed before 2239 * transaction commit --- so it is always safe just to discard data 2240 * immediately in that mode. --sct 2241 */ 2242 2243 /* 2244 * The journal_unmap_buffer helper function returns zero if the buffer 2245 * concerned remains pinned as an anonymous buffer belonging to an older 2246 * transaction. 2247 * 2248 * We're outside-transaction here. Either or both of j_running_transaction 2249 * and j_committing_transaction may be NULL. 2250 */ 2251 static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh, 2252 int partial_page) 2253 { 2254 transaction_t *transaction; 2255 struct journal_head *jh; 2256 int may_free = 1; 2257 2258 BUFFER_TRACE(bh, "entry"); 2259 2260 /* 2261 * It is safe to proceed here without the j_list_lock because the 2262 * buffers cannot be stolen by try_to_free_buffers as long as we are 2263 * holding the page lock. --sct 2264 */ 2265 2266 jh = jbd2_journal_grab_journal_head(bh); 2267 if (!jh) 2268 goto zap_buffer_unlocked; 2269 2270 /* OK, we have data buffer in journaled mode */ 2271 write_lock(&journal->j_state_lock); 2272 spin_lock(&jh->b_state_lock); 2273 spin_lock(&journal->j_list_lock); 2274 2275 /* 2276 * We cannot remove the buffer from checkpoint lists until the 2277 * transaction adding inode to orphan list (let's call it T) 2278 * is committed. Otherwise if the transaction changing the 2279 * buffer would be cleaned from the journal before T is 2280 * committed, a crash will cause that the correct contents of 2281 * the buffer will be lost. On the other hand we have to 2282 * clear the buffer dirty bit at latest at the moment when the 2283 * transaction marking the buffer as freed in the filesystem 2284 * structures is committed because from that moment on the 2285 * block can be reallocated and used by a different page. 2286 * Since the block hasn't been freed yet but the inode has 2287 * already been added to orphan list, it is safe for us to add 2288 * the buffer to BJ_Forget list of the newest transaction. 2289 * 2290 * Also we have to clear buffer_mapped flag of a truncated buffer 2291 * because the buffer_head may be attached to the page straddling 2292 * i_size (can happen only when blocksize < pagesize) and thus the 2293 * buffer_head can be reused when the file is extended again. So we end 2294 * up keeping around invalidated buffers attached to transactions' 2295 * BJ_Forget list just to stop checkpointing code from cleaning up 2296 * the transaction this buffer was modified in. 2297 */ 2298 transaction = jh->b_transaction; 2299 if (transaction == NULL) { 2300 /* First case: not on any transaction. If it 2301 * has no checkpoint link, then we can zap it: 2302 * it's a writeback-mode buffer so we don't care 2303 * if it hits disk safely. */ 2304 if (!jh->b_cp_transaction) { 2305 JBUFFER_TRACE(jh, "not on any transaction: zap"); 2306 goto zap_buffer; 2307 } 2308 2309 if (!buffer_dirty(bh)) { 2310 /* bdflush has written it. We can drop it now */ 2311 __jbd2_journal_remove_checkpoint(jh); 2312 goto zap_buffer; 2313 } 2314 2315 /* OK, it must be in the journal but still not 2316 * written fully to disk: it's metadata or 2317 * journaled data... */ 2318 2319 if (journal->j_running_transaction) { 2320 /* ... and once the current transaction has 2321 * committed, the buffer won't be needed any 2322 * longer. */ 2323 JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget"); 2324 may_free = __dispose_buffer(jh, 2325 journal->j_running_transaction); 2326 goto zap_buffer; 2327 } else { 2328 /* There is no currently-running transaction. So the 2329 * orphan record which we wrote for this file must have 2330 * passed into commit. We must attach this buffer to 2331 * the committing transaction, if it exists. */ 2332 if (journal->j_committing_transaction) { 2333 JBUFFER_TRACE(jh, "give to committing trans"); 2334 may_free = __dispose_buffer(jh, 2335 journal->j_committing_transaction); 2336 goto zap_buffer; 2337 } else { 2338 /* The orphan record's transaction has 2339 * committed. We can cleanse this buffer */ 2340 clear_buffer_jbddirty(bh); 2341 __jbd2_journal_remove_checkpoint(jh); 2342 goto zap_buffer; 2343 } 2344 } 2345 } else if (transaction == journal->j_committing_transaction) { 2346 JBUFFER_TRACE(jh, "on committing transaction"); 2347 /* 2348 * The buffer is committing, we simply cannot touch 2349 * it. If the page is straddling i_size we have to wait 2350 * for commit and try again. 2351 */ 2352 if (partial_page) { 2353 spin_unlock(&journal->j_list_lock); 2354 spin_unlock(&jh->b_state_lock); 2355 write_unlock(&journal->j_state_lock); 2356 jbd2_journal_put_journal_head(jh); 2357 return -EBUSY; 2358 } 2359 /* 2360 * OK, buffer won't be reachable after truncate. We just clear 2361 * b_modified to not confuse transaction credit accounting, and 2362 * set j_next_transaction to the running transaction (if there 2363 * is one) and mark buffer as freed so that commit code knows 2364 * it should clear dirty bits when it is done with the buffer. 2365 */ 2366 set_buffer_freed(bh); 2367 if (journal->j_running_transaction && buffer_jbddirty(bh)) 2368 jh->b_next_transaction = journal->j_running_transaction; 2369 jh->b_modified = 0; 2370 spin_unlock(&journal->j_list_lock); 2371 spin_unlock(&jh->b_state_lock); 2372 write_unlock(&journal->j_state_lock); 2373 jbd2_journal_put_journal_head(jh); 2374 return 0; 2375 } else { 2376 /* Good, the buffer belongs to the running transaction. 2377 * We are writing our own transaction's data, not any 2378 * previous one's, so it is safe to throw it away 2379 * (remember that we expect the filesystem to have set 2380 * i_size already for this truncate so recovery will not 2381 * expose the disk blocks we are discarding here.) */ 2382 J_ASSERT_JH(jh, transaction == journal->j_running_transaction); 2383 JBUFFER_TRACE(jh, "on running transaction"); 2384 may_free = __dispose_buffer(jh, transaction); 2385 } 2386 2387 zap_buffer: 2388 /* 2389 * This is tricky. Although the buffer is truncated, it may be reused 2390 * if blocksize < pagesize and it is attached to the page straddling 2391 * EOF. Since the buffer might have been added to BJ_Forget list of the 2392 * running transaction, journal_get_write_access() won't clear 2393 * b_modified and credit accounting gets confused. So clear b_modified 2394 * here. 2395 */ 2396 jh->b_modified = 0; 2397 spin_unlock(&journal->j_list_lock); 2398 spin_unlock(&jh->b_state_lock); 2399 write_unlock(&journal->j_state_lock); 2400 jbd2_journal_put_journal_head(jh); 2401 zap_buffer_unlocked: 2402 clear_buffer_dirty(bh); 2403 J_ASSERT_BH(bh, !buffer_jbddirty(bh)); 2404 clear_buffer_mapped(bh); 2405 clear_buffer_req(bh); 2406 clear_buffer_new(bh); 2407 clear_buffer_delay(bh); 2408 clear_buffer_unwritten(bh); 2409 bh->b_bdev = NULL; 2410 return may_free; 2411 } 2412 2413 /** 2414 * void jbd2_journal_invalidatepage() 2415 * @journal: journal to use for flush... 2416 * @page: page to flush 2417 * @offset: start of the range to invalidate 2418 * @length: length of the range to invalidate 2419 * 2420 * Reap page buffers containing data after in the specified range in page. 2421 * Can return -EBUSY if buffers are part of the committing transaction and 2422 * the page is straddling i_size. Caller then has to wait for current commit 2423 * and try again. 2424 */ 2425 int jbd2_journal_invalidatepage(journal_t *journal, 2426 struct page *page, 2427 unsigned int offset, 2428 unsigned int length) 2429 { 2430 struct buffer_head *head, *bh, *next; 2431 unsigned int stop = offset + length; 2432 unsigned int curr_off = 0; 2433 int partial_page = (offset || length < PAGE_SIZE); 2434 int may_free = 1; 2435 int ret = 0; 2436 2437 if (!PageLocked(page)) 2438 BUG(); 2439 if (!page_has_buffers(page)) 2440 return 0; 2441 2442 BUG_ON(stop > PAGE_SIZE || stop < length); 2443 2444 /* We will potentially be playing with lists other than just the 2445 * data lists (especially for journaled data mode), so be 2446 * cautious in our locking. */ 2447 2448 head = bh = page_buffers(page); 2449 do { 2450 unsigned int next_off = curr_off + bh->b_size; 2451 next = bh->b_this_page; 2452 2453 if (next_off > stop) 2454 return 0; 2455 2456 if (offset <= curr_off) { 2457 /* This block is wholly outside the truncation point */ 2458 lock_buffer(bh); 2459 ret = journal_unmap_buffer(journal, bh, partial_page); 2460 unlock_buffer(bh); 2461 if (ret < 0) 2462 return ret; 2463 may_free &= ret; 2464 } 2465 curr_off = next_off; 2466 bh = next; 2467 2468 } while (bh != head); 2469 2470 if (!partial_page) { 2471 if (may_free && try_to_free_buffers(page)) 2472 J_ASSERT(!page_has_buffers(page)); 2473 } 2474 return 0; 2475 } 2476 2477 /* 2478 * File a buffer on the given transaction list. 2479 */ 2480 void __jbd2_journal_file_buffer(struct journal_head *jh, 2481 transaction_t *transaction, int jlist) 2482 { 2483 struct journal_head **list = NULL; 2484 int was_dirty = 0; 2485 struct buffer_head *bh = jh2bh(jh); 2486 2487 lockdep_assert_held(&jh->b_state_lock); 2488 assert_spin_locked(&transaction->t_journal->j_list_lock); 2489 2490 J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); 2491 J_ASSERT_JH(jh, jh->b_transaction == transaction || 2492 jh->b_transaction == NULL); 2493 2494 if (jh->b_transaction && jh->b_jlist == jlist) 2495 return; 2496 2497 if (jlist == BJ_Metadata || jlist == BJ_Reserved || 2498 jlist == BJ_Shadow || jlist == BJ_Forget) { 2499 /* 2500 * For metadata buffers, we track dirty bit in buffer_jbddirty 2501 * instead of buffer_dirty. We should not see a dirty bit set 2502 * here because we clear it in do_get_write_access but e.g. 2503 * tune2fs can modify the sb and set the dirty bit at any time 2504 * so we try to gracefully handle that. 2505 */ 2506 if (buffer_dirty(bh)) 2507 warn_dirty_buffer(bh); 2508 if (test_clear_buffer_dirty(bh) || 2509 test_clear_buffer_jbddirty(bh)) 2510 was_dirty = 1; 2511 } 2512 2513 if (jh->b_transaction) 2514 __jbd2_journal_temp_unlink_buffer(jh); 2515 else 2516 jbd2_journal_grab_journal_head(bh); 2517 jh->b_transaction = transaction; 2518 2519 switch (jlist) { 2520 case BJ_None: 2521 J_ASSERT_JH(jh, !jh->b_committed_data); 2522 J_ASSERT_JH(jh, !jh->b_frozen_data); 2523 return; 2524 case BJ_Metadata: 2525 transaction->t_nr_buffers++; 2526 list = &transaction->t_buffers; 2527 break; 2528 case BJ_Forget: 2529 list = &transaction->t_forget; 2530 break; 2531 case BJ_Shadow: 2532 list = &transaction->t_shadow_list; 2533 break; 2534 case BJ_Reserved: 2535 list = &transaction->t_reserved_list; 2536 break; 2537 } 2538 2539 __blist_add_buffer(list, jh); 2540 jh->b_jlist = jlist; 2541 2542 if (was_dirty) 2543 set_buffer_jbddirty(bh); 2544 } 2545 2546 void jbd2_journal_file_buffer(struct journal_head *jh, 2547 transaction_t *transaction, int jlist) 2548 { 2549 spin_lock(&jh->b_state_lock); 2550 spin_lock(&transaction->t_journal->j_list_lock); 2551 __jbd2_journal_file_buffer(jh, transaction, jlist); 2552 spin_unlock(&transaction->t_journal->j_list_lock); 2553 spin_unlock(&jh->b_state_lock); 2554 } 2555 2556 /* 2557 * Remove a buffer from its current buffer list in preparation for 2558 * dropping it from its current transaction entirely. If the buffer has 2559 * already started to be used by a subsequent transaction, refile the 2560 * buffer on that transaction's metadata list. 2561 * 2562 * Called under j_list_lock 2563 * Called under jh->b_state_lock 2564 * 2565 * When this function returns true, there's no next transaction to refile to 2566 * and the caller has to drop jh reference through 2567 * jbd2_journal_put_journal_head(). 2568 */ 2569 bool __jbd2_journal_refile_buffer(struct journal_head *jh) 2570 { 2571 int was_dirty, jlist; 2572 struct buffer_head *bh = jh2bh(jh); 2573 2574 lockdep_assert_held(&jh->b_state_lock); 2575 if (jh->b_transaction) 2576 assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock); 2577 2578 /* If the buffer is now unused, just drop it. */ 2579 if (jh->b_next_transaction == NULL) { 2580 __jbd2_journal_unfile_buffer(jh); 2581 return true; 2582 } 2583 2584 /* 2585 * It has been modified by a later transaction: add it to the new 2586 * transaction's metadata list. 2587 */ 2588 2589 was_dirty = test_clear_buffer_jbddirty(bh); 2590 __jbd2_journal_temp_unlink_buffer(jh); 2591 2592 /* 2593 * b_transaction must be set, otherwise the new b_transaction won't 2594 * be holding jh reference 2595 */ 2596 J_ASSERT_JH(jh, jh->b_transaction != NULL); 2597 2598 /* 2599 * We set b_transaction here because b_next_transaction will inherit 2600 * our jh reference and thus __jbd2_journal_file_buffer() must not 2601 * take a new one. 2602 */ 2603 WRITE_ONCE(jh->b_transaction, jh->b_next_transaction); 2604 WRITE_ONCE(jh->b_next_transaction, NULL); 2605 if (buffer_freed(bh)) 2606 jlist = BJ_Forget; 2607 else if (jh->b_modified) 2608 jlist = BJ_Metadata; 2609 else 2610 jlist = BJ_Reserved; 2611 __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist); 2612 J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING); 2613 2614 if (was_dirty) 2615 set_buffer_jbddirty(bh); 2616 return false; 2617 } 2618 2619 /* 2620 * __jbd2_journal_refile_buffer() with necessary locking added. We take our 2621 * bh reference so that we can safely unlock bh. 2622 * 2623 * The jh and bh may be freed by this call. 2624 */ 2625 void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh) 2626 { 2627 bool drop; 2628 2629 spin_lock(&jh->b_state_lock); 2630 spin_lock(&journal->j_list_lock); 2631 drop = __jbd2_journal_refile_buffer(jh); 2632 spin_unlock(&jh->b_state_lock); 2633 spin_unlock(&journal->j_list_lock); 2634 if (drop) 2635 jbd2_journal_put_journal_head(jh); 2636 } 2637 2638 /* 2639 * File inode in the inode list of the handle's transaction 2640 */ 2641 static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode, 2642 unsigned long flags, loff_t start_byte, loff_t end_byte) 2643 { 2644 transaction_t *transaction = handle->h_transaction; 2645 journal_t *journal; 2646 2647 if (is_handle_aborted(handle)) 2648 return -EROFS; 2649 journal = transaction->t_journal; 2650 2651 jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino, 2652 transaction->t_tid); 2653 2654 spin_lock(&journal->j_list_lock); 2655 jinode->i_flags |= flags; 2656 2657 if (jinode->i_dirty_end) { 2658 jinode->i_dirty_start = min(jinode->i_dirty_start, start_byte); 2659 jinode->i_dirty_end = max(jinode->i_dirty_end, end_byte); 2660 } else { 2661 jinode->i_dirty_start = start_byte; 2662 jinode->i_dirty_end = end_byte; 2663 } 2664 2665 /* Is inode already attached where we need it? */ 2666 if (jinode->i_transaction == transaction || 2667 jinode->i_next_transaction == transaction) 2668 goto done; 2669 2670 /* 2671 * We only ever set this variable to 1 so the test is safe. Since 2672 * t_need_data_flush is likely to be set, we do the test to save some 2673 * cacheline bouncing 2674 */ 2675 if (!transaction->t_need_data_flush) 2676 transaction->t_need_data_flush = 1; 2677 /* On some different transaction's list - should be 2678 * the committing one */ 2679 if (jinode->i_transaction) { 2680 J_ASSERT(jinode->i_next_transaction == NULL); 2681 J_ASSERT(jinode->i_transaction == 2682 journal->j_committing_transaction); 2683 jinode->i_next_transaction = transaction; 2684 goto done; 2685 } 2686 /* Not on any transaction list... */ 2687 J_ASSERT(!jinode->i_next_transaction); 2688 jinode->i_transaction = transaction; 2689 list_add(&jinode->i_list, &transaction->t_inode_list); 2690 done: 2691 spin_unlock(&journal->j_list_lock); 2692 2693 return 0; 2694 } 2695 2696 int jbd2_journal_inode_ranged_write(handle_t *handle, 2697 struct jbd2_inode *jinode, loff_t start_byte, loff_t length) 2698 { 2699 return jbd2_journal_file_inode(handle, jinode, 2700 JI_WRITE_DATA | JI_WAIT_DATA, start_byte, 2701 start_byte + length - 1); 2702 } 2703 2704 int jbd2_journal_inode_ranged_wait(handle_t *handle, struct jbd2_inode *jinode, 2705 loff_t start_byte, loff_t length) 2706 { 2707 return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA, 2708 start_byte, start_byte + length - 1); 2709 } 2710 2711 /* 2712 * File truncate and transaction commit interact with each other in a 2713 * non-trivial way. If a transaction writing data block A is 2714 * committing, we cannot discard the data by truncate until we have 2715 * written them. Otherwise if we crashed after the transaction with 2716 * write has committed but before the transaction with truncate has 2717 * committed, we could see stale data in block A. This function is a 2718 * helper to solve this problem. It starts writeout of the truncated 2719 * part in case it is in the committing transaction. 2720 * 2721 * Filesystem code must call this function when inode is journaled in 2722 * ordered mode before truncation happens and after the inode has been 2723 * placed on orphan list with the new inode size. The second condition 2724 * avoids the race that someone writes new data and we start 2725 * committing the transaction after this function has been called but 2726 * before a transaction for truncate is started (and furthermore it 2727 * allows us to optimize the case where the addition to orphan list 2728 * happens in the same transaction as write --- we don't have to write 2729 * any data in such case). 2730 */ 2731 int jbd2_journal_begin_ordered_truncate(journal_t *journal, 2732 struct jbd2_inode *jinode, 2733 loff_t new_size) 2734 { 2735 transaction_t *inode_trans, *commit_trans; 2736 int ret = 0; 2737 2738 /* This is a quick check to avoid locking if not necessary */ 2739 if (!jinode->i_transaction) 2740 goto out; 2741 /* Locks are here just to force reading of recent values, it is 2742 * enough that the transaction was not committing before we started 2743 * a transaction adding the inode to orphan list */ 2744 read_lock(&journal->j_state_lock); 2745 commit_trans = journal->j_committing_transaction; 2746 read_unlock(&journal->j_state_lock); 2747 spin_lock(&journal->j_list_lock); 2748 inode_trans = jinode->i_transaction; 2749 spin_unlock(&journal->j_list_lock); 2750 if (inode_trans == commit_trans) { 2751 ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping, 2752 new_size, LLONG_MAX); 2753 if (ret) 2754 jbd2_journal_abort(journal, ret); 2755 } 2756 out: 2757 return ret; 2758 } 2759