xfs_dquot_item.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_lsn_t commit_lsn)
  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. }
  194. /*------------------ QUOTAOFF LOG ITEMS -------------------*/
  195. static inline struct xfs_qoff_logitem *QOFF_ITEM(struct xfs_log_item *lip)
  196. {
  197. return container_of(lip, struct xfs_qoff_logitem, qql_item);
  198. }
  199. /*
  200. * This returns the number of iovecs needed to log the given quotaoff item.
  201. * We only need 1 iovec for an quotaoff item. It just logs the
  202. * quotaoff_log_format structure.
  203. */
  204. STATIC void
  205. xfs_qm_qoff_logitem_size(
  206. struct xfs_log_item *lip,
  207. int *nvecs,
  208. int *nbytes)
  209. {
  210. *nvecs += 1;
  211. *nbytes += sizeof(struct xfs_qoff_logitem);
  212. }
  213. STATIC void
  214. xfs_qm_qoff_logitem_format(
  215. struct xfs_log_item *lip,
  216. struct xfs_log_vec *lv)
  217. {
  218. struct xfs_qoff_logitem *qflip = QOFF_ITEM(lip);
  219. struct xfs_log_iovec *vecp = NULL;
  220. struct xfs_qoff_logformat *qlf;
  221. qlf = xlog_prepare_iovec(lv, &vecp, XLOG_REG_TYPE_QUOTAOFF);
  222. qlf->qf_type = XFS_LI_QUOTAOFF;
  223. qlf->qf_size = 1;
  224. qlf->qf_flags = qflip->qql_flags;
  225. xlog_finish_iovec(lv, vecp, sizeof(struct xfs_qoff_logitem));
  226. }
  227. /*
  228. * There isn't much you can do to push a quotaoff item. It is simply
  229. * stuck waiting for the log to be flushed to disk.
  230. */
  231. STATIC uint
  232. xfs_qm_qoff_logitem_push(
  233. struct xfs_log_item *lip,
  234. struct list_head *buffer_list)
  235. {
  236. return XFS_ITEM_LOCKED;
  237. }
  238. STATIC xfs_lsn_t
  239. xfs_qm_qoffend_logitem_committed(
  240. struct xfs_log_item *lip,
  241. xfs_lsn_t lsn)
  242. {
  243. struct xfs_qoff_logitem *qfe = QOFF_ITEM(lip);
  244. struct xfs_qoff_logitem *qfs = qfe->qql_start_lip;
  245. xfs_qm_qoff_logitem_relse(qfs);
  246. kmem_free(lip->li_lv_shadow);
  247. kmem_free(qfe);
  248. return (xfs_lsn_t)-1;
  249. }
  250. STATIC void
  251. xfs_qm_qoff_logitem_release(
  252. struct xfs_log_item *lip)
  253. {
  254. struct xfs_qoff_logitem *qoff = QOFF_ITEM(lip);
  255. if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) {
  256. if (qoff->qql_start_lip)
  257. xfs_qm_qoff_logitem_relse(qoff->qql_start_lip);
  258. xfs_qm_qoff_logitem_relse(qoff);
  259. }
  260. }
  261. static const struct xfs_item_ops xfs_qm_qoffend_logitem_ops = {
  262. .iop_size = xfs_qm_qoff_logitem_size,
  263. .iop_format = xfs_qm_qoff_logitem_format,
  264. .iop_committed = xfs_qm_qoffend_logitem_committed,
  265. .iop_push = xfs_qm_qoff_logitem_push,
  266. .iop_release = xfs_qm_qoff_logitem_release,
  267. };
  268. static const struct xfs_item_ops xfs_qm_qoff_logitem_ops = {
  269. .iop_size = xfs_qm_qoff_logitem_size,
  270. .iop_format = xfs_qm_qoff_logitem_format,
  271. .iop_push = xfs_qm_qoff_logitem_push,
  272. .iop_release = xfs_qm_qoff_logitem_release,
  273. };
  274. /*
  275. * Delete the quotaoff intent from the AIL and free it. On success,
  276. * this should only be called for the start item. It can be used for
  277. * either on shutdown or abort.
  278. */
  279. void
  280. xfs_qm_qoff_logitem_relse(
  281. struct xfs_qoff_logitem *qoff)
  282. {
  283. struct xfs_log_item *lip = &qoff->qql_item;
  284. ASSERT(test_bit(XFS_LI_IN_AIL, &lip->li_flags) ||
  285. test_bit(XFS_LI_ABORTED, &lip->li_flags) ||
  286. XFS_FORCED_SHUTDOWN(lip->li_mountp));
  287. xfs_trans_ail_delete(lip, 0);
  288. kmem_free(lip->li_lv_shadow);
  289. kmem_free(qoff);
  290. }
  291. /*
  292. * Allocate and initialize an quotaoff item of the correct quota type(s).
  293. */
  294. struct xfs_qoff_logitem *
  295. xfs_qm_qoff_logitem_init(
  296. struct xfs_mount *mp,
  297. struct xfs_qoff_logitem *start,
  298. uint flags)
  299. {
  300. struct xfs_qoff_logitem *qf;
  301. qf = kmem_zalloc(sizeof(struct xfs_qoff_logitem), 0);
  302. xfs_log_item_init(mp, &qf->qql_item, XFS_LI_QUOTAOFF, start ?
  303. &xfs_qm_qoffend_logitem_ops : &xfs_qm_qoff_logitem_ops);
  304. qf->qql_item.li_mountp = mp;
  305. qf->qql_start_lip = start;
  306. qf->qql_flags = flags;
  307. return qf;
  308. }