1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
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_btree.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_rtbitmap.h"
16 #include "xfs_inode.h"
17 #include "xfs_bmap.h"
18 #include "xfs_bit.h"
19 #include "xfs_rtgroup.h"
20 #include "xfs_sb.h"
21 #include "xfs_rmap.h"
22 #include "xfs_rtrmap_btree.h"
23 #include "xfs_exchmaps.h"
24 #include "scrub/scrub.h"
25 #include "scrub/common.h"
26 #include "scrub/repair.h"
27 #include "scrub/tempexch.h"
28 #include "scrub/rtbitmap.h"
29 #include "scrub/btree.h"
30
31 /* Set us up with the realtime metadata locked. */
32 int
xchk_setup_rtbitmap(struct xfs_scrub * sc)33 xchk_setup_rtbitmap(
34 struct xfs_scrub *sc)
35 {
36 struct xfs_mount *mp = sc->mp;
37 struct xchk_rtbitmap *rtb;
38 int error;
39
40 if (xchk_need_intent_drain(sc))
41 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
42
43 rtb = kzalloc(struct_size(rtb, words, xchk_rtbitmap_wordcnt(sc)),
44 XCHK_GFP_FLAGS);
45 if (!rtb)
46 return -ENOMEM;
47 sc->buf = rtb;
48 rtb->sc = sc;
49
50 error = xchk_rtgroup_init(sc, sc->sm->sm_agno, &sc->sr);
51 if (error)
52 return error;
53
54 if (xchk_could_repair(sc)) {
55 error = xrep_setup_rtbitmap(sc, rtb);
56 if (error)
57 return error;
58 }
59
60 error = xchk_trans_alloc(sc, rtb->resblks);
61 if (error)
62 return error;
63
64 error = xchk_install_live_inode(sc, rtg_bitmap(sc->sr.rtg));
65 if (error)
66 return error;
67
68 error = xchk_ino_dqattach(sc);
69 if (error)
70 return error;
71
72 error = xchk_rtgroup_lock(sc, &sc->sr, XCHK_RTGLOCK_ALL);
73 if (error)
74 return error;
75
76 /*
77 * Now that we've locked the rtbitmap, we can't race with growfsrt
78 * trying to expand the bitmap or change the size of the rt volume.
79 * Hence it is safe to compute and check the geometry values.
80 */
81 if (mp->m_sb.sb_rblocks) {
82 rtb->rextents = xfs_blen_to_rtbxlen(mp, mp->m_sb.sb_rblocks);
83 rtb->rextslog = xfs_compute_rextslog(rtb->rextents);
84 rtb->rbmblocks = xfs_rtbitmap_blockcount(mp);
85 }
86
87 return 0;
88 }
89
90 /* Per-rtgroup bitmap contents. */
91
92 /* Cross-reference rtbitmap entries with other metadata. */
93 STATIC void
xchk_rtbitmap_xref(struct xchk_rtbitmap * rtb,xfs_rtblock_t startblock,xfs_rtblock_t blockcount)94 xchk_rtbitmap_xref(
95 struct xchk_rtbitmap *rtb,
96 xfs_rtblock_t startblock,
97 xfs_rtblock_t blockcount)
98 {
99 struct xfs_scrub *sc = rtb->sc;
100 xfs_rgblock_t rgbno = xfs_rtb_to_rgbno(sc->mp, startblock);
101
102 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
103 return;
104 if (!sc->sr.rmap_cur)
105 return;
106
107 xchk_xref_has_no_rt_owner(sc, rgbno, blockcount);
108 xchk_xref_is_not_rt_shared(sc, rgbno, blockcount);
109 xchk_xref_is_not_rt_cow_staging(sc, rgbno, blockcount);
110
111 if (rtb->next_free_rgbno < rgbno)
112 xchk_xref_has_rt_owner(sc, rtb->next_free_rgbno,
113 rgbno - rtb->next_free_rgbno);
114 rtb->next_free_rgbno = rgbno + blockcount;
115 }
116
117 /* Scrub a free extent record from the realtime bitmap. */
118 STATIC int
xchk_rtbitmap_rec(struct xfs_rtgroup * rtg,struct xfs_trans * tp,const struct xfs_rtalloc_rec * rec,void * priv)119 xchk_rtbitmap_rec(
120 struct xfs_rtgroup *rtg,
121 struct xfs_trans *tp,
122 const struct xfs_rtalloc_rec *rec,
123 void *priv)
124 {
125 struct xchk_rtbitmap *rtb = priv;
126 struct xfs_scrub *sc = rtb->sc;
127 xfs_rtblock_t startblock;
128 xfs_filblks_t blockcount;
129
130 startblock = xfs_rtx_to_rtb(rtg, rec->ar_startext);
131 blockcount = xfs_rtxlen_to_extlen(rtg_mount(rtg), rec->ar_extcount);
132
133 if (!xfs_verify_rtbext(rtg_mount(rtg), startblock, blockcount))
134 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
135
136 xchk_rtbitmap_xref(rtb, startblock, blockcount);
137
138 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
139 return -ECANCELED;
140
141 return 0;
142 }
143
144 /* Make sure the entire rtbitmap file is mapped with written extents. */
145 STATIC int
xchk_rtbitmap_check_extents(struct xfs_scrub * sc)146 xchk_rtbitmap_check_extents(
147 struct xfs_scrub *sc)
148 {
149 struct xfs_bmbt_irec map;
150 struct xfs_iext_cursor icur;
151 struct xfs_mount *mp = sc->mp;
152 struct xfs_inode *ip = sc->ip;
153 xfs_fileoff_t off = 0;
154 xfs_fileoff_t endoff;
155 int error = 0;
156
157 /* Mappings may not cross or lie beyond EOF. */
158 endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
159 if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
160 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
161 return 0;
162 }
163
164 while (off < endoff) {
165 int nmap = 1;
166
167 if (xchk_should_terminate(sc, &error) ||
168 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
169 break;
170
171 /* Make sure we have a written extent. */
172 error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
173 XFS_DATA_FORK);
174 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
175 break;
176
177 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
178 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
179 break;
180 }
181
182 off += map.br_blockcount;
183 }
184
185 return error;
186 }
187
188 /* Scrub this group's realtime bitmap. */
189 int
xchk_rtbitmap(struct xfs_scrub * sc)190 xchk_rtbitmap(
191 struct xfs_scrub *sc)
192 {
193 struct xfs_mount *mp = sc->mp;
194 struct xfs_rtgroup *rtg = sc->sr.rtg;
195 struct xfs_inode *rbmip = rtg_bitmap(rtg);
196 struct xchk_rtbitmap *rtb = sc->buf;
197 xfs_rgblock_t last_rgbno;
198 int error;
199
200 /* Is sb_rextents correct? */
201 if (mp->m_sb.sb_rextents != rtb->rextents) {
202 xchk_ino_set_corrupt(sc, rbmip->i_ino);
203 return 0;
204 }
205
206 /* Is sb_rextslog correct? */
207 if (mp->m_sb.sb_rextslog != rtb->rextslog) {
208 xchk_ino_set_corrupt(sc, rbmip->i_ino);
209 return 0;
210 }
211
212 /*
213 * Is sb_rbmblocks large enough to handle the current rt volume? In no
214 * case can we exceed 4bn bitmap blocks since the super field is a u32.
215 */
216 if (rtb->rbmblocks > U32_MAX) {
217 xchk_ino_set_corrupt(sc, rbmip->i_ino);
218 return 0;
219 }
220 if (mp->m_sb.sb_rbmblocks != rtb->rbmblocks) {
221 xchk_ino_set_corrupt(sc, rbmip->i_ino);
222 return 0;
223 }
224
225 /* The bitmap file length must be aligned to an fsblock. */
226 if (rbmip->i_disk_size & mp->m_blockmask) {
227 xchk_ino_set_corrupt(sc, rbmip->i_ino);
228 return 0;
229 }
230
231 /*
232 * Is the bitmap file itself large enough to handle the rt volume?
233 * growfsrt expands the bitmap file before updating sb_rextents, so the
234 * file can be larger than sb_rbmblocks.
235 */
236 if (rbmip->i_disk_size < XFS_FSB_TO_B(mp, rtb->rbmblocks)) {
237 xchk_ino_set_corrupt(sc, rbmip->i_ino);
238 return 0;
239 }
240
241 /* Invoke the fork scrubber. */
242 error = xchk_metadata_inode_forks(sc);
243 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
244 return error;
245
246 error = xchk_rtbitmap_check_extents(sc);
247 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
248 return error;
249
250 rtb->next_free_rgbno = 0;
251 error = xfs_rtalloc_query_all(rtg, sc->tp, xchk_rtbitmap_rec, rtb);
252 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
253 return error;
254
255 /*
256 * Check that the are rmappings for all rt extents between the end of
257 * the last free extent we saw and the last possible extent in the rt
258 * group.
259 */
260 last_rgbno = rtg->rtg_extents * mp->m_sb.sb_rextsize - 1;
261 if (rtb->next_free_rgbno < last_rgbno)
262 xchk_xref_has_rt_owner(sc, rtb->next_free_rgbno,
263 last_rgbno - rtb->next_free_rgbno);
264 return 0;
265 }
266
267 /* xref check that the extent is not free in the rtbitmap */
268 void
xchk_xref_is_used_rt_space(struct xfs_scrub * sc,xfs_rtblock_t rtbno,xfs_extlen_t len)269 xchk_xref_is_used_rt_space(
270 struct xfs_scrub *sc,
271 xfs_rtblock_t rtbno,
272 xfs_extlen_t len)
273 {
274 struct xfs_rtgroup *rtg = sc->sr.rtg;
275 struct xfs_inode *rbmip = rtg_bitmap(rtg);
276 xfs_rtxnum_t startext;
277 xfs_rtxnum_t endext;
278 bool is_free;
279 int error;
280
281 if (xchk_skip_xref(sc->sm))
282 return;
283
284 startext = xfs_rtb_to_rtx(sc->mp, rtbno);
285 endext = xfs_rtb_to_rtx(sc->mp, rtbno + len - 1);
286 error = xfs_rtalloc_extent_is_free(rtg, sc->tp, startext,
287 endext - startext + 1, &is_free);
288 if (!xchk_should_check_xref(sc, &error, NULL))
289 return;
290 if (is_free)
291 xchk_ino_xref_set_corrupt(sc, rbmip->i_ino);
292 }
293