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