agheader_repair.c 24 KB

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