^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2017 Oracle. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Darrick J. Wong <darrick.wong@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "xfs_rtalloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "xfs_bmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "scrub/scrub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "scrub/common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Set us up with the realtime metadata locked. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) xchk_setup_rt(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct xfs_scrub *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) error = xchk_setup_fs(sc, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTBITMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) sc->ip = sc->mp->m_rbmip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) xfs_ilock(sc->ip, sc->ilock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Realtime bitmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Scrub a free extent record from the realtime bitmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) xchk_rtbitmap_rec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct xfs_trans *tp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct xfs_rtalloc_rec *rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct xfs_scrub *sc = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) xfs_rtblock_t startblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) xfs_rtblock_t blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) startblock = rec->ar_startext * tp->t_mountp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) blockcount = rec->ar_extcount * tp->t_mountp->m_sb.sb_rextsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (startblock + blockcount <= startblock ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) !xfs_verify_rtbno(sc->mp, startblock) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) !xfs_verify_rtbno(sc->mp, startblock + blockcount - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Make sure the entire rtbitmap file is mapped with written extents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) xchk_rtbitmap_check_extents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct xfs_scrub *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct xfs_mount *mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct xfs_bmbt_irec map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) xfs_rtblock_t off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int nmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) for (off = 0; off < mp->m_sb.sb_rbmblocks;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (xchk_should_terminate(sc, &error) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Make sure we have a written extent. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) nmap = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) error = xfs_bmapi_read(mp->m_rbmip, off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) mp->m_sb.sb_rbmblocks - off, &map, &nmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) XFS_DATA_FORK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) off += map.br_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Scrub the realtime bitmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) xchk_rtbitmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct xfs_scrub *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Is the size of the rtbitmap correct? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (sc->mp->m_rbmip->i_d.di_size !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) XFS_FSB_TO_B(sc->mp, sc->mp->m_sb.sb_rbmblocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) xchk_ino_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* Invoke the fork scrubber. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) error = xchk_metadata_inode_forks(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) error = xchk_rtbitmap_check_extents(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) error = xfs_rtalloc_query_all(sc->tp, xchk_rtbitmap_rec, sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* Scrub the realtime summary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) xchk_rtsummary(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct xfs_scrub *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct xfs_inode *rsumip = sc->mp->m_rsumip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct xfs_inode *old_ip = sc->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) uint old_ilock_flags = sc->ilock_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * We ILOCK'd the rt bitmap ip in the setup routine, now lock the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * rt summary ip in compliance with the rt inode locking rules.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Since we switch sc->ip to rsumip we have to save the old ilock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * flags so that we don't mix up the inode state that @sc tracks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) sc->ip = rsumip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) sc->ilock_flags = XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) xfs_ilock(sc->ip, sc->ilock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* Invoke the fork scrubber. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) error = xchk_metadata_inode_forks(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* XXX: implement this some day */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) xchk_set_incomplete(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Switch back to the rtbitmap inode and lock flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) xfs_iunlock(sc->ip, sc->ilock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) sc->ilock_flags = old_ilock_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) sc->ip = old_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* xref check that the extent is not free in the rtbitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) xchk_xref_is_used_rt_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct xfs_scrub *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) xfs_rtblock_t fsbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) xfs_extlen_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) xfs_rtblock_t startext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) xfs_rtblock_t endext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) xfs_rtblock_t extcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) bool is_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (xchk_skip_xref(sc->sm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) startext = fsbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) endext = fsbno + len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) do_div(startext, sc->mp->m_sb.sb_rextsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) do_div(endext, sc->mp->m_sb.sb_rextsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) extcount = endext - startext + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) xfs_ilock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) error = xfs_rtalloc_extent_is_free(sc->mp, sc->tp, startext, extcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) &is_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!xchk_should_check_xref(sc, &error, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (is_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) xchk_ino_xref_set_corrupt(sc, sc->mp->m_rbmip->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) xfs_iunlock(sc->mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }