1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
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"
16 #include "xfs_alloc.h"
17 #include "xfs_alloc_btree.h"
18 #include "xfs_ialloc.h"
19 #include "xfs_ialloc_btree.h"
21 #include "xfs_rmap_btree.h"
22 #include "xfs_refcount_btree.h"
24 #include "scrub/scrub.h"
25 #include "scrub/common.h"
26 #include "scrub/trace.h"
27 #include "scrub/repair.h"
28 #include "scrub/bitmap.h"
29 #include "scrub/reap.h"
33 /* Repair the superblock. */
38 struct xfs_mount *mp = sc->mp;
43 /* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
44 agno = sc->sm->sm_agno;
48 error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
52 /* Last chance to abort before we start committing fixes. */
53 if (xchk_should_terminate(sc, &error))
56 /* Copy AG 0's superblock to this one. */
57 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
58 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
61 * Don't write out a secondary super with NEEDSREPAIR or log incompat
62 * features set, since both are ignored when set on a secondary.
64 if (xfs_has_crc(mp)) {
65 struct xfs_dsb *sb = bp->b_addr;
67 sb->sb_features_incompat &=
68 ~cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
69 sb->sb_features_log_incompat = 0;
72 /* Write this to disk. */
73 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
74 xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
80 struct xrep_agf_allocbt {
82 xfs_agblock_t freeblks;
83 xfs_agblock_t longest;
86 /* Record free space shape information. */
88 xrep_agf_walk_allocbt(
89 struct xfs_btree_cur *cur,
90 const struct xfs_alloc_rec_incore *rec,
93 struct xrep_agf_allocbt *raa = priv;
96 if (xchk_should_terminate(raa->sc, &error))
99 raa->freeblks += rec->ar_blockcount;
100 if (rec->ar_blockcount > raa->longest)
101 raa->longest = rec->ar_blockcount;
105 /* Does this AGFL block look sane? */
107 xrep_agf_check_agfl_block(
108 struct xfs_mount *mp,
112 struct xfs_scrub *sc = priv;
114 if (!xfs_verify_agbno(sc->sa.pag, agbno))
115 return -EFSCORRUPTED;
120 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
121 * XFS_BTNUM_ names here to avoid creating a sparse array.
132 /* Check a btree root candidate. */
134 xrep_check_btree_root(
135 struct xfs_scrub *sc,
136 struct xrep_find_ag_btree *fab)
138 return xfs_verify_agbno(sc->sa.pag, fab->root) &&
139 fab->height <= fab->maxlevels;
143 * Given the btree roots described by *fab, find the roots, check them for
144 * sanity, and pass the root data back out via *fab.
146 * This is /also/ a chicken and egg problem because we have to use the rmapbt
147 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no
148 * idea if the btrees make any sense. If we hit obvious corruptions in those
149 * btrees we'll bail out.
152 xrep_agf_find_btrees(
153 struct xfs_scrub *sc,
154 struct xfs_buf *agf_bp,
155 struct xrep_find_ag_btree *fab,
156 struct xfs_buf *agfl_bp)
158 struct xfs_agf *old_agf = agf_bp->b_addr;
161 /* Go find the root data. */
162 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
166 /* We must find the bnobt, cntbt, and rmapbt roots. */
167 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
168 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
169 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
170 return -EFSCORRUPTED;
173 * We relied on the rmapbt to reconstruct the AGF. If we get a
174 * different root then something's seriously wrong.
176 if (fab[XREP_AGF_RMAPBT].root !=
177 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
178 return -EFSCORRUPTED;
180 /* We must find the refcountbt root if that feature is enabled. */
181 if (xfs_has_reflink(sc->mp) &&
182 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
183 return -EFSCORRUPTED;
189 * Reinitialize the AGF header, making an in-core copy of the old contents so
190 * that we know which in-core state needs to be reinitialized.
193 xrep_agf_init_header(
194 struct xfs_scrub *sc,
195 struct xfs_buf *agf_bp,
196 struct xfs_agf *old_agf)
198 struct xfs_mount *mp = sc->mp;
199 struct xfs_perag *pag = sc->sa.pag;
200 struct xfs_agf *agf = agf_bp->b_addr;
202 memcpy(old_agf, agf, sizeof(*old_agf));
203 memset(agf, 0, BBTOB(agf_bp->b_length));
204 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
205 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
206 agf->agf_seqno = cpu_to_be32(pag->pag_agno);
207 agf->agf_length = cpu_to_be32(pag->block_count);
208 agf->agf_flfirst = old_agf->agf_flfirst;
209 agf->agf_fllast = old_agf->agf_fllast;
210 agf->agf_flcount = old_agf->agf_flcount;
212 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
214 /* Mark the incore AGF data stale until we're done fixing things. */
215 ASSERT(xfs_perag_initialised_agf(pag));
216 clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
219 /* Set btree root information in an AGF. */
222 struct xfs_scrub *sc,
224 struct xrep_find_ag_btree *fab)
226 agf->agf_roots[XFS_BTNUM_BNOi] =
227 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
228 agf->agf_levels[XFS_BTNUM_BNOi] =
229 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
231 agf->agf_roots[XFS_BTNUM_CNTi] =
232 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
233 agf->agf_levels[XFS_BTNUM_CNTi] =
234 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
236 agf->agf_roots[XFS_BTNUM_RMAPi] =
237 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
238 agf->agf_levels[XFS_BTNUM_RMAPi] =
239 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
241 if (xfs_has_reflink(sc->mp)) {
242 agf->agf_refcount_root =
243 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
244 agf->agf_refcount_level =
245 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
249 /* Update all AGF fields which derive from btree contents. */
251 xrep_agf_calc_from_btrees(
252 struct xfs_scrub *sc,
253 struct xfs_buf *agf_bp)
255 struct xrep_agf_allocbt raa = { .sc = sc };
256 struct xfs_btree_cur *cur = NULL;
257 struct xfs_agf *agf = agf_bp->b_addr;
258 struct xfs_mount *mp = sc->mp;
259 xfs_agblock_t btreeblks;
260 xfs_agblock_t blocks;
263 /* Update the AGF counters from the bnobt. */
264 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
265 sc->sa.pag, XFS_BTNUM_BNO);
266 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
269 error = xfs_btree_count_blocks(cur, &blocks);
272 xfs_btree_del_cursor(cur, error);
273 btreeblks = blocks - 1;
274 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
275 agf->agf_longest = cpu_to_be32(raa.longest);
277 /* Update the AGF counters from the cntbt. */
278 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
279 sc->sa.pag, XFS_BTNUM_CNT);
280 error = xfs_btree_count_blocks(cur, &blocks);
283 xfs_btree_del_cursor(cur, error);
284 btreeblks += blocks - 1;
286 /* Update the AGF counters from the rmapbt. */
287 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
288 error = xfs_btree_count_blocks(cur, &blocks);
291 xfs_btree_del_cursor(cur, error);
292 agf->agf_rmap_blocks = cpu_to_be32(blocks);
293 btreeblks += blocks - 1;
295 agf->agf_btreeblks = cpu_to_be32(btreeblks);
297 /* Update the AGF counters from the refcountbt. */
298 if (xfs_has_reflink(mp)) {
299 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
301 error = xfs_btree_count_blocks(cur, &blocks);
304 xfs_btree_del_cursor(cur, error);
305 agf->agf_refcount_blocks = cpu_to_be32(blocks);
310 xfs_btree_del_cursor(cur, error);
314 /* Commit the new AGF and reinitialize the incore state. */
317 struct xfs_scrub *sc,
318 struct xfs_buf *agf_bp)
320 struct xfs_perag *pag;
321 struct xfs_agf *agf = agf_bp->b_addr;
323 /* Trigger fdblocks recalculation */
324 xfs_force_summary_recalc(sc->mp);
326 /* Write this to disk. */
327 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
328 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
330 /* Now reinitialize the in-core counters we changed. */
332 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
333 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
334 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
335 pag->pagf_levels[XFS_BTNUM_BNOi] =
336 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
337 pag->pagf_levels[XFS_BTNUM_CNTi] =
338 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
339 pag->pagf_levels[XFS_BTNUM_RMAPi] =
340 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
341 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
342 set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
347 /* Repair the AGF. v5 filesystems only. */
350 struct xfs_scrub *sc)
352 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = {
354 .rmap_owner = XFS_RMAP_OWN_AG,
355 .buf_ops = &xfs_bnobt_buf_ops,
356 .maxlevels = sc->mp->m_alloc_maxlevels,
359 .rmap_owner = XFS_RMAP_OWN_AG,
360 .buf_ops = &xfs_cntbt_buf_ops,
361 .maxlevels = sc->mp->m_alloc_maxlevels,
363 [XREP_AGF_RMAPBT] = {
364 .rmap_owner = XFS_RMAP_OWN_AG,
365 .buf_ops = &xfs_rmapbt_buf_ops,
366 .maxlevels = sc->mp->m_rmap_maxlevels,
368 [XREP_AGF_REFCOUNTBT] = {
369 .rmap_owner = XFS_RMAP_OWN_REFC,
370 .buf_ops = &xfs_refcountbt_buf_ops,
371 .maxlevels = sc->mp->m_refc_maxlevels,
377 struct xfs_agf old_agf;
378 struct xfs_mount *mp = sc->mp;
379 struct xfs_buf *agf_bp;
380 struct xfs_buf *agfl_bp;
384 /* We require the rmapbt to rebuild anything. */
385 if (!xfs_has_rmapbt(mp))
389 * Make sure we have the AGF buffer, as scrub might have decided it
390 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
392 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
393 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
395 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
398 agf_bp->b_ops = &xfs_agf_buf_ops;
399 agf = agf_bp->b_addr;
402 * Load the AGFL so that we can screen out OWN_AG blocks that are on
403 * the AGFL now; these blocks might have once been part of the
404 * bno/cnt/rmap btrees but are not now. This is a chicken and egg
405 * problem: the AGF is corrupt, so we have to trust the AGFL contents
406 * because we can't do any serious cross-referencing with any of the
407 * btrees rooted in the AGF. If the AGFL contents are obviously bad
408 * then we'll bail out.
410 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
415 * Spot-check the AGFL blocks; if they're obviously corrupt then
416 * there's nothing we can do but bail out.
418 error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
419 xrep_agf_check_agfl_block, sc);
424 * Find the AGF btree roots. This is also a chicken-and-egg situation;
425 * see the function for more details.
427 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
431 /* Last chance to abort before we start committing fixes. */
432 if (xchk_should_terminate(sc, &error))
435 /* Start rewriting the header and implant the btrees we found. */
436 xrep_agf_init_header(sc, agf_bp, &old_agf);
437 xrep_agf_set_roots(sc, agf, fab);
438 error = xrep_agf_calc_from_btrees(sc, agf_bp);
442 /* Commit the changes and reinitialize incore state. */
443 return xrep_agf_commit_new(sc, agf_bp);
446 /* Mark the incore AGF state stale and revert the AGF. */
447 clear_bit(XFS_AGSTATE_AGF_INIT, &sc->sa.pag->pag_opstate);
448 memcpy(agf, &old_agf, sizeof(old_agf));
455 /* Bitmap of alleged AGFL blocks that we're not going to add. */
456 struct xagb_bitmap crossed;
458 /* Bitmap of other OWN_AG metadata blocks. */
459 struct xagb_bitmap agmetablocks;
461 /* Bitmap of free space. */
462 struct xagb_bitmap *freesp;
464 /* rmapbt cursor for finding crosslinked blocks */
465 struct xfs_btree_cur *rmap_cur;
467 struct xfs_scrub *sc;
470 /* Record all OWN_AG (free space btree) information from the rmap data. */
473 struct xfs_btree_cur *cur,
474 const struct xfs_rmap_irec *rec,
477 struct xrep_agfl *ra = priv;
480 if (xchk_should_terminate(ra->sc, &error))
483 /* Record all the OWN_AG blocks. */
484 if (rec->rm_owner == XFS_RMAP_OWN_AG) {
485 error = xagb_bitmap_set(ra->freesp, rec->rm_startblock,
491 return xagb_bitmap_set_btcur_path(&ra->agmetablocks, cur);
494 /* Strike out the blocks that are cross-linked according to the rmapbt. */
496 xrep_agfl_check_extent(
501 struct xrep_agfl *ra = priv;
502 xfs_agblock_t agbno = start;
503 xfs_agblock_t last_agbno = agbno + len - 1;
506 while (agbno <= last_agbno) {
509 error = xfs_rmap_has_other_keys(ra->rmap_cur, agbno, 1,
510 &XFS_RMAP_OINFO_AG, &other_owners);
515 error = xagb_bitmap_set(&ra->crossed, agbno, 1);
520 if (xchk_should_terminate(ra->sc, &error))
529 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
530 * which blocks belong to the AGFL.
532 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
533 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
534 * rmapbt). These are the old AGFL blocks, so return that list and the number
535 * of blocks we're actually going to put back on the AGFL.
538 xrep_agfl_collect_blocks(
539 struct xfs_scrub *sc,
540 struct xfs_buf *agf_bp,
541 struct xagb_bitmap *agfl_extents,
542 xfs_agblock_t *flcount)
545 struct xfs_mount *mp = sc->mp;
546 struct xfs_btree_cur *cur;
550 ra.freesp = agfl_extents;
551 xagb_bitmap_init(&ra.agmetablocks);
552 xagb_bitmap_init(&ra.crossed);
554 /* Find all space used by the free space btrees & rmapbt. */
555 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
556 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
557 xfs_btree_del_cursor(cur, error);
561 /* Find all blocks currently being used by the bnobt. */
562 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
563 sc->sa.pag, XFS_BTNUM_BNO);
564 error = xagb_bitmap_set_btblocks(&ra.agmetablocks, cur);
565 xfs_btree_del_cursor(cur, error);
569 /* Find all blocks currently being used by the cntbt. */
570 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
571 sc->sa.pag, XFS_BTNUM_CNT);
572 error = xagb_bitmap_set_btblocks(&ra.agmetablocks, cur);
573 xfs_btree_del_cursor(cur, error);
578 * Drop the freesp meta blocks that are in use by btrees.
579 * The remaining blocks /should/ be AGFL blocks.
581 error = xagb_bitmap_disunion(agfl_extents, &ra.agmetablocks);
585 /* Strike out the blocks that are cross-linked. */
586 ra.rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
587 error = xagb_bitmap_walk(agfl_extents, xrep_agfl_check_extent, &ra);
588 xfs_btree_del_cursor(ra.rmap_cur, error);
591 error = xagb_bitmap_disunion(agfl_extents, &ra.crossed);
596 * Calculate the new AGFL size. If we found more blocks than fit in
597 * the AGFL we'll free them later.
599 *flcount = min_t(uint64_t, xagb_bitmap_hweight(agfl_extents),
603 xagb_bitmap_destroy(&ra.crossed);
604 xagb_bitmap_destroy(&ra.agmetablocks);
608 /* Update the AGF and reset the in-core state. */
610 xrep_agfl_update_agf(
611 struct xfs_scrub *sc,
612 struct xfs_buf *agf_bp,
613 xfs_agblock_t flcount)
615 struct xfs_agf *agf = agf_bp->b_addr;
617 ASSERT(flcount <= xfs_agfl_size(sc->mp));
619 /* Trigger fdblocks recalculation */
620 xfs_force_summary_recalc(sc->mp);
622 /* Update the AGF counters. */
623 if (xfs_perag_initialised_agf(sc->sa.pag)) {
624 sc->sa.pag->pagf_flcount = flcount;
625 clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET,
626 &sc->sa.pag->pag_opstate);
628 agf->agf_flfirst = cpu_to_be32(0);
629 agf->agf_flcount = cpu_to_be32(flcount);
631 agf->agf_fllast = cpu_to_be32(flcount - 1);
633 agf->agf_fllast = cpu_to_be32(xfs_agfl_size(sc->mp) - 1);
635 xfs_alloc_log_agf(sc->tp, agf_bp,
636 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
639 struct xrep_agfl_fill {
640 struct xagb_bitmap used_extents;
641 struct xfs_scrub *sc;
643 xfs_agblock_t flcount;
647 /* Fill the AGFL with whatever blocks are in this extent. */
654 struct xrep_agfl_fill *af = priv;
655 struct xfs_scrub *sc = af->sc;
656 xfs_agblock_t agbno = start;
659 trace_xrep_agfl_insert(sc->sa.pag, agbno, len);
661 while (agbno < start + len && af->fl_off < af->flcount)
662 af->agfl_bno[af->fl_off++] = cpu_to_be32(agbno++);
664 error = xagb_bitmap_set(&af->used_extents, start, agbno - 1);
668 if (af->fl_off == af->flcount)
674 /* Write out a totally new AGFL. */
676 xrep_agfl_init_header(
677 struct xfs_scrub *sc,
678 struct xfs_buf *agfl_bp,
679 struct xagb_bitmap *agfl_extents,
680 xfs_agblock_t flcount)
682 struct xrep_agfl_fill af = {
686 struct xfs_mount *mp = sc->mp;
687 struct xfs_agfl *agfl;
690 ASSERT(flcount <= xfs_agfl_size(mp));
693 * Start rewriting the header by setting the bno[] array to
694 * NULLAGBLOCK, then setting AGFL header fields.
696 agfl = XFS_BUF_TO_AGFL(agfl_bp);
697 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
698 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
699 agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
700 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
703 * Fill the AGFL with the remaining blocks. If agfl_extents has more
704 * blocks than fit in the AGFL, they will be freed in a subsequent
707 xagb_bitmap_init(&af.used_extents);
708 af.agfl_bno = xfs_buf_to_agfl_bno(agfl_bp),
709 xagb_bitmap_walk(agfl_extents, xrep_agfl_fill, &af);
710 error = xagb_bitmap_disunion(agfl_extents, &af.used_extents);
714 /* Write new AGFL to disk. */
715 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
716 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
717 xagb_bitmap_destroy(&af.used_extents);
721 /* Repair the AGFL. */
724 struct xfs_scrub *sc)
726 struct xagb_bitmap agfl_extents;
727 struct xfs_mount *mp = sc->mp;
728 struct xfs_buf *agf_bp;
729 struct xfs_buf *agfl_bp;
730 xfs_agblock_t flcount;
733 /* We require the rmapbt to rebuild anything. */
734 if (!xfs_has_rmapbt(mp))
737 xagb_bitmap_init(&agfl_extents);
740 * Read the AGF so that we can query the rmapbt. We hope that there's
741 * nothing wrong with the AGF, but all the AG header repair functions
742 * have this chicken-and-egg problem.
744 error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
749 * Make sure we have the AGFL buffer, as scrub might have decided it
750 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
752 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
753 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
755 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
758 agfl_bp->b_ops = &xfs_agfl_buf_ops;
760 /* Gather all the extents we're going to put on the new AGFL. */
761 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
765 /* Last chance to abort before we start committing fixes. */
766 if (xchk_should_terminate(sc, &error))
770 * Update AGF and AGFL. We reset the global free block counter when
771 * we adjust the AGF flcount (which can fail) so avoid updating any
772 * buffers until we know that part works.
774 xrep_agfl_update_agf(sc, agf_bp, flcount);
775 error = xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
780 * Ok, the AGFL should be ready to go now. Roll the transaction to
781 * make the new AGFL permanent before we start using it to return
782 * freespace overflow to the freespace btrees.
784 sc->sa.agf_bp = agf_bp;
785 error = xrep_roll_ag_trans(sc);
789 /* Dump any AGFL overflow. */
790 error = xrep_reap_agblocks(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
793 xagb_bitmap_destroy(&agfl_extents);
800 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
801 * XFS_BTNUM_ names here to avoid creating a sparse array.
811 * Given the inode btree roots described by *fab, find the roots, check them
812 * for sanity, and pass the root data back out via *fab.
815 xrep_agi_find_btrees(
816 struct xfs_scrub *sc,
817 struct xrep_find_ag_btree *fab)
819 struct xfs_buf *agf_bp;
820 struct xfs_mount *mp = sc->mp;
824 error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
828 /* Find the btree roots. */
829 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
833 /* We must find the inobt root. */
834 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
835 return -EFSCORRUPTED;
837 /* We must find the finobt root if that feature is enabled. */
838 if (xfs_has_finobt(mp) &&
839 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
840 return -EFSCORRUPTED;
846 * Reinitialize the AGI header, making an in-core copy of the old contents so
847 * that we know which in-core state needs to be reinitialized.
850 xrep_agi_init_header(
851 struct xfs_scrub *sc,
852 struct xfs_buf *agi_bp,
853 struct xfs_agi *old_agi)
855 struct xfs_agi *agi = agi_bp->b_addr;
856 struct xfs_perag *pag = sc->sa.pag;
857 struct xfs_mount *mp = sc->mp;
859 memcpy(old_agi, agi, sizeof(*old_agi));
860 memset(agi, 0, BBTOB(agi_bp->b_length));
861 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
862 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
863 agi->agi_seqno = cpu_to_be32(pag->pag_agno);
864 agi->agi_length = cpu_to_be32(pag->block_count);
865 agi->agi_newino = cpu_to_be32(NULLAGINO);
866 agi->agi_dirino = cpu_to_be32(NULLAGINO);
868 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
870 /* We don't know how to fix the unlinked list yet. */
871 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
872 sizeof(agi->agi_unlinked));
874 /* Mark the incore AGF data stale until we're done fixing things. */
875 ASSERT(xfs_perag_initialised_agi(pag));
876 clear_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
879 /* Set btree root information in an AGI. */
882 struct xfs_scrub *sc,
884 struct xrep_find_ag_btree *fab)
886 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
887 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
889 if (xfs_has_finobt(sc->mp)) {
890 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
891 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
895 /* Update the AGI counters. */
897 xrep_agi_calc_from_btrees(
898 struct xfs_scrub *sc,
899 struct xfs_buf *agi_bp)
901 struct xfs_btree_cur *cur;
902 struct xfs_agi *agi = agi_bp->b_addr;
903 struct xfs_mount *mp = sc->mp;
905 xfs_agino_t freecount;
908 cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, agi_bp, XFS_BTNUM_INO);
909 error = xfs_ialloc_count_inodes(cur, &count, &freecount);
912 if (xfs_has_inobtcounts(mp)) {
913 xfs_agblock_t blocks;
915 error = xfs_btree_count_blocks(cur, &blocks);
918 agi->agi_iblocks = cpu_to_be32(blocks);
920 xfs_btree_del_cursor(cur, error);
922 agi->agi_count = cpu_to_be32(count);
923 agi->agi_freecount = cpu_to_be32(freecount);
925 if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
926 xfs_agblock_t blocks;
928 cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, agi_bp,
930 error = xfs_btree_count_blocks(cur, &blocks);
933 xfs_btree_del_cursor(cur, error);
934 agi->agi_fblocks = cpu_to_be32(blocks);
939 xfs_btree_del_cursor(cur, error);
943 /* Trigger reinitialization of the in-core data. */
946 struct xfs_scrub *sc,
947 struct xfs_buf *agi_bp)
949 struct xfs_perag *pag;
950 struct xfs_agi *agi = agi_bp->b_addr;
952 /* Trigger inode count recalculation */
953 xfs_force_summary_recalc(sc->mp);
955 /* Write this to disk. */
956 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
957 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
959 /* Now reinitialize the in-core counters if necessary. */
961 pag->pagi_count = be32_to_cpu(agi->agi_count);
962 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
963 set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
968 /* Repair the AGI. */
971 struct xfs_scrub *sc)
973 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = {
975 .rmap_owner = XFS_RMAP_OWN_INOBT,
976 .buf_ops = &xfs_inobt_buf_ops,
977 .maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
979 [XREP_AGI_FINOBT] = {
980 .rmap_owner = XFS_RMAP_OWN_INOBT,
981 .buf_ops = &xfs_finobt_buf_ops,
982 .maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
988 struct xfs_agi old_agi;
989 struct xfs_mount *mp = sc->mp;
990 struct xfs_buf *agi_bp;
994 /* We require the rmapbt to rebuild anything. */
995 if (!xfs_has_rmapbt(mp))
999 * Make sure we have the AGI buffer, as scrub might have decided it
1000 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
1002 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
1003 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
1005 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
1008 agi_bp->b_ops = &xfs_agi_buf_ops;
1009 agi = agi_bp->b_addr;
1011 /* Find the AGI btree roots. */
1012 error = xrep_agi_find_btrees(sc, fab);
1016 /* Last chance to abort before we start committing fixes. */
1017 if (xchk_should_terminate(sc, &error))
1020 /* Start rewriting the header and implant the btrees we found. */
1021 xrep_agi_init_header(sc, agi_bp, &old_agi);
1022 xrep_agi_set_roots(sc, agi, fab);
1023 error = xrep_agi_calc_from_btrees(sc, agi_bp);
1027 /* Reinitialize in-core state. */
1028 return xrep_agi_commit_new(sc, agi_bp);
1031 /* Mark the incore AGI state stale and revert the AGI. */
1032 clear_bit(XFS_AGSTATE_AGI_INIT, &sc->sa.pag->pag_opstate);
1033 memcpy(agi, &old_agi, sizeof(old_agi));