delayed-ref.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_DELAYED_REF_H
  6. #define BTRFS_DELAYED_REF_H
  7. #include <linux/refcount.h>
  8. /* these are the possible values of struct btrfs_delayed_ref_node->action */
  9. #define BTRFS_ADD_DELAYED_REF 1 /* add one backref to the tree */
  10. #define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */
  11. #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
  12. #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
  13. struct btrfs_delayed_ref_node {
  14. struct rb_node ref_node;
  15. /*
  16. * If action is BTRFS_ADD_DELAYED_REF, also link this node to
  17. * ref_head->ref_add_list, then we do not need to iterate the
  18. * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes.
  19. */
  20. struct list_head add_list;
  21. /* the starting bytenr of the extent */
  22. u64 bytenr;
  23. /* the size of the extent */
  24. u64 num_bytes;
  25. /* seq number to keep track of insertion order */
  26. u64 seq;
  27. /* ref count on this data structure */
  28. refcount_t refs;
  29. /*
  30. * how many refs is this entry adding or deleting. For
  31. * head refs, this may be a negative number because it is keeping
  32. * track of the total mods done to the reference count.
  33. * For individual refs, this will always be a positive number
  34. *
  35. * It may be more than one, since it is possible for a single
  36. * parent to have more than one ref on an extent
  37. */
  38. int ref_mod;
  39. unsigned int action:8;
  40. unsigned int type:8;
  41. /* is this node still in the rbtree? */
  42. unsigned int is_head:1;
  43. unsigned int in_tree:1;
  44. };
  45. struct btrfs_delayed_extent_op {
  46. struct btrfs_disk_key key;
  47. u8 level;
  48. bool update_key;
  49. bool update_flags;
  50. bool is_data;
  51. u64 flags_to_set;
  52. };
  53. /*
  54. * the head refs are used to hold a lock on a given extent, which allows us
  55. * to make sure that only one process is running the delayed refs
  56. * at a time for a single extent. They also store the sum of all the
  57. * reference count modifications we've queued up.
  58. */
  59. struct btrfs_delayed_ref_head {
  60. u64 bytenr;
  61. u64 num_bytes;
  62. refcount_t refs;
  63. /*
  64. * the mutex is held while running the refs, and it is also
  65. * held when checking the sum of reference modifications.
  66. */
  67. struct mutex mutex;
  68. spinlock_t lock;
  69. struct rb_root ref_tree;
  70. /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
  71. struct list_head ref_add_list;
  72. struct rb_node href_node;
  73. struct btrfs_delayed_extent_op *extent_op;
  74. /*
  75. * This is used to track the final ref_mod from all the refs associated
  76. * with this head ref, this is not adjusted as delayed refs are run,
  77. * this is meant to track if we need to do the csum accounting or not.
  78. */
  79. int total_ref_mod;
  80. /*
  81. * This is the current outstanding mod references for this bytenr. This
  82. * is used with lookup_extent_info to get an accurate reference count
  83. * for a bytenr, so it is adjusted as delayed refs are run so that any
  84. * on disk reference count + ref_mod is accurate.
  85. */
  86. int ref_mod;
  87. /*
  88. * For qgroup reserved space freeing.
  89. *
  90. * ref_root and reserved will be recorded after
  91. * BTRFS_ADD_DELAYED_EXTENT is called.
  92. * And will be used to free reserved qgroup space at
  93. * run_delayed_refs() time.
  94. */
  95. u64 qgroup_ref_root;
  96. u64 qgroup_reserved;
  97. /*
  98. * when a new extent is allocated, it is just reserved in memory
  99. * The actual extent isn't inserted into the extent allocation tree
  100. * until the delayed ref is processed. must_insert_reserved is
  101. * used to flag a delayed ref so the accounting can be updated
  102. * when a full insert is done.
  103. *
  104. * It is possible the extent will be freed before it is ever
  105. * inserted into the extent allocation tree. In this case
  106. * we need to update the in ram accounting to properly reflect
  107. * the free has happened.
  108. */
  109. unsigned int must_insert_reserved:1;
  110. unsigned int is_data:1;
  111. unsigned int is_system:1;
  112. unsigned int processing:1;
  113. };
  114. struct btrfs_delayed_tree_ref {
  115. struct btrfs_delayed_ref_node node;
  116. u64 root;
  117. u64 parent;
  118. int level;
  119. };
  120. struct btrfs_delayed_data_ref {
  121. struct btrfs_delayed_ref_node node;
  122. u64 root;
  123. u64 parent;
  124. u64 objectid;
  125. u64 offset;
  126. };
  127. struct btrfs_delayed_ref_root {
  128. /* head ref rbtree */
  129. struct rb_root href_root;
  130. /* dirty extent records */
  131. struct rb_root dirty_extent_root;
  132. /* this spin lock protects the rbtree and the entries inside */
  133. spinlock_t lock;
  134. /* how many delayed ref updates we've queued, used by the
  135. * throttling code
  136. */
  137. atomic_t num_entries;
  138. /* total number of head nodes in tree */
  139. unsigned long num_heads;
  140. /* total number of head nodes ready for processing */
  141. unsigned long num_heads_ready;
  142. u64 pending_csums;
  143. /*
  144. * set when the tree is flushing before a transaction commit,
  145. * used by the throttling code to decide if new updates need
  146. * to be run right away
  147. */
  148. int flushing;
  149. u64 run_delayed_start;
  150. /*
  151. * To make qgroup to skip given root.
  152. * This is for snapshot, as btrfs_qgroup_inherit() will manually
  153. * modify counters for snapshot and its source, so we should skip
  154. * the snapshot in new_root/old_roots or it will get calculated twice
  155. */
  156. u64 qgroup_to_skip;
  157. };
  158. extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
  159. extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
  160. extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
  161. extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
  162. int __init btrfs_delayed_ref_init(void);
  163. void __cold btrfs_delayed_ref_exit(void);
  164. static inline struct btrfs_delayed_extent_op *
  165. btrfs_alloc_delayed_extent_op(void)
  166. {
  167. return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
  168. }
  169. static inline void
  170. btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
  171. {
  172. if (op)
  173. kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
  174. }
  175. static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
  176. {
  177. WARN_ON(refcount_read(&ref->refs) == 0);
  178. if (refcount_dec_and_test(&ref->refs)) {
  179. WARN_ON(ref->in_tree);
  180. switch (ref->type) {
  181. case BTRFS_TREE_BLOCK_REF_KEY:
  182. case BTRFS_SHARED_BLOCK_REF_KEY:
  183. kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
  184. break;
  185. case BTRFS_EXTENT_DATA_REF_KEY:
  186. case BTRFS_SHARED_DATA_REF_KEY:
  187. kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
  188. break;
  189. default:
  190. BUG();
  191. }
  192. }
  193. }
  194. static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
  195. {
  196. if (refcount_dec_and_test(&head->refs))
  197. kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
  198. }
  199. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  200. u64 bytenr, u64 num_bytes, u64 parent,
  201. u64 ref_root, int level, int action,
  202. struct btrfs_delayed_extent_op *extent_op,
  203. int *old_ref_mod, int *new_ref_mod);
  204. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  205. u64 bytenr, u64 num_bytes,
  206. u64 parent, u64 ref_root,
  207. u64 owner, u64 offset, u64 reserved, int action,
  208. int *old_ref_mod, int *new_ref_mod);
  209. int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
  210. struct btrfs_trans_handle *trans,
  211. u64 bytenr, u64 num_bytes,
  212. struct btrfs_delayed_extent_op *extent_op);
  213. void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
  214. struct btrfs_delayed_ref_root *delayed_refs,
  215. struct btrfs_delayed_ref_head *head);
  216. struct btrfs_delayed_ref_head *
  217. btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  218. u64 bytenr);
  219. int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
  220. struct btrfs_delayed_ref_head *head);
  221. static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
  222. {
  223. mutex_unlock(&head->mutex);
  224. }
  225. struct btrfs_delayed_ref_head *
  226. btrfs_select_ref_head(struct btrfs_trans_handle *trans);
  227. int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
  228. /*
  229. * helper functions to cast a node into its container
  230. */
  231. static inline struct btrfs_delayed_tree_ref *
  232. btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
  233. {
  234. return container_of(node, struct btrfs_delayed_tree_ref, node);
  235. }
  236. static inline struct btrfs_delayed_data_ref *
  237. btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
  238. {
  239. return container_of(node, struct btrfs_delayed_data_ref, node);
  240. }
  241. #endif