xref: /openbmc/linux/fs/xfs/scrub/agheader_repair.c (revision dd093fb0)
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 alleged AGFL blocks that we're not going to add. */
446 	struct xbitmap		crossed;
447 
448 	/* Bitmap of other OWN_AG metadata blocks. */
449 	struct xbitmap		agmetablocks;
450 
451 	/* Bitmap of free space. */
452 	struct xbitmap		*freesp;
453 
454 	/* rmapbt cursor for finding crosslinked blocks */
455 	struct xfs_btree_cur	*rmap_cur;
456 
457 	struct xfs_scrub	*sc;
458 };
459 
460 /* Record all OWN_AG (free space btree) information from the rmap data. */
461 STATIC int
462 xrep_agfl_walk_rmap(
463 	struct xfs_btree_cur	*cur,
464 	const struct xfs_rmap_irec *rec,
465 	void			*priv)
466 {
467 	struct xrep_agfl	*ra = priv;
468 	xfs_fsblock_t		fsb;
469 	int			error = 0;
470 
471 	if (xchk_should_terminate(ra->sc, &error))
472 		return error;
473 
474 	/* Record all the OWN_AG blocks. */
475 	if (rec->rm_owner == XFS_RMAP_OWN_AG) {
476 		fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.pag->pag_agno,
477 				rec->rm_startblock);
478 		error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
479 		if (error)
480 			return error;
481 	}
482 
483 	return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
484 }
485 
486 /* Strike out the blocks that are cross-linked according to the rmapbt. */
487 STATIC int
488 xrep_agfl_check_extent(
489 	struct xrep_agfl	*ra,
490 	uint64_t		start,
491 	uint64_t		len)
492 {
493 	xfs_agblock_t		agbno = XFS_FSB_TO_AGBNO(ra->sc->mp, start);
494 	xfs_agblock_t		last_agbno = agbno + len - 1;
495 	int			error;
496 
497 	ASSERT(XFS_FSB_TO_AGNO(ra->sc->mp, start) == ra->sc->sa.pag->pag_agno);
498 
499 	while (agbno <= last_agbno) {
500 		bool		other_owners;
501 
502 		error = xfs_rmap_has_other_keys(ra->rmap_cur, agbno, 1,
503 				&XFS_RMAP_OINFO_AG, &other_owners);
504 		if (error)
505 			return error;
506 
507 		if (other_owners) {
508 			error = xbitmap_set(&ra->crossed, agbno, 1);
509 			if (error)
510 				return error;
511 		}
512 
513 		if (xchk_should_terminate(ra->sc, &error))
514 			return error;
515 		agbno++;
516 	}
517 
518 	return 0;
519 }
520 
521 /*
522  * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
523  * which blocks belong to the AGFL.
524  *
525  * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
526  * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
527  * rmapbt).  These are the old AGFL blocks, so return that list and the number
528  * of blocks we're actually going to put back on the AGFL.
529  */
530 STATIC int
531 xrep_agfl_collect_blocks(
532 	struct xfs_scrub	*sc,
533 	struct xfs_buf		*agf_bp,
534 	struct xbitmap		*agfl_extents,
535 	xfs_agblock_t		*flcount)
536 {
537 	struct xrep_agfl	ra;
538 	struct xfs_mount	*mp = sc->mp;
539 	struct xfs_btree_cur	*cur;
540 	struct xbitmap_range	*br, *n;
541 	int			error;
542 
543 	ra.sc = sc;
544 	ra.freesp = agfl_extents;
545 	xbitmap_init(&ra.agmetablocks);
546 	xbitmap_init(&ra.crossed);
547 
548 	/* Find all space used by the free space btrees & rmapbt. */
549 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
550 	error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
551 	xfs_btree_del_cursor(cur, error);
552 	if (error)
553 		goto out_bmp;
554 
555 	/* Find all blocks currently being used by the bnobt. */
556 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
557 			sc->sa.pag, XFS_BTNUM_BNO);
558 	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
559 	xfs_btree_del_cursor(cur, error);
560 	if (error)
561 		goto out_bmp;
562 
563 	/* Find all blocks currently being used by the cntbt. */
564 	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
565 			sc->sa.pag, XFS_BTNUM_CNT);
566 	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
567 	xfs_btree_del_cursor(cur, error);
568 	if (error)
569 		goto out_bmp;
570 
571 	/*
572 	 * Drop the freesp meta blocks that are in use by btrees.
573 	 * The remaining blocks /should/ be AGFL blocks.
574 	 */
575 	error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
576 	if (error)
577 		goto out_bmp;
578 
579 	/* Strike out the blocks that are cross-linked. */
580 	ra.rmap_cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
581 	for_each_xbitmap_extent(br, n, agfl_extents) {
582 		error = xrep_agfl_check_extent(&ra, br->start, br->len);
583 		if (error)
584 			break;
585 	}
586 	xfs_btree_del_cursor(ra.rmap_cur, error);
587 	if (error)
588 		goto out_bmp;
589 	error = xbitmap_disunion(agfl_extents, &ra.crossed);
590 	if (error)
591 		goto out_bmp;
592 
593 	/*
594 	 * Calculate the new AGFL size.  If we found more blocks than fit in
595 	 * the AGFL we'll free them later.
596 	 */
597 	*flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
598 			 xfs_agfl_size(mp));
599 
600 out_bmp:
601 	xbitmap_destroy(&ra.crossed);
602 	xbitmap_destroy(&ra.agmetablocks);
603 	return error;
604 }
605 
606 /* Update the AGF and reset the in-core state. */
607 STATIC void
608 xrep_agfl_update_agf(
609 	struct xfs_scrub	*sc,
610 	struct xfs_buf		*agf_bp,
611 	xfs_agblock_t		flcount)
612 {
613 	struct xfs_agf		*agf = agf_bp->b_addr;
614 
615 	ASSERT(flcount <= xfs_agfl_size(sc->mp));
616 
617 	/* Trigger fdblocks recalculation */
618 	xfs_force_summary_recalc(sc->mp);
619 
620 	/* Update the AGF counters. */
621 	if (sc->sa.pag->pagf_init)
622 		sc->sa.pag->pagf_flcount = flcount;
623 	agf->agf_flfirst = cpu_to_be32(0);
624 	agf->agf_flcount = cpu_to_be32(flcount);
625 	agf->agf_fllast = cpu_to_be32(flcount - 1);
626 
627 	xfs_alloc_log_agf(sc->tp, agf_bp,
628 			XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
629 }
630 
631 /* Write out a totally new AGFL. */
632 STATIC void
633 xrep_agfl_init_header(
634 	struct xfs_scrub	*sc,
635 	struct xfs_buf		*agfl_bp,
636 	struct xbitmap		*agfl_extents,
637 	xfs_agblock_t		flcount)
638 {
639 	struct xfs_mount	*mp = sc->mp;
640 	__be32			*agfl_bno;
641 	struct xbitmap_range	*br;
642 	struct xbitmap_range	*n;
643 	struct xfs_agfl		*agfl;
644 	xfs_agblock_t		agbno;
645 	unsigned int		fl_off;
646 
647 	ASSERT(flcount <= xfs_agfl_size(mp));
648 
649 	/*
650 	 * Start rewriting the header by setting the bno[] array to
651 	 * NULLAGBLOCK, then setting AGFL header fields.
652 	 */
653 	agfl = XFS_BUF_TO_AGFL(agfl_bp);
654 	memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
655 	agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
656 	agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
657 	uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
658 
659 	/*
660 	 * Fill the AGFL with the remaining blocks.  If agfl_extents has more
661 	 * blocks than fit in the AGFL, they will be freed in a subsequent
662 	 * step.
663 	 */
664 	fl_off = 0;
665 	agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
666 	for_each_xbitmap_extent(br, n, agfl_extents) {
667 		agbno = XFS_FSB_TO_AGBNO(mp, br->start);
668 
669 		trace_xrep_agfl_insert(mp, sc->sa.pag->pag_agno, agbno,
670 				br->len);
671 
672 		while (br->len > 0 && fl_off < flcount) {
673 			agfl_bno[fl_off] = cpu_to_be32(agbno);
674 			fl_off++;
675 			agbno++;
676 
677 			/*
678 			 * We've now used br->start by putting it in the AGFL,
679 			 * so bump br so that we don't reap the block later.
680 			 */
681 			br->start++;
682 			br->len--;
683 		}
684 
685 		if (br->len)
686 			break;
687 		list_del(&br->list);
688 		kfree(br);
689 	}
690 
691 	/* Write new AGFL to disk. */
692 	xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
693 	xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
694 }
695 
696 /* Repair the AGFL. */
697 int
698 xrep_agfl(
699 	struct xfs_scrub	*sc)
700 {
701 	struct xbitmap		agfl_extents;
702 	struct xfs_mount	*mp = sc->mp;
703 	struct xfs_buf		*agf_bp;
704 	struct xfs_buf		*agfl_bp;
705 	xfs_agblock_t		flcount;
706 	int			error;
707 
708 	/* We require the rmapbt to rebuild anything. */
709 	if (!xfs_has_rmapbt(mp))
710 		return -EOPNOTSUPP;
711 
712 	xbitmap_init(&agfl_extents);
713 
714 	/*
715 	 * Read the AGF so that we can query the rmapbt.  We hope that there's
716 	 * nothing wrong with the AGF, but all the AG header repair functions
717 	 * have this chicken-and-egg problem.
718 	 */
719 	error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
720 	if (error)
721 		return error;
722 
723 	/*
724 	 * Make sure we have the AGFL buffer, as scrub might have decided it
725 	 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
726 	 */
727 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
728 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
729 						XFS_AGFL_DADDR(mp)),
730 			XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
731 	if (error)
732 		return error;
733 	agfl_bp->b_ops = &xfs_agfl_buf_ops;
734 
735 	/* Gather all the extents we're going to put on the new AGFL. */
736 	error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
737 	if (error)
738 		goto err;
739 
740 	/*
741 	 * Update AGF and AGFL.  We reset the global free block counter when
742 	 * we adjust the AGF flcount (which can fail) so avoid updating any
743 	 * buffers until we know that part works.
744 	 */
745 	xrep_agfl_update_agf(sc, agf_bp, flcount);
746 	xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
747 
748 	/*
749 	 * Ok, the AGFL should be ready to go now.  Roll the transaction to
750 	 * make the new AGFL permanent before we start using it to return
751 	 * freespace overflow to the freespace btrees.
752 	 */
753 	sc->sa.agf_bp = agf_bp;
754 	error = xrep_roll_ag_trans(sc);
755 	if (error)
756 		goto err;
757 
758 	/* Dump any AGFL overflow. */
759 	error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
760 			XFS_AG_RESV_AGFL);
761 err:
762 	xbitmap_destroy(&agfl_extents);
763 	return error;
764 }
765 
766 /* AGI */
767 
768 /*
769  * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
770  * XFS_BTNUM_ names here to avoid creating a sparse array.
771  */
772 enum {
773 	XREP_AGI_INOBT = 0,
774 	XREP_AGI_FINOBT,
775 	XREP_AGI_END,
776 	XREP_AGI_MAX
777 };
778 
779 /*
780  * Given the inode btree roots described by *fab, find the roots, check them
781  * for sanity, and pass the root data back out via *fab.
782  */
783 STATIC int
784 xrep_agi_find_btrees(
785 	struct xfs_scrub		*sc,
786 	struct xrep_find_ag_btree	*fab)
787 {
788 	struct xfs_buf			*agf_bp;
789 	struct xfs_mount		*mp = sc->mp;
790 	int				error;
791 
792 	/* Read the AGF. */
793 	error = xfs_alloc_read_agf(sc->sa.pag, sc->tp, 0, &agf_bp);
794 	if (error)
795 		return error;
796 
797 	/* Find the btree roots. */
798 	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
799 	if (error)
800 		return error;
801 
802 	/* We must find the inobt root. */
803 	if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
804 		return -EFSCORRUPTED;
805 
806 	/* We must find the finobt root if that feature is enabled. */
807 	if (xfs_has_finobt(mp) &&
808 	    !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
809 		return -EFSCORRUPTED;
810 
811 	return 0;
812 }
813 
814 /*
815  * Reinitialize the AGI header, making an in-core copy of the old contents so
816  * that we know which in-core state needs to be reinitialized.
817  */
818 STATIC void
819 xrep_agi_init_header(
820 	struct xfs_scrub	*sc,
821 	struct xfs_buf		*agi_bp,
822 	struct xfs_agi		*old_agi)
823 {
824 	struct xfs_agi		*agi = agi_bp->b_addr;
825 	struct xfs_mount	*mp = sc->mp;
826 
827 	memcpy(old_agi, agi, sizeof(*old_agi));
828 	memset(agi, 0, BBTOB(agi_bp->b_length));
829 	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
830 	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
831 	agi->agi_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
832 	agi->agi_length = cpu_to_be32(sc->sa.pag->block_count);
833 	agi->agi_newino = cpu_to_be32(NULLAGINO);
834 	agi->agi_dirino = cpu_to_be32(NULLAGINO);
835 	if (xfs_has_crc(mp))
836 		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
837 
838 	/* We don't know how to fix the unlinked list yet. */
839 	memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
840 			sizeof(agi->agi_unlinked));
841 
842 	/* Mark the incore AGF data stale until we're done fixing things. */
843 	ASSERT(sc->sa.pag->pagi_init);
844 	sc->sa.pag->pagi_init = 0;
845 }
846 
847 /* Set btree root information in an AGI. */
848 STATIC void
849 xrep_agi_set_roots(
850 	struct xfs_scrub		*sc,
851 	struct xfs_agi			*agi,
852 	struct xrep_find_ag_btree	*fab)
853 {
854 	agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
855 	agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
856 
857 	if (xfs_has_finobt(sc->mp)) {
858 		agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
859 		agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
860 	}
861 }
862 
863 /* Update the AGI counters. */
864 STATIC int
865 xrep_agi_calc_from_btrees(
866 	struct xfs_scrub	*sc,
867 	struct xfs_buf		*agi_bp)
868 {
869 	struct xfs_btree_cur	*cur;
870 	struct xfs_agi		*agi = agi_bp->b_addr;
871 	struct xfs_mount	*mp = sc->mp;
872 	xfs_agino_t		count;
873 	xfs_agino_t		freecount;
874 	int			error;
875 
876 	cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
877 			sc->sa.pag, XFS_BTNUM_INO);
878 	error = xfs_ialloc_count_inodes(cur, &count, &freecount);
879 	if (error)
880 		goto err;
881 	if (xfs_has_inobtcounts(mp)) {
882 		xfs_agblock_t	blocks;
883 
884 		error = xfs_btree_count_blocks(cur, &blocks);
885 		if (error)
886 			goto err;
887 		agi->agi_iblocks = cpu_to_be32(blocks);
888 	}
889 	xfs_btree_del_cursor(cur, error);
890 
891 	agi->agi_count = cpu_to_be32(count);
892 	agi->agi_freecount = cpu_to_be32(freecount);
893 
894 	if (xfs_has_finobt(mp) && xfs_has_inobtcounts(mp)) {
895 		xfs_agblock_t	blocks;
896 
897 		cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
898 				sc->sa.pag, XFS_BTNUM_FINO);
899 		error = xfs_btree_count_blocks(cur, &blocks);
900 		if (error)
901 			goto err;
902 		xfs_btree_del_cursor(cur, error);
903 		agi->agi_fblocks = cpu_to_be32(blocks);
904 	}
905 
906 	return 0;
907 err:
908 	xfs_btree_del_cursor(cur, error);
909 	return error;
910 }
911 
912 /* Trigger reinitialization of the in-core data. */
913 STATIC int
914 xrep_agi_commit_new(
915 	struct xfs_scrub	*sc,
916 	struct xfs_buf		*agi_bp)
917 {
918 	struct xfs_perag	*pag;
919 	struct xfs_agi		*agi = agi_bp->b_addr;
920 
921 	/* Trigger inode count recalculation */
922 	xfs_force_summary_recalc(sc->mp);
923 
924 	/* Write this to disk. */
925 	xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
926 	xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
927 
928 	/* Now reinitialize the in-core counters if necessary. */
929 	pag = sc->sa.pag;
930 	pag->pagi_count = be32_to_cpu(agi->agi_count);
931 	pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
932 	pag->pagi_init = 1;
933 
934 	return 0;
935 }
936 
937 /* Repair the AGI. */
938 int
939 xrep_agi(
940 	struct xfs_scrub		*sc)
941 {
942 	struct xrep_find_ag_btree	fab[XREP_AGI_MAX] = {
943 		[XREP_AGI_INOBT] = {
944 			.rmap_owner = XFS_RMAP_OWN_INOBT,
945 			.buf_ops = &xfs_inobt_buf_ops,
946 			.maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
947 		},
948 		[XREP_AGI_FINOBT] = {
949 			.rmap_owner = XFS_RMAP_OWN_INOBT,
950 			.buf_ops = &xfs_finobt_buf_ops,
951 			.maxlevels = M_IGEO(sc->mp)->inobt_maxlevels,
952 		},
953 		[XREP_AGI_END] = {
954 			.buf_ops = NULL
955 		},
956 	};
957 	struct xfs_agi			old_agi;
958 	struct xfs_mount		*mp = sc->mp;
959 	struct xfs_buf			*agi_bp;
960 	struct xfs_agi			*agi;
961 	int				error;
962 
963 	/* We require the rmapbt to rebuild anything. */
964 	if (!xfs_has_rmapbt(mp))
965 		return -EOPNOTSUPP;
966 
967 	/*
968 	 * Make sure we have the AGI buffer, as scrub might have decided it
969 	 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
970 	 */
971 	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
972 			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
973 						XFS_AGI_DADDR(mp)),
974 			XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
975 	if (error)
976 		return error;
977 	agi_bp->b_ops = &xfs_agi_buf_ops;
978 	agi = agi_bp->b_addr;
979 
980 	/* Find the AGI btree roots. */
981 	error = xrep_agi_find_btrees(sc, fab);
982 	if (error)
983 		return error;
984 
985 	/* Start rewriting the header and implant the btrees we found. */
986 	xrep_agi_init_header(sc, agi_bp, &old_agi);
987 	xrep_agi_set_roots(sc, agi, fab);
988 	error = xrep_agi_calc_from_btrees(sc, agi_bp);
989 	if (error)
990 		goto out_revert;
991 
992 	/* Reinitialize in-core state. */
993 	return xrep_agi_commit_new(sc, agi_bp);
994 
995 out_revert:
996 	/* Mark the incore AGI state stale and revert the AGI. */
997 	sc->sa.pag->pagi_init = 0;
998 	memcpy(agi, &old_agi, sizeof(old_agi));
999 	return error;
1000 }
1001