1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved.
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"
17 #include "xfs_log_priv.h"
18 #include "xfs_trace.h"
20 struct workqueue_struct *xfs_discard_wq;
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.
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.
34 static struct xlog_ticket *
35 xlog_cil_ticket_alloc(
38 struct xlog_ticket *tic;
40 tic = xlog_ticket_alloc(log, 0, 1, 0);
43 * set the current reservation to zero so we know to steal the basic
44 * transaction overhead reservation from the first transaction commit.
51 * Check if the current log item was first committed in this sequence.
52 * We can't rely on just the log item being in the CIL, we have to check
53 * the recorded commit sequence number.
55 * Note: for this to be used in a non-racy manner, it has to be called with
56 * CIL flushing locked out. As a result, it should only be used during the
57 * transaction commit process when deciding what to format into the item.
60 xlog_item_in_current_chkpt(
62 struct xfs_log_item *lip)
64 if (list_empty(&lip->li_cil))
68 * li_seq is written on the first commit of a log item to record the
69 * first checkpoint it is written to. Hence if it is different to the
70 * current sequence, we're in a new checkpoint.
72 return lip->li_seq == READ_ONCE(cil->xc_current_sequence);
76 xfs_log_item_in_current_chkpt(
77 struct xfs_log_item *lip)
79 return xlog_item_in_current_chkpt(lip->li_log->l_cilp, lip);
83 * Unavoidable forward declaration - xlog_cil_push_work() calls
84 * xlog_cil_ctx_alloc() itself.
86 static void xlog_cil_push_work(struct work_struct *work);
88 static struct xfs_cil_ctx *
89 xlog_cil_ctx_alloc(void)
91 struct xfs_cil_ctx *ctx;
93 ctx = kmem_zalloc(sizeof(*ctx), KM_NOFS);
94 INIT_LIST_HEAD(&ctx->committing);
95 INIT_LIST_HEAD(&ctx->busy_extents);
96 INIT_WORK(&ctx->push_work, xlog_cil_push_work);
103 struct xfs_cil_ctx *ctx)
105 ctx->sequence = ++cil->xc_current_sequence;
111 * After the first stage of log recovery is done, we know where the head and
112 * tail of the log are. We need this log initialisation done before we can
113 * initialise the first CIL checkpoint context.
115 * Here we allocate a log ticket to track space usage during a CIL push. This
116 * ticket is passed to xlog_write() directly so that we don't slowly leak log
117 * space by failing to account for space used by log headers and additional
118 * region headers for split regions.
121 xlog_cil_init_post_recovery(
124 log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log);
125 log->l_cilp->xc_ctx->sequence = 1;
129 xlog_cil_iovec_space(
132 return round_up((sizeof(struct xfs_log_vec) +
133 niovecs * sizeof(struct xfs_log_iovec)),
138 * Allocate or pin log vector buffers for CIL insertion.
140 * The CIL currently uses disposable buffers for copying a snapshot of the
141 * modified items into the log during a push. The biggest problem with this is
142 * the requirement to allocate the disposable buffer during the commit if:
143 * a) does not exist; or
146 * If we do this allocation within xlog_cil_insert_format_items(), it is done
147 * under the xc_ctx_lock, which means that a CIL push cannot occur during
148 * the memory allocation. This means that we have a potential deadlock situation
149 * under low memory conditions when we have lots of dirty metadata pinned in
150 * the CIL and we need a CIL commit to occur to free memory.
152 * To avoid this, we need to move the memory allocation outside the
153 * xc_ctx_lock, but because the log vector buffers are disposable, that opens
154 * up a TOCTOU race condition w.r.t. the CIL committing and removing the log
155 * vector buffers between the check and the formatting of the item into the
156 * log vector buffer within the xc_ctx_lock.
158 * Because the log vector buffer needs to be unchanged during the CIL push
159 * process, we cannot share the buffer between the transaction commit (which
160 * modifies the buffer) and the CIL push context that is writing the changes
161 * into the log. This means skipping preallocation of buffer space is
162 * unreliable, but we most definitely do not want to be allocating and freeing
163 * buffers unnecessarily during commits when overwrites can be done safely.
165 * The simplest solution to this problem is to allocate a shadow buffer when a
166 * log item is committed for the second time, and then to only use this buffer
167 * if necessary. The buffer can remain attached to the log item until such time
168 * it is needed, and this is the buffer that is reallocated to match the size of
169 * the incoming modification. Then during the formatting of the item we can swap
170 * the active buffer with the new one if we can't reuse the existing buffer. We
171 * don't free the old buffer as it may be reused on the next modification if
172 * it's size is right, otherwise we'll free and reallocate it at that point.
174 * This function builds a vector for the changes in each log item in the
175 * transaction. It then works out the length of the buffer needed for each log
176 * item, allocates them and attaches the vector to the log item in preparation
177 * for the formatting step which occurs under the xc_ctx_lock.
179 * While this means the memory footprint goes up, it avoids the repeated
180 * alloc/free pattern that repeated modifications of an item would otherwise
181 * cause, and hence minimises the CPU overhead of such behaviour.
184 xlog_cil_alloc_shadow_bufs(
186 struct xfs_trans *tp)
188 struct xfs_log_item *lip;
190 list_for_each_entry(lip, &tp->t_items, li_trans) {
191 struct xfs_log_vec *lv;
195 bool ordered = false;
197 /* Skip items which aren't dirty in this transaction. */
198 if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
201 /* get number of vecs and size of data to be stored */
202 lip->li_ops->iop_size(lip, &niovecs, &nbytes);
205 * Ordered items need to be tracked but we do not wish to write
206 * them. We need a logvec to track the object, but we do not
207 * need an iovec or buffer to be allocated for copying data.
209 if (niovecs == XFS_LOG_VEC_ORDERED) {
216 * We 64-bit align the length of each iovec so that the start of
217 * the next one is naturally aligned. We'll need to account for
218 * that slack space here.
220 * We also add the xlog_op_header to each region when
221 * formatting, but that's not accounted to the size of the item
222 * at this point. Hence we'll need an addition number of bytes
223 * for each vector to hold an opheader.
225 * Then round nbytes up to 64-bit alignment so that the initial
226 * buffer alignment is easy to calculate and verify.
229 (sizeof(uint64_t) + sizeof(struct xlog_op_header));
230 nbytes = round_up(nbytes, sizeof(uint64_t));
233 * The data buffer needs to start 64-bit aligned, so round up
234 * that space to ensure we can align it appropriately and not
235 * overrun the buffer.
237 buf_size = nbytes + xlog_cil_iovec_space(niovecs);
240 * if we have no shadow buffer, or it is too small, we need to
243 if (!lip->li_lv_shadow ||
244 buf_size > lip->li_lv_shadow->lv_size) {
246 * We free and allocate here as a realloc would copy
247 * unnecessary data. We don't use kvzalloc() for the
248 * same reason - we don't need to zero the data area in
249 * the buffer, only the log vector header and the iovec
252 kmem_free(lip->li_lv_shadow);
253 lv = xlog_kvmalloc(buf_size);
255 memset(lv, 0, xlog_cil_iovec_space(niovecs));
258 lv->lv_size = buf_size;
260 lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
262 lv->lv_iovecp = (struct xfs_log_iovec *)&lv[1];
263 lip->li_lv_shadow = lv;
265 /* same or smaller, optimise common overwrite case */
266 lv = lip->li_lv_shadow;
268 lv->lv_buf_len = XFS_LOG_VEC_ORDERED;
275 /* Ensure the lv is set up according to ->iop_size */
276 lv->lv_niovecs = niovecs;
278 /* The allocated data region lies beyond the iovec region */
279 lv->lv_buf = (char *)lv + xlog_cil_iovec_space(niovecs);
285 * Prepare the log item for insertion into the CIL. Calculate the difference in
286 * log space it will consume, and if it is a new item pin it as well.
289 xfs_cil_prepare_item(
291 struct xfs_log_vec *lv,
292 struct xfs_log_vec *old_lv,
295 /* Account for the new LV being passed in */
296 if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
297 *diff_len += lv->lv_bytes;
300 * If there is no old LV, this is the first time we've seen the item in
301 * this CIL context and so we need to pin it. If we are replacing the
302 * old_lv, then remove the space it accounts for and make it the shadow
303 * buffer for later freeing. In both cases we are now switching to the
304 * shadow buffer, so update the pointer to it appropriately.
307 if (lv->lv_item->li_ops->iop_pin)
308 lv->lv_item->li_ops->iop_pin(lv->lv_item);
309 lv->lv_item->li_lv_shadow = NULL;
310 } else if (old_lv != lv) {
311 ASSERT(lv->lv_buf_len != XFS_LOG_VEC_ORDERED);
313 *diff_len -= old_lv->lv_bytes;
314 lv->lv_item->li_lv_shadow = old_lv;
317 /* attach new log vector to log item */
318 lv->lv_item->li_lv = lv;
321 * If this is the first time the item is being committed to the
322 * CIL, store the sequence number on the log item so we can
323 * tell in future commits whether this is the first checkpoint
324 * the item is being committed into.
326 if (!lv->lv_item->li_seq)
327 lv->lv_item->li_seq = log->l_cilp->xc_ctx->sequence;
331 * Format log item into a flat buffers
333 * For delayed logging, we need to hold a formatted buffer containing all the
334 * changes on the log item. This enables us to relog the item in memory and
335 * write it out asynchronously without needing to relock the object that was
336 * modified at the time it gets written into the iclog.
338 * This function takes the prepared log vectors attached to each log item, and
339 * formats the changes into the log vector buffer. The buffer it uses is
340 * dependent on the current state of the vector in the CIL - the shadow lv is
341 * guaranteed to be large enough for the current modification, but we will only
342 * use that if we can't reuse the existing lv. If we can't reuse the existing
343 * lv, then simple swap it out for the shadow lv. We don't free it - that is
344 * done lazily either by th enext modification or the freeing of the log item.
346 * We don't set up region headers during this process; we simply copy the
347 * regions into the flat buffer. We can do this because we still have to do a
348 * formatting step to write the regions into the iclog buffer. Writing the
349 * ophdrs during the iclog write means that we can support splitting large
350 * regions across iclog boundares without needing a change in the format of the
351 * item/region encapsulation.
353 * Hence what we need to do now is change the rewrite the vector array to point
354 * to the copied region inside the buffer we just allocated. This allows us to
355 * format the regions into the iclog as though they are being formatted
356 * directly out of the objects themselves.
359 xlog_cil_insert_format_items(
361 struct xfs_trans *tp,
364 struct xfs_log_item *lip;
366 /* Bail out if we didn't find a log item. */
367 if (list_empty(&tp->t_items)) {
372 list_for_each_entry(lip, &tp->t_items, li_trans) {
373 struct xfs_log_vec *lv;
374 struct xfs_log_vec *old_lv = NULL;
375 struct xfs_log_vec *shadow;
376 bool ordered = false;
378 /* Skip items which aren't dirty in this transaction. */
379 if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
383 * The formatting size information is already attached to
384 * the shadow lv on the log item.
386 shadow = lip->li_lv_shadow;
387 if (shadow->lv_buf_len == XFS_LOG_VEC_ORDERED)
390 /* Skip items that do not have any vectors for writing */
391 if (!shadow->lv_niovecs && !ordered)
394 /* compare to existing item size */
396 if (lip->li_lv && shadow->lv_size <= lip->li_lv->lv_size) {
397 /* same or smaller, optimise common overwrite case */
405 * set the item up as though it is a new insertion so
406 * that the space reservation accounting is correct.
408 *diff_len -= lv->lv_bytes;
410 /* Ensure the lv is set up according to ->iop_size */
411 lv->lv_niovecs = shadow->lv_niovecs;
413 /* reset the lv buffer information for new formatting */
416 lv->lv_buf = (char *)lv +
417 xlog_cil_iovec_space(lv->lv_niovecs);
419 /* switch to shadow buffer! */
423 /* track as an ordered logvec */
424 ASSERT(lip->li_lv == NULL);
429 ASSERT(IS_ALIGNED((unsigned long)lv->lv_buf, sizeof(uint64_t)));
430 lip->li_ops->iop_format(lip, lv);
432 xfs_cil_prepare_item(log, lv, old_lv, diff_len);
437 * Insert the log items into the CIL and calculate the difference in space
438 * consumed by the item. Add the space to the checkpoint ticket and calculate
439 * if the change requires additional log metadata. If it does, take that space
440 * as well. Remove the amount of space we added to the checkpoint ticket from
441 * the current transaction ticket so that the accounting works out correctly.
444 xlog_cil_insert_items(
446 struct xfs_trans *tp,
447 uint32_t released_space)
449 struct xfs_cil *cil = log->l_cilp;
450 struct xfs_cil_ctx *ctx = cil->xc_ctx;
451 struct xfs_log_item *lip;
454 int iovhdr_res = 0, split_res = 0, ctx_res = 0;
459 * We can do this safely because the context can't checkpoint until we
460 * are done so it doesn't matter exactly how we update the CIL.
462 xlog_cil_insert_format_items(log, tp, &len);
464 spin_lock(&cil->xc_cil_lock);
466 /* attach the transaction to the CIL if it has any busy extents */
467 if (!list_empty(&tp->t_busy))
468 list_splice_init(&tp->t_busy, &ctx->busy_extents);
471 * Now transfer enough transaction reservation to the context ticket
472 * for the checkpoint. The context ticket is special - the unit
473 * reservation has to grow as well as the current reservation as we
474 * steal from tickets so we can correctly determine the space used
475 * during the transaction commit.
477 if (ctx->ticket->t_curr_res == 0) {
478 ctx_res = ctx->ticket->t_unit_res;
479 ctx->ticket->t_curr_res = ctx_res;
480 tp->t_ticket->t_curr_res -= ctx_res;
483 /* do we need space for more log record headers? */
484 iclog_space = log->l_iclog_size - log->l_iclog_hsize;
485 if (len > 0 && (ctx->space_used / iclog_space !=
486 (ctx->space_used + len) / iclog_space)) {
487 split_res = (len + iclog_space - 1) / iclog_space;
488 /* need to take into account split region headers, too */
489 split_res *= log->l_iclog_hsize + sizeof(struct xlog_op_header);
490 ctx->ticket->t_unit_res += split_res;
491 ctx->ticket->t_curr_res += split_res;
492 tp->t_ticket->t_curr_res -= split_res;
493 ASSERT(tp->t_ticket->t_curr_res >= len);
495 tp->t_ticket->t_curr_res -= len;
496 tp->t_ticket->t_curr_res += released_space;
497 ctx->space_used += len;
498 ctx->space_used -= released_space;
501 * If we've overrun the reservation, dump the tx details before we move
502 * the log items. Shutdown is imminent...
504 if (WARN_ON(tp->t_ticket->t_curr_res < 0)) {
505 xfs_warn(log->l_mp, "Transaction log reservation overrun:");
507 " log items: %d bytes (iov hdrs: %d bytes)",
509 xfs_warn(log->l_mp, " split region headers: %d bytes",
511 xfs_warn(log->l_mp, " ctx ticket: %d bytes", ctx_res);
512 xlog_print_trans(tp);
516 * Now (re-)position everything modified at the tail of the CIL.
517 * We do this here so we only need to take the CIL lock once during
518 * the transaction commit.
520 list_for_each_entry(lip, &tp->t_items, li_trans) {
522 /* Skip items which aren't dirty in this transaction. */
523 if (!test_bit(XFS_LI_DIRTY, &lip->li_flags))
527 * Only move the item if it isn't already at the tail. This is
528 * to prevent a transient list_empty() state when reinserting
529 * an item that is already the only item in the CIL.
531 if (!list_is_last(&lip->li_cil, &cil->xc_cil))
532 list_move_tail(&lip->li_cil, &cil->xc_cil);
535 spin_unlock(&cil->xc_cil_lock);
537 if (tp->t_ticket->t_curr_res < 0)
538 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
542 xlog_cil_free_logvec(
543 struct xfs_log_vec *log_vector)
545 struct xfs_log_vec *lv;
547 for (lv = log_vector; lv; ) {
548 struct xfs_log_vec *next = lv->lv_next;
555 xlog_discard_endio_work(
556 struct work_struct *work)
558 struct xfs_cil_ctx *ctx =
559 container_of(work, struct xfs_cil_ctx, discard_endio_work);
560 struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
562 xfs_extent_busy_clear(mp, &ctx->busy_extents, false);
567 * Queue up the actual completion to a thread to avoid IRQ-safe locking for
568 * pagb_lock. Note that we need a unbounded workqueue, otherwise we might
569 * get the execution delayed up to 30 seconds for weird reasons.
575 struct xfs_cil_ctx *ctx = bio->bi_private;
577 INIT_WORK(&ctx->discard_endio_work, xlog_discard_endio_work);
578 queue_work(xfs_discard_wq, &ctx->discard_endio_work);
583 xlog_discard_busy_extents(
584 struct xfs_mount *mp,
585 struct xfs_cil_ctx *ctx)
587 struct list_head *list = &ctx->busy_extents;
588 struct xfs_extent_busy *busyp;
589 struct bio *bio = NULL;
590 struct blk_plug plug;
593 ASSERT(xfs_has_discard(mp));
595 blk_start_plug(&plug);
596 list_for_each_entry(busyp, list, list) {
597 trace_xfs_discard_extent(mp, busyp->agno, busyp->bno,
600 error = __blkdev_issue_discard(mp->m_ddev_targp->bt_bdev,
601 XFS_AGB_TO_DADDR(mp, busyp->agno, busyp->bno),
602 XFS_FSB_TO_BB(mp, busyp->length),
604 if (error && error != -EOPNOTSUPP) {
606 "discard failed for extent [0x%llx,%u], error %d",
607 (unsigned long long)busyp->bno,
615 bio->bi_private = ctx;
616 bio->bi_end_io = xlog_discard_endio;
619 xlog_discard_endio_work(&ctx->discard_endio_work);
621 blk_finish_plug(&plug);
625 * Mark all items committed and clear busy extents. We free the log vector
626 * chains in a separate pass so that we unpin the log items as quickly as
631 struct xfs_cil_ctx *ctx)
633 struct xfs_mount *mp = ctx->cil->xc_log->l_mp;
634 bool abort = xlog_is_shutdown(ctx->cil->xc_log);
637 * If the I/O failed, we're aborting the commit and already shutdown.
638 * Wake any commit waiters before aborting the log items so we don't
639 * block async log pushers on callbacks. Async log pushers explicitly do
640 * not wait on log force completion because they may be holding locks
641 * required to unpin items.
644 spin_lock(&ctx->cil->xc_push_lock);
645 wake_up_all(&ctx->cil->xc_start_wait);
646 wake_up_all(&ctx->cil->xc_commit_wait);
647 spin_unlock(&ctx->cil->xc_push_lock);
650 xfs_trans_committed_bulk(ctx->cil->xc_log->l_ailp, ctx->lv_chain,
651 ctx->start_lsn, abort);
653 xfs_extent_busy_sort(&ctx->busy_extents);
654 xfs_extent_busy_clear(mp, &ctx->busy_extents,
655 xfs_has_discard(mp) && !abort);
657 spin_lock(&ctx->cil->xc_push_lock);
658 list_del(&ctx->committing);
659 spin_unlock(&ctx->cil->xc_push_lock);
661 xlog_cil_free_logvec(ctx->lv_chain);
663 if (!list_empty(&ctx->busy_extents))
664 xlog_discard_busy_extents(mp, ctx);
670 xlog_cil_process_committed(
671 struct list_head *list)
673 struct xfs_cil_ctx *ctx;
675 while ((ctx = list_first_entry_or_null(list,
676 struct xfs_cil_ctx, iclog_entry))) {
677 list_del(&ctx->iclog_entry);
678 xlog_cil_committed(ctx);
683 * Record the LSN of the iclog we were just granted space to start writing into.
684 * If the context doesn't have a start_lsn recorded, then this iclog will
685 * contain the start record for the checkpoint. Otherwise this write contains
686 * the commit record for the checkpoint.
689 xlog_cil_set_ctx_write_state(
690 struct xfs_cil_ctx *ctx,
691 struct xlog_in_core *iclog)
693 struct xfs_cil *cil = ctx->cil;
694 xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn);
696 ASSERT(!ctx->commit_lsn);
697 if (!ctx->start_lsn) {
698 spin_lock(&cil->xc_push_lock);
700 * The LSN we need to pass to the log items on transaction
701 * commit is the LSN reported by the first log vector write, not
702 * the commit lsn. If we use the commit record lsn then we can
703 * move the grant write head beyond the tail LSN and overwrite
706 ctx->start_lsn = lsn;
707 wake_up_all(&cil->xc_start_wait);
708 spin_unlock(&cil->xc_push_lock);
711 * Make sure the metadata we are about to overwrite in the log
712 * has been flushed to stable storage before this iclog is
715 spin_lock(&cil->xc_log->l_icloglock);
716 iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
717 spin_unlock(&cil->xc_log->l_icloglock);
722 * Take a reference to the iclog for the context so that we still hold
723 * it when xlog_write is done and has released it. This means the
724 * context controls when the iclog is released for IO.
726 atomic_inc(&iclog->ic_refcnt);
729 * xlog_state_get_iclog_space() guarantees there is enough space in the
730 * iclog for an entire commit record, so we can attach the context
731 * callbacks now. This needs to be done before we make the commit_lsn
732 * visible to waiters so that checkpoints with commit records in the
733 * same iclog order their IO completion callbacks in the same order that
734 * the commit records appear in the iclog.
736 spin_lock(&cil->xc_log->l_icloglock);
737 list_add_tail(&ctx->iclog_entry, &iclog->ic_callbacks);
738 spin_unlock(&cil->xc_log->l_icloglock);
741 * Now we can record the commit LSN and wake anyone waiting for this
742 * sequence to have the ordered commit record assigned to a physical
743 * location in the log.
745 spin_lock(&cil->xc_push_lock);
746 ctx->commit_iclog = iclog;
747 ctx->commit_lsn = lsn;
748 wake_up_all(&cil->xc_commit_wait);
749 spin_unlock(&cil->xc_push_lock);
754 * Ensure that the order of log writes follows checkpoint sequence order. This
755 * relies on the context LSN being zero until the log write has guaranteed the
756 * LSN that the log write will start at via xlog_state_get_iclog_space().
764 xlog_cil_order_write(
767 enum _record_type record)
769 struct xfs_cil_ctx *ctx;
772 spin_lock(&cil->xc_push_lock);
773 list_for_each_entry(ctx, &cil->xc_committing, committing) {
775 * Avoid getting stuck in this loop because we were woken by the
776 * shutdown, but then went back to sleep once already in the
779 if (xlog_is_shutdown(cil->xc_log)) {
780 spin_unlock(&cil->xc_push_lock);
785 * Higher sequences will wait for this one so skip them.
786 * Don't wait for our own sequence, either.
788 if (ctx->sequence >= sequence)
791 /* Wait until the LSN for the record has been recorded. */
794 if (!ctx->start_lsn) {
795 xlog_wait(&cil->xc_start_wait, &cil->xc_push_lock);
800 if (!ctx->commit_lsn) {
801 xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
807 spin_unlock(&cil->xc_push_lock);
812 * Write out the log vector change now attached to the CIL context. This will
813 * write a start record that needs to be strictly ordered in ascending CIL
814 * sequence order so that log recovery will always use in-order start LSNs when
815 * replaying checkpoints.
818 xlog_cil_write_chain(
819 struct xfs_cil_ctx *ctx,
820 struct xfs_log_vec *chain,
823 struct xlog *log = ctx->cil->xc_log;
826 error = xlog_cil_order_write(ctx->cil, ctx->sequence, _START_RECORD);
829 return xlog_write(log, ctx, chain, ctx->ticket, chain_len);
833 * Write out the commit record of a checkpoint transaction to close off a
834 * running log write. These commit records are strictly ordered in ascending CIL
835 * sequence order so that log recovery will always replay the checkpoints in the
839 xlog_cil_write_commit_record(
840 struct xfs_cil_ctx *ctx)
842 struct xlog *log = ctx->cil->xc_log;
843 struct xlog_op_header ophdr = {
844 .oh_clientid = XFS_TRANSACTION,
845 .oh_tid = cpu_to_be32(ctx->ticket->t_tid),
846 .oh_flags = XLOG_COMMIT_TRANS,
848 struct xfs_log_iovec reg = {
850 .i_len = sizeof(struct xlog_op_header),
851 .i_type = XLOG_REG_TYPE_COMMIT,
853 struct xfs_log_vec vec = {
859 if (xlog_is_shutdown(log))
862 error = xlog_cil_order_write(ctx->cil, ctx->sequence, _COMMIT_RECORD);
866 /* account for space used by record data */
867 ctx->ticket->t_curr_res -= reg.i_len;
868 error = xlog_write(log, ctx, &vec, ctx->ticket, reg.i_len);
870 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
874 struct xlog_cil_trans_hdr {
875 struct xlog_op_header oph[2];
876 struct xfs_trans_header thdr;
877 struct xfs_log_iovec lhdr[2];
881 * Build a checkpoint transaction header to begin the journal transaction. We
882 * need to account for the space used by the transaction header here as it is
883 * not accounted for in xlog_write().
885 * This is the only place we write a transaction header, so we also build the
886 * log opheaders that indicate the start of a log transaction and wrap the
887 * transaction header. We keep the start record in it's own log vector rather
888 * than compacting them into a single region as this ends up making the logic
889 * in xlog_write() for handling empty opheaders for start, commit and unmount
890 * records much simpler.
893 xlog_cil_build_trans_hdr(
894 struct xfs_cil_ctx *ctx,
895 struct xlog_cil_trans_hdr *hdr,
896 struct xfs_log_vec *lvhdr,
899 struct xlog_ticket *tic = ctx->ticket;
900 __be32 tid = cpu_to_be32(tic->t_tid);
902 memset(hdr, 0, sizeof(*hdr));
904 /* Log start record */
905 hdr->oph[0].oh_tid = tid;
906 hdr->oph[0].oh_clientid = XFS_TRANSACTION;
907 hdr->oph[0].oh_flags = XLOG_START_TRANS;
909 /* log iovec region pointer */
910 hdr->lhdr[0].i_addr = &hdr->oph[0];
911 hdr->lhdr[0].i_len = sizeof(struct xlog_op_header);
912 hdr->lhdr[0].i_type = XLOG_REG_TYPE_LRHEADER;
915 hdr->oph[1].oh_tid = tid;
916 hdr->oph[1].oh_clientid = XFS_TRANSACTION;
917 hdr->oph[1].oh_len = cpu_to_be32(sizeof(struct xfs_trans_header));
919 /* transaction header in host byte order format */
920 hdr->thdr.th_magic = XFS_TRANS_HEADER_MAGIC;
921 hdr->thdr.th_type = XFS_TRANS_CHECKPOINT;
922 hdr->thdr.th_tid = tic->t_tid;
923 hdr->thdr.th_num_items = num_iovecs;
925 /* log iovec region pointer */
926 hdr->lhdr[1].i_addr = &hdr->oph[1];
927 hdr->lhdr[1].i_len = sizeof(struct xlog_op_header) +
928 sizeof(struct xfs_trans_header);
929 hdr->lhdr[1].i_type = XLOG_REG_TYPE_TRANSHDR;
931 lvhdr->lv_niovecs = 2;
932 lvhdr->lv_iovecp = &hdr->lhdr[0];
933 lvhdr->lv_bytes = hdr->lhdr[0].i_len + hdr->lhdr[1].i_len;
934 lvhdr->lv_next = ctx->lv_chain;
936 tic->t_curr_res -= lvhdr->lv_bytes;
940 * Pull all the log vectors off the items in the CIL, and remove the items from
941 * the CIL. We don't need the CIL lock here because it's only needed on the
942 * transaction commit side which is currently locked out by the flush lock.
944 * If a log item is marked with a whiteout, we do not need to write it to the
945 * journal and so we just move them to the whiteout list for the caller to
946 * dispose of appropriately.
949 xlog_cil_build_lv_chain(
951 struct xfs_cil_ctx *ctx,
952 struct list_head *whiteouts,
953 uint32_t *num_iovecs,
956 struct xfs_log_vec *lv = NULL;
958 while (!list_empty(&cil->xc_cil)) {
959 struct xfs_log_item *item;
961 item = list_first_entry(&cil->xc_cil,
962 struct xfs_log_item, li_cil);
964 if (test_bit(XFS_LI_WHITEOUT, &item->li_flags)) {
965 list_move(&item->li_cil, whiteouts);
966 trace_xfs_cil_whiteout_skip(item);
970 list_del_init(&item->li_cil);
972 ctx->lv_chain = item->li_lv;
974 lv->lv_next = item->li_lv;
977 *num_iovecs += lv->lv_niovecs;
979 /* we don't write ordered log vectors */
980 if (lv->lv_buf_len != XFS_LOG_VEC_ORDERED)
981 *num_bytes += lv->lv_bytes;
986 xlog_cil_cleanup_whiteouts(
987 struct list_head *whiteouts)
989 while (!list_empty(whiteouts)) {
990 struct xfs_log_item *item = list_first_entry(whiteouts,
991 struct xfs_log_item, li_cil);
992 list_del_init(&item->li_cil);
993 trace_xfs_cil_whiteout_unpin(item);
994 item->li_ops->iop_unpin(item, 1);
999 * Push the Committed Item List to the log.
1001 * If the current sequence is the same as xc_push_seq we need to do a flush. If
1002 * xc_push_seq is less than the current sequence, then it has already been
1003 * flushed and we don't need to do anything - the caller will wait for it to
1004 * complete if necessary.
1006 * xc_push_seq is checked unlocked against the sequence number for a match.
1007 * Hence we can allow log forces to run racily and not issue pushes for the
1008 * same sequence twice. If we get a race between multiple pushes for the same
1009 * sequence they will block on the first one and then abort, hence avoiding
1014 struct work_struct *work)
1016 struct xfs_cil_ctx *ctx =
1017 container_of(work, struct xfs_cil_ctx, push_work);
1018 struct xfs_cil *cil = ctx->cil;
1019 struct xlog *log = cil->xc_log;
1020 struct xfs_cil_ctx *new_ctx;
1024 struct xlog_cil_trans_hdr thdr;
1025 struct xfs_log_vec lvhdr = { NULL };
1027 bool push_commit_stable;
1028 LIST_HEAD (whiteouts);
1030 new_ctx = xlog_cil_ctx_alloc();
1031 new_ctx->ticket = xlog_cil_ticket_alloc(log);
1033 down_write(&cil->xc_ctx_lock);
1035 spin_lock(&cil->xc_push_lock);
1036 push_seq = cil->xc_push_seq;
1037 ASSERT(push_seq <= ctx->sequence);
1038 push_commit_stable = cil->xc_push_commit_stable;
1039 cil->xc_push_commit_stable = false;
1042 * As we are about to switch to a new, empty CIL context, we no longer
1043 * need to throttle tasks on CIL space overruns. Wake any waiters that
1044 * the hard push throttle may have caught so they can start committing
1045 * to the new context. The ctx->xc_push_lock provides the serialisation
1046 * necessary for safely using the lockless waitqueue_active() check in
1049 if (waitqueue_active(&cil->xc_push_wait))
1050 wake_up_all(&cil->xc_push_wait);
1053 * Check if we've anything to push. If there is nothing, then we don't
1054 * move on to a new sequence number and so we have to be able to push
1055 * this sequence again later.
1057 if (list_empty(&cil->xc_cil)) {
1058 cil->xc_push_seq = 0;
1059 spin_unlock(&cil->xc_push_lock);
1064 /* check for a previously pushed sequence */
1065 if (push_seq < ctx->sequence) {
1066 spin_unlock(&cil->xc_push_lock);
1071 * We are now going to push this context, so add it to the committing
1072 * list before we do anything else. This ensures that anyone waiting on
1073 * this push can easily detect the difference between a "push in
1074 * progress" and "CIL is empty, nothing to do".
1076 * IOWs, a wait loop can now check for:
1077 * the current sequence not being found on the committing list;
1079 * an unchanged sequence number
1080 * to detect a push that had nothing to do and therefore does not need
1081 * waiting on. If the CIL is not empty, we get put on the committing
1082 * list before emptying the CIL and bumping the sequence number. Hence
1083 * an empty CIL and an unchanged sequence number means we jumped out
1084 * above after doing nothing.
1086 * Hence the waiter will either find the commit sequence on the
1087 * committing list or the sequence number will be unchanged and the CIL
1088 * still dirty. In that latter case, the push has not yet started, and
1089 * so the waiter will have to continue trying to check the CIL
1090 * committing list until it is found. In extreme cases of delay, the
1091 * sequence may fully commit between the attempts the wait makes to wait
1092 * on the commit sequence.
1094 list_add(&ctx->committing, &cil->xc_committing);
1095 spin_unlock(&cil->xc_push_lock);
1097 xlog_cil_build_lv_chain(cil, ctx, &whiteouts, &num_iovecs, &num_bytes);
1100 * Switch the contexts so we can drop the context lock and move out
1101 * of a shared context. We can't just go straight to the commit record,
1102 * though - we need to synchronise with previous and future commits so
1103 * that the commit records are correctly ordered in the log to ensure
1104 * that we process items during log IO completion in the correct order.
1106 * For example, if we get an EFI in one checkpoint and the EFD in the
1107 * next (e.g. due to log forces), we do not want the checkpoint with
1108 * the EFD to be committed before the checkpoint with the EFI. Hence
1109 * we must strictly order the commit records of the checkpoints so
1110 * that: a) the checkpoint callbacks are attached to the iclogs in the
1111 * correct order; and b) the checkpoints are replayed in correct order
1114 * Hence we need to add this context to the committing context list so
1115 * that higher sequences will wait for us to write out a commit record
1118 * xfs_log_force_seq requires us to mirror the new sequence into the cil
1119 * structure atomically with the addition of this sequence to the
1120 * committing list. This also ensures that we can do unlocked checks
1121 * against the current sequence in log forces without risking
1122 * deferencing a freed context pointer.
1124 spin_lock(&cil->xc_push_lock);
1125 xlog_cil_ctx_switch(cil, new_ctx);
1126 spin_unlock(&cil->xc_push_lock);
1127 up_write(&cil->xc_ctx_lock);
1130 * Build a checkpoint transaction header and write it to the log to
1131 * begin the transaction. We need to account for the space used by the
1132 * transaction header here as it is not accounted for in xlog_write().
1134 xlog_cil_build_trans_hdr(ctx, &thdr, &lvhdr, num_iovecs);
1135 num_bytes += lvhdr.lv_bytes;
1137 error = xlog_cil_write_chain(ctx, &lvhdr, num_bytes);
1139 goto out_abort_free_ticket;
1141 error = xlog_cil_write_commit_record(ctx);
1143 goto out_abort_free_ticket;
1145 xfs_log_ticket_ungrant(log, ctx->ticket);
1148 * If the checkpoint spans multiple iclogs, wait for all previous iclogs
1149 * to complete before we submit the commit_iclog. We can't use state
1150 * checks for this - ACTIVE can be either a past completed iclog or a
1151 * future iclog being filled, while WANT_SYNC through SYNC_DONE can be a
1152 * past or future iclog awaiting IO or ordered IO completion to be run.
1153 * In the latter case, if it's a future iclog and we wait on it, the we
1154 * will hang because it won't get processed through to ic_force_wait
1155 * wakeup until this commit_iclog is written to disk. Hence we use the
1156 * iclog header lsn and compare it to the commit lsn to determine if we
1157 * need to wait on iclogs or not.
1159 spin_lock(&log->l_icloglock);
1160 if (ctx->start_lsn != ctx->commit_lsn) {
1163 plsn = be64_to_cpu(ctx->commit_iclog->ic_prev->ic_header.h_lsn);
1164 if (plsn && XFS_LSN_CMP(plsn, ctx->commit_lsn) < 0) {
1166 * Waiting on ic_force_wait orders the completion of
1167 * iclogs older than ic_prev. Hence we only need to wait
1168 * on the most recent older iclog here.
1170 xlog_wait_on_iclog(ctx->commit_iclog->ic_prev);
1171 spin_lock(&log->l_icloglock);
1175 * We need to issue a pre-flush so that the ordering for this
1176 * checkpoint is correctly preserved down to stable storage.
1178 ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FLUSH;
1182 * The commit iclog must be written to stable storage to guarantee
1183 * journal IO vs metadata writeback IO is correctly ordered on stable
1186 * If the push caller needs the commit to be immediately stable and the
1187 * commit_iclog is not yet marked as XLOG_STATE_WANT_SYNC to indicate it
1188 * will be written when released, switch it's state to WANT_SYNC right
1191 ctx->commit_iclog->ic_flags |= XLOG_ICL_NEED_FUA;
1192 if (push_commit_stable &&
1193 ctx->commit_iclog->ic_state == XLOG_STATE_ACTIVE)
1194 xlog_state_switch_iclogs(log, ctx->commit_iclog, 0);
1195 xlog_state_release_iclog(log, ctx->commit_iclog);
1197 /* Not safe to reference ctx now! */
1199 spin_unlock(&log->l_icloglock);
1200 xlog_cil_cleanup_whiteouts(&whiteouts);
1204 up_write(&cil->xc_ctx_lock);
1205 xfs_log_ticket_put(new_ctx->ticket);
1209 out_abort_free_ticket:
1210 xfs_log_ticket_ungrant(log, ctx->ticket);
1211 ASSERT(xlog_is_shutdown(log));
1212 xlog_cil_cleanup_whiteouts(&whiteouts);
1213 if (!ctx->commit_iclog) {
1214 xlog_cil_committed(ctx);
1217 spin_lock(&log->l_icloglock);
1218 xlog_state_release_iclog(log, ctx->commit_iclog);
1219 /* Not safe to reference ctx now! */
1220 spin_unlock(&log->l_icloglock);
1224 * We need to push CIL every so often so we don't cache more than we can fit in
1225 * the log. The limit really is that a checkpoint can't be more than half the
1226 * log (the current checkpoint is not allowed to overwrite the previous
1227 * checkpoint), but commit latency and memory usage limit this to a smaller
1231 xlog_cil_push_background(
1232 struct xlog *log) __releases(cil->xc_ctx_lock)
1234 struct xfs_cil *cil = log->l_cilp;
1237 * The cil won't be empty because we are called while holding the
1238 * context lock so whatever we added to the CIL will still be there
1240 ASSERT(!list_empty(&cil->xc_cil));
1243 * Don't do a background push if we haven't used up all the
1244 * space available yet.
1246 if (cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log)) {
1247 up_read(&cil->xc_ctx_lock);
1251 spin_lock(&cil->xc_push_lock);
1252 if (cil->xc_push_seq < cil->xc_current_sequence) {
1253 cil->xc_push_seq = cil->xc_current_sequence;
1254 queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
1258 * Drop the context lock now, we can't hold that if we need to sleep
1259 * because we are over the blocking threshold. The push_lock is still
1260 * held, so blocking threshold sleep/wakeup is still correctly
1263 up_read(&cil->xc_ctx_lock);
1266 * If we are well over the space limit, throttle the work that is being
1267 * done until the push work on this context has begun. Enforce the hard
1268 * throttle on all transaction commits once it has been activated, even
1269 * if the committing transactions have resulted in the space usage
1270 * dipping back down under the hard limit.
1272 * The ctx->xc_push_lock provides the serialisation necessary for safely
1273 * using the lockless waitqueue_active() check in this context.
1275 if (cil->xc_ctx->space_used >= XLOG_CIL_BLOCKING_SPACE_LIMIT(log) ||
1276 waitqueue_active(&cil->xc_push_wait)) {
1277 trace_xfs_log_cil_wait(log, cil->xc_ctx->ticket);
1278 ASSERT(cil->xc_ctx->space_used < log->l_logsize);
1279 xlog_wait(&cil->xc_push_wait, &cil->xc_push_lock);
1283 spin_unlock(&cil->xc_push_lock);
1288 * xlog_cil_push_now() is used to trigger an immediate CIL push to the sequence
1289 * number that is passed. When it returns, the work will be queued for
1290 * @push_seq, but it won't be completed.
1292 * If the caller is performing a synchronous force, we will flush the workqueue
1293 * to get previously queued work moving to minimise the wait time they will
1294 * undergo waiting for all outstanding pushes to complete. The caller is
1295 * expected to do the required waiting for push_seq to complete.
1297 * If the caller is performing an async push, we need to ensure that the
1298 * checkpoint is fully flushed out of the iclogs when we finish the push. If we
1299 * don't do this, then the commit record may remain sitting in memory in an
1300 * ACTIVE iclog. This then requires another full log force to push to disk,
1301 * which defeats the purpose of having an async, non-blocking CIL force
1302 * mechanism. Hence in this case we need to pass a flag to the push work to
1303 * indicate it needs to flush the commit record itself.
1311 struct xfs_cil *cil = log->l_cilp;
1316 ASSERT(push_seq && push_seq <= cil->xc_current_sequence);
1318 /* start on any pending background push to minimise wait time on it */
1320 flush_workqueue(cil->xc_push_wq);
1322 spin_lock(&cil->xc_push_lock);
1325 * If this is an async flush request, we always need to set the
1326 * xc_push_commit_stable flag even if something else has already queued
1327 * a push. The flush caller is asking for the CIL to be on stable
1328 * storage when the next push completes, so regardless of who has queued
1329 * the push, the flush requires stable semantics from it.
1331 cil->xc_push_commit_stable = async;
1334 * If the CIL is empty or we've already pushed the sequence then
1335 * there's no more work that we need to do.
1337 if (list_empty(&cil->xc_cil) || push_seq <= cil->xc_push_seq) {
1338 spin_unlock(&cil->xc_push_lock);
1342 cil->xc_push_seq = push_seq;
1343 queue_work(cil->xc_push_wq, &cil->xc_ctx->push_work);
1344 spin_unlock(&cil->xc_push_lock);
1351 struct xfs_cil *cil = log->l_cilp;
1354 spin_lock(&cil->xc_push_lock);
1355 if (list_empty(&cil->xc_cil))
1357 spin_unlock(&cil->xc_push_lock);
1362 * If there are intent done items in this transaction and the related intent was
1363 * committed in the current (same) CIL checkpoint, we don't need to write either
1364 * the intent or intent done item to the journal as the change will be
1365 * journalled atomically within this checkpoint. As we cannot remove items from
1366 * the CIL here, mark the related intent with a whiteout so that the CIL push
1367 * can remove it rather than writing it to the journal. Then remove the intent
1368 * done item from the current transaction and release it so it doesn't get put
1369 * into the CIL at all.
1372 xlog_cil_process_intents(
1373 struct xfs_cil *cil,
1374 struct xfs_trans *tp)
1376 struct xfs_log_item *lip, *ilip, *next;
1379 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
1380 if (!(lip->li_ops->flags & XFS_ITEM_INTENT_DONE))
1383 ilip = lip->li_ops->iop_intent(lip);
1384 if (!ilip || !xlog_item_in_current_chkpt(cil, ilip))
1386 set_bit(XFS_LI_WHITEOUT, &ilip->li_flags);
1387 trace_xfs_cil_whiteout_mark(ilip);
1388 len += ilip->li_lv->lv_bytes;
1389 kmem_free(ilip->li_lv);
1392 xfs_trans_del_item(lip);
1393 lip->li_ops->iop_release(lip);
1399 * Commit a transaction with the given vector to the Committed Item List.
1401 * To do this, we need to format the item, pin it in memory if required and
1402 * account for the space used by the transaction. Once we have done that we
1403 * need to release the unused reservation for the transaction, attach the
1404 * transaction to the checkpoint context so we carry the busy extents through
1405 * to checkpoint completion, and then unlock all the items in the transaction.
1407 * Called with the context lock already held in read mode to lock out
1408 * background commit, returns without it held once background commits are
1414 struct xfs_trans *tp,
1415 xfs_csn_t *commit_seq,
1418 struct xfs_cil *cil = log->l_cilp;
1419 struct xfs_log_item *lip, *next;
1420 uint32_t released_space = 0;
1423 * Do all necessary memory allocation before we lock the CIL.
1424 * This ensures the allocation does not deadlock with a CIL
1425 * push in memory reclaim (e.g. from kswapd).
1427 xlog_cil_alloc_shadow_bufs(log, tp);
1429 /* lock out background commit */
1430 down_read(&cil->xc_ctx_lock);
1432 if (tp->t_flags & XFS_TRANS_HAS_INTENT_DONE)
1433 released_space = xlog_cil_process_intents(cil, tp);
1435 xlog_cil_insert_items(log, tp, released_space);
1437 if (regrant && !xlog_is_shutdown(log))
1438 xfs_log_ticket_regrant(log, tp->t_ticket);
1440 xfs_log_ticket_ungrant(log, tp->t_ticket);
1441 tp->t_ticket = NULL;
1442 xfs_trans_unreserve_and_mod_sb(tp);
1445 * Once all the items of the transaction have been copied to the CIL,
1446 * the items can be unlocked and possibly freed.
1448 * This needs to be done before we drop the CIL context lock because we
1449 * have to update state in the log items and unlock them before they go
1450 * to disk. If we don't, then the CIL checkpoint can race with us and
1451 * we can run checkpoint completion before we've updated and unlocked
1452 * the log items. This affects (at least) processing of stale buffers,
1455 trace_xfs_trans_commit_items(tp, _RET_IP_);
1456 list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) {
1457 xfs_trans_del_item(lip);
1458 if (lip->li_ops->iop_committing)
1459 lip->li_ops->iop_committing(lip, cil->xc_ctx->sequence);
1462 *commit_seq = cil->xc_ctx->sequence;
1464 /* xlog_cil_push_background() releases cil->xc_ctx_lock */
1465 xlog_cil_push_background(log);
1469 * Flush the CIL to stable storage but don't wait for it to complete. This
1470 * requires the CIL push to ensure the commit record for the push hits the disk,
1471 * but otherwise is no different to a push done from a log force.
1477 xfs_csn_t seq = log->l_cilp->xc_current_sequence;
1479 trace_xfs_log_force(log->l_mp, seq, _RET_IP_);
1480 xlog_cil_push_now(log, seq, true);
1483 * If the CIL is empty, make sure that any previous checkpoint that may
1484 * still be in an active iclog is pushed to stable storage.
1486 if (list_empty(&log->l_cilp->xc_cil))
1487 xfs_log_force(log->l_mp, 0);
1491 * Conditionally push the CIL based on the sequence passed in.
1493 * We only need to push if we haven't already pushed the sequence number given.
1494 * Hence the only time we will trigger a push here is if the push sequence is
1495 * the same as the current context.
1497 * We return the current commit lsn to allow the callers to determine if a
1498 * iclog flush is necessary following this call.
1505 struct xfs_cil *cil = log->l_cilp;
1506 struct xfs_cil_ctx *ctx;
1507 xfs_lsn_t commit_lsn = NULLCOMMITLSN;
1509 ASSERT(sequence <= cil->xc_current_sequence);
1512 sequence = cil->xc_current_sequence;
1513 trace_xfs_log_force(log->l_mp, sequence, _RET_IP_);
1516 * check to see if we need to force out the current context.
1517 * xlog_cil_push() handles racing pushes for the same sequence,
1518 * so no need to deal with it here.
1521 xlog_cil_push_now(log, sequence, false);
1524 * See if we can find a previous sequence still committing.
1525 * We need to wait for all previous sequence commits to complete
1526 * before allowing the force of push_seq to go ahead. Hence block
1527 * on commits for those as well.
1529 spin_lock(&cil->xc_push_lock);
1530 list_for_each_entry(ctx, &cil->xc_committing, committing) {
1532 * Avoid getting stuck in this loop because we were woken by the
1533 * shutdown, but then went back to sleep once already in the
1536 if (xlog_is_shutdown(log))
1538 if (ctx->sequence > sequence)
1540 if (!ctx->commit_lsn) {
1542 * It is still being pushed! Wait for the push to
1543 * complete, then start again from the beginning.
1545 XFS_STATS_INC(log->l_mp, xs_log_force_sleep);
1546 xlog_wait(&cil->xc_commit_wait, &cil->xc_push_lock);
1549 if (ctx->sequence != sequence)
1552 commit_lsn = ctx->commit_lsn;
1556 * The call to xlog_cil_push_now() executes the push in the background.
1557 * Hence by the time we have got here it our sequence may not have been
1558 * pushed yet. This is true if the current sequence still matches the
1559 * push sequence after the above wait loop and the CIL still contains
1560 * dirty objects. This is guaranteed by the push code first adding the
1561 * context to the committing list before emptying the CIL.
1563 * Hence if we don't find the context in the committing list and the
1564 * current sequence number is unchanged then the CIL contents are
1565 * significant. If the CIL is empty, if means there was nothing to push
1566 * and that means there is nothing to wait for. If the CIL is not empty,
1567 * it means we haven't yet started the push, because if it had started
1568 * we would have found the context on the committing list.
1570 if (sequence == cil->xc_current_sequence &&
1571 !list_empty(&cil->xc_cil)) {
1572 spin_unlock(&cil->xc_push_lock);
1576 spin_unlock(&cil->xc_push_lock);
1580 * We detected a shutdown in progress. We need to trigger the log force
1581 * to pass through it's iclog state machine error handling, even though
1582 * we are already in a shutdown state. Hence we can't return
1583 * NULLCOMMITLSN here as that has special meaning to log forces (i.e.
1584 * LSN is already stable), so we return a zero LSN instead.
1587 spin_unlock(&cil->xc_push_lock);
1592 * Perform initial CIL structure initialisation.
1598 struct xfs_cil *cil;
1599 struct xfs_cil_ctx *ctx;
1601 cil = kmem_zalloc(sizeof(*cil), KM_MAYFAIL);
1605 * Limit the CIL pipeline depth to 4 concurrent works to bound the
1606 * concurrency the log spinlocks will be exposed to.
1608 cil->xc_push_wq = alloc_workqueue("xfs-cil/%s",
1609 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_UNBOUND),
1610 4, log->l_mp->m_super->s_id);
1611 if (!cil->xc_push_wq)
1612 goto out_destroy_cil;
1614 INIT_LIST_HEAD(&cil->xc_cil);
1615 INIT_LIST_HEAD(&cil->xc_committing);
1616 spin_lock_init(&cil->xc_cil_lock);
1617 spin_lock_init(&cil->xc_push_lock);
1618 init_waitqueue_head(&cil->xc_push_wait);
1619 init_rwsem(&cil->xc_ctx_lock);
1620 init_waitqueue_head(&cil->xc_start_wait);
1621 init_waitqueue_head(&cil->xc_commit_wait);
1625 ctx = xlog_cil_ctx_alloc();
1626 xlog_cil_ctx_switch(cil, ctx);
1639 if (log->l_cilp->xc_ctx) {
1640 if (log->l_cilp->xc_ctx->ticket)
1641 xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket);
1642 kmem_free(log->l_cilp->xc_ctx);
1645 ASSERT(list_empty(&log->l_cilp->xc_cil));
1646 destroy_workqueue(log->l_cilp->xc_push_wq);
1647 kmem_free(log->l_cilp);