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