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_btree.h" 13 #include "xfs_log_format.h" 14 #include "xfs_trans.h" 15 #include "xfs_inode.h" 16 #include "xfs_icache.h" 17 #include "xfs_alloc.h" 18 #include "xfs_alloc_btree.h" 19 #include "xfs_ialloc.h" 20 #include "xfs_ialloc_btree.h" 21 #include "xfs_refcount_btree.h" 22 #include "xfs_rmap.h" 23 #include "xfs_rmap_btree.h" 24 #include "xfs_log.h" 25 #include "xfs_trans_priv.h" 26 #include "xfs_attr.h" 27 #include "xfs_reflink.h" 28 #include "xfs_ag.h" 29 #include "scrub/scrub.h" 30 #include "scrub/common.h" 31 #include "scrub/trace.h" 32 #include "scrub/repair.h" 33 #include "scrub/health.h" 34 35 /* Common code for the metadata scrubbers. */ 36 37 /* 38 * Handling operational errors. 39 * 40 * The *_process_error() family of functions are used to process error return 41 * codes from functions called as part of a scrub operation. 42 * 43 * If there's no error, we return true to tell the caller that it's ok 44 * to move on to the next check in its list. 45 * 46 * For non-verifier errors (e.g. ENOMEM) we return false to tell the 47 * caller that something bad happened, and we preserve *error so that 48 * the caller can return the *error up the stack to userspace. 49 * 50 * Verifier errors (EFSBADCRC/EFSCORRUPTED) are recorded by setting 51 * OFLAG_CORRUPT in sm_flags and the *error is cleared. In other words, 52 * we track verifier errors (and failed scrub checks) via OFLAG_CORRUPT, 53 * not via return codes. We return false to tell the caller that 54 * something bad happened. Since the error has been cleared, the caller 55 * will (presumably) return that zero and scrubbing will move on to 56 * whatever's next. 57 * 58 * ftrace can be used to record the precise metadata location and the 59 * approximate code location of the failed operation. 60 */ 61 62 /* Check for operational errors. */ 63 static bool 64 __xchk_process_error( 65 struct xfs_scrub *sc, 66 xfs_agnumber_t agno, 67 xfs_agblock_t bno, 68 int *error, 69 __u32 errflag, 70 void *ret_ip) 71 { 72 switch (*error) { 73 case 0: 74 return true; 75 case -EDEADLOCK: 76 /* Used to restart an op with deadlock avoidance. */ 77 trace_xchk_deadlock_retry( 78 sc->ip ? sc->ip : XFS_I(file_inode(sc->file)), 79 sc->sm, *error); 80 break; 81 case -EFSBADCRC: 82 case -EFSCORRUPTED: 83 /* Note the badness but don't abort. */ 84 sc->sm->sm_flags |= errflag; 85 *error = 0; 86 fallthrough; 87 default: 88 trace_xchk_op_error(sc, agno, bno, *error, 89 ret_ip); 90 break; 91 } 92 return false; 93 } 94 95 bool 96 xchk_process_error( 97 struct xfs_scrub *sc, 98 xfs_agnumber_t agno, 99 xfs_agblock_t bno, 100 int *error) 101 { 102 return __xchk_process_error(sc, agno, bno, error, 103 XFS_SCRUB_OFLAG_CORRUPT, __return_address); 104 } 105 106 bool 107 xchk_xref_process_error( 108 struct xfs_scrub *sc, 109 xfs_agnumber_t agno, 110 xfs_agblock_t bno, 111 int *error) 112 { 113 return __xchk_process_error(sc, agno, bno, error, 114 XFS_SCRUB_OFLAG_XFAIL, __return_address); 115 } 116 117 /* Check for operational errors for a file offset. */ 118 static bool 119 __xchk_fblock_process_error( 120 struct xfs_scrub *sc, 121 int whichfork, 122 xfs_fileoff_t offset, 123 int *error, 124 __u32 errflag, 125 void *ret_ip) 126 { 127 switch (*error) { 128 case 0: 129 return true; 130 case -EDEADLOCK: 131 /* Used to restart an op with deadlock avoidance. */ 132 trace_xchk_deadlock_retry(sc->ip, sc->sm, *error); 133 break; 134 case -EFSBADCRC: 135 case -EFSCORRUPTED: 136 /* Note the badness but don't abort. */ 137 sc->sm->sm_flags |= errflag; 138 *error = 0; 139 fallthrough; 140 default: 141 trace_xchk_file_op_error(sc, whichfork, offset, *error, 142 ret_ip); 143 break; 144 } 145 return false; 146 } 147 148 bool 149 xchk_fblock_process_error( 150 struct xfs_scrub *sc, 151 int whichfork, 152 xfs_fileoff_t offset, 153 int *error) 154 { 155 return __xchk_fblock_process_error(sc, whichfork, offset, error, 156 XFS_SCRUB_OFLAG_CORRUPT, __return_address); 157 } 158 159 bool 160 xchk_fblock_xref_process_error( 161 struct xfs_scrub *sc, 162 int whichfork, 163 xfs_fileoff_t offset, 164 int *error) 165 { 166 return __xchk_fblock_process_error(sc, whichfork, offset, error, 167 XFS_SCRUB_OFLAG_XFAIL, __return_address); 168 } 169 170 /* 171 * Handling scrub corruption/optimization/warning checks. 172 * 173 * The *_set_{corrupt,preen,warning}() family of functions are used to 174 * record the presence of metadata that is incorrect (corrupt), could be 175 * optimized somehow (preen), or should be flagged for administrative 176 * review but is not incorrect (warn). 177 * 178 * ftrace can be used to record the precise metadata location and 179 * approximate code location of the failed check. 180 */ 181 182 /* Record a block which could be optimized. */ 183 void 184 xchk_block_set_preen( 185 struct xfs_scrub *sc, 186 struct xfs_buf *bp) 187 { 188 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN; 189 trace_xchk_block_preen(sc, bp->b_bn, __return_address); 190 } 191 192 /* 193 * Record an inode which could be optimized. The trace data will 194 * include the block given by bp if bp is given; otherwise it will use 195 * the block location of the inode record itself. 196 */ 197 void 198 xchk_ino_set_preen( 199 struct xfs_scrub *sc, 200 xfs_ino_t ino) 201 { 202 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_PREEN; 203 trace_xchk_ino_preen(sc, ino, __return_address); 204 } 205 206 /* Record something being wrong with the filesystem primary superblock. */ 207 void 208 xchk_set_corrupt( 209 struct xfs_scrub *sc) 210 { 211 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 212 trace_xchk_fs_error(sc, 0, __return_address); 213 } 214 215 /* Record a corrupt block. */ 216 void 217 xchk_block_set_corrupt( 218 struct xfs_scrub *sc, 219 struct xfs_buf *bp) 220 { 221 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 222 trace_xchk_block_error(sc, bp->b_bn, __return_address); 223 } 224 225 /* Record a corruption while cross-referencing. */ 226 void 227 xchk_block_xref_set_corrupt( 228 struct xfs_scrub *sc, 229 struct xfs_buf *bp) 230 { 231 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT; 232 trace_xchk_block_error(sc, bp->b_bn, __return_address); 233 } 234 235 /* 236 * Record a corrupt inode. The trace data will include the block given 237 * by bp if bp is given; otherwise it will use the block location of the 238 * inode record itself. 239 */ 240 void 241 xchk_ino_set_corrupt( 242 struct xfs_scrub *sc, 243 xfs_ino_t ino) 244 { 245 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 246 trace_xchk_ino_error(sc, ino, __return_address); 247 } 248 249 /* Record a corruption while cross-referencing with an inode. */ 250 void 251 xchk_ino_xref_set_corrupt( 252 struct xfs_scrub *sc, 253 xfs_ino_t ino) 254 { 255 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT; 256 trace_xchk_ino_error(sc, ino, __return_address); 257 } 258 259 /* Record corruption in a block indexed by a file fork. */ 260 void 261 xchk_fblock_set_corrupt( 262 struct xfs_scrub *sc, 263 int whichfork, 264 xfs_fileoff_t offset) 265 { 266 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 267 trace_xchk_fblock_error(sc, whichfork, offset, __return_address); 268 } 269 270 /* Record a corruption while cross-referencing a fork block. */ 271 void 272 xchk_fblock_xref_set_corrupt( 273 struct xfs_scrub *sc, 274 int whichfork, 275 xfs_fileoff_t offset) 276 { 277 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XCORRUPT; 278 trace_xchk_fblock_error(sc, whichfork, offset, __return_address); 279 } 280 281 /* 282 * Warn about inodes that need administrative review but is not 283 * incorrect. 284 */ 285 void 286 xchk_ino_set_warning( 287 struct xfs_scrub *sc, 288 xfs_ino_t ino) 289 { 290 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING; 291 trace_xchk_ino_warning(sc, ino, __return_address); 292 } 293 294 /* Warn about a block indexed by a file fork that needs review. */ 295 void 296 xchk_fblock_set_warning( 297 struct xfs_scrub *sc, 298 int whichfork, 299 xfs_fileoff_t offset) 300 { 301 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_WARNING; 302 trace_xchk_fblock_warning(sc, whichfork, offset, __return_address); 303 } 304 305 /* Signal an incomplete scrub. */ 306 void 307 xchk_set_incomplete( 308 struct xfs_scrub *sc) 309 { 310 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_INCOMPLETE; 311 trace_xchk_incomplete(sc, __return_address); 312 } 313 314 /* 315 * rmap scrubbing -- compute the number of blocks with a given owner, 316 * at least according to the reverse mapping data. 317 */ 318 319 struct xchk_rmap_ownedby_info { 320 const struct xfs_owner_info *oinfo; 321 xfs_filblks_t *blocks; 322 }; 323 324 STATIC int 325 xchk_count_rmap_ownedby_irec( 326 struct xfs_btree_cur *cur, 327 struct xfs_rmap_irec *rec, 328 void *priv) 329 { 330 struct xchk_rmap_ownedby_info *sroi = priv; 331 bool irec_attr; 332 bool oinfo_attr; 333 334 irec_attr = rec->rm_flags & XFS_RMAP_ATTR_FORK; 335 oinfo_attr = sroi->oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK; 336 337 if (rec->rm_owner != sroi->oinfo->oi_owner) 338 return 0; 339 340 if (XFS_RMAP_NON_INODE_OWNER(rec->rm_owner) || irec_attr == oinfo_attr) 341 (*sroi->blocks) += rec->rm_blockcount; 342 343 return 0; 344 } 345 346 /* 347 * Calculate the number of blocks the rmap thinks are owned by something. 348 * The caller should pass us an rmapbt cursor. 349 */ 350 int 351 xchk_count_rmap_ownedby_ag( 352 struct xfs_scrub *sc, 353 struct xfs_btree_cur *cur, 354 const struct xfs_owner_info *oinfo, 355 xfs_filblks_t *blocks) 356 { 357 struct xchk_rmap_ownedby_info sroi = { 358 .oinfo = oinfo, 359 .blocks = blocks, 360 }; 361 362 *blocks = 0; 363 return xfs_rmap_query_all(cur, xchk_count_rmap_ownedby_irec, 364 &sroi); 365 } 366 367 /* 368 * AG scrubbing 369 * 370 * These helpers facilitate locking an allocation group's header 371 * buffers, setting up cursors for all btrees that are present, and 372 * cleaning everything up once we're through. 373 */ 374 375 /* Decide if we want to return an AG header read failure. */ 376 static inline bool 377 want_ag_read_header_failure( 378 struct xfs_scrub *sc, 379 unsigned int type) 380 { 381 /* Return all AG header read failures when scanning btrees. */ 382 if (sc->sm->sm_type != XFS_SCRUB_TYPE_AGF && 383 sc->sm->sm_type != XFS_SCRUB_TYPE_AGFL && 384 sc->sm->sm_type != XFS_SCRUB_TYPE_AGI) 385 return true; 386 /* 387 * If we're scanning a given type of AG header, we only want to 388 * see read failures from that specific header. We'd like the 389 * other headers to cross-check them, but this isn't required. 390 */ 391 if (sc->sm->sm_type == type) 392 return true; 393 return false; 394 } 395 396 /* 397 * Grab all the headers for an AG. 398 * 399 * The headers should be released by xchk_ag_free, but as a fail 400 * safe we attach all the buffers we grab to the scrub transaction so 401 * they'll all be freed when we cancel it. 402 */ 403 int 404 xchk_ag_read_headers( 405 struct xfs_scrub *sc, 406 xfs_agnumber_t agno, 407 struct xchk_ag *sa) 408 { 409 struct xfs_mount *mp = sc->mp; 410 int error; 411 412 sa->agno = agno; 413 414 error = xfs_ialloc_read_agi(mp, sc->tp, agno, &sa->agi_bp); 415 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGI)) 416 goto out; 417 418 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, &sa->agf_bp); 419 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGF)) 420 goto out; 421 422 error = xfs_alloc_read_agfl(mp, sc->tp, agno, &sa->agfl_bp); 423 if (error && want_ag_read_header_failure(sc, XFS_SCRUB_TYPE_AGFL)) 424 goto out; 425 error = 0; 426 out: 427 return error; 428 } 429 430 /* Release all the AG btree cursors. */ 431 void 432 xchk_ag_btcur_free( 433 struct xchk_ag *sa) 434 { 435 if (sa->refc_cur) 436 xfs_btree_del_cursor(sa->refc_cur, XFS_BTREE_ERROR); 437 if (sa->rmap_cur) 438 xfs_btree_del_cursor(sa->rmap_cur, XFS_BTREE_ERROR); 439 if (sa->fino_cur) 440 xfs_btree_del_cursor(sa->fino_cur, XFS_BTREE_ERROR); 441 if (sa->ino_cur) 442 xfs_btree_del_cursor(sa->ino_cur, XFS_BTREE_ERROR); 443 if (sa->cnt_cur) 444 xfs_btree_del_cursor(sa->cnt_cur, XFS_BTREE_ERROR); 445 if (sa->bno_cur) 446 xfs_btree_del_cursor(sa->bno_cur, XFS_BTREE_ERROR); 447 448 sa->refc_cur = NULL; 449 sa->rmap_cur = NULL; 450 sa->fino_cur = NULL; 451 sa->ino_cur = NULL; 452 sa->bno_cur = NULL; 453 sa->cnt_cur = NULL; 454 } 455 456 /* Initialize all the btree cursors for an AG. */ 457 void 458 xchk_ag_btcur_init( 459 struct xfs_scrub *sc, 460 struct xchk_ag *sa) 461 { 462 struct xfs_mount *mp = sc->mp; 463 464 xchk_perag_get(sc->mp, sa); 465 if (sa->agf_bp && 466 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_BNO)) { 467 /* Set up a bnobt cursor for cross-referencing. */ 468 sa->bno_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp, 469 sa->pag, XFS_BTNUM_BNO); 470 } 471 472 if (sa->agf_bp && 473 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_CNT)) { 474 /* Set up a cntbt cursor for cross-referencing. */ 475 sa->cnt_cur = xfs_allocbt_init_cursor(mp, sc->tp, sa->agf_bp, 476 sa->pag, XFS_BTNUM_CNT); 477 } 478 479 /* Set up a inobt cursor for cross-referencing. */ 480 if (sa->agi_bp && 481 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_INO)) { 482 sa->ino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp, 483 sa->pag, XFS_BTNUM_INO); 484 } 485 486 /* Set up a finobt cursor for cross-referencing. */ 487 if (sa->agi_bp && xfs_sb_version_hasfinobt(&mp->m_sb) && 488 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_FINO)) { 489 sa->fino_cur = xfs_inobt_init_cursor(mp, sc->tp, sa->agi_bp, 490 sa->pag, XFS_BTNUM_FINO); 491 } 492 493 /* Set up a rmapbt cursor for cross-referencing. */ 494 if (sa->agf_bp && xfs_sb_version_hasrmapbt(&mp->m_sb) && 495 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_RMAP)) { 496 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp, 497 sa->pag); 498 } 499 500 /* Set up a refcountbt cursor for cross-referencing. */ 501 if (sa->agf_bp && xfs_sb_version_hasreflink(&mp->m_sb) && 502 xchk_ag_btree_healthy_enough(sc, sa->pag, XFS_BTNUM_REFC)) { 503 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp, 504 sa->agf_bp, sa->pag); 505 } 506 } 507 508 /* Release the AG header context and btree cursors. */ 509 void 510 xchk_ag_free( 511 struct xfs_scrub *sc, 512 struct xchk_ag *sa) 513 { 514 xchk_ag_btcur_free(sa); 515 if (sa->agfl_bp) { 516 xfs_trans_brelse(sc->tp, sa->agfl_bp); 517 sa->agfl_bp = NULL; 518 } 519 if (sa->agf_bp) { 520 xfs_trans_brelse(sc->tp, sa->agf_bp); 521 sa->agf_bp = NULL; 522 } 523 if (sa->agi_bp) { 524 xfs_trans_brelse(sc->tp, sa->agi_bp); 525 sa->agi_bp = NULL; 526 } 527 if (sa->pag) { 528 xfs_perag_put(sa->pag); 529 sa->pag = NULL; 530 } 531 sa->agno = NULLAGNUMBER; 532 } 533 534 /* 535 * For scrub, grab the AGI and the AGF headers, in that order. Locking 536 * order requires us to get the AGI before the AGF. We use the 537 * transaction to avoid deadlocking on crosslinked metadata buffers; 538 * either the caller passes one in (bmap scrub) or we have to create a 539 * transaction ourselves. 540 */ 541 int 542 xchk_ag_init( 543 struct xfs_scrub *sc, 544 xfs_agnumber_t agno, 545 struct xchk_ag *sa) 546 { 547 int error; 548 549 error = xchk_ag_read_headers(sc, agno, sa); 550 if (error) 551 return error; 552 553 xchk_ag_btcur_init(sc, sa); 554 return 0; 555 } 556 557 /* 558 * Grab the per-ag structure if we haven't already gotten it. Teardown of the 559 * xchk_ag will release it for us. 560 */ 561 void 562 xchk_perag_get( 563 struct xfs_mount *mp, 564 struct xchk_ag *sa) 565 { 566 if (!sa->pag) 567 sa->pag = xfs_perag_get(mp, sa->agno); 568 } 569 570 /* Per-scrubber setup functions */ 571 572 /* 573 * Grab an empty transaction so that we can re-grab locked buffers if 574 * one of our btrees turns out to be cyclic. 575 * 576 * If we're going to repair something, we need to ask for the largest possible 577 * log reservation so that we can handle the worst case scenario for metadata 578 * updates while rebuilding a metadata item. We also need to reserve as many 579 * blocks in the head transaction as we think we're going to need to rebuild 580 * the metadata object. 581 */ 582 int 583 xchk_trans_alloc( 584 struct xfs_scrub *sc, 585 uint resblks) 586 { 587 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) 588 return xfs_trans_alloc(sc->mp, &M_RES(sc->mp)->tr_itruncate, 589 resblks, 0, 0, &sc->tp); 590 591 return xfs_trans_alloc_empty(sc->mp, &sc->tp); 592 } 593 594 /* Set us up with a transaction and an empty context. */ 595 int 596 xchk_setup_fs( 597 struct xfs_scrub *sc) 598 { 599 uint resblks; 600 601 resblks = xrep_calc_ag_resblks(sc); 602 return xchk_trans_alloc(sc, resblks); 603 } 604 605 /* Set us up with AG headers and btree cursors. */ 606 int 607 xchk_setup_ag_btree( 608 struct xfs_scrub *sc, 609 bool force_log) 610 { 611 struct xfs_mount *mp = sc->mp; 612 int error; 613 614 /* 615 * If the caller asks us to checkpont the log, do so. This 616 * expensive operation should be performed infrequently and only 617 * as a last resort. Any caller that sets force_log should 618 * document why they need to do so. 619 */ 620 if (force_log) { 621 error = xchk_checkpoint_log(mp); 622 if (error) 623 return error; 624 } 625 626 error = xchk_setup_fs(sc); 627 if (error) 628 return error; 629 630 return xchk_ag_init(sc, sc->sm->sm_agno, &sc->sa); 631 } 632 633 /* Push everything out of the log onto disk. */ 634 int 635 xchk_checkpoint_log( 636 struct xfs_mount *mp) 637 { 638 int error; 639 640 error = xfs_log_force(mp, XFS_LOG_SYNC); 641 if (error) 642 return error; 643 xfs_ail_push_all_sync(mp->m_ail); 644 return 0; 645 } 646 647 /* 648 * Given an inode and the scrub control structure, grab either the 649 * inode referenced in the control structure or the inode passed in. 650 * The inode is not locked. 651 */ 652 int 653 xchk_get_inode( 654 struct xfs_scrub *sc) 655 { 656 struct xfs_imap imap; 657 struct xfs_mount *mp = sc->mp; 658 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file)); 659 struct xfs_inode *ip = NULL; 660 int error; 661 662 /* We want to scan the inode we already had opened. */ 663 if (sc->sm->sm_ino == 0 || sc->sm->sm_ino == ip_in->i_ino) { 664 sc->ip = ip_in; 665 return 0; 666 } 667 668 /* Look up the inode, see if the generation number matches. */ 669 if (xfs_internal_inum(mp, sc->sm->sm_ino)) 670 return -ENOENT; 671 error = xfs_iget(mp, NULL, sc->sm->sm_ino, 672 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE, 0, &ip); 673 switch (error) { 674 case -ENOENT: 675 /* Inode doesn't exist, just bail out. */ 676 return error; 677 case 0: 678 /* Got an inode, continue. */ 679 break; 680 case -EINVAL: 681 /* 682 * -EINVAL with IGET_UNTRUSTED could mean one of several 683 * things: userspace gave us an inode number that doesn't 684 * correspond to fs space, or doesn't have an inobt entry; 685 * or it could simply mean that the inode buffer failed the 686 * read verifiers. 687 * 688 * Try just the inode mapping lookup -- if it succeeds, then 689 * the inode buffer verifier failed and something needs fixing. 690 * Otherwise, we really couldn't find it so tell userspace 691 * that it no longer exists. 692 */ 693 error = xfs_imap(sc->mp, sc->tp, sc->sm->sm_ino, &imap, 694 XFS_IGET_UNTRUSTED | XFS_IGET_DONTCACHE); 695 if (error) 696 return -ENOENT; 697 error = -EFSCORRUPTED; 698 fallthrough; 699 default: 700 trace_xchk_op_error(sc, 701 XFS_INO_TO_AGNO(mp, sc->sm->sm_ino), 702 XFS_INO_TO_AGBNO(mp, sc->sm->sm_ino), 703 error, __return_address); 704 return error; 705 } 706 if (VFS_I(ip)->i_generation != sc->sm->sm_gen) { 707 xfs_irele(ip); 708 return -ENOENT; 709 } 710 711 sc->ip = ip; 712 return 0; 713 } 714 715 /* Set us up to scrub a file's contents. */ 716 int 717 xchk_setup_inode_contents( 718 struct xfs_scrub *sc, 719 unsigned int resblks) 720 { 721 int error; 722 723 error = xchk_get_inode(sc); 724 if (error) 725 return error; 726 727 /* Got the inode, lock it and we're ready to go. */ 728 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL; 729 xfs_ilock(sc->ip, sc->ilock_flags); 730 error = xchk_trans_alloc(sc, resblks); 731 if (error) 732 goto out; 733 sc->ilock_flags |= XFS_ILOCK_EXCL; 734 xfs_ilock(sc->ip, XFS_ILOCK_EXCL); 735 736 out: 737 /* scrub teardown will unlock and release the inode for us */ 738 return error; 739 } 740 741 /* 742 * Predicate that decides if we need to evaluate the cross-reference check. 743 * If there was an error accessing the cross-reference btree, just delete 744 * the cursor and skip the check. 745 */ 746 bool 747 xchk_should_check_xref( 748 struct xfs_scrub *sc, 749 int *error, 750 struct xfs_btree_cur **curpp) 751 { 752 /* No point in xref if we already know we're corrupt. */ 753 if (xchk_skip_xref(sc->sm)) 754 return false; 755 756 if (*error == 0) 757 return true; 758 759 if (curpp) { 760 /* If we've already given up on xref, just bail out. */ 761 if (!*curpp) 762 return false; 763 764 /* xref error, delete cursor and bail out. */ 765 xfs_btree_del_cursor(*curpp, XFS_BTREE_ERROR); 766 *curpp = NULL; 767 } 768 769 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL; 770 trace_xchk_xref_error(sc, *error, __return_address); 771 772 /* 773 * Errors encountered during cross-referencing with another 774 * data structure should not cause this scrubber to abort. 775 */ 776 *error = 0; 777 return false; 778 } 779 780 /* Run the structure verifiers on in-memory buffers to detect bad memory. */ 781 void 782 xchk_buffer_recheck( 783 struct xfs_scrub *sc, 784 struct xfs_buf *bp) 785 { 786 xfs_failaddr_t fa; 787 788 if (bp->b_ops == NULL) { 789 xchk_block_set_corrupt(sc, bp); 790 return; 791 } 792 if (bp->b_ops->verify_struct == NULL) { 793 xchk_set_incomplete(sc); 794 return; 795 } 796 fa = bp->b_ops->verify_struct(bp); 797 if (!fa) 798 return; 799 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 800 trace_xchk_block_error(sc, bp->b_bn, fa); 801 } 802 803 /* 804 * Scrub the attr/data forks of a metadata inode. The metadata inode must be 805 * pointed to by sc->ip and the ILOCK must be held. 806 */ 807 int 808 xchk_metadata_inode_forks( 809 struct xfs_scrub *sc) 810 { 811 __u32 smtype; 812 bool shared; 813 int error; 814 815 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 816 return 0; 817 818 /* Metadata inodes don't live on the rt device. */ 819 if (sc->ip->i_diflags & XFS_DIFLAG_REALTIME) { 820 xchk_ino_set_corrupt(sc, sc->ip->i_ino); 821 return 0; 822 } 823 824 /* They should never participate in reflink. */ 825 if (xfs_is_reflink_inode(sc->ip)) { 826 xchk_ino_set_corrupt(sc, sc->ip->i_ino); 827 return 0; 828 } 829 830 /* They also should never have extended attributes. */ 831 if (xfs_inode_hasattr(sc->ip)) { 832 xchk_ino_set_corrupt(sc, sc->ip->i_ino); 833 return 0; 834 } 835 836 /* Invoke the data fork scrubber. */ 837 smtype = sc->sm->sm_type; 838 sc->sm->sm_type = XFS_SCRUB_TYPE_BMBTD; 839 error = xchk_bmap_data(sc); 840 sc->sm->sm_type = smtype; 841 if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) 842 return error; 843 844 /* Look for incorrect shared blocks. */ 845 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) { 846 error = xfs_reflink_inode_has_shared_extents(sc->tp, sc->ip, 847 &shared); 848 if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, 849 &error)) 850 return error; 851 if (shared) 852 xchk_ino_set_corrupt(sc, sc->ip->i_ino); 853 } 854 855 return error; 856 } 857 858 /* 859 * Try to lock an inode in violation of the usual locking order rules. For 860 * example, trying to get the IOLOCK while in transaction context, or just 861 * plain breaking AG-order or inode-order inode locking rules. Either way, 862 * the only way to avoid an ABBA deadlock is to use trylock and back off if 863 * we can't. 864 */ 865 int 866 xchk_ilock_inverted( 867 struct xfs_inode *ip, 868 uint lock_mode) 869 { 870 int i; 871 872 for (i = 0; i < 20; i++) { 873 if (xfs_ilock_nowait(ip, lock_mode)) 874 return 0; 875 delay(1); 876 } 877 return -EDEADLOCK; 878 } 879 880 /* Pause background reaping of resources. */ 881 void 882 xchk_stop_reaping( 883 struct xfs_scrub *sc) 884 { 885 sc->flags |= XCHK_REAPING_DISABLED; 886 xfs_blockgc_stop(sc->mp); 887 } 888 889 /* Restart background reaping of resources. */ 890 void 891 xchk_start_reaping( 892 struct xfs_scrub *sc) 893 { 894 xfs_blockgc_start(sc->mp); 895 sc->flags &= ~XCHK_REAPING_DISABLED; 896 } 897