xfs_trans_refcount.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  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_defer.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_trans_priv.h"
  16. #include "xfs_refcount_item.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_refcount.h"
  19. /*
  20. * This routine is called to allocate a "refcount update done"
  21. * log item.
  22. */
  23. struct xfs_cud_log_item *
  24. xfs_trans_get_cud(
  25. struct xfs_trans *tp,
  26. struct xfs_cui_log_item *cuip)
  27. {
  28. struct xfs_cud_log_item *cudp;
  29. cudp = xfs_cud_init(tp->t_mountp, cuip);
  30. xfs_trans_add_item(tp, &cudp->cud_item);
  31. return cudp;
  32. }
  33. /*
  34. * Finish an refcount update and log it to the CUD. Note that the
  35. * transaction is marked dirty regardless of whether the refcount
  36. * update succeeds or fails to support the CUI/CUD lifecycle rules.
  37. */
  38. int
  39. xfs_trans_log_finish_refcount_update(
  40. struct xfs_trans *tp,
  41. struct xfs_cud_log_item *cudp,
  42. enum xfs_refcount_intent_type type,
  43. xfs_fsblock_t startblock,
  44. xfs_extlen_t blockcount,
  45. xfs_fsblock_t *new_fsb,
  46. xfs_extlen_t *new_len,
  47. struct xfs_btree_cur **pcur)
  48. {
  49. int error;
  50. error = xfs_refcount_finish_one(tp, type, startblock,
  51. blockcount, new_fsb, new_len, pcur);
  52. /*
  53. * Mark the transaction dirty, even on error. This ensures the
  54. * transaction is aborted, which:
  55. *
  56. * 1.) releases the CUI and frees the CUD
  57. * 2.) shuts down the filesystem
  58. */
  59. tp->t_flags |= XFS_TRANS_DIRTY;
  60. set_bit(XFS_LI_DIRTY, &cudp->cud_item.li_flags);
  61. return error;
  62. }
  63. /* Sort refcount intents by AG. */
  64. static int
  65. xfs_refcount_update_diff_items(
  66. void *priv,
  67. struct list_head *a,
  68. struct list_head *b)
  69. {
  70. struct xfs_mount *mp = priv;
  71. struct xfs_refcount_intent *ra;
  72. struct xfs_refcount_intent *rb;
  73. ra = container_of(a, struct xfs_refcount_intent, ri_list);
  74. rb = container_of(b, struct xfs_refcount_intent, ri_list);
  75. return XFS_FSB_TO_AGNO(mp, ra->ri_startblock) -
  76. XFS_FSB_TO_AGNO(mp, rb->ri_startblock);
  77. }
  78. /* Get an CUI. */
  79. STATIC void *
  80. xfs_refcount_update_create_intent(
  81. struct xfs_trans *tp,
  82. unsigned int count)
  83. {
  84. struct xfs_cui_log_item *cuip;
  85. ASSERT(tp != NULL);
  86. ASSERT(count > 0);
  87. cuip = xfs_cui_init(tp->t_mountp, count);
  88. ASSERT(cuip != NULL);
  89. /*
  90. * Get a log_item_desc to point at the new item.
  91. */
  92. xfs_trans_add_item(tp, &cuip->cui_item);
  93. return cuip;
  94. }
  95. /* Set the phys extent flags for this reverse mapping. */
  96. static void
  97. xfs_trans_set_refcount_flags(
  98. struct xfs_phys_extent *refc,
  99. enum xfs_refcount_intent_type type)
  100. {
  101. refc->pe_flags = 0;
  102. switch (type) {
  103. case XFS_REFCOUNT_INCREASE:
  104. case XFS_REFCOUNT_DECREASE:
  105. case XFS_REFCOUNT_ALLOC_COW:
  106. case XFS_REFCOUNT_FREE_COW:
  107. refc->pe_flags |= type;
  108. break;
  109. default:
  110. ASSERT(0);
  111. }
  112. }
  113. /* Log refcount updates in the intent item. */
  114. STATIC void
  115. xfs_refcount_update_log_item(
  116. struct xfs_trans *tp,
  117. void *intent,
  118. struct list_head *item)
  119. {
  120. struct xfs_cui_log_item *cuip = intent;
  121. struct xfs_refcount_intent *refc;
  122. uint next_extent;
  123. struct xfs_phys_extent *ext;
  124. refc = container_of(item, struct xfs_refcount_intent, ri_list);
  125. tp->t_flags |= XFS_TRANS_DIRTY;
  126. set_bit(XFS_LI_DIRTY, &cuip->cui_item.li_flags);
  127. /*
  128. * atomic_inc_return gives us the value after the increment;
  129. * we want to use it as an array index so we need to subtract 1 from
  130. * it.
  131. */
  132. next_extent = atomic_inc_return(&cuip->cui_next_extent) - 1;
  133. ASSERT(next_extent < cuip->cui_format.cui_nextents);
  134. ext = &cuip->cui_format.cui_extents[next_extent];
  135. ext->pe_startblock = refc->ri_startblock;
  136. ext->pe_len = refc->ri_blockcount;
  137. xfs_trans_set_refcount_flags(ext, refc->ri_type);
  138. }
  139. /* Get an CUD so we can process all the deferred refcount updates. */
  140. STATIC void *
  141. xfs_refcount_update_create_done(
  142. struct xfs_trans *tp,
  143. void *intent,
  144. unsigned int count)
  145. {
  146. return xfs_trans_get_cud(tp, intent);
  147. }
  148. /* Process a deferred refcount update. */
  149. STATIC int
  150. xfs_refcount_update_finish_item(
  151. struct xfs_trans *tp,
  152. struct list_head *item,
  153. void *done_item,
  154. void **state)
  155. {
  156. struct xfs_refcount_intent *refc;
  157. xfs_fsblock_t new_fsb;
  158. xfs_extlen_t new_aglen;
  159. int error;
  160. refc = container_of(item, struct xfs_refcount_intent, ri_list);
  161. error = xfs_trans_log_finish_refcount_update(tp, done_item,
  162. refc->ri_type,
  163. refc->ri_startblock,
  164. refc->ri_blockcount,
  165. &new_fsb, &new_aglen,
  166. (struct xfs_btree_cur **)state);
  167. /* Did we run out of reservation? Requeue what we didn't finish. */
  168. if (!error && new_aglen > 0) {
  169. ASSERT(refc->ri_type == XFS_REFCOUNT_INCREASE ||
  170. refc->ri_type == XFS_REFCOUNT_DECREASE);
  171. refc->ri_startblock = new_fsb;
  172. refc->ri_blockcount = new_aglen;
  173. return -EAGAIN;
  174. }
  175. kmem_free(refc);
  176. return error;
  177. }
  178. /* Clean up after processing deferred refcounts. */
  179. STATIC void
  180. xfs_refcount_update_finish_cleanup(
  181. struct xfs_trans *tp,
  182. void *state,
  183. int error)
  184. {
  185. struct xfs_btree_cur *rcur = state;
  186. xfs_refcount_finish_one_cleanup(tp, rcur, error);
  187. }
  188. /* Abort all pending CUIs. */
  189. STATIC void
  190. xfs_refcount_update_abort_intent(
  191. void *intent)
  192. {
  193. xfs_cui_release(intent);
  194. }
  195. /* Cancel a deferred refcount update. */
  196. STATIC void
  197. xfs_refcount_update_cancel_item(
  198. struct list_head *item)
  199. {
  200. struct xfs_refcount_intent *refc;
  201. refc = container_of(item, struct xfs_refcount_intent, ri_list);
  202. kmem_free(refc);
  203. }
  204. static const struct xfs_defer_op_type xfs_refcount_update_defer_type = {
  205. .type = XFS_DEFER_OPS_TYPE_REFCOUNT,
  206. .max_items = XFS_CUI_MAX_FAST_EXTENTS,
  207. .diff_items = xfs_refcount_update_diff_items,
  208. .create_intent = xfs_refcount_update_create_intent,
  209. .abort_intent = xfs_refcount_update_abort_intent,
  210. .log_item = xfs_refcount_update_log_item,
  211. .create_done = xfs_refcount_update_create_done,
  212. .finish_item = xfs_refcount_update_finish_item,
  213. .finish_cleanup = xfs_refcount_update_finish_cleanup,
  214. .cancel_item = xfs_refcount_update_cancel_item,
  215. };
  216. /* Register the deferred op type. */
  217. void
  218. xfs_refcount_update_init_defer_op(void)
  219. {
  220. xfs_defer_init_op_type(&xfs_refcount_update_defer_type);
  221. }