repair.c 30 KB

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