1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2018-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_sb.h" 16 #include "xfs_inode.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_rmap.h" 22 #include "xfs_rmap_btree.h" 23 #include "xfs_refcount_btree.h" 24 #include "xfs_rtbitmap.h" 25 #include "xfs_extent_busy.h" 26 #include "xfs_ag.h" 27 #include "xfs_ag_resv.h" 28 #include "xfs_quota.h" 29 #include "xfs_qm.h" 30 #include "xfs_defer.h" 31 #include "xfs_errortag.h" 32 #include "xfs_error.h" 33 #include "xfs_reflink.h" 34 #include "xfs_health.h" 35 #include "xfs_buf_mem.h" 36 #include "xfs_da_format.h" 37 #include "xfs_da_btree.h" 38 #include "xfs_attr.h" 39 #include "xfs_dir2.h" 40 #include "scrub/scrub.h" 41 #include "scrub/common.h" 42 #include "scrub/trace.h" 43 #include "scrub/repair.h" 44 #include "scrub/bitmap.h" 45 #include "scrub/stats.h" 46 #include "scrub/xfile.h" 47 #include "scrub/attr_repair.h" 48 49 /* 50 * Attempt to repair some metadata, if the metadata is corrupt and userspace 51 * told us to fix it. This function returns -EAGAIN to mean "re-run scrub", 52 * and will set *fixed to true if it thinks it repaired anything. 53 */ 54 int 55 xrep_attempt( 56 struct xfs_scrub *sc, 57 struct xchk_stats_run *run) 58 { 59 u64 repair_start; 60 int error = 0; 61 62 trace_xrep_attempt(XFS_I(file_inode(sc->file)), sc->sm, error); 63 64 xchk_ag_btcur_free(&sc->sa); 65 66 /* Repair whatever's broken. */ 67 ASSERT(sc->ops->repair); 68 run->repair_attempted = true; 69 repair_start = xchk_stats_now(); 70 error = sc->ops->repair(sc); 71 trace_xrep_done(XFS_I(file_inode(sc->file)), sc->sm, error); 72 run->repair_ns += xchk_stats_elapsed_ns(repair_start); 73 switch (error) { 74 case 0: 75 /* 76 * Repair succeeded. Commit the fixes and perform a second 77 * scrub so that we can tell userspace if we fixed the problem. 78 */ 79 sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT; 80 sc->flags |= XREP_ALREADY_FIXED; 81 run->repair_succeeded = true; 82 return -EAGAIN; 83 case -ECHRNG: 84 sc->flags |= XCHK_NEED_DRAIN; 85 run->retries++; 86 return -EAGAIN; 87 case -EDEADLOCK: 88 /* Tell the caller to try again having grabbed all the locks. */ 89 if (!(sc->flags & XCHK_TRY_HARDER)) { 90 sc->flags |= XCHK_TRY_HARDER; 91 run->retries++; 92 return -EAGAIN; 93 } 94 /* 95 * We tried harder but still couldn't grab all the resources 96 * we needed to fix it. The corruption has not been fixed, 97 * so exit to userspace with the scan's output flags unchanged. 98 */ 99 return 0; 100 default: 101 /* 102 * EAGAIN tells the caller to re-scrub, so we cannot return 103 * that here. 104 */ 105 ASSERT(error != -EAGAIN); 106 return error; 107 } 108 } 109 110 /* 111 * Complain about unfixable problems in the filesystem. We don't log 112 * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver 113 * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the 114 * administrator isn't running xfs_scrub in no-repairs mode. 115 * 116 * Use this helper function because _ratelimited silently declares a static 117 * structure to track rate limiting information. 118 */ 119 void 120 xrep_failure( 121 struct xfs_mount *mp) 122 { 123 xfs_alert_ratelimited(mp, 124 "Corruption not fixed during online repair. Unmount and run xfs_repair."); 125 } 126 127 /* 128 * Repair probe -- userspace uses this to probe if we're willing to repair a 129 * given mountpoint. 130 */ 131 int 132 xrep_probe( 133 struct xfs_scrub *sc) 134 { 135 int error = 0; 136 137 if (xchk_should_terminate(sc, &error)) 138 return error; 139 140 return 0; 141 } 142 143 /* 144 * Roll a transaction, keeping the AG headers locked and reinitializing 145 * the btree cursors. 146 */ 147 int 148 xrep_roll_ag_trans( 149 struct xfs_scrub *sc) 150 { 151 int error; 152 153 /* 154 * Keep the AG header buffers locked while we roll the transaction. 155 * Ensure that both AG buffers are dirty and held when we roll the 156 * transaction so that they move forward in the log without losing the 157 * bli (and hence the bli type) when the transaction commits. 158 * 159 * Normal code would never hold clean buffers across a roll, but repair 160 * needs both buffers to maintain a total lock on the AG. 161 */ 162 if (sc->sa.agi_bp) { 163 xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM); 164 xfs_trans_bhold(sc->tp, sc->sa.agi_bp); 165 } 166 167 if (sc->sa.agf_bp) { 168 xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM); 169 xfs_trans_bhold(sc->tp, sc->sa.agf_bp); 170 } 171 172 /* 173 * Roll the transaction. We still hold the AG header buffers locked 174 * regardless of whether or not that succeeds. On failure, the buffers 175 * will be released during teardown on our way out of the kernel. If 176 * successful, join the buffers to the new transaction and move on. 177 */ 178 error = xfs_trans_roll(&sc->tp); 179 if (error) 180 return error; 181 182 /* Join the AG headers to the new transaction. */ 183 if (sc->sa.agi_bp) 184 xfs_trans_bjoin(sc->tp, sc->sa.agi_bp); 185 if (sc->sa.agf_bp) 186 xfs_trans_bjoin(sc->tp, sc->sa.agf_bp); 187 188 return 0; 189 } 190 191 /* Roll the scrub transaction, holding the primary metadata locked. */ 192 int 193 xrep_roll_trans( 194 struct xfs_scrub *sc) 195 { 196 if (!sc->ip) 197 return xrep_roll_ag_trans(sc); 198 return xfs_trans_roll_inode(&sc->tp, sc->ip); 199 } 200 201 /* Finish all deferred work attached to the repair transaction. */ 202 int 203 xrep_defer_finish( 204 struct xfs_scrub *sc) 205 { 206 int error; 207 208 /* 209 * Keep the AG header buffers locked while we complete deferred work 210 * items. Ensure that both AG buffers are dirty and held when we roll 211 * the transaction so that they move forward in the log without losing 212 * the bli (and hence the bli type) when the transaction commits. 213 * 214 * Normal code would never hold clean buffers across a roll, but repair 215 * needs both buffers to maintain a total lock on the AG. 216 */ 217 if (sc->sa.agi_bp) { 218 xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM); 219 xfs_trans_bhold(sc->tp, sc->sa.agi_bp); 220 } 221 222 if (sc->sa.agf_bp) { 223 xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM); 224 xfs_trans_bhold(sc->tp, sc->sa.agf_bp); 225 } 226 227 /* 228 * Finish all deferred work items. We still hold the AG header buffers 229 * locked regardless of whether or not that succeeds. On failure, the 230 * buffers will be released during teardown on our way out of the 231 * kernel. If successful, join the buffers to the new transaction 232 * and move on. 233 */ 234 error = xfs_defer_finish(&sc->tp); 235 if (error) 236 return error; 237 238 /* 239 * Release the hold that we set above because defer_finish won't do 240 * that for us. The defer roll code redirties held buffers after each 241 * roll, so the AG header buffers should be ready for logging. 242 */ 243 if (sc->sa.agi_bp) 244 xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp); 245 if (sc->sa.agf_bp) 246 xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp); 247 248 return 0; 249 } 250 251 /* 252 * Does the given AG have enough space to rebuild a btree? Neither AG 253 * reservation can be critical, and we must have enough space (factoring 254 * in AG reservations) to construct a whole btree. 255 */ 256 bool 257 xrep_ag_has_space( 258 struct xfs_perag *pag, 259 xfs_extlen_t nr_blocks, 260 enum xfs_ag_resv_type type) 261 { 262 return !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) && 263 !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) && 264 pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks; 265 } 266 267 /* 268 * Figure out how many blocks to reserve for an AG repair. We calculate the 269 * worst case estimate for the number of blocks we'd need to rebuild one of 270 * any type of per-AG btree. 271 */ 272 xfs_extlen_t 273 xrep_calc_ag_resblks( 274 struct xfs_scrub *sc) 275 { 276 struct xfs_mount *mp = sc->mp; 277 struct xfs_scrub_metadata *sm = sc->sm; 278 struct xfs_perag *pag; 279 struct xfs_buf *bp; 280 xfs_agino_t icount = NULLAGINO; 281 xfs_extlen_t aglen = NULLAGBLOCK; 282 xfs_extlen_t usedlen; 283 xfs_extlen_t freelen; 284 xfs_extlen_t bnobt_sz; 285 xfs_extlen_t inobt_sz; 286 xfs_extlen_t rmapbt_sz; 287 xfs_extlen_t refcbt_sz; 288 int error; 289 290 if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)) 291 return 0; 292 293 pag = xfs_perag_get(mp, sm->sm_agno); 294 if (xfs_perag_initialised_agi(pag)) { 295 /* Use in-core icount if possible. */ 296 icount = pag->pagi_count; 297 } else { 298 /* Try to get the actual counters from disk. */ 299 error = xfs_ialloc_read_agi(pag, NULL, 0, &bp); 300 if (!error) { 301 icount = pag->pagi_count; 302 xfs_buf_relse(bp); 303 } 304 } 305 306 /* Now grab the block counters from the AGF. */ 307 error = xfs_alloc_read_agf(pag, NULL, 0, &bp); 308 if (error) { 309 aglen = pag_group(pag)->xg_block_count; 310 freelen = aglen; 311 usedlen = aglen; 312 } else { 313 struct xfs_agf *agf = bp->b_addr; 314 315 aglen = be32_to_cpu(agf->agf_length); 316 freelen = be32_to_cpu(agf->agf_freeblks); 317 usedlen = aglen - freelen; 318 xfs_buf_relse(bp); 319 } 320 321 /* If the icount is impossible, make some worst-case assumptions. */ 322 if (icount == NULLAGINO || 323 !xfs_verify_agino(pag, icount)) { 324 icount = pag->agino_max - pag->agino_min + 1; 325 } 326 327 /* If the block counts are impossible, make worst-case assumptions. */ 328 if (aglen == NULLAGBLOCK || 329 aglen != pag_group(pag)->xg_block_count || 330 freelen >= aglen) { 331 aglen = pag_group(pag)->xg_block_count; 332 freelen = aglen; 333 usedlen = aglen; 334 } 335 336 trace_xrep_calc_ag_resblks(pag, icount, aglen, freelen, usedlen); 337 338 /* 339 * Figure out how many blocks we'd need worst case to rebuild 340 * each type of btree. Note that we can only rebuild the 341 * bnobt/cntbt or inobt/finobt as pairs. 342 */ 343 bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen); 344 if (xfs_has_sparseinodes(mp)) 345 inobt_sz = xfs_iallocbt_calc_size(mp, icount / 346 XFS_INODES_PER_HOLEMASK_BIT); 347 else 348 inobt_sz = xfs_iallocbt_calc_size(mp, icount / 349 XFS_INODES_PER_CHUNK); 350 if (xfs_has_finobt(mp)) 351 inobt_sz *= 2; 352 if (xfs_has_reflink(mp)) 353 refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen); 354 else 355 refcbt_sz = 0; 356 if (xfs_has_rmapbt(mp)) { 357 /* 358 * Guess how many blocks we need to rebuild the rmapbt. 359 * For non-reflink filesystems we can't have more records than 360 * used blocks. However, with reflink it's possible to have 361 * more than one rmap record per AG block. We don't know how 362 * many rmaps there could be in the AG, so we start off with 363 * what we hope is an generous over-estimation. 364 */ 365 if (xfs_has_reflink(mp)) 366 rmapbt_sz = xfs_rmapbt_calc_size(mp, 367 (unsigned long long)aglen * 2); 368 else 369 rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen); 370 } else { 371 rmapbt_sz = 0; 372 } 373 374 trace_xrep_calc_ag_resblks_btsize(pag, bnobt_sz, inobt_sz, rmapbt_sz, 375 refcbt_sz); 376 xfs_perag_put(pag); 377 378 return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz)); 379 } 380 381 /* 382 * Reconstructing per-AG Btrees 383 * 384 * When a space btree is corrupt, we don't bother trying to fix it. Instead, 385 * we scan secondary space metadata to derive the records that should be in 386 * the damaged btree, initialize a fresh btree root, and insert the records. 387 * Note that for rebuilding the rmapbt we scan all the primary data to 388 * generate the new records. 389 * 390 * However, that leaves the matter of removing all the metadata describing the 391 * old broken structure. For primary metadata we use the rmap data to collect 392 * every extent with a matching rmap owner (bitmap); we then iterate all other 393 * metadata structures with the same rmap owner to collect the extents that 394 * cannot be removed (sublist). We then subtract sublist from bitmap to 395 * derive the blocks that were used by the old btree. These blocks can be 396 * reaped. 397 * 398 * For rmapbt reconstructions we must use different tactics for extent 399 * collection. First we iterate all primary metadata (this excludes the old 400 * rmapbt, obviously) to generate new rmap records. The gaps in the rmap 401 * records are collected as bitmap. The bnobt records are collected as 402 * sublist. As with the other btrees we subtract sublist from bitmap, and the 403 * result (since the rmapbt lives in the free space) are the blocks from the 404 * old rmapbt. 405 */ 406 407 /* Ensure the freelist is the correct size. */ 408 int 409 xrep_fix_freelist( 410 struct xfs_scrub *sc, 411 int alloc_flags) 412 { 413 struct xfs_alloc_arg args = {0}; 414 415 args.mp = sc->mp; 416 args.tp = sc->tp; 417 args.agno = pag_agno(sc->sa.pag); 418 args.alignment = 1; 419 args.pag = sc->sa.pag; 420 421 return xfs_alloc_fix_freelist(&args, alloc_flags); 422 } 423 424 /* 425 * Finding per-AG Btree Roots for AGF/AGI Reconstruction 426 * 427 * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild 428 * the AG headers by using the rmap data to rummage through the AG looking for 429 * btree roots. This is not guaranteed to work if the AG is heavily damaged 430 * or the rmap data are corrupt. 431 * 432 * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL 433 * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the 434 * AGI is being rebuilt. It must maintain these locks until it's safe for 435 * other threads to change the btrees' shapes. The caller provides 436 * information about the btrees to look for by passing in an array of 437 * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set. 438 * The (root, height) fields will be set on return if anything is found. The 439 * last element of the array should have a NULL buf_ops to mark the end of the 440 * array. 441 * 442 * For every rmapbt record matching any of the rmap owners in btree_info, 443 * read each block referenced by the rmap record. If the block is a btree 444 * block from this filesystem matching any of the magic numbers and has a 445 * level higher than what we've already seen, remember the block and the 446 * height of the tree required to have such a block. When the call completes, 447 * we return the highest block we've found for each btree description; those 448 * should be the roots. 449 */ 450 451 struct xrep_findroot { 452 struct xfs_scrub *sc; 453 struct xfs_buf *agfl_bp; 454 struct xfs_agf *agf; 455 struct xrep_find_ag_btree *btree_info; 456 }; 457 458 /* See if our block is in the AGFL. */ 459 STATIC int 460 xrep_findroot_agfl_walk( 461 struct xfs_mount *mp, 462 xfs_agblock_t bno, 463 void *priv) 464 { 465 xfs_agblock_t *agbno = priv; 466 467 return (*agbno == bno) ? -ECANCELED : 0; 468 } 469 470 /* Does this block match the btree information passed in? */ 471 STATIC int 472 xrep_findroot_block( 473 struct xrep_findroot *ri, 474 struct xrep_find_ag_btree *fab, 475 uint64_t owner, 476 xfs_agblock_t agbno, 477 bool *done_with_block) 478 { 479 struct xfs_mount *mp = ri->sc->mp; 480 struct xfs_buf *bp; 481 struct xfs_btree_block *btblock; 482 xfs_daddr_t daddr; 483 int block_level; 484 int error = 0; 485 486 daddr = xfs_agbno_to_daddr(ri->sc->sa.pag, agbno); 487 488 /* 489 * Blocks in the AGFL have stale contents that might just happen to 490 * have a matching magic and uuid. We don't want to pull these blocks 491 * in as part of a tree root, so we have to filter out the AGFL stuff 492 * here. If the AGFL looks insane we'll just refuse to repair. 493 */ 494 if (owner == XFS_RMAP_OWN_AG) { 495 error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp, 496 xrep_findroot_agfl_walk, &agbno); 497 if (error == -ECANCELED) 498 return 0; 499 if (error) 500 return error; 501 } 502 503 /* 504 * Read the buffer into memory so that we can see if it's a match for 505 * our btree type. We have no clue if it is beforehand, and we want to 506 * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which 507 * will cause needless disk reads in subsequent calls to this function) 508 * and logging metadata verifier failures. 509 * 510 * Therefore, pass in NULL buffer ops. If the buffer was already in 511 * memory from some other caller it will already have b_ops assigned. 512 * If it was in memory from a previous unsuccessful findroot_block 513 * call, the buffer won't have b_ops but it should be clean and ready 514 * for us to try to verify if the read call succeeds. The same applies 515 * if the buffer wasn't in memory at all. 516 * 517 * Note: If we never match a btree type with this buffer, it will be 518 * left in memory with NULL b_ops. This shouldn't be a problem unless 519 * the buffer gets written. 520 */ 521 error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr, 522 mp->m_bsize, 0, &bp, NULL); 523 if (error) 524 return error; 525 526 /* Ensure the block magic matches the btree type we're looking for. */ 527 btblock = XFS_BUF_TO_BLOCK(bp); 528 ASSERT(fab->buf_ops->magic[1] != 0); 529 if (btblock->bb_magic != fab->buf_ops->magic[1]) 530 goto out; 531 532 /* 533 * If the buffer already has ops applied and they're not the ones for 534 * this btree type, we know this block doesn't match the btree and we 535 * can bail out. 536 * 537 * If the buffer ops match ours, someone else has already validated 538 * the block for us, so we can move on to checking if this is a root 539 * block candidate. 540 * 541 * If the buffer does not have ops, nobody has successfully validated 542 * the contents and the buffer cannot be dirty. If the magic, uuid, 543 * and structure match this btree type then we'll move on to checking 544 * if it's a root block candidate. If there is no match, bail out. 545 */ 546 if (bp->b_ops) { 547 if (bp->b_ops != fab->buf_ops) 548 goto out; 549 } else { 550 ASSERT(!xfs_trans_buf_is_dirty(bp)); 551 if (!uuid_equal(&btblock->bb_u.s.bb_uuid, 552 &mp->m_sb.sb_meta_uuid)) 553 goto out; 554 /* 555 * Read verifiers can reference b_ops, so we set the pointer 556 * here. If the verifier fails we'll reset the buffer state 557 * to what it was before we touched the buffer. 558 */ 559 bp->b_ops = fab->buf_ops; 560 fab->buf_ops->verify_read(bp); 561 if (bp->b_error) { 562 bp->b_ops = NULL; 563 bp->b_error = 0; 564 goto out; 565 } 566 567 /* 568 * Some read verifiers will (re)set b_ops, so we must be 569 * careful not to change b_ops after running the verifier. 570 */ 571 } 572 573 /* 574 * This block passes the magic/uuid and verifier tests for this btree 575 * type. We don't need the caller to try the other tree types. 576 */ 577 *done_with_block = true; 578 579 /* 580 * Compare this btree block's level to the height of the current 581 * candidate root block. 582 * 583 * If the level matches the root we found previously, throw away both 584 * blocks because there can't be two candidate roots. 585 * 586 * If level is lower in the tree than the root we found previously, 587 * ignore this block. 588 */ 589 block_level = xfs_btree_get_level(btblock); 590 if (block_level + 1 == fab->height) { 591 fab->root = NULLAGBLOCK; 592 goto out; 593 } else if (block_level < fab->height) { 594 goto out; 595 } 596 597 /* 598 * This is the highest block in the tree that we've found so far. 599 * Update the btree height to reflect what we've learned from this 600 * block. 601 */ 602 fab->height = block_level + 1; 603 604 /* 605 * If this block doesn't have sibling pointers, then it's the new root 606 * block candidate. Otherwise, the root will be found farther up the 607 * tree. 608 */ 609 if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) && 610 btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK)) 611 fab->root = agbno; 612 else 613 fab->root = NULLAGBLOCK; 614 615 trace_xrep_findroot_block(ri->sc->sa.pag, agbno, 616 be32_to_cpu(btblock->bb_magic), fab->height - 1); 617 out: 618 xfs_trans_brelse(ri->sc->tp, bp); 619 return error; 620 } 621 622 /* 623 * Do any of the blocks in this rmap record match one of the btrees we're 624 * looking for? 625 */ 626 STATIC int 627 xrep_findroot_rmap( 628 struct xfs_btree_cur *cur, 629 const struct xfs_rmap_irec *rec, 630 void *priv) 631 { 632 struct xrep_findroot *ri = priv; 633 struct xrep_find_ag_btree *fab; 634 xfs_agblock_t b; 635 bool done; 636 int error = 0; 637 638 /* Ignore anything that isn't AG metadata. */ 639 if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner)) 640 return 0; 641 642 /* Otherwise scan each block + btree type. */ 643 for (b = 0; b < rec->rm_blockcount; b++) { 644 done = false; 645 for (fab = ri->btree_info; fab->buf_ops; fab++) { 646 if (rec->rm_owner != fab->rmap_owner) 647 continue; 648 error = xrep_findroot_block(ri, fab, 649 rec->rm_owner, rec->rm_startblock + b, 650 &done); 651 if (error) 652 return error; 653 if (done) 654 break; 655 } 656 } 657 658 return 0; 659 } 660 661 /* Find the roots of the per-AG btrees described in btree_info. */ 662 int 663 xrep_find_ag_btree_roots( 664 struct xfs_scrub *sc, 665 struct xfs_buf *agf_bp, 666 struct xrep_find_ag_btree *btree_info, 667 struct xfs_buf *agfl_bp) 668 { 669 struct xfs_mount *mp = sc->mp; 670 struct xrep_findroot ri; 671 struct xrep_find_ag_btree *fab; 672 struct xfs_btree_cur *cur; 673 int error; 674 675 ASSERT(xfs_buf_islocked(agf_bp)); 676 ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp)); 677 678 ri.sc = sc; 679 ri.btree_info = btree_info; 680 ri.agf = agf_bp->b_addr; 681 ri.agfl_bp = agfl_bp; 682 for (fab = btree_info; fab->buf_ops; fab++) { 683 ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG); 684 ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner)); 685 fab->root = NULLAGBLOCK; 686 fab->height = 0; 687 } 688 689 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag); 690 error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri); 691 xfs_btree_del_cursor(cur, error); 692 693 return error; 694 } 695 696 #ifdef CONFIG_XFS_QUOTA 697 /* Update some quota flags in the superblock. */ 698 void 699 xrep_update_qflags( 700 struct xfs_scrub *sc, 701 unsigned int clear_flags, 702 unsigned int set_flags) 703 { 704 struct xfs_mount *mp = sc->mp; 705 struct xfs_buf *bp; 706 707 mutex_lock(&mp->m_quotainfo->qi_quotaofflock); 708 if ((mp->m_qflags & clear_flags) == 0 && 709 (mp->m_qflags & set_flags) == set_flags) 710 goto no_update; 711 712 mp->m_qflags &= ~clear_flags; 713 mp->m_qflags |= set_flags; 714 715 spin_lock(&mp->m_sb_lock); 716 mp->m_sb.sb_qflags &= ~clear_flags; 717 mp->m_sb.sb_qflags |= set_flags; 718 spin_unlock(&mp->m_sb_lock); 719 720 /* 721 * Update the quota flags in the ondisk superblock without touching 722 * the summary counters. We have not quiesced inode chunk allocation, 723 * so we cannot coordinate with updates to the icount and ifree percpu 724 * counters. 725 */ 726 bp = xfs_trans_getsb(sc->tp); 727 xfs_sb_to_disk(bp->b_addr, &mp->m_sb); 728 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF); 729 xfs_trans_log_buf(sc->tp, bp, 0, sizeof(struct xfs_dsb) - 1); 730 731 no_update: 732 mutex_unlock(&mp->m_quotainfo->qi_quotaofflock); 733 } 734 735 /* Force a quotacheck the next time we mount. */ 736 void 737 xrep_force_quotacheck( 738 struct xfs_scrub *sc, 739 xfs_dqtype_t type) 740 { 741 uint flag; 742 743 flag = xfs_quota_chkd_flag(type); 744 if (!(flag & sc->mp->m_qflags)) 745 return; 746 747 xrep_update_qflags(sc, flag, 0); 748 } 749 750 /* 751 * Attach dquots to this inode, or schedule quotacheck to fix them. 752 * 753 * This function ensures that the appropriate dquots are attached to an inode. 754 * We cannot allow the dquot code to allocate an on-disk dquot block here 755 * because we're already in transaction context. The on-disk dquot should 756 * already exist anyway. If the quota code signals corruption or missing quota 757 * information, schedule quotacheck, which will repair corruptions in the quota 758 * metadata. 759 */ 760 int 761 xrep_ino_dqattach( 762 struct xfs_scrub *sc) 763 { 764 int error; 765 766 ASSERT(sc->tp != NULL); 767 ASSERT(sc->ip != NULL); 768 769 error = xfs_qm_dqattach(sc->ip); 770 switch (error) { 771 case -EFSBADCRC: 772 case -EFSCORRUPTED: 773 case -ENOENT: 774 xfs_err_ratelimited(sc->mp, 775 "inode %llu repair encountered quota error %d, quotacheck forced.", 776 (unsigned long long)sc->ip->i_ino, error); 777 if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot) 778 xrep_force_quotacheck(sc, XFS_DQTYPE_USER); 779 if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot) 780 xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP); 781 if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot) 782 xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ); 783 fallthrough; 784 case -ESRCH: 785 error = 0; 786 break; 787 default: 788 break; 789 } 790 791 return error; 792 } 793 #endif /* CONFIG_XFS_QUOTA */ 794 795 /* 796 * Ensure that the inode being repaired is ready to handle a certain number of 797 * extents, or return EFSCORRUPTED. Caller must hold the ILOCK of the inode 798 * being repaired and have joined it to the scrub transaction. 799 */ 800 int 801 xrep_ino_ensure_extent_count( 802 struct xfs_scrub *sc, 803 int whichfork, 804 xfs_extnum_t nextents) 805 { 806 xfs_extnum_t max_extents; 807 bool inode_has_nrext64; 808 809 inode_has_nrext64 = xfs_inode_has_large_extent_counts(sc->ip); 810 max_extents = xfs_iext_max_nextents(inode_has_nrext64, whichfork); 811 if (nextents <= max_extents) 812 return 0; 813 if (inode_has_nrext64) 814 return -EFSCORRUPTED; 815 if (!xfs_has_large_extent_counts(sc->mp)) 816 return -EFSCORRUPTED; 817 818 max_extents = xfs_iext_max_nextents(true, whichfork); 819 if (nextents > max_extents) 820 return -EFSCORRUPTED; 821 822 sc->ip->i_diflags2 |= XFS_DIFLAG2_NREXT64; 823 xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE); 824 return 0; 825 } 826 827 /* 828 * Initialize all the btree cursors for an AG repair except for the btree that 829 * we're rebuilding. 830 */ 831 void 832 xrep_ag_btcur_init( 833 struct xfs_scrub *sc, 834 struct xchk_ag *sa) 835 { 836 struct xfs_mount *mp = sc->mp; 837 838 /* Set up a bnobt cursor for cross-referencing. */ 839 if (sc->sm->sm_type != XFS_SCRUB_TYPE_BNOBT && 840 sc->sm->sm_type != XFS_SCRUB_TYPE_CNTBT) { 841 sa->bno_cur = xfs_bnobt_init_cursor(mp, sc->tp, sa->agf_bp, 842 sc->sa.pag); 843 sa->cnt_cur = xfs_cntbt_init_cursor(mp, sc->tp, sa->agf_bp, 844 sc->sa.pag); 845 } 846 847 /* Set up a inobt cursor for cross-referencing. */ 848 if (sc->sm->sm_type != XFS_SCRUB_TYPE_INOBT && 849 sc->sm->sm_type != XFS_SCRUB_TYPE_FINOBT) { 850 sa->ino_cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, 851 sa->agi_bp); 852 if (xfs_has_finobt(mp)) 853 sa->fino_cur = xfs_finobt_init_cursor(sc->sa.pag, 854 sc->tp, sa->agi_bp); 855 } 856 857 /* Set up a rmapbt cursor for cross-referencing. */ 858 if (sc->sm->sm_type != XFS_SCRUB_TYPE_RMAPBT && 859 xfs_has_rmapbt(mp)) 860 sa->rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, sa->agf_bp, 861 sc->sa.pag); 862 863 /* Set up a refcountbt cursor for cross-referencing. */ 864 if (sc->sm->sm_type != XFS_SCRUB_TYPE_REFCNTBT && 865 xfs_has_reflink(mp)) 866 sa->refc_cur = xfs_refcountbt_init_cursor(mp, sc->tp, 867 sa->agf_bp, sc->sa.pag); 868 } 869 870 /* 871 * Reinitialize the in-core AG state after a repair by rereading the AGF 872 * buffer. We had better get the same AGF buffer as the one that's attached 873 * to the scrub context. 874 */ 875 int 876 xrep_reinit_pagf( 877 struct xfs_scrub *sc) 878 { 879 struct xfs_perag *pag = sc->sa.pag; 880 struct xfs_buf *bp; 881 int error; 882 883 ASSERT(pag); 884 ASSERT(xfs_perag_initialised_agf(pag)); 885 886 clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate); 887 error = xfs_alloc_read_agf(pag, sc->tp, 0, &bp); 888 if (error) 889 return error; 890 891 if (bp != sc->sa.agf_bp) { 892 ASSERT(bp == sc->sa.agf_bp); 893 return -EFSCORRUPTED; 894 } 895 896 return 0; 897 } 898 899 /* 900 * Reinitialize the in-core AG state after a repair by rereading the AGI 901 * buffer. We had better get the same AGI buffer as the one that's attached 902 * to the scrub context. 903 */ 904 int 905 xrep_reinit_pagi( 906 struct xfs_scrub *sc) 907 { 908 struct xfs_perag *pag = sc->sa.pag; 909 struct xfs_buf *bp; 910 int error; 911 912 ASSERT(pag); 913 ASSERT(xfs_perag_initialised_agi(pag)); 914 915 clear_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate); 916 error = xfs_ialloc_read_agi(pag, sc->tp, 0, &bp); 917 if (error) 918 return error; 919 920 if (bp != sc->sa.agi_bp) { 921 ASSERT(bp == sc->sa.agi_bp); 922 return -EFSCORRUPTED; 923 } 924 925 return 0; 926 } 927 928 /* 929 * Given an active reference to a perag structure, load AG headers and cursors. 930 * This should only be called to scan an AG while repairing file-based metadata. 931 */ 932 int 933 xrep_ag_init( 934 struct xfs_scrub *sc, 935 struct xfs_perag *pag, 936 struct xchk_ag *sa) 937 { 938 int error; 939 940 ASSERT(!sa->pag); 941 942 error = xfs_ialloc_read_agi(pag, sc->tp, 0, &sa->agi_bp); 943 if (error) 944 return error; 945 946 error = xfs_alloc_read_agf(pag, sc->tp, 0, &sa->agf_bp); 947 if (error) 948 return error; 949 950 /* Grab our own passive reference from the caller's ref. */ 951 sa->pag = xfs_perag_hold(pag); 952 xrep_ag_btcur_init(sc, sa); 953 return 0; 954 } 955 956 #ifdef CONFIG_XFS_RT 957 /* 958 * Given a reference to a rtgroup structure, lock rtgroup btree inodes and 959 * create btree cursors. Must only be called to repair a regular rt file. 960 */ 961 int 962 xrep_rtgroup_init( 963 struct xfs_scrub *sc, 964 struct xfs_rtgroup *rtg, 965 struct xchk_rt *sr, 966 unsigned int rtglock_flags) 967 { 968 ASSERT(sr->rtg == NULL); 969 970 xfs_rtgroup_lock(rtg, rtglock_flags); 971 sr->rtlock_flags = rtglock_flags; 972 973 /* Grab our own passive reference from the caller's ref. */ 974 sr->rtg = xfs_rtgroup_hold(rtg); 975 return 0; 976 } 977 #endif /* CONFIG_XFS_RT */ 978 979 /* Reinitialize the per-AG block reservation for the AG we just fixed. */ 980 int 981 xrep_reset_perag_resv( 982 struct xfs_scrub *sc) 983 { 984 int error; 985 986 if (!(sc->flags & XREP_RESET_PERAG_RESV)) 987 return 0; 988 989 ASSERT(sc->sa.pag != NULL); 990 ASSERT(sc->ops->type == ST_PERAG); 991 ASSERT(sc->tp); 992 993 sc->flags &= ~XREP_RESET_PERAG_RESV; 994 xfs_ag_resv_free(sc->sa.pag); 995 error = xfs_ag_resv_init(sc->sa.pag, sc->tp); 996 if (error == -ENOSPC) { 997 xfs_err(sc->mp, 998 "Insufficient free space to reset per-AG reservation for AG %u after repair.", 999 pag_agno(sc->sa.pag)); 1000 error = 0; 1001 } 1002 1003 return error; 1004 } 1005 1006 /* Decide if we are going to call the repair function for a scrub type. */ 1007 bool 1008 xrep_will_attempt( 1009 struct xfs_scrub *sc) 1010 { 1011 /* Userspace asked us to rebuild the structure regardless. */ 1012 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_FORCE_REBUILD) 1013 return true; 1014 1015 /* Let debug users force us into the repair routines. */ 1016 if (XFS_TEST_ERROR(false, sc->mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR)) 1017 return true; 1018 1019 /* Metadata is corrupt or failed cross-referencing. */ 1020 if (xchk_needs_repair(sc->sm)) 1021 return true; 1022 1023 return false; 1024 } 1025 1026 /* Try to fix some part of a metadata inode by calling another scrubber. */ 1027 STATIC int 1028 xrep_metadata_inode_subtype( 1029 struct xfs_scrub *sc, 1030 unsigned int scrub_type) 1031 { 1032 struct xfs_scrub_subord *sub; 1033 int error; 1034 1035 /* 1036 * Let's see if the inode needs repair. Use a subordinate scrub context 1037 * to call the scrub and repair functions so that we can hang on to the 1038 * resources that we already acquired instead of using the standard 1039 * setup/teardown routines. 1040 */ 1041 sub = xchk_scrub_create_subord(sc, scrub_type); 1042 error = sub->sc.ops->scrub(&sub->sc); 1043 if (error) 1044 goto out; 1045 if (!xrep_will_attempt(&sub->sc)) 1046 goto out; 1047 1048 /* 1049 * Repair some part of the inode. This will potentially join the inode 1050 * to the transaction. 1051 */ 1052 error = sub->sc.ops->repair(&sub->sc); 1053 if (error) 1054 goto out; 1055 1056 /* 1057 * Finish all deferred intent items and then roll the transaction so 1058 * that the inode will not be joined to the transaction when we exit 1059 * the function. 1060 */ 1061 error = xfs_defer_finish(&sub->sc.tp); 1062 if (error) 1063 goto out; 1064 error = xfs_trans_roll(&sub->sc.tp); 1065 if (error) 1066 goto out; 1067 1068 /* 1069 * Clear the corruption flags and re-check the metadata that we just 1070 * repaired. 1071 */ 1072 sub->sc.sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT; 1073 error = sub->sc.ops->scrub(&sub->sc); 1074 if (error) 1075 goto out; 1076 1077 /* If corruption persists, the repair has failed. */ 1078 if (xchk_needs_repair(sub->sc.sm)) { 1079 error = -EFSCORRUPTED; 1080 goto out; 1081 } 1082 out: 1083 xchk_scrub_free_subord(sub); 1084 return error; 1085 } 1086 1087 /* 1088 * Repair the ondisk forks of a metadata inode. The caller must ensure that 1089 * sc->ip points to the metadata inode and the ILOCK is held on that inode. 1090 * The inode must not be joined to the transaction before the call, and will 1091 * not be afterwards. 1092 */ 1093 int 1094 xrep_metadata_inode_forks( 1095 struct xfs_scrub *sc) 1096 { 1097 bool dirty = false; 1098 int error; 1099 1100 /* Repair the inode record and the data fork. */ 1101 error = xrep_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_INODE); 1102 if (error) 1103 return error; 1104 1105 error = xrep_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTD); 1106 if (error) 1107 return error; 1108 1109 /* 1110 * Metadata files can only have extended attributes on metadir 1111 * filesystems, either for parent pointers or for actual xattr data. 1112 * For a non-metadir filesystem, make sure the attr fork looks ok 1113 * before we delete it. 1114 */ 1115 if (xfs_inode_hasattr(sc->ip)) { 1116 error = xrep_metadata_inode_subtype(sc, XFS_SCRUB_TYPE_BMBTA); 1117 if (error) 1118 return error; 1119 } 1120 1121 /* Clear the reflink flag since metadata never shares. */ 1122 if (xfs_is_reflink_inode(sc->ip)) { 1123 dirty = true; 1124 xfs_trans_ijoin(sc->tp, sc->ip, 0); 1125 error = xfs_reflink_clear_inode_flag(sc->ip, &sc->tp); 1126 if (error) 1127 return error; 1128 } 1129 1130 /* 1131 * Metadata files on non-metadir filesystems cannot have attr forks, 1132 * so clear them now. 1133 */ 1134 if (xfs_inode_hasattr(sc->ip) && !xfs_has_metadir(sc->mp)) { 1135 if (!dirty) { 1136 dirty = true; 1137 xfs_trans_ijoin(sc->tp, sc->ip, 0); 1138 } 1139 error = xrep_xattr_reset_fork(sc); 1140 if (error) 1141 return error; 1142 } 1143 1144 /* 1145 * If we modified the inode, roll the transaction but don't rejoin the 1146 * inode to the new transaction because xrep_bmap_data can do that. 1147 */ 1148 if (dirty) { 1149 error = xfs_trans_roll(&sc->tp); 1150 if (error) 1151 return error; 1152 dirty = false; 1153 } 1154 1155 return 0; 1156 } 1157 1158 /* 1159 * Set up an in-memory buffer cache so that we can use the xfbtree. Allocating 1160 * a shmem file might take loks, so we cannot be in transaction context. Park 1161 * our resources in the scrub context and let the teardown function take care 1162 * of them at the right time. 1163 */ 1164 int 1165 xrep_setup_xfbtree( 1166 struct xfs_scrub *sc, 1167 const char *descr) 1168 { 1169 ASSERT(sc->tp == NULL); 1170 1171 return xmbuf_alloc(sc->mp, descr, &sc->xmbtp); 1172 } 1173 1174 /* 1175 * Create a dummy transaction for use in a live update hook function. This 1176 * function MUST NOT be called from regular repair code because the current 1177 * process' transaction is saved via the cookie. 1178 */ 1179 int 1180 xrep_trans_alloc_hook_dummy( 1181 struct xfs_mount *mp, 1182 void **cookiep, 1183 struct xfs_trans **tpp) 1184 { 1185 int error; 1186 1187 *cookiep = current->journal_info; 1188 current->journal_info = NULL; 1189 1190 error = xfs_trans_alloc_empty(mp, tpp); 1191 if (!error) 1192 return 0; 1193 1194 current->journal_info = *cookiep; 1195 *cookiep = NULL; 1196 return error; 1197 } 1198 1199 /* Cancel a dummy transaction used by a live update hook function. */ 1200 void 1201 xrep_trans_cancel_hook_dummy( 1202 void **cookiep, 1203 struct xfs_trans *tp) 1204 { 1205 xfs_trans_cancel(tp); 1206 current->journal_info = *cookiep; 1207 *cookiep = NULL; 1208 } 1209 1210 /* 1211 * See if this buffer can pass the given ->verify_struct() function. 1212 * 1213 * If the buffer already has ops attached and they're not the ones that were 1214 * passed in, we reject the buffer. Otherwise, we perform the structure test 1215 * (note that we do not check CRCs) and return the outcome of the test. The 1216 * buffer ops and error state are left unchanged. 1217 */ 1218 bool 1219 xrep_buf_verify_struct( 1220 struct xfs_buf *bp, 1221 const struct xfs_buf_ops *ops) 1222 { 1223 const struct xfs_buf_ops *old_ops = bp->b_ops; 1224 xfs_failaddr_t fa; 1225 int old_error; 1226 1227 if (old_ops) { 1228 if (old_ops != ops) 1229 return false; 1230 } 1231 1232 old_error = bp->b_error; 1233 bp->b_ops = ops; 1234 fa = bp->b_ops->verify_struct(bp); 1235 bp->b_ops = old_ops; 1236 bp->b_error = old_error; 1237 1238 return fa == NULL; 1239 } 1240