1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_extfree_item.h"
19 #include "xfs_btree.h"
21 #include "xfs_alloc.h"
23 #include "xfs_trace.h"
24 #include "xfs_error.h"
26 kmem_zone_t *xfs_efi_zone;
27 kmem_zone_t *xfs_efd_zone;
29 static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
31 return container_of(lip, struct xfs_efi_log_item, efi_item);
36 struct xfs_efi_log_item *efip)
38 kmem_free(efip->efi_item.li_lv_shadow);
39 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
42 kmem_zone_free(xfs_efi_zone, efip);
46 * Freeing the efi requires that we remove it from the AIL if it has already
47 * been placed there. However, the EFI may not yet have been placed in the AIL
48 * when called by xfs_efi_release() from EFD processing due to the ordering of
49 * committed vs unpin operations in bulk insert operations. Hence the reference
50 * count to ensure only the last caller frees the EFI.
54 struct xfs_efi_log_item *efip)
56 ASSERT(atomic_read(&efip->efi_refcount) > 0);
57 if (atomic_dec_and_test(&efip->efi_refcount)) {
58 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
59 xfs_efi_item_free(efip);
64 * This returns the number of iovecs needed to log the given efi item.
65 * We only need 1 iovec for an efi item. It just logs the efi_log_format
70 struct xfs_efi_log_item *efip)
72 return sizeof(struct xfs_efi_log_format) +
73 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
78 struct xfs_log_item *lip,
83 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
87 * This is called to fill in the vector of log iovecs for the
88 * given efi log item. We use only 1 iovec, and we point that
89 * at the efi_log_format structure embedded in the efi item.
90 * It is at this point that we assert that all of the extent
91 * slots in the efi item have been filled.
95 struct xfs_log_item *lip,
96 struct xfs_log_vec *lv)
98 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
99 struct xfs_log_iovec *vecp = NULL;
101 ASSERT(atomic_read(&efip->efi_next_extent) ==
102 efip->efi_format.efi_nextents);
104 efip->efi_format.efi_type = XFS_LI_EFI;
105 efip->efi_format.efi_size = 1;
107 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
109 xfs_efi_item_sizeof(efip));
114 * The unpin operation is the last place an EFI is manipulated in the log. It is
115 * either inserted in the AIL or aborted in the event of a log I/O error. In
116 * either case, the EFI transaction has been successfully committed to make it
117 * this far. Therefore, we expect whoever committed the EFI to either construct
118 * and commit the EFD or drop the EFD's reference in the event of error. Simply
119 * drop the log's EFI reference now that the log is done with it.
123 struct xfs_log_item *lip,
126 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
127 xfs_efi_release(efip);
131 * The EFI has been either committed or aborted if the transaction has been
132 * cancelled. If the transaction was cancelled, an EFD isn't going to be
133 * constructed and thus we free the EFI here directly.
136 xfs_efi_item_release(
137 struct xfs_log_item *lip)
139 xfs_efi_release(EFI_ITEM(lip));
143 * Copy an EFI format buffer from the given buf, and into the destination
144 * EFI format structure.
145 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
146 * one of which will be the native format for this kernel.
147 * It will handle the conversion of formats if necessary.
150 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
152 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
154 uint len = sizeof(xfs_efi_log_format_t) +
155 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
156 uint len32 = sizeof(xfs_efi_log_format_32_t) +
157 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
158 uint len64 = sizeof(xfs_efi_log_format_64_t) +
159 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
161 if (buf->i_len == len) {
162 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
164 } else if (buf->i_len == len32) {
165 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
167 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
168 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
169 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
170 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
171 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
172 dst_efi_fmt->efi_extents[i].ext_start =
173 src_efi_fmt_32->efi_extents[i].ext_start;
174 dst_efi_fmt->efi_extents[i].ext_len =
175 src_efi_fmt_32->efi_extents[i].ext_len;
178 } else if (buf->i_len == len64) {
179 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
181 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
182 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
183 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
184 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
185 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
186 dst_efi_fmt->efi_extents[i].ext_start =
187 src_efi_fmt_64->efi_extents[i].ext_start;
188 dst_efi_fmt->efi_extents[i].ext_len =
189 src_efi_fmt_64->efi_extents[i].ext_len;
193 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
194 return -EFSCORRUPTED;
197 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
199 return container_of(lip, struct xfs_efd_log_item, efd_item);
203 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
205 kmem_free(efdp->efd_item.li_lv_shadow);
206 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
209 kmem_zone_free(xfs_efd_zone, efdp);
213 * This returns the number of iovecs needed to log the given efd item.
214 * We only need 1 iovec for an efd item. It just logs the efd_log_format
219 struct xfs_efd_log_item *efdp)
221 return sizeof(xfs_efd_log_format_t) +
222 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
227 struct xfs_log_item *lip,
232 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
236 * This is called to fill in the vector of log iovecs for the
237 * given efd log item. We use only 1 iovec, and we point that
238 * at the efd_log_format structure embedded in the efd item.
239 * It is at this point that we assert that all of the extent
240 * slots in the efd item have been filled.
244 struct xfs_log_item *lip,
245 struct xfs_log_vec *lv)
247 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
248 struct xfs_log_iovec *vecp = NULL;
250 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
252 efdp->efd_format.efd_type = XFS_LI_EFD;
253 efdp->efd_format.efd_size = 1;
255 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
257 xfs_efd_item_sizeof(efdp));
261 * The EFD is either committed or aborted if the transaction is cancelled. If
262 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
265 xfs_efd_item_release(
266 struct xfs_log_item *lip)
268 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
270 xfs_efi_release(efdp->efd_efip);
271 xfs_efd_item_free(efdp);
274 static const struct xfs_item_ops xfs_efd_item_ops = {
275 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
276 .iop_size = xfs_efd_item_size,
277 .iop_format = xfs_efd_item_format,
278 .iop_release = xfs_efd_item_release,
282 * Allocate an "extent free done" log item that will hold nextents worth of
283 * extents. The caller must use all nextents extents, because we are not
284 * flexible about this at all.
286 static struct xfs_efd_log_item *
288 struct xfs_trans *tp,
289 struct xfs_efi_log_item *efip,
290 unsigned int nextents)
292 struct xfs_efd_log_item *efdp;
294 ASSERT(nextents > 0);
296 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
297 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
298 (nextents - 1) * sizeof(struct xfs_extent),
301 efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
304 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
306 efdp->efd_efip = efip;
307 efdp->efd_format.efd_nextents = nextents;
308 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
310 xfs_trans_add_item(tp, &efdp->efd_item);
315 * Free an extent and log it to the EFD. Note that the transaction is marked
316 * dirty regardless of whether the extent free succeeds or fails to support the
317 * EFI/EFD lifecycle rules.
320 xfs_trans_free_extent(
321 struct xfs_trans *tp,
322 struct xfs_efd_log_item *efdp,
323 xfs_fsblock_t start_block,
324 xfs_extlen_t ext_len,
325 const struct xfs_owner_info *oinfo,
328 struct xfs_mount *mp = tp->t_mountp;
329 struct xfs_extent *extp;
331 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
332 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
336 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
338 error = __xfs_free_extent(tp, start_block, ext_len,
339 oinfo, XFS_AG_RESV_NONE, skip_discard);
341 * Mark the transaction dirty, even on error. This ensures the
342 * transaction is aborted, which:
344 * 1.) releases the EFI and frees the EFD
345 * 2.) shuts down the filesystem
347 tp->t_flags |= XFS_TRANS_DIRTY;
348 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
350 next_extent = efdp->efd_next_extent;
351 ASSERT(next_extent < efdp->efd_format.efd_nextents);
352 extp = &(efdp->efd_format.efd_extents[next_extent]);
353 extp->ext_start = start_block;
354 extp->ext_len = ext_len;
355 efdp->efd_next_extent++;
360 /* Sort bmap items by AG. */
362 xfs_extent_free_diff_items(
367 struct xfs_mount *mp = priv;
368 struct xfs_extent_free_item *ra;
369 struct xfs_extent_free_item *rb;
371 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
372 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
373 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
374 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
377 /* Log a free extent to the intent item. */
379 xfs_extent_free_log_item(
380 struct xfs_trans *tp,
381 struct xfs_efi_log_item *efip,
382 struct xfs_extent_free_item *free)
385 struct xfs_extent *extp;
387 tp->t_flags |= XFS_TRANS_DIRTY;
388 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
391 * atomic_inc_return gives us the value after the increment;
392 * we want to use it as an array index so we need to subtract 1 from
395 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
396 ASSERT(next_extent < efip->efi_format.efi_nextents);
397 extp = &efip->efi_format.efi_extents[next_extent];
398 extp->ext_start = free->xefi_startblock;
399 extp->ext_len = free->xefi_blockcount;
402 static struct xfs_log_item *
403 xfs_extent_free_create_intent(
404 struct xfs_trans *tp,
405 struct list_head *items,
409 struct xfs_mount *mp = tp->t_mountp;
410 struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
411 struct xfs_extent_free_item *free;
415 xfs_trans_add_item(tp, &efip->efi_item);
417 list_sort(mp, items, xfs_extent_free_diff_items);
418 list_for_each_entry(free, items, xefi_list)
419 xfs_extent_free_log_item(tp, efip, free);
420 return &efip->efi_item;
423 /* Get an EFD so we can process all the free extents. */
425 xfs_extent_free_create_done(
426 struct xfs_trans *tp,
427 struct xfs_log_item *intent,
430 return xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
433 /* Process a free extent. */
435 xfs_extent_free_finish_item(
436 struct xfs_trans *tp,
437 struct list_head *item,
441 struct xfs_extent_free_item *free;
444 free = container_of(item, struct xfs_extent_free_item, xefi_list);
445 error = xfs_trans_free_extent(tp, done_item,
446 free->xefi_startblock,
447 free->xefi_blockcount,
448 &free->xefi_oinfo, free->xefi_skip_discard);
453 /* Abort all pending EFIs. */
455 xfs_extent_free_abort_intent(
456 struct xfs_log_item *intent)
458 xfs_efi_release(EFI_ITEM(intent));
461 /* Cancel a free extent. */
463 xfs_extent_free_cancel_item(
464 struct list_head *item)
466 struct xfs_extent_free_item *free;
468 free = container_of(item, struct xfs_extent_free_item, xefi_list);
472 const struct xfs_defer_op_type xfs_extent_free_defer_type = {
473 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
474 .create_intent = xfs_extent_free_create_intent,
475 .abort_intent = xfs_extent_free_abort_intent,
476 .create_done = xfs_extent_free_create_done,
477 .finish_item = xfs_extent_free_finish_item,
478 .cancel_item = xfs_extent_free_cancel_item,
482 * AGFL blocks are accounted differently in the reserve pools and are not
483 * inserted into the busy extent list.
486 xfs_agfl_free_finish_item(
487 struct xfs_trans *tp,
488 struct list_head *item,
492 struct xfs_mount *mp = tp->t_mountp;
493 struct xfs_efd_log_item *efdp = done_item;
494 struct xfs_extent_free_item *free;
495 struct xfs_extent *extp;
496 struct xfs_buf *agbp;
502 free = container_of(item, struct xfs_extent_free_item, xefi_list);
503 ASSERT(free->xefi_blockcount == 1);
504 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
505 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
507 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
509 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
511 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
515 * Mark the transaction dirty, even on error. This ensures the
516 * transaction is aborted, which:
518 * 1.) releases the EFI and frees the EFD
519 * 2.) shuts down the filesystem
521 tp->t_flags |= XFS_TRANS_DIRTY;
522 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
524 next_extent = efdp->efd_next_extent;
525 ASSERT(next_extent < efdp->efd_format.efd_nextents);
526 extp = &(efdp->efd_format.efd_extents[next_extent]);
527 extp->ext_start = free->xefi_startblock;
528 extp->ext_len = free->xefi_blockcount;
529 efdp->efd_next_extent++;
535 /* sub-type with special handling for AGFL deferred frees */
536 const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
537 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
538 .create_intent = xfs_extent_free_create_intent,
539 .abort_intent = xfs_extent_free_abort_intent,
540 .create_done = xfs_extent_free_create_done,
541 .finish_item = xfs_agfl_free_finish_item,
542 .cancel_item = xfs_extent_free_cancel_item,
546 * Process an extent free intent item that was recovered from
547 * the log. We need to free the extents that it describes.
551 struct xfs_efi_log_item *efip,
552 struct list_head *capture_list)
554 struct xfs_mount *mp = efip->efi_item.li_mountp;
555 struct xfs_efd_log_item *efdp;
556 struct xfs_trans *tp;
560 xfs_fsblock_t startblock_fsb;
562 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
565 * First check the validity of the extents described by the
566 * EFI. If any are bad, then assume that all are bad and
569 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
570 extp = &efip->efi_format.efi_extents[i];
571 startblock_fsb = XFS_BB_TO_FSB(mp,
572 XFS_FSB_TO_DADDR(mp, extp->ext_start));
573 if (startblock_fsb == 0 ||
574 extp->ext_len == 0 ||
575 startblock_fsb >= mp->m_sb.sb_dblocks ||
576 extp->ext_len >= mp->m_sb.sb_agblocks) {
578 * This will pull the EFI from the AIL and
579 * free the memory associated with it.
581 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
582 xfs_efi_release(efip);
583 return -EFSCORRUPTED;
587 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
590 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
592 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
593 extp = &efip->efi_format.efi_extents[i];
594 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
596 &XFS_RMAP_OINFO_ANY_OWNER, false);
602 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
604 return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
607 xfs_trans_cancel(tp);
611 /* Relog an intent item to push the log tail forward. */
612 static struct xfs_log_item *
614 struct xfs_log_item *intent,
615 struct xfs_trans *tp)
617 struct xfs_efd_log_item *efdp;
618 struct xfs_efi_log_item *efip;
619 struct xfs_extent *extp;
622 count = EFI_ITEM(intent)->efi_format.efi_nextents;
623 extp = EFI_ITEM(intent)->efi_format.efi_extents;
625 tp->t_flags |= XFS_TRANS_DIRTY;
626 efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
627 efdp->efd_next_extent = count;
628 memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
629 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
631 efip = xfs_efi_init(tp->t_mountp, count);
632 memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
633 atomic_set(&efip->efi_next_extent, count);
634 xfs_trans_add_item(tp, &efip->efi_item);
635 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
636 return &efip->efi_item;
639 static const struct xfs_item_ops xfs_efi_item_ops = {
640 .iop_size = xfs_efi_item_size,
641 .iop_format = xfs_efi_item_format,
642 .iop_unpin = xfs_efi_item_unpin,
643 .iop_release = xfs_efi_item_release,
644 .iop_relog = xfs_efi_item_relog,
648 * Allocate and initialize an efi item with the given number of extents.
650 struct xfs_efi_log_item *
652 struct xfs_mount *mp,
656 struct xfs_efi_log_item *efip;
659 ASSERT(nextents > 0);
660 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
661 size = (uint)(sizeof(struct xfs_efi_log_item) +
662 ((nextents - 1) * sizeof(xfs_extent_t)));
663 efip = kmem_zalloc(size, 0);
665 efip = kmem_zone_zalloc(xfs_efi_zone, 0);
668 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
669 efip->efi_format.efi_nextents = nextents;
670 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
671 atomic_set(&efip->efi_next_extent, 0);
672 atomic_set(&efip->efi_refcount, 2);