xfs_dquot_buf.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_qm.h"
  32. #include "xfs_error.h"
  33. #include "xfs_cksum.h"
  34. #include "xfs_trace.h"
  35. int
  36. xfs_calc_dquots_per_chunk(
  37. unsigned int nbblks) /* basic block units */
  38. {
  39. unsigned int ndquots;
  40. ASSERT(nbblks > 0);
  41. ndquots = BBTOB(nbblks);
  42. do_div(ndquots, sizeof(xfs_dqblk_t));
  43. return ndquots;
  44. }
  45. /*
  46. * Do some primitive error checking on ondisk dquot data structures.
  47. */
  48. int
  49. xfs_dqcheck(
  50. struct xfs_mount *mp,
  51. xfs_disk_dquot_t *ddq,
  52. xfs_dqid_t id,
  53. uint type, /* used only when IO_dorepair is true */
  54. uint flags,
  55. char *str)
  56. {
  57. xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
  58. int errs = 0;
  59. /*
  60. * We can encounter an uninitialized dquot buffer for 2 reasons:
  61. * 1. If we crash while deleting the quotainode(s), and those blks got
  62. * used for user data. This is because we take the path of regular
  63. * file deletion; however, the size field of quotainodes is never
  64. * updated, so all the tricks that we play in itruncate_finish
  65. * don't quite matter.
  66. *
  67. * 2. We don't play the quota buffers when there's a quotaoff logitem.
  68. * But the allocation will be replayed so we'll end up with an
  69. * uninitialized quota block.
  70. *
  71. * This is all fine; things are still consistent, and we haven't lost
  72. * any quota information. Just don't complain about bad dquot blks.
  73. */
  74. if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC)) {
  75. if (flags & XFS_QMOPT_DOWARN)
  76. xfs_alert(mp,
  77. "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
  78. str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
  79. errs++;
  80. }
  81. if (ddq->d_version != XFS_DQUOT_VERSION) {
  82. if (flags & XFS_QMOPT_DOWARN)
  83. xfs_alert(mp,
  84. "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
  85. str, id, ddq->d_version, XFS_DQUOT_VERSION);
  86. errs++;
  87. }
  88. if (ddq->d_flags != XFS_DQ_USER &&
  89. ddq->d_flags != XFS_DQ_PROJ &&
  90. ddq->d_flags != XFS_DQ_GROUP) {
  91. if (flags & XFS_QMOPT_DOWARN)
  92. xfs_alert(mp,
  93. "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
  94. str, id, ddq->d_flags);
  95. errs++;
  96. }
  97. if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
  98. if (flags & XFS_QMOPT_DOWARN)
  99. xfs_alert(mp,
  100. "%s : ondisk-dquot 0x%p, ID mismatch: "
  101. "0x%x expected, found id 0x%x",
  102. str, ddq, id, be32_to_cpu(ddq->d_id));
  103. errs++;
  104. }
  105. if (!errs && ddq->d_id) {
  106. if (ddq->d_blk_softlimit &&
  107. be64_to_cpu(ddq->d_bcount) >
  108. be64_to_cpu(ddq->d_blk_softlimit)) {
  109. if (!ddq->d_btimer) {
  110. if (flags & XFS_QMOPT_DOWARN)
  111. xfs_alert(mp,
  112. "%s : Dquot ID 0x%x (0x%p) BLK TIMER NOT STARTED",
  113. str, (int)be32_to_cpu(ddq->d_id), ddq);
  114. errs++;
  115. }
  116. }
  117. if (ddq->d_ino_softlimit &&
  118. be64_to_cpu(ddq->d_icount) >
  119. be64_to_cpu(ddq->d_ino_softlimit)) {
  120. if (!ddq->d_itimer) {
  121. if (flags & XFS_QMOPT_DOWARN)
  122. xfs_alert(mp,
  123. "%s : Dquot ID 0x%x (0x%p) INODE TIMER NOT STARTED",
  124. str, (int)be32_to_cpu(ddq->d_id), ddq);
  125. errs++;
  126. }
  127. }
  128. if (ddq->d_rtb_softlimit &&
  129. be64_to_cpu(ddq->d_rtbcount) >
  130. be64_to_cpu(ddq->d_rtb_softlimit)) {
  131. if (!ddq->d_rtbtimer) {
  132. if (flags & XFS_QMOPT_DOWARN)
  133. xfs_alert(mp,
  134. "%s : Dquot ID 0x%x (0x%p) RTBLK TIMER NOT STARTED",
  135. str, (int)be32_to_cpu(ddq->d_id), ddq);
  136. errs++;
  137. }
  138. }
  139. }
  140. if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
  141. return errs;
  142. if (flags & XFS_QMOPT_DOWARN)
  143. xfs_notice(mp, "Re-initializing dquot ID 0x%x", id);
  144. /*
  145. * Typically, a repair is only requested by quotacheck.
  146. */
  147. ASSERT(id != -1);
  148. ASSERT(flags & XFS_QMOPT_DQREPAIR);
  149. memset(d, 0, sizeof(xfs_dqblk_t));
  150. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  151. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  152. d->dd_diskdq.d_flags = type;
  153. d->dd_diskdq.d_id = cpu_to_be32(id);
  154. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  155. uuid_copy(&d->dd_uuid, &mp->m_sb.sb_uuid);
  156. xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
  157. XFS_DQUOT_CRC_OFF);
  158. }
  159. return errs;
  160. }
  161. STATIC bool
  162. xfs_dquot_buf_verify_crc(
  163. struct xfs_mount *mp,
  164. struct xfs_buf *bp)
  165. {
  166. struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
  167. int ndquots;
  168. int i;
  169. if (!xfs_sb_version_hascrc(&mp->m_sb))
  170. return true;
  171. /*
  172. * if we are in log recovery, the quota subsystem has not been
  173. * initialised so we have no quotainfo structure. In that case, we need
  174. * to manually calculate the number of dquots in the buffer.
  175. */
  176. if (mp->m_quotainfo)
  177. ndquots = mp->m_quotainfo->qi_dqperchunk;
  178. else
  179. ndquots = xfs_calc_dquots_per_chunk(
  180. XFS_BB_TO_FSB(mp, bp->b_length));
  181. for (i = 0; i < ndquots; i++, d++) {
  182. if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
  183. XFS_DQUOT_CRC_OFF))
  184. return false;
  185. if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_uuid))
  186. return false;
  187. }
  188. return true;
  189. }
  190. STATIC bool
  191. xfs_dquot_buf_verify(
  192. struct xfs_mount *mp,
  193. struct xfs_buf *bp)
  194. {
  195. struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
  196. xfs_dqid_t id = 0;
  197. int ndquots;
  198. int i;
  199. /*
  200. * if we are in log recovery, the quota subsystem has not been
  201. * initialised so we have no quotainfo structure. In that case, we need
  202. * to manually calculate the number of dquots in the buffer.
  203. */
  204. if (mp->m_quotainfo)
  205. ndquots = mp->m_quotainfo->qi_dqperchunk;
  206. else
  207. ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
  208. /*
  209. * On the first read of the buffer, verify that each dquot is valid.
  210. * We don't know what the id of the dquot is supposed to be, just that
  211. * they should be increasing monotonically within the buffer. If the
  212. * first id is corrupt, then it will fail on the second dquot in the
  213. * buffer so corruptions could point to the wrong dquot in this case.
  214. */
  215. for (i = 0; i < ndquots; i++) {
  216. struct xfs_disk_dquot *ddq;
  217. int error;
  218. ddq = &d[i].dd_diskdq;
  219. if (i == 0)
  220. id = be32_to_cpu(ddq->d_id);
  221. error = xfs_dqcheck(mp, ddq, id + i, 0, XFS_QMOPT_DOWARN,
  222. "xfs_dquot_buf_verify");
  223. if (error)
  224. return false;
  225. }
  226. return true;
  227. }
  228. static void
  229. xfs_dquot_buf_read_verify(
  230. struct xfs_buf *bp)
  231. {
  232. struct xfs_mount *mp = bp->b_target->bt_mount;
  233. if (!xfs_dquot_buf_verify_crc(mp, bp))
  234. xfs_buf_ioerror(bp, EFSBADCRC);
  235. else if (!xfs_dquot_buf_verify(mp, bp))
  236. xfs_buf_ioerror(bp, EFSCORRUPTED);
  237. if (bp->b_error)
  238. xfs_verifier_error(bp);
  239. }
  240. /*
  241. * we don't calculate the CRC here as that is done when the dquot is flushed to
  242. * the buffer after the update is done. This ensures that the dquot in the
  243. * buffer always has an up-to-date CRC value.
  244. */
  245. static void
  246. xfs_dquot_buf_write_verify(
  247. struct xfs_buf *bp)
  248. {
  249. struct xfs_mount *mp = bp->b_target->bt_mount;
  250. if (!xfs_dquot_buf_verify(mp, bp)) {
  251. xfs_buf_ioerror(bp, EFSCORRUPTED);
  252. xfs_verifier_error(bp);
  253. return;
  254. }
  255. }
  256. const struct xfs_buf_ops xfs_dquot_buf_ops = {
  257. .verify_read = xfs_dquot_buf_read_verify,
  258. .verify_write = xfs_dquot_buf_write_verify,
  259. };