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