1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-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_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_trans.h"
14 #include "xfs_btree.h"
16 #include "xfs_refcount.h"
19 #include "xfs_alloc.h"
20 #include "xfs_alloc_btree.h"
21 #include "scrub/scrub.h"
22 #include "scrub/common.h"
23 #include "scrub/btree.h"
24 #include "scrub/bitmap.h"
27 * Set us up to scrub reverse mapping btrees.
33 if (xchk_need_intent_drain(sc))
34 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
36 return xchk_setup_ag_btree(sc, false);
39 /* Reverse-mapping scrubber. */
43 * The furthest-reaching of the rmapbt records that we've already
44 * processed. This enables us to detect overlapping records for space
45 * allocations that cannot be shared.
47 struct xfs_rmap_irec overlap_rec;
50 * The previous rmapbt record, so that we can check for two records
53 struct xfs_rmap_irec prev_rec;
55 /* Bitmaps containing all blocks for each type of AG metadata. */
56 struct xagb_bitmap fs_owned;
57 struct xagb_bitmap log_owned;
58 struct xagb_bitmap ag_owned;
60 /* Did we complete the AG space metadata bitmaps? */
61 bool bitmaps_complete;
64 /* Cross-reference a rmap against the refcount btree. */
66 xchk_rmapbt_xref_refc(
68 struct xfs_rmap_irec *irec)
78 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
81 non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
82 is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
83 is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
84 is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
86 /* If this is shared, must be a data fork extent. */
87 error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
88 irec->rm_blockcount, &fbno, &flen, false);
89 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
91 if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
92 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
95 /* Cross-reference with the other btrees. */
99 struct xfs_rmap_irec *irec)
101 xfs_agblock_t agbno = irec->rm_startblock;
102 xfs_extlen_t len = irec->rm_blockcount;
104 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
107 xchk_xref_is_used_space(sc, agbno, len);
108 if (irec->rm_owner == XFS_RMAP_OWN_INODES)
109 xchk_xref_is_inode_chunk(sc, agbno, len);
111 xchk_xref_is_not_inode_chunk(sc, agbno, len);
112 if (irec->rm_owner == XFS_RMAP_OWN_COW)
113 xchk_xref_is_cow_staging(sc, irec->rm_startblock,
114 irec->rm_blockcount);
116 xchk_rmapbt_xref_refc(sc, irec);
120 * Check for bogus UNWRITTEN flags in the rmapbt node block keys.
122 * In reverse mapping records, the file mapping extent state
123 * (XFS_RMAP_OFF_UNWRITTEN) is a record attribute, not a key field. It is not
124 * involved in lookups in any way. In older kernels, the functions that
125 * convert rmapbt records to keys forgot to filter out the extent state bit,
126 * even though the key comparison functions have filtered the flag correctly.
127 * If we spot an rmap key with the unwritten bit set in rm_offset, we should
128 * mark the btree as needing optimization to rebuild the btree without those
132 xchk_rmapbt_check_unwritten_in_keyflags(
133 struct xchk_btree *bs)
135 struct xfs_scrub *sc = bs->sc;
136 struct xfs_btree_cur *cur = bs->cur;
137 struct xfs_btree_block *keyblock;
138 union xfs_btree_key *lkey, *hkey;
139 __be64 badflag = cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
142 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_PREEN)
145 for (level = 1; level < cur->bc_nlevels; level++) {
149 /* Only check the first time we've seen this node block. */
150 if (cur->bc_levels[level].ptr > 1)
153 keyblock = xfs_btree_get_block(cur, level, &bp);
154 for (ptr = 1; ptr <= be16_to_cpu(keyblock->bb_numrecs); ptr++) {
155 lkey = xfs_btree_key_addr(cur, ptr, keyblock);
157 if (lkey->rmap.rm_offset & badflag) {
158 xchk_btree_set_preen(sc, cur, level);
162 hkey = xfs_btree_high_key_addr(cur, ptr, keyblock);
163 if (hkey->rmap.rm_offset & badflag) {
164 xchk_btree_set_preen(sc, cur, level);
172 xchk_rmapbt_is_shareable(
173 struct xfs_scrub *sc,
174 const struct xfs_rmap_irec *irec)
176 if (!xfs_has_reflink(sc->mp))
178 if (XFS_RMAP_NON_INODE_OWNER(irec->rm_owner))
180 if (irec->rm_flags & (XFS_RMAP_BMBT_BLOCK | XFS_RMAP_ATTR_FORK |
186 /* Flag failures for records that overlap but cannot. */
188 xchk_rmapbt_check_overlapping(
189 struct xchk_btree *bs,
190 struct xchk_rmap *cr,
191 const struct xfs_rmap_irec *irec)
193 xfs_agblock_t pnext, inext;
195 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
198 /* No previous record? */
199 if (cr->overlap_rec.rm_blockcount == 0)
202 /* Do overlap_rec and irec overlap? */
203 pnext = cr->overlap_rec.rm_startblock + cr->overlap_rec.rm_blockcount;
204 if (pnext <= irec->rm_startblock)
207 /* Overlap is only allowed if both records are data fork mappings. */
208 if (!xchk_rmapbt_is_shareable(bs->sc, &cr->overlap_rec) ||
209 !xchk_rmapbt_is_shareable(bs->sc, irec))
210 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
212 /* Save whichever rmap record extends furthest. */
213 inext = irec->rm_startblock + irec->rm_blockcount;
218 memcpy(&cr->overlap_rec, irec, sizeof(struct xfs_rmap_irec));
221 /* Decide if two reverse-mapping records can be merged. */
224 struct xchk_rmap *cr,
225 const struct xfs_rmap_irec *r2)
227 const struct xfs_rmap_irec *r1 = &cr->prev_rec;
229 /* Ignore if prev_rec is not yet initialized. */
230 if (cr->prev_rec.rm_blockcount == 0)
233 if (r1->rm_owner != r2->rm_owner)
235 if (r1->rm_startblock + r1->rm_blockcount != r2->rm_startblock)
237 if ((unsigned long long)r1->rm_blockcount + r2->rm_blockcount >
240 if (XFS_RMAP_NON_INODE_OWNER(r2->rm_owner))
242 /* must be an inode owner below here */
243 if (r1->rm_flags != r2->rm_flags)
245 if (r1->rm_flags & XFS_RMAP_BMBT_BLOCK)
247 return r1->rm_offset + r1->rm_blockcount == r2->rm_offset;
250 /* Flag failures for records that could be merged. */
252 xchk_rmapbt_check_mergeable(
253 struct xchk_btree *bs,
254 struct xchk_rmap *cr,
255 const struct xfs_rmap_irec *irec)
257 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
260 if (xchk_rmap_mergeable(cr, irec))
261 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
263 memcpy(&cr->prev_rec, irec, sizeof(struct xfs_rmap_irec));
266 /* Compare an rmap for AG metadata against the metadata walk. */
268 xchk_rmapbt_mark_bitmap(
269 struct xchk_btree *bs,
270 struct xchk_rmap *cr,
271 const struct xfs_rmap_irec *irec)
273 struct xfs_scrub *sc = bs->sc;
274 struct xagb_bitmap *bmp = NULL;
275 xfs_extlen_t fsbcount = irec->rm_blockcount;
278 * Skip corrupt records. It is essential that we detect records in the
279 * btree that cannot overlap but do, flag those as CORRUPT, and skip
280 * the bitmap comparison to avoid generating false XCORRUPT reports.
282 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
286 * If the AG metadata walk didn't complete, there's no point in
287 * comparing against partial results.
289 if (!cr->bitmaps_complete)
292 switch (irec->rm_owner) {
293 case XFS_RMAP_OWN_FS:
296 case XFS_RMAP_OWN_LOG:
297 bmp = &cr->log_owned;
299 case XFS_RMAP_OWN_AG:
307 if (xagb_bitmap_test(bmp, irec->rm_startblock, &fsbcount)) {
309 * The start of this reverse mapping corresponds to a set
310 * region in the bitmap. If the mapping covers more area than
311 * the set region, then it covers space that wasn't found by
312 * the AG metadata walk.
314 if (fsbcount < irec->rm_blockcount)
315 xchk_btree_xref_set_corrupt(bs->sc,
316 bs->sc->sa.rmap_cur, 0);
319 * The start of this reverse mapping does not correspond to a
320 * completely set region in the bitmap. The region wasn't
321 * fully set by walking the AG metadata, so this is a
322 * cross-referencing corruption.
324 xchk_btree_xref_set_corrupt(bs->sc, bs->sc->sa.rmap_cur, 0);
327 /* Unset the region so that we can detect missing rmap records. */
328 return xagb_bitmap_clear(bmp, irec->rm_startblock, irec->rm_blockcount);
331 /* Scrub an rmapbt record. */
334 struct xchk_btree *bs,
335 const union xfs_btree_rec *rec)
337 struct xchk_rmap *cr = bs->private;
338 struct xfs_rmap_irec irec;
340 if (xfs_rmap_btrec_to_irec(rec, &irec) != NULL ||
341 xfs_rmap_check_irec(bs->cur, &irec) != NULL) {
342 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
346 xchk_rmapbt_check_unwritten_in_keyflags(bs);
347 xchk_rmapbt_check_mergeable(bs, cr, &irec);
348 xchk_rmapbt_check_overlapping(bs, cr, &irec);
349 xchk_rmapbt_xref(bs->sc, &irec);
351 return xchk_rmapbt_mark_bitmap(bs, cr, &irec);
354 /* Add an AGFL block to the rmap list. */
356 xchk_rmapbt_walk_agfl(
357 struct xfs_mount *mp,
361 struct xagb_bitmap *bitmap = priv;
363 return xagb_bitmap_set(bitmap, agbno, 1);
367 * Set up bitmaps mapping all the AG metadata to compare with the rmapbt
370 * Grab our own btree cursors here if the scrub setup function didn't give us a
371 * btree cursor due to reports of poor health. We need to find out if the
372 * rmapbt disagrees with primary metadata btrees to tag the rmapbt as being
376 xchk_rmapbt_walk_ag_metadata(
377 struct xfs_scrub *sc,
378 struct xchk_rmap *cr)
380 struct xfs_mount *mp = sc->mp;
381 struct xfs_buf *agfl_bp;
382 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
383 struct xfs_btree_cur *cur;
386 /* OWN_FS: AG headers */
387 error = xagb_bitmap_set(&cr->fs_owned, XFS_SB_BLOCK(mp),
388 XFS_AGFL_BLOCK(mp) - XFS_SB_BLOCK(mp) + 1);
392 /* OWN_LOG: Internal log */
393 if (xfs_ag_contains_log(mp, sc->sa.pag->pag_agno)) {
394 error = xagb_bitmap_set(&cr->log_owned,
395 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart),
396 mp->m_sb.sb_logblocks);
401 /* OWN_AG: bnobt, cntbt, rmapbt, and AGFL */
402 cur = sc->sa.bno_cur;
404 cur = xfs_allocbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
405 sc->sa.pag, XFS_BTNUM_BNO);
406 error = xagb_bitmap_set_btblocks(&cr->ag_owned, cur);
407 if (cur != sc->sa.bno_cur)
408 xfs_btree_del_cursor(cur, error);
412 cur = sc->sa.cnt_cur;
414 cur = xfs_allocbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
415 sc->sa.pag, XFS_BTNUM_CNT);
416 error = xagb_bitmap_set_btblocks(&cr->ag_owned, cur);
417 if (cur != sc->sa.cnt_cur)
418 xfs_btree_del_cursor(cur, error);
422 error = xagb_bitmap_set_btblocks(&cr->ag_owned, sc->sa.rmap_cur);
426 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
430 error = xfs_agfl_walk(sc->mp, agf, agfl_bp, xchk_rmapbt_walk_agfl,
432 xfs_trans_brelse(sc->tp, agfl_bp);
436 * If there's an error, set XFAIL and disable the bitmap
437 * cross-referencing checks, but proceed with the scrub anyway.
440 xchk_btree_xref_process_error(sc, sc->sa.rmap_cur,
441 sc->sa.rmap_cur->bc_nlevels - 1, &error);
443 cr->bitmaps_complete = true;
448 * Check for set regions in the bitmaps; if there are any, the rmap records do
449 * not describe all the AG metadata.
452 xchk_rmapbt_check_bitmaps(
453 struct xfs_scrub *sc,
454 struct xchk_rmap *cr)
456 struct xfs_btree_cur *cur = sc->sa.rmap_cur;
459 if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
460 XFS_SCRUB_OFLAG_XFAIL))
464 level = cur->bc_nlevels - 1;
467 * Any bitmap with bits still set indicates that the reverse mapping
468 * doesn't cover the entire primary structure.
470 if (xagb_bitmap_hweight(&cr->fs_owned) != 0)
471 xchk_btree_xref_set_corrupt(sc, cur, level);
473 if (xagb_bitmap_hweight(&cr->log_owned) != 0)
474 xchk_btree_xref_set_corrupt(sc, cur, level);
476 if (xagb_bitmap_hweight(&cr->ag_owned) != 0)
477 xchk_btree_xref_set_corrupt(sc, cur, level);
480 /* Scrub the rmap btree for some AG. */
483 struct xfs_scrub *sc)
485 struct xchk_rmap *cr;
488 cr = kzalloc(sizeof(struct xchk_rmap), XCHK_GFP_FLAGS);
492 xagb_bitmap_init(&cr->fs_owned);
493 xagb_bitmap_init(&cr->log_owned);
494 xagb_bitmap_init(&cr->ag_owned);
496 error = xchk_rmapbt_walk_ag_metadata(sc, cr);
500 error = xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
501 &XFS_RMAP_OINFO_AG, cr);
505 xchk_rmapbt_check_bitmaps(sc, cr);
508 xagb_bitmap_destroy(&cr->ag_owned);
509 xagb_bitmap_destroy(&cr->log_owned);
510 xagb_bitmap_destroy(&cr->fs_owned);
515 /* xref check that the extent is owned only by a given owner */
517 xchk_xref_is_only_owned_by(
518 struct xfs_scrub *sc,
521 const struct xfs_owner_info *oinfo)
523 struct xfs_rmap_matches res;
526 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
529 error = xfs_rmap_count_owners(sc->sa.rmap_cur, bno, len, oinfo, &res);
530 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
532 if (res.matches != 1)
533 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
534 if (res.bad_non_owner_matches)
535 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
536 if (res.non_owner_matches)
537 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
540 /* xref check that the extent is not owned by a given owner */
542 xchk_xref_is_not_owned_by(
543 struct xfs_scrub *sc,
546 const struct xfs_owner_info *oinfo)
548 struct xfs_rmap_matches res;
551 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
554 error = xfs_rmap_count_owners(sc->sa.rmap_cur, bno, len, oinfo, &res);
555 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
557 if (res.matches != 0)
558 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
559 if (res.bad_non_owner_matches)
560 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
563 /* xref check that the extent has no reverse mapping at all */
565 xchk_xref_has_no_owner(
566 struct xfs_scrub *sc,
570 enum xbtree_recpacking outcome;
573 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
576 error = xfs_rmap_has_records(sc->sa.rmap_cur, bno, len, &outcome);
577 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
579 if (outcome != XBTREE_RECPACKING_EMPTY)
580 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);