delayed-ref.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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_cached 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. * when a new extent is allocated, it is just reserved in memory
  89. * The actual extent isn't inserted into the extent allocation tree
  90. * until the delayed ref is processed. must_insert_reserved is
  91. * used to flag a delayed ref so the accounting can be updated
  92. * when a full insert is done.
  93. *
  94. * It is possible the extent will be freed before it is ever
  95. * inserted into the extent allocation tree. In this case
  96. * we need to update the in ram accounting to properly reflect
  97. * the free has happened.
  98. */
  99. unsigned int must_insert_reserved:1;
  100. unsigned int is_data:1;
  101. unsigned int is_system:1;
  102. unsigned int processing:1;
  103. };
  104. struct btrfs_delayed_tree_ref {
  105. struct btrfs_delayed_ref_node node;
  106. u64 root;
  107. u64 parent;
  108. int level;
  109. };
  110. struct btrfs_delayed_data_ref {
  111. struct btrfs_delayed_ref_node node;
  112. u64 root;
  113. u64 parent;
  114. u64 objectid;
  115. u64 offset;
  116. };
  117. struct btrfs_delayed_ref_root {
  118. /* head ref rbtree */
  119. struct rb_root_cached href_root;
  120. /* dirty extent records */
  121. struct rb_root dirty_extent_root;
  122. /* this spin lock protects the rbtree and the entries inside */
  123. spinlock_t lock;
  124. /* how many delayed ref updates we've queued, used by the
  125. * throttling code
  126. */
  127. atomic_t num_entries;
  128. /* total number of head nodes in tree */
  129. unsigned long num_heads;
  130. /* total number of head nodes ready for processing */
  131. unsigned long num_heads_ready;
  132. u64 pending_csums;
  133. /*
  134. * set when the tree is flushing before a transaction commit,
  135. * used by the throttling code to decide if new updates need
  136. * to be run right away
  137. */
  138. int flushing;
  139. u64 run_delayed_start;
  140. /*
  141. * To make qgroup to skip given root.
  142. * This is for snapshot, as btrfs_qgroup_inherit() will manually
  143. * modify counters for snapshot and its source, so we should skip
  144. * the snapshot in new_root/old_roots or it will get calculated twice
  145. */
  146. u64 qgroup_to_skip;
  147. };
  148. enum btrfs_ref_type {
  149. BTRFS_REF_NOT_SET,
  150. BTRFS_REF_DATA,
  151. BTRFS_REF_METADATA,
  152. BTRFS_REF_LAST,
  153. };
  154. struct btrfs_data_ref {
  155. /* For EXTENT_DATA_REF */
  156. /* Root which refers to this data extent */
  157. u64 ref_root;
  158. /* Inode which refers to this data extent */
  159. u64 ino;
  160. /*
  161. * file_offset - extent_offset
  162. *
  163. * file_offset is the key.offset of the EXTENT_DATA key.
  164. * extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
  165. */
  166. u64 offset;
  167. };
  168. struct btrfs_tree_ref {
  169. /*
  170. * Level of this tree block
  171. *
  172. * Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
  173. */
  174. int level;
  175. /*
  176. * Root which refers to this tree block.
  177. *
  178. * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
  179. */
  180. u64 root;
  181. /* For non-skinny metadata, no special member needed */
  182. };
  183. struct btrfs_ref {
  184. enum btrfs_ref_type type;
  185. int action;
  186. /*
  187. * Whether this extent should go through qgroup record.
  188. *
  189. * Normally false, but for certain cases like delayed subtree scan,
  190. * setting this flag can hugely reduce qgroup overhead.
  191. */
  192. bool skip_qgroup;
  193. /*
  194. * Optional. For which root is this modification.
  195. * Mostly used for qgroup optimization.
  196. *
  197. * When unset, data/tree ref init code will populate it.
  198. * In certain cases, we're modifying reference for a different root.
  199. * E.g. COW fs tree blocks for balance.
  200. * In that case, tree_ref::root will be fs tree, but we're doing this
  201. * for reloc tree, then we should set @real_root to reloc tree.
  202. */
  203. u64 real_root;
  204. u64 bytenr;
  205. u64 len;
  206. /* Bytenr of the parent tree block */
  207. u64 parent;
  208. union {
  209. struct btrfs_data_ref data_ref;
  210. struct btrfs_tree_ref tree_ref;
  211. };
  212. };
  213. extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
  214. extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
  215. extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
  216. extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
  217. int __init btrfs_delayed_ref_init(void);
  218. void __cold btrfs_delayed_ref_exit(void);
  219. static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
  220. int action, u64 bytenr, u64 len, u64 parent)
  221. {
  222. generic_ref->action = action;
  223. generic_ref->bytenr = bytenr;
  224. generic_ref->len = len;
  225. generic_ref->parent = parent;
  226. }
  227. static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
  228. int level, u64 root)
  229. {
  230. /* If @real_root not set, use @root as fallback */
  231. if (!generic_ref->real_root)
  232. generic_ref->real_root = root;
  233. generic_ref->tree_ref.level = level;
  234. generic_ref->tree_ref.root = root;
  235. generic_ref->type = BTRFS_REF_METADATA;
  236. }
  237. static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
  238. u64 ref_root, u64 ino, u64 offset)
  239. {
  240. /* If @real_root not set, use @root as fallback */
  241. if (!generic_ref->real_root)
  242. generic_ref->real_root = ref_root;
  243. generic_ref->data_ref.ref_root = ref_root;
  244. generic_ref->data_ref.ino = ino;
  245. generic_ref->data_ref.offset = offset;
  246. generic_ref->type = BTRFS_REF_DATA;
  247. }
  248. static inline struct btrfs_delayed_extent_op *
  249. btrfs_alloc_delayed_extent_op(void)
  250. {
  251. return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
  252. }
  253. static inline void
  254. btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
  255. {
  256. if (op)
  257. kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
  258. }
  259. static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
  260. {
  261. WARN_ON(refcount_read(&ref->refs) == 0);
  262. if (refcount_dec_and_test(&ref->refs)) {
  263. WARN_ON(ref->in_tree);
  264. switch (ref->type) {
  265. case BTRFS_TREE_BLOCK_REF_KEY:
  266. case BTRFS_SHARED_BLOCK_REF_KEY:
  267. kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
  268. break;
  269. case BTRFS_EXTENT_DATA_REF_KEY:
  270. case BTRFS_SHARED_DATA_REF_KEY:
  271. kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
  272. break;
  273. default:
  274. BUG();
  275. }
  276. }
  277. }
  278. static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
  279. {
  280. if (refcount_dec_and_test(&head->refs))
  281. kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
  282. }
  283. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  284. struct btrfs_ref *generic_ref,
  285. struct btrfs_delayed_extent_op *extent_op,
  286. int *old_ref_mod, int *new_ref_mod);
  287. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  288. struct btrfs_ref *generic_ref,
  289. u64 reserved, int *old_ref_mod,
  290. int *new_ref_mod);
  291. int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
  292. u64 bytenr, u64 num_bytes,
  293. struct btrfs_delayed_extent_op *extent_op);
  294. void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
  295. struct btrfs_delayed_ref_root *delayed_refs,
  296. struct btrfs_delayed_ref_head *head);
  297. struct btrfs_delayed_ref_head *
  298. btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  299. u64 bytenr);
  300. int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
  301. struct btrfs_delayed_ref_head *head);
  302. static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
  303. {
  304. mutex_unlock(&head->mutex);
  305. }
  306. void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  307. struct btrfs_delayed_ref_head *head);
  308. struct btrfs_delayed_ref_head *btrfs_select_ref_head(
  309. struct btrfs_delayed_ref_root *delayed_refs);
  310. int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
  311. void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
  312. void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
  313. int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
  314. enum btrfs_reserve_flush_enum flush);
  315. void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
  316. struct btrfs_block_rsv *src,
  317. u64 num_bytes);
  318. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
  319. bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
  320. /*
  321. * helper functions to cast a node into its container
  322. */
  323. static inline struct btrfs_delayed_tree_ref *
  324. btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
  325. {
  326. return container_of(node, struct btrfs_delayed_tree_ref, node);
  327. }
  328. static inline struct btrfs_delayed_data_ref *
  329. btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
  330. {
  331. return container_of(node, struct btrfs_delayed_data_ref, node);
  332. }
  333. #endif