xref: /openbmc/linux/fs/xfs/scrub/agheader_repair.c (revision be9fb17d88f08af648a89784d30dbac83d893154)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0+
2d25522f1SDarrick J. Wong /*
3d25522f1SDarrick J. Wong  * Copyright (C) 2018 Oracle.  All Rights Reserved.
4d25522f1SDarrick J. Wong  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5d25522f1SDarrick J. Wong  */
6d25522f1SDarrick J. Wong #include "xfs.h"
7d25522f1SDarrick J. Wong #include "xfs_fs.h"
8d25522f1SDarrick J. Wong #include "xfs_shared.h"
9d25522f1SDarrick J. Wong #include "xfs_format.h"
10d25522f1SDarrick J. Wong #include "xfs_trans_resv.h"
11d25522f1SDarrick J. Wong #include "xfs_mount.h"
12d25522f1SDarrick J. Wong #include "xfs_btree.h"
13d25522f1SDarrick J. Wong #include "xfs_log_format.h"
14d25522f1SDarrick J. Wong #include "xfs_trans.h"
15d25522f1SDarrick J. Wong #include "xfs_sb.h"
16d25522f1SDarrick J. Wong #include "xfs_alloc.h"
17f9ed6debSDarrick J. Wong #include "xfs_alloc_btree.h"
18d25522f1SDarrick J. Wong #include "xfs_ialloc.h"
19f9ed6debSDarrick J. Wong #include "xfs_ialloc_btree.h"
20d25522f1SDarrick J. Wong #include "xfs_rmap.h"
21f9ed6debSDarrick J. Wong #include "xfs_rmap_btree.h"
22f9ed6debSDarrick J. Wong #include "xfs_refcount_btree.h"
239bbafc71SDave Chinner #include "xfs_ag.h"
24d25522f1SDarrick J. Wong #include "scrub/scrub.h"
25d25522f1SDarrick J. Wong #include "scrub/common.h"
26d25522f1SDarrick J. Wong #include "scrub/trace.h"
27f9ed6debSDarrick J. Wong #include "scrub/repair.h"
28f9ed6debSDarrick J. Wong #include "scrub/bitmap.h"
29d25522f1SDarrick J. Wong 
30d25522f1SDarrick J. Wong /* Superblock */
31d25522f1SDarrick J. Wong 
32d25522f1SDarrick J. Wong /* Repair the superblock. */
33d25522f1SDarrick J. Wong int
34b5e2196eSDarrick J. Wong xrep_superblock(
351d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc)
36d25522f1SDarrick J. Wong {
37d25522f1SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
38d25522f1SDarrick J. Wong 	struct xfs_buf		*bp;
39d25522f1SDarrick J. Wong 	xfs_agnumber_t		agno;
40d25522f1SDarrick J. Wong 	int			error;
41d25522f1SDarrick J. Wong 
42d25522f1SDarrick J. Wong 	/* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
43d25522f1SDarrick J. Wong 	agno = sc->sm->sm_agno;
44d25522f1SDarrick J. Wong 	if (agno == 0)
45d25522f1SDarrick J. Wong 		return -EOPNOTSUPP;
46d25522f1SDarrick J. Wong 
47d25522f1SDarrick J. Wong 	error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
48d25522f1SDarrick J. Wong 	if (error)
49d25522f1SDarrick J. Wong 		return error;
50d25522f1SDarrick J. Wong 
51d25522f1SDarrick J. Wong 	/* Copy AG 0's superblock to this one. */
52d25522f1SDarrick J. Wong 	xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
533e6e8afdSChristoph Hellwig 	xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
54d25522f1SDarrick J. Wong 
55d25522f1SDarrick J. Wong 	/* Write this to disk. */
56d25522f1SDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
57d25522f1SDarrick J. Wong 	xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
58d25522f1SDarrick J. Wong 	return error;
59d25522f1SDarrick J. Wong }
60f9ed6debSDarrick J. Wong 
61f9ed6debSDarrick J. Wong /* AGF */
62f9ed6debSDarrick J. Wong 
63f9ed6debSDarrick J. Wong struct xrep_agf_allocbt {
64f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc;
65f9ed6debSDarrick J. Wong 	xfs_agblock_t		freeblks;
66f9ed6debSDarrick J. Wong 	xfs_agblock_t		longest;
67f9ed6debSDarrick J. Wong };
68f9ed6debSDarrick J. Wong 
69f9ed6debSDarrick J. Wong /* Record free space shape information. */
70f9ed6debSDarrick J. Wong STATIC int
71f9ed6debSDarrick J. Wong xrep_agf_walk_allocbt(
72f9ed6debSDarrick J. Wong 	struct xfs_btree_cur		*cur,
73f9ed6debSDarrick J. Wong 	struct xfs_alloc_rec_incore	*rec,
74f9ed6debSDarrick J. Wong 	void				*priv)
75f9ed6debSDarrick J. Wong {
76f9ed6debSDarrick J. Wong 	struct xrep_agf_allocbt		*raa = priv;
77f9ed6debSDarrick J. Wong 	int				error = 0;
78f9ed6debSDarrick J. Wong 
79f9ed6debSDarrick J. Wong 	if (xchk_should_terminate(raa->sc, &error))
80f9ed6debSDarrick J. Wong 		return error;
81f9ed6debSDarrick J. Wong 
82f9ed6debSDarrick J. Wong 	raa->freeblks += rec->ar_blockcount;
83f9ed6debSDarrick J. Wong 	if (rec->ar_blockcount > raa->longest)
84f9ed6debSDarrick J. Wong 		raa->longest = rec->ar_blockcount;
85f9ed6debSDarrick J. Wong 	return error;
86f9ed6debSDarrick J. Wong }
87f9ed6debSDarrick J. Wong 
88f9ed6debSDarrick J. Wong /* Does this AGFL block look sane? */
89f9ed6debSDarrick J. Wong STATIC int
90f9ed6debSDarrick J. Wong xrep_agf_check_agfl_block(
91f9ed6debSDarrick J. Wong 	struct xfs_mount	*mp,
92f9ed6debSDarrick J. Wong 	xfs_agblock_t		agbno,
93f9ed6debSDarrick J. Wong 	void			*priv)
94f9ed6debSDarrick J. Wong {
95f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc = priv;
96f9ed6debSDarrick J. Wong 
97f9ed6debSDarrick J. Wong 	if (!xfs_verify_agbno(mp, sc->sa.agno, agbno))
98f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
99f9ed6debSDarrick J. Wong 	return 0;
100f9ed6debSDarrick J. Wong }
101f9ed6debSDarrick J. Wong 
102f9ed6debSDarrick J. Wong /*
103f9ed6debSDarrick J. Wong  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
104f9ed6debSDarrick J. Wong  * XFS_BTNUM_ names here to avoid creating a sparse array.
105f9ed6debSDarrick J. Wong  */
106f9ed6debSDarrick J. Wong enum {
107f9ed6debSDarrick J. Wong 	XREP_AGF_BNOBT = 0,
108f9ed6debSDarrick J. Wong 	XREP_AGF_CNTBT,
109f9ed6debSDarrick J. Wong 	XREP_AGF_RMAPBT,
110f9ed6debSDarrick J. Wong 	XREP_AGF_REFCOUNTBT,
111f9ed6debSDarrick J. Wong 	XREP_AGF_END,
112f9ed6debSDarrick J. Wong 	XREP_AGF_MAX
113f9ed6debSDarrick J. Wong };
114f9ed6debSDarrick J. Wong 
115f9ed6debSDarrick J. Wong /* Check a btree root candidate. */
116f9ed6debSDarrick J. Wong static inline bool
117f9ed6debSDarrick J. Wong xrep_check_btree_root(
118f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc,
119f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
120f9ed6debSDarrick J. Wong {
121f9ed6debSDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
122f9ed6debSDarrick J. Wong 	xfs_agnumber_t			agno = sc->sm->sm_agno;
123f9ed6debSDarrick J. Wong 
124f9ed6debSDarrick J. Wong 	return xfs_verify_agbno(mp, agno, fab->root) &&
125f9ed6debSDarrick J. Wong 	       fab->height <= XFS_BTREE_MAXLEVELS;
126f9ed6debSDarrick J. Wong }
127f9ed6debSDarrick J. Wong 
128f9ed6debSDarrick J. Wong /*
129f9ed6debSDarrick J. Wong  * Given the btree roots described by *fab, find the roots, check them for
130f9ed6debSDarrick J. Wong  * sanity, and pass the root data back out via *fab.
131f9ed6debSDarrick J. Wong  *
132f9ed6debSDarrick J. Wong  * This is /also/ a chicken and egg problem because we have to use the rmapbt
133f9ed6debSDarrick J. Wong  * (rooted in the AGF) to find the btrees rooted in the AGF.  We also have no
134f9ed6debSDarrick J. Wong  * idea if the btrees make any sense.  If we hit obvious corruptions in those
135f9ed6debSDarrick J. Wong  * btrees we'll bail out.
136f9ed6debSDarrick J. Wong  */
137f9ed6debSDarrick J. Wong STATIC int
138f9ed6debSDarrick J. Wong xrep_agf_find_btrees(
139f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc,
140f9ed6debSDarrick J. Wong 	struct xfs_buf			*agf_bp,
141f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	*fab,
142f9ed6debSDarrick J. Wong 	struct xfs_buf			*agfl_bp)
143f9ed6debSDarrick J. Wong {
1449798f615SChristoph Hellwig 	struct xfs_agf			*old_agf = agf_bp->b_addr;
145f9ed6debSDarrick J. Wong 	int				error;
146f9ed6debSDarrick J. Wong 
147f9ed6debSDarrick J. Wong 	/* Go find the root data. */
148f9ed6debSDarrick J. Wong 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
149f9ed6debSDarrick J. Wong 	if (error)
150f9ed6debSDarrick J. Wong 		return error;
151f9ed6debSDarrick J. Wong 
152f9ed6debSDarrick J. Wong 	/* We must find the bnobt, cntbt, and rmapbt roots. */
153f9ed6debSDarrick J. Wong 	if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
154f9ed6debSDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
155f9ed6debSDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
156f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
157f9ed6debSDarrick J. Wong 
158f9ed6debSDarrick J. Wong 	/*
159f9ed6debSDarrick J. Wong 	 * We relied on the rmapbt to reconstruct the AGF.  If we get a
160f9ed6debSDarrick J. Wong 	 * different root then something's seriously wrong.
161f9ed6debSDarrick J. Wong 	 */
162f9ed6debSDarrick J. Wong 	if (fab[XREP_AGF_RMAPBT].root !=
163f9ed6debSDarrick J. Wong 	    be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
164f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
165f9ed6debSDarrick J. Wong 
166f9ed6debSDarrick J. Wong 	/* We must find the refcountbt root if that feature is enabled. */
167f9ed6debSDarrick J. Wong 	if (xfs_sb_version_hasreflink(&sc->mp->m_sb) &&
168f9ed6debSDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
169f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
170f9ed6debSDarrick J. Wong 
171f9ed6debSDarrick J. Wong 	return 0;
172f9ed6debSDarrick J. Wong }
173f9ed6debSDarrick J. Wong 
174f9ed6debSDarrick J. Wong /*
175f9ed6debSDarrick J. Wong  * Reinitialize the AGF header, making an in-core copy of the old contents so
176f9ed6debSDarrick J. Wong  * that we know which in-core state needs to be reinitialized.
177f9ed6debSDarrick J. Wong  */
178f9ed6debSDarrick J. Wong STATIC void
179f9ed6debSDarrick J. Wong xrep_agf_init_header(
180f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc,
181f9ed6debSDarrick J. Wong 	struct xfs_buf		*agf_bp,
182f9ed6debSDarrick J. Wong 	struct xfs_agf		*old_agf)
183f9ed6debSDarrick J. Wong {
184f9ed6debSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
1859798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
186f9ed6debSDarrick J. Wong 
187f9ed6debSDarrick J. Wong 	memcpy(old_agf, agf, sizeof(*old_agf));
188f9ed6debSDarrick J. Wong 	memset(agf, 0, BBTOB(agf_bp->b_length));
189f9ed6debSDarrick J. Wong 	agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
190f9ed6debSDarrick J. Wong 	agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
191f9ed6debSDarrick J. Wong 	agf->agf_seqno = cpu_to_be32(sc->sa.agno);
192f9ed6debSDarrick J. Wong 	agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
193f9ed6debSDarrick J. Wong 	agf->agf_flfirst = old_agf->agf_flfirst;
194f9ed6debSDarrick J. Wong 	agf->agf_fllast = old_agf->agf_fllast;
195f9ed6debSDarrick J. Wong 	agf->agf_flcount = old_agf->agf_flcount;
196f9ed6debSDarrick J. Wong 	if (xfs_sb_version_hascrc(&mp->m_sb))
197f9ed6debSDarrick J. Wong 		uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
198f9ed6debSDarrick J. Wong 
199f9ed6debSDarrick J. Wong 	/* Mark the incore AGF data stale until we're done fixing things. */
200f9ed6debSDarrick J. Wong 	ASSERT(sc->sa.pag->pagf_init);
201f9ed6debSDarrick J. Wong 	sc->sa.pag->pagf_init = 0;
202f9ed6debSDarrick J. Wong }
203f9ed6debSDarrick J. Wong 
204f9ed6debSDarrick J. Wong /* Set btree root information in an AGF. */
205f9ed6debSDarrick J. Wong STATIC void
206f9ed6debSDarrick J. Wong xrep_agf_set_roots(
207f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc,
208f9ed6debSDarrick J. Wong 	struct xfs_agf			*agf,
209f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
210f9ed6debSDarrick J. Wong {
211f9ed6debSDarrick J. Wong 	agf->agf_roots[XFS_BTNUM_BNOi] =
212f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_BNOBT].root);
213f9ed6debSDarrick J. Wong 	agf->agf_levels[XFS_BTNUM_BNOi] =
214f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_BNOBT].height);
215f9ed6debSDarrick J. Wong 
216f9ed6debSDarrick J. Wong 	agf->agf_roots[XFS_BTNUM_CNTi] =
217f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_CNTBT].root);
218f9ed6debSDarrick J. Wong 	agf->agf_levels[XFS_BTNUM_CNTi] =
219f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_CNTBT].height);
220f9ed6debSDarrick J. Wong 
221f9ed6debSDarrick J. Wong 	agf->agf_roots[XFS_BTNUM_RMAPi] =
222f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
223f9ed6debSDarrick J. Wong 	agf->agf_levels[XFS_BTNUM_RMAPi] =
224f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
225f9ed6debSDarrick J. Wong 
226f9ed6debSDarrick J. Wong 	if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
227f9ed6debSDarrick J. Wong 		agf->agf_refcount_root =
228f9ed6debSDarrick J. Wong 				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
229f9ed6debSDarrick J. Wong 		agf->agf_refcount_level =
230f9ed6debSDarrick J. Wong 				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
231f9ed6debSDarrick J. Wong 	}
232f9ed6debSDarrick J. Wong }
233f9ed6debSDarrick J. Wong 
234f9ed6debSDarrick J. Wong /* Update all AGF fields which derive from btree contents. */
235f9ed6debSDarrick J. Wong STATIC int
236f9ed6debSDarrick J. Wong xrep_agf_calc_from_btrees(
237f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc,
238f9ed6debSDarrick J. Wong 	struct xfs_buf		*agf_bp)
239f9ed6debSDarrick J. Wong {
240f9ed6debSDarrick J. Wong 	struct xrep_agf_allocbt	raa = { .sc = sc };
241f9ed6debSDarrick J. Wong 	struct xfs_btree_cur	*cur = NULL;
2429798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
243f9ed6debSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
244f9ed6debSDarrick J. Wong 	xfs_agblock_t		btreeblks;
245f9ed6debSDarrick J. Wong 	xfs_agblock_t		blocks;
246f9ed6debSDarrick J. Wong 	int			error;
247f9ed6debSDarrick J. Wong 
248f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the bnobt. */
249f9ed6debSDarrick J. Wong 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
250*be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_BNO);
251f9ed6debSDarrick J. Wong 	error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
252f9ed6debSDarrick J. Wong 	if (error)
253f9ed6debSDarrick J. Wong 		goto err;
254f9ed6debSDarrick J. Wong 	error = xfs_btree_count_blocks(cur, &blocks);
255f9ed6debSDarrick J. Wong 	if (error)
256f9ed6debSDarrick J. Wong 		goto err;
257f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
258f9ed6debSDarrick J. Wong 	btreeblks = blocks - 1;
259f9ed6debSDarrick J. Wong 	agf->agf_freeblks = cpu_to_be32(raa.freeblks);
260f9ed6debSDarrick J. Wong 	agf->agf_longest = cpu_to_be32(raa.longest);
261f9ed6debSDarrick J. Wong 
262f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the cntbt. */
263f9ed6debSDarrick J. Wong 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
264*be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_CNT);
265f9ed6debSDarrick J. Wong 	error = xfs_btree_count_blocks(cur, &blocks);
266f9ed6debSDarrick J. Wong 	if (error)
267f9ed6debSDarrick J. Wong 		goto err;
268f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
269f9ed6debSDarrick J. Wong 	btreeblks += blocks - 1;
270f9ed6debSDarrick J. Wong 
271f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the rmapbt. */
272*be9fb17dSDave Chinner 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
273*be9fb17dSDave Chinner 			sc->sa.pag);
274f9ed6debSDarrick J. Wong 	error = xfs_btree_count_blocks(cur, &blocks);
275f9ed6debSDarrick J. Wong 	if (error)
276f9ed6debSDarrick J. Wong 		goto err;
277f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
278f9ed6debSDarrick J. Wong 	agf->agf_rmap_blocks = cpu_to_be32(blocks);
279f9ed6debSDarrick J. Wong 	btreeblks += blocks - 1;
280f9ed6debSDarrick J. Wong 
281f9ed6debSDarrick J. Wong 	agf->agf_btreeblks = cpu_to_be32(btreeblks);
282f9ed6debSDarrick J. Wong 
283f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the refcountbt. */
284f9ed6debSDarrick J. Wong 	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
285f9ed6debSDarrick J. Wong 		cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
286*be9fb17dSDave Chinner 				sc->sa.agno, sc->sa.pag);
287f9ed6debSDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &blocks);
288f9ed6debSDarrick J. Wong 		if (error)
289f9ed6debSDarrick J. Wong 			goto err;
290f9ed6debSDarrick J. Wong 		xfs_btree_del_cursor(cur, error);
291f9ed6debSDarrick J. Wong 		agf->agf_refcount_blocks = cpu_to_be32(blocks);
292f9ed6debSDarrick J. Wong 	}
293f9ed6debSDarrick J. Wong 
294f9ed6debSDarrick J. Wong 	return 0;
295f9ed6debSDarrick J. Wong err:
296f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
297f9ed6debSDarrick J. Wong 	return error;
298f9ed6debSDarrick J. Wong }
299f9ed6debSDarrick J. Wong 
300f9ed6debSDarrick J. Wong /* Commit the new AGF and reinitialize the incore state. */
301f9ed6debSDarrick J. Wong STATIC int
302f9ed6debSDarrick J. Wong xrep_agf_commit_new(
303f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc,
304f9ed6debSDarrick J. Wong 	struct xfs_buf		*agf_bp)
305f9ed6debSDarrick J. Wong {
306f9ed6debSDarrick J. Wong 	struct xfs_perag	*pag;
3079798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
308f9ed6debSDarrick J. Wong 
309f9ed6debSDarrick J. Wong 	/* Trigger fdblocks recalculation */
310f9ed6debSDarrick J. Wong 	xfs_force_summary_recalc(sc->mp);
311f9ed6debSDarrick J. Wong 
312f9ed6debSDarrick J. Wong 	/* Write this to disk. */
313f9ed6debSDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
314f9ed6debSDarrick J. Wong 	xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
315f9ed6debSDarrick J. Wong 
316f9ed6debSDarrick J. Wong 	/* Now reinitialize the in-core counters we changed. */
317f9ed6debSDarrick J. Wong 	pag = sc->sa.pag;
318f9ed6debSDarrick J. Wong 	pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
319f9ed6debSDarrick J. Wong 	pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
320f9ed6debSDarrick J. Wong 	pag->pagf_longest = be32_to_cpu(agf->agf_longest);
321f9ed6debSDarrick J. Wong 	pag->pagf_levels[XFS_BTNUM_BNOi] =
322f9ed6debSDarrick J. Wong 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
323f9ed6debSDarrick J. Wong 	pag->pagf_levels[XFS_BTNUM_CNTi] =
324f9ed6debSDarrick J. Wong 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
325f9ed6debSDarrick J. Wong 	pag->pagf_levels[XFS_BTNUM_RMAPi] =
326f9ed6debSDarrick J. Wong 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
327f9ed6debSDarrick J. Wong 	pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
328f9ed6debSDarrick J. Wong 	pag->pagf_init = 1;
329f9ed6debSDarrick J. Wong 
330f9ed6debSDarrick J. Wong 	return 0;
331f9ed6debSDarrick J. Wong }
332f9ed6debSDarrick J. Wong 
333f9ed6debSDarrick J. Wong /* Repair the AGF. v5 filesystems only. */
334f9ed6debSDarrick J. Wong int
335f9ed6debSDarrick J. Wong xrep_agf(
336f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc)
337f9ed6debSDarrick J. Wong {
338f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	fab[XREP_AGF_MAX] = {
339f9ed6debSDarrick J. Wong 		[XREP_AGF_BNOBT] = {
340f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_AG,
34127df4f50SBrian Foster 			.buf_ops = &xfs_bnobt_buf_ops,
342f9ed6debSDarrick J. Wong 		},
343f9ed6debSDarrick J. Wong 		[XREP_AGF_CNTBT] = {
344f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_AG,
34527df4f50SBrian Foster 			.buf_ops = &xfs_cntbt_buf_ops,
346f9ed6debSDarrick J. Wong 		},
347f9ed6debSDarrick J. Wong 		[XREP_AGF_RMAPBT] = {
348f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_AG,
349f9ed6debSDarrick J. Wong 			.buf_ops = &xfs_rmapbt_buf_ops,
350f9ed6debSDarrick J. Wong 		},
351f9ed6debSDarrick J. Wong 		[XREP_AGF_REFCOUNTBT] = {
352f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_REFC,
353f9ed6debSDarrick J. Wong 			.buf_ops = &xfs_refcountbt_buf_ops,
354f9ed6debSDarrick J. Wong 		},
355f9ed6debSDarrick J. Wong 		[XREP_AGF_END] = {
356f9ed6debSDarrick J. Wong 			.buf_ops = NULL,
357f9ed6debSDarrick J. Wong 		},
358f9ed6debSDarrick J. Wong 	};
359f9ed6debSDarrick J. Wong 	struct xfs_agf			old_agf;
360f9ed6debSDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
361f9ed6debSDarrick J. Wong 	struct xfs_buf			*agf_bp;
362f9ed6debSDarrick J. Wong 	struct xfs_buf			*agfl_bp;
363f9ed6debSDarrick J. Wong 	struct xfs_agf			*agf;
364f9ed6debSDarrick J. Wong 	int				error;
365f9ed6debSDarrick J. Wong 
366f9ed6debSDarrick J. Wong 	/* We require the rmapbt to rebuild anything. */
367f9ed6debSDarrick J. Wong 	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
368f9ed6debSDarrick J. Wong 		return -EOPNOTSUPP;
369f9ed6debSDarrick J. Wong 
370f9ed6debSDarrick J. Wong 	xchk_perag_get(sc->mp, &sc->sa);
371f9ed6debSDarrick J. Wong 	/*
372f9ed6debSDarrick J. Wong 	 * Make sure we have the AGF buffer, as scrub might have decided it
373f9ed6debSDarrick J. Wong 	 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
374f9ed6debSDarrick J. Wong 	 */
375f9ed6debSDarrick J. Wong 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
376f9ed6debSDarrick J. Wong 			XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGF_DADDR(mp)),
377f9ed6debSDarrick J. Wong 			XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
378f9ed6debSDarrick J. Wong 	if (error)
379f9ed6debSDarrick J. Wong 		return error;
380f9ed6debSDarrick J. Wong 	agf_bp->b_ops = &xfs_agf_buf_ops;
3819798f615SChristoph Hellwig 	agf = agf_bp->b_addr;
382f9ed6debSDarrick J. Wong 
383f9ed6debSDarrick J. Wong 	/*
384f9ed6debSDarrick J. Wong 	 * Load the AGFL so that we can screen out OWN_AG blocks that are on
385f9ed6debSDarrick J. Wong 	 * the AGFL now; these blocks might have once been part of the
386f9ed6debSDarrick J. Wong 	 * bno/cnt/rmap btrees but are not now.  This is a chicken and egg
387f9ed6debSDarrick J. Wong 	 * problem: the AGF is corrupt, so we have to trust the AGFL contents
388f9ed6debSDarrick J. Wong 	 * because we can't do any serious cross-referencing with any of the
389f9ed6debSDarrick J. Wong 	 * btrees rooted in the AGF.  If the AGFL contents are obviously bad
390f9ed6debSDarrick J. Wong 	 * then we'll bail out.
391f9ed6debSDarrick J. Wong 	 */
392f9ed6debSDarrick J. Wong 	error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.agno, &agfl_bp);
393f9ed6debSDarrick J. Wong 	if (error)
394f9ed6debSDarrick J. Wong 		return error;
395f9ed6debSDarrick J. Wong 
396f9ed6debSDarrick J. Wong 	/*
397f9ed6debSDarrick J. Wong 	 * Spot-check the AGFL blocks; if they're obviously corrupt then
398f9ed6debSDarrick J. Wong 	 * there's nothing we can do but bail out.
399f9ed6debSDarrick J. Wong 	 */
4009798f615SChristoph Hellwig 	error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
401f9ed6debSDarrick J. Wong 			xrep_agf_check_agfl_block, sc);
402f9ed6debSDarrick J. Wong 	if (error)
403f9ed6debSDarrick J. Wong 		return error;
404f9ed6debSDarrick J. Wong 
405f9ed6debSDarrick J. Wong 	/*
406f9ed6debSDarrick J. Wong 	 * Find the AGF btree roots.  This is also a chicken-and-egg situation;
407f9ed6debSDarrick J. Wong 	 * see the function for more details.
408f9ed6debSDarrick J. Wong 	 */
409f9ed6debSDarrick J. Wong 	error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
410f9ed6debSDarrick J. Wong 	if (error)
411f9ed6debSDarrick J. Wong 		return error;
412f9ed6debSDarrick J. Wong 
413f9ed6debSDarrick J. Wong 	/* Start rewriting the header and implant the btrees we found. */
414f9ed6debSDarrick J. Wong 	xrep_agf_init_header(sc, agf_bp, &old_agf);
415f9ed6debSDarrick J. Wong 	xrep_agf_set_roots(sc, agf, fab);
416f9ed6debSDarrick J. Wong 	error = xrep_agf_calc_from_btrees(sc, agf_bp);
417f9ed6debSDarrick J. Wong 	if (error)
418f9ed6debSDarrick J. Wong 		goto out_revert;
419f9ed6debSDarrick J. Wong 
420f9ed6debSDarrick J. Wong 	/* Commit the changes and reinitialize incore state. */
421f9ed6debSDarrick J. Wong 	return xrep_agf_commit_new(sc, agf_bp);
422f9ed6debSDarrick J. Wong 
423f9ed6debSDarrick J. Wong out_revert:
424f9ed6debSDarrick J. Wong 	/* Mark the incore AGF state stale and revert the AGF. */
425f9ed6debSDarrick J. Wong 	sc->sa.pag->pagf_init = 0;
426f9ed6debSDarrick J. Wong 	memcpy(agf, &old_agf, sizeof(old_agf));
427f9ed6debSDarrick J. Wong 	return error;
428f9ed6debSDarrick J. Wong }
4290e93d3f4SDarrick J. Wong 
4300e93d3f4SDarrick J. Wong /* AGFL */
4310e93d3f4SDarrick J. Wong 
4320e93d3f4SDarrick J. Wong struct xrep_agfl {
4330e93d3f4SDarrick J. Wong 	/* Bitmap of other OWN_AG metadata blocks. */
43400b10d48SDarrick J. Wong 	struct xbitmap		agmetablocks;
4350e93d3f4SDarrick J. Wong 
4360e93d3f4SDarrick J. Wong 	/* Bitmap of free space. */
43700b10d48SDarrick J. Wong 	struct xbitmap		*freesp;
4380e93d3f4SDarrick J. Wong 
4390e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc;
4400e93d3f4SDarrick J. Wong };
4410e93d3f4SDarrick J. Wong 
4420e93d3f4SDarrick J. Wong /* Record all OWN_AG (free space btree) information from the rmap data. */
4430e93d3f4SDarrick J. Wong STATIC int
4440e93d3f4SDarrick J. Wong xrep_agfl_walk_rmap(
4450e93d3f4SDarrick J. Wong 	struct xfs_btree_cur	*cur,
4460e93d3f4SDarrick J. Wong 	struct xfs_rmap_irec	*rec,
4470e93d3f4SDarrick J. Wong 	void			*priv)
4480e93d3f4SDarrick J. Wong {
4490e93d3f4SDarrick J. Wong 	struct xrep_agfl	*ra = priv;
4500e93d3f4SDarrick J. Wong 	xfs_fsblock_t		fsb;
4510e93d3f4SDarrick J. Wong 	int			error = 0;
4520e93d3f4SDarrick J. Wong 
4530e93d3f4SDarrick J. Wong 	if (xchk_should_terminate(ra->sc, &error))
4540e93d3f4SDarrick J. Wong 		return error;
4550e93d3f4SDarrick J. Wong 
4560e93d3f4SDarrick J. Wong 	/* Record all the OWN_AG blocks. */
4570e93d3f4SDarrick J. Wong 	if (rec->rm_owner == XFS_RMAP_OWN_AG) {
458576af732SDave Chinner 		fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.agno,
4590e93d3f4SDarrick J. Wong 				rec->rm_startblock);
46000b10d48SDarrick J. Wong 		error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
4610e93d3f4SDarrick J. Wong 		if (error)
4620e93d3f4SDarrick J. Wong 			return error;
4630e93d3f4SDarrick J. Wong 	}
4640e93d3f4SDarrick J. Wong 
46500b10d48SDarrick J. Wong 	return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
4660e93d3f4SDarrick J. Wong }
4670e93d3f4SDarrick J. Wong 
4680e93d3f4SDarrick J. Wong /*
4690e93d3f4SDarrick J. Wong  * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
4700e93d3f4SDarrick J. Wong  * which blocks belong to the AGFL.
4710e93d3f4SDarrick J. Wong  *
4720e93d3f4SDarrick J. Wong  * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
4730e93d3f4SDarrick J. Wong  * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
4740e93d3f4SDarrick J. Wong  * rmapbt).  These are the old AGFL blocks, so return that list and the number
4750e93d3f4SDarrick J. Wong  * of blocks we're actually going to put back on the AGFL.
4760e93d3f4SDarrick J. Wong  */
4770e93d3f4SDarrick J. Wong STATIC int
4780e93d3f4SDarrick J. Wong xrep_agfl_collect_blocks(
4790e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc,
4800e93d3f4SDarrick J. Wong 	struct xfs_buf		*agf_bp,
48100b10d48SDarrick J. Wong 	struct xbitmap		*agfl_extents,
4820e93d3f4SDarrick J. Wong 	xfs_agblock_t		*flcount)
4830e93d3f4SDarrick J. Wong {
4840e93d3f4SDarrick J. Wong 	struct xrep_agfl	ra;
4850e93d3f4SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
4860e93d3f4SDarrick J. Wong 	struct xfs_btree_cur	*cur;
4870e93d3f4SDarrick J. Wong 	int			error;
4880e93d3f4SDarrick J. Wong 
4890e93d3f4SDarrick J. Wong 	ra.sc = sc;
4900e93d3f4SDarrick J. Wong 	ra.freesp = agfl_extents;
49100b10d48SDarrick J. Wong 	xbitmap_init(&ra.agmetablocks);
4920e93d3f4SDarrick J. Wong 
4930e93d3f4SDarrick J. Wong 	/* Find all space used by the free space btrees & rmapbt. */
494*be9fb17dSDave Chinner 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
495*be9fb17dSDave Chinner 			sc->sa.pag);
4960e93d3f4SDarrick J. Wong 	error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
4970e93d3f4SDarrick J. Wong 	if (error)
4980e93d3f4SDarrick J. Wong 		goto err;
4990e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
5000e93d3f4SDarrick J. Wong 
5010e93d3f4SDarrick J. Wong 	/* Find all blocks currently being used by the bnobt. */
5020e93d3f4SDarrick J. Wong 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
503*be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_BNO);
50400b10d48SDarrick J. Wong 	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
5050e93d3f4SDarrick J. Wong 	if (error)
5060e93d3f4SDarrick J. Wong 		goto err;
5070e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
5080e93d3f4SDarrick J. Wong 
5090e93d3f4SDarrick J. Wong 	/* Find all blocks currently being used by the cntbt. */
5100e93d3f4SDarrick J. Wong 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
511*be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_CNT);
51200b10d48SDarrick J. Wong 	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
5130e93d3f4SDarrick J. Wong 	if (error)
5140e93d3f4SDarrick J. Wong 		goto err;
5150e93d3f4SDarrick J. Wong 
5160e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
5170e93d3f4SDarrick J. Wong 
5180e93d3f4SDarrick J. Wong 	/*
5190e93d3f4SDarrick J. Wong 	 * Drop the freesp meta blocks that are in use by btrees.
5200e93d3f4SDarrick J. Wong 	 * The remaining blocks /should/ be AGFL blocks.
5210e93d3f4SDarrick J. Wong 	 */
52200b10d48SDarrick J. Wong 	error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
52300b10d48SDarrick J. Wong 	xbitmap_destroy(&ra.agmetablocks);
5240e93d3f4SDarrick J. Wong 	if (error)
5250e93d3f4SDarrick J. Wong 		return error;
5260e93d3f4SDarrick J. Wong 
5270e93d3f4SDarrick J. Wong 	/*
5280e93d3f4SDarrick J. Wong 	 * Calculate the new AGFL size.  If we found more blocks than fit in
5290e93d3f4SDarrick J. Wong 	 * the AGFL we'll free them later.
5300e93d3f4SDarrick J. Wong 	 */
531608eb3ceSDarrick J. Wong 	*flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
532608eb3ceSDarrick J. Wong 			 xfs_agfl_size(mp));
5330e93d3f4SDarrick J. Wong 	return 0;
5340e93d3f4SDarrick J. Wong 
5350e93d3f4SDarrick J. Wong err:
53600b10d48SDarrick J. Wong 	xbitmap_destroy(&ra.agmetablocks);
5370e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
5380e93d3f4SDarrick J. Wong 	return error;
5390e93d3f4SDarrick J. Wong }
5400e93d3f4SDarrick J. Wong 
5410e93d3f4SDarrick J. Wong /* Update the AGF and reset the in-core state. */
5420e93d3f4SDarrick J. Wong STATIC void
5430e93d3f4SDarrick J. Wong xrep_agfl_update_agf(
5440e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc,
5450e93d3f4SDarrick J. Wong 	struct xfs_buf		*agf_bp,
5460e93d3f4SDarrick J. Wong 	xfs_agblock_t		flcount)
5470e93d3f4SDarrick J. Wong {
5489798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
5490e93d3f4SDarrick J. Wong 
5500e93d3f4SDarrick J. Wong 	ASSERT(flcount <= xfs_agfl_size(sc->mp));
5510e93d3f4SDarrick J. Wong 
5520e93d3f4SDarrick J. Wong 	/* Trigger fdblocks recalculation */
5530e93d3f4SDarrick J. Wong 	xfs_force_summary_recalc(sc->mp);
5540e93d3f4SDarrick J. Wong 
5550e93d3f4SDarrick J. Wong 	/* Update the AGF counters. */
5560e93d3f4SDarrick J. Wong 	if (sc->sa.pag->pagf_init)
5570e93d3f4SDarrick J. Wong 		sc->sa.pag->pagf_flcount = flcount;
5580e93d3f4SDarrick J. Wong 	agf->agf_flfirst = cpu_to_be32(0);
5590e93d3f4SDarrick J. Wong 	agf->agf_flcount = cpu_to_be32(flcount);
5600e93d3f4SDarrick J. Wong 	agf->agf_fllast = cpu_to_be32(flcount - 1);
5610e93d3f4SDarrick J. Wong 
5620e93d3f4SDarrick J. Wong 	xfs_alloc_log_agf(sc->tp, agf_bp,
5630e93d3f4SDarrick J. Wong 			XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
5640e93d3f4SDarrick J. Wong }
5650e93d3f4SDarrick J. Wong 
5660e93d3f4SDarrick J. Wong /* Write out a totally new AGFL. */
5670e93d3f4SDarrick J. Wong STATIC void
5680e93d3f4SDarrick J. Wong xrep_agfl_init_header(
5690e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc,
5700e93d3f4SDarrick J. Wong 	struct xfs_buf		*agfl_bp,
57100b10d48SDarrick J. Wong 	struct xbitmap		*agfl_extents,
5720e93d3f4SDarrick J. Wong 	xfs_agblock_t		flcount)
5730e93d3f4SDarrick J. Wong {
5740e93d3f4SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
5750e93d3f4SDarrick J. Wong 	__be32			*agfl_bno;
57600b10d48SDarrick J. Wong 	struct xbitmap_range	*br;
57700b10d48SDarrick J. Wong 	struct xbitmap_range	*n;
5780e93d3f4SDarrick J. Wong 	struct xfs_agfl		*agfl;
5790e93d3f4SDarrick J. Wong 	xfs_agblock_t		agbno;
5800e93d3f4SDarrick J. Wong 	unsigned int		fl_off;
5810e93d3f4SDarrick J. Wong 
5820e93d3f4SDarrick J. Wong 	ASSERT(flcount <= xfs_agfl_size(mp));
5830e93d3f4SDarrick J. Wong 
5840e93d3f4SDarrick J. Wong 	/*
5850e93d3f4SDarrick J. Wong 	 * Start rewriting the header by setting the bno[] array to
5860e93d3f4SDarrick J. Wong 	 * NULLAGBLOCK, then setting AGFL header fields.
5870e93d3f4SDarrick J. Wong 	 */
5880e93d3f4SDarrick J. Wong 	agfl = XFS_BUF_TO_AGFL(agfl_bp);
5890e93d3f4SDarrick J. Wong 	memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
5900e93d3f4SDarrick J. Wong 	agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
5910e93d3f4SDarrick J. Wong 	agfl->agfl_seqno = cpu_to_be32(sc->sa.agno);
5920e93d3f4SDarrick J. Wong 	uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
5930e93d3f4SDarrick J. Wong 
5940e93d3f4SDarrick J. Wong 	/*
5950e93d3f4SDarrick J. Wong 	 * Fill the AGFL with the remaining blocks.  If agfl_extents has more
5960e93d3f4SDarrick J. Wong 	 * blocks than fit in the AGFL, they will be freed in a subsequent
5970e93d3f4SDarrick J. Wong 	 * step.
5980e93d3f4SDarrick J. Wong 	 */
5990e93d3f4SDarrick J. Wong 	fl_off = 0;
600183606d8SChristoph Hellwig 	agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
60100b10d48SDarrick J. Wong 	for_each_xbitmap_extent(br, n, agfl_extents) {
6020e93d3f4SDarrick J. Wong 		agbno = XFS_FSB_TO_AGBNO(mp, br->start);
6030e93d3f4SDarrick J. Wong 
6040e93d3f4SDarrick J. Wong 		trace_xrep_agfl_insert(mp, sc->sa.agno, agbno, br->len);
6050e93d3f4SDarrick J. Wong 
6060e93d3f4SDarrick J. Wong 		while (br->len > 0 && fl_off < flcount) {
6070e93d3f4SDarrick J. Wong 			agfl_bno[fl_off] = cpu_to_be32(agbno);
6080e93d3f4SDarrick J. Wong 			fl_off++;
6090e93d3f4SDarrick J. Wong 			agbno++;
6100e93d3f4SDarrick J. Wong 
6110e93d3f4SDarrick J. Wong 			/*
6120e93d3f4SDarrick J. Wong 			 * We've now used br->start by putting it in the AGFL,
6130e93d3f4SDarrick J. Wong 			 * so bump br so that we don't reap the block later.
6140e93d3f4SDarrick J. Wong 			 */
6150e93d3f4SDarrick J. Wong 			br->start++;
6160e93d3f4SDarrick J. Wong 			br->len--;
6170e93d3f4SDarrick J. Wong 		}
6180e93d3f4SDarrick J. Wong 
6190e93d3f4SDarrick J. Wong 		if (br->len)
6200e93d3f4SDarrick J. Wong 			break;
6210e93d3f4SDarrick J. Wong 		list_del(&br->list);
6220e93d3f4SDarrick J. Wong 		kmem_free(br);
6230e93d3f4SDarrick J. Wong 	}
6240e93d3f4SDarrick J. Wong 
6250e93d3f4SDarrick J. Wong 	/* Write new AGFL to disk. */
6260e93d3f4SDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
6270e93d3f4SDarrick J. Wong 	xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
6280e93d3f4SDarrick J. Wong }
6290e93d3f4SDarrick J. Wong 
6300e93d3f4SDarrick J. Wong /* Repair the AGFL. */
6310e93d3f4SDarrick J. Wong int
6320e93d3f4SDarrick J. Wong xrep_agfl(
6330e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc)
6340e93d3f4SDarrick J. Wong {
63500b10d48SDarrick J. Wong 	struct xbitmap		agfl_extents;
6360e93d3f4SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
6370e93d3f4SDarrick J. Wong 	struct xfs_buf		*agf_bp;
6380e93d3f4SDarrick J. Wong 	struct xfs_buf		*agfl_bp;
6390e93d3f4SDarrick J. Wong 	xfs_agblock_t		flcount;
6400e93d3f4SDarrick J. Wong 	int			error;
6410e93d3f4SDarrick J. Wong 
6420e93d3f4SDarrick J. Wong 	/* We require the rmapbt to rebuild anything. */
6430e93d3f4SDarrick J. Wong 	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
6440e93d3f4SDarrick J. Wong 		return -EOPNOTSUPP;
6450e93d3f4SDarrick J. Wong 
6460e93d3f4SDarrick J. Wong 	xchk_perag_get(sc->mp, &sc->sa);
64700b10d48SDarrick J. Wong 	xbitmap_init(&agfl_extents);
6480e93d3f4SDarrick J. Wong 
6490e93d3f4SDarrick J. Wong 	/*
6500e93d3f4SDarrick J. Wong 	 * Read the AGF so that we can query the rmapbt.  We hope that there's
6510e93d3f4SDarrick J. Wong 	 * nothing wrong with the AGF, but all the AG header repair functions
6520e93d3f4SDarrick J. Wong 	 * have this chicken-and-egg problem.
6530e93d3f4SDarrick J. Wong 	 */
6540e93d3f4SDarrick J. Wong 	error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
6550e93d3f4SDarrick J. Wong 	if (error)
6560e93d3f4SDarrick J. Wong 		return error;
6570e93d3f4SDarrick J. Wong 
6580e93d3f4SDarrick J. Wong 	/*
6590e93d3f4SDarrick J. Wong 	 * Make sure we have the AGFL buffer, as scrub might have decided it
6600e93d3f4SDarrick J. Wong 	 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
6610e93d3f4SDarrick J. Wong 	 */
6620e93d3f4SDarrick J. Wong 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
6630e93d3f4SDarrick J. Wong 			XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGFL_DADDR(mp)),
6640e93d3f4SDarrick J. Wong 			XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
6650e93d3f4SDarrick J. Wong 	if (error)
6660e93d3f4SDarrick J. Wong 		return error;
6670e93d3f4SDarrick J. Wong 	agfl_bp->b_ops = &xfs_agfl_buf_ops;
6680e93d3f4SDarrick J. Wong 
6690e93d3f4SDarrick J. Wong 	/* Gather all the extents we're going to put on the new AGFL. */
6700e93d3f4SDarrick J. Wong 	error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
6710e93d3f4SDarrick J. Wong 	if (error)
6720e93d3f4SDarrick J. Wong 		goto err;
6730e93d3f4SDarrick J. Wong 
6740e93d3f4SDarrick J. Wong 	/*
6750e93d3f4SDarrick J. Wong 	 * Update AGF and AGFL.  We reset the global free block counter when
6760e93d3f4SDarrick J. Wong 	 * we adjust the AGF flcount (which can fail) so avoid updating any
6770e93d3f4SDarrick J. Wong 	 * buffers until we know that part works.
6780e93d3f4SDarrick J. Wong 	 */
6790e93d3f4SDarrick J. Wong 	xrep_agfl_update_agf(sc, agf_bp, flcount);
6800e93d3f4SDarrick J. Wong 	xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
6810e93d3f4SDarrick J. Wong 
6820e93d3f4SDarrick J. Wong 	/*
6830e93d3f4SDarrick J. Wong 	 * Ok, the AGFL should be ready to go now.  Roll the transaction to
6840e93d3f4SDarrick J. Wong 	 * make the new AGFL permanent before we start using it to return
6850e93d3f4SDarrick J. Wong 	 * freespace overflow to the freespace btrees.
6860e93d3f4SDarrick J. Wong 	 */
6870e93d3f4SDarrick J. Wong 	sc->sa.agf_bp = agf_bp;
6880e93d3f4SDarrick J. Wong 	sc->sa.agfl_bp = agfl_bp;
6890e93d3f4SDarrick J. Wong 	error = xrep_roll_ag_trans(sc);
6900e93d3f4SDarrick J. Wong 	if (error)
6910e93d3f4SDarrick J. Wong 		goto err;
6920e93d3f4SDarrick J. Wong 
6930e93d3f4SDarrick J. Wong 	/* Dump any AGFL overflow. */
69437a6547dSDarrick J. Wong 	error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
6957280fedaSDarrick J. Wong 			XFS_AG_RESV_AGFL);
6960e93d3f4SDarrick J. Wong err:
69700b10d48SDarrick J. Wong 	xbitmap_destroy(&agfl_extents);
6980e93d3f4SDarrick J. Wong 	return error;
6990e93d3f4SDarrick J. Wong }
70013942aa9SDarrick J. Wong 
70113942aa9SDarrick J. Wong /* AGI */
70213942aa9SDarrick J. Wong 
70313942aa9SDarrick J. Wong /*
70413942aa9SDarrick J. Wong  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
70513942aa9SDarrick J. Wong  * XFS_BTNUM_ names here to avoid creating a sparse array.
70613942aa9SDarrick J. Wong  */
70713942aa9SDarrick J. Wong enum {
70813942aa9SDarrick J. Wong 	XREP_AGI_INOBT = 0,
70913942aa9SDarrick J. Wong 	XREP_AGI_FINOBT,
71013942aa9SDarrick J. Wong 	XREP_AGI_END,
71113942aa9SDarrick J. Wong 	XREP_AGI_MAX
71213942aa9SDarrick J. Wong };
71313942aa9SDarrick J. Wong 
71413942aa9SDarrick J. Wong /*
71513942aa9SDarrick J. Wong  * Given the inode btree roots described by *fab, find the roots, check them
71613942aa9SDarrick J. Wong  * for sanity, and pass the root data back out via *fab.
71713942aa9SDarrick J. Wong  */
71813942aa9SDarrick J. Wong STATIC int
71913942aa9SDarrick J. Wong xrep_agi_find_btrees(
72013942aa9SDarrick J. Wong 	struct xfs_scrub		*sc,
72113942aa9SDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
72213942aa9SDarrick J. Wong {
72313942aa9SDarrick J. Wong 	struct xfs_buf			*agf_bp;
72413942aa9SDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
72513942aa9SDarrick J. Wong 	int				error;
72613942aa9SDarrick J. Wong 
72713942aa9SDarrick J. Wong 	/* Read the AGF. */
72813942aa9SDarrick J. Wong 	error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
72913942aa9SDarrick J. Wong 	if (error)
73013942aa9SDarrick J. Wong 		return error;
73113942aa9SDarrick J. Wong 
73213942aa9SDarrick J. Wong 	/* Find the btree roots. */
73313942aa9SDarrick J. Wong 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
73413942aa9SDarrick J. Wong 	if (error)
73513942aa9SDarrick J. Wong 		return error;
73613942aa9SDarrick J. Wong 
73713942aa9SDarrick J. Wong 	/* We must find the inobt root. */
73813942aa9SDarrick J. Wong 	if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
73913942aa9SDarrick J. Wong 		return -EFSCORRUPTED;
74013942aa9SDarrick J. Wong 
74113942aa9SDarrick J. Wong 	/* We must find the finobt root if that feature is enabled. */
74213942aa9SDarrick J. Wong 	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
74313942aa9SDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
74413942aa9SDarrick J. Wong 		return -EFSCORRUPTED;
74513942aa9SDarrick J. Wong 
74613942aa9SDarrick J. Wong 	return 0;
74713942aa9SDarrick J. Wong }
74813942aa9SDarrick J. Wong 
74913942aa9SDarrick J. Wong /*
75013942aa9SDarrick J. Wong  * Reinitialize the AGI header, making an in-core copy of the old contents so
75113942aa9SDarrick J. Wong  * that we know which in-core state needs to be reinitialized.
75213942aa9SDarrick J. Wong  */
75313942aa9SDarrick J. Wong STATIC void
75413942aa9SDarrick J. Wong xrep_agi_init_header(
75513942aa9SDarrick J. Wong 	struct xfs_scrub	*sc,
75613942aa9SDarrick J. Wong 	struct xfs_buf		*agi_bp,
75713942aa9SDarrick J. Wong 	struct xfs_agi		*old_agi)
75813942aa9SDarrick J. Wong {
759370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agi_bp->b_addr;
76013942aa9SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
76113942aa9SDarrick J. Wong 
76213942aa9SDarrick J. Wong 	memcpy(old_agi, agi, sizeof(*old_agi));
76313942aa9SDarrick J. Wong 	memset(agi, 0, BBTOB(agi_bp->b_length));
76413942aa9SDarrick J. Wong 	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
76513942aa9SDarrick J. Wong 	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
76613942aa9SDarrick J. Wong 	agi->agi_seqno = cpu_to_be32(sc->sa.agno);
76713942aa9SDarrick J. Wong 	agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
76813942aa9SDarrick J. Wong 	agi->agi_newino = cpu_to_be32(NULLAGINO);
76913942aa9SDarrick J. Wong 	agi->agi_dirino = cpu_to_be32(NULLAGINO);
77013942aa9SDarrick J. Wong 	if (xfs_sb_version_hascrc(&mp->m_sb))
77113942aa9SDarrick J. Wong 		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
77213942aa9SDarrick J. Wong 
77313942aa9SDarrick J. Wong 	/* We don't know how to fix the unlinked list yet. */
77413942aa9SDarrick J. Wong 	memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
77513942aa9SDarrick J. Wong 			sizeof(agi->agi_unlinked));
77613942aa9SDarrick J. Wong 
77713942aa9SDarrick J. Wong 	/* Mark the incore AGF data stale until we're done fixing things. */
77813942aa9SDarrick J. Wong 	ASSERT(sc->sa.pag->pagi_init);
77913942aa9SDarrick J. Wong 	sc->sa.pag->pagi_init = 0;
78013942aa9SDarrick J. Wong }
78113942aa9SDarrick J. Wong 
78213942aa9SDarrick J. Wong /* Set btree root information in an AGI. */
78313942aa9SDarrick J. Wong STATIC void
78413942aa9SDarrick J. Wong xrep_agi_set_roots(
78513942aa9SDarrick J. Wong 	struct xfs_scrub		*sc,
78613942aa9SDarrick J. Wong 	struct xfs_agi			*agi,
78713942aa9SDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
78813942aa9SDarrick J. Wong {
78913942aa9SDarrick J. Wong 	agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
79013942aa9SDarrick J. Wong 	agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
79113942aa9SDarrick J. Wong 
79213942aa9SDarrick J. Wong 	if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) {
79313942aa9SDarrick J. Wong 		agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
79413942aa9SDarrick J. Wong 		agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
79513942aa9SDarrick J. Wong 	}
79613942aa9SDarrick J. Wong }
79713942aa9SDarrick J. Wong 
79813942aa9SDarrick J. Wong /* Update the AGI counters. */
79913942aa9SDarrick J. Wong STATIC int
80013942aa9SDarrick J. Wong xrep_agi_calc_from_btrees(
80113942aa9SDarrick J. Wong 	struct xfs_scrub	*sc,
80213942aa9SDarrick J. Wong 	struct xfs_buf		*agi_bp)
80313942aa9SDarrick J. Wong {
80413942aa9SDarrick J. Wong 	struct xfs_btree_cur	*cur;
805370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agi_bp->b_addr;
80613942aa9SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
80713942aa9SDarrick J. Wong 	xfs_agino_t		count;
80813942aa9SDarrick J. Wong 	xfs_agino_t		freecount;
80913942aa9SDarrick J. Wong 	int			error;
81013942aa9SDarrick J. Wong 
81113942aa9SDarrick J. Wong 	cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno,
812*be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_INO);
81313942aa9SDarrick J. Wong 	error = xfs_ialloc_count_inodes(cur, &count, &freecount);
81413942aa9SDarrick J. Wong 	if (error)
81513942aa9SDarrick J. Wong 		goto err;
81611f74423SDarrick J. Wong 	if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
81711f74423SDarrick J. Wong 		xfs_agblock_t	blocks;
81811f74423SDarrick J. Wong 
81911f74423SDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &blocks);
82011f74423SDarrick J. Wong 		if (error)
82111f74423SDarrick J. Wong 			goto err;
82211f74423SDarrick J. Wong 		agi->agi_iblocks = cpu_to_be32(blocks);
82311f74423SDarrick J. Wong 	}
82413942aa9SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
82513942aa9SDarrick J. Wong 
82613942aa9SDarrick J. Wong 	agi->agi_count = cpu_to_be32(count);
82713942aa9SDarrick J. Wong 	agi->agi_freecount = cpu_to_be32(freecount);
82811f74423SDarrick J. Wong 
82911f74423SDarrick J. Wong 	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
83011f74423SDarrick J. Wong 	    xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
83111f74423SDarrick J. Wong 		xfs_agblock_t	blocks;
83211f74423SDarrick J. Wong 
83311f74423SDarrick J. Wong 		cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno,
834*be9fb17dSDave Chinner 				sc->sa.pag, XFS_BTNUM_FINO);
83511f74423SDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &blocks);
83611f74423SDarrick J. Wong 		if (error)
83711f74423SDarrick J. Wong 			goto err;
83811f74423SDarrick J. Wong 		xfs_btree_del_cursor(cur, error);
83911f74423SDarrick J. Wong 		agi->agi_fblocks = cpu_to_be32(blocks);
84011f74423SDarrick J. Wong 	}
84111f74423SDarrick J. Wong 
84213942aa9SDarrick J. Wong 	return 0;
84313942aa9SDarrick J. Wong err:
84413942aa9SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
84513942aa9SDarrick J. Wong 	return error;
84613942aa9SDarrick J. Wong }
84713942aa9SDarrick J. Wong 
84813942aa9SDarrick J. Wong /* Trigger reinitialization of the in-core data. */
84913942aa9SDarrick J. Wong STATIC int
85013942aa9SDarrick J. Wong xrep_agi_commit_new(
85113942aa9SDarrick J. Wong 	struct xfs_scrub	*sc,
85213942aa9SDarrick J. Wong 	struct xfs_buf		*agi_bp)
85313942aa9SDarrick J. Wong {
85413942aa9SDarrick J. Wong 	struct xfs_perag	*pag;
855370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agi_bp->b_addr;
85613942aa9SDarrick J. Wong 
85713942aa9SDarrick J. Wong 	/* Trigger inode count recalculation */
85813942aa9SDarrick J. Wong 	xfs_force_summary_recalc(sc->mp);
85913942aa9SDarrick J. Wong 
86013942aa9SDarrick J. Wong 	/* Write this to disk. */
86113942aa9SDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
86213942aa9SDarrick J. Wong 	xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
86313942aa9SDarrick J. Wong 
86413942aa9SDarrick J. Wong 	/* Now reinitialize the in-core counters if necessary. */
86513942aa9SDarrick J. Wong 	pag = sc->sa.pag;
86613942aa9SDarrick J. Wong 	pag->pagi_count = be32_to_cpu(agi->agi_count);
86713942aa9SDarrick J. Wong 	pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
86813942aa9SDarrick J. Wong 	pag->pagi_init = 1;
86913942aa9SDarrick J. Wong 
87013942aa9SDarrick J. Wong 	return 0;
87113942aa9SDarrick J. Wong }
87213942aa9SDarrick J. Wong 
87313942aa9SDarrick J. Wong /* Repair the AGI. */
87413942aa9SDarrick J. Wong int
87513942aa9SDarrick J. Wong xrep_agi(
87613942aa9SDarrick J. Wong 	struct xfs_scrub		*sc)
87713942aa9SDarrick J. Wong {
87813942aa9SDarrick J. Wong 	struct xrep_find_ag_btree	fab[XREP_AGI_MAX] = {
87913942aa9SDarrick J. Wong 		[XREP_AGI_INOBT] = {
88013942aa9SDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_INOBT,
88113942aa9SDarrick J. Wong 			.buf_ops = &xfs_inobt_buf_ops,
88213942aa9SDarrick J. Wong 		},
88313942aa9SDarrick J. Wong 		[XREP_AGI_FINOBT] = {
88413942aa9SDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_INOBT,
88501e68f40SBrian Foster 			.buf_ops = &xfs_finobt_buf_ops,
88613942aa9SDarrick J. Wong 		},
88713942aa9SDarrick J. Wong 		[XREP_AGI_END] = {
88813942aa9SDarrick J. Wong 			.buf_ops = NULL
88913942aa9SDarrick J. Wong 		},
89013942aa9SDarrick J. Wong 	};
89113942aa9SDarrick J. Wong 	struct xfs_agi			old_agi;
89213942aa9SDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
89313942aa9SDarrick J. Wong 	struct xfs_buf			*agi_bp;
89413942aa9SDarrick J. Wong 	struct xfs_agi			*agi;
89513942aa9SDarrick J. Wong 	int				error;
89613942aa9SDarrick J. Wong 
89713942aa9SDarrick J. Wong 	/* We require the rmapbt to rebuild anything. */
89813942aa9SDarrick J. Wong 	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
89913942aa9SDarrick J. Wong 		return -EOPNOTSUPP;
90013942aa9SDarrick J. Wong 
90113942aa9SDarrick J. Wong 	xchk_perag_get(sc->mp, &sc->sa);
90213942aa9SDarrick J. Wong 	/*
90313942aa9SDarrick J. Wong 	 * Make sure we have the AGI buffer, as scrub might have decided it
90413942aa9SDarrick J. Wong 	 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
90513942aa9SDarrick J. Wong 	 */
90613942aa9SDarrick J. Wong 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
90713942aa9SDarrick J. Wong 			XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGI_DADDR(mp)),
90813942aa9SDarrick J. Wong 			XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
90913942aa9SDarrick J. Wong 	if (error)
91013942aa9SDarrick J. Wong 		return error;
91113942aa9SDarrick J. Wong 	agi_bp->b_ops = &xfs_agi_buf_ops;
912370c782bSChristoph Hellwig 	agi = agi_bp->b_addr;
91313942aa9SDarrick J. Wong 
91413942aa9SDarrick J. Wong 	/* Find the AGI btree roots. */
91513942aa9SDarrick J. Wong 	error = xrep_agi_find_btrees(sc, fab);
91613942aa9SDarrick J. Wong 	if (error)
91713942aa9SDarrick J. Wong 		return error;
91813942aa9SDarrick J. Wong 
91913942aa9SDarrick J. Wong 	/* Start rewriting the header and implant the btrees we found. */
92013942aa9SDarrick J. Wong 	xrep_agi_init_header(sc, agi_bp, &old_agi);
92113942aa9SDarrick J. Wong 	xrep_agi_set_roots(sc, agi, fab);
92213942aa9SDarrick J. Wong 	error = xrep_agi_calc_from_btrees(sc, agi_bp);
92313942aa9SDarrick J. Wong 	if (error)
92413942aa9SDarrick J. Wong 		goto out_revert;
92513942aa9SDarrick J. Wong 
92613942aa9SDarrick J. Wong 	/* Reinitialize in-core state. */
92713942aa9SDarrick J. Wong 	return xrep_agi_commit_new(sc, agi_bp);
92813942aa9SDarrick J. Wong 
92913942aa9SDarrick J. Wong out_revert:
93013942aa9SDarrick J. Wong 	/* Mark the incore AGI state stale and revert the AGI. */
93113942aa9SDarrick J. Wong 	sc->sa.pag->pagi_init = 0;
93213942aa9SDarrick J. Wong 	memcpy(agi, &old_agi, sizeof(old_agi));
93313942aa9SDarrick J. Wong 	return error;
93413942aa9SDarrick J. Wong }
935