rmap.c 5.9 KB

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