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_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_mount.h" 13 #include "xfs_trans.h" 14 #include "xfs_ag.h" 15 #include "xfs_btree.h" 16 #include "xfs_rmap.h" 17 #include "xfs_refcount.h" 18 #include "scrub/scrub.h" 19 #include "scrub/common.h" 20 #include "scrub/btree.h" 21 #include "scrub/trace.h" 22 #include "scrub/repair.h" 23 #include "scrub/refcount.h" 24 25 /* 26 * Set us up to scrub reference count btrees. 27 */ 28 int 29 xchk_setup_ag_refcountbt( 30 struct xfs_scrub *sc) 31 { 32 if (xchk_need_intent_drain(sc)) 33 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN); 34 35 if (xchk_could_repair(sc)) { 36 int error; 37 38 error = xrep_setup_ag_refcountbt(sc); 39 if (error) 40 return error; 41 } 42 43 return xchk_setup_ag_btree(sc, false); 44 } 45 46 /* Reference count btree scrubber. */ 47 48 /* 49 * Confirming Reference Counts via Reverse Mappings 50 * 51 * We want to count the reverse mappings overlapping a refcount record 52 * (bno, len, refcount), allowing for the possibility that some of the 53 * overlap may come from smaller adjoining reverse mappings, while some 54 * comes from single extents which overlap the range entirely. The 55 * outer loop is as follows: 56 * 57 * 1. For all reverse mappings overlapping the refcount extent, 58 * a. If a given rmap completely overlaps, mark it as seen. 59 * b. Otherwise, record the fragment (in agbno order) for later 60 * processing. 61 * 62 * Once we've seen all the rmaps, we know that for all blocks in the 63 * refcount record we want to find $refcount owners and we've already 64 * visited $seen extents that overlap all the blocks. Therefore, we 65 * need to find ($refcount - $seen) owners for every block in the 66 * extent; call that quantity $target_nr. Proceed as follows: 67 * 68 * 2. Pull the first $target_nr fragments from the list; all of them 69 * should start at or before the start of the extent. 70 * Call this subset of fragments the working set. 71 * 3. Until there are no more unprocessed fragments, 72 * a. Find the shortest fragments in the set and remove them. 73 * b. Note the block number of the end of these fragments. 74 * c. Pull the same number of fragments from the list. All of these 75 * fragments should start at the block number recorded in the 76 * previous step. 77 * d. Put those fragments in the set. 78 * 4. Check that there are $target_nr fragments remaining in the list, 79 * and that they all end at or beyond the end of the refcount extent. 80 * 81 * If the refcount is correct, all the check conditions in the algorithm 82 * should always hold true. If not, the refcount is incorrect. 83 */ 84 /* 85 * Decide if the given rmap is large enough that we can redeem it 86 * towards refcount verification now, or if it's a fragment, in 87 * which case we'll hang onto it in the hopes that we'll later 88 * discover that we've collected exactly the correct number of 89 * fragments as the refcountbt says we should have. 90 */ 91 int 92 xchk_refcountbt_rmap_check( 93 struct xfs_btree_cur *cur, 94 const struct xfs_rmap_irec *rec, 95 void *priv) 96 { 97 struct xchk_refcnt_check *refchk = priv; 98 struct xchk_refcnt_frag *frag; 99 xfs_agblock_t rm_last; 100 xfs_agblock_t rc_last; 101 int error = 0; 102 103 if (xchk_should_terminate(refchk->sc, &error)) 104 return error; 105 106 rm_last = rec->rm_startblock + rec->rm_blockcount - 1; 107 rc_last = refchk->bno + refchk->len - 1; 108 109 /* Confirm that a single-owner refc extent is a CoW stage. */ 110 if (refchk->refcount == 1 && rec->rm_owner != XFS_RMAP_OWN_COW) { 111 xchk_btree_xref_set_corrupt(refchk->sc, cur, 0); 112 return 0; 113 } 114 115 /* 116 * The refcount btree should only have records for written file data 117 * blocks or for COW staging blocks. 118 */ 119 if ((XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) && 120 rec->rm_owner != XFS_RMAP_OWN_COW) || 121 (rec->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK | 122 XFS_RMAP_UNWRITTEN))) { 123 xchk_btree_xref_set_corrupt(refchk->sc, cur, 0); 124 return 0; 125 } 126 127 if (rec->rm_startblock <= refchk->bno && rm_last >= rc_last) { 128 /* 129 * The rmap overlaps the refcount record, so we can confirm 130 * one refcount owner seen. 131 */ 132 refchk->seen++; 133 } else { 134 /* 135 * This rmap covers only part of the refcount record, so 136 * save the fragment for later processing. If the rmapbt 137 * is healthy each rmap_irec we see will be in agbno order 138 * so we don't need insertion sort here. 139 */ 140 frag = kmalloc_obj(struct xchk_refcnt_frag, XCHK_GFP_FLAGS); 141 if (!frag) 142 return -ENOMEM; 143 memcpy(&frag->rm, rec, sizeof(frag->rm)); 144 list_add_tail(&frag->list, &refchk->fragments); 145 } 146 147 return 0; 148 } 149 150 /* 151 * Given a bunch of rmap fragments, iterate through them, keeping 152 * a running tally of the refcount. If this ever deviates from 153 * what we expect (which is the refcountbt's refcount minus the 154 * number of extents that totally covered the refcountbt extent), 155 * we have a refcountbt error. 156 */ 157 void 158 xchk_refcountbt_process_rmap_fragments( 159 struct xchk_refcnt_check *refchk) 160 { 161 struct list_head worklist; 162 struct xchk_refcnt_frag *frag; 163 struct xchk_refcnt_frag *n; 164 xfs_agblock_t bno; 165 xfs_agblock_t rbno; 166 xfs_agblock_t next_rbno; 167 xfs_nlink_t nr; 168 xfs_nlink_t target_nr; 169 170 target_nr = refchk->refcount - refchk->seen; 171 if (target_nr == 0) { 172 if (!list_empty(&refchk->fragments)) 173 refchk->seen = 0; 174 return; 175 } 176 177 /* 178 * There are (refchk->rc.rc_refcount - refchk->nr refcount) 179 * references we haven't found yet. Pull that many off the 180 * fragment list and figure out where the smallest rmap ends 181 * (and therefore the next rmap should start). All the rmaps 182 * we pull off should start at or before the beginning of the 183 * refcount record's range. 184 */ 185 INIT_LIST_HEAD(&worklist); 186 rbno = NULLAGBLOCK; 187 188 /* Make sure the fragments actually /are/ in agbno order. */ 189 bno = 0; 190 list_for_each_entry(frag, &refchk->fragments, list) { 191 if (frag->rm.rm_startblock < bno) 192 goto done; 193 bno = frag->rm.rm_startblock; 194 } 195 196 /* 197 * Find all the rmaps that start at or before the refc extent, 198 * and put them on the worklist. 199 */ 200 nr = 0; 201 list_for_each_entry_safe(frag, n, &refchk->fragments, list) { 202 if (frag->rm.rm_startblock > refchk->bno || nr > target_nr) 203 break; 204 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount; 205 if (bno < rbno) 206 rbno = bno; 207 list_move_tail(&frag->list, &worklist); 208 nr++; 209 } 210 211 /* 212 * We should have found exactly $target_nr rmap fragments starting 213 * at or before the refcount extent. 214 */ 215 if (nr != target_nr) 216 goto done; 217 218 while (!list_empty(&refchk->fragments)) { 219 /* Discard any fragments ending at rbno from the worklist. */ 220 nr = 0; 221 next_rbno = NULLAGBLOCK; 222 list_for_each_entry_safe(frag, n, &worklist, list) { 223 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount; 224 if (bno != rbno) { 225 if (bno < next_rbno) 226 next_rbno = bno; 227 continue; 228 } 229 list_del(&frag->list); 230 kfree(frag); 231 nr++; 232 } 233 234 /* Try to add nr rmaps starting at rbno to the worklist. */ 235 list_for_each_entry_safe(frag, n, &refchk->fragments, list) { 236 bno = frag->rm.rm_startblock + frag->rm.rm_blockcount; 237 if (frag->rm.rm_startblock != rbno) 238 goto done; 239 list_move_tail(&frag->list, &worklist); 240 if (next_rbno > bno) 241 next_rbno = bno; 242 nr--; 243 if (nr == 0) 244 break; 245 } 246 247 /* 248 * If we get here and nr > 0, this means that we added fewer 249 * items to the worklist than we discarded because the fragment 250 * list ran out of items. Therefore, we cannot maintain the 251 * required refcount. Something is wrong, so we're done. 252 */ 253 if (nr) 254 goto done; 255 256 rbno = next_rbno; 257 } 258 259 /* 260 * Make sure the last extent we processed ends at or beyond 261 * the end of the refcount extent. 262 */ 263 if (rbno < refchk->bno + refchk->len) 264 goto done; 265 266 /* Actually record us having seen the remaining refcount. */ 267 refchk->seen = refchk->refcount; 268 done: 269 /* Delete fragments and work list. */ 270 list_for_each_entry_safe(frag, n, &worklist, list) { 271 list_del(&frag->list); 272 kfree(frag); 273 } 274 list_for_each_entry_safe(frag, n, &refchk->fragments, list) { 275 list_del(&frag->list); 276 kfree(frag); 277 } 278 } 279 280 /* Use the rmap entries covering this extent to verify the refcount. */ 281 STATIC void 282 xchk_refcountbt_xref_rmap( 283 struct xfs_scrub *sc, 284 const struct xfs_refcount_irec *irec) 285 { 286 struct xchk_refcnt_check refchk = { 287 .sc = sc, 288 .bno = irec->rc_startblock, 289 .len = irec->rc_blockcount, 290 .refcount = irec->rc_refcount, 291 .seen = 0, 292 }; 293 struct xfs_rmap_irec low; 294 struct xfs_rmap_irec high; 295 struct xchk_refcnt_frag *frag; 296 struct xchk_refcnt_frag *n; 297 int error; 298 299 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm)) 300 return; 301 302 /* Cross-reference with the rmapbt to confirm the refcount. */ 303 memset(&low, 0, sizeof(low)); 304 low.rm_startblock = irec->rc_startblock; 305 memset(&high, 0xFF, sizeof(high)); 306 high.rm_startblock = irec->rc_startblock + irec->rc_blockcount - 1; 307 308 INIT_LIST_HEAD(&refchk.fragments); 309 error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high, 310 &xchk_refcountbt_rmap_check, &refchk); 311 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur)) 312 goto out_free; 313 314 xchk_refcountbt_process_rmap_fragments(&refchk); 315 if (irec->rc_refcount != refchk.seen) { 316 trace_xchk_refcount_incorrect(sc->sa.pag, irec, refchk.seen); 317 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 318 } 319 320 out_free: 321 list_for_each_entry_safe(frag, n, &refchk.fragments, list) { 322 list_del(&frag->list); 323 kfree(frag); 324 } 325 } 326 327 /* Cross-reference with the other btrees. */ 328 STATIC void 329 xchk_refcountbt_xref( 330 struct xfs_scrub *sc, 331 const struct xfs_refcount_irec *irec) 332 { 333 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 334 return; 335 336 xchk_xref_is_used_space(sc, irec->rc_startblock, irec->rc_blockcount); 337 xchk_xref_is_not_inode_chunk(sc, irec->rc_startblock, 338 irec->rc_blockcount); 339 xchk_refcountbt_xref_rmap(sc, irec); 340 } 341 342 struct xchk_refcbt_records { 343 /* Previous refcount record. */ 344 struct xfs_refcount_irec prev_rec; 345 346 /* The next AG block where we aren't expecting shared extents. */ 347 xfs_agblock_t next_unshared_agbno; 348 349 /* Number of CoW blocks we expect. */ 350 xfs_agblock_t cow_blocks; 351 352 /* Was the last record a shared or CoW staging extent? */ 353 enum xfs_refc_domain prev_domain; 354 }; 355 356 int 357 xchk_refcountbt_rmap_check_gap( 358 struct xfs_btree_cur *cur, 359 const struct xfs_rmap_irec *rec, 360 void *priv) 361 { 362 xfs_agblock_t *next_bno = priv; 363 364 if (*next_bno != NULLAGBLOCK && rec->rm_startblock < *next_bno) 365 return -ECANCELED; 366 367 *next_bno = rec->rm_startblock + rec->rm_blockcount; 368 return 0; 369 } 370 371 /* 372 * Make sure that a gap in the reference count records does not correspond to 373 * overlapping records (i.e. shared extents) in the reverse mappings. 374 */ 375 static inline void 376 xchk_refcountbt_xref_gaps( 377 struct xfs_scrub *sc, 378 struct xchk_refcbt_records *rrc, 379 xfs_agblock_t bno) 380 { 381 struct xfs_rmap_irec low; 382 struct xfs_rmap_irec high; 383 xfs_agblock_t next_bno = NULLAGBLOCK; 384 int error; 385 386 if (bno <= rrc->next_unshared_agbno || !sc->sa.rmap_cur || 387 xchk_skip_xref(sc->sm)) 388 return; 389 390 memset(&low, 0, sizeof(low)); 391 low.rm_startblock = rrc->next_unshared_agbno; 392 memset(&high, 0xFF, sizeof(high)); 393 high.rm_startblock = bno - 1; 394 395 error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high, 396 xchk_refcountbt_rmap_check_gap, &next_bno); 397 if (error == -ECANCELED) 398 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 399 else 400 xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur); 401 } 402 403 static inline bool 404 xchk_refcount_mergeable( 405 struct xchk_refcbt_records *rrc, 406 const struct xfs_refcount_irec *r2) 407 { 408 const struct xfs_refcount_irec *r1 = &rrc->prev_rec; 409 410 /* Ignore if prev_rec is not yet initialized. */ 411 if (r1->rc_blockcount == 0) 412 return false; 413 414 if (r1->rc_domain != r2->rc_domain) 415 return false; 416 if (r1->rc_startblock + r1->rc_blockcount != r2->rc_startblock) 417 return false; 418 if (r1->rc_refcount != r2->rc_refcount) 419 return false; 420 if ((unsigned long long)r1->rc_blockcount + r2->rc_blockcount > 421 XFS_REFC_LEN_MAX) 422 return false; 423 424 return true; 425 } 426 427 /* Flag failures for records that could be merged. */ 428 STATIC void 429 xchk_refcountbt_check_mergeable( 430 struct xchk_btree *bs, 431 struct xchk_refcbt_records *rrc, 432 const struct xfs_refcount_irec *irec) 433 { 434 if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 435 return; 436 437 if (xchk_refcount_mergeable(rrc, irec)) 438 xchk_btree_set_corrupt(bs->sc, bs->cur, 0); 439 440 memcpy(&rrc->prev_rec, irec, sizeof(struct xfs_refcount_irec)); 441 } 442 443 /* Scrub a refcountbt record. */ 444 STATIC int 445 xchk_refcountbt_rec( 446 struct xchk_btree *bs, 447 const union xfs_btree_rec *rec) 448 { 449 struct xfs_refcount_irec irec; 450 struct xchk_refcbt_records *rrc = bs->private; 451 452 xfs_refcount_btrec_to_irec(rec, &irec); 453 if (xfs_refcount_check_irec(to_perag(bs->cur->bc_group), &irec) != 454 NULL) { 455 xchk_btree_set_corrupt(bs->sc, bs->cur, 0); 456 return 0; 457 } 458 459 if (irec.rc_domain == XFS_REFC_DOMAIN_COW) 460 rrc->cow_blocks += irec.rc_blockcount; 461 462 /* Shared records always come before CoW records. */ 463 if (irec.rc_domain == XFS_REFC_DOMAIN_SHARED && 464 rrc->prev_domain == XFS_REFC_DOMAIN_COW) 465 xchk_btree_set_corrupt(bs->sc, bs->cur, 0); 466 rrc->prev_domain = irec.rc_domain; 467 468 xchk_refcountbt_check_mergeable(bs, rrc, &irec); 469 xchk_refcountbt_xref(bs->sc, &irec); 470 471 /* 472 * If this is a record for a shared extent, check that all blocks 473 * between the previous record and this one have at most one reverse 474 * mapping. 475 */ 476 if (irec.rc_domain == XFS_REFC_DOMAIN_SHARED) { 477 xchk_refcountbt_xref_gaps(bs->sc, rrc, irec.rc_startblock); 478 rrc->next_unshared_agbno = irec.rc_startblock + 479 irec.rc_blockcount; 480 } 481 482 return 0; 483 } 484 485 /* Make sure we have as many refc blocks as the rmap says. */ 486 STATIC void 487 xchk_refcount_xref_rmap( 488 struct xfs_scrub *sc, 489 xfs_filblks_t cow_blocks) 490 { 491 xfs_filblks_t refcbt_blocks = 0; 492 xfs_filblks_t blocks; 493 int error; 494 495 if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm)) 496 return; 497 498 /* Check that we saw as many refcbt blocks as the rmap knows about. */ 499 error = xfs_btree_count_blocks(sc->sa.refc_cur, &refcbt_blocks); 500 if (!xchk_btree_process_error(sc, sc->sa.refc_cur, 0, &error)) 501 return; 502 error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, 503 &XFS_RMAP_OINFO_REFC, &blocks); 504 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur)) 505 return; 506 if (blocks != refcbt_blocks) 507 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 508 509 /* Check that we saw as many cow blocks as the rmap knows about. */ 510 error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, 511 &XFS_RMAP_OINFO_COW, &blocks); 512 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur)) 513 return; 514 if (blocks != cow_blocks) 515 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 516 } 517 518 /* Scrub the refcount btree for some AG. */ 519 int 520 xchk_refcountbt( 521 struct xfs_scrub *sc) 522 { 523 struct xchk_refcbt_records rrc = { 524 .cow_blocks = 0, 525 .next_unshared_agbno = 0, 526 .prev_domain = XFS_REFC_DOMAIN_SHARED, 527 }; 528 int error; 529 530 error = xchk_btree(sc, sc->sa.refc_cur, xchk_refcountbt_rec, 531 &XFS_RMAP_OINFO_REFC, &rrc); 532 if (error) 533 return error; 534 535 /* 536 * Check that all blocks between the last refcount > 1 record and the 537 * end of the AG have at most one reverse mapping. 538 */ 539 xchk_refcountbt_xref_gaps(sc, &rrc, sc->mp->m_sb.sb_agblocks); 540 541 xchk_refcount_xref_rmap(sc, rrc.cow_blocks); 542 543 return 0; 544 } 545 546 /* xref check that a cow staging extent is marked in the refcountbt. */ 547 void 548 xchk_xref_is_cow_staging( 549 struct xfs_scrub *sc, 550 xfs_agblock_t agbno, 551 xfs_extlen_t len) 552 { 553 struct xfs_refcount_irec rc; 554 int has_refcount; 555 int error; 556 557 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm)) 558 return; 559 560 /* Find the CoW staging extent. */ 561 error = xfs_refcount_lookup_le(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW, 562 agbno, &has_refcount); 563 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 564 return; 565 if (!has_refcount) { 566 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 567 return; 568 } 569 570 error = xfs_refcount_get_rec(sc->sa.refc_cur, &rc, &has_refcount); 571 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 572 return; 573 if (!has_refcount) { 574 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 575 return; 576 } 577 578 /* CoW lookup returned a shared extent record? */ 579 if (rc.rc_domain != XFS_REFC_DOMAIN_COW) 580 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 581 582 /* Can't start after bno */ 583 if (rc.rc_startblock > agbno) 584 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 585 586 /* Must be at least as long as what was passed in */ 587 if (rc.rc_startblock + rc.rc_blockcount < agbno + len) 588 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 589 } 590 591 /* 592 * xref check that the extent is not shared. Only file data blocks 593 * can have multiple owners. 594 */ 595 void 596 xchk_xref_is_not_shared( 597 struct xfs_scrub *sc, 598 xfs_agblock_t agbno, 599 xfs_extlen_t len) 600 { 601 enum xbtree_recpacking outcome; 602 int error; 603 604 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm)) 605 return; 606 607 error = xfs_refcount_has_records(sc->sa.refc_cur, 608 XFS_REFC_DOMAIN_SHARED, agbno, len, &outcome); 609 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 610 return; 611 if (outcome != XBTREE_RECPACKING_EMPTY) 612 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 613 } 614 615 /* xref check that the extent is not being used for CoW staging. */ 616 void 617 xchk_xref_is_not_cow_staging( 618 struct xfs_scrub *sc, 619 xfs_agblock_t agbno, 620 xfs_extlen_t len) 621 { 622 enum xbtree_recpacking outcome; 623 int error; 624 625 if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm)) 626 return; 627 628 error = xfs_refcount_has_records(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW, 629 agbno, len, &outcome); 630 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 631 return; 632 if (outcome != XBTREE_RECPACKING_EMPTY) 633 xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 634 } 635