xfs_trans_bmap.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_bmap_item.h"
  17. #include "xfs_alloc.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_inode.h"
  20. /*
  21. * This routine is called to allocate a "bmap update done"
  22. * log item.
  23. */
  24. struct xfs_bud_log_item *
  25. xfs_trans_get_bud(
  26. struct xfs_trans *tp,
  27. struct xfs_bui_log_item *buip)
  28. {
  29. struct xfs_bud_log_item *budp;
  30. budp = xfs_bud_init(tp->t_mountp, buip);
  31. xfs_trans_add_item(tp, &budp->bud_item);
  32. return budp;
  33. }
  34. /*
  35. * Finish an bmap update and log it to the BUD. Note that the
  36. * transaction is marked dirty regardless of whether the bmap update
  37. * succeeds or fails to support the BUI/BUD lifecycle rules.
  38. */
  39. int
  40. xfs_trans_log_finish_bmap_update(
  41. struct xfs_trans *tp,
  42. struct xfs_bud_log_item *budp,
  43. enum xfs_bmap_intent_type type,
  44. struct xfs_inode *ip,
  45. int whichfork,
  46. xfs_fileoff_t startoff,
  47. xfs_fsblock_t startblock,
  48. xfs_filblks_t *blockcount,
  49. xfs_exntst_t state)
  50. {
  51. int error;
  52. error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
  53. startblock, blockcount, state);
  54. /*
  55. * Mark the transaction dirty, even on error. This ensures the
  56. * transaction is aborted, which:
  57. *
  58. * 1.) releases the BUI and frees the BUD
  59. * 2.) shuts down the filesystem
  60. */
  61. tp->t_flags |= XFS_TRANS_DIRTY;
  62. set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
  63. return error;
  64. }
  65. /* Sort bmap intents by inode. */
  66. static int
  67. xfs_bmap_update_diff_items(
  68. void *priv,
  69. struct list_head *a,
  70. struct list_head *b)
  71. {
  72. struct xfs_bmap_intent *ba;
  73. struct xfs_bmap_intent *bb;
  74. ba = container_of(a, struct xfs_bmap_intent, bi_list);
  75. bb = container_of(b, struct xfs_bmap_intent, bi_list);
  76. return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
  77. }
  78. /* Get an BUI. */
  79. STATIC void *
  80. xfs_bmap_update_create_intent(
  81. struct xfs_trans *tp,
  82. unsigned int count)
  83. {
  84. struct xfs_bui_log_item *buip;
  85. ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
  86. ASSERT(tp != NULL);
  87. buip = xfs_bui_init(tp->t_mountp);
  88. ASSERT(buip != NULL);
  89. /*
  90. * Get a log_item_desc to point at the new item.
  91. */
  92. xfs_trans_add_item(tp, &buip->bui_item);
  93. return buip;
  94. }
  95. /* Set the map extent flags for this mapping. */
  96. static void
  97. xfs_trans_set_bmap_flags(
  98. struct xfs_map_extent *bmap,
  99. enum xfs_bmap_intent_type type,
  100. int whichfork,
  101. xfs_exntst_t state)
  102. {
  103. bmap->me_flags = 0;
  104. switch (type) {
  105. case XFS_BMAP_MAP:
  106. case XFS_BMAP_UNMAP:
  107. bmap->me_flags = type;
  108. break;
  109. default:
  110. ASSERT(0);
  111. }
  112. if (state == XFS_EXT_UNWRITTEN)
  113. bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
  114. if (whichfork == XFS_ATTR_FORK)
  115. bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
  116. }
  117. /* Log bmap updates in the intent item. */
  118. STATIC void
  119. xfs_bmap_update_log_item(
  120. struct xfs_trans *tp,
  121. void *intent,
  122. struct list_head *item)
  123. {
  124. struct xfs_bui_log_item *buip = intent;
  125. struct xfs_bmap_intent *bmap;
  126. uint next_extent;
  127. struct xfs_map_extent *map;
  128. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  129. tp->t_flags |= XFS_TRANS_DIRTY;
  130. set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
  131. /*
  132. * atomic_inc_return gives us the value after the increment;
  133. * we want to use it as an array index so we need to subtract 1 from
  134. * it.
  135. */
  136. next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
  137. ASSERT(next_extent < buip->bui_format.bui_nextents);
  138. map = &buip->bui_format.bui_extents[next_extent];
  139. map->me_owner = bmap->bi_owner->i_ino;
  140. map->me_startblock = bmap->bi_bmap.br_startblock;
  141. map->me_startoff = bmap->bi_bmap.br_startoff;
  142. map->me_len = bmap->bi_bmap.br_blockcount;
  143. xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
  144. bmap->bi_bmap.br_state);
  145. }
  146. /* Get an BUD so we can process all the deferred rmap updates. */
  147. STATIC void *
  148. xfs_bmap_update_create_done(
  149. struct xfs_trans *tp,
  150. void *intent,
  151. unsigned int count)
  152. {
  153. return xfs_trans_get_bud(tp, intent);
  154. }
  155. /* Process a deferred rmap update. */
  156. STATIC int
  157. xfs_bmap_update_finish_item(
  158. struct xfs_trans *tp,
  159. struct list_head *item,
  160. void *done_item,
  161. void **state)
  162. {
  163. struct xfs_bmap_intent *bmap;
  164. xfs_filblks_t count;
  165. int error;
  166. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  167. count = bmap->bi_bmap.br_blockcount;
  168. error = xfs_trans_log_finish_bmap_update(tp, done_item,
  169. bmap->bi_type,
  170. bmap->bi_owner, bmap->bi_whichfork,
  171. bmap->bi_bmap.br_startoff,
  172. bmap->bi_bmap.br_startblock,
  173. &count,
  174. bmap->bi_bmap.br_state);
  175. if (!error && count > 0) {
  176. ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
  177. bmap->bi_bmap.br_blockcount = count;
  178. return -EAGAIN;
  179. }
  180. kmem_free(bmap);
  181. return error;
  182. }
  183. /* Abort all pending BUIs. */
  184. STATIC void
  185. xfs_bmap_update_abort_intent(
  186. void *intent)
  187. {
  188. xfs_bui_release(intent);
  189. }
  190. /* Cancel a deferred rmap update. */
  191. STATIC void
  192. xfs_bmap_update_cancel_item(
  193. struct list_head *item)
  194. {
  195. struct xfs_bmap_intent *bmap;
  196. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  197. kmem_free(bmap);
  198. }
  199. static const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
  200. .type = XFS_DEFER_OPS_TYPE_BMAP,
  201. .max_items = XFS_BUI_MAX_FAST_EXTENTS,
  202. .diff_items = xfs_bmap_update_diff_items,
  203. .create_intent = xfs_bmap_update_create_intent,
  204. .abort_intent = xfs_bmap_update_abort_intent,
  205. .log_item = xfs_bmap_update_log_item,
  206. .create_done = xfs_bmap_update_create_done,
  207. .finish_item = xfs_bmap_update_finish_item,
  208. .cancel_item = xfs_bmap_update_cancel_item,
  209. };
  210. /* Register the deferred op type. */
  211. void
  212. xfs_bmap_update_init_defer_op(void)
  213. {
  214. xfs_defer_init_op_type(&xfs_bmap_update_defer_type);
  215. }