xfs_trans_bmap.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2016 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_shared.h"
  23. #include "xfs_format.h"
  24. #include "xfs_log_format.h"
  25. #include "xfs_trans_resv.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_defer.h"
  28. #include "xfs_trans.h"
  29. #include "xfs_trans_priv.h"
  30. #include "xfs_bmap_item.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_inode.h"
  34. /*
  35. * This routine is called to allocate a "bmap update done"
  36. * log item.
  37. */
  38. struct xfs_bud_log_item *
  39. xfs_trans_get_bud(
  40. struct xfs_trans *tp,
  41. struct xfs_bui_log_item *buip)
  42. {
  43. struct xfs_bud_log_item *budp;
  44. budp = xfs_bud_init(tp->t_mountp, buip);
  45. xfs_trans_add_item(tp, &budp->bud_item);
  46. return budp;
  47. }
  48. /*
  49. * Finish an bmap update and log it to the BUD. Note that the
  50. * transaction is marked dirty regardless of whether the bmap update
  51. * succeeds or fails to support the BUI/BUD lifecycle rules.
  52. */
  53. int
  54. xfs_trans_log_finish_bmap_update(
  55. struct xfs_trans *tp,
  56. struct xfs_bud_log_item *budp,
  57. struct xfs_defer_ops *dop,
  58. enum xfs_bmap_intent_type type,
  59. struct xfs_inode *ip,
  60. int whichfork,
  61. xfs_fileoff_t startoff,
  62. xfs_fsblock_t startblock,
  63. xfs_filblks_t *blockcount,
  64. xfs_exntst_t state)
  65. {
  66. int error;
  67. error = xfs_bmap_finish_one(tp, dop, ip, type, whichfork, startoff,
  68. startblock, blockcount, state);
  69. /*
  70. * Mark the transaction dirty, even on error. This ensures the
  71. * transaction is aborted, which:
  72. *
  73. * 1.) releases the BUI and frees the BUD
  74. * 2.) shuts down the filesystem
  75. */
  76. tp->t_flags |= XFS_TRANS_DIRTY;
  77. budp->bud_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  78. return error;
  79. }
  80. /* Sort bmap intents by inode. */
  81. static int
  82. xfs_bmap_update_diff_items(
  83. void *priv,
  84. struct list_head *a,
  85. struct list_head *b)
  86. {
  87. struct xfs_bmap_intent *ba;
  88. struct xfs_bmap_intent *bb;
  89. ba = container_of(a, struct xfs_bmap_intent, bi_list);
  90. bb = container_of(b, struct xfs_bmap_intent, bi_list);
  91. return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
  92. }
  93. /* Get an BUI. */
  94. STATIC void *
  95. xfs_bmap_update_create_intent(
  96. struct xfs_trans *tp,
  97. unsigned int count)
  98. {
  99. struct xfs_bui_log_item *buip;
  100. ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
  101. ASSERT(tp != NULL);
  102. buip = xfs_bui_init(tp->t_mountp);
  103. ASSERT(buip != NULL);
  104. /*
  105. * Get a log_item_desc to point at the new item.
  106. */
  107. xfs_trans_add_item(tp, &buip->bui_item);
  108. return buip;
  109. }
  110. /* Set the map extent flags for this mapping. */
  111. static void
  112. xfs_trans_set_bmap_flags(
  113. struct xfs_map_extent *bmap,
  114. enum xfs_bmap_intent_type type,
  115. int whichfork,
  116. xfs_exntst_t state)
  117. {
  118. bmap->me_flags = 0;
  119. switch (type) {
  120. case XFS_BMAP_MAP:
  121. case XFS_BMAP_UNMAP:
  122. bmap->me_flags = type;
  123. break;
  124. default:
  125. ASSERT(0);
  126. }
  127. if (state == XFS_EXT_UNWRITTEN)
  128. bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
  129. if (whichfork == XFS_ATTR_FORK)
  130. bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
  131. }
  132. /* Log bmap updates in the intent item. */
  133. STATIC void
  134. xfs_bmap_update_log_item(
  135. struct xfs_trans *tp,
  136. void *intent,
  137. struct list_head *item)
  138. {
  139. struct xfs_bui_log_item *buip = intent;
  140. struct xfs_bmap_intent *bmap;
  141. uint next_extent;
  142. struct xfs_map_extent *map;
  143. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  144. tp->t_flags |= XFS_TRANS_DIRTY;
  145. buip->bui_item.li_desc->lid_flags |= XFS_LID_DIRTY;
  146. /*
  147. * atomic_inc_return gives us the value after the increment;
  148. * we want to use it as an array index so we need to subtract 1 from
  149. * it.
  150. */
  151. next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
  152. ASSERT(next_extent < buip->bui_format.bui_nextents);
  153. map = &buip->bui_format.bui_extents[next_extent];
  154. map->me_owner = bmap->bi_owner->i_ino;
  155. map->me_startblock = bmap->bi_bmap.br_startblock;
  156. map->me_startoff = bmap->bi_bmap.br_startoff;
  157. map->me_len = bmap->bi_bmap.br_blockcount;
  158. xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
  159. bmap->bi_bmap.br_state);
  160. }
  161. /* Get an BUD so we can process all the deferred rmap updates. */
  162. STATIC void *
  163. xfs_bmap_update_create_done(
  164. struct xfs_trans *tp,
  165. void *intent,
  166. unsigned int count)
  167. {
  168. return xfs_trans_get_bud(tp, intent);
  169. }
  170. /* Process a deferred rmap update. */
  171. STATIC int
  172. xfs_bmap_update_finish_item(
  173. struct xfs_trans *tp,
  174. struct xfs_defer_ops *dop,
  175. struct list_head *item,
  176. void *done_item,
  177. void **state)
  178. {
  179. struct xfs_bmap_intent *bmap;
  180. xfs_filblks_t count;
  181. int error;
  182. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  183. count = bmap->bi_bmap.br_blockcount;
  184. error = xfs_trans_log_finish_bmap_update(tp, done_item, dop,
  185. bmap->bi_type,
  186. bmap->bi_owner, bmap->bi_whichfork,
  187. bmap->bi_bmap.br_startoff,
  188. bmap->bi_bmap.br_startblock,
  189. &count,
  190. bmap->bi_bmap.br_state);
  191. if (!error && count > 0) {
  192. ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
  193. bmap->bi_bmap.br_blockcount = count;
  194. return -EAGAIN;
  195. }
  196. kmem_free(bmap);
  197. return error;
  198. }
  199. /* Abort all pending BUIs. */
  200. STATIC void
  201. xfs_bmap_update_abort_intent(
  202. void *intent)
  203. {
  204. xfs_bui_release(intent);
  205. }
  206. /* Cancel a deferred rmap update. */
  207. STATIC void
  208. xfs_bmap_update_cancel_item(
  209. struct list_head *item)
  210. {
  211. struct xfs_bmap_intent *bmap;
  212. bmap = container_of(item, struct xfs_bmap_intent, bi_list);
  213. kmem_free(bmap);
  214. }
  215. static const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
  216. .type = XFS_DEFER_OPS_TYPE_BMAP,
  217. .max_items = XFS_BUI_MAX_FAST_EXTENTS,
  218. .diff_items = xfs_bmap_update_diff_items,
  219. .create_intent = xfs_bmap_update_create_intent,
  220. .abort_intent = xfs_bmap_update_abort_intent,
  221. .log_item = xfs_bmap_update_log_item,
  222. .create_done = xfs_bmap_update_create_done,
  223. .finish_item = xfs_bmap_update_finish_item,
  224. .cancel_item = xfs_bmap_update_cancel_item,
  225. };
  226. /* Register the deferred op type. */
  227. void
  228. xfs_bmap_update_init_defer_op(void)
  229. {
  230. xfs_defer_init_op_type(&xfs_bmap_update_defer_type);
  231. }