1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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"
32 /* Repair the superblock. */
37 struct xfs_mount *mp = sc->mp;
42 /* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
43 agno = sc->sm->sm_agno;
47 error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
51 /* Copy AG 0's superblock to this one. */
52 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
53 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
55 /* Write this to disk. */
56 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
57 xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
63 struct xrep_agf_allocbt {
65 xfs_agblock_t freeblks;
66 xfs_agblock_t longest;
69 /* Record free space shape information. */
71 xrep_agf_walk_allocbt(
72 struct xfs_btree_cur *cur,
73 const struct xfs_alloc_rec_incore *rec,
76 struct xrep_agf_allocbt *raa = priv;
79 if (xchk_should_terminate(raa->sc, &error))
82 raa->freeblks += rec->ar_blockcount;
83 if (rec->ar_blockcount > raa->longest)
84 raa->longest = rec->ar_blockcount;
88 /* Does this AGFL block look sane? */
90 xrep_agf_check_agfl_block(
95 struct xfs_scrub *sc = priv;
97 if (!xfs_verify_agbno(mp, sc->sa.pag->pag_agno, agbno))
103 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
104 * XFS_BTNUM_ names here to avoid creating a sparse array.
115 /* Check a btree root candidate. */
117 xrep_check_btree_root(
118 struct xfs_scrub *sc,
119 struct xrep_find_ag_btree *fab)
121 struct xfs_mount *mp = sc->mp;
122 xfs_agnumber_t agno = sc->sm->sm_agno;
124 return xfs_verify_agbno(mp, agno, fab->root) &&
125 fab->height <= XFS_BTREE_MAXLEVELS;
129 * Given the btree roots described by *fab, find the roots, check them for
130 * sanity, and pass the root data back out via *fab.
132 * This is /also/ a chicken and egg problem because we have to use the rmapbt
133 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no
134 * idea if the btrees make any sense. If we hit obvious corruptions in those
135 * btrees we'll bail out.
138 xrep_agf_find_btrees(
139 struct xfs_scrub *sc,
140 struct xfs_buf *agf_bp,
141 struct xrep_find_ag_btree *fab,
142 struct xfs_buf *agfl_bp)
144 struct xfs_agf *old_agf = agf_bp->b_addr;
147 /* Go find the root data. */
148 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
152 /* We must find the bnobt, cntbt, and rmapbt roots. */
153 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
154 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
155 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
156 return -EFSCORRUPTED;
159 * We relied on the rmapbt to reconstruct the AGF. If we get a
160 * different root then something's seriously wrong.
162 if (fab[XREP_AGF_RMAPBT].root !=
163 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
164 return -EFSCORRUPTED;
166 /* We must find the refcountbt root if that feature is enabled. */
167 if (xfs_has_reflink(sc->mp) &&
168 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
169 return -EFSCORRUPTED;
175 * Reinitialize the AGF header, making an in-core copy of the old contents so
176 * that we know which in-core state needs to be reinitialized.
179 xrep_agf_init_header(
180 struct xfs_scrub *sc,
181 struct xfs_buf *agf_bp,
182 struct xfs_agf *old_agf)
184 struct xfs_mount *mp = sc->mp;
185 struct xfs_agf *agf = agf_bp->b_addr;
187 memcpy(old_agf, agf, sizeof(*old_agf));
188 memset(agf, 0, BBTOB(agf_bp->b_length));
189 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
190 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
191 agf->agf_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
192 agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp,
193 sc->sa.pag->pag_agno));
194 agf->agf_flfirst = old_agf->agf_flfirst;
195 agf->agf_fllast = old_agf->agf_fllast;
196 agf->agf_flcount = old_agf->agf_flcount;
198 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
200 /* Mark the incore AGF data stale until we're done fixing things. */
201 ASSERT(sc->sa.pag->pagf_init);
202 sc->sa.pag->pagf_init = 0;
205 /* Set btree root information in an AGF. */
208 struct xfs_scrub *sc,
210 struct xrep_find_ag_btree *fab)
212 agf->agf_roots[XFS_BTNUM_BNOi] =
213 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
214 agf->agf_levels[XFS_BTNUM_BNOi] =
215 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
217 agf->agf_roots[XFS_BTNUM_CNTi] =
218 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
219 agf->agf_levels[XFS_BTNUM_CNTi] =
220 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
222 agf->agf_roots[XFS_BTNUM_RMAPi] =
223 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
224 agf->agf_levels[XFS_BTNUM_RMAPi] =
225 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
227 if (xfs_has_reflink(sc->mp)) {
228 agf->agf_refcount_root =
229 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
230 agf->agf_refcount_level =
231 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
235 /* Update all AGF fields which derive from btree contents. */
237 xrep_agf_calc_from_btrees(
238 struct xfs_scrub *sc,
239 struct xfs_buf *agf_bp)
241 struct xrep_agf_allocbt raa = { .sc = sc };
242 struct xfs_btree_cur *cur = NULL;
243 struct xfs_agf *agf = agf_bp->b_addr;
244 struct xfs_mount *mp = sc->mp;
245 xfs_agblock_t btreeblks;
246 xfs_agblock_t blocks;
249 /* Update the AGF counters from the bnobt. */
250 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
251 sc->sa.pag, XFS_BTNUM_BNO);
252 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
255 error = xfs_btree_count_blocks(cur, &blocks);
258 xfs_btree_del_cursor(cur, error);
259 btreeblks = blocks - 1;
260 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
261 agf->agf_longest = cpu_to_be32(raa.longest);
263 /* Update the AGF counters from the cntbt. */
264 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
265 sc->sa.pag, XFS_BTNUM_CNT);
266 error = xfs_btree_count_blocks(cur, &blocks);
269 xfs_btree_del_cursor(cur, error);
270 btreeblks += blocks - 1;
272 /* Update the AGF counters from the rmapbt. */
273 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
274 error = xfs_btree_count_blocks(cur, &blocks);
277 xfs_btree_del_cursor(cur, error);
278 agf->agf_rmap_blocks = cpu_to_be32(blocks);
279 btreeblks += blocks - 1;
281 agf->agf_btreeblks = cpu_to_be32(btreeblks);
283 /* Update the AGF counters from the refcountbt. */
284 if (xfs_has_reflink(mp)) {
285 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
287 error = xfs_btree_count_blocks(cur, &blocks);
290 xfs_btree_del_cursor(cur, error);
291 agf->agf_refcount_blocks = cpu_to_be32(blocks);
296 xfs_btree_del_cursor(cur, error);
300 /* Commit the new AGF and reinitialize the incore state. */
303 struct xfs_scrub *sc,
304 struct xfs_buf *agf_bp)
306 struct xfs_perag *pag;
307 struct xfs_agf *agf = agf_bp->b_addr;
309 /* Trigger fdblocks recalculation */
310 xfs_force_summary_recalc(sc->mp);
312 /* Write this to disk. */
313 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
314 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
316 /* Now reinitialize the in-core counters we changed. */
318 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
319 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
320 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
321 pag->pagf_levels[XFS_BTNUM_BNOi] =
322 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
323 pag->pagf_levels[XFS_BTNUM_CNTi] =
324 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
325 pag->pagf_levels[XFS_BTNUM_RMAPi] =
326 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
327 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
333 /* Repair the AGF. v5 filesystems only. */
336 struct xfs_scrub *sc)
338 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = {
340 .rmap_owner = XFS_RMAP_OWN_AG,
341 .buf_ops = &xfs_bnobt_buf_ops,
344 .rmap_owner = XFS_RMAP_OWN_AG,
345 .buf_ops = &xfs_cntbt_buf_ops,
347 [XREP_AGF_RMAPBT] = {
348 .rmap_owner = XFS_RMAP_OWN_AG,
349 .buf_ops = &xfs_rmapbt_buf_ops,
351 [XREP_AGF_REFCOUNTBT] = {
352 .rmap_owner = XFS_RMAP_OWN_REFC,
353 .buf_ops = &xfs_refcountbt_buf_ops,
359 struct xfs_agf old_agf;
360 struct xfs_mount *mp = sc->mp;
361 struct xfs_buf *agf_bp;
362 struct xfs_buf *agfl_bp;
366 /* We require the rmapbt to rebuild anything. */
367 if (!xfs_has_rmapbt(mp))
371 * Make sure we have the AGF buffer, as scrub might have decided it
372 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
374 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
375 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
377 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
380 agf_bp->b_ops = &xfs_agf_buf_ops;
381 agf = agf_bp->b_addr;
384 * Load the AGFL so that we can screen out OWN_AG blocks that are on
385 * the AGFL now; these blocks might have once been part of the
386 * bno/cnt/rmap btrees but are not now. This is a chicken and egg
387 * problem: the AGF is corrupt, so we have to trust the AGFL contents
388 * because we can't do any serious cross-referencing with any of the
389 * btrees rooted in the AGF. If the AGFL contents are obviously bad
390 * then we'll bail out.
392 error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.pag->pag_agno, &agfl_bp);
397 * Spot-check the AGFL blocks; if they're obviously corrupt then
398 * there's nothing we can do but bail out.
400 error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
401 xrep_agf_check_agfl_block, sc);
406 * Find the AGF btree roots. This is also a chicken-and-egg situation;
407 * see the function for more details.
409 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
413 /* Start rewriting the header and implant the btrees we found. */
414 xrep_agf_init_header(sc, agf_bp, &old_agf);
415 xrep_agf_set_roots(sc, agf, fab);
416 error = xrep_agf_calc_from_btrees(sc, agf_bp);
420 /* Commit the changes and reinitialize incore state. */
421 return xrep_agf_commit_new(sc, agf_bp);
424 /* Mark the incore AGF state stale and revert the AGF. */
425 sc->sa.pag->pagf_init = 0;
426 memcpy(agf, &old_agf, sizeof(old_agf));
433 /* Bitmap of other OWN_AG metadata blocks. */
434 struct xbitmap agmetablocks;
436 /* Bitmap of free space. */
437 struct xbitmap *freesp;
439 struct xfs_scrub *sc;
442 /* Record all OWN_AG (free space btree) information from the rmap data. */
445 struct xfs_btree_cur *cur,
446 const struct xfs_rmap_irec *rec,
449 struct xrep_agfl *ra = priv;
453 if (xchk_should_terminate(ra->sc, &error))
456 /* Record all the OWN_AG blocks. */
457 if (rec->rm_owner == XFS_RMAP_OWN_AG) {
458 fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.pag->pag_agno,
460 error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
465 return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
469 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
470 * which blocks belong to the AGFL.
472 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
473 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
474 * rmapbt). These are the old AGFL blocks, so return that list and the number
475 * of blocks we're actually going to put back on the AGFL.
478 xrep_agfl_collect_blocks(
479 struct xfs_scrub *sc,
480 struct xfs_buf *agf_bp,
481 struct xbitmap *agfl_extents,
482 xfs_agblock_t *flcount)
485 struct xfs_mount *mp = sc->mp;
486 struct xfs_btree_cur *cur;
490 ra.freesp = agfl_extents;
491 xbitmap_init(&ra.agmetablocks);
493 /* Find all space used by the free space btrees & rmapbt. */
494 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
495 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
498 xfs_btree_del_cursor(cur, error);
500 /* Find all blocks currently being used by the bnobt. */
501 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
502 sc->sa.pag, XFS_BTNUM_BNO);
503 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
506 xfs_btree_del_cursor(cur, error);
508 /* Find all blocks currently being used by the cntbt. */
509 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
510 sc->sa.pag, XFS_BTNUM_CNT);
511 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
515 xfs_btree_del_cursor(cur, error);
518 * Drop the freesp meta blocks that are in use by btrees.
519 * The remaining blocks /should/ be AGFL blocks.
521 error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
522 xbitmap_destroy(&ra.agmetablocks);
527 * Calculate the new AGFL size. If we found more blocks than fit in
528 * the AGFL we'll free them later.
530 *flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
535 xbitmap_destroy(&ra.agmetablocks);
536 xfs_btree_del_cursor(cur, error);
540 /* Update the AGF and reset the in-core state. */
542 xrep_agfl_update_agf(
543 struct xfs_scrub *sc,
544 struct xfs_buf *agf_bp,
545 xfs_agblock_t flcount)
547 struct xfs_agf *agf = agf_bp->b_addr;
549 ASSERT(flcount <= xfs_agfl_size(sc->mp));
551 /* Trigger fdblocks recalculation */
552 xfs_force_summary_recalc(sc->mp);
554 /* Update the AGF counters. */
555 if (sc->sa.pag->pagf_init)
556 sc->sa.pag->pagf_flcount = flcount;
557 agf->agf_flfirst = cpu_to_be32(0);
558 agf->agf_flcount = cpu_to_be32(flcount);
559 agf->agf_fllast = cpu_to_be32(flcount - 1);
561 xfs_alloc_log_agf(sc->tp, agf_bp,
562 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
565 /* Write out a totally new AGFL. */
567 xrep_agfl_init_header(
568 struct xfs_scrub *sc,
569 struct xfs_buf *agfl_bp,
570 struct xbitmap *agfl_extents,
571 xfs_agblock_t flcount)
573 struct xfs_mount *mp = sc->mp;
575 struct xbitmap_range *br;
576 struct xbitmap_range *n;
577 struct xfs_agfl *agfl;
581 ASSERT(flcount <= xfs_agfl_size(mp));
584 * Start rewriting the header by setting the bno[] array to
585 * NULLAGBLOCK, then setting AGFL header fields.
587 agfl = XFS_BUF_TO_AGFL(agfl_bp);
588 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
589 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
590 agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
591 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
594 * Fill the AGFL with the remaining blocks. If agfl_extents has more
595 * blocks than fit in the AGFL, they will be freed in a subsequent
599 agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
600 for_each_xbitmap_extent(br, n, agfl_extents) {
601 agbno = XFS_FSB_TO_AGBNO(mp, br->start);
603 trace_xrep_agfl_insert(mp, sc->sa.pag->pag_agno, agbno,
606 while (br->len > 0 && fl_off < flcount) {
607 agfl_bno[fl_off] = cpu_to_be32(agbno);
612 * We've now used br->start by putting it in the AGFL,
613 * so bump br so that we don't reap the block later.
625 /* Write new AGFL to disk. */
626 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
627 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
630 /* Repair the AGFL. */
633 struct xfs_scrub *sc)
635 struct xbitmap agfl_extents;
636 struct xfs_mount *mp = sc->mp;
637 struct xfs_buf *agf_bp;
638 struct xfs_buf *agfl_bp;
639 xfs_agblock_t flcount;
642 /* We require the rmapbt to rebuild anything. */
643 if (!xfs_has_rmapbt(mp))
646 xbitmap_init(&agfl_extents);
649 * Read the AGF so that we can query the rmapbt. We hope that there's
650 * nothing wrong with the AGF, but all the AG header repair functions
651 * have this chicken-and-egg problem.
653 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.pag->pag_agno, 0,
659 * Make sure we have the AGFL buffer, as scrub might have decided it
660 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
662 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
663 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
665 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
668 agfl_bp->b_ops = &xfs_agfl_buf_ops;
670 /* Gather all the extents we're going to put on the new AGFL. */
671 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
676 * Update AGF and AGFL. We reset the global free block counter when
677 * we adjust the AGF flcount (which can fail) so avoid updating any
678 * buffers until we know that part works.
680 xrep_agfl_update_agf(sc, agf_bp, flcount);
681 xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
684 * Ok, the AGFL should be ready to go now. Roll the transaction to
685 * make the new AGFL permanent before we start using it to return
686 * freespace overflow to the freespace btrees.
688 sc->sa.agf_bp = agf_bp;
689 sc->sa.agfl_bp = agfl_bp;
690 error = xrep_roll_ag_trans(sc);
694 /* Dump any AGFL overflow. */
695 error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
698 xbitmap_destroy(&agfl_extents);
705 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
706 * XFS_BTNUM_ names here to avoid creating a sparse array.
716 * Given the inode btree roots described by *fab, find the roots, check them
717 * for sanity, and pass the root data back out via *fab.
720 xrep_agi_find_btrees(
721 struct xfs_scrub *sc,
722 struct xrep_find_ag_btree *fab)
724 struct xfs_buf *agf_bp;
725 struct xfs_mount *mp = sc->mp;
729 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.pag->pag_agno, 0,
734 /* Find the btree roots. */
735 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
739 /* We must find the inobt root. */
740 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
741 return -EFSCORRUPTED;
743 /* We must find the finobt root if that feature is enabled. */
744 if (xfs_has_finobt(mp) &&
745 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
746 return -EFSCORRUPTED;
752 * Reinitialize the AGI header, making an in-core copy of the old contents so
753 * that we know which in-core state needs to be reinitialized.
756 xrep_agi_init_header(
757 struct xfs_scrub *sc,
758 struct xfs_buf *agi_bp,
759 struct xfs_agi *old_agi)
761 struct xfs_agi *agi = agi_bp->b_addr;
762 struct xfs_mount *mp = sc->mp;
764 memcpy(old_agi, agi, sizeof(*old_agi));
765 memset(agi, 0, BBTOB(agi_bp->b_length));
766 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
767 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
768 agi->agi_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
769 agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp,
770 sc->sa.pag->pag_agno));
771 agi->agi_newino = cpu_to_be32(NULLAGINO);
772 agi->agi_dirino = cpu_to_be32(NULLAGINO);
774 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
776 /* We don't know how to fix the unlinked list yet. */
777 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
778 sizeof(agi->agi_unlinked));
780 /* Mark the incore AGF data stale until we're done fixing things. */
781 ASSERT(sc->sa.pag->pagi_init);
782 sc->sa.pag->pagi_init = 0;
785 /* Set btree root information in an AGI. */
788 struct xfs_scrub *sc,
790 struct xrep_find_ag_btree *fab)
792 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
793 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
795 if (xfs_has_finobt(sc->mp)) {
796 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
797 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
801 /* Update the AGI counters. */
803 xrep_agi_calc_from_btrees(
804 struct xfs_scrub *sc,
805 struct xfs_buf *agi_bp)
807 struct xfs_btree_cur *cur;
808 struct xfs_agi *agi = agi_bp->b_addr;
809 struct xfs_mount *mp = sc->mp;
811 xfs_agino_t freecount;
814 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
815 sc->sa.pag, XFS_BTNUM_INO);
816 error = xfs_ialloc_count_inodes(cur, &count, &freecount);
819 if (xfs_has_inobtcounts(mp)) {
820 xfs_agblock_t blocks;
822 error = xfs_btree_count_blocks(cur, &blocks);
825 agi->agi_iblocks = cpu_to_be32(blocks);
827 xfs_btree_del_cursor(cur, error);
829 agi->agi_count = cpu_to_be32(count);
830 agi->agi_freecount = cpu_to_be32(freecount);
832 if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
833 xfs_agblock_t blocks;
835 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
836 sc->sa.pag, XFS_BTNUM_FINO);
837 error = xfs_btree_count_blocks(cur, &blocks);
840 xfs_btree_del_cursor(cur, error);
841 agi->agi_fblocks = cpu_to_be32(blocks);
846 xfs_btree_del_cursor(cur, error);
850 /* Trigger reinitialization of the in-core data. */
853 struct xfs_scrub *sc,
854 struct xfs_buf *agi_bp)
856 struct xfs_perag *pag;
857 struct xfs_agi *agi = agi_bp->b_addr;
859 /* Trigger inode count recalculation */
860 xfs_force_summary_recalc(sc->mp);
862 /* Write this to disk. */
863 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
864 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
866 /* Now reinitialize the in-core counters if necessary. */
868 pag->pagi_count = be32_to_cpu(agi->agi_count);
869 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
875 /* Repair the AGI. */
878 struct xfs_scrub *sc)
880 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = {
882 .rmap_owner = XFS_RMAP_OWN_INOBT,
883 .buf_ops = &xfs_inobt_buf_ops,
885 [XREP_AGI_FINOBT] = {
886 .rmap_owner = XFS_RMAP_OWN_INOBT,
887 .buf_ops = &xfs_finobt_buf_ops,
893 struct xfs_agi old_agi;
894 struct xfs_mount *mp = sc->mp;
895 struct xfs_buf *agi_bp;
899 /* We require the rmapbt to rebuild anything. */
900 if (!xfs_has_rmapbt(mp))
904 * Make sure we have the AGI buffer, as scrub might have decided it
905 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
907 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
908 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
910 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
913 agi_bp->b_ops = &xfs_agi_buf_ops;
914 agi = agi_bp->b_addr;
916 /* Find the AGI btree roots. */
917 error = xrep_agi_find_btrees(sc, fab);
921 /* Start rewriting the header and implant the btrees we found. */
922 xrep_agi_init_header(sc, agi_bp, &old_agi);
923 xrep_agi_set_roots(sc, agi, fab);
924 error = xrep_agi_calc_from_btrees(sc, agi_bp);
928 /* Reinitialize in-core state. */
929 return xrep_agi_commit_new(sc, agi_bp);
932 /* Mark the incore AGI state stale and revert the AGI. */
933 sc->sa.pag->pagi_init = 0;
934 memcpy(agi, &old_agi, sizeof(old_agi));