1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 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_btree.h"
13 #include "xfs_inode.h"
14 #include "xfs_log_format.h"
15 #include "xfs_trans.h"
16 #include "xfs_rtbitmap.h"
20 #include "scrub/scrub.h"
21 #include "scrub/common.h"
22 #include "scrub/trace.h"
23 #include "scrub/xfile.h"
29 * We check the realtime summary by scanning the realtime bitmap file to create
30 * a new summary file incore, and then we compare the computed version against
31 * the ondisk version. We use the 'xfile' functionality to store this
32 * (potentially large) amount of data in pageable memory.
35 struct xchk_rtsummary {
36 struct xfs_rtalloc_args args;
41 unsigned int rsumlevels;
43 /* Memory buffer for the summary comparison. */
44 union xfs_suminfo_raw words[];
47 /* Set us up to check the rtsummary file. */
52 struct xfs_mount *mp = sc->mp;
54 struct xchk_rtsummary *rts;
57 rts = kvzalloc(struct_size(rts, words, mp->m_blockwsize),
64 * Create an xfile to construct a new rtsummary file. The xfile allows
65 * us to avoid pinning kernel memory for this purpose.
67 descr = xchk_xfile_descr(sc, "realtime summary file");
68 error = xfile_create(descr, mp->m_rsumsize, &sc->xfile);
73 error = xchk_trans_alloc(sc, 0);
77 error = xchk_install_live_inode(sc, mp->m_rsumip);
81 error = xchk_ino_dqattach(sc);
86 * Locking order requires us to take the rtbitmap first. We must be
87 * careful to unlock it ourselves when we are done with the rtbitmap
88 * file since the scrub infrastructure won't do that for us. Only
89 * then we can lock the rtsummary inode.
91 xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
92 xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
95 * Now that we've locked the rtbitmap and rtsummary, we can't race with
96 * growfsrt trying to expand the summary or change the size of the rt
97 * volume. Hence it is safe to compute and check the geometry values.
99 if (mp->m_sb.sb_rblocks) {
100 xfs_filblks_t rsumblocks;
103 rts->rextents = xfs_rtb_to_rtx(mp, mp->m_sb.sb_rblocks);
104 rextslog = xfs_compute_rextslog(rts->rextents);
105 rts->rsumlevels = rextslog + 1;
106 rts->rbmblocks = xfs_rtbitmap_blockcount(mp, rts->rextents);
107 rsumblocks = xfs_rtsummary_blockcount(mp, rts->rsumlevels,
109 rts->rsumsize = XFS_FSB_TO_B(mp, rsumblocks);
114 /* Helper functions to record suminfo words in an xfile. */
118 struct xfs_scrub *sc,
119 xfs_rtsumoff_t sumoff,
120 union xfs_suminfo_raw *rawinfo)
122 return xfile_load(sc->xfile, rawinfo,
123 sizeof(union xfs_suminfo_raw),
124 sumoff << XFS_WORDLOG);
129 struct xfs_scrub *sc,
130 xfs_rtsumoff_t sumoff,
131 const union xfs_suminfo_raw rawinfo)
133 return xfile_store(sc->xfile, &rawinfo,
134 sizeof(union xfs_suminfo_raw),
135 sumoff << XFS_WORDLOG);
140 struct xfs_scrub *sc,
141 xfs_rtsumoff_t sumoff,
142 union xfs_suminfo_raw *rawinfo,
143 unsigned int nr_words)
145 return xfile_load(sc->xfile, rawinfo, nr_words << XFS_WORDLOG,
146 sumoff << XFS_WORDLOG);
149 static inline xfs_suminfo_t
151 struct xfs_mount *mp,
152 union xfs_suminfo_raw *v)
158 /* Update the summary file to reflect the free extent that we've accumulated. */
160 xchk_rtsum_record_free(
161 struct xfs_mount *mp,
162 struct xfs_trans *tp,
163 const struct xfs_rtalloc_rec *rec,
166 struct xfs_scrub *sc = priv;
167 xfs_fileoff_t rbmoff;
172 union xfs_suminfo_raw v;
176 if (xchk_should_terminate(sc, &error))
179 /* Compute the relevant location in the rtsum file. */
180 rbmoff = xfs_rtx_to_rbmblock(mp, rec->ar_startext);
181 lenlog = xfs_highbit64(rec->ar_extcount);
182 offs = xfs_rtsumoffs(mp, lenlog, rbmoff);
184 rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
185 rtlen = xfs_rtx_to_rtb(mp, rec->ar_extcount);
187 if (!xfs_verify_rtbext(mp, rtbno, rtlen)) {
188 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
189 return -EFSCORRUPTED;
192 /* Bump the summary count. */
193 error = xfsum_load(sc, offs, &v);
197 value = xchk_rtsum_inc(sc->mp, &v);
198 trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
199 lenlog, offs, value);
201 return xfsum_store(sc, offs, v);
204 /* Compute the realtime summary from the realtime bitmap. */
207 struct xfs_scrub *sc)
209 struct xfs_mount *mp = sc->mp;
210 unsigned long long rtbmp_blocks;
212 /* If the bitmap size doesn't match the computed size, bail. */
213 rtbmp_blocks = xfs_rtbitmap_blockcount(mp, mp->m_sb.sb_rextents);
214 if (XFS_FSB_TO_B(mp, rtbmp_blocks) != mp->m_rbmip->i_disk_size)
215 return -EFSCORRUPTED;
217 return xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtsum_record_free,
221 /* Compare the rtsummary file against the one we computed. */
224 struct xfs_scrub *sc)
226 struct xfs_bmbt_irec map;
227 struct xfs_iext_cursor icur;
229 struct xfs_mount *mp = sc->mp;
230 struct xfs_inode *ip = sc->ip;
231 struct xchk_rtsummary *rts = sc->buf;
232 xfs_fileoff_t off = 0;
233 xfs_fileoff_t endoff;
234 xfs_rtsumoff_t sumoff = 0;
237 rts->args.mp = sc->mp;
238 rts->args.tp = sc->tp;
240 /* Mappings may not cross or lie beyond EOF. */
241 endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
242 if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
243 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
247 while (off < endoff) {
250 if (xchk_should_terminate(sc, &error))
252 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
255 /* Make sure we have a written extent. */
256 error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
258 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
261 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
262 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
266 off += map.br_blockcount;
269 for (off = 0; off < endoff; off++) {
270 union xfs_suminfo_raw *ondisk_info;
272 /* Read a block's worth of ondisk rtsummary file. */
273 error = xfs_rtsummary_read_buf(&rts->args, off);
274 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
277 /* Read a block's worth of computed rtsummary file. */
278 error = xfsum_copyout(sc, sumoff, rts->words, mp->m_blockwsize);
280 xfs_rtbuf_cache_relse(&rts->args);
284 ondisk_info = xfs_rsumblock_infoptr(&rts->args, 0);
285 if (memcmp(ondisk_info, rts->words,
286 mp->m_blockwsize << XFS_WORDLOG) != 0) {
287 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
288 xfs_rtbuf_cache_relse(&rts->args);
292 xfs_rtbuf_cache_relse(&rts->args);
293 sumoff += mp->m_blockwsize;
299 /* Scrub the realtime summary. */
302 struct xfs_scrub *sc)
304 struct xfs_mount *mp = sc->mp;
305 struct xchk_rtsummary *rts = sc->buf;
308 /* Is sb_rextents correct? */
309 if (mp->m_sb.sb_rextents != rts->rextents) {
310 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
314 /* Is m_rsumlevels correct? */
315 if (mp->m_rsumlevels != rts->rsumlevels) {
316 xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
320 /* Is m_rsumsize correct? */
321 if (mp->m_rsumsize != rts->rsumsize) {
322 xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
326 /* The summary file length must be aligned to an fsblock. */
327 if (mp->m_rsumip->i_disk_size & mp->m_blockmask) {
328 xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
333 * Is the summary file itself large enough to handle the rt volume?
334 * growfsrt expands the summary file before updating sb_rextents, so
335 * the file can be larger than rsumsize.
337 if (mp->m_rsumip->i_disk_size < rts->rsumsize) {
338 xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
342 /* Invoke the fork scrubber. */
343 error = xchk_metadata_inode_forks(sc);
344 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
347 /* Construct the new summary file from the rtbitmap. */
348 error = xchk_rtsum_compute(sc);
349 if (error == -EFSCORRUPTED) {
351 * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
352 * error since we're checking the summary file.
354 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
361 /* Does the computed summary file match the actual rtsummary file? */
362 error = xchk_rtsum_compare(sc);
365 /* Unlock the rtbitmap since we're done with it. */
366 xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);