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 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.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_sb_version_hasreflink(&sc->mp->m_sb) &&
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.agno);
192 agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
193 agf->agf_flfirst = old_agf->agf_flfirst;
194 agf->agf_fllast = old_agf->agf_fllast;
195 agf->agf_flcount = old_agf->agf_flcount;
196 if (xfs_sb_version_hascrc(&mp->m_sb))
197 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
199 /* Mark the incore AGF data stale until we're done fixing things. */
200 ASSERT(sc->sa.pag->pagf_init);
201 sc->sa.pag->pagf_init = 0;
204 /* Set btree root information in an AGF. */
207 struct xfs_scrub *sc,
209 struct xrep_find_ag_btree *fab)
211 agf->agf_roots[XFS_BTNUM_BNOi] =
212 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
213 agf->agf_levels[XFS_BTNUM_BNOi] =
214 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
216 agf->agf_roots[XFS_BTNUM_CNTi] =
217 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
218 agf->agf_levels[XFS_BTNUM_CNTi] =
219 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
221 agf->agf_roots[XFS_BTNUM_RMAPi] =
222 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
223 agf->agf_levels[XFS_BTNUM_RMAPi] =
224 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
226 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
227 agf->agf_refcount_root =
228 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
229 agf->agf_refcount_level =
230 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
234 /* Update all AGF fields which derive from btree contents. */
236 xrep_agf_calc_from_btrees(
237 struct xfs_scrub *sc,
238 struct xfs_buf *agf_bp)
240 struct xrep_agf_allocbt raa = { .sc = sc };
241 struct xfs_btree_cur *cur = NULL;
242 struct xfs_agf *agf = agf_bp->b_addr;
243 struct xfs_mount *mp = sc->mp;
244 xfs_agblock_t btreeblks;
245 xfs_agblock_t blocks;
248 /* Update the AGF counters from the bnobt. */
249 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
250 sc->sa.pag, XFS_BTNUM_BNO);
251 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
254 error = xfs_btree_count_blocks(cur, &blocks);
257 xfs_btree_del_cursor(cur, error);
258 btreeblks = blocks - 1;
259 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
260 agf->agf_longest = cpu_to_be32(raa.longest);
262 /* Update the AGF counters from the cntbt. */
263 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
264 sc->sa.pag, XFS_BTNUM_CNT);
265 error = xfs_btree_count_blocks(cur, &blocks);
268 xfs_btree_del_cursor(cur, error);
269 btreeblks += blocks - 1;
271 /* Update the AGF counters from the rmapbt. */
272 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
273 error = xfs_btree_count_blocks(cur, &blocks);
276 xfs_btree_del_cursor(cur, error);
277 agf->agf_rmap_blocks = cpu_to_be32(blocks);
278 btreeblks += blocks - 1;
280 agf->agf_btreeblks = cpu_to_be32(btreeblks);
282 /* Update the AGF counters from the refcountbt. */
283 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
284 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
286 error = xfs_btree_count_blocks(cur, &blocks);
289 xfs_btree_del_cursor(cur, error);
290 agf->agf_refcount_blocks = cpu_to_be32(blocks);
295 xfs_btree_del_cursor(cur, error);
299 /* Commit the new AGF and reinitialize the incore state. */
302 struct xfs_scrub *sc,
303 struct xfs_buf *agf_bp)
305 struct xfs_perag *pag;
306 struct xfs_agf *agf = agf_bp->b_addr;
308 /* Trigger fdblocks recalculation */
309 xfs_force_summary_recalc(sc->mp);
311 /* Write this to disk. */
312 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
313 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
315 /* Now reinitialize the in-core counters we changed. */
317 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
318 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
319 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
320 pag->pagf_levels[XFS_BTNUM_BNOi] =
321 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
322 pag->pagf_levels[XFS_BTNUM_CNTi] =
323 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
324 pag->pagf_levels[XFS_BTNUM_RMAPi] =
325 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
326 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
332 /* Repair the AGF. v5 filesystems only. */
335 struct xfs_scrub *sc)
337 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = {
339 .rmap_owner = XFS_RMAP_OWN_AG,
340 .buf_ops = &xfs_bnobt_buf_ops,
343 .rmap_owner = XFS_RMAP_OWN_AG,
344 .buf_ops = &xfs_cntbt_buf_ops,
346 [XREP_AGF_RMAPBT] = {
347 .rmap_owner = XFS_RMAP_OWN_AG,
348 .buf_ops = &xfs_rmapbt_buf_ops,
350 [XREP_AGF_REFCOUNTBT] = {
351 .rmap_owner = XFS_RMAP_OWN_REFC,
352 .buf_ops = &xfs_refcountbt_buf_ops,
358 struct xfs_agf old_agf;
359 struct xfs_mount *mp = sc->mp;
360 struct xfs_buf *agf_bp;
361 struct xfs_buf *agfl_bp;
365 /* We require the rmapbt to rebuild anything. */
366 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
369 xchk_perag_get(sc->mp, &sc->sa);
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.agno, XFS_AGF_DADDR(mp)),
376 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
379 agf_bp->b_ops = &xfs_agf_buf_ops;
380 agf = agf_bp->b_addr;
383 * Load the AGFL so that we can screen out OWN_AG blocks that are on
384 * the AGFL now; these blocks might have once been part of the
385 * bno/cnt/rmap btrees but are not now. This is a chicken and egg
386 * problem: the AGF is corrupt, so we have to trust the AGFL contents
387 * because we can't do any serious cross-referencing with any of the
388 * btrees rooted in the AGF. If the AGFL contents are obviously bad
389 * then we'll bail out.
391 error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.agno, &agfl_bp);
396 * Spot-check the AGFL blocks; if they're obviously corrupt then
397 * there's nothing we can do but bail out.
399 error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
400 xrep_agf_check_agfl_block, sc);
405 * Find the AGF btree roots. This is also a chicken-and-egg situation;
406 * see the function for more details.
408 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
412 /* Start rewriting the header and implant the btrees we found. */
413 xrep_agf_init_header(sc, agf_bp, &old_agf);
414 xrep_agf_set_roots(sc, agf, fab);
415 error = xrep_agf_calc_from_btrees(sc, agf_bp);
419 /* Commit the changes and reinitialize incore state. */
420 return xrep_agf_commit_new(sc, agf_bp);
423 /* Mark the incore AGF state stale and revert the AGF. */
424 sc->sa.pag->pagf_init = 0;
425 memcpy(agf, &old_agf, sizeof(old_agf));
432 /* Bitmap of other OWN_AG metadata blocks. */
433 struct xbitmap agmetablocks;
435 /* Bitmap of free space. */
436 struct xbitmap *freesp;
438 struct xfs_scrub *sc;
441 /* Record all OWN_AG (free space btree) information from the rmap data. */
444 struct xfs_btree_cur *cur,
445 struct xfs_rmap_irec *rec,
448 struct xrep_agfl *ra = priv;
452 if (xchk_should_terminate(ra->sc, &error))
455 /* Record all the OWN_AG blocks. */
456 if (rec->rm_owner == XFS_RMAP_OWN_AG) {
457 fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.agno,
459 error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
464 return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
468 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
469 * which blocks belong to the AGFL.
471 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
472 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
473 * rmapbt). These are the old AGFL blocks, so return that list and the number
474 * of blocks we're actually going to put back on the AGFL.
477 xrep_agfl_collect_blocks(
478 struct xfs_scrub *sc,
479 struct xfs_buf *agf_bp,
480 struct xbitmap *agfl_extents,
481 xfs_agblock_t *flcount)
484 struct xfs_mount *mp = sc->mp;
485 struct xfs_btree_cur *cur;
489 ra.freesp = agfl_extents;
490 xbitmap_init(&ra.agmetablocks);
492 /* Find all space used by the free space btrees & rmapbt. */
493 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
494 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
497 xfs_btree_del_cursor(cur, error);
499 /* Find all blocks currently being used by the bnobt. */
500 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
501 sc->sa.pag, XFS_BTNUM_BNO);
502 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
505 xfs_btree_del_cursor(cur, error);
507 /* Find all blocks currently being used by the cntbt. */
508 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
509 sc->sa.pag, XFS_BTNUM_CNT);
510 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
514 xfs_btree_del_cursor(cur, error);
517 * Drop the freesp meta blocks that are in use by btrees.
518 * The remaining blocks /should/ be AGFL blocks.
520 error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
521 xbitmap_destroy(&ra.agmetablocks);
526 * Calculate the new AGFL size. If we found more blocks than fit in
527 * the AGFL we'll free them later.
529 *flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
534 xbitmap_destroy(&ra.agmetablocks);
535 xfs_btree_del_cursor(cur, error);
539 /* Update the AGF and reset the in-core state. */
541 xrep_agfl_update_agf(
542 struct xfs_scrub *sc,
543 struct xfs_buf *agf_bp,
544 xfs_agblock_t flcount)
546 struct xfs_agf *agf = agf_bp->b_addr;
548 ASSERT(flcount <= xfs_agfl_size(sc->mp));
550 /* Trigger fdblocks recalculation */
551 xfs_force_summary_recalc(sc->mp);
553 /* Update the AGF counters. */
554 if (sc->sa.pag->pagf_init)
555 sc->sa.pag->pagf_flcount = flcount;
556 agf->agf_flfirst = cpu_to_be32(0);
557 agf->agf_flcount = cpu_to_be32(flcount);
558 agf->agf_fllast = cpu_to_be32(flcount - 1);
560 xfs_alloc_log_agf(sc->tp, agf_bp,
561 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
564 /* Write out a totally new AGFL. */
566 xrep_agfl_init_header(
567 struct xfs_scrub *sc,
568 struct xfs_buf *agfl_bp,
569 struct xbitmap *agfl_extents,
570 xfs_agblock_t flcount)
572 struct xfs_mount *mp = sc->mp;
574 struct xbitmap_range *br;
575 struct xbitmap_range *n;
576 struct xfs_agfl *agfl;
580 ASSERT(flcount <= xfs_agfl_size(mp));
583 * Start rewriting the header by setting the bno[] array to
584 * NULLAGBLOCK, then setting AGFL header fields.
586 agfl = XFS_BUF_TO_AGFL(agfl_bp);
587 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
588 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
589 agfl->agfl_seqno = cpu_to_be32(sc->sa.agno);
590 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
593 * Fill the AGFL with the remaining blocks. If agfl_extents has more
594 * blocks than fit in the AGFL, they will be freed in a subsequent
598 agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
599 for_each_xbitmap_extent(br, n, agfl_extents) {
600 agbno = XFS_FSB_TO_AGBNO(mp, br->start);
602 trace_xrep_agfl_insert(mp, sc->sa.agno, agbno, br->len);
604 while (br->len > 0 && fl_off < flcount) {
605 agfl_bno[fl_off] = cpu_to_be32(agbno);
610 * We've now used br->start by putting it in the AGFL,
611 * so bump br so that we don't reap the block later.
623 /* Write new AGFL to disk. */
624 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
625 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
628 /* Repair the AGFL. */
631 struct xfs_scrub *sc)
633 struct xbitmap agfl_extents;
634 struct xfs_mount *mp = sc->mp;
635 struct xfs_buf *agf_bp;
636 struct xfs_buf *agfl_bp;
637 xfs_agblock_t flcount;
640 /* We require the rmapbt to rebuild anything. */
641 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
644 xchk_perag_get(sc->mp, &sc->sa);
645 xbitmap_init(&agfl_extents);
648 * Read the AGF so that we can query the rmapbt. We hope that there's
649 * nothing wrong with the AGF, but all the AG header repair functions
650 * have this chicken-and-egg problem.
652 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
657 * Make sure we have the AGFL buffer, as scrub might have decided it
658 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
660 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
661 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGFL_DADDR(mp)),
662 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
665 agfl_bp->b_ops = &xfs_agfl_buf_ops;
667 /* Gather all the extents we're going to put on the new AGFL. */
668 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
673 * Update AGF and AGFL. We reset the global free block counter when
674 * we adjust the AGF flcount (which can fail) so avoid updating any
675 * buffers until we know that part works.
677 xrep_agfl_update_agf(sc, agf_bp, flcount);
678 xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
681 * Ok, the AGFL should be ready to go now. Roll the transaction to
682 * make the new AGFL permanent before we start using it to return
683 * freespace overflow to the freespace btrees.
685 sc->sa.agf_bp = agf_bp;
686 sc->sa.agfl_bp = agfl_bp;
687 error = xrep_roll_ag_trans(sc);
691 /* Dump any AGFL overflow. */
692 error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
695 xbitmap_destroy(&agfl_extents);
702 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
703 * XFS_BTNUM_ names here to avoid creating a sparse array.
713 * Given the inode btree roots described by *fab, find the roots, check them
714 * for sanity, and pass the root data back out via *fab.
717 xrep_agi_find_btrees(
718 struct xfs_scrub *sc,
719 struct xrep_find_ag_btree *fab)
721 struct xfs_buf *agf_bp;
722 struct xfs_mount *mp = sc->mp;
726 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
730 /* Find the btree roots. */
731 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
735 /* We must find the inobt root. */
736 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
737 return -EFSCORRUPTED;
739 /* We must find the finobt root if that feature is enabled. */
740 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
741 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
742 return -EFSCORRUPTED;
748 * Reinitialize the AGI header, making an in-core copy of the old contents so
749 * that we know which in-core state needs to be reinitialized.
752 xrep_agi_init_header(
753 struct xfs_scrub *sc,
754 struct xfs_buf *agi_bp,
755 struct xfs_agi *old_agi)
757 struct xfs_agi *agi = agi_bp->b_addr;
758 struct xfs_mount *mp = sc->mp;
760 memcpy(old_agi, agi, sizeof(*old_agi));
761 memset(agi, 0, BBTOB(agi_bp->b_length));
762 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
763 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
764 agi->agi_seqno = cpu_to_be32(sc->sa.agno);
765 agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
766 agi->agi_newino = cpu_to_be32(NULLAGINO);
767 agi->agi_dirino = cpu_to_be32(NULLAGINO);
768 if (xfs_sb_version_hascrc(&mp->m_sb))
769 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
771 /* We don't know how to fix the unlinked list yet. */
772 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
773 sizeof(agi->agi_unlinked));
775 /* Mark the incore AGF data stale until we're done fixing things. */
776 ASSERT(sc->sa.pag->pagi_init);
777 sc->sa.pag->pagi_init = 0;
780 /* Set btree root information in an AGI. */
783 struct xfs_scrub *sc,
785 struct xrep_find_ag_btree *fab)
787 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
788 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
790 if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) {
791 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
792 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
796 /* Update the AGI counters. */
798 xrep_agi_calc_from_btrees(
799 struct xfs_scrub *sc,
800 struct xfs_buf *agi_bp)
802 struct xfs_btree_cur *cur;
803 struct xfs_agi *agi = agi_bp->b_addr;
804 struct xfs_mount *mp = sc->mp;
806 xfs_agino_t freecount;
809 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
810 sc->sa.pag, XFS_BTNUM_INO);
811 error = xfs_ialloc_count_inodes(cur, &count, &freecount);
814 if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
815 xfs_agblock_t blocks;
817 error = xfs_btree_count_blocks(cur, &blocks);
820 agi->agi_iblocks = cpu_to_be32(blocks);
822 xfs_btree_del_cursor(cur, error);
824 agi->agi_count = cpu_to_be32(count);
825 agi->agi_freecount = cpu_to_be32(freecount);
827 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
828 xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
829 xfs_agblock_t blocks;
831 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
832 sc->sa.pag, XFS_BTNUM_FINO);
833 error = xfs_btree_count_blocks(cur, &blocks);
836 xfs_btree_del_cursor(cur, error);
837 agi->agi_fblocks = cpu_to_be32(blocks);
842 xfs_btree_del_cursor(cur, error);
846 /* Trigger reinitialization of the in-core data. */
849 struct xfs_scrub *sc,
850 struct xfs_buf *agi_bp)
852 struct xfs_perag *pag;
853 struct xfs_agi *agi = agi_bp->b_addr;
855 /* Trigger inode count recalculation */
856 xfs_force_summary_recalc(sc->mp);
858 /* Write this to disk. */
859 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
860 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
862 /* Now reinitialize the in-core counters if necessary. */
864 pag->pagi_count = be32_to_cpu(agi->agi_count);
865 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
871 /* Repair the AGI. */
874 struct xfs_scrub *sc)
876 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = {
878 .rmap_owner = XFS_RMAP_OWN_INOBT,
879 .buf_ops = &xfs_inobt_buf_ops,
881 [XREP_AGI_FINOBT] = {
882 .rmap_owner = XFS_RMAP_OWN_INOBT,
883 .buf_ops = &xfs_finobt_buf_ops,
889 struct xfs_agi old_agi;
890 struct xfs_mount *mp = sc->mp;
891 struct xfs_buf *agi_bp;
895 /* We require the rmapbt to rebuild anything. */
896 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
899 xchk_perag_get(sc->mp, &sc->sa);
901 * Make sure we have the AGI buffer, as scrub might have decided it
902 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
904 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
905 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGI_DADDR(mp)),
906 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
909 agi_bp->b_ops = &xfs_agi_buf_ops;
910 agi = agi_bp->b_addr;
912 /* Find the AGI btree roots. */
913 error = xrep_agi_find_btrees(sc, fab);
917 /* Start rewriting the header and implant the btrees we found. */
918 xrep_agi_init_header(sc, agi_bp, &old_agi);
919 xrep_agi_set_roots(sc, agi, fab);
920 error = xrep_agi_calc_from_btrees(sc, agi_bp);
924 /* Reinitialize in-core state. */
925 return xrep_agi_commit_new(sc, agi_bp);
928 /* Mark the incore AGI state stale and revert the AGI. */
929 sc->sa.pag->pagi_init = 0;
930 memcpy(agi, &old_agi, sizeof(old_agi));