xfs_dquot_buf.c 8.0 KB

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