bmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 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_inode_fork.h"
  20. #include "xfs_alloc.h"
  21. #include "xfs_rtalloc.h"
  22. #include "xfs_bmap.h"
  23. #include "xfs_bmap_util.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_rmap.h"
  26. #include "xfs_rmap_btree.h"
  27. #include "xfs_refcount.h"
  28. #include "scrub/xfs_scrub.h"
  29. #include "scrub/scrub.h"
  30. #include "scrub/common.h"
  31. #include "scrub/btree.h"
  32. #include "scrub/trace.h"
  33. /* Set us up with an inode's bmap. */
  34. int
  35. xfs_scrub_setup_inode_bmap(
  36. struct xfs_scrub_context *sc,
  37. struct xfs_inode *ip)
  38. {
  39. int error;
  40. error = xfs_scrub_get_inode(sc, ip);
  41. if (error)
  42. goto out;
  43. sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  44. xfs_ilock(sc->ip, sc->ilock_flags);
  45. /*
  46. * We don't want any ephemeral data fork updates sitting around
  47. * while we inspect block mappings, so wait for directio to finish
  48. * and flush dirty data if we have delalloc reservations.
  49. */
  50. if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
  51. sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
  52. inode_dio_wait(VFS_I(sc->ip));
  53. error = filemap_write_and_wait(VFS_I(sc->ip)->i_mapping);
  54. if (error)
  55. goto out;
  56. }
  57. /* Got the inode, lock it and we're ready to go. */
  58. error = xfs_scrub_trans_alloc(sc, 0);
  59. if (error)
  60. goto out;
  61. sc->ilock_flags |= XFS_ILOCK_EXCL;
  62. xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
  63. out:
  64. /* scrub teardown will unlock and release the inode */
  65. return error;
  66. }
  67. /*
  68. * Inode fork block mapping (BMBT) scrubber.
  69. * More complex than the others because we have to scrub
  70. * all the extents regardless of whether or not the fork
  71. * is in btree format.
  72. */
  73. struct xfs_scrub_bmap_info {
  74. struct xfs_scrub_context *sc;
  75. xfs_fileoff_t lastoff;
  76. bool is_rt;
  77. bool is_shared;
  78. int whichfork;
  79. };
  80. /* Look for a corresponding rmap for this irec. */
  81. static inline bool
  82. xfs_scrub_bmap_get_rmap(
  83. struct xfs_scrub_bmap_info *info,
  84. struct xfs_bmbt_irec *irec,
  85. xfs_agblock_t agbno,
  86. uint64_t owner,
  87. struct xfs_rmap_irec *rmap)
  88. {
  89. xfs_fileoff_t offset;
  90. unsigned int rflags = 0;
  91. int has_rmap;
  92. int error;
  93. if (info->whichfork == XFS_ATTR_FORK)
  94. rflags |= XFS_RMAP_ATTR_FORK;
  95. /*
  96. * CoW staging extents are owned (on disk) by the refcountbt, so
  97. * their rmaps do not have offsets.
  98. */
  99. if (info->whichfork == XFS_COW_FORK)
  100. offset = 0;
  101. else
  102. offset = irec->br_startoff;
  103. /*
  104. * If the caller thinks this could be a shared bmbt extent (IOWs,
  105. * any data fork extent of a reflink inode) then we have to use the
  106. * range rmap lookup to make sure we get the correct owner/offset.
  107. */
  108. if (info->is_shared) {
  109. error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
  110. owner, offset, rflags, rmap, &has_rmap);
  111. if (!xfs_scrub_should_check_xref(info->sc, &error,
  112. &info->sc->sa.rmap_cur))
  113. return false;
  114. goto out;
  115. }
  116. /*
  117. * Otherwise, use the (faster) regular lookup.
  118. */
  119. error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno, 0, owner,
  120. offset, rflags, &has_rmap);
  121. if (!xfs_scrub_should_check_xref(info->sc, &error,
  122. &info->sc->sa.rmap_cur))
  123. return false;
  124. if (!has_rmap)
  125. goto out;
  126. error = xfs_rmap_get_rec(info->sc->sa.rmap_cur, rmap, &has_rmap);
  127. if (!xfs_scrub_should_check_xref(info->sc, &error,
  128. &info->sc->sa.rmap_cur))
  129. return false;
  130. out:
  131. if (!has_rmap)
  132. xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
  133. irec->br_startoff);
  134. return has_rmap;
  135. }
  136. /* Make sure that we have rmapbt records for this extent. */
  137. STATIC void
  138. xfs_scrub_bmap_xref_rmap(
  139. struct xfs_scrub_bmap_info *info,
  140. struct xfs_bmbt_irec *irec,
  141. xfs_agblock_t agbno)
  142. {
  143. struct xfs_rmap_irec rmap;
  144. unsigned long long rmap_end;
  145. uint64_t owner;
  146. if (!info->sc->sa.rmap_cur || xfs_scrub_skip_xref(info->sc->sm))
  147. return;
  148. if (info->whichfork == XFS_COW_FORK)
  149. owner = XFS_RMAP_OWN_COW;
  150. else
  151. owner = info->sc->ip->i_ino;
  152. /* Find the rmap record for this irec. */
  153. if (!xfs_scrub_bmap_get_rmap(info, irec, agbno, owner, &rmap))
  154. return;
  155. /* Check the rmap. */
  156. rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
  157. if (rmap.rm_startblock > agbno ||
  158. agbno + irec->br_blockcount > rmap_end)
  159. xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
  160. irec->br_startoff);
  161. /*
  162. * Check the logical offsets if applicable. CoW staging extents
  163. * don't track logical offsets since the mappings only exist in
  164. * memory.
  165. */
  166. if (info->whichfork != XFS_COW_FORK) {
  167. rmap_end = (unsigned long long)rmap.rm_offset +
  168. rmap.rm_blockcount;
  169. if (rmap.rm_offset > irec->br_startoff ||
  170. irec->br_startoff + irec->br_blockcount > rmap_end)
  171. xfs_scrub_fblock_xref_set_corrupt(info->sc,
  172. info->whichfork, irec->br_startoff);
  173. }
  174. if (rmap.rm_owner != owner)
  175. xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
  176. irec->br_startoff);
  177. /*
  178. * Check for discrepancies between the unwritten flag in the irec and
  179. * the rmap. Note that the (in-memory) CoW fork distinguishes between
  180. * unwritten and written extents, but we don't track that in the rmap
  181. * records because the blocks are owned (on-disk) by the refcountbt,
  182. * which doesn't track unwritten state.
  183. */
  184. if (owner != XFS_RMAP_OWN_COW &&
  185. irec->br_state == XFS_EXT_UNWRITTEN &&
  186. !(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
  187. xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
  188. irec->br_startoff);
  189. if (info->whichfork == XFS_ATTR_FORK &&
  190. !(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
  191. xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
  192. irec->br_startoff);
  193. if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
  194. xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
  195. irec->br_startoff);
  196. }
  197. /* Cross-reference a single rtdev extent record. */
  198. STATIC void
  199. xfs_scrub_bmap_rt_extent_xref(
  200. struct xfs_scrub_bmap_info *info,
  201. struct xfs_inode *ip,
  202. struct xfs_btree_cur *cur,
  203. struct xfs_bmbt_irec *irec)
  204. {
  205. if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  206. return;
  207. xfs_scrub_xref_is_used_rt_space(info->sc, irec->br_startblock,
  208. irec->br_blockcount);
  209. }
  210. /* Cross-reference a single datadev extent record. */
  211. STATIC void
  212. xfs_scrub_bmap_extent_xref(
  213. struct xfs_scrub_bmap_info *info,
  214. struct xfs_inode *ip,
  215. struct xfs_btree_cur *cur,
  216. struct xfs_bmbt_irec *irec)
  217. {
  218. struct xfs_mount *mp = info->sc->mp;
  219. xfs_agnumber_t agno;
  220. xfs_agblock_t agbno;
  221. xfs_extlen_t len;
  222. int error;
  223. if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  224. return;
  225. agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
  226. agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
  227. len = irec->br_blockcount;
  228. error = xfs_scrub_ag_init(info->sc, agno, &info->sc->sa);
  229. if (!xfs_scrub_fblock_process_error(info->sc, info->whichfork,
  230. irec->br_startoff, &error))
  231. return;
  232. xfs_scrub_xref_is_used_space(info->sc, agbno, len);
  233. xfs_scrub_xref_is_not_inode_chunk(info->sc, agbno, len);
  234. xfs_scrub_bmap_xref_rmap(info, irec, agbno);
  235. switch (info->whichfork) {
  236. case XFS_DATA_FORK:
  237. if (xfs_is_reflink_inode(info->sc->ip))
  238. break;
  239. /* fall through */
  240. case XFS_ATTR_FORK:
  241. xfs_scrub_xref_is_not_shared(info->sc, agbno,
  242. irec->br_blockcount);
  243. break;
  244. case XFS_COW_FORK:
  245. xfs_scrub_xref_is_cow_staging(info->sc, agbno,
  246. irec->br_blockcount);
  247. break;
  248. }
  249. xfs_scrub_ag_free(info->sc, &info->sc->sa);
  250. }
  251. /* Scrub a single extent record. */
  252. STATIC int
  253. xfs_scrub_bmap_extent(
  254. struct xfs_inode *ip,
  255. struct xfs_btree_cur *cur,
  256. struct xfs_scrub_bmap_info *info,
  257. struct xfs_bmbt_irec *irec)
  258. {
  259. struct xfs_mount *mp = info->sc->mp;
  260. struct xfs_buf *bp = NULL;
  261. xfs_filblks_t end;
  262. int error = 0;
  263. if (cur)
  264. xfs_btree_get_block(cur, 0, &bp);
  265. /*
  266. * Check for out-of-order extents. This record could have come
  267. * from the incore list, for which there is no ordering check.
  268. */
  269. if (irec->br_startoff < info->lastoff)
  270. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  271. irec->br_startoff);
  272. /* There should never be a "hole" extent in either extent list. */
  273. if (irec->br_startblock == HOLESTARTBLOCK)
  274. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  275. irec->br_startoff);
  276. /*
  277. * Check for delalloc extents. We never iterate the ones in the
  278. * in-core extent scan, and we should never see these in the bmbt.
  279. */
  280. if (isnullstartblock(irec->br_startblock))
  281. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  282. irec->br_startoff);
  283. /* Make sure the extent points to a valid place. */
  284. if (irec->br_blockcount > MAXEXTLEN)
  285. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  286. irec->br_startoff);
  287. if (irec->br_startblock + irec->br_blockcount <= irec->br_startblock)
  288. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  289. irec->br_startoff);
  290. end = irec->br_startblock + irec->br_blockcount - 1;
  291. if (info->is_rt &&
  292. (!xfs_verify_rtbno(mp, irec->br_startblock) ||
  293. !xfs_verify_rtbno(mp, end)))
  294. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  295. irec->br_startoff);
  296. if (!info->is_rt &&
  297. (!xfs_verify_fsbno(mp, irec->br_startblock) ||
  298. !xfs_verify_fsbno(mp, end) ||
  299. XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
  300. XFS_FSB_TO_AGNO(mp, end)))
  301. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  302. irec->br_startoff);
  303. /* We don't allow unwritten extents on attr forks. */
  304. if (irec->br_state == XFS_EXT_UNWRITTEN &&
  305. info->whichfork == XFS_ATTR_FORK)
  306. xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
  307. irec->br_startoff);
  308. if (info->is_rt)
  309. xfs_scrub_bmap_rt_extent_xref(info, ip, cur, irec);
  310. else
  311. xfs_scrub_bmap_extent_xref(info, ip, cur, irec);
  312. info->lastoff = irec->br_startoff + irec->br_blockcount;
  313. return error;
  314. }
  315. /* Scrub a bmbt record. */
  316. STATIC int
  317. xfs_scrub_bmapbt_rec(
  318. struct xfs_scrub_btree *bs,
  319. union xfs_btree_rec *rec)
  320. {
  321. struct xfs_bmbt_irec irec;
  322. struct xfs_scrub_bmap_info *info = bs->private;
  323. struct xfs_inode *ip = bs->cur->bc_private.b.ip;
  324. struct xfs_buf *bp = NULL;
  325. struct xfs_btree_block *block;
  326. uint64_t owner;
  327. int i;
  328. /*
  329. * Check the owners of the btree blocks up to the level below
  330. * the root since the verifiers don't do that.
  331. */
  332. if (xfs_sb_version_hascrc(&bs->cur->bc_mp->m_sb) &&
  333. bs->cur->bc_ptrs[0] == 1) {
  334. for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
  335. block = xfs_btree_get_block(bs->cur, i, &bp);
  336. owner = be64_to_cpu(block->bb_u.l.bb_owner);
  337. if (owner != ip->i_ino)
  338. xfs_scrub_fblock_set_corrupt(bs->sc,
  339. info->whichfork, 0);
  340. }
  341. }
  342. /* Set up the in-core record and scrub it. */
  343. xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
  344. return xfs_scrub_bmap_extent(ip, bs->cur, info, &irec);
  345. }
  346. /* Scan the btree records. */
  347. STATIC int
  348. xfs_scrub_bmap_btree(
  349. struct xfs_scrub_context *sc,
  350. int whichfork,
  351. struct xfs_scrub_bmap_info *info)
  352. {
  353. struct xfs_owner_info oinfo;
  354. struct xfs_mount *mp = sc->mp;
  355. struct xfs_inode *ip = sc->ip;
  356. struct xfs_btree_cur *cur;
  357. int error;
  358. cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
  359. xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
  360. error = xfs_scrub_btree(sc, cur, xfs_scrub_bmapbt_rec, &oinfo, info);
  361. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR :
  362. XFS_BTREE_NOERROR);
  363. return error;
  364. }
  365. struct xfs_scrub_bmap_check_rmap_info {
  366. struct xfs_scrub_context *sc;
  367. int whichfork;
  368. struct xfs_iext_cursor icur;
  369. };
  370. /* Can we find bmaps that fit this rmap? */
  371. STATIC int
  372. xfs_scrub_bmap_check_rmap(
  373. struct xfs_btree_cur *cur,
  374. struct xfs_rmap_irec *rec,
  375. void *priv)
  376. {
  377. struct xfs_bmbt_irec irec;
  378. struct xfs_scrub_bmap_check_rmap_info *sbcri = priv;
  379. struct xfs_ifork *ifp;
  380. struct xfs_scrub_context *sc = sbcri->sc;
  381. bool have_map;
  382. /* Is this even the right fork? */
  383. if (rec->rm_owner != sc->ip->i_ino)
  384. return 0;
  385. if ((sbcri->whichfork == XFS_ATTR_FORK) ^
  386. !!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
  387. return 0;
  388. if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
  389. return 0;
  390. /* Now look up the bmbt record. */
  391. ifp = XFS_IFORK_PTR(sc->ip, sbcri->whichfork);
  392. if (!ifp) {
  393. xfs_scrub_fblock_set_corrupt(sc, sbcri->whichfork,
  394. rec->rm_offset);
  395. goto out;
  396. }
  397. have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
  398. &sbcri->icur, &irec);
  399. if (!have_map)
  400. xfs_scrub_fblock_set_corrupt(sc, sbcri->whichfork,
  401. rec->rm_offset);
  402. /*
  403. * bmap extent record lengths are constrained to 2^21 blocks in length
  404. * because of space constraints in the on-disk metadata structure.
  405. * However, rmap extent record lengths are constrained only by AG
  406. * length, so we have to loop through the bmbt to make sure that the
  407. * entire rmap is covered by bmbt records.
  408. */
  409. while (have_map) {
  410. if (irec.br_startoff != rec->rm_offset)
  411. xfs_scrub_fblock_set_corrupt(sc, sbcri->whichfork,
  412. rec->rm_offset);
  413. if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
  414. cur->bc_private.a.agno, rec->rm_startblock))
  415. xfs_scrub_fblock_set_corrupt(sc, sbcri->whichfork,
  416. rec->rm_offset);
  417. if (irec.br_blockcount > rec->rm_blockcount)
  418. xfs_scrub_fblock_set_corrupt(sc, sbcri->whichfork,
  419. rec->rm_offset);
  420. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  421. break;
  422. rec->rm_startblock += irec.br_blockcount;
  423. rec->rm_offset += irec.br_blockcount;
  424. rec->rm_blockcount -= irec.br_blockcount;
  425. if (rec->rm_blockcount == 0)
  426. break;
  427. have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
  428. if (!have_map)
  429. xfs_scrub_fblock_set_corrupt(sc, sbcri->whichfork,
  430. rec->rm_offset);
  431. }
  432. out:
  433. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  434. return XFS_BTREE_QUERY_RANGE_ABORT;
  435. return 0;
  436. }
  437. /* Make sure each rmap has a corresponding bmbt entry. */
  438. STATIC int
  439. xfs_scrub_bmap_check_ag_rmaps(
  440. struct xfs_scrub_context *sc,
  441. int whichfork,
  442. xfs_agnumber_t agno)
  443. {
  444. struct xfs_scrub_bmap_check_rmap_info sbcri;
  445. struct xfs_btree_cur *cur;
  446. struct xfs_buf *agf;
  447. int error;
  448. error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf);
  449. if (error)
  450. return error;
  451. cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, agno);
  452. if (!cur) {
  453. error = -ENOMEM;
  454. goto out_agf;
  455. }
  456. sbcri.sc = sc;
  457. sbcri.whichfork = whichfork;
  458. error = xfs_rmap_query_all(cur, xfs_scrub_bmap_check_rmap, &sbcri);
  459. if (error == XFS_BTREE_QUERY_RANGE_ABORT)
  460. error = 0;
  461. xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
  462. out_agf:
  463. xfs_trans_brelse(sc->tp, agf);
  464. return error;
  465. }
  466. /* Make sure each rmap has a corresponding bmbt entry. */
  467. STATIC int
  468. xfs_scrub_bmap_check_rmaps(
  469. struct xfs_scrub_context *sc,
  470. int whichfork)
  471. {
  472. loff_t size;
  473. xfs_agnumber_t agno;
  474. int error;
  475. if (!xfs_sb_version_hasrmapbt(&sc->mp->m_sb) ||
  476. whichfork == XFS_COW_FORK ||
  477. (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  478. return 0;
  479. /* Don't support realtime rmap checks yet. */
  480. if (XFS_IS_REALTIME_INODE(sc->ip) && whichfork == XFS_DATA_FORK)
  481. return 0;
  482. /*
  483. * Only do this for complex maps that are in btree format, or for
  484. * situations where we would seem to have a size but zero extents.
  485. * The inode repair code can zap broken iforks, which means we have
  486. * to flag this bmap as corrupt if there are rmaps that need to be
  487. * reattached.
  488. */
  489. switch (whichfork) {
  490. case XFS_DATA_FORK:
  491. size = i_size_read(VFS_I(sc->ip));
  492. break;
  493. case XFS_ATTR_FORK:
  494. size = XFS_IFORK_Q(sc->ip);
  495. break;
  496. default:
  497. size = 0;
  498. break;
  499. }
  500. if (XFS_IFORK_FORMAT(sc->ip, whichfork) != XFS_DINODE_FMT_BTREE &&
  501. (size == 0 || XFS_IFORK_NEXTENTS(sc->ip, whichfork) > 0))
  502. return 0;
  503. for (agno = 0; agno < sc->mp->m_sb.sb_agcount; agno++) {
  504. error = xfs_scrub_bmap_check_ag_rmaps(sc, whichfork, agno);
  505. if (error)
  506. return error;
  507. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  508. break;
  509. }
  510. return 0;
  511. }
  512. /*
  513. * Scrub an inode fork's block mappings.
  514. *
  515. * First we scan every record in every btree block, if applicable.
  516. * Then we unconditionally scan the incore extent cache.
  517. */
  518. STATIC int
  519. xfs_scrub_bmap(
  520. struct xfs_scrub_context *sc,
  521. int whichfork)
  522. {
  523. struct xfs_bmbt_irec irec;
  524. struct xfs_scrub_bmap_info info = { NULL };
  525. struct xfs_mount *mp = sc->mp;
  526. struct xfs_inode *ip = sc->ip;
  527. struct xfs_ifork *ifp;
  528. xfs_fileoff_t endoff;
  529. struct xfs_iext_cursor icur;
  530. int error = 0;
  531. ifp = XFS_IFORK_PTR(ip, whichfork);
  532. info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
  533. info.whichfork = whichfork;
  534. info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
  535. info.sc = sc;
  536. switch (whichfork) {
  537. case XFS_COW_FORK:
  538. /* Non-existent CoW forks are ignorable. */
  539. if (!ifp)
  540. goto out;
  541. /* No CoW forks on non-reflink inodes/filesystems. */
  542. if (!xfs_is_reflink_inode(ip)) {
  543. xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
  544. goto out;
  545. }
  546. break;
  547. case XFS_ATTR_FORK:
  548. if (!ifp)
  549. goto out_check_rmap;
  550. if (!xfs_sb_version_hasattr(&mp->m_sb) &&
  551. !xfs_sb_version_hasattr2(&mp->m_sb))
  552. xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino);
  553. break;
  554. default:
  555. ASSERT(whichfork == XFS_DATA_FORK);
  556. break;
  557. }
  558. /* Check the fork values */
  559. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  560. case XFS_DINODE_FMT_UUID:
  561. case XFS_DINODE_FMT_DEV:
  562. case XFS_DINODE_FMT_LOCAL:
  563. /* No mappings to check. */
  564. goto out;
  565. case XFS_DINODE_FMT_EXTENTS:
  566. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  567. xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
  568. goto out;
  569. }
  570. break;
  571. case XFS_DINODE_FMT_BTREE:
  572. if (whichfork == XFS_COW_FORK) {
  573. xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
  574. goto out;
  575. }
  576. error = xfs_scrub_bmap_btree(sc, whichfork, &info);
  577. if (error)
  578. goto out;
  579. break;
  580. default:
  581. xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
  582. goto out;
  583. }
  584. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  585. goto out;
  586. /* Now try to scrub the in-memory extent list. */
  587. if (!(ifp->if_flags & XFS_IFEXTENTS)) {
  588. error = xfs_iread_extents(sc->tp, ip, whichfork);
  589. if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
  590. goto out;
  591. }
  592. /* Find the offset of the last extent in the mapping. */
  593. error = xfs_bmap_last_offset(ip, &endoff, whichfork);
  594. if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
  595. goto out;
  596. /* Scrub extent records. */
  597. info.lastoff = 0;
  598. ifp = XFS_IFORK_PTR(ip, whichfork);
  599. for_each_xfs_iext(ifp, &icur, &irec) {
  600. if (xfs_scrub_should_terminate(sc, &error) ||
  601. (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  602. break;
  603. if (isnullstartblock(irec.br_startblock))
  604. continue;
  605. if (irec.br_startoff >= endoff) {
  606. xfs_scrub_fblock_set_corrupt(sc, whichfork,
  607. irec.br_startoff);
  608. goto out;
  609. }
  610. error = xfs_scrub_bmap_extent(ip, NULL, &info, &irec);
  611. if (error)
  612. goto out;
  613. }
  614. out_check_rmap:
  615. error = xfs_scrub_bmap_check_rmaps(sc, whichfork);
  616. if (!xfs_scrub_fblock_xref_process_error(sc, whichfork, 0, &error))
  617. goto out;
  618. out:
  619. return error;
  620. }
  621. /* Scrub an inode's data fork. */
  622. int
  623. xfs_scrub_bmap_data(
  624. struct xfs_scrub_context *sc)
  625. {
  626. return xfs_scrub_bmap(sc, XFS_DATA_FORK);
  627. }
  628. /* Scrub an inode's attr fork. */
  629. int
  630. xfs_scrub_bmap_attr(
  631. struct xfs_scrub_context *sc)
  632. {
  633. return xfs_scrub_bmap(sc, XFS_ATTR_FORK);
  634. }
  635. /* Scrub an inode's CoW fork. */
  636. int
  637. xfs_scrub_bmap_cow(
  638. struct xfs_scrub_context *sc)
  639. {
  640. if (!xfs_is_reflink_inode(sc->ip))
  641. return -ENOENT;
  642. return xfs_scrub_bmap(sc, XFS_COW_FORK);
  643. }