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_rtalloc.h"
19 #include "scrub/scrub.h"
20 #include "scrub/common.h"
21 #include "scrub/trace.h"
22 #include "scrub/xfile.h"
28 * We check the realtime summary by scanning the realtime bitmap file to create
29 * a new summary file incore, and then we compare the computed version against
30 * the ondisk version. We use the 'xfile' functionality to store this
31 * (potentially large) amount of data in pageable memory.
34 /* Set us up to check the rtsummary file. */
39 struct xfs_mount *mp = sc->mp;
44 * Create an xfile to construct a new rtsummary file. The xfile allows
45 * us to avoid pinning kernel memory for this purpose.
47 descr = xchk_xfile_descr(sc, "realtime summary file");
48 error = xfile_create(descr, mp->m_rsumsize, &sc->xfile);
53 error = xchk_trans_alloc(sc, 0);
57 /* Allocate a memory buffer for the summary comparison. */
58 sc->buf = kvmalloc(mp->m_sb.sb_blocksize, XCHK_GFP_FLAGS);
62 error = xchk_install_live_inode(sc, mp->m_rsumip);
67 * Locking order requires us to take the rtbitmap first. We must be
68 * careful to unlock it ourselves when we are done with the rtbitmap
69 * file since the scrub infrastructure won't do that for us. Only
70 * then we can lock the rtsummary inode.
72 xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
73 xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
77 /* Helper functions to record suminfo words in an xfile. */
79 typedef unsigned int xchk_rtsumoff_t;
84 xchk_rtsumoff_t sumoff,
87 return xfile_obj_load(sc->xfile, info, sizeof(xfs_suminfo_t),
88 sumoff << XFS_WORDLOG);
94 xchk_rtsumoff_t sumoff,
95 const xfs_suminfo_t info)
97 return xfile_obj_store(sc->xfile, &info, sizeof(xfs_suminfo_t),
98 sumoff << XFS_WORDLOG);
103 struct xfs_scrub *sc,
104 xchk_rtsumoff_t sumoff,
106 unsigned int nr_words)
108 return xfile_obj_load(sc->xfile, info, nr_words << XFS_WORDLOG,
109 sumoff << XFS_WORDLOG);
112 /* Update the summary file to reflect the free extent that we've accumulated. */
114 xchk_rtsum_record_free(
115 struct xfs_mount *mp,
116 struct xfs_trans *tp,
117 const struct xfs_rtalloc_rec *rec,
120 struct xfs_scrub *sc = priv;
121 xfs_fileoff_t rbmoff;
124 xchk_rtsumoff_t offs;
129 if (xchk_should_terminate(sc, &error))
132 /* Compute the relevant location in the rtsum file. */
133 rbmoff = XFS_BITTOBLOCK(mp, rec->ar_startext);
134 lenlog = XFS_RTBLOCKLOG(rec->ar_extcount);
135 offs = XFS_SUMOFFS(mp, lenlog, rbmoff);
137 rtbno = rec->ar_startext * mp->m_sb.sb_rextsize;
138 rtlen = rec->ar_extcount * mp->m_sb.sb_rextsize;
140 if (!xfs_verify_rtext(mp, rtbno, rtlen)) {
141 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
142 return -EFSCORRUPTED;
145 /* Bump the summary count. */
146 error = xfsum_load(sc, offs, &v);
151 trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
154 return xfsum_store(sc, offs, v);
157 /* Compute the realtime summary from the realtime bitmap. */
160 struct xfs_scrub *sc)
162 struct xfs_mount *mp = sc->mp;
163 unsigned long long rtbmp_bytes;
165 /* If the bitmap size doesn't match the computed size, bail. */
166 rtbmp_bytes = howmany_64(mp->m_sb.sb_rextents, NBBY);
167 if (roundup_64(rtbmp_bytes, mp->m_sb.sb_blocksize) !=
168 mp->m_rbmip->i_disk_size)
169 return -EFSCORRUPTED;
171 return xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtsum_record_free,
175 /* Compare the rtsummary file against the one we computed. */
178 struct xfs_scrub *sc)
180 struct xfs_mount *mp = sc->mp;
182 struct xfs_bmbt_irec map;
184 xchk_rtsumoff_t sumoff = 0;
187 for (off = 0; off < XFS_B_TO_FSB(mp, mp->m_rsumsize); off++) {
190 if (xchk_should_terminate(sc, &error))
192 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
195 /* Make sure we have a written extent. */
197 error = xfs_bmapi_read(mp->m_rsumip, off, 1, &map, &nmap,
199 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
202 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
203 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
207 /* Read a block's worth of ondisk rtsummary file. */
208 error = xfs_rtbuf_get(mp, sc->tp, off, 1, &bp);
209 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
212 /* Read a block's worth of computed rtsummary file. */
213 error = xfsum_copyout(sc, sumoff, sc->buf, mp->m_blockwsize);
215 xfs_trans_brelse(sc->tp, bp);
219 if (memcmp(bp->b_addr, sc->buf,
220 mp->m_blockwsize << XFS_WORDLOG) != 0)
221 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
223 xfs_trans_brelse(sc->tp, bp);
224 sumoff += mp->m_blockwsize;
230 /* Scrub the realtime summary. */
233 struct xfs_scrub *sc)
235 struct xfs_mount *mp = sc->mp;
238 /* Invoke the fork scrubber. */
239 error = xchk_metadata_inode_forks(sc);
240 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
243 /* Construct the new summary file from the rtbitmap. */
244 error = xchk_rtsum_compute(sc);
245 if (error == -EFSCORRUPTED) {
247 * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
248 * error since we're checking the summary file.
250 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
257 /* Does the computed summary file match the actual rtsummary file? */
258 error = xchk_rtsum_compare(sc);
261 /* Unlock the rtbitmap since we're done with it. */
262 xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);