delayed-ref.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. /* these are the possible values of struct btrfs_delayed_ref_node->action */
  21. #define BTRFS_ADD_DELAYED_REF 1 /* add one backref to the tree */
  22. #define BTRFS_DROP_DELAYED_REF 2 /* delete one backref from the tree */
  23. #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
  24. #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
  25. /*
  26. * XXX: Qu: I really hate the design that ref_head and tree/data ref shares the
  27. * same ref_node structure.
  28. * Ref_head is in a higher logic level than tree/data ref, and duplicated
  29. * bytenr/num_bytes in ref_node is really a waste or memory, they should be
  30. * referred from ref_head.
  31. * This gets more disgusting after we use list to store tree/data ref in
  32. * ref_head. Must clean this mess up later.
  33. */
  34. struct btrfs_delayed_ref_node {
  35. /*
  36. * ref_head use rb tree, stored in ref_root->href.
  37. * indexed by bytenr
  38. */
  39. struct rb_node rb_node;
  40. /*data/tree ref use list, stored in ref_head->ref_list. */
  41. struct list_head list;
  42. /* the starting bytenr of the extent */
  43. u64 bytenr;
  44. /* the size of the extent */
  45. u64 num_bytes;
  46. /* seq number to keep track of insertion order */
  47. u64 seq;
  48. /* ref count on this data structure */
  49. atomic_t refs;
  50. /*
  51. * how many refs is this entry adding or deleting. For
  52. * head refs, this may be a negative number because it is keeping
  53. * track of the total mods done to the reference count.
  54. * For individual refs, this will always be a positive number
  55. *
  56. * It may be more than one, since it is possible for a single
  57. * parent to have more than one ref on an extent
  58. */
  59. int ref_mod;
  60. unsigned int action:8;
  61. unsigned int type:8;
  62. unsigned int no_quota:1;
  63. /* is this node still in the rbtree? */
  64. unsigned int is_head:1;
  65. unsigned int in_tree:1;
  66. };
  67. struct btrfs_delayed_extent_op {
  68. struct btrfs_disk_key key;
  69. u64 flags_to_set;
  70. int level;
  71. unsigned int update_key:1;
  72. unsigned int update_flags:1;
  73. unsigned int is_data:1;
  74. };
  75. /*
  76. * the head refs are used to hold a lock on a given extent, which allows us
  77. * to make sure that only one process is running the delayed refs
  78. * at a time for a single extent. They also store the sum of all the
  79. * reference count modifications we've queued up.
  80. */
  81. struct btrfs_delayed_ref_head {
  82. struct btrfs_delayed_ref_node node;
  83. /*
  84. * the mutex is held while running the refs, and it is also
  85. * held when checking the sum of reference modifications.
  86. */
  87. struct mutex mutex;
  88. spinlock_t lock;
  89. struct list_head ref_list;
  90. struct rb_node href_node;
  91. struct btrfs_delayed_extent_op *extent_op;
  92. /*
  93. * This is used to track the final ref_mod from all the refs associated
  94. * with this head ref, this is not adjusted as delayed refs are run,
  95. * this is meant to track if we need to do the csum accounting or not.
  96. */
  97. int total_ref_mod;
  98. /*
  99. * when a new extent is allocated, it is just reserved in memory
  100. * The actual extent isn't inserted into the extent allocation tree
  101. * until the delayed ref is processed. must_insert_reserved is
  102. * used to flag a delayed ref so the accounting can be updated
  103. * when a full insert is done.
  104. *
  105. * It is possible the extent will be freed before it is ever
  106. * inserted into the extent allocation tree. In this case
  107. * we need to update the in ram accounting to properly reflect
  108. * the free has happened.
  109. */
  110. unsigned int must_insert_reserved:1;
  111. unsigned int is_data: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 manully
  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 btrfs_delayed_ref_init(void);
  163. void 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(atomic_read(&ref->refs) == 0);
  178. if (atomic_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. case 0:
  190. kmem_cache_free(btrfs_delayed_ref_head_cachep, ref);
  191. break;
  192. default:
  193. BUG();
  194. }
  195. }
  196. }
  197. int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  198. struct btrfs_trans_handle *trans,
  199. u64 bytenr, u64 num_bytes, u64 parent,
  200. u64 ref_root, int level, int action,
  201. struct btrfs_delayed_extent_op *extent_op,
  202. int no_quota);
  203. int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  204. struct btrfs_trans_handle *trans,
  205. u64 bytenr, u64 num_bytes,
  206. u64 parent, u64 ref_root,
  207. u64 owner, u64 offset, int action,
  208. struct btrfs_delayed_extent_op *extent_op,
  209. int no_quota);
  210. int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
  211. struct btrfs_trans_handle *trans,
  212. u64 bytenr, u64 num_bytes,
  213. struct btrfs_delayed_extent_op *extent_op);
  214. void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
  215. struct btrfs_fs_info *fs_info,
  216. struct btrfs_delayed_ref_root *delayed_refs,
  217. struct btrfs_delayed_ref_head *head);
  218. struct btrfs_delayed_ref_head *
  219. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
  220. int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
  221. struct btrfs_delayed_ref_head *head);
  222. static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
  223. {
  224. mutex_unlock(&head->mutex);
  225. }
  226. struct btrfs_delayed_ref_head *
  227. btrfs_select_ref_head(struct btrfs_trans_handle *trans);
  228. int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
  229. struct btrfs_delayed_ref_root *delayed_refs,
  230. u64 seq);
  231. /*
  232. * a node might live in a head or a regular ref, this lets you
  233. * test for the proper type to use.
  234. */
  235. static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
  236. {
  237. return node->is_head;
  238. }
  239. /*
  240. * helper functions to cast a node into its container
  241. */
  242. static inline struct btrfs_delayed_tree_ref *
  243. btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
  244. {
  245. WARN_ON(btrfs_delayed_ref_is_head(node));
  246. return container_of(node, struct btrfs_delayed_tree_ref, node);
  247. }
  248. static inline struct btrfs_delayed_data_ref *
  249. btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
  250. {
  251. WARN_ON(btrfs_delayed_ref_is_head(node));
  252. return container_of(node, struct btrfs_delayed_data_ref, node);
  253. }
  254. static inline struct btrfs_delayed_ref_head *
  255. btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
  256. {
  257. WARN_ON(!btrfs_delayed_ref_is_head(node));
  258. return container_of(node, struct btrfs_delayed_ref_head, node);
  259. }
  260. #endif