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