blob: 401f71579ce6d136b36fd176d7e9baca9f1a893f [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wongd25522f2018-05-29 22:18:12 -07002/*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
Darrick J. Wongd25522f2018-05-29 22:18:12 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wongd25522f2018-05-29 22:18:12 -07005 */
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"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070012#include "xfs_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070013#include "xfs_log_format.h"
14#include "xfs_trans.h"
15#include "xfs_sb.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070016#include "xfs_alloc.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070017#include "xfs_alloc_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070018#include "xfs_ialloc.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070019#include "xfs_ialloc_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070020#include "xfs_rmap.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070021#include "xfs_rmap_btree.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070022#include "xfs_refcount_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070023#include "scrub/scrub.h"
24#include "scrub/common.h"
25#include "scrub/trace.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070026#include "scrub/repair.h"
27#include "scrub/bitmap.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070028
29/* Superblock */
30
31/* Repair the superblock. */
32int
Darrick J. Wongb5e21962018-07-19 12:29:11 -070033xrep_superblock(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070034 struct xfs_scrub *sc)
Darrick J. Wongd25522f2018-05-29 22:18:12 -070035{
Darrick J. Wong032d91f2018-07-19 12:29:12 -070036 struct xfs_mount *mp = sc->mp;
37 struct xfs_buf *bp;
38 xfs_agnumber_t agno;
39 int error;
Darrick J. Wongd25522f2018-05-29 22:18:12 -070040
41 /* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
42 agno = sc->sm->sm_agno;
43 if (agno == 0)
44 return -EOPNOTSUPP;
45
46 error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
47 if (error)
48 return error;
49
50 /* Copy AG 0's superblock to this one. */
51 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
Christoph Hellwig3e6e8af2020-03-10 08:57:30 -070052 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
Darrick J. Wongd25522f2018-05-29 22:18:12 -070053
54 /* Write this to disk. */
55 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
56 xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
57 return error;
58}
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070059
60/* AGF */
61
62struct xrep_agf_allocbt {
63 struct xfs_scrub *sc;
64 xfs_agblock_t freeblks;
65 xfs_agblock_t longest;
66};
67
68/* Record free space shape information. */
69STATIC int
70xrep_agf_walk_allocbt(
71 struct xfs_btree_cur *cur,
72 struct xfs_alloc_rec_incore *rec,
73 void *priv)
74{
75 struct xrep_agf_allocbt *raa = priv;
76 int error = 0;
77
78 if (xchk_should_terminate(raa->sc, &error))
79 return error;
80
81 raa->freeblks += rec->ar_blockcount;
82 if (rec->ar_blockcount > raa->longest)
83 raa->longest = rec->ar_blockcount;
84 return error;
85}
86
87/* Does this AGFL block look sane? */
88STATIC int
89xrep_agf_check_agfl_block(
90 struct xfs_mount *mp,
91 xfs_agblock_t agbno,
92 void *priv)
93{
94 struct xfs_scrub *sc = priv;
95
96 if (!xfs_verify_agbno(mp, sc->sa.agno, agbno))
97 return -EFSCORRUPTED;
98 return 0;
99}
100
101/*
102 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
103 * XFS_BTNUM_ names here to avoid creating a sparse array.
104 */
105enum {
106 XREP_AGF_BNOBT = 0,
107 XREP_AGF_CNTBT,
108 XREP_AGF_RMAPBT,
109 XREP_AGF_REFCOUNTBT,
110 XREP_AGF_END,
111 XREP_AGF_MAX
112};
113
114/* Check a btree root candidate. */
115static inline bool
116xrep_check_btree_root(
117 struct xfs_scrub *sc,
118 struct xrep_find_ag_btree *fab)
119{
120 struct xfs_mount *mp = sc->mp;
121 xfs_agnumber_t agno = sc->sm->sm_agno;
122
123 return xfs_verify_agbno(mp, agno, fab->root) &&
124 fab->height <= XFS_BTREE_MAXLEVELS;
125}
126
127/*
128 * Given the btree roots described by *fab, find the roots, check them for
129 * sanity, and pass the root data back out via *fab.
130 *
131 * This is /also/ a chicken and egg problem because we have to use the rmapbt
132 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no
133 * idea if the btrees make any sense. If we hit obvious corruptions in those
134 * btrees we'll bail out.
135 */
136STATIC int
137xrep_agf_find_btrees(
138 struct xfs_scrub *sc,
139 struct xfs_buf *agf_bp,
140 struct xrep_find_ag_btree *fab,
141 struct xfs_buf *agfl_bp)
142{
Christoph Hellwig9798f612020-03-10 08:57:29 -0700143 struct xfs_agf *old_agf = agf_bp->b_addr;
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700144 int error;
145
146 /* Go find the root data. */
147 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
148 if (error)
149 return error;
150
151 /* We must find the bnobt, cntbt, and rmapbt roots. */
152 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
153 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
154 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
155 return -EFSCORRUPTED;
156
157 /*
158 * We relied on the rmapbt to reconstruct the AGF. If we get a
159 * different root then something's seriously wrong.
160 */
161 if (fab[XREP_AGF_RMAPBT].root !=
162 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
163 return -EFSCORRUPTED;
164
165 /* We must find the refcountbt root if that feature is enabled. */
166 if (xfs_sb_version_hasreflink(&sc->mp->m_sb) &&
167 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
168 return -EFSCORRUPTED;
169
170 return 0;
171}
172
173/*
174 * Reinitialize the AGF header, making an in-core copy of the old contents so
175 * that we know which in-core state needs to be reinitialized.
176 */
177STATIC void
178xrep_agf_init_header(
179 struct xfs_scrub *sc,
180 struct xfs_buf *agf_bp,
181 struct xfs_agf *old_agf)
182{
183 struct xfs_mount *mp = sc->mp;
Christoph Hellwig9798f612020-03-10 08:57:29 -0700184 struct xfs_agf *agf = agf_bp->b_addr;
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700185
186 memcpy(old_agf, agf, sizeof(*old_agf));
187 memset(agf, 0, BBTOB(agf_bp->b_length));
188 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
189 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
190 agf->agf_seqno = cpu_to_be32(sc->sa.agno);
191 agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
192 agf->agf_flfirst = old_agf->agf_flfirst;
193 agf->agf_fllast = old_agf->agf_fllast;
194 agf->agf_flcount = old_agf->agf_flcount;
195 if (xfs_sb_version_hascrc(&mp->m_sb))
196 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
197
198 /* Mark the incore AGF data stale until we're done fixing things. */
199 ASSERT(sc->sa.pag->pagf_init);
200 sc->sa.pag->pagf_init = 0;
201}
202
203/* Set btree root information in an AGF. */
204STATIC void
205xrep_agf_set_roots(
206 struct xfs_scrub *sc,
207 struct xfs_agf *agf,
208 struct xrep_find_ag_btree *fab)
209{
210 agf->agf_roots[XFS_BTNUM_BNOi] =
211 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
212 agf->agf_levels[XFS_BTNUM_BNOi] =
213 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
214
215 agf->agf_roots[XFS_BTNUM_CNTi] =
216 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
217 agf->agf_levels[XFS_BTNUM_CNTi] =
218 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
219
220 agf->agf_roots[XFS_BTNUM_RMAPi] =
221 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
222 agf->agf_levels[XFS_BTNUM_RMAPi] =
223 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
224
225 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
226 agf->agf_refcount_root =
227 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
228 agf->agf_refcount_level =
229 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
230 }
231}
232
233/* Update all AGF fields which derive from btree contents. */
234STATIC int
235xrep_agf_calc_from_btrees(
236 struct xfs_scrub *sc,
237 struct xfs_buf *agf_bp)
238{
239 struct xrep_agf_allocbt raa = { .sc = sc };
240 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig9798f612020-03-10 08:57:29 -0700241 struct xfs_agf *agf = agf_bp->b_addr;
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700242 struct xfs_mount *mp = sc->mp;
243 xfs_agblock_t btreeblks;
244 xfs_agblock_t blocks;
245 int error;
246
247 /* Update the AGF counters from the bnobt. */
248 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
249 XFS_BTNUM_BNO);
250 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
251 if (error)
252 goto err;
253 error = xfs_btree_count_blocks(cur, &blocks);
254 if (error)
255 goto err;
256 xfs_btree_del_cursor(cur, error);
257 btreeblks = blocks - 1;
258 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
259 agf->agf_longest = cpu_to_be32(raa.longest);
260
261 /* Update the AGF counters from the cntbt. */
262 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
263 XFS_BTNUM_CNT);
264 error = xfs_btree_count_blocks(cur, &blocks);
265 if (error)
266 goto err;
267 xfs_btree_del_cursor(cur, error);
268 btreeblks += blocks - 1;
269
270 /* Update the AGF counters from the rmapbt. */
271 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
272 error = xfs_btree_count_blocks(cur, &blocks);
273 if (error)
274 goto err;
275 xfs_btree_del_cursor(cur, error);
276 agf->agf_rmap_blocks = cpu_to_be32(blocks);
277 btreeblks += blocks - 1;
278
279 agf->agf_btreeblks = cpu_to_be32(btreeblks);
280
281 /* Update the AGF counters from the refcountbt. */
282 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
283 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
284 sc->sa.agno);
285 error = xfs_btree_count_blocks(cur, &blocks);
286 if (error)
287 goto err;
288 xfs_btree_del_cursor(cur, error);
289 agf->agf_refcount_blocks = cpu_to_be32(blocks);
290 }
291
292 return 0;
293err:
294 xfs_btree_del_cursor(cur, error);
295 return error;
296}
297
298/* Commit the new AGF and reinitialize the incore state. */
299STATIC int
300xrep_agf_commit_new(
301 struct xfs_scrub *sc,
302 struct xfs_buf *agf_bp)
303{
304 struct xfs_perag *pag;
Christoph Hellwig9798f612020-03-10 08:57:29 -0700305 struct xfs_agf *agf = agf_bp->b_addr;
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700306
307 /* Trigger fdblocks recalculation */
308 xfs_force_summary_recalc(sc->mp);
309
310 /* Write this to disk. */
311 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
312 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
313
314 /* Now reinitialize the in-core counters we changed. */
315 pag = sc->sa.pag;
316 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
317 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
318 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
319 pag->pagf_levels[XFS_BTNUM_BNOi] =
320 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
321 pag->pagf_levels[XFS_BTNUM_CNTi] =
322 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
323 pag->pagf_levels[XFS_BTNUM_RMAPi] =
324 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
325 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
326 pag->pagf_init = 1;
327
328 return 0;
329}
330
331/* Repair the AGF. v5 filesystems only. */
332int
333xrep_agf(
334 struct xfs_scrub *sc)
335{
336 struct xrep_find_ag_btree fab[XREP_AGF_MAX] = {
337 [XREP_AGF_BNOBT] = {
338 .rmap_owner = XFS_RMAP_OWN_AG,
Brian Foster27df4f52019-02-07 10:45:47 -0800339 .buf_ops = &xfs_bnobt_buf_ops,
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700340 },
341 [XREP_AGF_CNTBT] = {
342 .rmap_owner = XFS_RMAP_OWN_AG,
Brian Foster27df4f52019-02-07 10:45:47 -0800343 .buf_ops = &xfs_cntbt_buf_ops,
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700344 },
345 [XREP_AGF_RMAPBT] = {
346 .rmap_owner = XFS_RMAP_OWN_AG,
347 .buf_ops = &xfs_rmapbt_buf_ops,
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700348 },
349 [XREP_AGF_REFCOUNTBT] = {
350 .rmap_owner = XFS_RMAP_OWN_REFC,
351 .buf_ops = &xfs_refcountbt_buf_ops,
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700352 },
353 [XREP_AGF_END] = {
354 .buf_ops = NULL,
355 },
356 };
357 struct xfs_agf old_agf;
358 struct xfs_mount *mp = sc->mp;
359 struct xfs_buf *agf_bp;
360 struct xfs_buf *agfl_bp;
361 struct xfs_agf *agf;
362 int error;
363
364 /* We require the rmapbt to rebuild anything. */
365 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
366 return -EOPNOTSUPP;
367
368 xchk_perag_get(sc->mp, &sc->sa);
369 /*
370 * Make sure we have the AGF buffer, as scrub might have decided it
371 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
372 */
373 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
374 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGF_DADDR(mp)),
375 XFS_FSS_TO_BB(mp, 1), 0, &agf_bp, NULL);
376 if (error)
377 return error;
378 agf_bp->b_ops = &xfs_agf_buf_ops;
Christoph Hellwig9798f612020-03-10 08:57:29 -0700379 agf = agf_bp->b_addr;
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700380
381 /*
382 * Load the AGFL so that we can screen out OWN_AG blocks that are on
383 * the AGFL now; these blocks might have once been part of the
384 * bno/cnt/rmap btrees but are not now. This is a chicken and egg
385 * problem: the AGF is corrupt, so we have to trust the AGFL contents
386 * because we can't do any serious cross-referencing with any of the
387 * btrees rooted in the AGF. If the AGFL contents are obviously bad
388 * then we'll bail out.
389 */
390 error = xfs_alloc_read_agfl(mp, sc->tp, sc->sa.agno, &agfl_bp);
391 if (error)
392 return error;
393
394 /*
395 * Spot-check the AGFL blocks; if they're obviously corrupt then
396 * there's nothing we can do but bail out.
397 */
Christoph Hellwig9798f612020-03-10 08:57:29 -0700398 error = xfs_agfl_walk(sc->mp, agf_bp->b_addr, agfl_bp,
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700399 xrep_agf_check_agfl_block, sc);
400 if (error)
401 return error;
402
403 /*
404 * Find the AGF btree roots. This is also a chicken-and-egg situation;
405 * see the function for more details.
406 */
407 error = xrep_agf_find_btrees(sc, agf_bp, fab, agfl_bp);
408 if (error)
409 return error;
410
411 /* Start rewriting the header and implant the btrees we found. */
412 xrep_agf_init_header(sc, agf_bp, &old_agf);
413 xrep_agf_set_roots(sc, agf, fab);
414 error = xrep_agf_calc_from_btrees(sc, agf_bp);
415 if (error)
416 goto out_revert;
417
418 /* Commit the changes and reinitialize incore state. */
419 return xrep_agf_commit_new(sc, agf_bp);
420
421out_revert:
422 /* Mark the incore AGF state stale and revert the AGF. */
423 sc->sa.pag->pagf_init = 0;
424 memcpy(agf, &old_agf, sizeof(old_agf));
425 return error;
426}
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700427
428/* AGFL */
429
430struct xrep_agfl {
431 /* Bitmap of other OWN_AG metadata blocks. */
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700432 struct xbitmap agmetablocks;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700433
434 /* Bitmap of free space. */
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700435 struct xbitmap *freesp;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700436
437 struct xfs_scrub *sc;
438};
439
440/* Record all OWN_AG (free space btree) information from the rmap data. */
441STATIC int
442xrep_agfl_walk_rmap(
443 struct xfs_btree_cur *cur,
444 struct xfs_rmap_irec *rec,
445 void *priv)
446{
447 struct xrep_agfl *ra = priv;
448 xfs_fsblock_t fsb;
449 int error = 0;
450
451 if (xchk_should_terminate(ra->sc, &error))
452 return error;
453
454 /* Record all the OWN_AG blocks. */
455 if (rec->rm_owner == XFS_RMAP_OWN_AG) {
Dave Chinner576af732020-03-10 17:51:15 -0700456 fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_ag.agno,
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700457 rec->rm_startblock);
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700458 error = xbitmap_set(ra->freesp, fsb, rec->rm_blockcount);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700459 if (error)
460 return error;
461 }
462
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700463 return xbitmap_set_btcur_path(&ra->agmetablocks, cur);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700464}
465
466/*
467 * Map out all the non-AGFL OWN_AG space in this AG so that we can deduce
468 * which blocks belong to the AGFL.
469 *
470 * Compute the set of old AGFL blocks by subtracting from the list of OWN_AG
471 * blocks the list of blocks owned by all other OWN_AG metadata (bnobt, cntbt,
472 * rmapbt). These are the old AGFL blocks, so return that list and the number
473 * of blocks we're actually going to put back on the AGFL.
474 */
475STATIC int
476xrep_agfl_collect_blocks(
477 struct xfs_scrub *sc,
478 struct xfs_buf *agf_bp,
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700479 struct xbitmap *agfl_extents,
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700480 xfs_agblock_t *flcount)
481{
482 struct xrep_agfl ra;
483 struct xfs_mount *mp = sc->mp;
484 struct xfs_btree_cur *cur;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700485 int error;
486
487 ra.sc = sc;
488 ra.freesp = agfl_extents;
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700489 xbitmap_init(&ra.agmetablocks);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700490
491 /* Find all space used by the free space btrees & rmapbt. */
492 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
493 error = xfs_rmap_query_all(cur, xrep_agfl_walk_rmap, &ra);
494 if (error)
495 goto err;
496 xfs_btree_del_cursor(cur, error);
497
498 /* Find all blocks currently being used by the bnobt. */
499 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
500 XFS_BTNUM_BNO);
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700501 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700502 if (error)
503 goto err;
504 xfs_btree_del_cursor(cur, error);
505
506 /* Find all blocks currently being used by the cntbt. */
507 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
508 XFS_BTNUM_CNT);
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700509 error = xbitmap_set_btblocks(&ra.agmetablocks, cur);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700510 if (error)
511 goto err;
512
513 xfs_btree_del_cursor(cur, error);
514
515 /*
516 * Drop the freesp meta blocks that are in use by btrees.
517 * The remaining blocks /should/ be AGFL blocks.
518 */
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700519 error = xbitmap_disunion(agfl_extents, &ra.agmetablocks);
520 xbitmap_destroy(&ra.agmetablocks);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700521 if (error)
522 return error;
523
524 /*
525 * Calculate the new AGFL size. If we found more blocks than fit in
526 * the AGFL we'll free them later.
527 */
Darrick J. Wong608eb3c2020-03-16 17:16:35 -0700528 *flcount = min_t(uint64_t, xbitmap_hweight(agfl_extents),
529 xfs_agfl_size(mp));
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700530 return 0;
531
532err:
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700533 xbitmap_destroy(&ra.agmetablocks);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700534 xfs_btree_del_cursor(cur, error);
535 return error;
536}
537
538/* Update the AGF and reset the in-core state. */
539STATIC void
540xrep_agfl_update_agf(
541 struct xfs_scrub *sc,
542 struct xfs_buf *agf_bp,
543 xfs_agblock_t flcount)
544{
Christoph Hellwig9798f612020-03-10 08:57:29 -0700545 struct xfs_agf *agf = agf_bp->b_addr;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700546
547 ASSERT(flcount <= xfs_agfl_size(sc->mp));
548
549 /* Trigger fdblocks recalculation */
550 xfs_force_summary_recalc(sc->mp);
551
552 /* Update the AGF counters. */
553 if (sc->sa.pag->pagf_init)
554 sc->sa.pag->pagf_flcount = flcount;
555 agf->agf_flfirst = cpu_to_be32(0);
556 agf->agf_flcount = cpu_to_be32(flcount);
557 agf->agf_fllast = cpu_to_be32(flcount - 1);
558
559 xfs_alloc_log_agf(sc->tp, agf_bp,
560 XFS_AGF_FLFIRST | XFS_AGF_FLLAST | XFS_AGF_FLCOUNT);
561}
562
563/* Write out a totally new AGFL. */
564STATIC void
565xrep_agfl_init_header(
566 struct xfs_scrub *sc,
567 struct xfs_buf *agfl_bp,
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700568 struct xbitmap *agfl_extents,
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700569 xfs_agblock_t flcount)
570{
571 struct xfs_mount *mp = sc->mp;
572 __be32 *agfl_bno;
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700573 struct xbitmap_range *br;
574 struct xbitmap_range *n;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700575 struct xfs_agfl *agfl;
576 xfs_agblock_t agbno;
577 unsigned int fl_off;
578
579 ASSERT(flcount <= xfs_agfl_size(mp));
580
581 /*
582 * Start rewriting the header by setting the bno[] array to
583 * NULLAGBLOCK, then setting AGFL header fields.
584 */
585 agfl = XFS_BUF_TO_AGFL(agfl_bp);
586 memset(agfl, 0xFF, BBTOB(agfl_bp->b_length));
587 agfl->agfl_magicnum = cpu_to_be32(XFS_AGFL_MAGIC);
588 agfl->agfl_seqno = cpu_to_be32(sc->sa.agno);
589 uuid_copy(&agfl->agfl_uuid, &mp->m_sb.sb_meta_uuid);
590
591 /*
592 * Fill the AGFL with the remaining blocks. If agfl_extents has more
593 * blocks than fit in the AGFL, they will be freed in a subsequent
594 * step.
595 */
596 fl_off = 0;
Christoph Hellwig183606d2020-03-10 08:57:28 -0700597 agfl_bno = xfs_buf_to_agfl_bno(agfl_bp);
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700598 for_each_xbitmap_extent(br, n, agfl_extents) {
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700599 agbno = XFS_FSB_TO_AGBNO(mp, br->start);
600
601 trace_xrep_agfl_insert(mp, sc->sa.agno, agbno, br->len);
602
603 while (br->len > 0 && fl_off < flcount) {
604 agfl_bno[fl_off] = cpu_to_be32(agbno);
605 fl_off++;
606 agbno++;
607
608 /*
609 * We've now used br->start by putting it in the AGFL,
610 * so bump br so that we don't reap the block later.
611 */
612 br->start++;
613 br->len--;
614 }
615
616 if (br->len)
617 break;
618 list_del(&br->list);
619 kmem_free(br);
620 }
621
622 /* Write new AGFL to disk. */
623 xfs_trans_buf_set_type(sc->tp, agfl_bp, XFS_BLFT_AGFL_BUF);
624 xfs_trans_log_buf(sc->tp, agfl_bp, 0, BBTOB(agfl_bp->b_length) - 1);
625}
626
627/* Repair the AGFL. */
628int
629xrep_agfl(
630 struct xfs_scrub *sc)
631{
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700632 struct xbitmap agfl_extents;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700633 struct xfs_mount *mp = sc->mp;
634 struct xfs_buf *agf_bp;
635 struct xfs_buf *agfl_bp;
636 xfs_agblock_t flcount;
637 int error;
638
639 /* We require the rmapbt to rebuild anything. */
640 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
641 return -EOPNOTSUPP;
642
643 xchk_perag_get(sc->mp, &sc->sa);
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700644 xbitmap_init(&agfl_extents);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700645
646 /*
647 * Read the AGF so that we can query the rmapbt. We hope that there's
648 * nothing wrong with the AGF, but all the AG header repair functions
649 * have this chicken-and-egg problem.
650 */
651 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
652 if (error)
653 return error;
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700654
655 /*
656 * Make sure we have the AGFL buffer, as scrub might have decided it
657 * was corrupt after xfs_alloc_read_agfl failed with -EFSCORRUPTED.
658 */
659 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
660 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGFL_DADDR(mp)),
661 XFS_FSS_TO_BB(mp, 1), 0, &agfl_bp, NULL);
662 if (error)
663 return error;
664 agfl_bp->b_ops = &xfs_agfl_buf_ops;
665
666 /* Gather all the extents we're going to put on the new AGFL. */
667 error = xrep_agfl_collect_blocks(sc, agf_bp, &agfl_extents, &flcount);
668 if (error)
669 goto err;
670
671 /*
672 * Update AGF and AGFL. We reset the global free block counter when
673 * we adjust the AGF flcount (which can fail) so avoid updating any
674 * buffers until we know that part works.
675 */
676 xrep_agfl_update_agf(sc, agf_bp, flcount);
677 xrep_agfl_init_header(sc, agfl_bp, &agfl_extents, flcount);
678
679 /*
680 * Ok, the AGFL should be ready to go now. Roll the transaction to
681 * make the new AGFL permanent before we start using it to return
682 * freespace overflow to the freespace btrees.
683 */
684 sc->sa.agf_bp = agf_bp;
685 sc->sa.agfl_bp = agfl_bp;
686 error = xrep_roll_ag_trans(sc);
687 if (error)
688 goto err;
689
690 /* Dump any AGFL overflow. */
Darrick J. Wong37a65472020-03-16 17:12:34 -0700691 error = xrep_reap_extents(sc, &agfl_extents, &XFS_RMAP_OINFO_AG,
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800692 XFS_AG_RESV_AGFL);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700693err:
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700694 xbitmap_destroy(&agfl_extents);
Darrick J. Wong0e93d3f2018-08-09 22:43:02 -0700695 return error;
696}
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700697
698/* AGI */
699
700/*
701 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
702 * XFS_BTNUM_ names here to avoid creating a sparse array.
703 */
704enum {
705 XREP_AGI_INOBT = 0,
706 XREP_AGI_FINOBT,
707 XREP_AGI_END,
708 XREP_AGI_MAX
709};
710
711/*
712 * Given the inode btree roots described by *fab, find the roots, check them
713 * for sanity, and pass the root data back out via *fab.
714 */
715STATIC int
716xrep_agi_find_btrees(
717 struct xfs_scrub *sc,
718 struct xrep_find_ag_btree *fab)
719{
720 struct xfs_buf *agf_bp;
721 struct xfs_mount *mp = sc->mp;
722 int error;
723
724 /* Read the AGF. */
725 error = xfs_alloc_read_agf(mp, sc->tp, sc->sa.agno, 0, &agf_bp);
726 if (error)
727 return error;
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700728
729 /* Find the btree roots. */
730 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, NULL);
731 if (error)
732 return error;
733
734 /* We must find the inobt root. */
735 if (!xrep_check_btree_root(sc, &fab[XREP_AGI_INOBT]))
736 return -EFSCORRUPTED;
737
738 /* We must find the finobt root if that feature is enabled. */
739 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
740 !xrep_check_btree_root(sc, &fab[XREP_AGI_FINOBT]))
741 return -EFSCORRUPTED;
742
743 return 0;
744}
745
746/*
747 * Reinitialize the AGI header, making an in-core copy of the old contents so
748 * that we know which in-core state needs to be reinitialized.
749 */
750STATIC void
751xrep_agi_init_header(
752 struct xfs_scrub *sc,
753 struct xfs_buf *agi_bp,
754 struct xfs_agi *old_agi)
755{
Christoph Hellwig370c7822020-03-10 08:57:29 -0700756 struct xfs_agi *agi = agi_bp->b_addr;
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700757 struct xfs_mount *mp = sc->mp;
758
759 memcpy(old_agi, agi, sizeof(*old_agi));
760 memset(agi, 0, BBTOB(agi_bp->b_length));
761 agi->agi_magicnum = cpu_to_be32(XFS_AGI_MAGIC);
762 agi->agi_versionnum = cpu_to_be32(XFS_AGI_VERSION);
763 agi->agi_seqno = cpu_to_be32(sc->sa.agno);
764 agi->agi_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
765 agi->agi_newino = cpu_to_be32(NULLAGINO);
766 agi->agi_dirino = cpu_to_be32(NULLAGINO);
767 if (xfs_sb_version_hascrc(&mp->m_sb))
768 uuid_copy(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid);
769
770 /* We don't know how to fix the unlinked list yet. */
771 memcpy(&agi->agi_unlinked, &old_agi->agi_unlinked,
772 sizeof(agi->agi_unlinked));
773
774 /* Mark the incore AGF data stale until we're done fixing things. */
775 ASSERT(sc->sa.pag->pagi_init);
776 sc->sa.pag->pagi_init = 0;
777}
778
779/* Set btree root information in an AGI. */
780STATIC void
781xrep_agi_set_roots(
782 struct xfs_scrub *sc,
783 struct xfs_agi *agi,
784 struct xrep_find_ag_btree *fab)
785{
786 agi->agi_root = cpu_to_be32(fab[XREP_AGI_INOBT].root);
787 agi->agi_level = cpu_to_be32(fab[XREP_AGI_INOBT].height);
788
789 if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) {
790 agi->agi_free_root = cpu_to_be32(fab[XREP_AGI_FINOBT].root);
791 agi->agi_free_level = cpu_to_be32(fab[XREP_AGI_FINOBT].height);
792 }
793}
794
795/* Update the AGI counters. */
796STATIC int
797xrep_agi_calc_from_btrees(
798 struct xfs_scrub *sc,
799 struct xfs_buf *agi_bp)
800{
801 struct xfs_btree_cur *cur;
Christoph Hellwig370c7822020-03-10 08:57:29 -0700802 struct xfs_agi *agi = agi_bp->b_addr;
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700803 struct xfs_mount *mp = sc->mp;
804 xfs_agino_t count;
805 xfs_agino_t freecount;
806 int error;
807
808 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno,
809 XFS_BTNUM_INO);
810 error = xfs_ialloc_count_inodes(cur, &count, &freecount);
811 if (error)
812 goto err;
Darrick J. Wong11f74422020-08-26 10:48:50 -0700813 if (xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
814 xfs_agblock_t blocks;
815
816 error = xfs_btree_count_blocks(cur, &blocks);
817 if (error)
818 goto err;
819 agi->agi_iblocks = cpu_to_be32(blocks);
820 }
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700821 xfs_btree_del_cursor(cur, error);
822
823 agi->agi_count = cpu_to_be32(count);
824 agi->agi_freecount = cpu_to_be32(freecount);
Darrick J. Wong11f74422020-08-26 10:48:50 -0700825
826 if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
827 xfs_sb_version_hasinobtcounts(&mp->m_sb)) {
828 xfs_agblock_t blocks;
829
830 cur = xfs_inobt_init_cursor(mp, sc->tp, agi_bp, sc->sa.agno,
831 XFS_BTNUM_FINO);
832 if (error)
833 goto err;
834 error = xfs_btree_count_blocks(cur, &blocks);
835 if (error)
836 goto err;
837 xfs_btree_del_cursor(cur, error);
838 agi->agi_fblocks = cpu_to_be32(blocks);
839 }
840
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700841 return 0;
842err:
843 xfs_btree_del_cursor(cur, error);
844 return error;
845}
846
847/* Trigger reinitialization of the in-core data. */
848STATIC int
849xrep_agi_commit_new(
850 struct xfs_scrub *sc,
851 struct xfs_buf *agi_bp)
852{
853 struct xfs_perag *pag;
Christoph Hellwig370c7822020-03-10 08:57:29 -0700854 struct xfs_agi *agi = agi_bp->b_addr;
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700855
856 /* Trigger inode count recalculation */
857 xfs_force_summary_recalc(sc->mp);
858
859 /* Write this to disk. */
860 xfs_trans_buf_set_type(sc->tp, agi_bp, XFS_BLFT_AGI_BUF);
861 xfs_trans_log_buf(sc->tp, agi_bp, 0, BBTOB(agi_bp->b_length) - 1);
862
863 /* Now reinitialize the in-core counters if necessary. */
864 pag = sc->sa.pag;
865 pag->pagi_count = be32_to_cpu(agi->agi_count);
866 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
867 pag->pagi_init = 1;
868
869 return 0;
870}
871
872/* Repair the AGI. */
873int
874xrep_agi(
875 struct xfs_scrub *sc)
876{
877 struct xrep_find_ag_btree fab[XREP_AGI_MAX] = {
878 [XREP_AGI_INOBT] = {
879 .rmap_owner = XFS_RMAP_OWN_INOBT,
880 .buf_ops = &xfs_inobt_buf_ops,
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700881 },
882 [XREP_AGI_FINOBT] = {
883 .rmap_owner = XFS_RMAP_OWN_INOBT,
Brian Foster01e68f42019-02-07 10:45:46 -0800884 .buf_ops = &xfs_finobt_buf_ops,
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700885 },
886 [XREP_AGI_END] = {
887 .buf_ops = NULL
888 },
889 };
890 struct xfs_agi old_agi;
891 struct xfs_mount *mp = sc->mp;
892 struct xfs_buf *agi_bp;
893 struct xfs_agi *agi;
894 int error;
895
896 /* We require the rmapbt to rebuild anything. */
897 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
898 return -EOPNOTSUPP;
899
900 xchk_perag_get(sc->mp, &sc->sa);
901 /*
902 * Make sure we have the AGI buffer, as scrub might have decided it
903 * was corrupt after xfs_ialloc_read_agi failed with -EFSCORRUPTED.
904 */
905 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
906 XFS_AG_DADDR(mp, sc->sa.agno, XFS_AGI_DADDR(mp)),
907 XFS_FSS_TO_BB(mp, 1), 0, &agi_bp, NULL);
908 if (error)
909 return error;
910 agi_bp->b_ops = &xfs_agi_buf_ops;
Christoph Hellwig370c7822020-03-10 08:57:29 -0700911 agi = agi_bp->b_addr;
Darrick J. Wong13942aa2018-08-09 22:43:04 -0700912
913 /* Find the AGI btree roots. */
914 error = xrep_agi_find_btrees(sc, fab);
915 if (error)
916 return error;
917
918 /* Start rewriting the header and implant the btrees we found. */
919 xrep_agi_init_header(sc, agi_bp, &old_agi);
920 xrep_agi_set_roots(sc, agi, fab);
921 error = xrep_agi_calc_from_btrees(sc, agi_bp);
922 if (error)
923 goto out_revert;
924
925 /* Reinitialize in-core state. */
926 return xrep_agi_commit_new(sc, agi_bp);
927
928out_revert:
929 /* Mark the incore AGI state stale and revert the AGI. */
930 sc->sa.pag->pagi_init = 0;
931 memcpy(agi, &old_agi, sizeof(old_agi));
932 return error;
933}