1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 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_defer.h"
13 #include "xfs_btree.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
18 #include "xfs_alloc.h"
20 #include "scrub/xfs_scrub.h"
21 #include "scrub/scrub.h"
22 #include "scrub/common.h"
23 #include "scrub/btree.h"
24 #include "scrub/trace.h"
27 * Set us up to scrub free space btrees.
30 xchk_setup_ag_allocbt(
34 return xchk_setup_ag_btree(sc, ip, false);
37 /* Free space btree scrubber. */
39 * Ensure there's a corresponding cntbt/bnobt record matching this
40 * bnobt/cntbt record, respectively.
43 xchk_allocbt_xref_other(
48 struct xfs_btree_cur **pcur;
54 if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
55 pcur = &sc->sa.cnt_cur;
57 pcur = &sc->sa.bno_cur;
58 if (!*pcur || xchk_skip_xref(sc->sm))
61 error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
62 if (!xchk_should_check_xref(sc, &error, pcur))
65 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
69 error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &has_otherrec);
70 if (!xchk_should_check_xref(sc, &error, pcur))
73 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
77 if (fbno != agbno || flen != len)
78 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
81 /* Cross-reference with the other btrees. */
88 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
91 xchk_allocbt_xref_other(sc, agbno, len);
92 xchk_xref_is_not_inode_chunk(sc, agbno, len);
93 xchk_xref_has_no_owner(sc, agbno, len);
94 xchk_xref_is_not_shared(sc, agbno, len);
97 /* Scrub a bnobt/cntbt record. */
100 struct xchk_btree *bs,
101 union xfs_btree_rec *rec)
103 struct xfs_mount *mp = bs->cur->bc_mp;
104 xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
109 bno = be32_to_cpu(rec->alloc.ar_startblock);
110 len = be32_to_cpu(rec->alloc.ar_blockcount);
112 if (bno + len <= bno ||
113 !xfs_verify_agbno(mp, agno, bno) ||
114 !xfs_verify_agbno(mp, agno, bno + len - 1))
115 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
117 xchk_allocbt_xref(bs->sc, bno, len);
122 /* Scrub the freespace btrees for some AG. */
125 struct xfs_scrub *sc,
128 struct xfs_btree_cur *cur;
130 cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
131 return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, NULL);
136 struct xfs_scrub *sc)
138 return xchk_allocbt(sc, XFS_BTNUM_BNO);
143 struct xfs_scrub *sc)
145 return xchk_allocbt(sc, XFS_BTNUM_CNT);
148 /* xref check that the extent is not free */
150 xchk_xref_is_used_space(
151 struct xfs_scrub *sc,
158 if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
161 error = xfs_alloc_has_record(sc->sa.bno_cur, agbno, len, &is_freesp);
162 if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
165 xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);