1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
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 "xfs_ialloc_btree.h"
22 #include "xfs_refcount_btree.h"
23 #include "scrub/scrub.h"
24 #include "scrub/common.h"
25 #include "scrub/btree.h"
26 #include "scrub/bitmap.h"
29 * Set us up to scrub reverse mapping btrees.
35 if (xchk_need_intent_drain(sc))
36 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
38 return xchk_setup_ag_btree(sc, false);
41 /* Reverse-mapping scrubber. */
45 * The furthest-reaching of the rmapbt records that we've already
46 * processed. This enables us to detect overlapping records for space
47 * allocations that cannot be shared.
49 struct xfs_rmap_irec overlap_rec;
52 * The previous rmapbt record, so that we can check for two records
55 struct xfs_rmap_irec prev_rec;
57 /* Bitmaps containing all blocks for each type of AG metadata. */
58 struct xagb_bitmap fs_owned;
59 struct xagb_bitmap log_owned;
60 struct xagb_bitmap ag_owned;
61 struct xagb_bitmap inobt_owned;
62 struct xagb_bitmap refcbt_owned;
64 /* Did we complete the AG space metadata bitmaps? */
65 bool bitmaps_complete;
68 /* Cross-reference a rmap against the refcount btree. */
70 xchk_rmapbt_xref_refc(
72 struct xfs_rmap_irec *irec)
82 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
85 non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
86 is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
87 is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
88 is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
90 /* If this is shared, must be a data fork extent. */
91 error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
92 irec->rm_blockcount, &fbno, &flen, false);
93 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
95 if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
96 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
99 /* Cross-reference with the other btrees. */
102 struct xfs_scrub *sc,
103 struct xfs_rmap_irec *irec)
105 xfs_agblock_t agbno = irec->rm_startblock;
106 xfs_extlen_t len = irec->rm_blockcount;
108 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
111 xchk_xref_is_used_space(sc, agbno, len);
112 if (irec->rm_owner == XFS_RMAP_OWN_INODES)
113 xchk_xref_is_inode_chunk(sc, agbno, len);
115 xchk_xref_is_not_inode_chunk(sc, agbno, len);
116 if (irec->rm_owner == XFS_RMAP_OWN_COW)
117 xchk_xref_is_cow_staging(sc, irec->rm_startblock,
118 irec->rm_blockcount);
120 xchk_rmapbt_xref_refc(sc, irec);
124 * Check for bogus UNWRITTEN flags in the rmapbt node block keys.
126 * In reverse mapping records, the file mapping extent state
127 * (XFS_RMAP_OFF_UNWRITTEN) is a record attribute, not a key field. It is not
128 * involved in lookups in any way. In older kernels, the functions that
129 * convert rmapbt records to keys forgot to filter out the extent state bit,
130 * even though the key comparison functions have filtered the flag correctly.
131 * If we spot an rmap key with the unwritten bit set in rm_offset, we should
132 * mark the btree as needing optimization to rebuild the btree without those
136 xchk_rmapbt_check_unwritten_in_keyflags(
137 struct xchk_btree *bs)
139 struct xfs_scrub *sc = bs->sc;
140 struct xfs_btree_cur *cur = bs->cur;
141 struct xfs_btree_block *keyblock;
142 union xfs_btree_key *lkey, *hkey;
143 __be64 badflag = cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
146 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_PREEN)
149 for (level = 1; level < cur->bc_nlevels; level++) {
153 /* Only check the first time we've seen this node block. */
154 if (cur->bc_levels[level].ptr > 1)
157 keyblock = xfs_btree_get_block(cur, level, &bp);
158 for (ptr = 1; ptr <= be16_to_cpu(keyblock->bb_numrecs); ptr++) {
159 lkey = xfs_btree_key_addr(cur, ptr, keyblock);
161 if (lkey->rmap.rm_offset & badflag) {
162 xchk_btree_set_preen(sc, cur, level);
166 hkey = xfs_btree_high_key_addr(cur, ptr, keyblock);
167 if (hkey->rmap.rm_offset & badflag) {
168 xchk_btree_set_preen(sc, cur, level);
176 xchk_rmapbt_is_shareable(
177 struct xfs_scrub *sc,
178 const struct xfs_rmap_irec *irec)
180 if (!xfs_has_reflink(sc->mp))
182 if (XFS_RMAP_NON_INODE_OWNER(irec->rm_owner))
184 if (irec->rm_flags & (XFS_RMAP_BMBT_BLOCK | XFS_RMAP_ATTR_FORK |
190 /* Flag failures for records that overlap but cannot. */
192 xchk_rmapbt_check_overlapping(
193 struct xchk_btree *bs,
194 struct xchk_rmap *cr,
195 const struct xfs_rmap_irec *irec)
197 xfs_agblock_t pnext, inext;
199 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
202 /* No previous record? */
203 if (cr->overlap_rec.rm_blockcount == 0)
206 /* Do overlap_rec and irec overlap? */
207 pnext = cr->overlap_rec.rm_startblock + cr->overlap_rec.rm_blockcount;
208 if (pnext <= irec->rm_startblock)
211 /* Overlap is only allowed if both records are data fork mappings. */
212 if (!xchk_rmapbt_is_shareable(bs->sc, &cr->overlap_rec) ||
213 !xchk_rmapbt_is_shareable(bs->sc, irec))
214 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
216 /* Save whichever rmap record extends furthest. */
217 inext = irec->rm_startblock + irec->rm_blockcount;
222 memcpy(&cr->overlap_rec, irec, sizeof(struct xfs_rmap_irec));
225 /* Decide if two reverse-mapping records can be merged. */
228 struct xchk_rmap *cr,
229 const struct xfs_rmap_irec *r2)
231 const struct xfs_rmap_irec *r1 = &cr->prev_rec;
233 /* Ignore if prev_rec is not yet initialized. */
234 if (cr->prev_rec.rm_blockcount == 0)
237 if (r1->rm_owner != r2->rm_owner)
239 if (r1->rm_startblock + r1->rm_blockcount != r2->rm_startblock)
241 if ((unsigned long long)r1->rm_blockcount + r2->rm_blockcount >
244 if (XFS_RMAP_NON_INODE_OWNER(r2->rm_owner))
246 /* must be an inode owner below here */
247 if (r1->rm_flags != r2->rm_flags)
249 if (r1->rm_flags & XFS_RMAP_BMBT_BLOCK)
251 return r1->rm_offset + r1->rm_blockcount == r2->rm_offset;
254 /* Flag failures for records that could be merged. */
256 xchk_rmapbt_check_mergeable(
257 struct xchk_btree *bs,
258 struct xchk_rmap *cr,
259 const struct xfs_rmap_irec *irec)
261 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
264 if (xchk_rmap_mergeable(cr, irec))
265 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
267 memcpy(&cr->prev_rec, irec, sizeof(struct xfs_rmap_irec));
270 /* Compare an rmap for AG metadata against the metadata walk. */
272 xchk_rmapbt_mark_bitmap(
273 struct xchk_btree *bs,
274 struct xchk_rmap *cr,
275 const struct xfs_rmap_irec *irec)
277 struct xfs_scrub *sc = bs->sc;
278 struct xagb_bitmap *bmp = NULL;
279 xfs_extlen_t fsbcount = irec->rm_blockcount;
282 * Skip corrupt records. It is essential that we detect records in the
283 * btree that cannot overlap but do, flag those as CORRUPT, and skip
284 * the bitmap comparison to avoid generating false XCORRUPT reports.
286 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
290 * If the AG metadata walk didn't complete, there's no point in
291 * comparing against partial results.
293 if (!cr->bitmaps_complete)
296 switch (irec->rm_owner) {
297 case XFS_RMAP_OWN_FS:
300 case XFS_RMAP_OWN_LOG:
301 bmp = &cr->log_owned;
303 case XFS_RMAP_OWN_AG:
306 case XFS_RMAP_OWN_INOBT:
307 bmp = &cr->inobt_owned;
309 case XFS_RMAP_OWN_REFC:
310 bmp = &cr->refcbt_owned;
317 if (xagb_bitmap_test(bmp, irec->rm_startblock, &fsbcount)) {
319 * The start of this reverse mapping corresponds to a set
320 * region in the bitmap. If the mapping covers more area than
321 * the set region, then it covers space that wasn't found by
322 * the AG metadata walk.
324 if (fsbcount < irec->rm_blockcount)
325 xchk_btree_xref_set_corrupt(bs->sc,
326 bs->sc->sa.rmap_cur, 0);
329 * The start of this reverse mapping does not correspond to a
330 * completely set region in the bitmap. The region wasn't
331 * fully set by walking the AG metadata, so this is a
332 * cross-referencing corruption.
334 xchk_btree_xref_set_corrupt(bs->sc, bs->sc->sa.rmap_cur, 0);
337 /* Unset the region so that we can detect missing rmap records. */
338 return xagb_bitmap_clear(bmp, irec->rm_startblock, irec->rm_blockcount);
341 /* Scrub an rmapbt record. */
344 struct xchk_btree *bs,
345 const union xfs_btree_rec *rec)
347 struct xchk_rmap *cr = bs->private;
348 struct xfs_rmap_irec irec;
350 if (xfs_rmap_btrec_to_irec(rec, &irec) != NULL ||
351 xfs_rmap_check_irec(bs->cur, &irec) != NULL) {
352 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
356 xchk_rmapbt_check_unwritten_in_keyflags(bs);
357 xchk_rmapbt_check_mergeable(bs, cr, &irec);
358 xchk_rmapbt_check_overlapping(bs, cr, &irec);
359 xchk_rmapbt_xref(bs->sc, &irec);
361 return xchk_rmapbt_mark_bitmap(bs, cr, &irec);
364 /* Add an AGFL block to the rmap list. */
366 xchk_rmapbt_walk_agfl(
367 struct xfs_mount *mp,
371 struct xagb_bitmap *bitmap = priv;
373 return xagb_bitmap_set(bitmap, agbno, 1);
377 * Set up bitmaps mapping all the AG metadata to compare with the rmapbt
380 * Grab our own btree cursors here if the scrub setup function didn't give us a
381 * btree cursor due to reports of poor health. We need to find out if the
382 * rmapbt disagrees with primary metadata btrees to tag the rmapbt as being
386 xchk_rmapbt_walk_ag_metadata(
387 struct xfs_scrub *sc,
388 struct xchk_rmap *cr)
390 struct xfs_mount *mp = sc->mp;
391 struct xfs_buf *agfl_bp;
392 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
393 struct xfs_btree_cur *cur;
396 /* OWN_FS: AG headers */
397 error = xagb_bitmap_set(&cr->fs_owned, XFS_SB_BLOCK(mp),
398 XFS_AGFL_BLOCK(mp) - XFS_SB_BLOCK(mp) + 1);
402 /* OWN_LOG: Internal log */
403 if (xfs_ag_contains_log(mp, sc->sa.pag->pag_agno)) {
404 error = xagb_bitmap_set(&cr->log_owned,
405 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart),
406 mp->m_sb.sb_logblocks);
411 /* OWN_AG: bnobt, cntbt, rmapbt, and AGFL */
412 cur = sc->sa.bno_cur;
414 cur = xfs_allocbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
415 sc->sa.pag, XFS_BTNUM_BNO);
416 error = xagb_bitmap_set_btblocks(&cr->ag_owned, cur);
417 if (cur != sc->sa.bno_cur)
418 xfs_btree_del_cursor(cur, error);
422 cur = sc->sa.cnt_cur;
424 cur = xfs_allocbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
425 sc->sa.pag, XFS_BTNUM_CNT);
426 error = xagb_bitmap_set_btblocks(&cr->ag_owned, cur);
427 if (cur != sc->sa.cnt_cur)
428 xfs_btree_del_cursor(cur, error);
432 error = xagb_bitmap_set_btblocks(&cr->ag_owned, sc->sa.rmap_cur);
436 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
440 error = xfs_agfl_walk(sc->mp, agf, agfl_bp, xchk_rmapbt_walk_agfl,
442 xfs_trans_brelse(sc->tp, agfl_bp);
446 /* OWN_INOBT: inobt, finobt */
447 cur = sc->sa.ino_cur;
449 cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, sc->sa.agi_bp,
451 error = xagb_bitmap_set_btblocks(&cr->inobt_owned, cur);
452 if (cur != sc->sa.ino_cur)
453 xfs_btree_del_cursor(cur, error);
457 if (xfs_has_finobt(sc->mp)) {
458 cur = sc->sa.fino_cur;
460 cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp,
461 sc->sa.agi_bp, XFS_BTNUM_FINO);
462 error = xagb_bitmap_set_btblocks(&cr->inobt_owned, cur);
463 if (cur != sc->sa.fino_cur)
464 xfs_btree_del_cursor(cur, error);
469 /* OWN_REFC: refcountbt */
470 if (xfs_has_reflink(sc->mp)) {
471 cur = sc->sa.refc_cur;
473 cur = xfs_refcountbt_init_cursor(sc->mp, sc->tp,
474 sc->sa.agf_bp, sc->sa.pag);
475 error = xagb_bitmap_set_btblocks(&cr->refcbt_owned, cur);
476 if (cur != sc->sa.refc_cur)
477 xfs_btree_del_cursor(cur, error);
484 * If there's an error, set XFAIL and disable the bitmap
485 * cross-referencing checks, but proceed with the scrub anyway.
488 xchk_btree_xref_process_error(sc, sc->sa.rmap_cur,
489 sc->sa.rmap_cur->bc_nlevels - 1, &error);
491 cr->bitmaps_complete = true;
496 * Check for set regions in the bitmaps; if there are any, the rmap records do
497 * not describe all the AG metadata.
500 xchk_rmapbt_check_bitmaps(
501 struct xfs_scrub *sc,
502 struct xchk_rmap *cr)
504 struct xfs_btree_cur *cur = sc->sa.rmap_cur;
507 if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
508 XFS_SCRUB_OFLAG_XFAIL))
512 level = cur->bc_nlevels - 1;
515 * Any bitmap with bits still set indicates that the reverse mapping
516 * doesn't cover the entire primary structure.
518 if (xagb_bitmap_hweight(&cr->fs_owned) != 0)
519 xchk_btree_xref_set_corrupt(sc, cur, level);
521 if (xagb_bitmap_hweight(&cr->log_owned) != 0)
522 xchk_btree_xref_set_corrupt(sc, cur, level);
524 if (xagb_bitmap_hweight(&cr->ag_owned) != 0)
525 xchk_btree_xref_set_corrupt(sc, cur, level);
527 if (xagb_bitmap_hweight(&cr->inobt_owned) != 0)
528 xchk_btree_xref_set_corrupt(sc, cur, level);
530 if (xagb_bitmap_hweight(&cr->refcbt_owned) != 0)
531 xchk_btree_xref_set_corrupt(sc, cur, level);
534 /* Scrub the rmap btree for some AG. */
537 struct xfs_scrub *sc)
539 struct xchk_rmap *cr;
542 cr = kzalloc(sizeof(struct xchk_rmap), XCHK_GFP_FLAGS);
546 xagb_bitmap_init(&cr->fs_owned);
547 xagb_bitmap_init(&cr->log_owned);
548 xagb_bitmap_init(&cr->ag_owned);
549 xagb_bitmap_init(&cr->inobt_owned);
550 xagb_bitmap_init(&cr->refcbt_owned);
552 error = xchk_rmapbt_walk_ag_metadata(sc, cr);
556 error = xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
557 &XFS_RMAP_OINFO_AG, cr);
561 xchk_rmapbt_check_bitmaps(sc, cr);
564 xagb_bitmap_destroy(&cr->refcbt_owned);
565 xagb_bitmap_destroy(&cr->inobt_owned);
566 xagb_bitmap_destroy(&cr->ag_owned);
567 xagb_bitmap_destroy(&cr->log_owned);
568 xagb_bitmap_destroy(&cr->fs_owned);
573 /* xref check that the extent is owned only by a given owner */
575 xchk_xref_is_only_owned_by(
576 struct xfs_scrub *sc,
579 const struct xfs_owner_info *oinfo)
581 struct xfs_rmap_matches res;
584 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
587 error = xfs_rmap_count_owners(sc->sa.rmap_cur, bno, len, oinfo, &res);
588 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
590 if (res.matches != 1)
591 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
592 if (res.bad_non_owner_matches)
593 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
594 if (res.non_owner_matches)
595 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
598 /* xref check that the extent is not owned by a given owner */
600 xchk_xref_is_not_owned_by(
601 struct xfs_scrub *sc,
604 const struct xfs_owner_info *oinfo)
606 struct xfs_rmap_matches res;
609 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
612 error = xfs_rmap_count_owners(sc->sa.rmap_cur, bno, len, oinfo, &res);
613 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
615 if (res.matches != 0)
616 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
617 if (res.bad_non_owner_matches)
618 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
621 /* xref check that the extent has no reverse mapping at all */
623 xchk_xref_has_no_owner(
624 struct xfs_scrub *sc,
628 enum xbtree_recpacking outcome;
631 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
634 error = xfs_rmap_has_records(sc->sa.rmap_cur, bno, len, &outcome);
635 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
637 if (outcome != XBTREE_RECPACKING_EMPTY)
638 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);