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(mp, sc->sa.pag->pag_agno, 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 struct xfs_mount *mp = sc->mp;
134 xfs_agnumber_t agno = sc->sm->sm_agno;
136 return xfs_verify_agbno(mp, agno, fab->root) &&
137 fab->height <= fab->maxlevels;
141 * Given the btree roots described by *fab, find the roots, check them for
142 * sanity, and pass the root data back out via *fab.
144 * This is /also/ a chicken and egg problem because we have to use the rmapbt
145 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no
146 * idea if the btrees make any sense. If we hit obvious corruptions in those
147 * btrees we'll bail out.
150 xrep_agf_find_btrees(
151 struct xfs_scrub *sc,
152 struct xfs_buf *agf_bp,
153 struct xrep_find_ag_btree *fab,
154 struct xfs_buf *agfl_bp)
156 struct xfs_agf *old_agf = agf_bp->b_addr;
159 /* Go find the root data. */
160 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
164 /* We must find the bnobt, cntbt, and rmapbt roots. */
165 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
166 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
167 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
168 return -EFSCORRUPTED;
171 * We relied on the rmapbt to reconstruct the AGF. If we get a
172 * different root then something's seriously wrong.
174 if (fab[XREP_AGF_RMAPBT].root !=
175 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
176 return -EFSCORRUPTED;
178 /* We must find the refcountbt root if that feature is enabled. */
179 if (xfs_has_reflink(sc->mp) &&
180 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
181 return -EFSCORRUPTED;
187 * Reinitialize the AGF header, making an in-core copy of the old contents so
188 * that we know which in-core state needs to be reinitialized.
191 xrep_agf_init_header(
192 struct xfs_scrub *sc,
193 struct xfs_buf *agf_bp,
194 struct xfs_agf *old_agf)
196 struct xfs_mount *mp = sc->mp;
197 struct xfs_agf *agf = agf_bp->b_addr;
199 memcpy(old_agf, agf, sizeof(*old_agf));
200 memset(agf, 0, BBTOB(agf_bp->b_length));
201 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
202 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
203 agf->agf_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
204 agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp,
205 sc->sa.pag->pag_agno));
206 agf->agf_flfirst = old_agf->agf_flfirst;
207 agf->agf_fllast = old_agf->agf_fllast;
208 agf->agf_flcount = old_agf->agf_flcount;
210 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
212 /* Mark the incore AGF data stale until we're done fixing things. */
213 ASSERT(sc->sa.pag->pagf_init);
214 sc->sa.pag->pagf_init = 0;
217 /* Set btree root information in an AGF. */
220 struct xfs_scrub *sc,
222 struct xrep_find_ag_btree *fab)
224 agf->agf_roots[XFS_BTNUM_BNOi] =
225 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
226 agf->agf_levels[XFS_BTNUM_BNOi] =
227 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
229 agf->agf_roots[XFS_BTNUM_CNTi] =
230 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
231 agf->agf_levels[XFS_BTNUM_CNTi] =
232 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
234 agf->agf_roots[XFS_BTNUM_RMAPi] =
235 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
236 agf->agf_levels[XFS_BTNUM_RMAPi] =
237 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
239 if (xfs_has_reflink(sc->mp)) {
240 agf->agf_refcount_root =
241 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
242 agf->agf_refcount_level =
243 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
247 /* Update all AGF fields which derive from btree contents. */
249 xrep_agf_calc_from_btrees(
250 struct xfs_scrub *sc,
251 struct xfs_buf *agf_bp)
253 struct xrep_agf_allocbt raa = { .sc = sc };
254 struct xfs_btree_cur *cur = NULL;
255 struct xfs_agf *agf = agf_bp->b_addr;
256 struct xfs_mount *mp = sc->mp;
257 xfs_agblock_t btreeblks;
258 xfs_agblock_t blocks;
261 /* Update the AGF counters from the bnobt. */
262 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
263 sc->sa.pag, XFS_BTNUM_BNO);
264 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
267 error = xfs_btree_count_blocks(cur, &blocks);
270 xfs_btree_del_cursor(cur, error);
271 btreeblks = blocks - 1;
272 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
273 agf->agf_longest = cpu_to_be32(raa.longest);
275 /* Update the AGF counters from the cntbt. */
276 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
277 sc->sa.pag, XFS_BTNUM_CNT);
278 error = xfs_btree_count_blocks(cur, &blocks);
281 xfs_btree_del_cursor(cur, error);
282 btreeblks += blocks - 1;
284 /* Update the AGF counters from the rmapbt. */
285 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
286 error = xfs_btree_count_blocks(cur, &blocks);
289 xfs_btree_del_cursor(cur, error);
290 agf->agf_rmap_blocks = cpu_to_be32(blocks);
291 btreeblks += blocks - 1;
293 agf->agf_btreeblks = cpu_to_be32(btreeblks);
295 /* Update the AGF counters from the refcountbt. */
296 if (xfs_has_reflink(mp)) {
297 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
299 error = xfs_btree_count_blocks(cur, &blocks);
302 xfs_btree_del_cursor(cur, error);
303 agf->agf_refcount_blocks = cpu_to_be32(blocks);
308 xfs_btree_del_cursor(cur, error);
312 /* Commit the new AGF and reinitialize the incore state. */
315 struct xfs_scrub *sc,
316 struct xfs_buf *agf_bp)
318 struct xfs_perag *pag;
319 struct xfs_agf *agf = agf_bp->b_addr;
321 /* Trigger fdblocks recalculation */
322 xfs_force_summary_recalc(sc->mp);
324 /* Write this to disk. */
325 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
326 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
328 /* Now reinitialize the in-core counters we changed. */
330 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
331 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
332 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
333 pag->pagf_levels[XFS_BTNUM_BNOi] =
334 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
335 pag->pagf_levels[XFS_BTNUM_CNTi] =
336 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
337 pag->pagf_levels[XFS_BTNUM_RMAPi] =
338 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
339 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
345 /* Repair the AGF. v5 filesystems only. */
348 struct xfs_scrub *sc)
350 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = {
352 .rmap_owner = XFS_RMAP_OWN_AG,
353 .buf_ops = &xfs_bnobt_buf_ops,
354 .maxlevels = sc->mp->m_alloc_maxlevels,
357 .rmap_owner = XFS_RMAP_OWN_AG,
358 .buf_ops = &xfs_cntbt_buf_ops,
359 .maxlevels = sc->mp->m_alloc_maxlevels,
361 [XREP_AGF_RMAPBT] = {
362 .rmap_owner = XFS_RMAP_OWN_AG,
363 .buf_ops = &xfs_rmapbt_buf_ops,
364 .maxlevels = sc->mp->m_rmap_maxlevels,
366 [XREP_AGF_REFCOUNTBT] = {
367 .rmap_owner = XFS_RMAP_OWN_REFC,
368 .buf_ops = &xfs_refcountbt_buf_ops,
369 .maxlevels = sc->mp->m_refc_maxlevels,
375 struct xfs_agf old_agf;
376 struct xfs_mount *mp = sc->mp;
377 struct xfs_buf *agf_bp;
378 struct xfs_buf *agfl_bp;
382 /* We require the rmapbt to rebuild anything. */
383 if (!xfs_has_rmapbt(mp))
387 * Make sure we have the AGF buffer, as scrub might have decided it
388 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
390 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
391 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
393 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
396 agf_bp->b_ops = &xfs_agf_buf_ops;
397 agf = agf_bp->b_addr;
400 * Load the AGFL so that we can screen out OWN_AG blocks that are on
401 * the AGFL now; these blocks might have once been part of the
402 * bno/cnt/rmap btrees but are not now. This is a chicken and egg
403 * problem: the AGF is corrupt, so we have to trust the AGFL contents
404 * because we can't do any serious cross-referencing with any of the
405 * btrees rooted in the AGF. If the AGFL contents are obviously bad
406 * then we'll bail out.
408 error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.pag->pag_agno, &agfl_bp);
413 * Spot-check the AGFL blocks; if they're obviously corrupt then
414 * there's nothing we can do but bail out.
416 error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
417 xrep_agf_check_agfl_block, sc);
422 * Find the AGF btree roots. This is also a chicken-and-egg situation;
423 * see the function for more details.
425 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
429 /* Start rewriting the header and implant the btrees we found. */
430 xrep_agf_init_header(sc, agf_bp, &old_agf);
431 xrep_agf_set_roots(sc, agf, fab);
432 error = xrep_agf_calc_from_btrees(sc, agf_bp);
436 /* Commit the changes and reinitialize incore state. */
437 return xrep_agf_commit_new(sc, agf_bp);
440 /* Mark the incore AGF state stale and revert the AGF. */
441 sc->sa.pag->pagf_init = 0;
442 memcpy(agf, &old_agf, sizeof(old_agf));
449 /* Bitmap of other OWN_AG metadata blocks. */
450 struct xbitmap agmetablocks;
452 /* Bitmap of free space. */
453 struct xbitmap *freesp;
455 struct xfs_scrub *sc;
458 /* Record all OWN_AG (free space btree) information from the rmap data. */
461 struct xfs_btree_cur *cur,
462 const struct xfs_rmap_irec *rec,
465 struct xrep_agfl *ra = priv;
469 if (xchk_should_terminate(ra->sc, &error))
472 /* Record all the OWN_AG blocks. */
473 if (rec->rm_owner == XFS_RMAP_OWN_AG) {
474 fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.pag->pag_agno,
476 error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
481 return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
485 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
486 * which blocks belong to the AGFL.
488 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
489 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
490 * rmapbt). These are the old AGFL blocks, so return that list and the number
491 * of blocks we're actually going to put back on the AGFL.
494 xrep_agfl_collect_blocks(
495 struct xfs_scrub *sc,
496 struct xfs_buf *agf_bp,
497 struct xbitmap *agfl_extents,
498 xfs_agblock_t *flcount)
501 struct xfs_mount *mp = sc->mp;
502 struct xfs_btree_cur *cur;
506 ra.freesp = agfl_extents;
507 xbitmap_init(&ra.agmetablocks);
509 /* Find all space used by the free space btrees & rmapbt. */
510 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
511 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
514 xfs_btree_del_cursor(cur, error);
516 /* Find all blocks currently being used by the bnobt. */
517 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
518 sc->sa.pag, XFS_BTNUM_BNO);
519 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
522 xfs_btree_del_cursor(cur, error);
524 /* Find all blocks currently being used by the cntbt. */
525 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
526 sc->sa.pag, XFS_BTNUM_CNT);
527 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
531 xfs_btree_del_cursor(cur, error);
534 * Drop the freesp meta blocks that are in use by btrees.
535 * The remaining blocks /should/ be AGFL blocks.
537 error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
538 xbitmap_destroy(&ra.agmetablocks);
543 * Calculate the new AGFL size. If we found more blocks than fit in
544 * the AGFL we'll free them later.
546 *flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
551 xbitmap_destroy(&ra.agmetablocks);
552 xfs_btree_del_cursor(cur, error);
556 /* Update the AGF and reset the in-core state. */
558 xrep_agfl_update_agf(
559 struct xfs_scrub *sc,
560 struct xfs_buf *agf_bp,
561 xfs_agblock_t flcount)
563 struct xfs_agf *agf = agf_bp->b_addr;
565 ASSERT(flcount <= xfs_agfl_size(sc->mp));
567 /* Trigger fdblocks recalculation */
568 xfs_force_summary_recalc(sc->mp);
570 /* Update the AGF counters. */
571 if (sc->sa.pag->pagf_init)
572 sc->sa.pag->pagf_flcount = flcount;
573 agf->agf_flfirst = cpu_to_be32(0);
574 agf->agf_flcount = cpu_to_be32(flcount);
575 agf->agf_fllast = cpu_to_be32(flcount - 1);
577 xfs_alloc_log_agf(sc->tp, agf_bp,
578 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
581 /* Write out a totally new AGFL. */
583 xrep_agfl_init_header(
584 struct xfs_scrub *sc,
585 struct xfs_buf *agfl_bp,
586 struct xbitmap *agfl_extents,
587 xfs_agblock_t flcount)
589 struct xfs_mount *mp = sc->mp;
591 struct xbitmap_range *br;
592 struct xbitmap_range *n;
593 struct xfs_agfl *agfl;
597 ASSERT(flcount <= xfs_agfl_size(mp));
600 * Start rewriting the header by setting the bno[] array to
601 * NULLAGBLOCK, then setting AGFL header fields.
603 agfl = XFS_BUF_TO_AGFL(agfl_bp);
604 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
605 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
606 agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
607 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
610 * Fill the AGFL with the remaining blocks. If agfl_extents has more
611 * blocks than fit in the AGFL, they will be freed in a subsequent
615 agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
616 for_each_xbitmap_extent(br, n, agfl_extents) {
617 agbno = XFS_FSB_TO_AGBNO(mp, br->start);
619 trace_xrep_agfl_insert(mp, sc->sa.pag->pag_agno, agbno,
622 while (br->len > 0 && fl_off < flcount) {
623 agfl_bno[fl_off] = cpu_to_be32(agbno);
628 * We've now used br->start by putting it in the AGFL,
629 * so bump br so that we don't reap the block later.
641 /* Write new AGFL to disk. */
642 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
643 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
646 /* Repair the AGFL. */
649 struct xfs_scrub *sc)
651 struct xbitmap agfl_extents;
652 struct xfs_mount *mp = sc->mp;
653 struct xfs_buf *agf_bp;
654 struct xfs_buf *agfl_bp;
655 xfs_agblock_t flcount;
658 /* We require the rmapbt to rebuild anything. */
659 if (!xfs_has_rmapbt(mp))
662 xbitmap_init(&agfl_extents);
665 * Read the AGF so that we can query the rmapbt. We hope that there's
666 * nothing wrong with the AGF, but all the AG header repair functions
667 * have this chicken-and-egg problem.
669 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.pag->pag_agno, 0,
675 * Make sure we have the AGFL buffer, as scrub might have decided it
676 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
678 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
679 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
681 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
684 agfl_bp->b_ops = &xfs_agfl_buf_ops;
686 /* Gather all the extents we're going to put on the new AGFL. */
687 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
692 * Update AGF and AGFL. We reset the global free block counter when
693 * we adjust the AGF flcount (which can fail) so avoid updating any
694 * buffers until we know that part works.
696 xrep_agfl_update_agf(sc, agf_bp, flcount);
697 xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
700 * Ok, the AGFL should be ready to go now. Roll the transaction to
701 * make the new AGFL permanent before we start using it to return
702 * freespace overflow to the freespace btrees.
704 sc->sa.agf_bp = agf_bp;
705 sc->sa.agfl_bp = agfl_bp;
706 error = xrep_roll_ag_trans(sc);
710 /* Dump any AGFL overflow. */
711 error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
714 xbitmap_destroy(&agfl_extents);
721 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
722 * XFS_BTNUM_ names here to avoid creating a sparse array.
732 * Given the inode btree roots described by *fab, find the roots, check them
733 * for sanity, and pass the root data back out via *fab.
736 xrep_agi_find_btrees(
737 struct xfs_scrub *sc,
738 struct xrep_find_ag_btree *fab)
740 struct xfs_buf *agf_bp;
741 struct xfs_mount *mp = sc->mp;
745 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.pag->pag_agno, 0,
750 /* Find the btree roots. */
751 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
755 /* We must find the inobt root. */
756 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
757 return -EFSCORRUPTED;
759 /* We must find the finobt root if that feature is enabled. */
760 if (xfs_has_finobt(mp) &&
761 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
762 return -EFSCORRUPTED;
768 * Reinitialize the AGI header, making an in-core copy of the old contents so
769 * that we know which in-core state needs to be reinitialized.
772 xrep_agi_init_header(
773 struct xfs_scrub *sc,
774 struct xfs_buf *agi_bp,
775 struct xfs_agi *old_agi)
777 struct xfs_agi *agi = agi_bp->b_addr;
778 struct xfs_mount *mp = sc->mp;
780 memcpy(old_agi, agi, sizeof(*old_agi));
781 memset(agi, 0, BBTOB(agi_bp->b_length));
782 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
783 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
784 agi->agi_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
785 agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp,
786 sc->sa.pag->pag_agno));
787 agi->agi_newino = cpu_to_be32(NULLAGINO);
788 agi->agi_dirino = cpu_to_be32(NULLAGINO);
790 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
792 /* We don't know how to fix the unlinked list yet. */
793 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
794 sizeof(agi->agi_unlinked));
796 /* Mark the incore AGF data stale until we're done fixing things. */
797 ASSERT(sc->sa.pag->pagi_init);
798 sc->sa.pag->pagi_init = 0;
801 /* Set btree root information in an AGI. */
804 struct xfs_scrub *sc,
806 struct xrep_find_ag_btree *fab)
808 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
809 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
811 if (xfs_has_finobt(sc->mp)) {
812 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
813 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
817 /* Update the AGI counters. */
819 xrep_agi_calc_from_btrees(
820 struct xfs_scrub *sc,
821 struct xfs_buf *agi_bp)
823 struct xfs_btree_cur *cur;
824 struct xfs_agi *agi = agi_bp->b_addr;
825 struct xfs_mount *mp = sc->mp;
827 xfs_agino_t freecount;
830 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
831 sc->sa.pag, XFS_BTNUM_INO);
832 error = xfs_ialloc_count_inodes(cur, &count, &freecount);
835 if (xfs_has_inobtcounts(mp)) {
836 xfs_agblock_t blocks;
838 error = xfs_btree_count_blocks(cur, &blocks);
841 agi->agi_iblocks = cpu_to_be32(blocks);
843 xfs_btree_del_cursor(cur, error);
845 agi->agi_count = cpu_to_be32(count);
846 agi->agi_freecount = cpu_to_be32(freecount);
848 if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
849 xfs_agblock_t blocks;
851 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
852 sc->sa.pag, XFS_BTNUM_FINO);
853 error = xfs_btree_count_blocks(cur, &blocks);
856 xfs_btree_del_cursor(cur, error);
857 agi->agi_fblocks = cpu_to_be32(blocks);
862 xfs_btree_del_cursor(cur, error);
866 /* Trigger reinitialization of the in-core data. */
869 struct xfs_scrub *sc,
870 struct xfs_buf *agi_bp)
872 struct xfs_perag *pag;
873 struct xfs_agi *agi = agi_bp->b_addr;
875 /* Trigger inode count recalculation */
876 xfs_force_summary_recalc(sc->mp);
878 /* Write this to disk. */
879 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
880 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
882 /* Now reinitialize the in-core counters if necessary. */
884 pag->pagi_count = be32_to_cpu(agi->agi_count);
885 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
891 /* Repair the AGI. */
894 struct xfs_scrub *sc)
896 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = {
898 .rmap_owner = XFS_RMAP_OWN_INOBT,
899 .buf_ops = &xfs_inobt_buf_ops,
900 .maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
902 [XREP_AGI_FINOBT] = {
903 .rmap_owner = XFS_RMAP_OWN_INOBT,
904 .buf_ops = &xfs_finobt_buf_ops,
905 .maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
911 struct xfs_agi old_agi;
912 struct xfs_mount *mp = sc->mp;
913 struct xfs_buf *agi_bp;
917 /* We require the rmapbt to rebuild anything. */
918 if (!xfs_has_rmapbt(mp))
922 * Make sure we have the AGI buffer, as scrub might have decided it
923 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
925 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
926 XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
928 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
931 agi_bp->b_ops = &xfs_agi_buf_ops;
932 agi = agi_bp->b_addr;
934 /* Find the AGI btree roots. */
935 error = xrep_agi_find_btrees(sc, fab);
939 /* Start rewriting the header and implant the btrees we found. */
940 xrep_agi_init_header(sc, agi_bp, &old_agi);
941 xrep_agi_set_roots(sc, agi, fab);
942 error = xrep_agi_calc_from_btrees(sc, agi_bp);
946 /* Reinitialize in-core state. */
947 return xrep_agi_commit_new(sc, agi_bp);
950 /* Mark the incore AGI state stale and revert the AGI. */
951 sc->sa.pag->pagi_init = 0;
952 memcpy(agi, &old_agi, sizeof(old_agi));