1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_rtbitmap.h"
15 #include "xfs_inode.h"
18 #include "scrub/scrub.h"
19 #include "scrub/common.h"
20 #include "scrub/repair.h"
21 #include "scrub/rtbitmap.h"
23 /* Set us up with the realtime metadata locked. */
28 struct xfs_mount *mp = sc->mp;
29 struct xchk_rtbitmap *rtb;
32 rtb = kzalloc(sizeof(struct xchk_rtbitmap), XCHK_GFP_FLAGS);
37 if (xchk_could_repair(sc)) {
38 error = xrep_setup_rtbitmap(sc, rtb);
43 error = xchk_trans_alloc(sc, rtb->resblks);
47 error = xchk_install_live_inode(sc, sc->mp->m_rbmip);
51 error = xchk_ino_dqattach(sc);
55 xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
58 * Now that we've locked the rtbitmap, we can't race with growfsrt
59 * trying to expand the bitmap or change the size of the rt volume.
60 * Hence it is safe to compute and check the geometry values.
62 if (mp->m_sb.sb_rblocks) {
63 rtb->rextents = xfs_rtb_to_rtx(mp, mp->m_sb.sb_rblocks);
64 rtb->rextslog = xfs_compute_rextslog(rtb->rextents);
65 rtb->rbmblocks = xfs_rtbitmap_blockcount(mp, rtb->rextents);
70 /* Realtime bitmap. */
72 /* Scrub a free extent record from the realtime bitmap. */
77 const struct xfs_rtalloc_rec *rec,
80 struct xfs_scrub *sc = priv;
81 xfs_rtblock_t startblock;
82 xfs_filblks_t blockcount;
84 startblock = xfs_rtx_to_rtb(mp, rec->ar_startext);
85 blockcount = xfs_rtx_to_rtb(mp, rec->ar_extcount);
87 if (!xfs_verify_rtbext(mp, startblock, blockcount))
88 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
92 /* Make sure the entire rtbitmap file is mapped with written extents. */
94 xchk_rtbitmap_check_extents(
97 struct xfs_bmbt_irec map;
98 struct xfs_iext_cursor icur;
99 struct xfs_mount *mp = sc->mp;
100 struct xfs_inode *ip = sc->ip;
101 xfs_fileoff_t off = 0;
102 xfs_fileoff_t endoff;
105 /* Mappings may not cross or lie beyond EOF. */
106 endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
107 if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
108 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
112 while (off < endoff) {
115 if (xchk_should_terminate(sc, &error) ||
116 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
119 /* Make sure we have a written extent. */
120 error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
122 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
125 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
126 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
130 off += map.br_blockcount;
136 /* Scrub the realtime bitmap. */
139 struct xfs_scrub *sc)
141 struct xfs_mount *mp = sc->mp;
142 struct xchk_rtbitmap *rtb = sc->buf;
145 /* Is sb_rextents correct? */
146 if (mp->m_sb.sb_rextents != rtb->rextents) {
147 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
151 /* Is sb_rextslog correct? */
152 if (mp->m_sb.sb_rextslog != rtb->rextslog) {
153 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
158 * Is sb_rbmblocks large enough to handle the current rt volume? In no
159 * case can we exceed 4bn bitmap blocks since the super field is a u32.
161 if (rtb->rbmblocks > U32_MAX) {
162 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
165 if (mp->m_sb.sb_rbmblocks != rtb->rbmblocks) {
166 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
170 /* The bitmap file length must be aligned to an fsblock. */
171 if (mp->m_rbmip->i_disk_size & mp->m_blockmask) {
172 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
177 * Is the bitmap file itself large enough to handle the rt volume?
178 * growfsrt expands the bitmap file before updating sb_rextents, so the
179 * file can be larger than sb_rbmblocks.
181 if (mp->m_rbmip->i_disk_size < XFS_FSB_TO_B(mp, rtb->rbmblocks)) {
182 xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
186 /* Invoke the fork scrubber. */
187 error = xchk_metadata_inode_forks(sc);
188 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
191 error = xchk_rtbitmap_check_extents(sc);
192 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
195 error = xfs_rtalloc_query_all(mp, sc->tp, xchk_rtbitmap_rec, sc);
196 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
202 /* xref check that the extent is not free in the rtbitmap */
204 xchk_xref_is_used_rt_space(
205 struct xfs_scrub *sc,
209 xfs_rtxnum_t startext;
214 if (xchk_skip_xref(sc->sm))
217 startext = xfs_rtb_to_rtx(sc->mp, rtbno);
218 endext = xfs_rtb_to_rtx(sc->mp, rtbno + len - 1);
219 xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
220 error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext,
221 endext - startext + 1, &is_free);
222 if (!xchk_should_check_xref(sc, &error, NULL))
225 xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
227 xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);