20 #include <linux/time.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
33 static inline void __buffer_unlink_first(
struct journal_head *jh)
39 if (transaction->t_checkpoint_list == jh) {
40 transaction->t_checkpoint_list = jh->
b_cpnext;
41 if (transaction->t_checkpoint_list == jh)
42 transaction->t_checkpoint_list =
NULL;
51 static inline void __buffer_unlink(
struct journal_head *jh)
55 __buffer_unlink_first(jh);
56 if (transaction->t_checkpoint_io_list == jh) {
57 transaction->t_checkpoint_io_list = jh->
b_cpnext;
58 if (transaction->t_checkpoint_io_list == jh)
59 transaction->t_checkpoint_io_list =
NULL;
68 static inline void __buffer_relink_io(
struct journal_head *jh)
72 __buffer_unlink_first(jh);
74 if (!transaction->t_checkpoint_io_list) {
77 jh->
b_cpnext = transaction->t_checkpoint_io_list;
78 jh->
b_cpprev = transaction->t_checkpoint_io_list->b_cpprev;
82 transaction->t_checkpoint_io_list = jh;
95 struct buffer_head *bh = jh2bh(jh);
98 !buffer_dirty(bh) && !buffer_write_io_error(bh)) {
104 JBUFFER_TRACE(jh,
"remove from checkpoint list");
106 BUFFER_TRACE(bh,
"release");
120 int nblocks, space_left;
123 nblocks = jbd_space_needed(journal);
125 if (journal->j_flags & JBD2_ABORT)
142 spin_lock(&journal->j_list_lock);
143 nblocks = jbd_space_needed(journal);
145 if (space_left < nblocks) {
146 int chkpt = journal->j_checkpoint_transactions !=
NULL;
149 if (journal->j_committing_transaction)
150 tid = journal->j_committing_transaction->t_tid;
151 spin_unlock(&journal->j_list_lock);
162 "only had %d space available\n",
163 __func__, nblocks, space_left);
165 "journal space in %s\n", __func__,
172 spin_unlock(&journal->j_list_lock);
189 static int __wait_cp_io(journal_t *journal,
transaction_t *transaction)
192 struct buffer_head *bh;
197 this_tid = transaction->t_tid;
200 if (journal->j_checkpoint_transactions != transaction ||
201 transaction->t_tid != this_tid)
203 while (!released && transaction->t_checkpoint_io_list) {
204 jh = transaction->t_checkpoint_io_list;
207 if (buffer_locked(bh)) {
208 spin_unlock(&journal->j_list_lock);
211 BUFFER_TRACE(bh,
"brelse");
213 spin_lock(&journal->j_list_lock);
216 if (
unlikely(buffer_write_io_error(bh)))
231 __flush_batch(journal_t *journal,
int *batch_count)
237 for (i = 0; i < *batch_count; i++)
241 for (i = 0; i < *batch_count; i++) {
242 struct buffer_head *bh = journal->j_chkpt_bhs[
i];
243 BUFFER_TRACE(bh,
"brelse");
258 static int __process_buffer(journal_t *journal,
struct journal_head *jh,
261 struct buffer_head *bh = jh2bh(jh);
264 if (buffer_locked(bh)) {
266 spin_unlock(&journal->j_list_lock);
269 BUFFER_TRACE(bh,
"brelse");
276 transaction->t_chp_stats.cs_forced_to_close++;
277 spin_unlock(&journal->j_list_lock);
278 if (
unlikely(journal->j_flags & JBD2_UNMOUNT))
285 "Waiting for Godot: block %llu\n",
287 (
unsigned long long) bh->b_blocknr);
291 }
else if (!buffer_dirty(bh)) {
293 if (
unlikely(buffer_write_io_error(bh)))
296 BUFFER_TRACE(bh,
"remove from checkpoint");
298 spin_unlock(&journal->j_list_lock);
308 BUFFER_TRACE(bh,
"queue");
310 J_ASSERT_BH(bh, !buffer_jwrite(bh));
311 journal->j_chkpt_bhs[*batch_count] = bh;
312 __buffer_relink_io(jh);
313 transaction->t_chp_stats.cs_written++;
315 if (*batch_count == JBD2_NR_BATCH) {
316 spin_unlock(&journal->j_list_lock);
317 __flush_batch(journal, batch_count);
346 trace_jbd2_checkpoint(journal, result);
347 jbd_debug(1,
"cleanup_journal_tail returned %d\n", result);
356 spin_lock(&journal->j_list_lock);
357 if (!journal->j_checkpoint_transactions)
359 transaction = journal->j_checkpoint_transactions;
360 if (transaction->t_chp_stats.cs_chp_time == 0)
361 transaction->t_chp_stats.cs_chp_time =
jiffies;
362 this_tid = transaction->t_tid;
369 if (journal->j_checkpoint_transactions == transaction &&
370 transaction->t_tid == this_tid) {
375 while (!retry && transaction->t_checkpoint_list) {
376 jh = transaction->t_checkpoint_list;
377 retry = __process_buffer(journal, jh, &batch_count,
379 if (retry < 0 && !result)
381 if (!retry && (need_resched() ||
382 spin_needbreak(&journal->j_list_lock))) {
383 spin_unlock(&journal->j_list_lock);
391 spin_unlock(&journal->j_list_lock);
394 __flush_batch(journal, &batch_count);
398 spin_lock(&journal->j_list_lock);
405 err = __wait_cp_io(journal, transaction);
410 spin_unlock(&journal->j_list_lock);
416 return (result < 0) ? result : 0;
442 if (is_journal_aborted(journal))
447 J_ASSERT(blocknr != 0);
457 if (journal->j_flags & JBD2_BARRIER)
478 static int journal_clean_one_cp_list(
struct journal_head *jh,
int *released)
492 ret = __try_to_free_cp_buf(jh);
508 }
while (jh != last_jh);
529 transaction = journal->j_checkpoint_transactions;
533 last_transaction = transaction->t_cpprev;
536 transaction = next_transaction;
537 next_transaction = transaction->t_cpnext;
538 ret += journal_clean_one_cp_list(transaction->
539 t_checkpoint_list, &released);
554 ret += journal_clean_one_cp_list(transaction->
555 t_checkpoint_io_list, &released);
558 }
while (transaction != last_transaction);
583 struct transaction_chp_stats_s *
stats;
588 JBUFFER_TRACE(jh,
"entry");
591 JBUFFER_TRACE(jh,
"not on transaction");
594 journal = transaction->t_journal;
596 JBUFFER_TRACE(jh,
"removing from transaction");
601 if (transaction->t_checkpoint_list !=
NULL ||
602 transaction->t_checkpoint_io_list !=
NULL)
614 if (transaction->t_state != T_FINISHED)
619 stats = &transaction->t_chp_stats;
620 if (stats->cs_chp_time)
621 stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
623 trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
624 transaction->t_tid, stats);
631 wake_up(&journal->j_wait_logspace);
648 JBUFFER_TRACE(jh,
"entry");
649 J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
656 if (!transaction->t_checkpoint_list) {
659 jh->
b_cpnext = transaction->t_checkpoint_list;
660 jh->
b_cpprev = transaction->t_checkpoint_list->b_cpprev;
664 transaction->t_checkpoint_list = jh;
680 if (transaction->t_cpnext) {
681 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
682 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
683 if (journal->j_checkpoint_transactions == transaction)
684 journal->j_checkpoint_transactions =
685 transaction->t_cpnext;
686 if (journal->j_checkpoint_transactions == transaction)
687 journal->j_checkpoint_transactions =
NULL;
690 J_ASSERT(transaction->t_state == T_FINISHED);
691 J_ASSERT(transaction->t_buffers ==
NULL);
692 J_ASSERT(transaction->t_forget ==
NULL);
693 J_ASSERT(transaction->t_iobuf_list ==
NULL);
694 J_ASSERT(transaction->t_shadow_list ==
NULL);
695 J_ASSERT(transaction->t_log_list ==
NULL);
696 J_ASSERT(transaction->t_checkpoint_list ==
NULL);
697 J_ASSERT(transaction->t_checkpoint_io_list ==
NULL);
698 J_ASSERT(
atomic_read(&transaction->t_updates) == 0);
699 J_ASSERT(journal->j_committing_transaction != transaction);
700 J_ASSERT(journal->j_running_transaction != transaction);
702 trace_jbd2_drop_transaction(journal, transaction);
704 jbd_debug(1,
"Dropping transaction %d, all done\n", transaction->t_tid);