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