25e3c20eb19f6be5bb9031777b324bb5caa2e7fd
[platform/kernel/linux-starfive.git] / fs / jbd2 / checkpoint.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * linux/fs/jbd2/checkpoint.c
4  *
5  * Written by Stephen C. Tweedie <sct@redhat.com>, 1999
6  *
7  * Copyright 1999 Red Hat Software --- All Rights Reserved
8  *
9  * Checkpoint routines for the generic filesystem journaling code.
10  * Part of the ext2fs journaling system.
11  *
12  * Checkpointing is the process of ensuring that a section of the log is
13  * committed fully to disk, so that that portion of the log can be
14  * reused.
15  */
16
17 #include <linux/time.h>
18 #include <linux/fs.h>
19 #include <linux/jbd2.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/blkdev.h>
23 #include <trace/events/jbd2.h>
24
25 /*
26  * Unlink a buffer from a transaction checkpoint list.
27  *
28  * Called with j_list_lock held.
29  */
30 static inline void __buffer_unlink_first(struct journal_head *jh)
31 {
32         transaction_t *transaction = jh->b_cp_transaction;
33
34         jh->b_cpnext->b_cpprev = jh->b_cpprev;
35         jh->b_cpprev->b_cpnext = jh->b_cpnext;
36         if (transaction->t_checkpoint_list == jh) {
37                 transaction->t_checkpoint_list = jh->b_cpnext;
38                 if (transaction->t_checkpoint_list == jh)
39                         transaction->t_checkpoint_list = NULL;
40         }
41 }
42
43 /*
44  * Unlink a buffer from a transaction checkpoint(io) list.
45  *
46  * Called with j_list_lock held.
47  */
48 static inline void __buffer_unlink(struct journal_head *jh)
49 {
50         transaction_t *transaction = jh->b_cp_transaction;
51
52         __buffer_unlink_first(jh);
53         if (transaction->t_checkpoint_io_list == jh) {
54                 transaction->t_checkpoint_io_list = jh->b_cpnext;
55                 if (transaction->t_checkpoint_io_list == jh)
56                         transaction->t_checkpoint_io_list = NULL;
57         }
58 }
59
60 /*
61  * Check a checkpoint buffer could be release or not.
62  *
63  * Requires j_list_lock
64  */
65 static inline bool __cp_buffer_busy(struct journal_head *jh)
66 {
67         struct buffer_head *bh = jh2bh(jh);
68
69         return (jh->b_transaction || buffer_locked(bh) || buffer_dirty(bh));
70 }
71
72 /*
73  * __jbd2_log_wait_for_space: wait until there is space in the journal.
74  *
75  * Called under j-state_lock *only*.  It will be unlocked if we have to wait
76  * for a checkpoint to free up some space in the log.
77  */
78 void __jbd2_log_wait_for_space(journal_t *journal)
79 __acquires(&journal->j_state_lock)
80 __releases(&journal->j_state_lock)
81 {
82         int nblocks, space_left;
83         /* assert_spin_locked(&journal->j_state_lock); */
84
85         nblocks = journal->j_max_transaction_buffers;
86         while (jbd2_log_space_left(journal) < nblocks) {
87                 write_unlock(&journal->j_state_lock);
88                 mutex_lock_io(&journal->j_checkpoint_mutex);
89
90                 /*
91                  * Test again, another process may have checkpointed while we
92                  * were waiting for the checkpoint lock. If there are no
93                  * transactions ready to be checkpointed, try to recover
94                  * journal space by calling cleanup_journal_tail(), and if
95                  * that doesn't work, by waiting for the currently committing
96                  * transaction to complete.  If there is absolutely no way
97                  * to make progress, this is either a BUG or corrupted
98                  * filesystem, so abort the journal and leave a stack
99                  * trace for forensic evidence.
100                  */
101                 write_lock(&journal->j_state_lock);
102                 if (journal->j_flags & JBD2_ABORT) {
103                         mutex_unlock(&journal->j_checkpoint_mutex);
104                         return;
105                 }
106                 spin_lock(&journal->j_list_lock);
107                 space_left = jbd2_log_space_left(journal);
108                 if (space_left < nblocks) {
109                         int chkpt = journal->j_checkpoint_transactions != NULL;
110                         tid_t tid = 0;
111
112                         if (journal->j_committing_transaction)
113                                 tid = journal->j_committing_transaction->t_tid;
114                         spin_unlock(&journal->j_list_lock);
115                         write_unlock(&journal->j_state_lock);
116                         if (chkpt) {
117                                 jbd2_log_do_checkpoint(journal);
118                         } else if (jbd2_cleanup_journal_tail(journal) == 0) {
119                                 /* We were able to recover space; yay! */
120                                 ;
121                         } else if (tid) {
122                                 /*
123                                  * jbd2_journal_commit_transaction() may want
124                                  * to take the checkpoint_mutex if JBD2_FLUSHED
125                                  * is set.  So we need to temporarily drop it.
126                                  */
127                                 mutex_unlock(&journal->j_checkpoint_mutex);
128                                 jbd2_log_wait_commit(journal, tid);
129                                 write_lock(&journal->j_state_lock);
130                                 continue;
131                         } else {
132                                 printk(KERN_ERR "%s: needed %d blocks and "
133                                        "only had %d space available\n",
134                                        __func__, nblocks, space_left);
135                                 printk(KERN_ERR "%s: no way to get more "
136                                        "journal space in %s\n", __func__,
137                                        journal->j_devname);
138                                 WARN_ON(1);
139                                 jbd2_journal_abort(journal, -EIO);
140                         }
141                         write_lock(&journal->j_state_lock);
142                 } else {
143                         spin_unlock(&journal->j_list_lock);
144                 }
145                 mutex_unlock(&journal->j_checkpoint_mutex);
146         }
147 }
148
149 static void
150 __flush_batch(journal_t *journal, int *batch_count)
151 {
152         int i;
153         struct blk_plug plug;
154
155         blk_start_plug(&plug);
156         for (i = 0; i < *batch_count; i++)
157                 write_dirty_buffer(journal->j_chkpt_bhs[i], REQ_SYNC);
158         blk_finish_plug(&plug);
159
160         for (i = 0; i < *batch_count; i++) {
161                 struct buffer_head *bh = journal->j_chkpt_bhs[i];
162                 BUFFER_TRACE(bh, "brelse");
163                 __brelse(bh);
164                 journal->j_chkpt_bhs[i] = NULL;
165         }
166         *batch_count = 0;
167 }
168
169 /*
170  * Perform an actual checkpoint. We take the first transaction on the
171  * list of transactions to be checkpointed and send all its buffers
172  * to disk. We submit larger chunks of data at once.
173  *
174  * The journal should be locked before calling this function.
175  * Called with j_checkpoint_mutex held.
176  */
177 int jbd2_log_do_checkpoint(journal_t *journal)
178 {
179         struct journal_head     *jh;
180         struct buffer_head      *bh;
181         transaction_t           *transaction;
182         tid_t                   this_tid;
183         int                     result, batch_count = 0;
184
185         jbd2_debug(1, "Start checkpoint\n");
186
187         /*
188          * First thing: if there are any transactions in the log which
189          * don't need checkpointing, just eliminate them from the
190          * journal straight away.
191          */
192         result = jbd2_cleanup_journal_tail(journal);
193         trace_jbd2_checkpoint(journal, result);
194         jbd2_debug(1, "cleanup_journal_tail returned %d\n", result);
195         if (result <= 0)
196                 return result;
197
198         /*
199          * OK, we need to start writing disk blocks.  Take one transaction
200          * and write it.
201          */
202         spin_lock(&journal->j_list_lock);
203         if (!journal->j_checkpoint_transactions)
204                 goto out;
205         transaction = journal->j_checkpoint_transactions;
206         if (transaction->t_chp_stats.cs_chp_time == 0)
207                 transaction->t_chp_stats.cs_chp_time = jiffies;
208         this_tid = transaction->t_tid;
209 restart:
210         /*
211          * If someone cleaned up this transaction while we slept, we're
212          * done (maybe it's a new transaction, but it fell at the same
213          * address).
214          */
215         if (journal->j_checkpoint_transactions != transaction ||
216             transaction->t_tid != this_tid)
217                 goto out;
218
219         /* checkpoint all of the transaction's buffers */
220         while (transaction->t_checkpoint_list) {
221                 jh = transaction->t_checkpoint_list;
222                 bh = jh2bh(jh);
223
224                 /*
225                  * The buffer may be writing back, or flushing out in the
226                  * last couple of cycles, or re-adding into a new transaction,
227                  * need to check it again until it's unlocked.
228                  */
229                 if (buffer_locked(bh)) {
230                         get_bh(bh);
231                         spin_unlock(&journal->j_list_lock);
232                         wait_on_buffer(bh);
233                         /* the journal_head may have gone by now */
234                         BUFFER_TRACE(bh, "brelse");
235                         __brelse(bh);
236                         goto retry;
237                 }
238                 if (jh->b_transaction != NULL) {
239                         transaction_t *t = jh->b_transaction;
240                         tid_t tid = t->t_tid;
241
242                         transaction->t_chp_stats.cs_forced_to_close++;
243                         spin_unlock(&journal->j_list_lock);
244                         if (unlikely(journal->j_flags & JBD2_UNMOUNT))
245                                 /*
246                                  * The journal thread is dead; so
247                                  * starting and waiting for a commit
248                                  * to finish will cause us to wait for
249                                  * a _very_ long time.
250                                  */
251                                 printk(KERN_ERR
252                 "JBD2: %s: Waiting for Godot: block %llu\n",
253                 journal->j_devname, (unsigned long long) bh->b_blocknr);
254
255                         if (batch_count)
256                                 __flush_batch(journal, &batch_count);
257                         jbd2_log_start_commit(journal, tid);
258                         /*
259                          * jbd2_journal_commit_transaction() may want
260                          * to take the checkpoint_mutex if JBD2_FLUSHED
261                          * is set, jbd2_update_log_tail() called by
262                          * jbd2_journal_commit_transaction() may also take
263                          * checkpoint_mutex.  So we need to temporarily
264                          * drop it.
265                          */
266                         mutex_unlock(&journal->j_checkpoint_mutex);
267                         jbd2_log_wait_commit(journal, tid);
268                         mutex_lock_io(&journal->j_checkpoint_mutex);
269                         spin_lock(&journal->j_list_lock);
270                         goto restart;
271                 }
272                 if (!buffer_dirty(bh)) {
273                         BUFFER_TRACE(bh, "remove from checkpoint");
274                         /*
275                          * If the transaction was released or the checkpoint
276                          * list was empty, we're done.
277                          */
278                         if (__jbd2_journal_remove_checkpoint(jh) ||
279                             !transaction->t_checkpoint_list)
280                                 goto out;
281                 } else {
282                         /*
283                          * We are about to write the buffer, it could be
284                          * raced by some other transaction shrink or buffer
285                          * re-log logic once we release the j_list_lock,
286                          * leave it on the checkpoint list and check status
287                          * again to make sure it's clean.
288                          */
289                         BUFFER_TRACE(bh, "queue");
290                         get_bh(bh);
291                         J_ASSERT_BH(bh, !buffer_jwrite(bh));
292                         journal->j_chkpt_bhs[batch_count++] = bh;
293                         transaction->t_chp_stats.cs_written++;
294                         transaction->t_checkpoint_list = jh->b_cpnext;
295                 }
296
297                 if ((batch_count == JBD2_NR_BATCH) ||
298                     need_resched() || spin_needbreak(&journal->j_list_lock) ||
299                     jh2bh(transaction->t_checkpoint_list) == journal->j_chkpt_bhs[0])
300                         goto unlock_and_flush;
301         }
302
303         if (batch_count) {
304                 unlock_and_flush:
305                         spin_unlock(&journal->j_list_lock);
306                 retry:
307                         if (batch_count)
308                                 __flush_batch(journal, &batch_count);
309                         spin_lock(&journal->j_list_lock);
310                         goto restart;
311         }
312
313 out:
314         spin_unlock(&journal->j_list_lock);
315         result = jbd2_cleanup_journal_tail(journal);
316
317         return (result < 0) ? result : 0;
318 }
319
320 /*
321  * Check the list of checkpoint transactions for the journal to see if
322  * we have already got rid of any since the last update of the log tail
323  * in the journal superblock.  If so, we can instantly roll the
324  * superblock forward to remove those transactions from the log.
325  *
326  * Return <0 on error, 0 on success, 1 if there was nothing to clean up.
327  *
328  * Called with the journal lock held.
329  *
330  * This is the only part of the journaling code which really needs to be
331  * aware of transaction aborts.  Checkpointing involves writing to the
332  * main filesystem area rather than to the journal, so it can proceed
333  * even in abort state, but we must not update the super block if
334  * checkpointing may have failed.  Otherwise, we would lose some metadata
335  * buffers which should be written-back to the filesystem.
336  */
337
338 int jbd2_cleanup_journal_tail(journal_t *journal)
339 {
340         tid_t           first_tid;
341         unsigned long   blocknr;
342
343         if (is_journal_aborted(journal))
344                 return -EIO;
345
346         if (!jbd2_journal_get_log_tail(journal, &first_tid, &blocknr))
347                 return 1;
348         J_ASSERT(blocknr != 0);
349
350         /*
351          * We need to make sure that any blocks that were recently written out
352          * --- perhaps by jbd2_log_do_checkpoint() --- are flushed out before
353          * we drop the transactions from the journal. It's unlikely this will
354          * be necessary, especially with an appropriately sized journal, but we
355          * need this to guarantee correctness.  Fortunately
356          * jbd2_cleanup_journal_tail() doesn't get called all that often.
357          */
358         if (journal->j_flags & JBD2_BARRIER)
359                 blkdev_issue_flush(journal->j_fs_dev);
360
361         return __jbd2_update_log_tail(journal, first_tid, blocknr);
362 }
363
364
365 /* Checkpoint list management */
366
367 /*
368  * journal_clean_one_cp_list
369  *
370  * Find all the written-back checkpoint buffers in the given list and
371  * release them. If 'destroy' is set, clean all buffers unconditionally.
372  *
373  * Called with j_list_lock held.
374  * Returns 1 if we freed the transaction, 0 otherwise.
375  */
376 static int journal_clean_one_cp_list(struct journal_head *jh, bool destroy)
377 {
378         struct journal_head *last_jh;
379         struct journal_head *next_jh = jh;
380
381         if (!jh)
382                 return 0;
383
384         last_jh = jh->b_cpprev;
385         do {
386                 jh = next_jh;
387                 next_jh = jh->b_cpnext;
388
389                 if (!destroy && __cp_buffer_busy(jh))
390                         return 0;
391
392                 if (__jbd2_journal_remove_checkpoint(jh))
393                         return 1;
394                 /*
395                  * This function only frees up some memory
396                  * if possible so we dont have an obligation
397                  * to finish processing. Bail out if preemption
398                  * requested:
399                  */
400                 if (need_resched())
401                         return 0;
402         } while (jh != last_jh);
403
404         return 0;
405 }
406
407 /*
408  * journal_shrink_one_cp_list
409  *
410  * Find 'nr_to_scan' written-back checkpoint buffers in the given list
411  * and try to release them. If the whole transaction is released, set
412  * the 'released' parameter. Return the number of released checkpointed
413  * buffers.
414  *
415  * Called with j_list_lock held.
416  */
417 static unsigned long journal_shrink_one_cp_list(struct journal_head *jh,
418                                                 unsigned long *nr_to_scan,
419                                                 bool *released)
420 {
421         struct journal_head *last_jh;
422         struct journal_head *next_jh = jh;
423         unsigned long nr_freed = 0;
424         int ret;
425
426         if (!jh || *nr_to_scan == 0)
427                 return 0;
428
429         last_jh = jh->b_cpprev;
430         do {
431                 jh = next_jh;
432                 next_jh = jh->b_cpnext;
433
434                 (*nr_to_scan)--;
435                 if (__cp_buffer_busy(jh))
436                         continue;
437
438                 nr_freed++;
439                 ret = __jbd2_journal_remove_checkpoint(jh);
440                 if (ret) {
441                         *released = true;
442                         break;
443                 }
444
445                 if (need_resched())
446                         break;
447         } while (jh != last_jh && *nr_to_scan);
448
449         return nr_freed;
450 }
451
452 /*
453  * jbd2_journal_shrink_checkpoint_list
454  *
455  * Find 'nr_to_scan' written-back checkpoint buffers in the journal
456  * and try to release them. Return the number of released checkpointed
457  * buffers.
458  *
459  * Called with j_list_lock held.
460  */
461 unsigned long jbd2_journal_shrink_checkpoint_list(journal_t *journal,
462                                                   unsigned long *nr_to_scan)
463 {
464         transaction_t *transaction, *last_transaction, *next_transaction;
465         bool released;
466         tid_t first_tid = 0, last_tid = 0, next_tid = 0;
467         tid_t tid = 0;
468         unsigned long nr_freed = 0;
469         unsigned long nr_scanned = *nr_to_scan;
470
471 again:
472         spin_lock(&journal->j_list_lock);
473         if (!journal->j_checkpoint_transactions) {
474                 spin_unlock(&journal->j_list_lock);
475                 goto out;
476         }
477
478         /*
479          * Get next shrink transaction, resume previous scan or start
480          * over again. If some others do checkpoint and drop transaction
481          * from the checkpoint list, we ignore saved j_shrink_transaction
482          * and start over unconditionally.
483          */
484         if (journal->j_shrink_transaction)
485                 transaction = journal->j_shrink_transaction;
486         else
487                 transaction = journal->j_checkpoint_transactions;
488
489         if (!first_tid)
490                 first_tid = transaction->t_tid;
491         last_transaction = journal->j_checkpoint_transactions->t_cpprev;
492         next_transaction = transaction;
493         last_tid = last_transaction->t_tid;
494         do {
495                 transaction = next_transaction;
496                 next_transaction = transaction->t_cpnext;
497                 tid = transaction->t_tid;
498                 released = false;
499
500                 nr_freed += journal_shrink_one_cp_list(transaction->t_checkpoint_list,
501                                                        nr_to_scan, &released);
502                 if (*nr_to_scan == 0)
503                         break;
504                 if (need_resched() || spin_needbreak(&journal->j_list_lock))
505                         break;
506                 if (released)
507                         continue;
508
509                 nr_freed += journal_shrink_one_cp_list(transaction->t_checkpoint_io_list,
510                                                        nr_to_scan, &released);
511                 if (*nr_to_scan == 0)
512                         break;
513                 if (need_resched() || spin_needbreak(&journal->j_list_lock))
514                         break;
515         } while (transaction != last_transaction);
516
517         if (transaction != last_transaction) {
518                 journal->j_shrink_transaction = next_transaction;
519                 next_tid = next_transaction->t_tid;
520         } else {
521                 journal->j_shrink_transaction = NULL;
522                 next_tid = 0;
523         }
524
525         spin_unlock(&journal->j_list_lock);
526         cond_resched();
527
528         if (*nr_to_scan && next_tid)
529                 goto again;
530 out:
531         nr_scanned -= *nr_to_scan;
532         trace_jbd2_shrink_checkpoint_list(journal, first_tid, tid, last_tid,
533                                           nr_freed, nr_scanned, next_tid);
534
535         return nr_freed;
536 }
537
538 /*
539  * journal_clean_checkpoint_list
540  *
541  * Find all the written-back checkpoint buffers in the journal and release them.
542  * If 'destroy' is set, release all buffers unconditionally.
543  *
544  * Called with j_list_lock held.
545  */
546 void __jbd2_journal_clean_checkpoint_list(journal_t *journal, bool destroy)
547 {
548         transaction_t *transaction, *last_transaction, *next_transaction;
549         int ret;
550
551         transaction = journal->j_checkpoint_transactions;
552         if (!transaction)
553                 return;
554
555         last_transaction = transaction->t_cpprev;
556         next_transaction = transaction;
557         do {
558                 transaction = next_transaction;
559                 next_transaction = transaction->t_cpnext;
560                 ret = journal_clean_one_cp_list(transaction->t_checkpoint_list,
561                                                 destroy);
562                 /*
563                  * This function only frees up some memory if possible so we
564                  * dont have an obligation to finish processing. Bail out if
565                  * preemption requested:
566                  */
567                 if (need_resched())
568                         return;
569                 if (ret)
570                         continue;
571                 /*
572                  * It is essential that we are as careful as in the case of
573                  * t_checkpoint_list with removing the buffer from the list as
574                  * we can possibly see not yet submitted buffers on io_list
575                  */
576                 ret = journal_clean_one_cp_list(transaction->
577                                 t_checkpoint_io_list, destroy);
578                 if (need_resched())
579                         return;
580                 /*
581                  * Stop scanning if we couldn't free the transaction. This
582                  * avoids pointless scanning of transactions which still
583                  * weren't checkpointed.
584                  */
585                 if (!ret)
586                         return;
587         } while (transaction != last_transaction);
588 }
589
590 /*
591  * Remove buffers from all checkpoint lists as journal is aborted and we just
592  * need to free memory
593  */
594 void jbd2_journal_destroy_checkpoint(journal_t *journal)
595 {
596         /*
597          * We loop because __jbd2_journal_clean_checkpoint_list() may abort
598          * early due to a need of rescheduling.
599          */
600         while (1) {
601                 spin_lock(&journal->j_list_lock);
602                 if (!journal->j_checkpoint_transactions) {
603                         spin_unlock(&journal->j_list_lock);
604                         break;
605                 }
606                 __jbd2_journal_clean_checkpoint_list(journal, true);
607                 spin_unlock(&journal->j_list_lock);
608                 cond_resched();
609         }
610 }
611
612 /*
613  * journal_remove_checkpoint: called after a buffer has been committed
614  * to disk (either by being write-back flushed to disk, or being
615  * committed to the log).
616  *
617  * We cannot safely clean a transaction out of the log until all of the
618  * buffer updates committed in that transaction have safely been stored
619  * elsewhere on disk.  To achieve this, all of the buffers in a
620  * transaction need to be maintained on the transaction's checkpoint
621  * lists until they have been rewritten, at which point this function is
622  * called to remove the buffer from the existing transaction's
623  * checkpoint lists.
624  *
625  * The function returns 1 if it frees the transaction, 0 otherwise.
626  * The function can free jh and bh.
627  *
628  * This function is called with j_list_lock held.
629  */
630 int __jbd2_journal_remove_checkpoint(struct journal_head *jh)
631 {
632         struct transaction_chp_stats_s *stats;
633         transaction_t *transaction;
634         journal_t *journal;
635         struct buffer_head *bh = jh2bh(jh);
636
637         JBUFFER_TRACE(jh, "entry");
638
639         transaction = jh->b_cp_transaction;
640         if (!transaction) {
641                 JBUFFER_TRACE(jh, "not on transaction");
642                 return 0;
643         }
644         journal = transaction->t_journal;
645
646         JBUFFER_TRACE(jh, "removing from transaction");
647
648         /*
649          * If we have failed to write the buffer out to disk, the filesystem
650          * may become inconsistent. We cannot abort the journal here since
651          * we hold j_list_lock and we have to be careful about races with
652          * jbd2_journal_destroy(). So mark the writeback IO error in the
653          * journal here and we abort the journal later from a better context.
654          */
655         if (buffer_write_io_error(bh))
656                 set_bit(JBD2_CHECKPOINT_IO_ERROR, &journal->j_atomic_flags);
657
658         __buffer_unlink(jh);
659         jh->b_cp_transaction = NULL;
660         percpu_counter_dec(&journal->j_checkpoint_jh_count);
661         jbd2_journal_put_journal_head(jh);
662
663         /* Is this transaction empty? */
664         if (transaction->t_checkpoint_list || transaction->t_checkpoint_io_list)
665                 return 0;
666
667         /*
668          * There is one special case to worry about: if we have just pulled the
669          * buffer off a running or committing transaction's checkpoing list,
670          * then even if the checkpoint list is empty, the transaction obviously
671          * cannot be dropped!
672          *
673          * The locking here around t_state is a bit sleazy.
674          * See the comment at the end of jbd2_journal_commit_transaction().
675          */
676         if (transaction->t_state != T_FINISHED)
677                 return 0;
678
679         /*
680          * OK, that was the last buffer for the transaction, we can now
681          * safely remove this transaction from the log.
682          */
683         stats = &transaction->t_chp_stats;
684         if (stats->cs_chp_time)
685                 stats->cs_chp_time = jbd2_time_diff(stats->cs_chp_time,
686                                                     jiffies);
687         trace_jbd2_checkpoint_stats(journal->j_fs_dev->bd_dev,
688                                     transaction->t_tid, stats);
689
690         __jbd2_journal_drop_transaction(journal, transaction);
691         jbd2_journal_free_transaction(transaction);
692         return 1;
693 }
694
695 /*
696  * journal_insert_checkpoint: put a committed buffer onto a checkpoint
697  * list so that we know when it is safe to clean the transaction out of
698  * the log.
699  *
700  * Called with the journal locked.
701  * Called with j_list_lock held.
702  */
703 void __jbd2_journal_insert_checkpoint(struct journal_head *jh,
704                                transaction_t *transaction)
705 {
706         JBUFFER_TRACE(jh, "entry");
707         J_ASSERT_JH(jh, buffer_dirty(jh2bh(jh)) || buffer_jbddirty(jh2bh(jh)));
708         J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
709
710         /* Get reference for checkpointing transaction */
711         jbd2_journal_grab_journal_head(jh2bh(jh));
712         jh->b_cp_transaction = transaction;
713
714         if (!transaction->t_checkpoint_list) {
715                 jh->b_cpnext = jh->b_cpprev = jh;
716         } else {
717                 jh->b_cpnext = transaction->t_checkpoint_list;
718                 jh->b_cpprev = transaction->t_checkpoint_list->b_cpprev;
719                 jh->b_cpprev->b_cpnext = jh;
720                 jh->b_cpnext->b_cpprev = jh;
721         }
722         transaction->t_checkpoint_list = jh;
723         percpu_counter_inc(&transaction->t_journal->j_checkpoint_jh_count);
724 }
725
726 /*
727  * We've finished with this transaction structure: adios...
728  *
729  * The transaction must have no links except for the checkpoint by this
730  * point.
731  *
732  * Called with the journal locked.
733  * Called with j_list_lock held.
734  */
735
736 void __jbd2_journal_drop_transaction(journal_t *journal, transaction_t *transaction)
737 {
738         assert_spin_locked(&journal->j_list_lock);
739
740         journal->j_shrink_transaction = NULL;
741         if (transaction->t_cpnext) {
742                 transaction->t_cpnext->t_cpprev = transaction->t_cpprev;
743                 transaction->t_cpprev->t_cpnext = transaction->t_cpnext;
744                 if (journal->j_checkpoint_transactions == transaction)
745                         journal->j_checkpoint_transactions =
746                                 transaction->t_cpnext;
747                 if (journal->j_checkpoint_transactions == transaction)
748                         journal->j_checkpoint_transactions = NULL;
749         }
750
751         J_ASSERT(transaction->t_state == T_FINISHED);
752         J_ASSERT(transaction->t_buffers == NULL);
753         J_ASSERT(transaction->t_forget == NULL);
754         J_ASSERT(transaction->t_shadow_list == NULL);
755         J_ASSERT(transaction->t_checkpoint_list == NULL);
756         J_ASSERT(transaction->t_checkpoint_io_list == NULL);
757         J_ASSERT(atomic_read(&transaction->t_updates) == 0);
758         J_ASSERT(journal->j_committing_transaction != transaction);
759         J_ASSERT(journal->j_running_transaction != transaction);
760
761         trace_jbd2_drop_transaction(journal, transaction);
762
763         jbd2_debug(1, "Dropping transaction %d, all done\n", transaction->t_tid);
764 }