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_platform.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_bit.h" 14 #include "xfs_log_format.h" 15 #include "xfs_trans.h" 16 #include "xfs_inode.h" 17 #include "xfs_alloc.h" 18 #include "xfs_bmap.h" 19 #include "xfs_bmap_btree.h" 20 #include "xfs_rmap.h" 21 #include "xfs_rmap_btree.h" 22 #include "xfs_rtgroup.h" 23 #include "xfs_health.h" 24 #include "xfs_rtalloc.h" 25 #include "xfs_rtrmap_btree.h" 26 #include "scrub/scrub.h" 27 #include "scrub/common.h" 28 #include "scrub/btree.h" 29 #include "scrub/health.h" 30 #include "xfs_ag.h" 31 32 /* Set us up with an inode's bmap. */ 33 int 34 xchk_setup_inode_bmap( 35 struct xfs_scrub *sc) 36 { 37 int error; 38 39 if (xchk_need_intent_drain(sc)) 40 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN); 41 42 error = xchk_iget_for_scrubbing(sc); 43 if (error) 44 goto out; 45 46 xchk_ilock(sc, XFS_IOLOCK_EXCL); 47 48 /* 49 * We don't want any ephemeral data/cow fork updates sitting around 50 * while we inspect block mappings, so wait for directio to finish 51 * and flush dirty data if we have delalloc reservations. 52 */ 53 if (S_ISREG(VFS_I(sc->ip)->i_mode) && 54 sc->sm->sm_type != XFS_SCRUB_TYPE_BMBTA) { 55 struct address_space *mapping = VFS_I(sc->ip)->i_mapping; 56 bool is_repair = xchk_could_repair(sc); 57 58 xchk_ilock(sc, XFS_MMAPLOCK_EXCL); 59 60 /* Break all our leases, we're going to mess with things. */ 61 if (is_repair) { 62 error = xfs_break_layouts(VFS_I(sc->ip), 63 &sc->ilock_flags, BREAK_WRITE); 64 if (error) 65 goto out; 66 } 67 68 inode_dio_wait(VFS_I(sc->ip)); 69 70 /* 71 * Try to flush all incore state to disk before we examine the 72 * space mappings for the data fork. Leave accumulated errors 73 * in the mapping for the writer threads to consume. 74 * 75 * On ENOSPC or EIO writeback errors, we continue into the 76 * extent mapping checks because write failures do not 77 * necessarily imply anything about the correctness of the file 78 * metadata. The metadata and the file data could be on 79 * completely separate devices; a media failure might only 80 * affect a subset of the disk, etc. We can handle delalloc 81 * extents in the scrubber, so leaving them in memory is fine. 82 */ 83 error = filemap_fdatawrite(mapping); 84 if (!error) 85 error = filemap_fdatawait_keep_errors(mapping); 86 if (error && (error != -ENOSPC && error != -EIO)) 87 goto out; 88 89 /* Drop the page cache if we're repairing block mappings. */ 90 if (is_repair) { 91 error = invalidate_inode_pages2( 92 VFS_I(sc->ip)->i_mapping); 93 if (error) 94 goto out; 95 } 96 97 } 98 99 /* Got the inode, lock it and we're ready to go. */ 100 error = xchk_trans_alloc(sc, 0); 101 if (error) 102 goto out; 103 104 error = xchk_ino_dqattach(sc); 105 if (error) 106 goto out; 107 108 xchk_ilock(sc, XFS_ILOCK_EXCL); 109 out: 110 /* scrub teardown will unlock and release the inode */ 111 return error; 112 } 113 114 /* 115 * Inode fork block mapping (BMBT) scrubber. 116 * More complex than the others because we have to scrub 117 * all the extents regardless of whether or not the fork 118 * is in btree format. 119 */ 120 121 struct xchk_bmap_info { 122 struct xfs_scrub *sc; 123 124 /* Incore extent tree cursor */ 125 struct xfs_iext_cursor icur; 126 127 /* Previous fork mapping that we examined */ 128 struct xfs_bmbt_irec prev_rec; 129 130 /* Is this a realtime fork? */ 131 bool is_rt; 132 133 /* May mappings point to shared space? */ 134 bool is_shared; 135 136 /* Was the incore extent tree loaded? */ 137 bool was_loaded; 138 139 /* Which inode fork are we checking? */ 140 int whichfork; 141 }; 142 143 /* Look for a corresponding rmap for this irec. */ 144 static inline bool 145 xchk_bmap_get_rmap( 146 struct xchk_bmap_info *info, 147 struct xfs_bmbt_irec *irec, 148 xfs_agblock_t bno, 149 uint64_t owner, 150 struct xfs_rmap_irec *rmap) 151 { 152 struct xfs_btree_cur **curp = &info->sc->sa.rmap_cur; 153 xfs_fileoff_t offset; 154 unsigned int rflags = 0; 155 int has_rmap; 156 int error; 157 158 if (xfs_ifork_is_realtime(info->sc->ip, info->whichfork)) 159 curp = &info->sc->sr.rmap_cur; 160 161 if (*curp == NULL) 162 return false; 163 164 if (info->whichfork == XFS_ATTR_FORK) 165 rflags |= XFS_RMAP_ATTR_FORK; 166 if (irec->br_state == XFS_EXT_UNWRITTEN) 167 rflags |= XFS_RMAP_UNWRITTEN; 168 169 /* 170 * CoW staging extents are owned (on disk) by the refcountbt, so 171 * their rmaps do not have offsets. 172 */ 173 if (info->whichfork == XFS_COW_FORK) 174 offset = 0; 175 else 176 offset = irec->br_startoff; 177 178 /* 179 * If the caller thinks this could be a shared bmbt extent (IOWs, 180 * any data fork extent of a reflink inode) then we have to use the 181 * range rmap lookup to make sure we get the correct owner/offset. 182 */ 183 if (info->is_shared) { 184 error = xfs_rmap_lookup_le_range(*curp, bno, owner, offset, 185 rflags, rmap, &has_rmap); 186 } else { 187 error = xfs_rmap_lookup_le(*curp, bno, owner, offset, 188 rflags, rmap, &has_rmap); 189 } 190 if (!xchk_should_check_xref(info->sc, &error, curp)) 191 return false; 192 193 if (!has_rmap) 194 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 195 irec->br_startoff); 196 return has_rmap; 197 } 198 199 /* Make sure that we have rmapbt records for this data/attr fork extent. */ 200 STATIC void 201 xchk_bmap_xref_rmap( 202 struct xchk_bmap_info *info, 203 struct xfs_bmbt_irec *irec, 204 xfs_agblock_t bno) 205 { 206 struct xfs_rmap_irec rmap; 207 unsigned long long rmap_end; 208 uint64_t owner = I_INO(info->sc->ip); 209 210 if (xchk_skip_xref(info->sc->sm)) 211 return; 212 213 /* Find the rmap record for this irec. */ 214 if (!xchk_bmap_get_rmap(info, irec, bno, owner, &rmap)) 215 return; 216 217 /* 218 * The rmap must be an exact match for this incore file mapping record, 219 * which may have arisen from multiple ondisk records. 220 */ 221 if (rmap.rm_startblock != bno) 222 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 223 irec->br_startoff); 224 225 rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount; 226 if (rmap_end != bno + irec->br_blockcount) 227 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 228 irec->br_startoff); 229 230 /* Check the logical offsets. */ 231 if (rmap.rm_offset != irec->br_startoff) 232 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 233 irec->br_startoff); 234 235 rmap_end = (unsigned long long)rmap.rm_offset + rmap.rm_blockcount; 236 if (rmap_end != irec->br_startoff + irec->br_blockcount) 237 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 238 irec->br_startoff); 239 240 /* Check the owner */ 241 if (rmap.rm_owner != owner) 242 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 243 irec->br_startoff); 244 245 /* 246 * Check for discrepancies between the unwritten flag in the irec and 247 * the rmap. Note that the (in-memory) CoW fork distinguishes between 248 * unwritten and written extents, but we don't track that in the rmap 249 * records because the blocks are owned (on-disk) by the refcountbt, 250 * which doesn't track unwritten state. 251 */ 252 if (!!(irec->br_state == XFS_EXT_UNWRITTEN) != 253 !!(rmap.rm_flags & XFS_RMAP_UNWRITTEN)) 254 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 255 irec->br_startoff); 256 257 if (!!(info->whichfork == XFS_ATTR_FORK) != 258 !!(rmap.rm_flags & XFS_RMAP_ATTR_FORK)) 259 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 260 irec->br_startoff); 261 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK) 262 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 263 irec->br_startoff); 264 } 265 266 /* Make sure that we have rmapbt records for this COW fork extent. */ 267 STATIC void 268 xchk_bmap_xref_rmap_cow( 269 struct xchk_bmap_info *info, 270 struct xfs_bmbt_irec *irec, 271 xfs_agblock_t bno) 272 { 273 struct xfs_rmap_irec rmap; 274 unsigned long long rmap_end; 275 uint64_t owner = XFS_RMAP_OWN_COW; 276 277 if (xchk_skip_xref(info->sc->sm)) 278 return; 279 280 /* Find the rmap record for this irec. */ 281 if (!xchk_bmap_get_rmap(info, irec, bno, owner, &rmap)) 282 return; 283 284 /* 285 * CoW staging extents are owned by the refcount btree, so the rmap 286 * can start before and end after the physical space allocated to this 287 * mapping. There are no offsets to check. 288 */ 289 if (rmap.rm_startblock > bno) 290 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 291 irec->br_startoff); 292 293 rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount; 294 if (rmap_end < bno + irec->br_blockcount) 295 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 296 irec->br_startoff); 297 298 /* Check the owner */ 299 if (rmap.rm_owner != owner) 300 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 301 irec->br_startoff); 302 303 /* 304 * No flags allowed. Note that the (in-memory) CoW fork distinguishes 305 * between unwritten and written extents, but we don't track that in 306 * the rmap records because the blocks are owned (on-disk) by the 307 * refcountbt, which doesn't track unwritten state. 308 */ 309 if (rmap.rm_flags & XFS_RMAP_ATTR_FORK) 310 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 311 irec->br_startoff); 312 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK) 313 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 314 irec->br_startoff); 315 if (rmap.rm_flags & XFS_RMAP_UNWRITTEN) 316 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork, 317 irec->br_startoff); 318 } 319 320 /* Cross-reference a single rtdev extent record. */ 321 STATIC void 322 xchk_bmap_rt_iextent_xref( 323 struct xfs_inode *ip, 324 struct xchk_bmap_info *info, 325 struct xfs_bmbt_irec *irec) 326 { 327 struct xfs_owner_info oinfo; 328 xfs_rgblock_t rgbno; 329 int error; 330 331 error = xchk_rtgroup_init_existing(info->sc, 332 xfs_rtb_to_rgno(ip->i_mount, irec->br_startblock)); 333 if (!xchk_fblock_process_error(info->sc, info->whichfork, 334 irec->br_startoff, &error)) 335 return; 336 337 error = xchk_rtgroup_lock(info->sc, XCHK_RTGLOCK_ALL); 338 if (!xchk_fblock_process_error(info->sc, info->whichfork, 339 irec->br_startoff, &error)) 340 goto out_free; 341 342 xchk_xref_is_used_rt_space(info->sc, irec->br_startblock, 343 irec->br_blockcount); 344 345 if (!xfs_has_rtrmapbt(info->sc->mp)) 346 goto out_cur; 347 348 rgbno = xfs_rtb_to_rgbno(info->sc->mp, irec->br_startblock); 349 350 switch (info->whichfork) { 351 case XFS_DATA_FORK: 352 xchk_bmap_xref_rmap(info, irec, rgbno); 353 if (!xfs_is_reflink_inode(info->sc->ip)) { 354 xfs_rmap_inode_owner(&oinfo, info->sc->ip, 355 info->whichfork, irec->br_startoff); 356 xchk_xref_is_only_rt_owned_by(info->sc, rgbno, 357 irec->br_blockcount, &oinfo); 358 xchk_xref_is_not_rt_shared(info->sc, rgbno, 359 irec->br_blockcount); 360 } 361 xchk_xref_is_not_rt_cow_staging(info->sc, rgbno, 362 irec->br_blockcount); 363 break; 364 case XFS_COW_FORK: 365 xchk_bmap_xref_rmap_cow(info, irec, rgbno); 366 xchk_xref_is_only_rt_owned_by(info->sc, rgbno, 367 irec->br_blockcount, &XFS_RMAP_OINFO_COW); 368 xchk_xref_is_rt_cow_staging(info->sc, rgbno, 369 irec->br_blockcount); 370 xchk_xref_is_not_rt_shared(info->sc, rgbno, 371 irec->br_blockcount); 372 break; 373 } 374 out_cur: 375 xchk_rtgroup_btcur_free(info->sc); 376 out_free: 377 xchk_rtgroup_free(info->sc); 378 } 379 380 /* Cross-reference a single datadev extent record. */ 381 STATIC void 382 xchk_bmap_iextent_xref( 383 struct xfs_inode *ip, 384 struct xchk_bmap_info *info, 385 struct xfs_bmbt_irec *irec) 386 { 387 struct xfs_owner_info oinfo; 388 struct xfs_mount *mp = info->sc->mp; 389 xfs_agnumber_t agno; 390 xfs_agblock_t agbno; 391 xfs_extlen_t len; 392 int error; 393 394 agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock); 395 agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock); 396 len = irec->br_blockcount; 397 398 error = xchk_ag_init_existing(info->sc, agno); 399 if (!xchk_fblock_process_error(info->sc, info->whichfork, 400 irec->br_startoff, &error)) 401 goto out_free; 402 403 xchk_xref_is_used_space(info->sc, agbno, len); 404 xchk_xref_is_not_inode_chunk(info->sc, agbno, len); 405 switch (info->whichfork) { 406 case XFS_DATA_FORK: 407 xchk_bmap_xref_rmap(info, irec, agbno); 408 if (!xfs_is_reflink_inode(info->sc->ip)) { 409 xfs_rmap_inode_owner(&oinfo, info->sc->ip, 410 info->whichfork, irec->br_startoff); 411 xchk_xref_is_only_owned_by(info->sc, agbno, 412 irec->br_blockcount, &oinfo); 413 xchk_xref_is_not_shared(info->sc, agbno, 414 irec->br_blockcount); 415 } 416 xchk_xref_is_not_cow_staging(info->sc, agbno, 417 irec->br_blockcount); 418 break; 419 case XFS_ATTR_FORK: 420 xchk_bmap_xref_rmap(info, irec, agbno); 421 xfs_rmap_inode_owner(&oinfo, info->sc->ip, 422 info->whichfork, irec->br_startoff); 423 xchk_xref_is_only_owned_by(info->sc, agbno, irec->br_blockcount, 424 &oinfo); 425 xchk_xref_is_not_shared(info->sc, agbno, 426 irec->br_blockcount); 427 xchk_xref_is_not_cow_staging(info->sc, agbno, 428 irec->br_blockcount); 429 break; 430 case XFS_COW_FORK: 431 xchk_bmap_xref_rmap_cow(info, irec, agbno); 432 xchk_xref_is_only_owned_by(info->sc, agbno, irec->br_blockcount, 433 &XFS_RMAP_OINFO_COW); 434 xchk_xref_is_cow_staging(info->sc, agbno, 435 irec->br_blockcount); 436 xchk_xref_is_not_shared(info->sc, agbno, 437 irec->br_blockcount); 438 break; 439 } 440 441 out_free: 442 xchk_ag_free(info->sc); 443 } 444 445 /* 446 * Directories and attr forks should never have blocks that can't be addressed 447 * by a xfs_dablk_t. 448 */ 449 STATIC void 450 xchk_bmap_dirattr_extent( 451 struct xfs_inode *ip, 452 struct xchk_bmap_info *info, 453 struct xfs_bmbt_irec *irec) 454 { 455 xfs_fileoff_t off; 456 457 if (!S_ISDIR(VFS_I(ip)->i_mode) && info->whichfork != XFS_ATTR_FORK) 458 return; 459 460 if (!xfs_verify_dablk(irec->br_startoff)) 461 xchk_fblock_set_corrupt(info->sc, info->whichfork, 462 irec->br_startoff); 463 464 off = irec->br_startoff + irec->br_blockcount - 1; 465 if (!xfs_verify_dablk(off)) 466 xchk_fblock_set_corrupt(info->sc, info->whichfork, off); 467 } 468 469 /* Scrub a single extent record. */ 470 STATIC void 471 xchk_bmap_iextent( 472 struct xfs_inode *ip, 473 struct xchk_bmap_info *info, 474 struct xfs_bmbt_irec *irec) 475 { 476 struct xfs_mount *mp = info->sc->mp; 477 478 /* 479 * Check for out-of-order extents. This record could have come 480 * from the incore list, for which there is no ordering check. 481 */ 482 if (irec->br_startoff < info->prev_rec.br_startoff + 483 info->prev_rec.br_blockcount) 484 xchk_fblock_set_corrupt(info->sc, info->whichfork, 485 irec->br_startoff); 486 487 if (!xfs_verify_fileext(irec->br_startoff, irec->br_blockcount)) 488 xchk_fblock_set_corrupt(info->sc, info->whichfork, 489 irec->br_startoff); 490 491 xchk_bmap_dirattr_extent(ip, info, irec); 492 493 /* Make sure the extent points to a valid place. */ 494 if (info->is_rt && 495 !xfs_verify_rtbext(mp, irec->br_startblock, irec->br_blockcount)) 496 xchk_fblock_set_corrupt(info->sc, info->whichfork, 497 irec->br_startoff); 498 if (!info->is_rt && 499 !xfs_verify_fsbext(mp, irec->br_startblock, irec->br_blockcount)) 500 xchk_fblock_set_corrupt(info->sc, info->whichfork, 501 irec->br_startoff); 502 503 /* We don't allow unwritten extents on attr forks. */ 504 if (irec->br_state == XFS_EXT_UNWRITTEN && 505 info->whichfork == XFS_ATTR_FORK) 506 xchk_fblock_set_corrupt(info->sc, info->whichfork, 507 irec->br_startoff); 508 509 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 510 return; 511 512 if (info->is_rt) 513 xchk_bmap_rt_iextent_xref(ip, info, irec); 514 else 515 xchk_bmap_iextent_xref(ip, info, irec); 516 } 517 518 /* Scrub a bmbt record. */ 519 STATIC int 520 xchk_bmapbt_rec( 521 struct xchk_btree *bs, 522 const union xfs_btree_rec *rec) 523 { 524 struct xfs_bmbt_irec irec; 525 struct xfs_bmbt_irec iext_irec; 526 struct xfs_iext_cursor icur; 527 struct xchk_bmap_info *info = bs->private; 528 struct xfs_inode *ip = bs->cur->bc_ino.ip; 529 struct xfs_buf *bp = NULL; 530 struct xfs_btree_block *block; 531 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, info->whichfork); 532 uint64_t owner; 533 int i; 534 535 /* 536 * Check the owners of the btree blocks up to the level below 537 * the root since the verifiers don't do that. 538 */ 539 if (xfs_has_crc(bs->cur->bc_mp) && 540 bs->cur->bc_levels[0].ptr == 1) { 541 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) { 542 block = xfs_btree_get_block(bs->cur, i, &bp); 543 owner = be64_to_cpu(block->bb_u.l.bb_owner); 544 if (owner != I_INO(ip)) 545 xchk_fblock_set_corrupt(bs->sc, 546 info->whichfork, 0); 547 } 548 } 549 550 /* 551 * Check that the incore extent tree contains an extent that matches 552 * this one exactly. We validate those cached bmaps later, so we don't 553 * need to check them here. If the incore extent tree was just loaded 554 * from disk by the scrubber, we assume that its contents match what's 555 * on disk (we still hold the ILOCK) and skip the equivalence check. 556 */ 557 if (!info->was_loaded) 558 return 0; 559 560 xfs_bmbt_disk_get_all(&rec->bmbt, &irec); 561 if (xfs_bmap_validate_extent(ip, info->whichfork, &irec) != NULL) { 562 xchk_fblock_set_corrupt(bs->sc, info->whichfork, 563 irec.br_startoff); 564 return 0; 565 } 566 567 if (!xfs_iext_lookup_extent(ip, ifp, irec.br_startoff, &icur, 568 &iext_irec) || 569 irec.br_startoff != iext_irec.br_startoff || 570 irec.br_startblock != iext_irec.br_startblock || 571 irec.br_blockcount != iext_irec.br_blockcount || 572 irec.br_state != iext_irec.br_state) 573 xchk_fblock_set_corrupt(bs->sc, info->whichfork, 574 irec.br_startoff); 575 return 0; 576 } 577 578 /* Scan the btree records. */ 579 STATIC int 580 xchk_bmap_btree( 581 struct xfs_scrub *sc, 582 int whichfork, 583 struct xchk_bmap_info *info) 584 { 585 struct xfs_owner_info oinfo; 586 struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, whichfork); 587 struct xfs_mount *mp = sc->mp; 588 struct xfs_inode *ip = sc->ip; 589 struct xfs_btree_cur *cur; 590 int error; 591 592 /* Load the incore bmap cache if it's not loaded. */ 593 info->was_loaded = !xfs_need_iread_extents(ifp); 594 595 error = xfs_iread_extents(sc->tp, ip, whichfork); 596 if (!xchk_fblock_process_error(sc, whichfork, 0, &error)) 597 goto out; 598 599 /* Check the btree structure. */ 600 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork); 601 xfs_rmap_inode_bmbt_owner(&oinfo, ip, whichfork); 602 error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info); 603 xfs_btree_del_cursor(cur, error); 604 out: 605 return error; 606 } 607 608 struct xchk_bmap_check_rmap_info { 609 struct xfs_scrub *sc; 610 int whichfork; 611 struct xfs_iext_cursor icur; 612 }; 613 614 /* Can we find bmaps that fit this rmap? */ 615 STATIC int 616 xchk_bmap_check_rmap( 617 struct xfs_btree_cur *cur, 618 const struct xfs_rmap_irec *rec, 619 void *priv) 620 { 621 struct xfs_bmbt_irec irec; 622 struct xfs_rmap_irec check_rec; 623 struct xchk_bmap_check_rmap_info *sbcri = priv; 624 struct xfs_ifork *ifp; 625 struct xfs_scrub *sc = sbcri->sc; 626 bool have_map; 627 int error = 0; 628 629 if (xchk_should_terminate(sc, &error)) 630 return error; 631 632 /* Is this even the right fork? */ 633 if (rec->rm_owner != I_INO(sc->ip)) 634 return 0; 635 if ((sbcri->whichfork == XFS_ATTR_FORK) ^ 636 !!(rec->rm_flags & XFS_RMAP_ATTR_FORK)) 637 return 0; 638 if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK) 639 return 0; 640 641 /* Now look up the bmbt record. */ 642 ifp = xfs_ifork_ptr(sc->ip, sbcri->whichfork); 643 if (!ifp) { 644 xchk_fblock_set_corrupt(sc, sbcri->whichfork, 645 rec->rm_offset); 646 goto out; 647 } 648 have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset, 649 &sbcri->icur, &irec); 650 if (!have_map) 651 xchk_fblock_set_corrupt(sc, sbcri->whichfork, 652 rec->rm_offset); 653 /* 654 * bmap extent record lengths are constrained to 2^21 blocks in length 655 * because of space constraints in the on-disk metadata structure. 656 * However, rmap extent record lengths are constrained only by AG 657 * length, so we have to loop through the bmbt to make sure that the 658 * entire rmap is covered by bmbt records. 659 */ 660 check_rec = *rec; 661 while (have_map) { 662 if (xchk_should_terminate(sc, &error)) 663 return error; 664 665 if (irec.br_startoff != check_rec.rm_offset) 666 xchk_fblock_set_corrupt(sc, sbcri->whichfork, 667 check_rec.rm_offset); 668 if (irec.br_startblock != 669 xfs_gbno_to_fsb(cur->bc_group, check_rec.rm_startblock)) 670 xchk_fblock_set_corrupt(sc, sbcri->whichfork, 671 check_rec.rm_offset); 672 if (irec.br_blockcount > check_rec.rm_blockcount) 673 xchk_fblock_set_corrupt(sc, sbcri->whichfork, 674 check_rec.rm_offset); 675 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 676 break; 677 check_rec.rm_startblock += irec.br_blockcount; 678 check_rec.rm_offset += irec.br_blockcount; 679 check_rec.rm_blockcount -= irec.br_blockcount; 680 if (check_rec.rm_blockcount == 0) 681 break; 682 have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec); 683 if (!have_map) 684 xchk_fblock_set_corrupt(sc, sbcri->whichfork, 685 check_rec.rm_offset); 686 } 687 688 out: 689 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 690 return -ECANCELED; 691 return 0; 692 } 693 694 /* Make sure each rmap has a corresponding bmbt entry. */ 695 STATIC int 696 xchk_bmap_check_ag_rmaps( 697 struct xfs_scrub *sc, 698 int whichfork, 699 struct xfs_perag *pag) 700 { 701 struct xchk_bmap_check_rmap_info sbcri; 702 struct xfs_btree_cur *cur; 703 struct xfs_buf *agf; 704 int error; 705 706 error = xfs_alloc_read_agf(pag, sc->tp, 0, &agf); 707 if (error) 708 return error; 709 710 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, pag); 711 712 sbcri.sc = sc; 713 sbcri.whichfork = whichfork; 714 error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri); 715 if (error == -ECANCELED) 716 error = 0; 717 718 xfs_btree_del_cursor(cur, error); 719 xfs_trans_brelse(sc->tp, agf); 720 return error; 721 } 722 723 /* Make sure each rt rmap has a corresponding bmbt entry. */ 724 STATIC int 725 xchk_bmap_check_rt_rmaps( 726 struct xfs_scrub *sc, 727 struct xfs_rtgroup *rtg) 728 { 729 struct xchk_bmap_check_rmap_info sbcri; 730 struct xfs_btree_cur *cur; 731 int error; 732 733 xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP); 734 cur = xfs_rtrmapbt_init_cursor(sc->tp, rtg); 735 736 sbcri.sc = sc; 737 sbcri.whichfork = XFS_DATA_FORK; 738 error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri); 739 if (error == -ECANCELED) 740 error = 0; 741 742 xfs_btree_del_cursor(cur, error); 743 xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_RMAP); 744 return error; 745 } 746 747 /* 748 * Decide if we want to scan the reverse mappings to determine if the attr 749 * fork /really/ has zero space mappings. 750 */ 751 STATIC bool 752 xchk_bmap_check_empty_attrfork( 753 struct xfs_inode *ip) 754 { 755 struct xfs_ifork *ifp = &ip->i_af; 756 757 /* 758 * If the dinode repair found a bad attr fork, it will reset the fork 759 * to extents format with zero records and wait for the this scrubber 760 * to reconstruct the block mappings. If the fork is not in this 761 * state, then the fork cannot have been zapped. 762 */ 763 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS || ifp->if_nextents != 0) 764 return false; 765 766 /* 767 * Files can have an attr fork in EXTENTS format with zero records for 768 * several reasons: 769 * 770 * a) an attr set created a fork but ran out of space 771 * b) attr replace deleted an old attr but failed during the set step 772 * c) the data fork was in btree format when all attrs were deleted, so 773 * the fork was left in place 774 * d) the inode repair code zapped the fork 775 * 776 * Only in case (d) do we want to scan the rmapbt to see if we need to 777 * rebuild the attr fork. The fork zap code clears all DAC permission 778 * bits and zeroes the uid and gid, so avoid the scan if any of those 779 * three conditions are not met. 780 */ 781 if ((VFS_I(ip)->i_mode & 0777) != 0) 782 return false; 783 if (!uid_eq(VFS_I(ip)->i_uid, GLOBAL_ROOT_UID)) 784 return false; 785 if (!gid_eq(VFS_I(ip)->i_gid, GLOBAL_ROOT_GID)) 786 return false; 787 788 return true; 789 } 790 791 /* 792 * Decide if we want to scan the reverse mappings to determine if the data 793 * fork /really/ has zero space mappings. 794 */ 795 STATIC bool 796 xchk_bmap_check_empty_datafork( 797 struct xfs_inode *ip) 798 { 799 struct xfs_ifork *ifp = &ip->i_df; 800 801 /* 802 * If the dinode repair found a bad data fork, it will reset the fork 803 * to extents format with zero records and wait for the this scrubber 804 * to reconstruct the block mappings. If the fork is not in this 805 * state, then the fork cannot have been zapped. 806 */ 807 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS || ifp->if_nextents != 0) 808 return false; 809 810 /* 811 * If we encounter an empty data fork along with evidence that the fork 812 * might not really be empty, we need to scan the reverse mappings to 813 * decide if we're going to rebuild the fork. Data forks with nonzero 814 * file size are scanned. 815 */ 816 return i_size_read(VFS_I(ip)) != 0; 817 } 818 819 /* 820 * Decide if we want to walk every rmap btree in the fs to make sure that each 821 * rmap for this file fork has corresponding bmbt entries. 822 */ 823 static bool 824 xchk_bmap_want_check_rmaps( 825 struct xchk_bmap_info *info) 826 { 827 struct xfs_scrub *sc = info->sc; 828 829 if (!xfs_has_rmapbt(sc->mp)) 830 return false; 831 if (info->whichfork == XFS_COW_FORK) 832 return false; 833 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 834 return false; 835 836 if (info->whichfork == XFS_ATTR_FORK) 837 return xchk_bmap_check_empty_attrfork(sc->ip); 838 839 return xchk_bmap_check_empty_datafork(sc->ip); 840 } 841 842 /* Make sure each rmap has a corresponding bmbt entry. */ 843 STATIC int 844 xchk_bmap_check_rmaps( 845 struct xfs_scrub *sc, 846 int whichfork) 847 { 848 struct xfs_perag *pag = NULL; 849 int error; 850 851 if (xfs_ifork_is_realtime(sc->ip, whichfork)) { 852 struct xfs_rtgroup *rtg = NULL; 853 854 while ((rtg = xfs_rtgroup_next(sc->mp, rtg))) { 855 error = xchk_bmap_check_rt_rmaps(sc, rtg); 856 if (error || 857 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) { 858 xfs_rtgroup_rele(rtg); 859 return error; 860 } 861 } 862 863 return 0; 864 } 865 866 while ((pag = xfs_perag_next(sc->mp, pag))) { 867 error = xchk_bmap_check_ag_rmaps(sc, whichfork, pag); 868 if (error || 869 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) { 870 xfs_perag_rele(pag); 871 return error; 872 } 873 } 874 875 return 0; 876 } 877 878 /* Scrub a delalloc reservation from the incore extent map tree. */ 879 STATIC void 880 xchk_bmap_iextent_delalloc( 881 struct xfs_inode *ip, 882 struct xchk_bmap_info *info, 883 struct xfs_bmbt_irec *irec) 884 { 885 /* 886 * Check for out-of-order extents. This record could have come 887 * from the incore list, for which there is no ordering check. 888 */ 889 if (irec->br_startoff < info->prev_rec.br_startoff + 890 info->prev_rec.br_blockcount) 891 xchk_fblock_set_corrupt(info->sc, info->whichfork, 892 irec->br_startoff); 893 894 if (!xfs_verify_fileext(irec->br_startoff, irec->br_blockcount)) 895 xchk_fblock_set_corrupt(info->sc, info->whichfork, 896 irec->br_startoff); 897 898 /* Make sure the extent points to a valid place. */ 899 if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN) 900 xchk_fblock_set_corrupt(info->sc, info->whichfork, 901 irec->br_startoff); 902 } 903 904 /* Decide if this individual fork mapping is ok. */ 905 static bool 906 xchk_bmap_iext_mapping( 907 struct xchk_bmap_info *info, 908 const struct xfs_bmbt_irec *irec) 909 { 910 /* There should never be a "hole" extent in either extent list. */ 911 if (irec->br_startblock == HOLESTARTBLOCK) 912 return false; 913 if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN) 914 return false; 915 return true; 916 } 917 918 /* Are these two mappings contiguous with each other? */ 919 static inline bool 920 xchk_are_bmaps_contiguous( 921 const struct xchk_bmap_info *info, 922 const struct xfs_bmbt_irec *b1, 923 const struct xfs_bmbt_irec *b2) 924 { 925 struct xfs_mount *mp = info->sc->mp; 926 927 /* Don't try to combine unallocated mappings. */ 928 if (!xfs_bmap_is_real_extent(b1)) 929 return false; 930 if (!xfs_bmap_is_real_extent(b2)) 931 return false; 932 933 /* Does b2 come right after b1 in the logical and physical range? */ 934 if (b1->br_startoff + b1->br_blockcount != b2->br_startoff) 935 return false; 936 if (b1->br_startblock + b1->br_blockcount != b2->br_startblock) 937 return false; 938 if (b1->br_state != b2->br_state) 939 return false; 940 941 /* 942 * Don't combine bmaps that would cross rtgroup boundaries. This is a 943 * valid state, but if combined they will fail rtb extent checks. 944 */ 945 if (info->is_rt && xfs_has_rtgroups(mp)) { 946 if (xfs_rtb_to_rgno(mp, b1->br_startblock) != 947 xfs_rtb_to_rgno(mp, b2->br_startblock)) 948 return false; 949 } 950 951 return true; 952 } 953 954 /* 955 * Walk the incore extent records, accumulating consecutive contiguous records 956 * into a single incore mapping. Returns true if @irec has been set to a 957 * mapping or false if there are no more mappings. Caller must ensure that 958 * @info.icur is zeroed before the first call. 959 */ 960 static bool 961 xchk_bmap_iext_iter( 962 struct xchk_bmap_info *info, 963 struct xfs_bmbt_irec *irec) 964 { 965 struct xfs_bmbt_irec got; 966 struct xfs_ifork *ifp; 967 unsigned int nr = 0; 968 969 ifp = xfs_ifork_ptr(info->sc->ip, info->whichfork); 970 971 /* Advance to the next iextent record and check the mapping. */ 972 xfs_iext_next(ifp, &info->icur); 973 if (!xfs_iext_get_extent(ifp, &info->icur, irec)) 974 return false; 975 976 if (!xchk_bmap_iext_mapping(info, irec)) { 977 xchk_fblock_set_corrupt(info->sc, info->whichfork, 978 irec->br_startoff); 979 return false; 980 } 981 nr++; 982 983 /* 984 * Iterate subsequent iextent records and merge them with the one 985 * that we just read, if possible. 986 */ 987 while (xfs_iext_peek_next_extent(ifp, &info->icur, &got)) { 988 if (!xchk_are_bmaps_contiguous(info, irec, &got)) 989 break; 990 991 if (!xchk_bmap_iext_mapping(info, &got)) { 992 xchk_fblock_set_corrupt(info->sc, info->whichfork, 993 got.br_startoff); 994 return false; 995 } 996 nr++; 997 998 irec->br_blockcount += got.br_blockcount; 999 xfs_iext_next(ifp, &info->icur); 1000 } 1001 1002 /* 1003 * If the merged mapping could be expressed with fewer bmbt records 1004 * than we actually found, notify the user that this fork could be 1005 * optimized. CoW forks only exist in memory so we ignore them. 1006 */ 1007 if (nr > 1 && info->whichfork != XFS_COW_FORK && 1008 howmany_64(irec->br_blockcount, XFS_MAX_BMBT_EXTLEN) < nr) 1009 xchk_ino_set_preen(info->sc, I_INO(info->sc->ip)); 1010 1011 return true; 1012 } 1013 1014 /* 1015 * Scrub an inode fork's block mappings. 1016 * 1017 * First we scan every record in every btree block, if applicable. 1018 * Then we unconditionally scan the incore extent cache. 1019 */ 1020 STATIC int 1021 xchk_bmap( 1022 struct xfs_scrub *sc, 1023 int whichfork) 1024 { 1025 struct xfs_bmbt_irec irec; 1026 struct xchk_bmap_info info = { NULL }; 1027 struct xfs_mount *mp = sc->mp; 1028 struct xfs_inode *ip = sc->ip; 1029 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 1030 xfs_fileoff_t endoff; 1031 int error = 0; 1032 1033 /* Non-existent forks can be ignored. */ 1034 if (!ifp) 1035 return -ENOENT; 1036 1037 info.is_rt = xfs_ifork_is_realtime(ip, whichfork); 1038 info.whichfork = whichfork; 1039 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip); 1040 info.sc = sc; 1041 1042 switch (whichfork) { 1043 case XFS_COW_FORK: 1044 /* No CoW forks filesystem doesn't support out of place writes */ 1045 if (!xfs_has_reflink(mp) && !xfs_has_zoned(mp)) { 1046 xchk_ip_set_corrupt(sc, sc->ip); 1047 return 0; 1048 } 1049 break; 1050 case XFS_ATTR_FORK: 1051 /* 1052 * "attr" means that an attr fork was created at some point in 1053 * the life of this filesystem. "attr2" means that inodes have 1054 * variable-sized data/attr fork areas. Hence we only check 1055 * attr here. 1056 */ 1057 if (!xfs_has_attr(mp)) 1058 xchk_ip_set_corrupt(sc, sc->ip); 1059 break; 1060 default: 1061 ASSERT(whichfork == XFS_DATA_FORK); 1062 break; 1063 } 1064 1065 /* Check the fork values */ 1066 switch (ifp->if_format) { 1067 case XFS_DINODE_FMT_UUID: 1068 case XFS_DINODE_FMT_DEV: 1069 case XFS_DINODE_FMT_LOCAL: 1070 case XFS_DINODE_FMT_META_BTREE: 1071 /* No mappings to check. */ 1072 if (whichfork == XFS_COW_FORK) 1073 xchk_fblock_set_corrupt(sc, whichfork, 0); 1074 return 0; 1075 case XFS_DINODE_FMT_EXTENTS: 1076 break; 1077 case XFS_DINODE_FMT_BTREE: 1078 if (whichfork == XFS_COW_FORK) { 1079 xchk_fblock_set_corrupt(sc, whichfork, 0); 1080 return 0; 1081 } 1082 1083 error = xchk_bmap_btree(sc, whichfork, &info); 1084 if (error) 1085 return error; 1086 break; 1087 default: 1088 xchk_fblock_set_corrupt(sc, whichfork, 0); 1089 return 0; 1090 } 1091 1092 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 1093 return 0; 1094 1095 /* Find the offset of the last extent in the mapping. */ 1096 error = xfs_bmap_last_offset(ip, &endoff, whichfork); 1097 if (!xchk_fblock_process_error(sc, whichfork, 0, &error)) 1098 return error; 1099 1100 /* 1101 * Scrub extent records. We use a special iterator function here that 1102 * combines adjacent mappings if they are logically and physically 1103 * contiguous. For large allocations that require multiple bmbt 1104 * records, this reduces the number of cross-referencing calls, which 1105 * reduces runtime. Cross referencing with the rmap is simpler because 1106 * the rmap must match the combined mapping exactly. 1107 */ 1108 while (xchk_bmap_iext_iter(&info, &irec)) { 1109 if (xchk_should_terminate(sc, &error)) 1110 return error; 1111 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 1112 return 0; 1113 1114 if (irec.br_startoff >= endoff) { 1115 xchk_fblock_set_corrupt(sc, whichfork, 1116 irec.br_startoff); 1117 return 0; 1118 } 1119 1120 if (isnullstartblock(irec.br_startblock)) 1121 xchk_bmap_iextent_delalloc(ip, &info, &irec); 1122 else 1123 xchk_bmap_iextent(ip, &info, &irec); 1124 memcpy(&info.prev_rec, &irec, sizeof(struct xfs_bmbt_irec)); 1125 } 1126 1127 if (xchk_bmap_want_check_rmaps(&info)) { 1128 error = xchk_bmap_check_rmaps(sc, whichfork); 1129 if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error)) 1130 return error; 1131 } 1132 1133 return 0; 1134 } 1135 1136 /* Scrub an inode's data fork. */ 1137 int 1138 xchk_bmap_data( 1139 struct xfs_scrub *sc) 1140 { 1141 int error; 1142 1143 if (xchk_file_looks_zapped(sc, XFS_SICK_INO_BMBTD_ZAPPED)) { 1144 xchk_ip_set_corrupt(sc, sc->ip); 1145 return 0; 1146 } 1147 1148 error = xchk_bmap(sc, XFS_DATA_FORK); 1149 if (error) 1150 return error; 1151 1152 /* If the data fork is clean, it is clearly not zapped. */ 1153 xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTD_ZAPPED); 1154 return 0; 1155 } 1156 1157 /* Scrub an inode's attr fork. */ 1158 int 1159 xchk_bmap_attr( 1160 struct xfs_scrub *sc) 1161 { 1162 int error; 1163 1164 /* 1165 * If the attr fork has been zapped, it's possible that forkoff was 1166 * reset to zero and hence sc->ip->i_afp is NULL. We don't want the 1167 * NULL ifp check in xchk_bmap to conclude that the attr fork is ok, 1168 * so short circuit that logic by setting the corruption flag and 1169 * returning immediately. 1170 */ 1171 if (xchk_file_looks_zapped(sc, XFS_SICK_INO_BMBTA_ZAPPED)) { 1172 xchk_ip_set_corrupt(sc, sc->ip); 1173 return 0; 1174 } 1175 1176 error = xchk_bmap(sc, XFS_ATTR_FORK); 1177 /* A repaired, empty attr fork no longer has mappings to check. */ 1178 if (error == -ENOENT && (sc->flags & XREP_ALREADY_FIXED)) { 1179 xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTA_ZAPPED); 1180 return 0; 1181 } 1182 if (error) 1183 return error; 1184 1185 /* If the attr fork is clean, it is clearly not zapped. */ 1186 xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTA_ZAPPED); 1187 return 0; 1188 } 1189 1190 /* Scrub an inode's CoW fork. */ 1191 int 1192 xchk_bmap_cow( 1193 struct xfs_scrub *sc) 1194 { 1195 return xchk_bmap(sc, XFS_COW_FORK); 1196 } 1197