xref: /openbmc/linux/fs/xfs/scrub/agheader_repair.c (revision 178b48d5)
1739a2fe0SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later
2d25522f1SDarrick J. Wong /*
3ecc73f8aSDarrick J. Wong  * Copyright (C) 2018-2023 Oracle.  All Rights Reserved.
4739a2fe0SDarrick J. Wong  * Author: Darrick J. Wong <djwong@kernel.org>
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 
554a9bca86SDarrick J. Wong 	/*
564a9bca86SDarrick J. Wong 	 * Don't write out a secondary super with NEEDSREPAIR or log incompat
574a9bca86SDarrick J. Wong 	 * features set, since both are ignored when set on a secondary.
584a9bca86SDarrick J. Wong 	 */
594a9bca86SDarrick J. Wong 	if (xfs_has_crc(mp)) {
604a9bca86SDarrick J. Wong 		struct xfs_dsb		*sb = bp->b_addr;
614a9bca86SDarrick J. Wong 
624a9bca86SDarrick J. Wong 		sb->sb_features_incompat &=
634a9bca86SDarrick J. Wong 				~cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
644a9bca86SDarrick J. Wong 		sb->sb_features_log_incompat = 0;
654a9bca86SDarrick J. Wong 	}
664a9bca86SDarrick J. Wong 
67d25522f1SDarrick J. Wong 	/* Write this to disk. */
68d25522f1SDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
69d25522f1SDarrick J. Wong 	xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
70d25522f1SDarrick J. Wong 	return error;
71d25522f1SDarrick J. Wong }
72f9ed6debSDarrick J. Wong 
73f9ed6debSDarrick J. Wong /* AGF */
74f9ed6debSDarrick J. Wong 
75f9ed6debSDarrick J. Wong struct xrep_agf_allocbt {
76f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc;
77f9ed6debSDarrick J. Wong 	xfs_agblock_t		freeblks;
78f9ed6debSDarrick J. Wong 	xfs_agblock_t		longest;
79f9ed6debSDarrick J. Wong };
80f9ed6debSDarrick J. Wong 
81f9ed6debSDarrick J. Wong /* Record free space shape information. */
82f9ed6debSDarrick J. Wong STATIC int
83f9ed6debSDarrick J. Wong xrep_agf_walk_allocbt(
84f9ed6debSDarrick J. Wong 	struct xfs_btree_cur		*cur,
85159eb69dSDarrick J. Wong 	const struct xfs_alloc_rec_incore *rec,
86f9ed6debSDarrick J. Wong 	void				*priv)
87f9ed6debSDarrick J. Wong {
88f9ed6debSDarrick J. Wong 	struct xrep_agf_allocbt		*raa = priv;
89f9ed6debSDarrick J. Wong 	int				error = 0;
90f9ed6debSDarrick J. Wong 
91f9ed6debSDarrick J. Wong 	if (xchk_should_terminate(raa->sc, &error))
92f9ed6debSDarrick J. Wong 		return error;
93f9ed6debSDarrick J. Wong 
94f9ed6debSDarrick J. Wong 	raa->freeblks += rec->ar_blockcount;
95f9ed6debSDarrick J. Wong 	if (rec->ar_blockcount > raa->longest)
96f9ed6debSDarrick J. Wong 		raa->longest = rec->ar_blockcount;
97f9ed6debSDarrick J. Wong 	return error;
98f9ed6debSDarrick J. Wong }
99f9ed6debSDarrick J. Wong 
100f9ed6debSDarrick J. Wong /* Does this AGFL block look sane? */
101f9ed6debSDarrick J. Wong STATIC int
102f9ed6debSDarrick J. Wong xrep_agf_check_agfl_block(
103f9ed6debSDarrick J. Wong 	struct xfs_mount	*mp,
104f9ed6debSDarrick J. Wong 	xfs_agblock_t		agbno,
105f9ed6debSDarrick J. Wong 	void			*priv)
106f9ed6debSDarrick J. Wong {
107f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc = priv;
108f9ed6debSDarrick J. Wong 
1090800169eSDave Chinner 	if (!xfs_verify_agbno(sc->sa.pag, agbno))
110f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
111f9ed6debSDarrick J. Wong 	return 0;
112f9ed6debSDarrick J. Wong }
113f9ed6debSDarrick J. Wong 
114f9ed6debSDarrick J. Wong /*
115f9ed6debSDarrick J. Wong  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
116f9ed6debSDarrick J. Wong  * XFS_BTNUM_ names here to avoid creating a sparse array.
117f9ed6debSDarrick J. Wong  */
118f9ed6debSDarrick J. Wong enum {
119f9ed6debSDarrick J. Wong 	XREP_AGF_BNOBT = 0,
120f9ed6debSDarrick J. Wong 	XREP_AGF_CNTBT,
121f9ed6debSDarrick J. Wong 	XREP_AGF_RMAPBT,
122f9ed6debSDarrick J. Wong 	XREP_AGF_REFCOUNTBT,
123f9ed6debSDarrick J. Wong 	XREP_AGF_END,
124f9ed6debSDarrick J. Wong 	XREP_AGF_MAX
125f9ed6debSDarrick J. Wong };
126f9ed6debSDarrick J. Wong 
127f9ed6debSDarrick J. Wong /* Check a btree root candidate. */
128f9ed6debSDarrick J. Wong static inline bool
129f9ed6debSDarrick J. Wong xrep_check_btree_root(
130f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc,
131f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
132f9ed6debSDarrick J. Wong {
1330800169eSDave Chinner 	return xfs_verify_agbno(sc->sa.pag, fab->root) &&
1341ba6fd34SDarrick J. Wong 	       fab->height <= fab->maxlevels;
135f9ed6debSDarrick J. Wong }
136f9ed6debSDarrick J. Wong 
137f9ed6debSDarrick J. Wong /*
138f9ed6debSDarrick J. Wong  * Given the btree roots described by *fab, find the roots, check them for
139f9ed6debSDarrick J. Wong  * sanity, and pass the root data back out via *fab.
140f9ed6debSDarrick J. Wong  *
141f9ed6debSDarrick J. Wong  * This is /also/ a chicken and egg problem because we have to use the rmapbt
142f9ed6debSDarrick J. Wong  * (rooted in the AGF) to find the btrees rooted in the AGF.  We also have no
143f9ed6debSDarrick J. Wong  * idea if the btrees make any sense.  If we hit obvious corruptions in those
144f9ed6debSDarrick J. Wong  * btrees we'll bail out.
145f9ed6debSDarrick J. Wong  */
146f9ed6debSDarrick J. Wong STATIC int
147f9ed6debSDarrick J. Wong xrep_agf_find_btrees(
148f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc,
149f9ed6debSDarrick J. Wong 	struct xfs_buf			*agf_bp,
150f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	*fab,
151f9ed6debSDarrick J. Wong 	struct xfs_buf			*agfl_bp)
152f9ed6debSDarrick J. Wong {
1539798f615SChristoph Hellwig 	struct xfs_agf			*old_agf = agf_bp->b_addr;
154f9ed6debSDarrick J. Wong 	int				error;
155f9ed6debSDarrick J. Wong 
156f9ed6debSDarrick J. Wong 	/* Go find the root data. */
157f9ed6debSDarrick J. Wong 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
158f9ed6debSDarrick J. Wong 	if (error)
159f9ed6debSDarrick J. Wong 		return error;
160f9ed6debSDarrick J. Wong 
161f9ed6debSDarrick J. Wong 	/* We must find the bnobt, cntbt, and rmapbt roots. */
162f9ed6debSDarrick J. Wong 	if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
163f9ed6debSDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
164f9ed6debSDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
165f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
166f9ed6debSDarrick J. Wong 
167f9ed6debSDarrick J. Wong 	/*
168f9ed6debSDarrick J. Wong 	 * We relied on the rmapbt to reconstruct the AGF.  If we get a
169f9ed6debSDarrick J. Wong 	 * different root then something's seriously wrong.
170f9ed6debSDarrick J. Wong 	 */
171f9ed6debSDarrick J. Wong 	if (fab[XREP_AGF_RMAPBT].root !=
172f9ed6debSDarrick J. Wong 	    be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
173f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
174f9ed6debSDarrick J. Wong 
175f9ed6debSDarrick J. Wong 	/* We must find the refcountbt root if that feature is enabled. */
17638c26bfdSDave Chinner 	if (xfs_has_reflink(sc->mp) &&
177f9ed6debSDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
178f9ed6debSDarrick J. Wong 		return -EFSCORRUPTED;
179f9ed6debSDarrick J. Wong 
180f9ed6debSDarrick J. Wong 	return 0;
181f9ed6debSDarrick J. Wong }
182f9ed6debSDarrick J. Wong 
183f9ed6debSDarrick J. Wong /*
184f9ed6debSDarrick J. Wong  * Reinitialize the AGF header, making an in-core copy of the old contents so
185f9ed6debSDarrick J. Wong  * that we know which in-core state needs to be reinitialized.
186f9ed6debSDarrick J. Wong  */
187f9ed6debSDarrick J. Wong STATIC void
188f9ed6debSDarrick J. Wong xrep_agf_init_header(
189f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc,
190f9ed6debSDarrick J. Wong 	struct xfs_buf		*agf_bp,
191f9ed6debSDarrick J. Wong 	struct xfs_agf		*old_agf)
192f9ed6debSDarrick J. Wong {
193f9ed6debSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
1947ac2ff8bSDave Chinner 	struct xfs_perag	*pag = sc->sa.pag;
1959798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
196f9ed6debSDarrick J. Wong 
197f9ed6debSDarrick J. Wong 	memcpy(old_agf, agf, sizeof(*old_agf));
198f9ed6debSDarrick J. Wong 	memset(agf, 0, BBTOB(agf_bp->b_length));
199f9ed6debSDarrick J. Wong 	agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
200f9ed6debSDarrick J. Wong 	agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
2017ac2ff8bSDave Chinner 	agf->agf_seqno = cpu_to_be32(pag->pag_agno);
2027ac2ff8bSDave Chinner 	agf->agf_length = cpu_to_be32(pag->block_count);
203f9ed6debSDarrick J. Wong 	agf->agf_flfirst = old_agf->agf_flfirst;
204f9ed6debSDarrick J. Wong 	agf->agf_fllast = old_agf->agf_fllast;
205f9ed6debSDarrick J. Wong 	agf->agf_flcount = old_agf->agf_flcount;
20638c26bfdSDave Chinner 	if (xfs_has_crc(mp))
207f9ed6debSDarrick J. Wong 		uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
208f9ed6debSDarrick J. Wong 
209f9ed6debSDarrick J. Wong 	/* Mark the incore AGF data stale until we're done fixing things. */
2107ac2ff8bSDave Chinner 	ASSERT(xfs_perag_initialised_agf(pag));
2117ac2ff8bSDave Chinner 	clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
212f9ed6debSDarrick J. Wong }
213f9ed6debSDarrick J. Wong 
214f9ed6debSDarrick J. Wong /* Set btree root information in an AGF. */
215f9ed6debSDarrick J. Wong STATIC void
216f9ed6debSDarrick J. Wong xrep_agf_set_roots(
217f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc,
218f9ed6debSDarrick J. Wong 	struct xfs_agf			*agf,
219f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
220f9ed6debSDarrick J. Wong {
221f9ed6debSDarrick J. Wong 	agf->agf_roots[XFS_BTNUM_BNOi] =
222f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_BNOBT].root);
223f9ed6debSDarrick J. Wong 	agf->agf_levels[XFS_BTNUM_BNOi] =
224f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_BNOBT].height);
225f9ed6debSDarrick J. Wong 
226f9ed6debSDarrick J. Wong 	agf->agf_roots[XFS_BTNUM_CNTi] =
227f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_CNTBT].root);
228f9ed6debSDarrick J. Wong 	agf->agf_levels[XFS_BTNUM_CNTi] =
229f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_CNTBT].height);
230f9ed6debSDarrick J. Wong 
231f9ed6debSDarrick J. Wong 	agf->agf_roots[XFS_BTNUM_RMAPi] =
232f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
233f9ed6debSDarrick J. Wong 	agf->agf_levels[XFS_BTNUM_RMAPi] =
234f9ed6debSDarrick J. Wong 			cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
235f9ed6debSDarrick J. Wong 
23638c26bfdSDave Chinner 	if (xfs_has_reflink(sc->mp)) {
237f9ed6debSDarrick J. Wong 		agf->agf_refcount_root =
238f9ed6debSDarrick J. Wong 				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
239f9ed6debSDarrick J. Wong 		agf->agf_refcount_level =
240f9ed6debSDarrick J. Wong 				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
241f9ed6debSDarrick J. Wong 	}
242f9ed6debSDarrick J. Wong }
243f9ed6debSDarrick J. Wong 
244f9ed6debSDarrick J. Wong /* Update all AGF fields which derive from btree contents. */
245f9ed6debSDarrick J. Wong STATIC int
246f9ed6debSDarrick J. Wong xrep_agf_calc_from_btrees(
247f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc,
248f9ed6debSDarrick J. Wong 	struct xfs_buf		*agf_bp)
249f9ed6debSDarrick J. Wong {
250f9ed6debSDarrick J. Wong 	struct xrep_agf_allocbt	raa = { .sc = sc };
251f9ed6debSDarrick J. Wong 	struct xfs_btree_cur	*cur = NULL;
2529798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
253f9ed6debSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
254f9ed6debSDarrick J. Wong 	xfs_agblock_t		btreeblks;
255f9ed6debSDarrick J. Wong 	xfs_agblock_t		blocks;
256f9ed6debSDarrick J. Wong 	int			error;
257f9ed6debSDarrick J. Wong 
258f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the bnobt. */
259289d38d2SDave Chinner 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
260be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_BNO);
261f9ed6debSDarrick J. Wong 	error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
262f9ed6debSDarrick J. Wong 	if (error)
263f9ed6debSDarrick J. Wong 		goto err;
264f9ed6debSDarrick J. Wong 	error = xfs_btree_count_blocks(cur, &blocks);
265f9ed6debSDarrick J. Wong 	if (error)
266f9ed6debSDarrick J. Wong 		goto err;
267f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
268f9ed6debSDarrick J. Wong 	btreeblks = blocks - 1;
269f9ed6debSDarrick J. Wong 	agf->agf_freeblks = cpu_to_be32(raa.freeblks);
270f9ed6debSDarrick J. Wong 	agf->agf_longest = cpu_to_be32(raa.longest);
271f9ed6debSDarrick J. Wong 
272f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the cntbt. */
273289d38d2SDave Chinner 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
274be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_CNT);
275f9ed6debSDarrick J. Wong 	error = xfs_btree_count_blocks(cur, &blocks);
276f9ed6debSDarrick J. Wong 	if (error)
277f9ed6debSDarrick J. Wong 		goto err;
278f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
279f9ed6debSDarrick J. Wong 	btreeblks += blocks - 1;
280f9ed6debSDarrick J. Wong 
281f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the rmapbt. */
282fa9c3c19SDave Chinner 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
283f9ed6debSDarrick J. Wong 	error = xfs_btree_count_blocks(cur, &blocks);
284f9ed6debSDarrick J. Wong 	if (error)
285f9ed6debSDarrick J. Wong 		goto err;
286f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
287f9ed6debSDarrick J. Wong 	agf->agf_rmap_blocks = cpu_to_be32(blocks);
288f9ed6debSDarrick J. Wong 	btreeblks += blocks - 1;
289f9ed6debSDarrick J. Wong 
290f9ed6debSDarrick J. Wong 	agf->agf_btreeblks = cpu_to_be32(btreeblks);
291f9ed6debSDarrick J. Wong 
292f9ed6debSDarrick J. Wong 	/* Update the AGF counters from the refcountbt. */
29338c26bfdSDave Chinner 	if (xfs_has_reflink(mp)) {
294f9ed6debSDarrick J. Wong 		cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
295a81a0621SDave Chinner 				sc->sa.pag);
296f9ed6debSDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &blocks);
297f9ed6debSDarrick J. Wong 		if (error)
298f9ed6debSDarrick J. Wong 			goto err;
299f9ed6debSDarrick J. Wong 		xfs_btree_del_cursor(cur, error);
300f9ed6debSDarrick J. Wong 		agf->agf_refcount_blocks = cpu_to_be32(blocks);
301f9ed6debSDarrick J. Wong 	}
302f9ed6debSDarrick J. Wong 
303f9ed6debSDarrick J. Wong 	return 0;
304f9ed6debSDarrick J. Wong err:
305f9ed6debSDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
306f9ed6debSDarrick J. Wong 	return error;
307f9ed6debSDarrick J. Wong }
308f9ed6debSDarrick J. Wong 
309f9ed6debSDarrick J. Wong /* Commit the new AGF and reinitialize the incore state. */
310f9ed6debSDarrick J. Wong STATIC int
311f9ed6debSDarrick J. Wong xrep_agf_commit_new(
312f9ed6debSDarrick J. Wong 	struct xfs_scrub	*sc,
313f9ed6debSDarrick J. Wong 	struct xfs_buf		*agf_bp)
314f9ed6debSDarrick J. Wong {
315f9ed6debSDarrick J. Wong 	struct xfs_perag	*pag;
3169798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
317f9ed6debSDarrick J. Wong 
318f9ed6debSDarrick J. Wong 	/* Trigger fdblocks recalculation */
319f9ed6debSDarrick J. Wong 	xfs_force_summary_recalc(sc->mp);
320f9ed6debSDarrick J. Wong 
321f9ed6debSDarrick J. Wong 	/* Write this to disk. */
322f9ed6debSDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
323f9ed6debSDarrick J. Wong 	xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
324f9ed6debSDarrick J. Wong 
325f9ed6debSDarrick J. Wong 	/* Now reinitialize the in-core counters we changed. */
326f9ed6debSDarrick J. Wong 	pag = sc->sa.pag;
327f9ed6debSDarrick J. Wong 	pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
328f9ed6debSDarrick J. Wong 	pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
329f9ed6debSDarrick J. Wong 	pag->pagf_longest = be32_to_cpu(agf->agf_longest);
330f9ed6debSDarrick J. Wong 	pag->pagf_levels[XFS_BTNUM_BNOi] =
331f9ed6debSDarrick J. Wong 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
332f9ed6debSDarrick J. Wong 	pag->pagf_levels[XFS_BTNUM_CNTi] =
333f9ed6debSDarrick J. Wong 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
334f9ed6debSDarrick J. Wong 	pag->pagf_levels[XFS_BTNUM_RMAPi] =
335f9ed6debSDarrick J. Wong 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
336f9ed6debSDarrick J. Wong 	pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
3377ac2ff8bSDave Chinner 	set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
338f9ed6debSDarrick J. Wong 
339f9ed6debSDarrick J. Wong 	return 0;
340f9ed6debSDarrick J. Wong }
341f9ed6debSDarrick J. Wong 
342f9ed6debSDarrick J. Wong /* Repair the AGF. v5 filesystems only. */
343f9ed6debSDarrick J. Wong int
344f9ed6debSDarrick J. Wong xrep_agf(
345f9ed6debSDarrick J. Wong 	struct xfs_scrub		*sc)
346f9ed6debSDarrick J. Wong {
347f9ed6debSDarrick J. Wong 	struct xrep_find_ag_btree	fab[XREP_AGF_MAX] = {
348f9ed6debSDarrick J. Wong 		[XREP_AGF_BNOBT] = {
349f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_AG,
35027df4f50SBrian Foster 			.buf_ops = &xfs_bnobt_buf_ops,
3517cb3efb4SDarrick J. Wong 			.maxlevels = sc->mp->m_alloc_maxlevels,
352f9ed6debSDarrick J. Wong 		},
353f9ed6debSDarrick J. Wong 		[XREP_AGF_CNTBT] = {
354f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_AG,
35527df4f50SBrian Foster 			.buf_ops = &xfs_cntbt_buf_ops,
3567cb3efb4SDarrick J. Wong 			.maxlevels = sc->mp->m_alloc_maxlevels,
357f9ed6debSDarrick J. Wong 		},
358f9ed6debSDarrick J. Wong 		[XREP_AGF_RMAPBT] = {
359f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_AG,
360f9ed6debSDarrick J. Wong 			.buf_ops = &xfs_rmapbt_buf_ops,
3611ba6fd34SDarrick J. Wong 			.maxlevels = sc->mp->m_rmap_maxlevels,
362f9ed6debSDarrick J. Wong 		},
363f9ed6debSDarrick J. Wong 		[XREP_AGF_REFCOUNTBT] = {
364f9ed6debSDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_REFC,
365f9ed6debSDarrick J. Wong 			.buf_ops = &xfs_refcountbt_buf_ops,
3661ba6fd34SDarrick J. Wong 			.maxlevels = sc->mp->m_refc_maxlevels,
367f9ed6debSDarrick J. Wong 		},
368f9ed6debSDarrick J. Wong 		[XREP_AGF_END] = {
369f9ed6debSDarrick J. Wong 			.buf_ops = NULL,
370f9ed6debSDarrick J. Wong 		},
371f9ed6debSDarrick J. Wong 	};
372f9ed6debSDarrick J. Wong 	struct xfs_agf			old_agf;
373f9ed6debSDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
374f9ed6debSDarrick J. Wong 	struct xfs_buf			*agf_bp;
375f9ed6debSDarrick J. Wong 	struct xfs_buf			*agfl_bp;
376f9ed6debSDarrick J. Wong 	struct xfs_agf			*agf;
377f9ed6debSDarrick J. Wong 	int				error;
378f9ed6debSDarrick J. Wong 
379f9ed6debSDarrick J. Wong 	/* We require the rmapbt to rebuild anything. */
38038c26bfdSDave Chinner 	if (!xfs_has_rmapbt(mp))
381f9ed6debSDarrick J. Wong 		return -EOPNOTSUPP;
382f9ed6debSDarrick J. Wong 
383f9ed6debSDarrick J. Wong 	/*
384f9ed6debSDarrick J. Wong 	 * Make sure we have the AGF buffer, as scrub might have decided it
385f9ed6debSDarrick J. Wong 	 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
386f9ed6debSDarrick J. Wong 	 */
387f9ed6debSDarrick J. Wong 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
38854406764SDarrick J. Wong 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
38954406764SDarrick J. Wong 						XFS_AGF_DADDR(mp)),
390f9ed6debSDarrick J. Wong 			XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
391f9ed6debSDarrick J. Wong 	if (error)
392f9ed6debSDarrick J. Wong 		return error;
393f9ed6debSDarrick J. Wong 	agf_bp->b_ops = &xfs_agf_buf_ops;
3949798f615SChristoph Hellwig 	agf = agf_bp->b_addr;
395f9ed6debSDarrick J. Wong 
396f9ed6debSDarrick J. Wong 	/*
397f9ed6debSDarrick J. Wong 	 * Load the AGFL so that we can screen out OWN_AG blocks that are on
398f9ed6debSDarrick J. Wong 	 * the AGFL now; these blocks might have once been part of the
399f9ed6debSDarrick J. Wong 	 * bno/cnt/rmap btrees but are not now.  This is a chicken and egg
400f9ed6debSDarrick J. Wong 	 * problem: the AGF is corrupt, so we have to trust the AGFL contents
401f9ed6debSDarrick J. Wong 	 * because we can't do any serious cross-referencing with any of the
402f9ed6debSDarrick J. Wong 	 * btrees rooted in the AGF.  If the AGFL contents are obviously bad
403f9ed6debSDarrick J. Wong 	 * then we'll bail out.
404f9ed6debSDarrick J. Wong 	 */
405cec7bb7dSDave Chinner 	error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
406f9ed6debSDarrick J. Wong 	if (error)
407f9ed6debSDarrick J. Wong 		return error;
408f9ed6debSDarrick J. Wong 
409f9ed6debSDarrick J. Wong 	/*
410f9ed6debSDarrick J. Wong 	 * Spot-check the AGFL blocks; if they're obviously corrupt then
411f9ed6debSDarrick J. Wong 	 * there's nothing we can do but bail out.
412f9ed6debSDarrick J. Wong 	 */
4139798f615SChristoph Hellwig 	error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
414f9ed6debSDarrick J. Wong 			xrep_agf_check_agfl_block, sc);
415f9ed6debSDarrick J. Wong 	if (error)
416f9ed6debSDarrick J. Wong 		return error;
417f9ed6debSDarrick J. Wong 
418f9ed6debSDarrick J. Wong 	/*
419f9ed6debSDarrick J. Wong 	 * Find the AGF btree roots.  This is also a chicken-and-egg situation;
420f9ed6debSDarrick J. Wong 	 * see the function for more details.
421f9ed6debSDarrick J. Wong 	 */
422f9ed6debSDarrick J. Wong 	error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
423f9ed6debSDarrick J. Wong 	if (error)
424f9ed6debSDarrick J. Wong 		return error;
425f9ed6debSDarrick J. Wong 
426f9ed6debSDarrick J. Wong 	/* Start rewriting the header and implant the btrees we found. */
427f9ed6debSDarrick J. Wong 	xrep_agf_init_header(sc, agf_bp, &old_agf);
428f9ed6debSDarrick J. Wong 	xrep_agf_set_roots(sc, agf, fab);
429f9ed6debSDarrick J. Wong 	error = xrep_agf_calc_from_btrees(sc, agf_bp);
430f9ed6debSDarrick J. Wong 	if (error)
431f9ed6debSDarrick J. Wong 		goto out_revert;
432f9ed6debSDarrick J. Wong 
433f9ed6debSDarrick J. Wong 	/* Commit the changes and reinitialize incore state. */
434f9ed6debSDarrick J. Wong 	return xrep_agf_commit_new(sc, agf_bp);
435f9ed6debSDarrick J. Wong 
436f9ed6debSDarrick J. Wong out_revert:
437f9ed6debSDarrick J. Wong 	/* Mark the incore AGF state stale and revert the AGF. */
4387ac2ff8bSDave Chinner 	clear_bit(XFS_AGSTATE_AGF_INIT, &sc->sa.pag->pag_opstate);
439f9ed6debSDarrick J. Wong 	memcpy(agf, &old_agf, sizeof(old_agf));
440f9ed6debSDarrick J. Wong 	return error;
441f9ed6debSDarrick J. Wong }
4420e93d3f4SDarrick J. Wong 
4430e93d3f4SDarrick J. Wong /* AGFL */
4440e93d3f4SDarrick J. Wong 
4450e93d3f4SDarrick J. Wong struct xrep_agfl {
446b255fab0SDarrick J. Wong 	/* Bitmap of alleged AGFL blocks that we're not going to add. */
447b255fab0SDarrick J. Wong 	struct xbitmap		crossed;
448b255fab0SDarrick J. Wong 
4490e93d3f4SDarrick J. Wong 	/* Bitmap of other OWN_AG metadata blocks. */
45000b10d48SDarrick J. Wong 	struct xbitmap		agmetablocks;
4510e93d3f4SDarrick J. Wong 
4520e93d3f4SDarrick J. Wong 	/* Bitmap of free space. */
45300b10d48SDarrick J. Wong 	struct xbitmap		*freesp;
4540e93d3f4SDarrick J. Wong 
455b255fab0SDarrick J. Wong 	/* rmapbt cursor for finding crosslinked blocks */
456b255fab0SDarrick J. Wong 	struct xfs_btree_cur	*rmap_cur;
457b255fab0SDarrick J. Wong 
4580e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc;
4590e93d3f4SDarrick J. Wong };
4600e93d3f4SDarrick J. Wong 
4610e93d3f4SDarrick J. Wong /* Record all OWN_AG (free space btree) information from the rmap data. */
4620e93d3f4SDarrick J. Wong STATIC int
4630e93d3f4SDarrick J. Wong xrep_agfl_walk_rmap(
4640e93d3f4SDarrick J. Wong 	struct xfs_btree_cur	*cur,
465159eb69dSDarrick J. Wong 	const struct xfs_rmap_irec *rec,
4660e93d3f4SDarrick J. Wong 	void			*priv)
4670e93d3f4SDarrick J. Wong {
4680e93d3f4SDarrick J. Wong 	struct xrep_agfl	*ra = priv;
4690e93d3f4SDarrick J. Wong 	xfs_fsblock_t		fsb;
4700e93d3f4SDarrick J. Wong 	int			error = 0;
4710e93d3f4SDarrick J. Wong 
4720e93d3f4SDarrick J. Wong 	if (xchk_should_terminate(ra->sc, &error))
4730e93d3f4SDarrick J. Wong 		return error;
4740e93d3f4SDarrick J. Wong 
4750e93d3f4SDarrick J. Wong 	/* Record all the OWN_AG blocks. */
4760e93d3f4SDarrick J. Wong 	if (rec->rm_owner == XFS_RMAP_OWN_AG) {
47750f02fe3SDave Chinner 		fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.pag->pag_agno,
4780e93d3f4SDarrick J. Wong 				rec->rm_startblock);
47900b10d48SDarrick J. Wong 		error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
4800e93d3f4SDarrick J. Wong 		if (error)
4810e93d3f4SDarrick J. Wong 			return error;
4820e93d3f4SDarrick J. Wong 	}
4830e93d3f4SDarrick J. Wong 
48400b10d48SDarrick J. Wong 	return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
4850e93d3f4SDarrick J. Wong }
4860e93d3f4SDarrick J. Wong 
487b255fab0SDarrick J. Wong /* Strike out the blocks that are cross-linked according to the rmapbt. */
488b255fab0SDarrick J. Wong STATIC int
489b255fab0SDarrick J. Wong xrep_agfl_check_extent(
490b255fab0SDarrick J. Wong 	uint64_t		start,
491*178b48d5SDarrick J. Wong 	uint64_t		len,
492*178b48d5SDarrick J. Wong 	void			*priv)
493b255fab0SDarrick J. Wong {
494*178b48d5SDarrick J. Wong 	struct xrep_agfl	*ra = priv;
495b255fab0SDarrick J. Wong 	xfs_agblock_t		agbno = XFS_FSB_TO_AGBNO(ra->sc->mp, start);
496b255fab0SDarrick J. Wong 	xfs_agblock_t		last_agbno = agbno + len - 1;
497b255fab0SDarrick J. Wong 	int			error;
498b255fab0SDarrick J. Wong 
499b255fab0SDarrick J. Wong 	ASSERT(XFS_FSB_TO_AGNO(ra->sc->mp, start) == ra->sc->sa.pag->pag_agno);
500b255fab0SDarrick J. Wong 
501b255fab0SDarrick J. Wong 	while (agbno <= last_agbno) {
502b255fab0SDarrick J. Wong 		bool		other_owners;
503b255fab0SDarrick J. Wong 
504b255fab0SDarrick J. Wong 		error = xfs_rmap_has_other_keys(ra->rmap_cur, agbno, 1,
505b255fab0SDarrick J. Wong 				&XFS_RMAP_OINFO_AG, &other_owners);
506b255fab0SDarrick J. Wong 		if (error)
507b255fab0SDarrick J. Wong 			return error;
508b255fab0SDarrick J. Wong 
509b255fab0SDarrick J. Wong 		if (other_owners) {
510b255fab0SDarrick J. Wong 			error = xbitmap_set(&ra->crossed, agbno, 1);
511b255fab0SDarrick J. Wong 			if (error)
512b255fab0SDarrick J. Wong 				return error;
513b255fab0SDarrick J. Wong 		}
514b255fab0SDarrick J. Wong 
515b255fab0SDarrick J. Wong 		if (xchk_should_terminate(ra->sc, &error))
516b255fab0SDarrick J. Wong 			return error;
517b255fab0SDarrick J. Wong 		agbno++;
518b255fab0SDarrick J. Wong 	}
519b255fab0SDarrick J. Wong 
520b255fab0SDarrick J. Wong 	return 0;
521b255fab0SDarrick J. Wong }
522b255fab0SDarrick J. Wong 
5230e93d3f4SDarrick J. Wong /*
5240e93d3f4SDarrick J. Wong  * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
5250e93d3f4SDarrick J. Wong  * which blocks belong to the AGFL.
5260e93d3f4SDarrick J. Wong  *
5270e93d3f4SDarrick J. Wong  * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
5280e93d3f4SDarrick J. Wong  * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
5290e93d3f4SDarrick J. Wong  * rmapbt).  These are the old AGFL blocks, so return that list and the number
5300e93d3f4SDarrick J. Wong  * of blocks we're actually going to put back on the AGFL.
5310e93d3f4SDarrick J. Wong  */
5320e93d3f4SDarrick J. Wong STATIC int
5330e93d3f4SDarrick J. Wong xrep_agfl_collect_blocks(
5340e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc,
5350e93d3f4SDarrick J. Wong 	struct xfs_buf		*agf_bp,
53600b10d48SDarrick J. Wong 	struct xbitmap		*agfl_extents,
5370e93d3f4SDarrick J. Wong 	xfs_agblock_t		*flcount)
5380e93d3f4SDarrick J. Wong {
5390e93d3f4SDarrick J. Wong 	struct xrep_agfl	ra;
5400e93d3f4SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
5410e93d3f4SDarrick J. Wong 	struct xfs_btree_cur	*cur;
5420e93d3f4SDarrick J. Wong 	int			error;
5430e93d3f4SDarrick J. Wong 
5440e93d3f4SDarrick J. Wong 	ra.sc = sc;
5450e93d3f4SDarrick J. Wong 	ra.freesp = agfl_extents;
54600b10d48SDarrick J. Wong 	xbitmap_init(&ra.agmetablocks);
547b255fab0SDarrick J. Wong 	xbitmap_init(&ra.crossed);
5480e93d3f4SDarrick J. Wong 
5490e93d3f4SDarrick J. Wong 	/* Find all space used by the free space btrees & rmapbt. */
550fa9c3c19SDave Chinner 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
5510e93d3f4SDarrick J. Wong 	error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
5520e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
553b255fab0SDarrick J. Wong 	if (error)
554b255fab0SDarrick J. Wong 		goto out_bmp;
5550e93d3f4SDarrick J. Wong 
5560e93d3f4SDarrick J. Wong 	/* Find all blocks currently being used by the bnobt. */
557289d38d2SDave Chinner 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
558be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_BNO);
55900b10d48SDarrick J. Wong 	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
5600e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
561b255fab0SDarrick J. Wong 	if (error)
562b255fab0SDarrick J. Wong 		goto out_bmp;
5630e93d3f4SDarrick J. Wong 
5640e93d3f4SDarrick J. Wong 	/* Find all blocks currently being used by the cntbt. */
565289d38d2SDave Chinner 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
566be9fb17dSDave Chinner 			sc->sa.pag, XFS_BTNUM_CNT);
56700b10d48SDarrick J. Wong 	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
5680e93d3f4SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
569b255fab0SDarrick J. Wong 	if (error)
570b255fab0SDarrick J. Wong 		goto out_bmp;
5710e93d3f4SDarrick J. Wong 
5720e93d3f4SDarrick J. Wong 	/*
5730e93d3f4SDarrick J. Wong 	 * Drop the freesp meta blocks that are in use by btrees.
5740e93d3f4SDarrick J. Wong 	 * The remaining blocks /should/ be AGFL blocks.
5750e93d3f4SDarrick J. Wong 	 */
57600b10d48SDarrick J. Wong 	error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
5770e93d3f4SDarrick J. Wong 	if (error)
578b255fab0SDarrick J. Wong 		goto out_bmp;
579b255fab0SDarrick J. Wong 
580b255fab0SDarrick J. Wong 	/* Strike out the blocks that are cross-linked. */
581b255fab0SDarrick J. Wong 	ra.rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
582*178b48d5SDarrick J. Wong 	error = xbitmap_walk(agfl_extents, xrep_agfl_check_extent, &ra);
583b255fab0SDarrick J. Wong 	xfs_btree_del_cursor(ra.rmap_cur, error);
584b255fab0SDarrick J. Wong 	if (error)
585b255fab0SDarrick J. Wong 		goto out_bmp;
586b255fab0SDarrick J. Wong 	error = xbitmap_disunion(agfl_extents, &ra.crossed);
587b255fab0SDarrick J. Wong 	if (error)
588b255fab0SDarrick J. Wong 		goto out_bmp;
5890e93d3f4SDarrick J. Wong 
5900e93d3f4SDarrick J. Wong 	/*
5910e93d3f4SDarrick J. Wong 	 * Calculate the new AGFL size.  If we found more blocks than fit in
5920e93d3f4SDarrick J. Wong 	 * the AGFL we'll free them later.
5930e93d3f4SDarrick J. Wong 	 */
594608eb3ceSDarrick J. Wong 	*flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
595608eb3ceSDarrick J. Wong 			 xfs_agfl_size(mp));
5960e93d3f4SDarrick J. Wong 
597b255fab0SDarrick J. Wong out_bmp:
598b255fab0SDarrick J. Wong 	xbitmap_destroy(&ra.crossed);
59900b10d48SDarrick J. Wong 	xbitmap_destroy(&ra.agmetablocks);
6000e93d3f4SDarrick J. Wong 	return error;
6010e93d3f4SDarrick J. Wong }
6020e93d3f4SDarrick J. Wong 
6030e93d3f4SDarrick J. Wong /* Update the AGF and reset the in-core state. */
6040e93d3f4SDarrick J. Wong STATIC void
6050e93d3f4SDarrick J. Wong xrep_agfl_update_agf(
6060e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc,
6070e93d3f4SDarrick J. Wong 	struct xfs_buf		*agf_bp,
6080e93d3f4SDarrick J. Wong 	xfs_agblock_t		flcount)
6090e93d3f4SDarrick J. Wong {
6109798f615SChristoph Hellwig 	struct xfs_agf		*agf = agf_bp->b_addr;
6110e93d3f4SDarrick J. Wong 
6120e93d3f4SDarrick J. Wong 	ASSERT(flcount <= xfs_agfl_size(sc->mp));
6130e93d3f4SDarrick J. Wong 
6140e93d3f4SDarrick J. Wong 	/* Trigger fdblocks recalculation */
6150e93d3f4SDarrick J. Wong 	xfs_force_summary_recalc(sc->mp);
6160e93d3f4SDarrick J. Wong 
6170e93d3f4SDarrick J. Wong 	/* Update the AGF counters. */
6187ac2ff8bSDave Chinner 	if (xfs_perag_initialised_agf(sc->sa.pag))
6190e93d3f4SDarrick J. Wong 		sc->sa.pag->pagf_flcount = flcount;
6200e93d3f4SDarrick J. Wong 	agf->agf_flfirst = cpu_to_be32(0);
6210e93d3f4SDarrick J. Wong 	agf->agf_flcount = cpu_to_be32(flcount);
6220e93d3f4SDarrick J. Wong 	agf->agf_fllast = cpu_to_be32(flcount - 1);
6230e93d3f4SDarrick J. Wong 
6240e93d3f4SDarrick J. Wong 	xfs_alloc_log_agf(sc->tp, agf_bp,
6250e93d3f4SDarrick J. Wong 			XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
6260e93d3f4SDarrick J. Wong }
6270e93d3f4SDarrick J. Wong 
628*178b48d5SDarrick J. Wong struct xrep_agfl_fill {
629*178b48d5SDarrick J. Wong 	struct xbitmap		used_extents;
630*178b48d5SDarrick J. Wong 	struct xfs_scrub	*sc;
631*178b48d5SDarrick J. Wong 	__be32			*agfl_bno;
632*178b48d5SDarrick J. Wong 	xfs_agblock_t		flcount;
633*178b48d5SDarrick J. Wong 	unsigned int		fl_off;
634*178b48d5SDarrick J. Wong };
635*178b48d5SDarrick J. Wong 
636*178b48d5SDarrick J. Wong /* Fill the AGFL with whatever blocks are in this extent. */
637*178b48d5SDarrick J. Wong static int
638*178b48d5SDarrick J. Wong xrep_agfl_fill(
639*178b48d5SDarrick J. Wong 	uint64_t		start,
640*178b48d5SDarrick J. Wong 	uint64_t		len,
641*178b48d5SDarrick J. Wong 	void			*priv)
642*178b48d5SDarrick J. Wong {
643*178b48d5SDarrick J. Wong 	struct xrep_agfl_fill	*af = priv;
644*178b48d5SDarrick J. Wong 	struct xfs_scrub	*sc = af->sc;
645*178b48d5SDarrick J. Wong 	xfs_fsblock_t		fsbno = start;
646*178b48d5SDarrick J. Wong 	int			error;
647*178b48d5SDarrick J. Wong 
648*178b48d5SDarrick J. Wong 	while (fsbno < start + len && af->fl_off < af->flcount)
649*178b48d5SDarrick J. Wong 		af->agfl_bno[af->fl_off++] =
650*178b48d5SDarrick J. Wong 				cpu_to_be32(XFS_FSB_TO_AGBNO(sc->mp, fsbno++));
651*178b48d5SDarrick J. Wong 
652*178b48d5SDarrick J. Wong 	trace_xrep_agfl_insert(sc->mp, sc->sa.pag->pag_agno,
653*178b48d5SDarrick J. Wong 			XFS_FSB_TO_AGBNO(sc->mp, start), len);
654*178b48d5SDarrick J. Wong 
655*178b48d5SDarrick J. Wong 	error = xbitmap_set(&af->used_extents, start, fsbno - 1);
656*178b48d5SDarrick J. Wong 	if (error)
657*178b48d5SDarrick J. Wong 		return error;
658*178b48d5SDarrick J. Wong 
659*178b48d5SDarrick J. Wong 	if (af->fl_off == af->flcount)
660*178b48d5SDarrick J. Wong 		return -ECANCELED;
661*178b48d5SDarrick J. Wong 
662*178b48d5SDarrick J. Wong 	return 0;
663*178b48d5SDarrick J. Wong }
664*178b48d5SDarrick J. Wong 
6650e93d3f4SDarrick J. Wong /* Write out a totally new AGFL. */
6660e93d3f4SDarrick J. Wong STATIC void
6670e93d3f4SDarrick J. Wong xrep_agfl_init_header(
6680e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc,
6690e93d3f4SDarrick J. Wong 	struct xfs_buf		*agfl_bp,
67000b10d48SDarrick J. Wong 	struct xbitmap		*agfl_extents,
6710e93d3f4SDarrick J. Wong 	xfs_agblock_t		flcount)
6720e93d3f4SDarrick J. Wong {
673*178b48d5SDarrick J. Wong 	struct xrep_agfl_fill	af = {
674*178b48d5SDarrick J. Wong 		.sc		= sc,
675*178b48d5SDarrick J. Wong 		.flcount	= flcount,
676*178b48d5SDarrick J. Wong 	};
6770e93d3f4SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
6780e93d3f4SDarrick J. Wong 	struct xfs_agfl		*agfl;
6790e93d3f4SDarrick J. Wong 
6800e93d3f4SDarrick J. Wong 	ASSERT(flcount <= xfs_agfl_size(mp));
6810e93d3f4SDarrick J. Wong 
6820e93d3f4SDarrick J. Wong 	/*
6830e93d3f4SDarrick J. Wong 	 * Start rewriting the header by setting the bno[] array to
6840e93d3f4SDarrick J. Wong 	 * NULLAGBLOCK, then setting AGFL header fields.
6850e93d3f4SDarrick J. Wong 	 */
6860e93d3f4SDarrick J. Wong 	agfl = XFS_BUF_TO_AGFL(agfl_bp);
6870e93d3f4SDarrick J. Wong 	memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
6880e93d3f4SDarrick J. Wong 	agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
68954406764SDarrick J. Wong 	agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
6900e93d3f4SDarrick J. Wong 	uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
6910e93d3f4SDarrick J. Wong 
6920e93d3f4SDarrick J. Wong 	/*
6930e93d3f4SDarrick J. Wong 	 * Fill the AGFL with the remaining blocks.  If agfl_extents has more
6940e93d3f4SDarrick J. Wong 	 * blocks than fit in the AGFL, they will be freed in a subsequent
6950e93d3f4SDarrick J. Wong 	 * step.
6960e93d3f4SDarrick J. Wong 	 */
697*178b48d5SDarrick J. Wong 	xbitmap_init(&af.used_extents);
698*178b48d5SDarrick J. Wong 	af.agfl_bno = xfs_buf_to_agfl_bno(agfl_bp),
699*178b48d5SDarrick J. Wong 	xbitmap_walk(agfl_extents, xrep_agfl_fill, &af);
700*178b48d5SDarrick J. Wong 	xbitmap_disunion(agfl_extents, &af.used_extents);
7010e93d3f4SDarrick J. Wong 
7020e93d3f4SDarrick J. Wong 	/* Write new AGFL to disk. */
7030e93d3f4SDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
7040e93d3f4SDarrick J. Wong 	xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
705*178b48d5SDarrick J. Wong 	xbitmap_destroy(&af.used_extents);
7060e93d3f4SDarrick J. Wong }
7070e93d3f4SDarrick J. Wong 
7080e93d3f4SDarrick J. Wong /* Repair the AGFL. */
7090e93d3f4SDarrick J. Wong int
7100e93d3f4SDarrick J. Wong xrep_agfl(
7110e93d3f4SDarrick J. Wong 	struct xfs_scrub	*sc)
7120e93d3f4SDarrick J. Wong {
71300b10d48SDarrick J. Wong 	struct xbitmap		agfl_extents;
7140e93d3f4SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
7150e93d3f4SDarrick J. Wong 	struct xfs_buf		*agf_bp;
7160e93d3f4SDarrick J. Wong 	struct xfs_buf		*agfl_bp;
7170e93d3f4SDarrick J. Wong 	xfs_agblock_t		flcount;
7180e93d3f4SDarrick J. Wong 	int			error;
7190e93d3f4SDarrick J. Wong 
7200e93d3f4SDarrick J. Wong 	/* We require the rmapbt to rebuild anything. */
72138c26bfdSDave Chinner 	if (!xfs_has_rmapbt(mp))
7220e93d3f4SDarrick J. Wong 		return -EOPNOTSUPP;
7230e93d3f4SDarrick J. Wong 
72400b10d48SDarrick J. Wong 	xbitmap_init(&agfl_extents);
7250e93d3f4SDarrick J. Wong 
7260e93d3f4SDarrick J. Wong 	/*
7270e93d3f4SDarrick J. Wong 	 * Read the AGF so that we can query the rmapbt.  We hope that there's
7280e93d3f4SDarrick J. Wong 	 * nothing wrong with the AGF, but all the AG header repair functions
7290e93d3f4SDarrick J. Wong 	 * have this chicken-and-egg problem.
7300e93d3f4SDarrick J. Wong 	 */
73108d3e84fSDave Chinner 	error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
7320e93d3f4SDarrick J. Wong 	if (error)
7330e93d3f4SDarrick J. Wong 		return error;
7340e93d3f4SDarrick J. Wong 
7350e93d3f4SDarrick J. Wong 	/*
7360e93d3f4SDarrick J. Wong 	 * Make sure we have the AGFL buffer, as scrub might have decided it
7370e93d3f4SDarrick J. Wong 	 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
7380e93d3f4SDarrick J. Wong 	 */
7390e93d3f4SDarrick J. Wong 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
74054406764SDarrick J. Wong 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
74154406764SDarrick J. Wong 						XFS_AGFL_DADDR(mp)),
7420e93d3f4SDarrick J. Wong 			XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
7430e93d3f4SDarrick J. Wong 	if (error)
7440e93d3f4SDarrick J. Wong 		return error;
7450e93d3f4SDarrick J. Wong 	agfl_bp->b_ops = &xfs_agfl_buf_ops;
7460e93d3f4SDarrick J. Wong 
7470e93d3f4SDarrick J. Wong 	/* Gather all the extents we're going to put on the new AGFL. */
7480e93d3f4SDarrick J. Wong 	error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
7490e93d3f4SDarrick J. Wong 	if (error)
7500e93d3f4SDarrick J. Wong 		goto err;
7510e93d3f4SDarrick J. Wong 
7520e93d3f4SDarrick J. Wong 	/*
7530e93d3f4SDarrick J. Wong 	 * Update AGF and AGFL.  We reset the global free block counter when
7540e93d3f4SDarrick J. Wong 	 * we adjust the AGF flcount (which can fail) so avoid updating any
7550e93d3f4SDarrick J. Wong 	 * buffers until we know that part works.
7560e93d3f4SDarrick J. Wong 	 */
7570e93d3f4SDarrick J. Wong 	xrep_agfl_update_agf(sc, agf_bp, flcount);
7580e93d3f4SDarrick J. Wong 	xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
7590e93d3f4SDarrick J. Wong 
7600e93d3f4SDarrick J. Wong 	/*
7610e93d3f4SDarrick J. Wong 	 * Ok, the AGFL should be ready to go now.  Roll the transaction to
7620e93d3f4SDarrick J. Wong 	 * make the new AGFL permanent before we start using it to return
7630e93d3f4SDarrick J. Wong 	 * freespace overflow to the freespace btrees.
7640e93d3f4SDarrick J. Wong 	 */
7650e93d3f4SDarrick J. Wong 	sc->sa.agf_bp = agf_bp;
7660e93d3f4SDarrick J. Wong 	error = xrep_roll_ag_trans(sc);
7670e93d3f4SDarrick J. Wong 	if (error)
7680e93d3f4SDarrick J. Wong 		goto err;
7690e93d3f4SDarrick J. Wong 
7700e93d3f4SDarrick J. Wong 	/* Dump any AGFL overflow. */
77137a6547dSDarrick J. Wong 	error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
7727280fedaSDarrick J. Wong 			XFS_AG_RESV_AGFL);
7730e93d3f4SDarrick J. Wong err:
77400b10d48SDarrick J. Wong 	xbitmap_destroy(&agfl_extents);
7750e93d3f4SDarrick J. Wong 	return error;
7760e93d3f4SDarrick J. Wong }
77713942aa9SDarrick J. Wong 
77813942aa9SDarrick J. Wong /* AGI */
77913942aa9SDarrick J. Wong 
78013942aa9SDarrick J. Wong /*
78113942aa9SDarrick J. Wong  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
78213942aa9SDarrick J. Wong  * XFS_BTNUM_ names here to avoid creating a sparse array.
78313942aa9SDarrick J. Wong  */
78413942aa9SDarrick J. Wong enum {
78513942aa9SDarrick J. Wong 	XREP_AGI_INOBT = 0,
78613942aa9SDarrick J. Wong 	XREP_AGI_FINOBT,
78713942aa9SDarrick J. Wong 	XREP_AGI_END,
78813942aa9SDarrick J. Wong 	XREP_AGI_MAX
78913942aa9SDarrick J. Wong };
79013942aa9SDarrick J. Wong 
79113942aa9SDarrick J. Wong /*
79213942aa9SDarrick J. Wong  * Given the inode btree roots described by *fab, find the roots, check them
79313942aa9SDarrick J. Wong  * for sanity, and pass the root data back out via *fab.
79413942aa9SDarrick J. Wong  */
79513942aa9SDarrick J. Wong STATIC int
79613942aa9SDarrick J. Wong xrep_agi_find_btrees(
79713942aa9SDarrick J. Wong 	struct xfs_scrub		*sc,
79813942aa9SDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
79913942aa9SDarrick J. Wong {
80013942aa9SDarrick J. Wong 	struct xfs_buf			*agf_bp;
80113942aa9SDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
80213942aa9SDarrick J. Wong 	int				error;
80313942aa9SDarrick J. Wong 
80413942aa9SDarrick J. Wong 	/* Read the AGF. */
80508d3e84fSDave Chinner 	error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
80613942aa9SDarrick J. Wong 	if (error)
80713942aa9SDarrick J. Wong 		return error;
80813942aa9SDarrick J. Wong 
80913942aa9SDarrick J. Wong 	/* Find the btree roots. */
81013942aa9SDarrick J. Wong 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
81113942aa9SDarrick J. Wong 	if (error)
81213942aa9SDarrick J. Wong 		return error;
81313942aa9SDarrick J. Wong 
81413942aa9SDarrick J. Wong 	/* We must find the inobt root. */
81513942aa9SDarrick J. Wong 	if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
81613942aa9SDarrick J. Wong 		return -EFSCORRUPTED;
81713942aa9SDarrick J. Wong 
81813942aa9SDarrick J. Wong 	/* We must find the finobt root if that feature is enabled. */
81938c26bfdSDave Chinner 	if (xfs_has_finobt(mp) &&
82013942aa9SDarrick J. Wong 	    !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
82113942aa9SDarrick J. Wong 		return -EFSCORRUPTED;
82213942aa9SDarrick J. Wong 
82313942aa9SDarrick J. Wong 	return 0;
82413942aa9SDarrick J. Wong }
82513942aa9SDarrick J. Wong 
82613942aa9SDarrick J. Wong /*
82713942aa9SDarrick J. Wong  * Reinitialize the AGI header, making an in-core copy of the old contents so
82813942aa9SDarrick J. Wong  * that we know which in-core state needs to be reinitialized.
82913942aa9SDarrick J. Wong  */
83013942aa9SDarrick J. Wong STATIC void
83113942aa9SDarrick J. Wong xrep_agi_init_header(
83213942aa9SDarrick J. Wong 	struct xfs_scrub	*sc,
83313942aa9SDarrick J. Wong 	struct xfs_buf		*agi_bp,
83413942aa9SDarrick J. Wong 	struct xfs_agi		*old_agi)
83513942aa9SDarrick J. Wong {
836370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agi_bp->b_addr;
8377ac2ff8bSDave Chinner 	struct xfs_perag	*pag = sc->sa.pag;
83813942aa9SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
83913942aa9SDarrick J. Wong 
84013942aa9SDarrick J. Wong 	memcpy(old_agi, agi, sizeof(*old_agi));
84113942aa9SDarrick J. Wong 	memset(agi, 0, BBTOB(agi_bp->b_length));
84213942aa9SDarrick J. Wong 	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
84313942aa9SDarrick J. Wong 	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
8447ac2ff8bSDave Chinner 	agi->agi_seqno = cpu_to_be32(pag->pag_agno);
8457ac2ff8bSDave Chinner 	agi->agi_length = cpu_to_be32(pag->block_count);
84613942aa9SDarrick J. Wong 	agi->agi_newino = cpu_to_be32(NULLAGINO);
84713942aa9SDarrick J. Wong 	agi->agi_dirino = cpu_to_be32(NULLAGINO);
84838c26bfdSDave Chinner 	if (xfs_has_crc(mp))
84913942aa9SDarrick J. Wong 		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
85013942aa9SDarrick J. Wong 
85113942aa9SDarrick J. Wong 	/* We don't know how to fix the unlinked list yet. */
85213942aa9SDarrick J. Wong 	memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
85313942aa9SDarrick J. Wong 			sizeof(agi->agi_unlinked));
85413942aa9SDarrick J. Wong 
85513942aa9SDarrick J. Wong 	/* Mark the incore AGF data stale until we're done fixing things. */
8567ac2ff8bSDave Chinner 	ASSERT(xfs_perag_initialised_agi(pag));
8577ac2ff8bSDave Chinner 	clear_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
85813942aa9SDarrick J. Wong }
85913942aa9SDarrick J. Wong 
86013942aa9SDarrick J. Wong /* Set btree root information in an AGI. */
86113942aa9SDarrick J. Wong STATIC void
86213942aa9SDarrick J. Wong xrep_agi_set_roots(
86313942aa9SDarrick J. Wong 	struct xfs_scrub		*sc,
86413942aa9SDarrick J. Wong 	struct xfs_agi			*agi,
86513942aa9SDarrick J. Wong 	struct xrep_find_ag_btree	*fab)
86613942aa9SDarrick J. Wong {
86713942aa9SDarrick J. Wong 	agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
86813942aa9SDarrick J. Wong 	agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
86913942aa9SDarrick J. Wong 
87038c26bfdSDave Chinner 	if (xfs_has_finobt(sc->mp)) {
87113942aa9SDarrick J. Wong 		agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
87213942aa9SDarrick J. Wong 		agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
87313942aa9SDarrick J. Wong 	}
87413942aa9SDarrick J. Wong }
87513942aa9SDarrick J. Wong 
87613942aa9SDarrick J. Wong /* Update the AGI counters. */
87713942aa9SDarrick J. Wong STATIC int
87813942aa9SDarrick J. Wong xrep_agi_calc_from_btrees(
87913942aa9SDarrick J. Wong 	struct xfs_scrub	*sc,
88013942aa9SDarrick J. Wong 	struct xfs_buf		*agi_bp)
88113942aa9SDarrick J. Wong {
88213942aa9SDarrick J. Wong 	struct xfs_btree_cur	*cur;
883370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agi_bp->b_addr;
88413942aa9SDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
88513942aa9SDarrick J. Wong 	xfs_agino_t		count;
88613942aa9SDarrick J. Wong 	xfs_agino_t		freecount;
88713942aa9SDarrick J. Wong 	int			error;
88813942aa9SDarrick J. Wong 
889bab8b795SDave Chinner 	cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, agi_bp, XFS_BTNUM_INO);
89013942aa9SDarrick J. Wong 	error = xfs_ialloc_count_inodes(cur, &count, &freecount);
89113942aa9SDarrick J. Wong 	if (error)
89213942aa9SDarrick J. Wong 		goto err;
893ebd9027dSDave Chinner 	if (xfs_has_inobtcounts(mp)) {
89411f74423SDarrick J. Wong 		xfs_agblock_t	blocks;
89511f74423SDarrick J. Wong 
89611f74423SDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &blocks);
89711f74423SDarrick J. Wong 		if (error)
89811f74423SDarrick J. Wong 			goto err;
89911f74423SDarrick J. Wong 		agi->agi_iblocks = cpu_to_be32(blocks);
90011f74423SDarrick J. Wong 	}
90113942aa9SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
90213942aa9SDarrick J. Wong 
90313942aa9SDarrick J. Wong 	agi->agi_count = cpu_to_be32(count);
90413942aa9SDarrick J. Wong 	agi->agi_freecount = cpu_to_be32(freecount);
90511f74423SDarrick J. Wong 
906ebd9027dSDave Chinner 	if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
90711f74423SDarrick J. Wong 		xfs_agblock_t	blocks;
90811f74423SDarrick J. Wong 
909bab8b795SDave Chinner 		cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, agi_bp,
910bab8b795SDave Chinner 				XFS_BTNUM_FINO);
91111f74423SDarrick J. Wong 		error = xfs_btree_count_blocks(cur, &blocks);
91211f74423SDarrick J. Wong 		if (error)
91311f74423SDarrick J. Wong 			goto err;
91411f74423SDarrick J. Wong 		xfs_btree_del_cursor(cur, error);
91511f74423SDarrick J. Wong 		agi->agi_fblocks = cpu_to_be32(blocks);
91611f74423SDarrick J. Wong 	}
91711f74423SDarrick J. Wong 
91813942aa9SDarrick J. Wong 	return 0;
91913942aa9SDarrick J. Wong err:
92013942aa9SDarrick J. Wong 	xfs_btree_del_cursor(cur, error);
92113942aa9SDarrick J. Wong 	return error;
92213942aa9SDarrick J. Wong }
92313942aa9SDarrick J. Wong 
92413942aa9SDarrick J. Wong /* Trigger reinitialization of the in-core data. */
92513942aa9SDarrick J. Wong STATIC int
92613942aa9SDarrick J. Wong xrep_agi_commit_new(
92713942aa9SDarrick J. Wong 	struct xfs_scrub	*sc,
92813942aa9SDarrick J. Wong 	struct xfs_buf		*agi_bp)
92913942aa9SDarrick J. Wong {
93013942aa9SDarrick J. Wong 	struct xfs_perag	*pag;
931370c782bSChristoph Hellwig 	struct xfs_agi		*agi = agi_bp->b_addr;
93213942aa9SDarrick J. Wong 
93313942aa9SDarrick J. Wong 	/* Trigger inode count recalculation */
93413942aa9SDarrick J. Wong 	xfs_force_summary_recalc(sc->mp);
93513942aa9SDarrick J. Wong 
93613942aa9SDarrick J. Wong 	/* Write this to disk. */
93713942aa9SDarrick J. Wong 	xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
93813942aa9SDarrick J. Wong 	xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
93913942aa9SDarrick J. Wong 
94013942aa9SDarrick J. Wong 	/* Now reinitialize the in-core counters if necessary. */
94113942aa9SDarrick J. Wong 	pag = sc->sa.pag;
94213942aa9SDarrick J. Wong 	pag->pagi_count = be32_to_cpu(agi->agi_count);
94313942aa9SDarrick J. Wong 	pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
9447ac2ff8bSDave Chinner 	set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
94513942aa9SDarrick J. Wong 
94613942aa9SDarrick J. Wong 	return 0;
94713942aa9SDarrick J. Wong }
94813942aa9SDarrick J. Wong 
94913942aa9SDarrick J. Wong /* Repair the AGI. */
95013942aa9SDarrick J. Wong int
95113942aa9SDarrick J. Wong xrep_agi(
95213942aa9SDarrick J. Wong 	struct xfs_scrub		*sc)
95313942aa9SDarrick J. Wong {
95413942aa9SDarrick J. Wong 	struct xrep_find_ag_btree	fab[XREP_AGI_MAX] = {
95513942aa9SDarrick J. Wong 		[XREP_AGI_INOBT] = {
95613942aa9SDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_INOBT,
95713942aa9SDarrick J. Wong 			.buf_ops = &xfs_inobt_buf_ops,
9581ba6fd34SDarrick J. Wong 			.maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
95913942aa9SDarrick J. Wong 		},
96013942aa9SDarrick J. Wong 		[XREP_AGI_FINOBT] = {
96113942aa9SDarrick J. Wong 			.rmap_owner = XFS_RMAP_OWN_INOBT,
96201e68f40SBrian Foster 			.buf_ops = &xfs_finobt_buf_ops,
9631ba6fd34SDarrick J. Wong 			.maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
96413942aa9SDarrick J. Wong 		},
96513942aa9SDarrick J. Wong 		[XREP_AGI_END] = {
96613942aa9SDarrick J. Wong 			.buf_ops = NULL
96713942aa9SDarrick J. Wong 		},
96813942aa9SDarrick J. Wong 	};
96913942aa9SDarrick J. Wong 	struct xfs_agi			old_agi;
97013942aa9SDarrick J. Wong 	struct xfs_mount		*mp = sc->mp;
97113942aa9SDarrick J. Wong 	struct xfs_buf			*agi_bp;
97213942aa9SDarrick J. Wong 	struct xfs_agi			*agi;
97313942aa9SDarrick J. Wong 	int				error;
97413942aa9SDarrick J. Wong 
97513942aa9SDarrick J. Wong 	/* We require the rmapbt to rebuild anything. */
97638c26bfdSDave Chinner 	if (!xfs_has_rmapbt(mp))
97713942aa9SDarrick J. Wong 		return -EOPNOTSUPP;
97813942aa9SDarrick J. Wong 
97913942aa9SDarrick J. Wong 	/*
98013942aa9SDarrick J. Wong 	 * Make sure we have the AGI buffer, as scrub might have decided it
98113942aa9SDarrick J. Wong 	 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
98213942aa9SDarrick J. Wong 	 */
98313942aa9SDarrick J. Wong 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
98454406764SDarrick J. Wong 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
98554406764SDarrick J. Wong 						XFS_AGI_DADDR(mp)),
98613942aa9SDarrick J. Wong 			XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
98713942aa9SDarrick J. Wong 	if (error)
98813942aa9SDarrick J. Wong 		return error;
98913942aa9SDarrick J. Wong 	agi_bp->b_ops = &xfs_agi_buf_ops;
990370c782bSChristoph Hellwig 	agi = agi_bp->b_addr;
99113942aa9SDarrick J. Wong 
99213942aa9SDarrick J. Wong 	/* Find the AGI btree roots. */
99313942aa9SDarrick J. Wong 	error = xrep_agi_find_btrees(sc, fab);
99413942aa9SDarrick J. Wong 	if (error)
99513942aa9SDarrick J. Wong 		return error;
99613942aa9SDarrick J. Wong 
99713942aa9SDarrick J. Wong 	/* Start rewriting the header and implant the btrees we found. */
99813942aa9SDarrick J. Wong 	xrep_agi_init_header(sc, agi_bp, &old_agi);
99913942aa9SDarrick J. Wong 	xrep_agi_set_roots(sc, agi, fab);
100013942aa9SDarrick J. Wong 	error = xrep_agi_calc_from_btrees(sc, agi_bp);
100113942aa9SDarrick J. Wong 	if (error)
100213942aa9SDarrick J. Wong 		goto out_revert;
100313942aa9SDarrick J. Wong 
100413942aa9SDarrick J. Wong 	/* Reinitialize in-core state. */
100513942aa9SDarrick J. Wong 	return xrep_agi_commit_new(sc, agi_bp);
100613942aa9SDarrick J. Wong 
100713942aa9SDarrick J. Wong out_revert:
100813942aa9SDarrick J. Wong 	/* Mark the incore AGI state stale and revert the AGI. */
10097ac2ff8bSDave Chinner 	clear_bit(XFS_AGSTATE_AGI_INIT, &sc->sa.pag->pag_opstate);
101013942aa9SDarrick J. Wong 	memcpy(agi, &old_agi, sizeof(old_agi));
101113942aa9SDarrick J. Wong 	return error;
101213942aa9SDarrick J. Wong }
1013