xfs_dquot_buf.c 7.6 KB

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