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