1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2018-2024 Oracle. All Rights Reserved.
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_buf_mem.h"
16 #include "xfs_btree_mem.h"
18 #include "xfs_log_format.h"
19 #include "xfs_trans.h"
21 #include "xfs_alloc.h"
22 #include "xfs_alloc_btree.h"
23 #include "xfs_ialloc.h"
24 #include "xfs_ialloc_btree.h"
26 #include "xfs_rmap_btree.h"
27 #include "xfs_inode.h"
28 #include "xfs_icache.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_refcount.h"
32 #include "xfs_refcount_btree.h"
34 #include "scrub/xfs_scrub.h"
35 #include "scrub/scrub.h"
36 #include "scrub/common.h"
37 #include "scrub/btree.h"
38 #include "scrub/trace.h"
39 #include "scrub/repair.h"
40 #include "scrub/bitmap.h"
41 #include "scrub/agb_bitmap.h"
42 #include "scrub/xfile.h"
43 #include "scrub/xfarray.h"
44 #include "scrub/iscan.h"
45 #include "scrub/newbt.h"
46 #include "scrub/reap.h"
49 * Reverse Mapping Btree Repair
50 * ============================
52 * This is the most involved of all the AG space btree rebuilds. Everywhere
53 * else in XFS we lock inodes and then AG data structures, but generating the
54 * list of rmap records requires that we be able to scan both block mapping
55 * btrees of every inode in the filesystem to see if it owns any extents in
56 * this AG. We can't tolerate any inode updates while we do this, so we
57 * freeze the filesystem to lock everyone else out, and grant ourselves
58 * special privileges to run transactions with regular background reclamation
61 * We also have to be very careful not to allow inode reclaim to start a
62 * transaction because all transactions (other than our own) will block.
63 * Deferred inode inactivation helps us out there.
65 * I) Reverse mappings for all non-space metadata and file data are collected
66 * according to the following algorithm:
68 * 1. For each fork of each inode:
69 * 1.1. Create a bitmap BMBIT to track bmbt blocks if necessary.
70 * 1.2. If the incore extent map isn't loaded, walk the bmbt to accumulate
71 * bmaps into rmap records (see 1.1.4). Set bits in BMBIT for each btree
73 * 1.3. If the incore extent map is loaded but the fork is in btree format,
74 * just visit the bmbt blocks to set the corresponding BMBIT areas.
75 * 1.4. From the incore extent map, accumulate each bmap that falls into our
76 * target AG. Remember, multiple bmap records can map to a single rmap
77 * record, so we cannot simply emit rmap records 1:1.
78 * 1.5. Emit rmap records for each extent in BMBIT and free it.
79 * 2. Create bitmaps INOBIT and ICHUNKBIT.
80 * 3. For each record in the inobt, set the corresponding areas in ICHUNKBIT,
81 * and set bits in INOBIT for each btree block. If the inobt has no records
82 * at all, we must be careful to record its root in INOBIT.
83 * 4. For each block in the finobt, set the corresponding INOBIT area.
84 * 5. Emit rmap records for each extent in INOBIT and ICHUNKBIT and free them.
85 * 6. Create bitmaps REFCBIT and COWBIT.
86 * 7. For each CoW staging extent in the refcountbt, set the corresponding
88 * 8. For each block in the refcountbt, set the corresponding REFCBIT area.
89 * 9. Emit rmap records for each extent in REFCBIT and COWBIT and free them.
90 * A. Emit rmap for the AG headers.
91 * B. Emit rmap for the log, if there is one.
93 * II) The rmapbt shape and space metadata rmaps are computed as follows:
95 * 1. Count the rmaps collected in the previous step. (= NR)
96 * 2. Estimate the number of rmapbt blocks needed to store NR records. (= RMB)
97 * 3. Reserve RMB blocks through the newbt using the allocator in normap mode.
98 * 4. Create bitmap AGBIT.
99 * 5. For each reservation in the newbt, set the corresponding areas in AGBIT.
100 * 6. For each block in the AGFL, bnobt, and cntbt, set the bits in AGBIT.
101 * 7. Count the extents in AGBIT. (= AGNR)
102 * 8. Estimate the number of rmapbt blocks needed for NR + AGNR rmaps. (= RMB')
103 * 9. If RMB' >= RMB, reserve RMB' - RMB more newbt blocks, set RMB = RMB',
104 * and clear AGBIT. Go to step 5.
105 * A. Emit rmaps for each extent in AGBIT.
107 * III) The rmapbt is constructed and set in place as follows:
109 * 1. Sort the rmap records.
110 * 2. Bulk load the rmaps.
112 * IV) Reap the old btree blocks.
114 * 1. Create a bitmap OLDRMBIT.
115 * 2. For each gap in the new rmapbt, set the corresponding areas of OLDRMBIT.
116 * 3. For each extent in the bnobt, clear the corresponding parts of OLDRMBIT.
117 * 4. Reap the extents corresponding to the set areas in OLDRMBIT. These are
118 * the parts of the AG that the rmap didn't find during its scan of the
119 * primary metadata and aren't known to be in the free space, which implies
120 * that they were the old rmapbt blocks.
123 * We use the 'xrep_rmap' prefix for all the rmap functions.
126 /* Context for collecting rmaps */
128 /* new rmapbt information */
129 struct xrep_newbt new_btree;
131 /* lock for the xfbtree and xfile */
134 /* rmap records generated from primary metadata */
135 struct xfbtree rmap_btree;
137 struct xfs_scrub *sc;
139 /* in-memory btree cursor for the xfs_btree_bload iteration */
140 struct xfs_btree_cur *mcur;
142 /* Hooks into rmap update code. */
143 struct xfs_rmap_hook rhook;
145 /* inode scan cursor */
146 struct xchk_iscan iscan;
148 /* Number of non-freespace records found. */
149 unsigned long long nr_records;
151 /* bnobt/cntbt contribution to btreeblks */
152 xfs_agblock_t freesp_btblocks;
154 /* old agf_rmap_blocks counter */
155 unsigned int old_rmapbt_fsbcount;
158 /* Set us up to repair reverse mapping btrees. */
160 xrep_setup_ag_rmapbt(
161 struct xfs_scrub *sc)
163 struct xrep_rmap *rr;
167 xchk_fsgates_enable(sc, XCHK_FSGATES_RMAP);
169 descr = xchk_xfile_ag_descr(sc, "reverse mapping records");
170 error = xrep_setup_xfbtree(sc, descr);
175 rr = kzalloc(sizeof(struct xrep_rmap), XCHK_GFP_FLAGS);
184 /* Make sure there's nothing funny about this mapping. */
186 xrep_rmap_check_mapping(
187 struct xfs_scrub *sc,
188 const struct xfs_rmap_irec *rec)
190 enum xbtree_recpacking outcome;
193 if (xfs_rmap_check_irec(sc->sa.pag, rec) != NULL)
194 return -EFSCORRUPTED;
196 /* Make sure this isn't free space. */
197 error = xfs_alloc_has_records(sc->sa.bno_cur, rec->rm_startblock,
198 rec->rm_blockcount, &outcome);
201 if (outcome != XBTREE_RECPACKING_EMPTY)
202 return -EFSCORRUPTED;
207 /* Store a reverse-mapping record. */
210 struct xrep_rmap *rr,
211 xfs_agblock_t startblock,
212 xfs_extlen_t blockcount,
217 struct xfs_rmap_irec rmap = {
218 .rm_startblock = startblock,
219 .rm_blockcount = blockcount,
224 struct xfs_scrub *sc = rr->sc;
225 struct xfs_btree_cur *mcur;
228 if (xchk_should_terminate(sc, &error))
231 if (xchk_iscan_aborted(&rr->iscan))
232 return -EFSCORRUPTED;
234 trace_xrep_rmap_found(sc->mp, sc->sa.pag->pag_agno, &rmap);
236 mutex_lock(&rr->lock);
237 mcur = xfs_rmapbt_mem_cursor(sc->sa.pag, sc->tp, &rr->rmap_btree);
238 error = xfs_rmap_map_raw(mcur, &rmap);
239 xfs_btree_del_cursor(mcur, error);
243 error = xfbtree_trans_commit(&rr->rmap_btree, sc->tp);
247 mutex_unlock(&rr->lock);
251 xfbtree_trans_cancel(&rr->rmap_btree, sc->tp);
253 xchk_iscan_abort(&rr->iscan);
254 mutex_unlock(&rr->lock);
258 struct xrep_rmap_stash_run {
259 struct xrep_rmap *rr;
261 unsigned int rmap_flags;
270 struct xrep_rmap_stash_run *rsr = priv;
271 struct xrep_rmap *rr = rsr->rr;
273 return xrep_rmap_stash(rr, start, len, rsr->owner, 0, rsr->rmap_flags);
277 * Emit rmaps for every extent of bits set in the bitmap. Caller must ensure
278 * that the ranges are in units of FS blocks.
281 xrep_rmap_stash_bitmap(
282 struct xrep_rmap *rr,
283 struct xagb_bitmap *bitmap,
284 const struct xfs_owner_info *oinfo)
286 struct xrep_rmap_stash_run rsr = {
288 .owner = oinfo->oi_owner,
292 if (oinfo->oi_flags & XFS_OWNER_INFO_ATTR_FORK)
293 rsr.rmap_flags |= XFS_RMAP_ATTR_FORK;
294 if (oinfo->oi_flags & XFS_OWNER_INFO_BMBT_BLOCK)
295 rsr.rmap_flags |= XFS_RMAP_BMBT_BLOCK;
297 return xagb_bitmap_walk(bitmap, xrep_rmap_stash_run, &rsr);
300 /* Section (I): Finding all file and bmbt extents. */
302 /* Context for accumulating rmaps for an inode fork. */
303 struct xrep_rmap_ifork {
305 * Accumulate rmap data here to turn multiple adjacent bmaps into a
308 struct xfs_rmap_irec accum;
310 /* Bitmap of bmbt blocks in this AG. */
311 struct xagb_bitmap bmbt_blocks;
313 struct xrep_rmap *rr;
315 /* Which inode fork? */
319 /* Stash an rmap that we accumulated while walking an inode fork. */
321 xrep_rmap_stash_accumulated(
322 struct xrep_rmap_ifork *rf)
324 if (rf->accum.rm_blockcount == 0)
327 return xrep_rmap_stash(rf->rr, rf->accum.rm_startblock,
328 rf->accum.rm_blockcount, rf->accum.rm_owner,
329 rf->accum.rm_offset, rf->accum.rm_flags);
332 /* Accumulate a bmbt record. */
334 xrep_rmap_visit_bmbt(
335 struct xfs_btree_cur *cur,
336 struct xfs_bmbt_irec *rec,
339 struct xrep_rmap_ifork *rf = priv;
340 struct xfs_mount *mp = rf->rr->sc->mp;
341 struct xfs_rmap_irec *accum = &rf->accum;
343 unsigned int rmap_flags = 0;
346 if (XFS_FSB_TO_AGNO(mp, rec->br_startblock) !=
347 rf->rr->sc->sa.pag->pag_agno)
350 agbno = XFS_FSB_TO_AGBNO(mp, rec->br_startblock);
351 if (rf->whichfork == XFS_ATTR_FORK)
352 rmap_flags |= XFS_RMAP_ATTR_FORK;
353 if (rec->br_state == XFS_EXT_UNWRITTEN)
354 rmap_flags |= XFS_RMAP_UNWRITTEN;
356 /* If this bmap is adjacent to the previous one, just add it. */
357 if (accum->rm_blockcount > 0 &&
358 rec->br_startoff == accum->rm_offset + accum->rm_blockcount &&
359 agbno == accum->rm_startblock + accum->rm_blockcount &&
360 rmap_flags == accum->rm_flags) {
361 accum->rm_blockcount += rec->br_blockcount;
365 /* Otherwise stash the old rmap and start accumulating a new one. */
366 error = xrep_rmap_stash_accumulated(rf);
370 accum->rm_startblock = agbno;
371 accum->rm_blockcount = rec->br_blockcount;
372 accum->rm_offset = rec->br_startoff;
373 accum->rm_flags = rmap_flags;
377 /* Add a btree block to the bitmap. */
379 xrep_rmap_visit_iroot_btree_block(
380 struct xfs_btree_cur *cur,
384 struct xrep_rmap_ifork *rf = priv;
389 xfs_btree_get_block(cur, level, &bp);
393 fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, xfs_buf_daddr(bp));
394 if (XFS_FSB_TO_AGNO(cur->bc_mp, fsbno) != rf->rr->sc->sa.pag->pag_agno)
397 agbno = XFS_FSB_TO_AGBNO(cur->bc_mp, fsbno);
398 return xagb_bitmap_set(&rf->bmbt_blocks, agbno, 1);
402 * Iterate a metadata btree rooted in an inode to collect rmap records for
403 * anything in this fork that matches the AG.
406 xrep_rmap_scan_iroot_btree(
407 struct xrep_rmap_ifork *rf,
408 struct xfs_btree_cur *cur)
410 struct xfs_owner_info oinfo;
411 struct xrep_rmap *rr = rf->rr;
414 xagb_bitmap_init(&rf->bmbt_blocks);
416 /* Record all the blocks in the btree itself. */
417 error = xfs_btree_visit_blocks(cur, xrep_rmap_visit_iroot_btree_block,
418 XFS_BTREE_VISIT_ALL, rf);
422 /* Emit rmaps for the btree blocks. */
423 xfs_rmap_ino_bmbt_owner(&oinfo, rf->accum.rm_owner, rf->whichfork);
424 error = xrep_rmap_stash_bitmap(rr, &rf->bmbt_blocks, &oinfo);
428 /* Stash any remaining accumulated rmaps. */
429 error = xrep_rmap_stash_accumulated(rf);
431 xagb_bitmap_destroy(&rf->bmbt_blocks);
437 struct xfs_inode *ip,
440 return XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK;
444 * Iterate the block mapping btree to collect rmap records for anything in this
445 * fork that matches the AG. Sets @mappings_done to true if we've scanned the
446 * block mappings in this fork.
450 struct xrep_rmap_ifork *rf,
451 struct xfs_inode *ip,
454 struct xrep_rmap *rr = rf->rr;
455 struct xfs_btree_cur *cur;
456 struct xfs_ifork *ifp;
459 *mappings_done = false;
460 ifp = xfs_ifork_ptr(ip, rf->whichfork);
461 cur = xfs_bmbt_init_cursor(rr->sc->mp, rr->sc->tp, ip, rf->whichfork);
463 if (!xfs_ifork_is_realtime(ip, rf->whichfork) &&
464 xfs_need_iread_extents(ifp)) {
466 * If the incore extent cache isn't loaded, scan the bmbt for
467 * mapping records. This avoids loading the incore extent
468 * tree, which will increase memory pressure at a time when
469 * we're trying to run as quickly as we possibly can. Ignore
472 error = xfs_bmap_query_all(cur, xrep_rmap_visit_bmbt, rf);
476 *mappings_done = true;
479 /* Scan for the bmbt blocks, which always live on the data device. */
480 error = xrep_rmap_scan_iroot_btree(rf, cur);
482 xfs_btree_del_cursor(cur, error);
487 * Iterate the in-core extent cache to collect rmap records for anything in
488 * this fork that matches the AG.
492 struct xrep_rmap_ifork *rf,
493 struct xfs_ifork *ifp)
495 struct xfs_bmbt_irec rec;
496 struct xfs_iext_cursor icur;
499 for_each_xfs_iext(ifp, &icur, &rec) {
500 if (isnullstartblock(rec.br_startblock))
502 error = xrep_rmap_visit_bmbt(NULL, &rec, rf);
507 return xrep_rmap_stash_accumulated(rf);
510 /* Find all the extents from a given AG in an inode fork. */
512 xrep_rmap_scan_ifork(
513 struct xrep_rmap *rr,
514 struct xfs_inode *ip,
517 struct xrep_rmap_ifork rf = {
518 .accum = { .rm_owner = ip->i_ino, },
520 .whichfork = whichfork,
522 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
528 if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
532 * Scan the bmap btree for data device mappings. This includes
533 * the btree blocks themselves, even if this is a realtime
536 error = xrep_rmap_scan_bmbt(&rf, ip, &mappings_done);
537 if (error || mappings_done)
539 } else if (ifp->if_format != XFS_DINODE_FMT_EXTENTS) {
543 /* Scan incore extent cache if this isn't a realtime file. */
544 if (xfs_ifork_is_realtime(ip, whichfork))
547 return xrep_rmap_scan_iext(&rf, ifp);
551 * Take ILOCK on a file that we want to scan.
553 * Select ILOCK_EXCL if the file has an unloaded data bmbt or has an unloaded
554 * attr bmbt. Otherwise, take ILOCK_SHARED.
556 static inline unsigned int
557 xrep_rmap_scan_ilock(
558 struct xfs_inode *ip)
560 uint lock_mode = XFS_ILOCK_SHARED;
562 if (xfs_need_iread_extents(&ip->i_df)) {
563 lock_mode = XFS_ILOCK_EXCL;
567 if (xfs_inode_has_attr_fork(ip) && xfs_need_iread_extents(&ip->i_af))
568 lock_mode = XFS_ILOCK_EXCL;
571 xfs_ilock(ip, lock_mode);
575 /* Record reverse mappings for a file. */
577 xrep_rmap_scan_inode(
578 struct xrep_rmap *rr,
579 struct xfs_inode *ip)
581 unsigned int lock_mode = 0;
585 * Directory updates (create/link/unlink/rename) drop the directory's
586 * ILOCK before finishing any rmapbt updates associated with directory
587 * shape changes. For this scan to coordinate correctly with the live
588 * update hook, we must take the only lock (i_rwsem) that is held all
589 * the way to dir op completion. This will get fixed by the parent
592 if (S_ISDIR(VFS_I(ip)->i_mode)) {
593 lock_mode = XFS_IOLOCK_SHARED;
594 xfs_ilock(ip, lock_mode);
596 lock_mode |= xrep_rmap_scan_ilock(ip);
598 /* Check the data fork. */
599 error = xrep_rmap_scan_ifork(rr, ip, XFS_DATA_FORK);
603 /* Check the attr fork. */
604 error = xrep_rmap_scan_ifork(rr, ip, XFS_ATTR_FORK);
608 /* COW fork extents are "owned" by the refcount btree. */
610 xchk_iscan_mark_visited(&rr->iscan, ip);
612 xfs_iunlock(ip, lock_mode);
616 /* Section (I): Find all AG metadata extents except for free space metadata. */
618 struct xrep_rmap_inodes {
619 struct xrep_rmap *rr;
620 struct xagb_bitmap inobt_blocks; /* INOBIT */
621 struct xagb_bitmap ichunk_blocks; /* ICHUNKBIT */
624 /* Record inode btree rmaps. */
626 xrep_rmap_walk_inobt(
627 struct xfs_btree_cur *cur,
628 const union xfs_btree_rec *rec,
631 struct xfs_inobt_rec_incore irec;
632 struct xrep_rmap_inodes *ri = priv;
633 struct xfs_mount *mp = cur->bc_mp;
637 xfs_agino_t iperhole;
641 /* Record the inobt blocks. */
642 error = xagb_bitmap_set_btcur_path(&ri->inobt_blocks, cur);
646 xfs_inobt_btrec_to_irec(mp, rec, &irec);
647 if (xfs_inobt_check_irec(cur->bc_ag.pag, &irec) != NULL)
648 return -EFSCORRUPTED;
650 agino = irec.ir_startino;
652 /* Record a non-sparse inode chunk. */
653 if (!xfs_inobt_issparse(irec.ir_holemask)) {
654 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
655 aglen = max_t(xfs_extlen_t, 1,
656 XFS_INODES_PER_CHUNK / mp->m_sb.sb_inopblock);
658 return xagb_bitmap_set(&ri->ichunk_blocks, agbno, aglen);
661 /* Iterate each chunk. */
662 iperhole = max_t(xfs_agino_t, mp->m_sb.sb_inopblock,
663 XFS_INODES_PER_HOLEMASK_BIT);
664 aglen = iperhole / mp->m_sb.sb_inopblock;
665 for (i = 0, agino = irec.ir_startino;
666 i < XFS_INOBT_HOLEMASK_BITS;
667 i += iperhole / XFS_INODES_PER_HOLEMASK_BIT, agino += iperhole) {
669 if (irec.ir_holemask & (1 << i))
672 /* Record the inode chunk otherwise. */
673 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
674 error = xagb_bitmap_set(&ri->ichunk_blocks, agbno, aglen);
682 /* Collect rmaps for the blocks containing inode btrees and the inode chunks. */
684 xrep_rmap_find_inode_rmaps(
685 struct xrep_rmap *rr)
687 struct xrep_rmap_inodes ri = {
690 struct xfs_scrub *sc = rr->sc;
693 xagb_bitmap_init(&ri.inobt_blocks);
694 xagb_bitmap_init(&ri.ichunk_blocks);
697 * Iterate every record in the inobt so we can capture all the inode
698 * chunks and the blocks in the inobt itself.
700 error = xfs_btree_query_all(sc->sa.ino_cur, xrep_rmap_walk_inobt, &ri);
705 * Note that if there are zero records in the inobt then query_all does
706 * nothing and we have to account the empty inobt root manually.
708 if (xagb_bitmap_empty(&ri.ichunk_blocks)) {
709 struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
711 error = xagb_bitmap_set(&ri.inobt_blocks,
712 be32_to_cpu(agi->agi_root), 1);
717 /* Scan the finobt too. */
718 if (xfs_has_finobt(sc->mp)) {
719 error = xagb_bitmap_set_btblocks(&ri.inobt_blocks,
725 /* Generate rmaps for everything. */
726 error = xrep_rmap_stash_bitmap(rr, &ri.inobt_blocks,
727 &XFS_RMAP_OINFO_INOBT);
730 error = xrep_rmap_stash_bitmap(rr, &ri.ichunk_blocks,
731 &XFS_RMAP_OINFO_INODES);
734 xagb_bitmap_destroy(&ri.inobt_blocks);
735 xagb_bitmap_destroy(&ri.ichunk_blocks);
739 /* Record a CoW staging extent. */
741 xrep_rmap_walk_cowblocks(
742 struct xfs_btree_cur *cur,
743 const struct xfs_refcount_irec *irec,
746 struct xagb_bitmap *bitmap = priv;
748 if (!xfs_refcount_check_domain(irec) ||
749 irec->rc_domain != XFS_REFC_DOMAIN_COW)
750 return -EFSCORRUPTED;
752 return xagb_bitmap_set(bitmap, irec->rc_startblock, irec->rc_blockcount);
756 * Collect rmaps for the blocks containing the refcount btree, and all CoW
760 xrep_rmap_find_refcount_rmaps(
761 struct xrep_rmap *rr)
763 struct xagb_bitmap refcountbt_blocks; /* REFCBIT */
764 struct xagb_bitmap cow_blocks; /* COWBIT */
765 struct xfs_refcount_irec low = {
767 .rc_domain = XFS_REFC_DOMAIN_COW,
769 struct xfs_refcount_irec high = {
770 .rc_startblock = -1U,
771 .rc_domain = XFS_REFC_DOMAIN_COW,
773 struct xfs_scrub *sc = rr->sc;
776 if (!xfs_has_reflink(sc->mp))
779 xagb_bitmap_init(&refcountbt_blocks);
780 xagb_bitmap_init(&cow_blocks);
783 error = xagb_bitmap_set_btblocks(&refcountbt_blocks, sc->sa.refc_cur);
787 /* Collect rmaps for CoW staging extents. */
788 error = xfs_refcount_query_range(sc->sa.refc_cur, &low, &high,
789 xrep_rmap_walk_cowblocks, &cow_blocks);
793 /* Generate rmaps for everything. */
794 error = xrep_rmap_stash_bitmap(rr, &cow_blocks, &XFS_RMAP_OINFO_COW);
797 error = xrep_rmap_stash_bitmap(rr, &refcountbt_blocks,
798 &XFS_RMAP_OINFO_REFC);
801 xagb_bitmap_destroy(&cow_blocks);
802 xagb_bitmap_destroy(&refcountbt_blocks);
806 /* Generate rmaps for the AG headers (AGI/AGF/AGFL) */
808 xrep_rmap_find_agheader_rmaps(
809 struct xrep_rmap *rr)
811 struct xfs_scrub *sc = rr->sc;
813 /* Create a record for the AG sb->agfl. */
814 return xrep_rmap_stash(rr, XFS_SB_BLOCK(sc->mp),
815 XFS_AGFL_BLOCK(sc->mp) - XFS_SB_BLOCK(sc->mp) + 1,
816 XFS_RMAP_OWN_FS, 0, 0);
819 /* Generate rmaps for the log, if it's in this AG. */
821 xrep_rmap_find_log_rmaps(
822 struct xrep_rmap *rr)
824 struct xfs_scrub *sc = rr->sc;
826 if (!xfs_ag_contains_log(sc->mp, sc->sa.pag->pag_agno))
829 return xrep_rmap_stash(rr,
830 XFS_FSB_TO_AGBNO(sc->mp, sc->mp->m_sb.sb_logstart),
831 sc->mp->m_sb.sb_logblocks, XFS_RMAP_OWN_LOG, 0, 0);
834 /* Check and count all the records that we gathered. */
836 xrep_rmap_check_record(
837 struct xfs_btree_cur *cur,
838 const struct xfs_rmap_irec *rec,
841 struct xrep_rmap *rr = priv;
844 error = xrep_rmap_check_mapping(rr->sc, rec);
853 * Generate all the reverse-mappings for this AG, a list of the old rmapbt
854 * blocks, and the new btreeblks count. Figure out if we have enough free
855 * space to reconstruct the inode btrees. The caller must clean up the lists
856 * if anything goes wrong. This implements section (I) above.
859 xrep_rmap_find_rmaps(
860 struct xrep_rmap *rr)
862 struct xfs_scrub *sc = rr->sc;
863 struct xchk_ag *sa = &sc->sa;
864 struct xfs_inode *ip;
865 struct xfs_btree_cur *mcur;
868 /* Find all the per-AG metadata. */
869 xrep_ag_btcur_init(sc, &sc->sa);
871 error = xrep_rmap_find_inode_rmaps(rr);
875 error = xrep_rmap_find_refcount_rmaps(rr);
879 error = xrep_rmap_find_agheader_rmaps(rr);
883 error = xrep_rmap_find_log_rmaps(rr);
885 xchk_ag_btcur_free(&sc->sa);
890 * Set up for a potentially lengthy filesystem scan by reducing our
891 * transaction resource usage for the duration. Specifically:
893 * Unlock the AG header buffers and cancel the transaction to release
894 * the log grant space while we scan the filesystem.
896 * Create a new empty transaction to eliminate the possibility of the
897 * inode scan deadlocking on cyclical metadata.
899 * We pass the empty transaction to the file scanning function to avoid
900 * repeatedly cycling empty transactions. This can be done even though
901 * we take the IOLOCK to quiesce the file because empty transactions
902 * do not take sb_internal.
906 xchk_trans_cancel(sc);
907 error = xchk_trans_alloc_empty(sc);
911 /* Iterate all AGs for inodes rmaps. */
912 while ((error = xchk_iscan_iter(&rr->iscan, &ip)) == 1) {
913 error = xrep_rmap_scan_inode(rr, ip);
918 if (xchk_should_terminate(sc, &error))
921 xchk_iscan_iter_finish(&rr->iscan);
926 * Switch out for a real transaction and lock the AG headers in
927 * preparation for building a new tree.
929 xchk_trans_cancel(sc);
930 error = xchk_setup_fs(sc);
933 error = xchk_perag_drain_and_lock(sc);
938 * If a hook failed to update the in-memory btree, we lack the data to
939 * continue the repair.
941 if (xchk_iscan_aborted(&rr->iscan))
942 return -EFSCORRUPTED;
945 * Now that we have everything locked again, we need to count the
946 * number of rmap records stashed in the btree. This should reflect
947 * all actively-owned space in the filesystem. At the same time, check
948 * all our records before we start building a new btree, which requires
951 mcur = xfs_rmapbt_mem_cursor(rr->sc->sa.pag, NULL, &rr->rmap_btree);
952 sc->sa.bno_cur = xfs_bnobt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
956 error = xfs_rmap_query_all(mcur, xrep_rmap_check_record, rr);
958 xfs_btree_del_cursor(sc->sa.bno_cur, error);
959 sc->sa.bno_cur = NULL;
960 xfs_btree_del_cursor(mcur, error);
965 /* Section (II): Reserving space for new rmapbt and setting free space bitmap */
967 struct xrep_rmap_agfl {
968 struct xagb_bitmap *bitmap;
972 /* Add an AGFL block to the rmap list. */
975 struct xfs_mount *mp,
979 struct xrep_rmap_agfl *ra = priv;
981 return xagb_bitmap_set(ra->bitmap, agbno, 1);
985 * Run one round of reserving space for the new rmapbt and recomputing the
986 * number of blocks needed to store the previously observed rmapbt records and
987 * the ones we'll create for the free space metadata. When we don't need more
988 * blocks, return a bitmap of OWN_AG extents in @freesp_blocks and set @done to
992 xrep_rmap_try_reserve(
993 struct xrep_rmap *rr,
994 struct xfs_btree_cur *rmap_cur,
995 struct xagb_bitmap *freesp_blocks,
996 uint64_t *blocks_reserved,
999 struct xrep_rmap_agfl ra = {
1000 .bitmap = freesp_blocks,
1001 .agno = rr->sc->sa.pag->pag_agno,
1003 struct xfs_scrub *sc = rr->sc;
1004 struct xrep_newbt_resv *resv, *n;
1005 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
1006 struct xfs_buf *agfl_bp;
1007 uint64_t nr_blocks; /* RMB */
1008 uint64_t freesp_records;
1012 * We're going to recompute new_btree.bload.nr_blocks at the end of
1013 * this function to reflect however many btree blocks we need to store
1014 * all the rmap records (including the ones that reflect the changes we
1015 * made to support the new rmapbt blocks), so we save the old value
1016 * here so we can decide if we've reserved enough blocks.
1018 nr_blocks = rr->new_btree.bload.nr_blocks;
1021 * Make sure we've reserved enough space for the new btree. This can
1022 * change the shape of the free space btrees, which can cause secondary
1023 * interactions with the rmap records because all three space btrees
1024 * have the same rmap owner. We'll account for all that below.
1026 error = xrep_newbt_alloc_blocks(&rr->new_btree,
1027 nr_blocks - *blocks_reserved);
1031 *blocks_reserved = rr->new_btree.bload.nr_blocks;
1033 /* Clear everything in the bitmap. */
1034 xagb_bitmap_destroy(freesp_blocks);
1036 /* Set all the bnobt blocks in the bitmap. */
1037 sc->sa.bno_cur = xfs_bnobt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
1039 error = xagb_bitmap_set_btblocks(freesp_blocks, sc->sa.bno_cur);
1040 xfs_btree_del_cursor(sc->sa.bno_cur, error);
1041 sc->sa.bno_cur = NULL;
1045 /* Set all the cntbt blocks in the bitmap. */
1046 sc->sa.cnt_cur = xfs_cntbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
1048 error = xagb_bitmap_set_btblocks(freesp_blocks, sc->sa.cnt_cur);
1049 xfs_btree_del_cursor(sc->sa.cnt_cur, error);
1050 sc->sa.cnt_cur = NULL;
1054 /* Record our new btreeblks value. */
1055 rr->freesp_btblocks = xagb_bitmap_hweight(freesp_blocks) - 2;
1057 /* Set all the new rmapbt blocks in the bitmap. */
1058 list_for_each_entry_safe(resv, n, &rr->new_btree.resv_list, list) {
1059 error = xagb_bitmap_set(freesp_blocks, resv->agbno, resv->len);
1064 /* Set all the AGFL blocks in the bitmap. */
1065 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
1069 error = xfs_agfl_walk(sc->mp, agf, agfl_bp, xrep_rmap_walk_agfl, &ra);
1073 /* Count the extents in the bitmap. */
1074 freesp_records = xagb_bitmap_count_set_regions(freesp_blocks);
1076 /* Compute how many blocks we'll need for all the rmaps. */
1077 error = xfs_btree_bload_compute_geometry(rmap_cur,
1078 &rr->new_btree.bload, rr->nr_records + freesp_records);
1082 /* We're done when we don't need more blocks. */
1083 *done = nr_blocks >= rr->new_btree.bload.nr_blocks;
1088 * Iteratively reserve space for rmap btree while recording OWN_AG rmaps for
1089 * the free space metadata. This implements section (II) above.
1092 xrep_rmap_reserve_space(
1093 struct xrep_rmap *rr,
1094 struct xfs_btree_cur *rmap_cur)
1096 struct xagb_bitmap freesp_blocks; /* AGBIT */
1097 uint64_t blocks_reserved = 0;
1101 /* Compute how many blocks we'll need for the rmaps collected so far. */
1102 error = xfs_btree_bload_compute_geometry(rmap_cur,
1103 &rr->new_btree.bload, rr->nr_records);
1107 /* Last chance to abort before we start committing fixes. */
1108 if (xchk_should_terminate(rr->sc, &error))
1111 xagb_bitmap_init(&freesp_blocks);
1114 * Iteratively reserve space for the new rmapbt and recompute the
1115 * number of blocks needed to store the previously observed rmapbt
1116 * records and the ones we'll create for the free space metadata.
1117 * Finish when we don't need more blocks.
1120 error = xrep_rmap_try_reserve(rr, rmap_cur, &freesp_blocks,
1121 &blocks_reserved, &done);
1126 /* Emit rmaps for everything in the free space bitmap. */
1127 xrep_ag_btcur_init(rr->sc, &rr->sc->sa);
1128 error = xrep_rmap_stash_bitmap(rr, &freesp_blocks, &XFS_RMAP_OINFO_AG);
1129 xchk_ag_btcur_free(&rr->sc->sa);
1132 xagb_bitmap_destroy(&freesp_blocks);
1136 /* Section (III): Building the new rmap btree. */
1138 /* Update the AGF counters. */
1140 xrep_rmap_reset_counters(
1141 struct xrep_rmap *rr)
1143 struct xfs_scrub *sc = rr->sc;
1144 struct xfs_perag *pag = sc->sa.pag;
1145 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
1146 xfs_agblock_t rmap_btblocks;
1149 * The AGF header contains extra information related to the reverse
1150 * mapping btree, so we must update those fields here.
1152 rmap_btblocks = rr->new_btree.afake.af_blocks - 1;
1153 agf->agf_btreeblks = cpu_to_be32(rr->freesp_btblocks + rmap_btblocks);
1154 xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_BTREEBLKS);
1157 * After we commit the new btree to disk, it is possible that the
1158 * process to reap the old btree blocks will race with the AIL trying
1159 * to checkpoint the old btree blocks into the filesystem. If the new
1160 * tree is shorter than the old one, the rmapbt write verifier will
1161 * fail and the AIL will shut down the filesystem.
1163 * To avoid this, save the old incore btree height values as the alt
1164 * height values before re-initializing the perag info from the updated
1165 * AGF to capture all the new values.
1167 pag->pagf_repair_rmap_level = pag->pagf_rmap_level;
1169 /* Reinitialize with the values we just logged. */
1170 return xrep_reinit_pagf(sc);
1173 /* Retrieve rmapbt data for bulk load. */
1175 xrep_rmap_get_records(
1176 struct xfs_btree_cur *cur,
1178 struct xfs_btree_block *block,
1179 unsigned int nr_wanted,
1182 struct xrep_rmap *rr = priv;
1183 union xfs_btree_rec *block_rec;
1184 unsigned int loaded;
1187 for (loaded = 0; loaded < nr_wanted; loaded++, idx++) {
1190 error = xfs_btree_increment(rr->mcur, 0, &stat);
1194 return -EFSCORRUPTED;
1196 error = xfs_rmap_get_rec(rr->mcur, &cur->bc_rec.r, &stat);
1200 return -EFSCORRUPTED;
1202 block_rec = xfs_btree_rec_addr(cur, idx, block);
1203 cur->bc_ops->init_rec_from_cur(cur, block_rec);
1209 /* Feed one of the new btree blocks to the bulk loader. */
1211 xrep_rmap_claim_block(
1212 struct xfs_btree_cur *cur,
1213 union xfs_btree_ptr *ptr,
1216 struct xrep_rmap *rr = priv;
1218 return xrep_newbt_claim_block(cur, &rr->new_btree, ptr);
1221 /* Custom allocation function for new rmap btrees. */
1223 xrep_rmap_alloc_vextent(
1224 struct xfs_scrub *sc,
1225 struct xfs_alloc_arg *args,
1226 xfs_fsblock_t alloc_hint)
1231 * We don't want an rmap update on the allocation, since we iteratively
1232 * compute the OWN_AG records /after/ allocating blocks for the records
1233 * that we already know we need to store. Therefore, fix the freelist
1234 * with the NORMAP flag set so that we don't also try to create an rmap
1235 * for new AGFL blocks.
1237 error = xrep_fix_freelist(sc, XFS_ALLOC_FLAG_NORMAP);
1242 * If xrep_fix_freelist fixed the freelist by moving blocks from the
1243 * free space btrees or by removing blocks from the AGFL and queueing
1244 * an EFI to free the block, the transaction will be dirty. This
1245 * second case is of interest to us.
1247 * Later on, we will need to compare gaps in the new recordset against
1248 * the block usage of all OWN_AG owners in order to free the old
1249 * btree's blocks, which means that we can't have EFIs for former AGFL
1250 * blocks attached to the repair transaction when we commit the new
1253 * xrep_newbt_alloc_blocks guarantees this for us by calling
1254 * xrep_defer_finish to commit anything that fix_freelist may have
1255 * added to the transaction.
1257 return xfs_alloc_vextent_near_bno(args, alloc_hint);
1261 /* Count the records in this btree. */
1263 xrep_rmap_count_records(
1264 struct xfs_btree_cur *cur,
1265 unsigned long long *nr)
1272 error = xfs_btree_goto_left_edge(cur);
1276 while (running && !(error = xfs_btree_increment(cur, 0, &running))) {
1284 * Use the collected rmap information to stage a new rmap btree. If this is
1285 * successful we'll return with the new btree root information logged to the
1286 * repair transaction but not yet committed. This implements section (III)
1290 xrep_rmap_build_new_tree(
1291 struct xrep_rmap *rr)
1293 struct xfs_scrub *sc = rr->sc;
1294 struct xfs_perag *pag = sc->sa.pag;
1295 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
1296 struct xfs_btree_cur *rmap_cur;
1297 xfs_fsblock_t fsbno;
1301 * Preserve the old rmapbt block count so that we can adjust the
1302 * per-AG rmapbt reservation after we commit the new btree root and
1303 * want to dispose of the old btree blocks.
1305 rr->old_rmapbt_fsbcount = be32_to_cpu(agf->agf_rmap_blocks);
1308 * Prepare to construct the new btree by reserving disk space for the
1309 * new btree and setting up all the accounting information we'll need
1310 * to root the new btree while it's under construction and before we
1311 * attach it to the AG header. The new blocks are accounted to the
1312 * rmapbt per-AG reservation, which we will adjust further after
1313 * committing the new btree.
1315 fsbno = XFS_AGB_TO_FSB(sc->mp, pag->pag_agno, XFS_RMAP_BLOCK(sc->mp));
1316 xrep_newbt_init_ag(&rr->new_btree, sc, &XFS_RMAP_OINFO_SKIP_UPDATE,
1317 fsbno, XFS_AG_RESV_RMAPBT);
1318 rr->new_btree.bload.get_records = xrep_rmap_get_records;
1319 rr->new_btree.bload.claim_block = xrep_rmap_claim_block;
1320 rr->new_btree.alloc_vextent = xrep_rmap_alloc_vextent;
1321 rmap_cur = xfs_rmapbt_init_cursor(sc->mp, NULL, NULL, pag);
1322 xfs_btree_stage_afakeroot(rmap_cur, &rr->new_btree.afake);
1325 * Initialize @rr->new_btree, reserve space for the new rmapbt,
1326 * and compute OWN_AG rmaps.
1328 error = xrep_rmap_reserve_space(rr, rmap_cur);
1333 * Count the rmapbt records again, because the space reservation
1334 * for the rmapbt itself probably added more records to the btree.
1336 rr->mcur = xfs_rmapbt_mem_cursor(rr->sc->sa.pag, NULL,
1339 error = xrep_rmap_count_records(rr->mcur, &rr->nr_records);
1344 * Due to btree slack factors, it's possible for a new btree to be one
1345 * level taller than the old btree. Update the incore btree height so
1346 * that we don't trip the verifiers when writing the new btree blocks
1349 pag->pagf_repair_rmap_level = rr->new_btree.bload.btree_height;
1352 * Move the cursor to the left edge of the tree so that the first
1353 * increment in ->get_records positions us at the first record.
1355 error = xfs_btree_goto_left_edge(rr->mcur);
1359 /* Add all observed rmap records. */
1360 error = xfs_btree_bload(rmap_cur, &rr->new_btree.bload, rr);
1365 * Install the new btree in the AG header. After this point the old
1366 * btree is no longer accessible and the new tree is live.
1368 xfs_rmapbt_commit_staged_btree(rmap_cur, sc->tp, sc->sa.agf_bp);
1369 xfs_btree_del_cursor(rmap_cur, 0);
1370 xfs_btree_del_cursor(rr->mcur, 0);
1374 * Now that we've written the new btree to disk, we don't need to keep
1375 * updating the in-memory btree. Abort the scan to stop live updates.
1377 xchk_iscan_abort(&rr->iscan);
1380 * The newly committed rmap recordset includes mappings for the blocks
1381 * that we reserved to build the new btree. If there is excess space
1382 * reservation to be freed, the corresponding rmap records must also be
1385 rr->new_btree.oinfo = XFS_RMAP_OINFO_AG;
1387 /* Reset the AGF counters now that we've changed the btree shape. */
1388 error = xrep_rmap_reset_counters(rr);
1392 /* Dispose of any unused blocks and the accounting information. */
1393 error = xrep_newbt_commit(&rr->new_btree);
1397 return xrep_roll_ag_trans(sc);
1400 pag->pagf_repair_rmap_level = 0;
1402 xfs_btree_del_cursor(rr->mcur, error);
1404 xfs_btree_del_cursor(rmap_cur, error);
1406 xrep_newbt_cancel(&rr->new_btree);
1410 /* Section (IV): Reaping the old btree. */
1412 struct xrep_rmap_find_gaps {
1413 struct xagb_bitmap rmap_gaps;
1414 xfs_agblock_t next_agbno;
1417 /* Subtract each free extent in the bnobt from the rmap gaps. */
1419 xrep_rmap_find_freesp(
1420 struct xfs_btree_cur *cur,
1421 const struct xfs_alloc_rec_incore *rec,
1424 struct xrep_rmap_find_gaps *rfg = priv;
1426 return xagb_bitmap_clear(&rfg->rmap_gaps, rec->ar_startblock,
1427 rec->ar_blockcount);
1430 /* Record the free space we find, as part of cleaning out the btree. */
1432 xrep_rmap_find_gaps(
1433 struct xfs_btree_cur *cur,
1434 const struct xfs_rmap_irec *rec,
1437 struct xrep_rmap_find_gaps *rfg = priv;
1440 if (rec->rm_startblock > rfg->next_agbno) {
1441 error = xagb_bitmap_set(&rfg->rmap_gaps, rfg->next_agbno,
1442 rec->rm_startblock - rfg->next_agbno);
1447 rfg->next_agbno = max_t(xfs_agblock_t, rfg->next_agbno,
1448 rec->rm_startblock + rec->rm_blockcount);
1453 * Reap the old rmapbt blocks. Now that the rmapbt is fully rebuilt, we make
1454 * a list of gaps in the rmap records and a list of the extents mentioned in
1455 * the bnobt. Any block that's in the new rmapbt gap list but not mentioned
1456 * in the bnobt is a block from the old rmapbt and can be removed.
1459 xrep_rmap_remove_old_tree(
1460 struct xrep_rmap *rr)
1462 struct xrep_rmap_find_gaps rfg = {
1465 struct xfs_scrub *sc = rr->sc;
1466 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
1467 struct xfs_perag *pag = sc->sa.pag;
1468 struct xfs_btree_cur *mcur;
1469 xfs_agblock_t agend;
1472 xagb_bitmap_init(&rfg.rmap_gaps);
1474 /* Compute free space from the new rmapbt. */
1475 mcur = xfs_rmapbt_mem_cursor(rr->sc->sa.pag, NULL, &rr->rmap_btree);
1477 error = xfs_rmap_query_all(mcur, xrep_rmap_find_gaps, &rfg);
1478 xfs_btree_del_cursor(mcur, error);
1482 /* Insert a record for space between the last rmap and EOAG. */
1483 agend = be32_to_cpu(agf->agf_length);
1484 if (rfg.next_agbno < agend) {
1485 error = xagb_bitmap_set(&rfg.rmap_gaps, rfg.next_agbno,
1486 agend - rfg.next_agbno);
1491 /* Compute free space from the existing bnobt. */
1492 sc->sa.bno_cur = xfs_bnobt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
1494 error = xfs_alloc_query_all(sc->sa.bno_cur, xrep_rmap_find_freesp,
1496 xfs_btree_del_cursor(sc->sa.bno_cur, error);
1497 sc->sa.bno_cur = NULL;
1502 * Free the "free" blocks that the new rmapbt knows about but the bnobt
1503 * doesn't--these are the old rmapbt blocks. Credit the old rmapbt
1504 * block usage count back to the per-AG rmapbt reservation (and not
1505 * fdblocks, since the rmap btree lives in free space) to keep the
1506 * reservation and free space accounting correct.
1508 error = xrep_reap_agblocks(sc, &rfg.rmap_gaps,
1509 &XFS_RMAP_OINFO_ANY_OWNER, XFS_AG_RESV_RMAPBT);
1514 * Now that we've zapped all the old rmapbt blocks we can turn off
1515 * the alternate height mechanism and reset the per-AG space
1518 pag->pagf_repair_rmap_level = 0;
1519 sc->flags |= XREP_RESET_PERAG_RESV;
1521 xagb_bitmap_destroy(&rfg.rmap_gaps);
1526 xrep_rmapbt_want_live_update(
1527 struct xchk_iscan *iscan,
1528 const struct xfs_owner_info *oi)
1530 if (xchk_iscan_aborted(iscan))
1534 * Before unlocking the AG header to perform the inode scan, we
1535 * recorded reverse mappings for all AG metadata except for the OWN_AG
1536 * metadata. IOWs, the in-memory btree knows about the AG headers, the
1537 * two inode btrees, the CoW staging extents, and the refcount btrees.
1538 * For these types of metadata, we need to record the live updates in
1539 * the in-memory rmap btree.
1541 * However, we do not scan the free space btrees or the AGFL until we
1542 * have re-locked the AGF and are ready to reserve space for the new
1543 * rmap btree, so we do not want live updates for OWN_AG metadata.
1545 if (XFS_RMAP_NON_INODE_OWNER(oi->oi_owner))
1546 return oi->oi_owner != XFS_RMAP_OWN_AG;
1548 /* Ignore updates to files that the scanner hasn't visited yet. */
1549 return xchk_iscan_want_live_update(iscan, oi->oi_owner);
1553 * Apply a rmapbt update from the regular filesystem into our shadow btree.
1554 * We're running from the thread that owns the AGF buffer and is generating
1555 * the update, so we must be careful about which parts of the struct xrep_rmap
1559 xrep_rmapbt_live_update(
1560 struct notifier_block *nb,
1561 unsigned long action,
1564 struct xfs_rmap_update_params *p = data;
1565 struct xrep_rmap *rr;
1566 struct xfs_mount *mp;
1567 struct xfs_btree_cur *mcur;
1568 struct xfs_trans *tp;
1572 rr = container_of(nb, struct xrep_rmap, rhook.rmap_hook.nb);
1575 if (!xrep_rmapbt_want_live_update(&rr->iscan, &p->oinfo))
1578 trace_xrep_rmap_live_update(mp, rr->sc->sa.pag->pag_agno, action, p);
1580 error = xrep_trans_alloc_hook_dummy(mp, &txcookie, &tp);
1584 mutex_lock(&rr->lock);
1585 mcur = xfs_rmapbt_mem_cursor(rr->sc->sa.pag, tp, &rr->rmap_btree);
1586 error = __xfs_rmap_finish_intent(mcur, action, p->startblock,
1587 p->blockcount, &p->oinfo, p->unwritten);
1588 xfs_btree_del_cursor(mcur, error);
1592 error = xfbtree_trans_commit(&rr->rmap_btree, tp);
1596 xrep_trans_cancel_hook_dummy(&txcookie, tp);
1597 mutex_unlock(&rr->lock);
1601 xfbtree_trans_cancel(&rr->rmap_btree, tp);
1602 xrep_trans_cancel_hook_dummy(&txcookie, tp);
1604 mutex_unlock(&rr->lock);
1605 xchk_iscan_abort(&rr->iscan);
1610 /* Set up the filesystem scan components. */
1612 xrep_rmap_setup_scan(
1613 struct xrep_rmap *rr)
1615 struct xfs_scrub *sc = rr->sc;
1618 mutex_init(&rr->lock);
1620 /* Set up in-memory rmap btree */
1621 error = xfs_rmapbt_mem_init(sc->mp, &rr->rmap_btree, sc->xmbtp,
1622 sc->sa.pag->pag_agno);
1626 /* Retry iget every tenth of a second for up to 30 seconds. */
1627 xchk_iscan_start(sc, 30000, 100, &rr->iscan);
1630 * Hook into live rmap operations so that we can update our in-memory
1631 * btree to reflect live changes on the filesystem. Since we drop the
1632 * AGF buffer to scan all the inodes, we need this piece to avoid
1633 * installing a stale btree.
1635 ASSERT(sc->flags & XCHK_FSGATES_RMAP);
1636 xfs_rmap_hook_setup(&rr->rhook, xrep_rmapbt_live_update);
1637 error = xfs_rmap_hook_add(sc->sa.pag, &rr->rhook);
1643 xchk_iscan_teardown(&rr->iscan);
1644 xfbtree_destroy(&rr->rmap_btree);
1646 mutex_destroy(&rr->lock);
1650 /* Tear down scan components. */
1653 struct xrep_rmap *rr)
1655 struct xfs_scrub *sc = rr->sc;
1657 xchk_iscan_abort(&rr->iscan);
1658 xfs_rmap_hook_del(sc->sa.pag, &rr->rhook);
1659 xchk_iscan_teardown(&rr->iscan);
1660 xfbtree_destroy(&rr->rmap_btree);
1661 mutex_destroy(&rr->lock);
1664 /* Repair the rmap btree for some AG. */
1667 struct xfs_scrub *sc)
1669 struct xrep_rmap *rr = sc->buf;
1672 error = xrep_rmap_setup_scan(rr);
1677 * Collect rmaps for everything in this AG that isn't space metadata.
1678 * These rmaps won't change even as we try to allocate blocks.
1680 error = xrep_rmap_find_rmaps(rr);
1684 /* Rebuild the rmap information. */
1685 error = xrep_rmap_build_new_tree(rr);
1689 /* Kill the old tree. */
1690 error = xrep_rmap_remove_old_tree(rr);
1695 xrep_rmap_teardown(rr);