1 // SPDX-License-Identifier: GPL-2.0+ 1 2 /* 3 * linux/fs/jbd2/commit.c 4 * 5 * Written by Stephen C. Tweedie <sct@redhat.c 6 * 7 * Copyright 1998 Red Hat corp --- All Rights 8 * 9 * Journal commit routines for the generic fil 10 * part of the ext2fs journaling system. 11 */ 12 13 #include <linux/time.h> 14 #include <linux/fs.h> 15 #include <linux/jbd2.h> 16 #include <linux/errno.h> 17 #include <linux/slab.h> 18 #include <linux/mm.h> 19 #include <linux/pagemap.h> 20 #include <linux/jiffies.h> 21 #include <linux/crc32.h> 22 #include <linux/writeback.h> 23 #include <linux/backing-dev.h> 24 #include <linux/bio.h> 25 #include <linux/blkdev.h> 26 #include <linux/bitops.h> 27 #include <trace/events/jbd2.h> 28 29 /* 30 * IO end handler for temporary buffer_heads h 31 */ 32 static void journal_end_buffer_io_sync(struct 33 { 34 struct buffer_head *orig_bh = bh->b_pr 35 36 BUFFER_TRACE(bh, ""); 37 if (uptodate) 38 set_buffer_uptodate(bh); 39 else 40 clear_buffer_uptodate(bh); 41 if (orig_bh) { 42 clear_bit_unlock(BH_Shadow, &o 43 smp_mb__after_atomic(); 44 wake_up_bit(&orig_bh->b_state, 45 } 46 unlock_buffer(bh); 47 } 48 49 /* 50 * When an ext4 file is truncated, it is possi 51 * successfully freed, because they are attach 52 * After the transaction commits, these pages 53 * ->mapping, and with attached buffers. Thes 54 * by the VM, but their apparent absence upset 55 * the numbers in /proc/meminfo look odd. 56 * 57 * So here, we have a buffer which has just co 58 * see if we can strip all buffers from the ba 59 * 60 * Called under lock_journal(), and possibly u 61 * caller provided us with a ref against the b 62 */ 63 static void release_buffer_page(struct buffer_ 64 { 65 struct folio *folio; 66 67 if (buffer_dirty(bh)) 68 goto nope; 69 if (atomic_read(&bh->b_count) != 1) 70 goto nope; 71 folio = bh->b_folio; 72 if (folio->mapping) 73 goto nope; 74 75 /* OK, it's a truncated page */ 76 if (!folio_trylock(folio)) 77 goto nope; 78 79 folio_get(folio); 80 __brelse(bh); 81 try_to_free_buffers(folio); 82 folio_unlock(folio); 83 folio_put(folio); 84 return; 85 86 nope: 87 __brelse(bh); 88 } 89 90 static void jbd2_commit_block_csum_set(journal 91 { 92 struct commit_header *h; 93 __u32 csum; 94 95 if (!jbd2_journal_has_csum_v2or3(j)) 96 return; 97 98 h = (struct commit_header *)(bh->b_dat 99 h->h_chksum_type = 0; 100 h->h_chksum_size = 0; 101 h->h_chksum[0] = 0; 102 csum = jbd2_chksum(j, j->j_csum_seed, 103 h->h_chksum[0] = cpu_to_be32(csum); 104 } 105 106 /* 107 * Done it all: now submit the commit record. 108 * cleaned up our previous buffers by now, so 109 * mode we can now just skip the rest of the j 110 * entirely. 111 * 112 * Returns 1 if the journal needs to be aborte 113 */ 114 static int journal_submit_commit_record(journa 115 transa 116 struct 117 __u32 118 { 119 struct commit_header *tmp; 120 struct buffer_head *bh; 121 struct timespec64 now; 122 blk_opf_t write_flags = REQ_OP_WRITE | 123 124 *cbh = NULL; 125 126 if (is_journal_aborted(journal)) 127 return 0; 128 129 bh = jbd2_journal_get_descriptor_buffe 130 131 if (!bh) 132 return 1; 133 134 tmp = (struct commit_header *)bh->b_da 135 ktime_get_coarse_real_ts64(&now); 136 tmp->h_commit_sec = cpu_to_be64(now.tv 137 tmp->h_commit_nsec = cpu_to_be32(now.t 138 139 if (jbd2_has_feature_checksum(journal) 140 tmp->h_chksum_type = JBD2 141 tmp->h_chksum_size = JBD2 142 tmp->h_chksum[0] = cpu_ 143 } 144 jbd2_commit_block_csum_set(journal, bh 145 146 BUFFER_TRACE(bh, "submit commit block" 147 lock_buffer(bh); 148 clear_buffer_dirty(bh); 149 set_buffer_uptodate(bh); 150 bh->b_end_io = journal_end_buffer_io_s 151 152 if (journal->j_flags & JBD2_BARRIER && 153 !jbd2_has_feature_async_commit(jou 154 write_flags |= REQ_PREFLUSH | 155 156 submit_bh(write_flags, bh); 157 *cbh = bh; 158 return 0; 159 } 160 161 /* 162 * This function along with journal_submit_com 163 * allows to write the commit record asynchron 164 */ 165 static int journal_wait_on_commit_record(journ 166 struc 167 { 168 int ret = 0; 169 170 clear_buffer_dirty(bh); 171 wait_on_buffer(bh); 172 173 if (unlikely(!buffer_uptodate(bh))) 174 ret = -EIO; 175 put_bh(bh); /* One for getb 176 177 return ret; 178 } 179 180 /* Send all the data buffers related to an ino 181 int jbd2_submit_inode_data(journal_t *journal, 182 { 183 if (!jinode || !(jinode->i_flags & JI_ 184 return 0; 185 186 trace_jbd2_submit_inode_data(jinode->i 187 return journal->j_submit_inode_data_bu 188 189 } 190 EXPORT_SYMBOL(jbd2_submit_inode_data); 191 192 int jbd2_wait_inode_data(journal_t *journal, s 193 { 194 if (!jinode || !(jinode->i_flags & JI_ 195 !jinode->i_vfs_inode || !jinod 196 return 0; 197 return filemap_fdatawait_range_keep_er 198 jinode->i_vfs_inode->i_mapping 199 jinode->i_dirty_end); 200 } 201 EXPORT_SYMBOL(jbd2_wait_inode_data); 202 203 /* 204 * Submit all the data buffers of inode associ 205 * disk. 206 * 207 * We are in a committing transaction. Therefo 208 * our inode list. We use JI_COMMIT_RUNNING fl 209 * operate on from being released while we wri 210 */ 211 static int journal_submit_data_buffers(journal 212 transaction_t *commit_transact 213 { 214 struct jbd2_inode *jinode; 215 int err, ret = 0; 216 217 spin_lock(&journal->j_list_lock); 218 list_for_each_entry(jinode, &commit_tr 219 if (!(jinode->i_flags & JI_WRI 220 continue; 221 jinode->i_flags |= JI_COMMIT_R 222 spin_unlock(&journal->j_list_l 223 /* submit the inode data buffe 224 trace_jbd2_submit_inode_data(j 225 if (journal->j_submit_inode_da 226 err = journal->j_submi 227 if (!ret) 228 ret = err; 229 } 230 spin_lock(&journal->j_list_loc 231 J_ASSERT(jinode->i_transaction 232 jinode->i_flags &= ~JI_COMMIT_ 233 smp_mb(); 234 wake_up_bit(&jinode->i_flags, 235 } 236 spin_unlock(&journal->j_list_lock); 237 return ret; 238 } 239 240 int jbd2_journal_finish_inode_data_buffers(str 241 { 242 struct address_space *mapping = jinode 243 244 return filemap_fdatawait_range_keep_er 245 246 247 } 248 249 /* 250 * Wait for data submitted for writeout, refil 251 * transaction if needed. 252 * 253 */ 254 static int journal_finish_inode_data_buffers(j 255 transaction_t *commit_transact 256 { 257 struct jbd2_inode *jinode, *next_i; 258 int err, ret = 0; 259 260 /* For locking, see the comment in jou 261 spin_lock(&journal->j_list_lock); 262 list_for_each_entry(jinode, &commit_tr 263 if (!(jinode->i_flags & JI_WAI 264 continue; 265 jinode->i_flags |= JI_COMMIT_R 266 spin_unlock(&journal->j_list_l 267 /* wait for the inode data buf 268 if (journal->j_finish_inode_da 269 err = journal->j_finis 270 if (!ret) 271 ret = err; 272 } 273 cond_resched(); 274 spin_lock(&journal->j_list_loc 275 jinode->i_flags &= ~JI_COMMIT_ 276 smp_mb(); 277 wake_up_bit(&jinode->i_flags, 278 } 279 280 /* Now refile inode to proper lists */ 281 list_for_each_entry_safe(jinode, next_ 282 &commit_trans 283 list_del(&jinode->i_list); 284 if (jinode->i_next_transaction 285 jinode->i_transaction 286 jinode->i_next_transac 287 list_add(&jinode->i_li 288 &jinode->i_tra 289 } else { 290 jinode->i_transaction 291 jinode->i_dirty_start 292 jinode->i_dirty_end = 293 } 294 } 295 spin_unlock(&journal->j_list_lock); 296 297 return ret; 298 } 299 300 static __u32 jbd2_checksum_data(__u32 crc32_su 301 { 302 char *addr; 303 __u32 checksum; 304 305 addr = kmap_local_folio(bh->b_folio, b 306 checksum = crc32_be(crc32_sum, addr, b 307 kunmap_local(addr); 308 309 return checksum; 310 } 311 312 static void write_tag_block(journal_t *j, jour 313 unsigned lo 314 { 315 tag->t_blocknr = cpu_to_be32(block & ( 316 if (jbd2_has_feature_64bit(j)) 317 tag->t_blocknr_high = cpu_to_b 318 } 319 320 static void jbd2_block_tag_csum_set(journal_t 321 struct buf 322 { 323 journal_block_tag3_t *tag3 = (journal_ 324 __u8 *addr; 325 __u32 csum32; 326 __be32 seq; 327 328 if (!jbd2_journal_has_csum_v2or3(j)) 329 return; 330 331 seq = cpu_to_be32(sequence); 332 addr = kmap_local_folio(bh->b_folio, b 333 csum32 = jbd2_chksum(j, j->j_csum_seed 334 csum32 = jbd2_chksum(j, csum32, addr, 335 kunmap_local(addr); 336 337 if (jbd2_has_feature_csum3(j)) 338 tag3->t_checksum = cpu_to_be32 339 else 340 tag->t_checksum = cpu_to_be16( 341 } 342 /* 343 * jbd2_journal_commit_transaction 344 * 345 * The primary function for committing a trans 346 * function is called by the journal thread to 347 */ 348 void jbd2_journal_commit_transaction(journal_t 349 { 350 struct transaction_stats_s stats; 351 transaction_t *commit_transaction; 352 struct journal_head *jh; 353 struct buffer_head *descriptor; 354 struct buffer_head **wbuf = journal->j 355 int bufs; 356 int escape; 357 int err; 358 unsigned long long blocknr; 359 ktime_t start_time; 360 u64 commit_time; 361 char *tagp = NULL; 362 journal_block_tag_t *tag = NULL; 363 int space_left = 0; 364 int first_tag = 0; 365 int tag_flag; 366 int i; 367 int tag_bytes = journal_tag_bytes(jour 368 struct buffer_head *cbh = NULL; /* For 369 __u32 crc32_sum = ~0; 370 struct blk_plug plug; 371 /* Tail of the journal */ 372 unsigned long first_block; 373 tid_t first_tid; 374 int update_tail; 375 int csum_size = 0; 376 LIST_HEAD(io_bufs); 377 LIST_HEAD(log_bufs); 378 379 if (jbd2_journal_has_csum_v2or3(journa 380 csum_size = sizeof(struct jbd2 381 382 /* 383 * First job: lock down the current tr 384 * all outstanding updates to complete 385 */ 386 387 /* Do we need to erase the effects of 388 if (journal->j_flags & JBD2_FLUSHED) { 389 jbd2_debug(3, "super block upd 390 mutex_lock_io(&journal->j_chec 391 /* 392 * We hold j_checkpoint_mutex 393 * We don't need any special d 394 * since journal is empty and 395 * flushed only with transacti 396 */ 397 jbd2_journal_update_sb_log_tai 398 399 400 mutex_unlock(&journal->j_check 401 } else { 402 jbd2_debug(3, "superblock not 403 } 404 405 J_ASSERT(journal->j_running_transactio 406 J_ASSERT(journal->j_committing_transac 407 408 write_lock(&journal->j_state_lock); 409 journal->j_flags |= JBD2_FULL_COMMIT_O 410 while (journal->j_flags & JBD2_FAST_CO 411 DEFINE_WAIT(wait); 412 413 prepare_to_wait(&journal->j_fc 414 TASK_UNINTERRU 415 write_unlock(&journal->j_state 416 schedule(); 417 write_lock(&journal->j_state_l 418 finish_wait(&journal->j_fc_wai 419 /* 420 * TODO: by blocking fast comm 421 * fsync() latency slightly. S 422 * to block fast commits until 423 * state. So an optimization i 424 * commits here and wait for e 425 * just before we enter T_FLUS 426 * commits and this full commi 427 */ 428 } 429 write_unlock(&journal->j_state_lock); 430 431 commit_transaction = journal->j_runnin 432 433 trace_jbd2_start_commit(journal, commi 434 jbd2_debug(1, "JBD2: starting commit o 435 commit_transaction->t_ 436 437 write_lock(&journal->j_state_lock); 438 journal->j_fc_off = 0; 439 J_ASSERT(commit_transaction->t_state = 440 commit_transaction->t_state = T_LOCKED 441 442 trace_jbd2_commit_locking(journal, com 443 stats.run.rs_wait = commit_transaction 444 stats.run.rs_request_delay = 0; 445 stats.run.rs_locked = jiffies; 446 if (commit_transaction->t_requested) 447 stats.run.rs_request_delay = 448 jbd2_time_diff(commit_ 449 stats.r 450 stats.run.rs_running = jbd2_time_diff( 451 452 453 // waits for any t_updates to finish 454 jbd2_journal_wait_updates(journal); 455 456 commit_transaction->t_state = T_SWITCH 457 458 J_ASSERT (atomic_read(&commit_transact 459 journal->j_max_transac 460 461 /* 462 * First thing we are allowed to do is 463 * BJ_Reserved buffers. Note, it is _ 464 * that there are no such buffers: if 465 * operation like a truncate needs to 466 * transactions, then it may try to do 467 * there are still BJ_Reserved buffers 468 * be released cleanly from the curren 469 * 470 * In this case, the filesystem must s 471 * again before modifying the buffer i 472 * we do not require it to remember ex 473 * has reserved. This is consistent w 474 * that multiple jbd2_journal_get_writ 475 * buffer are perfectly permissible. 476 * We use journal->j_state_lock here t 477 * t_reserved_list with eviction of bu 478 */ 479 while (commit_transaction->t_reserved_ 480 jh = commit_transaction->t_res 481 JBUFFER_TRACE(jh, "reserved, u 482 /* 483 * A jbd2_journal_get_undo_acc 484 * leave undo-committed data. 485 */ 486 if (jh->b_committed_data) { 487 struct buffer_head *bh 488 489 spin_lock(&jh->b_state 490 jbd2_free(jh->b_commit 491 jh->b_committed_data = 492 spin_unlock(&jh->b_sta 493 } 494 jbd2_journal_refile_buffer(jou 495 } 496 497 write_unlock(&journal->j_state_lock); 498 /* 499 * Now try to drop any written-back bu 500 * checkpoint lists. We do this *befo 501 * frees some memory 502 */ 503 spin_lock(&journal->j_list_lock); 504 __jbd2_journal_clean_checkpoint_list(j 505 spin_unlock(&journal->j_list_lock); 506 507 jbd2_debug(3, "JBD2: commit phase 1\n" 508 509 /* 510 * Clear revoked flag to reflect there 511 * in the next transaction which is go 512 */ 513 jbd2_clear_buffer_revoked_flags(journa 514 515 /* 516 * Switch to a new revoke table. 517 */ 518 jbd2_journal_switch_revoke_table(journ 519 520 write_lock(&journal->j_state_lock); 521 /* 522 * Reserved credits cannot be claimed 523 */ 524 atomic_sub(atomic_read(&journal->j_res 525 &commit_transaction->t_outs 526 527 trace_jbd2_commit_flushing(journal, co 528 stats.run.rs_flushing = jiffies; 529 stats.run.rs_locked = jbd2_time_diff(s 530 s 531 532 commit_transaction->t_state = T_FLUSH; 533 journal->j_committing_transaction = co 534 journal->j_running_transaction = NULL; 535 start_time = ktime_get(); 536 commit_transaction->t_log_start = jour 537 wake_up_all(&journal->j_wait_transacti 538 write_unlock(&journal->j_state_lock); 539 540 jbd2_debug(3, "JBD2: commit phase 2a\n 541 542 /* 543 * Now start flushing things to disk, 544 * on the transaction lists. Data blo 545 */ 546 err = journal_submit_data_buffers(jour 547 if (err) 548 jbd2_journal_abort(journal, er 549 550 blk_start_plug(&plug); 551 jbd2_journal_write_revoke_records(comm 552 553 jbd2_debug(3, "JBD2: commit phase 2b\n 554 555 /* 556 * Way to go: we have now written out 557 * transaction! Now comes the tricky 558 * metadata. Loop over the transactio 559 */ 560 write_lock(&journal->j_state_lock); 561 commit_transaction->t_state = T_COMMIT 562 write_unlock(&journal->j_state_lock); 563 564 trace_jbd2_commit_logging(journal, com 565 stats.run.rs_logging = jiffies; 566 stats.run.rs_flushing = jbd2_time_diff 567 568 stats.run.rs_blocks = commit_transacti 569 stats.run.rs_blocks_logged = 0; 570 571 J_ASSERT(commit_transaction->t_nr_buff 572 atomic_read(&commit_transacti 573 574 bufs = 0; 575 descriptor = NULL; 576 while (commit_transaction->t_buffers) 577 578 /* Find the next buffer to be 579 580 jh = commit_transaction->t_buf 581 582 /* If we're in abort mode, we 583 release it. */ 584 585 if (is_journal_aborted(journal 586 clear_buffer_jbddirty( 587 JBUFFER_TRACE(jh, "jou 588 jbd2_buffer_abort_trig 589 590 591 592 jbd2_journal_refile_bu 593 /* If that was the las 594 * any descriptor buff 595 * already allocated, 596 * aborting. */ 597 if (!commit_transactio 598 goto start_jou 599 continue; 600 } 601 602 /* Make sure we have a descrip 603 record the metadata buffer. 604 605 if (!descriptor) { 606 J_ASSERT (bufs == 0); 607 608 jbd2_debug(4, "JBD2: g 609 610 descriptor = jbd2_jour 611 612 613 if (!descriptor) { 614 jbd2_journal_a 615 continue; 616 } 617 618 jbd2_debug(4, "JBD2: g 619 (unsigned long 620 descriptor->b_ 621 tagp = &descriptor->b_ 622 space_left = descripto 623 624 first_tag = 1; 625 set_buffer_jwrite(desc 626 set_buffer_dirty(descr 627 wbuf[bufs++] = descrip 628 629 /* Record it so that w 630 completion later */ 631 BUFFER_TRACE(descripto 632 jbd2_file_log_bh(&log_ 633 } 634 635 /* Where is the buffer to be w 636 637 err = jbd2_journal_next_log_bl 638 /* If the block mapping failed 639 and repeat this loop: we'll 640 refile-on-abort condition a 641 if (err) { 642 jbd2_journal_abort(jou 643 continue; 644 } 645 646 /* 647 * start_this_handle() uses t_ 648 * the free space in the log. 649 */ 650 atomic_dec(&commit_transaction 651 652 /* Bump b_count to prevent tru 653 the shadowed buffer! @@@ T 654 rid of the shadow pairing o 655 atomic_inc(&jh2bh(jh)->b_count 656 657 /* 658 * Make a temporary IO buffer 659 * (this will requeue the meta 660 */ 661 set_bit(BH_JWrite, &jh2bh(jh)- 662 JBUFFER_TRACE(jh, "ph3: write 663 escape = jbd2_journal_write_me 664 665 if (escape < 0) { 666 jbd2_journal_abort(jou 667 continue; 668 } 669 jbd2_file_log_bh(&io_bufs, wbu 670 671 /* Record the new block's tag 672 buffer */ 673 674 tag_flag = 0; 675 if (escape) 676 tag_flag |= JBD2_FLAG_ 677 if (!first_tag) 678 tag_flag |= JBD2_FLAG_ 679 680 tag = (journal_block_tag_t *) 681 write_tag_block(journal, tag, 682 tag->t_flags = cpu_to_be16(tag 683 jbd2_block_tag_csum_set(journa 684 commit 685 tagp += tag_bytes; 686 space_left -= tag_bytes; 687 bufs++; 688 689 if (first_tag) { 690 memcpy (tagp, journal- 691 tagp += 16; 692 space_left -= 16; 693 first_tag = 0; 694 } 695 696 /* If there's no more to do, o 697 let the IO rip! */ 698 699 if (bufs == journal->j_wbufsiz 700 commit_transaction->t_buff 701 space_left < tag_bytes + 1 702 703 jbd2_debug(4, "JBD2: S 704 705 /* Write an end-of-des 706 submitting the IOs. 707 the last tag we set 708 709 tag->t_flags |= cpu_to 710 start_journal_io: 711 if (descriptor) 712 jbd2_descripto 713 714 715 for (i = 0; i < bufs; 716 struct buffer_ 717 718 /* 719 * Compute che 720 */ 721 if (jbd2_has_f 722 crc32_ 723 jb 724 } 725 726 lock_buffer(bh 727 clear_buffer_d 728 set_buffer_upt 729 bh->b_end_io = 730 submit_bh(REQ_ 731 bh); 732 } 733 cond_resched(); 734 735 /* Force a new descrip 736 time round the loop 737 descriptor = NULL; 738 bufs = 0; 739 } 740 } 741 742 err = journal_finish_inode_data_buffer 743 if (err) { 744 printk(KERN_WARNING 745 "JBD2: Detected IO err 746 "on %s\n", journal->j_d 747 if (journal->j_flags & JBD2_AB 748 jbd2_journal_abort(jou 749 err = 0; 750 } 751 752 /* 753 * Get current oldest transaction in t 754 * to the filesystem device. After the 755 * blocks of all older transactions ar 756 * storage and we will be safe to upda 757 * superblock with the numbers we get 758 */ 759 update_tail = 760 jbd2_journal_get_log_tail(jour 761 762 write_lock(&journal->j_state_lock); 763 if (update_tail) { 764 long freed = first_block - jou 765 766 if (first_block < journal->j_t 767 freed += journal->j_la 768 /* Update tail only if we free 769 if (freed < journal->j_max_tra 770 update_tail = 0; 771 } 772 J_ASSERT(commit_transaction->t_state = 773 commit_transaction->t_state = T_COMMIT 774 write_unlock(&journal->j_state_lock); 775 776 /* 777 * If the journal is not located on th 778 * then we must flush the file system 779 * the commit record 780 */ 781 if (commit_transaction->t_need_data_fl 782 (journal->j_fs_dev != journal->j_d 783 (journal->j_flags & JBD2_BARRIER)) 784 blkdev_issue_flush(journal->j_ 785 786 /* Done it all: now write the commit r 787 if (jbd2_has_feature_async_commit(jour 788 err = journal_submit_commit_re 789 790 if (err) 791 jbd2_journal_abort(jou 792 } 793 794 blk_finish_plug(&plug); 795 796 /* Lo and behold: we have just managed 797 the log. Before we can commit it, 798 complete. Control buffers being wr 799 transaction's t_log_list queue, and 800 the io_bufs list. 801 802 Wait for the buffers in reverse ord 803 less likely to be woken up until al 804 so we incur less scheduling load. 805 */ 806 807 jbd2_debug(3, "JBD2: commit phase 3\n" 808 809 while (!list_empty(&io_bufs)) { 810 struct buffer_head *bh = list_ 811 812 813 814 wait_on_buffer(bh); 815 cond_resched(); 816 817 if (unlikely(!buffer_uptodate( 818 err = -EIO; 819 jbd2_unfile_log_bh(bh); 820 stats.run.rs_blocks_logged++; 821 822 /* 823 * The list contains temporary 824 * jbd2_journal_write_metadata 825 */ 826 BUFFER_TRACE(bh, "dumping temp 827 __brelse(bh); 828 J_ASSERT_BH(bh, atomic_read(&b 829 free_buffer_head(bh); 830 831 /* We also have to refile the 832 jh = commit_transaction->t_sha 833 bh = jh2bh(jh); 834 clear_buffer_jwrite(bh); 835 J_ASSERT_BH(bh, buffer_jbddirt 836 J_ASSERT_BH(bh, !buffer_shadow 837 838 /* The metadata is now release 839 to remember it against this 840 we finally commit, we can d 841 required. */ 842 JBUFFER_TRACE(jh, "file as BJ_ 843 jbd2_journal_file_buffer(jh, c 844 JBUFFER_TRACE(jh, "brelse shad 845 __brelse(bh); 846 } 847 848 J_ASSERT (commit_transaction->t_shadow 849 850 jbd2_debug(3, "JBD2: commit phase 4\n" 851 852 /* Here we wait for the revoke record 853 while (!list_empty(&log_bufs)) { 854 struct buffer_head *bh; 855 856 bh = list_entry(log_bufs.prev, 857 wait_on_buffer(bh); 858 cond_resched(); 859 860 if (unlikely(!buffer_uptodate( 861 err = -EIO; 862 863 BUFFER_TRACE(bh, "ph5: control 864 clear_buffer_jwrite(bh); 865 jbd2_unfile_log_bh(bh); 866 stats.run.rs_blocks_logged++; 867 __brelse(bh); /* One 868 /* AKPM: bforget here */ 869 } 870 871 if (err) 872 jbd2_journal_abort(journal, er 873 874 jbd2_debug(3, "JBD2: commit phase 5\n" 875 write_lock(&journal->j_state_lock); 876 J_ASSERT(commit_transaction->t_state = 877 commit_transaction->t_state = T_COMMIT 878 write_unlock(&journal->j_state_lock); 879 880 if (!jbd2_has_feature_async_commit(jou 881 err = journal_submit_commit_re 882 883 if (err) 884 jbd2_journal_abort(jou 885 } 886 if (cbh) 887 err = journal_wait_on_commit_r 888 stats.run.rs_blocks_logged++; 889 if (jbd2_has_feature_async_commit(jour 890 journal->j_flags & JBD2_BARRIER) { 891 blkdev_issue_flush(journal->j_ 892 } 893 894 if (err) 895 jbd2_journal_abort(journal, er 896 897 WARN_ON_ONCE( 898 atomic_read(&commit_transactio 899 900 /* 901 * Now disk caches for filesystem devi 902 * erase checkpointed transactions fro 903 * superblock. 904 */ 905 if (update_tail) 906 jbd2_update_log_tail(journal, 907 908 /* End of a transaction! Finally, we 909 processing: any buffers committed a 910 transaction can be removed from any 911 before. */ 912 913 jbd2_debug(3, "JBD2: commit phase 6\n" 914 915 J_ASSERT(list_empty(&commit_transactio 916 J_ASSERT(commit_transaction->t_buffers 917 J_ASSERT(commit_transaction->t_checkpo 918 J_ASSERT(commit_transaction->t_shadow_ 919 920 restart_loop: 921 /* 922 * As there are other places (journal_ 923 * to this list we have to be careful 924 */ 925 spin_lock(&journal->j_list_lock); 926 while (commit_transaction->t_forget) { 927 transaction_t *cp_transaction; 928 struct buffer_head *bh; 929 int try_to_free = 0; 930 bool drop_ref; 931 932 jh = commit_transaction->t_for 933 spin_unlock(&journal->j_list_l 934 bh = jh2bh(jh); 935 /* 936 * Get a reference so that bh 937 * done with it. 938 */ 939 get_bh(bh); 940 spin_lock(&jh->b_state_lock); 941 J_ASSERT_JH(jh, jh->b_transact 942 943 /* 944 * If there is undo-protected 945 * this buffer, then we can re 946 * buffer needing such protect 947 * field now points to a commi 948 * buffer, so rotate that fiel 949 * data. 950 * 951 * Otherwise, we can just thro 952 * 953 * We also know that the froze 954 * its triggers if they exist, 955 */ 956 if (jh->b_committed_data) { 957 jbd2_free(jh->b_commit 958 jh->b_committed_data = 959 if (jh->b_frozen_data) 960 jh->b_committe 961 jh->b_frozen_d 962 jh->b_frozen_t 963 } 964 } else if (jh->b_frozen_data) 965 jbd2_free(jh->b_frozen 966 jh->b_frozen_data = NU 967 jh->b_frozen_triggers 968 } 969 970 spin_lock(&journal->j_list_loc 971 cp_transaction = jh->b_cp_tran 972 if (cp_transaction) { 973 JBUFFER_TRACE(jh, "rem 974 cp_transaction->t_chp_ 975 __jbd2_journal_remove_ 976 } 977 978 /* Only re-checkpoint the buff 979 * dirty. If the buffer was a 980 * by jbd2_journal_forget, it 981 * there's no point in keeping 982 * it. */ 983 984 /* 985 * A buffer which has been fre 986 * by a previous transaction, 987 * the running transaction. If 988 * contains "add to orphan" op 989 * invalidate the buffer now. 990 * since the buffer may be sti 991 * pagesize and it is attached 992 */ 993 if (buffer_freed(bh) && !jh->b 994 struct address_space * 995 996 clear_buffer_freed(bh) 997 clear_buffer_jbddirty( 998 999 /* 1000 * Block device buffe 1001 * time, so it is eno 1002 * buffer_freed bits. 1003 * journalled data) w 1004 * more bits. We also 1005 * because the data p 1006 * our hands. Note th 1007 * need to make buffe 1008 * already detached f 1009 * get reused. 1010 */ 1011 mapping = READ_ONCE(b 1012 if (mapping && !sb_is 1013 clear_buffer_ 1014 clear_buffer_ 1015 clear_buffer_ 1016 bh->b_bdev = 1017 } 1018 } 1019 1020 if (buffer_jbddirty(bh)) { 1021 JBUFFER_TRACE(jh, "ad 1022 __jbd2_journal_insert 1023 if (is_journal_aborte 1024 clear_buffer_ 1025 } else { 1026 J_ASSERT_BH(bh, !buff 1027 /* 1028 * The buffer on BJ_F 1029 * it has been freed 1030 * could not have bee 1031 * transaction has co 1032 * reallocated once w 1033 * disk and before we 1034 * list. 1035 */ 1036 if (!jh->b_next_trans 1037 try_to_free = 1038 } 1039 JBUFFER_TRACE(jh, "refile or 1040 drop_ref = __jbd2_journal_ref 1041 spin_unlock(&jh->b_state_lock 1042 if (drop_ref) 1043 jbd2_journal_put_jour 1044 if (try_to_free) 1045 release_buffer_page(b 1046 else 1047 __brelse(bh); 1048 cond_resched_lock(&journal->j 1049 } 1050 spin_unlock(&journal->j_list_lock); 1051 /* 1052 * This is a bit sleazy. We use j_li 1053 * of a transaction into T_FINISHED s 1054 * __jbd2_journal_drop_transaction(). 1055 * other checkpointing code processin 1056 */ 1057 write_lock(&journal->j_state_lock); 1058 spin_lock(&journal->j_list_lock); 1059 /* 1060 * Now recheck if some buffers did no 1061 * while the lock was dropped... 1062 */ 1063 if (commit_transaction->t_forget) { 1064 spin_unlock(&journal->j_list_ 1065 write_unlock(&journal->j_stat 1066 goto restart_loop; 1067 } 1068 1069 /* Add the transaction to the checkpo 1070 * __journal_remove_checkpoint() can 1071 * under us because it is not marked 1072 if (journal->j_checkpoint_transaction 1073 journal->j_checkpoint_transac 1074 commit_transaction->t_cpnext 1075 commit_transaction->t_cpprev 1076 } else { 1077 commit_transaction->t_cpnext 1078 journal->j_checkpoint 1079 commit_transaction->t_cpprev 1080 commit_transaction->t 1081 commit_transaction->t_cpnext- 1082 commit_transaction; 1083 commit_transaction->t_cpprev- 1084 commit_transa 1085 } 1086 spin_unlock(&journal->j_list_lock); 1087 1088 /* Done with this transaction! */ 1089 1090 jbd2_debug(3, "JBD2: commit phase 7\n 1091 1092 J_ASSERT(commit_transaction->t_state 1093 1094 commit_transaction->t_start = jiffies 1095 stats.run.rs_logging = jbd2_time_diff 1096 1097 1098 /* 1099 * File the transaction statistics 1100 */ 1101 stats.ts_tid = commit_transaction->t_ 1102 stats.run.rs_handle_count = 1103 atomic_read(&commit_transacti 1104 trace_jbd2_run_stats(journal->j_fs_de 1105 commit_transacti 1106 stats.ts_requested = (commit_transact 1107 1108 commit_transaction->t_state = T_COMMI 1109 J_ASSERT(commit_transaction == journa 1110 WRITE_ONCE(journal->j_commit_sequence 1111 journal->j_committing_transaction = N 1112 commit_time = ktime_to_ns(ktime_sub(k 1113 1114 /* 1115 * weight the commit time higher than 1116 * react too strongly to vast changes 1117 */ 1118 if (likely(journal->j_average_commit_ 1119 journal->j_average_commit_tim 1120 journal->j_av 1121 else 1122 journal->j_average_commit_tim 1123 1124 write_unlock(&journal->j_state_lock); 1125 1126 if (journal->j_commit_callback) 1127 journal->j_commit_callback(jo 1128 if (journal->j_fc_cleanup_callback) 1129 journal->j_fc_cleanup_callbac 1130 1131 trace_jbd2_end_commit(journal, commit 1132 jbd2_debug(1, "JBD2: commit %d comple 1133 journal->j_commit_sequence, 1134 1135 write_lock(&journal->j_state_lock); 1136 journal->j_flags &= ~JBD2_FULL_COMMIT 1137 journal->j_flags &= ~JBD2_FAST_COMMIT 1138 spin_lock(&journal->j_list_lock); 1139 commit_transaction->t_state = T_FINIS 1140 /* Check if the transaction can be dr 1141 if (commit_transaction->t_checkpoint_ 1142 __jbd2_journal_drop_transacti 1143 jbd2_journal_free_transaction 1144 } 1145 spin_unlock(&journal->j_list_lock); 1146 write_unlock(&journal->j_state_lock); 1147 wake_up(&journal->j_wait_done_commit) 1148 wake_up(&journal->j_fc_wait); 1149 1150 /* 1151 * Calculate overall stats 1152 */ 1153 spin_lock(&journal->j_history_lock); 1154 journal->j_stats.ts_tid++; 1155 journal->j_stats.ts_requested += stat 1156 journal->j_stats.run.rs_wait += stats 1157 journal->j_stats.run.rs_request_delay 1158 journal->j_stats.run.rs_running += st 1159 journal->j_stats.run.rs_locked += sta 1160 journal->j_stats.run.rs_flushing += s 1161 journal->j_stats.run.rs_logging += st 1162 journal->j_stats.run.rs_handle_count 1163 journal->j_stats.run.rs_blocks += sta 1164 journal->j_stats.run.rs_blocks_logged 1165 spin_unlock(&journal->j_history_lock) 1166 } 1167
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.