xfs_dquot_buf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_quota.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_qm.h"
  18. #include "xfs_error.h"
  19. #include "xfs_cksum.h"
  20. #include "xfs_trace.h"
  21. int
  22. xfs_calc_dquots_per_chunk(
  23. unsigned int nbblks) /* basic block units */
  24. {
  25. ASSERT(nbblks > 0);
  26. return BBTOB(nbblks) / sizeof(xfs_dqblk_t);
  27. }
  28. /*
  29. * Do some primitive error checking on ondisk dquot data structures.
  30. *
  31. * The xfs_dqblk structure /contains/ the xfs_disk_dquot structure;
  32. * we verify them separately because at some points we have only the
  33. * smaller xfs_disk_dquot structure available.
  34. */
  35. xfs_failaddr_t
  36. xfs_dquot_verify(
  37. struct xfs_mount *mp,
  38. xfs_disk_dquot_t *ddq,
  39. xfs_dqid_t id,
  40. uint type) /* used only during quotacheck */
  41. {
  42. /*
  43. * We can encounter an uninitialized dquot buffer for 2 reasons:
  44. * 1. If we crash while deleting the quotainode(s), and those blks got
  45. * used for user data. This is because we take the path of regular
  46. * file deletion; however, the size field of quotainodes is never
  47. * updated, so all the tricks that we play in itruncate_finish
  48. * don't quite matter.
  49. *
  50. * 2. We don't play the quota buffers when there's a quotaoff logitem.
  51. * But the allocation will be replayed so we'll end up with an
  52. * uninitialized quota block.
  53. *
  54. * This is all fine; things are still consistent, and we haven't lost
  55. * any quota information. Just don't complain about bad dquot blks.
  56. */
  57. if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC))
  58. return __this_address;
  59. if (ddq->d_version != XFS_DQUOT_VERSION)
  60. return __this_address;
  61. if (type && ddq->d_flags != type)
  62. return __this_address;
  63. if (ddq->d_flags != XFS_DQ_USER &&
  64. ddq->d_flags != XFS_DQ_PROJ &&
  65. ddq->d_flags != XFS_DQ_GROUP)
  66. return __this_address;
  67. if (id != -1 && id != be32_to_cpu(ddq->d_id))
  68. return __this_address;
  69. if (!ddq->d_id)
  70. return NULL;
  71. if (ddq->d_blk_softlimit &&
  72. be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) &&
  73. !ddq->d_btimer)
  74. return __this_address;
  75. if (ddq->d_ino_softlimit &&
  76. be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) &&
  77. !ddq->d_itimer)
  78. return __this_address;
  79. if (ddq->d_rtb_softlimit &&
  80. be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) &&
  81. !ddq->d_rtbtimer)
  82. return __this_address;
  83. return NULL;
  84. }
  85. xfs_failaddr_t
  86. xfs_dqblk_verify(
  87. struct xfs_mount *mp,
  88. struct xfs_dqblk *dqb,
  89. xfs_dqid_t id,
  90. uint type) /* used only during quotacheck */
  91. {
  92. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  93. !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid))
  94. return __this_address;
  95. return xfs_dquot_verify(mp, &dqb->dd_diskdq, id, type);
  96. }
  97. /*
  98. * Do some primitive error checking on ondisk dquot data structures.
  99. */
  100. int
  101. xfs_dqblk_repair(
  102. struct xfs_mount *mp,
  103. struct xfs_dqblk *dqb,
  104. xfs_dqid_t id,
  105. uint type)
  106. {
  107. /*
  108. * Typically, a repair is only requested by quotacheck.
  109. */
  110. ASSERT(id != -1);
  111. memset(dqb, 0, sizeof(xfs_dqblk_t));
  112. dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  113. dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  114. dqb->dd_diskdq.d_flags = type;
  115. dqb->dd_diskdq.d_id = cpu_to_be32(id);
  116. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  117. uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
  118. xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
  119. XFS_DQUOT_CRC_OFF);
  120. }
  121. return 0;
  122. }
  123. STATIC bool
  124. xfs_dquot_buf_verify_crc(
  125. struct xfs_mount *mp,
  126. struct xfs_buf *bp,
  127. bool readahead)
  128. {
  129. struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
  130. int ndquots;
  131. int i;
  132. if (!xfs_sb_version_hascrc(&mp->m_sb))
  133. return true;
  134. /*
  135. * if we are in log recovery, the quota subsystem has not been
  136. * initialised so we have no quotainfo structure. In that case, we need
  137. * to manually calculate the number of dquots in the buffer.
  138. */
  139. if (mp->m_quotainfo)
  140. ndquots = mp->m_quotainfo->qi_dqperchunk;
  141. else
  142. ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
  143. for (i = 0; i < ndquots; i++, d++) {
  144. if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
  145. XFS_DQUOT_CRC_OFF)) {
  146. if (!readahead)
  147. xfs_buf_verifier_error(bp, -EFSBADCRC, __func__,
  148. d, sizeof(*d), __this_address);
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. STATIC xfs_failaddr_t
  155. xfs_dquot_buf_verify(
  156. struct xfs_mount *mp,
  157. struct xfs_buf *bp,
  158. bool readahead)
  159. {
  160. struct xfs_dqblk *dqb = bp->b_addr;
  161. xfs_failaddr_t fa;
  162. xfs_dqid_t id = 0;
  163. int ndquots;
  164. int i;
  165. /*
  166. * if we are in log recovery, the quota subsystem has not been
  167. * initialised so we have no quotainfo structure. In that case, we need
  168. * to manually calculate the number of dquots in the buffer.
  169. */
  170. if (mp->m_quotainfo)
  171. ndquots = mp->m_quotainfo->qi_dqperchunk;
  172. else
  173. ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
  174. /*
  175. * On the first read of the buffer, verify that each dquot is valid.
  176. * We don't know what the id of the dquot is supposed to be, just that
  177. * they should be increasing monotonically within the buffer. If the
  178. * first id is corrupt, then it will fail on the second dquot in the
  179. * buffer so corruptions could point to the wrong dquot in this case.
  180. */
  181. for (i = 0; i < ndquots; i++) {
  182. struct xfs_disk_dquot *ddq;
  183. ddq = &dqb[i].dd_diskdq;
  184. if (i == 0)
  185. id = be32_to_cpu(ddq->d_id);
  186. fa = xfs_dqblk_verify(mp, &dqb[i], id + i, 0);
  187. if (fa) {
  188. if (!readahead)
  189. xfs_buf_verifier_error(bp, -EFSCORRUPTED,
  190. __func__, &dqb[i],
  191. sizeof(struct xfs_dqblk), fa);
  192. return fa;
  193. }
  194. }
  195. return NULL;
  196. }
  197. static xfs_failaddr_t
  198. xfs_dquot_buf_verify_struct(
  199. struct xfs_buf *bp)
  200. {
  201. struct xfs_mount *mp = bp->b_target->bt_mount;
  202. return xfs_dquot_buf_verify(mp, bp, false);
  203. }
  204. static void
  205. xfs_dquot_buf_read_verify(
  206. struct xfs_buf *bp)
  207. {
  208. struct xfs_mount *mp = bp->b_target->bt_mount;
  209. if (!xfs_dquot_buf_verify_crc(mp, bp, false))
  210. return;
  211. xfs_dquot_buf_verify(mp, bp, false);
  212. }
  213. /*
  214. * readahead errors are silent and simply leave the buffer as !done so a real
  215. * read will then be run with the xfs_dquot_buf_ops verifier. See
  216. * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
  217. * reporting the failure.
  218. */
  219. static void
  220. xfs_dquot_buf_readahead_verify(
  221. struct xfs_buf *bp)
  222. {
  223. struct xfs_mount *mp = bp->b_target->bt_mount;
  224. if (!xfs_dquot_buf_verify_crc(mp, bp, true) ||
  225. xfs_dquot_buf_verify(mp, bp, true) != NULL) {
  226. xfs_buf_ioerror(bp, -EIO);
  227. bp->b_flags &= ~XBF_DONE;
  228. }
  229. }
  230. /*
  231. * we don't calculate the CRC here as that is done when the dquot is flushed to
  232. * the buffer after the update is done. This ensures that the dquot in the
  233. * buffer always has an up-to-date CRC value.
  234. */
  235. static void
  236. xfs_dquot_buf_write_verify(
  237. struct xfs_buf *bp)
  238. {
  239. struct xfs_mount *mp = bp->b_target->bt_mount;
  240. xfs_dquot_buf_verify(mp, bp, false);
  241. }
  242. const struct xfs_buf_ops xfs_dquot_buf_ops = {
  243. .name = "xfs_dquot",
  244. .verify_read = xfs_dquot_buf_read_verify,
  245. .verify_write = xfs_dquot_buf_write_verify,
  246. .verify_struct = xfs_dquot_buf_verify_struct,
  247. };
  248. const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
  249. .name = "xfs_dquot_ra",
  250. .verify_read = xfs_dquot_buf_readahead_verify,
  251. .verify_write = xfs_dquot_buf_write_verify,
  252. };