agheader_repair.c 24 KB

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