xfs_dquot_buf.c 8.2 KB

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