delayed-ref.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /* ref count on this data structure */
  32. atomic_t refs;
  33. /*
  34. * how many refs is this entry adding or deleting. For
  35. * head refs, this may be a negative number because it is keeping
  36. * track of the total mods done to the reference count.
  37. * For individual refs, this will always be a positive number
  38. *
  39. * It may be more than one, since it is possible for a single
  40. * parent to have more than one ref on an extent
  41. */
  42. int ref_mod;
  43. unsigned int action:8;
  44. unsigned int type:8;
  45. /* is this node still in the rbtree? */
  46. unsigned int is_head:1;
  47. unsigned int in_tree:1;
  48. };
  49. struct btrfs_delayed_extent_op {
  50. struct btrfs_disk_key key;
  51. u64 flags_to_set;
  52. unsigned int update_key:1;
  53. unsigned int update_flags:1;
  54. unsigned int is_data:1;
  55. };
  56. /*
  57. * the head refs are used to hold a lock on a given extent, which allows us
  58. * to make sure that only one process is running the delayed refs
  59. * at a time for a single extent. They also store the sum of all the
  60. * reference count modifications we've queued up.
  61. */
  62. struct btrfs_delayed_ref_head {
  63. struct btrfs_delayed_ref_node node;
  64. /*
  65. * the mutex is held while running the refs, and it is also
  66. * held when checking the sum of reference modifications.
  67. */
  68. struct mutex mutex;
  69. struct list_head cluster;
  70. struct btrfs_delayed_extent_op *extent_op;
  71. /*
  72. * when a new extent is allocated, it is just reserved in memory
  73. * The actual extent isn't inserted into the extent allocation tree
  74. * until the delayed ref is processed. must_insert_reserved is
  75. * used to flag a delayed ref so the accounting can be updated
  76. * when a full insert is done.
  77. *
  78. * It is possible the extent will be freed before it is ever
  79. * inserted into the extent allocation tree. In this case
  80. * we need to update the in ram accounting to properly reflect
  81. * the free has happened.
  82. */
  83. unsigned int must_insert_reserved:1;
  84. unsigned int is_data:1;
  85. };
  86. struct btrfs_delayed_tree_ref {
  87. struct btrfs_delayed_ref_node node;
  88. union {
  89. u64 root;
  90. u64 parent;
  91. };
  92. int level;
  93. };
  94. struct btrfs_delayed_data_ref {
  95. struct btrfs_delayed_ref_node node;
  96. union {
  97. u64 root;
  98. u64 parent;
  99. };
  100. u64 objectid;
  101. u64 offset;
  102. };
  103. struct btrfs_delayed_ref_root {
  104. struct rb_root root;
  105. /* this spin lock protects the rbtree and the entries inside */
  106. spinlock_t lock;
  107. /* how many delayed ref updates we've queued, used by the
  108. * throttling code
  109. */
  110. unsigned long num_entries;
  111. /* total number of head nodes in tree */
  112. unsigned long num_heads;
  113. /* total number of head nodes ready for processing */
  114. unsigned long num_heads_ready;
  115. /*
  116. * set when the tree is flushing before a transaction commit,
  117. * used by the throttling code to decide if new updates need
  118. * to be run right away
  119. */
  120. int flushing;
  121. u64 run_delayed_start;
  122. };
  123. static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
  124. {
  125. WARN_ON(atomic_read(&ref->refs) == 0);
  126. if (atomic_dec_and_test(&ref->refs)) {
  127. WARN_ON(ref->in_tree);
  128. kfree(ref);
  129. }
  130. }
  131. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  132. u64 bytenr, u64 num_bytes, u64 parent,
  133. u64 ref_root, int level, int action,
  134. struct btrfs_delayed_extent_op *extent_op);
  135. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  136. u64 bytenr, u64 num_bytes,
  137. u64 parent, u64 ref_root,
  138. u64 owner, u64 offset, int action,
  139. struct btrfs_delayed_extent_op *extent_op);
  140. int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
  141. u64 bytenr, u64 num_bytes,
  142. struct btrfs_delayed_extent_op *extent_op);
  143. struct btrfs_delayed_ref_head *
  144. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr);
  145. int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
  146. struct btrfs_delayed_ref_head *head);
  147. int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
  148. struct list_head *cluster, u64 search_start);
  149. /*
  150. * a node might live in a head or a regular ref, this lets you
  151. * test for the proper type to use.
  152. */
  153. static int btrfs_delayed_ref_is_head(struct btrfs_delayed_ref_node *node)
  154. {
  155. return node->is_head;
  156. }
  157. /*
  158. * helper functions to cast a node into its container
  159. */
  160. static inline struct btrfs_delayed_tree_ref *
  161. btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
  162. {
  163. WARN_ON(btrfs_delayed_ref_is_head(node));
  164. return container_of(node, struct btrfs_delayed_tree_ref, node);
  165. }
  166. static inline struct btrfs_delayed_data_ref *
  167. btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
  168. {
  169. WARN_ON(btrfs_delayed_ref_is_head(node));
  170. return container_of(node, struct btrfs_delayed_data_ref, node);
  171. }
  172. static inline struct btrfs_delayed_ref_head *
  173. btrfs_delayed_node_to_head(struct btrfs_delayed_ref_node *node)
  174. {
  175. WARN_ON(!btrfs_delayed_ref_is_head(node));
  176. return container_of(node, struct btrfs_delayed_ref_head, node);
  177. }
  178. #endif