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