quota.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_bit.h"
  10. #include "xfs_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_log_format.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_quota.h"
  17. #include "xfs_qm.h"
  18. #include "xfs_bmap.h"
  19. #include "scrub/scrub.h"
  20. #include "scrub/common.h"
  21. #include "scrub/quota.h"
  22. /* Convert a scrub type code to a DQ flag, or return 0 if error. */
  23. xfs_dqtype_t
  24. xchk_quota_to_dqtype(
  25. struct xfs_scrub *sc)
  26. {
  27. switch (sc->sm->sm_type) {
  28. case XFS_SCRUB_TYPE_UQUOTA:
  29. return XFS_DQTYPE_USER;
  30. case XFS_SCRUB_TYPE_GQUOTA:
  31. return XFS_DQTYPE_GROUP;
  32. case XFS_SCRUB_TYPE_PQUOTA:
  33. return XFS_DQTYPE_PROJ;
  34. default:
  35. return 0;
  36. }
  37. }
  38. /* Set us up to scrub a quota. */
  39. int
  40. xchk_setup_quota(
  41. struct xfs_scrub *sc)
  42. {
  43. xfs_dqtype_t dqtype;
  44. int error;
  45. if (!XFS_IS_QUOTA_ON(sc->mp))
  46. return -ENOENT;
  47. dqtype = xchk_quota_to_dqtype(sc);
  48. if (dqtype == 0)
  49. return -EINVAL;
  50. if (!xfs_this_quota_on(sc->mp, dqtype))
  51. return -ENOENT;
  52. if (xchk_need_intent_drain(sc))
  53. xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
  54. error = xchk_setup_fs(sc);
  55. if (error)
  56. return error;
  57. error = xchk_install_live_inode(sc, xfs_quota_inode(sc->mp, dqtype));
  58. if (error)
  59. return error;
  60. xchk_ilock(sc, XFS_ILOCK_EXCL);
  61. return 0;
  62. }
  63. /* Quotas. */
  64. struct xchk_quota_info {
  65. struct xfs_scrub *sc;
  66. xfs_dqid_t last_id;
  67. };
  68. /* There's a written block backing this dquot, right? */
  69. STATIC int
  70. xchk_quota_item_bmap(
  71. struct xfs_scrub *sc,
  72. struct xfs_dquot *dq,
  73. xfs_fileoff_t offset)
  74. {
  75. struct xfs_bmbt_irec irec;
  76. struct xfs_mount *mp = sc->mp;
  77. int nmaps = 1;
  78. int error;
  79. if (!xfs_verify_fileoff(mp, offset)) {
  80. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  81. return 0;
  82. }
  83. if (dq->q_fileoffset != offset) {
  84. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  85. return 0;
  86. }
  87. error = xfs_bmapi_read(sc->ip, offset, 1, &irec, &nmaps, 0);
  88. if (error)
  89. return error;
  90. if (nmaps != 1) {
  91. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  92. return 0;
  93. }
  94. if (!xfs_verify_fsbno(mp, irec.br_startblock))
  95. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  96. if (XFS_FSB_TO_DADDR(mp, irec.br_startblock) != dq->q_blkno)
  97. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  98. if (!xfs_bmap_is_written_extent(&irec))
  99. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  100. return 0;
  101. }
  102. /* Complain if a quota timer is incorrectly set. */
  103. static inline void
  104. xchk_quota_item_timer(
  105. struct xfs_scrub *sc,
  106. xfs_fileoff_t offset,
  107. const struct xfs_dquot_res *res)
  108. {
  109. if ((res->softlimit && res->count > res->softlimit) ||
  110. (res->hardlimit && res->count > res->hardlimit)) {
  111. if (!res->timer)
  112. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  113. } else {
  114. if (res->timer)
  115. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  116. }
  117. }
  118. /* Scrub the fields in an individual quota item. */
  119. STATIC int
  120. xchk_quota_item(
  121. struct xchk_quota_info *sqi,
  122. struct xfs_dquot *dq)
  123. {
  124. struct xfs_scrub *sc = sqi->sc;
  125. struct xfs_mount *mp = sc->mp;
  126. struct xfs_quotainfo *qi = mp->m_quotainfo;
  127. xfs_fileoff_t offset;
  128. xfs_ino_t fs_icount;
  129. int error = 0;
  130. if (xchk_should_terminate(sc, &error))
  131. return error;
  132. /*
  133. * We want to validate the bmap record for the storage backing this
  134. * dquot, so we need to lock the dquot and the quota file. For quota
  135. * operations, the locking order is first the ILOCK and then the dquot.
  136. * However, dqiterate gave us a locked dquot, so drop the dquot lock to
  137. * get the ILOCK.
  138. */
  139. xfs_dqunlock(dq);
  140. xchk_ilock(sc, XFS_ILOCK_SHARED);
  141. xfs_dqlock(dq);
  142. /*
  143. * Except for the root dquot, the actual dquot we got must either have
  144. * the same or higher id as we saw before.
  145. */
  146. offset = dq->q_id / qi->qi_dqperchunk;
  147. if (dq->q_id && dq->q_id <= sqi->last_id)
  148. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  149. sqi->last_id = dq->q_id;
  150. error = xchk_quota_item_bmap(sc, dq, offset);
  151. xchk_iunlock(sc, XFS_ILOCK_SHARED);
  152. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, offset, &error))
  153. return error;
  154. /*
  155. * Warn if the hard limits are larger than the fs.
  156. * Administrators can do this, though in production this seems
  157. * suspect, which is why we flag it for review.
  158. *
  159. * Complain about corruption if the soft limit is greater than
  160. * the hard limit.
  161. */
  162. if (dq->q_blk.hardlimit > mp->m_sb.sb_dblocks)
  163. xchk_fblock_set_warning(sc, XFS_DATA_FORK, offset);
  164. if (dq->q_blk.softlimit > dq->q_blk.hardlimit)
  165. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  166. if (dq->q_ino.hardlimit > M_IGEO(mp)->maxicount)
  167. xchk_fblock_set_warning(sc, XFS_DATA_FORK, offset);
  168. if (dq->q_ino.softlimit > dq->q_ino.hardlimit)
  169. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  170. if (dq->q_rtb.hardlimit > mp->m_sb.sb_rblocks)
  171. xchk_fblock_set_warning(sc, XFS_DATA_FORK, offset);
  172. if (dq->q_rtb.softlimit > dq->q_rtb.hardlimit)
  173. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  174. /* Check the resource counts. */
  175. fs_icount = percpu_counter_sum(&mp->m_icount);
  176. /*
  177. * Check that usage doesn't exceed physical limits. However, on
  178. * a reflink filesystem we're allowed to exceed physical space
  179. * if there are no quota limits.
  180. */
  181. if (xfs_has_reflink(mp)) {
  182. if (mp->m_sb.sb_dblocks < dq->q_blk.count)
  183. xchk_fblock_set_warning(sc, XFS_DATA_FORK,
  184. offset);
  185. } else {
  186. if (mp->m_sb.sb_dblocks < dq->q_blk.count)
  187. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
  188. offset);
  189. }
  190. if (dq->q_ino.count > fs_icount || dq->q_rtb.count > mp->m_sb.sb_rblocks)
  191. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
  192. /*
  193. * We can violate the hard limits if the admin suddenly sets a
  194. * lower limit than the actual usage. However, we flag it for
  195. * admin review.
  196. */
  197. if (dq->q_id == 0)
  198. goto out;
  199. if (dq->q_blk.hardlimit != 0 &&
  200. dq->q_blk.count > dq->q_blk.hardlimit)
  201. xchk_fblock_set_warning(sc, XFS_DATA_FORK, offset);
  202. if (dq->q_ino.hardlimit != 0 &&
  203. dq->q_ino.count > dq->q_ino.hardlimit)
  204. xchk_fblock_set_warning(sc, XFS_DATA_FORK, offset);
  205. if (dq->q_rtb.hardlimit != 0 &&
  206. dq->q_rtb.count > dq->q_rtb.hardlimit)
  207. xchk_fblock_set_warning(sc, XFS_DATA_FORK, offset);
  208. xchk_quota_item_timer(sc, offset, &dq->q_blk);
  209. xchk_quota_item_timer(sc, offset, &dq->q_ino);
  210. xchk_quota_item_timer(sc, offset, &dq->q_rtb);
  211. out:
  212. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  213. return -ECANCELED;
  214. return 0;
  215. }
  216. /* Check the quota's data fork. */
  217. STATIC int
  218. xchk_quota_data_fork(
  219. struct xfs_scrub *sc)
  220. {
  221. struct xfs_bmbt_irec irec = { 0 };
  222. struct xfs_iext_cursor icur;
  223. struct xfs_quotainfo *qi = sc->mp->m_quotainfo;
  224. struct xfs_ifork *ifp;
  225. xfs_fileoff_t max_dqid_off;
  226. int error = 0;
  227. /* Invoke the fork scrubber. */
  228. error = xchk_metadata_inode_forks(sc);
  229. if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
  230. return error;
  231. /* Check for data fork problems that apply only to quota files. */
  232. max_dqid_off = XFS_DQ_ID_MAX / qi->qi_dqperchunk;
  233. ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
  234. for_each_xfs_iext(ifp, &icur, &irec) {
  235. if (xchk_should_terminate(sc, &error))
  236. break;
  237. /*
  238. * delalloc/unwritten extents or blocks mapped above the highest
  239. * quota id shouldn't happen.
  240. */
  241. if (!xfs_bmap_is_written_extent(&irec) ||
  242. irec.br_startoff > max_dqid_off ||
  243. irec.br_startoff + irec.br_blockcount - 1 > max_dqid_off) {
  244. xchk_fblock_set_corrupt(sc, XFS_DATA_FORK,
  245. irec.br_startoff);
  246. break;
  247. }
  248. }
  249. return error;
  250. }
  251. /* Scrub all of a quota type's items. */
  252. int
  253. xchk_quota(
  254. struct xfs_scrub *sc)
  255. {
  256. struct xchk_dqiter cursor = { };
  257. struct xchk_quota_info sqi = { .sc = sc };
  258. struct xfs_mount *mp = sc->mp;
  259. struct xfs_quotainfo *qi = mp->m_quotainfo;
  260. struct xfs_dquot *dq;
  261. xfs_dqtype_t dqtype;
  262. int error = 0;
  263. dqtype = xchk_quota_to_dqtype(sc);
  264. /* Look for problem extents. */
  265. error = xchk_quota_data_fork(sc);
  266. if (error)
  267. goto out;
  268. if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
  269. goto out;
  270. /*
  271. * Check all the quota items. Now that we've checked the quota inode
  272. * data fork we have to drop ILOCK_EXCL to use the regular dquot
  273. * functions.
  274. */
  275. xchk_iunlock(sc, sc->ilock_flags);
  276. /* Now look for things that the quota verifiers won't complain about. */
  277. xchk_dqiter_init(&cursor, sc, dqtype);
  278. while ((error = xchk_dquot_iter(&cursor, &dq)) == 1) {
  279. error = xchk_quota_item(&sqi, dq);
  280. xfs_qm_dqput(dq);
  281. if (error)
  282. break;
  283. }
  284. if (error == -ECANCELED)
  285. error = 0;
  286. if (!xchk_fblock_process_error(sc, XFS_DATA_FORK,
  287. sqi.last_id * qi->qi_dqperchunk, &error))
  288. goto out;
  289. out:
  290. return error;
  291. }