rtbitmap.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs_platform.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_log_format.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_rtbitmap.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_bmap.h"
  18. #include "xfs_bit.h"
  19. #include "xfs_rtgroup.h"
  20. #include "xfs_sb.h"
  21. #include "xfs_rmap.h"
  22. #include "xfs_rtrmap_btree.h"
  23. #include "xfs_exchmaps.h"
  24. #include "xfs_zone_alloc.h"
  25. #include "scrub/scrub.h"
  26. #include "scrub/common.h"
  27. #include "scrub/repair.h"
  28. #include "scrub/tempexch.h"
  29. #include "scrub/rtbitmap.h"
  30. #include "scrub/btree.h"
  31. /* Set us up with the realtime metadata locked. */
  32. int
  33. xchk_setup_rtbitmap(
  34. struct xfs_scrub *sc)
  35. {
  36. struct xfs_mount *mp = sc->mp;
  37. struct xchk_rtbitmap *rtb;
  38. int error;
  39. if (xchk_need_intent_drain(sc))
  40. xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
  41. rtb = kzalloc_flex(*rtb, words, xchk_rtbitmap_wordcnt(sc),
  42. XCHK_GFP_FLAGS);
  43. if (!rtb)
  44. return -ENOMEM;
  45. sc->buf = rtb;
  46. rtb->sc = sc;
  47. error = xchk_rtgroup_init(sc, sc->sm->sm_agno, &sc->sr);
  48. if (error)
  49. return error;
  50. if (xchk_could_repair(sc)) {
  51. error = xrep_setup_rtbitmap(sc, rtb);
  52. if (error)
  53. return error;
  54. }
  55. error = xchk_trans_alloc(sc, rtb->resblks);
  56. if (error)
  57. return error;
  58. error = xchk_install_live_inode(sc, rtg_bitmap(sc->sr.rtg));
  59. if (error)
  60. return error;
  61. error = xchk_ino_dqattach(sc);
  62. if (error)
  63. return error;
  64. error = xchk_rtgroup_lock(sc, &sc->sr, XCHK_RTGLOCK_ALL);
  65. if (error)
  66. return error;
  67. /*
  68. * Now that we've locked the rtbitmap, we can't race with growfsrt
  69. * trying to expand the bitmap or change the size of the rt volume.
  70. * Hence it is safe to compute and check the geometry values.
  71. */
  72. if (mp->m_sb.sb_rblocks) {
  73. rtb->rextents = xfs_blen_to_rtbxlen(mp, mp->m_sb.sb_rblocks);
  74. rtb->rextslog = xfs_compute_rextslog(rtb->rextents);
  75. rtb->rbmblocks = xfs_rtbitmap_blockcount(mp);
  76. }
  77. return 0;
  78. }
  79. /* Per-rtgroup bitmap contents. */
  80. /* Cross-reference rtbitmap entries with other metadata. */
  81. STATIC void
  82. xchk_rtbitmap_xref(
  83. struct xchk_rtbitmap *rtb,
  84. xfs_rtblock_t startblock,
  85. xfs_rtblock_t blockcount)
  86. {
  87. struct xfs_scrub *sc = rtb->sc;
  88. xfs_rgblock_t rgbno = xfs_rtb_to_rgbno(sc->mp, startblock);
  89. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  90. return;
  91. if (!sc->sr.rmap_cur)
  92. return;
  93. xchk_xref_has_no_rt_owner(sc, rgbno, blockcount);
  94. xchk_xref_is_not_rt_shared(sc, rgbno, blockcount);
  95. xchk_xref_is_not_rt_cow_staging(sc, rgbno, blockcount);
  96. if (rtb->next_free_rgbno < rgbno)
  97. xchk_xref_has_rt_owner(sc, rtb->next_free_rgbno,
  98. rgbno - rtb->next_free_rgbno);
  99. rtb->next_free_rgbno = rgbno + blockcount;
  100. }
  101. /* Scrub a free extent record from the realtime bitmap. */
  102. STATIC int
  103. xchk_rtbitmap_rec(
  104. struct xfs_rtgroup *rtg,
  105. struct xfs_trans *tp,
  106. const struct xfs_rtalloc_rec *rec,
  107. void *priv)
  108. {
  109. struct xchk_rtbitmap *rtb = priv;
  110. struct xfs_scrub *sc = rtb->sc;
  111. xfs_rtblock_t startblock;
  112. xfs_filblks_t blockcount;
  113. startblock = xfs_rtx_to_rtb(rtg, rec->ar_startext);
  114. blockcount = xfs_rtxlen_to_extlen(rtg_mount(rtg), rec->ar_extcount);
  115. if (!xfs_verify_rtbext(rtg_mount(rtg), startblock, blockcount))
  116. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, 0);
  117. xchk_rtbitmap_xref(rtb, startblock, blockcount);
  118. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  119. return -ECANCELED;
  120. return 0;
  121. }
  122. /* Make sure the entire rtbitmap file is mapped with written extents. */
  123. STATIC int
  124. xchk_rtbitmap_check_extents(
  125. struct xfs_scrub *sc)
  126. {
  127. struct xfs_bmbt_irec map;
  128. struct xfs_iext_cursor icur;
  129. struct xfs_mount *mp = sc->mp;
  130. struct xfs_inode *ip = sc->ip;
  131. xfs_fileoff_t off = 0;
  132. xfs_fileoff_t endoff;
  133. int error = 0;
  134. /* Mappings may not cross or lie beyond EOF. */
  135. endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
  136. if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
  137. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
  138. return 0;
  139. }
  140. while (off < endoff) {
  141. int nmap = 1;
  142. if (xchk_should_terminate(sc, &error) ||
  143. (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  144. break;
  145. /* Make sure we have a written extent. */
  146. error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
  147. XFS_DATA_FORK);
  148. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
  149. break;
  150. if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
  151. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
  152. break;
  153. }
  154. off += map.br_blockcount;
  155. }
  156. return error;
  157. }
  158. /* Scrub this group's realtime bitmap. */
  159. int
  160. xchk_rtbitmap(
  161. struct xfs_scrub *sc)
  162. {
  163. struct xfs_mount *mp = sc->mp;
  164. struct xfs_rtgroup *rtg = sc->sr.rtg;
  165. struct xfs_inode *rbmip = rtg_bitmap(rtg);
  166. struct xchk_rtbitmap *rtb = sc->buf;
  167. xfs_rgblock_t last_rgbno;
  168. int error;
  169. /* Is sb_rextents correct? */
  170. if (mp->m_sb.sb_rextents != rtb->rextents) {
  171. xchk_ino_set_corrupt(sc, rbmip->i_ino);
  172. return 0;
  173. }
  174. /* Is sb_rextslog correct? */
  175. if (mp->m_sb.sb_rextslog != rtb->rextslog) {
  176. xchk_ino_set_corrupt(sc, rbmip->i_ino);
  177. return 0;
  178. }
  179. /*
  180. * Is sb_rbmblocks large enough to handle the current rt volume? In no
  181. * case can we exceed 4bn bitmap blocks since the super field is a u32.
  182. */
  183. if (rtb->rbmblocks > U32_MAX) {
  184. xchk_ino_set_corrupt(sc, rbmip->i_ino);
  185. return 0;
  186. }
  187. if (mp->m_sb.sb_rbmblocks != rtb->rbmblocks) {
  188. xchk_ino_set_corrupt(sc, rbmip->i_ino);
  189. return 0;
  190. }
  191. /* The bitmap file length must be aligned to an fsblock. */
  192. if (rbmip->i_disk_size & mp->m_blockmask) {
  193. xchk_ino_set_corrupt(sc, rbmip->i_ino);
  194. return 0;
  195. }
  196. /*
  197. * Is the bitmap file itself large enough to handle the rt volume?
  198. * growfsrt expands the bitmap file before updating sb_rextents, so the
  199. * file can be larger than sb_rbmblocks.
  200. */
  201. if (rbmip->i_disk_size < XFS_FSB_TO_B(mp, rtb->rbmblocks)) {
  202. xchk_ino_set_corrupt(sc, rbmip->i_ino);
  203. return 0;
  204. }
  205. /* Invoke the fork scrubber. */
  206. error = xchk_metadata_inode_forks(sc);
  207. if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  208. return error;
  209. error = xchk_rtbitmap_check_extents(sc);
  210. if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  211. return error;
  212. rtb->next_free_rgbno = 0;
  213. error = xfs_rtalloc_query_all(rtg, sc->tp, xchk_rtbitmap_rec, rtb);
  214. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, 0, &error))
  215. return error;
  216. /*
  217. * Check that the are rmappings for all rt extents between the end of
  218. * the last free extent we saw and the last possible extent in the rt
  219. * group.
  220. */
  221. last_rgbno = rtg->rtg_extents * mp->m_sb.sb_rextsize - 1;
  222. if (rtb->next_free_rgbno < last_rgbno)
  223. xchk_xref_has_rt_owner(sc, rtb->next_free_rgbno,
  224. last_rgbno - rtb->next_free_rgbno);
  225. return 0;
  226. }
  227. /* xref check that the extent is not free in the rtbitmap */
  228. void
  229. xchk_xref_is_used_rt_space(
  230. struct xfs_scrub *sc,
  231. xfs_rtblock_t rtbno,
  232. xfs_extlen_t len)
  233. {
  234. struct xfs_rtgroup *rtg = sc->sr.rtg;
  235. xfs_rtxnum_t startext;
  236. xfs_rtxnum_t endext;
  237. bool is_free;
  238. int error;
  239. if (xchk_skip_xref(sc->sm))
  240. return;
  241. if (xfs_has_zoned(sc->mp)) {
  242. if (!xfs_zone_rgbno_is_valid(rtg,
  243. xfs_rtb_to_rgbno(sc->mp, rtbno) + len - 1))
  244. xchk_ino_xref_set_corrupt(sc, rtg_rmap(rtg)->i_ino);
  245. return;
  246. }
  247. startext = xfs_rtb_to_rtx(sc->mp, rtbno);
  248. endext = xfs_rtb_to_rtx(sc->mp, rtbno + len - 1);
  249. error = xfs_rtalloc_extent_is_free(rtg, sc->tp, startext,
  250. endext - startext + 1, &is_free);
  251. if (!xchk_should_check_xref(sc, &error, NULL))
  252. return;
  253. if (is_free)
  254. xchk_ino_xref_set_corrupt(sc, rtg_bitmap(rtg)->i_ino);
  255. }