0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/time.h>
0014 #include <linux/fs.h>
0015 #include <linux/jbd2.h>
0016 #include <linux/errno.h>
0017 #include <linux/slab.h>
0018 #include <linux/mm.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/jiffies.h>
0021 #include <linux/crc32.h>
0022 #include <linux/writeback.h>
0023 #include <linux/backing-dev.h>
0024 #include <linux/bio.h>
0025 #include <linux/blkdev.h>
0026 #include <linux/bitops.h>
0027 #include <trace/events/jbd2.h>
0028
0029
0030
0031
0032 static void journal_end_buffer_io_sync(struct buffer_head *bh, int uptodate)
0033 {
0034 struct buffer_head *orig_bh = bh->b_private;
0035
0036 BUFFER_TRACE(bh, "");
0037 if (uptodate)
0038 set_buffer_uptodate(bh);
0039 else
0040 clear_buffer_uptodate(bh);
0041 if (orig_bh) {
0042 clear_bit_unlock(BH_Shadow, &orig_bh->b_state);
0043 smp_mb__after_atomic();
0044 wake_up_bit(&orig_bh->b_state, BH_Shadow);
0045 }
0046 unlock_buffer(bh);
0047 }
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 static void release_buffer_page(struct buffer_head *bh)
0064 {
0065 struct folio *folio;
0066 struct page *page;
0067
0068 if (buffer_dirty(bh))
0069 goto nope;
0070 if (atomic_read(&bh->b_count) != 1)
0071 goto nope;
0072 page = bh->b_page;
0073 if (!page)
0074 goto nope;
0075 folio = page_folio(page);
0076 if (folio->mapping)
0077 goto nope;
0078
0079
0080 if (!folio_trylock(folio))
0081 goto nope;
0082
0083 folio_get(folio);
0084 __brelse(bh);
0085 try_to_free_buffers(folio);
0086 folio_unlock(folio);
0087 folio_put(folio);
0088 return;
0089
0090 nope:
0091 __brelse(bh);
0092 }
0093
0094 static void jbd2_commit_block_csum_set(journal_t *j, struct buffer_head *bh)
0095 {
0096 struct commit_header *h;
0097 __u32 csum;
0098
0099 if (!jbd2_journal_has_csum_v2or3(j))
0100 return;
0101
0102 h = (struct commit_header *)(bh->b_data);
0103 h->h_chksum_type = 0;
0104 h->h_chksum_size = 0;
0105 h->h_chksum[0] = 0;
0106 csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
0107 h->h_chksum[0] = cpu_to_be32(csum);
0108 }
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 static int journal_submit_commit_record(journal_t *journal,
0119 transaction_t *commit_transaction,
0120 struct buffer_head **cbh,
0121 __u32 crc32_sum)
0122 {
0123 struct commit_header *tmp;
0124 struct buffer_head *bh;
0125 int ret;
0126 struct timespec64 now;
0127
0128 *cbh = NULL;
0129
0130 if (is_journal_aborted(journal))
0131 return 0;
0132
0133 bh = jbd2_journal_get_descriptor_buffer(commit_transaction,
0134 JBD2_COMMIT_BLOCK);
0135 if (!bh)
0136 return 1;
0137
0138 tmp = (struct commit_header *)bh->b_data;
0139 ktime_get_coarse_real_ts64(&now);
0140 tmp->h_commit_sec = cpu_to_be64(now.tv_sec);
0141 tmp->h_commit_nsec = cpu_to_be32(now.tv_nsec);
0142
0143 if (jbd2_has_feature_checksum(journal)) {
0144 tmp->h_chksum_type = JBD2_CRC32_CHKSUM;
0145 tmp->h_chksum_size = JBD2_CRC32_CHKSUM_SIZE;
0146 tmp->h_chksum[0] = cpu_to_be32(crc32_sum);
0147 }
0148 jbd2_commit_block_csum_set(journal, bh);
0149
0150 BUFFER_TRACE(bh, "submit commit block");
0151 lock_buffer(bh);
0152 clear_buffer_dirty(bh);
0153 set_buffer_uptodate(bh);
0154 bh->b_end_io = journal_end_buffer_io_sync;
0155
0156 if (journal->j_flags & JBD2_BARRIER &&
0157 !jbd2_has_feature_async_commit(journal))
0158 ret = submit_bh(REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH |
0159 REQ_FUA, bh);
0160 else
0161 ret = submit_bh(REQ_OP_WRITE | REQ_SYNC, bh);
0162
0163 *cbh = bh;
0164 return ret;
0165 }
0166
0167
0168
0169
0170
0171 static int journal_wait_on_commit_record(journal_t *journal,
0172 struct buffer_head *bh)
0173 {
0174 int ret = 0;
0175
0176 clear_buffer_dirty(bh);
0177 wait_on_buffer(bh);
0178
0179 if (unlikely(!buffer_uptodate(bh)))
0180 ret = -EIO;
0181 put_bh(bh);
0182
0183 return ret;
0184 }
0185
0186
0187
0188
0189
0190
0191
0192 int jbd2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
0193 {
0194 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
0195 struct writeback_control wbc = {
0196 .sync_mode = WB_SYNC_ALL,
0197 .nr_to_write = mapping->nrpages * 2,
0198 .range_start = jinode->i_dirty_start,
0199 .range_end = jinode->i_dirty_end,
0200 };
0201
0202
0203
0204
0205
0206
0207
0208 return generic_writepages(mapping, &wbc);
0209 }
0210
0211
0212 int jbd2_submit_inode_data(struct jbd2_inode *jinode)
0213 {
0214
0215 if (!jinode || !(jinode->i_flags & JI_WRITE_DATA))
0216 return 0;
0217
0218 trace_jbd2_submit_inode_data(jinode->i_vfs_inode);
0219 return jbd2_journal_submit_inode_data_buffers(jinode);
0220
0221 }
0222 EXPORT_SYMBOL(jbd2_submit_inode_data);
0223
0224 int jbd2_wait_inode_data(journal_t *journal, struct jbd2_inode *jinode)
0225 {
0226 if (!jinode || !(jinode->i_flags & JI_WAIT_DATA) ||
0227 !jinode->i_vfs_inode || !jinode->i_vfs_inode->i_mapping)
0228 return 0;
0229 return filemap_fdatawait_range_keep_errors(
0230 jinode->i_vfs_inode->i_mapping, jinode->i_dirty_start,
0231 jinode->i_dirty_end);
0232 }
0233 EXPORT_SYMBOL(jbd2_wait_inode_data);
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 static int journal_submit_data_buffers(journal_t *journal,
0244 transaction_t *commit_transaction)
0245 {
0246 struct jbd2_inode *jinode;
0247 int err, ret = 0;
0248
0249 spin_lock(&journal->j_list_lock);
0250 list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
0251 if (!(jinode->i_flags & JI_WRITE_DATA))
0252 continue;
0253 jinode->i_flags |= JI_COMMIT_RUNNING;
0254 spin_unlock(&journal->j_list_lock);
0255
0256 trace_jbd2_submit_inode_data(jinode->i_vfs_inode);
0257 if (journal->j_submit_inode_data_buffers) {
0258 err = journal->j_submit_inode_data_buffers(jinode);
0259 if (!ret)
0260 ret = err;
0261 }
0262 spin_lock(&journal->j_list_lock);
0263 J_ASSERT(jinode->i_transaction == commit_transaction);
0264 jinode->i_flags &= ~JI_COMMIT_RUNNING;
0265 smp_mb();
0266 wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
0267 }
0268 spin_unlock(&journal->j_list_lock);
0269 return ret;
0270 }
0271
0272 int jbd2_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
0273 {
0274 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
0275
0276 return filemap_fdatawait_range_keep_errors(mapping,
0277 jinode->i_dirty_start,
0278 jinode->i_dirty_end);
0279 }
0280
0281
0282
0283
0284
0285
0286 static int journal_finish_inode_data_buffers(journal_t *journal,
0287 transaction_t *commit_transaction)
0288 {
0289 struct jbd2_inode *jinode, *next_i;
0290 int err, ret = 0;
0291
0292
0293 spin_lock(&journal->j_list_lock);
0294 list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
0295 if (!(jinode->i_flags & JI_WAIT_DATA))
0296 continue;
0297 jinode->i_flags |= JI_COMMIT_RUNNING;
0298 spin_unlock(&journal->j_list_lock);
0299
0300 if (journal->j_finish_inode_data_buffers) {
0301 err = journal->j_finish_inode_data_buffers(jinode);
0302 if (!ret)
0303 ret = err;
0304 }
0305 spin_lock(&journal->j_list_lock);
0306 jinode->i_flags &= ~JI_COMMIT_RUNNING;
0307 smp_mb();
0308 wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
0309 }
0310
0311
0312 list_for_each_entry_safe(jinode, next_i,
0313 &commit_transaction->t_inode_list, i_list) {
0314 list_del(&jinode->i_list);
0315 if (jinode->i_next_transaction) {
0316 jinode->i_transaction = jinode->i_next_transaction;
0317 jinode->i_next_transaction = NULL;
0318 list_add(&jinode->i_list,
0319 &jinode->i_transaction->t_inode_list);
0320 } else {
0321 jinode->i_transaction = NULL;
0322 jinode->i_dirty_start = 0;
0323 jinode->i_dirty_end = 0;
0324 }
0325 }
0326 spin_unlock(&journal->j_list_lock);
0327
0328 return ret;
0329 }
0330
0331 static __u32 jbd2_checksum_data(__u32 crc32_sum, struct buffer_head *bh)
0332 {
0333 struct page *page = bh->b_page;
0334 char *addr;
0335 __u32 checksum;
0336
0337 addr = kmap_atomic(page);
0338 checksum = crc32_be(crc32_sum,
0339 (void *)(addr + offset_in_page(bh->b_data)), bh->b_size);
0340 kunmap_atomic(addr);
0341
0342 return checksum;
0343 }
0344
0345 static void write_tag_block(journal_t *j, journal_block_tag_t *tag,
0346 unsigned long long block)
0347 {
0348 tag->t_blocknr = cpu_to_be32(block & (u32)~0);
0349 if (jbd2_has_feature_64bit(j))
0350 tag->t_blocknr_high = cpu_to_be32((block >> 31) >> 1);
0351 }
0352
0353 static void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
0354 struct buffer_head *bh, __u32 sequence)
0355 {
0356 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
0357 struct page *page = bh->b_page;
0358 __u8 *addr;
0359 __u32 csum32;
0360 __be32 seq;
0361
0362 if (!jbd2_journal_has_csum_v2or3(j))
0363 return;
0364
0365 seq = cpu_to_be32(sequence);
0366 addr = kmap_atomic(page);
0367 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
0368 csum32 = jbd2_chksum(j, csum32, addr + offset_in_page(bh->b_data),
0369 bh->b_size);
0370 kunmap_atomic(addr);
0371
0372 if (jbd2_has_feature_csum3(j))
0373 tag3->t_checksum = cpu_to_be32(csum32);
0374 else
0375 tag->t_checksum = cpu_to_be16(csum32);
0376 }
0377
0378
0379
0380
0381
0382
0383 void jbd2_journal_commit_transaction(journal_t *journal)
0384 {
0385 struct transaction_stats_s stats;
0386 transaction_t *commit_transaction;
0387 struct journal_head *jh;
0388 struct buffer_head *descriptor;
0389 struct buffer_head **wbuf = journal->j_wbuf;
0390 int bufs;
0391 int flags;
0392 int err;
0393 unsigned long long blocknr;
0394 ktime_t start_time;
0395 u64 commit_time;
0396 char *tagp = NULL;
0397 journal_block_tag_t *tag = NULL;
0398 int space_left = 0;
0399 int first_tag = 0;
0400 int tag_flag;
0401 int i;
0402 int tag_bytes = journal_tag_bytes(journal);
0403 struct buffer_head *cbh = NULL;
0404 __u32 crc32_sum = ~0;
0405 struct blk_plug plug;
0406
0407 unsigned long first_block;
0408 tid_t first_tid;
0409 int update_tail;
0410 int csum_size = 0;
0411 LIST_HEAD(io_bufs);
0412 LIST_HEAD(log_bufs);
0413
0414 if (jbd2_journal_has_csum_v2or3(journal))
0415 csum_size = sizeof(struct jbd2_journal_block_tail);
0416
0417
0418
0419
0420
0421
0422
0423 if (journal->j_flags & JBD2_FLUSHED) {
0424 jbd2_debug(3, "super block updated\n");
0425 mutex_lock_io(&journal->j_checkpoint_mutex);
0426
0427
0428
0429
0430
0431
0432 jbd2_journal_update_sb_log_tail(journal,
0433 journal->j_tail_sequence,
0434 journal->j_tail,
0435 REQ_SYNC);
0436 mutex_unlock(&journal->j_checkpoint_mutex);
0437 } else {
0438 jbd2_debug(3, "superblock not updated\n");
0439 }
0440
0441 J_ASSERT(journal->j_running_transaction != NULL);
0442 J_ASSERT(journal->j_committing_transaction == NULL);
0443
0444 write_lock(&journal->j_state_lock);
0445 journal->j_flags |= JBD2_FULL_COMMIT_ONGOING;
0446 while (journal->j_flags & JBD2_FAST_COMMIT_ONGOING) {
0447 DEFINE_WAIT(wait);
0448
0449 prepare_to_wait(&journal->j_fc_wait, &wait,
0450 TASK_UNINTERRUPTIBLE);
0451 write_unlock(&journal->j_state_lock);
0452 schedule();
0453 write_lock(&journal->j_state_lock);
0454 finish_wait(&journal->j_fc_wait, &wait);
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 }
0465 write_unlock(&journal->j_state_lock);
0466
0467 commit_transaction = journal->j_running_transaction;
0468
0469 trace_jbd2_start_commit(journal, commit_transaction);
0470 jbd2_debug(1, "JBD2: starting commit of transaction %d\n",
0471 commit_transaction->t_tid);
0472
0473 write_lock(&journal->j_state_lock);
0474 journal->j_fc_off = 0;
0475 J_ASSERT(commit_transaction->t_state == T_RUNNING);
0476 commit_transaction->t_state = T_LOCKED;
0477
0478 trace_jbd2_commit_locking(journal, commit_transaction);
0479 stats.run.rs_wait = commit_transaction->t_max_wait;
0480 stats.run.rs_request_delay = 0;
0481 stats.run.rs_locked = jiffies;
0482 if (commit_transaction->t_requested)
0483 stats.run.rs_request_delay =
0484 jbd2_time_diff(commit_transaction->t_requested,
0485 stats.run.rs_locked);
0486 stats.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
0487 stats.run.rs_locked);
0488
0489
0490 jbd2_journal_wait_updates(journal);
0491
0492 commit_transaction->t_state = T_SWITCH;
0493
0494 J_ASSERT (atomic_read(&commit_transaction->t_outstanding_credits) <=
0495 journal->j_max_transaction_buffers);
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515 while (commit_transaction->t_reserved_list) {
0516 jh = commit_transaction->t_reserved_list;
0517 JBUFFER_TRACE(jh, "reserved, unused: refile");
0518
0519
0520
0521
0522 if (jh->b_committed_data) {
0523 struct buffer_head *bh = jh2bh(jh);
0524
0525 spin_lock(&jh->b_state_lock);
0526 jbd2_free(jh->b_committed_data, bh->b_size);
0527 jh->b_committed_data = NULL;
0528 spin_unlock(&jh->b_state_lock);
0529 }
0530 jbd2_journal_refile_buffer(journal, jh);
0531 }
0532
0533 write_unlock(&journal->j_state_lock);
0534
0535
0536
0537
0538
0539 spin_lock(&journal->j_list_lock);
0540 __jbd2_journal_clean_checkpoint_list(journal, false);
0541 spin_unlock(&journal->j_list_lock);
0542
0543 jbd2_debug(3, "JBD2: commit phase 1\n");
0544
0545
0546
0547
0548
0549 jbd2_clear_buffer_revoked_flags(journal);
0550
0551
0552
0553
0554 jbd2_journal_switch_revoke_table(journal);
0555
0556 write_lock(&journal->j_state_lock);
0557
0558
0559
0560 atomic_sub(atomic_read(&journal->j_reserved_credits),
0561 &commit_transaction->t_outstanding_credits);
0562
0563 trace_jbd2_commit_flushing(journal, commit_transaction);
0564 stats.run.rs_flushing = jiffies;
0565 stats.run.rs_locked = jbd2_time_diff(stats.run.rs_locked,
0566 stats.run.rs_flushing);
0567
0568 commit_transaction->t_state = T_FLUSH;
0569 journal->j_committing_transaction = commit_transaction;
0570 journal->j_running_transaction = NULL;
0571 start_time = ktime_get();
0572 commit_transaction->t_log_start = journal->j_head;
0573 wake_up(&journal->j_wait_transaction_locked);
0574 write_unlock(&journal->j_state_lock);
0575
0576 jbd2_debug(3, "JBD2: commit phase 2a\n");
0577
0578
0579
0580
0581
0582 err = journal_submit_data_buffers(journal, commit_transaction);
0583 if (err)
0584 jbd2_journal_abort(journal, err);
0585
0586 blk_start_plug(&plug);
0587 jbd2_journal_write_revoke_records(commit_transaction, &log_bufs);
0588
0589 jbd2_debug(3, "JBD2: commit phase 2b\n");
0590
0591
0592
0593
0594
0595
0596 write_lock(&journal->j_state_lock);
0597 commit_transaction->t_state = T_COMMIT;
0598 write_unlock(&journal->j_state_lock);
0599
0600 trace_jbd2_commit_logging(journal, commit_transaction);
0601 stats.run.rs_logging = jiffies;
0602 stats.run.rs_flushing = jbd2_time_diff(stats.run.rs_flushing,
0603 stats.run.rs_logging);
0604 stats.run.rs_blocks = commit_transaction->t_nr_buffers;
0605 stats.run.rs_blocks_logged = 0;
0606
0607 J_ASSERT(commit_transaction->t_nr_buffers <=
0608 atomic_read(&commit_transaction->t_outstanding_credits));
0609
0610 err = 0;
0611 bufs = 0;
0612 descriptor = NULL;
0613 while (commit_transaction->t_buffers) {
0614
0615
0616
0617 jh = commit_transaction->t_buffers;
0618
0619
0620
0621
0622 if (is_journal_aborted(journal)) {
0623 clear_buffer_jbddirty(jh2bh(jh));
0624 JBUFFER_TRACE(jh, "journal is aborting: refile");
0625 jbd2_buffer_abort_trigger(jh,
0626 jh->b_frozen_data ?
0627 jh->b_frozen_triggers :
0628 jh->b_triggers);
0629 jbd2_journal_refile_buffer(journal, jh);
0630
0631
0632
0633
0634 if (!commit_transaction->t_buffers)
0635 goto start_journal_io;
0636 continue;
0637 }
0638
0639
0640
0641
0642 if (!descriptor) {
0643 J_ASSERT (bufs == 0);
0644
0645 jbd2_debug(4, "JBD2: get descriptor\n");
0646
0647 descriptor = jbd2_journal_get_descriptor_buffer(
0648 commit_transaction,
0649 JBD2_DESCRIPTOR_BLOCK);
0650 if (!descriptor) {
0651 jbd2_journal_abort(journal, -EIO);
0652 continue;
0653 }
0654
0655 jbd2_debug(4, "JBD2: got buffer %llu (%p)\n",
0656 (unsigned long long)descriptor->b_blocknr,
0657 descriptor->b_data);
0658 tagp = &descriptor->b_data[sizeof(journal_header_t)];
0659 space_left = descriptor->b_size -
0660 sizeof(journal_header_t);
0661 first_tag = 1;
0662 set_buffer_jwrite(descriptor);
0663 set_buffer_dirty(descriptor);
0664 wbuf[bufs++] = descriptor;
0665
0666
0667
0668 BUFFER_TRACE(descriptor, "ph3: file as descriptor");
0669 jbd2_file_log_bh(&log_bufs, descriptor);
0670 }
0671
0672
0673
0674 err = jbd2_journal_next_log_block(journal, &blocknr);
0675
0676
0677
0678 if (err) {
0679 jbd2_journal_abort(journal, err);
0680 continue;
0681 }
0682
0683
0684
0685
0686
0687 atomic_dec(&commit_transaction->t_outstanding_credits);
0688
0689
0690
0691
0692 atomic_inc(&jh2bh(jh)->b_count);
0693
0694
0695
0696
0697
0698 set_bit(BH_JWrite, &jh2bh(jh)->b_state);
0699 JBUFFER_TRACE(jh, "ph3: write metadata");
0700 flags = jbd2_journal_write_metadata_buffer(commit_transaction,
0701 jh, &wbuf[bufs], blocknr);
0702 if (flags < 0) {
0703 jbd2_journal_abort(journal, flags);
0704 continue;
0705 }
0706 jbd2_file_log_bh(&io_bufs, wbuf[bufs]);
0707
0708
0709
0710
0711 tag_flag = 0;
0712 if (flags & 1)
0713 tag_flag |= JBD2_FLAG_ESCAPE;
0714 if (!first_tag)
0715 tag_flag |= JBD2_FLAG_SAME_UUID;
0716
0717 tag = (journal_block_tag_t *) tagp;
0718 write_tag_block(journal, tag, jh2bh(jh)->b_blocknr);
0719 tag->t_flags = cpu_to_be16(tag_flag);
0720 jbd2_block_tag_csum_set(journal, tag, wbuf[bufs],
0721 commit_transaction->t_tid);
0722 tagp += tag_bytes;
0723 space_left -= tag_bytes;
0724 bufs++;
0725
0726 if (first_tag) {
0727 memcpy (tagp, journal->j_uuid, 16);
0728 tagp += 16;
0729 space_left -= 16;
0730 first_tag = 0;
0731 }
0732
0733
0734
0735
0736 if (bufs == journal->j_wbufsize ||
0737 commit_transaction->t_buffers == NULL ||
0738 space_left < tag_bytes + 16 + csum_size) {
0739
0740 jbd2_debug(4, "JBD2: Submit %d IOs\n", bufs);
0741
0742
0743
0744
0745
0746 tag->t_flags |= cpu_to_be16(JBD2_FLAG_LAST_TAG);
0747 start_journal_io:
0748 if (descriptor)
0749 jbd2_descriptor_block_csum_set(journal,
0750 descriptor);
0751
0752 for (i = 0; i < bufs; i++) {
0753 struct buffer_head *bh = wbuf[i];
0754
0755
0756
0757 if (jbd2_has_feature_checksum(journal)) {
0758 crc32_sum =
0759 jbd2_checksum_data(crc32_sum, bh);
0760 }
0761
0762 lock_buffer(bh);
0763 clear_buffer_dirty(bh);
0764 set_buffer_uptodate(bh);
0765 bh->b_end_io = journal_end_buffer_io_sync;
0766 submit_bh(REQ_OP_WRITE | REQ_SYNC, bh);
0767 }
0768 cond_resched();
0769
0770
0771
0772 descriptor = NULL;
0773 bufs = 0;
0774 }
0775 }
0776
0777 err = journal_finish_inode_data_buffers(journal, commit_transaction);
0778 if (err) {
0779 printk(KERN_WARNING
0780 "JBD2: Detected IO errors while flushing file data "
0781 "on %s\n", journal->j_devname);
0782 if (journal->j_flags & JBD2_ABORT_ON_SYNCDATA_ERR)
0783 jbd2_journal_abort(journal, err);
0784 err = 0;
0785 }
0786
0787
0788
0789
0790
0791
0792
0793
0794 update_tail =
0795 jbd2_journal_get_log_tail(journal, &first_tid, &first_block);
0796
0797 write_lock(&journal->j_state_lock);
0798 if (update_tail) {
0799 long freed = first_block - journal->j_tail;
0800
0801 if (first_block < journal->j_tail)
0802 freed += journal->j_last - journal->j_first;
0803
0804 if (freed < jbd2_journal_get_max_txn_bufs(journal))
0805 update_tail = 0;
0806 }
0807 J_ASSERT(commit_transaction->t_state == T_COMMIT);
0808 commit_transaction->t_state = T_COMMIT_DFLUSH;
0809 write_unlock(&journal->j_state_lock);
0810
0811
0812
0813
0814
0815
0816 if (commit_transaction->t_need_data_flush &&
0817 (journal->j_fs_dev != journal->j_dev) &&
0818 (journal->j_flags & JBD2_BARRIER))
0819 blkdev_issue_flush(journal->j_fs_dev);
0820
0821
0822 if (jbd2_has_feature_async_commit(journal)) {
0823 err = journal_submit_commit_record(journal, commit_transaction,
0824 &cbh, crc32_sum);
0825 if (err)
0826 jbd2_journal_abort(journal, err);
0827 }
0828
0829 blk_finish_plug(&plug);
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842 jbd2_debug(3, "JBD2: commit phase 3\n");
0843
0844 while (!list_empty(&io_bufs)) {
0845 struct buffer_head *bh = list_entry(io_bufs.prev,
0846 struct buffer_head,
0847 b_assoc_buffers);
0848
0849 wait_on_buffer(bh);
0850 cond_resched();
0851
0852 if (unlikely(!buffer_uptodate(bh)))
0853 err = -EIO;
0854 jbd2_unfile_log_bh(bh);
0855 stats.run.rs_blocks_logged++;
0856
0857
0858
0859
0860
0861 BUFFER_TRACE(bh, "dumping temporary bh");
0862 __brelse(bh);
0863 J_ASSERT_BH(bh, atomic_read(&bh->b_count) == 0);
0864 free_buffer_head(bh);
0865
0866
0867 jh = commit_transaction->t_shadow_list->b_tprev;
0868 bh = jh2bh(jh);
0869 clear_buffer_jwrite(bh);
0870 J_ASSERT_BH(bh, buffer_jbddirty(bh));
0871 J_ASSERT_BH(bh, !buffer_shadow(bh));
0872
0873
0874
0875
0876
0877 JBUFFER_TRACE(jh, "file as BJ_Forget");
0878 jbd2_journal_file_buffer(jh, commit_transaction, BJ_Forget);
0879 JBUFFER_TRACE(jh, "brelse shadowed buffer");
0880 __brelse(bh);
0881 }
0882
0883 J_ASSERT (commit_transaction->t_shadow_list == NULL);
0884
0885 jbd2_debug(3, "JBD2: commit phase 4\n");
0886
0887
0888 while (!list_empty(&log_bufs)) {
0889 struct buffer_head *bh;
0890
0891 bh = list_entry(log_bufs.prev, struct buffer_head, b_assoc_buffers);
0892 wait_on_buffer(bh);
0893 cond_resched();
0894
0895 if (unlikely(!buffer_uptodate(bh)))
0896 err = -EIO;
0897
0898 BUFFER_TRACE(bh, "ph5: control buffer writeout done: unfile");
0899 clear_buffer_jwrite(bh);
0900 jbd2_unfile_log_bh(bh);
0901 stats.run.rs_blocks_logged++;
0902 __brelse(bh);
0903
0904 }
0905
0906 if (err)
0907 jbd2_journal_abort(journal, err);
0908
0909 jbd2_debug(3, "JBD2: commit phase 5\n");
0910 write_lock(&journal->j_state_lock);
0911 J_ASSERT(commit_transaction->t_state == T_COMMIT_DFLUSH);
0912 commit_transaction->t_state = T_COMMIT_JFLUSH;
0913 write_unlock(&journal->j_state_lock);
0914
0915 if (!jbd2_has_feature_async_commit(journal)) {
0916 err = journal_submit_commit_record(journal, commit_transaction,
0917 &cbh, crc32_sum);
0918 if (err)
0919 jbd2_journal_abort(journal, err);
0920 }
0921 if (cbh)
0922 err = journal_wait_on_commit_record(journal, cbh);
0923 stats.run.rs_blocks_logged++;
0924 if (jbd2_has_feature_async_commit(journal) &&
0925 journal->j_flags & JBD2_BARRIER) {
0926 blkdev_issue_flush(journal->j_dev);
0927 }
0928
0929 if (err)
0930 jbd2_journal_abort(journal, err);
0931
0932 WARN_ON_ONCE(
0933 atomic_read(&commit_transaction->t_outstanding_credits) < 0);
0934
0935
0936
0937
0938
0939
0940 if (update_tail)
0941 jbd2_update_log_tail(journal, first_tid, first_block);
0942
0943
0944
0945
0946
0947
0948 jbd2_debug(3, "JBD2: commit phase 6\n");
0949
0950 J_ASSERT(list_empty(&commit_transaction->t_inode_list));
0951 J_ASSERT(commit_transaction->t_buffers == NULL);
0952 J_ASSERT(commit_transaction->t_checkpoint_list == NULL);
0953 J_ASSERT(commit_transaction->t_shadow_list == NULL);
0954
0955 restart_loop:
0956
0957
0958
0959
0960 spin_lock(&journal->j_list_lock);
0961 while (commit_transaction->t_forget) {
0962 transaction_t *cp_transaction;
0963 struct buffer_head *bh;
0964 int try_to_free = 0;
0965 bool drop_ref;
0966
0967 jh = commit_transaction->t_forget;
0968 spin_unlock(&journal->j_list_lock);
0969 bh = jh2bh(jh);
0970
0971
0972
0973
0974 get_bh(bh);
0975 spin_lock(&jh->b_state_lock);
0976 J_ASSERT_JH(jh, jh->b_transaction == commit_transaction);
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991 if (jh->b_committed_data) {
0992 jbd2_free(jh->b_committed_data, bh->b_size);
0993 jh->b_committed_data = NULL;
0994 if (jh->b_frozen_data) {
0995 jh->b_committed_data = jh->b_frozen_data;
0996 jh->b_frozen_data = NULL;
0997 jh->b_frozen_triggers = NULL;
0998 }
0999 } else if (jh->b_frozen_data) {
1000 jbd2_free(jh->b_frozen_data, bh->b_size);
1001 jh->b_frozen_data = NULL;
1002 jh->b_frozen_triggers = NULL;
1003 }
1004
1005 spin_lock(&journal->j_list_lock);
1006 cp_transaction = jh->b_cp_transaction;
1007 if (cp_transaction) {
1008 JBUFFER_TRACE(jh, "remove from old cp transaction");
1009 cp_transaction->t_chp_stats.cs_dropped++;
1010 __jbd2_journal_remove_checkpoint(jh);
1011 }
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 if (buffer_freed(bh) && !jh->b_next_transaction) {
1029 struct address_space *mapping;
1030
1031 clear_buffer_freed(bh);
1032 clear_buffer_jbddirty(bh);
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 mapping = READ_ONCE(bh->b_page->mapping);
1047 if (mapping && !sb_is_blkdev_sb(mapping->host->i_sb)) {
1048 clear_buffer_mapped(bh);
1049 clear_buffer_new(bh);
1050 clear_buffer_req(bh);
1051 bh->b_bdev = NULL;
1052 }
1053 }
1054
1055 if (buffer_jbddirty(bh)) {
1056 JBUFFER_TRACE(jh, "add to new checkpointing trans");
1057 __jbd2_journal_insert_checkpoint(jh, commit_transaction);
1058 if (is_journal_aborted(journal))
1059 clear_buffer_jbddirty(bh);
1060 } else {
1061 J_ASSERT_BH(bh, !buffer_dirty(bh));
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071 if (!jh->b_next_transaction)
1072 try_to_free = 1;
1073 }
1074 JBUFFER_TRACE(jh, "refile or unfile buffer");
1075 drop_ref = __jbd2_journal_refile_buffer(jh);
1076 spin_unlock(&jh->b_state_lock);
1077 if (drop_ref)
1078 jbd2_journal_put_journal_head(jh);
1079 if (try_to_free)
1080 release_buffer_page(bh);
1081 else
1082 __brelse(bh);
1083 cond_resched_lock(&journal->j_list_lock);
1084 }
1085 spin_unlock(&journal->j_list_lock);
1086
1087
1088
1089
1090
1091
1092 write_lock(&journal->j_state_lock);
1093 spin_lock(&journal->j_list_lock);
1094
1095
1096
1097
1098 if (commit_transaction->t_forget) {
1099 spin_unlock(&journal->j_list_lock);
1100 write_unlock(&journal->j_state_lock);
1101 goto restart_loop;
1102 }
1103
1104
1105
1106
1107 if (journal->j_checkpoint_transactions == NULL) {
1108 journal->j_checkpoint_transactions = commit_transaction;
1109 commit_transaction->t_cpnext = commit_transaction;
1110 commit_transaction->t_cpprev = commit_transaction;
1111 } else {
1112 commit_transaction->t_cpnext =
1113 journal->j_checkpoint_transactions;
1114 commit_transaction->t_cpprev =
1115 commit_transaction->t_cpnext->t_cpprev;
1116 commit_transaction->t_cpnext->t_cpprev =
1117 commit_transaction;
1118 commit_transaction->t_cpprev->t_cpnext =
1119 commit_transaction;
1120 }
1121 spin_unlock(&journal->j_list_lock);
1122
1123
1124
1125 jbd2_debug(3, "JBD2: commit phase 7\n");
1126
1127 J_ASSERT(commit_transaction->t_state == T_COMMIT_JFLUSH);
1128
1129 commit_transaction->t_start = jiffies;
1130 stats.run.rs_logging = jbd2_time_diff(stats.run.rs_logging,
1131 commit_transaction->t_start);
1132
1133
1134
1135
1136 stats.ts_tid = commit_transaction->t_tid;
1137 stats.run.rs_handle_count =
1138 atomic_read(&commit_transaction->t_handle_count);
1139 trace_jbd2_run_stats(journal->j_fs_dev->bd_dev,
1140 commit_transaction->t_tid, &stats.run);
1141 stats.ts_requested = (commit_transaction->t_requested) ? 1 : 0;
1142
1143 commit_transaction->t_state = T_COMMIT_CALLBACK;
1144 J_ASSERT(commit_transaction == journal->j_committing_transaction);
1145 journal->j_commit_sequence = commit_transaction->t_tid;
1146 journal->j_committing_transaction = NULL;
1147 commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
1148
1149
1150
1151
1152
1153 if (likely(journal->j_average_commit_time))
1154 journal->j_average_commit_time = (commit_time +
1155 journal->j_average_commit_time*3) / 4;
1156 else
1157 journal->j_average_commit_time = commit_time;
1158
1159 write_unlock(&journal->j_state_lock);
1160
1161 if (journal->j_commit_callback)
1162 journal->j_commit_callback(journal, commit_transaction);
1163 if (journal->j_fc_cleanup_callback)
1164 journal->j_fc_cleanup_callback(journal, 1, commit_transaction->t_tid);
1165
1166 trace_jbd2_end_commit(journal, commit_transaction);
1167 jbd2_debug(1, "JBD2: commit %d complete, head %d\n",
1168 journal->j_commit_sequence, journal->j_tail_sequence);
1169
1170 write_lock(&journal->j_state_lock);
1171 journal->j_flags &= ~JBD2_FULL_COMMIT_ONGOING;
1172 journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING;
1173 spin_lock(&journal->j_list_lock);
1174 commit_transaction->t_state = T_FINISHED;
1175
1176 if (commit_transaction->t_checkpoint_list == NULL &&
1177 commit_transaction->t_checkpoint_io_list == NULL) {
1178 __jbd2_journal_drop_transaction(journal, commit_transaction);
1179 jbd2_journal_free_transaction(commit_transaction);
1180 }
1181 spin_unlock(&journal->j_list_lock);
1182 write_unlock(&journal->j_state_lock);
1183 wake_up(&journal->j_wait_done_commit);
1184 wake_up(&journal->j_fc_wait);
1185
1186
1187
1188
1189 spin_lock(&journal->j_history_lock);
1190 journal->j_stats.ts_tid++;
1191 journal->j_stats.ts_requested += stats.ts_requested;
1192 journal->j_stats.run.rs_wait += stats.run.rs_wait;
1193 journal->j_stats.run.rs_request_delay += stats.run.rs_request_delay;
1194 journal->j_stats.run.rs_running += stats.run.rs_running;
1195 journal->j_stats.run.rs_locked += stats.run.rs_locked;
1196 journal->j_stats.run.rs_flushing += stats.run.rs_flushing;
1197 journal->j_stats.run.rs_logging += stats.run.rs_logging;
1198 journal->j_stats.run.rs_handle_count += stats.run.rs_handle_count;
1199 journal->j_stats.run.rs_blocks += stats.run.rs_blocks;
1200 journal->j_stats.run.rs_blocks_logged += stats.run.rs_blocks_logged;
1201 spin_unlock(&journal->j_history_lock);
1202 }