dqiterate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 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. #include "scrub/trace.h"
  23. /* Initialize a dquot iteration cursor. */
  24. void
  25. xchk_dqiter_init(
  26. struct xchk_dqiter *cursor,
  27. struct xfs_scrub *sc,
  28. xfs_dqtype_t dqtype)
  29. {
  30. cursor->sc = sc;
  31. cursor->bmap.br_startoff = NULLFILEOFF;
  32. cursor->dqtype = dqtype & XFS_DQTYPE_REC_MASK;
  33. cursor->quota_ip = xfs_quota_inode(sc->mp, cursor->dqtype);
  34. cursor->id = 0;
  35. }
  36. /*
  37. * Ensure that the cached data fork mapping for the dqiter cursor is fresh and
  38. * covers the dquot pointed to by the scan cursor.
  39. */
  40. STATIC int
  41. xchk_dquot_iter_revalidate_bmap(
  42. struct xchk_dqiter *cursor)
  43. {
  44. struct xfs_quotainfo *qi = cursor->sc->mp->m_quotainfo;
  45. struct xfs_ifork *ifp = xfs_ifork_ptr(cursor->quota_ip,
  46. XFS_DATA_FORK);
  47. xfs_fileoff_t fileoff;
  48. xfs_dqid_t this_id = cursor->id;
  49. int nmaps = 1;
  50. int error;
  51. fileoff = this_id / qi->qi_dqperchunk;
  52. /*
  53. * If we have a mapping for cursor->id and it's still fresh, there's
  54. * no need to reread the bmbt.
  55. */
  56. if (cursor->bmap.br_startoff != NULLFILEOFF &&
  57. cursor->if_seq == ifp->if_seq &&
  58. cursor->bmap.br_startoff + cursor->bmap.br_blockcount > fileoff)
  59. return 0;
  60. /* Look up the data fork mapping for the dquot id of interest. */
  61. error = xfs_bmapi_read(cursor->quota_ip, fileoff,
  62. XFS_MAX_FILEOFF - fileoff, &cursor->bmap, &nmaps, 0);
  63. if (error)
  64. return error;
  65. if (!nmaps) {
  66. ASSERT(nmaps > 0);
  67. return -EFSCORRUPTED;
  68. }
  69. if (cursor->bmap.br_startoff > fileoff) {
  70. ASSERT(cursor->bmap.br_startoff == fileoff);
  71. return -EFSCORRUPTED;
  72. }
  73. cursor->if_seq = ifp->if_seq;
  74. trace_xchk_dquot_iter_revalidate_bmap(cursor, cursor->id);
  75. return 0;
  76. }
  77. /* Advance the dqiter cursor to the next non-sparse region of the quota file. */
  78. STATIC int
  79. xchk_dquot_iter_advance_bmap(
  80. struct xchk_dqiter *cursor,
  81. uint64_t *next_ondisk_id)
  82. {
  83. struct xfs_quotainfo *qi = cursor->sc->mp->m_quotainfo;
  84. struct xfs_ifork *ifp = xfs_ifork_ptr(cursor->quota_ip,
  85. XFS_DATA_FORK);
  86. xfs_fileoff_t fileoff;
  87. uint64_t next_id;
  88. int nmaps = 1;
  89. int error;
  90. /* Find the dquot id for the next non-hole mapping. */
  91. do {
  92. fileoff = cursor->bmap.br_startoff + cursor->bmap.br_blockcount;
  93. if (fileoff > XFS_DQ_ID_MAX / qi->qi_dqperchunk) {
  94. /* The hole goes beyond the max dquot id, we're done */
  95. *next_ondisk_id = -1ULL;
  96. return 0;
  97. }
  98. error = xfs_bmapi_read(cursor->quota_ip, fileoff,
  99. XFS_MAX_FILEOFF - fileoff, &cursor->bmap,
  100. &nmaps, 0);
  101. if (error)
  102. return error;
  103. if (!nmaps) {
  104. /* Must have reached the end of the mappings. */
  105. *next_ondisk_id = -1ULL;
  106. return 0;
  107. }
  108. if (cursor->bmap.br_startoff > fileoff) {
  109. ASSERT(cursor->bmap.br_startoff == fileoff);
  110. return -EFSCORRUPTED;
  111. }
  112. } while (!xfs_bmap_is_real_extent(&cursor->bmap));
  113. next_id = cursor->bmap.br_startoff * qi->qi_dqperchunk;
  114. if (next_id > XFS_DQ_ID_MAX) {
  115. /* The hole goes beyond the max dquot id, we're done */
  116. *next_ondisk_id = -1ULL;
  117. return 0;
  118. }
  119. /* Propose jumping forward to the dquot in the next allocated block. */
  120. *next_ondisk_id = next_id;
  121. cursor->if_seq = ifp->if_seq;
  122. trace_xchk_dquot_iter_advance_bmap(cursor, *next_ondisk_id);
  123. return 0;
  124. }
  125. /*
  126. * Find the id of the next highest incore dquot. Normally this will correspond
  127. * exactly with the quota file block mappings, but repair might have erased a
  128. * mapping because it was crosslinked; in that case, we need to re-allocate the
  129. * space so that we can reset q_blkno.
  130. */
  131. STATIC void
  132. xchk_dquot_iter_advance_incore(
  133. struct xchk_dqiter *cursor,
  134. uint64_t *next_incore_id)
  135. {
  136. struct xfs_quotainfo *qi = cursor->sc->mp->m_quotainfo;
  137. struct radix_tree_root *tree = xfs_dquot_tree(qi, cursor->dqtype);
  138. struct xfs_dquot *dq;
  139. unsigned int nr_found;
  140. *next_incore_id = -1ULL;
  141. mutex_lock(&qi->qi_tree_lock);
  142. nr_found = radix_tree_gang_lookup(tree, (void **)&dq, cursor->id, 1);
  143. if (nr_found)
  144. *next_incore_id = dq->q_id;
  145. mutex_unlock(&qi->qi_tree_lock);
  146. trace_xchk_dquot_iter_advance_incore(cursor, *next_incore_id);
  147. }
  148. /*
  149. * Walk all incore dquots of this filesystem. Caller must set *@cursorp to
  150. * zero before the first call, and must not hold the quota file ILOCK.
  151. * Returns 1 and a valid *@dqpp; 0 and *@dqpp == NULL when there are no more
  152. * dquots to iterate; or a negative errno.
  153. */
  154. int
  155. xchk_dquot_iter(
  156. struct xchk_dqiter *cursor,
  157. struct xfs_dquot **dqpp)
  158. {
  159. struct xfs_mount *mp = cursor->sc->mp;
  160. struct xfs_dquot *dq = NULL;
  161. uint64_t next_ondisk, next_incore = -1ULL;
  162. unsigned int lock_mode;
  163. int error = 0;
  164. if (cursor->id > XFS_DQ_ID_MAX)
  165. return 0;
  166. next_ondisk = cursor->id;
  167. /* Revalidate and/or advance the cursor. */
  168. lock_mode = xfs_ilock_data_map_shared(cursor->quota_ip);
  169. error = xchk_dquot_iter_revalidate_bmap(cursor);
  170. if (!error && !xfs_bmap_is_real_extent(&cursor->bmap))
  171. error = xchk_dquot_iter_advance_bmap(cursor, &next_ondisk);
  172. xfs_iunlock(cursor->quota_ip, lock_mode);
  173. if (error)
  174. return error;
  175. if (next_ondisk > cursor->id)
  176. xchk_dquot_iter_advance_incore(cursor, &next_incore);
  177. /* Pick the next dquot in the sequence and return it. */
  178. cursor->id = min(next_ondisk, next_incore);
  179. if (cursor->id > XFS_DQ_ID_MAX)
  180. return 0;
  181. trace_xchk_dquot_iter(cursor, cursor->id);
  182. error = xfs_qm_dqget(mp, cursor->id, cursor->dqtype, false, &dq);
  183. if (error)
  184. return error;
  185. cursor->id = dq->q_id + 1;
  186. *dqpp = dq;
  187. return 1;
  188. }