1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2018 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_defer.h" 13 #include "xfs_btree.h" 14 #include "xfs_bit.h" 15 #include "xfs_log_format.h" 16 #include "xfs_trans.h" 17 #include "xfs_sb.h" 18 #include "xfs_inode.h" 19 #include "xfs_alloc.h" 20 #include "xfs_alloc_btree.h" 21 #include "xfs_ialloc.h" 22 #include "xfs_ialloc_btree.h" 23 #include "xfs_rmap.h" 24 #include "xfs_rmap_btree.h" 25 #include "xfs_refcount.h" 26 #include "xfs_refcount_btree.h" 27 #include "scrub/xfs_scrub.h" 28 #include "scrub/scrub.h" 29 #include "scrub/common.h" 30 #include "scrub/trace.h" 31 #include "scrub/repair.h" 32 #include "scrub/bitmap.h" 33 34 /* Superblock */ 35 36 /* Repair the superblock. */ 37 int 38 xrep_superblock( 39 struct xfs_scrub *sc) 40 { 41 struct xfs_mount *mp = sc->mp; 42 struct xfs_buf *bp; 43 xfs_agnumber_t agno; 44 int error; 45 46 /* Don't try to repair AG 0's sb; let xfs_repair deal with it. */ 47 agno = sc->sm->sm_agno; 48 if (agno == 0) 49 return -EOPNOTSUPP; 50 51 error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp); 52 if (error) 53 return error; 54 55 /* Copy AG 0's superblock to this one. */ 56 xfs_buf_zero(bp, 0, BBTOB(bp->b_length)); 57 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb); 58 59 /* Write this to disk. */ 60 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF); 61 xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1); 62 return error; 63 } 64 65 /* AGF */ 66 67 struct xrep_agf_allocbt { 68 struct xfs_scrub *sc; 69 xfs_agblock_t freeblks; 70 xfs_agblock_t longest; 71 }; 72 73 /* Record free space shape information. */ 74 STATIC int 75 xrep_agf_walk_allocbt( 76 struct xfs_btree_cur *cur, 77 struct xfs_alloc_rec_incore *rec, 78 void *priv) 79 { 80 struct xrep_agf_allocbt *raa = priv; 81 int error = 0; 82 83 if (xchk_should_terminate(raa->sc, &error)) 84 return error; 85 86 raa->freeblks += rec->ar_blockcount; 87 if (rec->ar_blockcount > raa->longest) 88 raa->longest = rec->ar_blockcount; 89 return error; 90 } 91 92 /* Does this AGFL block look sane? */ 93 STATIC int 94 xrep_agf_check_agfl_block( 95 struct xfs_mount *mp, 96 xfs_agblock_t agbno, 97 void *priv) 98 { 99 struct xfs_scrub *sc = priv; 100 101 if (!xfs_verify_agbno(mp, sc->sa.agno, agbno)) 102 return -EFSCORRUPTED; 103 return 0; 104 } 105 106 /* 107 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the 108 * XFS_BTNUM_ names here to avoid creating a sparse array. 109 */ 110 enum { 111 XREP_AGF_BNOBT = 0, 112 XREP_AGF_CNTBT, 113 XREP_AGF_RMAPBT, 114 XREP_AGF_REFCOUNTBT, 115 XREP_AGF_END, 116 XREP_AGF_MAX 117 }; 118 119 /* Check a btree root candidate. */ 120 static inline bool 121 xrep_check_btree_root( 122 struct xfs_scrub *sc, 123 struct xrep_find_ag_btree *fab) 124 { 125 struct xfs_mount *mp = sc->mp; 126 xfs_agnumber_t agno = sc->sm->sm_agno; 127 128 return xfs_verify_agbno(mp, agno, fab->root) && 129 fab->height <= XFS_BTREE_MAXLEVELS; 130 } 131 132 /* 133 * Given the btree roots described by *fab, find the roots, check them for 134 * sanity, and pass the root data back out via *fab. 135 * 136 * This is /also/ a chicken and egg problem because we have to use the rmapbt 137 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no 138 * idea if the btrees make any sense. If we hit obvious corruptions in those 139 * btrees we'll bail out. 140 */ 141 STATIC int 142 xrep_agf_find_btrees( 143 struct xfs_scrub *sc, 144 struct xfs_buf *agf_bp, 145 struct xrep_find_ag_btree *fab, 146 struct xfs_buf *agfl_bp) 147 { 148 struct xfs_agf *old_agf = XFS_BUF_TO_AGF(agf_bp); 149 int error; 150 151 /* Go find the root data. */ 152 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp); 153 if (error) 154 return error; 155 156 /* We must find the bnobt, cntbt, and rmapbt roots. */ 157 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) || 158 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) || 159 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT])) 160 return -EFSCORRUPTED; 161 162 /* 163 * We relied on the rmapbt to reconstruct the AGF. If we get a 164 * different root then something's seriously wrong. 165 */ 166 if (fab[XREP_AGF_RMAPBT].root != 167 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi])) 168 return -EFSCORRUPTED; 169 170 /* We must find the refcountbt root if that feature is enabled. */ 171 if (xfs_sb_version_hasreflink(&sc->mp->m_sb) && 172 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT])) 173 return -EFSCORRUPTED; 174 175 return 0; 176 } 177 178 /* 179 * Reinitialize the AGF header, making an in-core copy of the old contents so 180 * that we know which in-core state needs to be reinitialized. 181 */ 182 STATIC void 183 xrep_agf_init_header( 184 struct xfs_scrub *sc, 185 struct xfs_buf *agf_bp, 186 struct xfs_agf *old_agf) 187 { 188 struct xfs_mount *mp = sc->mp; 189 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp); 190 191 memcpy(old_agf, agf, sizeof(*old_agf)); 192 memset(agf, 0, BBTOB(agf_bp->b_length)); 193 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC); 194 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION); 195 agf->agf_seqno = cpu_to_be32(sc->sa.agno); 196 agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno)); 197 agf->agf_flfirst = old_agf->agf_flfirst; 198 agf->agf_fllast = old_agf->agf_fllast; 199 agf->agf_flcount = old_agf->agf_flcount; 200 if (xfs_sb_version_hascrc(&mp->m_sb)) 201 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid); 202 203 /* Mark the incore AGF data stale until we're done fixing things. */ 204 ASSERT(sc->sa.pag->pagf_init); 205 sc->sa.pag->pagf_init = 0; 206 } 207 208 /* Set btree root information in an AGF. */ 209 STATIC void 210 xrep_agf_set_roots( 211 struct xfs_scrub *sc, 212 struct xfs_agf *agf, 213 struct xrep_find_ag_btree *fab) 214 { 215 agf->agf_roots[XFS_BTNUM_BNOi] = 216 cpu_to_be32(fab[XREP_AGF_BNOBT].root); 217 agf->agf_levels[XFS_BTNUM_BNOi] = 218 cpu_to_be32(fab[XREP_AGF_BNOBT].height); 219 220 agf->agf_roots[XFS_BTNUM_CNTi] = 221 cpu_to_be32(fab[XREP_AGF_CNTBT].root); 222 agf->agf_levels[XFS_BTNUM_CNTi] = 223 cpu_to_be32(fab[XREP_AGF_CNTBT].height); 224 225 agf->agf_roots[XFS_BTNUM_RMAPi] = 226 cpu_to_be32(fab[XREP_AGF_RMAPBT].root); 227 agf->agf_levels[XFS_BTNUM_RMAPi] = 228 cpu_to_be32(fab[XREP_AGF_RMAPBT].height); 229 230 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) { 231 agf->agf_refcount_root = 232 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root); 233 agf->agf_refcount_level = 234 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height); 235 } 236 } 237 238 /* Update all AGF fields which derive from btree contents. */ 239 STATIC int 240 xrep_agf_calc_from_btrees( 241 struct xfs_scrub *sc, 242 struct xfs_buf *agf_bp) 243 { 244 struct xrep_agf_allocbt raa = { .sc = sc }; 245 struct xfs_btree_cur *cur = NULL; 246 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp); 247 struct xfs_mount *mp = sc->mp; 248 xfs_agblock_t btreeblks; 249 xfs_agblock_t blocks; 250 int error; 251 252 /* Update the AGF counters from the bnobt. */ 253 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno, 254 XFS_BTNUM_BNO); 255 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa); 256 if (error) 257 goto err; 258 error = xfs_btree_count_blocks(cur, &blocks); 259 if (error) 260 goto err; 261 xfs_btree_del_cursor(cur, error); 262 btreeblks = blocks - 1; 263 agf->agf_freeblks = cpu_to_be32(raa.freeblks); 264 agf->agf_longest = cpu_to_be32(raa.longest); 265 266 /* Update the AGF counters from the cntbt. */ 267 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno, 268 XFS_BTNUM_CNT); 269 error = xfs_btree_count_blocks(cur, &blocks); 270 if (error) 271 goto err; 272 xfs_btree_del_cursor(cur, error); 273 btreeblks += blocks - 1; 274 275 /* Update the AGF counters from the rmapbt. */ 276 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno); 277 error = xfs_btree_count_blocks(cur, &blocks); 278 if (error) 279 goto err; 280 xfs_btree_del_cursor(cur, error); 281 agf->agf_rmap_blocks = cpu_to_be32(blocks); 282 btreeblks += blocks - 1; 283 284 agf->agf_btreeblks = cpu_to_be32(btreeblks); 285 286 /* Update the AGF counters from the refcountbt. */ 287 if (xfs_sb_version_hasreflink(&mp->m_sb)) { 288 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp, 289 sc->sa.agno); 290 error = xfs_btree_count_blocks(cur, &blocks); 291 if (error) 292 goto err; 293 xfs_btree_del_cursor(cur, error); 294 agf->agf_refcount_blocks = cpu_to_be32(blocks); 295 } 296 297 return 0; 298 err: 299 xfs_btree_del_cursor(cur, error); 300 return error; 301 } 302 303 /* Commit the new AGF and reinitialize the incore state. */ 304 STATIC int 305 xrep_agf_commit_new( 306 struct xfs_scrub *sc, 307 struct xfs_buf *agf_bp) 308 { 309 struct xfs_perag *pag; 310 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp); 311 312 /* Trigger fdblocks recalculation */ 313 xfs_force_summary_recalc(sc->mp); 314 315 /* Write this to disk. */ 316 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF); 317 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1); 318 319 /* Now reinitialize the in-core counters we changed. */ 320 pag = sc->sa.pag; 321 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks); 322 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks); 323 pag->pagf_longest = be32_to_cpu(agf->agf_longest); 324 pag->pagf_levels[XFS_BTNUM_BNOi] = 325 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]); 326 pag->pagf_levels[XFS_BTNUM_CNTi] = 327 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]); 328 pag->pagf_levels[XFS_BTNUM_RMAPi] = 329 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]); 330 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level); 331 pag->pagf_init = 1; 332 333 return 0; 334 } 335 336 /* Repair the AGF. v5 filesystems only. */ 337 int 338 xrep_agf( 339 struct xfs_scrub *sc) 340 { 341 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = { 342 [XREP_AGF_BNOBT] = { 343 .rmap_owner = XFS_RMAP_OWN_AG, 344 .buf_ops = &xfs_bnobt_buf_ops, 345 }, 346 [XREP_AGF_CNTBT] = { 347 .rmap_owner = XFS_RMAP_OWN_AG, 348 .buf_ops = &xfs_cntbt_buf_ops, 349 }, 350 [XREP_AGF_RMAPBT] = { 351 .rmap_owner = XFS_RMAP_OWN_AG, 352 .buf_ops = &xfs_rmapbt_buf_ops, 353 }, 354 [XREP_AGF_REFCOUNTBT] = { 355 .rmap_owner = XFS_RMAP_OWN_REFC, 356 .buf_ops = &xfs_refcountbt_buf_ops, 357 }, 358 [XREP_AGF_END] = { 359 .buf_ops = NULL, 360 }, 361 }; 362 struct xfs_agf old_agf; 363 struct xfs_mount *mp = sc->mp; 364 struct xfs_buf *agf_bp; 365 struct xfs_buf *agfl_bp; 366 struct xfs_agf *agf; 367 int error; 368 369 /* We require the rmapbt to rebuild anything. */ 370 if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) 371 return -EOPNOTSUPP; 372 373 xchk_perag_get(sc->mp, &sc->sa); 374 /* 375 * Make sure we have the AGF buffer, as scrub might have decided it 376 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED. 377 */ 378 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp, 379 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGF_DADDR(mp)), 380 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL); 381 if (error) 382 return error; 383 agf_bp->b_ops = &xfs_agf_buf_ops; 384 agf = XFS_BUF_TO_AGF(agf_bp); 385 386 /* 387 * Load the AGFL so that we can screen out OWN_AG blocks that are on 388 * the AGFL now; these blocks might have once been part of the 389 * bno/cnt/rmap btrees but are not now. This is a chicken and egg 390 * problem: the AGF is corrupt, so we have to trust the AGFL contents 391 * because we can't do any serious cross-referencing with any of the 392 * btrees rooted in the AGF. If the AGFL contents are obviously bad 393 * then we'll bail out. 394 */ 395 error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.agno, &agfl_bp); 396 if (error) 397 return error; 398 399 /* 400 * Spot-check the AGFL blocks; if they're obviously corrupt then 401 * there's nothing we can do but bail out. 402 */ 403 error = xfs_agfl_walk(sc->mp, XFS_BUF_TO_AGF(agf_bp), agfl_bp, 404 xrep_agf_check_agfl_block, sc); 405 if (error) 406 return error; 407 408 /* 409 * Find the AGF btree roots. This is also a chicken-and-egg situation; 410 * see the function for more details. 411 */ 412 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp); 413 if (error) 414 return error; 415 416 /* Start rewriting the header and implant the btrees we found. */ 417 xrep_agf_init_header(sc, agf_bp, &old_agf); 418 xrep_agf_set_roots(sc, agf, fab); 419 error = xrep_agf_calc_from_btrees(sc, agf_bp); 420 if (error) 421 goto out_revert; 422 423 /* Commit the changes and reinitialize incore state. */ 424 return xrep_agf_commit_new(sc, agf_bp); 425 426 out_revert: 427 /* Mark the incore AGF state stale and revert the AGF. */ 428 sc->sa.pag->pagf_init = 0; 429 memcpy(agf, &old_agf, sizeof(old_agf)); 430 return error; 431 } 432 433 /* AGFL */ 434 435 struct xrep_agfl { 436 /* Bitmap of other OWN_AG metadata blocks. */ 437 struct xfs_bitmap agmetablocks; 438 439 /* Bitmap of free space. */ 440 struct xfs_bitmap *freesp; 441 442 struct xfs_scrub *sc; 443 }; 444 445 /* Record all OWN_AG (free space btree) information from the rmap data. */ 446 STATIC int 447 xrep_agfl_walk_rmap( 448 struct xfs_btree_cur *cur, 449 struct xfs_rmap_irec *rec, 450 void *priv) 451 { 452 struct xrep_agfl *ra = priv; 453 xfs_fsblock_t fsb; 454 int error = 0; 455 456 if (xchk_should_terminate(ra->sc, &error)) 457 return error; 458 459 /* Record all the OWN_AG blocks. */ 460 if (rec->rm_owner == XFS_RMAP_OWN_AG) { 461 fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_private.a.agno, 462 rec->rm_startblock); 463 error = xfs_bitmap_set(ra->freesp, fsb, rec->rm_blockcount); 464 if (error) 465 return error; 466 } 467 468 return xfs_bitmap_set_btcur_path(&ra->agmetablocks, cur); 469 } 470 471 /* 472 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce 473 * which blocks belong to the AGFL. 474 * 475 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG 476 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt, 477 * rmapbt). These are the old AGFL blocks, so return that list and the number 478 * of blocks we're actually going to put back on the AGFL. 479 */ 480 STATIC int 481 xrep_agfl_collect_blocks( 482 struct xfs_scrub *sc, 483 struct xfs_buf *agf_bp, 484 struct xfs_bitmap *agfl_extents, 485 xfs_agblock_t *flcount) 486 { 487 struct xrep_agfl ra; 488 struct xfs_mount *mp = sc->mp; 489 struct xfs_btree_cur *cur; 490 struct xfs_bitmap_range *br; 491 struct xfs_bitmap_range *n; 492 int error; 493 494 ra.sc = sc; 495 ra.freesp = agfl_extents; 496 xfs_bitmap_init(&ra.agmetablocks); 497 498 /* Find all space used by the free space btrees & rmapbt. */ 499 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno); 500 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra); 501 if (error) 502 goto err; 503 xfs_btree_del_cursor(cur, error); 504 505 /* Find all blocks currently being used by the bnobt. */ 506 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno, 507 XFS_BTNUM_BNO); 508 error = xfs_bitmap_set_btblocks(&ra.agmetablocks, cur); 509 if (error) 510 goto err; 511 xfs_btree_del_cursor(cur, error); 512 513 /* Find all blocks currently being used by the cntbt. */ 514 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno, 515 XFS_BTNUM_CNT); 516 error = xfs_bitmap_set_btblocks(&ra.agmetablocks, cur); 517 if (error) 518 goto err; 519 520 xfs_btree_del_cursor(cur, error); 521 522 /* 523 * Drop the freesp meta blocks that are in use by btrees. 524 * The remaining blocks /should/ be AGFL blocks. 525 */ 526 error = xfs_bitmap_disunion(agfl_extents, &ra.agmetablocks); 527 xfs_bitmap_destroy(&ra.agmetablocks); 528 if (error) 529 return error; 530 531 /* 532 * Calculate the new AGFL size. If we found more blocks than fit in 533 * the AGFL we'll free them later. 534 */ 535 *flcount = 0; 536 for_each_xfs_bitmap_extent(br, n, agfl_extents) { 537 *flcount += br->len; 538 if (*flcount > xfs_agfl_size(mp)) 539 break; 540 } 541 if (*flcount > xfs_agfl_size(mp)) 542 *flcount = xfs_agfl_size(mp); 543 return 0; 544 545 err: 546 xfs_bitmap_destroy(&ra.agmetablocks); 547 xfs_btree_del_cursor(cur, error); 548 return error; 549 } 550 551 /* Update the AGF and reset the in-core state. */ 552 STATIC void 553 xrep_agfl_update_agf( 554 struct xfs_scrub *sc, 555 struct xfs_buf *agf_bp, 556 xfs_agblock_t flcount) 557 { 558 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp); 559 560 ASSERT(flcount <= xfs_agfl_size(sc->mp)); 561 562 /* Trigger fdblocks recalculation */ 563 xfs_force_summary_recalc(sc->mp); 564 565 /* Update the AGF counters. */ 566 if (sc->sa.pag->pagf_init) 567 sc->sa.pag->pagf_flcount = flcount; 568 agf->agf_flfirst = cpu_to_be32(0); 569 agf->agf_flcount = cpu_to_be32(flcount); 570 agf->agf_fllast = cpu_to_be32(flcount - 1); 571 572 xfs_alloc_log_agf(sc->tp, agf_bp, 573 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT); 574 } 575 576 /* Write out a totally new AGFL. */ 577 STATIC void 578 xrep_agfl_init_header( 579 struct xfs_scrub *sc, 580 struct xfs_buf *agfl_bp, 581 struct xfs_bitmap *agfl_extents, 582 xfs_agblock_t flcount) 583 { 584 struct xfs_mount *mp = sc->mp; 585 __be32 *agfl_bno; 586 struct xfs_bitmap_range *br; 587 struct xfs_bitmap_range *n; 588 struct xfs_agfl *agfl; 589 xfs_agblock_t agbno; 590 unsigned int fl_off; 591 592 ASSERT(flcount <= xfs_agfl_size(mp)); 593 594 /* 595 * Start rewriting the header by setting the bno[] array to 596 * NULLAGBLOCK, then setting AGFL header fields. 597 */ 598 agfl = XFS_BUF_TO_AGFL(agfl_bp); 599 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length)); 600 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC); 601 agfl->agfl_seqno = cpu_to_be32(sc->sa.agno); 602 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid); 603 604 /* 605 * Fill the AGFL with the remaining blocks. If agfl_extents has more 606 * blocks than fit in the AGFL, they will be freed in a subsequent 607 * step. 608 */ 609 fl_off = 0; 610 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, agfl_bp); 611 for_each_xfs_bitmap_extent(br, n, agfl_extents) { 612 agbno = XFS_FSB_TO_AGBNO(mp, br->start); 613 614 trace_xrep_agfl_insert(mp, sc->sa.agno, agbno, br->len); 615 616 while (br->len > 0 && fl_off < flcount) { 617 agfl_bno[fl_off] = cpu_to_be32(agbno); 618 fl_off++; 619 agbno++; 620 621 /* 622 * We've now used br->start by putting it in the AGFL, 623 * so bump br so that we don't reap the block later. 624 */ 625 br->start++; 626 br->len--; 627 } 628 629 if (br->len) 630 break; 631 list_del(&br->list); 632 kmem_free(br); 633 } 634 635 /* Write new AGFL to disk. */ 636 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF); 637 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1); 638 } 639 640 /* Repair the AGFL. */ 641 int 642 xrep_agfl( 643 struct xfs_scrub *sc) 644 { 645 struct xfs_bitmap agfl_extents; 646 struct xfs_mount *mp = sc->mp; 647 struct xfs_buf *agf_bp; 648 struct xfs_buf *agfl_bp; 649 xfs_agblock_t flcount; 650 int error; 651 652 /* We require the rmapbt to rebuild anything. */ 653 if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) 654 return -EOPNOTSUPP; 655 656 xchk_perag_get(sc->mp, &sc->sa); 657 xfs_bitmap_init(&agfl_extents); 658 659 /* 660 * Read the AGF so that we can query the rmapbt. We hope that there's 661 * nothing wrong with the AGF, but all the AG header repair functions 662 * have this chicken-and-egg problem. 663 */ 664 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp); 665 if (error) 666 return error; 667 if (!agf_bp) 668 return -ENOMEM; 669 670 /* 671 * Make sure we have the AGFL buffer, as scrub might have decided it 672 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED. 673 */ 674 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp, 675 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGFL_DADDR(mp)), 676 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL); 677 if (error) 678 return error; 679 agfl_bp->b_ops = &xfs_agfl_buf_ops; 680 681 /* Gather all the extents we're going to put on the new AGFL. */ 682 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount); 683 if (error) 684 goto err; 685 686 /* 687 * Update AGF and AGFL. We reset the global free block counter when 688 * we adjust the AGF flcount (which can fail) so avoid updating any 689 * buffers until we know that part works. 690 */ 691 xrep_agfl_update_agf(sc, agf_bp, flcount); 692 xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount); 693 694 /* 695 * Ok, the AGFL should be ready to go now. Roll the transaction to 696 * make the new AGFL permanent before we start using it to return 697 * freespace overflow to the freespace btrees. 698 */ 699 sc->sa.agf_bp = agf_bp; 700 sc->sa.agfl_bp = agfl_bp; 701 error = xrep_roll_ag_trans(sc); 702 if (error) 703 goto err; 704 705 /* Dump any AGFL overflow. */ 706 return xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG, 707 XFS_AG_RESV_AGFL); 708 err: 709 xfs_bitmap_destroy(&agfl_extents); 710 return error; 711 } 712 713 /* AGI */ 714 715 /* 716 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the 717 * XFS_BTNUM_ names here to avoid creating a sparse array. 718 */ 719 enum { 720 XREP_AGI_INOBT = 0, 721 XREP_AGI_FINOBT, 722 XREP_AGI_END, 723 XREP_AGI_MAX 724 }; 725 726 /* 727 * Given the inode btree roots described by *fab, find the roots, check them 728 * for sanity, and pass the root data back out via *fab. 729 */ 730 STATIC int 731 xrep_agi_find_btrees( 732 struct xfs_scrub *sc, 733 struct xrep_find_ag_btree *fab) 734 { 735 struct xfs_buf *agf_bp; 736 struct xfs_mount *mp = sc->mp; 737 int error; 738 739 /* Read the AGF. */ 740 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp); 741 if (error) 742 return error; 743 if (!agf_bp) 744 return -ENOMEM; 745 746 /* Find the btree roots. */ 747 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL); 748 if (error) 749 return error; 750 751 /* We must find the inobt root. */ 752 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT])) 753 return -EFSCORRUPTED; 754 755 /* We must find the finobt root if that feature is enabled. */ 756 if (xfs_sb_version_hasfinobt(&mp->m_sb) && 757 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT])) 758 return -EFSCORRUPTED; 759 760 return 0; 761 } 762 763 /* 764 * Reinitialize the AGI header, making an in-core copy of the old contents so 765 * that we know which in-core state needs to be reinitialized. 766 */ 767 STATIC void 768 xrep_agi_init_header( 769 struct xfs_scrub *sc, 770 struct xfs_buf *agi_bp, 771 struct xfs_agi *old_agi) 772 { 773 struct xfs_agi *agi = XFS_BUF_TO_AGI(agi_bp); 774 struct xfs_mount *mp = sc->mp; 775 776 memcpy(old_agi, agi, sizeof(*old_agi)); 777 memset(agi, 0, BBTOB(agi_bp->b_length)); 778 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC); 779 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION); 780 agi->agi_seqno = cpu_to_be32(sc->sa.agno); 781 agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno)); 782 agi->agi_newino = cpu_to_be32(NULLAGINO); 783 agi->agi_dirino = cpu_to_be32(NULLAGINO); 784 if (xfs_sb_version_hascrc(&mp->m_sb)) 785 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid); 786 787 /* We don't know how to fix the unlinked list yet. */ 788 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked, 789 sizeof(agi->agi_unlinked)); 790 791 /* Mark the incore AGF data stale until we're done fixing things. */ 792 ASSERT(sc->sa.pag->pagi_init); 793 sc->sa.pag->pagi_init = 0; 794 } 795 796 /* Set btree root information in an AGI. */ 797 STATIC void 798 xrep_agi_set_roots( 799 struct xfs_scrub *sc, 800 struct xfs_agi *agi, 801 struct xrep_find_ag_btree *fab) 802 { 803 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root); 804 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height); 805 806 if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) { 807 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root); 808 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height); 809 } 810 } 811 812 /* Update the AGI counters. */ 813 STATIC int 814 xrep_agi_calc_from_btrees( 815 struct xfs_scrub *sc, 816 struct xfs_buf *agi_bp) 817 { 818 struct xfs_btree_cur *cur; 819 struct xfs_agi *agi = XFS_BUF_TO_AGI(agi_bp); 820 struct xfs_mount *mp = sc->mp; 821 xfs_agino_t count; 822 xfs_agino_t freecount; 823 int error; 824 825 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno, 826 XFS_BTNUM_INO); 827 error = xfs_ialloc_count_inodes(cur, &count, &freecount); 828 if (error) 829 goto err; 830 xfs_btree_del_cursor(cur, error); 831 832 agi->agi_count = cpu_to_be32(count); 833 agi->agi_freecount = cpu_to_be32(freecount); 834 return 0; 835 err: 836 xfs_btree_del_cursor(cur, error); 837 return error; 838 } 839 840 /* Trigger reinitialization of the in-core data. */ 841 STATIC int 842 xrep_agi_commit_new( 843 struct xfs_scrub *sc, 844 struct xfs_buf *agi_bp) 845 { 846 struct xfs_perag *pag; 847 struct xfs_agi *agi = XFS_BUF_TO_AGI(agi_bp); 848 849 /* Trigger inode count recalculation */ 850 xfs_force_summary_recalc(sc->mp); 851 852 /* Write this to disk. */ 853 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF); 854 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1); 855 856 /* Now reinitialize the in-core counters if necessary. */ 857 pag = sc->sa.pag; 858 pag->pagi_count = be32_to_cpu(agi->agi_count); 859 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount); 860 pag->pagi_init = 1; 861 862 return 0; 863 } 864 865 /* Repair the AGI. */ 866 int 867 xrep_agi( 868 struct xfs_scrub *sc) 869 { 870 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = { 871 [XREP_AGI_INOBT] = { 872 .rmap_owner = XFS_RMAP_OWN_INOBT, 873 .buf_ops = &xfs_inobt_buf_ops, 874 }, 875 [XREP_AGI_FINOBT] = { 876 .rmap_owner = XFS_RMAP_OWN_INOBT, 877 .buf_ops = &xfs_finobt_buf_ops, 878 }, 879 [XREP_AGI_END] = { 880 .buf_ops = NULL 881 }, 882 }; 883 struct xfs_agi old_agi; 884 struct xfs_mount *mp = sc->mp; 885 struct xfs_buf *agi_bp; 886 struct xfs_agi *agi; 887 int error; 888 889 /* We require the rmapbt to rebuild anything. */ 890 if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) 891 return -EOPNOTSUPP; 892 893 xchk_perag_get(sc->mp, &sc->sa); 894 /* 895 * Make sure we have the AGI buffer, as scrub might have decided it 896 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED. 897 */ 898 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp, 899 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGI_DADDR(mp)), 900 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL); 901 if (error) 902 return error; 903 agi_bp->b_ops = &xfs_agi_buf_ops; 904 agi = XFS_BUF_TO_AGI(agi_bp); 905 906 /* Find the AGI btree roots. */ 907 error = xrep_agi_find_btrees(sc, fab); 908 if (error) 909 return error; 910 911 /* Start rewriting the header and implant the btrees we found. */ 912 xrep_agi_init_header(sc, agi_bp, &old_agi); 913 xrep_agi_set_roots(sc, agi, fab); 914 error = xrep_agi_calc_from_btrees(sc, agi_bp); 915 if (error) 916 goto out_revert; 917 918 /* Reinitialize in-core state. */ 919 return xrep_agi_commit_new(sc, agi_bp); 920 921 out_revert: 922 /* Mark the incore AGI state stale and revert the AGI. */ 923 sc->sa.pag->pagi_init = 0; 924 memcpy(agi, &old_agi, sizeof(old_agi)); 925 return error; 926 } 927