xfs_dquot_item.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2003 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_quota.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_buf_item.h"
  17. #include "xfs_trans_priv.h"
  18. #include "xfs_qm.h"
  19. #include "xfs_log.h"
  20. static inline struct xfs_dq_logitem *DQUOT_ITEM(struct xfs_log_item *lip)
  21. {
  22. return container_of(lip, struct xfs_dq_logitem, qli_item);
  23. }
  24. /*
  25. * returns the number of iovecs needed to log the given dquot item.
  26. */
  27. STATIC void
  28. xfs_qm_dquot_logitem_size(
  29. struct xfs_log_item *lip,
  30. int *nvecs,
  31. int *nbytes)
  32. {
  33. *nvecs += 2;
  34. *nbytes += sizeof(struct xfs_dq_logformat) +
  35. sizeof(struct xfs_disk_dquot);
  36. }
  37. /*
  38. * fills in the vector of log iovecs for the given dquot log item.
  39. */
  40. STATIC void
  41. xfs_qm_dquot_logitem_format(
  42. struct xfs_log_item *lip,
  43. struct xfs_log_vec *lv)
  44. {
  45. struct xfs_disk_dquot ddq;
  46. struct xfs_dq_logitem *qlip = DQUOT_ITEM(lip);
  47. struct xfs_log_iovec *vecp = NULL;
  48. struct xfs_dq_logformat *qlf;
  49. qlf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_QFORMAT);
  50. qlf->qlf_type = XFS_LI_DQUOT;
  51. qlf->qlf_size = 2;
  52. qlf->qlf_id = qlip->qli_dquot->q_id;
  53. qlf->qlf_blkno = qlip->qli_dquot->q_blkno;
  54. qlf->qlf_len = 1;
  55. qlf->qlf_boffset = qlip->qli_dquot->q_bufoffset;
  56. xlog_finish_iovec(lv, vecp, sizeof(struct xfs_dq_logformat));
  57. xfs_dquot_to_disk(&ddq, qlip->qli_dquot);
  58. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq,
  59. sizeof(struct xfs_disk_dquot));
  60. }
  61. /*
  62. * Increment the pin count of the given dquot.
  63. */
  64. STATIC void
  65. xfs_qm_dquot_logitem_pin(
  66. struct xfs_log_item *lip)
  67. {
  68. struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
  69. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  70. atomic_inc(&dqp->q_pincount);
  71. }
  72. /*
  73. * Decrement the pin count of the given dquot, and wake up
  74. * anyone in xfs_dqwait_unpin() if the count goes to 0. The
  75. * dquot must have been previously pinned with a call to
  76. * xfs_qm_dquot_logitem_pin().
  77. */
  78. STATIC void
  79. xfs_qm_dquot_logitem_unpin(
  80. struct xfs_log_item *lip,
  81. int remove)
  82. {
  83. struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
  84. ASSERT(atomic_read(&dqp->q_pincount) > 0);
  85. if (atomic_dec_and_test(&dqp->q_pincount))
  86. wake_up(&dqp->q_pinwait);
  87. }
  88. /*
  89. * This is called to wait for the given dquot to be unpinned.
  90. * Most of these pin/unpin routines are plagiarized from inode code.
  91. */
  92. void
  93. xfs_qm_dqunpin_wait(
  94. struct xfs_dquot *dqp)
  95. {
  96. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  97. if (atomic_read(&dqp->q_pincount) == 0)
  98. return;
  99. /*
  100. * Give the log a push so we don't wait here too long.
  101. */
  102. xfs_log_force(dqp->q_mount, 0);
  103. wait_event(dqp->q_pinwait, (atomic_read(&dqp->q_pincount) == 0));
  104. }
  105. STATIC uint
  106. xfs_qm_dquot_logitem_push(
  107. struct xfs_log_item *lip,
  108. struct list_head *buffer_list)
  109. __releases(&lip->li_ailp->ail_lock)
  110. __acquires(&lip->li_ailp->ail_lock)
  111. {
  112. struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
  113. struct xfs_buf *bp = lip->li_buf;
  114. uint rval = XFS_ITEM_SUCCESS;
  115. int error;
  116. if (atomic_read(&dqp->q_pincount) > 0)
  117. return XFS_ITEM_PINNED;
  118. if (!xfs_dqlock_nowait(dqp))
  119. return XFS_ITEM_LOCKED;
  120. /*
  121. * Re-check the pincount now that we stabilized the value by
  122. * taking the quota lock.
  123. */
  124. if (atomic_read(&dqp->q_pincount) > 0) {
  125. rval = XFS_ITEM_PINNED;
  126. goto out_unlock;
  127. }
  128. /*
  129. * Someone else is already flushing the dquot. Nothing we can do
  130. * here but wait for the flush to finish and remove the item from
  131. * the AIL.
  132. */
  133. if (!xfs_dqflock_nowait(dqp)) {
  134. rval = XFS_ITEM_FLUSHING;
  135. goto out_unlock;
  136. }
  137. spin_unlock(&lip->li_ailp->ail_lock);
  138. error = xfs_qm_dqflush(dqp, &bp);
  139. if (!error) {
  140. if (!xfs_buf_delwri_queue(bp, buffer_list))
  141. rval = XFS_ITEM_FLUSHING;
  142. xfs_buf_relse(bp);
  143. } else if (error == -EAGAIN)
  144. rval = XFS_ITEM_LOCKED;
  145. spin_lock(&lip->li_ailp->ail_lock);
  146. out_unlock:
  147. xfs_dqunlock(dqp);
  148. return rval;
  149. }
  150. STATIC void
  151. xfs_qm_dquot_logitem_release(
  152. struct xfs_log_item *lip)
  153. {
  154. struct xfs_dquot *dqp = DQUOT_ITEM(lip)->qli_dquot;
  155. ASSERT(XFS_DQ_IS_LOCKED(dqp));
  156. /*
  157. * dquots are never 'held' from getting unlocked at the end of
  158. * a transaction. Their locking and unlocking is hidden inside the
  159. * transaction layer, within trans_commit. Hence, no LI_HOLD flag
  160. * for the logitem.
  161. */
  162. xfs_dqunlock(dqp);
  163. }
  164. STATIC void
  165. xfs_qm_dquot_logitem_committing(
  166. struct xfs_log_item *lip,
  167. xfs_csn_t seq)
  168. {
  169. return xfs_qm_dquot_logitem_release(lip);
  170. }
  171. static const struct xfs_item_ops xfs_dquot_item_ops = {
  172. .iop_size = xfs_qm_dquot_logitem_size,
  173. .iop_format = xfs_qm_dquot_logitem_format,
  174. .iop_pin = xfs_qm_dquot_logitem_pin,
  175. .iop_unpin = xfs_qm_dquot_logitem_unpin,
  176. .iop_release = xfs_qm_dquot_logitem_release,
  177. .iop_committing = xfs_qm_dquot_logitem_committing,
  178. .iop_push = xfs_qm_dquot_logitem_push,
  179. };
  180. /*
  181. * Initialize the dquot log item for a newly allocated dquot.
  182. * The dquot isn't locked at this point, but it isn't on any of the lists
  183. * either, so we don't care.
  184. */
  185. void
  186. xfs_qm_dquot_logitem_init(
  187. struct xfs_dquot *dqp)
  188. {
  189. struct xfs_dq_logitem *lp = &dqp->q_logitem;
  190. xfs_log_item_init(dqp->q_mount, &lp->qli_item, XFS_LI_DQUOT,
  191. &xfs_dquot_item_ops);
  192. lp->qli_dquot = dqp;
  193. }