1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2022-2023 Oracle. All Rights Reserved.
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 "xfs_da_format.h"
31 #include "xfs_da_btree.h"
33 #include "xfs_attr_remote.h"
34 #include "scrub/scrub.h"
35 #include "scrub/common.h"
36 #include "scrub/trace.h"
37 #include "scrub/repair.h"
38 #include "scrub/bitmap.h"
39 #include "scrub/reap.h"
42 * Disposal of Blocks from Old Metadata
44 * Now that we've constructed a new btree to replace the damaged one, we want
45 * to dispose of the blocks that (we think) the old btree was using.
46 * Previously, we used the rmapbt to collect the extents (bitmap) with the
47 * rmap owner corresponding to the tree we rebuilt, collected extents for any
48 * blocks with the same rmap owner that are owned by another data structure
49 * (sublist), and subtracted sublist from bitmap. In theory the extents
50 * remaining in bitmap are the old btree's blocks.
52 * Unfortunately, it's possible that the btree was crosslinked with other
53 * blocks on disk. The rmap data can tell us if there are multiple owners, so
54 * if the rmapbt says there is an owner of this block other than @oinfo, then
55 * the block is crosslinked. Remove the reverse mapping and continue.
57 * If there is one rmap record, we can free the block, which removes the
58 * reverse mapping but doesn't add the block to the free space. Our repair
59 * strategy is to hope the other metadata objects crosslinked on this block
60 * will be rebuilt (atop different blocks), thereby removing all the cross
63 * If there are no rmap records at all, we also free the block. If the btree
64 * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
65 * supposed to be a rmap record and everything is ok. For other btrees there
66 * had to have been an rmap entry for the block to have ended up on @bitmap,
67 * so if it's gone now there's something wrong and the fs will shut down.
69 * Note: If there are multiple rmap records with only the same rmap owner as
70 * the btree we're trying to rebuild and the block is indeed owned by another
71 * data structure with the same rmap owner, then the block will be in sublist
72 * and therefore doesn't need disposal. If there are multiple rmap records
73 * with only the same rmap owner but the block is not owned by something with
74 * the same rmap owner, the block will be freed.
76 * The caller is responsible for locking the AG headers for the entire rebuild
77 * operation so that nothing else can sneak in and change the AG state while
78 * we're not looking. We must also invalidate any buffers associated with
82 /* Information about reaping extents after a repair. */
86 /* Reverse mapping owner and metadata reservation type. */
87 const struct xfs_owner_info *oinfo;
88 enum xfs_ag_resv_type resv;
90 /* If true, roll the transaction before reaping the next extent. */
93 /* Number of deferred reaps attached to the current transaction. */
94 unsigned int deferred;
96 /* Number of invalidated buffers logged to the current transaction. */
97 unsigned int invalidated;
99 /* Number of deferred reaps queued during the whole reap sequence. */
100 unsigned long long total_deferred;
103 /* Put a block back on the AGFL. */
106 struct xfs_scrub *sc,
109 struct xfs_buf *agfl_bp;
112 /* Make sure there's space on the freelist. */
113 error = xrep_fix_freelist(sc, true);
118 * Since we're "freeing" a lost block onto the AGFL, we have to
119 * create an rmap for the block prior to merging it or else other
122 error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.pag, agbno, 1,
127 /* Put the block on the AGFL. */
128 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
132 error = xfs_alloc_put_freelist(sc->sa.pag, sc->tp, sc->sa.agf_bp,
136 xfs_extent_busy_insert(sc->tp, sc->sa.pag, agbno, 1,
137 XFS_EXTENT_BUSY_SKIP_DISCARD);
142 /* Are there any uncommitted reap operations? */
143 static inline bool xreap_dirty(const struct xreap_state *rs)
151 if (rs->total_deferred)
156 #define XREAP_MAX_BINVAL (2048)
159 * Decide if we want to roll the transaction after reaping an extent. We don't
160 * want to overrun the transaction reservation, so we prohibit more than
161 * 128 EFIs per transaction. For the same reason, we limit the number
162 * of buffer invalidations to 2048.
164 static inline bool xreap_want_roll(const struct xreap_state *rs)
168 if (rs->deferred > XREP_MAX_ITRUNCATE_EFIS)
170 if (rs->invalidated > XREAP_MAX_BINVAL)
175 static inline void xreap_reset(struct xreap_state *rs)
177 rs->total_deferred += rs->deferred;
180 rs->force_roll = false;
183 #define XREAP_MAX_DEFER_CHAIN (2048)
186 * Decide if we want to finish the deferred ops that are attached to the scrub
187 * transaction. We don't want to queue huge chains of deferred ops because
188 * that can consume a lot of log space and kernel memory. Hence we trigger a
189 * xfs_defer_finish if there are more than 2048 deferred reap operations or the
190 * caller did some real work.
193 xreap_want_defer_finish(const struct xreap_state *rs)
197 if (rs->total_deferred > XREAP_MAX_DEFER_CHAIN)
202 static inline void xreap_defer_finish_reset(struct xreap_state *rs)
204 rs->total_deferred = 0;
207 rs->force_roll = false;
210 /* Try to invalidate the incore buffers for an extent that we're freeing. */
212 xreap_agextent_binval(
213 struct xreap_state *rs,
215 xfs_extlen_t *aglenp)
217 struct xfs_scrub *sc = rs->sc;
218 struct xfs_perag *pag = sc->sa.pag;
219 struct xfs_mount *mp = sc->mp;
220 xfs_agnumber_t agno = sc->sa.pag->pag_agno;
221 xfs_agblock_t agbno_next = agbno + *aglenp;
222 xfs_agblock_t bno = agbno;
225 * Avoid invalidating AG headers and post-EOFS blocks because we never
228 if (!xfs_verify_agbno(pag, agbno) ||
229 !xfs_verify_agbno(pag, agbno_next - 1))
233 * If there are incore buffers for these blocks, invalidate them. We
234 * assume that the lack of any other known owners means that the buffer
235 * can be locked without risk of deadlocking. The buffer cache cannot
236 * detect aliasing, so employ nested loops to scan for incore buffers
237 * of any plausible size.
239 while (bno < agbno_next) {
240 xfs_agblock_t fsbcount;
241 xfs_agblock_t max_fsbs;
244 * Max buffer size is the max remote xattr buffer size, which
245 * is one fs block larger than 64k.
247 max_fsbs = min_t(xfs_agblock_t, agbno_next - bno,
248 xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX));
250 for (fsbcount = 1; fsbcount < max_fsbs; fsbcount++) {
251 struct xfs_buf *bp = NULL;
255 daddr = XFS_AGB_TO_DADDR(mp, agno, bno);
256 error = xfs_buf_incore(mp->m_ddev_targp, daddr,
257 XFS_FSB_TO_BB(mp, fsbcount),
262 xfs_trans_bjoin(sc->tp, bp);
263 xfs_trans_binval(sc->tp, bp);
267 * Stop invalidating if we've hit the limit; we should
268 * still have enough reservation left to free however
271 if (rs->invalidated > XREAP_MAX_BINVAL) {
272 *aglenp -= agbno_next - bno;
281 trace_xreap_agextent_binval(sc->sa.pag, agbno, *aglenp);
285 * Figure out the longest run of blocks that we can dispose of with a single
286 * call. Cross-linked blocks should have their reverse mappings removed, but
287 * single-owner extents can be freed. AGFL blocks can only be put back one at
291 xreap_agextent_select(
292 struct xreap_state *rs,
294 xfs_agblock_t agbno_next,
296 xfs_extlen_t *aglenp)
298 struct xfs_scrub *sc = rs->sc;
299 struct xfs_btree_cur *cur;
300 xfs_agblock_t bno = agbno + 1;
301 xfs_extlen_t len = 1;
305 * Determine if there are any other rmap records covering the first
306 * block of this extent. If so, the block is crosslinked.
308 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
310 error = xfs_rmap_has_other_keys(cur, agbno, 1, rs->oinfo,
315 /* AGFL blocks can only be deal with one at a time. */
316 if (rs->resv == XFS_AG_RESV_AGFL)
320 * Figure out how many of the subsequent blocks have the same crosslink
323 while (bno < agbno_next) {
324 bool also_crosslinked;
326 error = xfs_rmap_has_other_keys(cur, bno, 1, rs->oinfo,
331 if (*crosslinked != also_crosslinked)
340 trace_xreap_agextent_select(sc->sa.pag, agbno, len, *crosslinked);
342 xfs_btree_del_cursor(cur, error);
347 * Dispose of as much of the beginning of this AG extent as possible. The
348 * number of blocks disposed of will be returned in @aglenp.
352 struct xreap_state *rs,
354 xfs_extlen_t *aglenp,
357 struct xfs_scrub *sc = rs->sc;
361 fsbno = XFS_AGB_TO_FSB(sc->mp, sc->sa.pag->pag_agno, agbno);
364 * If there are other rmappings, this block is cross linked and must
365 * not be freed. Remove the reverse mapping and move on. Otherwise,
366 * we were the only owner of the block, so free the extent, which will
367 * also remove the rmap.
369 * XXX: XFS doesn't support detecting the case where a single block
370 * metadata structure is crosslinked with a multi-block structure
371 * because the buffer cache doesn't detect aliasing problems, so we
372 * can't fix 100% of crosslinking problems (yet). The verifiers will
373 * blow on writeout, the filesystem will shut down, and the admin gets
377 trace_xreap_dispose_unmap_extent(sc->sa.pag, agbno, *aglenp);
379 rs->force_roll = true;
380 return xfs_rmap_free(sc->tp, sc->sa.agf_bp, sc->sa.pag, agbno,
384 trace_xreap_dispose_free_extent(sc->sa.pag, agbno, *aglenp);
387 * Invalidate as many buffers as we can, starting at agbno. If this
388 * function sets *aglenp to zero, the transaction is full of logged
389 * buffer invalidations, so we need to return early so that we can
392 xreap_agextent_binval(rs, agbno, aglenp);
394 ASSERT(xreap_want_roll(rs));
398 /* Put blocks back on the AGFL one at a time. */
399 if (rs->resv == XFS_AG_RESV_AGFL) {
400 ASSERT(*aglenp == 1);
401 error = xreap_put_freelist(sc, agbno);
405 rs->force_roll = true;
410 * Use deferred frees to get rid of the old btree blocks to try to
411 * minimize the window in which we could crash and lose the old blocks.
413 error = __xfs_free_extent_later(sc->tp, fsbno, *aglenp, rs->oinfo,
423 * Break an AG metadata extent into sub-extents by fate (crosslinked, not
424 * crosslinked), and dispose of each sub-extent separately.
432 struct xreap_state *rs = priv;
433 struct xfs_scrub *sc = rs->sc;
434 xfs_agblock_t agbno = fsbno;
435 xfs_agblock_t agbno_next = agbno + len;
438 ASSERT(len <= XFS_MAX_BMBT_EXTLEN);
439 ASSERT(sc->ip == NULL);
441 while (agbno < agbno_next) {
445 error = xreap_agextent_select(rs, agbno, agbno_next,
446 &crosslinked, &aglen);
450 error = xreap_agextent_iter(rs, agbno, &aglen, crosslinked);
454 if (xreap_want_defer_finish(rs)) {
455 error = xrep_defer_finish(sc);
458 xreap_defer_finish_reset(rs);
459 } else if (xreap_want_roll(rs)) {
460 error = xrep_roll_ag_trans(sc);
472 /* Dispose of every block of every AG metadata extent in the bitmap. */
475 struct xfs_scrub *sc,
476 struct xagb_bitmap *bitmap,
477 const struct xfs_owner_info *oinfo,
478 enum xfs_ag_resv_type type)
480 struct xreap_state rs = {
487 ASSERT(xfs_has_rmapbt(sc->mp));
488 ASSERT(sc->ip == NULL);
490 error = xagb_bitmap_walk(bitmap, xreap_agmeta_extent, &rs);
494 if (xreap_dirty(&rs))
495 return xrep_defer_finish(sc);