agheader_repair.c revision 159eb69d
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. */
33int
34xrep_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
63struct 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. */
70STATIC int
71xrep_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? */
89STATIC int
90xrep_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 */
106enum {
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. */
116static inline bool
117xrep_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 <= XFS_BTREE_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 */
137STATIC int
138xrep_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_sb_version_hasreflink(&sc->mp->m_sb) &&
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 */
178STATIC void
179xrep_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_sb_version_hascrc(&mp->m_sb))
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. */
206STATIC void
207xrep_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_sb_version_hasreflink(&sc->mp->m_sb)) {
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. */
236STATIC int
237xrep_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_sb_version_hasreflink(&mp->m_sb)) {
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;
295err:
296	xfs_btree_del_cursor(cur, error);
297	return error;
298}
299
300/* Commit the new AGF and reinitialize the incore state. */
301STATIC int
302xrep_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. */
334int
335xrep_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		},
343		[XREP_AGF_CNTBT] = {
344			.rmap_owner = XFS_RMAP_OWN_AG,
345			.buf_ops = &xfs_cntbt_buf_ops,
346		},
347		[XREP_AGF_RMAPBT] = {
348			.rmap_owner = XFS_RMAP_OWN_AG,
349			.buf_ops = &xfs_rmapbt_buf_ops,
350		},
351		[XREP_AGF_REFCOUNTBT] = {
352			.rmap_owner = XFS_RMAP_OWN_REFC,
353			.buf_ops = &xfs_refcountbt_buf_ops,
354		},
355		[XREP_AGF_END] = {
356			.buf_ops = NULL,
357		},
358	};
359	struct xfs_agf			old_agf;
360	struct xfs_mount		*mp = sc->mp;
361	struct xfs_buf			*agf_bp;
362	struct xfs_buf			*agfl_bp;
363	struct xfs_agf			*agf;
364	int				error;
365
366	/* We require the rmapbt to rebuild anything. */
367	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
368		return -EOPNOTSUPP;
369
370	/*
371	 * Make sure we have the AGF buffer, as scrub might have decided it
372	 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
373	 */
374	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
375			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
376						XFS_AGF_DADDR(mp)),
377			XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
378	if (error)
379		return error;
380	agf_bp->b_ops = &xfs_agf_buf_ops;
381	agf = agf_bp->b_addr;
382
383	/*
384	 * Load the AGFL so that we can screen out OWN_AG blocks that are on
385	 * the AGFL now; these blocks might have once been part of the
386	 * bno/cnt/rmap btrees but are not now.  This is a chicken and egg
387	 * problem: the AGF is corrupt, so we have to trust the AGFL contents
388	 * because we can't do any serious cross-referencing with any of the
389	 * btrees rooted in the AGF.  If the AGFL contents are obviously bad
390	 * then we'll bail out.
391	 */
392	error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.pag->pag_agno, &agfl_bp);
393	if (error)
394		return error;
395
396	/*
397	 * Spot-check the AGFL blocks; if they're obviously corrupt then
398	 * there's nothing we can do but bail out.
399	 */
400	error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
401			xrep_agf_check_agfl_block, sc);
402	if (error)
403		return error;
404
405	/*
406	 * Find the AGF btree roots.  This is also a chicken-and-egg situation;
407	 * see the function for more details.
408	 */
409	error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
410	if (error)
411		return error;
412
413	/* Start rewriting the header and implant the btrees we found. */
414	xrep_agf_init_header(sc, agf_bp, &old_agf);
415	xrep_agf_set_roots(sc, agf, fab);
416	error = xrep_agf_calc_from_btrees(sc, agf_bp);
417	if (error)
418		goto out_revert;
419
420	/* Commit the changes and reinitialize incore state. */
421	return xrep_agf_commit_new(sc, agf_bp);
422
423out_revert:
424	/* Mark the incore AGF state stale and revert the AGF. */
425	sc->sa.pag->pagf_init = 0;
426	memcpy(agf, &old_agf, sizeof(old_agf));
427	return error;
428}
429
430/* AGFL */
431
432struct xrep_agfl {
433	/* Bitmap of other OWN_AG metadata blocks. */
434	struct xbitmap		agmetablocks;
435
436	/* Bitmap of free space. */
437	struct xbitmap		*freesp;
438
439	struct xfs_scrub	*sc;
440};
441
442/* Record all OWN_AG (free space btree) information from the rmap data. */
443STATIC int
444xrep_agfl_walk_rmap(
445	struct xfs_btree_cur	*cur,
446	const struct xfs_rmap_irec *rec,
447	void			*priv)
448{
449	struct xrep_agfl	*ra = priv;
450	xfs_fsblock_t		fsb;
451	int			error = 0;
452
453	if (xchk_should_terminate(ra->sc, &error))
454		return error;
455
456	/* Record all the OWN_AG blocks. */
457	if (rec->rm_owner == XFS_RMAP_OWN_AG) {
458		fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.pag->pag_agno,
459				rec->rm_startblock);
460		error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
461		if (error)
462			return error;
463	}
464
465	return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
466}
467
468/*
469 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
470 * which blocks belong to the AGFL.
471 *
472 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
473 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
474 * rmapbt).  These are the old AGFL blocks, so return that list and the number
475 * of blocks we're actually going to put back on the AGFL.
476 */
477STATIC int
478xrep_agfl_collect_blocks(
479	struct xfs_scrub	*sc,
480	struct xfs_buf		*agf_bp,
481	struct xbitmap		*agfl_extents,
482	xfs_agblock_t		*flcount)
483{
484	struct xrep_agfl	ra;
485	struct xfs_mount	*mp = sc->mp;
486	struct xfs_btree_cur	*cur;
487	int			error;
488
489	ra.sc = sc;
490	ra.freesp = agfl_extents;
491	xbitmap_init(&ra.agmetablocks);
492
493	/* Find all space used by the free space btrees & rmapbt. */
494	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
495	error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
496	if (error)
497		goto err;
498	xfs_btree_del_cursor(cur, error);
499
500	/* Find all blocks currently being used by the bnobt. */
501	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
502			sc->sa.pag, XFS_BTNUM_BNO);
503	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
504	if (error)
505		goto err;
506	xfs_btree_del_cursor(cur, error);
507
508	/* Find all blocks currently being used by the cntbt. */
509	cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp,
510			sc->sa.pag, XFS_BTNUM_CNT);
511	error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
512	if (error)
513		goto err;
514
515	xfs_btree_del_cursor(cur, error);
516
517	/*
518	 * Drop the freesp meta blocks that are in use by btrees.
519	 * The remaining blocks /should/ be AGFL blocks.
520	 */
521	error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
522	xbitmap_destroy(&ra.agmetablocks);
523	if (error)
524		return error;
525
526	/*
527	 * Calculate the new AGFL size.  If we found more blocks than fit in
528	 * the AGFL we'll free them later.
529	 */
530	*flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
531			 xfs_agfl_size(mp));
532	return 0;
533
534err:
535	xbitmap_destroy(&ra.agmetablocks);
536	xfs_btree_del_cursor(cur, error);
537	return error;
538}
539
540/* Update the AGF and reset the in-core state. */
541STATIC void
542xrep_agfl_update_agf(
543	struct xfs_scrub	*sc,
544	struct xfs_buf		*agf_bp,
545	xfs_agblock_t		flcount)
546{
547	struct xfs_agf		*agf = agf_bp->b_addr;
548
549	ASSERT(flcount <= xfs_agfl_size(sc->mp));
550
551	/* Trigger fdblocks recalculation */
552	xfs_force_summary_recalc(sc->mp);
553
554	/* Update the AGF counters. */
555	if (sc->sa.pag->pagf_init)
556		sc->sa.pag->pagf_flcount = flcount;
557	agf->agf_flfirst = cpu_to_be32(0);
558	agf->agf_flcount = cpu_to_be32(flcount);
559	agf->agf_fllast = cpu_to_be32(flcount - 1);
560
561	xfs_alloc_log_agf(sc->tp, agf_bp,
562			XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
563}
564
565/* Write out a totally new AGFL. */
566STATIC void
567xrep_agfl_init_header(
568	struct xfs_scrub	*sc,
569	struct xfs_buf		*agfl_bp,
570	struct xbitmap		*agfl_extents,
571	xfs_agblock_t		flcount)
572{
573	struct xfs_mount	*mp = sc->mp;
574	__be32			*agfl_bno;
575	struct xbitmap_range	*br;
576	struct xbitmap_range	*n;
577	struct xfs_agfl		*agfl;
578	xfs_agblock_t		agbno;
579	unsigned int		fl_off;
580
581	ASSERT(flcount <= xfs_agfl_size(mp));
582
583	/*
584	 * Start rewriting the header by setting the bno[] array to
585	 * NULLAGBLOCK, then setting AGFL header fields.
586	 */
587	agfl = XFS_BUF_TO_AGFL(agfl_bp);
588	memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
589	agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
590	agfl->agfl_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
591	uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
592
593	/*
594	 * Fill the AGFL with the remaining blocks.  If agfl_extents has more
595	 * blocks than fit in the AGFL, they will be freed in a subsequent
596	 * step.
597	 */
598	fl_off = 0;
599	agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
600	for_each_xbitmap_extent(br, n, agfl_extents) {
601		agbno = XFS_FSB_TO_AGBNO(mp, br->start);
602
603		trace_xrep_agfl_insert(mp, sc->sa.pag->pag_agno, agbno,
604				br->len);
605
606		while (br->len > 0 && fl_off < flcount) {
607			agfl_bno[fl_off] = cpu_to_be32(agbno);
608			fl_off++;
609			agbno++;
610
611			/*
612			 * We've now used br->start by putting it in the AGFL,
613			 * so bump br so that we don't reap the block later.
614			 */
615			br->start++;
616			br->len--;
617		}
618
619		if (br->len)
620			break;
621		list_del(&br->list);
622		kmem_free(br);
623	}
624
625	/* Write new AGFL to disk. */
626	xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
627	xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
628}
629
630/* Repair the AGFL. */
631int
632xrep_agfl(
633	struct xfs_scrub	*sc)
634{
635	struct xbitmap		agfl_extents;
636	struct xfs_mount	*mp = sc->mp;
637	struct xfs_buf		*agf_bp;
638	struct xfs_buf		*agfl_bp;
639	xfs_agblock_t		flcount;
640	int			error;
641
642	/* We require the rmapbt to rebuild anything. */
643	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
644		return -EOPNOTSUPP;
645
646	xbitmap_init(&agfl_extents);
647
648	/*
649	 * Read the AGF so that we can query the rmapbt.  We hope that there's
650	 * nothing wrong with the AGF, but all the AG header repair functions
651	 * have this chicken-and-egg problem.
652	 */
653	error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.pag->pag_agno, 0,
654			&agf_bp);
655	if (error)
656		return error;
657
658	/*
659	 * Make sure we have the AGFL buffer, as scrub might have decided it
660	 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
661	 */
662	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
663			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
664						XFS_AGFL_DADDR(mp)),
665			XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
666	if (error)
667		return error;
668	agfl_bp->b_ops = &xfs_agfl_buf_ops;
669
670	/* Gather all the extents we're going to put on the new AGFL. */
671	error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
672	if (error)
673		goto err;
674
675	/*
676	 * Update AGF and AGFL.  We reset the global free block counter when
677	 * we adjust the AGF flcount (which can fail) so avoid updating any
678	 * buffers until we know that part works.
679	 */
680	xrep_agfl_update_agf(sc, agf_bp, flcount);
681	xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
682
683	/*
684	 * Ok, the AGFL should be ready to go now.  Roll the transaction to
685	 * make the new AGFL permanent before we start using it to return
686	 * freespace overflow to the freespace btrees.
687	 */
688	sc->sa.agf_bp = agf_bp;
689	sc->sa.agfl_bp = agfl_bp;
690	error = xrep_roll_ag_trans(sc);
691	if (error)
692		goto err;
693
694	/* Dump any AGFL overflow. */
695	error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
696			XFS_AG_RESV_AGFL);
697err:
698	xbitmap_destroy(&agfl_extents);
699	return error;
700}
701
702/* AGI */
703
704/*
705 * Offset within the xrep_find_ag_btree array for each btree type.  Avoid the
706 * XFS_BTNUM_ names here to avoid creating a sparse array.
707 */
708enum {
709	XREP_AGI_INOBT = 0,
710	XREP_AGI_FINOBT,
711	XREP_AGI_END,
712	XREP_AGI_MAX
713};
714
715/*
716 * Given the inode btree roots described by *fab, find the roots, check them
717 * for sanity, and pass the root data back out via *fab.
718 */
719STATIC int
720xrep_agi_find_btrees(
721	struct xfs_scrub		*sc,
722	struct xrep_find_ag_btree	*fab)
723{
724	struct xfs_buf			*agf_bp;
725	struct xfs_mount		*mp = sc->mp;
726	int				error;
727
728	/* Read the AGF. */
729	error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.pag->pag_agno, 0,
730			&agf_bp);
731	if (error)
732		return error;
733
734	/* Find the btree roots. */
735	error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
736	if (error)
737		return error;
738
739	/* We must find the inobt root. */
740	if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
741		return -EFSCORRUPTED;
742
743	/* We must find the finobt root if that feature is enabled. */
744	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
745	    !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
746		return -EFSCORRUPTED;
747
748	return 0;
749}
750
751/*
752 * Reinitialize the AGI header, making an in-core copy of the old contents so
753 * that we know which in-core state needs to be reinitialized.
754 */
755STATIC void
756xrep_agi_init_header(
757	struct xfs_scrub	*sc,
758	struct xfs_buf		*agi_bp,
759	struct xfs_agi		*old_agi)
760{
761	struct xfs_agi		*agi = agi_bp->b_addr;
762	struct xfs_mount	*mp = sc->mp;
763
764	memcpy(old_agi, agi, sizeof(*old_agi));
765	memset(agi, 0, BBTOB(agi_bp->b_length));
766	agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
767	agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
768	agi->agi_seqno = cpu_to_be32(sc->sa.pag->pag_agno);
769	agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp,
770							sc->sa.pag->pag_agno));
771	agi->agi_newino = cpu_to_be32(NULLAGINO);
772	agi->agi_dirino = cpu_to_be32(NULLAGINO);
773	if (xfs_sb_version_hascrc(&mp->m_sb))
774		uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
775
776	/* We don't know how to fix the unlinked list yet. */
777	memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
778			sizeof(agi->agi_unlinked));
779
780	/* Mark the incore AGF data stale until we're done fixing things. */
781	ASSERT(sc->sa.pag->pagi_init);
782	sc->sa.pag->pagi_init = 0;
783}
784
785/* Set btree root information in an AGI. */
786STATIC void
787xrep_agi_set_roots(
788	struct xfs_scrub		*sc,
789	struct xfs_agi			*agi,
790	struct xrep_find_ag_btree	*fab)
791{
792	agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
793	agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
794
795	if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) {
796		agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
797		agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
798	}
799}
800
801/* Update the AGI counters. */
802STATIC int
803xrep_agi_calc_from_btrees(
804	struct xfs_scrub	*sc,
805	struct xfs_buf		*agi_bp)
806{
807	struct xfs_btree_cur	*cur;
808	struct xfs_agi		*agi = agi_bp->b_addr;
809	struct xfs_mount	*mp = sc->mp;
810	xfs_agino_t		count;
811	xfs_agino_t		freecount;
812	int			error;
813
814	cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
815			sc->sa.pag, XFS_BTNUM_INO);
816	error = xfs_ialloc_count_inodes(cur, &count, &freecount);
817	if (error)
818		goto err;
819	if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
820		xfs_agblock_t	blocks;
821
822		error = xfs_btree_count_blocks(cur, &blocks);
823		if (error)
824			goto err;
825		agi->agi_iblocks = cpu_to_be32(blocks);
826	}
827	xfs_btree_del_cursor(cur, error);
828
829	agi->agi_count = cpu_to_be32(count);
830	agi->agi_freecount = cpu_to_be32(freecount);
831
832	if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
833	    xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
834		xfs_agblock_t	blocks;
835
836		cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp,
837				sc->sa.pag, XFS_BTNUM_FINO);
838		error = xfs_btree_count_blocks(cur, &blocks);
839		if (error)
840			goto err;
841		xfs_btree_del_cursor(cur, error);
842		agi->agi_fblocks = cpu_to_be32(blocks);
843	}
844
845	return 0;
846err:
847	xfs_btree_del_cursor(cur, error);
848	return error;
849}
850
851/* Trigger reinitialization of the in-core data. */
852STATIC int
853xrep_agi_commit_new(
854	struct xfs_scrub	*sc,
855	struct xfs_buf		*agi_bp)
856{
857	struct xfs_perag	*pag;
858	struct xfs_agi		*agi = agi_bp->b_addr;
859
860	/* Trigger inode count recalculation */
861	xfs_force_summary_recalc(sc->mp);
862
863	/* Write this to disk. */
864	xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
865	xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
866
867	/* Now reinitialize the in-core counters if necessary. */
868	pag = sc->sa.pag;
869	pag->pagi_count = be32_to_cpu(agi->agi_count);
870	pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
871	pag->pagi_init = 1;
872
873	return 0;
874}
875
876/* Repair the AGI. */
877int
878xrep_agi(
879	struct xfs_scrub		*sc)
880{
881	struct xrep_find_ag_btree	fab[XREP_AGI_MAX] = {
882		[XREP_AGI_INOBT] = {
883			.rmap_owner = XFS_RMAP_OWN_INOBT,
884			.buf_ops = &xfs_inobt_buf_ops,
885		},
886		[XREP_AGI_FINOBT] = {
887			.rmap_owner = XFS_RMAP_OWN_INOBT,
888			.buf_ops = &xfs_finobt_buf_ops,
889		},
890		[XREP_AGI_END] = {
891			.buf_ops = NULL
892		},
893	};
894	struct xfs_agi			old_agi;
895	struct xfs_mount		*mp = sc->mp;
896	struct xfs_buf			*agi_bp;
897	struct xfs_agi			*agi;
898	int				error;
899
900	/* We require the rmapbt to rebuild anything. */
901	if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
902		return -EOPNOTSUPP;
903
904	/*
905	 * Make sure we have the AGI buffer, as scrub might have decided it
906	 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
907	 */
908	error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
909			XFS_AG_DADDR(mp, sc->sa.pag->pag_agno,
910						XFS_AGI_DADDR(mp)),
911			XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
912	if (error)
913		return error;
914	agi_bp->b_ops = &xfs_agi_buf_ops;
915	agi = agi_bp->b_addr;
916
917	/* Find the AGI btree roots. */
918	error = xrep_agi_find_btrees(sc, fab);
919	if (error)
920		return error;
921
922	/* Start rewriting the header and implant the btrees we found. */
923	xrep_agi_init_header(sc, agi_bp, &old_agi);
924	xrep_agi_set_roots(sc, agi, fab);
925	error = xrep_agi_calc_from_btrees(sc, agi_bp);
926	if (error)
927		goto out_revert;
928
929	/* Reinitialize in-core state. */
930	return xrep_agi_commit_new(sc, agi_bp);
931
932out_revert:
933	/* Mark the incore AGI state stale and revert the AGI. */
934	sc->sa.pag->pagi_init = 0;
935	memcpy(agi, &old_agi, sizeof(old_agi));
936	return error;
937}
938