delayed-ref.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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->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. struct btrfs_delayed_ref_node {
  26. struct rb_node rb_node;
  27. /* the starting bytenr of the extent */
  28. u64 bytenr;
  29. /* the size of the extent */
  30. u64 num_bytes;
  31. /* seq number to keep track of insertion order */
  32. u64 seq;
  33. /* ref count on this data structure */
  34. atomic_t refs;
  35. /*
  36. * how many refs is this entry adding or deleting. For
  37. * head refs, this may be a negative number because it is keeping
  38. * track of the total mods done to the reference count.
  39. * For individual refs, this will always be a positive number
  40. *
  41. * It may be more than one, since it is possible for a single
  42. * parent to have more than one ref on an extent
  43. */
  44. int ref_mod;
  45. unsigned int action:8;
  46. unsigned int type:8;
  47. /* is this node still in the rbtree? */
  48. unsigned int is_head:1;
  49. unsigned int in_tree:1;
  50. };
  51. struct btrfs_delayed_extent_op {
  52. struct btrfs_disk_key key;
  53. u64 flags_to_set;
  54. unsigned int update_key:1;
  55. unsigned int update_flags:1;
  56. unsigned int is_data:1;
  57. };
  58. /*
  59. * the head refs are used to hold a lock on a given extent, which allows us
  60. * to make sure that only one process is running the delayed refs
  61. * at a time for a single extent. They also store the sum of all the
  62. * reference count modifications we've queued up.
  63. */
  64. struct btrfs_delayed_ref_head {
  65. struct btrfs_delayed_ref_node node;
  66. /*
  67. * the mutex is held while running the refs, and it is also
  68. * held when checking the sum of reference modifications.
  69. */
  70. struct mutex mutex;
  71. struct list_head cluster;
  72. struct btrfs_delayed_extent_op *extent_op;
  73. /*
  74. * when a new extent is allocated, it is just reserved in memory
  75. * The actual extent isn't inserted into the extent allocation tree
  76. * until the delayed ref is processed. must_insert_reserved is
  77. * used to flag a delayed ref so the accounting can be updated
  78. * when a full insert is done.
  79. *
  80. * It is possible the extent will be freed before it is ever
  81. * inserted into the extent allocation tree. In this case
  82. * we need to update the in ram accounting to properly reflect
  83. * the free has happened.
  84. */
  85. unsigned int must_insert_reserved:1;
  86. unsigned int is_data:1;
  87. };
  88. struct btrfs_delayed_tree_ref {
  89. struct btrfs_delayed_ref_node node;
  90. u64 root;
  91. u64 parent;
  92. int level;
  93. };
  94. struct btrfs_delayed_data_ref {
  95. struct btrfs_delayed_ref_node node;
  96. u64 root;
  97. u64 parent;
  98. u64 objectid;
  99. u64 offset;
  100. };
  101. struct btrfs_delayed_ref_root {
  102. struct rb_root root;
  103. /* this spin lock protects the rbtree and the entries inside */
  104. spinlock_t lock;
  105. /* how many delayed ref updates we've queued, used by the
  106. * throttling code
  107. */
  108. unsigned long num_entries;
  109. /* total number of head nodes in tree */
  110. unsigned long num_heads;
  111. /* total number of head nodes ready for processing */
  112. unsigned long num_heads_ready;
  113. /*
  114. * set when the tree is flushing before a transaction commit,
  115. * used by the throttling code to decide if new updates need
  116. * to be run right away
  117. */
  118. int flushing;
  119. u64 run_delayed_start;
  120. /*
  121. * seq number of delayed refs. We need to know if a backref was being
  122. * added before the currently processed ref or afterwards.
  123. */
  124. u64 seq;
  125. /*
  126. * seq_list holds a list of all seq numbers that are currently being
  127. * added to the list. While walking backrefs (btrfs_find_all_roots,
  128. * qgroups), which might take some time, no newer ref must be processed,
  129. * as it might influence the outcome of the walk.
  130. */
  131. struct list_head seq_head;
  132. /*
  133. * when the only refs we have in the list must not be processed, we want
  134. * to wait for more refs to show up or for the end of backref walking.
  135. */
  136. wait_queue_head_t seq_wait;
  137. };
  138. static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
  139. {
  140. WARN_ON(atomic_read(&ref->refs) == 0);
  141. if (atomic_dec_and_test(&ref->refs)) {
  142. WARN_ON(ref->in_tree);
  143. kfree(ref);
  144. }
  145. }
  146. int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  147. struct btrfs_trans_handle *trans,
  148. u64 bytenr, u64 num_bytes, u64 parent,
  149. u64 ref_root, int level, int action,
  150. struct btrfs_delayed_extent_op *extent_op,
  151. int for_cow);
  152. int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  153. struct btrfs_trans_handle *trans,
  154. u64 bytenr, u64 num_bytes,
  155. u64 parent, u64 ref_root,
  156. u64 owner, u64 offset, int action,
  157. struct btrfs_delayed_extent_op *extent_op,
  158. int for_cow);
  159. int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
  160. struct btrfs_trans_handle *trans,
  161. u64 bytenr, u64 num_bytes,
  162. struct btrfs_delayed_extent_op *extent_op);
  163. struct btrfs_delayed_ref_head *
  164. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
  165. int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
  166. struct btrfs_delayed_ref_head *head);
  167. int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
  168. struct list_head *cluster, u64 search_start);
  169. struct seq_list {
  170. struct list_head list;
  171. u64 seq;
  172. };
  173. static inline u64 inc_delayed_seq(struct btrfs_delayed_ref_root *delayed_refs)
  174. {
  175. assert_spin_locked(&delayed_refs->lock);
  176. ++delayed_refs->seq;
  177. return delayed_refs->seq;
  178. }
  179. static inline void
  180. btrfs_get_delayed_seq(struct btrfs_delayed_ref_root *delayed_refs,
  181. struct seq_list *elem)
  182. {
  183. assert_spin_locked(&delayed_refs->lock);
  184. elem->seq = delayed_refs->seq;
  185. list_add_tail(&elem->list, &delayed_refs->seq_head);
  186. }
  187. static inline void
  188. btrfs_put_delayed_seq(struct btrfs_delayed_ref_root *delayed_refs,
  189. struct seq_list *elem)
  190. {
  191. spin_lock(&delayed_refs->lock);
  192. list_del(&elem->list);
  193. wake_up(&delayed_refs->seq_wait);
  194. spin_unlock(&delayed_refs->lock);
  195. }
  196. int btrfs_check_delayed_seq(struct btrfs_delayed_ref_root *delayed_refs,
  197. u64 seq);
  198. /*
  199. * delayed refs with a ref_seq > 0 must be held back during backref walking.
  200. * this only applies to items in one of the fs-trees. for_cow items never need
  201. * to be held back, so they won't get a ref_seq number.
  202. */
  203. static inline int need_ref_seq(int for_cow, u64 rootid)
  204. {
  205. if (for_cow)
  206. return 0;
  207. if (rootid == BTRFS_FS_TREE_OBJECTID)
  208. return 1;
  209. if ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID)
  210. return 1;
  211. return 0;
  212. }
  213. /*
  214. * a node might live in a head or a regular ref, this lets you
  215. * test for the proper type to use.
  216. */
  217. static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
  218. {
  219. return node->is_head;
  220. }
  221. /*
  222. * helper functions to cast a node into its container
  223. */
  224. static inline struct btrfs_delayed_tree_ref *
  225. btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
  226. {
  227. WARN_ON(btrfs_delayed_ref_is_head(node));
  228. return container_of(node, struct btrfs_delayed_tree_ref, node);
  229. }
  230. static inline struct btrfs_delayed_data_ref *
  231. btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
  232. {
  233. WARN_ON(btrfs_delayed_ref_is_head(node));
  234. return container_of(node, struct btrfs_delayed_data_ref, node);
  235. }
  236. static inline struct btrfs_delayed_ref_head *
  237. btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
  238. {
  239. WARN_ON(!btrfs_delayed_ref_is_head(node));
  240. return container_of(node, struct btrfs_delayed_ref_head, node);
  241. }
  242. #endif