18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Oracle.  All Rights Reserved.
48c2ecf20Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include "xfs.h"
78c2ecf20Sopenharmony_ci#include "xfs_fs.h"
88c2ecf20Sopenharmony_ci#include "xfs_shared.h"
98c2ecf20Sopenharmony_ci#include "xfs_format.h"
108c2ecf20Sopenharmony_ci#include "xfs_trans_resv.h"
118c2ecf20Sopenharmony_ci#include "xfs_mount.h"
128c2ecf20Sopenharmony_ci#include "xfs_btree.h"
138c2ecf20Sopenharmony_ci#include "xfs_log_format.h"
148c2ecf20Sopenharmony_ci#include "xfs_trans.h"
158c2ecf20Sopenharmony_ci#include "xfs_sb.h"
168c2ecf20Sopenharmony_ci#include "xfs_alloc.h"
178c2ecf20Sopenharmony_ci#include "xfs_alloc_btree.h"
188c2ecf20Sopenharmony_ci#include "xfs_ialloc.h"
198c2ecf20Sopenharmony_ci#include "xfs_ialloc_btree.h"
208c2ecf20Sopenharmony_ci#include "xfs_rmap.h"
218c2ecf20Sopenharmony_ci#include "xfs_rmap_btree.h"
228c2ecf20Sopenharmony_ci#include "xfs_refcount_btree.h"
238c2ecf20Sopenharmony_ci#include "scrub/scrub.h"
248c2ecf20Sopenharmony_ci#include "scrub/common.h"
258c2ecf20Sopenharmony_ci#include "scrub/trace.h"
268c2ecf20Sopenharmony_ci#include "scrub/repair.h"
278c2ecf20Sopenharmony_ci#include "scrub/bitmap.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Superblock */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* Repair the superblock. */
328c2ecf20Sopenharmony_ciint
338c2ecf20Sopenharmony_cixrep_superblock(
348c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
378c2ecf20Sopenharmony_ci	struct xfs_buf		*bp;
388c2ecf20Sopenharmony_ci	xfs_agnumber_t		agno;
398c2ecf20Sopenharmony_ci	int			error;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	/* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
428c2ecf20Sopenharmony_ci	agno = sc->sm->sm_agno;
438c2ecf20Sopenharmony_ci	if (agno == 0)
448c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
478c2ecf20Sopenharmony_ci	if (error)
488c2ecf20Sopenharmony_ci		return error;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	/* Copy AG 0's superblock to this one. */
518c2ecf20Sopenharmony_ci	xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
528c2ecf20Sopenharmony_ci	xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/* Write this to disk. */
558c2ecf20Sopenharmony_ci	xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
568c2ecf20Sopenharmony_ci	xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
578c2ecf20Sopenharmony_ci	return error;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* AGF */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct xrep_agf_allocbt {
638c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc;
648c2ecf20Sopenharmony_ci	xfs_agblock_t		freeblks;
658c2ecf20Sopenharmony_ci	xfs_agblock_t		longest;
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* Record free space shape information. */
698c2ecf20Sopenharmony_ciSTATIC int
708c2ecf20Sopenharmony_cixrep_agf_walk_allocbt(
718c2ecf20Sopenharmony_ci	struct xfs_btree_cur		*cur,
728c2ecf20Sopenharmony_ci	struct xfs_alloc_rec_incore	*rec,
738c2ecf20Sopenharmony_ci	void				*priv)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct xrep_agf_allocbt		*raa = priv;
768c2ecf20Sopenharmony_ci	int				error = 0;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (xchk_should_terminate(raa->sc, &error))
798c2ecf20Sopenharmony_ci		return error;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	raa->freeblks += rec->ar_blockcount;
828c2ecf20Sopenharmony_ci	if (rec->ar_blockcount > raa->longest)
838c2ecf20Sopenharmony_ci		raa->longest = rec->ar_blockcount;
848c2ecf20Sopenharmony_ci	return error;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Does this AGFL block look sane? */
888c2ecf20Sopenharmony_ciSTATIC int
898c2ecf20Sopenharmony_cixrep_agf_check_agfl_block(
908c2ecf20Sopenharmony_ci	struct xfs_mount	*mp,
918c2ecf20Sopenharmony_ci	xfs_agblock_t		agbno,
928c2ecf20Sopenharmony_ci	void			*priv)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc = priv;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (!xfs_verify_agbno(mp, sc->sa.agno, agbno))
978c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
988c2ecf20Sopenharmony_ci	return 0;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
1038c2ecf20Sopenharmony_ci * XFS_BTNUM_ names here to avoid creating a sparse array.
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_cienum {
1068c2ecf20Sopenharmony_ci	XREP_AGF_BNOBT = 0,
1078c2ecf20Sopenharmony_ci	XREP_AGF_CNTBT,
1088c2ecf20Sopenharmony_ci	XREP_AGF_RMAPBT,
1098c2ecf20Sopenharmony_ci	XREP_AGF_REFCOUNTBT,
1108c2ecf20Sopenharmony_ci	XREP_AGF_END,
1118c2ecf20Sopenharmony_ci	XREP_AGF_MAX
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* Check a btree root candidate. */
1158c2ecf20Sopenharmony_cistatic inline bool
1168c2ecf20Sopenharmony_cixrep_check_btree_root(
1178c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc,
1188c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	*fab)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct xfs_mount		*mp = sc->mp;
1218c2ecf20Sopenharmony_ci	xfs_agnumber_t			agno = sc->sm->sm_agno;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	return xfs_verify_agbno(mp, agno, fab->root) &&
1248c2ecf20Sopenharmony_ci	       fab->height <= XFS_BTREE_MAXLEVELS;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * Given the btree roots described by *fab, find the roots, check them for
1298c2ecf20Sopenharmony_ci * sanity, and pass the root data back out via *fab.
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * This is /also/ a chicken and egg problem because we have to use the rmapbt
1328c2ecf20Sopenharmony_ci * (rooted in the AGF) to find the btrees rooted in the AGF.  We also have no
1338c2ecf20Sopenharmony_ci * idea if the btrees make any sense.  If we hit obvious corruptions in those
1348c2ecf20Sopenharmony_ci * btrees we'll bail out.
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_ciSTATIC int
1378c2ecf20Sopenharmony_cixrep_agf_find_btrees(
1388c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc,
1398c2ecf20Sopenharmony_ci	struct xfs_buf			*agf_bp,
1408c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	*fab,
1418c2ecf20Sopenharmony_ci	struct xfs_buf			*agfl_bp)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct xfs_agf			*old_agf = agf_bp->b_addr;
1448c2ecf20Sopenharmony_ci	int				error;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/* Go find the root data. */
1478c2ecf20Sopenharmony_ci	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
1488c2ecf20Sopenharmony_ci	if (error)
1498c2ecf20Sopenharmony_ci		return error;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* We must find the bnobt, cntbt, and rmapbt roots. */
1528c2ecf20Sopenharmony_ci	if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
1538c2ecf20Sopenharmony_ci	    !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
1548c2ecf20Sopenharmony_ci	    !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
1558c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/*
1588c2ecf20Sopenharmony_ci	 * We relied on the rmapbt to reconstruct the AGF.  If we get a
1598c2ecf20Sopenharmony_ci	 * different root then something's seriously wrong.
1608c2ecf20Sopenharmony_ci	 */
1618c2ecf20Sopenharmony_ci	if (fab[XREP_AGF_RMAPBT].root !=
1628c2ecf20Sopenharmony_ci	    be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
1638c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* We must find the refcountbt root if that feature is enabled. */
1668c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasreflink(&sc->mp->m_sb) &&
1678c2ecf20Sopenharmony_ci	    !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
1688c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/*
1748c2ecf20Sopenharmony_ci * Reinitialize the AGF header, making an in-core copy of the old contents so
1758c2ecf20Sopenharmony_ci * that we know which in-core state needs to be reinitialized.
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_ciSTATIC void
1788c2ecf20Sopenharmony_cixrep_agf_init_header(
1798c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
1808c2ecf20Sopenharmony_ci	struct xfs_buf		*agf_bp,
1818c2ecf20Sopenharmony_ci	struct xfs_agf		*old_agf)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
1848c2ecf20Sopenharmony_ci	struct xfs_agf		*agf = agf_bp->b_addr;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	memcpy(old_agf, agf, sizeof(*old_agf));
1878c2ecf20Sopenharmony_ci	memset(agf, 0, BBTOB(agf_bp->b_length));
1888c2ecf20Sopenharmony_ci	agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
1898c2ecf20Sopenharmony_ci	agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
1908c2ecf20Sopenharmony_ci	agf->agf_seqno = cpu_to_be32(sc->sa.agno);
1918c2ecf20Sopenharmony_ci	agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
1928c2ecf20Sopenharmony_ci	agf->agf_flfirst = old_agf->agf_flfirst;
1938c2ecf20Sopenharmony_ci	agf->agf_fllast = old_agf->agf_fllast;
1948c2ecf20Sopenharmony_ci	agf->agf_flcount = old_agf->agf_flcount;
1958c2ecf20Sopenharmony_ci	if (xfs_sb_version_hascrc(&mp->m_sb))
1968c2ecf20Sopenharmony_ci		uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* Mark the incore AGF data stale until we're done fixing things. */
1998c2ecf20Sopenharmony_ci	ASSERT(sc->sa.pag->pagf_init);
2008c2ecf20Sopenharmony_ci	sc->sa.pag->pagf_init = 0;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/* Set btree root information in an AGF. */
2048c2ecf20Sopenharmony_ciSTATIC void
2058c2ecf20Sopenharmony_cixrep_agf_set_roots(
2068c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc,
2078c2ecf20Sopenharmony_ci	struct xfs_agf			*agf,
2088c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	*fab)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	agf->agf_roots[XFS_BTNUM_BNOi] =
2118c2ecf20Sopenharmony_ci			cpu_to_be32(fab[XREP_AGF_BNOBT].root);
2128c2ecf20Sopenharmony_ci	agf->agf_levels[XFS_BTNUM_BNOi] =
2138c2ecf20Sopenharmony_ci			cpu_to_be32(fab[XREP_AGF_BNOBT].height);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	agf->agf_roots[XFS_BTNUM_CNTi] =
2168c2ecf20Sopenharmony_ci			cpu_to_be32(fab[XREP_AGF_CNTBT].root);
2178c2ecf20Sopenharmony_ci	agf->agf_levels[XFS_BTNUM_CNTi] =
2188c2ecf20Sopenharmony_ci			cpu_to_be32(fab[XREP_AGF_CNTBT].height);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	agf->agf_roots[XFS_BTNUM_RMAPi] =
2218c2ecf20Sopenharmony_ci			cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
2228c2ecf20Sopenharmony_ci	agf->agf_levels[XFS_BTNUM_RMAPi] =
2238c2ecf20Sopenharmony_ci			cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
2268c2ecf20Sopenharmony_ci		agf->agf_refcount_root =
2278c2ecf20Sopenharmony_ci				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
2288c2ecf20Sopenharmony_ci		agf->agf_refcount_level =
2298c2ecf20Sopenharmony_ci				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/* Update all AGF fields which derive from btree contents. */
2348c2ecf20Sopenharmony_ciSTATIC int
2358c2ecf20Sopenharmony_cixrep_agf_calc_from_btrees(
2368c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
2378c2ecf20Sopenharmony_ci	struct xfs_buf		*agf_bp)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct xrep_agf_allocbt	raa = { .sc = sc };
2408c2ecf20Sopenharmony_ci	struct xfs_btree_cur	*cur = NULL;
2418c2ecf20Sopenharmony_ci	struct xfs_agf		*agf = agf_bp->b_addr;
2428c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
2438c2ecf20Sopenharmony_ci	xfs_agblock_t		btreeblks;
2448c2ecf20Sopenharmony_ci	xfs_agblock_t		blocks;
2458c2ecf20Sopenharmony_ci	int			error;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	/* Update the AGF counters from the bnobt. */
2488c2ecf20Sopenharmony_ci	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
2498c2ecf20Sopenharmony_ci			XFS_BTNUM_BNO);
2508c2ecf20Sopenharmony_ci	error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
2518c2ecf20Sopenharmony_ci	if (error)
2528c2ecf20Sopenharmony_ci		goto err;
2538c2ecf20Sopenharmony_ci	error = xfs_btree_count_blocks(cur, &blocks);
2548c2ecf20Sopenharmony_ci	if (error)
2558c2ecf20Sopenharmony_ci		goto err;
2568c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
2578c2ecf20Sopenharmony_ci	btreeblks = blocks - 1;
2588c2ecf20Sopenharmony_ci	agf->agf_freeblks = cpu_to_be32(raa.freeblks);
2598c2ecf20Sopenharmony_ci	agf->agf_longest = cpu_to_be32(raa.longest);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* Update the AGF counters from the cntbt. */
2628c2ecf20Sopenharmony_ci	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
2638c2ecf20Sopenharmony_ci			XFS_BTNUM_CNT);
2648c2ecf20Sopenharmony_ci	error = xfs_btree_count_blocks(cur, &blocks);
2658c2ecf20Sopenharmony_ci	if (error)
2668c2ecf20Sopenharmony_ci		goto err;
2678c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
2688c2ecf20Sopenharmony_ci	btreeblks += blocks - 1;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* Update the AGF counters from the rmapbt. */
2718c2ecf20Sopenharmony_ci	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
2728c2ecf20Sopenharmony_ci	error = xfs_btree_count_blocks(cur, &blocks);
2738c2ecf20Sopenharmony_ci	if (error)
2748c2ecf20Sopenharmony_ci		goto err;
2758c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
2768c2ecf20Sopenharmony_ci	agf->agf_rmap_blocks = cpu_to_be32(blocks);
2778c2ecf20Sopenharmony_ci	btreeblks += blocks - 1;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	agf->agf_btreeblks = cpu_to_be32(btreeblks);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* Update the AGF counters from the refcountbt. */
2828c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
2838c2ecf20Sopenharmony_ci		cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
2848c2ecf20Sopenharmony_ci				sc->sa.agno);
2858c2ecf20Sopenharmony_ci		error = xfs_btree_count_blocks(cur, &blocks);
2868c2ecf20Sopenharmony_ci		if (error)
2878c2ecf20Sopenharmony_ci			goto err;
2888c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(cur, error);
2898c2ecf20Sopenharmony_ci		agf->agf_refcount_blocks = cpu_to_be32(blocks);
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return 0;
2938c2ecf20Sopenharmony_cierr:
2948c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
2958c2ecf20Sopenharmony_ci	return error;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/* Commit the new AGF and reinitialize the incore state. */
2998c2ecf20Sopenharmony_ciSTATIC int
3008c2ecf20Sopenharmony_cixrep_agf_commit_new(
3018c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
3028c2ecf20Sopenharmony_ci	struct xfs_buf		*agf_bp)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct xfs_perag	*pag;
3058c2ecf20Sopenharmony_ci	struct xfs_agf		*agf = agf_bp->b_addr;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	/* Trigger fdblocks recalculation */
3088c2ecf20Sopenharmony_ci	xfs_force_summary_recalc(sc->mp);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Write this to disk. */
3118c2ecf20Sopenharmony_ci	xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
3128c2ecf20Sopenharmony_ci	xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* Now reinitialize the in-core counters we changed. */
3158c2ecf20Sopenharmony_ci	pag = sc->sa.pag;
3168c2ecf20Sopenharmony_ci	pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
3178c2ecf20Sopenharmony_ci	pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
3188c2ecf20Sopenharmony_ci	pag->pagf_longest = be32_to_cpu(agf->agf_longest);
3198c2ecf20Sopenharmony_ci	pag->pagf_levels[XFS_BTNUM_BNOi] =
3208c2ecf20Sopenharmony_ci			be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
3218c2ecf20Sopenharmony_ci	pag->pagf_levels[XFS_BTNUM_CNTi] =
3228c2ecf20Sopenharmony_ci			be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
3238c2ecf20Sopenharmony_ci	pag->pagf_levels[XFS_BTNUM_RMAPi] =
3248c2ecf20Sopenharmony_ci			be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
3258c2ecf20Sopenharmony_ci	pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
3268c2ecf20Sopenharmony_ci	pag->pagf_init = 1;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	return 0;
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci/* Repair the AGF. v5 filesystems only. */
3328c2ecf20Sopenharmony_ciint
3338c2ecf20Sopenharmony_cixrep_agf(
3348c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	fab[XREP_AGF_MAX] = {
3378c2ecf20Sopenharmony_ci		[XREP_AGF_BNOBT] = {
3388c2ecf20Sopenharmony_ci			.rmap_owner = XFS_RMAP_OWN_AG,
3398c2ecf20Sopenharmony_ci			.buf_ops = &xfs_bnobt_buf_ops,
3408c2ecf20Sopenharmony_ci		},
3418c2ecf20Sopenharmony_ci		[XREP_AGF_CNTBT] = {
3428c2ecf20Sopenharmony_ci			.rmap_owner = XFS_RMAP_OWN_AG,
3438c2ecf20Sopenharmony_ci			.buf_ops = &xfs_cntbt_buf_ops,
3448c2ecf20Sopenharmony_ci		},
3458c2ecf20Sopenharmony_ci		[XREP_AGF_RMAPBT] = {
3468c2ecf20Sopenharmony_ci			.rmap_owner = XFS_RMAP_OWN_AG,
3478c2ecf20Sopenharmony_ci			.buf_ops = &xfs_rmapbt_buf_ops,
3488c2ecf20Sopenharmony_ci		},
3498c2ecf20Sopenharmony_ci		[XREP_AGF_REFCOUNTBT] = {
3508c2ecf20Sopenharmony_ci			.rmap_owner = XFS_RMAP_OWN_REFC,
3518c2ecf20Sopenharmony_ci			.buf_ops = &xfs_refcountbt_buf_ops,
3528c2ecf20Sopenharmony_ci		},
3538c2ecf20Sopenharmony_ci		[XREP_AGF_END] = {
3548c2ecf20Sopenharmony_ci			.buf_ops = NULL,
3558c2ecf20Sopenharmony_ci		},
3568c2ecf20Sopenharmony_ci	};
3578c2ecf20Sopenharmony_ci	struct xfs_agf			old_agf;
3588c2ecf20Sopenharmony_ci	struct xfs_mount		*mp = sc->mp;
3598c2ecf20Sopenharmony_ci	struct xfs_buf			*agf_bp;
3608c2ecf20Sopenharmony_ci	struct xfs_buf			*agfl_bp;
3618c2ecf20Sopenharmony_ci	struct xfs_agf			*agf;
3628c2ecf20Sopenharmony_ci	int				error;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/* We require the rmapbt to rebuild anything. */
3658c2ecf20Sopenharmony_ci	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
3668c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	xchk_perag_get(sc->mp, &sc->sa);
3698c2ecf20Sopenharmony_ci	/*
3708c2ecf20Sopenharmony_ci	 * Make sure we have the AGF buffer, as scrub might have decided it
3718c2ecf20Sopenharmony_ci	 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
3728c2ecf20Sopenharmony_ci	 */
3738c2ecf20Sopenharmony_ci	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
3748c2ecf20Sopenharmony_ci			XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGF_DADDR(mp)),
3758c2ecf20Sopenharmony_ci			XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
3768c2ecf20Sopenharmony_ci	if (error)
3778c2ecf20Sopenharmony_ci		return error;
3788c2ecf20Sopenharmony_ci	agf_bp->b_ops = &xfs_agf_buf_ops;
3798c2ecf20Sopenharmony_ci	agf = agf_bp->b_addr;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/*
3828c2ecf20Sopenharmony_ci	 * Load the AGFL so that we can screen out OWN_AG blocks that are on
3838c2ecf20Sopenharmony_ci	 * the AGFL now; these blocks might have once been part of the
3848c2ecf20Sopenharmony_ci	 * bno/cnt/rmap btrees but are not now.  This is a chicken and egg
3858c2ecf20Sopenharmony_ci	 * problem: the AGF is corrupt, so we have to trust the AGFL contents
3868c2ecf20Sopenharmony_ci	 * because we can't do any serious cross-referencing with any of the
3878c2ecf20Sopenharmony_ci	 * btrees rooted in the AGF.  If the AGFL contents are obviously bad
3888c2ecf20Sopenharmony_ci	 * then we'll bail out.
3898c2ecf20Sopenharmony_ci	 */
3908c2ecf20Sopenharmony_ci	error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.agno, &agfl_bp);
3918c2ecf20Sopenharmony_ci	if (error)
3928c2ecf20Sopenharmony_ci		return error;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	/*
3958c2ecf20Sopenharmony_ci	 * Spot-check the AGFL blocks; if they're obviously corrupt then
3968c2ecf20Sopenharmony_ci	 * there's nothing we can do but bail out.
3978c2ecf20Sopenharmony_ci	 */
3988c2ecf20Sopenharmony_ci	error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
3998c2ecf20Sopenharmony_ci			xrep_agf_check_agfl_block, sc);
4008c2ecf20Sopenharmony_ci	if (error)
4018c2ecf20Sopenharmony_ci		return error;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	/*
4048c2ecf20Sopenharmony_ci	 * Find the AGF btree roots.  This is also a chicken-and-egg situation;
4058c2ecf20Sopenharmony_ci	 * see the function for more details.
4068c2ecf20Sopenharmony_ci	 */
4078c2ecf20Sopenharmony_ci	error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
4088c2ecf20Sopenharmony_ci	if (error)
4098c2ecf20Sopenharmony_ci		return error;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	/* Start rewriting the header and implant the btrees we found. */
4128c2ecf20Sopenharmony_ci	xrep_agf_init_header(sc, agf_bp, &old_agf);
4138c2ecf20Sopenharmony_ci	xrep_agf_set_roots(sc, agf, fab);
4148c2ecf20Sopenharmony_ci	error = xrep_agf_calc_from_btrees(sc, agf_bp);
4158c2ecf20Sopenharmony_ci	if (error)
4168c2ecf20Sopenharmony_ci		goto out_revert;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	/* Commit the changes and reinitialize incore state. */
4198c2ecf20Sopenharmony_ci	return xrep_agf_commit_new(sc, agf_bp);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ciout_revert:
4228c2ecf20Sopenharmony_ci	/* Mark the incore AGF state stale and revert the AGF. */
4238c2ecf20Sopenharmony_ci	sc->sa.pag->pagf_init = 0;
4248c2ecf20Sopenharmony_ci	memcpy(agf, &old_agf, sizeof(old_agf));
4258c2ecf20Sopenharmony_ci	return error;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci/* AGFL */
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistruct xrep_agfl {
4318c2ecf20Sopenharmony_ci	/* Bitmap of other OWN_AG metadata blocks. */
4328c2ecf20Sopenharmony_ci	struct xbitmap		agmetablocks;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	/* Bitmap of free space. */
4358c2ecf20Sopenharmony_ci	struct xbitmap		*freesp;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc;
4388c2ecf20Sopenharmony_ci};
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* Record all OWN_AG (free space btree) information from the rmap data. */
4418c2ecf20Sopenharmony_ciSTATIC int
4428c2ecf20Sopenharmony_cixrep_agfl_walk_rmap(
4438c2ecf20Sopenharmony_ci	struct xfs_btree_cur	*cur,
4448c2ecf20Sopenharmony_ci	struct xfs_rmap_irec	*rec,
4458c2ecf20Sopenharmony_ci	void			*priv)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct xrep_agfl	*ra = priv;
4488c2ecf20Sopenharmony_ci	xfs_fsblock_t		fsb;
4498c2ecf20Sopenharmony_ci	int			error = 0;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if (xchk_should_terminate(ra->sc, &error))
4528c2ecf20Sopenharmony_ci		return error;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	/* Record all the OWN_AG blocks. */
4558c2ecf20Sopenharmony_ci	if (rec->rm_owner == XFS_RMAP_OWN_AG) {
4568c2ecf20Sopenharmony_ci		fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.agno,
4578c2ecf20Sopenharmony_ci				rec->rm_startblock);
4588c2ecf20Sopenharmony_ci		error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
4598c2ecf20Sopenharmony_ci		if (error)
4608c2ecf20Sopenharmony_ci			return error;
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci/*
4678c2ecf20Sopenharmony_ci * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
4688c2ecf20Sopenharmony_ci * which blocks belong to the AGFL.
4698c2ecf20Sopenharmony_ci *
4708c2ecf20Sopenharmony_ci * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
4718c2ecf20Sopenharmony_ci * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
4728c2ecf20Sopenharmony_ci * rmapbt).  These are the old AGFL blocks, so return that list and the number
4738c2ecf20Sopenharmony_ci * of blocks we're actually going to put back on the AGFL.
4748c2ecf20Sopenharmony_ci */
4758c2ecf20Sopenharmony_ciSTATIC int
4768c2ecf20Sopenharmony_cixrep_agfl_collect_blocks(
4778c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
4788c2ecf20Sopenharmony_ci	struct xfs_buf		*agf_bp,
4798c2ecf20Sopenharmony_ci	struct xbitmap		*agfl_extents,
4808c2ecf20Sopenharmony_ci	xfs_agblock_t		*flcount)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	struct xrep_agfl	ra;
4838c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
4848c2ecf20Sopenharmony_ci	struct xfs_btree_cur	*cur;
4858c2ecf20Sopenharmony_ci	int			error;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	ra.sc = sc;
4888c2ecf20Sopenharmony_ci	ra.freesp = agfl_extents;
4898c2ecf20Sopenharmony_ci	xbitmap_init(&ra.agmetablocks);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/* Find all space used by the free space btrees & rmapbt. */
4928c2ecf20Sopenharmony_ci	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
4938c2ecf20Sopenharmony_ci	error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
4948c2ecf20Sopenharmony_ci	if (error)
4958c2ecf20Sopenharmony_ci		goto err;
4968c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	/* Find all blocks currently being used by the bnobt. */
4998c2ecf20Sopenharmony_ci	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
5008c2ecf20Sopenharmony_ci			XFS_BTNUM_BNO);
5018c2ecf20Sopenharmony_ci	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
5028c2ecf20Sopenharmony_ci	if (error)
5038c2ecf20Sopenharmony_ci		goto err;
5048c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* Find all blocks currently being used by the cntbt. */
5078c2ecf20Sopenharmony_ci	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
5088c2ecf20Sopenharmony_ci			XFS_BTNUM_CNT);
5098c2ecf20Sopenharmony_ci	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
5108c2ecf20Sopenharmony_ci	if (error)
5118c2ecf20Sopenharmony_ci		goto err;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/*
5168c2ecf20Sopenharmony_ci	 * Drop the freesp meta blocks that are in use by btrees.
5178c2ecf20Sopenharmony_ci	 * The remaining blocks /should/ be AGFL blocks.
5188c2ecf20Sopenharmony_ci	 */
5198c2ecf20Sopenharmony_ci	error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
5208c2ecf20Sopenharmony_ci	xbitmap_destroy(&ra.agmetablocks);
5218c2ecf20Sopenharmony_ci	if (error)
5228c2ecf20Sopenharmony_ci		return error;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	/*
5258c2ecf20Sopenharmony_ci	 * Calculate the new AGFL size.  If we found more blocks than fit in
5268c2ecf20Sopenharmony_ci	 * the AGFL we'll free them later.
5278c2ecf20Sopenharmony_ci	 */
5288c2ecf20Sopenharmony_ci	*flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
5298c2ecf20Sopenharmony_ci			 xfs_agfl_size(mp));
5308c2ecf20Sopenharmony_ci	return 0;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cierr:
5338c2ecf20Sopenharmony_ci	xbitmap_destroy(&ra.agmetablocks);
5348c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
5358c2ecf20Sopenharmony_ci	return error;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/* Update the AGF and reset the in-core state. */
5398c2ecf20Sopenharmony_ciSTATIC void
5408c2ecf20Sopenharmony_cixrep_agfl_update_agf(
5418c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
5428c2ecf20Sopenharmony_ci	struct xfs_buf		*agf_bp,
5438c2ecf20Sopenharmony_ci	xfs_agblock_t		flcount)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	struct xfs_agf		*agf = agf_bp->b_addr;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	ASSERT(flcount <= xfs_agfl_size(sc->mp));
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	/* Trigger fdblocks recalculation */
5508c2ecf20Sopenharmony_ci	xfs_force_summary_recalc(sc->mp);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* Update the AGF counters. */
5538c2ecf20Sopenharmony_ci	if (sc->sa.pag->pagf_init)
5548c2ecf20Sopenharmony_ci		sc->sa.pag->pagf_flcount = flcount;
5558c2ecf20Sopenharmony_ci	agf->agf_flfirst = cpu_to_be32(0);
5568c2ecf20Sopenharmony_ci	agf->agf_flcount = cpu_to_be32(flcount);
5578c2ecf20Sopenharmony_ci	agf->agf_fllast = cpu_to_be32(flcount - 1);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	xfs_alloc_log_agf(sc->tp, agf_bp,
5608c2ecf20Sopenharmony_ci			XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci/* Write out a totally new AGFL. */
5648c2ecf20Sopenharmony_ciSTATIC void
5658c2ecf20Sopenharmony_cixrep_agfl_init_header(
5668c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
5678c2ecf20Sopenharmony_ci	struct xfs_buf		*agfl_bp,
5688c2ecf20Sopenharmony_ci	struct xbitmap		*agfl_extents,
5698c2ecf20Sopenharmony_ci	xfs_agblock_t		flcount)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
5728c2ecf20Sopenharmony_ci	__be32			*agfl_bno;
5738c2ecf20Sopenharmony_ci	struct xbitmap_range	*br;
5748c2ecf20Sopenharmony_ci	struct xbitmap_range	*n;
5758c2ecf20Sopenharmony_ci	struct xfs_agfl		*agfl;
5768c2ecf20Sopenharmony_ci	xfs_agblock_t		agbno;
5778c2ecf20Sopenharmony_ci	unsigned int		fl_off;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	ASSERT(flcount <= xfs_agfl_size(mp));
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	/*
5828c2ecf20Sopenharmony_ci	 * Start rewriting the header by setting the bno[] array to
5838c2ecf20Sopenharmony_ci	 * NULLAGBLOCK, then setting AGFL header fields.
5848c2ecf20Sopenharmony_ci	 */
5858c2ecf20Sopenharmony_ci	agfl = XFS_BUF_TO_AGFL(agfl_bp);
5868c2ecf20Sopenharmony_ci	memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
5878c2ecf20Sopenharmony_ci	agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
5888c2ecf20Sopenharmony_ci	agfl->agfl_seqno = cpu_to_be32(sc->sa.agno);
5898c2ecf20Sopenharmony_ci	uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/*
5928c2ecf20Sopenharmony_ci	 * Fill the AGFL with the remaining blocks.  If agfl_extents has more
5938c2ecf20Sopenharmony_ci	 * blocks than fit in the AGFL, they will be freed in a subsequent
5948c2ecf20Sopenharmony_ci	 * step.
5958c2ecf20Sopenharmony_ci	 */
5968c2ecf20Sopenharmony_ci	fl_off = 0;
5978c2ecf20Sopenharmony_ci	agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
5988c2ecf20Sopenharmony_ci	for_each_xbitmap_extent(br, n, agfl_extents) {
5998c2ecf20Sopenharmony_ci		agbno = XFS_FSB_TO_AGBNO(mp, br->start);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci		trace_xrep_agfl_insert(mp, sc->sa.agno, agbno, br->len);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		while (br->len > 0 && fl_off < flcount) {
6048c2ecf20Sopenharmony_ci			agfl_bno[fl_off] = cpu_to_be32(agbno);
6058c2ecf20Sopenharmony_ci			fl_off++;
6068c2ecf20Sopenharmony_ci			agbno++;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci			/*
6098c2ecf20Sopenharmony_ci			 * We've now used br->start by putting it in the AGFL,
6108c2ecf20Sopenharmony_ci			 * so bump br so that we don't reap the block later.
6118c2ecf20Sopenharmony_ci			 */
6128c2ecf20Sopenharmony_ci			br->start++;
6138c2ecf20Sopenharmony_ci			br->len--;
6148c2ecf20Sopenharmony_ci		}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci		if (br->len)
6178c2ecf20Sopenharmony_ci			break;
6188c2ecf20Sopenharmony_ci		list_del(&br->list);
6198c2ecf20Sopenharmony_ci		kmem_free(br);
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	/* Write new AGFL to disk. */
6238c2ecf20Sopenharmony_ci	xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
6248c2ecf20Sopenharmony_ci	xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci/* Repair the AGFL. */
6288c2ecf20Sopenharmony_ciint
6298c2ecf20Sopenharmony_cixrep_agfl(
6308c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct xbitmap		agfl_extents;
6338c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
6348c2ecf20Sopenharmony_ci	struct xfs_buf		*agf_bp;
6358c2ecf20Sopenharmony_ci	struct xfs_buf		*agfl_bp;
6368c2ecf20Sopenharmony_ci	xfs_agblock_t		flcount;
6378c2ecf20Sopenharmony_ci	int			error;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* We require the rmapbt to rebuild anything. */
6408c2ecf20Sopenharmony_ci	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
6418c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	xchk_perag_get(sc->mp, &sc->sa);
6448c2ecf20Sopenharmony_ci	xbitmap_init(&agfl_extents);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	/*
6478c2ecf20Sopenharmony_ci	 * Read the AGF so that we can query the rmapbt.  We hope that there's
6488c2ecf20Sopenharmony_ci	 * nothing wrong with the AGF, but all the AG header repair functions
6498c2ecf20Sopenharmony_ci	 * have this chicken-and-egg problem.
6508c2ecf20Sopenharmony_ci	 */
6518c2ecf20Sopenharmony_ci	error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
6528c2ecf20Sopenharmony_ci	if (error)
6538c2ecf20Sopenharmony_ci		return error;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	/*
6568c2ecf20Sopenharmony_ci	 * Make sure we have the AGFL buffer, as scrub might have decided it
6578c2ecf20Sopenharmony_ci	 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
6588c2ecf20Sopenharmony_ci	 */
6598c2ecf20Sopenharmony_ci	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
6608c2ecf20Sopenharmony_ci			XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGFL_DADDR(mp)),
6618c2ecf20Sopenharmony_ci			XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
6628c2ecf20Sopenharmony_ci	if (error)
6638c2ecf20Sopenharmony_ci		return error;
6648c2ecf20Sopenharmony_ci	agfl_bp->b_ops = &xfs_agfl_buf_ops;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	/* Gather all the extents we're going to put on the new AGFL. */
6678c2ecf20Sopenharmony_ci	error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
6688c2ecf20Sopenharmony_ci	if (error)
6698c2ecf20Sopenharmony_ci		goto err;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	/*
6728c2ecf20Sopenharmony_ci	 * Update AGF and AGFL.  We reset the global free block counter when
6738c2ecf20Sopenharmony_ci	 * we adjust the AGF flcount (which can fail) so avoid updating any
6748c2ecf20Sopenharmony_ci	 * buffers until we know that part works.
6758c2ecf20Sopenharmony_ci	 */
6768c2ecf20Sopenharmony_ci	xrep_agfl_update_agf(sc, agf_bp, flcount);
6778c2ecf20Sopenharmony_ci	xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	/*
6808c2ecf20Sopenharmony_ci	 * Ok, the AGFL should be ready to go now.  Roll the transaction to
6818c2ecf20Sopenharmony_ci	 * make the new AGFL permanent before we start using it to return
6828c2ecf20Sopenharmony_ci	 * freespace overflow to the freespace btrees.
6838c2ecf20Sopenharmony_ci	 */
6848c2ecf20Sopenharmony_ci	sc->sa.agf_bp = agf_bp;
6858c2ecf20Sopenharmony_ci	sc->sa.agfl_bp = agfl_bp;
6868c2ecf20Sopenharmony_ci	error = xrep_roll_ag_trans(sc);
6878c2ecf20Sopenharmony_ci	if (error)
6888c2ecf20Sopenharmony_ci		goto err;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/* Dump any AGFL overflow. */
6918c2ecf20Sopenharmony_ci	error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
6928c2ecf20Sopenharmony_ci			XFS_AG_RESV_AGFL);
6938c2ecf20Sopenharmony_cierr:
6948c2ecf20Sopenharmony_ci	xbitmap_destroy(&agfl_extents);
6958c2ecf20Sopenharmony_ci	return error;
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci/* AGI */
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci/*
7018c2ecf20Sopenharmony_ci * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
7028c2ecf20Sopenharmony_ci * XFS_BTNUM_ names here to avoid creating a sparse array.
7038c2ecf20Sopenharmony_ci */
7048c2ecf20Sopenharmony_cienum {
7058c2ecf20Sopenharmony_ci	XREP_AGI_INOBT = 0,
7068c2ecf20Sopenharmony_ci	XREP_AGI_FINOBT,
7078c2ecf20Sopenharmony_ci	XREP_AGI_END,
7088c2ecf20Sopenharmony_ci	XREP_AGI_MAX
7098c2ecf20Sopenharmony_ci};
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci/*
7128c2ecf20Sopenharmony_ci * Given the inode btree roots described by *fab, find the roots, check them
7138c2ecf20Sopenharmony_ci * for sanity, and pass the root data back out via *fab.
7148c2ecf20Sopenharmony_ci */
7158c2ecf20Sopenharmony_ciSTATIC int
7168c2ecf20Sopenharmony_cixrep_agi_find_btrees(
7178c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc,
7188c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	*fab)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	struct xfs_buf			*agf_bp;
7218c2ecf20Sopenharmony_ci	struct xfs_mount		*mp = sc->mp;
7228c2ecf20Sopenharmony_ci	int				error;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	/* Read the AGF. */
7258c2ecf20Sopenharmony_ci	error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
7268c2ecf20Sopenharmony_ci	if (error)
7278c2ecf20Sopenharmony_ci		return error;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	/* Find the btree roots. */
7308c2ecf20Sopenharmony_ci	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
7318c2ecf20Sopenharmony_ci	if (error)
7328c2ecf20Sopenharmony_ci		return error;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	/* We must find the inobt root. */
7358c2ecf20Sopenharmony_ci	if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
7368c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	/* We must find the finobt root if that feature is enabled. */
7398c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
7408c2ecf20Sopenharmony_ci	    !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
7418c2ecf20Sopenharmony_ci		return -EFSCORRUPTED;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	return 0;
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci/*
7478c2ecf20Sopenharmony_ci * Reinitialize the AGI header, making an in-core copy of the old contents so
7488c2ecf20Sopenharmony_ci * that we know which in-core state needs to be reinitialized.
7498c2ecf20Sopenharmony_ci */
7508c2ecf20Sopenharmony_ciSTATIC void
7518c2ecf20Sopenharmony_cixrep_agi_init_header(
7528c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
7538c2ecf20Sopenharmony_ci	struct xfs_buf		*agi_bp,
7548c2ecf20Sopenharmony_ci	struct xfs_agi		*old_agi)
7558c2ecf20Sopenharmony_ci{
7568c2ecf20Sopenharmony_ci	struct xfs_agi		*agi = agi_bp->b_addr;
7578c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	memcpy(old_agi, agi, sizeof(*old_agi));
7608c2ecf20Sopenharmony_ci	memset(agi, 0, BBTOB(agi_bp->b_length));
7618c2ecf20Sopenharmony_ci	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
7628c2ecf20Sopenharmony_ci	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
7638c2ecf20Sopenharmony_ci	agi->agi_seqno = cpu_to_be32(sc->sa.agno);
7648c2ecf20Sopenharmony_ci	agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
7658c2ecf20Sopenharmony_ci	agi->agi_newino = cpu_to_be32(NULLAGINO);
7668c2ecf20Sopenharmony_ci	agi->agi_dirino = cpu_to_be32(NULLAGINO);
7678c2ecf20Sopenharmony_ci	if (xfs_sb_version_hascrc(&mp->m_sb))
7688c2ecf20Sopenharmony_ci		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* We don't know how to fix the unlinked list yet. */
7718c2ecf20Sopenharmony_ci	memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
7728c2ecf20Sopenharmony_ci			sizeof(agi->agi_unlinked));
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	/* Mark the incore AGF data stale until we're done fixing things. */
7758c2ecf20Sopenharmony_ci	ASSERT(sc->sa.pag->pagi_init);
7768c2ecf20Sopenharmony_ci	sc->sa.pag->pagi_init = 0;
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci/* Set btree root information in an AGI. */
7808c2ecf20Sopenharmony_ciSTATIC void
7818c2ecf20Sopenharmony_cixrep_agi_set_roots(
7828c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc,
7838c2ecf20Sopenharmony_ci	struct xfs_agi			*agi,
7848c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	*fab)
7858c2ecf20Sopenharmony_ci{
7868c2ecf20Sopenharmony_ci	agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
7878c2ecf20Sopenharmony_ci	agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) {
7908c2ecf20Sopenharmony_ci		agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
7918c2ecf20Sopenharmony_ci		agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci/* Update the AGI counters. */
7968c2ecf20Sopenharmony_ciSTATIC int
7978c2ecf20Sopenharmony_cixrep_agi_calc_from_btrees(
7988c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
7998c2ecf20Sopenharmony_ci	struct xfs_buf		*agi_bp)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	struct xfs_btree_cur	*cur;
8028c2ecf20Sopenharmony_ci	struct xfs_agi		*agi = agi_bp->b_addr;
8038c2ecf20Sopenharmony_ci	struct xfs_mount	*mp = sc->mp;
8048c2ecf20Sopenharmony_ci	xfs_agino_t		count;
8058c2ecf20Sopenharmony_ci	xfs_agino_t		freecount;
8068c2ecf20Sopenharmony_ci	int			error;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno,
8098c2ecf20Sopenharmony_ci			XFS_BTNUM_INO);
8108c2ecf20Sopenharmony_ci	error = xfs_ialloc_count_inodes(cur, &count, &freecount);
8118c2ecf20Sopenharmony_ci	if (error)
8128c2ecf20Sopenharmony_ci		goto err;
8138c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
8148c2ecf20Sopenharmony_ci		xfs_agblock_t	blocks;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		error = xfs_btree_count_blocks(cur, &blocks);
8178c2ecf20Sopenharmony_ci		if (error)
8188c2ecf20Sopenharmony_ci			goto err;
8198c2ecf20Sopenharmony_ci		agi->agi_iblocks = cpu_to_be32(blocks);
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	agi->agi_count = cpu_to_be32(count);
8248c2ecf20Sopenharmony_ci	agi->agi_freecount = cpu_to_be32(freecount);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
8278c2ecf20Sopenharmony_ci	    xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
8288c2ecf20Sopenharmony_ci		xfs_agblock_t	blocks;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci		cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno,
8318c2ecf20Sopenharmony_ci				XFS_BTNUM_FINO);
8328c2ecf20Sopenharmony_ci		if (error)
8338c2ecf20Sopenharmony_ci			goto err;
8348c2ecf20Sopenharmony_ci		error = xfs_btree_count_blocks(cur, &blocks);
8358c2ecf20Sopenharmony_ci		if (error)
8368c2ecf20Sopenharmony_ci			goto err;
8378c2ecf20Sopenharmony_ci		xfs_btree_del_cursor(cur, error);
8388c2ecf20Sopenharmony_ci		agi->agi_fblocks = cpu_to_be32(blocks);
8398c2ecf20Sopenharmony_ci	}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	return 0;
8428c2ecf20Sopenharmony_cierr:
8438c2ecf20Sopenharmony_ci	xfs_btree_del_cursor(cur, error);
8448c2ecf20Sopenharmony_ci	return error;
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci/* Trigger reinitialization of the in-core data. */
8488c2ecf20Sopenharmony_ciSTATIC int
8498c2ecf20Sopenharmony_cixrep_agi_commit_new(
8508c2ecf20Sopenharmony_ci	struct xfs_scrub	*sc,
8518c2ecf20Sopenharmony_ci	struct xfs_buf		*agi_bp)
8528c2ecf20Sopenharmony_ci{
8538c2ecf20Sopenharmony_ci	struct xfs_perag	*pag;
8548c2ecf20Sopenharmony_ci	struct xfs_agi		*agi = agi_bp->b_addr;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	/* Trigger inode count recalculation */
8578c2ecf20Sopenharmony_ci	xfs_force_summary_recalc(sc->mp);
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	/* Write this to disk. */
8608c2ecf20Sopenharmony_ci	xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
8618c2ecf20Sopenharmony_ci	xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	/* Now reinitialize the in-core counters if necessary. */
8648c2ecf20Sopenharmony_ci	pag = sc->sa.pag;
8658c2ecf20Sopenharmony_ci	pag->pagi_count = be32_to_cpu(agi->agi_count);
8668c2ecf20Sopenharmony_ci	pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
8678c2ecf20Sopenharmony_ci	pag->pagi_init = 1;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	return 0;
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci/* Repair the AGI. */
8738c2ecf20Sopenharmony_ciint
8748c2ecf20Sopenharmony_cixrep_agi(
8758c2ecf20Sopenharmony_ci	struct xfs_scrub		*sc)
8768c2ecf20Sopenharmony_ci{
8778c2ecf20Sopenharmony_ci	struct xrep_find_ag_btree	fab[XREP_AGI_MAX] = {
8788c2ecf20Sopenharmony_ci		[XREP_AGI_INOBT] = {
8798c2ecf20Sopenharmony_ci			.rmap_owner = XFS_RMAP_OWN_INOBT,
8808c2ecf20Sopenharmony_ci			.buf_ops = &xfs_inobt_buf_ops,
8818c2ecf20Sopenharmony_ci		},
8828c2ecf20Sopenharmony_ci		[XREP_AGI_FINOBT] = {
8838c2ecf20Sopenharmony_ci			.rmap_owner = XFS_RMAP_OWN_INOBT,
8848c2ecf20Sopenharmony_ci			.buf_ops = &xfs_finobt_buf_ops,
8858c2ecf20Sopenharmony_ci		},
8868c2ecf20Sopenharmony_ci		[XREP_AGI_END] = {
8878c2ecf20Sopenharmony_ci			.buf_ops = NULL
8888c2ecf20Sopenharmony_ci		},
8898c2ecf20Sopenharmony_ci	};
8908c2ecf20Sopenharmony_ci	struct xfs_agi			old_agi;
8918c2ecf20Sopenharmony_ci	struct xfs_mount		*mp = sc->mp;
8928c2ecf20Sopenharmony_ci	struct xfs_buf			*agi_bp;
8938c2ecf20Sopenharmony_ci	struct xfs_agi			*agi;
8948c2ecf20Sopenharmony_ci	int				error;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	/* We require the rmapbt to rebuild anything. */
8978c2ecf20Sopenharmony_ci	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
8988c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	xchk_perag_get(sc->mp, &sc->sa);
9018c2ecf20Sopenharmony_ci	/*
9028c2ecf20Sopenharmony_ci	 * Make sure we have the AGI buffer, as scrub might have decided it
9038c2ecf20Sopenharmony_ci	 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
9048c2ecf20Sopenharmony_ci	 */
9058c2ecf20Sopenharmony_ci	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
9068c2ecf20Sopenharmony_ci			XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGI_DADDR(mp)),
9078c2ecf20Sopenharmony_ci			XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
9088c2ecf20Sopenharmony_ci	if (error)
9098c2ecf20Sopenharmony_ci		return error;
9108c2ecf20Sopenharmony_ci	agi_bp->b_ops = &xfs_agi_buf_ops;
9118c2ecf20Sopenharmony_ci	agi = agi_bp->b_addr;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	/* Find the AGI btree roots. */
9148c2ecf20Sopenharmony_ci	error = xrep_agi_find_btrees(sc, fab);
9158c2ecf20Sopenharmony_ci	if (error)
9168c2ecf20Sopenharmony_ci		return error;
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	/* Start rewriting the header and implant the btrees we found. */
9198c2ecf20Sopenharmony_ci	xrep_agi_init_header(sc, agi_bp, &old_agi);
9208c2ecf20Sopenharmony_ci	xrep_agi_set_roots(sc, agi, fab);
9218c2ecf20Sopenharmony_ci	error = xrep_agi_calc_from_btrees(sc, agi_bp);
9228c2ecf20Sopenharmony_ci	if (error)
9238c2ecf20Sopenharmony_ci		goto out_revert;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	/* Reinitialize in-core state. */
9268c2ecf20Sopenharmony_ci	return xrep_agi_commit_new(sc, agi_bp);
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ciout_revert:
9298c2ecf20Sopenharmony_ci	/* Mark the incore AGI state stale and revert the AGI. */
9308c2ecf20Sopenharmony_ci	sc->sa.pag->pagi_init = 0;
9318c2ecf20Sopenharmony_ci	memcpy(agi, &old_agi, sizeof(old_agi));
9328c2ecf20Sopenharmony_ci	return error;
9338c2ecf20Sopenharmony_ci}
934