rtsummary.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.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_inode.h"
  14. #include "xfs_log_format.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_rtbitmap.h"
  17. #include "xfs_bit.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_sb.h"
  20. #include "xfs_exchmaps.h"
  21. #include "scrub/scrub.h"
  22. #include "scrub/common.h"
  23. #include "scrub/trace.h"
  24. #include "scrub/xfile.h"
  25. #include "scrub/repair.h"
  26. #include "scrub/tempexch.h"
  27. #include "scrub/rtsummary.h"
  28. /*
  29. * Realtime Summary
  30. * ================
  31. *
  32. * We check the realtime summary by scanning the realtime bitmap file to create
  33. * a new summary file incore, and then we compare the computed version against
  34. * the ondisk version. We use the 'xfile' functionality to store this
  35. * (potentially large) amount of data in pageable memory.
  36. */
  37. /* Set us up to check the rtsummary file. */
  38. int
  39. xchk_setup_rtsummary(
  40. struct xfs_scrub *sc)
  41. {
  42. struct xfs_mount *mp = sc->mp;
  43. char *descr;
  44. struct xchk_rtsummary *rts;
  45. int error;
  46. rts = kvzalloc(struct_size(rts, words, mp->m_blockwsize),
  47. XCHK_GFP_FLAGS);
  48. if (!rts)
  49. return -ENOMEM;
  50. sc->buf = rts;
  51. if (xchk_could_repair(sc)) {
  52. error = xrep_setup_rtsummary(sc, rts);
  53. if (error)
  54. return error;
  55. }
  56. /*
  57. * Create an xfile to construct a new rtsummary file. The xfile allows
  58. * us to avoid pinning kernel memory for this purpose.
  59. */
  60. descr = xchk_xfile_descr(sc, "realtime summary file");
  61. error = xfile_create(descr, XFS_FSB_TO_B(mp, mp->m_rsumblocks),
  62. &sc->xfile);
  63. kfree(descr);
  64. if (error)
  65. return error;
  66. error = xchk_trans_alloc(sc, rts->resblks);
  67. if (error)
  68. return error;
  69. error = xchk_install_live_inode(sc, mp->m_rsumip);
  70. if (error)
  71. return error;
  72. error = xchk_ino_dqattach(sc);
  73. if (error)
  74. return error;
  75. /*
  76. * Locking order requires us to take the rtbitmap first. We must be
  77. * careful to unlock it ourselves when we are done with the rtbitmap
  78. * file since the scrub infrastructure won't do that for us. Only
  79. * then we can lock the rtsummary inode.
  80. */
  81. xfs_ilock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
  82. xchk_ilock(sc, XFS_ILOCK_EXCL | XFS_ILOCK_RTSUM);
  83. /*
  84. * Now that we've locked the rtbitmap and rtsummary, we can't race with
  85. * growfsrt trying to expand the summary or change the size of the rt
  86. * volume. Hence it is safe to compute and check the geometry values.
  87. */
  88. if (mp->m_sb.sb_rblocks) {
  89. int rextslog;
  90. rts->rextents = xfs_rtb_to_rtx(mp, mp->m_sb.sb_rblocks);
  91. rextslog = xfs_compute_rextslog(rts->rextents);
  92. rts->rsumlevels = rextslog + 1;
  93. rts->rbmblocks = xfs_rtbitmap_blockcount(mp, rts->rextents);
  94. rts->rsumblocks = xfs_rtsummary_blockcount(mp, rts->rsumlevels,
  95. rts->rbmblocks);
  96. }
  97. return 0;
  98. }
  99. /* Helper functions to record suminfo words in an xfile. */
  100. static inline int
  101. xfsum_load(
  102. struct xfs_scrub *sc,
  103. xfs_rtsumoff_t sumoff,
  104. union xfs_suminfo_raw *rawinfo)
  105. {
  106. return xfile_load(sc->xfile, rawinfo,
  107. sizeof(union xfs_suminfo_raw),
  108. sumoff << XFS_WORDLOG);
  109. }
  110. static inline int
  111. xfsum_store(
  112. struct xfs_scrub *sc,
  113. xfs_rtsumoff_t sumoff,
  114. const union xfs_suminfo_raw rawinfo)
  115. {
  116. return xfile_store(sc->xfile, &rawinfo,
  117. sizeof(union xfs_suminfo_raw),
  118. sumoff << XFS_WORDLOG);
  119. }
  120. inline int
  121. xfsum_copyout(
  122. struct xfs_scrub *sc,
  123. xfs_rtsumoff_t sumoff,
  124. union xfs_suminfo_raw *rawinfo,
  125. unsigned int nr_words)
  126. {
  127. return xfile_load(sc->xfile, rawinfo, nr_words << XFS_WORDLOG,
  128. sumoff << XFS_WORDLOG);
  129. }
  130. static inline xfs_suminfo_t
  131. xchk_rtsum_inc(
  132. struct xfs_mount *mp,
  133. union xfs_suminfo_raw *v)
  134. {
  135. v->old += 1;
  136. return v->old;
  137. }
  138. /* Update the summary file to reflect the free extent that we've accumulated. */
  139. STATIC int
  140. xchk_rtsum_record_free(
  141. struct xfs_mount *mp,
  142. struct xfs_trans *tp,
  143. const struct xfs_rtalloc_rec *rec,
  144. void *priv)
  145. {
  146. struct xfs_scrub *sc = priv;
  147. xfs_fileoff_t rbmoff;
  148. xfs_rtblock_t rtbno;
  149. xfs_filblks_t rtlen;
  150. xfs_rtsumoff_t offs;
  151. unsigned int lenlog;
  152. union xfs_suminfo_raw v;
  153. xfs_suminfo_t value;
  154. int error = 0;
  155. if (xchk_should_terminate(sc, &error))
  156. return error;
  157. /* Compute the relevant location in the rtsum file. */
  158. rbmoff = xfs_rtx_to_rbmblock(mp, rec->ar_startext);
  159. lenlog = xfs_highbit64(rec->ar_extcount);
  160. offs = xfs_rtsumoffs(mp, lenlog, rbmoff);
  161. rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
  162. rtlen = xfs_rtx_to_rtb(mp, rec->ar_extcount);
  163. if (!xfs_verify_rtbext(mp, rtbno, rtlen)) {
  164. xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
  165. return -EFSCORRUPTED;
  166. }
  167. /* Bump the summary count. */
  168. error = xfsum_load(sc, offs, &v);
  169. if (error)
  170. return error;
  171. value = xchk_rtsum_inc(sc->mp, &v);
  172. trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount,
  173. lenlog, offs, value);
  174. return xfsum_store(sc, offs, v);
  175. }
  176. /* Compute the realtime summary from the realtime bitmap. */
  177. STATIC int
  178. xchk_rtsum_compute(
  179. struct xfs_scrub *sc)
  180. {
  181. struct xfs_mount *mp = sc->mp;
  182. unsigned long long rtbmp_blocks;
  183. /* If the bitmap size doesn't match the computed size, bail. */
  184. rtbmp_blocks = xfs_rtbitmap_blockcount(mp, mp->m_sb.sb_rextents);
  185. if (XFS_FSB_TO_B(mp, rtbmp_blocks) != mp->m_rbmip->i_disk_size)
  186. return -EFSCORRUPTED;
  187. return xfs_rtalloc_query_all(sc->mp, sc->tp, xchk_rtsum_record_free,
  188. sc);
  189. }
  190. /* Compare the rtsummary file against the one we computed. */
  191. STATIC int
  192. xchk_rtsum_compare(
  193. struct xfs_scrub *sc)
  194. {
  195. struct xfs_bmbt_irec map;
  196. struct xfs_iext_cursor icur;
  197. struct xfs_mount *mp = sc->mp;
  198. struct xfs_inode *ip = sc->ip;
  199. struct xchk_rtsummary *rts = sc->buf;
  200. xfs_fileoff_t off = 0;
  201. xfs_fileoff_t endoff;
  202. xfs_rtsumoff_t sumoff = 0;
  203. int error = 0;
  204. rts->args.mp = sc->mp;
  205. rts->args.tp = sc->tp;
  206. /* Mappings may not cross or lie beyond EOF. */
  207. endoff = XFS_B_TO_FSB(mp, ip->i_disk_size);
  208. if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) {
  209. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff);
  210. return 0;
  211. }
  212. while (off < endoff) {
  213. int nmap = 1;
  214. if (xchk_should_terminate(sc, &error))
  215. return error;
  216. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  217. return 0;
  218. /* Make sure we have a written extent. */
  219. error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap,
  220. XFS_DATA_FORK);
  221. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
  222. return error;
  223. if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) {
  224. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
  225. return 0;
  226. }
  227. off += map.br_blockcount;
  228. }
  229. for (off = 0; off < endoff; off++) {
  230. union xfs_suminfo_raw *ondisk_info;
  231. /* Read a block's worth of ondisk rtsummary file. */
  232. error = xfs_rtsummary_read_buf(&rts->args, off);
  233. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error))
  234. return error;
  235. /* Read a block's worth of computed rtsummary file. */
  236. error = xfsum_copyout(sc, sumoff, rts->words, mp->m_blockwsize);
  237. if (error) {
  238. xfs_rtbuf_cache_relse(&rts->args);
  239. return error;
  240. }
  241. ondisk_info = xfs_rsumblock_infoptr(&rts->args, 0);
  242. if (memcmp(ondisk_info, rts->words,
  243. mp->m_blockwsize << XFS_WORDLOG) != 0) {
  244. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off);
  245. xfs_rtbuf_cache_relse(&rts->args);
  246. return error;
  247. }
  248. xfs_rtbuf_cache_relse(&rts->args);
  249. sumoff += mp->m_blockwsize;
  250. }
  251. return 0;
  252. }
  253. /* Scrub the realtime summary. */
  254. int
  255. xchk_rtsummary(
  256. struct xfs_scrub *sc)
  257. {
  258. struct xfs_mount *mp = sc->mp;
  259. struct xchk_rtsummary *rts = sc->buf;
  260. int error = 0;
  261. /* Is sb_rextents correct? */
  262. if (mp->m_sb.sb_rextents != rts->rextents) {
  263. xchk_ino_set_corrupt(sc, mp->m_rbmip->i_ino);
  264. goto out_rbm;
  265. }
  266. /* Is m_rsumlevels correct? */
  267. if (mp->m_rsumlevels != rts->rsumlevels) {
  268. xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
  269. goto out_rbm;
  270. }
  271. /* Is m_rsumsize correct? */
  272. if (mp->m_rsumblocks != rts->rsumblocks) {
  273. xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
  274. goto out_rbm;
  275. }
  276. /* The summary file length must be aligned to an fsblock. */
  277. if (mp->m_rsumip->i_disk_size & mp->m_blockmask) {
  278. xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
  279. goto out_rbm;
  280. }
  281. /*
  282. * Is the summary file itself large enough to handle the rt volume?
  283. * growfsrt expands the summary file before updating sb_rextents, so
  284. * the file can be larger than rsumsize.
  285. */
  286. if (mp->m_rsumip->i_disk_size < XFS_FSB_TO_B(mp, rts->rsumblocks)) {
  287. xchk_ino_set_corrupt(sc, mp->m_rsumip->i_ino);
  288. goto out_rbm;
  289. }
  290. /* Invoke the fork scrubber. */
  291. error = xchk_metadata_inode_forks(sc);
  292. if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  293. goto out_rbm;
  294. /* Construct the new summary file from the rtbitmap. */
  295. error = xchk_rtsum_compute(sc);
  296. if (error == -EFSCORRUPTED) {
  297. /*
  298. * EFSCORRUPTED means the rtbitmap is corrupt, which is an xref
  299. * error since we're checking the summary file.
  300. */
  301. xchk_ino_xref_set_corrupt(sc, mp->m_rbmip->i_ino);
  302. error = 0;
  303. goto out_rbm;
  304. }
  305. if (error)
  306. goto out_rbm;
  307. /* Does the computed summary file match the actual rtsummary file? */
  308. error = xchk_rtsum_compare(sc);
  309. out_rbm:
  310. /*
  311. * Unlock the rtbitmap since we're done with it. All other writers of
  312. * the rt free space metadata grab the bitmap and summary ILOCKs in
  313. * that order, so we're still protected against allocation activities
  314. * even if we continue on to the repair function.
  315. */
  316. xfs_iunlock(mp->m_rbmip, XFS_ILOCK_SHARED | XFS_ILOCK_RTBITMAP);
  317. return error;
  318. }