1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved. 4 * Author: Darrick J. Wong <djwong@kernel.org> 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_btree.h" 13 #include "xfs_log_format.h" 14 #include "xfs_trans.h" 15 #include "xfs_rtbitmap.h" 16 #include "xfs_inode.h" 17 #include "xfs_bmap.h" 18 #include "xfs_bit.h" 19 #include "xfs_rtgroup.h" 20 #include "xfs_sb.h" 21 #include "xfs_rmap.h" 22 #include "xfs_rtrmap_btree.h" 23 #include "xfs_exchmaps.h" 24 #include "xfs_zone_alloc.h" 25 #include "scrub/scrub.h" 26 #include "scrub/common.h" 27 #include "scrub/repair.h" 28 #include "scrub/tempexch.h" 29 #include "scrub/rtbitmap.h" 30 #include "scrub/btree.h" 31 32 /* Set us up with the realtime metadata locked. */ 33 int 34 xchk_setup_rtbitmap( 35 struct xfs_scrub *sc) 36 { 37 struct xfs_mount *mp = sc->mp; 38 struct xchk_rtbitmap *rtb; 39 int error; 40 41 if (xchk_need_intent_drain(sc)) 42 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN); 43 44 rtb = kzalloc(struct_size(rtb, words, xchk_rtbitmap_wordcnt(sc)), 45 XCHK_GFP_FLAGS); 46 if (!rtb) 47 return -ENOMEM; 48 sc->buf = rtb; 49 rtb->sc = sc; 50 51 error = xchk_rtgroup_init(sc, sc->sm->sm_agno, &sc->sr); 52 if (error) 53 return error; 54 55 if (xchk_could_repair(sc)) { 56 error = xrep_setup_rtbitmap(sc, rtb); 57 if (error) 58 return error; 59 } 60 61 error = xchk_trans_alloc(sc, rtb->resblks); 62 if (error) 63 return error; 64 65 error = xchk_install_live_inode(sc, rtg_bitmap(sc->sr.rtg)); 66 if (error) 67 return error; 68 69 error = xchk_ino_dqattach(sc); 70 if (error) 71 return error; 72 73 error = xchk_rtgroup_lock(sc, &sc->sr, XCHK_RTGLOCK_ALL); 74 if (error) 75 return error; 76 77 /* 78 * Now that we've locked the rtbitmap, we can't race with growfsrt 79 * trying to expand the bitmap or change the size of the rt volume. 80 * Hence it is safe to compute and check the geometry values. 81 */ 82 if (mp->m_sb.sb_rblocks) { 83 rtb->rextents = xfs_blen_to_rtbxlen(mp, mp->m_sb.sb_rblocks); 84 rtb->rextslog = xfs_compute_rextslog(rtb->rextents); 85 rtb->rbmblocks = xfs_rtbitmap_blockcount(mp); 86 } 87 88 return 0; 89 } 90 91 /* Per-rtgroup bitmap contents. */ 92 93 /* Cross-reference rtbitmap entries with other metadata. */ 94 STATIC void 95 xchk_rtbitmap_xref( 96 struct xchk_rtbitmap *rtb, 97 xfs_rtblock_t startblock, 98 xfs_rtblock_t blockcount) 99 { 100 struct xfs_scrub *sc = rtb->sc; 101 xfs_rgblock_t rgbno = xfs_rtb_to_rgbno(sc->mp, startblock); 102 103 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 104 return; 105 if (!sc->sr.rmap_cur) 106 return; 107 108 xchk_xref_has_no_rt_owner(sc, rgbno, blockcount); 109 xchk_xref_is_not_rt_shared(sc, rgbno, blockcount); 110 xchk_xref_is_not_rt_cow_staging(sc, rgbno, blockcount); 111 112 if (rtb->next_free_rgbno < rgbno) 113 xchk_xref_has_rt_owner(sc, rtb->next_free_rgbno, 114 rgbno - rtb->next_free_rgbno); 115 rtb->next_free_rgbno = rgbno + blockcount; 116 } 117 118 /* Scrub a free extent record from the realtime bitmap. */ 119 STATIC int 120 xchk_rtbitmap_rec( 121 struct xfs_rtgroup *rtg, 122 struct xfs_trans *tp, 123 const struct xfs_rtalloc_rec *rec, 124 void *priv) 125 { 126 struct xchk_rtbitmap *rtb = priv; 127 struct xfs_scrub *sc = rtb->sc; 128 xfs_rtblock_t startblock; 129 xfs_filblks_t blockcount; 130 131 startblock = xfs_rtx_to_rtb(rtg, rec->ar_startext); 132 blockcount = xfs_rtxlen_to_extlen(rtg_mount(rtg), rec->ar_extcount); 133 134 if (!xfs_verify_rtbext(rtg_mount(rtg), startblock, blockcount)) 135 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0); 136 137 xchk_rtbitmap_xref(rtb, startblock, blockcount); 138 139 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 140 return -ECANCELED; 141 142 return 0; 143 } 144 145 /* Make sure the entire rtbitmap file is mapped with written extents. */ 146 STATIC int 147 xchk_rtbitmap_check_extents( 148 struct xfs_scrub *sc) 149 { 150 struct xfs_bmbt_irec map; 151 struct xfs_iext_cursor icur; 152 struct xfs_mount *mp = sc->mp; 153 struct xfs_inode *ip = sc->ip; 154 xfs_fileoff_t off = 0; 155 xfs_fileoff_t endoff; 156 int error = 0; 157 158 /* Mappings may not cross or lie beyond EOF. */ 159 endoff = XFS_B_TO_FSB(mp, ip->i_disk_size); 160 if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) { 161 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff); 162 return 0; 163 } 164 165 while (off < endoff) { 166 int nmap = 1; 167 168 if (xchk_should_terminate(sc, &error) || 169 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) 170 break; 171 172 /* Make sure we have a written extent. */ 173 error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap, 174 XFS_DATA_FORK); 175 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error)) 176 break; 177 178 if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) { 179 xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off); 180 break; 181 } 182 183 off += map.br_blockcount; 184 } 185 186 return error; 187 } 188 189 /* Scrub this group's realtime bitmap. */ 190 int 191 xchk_rtbitmap( 192 struct xfs_scrub *sc) 193 { 194 struct xfs_mount *mp = sc->mp; 195 struct xfs_rtgroup *rtg = sc->sr.rtg; 196 struct xfs_inode *rbmip = rtg_bitmap(rtg); 197 struct xchk_rtbitmap *rtb = sc->buf; 198 xfs_rgblock_t last_rgbno; 199 int error; 200 201 /* Is sb_rextents correct? */ 202 if (mp->m_sb.sb_rextents != rtb->rextents) { 203 xchk_ino_set_corrupt(sc, rbmip->i_ino); 204 return 0; 205 } 206 207 /* Is sb_rextslog correct? */ 208 if (mp->m_sb.sb_rextslog != rtb->rextslog) { 209 xchk_ino_set_corrupt(sc, rbmip->i_ino); 210 return 0; 211 } 212 213 /* 214 * Is sb_rbmblocks large enough to handle the current rt volume? In no 215 * case can we exceed 4bn bitmap blocks since the super field is a u32. 216 */ 217 if (rtb->rbmblocks > U32_MAX) { 218 xchk_ino_set_corrupt(sc, rbmip->i_ino); 219 return 0; 220 } 221 if (mp->m_sb.sb_rbmblocks != rtb->rbmblocks) { 222 xchk_ino_set_corrupt(sc, rbmip->i_ino); 223 return 0; 224 } 225 226 /* The bitmap file length must be aligned to an fsblock. */ 227 if (rbmip->i_disk_size & mp->m_blockmask) { 228 xchk_ino_set_corrupt(sc, rbmip->i_ino); 229 return 0; 230 } 231 232 /* 233 * Is the bitmap file itself large enough to handle the rt volume? 234 * growfsrt expands the bitmap file before updating sb_rextents, so the 235 * file can be larger than sb_rbmblocks. 236 */ 237 if (rbmip->i_disk_size < XFS_FSB_TO_B(mp, rtb->rbmblocks)) { 238 xchk_ino_set_corrupt(sc, rbmip->i_ino); 239 return 0; 240 } 241 242 /* Invoke the fork scrubber. */ 243 error = xchk_metadata_inode_forks(sc); 244 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) 245 return error; 246 247 error = xchk_rtbitmap_check_extents(sc); 248 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) 249 return error; 250 251 rtb->next_free_rgbno = 0; 252 error = xfs_rtalloc_query_all(rtg, sc->tp, xchk_rtbitmap_rec, rtb); 253 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error)) 254 return error; 255 256 /* 257 * Check that the are rmappings for all rt extents between the end of 258 * the last free extent we saw and the last possible extent in the rt 259 * group. 260 */ 261 last_rgbno = rtg->rtg_extents * mp->m_sb.sb_rextsize - 1; 262 if (rtb->next_free_rgbno < last_rgbno) 263 xchk_xref_has_rt_owner(sc, rtb->next_free_rgbno, 264 last_rgbno - rtb->next_free_rgbno); 265 return 0; 266 } 267 268 /* xref check that the extent is not free in the rtbitmap */ 269 void 270 xchk_xref_is_used_rt_space( 271 struct xfs_scrub *sc, 272 xfs_rtblock_t rtbno, 273 xfs_extlen_t len) 274 { 275 struct xfs_rtgroup *rtg = sc->sr.rtg; 276 xfs_rtxnum_t startext; 277 xfs_rtxnum_t endext; 278 bool is_free; 279 int error; 280 281 if (xchk_skip_xref(sc->sm)) 282 return; 283 284 if (xfs_has_zoned(sc->mp)) { 285 if (!xfs_zone_rgbno_is_valid(rtg, 286 xfs_rtb_to_rgbno(sc->mp, rtbno) + len - 1)) 287 xchk_ino_xref_set_corrupt(sc, rtg_rmap(rtg)->i_ino); 288 return; 289 } 290 291 startext = xfs_rtb_to_rtx(sc->mp, rtbno); 292 endext = xfs_rtb_to_rtx(sc->mp, rtbno + len - 1); 293 error = xfs_rtalloc_extent_is_free(rtg, sc->tp, startext, 294 endext - startext + 1, &is_free); 295 if (!xchk_should_check_xref(sc, &error, NULL)) 296 return; 297 if (is_free) 298 xchk_ino_xref_set_corrupt(sc, rtg_bitmap(rtg)->i_ino); 299 } 300