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_defer.h" 13 #include "xfs_btree.h" 14 #include "xfs_btree_staging.h" 15 #include "xfs_inode.h" 16 #include "xfs_bit.h" 17 #include "xfs_log_format.h" 18 #include "xfs_trans.h" 19 #include "xfs_sb.h" 20 #include "xfs_alloc.h" 21 #include "xfs_ialloc.h" 22 #include "xfs_rmap.h" 23 #include "xfs_rmap_btree.h" 24 #include "xfs_refcount.h" 25 #include "xfs_refcount_btree.h" 26 #include "xfs_error.h" 27 #include "xfs_ag.h" 28 #include "xfs_health.h" 29 #include "scrub/xfs_scrub.h" 30 #include "scrub/scrub.h" 31 #include "scrub/common.h" 32 #include "scrub/btree.h" 33 #include "scrub/trace.h" 34 #include "scrub/repair.h" 35 #include "scrub/bitmap.h" 36 #include "scrub/agb_bitmap.h" 37 #include "scrub/xfile.h" 38 #include "scrub/xfarray.h" 39 #include "scrub/newbt.h" 40 #include "scrub/reap.h" 41 #include "scrub/rcbag.h" 42 43 /* 44 * Rebuilding the Reference Count Btree 45 * ==================================== 46 * 47 * This algorithm is "borrowed" from xfs_repair. Imagine the rmap 48 * entries as rectangles representing extents of physical blocks, and 49 * that the rectangles can be laid down to allow them to overlap each 50 * other; then we know that we must emit a refcnt btree entry wherever 51 * the amount of overlap changes, i.e. the emission stimulus is 52 * level-triggered: 53 * 54 * - --- 55 * -- ----- ---- --- ------ 56 * -- ---- ----------- ---- --------- 57 * -------------------------------- ----------- 58 * ^ ^ ^^ ^^ ^ ^^ ^^^ ^^^^ ^ ^^ ^ ^ ^ 59 * 2 1 23 21 3 43 234 2123 1 01 2 3 0 60 * 61 * For our purposes, a rmap is a tuple (startblock, len, fileoff, owner). 62 * 63 * Note that in the actual refcnt btree we don't store the refcount < 2 64 * cases because the bnobt tells us which blocks are free; single-use 65 * blocks aren't recorded in the bnobt or the refcntbt. If the rmapbt 66 * supports storing multiple entries covering a given block we could 67 * theoretically dispense with the refcntbt and simply count rmaps, but 68 * that's inefficient in the (hot) write path, so we'll take the cost of 69 * the extra tree to save time. Also there's no guarantee that rmap 70 * will be enabled. 71 * 72 * Given an array of rmaps sorted by physical block number, a starting 73 * physical block (sp), a bag to hold rmaps that cover sp, and the next 74 * physical block where the level changes (np), we can reconstruct the 75 * refcount btree as follows: 76 * 77 * While there are still unprocessed rmaps in the array, 78 * - Set sp to the physical block (pblk) of the next unprocessed rmap. 79 * - Add to the bag all rmaps in the array where startblock == sp. 80 * - Set np to the physical block where the bag size will change. This 81 * is the minimum of (the pblk of the next unprocessed rmap) and 82 * (startblock + len of each rmap in the bag). 83 * - Record the bag size as old_bag_size. 84 * 85 * - While the bag isn't empty, 86 * - Remove from the bag all rmaps where startblock + len == np. 87 * - Add to the bag all rmaps in the array where startblock == np. 88 * - If the bag size isn't old_bag_size, store the refcount entry 89 * (sp, np - sp, bag_size) in the refcnt btree. 90 * - If the bag is empty, break out of the inner loop. 91 * - Set old_bag_size to the bag size 92 * - Set sp = np. 93 * - Set np to the physical block where the bag size will change. 94 * This is the minimum of (the pblk of the next unprocessed rmap) 95 * and (startblock + len of each rmap in the bag). 96 * 97 * Like all the other repairers, we make a list of all the refcount 98 * records we need, then reinitialize the refcount btree root and 99 * insert all the records. 100 */ 101 102 struct xrep_refc { 103 /* refcount extents */ 104 struct xfarray *refcount_records; 105 106 /* new refcountbt information */ 107 struct xrep_newbt new_btree; 108 109 /* old refcountbt blocks */ 110 struct xagb_bitmap old_refcountbt_blocks; 111 112 struct xfs_scrub *sc; 113 114 /* get_records()'s position in the refcount record array. */ 115 xfarray_idx_t array_cur; 116 117 /* # of refcountbt blocks */ 118 xfs_extlen_t btblocks; 119 }; 120 121 /* Set us up to repair refcount btrees. */ 122 int 123 xrep_setup_ag_refcountbt( 124 struct xfs_scrub *sc) 125 { 126 return xrep_setup_xfbtree(sc, "rmap record bag"); 127 } 128 129 /* Check for any obvious conflicts with this shared/CoW staging extent. */ 130 STATIC int 131 xrep_refc_check_ext( 132 struct xfs_scrub *sc, 133 const struct xfs_refcount_irec *rec) 134 { 135 enum xbtree_recpacking outcome; 136 int error; 137 138 if (xfs_refcount_check_irec(sc->sa.pag, rec) != NULL) 139 return -EFSCORRUPTED; 140 141 /* Make sure this isn't free space. */ 142 error = xfs_alloc_has_records(sc->sa.bno_cur, rec->rc_startblock, 143 rec->rc_blockcount, &outcome); 144 if (error) 145 return error; 146 if (outcome != XBTREE_RECPACKING_EMPTY) 147 return -EFSCORRUPTED; 148 149 /* Must not be an inode chunk. */ 150 error = xfs_ialloc_has_inodes_at_extent(sc->sa.ino_cur, 151 rec->rc_startblock, rec->rc_blockcount, &outcome); 152 if (error) 153 return error; 154 if (outcome != XBTREE_RECPACKING_EMPTY) 155 return -EFSCORRUPTED; 156 157 return 0; 158 } 159 160 /* Record a reference count extent. */ 161 STATIC int 162 xrep_refc_stash( 163 struct xrep_refc *rr, 164 enum xfs_refc_domain domain, 165 xfs_agblock_t agbno, 166 xfs_extlen_t len, 167 uint64_t refcount) 168 { 169 struct xfs_refcount_irec irec = { 170 .rc_startblock = agbno, 171 .rc_blockcount = len, 172 .rc_domain = domain, 173 }; 174 struct xfs_scrub *sc = rr->sc; 175 int error = 0; 176 177 if (xchk_should_terminate(sc, &error)) 178 return error; 179 180 irec.rc_refcount = min_t(uint64_t, XFS_REFC_REFCOUNT_MAX, refcount); 181 182 error = xrep_refc_check_ext(rr->sc, &irec); 183 if (error) 184 return error; 185 186 trace_xrep_refc_found(pag_group(sc->sa.pag), &irec); 187 188 return xfarray_append(rr->refcount_records, &irec); 189 } 190 191 /* Record a CoW staging extent. */ 192 STATIC int 193 xrep_refc_stash_cow( 194 struct xrep_refc *rr, 195 xfs_agblock_t agbno, 196 xfs_extlen_t len) 197 { 198 return xrep_refc_stash(rr, XFS_REFC_DOMAIN_COW, agbno, len, 1); 199 } 200 201 /* Decide if an rmap could describe a shared extent. */ 202 static inline bool 203 xrep_refc_rmap_shareable( 204 struct xfs_mount *mp, 205 const struct xfs_rmap_irec *rmap) 206 { 207 /* AG metadata are never sharable */ 208 if (XFS_RMAP_NON_INODE_OWNER(rmap->rm_owner)) 209 return false; 210 211 /* Metadata in files are never shareable */ 212 if (xfs_is_sb_inum(mp, rmap->rm_owner)) 213 return false; 214 215 /* Metadata and unwritten file blocks are not shareable. */ 216 if (rmap->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK | 217 XFS_RMAP_UNWRITTEN)) 218 return false; 219 220 return true; 221 } 222 223 /* 224 * Walk along the reverse mapping records until we find one that could describe 225 * a shared extent. 226 */ 227 STATIC int 228 xrep_refc_walk_rmaps( 229 struct xrep_refc *rr, 230 struct xfs_rmap_irec *rmap, 231 bool *have_rec) 232 { 233 struct xfs_btree_cur *cur = rr->sc->sa.rmap_cur; 234 struct xfs_mount *mp = cur->bc_mp; 235 int have_gt; 236 int error = 0; 237 238 *have_rec = false; 239 240 /* 241 * Loop through the remaining rmaps. Remember CoW staging 242 * extents and the refcountbt blocks from the old tree for later 243 * disposal. We can only share written data fork extents, so 244 * keep looping until we find an rmap for one. 245 */ 246 do { 247 if (xchk_should_terminate(rr->sc, &error)) 248 return error; 249 250 error = xfs_btree_increment(cur, 0, &have_gt); 251 if (error) 252 return error; 253 if (!have_gt) 254 return 0; 255 256 error = xfs_rmap_get_rec(cur, rmap, &have_gt); 257 if (error) 258 return error; 259 if (XFS_IS_CORRUPT(mp, !have_gt)) { 260 xfs_btree_mark_sick(cur); 261 return -EFSCORRUPTED; 262 } 263 264 if (rmap->rm_owner == XFS_RMAP_OWN_COW) { 265 error = xrep_refc_stash_cow(rr, rmap->rm_startblock, 266 rmap->rm_blockcount); 267 if (error) 268 return error; 269 } else if (rmap->rm_owner == XFS_RMAP_OWN_REFC) { 270 /* refcountbt block, dump it when we're done. */ 271 rr->btblocks += rmap->rm_blockcount; 272 error = xagb_bitmap_set(&rr->old_refcountbt_blocks, 273 rmap->rm_startblock, 274 rmap->rm_blockcount); 275 if (error) 276 return error; 277 } 278 } while (!xrep_refc_rmap_shareable(mp, rmap)); 279 280 *have_rec = true; 281 return 0; 282 } 283 284 static inline uint32_t 285 xrep_refc_encode_startblock( 286 const struct xfs_refcount_irec *irec) 287 { 288 uint32_t start; 289 290 start = irec->rc_startblock & ~XFS_REFC_COWFLAG; 291 if (irec->rc_domain == XFS_REFC_DOMAIN_COW) 292 start |= XFS_REFC_COWFLAG; 293 294 return start; 295 } 296 297 /* Sort in the same order as the ondisk records. */ 298 static int 299 xrep_refc_extent_cmp( 300 const void *a, 301 const void *b) 302 { 303 const struct xfs_refcount_irec *ap = a; 304 const struct xfs_refcount_irec *bp = b; 305 uint32_t sa, sb; 306 307 sa = xrep_refc_encode_startblock(ap); 308 sb = xrep_refc_encode_startblock(bp); 309 310 if (sa > sb) 311 return 1; 312 if (sa < sb) 313 return -1; 314 return 0; 315 } 316 317 /* 318 * Sort the refcount extents by startblock or else the btree records will be in 319 * the wrong order. Make sure the records do not overlap in physical space. 320 */ 321 STATIC int 322 xrep_refc_sort_records( 323 struct xrep_refc *rr) 324 { 325 struct xfs_refcount_irec irec; 326 xfarray_idx_t cur; 327 enum xfs_refc_domain dom = XFS_REFC_DOMAIN_SHARED; 328 xfs_agblock_t next_agbno = 0; 329 int error; 330 331 error = xfarray_sort(rr->refcount_records, xrep_refc_extent_cmp, 332 XFARRAY_SORT_KILLABLE); 333 if (error) 334 return error; 335 336 foreach_xfarray_idx(rr->refcount_records, cur) { 337 if (xchk_should_terminate(rr->sc, &error)) 338 return error; 339 340 error = xfarray_load(rr->refcount_records, cur, &irec); 341 if (error) 342 return error; 343 344 if (dom == XFS_REFC_DOMAIN_SHARED && 345 irec.rc_domain == XFS_REFC_DOMAIN_COW) { 346 dom = irec.rc_domain; 347 next_agbno = 0; 348 } 349 350 if (dom != irec.rc_domain) 351 return -EFSCORRUPTED; 352 if (irec.rc_startblock < next_agbno) 353 return -EFSCORRUPTED; 354 355 next_agbno = irec.rc_startblock + irec.rc_blockcount; 356 } 357 358 return error; 359 } 360 361 /* 362 * Walk forward through the rmap btree to collect all rmaps starting at 363 * @bno in @rmap_bag. These represent the file(s) that share ownership of 364 * the current block. Upon return, the rmap cursor points to the last record 365 * satisfying the startblock constraint. 366 */ 367 static int 368 xrep_refc_push_rmaps_at( 369 struct xrep_refc *rr, 370 struct rcbag *rcstack, 371 xfs_agblock_t bno, 372 struct xfs_rmap_irec *rmap, 373 bool *have) 374 { 375 struct xfs_scrub *sc = rr->sc; 376 int have_gt; 377 int error; 378 379 while (*have && rmap->rm_startblock == bno) { 380 error = rcbag_add(rcstack, rr->sc->tp, rmap); 381 if (error) 382 return error; 383 384 error = xrep_refc_walk_rmaps(rr, rmap, have); 385 if (error) 386 return error; 387 } 388 389 error = xfs_btree_decrement(sc->sa.rmap_cur, 0, &have_gt); 390 if (error) 391 return error; 392 if (XFS_IS_CORRUPT(sc->mp, !have_gt)) { 393 xfs_btree_mark_sick(sc->sa.rmap_cur); 394 return -EFSCORRUPTED; 395 } 396 397 return 0; 398 } 399 400 /* Iterate all the rmap records to generate reference count data. */ 401 STATIC int 402 xrep_refc_find_refcounts( 403 struct xrep_refc *rr) 404 { 405 struct xfs_scrub *sc = rr->sc; 406 struct rcbag *rcstack; 407 uint64_t old_stack_height; 408 xfs_agblock_t sbno; 409 xfs_agblock_t cbno; 410 xfs_agblock_t nbno; 411 bool have; 412 int error; 413 414 xrep_ag_btcur_init(sc, &sc->sa); 415 416 /* 417 * Set up a bag to store all the rmap records that we're tracking to 418 * generate a reference count record. If the size of the bag exceeds 419 * XFS_REFC_REFCOUNT_MAX, we clamp rc_refcount. 420 */ 421 error = rcbag_init(sc->mp, sc->xmbtp, &rcstack); 422 if (error) 423 goto out_cur; 424 425 /* Start the rmapbt cursor to the left of all records. */ 426 error = xfs_btree_goto_left_edge(sc->sa.rmap_cur); 427 if (error) 428 goto out_bag; 429 430 /* Process reverse mappings into refcount data. */ 431 while (xfs_btree_has_more_records(sc->sa.rmap_cur)) { 432 struct xfs_rmap_irec rmap; 433 434 /* Push all rmaps with pblk == sbno onto the stack */ 435 error = xrep_refc_walk_rmaps(rr, &rmap, &have); 436 if (error) 437 goto out_bag; 438 if (!have) 439 break; 440 sbno = cbno = rmap.rm_startblock; 441 error = xrep_refc_push_rmaps_at(rr, rcstack, sbno, &rmap, 442 &have); 443 if (error) 444 goto out_bag; 445 446 /* Set nbno to the bno of the next refcount change */ 447 error = rcbag_next_edge(rcstack, sc->tp, &rmap, have, &nbno); 448 if (error) 449 goto out_bag; 450 451 ASSERT(nbno > sbno); 452 old_stack_height = rcbag_count(rcstack); 453 454 /* While stack isn't empty... */ 455 while (rcbag_count(rcstack) > 0) { 456 /* Pop all rmaps that end at nbno */ 457 error = rcbag_remove_ending_at(rcstack, sc->tp, nbno); 458 if (error) 459 goto out_bag; 460 461 /* Push array items that start at nbno */ 462 error = xrep_refc_walk_rmaps(rr, &rmap, &have); 463 if (error) 464 goto out_bag; 465 if (have) { 466 error = xrep_refc_push_rmaps_at(rr, rcstack, 467 nbno, &rmap, &have); 468 if (error) 469 goto out_bag; 470 } 471 472 /* Emit refcount if necessary */ 473 ASSERT(nbno > cbno); 474 if (rcbag_count(rcstack) != old_stack_height) { 475 if (old_stack_height > 1) { 476 error = xrep_refc_stash(rr, 477 XFS_REFC_DOMAIN_SHARED, 478 cbno, nbno - cbno, 479 old_stack_height); 480 if (error) 481 goto out_bag; 482 } 483 cbno = nbno; 484 } 485 486 /* Stack empty, go find the next rmap */ 487 if (rcbag_count(rcstack) == 0) 488 break; 489 old_stack_height = rcbag_count(rcstack); 490 sbno = nbno; 491 492 /* Set nbno to the bno of the next refcount change */ 493 error = rcbag_next_edge(rcstack, sc->tp, &rmap, have, 494 &nbno); 495 if (error) 496 goto out_bag; 497 498 ASSERT(nbno > sbno); 499 } 500 } 501 502 ASSERT(rcbag_count(rcstack) == 0); 503 out_bag: 504 rcbag_free(&rcstack); 505 out_cur: 506 xchk_ag_btcur_free(&sc->sa); 507 return error; 508 } 509 510 /* Retrieve refcountbt data for bulk load. */ 511 STATIC int 512 xrep_refc_get_records( 513 struct xfs_btree_cur *cur, 514 unsigned int idx, 515 struct xfs_btree_block *block, 516 unsigned int nr_wanted, 517 void *priv) 518 { 519 struct xfs_refcount_irec *irec = &cur->bc_rec.rc; 520 struct xrep_refc *rr = priv; 521 union xfs_btree_rec *block_rec; 522 unsigned int loaded; 523 int error; 524 525 for (loaded = 0; loaded < nr_wanted; loaded++, idx++) { 526 error = xfarray_load(rr->refcount_records, rr->array_cur++, 527 irec); 528 if (error) 529 return error; 530 531 block_rec = xfs_btree_rec_addr(cur, idx, block); 532 cur->bc_ops->init_rec_from_cur(cur, block_rec); 533 } 534 535 return loaded; 536 } 537 538 /* Feed one of the new btree blocks to the bulk loader. */ 539 STATIC int 540 xrep_refc_claim_block( 541 struct xfs_btree_cur *cur, 542 union xfs_btree_ptr *ptr, 543 void *priv) 544 { 545 struct xrep_refc *rr = priv; 546 547 return xrep_newbt_claim_block(cur, &rr->new_btree, ptr); 548 } 549 550 /* Update the AGF counters. */ 551 STATIC int 552 xrep_refc_reset_counters( 553 struct xrep_refc *rr) 554 { 555 struct xfs_scrub *sc = rr->sc; 556 struct xfs_perag *pag = sc->sa.pag; 557 558 /* 559 * After we commit the new btree to disk, it is possible that the 560 * process to reap the old btree blocks will race with the AIL trying 561 * to checkpoint the old btree blocks into the filesystem. If the new 562 * tree is shorter than the old one, the refcountbt write verifier will 563 * fail and the AIL will shut down the filesystem. 564 * 565 * To avoid this, save the old incore btree height values as the alt 566 * height values before re-initializing the perag info from the updated 567 * AGF to capture all the new values. 568 */ 569 pag->pagf_repair_refcount_level = pag->pagf_refcount_level; 570 571 /* Reinitialize with the values we just logged. */ 572 return xrep_reinit_pagf(sc); 573 } 574 575 /* 576 * Use the collected refcount information to stage a new refcount btree. If 577 * this is successful we'll return with the new btree root information logged 578 * to the repair transaction but not yet committed. 579 */ 580 STATIC int 581 xrep_refc_build_new_tree( 582 struct xrep_refc *rr) 583 { 584 struct xfs_scrub *sc = rr->sc; 585 struct xfs_btree_cur *refc_cur; 586 struct xfs_perag *pag = sc->sa.pag; 587 int error; 588 589 error = xrep_refc_sort_records(rr); 590 if (error) 591 return error; 592 593 /* 594 * Prepare to construct the new btree by reserving disk space for the 595 * new btree and setting up all the accounting information we'll need 596 * to root the new btree while it's under construction and before we 597 * attach it to the AG header. 598 */ 599 xrep_newbt_init_ag(&rr->new_btree, sc, &XFS_RMAP_OINFO_REFC, 600 xfs_agbno_to_fsb(pag, xfs_refc_block(sc->mp)), 601 XFS_AG_RESV_METADATA); 602 rr->new_btree.bload.get_records = xrep_refc_get_records; 603 rr->new_btree.bload.claim_block = xrep_refc_claim_block; 604 605 /* Compute how many blocks we'll need. */ 606 refc_cur = xfs_refcountbt_init_cursor(sc->mp, NULL, NULL, pag); 607 xfs_btree_stage_afakeroot(refc_cur, &rr->new_btree.afake); 608 error = xfs_btree_bload_compute_geometry(refc_cur, 609 &rr->new_btree.bload, 610 xfarray_length(rr->refcount_records)); 611 if (error) 612 goto err_cur; 613 614 /* Last chance to abort before we start committing fixes. */ 615 if (xchk_should_terminate(sc, &error)) 616 goto err_cur; 617 618 /* Reserve the space we'll need for the new btree. */ 619 error = xrep_newbt_alloc_blocks(&rr->new_btree, 620 rr->new_btree.bload.nr_blocks); 621 if (error) 622 goto err_cur; 623 624 /* 625 * Due to btree slack factors, it's possible for a new btree to be one 626 * level taller than the old btree. Update the incore btree height so 627 * that we don't trip the verifiers when writing the new btree blocks 628 * to disk. 629 */ 630 pag->pagf_repair_refcount_level = rr->new_btree.bload.btree_height; 631 632 /* Add all observed refcount records. */ 633 rr->array_cur = XFARRAY_CURSOR_INIT; 634 error = xfs_btree_bload(refc_cur, &rr->new_btree.bload, rr); 635 if (error) 636 goto err_level; 637 638 /* 639 * Install the new btree in the AG header. After this point the old 640 * btree is no longer accessible and the new tree is live. 641 */ 642 xfs_refcountbt_commit_staged_btree(refc_cur, sc->tp, sc->sa.agf_bp); 643 xfs_btree_del_cursor(refc_cur, 0); 644 645 /* Reset the AGF counters now that we've changed the btree shape. */ 646 error = xrep_refc_reset_counters(rr); 647 if (error) 648 goto err_newbt; 649 650 /* Dispose of any unused blocks and the accounting information. */ 651 error = xrep_newbt_commit(&rr->new_btree); 652 if (error) 653 return error; 654 655 return xrep_roll_ag_trans(sc); 656 657 err_level: 658 pag->pagf_repair_refcount_level = 0; 659 err_cur: 660 xfs_btree_del_cursor(refc_cur, error); 661 err_newbt: 662 xrep_newbt_cancel(&rr->new_btree); 663 return error; 664 } 665 666 /* 667 * Now that we've logged the roots of the new btrees, invalidate all of the 668 * old blocks and free them. 669 */ 670 STATIC int 671 xrep_refc_remove_old_tree( 672 struct xrep_refc *rr) 673 { 674 struct xfs_scrub *sc = rr->sc; 675 struct xfs_perag *pag = sc->sa.pag; 676 int error; 677 678 /* Free the old refcountbt blocks if they're not in use. */ 679 error = xrep_reap_agblocks(sc, &rr->old_refcountbt_blocks, 680 &XFS_RMAP_OINFO_REFC, XFS_AG_RESV_METADATA); 681 if (error) 682 return error; 683 684 /* 685 * Now that we've zapped all the old refcountbt blocks we can turn off 686 * the alternate height mechanism and reset the per-AG space 687 * reservations. 688 */ 689 pag->pagf_repair_refcount_level = 0; 690 sc->flags |= XREP_RESET_PERAG_RESV; 691 return 0; 692 } 693 694 /* Rebuild the refcount btree. */ 695 int 696 xrep_refcountbt( 697 struct xfs_scrub *sc) 698 { 699 struct xrep_refc *rr; 700 struct xfs_mount *mp = sc->mp; 701 int error; 702 703 /* We require the rmapbt to rebuild anything. */ 704 if (!xfs_has_rmapbt(mp)) 705 return -EOPNOTSUPP; 706 707 rr = kzalloc(sizeof(struct xrep_refc), XCHK_GFP_FLAGS); 708 if (!rr) 709 return -ENOMEM; 710 rr->sc = sc; 711 712 /* Set up enough storage to handle one refcount record per block. */ 713 error = xfarray_create("reference count records", mp->m_sb.sb_agblocks, 714 sizeof(struct xfs_refcount_irec), 715 &rr->refcount_records); 716 if (error) 717 goto out_rr; 718 719 /* Collect all reference counts. */ 720 xagb_bitmap_init(&rr->old_refcountbt_blocks); 721 error = xrep_refc_find_refcounts(rr); 722 if (error) 723 goto out_bitmap; 724 725 /* Rebuild the refcount information. */ 726 error = xrep_refc_build_new_tree(rr); 727 if (error) 728 goto out_bitmap; 729 730 /* Kill the old tree. */ 731 error = xrep_refc_remove_old_tree(rr); 732 if (error) 733 goto out_bitmap; 734 735 out_bitmap: 736 xagb_bitmap_destroy(&rr->old_refcountbt_blocks); 737 xfarray_destroy(rr->refcount_records); 738 out_rr: 739 kfree(rr); 740 return error; 741 } 742