delayed-ref.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. u64 flags_to_set;
  51. };
  52. /*
  53. * the head refs are used to hold a lock on a given extent, which allows us
  54. * to make sure that only one process is running the delayed refs
  55. * at a time for a single extent. They also store the sum of all the
  56. * reference count modifications we've queued up.
  57. */
  58. struct btrfs_delayed_ref_head {
  59. u64 bytenr;
  60. u64 num_bytes;
  61. refcount_t refs;
  62. /*
  63. * the mutex is held while running the refs, and it is also
  64. * held when checking the sum of reference modifications.
  65. */
  66. struct mutex mutex;
  67. spinlock_t lock;
  68. struct rb_root_cached ref_tree;
  69. /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
  70. struct list_head ref_add_list;
  71. struct rb_node href_node;
  72. struct btrfs_delayed_extent_op *extent_op;
  73. /*
  74. * This is used to track the final ref_mod from all the refs associated
  75. * with this head ref, this is not adjusted as delayed refs are run,
  76. * this is meant to track if we need to do the csum accounting or not.
  77. */
  78. int total_ref_mod;
  79. /*
  80. * This is the current outstanding mod references for this bytenr. This
  81. * is used with lookup_extent_info to get an accurate reference count
  82. * for a bytenr, so it is adjusted as delayed refs are run so that any
  83. * on disk reference count + ref_mod is accurate.
  84. */
  85. int ref_mod;
  86. /*
  87. * when a new extent is allocated, it is just reserved in memory
  88. * The actual extent isn't inserted into the extent allocation tree
  89. * until the delayed ref is processed. must_insert_reserved is
  90. * used to flag a delayed ref so the accounting can be updated
  91. * when a full insert is done.
  92. *
  93. * It is possible the extent will be freed before it is ever
  94. * inserted into the extent allocation tree. In this case
  95. * we need to update the in ram accounting to properly reflect
  96. * the free has happened.
  97. */
  98. unsigned int must_insert_reserved:1;
  99. unsigned int is_data:1;
  100. unsigned int is_system:1;
  101. unsigned int processing:1;
  102. };
  103. struct btrfs_delayed_tree_ref {
  104. struct btrfs_delayed_ref_node node;
  105. u64 root;
  106. u64 parent;
  107. int level;
  108. };
  109. struct btrfs_delayed_data_ref {
  110. struct btrfs_delayed_ref_node node;
  111. u64 root;
  112. u64 parent;
  113. u64 objectid;
  114. u64 offset;
  115. };
  116. enum btrfs_delayed_ref_flags {
  117. /* Indicate that we are flushing delayed refs for the commit */
  118. BTRFS_DELAYED_REFS_FLUSHING,
  119. };
  120. struct btrfs_delayed_ref_root {
  121. /* head ref rbtree */
  122. struct rb_root_cached href_root;
  123. /* dirty extent records */
  124. struct rb_root dirty_extent_root;
  125. /* this spin lock protects the rbtree and the entries inside */
  126. spinlock_t lock;
  127. /* how many delayed ref updates we've queued, used by the
  128. * throttling code
  129. */
  130. atomic_t num_entries;
  131. /* total number of head nodes in tree */
  132. unsigned long num_heads;
  133. /* total number of head nodes ready for processing */
  134. unsigned long num_heads_ready;
  135. u64 pending_csums;
  136. unsigned long flags;
  137. u64 run_delayed_start;
  138. /*
  139. * To make qgroup to skip given root.
  140. * This is for snapshot, as btrfs_qgroup_inherit() will manually
  141. * modify counters for snapshot and its source, so we should skip
  142. * the snapshot in new_root/old_roots or it will get calculated twice
  143. */
  144. u64 qgroup_to_skip;
  145. };
  146. enum btrfs_ref_type {
  147. BTRFS_REF_NOT_SET,
  148. BTRFS_REF_DATA,
  149. BTRFS_REF_METADATA,
  150. BTRFS_REF_LAST,
  151. };
  152. struct btrfs_data_ref {
  153. /* For EXTENT_DATA_REF */
  154. /* Original root this data extent belongs to */
  155. u64 owning_root;
  156. /* Inode which refers to this data extent */
  157. u64 ino;
  158. /*
  159. * file_offset - extent_offset
  160. *
  161. * file_offset is the key.offset of the EXTENT_DATA key.
  162. * extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
  163. */
  164. u64 offset;
  165. };
  166. struct btrfs_tree_ref {
  167. /*
  168. * Level of this tree block
  169. *
  170. * Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
  171. */
  172. int level;
  173. /*
  174. * Root which owns this tree block.
  175. *
  176. * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
  177. */
  178. u64 owning_root;
  179. /* For non-skinny metadata, no special member needed */
  180. };
  181. struct btrfs_ref {
  182. enum btrfs_ref_type type;
  183. int action;
  184. /*
  185. * Whether this extent should go through qgroup record.
  186. *
  187. * Normally false, but for certain cases like delayed subtree scan,
  188. * setting this flag can hugely reduce qgroup overhead.
  189. */
  190. bool skip_qgroup;
  191. #ifdef CONFIG_BTRFS_FS_REF_VERIFY
  192. /* Through which root is this modification. */
  193. u64 real_root;
  194. #endif
  195. u64 bytenr;
  196. u64 len;
  197. /* Bytenr of the parent tree block */
  198. u64 parent;
  199. union {
  200. struct btrfs_data_ref data_ref;
  201. struct btrfs_tree_ref tree_ref;
  202. };
  203. };
  204. extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
  205. extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
  206. extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
  207. extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
  208. int __init btrfs_delayed_ref_init(void);
  209. void __cold btrfs_delayed_ref_exit(void);
  210. static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
  211. int action, u64 bytenr, u64 len, u64 parent)
  212. {
  213. generic_ref->action = action;
  214. generic_ref->bytenr = bytenr;
  215. generic_ref->len = len;
  216. generic_ref->parent = parent;
  217. }
  218. static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
  219. int level, u64 root, u64 mod_root, bool skip_qgroup)
  220. {
  221. #ifdef CONFIG_BTRFS_FS_REF_VERIFY
  222. /* If @real_root not set, use @root as fallback */
  223. generic_ref->real_root = mod_root ?: root;
  224. #endif
  225. generic_ref->tree_ref.level = level;
  226. generic_ref->tree_ref.owning_root = root;
  227. generic_ref->type = BTRFS_REF_METADATA;
  228. if (skip_qgroup || !(is_fstree(root) &&
  229. (!mod_root || is_fstree(mod_root))))
  230. generic_ref->skip_qgroup = true;
  231. else
  232. generic_ref->skip_qgroup = false;
  233. }
  234. static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
  235. u64 ref_root, u64 ino, u64 offset, u64 mod_root,
  236. bool skip_qgroup)
  237. {
  238. #ifdef CONFIG_BTRFS_FS_REF_VERIFY
  239. /* If @real_root not set, use @root as fallback */
  240. generic_ref->real_root = mod_root ?: ref_root;
  241. #endif
  242. generic_ref->data_ref.owning_root = ref_root;
  243. generic_ref->data_ref.ino = ino;
  244. generic_ref->data_ref.offset = offset;
  245. generic_ref->type = BTRFS_REF_DATA;
  246. if (skip_qgroup || !(is_fstree(ref_root) &&
  247. (!mod_root || is_fstree(mod_root))))
  248. generic_ref->skip_qgroup = true;
  249. else
  250. generic_ref->skip_qgroup = false;
  251. }
  252. static inline struct btrfs_delayed_extent_op *
  253. btrfs_alloc_delayed_extent_op(void)
  254. {
  255. return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
  256. }
  257. static inline void
  258. btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
  259. {
  260. if (op)
  261. kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
  262. }
  263. static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
  264. {
  265. WARN_ON(refcount_read(&ref->refs) == 0);
  266. if (refcount_dec_and_test(&ref->refs)) {
  267. WARN_ON(ref->in_tree);
  268. switch (ref->type) {
  269. case BTRFS_TREE_BLOCK_REF_KEY:
  270. case BTRFS_SHARED_BLOCK_REF_KEY:
  271. kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
  272. break;
  273. case BTRFS_EXTENT_DATA_REF_KEY:
  274. case BTRFS_SHARED_DATA_REF_KEY:
  275. kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
  276. break;
  277. default:
  278. BUG();
  279. }
  280. }
  281. }
  282. static inline u64 btrfs_ref_head_to_space_flags(
  283. struct btrfs_delayed_ref_head *head_ref)
  284. {
  285. if (head_ref->is_data)
  286. return BTRFS_BLOCK_GROUP_DATA;
  287. else if (head_ref->is_system)
  288. return BTRFS_BLOCK_GROUP_SYSTEM;
  289. return BTRFS_BLOCK_GROUP_METADATA;
  290. }
  291. static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
  292. {
  293. if (refcount_dec_and_test(&head->refs))
  294. kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
  295. }
  296. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  297. struct btrfs_ref *generic_ref,
  298. struct btrfs_delayed_extent_op *extent_op);
  299. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  300. struct btrfs_ref *generic_ref,
  301. u64 reserved);
  302. int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
  303. u64 bytenr, u64 num_bytes,
  304. struct btrfs_delayed_extent_op *extent_op);
  305. void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
  306. struct btrfs_delayed_ref_root *delayed_refs,
  307. struct btrfs_delayed_ref_head *head);
  308. struct btrfs_delayed_ref_head *
  309. btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  310. u64 bytenr);
  311. int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
  312. struct btrfs_delayed_ref_head *head);
  313. static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
  314. {
  315. mutex_unlock(&head->mutex);
  316. }
  317. void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  318. struct btrfs_delayed_ref_head *head);
  319. struct btrfs_delayed_ref_head *btrfs_select_ref_head(
  320. struct btrfs_delayed_ref_root *delayed_refs);
  321. int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
  322. void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
  323. void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
  324. int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
  325. enum btrfs_reserve_flush_enum flush);
  326. void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
  327. u64 num_bytes);
  328. int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans);
  329. bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
  330. /*
  331. * helper functions to cast a node into its container
  332. */
  333. static inline struct btrfs_delayed_tree_ref *
  334. btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
  335. {
  336. return container_of(node, struct btrfs_delayed_tree_ref, node);
  337. }
  338. static inline struct btrfs_delayed_data_ref *
  339. btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
  340. {
  341. return container_of(node, struct btrfs_delayed_data_ref, node);
  342. }
  343. #endif