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