1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
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"
14 #include "xfs_bit.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
17 #include "xfs_sb.h"
18 #include "xfs_alloc.h"
19 #include "xfs_ialloc.h"
20 #include "xfs_rmap.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"
27
28 /*
29 * Set us up to scrub reverse mapping btrees.
30 */
31 int
xchk_setup_ag_rmapbt(struct xfs_scrub * sc,struct xfs_inode * ip)32 xchk_setup_ag_rmapbt(
33 struct xfs_scrub *sc,
34 struct xfs_inode *ip)
35 {
36 return xchk_setup_ag_btree(sc, ip, false);
37 }
38
39 /* Reverse-mapping scrubber. */
40
41 /* Cross-reference a rmap against the refcount btree. */
42 STATIC void
xchk_rmapbt_xref_refc(struct xfs_scrub * sc,struct xfs_rmap_irec * irec)43 xchk_rmapbt_xref_refc(
44 struct xfs_scrub *sc,
45 struct xfs_rmap_irec *irec)
46 {
47 xfs_agblock_t fbno;
48 xfs_extlen_t flen;
49 bool non_inode;
50 bool is_bmbt;
51 bool is_attr;
52 bool is_unwritten;
53 int error;
54
55 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
56 return;
57
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;
62
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))
67 return;
68 if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
69 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
70 }
71
72 /* Cross-reference with the other btrees. */
73 STATIC void
xchk_rmapbt_xref(struct xfs_scrub * sc,struct xfs_rmap_irec * irec)74 xchk_rmapbt_xref(
75 struct xfs_scrub *sc,
76 struct xfs_rmap_irec *irec)
77 {
78 xfs_agblock_t agbno = irec->rm_startblock;
79 xfs_extlen_t len = irec->rm_blockcount;
80
81 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
82 return;
83
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);
87 else
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,
91 irec->rm_blockcount);
92 else
93 xchk_rmapbt_xref_refc(sc, irec);
94 }
95
96 /* Scrub an rmapbt record. */
97 STATIC int
xchk_rmapbt_rec(struct xchk_btree * bs,union xfs_btree_rec * rec)98 xchk_rmapbt_rec(
99 struct xchk_btree *bs,
100 union xfs_btree_rec *rec)
101 {
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;
105 bool non_inode;
106 bool is_unwritten;
107 bool is_bmbt;
108 bool is_attr;
109 int error;
110
111 error = xfs_rmap_btrec_to_irec(rec, &irec);
112 if (!xchk_btree_process_error(bs->sc, bs->cur, 0, &error))
113 goto out;
114
115 /* Check extent. */
116 if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
117 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
118
119 if (irec.rm_owner == XFS_RMAP_OWN_FS) {
120 /*
121 * xfs_verify_agbno returns false for static fs metadata.
122 * Since that only exists at the start of the AG, validate
123 * that by hand.
124 */
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);
128 } else {
129 /*
130 * Otherwise we must point somewhere past the static metadata
131 * but before the end of the FS. Run the regular check.
132 */
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);
137 }
138
139 /* Check flags. */
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;
144
145 if (is_bmbt && irec.rm_offset != 0)
146 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
147
148 if (non_inode && irec.rm_offset != 0)
149 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
150
151 if (is_unwritten && (is_bmbt || non_inode || is_attr))
152 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
153
154 if (non_inode && (is_bmbt || is_unwritten || is_attr))
155 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
156
157 if (!non_inode) {
158 if (!xfs_verify_ino(mp, irec.rm_owner))
159 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
160 } else {
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);
165 }
166
167 xchk_rmapbt_xref(bs->sc, &irec);
168 out:
169 return error;
170 }
171
172 /* Scrub the rmap btree for some AG. */
173 int
xchk_rmapbt(struct xfs_scrub * sc)174 xchk_rmapbt(
175 struct xfs_scrub *sc)
176 {
177 struct xfs_owner_info oinfo;
178
179 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
180 return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
181 &oinfo, NULL);
182 }
183
184 /* xref check that the extent is owned by a given owner */
185 static inline void
xchk_xref_check_owner(struct xfs_scrub * sc,xfs_agblock_t bno,xfs_extlen_t len,struct xfs_owner_info * oinfo,bool should_have_rmap)186 xchk_xref_check_owner(
187 struct xfs_scrub *sc,
188 xfs_agblock_t bno,
189 xfs_extlen_t len,
190 struct xfs_owner_info *oinfo,
191 bool should_have_rmap)
192 {
193 bool has_rmap;
194 int error;
195
196 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
197 return;
198
199 error = xfs_rmap_record_exists(sc->sa.rmap_cur, bno, len, oinfo,
200 &has_rmap);
201 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
202 return;
203 if (has_rmap != should_have_rmap)
204 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
205 }
206
207 /* xref check that the extent is owned by a given owner */
208 void
xchk_xref_is_owned_by(struct xfs_scrub * sc,xfs_agblock_t bno,xfs_extlen_t len,struct xfs_owner_info * oinfo)209 xchk_xref_is_owned_by(
210 struct xfs_scrub *sc,
211 xfs_agblock_t bno,
212 xfs_extlen_t len,
213 struct xfs_owner_info *oinfo)
214 {
215 xchk_xref_check_owner(sc, bno, len, oinfo, true);
216 }
217
218 /* xref check that the extent is not owned by a given owner */
219 void
xchk_xref_is_not_owned_by(struct xfs_scrub * sc,xfs_agblock_t bno,xfs_extlen_t len,struct xfs_owner_info * oinfo)220 xchk_xref_is_not_owned_by(
221 struct xfs_scrub *sc,
222 xfs_agblock_t bno,
223 xfs_extlen_t len,
224 struct xfs_owner_info *oinfo)
225 {
226 xchk_xref_check_owner(sc, bno, len, oinfo, false);
227 }
228
229 /* xref check that the extent has no reverse mapping at all */
230 void
xchk_xref_has_no_owner(struct xfs_scrub * sc,xfs_agblock_t bno,xfs_extlen_t len)231 xchk_xref_has_no_owner(
232 struct xfs_scrub *sc,
233 xfs_agblock_t bno,
234 xfs_extlen_t len)
235 {
236 bool has_rmap;
237 int error;
238
239 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
240 return;
241
242 error = xfs_rmap_has_record(sc->sa.rmap_cur, bno, len, &has_rmap);
243 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
244 return;
245 if (has_rmap)
246 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
247 }
248