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"
19 #include "xfs_ialloc.h"
21 #include "xfs_refcount.h"
22 #include "scrub/xfs_scrub.h"
23 #include "scrub/scrub.h"
24 #include "scrub/common.h"
25 #include "scrub/btree.h"
26 #include "scrub/trace.h"
29 * Set us up to scrub reverse mapping btrees.
36 return xchk_setup_ag_btree(sc, ip, false);
39 /* Reverse-mapping scrubber. */
41 /* Cross-reference a rmap against the refcount btree. */
43 xchk_rmapbt_xref_refc(
45 struct xfs_rmap_irec *irec)
55 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
58 non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
59 is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
60 is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
61 is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
63 /* If this is shared, must be a data fork extent. */
64 error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
65 irec->rm_blockcount, &fbno, &flen, false);
66 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
68 if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
69 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
72 /* Cross-reference with the other btrees. */
76 struct xfs_rmap_irec *irec)
78 xfs_agblock_t agbno = irec->rm_startblock;
79 xfs_extlen_t len = irec->rm_blockcount;
81 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
84 xchk_xref_is_used_space(sc, agbno, len);
85 if (irec->rm_owner == XFS_RMAP_OWN_INODES)
86 xchk_xref_is_inode_chunk(sc, agbno, len);
88 xchk_xref_is_not_inode_chunk(sc, agbno, len);
89 if (irec->rm_owner == XFS_RMAP_OWN_COW)
90 xchk_xref_is_cow_staging(sc, irec->rm_startblock,
93 xchk_rmapbt_xref_refc(sc, irec);
96 /* Scrub an rmapbt record. */
99 struct xchk_btree *bs,
100 union xfs_btree_rec *rec)
102 struct xfs_mount *mp = bs->cur->bc_mp;
103 struct xfs_rmap_irec irec;
104 xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
111 error = xfs_rmap_btrec_to_irec(rec, &irec);
112 if (!xchk_btree_process_error(bs->sc, bs->cur, 0, &error))
116 if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
117 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
119 if (irec.rm_owner == XFS_RMAP_OWN_FS) {
121 * xfs_verify_agbno returns false for static fs metadata.
122 * Since that only exists at the start of the AG, validate
125 if (irec.rm_startblock != 0 ||
126 irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
127 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
130 * Otherwise we must point somewhere past the static metadata
131 * but before the end of the FS. Run the regular check.
133 if (!xfs_verify_agbno(mp, agno, irec.rm_startblock) ||
134 !xfs_verify_agbno(mp, agno, irec.rm_startblock +
135 irec.rm_blockcount - 1))
136 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
140 non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
141 is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
142 is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
143 is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
145 if (is_bmbt && irec.rm_offset != 0)
146 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
148 if (non_inode && irec.rm_offset != 0)
149 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
151 if (is_unwritten && (is_bmbt || non_inode || is_attr))
152 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
154 if (non_inode && (is_bmbt || is_unwritten || is_attr))
155 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
158 if (!xfs_verify_ino(mp, irec.rm_owner))
159 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
161 /* Non-inode owner within the magic values? */
162 if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
163 irec.rm_owner > XFS_RMAP_OWN_FS)
164 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
167 xchk_rmapbt_xref(bs->sc, &irec);
172 /* Scrub the rmap btree for some AG. */
175 struct xfs_scrub *sc)
177 return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
178 &XFS_RMAP_OINFO_AG, NULL);
181 /* xref check that the extent is owned by a given owner */
183 xchk_xref_check_owner(
184 struct xfs_scrub *sc,
187 const struct xfs_owner_info *oinfo,
188 bool should_have_rmap)
193 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
196 error = xfs_rmap_record_exists(sc->sa.rmap_cur, bno, len, oinfo,
198 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
200 if (has_rmap != should_have_rmap)
201 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
204 /* xref check that the extent is owned by a given owner */
206 xchk_xref_is_owned_by(
207 struct xfs_scrub *sc,
210 const struct xfs_owner_info *oinfo)
212 xchk_xref_check_owner(sc, bno, len, oinfo, true);
215 /* xref check that the extent is not owned by a given owner */
217 xchk_xref_is_not_owned_by(
218 struct xfs_scrub *sc,
221 const struct xfs_owner_info *oinfo)
223 xchk_xref_check_owner(sc, bno, len, oinfo, false);
226 /* xref check that the extent has no reverse mapping at all */
228 xchk_xref_has_no_owner(
229 struct xfs_scrub *sc,
236 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
239 error = xfs_rmap_has_record(sc->sa.rmap_cur, bno, len, &has_rmap);
240 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
243 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);