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);
56 * Don't write out a secondary super with NEEDSREPAIR or log incompat
57 * features set, since both are ignored when set on a secondary.
59 if (xfs_has_crc(mp)) {
60 struct xfs_dsb *sb = bp->b_addr;
62 sb->sb_features_incompat &=
63 ~cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
64 sb->sb_features_log_incompat = 0;
67 /* Write this to disk. */
68 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
69 xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
75 struct xrep_agf_allocbt {
77 xfs_agblock_t freeblks;
78 xfs_agblock_t longest;
81 /* Record free space shape information. */
83 xrep_agf_walk_allocbt(
84 struct xfs_btree_cur *cur,
85 const struct xfs_alloc_rec_incore *rec,
88 struct xrep_agf_allocbt *raa = priv;
91 if (xchk_should_terminate(raa->sc, &error))
94 raa->freeblks += rec->ar_blockcount;
95 if (rec->ar_blockcount > raa->longest)
96 raa->longest = rec->ar_blockcount;
100 /* Does this AGFL block look sane? */
102 xrep_agf_check_agfl_block(
103 struct xfs_mount *mp,
107 struct xfs_scrub *sc = priv;
109 if (!xfs_verify_agbno(sc->sa.pag, agbno))
110 return -EFSCORRUPTED;
115 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
116 * XFS_BTNUM_ names here to avoid creating a sparse array.
127 /* Check a btree root candidate. */
129 xrep_check_btree_root(
130 struct xfs_scrub *sc,
131 struct xrep_find_ag_btree *fab)
133 return xfs_verify_agbno(sc->sa.pag, fab->root) &&
134 fab->height <= fab->maxlevels;
138 * Given the btree roots described by *fab, find the roots, check them for
139 * sanity, and pass the root data back out via *fab.
141 * This is /also/ a chicken and egg problem because we have to use the rmapbt
142 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no
143 * idea if the btrees make any sense. If we hit obvious corruptions in those
144 * btrees we'll bail out.
147 xrep_agf_find_btrees(
148 struct xfs_scrub *sc,
149 struct xfs_buf *agf_bp,
150 struct xrep_find_ag_btree *fab,
151 struct xfs_buf *agfl_bp)
153 struct xfs_agf *old_agf = agf_bp->b_addr;
156 /* Go find the root data. */
157 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
161 /* We must find the bnobt, cntbt, and rmapbt roots. */
162 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
163 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
164 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
165 return -EFSCORRUPTED;
168 * We relied on the rmapbt to reconstruct the AGF. If we get a
169 * different root then something's seriously wrong.
171 if (fab[XREP_AGF_RMAPBT].root !=
172 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
173 return -EFSCORRUPTED;
175 /* We must find the refcountbt root if that feature is enabled. */
176 if (xfs_has_reflink(sc->mp) &&
177 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
178 return -EFSCORRUPTED;
184 * Reinitialize the AGF header, making an in-core copy of the old contents so
185 * that we know which in-core state needs to be reinitialized.
188 xrep_agf_init_header(
189 struct xfs_scrub *sc,
190 struct xfs_buf *agf_bp,
191 struct xfs_agf *old_agf)
193 struct xfs_mount *mp = sc->mp;
194 struct xfs_agf *agf = agf_bp->b_addr;
196 memcpy(old_agf, agf, sizeof(*old_agf));
197 memset(agf, 0, BBTOB(agf_bp->b_length));
198 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
199 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
200 agf->agf_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
201 agf->agf_length = cpu_to_be32(sc->sa.pag->block_count);
202 agf->agf_flfirst = old_agf->agf_flfirst;
203 agf->agf_fllast = old_agf->agf_fllast;
204 agf->agf_flcount = old_agf->agf_flcount;
206 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
208 /* Mark the incore AGF data stale until we're done fixing things. */
209 ASSERT(sc->sa.pag->pagf_init);
210 sc->sa.pag->pagf_init = 0;
213 /* Set btree root information in an AGF. */
216 struct xfs_scrub *sc,
218 struct xrep_find_ag_btree *fab)
220 agf->agf_roots[XFS_BTNUM_BNOi] =
221 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
222 agf->agf_levels[XFS_BTNUM_BNOi] =
223 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
225 agf->agf_roots[XFS_BTNUM_CNTi] =
226 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
227 agf->agf_levels[XFS_BTNUM_CNTi] =
228 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
230 agf->agf_roots[XFS_BTNUM_RMAPi] =
231 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
232 agf->agf_levels[XFS_BTNUM_RMAPi] =
233 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
235 if (xfs_has_reflink(sc->mp)) {
236 agf->agf_refcount_root =
237 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
238 agf->agf_refcount_level =
239 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
243 /* Update all AGF fields which derive from btree contents. */
245 xrep_agf_calc_from_btrees(
246 struct xfs_scrub *sc,
247 struct xfs_buf *agf_bp)
249 struct xrep_agf_allocbt raa = { .sc = sc };
250 struct xfs_btree_cur *cur = NULL;
251 struct xfs_agf *agf = agf_bp->b_addr;
252 struct xfs_mount *mp = sc->mp;
253 xfs_agblock_t btreeblks;
254 xfs_agblock_t blocks;
257 /* Update the AGF counters from the bnobt. */
258 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
259 sc->sa.pag, XFS_BTNUM_BNO);
260 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
263 error = xfs_btree_count_blocks(cur, &blocks);
266 xfs_btree_del_cursor(cur, error);
267 btreeblks = blocks - 1;
268 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
269 agf->agf_longest = cpu_to_be32(raa.longest);
271 /* Update the AGF counters from the cntbt. */
272 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
273 sc->sa.pag, XFS_BTNUM_CNT);
274 error = xfs_btree_count_blocks(cur, &blocks);
277 xfs_btree_del_cursor(cur, error);
278 btreeblks += blocks - 1;
280 /* Update the AGF counters from the rmapbt. */
281 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
282 error = xfs_btree_count_blocks(cur, &blocks);
285 xfs_btree_del_cursor(cur, error);
286 agf->agf_rmap_blocks = cpu_to_be32(blocks);
287 btreeblks += blocks - 1;
289 agf->agf_btreeblks = cpu_to_be32(btreeblks);
291 /* Update the AGF counters from the refcountbt. */
292 if (xfs_has_reflink(mp)) {
293 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
295 error = xfs_btree_count_blocks(cur, &blocks);
298 xfs_btree_del_cursor(cur, error);
299 agf->agf_refcount_blocks = cpu_to_be32(blocks);
304 xfs_btree_del_cursor(cur, error);
308 /* Commit the new AGF and reinitialize the incore state. */
311 struct xfs_scrub *sc,
312 struct xfs_buf *agf_bp)
314 struct xfs_perag *pag;
315 struct xfs_agf *agf = agf_bp->b_addr;
317 /* Trigger fdblocks recalculation */
318 xfs_force_summary_recalc(sc->mp);
320 /* Write this to disk. */
321 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
322 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
324 /* Now reinitialize the in-core counters we changed. */
326 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
327 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
328 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
329 pag->pagf_levels[XFS_BTNUM_BNOi] =
330 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
331 pag->pagf_levels[XFS_BTNUM_CNTi] =
332 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
333 pag->pagf_levels[XFS_BTNUM_RMAPi] =
334 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
335 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
341 /* Repair the AGF. v5 filesystems only. */
344 struct xfs_scrub *sc)
346 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = {
348 .rmap_owner = XFS_RMAP_OWN_AG,
349 .buf_ops = &xfs_bnobt_buf_ops,
350 .maxlevels = sc->mp->m_alloc_maxlevels,
353 .rmap_owner = XFS_RMAP_OWN_AG,
354 .buf_ops = &xfs_cntbt_buf_ops,
355 .maxlevels = sc->mp->m_alloc_maxlevels,
357 [XREP_AGF_RMAPBT] = {
358 .rmap_owner = XFS_RMAP_OWN_AG,
359 .buf_ops = &xfs_rmapbt_buf_ops,
360 .maxlevels = sc->mp->m_rmap_maxlevels,
362 [XREP_AGF_REFCOUNTBT] = {
363 .rmap_owner = XFS_RMAP_OWN_REFC,
364 .buf_ops = &xfs_refcountbt_buf_ops,
365 .maxlevels = sc->mp->m_refc_maxlevels,
371 struct xfs_agf old_agf;
372 struct xfs_mount *mp = sc->mp;
373 struct xfs_buf *agf_bp;
374 struct xfs_buf *agfl_bp;
378 /* We require the rmapbt to rebuild anything. */
379 if (!xfs_has_rmapbt(mp))
383 * Make sure we have the AGF buffer, as scrub might have decided it
384 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
386 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
387 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
389 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
392 agf_bp->b_ops = &xfs_agf_buf_ops;
393 agf = agf_bp->b_addr;
396 * Load the AGFL so that we can screen out OWN_AG blocks that are on
397 * the AGFL now; these blocks might have once been part of the
398 * bno/cnt/rmap btrees but are not now. This is a chicken and egg
399 * problem: the AGF is corrupt, so we have to trust the AGFL contents
400 * because we can't do any serious cross-referencing with any of the
401 * btrees rooted in the AGF. If the AGFL contents are obviously bad
402 * then we'll bail out.
404 error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
409 * Spot-check the AGFL blocks; if they're obviously corrupt then
410 * there's nothing we can do but bail out.
412 error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
413 xrep_agf_check_agfl_block, sc);
418 * Find the AGF btree roots. This is also a chicken-and-egg situation;
419 * see the function for more details.
421 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
425 /* Start rewriting the header and implant the btrees we found. */
426 xrep_agf_init_header(sc, agf_bp, &old_agf);
427 xrep_agf_set_roots(sc, agf, fab);
428 error = xrep_agf_calc_from_btrees(sc, agf_bp);
432 /* Commit the changes and reinitialize incore state. */
433 return xrep_agf_commit_new(sc, agf_bp);
436 /* Mark the incore AGF state stale and revert the AGF. */
437 sc->sa.pag->pagf_init = 0;
438 memcpy(agf, &old_agf, sizeof(old_agf));
445 /* Bitmap of other OWN_AG metadata blocks. */
446 struct xbitmap agmetablocks;
448 /* Bitmap of free space. */
449 struct xbitmap *freesp;
451 struct xfs_scrub *sc;
454 /* Record all OWN_AG (free space btree) information from the rmap data. */
457 struct xfs_btree_cur *cur,
458 const struct xfs_rmap_irec *rec,
461 struct xrep_agfl *ra = priv;
465 if (xchk_should_terminate(ra->sc, &error))
468 /* Record all the OWN_AG blocks. */
469 if (rec->rm_owner == XFS_RMAP_OWN_AG) {
470 fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.pag->pag_agno,
472 error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
477 return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
481 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
482 * which blocks belong to the AGFL.
484 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
485 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
486 * rmapbt). These are the old AGFL blocks, so return that list and the number
487 * of blocks we're actually going to put back on the AGFL.
490 xrep_agfl_collect_blocks(
491 struct xfs_scrub *sc,
492 struct xfs_buf *agf_bp,
493 struct xbitmap *agfl_extents,
494 xfs_agblock_t *flcount)
497 struct xfs_mount *mp = sc->mp;
498 struct xfs_btree_cur *cur;
502 ra.freesp = agfl_extents;
503 xbitmap_init(&ra.agmetablocks);
505 /* Find all space used by the free space btrees & rmapbt. */
506 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
507 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
510 xfs_btree_del_cursor(cur, error);
512 /* Find all blocks currently being used by the bnobt. */
513 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
514 sc->sa.pag, XFS_BTNUM_BNO);
515 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
518 xfs_btree_del_cursor(cur, error);
520 /* Find all blocks currently being used by the cntbt. */
521 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
522 sc->sa.pag, XFS_BTNUM_CNT);
523 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
527 xfs_btree_del_cursor(cur, error);
530 * Drop the freesp meta blocks that are in use by btrees.
531 * The remaining blocks /should/ be AGFL blocks.
533 error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
534 xbitmap_destroy(&ra.agmetablocks);
539 * Calculate the new AGFL size. If we found more blocks than fit in
540 * the AGFL we'll free them later.
542 *flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
547 xbitmap_destroy(&ra.agmetablocks);
548 xfs_btree_del_cursor(cur, error);
552 /* Update the AGF and reset the in-core state. */
554 xrep_agfl_update_agf(
555 struct xfs_scrub *sc,
556 struct xfs_buf *agf_bp,
557 xfs_agblock_t flcount)
559 struct xfs_agf *agf = agf_bp->b_addr;
561 ASSERT(flcount <= xfs_agfl_size(sc->mp));
563 /* Trigger fdblocks recalculation */
564 xfs_force_summary_recalc(sc->mp);
566 /* Update the AGF counters. */
567 if (sc->sa.pag->pagf_init)
568 sc->sa.pag->pagf_flcount = flcount;
569 agf->agf_flfirst = cpu_to_be32(0);
570 agf->agf_flcount = cpu_to_be32(flcount);
571 agf->agf_fllast = cpu_to_be32(flcount - 1);
573 xfs_alloc_log_agf(sc->tp, agf_bp,
574 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
577 /* Write out a totally new AGFL. */
579 xrep_agfl_init_header(
580 struct xfs_scrub *sc,
581 struct xfs_buf *agfl_bp,
582 struct xbitmap *agfl_extents,
583 xfs_agblock_t flcount)
585 struct xfs_mount *mp = sc->mp;
587 struct xbitmap_range *br;
588 struct xbitmap_range *n;
589 struct xfs_agfl *agfl;
593 ASSERT(flcount <= xfs_agfl_size(mp));
596 * Start rewriting the header by setting the bno[] array to
597 * NULLAGBLOCK, then setting AGFL header fields.
599 agfl = XFS_BUF_TO_AGFL(agfl_bp);
600 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
601 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
602 agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
603 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
606 * Fill the AGFL with the remaining blocks. If agfl_extents has more
607 * blocks than fit in the AGFL, they will be freed in a subsequent
611 agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
612 for_each_xbitmap_extent(br, n, agfl_extents) {
613 agbno = XFS_FSB_TO_AGBNO(mp, br->start);
615 trace_xrep_agfl_insert(mp, sc->sa.pag->pag_agno, agbno,
618 while (br->len > 0 && fl_off < flcount) {
619 agfl_bno[fl_off] = cpu_to_be32(agbno);
624 * We've now used br->start by putting it in the AGFL,
625 * so bump br so that we don't reap the block later.
637 /* Write new AGFL to disk. */
638 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
639 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
642 /* Repair the AGFL. */
645 struct xfs_scrub *sc)
647 struct xbitmap agfl_extents;
648 struct xfs_mount *mp = sc->mp;
649 struct xfs_buf *agf_bp;
650 struct xfs_buf *agfl_bp;
651 xfs_agblock_t flcount;
654 /* We require the rmapbt to rebuild anything. */
655 if (!xfs_has_rmapbt(mp))
658 xbitmap_init(&agfl_extents);
661 * Read the AGF so that we can query the rmapbt. We hope that there's
662 * nothing wrong with the AGF, but all the AG header repair functions
663 * have this chicken-and-egg problem.
665 error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
670 * Make sure we have the AGFL buffer, as scrub might have decided it
671 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
673 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
674 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
676 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
679 agfl_bp->b_ops = &xfs_agfl_buf_ops;
681 /* Gather all the extents we're going to put on the new AGFL. */
682 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
687 * Update AGF and AGFL. We reset the global free block counter when
688 * we adjust the AGF flcount (which can fail) so avoid updating any
689 * buffers until we know that part works.
691 xrep_agfl_update_agf(sc, agf_bp, flcount);
692 xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
695 * Ok, the AGFL should be ready to go now. Roll the transaction to
696 * make the new AGFL permanent before we start using it to return
697 * freespace overflow to the freespace btrees.
699 sc->sa.agf_bp = agf_bp;
700 sc->sa.agfl_bp = agfl_bp;
701 error = xrep_roll_ag_trans(sc);
705 /* Dump any AGFL overflow. */
706 error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
709 xbitmap_destroy(&agfl_extents);
716 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
717 * XFS_BTNUM_ names here to avoid creating a sparse array.
727 * Given the inode btree roots described by *fab, find the roots, check them
728 * for sanity, and pass the root data back out via *fab.
731 xrep_agi_find_btrees(
732 struct xfs_scrub *sc,
733 struct xrep_find_ag_btree *fab)
735 struct xfs_buf *agf_bp;
736 struct xfs_mount *mp = sc->mp;
740 error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
744 /* Find the btree roots. */
745 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
749 /* We must find the inobt root. */
750 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
751 return -EFSCORRUPTED;
753 /* We must find the finobt root if that feature is enabled. */
754 if (xfs_has_finobt(mp) &&
755 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
756 return -EFSCORRUPTED;
762 * Reinitialize the AGI header, making an in-core copy of the old contents so
763 * that we know which in-core state needs to be reinitialized.
766 xrep_agi_init_header(
767 struct xfs_scrub *sc,
768 struct xfs_buf *agi_bp,
769 struct xfs_agi *old_agi)
771 struct xfs_agi *agi = agi_bp->b_addr;
772 struct xfs_mount *mp = sc->mp;
774 memcpy(old_agi, agi, sizeof(*old_agi));
775 memset(agi, 0, BBTOB(agi_bp->b_length));
776 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
777 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
778 agi->agi_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
779 agi->agi_length = cpu_to_be32(sc->sa.pag->block_count);
780 agi->agi_newino = cpu_to_be32(NULLAGINO);
781 agi->agi_dirino = cpu_to_be32(NULLAGINO);
783 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
785 /* We don't know how to fix the unlinked list yet. */
786 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
787 sizeof(agi->agi_unlinked));
789 /* Mark the incore AGF data stale until we're done fixing things. */
790 ASSERT(sc->sa.pag->pagi_init);
791 sc->sa.pag->pagi_init = 0;
794 /* Set btree root information in an AGI. */
797 struct xfs_scrub *sc,
799 struct xrep_find_ag_btree *fab)
801 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
802 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
804 if (xfs_has_finobt(sc->mp)) {
805 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
806 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
810 /* Update the AGI counters. */
812 xrep_agi_calc_from_btrees(
813 struct xfs_scrub *sc,
814 struct xfs_buf *agi_bp)
816 struct xfs_btree_cur *cur;
817 struct xfs_agi *agi = agi_bp->b_addr;
818 struct xfs_mount *mp = sc->mp;
820 xfs_agino_t freecount;
823 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
824 sc->sa.pag, XFS_BTNUM_INO);
825 error = xfs_ialloc_count_inodes(cur, &count, &freecount);
828 if (xfs_has_inobtcounts(mp)) {
829 xfs_agblock_t blocks;
831 error = xfs_btree_count_blocks(cur, &blocks);
834 agi->agi_iblocks = cpu_to_be32(blocks);
836 xfs_btree_del_cursor(cur, error);
838 agi->agi_count = cpu_to_be32(count);
839 agi->agi_freecount = cpu_to_be32(freecount);
841 if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
842 xfs_agblock_t blocks;
844 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
845 sc->sa.pag, XFS_BTNUM_FINO);
846 error = xfs_btree_count_blocks(cur, &blocks);
849 xfs_btree_del_cursor(cur, error);
850 agi->agi_fblocks = cpu_to_be32(blocks);
855 xfs_btree_del_cursor(cur, error);
859 /* Trigger reinitialization of the in-core data. */
862 struct xfs_scrub *sc,
863 struct xfs_buf *agi_bp)
865 struct xfs_perag *pag;
866 struct xfs_agi *agi = agi_bp->b_addr;
868 /* Trigger inode count recalculation */
869 xfs_force_summary_recalc(sc->mp);
871 /* Write this to disk. */
872 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
873 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
875 /* Now reinitialize the in-core counters if necessary. */
877 pag->pagi_count = be32_to_cpu(agi->agi_count);
878 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
884 /* Repair the AGI. */
887 struct xfs_scrub *sc)
889 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = {
891 .rmap_owner = XFS_RMAP_OWN_INOBT,
892 .buf_ops = &xfs_inobt_buf_ops,
893 .maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
895 [XREP_AGI_FINOBT] = {
896 .rmap_owner = XFS_RMAP_OWN_INOBT,
897 .buf_ops = &xfs_finobt_buf_ops,
898 .maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
904 struct xfs_agi old_agi;
905 struct xfs_mount *mp = sc->mp;
906 struct xfs_buf *agi_bp;
910 /* We require the rmapbt to rebuild anything. */
911 if (!xfs_has_rmapbt(mp))
915 * Make sure we have the AGI buffer, as scrub might have decided it
916 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
918 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
919 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
921 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
924 agi_bp->b_ops = &xfs_agi_buf_ops;
925 agi = agi_bp->b_addr;
927 /* Find the AGI btree roots. */
928 error = xrep_agi_find_btrees(sc, fab);
932 /* Start rewriting the header and implant the btrees we found. */
933 xrep_agi_init_header(sc, agi_bp, &old_agi);
934 xrep_agi_set_roots(sc, agi, fab);
935 error = xrep_agi_calc_from_btrees(sc, agi_bp);
939 /* Reinitialize in-core state. */
940 return xrep_agi_commit_new(sc, agi_bp);
943 /* Mark the incore AGI state stale and revert the AGI. */
944 sc->sa.pag->pagi_init = 0;
945 memcpy(agi, &old_agi, sizeof(old_agi));