1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2022-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_btree.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
16 #include "xfs_inode.h"
17 #include "xfs_alloc.h"
18 #include "xfs_alloc_btree.h"
19 #include "xfs_ialloc.h"
20 #include "xfs_ialloc_btree.h"
22 #include "xfs_rmap_btree.h"
23 #include "xfs_refcount_btree.h"
24 #include "xfs_extent_busy.h"
26 #include "xfs_ag_resv.h"
27 #include "xfs_quota.h"
30 #include "scrub/scrub.h"
31 #include "scrub/common.h"
32 #include "scrub/trace.h"
33 #include "scrub/repair.h"
34 #include "scrub/bitmap.h"
35 #include "scrub/reap.h"
38 * Disposal of Blocks from Old Metadata
40 * Now that we've constructed a new btree to replace the damaged one, we want
41 * to dispose of the blocks that (we think) the old btree was using.
42 * Previously, we used the rmapbt to collect the extents (bitmap) with the
43 * rmap owner corresponding to the tree we rebuilt, collected extents for any
44 * blocks with the same rmap owner that are owned by another data structure
45 * (sublist), and subtracted sublist from bitmap. In theory the extents
46 * remaining in bitmap are the old btree's blocks.
48 * Unfortunately, it's possible that the btree was crosslinked with other
49 * blocks on disk. The rmap data can tell us if there are multiple owners, so
50 * if the rmapbt says there is an owner of this block other than @oinfo, then
51 * the block is crosslinked. Remove the reverse mapping and continue.
53 * If there is one rmap record, we can free the block, which removes the
54 * reverse mapping but doesn't add the block to the free space. Our repair
55 * strategy is to hope the other metadata objects crosslinked on this block
56 * will be rebuilt (atop different blocks), thereby removing all the cross
59 * If there are no rmap records at all, we also free the block. If the btree
60 * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
61 * supposed to be a rmap record and everything is ok. For other btrees there
62 * had to have been an rmap entry for the block to have ended up on @bitmap,
63 * so if it's gone now there's something wrong and the fs will shut down.
65 * Note: If there are multiple rmap records with only the same rmap owner as
66 * the btree we're trying to rebuild and the block is indeed owned by another
67 * data structure with the same rmap owner, then the block will be in sublist
68 * and therefore doesn't need disposal. If there are multiple rmap records
69 * with only the same rmap owner but the block is not owned by something with
70 * the same rmap owner, the block will be freed.
72 * The caller is responsible for locking the AG headers for the entire rebuild
73 * operation so that nothing else can sneak in and change the AG state while
74 * we're not looking. We must also invalidate any buffers associated with
78 /* Information about reaping extents after a repair. */
79 struct xrep_reap_state {
82 /* Reverse mapping owner and metadata reservation type. */
83 const struct xfs_owner_info *oinfo;
84 enum xfs_ag_resv_type resv;
86 /* Number of deferred reaps attached to the current transaction. */
87 unsigned int deferred;
90 /* Put a block back on the AGFL. */
96 struct xfs_buf *agfl_bp;
99 /* Make sure there's space on the freelist. */
100 error = xrep_fix_freelist(sc, true);
105 * Since we're "freeing" a lost block onto the AGFL, we have to
106 * create an rmap for the block prior to merging it or else other
109 error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.pag, agbno, 1,
114 /* Put the block on the AGFL. */
115 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
119 error = xfs_alloc_put_freelist(sc->sa.pag, sc->tp, sc->sa.agf_bp,
123 xfs_extent_busy_insert(sc->tp, sc->sa.pag, agbno, 1,
124 XFS_EXTENT_BUSY_SKIP_DISCARD);
129 /* Try to invalidate the incore buffer for a block that we're about to free. */
131 xrep_block_reap_binval(
132 struct xfs_scrub *sc,
135 struct xfs_buf *bp = NULL;
139 * If there's an incore buffer for exactly this block, invalidate it.
140 * Avoid invalidating AG headers and post-EOFS blocks because we never
143 if (!xfs_verify_fsbno(sc->mp, fsbno))
147 * We assume that the lack of any other known owners means that the
148 * buffer can be locked without risk of deadlocking.
150 error = xfs_buf_incore(sc->mp->m_ddev_targp,
151 XFS_FSB_TO_DADDR(sc->mp, fsbno),
152 XFS_FSB_TO_BB(sc->mp, 1), 0, &bp);
156 xfs_trans_bjoin(sc->tp, bp);
157 xfs_trans_binval(sc->tp, bp);
160 /* Dispose of a single block. */
166 struct xrep_reap_state *rs = priv;
167 struct xfs_scrub *sc = rs->sc;
168 struct xfs_btree_cur *cur;
172 bool need_roll = true;
175 agno = XFS_FSB_TO_AGNO(sc->mp, fsbno);
176 agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
178 /* We don't support reaping file extents yet. */
179 if (sc->ip != NULL || sc->sa.pag->pag_agno != agno) {
181 return -EFSCORRUPTED;
184 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp, sc->sa.pag);
186 /* Can we find any other rmappings? */
187 error = xfs_rmap_has_other_keys(cur, agbno, 1, rs->oinfo,
189 xfs_btree_del_cursor(cur, error);
194 * If there are other rmappings, this block is cross linked and must
195 * not be freed. Remove the reverse mapping and move on. Otherwise,
196 * we were the only owner of the block, so free the extent, which will
197 * also remove the rmap.
199 * XXX: XFS doesn't support detecting the case where a single block
200 * metadata structure is crosslinked with a multi-block structure
201 * because the buffer cache doesn't detect aliasing problems, so we
202 * can't fix 100% of crosslinking problems (yet). The verifiers will
203 * blow on writeout, the filesystem will shut down, and the admin gets
206 if (has_other_rmap) {
207 trace_xrep_dispose_unmap_extent(sc->sa.pag, agbno, 1);
209 error = xfs_rmap_free(sc->tp, sc->sa.agf_bp, sc->sa.pag, agbno,
217 trace_xrep_dispose_free_extent(sc->sa.pag, agbno, 1);
219 xrep_block_reap_binval(sc, fsbno);
221 if (rs->resv == XFS_AG_RESV_AGFL) {
222 error = xrep_put_freelist(sc, agbno);
225 * Use deferred frees to get rid of the old btree blocks to try
226 * to minimize the window in which we could crash and lose the
227 * old blocks. However, we still need to roll the transaction
228 * every 100 or so EFIs so that we don't exceed the log
231 error = __xfs_free_extent_later(sc->tp, fsbno, 1, rs->oinfo,
236 need_roll = rs->deferred > 100;
238 if (error || !need_roll)
243 return xrep_roll_ag_trans(sc);
246 /* Dispose of every block of every extent in the bitmap. */
249 struct xfs_scrub *sc,
250 struct xbitmap *bitmap,
251 const struct xfs_owner_info *oinfo,
252 enum xfs_ag_resv_type type)
254 struct xrep_reap_state rs = {
261 ASSERT(xfs_has_rmapbt(sc->mp));
263 error = xbitmap_walk_bits(bitmap, xrep_reap_block, &rs);
264 if (error || rs.deferred == 0)
267 return xrep_roll_ag_trans(sc);