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