16 #include <linux/time.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
29 #include <linux/bitops.h>
35 static void journal_end_buffer_io_sync(
struct buffer_head *bh,
int uptodate)
39 set_buffer_uptodate(bh);
41 clear_buffer_uptodate(bh);
59 static void release_buffer_page(
struct buffer_head *bh)
74 if (!trylock_page(page))
88 static void jbd2_commit_block_csum_set(journal_t *
j,
101 csum = jbd2_chksum(j, j->j_csum_seed, jh2bh(descriptor)->b_data,
114 static int journal_submit_commit_record(journal_t *journal,
116 struct buffer_head **cbh,
121 struct buffer_head *bh;
127 if (is_journal_aborted(journal))
134 bh = jh2bh(descriptor);
149 jbd2_commit_block_csum_set(journal, descriptor);
151 JBUFFER_TRACE(descriptor,
"submit commit block");
153 clear_buffer_dirty(bh);
154 set_buffer_uptodate(bh);
155 bh->b_end_io = journal_end_buffer_io_sync;
157 if (journal->j_flags & JBD2_BARRIER &&
172 static int journal_wait_on_commit_record(journal_t *journal,
173 struct buffer_head *bh)
177 clear_buffer_dirty(bh);
199 .nr_to_write = mapping->
nrpages * 2,
201 .range_end = i_size_read(mapping->
host),
216 static int journal_submit_data_buffers(journal_t *journal,
219 struct jbd2_inode *jinode;
223 spin_lock(&journal->j_list_lock);
225 mapping = jinode->i_vfs_inode->i_mapping;
226 set_bit(__JI_COMMIT_RUNNING, &jinode->i_flags);
227 spin_unlock(&journal->j_list_lock);
234 trace_jbd2_submit_inode_data(jinode->i_vfs_inode);
235 err = journal_submit_inode_data_buffers(mapping);
238 spin_lock(&journal->j_list_lock);
239 J_ASSERT(jinode->i_transaction == commit_transaction);
240 clear_bit(__JI_COMMIT_RUNNING, &jinode->i_flags);
242 wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
244 spin_unlock(&journal->j_list_lock);
253 static int journal_finish_inode_data_buffers(journal_t *journal,
256 struct jbd2_inode *jinode, *next_i;
260 spin_lock(&journal->j_list_lock);
262 set_bit(__JI_COMMIT_RUNNING, &jinode->i_flags);
263 spin_unlock(&journal->j_list_lock);
272 &jinode->i_vfs_inode->i_mapping->flags);
277 spin_lock(&journal->j_list_lock);
278 clear_bit(__JI_COMMIT_RUNNING, &jinode->i_flags);
280 wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
285 &commit_transaction->t_inode_list, i_list) {
287 if (jinode->i_next_transaction) {
288 jinode->i_transaction = jinode->i_next_transaction;
289 jinode->i_next_transaction =
NULL;
290 list_add(&jinode->i_list,
291 &jinode->i_transaction->t_inode_list);
293 jinode->i_transaction =
NULL;
296 spin_unlock(&journal->j_list_lock);
301 static __u32 jbd2_checksum_data(
__u32 crc32_sum,
struct buffer_head *bh)
303 struct page *page = bh->b_page;
316 unsigned long long block)
323 static void jbd2_descr_block_csum_set(journal_t *j,
333 (jh2bh(descriptor)->b_data + j->j_blocksize -
336 csum = jbd2_chksum(j, j->j_csum_seed, jh2bh(descriptor)->b_data,
344 struct page *page = bh->b_page;
353 csum = jbd2_chksum(j, j->j_csum_seed, (
__u8 *)&sequence,
369 struct transaction_stats_s stats;
372 struct buffer_head **wbuf = journal->j_wbuf;
387 struct buffer_head *cbh =
NULL;
388 __u32 crc32_sum = ~0;
391 unsigned long first_block;
405 if (journal->j_flags & JBD2_FLUSHED) {
415 journal->j_tail_sequence,
420 jbd_debug(3,
"superblock not updated\n");
423 J_ASSERT(journal->j_running_transaction !=
NULL);
424 J_ASSERT(journal->j_committing_transaction ==
NULL);
426 commit_transaction = journal->j_running_transaction;
427 J_ASSERT(commit_transaction->t_state == T_RUNNING);
429 trace_jbd2_start_commit(journal, commit_transaction);
430 jbd_debug(1,
"JBD2: starting commit of transaction %d\n",
431 commit_transaction->t_tid);
434 commit_transaction->t_state = T_LOCKED;
436 trace_jbd2_commit_locking(journal, commit_transaction);
437 stats.run.rs_wait = commit_transaction->t_max_wait;
439 stats.run.rs_running = jbd2_time_diff(commit_transaction->t_start,
440 stats.run.rs_locked);
442 spin_lock(&commit_transaction->t_handle_lock);
443 while (
atomic_read(&commit_transaction->t_updates)) {
449 spin_unlock(&commit_transaction->t_handle_lock);
453 spin_lock(&commit_transaction->t_handle_lock);
457 spin_unlock(&commit_transaction->t_handle_lock);
459 J_ASSERT (
atomic_read(&commit_transaction->t_outstanding_credits) <=
460 journal->j_max_transaction_buffers);
478 while (commit_transaction->t_reserved_list) {
479 jh = commit_transaction->t_reserved_list;
480 JBUFFER_TRACE(jh,
"reserved, unused: refile");
486 struct buffer_head *bh = jh2bh(jh);
488 jbd_lock_bh_state(bh);
491 jbd_unlock_bh_state(bh);
501 spin_lock(&journal->j_list_lock);
503 spin_unlock(&journal->j_list_lock);
511 jbd2_clear_buffer_revoked_flags(journal);
516 jbd2_journal_switch_revoke_table(journal);
518 trace_jbd2_commit_flushing(journal, commit_transaction);
519 stats.run.rs_flushing =
jiffies;
520 stats.run.rs_locked = jbd2_time_diff(stats.run.rs_locked,
521 stats.run.rs_flushing);
523 commit_transaction->t_state = T_FLUSH;
524 journal->j_committing_transaction = commit_transaction;
525 journal->j_running_transaction =
NULL;
527 commit_transaction->t_log_start = journal->j_head;
528 wake_up(&journal->j_wait_transaction_locked);
537 err = journal_submit_data_buffers(journal, commit_transaction);
542 jbd2_journal_write_revoke_records(journal, commit_transaction,
554 commit_transaction->t_state = T_COMMIT;
557 trace_jbd2_commit_logging(journal, commit_transaction);
558 stats.run.rs_logging =
jiffies;
559 stats.run.rs_flushing = jbd2_time_diff(stats.run.rs_flushing,
560 stats.run.rs_logging);
561 stats.run.rs_blocks =
562 atomic_read(&commit_transaction->t_outstanding_credits);
563 stats.run.rs_blocks_logged = 0;
565 J_ASSERT(commit_transaction->t_nr_buffers <=
566 atomic_read(&commit_transaction->t_outstanding_credits));
572 while (commit_transaction->t_buffers) {
576 jh = commit_transaction->t_buffers;
581 if (is_journal_aborted(journal)) {
582 clear_buffer_jbddirty(jh2bh(jh));
583 JBUFFER_TRACE(jh,
"journal is aborting: refile");
593 if (!commit_transaction->t_buffers)
594 goto start_journal_io;
602 struct buffer_head *bh;
604 J_ASSERT (bufs == 0);
614 bh = jh2bh(descriptor);
615 jbd_debug(4,
"JBD2: got buffer %llu (%p)\n",
616 (
unsigned long long)bh->b_blocknr, bh->b_data);
625 set_buffer_jwrite(bh);
626 set_buffer_dirty(bh);
631 BUFFER_TRACE(bh,
"ph3: file as descriptor");
652 atomic_dec(&commit_transaction->t_outstanding_credits);
670 JBUFFER_TRACE(jh,
"ph3: write metadata");
672 jh, &new_jh, blocknr);
678 wbuf[bufs++] = jh2bh(new_jh);
690 write_tag_block(tag_bytes, tag, jh2bh(jh)->b_blocknr);
692 jbd2_block_tag_csum_set(journal, tag, jh2bh(new_jh),
693 commit_transaction->t_tid);
695 space_left -= tag_bytes;
698 memcpy (tagp, journal->j_uuid, 16);
707 if (bufs == journal->j_wbufsize ||
708 commit_transaction->t_buffers ==
NULL ||
709 space_left < tag_bytes + 16 + csum_size) {
711 jbd_debug(4,
"JBD2: Submit %d IOs\n", bufs);
719 jbd2_descr_block_csum_set(journal, descriptor);
721 for (i = 0; i <
bufs; i++) {
722 struct buffer_head *bh = wbuf[
i];
729 jbd2_checksum_data(crc32_sum, bh);
733 clear_buffer_dirty(bh);
734 set_buffer_uptodate(bh);
735 bh->b_end_io = journal_end_buffer_io_sync;
739 stats.run.rs_blocks_logged +=
bufs;
748 err = journal_finish_inode_data_buffers(journal, commit_transaction);
751 "JBD2: Detected IO errors while flushing file data "
752 "on %s\n", journal->j_devname);
753 if (journal->j_flags & JBD2_ABORT_ON_SYNCDATA_ERR)
770 long freed = first_block - journal->j_tail;
772 if (first_block < journal->j_tail)
773 freed += journal->j_last - journal->j_first;
775 if (freed < journal->j_maxlen / 4)
778 J_ASSERT(commit_transaction->t_state == T_COMMIT);
779 commit_transaction->t_state = T_COMMIT_DFLUSH;
787 if (commit_transaction->t_need_data_flush &&
788 (journal->j_fs_dev != journal->j_dev) &&
789 (journal->j_flags & JBD2_BARRIER))
795 err = journal_submit_commit_record(journal, commit_transaction,
821 while (commit_transaction->t_iobuf_list !=
NULL) {
822 struct buffer_head *bh;
824 jh = commit_transaction->t_iobuf_list->
b_tprev;
826 if (buffer_locked(bh)) {
836 clear_buffer_jwrite(bh);
838 JBUFFER_TRACE(jh,
"ph4: unfile after journal write");
845 BUFFER_TRACE(bh,
"dumping temporary bh");
853 jh = commit_transaction->t_shadow_list->
b_tprev;
856 J_ASSERT_BH(bh, buffer_jbddirty(bh));
862 JBUFFER_TRACE(jh,
"file as BJ_Forget");
872 JBUFFER_TRACE(jh,
"brelse shadowed buffer");
876 J_ASSERT (commit_transaction->t_shadow_list ==
NULL);
882 while (commit_transaction->t_log_list !=
NULL) {
883 struct buffer_head *bh;
885 jh = commit_transaction->t_log_list->
b_tprev;
887 if (buffer_locked(bh)) {
889 goto wait_for_ctlbuf;
892 goto wait_for_ctlbuf;
897 BUFFER_TRACE(bh,
"ph5: control buffer writeout done: unfile");
898 clear_buffer_jwrite(bh);
910 J_ASSERT(commit_transaction->t_state == T_COMMIT_DFLUSH);
911 commit_transaction->t_state = T_COMMIT_JFLUSH;
916 err = journal_submit_commit_record(journal, commit_transaction,
922 err = journal_wait_on_commit_record(journal, cbh);
925 journal->j_flags & JBD2_BARRIER) {
947 J_ASSERT(list_empty(&commit_transaction->t_inode_list));
948 J_ASSERT(commit_transaction->t_buffers ==
NULL);
949 J_ASSERT(commit_transaction->t_checkpoint_list ==
NULL);
950 J_ASSERT(commit_transaction->t_iobuf_list ==
NULL);
951 J_ASSERT(commit_transaction->t_shadow_list ==
NULL);
952 J_ASSERT(commit_transaction->t_log_list ==
NULL);
959 spin_lock(&journal->j_list_lock);
960 while (commit_transaction->t_forget) {
962 struct buffer_head *bh;
965 jh = commit_transaction->t_forget;
966 spin_unlock(&journal->j_list_lock);
973 jbd_lock_bh_state(bh);
1003 spin_lock(&journal->j_list_lock);
1005 if (cp_transaction) {
1006 JBUFFER_TRACE(jh,
"remove from old cp transaction");
1007 cp_transaction->t_chp_stats.cs_dropped++;
1021 if (buffer_freed(bh)) {
1039 clear_buffer_freed(bh);
1040 clear_buffer_jbddirty(bh);
1041 clear_buffer_mapped(bh);
1042 clear_buffer_new(bh);
1043 clear_buffer_req(bh);
1048 if (buffer_jbddirty(bh)) {
1049 JBUFFER_TRACE(jh,
"add to new checkpointing trans");
1051 if (is_journal_aborted(journal))
1052 clear_buffer_jbddirty(bh);
1054 J_ASSERT_BH(bh, !buffer_dirty(bh));
1067 JBUFFER_TRACE(jh,
"refile or unfile buffer");
1069 jbd_unlock_bh_state(bh);
1071 release_buffer_page(bh);
1076 spin_unlock(&journal->j_list_lock);
1084 spin_lock(&journal->j_list_lock);
1089 if (commit_transaction->t_forget) {
1090 spin_unlock(&journal->j_list_lock);
1099 J_ASSERT(commit_transaction->t_state == T_COMMIT_JFLUSH);
1101 commit_transaction->t_start =
jiffies;
1102 stats.run.rs_logging = jbd2_time_diff(stats.run.rs_logging,
1103 commit_transaction->t_start);
1108 stats.ts_tid = commit_transaction->t_tid;
1109 stats.run.rs_handle_count =
1111 trace_jbd2_run_stats(journal->j_fs_dev->bd_dev,
1112 commit_transaction->t_tid, &stats.run);
1117 spin_lock(&journal->j_history_lock);
1118 journal->j_stats.ts_tid++;
1119 journal->j_stats.run.rs_wait += stats.run.rs_wait;
1120 journal->j_stats.run.rs_running += stats.run.rs_running;
1121 journal->j_stats.run.rs_locked += stats.run.rs_locked;
1122 journal->j_stats.run.rs_flushing += stats.run.rs_flushing;
1123 journal->j_stats.run.rs_logging += stats.run.rs_logging;
1124 journal->j_stats.run.rs_handle_count += stats.run.rs_handle_count;
1125 journal->j_stats.run.rs_blocks += stats.run.rs_blocks;
1126 journal->j_stats.run.rs_blocks_logged += stats.run.rs_blocks_logged;
1127 spin_unlock(&journal->j_history_lock);
1129 commit_transaction->t_state = T_FINISHED;
1130 J_ASSERT(commit_transaction == journal->j_committing_transaction);
1131 journal->j_commit_sequence = commit_transaction->t_tid;
1132 journal->j_committing_transaction =
NULL;
1133 commit_time = ktime_to_ns(ktime_sub(
ktime_get(), start_time));
1139 if (
likely(journal->j_average_commit_time))
1140 journal->j_average_commit_time = (commit_time +
1141 journal->j_average_commit_time*3) / 4;
1143 journal->j_average_commit_time = commit_time;
1146 if (commit_transaction->t_checkpoint_list ==
NULL &&
1147 commit_transaction->t_checkpoint_io_list ==
NULL) {
1151 if (journal->j_checkpoint_transactions ==
NULL) {
1152 journal->j_checkpoint_transactions = commit_transaction;
1153 commit_transaction->t_cpnext = commit_transaction;
1154 commit_transaction->t_cpprev = commit_transaction;
1156 commit_transaction->t_cpnext =
1157 journal->j_checkpoint_transactions;
1158 commit_transaction->t_cpprev =
1159 commit_transaction->t_cpnext->t_cpprev;
1160 commit_transaction->t_cpnext->t_cpprev =
1162 commit_transaction->t_cpprev->t_cpnext =
1166 spin_unlock(&journal->j_list_lock);
1168 if (journal->j_commit_callback)
1169 journal->j_commit_callback(journal, commit_transaction);
1171 trace_jbd2_end_commit(journal, commit_transaction);
1172 jbd_debug(1,
"JBD2: commit %d complete, head %d\n",
1173 journal->j_commit_sequence, journal->j_tail_sequence);
1177 wake_up(&journal->j_wait_done_commit);