blob: aa180492a4a508d9539249ed0ee1290fb58df0fd [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"
12#include "xfs_defer.h"
13#include "xfs_btree.h"
14#include "xfs_bit.h"
15#include "xfs_log_format.h"
16#include "xfs_trans.h"
17#include "xfs_sb.h"
18#include "xfs_inode.h"
19#include "xfs_alloc.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070020#include "xfs_alloc_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070021#include "xfs_ialloc.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070022#include "xfs_ialloc_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070023#include "xfs_rmap.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070024#include "xfs_rmap_btree.h"
25#include "xfs_refcount.h"
26#include "xfs_refcount_btree.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070027#include "scrub/xfs_scrub.h"
28#include "scrub/scrub.h"
29#include "scrub/common.h"
30#include "scrub/trace.h"
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070031#include "scrub/repair.h"
32#include "scrub/bitmap.h"
Darrick J. Wongd25522f2018-05-29 22:18:12 -070033
34/* Superblock */
35
36/* Repair the superblock. */
37int
Darrick J. Wongb5e21962018-07-19 12:29:11 -070038xrep_superblock(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -070039 struct xfs_scrub *sc)
Darrick J. Wongd25522f2018-05-29 22:18:12 -070040{
Darrick J. Wong032d91f2018-07-19 12:29:12 -070041 struct xfs_mount *mp = sc->mp;
42 struct xfs_buf *bp;
43 xfs_agnumber_t agno;
44 int error;
Darrick J. Wongd25522f2018-05-29 22:18:12 -070045
46 /* Don't try to repair AG 0's sb; let xfs_repair deal with it. */
47 agno = sc->sm->sm_agno;
48 if (agno == 0)
49 return -EOPNOTSUPP;
50
51 error = xfs_sb_get_secondary(mp, sc->tp, agno, &bp);
52 if (error)
53 return error;
54
55 /* Copy AG 0's superblock to this one. */
56 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
57 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
58
59 /* Write this to disk. */
60 xfs_trans_buf_set_type(sc->tp, bp, XFS_BLFT_SB_BUF);
61 xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
62 return error;
63}
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -070064
65/* AGF */
66
67struct xrep_agf_allocbt {
68 struct xfs_scrub *sc;
69 xfs_agblock_t freeblks;
70 xfs_agblock_t longest;
71};
72
73/* Record free space shape information. */
74STATIC int
75xrep_agf_walk_allocbt(
76 struct xfs_btree_cur *cur,
77 struct xfs_alloc_rec_incore *rec,
78 void *priv)
79{
80 struct xrep_agf_allocbt *raa = priv;
81 int error = 0;
82
83 if (xchk_should_terminate(raa->sc, &error))
84 return error;
85
86 raa->freeblks += rec->ar_blockcount;
87 if (rec->ar_blockcount > raa->longest)
88 raa->longest = rec->ar_blockcount;
89 return error;
90}
91
92/* Does this AGFL block look sane? */
93STATIC int
94xrep_agf_check_agfl_block(
95 struct xfs_mount *mp,
96 xfs_agblock_t agbno,
97 void *priv)
98{
99 struct xfs_scrub *sc = priv;
100
101 if (!xfs_verify_agbno(mp, sc->sa.agno, agbno))
102 return -EFSCORRUPTED;
103 return 0;
104}
105
106/*
107 * Offset within the xrep_find_ag_btree array for each btree type. Avoid the
108 * XFS_BTNUM_ names here to avoid creating a sparse array.
109 */
110enum {
111 XREP_AGF_BNOBT = 0,
112 XREP_AGF_CNTBT,
113 XREP_AGF_RMAPBT,
114 XREP_AGF_REFCOUNTBT,
115 XREP_AGF_END,
116 XREP_AGF_MAX
117};
118
119/* Check a btree root candidate. */
120static inline bool
121xrep_check_btree_root(
122 struct xfs_scrub *sc,
123 struct xrep_find_ag_btree *fab)
124{
125 struct xfs_mount *mp = sc->mp;
126 xfs_agnumber_t agno = sc->sm->sm_agno;
127
128 return xfs_verify_agbno(mp, agno, fab->root) &&
129 fab->height <= XFS_BTREE_MAXLEVELS;
130}
131
132/*
133 * Given the btree roots described by *fab, find the roots, check them for
134 * sanity, and pass the root data back out via *fab.
135 *
136 * This is /also/ a chicken and egg problem because we have to use the rmapbt
137 * (rooted in the AGF) to find the btrees rooted in the AGF. We also have no
138 * idea if the btrees make any sense. If we hit obvious corruptions in those
139 * btrees we'll bail out.
140 */
141STATIC int
142xrep_agf_find_btrees(
143 struct xfs_scrub *sc,
144 struct xfs_buf *agf_bp,
145 struct xrep_find_ag_btree *fab,
146 struct xfs_buf *agfl_bp)
147{
148 struct xfs_agf *old_agf = XFS_BUF_TO_AGF(agf_bp);
149 int error;
150
151 /* Go find the root data. */
152 error = xrep_find_ag_btree_roots(sc, agf_bp, fab, agfl_bp);
153 if (error)
154 return error;
155
156 /* We must find the bnobt, cntbt, and rmapbt roots. */
157 if (!xrep_check_btree_root(sc, &fab[XREP_AGF_BNOBT]) ||
158 !xrep_check_btree_root(sc, &fab[XREP_AGF_CNTBT]) ||
159 !xrep_check_btree_root(sc, &fab[XREP_AGF_RMAPBT]))
160 return -EFSCORRUPTED;
161
162 /*
163 * We relied on the rmapbt to reconstruct the AGF. If we get a
164 * different root then something's seriously wrong.
165 */
166 if (fab[XREP_AGF_RMAPBT].root !=
167 be32_to_cpu(old_agf->agf_roots[XFS_BTNUM_RMAPi]))
168 return -EFSCORRUPTED;
169
170 /* We must find the refcountbt root if that feature is enabled. */
171 if (xfs_sb_version_hasreflink(&sc->mp->m_sb) &&
172 !xrep_check_btree_root(sc, &fab[XREP_AGF_REFCOUNTBT]))
173 return -EFSCORRUPTED;
174
175 return 0;
176}
177
178/*
179 * Reinitialize the AGF header, making an in-core copy of the old contents so
180 * that we know which in-core state needs to be reinitialized.
181 */
182STATIC void
183xrep_agf_init_header(
184 struct xfs_scrub *sc,
185 struct xfs_buf *agf_bp,
186 struct xfs_agf *old_agf)
187{
188 struct xfs_mount *mp = sc->mp;
189 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp);
190
191 memcpy(old_agf, agf, sizeof(*old_agf));
192 memset(agf, 0, BBTOB(agf_bp->b_length));
193 agf->agf_magicnum = cpu_to_be32(XFS_AGF_MAGIC);
194 agf->agf_versionnum = cpu_to_be32(XFS_AGF_VERSION);
195 agf->agf_seqno = cpu_to_be32(sc->sa.agno);
196 agf->agf_length = cpu_to_be32(xfs_ag_block_count(mp, sc->sa.agno));
197 agf->agf_flfirst = old_agf->agf_flfirst;
198 agf->agf_fllast = old_agf->agf_fllast;
199 agf->agf_flcount = old_agf->agf_flcount;
200 if (xfs_sb_version_hascrc(&mp->m_sb))
201 uuid_copy(&agf->agf_uuid, &mp->m_sb.sb_meta_uuid);
202
203 /* Mark the incore AGF data stale until we're done fixing things. */
204 ASSERT(sc->sa.pag->pagf_init);
205 sc->sa.pag->pagf_init = 0;
206}
207
208/* Set btree root information in an AGF. */
209STATIC void
210xrep_agf_set_roots(
211 struct xfs_scrub *sc,
212 struct xfs_agf *agf,
213 struct xrep_find_ag_btree *fab)
214{
215 agf->agf_roots[XFS_BTNUM_BNOi] =
216 cpu_to_be32(fab[XREP_AGF_BNOBT].root);
217 agf->agf_levels[XFS_BTNUM_BNOi] =
218 cpu_to_be32(fab[XREP_AGF_BNOBT].height);
219
220 agf->agf_roots[XFS_BTNUM_CNTi] =
221 cpu_to_be32(fab[XREP_AGF_CNTBT].root);
222 agf->agf_levels[XFS_BTNUM_CNTi] =
223 cpu_to_be32(fab[XREP_AGF_CNTBT].height);
224
225 agf->agf_roots[XFS_BTNUM_RMAPi] =
226 cpu_to_be32(fab[XREP_AGF_RMAPBT].root);
227 agf->agf_levels[XFS_BTNUM_RMAPi] =
228 cpu_to_be32(fab[XREP_AGF_RMAPBT].height);
229
230 if (xfs_sb_version_hasreflink(&sc->mp->m_sb)) {
231 agf->agf_refcount_root =
232 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].root);
233 agf->agf_refcount_level =
234 cpu_to_be32(fab[XREP_AGF_REFCOUNTBT].height);
235 }
236}
237
238/* Update all AGF fields which derive from btree contents. */
239STATIC int
240xrep_agf_calc_from_btrees(
241 struct xfs_scrub *sc,
242 struct xfs_buf *agf_bp)
243{
244 struct xrep_agf_allocbt raa = { .sc = sc };
245 struct xfs_btree_cur *cur = NULL;
246 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp);
247 struct xfs_mount *mp = sc->mp;
248 xfs_agblock_t btreeblks;
249 xfs_agblock_t blocks;
250 int error;
251
252 /* Update the AGF counters from the bnobt. */
253 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
254 XFS_BTNUM_BNO);
255 error = xfs_alloc_query_all(cur, xrep_agf_walk_allocbt, &raa);
256 if (error)
257 goto err;
258 error = xfs_btree_count_blocks(cur, &blocks);
259 if (error)
260 goto err;
261 xfs_btree_del_cursor(cur, error);
262 btreeblks = blocks - 1;
263 agf->agf_freeblks = cpu_to_be32(raa.freeblks);
264 agf->agf_longest = cpu_to_be32(raa.longest);
265
266 /* Update the AGF counters from the cntbt. */
267 cur = xfs_allocbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno,
268 XFS_BTNUM_CNT);
269 error = xfs_btree_count_blocks(cur, &blocks);
270 if (error)
271 goto err;
272 xfs_btree_del_cursor(cur, error);
273 btreeblks += blocks - 1;
274
275 /* Update the AGF counters from the rmapbt. */
276 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
277 error = xfs_btree_count_blocks(cur, &blocks);
278 if (error)
279 goto err;
280 xfs_btree_del_cursor(cur, error);
281 agf->agf_rmap_blocks = cpu_to_be32(blocks);
282 btreeblks += blocks - 1;
283
284 agf->agf_btreeblks = cpu_to_be32(btreeblks);
285
286 /* Update the AGF counters from the refcountbt. */
287 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
288 cur = xfs_refcountbt_init_cursor(mp, sc->tp, agf_bp,
289 sc->sa.agno);
290 error = xfs_btree_count_blocks(cur, &blocks);
291 if (error)
292 goto err;
293 xfs_btree_del_cursor(cur, error);
294 agf->agf_refcount_blocks = cpu_to_be32(blocks);
295 }
296
297 return 0;
298err:
299 xfs_btree_del_cursor(cur, error);
300 return error;
301}
302
303/* Commit the new AGF and reinitialize the incore state. */
304STATIC int
305xrep_agf_commit_new(
306 struct xfs_scrub *sc,
307 struct xfs_buf *agf_bp)
308{
309 struct xfs_perag *pag;
310 struct xfs_agf *agf = XFS_BUF_TO_AGF(agf_bp);
311
312 /* Trigger fdblocks recalculation */
313 xfs_force_summary_recalc(sc->mp);
314
315 /* Write this to disk. */
316 xfs_trans_buf_set_type(sc->tp, agf_bp, XFS_BLFT_AGF_BUF);
317 xfs_trans_log_buf(sc->tp, agf_bp, 0, BBTOB(agf_bp->b_length) - 1);
318
319 /* Now reinitialize the in-core counters we changed. */
320 pag = sc->sa.pag;
321 pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
322 pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
323 pag->pagf_longest = be32_to_cpu(agf->agf_longest);
324 pag->pagf_levels[XFS_BTNUM_BNOi] =
325 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
326 pag->pagf_levels[XFS_BTNUM_CNTi] =
327 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
328 pag->pagf_levels[XFS_BTNUM_RMAPi] =
329 be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAPi]);
330 pag->pagf_refcount_level = be32_to_cpu(agf->agf_refcount_level);
331 pag->pagf_init = 1;
332
333 return 0;
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_allocbt_buf_ops,
345 .magic = XFS_ABTB_CRC_MAGIC,
346 },
347 [XREP_AGF_CNTBT] = {
348 .rmap_owner = XFS_RMAP_OWN_AG,
349 .buf_ops = &xfs_allocbt_buf_ops,
350 .magic = XFS_ABTC_CRC_MAGIC,
351 },
352 [XREP_AGF_RMAPBT] = {
353 .rmap_owner = XFS_RMAP_OWN_AG,
354 .buf_ops = &xfs_rmapbt_buf_ops,
355 .magic = XFS_RMAP_CRC_MAGIC,
356 },
357 [XREP_AGF_REFCOUNTBT] = {
358 .rmap_owner = XFS_RMAP_OWN_REFC,
359 .buf_ops = &xfs_refcountbt_buf_ops,
360 .magic = XFS_REFC_CRC_MAGIC,
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_sb_version_hasrmapbt(&mp->m_sb))
375 return -EOPNOTSUPP;
376
377 xchk_perag_get(sc->mp, &sc->sa);
378 /*
379 * Make sure we have the AGF buffer, as scrub might have decided it
380 * was corrupt after xfs_alloc_read_agf failed with -EFSCORRUPTED.
381 */
382 error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp,
383 XFS_AG_DADDR(mp, sc->sa.agno, 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 = XFS_BUF_TO_AGF(agf_bp);
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(mp, sc->tp, sc->sa.agno, &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, XFS_BUF_TO_AGF(agf_bp), 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 /* Start rewriting the header and implant the btrees we found. */
421 xrep_agf_init_header(sc, agf_bp, &old_agf);
422 xrep_agf_set_roots(sc, agf, fab);
423 error = xrep_agf_calc_from_btrees(sc, agf_bp);
424 if (error)
425 goto out_revert;
426
427 /* Commit the changes and reinitialize incore state. */
428 return xrep_agf_commit_new(sc, agf_bp);
429
430out_revert:
431 /* Mark the incore AGF state stale and revert the AGF. */
432 sc->sa.pag->pagf_init = 0;
433 memcpy(agf, &old_agf, sizeof(old_agf));
434 return error;
435}