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