xref: /linux/fs/xfs/scrub/agheader_repair.c (revision 2b0cfa6e49566c8fa6759734cf821aa6e8271a9e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2018-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_btree.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_sb.h"
16 #include "xfs_alloc.h"
17 #include "xfs_alloc_btree.h"
18 #include "xfs_ialloc.h"
19 #include "xfs_ialloc_btree.h"
20 #include "xfs_rmap.h"
21 #include "xfs_rmap_btree.h"
22 #include "xfs_refcount_btree.h"
23 #include "xfs_ag.h"
24 #include "scrub/scrub.h"
25 #include "scrub/common.h"
26 #include "scrub/trace.h"
27 #include "scrub/repair.h"
28 #include "scrub/bitmap.h"
29 #include "scrub/agb_bitmap.h"
30 #include "scrub/reap.h"
31 
32 /* Superblock */
33 
34 /* Repair the superblock. */
35 int
36 xrep_superblock(
37 	struct xfs_scrub	*sc)
38 {
39 	struct xfs_mount	*mp = sc->mp;
40 	struct xfs_buf		*bp;
41 	xfs_agnumber_t		agno;
42 	int			error;
43 
44 	/* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
45 	agno = sc->sm->sm_agno;
46 	if (agno == 0)
47 		return -EOPNOTSUPP;
48 
49 	error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
50 	if (error)
51 		return error;
52 
53 	/* Last chance to abort before we start committing fixes. */
54 	if (xchk_should_terminate(sc, &error))
55 		return error;
56 
57 	/* Copy AG 0's superblock to this one. */
58 	xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
59 	xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
60 
61 	/*
62 	 * Don't write out a secondary super with NEEDSREPAIR or log incompat
63 	 * features set, since both are ignored when set on a secondary.
64 	 */
65 	if (xfs_has_crc(mp)) {
66 		struct xfs_dsb		*sb = bp->b_addr;
67 
68 		sb->sb_features_incompat &=
69 				~cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
70 		sb->sb_features_log_incompat = 0;
71 	}
72 
73 	/* Write this to disk. */
74 	xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
75 	xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
76 	return 0;
77 }
78 
79 /* AGF */
80 
81 struct xrep_agf_allocbt {
82 	struct xfs_scrub	*sc;
83 	xfs_agblock_t		freeblks;
84 	xfs_agblock_t		longest;
85 };
86 
87 /* Record free space shape information. */
88 STATIC int
89 xrep_agf_walk_allocbt(
90 	struct xfs_btree_cur		*cur,
91 	const struct xfs_alloc_rec_incore *rec,
92 	void				*priv)
93 {
94 	struct xrep_agf_allocbt		*raa = priv;
95 	int				error = 0;
96 
97 	if (xchk_should_terminate(raa->sc, &error))
98 		return error;
99 
100 	raa->freeblks += rec->ar_blockcount;
101 	if (rec->ar_blockcount > raa->longest)
102 		raa->longest = rec->ar_blockcount;
103 	return error;
104 }
105 
106 /* Does this AGFL block look sane? */
107 STATIC int
108 xrep_agf_check_agfl_block(
109 	struct xfs_mount	*mp,
110 	xfs_agblock_t		agbno,
111 	void			*priv)
112 {
113 	struct xfs_scrub	*sc = priv;
114 
115 	if (!xfs_verify_agbno(sc->sa.pag, agbno))
116 		return -EFSCORRUPTED;
117 	return 0;
118 }
119 
120 /*
121  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
122  * XFS_BTNUM_ names here to avoid creating a sparse array.
123  */
124 enum {
125 	XREP_AGF_BNOBT = 0,
126 	XREP_AGF_CNTBT,
127 	XREP_AGF_RMAPBT,
128 	XREP_AGF_REFCOUNTBT,
129 	XREP_AGF_END,
130 	XREP_AGF_MAX
131 };
132 
133 /* Check a btree root candidate. */
134 static inline bool
135 xrep_check_btree_root(
136 	struct xfs_scrub		*sc,
137 	struct xrep_find_ag_btree	*fab)
138 {
139 	return xfs_verify_agbno(sc->sa.pag, fab->root) &&
140 	       fab->height <= fab->maxlevels;
141 }
142 
143 /*
144  * Given the btree roots described by *fab, find the roots, check them for
145  * sanity, and pass the root data back out via *fab.
146  *
147  * This is /also/ a chicken and egg problem because we have to use the rmapbt
148  * (rooted in the AGF) to find the btrees rooted in the AGF.  We also have no
149  * idea if the btrees make any sense.  If we hit obvious corruptions in those
150  * btrees we'll bail out.
151  */
152 STATIC int
153 xrep_agf_find_btrees(
154 	struct xfs_scrub		*sc,
155 	struct xfs_buf			*agf_bp,
156 	struct xrep_find_ag_btree	*fab,
157 	struct xfs_buf			*agfl_bp)
158 {
159 	struct xfs_agf			*old_agf = agf_bp->b_addr;
160 	int				error;
161 
162 	/* Go find the root data. */
163 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
164 	if (error)
165 		return error;
166 
167 	/* We must find the bnobt, cntbt, and rmapbt roots. */
168 	if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
169 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
170 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
171 		return -EFSCORRUPTED;
172 
173 	/*
174 	 * We relied on the rmapbt to reconstruct the AGF.  If we get a
175 	 * different root then something's seriously wrong.
176 	 */
177 	if (fab[XREP_AGF_RMAPBT].root !=
178 	    be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
179 		return -EFSCORRUPTED;
180 
181 	/* We must find the refcountbt root if that feature is enabled. */
182 	if (xfs_has_reflink(sc->mp) &&
183 	    !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
184 		return -EFSCORRUPTED;
185 
186 	return 0;
187 }
188 
189 /*
190  * Reinitialize the AGF header, making an in-core copy of the old contents so
191  * that we know which in-core state needs to be reinitialized.
192  */
193 STATIC void
194 xrep_agf_init_header(
195 	struct xfs_scrub	*sc,
196 	struct xfs_buf		*agf_bp,
197 	struct xfs_agf		*old_agf)
198 {
199 	struct xfs_mount	*mp = sc->mp;
200 	struct xfs_perag	*pag = sc->sa.pag;
201 	struct xfs_agf		*agf = agf_bp->b_addr;
202 
203 	memcpy(old_agf, agf, sizeof(*old_agf));
204 	memset(agf, 0, BBTOB(agf_bp->b_length));
205 	agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
206 	agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
207 	agf->agf_seqno = cpu_to_be32(pag->pag_agno);
208 	agf->agf_length = cpu_to_be32(pag->block_count);
209 	agf->agf_flfirst = old_agf->agf_flfirst;
210 	agf->agf_fllast = old_agf->agf_fllast;
211 	agf->agf_flcount = old_agf->agf_flcount;
212 	if (xfs_has_crc(mp))
213 		uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
214 
215 	/* Mark the incore AGF data stale until we're done fixing things. */
216 	ASSERT(xfs_perag_initialised_agf(pag));
217 	clear_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
218 }
219 
220 /* Set btree root information in an AGF. */
221 STATIC void
222 xrep_agf_set_roots(
223 	struct xfs_scrub		*sc,
224 	struct xfs_agf			*agf,
225 	struct xrep_find_ag_btree	*fab)
226 {
227 	agf->agf_roots[XFS_BTNUM_BNOi] =
228 			cpu_to_be32(fab[XREP_AGF_BNOBT].root);
229 	agf->agf_levels[XFS_BTNUM_BNOi] =
230 			cpu_to_be32(fab[XREP_AGF_BNOBT].height);
231 
232 	agf->agf_roots[XFS_BTNUM_CNTi] =
233 			cpu_to_be32(fab[XREP_AGF_CNTBT].root);
234 	agf->agf_levels[XFS_BTNUM_CNTi] =
235 			cpu_to_be32(fab[XREP_AGF_CNTBT].height);
236 
237 	agf->agf_roots[XFS_BTNUM_RMAPi] =
238 			cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
239 	agf->agf_levels[XFS_BTNUM_RMAPi] =
240 			cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
241 
242 	if (xfs_has_reflink(sc->mp)) {
243 		agf->agf_refcount_root =
244 				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
245 		agf->agf_refcount_level =
246 				cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
247 	}
248 }
249 
250 /* Update all AGF fields which derive from btree contents. */
251 STATIC int
252 xrep_agf_calc_from_btrees(
253 	struct xfs_scrub	*sc,
254 	struct xfs_buf		*agf_bp)
255 {
256 	struct xrep_agf_allocbt	raa = { .sc = sc };
257 	struct xfs_btree_cur	*cur = NULL;
258 	struct xfs_agf		*agf = agf_bp->b_addr;
259 	struct xfs_mount	*mp = sc->mp;
260 	xfs_agblock_t		btreeblks;
261 	xfs_agblock_t		blocks;
262 	int			error;
263 
264 	/* Update the AGF counters from the bnobt. */
265 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
266 			sc->sa.pag, XFS_BTNUM_BNO);
267 	error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
268 	if (error)
269 		goto err;
270 	error = xfs_btree_count_blocks(cur, &blocks);
271 	if (error)
272 		goto err;
273 	xfs_btree_del_cursor(cur, error);
274 	btreeblks = blocks - 1;
275 	agf->agf_freeblks = cpu_to_be32(raa.freeblks);
276 	agf->agf_longest = cpu_to_be32(raa.longest);
277 
278 	/* Update the AGF counters from the cntbt. */
279 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
280 			sc->sa.pag, XFS_BTNUM_CNT);
281 	error = xfs_btree_count_blocks(cur, &blocks);
282 	if (error)
283 		goto err;
284 	xfs_btree_del_cursor(cur, error);
285 	btreeblks += blocks - 1;
286 
287 	/* Update the AGF counters from the rmapbt. */
288 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
289 	error = xfs_btree_count_blocks(cur, &blocks);
290 	if (error)
291 		goto err;
292 	xfs_btree_del_cursor(cur, error);
293 	agf->agf_rmap_blocks = cpu_to_be32(blocks);
294 	btreeblks += blocks - 1;
295 
296 	agf->agf_btreeblks = cpu_to_be32(btreeblks);
297 
298 	/* Update the AGF counters from the refcountbt. */
299 	if (xfs_has_reflink(mp)) {
300 		cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
301 				sc->sa.pag);
302 		error = xfs_btree_count_blocks(cur, &blocks);
303 		if (error)
304 			goto err;
305 		xfs_btree_del_cursor(cur, error);
306 		agf->agf_refcount_blocks = cpu_to_be32(blocks);
307 	}
308 
309 	return 0;
310 err:
311 	xfs_btree_del_cursor(cur, error);
312 	return error;
313 }
314 
315 /* Commit the new AGF and reinitialize the incore state. */
316 STATIC int
317 xrep_agf_commit_new(
318 	struct xfs_scrub	*sc,
319 	struct xfs_buf		*agf_bp)
320 {
321 	struct xfs_perag	*pag;
322 	struct xfs_agf		*agf = agf_bp->b_addr;
323 
324 	/* Trigger fdblocks recalculation */
325 	xfs_force_summary_recalc(sc->mp);
326 
327 	/* Write this to disk. */
328 	xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
329 	xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
330 
331 	/* Now reinitialize the in-core counters we changed. */
332 	pag = sc->sa.pag;
333 	pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
334 	pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
335 	pag->pagf_longest = be32_to_cpu(agf->agf_longest);
336 	pag->pagf_levels[XFS_BTNUM_BNOi] =
337 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
338 	pag->pagf_levels[XFS_BTNUM_CNTi] =
339 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
340 	pag->pagf_levels[XFS_BTNUM_RMAPi] =
341 			be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
342 	pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
343 	set_bit(XFS_AGSTATE_AGF_INIT, &pag->pag_opstate);
344 
345 	return xrep_roll_ag_trans(sc);
346 }
347 
348 /* Repair the AGF. v5 filesystems only. */
349 int
350 xrep_agf(
351 	struct xfs_scrub		*sc)
352 {
353 	struct xrep_find_ag_btree	fab[XREP_AGF_MAX] = {
354 		[XREP_AGF_BNOBT] = {
355 			.rmap_owner = XFS_RMAP_OWN_AG,
356 			.buf_ops = &xfs_bnobt_buf_ops,
357 			.maxlevels = sc->mp->m_alloc_maxlevels,
358 		},
359 		[XREP_AGF_CNTBT] = {
360 			.rmap_owner = XFS_RMAP_OWN_AG,
361 			.buf_ops = &xfs_cntbt_buf_ops,
362 			.maxlevels = sc->mp->m_alloc_maxlevels,
363 		},
364 		[XREP_AGF_RMAPBT] = {
365 			.rmap_owner = XFS_RMAP_OWN_AG,
366 			.buf_ops = &xfs_rmapbt_buf_ops,
367 			.maxlevels = sc->mp->m_rmap_maxlevels,
368 		},
369 		[XREP_AGF_REFCOUNTBT] = {
370 			.rmap_owner = XFS_RMAP_OWN_REFC,
371 			.buf_ops = &xfs_refcountbt_buf_ops,
372 			.maxlevels = sc->mp->m_refc_maxlevels,
373 		},
374 		[XREP_AGF_END] = {
375 			.buf_ops = NULL,
376 		},
377 	};
378 	struct xfs_agf			old_agf;
379 	struct xfs_mount		*mp = sc->mp;
380 	struct xfs_buf			*agf_bp;
381 	struct xfs_buf			*agfl_bp;
382 	struct xfs_agf			*agf;
383 	int				error;
384 
385 	/* We require the rmapbt to rebuild anything. */
386 	if (!xfs_has_rmapbt(mp))
387 		return -EOPNOTSUPP;
388 
389 	/*
390 	 * Make sure we have the AGF buffer, as scrub might have decided it
391 	 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
392 	 */
393 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
394 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
395 						XFS_AGF_DADDR(mp)),
396 			XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
397 	if (error)
398 		return error;
399 	agf_bp->b_ops = &xfs_agf_buf_ops;
400 	agf = agf_bp->b_addr;
401 
402 	/*
403 	 * Load the AGFL so that we can screen out OWN_AG blocks that are on
404 	 * the AGFL now; these blocks might have once been part of the
405 	 * bno/cnt/rmap btrees but are not now.  This is a chicken and egg
406 	 * problem: the AGF is corrupt, so we have to trust the AGFL contents
407 	 * because we can't do any serious cross-referencing with any of the
408 	 * btrees rooted in the AGF.  If the AGFL contents are obviously bad
409 	 * then we'll bail out.
410 	 */
411 	error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &agfl_bp);
412 	if (error)
413 		return error;
414 
415 	/*
416 	 * Spot-check the AGFL blocks; if they're obviously corrupt then
417 	 * there's nothing we can do but bail out.
418 	 */
419 	error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
420 			xrep_agf_check_agfl_block, sc);
421 	if (error)
422 		return error;
423 
424 	/*
425 	 * Find the AGF btree roots.  This is also a chicken-and-egg situation;
426 	 * see the function for more details.
427 	 */
428 	error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
429 	if (error)
430 		return error;
431 
432 	/* Last chance to abort before we start committing fixes. */
433 	if (xchk_should_terminate(sc, &error))
434 		return error;
435 
436 	/* Start rewriting the header and implant the btrees we found. */
437 	xrep_agf_init_header(sc, agf_bp, &old_agf);
438 	xrep_agf_set_roots(sc, agf, fab);
439 	error = xrep_agf_calc_from_btrees(sc, agf_bp);
440 	if (error)
441 		goto out_revert;
442 
443 	/* Commit the changes and reinitialize incore state. */
444 	return xrep_agf_commit_new(sc, agf_bp);
445 
446 out_revert:
447 	/* Mark the incore AGF state stale and revert the AGF. */
448 	clear_bit(XFS_AGSTATE_AGF_INIT, &sc->sa.pag->pag_opstate);
449 	memcpy(agf, &old_agf, sizeof(old_agf));
450 	return error;
451 }
452 
453 /* AGFL */
454 
455 struct xrep_agfl {
456 	/* Bitmap of alleged AGFL blocks that we're not going to add. */
457 	struct xagb_bitmap	crossed;
458 
459 	/* Bitmap of other OWN_AG metadata blocks. */
460 	struct xagb_bitmap	agmetablocks;
461 
462 	/* Bitmap of free space. */
463 	struct xagb_bitmap	*freesp;
464 
465 	/* rmapbt cursor for finding crosslinked blocks */
466 	struct xfs_btree_cur	*rmap_cur;
467 
468 	struct xfs_scrub	*sc;
469 };
470 
471 /* Record all OWN_AG (free space btree) information from the rmap data. */
472 STATIC int
473 xrep_agfl_walk_rmap(
474 	struct xfs_btree_cur	*cur,
475 	const struct xfs_rmap_irec *rec,
476 	void			*priv)
477 {
478 	struct xrep_agfl	*ra = priv;
479 	int			error = 0;
480 
481 	if (xchk_should_terminate(ra->sc, &error))
482 		return error;
483 
484 	/* Record all the OWN_AG blocks. */
485 	if (rec->rm_owner == XFS_RMAP_OWN_AG) {
486 		error = xagb_bitmap_set(ra->freesp, rec->rm_startblock,
487 				rec->rm_blockcount);
488 		if (error)
489 			return error;
490 	}
491 
492 	return xagb_bitmap_set_btcur_path(&ra->agmetablocks, cur);
493 }
494 
495 /* Strike out the blocks that are cross-linked according to the rmapbt. */
496 STATIC int
497 xrep_agfl_check_extent(
498 	uint32_t		agbno,
499 	uint32_t		len,
500 	void			*priv)
501 {
502 	struct xrep_agfl	*ra = priv;
503 	xfs_agblock_t		last_agbno = agbno + len - 1;
504 	int			error;
505 
506 	while (agbno <= last_agbno) {
507 		bool		other_owners;
508 
509 		error = xfs_rmap_has_other_keys(ra->rmap_cur, agbno, 1,
510 				&XFS_RMAP_OINFO_AG, &other_owners);
511 		if (error)
512 			return error;
513 
514 		if (other_owners) {
515 			error = xagb_bitmap_set(&ra->crossed, agbno, 1);
516 			if (error)
517 				return error;
518 		}
519 
520 		if (xchk_should_terminate(ra->sc, &error))
521 			return error;
522 		agbno++;
523 	}
524 
525 	return 0;
526 }
527 
528 /*
529  * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
530  * which blocks belong to the AGFL.
531  *
532  * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
533  * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
534  * rmapbt).  These are the old AGFL blocks, so return that list and the number
535  * of blocks we're actually going to put back on the AGFL.
536  */
537 STATIC int
538 xrep_agfl_collect_blocks(
539 	struct xfs_scrub	*sc,
540 	struct xfs_buf		*agf_bp,
541 	struct xagb_bitmap	*agfl_extents,
542 	xfs_agblock_t		*flcount)
543 {
544 	struct xrep_agfl	ra;
545 	struct xfs_mount	*mp = sc->mp;
546 	struct xfs_btree_cur	*cur;
547 	int			error;
548 
549 	ra.sc = sc;
550 	ra.freesp = agfl_extents;
551 	xagb_bitmap_init(&ra.agmetablocks);
552 	xagb_bitmap_init(&ra.crossed);
553 
554 	/* Find all space used by the free space btrees & rmapbt. */
555 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
556 	error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
557 	xfs_btree_del_cursor(cur, error);
558 	if (error)
559 		goto out_bmp;
560 
561 	/* Find all blocks currently being used by the bnobt. */
562 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
563 			sc->sa.pag, XFS_BTNUM_BNO);
564 	error = xagb_bitmap_set_btblocks(&ra.agmetablocks, cur);
565 	xfs_btree_del_cursor(cur, error);
566 	if (error)
567 		goto out_bmp;
568 
569 	/* Find all blocks currently being used by the cntbt. */
570 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
571 			sc->sa.pag, XFS_BTNUM_CNT);
572 	error = xagb_bitmap_set_btblocks(&ra.agmetablocks, cur);
573 	xfs_btree_del_cursor(cur, error);
574 	if (error)
575 		goto out_bmp;
576 
577 	/*
578 	 * Drop the freesp meta blocks that are in use by btrees.
579 	 * The remaining blocks /should/ be AGFL blocks.
580 	 */
581 	error = xagb_bitmap_disunion(agfl_extents, &ra.agmetablocks);
582 	if (error)
583 		goto out_bmp;
584 
585 	/* Strike out the blocks that are cross-linked. */
586 	ra.rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
587 	error = xagb_bitmap_walk(agfl_extents, xrep_agfl_check_extent, &ra);
588 	xfs_btree_del_cursor(ra.rmap_cur, error);
589 	if (error)
590 		goto out_bmp;
591 	error = xagb_bitmap_disunion(agfl_extents, &ra.crossed);
592 	if (error)
593 		goto out_bmp;
594 
595 	/*
596 	 * Calculate the new AGFL size.  If we found more blocks than fit in
597 	 * the AGFL we'll free them later.
598 	 */
599 	*flcount = min_t(uint64_t, xagb_bitmap_hweight(agfl_extents),
600 			 xfs_agfl_size(mp));
601 
602 out_bmp:
603 	xagb_bitmap_destroy(&ra.crossed);
604 	xagb_bitmap_destroy(&ra.agmetablocks);
605 	return error;
606 }
607 
608 /* Update the AGF and reset the in-core state. */
609 STATIC void
610 xrep_agfl_update_agf(
611 	struct xfs_scrub	*sc,
612 	struct xfs_buf		*agf_bp,
613 	xfs_agblock_t		flcount)
614 {
615 	struct xfs_agf		*agf = agf_bp->b_addr;
616 
617 	ASSERT(flcount <= xfs_agfl_size(sc->mp));
618 
619 	/* Trigger fdblocks recalculation */
620 	xfs_force_summary_recalc(sc->mp);
621 
622 	/* Update the AGF counters. */
623 	if (xfs_perag_initialised_agf(sc->sa.pag)) {
624 		sc->sa.pag->pagf_flcount = flcount;
625 		clear_bit(XFS_AGSTATE_AGFL_NEEDS_RESET,
626 				&sc->sa.pag->pag_opstate);
627 	}
628 	agf->agf_flfirst = cpu_to_be32(0);
629 	agf->agf_flcount = cpu_to_be32(flcount);
630 	if (flcount)
631 		agf->agf_fllast = cpu_to_be32(flcount - 1);
632 	else
633 		agf->agf_fllast = cpu_to_be32(xfs_agfl_size(sc->mp) - 1);
634 
635 	xfs_alloc_log_agf(sc->tp, agf_bp,
636 			XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
637 }
638 
639 struct xrep_agfl_fill {
640 	struct xagb_bitmap	used_extents;
641 	struct xfs_scrub	*sc;
642 	__be32			*agfl_bno;
643 	xfs_agblock_t		flcount;
644 	unsigned int		fl_off;
645 };
646 
647 /* Fill the AGFL with whatever blocks are in this extent. */
648 static int
649 xrep_agfl_fill(
650 	uint32_t		start,
651 	uint32_t		len,
652 	void			*priv)
653 {
654 	struct xrep_agfl_fill	*af = priv;
655 	struct xfs_scrub	*sc = af->sc;
656 	xfs_agblock_t		agbno = start;
657 	int			error;
658 
659 	trace_xrep_agfl_insert(sc->sa.pag, agbno, len);
660 
661 	while (agbno < start + len && af->fl_off < af->flcount)
662 		af->agfl_bno[af->fl_off++] = cpu_to_be32(agbno++);
663 
664 	error = xagb_bitmap_set(&af->used_extents, start, agbno - 1);
665 	if (error)
666 		return error;
667 
668 	if (af->fl_off == af->flcount)
669 		return -ECANCELED;
670 
671 	return 0;
672 }
673 
674 /* Write out a totally new AGFL. */
675 STATIC int
676 xrep_agfl_init_header(
677 	struct xfs_scrub	*sc,
678 	struct xfs_buf		*agfl_bp,
679 	struct xagb_bitmap	*agfl_extents,
680 	xfs_agblock_t		flcount)
681 {
682 	struct xrep_agfl_fill	af = {
683 		.sc		= sc,
684 		.flcount	= flcount,
685 	};
686 	struct xfs_mount	*mp = sc->mp;
687 	struct xfs_agfl		*agfl;
688 	int			error;
689 
690 	ASSERT(flcount <= xfs_agfl_size(mp));
691 
692 	/*
693 	 * Start rewriting the header by setting the bno[] array to
694 	 * NULLAGBLOCK, then setting AGFL header fields.
695 	 */
696 	agfl = XFS_BUF_TO_AGFL(agfl_bp);
697 	memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
698 	agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
699 	agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
700 	uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
701 
702 	/*
703 	 * Fill the AGFL with the remaining blocks.  If agfl_extents has more
704 	 * blocks than fit in the AGFL, they will be freed in a subsequent
705 	 * step.
706 	 */
707 	xagb_bitmap_init(&af.used_extents);
708 	af.agfl_bno = xfs_buf_to_agfl_bno(agfl_bp),
709 	xagb_bitmap_walk(agfl_extents, xrep_agfl_fill, &af);
710 	error = xagb_bitmap_disunion(agfl_extents, &af.used_extents);
711 	if (error)
712 		return error;
713 
714 	/* Write new AGFL to disk. */
715 	xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
716 	xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
717 	xagb_bitmap_destroy(&af.used_extents);
718 	return 0;
719 }
720 
721 /* Repair the AGFL. */
722 int
723 xrep_agfl(
724 	struct xfs_scrub	*sc)
725 {
726 	struct xagb_bitmap	agfl_extents;
727 	struct xfs_mount	*mp = sc->mp;
728 	struct xfs_buf		*agf_bp;
729 	struct xfs_buf		*agfl_bp;
730 	xfs_agblock_t		flcount;
731 	int			error;
732 
733 	/* We require the rmapbt to rebuild anything. */
734 	if (!xfs_has_rmapbt(mp))
735 		return -EOPNOTSUPP;
736 
737 	xagb_bitmap_init(&agfl_extents);
738 
739 	/*
740 	 * Read the AGF so that we can query the rmapbt.  We hope that there's
741 	 * nothing wrong with the AGF, but all the AG header repair functions
742 	 * have this chicken-and-egg problem.
743 	 */
744 	error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
745 	if (error)
746 		return error;
747 
748 	/*
749 	 * Make sure we have the AGFL buffer, as scrub might have decided it
750 	 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
751 	 */
752 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
753 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
754 						XFS_AGFL_DADDR(mp)),
755 			XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
756 	if (error)
757 		return error;
758 	agfl_bp->b_ops = &xfs_agfl_buf_ops;
759 
760 	/* Gather all the extents we're going to put on the new AGFL. */
761 	error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
762 	if (error)
763 		goto err;
764 
765 	/* Last chance to abort before we start committing fixes. */
766 	if (xchk_should_terminate(sc, &error))
767 		goto err;
768 
769 	/*
770 	 * Update AGF and AGFL.  We reset the global free block counter when
771 	 * we adjust the AGF flcount (which can fail) so avoid updating any
772 	 * buffers until we know that part works.
773 	 */
774 	xrep_agfl_update_agf(sc, agf_bp, flcount);
775 	error = xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
776 	if (error)
777 		goto err;
778 
779 	/*
780 	 * Ok, the AGFL should be ready to go now.  Roll the transaction to
781 	 * make the new AGFL permanent before we start using it to return
782 	 * freespace overflow to the freespace btrees.
783 	 */
784 	sc->sa.agf_bp = agf_bp;
785 	error = xrep_roll_ag_trans(sc);
786 	if (error)
787 		goto err;
788 
789 	/* Dump any AGFL overflow. */
790 	error = xrep_reap_agblocks(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
791 			XFS_AG_RESV_AGFL);
792 	if (error)
793 		goto err;
794 
795 err:
796 	xagb_bitmap_destroy(&agfl_extents);
797 	return error;
798 }
799 
800 /* AGI */
801 
802 /*
803  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
804  * XFS_BTNUM_ names here to avoid creating a sparse array.
805  */
806 enum {
807 	XREP_AGI_INOBT = 0,
808 	XREP_AGI_FINOBT,
809 	XREP_AGI_END,
810 	XREP_AGI_MAX
811 };
812 
813 /*
814  * Given the inode btree roots described by *fab, find the roots, check them
815  * for sanity, and pass the root data back out via *fab.
816  */
817 STATIC int
818 xrep_agi_find_btrees(
819 	struct xfs_scrub		*sc,
820 	struct xrep_find_ag_btree	*fab)
821 {
822 	struct xfs_buf			*agf_bp;
823 	struct xfs_mount		*mp = sc->mp;
824 	int				error;
825 
826 	/* Read the AGF. */
827 	error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
828 	if (error)
829 		return error;
830 
831 	/* Find the btree roots. */
832 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
833 	if (error)
834 		return error;
835 
836 	/* We must find the inobt root. */
837 	if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
838 		return -EFSCORRUPTED;
839 
840 	/* We must find the finobt root if that feature is enabled. */
841 	if (xfs_has_finobt(mp) &&
842 	    !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
843 		return -EFSCORRUPTED;
844 
845 	return 0;
846 }
847 
848 /*
849  * Reinitialize the AGI header, making an in-core copy of the old contents so
850  * that we know which in-core state needs to be reinitialized.
851  */
852 STATIC void
853 xrep_agi_init_header(
854 	struct xfs_scrub	*sc,
855 	struct xfs_buf		*agi_bp,
856 	struct xfs_agi		*old_agi)
857 {
858 	struct xfs_agi		*agi = agi_bp->b_addr;
859 	struct xfs_perag	*pag = sc->sa.pag;
860 	struct xfs_mount	*mp = sc->mp;
861 
862 	memcpy(old_agi, agi, sizeof(*old_agi));
863 	memset(agi, 0, BBTOB(agi_bp->b_length));
864 	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
865 	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
866 	agi->agi_seqno = cpu_to_be32(pag->pag_agno);
867 	agi->agi_length = cpu_to_be32(pag->block_count);
868 	agi->agi_newino = cpu_to_be32(NULLAGINO);
869 	agi->agi_dirino = cpu_to_be32(NULLAGINO);
870 	if (xfs_has_crc(mp))
871 		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
872 
873 	/* We don't know how to fix the unlinked list yet. */
874 	memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
875 			sizeof(agi->agi_unlinked));
876 
877 	/* Mark the incore AGF data stale until we're done fixing things. */
878 	ASSERT(xfs_perag_initialised_agi(pag));
879 	clear_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
880 }
881 
882 /* Set btree root information in an AGI. */
883 STATIC void
884 xrep_agi_set_roots(
885 	struct xfs_scrub		*sc,
886 	struct xfs_agi			*agi,
887 	struct xrep_find_ag_btree	*fab)
888 {
889 	agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
890 	agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
891 
892 	if (xfs_has_finobt(sc->mp)) {
893 		agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
894 		agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
895 	}
896 }
897 
898 /* Update the AGI counters. */
899 STATIC int
900 xrep_agi_calc_from_btrees(
901 	struct xfs_scrub	*sc,
902 	struct xfs_buf		*agi_bp)
903 {
904 	struct xfs_btree_cur	*cur;
905 	struct xfs_agi		*agi = agi_bp->b_addr;
906 	struct xfs_mount	*mp = sc->mp;
907 	xfs_agino_t		count;
908 	xfs_agino_t		freecount;
909 	int			error;
910 
911 	cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, agi_bp, XFS_BTNUM_INO);
912 	error = xfs_ialloc_count_inodes(cur, &count, &freecount);
913 	if (error)
914 		goto err;
915 	if (xfs_has_inobtcounts(mp)) {
916 		xfs_agblock_t	blocks;
917 
918 		error = xfs_btree_count_blocks(cur, &blocks);
919 		if (error)
920 			goto err;
921 		agi->agi_iblocks = cpu_to_be32(blocks);
922 	}
923 	xfs_btree_del_cursor(cur, error);
924 
925 	agi->agi_count = cpu_to_be32(count);
926 	agi->agi_freecount = cpu_to_be32(freecount);
927 
928 	if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
929 		xfs_agblock_t	blocks;
930 
931 		cur = xfs_inobt_init_cursor(sc->sa.pag, sc->tp, agi_bp,
932 				XFS_BTNUM_FINO);
933 		error = xfs_btree_count_blocks(cur, &blocks);
934 		if (error)
935 			goto err;
936 		xfs_btree_del_cursor(cur, error);
937 		agi->agi_fblocks = cpu_to_be32(blocks);
938 	}
939 
940 	return 0;
941 err:
942 	xfs_btree_del_cursor(cur, error);
943 	return error;
944 }
945 
946 /* Trigger reinitialization of the in-core data. */
947 STATIC int
948 xrep_agi_commit_new(
949 	struct xfs_scrub	*sc,
950 	struct xfs_buf		*agi_bp)
951 {
952 	struct xfs_perag	*pag;
953 	struct xfs_agi		*agi = agi_bp->b_addr;
954 
955 	/* Trigger inode count recalculation */
956 	xfs_force_summary_recalc(sc->mp);
957 
958 	/* Write this to disk. */
959 	xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
960 	xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
961 
962 	/* Now reinitialize the in-core counters if necessary. */
963 	pag = sc->sa.pag;
964 	pag->pagi_count = be32_to_cpu(agi->agi_count);
965 	pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
966 	set_bit(XFS_AGSTATE_AGI_INIT, &pag->pag_opstate);
967 
968 	return xrep_roll_ag_trans(sc);
969 }
970 
971 /* Repair the AGI. */
972 int
973 xrep_agi(
974 	struct xfs_scrub		*sc)
975 {
976 	struct xrep_find_ag_btree	fab[XREP_AGI_MAX] = {
977 		[XREP_AGI_INOBT] = {
978 			.rmap_owner = XFS_RMAP_OWN_INOBT,
979 			.buf_ops = &xfs_inobt_buf_ops,
980 			.maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
981 		},
982 		[XREP_AGI_FINOBT] = {
983 			.rmap_owner = XFS_RMAP_OWN_INOBT,
984 			.buf_ops = &xfs_finobt_buf_ops,
985 			.maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
986 		},
987 		[XREP_AGI_END] = {
988 			.buf_ops = NULL
989 		},
990 	};
991 	struct xfs_agi			old_agi;
992 	struct xfs_mount		*mp = sc->mp;
993 	struct xfs_buf			*agi_bp;
994 	struct xfs_agi			*agi;
995 	int				error;
996 
997 	/* We require the rmapbt to rebuild anything. */
998 	if (!xfs_has_rmapbt(mp))
999 		return -EOPNOTSUPP;
1000 
1001 	/*
1002 	 * Make sure we have the AGI buffer, as scrub might have decided it
1003 	 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
1004 	 */
1005 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
1006 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
1007 						XFS_AGI_DADDR(mp)),
1008 			XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
1009 	if (error)
1010 		return error;
1011 	agi_bp->b_ops = &xfs_agi_buf_ops;
1012 	agi = agi_bp->b_addr;
1013 
1014 	/* Find the AGI btree roots. */
1015 	error = xrep_agi_find_btrees(sc, fab);
1016 	if (error)
1017 		return error;
1018 
1019 	/* Last chance to abort before we start committing fixes. */
1020 	if (xchk_should_terminate(sc, &error))
1021 		return error;
1022 
1023 	/* Start rewriting the header and implant the btrees we found. */
1024 	xrep_agi_init_header(sc, agi_bp, &old_agi);
1025 	xrep_agi_set_roots(sc, agi, fab);
1026 	error = xrep_agi_calc_from_btrees(sc, agi_bp);
1027 	if (error)
1028 		goto out_revert;
1029 
1030 	/* Reinitialize in-core state. */
1031 	return xrep_agi_commit_new(sc, agi_bp);
1032 
1033 out_revert:
1034 	/* Mark the incore AGI state stale and revert the AGI. */
1035 	clear_bit(XFS_AGSTATE_AGI_INIT, &sc->sa.pag->pag_opstate);
1036 	memcpy(agi, &old_agi, sizeof(old_agi));
1037 	return error;
1038 }
1039