quotacheck_repair.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2020-2024 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_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_log_format.h"
  13. #include "xfs_trans.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_quota.h"
  16. #include "xfs_qm.h"
  17. #include "xfs_icache.h"
  18. #include "xfs_bmap_util.h"
  19. #include "xfs_iwalk.h"
  20. #include "xfs_ialloc.h"
  21. #include "xfs_sb.h"
  22. #include "scrub/scrub.h"
  23. #include "scrub/common.h"
  24. #include "scrub/repair.h"
  25. #include "scrub/xfile.h"
  26. #include "scrub/xfarray.h"
  27. #include "scrub/iscan.h"
  28. #include "scrub/quota.h"
  29. #include "scrub/quotacheck.h"
  30. #include "scrub/trace.h"
  31. /*
  32. * Live Quotacheck Repair
  33. * ======================
  34. *
  35. * Use the live quota counter information that we collected to replace the
  36. * counter values in the incore dquots. A scrub->repair cycle should have left
  37. * the live data and hooks active, so this is safe so long as we make sure the
  38. * dquot is locked.
  39. */
  40. /* Commit new counters to a dquot. */
  41. static int
  42. xqcheck_commit_dquot(
  43. struct xqcheck *xqc,
  44. xfs_dqtype_t dqtype,
  45. struct xfs_dquot *dq)
  46. {
  47. struct xqcheck_dquot xcdq;
  48. struct xfarray *counts = xqcheck_counters_for(xqc, dqtype);
  49. int64_t delta;
  50. bool dirty = false;
  51. int error = 0;
  52. /* Unlock the dquot just long enough to allocate a transaction. */
  53. xfs_dqunlock(dq);
  54. error = xchk_trans_alloc(xqc->sc, 0);
  55. xfs_dqlock(dq);
  56. if (error)
  57. return error;
  58. xfs_trans_dqjoin(xqc->sc->tp, dq);
  59. if (xchk_iscan_aborted(&xqc->iscan)) {
  60. error = -ECANCELED;
  61. goto out_cancel;
  62. }
  63. mutex_lock(&xqc->lock);
  64. error = xfarray_load_sparse(counts, dq->q_id, &xcdq);
  65. if (error)
  66. goto out_unlock;
  67. /* Adjust counters as needed. */
  68. delta = (int64_t)xcdq.icount - dq->q_ino.count;
  69. if (delta) {
  70. dq->q_ino.reserved += delta;
  71. dq->q_ino.count += delta;
  72. dirty = true;
  73. }
  74. delta = (int64_t)xcdq.bcount - dq->q_blk.count;
  75. if (delta) {
  76. dq->q_blk.reserved += delta;
  77. dq->q_blk.count += delta;
  78. dirty = true;
  79. }
  80. delta = (int64_t)xcdq.rtbcount - dq->q_rtb.count;
  81. if (delta) {
  82. dq->q_rtb.reserved += delta;
  83. dq->q_rtb.count += delta;
  84. dirty = true;
  85. }
  86. xcdq.flags |= (XQCHECK_DQUOT_REPAIR_SCANNED | XQCHECK_DQUOT_WRITTEN);
  87. error = xfarray_store(counts, dq->q_id, &xcdq);
  88. if (error == -EFBIG) {
  89. /*
  90. * EFBIG means we tried to store data at too high a byte offset
  91. * in the sparse array. IOWs, we cannot complete the repair
  92. * and must cancel the whole operation. This should never
  93. * happen, but we need to catch it anyway.
  94. */
  95. error = -ECANCELED;
  96. }
  97. mutex_unlock(&xqc->lock);
  98. if (error || !dirty)
  99. goto out_cancel;
  100. trace_xrep_quotacheck_dquot(xqc->sc->mp, dq->q_type, dq->q_id);
  101. /* Commit the dirty dquot to disk. */
  102. dq->q_flags |= XFS_DQFLAG_DIRTY;
  103. if (dq->q_id)
  104. xfs_qm_adjust_dqtimers(dq);
  105. xfs_trans_log_dquot(xqc->sc->tp, dq);
  106. /*
  107. * Transaction commit unlocks the dquot, so we must re-lock it so that
  108. * the caller can put the reference (which apparently requires a locked
  109. * dquot).
  110. */
  111. error = xrep_trans_commit(xqc->sc);
  112. xfs_dqlock(dq);
  113. return error;
  114. out_unlock:
  115. mutex_unlock(&xqc->lock);
  116. out_cancel:
  117. xchk_trans_cancel(xqc->sc);
  118. /* Re-lock the dquot so the caller can put the reference. */
  119. xfs_dqlock(dq);
  120. return error;
  121. }
  122. /* Commit new quota counters for a particular quota type. */
  123. STATIC int
  124. xqcheck_commit_dqtype(
  125. struct xqcheck *xqc,
  126. unsigned int dqtype)
  127. {
  128. struct xchk_dqiter cursor = { };
  129. struct xqcheck_dquot xcdq;
  130. struct xfs_scrub *sc = xqc->sc;
  131. struct xfs_mount *mp = sc->mp;
  132. struct xfarray *counts = xqcheck_counters_for(xqc, dqtype);
  133. struct xfs_dquot *dq;
  134. xfarray_idx_t cur = XFARRAY_CURSOR_INIT;
  135. int error;
  136. /*
  137. * Update the counters of every dquot that the quota file knows about.
  138. */
  139. xchk_dqiter_init(&cursor, sc, dqtype);
  140. while ((error = xchk_dquot_iter(&cursor, &dq)) == 1) {
  141. error = xqcheck_commit_dquot(xqc, dqtype, dq);
  142. xfs_qm_dqput(dq);
  143. if (error)
  144. break;
  145. }
  146. if (error)
  147. return error;
  148. /*
  149. * Make a second pass to deal with the dquots that we know about but
  150. * the quota file previously did not know about.
  151. */
  152. mutex_lock(&xqc->lock);
  153. while ((error = xfarray_iter(counts, &cur, &xcdq)) == 1) {
  154. xfs_dqid_t id = cur - 1;
  155. if (xcdq.flags & XQCHECK_DQUOT_REPAIR_SCANNED)
  156. continue;
  157. mutex_unlock(&xqc->lock);
  158. /*
  159. * Grab the dquot, allowing for dquot block allocation in a
  160. * separate transaction. We committed the scrub transaction
  161. * in a previous step, so we will not be creating nested
  162. * transactions here.
  163. */
  164. error = xfs_qm_dqget(mp, id, dqtype, true, &dq);
  165. if (error)
  166. return error;
  167. error = xqcheck_commit_dquot(xqc, dqtype, dq);
  168. xfs_qm_dqput(dq);
  169. if (error)
  170. return error;
  171. mutex_lock(&xqc->lock);
  172. }
  173. mutex_unlock(&xqc->lock);
  174. return error;
  175. }
  176. /* Figure out quota CHKD flags for the running quota types. */
  177. static inline unsigned int
  178. xqcheck_chkd_flags(
  179. struct xfs_mount *mp)
  180. {
  181. unsigned int ret = 0;
  182. if (XFS_IS_UQUOTA_ON(mp))
  183. ret |= XFS_UQUOTA_CHKD;
  184. if (XFS_IS_GQUOTA_ON(mp))
  185. ret |= XFS_GQUOTA_CHKD;
  186. if (XFS_IS_PQUOTA_ON(mp))
  187. ret |= XFS_PQUOTA_CHKD;
  188. return ret;
  189. }
  190. /* Commit the new dquot counters. */
  191. int
  192. xrep_quotacheck(
  193. struct xfs_scrub *sc)
  194. {
  195. struct xqcheck *xqc = sc->buf;
  196. unsigned int qflags = xqcheck_chkd_flags(sc->mp);
  197. int error;
  198. /*
  199. * Clear the CHKD flag for the running quota types and commit the scrub
  200. * transaction so that we can allocate new quota block mappings if we
  201. * have to. If we crash after this point, the sb still has the CHKD
  202. * flags cleared, so mount quotacheck will fix all of this up.
  203. */
  204. xrep_update_qflags(sc, qflags, 0);
  205. error = xrep_trans_commit(sc);
  206. if (error)
  207. return error;
  208. /* Commit the new counters to the dquots. */
  209. if (xqc->ucounts) {
  210. error = xqcheck_commit_dqtype(xqc, XFS_DQTYPE_USER);
  211. if (error)
  212. return error;
  213. }
  214. if (xqc->gcounts) {
  215. error = xqcheck_commit_dqtype(xqc, XFS_DQTYPE_GROUP);
  216. if (error)
  217. return error;
  218. }
  219. if (xqc->pcounts) {
  220. error = xqcheck_commit_dqtype(xqc, XFS_DQTYPE_PROJ);
  221. if (error)
  222. return error;
  223. }
  224. /* Set the CHKD flags now that we've fixed quota counts. */
  225. error = xchk_trans_alloc(sc, 0);
  226. if (error)
  227. return error;
  228. xrep_update_qflags(sc, 0, qflags);
  229. return xrep_trans_commit(sc);
  230. }