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