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