summaryrefslogtreecommitdiff
path: root/fs/xfs/scrub/newbt.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/xfs/scrub/newbt.c')
-rw-r--r--fs/xfs/scrub/newbt.c185
1 files changed, 181 insertions, 4 deletions
diff --git a/fs/xfs/scrub/newbt.c b/fs/xfs/scrub/newbt.c
index 8adbee26f2f9..c7f23e501095 100644
--- a/fs/xfs/scrub/newbt.c
+++ b/fs/xfs/scrub/newbt.c
@@ -13,12 +13,14 @@
#include "xfs_btree_staging.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
+#include "xfs_log.h"
#include "xfs_sb.h"
#include "xfs_inode.h"
#include "xfs_alloc.h"
#include "xfs_rmap.h"
#include "xfs_ag.h"
#include "xfs_defer.h"
+#include "xfs_extfree_item.h"
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/trace.h"
@@ -128,14 +130,150 @@ xrep_newbt_init_bare(
XFS_AG_RESV_NONE);
}
-/* Designate specific blocks to be used to build our new btree. */
+/*
+ * Set up automatic reaping of the blocks reserved for btree reconstruction in
+ * case we crash by logging a deferred free item for each extent we allocate so
+ * that we can get all of the space back if we crash before we can commit the
+ * new btree. This function returns a token that can be used to cancel
+ * automatic reaping if repair is successful.
+ */
+static int
+xrep_newbt_schedule_autoreap(
+ struct xrep_newbt *xnr,
+ struct xrep_newbt_resv *resv)
+{
+ struct xfs_extent_free_item efi_item = {
+ .xefi_startblock = resv->fsbno,
+ .xefi_blockcount = resv->len,
+ .xefi_owner = xnr->oinfo.oi_owner,
+ .xefi_flags = XFS_EFI_SKIP_DISCARD,
+ };
+ struct xfs_log_item *lip;
+ LIST_HEAD(items);
+
+ ASSERT(xnr->oinfo.oi_offset == 0);
+
+ if (xnr->oinfo.oi_flags & XFS_OWNER_INFO_ATTR_FORK)
+ efi_item.xefi_flags |= XFS_EFI_ATTR_FORK;
+ if (xnr->oinfo.oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
+ efi_item.xefi_flags |= XFS_EFI_BMBT_BLOCK;
+
+ INIT_LIST_HEAD(&efi_item.xefi_list);
+ list_add(&efi_item.xefi_list, &items);
+ xfs_fs_bump_intents(xnr->sc->mp, resv->fsbno);
+ lip = xfs_extent_free_defer_type.create_intent(xnr->sc->tp,
+ &items, 1, false);
+ if (!lip) {
+ ASSERT(0);
+ xfs_fs_drop_intents(xnr->sc->mp, resv->fsbno);
+ return -EFSCORRUPTED;
+ }
+ if (IS_ERR(lip)) {
+ xfs_fs_drop_intents(xnr->sc->mp, resv->fsbno);
+ return PTR_ERR(lip);
+ }
+
+ resv->efi = lip;
+ return 0;
+}
+
+/*
+ * Earlier, we logged EFIs for the extents that we allocated to hold the new
+ * btree so that we could automatically roll back those allocations if the
+ * system crashed. Now we log an EFD to cancel the EFI, either because the
+ * repair succeeded and the new blocks are in use; or because the repair was
+ * cancelled and we're about to free the extents directly.
+ */
+static inline void
+xrep_newbt_finish_autoreap(
+ struct xfs_scrub *sc,
+ struct xrep_newbt_resv *resv)
+{
+ struct xfs_efd_log_item *efdp;
+ struct xfs_extent *extp;
+ struct xfs_log_item *efd_lip;
+
+ efd_lip = xfs_extent_free_defer_type.create_done(sc->tp, resv->efi, 1);
+ efdp = container_of(efd_lip, struct xfs_efd_log_item, efd_item);
+ extp = efdp->efd_format.efd_extents;
+ extp->ext_start = resv->fsbno;
+ extp->ext_len = resv->len;
+ efdp->efd_next_extent++;
+ set_bit(XFS_LI_DIRTY, &efd_lip->li_flags);
+}
+
+/* Abort an EFI logged for a new btree block reservation. */
+static inline void
+xrep_newbt_cancel_autoreap(
+ struct xrep_newbt_resv *resv)
+{
+ xfs_extent_free_defer_type.abort_intent(resv->efi);
+}
+
+/*
+ * Relog the EFIs attached to a staging btree so that we don't pin the log
+ * tail. Same logic as xfs_defer_relog.
+ */
int
-xrep_newbt_add_blocks(
+xrep_newbt_relog_autoreap(
+ struct xrep_newbt *xnr)
+{
+ struct xrep_newbt_resv *resv;
+ unsigned int efi_bytes = 0;
+
+ list_for_each_entry(resv, &xnr->resv_list, list) {
+ /*
+ * If the log intent item for this deferred op is in a
+ * different checkpoint, relog it to keep the log tail moving
+ * forward. We're ok with this being racy because an incorrect
+ * decision means we'll be a little slower at pushing the tail.
+ */
+ if (!resv->efi || xfs_log_item_in_current_chkpt(resv->efi))
+ continue;
+
+ resv->efi = xfs_trans_item_relog(resv->efi, xnr->sc->tp);
+
+ /*
+ * If free space is very fragmented, it's possible that the new
+ * btree will be allocated a large number of small extents.
+ * On an active system, it's possible that so many of those
+ * EFIs will need relogging here that doing them all in one
+ * transaction will overflow the reservation.
+ *
+ * Each allocation for the new btree (xrep_newbt_resv) points
+ * to a unique single-mapping EFI, so each relog operation logs
+ * a single-mapping EFD followed by a new EFI. Each single
+ * mapping EF[ID] item consumes about 128 bytes, so we'll
+ * assume 256 bytes per relog. Roll if we consume more than
+ * half of the transaction reservation.
+ */
+ efi_bytes += 256;
+ if (efi_bytes > xnr->sc->tp->t_log_res / 2) {
+ int error;
+
+ error = xrep_roll_trans(xnr->sc);
+ if (error)
+ return error;
+
+ efi_bytes = 0;
+ }
+ }
+
+ if (xnr->sc->tp->t_flags & XFS_TRANS_DIRTY)
+ return xrep_roll_trans(xnr->sc);
+ return 0;
+}
+
+/* Designate specific blocks to be used to build our new btree. */
+static int
+__xrep_newbt_add_blocks(
struct xrep_newbt *xnr,
xfs_fsblock_t fsbno,
- xfs_extlen_t len)
+ xfs_extlen_t len,
+ bool auto_reap)
{
struct xrep_newbt_resv *resv;
+ int error;
resv = kmalloc(sizeof(struct xrep_newbt_resv), XCHK_GFP_FLAGS);
if (!resv)
@@ -145,10 +283,31 @@ xrep_newbt_add_blocks(
resv->fsbno = fsbno;
resv->len = len;
resv->used = 0;
+ if (auto_reap) {
+ error = xrep_newbt_schedule_autoreap(xnr, resv);
+ if (error) {
+ kfree(resv);
+ return error;
+ }
+ }
+
list_add_tail(&resv->list, &xnr->resv_list);
return 0;
}
+/*
+ * Allow certain callers to add disk space directly to the reservation.
+ * Callers are responsible for cleaning up the reservations.
+ */
+int
+xrep_newbt_add_blocks(
+ struct xrep_newbt *xnr,
+ xfs_fsblock_t fsbno,
+ xfs_extlen_t len)
+{
+ return __xrep_newbt_add_blocks(xnr, fsbno, len, false);
+}
+
/* Allocate disk space for our new btree. */
int
xrep_newbt_alloc_blocks(
@@ -190,7 +349,8 @@ xrep_newbt_alloc_blocks(
XFS_FSB_TO_AGBNO(sc->mp, args.fsbno),
args.len, xnr->oinfo.oi_owner);
- error = xrep_newbt_add_blocks(xnr, args.fsbno, args.len);
+ error = __xrep_newbt_add_blocks(xnr, args.fsbno, args.len,
+ true);
if (error)
return error;
@@ -219,6 +379,8 @@ xrep_newbt_free_resv(
* reservations.
*/
list_for_each_entry_safe(resv, n, &xnr->resv_list, list) {
+ xrep_newbt_cancel_autoreap(resv);
+ xfs_fs_drop_intents(sc->mp, resv->fsbno);
list_del(&resv->list);
kfree(resv);
}
@@ -242,6 +404,8 @@ xrep_newbt_cancel_resv(
{
struct xfs_scrub *sc = xnr->sc;
+ xrep_newbt_finish_autoreap(sc, resv);
+
trace_xrep_newbt_cancel_blocks(sc->mp,
XFS_FSB_TO_AGNO(sc->mp, resv->fsbno),
XFS_FSB_TO_AGBNO(sc->mp, resv->fsbno),
@@ -249,6 +413,9 @@ xrep_newbt_cancel_resv(
__xfs_free_extent_later(sc->tp, resv->fsbno, resv->len,
&xnr->oinfo, true);
+
+ /* Drop the intent drain after we commit the new item. */
+ xfs_fs_drop_intents(sc->mp, resv->fsbno);
}
/*
@@ -313,6 +480,9 @@ xrep_newbt_destroy_resv(
struct xrep_newbt_resv *resv)
{
struct xfs_scrub *sc = xnr->sc;
+ xfs_fsblock_t fsbno = resv->fsbno;
+
+ xrep_newbt_finish_autoreap(sc, resv);
/*
* Use the deferred freeing mechanism to schedule for deletion any
@@ -336,6 +506,13 @@ xrep_newbt_destroy_resv(
__xfs_free_extent_later(sc->tp, resv->fsbno, resv->len,
&xnr->oinfo, true);
}
+
+ /*
+ * Drop the intent drain after we commit the new item. Use the
+ * original fsbno from the reservation because destroying the
+ * reservation consumes resv->fsbno.
+ */
+ xfs_fs_drop_intents(sc->mp, fsbno);
}
/* Free all the accounting info and disk space we reserved for a new btree. */