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