xref: /openbmc/linux/fs/xfs/xfs_log_cil.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
271e330b5SDave Chinner /*
371e330b5SDave Chinner  * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved.
471e330b5SDave Chinner  */
571e330b5SDave Chinner 
671e330b5SDave Chinner #include "xfs.h"
771e330b5SDave Chinner #include "xfs_fs.h"
84fb6e8adSChristoph Hellwig #include "xfs_format.h"
9239880efSDave Chinner #include "xfs_log_format.h"
1070a9883cSDave Chinner #include "xfs_shared.h"
11239880efSDave Chinner #include "xfs_trans_resv.h"
1271e330b5SDave Chinner #include "xfs_mount.h"
13efc27b52SDave Chinner #include "xfs_extent_busy.h"
14239880efSDave Chinner #include "xfs_trans.h"
15239880efSDave Chinner #include "xfs_trans_priv.h"
16239880efSDave Chinner #include "xfs_log.h"
17239880efSDave Chinner #include "xfs_log_priv.h"
184560e78fSChristoph Hellwig #include "xfs_trace.h"
19428c4435SDave Chinner #include "xfs_discard.h"
2071e330b5SDave Chinner 
2171e330b5SDave Chinner /*
2271e330b5SDave Chinner  * Allocate a new ticket. Failing to get a new ticket makes it really hard to
2371e330b5SDave Chinner  * recover, so we don't allow failure here. Also, we allocate in a context that
2471e330b5SDave Chinner  * we don't want to be issuing transactions from, so we need to tell the
2571e330b5SDave Chinner  * allocation code this as well.
2671e330b5SDave Chinner  *
2771e330b5SDave Chinner  * We don't reserve any space for the ticket - we are going to steal whatever
2871e330b5SDave Chinner  * space we require from transactions as they commit. To ensure we reserve all
2971e330b5SDave Chinner  * the space required, we need to set the current reservation of the ticket to
3071e330b5SDave Chinner  * zero so that we know to steal the initial transaction overhead from the
3171e330b5SDave Chinner  * first transaction commit.
3271e330b5SDave Chinner  */
3371e330b5SDave Chinner static struct xlog_ticket *
xlog_cil_ticket_alloc(struct xlog * log)3471e330b5SDave Chinner xlog_cil_ticket_alloc(
35f7bdf03aSMark Tinguely 	struct xlog	*log)
3671e330b5SDave Chinner {
3771e330b5SDave Chinner 	struct xlog_ticket *tic;
3871e330b5SDave Chinner 
39c7610dceSDave Chinner 	tic = xlog_ticket_alloc(log, 0, 1, 0);
4071e330b5SDave Chinner 
4171e330b5SDave Chinner 	/*
4271e330b5SDave Chinner 	 * set the current reservation to zero so we know to steal the basic
4371e330b5SDave Chinner 	 * transaction overhead reservation from the first transaction commit.
4471e330b5SDave Chinner 	 */
4571e330b5SDave Chinner 	tic->t_curr_res = 0;
4631151cc3SDave Chinner 	tic->t_iclog_hdrs = 0;
4771e330b5SDave Chinner 	return tic;
4871e330b5SDave Chinner }
4971e330b5SDave Chinner 
5031151cc3SDave Chinner static inline void
xlog_cil_set_iclog_hdr_count(struct xfs_cil * cil)5131151cc3SDave Chinner xlog_cil_set_iclog_hdr_count(struct xfs_cil *cil)
5231151cc3SDave Chinner {
5331151cc3SDave Chinner 	struct xlog	*log = cil->xc_log;
5431151cc3SDave Chinner 
5531151cc3SDave Chinner 	atomic_set(&cil->xc_iclog_hdrs,
5631151cc3SDave Chinner 		   (XLOG_CIL_BLOCKING_SPACE_LIMIT(log) /
5731151cc3SDave Chinner 			(log->l_iclog_size - log->l_iclog_hsize)));
5831151cc3SDave Chinner }
5931151cc3SDave Chinner 
6071e330b5SDave Chinner /*
6122b1afc5SDave Chinner  * Check if the current log item was first committed in this sequence.
6222b1afc5SDave Chinner  * We can't rely on just the log item being in the CIL, we have to check
6322b1afc5SDave Chinner  * the recorded commit sequence number.
6422b1afc5SDave Chinner  *
6522b1afc5SDave Chinner  * Note: for this to be used in a non-racy manner, it has to be called with
6622b1afc5SDave Chinner  * CIL flushing locked out. As a result, it should only be used during the
6722b1afc5SDave Chinner  * transaction commit process when deciding what to format into the item.
6822b1afc5SDave Chinner  */
6922b1afc5SDave Chinner static bool
xlog_item_in_current_chkpt(struct xfs_cil * cil,struct xfs_log_item * lip)7022b1afc5SDave Chinner xlog_item_in_current_chkpt(
7122b1afc5SDave Chinner 	struct xfs_cil		*cil,
7222b1afc5SDave Chinner 	struct xfs_log_item	*lip)
7322b1afc5SDave Chinner {
7488591e7fSDave Chinner 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
7522b1afc5SDave Chinner 		return false;
7622b1afc5SDave Chinner 
7722b1afc5SDave Chinner 	/*
7822b1afc5SDave Chinner 	 * li_seq is written on the first commit of a log item to record the
7922b1afc5SDave Chinner 	 * first checkpoint it is written to. Hence if it is different to the
8022b1afc5SDave Chinner 	 * current sequence, we're in a new checkpoint.
8122b1afc5SDave Chinner 	 */
8222b1afc5SDave Chinner 	return lip->li_seq == READ_ONCE(cil->xc_current_sequence);
8322b1afc5SDave Chinner }
8422b1afc5SDave Chinner 
8522b1afc5SDave Chinner bool
xfs_log_item_in_current_chkpt(struct xfs_log_item * lip)8622b1afc5SDave Chinner xfs_log_item_in_current_chkpt(
8722b1afc5SDave Chinner 	struct xfs_log_item *lip)
8822b1afc5SDave Chinner {
8922b1afc5SDave Chinner 	return xlog_item_in_current_chkpt(lip->li_log->l_cilp, lip);
9022b1afc5SDave Chinner }
9122b1afc5SDave Chinner 
9222b1afc5SDave Chinner /*
9339823d0fSDave Chinner  * Unavoidable forward declaration - xlog_cil_push_work() calls
9439823d0fSDave Chinner  * xlog_cil_ctx_alloc() itself.
9539823d0fSDave Chinner  */
9639823d0fSDave Chinner static void xlog_cil_push_work(struct work_struct *work);
9739823d0fSDave Chinner 
9839823d0fSDave Chinner static struct xfs_cil_ctx *
xlog_cil_ctx_alloc(void)9939823d0fSDave Chinner xlog_cil_ctx_alloc(void)
10039823d0fSDave Chinner {
10139823d0fSDave Chinner 	struct xfs_cil_ctx	*ctx;
10239823d0fSDave Chinner 
10339823d0fSDave Chinner 	ctx = kmem_zalloc(sizeof(*ctx), KM_NOFS);
10439823d0fSDave Chinner 	INIT_LIST_HEAD(&ctx->committing);
105428c4435SDave Chinner 	INIT_LIST_HEAD(&ctx->busy_extents.extent_list);
106c0fb4765SDave Chinner 	INIT_LIST_HEAD(&ctx->log_items);
10716924853SDave Chinner 	INIT_LIST_HEAD(&ctx->lv_chain);
10839823d0fSDave Chinner 	INIT_WORK(&ctx->push_work, xlog_cil_push_work);
10939823d0fSDave Chinner 	return ctx;
11039823d0fSDave Chinner }
11139823d0fSDave Chinner 
1127c8ade21SDave Chinner /*
1137c8ade21SDave Chinner  * Aggregate the CIL per cpu structures into global counts, lists, etc and
1147c8ade21SDave Chinner  * clear the percpu state ready for the next context to use. This is called
1157c8ade21SDave Chinner  * from the push code with the context lock held exclusively, hence nothing else
1167c8ade21SDave Chinner  * will be accessing or modifying the per-cpu counters.
1177c8ade21SDave Chinner  */
1187c8ade21SDave Chinner static void
xlog_cil_push_pcp_aggregate(struct xfs_cil * cil,struct xfs_cil_ctx * ctx)1197c8ade21SDave Chinner xlog_cil_push_pcp_aggregate(
1207c8ade21SDave Chinner 	struct xfs_cil		*cil,
1217c8ade21SDave Chinner 	struct xfs_cil_ctx	*ctx)
1227c8ade21SDave Chinner {
1237c8ade21SDave Chinner 	struct xlog_cil_pcp	*cilpcp;
1247c8ade21SDave Chinner 	int			cpu;
1257c8ade21SDave Chinner 
126ecd49f7aSDarrick J. Wong 	for_each_cpu(cpu, &ctx->cil_pcpmask) {
1277c8ade21SDave Chinner 		cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
1287c8ade21SDave Chinner 
1291dd2a2c1SDave Chinner 		ctx->ticket->t_curr_res += cilpcp->space_reserved;
1301dd2a2c1SDave Chinner 		cilpcp->space_reserved = 0;
1311dd2a2c1SDave Chinner 
132df7a4a21SDave Chinner 		if (!list_empty(&cilpcp->busy_extents)) {
133df7a4a21SDave Chinner 			list_splice_init(&cilpcp->busy_extents,
134428c4435SDave Chinner 					&ctx->busy_extents.extent_list);
135df7a4a21SDave Chinner 		}
136c0fb4765SDave Chinner 		if (!list_empty(&cilpcp->log_items))
137c0fb4765SDave Chinner 			list_splice_init(&cilpcp->log_items, &ctx->log_items);
138df7a4a21SDave Chinner 
1397c8ade21SDave Chinner 		/*
1407c8ade21SDave Chinner 		 * We're in the middle of switching cil contexts.  Reset the
1417c8ade21SDave Chinner 		 * counter we use to detect when the current context is nearing
1427c8ade21SDave Chinner 		 * full.
1437c8ade21SDave Chinner 		 */
1447c8ade21SDave Chinner 		cilpcp->space_used = 0;
1457c8ade21SDave Chinner 	}
1467c8ade21SDave Chinner }
1477c8ade21SDave Chinner 
1487c8ade21SDave Chinner /*
1497c8ade21SDave Chinner  * Aggregate the CIL per-cpu space used counters into the global atomic value.
1507c8ade21SDave Chinner  * This is called when the per-cpu counter aggregation will first pass the soft
1517c8ade21SDave Chinner  * limit threshold so we can switch to atomic counter aggregation for accurate
1527c8ade21SDave Chinner  * detection of hard limit traversal.
1537c8ade21SDave Chinner  */
1547c8ade21SDave Chinner static void
xlog_cil_insert_pcp_aggregate(struct xfs_cil * cil,struct xfs_cil_ctx * ctx)1557c8ade21SDave Chinner xlog_cil_insert_pcp_aggregate(
1567c8ade21SDave Chinner 	struct xfs_cil		*cil,
1577c8ade21SDave Chinner 	struct xfs_cil_ctx	*ctx)
1587c8ade21SDave Chinner {
1597c8ade21SDave Chinner 	int			cpu;
1607c8ade21SDave Chinner 	int			count = 0;
1617c8ade21SDave Chinner 
1627c8ade21SDave Chinner 	/* Trigger atomic updates then aggregate only for the first caller */
1637c8ade21SDave Chinner 	if (!test_and_clear_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags))
1647c8ade21SDave Chinner 		return;
1657c8ade21SDave Chinner 
166ecd49f7aSDarrick J. Wong 	/*
167ecd49f7aSDarrick J. Wong 	 * We can race with other cpus setting cil_pcpmask.  However, we've
168ecd49f7aSDarrick J. Wong 	 * atomically cleared PCP_SPACE which forces other threads to add to
169ecd49f7aSDarrick J. Wong 	 * the global space used count.  cil_pcpmask is a superset of cilpcp
170ecd49f7aSDarrick J. Wong 	 * structures that could have a nonzero space_used.
171ecd49f7aSDarrick J. Wong 	 */
172ecd49f7aSDarrick J. Wong 	for_each_cpu(cpu, &ctx->cil_pcpmask) {
173*b5d917a6SUros Bizjak 		struct xlog_cil_pcp	*cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
174*b5d917a6SUros Bizjak 		int			old = READ_ONCE(cilpcp->space_used);
1757c8ade21SDave Chinner 
176*b5d917a6SUros Bizjak 		while (!try_cmpxchg(&cilpcp->space_used, &old, 0))
177*b5d917a6SUros Bizjak 			;
1787c8ade21SDave Chinner 		count += old;
1797c8ade21SDave Chinner 	}
1807c8ade21SDave Chinner 	atomic_add(count, &ctx->space_used);
1817c8ade21SDave Chinner }
1827c8ade21SDave Chinner 
18339823d0fSDave Chinner static void
xlog_cil_ctx_switch(struct xfs_cil * cil,struct xfs_cil_ctx * ctx)18439823d0fSDave Chinner xlog_cil_ctx_switch(
18539823d0fSDave Chinner 	struct xfs_cil		*cil,
18639823d0fSDave Chinner 	struct xfs_cil_ctx	*ctx)
18739823d0fSDave Chinner {
18831151cc3SDave Chinner 	xlog_cil_set_iclog_hdr_count(cil);
18988591e7fSDave Chinner 	set_bit(XLOG_CIL_EMPTY, &cil->xc_flags);
1907c8ade21SDave Chinner 	set_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags);
19139823d0fSDave Chinner 	ctx->sequence = ++cil->xc_current_sequence;
19239823d0fSDave Chinner 	ctx->cil = cil;
19339823d0fSDave Chinner 	cil->xc_ctx = ctx;
19439823d0fSDave Chinner }
19539823d0fSDave Chinner 
19639823d0fSDave Chinner /*
19771e330b5SDave Chinner  * After the first stage of log recovery is done, we know where the head and
19871e330b5SDave Chinner  * tail of the log are. We need this log initialisation done before we can
19971e330b5SDave Chinner  * initialise the first CIL checkpoint context.
20071e330b5SDave Chinner  *
20171e330b5SDave Chinner  * Here we allocate a log ticket to track space usage during a CIL push.  This
20271e330b5SDave Chinner  * ticket is passed to xlog_write() directly so that we don't slowly leak log
20371e330b5SDave Chinner  * space by failing to account for space used by log headers and additional
20471e330b5SDave Chinner  * region headers for split regions.
20571e330b5SDave Chinner  */
20671e330b5SDave Chinner void
xlog_cil_init_post_recovery(struct xlog * log)20771e330b5SDave Chinner xlog_cil_init_post_recovery(
208f7bdf03aSMark Tinguely 	struct xlog	*log)
20971e330b5SDave Chinner {
21071e330b5SDave Chinner 	log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
21171e330b5SDave Chinner 	log->l_cilp->xc_ctx->sequence = 1;
21231151cc3SDave Chinner 	xlog_cil_set_iclog_hdr_count(log->l_cilp);
21371e330b5SDave Chinner }
21471e330b5SDave Chinner 
215b1c5ebb2SDave Chinner static inline int
xlog_cil_iovec_space(uint niovecs)216b1c5ebb2SDave Chinner xlog_cil_iovec_space(
217b1c5ebb2SDave Chinner 	uint	niovecs)
218b1c5ebb2SDave Chinner {
219b1c5ebb2SDave Chinner 	return round_up((sizeof(struct xfs_log_vec) +
220b1c5ebb2SDave Chinner 					niovecs * sizeof(struct xfs_log_iovec)),
221b1c5ebb2SDave Chinner 			sizeof(uint64_t));
222b1c5ebb2SDave Chinner }
223b1c5ebb2SDave Chinner 
224b1c5ebb2SDave Chinner /*
225b1c5ebb2SDave Chinner  * Allocate or pin log vector buffers for CIL insertion.
226b1c5ebb2SDave Chinner  *
227b1c5ebb2SDave Chinner  * The CIL currently uses disposable buffers for copying a snapshot of the
228b1c5ebb2SDave Chinner  * modified items into the log during a push. The biggest problem with this is
229b1c5ebb2SDave Chinner  * the requirement to allocate the disposable buffer during the commit if:
230b1c5ebb2SDave Chinner  *	a) does not exist; or
231b1c5ebb2SDave Chinner  *	b) it is too small
232b1c5ebb2SDave Chinner  *
233b1c5ebb2SDave Chinner  * If we do this allocation within xlog_cil_insert_format_items(), it is done
234b1c5ebb2SDave Chinner  * under the xc_ctx_lock, which means that a CIL push cannot occur during
235b1c5ebb2SDave Chinner  * the memory allocation. This means that we have a potential deadlock situation
236b1c5ebb2SDave Chinner  * under low memory conditions when we have lots of dirty metadata pinned in
237b1c5ebb2SDave Chinner  * the CIL and we need a CIL commit to occur to free memory.
238b1c5ebb2SDave Chinner  *
239b1c5ebb2SDave Chinner  * To avoid this, we need to move the memory allocation outside the
240b1c5ebb2SDave Chinner  * xc_ctx_lock, but because the log vector buffers are disposable, that opens
241b1c5ebb2SDave Chinner  * up a TOCTOU race condition w.r.t. the CIL committing and removing the log
242b1c5ebb2SDave Chinner  * vector buffers between the check and the formatting of the item into the
243b1c5ebb2SDave Chinner  * log vector buffer within the xc_ctx_lock.
244b1c5ebb2SDave Chinner  *
245b1c5ebb2SDave Chinner  * Because the log vector buffer needs to be unchanged during the CIL push
246b1c5ebb2SDave Chinner  * process, we cannot share the buffer between the transaction commit (which
247b1c5ebb2SDave Chinner  * modifies the buffer) and the CIL push context that is writing the changes
248b1c5ebb2SDave Chinner  * into the log. This means skipping preallocation of buffer space is
249b1c5ebb2SDave Chinner  * unreliable, but we most definitely do not want to be allocating and freeing
250b1c5ebb2SDave Chinner  * buffers unnecessarily during commits when overwrites can be done safely.
251b1c5ebb2SDave Chinner  *
252b1c5ebb2SDave Chinner  * The simplest solution to this problem is to allocate a shadow buffer when a
253b1c5ebb2SDave Chinner  * log item is committed for the second time, and then to only use this buffer
254b1c5ebb2SDave Chinner  * if necessary. The buffer can remain attached to the log item until such time
255b1c5ebb2SDave Chinner  * it is needed, and this is the buffer that is reallocated to match the size of
256b1c5ebb2SDave Chinner  * the incoming modification. Then during the formatting of the item we can swap
257b1c5ebb2SDave Chinner  * the active buffer with the new one if we can't reuse the existing buffer. We
258b1c5ebb2SDave Chinner  * don't free the old buffer as it may be reused on the next modification if
259b1c5ebb2SDave Chinner  * it's size is right, otherwise we'll free and reallocate it at that point.
260b1c5ebb2SDave Chinner  *
261b1c5ebb2SDave Chinner  * This function builds a vector for the changes in each log item in the
262b1c5ebb2SDave Chinner  * transaction. It then works out the length of the buffer needed for each log
263b1c5ebb2SDave Chinner  * item, allocates them and attaches the vector to the log item in preparation
264b1c5ebb2SDave Chinner  * for the formatting step which occurs under the xc_ctx_lock.
265b1c5ebb2SDave Chinner  *
266b1c5ebb2SDave Chinner  * While this means the memory footprint goes up, it avoids the repeated
267b1c5ebb2SDave Chinner  * alloc/free pattern that repeated modifications of an item would otherwise
268b1c5ebb2SDave Chinner  * cause, and hence minimises the CPU overhead of such behaviour.
269b1c5ebb2SDave Chinner  */
270b1c5ebb2SDave Chinner static void
xlog_cil_alloc_shadow_bufs(struct xlog * log,struct xfs_trans * tp)271b1c5ebb2SDave Chinner xlog_cil_alloc_shadow_bufs(
272b1c5ebb2SDave Chinner 	struct xlog		*log,
273b1c5ebb2SDave Chinner 	struct xfs_trans	*tp)
274b1c5ebb2SDave Chinner {
275e6631f85SDave Chinner 	struct xfs_log_item	*lip;
276b1c5ebb2SDave Chinner 
277e6631f85SDave Chinner 	list_for_each_entry(lip, &tp->t_items, li_trans) {
278b1c5ebb2SDave Chinner 		struct xfs_log_vec *lv;
279b1c5ebb2SDave Chinner 		int	niovecs = 0;
280b1c5ebb2SDave Chinner 		int	nbytes = 0;
281b1c5ebb2SDave Chinner 		int	buf_size;
282b1c5ebb2SDave Chinner 		bool	ordered = false;
283b1c5ebb2SDave Chinner 
284b1c5ebb2SDave Chinner 		/* Skip items which aren't dirty in this transaction. */
285e6631f85SDave Chinner 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
286b1c5ebb2SDave Chinner 			continue;
287b1c5ebb2SDave Chinner 
288b1c5ebb2SDave Chinner 		/* get number of vecs and size of data to be stored */
289b1c5ebb2SDave Chinner 		lip->li_ops->iop_size(lip, &niovecs, &nbytes);
290b1c5ebb2SDave Chinner 
291b1c5ebb2SDave Chinner 		/*
292b1c5ebb2SDave Chinner 		 * Ordered items need to be tracked but we do not wish to write
293b1c5ebb2SDave Chinner 		 * them. We need a logvec to track the object, but we do not
294b1c5ebb2SDave Chinner 		 * need an iovec or buffer to be allocated for copying data.
295b1c5ebb2SDave Chinner 		 */
296b1c5ebb2SDave Chinner 		if (niovecs == XFS_LOG_VEC_ORDERED) {
297b1c5ebb2SDave Chinner 			ordered = true;
298b1c5ebb2SDave Chinner 			niovecs = 0;
299b1c5ebb2SDave Chinner 			nbytes = 0;
300b1c5ebb2SDave Chinner 		}
301b1c5ebb2SDave Chinner 
302b1c5ebb2SDave Chinner 		/*
3038d547cf9SDave Chinner 		 * We 64-bit align the length of each iovec so that the start of
3048d547cf9SDave Chinner 		 * the next one is naturally aligned.  We'll need to account for
3058d547cf9SDave Chinner 		 * that slack space here.
3068d547cf9SDave Chinner 		 *
3078d547cf9SDave Chinner 		 * We also add the xlog_op_header to each region when
3088d547cf9SDave Chinner 		 * formatting, but that's not accounted to the size of the item
3098d547cf9SDave Chinner 		 * at this point. Hence we'll need an addition number of bytes
3108d547cf9SDave Chinner 		 * for each vector to hold an opheader.
3118d547cf9SDave Chinner 		 *
3128d547cf9SDave Chinner 		 * Then round nbytes up to 64-bit alignment so that the initial
3138d547cf9SDave Chinner 		 * buffer alignment is easy to calculate and verify.
314b1c5ebb2SDave Chinner 		 */
3158d547cf9SDave Chinner 		nbytes += niovecs *
3168d547cf9SDave Chinner 			(sizeof(uint64_t) + sizeof(struct xlog_op_header));
317b1c5ebb2SDave Chinner 		nbytes = round_up(nbytes, sizeof(uint64_t));
318b1c5ebb2SDave Chinner 
319b1c5ebb2SDave Chinner 		/*
320b1c5ebb2SDave Chinner 		 * The data buffer needs to start 64-bit aligned, so round up
321b1c5ebb2SDave Chinner 		 * that space to ensure we can align it appropriately and not
322b1c5ebb2SDave Chinner 		 * overrun the buffer.
323b1c5ebb2SDave Chinner 		 */
324b1c5ebb2SDave Chinner 		buf_size = nbytes + xlog_cil_iovec_space(niovecs);
325b1c5ebb2SDave Chinner 
326b1c5ebb2SDave Chinner 		/*
327b1c5ebb2SDave Chinner 		 * if we have no shadow buffer, or it is too small, we need to
328b1c5ebb2SDave Chinner 		 * reallocate it.
329b1c5ebb2SDave Chinner 		 */
330b1c5ebb2SDave Chinner 		if (!lip->li_lv_shadow ||
331b1c5ebb2SDave Chinner 		    buf_size > lip->li_lv_shadow->lv_size) {
332b1c5ebb2SDave Chinner 			/*
333b1c5ebb2SDave Chinner 			 * We free and allocate here as a realloc would copy
3348dc9384bSDave Chinner 			 * unnecessary data. We don't use kvzalloc() for the
335b1c5ebb2SDave Chinner 			 * same reason - we don't need to zero the data area in
336b1c5ebb2SDave Chinner 			 * the buffer, only the log vector header and the iovec
337b1c5ebb2SDave Chinner 			 * storage.
338b1c5ebb2SDave Chinner 			 */
339b1c5ebb2SDave Chinner 			kmem_free(lip->li_lv_shadow);
34045ff8b47SDave Chinner 			lv = xlog_kvmalloc(buf_size);
341b1c5ebb2SDave Chinner 
342b1c5ebb2SDave Chinner 			memset(lv, 0, xlog_cil_iovec_space(niovecs));
343b1c5ebb2SDave Chinner 
34416924853SDave Chinner 			INIT_LIST_HEAD(&lv->lv_list);
345b1c5ebb2SDave Chinner 			lv->lv_item = lip;
346b1c5ebb2SDave Chinner 			lv->lv_size = buf_size;
347b1c5ebb2SDave Chinner 			if (ordered)
348b1c5ebb2SDave Chinner 				lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
349b1c5ebb2SDave Chinner 			else
350b1c5ebb2SDave Chinner 				lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
351b1c5ebb2SDave Chinner 			lip->li_lv_shadow = lv;
352b1c5ebb2SDave Chinner 		} else {
353b1c5ebb2SDave Chinner 			/* same or smaller, optimise common overwrite case */
354b1c5ebb2SDave Chinner 			lv = lip->li_lv_shadow;
355b1c5ebb2SDave Chinner 			if (ordered)
356b1c5ebb2SDave Chinner 				lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
357b1c5ebb2SDave Chinner 			else
358b1c5ebb2SDave Chinner 				lv->lv_buf_len = 0;
359b1c5ebb2SDave Chinner 			lv->lv_bytes = 0;
360b1c5ebb2SDave Chinner 		}
361b1c5ebb2SDave Chinner 
362b1c5ebb2SDave Chinner 		/* Ensure the lv is set up according to ->iop_size */
363b1c5ebb2SDave Chinner 		lv->lv_niovecs = niovecs;
364b1c5ebb2SDave Chinner 
365b1c5ebb2SDave Chinner 		/* The allocated data region lies beyond the iovec region */
366b1c5ebb2SDave Chinner 		lv->lv_buf = (char *)lv + xlog_cil_iovec_space(niovecs);
367b1c5ebb2SDave Chinner 	}
368b1c5ebb2SDave Chinner 
369b1c5ebb2SDave Chinner }
370b1c5ebb2SDave Chinner 
37171e330b5SDave Chinner /*
372991aaf65SDave Chinner  * Prepare the log item for insertion into the CIL. Calculate the difference in
373593e3439SDave Chinner  * log space it will consume, and if it is a new item pin it as well.
374991aaf65SDave Chinner  */
375991aaf65SDave Chinner STATIC void
xfs_cil_prepare_item(struct xlog * log,struct xfs_log_vec * lv,struct xfs_log_vec * old_lv,int * diff_len)376991aaf65SDave Chinner xfs_cil_prepare_item(
377991aaf65SDave Chinner 	struct xlog		*log,
378991aaf65SDave Chinner 	struct xfs_log_vec	*lv,
379991aaf65SDave Chinner 	struct xfs_log_vec	*old_lv,
380593e3439SDave Chinner 	int			*diff_len)
381991aaf65SDave Chinner {
382991aaf65SDave Chinner 	/* Account for the new LV being passed in */
383593e3439SDave Chinner 	if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
384110dc24aSDave Chinner 		*diff_len += lv->lv_bytes;
385991aaf65SDave Chinner 
386991aaf65SDave Chinner 	/*
387991aaf65SDave Chinner 	 * If there is no old LV, this is the first time we've seen the item in
388991aaf65SDave Chinner 	 * this CIL context and so we need to pin it. If we are replacing the
389b1c5ebb2SDave Chinner 	 * old_lv, then remove the space it accounts for and make it the shadow
390b1c5ebb2SDave Chinner 	 * buffer for later freeing. In both cases we are now switching to the
391b63da6c8SRandy Dunlap 	 * shadow buffer, so update the pointer to it appropriately.
392991aaf65SDave Chinner 	 */
393b1c5ebb2SDave Chinner 	if (!old_lv) {
394e8b78db7SChristoph Hellwig 		if (lv->lv_item->li_ops->iop_pin)
395991aaf65SDave Chinner 			lv->lv_item->li_ops->iop_pin(lv->lv_item);
396b1c5ebb2SDave Chinner 		lv->lv_item->li_lv_shadow = NULL;
397b1c5ebb2SDave Chinner 	} else if (old_lv != lv) {
398991aaf65SDave Chinner 		ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
399991aaf65SDave Chinner 
400110dc24aSDave Chinner 		*diff_len -= old_lv->lv_bytes;
401b1c5ebb2SDave Chinner 		lv->lv_item->li_lv_shadow = old_lv;
402991aaf65SDave Chinner 	}
403991aaf65SDave Chinner 
404991aaf65SDave Chinner 	/* attach new log vector to log item */
405991aaf65SDave Chinner 	lv->lv_item->li_lv = lv;
406991aaf65SDave Chinner 
407991aaf65SDave Chinner 	/*
408991aaf65SDave Chinner 	 * If this is the first time the item is being committed to the
409991aaf65SDave Chinner 	 * CIL, store the sequence number on the log item so we can
410991aaf65SDave Chinner 	 * tell in future commits whether this is the first checkpoint
411991aaf65SDave Chinner 	 * the item is being committed into.
412991aaf65SDave Chinner 	 */
413991aaf65SDave Chinner 	if (!lv->lv_item->li_seq)
414991aaf65SDave Chinner 		lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
415991aaf65SDave Chinner }
416991aaf65SDave Chinner 
417991aaf65SDave Chinner /*
41871e330b5SDave Chinner  * Format log item into a flat buffers
41971e330b5SDave Chinner  *
42071e330b5SDave Chinner  * For delayed logging, we need to hold a formatted buffer containing all the
42171e330b5SDave Chinner  * changes on the log item. This enables us to relog the item in memory and
42271e330b5SDave Chinner  * write it out asynchronously without needing to relock the object that was
42371e330b5SDave Chinner  * modified at the time it gets written into the iclog.
42471e330b5SDave Chinner  *
425b1c5ebb2SDave Chinner  * This function takes the prepared log vectors attached to each log item, and
426b1c5ebb2SDave Chinner  * formats the changes into the log vector buffer. The buffer it uses is
427b1c5ebb2SDave Chinner  * dependent on the current state of the vector in the CIL - the shadow lv is
428b1c5ebb2SDave Chinner  * guaranteed to be large enough for the current modification, but we will only
429b1c5ebb2SDave Chinner  * use that if we can't reuse the existing lv. If we can't reuse the existing
430b1c5ebb2SDave Chinner  * lv, then simple swap it out for the shadow lv. We don't free it - that is
431b1c5ebb2SDave Chinner  * done lazily either by th enext modification or the freeing of the log item.
43271e330b5SDave Chinner  *
43371e330b5SDave Chinner  * We don't set up region headers during this process; we simply copy the
43471e330b5SDave Chinner  * regions into the flat buffer. We can do this because we still have to do a
43571e330b5SDave Chinner  * formatting step to write the regions into the iclog buffer.  Writing the
43671e330b5SDave Chinner  * ophdrs during the iclog write means that we can support splitting large
43771e330b5SDave Chinner  * regions across iclog boundares without needing a change in the format of the
43871e330b5SDave Chinner  * item/region encapsulation.
43971e330b5SDave Chinner  *
44071e330b5SDave Chinner  * Hence what we need to do now is change the rewrite the vector array to point
44171e330b5SDave Chinner  * to the copied region inside the buffer we just allocated. This allows us to
44271e330b5SDave Chinner  * format the regions into the iclog as though they are being formatted
44371e330b5SDave Chinner  * directly out of the objects themselves.
44471e330b5SDave Chinner  */
445991aaf65SDave Chinner static void
xlog_cil_insert_format_items(struct xlog * log,struct xfs_trans * tp,int * diff_len)446991aaf65SDave Chinner xlog_cil_insert_format_items(
447991aaf65SDave Chinner 	struct xlog		*log,
448991aaf65SDave Chinner 	struct xfs_trans	*tp,
449593e3439SDave Chinner 	int			*diff_len)
45071e330b5SDave Chinner {
451e6631f85SDave Chinner 	struct xfs_log_item	*lip;
45271e330b5SDave Chinner 
4530244b960SChristoph Hellwig 	/* Bail out if we didn't find a log item.  */
4540244b960SChristoph Hellwig 	if (list_empty(&tp->t_items)) {
4550244b960SChristoph Hellwig 		ASSERT(0);
456991aaf65SDave Chinner 		return;
4570244b960SChristoph Hellwig 	}
4580244b960SChristoph Hellwig 
459e6631f85SDave Chinner 	list_for_each_entry(lip, &tp->t_items, li_trans) {
4607492c5b4SDave Chinner 		struct xfs_log_vec *lv;
461b1c5ebb2SDave Chinner 		struct xfs_log_vec *old_lv = NULL;
462b1c5ebb2SDave Chinner 		struct xfs_log_vec *shadow;
463fd63875cSDave Chinner 		bool	ordered = false;
46471e330b5SDave Chinner 
4650244b960SChristoph Hellwig 		/* Skip items which aren't dirty in this transaction. */
466e6631f85SDave Chinner 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
4670244b960SChristoph Hellwig 			continue;
4680244b960SChristoph Hellwig 
469b1c5ebb2SDave Chinner 		/*
470b1c5ebb2SDave Chinner 		 * The formatting size information is already attached to
471b1c5ebb2SDave Chinner 		 * the shadow lv on the log item.
472b1c5ebb2SDave Chinner 		 */
473b1c5ebb2SDave Chinner 		shadow = lip->li_lv_shadow;
474b1c5ebb2SDave Chinner 		if (shadow->lv_buf_len == XFS_LOG_VEC_ORDERED)
475b1c5ebb2SDave Chinner 			ordered = true;
476166d1368SDave Chinner 
4770244b960SChristoph Hellwig 		/* Skip items that do not have any vectors for writing */
478b1c5ebb2SDave Chinner 		if (!shadow->lv_niovecs && !ordered)
4790244b960SChristoph Hellwig 			continue;
4800244b960SChristoph Hellwig 
481f5baac35SDave Chinner 		/* compare to existing item size */
482b1c5ebb2SDave Chinner 		old_lv = lip->li_lv;
483b1c5ebb2SDave Chinner 		if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {
484f5baac35SDave Chinner 			/* same or smaller, optimise common overwrite case */
485f5baac35SDave Chinner 			lv = lip->li_lv;
486f5baac35SDave Chinner 
487f5baac35SDave Chinner 			if (ordered)
488f5baac35SDave Chinner 				goto insert;
489f5baac35SDave Chinner 
490991aaf65SDave Chinner 			/*
491991aaf65SDave Chinner 			 * set the item up as though it is a new insertion so
492991aaf65SDave Chinner 			 * that the space reservation accounting is correct.
493991aaf65SDave Chinner 			 */
494110dc24aSDave Chinner 			*diff_len -= lv->lv_bytes;
495b1c5ebb2SDave Chinner 
496b1c5ebb2SDave Chinner 			/* Ensure the lv is set up according to ->iop_size */
497b1c5ebb2SDave Chinner 			lv->lv_niovecs = shadow->lv_niovecs;
498b1c5ebb2SDave Chinner 
499b1c5ebb2SDave Chinner 			/* reset the lv buffer information for new formatting */
500b1c5ebb2SDave Chinner 			lv->lv_buf_len = 0;
501b1c5ebb2SDave Chinner 			lv->lv_bytes = 0;
502b1c5ebb2SDave Chinner 			lv->lv_buf = (char *)lv +
503b1c5ebb2SDave Chinner 					xlog_cil_iovec_space(lv->lv_niovecs);
5049597df6bSChristoph Hellwig 		} else {
505b1c5ebb2SDave Chinner 			/* switch to shadow buffer! */
506b1c5ebb2SDave Chinner 			lv = shadow;
5077492c5b4SDave Chinner 			lv->lv_item = lip;
508fd63875cSDave Chinner 			if (ordered) {
509fd63875cSDave Chinner 				/* track as an ordered logvec */
5107492c5b4SDave Chinner 				ASSERT(lip->li_lv == NULL);
5117492c5b4SDave Chinner 				goto insert;
512fd63875cSDave Chinner 			}
5139597df6bSChristoph Hellwig 		}
5149597df6bSChristoph Hellwig 
5153895e51fSDave Chinner 		ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
516bde7cff6SChristoph Hellwig 		lip->li_ops->iop_format(lip, lv);
5177492c5b4SDave Chinner insert:
518593e3439SDave Chinner 		xfs_cil_prepare_item(log, lv, old_lv, diff_len);
51971e330b5SDave Chinner 	}
520d1583a38SDave Chinner }
521d1583a38SDave Chinner 
522d1583a38SDave Chinner /*
5237c8ade21SDave Chinner  * The use of lockless waitqueue_active() requires that the caller has
5247c8ade21SDave Chinner  * serialised itself against the wakeup call in xlog_cil_push_work(). That
5257c8ade21SDave Chinner  * can be done by either holding the push lock or the context lock.
5267c8ade21SDave Chinner  */
5277c8ade21SDave Chinner static inline bool
xlog_cil_over_hard_limit(struct xlog * log,int32_t space_used)5287c8ade21SDave Chinner xlog_cil_over_hard_limit(
5297c8ade21SDave Chinner 	struct xlog	*log,
5307c8ade21SDave Chinner 	int32_t		space_used)
5317c8ade21SDave Chinner {
5327c8ade21SDave Chinner 	if (waitqueue_active(&log->l_cilp->xc_push_wait))
5337c8ade21SDave Chinner 		return true;
5347c8ade21SDave Chinner 	if (space_used >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log))
5357c8ade21SDave Chinner 		return true;
5367c8ade21SDave Chinner 	return false;
5377c8ade21SDave Chinner }
5387c8ade21SDave Chinner 
5397c8ade21SDave Chinner /*
540d1583a38SDave Chinner  * Insert the log items into the CIL and calculate the difference in space
541d1583a38SDave Chinner  * consumed by the item. Add the space to the checkpoint ticket and calculate
542d1583a38SDave Chinner  * if the change requires additional log metadata. If it does, take that space
54342b2aa86SJustin P. Mattock  * as well. Remove the amount of space we added to the checkpoint ticket from
544d1583a38SDave Chinner  * the current transaction ticket so that the accounting works out correctly.
545d1583a38SDave Chinner  */
54671e330b5SDave Chinner static void
xlog_cil_insert_items(struct xlog * log,struct xfs_trans * tp,uint32_t released_space)5473b93c7aaSDave Chinner xlog_cil_insert_items(
548f7bdf03aSMark Tinguely 	struct xlog		*log,
5490d227466SDave Chinner 	struct xfs_trans	*tp,
5500d227466SDave Chinner 	uint32_t		released_space)
5513b93c7aaSDave Chinner {
552d1583a38SDave Chinner 	struct xfs_cil		*cil = log->l_cilp;
553d1583a38SDave Chinner 	struct xfs_cil_ctx	*ctx = cil->xc_ctx;
554e6631f85SDave Chinner 	struct xfs_log_item	*lip;
555d1583a38SDave Chinner 	int			len = 0;
556e2f23426SBrian Foster 	int			iovhdr_res = 0, split_res = 0, ctx_res = 0;
5577c8ade21SDave Chinner 	int			space_used;
558016a2338SDave Chinner 	int			order;
559ecd49f7aSDarrick J. Wong 	unsigned int		cpu_nr;
5607c8ade21SDave Chinner 	struct xlog_cil_pcp	*cilpcp;
5613b93c7aaSDave Chinner 
562991aaf65SDave Chinner 	ASSERT(tp);
563d1583a38SDave Chinner 
564d1583a38SDave Chinner 	/*
565d1583a38SDave Chinner 	 * We can do this safely because the context can't checkpoint until we
566d1583a38SDave Chinner 	 * are done so it doesn't matter exactly how we update the CIL.
567d1583a38SDave Chinner 	 */
568593e3439SDave Chinner 	xlog_cil_insert_format_items(log, tp, &len);
569fd63875cSDave Chinner 
570e2f23426SBrian Foster 	/*
5717c8ade21SDave Chinner 	 * Subtract the space released by intent cancelation from the space we
5727c8ade21SDave Chinner 	 * consumed so that we remove it from the CIL space and add it back to
5737c8ade21SDave Chinner 	 * the current transaction reservation context.
5747c8ade21SDave Chinner 	 */
5757c8ade21SDave Chinner 	len -= released_space;
5767c8ade21SDave Chinner 
5777c8ade21SDave Chinner 	/*
5787c8ade21SDave Chinner 	 * Grab the per-cpu pointer for the CIL before we start any accounting.
5797c8ade21SDave Chinner 	 * That ensures that we are running with pre-emption disabled and so we
5807c8ade21SDave Chinner 	 * can't be scheduled away between split sample/update operations that
5817c8ade21SDave Chinner 	 * are done without outside locking to serialise them.
5827c8ade21SDave Chinner 	 */
583ecd49f7aSDarrick J. Wong 	cpu_nr = get_cpu();
584ecd49f7aSDarrick J. Wong 	cilpcp = this_cpu_ptr(cil->xc_pcp);
585ecd49f7aSDarrick J. Wong 
586ecd49f7aSDarrick J. Wong 	/* Tell the future push that there was work added by this CPU. */
587ecd49f7aSDarrick J. Wong 	if (!cpumask_test_cpu(cpu_nr, &ctx->cil_pcpmask))
588ecd49f7aSDarrick J. Wong 		cpumask_test_and_set_cpu(cpu_nr, &ctx->cil_pcpmask);
5897c8ade21SDave Chinner 
5907c8ade21SDave Chinner 	/*
59188591e7fSDave Chinner 	 * We need to take the CIL checkpoint unit reservation on the first
59288591e7fSDave Chinner 	 * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't
593c0fb4765SDave Chinner 	 * unnecessarily do an atomic op in the fast path here. We can clear the
594c0fb4765SDave Chinner 	 * XLOG_CIL_EMPTY bit as we are under the xc_ctx_lock here and that
595c0fb4765SDave Chinner 	 * needs to be held exclusively to reset the XLOG_CIL_EMPTY bit.
596e2f23426SBrian Foster 	 */
59788591e7fSDave Chinner 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags) &&
59812380d23SDave Chinner 	    test_and_clear_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
599e2f23426SBrian Foster 		ctx_res = ctx->ticket->t_unit_res;
60012380d23SDave Chinner 
60131151cc3SDave Chinner 	/*
60231151cc3SDave Chinner 	 * Check if we need to steal iclog headers. atomic_read() is not a
60331151cc3SDave Chinner 	 * locked atomic operation, so we can check the value before we do any
60431151cc3SDave Chinner 	 * real atomic ops in the fast path. If we've already taken the CIL unit
60531151cc3SDave Chinner 	 * reservation from this commit, we've already got one iclog header
60631151cc3SDave Chinner 	 * space reserved so we have to account for that otherwise we risk
60731151cc3SDave Chinner 	 * overrunning the reservation on this ticket.
60831151cc3SDave Chinner 	 *
60931151cc3SDave Chinner 	 * If the CIL is already at the hard limit, we might need more header
61031151cc3SDave Chinner 	 * space that originally reserved. So steal more header space from every
61131151cc3SDave Chinner 	 * commit that occurs once we are over the hard limit to ensure the CIL
61231151cc3SDave Chinner 	 * push won't run out of reservation space.
61331151cc3SDave Chinner 	 *
61431151cc3SDave Chinner 	 * This can steal more than we need, but that's OK.
6157c8ade21SDave Chinner 	 *
6167c8ade21SDave Chinner 	 * The cil->xc_ctx_lock provides the serialisation necessary for safely
6177c8ade21SDave Chinner 	 * calling xlog_cil_over_hard_limit() in this context.
61831151cc3SDave Chinner 	 */
6197c8ade21SDave Chinner 	space_used = atomic_read(&ctx->space_used) + cilpcp->space_used + len;
62031151cc3SDave Chinner 	if (atomic_read(&cil->xc_iclog_hdrs) > 0 ||
6217c8ade21SDave Chinner 	    xlog_cil_over_hard_limit(log, space_used)) {
6227c8ade21SDave Chinner 		split_res = log->l_iclog_hsize +
62331151cc3SDave Chinner 					sizeof(struct xlog_op_header);
62431151cc3SDave Chinner 		if (ctx_res)
62531151cc3SDave Chinner 			ctx_res += split_res * (tp->t_ticket->t_iclog_hdrs - 1);
62631151cc3SDave Chinner 		else
62731151cc3SDave Chinner 			ctx_res = split_res * tp->t_ticket->t_iclog_hdrs;
62831151cc3SDave Chinner 		atomic_sub(tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs);
629e2f23426SBrian Foster 	}
6301dd2a2c1SDave Chinner 	cilpcp->space_reserved += ctx_res;
63131151cc3SDave Chinner 
6327c8ade21SDave Chinner 	/*
6337c8ade21SDave Chinner 	 * Accurately account when over the soft limit, otherwise fold the
6347c8ade21SDave Chinner 	 * percpu count into the global count if over the per-cpu threshold.
6357c8ade21SDave Chinner 	 */
6367c8ade21SDave Chinner 	if (!test_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags)) {
6377c8ade21SDave Chinner 		atomic_add(len, &ctx->space_used);
6387c8ade21SDave Chinner 	} else if (cilpcp->space_used + len >
6397c8ade21SDave Chinner 			(XLOG_CIL_SPACE_LIMIT(log) / num_online_cpus())) {
6407c8ade21SDave Chinner 		space_used = atomic_add_return(cilpcp->space_used + len,
6417c8ade21SDave Chinner 						&ctx->space_used);
6427c8ade21SDave Chinner 		cilpcp->space_used = 0;
643e2f23426SBrian Foster 
644fd63875cSDave Chinner 		/*
6457c8ade21SDave Chinner 		 * If we just transitioned over the soft limit, we need to
6467c8ade21SDave Chinner 		 * transition to the global atomic counter.
647d4ca1d55SBrian Foster 		 */
6487c8ade21SDave Chinner 		if (space_used >= XLOG_CIL_SPACE_LIMIT(log))
6497c8ade21SDave Chinner 			xlog_cil_insert_pcp_aggregate(cil, ctx);
6507c8ade21SDave Chinner 	} else {
6517c8ade21SDave Chinner 		cilpcp->space_used += len;
652d4ca1d55SBrian Foster 	}
653df7a4a21SDave Chinner 	/* attach the transaction to the CIL if it has any busy extents */
654df7a4a21SDave Chinner 	if (!list_empty(&tp->t_busy))
655df7a4a21SDave Chinner 		list_splice_init(&tp->t_busy, &cilpcp->busy_extents);
6567c8ade21SDave Chinner 
657d4ca1d55SBrian Foster 	/*
658016a2338SDave Chinner 	 * Now update the order of everything modified in the transaction
659016a2338SDave Chinner 	 * and insert items into the CIL if they aren't already there.
660991aaf65SDave Chinner 	 * We do this here so we only need to take the CIL lock once during
661991aaf65SDave Chinner 	 * the transaction commit.
662fd63875cSDave Chinner 	 */
663016a2338SDave Chinner 	order = atomic_inc_return(&ctx->order_id);
664e6631f85SDave Chinner 	list_for_each_entry(lip, &tp->t_items, li_trans) {
665991aaf65SDave Chinner 		/* Skip items which aren't dirty in this transaction. */
666e6631f85SDave Chinner 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
667991aaf65SDave Chinner 			continue;
668991aaf65SDave Chinner 
669016a2338SDave Chinner 		lip->li_order_id = order;
670016a2338SDave Chinner 		if (!list_empty(&lip->li_cil))
671016a2338SDave Chinner 			continue;
672c0fb4765SDave Chinner 		list_add_tail(&lip->li_cil, &cilpcp->log_items);
673fd63875cSDave Chinner 	}
674ecd49f7aSDarrick J. Wong 	put_cpu();
675d4ca1d55SBrian Foster 
6767c8ade21SDave Chinner 	/*
6777c8ade21SDave Chinner 	 * If we've overrun the reservation, dump the tx details before we move
6787c8ade21SDave Chinner 	 * the log items. Shutdown is imminent...
6797c8ade21SDave Chinner 	 */
6807c8ade21SDave Chinner 	tp->t_ticket->t_curr_res -= ctx_res + len;
6817c8ade21SDave Chinner 	if (WARN_ON(tp->t_ticket->t_curr_res < 0)) {
6827c8ade21SDave Chinner 		xfs_warn(log->l_mp, "Transaction log reservation overrun:");
6837c8ade21SDave Chinner 		xfs_warn(log->l_mp,
6847c8ade21SDave Chinner 			 "  log items: %d bytes (iov hdrs: %d bytes)",
6857c8ade21SDave Chinner 			 len, iovhdr_res);
6867c8ade21SDave Chinner 		xfs_warn(log->l_mp, "  split region headers: %d bytes",
6877c8ade21SDave Chinner 			 split_res);
6887c8ade21SDave Chinner 		xfs_warn(log->l_mp, "  ctx ticket: %d bytes", ctx_res);
6897c8ade21SDave Chinner 		xlog_print_trans(tp);
690b5f17becSDave Chinner 		xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
6913b93c7aaSDave Chinner 	}
6927c8ade21SDave Chinner }
6933b93c7aaSDave Chinner 
6943b93c7aaSDave Chinner static void
xlog_cil_free_logvec(struct list_head * lv_chain)69571e330b5SDave Chinner xlog_cil_free_logvec(
69616924853SDave Chinner 	struct list_head	*lv_chain)
69771e330b5SDave Chinner {
69871e330b5SDave Chinner 	struct xfs_log_vec	*lv;
69971e330b5SDave Chinner 
70016924853SDave Chinner 	while (!list_empty(lv_chain)) {
70116924853SDave Chinner 		lv = list_first_entry(lv_chain, struct xfs_log_vec, lv_list);
70216924853SDave Chinner 		list_del_init(&lv->lv_list);
70371e330b5SDave Chinner 		kmem_free(lv);
70471e330b5SDave Chinner 	}
70571e330b5SDave Chinner }
70671e330b5SDave Chinner 
70771e330b5SDave Chinner /*
70871e330b5SDave Chinner  * Mark all items committed and clear busy extents. We free the log vector
70971e330b5SDave Chinner  * chains in a separate pass so that we unpin the log items as quickly as
71071e330b5SDave Chinner  * possible.
71171e330b5SDave Chinner  */
71271e330b5SDave Chinner static void
xlog_cil_committed(struct xfs_cil_ctx * ctx)71371e330b5SDave Chinner xlog_cil_committed(
71412e6a0f4SChristoph Hellwig 	struct xfs_cil_ctx	*ctx)
71571e330b5SDave Chinner {
716e84661aaSChristoph Hellwig 	struct xfs_mount	*mp = ctx->cil->xc_log->l_mp;
7172039a272SDave Chinner 	bool			abort = xlog_is_shutdown(ctx->cil->xc_log);
71871e330b5SDave Chinner 
719545aa41fSBrian Foster 	/*
720545aa41fSBrian Foster 	 * If the I/O failed, we're aborting the commit and already shutdown.
721545aa41fSBrian Foster 	 * Wake any commit waiters before aborting the log items so we don't
722545aa41fSBrian Foster 	 * block async log pushers on callbacks. Async log pushers explicitly do
723545aa41fSBrian Foster 	 * not wait on log force completion because they may be holding locks
724545aa41fSBrian Foster 	 * required to unpin items.
725545aa41fSBrian Foster 	 */
726545aa41fSBrian Foster 	if (abort) {
727545aa41fSBrian Foster 		spin_lock(&ctx->cil->xc_push_lock);
72868a74dcaSDave Chinner 		wake_up_all(&ctx->cil->xc_start_wait);
729545aa41fSBrian Foster 		wake_up_all(&ctx->cil->xc_commit_wait);
730545aa41fSBrian Foster 		spin_unlock(&ctx->cil->xc_push_lock);
731545aa41fSBrian Foster 	}
732545aa41fSBrian Foster 
73316924853SDave Chinner 	xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, &ctx->lv_chain,
7340e57f6a3SDave Chinner 					ctx->start_lsn, abort);
73571e330b5SDave Chinner 
736428c4435SDave Chinner 	xfs_extent_busy_sort(&ctx->busy_extents.extent_list);
737428c4435SDave Chinner 	xfs_extent_busy_clear(mp, &ctx->busy_extents.extent_list,
7380560f31aSDave Chinner 			      xfs_has_discard(mp) && !abort);
73971e330b5SDave Chinner 
7404bb928cdSDave Chinner 	spin_lock(&ctx->cil->xc_push_lock);
74171e330b5SDave Chinner 	list_del(&ctx->committing);
7424bb928cdSDave Chinner 	spin_unlock(&ctx->cil->xc_push_lock);
74371e330b5SDave Chinner 
74416924853SDave Chinner 	xlog_cil_free_logvec(&ctx->lv_chain);
745e84661aaSChristoph Hellwig 
746428c4435SDave Chinner 	if (!list_empty(&ctx->busy_extents.extent_list)) {
747428c4435SDave Chinner 		ctx->busy_extents.mount = mp;
748428c4435SDave Chinner 		ctx->busy_extents.owner = ctx;
749428c4435SDave Chinner 		xfs_discard_extents(mp, &ctx->busy_extents);
750428c4435SDave Chinner 		return;
751428c4435SDave Chinner 	}
752428c4435SDave Chinner 
75371e330b5SDave Chinner 	kmem_free(ctx);
75471e330b5SDave Chinner }
75571e330b5SDave Chinner 
75689ae379dSChristoph Hellwig void
xlog_cil_process_committed(struct list_head * list)75789ae379dSChristoph Hellwig xlog_cil_process_committed(
75812e6a0f4SChristoph Hellwig 	struct list_head	*list)
75989ae379dSChristoph Hellwig {
76089ae379dSChristoph Hellwig 	struct xfs_cil_ctx	*ctx;
76189ae379dSChristoph Hellwig 
76289ae379dSChristoph Hellwig 	while ((ctx = list_first_entry_or_null(list,
76389ae379dSChristoph Hellwig 			struct xfs_cil_ctx, iclog_entry))) {
76489ae379dSChristoph Hellwig 		list_del(&ctx->iclog_entry);
76512e6a0f4SChristoph Hellwig 		xlog_cil_committed(ctx);
76689ae379dSChristoph Hellwig 	}
76789ae379dSChristoph Hellwig }
76889ae379dSChristoph Hellwig 
76971e330b5SDave Chinner /*
770c45aba40SDave Chinner * Record the LSN of the iclog we were just granted space to start writing into.
771c45aba40SDave Chinner * If the context doesn't have a start_lsn recorded, then this iclog will
772c45aba40SDave Chinner * contain the start record for the checkpoint. Otherwise this write contains
773c45aba40SDave Chinner * the commit record for the checkpoint.
774c45aba40SDave Chinner */
775c45aba40SDave Chinner void
xlog_cil_set_ctx_write_state(struct xfs_cil_ctx * ctx,struct xlog_in_core * iclog)776c45aba40SDave Chinner xlog_cil_set_ctx_write_state(
777c45aba40SDave Chinner 	struct xfs_cil_ctx	*ctx,
778c45aba40SDave Chinner 	struct xlog_in_core	*iclog)
779c45aba40SDave Chinner {
780c45aba40SDave Chinner 	struct xfs_cil		*cil = ctx->cil;
781c45aba40SDave Chinner 	xfs_lsn_t		lsn = be64_to_cpu(iclog->ic_header.h_lsn);
782c45aba40SDave Chinner 
783c45aba40SDave Chinner 	ASSERT(!ctx->commit_lsn);
784caa80090SDave Chinner 	if (!ctx->start_lsn) {
785c45aba40SDave Chinner 		spin_lock(&cil->xc_push_lock);
78668a74dcaSDave Chinner 		/*
78768a74dcaSDave Chinner 		 * The LSN we need to pass to the log items on transaction
78868a74dcaSDave Chinner 		 * commit is the LSN reported by the first log vector write, not
78968a74dcaSDave Chinner 		 * the commit lsn. If we use the commit record lsn then we can
790919edbadSDave Chinner 		 * move the grant write head beyond the tail LSN and overwrite
791919edbadSDave Chinner 		 * it.
79268a74dcaSDave Chinner 		 */
793c45aba40SDave Chinner 		ctx->start_lsn = lsn;
79468a74dcaSDave Chinner 		wake_up_all(&cil->xc_start_wait);
795caa80090SDave Chinner 		spin_unlock(&cil->xc_push_lock);
796919edbadSDave Chinner 
797919edbadSDave Chinner 		/*
798919edbadSDave Chinner 		 * Make sure the metadata we are about to overwrite in the log
799919edbadSDave Chinner 		 * has been flushed to stable storage before this iclog is
800919edbadSDave Chinner 		 * issued.
801919edbadSDave Chinner 		 */
802919edbadSDave Chinner 		spin_lock(&cil->xc_log->l_icloglock);
803919edbadSDave Chinner 		iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
804919edbadSDave Chinner 		spin_unlock(&cil->xc_log->l_icloglock);
805caa80090SDave Chinner 		return;
806caa80090SDave Chinner 	}
807caa80090SDave Chinner 
808caa80090SDave Chinner 	/*
809caa80090SDave Chinner 	 * Take a reference to the iclog for the context so that we still hold
810caa80090SDave Chinner 	 * it when xlog_write is done and has released it. This means the
811caa80090SDave Chinner 	 * context controls when the iclog is released for IO.
812caa80090SDave Chinner 	 */
813caa80090SDave Chinner 	atomic_inc(&iclog->ic_refcnt);
814caa80090SDave Chinner 
815caa80090SDave Chinner 	/*
816caa80090SDave Chinner 	 * xlog_state_get_iclog_space() guarantees there is enough space in the
817caa80090SDave Chinner 	 * iclog for an entire commit record, so we can attach the context
818caa80090SDave Chinner 	 * callbacks now.  This needs to be done before we make the commit_lsn
819caa80090SDave Chinner 	 * visible to waiters so that checkpoints with commit records in the
820caa80090SDave Chinner 	 * same iclog order their IO completion callbacks in the same order that
821caa80090SDave Chinner 	 * the commit records appear in the iclog.
822caa80090SDave Chinner 	 */
823caa80090SDave Chinner 	spin_lock(&cil->xc_log->l_icloglock);
824caa80090SDave Chinner 	list_add_tail(&ctx->iclog_entry, &iclog->ic_callbacks);
825caa80090SDave Chinner 	spin_unlock(&cil->xc_log->l_icloglock);
826caa80090SDave Chinner 
827caa80090SDave Chinner 	/*
828caa80090SDave Chinner 	 * Now we can record the commit LSN and wake anyone waiting for this
829caa80090SDave Chinner 	 * sequence to have the ordered commit record assigned to a physical
830caa80090SDave Chinner 	 * location in the log.
831caa80090SDave Chinner 	 */
832caa80090SDave Chinner 	spin_lock(&cil->xc_push_lock);
833caa80090SDave Chinner 	ctx->commit_iclog = iclog;
834c45aba40SDave Chinner 	ctx->commit_lsn = lsn;
835caa80090SDave Chinner 	wake_up_all(&cil->xc_commit_wait);
836c45aba40SDave Chinner 	spin_unlock(&cil->xc_push_lock);
837c45aba40SDave Chinner }
838c45aba40SDave Chinner 
839c45aba40SDave Chinner 
840c45aba40SDave Chinner /*
841bf034bc8SDave Chinner  * Ensure that the order of log writes follows checkpoint sequence order. This
842bf034bc8SDave Chinner  * relies on the context LSN being zero until the log write has guaranteed the
843bf034bc8SDave Chinner  * LSN that the log write will start at via xlog_state_get_iclog_space().
844bf034bc8SDave Chinner  */
84568a74dcaSDave Chinner enum _record_type {
84668a74dcaSDave Chinner 	_START_RECORD,
84768a74dcaSDave Chinner 	_COMMIT_RECORD,
84868a74dcaSDave Chinner };
84968a74dcaSDave Chinner 
850bf034bc8SDave Chinner static int
xlog_cil_order_write(struct xfs_cil * cil,xfs_csn_t sequence,enum _record_type record)851bf034bc8SDave Chinner xlog_cil_order_write(
852bf034bc8SDave Chinner 	struct xfs_cil		*cil,
85368a74dcaSDave Chinner 	xfs_csn_t		sequence,
85468a74dcaSDave Chinner 	enum _record_type	record)
855bf034bc8SDave Chinner {
856bf034bc8SDave Chinner 	struct xfs_cil_ctx	*ctx;
857bf034bc8SDave Chinner 
858bf034bc8SDave Chinner restart:
859bf034bc8SDave Chinner 	spin_lock(&cil->xc_push_lock);
860bf034bc8SDave Chinner 	list_for_each_entry(ctx, &cil->xc_committing, committing) {
861bf034bc8SDave Chinner 		/*
862bf034bc8SDave Chinner 		 * Avoid getting stuck in this loop because we were woken by the
863bf034bc8SDave Chinner 		 * shutdown, but then went back to sleep once already in the
864bf034bc8SDave Chinner 		 * shutdown state.
865bf034bc8SDave Chinner 		 */
866bf034bc8SDave Chinner 		if (xlog_is_shutdown(cil->xc_log)) {
867bf034bc8SDave Chinner 			spin_unlock(&cil->xc_push_lock);
868bf034bc8SDave Chinner 			return -EIO;
869bf034bc8SDave Chinner 		}
870bf034bc8SDave Chinner 
871bf034bc8SDave Chinner 		/*
872bf034bc8SDave Chinner 		 * Higher sequences will wait for this one so skip them.
873bf034bc8SDave Chinner 		 * Don't wait for our own sequence, either.
874bf034bc8SDave Chinner 		 */
875bf034bc8SDave Chinner 		if (ctx->sequence >= sequence)
876bf034bc8SDave Chinner 			continue;
87768a74dcaSDave Chinner 
87868a74dcaSDave Chinner 		/* Wait until the LSN for the record has been recorded. */
87968a74dcaSDave Chinner 		switch (record) {
88068a74dcaSDave Chinner 		case _START_RECORD:
88168a74dcaSDave Chinner 			if (!ctx->start_lsn) {
88268a74dcaSDave Chinner 				xlog_wait(&cil->xc_start_wait, &cil->xc_push_lock);
88368a74dcaSDave Chinner 				goto restart;
88468a74dcaSDave Chinner 			}
88568a74dcaSDave Chinner 			break;
88668a74dcaSDave Chinner 		case _COMMIT_RECORD:
887bf034bc8SDave Chinner 			if (!ctx->commit_lsn) {
888bf034bc8SDave Chinner 				xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
889bf034bc8SDave Chinner 				goto restart;
890bf034bc8SDave Chinner 			}
89168a74dcaSDave Chinner 			break;
89268a74dcaSDave Chinner 		}
893bf034bc8SDave Chinner 	}
894bf034bc8SDave Chinner 	spin_unlock(&cil->xc_push_lock);
895bf034bc8SDave Chinner 	return 0;
896bf034bc8SDave Chinner }
897bf034bc8SDave Chinner 
898bf034bc8SDave Chinner /*
89968a74dcaSDave Chinner  * Write out the log vector change now attached to the CIL context. This will
90068a74dcaSDave Chinner  * write a start record that needs to be strictly ordered in ascending CIL
90168a74dcaSDave Chinner  * sequence order so that log recovery will always use in-order start LSNs when
90268a74dcaSDave Chinner  * replaying checkpoints.
90368a74dcaSDave Chinner  */
90468a74dcaSDave Chinner static int
xlog_cil_write_chain(struct xfs_cil_ctx * ctx,uint32_t chain_len)90568a74dcaSDave Chinner xlog_cil_write_chain(
90668a74dcaSDave Chinner 	struct xfs_cil_ctx	*ctx,
907d80fc291SDave Chinner 	uint32_t		chain_len)
90868a74dcaSDave Chinner {
90968a74dcaSDave Chinner 	struct xlog		*log = ctx->cil->xc_log;
91068a74dcaSDave Chinner 	int			error;
91168a74dcaSDave Chinner 
91268a74dcaSDave Chinner 	error = xlog_cil_order_write(ctx->cil, ctx->sequence, _START_RECORD);
91368a74dcaSDave Chinner 	if (error)
91468a74dcaSDave Chinner 		return error;
91516924853SDave Chinner 	return xlog_write(log, ctx, &ctx->lv_chain, ctx->ticket, chain_len);
91668a74dcaSDave Chinner }
91768a74dcaSDave Chinner 
91868a74dcaSDave Chinner /*
919bf034bc8SDave Chinner  * Write out the commit record of a checkpoint transaction to close off a
920bf034bc8SDave Chinner  * running log write. These commit records are strictly ordered in ascending CIL
921bf034bc8SDave Chinner  * sequence order so that log recovery will always replay the checkpoints in the
922bf034bc8SDave Chinner  * correct order.
9232ce82b72SDave Chinner  */
9242ce82b72SDave Chinner static int
xlog_cil_write_commit_record(struct xfs_cil_ctx * ctx)9252ce82b72SDave Chinner xlog_cil_write_commit_record(
926caa80090SDave Chinner 	struct xfs_cil_ctx	*ctx)
9272ce82b72SDave Chinner {
928c45aba40SDave Chinner 	struct xlog		*log = ctx->cil->xc_log;
92954021b62SDave Chinner 	struct xlog_op_header	ophdr = {
93054021b62SDave Chinner 		.oh_clientid = XFS_TRANSACTION,
93154021b62SDave Chinner 		.oh_tid = cpu_to_be32(ctx->ticket->t_tid),
93254021b62SDave Chinner 		.oh_flags = XLOG_COMMIT_TRANS,
93354021b62SDave Chinner 	};
9342ce82b72SDave Chinner 	struct xfs_log_iovec	reg = {
93554021b62SDave Chinner 		.i_addr = &ophdr,
93654021b62SDave Chinner 		.i_len = sizeof(struct xlog_op_header),
9372ce82b72SDave Chinner 		.i_type = XLOG_REG_TYPE_COMMIT,
9382ce82b72SDave Chinner 	};
9392ce82b72SDave Chinner 	struct xfs_log_vec	vec = {
9402ce82b72SDave Chinner 		.lv_niovecs = 1,
9412ce82b72SDave Chinner 		.lv_iovecp = &reg,
9422ce82b72SDave Chinner 	};
9432ce82b72SDave Chinner 	int			error;
94416924853SDave Chinner 	LIST_HEAD(lv_chain);
94516924853SDave Chinner 	list_add(&vec.lv_list, &lv_chain);
9462ce82b72SDave Chinner 
9472ce82b72SDave Chinner 	if (xlog_is_shutdown(log))
9482ce82b72SDave Chinner 		return -EIO;
9492ce82b72SDave Chinner 
95068a74dcaSDave Chinner 	error = xlog_cil_order_write(ctx->cil, ctx->sequence, _COMMIT_RECORD);
95168a74dcaSDave Chinner 	if (error)
95268a74dcaSDave Chinner 		return error;
95368a74dcaSDave Chinner 
95454021b62SDave Chinner 	/* account for space used by record data */
95554021b62SDave Chinner 	ctx->ticket->t_curr_res -= reg.i_len;
95616924853SDave Chinner 	error = xlog_write(log, ctx, &lv_chain, ctx->ticket, reg.i_len);
9572ce82b72SDave Chinner 	if (error)
958b5f17becSDave Chinner 		xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
9592ce82b72SDave Chinner 	return error;
9602ce82b72SDave Chinner }
9612ce82b72SDave Chinner 
962735fbf67SDave Chinner struct xlog_cil_trans_hdr {
9636eaed95eSDave Chinner 	struct xlog_op_header	oph[2];
964735fbf67SDave Chinner 	struct xfs_trans_header	thdr;
9656eaed95eSDave Chinner 	struct xfs_log_iovec	lhdr[2];
966735fbf67SDave Chinner };
967735fbf67SDave Chinner 
968735fbf67SDave Chinner /*
969735fbf67SDave Chinner  * Build a checkpoint transaction header to begin the journal transaction.  We
970735fbf67SDave Chinner  * need to account for the space used by the transaction header here as it is
971735fbf67SDave Chinner  * not accounted for in xlog_write().
9726eaed95eSDave Chinner  *
9736eaed95eSDave Chinner  * This is the only place we write a transaction header, so we also build the
9746eaed95eSDave Chinner  * log opheaders that indicate the start of a log transaction and wrap the
9756eaed95eSDave Chinner  * transaction header. We keep the start record in it's own log vector rather
9766eaed95eSDave Chinner  * than compacting them into a single region as this ends up making the logic
9776eaed95eSDave Chinner  * in xlog_write() for handling empty opheaders for start, commit and unmount
9786eaed95eSDave Chinner  * records much simpler.
979735fbf67SDave Chinner  */
980735fbf67SDave Chinner static void
xlog_cil_build_trans_hdr(struct xfs_cil_ctx * ctx,struct xlog_cil_trans_hdr * hdr,struct xfs_log_vec * lvhdr,int num_iovecs)981735fbf67SDave Chinner xlog_cil_build_trans_hdr(
982735fbf67SDave Chinner 	struct xfs_cil_ctx	*ctx,
983735fbf67SDave Chinner 	struct xlog_cil_trans_hdr *hdr,
984735fbf67SDave Chinner 	struct xfs_log_vec	*lvhdr,
985735fbf67SDave Chinner 	int			num_iovecs)
986735fbf67SDave Chinner {
987735fbf67SDave Chinner 	struct xlog_ticket	*tic = ctx->ticket;
9886eaed95eSDave Chinner 	__be32			tid = cpu_to_be32(tic->t_tid);
989735fbf67SDave Chinner 
990735fbf67SDave Chinner 	memset(hdr, 0, sizeof(*hdr));
991735fbf67SDave Chinner 
9926eaed95eSDave Chinner 	/* Log start record */
9936eaed95eSDave Chinner 	hdr->oph[0].oh_tid = tid;
9946eaed95eSDave Chinner 	hdr->oph[0].oh_clientid = XFS_TRANSACTION;
9956eaed95eSDave Chinner 	hdr->oph[0].oh_flags = XLOG_START_TRANS;
9966eaed95eSDave Chinner 
9976eaed95eSDave Chinner 	/* log iovec region pointer */
9986eaed95eSDave Chinner 	hdr->lhdr[0].i_addr = &hdr->oph[0];
9996eaed95eSDave Chinner 	hdr->lhdr[0].i_len = sizeof(struct xlog_op_header);
10006eaed95eSDave Chinner 	hdr->lhdr[0].i_type = XLOG_REG_TYPE_LRHEADER;
10016eaed95eSDave Chinner 
10026eaed95eSDave Chinner 	/* log opheader */
10036eaed95eSDave Chinner 	hdr->oph[1].oh_tid = tid;
10046eaed95eSDave Chinner 	hdr->oph[1].oh_clientid = XFS_TRANSACTION;
10056eaed95eSDave Chinner 	hdr->oph[1].oh_len = cpu_to_be32(sizeof(struct xfs_trans_header));
10066eaed95eSDave Chinner 
10076eaed95eSDave Chinner 	/* transaction header in host byte order format */
1008735fbf67SDave Chinner 	hdr->thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
1009735fbf67SDave Chinner 	hdr->thdr.th_type = XFS_TRANS_CHECKPOINT;
1010735fbf67SDave Chinner 	hdr->thdr.th_tid = tic->t_tid;
1011735fbf67SDave Chinner 	hdr->thdr.th_num_items = num_iovecs;
1012735fbf67SDave Chinner 
10136eaed95eSDave Chinner 	/* log iovec region pointer */
10146eaed95eSDave Chinner 	hdr->lhdr[1].i_addr = &hdr->oph[1];
10156eaed95eSDave Chinner 	hdr->lhdr[1].i_len = sizeof(struct xlog_op_header) +
10166eaed95eSDave Chinner 				sizeof(struct xfs_trans_header);
10176eaed95eSDave Chinner 	hdr->lhdr[1].i_type = XLOG_REG_TYPE_TRANSHDR;
10186eaed95eSDave Chinner 
10196eaed95eSDave Chinner 	lvhdr->lv_niovecs = 2;
10206eaed95eSDave Chinner 	lvhdr->lv_iovecp = &hdr->lhdr[0];
1021d80fc291SDave Chinner 	lvhdr->lv_bytes = hdr->lhdr[0].i_len + hdr->lhdr[1].i_len;
1022d80fc291SDave Chinner 
1023d80fc291SDave Chinner 	tic->t_curr_res -= lvhdr->lv_bytes;
1024735fbf67SDave Chinner }
1025735fbf67SDave Chinner 
10262ce82b72SDave Chinner /*
1027016a2338SDave Chinner  * CIL item reordering compare function. We want to order in ascending ID order,
1028016a2338SDave Chinner  * but we want to leave items with the same ID in the order they were added to
1029016a2338SDave Chinner  * the list. This is important for operations like reflink where we log 4 order
1030016a2338SDave Chinner  * dependent intents in a single transaction when we overwrite an existing
1031016a2338SDave Chinner  * shared extent with a new shared extent. i.e. BUI(unmap), CUI(drop),
1032016a2338SDave Chinner  * CUI (inc), BUI(remap)...
1033016a2338SDave Chinner  */
1034016a2338SDave Chinner static int
xlog_cil_order_cmp(void * priv,const struct list_head * a,const struct list_head * b)1035016a2338SDave Chinner xlog_cil_order_cmp(
1036016a2338SDave Chinner 	void			*priv,
1037016a2338SDave Chinner 	const struct list_head	*a,
1038016a2338SDave Chinner 	const struct list_head	*b)
1039016a2338SDave Chinner {
10404eb56069SDave Chinner 	struct xfs_log_vec	*l1 = container_of(a, struct xfs_log_vec, lv_list);
10414eb56069SDave Chinner 	struct xfs_log_vec	*l2 = container_of(b, struct xfs_log_vec, lv_list);
1042016a2338SDave Chinner 
10434eb56069SDave Chinner 	return l1->lv_order_id > l2->lv_order_id;
1044016a2338SDave Chinner }
1045016a2338SDave Chinner 
1046016a2338SDave Chinner /*
104722b1afc5SDave Chinner  * Pull all the log vectors off the items in the CIL, and remove the items from
104822b1afc5SDave Chinner  * the CIL. We don't need the CIL lock here because it's only needed on the
104922b1afc5SDave Chinner  * transaction commit side which is currently locked out by the flush lock.
10500d227466SDave Chinner  *
10510d227466SDave Chinner  * If a log item is marked with a whiteout, we do not need to write it to the
10520d227466SDave Chinner  * journal and so we just move them to the whiteout list for the caller to
10530d227466SDave Chinner  * dispose of appropriately.
105422b1afc5SDave Chinner  */
105522b1afc5SDave Chinner static void
xlog_cil_build_lv_chain(struct xfs_cil_ctx * ctx,struct list_head * whiteouts,uint32_t * num_iovecs,uint32_t * num_bytes)105622b1afc5SDave Chinner xlog_cil_build_lv_chain(
105722b1afc5SDave Chinner 	struct xfs_cil_ctx	*ctx,
10580d227466SDave Chinner 	struct list_head	*whiteouts,
105922b1afc5SDave Chinner 	uint32_t		*num_iovecs,
106022b1afc5SDave Chinner 	uint32_t		*num_bytes)
106122b1afc5SDave Chinner {
1062c0fb4765SDave Chinner 	while (!list_empty(&ctx->log_items)) {
106322b1afc5SDave Chinner 		struct xfs_log_item	*item;
106416924853SDave Chinner 		struct xfs_log_vec	*lv;
106522b1afc5SDave Chinner 
1066c0fb4765SDave Chinner 		item = list_first_entry(&ctx->log_items,
106722b1afc5SDave Chinner 					struct xfs_log_item, li_cil);
10680d227466SDave Chinner 
10690d227466SDave Chinner 		if (test_bit(XFS_LI_WHITEOUT, &item->li_flags)) {
10700d227466SDave Chinner 			list_move(&item->li_cil, whiteouts);
10710d227466SDave Chinner 			trace_xfs_cil_whiteout_skip(item);
10720d227466SDave Chinner 			continue;
10730d227466SDave Chinner 		}
10740d227466SDave Chinner 
107522b1afc5SDave Chinner 		lv = item->li_lv;
10764eb56069SDave Chinner 		lv->lv_order_id = item->li_order_id;
107722b1afc5SDave Chinner 
107822b1afc5SDave Chinner 		/* we don't write ordered log vectors */
107922b1afc5SDave Chinner 		if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
108022b1afc5SDave Chinner 			*num_bytes += lv->lv_bytes;
108116924853SDave Chinner 		*num_iovecs += lv->lv_niovecs;
108216924853SDave Chinner 		list_add_tail(&lv->lv_list, &ctx->lv_chain);
108316924853SDave Chinner 
108416924853SDave Chinner 		list_del_init(&item->li_cil);
108516924853SDave Chinner 		item->li_order_id = 0;
108616924853SDave Chinner 		item->li_lv = NULL;
108722b1afc5SDave Chinner 	}
108822b1afc5SDave Chinner }
108922b1afc5SDave Chinner 
10900d227466SDave Chinner static void
xlog_cil_cleanup_whiteouts(struct list_head * whiteouts)10910d227466SDave Chinner xlog_cil_cleanup_whiteouts(
10920d227466SDave Chinner 	struct list_head	*whiteouts)
10930d227466SDave Chinner {
10940d227466SDave Chinner 	while (!list_empty(whiteouts)) {
10950d227466SDave Chinner 		struct xfs_log_item *item = list_first_entry(whiteouts,
10960d227466SDave Chinner 						struct xfs_log_item, li_cil);
10970d227466SDave Chinner 		list_del_init(&item->li_cil);
10980d227466SDave Chinner 		trace_xfs_cil_whiteout_unpin(item);
10990d227466SDave Chinner 		item->li_ops->iop_unpin(item, 1);
11000d227466SDave Chinner 	}
11010d227466SDave Chinner }
11020d227466SDave Chinner 
110371e330b5SDave Chinner /*
1104c7cc296dSChristoph Hellwig  * Push the Committed Item List to the log.
1105c7cc296dSChristoph Hellwig  *
1106c7cc296dSChristoph Hellwig  * If the current sequence is the same as xc_push_seq we need to do a flush. If
1107c7cc296dSChristoph Hellwig  * xc_push_seq is less than the current sequence, then it has already been
1108a44f13edSDave Chinner  * flushed and we don't need to do anything - the caller will wait for it to
1109a44f13edSDave Chinner  * complete if necessary.
1110a44f13edSDave Chinner  *
1111c7cc296dSChristoph Hellwig  * xc_push_seq is checked unlocked against the sequence number for a match.
1112c7cc296dSChristoph Hellwig  * Hence we can allow log forces to run racily and not issue pushes for the
1113c7cc296dSChristoph Hellwig  * same sequence twice.  If we get a race between multiple pushes for the same
1114c7cc296dSChristoph Hellwig  * sequence they will block on the first one and then abort, hence avoiding
1115c7cc296dSChristoph Hellwig  * needless pushes.
111671e330b5SDave Chinner  */
1117c7cc296dSChristoph Hellwig static void
xlog_cil_push_work(struct work_struct * work)1118c7cc296dSChristoph Hellwig xlog_cil_push_work(
1119c7cc296dSChristoph Hellwig 	struct work_struct	*work)
112071e330b5SDave Chinner {
112139823d0fSDave Chinner 	struct xfs_cil_ctx	*ctx =
112239823d0fSDave Chinner 		container_of(work, struct xfs_cil_ctx, push_work);
112339823d0fSDave Chinner 	struct xfs_cil		*cil = ctx->cil;
1124c7cc296dSChristoph Hellwig 	struct xlog		*log = cil->xc_log;
112571e330b5SDave Chinner 	struct xfs_cil_ctx	*new_ctx;
1126d80fc291SDave Chinner 	int			num_iovecs = 0;
1127d80fc291SDave Chinner 	int			num_bytes = 0;
112871e330b5SDave Chinner 	int			error = 0;
1129735fbf67SDave Chinner 	struct xlog_cil_trans_hdr thdr;
113016924853SDave Chinner 	struct xfs_log_vec	lvhdr = {};
11310dc8f7f1SDave Chinner 	xfs_csn_t		push_seq;
11320020a190SDave Chinner 	bool			push_commit_stable;
11330d227466SDave Chinner 	LIST_HEAD		(whiteouts);
1134d9f68777SDave Chinner 	struct xlog_ticket	*ticket;
113571e330b5SDave Chinner 
113639823d0fSDave Chinner 	new_ctx = xlog_cil_ctx_alloc();
113771e330b5SDave Chinner 	new_ctx->ticket = xlog_cil_ticket_alloc(log);
113871e330b5SDave Chinner 
113971e330b5SDave Chinner 	down_write(&cil->xc_ctx_lock);
114071e330b5SDave Chinner 
11414bb928cdSDave Chinner 	spin_lock(&cil->xc_push_lock);
11424c2d542fSDave Chinner 	push_seq = cil->xc_push_seq;
11434c2d542fSDave Chinner 	ASSERT(push_seq <= ctx->sequence);
11440020a190SDave Chinner 	push_commit_stable = cil->xc_push_commit_stable;
11450020a190SDave Chinner 	cil->xc_push_commit_stable = false;
114671e330b5SDave Chinner 
11474c2d542fSDave Chinner 	/*
114819f4e7ccSDave Chinner 	 * As we are about to switch to a new, empty CIL context, we no longer
114919f4e7ccSDave Chinner 	 * need to throttle tasks on CIL space overruns. Wake any waiters that
115019f4e7ccSDave Chinner 	 * the hard push throttle may have caught so they can start committing
115119f4e7ccSDave Chinner 	 * to the new context. The ctx->xc_push_lock provides the serialisation
115219f4e7ccSDave Chinner 	 * necessary for safely using the lockless waitqueue_active() check in
115319f4e7ccSDave Chinner 	 * this context.
11540e7ab7efSDave Chinner 	 */
115519f4e7ccSDave Chinner 	if (waitqueue_active(&cil->xc_push_wait))
1156c7f87f39SDave Chinner 		wake_up_all(&cil->xc_push_wait);
11570e7ab7efSDave Chinner 
11587c8ade21SDave Chinner 	xlog_cil_push_pcp_aggregate(cil, ctx);
11597c8ade21SDave Chinner 
11600e7ab7efSDave Chinner 	/*
11614c2d542fSDave Chinner 	 * Check if we've anything to push. If there is nothing, then we don't
11624c2d542fSDave Chinner 	 * move on to a new sequence number and so we have to be able to push
11634c2d542fSDave Chinner 	 * this sequence again later.
11644c2d542fSDave Chinner 	 */
116588591e7fSDave Chinner 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) {
11664c2d542fSDave Chinner 		cil->xc_push_seq = 0;
11674bb928cdSDave Chinner 		spin_unlock(&cil->xc_push_lock);
1168a44f13edSDave Chinner 		goto out_skip;
11694c2d542fSDave Chinner 	}
11704c2d542fSDave Chinner 
1171a44f13edSDave Chinner 
1172cf085a1bSJoe Perches 	/* check for a previously pushed sequence */
117339823d0fSDave Chinner 	if (push_seq < ctx->sequence) {
11748af3dcd3SDave Chinner 		spin_unlock(&cil->xc_push_lock);
1175df806158SDave Chinner 		goto out_skip;
11768af3dcd3SDave Chinner 	}
11778af3dcd3SDave Chinner 
11788af3dcd3SDave Chinner 	/*
11798af3dcd3SDave Chinner 	 * We are now going to push this context, so add it to the committing
11808af3dcd3SDave Chinner 	 * list before we do anything else. This ensures that anyone waiting on
11818af3dcd3SDave Chinner 	 * this push can easily detect the difference between a "push in
11828af3dcd3SDave Chinner 	 * progress" and "CIL is empty, nothing to do".
11838af3dcd3SDave Chinner 	 *
11848af3dcd3SDave Chinner 	 * IOWs, a wait loop can now check for:
11858af3dcd3SDave Chinner 	 *	the current sequence not being found on the committing list;
11868af3dcd3SDave Chinner 	 *	an empty CIL; and
11878af3dcd3SDave Chinner 	 *	an unchanged sequence number
11888af3dcd3SDave Chinner 	 * to detect a push that had nothing to do and therefore does not need
11898af3dcd3SDave Chinner 	 * waiting on. If the CIL is not empty, we get put on the committing
11908af3dcd3SDave Chinner 	 * list before emptying the CIL and bumping the sequence number. Hence
11918af3dcd3SDave Chinner 	 * an empty CIL and an unchanged sequence number means we jumped out
11928af3dcd3SDave Chinner 	 * above after doing nothing.
11938af3dcd3SDave Chinner 	 *
11948af3dcd3SDave Chinner 	 * Hence the waiter will either find the commit sequence on the
11958af3dcd3SDave Chinner 	 * committing list or the sequence number will be unchanged and the CIL
11968af3dcd3SDave Chinner 	 * still dirty. In that latter case, the push has not yet started, and
11978af3dcd3SDave Chinner 	 * so the waiter will have to continue trying to check the CIL
11988af3dcd3SDave Chinner 	 * committing list until it is found. In extreme cases of delay, the
11998af3dcd3SDave Chinner 	 * sequence may fully commit between the attempts the wait makes to wait
12008af3dcd3SDave Chinner 	 * on the commit sequence.
12018af3dcd3SDave Chinner 	 */
12028af3dcd3SDave Chinner 	list_add(&ctx->committing, &cil->xc_committing);
12038af3dcd3SDave Chinner 	spin_unlock(&cil->xc_push_lock);
1204df806158SDave Chinner 
1205c0fb4765SDave Chinner 	xlog_cil_build_lv_chain(ctx, &whiteouts, &num_iovecs, &num_bytes);
120671e330b5SDave Chinner 
120771e330b5SDave Chinner 	/*
120839823d0fSDave Chinner 	 * Switch the contexts so we can drop the context lock and move out
120971e330b5SDave Chinner 	 * of a shared context. We can't just go straight to the commit record,
121071e330b5SDave Chinner 	 * though - we need to synchronise with previous and future commits so
121171e330b5SDave Chinner 	 * that the commit records are correctly ordered in the log to ensure
121271e330b5SDave Chinner 	 * that we process items during log IO completion in the correct order.
121371e330b5SDave Chinner 	 *
121471e330b5SDave Chinner 	 * For example, if we get an EFI in one checkpoint and the EFD in the
121571e330b5SDave Chinner 	 * next (e.g. due to log forces), we do not want the checkpoint with
121671e330b5SDave Chinner 	 * the EFD to be committed before the checkpoint with the EFI.  Hence
121771e330b5SDave Chinner 	 * we must strictly order the commit records of the checkpoints so
121871e330b5SDave Chinner 	 * that: a) the checkpoint callbacks are attached to the iclogs in the
121971e330b5SDave Chinner 	 * correct order; and b) the checkpoints are replayed in correct order
122071e330b5SDave Chinner 	 * in log recovery.
122171e330b5SDave Chinner 	 *
122271e330b5SDave Chinner 	 * Hence we need to add this context to the committing context list so
122371e330b5SDave Chinner 	 * that higher sequences will wait for us to write out a commit record
122471e330b5SDave Chinner 	 * before they do.
1225f876e446SDave Chinner 	 *
12265f9b4b0dSDave Chinner 	 * xfs_log_force_seq requires us to mirror the new sequence into the cil
1227f876e446SDave Chinner 	 * structure atomically with the addition of this sequence to the
1228f876e446SDave Chinner 	 * committing list. This also ensures that we can do unlocked checks
1229f876e446SDave Chinner 	 * against the current sequence in log forces without risking
1230f876e446SDave Chinner 	 * deferencing a freed context pointer.
123171e330b5SDave Chinner 	 */
12324bb928cdSDave Chinner 	spin_lock(&cil->xc_push_lock);
123339823d0fSDave Chinner 	xlog_cil_ctx_switch(cil, new_ctx);
12344bb928cdSDave Chinner 	spin_unlock(&cil->xc_push_lock);
123571e330b5SDave Chinner 	up_write(&cil->xc_ctx_lock);
123671e330b5SDave Chinner 
123771e330b5SDave Chinner 	/*
12384eb56069SDave Chinner 	 * Sort the log vector chain before we add the transaction headers.
12394eb56069SDave Chinner 	 * This ensures we always have the transaction headers at the start
12404eb56069SDave Chinner 	 * of the chain.
12414eb56069SDave Chinner 	 */
12424eb56069SDave Chinner 	list_sort(NULL, &ctx->lv_chain, xlog_cil_order_cmp);
12434eb56069SDave Chinner 
12444eb56069SDave Chinner 	/*
124571e330b5SDave Chinner 	 * Build a checkpoint transaction header and write it to the log to
124671e330b5SDave Chinner 	 * begin the transaction. We need to account for the space used by the
124771e330b5SDave Chinner 	 * transaction header here as it is not accounted for in xlog_write().
124816924853SDave Chinner 	 * Add the lvhdr to the head of the lv chain we pass to xlog_write() so
124916924853SDave Chinner 	 * it gets written into the iclog first.
125071e330b5SDave Chinner 	 */
1251735fbf67SDave Chinner 	xlog_cil_build_trans_hdr(ctx, &thdr, &lvhdr, num_iovecs);
1252d80fc291SDave Chinner 	num_bytes += lvhdr.lv_bytes;
125316924853SDave Chinner 	list_add(&lvhdr.lv_list, &ctx->lv_chain);
125471e330b5SDave Chinner 
125516924853SDave Chinner 	/*
125616924853SDave Chinner 	 * Take the lvhdr back off the lv_chain immediately after calling
125716924853SDave Chinner 	 * xlog_cil_write_chain() as it should not be passed to log IO
125816924853SDave Chinner 	 * completion.
125916924853SDave Chinner 	 */
126016924853SDave Chinner 	error = xlog_cil_write_chain(ctx, num_bytes);
126116924853SDave Chinner 	list_del(&lvhdr.lv_list);
1262bf034bc8SDave Chinner 	if (error)
1263ac983517SDave Chinner 		goto out_abort_free_ticket;
126471e330b5SDave Chinner 
1265caa80090SDave Chinner 	error = xlog_cil_write_commit_record(ctx);
1266dd401770SDave Chinner 	if (error)
1267dd401770SDave Chinner 		goto out_abort_free_ticket;
1268dd401770SDave Chinner 
1269d9f68777SDave Chinner 	/*
1270d9f68777SDave Chinner 	 * Grab the ticket from the ctx so we can ungrant it after releasing the
1271d9f68777SDave Chinner 	 * commit_iclog. The ctx may be freed by the time we return from
1272d9f68777SDave Chinner 	 * releasing the commit_iclog (i.e. checkpoint has been completed and
1273d9f68777SDave Chinner 	 * callback run) so we can't reference the ctx after the call to
1274d9f68777SDave Chinner 	 * xlog_state_release_iclog().
1275d9f68777SDave Chinner 	 */
1276d9f68777SDave Chinner 	ticket = ctx->ticket;
127771e330b5SDave Chinner 
1278a1bb8505SDave Chinner 	/*
12791effb72aSDave Chinner 	 * If the checkpoint spans multiple iclogs, wait for all previous iclogs
12801effb72aSDave Chinner 	 * to complete before we submit the commit_iclog. We can't use state
12811effb72aSDave Chinner 	 * checks for this - ACTIVE can be either a past completed iclog or a
12821effb72aSDave Chinner 	 * future iclog being filled, while WANT_SYNC through SYNC_DONE can be a
12831effb72aSDave Chinner 	 * past or future iclog awaiting IO or ordered IO completion to be run.
12841effb72aSDave Chinner 	 * In the latter case, if it's a future iclog and we wait on it, the we
12851effb72aSDave Chinner 	 * will hang because it won't get processed through to ic_force_wait
12861effb72aSDave Chinner 	 * wakeup until this commit_iclog is written to disk.  Hence we use the
12871effb72aSDave Chinner 	 * iclog header lsn and compare it to the commit lsn to determine if we
12881effb72aSDave Chinner 	 * need to wait on iclogs or not.
1289a79b28c2SDave Chinner 	 */
1290caa80090SDave Chinner 	spin_lock(&log->l_icloglock);
1291c45aba40SDave Chinner 	if (ctx->start_lsn != ctx->commit_lsn) {
12921effb72aSDave Chinner 		xfs_lsn_t	plsn;
12931effb72aSDave Chinner 
1294caa80090SDave Chinner 		plsn = be64_to_cpu(ctx->commit_iclog->ic_prev->ic_header.h_lsn);
1295c45aba40SDave Chinner 		if (plsn && XFS_LSN_CMP(plsn, ctx->commit_lsn) < 0) {
12961effb72aSDave Chinner 			/*
12971effb72aSDave Chinner 			 * Waiting on ic_force_wait orders the completion of
12981effb72aSDave Chinner 			 * iclogs older than ic_prev. Hence we only need to wait
12991effb72aSDave Chinner 			 * on the most recent older iclog here.
13001effb72aSDave Chinner 			 */
1301caa80090SDave Chinner 			xlog_wait_on_iclog(ctx->commit_iclog->ic_prev);
1302eef983ffSDave Chinner 			spin_lock(&log->l_icloglock);
13031effb72aSDave Chinner 		}
13041effb72aSDave Chinner 
13051effb72aSDave Chinner 		/*
13061effb72aSDave Chinner 		 * We need to issue a pre-flush so that the ordering for this
13071effb72aSDave Chinner 		 * checkpoint is correctly preserved down to stable storage.
13081effb72aSDave Chinner 		 */
1309caa80090SDave Chinner 		ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
1310a79b28c2SDave Chinner 	}
1311a79b28c2SDave Chinner 
1312eef983ffSDave Chinner 	/*
1313eef983ffSDave Chinner 	 * The commit iclog must be written to stable storage to guarantee
1314eef983ffSDave Chinner 	 * journal IO vs metadata writeback IO is correctly ordered on stable
1315eef983ffSDave Chinner 	 * storage.
13160020a190SDave Chinner 	 *
13170020a190SDave Chinner 	 * If the push caller needs the commit to be immediately stable and the
13180020a190SDave Chinner 	 * commit_iclog is not yet marked as XLOG_STATE_WANT_SYNC to indicate it
13190020a190SDave Chinner 	 * will be written when released, switch it's state to WANT_SYNC right
13200020a190SDave Chinner 	 * now.
1321eef983ffSDave Chinner 	 */
1322caa80090SDave Chinner 	ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FUA;
13230020a190SDave Chinner 	if (push_commit_stable &&
13240020a190SDave Chinner 	    ctx->commit_iclog->ic_state == XLOG_STATE_ACTIVE)
13250020a190SDave Chinner 		xlog_state_switch_iclogs(log, ctx->commit_iclog, 0);
1326d9f68777SDave Chinner 	ticket = ctx->ticket;
1327d9f68777SDave Chinner 	xlog_state_release_iclog(log, ctx->commit_iclog, ticket);
1328502a01faSDave Chinner 
1329502a01faSDave Chinner 	/* Not safe to reference ctx now! */
1330502a01faSDave Chinner 
1331eef983ffSDave Chinner 	spin_unlock(&log->l_icloglock);
13320d227466SDave Chinner 	xlog_cil_cleanup_whiteouts(&whiteouts);
1333d9f68777SDave Chinner 	xfs_log_ticket_ungrant(log, ticket);
1334c7cc296dSChristoph Hellwig 	return;
133571e330b5SDave Chinner 
133671e330b5SDave Chinner out_skip:
133771e330b5SDave Chinner 	up_write(&cil->xc_ctx_lock);
133871e330b5SDave Chinner 	xfs_log_ticket_put(new_ctx->ticket);
133971e330b5SDave Chinner 	kmem_free(new_ctx);
1340c7cc296dSChristoph Hellwig 	return;
134171e330b5SDave Chinner 
13427db37c5eSDave Chinner out_abort_free_ticket:
13432039a272SDave Chinner 	ASSERT(xlog_is_shutdown(log));
13440d227466SDave Chinner 	xlog_cil_cleanup_whiteouts(&whiteouts);
1345caa80090SDave Chinner 	if (!ctx->commit_iclog) {
1346d9f68777SDave Chinner 		xfs_log_ticket_ungrant(log, ctx->ticket);
134712e6a0f4SChristoph Hellwig 		xlog_cil_committed(ctx);
1348caa80090SDave Chinner 		return;
1349caa80090SDave Chinner 	}
1350caa80090SDave Chinner 	spin_lock(&log->l_icloglock);
1351d9f68777SDave Chinner 	ticket = ctx->ticket;
1352d9f68777SDave Chinner 	xlog_state_release_iclog(log, ctx->commit_iclog, ticket);
1353caa80090SDave Chinner 	/* Not safe to reference ctx now! */
1354caa80090SDave Chinner 	spin_unlock(&log->l_icloglock);
1355d9f68777SDave Chinner 	xfs_log_ticket_ungrant(log, ticket);
13564c2d542fSDave Chinner }
13574c2d542fSDave Chinner 
13584c2d542fSDave Chinner /*
13594c2d542fSDave Chinner  * We need to push CIL every so often so we don't cache more than we can fit in
13604c2d542fSDave Chinner  * the log. The limit really is that a checkpoint can't be more than half the
13614c2d542fSDave Chinner  * log (the current checkpoint is not allowed to overwrite the previous
13624c2d542fSDave Chinner  * checkpoint), but commit latency and memory usage limit this to a smaller
13634c2d542fSDave Chinner  * size.
13644c2d542fSDave Chinner  */
13654c2d542fSDave Chinner static void
xlog_cil_push_background(struct xlog * log)13664c2d542fSDave Chinner xlog_cil_push_background(
13670e7ab7efSDave Chinner 	struct xlog	*log) __releases(cil->xc_ctx_lock)
13684c2d542fSDave Chinner {
13694c2d542fSDave Chinner 	struct xfs_cil	*cil = log->l_cilp;
13707c8ade21SDave Chinner 	int		space_used = atomic_read(&cil->xc_ctx->space_used);
13714c2d542fSDave Chinner 
13724c2d542fSDave Chinner 	/*
13734c2d542fSDave Chinner 	 * The cil won't be empty because we are called while holding the
137488591e7fSDave Chinner 	 * context lock so whatever we added to the CIL will still be there.
13754c2d542fSDave Chinner 	 */
137688591e7fSDave Chinner 	ASSERT(!test_bit(XLOG_CIL_EMPTY, &cil->xc_flags));
13774c2d542fSDave Chinner 
13784c2d542fSDave Chinner 	/*
13791ccb0745SDave Chinner 	 * We are done if:
13801ccb0745SDave Chinner 	 * - we haven't used up all the space available yet; or
13811ccb0745SDave Chinner 	 * - we've already queued up a push; and
13821ccb0745SDave Chinner 	 * - we're not over the hard limit; and
13831ccb0745SDave Chinner 	 * - nothing has been over the hard limit.
13841ccb0745SDave Chinner 	 *
13851ccb0745SDave Chinner 	 * If so, we don't need to take the push lock as there's nothing to do.
13864c2d542fSDave Chinner 	 */
13871ccb0745SDave Chinner 	if (space_used < XLOG_CIL_SPACE_LIMIT(log) ||
13881ccb0745SDave Chinner 	    (cil->xc_push_seq == cil->xc_current_sequence &&
13891ccb0745SDave Chinner 	     space_used < XLOG_CIL_BLOCKING_SPACE_LIMIT(log) &&
13901ccb0745SDave Chinner 	     !waitqueue_active(&cil->xc_push_wait))) {
13910e7ab7efSDave Chinner 		up_read(&cil->xc_ctx_lock);
13924c2d542fSDave Chinner 		return;
13930e7ab7efSDave Chinner 	}
13944c2d542fSDave Chinner 
13954bb928cdSDave Chinner 	spin_lock(&cil->xc_push_lock);
13964c2d542fSDave Chinner 	if (cil->xc_push_seq < cil->xc_current_sequence) {
13974c2d542fSDave Chinner 		cil->xc_push_seq = cil->xc_current_sequence;
139833c0dd78SDave Chinner 		queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
13994c2d542fSDave Chinner 	}
14000e7ab7efSDave Chinner 
14010e7ab7efSDave Chinner 	/*
14020e7ab7efSDave Chinner 	 * Drop the context lock now, we can't hold that if we need to sleep
14030e7ab7efSDave Chinner 	 * because we are over the blocking threshold. The push_lock is still
14040e7ab7efSDave Chinner 	 * held, so blocking threshold sleep/wakeup is still correctly
14050e7ab7efSDave Chinner 	 * serialised here.
14060e7ab7efSDave Chinner 	 */
14070e7ab7efSDave Chinner 	up_read(&cil->xc_ctx_lock);
14080e7ab7efSDave Chinner 
14090e7ab7efSDave Chinner 	/*
14100e7ab7efSDave Chinner 	 * If we are well over the space limit, throttle the work that is being
141119f4e7ccSDave Chinner 	 * done until the push work on this context has begun. Enforce the hard
141219f4e7ccSDave Chinner 	 * throttle on all transaction commits once it has been activated, even
141319f4e7ccSDave Chinner 	 * if the committing transactions have resulted in the space usage
141419f4e7ccSDave Chinner 	 * dipping back down under the hard limit.
141519f4e7ccSDave Chinner 	 *
141619f4e7ccSDave Chinner 	 * The ctx->xc_push_lock provides the serialisation necessary for safely
14177c8ade21SDave Chinner 	 * calling xlog_cil_over_hard_limit() in this context.
14180e7ab7efSDave Chinner 	 */
14197c8ade21SDave Chinner 	if (xlog_cil_over_hard_limit(log, space_used)) {
14200e7ab7efSDave Chinner 		trace_xfs_log_cil_wait(log, cil->xc_ctx->ticket);
14217c8ade21SDave Chinner 		ASSERT(space_used < log->l_logsize);
1422c7f87f39SDave Chinner 		xlog_wait(&cil->xc_push_wait, &cil->xc_push_lock);
14230e7ab7efSDave Chinner 		return;
14240e7ab7efSDave Chinner 	}
14250e7ab7efSDave Chinner 
14264bb928cdSDave Chinner 	spin_unlock(&cil->xc_push_lock);
14274c2d542fSDave Chinner 
14284c2d542fSDave Chinner }
14294c2d542fSDave Chinner 
1430f876e446SDave Chinner /*
1431f876e446SDave Chinner  * xlog_cil_push_now() is used to trigger an immediate CIL push to the sequence
1432f876e446SDave Chinner  * number that is passed. When it returns, the work will be queued for
14330020a190SDave Chinner  * @push_seq, but it won't be completed.
14340020a190SDave Chinner  *
14350020a190SDave Chinner  * If the caller is performing a synchronous force, we will flush the workqueue
14360020a190SDave Chinner  * to get previously queued work moving to minimise the wait time they will
14370020a190SDave Chinner  * undergo waiting for all outstanding pushes to complete. The caller is
14380020a190SDave Chinner  * expected to do the required waiting for push_seq to complete.
14390020a190SDave Chinner  *
14400020a190SDave Chinner  * If the caller is performing an async push, we need to ensure that the
14410020a190SDave Chinner  * checkpoint is fully flushed out of the iclogs when we finish the push. If we
14420020a190SDave Chinner  * don't do this, then the commit record may remain sitting in memory in an
14430020a190SDave Chinner  * ACTIVE iclog. This then requires another full log force to push to disk,
14440020a190SDave Chinner  * which defeats the purpose of having an async, non-blocking CIL force
14450020a190SDave Chinner  * mechanism. Hence in this case we need to pass a flag to the push work to
14460020a190SDave Chinner  * indicate it needs to flush the commit record itself.
1447f876e446SDave Chinner  */
14484c2d542fSDave Chinner static void
xlog_cil_push_now(struct xlog * log,xfs_lsn_t push_seq,bool async)1449f876e446SDave Chinner xlog_cil_push_now(
1450f7bdf03aSMark Tinguely 	struct xlog	*log,
14510020a190SDave Chinner 	xfs_lsn_t	push_seq,
14520020a190SDave Chinner 	bool		async)
14534c2d542fSDave Chinner {
14544c2d542fSDave Chinner 	struct xfs_cil	*cil = log->l_cilp;
14554c2d542fSDave Chinner 
14564c2d542fSDave Chinner 	if (!cil)
14574c2d542fSDave Chinner 		return;
14584c2d542fSDave Chinner 
14594c2d542fSDave Chinner 	ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
14604c2d542fSDave Chinner 
14614c2d542fSDave Chinner 	/* start on any pending background push to minimise wait time on it */
14620020a190SDave Chinner 	if (!async)
146333c0dd78SDave Chinner 		flush_workqueue(cil->xc_push_wq);
14644c2d542fSDave Chinner 
146570447e0aSDave Chinner 	spin_lock(&cil->xc_push_lock);
146670447e0aSDave Chinner 
146770447e0aSDave Chinner 	/*
146870447e0aSDave Chinner 	 * If this is an async flush request, we always need to set the
146970447e0aSDave Chinner 	 * xc_push_commit_stable flag even if something else has already queued
147070447e0aSDave Chinner 	 * a push. The flush caller is asking for the CIL to be on stable
147170447e0aSDave Chinner 	 * storage when the next push completes, so regardless of who has queued
147270447e0aSDave Chinner 	 * the push, the flush requires stable semantics from it.
147370447e0aSDave Chinner 	 */
147470447e0aSDave Chinner 	cil->xc_push_commit_stable = async;
147570447e0aSDave Chinner 
14764c2d542fSDave Chinner 	/*
14774c2d542fSDave Chinner 	 * If the CIL is empty or we've already pushed the sequence then
147870447e0aSDave Chinner 	 * there's no more work that we need to do.
14794c2d542fSDave Chinner 	 */
148088591e7fSDave Chinner 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags) ||
148188591e7fSDave Chinner 	    push_seq <= cil->xc_push_seq) {
14824bb928cdSDave Chinner 		spin_unlock(&cil->xc_push_lock);
14834c2d542fSDave Chinner 		return;
14844c2d542fSDave Chinner 	}
14854c2d542fSDave Chinner 
14864c2d542fSDave Chinner 	cil->xc_push_seq = push_seq;
148733c0dd78SDave Chinner 	queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
14884bb928cdSDave Chinner 	spin_unlock(&cil->xc_push_lock);
14894c2d542fSDave Chinner }
14904c2d542fSDave Chinner 
14912c6e24ceSDave Chinner bool
xlog_cil_empty(struct xlog * log)14922c6e24ceSDave Chinner xlog_cil_empty(
14932c6e24ceSDave Chinner 	struct xlog	*log)
14942c6e24ceSDave Chinner {
14952c6e24ceSDave Chinner 	struct xfs_cil	*cil = log->l_cilp;
14962c6e24ceSDave Chinner 	bool		empty = false;
14972c6e24ceSDave Chinner 
14982c6e24ceSDave Chinner 	spin_lock(&cil->xc_push_lock);
149988591e7fSDave Chinner 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
15002c6e24ceSDave Chinner 		empty = true;
15012c6e24ceSDave Chinner 	spin_unlock(&cil->xc_push_lock);
15022c6e24ceSDave Chinner 	return empty;
15032c6e24ceSDave Chinner }
15042c6e24ceSDave Chinner 
150571e330b5SDave Chinner /*
15060d227466SDave Chinner  * If there are intent done items in this transaction and the related intent was
15070d227466SDave Chinner  * committed in the current (same) CIL checkpoint, we don't need to write either
15080d227466SDave Chinner  * the intent or intent done item to the journal as the change will be
15090d227466SDave Chinner  * journalled atomically within this checkpoint. As we cannot remove items from
15100d227466SDave Chinner  * the CIL here, mark the related intent with a whiteout so that the CIL push
15110d227466SDave Chinner  * can remove it rather than writing it to the journal. Then remove the intent
15120d227466SDave Chinner  * done item from the current transaction and release it so it doesn't get put
15130d227466SDave Chinner  * into the CIL at all.
15140d227466SDave Chinner  */
15150d227466SDave Chinner static uint32_t
xlog_cil_process_intents(struct xfs_cil * cil,struct xfs_trans * tp)15160d227466SDave Chinner xlog_cil_process_intents(
15170d227466SDave Chinner 	struct xfs_cil		*cil,
15180d227466SDave Chinner 	struct xfs_trans	*tp)
15190d227466SDave Chinner {
15200d227466SDave Chinner 	struct xfs_log_item	*lip, *ilip, *next;
15210d227466SDave Chinner 	uint32_t		len = 0;
15220d227466SDave Chinner 
15230d227466SDave Chinner 	list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
15240d227466SDave Chinner 		if (!(lip->li_ops->flags & XFS_ITEM_INTENT_DONE))
15250d227466SDave Chinner 			continue;
15260d227466SDave Chinner 
15270d227466SDave Chinner 		ilip = lip->li_ops->iop_intent(lip);
15280d227466SDave Chinner 		if (!ilip || !xlog_item_in_current_chkpt(cil, ilip))
15290d227466SDave Chinner 			continue;
15300d227466SDave Chinner 		set_bit(XFS_LI_WHITEOUT, &ilip->li_flags);
15310d227466SDave Chinner 		trace_xfs_cil_whiteout_mark(ilip);
15320d227466SDave Chinner 		len += ilip->li_lv->lv_bytes;
15330d227466SDave Chinner 		kmem_free(ilip->li_lv);
15340d227466SDave Chinner 		ilip->li_lv = NULL;
15350d227466SDave Chinner 
15360d227466SDave Chinner 		xfs_trans_del_item(lip);
15370d227466SDave Chinner 		lip->li_ops->iop_release(lip);
15380d227466SDave Chinner 	}
15390d227466SDave Chinner 	return len;
15400d227466SDave Chinner }
15410d227466SDave Chinner 
15420d227466SDave Chinner /*
1543a44f13edSDave Chinner  * Commit a transaction with the given vector to the Committed Item List.
1544a44f13edSDave Chinner  *
1545a44f13edSDave Chinner  * To do this, we need to format the item, pin it in memory if required and
1546a44f13edSDave Chinner  * account for the space used by the transaction. Once we have done that we
1547a44f13edSDave Chinner  * need to release the unused reservation for the transaction, attach the
1548a44f13edSDave Chinner  * transaction to the checkpoint context so we carry the busy extents through
1549a44f13edSDave Chinner  * to checkpoint completion, and then unlock all the items in the transaction.
1550a44f13edSDave Chinner  *
1551a44f13edSDave Chinner  * Called with the context lock already held in read mode to lock out
1552a44f13edSDave Chinner  * background commit, returns without it held once background commits are
1553a44f13edSDave Chinner  * allowed again.
1554a44f13edSDave Chinner  */
1555c6f97264SJie Liu void
xlog_cil_commit(struct xlog * log,struct xfs_trans * tp,xfs_csn_t * commit_seq,bool regrant)15565f9b4b0dSDave Chinner xlog_cil_commit(
15575f9b4b0dSDave Chinner 	struct xlog		*log,
1558a44f13edSDave Chinner 	struct xfs_trans	*tp,
15595f9b4b0dSDave Chinner 	xfs_csn_t		*commit_seq,
156070393313SChristoph Hellwig 	bool			regrant)
1561a44f13edSDave Chinner {
1562991aaf65SDave Chinner 	struct xfs_cil		*cil = log->l_cilp;
1563195cd83dSChristoph Hellwig 	struct xfs_log_item	*lip, *next;
15640d227466SDave Chinner 	uint32_t		released_space = 0;
1565a44f13edSDave Chinner 
1566b1c5ebb2SDave Chinner 	/*
1567b1c5ebb2SDave Chinner 	 * Do all necessary memory allocation before we lock the CIL.
1568b1c5ebb2SDave Chinner 	 * This ensures the allocation does not deadlock with a CIL
1569b1c5ebb2SDave Chinner 	 * push in memory reclaim (e.g. from kswapd).
1570b1c5ebb2SDave Chinner 	 */
1571b1c5ebb2SDave Chinner 	xlog_cil_alloc_shadow_bufs(log, tp);
1572b1c5ebb2SDave Chinner 
1573f5baac35SDave Chinner 	/* lock out background commit */
1574991aaf65SDave Chinner 	down_read(&cil->xc_ctx_lock);
1575f5baac35SDave Chinner 
15760d227466SDave Chinner 	if (tp->t_flags & XFS_TRANS_HAS_INTENT_DONE)
15770d227466SDave Chinner 		released_space = xlog_cil_process_intents(cil, tp);
15780d227466SDave Chinner 
15790d227466SDave Chinner 	xlog_cil_insert_items(log, tp, released_space);
1580a44f13edSDave Chinner 
15812039a272SDave Chinner 	if (regrant && !xlog_is_shutdown(log))
15828b41e3f9SChristoph Hellwig 		xfs_log_ticket_regrant(log, tp->t_ticket);
15838b41e3f9SChristoph Hellwig 	else
15848b41e3f9SChristoph Hellwig 		xfs_log_ticket_ungrant(log, tp->t_ticket);
1585ba18781bSDave Chinner 	tp->t_ticket = NULL;
1586a44f13edSDave Chinner 	xfs_trans_unreserve_and_mod_sb(tp);
1587a44f13edSDave Chinner 
1588a44f13edSDave Chinner 	/*
1589a44f13edSDave Chinner 	 * Once all the items of the transaction have been copied to the CIL,
1590195cd83dSChristoph Hellwig 	 * the items can be unlocked and possibly freed.
1591a44f13edSDave Chinner 	 *
1592a44f13edSDave Chinner 	 * This needs to be done before we drop the CIL context lock because we
1593a44f13edSDave Chinner 	 * have to update state in the log items and unlock them before they go
1594a44f13edSDave Chinner 	 * to disk. If we don't, then the CIL checkpoint can race with us and
1595a44f13edSDave Chinner 	 * we can run checkpoint completion before we've updated and unlocked
1596a44f13edSDave Chinner 	 * the log items. This affects (at least) processing of stale buffers,
1597a44f13edSDave Chinner 	 * inodes and EFIs.
1598a44f13edSDave Chinner 	 */
1599195cd83dSChristoph Hellwig 	trace_xfs_trans_commit_items(tp, _RET_IP_);
1600195cd83dSChristoph Hellwig 	list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
1601195cd83dSChristoph Hellwig 		xfs_trans_del_item(lip);
1602195cd83dSChristoph Hellwig 		if (lip->li_ops->iop_committing)
16035f9b4b0dSDave Chinner 			lip->li_ops->iop_committing(lip, cil->xc_ctx->sequence);
1604195cd83dSChristoph Hellwig 	}
16055f9b4b0dSDave Chinner 	if (commit_seq)
16065f9b4b0dSDave Chinner 		*commit_seq = cil->xc_ctx->sequence;
1607a44f13edSDave Chinner 
16080e7ab7efSDave Chinner 	/* xlog_cil_push_background() releases cil->xc_ctx_lock */
16090e7ab7efSDave Chinner 	xlog_cil_push_background(log);
1610a44f13edSDave Chinner }
1611a44f13edSDave Chinner 
1612a44f13edSDave Chinner /*
16130020a190SDave Chinner  * Flush the CIL to stable storage but don't wait for it to complete. This
16140020a190SDave Chinner  * requires the CIL push to ensure the commit record for the push hits the disk,
16150020a190SDave Chinner  * but otherwise is no different to a push done from a log force.
16160020a190SDave Chinner  */
16170020a190SDave Chinner void
xlog_cil_flush(struct xlog * log)16180020a190SDave Chinner xlog_cil_flush(
16190020a190SDave Chinner 	struct xlog	*log)
16200020a190SDave Chinner {
16210020a190SDave Chinner 	xfs_csn_t	seq = log->l_cilp->xc_current_sequence;
16220020a190SDave Chinner 
16230020a190SDave Chinner 	trace_xfs_log_force(log->l_mp, seq, _RET_IP_);
16240020a190SDave Chinner 	xlog_cil_push_now(log, seq, true);
162570447e0aSDave Chinner 
162670447e0aSDave Chinner 	/*
162770447e0aSDave Chinner 	 * If the CIL is empty, make sure that any previous checkpoint that may
162870447e0aSDave Chinner 	 * still be in an active iclog is pushed to stable storage.
162970447e0aSDave Chinner 	 */
1630c0fb4765SDave Chinner 	if (test_bit(XLOG_CIL_EMPTY, &log->l_cilp->xc_flags))
163170447e0aSDave Chinner 		xfs_log_force(log->l_mp, 0);
16320020a190SDave Chinner }
16330020a190SDave Chinner 
16340020a190SDave Chinner /*
163571e330b5SDave Chinner  * Conditionally push the CIL based on the sequence passed in.
163671e330b5SDave Chinner  *
16370020a190SDave Chinner  * We only need to push if we haven't already pushed the sequence number given.
16380020a190SDave Chinner  * Hence the only time we will trigger a push here is if the push sequence is
16390020a190SDave Chinner  * the same as the current context.
164071e330b5SDave Chinner  *
164171e330b5SDave Chinner  * We return the current commit lsn to allow the callers to determine if a
164271e330b5SDave Chinner  * iclog flush is necessary following this call.
164371e330b5SDave Chinner  */
164471e330b5SDave Chinner xfs_lsn_t
xlog_cil_force_seq(struct xlog * log,xfs_csn_t sequence)16455f9b4b0dSDave Chinner xlog_cil_force_seq(
1646f7bdf03aSMark Tinguely 	struct xlog	*log,
16475f9b4b0dSDave Chinner 	xfs_csn_t	sequence)
164871e330b5SDave Chinner {
164971e330b5SDave Chinner 	struct xfs_cil		*cil = log->l_cilp;
165071e330b5SDave Chinner 	struct xfs_cil_ctx	*ctx;
165171e330b5SDave Chinner 	xfs_lsn_t		commit_lsn = NULLCOMMITLSN;
165271e330b5SDave Chinner 
1653a44f13edSDave Chinner 	ASSERT(sequence <= cil->xc_current_sequence);
165471e330b5SDave Chinner 
16550020a190SDave Chinner 	if (!sequence)
16560020a190SDave Chinner 		sequence = cil->xc_current_sequence;
16570020a190SDave Chinner 	trace_xfs_log_force(log->l_mp, sequence, _RET_IP_);
16580020a190SDave Chinner 
1659a44f13edSDave Chinner 	/*
1660a44f13edSDave Chinner 	 * check to see if we need to force out the current context.
1661a44f13edSDave Chinner 	 * xlog_cil_push() handles racing pushes for the same sequence,
1662a44f13edSDave Chinner 	 * so no need to deal with it here.
1663a44f13edSDave Chinner 	 */
1664f876e446SDave Chinner restart:
16650020a190SDave Chinner 	xlog_cil_push_now(log, sequence, false);
166671e330b5SDave Chinner 
166771e330b5SDave Chinner 	/*
166871e330b5SDave Chinner 	 * See if we can find a previous sequence still committing.
166971e330b5SDave Chinner 	 * We need to wait for all previous sequence commits to complete
167071e330b5SDave Chinner 	 * before allowing the force of push_seq to go ahead. Hence block
167171e330b5SDave Chinner 	 * on commits for those as well.
167271e330b5SDave Chinner 	 */
16734bb928cdSDave Chinner 	spin_lock(&cil->xc_push_lock);
167471e330b5SDave Chinner 	list_for_each_entry(ctx, &cil->xc_committing, committing) {
1675ac983517SDave Chinner 		/*
1676ac983517SDave Chinner 		 * Avoid getting stuck in this loop because we were woken by the
1677ac983517SDave Chinner 		 * shutdown, but then went back to sleep once already in the
1678ac983517SDave Chinner 		 * shutdown state.
1679ac983517SDave Chinner 		 */
16802039a272SDave Chinner 		if (xlog_is_shutdown(log))
1681ac983517SDave Chinner 			goto out_shutdown;
1682a44f13edSDave Chinner 		if (ctx->sequence > sequence)
168371e330b5SDave Chinner 			continue;
168471e330b5SDave Chinner 		if (!ctx->commit_lsn) {
168571e330b5SDave Chinner 			/*
168671e330b5SDave Chinner 			 * It is still being pushed! Wait for the push to
168771e330b5SDave Chinner 			 * complete, then start again from the beginning.
168871e330b5SDave Chinner 			 */
16890020a190SDave Chinner 			XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
16904bb928cdSDave Chinner 			xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
169171e330b5SDave Chinner 			goto restart;
169271e330b5SDave Chinner 		}
1693a44f13edSDave Chinner 		if (ctx->sequence != sequence)
169471e330b5SDave Chinner 			continue;
169571e330b5SDave Chinner 		/* found it! */
169671e330b5SDave Chinner 		commit_lsn = ctx->commit_lsn;
169771e330b5SDave Chinner 	}
1698f876e446SDave Chinner 
1699f876e446SDave Chinner 	/*
1700f876e446SDave Chinner 	 * The call to xlog_cil_push_now() executes the push in the background.
1701f876e446SDave Chinner 	 * Hence by the time we have got here it our sequence may not have been
1702f876e446SDave Chinner 	 * pushed yet. This is true if the current sequence still matches the
1703f876e446SDave Chinner 	 * push sequence after the above wait loop and the CIL still contains
17048af3dcd3SDave Chinner 	 * dirty objects. This is guaranteed by the push code first adding the
17058af3dcd3SDave Chinner 	 * context to the committing list before emptying the CIL.
1706f876e446SDave Chinner 	 *
17078af3dcd3SDave Chinner 	 * Hence if we don't find the context in the committing list and the
17088af3dcd3SDave Chinner 	 * current sequence number is unchanged then the CIL contents are
17098af3dcd3SDave Chinner 	 * significant.  If the CIL is empty, if means there was nothing to push
17108af3dcd3SDave Chinner 	 * and that means there is nothing to wait for. If the CIL is not empty,
17118af3dcd3SDave Chinner 	 * it means we haven't yet started the push, because if it had started
17128af3dcd3SDave Chinner 	 * we would have found the context on the committing list.
1713f876e446SDave Chinner 	 */
1714f876e446SDave Chinner 	if (sequence == cil->xc_current_sequence &&
171588591e7fSDave Chinner 	    !test_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) {
1716f876e446SDave Chinner 		spin_unlock(&cil->xc_push_lock);
1717f876e446SDave Chinner 		goto restart;
1718f876e446SDave Chinner 	}
1719f876e446SDave Chinner 
17204bb928cdSDave Chinner 	spin_unlock(&cil->xc_push_lock);
172171e330b5SDave Chinner 	return commit_lsn;
1722ac983517SDave Chinner 
1723ac983517SDave Chinner 	/*
1724ac983517SDave Chinner 	 * We detected a shutdown in progress. We need to trigger the log force
1725ac983517SDave Chinner 	 * to pass through it's iclog state machine error handling, even though
1726ac983517SDave Chinner 	 * we are already in a shutdown state. Hence we can't return
1727ac983517SDave Chinner 	 * NULLCOMMITLSN here as that has special meaning to log forces (i.e.
1728ac983517SDave Chinner 	 * LSN is already stable), so we return a zero LSN instead.
1729ac983517SDave Chinner 	 */
1730ac983517SDave Chinner out_shutdown:
1731ac983517SDave Chinner 	spin_unlock(&cil->xc_push_lock);
1732ac983517SDave Chinner 	return 0;
173371e330b5SDave Chinner }
1734ccf7c23fSDave Chinner 
1735ccf7c23fSDave Chinner /*
17364c2d542fSDave Chinner  * Perform initial CIL structure initialisation.
17374c2d542fSDave Chinner  */
17384c2d542fSDave Chinner int
xlog_cil_init(struct xlog * log)17394c2d542fSDave Chinner xlog_cil_init(
1740f7bdf03aSMark Tinguely 	struct xlog		*log)
17414c2d542fSDave Chinner {
17424c2d542fSDave Chinner 	struct xfs_cil		*cil;
17434c2d542fSDave Chinner 	struct xfs_cil_ctx	*ctx;
1744df7a4a21SDave Chinner 	struct xlog_cil_pcp	*cilpcp;
1745df7a4a21SDave Chinner 	int			cpu;
17464c2d542fSDave Chinner 
1747707e0ddaSTetsuo Handa 	cil = kmem_zalloc(sizeof(*cil), KM_MAYFAIL);
17484c2d542fSDave Chinner 	if (!cil)
17492451337dSDave Chinner 		return -ENOMEM;
175033c0dd78SDave Chinner 	/*
175133c0dd78SDave Chinner 	 * Limit the CIL pipeline depth to 4 concurrent works to bound the
175233c0dd78SDave Chinner 	 * concurrency the log spinlocks will be exposed to.
175333c0dd78SDave Chinner 	 */
175433c0dd78SDave Chinner 	cil->xc_push_wq = alloc_workqueue("xfs-cil/%s",
175533c0dd78SDave Chinner 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_UNBOUND),
175633c0dd78SDave Chinner 			4, log->l_mp->m_super->s_id);
175733c0dd78SDave Chinner 	if (!cil->xc_push_wq)
175833c0dd78SDave Chinner 		goto out_destroy_cil;
17594c2d542fSDave Chinner 
1760af1c2146SDave Chinner 	cil->xc_log = log;
1761af1c2146SDave Chinner 	cil->xc_pcp = alloc_percpu(struct xlog_cil_pcp);
1762af1c2146SDave Chinner 	if (!cil->xc_pcp)
1763af1c2146SDave Chinner 		goto out_destroy_wq;
1764af1c2146SDave Chinner 
1765df7a4a21SDave Chinner 	for_each_possible_cpu(cpu) {
1766df7a4a21SDave Chinner 		cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
1767df7a4a21SDave Chinner 		INIT_LIST_HEAD(&cilpcp->busy_extents);
1768c0fb4765SDave Chinner 		INIT_LIST_HEAD(&cilpcp->log_items);
1769df7a4a21SDave Chinner 	}
1770df7a4a21SDave Chinner 
17714c2d542fSDave Chinner 	INIT_LIST_HEAD(&cil->xc_committing);
17724bb928cdSDave Chinner 	spin_lock_init(&cil->xc_push_lock);
1773c7f87f39SDave Chinner 	init_waitqueue_head(&cil->xc_push_wait);
17744c2d542fSDave Chinner 	init_rwsem(&cil->xc_ctx_lock);
177568a74dcaSDave Chinner 	init_waitqueue_head(&cil->xc_start_wait);
17764c2d542fSDave Chinner 	init_waitqueue_head(&cil->xc_commit_wait);
17774c2d542fSDave Chinner 	log->l_cilp = cil;
177839823d0fSDave Chinner 
177939823d0fSDave Chinner 	ctx = xlog_cil_ctx_alloc();
178039823d0fSDave Chinner 	xlog_cil_ctx_switch(cil, ctx);
17814c2d542fSDave Chinner 	return 0;
178233c0dd78SDave Chinner 
1783af1c2146SDave Chinner out_destroy_wq:
1784af1c2146SDave Chinner 	destroy_workqueue(cil->xc_push_wq);
178533c0dd78SDave Chinner out_destroy_cil:
178633c0dd78SDave Chinner 	kmem_free(cil);
178733c0dd78SDave Chinner 	return -ENOMEM;
17884c2d542fSDave Chinner }
17894c2d542fSDave Chinner 
17904c2d542fSDave Chinner void
xlog_cil_destroy(struct xlog * log)17914c2d542fSDave Chinner xlog_cil_destroy(
1792f7bdf03aSMark Tinguely 	struct xlog	*log)
17934c2d542fSDave Chinner {
179488591e7fSDave Chinner 	struct xfs_cil	*cil = log->l_cilp;
179588591e7fSDave Chinner 
179688591e7fSDave Chinner 	if (cil->xc_ctx) {
179788591e7fSDave Chinner 		if (cil->xc_ctx->ticket)
179888591e7fSDave Chinner 			xfs_log_ticket_put(cil->xc_ctx->ticket);
179988591e7fSDave Chinner 		kmem_free(cil->xc_ctx);
18004c2d542fSDave Chinner 	}
18014c2d542fSDave Chinner 
180288591e7fSDave Chinner 	ASSERT(test_bit(XLOG_CIL_EMPTY, &cil->xc_flags));
1803af1c2146SDave Chinner 	free_percpu(cil->xc_pcp);
180488591e7fSDave Chinner 	destroy_workqueue(cil->xc_push_wq);
180588591e7fSDave Chinner 	kmem_free(cil);
18064c2d542fSDave Chinner }
18074c2d542fSDave Chinner 
1808