bmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_btree.h"
  13. #include "xfs_bit.h"
  14. #include "xfs_log_format.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_bmap_btree.h"
  20. #include "xfs_rmap.h"
  21. #include "xfs_rmap_btree.h"
  22. #include "scrub/scrub.h"
  23. #include "scrub/common.h"
  24. #include "scrub/btree.h"
  25. #include "xfs_ag.h"
  26. /* Set us up with an inode's bmap. */
  27. int
  28. xchk_setup_inode_bmap(
  29. struct xfs_scrub *sc)
  30. {
  31. int error;
  32. error = xchk_get_inode(sc);
  33. if (error)
  34. goto out;
  35. sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  36. xfs_ilock(sc->ip, sc->ilock_flags);
  37. /*
  38. * We don't want any ephemeral data fork updates sitting around
  39. * while we inspect block mappings, so wait for directio to finish
  40. * and flush dirty data if we have delalloc reservations.
  41. */
  42. if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
  43. sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
  44. struct address_space *mapping = VFS_I(sc->ip)->i_mapping;
  45. inode_dio_wait(VFS_I(sc->ip));
  46. /*
  47. * Try to flush all incore state to disk before we examine the
  48. * space mappings for the data fork. Leave accumulated errors
  49. * in the mapping for the writer threads to consume.
  50. *
  51. * On ENOSPC or EIO writeback errors, we continue into the
  52. * extent mapping checks because write failures do not
  53. * necessarily imply anything about the correctness of the file
  54. * metadata. The metadata and the file data could be on
  55. * completely separate devices; a media failure might only
  56. * affect a subset of the disk, etc. We can handle delalloc
  57. * extents in the scrubber, so leaving them in memory is fine.
  58. */
  59. error = filemap_fdatawrite(mapping);
  60. if (!error)
  61. error = filemap_fdatawait_keep_errors(mapping);
  62. if (error && (error != -ENOSPC && error != -EIO))
  63. goto out;
  64. }
  65. /* Got the inode, lock it and we're ready to go. */
  66. error = xchk_trans_alloc(sc, 0);
  67. if (error)
  68. goto out;
  69. sc->ilock_flags |= XFS_ILOCK_EXCL;
  70. xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
  71. out:
  72. /* scrub teardown will unlock and release the inode */
  73. return error;
  74. }
  75. /*
  76. * Inode fork block mapping (BMBT) scrubber.
  77. * More complex than the others because we have to scrub
  78. * all the extents regardless of whether or not the fork
  79. * is in btree format.
  80. */
  81. struct xchk_bmap_info {
  82. struct xfs_scrub *sc;
  83. xfs_fileoff_t lastoff;
  84. bool is_rt;
  85. bool is_shared;
  86. bool was_loaded;
  87. int whichfork;
  88. };
  89. /* Look for a corresponding rmap for this irec. */
  90. static inline bool
  91. xchk_bmap_get_rmap(
  92. struct xchk_bmap_info *info,
  93. struct xfs_bmbt_irec *irec,
  94. xfs_agblock_t agbno,
  95. uint64_t owner,
  96. struct xfs_rmap_irec *rmap)
  97. {
  98. xfs_fileoff_t offset;
  99. unsigned int rflags = 0;
  100. int has_rmap;
  101. int error;
  102. if (info->whichfork == XFS_ATTR_FORK)
  103. rflags |= XFS_RMAP_ATTR_FORK;
  104. if (irec->br_state == XFS_EXT_UNWRITTEN)
  105. rflags |= XFS_RMAP_UNWRITTEN;
  106. /*
  107. * CoW staging extents are owned (on disk) by the refcountbt, so
  108. * their rmaps do not have offsets.
  109. */
  110. if (info->whichfork == XFS_COW_FORK)
  111. offset = 0;
  112. else
  113. offset = irec->br_startoff;
  114. /*
  115. * If the caller thinks this could be a shared bmbt extent (IOWs,
  116. * any data fork extent of a reflink inode) then we have to use the
  117. * range rmap lookup to make sure we get the correct owner/offset.
  118. */
  119. if (info->is_shared) {
  120. error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
  121. owner, offset, rflags, rmap, &has_rmap);
  122. } else {
  123. error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno,
  124. owner, offset, rflags, rmap, &has_rmap);
  125. }
  126. if (!xchk_should_check_xref(info->sc, &error, &info->sc->sa.rmap_cur))
  127. return false;
  128. if (!has_rmap)
  129. xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
  130. irec->br_startoff);
  131. return has_rmap;
  132. }
  133. /* Make sure that we have rmapbt records for this extent. */
  134. STATIC void
  135. xchk_bmap_xref_rmap(
  136. struct xchk_bmap_info *info,
  137. struct xfs_bmbt_irec *irec,
  138. xfs_agblock_t agbno)
  139. {
  140. struct xfs_rmap_irec rmap;
  141. unsigned long long rmap_end;
  142. uint64_t owner;
  143. if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
  144. return;
  145. if (info->whichfork == XFS_COW_FORK)
  146. owner = XFS_RMAP_OWN_COW;
  147. else
  148. owner = info->sc->ip->i_ino;
  149. /* Find the rmap record for this irec. */
  150. if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
  151. return;
  152. /* Check the rmap. */
  153. rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
  154. if (rmap.rm_startblock > agbno ||
  155. agbno + irec->br_blockcount > rmap_end)
  156. xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
  157. irec->br_startoff);
  158. /*
  159. * Check the logical offsets if applicable. CoW staging extents
  160. * don't track logical offsets since the mappings only exist in
  161. * memory.
  162. */
  163. if (info->whichfork != XFS_COW_FORK) {
  164. rmap_end = (unsigned long long)rmap.rm_offset +
  165. rmap.rm_blockcount;
  166. if (rmap.rm_offset > irec->br_startoff ||
  167. irec->br_startoff + irec->br_blockcount > rmap_end)
  168. xchk_fblock_xref_set_corrupt(info->sc,
  169. info->whichfork, irec->br_startoff);
  170. }
  171. if (rmap.rm_owner != owner)
  172. xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
  173. irec->br_startoff);
  174. /*
  175. * Check for discrepancies between the unwritten flag in the irec and
  176. * the rmap. Note that the (in-memory) CoW fork distinguishes between
  177. * unwritten and written extents, but we don't track that in the rmap
  178. * records because the blocks are owned (on-disk) by the refcountbt,
  179. * which doesn't track unwritten state.
  180. */
  181. if (owner != XFS_RMAP_OWN_COW &&
  182. !!(irec->br_state == XFS_EXT_UNWRITTEN) !=
  183. !!(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
  184. xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
  185. irec->br_startoff);
  186. if (!!(info->whichfork == XFS_ATTR_FORK) !=
  187. !!(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
  188. xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
  189. irec->br_startoff);
  190. if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
  191. xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
  192. irec->br_startoff);
  193. }
  194. /* Cross-reference a single rtdev extent record. */
  195. STATIC void
  196. xchk_bmap_rt_iextent_xref(
  197. struct xfs_inode *ip,
  198. struct xchk_bmap_info *info,
  199. struct xfs_bmbt_irec *irec)
  200. {
  201. xchk_xref_is_used_rt_space(info->sc, irec->br_startblock,
  202. irec->br_blockcount);
  203. }
  204. /* Cross-reference a single datadev extent record. */
  205. STATIC void
  206. xchk_bmap_iextent_xref(
  207. struct xfs_inode *ip,
  208. struct xchk_bmap_info *info,
  209. struct xfs_bmbt_irec *irec)
  210. {
  211. struct xfs_mount *mp = info->sc->mp;
  212. xfs_agnumber_t agno;
  213. xfs_agblock_t agbno;
  214. xfs_extlen_t len;
  215. int error;
  216. agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
  217. agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
  218. len = irec->br_blockcount;
  219. error = xchk_ag_init_existing(info->sc, agno, &info->sc->sa);
  220. if (!xchk_fblock_process_error(info->sc, info->whichfork,
  221. irec->br_startoff, &error))
  222. goto out_free;
  223. xchk_xref_is_used_space(info->sc, agbno, len);
  224. xchk_xref_is_not_inode_chunk(info->sc, agbno, len);
  225. xchk_bmap_xref_rmap(info, irec, agbno);
  226. switch (info->whichfork) {
  227. case XFS_DATA_FORK:
  228. if (xfs_is_reflink_inode(info->sc->ip))
  229. break;
  230. fallthrough;
  231. case XFS_ATTR_FORK:
  232. xchk_xref_is_not_shared(info->sc, agbno,
  233. irec->br_blockcount);
  234. break;
  235. case XFS_COW_FORK:
  236. xchk_xref_is_cow_staging(info->sc, agbno,
  237. irec->br_blockcount);
  238. break;
  239. }
  240. out_free:
  241. xchk_ag_free(info->sc, &info->sc->sa);
  242. }
  243. /*
  244. * Directories and attr forks should never have blocks that can't be addressed
  245. * by a xfs_dablk_t.
  246. */
  247. STATIC void
  248. xchk_bmap_dirattr_extent(
  249. struct xfs_inode *ip,
  250. struct xchk_bmap_info *info,
  251. struct xfs_bmbt_irec *irec)
  252. {
  253. struct xfs_mount *mp = ip->i_mount;
  254. xfs_fileoff_t off;
  255. if (!S_ISDIR(VFS_I(ip)->i_mode) && info->whichfork != XFS_ATTR_FORK)
  256. return;
  257. if (!xfs_verify_dablk(mp, irec->br_startoff))
  258. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  259. irec->br_startoff);
  260. off = irec->br_startoff + irec->br_blockcount - 1;
  261. if (!xfs_verify_dablk(mp, off))
  262. xchk_fblock_set_corrupt(info->sc, info->whichfork, off);
  263. }
  264. /* Scrub a single extent record. */
  265. STATIC int
  266. xchk_bmap_iextent(
  267. struct xfs_inode *ip,
  268. struct xchk_bmap_info *info,
  269. struct xfs_bmbt_irec *irec)
  270. {
  271. struct xfs_mount *mp = info->sc->mp;
  272. int error = 0;
  273. /*
  274. * Check for out-of-order extents. This record could have come
  275. * from the incore list, for which there is no ordering check.
  276. */
  277. if (irec->br_startoff < info->lastoff)
  278. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  279. irec->br_startoff);
  280. if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
  281. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  282. irec->br_startoff);
  283. xchk_bmap_dirattr_extent(ip, info, irec);
  284. /* There should never be a "hole" extent in either extent list. */
  285. if (irec->br_startblock == HOLESTARTBLOCK)
  286. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  287. irec->br_startoff);
  288. /*
  289. * Check for delalloc extents. We never iterate the ones in the
  290. * in-core extent scan, and we should never see these in the bmbt.
  291. */
  292. if (isnullstartblock(irec->br_startblock))
  293. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  294. irec->br_startoff);
  295. /* Make sure the extent points to a valid place. */
  296. if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN)
  297. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  298. irec->br_startoff);
  299. if (info->is_rt &&
  300. !xfs_verify_rtext(mp, irec->br_startblock, irec->br_blockcount))
  301. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  302. irec->br_startoff);
  303. if (!info->is_rt &&
  304. !xfs_verify_fsbext(mp, irec->br_startblock, irec->br_blockcount))
  305. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  306. irec->br_startoff);
  307. /* We don't allow unwritten extents on attr forks. */
  308. if (irec->br_state == XFS_EXT_UNWRITTEN &&
  309. info->whichfork == XFS_ATTR_FORK)
  310. xchk_fblock_set_corrupt(info->sc, info->whichfork,
  311. irec->br_startoff);
  312. if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  313. return 0;
  314. if (info->is_rt)
  315. xchk_bmap_rt_iextent_xref(ip, info, irec);
  316. else
  317. xchk_bmap_iextent_xref(ip, info, irec);
  318. info->lastoff = irec->br_startoff + irec->br_blockcount;
  319. return error;
  320. }
  321. /* Scrub a bmbt record. */
  322. STATIC int
  323. xchk_bmapbt_rec(
  324. struct xchk_btree *bs,
  325. const union xfs_btree_rec *rec)
  326. {
  327. struct xfs_bmbt_irec irec;
  328. struct xfs_bmbt_irec iext_irec;
  329. struct xfs_iext_cursor icur;
  330. struct xchk_bmap_info *info = bs->private;
  331. struct xfs_inode *ip = bs->cur->bc_ino.ip;
  332. struct xfs_buf *bp = NULL;
  333. struct xfs_btree_block *block;
  334. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, info->whichfork);
  335. uint64_t owner;
  336. int i;
  337. /*
  338. * Check the owners of the btree blocks up to the level below
  339. * the root since the verifiers don't do that.
  340. */
  341. if (xfs_has_crc(bs->cur->bc_mp) &&
  342. bs->cur->bc_levels[0].ptr == 1) {
  343. for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
  344. block = xfs_btree_get_block(bs->cur, i, &bp);
  345. owner = be64_to_cpu(block->bb_u.l.bb_owner);
  346. if (owner != ip->i_ino)
  347. xchk_fblock_set_corrupt(bs->sc,
  348. info->whichfork, 0);
  349. }
  350. }
  351. /*
  352. * Check that the incore extent tree contains an extent that matches
  353. * this one exactly. We validate those cached bmaps later, so we don't
  354. * need to check them here. If the incore extent tree was just loaded
  355. * from disk by the scrubber, we assume that its contents match what's
  356. * on disk (we still hold the ILOCK) and skip the equivalence check.
  357. */
  358. if (!info->was_loaded)
  359. return 0;
  360. xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
  361. if (!xfs_iext_lookup_extent(ip, ifp, irec.br_startoff, &icur,
  362. &iext_irec) ||
  363. irec.br_startoff != iext_irec.br_startoff ||
  364. irec.br_startblock != iext_irec.br_startblock ||
  365. irec.br_blockcount != iext_irec.br_blockcount ||
  366. irec.br_state != iext_irec.br_state)
  367. xchk_fblock_set_corrupt(bs->sc, info->whichfork,
  368. irec.br_startoff);
  369. return 0;
  370. }
  371. /* Scan the btree records. */
  372. STATIC int
  373. xchk_bmap_btree(
  374. struct xfs_scrub *sc,
  375. int whichfork,
  376. struct xchk_bmap_info *info)
  377. {
  378. struct xfs_owner_info oinfo;
  379. struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, whichfork);
  380. struct xfs_mount *mp = sc->mp;
  381. struct xfs_inode *ip = sc->ip;
  382. struct xfs_btree_cur *cur;
  383. int error;
  384. /* Load the incore bmap cache if it's not loaded. */
  385. info->was_loaded = !xfs_need_iread_extents(ifp);
  386. error = xfs_iread_extents(sc->tp, ip, whichfork);
  387. if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
  388. goto out;
  389. /* Check the btree structure. */
  390. cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
  391. xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
  392. error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info);
  393. xfs_btree_del_cursor(cur, error);
  394. out:
  395. return error;
  396. }
  397. struct xchk_bmap_check_rmap_info {
  398. struct xfs_scrub *sc;
  399. int whichfork;
  400. struct xfs_iext_cursor icur;
  401. };
  402. /* Can we find bmaps that fit this rmap? */
  403. STATIC int
  404. xchk_bmap_check_rmap(
  405. struct xfs_btree_cur *cur,
  406. const struct xfs_rmap_irec *rec,
  407. void *priv)
  408. {
  409. struct xfs_bmbt_irec irec;
  410. struct xfs_rmap_irec check_rec;
  411. struct xchk_bmap_check_rmap_info *sbcri = priv;
  412. struct xfs_ifork *ifp;
  413. struct xfs_scrub *sc = sbcri->sc;
  414. bool have_map;
  415. /* Is this even the right fork? */
  416. if (rec->rm_owner != sc->ip->i_ino)
  417. return 0;
  418. if ((sbcri->whichfork == XFS_ATTR_FORK) ^
  419. !!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
  420. return 0;
  421. if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
  422. return 0;
  423. /* Now look up the bmbt record. */
  424. ifp = xfs_ifork_ptr(sc->ip, sbcri->whichfork);
  425. if (!ifp) {
  426. xchk_fblock_set_corrupt(sc, sbcri->whichfork,
  427. rec->rm_offset);
  428. goto out;
  429. }
  430. have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
  431. &sbcri->icur, &irec);
  432. if (!have_map)
  433. xchk_fblock_set_corrupt(sc, sbcri->whichfork,
  434. rec->rm_offset);
  435. /*
  436. * bmap extent record lengths are constrained to 2^21 blocks in length
  437. * because of space constraints in the on-disk metadata structure.
  438. * However, rmap extent record lengths are constrained only by AG
  439. * length, so we have to loop through the bmbt to make sure that the
  440. * entire rmap is covered by bmbt records.
  441. */
  442. check_rec = *rec;
  443. while (have_map) {
  444. if (irec.br_startoff != check_rec.rm_offset)
  445. xchk_fblock_set_corrupt(sc, sbcri->whichfork,
  446. check_rec.rm_offset);
  447. if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
  448. cur->bc_ag.pag->pag_agno,
  449. check_rec.rm_startblock))
  450. xchk_fblock_set_corrupt(sc, sbcri->whichfork,
  451. check_rec.rm_offset);
  452. if (irec.br_blockcount > check_rec.rm_blockcount)
  453. xchk_fblock_set_corrupt(sc, sbcri->whichfork,
  454. check_rec.rm_offset);
  455. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  456. break;
  457. check_rec.rm_startblock += irec.br_blockcount;
  458. check_rec.rm_offset += irec.br_blockcount;
  459. check_rec.rm_blockcount -= irec.br_blockcount;
  460. if (check_rec.rm_blockcount == 0)
  461. break;
  462. have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
  463. if (!have_map)
  464. xchk_fblock_set_corrupt(sc, sbcri->whichfork,
  465. check_rec.rm_offset);
  466. }
  467. out:
  468. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  469. return -ECANCELED;
  470. return 0;
  471. }
  472. /* Make sure each rmap has a corresponding bmbt entry. */
  473. STATIC int
  474. xchk_bmap_check_ag_rmaps(
  475. struct xfs_scrub *sc,
  476. int whichfork,
  477. struct xfs_perag *pag)
  478. {
  479. struct xchk_bmap_check_rmap_info sbcri;
  480. struct xfs_btree_cur *cur;
  481. struct xfs_buf *agf;
  482. int error;
  483. error = xfs_alloc_read_agf(pag, sc->tp, 0, &agf);
  484. if (error)
  485. return error;
  486. cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, pag);
  487. sbcri.sc = sc;
  488. sbcri.whichfork = whichfork;
  489. error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri);
  490. if (error == -ECANCELED)
  491. error = 0;
  492. xfs_btree_del_cursor(cur, error);
  493. xfs_trans_brelse(sc->tp, agf);
  494. return error;
  495. }
  496. /* Make sure each rmap has a corresponding bmbt entry. */
  497. STATIC int
  498. xchk_bmap_check_rmaps(
  499. struct xfs_scrub *sc,
  500. int whichfork)
  501. {
  502. struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, whichfork);
  503. struct xfs_perag *pag;
  504. xfs_agnumber_t agno;
  505. bool zero_size;
  506. int error;
  507. if (!xfs_has_rmapbt(sc->mp) ||
  508. whichfork == XFS_COW_FORK ||
  509. (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  510. return 0;
  511. /* Don't support realtime rmap checks yet. */
  512. if (XFS_IS_REALTIME_INODE(sc->ip) && whichfork == XFS_DATA_FORK)
  513. return 0;
  514. ASSERT(xfs_ifork_ptr(sc->ip, whichfork) != NULL);
  515. /*
  516. * Only do this for complex maps that are in btree format, or for
  517. * situations where we would seem to have a size but zero extents.
  518. * The inode repair code can zap broken iforks, which means we have
  519. * to flag this bmap as corrupt if there are rmaps that need to be
  520. * reattached.
  521. */
  522. if (whichfork == XFS_DATA_FORK)
  523. zero_size = i_size_read(VFS_I(sc->ip)) == 0;
  524. else
  525. zero_size = false;
  526. if (ifp->if_format != XFS_DINODE_FMT_BTREE &&
  527. (zero_size || ifp->if_nextents > 0))
  528. return 0;
  529. for_each_perag(sc->mp, agno, pag) {
  530. error = xchk_bmap_check_ag_rmaps(sc, whichfork, pag);
  531. if (error)
  532. break;
  533. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  534. break;
  535. }
  536. if (pag)
  537. xfs_perag_put(pag);
  538. return error;
  539. }
  540. /*
  541. * Scrub an inode fork's block mappings.
  542. *
  543. * First we scan every record in every btree block, if applicable.
  544. * Then we unconditionally scan the incore extent cache.
  545. */
  546. STATIC int
  547. xchk_bmap(
  548. struct xfs_scrub *sc,
  549. int whichfork)
  550. {
  551. struct xfs_bmbt_irec irec;
  552. struct xchk_bmap_info info = { NULL };
  553. struct xfs_mount *mp = sc->mp;
  554. struct xfs_inode *ip = sc->ip;
  555. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  556. xfs_fileoff_t endoff;
  557. struct xfs_iext_cursor icur;
  558. int error = 0;
  559. /* Non-existent forks can be ignored. */
  560. if (!ifp)
  561. goto out;
  562. info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
  563. info.whichfork = whichfork;
  564. info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
  565. info.sc = sc;
  566. switch (whichfork) {
  567. case XFS_COW_FORK:
  568. /* No CoW forks on non-reflink inodes/filesystems. */
  569. if (!xfs_is_reflink_inode(ip)) {
  570. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  571. goto out;
  572. }
  573. break;
  574. case XFS_ATTR_FORK:
  575. if (!xfs_has_attr(mp) && !xfs_has_attr2(mp))
  576. xchk_ino_set_corrupt(sc, sc->ip->i_ino);
  577. break;
  578. default:
  579. ASSERT(whichfork == XFS_DATA_FORK);
  580. break;
  581. }
  582. /* Check the fork values */
  583. switch (ifp->if_format) {
  584. case XFS_DINODE_FMT_UUID:
  585. case XFS_DINODE_FMT_DEV:
  586. case XFS_DINODE_FMT_LOCAL:
  587. /* No mappings to check. */
  588. goto out;
  589. case XFS_DINODE_FMT_EXTENTS:
  590. break;
  591. case XFS_DINODE_FMT_BTREE:
  592. if (whichfork == XFS_COW_FORK) {
  593. xchk_fblock_set_corrupt(sc, whichfork, 0);
  594. goto out;
  595. }
  596. error = xchk_bmap_btree(sc, whichfork, &info);
  597. if (error)
  598. goto out;
  599. break;
  600. default:
  601. xchk_fblock_set_corrupt(sc, whichfork, 0);
  602. goto out;
  603. }
  604. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  605. goto out;
  606. /* Find the offset of the last extent in the mapping. */
  607. error = xfs_bmap_last_offset(ip, &endoff, whichfork);
  608. if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
  609. goto out;
  610. /* Scrub extent records. */
  611. info.lastoff = 0;
  612. ifp = xfs_ifork_ptr(ip, whichfork);
  613. for_each_xfs_iext(ifp, &icur, &irec) {
  614. if (xchk_should_terminate(sc, &error) ||
  615. (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  616. goto out;
  617. if (isnullstartblock(irec.br_startblock))
  618. continue;
  619. if (irec.br_startoff >= endoff) {
  620. xchk_fblock_set_corrupt(sc, whichfork,
  621. irec.br_startoff);
  622. goto out;
  623. }
  624. error = xchk_bmap_iextent(ip, &info, &irec);
  625. if (error)
  626. goto out;
  627. }
  628. error = xchk_bmap_check_rmaps(sc, whichfork);
  629. if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error))
  630. goto out;
  631. out:
  632. return error;
  633. }
  634. /* Scrub an inode's data fork. */
  635. int
  636. xchk_bmap_data(
  637. struct xfs_scrub *sc)
  638. {
  639. return xchk_bmap(sc, XFS_DATA_FORK);
  640. }
  641. /* Scrub an inode's attr fork. */
  642. int
  643. xchk_bmap_attr(
  644. struct xfs_scrub *sc)
  645. {
  646. return xchk_bmap(sc, XFS_ATTR_FORK);
  647. }
  648. /* Scrub an inode's CoW fork. */
  649. int
  650. xchk_bmap_cow(
  651. struct xfs_scrub *sc)
  652. {
  653. if (!xfs_is_reflink_inode(sc->ip))
  654. return -ENOENT;
  655. return xchk_bmap(sc, XFS_COW_FORK);
  656. }