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"
25 #include "xfs_log_recover.h"
27 kmem_zone_t *xfs_efi_zone;
28 kmem_zone_t *xfs_efd_zone;
30 static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
32 return container_of(lip, struct xfs_efi_log_item, efi_item);
37 struct xfs_efi_log_item *efip)
39 kmem_free(efip->efi_item.li_lv_shadow);
40 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
43 kmem_cache_free(xfs_efi_zone, efip);
47 * Freeing the efi requires that we remove it from the AIL if it has already
48 * been placed there. However, the EFI may not yet have been placed in the AIL
49 * when called by xfs_efi_release() from EFD processing due to the ordering of
50 * committed vs unpin operations in bulk insert operations. Hence the reference
51 * count to ensure only the last caller frees the EFI.
55 struct xfs_efi_log_item *efip)
57 ASSERT(atomic_read(&efip->efi_refcount) > 0);
58 if (atomic_dec_and_test(&efip->efi_refcount)) {
59 xfs_trans_ail_delete(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
60 xfs_efi_item_free(efip);
65 * This returns the number of iovecs needed to log the given efi item.
66 * We only need 1 iovec for an efi item. It just logs the efi_log_format
71 struct xfs_efi_log_item *efip)
73 return sizeof(struct xfs_efi_log_format) +
74 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
79 struct xfs_log_item *lip,
84 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
88 * This is called to fill in the vector of log iovecs for the
89 * given efi log item. We use only 1 iovec, and we point that
90 * at the efi_log_format structure embedded in the efi item.
91 * It is at this point that we assert that all of the extent
92 * slots in the efi item have been filled.
96 struct xfs_log_item *lip,
97 struct xfs_log_vec *lv)
99 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
100 struct xfs_log_iovec *vecp = NULL;
102 ASSERT(atomic_read(&efip->efi_next_extent) ==
103 efip->efi_format.efi_nextents);
105 efip->efi_format.efi_type = XFS_LI_EFI;
106 efip->efi_format.efi_size = 1;
108 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
110 xfs_efi_item_sizeof(efip));
115 * The unpin operation is the last place an EFI is manipulated in the log. It is
116 * either inserted in the AIL or aborted in the event of a log I/O error. In
117 * either case, the EFI transaction has been successfully committed to make it
118 * this far. Therefore, we expect whoever committed the EFI to either construct
119 * and commit the EFD or drop the EFD's reference in the event of error. Simply
120 * drop the log's EFI reference now that the log is done with it.
124 struct xfs_log_item *lip,
127 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
128 xfs_efi_release(efip);
132 * The EFI has been either committed or aborted if the transaction has been
133 * cancelled. If the transaction was cancelled, an EFD isn't going to be
134 * constructed and thus we free the EFI here directly.
137 xfs_efi_item_release(
138 struct xfs_log_item *lip)
140 xfs_efi_release(EFI_ITEM(lip));
143 static const struct xfs_item_ops xfs_efi_item_ops = {
144 .iop_size = xfs_efi_item_size,
145 .iop_format = xfs_efi_item_format,
146 .iop_unpin = xfs_efi_item_unpin,
147 .iop_release = xfs_efi_item_release,
152 * Allocate and initialize an efi item with the given number of extents.
154 struct xfs_efi_log_item *
156 struct xfs_mount *mp,
160 struct xfs_efi_log_item *efip;
163 ASSERT(nextents > 0);
164 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
165 size = (uint)(sizeof(struct xfs_efi_log_item) +
166 ((nextents - 1) * sizeof(xfs_extent_t)));
167 efip = kmem_zalloc(size, 0);
169 efip = kmem_zone_zalloc(xfs_efi_zone, 0);
172 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
173 efip->efi_format.efi_nextents = nextents;
174 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
175 atomic_set(&efip->efi_next_extent, 0);
176 atomic_set(&efip->efi_refcount, 2);
182 * Copy an EFI format buffer from the given buf, and into the destination
183 * EFI format structure.
184 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
185 * one of which will be the native format for this kernel.
186 * It will handle the conversion of formats if necessary.
189 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
191 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
193 uint len = sizeof(xfs_efi_log_format_t) +
194 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
195 uint len32 = sizeof(xfs_efi_log_format_32_t) +
196 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
197 uint len64 = sizeof(xfs_efi_log_format_64_t) +
198 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
200 if (buf->i_len == len) {
201 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
203 } else if (buf->i_len == len32) {
204 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
206 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
207 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
208 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
209 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
210 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
211 dst_efi_fmt->efi_extents[i].ext_start =
212 src_efi_fmt_32->efi_extents[i].ext_start;
213 dst_efi_fmt->efi_extents[i].ext_len =
214 src_efi_fmt_32->efi_extents[i].ext_len;
217 } else if (buf->i_len == len64) {
218 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
220 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
221 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
222 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
223 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
224 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
225 dst_efi_fmt->efi_extents[i].ext_start =
226 src_efi_fmt_64->efi_extents[i].ext_start;
227 dst_efi_fmt->efi_extents[i].ext_len =
228 src_efi_fmt_64->efi_extents[i].ext_len;
232 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
233 return -EFSCORRUPTED;
236 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
238 return container_of(lip, struct xfs_efd_log_item, efd_item);
242 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
244 kmem_free(efdp->efd_item.li_lv_shadow);
245 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
248 kmem_cache_free(xfs_efd_zone, efdp);
252 * This returns the number of iovecs needed to log the given efd item.
253 * We only need 1 iovec for an efd item. It just logs the efd_log_format
258 struct xfs_efd_log_item *efdp)
260 return sizeof(xfs_efd_log_format_t) +
261 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
266 struct xfs_log_item *lip,
271 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
275 * This is called to fill in the vector of log iovecs for the
276 * given efd log item. We use only 1 iovec, and we point that
277 * at the efd_log_format structure embedded in the efd item.
278 * It is at this point that we assert that all of the extent
279 * slots in the efd item have been filled.
283 struct xfs_log_item *lip,
284 struct xfs_log_vec *lv)
286 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
287 struct xfs_log_iovec *vecp = NULL;
289 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
291 efdp->efd_format.efd_type = XFS_LI_EFD;
292 efdp->efd_format.efd_size = 1;
294 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
296 xfs_efd_item_sizeof(efdp));
300 * The EFD is either committed or aborted if the transaction is cancelled. If
301 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
304 xfs_efd_item_release(
305 struct xfs_log_item *lip)
307 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
309 xfs_efi_release(efdp->efd_efip);
310 xfs_efd_item_free(efdp);
313 static const struct xfs_item_ops xfs_efd_item_ops = {
314 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
315 .iop_size = xfs_efd_item_size,
316 .iop_format = xfs_efd_item_format,
317 .iop_release = xfs_efd_item_release,
321 * Allocate an "extent free done" log item that will hold nextents worth of
322 * extents. The caller must use all nextents extents, because we are not
323 * flexible about this at all.
325 static struct xfs_efd_log_item *
327 struct xfs_trans *tp,
328 struct xfs_efi_log_item *efip,
329 unsigned int nextents)
331 struct xfs_efd_log_item *efdp;
333 ASSERT(nextents > 0);
335 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
336 efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
337 (nextents - 1) * sizeof(struct xfs_extent),
340 efdp = kmem_zone_zalloc(xfs_efd_zone, 0);
343 xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
345 efdp->efd_efip = efip;
346 efdp->efd_format.efd_nextents = nextents;
347 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
349 xfs_trans_add_item(tp, &efdp->efd_item);
354 * Free an extent and log it to the EFD. Note that the transaction is marked
355 * dirty regardless of whether the extent free succeeds or fails to support the
356 * EFI/EFD lifecycle rules.
359 xfs_trans_free_extent(
360 struct xfs_trans *tp,
361 struct xfs_efd_log_item *efdp,
362 xfs_fsblock_t start_block,
363 xfs_extlen_t ext_len,
364 const struct xfs_owner_info *oinfo,
367 struct xfs_mount *mp = tp->t_mountp;
368 struct xfs_extent *extp;
370 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block);
371 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp,
375 trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
377 error = __xfs_free_extent(tp, start_block, ext_len,
378 oinfo, XFS_AG_RESV_NONE, skip_discard);
380 * Mark the transaction dirty, even on error. This ensures the
381 * transaction is aborted, which:
383 * 1.) releases the EFI and frees the EFD
384 * 2.) shuts down the filesystem
386 tp->t_flags |= XFS_TRANS_DIRTY;
387 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
389 next_extent = efdp->efd_next_extent;
390 ASSERT(next_extent < efdp->efd_format.efd_nextents);
391 extp = &(efdp->efd_format.efd_extents[next_extent]);
392 extp->ext_start = start_block;
393 extp->ext_len = ext_len;
394 efdp->efd_next_extent++;
399 /* Sort bmap items by AG. */
401 xfs_extent_free_diff_items(
406 struct xfs_mount *mp = priv;
407 struct xfs_extent_free_item *ra;
408 struct xfs_extent_free_item *rb;
410 ra = container_of(a, struct xfs_extent_free_item, xefi_list);
411 rb = container_of(b, struct xfs_extent_free_item, xefi_list);
412 return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
413 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
416 /* Log a free extent to the intent item. */
418 xfs_extent_free_log_item(
419 struct xfs_trans *tp,
420 struct xfs_efi_log_item *efip,
421 struct xfs_extent_free_item *free)
424 struct xfs_extent *extp;
426 tp->t_flags |= XFS_TRANS_DIRTY;
427 set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
430 * atomic_inc_return gives us the value after the increment;
431 * we want to use it as an array index so we need to subtract 1 from
434 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
435 ASSERT(next_extent < efip->efi_format.efi_nextents);
436 extp = &efip->efi_format.efi_extents[next_extent];
437 extp->ext_start = free->xefi_startblock;
438 extp->ext_len = free->xefi_blockcount;
441 static struct xfs_log_item *
442 xfs_extent_free_create_intent(
443 struct xfs_trans *tp,
444 struct list_head *items,
448 struct xfs_mount *mp = tp->t_mountp;
449 struct xfs_efi_log_item *efip = xfs_efi_init(mp, count);
450 struct xfs_extent_free_item *free;
454 xfs_trans_add_item(tp, &efip->efi_item);
456 list_sort(mp, items, xfs_extent_free_diff_items);
457 list_for_each_entry(free, items, xefi_list)
458 xfs_extent_free_log_item(tp, efip, free);
459 return &efip->efi_item;
462 /* Get an EFD so we can process all the free extents. */
463 static struct xfs_log_item *
464 xfs_extent_free_create_done(
465 struct xfs_trans *tp,
466 struct xfs_log_item *intent,
469 return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
472 /* Process a free extent. */
474 xfs_extent_free_finish_item(
475 struct xfs_trans *tp,
476 struct xfs_log_item *done,
477 struct list_head *item,
478 struct xfs_btree_cur **state)
480 struct xfs_extent_free_item *free;
483 free = container_of(item, struct xfs_extent_free_item, xefi_list);
484 error = xfs_trans_free_extent(tp, EFD_ITEM(done),
485 free->xefi_startblock,
486 free->xefi_blockcount,
487 &free->xefi_oinfo, free->xefi_skip_discard);
492 /* Abort all pending EFIs. */
494 xfs_extent_free_abort_intent(
495 struct xfs_log_item *intent)
497 xfs_efi_release(EFI_ITEM(intent));
500 /* Cancel a free extent. */
502 xfs_extent_free_cancel_item(
503 struct list_head *item)
505 struct xfs_extent_free_item *free;
507 free = container_of(item, struct xfs_extent_free_item, xefi_list);
511 const struct xfs_defer_op_type xfs_extent_free_defer_type = {
512 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
513 .create_intent = xfs_extent_free_create_intent,
514 .abort_intent = xfs_extent_free_abort_intent,
515 .create_done = xfs_extent_free_create_done,
516 .finish_item = xfs_extent_free_finish_item,
517 .cancel_item = xfs_extent_free_cancel_item,
521 * AGFL blocks are accounted differently in the reserve pools and are not
522 * inserted into the busy extent list.
525 xfs_agfl_free_finish_item(
526 struct xfs_trans *tp,
527 struct xfs_log_item *done,
528 struct list_head *item,
529 struct xfs_btree_cur **state)
531 struct xfs_mount *mp = tp->t_mountp;
532 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
533 struct xfs_extent_free_item *free;
534 struct xfs_extent *extp;
535 struct xfs_buf *agbp;
541 free = container_of(item, struct xfs_extent_free_item, xefi_list);
542 ASSERT(free->xefi_blockcount == 1);
543 agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
544 agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
546 trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
548 error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
550 error = xfs_free_agfl_block(tp, agno, agbno, agbp,
554 * Mark the transaction dirty, even on error. This ensures the
555 * transaction is aborted, which:
557 * 1.) releases the EFI and frees the EFD
558 * 2.) shuts down the filesystem
560 tp->t_flags |= XFS_TRANS_DIRTY;
561 set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
563 next_extent = efdp->efd_next_extent;
564 ASSERT(next_extent < efdp->efd_format.efd_nextents);
565 extp = &(efdp->efd_format.efd_extents[next_extent]);
566 extp->ext_start = free->xefi_startblock;
567 extp->ext_len = free->xefi_blockcount;
568 efdp->efd_next_extent++;
574 /* sub-type with special handling for AGFL deferred frees */
575 const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
576 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
577 .create_intent = xfs_extent_free_create_intent,
578 .abort_intent = xfs_extent_free_abort_intent,
579 .create_done = xfs_extent_free_create_done,
580 .finish_item = xfs_agfl_free_finish_item,
581 .cancel_item = xfs_extent_free_cancel_item,
585 * Process an extent free intent item that was recovered from
586 * the log. We need to free the extents that it describes.
590 struct xfs_mount *mp,
591 struct xfs_efi_log_item *efip)
593 struct xfs_efd_log_item *efdp;
594 struct xfs_trans *tp;
598 xfs_fsblock_t startblock_fsb;
600 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
603 * First check the validity of the extents described by the
604 * EFI. If any are bad, then assume that all are bad and
607 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
608 extp = &efip->efi_format.efi_extents[i];
609 startblock_fsb = XFS_BB_TO_FSB(mp,
610 XFS_FSB_TO_DADDR(mp, extp->ext_start));
611 if (startblock_fsb == 0 ||
612 extp->ext_len == 0 ||
613 startblock_fsb >= mp->m_sb.sb_dblocks ||
614 extp->ext_len >= mp->m_sb.sb_agblocks) {
616 * This will pull the EFI from the AIL and
617 * free the memory associated with it.
619 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
620 xfs_efi_release(efip);
621 return -EFSCORRUPTED;
625 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
628 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
630 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
631 extp = &efip->efi_format.efi_extents[i];
632 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
634 &XFS_RMAP_OINFO_ANY_OWNER, false);
640 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
641 error = xfs_trans_commit(tp);
645 xfs_trans_cancel(tp);
649 const struct xlog_recover_item_ops xlog_efi_item_ops = {
650 .item_type = XFS_LI_EFI,
653 const struct xlog_recover_item_ops xlog_efd_item_ops = {
654 .item_type = XFS_LI_EFD,