16 #include <linux/time.h>
19 #include <linux/errno.h>
29 static void journal_end_buffer_io_sync(
struct buffer_head *bh,
int uptodate)
33 set_buffer_uptodate(bh);
35 clear_buffer_uptodate(bh);
53 static void release_buffer_page(
struct buffer_head *bh)
68 if (!trylock_page(page))
86 static void release_data_buffer(
struct buffer_head *bh)
88 if (buffer_freed(bh)) {
90 clear_buffer_freed(bh);
91 clear_buffer_mapped(bh);
95 release_buffer_page(bh);
105 static int inverted_lock(journal_t *journal,
struct buffer_head *bh)
107 if (!jbd_trylock_bh_state(bh)) {
108 spin_unlock(&journal->j_list_lock);
122 static int journal_write_commit_record(journal_t *journal,
126 struct buffer_head *bh;
130 if (is_journal_aborted(journal))
137 bh = jh2bh(descriptor);
144 JBUFFER_TRACE(descriptor,
"write commit block");
145 set_buffer_dirty(bh);
147 if (journal->j_flags & JFS_BARRIER)
155 return (ret == -
EIO);
158 static void journal_do_submit_data(
struct buffer_head **wbuf,
int bufs,
163 for (i = 0; i <
bufs; i++) {
173 static int journal_submit_data_buffers(journal_t *journal,
178 struct buffer_head *bh;
181 struct buffer_head **wbuf = journal->j_wbuf;
194 spin_lock(&journal->j_list_lock);
196 while (commit_transaction->t_sync_datalist) {
197 jh = commit_transaction->t_sync_datalist;
209 if (buffer_dirty(bh)) {
210 if (!trylock_buffer(bh)) {
211 BUFFER_TRACE(bh,
"needs blocking lock");
212 spin_unlock(&journal->j_list_lock);
213 trace_jbd_do_submit_data(journal,
216 journal_do_submit_data(wbuf, bufs, write_op);
219 spin_lock(&journal->j_list_lock);
224 if (!inverted_lock(journal, bh)) {
225 jbd_lock_bh_state(bh);
226 spin_lock(&journal->j_list_lock);
229 if (!buffer_jbd(bh) || bh2jh(bh) != jh
232 jbd_unlock_bh_state(bh);
235 BUFFER_TRACE(bh,
"already cleaned up");
236 release_data_buffer(bh);
239 if (locked && test_clear_buffer_dirty(bh)) {
240 BUFFER_TRACE(bh,
"needs writeout, adding to array");
244 jbd_unlock_bh_state(bh);
245 if (bufs == journal->j_wbufsize) {
246 spin_unlock(&journal->j_list_lock);
247 trace_jbd_do_submit_data(journal,
249 journal_do_submit_data(wbuf, bufs, write_op);
253 }
else if (!locked && buffer_locked(bh)) {
256 jbd_unlock_bh_state(bh);
259 BUFFER_TRACE(bh,
"writeout complete: unfile");
263 jbd_unlock_bh_state(bh);
266 release_data_buffer(bh);
269 if (need_resched() || spin_needbreak(&journal->j_list_lock)) {
270 spin_unlock(&journal->j_list_lock);
274 spin_unlock(&journal->j_list_lock);
275 trace_jbd_do_submit_data(journal, commit_transaction);
276 journal_do_submit_data(wbuf, bufs, write_op);
291 struct buffer_head **wbuf = journal->j_wbuf;
306 int write_op =
WRITE;
314 if (journal->j_flags & JFS_FLUSHED) {
327 jbd_debug(3,
"superblock not updated\n");
330 J_ASSERT(journal->j_running_transaction !=
NULL);
331 J_ASSERT(journal->j_committing_transaction ==
NULL);
333 commit_transaction = journal->j_running_transaction;
334 J_ASSERT(commit_transaction->t_state == T_RUNNING);
336 trace_jbd_start_commit(journal, commit_transaction);
337 jbd_debug(1,
"JBD: starting commit of transaction %d\n",
338 commit_transaction->t_tid);
340 spin_lock(&journal->j_state_lock);
341 commit_transaction->t_state = T_LOCKED;
343 trace_jbd_commit_locking(journal, commit_transaction);
344 spin_lock(&commit_transaction->t_handle_lock);
345 while (commit_transaction->t_updates) {
350 if (commit_transaction->t_updates) {
351 spin_unlock(&commit_transaction->t_handle_lock);
352 spin_unlock(&journal->j_state_lock);
354 spin_lock(&journal->j_state_lock);
355 spin_lock(&commit_transaction->t_handle_lock);
359 spin_unlock(&commit_transaction->t_handle_lock);
361 J_ASSERT (commit_transaction->t_outstanding_credits <=
362 journal->j_max_transaction_buffers);
380 while (commit_transaction->t_reserved_list) {
381 jh = commit_transaction->t_reserved_list;
382 JBUFFER_TRACE(jh,
"reserved, unused: refile");
388 struct buffer_head *bh = jh2bh(jh);
390 jbd_lock_bh_state(bh);
393 jbd_unlock_bh_state(bh);
403 spin_lock(&journal->j_list_lock);
405 spin_unlock(&journal->j_list_lock);
413 journal_clear_buffer_revoked_flags(journal);
418 journal_switch_revoke_table(journal);
420 trace_jbd_commit_flushing(journal, commit_transaction);
421 commit_transaction->t_state = T_FLUSH;
422 journal->j_committing_transaction = commit_transaction;
423 journal->j_running_transaction =
NULL;
425 commit_transaction->t_log_start = journal->j_head;
426 wake_up(&journal->j_wait_transaction_locked);
427 spin_unlock(&journal->j_state_lock);
431 if (tid_geq(journal->j_commit_waited, commit_transaction->t_tid))
439 err = journal_submit_data_buffers(journal, commit_transaction,
446 spin_lock(&journal->j_list_lock);
447 while (commit_transaction->t_locked_list) {
448 struct buffer_head *bh;
450 jh = commit_transaction->t_locked_list->
b_tprev;
453 if (buffer_locked(bh)) {
454 spin_unlock(&journal->j_list_lock);
456 spin_lock(&journal->j_list_lock);
458 if (
unlikely(!buffer_uptodate(bh))) {
459 if (!trylock_page(bh->b_page)) {
460 spin_unlock(&journal->j_list_lock);
461 lock_page(bh->b_page);
462 spin_lock(&journal->j_list_lock);
464 if (bh->b_page->mapping)
468 SetPageError(bh->b_page);
471 if (!inverted_lock(journal, bh)) {
473 spin_lock(&journal->j_list_lock);
476 if (buffer_jbd(bh) && bh2jh(bh) == jh &&
480 jbd_unlock_bh_state(bh);
481 release_data_buffer(bh);
484 spin_unlock(&journal->j_list_lock);
490 "JBD: Detected IO errors while flushing file data "
491 "on %s\n",
bdevname(journal->j_fs_dev, b));
492 if (journal->j_flags & JFS_ABORT_ON_SYNCDATA_ERR)
499 journal_write_revoke_records(journal, commit_transaction, write_op);
507 J_ASSERT (commit_transaction->t_sync_datalist ==
NULL);
516 spin_lock(&journal->j_state_lock);
517 commit_transaction->t_state = T_COMMIT;
518 spin_unlock(&journal->j_state_lock);
520 trace_jbd_commit_logging(journal, commit_transaction);
521 J_ASSERT(commit_transaction->t_nr_buffers <=
522 commit_transaction->t_outstanding_credits);
526 while (commit_transaction->t_buffers) {
530 jh = commit_transaction->t_buffers;
535 if (is_journal_aborted(journal)) {
536 clear_buffer_jbddirty(jh2bh(jh));
537 JBUFFER_TRACE(jh,
"journal is aborting: refile");
543 if (!commit_transaction->t_buffers)
544 goto start_journal_io;
552 struct buffer_head *bh;
554 J_ASSERT (bufs == 0);
564 bh = jh2bh(descriptor);
565 jbd_debug(4,
"JBD: got buffer %llu (%p)\n",
566 (
unsigned long long)bh->b_blocknr, bh->b_data);
575 set_buffer_jwrite(bh);
576 set_buffer_dirty(bh);
581 BUFFER_TRACE(bh,
"ph3: file as descriptor");
602 commit_transaction->t_outstanding_credits--;
613 set_buffer_jwrite(jh2bh(jh));
620 JBUFFER_TRACE(jh,
"ph3: write metadata");
622 jh, &new_jh, blocknr);
623 set_buffer_jwrite(jh2bh(new_jh));
624 wbuf[bufs++] = jh2bh(new_jh);
642 memcpy (tagp, journal->j_uuid, 16);
651 if (bufs == journal->j_wbufsize ||
652 commit_transaction->t_buffers ==
NULL ||
655 jbd_debug(4,
"JBD: Submit %d IOs\n", bufs);
664 for (i = 0; i <
bufs; i++) {
665 struct buffer_head *bh = wbuf[
i];
667 clear_buffer_dirty(bh);
668 set_buffer_uptodate(bh);
669 bh->b_end_io = journal_end_buffer_io_sync;
701 while (commit_transaction->t_iobuf_list !=
NULL) {
702 struct buffer_head *bh;
704 jh = commit_transaction->t_iobuf_list->
b_tprev;
706 if (buffer_locked(bh)) {
716 clear_buffer_jwrite(bh);
718 JBUFFER_TRACE(jh,
"ph4: unfile after journal write");
725 BUFFER_TRACE(bh,
"dumping temporary bh");
733 jh = commit_transaction->t_shadow_list->
b_tprev;
735 clear_buffer_jwrite(bh);
736 J_ASSERT_BH(bh, buffer_jbddirty(bh));
742 JBUFFER_TRACE(jh,
"file as BJ_Forget");
752 JBUFFER_TRACE(jh,
"brelse shadowed buffer");
756 J_ASSERT (commit_transaction->t_shadow_list ==
NULL);
762 while (commit_transaction->t_log_list !=
NULL) {
763 struct buffer_head *bh;
765 jh = commit_transaction->t_log_list->
b_tprev;
767 if (buffer_locked(bh)) {
769 goto wait_for_ctlbuf;
772 goto wait_for_ctlbuf;
777 BUFFER_TRACE(bh,
"ph5: control buffer writeout done: unfile");
778 clear_buffer_jwrite(bh);
791 spin_lock(&journal->j_state_lock);
792 J_ASSERT(commit_transaction->t_state == T_COMMIT);
793 commit_transaction->t_state = T_COMMIT_RECORD;
794 spin_unlock(&journal->j_state_lock);
796 if (journal_write_commit_record(journal, commit_transaction))
809 J_ASSERT(commit_transaction->t_sync_datalist ==
NULL);
810 J_ASSERT(commit_transaction->t_buffers ==
NULL);
811 J_ASSERT(commit_transaction->t_checkpoint_list ==
NULL);
812 J_ASSERT(commit_transaction->t_iobuf_list ==
NULL);
813 J_ASSERT(commit_transaction->t_shadow_list ==
NULL);
814 J_ASSERT(commit_transaction->t_log_list ==
NULL);
821 spin_lock(&journal->j_list_lock);
822 while (commit_transaction->t_forget) {
824 struct buffer_head *bh;
827 jh = commit_transaction->t_forget;
828 spin_unlock(&journal->j_list_lock);
835 jbd_lock_bh_state(bh);
861 spin_lock(&journal->j_list_lock);
863 if (cp_transaction) {
864 JBUFFER_TRACE(jh,
"remove from old cp transaction");
878 if (buffer_freed(bh)) {
896 clear_buffer_freed(bh);
897 clear_buffer_jbddirty(bh);
898 clear_buffer_mapped(bh);
899 clear_buffer_new(bh);
900 clear_buffer_req(bh);
905 if (buffer_jbddirty(bh)) {
906 JBUFFER_TRACE(jh,
"add to new checkpointing trans");
908 if (is_journal_aborted(journal))
909 clear_buffer_jbddirty(bh);
911 J_ASSERT_BH(bh, !buffer_dirty(bh));
924 JBUFFER_TRACE(jh,
"refile or unfile freed buffer");
926 jbd_unlock_bh_state(bh);
928 release_buffer_page(bh);
933 spin_unlock(&journal->j_list_lock);
940 spin_lock(&journal->j_state_lock);
941 spin_lock(&journal->j_list_lock);
946 if (commit_transaction->t_forget) {
947 spin_unlock(&journal->j_list_lock);
948 spin_unlock(&journal->j_state_lock);
956 J_ASSERT(commit_transaction->t_state == T_COMMIT_RECORD);
958 commit_transaction->t_state = T_FINISHED;
959 J_ASSERT(commit_transaction == journal->j_committing_transaction);
960 journal->j_commit_sequence = commit_transaction->t_tid;
961 journal->j_committing_transaction =
NULL;
962 commit_time = ktime_to_ns(ktime_sub(
ktime_get(), start_time));
968 if (
likely(journal->j_average_commit_time))
969 journal->j_average_commit_time = (commit_time*3 +
970 journal->j_average_commit_time) / 4;
972 journal->j_average_commit_time = commit_time;
974 spin_unlock(&journal->j_state_lock);
976 if (commit_transaction->t_checkpoint_list ==
NULL &&
977 commit_transaction->t_checkpoint_io_list ==
NULL) {
980 if (journal->j_checkpoint_transactions ==
NULL) {
981 journal->j_checkpoint_transactions = commit_transaction;
982 commit_transaction->t_cpnext = commit_transaction;
983 commit_transaction->t_cpprev = commit_transaction;
985 commit_transaction->t_cpnext =
986 journal->j_checkpoint_transactions;
987 commit_transaction->t_cpprev =
988 commit_transaction->t_cpnext->t_cpprev;
989 commit_transaction->t_cpnext->t_cpprev =
991 commit_transaction->t_cpprev->t_cpnext =
995 spin_unlock(&journal->j_list_lock);
997 trace_jbd_end_commit(journal, commit_transaction);
998 jbd_debug(1,
"JBD: commit %d complete, head %d\n",
999 journal->j_commit_sequence, journal->j_tail_sequence);
1001 wake_up(&journal->j_wait_done_commit);