delayed-ref.h 9.3 KB

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