repair.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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_icache.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_alloc_btree.h"
  22. #include "xfs_ialloc.h"
  23. #include "xfs_ialloc_btree.h"
  24. #include "xfs_rmap.h"
  25. #include "xfs_rmap_btree.h"
  26. #include "xfs_refcount.h"
  27. #include "xfs_refcount_btree.h"
  28. #include "xfs_extent_busy.h"
  29. #include "xfs_ag_resv.h"
  30. #include "xfs_trans_space.h"
  31. #include "xfs_quota.h"
  32. #include "scrub/xfs_scrub.h"
  33. #include "scrub/scrub.h"
  34. #include "scrub/common.h"
  35. #include "scrub/trace.h"
  36. #include "scrub/repair.h"
  37. #include "scrub/bitmap.h"
  38. /*
  39. * Attempt to repair some metadata, if the metadata is corrupt and userspace
  40. * told us to fix it. This function returns -EAGAIN to mean "re-run scrub",
  41. * and will set *fixed to true if it thinks it repaired anything.
  42. */
  43. int
  44. xrep_attempt(
  45. struct xfs_inode *ip,
  46. struct xfs_scrub *sc,
  47. bool *fixed)
  48. {
  49. int error = 0;
  50. trace_xrep_attempt(ip, sc->sm, error);
  51. xchk_ag_btcur_free(&sc->sa);
  52. /* Repair whatever's broken. */
  53. ASSERT(sc->ops->repair);
  54. error = sc->ops->repair(sc);
  55. trace_xrep_done(ip, sc->sm, error);
  56. switch (error) {
  57. case 0:
  58. /*
  59. * Repair succeeded. Commit the fixes and perform a second
  60. * scrub so that we can tell userspace if we fixed the problem.
  61. */
  62. sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
  63. *fixed = true;
  64. return -EAGAIN;
  65. case -EDEADLOCK:
  66. case -EAGAIN:
  67. /* Tell the caller to try again having grabbed all the locks. */
  68. if (!sc->try_harder) {
  69. sc->try_harder = true;
  70. return -EAGAIN;
  71. }
  72. /*
  73. * We tried harder but still couldn't grab all the resources
  74. * we needed to fix it. The corruption has not been fixed,
  75. * so report back to userspace.
  76. */
  77. return -EFSCORRUPTED;
  78. default:
  79. return error;
  80. }
  81. }
  82. /*
  83. * Complain about unfixable problems in the filesystem. We don't log
  84. * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
  85. * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
  86. * administrator isn't running xfs_scrub in no-repairs mode.
  87. *
  88. * Use this helper function because _ratelimited silently declares a static
  89. * structure to track rate limiting information.
  90. */
  91. void
  92. xrep_failure(
  93. struct xfs_mount *mp)
  94. {
  95. xfs_alert_ratelimited(mp,
  96. "Corruption not fixed during online repair. Unmount and run xfs_repair.");
  97. }
  98. /*
  99. * Repair probe -- userspace uses this to probe if we're willing to repair a
  100. * given mountpoint.
  101. */
  102. int
  103. xrep_probe(
  104. struct xfs_scrub *sc)
  105. {
  106. int error = 0;
  107. if (xchk_should_terminate(sc, &error))
  108. return error;
  109. return 0;
  110. }
  111. /*
  112. * Roll a transaction, keeping the AG headers locked and reinitializing
  113. * the btree cursors.
  114. */
  115. int
  116. xrep_roll_ag_trans(
  117. struct xfs_scrub *sc)
  118. {
  119. int error;
  120. /* Keep the AG header buffers locked so we can keep going. */
  121. if (sc->sa.agi_bp)
  122. xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
  123. if (sc->sa.agf_bp)
  124. xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
  125. if (sc->sa.agfl_bp)
  126. xfs_trans_bhold(sc->tp, sc->sa.agfl_bp);
  127. /* Roll the transaction. */
  128. error = xfs_trans_roll(&sc->tp);
  129. if (error)
  130. goto out_release;
  131. /* Join AG headers to the new transaction. */
  132. if (sc->sa.agi_bp)
  133. xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
  134. if (sc->sa.agf_bp)
  135. xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
  136. if (sc->sa.agfl_bp)
  137. xfs_trans_bjoin(sc->tp, sc->sa.agfl_bp);
  138. return 0;
  139. out_release:
  140. /*
  141. * Rolling failed, so release the hold on the buffers. The
  142. * buffers will be released during teardown on our way out
  143. * of the kernel.
  144. */
  145. if (sc->sa.agi_bp)
  146. xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp);
  147. if (sc->sa.agf_bp)
  148. xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp);
  149. if (sc->sa.agfl_bp)
  150. xfs_trans_bhold_release(sc->tp, sc->sa.agfl_bp);
  151. return error;
  152. }
  153. /*
  154. * Does the given AG have enough space to rebuild a btree? Neither AG
  155. * reservation can be critical, and we must have enough space (factoring
  156. * in AG reservations) to construct a whole btree.
  157. */
  158. bool
  159. xrep_ag_has_space(
  160. struct xfs_perag *pag,
  161. xfs_extlen_t nr_blocks,
  162. enum xfs_ag_resv_type type)
  163. {
  164. return !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
  165. !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
  166. pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
  167. }
  168. /*
  169. * Figure out how many blocks to reserve for an AG repair. We calculate the
  170. * worst case estimate for the number of blocks we'd need to rebuild one of
  171. * any type of per-AG btree.
  172. */
  173. xfs_extlen_t
  174. xrep_calc_ag_resblks(
  175. struct xfs_scrub *sc)
  176. {
  177. struct xfs_mount *mp = sc->mp;
  178. struct xfs_scrub_metadata *sm = sc->sm;
  179. struct xfs_perag *pag;
  180. struct xfs_buf *bp;
  181. xfs_agino_t icount = NULLAGINO;
  182. xfs_extlen_t aglen = NULLAGBLOCK;
  183. xfs_extlen_t usedlen;
  184. xfs_extlen_t freelen;
  185. xfs_extlen_t bnobt_sz;
  186. xfs_extlen_t inobt_sz;
  187. xfs_extlen_t rmapbt_sz;
  188. xfs_extlen_t refcbt_sz;
  189. int error;
  190. if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
  191. return 0;
  192. pag = xfs_perag_get(mp, sm->sm_agno);
  193. if (pag->pagi_init) {
  194. /* Use in-core icount if possible. */
  195. icount = pag->pagi_count;
  196. } else {
  197. /* Try to get the actual counters from disk. */
  198. error = xfs_ialloc_read_agi(mp, NULL, sm->sm_agno, &bp);
  199. if (!error) {
  200. icount = pag->pagi_count;
  201. xfs_buf_relse(bp);
  202. }
  203. }
  204. /* Now grab the block counters from the AGF. */
  205. error = xfs_alloc_read_agf(mp, NULL, sm->sm_agno, 0, &bp);
  206. if (!error) {
  207. aglen = be32_to_cpu(XFS_BUF_TO_AGF(bp)->agf_length);
  208. freelen = be32_to_cpu(XFS_BUF_TO_AGF(bp)->agf_freeblks);
  209. usedlen = aglen - freelen;
  210. xfs_buf_relse(bp);
  211. }
  212. xfs_perag_put(pag);
  213. /* If the icount is impossible, make some worst-case assumptions. */
  214. if (icount == NULLAGINO ||
  215. !xfs_verify_agino(mp, sm->sm_agno, icount)) {
  216. xfs_agino_t first, last;
  217. xfs_agino_range(mp, sm->sm_agno, &first, &last);
  218. icount = last - first + 1;
  219. }
  220. /* If the block counts are impossible, make worst-case assumptions. */
  221. if (aglen == NULLAGBLOCK ||
  222. aglen != xfs_ag_block_count(mp, sm->sm_agno) ||
  223. freelen >= aglen) {
  224. aglen = xfs_ag_block_count(mp, sm->sm_agno);
  225. freelen = aglen;
  226. usedlen = aglen;
  227. }
  228. trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
  229. freelen, usedlen);
  230. /*
  231. * Figure out how many blocks we'd need worst case to rebuild
  232. * each type of btree. Note that we can only rebuild the
  233. * bnobt/cntbt or inobt/finobt as pairs.
  234. */
  235. bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
  236. if (xfs_sb_version_hassparseinodes(&mp->m_sb))
  237. inobt_sz = xfs_iallocbt_calc_size(mp, icount /
  238. XFS_INODES_PER_HOLEMASK_BIT);
  239. else
  240. inobt_sz = xfs_iallocbt_calc_size(mp, icount /
  241. XFS_INODES_PER_CHUNK);
  242. if (xfs_sb_version_hasfinobt(&mp->m_sb))
  243. inobt_sz *= 2;
  244. if (xfs_sb_version_hasreflink(&mp->m_sb))
  245. refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
  246. else
  247. refcbt_sz = 0;
  248. if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
  249. /*
  250. * Guess how many blocks we need to rebuild the rmapbt.
  251. * For non-reflink filesystems we can't have more records than
  252. * used blocks. However, with reflink it's possible to have
  253. * more than one rmap record per AG block. We don't know how
  254. * many rmaps there could be in the AG, so we start off with
  255. * what we hope is an generous over-estimation.
  256. */
  257. if (xfs_sb_version_hasreflink(&mp->m_sb))
  258. rmapbt_sz = xfs_rmapbt_calc_size(mp,
  259. (unsigned long long)aglen * 2);
  260. else
  261. rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
  262. } else {
  263. rmapbt_sz = 0;
  264. }
  265. trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
  266. inobt_sz, rmapbt_sz, refcbt_sz);
  267. return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
  268. }
  269. /* Allocate a block in an AG. */
  270. int
  271. xrep_alloc_ag_block(
  272. struct xfs_scrub *sc,
  273. struct xfs_owner_info *oinfo,
  274. xfs_fsblock_t *fsbno,
  275. enum xfs_ag_resv_type resv)
  276. {
  277. struct xfs_alloc_arg args = {0};
  278. xfs_agblock_t bno;
  279. int error;
  280. switch (resv) {
  281. case XFS_AG_RESV_AGFL:
  282. case XFS_AG_RESV_RMAPBT:
  283. error = xfs_alloc_get_freelist(sc->tp, sc->sa.agf_bp, &bno, 1);
  284. if (error)
  285. return error;
  286. if (bno == NULLAGBLOCK)
  287. return -ENOSPC;
  288. xfs_extent_busy_reuse(sc->mp, sc->sa.agno, bno,
  289. 1, false);
  290. *fsbno = XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, bno);
  291. if (resv == XFS_AG_RESV_RMAPBT)
  292. xfs_ag_resv_rmapbt_alloc(sc->mp, sc->sa.agno);
  293. return 0;
  294. default:
  295. break;
  296. }
  297. args.tp = sc->tp;
  298. args.mp = sc->mp;
  299. args.oinfo = *oinfo;
  300. args.fsbno = XFS_AGB_TO_FSB(args.mp, sc->sa.agno, 0);
  301. args.minlen = 1;
  302. args.maxlen = 1;
  303. args.prod = 1;
  304. args.type = XFS_ALLOCTYPE_THIS_AG;
  305. args.resv = resv;
  306. error = xfs_alloc_vextent(&args);
  307. if (error)
  308. return error;
  309. if (args.fsbno == NULLFSBLOCK)
  310. return -ENOSPC;
  311. ASSERT(args.len == 1);
  312. *fsbno = args.fsbno;
  313. return 0;
  314. }
  315. /* Initialize a new AG btree root block with zero entries. */
  316. int
  317. xrep_init_btblock(
  318. struct xfs_scrub *sc,
  319. xfs_fsblock_t fsb,
  320. struct xfs_buf **bpp,
  321. xfs_btnum_t btnum,
  322. const struct xfs_buf_ops *ops)
  323. {
  324. struct xfs_trans *tp = sc->tp;
  325. struct xfs_mount *mp = sc->mp;
  326. struct xfs_buf *bp;
  327. trace_xrep_init_btblock(mp, XFS_FSB_TO_AGNO(mp, fsb),
  328. XFS_FSB_TO_AGBNO(mp, fsb), btnum);
  329. ASSERT(XFS_FSB_TO_AGNO(mp, fsb) == sc->sa.agno);
  330. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, XFS_FSB_TO_DADDR(mp, fsb),
  331. XFS_FSB_TO_BB(mp, 1), 0);
  332. xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
  333. xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.agno, 0);
  334. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
  335. xfs_trans_log_buf(tp, bp, 0, bp->b_length);
  336. bp->b_ops = ops;
  337. *bpp = bp;
  338. return 0;
  339. }
  340. /*
  341. * Reconstructing per-AG Btrees
  342. *
  343. * When a space btree is corrupt, we don't bother trying to fix it. Instead,
  344. * we scan secondary space metadata to derive the records that should be in
  345. * the damaged btree, initialize a fresh btree root, and insert the records.
  346. * Note that for rebuilding the rmapbt we scan all the primary data to
  347. * generate the new records.
  348. *
  349. * However, that leaves the matter of removing all the metadata describing the
  350. * old broken structure. For primary metadata we use the rmap data to collect
  351. * every extent with a matching rmap owner (bitmap); we then iterate all other
  352. * metadata structures with the same rmap owner to collect the extents that
  353. * cannot be removed (sublist). We then subtract sublist from bitmap to
  354. * derive the blocks that were used by the old btree. These blocks can be
  355. * reaped.
  356. *
  357. * For rmapbt reconstructions we must use different tactics for extent
  358. * collection. First we iterate all primary metadata (this excludes the old
  359. * rmapbt, obviously) to generate new rmap records. The gaps in the rmap
  360. * records are collected as bitmap. The bnobt records are collected as
  361. * sublist. As with the other btrees we subtract sublist from bitmap, and the
  362. * result (since the rmapbt lives in the free space) are the blocks from the
  363. * old rmapbt.
  364. *
  365. * Disposal of Blocks from Old per-AG Btrees
  366. *
  367. * Now that we've constructed a new btree to replace the damaged one, we want
  368. * to dispose of the blocks that (we think) the old btree was using.
  369. * Previously, we used the rmapbt to collect the extents (bitmap) with the
  370. * rmap owner corresponding to the tree we rebuilt, collected extents for any
  371. * blocks with the same rmap owner that are owned by another data structure
  372. * (sublist), and subtracted sublist from bitmap. In theory the extents
  373. * remaining in bitmap are the old btree's blocks.
  374. *
  375. * Unfortunately, it's possible that the btree was crosslinked with other
  376. * blocks on disk. The rmap data can tell us if there are multiple owners, so
  377. * if the rmapbt says there is an owner of this block other than @oinfo, then
  378. * the block is crosslinked. Remove the reverse mapping and continue.
  379. *
  380. * If there is one rmap record, we can free the block, which removes the
  381. * reverse mapping but doesn't add the block to the free space. Our repair
  382. * strategy is to hope the other metadata objects crosslinked on this block
  383. * will be rebuilt (atop different blocks), thereby removing all the cross
  384. * links.
  385. *
  386. * If there are no rmap records at all, we also free the block. If the btree
  387. * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
  388. * supposed to be a rmap record and everything is ok. For other btrees there
  389. * had to have been an rmap entry for the block to have ended up on @bitmap,
  390. * so if it's gone now there's something wrong and the fs will shut down.
  391. *
  392. * Note: If there are multiple rmap records with only the same rmap owner as
  393. * the btree we're trying to rebuild and the block is indeed owned by another
  394. * data structure with the same rmap owner, then the block will be in sublist
  395. * and therefore doesn't need disposal. If there are multiple rmap records
  396. * with only the same rmap owner but the block is not owned by something with
  397. * the same rmap owner, the block will be freed.
  398. *
  399. * The caller is responsible for locking the AG headers for the entire rebuild
  400. * operation so that nothing else can sneak in and change the AG state while
  401. * we're not looking. We also assume that the caller already invalidated any
  402. * buffers associated with @bitmap.
  403. */
  404. /*
  405. * Invalidate buffers for per-AG btree blocks we're dumping. This function
  406. * is not intended for use with file data repairs; we have bunmapi for that.
  407. */
  408. int
  409. xrep_invalidate_blocks(
  410. struct xfs_scrub *sc,
  411. struct xfs_bitmap *bitmap)
  412. {
  413. struct xfs_bitmap_range *bmr;
  414. struct xfs_bitmap_range *n;
  415. struct xfs_buf *bp;
  416. xfs_fsblock_t fsbno;
  417. /*
  418. * For each block in each extent, see if there's an incore buffer for
  419. * exactly that block; if so, invalidate it. The buffer cache only
  420. * lets us look for one buffer at a time, so we have to look one block
  421. * at a time. Avoid invalidating AG headers and post-EOFS blocks
  422. * because we never own those; and if we can't TRYLOCK the buffer we
  423. * assume it's owned by someone else.
  424. */
  425. for_each_xfs_bitmap_block(fsbno, bmr, n, bitmap) {
  426. /* Skip AG headers and post-EOFS blocks */
  427. if (!xfs_verify_fsbno(sc->mp, fsbno))
  428. continue;
  429. bp = xfs_buf_incore(sc->mp->m_ddev_targp,
  430. XFS_FSB_TO_DADDR(sc->mp, fsbno),
  431. XFS_FSB_TO_BB(sc->mp, 1), XBF_TRYLOCK);
  432. if (bp) {
  433. xfs_trans_bjoin(sc->tp, bp);
  434. xfs_trans_binval(sc->tp, bp);
  435. }
  436. }
  437. return 0;
  438. }
  439. /* Ensure the freelist is the correct size. */
  440. int
  441. xrep_fix_freelist(
  442. struct xfs_scrub *sc,
  443. bool can_shrink)
  444. {
  445. struct xfs_alloc_arg args = {0};
  446. args.mp = sc->mp;
  447. args.tp = sc->tp;
  448. args.agno = sc->sa.agno;
  449. args.alignment = 1;
  450. args.pag = sc->sa.pag;
  451. return xfs_alloc_fix_freelist(&args,
  452. can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
  453. }
  454. /*
  455. * Put a block back on the AGFL.
  456. */
  457. STATIC int
  458. xrep_put_freelist(
  459. struct xfs_scrub *sc,
  460. xfs_agblock_t agbno)
  461. {
  462. struct xfs_owner_info oinfo;
  463. int error;
  464. /* Make sure there's space on the freelist. */
  465. error = xrep_fix_freelist(sc, true);
  466. if (error)
  467. return error;
  468. /*
  469. * Since we're "freeing" a lost block onto the AGFL, we have to
  470. * create an rmap for the block prior to merging it or else other
  471. * parts will break.
  472. */
  473. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
  474. error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.agno, agbno, 1,
  475. &oinfo);
  476. if (error)
  477. return error;
  478. /* Put the block on the AGFL. */
  479. error = xfs_alloc_put_freelist(sc->tp, sc->sa.agf_bp, sc->sa.agfl_bp,
  480. agbno, 0);
  481. if (error)
  482. return error;
  483. xfs_extent_busy_insert(sc->tp, sc->sa.agno, agbno, 1,
  484. XFS_EXTENT_BUSY_SKIP_DISCARD);
  485. return 0;
  486. }
  487. /* Dispose of a single block. */
  488. STATIC int
  489. xrep_reap_block(
  490. struct xfs_scrub *sc,
  491. xfs_fsblock_t fsbno,
  492. struct xfs_owner_info *oinfo,
  493. enum xfs_ag_resv_type resv)
  494. {
  495. struct xfs_btree_cur *cur;
  496. struct xfs_buf *agf_bp = NULL;
  497. xfs_agnumber_t agno;
  498. xfs_agblock_t agbno;
  499. bool has_other_rmap;
  500. int error;
  501. agno = XFS_FSB_TO_AGNO(sc->mp, fsbno);
  502. agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
  503. /*
  504. * If we are repairing per-inode metadata, we need to read in the AGF
  505. * buffer. Otherwise, we're repairing a per-AG structure, so reuse
  506. * the AGF buffer that the setup functions already grabbed.
  507. */
  508. if (sc->ip) {
  509. error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf_bp);
  510. if (error)
  511. return error;
  512. if (!agf_bp)
  513. return -ENOMEM;
  514. } else {
  515. agf_bp = sc->sa.agf_bp;
  516. }
  517. cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf_bp, agno);
  518. /* Can we find any other rmappings? */
  519. error = xfs_rmap_has_other_keys(cur, agbno, 1, oinfo, &has_other_rmap);
  520. xfs_btree_del_cursor(cur, error);
  521. if (error)
  522. goto out_free;
  523. /*
  524. * If there are other rmappings, this block is cross linked and must
  525. * not be freed. Remove the reverse mapping and move on. Otherwise,
  526. * we were the only owner of the block, so free the extent, which will
  527. * also remove the rmap.
  528. *
  529. * XXX: XFS doesn't support detecting the case where a single block
  530. * metadata structure is crosslinked with a multi-block structure
  531. * because the buffer cache doesn't detect aliasing problems, so we
  532. * can't fix 100% of crosslinking problems (yet). The verifiers will
  533. * blow on writeout, the filesystem will shut down, and the admin gets
  534. * to run xfs_repair.
  535. */
  536. if (has_other_rmap)
  537. error = xfs_rmap_free(sc->tp, agf_bp, agno, agbno, 1, oinfo);
  538. else if (resv == XFS_AG_RESV_AGFL)
  539. error = xrep_put_freelist(sc, agbno);
  540. else
  541. error = xfs_free_extent(sc->tp, fsbno, 1, oinfo, resv);
  542. if (agf_bp != sc->sa.agf_bp)
  543. xfs_trans_brelse(sc->tp, agf_bp);
  544. if (error)
  545. return error;
  546. if (sc->ip)
  547. return xfs_trans_roll_inode(&sc->tp, sc->ip);
  548. return xrep_roll_ag_trans(sc);
  549. out_free:
  550. if (agf_bp != sc->sa.agf_bp)
  551. xfs_trans_brelse(sc->tp, agf_bp);
  552. return error;
  553. }
  554. /* Dispose of every block of every extent in the bitmap. */
  555. int
  556. xrep_reap_extents(
  557. struct xfs_scrub *sc,
  558. struct xfs_bitmap *bitmap,
  559. struct xfs_owner_info *oinfo,
  560. enum xfs_ag_resv_type type)
  561. {
  562. struct xfs_bitmap_range *bmr;
  563. struct xfs_bitmap_range *n;
  564. xfs_fsblock_t fsbno;
  565. int error = 0;
  566. ASSERT(xfs_sb_version_hasrmapbt(&sc->mp->m_sb));
  567. for_each_xfs_bitmap_block(fsbno, bmr, n, bitmap) {
  568. ASSERT(sc->ip != NULL ||
  569. XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.agno);
  570. trace_xrep_dispose_btree_extent(sc->mp,
  571. XFS_FSB_TO_AGNO(sc->mp, fsbno),
  572. XFS_FSB_TO_AGBNO(sc->mp, fsbno), 1);
  573. error = xrep_reap_block(sc, fsbno, oinfo, type);
  574. if (error)
  575. goto out;
  576. }
  577. out:
  578. xfs_bitmap_destroy(bitmap);
  579. return error;
  580. }
  581. /*
  582. * Finding per-AG Btree Roots for AGF/AGI Reconstruction
  583. *
  584. * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
  585. * the AG headers by using the rmap data to rummage through the AG looking for
  586. * btree roots. This is not guaranteed to work if the AG is heavily damaged
  587. * or the rmap data are corrupt.
  588. *
  589. * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
  590. * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
  591. * AGI is being rebuilt. It must maintain these locks until it's safe for
  592. * other threads to change the btrees' shapes. The caller provides
  593. * information about the btrees to look for by passing in an array of
  594. * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
  595. * The (root, height) fields will be set on return if anything is found. The
  596. * last element of the array should have a NULL buf_ops to mark the end of the
  597. * array.
  598. *
  599. * For every rmapbt record matching any of the rmap owners in btree_info,
  600. * read each block referenced by the rmap record. If the block is a btree
  601. * block from this filesystem matching any of the magic numbers and has a
  602. * level higher than what we've already seen, remember the block and the
  603. * height of the tree required to have such a block. When the call completes,
  604. * we return the highest block we've found for each btree description; those
  605. * should be the roots.
  606. */
  607. struct xrep_findroot {
  608. struct xfs_scrub *sc;
  609. struct xfs_buf *agfl_bp;
  610. struct xfs_agf *agf;
  611. struct xrep_find_ag_btree *btree_info;
  612. };
  613. /* See if our block is in the AGFL. */
  614. STATIC int
  615. xrep_findroot_agfl_walk(
  616. struct xfs_mount *mp,
  617. xfs_agblock_t bno,
  618. void *priv)
  619. {
  620. xfs_agblock_t *agbno = priv;
  621. return (*agbno == bno) ? XFS_BTREE_QUERY_RANGE_ABORT : 0;
  622. }
  623. /* Does this block match the btree information passed in? */
  624. STATIC int
  625. xrep_findroot_block(
  626. struct xrep_findroot *ri,
  627. struct xrep_find_ag_btree *fab,
  628. uint64_t owner,
  629. xfs_agblock_t agbno,
  630. bool *found_it)
  631. {
  632. struct xfs_mount *mp = ri->sc->mp;
  633. struct xfs_buf *bp;
  634. struct xfs_btree_block *btblock;
  635. xfs_daddr_t daddr;
  636. int error;
  637. daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.agno, agbno);
  638. /*
  639. * Blocks in the AGFL have stale contents that might just happen to
  640. * have a matching magic and uuid. We don't want to pull these blocks
  641. * in as part of a tree root, so we have to filter out the AGFL stuff
  642. * here. If the AGFL looks insane we'll just refuse to repair.
  643. */
  644. if (owner == XFS_RMAP_OWN_AG) {
  645. error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
  646. xrep_findroot_agfl_walk, &agbno);
  647. if (error == XFS_BTREE_QUERY_RANGE_ABORT)
  648. return 0;
  649. if (error)
  650. return error;
  651. }
  652. error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
  653. mp->m_bsize, 0, &bp, NULL);
  654. if (error)
  655. return error;
  656. /*
  657. * Does this look like a block matching our fs and higher than any
  658. * other block we've found so far? If so, reattach buffer verifiers
  659. * so the AIL won't complain if the buffer is also dirty.
  660. */
  661. btblock = XFS_BUF_TO_BLOCK(bp);
  662. if (be32_to_cpu(btblock->bb_magic) != fab->magic)
  663. goto out;
  664. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  665. !uuid_equal(&btblock->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
  666. goto out;
  667. bp->b_ops = fab->buf_ops;
  668. /* Ignore this block if it's lower in the tree than we've seen. */
  669. if (fab->root != NULLAGBLOCK &&
  670. xfs_btree_get_level(btblock) < fab->height)
  671. goto out;
  672. /* Make sure we pass the verifiers. */
  673. bp->b_ops->verify_read(bp);
  674. if (bp->b_error)
  675. goto out;
  676. fab->root = agbno;
  677. fab->height = xfs_btree_get_level(btblock) + 1;
  678. *found_it = true;
  679. trace_xrep_findroot_block(mp, ri->sc->sa.agno, agbno,
  680. be32_to_cpu(btblock->bb_magic), fab->height - 1);
  681. out:
  682. xfs_trans_brelse(ri->sc->tp, bp);
  683. return error;
  684. }
  685. /*
  686. * Do any of the blocks in this rmap record match one of the btrees we're
  687. * looking for?
  688. */
  689. STATIC int
  690. xrep_findroot_rmap(
  691. struct xfs_btree_cur *cur,
  692. struct xfs_rmap_irec *rec,
  693. void *priv)
  694. {
  695. struct xrep_findroot *ri = priv;
  696. struct xrep_find_ag_btree *fab;
  697. xfs_agblock_t b;
  698. bool found_it;
  699. int error = 0;
  700. /* Ignore anything that isn't AG metadata. */
  701. if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
  702. return 0;
  703. /* Otherwise scan each block + btree type. */
  704. for (b = 0; b < rec->rm_blockcount; b++) {
  705. found_it = false;
  706. for (fab = ri->btree_info; fab->buf_ops; fab++) {
  707. if (rec->rm_owner != fab->rmap_owner)
  708. continue;
  709. error = xrep_findroot_block(ri, fab,
  710. rec->rm_owner, rec->rm_startblock + b,
  711. &found_it);
  712. if (error)
  713. return error;
  714. if (found_it)
  715. break;
  716. }
  717. }
  718. return 0;
  719. }
  720. /* Find the roots of the per-AG btrees described in btree_info. */
  721. int
  722. xrep_find_ag_btree_roots(
  723. struct xfs_scrub *sc,
  724. struct xfs_buf *agf_bp,
  725. struct xrep_find_ag_btree *btree_info,
  726. struct xfs_buf *agfl_bp)
  727. {
  728. struct xfs_mount *mp = sc->mp;
  729. struct xrep_findroot ri;
  730. struct xrep_find_ag_btree *fab;
  731. struct xfs_btree_cur *cur;
  732. int error;
  733. ASSERT(xfs_buf_islocked(agf_bp));
  734. ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
  735. ri.sc = sc;
  736. ri.btree_info = btree_info;
  737. ri.agf = XFS_BUF_TO_AGF(agf_bp);
  738. ri.agfl_bp = agfl_bp;
  739. for (fab = btree_info; fab->buf_ops; fab++) {
  740. ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
  741. ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
  742. fab->root = NULLAGBLOCK;
  743. fab->height = 0;
  744. }
  745. cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
  746. error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
  747. xfs_btree_del_cursor(cur, error);
  748. return error;
  749. }
  750. /* Force a quotacheck the next time we mount. */
  751. void
  752. xrep_force_quotacheck(
  753. struct xfs_scrub *sc,
  754. uint dqtype)
  755. {
  756. uint flag;
  757. flag = xfs_quota_chkd_flag(dqtype);
  758. if (!(flag & sc->mp->m_qflags))
  759. return;
  760. sc->mp->m_qflags &= ~flag;
  761. spin_lock(&sc->mp->m_sb_lock);
  762. sc->mp->m_sb.sb_qflags &= ~flag;
  763. spin_unlock(&sc->mp->m_sb_lock);
  764. xfs_log_sb(sc->tp);
  765. }
  766. /*
  767. * Attach dquots to this inode, or schedule quotacheck to fix them.
  768. *
  769. * This function ensures that the appropriate dquots are attached to an inode.
  770. * We cannot allow the dquot code to allocate an on-disk dquot block here
  771. * because we're already in transaction context with the inode locked. The
  772. * on-disk dquot should already exist anyway. If the quota code signals
  773. * corruption or missing quota information, schedule quotacheck, which will
  774. * repair corruptions in the quota metadata.
  775. */
  776. int
  777. xrep_ino_dqattach(
  778. struct xfs_scrub *sc)
  779. {
  780. int error;
  781. error = xfs_qm_dqattach_locked(sc->ip, false);
  782. switch (error) {
  783. case -EFSBADCRC:
  784. case -EFSCORRUPTED:
  785. case -ENOENT:
  786. xfs_err_ratelimited(sc->mp,
  787. "inode %llu repair encountered quota error %d, quotacheck forced.",
  788. (unsigned long long)sc->ip->i_ino, error);
  789. if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
  790. xrep_force_quotacheck(sc, XFS_DQ_USER);
  791. if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
  792. xrep_force_quotacheck(sc, XFS_DQ_GROUP);
  793. if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
  794. xrep_force_quotacheck(sc, XFS_DQ_PROJ);
  795. /* fall through */
  796. case -ESRCH:
  797. error = 0;
  798. break;
  799. default:
  800. break;
  801. }
  802. return error;
  803. }