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_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_btree.h"
14 #include "xfs_refcount.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
19 #include "scrub/btree.h"
20 #include "scrub/bitmap.h"
23 * Set us up to scrub reverse mapping btrees.
29 if (xchk_need_intent_drain(sc))
30 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
32 return xchk_setup_ag_btree(sc, false);
35 /* Reverse-mapping scrubber. */
39 * The furthest-reaching of the rmapbt records that we've already
40 * processed. This enables us to detect overlapping records for space
41 * allocations that cannot be shared.
43 struct xfs_rmap_irec overlap_rec;
46 * The previous rmapbt record, so that we can check for two records
49 struct xfs_rmap_irec prev_rec;
51 /* Bitmaps containing all blocks for each type of AG metadata. */
52 struct xagb_bitmap fs_owned;
53 struct xagb_bitmap log_owned;
55 /* Did we complete the AG space metadata bitmaps? */
56 bool bitmaps_complete;
59 /* Cross-reference a rmap against the refcount btree. */
61 xchk_rmapbt_xref_refc(
63 struct xfs_rmap_irec *irec)
73 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
76 non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
77 is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
78 is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
79 is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
81 /* If this is shared, must be a data fork extent. */
82 error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
83 irec->rm_blockcount, &fbno, &flen, false);
84 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
86 if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
87 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
90 /* Cross-reference with the other btrees. */
94 struct xfs_rmap_irec *irec)
96 xfs_agblock_t agbno = irec->rm_startblock;
97 xfs_extlen_t len = irec->rm_blockcount;
99 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
102 xchk_xref_is_used_space(sc, agbno, len);
103 if (irec->rm_owner == XFS_RMAP_OWN_INODES)
104 xchk_xref_is_inode_chunk(sc, agbno, len);
106 xchk_xref_is_not_inode_chunk(sc, agbno, len);
107 if (irec->rm_owner == XFS_RMAP_OWN_COW)
108 xchk_xref_is_cow_staging(sc, irec->rm_startblock,
109 irec->rm_blockcount);
111 xchk_rmapbt_xref_refc(sc, irec);
115 * Check for bogus UNWRITTEN flags in the rmapbt node block keys.
117 * In reverse mapping records, the file mapping extent state
118 * (XFS_RMAP_OFF_UNWRITTEN) is a record attribute, not a key field. It is not
119 * involved in lookups in any way. In older kernels, the functions that
120 * convert rmapbt records to keys forgot to filter out the extent state bit,
121 * even though the key comparison functions have filtered the flag correctly.
122 * If we spot an rmap key with the unwritten bit set in rm_offset, we should
123 * mark the btree as needing optimization to rebuild the btree without those
127 xchk_rmapbt_check_unwritten_in_keyflags(
128 struct xchk_btree *bs)
130 struct xfs_scrub *sc = bs->sc;
131 struct xfs_btree_cur *cur = bs->cur;
132 struct xfs_btree_block *keyblock;
133 union xfs_btree_key *lkey, *hkey;
134 __be64 badflag = cpu_to_be64(XFS_RMAP_OFF_UNWRITTEN);
137 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_PREEN)
140 for (level = 1; level < cur->bc_nlevels; level++) {
144 /* Only check the first time we've seen this node block. */
145 if (cur->bc_levels[level].ptr > 1)
148 keyblock = xfs_btree_get_block(cur, level, &bp);
149 for (ptr = 1; ptr <= be16_to_cpu(keyblock->bb_numrecs); ptr++) {
150 lkey = xfs_btree_key_addr(cur, ptr, keyblock);
152 if (lkey->rmap.rm_offset & badflag) {
153 xchk_btree_set_preen(sc, cur, level);
157 hkey = xfs_btree_high_key_addr(cur, ptr, keyblock);
158 if (hkey->rmap.rm_offset & badflag) {
159 xchk_btree_set_preen(sc, cur, level);
167 xchk_rmapbt_is_shareable(
168 struct xfs_scrub *sc,
169 const struct xfs_rmap_irec *irec)
171 if (!xfs_has_reflink(sc->mp))
173 if (XFS_RMAP_NON_INODE_OWNER(irec->rm_owner))
175 if (irec->rm_flags & (XFS_RMAP_BMBT_BLOCK | XFS_RMAP_ATTR_FORK |
181 /* Flag failures for records that overlap but cannot. */
183 xchk_rmapbt_check_overlapping(
184 struct xchk_btree *bs,
185 struct xchk_rmap *cr,
186 const struct xfs_rmap_irec *irec)
188 xfs_agblock_t pnext, inext;
190 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
193 /* No previous record? */
194 if (cr->overlap_rec.rm_blockcount == 0)
197 /* Do overlap_rec and irec overlap? */
198 pnext = cr->overlap_rec.rm_startblock + cr->overlap_rec.rm_blockcount;
199 if (pnext <= irec->rm_startblock)
202 /* Overlap is only allowed if both records are data fork mappings. */
203 if (!xchk_rmapbt_is_shareable(bs->sc, &cr->overlap_rec) ||
204 !xchk_rmapbt_is_shareable(bs->sc, irec))
205 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
207 /* Save whichever rmap record extends furthest. */
208 inext = irec->rm_startblock + irec->rm_blockcount;
213 memcpy(&cr->overlap_rec, irec, sizeof(struct xfs_rmap_irec));
216 /* Decide if two reverse-mapping records can be merged. */
219 struct xchk_rmap *cr,
220 const struct xfs_rmap_irec *r2)
222 const struct xfs_rmap_irec *r1 = &cr->prev_rec;
224 /* Ignore if prev_rec is not yet initialized. */
225 if (cr->prev_rec.rm_blockcount == 0)
228 if (r1->rm_owner != r2->rm_owner)
230 if (r1->rm_startblock + r1->rm_blockcount != r2->rm_startblock)
232 if ((unsigned long long)r1->rm_blockcount + r2->rm_blockcount >
235 if (XFS_RMAP_NON_INODE_OWNER(r2->rm_owner))
237 /* must be an inode owner below here */
238 if (r1->rm_flags != r2->rm_flags)
240 if (r1->rm_flags & XFS_RMAP_BMBT_BLOCK)
242 return r1->rm_offset + r1->rm_blockcount == r2->rm_offset;
245 /* Flag failures for records that could be merged. */
247 xchk_rmapbt_check_mergeable(
248 struct xchk_btree *bs,
249 struct xchk_rmap *cr,
250 const struct xfs_rmap_irec *irec)
252 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
255 if (xchk_rmap_mergeable(cr, irec))
256 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
258 memcpy(&cr->prev_rec, irec, sizeof(struct xfs_rmap_irec));
261 /* Compare an rmap for AG metadata against the metadata walk. */
263 xchk_rmapbt_mark_bitmap(
264 struct xchk_btree *bs,
265 struct xchk_rmap *cr,
266 const struct xfs_rmap_irec *irec)
268 struct xfs_scrub *sc = bs->sc;
269 struct xagb_bitmap *bmp = NULL;
270 xfs_extlen_t fsbcount = irec->rm_blockcount;
273 * Skip corrupt records. It is essential that we detect records in the
274 * btree that cannot overlap but do, flag those as CORRUPT, and skip
275 * the bitmap comparison to avoid generating false XCORRUPT reports.
277 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
281 * If the AG metadata walk didn't complete, there's no point in
282 * comparing against partial results.
284 if (!cr->bitmaps_complete)
287 switch (irec->rm_owner) {
288 case XFS_RMAP_OWN_FS:
291 case XFS_RMAP_OWN_LOG:
292 bmp = &cr->log_owned;
299 if (xagb_bitmap_test(bmp, irec->rm_startblock, &fsbcount)) {
301 * The start of this reverse mapping corresponds to a set
302 * region in the bitmap. If the mapping covers more area than
303 * the set region, then it covers space that wasn't found by
304 * the AG metadata walk.
306 if (fsbcount < irec->rm_blockcount)
307 xchk_btree_xref_set_corrupt(bs->sc,
308 bs->sc->sa.rmap_cur, 0);
311 * The start of this reverse mapping does not correspond to a
312 * completely set region in the bitmap. The region wasn't
313 * fully set by walking the AG metadata, so this is a
314 * cross-referencing corruption.
316 xchk_btree_xref_set_corrupt(bs->sc, bs->sc->sa.rmap_cur, 0);
319 /* Unset the region so that we can detect missing rmap records. */
320 return xagb_bitmap_clear(bmp, irec->rm_startblock, irec->rm_blockcount);
323 /* Scrub an rmapbt record. */
326 struct xchk_btree *bs,
327 const union xfs_btree_rec *rec)
329 struct xchk_rmap *cr = bs->private;
330 struct xfs_rmap_irec irec;
332 if (xfs_rmap_btrec_to_irec(rec, &irec) != NULL ||
333 xfs_rmap_check_irec(bs->cur, &irec) != NULL) {
334 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
338 xchk_rmapbt_check_unwritten_in_keyflags(bs);
339 xchk_rmapbt_check_mergeable(bs, cr, &irec);
340 xchk_rmapbt_check_overlapping(bs, cr, &irec);
341 xchk_rmapbt_xref(bs->sc, &irec);
343 return xchk_rmapbt_mark_bitmap(bs, cr, &irec);
347 * Set up bitmaps mapping all the AG metadata to compare with the rmapbt
351 xchk_rmapbt_walk_ag_metadata(
352 struct xfs_scrub *sc,
353 struct xchk_rmap *cr)
355 struct xfs_mount *mp = sc->mp;
358 /* OWN_FS: AG headers */
359 error = xagb_bitmap_set(&cr->fs_owned, XFS_SB_BLOCK(mp),
360 XFS_AGFL_BLOCK(mp) - XFS_SB_BLOCK(mp) + 1);
364 /* OWN_LOG: Internal log */
365 if (xfs_ag_contains_log(mp, sc->sa.pag->pag_agno)) {
366 error = xagb_bitmap_set(&cr->log_owned,
367 XFS_FSB_TO_AGBNO(mp, mp->m_sb.sb_logstart),
368 mp->m_sb.sb_logblocks);
375 * If there's an error, set XFAIL and disable the bitmap
376 * cross-referencing checks, but proceed with the scrub anyway.
379 xchk_btree_xref_process_error(sc, sc->sa.rmap_cur,
380 sc->sa.rmap_cur->bc_nlevels - 1, &error);
382 cr->bitmaps_complete = true;
387 * Check for set regions in the bitmaps; if there are any, the rmap records do
388 * not describe all the AG metadata.
391 xchk_rmapbt_check_bitmaps(
392 struct xfs_scrub *sc,
393 struct xchk_rmap *cr)
395 struct xfs_btree_cur *cur = sc->sa.rmap_cur;
398 if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
399 XFS_SCRUB_OFLAG_XFAIL))
403 level = cur->bc_nlevels - 1;
406 * Any bitmap with bits still set indicates that the reverse mapping
407 * doesn't cover the entire primary structure.
409 if (xagb_bitmap_hweight(&cr->fs_owned) != 0)
410 xchk_btree_xref_set_corrupt(sc, cur, level);
412 if (xagb_bitmap_hweight(&cr->log_owned) != 0)
413 xchk_btree_xref_set_corrupt(sc, cur, level);
416 /* Scrub the rmap btree for some AG. */
419 struct xfs_scrub *sc)
421 struct xchk_rmap *cr;
424 cr = kzalloc(sizeof(struct xchk_rmap), XCHK_GFP_FLAGS);
428 xagb_bitmap_init(&cr->fs_owned);
429 xagb_bitmap_init(&cr->log_owned);
431 error = xchk_rmapbt_walk_ag_metadata(sc, cr);
435 error = xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
436 &XFS_RMAP_OINFO_AG, cr);
440 xchk_rmapbt_check_bitmaps(sc, cr);
443 xagb_bitmap_destroy(&cr->log_owned);
444 xagb_bitmap_destroy(&cr->fs_owned);
449 /* xref check that the extent is owned only by a given owner */
451 xchk_xref_is_only_owned_by(
452 struct xfs_scrub *sc,
455 const struct xfs_owner_info *oinfo)
457 struct xfs_rmap_matches res;
460 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
463 error = xfs_rmap_count_owners(sc->sa.rmap_cur, bno, len, oinfo, &res);
464 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
466 if (res.matches != 1)
467 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
468 if (res.bad_non_owner_matches)
469 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
470 if (res.non_owner_matches)
471 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
474 /* xref check that the extent is not owned by a given owner */
476 xchk_xref_is_not_owned_by(
477 struct xfs_scrub *sc,
480 const struct xfs_owner_info *oinfo)
482 struct xfs_rmap_matches res;
485 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
488 error = xfs_rmap_count_owners(sc->sa.rmap_cur, bno, len, oinfo, &res);
489 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
491 if (res.matches != 0)
492 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
493 if (res.bad_non_owner_matches)
494 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
497 /* xref check that the extent has no reverse mapping at all */
499 xchk_xref_has_no_owner(
500 struct xfs_scrub *sc,
504 enum xbtree_recpacking outcome;
507 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
510 error = xfs_rmap_has_records(sc->sa.rmap_cur, bno, len, &outcome);
511 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
513 if (outcome != XBTREE_RECPACKING_EMPTY)
514 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);