summaryrefslogtreecommitdiff
path: root/fs/xfs/scrub/rmap_repair.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/xfs/scrub/rmap_repair.c')
-rw-r--r--fs/xfs/scrub/rmap_repair.c145
1 files changed, 138 insertions, 7 deletions
diff --git a/fs/xfs/scrub/rmap_repair.c b/fs/xfs/scrub/rmap_repair.c
index 48b84d59c244..1844ebb920e1 100644
--- a/fs/xfs/scrub/rmap_repair.c
+++ b/fs/xfs/scrub/rmap_repair.c
@@ -128,6 +128,8 @@ xrep_setup_ag_rmapbt(
{
int error;
+ xchk_fshooks_enable(sc, XCHK_FSHOOKS_RMAP);
+
error = xfile_create(sc->mp, "rmapbt repair", 0, &sc->xfile);
if (error)
return error;
@@ -144,6 +146,9 @@ struct xrep_rmap {
/* new rmapbt information */
struct xrep_newbt new_btree;
+ /* lock for the xfbtree and xfile */
+ struct mutex lock;
+
/* rmap records generated from primary metadata */
struct xfbtree *rmap_btree;
@@ -152,6 +157,9 @@ struct xrep_rmap {
/* in-memory btree cursor for the xfs_btree_bload iteration */
struct xfs_btree_cur *mcur;
+ /* Hooks into rmap update code. */
+ struct xfs_rmap_hook hooks;
+
/* inode scan cursor */
struct xchk_iscan iscan;
@@ -227,11 +235,15 @@ xrep_rmap_stash(
if (xchk_should_terminate(sc, &error))
return error;
+ if (xchk_iscan_aborted(&rr->iscan))
+ return -EFSCORRUPTED;
+
trace_xrep_rmap_found(sc->mp, sc->sa.pag->pag_agno, &rmap);
+ mutex_lock(&rr->lock);
error = xfbtree_head_read_buf(rr->rmap_btree, sc->tp, &mhead_bp);
if (error)
- return error;
+ goto out_abort;
mcur = xfs_rmapbt_mem_cursor(sc->mp, sc->tp, mhead_bp, rr->rmap_btree);
error = xfs_rmap_map_raw(mcur, &rmap);
@@ -239,10 +251,18 @@ xrep_rmap_stash(
if (error)
goto out_cancel;
- return xfbtree_trans_commit(rr->rmap_btree, sc->tp);
+ error = xfbtree_trans_commit(rr->rmap_btree, sc->tp);
+ if (error)
+ goto out_abort;
+
+ mutex_unlock(&rr->lock);
+ return 0;
out_cancel:
xfbtree_trans_cancel(rr->rmap_btree, sc->tp);
+out_abort:
+ xchk_iscan_abort(&rr->iscan);
+ mutex_unlock(&rr->lock);
return error;
}
@@ -884,6 +904,13 @@ end_agscan:
return error;
/*
+ * If a hook failed to update the in-memory btree, we lack the data to
+ * continue the repair.
+ */
+ if (xchk_iscan_aborted(&rr->iscan))
+ return -EFSCORRUPTED;
+
+ /*
* Now that we have everything locked again, we need to count the
* number of rmap records stashed in the btree. This should reflect
* all actively-owned space in the filesystem. At the same time, check
@@ -1482,6 +1509,96 @@ out_bitmap:
return error;
}
+static inline bool
+xrep_rmapbt_want_live_update(
+ struct xchk_iscan *iscan,
+ const struct xfs_owner_info *oi)
+{
+ if (xchk_iscan_aborted(iscan))
+ return false;
+
+ /*
+ * Before unlocking the AG header to perform the inode scan, we
+ * recorded reverse mappings for all AG metadata except for the OWN_AG
+ * metadata. IOWs, the in-memory btree knows about the AG headers, the
+ * two inode btrees, the CoW staging extents, and the refcount btrees.
+ * For these types of metadata, we need to record the live updates in
+ * the in-memory rmap btree.
+ *
+ * However, we do not scan the free space btrees or the AGFL until we
+ * have re-locked the AGF and are ready to reserve space for the new
+ * new rmap btree, so we do not want live updates for OWN_AG metadata.
+ */
+ if (XFS_RMAP_NON_INODE_OWNER(oi->oi_owner))
+ return oi->oi_owner != XFS_RMAP_OWN_AG;
+
+ /* Ignore updates to files that the scanner hasn't visited yet. */
+ return xchk_iscan_want_live_update(iscan, oi->oi_owner);
+}
+
+/*
+ * Apply a rmapbt update from the regular filesystem into our shadow btree.
+ * We're running from the thread that owns the AGF buffer and is generating
+ * the update, so we must be careful about which parts of the struct xrep_rmap
+ * that we change.
+ */
+static int
+xrep_rmapbt_live_update(
+ struct xfs_hook *hook,
+ unsigned long action,
+ void *data)
+{
+ struct xfs_rmap_update_params *p = data;
+ struct xrep_rmap *rr;
+ struct xfs_mount *mp;
+ struct xfs_btree_cur *mcur;
+ struct xfs_buf *mhead_bp;
+ struct xfs_trans *tp;
+ void *txcookie;
+ int error;
+
+ rr = container_of(hook, struct xrep_rmap, hooks.update_hook);
+ mp = rr->sc->mp;
+
+ if (!xrep_rmapbt_want_live_update(&rr->iscan, &p->oinfo))
+ goto out_unlock;
+
+ trace_xrep_rmap_live_update(mp, rr->sc->sa.pag->pag_agno, action, p);
+
+ error = xrep_trans_alloc_hook_dummy(mp, &txcookie, &tp);
+ if (error)
+ goto out_abort;
+
+ mutex_lock(&rr->lock);
+ error = xfbtree_head_read_buf(rr->rmap_btree, tp, &mhead_bp);
+ if (error)
+ goto out_cancel;
+
+ mcur = xfs_rmapbt_mem_cursor(mp, tp, mhead_bp, rr->rmap_btree);
+ error = __xfs_rmap_finish_intent(mcur, action, p->startblock,
+ p->blockcount, &p->oinfo, p->unwritten);
+ xfs_btree_del_cursor(mcur, error);
+ if (error)
+ goto out_cancel;
+
+ error = xfbtree_trans_commit(rr->rmap_btree, tp);
+ if (error)
+ goto out_cancel;
+
+ xrep_trans_cancel_hook_dummy(&txcookie, tp);
+ mutex_unlock(&rr->lock);
+ return NOTIFY_DONE;
+
+out_cancel:
+ xfbtree_trans_cancel(rr->rmap_btree, tp);
+ xrep_trans_cancel_hook_dummy(&txcookie, tp);
+out_abort:
+ mutex_unlock(&rr->lock);
+ xchk_iscan_abort(&rr->iscan);
+out_unlock:
+ return NOTIFY_DONE;
+}
+
/* Repair the rmap btree for some AG. */
int
xrep_rmapbt(
@@ -1490,13 +1607,11 @@ xrep_rmapbt(
struct xrep_rmap *rr;
int error;
- /* Functionality is not yet complete. */
- return xrep_notsupported(sc);
-
rr = kzalloc(sizeof(struct xrep_rmap), XCHK_GFP_FLAGS);
if (!rr)
return -ENOMEM;
rr->sc = sc;
+ mutex_init(&rr->lock);
/* Set up in-memory rmap btree */
error = xfs_rmapbt_mem_create(sc->mp, sc->sa.pag->pag_agno,
@@ -1508,25 +1623,41 @@ xrep_rmapbt(
xchk_iscan_start(&rr->iscan, 30000, 100);
/*
+ * Hook into live rmap operations so that we can update our in-memory
+ * btree to reflect live changes on the filesystem. Since we drop the
+ * AGF buffer to scan all the inodes, we need this piece to avoid
+ * installing a stale btree.
+ */
+ ASSERT(sc->flags & XCHK_FSHOOKS_RMAP);
+ xfs_hook_setup(&rr->hooks.update_hook, xrep_rmapbt_live_update);
+ error = xfs_rmap_hook_add(sc->sa.pag, &rr->hooks);
+ if (error)
+ goto out_records;
+
+ /*
* Collect rmaps for everything in this AG that isn't space metadata.
* These rmaps won't change even as we try to allocate blocks.
*/
error = xrep_rmap_find_rmaps(rr);
if (error)
- goto out_records;
+ goto out_abort;
/* Rebuild the rmap information. */
error = xrep_rmap_build_new_tree(rr);
if (error)
- goto out_records;
+ goto out_abort;
/* Kill the old tree. */
error = xrep_rmap_remove_old_tree(rr);
+out_abort:
+ xchk_iscan_abort(&rr->iscan);
+ xfs_rmap_hook_del(sc->sa.pag, &rr->hooks);
out_records:
xchk_iscan_finish(&rr->iscan);
xfbtree_destroy(rr->rmap_btree);
out_rr:
+ mutex_destroy(&rr->lock);
kfree(rr);
return error;
}