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_inode.h"
14 #include "xfs_log_format.h"
15 #include "xfs_trans.h"
16 #include "xfs_rtbitmap.h"
17 #include "xfs_bit.h"
18 #include "xfs_bmap.h"
19 #include "xfs_sb.h"
20 #include "xfs_exchmaps.h"
21 #include "xfs_rtgroup.h"
22 #include "scrub/scrub.h"
23 #include "scrub/common.h"
24 #include "scrub/trace.h"
25 #include "scrub/xfile.h"
26 #include "scrub/repair.h"
27 #include "scrub/tempexch.h"
28 #include "scrub/rtsummary.h"
29
30 /*
31 * Realtime Summary
32 * ================
33 *
34 * We check the realtime summary by scanning the realtime bitmap file to create
35 * a new summary file incore, and then we compare the computed version against
36 * the ondisk version. We use the 'xfile' functionality to store this
37 * (potentially large) amount of data in pageable memory.
38 */
39
40 /* Set us up to check the rtsummary file. */
41 int
xchk_setup_rtsummary(struct xfs_scrub * sc)42 xchk_setup_rtsummary(
43 struct xfs_scrub *sc)
44 {
45 struct xfs_mount *mp = sc->mp;
46 char *descr;
47 struct xchk_rtsummary *rts;
48 int error;
49
50 if (xchk_need_intent_drain(sc))
51 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
52
53 rts = kvzalloc(struct_size(rts, words, mp->m_blockwsize),
54 XCHK_GFP_FLAGS);
55 if (!rts)
56 return -ENOMEM;
57 sc->buf = rts;
58
59 error = xchk_rtgroup_init(sc, sc->sm->sm_agno, &sc->sr);
60 if (error)
61 return error;
62
63 if (xchk_could_repair(sc)) {
64 error = xrep_setup_rtsummary(sc, rts);
65 if (error)
66 return error;
67 }
68
69 /*
70 * Create an xfile to construct a new rtsummary file. The xfile allows
71 * us to avoid pinning kernel memory for this purpose.
72 */
73 descr = xchk_xfile_descr(sc, "realtime summary file");
74 error = xfile_create(descr, XFS_FSB_TO_B(mp, mp->m_rsumblocks),
75 &sc->xfile);
76 kfree(descr);
77 if (error)
78 return error;
79
80 error = xchk_trans_alloc(sc, rts->resblks);
81 if (error)
82 return error;
83
84 error = xchk_install_live_inode(sc, rtg_summary(sc->sr.rtg));
85 if (error)
86 return error;
87
88 error = xchk_ino_dqattach(sc);
89 if (error)
90 return error;
91
92 error = xchk_rtgroup_lock(sc, &sc->sr, XFS_RTGLOCK_BITMAP);
93 if (error)
94 return error;
95
96 /*
97 * Now that we've locked the rtbitmap and rtsummary, we can't race with
98 * growfsrt trying to expand the summary or change the size of the rt
99 * volume. Hence it is safe to compute and check the geometry values.
100 *
101 * Note that there is no strict requirement for an exclusive lock on the
102 * summary here, but to keep the locking APIs simple we lock both inodes
103 * exclusively here. If we ever start caring about running concurrent
104 * fsmap with scrub this could be changed.
105 */
106 if (mp->m_sb.sb_rblocks) {
107 rts->rextents = xfs_blen_to_rtbxlen(mp, mp->m_sb.sb_rblocks);
108 rts->rbmblocks = xfs_rtbitmap_blockcount(mp);
109 rts->rsumblocks =
110 xfs_rtsummary_blockcount(mp, &rts->rsumlevels);
111 }
112
113 return 0;
114 }
115
116 /* Helper functions to record suminfo words in an xfile. */
117
118 static inline int
xfsum_load(struct xfs_scrub * sc,xfs_rtsumoff_t sumoff,union xfs_suminfo_raw * rawinfo)119 xfsum_load(
120 struct xfs_scrub *sc,
121 xfs_rtsumoff_t sumoff,
122 union xfs_suminfo_raw *rawinfo)
123 {
124 return xfile_load(sc->xfile, rawinfo,
125 sizeof(union xfs_suminfo_raw),
126 sumoff << XFS_WORDLOG);
127 }
128
129 static inline int
xfsum_store(struct xfs_scrub * sc,xfs_rtsumoff_t sumoff,const union xfs_suminfo_raw rawinfo)130 xfsum_store(
131 struct xfs_scrub *sc,
132 xfs_rtsumoff_t sumoff,
133 const union xfs_suminfo_raw rawinfo)
134 {
135 return xfile_store(sc->xfile, &rawinfo,
136 sizeof(union xfs_suminfo_raw),
137 sumoff << XFS_WORDLOG);
138 }
139
140 inline int
xfsum_copyout(struct xfs_scrub * sc,xfs_rtsumoff_t sumoff,union xfs_suminfo_raw * rawinfo,unsigned int nr_words)141 xfsum_copyout(
142 struct xfs_scrub *sc,
143 xfs_rtsumoff_t sumoff,
144 union xfs_suminfo_raw *rawinfo,
145 unsigned int nr_words)
146 {
147 return xfile_load(sc->xfile, rawinfo, nr_words << XFS_WORDLOG,
148 sumoff << XFS_WORDLOG);
149 }
150
151 static inline xfs_suminfo_t
xchk_rtsum_inc(struct xfs_mount * mp,union xfs_suminfo_raw * v)152 xchk_rtsum_inc(
153 struct xfs_mount *mp,
154 union xfs_suminfo_raw *v)
155 {
156 if (xfs_has_rtgroups(mp)) {
157 be32_add_cpu(&v->rtg, 1);
158 return be32_to_cpu(v->rtg);
159 }
160
161 v->old += 1;
162 return v->old;
163 }
164
165 /* Update the summary file to reflect the free extent that we've accumulated. */
166 STATIC int
xchk_rtsum_record_free(struct xfs_rtgroup * rtg,struct xfs_trans * tp,const struct xfs_rtalloc_rec * rec,void * priv)167 xchk_rtsum_record_free(
168 struct xfs_rtgroup *rtg,
169 struct xfs_trans *tp,
170 const struct xfs_rtalloc_rec *rec,
171 void *priv)
172 {
173 struct xfs_mount *mp = rtg_mount(rtg);
174 struct xfs_scrub *sc = priv;
175 xfs_fileoff_t rbmoff;
176 xfs_rtblock_t rtbno;
177 xfs_filblks_t rtlen;
178 xfs_rtsumoff_t offs;
179 unsigned int lenlog;
180 union xfs_suminfo_raw v;
181 xfs_suminfo_t value;
182 int error = 0;
183
184 if (xchk_should_terminate(sc, &error))
185 return error;
186
187 /* Compute the relevant location in the rtsum file. */
188 rbmoff = xfs_rtx_to_rbmblock(mp, rec->ar_startext);
189 lenlog = xfs_highbit64(rec->ar_extcount);
190 offs = xfs_rtsumoffs(mp, lenlog, rbmoff);
191
192 rtbno = xfs_rtx_to_rtb(rtg, rec->ar_startext);
193 rtlen = xfs_rtxlen_to_extlen(mp, rec->ar_extcount);
194
195 if (!xfs_verify_rtbext(mp, rtbno, rtlen)) {
196 xchk_ino_xref_set_corrupt(sc, rtg_bitmap(rtg)->i_ino);
197 return -EFSCORRUPTED;
198 }
199
200 /* Bump the summary count. */
201 error = xfsum_load(sc, offs, &v);
202 if (error)
203 return error;
204
205 value = xchk_rtsum_inc(sc->mp, &v);
206 trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
207 lenlog, offs, value);
208
209 return xfsum_store(sc, offs, v);
210 }
211
212 /* Compute the realtime summary from the realtime bitmap. */
213 STATIC int
xchk_rtsum_compute(struct xfs_scrub * sc)214 xchk_rtsum_compute(
215 struct xfs_scrub *sc)
216 {
217 struct xfs_mount *mp = sc->mp;
218 struct xfs_rtgroup *rtg = sc->sr.rtg;
219
220 /* If the bitmap size doesn't match the computed size, bail. */
221 if (XFS_FSB_TO_B(mp, xfs_rtbitmap_blockcount(mp)) !=
222 rtg_bitmap(rtg)->i_disk_size)
223 return -EFSCORRUPTED;
224
225 return xfs_rtalloc_query_all(rtg, sc->tp, xchk_rtsum_record_free, sc);
226 }
227
228 /* Compare the rtsummary file against the one we computed. */
229 STATIC int
xchk_rtsum_compare(struct xfs_scrub * sc)230 xchk_rtsum_compare(
231 struct xfs_scrub *sc)
232 {
233 struct xfs_bmbt_irec map;
234 struct xfs_iext_cursor icur;
235
236 struct xfs_mount *mp = sc->mp;
237 struct xfs_inode *ip = sc->ip;
238 struct xchk_rtsummary *rts = sc->buf;
239 xfs_fileoff_t off = 0;
240 xfs_fileoff_t endoff;
241 xfs_rtsumoff_t sumoff = 0;
242 int error = 0;
243
244 rts->args.mp = mp;
245 rts->args.tp = sc->tp;
246 rts->args.rtg = sc->sr.rtg;
247
248 /* Mappings may not cross or lie beyond EOF. */
249 endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
250 if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
251 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
252 return 0;
253 }
254
255 while (off < endoff) {
256 int nmap = 1;
257
258 if (xchk_should_terminate(sc, &error))
259 return error;
260 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
261 return 0;
262
263 /* Make sure we have a written extent. */
264 error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
265 XFS_DATA_FORK);
266 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
267 return error;
268
269 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
270 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
271 return 0;
272 }
273
274 off += map.br_blockcount;
275 }
276
277 for (off = 0; off < endoff; off++) {
278 union xfs_suminfo_raw *ondisk_info;
279
280 /* Read a block's worth of ondisk rtsummary file. */
281 error = xfs_rtsummary_read_buf(&rts->args, off);
282 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
283 return error;
284
285 /* Read a block's worth of computed rtsummary file. */
286 error = xfsum_copyout(sc, sumoff, rts->words, mp->m_blockwsize);
287 if (error) {
288 xfs_rtbuf_cache_relse(&rts->args);
289 return error;
290 }
291
292 ondisk_info = xfs_rsumblock_infoptr(&rts->args, 0);
293 if (memcmp(ondisk_info, rts->words,
294 mp->m_blockwsize << XFS_WORDLOG) != 0) {
295 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
296 xfs_rtbuf_cache_relse(&rts->args);
297 return error;
298 }
299
300 xfs_rtbuf_cache_relse(&rts->args);
301 sumoff += mp->m_blockwsize;
302 }
303
304 return 0;
305 }
306
307 /* Scrub the realtime summary. */
308 int
xchk_rtsummary(struct xfs_scrub * sc)309 xchk_rtsummary(
310 struct xfs_scrub *sc)
311 {
312 struct xfs_mount *mp = sc->mp;
313 struct xfs_rtgroup *rtg = sc->sr.rtg;
314 struct xfs_inode *rbmip = rtg_bitmap(rtg);
315 struct xfs_inode *rsumip = rtg_summary(rtg);
316 struct xchk_rtsummary *rts = sc->buf;
317 int error;
318
319 /* Is sb_rextents correct? */
320 if (mp->m_sb.sb_rextents != rts->rextents) {
321 xchk_ino_set_corrupt(sc, rbmip->i_ino);
322 return 0;
323 }
324
325 /* Is m_rsumlevels correct? */
326 if (mp->m_rsumlevels != rts->rsumlevels) {
327 xchk_ino_set_corrupt(sc, rsumip->i_ino);
328 return 0;
329 }
330
331 /* Is m_rsumsize correct? */
332 if (mp->m_rsumblocks != rts->rsumblocks) {
333 xchk_ino_set_corrupt(sc, rsumip->i_ino);
334 return 0;
335 }
336
337 /* The summary file length must be aligned to an fsblock. */
338 if (rsumip->i_disk_size & mp->m_blockmask) {
339 xchk_ino_set_corrupt(sc, rsumip->i_ino);
340 return 0;
341 }
342
343 /*
344 * Is the summary file itself large enough to handle the rt volume?
345 * growfsrt expands the summary file before updating sb_rextents, so
346 * the file can be larger than rsumsize.
347 */
348 if (rsumip->i_disk_size < XFS_FSB_TO_B(mp, rts->rsumblocks)) {
349 xchk_ino_set_corrupt(sc, rsumip->i_ino);
350 return 0;
351 }
352
353 /* Invoke the fork scrubber. */
354 error = xchk_metadata_inode_forks(sc);
355 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
356 return error;
357
358 /* Construct the new summary file from the rtbitmap. */
359 error = xchk_rtsum_compute(sc);
360 if (error == -EFSCORRUPTED) {
361 /*
362 * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
363 * error since we're checking the summary file.
364 */
365 xchk_ino_set_corrupt(sc, rbmip->i_ino);
366 return 0;
367 }
368 if (error)
369 return error;
370
371 /* Does the computed summary file match the actual rtsummary file? */
372 return xchk_rtsum_compare(sc);
373 }
374