rmap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_alloc.h"
  19. #include "xfs_ialloc.h"
  20. #include "xfs_rmap.h"
  21. #include "xfs_refcount.h"
  22. #include "scrub/xfs_scrub.h"
  23. #include "scrub/scrub.h"
  24. #include "scrub/common.h"
  25. #include "scrub/btree.h"
  26. #include "scrub/trace.h"
  27. /*
  28. * Set us up to scrub reverse mapping btrees.
  29. */
  30. int
  31. xchk_setup_ag_rmapbt(
  32. struct xfs_scrub *sc,
  33. struct xfs_inode *ip)
  34. {
  35. return xchk_setup_ag_btree(sc, ip, false);
  36. }
  37. /* Reverse-mapping scrubber. */
  38. /* Cross-reference a rmap against the refcount btree. */
  39. STATIC void
  40. xchk_rmapbt_xref_refc(
  41. struct xfs_scrub *sc,
  42. struct xfs_rmap_irec *irec)
  43. {
  44. xfs_agblock_t fbno;
  45. xfs_extlen_t flen;
  46. bool non_inode;
  47. bool is_bmbt;
  48. bool is_attr;
  49. bool is_unwritten;
  50. int error;
  51. if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
  52. return;
  53. non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
  54. is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
  55. is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
  56. is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
  57. /* If this is shared, must be a data fork extent. */
  58. error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
  59. irec->rm_blockcount, &fbno, &flen, false);
  60. if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
  61. return;
  62. if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
  63. xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
  64. }
  65. /* Cross-reference with the other btrees. */
  66. STATIC void
  67. xchk_rmapbt_xref(
  68. struct xfs_scrub *sc,
  69. struct xfs_rmap_irec *irec)
  70. {
  71. xfs_agblock_t agbno = irec->rm_startblock;
  72. xfs_extlen_t len = irec->rm_blockcount;
  73. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  74. return;
  75. xchk_xref_is_used_space(sc, agbno, len);
  76. if (irec->rm_owner == XFS_RMAP_OWN_INODES)
  77. xchk_xref_is_inode_chunk(sc, agbno, len);
  78. else
  79. xchk_xref_is_not_inode_chunk(sc, agbno, len);
  80. if (irec->rm_owner == XFS_RMAP_OWN_COW)
  81. xchk_xref_is_cow_staging(sc, irec->rm_startblock,
  82. irec->rm_blockcount);
  83. else
  84. xchk_rmapbt_xref_refc(sc, irec);
  85. }
  86. /* Scrub an rmapbt record. */
  87. STATIC int
  88. xchk_rmapbt_rec(
  89. struct xchk_btree *bs,
  90. union xfs_btree_rec *rec)
  91. {
  92. struct xfs_mount *mp = bs->cur->bc_mp;
  93. struct xfs_rmap_irec irec;
  94. xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
  95. bool non_inode;
  96. bool is_unwritten;
  97. bool is_bmbt;
  98. bool is_attr;
  99. int error;
  100. error = xfs_rmap_btrec_to_irec(rec, &irec);
  101. if (!xchk_btree_process_error(bs->sc, bs->cur, 0, &error))
  102. goto out;
  103. /* Check extent. */
  104. if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
  105. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  106. if (irec.rm_owner == XFS_RMAP_OWN_FS) {
  107. /*
  108. * xfs_verify_agbno returns false for static fs metadata.
  109. * Since that only exists at the start of the AG, validate
  110. * that by hand.
  111. */
  112. if (irec.rm_startblock != 0 ||
  113. irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
  114. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  115. } else {
  116. /*
  117. * Otherwise we must point somewhere past the static metadata
  118. * but before the end of the FS. Run the regular check.
  119. */
  120. if (!xfs_verify_agbno(mp, agno, irec.rm_startblock) ||
  121. !xfs_verify_agbno(mp, agno, irec.rm_startblock +
  122. irec.rm_blockcount - 1))
  123. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  124. }
  125. /* Check flags. */
  126. non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
  127. is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
  128. is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
  129. is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
  130. if (is_bmbt && irec.rm_offset != 0)
  131. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  132. if (non_inode && irec.rm_offset != 0)
  133. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  134. if (is_unwritten && (is_bmbt || non_inode || is_attr))
  135. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  136. if (non_inode && (is_bmbt || is_unwritten || is_attr))
  137. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  138. if (!non_inode) {
  139. if (!xfs_verify_ino(mp, irec.rm_owner))
  140. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  141. } else {
  142. /* Non-inode owner within the magic values? */
  143. if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
  144. irec.rm_owner > XFS_RMAP_OWN_FS)
  145. xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
  146. }
  147. xchk_rmapbt_xref(bs->sc, &irec);
  148. out:
  149. return error;
  150. }
  151. /* Scrub the rmap btree for some AG. */
  152. int
  153. xchk_rmapbt(
  154. struct xfs_scrub *sc)
  155. {
  156. struct xfs_owner_info oinfo;
  157. xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
  158. return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
  159. &oinfo, NULL);
  160. }
  161. /* xref check that the extent is owned by a given owner */
  162. static inline void
  163. xchk_xref_check_owner(
  164. struct xfs_scrub *sc,
  165. xfs_agblock_t bno,
  166. xfs_extlen_t len,
  167. struct xfs_owner_info *oinfo,
  168. bool should_have_rmap)
  169. {
  170. bool has_rmap;
  171. int error;
  172. if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
  173. return;
  174. error = xfs_rmap_record_exists(sc->sa.rmap_cur, bno, len, oinfo,
  175. &has_rmap);
  176. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  177. return;
  178. if (has_rmap != should_have_rmap)
  179. xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
  180. }
  181. /* xref check that the extent is owned by a given owner */
  182. void
  183. xchk_xref_is_owned_by(
  184. struct xfs_scrub *sc,
  185. xfs_agblock_t bno,
  186. xfs_extlen_t len,
  187. struct xfs_owner_info *oinfo)
  188. {
  189. xchk_xref_check_owner(sc, bno, len, oinfo, true);
  190. }
  191. /* xref check that the extent is not owned by a given owner */
  192. void
  193. xchk_xref_is_not_owned_by(
  194. struct xfs_scrub *sc,
  195. xfs_agblock_t bno,
  196. xfs_extlen_t len,
  197. struct xfs_owner_info *oinfo)
  198. {
  199. xchk_xref_check_owner(sc, bno, len, oinfo, false);
  200. }
  201. /* xref check that the extent has no reverse mapping at all */
  202. void
  203. xchk_xref_has_no_owner(
  204. struct xfs_scrub *sc,
  205. xfs_agblock_t bno,
  206. xfs_extlen_t len)
  207. {
  208. bool has_rmap;
  209. int error;
  210. if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
  211. return;
  212. error = xfs_rmap_has_record(sc->sa.rmap_cur, bno, len, &has_rmap);
  213. if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
  214. return;
  215. if (has_rmap)
  216. xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
  217. }