xref: /openbmc/linux/fs/xfs/xfs_log_cil.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved.
4  */
5 
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_shared.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_extent_busy.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_log.h"
17 #include "xfs_log_priv.h"
18 #include "xfs_trace.h"
19 #include "xfs_discard.h"
20 
21 /*
22  * Allocate a new ticket. Failing to get a new ticket makes it really hard to
23  * recover, so we don't allow failure here. Also, we allocate in a context that
24  * we don't want to be issuing transactions from, so we need to tell the
25  * allocation code this as well.
26  *
27  * We don't reserve any space for the ticket - we are going to steal whatever
28  * space we require from transactions as they commit. To ensure we reserve all
29  * the space required, we need to set the current reservation of the ticket to
30  * zero so that we know to steal the initial transaction overhead from the
31  * first transaction commit.
32  */
33 static struct xlog_ticket *
xlog_cil_ticket_alloc(struct xlog * log)34 xlog_cil_ticket_alloc(
35 	struct xlog	*log)
36 {
37 	struct xlog_ticket *tic;
38 
39 	tic = xlog_ticket_alloc(log, 0, 1, 0);
40 
41 	/*
42 	 * set the current reservation to zero so we know to steal the basic
43 	 * transaction overhead reservation from the first transaction commit.
44 	 */
45 	tic->t_curr_res = 0;
46 	tic->t_iclog_hdrs = 0;
47 	return tic;
48 }
49 
50 static inline void
xlog_cil_set_iclog_hdr_count(struct xfs_cil * cil)51 xlog_cil_set_iclog_hdr_count(struct xfs_cil *cil)
52 {
53 	struct xlog	*log = cil->xc_log;
54 
55 	atomic_set(&cil->xc_iclog_hdrs,
56 		   (XLOG_CIL_BLOCKING_SPACE_LIMIT(log) /
57 			(log->l_iclog_size - log->l_iclog_hsize)));
58 }
59 
60 /*
61  * Check if the current log item was first committed in this sequence.
62  * We can't rely on just the log item being in the CIL, we have to check
63  * the recorded commit sequence number.
64  *
65  * Note: for this to be used in a non-racy manner, it has to be called with
66  * CIL flushing locked out. As a result, it should only be used during the
67  * transaction commit process when deciding what to format into the item.
68  */
69 static bool
xlog_item_in_current_chkpt(struct xfs_cil * cil,struct xfs_log_item * lip)70 xlog_item_in_current_chkpt(
71 	struct xfs_cil		*cil,
72 	struct xfs_log_item	*lip)
73 {
74 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
75 		return false;
76 
77 	/*
78 	 * li_seq is written on the first commit of a log item to record the
79 	 * first checkpoint it is written to. Hence if it is different to the
80 	 * current sequence, we're in a new checkpoint.
81 	 */
82 	return lip->li_seq == READ_ONCE(cil->xc_current_sequence);
83 }
84 
85 bool
xfs_log_item_in_current_chkpt(struct xfs_log_item * lip)86 xfs_log_item_in_current_chkpt(
87 	struct xfs_log_item *lip)
88 {
89 	return xlog_item_in_current_chkpt(lip->li_log->l_cilp, lip);
90 }
91 
92 /*
93  * Unavoidable forward declaration - xlog_cil_push_work() calls
94  * xlog_cil_ctx_alloc() itself.
95  */
96 static void xlog_cil_push_work(struct work_struct *work);
97 
98 static struct xfs_cil_ctx *
xlog_cil_ctx_alloc(void)99 xlog_cil_ctx_alloc(void)
100 {
101 	struct xfs_cil_ctx	*ctx;
102 
103 	ctx = kmem_zalloc(sizeof(*ctx), KM_NOFS);
104 	INIT_LIST_HEAD(&ctx->committing);
105 	INIT_LIST_HEAD(&ctx->busy_extents.extent_list);
106 	INIT_LIST_HEAD(&ctx->log_items);
107 	INIT_LIST_HEAD(&ctx->lv_chain);
108 	INIT_WORK(&ctx->push_work, xlog_cil_push_work);
109 	return ctx;
110 }
111 
112 /*
113  * Aggregate the CIL per cpu structures into global counts, lists, etc and
114  * clear the percpu state ready for the next context to use. This is called
115  * from the push code with the context lock held exclusively, hence nothing else
116  * will be accessing or modifying the per-cpu counters.
117  */
118 static void
xlog_cil_push_pcp_aggregate(struct xfs_cil * cil,struct xfs_cil_ctx * ctx)119 xlog_cil_push_pcp_aggregate(
120 	struct xfs_cil		*cil,
121 	struct xfs_cil_ctx	*ctx)
122 {
123 	struct xlog_cil_pcp	*cilpcp;
124 	int			cpu;
125 
126 	for_each_cpu(cpu, &ctx->cil_pcpmask) {
127 		cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
128 
129 		ctx->ticket->t_curr_res += cilpcp->space_reserved;
130 		cilpcp->space_reserved = 0;
131 
132 		if (!list_empty(&cilpcp->busy_extents)) {
133 			list_splice_init(&cilpcp->busy_extents,
134 					&ctx->busy_extents.extent_list);
135 		}
136 		if (!list_empty(&cilpcp->log_items))
137 			list_splice_init(&cilpcp->log_items, &ctx->log_items);
138 
139 		/*
140 		 * We're in the middle of switching cil contexts.  Reset the
141 		 * counter we use to detect when the current context is nearing
142 		 * full.
143 		 */
144 		cilpcp->space_used = 0;
145 	}
146 }
147 
148 /*
149  * Aggregate the CIL per-cpu space used counters into the global atomic value.
150  * This is called when the per-cpu counter aggregation will first pass the soft
151  * limit threshold so we can switch to atomic counter aggregation for accurate
152  * detection of hard limit traversal.
153  */
154 static void
xlog_cil_insert_pcp_aggregate(struct xfs_cil * cil,struct xfs_cil_ctx * ctx)155 xlog_cil_insert_pcp_aggregate(
156 	struct xfs_cil		*cil,
157 	struct xfs_cil_ctx	*ctx)
158 {
159 	int			cpu;
160 	int			count = 0;
161 
162 	/* Trigger atomic updates then aggregate only for the first caller */
163 	if (!test_and_clear_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags))
164 		return;
165 
166 	/*
167 	 * We can race with other cpus setting cil_pcpmask.  However, we've
168 	 * atomically cleared PCP_SPACE which forces other threads to add to
169 	 * the global space used count.  cil_pcpmask is a superset of cilpcp
170 	 * structures that could have a nonzero space_used.
171 	 */
172 	for_each_cpu(cpu, &ctx->cil_pcpmask) {
173 		struct xlog_cil_pcp	*cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
174 		int			old = READ_ONCE(cilpcp->space_used);
175 
176 		while (!try_cmpxchg(&cilpcp->space_used, &old, 0))
177 			;
178 		count += old;
179 	}
180 	atomic_add(count, &ctx->space_used);
181 }
182 
183 static void
xlog_cil_ctx_switch(struct xfs_cil * cil,struct xfs_cil_ctx * ctx)184 xlog_cil_ctx_switch(
185 	struct xfs_cil		*cil,
186 	struct xfs_cil_ctx	*ctx)
187 {
188 	xlog_cil_set_iclog_hdr_count(cil);
189 	set_bit(XLOG_CIL_EMPTY, &cil->xc_flags);
190 	set_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags);
191 	ctx->sequence = ++cil->xc_current_sequence;
192 	ctx->cil = cil;
193 	cil->xc_ctx = ctx;
194 }
195 
196 /*
197  * After the first stage of log recovery is done, we know where the head and
198  * tail of the log are. We need this log initialisation done before we can
199  * initialise the first CIL checkpoint context.
200  *
201  * Here we allocate a log ticket to track space usage during a CIL push.  This
202  * ticket is passed to xlog_write() directly so that we don't slowly leak log
203  * space by failing to account for space used by log headers and additional
204  * region headers for split regions.
205  */
206 void
xlog_cil_init_post_recovery(struct xlog * log)207 xlog_cil_init_post_recovery(
208 	struct xlog	*log)
209 {
210 	log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
211 	log->l_cilp->xc_ctx->sequence = 1;
212 	xlog_cil_set_iclog_hdr_count(log->l_cilp);
213 }
214 
215 static inline int
xlog_cil_iovec_space(uint niovecs)216 xlog_cil_iovec_space(
217 	uint	niovecs)
218 {
219 	return round_up((sizeof(struct xfs_log_vec) +
220 					niovecs * sizeof(struct xfs_log_iovec)),
221 			sizeof(uint64_t));
222 }
223 
224 /*
225  * Allocate or pin log vector buffers for CIL insertion.
226  *
227  * The CIL currently uses disposable buffers for copying a snapshot of the
228  * modified items into the log during a push. The biggest problem with this is
229  * the requirement to allocate the disposable buffer during the commit if:
230  *	a) does not exist; or
231  *	b) it is too small
232  *
233  * If we do this allocation within xlog_cil_insert_format_items(), it is done
234  * under the xc_ctx_lock, which means that a CIL push cannot occur during
235  * the memory allocation. This means that we have a potential deadlock situation
236  * under low memory conditions when we have lots of dirty metadata pinned in
237  * the CIL and we need a CIL commit to occur to free memory.
238  *
239  * To avoid this, we need to move the memory allocation outside the
240  * xc_ctx_lock, but because the log vector buffers are disposable, that opens
241  * up a TOCTOU race condition w.r.t. the CIL committing and removing the log
242  * vector buffers between the check and the formatting of the item into the
243  * log vector buffer within the xc_ctx_lock.
244  *
245  * Because the log vector buffer needs to be unchanged during the CIL push
246  * process, we cannot share the buffer between the transaction commit (which
247  * modifies the buffer) and the CIL push context that is writing the changes
248  * into the log. This means skipping preallocation of buffer space is
249  * unreliable, but we most definitely do not want to be allocating and freeing
250  * buffers unnecessarily during commits when overwrites can be done safely.
251  *
252  * The simplest solution to this problem is to allocate a shadow buffer when a
253  * log item is committed for the second time, and then to only use this buffer
254  * if necessary. The buffer can remain attached to the log item until such time
255  * it is needed, and this is the buffer that is reallocated to match the size of
256  * the incoming modification. Then during the formatting of the item we can swap
257  * the active buffer with the new one if we can't reuse the existing buffer. We
258  * don't free the old buffer as it may be reused on the next modification if
259  * it's size is right, otherwise we'll free and reallocate it at that point.
260  *
261  * This function builds a vector for the changes in each log item in the
262  * transaction. It then works out the length of the buffer needed for each log
263  * item, allocates them and attaches the vector to the log item in preparation
264  * for the formatting step which occurs under the xc_ctx_lock.
265  *
266  * While this means the memory footprint goes up, it avoids the repeated
267  * alloc/free pattern that repeated modifications of an item would otherwise
268  * cause, and hence minimises the CPU overhead of such behaviour.
269  */
270 static void
xlog_cil_alloc_shadow_bufs(struct xlog * log,struct xfs_trans * tp)271 xlog_cil_alloc_shadow_bufs(
272 	struct xlog		*log,
273 	struct xfs_trans	*tp)
274 {
275 	struct xfs_log_item	*lip;
276 
277 	list_for_each_entry(lip, &tp->t_items, li_trans) {
278 		struct xfs_log_vec *lv;
279 		int	niovecs = 0;
280 		int	nbytes = 0;
281 		int	buf_size;
282 		bool	ordered = false;
283 
284 		/* Skip items which aren't dirty in this transaction. */
285 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
286 			continue;
287 
288 		/* get number of vecs and size of data to be stored */
289 		lip->li_ops->iop_size(lip, &niovecs, &nbytes);
290 
291 		/*
292 		 * Ordered items need to be tracked but we do not wish to write
293 		 * them. We need a logvec to track the object, but we do not
294 		 * need an iovec or buffer to be allocated for copying data.
295 		 */
296 		if (niovecs == XFS_LOG_VEC_ORDERED) {
297 			ordered = true;
298 			niovecs = 0;
299 			nbytes = 0;
300 		}
301 
302 		/*
303 		 * We 64-bit align the length of each iovec so that the start of
304 		 * the next one is naturally aligned.  We'll need to account for
305 		 * that slack space here.
306 		 *
307 		 * We also add the xlog_op_header to each region when
308 		 * formatting, but that's not accounted to the size of the item
309 		 * at this point. Hence we'll need an addition number of bytes
310 		 * for each vector to hold an opheader.
311 		 *
312 		 * Then round nbytes up to 64-bit alignment so that the initial
313 		 * buffer alignment is easy to calculate and verify.
314 		 */
315 		nbytes += niovecs *
316 			(sizeof(uint64_t) + sizeof(struct xlog_op_header));
317 		nbytes = round_up(nbytes, sizeof(uint64_t));
318 
319 		/*
320 		 * The data buffer needs to start 64-bit aligned, so round up
321 		 * that space to ensure we can align it appropriately and not
322 		 * overrun the buffer.
323 		 */
324 		buf_size = nbytes + xlog_cil_iovec_space(niovecs);
325 
326 		/*
327 		 * if we have no shadow buffer, or it is too small, we need to
328 		 * reallocate it.
329 		 */
330 		if (!lip->li_lv_shadow ||
331 		    buf_size > lip->li_lv_shadow->lv_size) {
332 			/*
333 			 * We free and allocate here as a realloc would copy
334 			 * unnecessary data. We don't use kvzalloc() for the
335 			 * same reason - we don't need to zero the data area in
336 			 * the buffer, only the log vector header and the iovec
337 			 * storage.
338 			 */
339 			kmem_free(lip->li_lv_shadow);
340 			lv = xlog_kvmalloc(buf_size);
341 
342 			memset(lv, 0, xlog_cil_iovec_space(niovecs));
343 
344 			INIT_LIST_HEAD(&lv->lv_list);
345 			lv->lv_item = lip;
346 			lv->lv_size = buf_size;
347 			if (ordered)
348 				lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
349 			else
350 				lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
351 			lip->li_lv_shadow = lv;
352 		} else {
353 			/* same or smaller, optimise common overwrite case */
354 			lv = lip->li_lv_shadow;
355 			if (ordered)
356 				lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
357 			else
358 				lv->lv_buf_len = 0;
359 			lv->lv_bytes = 0;
360 		}
361 
362 		/* Ensure the lv is set up according to ->iop_size */
363 		lv->lv_niovecs = niovecs;
364 
365 		/* The allocated data region lies beyond the iovec region */
366 		lv->lv_buf = (char *)lv + xlog_cil_iovec_space(niovecs);
367 	}
368 
369 }
370 
371 /*
372  * Prepare the log item for insertion into the CIL. Calculate the difference in
373  * log space it will consume, and if it is a new item pin it as well.
374  */
375 STATIC void
xfs_cil_prepare_item(struct xlog * log,struct xfs_log_vec * lv,struct xfs_log_vec * old_lv,int * diff_len)376 xfs_cil_prepare_item(
377 	struct xlog		*log,
378 	struct xfs_log_vec	*lv,
379 	struct xfs_log_vec	*old_lv,
380 	int			*diff_len)
381 {
382 	/* Account for the new LV being passed in */
383 	if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
384 		*diff_len += lv->lv_bytes;
385 
386 	/*
387 	 * If there is no old LV, this is the first time we've seen the item in
388 	 * this CIL context and so we need to pin it. If we are replacing the
389 	 * old_lv, then remove the space it accounts for and make it the shadow
390 	 * buffer for later freeing. In both cases we are now switching to the
391 	 * shadow buffer, so update the pointer to it appropriately.
392 	 */
393 	if (!old_lv) {
394 		if (lv->lv_item->li_ops->iop_pin)
395 			lv->lv_item->li_ops->iop_pin(lv->lv_item);
396 		lv->lv_item->li_lv_shadow = NULL;
397 	} else if (old_lv != lv) {
398 		ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
399 
400 		*diff_len -= old_lv->lv_bytes;
401 		lv->lv_item->li_lv_shadow = old_lv;
402 	}
403 
404 	/* attach new log vector to log item */
405 	lv->lv_item->li_lv = lv;
406 
407 	/*
408 	 * If this is the first time the item is being committed to the
409 	 * CIL, store the sequence number on the log item so we can
410 	 * tell in future commits whether this is the first checkpoint
411 	 * the item is being committed into.
412 	 */
413 	if (!lv->lv_item->li_seq)
414 		lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
415 }
416 
417 /*
418  * Format log item into a flat buffers
419  *
420  * For delayed logging, we need to hold a formatted buffer containing all the
421  * changes on the log item. This enables us to relog the item in memory and
422  * write it out asynchronously without needing to relock the object that was
423  * modified at the time it gets written into the iclog.
424  *
425  * This function takes the prepared log vectors attached to each log item, and
426  * formats the changes into the log vector buffer. The buffer it uses is
427  * dependent on the current state of the vector in the CIL - the shadow lv is
428  * guaranteed to be large enough for the current modification, but we will only
429  * use that if we can't reuse the existing lv. If we can't reuse the existing
430  * lv, then simple swap it out for the shadow lv. We don't free it - that is
431  * done lazily either by th enext modification or the freeing of the log item.
432  *
433  * We don't set up region headers during this process; we simply copy the
434  * regions into the flat buffer. We can do this because we still have to do a
435  * formatting step to write the regions into the iclog buffer.  Writing the
436  * ophdrs during the iclog write means that we can support splitting large
437  * regions across iclog boundares without needing a change in the format of the
438  * item/region encapsulation.
439  *
440  * Hence what we need to do now is change the rewrite the vector array to point
441  * to the copied region inside the buffer we just allocated. This allows us to
442  * format the regions into the iclog as though they are being formatted
443  * directly out of the objects themselves.
444  */
445 static void
xlog_cil_insert_format_items(struct xlog * log,struct xfs_trans * tp,int * diff_len)446 xlog_cil_insert_format_items(
447 	struct xlog		*log,
448 	struct xfs_trans	*tp,
449 	int			*diff_len)
450 {
451 	struct xfs_log_item	*lip;
452 
453 	/* Bail out if we didn't find a log item.  */
454 	if (list_empty(&tp->t_items)) {
455 		ASSERT(0);
456 		return;
457 	}
458 
459 	list_for_each_entry(lip, &tp->t_items, li_trans) {
460 		struct xfs_log_vec *lv;
461 		struct xfs_log_vec *old_lv = NULL;
462 		struct xfs_log_vec *shadow;
463 		bool	ordered = false;
464 
465 		/* Skip items which aren't dirty in this transaction. */
466 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
467 			continue;
468 
469 		/*
470 		 * The formatting size information is already attached to
471 		 * the shadow lv on the log item.
472 		 */
473 		shadow = lip->li_lv_shadow;
474 		if (shadow->lv_buf_len == XFS_LOG_VEC_ORDERED)
475 			ordered = true;
476 
477 		/* Skip items that do not have any vectors for writing */
478 		if (!shadow->lv_niovecs && !ordered)
479 			continue;
480 
481 		/* compare to existing item size */
482 		old_lv = lip->li_lv;
483 		if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {
484 			/* same or smaller, optimise common overwrite case */
485 			lv = lip->li_lv;
486 
487 			if (ordered)
488 				goto insert;
489 
490 			/*
491 			 * set the item up as though it is a new insertion so
492 			 * that the space reservation accounting is correct.
493 			 */
494 			*diff_len -= lv->lv_bytes;
495 
496 			/* Ensure the lv is set up according to ->iop_size */
497 			lv->lv_niovecs = shadow->lv_niovecs;
498 
499 			/* reset the lv buffer information for new formatting */
500 			lv->lv_buf_len = 0;
501 			lv->lv_bytes = 0;
502 			lv->lv_buf = (char *)lv +
503 					xlog_cil_iovec_space(lv->lv_niovecs);
504 		} else {
505 			/* switch to shadow buffer! */
506 			lv = shadow;
507 			lv->lv_item = lip;
508 			if (ordered) {
509 				/* track as an ordered logvec */
510 				ASSERT(lip->li_lv == NULL);
511 				goto insert;
512 			}
513 		}
514 
515 		ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
516 		lip->li_ops->iop_format(lip, lv);
517 insert:
518 		xfs_cil_prepare_item(log, lv, old_lv, diff_len);
519 	}
520 }
521 
522 /*
523  * The use of lockless waitqueue_active() requires that the caller has
524  * serialised itself against the wakeup call in xlog_cil_push_work(). That
525  * can be done by either holding the push lock or the context lock.
526  */
527 static inline bool
xlog_cil_over_hard_limit(struct xlog * log,int32_t space_used)528 xlog_cil_over_hard_limit(
529 	struct xlog	*log,
530 	int32_t		space_used)
531 {
532 	if (waitqueue_active(&log->l_cilp->xc_push_wait))
533 		return true;
534 	if (space_used >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log))
535 		return true;
536 	return false;
537 }
538 
539 /*
540  * Insert the log items into the CIL and calculate the difference in space
541  * consumed by the item. Add the space to the checkpoint ticket and calculate
542  * if the change requires additional log metadata. If it does, take that space
543  * as well. Remove the amount of space we added to the checkpoint ticket from
544  * the current transaction ticket so that the accounting works out correctly.
545  */
546 static void
xlog_cil_insert_items(struct xlog * log,struct xfs_trans * tp,uint32_t released_space)547 xlog_cil_insert_items(
548 	struct xlog		*log,
549 	struct xfs_trans	*tp,
550 	uint32_t		released_space)
551 {
552 	struct xfs_cil		*cil = log->l_cilp;
553 	struct xfs_cil_ctx	*ctx = cil->xc_ctx;
554 	struct xfs_log_item	*lip;
555 	int			len = 0;
556 	int			iovhdr_res = 0, split_res = 0, ctx_res = 0;
557 	int			space_used;
558 	int			order;
559 	unsigned int		cpu_nr;
560 	struct xlog_cil_pcp	*cilpcp;
561 
562 	ASSERT(tp);
563 
564 	/*
565 	 * We can do this safely because the context can't checkpoint until we
566 	 * are done so it doesn't matter exactly how we update the CIL.
567 	 */
568 	xlog_cil_insert_format_items(log, tp, &len);
569 
570 	/*
571 	 * Subtract the space released by intent cancelation from the space we
572 	 * consumed so that we remove it from the CIL space and add it back to
573 	 * the current transaction reservation context.
574 	 */
575 	len -= released_space;
576 
577 	/*
578 	 * Grab the per-cpu pointer for the CIL before we start any accounting.
579 	 * That ensures that we are running with pre-emption disabled and so we
580 	 * can't be scheduled away between split sample/update operations that
581 	 * are done without outside locking to serialise them.
582 	 */
583 	cpu_nr = get_cpu();
584 	cilpcp = this_cpu_ptr(cil->xc_pcp);
585 
586 	/* Tell the future push that there was work added by this CPU. */
587 	if (!cpumask_test_cpu(cpu_nr, &ctx->cil_pcpmask))
588 		cpumask_test_and_set_cpu(cpu_nr, &ctx->cil_pcpmask);
589 
590 	/*
591 	 * We need to take the CIL checkpoint unit reservation on the first
592 	 * commit into the CIL. Test the XLOG_CIL_EMPTY bit first so we don't
593 	 * unnecessarily do an atomic op in the fast path here. We can clear the
594 	 * XLOG_CIL_EMPTY bit as we are under the xc_ctx_lock here and that
595 	 * needs to be held exclusively to reset the XLOG_CIL_EMPTY bit.
596 	 */
597 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags) &&
598 	    test_and_clear_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
599 		ctx_res = ctx->ticket->t_unit_res;
600 
601 	/*
602 	 * Check if we need to steal iclog headers. atomic_read() is not a
603 	 * locked atomic operation, so we can check the value before we do any
604 	 * real atomic ops in the fast path. If we've already taken the CIL unit
605 	 * reservation from this commit, we've already got one iclog header
606 	 * space reserved so we have to account for that otherwise we risk
607 	 * overrunning the reservation on this ticket.
608 	 *
609 	 * If the CIL is already at the hard limit, we might need more header
610 	 * space that originally reserved. So steal more header space from every
611 	 * commit that occurs once we are over the hard limit to ensure the CIL
612 	 * push won't run out of reservation space.
613 	 *
614 	 * This can steal more than we need, but that's OK.
615 	 *
616 	 * The cil->xc_ctx_lock provides the serialisation necessary for safely
617 	 * calling xlog_cil_over_hard_limit() in this context.
618 	 */
619 	space_used = atomic_read(&ctx->space_used) + cilpcp->space_used + len;
620 	if (atomic_read(&cil->xc_iclog_hdrs) > 0 ||
621 	    xlog_cil_over_hard_limit(log, space_used)) {
622 		split_res = log->l_iclog_hsize +
623 					sizeof(struct xlog_op_header);
624 		if (ctx_res)
625 			ctx_res += split_res * (tp->t_ticket->t_iclog_hdrs - 1);
626 		else
627 			ctx_res = split_res * tp->t_ticket->t_iclog_hdrs;
628 		atomic_sub(tp->t_ticket->t_iclog_hdrs, &cil->xc_iclog_hdrs);
629 	}
630 	cilpcp->space_reserved += ctx_res;
631 
632 	/*
633 	 * Accurately account when over the soft limit, otherwise fold the
634 	 * percpu count into the global count if over the per-cpu threshold.
635 	 */
636 	if (!test_bit(XLOG_CIL_PCP_SPACE, &cil->xc_flags)) {
637 		atomic_add(len, &ctx->space_used);
638 	} else if (cilpcp->space_used + len >
639 			(XLOG_CIL_SPACE_LIMIT(log) / num_online_cpus())) {
640 		space_used = atomic_add_return(cilpcp->space_used + len,
641 						&ctx->space_used);
642 		cilpcp->space_used = 0;
643 
644 		/*
645 		 * If we just transitioned over the soft limit, we need to
646 		 * transition to the global atomic counter.
647 		 */
648 		if (space_used >= XLOG_CIL_SPACE_LIMIT(log))
649 			xlog_cil_insert_pcp_aggregate(cil, ctx);
650 	} else {
651 		cilpcp->space_used += len;
652 	}
653 	/* attach the transaction to the CIL if it has any busy extents */
654 	if (!list_empty(&tp->t_busy))
655 		list_splice_init(&tp->t_busy, &cilpcp->busy_extents);
656 
657 	/*
658 	 * Now update the order of everything modified in the transaction
659 	 * and insert items into the CIL if they aren't already there.
660 	 * We do this here so we only need to take the CIL lock once during
661 	 * the transaction commit.
662 	 */
663 	order = atomic_inc_return(&ctx->order_id);
664 	list_for_each_entry(lip, &tp->t_items, li_trans) {
665 		/* Skip items which aren't dirty in this transaction. */
666 		if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
667 			continue;
668 
669 		lip->li_order_id = order;
670 		if (!list_empty(&lip->li_cil))
671 			continue;
672 		list_add_tail(&lip->li_cil, &cilpcp->log_items);
673 	}
674 	put_cpu();
675 
676 	/*
677 	 * If we've overrun the reservation, dump the tx details before we move
678 	 * the log items. Shutdown is imminent...
679 	 */
680 	tp->t_ticket->t_curr_res -= ctx_res + len;
681 	if (WARN_ON(tp->t_ticket->t_curr_res < 0)) {
682 		xfs_warn(log->l_mp, "Transaction log reservation overrun:");
683 		xfs_warn(log->l_mp,
684 			 "  log items: %d bytes (iov hdrs: %d bytes)",
685 			 len, iovhdr_res);
686 		xfs_warn(log->l_mp, "  split region headers: %d bytes",
687 			 split_res);
688 		xfs_warn(log->l_mp, "  ctx ticket: %d bytes", ctx_res);
689 		xlog_print_trans(tp);
690 		xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
691 	}
692 }
693 
694 static void
xlog_cil_free_logvec(struct list_head * lv_chain)695 xlog_cil_free_logvec(
696 	struct list_head	*lv_chain)
697 {
698 	struct xfs_log_vec	*lv;
699 
700 	while (!list_empty(lv_chain)) {
701 		lv = list_first_entry(lv_chain, struct xfs_log_vec, lv_list);
702 		list_del_init(&lv->lv_list);
703 		kmem_free(lv);
704 	}
705 }
706 
707 /*
708  * Mark all items committed and clear busy extents. We free the log vector
709  * chains in a separate pass so that we unpin the log items as quickly as
710  * possible.
711  */
712 static void
xlog_cil_committed(struct xfs_cil_ctx * ctx)713 xlog_cil_committed(
714 	struct xfs_cil_ctx	*ctx)
715 {
716 	struct xfs_mount	*mp = ctx->cil->xc_log->l_mp;
717 	bool			abort = xlog_is_shutdown(ctx->cil->xc_log);
718 
719 	/*
720 	 * If the I/O failed, we're aborting the commit and already shutdown.
721 	 * Wake any commit waiters before aborting the log items so we don't
722 	 * block async log pushers on callbacks. Async log pushers explicitly do
723 	 * not wait on log force completion because they may be holding locks
724 	 * required to unpin items.
725 	 */
726 	if (abort) {
727 		spin_lock(&ctx->cil->xc_push_lock);
728 		wake_up_all(&ctx->cil->xc_start_wait);
729 		wake_up_all(&ctx->cil->xc_commit_wait);
730 		spin_unlock(&ctx->cil->xc_push_lock);
731 	}
732 
733 	xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, &ctx->lv_chain,
734 					ctx->start_lsn, abort);
735 
736 	xfs_extent_busy_sort(&ctx->busy_extents.extent_list);
737 	xfs_extent_busy_clear(mp, &ctx->busy_extents.extent_list,
738 			      xfs_has_discard(mp) && !abort);
739 
740 	spin_lock(&ctx->cil->xc_push_lock);
741 	list_del(&ctx->committing);
742 	spin_unlock(&ctx->cil->xc_push_lock);
743 
744 	xlog_cil_free_logvec(&ctx->lv_chain);
745 
746 	if (!list_empty(&ctx->busy_extents.extent_list)) {
747 		ctx->busy_extents.mount = mp;
748 		ctx->busy_extents.owner = ctx;
749 		xfs_discard_extents(mp, &ctx->busy_extents);
750 		return;
751 	}
752 
753 	kmem_free(ctx);
754 }
755 
756 void
xlog_cil_process_committed(struct list_head * list)757 xlog_cil_process_committed(
758 	struct list_head	*list)
759 {
760 	struct xfs_cil_ctx	*ctx;
761 
762 	while ((ctx = list_first_entry_or_null(list,
763 			struct xfs_cil_ctx, iclog_entry))) {
764 		list_del(&ctx->iclog_entry);
765 		xlog_cil_committed(ctx);
766 	}
767 }
768 
769 /*
770 * Record the LSN of the iclog we were just granted space to start writing into.
771 * If the context doesn't have a start_lsn recorded, then this iclog will
772 * contain the start record for the checkpoint. Otherwise this write contains
773 * the commit record for the checkpoint.
774 */
775 void
xlog_cil_set_ctx_write_state(struct xfs_cil_ctx * ctx,struct xlog_in_core * iclog)776 xlog_cil_set_ctx_write_state(
777 	struct xfs_cil_ctx	*ctx,
778 	struct xlog_in_core	*iclog)
779 {
780 	struct xfs_cil		*cil = ctx->cil;
781 	xfs_lsn_t		lsn = be64_to_cpu(iclog->ic_header.h_lsn);
782 
783 	ASSERT(!ctx->commit_lsn);
784 	if (!ctx->start_lsn) {
785 		spin_lock(&cil->xc_push_lock);
786 		/*
787 		 * The LSN we need to pass to the log items on transaction
788 		 * commit is the LSN reported by the first log vector write, not
789 		 * the commit lsn. If we use the commit record lsn then we can
790 		 * move the grant write head beyond the tail LSN and overwrite
791 		 * it.
792 		 */
793 		ctx->start_lsn = lsn;
794 		wake_up_all(&cil->xc_start_wait);
795 		spin_unlock(&cil->xc_push_lock);
796 
797 		/*
798 		 * Make sure the metadata we are about to overwrite in the log
799 		 * has been flushed to stable storage before this iclog is
800 		 * issued.
801 		 */
802 		spin_lock(&cil->xc_log->l_icloglock);
803 		iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
804 		spin_unlock(&cil->xc_log->l_icloglock);
805 		return;
806 	}
807 
808 	/*
809 	 * Take a reference to the iclog for the context so that we still hold
810 	 * it when xlog_write is done and has released it. This means the
811 	 * context controls when the iclog is released for IO.
812 	 */
813 	atomic_inc(&iclog->ic_refcnt);
814 
815 	/*
816 	 * xlog_state_get_iclog_space() guarantees there is enough space in the
817 	 * iclog for an entire commit record, so we can attach the context
818 	 * callbacks now.  This needs to be done before we make the commit_lsn
819 	 * visible to waiters so that checkpoints with commit records in the
820 	 * same iclog order their IO completion callbacks in the same order that
821 	 * the commit records appear in the iclog.
822 	 */
823 	spin_lock(&cil->xc_log->l_icloglock);
824 	list_add_tail(&ctx->iclog_entry, &iclog->ic_callbacks);
825 	spin_unlock(&cil->xc_log->l_icloglock);
826 
827 	/*
828 	 * Now we can record the commit LSN and wake anyone waiting for this
829 	 * sequence to have the ordered commit record assigned to a physical
830 	 * location in the log.
831 	 */
832 	spin_lock(&cil->xc_push_lock);
833 	ctx->commit_iclog = iclog;
834 	ctx->commit_lsn = lsn;
835 	wake_up_all(&cil->xc_commit_wait);
836 	spin_unlock(&cil->xc_push_lock);
837 }
838 
839 
840 /*
841  * Ensure that the order of log writes follows checkpoint sequence order. This
842  * relies on the context LSN being zero until the log write has guaranteed the
843  * LSN that the log write will start at via xlog_state_get_iclog_space().
844  */
845 enum _record_type {
846 	_START_RECORD,
847 	_COMMIT_RECORD,
848 };
849 
850 static int
xlog_cil_order_write(struct xfs_cil * cil,xfs_csn_t sequence,enum _record_type record)851 xlog_cil_order_write(
852 	struct xfs_cil		*cil,
853 	xfs_csn_t		sequence,
854 	enum _record_type	record)
855 {
856 	struct xfs_cil_ctx	*ctx;
857 
858 restart:
859 	spin_lock(&cil->xc_push_lock);
860 	list_for_each_entry(ctx, &cil->xc_committing, committing) {
861 		/*
862 		 * Avoid getting stuck in this loop because we were woken by the
863 		 * shutdown, but then went back to sleep once already in the
864 		 * shutdown state.
865 		 */
866 		if (xlog_is_shutdown(cil->xc_log)) {
867 			spin_unlock(&cil->xc_push_lock);
868 			return -EIO;
869 		}
870 
871 		/*
872 		 * Higher sequences will wait for this one so skip them.
873 		 * Don't wait for our own sequence, either.
874 		 */
875 		if (ctx->sequence >= sequence)
876 			continue;
877 
878 		/* Wait until the LSN for the record has been recorded. */
879 		switch (record) {
880 		case _START_RECORD:
881 			if (!ctx->start_lsn) {
882 				xlog_wait(&cil->xc_start_wait, &cil->xc_push_lock);
883 				goto restart;
884 			}
885 			break;
886 		case _COMMIT_RECORD:
887 			if (!ctx->commit_lsn) {
888 				xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
889 				goto restart;
890 			}
891 			break;
892 		}
893 	}
894 	spin_unlock(&cil->xc_push_lock);
895 	return 0;
896 }
897 
898 /*
899  * Write out the log vector change now attached to the CIL context. This will
900  * write a start record that needs to be strictly ordered in ascending CIL
901  * sequence order so that log recovery will always use in-order start LSNs when
902  * replaying checkpoints.
903  */
904 static int
xlog_cil_write_chain(struct xfs_cil_ctx * ctx,uint32_t chain_len)905 xlog_cil_write_chain(
906 	struct xfs_cil_ctx	*ctx,
907 	uint32_t		chain_len)
908 {
909 	struct xlog		*log = ctx->cil->xc_log;
910 	int			error;
911 
912 	error = xlog_cil_order_write(ctx->cil, ctx->sequence, _START_RECORD);
913 	if (error)
914 		return error;
915 	return xlog_write(log, ctx, &ctx->lv_chain, ctx->ticket, chain_len);
916 }
917 
918 /*
919  * Write out the commit record of a checkpoint transaction to close off a
920  * running log write. These commit records are strictly ordered in ascending CIL
921  * sequence order so that log recovery will always replay the checkpoints in the
922  * correct order.
923  */
924 static int
xlog_cil_write_commit_record(struct xfs_cil_ctx * ctx)925 xlog_cil_write_commit_record(
926 	struct xfs_cil_ctx	*ctx)
927 {
928 	struct xlog		*log = ctx->cil->xc_log;
929 	struct xlog_op_header	ophdr = {
930 		.oh_clientid = XFS_TRANSACTION,
931 		.oh_tid = cpu_to_be32(ctx->ticket->t_tid),
932 		.oh_flags = XLOG_COMMIT_TRANS,
933 	};
934 	struct xfs_log_iovec	reg = {
935 		.i_addr = &ophdr,
936 		.i_len = sizeof(struct xlog_op_header),
937 		.i_type = XLOG_REG_TYPE_COMMIT,
938 	};
939 	struct xfs_log_vec	vec = {
940 		.lv_niovecs = 1,
941 		.lv_iovecp = &reg,
942 	};
943 	int			error;
944 	LIST_HEAD(lv_chain);
945 	list_add(&vec.lv_list, &lv_chain);
946 
947 	if (xlog_is_shutdown(log))
948 		return -EIO;
949 
950 	error = xlog_cil_order_write(ctx->cil, ctx->sequence, _COMMIT_RECORD);
951 	if (error)
952 		return error;
953 
954 	/* account for space used by record data */
955 	ctx->ticket->t_curr_res -= reg.i_len;
956 	error = xlog_write(log, ctx, &lv_chain, ctx->ticket, reg.i_len);
957 	if (error)
958 		xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
959 	return error;
960 }
961 
962 struct xlog_cil_trans_hdr {
963 	struct xlog_op_header	oph[2];
964 	struct xfs_trans_header	thdr;
965 	struct xfs_log_iovec	lhdr[2];
966 };
967 
968 /*
969  * Build a checkpoint transaction header to begin the journal transaction.  We
970  * need to account for the space used by the transaction header here as it is
971  * not accounted for in xlog_write().
972  *
973  * This is the only place we write a transaction header, so we also build the
974  * log opheaders that indicate the start of a log transaction and wrap the
975  * transaction header. We keep the start record in it's own log vector rather
976  * than compacting them into a single region as this ends up making the logic
977  * in xlog_write() for handling empty opheaders for start, commit and unmount
978  * records much simpler.
979  */
980 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)981 xlog_cil_build_trans_hdr(
982 	struct xfs_cil_ctx	*ctx,
983 	struct xlog_cil_trans_hdr *hdr,
984 	struct xfs_log_vec	*lvhdr,
985 	int			num_iovecs)
986 {
987 	struct xlog_ticket	*tic = ctx->ticket;
988 	__be32			tid = cpu_to_be32(tic->t_tid);
989 
990 	memset(hdr, 0, sizeof(*hdr));
991 
992 	/* Log start record */
993 	hdr->oph[0].oh_tid = tid;
994 	hdr->oph[0].oh_clientid = XFS_TRANSACTION;
995 	hdr->oph[0].oh_flags = XLOG_START_TRANS;
996 
997 	/* log iovec region pointer */
998 	hdr->lhdr[0].i_addr = &hdr->oph[0];
999 	hdr->lhdr[0].i_len = sizeof(struct xlog_op_header);
1000 	hdr->lhdr[0].i_type = XLOG_REG_TYPE_LRHEADER;
1001 
1002 	/* log opheader */
1003 	hdr->oph[1].oh_tid = tid;
1004 	hdr->oph[1].oh_clientid = XFS_TRANSACTION;
1005 	hdr->oph[1].oh_len = cpu_to_be32(sizeof(struct xfs_trans_header));
1006 
1007 	/* transaction header in host byte order format */
1008 	hdr->thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
1009 	hdr->thdr.th_type = XFS_TRANS_CHECKPOINT;
1010 	hdr->thdr.th_tid = tic->t_tid;
1011 	hdr->thdr.th_num_items = num_iovecs;
1012 
1013 	/* log iovec region pointer */
1014 	hdr->lhdr[1].i_addr = &hdr->oph[1];
1015 	hdr->lhdr[1].i_len = sizeof(struct xlog_op_header) +
1016 				sizeof(struct xfs_trans_header);
1017 	hdr->lhdr[1].i_type = XLOG_REG_TYPE_TRANSHDR;
1018 
1019 	lvhdr->lv_niovecs = 2;
1020 	lvhdr->lv_iovecp = &hdr->lhdr[0];
1021 	lvhdr->lv_bytes = hdr->lhdr[0].i_len + hdr->lhdr[1].i_len;
1022 
1023 	tic->t_curr_res -= lvhdr->lv_bytes;
1024 }
1025 
1026 /*
1027  * CIL item reordering compare function. We want to order in ascending ID order,
1028  * but we want to leave items with the same ID in the order they were added to
1029  * the list. This is important for operations like reflink where we log 4 order
1030  * dependent intents in a single transaction when we overwrite an existing
1031  * shared extent with a new shared extent. i.e. BUI(unmap), CUI(drop),
1032  * CUI (inc), BUI(remap)...
1033  */
1034 static int
xlog_cil_order_cmp(void * priv,const struct list_head * a,const struct list_head * b)1035 xlog_cil_order_cmp(
1036 	void			*priv,
1037 	const struct list_head	*a,
1038 	const struct list_head	*b)
1039 {
1040 	struct xfs_log_vec	*l1 = container_of(a, struct xfs_log_vec, lv_list);
1041 	struct xfs_log_vec	*l2 = container_of(b, struct xfs_log_vec, lv_list);
1042 
1043 	return l1->lv_order_id > l2->lv_order_id;
1044 }
1045 
1046 /*
1047  * Pull all the log vectors off the items in the CIL, and remove the items from
1048  * the CIL. We don't need the CIL lock here because it's only needed on the
1049  * transaction commit side which is currently locked out by the flush lock.
1050  *
1051  * If a log item is marked with a whiteout, we do not need to write it to the
1052  * journal and so we just move them to the whiteout list for the caller to
1053  * dispose of appropriately.
1054  */
1055 static void
xlog_cil_build_lv_chain(struct xfs_cil_ctx * ctx,struct list_head * whiteouts,uint32_t * num_iovecs,uint32_t * num_bytes)1056 xlog_cil_build_lv_chain(
1057 	struct xfs_cil_ctx	*ctx,
1058 	struct list_head	*whiteouts,
1059 	uint32_t		*num_iovecs,
1060 	uint32_t		*num_bytes)
1061 {
1062 	while (!list_empty(&ctx->log_items)) {
1063 		struct xfs_log_item	*item;
1064 		struct xfs_log_vec	*lv;
1065 
1066 		item = list_first_entry(&ctx->log_items,
1067 					struct xfs_log_item, li_cil);
1068 
1069 		if (test_bit(XFS_LI_WHITEOUT, &item->li_flags)) {
1070 			list_move(&item->li_cil, whiteouts);
1071 			trace_xfs_cil_whiteout_skip(item);
1072 			continue;
1073 		}
1074 
1075 		lv = item->li_lv;
1076 		lv->lv_order_id = item->li_order_id;
1077 
1078 		/* we don't write ordered log vectors */
1079 		if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
1080 			*num_bytes += lv->lv_bytes;
1081 		*num_iovecs += lv->lv_niovecs;
1082 		list_add_tail(&lv->lv_list, &ctx->lv_chain);
1083 
1084 		list_del_init(&item->li_cil);
1085 		item->li_order_id = 0;
1086 		item->li_lv = NULL;
1087 	}
1088 }
1089 
1090 static void
xlog_cil_cleanup_whiteouts(struct list_head * whiteouts)1091 xlog_cil_cleanup_whiteouts(
1092 	struct list_head	*whiteouts)
1093 {
1094 	while (!list_empty(whiteouts)) {
1095 		struct xfs_log_item *item = list_first_entry(whiteouts,
1096 						struct xfs_log_item, li_cil);
1097 		list_del_init(&item->li_cil);
1098 		trace_xfs_cil_whiteout_unpin(item);
1099 		item->li_ops->iop_unpin(item, 1);
1100 	}
1101 }
1102 
1103 /*
1104  * Push the Committed Item List to the log.
1105  *
1106  * If the current sequence is the same as xc_push_seq we need to do a flush. If
1107  * xc_push_seq is less than the current sequence, then it has already been
1108  * flushed and we don't need to do anything - the caller will wait for it to
1109  * complete if necessary.
1110  *
1111  * xc_push_seq is checked unlocked against the sequence number for a match.
1112  * Hence we can allow log forces to run racily and not issue pushes for the
1113  * same sequence twice.  If we get a race between multiple pushes for the same
1114  * sequence they will block on the first one and then abort, hence avoiding
1115  * needless pushes.
1116  */
1117 static void
xlog_cil_push_work(struct work_struct * work)1118 xlog_cil_push_work(
1119 	struct work_struct	*work)
1120 {
1121 	struct xfs_cil_ctx	*ctx =
1122 		container_of(work, struct xfs_cil_ctx, push_work);
1123 	struct xfs_cil		*cil = ctx->cil;
1124 	struct xlog		*log = cil->xc_log;
1125 	struct xfs_cil_ctx	*new_ctx;
1126 	int			num_iovecs = 0;
1127 	int			num_bytes = 0;
1128 	int			error = 0;
1129 	struct xlog_cil_trans_hdr thdr;
1130 	struct xfs_log_vec	lvhdr = {};
1131 	xfs_csn_t		push_seq;
1132 	bool			push_commit_stable;
1133 	LIST_HEAD		(whiteouts);
1134 	struct xlog_ticket	*ticket;
1135 
1136 	new_ctx = xlog_cil_ctx_alloc();
1137 	new_ctx->ticket = xlog_cil_ticket_alloc(log);
1138 
1139 	down_write(&cil->xc_ctx_lock);
1140 
1141 	spin_lock(&cil->xc_push_lock);
1142 	push_seq = cil->xc_push_seq;
1143 	ASSERT(push_seq <= ctx->sequence);
1144 	push_commit_stable = cil->xc_push_commit_stable;
1145 	cil->xc_push_commit_stable = false;
1146 
1147 	/*
1148 	 * As we are about to switch to a new, empty CIL context, we no longer
1149 	 * need to throttle tasks on CIL space overruns. Wake any waiters that
1150 	 * the hard push throttle may have caught so they can start committing
1151 	 * to the new context. The ctx->xc_push_lock provides the serialisation
1152 	 * necessary for safely using the lockless waitqueue_active() check in
1153 	 * this context.
1154 	 */
1155 	if (waitqueue_active(&cil->xc_push_wait))
1156 		wake_up_all(&cil->xc_push_wait);
1157 
1158 	xlog_cil_push_pcp_aggregate(cil, ctx);
1159 
1160 	/*
1161 	 * Check if we've anything to push. If there is nothing, then we don't
1162 	 * move on to a new sequence number and so we have to be able to push
1163 	 * this sequence again later.
1164 	 */
1165 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) {
1166 		cil->xc_push_seq = 0;
1167 		spin_unlock(&cil->xc_push_lock);
1168 		goto out_skip;
1169 	}
1170 
1171 
1172 	/* check for a previously pushed sequence */
1173 	if (push_seq < ctx->sequence) {
1174 		spin_unlock(&cil->xc_push_lock);
1175 		goto out_skip;
1176 	}
1177 
1178 	/*
1179 	 * We are now going to push this context, so add it to the committing
1180 	 * list before we do anything else. This ensures that anyone waiting on
1181 	 * this push can easily detect the difference between a "push in
1182 	 * progress" and "CIL is empty, nothing to do".
1183 	 *
1184 	 * IOWs, a wait loop can now check for:
1185 	 *	the current sequence not being found on the committing list;
1186 	 *	an empty CIL; and
1187 	 *	an unchanged sequence number
1188 	 * to detect a push that had nothing to do and therefore does not need
1189 	 * waiting on. If the CIL is not empty, we get put on the committing
1190 	 * list before emptying the CIL and bumping the sequence number. Hence
1191 	 * an empty CIL and an unchanged sequence number means we jumped out
1192 	 * above after doing nothing.
1193 	 *
1194 	 * Hence the waiter will either find the commit sequence on the
1195 	 * committing list or the sequence number will be unchanged and the CIL
1196 	 * still dirty. In that latter case, the push has not yet started, and
1197 	 * so the waiter will have to continue trying to check the CIL
1198 	 * committing list until it is found. In extreme cases of delay, the
1199 	 * sequence may fully commit between the attempts the wait makes to wait
1200 	 * on the commit sequence.
1201 	 */
1202 	list_add(&ctx->committing, &cil->xc_committing);
1203 	spin_unlock(&cil->xc_push_lock);
1204 
1205 	xlog_cil_build_lv_chain(ctx, &whiteouts, &num_iovecs, &num_bytes);
1206 
1207 	/*
1208 	 * Switch the contexts so we can drop the context lock and move out
1209 	 * of a shared context. We can't just go straight to the commit record,
1210 	 * though - we need to synchronise with previous and future commits so
1211 	 * that the commit records are correctly ordered in the log to ensure
1212 	 * that we process items during log IO completion in the correct order.
1213 	 *
1214 	 * For example, if we get an EFI in one checkpoint and the EFD in the
1215 	 * next (e.g. due to log forces), we do not want the checkpoint with
1216 	 * the EFD to be committed before the checkpoint with the EFI.  Hence
1217 	 * we must strictly order the commit records of the checkpoints so
1218 	 * that: a) the checkpoint callbacks are attached to the iclogs in the
1219 	 * correct order; and b) the checkpoints are replayed in correct order
1220 	 * in log recovery.
1221 	 *
1222 	 * Hence we need to add this context to the committing context list so
1223 	 * that higher sequences will wait for us to write out a commit record
1224 	 * before they do.
1225 	 *
1226 	 * xfs_log_force_seq requires us to mirror the new sequence into the cil
1227 	 * structure atomically with the addition of this sequence to the
1228 	 * committing list. This also ensures that we can do unlocked checks
1229 	 * against the current sequence in log forces without risking
1230 	 * deferencing a freed context pointer.
1231 	 */
1232 	spin_lock(&cil->xc_push_lock);
1233 	xlog_cil_ctx_switch(cil, new_ctx);
1234 	spin_unlock(&cil->xc_push_lock);
1235 	up_write(&cil->xc_ctx_lock);
1236 
1237 	/*
1238 	 * Sort the log vector chain before we add the transaction headers.
1239 	 * This ensures we always have the transaction headers at the start
1240 	 * of the chain.
1241 	 */
1242 	list_sort(NULL, &ctx->lv_chain, xlog_cil_order_cmp);
1243 
1244 	/*
1245 	 * Build a checkpoint transaction header and write it to the log to
1246 	 * begin the transaction. We need to account for the space used by the
1247 	 * transaction header here as it is not accounted for in xlog_write().
1248 	 * Add the lvhdr to the head of the lv chain we pass to xlog_write() so
1249 	 * it gets written into the iclog first.
1250 	 */
1251 	xlog_cil_build_trans_hdr(ctx, &thdr, &lvhdr, num_iovecs);
1252 	num_bytes += lvhdr.lv_bytes;
1253 	list_add(&lvhdr.lv_list, &ctx->lv_chain);
1254 
1255 	/*
1256 	 * Take the lvhdr back off the lv_chain immediately after calling
1257 	 * xlog_cil_write_chain() as it should not be passed to log IO
1258 	 * completion.
1259 	 */
1260 	error = xlog_cil_write_chain(ctx, num_bytes);
1261 	list_del(&lvhdr.lv_list);
1262 	if (error)
1263 		goto out_abort_free_ticket;
1264 
1265 	error = xlog_cil_write_commit_record(ctx);
1266 	if (error)
1267 		goto out_abort_free_ticket;
1268 
1269 	/*
1270 	 * Grab the ticket from the ctx so we can ungrant it after releasing the
1271 	 * commit_iclog. The ctx may be freed by the time we return from
1272 	 * releasing the commit_iclog (i.e. checkpoint has been completed and
1273 	 * callback run) so we can't reference the ctx after the call to
1274 	 * xlog_state_release_iclog().
1275 	 */
1276 	ticket = ctx->ticket;
1277 
1278 	/*
1279 	 * If the checkpoint spans multiple iclogs, wait for all previous iclogs
1280 	 * to complete before we submit the commit_iclog. We can't use state
1281 	 * checks for this - ACTIVE can be either a past completed iclog or a
1282 	 * future iclog being filled, while WANT_SYNC through SYNC_DONE can be a
1283 	 * past or future iclog awaiting IO or ordered IO completion to be run.
1284 	 * In the latter case, if it's a future iclog and we wait on it, the we
1285 	 * will hang because it won't get processed through to ic_force_wait
1286 	 * wakeup until this commit_iclog is written to disk.  Hence we use the
1287 	 * iclog header lsn and compare it to the commit lsn to determine if we
1288 	 * need to wait on iclogs or not.
1289 	 */
1290 	spin_lock(&log->l_icloglock);
1291 	if (ctx->start_lsn != ctx->commit_lsn) {
1292 		xfs_lsn_t	plsn;
1293 
1294 		plsn = be64_to_cpu(ctx->commit_iclog->ic_prev->ic_header.h_lsn);
1295 		if (plsn && XFS_LSN_CMP(plsn, ctx->commit_lsn) < 0) {
1296 			/*
1297 			 * Waiting on ic_force_wait orders the completion of
1298 			 * iclogs older than ic_prev. Hence we only need to wait
1299 			 * on the most recent older iclog here.
1300 			 */
1301 			xlog_wait_on_iclog(ctx->commit_iclog->ic_prev);
1302 			spin_lock(&log->l_icloglock);
1303 		}
1304 
1305 		/*
1306 		 * We need to issue a pre-flush so that the ordering for this
1307 		 * checkpoint is correctly preserved down to stable storage.
1308 		 */
1309 		ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
1310 	}
1311 
1312 	/*
1313 	 * The commit iclog must be written to stable storage to guarantee
1314 	 * journal IO vs metadata writeback IO is correctly ordered on stable
1315 	 * storage.
1316 	 *
1317 	 * If the push caller needs the commit to be immediately stable and the
1318 	 * commit_iclog is not yet marked as XLOG_STATE_WANT_SYNC to indicate it
1319 	 * will be written when released, switch it's state to WANT_SYNC right
1320 	 * now.
1321 	 */
1322 	ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FUA;
1323 	if (push_commit_stable &&
1324 	    ctx->commit_iclog->ic_state == XLOG_STATE_ACTIVE)
1325 		xlog_state_switch_iclogs(log, ctx->commit_iclog, 0);
1326 	ticket = ctx->ticket;
1327 	xlog_state_release_iclog(log, ctx->commit_iclog, ticket);
1328 
1329 	/* Not safe to reference ctx now! */
1330 
1331 	spin_unlock(&log->l_icloglock);
1332 	xlog_cil_cleanup_whiteouts(&whiteouts);
1333 	xfs_log_ticket_ungrant(log, ticket);
1334 	return;
1335 
1336 out_skip:
1337 	up_write(&cil->xc_ctx_lock);
1338 	xfs_log_ticket_put(new_ctx->ticket);
1339 	kmem_free(new_ctx);
1340 	return;
1341 
1342 out_abort_free_ticket:
1343 	ASSERT(xlog_is_shutdown(log));
1344 	xlog_cil_cleanup_whiteouts(&whiteouts);
1345 	if (!ctx->commit_iclog) {
1346 		xfs_log_ticket_ungrant(log, ctx->ticket);
1347 		xlog_cil_committed(ctx);
1348 		return;
1349 	}
1350 	spin_lock(&log->l_icloglock);
1351 	ticket = ctx->ticket;
1352 	xlog_state_release_iclog(log, ctx->commit_iclog, ticket);
1353 	/* Not safe to reference ctx now! */
1354 	spin_unlock(&log->l_icloglock);
1355 	xfs_log_ticket_ungrant(log, ticket);
1356 }
1357 
1358 /*
1359  * We need to push CIL every so often so we don't cache more than we can fit in
1360  * the log. The limit really is that a checkpoint can't be more than half the
1361  * log (the current checkpoint is not allowed to overwrite the previous
1362  * checkpoint), but commit latency and memory usage limit this to a smaller
1363  * size.
1364  */
1365 static void
xlog_cil_push_background(struct xlog * log)1366 xlog_cil_push_background(
1367 	struct xlog	*log) __releases(cil->xc_ctx_lock)
1368 {
1369 	struct xfs_cil	*cil = log->l_cilp;
1370 	int		space_used = atomic_read(&cil->xc_ctx->space_used);
1371 
1372 	/*
1373 	 * The cil won't be empty because we are called while holding the
1374 	 * context lock so whatever we added to the CIL will still be there.
1375 	 */
1376 	ASSERT(!test_bit(XLOG_CIL_EMPTY, &cil->xc_flags));
1377 
1378 	/*
1379 	 * We are done if:
1380 	 * - we haven't used up all the space available yet; or
1381 	 * - we've already queued up a push; and
1382 	 * - we're not over the hard limit; and
1383 	 * - nothing has been over the hard limit.
1384 	 *
1385 	 * If so, we don't need to take the push lock as there's nothing to do.
1386 	 */
1387 	if (space_used < XLOG_CIL_SPACE_LIMIT(log) ||
1388 	    (cil->xc_push_seq == cil->xc_current_sequence &&
1389 	     space_used < XLOG_CIL_BLOCKING_SPACE_LIMIT(log) &&
1390 	     !waitqueue_active(&cil->xc_push_wait))) {
1391 		up_read(&cil->xc_ctx_lock);
1392 		return;
1393 	}
1394 
1395 	spin_lock(&cil->xc_push_lock);
1396 	if (cil->xc_push_seq < cil->xc_current_sequence) {
1397 		cil->xc_push_seq = cil->xc_current_sequence;
1398 		queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
1399 	}
1400 
1401 	/*
1402 	 * Drop the context lock now, we can't hold that if we need to sleep
1403 	 * because we are over the blocking threshold. The push_lock is still
1404 	 * held, so blocking threshold sleep/wakeup is still correctly
1405 	 * serialised here.
1406 	 */
1407 	up_read(&cil->xc_ctx_lock);
1408 
1409 	/*
1410 	 * If we are well over the space limit, throttle the work that is being
1411 	 * done until the push work on this context has begun. Enforce the hard
1412 	 * throttle on all transaction commits once it has been activated, even
1413 	 * if the committing transactions have resulted in the space usage
1414 	 * dipping back down under the hard limit.
1415 	 *
1416 	 * The ctx->xc_push_lock provides the serialisation necessary for safely
1417 	 * calling xlog_cil_over_hard_limit() in this context.
1418 	 */
1419 	if (xlog_cil_over_hard_limit(log, space_used)) {
1420 		trace_xfs_log_cil_wait(log, cil->xc_ctx->ticket);
1421 		ASSERT(space_used < log->l_logsize);
1422 		xlog_wait(&cil->xc_push_wait, &cil->xc_push_lock);
1423 		return;
1424 	}
1425 
1426 	spin_unlock(&cil->xc_push_lock);
1427 
1428 }
1429 
1430 /*
1431  * xlog_cil_push_now() is used to trigger an immediate CIL push to the sequence
1432  * number that is passed. When it returns, the work will be queued for
1433  * @push_seq, but it won't be completed.
1434  *
1435  * If the caller is performing a synchronous force, we will flush the workqueue
1436  * to get previously queued work moving to minimise the wait time they will
1437  * undergo waiting for all outstanding pushes to complete. The caller is
1438  * expected to do the required waiting for push_seq to complete.
1439  *
1440  * If the caller is performing an async push, we need to ensure that the
1441  * checkpoint is fully flushed out of the iclogs when we finish the push. If we
1442  * don't do this, then the commit record may remain sitting in memory in an
1443  * ACTIVE iclog. This then requires another full log force to push to disk,
1444  * which defeats the purpose of having an async, non-blocking CIL force
1445  * mechanism. Hence in this case we need to pass a flag to the push work to
1446  * indicate it needs to flush the commit record itself.
1447  */
1448 static void
xlog_cil_push_now(struct xlog * log,xfs_lsn_t push_seq,bool async)1449 xlog_cil_push_now(
1450 	struct xlog	*log,
1451 	xfs_lsn_t	push_seq,
1452 	bool		async)
1453 {
1454 	struct xfs_cil	*cil = log->l_cilp;
1455 
1456 	if (!cil)
1457 		return;
1458 
1459 	ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
1460 
1461 	/* start on any pending background push to minimise wait time on it */
1462 	if (!async)
1463 		flush_workqueue(cil->xc_push_wq);
1464 
1465 	spin_lock(&cil->xc_push_lock);
1466 
1467 	/*
1468 	 * If this is an async flush request, we always need to set the
1469 	 * xc_push_commit_stable flag even if something else has already queued
1470 	 * a push. The flush caller is asking for the CIL to be on stable
1471 	 * storage when the next push completes, so regardless of who has queued
1472 	 * the push, the flush requires stable semantics from it.
1473 	 */
1474 	cil->xc_push_commit_stable = async;
1475 
1476 	/*
1477 	 * If the CIL is empty or we've already pushed the sequence then
1478 	 * there's no more work that we need to do.
1479 	 */
1480 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags) ||
1481 	    push_seq <= cil->xc_push_seq) {
1482 		spin_unlock(&cil->xc_push_lock);
1483 		return;
1484 	}
1485 
1486 	cil->xc_push_seq = push_seq;
1487 	queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
1488 	spin_unlock(&cil->xc_push_lock);
1489 }
1490 
1491 bool
xlog_cil_empty(struct xlog * log)1492 xlog_cil_empty(
1493 	struct xlog	*log)
1494 {
1495 	struct xfs_cil	*cil = log->l_cilp;
1496 	bool		empty = false;
1497 
1498 	spin_lock(&cil->xc_push_lock);
1499 	if (test_bit(XLOG_CIL_EMPTY, &cil->xc_flags))
1500 		empty = true;
1501 	spin_unlock(&cil->xc_push_lock);
1502 	return empty;
1503 }
1504 
1505 /*
1506  * If there are intent done items in this transaction and the related intent was
1507  * committed in the current (same) CIL checkpoint, we don't need to write either
1508  * the intent or intent done item to the journal as the change will be
1509  * journalled atomically within this checkpoint. As we cannot remove items from
1510  * the CIL here, mark the related intent with a whiteout so that the CIL push
1511  * can remove it rather than writing it to the journal. Then remove the intent
1512  * done item from the current transaction and release it so it doesn't get put
1513  * into the CIL at all.
1514  */
1515 static uint32_t
xlog_cil_process_intents(struct xfs_cil * cil,struct xfs_trans * tp)1516 xlog_cil_process_intents(
1517 	struct xfs_cil		*cil,
1518 	struct xfs_trans	*tp)
1519 {
1520 	struct xfs_log_item	*lip, *ilip, *next;
1521 	uint32_t		len = 0;
1522 
1523 	list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
1524 		if (!(lip->li_ops->flags & XFS_ITEM_INTENT_DONE))
1525 			continue;
1526 
1527 		ilip = lip->li_ops->iop_intent(lip);
1528 		if (!ilip || !xlog_item_in_current_chkpt(cil, ilip))
1529 			continue;
1530 		set_bit(XFS_LI_WHITEOUT, &ilip->li_flags);
1531 		trace_xfs_cil_whiteout_mark(ilip);
1532 		len += ilip->li_lv->lv_bytes;
1533 		kmem_free(ilip->li_lv);
1534 		ilip->li_lv = NULL;
1535 
1536 		xfs_trans_del_item(lip);
1537 		lip->li_ops->iop_release(lip);
1538 	}
1539 	return len;
1540 }
1541 
1542 /*
1543  * Commit a transaction with the given vector to the Committed Item List.
1544  *
1545  * To do this, we need to format the item, pin it in memory if required and
1546  * account for the space used by the transaction. Once we have done that we
1547  * need to release the unused reservation for the transaction, attach the
1548  * transaction to the checkpoint context so we carry the busy extents through
1549  * to checkpoint completion, and then unlock all the items in the transaction.
1550  *
1551  * Called with the context lock already held in read mode to lock out
1552  * background commit, returns without it held once background commits are
1553  * allowed again.
1554  */
1555 void
xlog_cil_commit(struct xlog * log,struct xfs_trans * tp,xfs_csn_t * commit_seq,bool regrant)1556 xlog_cil_commit(
1557 	struct xlog		*log,
1558 	struct xfs_trans	*tp,
1559 	xfs_csn_t		*commit_seq,
1560 	bool			regrant)
1561 {
1562 	struct xfs_cil		*cil = log->l_cilp;
1563 	struct xfs_log_item	*lip, *next;
1564 	uint32_t		released_space = 0;
1565 
1566 	/*
1567 	 * Do all necessary memory allocation before we lock the CIL.
1568 	 * This ensures the allocation does not deadlock with a CIL
1569 	 * push in memory reclaim (e.g. from kswapd).
1570 	 */
1571 	xlog_cil_alloc_shadow_bufs(log, tp);
1572 
1573 	/* lock out background commit */
1574 	down_read(&cil->xc_ctx_lock);
1575 
1576 	if (tp->t_flags & XFS_TRANS_HAS_INTENT_DONE)
1577 		released_space = xlog_cil_process_intents(cil, tp);
1578 
1579 	xlog_cil_insert_items(log, tp, released_space);
1580 
1581 	if (regrant && !xlog_is_shutdown(log))
1582 		xfs_log_ticket_regrant(log, tp->t_ticket);
1583 	else
1584 		xfs_log_ticket_ungrant(log, tp->t_ticket);
1585 	tp->t_ticket = NULL;
1586 	xfs_trans_unreserve_and_mod_sb(tp);
1587 
1588 	/*
1589 	 * Once all the items of the transaction have been copied to the CIL,
1590 	 * the items can be unlocked and possibly freed.
1591 	 *
1592 	 * This needs to be done before we drop the CIL context lock because we
1593 	 * have to update state in the log items and unlock them before they go
1594 	 * to disk. If we don't, then the CIL checkpoint can race with us and
1595 	 * we can run checkpoint completion before we've updated and unlocked
1596 	 * the log items. This affects (at least) processing of stale buffers,
1597 	 * inodes and EFIs.
1598 	 */
1599 	trace_xfs_trans_commit_items(tp, _RET_IP_);
1600 	list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
1601 		xfs_trans_del_item(lip);
1602 		if (lip->li_ops->iop_committing)
1603 			lip->li_ops->iop_committing(lip, cil->xc_ctx->sequence);
1604 	}
1605 	if (commit_seq)
1606 		*commit_seq = cil->xc_ctx->sequence;
1607 
1608 	/* xlog_cil_push_background() releases cil->xc_ctx_lock */
1609 	xlog_cil_push_background(log);
1610 }
1611 
1612 /*
1613  * Flush the CIL to stable storage but don't wait for it to complete. This
1614  * requires the CIL push to ensure the commit record for the push hits the disk,
1615  * but otherwise is no different to a push done from a log force.
1616  */
1617 void
xlog_cil_flush(struct xlog * log)1618 xlog_cil_flush(
1619 	struct xlog	*log)
1620 {
1621 	xfs_csn_t	seq = log->l_cilp->xc_current_sequence;
1622 
1623 	trace_xfs_log_force(log->l_mp, seq, _RET_IP_);
1624 	xlog_cil_push_now(log, seq, true);
1625 
1626 	/*
1627 	 * If the CIL is empty, make sure that any previous checkpoint that may
1628 	 * still be in an active iclog is pushed to stable storage.
1629 	 */
1630 	if (test_bit(XLOG_CIL_EMPTY, &log->l_cilp->xc_flags))
1631 		xfs_log_force(log->l_mp, 0);
1632 }
1633 
1634 /*
1635  * Conditionally push the CIL based on the sequence passed in.
1636  *
1637  * We only need to push if we haven't already pushed the sequence number given.
1638  * Hence the only time we will trigger a push here is if the push sequence is
1639  * the same as the current context.
1640  *
1641  * We return the current commit lsn to allow the callers to determine if a
1642  * iclog flush is necessary following this call.
1643  */
1644 xfs_lsn_t
xlog_cil_force_seq(struct xlog * log,xfs_csn_t sequence)1645 xlog_cil_force_seq(
1646 	struct xlog	*log,
1647 	xfs_csn_t	sequence)
1648 {
1649 	struct xfs_cil		*cil = log->l_cilp;
1650 	struct xfs_cil_ctx	*ctx;
1651 	xfs_lsn_t		commit_lsn = NULLCOMMITLSN;
1652 
1653 	ASSERT(sequence <= cil->xc_current_sequence);
1654 
1655 	if (!sequence)
1656 		sequence = cil->xc_current_sequence;
1657 	trace_xfs_log_force(log->l_mp, sequence, _RET_IP_);
1658 
1659 	/*
1660 	 * check to see if we need to force out the current context.
1661 	 * xlog_cil_push() handles racing pushes for the same sequence,
1662 	 * so no need to deal with it here.
1663 	 */
1664 restart:
1665 	xlog_cil_push_now(log, sequence, false);
1666 
1667 	/*
1668 	 * See if we can find a previous sequence still committing.
1669 	 * We need to wait for all previous sequence commits to complete
1670 	 * before allowing the force of push_seq to go ahead. Hence block
1671 	 * on commits for those as well.
1672 	 */
1673 	spin_lock(&cil->xc_push_lock);
1674 	list_for_each_entry(ctx, &cil->xc_committing, committing) {
1675 		/*
1676 		 * Avoid getting stuck in this loop because we were woken by the
1677 		 * shutdown, but then went back to sleep once already in the
1678 		 * shutdown state.
1679 		 */
1680 		if (xlog_is_shutdown(log))
1681 			goto out_shutdown;
1682 		if (ctx->sequence > sequence)
1683 			continue;
1684 		if (!ctx->commit_lsn) {
1685 			/*
1686 			 * It is still being pushed! Wait for the push to
1687 			 * complete, then start again from the beginning.
1688 			 */
1689 			XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
1690 			xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
1691 			goto restart;
1692 		}
1693 		if (ctx->sequence != sequence)
1694 			continue;
1695 		/* found it! */
1696 		commit_lsn = ctx->commit_lsn;
1697 	}
1698 
1699 	/*
1700 	 * The call to xlog_cil_push_now() executes the push in the background.
1701 	 * Hence by the time we have got here it our sequence may not have been
1702 	 * pushed yet. This is true if the current sequence still matches the
1703 	 * push sequence after the above wait loop and the CIL still contains
1704 	 * dirty objects. This is guaranteed by the push code first adding the
1705 	 * context to the committing list before emptying the CIL.
1706 	 *
1707 	 * Hence if we don't find the context in the committing list and the
1708 	 * current sequence number is unchanged then the CIL contents are
1709 	 * significant.  If the CIL is empty, if means there was nothing to push
1710 	 * and that means there is nothing to wait for. If the CIL is not empty,
1711 	 * it means we haven't yet started the push, because if it had started
1712 	 * we would have found the context on the committing list.
1713 	 */
1714 	if (sequence == cil->xc_current_sequence &&
1715 	    !test_bit(XLOG_CIL_EMPTY, &cil->xc_flags)) {
1716 		spin_unlock(&cil->xc_push_lock);
1717 		goto restart;
1718 	}
1719 
1720 	spin_unlock(&cil->xc_push_lock);
1721 	return commit_lsn;
1722 
1723 	/*
1724 	 * We detected a shutdown in progress. We need to trigger the log force
1725 	 * to pass through it's iclog state machine error handling, even though
1726 	 * we are already in a shutdown state. Hence we can't return
1727 	 * NULLCOMMITLSN here as that has special meaning to log forces (i.e.
1728 	 * LSN is already stable), so we return a zero LSN instead.
1729 	 */
1730 out_shutdown:
1731 	spin_unlock(&cil->xc_push_lock);
1732 	return 0;
1733 }
1734 
1735 /*
1736  * Perform initial CIL structure initialisation.
1737  */
1738 int
xlog_cil_init(struct xlog * log)1739 xlog_cil_init(
1740 	struct xlog		*log)
1741 {
1742 	struct xfs_cil		*cil;
1743 	struct xfs_cil_ctx	*ctx;
1744 	struct xlog_cil_pcp	*cilpcp;
1745 	int			cpu;
1746 
1747 	cil = kmem_zalloc(sizeof(*cil), KM_MAYFAIL);
1748 	if (!cil)
1749 		return -ENOMEM;
1750 	/*
1751 	 * Limit the CIL pipeline depth to 4 concurrent works to bound the
1752 	 * concurrency the log spinlocks will be exposed to.
1753 	 */
1754 	cil->xc_push_wq = alloc_workqueue("xfs-cil/%s",
1755 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_UNBOUND),
1756 			4, log->l_mp->m_super->s_id);
1757 	if (!cil->xc_push_wq)
1758 		goto out_destroy_cil;
1759 
1760 	cil->xc_log = log;
1761 	cil->xc_pcp = alloc_percpu(struct xlog_cil_pcp);
1762 	if (!cil->xc_pcp)
1763 		goto out_destroy_wq;
1764 
1765 	for_each_possible_cpu(cpu) {
1766 		cilpcp = per_cpu_ptr(cil->xc_pcp, cpu);
1767 		INIT_LIST_HEAD(&cilpcp->busy_extents);
1768 		INIT_LIST_HEAD(&cilpcp->log_items);
1769 	}
1770 
1771 	INIT_LIST_HEAD(&cil->xc_committing);
1772 	spin_lock_init(&cil->xc_push_lock);
1773 	init_waitqueue_head(&cil->xc_push_wait);
1774 	init_rwsem(&cil->xc_ctx_lock);
1775 	init_waitqueue_head(&cil->xc_start_wait);
1776 	init_waitqueue_head(&cil->xc_commit_wait);
1777 	log->l_cilp = cil;
1778 
1779 	ctx = xlog_cil_ctx_alloc();
1780 	xlog_cil_ctx_switch(cil, ctx);
1781 	return 0;
1782 
1783 out_destroy_wq:
1784 	destroy_workqueue(cil->xc_push_wq);
1785 out_destroy_cil:
1786 	kmem_free(cil);
1787 	return -ENOMEM;
1788 }
1789 
1790 void
xlog_cil_destroy(struct xlog * log)1791 xlog_cil_destroy(
1792 	struct xlog	*log)
1793 {
1794 	struct xfs_cil	*cil = log->l_cilp;
1795 
1796 	if (cil->xc_ctx) {
1797 		if (cil->xc_ctx->ticket)
1798 			xfs_log_ticket_put(cil->xc_ctx->ticket);
1799 		kmem_free(cil->xc_ctx);
1800 	}
1801 
1802 	ASSERT(test_bit(XLOG_CIL_EMPTY, &cil->xc_flags));
1803 	free_percpu(cil->xc_pcp);
1804 	destroy_workqueue(cil->xc_push_wq);
1805 	kmem_free(cil);
1806 }
1807 
1808