VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

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