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"
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 xfs_rtsumoff_t sumoff,
85 union xfs_suminfo_raw *rawinfo)
87 return xfile_obj_load(sc->xfile, rawinfo,
88 sizeof(union xfs_suminfo_raw),
89 sumoff << XFS_WORDLOG);
95 xfs_rtsumoff_t sumoff,
96 const union xfs_suminfo_raw rawinfo)
98 return xfile_obj_store(sc->xfile, &rawinfo,
99 sizeof(union xfs_suminfo_raw),
100 sumoff << XFS_WORDLOG);
105 struct xfs_scrub *sc,
106 xfs_rtsumoff_t sumoff,
107 union xfs_suminfo_raw *rawinfo,
108 unsigned int nr_words)
110 return xfile_obj_load(sc->xfile, rawinfo, nr_words << XFS_WORDLOG,
111 sumoff << XFS_WORDLOG);
114 static inline xfs_suminfo_t
116 struct xfs_mount *mp,
117 union xfs_suminfo_raw *v)
123 /* Update the summary file to reflect the free extent that we've accumulated. */
125 xchk_rtsum_record_free(
126 struct xfs_mount *mp,
127 struct xfs_trans *tp,
128 const struct xfs_rtalloc_rec *rec,
131 struct xfs_scrub *sc = priv;
132 xfs_fileoff_t rbmoff;
137 union xfs_suminfo_raw v;
141 if (xchk_should_terminate(sc, &error))
144 /* Compute the relevant location in the rtsum file. */
145 rbmoff = xfs_rtx_to_rbmblock(mp, rec->ar_startext);
146 lenlog = XFS_RTBLOCKLOG(rec->ar_extcount);
147 offs = xfs_rtsumoffs(mp, lenlog, rbmoff);
149 rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
150 rtlen = xfs_rtx_to_rtb(mp, rec->ar_extcount);
152 if (!xfs_verify_rtbext(mp, rtbno, rtlen)) {
153 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
154 return -EFSCORRUPTED;
157 /* Bump the summary count. */
158 error = xfsum_load(sc, offs, &v);
162 value = xchk_rtsum_inc(sc->mp, &v);
163 trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
164 lenlog, offs, value);
166 return xfsum_store(sc, offs, v);
169 /* Compute the realtime summary from the realtime bitmap. */
172 struct xfs_scrub *sc)
174 struct xfs_mount *mp = sc->mp;
175 unsigned long long rtbmp_blocks;
177 /* If the bitmap size doesn't match the computed size, bail. */
178 rtbmp_blocks = xfs_rtbitmap_blockcount(mp, mp->m_sb.sb_rextents);
179 if (XFS_FSB_TO_B(mp, rtbmp_blocks) != mp->m_rbmip->i_disk_size)
180 return -EFSCORRUPTED;
182 return xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtsum_record_free,
186 /* Compare the rtsummary file against the one we computed. */
189 struct xfs_scrub *sc)
191 struct xfs_rtalloc_args args = {
195 struct xfs_mount *mp = sc->mp;
196 struct xfs_bmbt_irec map;
198 xchk_rtsumoff_t sumoff = 0;
201 for (off = 0; off < XFS_B_TO_FSB(mp, mp->m_rsumsize); off++) {
202 union xfs_suminfo_raw *ondisk_info;
205 if (xchk_should_terminate(sc, &error))
207 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
210 /* Make sure we have a written extent. */
212 error = xfs_bmapi_read(mp->m_rsumip, off, 1, &map, &nmap,
214 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
217 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
218 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
222 /* Read a block's worth of ondisk rtsummary file. */
223 error = xfs_rtsummary_read_buf(&args, off);
224 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
227 /* Read a block's worth of computed rtsummary file. */
228 error = xfsum_copyout(sc, sumoff, sc->buf, mp->m_blockwsize);
230 xfs_rtbuf_cache_relse(&args);
234 ondisk_info = xfs_rsumblock_infoptr(&args, 0);
235 if (memcmp(ondisk_info, sc->buf,
236 mp->m_blockwsize << XFS_WORDLOG) != 0)
237 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
239 xfs_rtbuf_cache_relse(&args);
240 sumoff += mp->m_blockwsize;
246 /* Scrub the realtime summary. */
249 struct xfs_scrub *sc)
251 struct xfs_mount *mp = sc->mp;
254 /* Invoke the fork scrubber. */
255 error = xchk_metadata_inode_forks(sc);
256 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
259 /* Construct the new summary file from the rtbitmap. */
260 error = xchk_rtsum_compute(sc);
261 if (error == -EFSCORRUPTED) {
263 * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
264 * error since we're checking the summary file.
266 xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
273 /* Does the computed summary file match the actual rtsummary file? */
274 error = xchk_rtsum_compare(sc);
277 /* Unlock the rtbitmap since we're done with it. */
278 xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);