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_log_format.h"
13 #include "xfs_trans.h"
14 #include "xfs_rtalloc.h"
15 #include "xfs_inode.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
20 /* Set us up with the realtime metadata locked. */
27 error = xchk_trans_alloc(sc, 0);
31 error = xchk_install_live_inode(sc, sc->mp->m_rbmip);
35 xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP);
39 /* Realtime bitmap. */
41 /* Scrub a free extent record from the realtime bitmap. */
46 const struct xfs_rtalloc_rec *rec,
49 struct xfs_scrub *sc = priv;
50 xfs_rtblock_t startblock;
51 xfs_rtblock_t blockcount;
53 startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
54 blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
56 if (!xfs_verify_rtext(mp, startblock, blockcount))
57 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
61 /* Make sure the entire rtbitmap file is mapped with written extents. */
63 xchk_rtbitmap_check_extents(
66 struct xfs_mount *mp = sc->mp;
67 struct xfs_bmbt_irec map;
72 for (off = 0; off < mp->m_sb.sb_rbmblocks;) {
73 if (xchk_should_terminate(sc, &error) ||
74 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
77 /* Make sure we have a written extent. */
79 error = xfs_bmapi_read(mp->m_rbmip, off,
80 mp->m_sb.sb_rbmblocks - off, &map, &nmap,
82 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
85 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
86 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
90 off += map.br_blockcount;
96 /* Scrub the realtime bitmap. */
103 /* Is the size of the rtbitmap correct? */
104 if (sc->mp->m_rbmip->i_disk_size !=
105 XFS_FSB_TO_B(sc->mp, sc->mp->m_sb.sb_rbmblocks)) {
106 xchk_ino_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
110 /* Invoke the fork scrubber. */
111 error = xchk_metadata_inode_forks(sc);
112 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
115 error = xchk_rtbitmap_check_extents(sc);
116 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
119 error = xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtbitmap_rec, sc);
120 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
127 /* xref check that the extent is not free in the rtbitmap */
129 xchk_xref_is_used_rt_space(
130 struct xfs_scrub *sc,
134 xfs_rtblock_t startext;
135 xfs_rtblock_t endext;
136 xfs_rtblock_t extcount;
140 if (xchk_skip_xref(sc->sm))
144 endext = fsbno + len - 1;
145 do_div(startext, sc->mp->m_sb.sb_rextsize);
146 do_div(endext, sc->mp->m_sb.sb_rextsize);
147 extcount = endext - startext + 1;
148 xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
149 error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
151 if (!xchk_should_check_xref(sc, &error, NULL))
154 xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
156 xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);