xfs_dquot_buf.c 8.1 KB

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