1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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"
15 #include "scrub/scrub.h"
16 #include "scrub/common.h"
17 #include "scrub/btree.h"
20 * Set us up to scrub reverse mapping btrees.
26 return xchk_setup_ag_btree(sc, false);
29 /* Reverse-mapping scrubber. */
31 /* Cross-reference a rmap against the refcount btree. */
33 xchk_rmapbt_xref_refc(
35 struct xfs_rmap_irec *irec)
45 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
48 non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
49 is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
50 is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
51 is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
53 /* If this is shared, must be a data fork extent. */
54 error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
55 irec->rm_blockcount, &fbno, &flen, false);
56 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
58 if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
59 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
62 /* Cross-reference with the other btrees. */
66 struct xfs_rmap_irec *irec)
68 xfs_agblock_t agbno = irec->rm_startblock;
69 xfs_extlen_t len = irec->rm_blockcount;
71 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
74 xchk_xref_is_used_space(sc, agbno, len);
75 if (irec->rm_owner == XFS_RMAP_OWN_INODES)
76 xchk_xref_is_inode_chunk(sc, agbno, len);
78 xchk_xref_is_not_inode_chunk(sc, agbno, len);
79 if (irec->rm_owner == XFS_RMAP_OWN_COW)
80 xchk_xref_is_cow_staging(sc, irec->rm_startblock,
83 xchk_rmapbt_xref_refc(sc, irec);
86 /* Scrub an rmapbt record. */
89 struct xchk_btree *bs,
90 union xfs_btree_rec *rec)
92 struct xfs_mount *mp = bs->cur->bc_mp;
93 struct xfs_rmap_irec irec;
94 xfs_agnumber_t agno = bs->cur->bc_ag.agno;
101 error = xfs_rmap_btrec_to_irec(rec, &irec);
102 if (!xchk_btree_process_error(bs->sc, bs->cur, 0, &error))
106 if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
107 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
109 if (irec.rm_owner == XFS_RMAP_OWN_FS) {
111 * xfs_verify_agbno returns false for static fs metadata.
112 * Since that only exists at the start of the AG, validate
115 if (irec.rm_startblock != 0 ||
116 irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
117 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
120 * Otherwise we must point somewhere past the static metadata
121 * but before the end of the FS. Run the regular check.
123 if (!xfs_verify_agbno(mp, agno, irec.rm_startblock) ||
124 !xfs_verify_agbno(mp, agno, irec.rm_startblock +
125 irec.rm_blockcount - 1))
126 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
130 non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
131 is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
132 is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
133 is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
135 if (is_bmbt && irec.rm_offset != 0)
136 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
138 if (non_inode && irec.rm_offset != 0)
139 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
141 if (is_unwritten && (is_bmbt || non_inode || is_attr))
142 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
144 if (non_inode && (is_bmbt || is_unwritten || is_attr))
145 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
148 if (!xfs_verify_ino(mp, irec.rm_owner))
149 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
151 /* Non-inode owner within the magic values? */
152 if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
153 irec.rm_owner > XFS_RMAP_OWN_FS)
154 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
157 xchk_rmapbt_xref(bs->sc, &irec);
162 /* Scrub the rmap btree for some AG. */
165 struct xfs_scrub *sc)
167 return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
168 &XFS_RMAP_OINFO_AG, NULL);
171 /* xref check that the extent is owned by a given owner */
173 xchk_xref_check_owner(
174 struct xfs_scrub *sc,
177 const struct xfs_owner_info *oinfo,
178 bool should_have_rmap)
183 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
186 error = xfs_rmap_record_exists(sc->sa.rmap_cur, bno, len, oinfo,
188 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
190 if (has_rmap != should_have_rmap)
191 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
194 /* xref check that the extent is owned by a given owner */
196 xchk_xref_is_owned_by(
197 struct xfs_scrub *sc,
200 const struct xfs_owner_info *oinfo)
202 xchk_xref_check_owner(sc, bno, len, oinfo, true);
205 /* xref check that the extent is not owned by a given owner */
207 xchk_xref_is_not_owned_by(
208 struct xfs_scrub *sc,
211 const struct xfs_owner_info *oinfo)
213 xchk_xref_check_owner(sc, bno, len, oinfo, false);
216 /* xref check that the extent has no reverse mapping at all */
218 xchk_xref_has_no_owner(
219 struct xfs_scrub *sc,
226 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
229 error = xfs_rmap_has_record(sc->sa.rmap_cur, bno, len, &has_rmap);
230 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
233 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);