delayed-ref.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/types.h>
  8. #include <linux/refcount.h>
  9. #include <linux/list.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/mutex.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/slab.h>
  14. #include <uapi/linux/btrfs_tree.h>
  15. struct btrfs_trans_handle;
  16. struct btrfs_fs_info;
  17. /* these are the possible values of struct btrfs_delayed_ref_node->action */
  18. enum btrfs_delayed_ref_action {
  19. /* Add one backref to the tree */
  20. BTRFS_ADD_DELAYED_REF = 1,
  21. /* Delete one backref from the tree */
  22. BTRFS_DROP_DELAYED_REF,
  23. /* Record a full extent allocation */
  24. BTRFS_ADD_DELAYED_EXTENT,
  25. /* Not changing ref count on head ref */
  26. BTRFS_UPDATE_DELAYED_HEAD,
  27. } __packed;
  28. struct btrfs_data_ref {
  29. /* For EXTENT_DATA_REF */
  30. /* Inode which refers to this data extent */
  31. u64 objectid;
  32. /*
  33. * file_offset - extent_offset
  34. *
  35. * file_offset is the key.offset of the EXTENT_DATA key.
  36. * extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
  37. */
  38. u64 offset;
  39. };
  40. struct btrfs_tree_ref {
  41. /*
  42. * Level of this tree block.
  43. *
  44. * Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
  45. */
  46. int level;
  47. /* For non-skinny metadata, no special member needed */
  48. };
  49. struct btrfs_delayed_ref_node {
  50. struct rb_node ref_node;
  51. /*
  52. * If action is BTRFS_ADD_DELAYED_REF, also link this node to
  53. * ref_head->ref_add_list, then we do not need to iterate the
  54. * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes.
  55. */
  56. struct list_head add_list;
  57. /* the starting bytenr of the extent */
  58. u64 bytenr;
  59. /* the size of the extent */
  60. u64 num_bytes;
  61. /* seq number to keep track of insertion order */
  62. u64 seq;
  63. /* The ref_root for this ref */
  64. u64 ref_root;
  65. /*
  66. * The parent for this ref, if this isn't set the ref_root is the
  67. * reference owner.
  68. */
  69. u64 parent;
  70. /* ref count on this data structure */
  71. refcount_t refs;
  72. /*
  73. * how many refs is this entry adding or deleting. For
  74. * head refs, this may be a negative number because it is keeping
  75. * track of the total mods done to the reference count.
  76. * For individual refs, this will always be a positive number
  77. *
  78. * It may be more than one, since it is possible for a single
  79. * parent to have more than one ref on an extent
  80. */
  81. int ref_mod;
  82. unsigned int action:8;
  83. unsigned int type:8;
  84. union {
  85. struct btrfs_tree_ref tree_ref;
  86. struct btrfs_data_ref data_ref;
  87. };
  88. };
  89. struct btrfs_delayed_extent_op {
  90. struct btrfs_disk_key key;
  91. bool update_key;
  92. bool update_flags;
  93. u64 flags_to_set;
  94. };
  95. /*
  96. * the head refs are used to hold a lock on a given extent, which allows us
  97. * to make sure that only one process is running the delayed refs
  98. * at a time for a single extent. They also store the sum of all the
  99. * reference count modifications we've queued up.
  100. */
  101. struct btrfs_delayed_ref_head {
  102. u64 bytenr;
  103. u64 num_bytes;
  104. /*
  105. * For insertion into struct btrfs_delayed_ref_root::href_root.
  106. * Keep it in the same cache line as 'bytenr' for more efficient
  107. * searches in the rbtree.
  108. */
  109. struct rb_node href_node;
  110. /*
  111. * the mutex is held while running the refs, and it is also
  112. * held when checking the sum of reference modifications.
  113. */
  114. struct mutex mutex;
  115. refcount_t refs;
  116. /* Protects 'ref_tree' and 'ref_add_list'. */
  117. spinlock_t lock;
  118. struct rb_root_cached ref_tree;
  119. /* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
  120. struct list_head ref_add_list;
  121. struct btrfs_delayed_extent_op *extent_op;
  122. /*
  123. * This is used to track the final ref_mod from all the refs associated
  124. * with this head ref, this is not adjusted as delayed refs are run,
  125. * this is meant to track if we need to do the csum accounting or not.
  126. */
  127. int total_ref_mod;
  128. /*
  129. * This is the current outstanding mod references for this bytenr. This
  130. * is used with lookup_extent_info to get an accurate reference count
  131. * for a bytenr, so it is adjusted as delayed refs are run so that any
  132. * on disk reference count + ref_mod is accurate.
  133. */
  134. int ref_mod;
  135. /*
  136. * The root that triggered the allocation when must_insert_reserved is
  137. * set to true.
  138. */
  139. u64 owning_root;
  140. /*
  141. * Track reserved bytes when setting must_insert_reserved. On success
  142. * or cleanup, we will need to free the reservation.
  143. */
  144. u64 reserved_bytes;
  145. /* Tree block level, for metadata only. */
  146. u8 level;
  147. /*
  148. * when a new extent is allocated, it is just reserved in memory
  149. * The actual extent isn't inserted into the extent allocation tree
  150. * until the delayed ref is processed. must_insert_reserved is
  151. * used to flag a delayed ref so the accounting can be updated
  152. * when a full insert is done.
  153. *
  154. * It is possible the extent will be freed before it is ever
  155. * inserted into the extent allocation tree. In this case
  156. * we need to update the in ram accounting to properly reflect
  157. * the free has happened.
  158. */
  159. bool must_insert_reserved;
  160. bool is_data;
  161. bool is_system;
  162. bool processing;
  163. };
  164. enum btrfs_delayed_ref_flags {
  165. /* Indicate that we are flushing delayed refs for the commit */
  166. BTRFS_DELAYED_REFS_FLUSHING,
  167. };
  168. struct btrfs_delayed_ref_root {
  169. /* head ref rbtree */
  170. struct rb_root_cached href_root;
  171. /*
  172. * Track dirty extent records.
  173. * The keys correspond to the logical address of the extent ("bytenr")
  174. * right shifted by fs_info->sectorsize_bits. This is both to get a more
  175. * dense index space (optimizes xarray structure) and because indexes in
  176. * xarrays are of "unsigned long" type, meaning they are 32 bits wide on
  177. * 32 bits platforms, limiting the extent range to 4G which is too low
  178. * and makes it unusable (truncated index values) on 32 bits platforms.
  179. */
  180. struct xarray dirty_extents;
  181. /* this spin lock protects the rbtree and the entries inside */
  182. spinlock_t lock;
  183. /* how many delayed ref updates we've queued, used by the
  184. * throttling code
  185. */
  186. atomic_t num_entries;
  187. /* total number of head nodes in tree */
  188. unsigned long num_heads;
  189. /* total number of head nodes ready for processing */
  190. unsigned long num_heads_ready;
  191. u64 pending_csums;
  192. unsigned long flags;
  193. u64 run_delayed_start;
  194. /*
  195. * To make qgroup to skip given root.
  196. * This is for snapshot, as btrfs_qgroup_inherit() will manually
  197. * modify counters for snapshot and its source, so we should skip
  198. * the snapshot in new_root/old_roots or it will get calculated twice
  199. */
  200. u64 qgroup_to_skip;
  201. };
  202. enum btrfs_ref_type {
  203. BTRFS_REF_NOT_SET,
  204. BTRFS_REF_DATA,
  205. BTRFS_REF_METADATA,
  206. BTRFS_REF_LAST,
  207. } __packed;
  208. struct btrfs_ref {
  209. enum btrfs_ref_type type;
  210. enum btrfs_delayed_ref_action action;
  211. /*
  212. * Whether this extent should go through qgroup record.
  213. *
  214. * Normally false, but for certain cases like delayed subtree scan,
  215. * setting this flag can hugely reduce qgroup overhead.
  216. */
  217. bool skip_qgroup;
  218. #ifdef CONFIG_BTRFS_FS_REF_VERIFY
  219. /* Through which root is this modification. */
  220. u64 real_root;
  221. #endif
  222. u64 bytenr;
  223. u64 num_bytes;
  224. u64 owning_root;
  225. /*
  226. * The root that owns the reference for this reference, this will be set
  227. * or ->parent will be set, depending on what type of reference this is.
  228. */
  229. u64 ref_root;
  230. /* Bytenr of the parent tree block */
  231. u64 parent;
  232. union {
  233. struct btrfs_data_ref data_ref;
  234. struct btrfs_tree_ref tree_ref;
  235. };
  236. };
  237. extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
  238. extern struct kmem_cache *btrfs_delayed_ref_node_cachep;
  239. extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
  240. int __init btrfs_delayed_ref_init(void);
  241. void __cold btrfs_delayed_ref_exit(void);
  242. static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,
  243. int num_delayed_refs)
  244. {
  245. u64 num_bytes;
  246. num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);
  247. /*
  248. * We have to check the mount option here because we could be enabling
  249. * the free space tree for the first time and don't have the compat_ro
  250. * option set yet.
  251. *
  252. * We need extra reservations if we have the free space tree because
  253. * we'll have to modify that tree as well.
  254. */
  255. if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
  256. num_bytes *= 2;
  257. return num_bytes;
  258. }
  259. static inline u64 btrfs_calc_delayed_ref_csum_bytes(const struct btrfs_fs_info *fs_info,
  260. int num_csum_items)
  261. {
  262. /*
  263. * Deleting csum items does not result in new nodes/leaves and does not
  264. * require changing the free space tree, only the csum tree, so this is
  265. * all we need.
  266. */
  267. return btrfs_calc_metadata_size(fs_info, num_csum_items);
  268. }
  269. void btrfs_init_tree_ref(struct btrfs_ref *generic_ref, int level, u64 mod_root,
  270. bool skip_qgroup);
  271. void btrfs_init_data_ref(struct btrfs_ref *generic_ref, u64 ino, u64 offset,
  272. u64 mod_root, bool skip_qgroup);
  273. static inline struct btrfs_delayed_extent_op *
  274. btrfs_alloc_delayed_extent_op(void)
  275. {
  276. return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
  277. }
  278. static inline void
  279. btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
  280. {
  281. if (op)
  282. kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
  283. }
  284. void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref);
  285. static inline u64 btrfs_ref_head_to_space_flags(
  286. struct btrfs_delayed_ref_head *head_ref)
  287. {
  288. if (head_ref->is_data)
  289. return BTRFS_BLOCK_GROUP_DATA;
  290. else if (head_ref->is_system)
  291. return BTRFS_BLOCK_GROUP_SYSTEM;
  292. return BTRFS_BLOCK_GROUP_METADATA;
  293. }
  294. static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
  295. {
  296. if (refcount_dec_and_test(&head->refs))
  297. kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
  298. }
  299. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  300. struct btrfs_ref *generic_ref,
  301. struct btrfs_delayed_extent_op *extent_op);
  302. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  303. struct btrfs_ref *generic_ref,
  304. u64 reserved);
  305. int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
  306. u64 bytenr, u64 num_bytes, u8 level,
  307. struct btrfs_delayed_extent_op *extent_op);
  308. void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
  309. struct btrfs_delayed_ref_root *delayed_refs,
  310. struct btrfs_delayed_ref_head *head);
  311. struct btrfs_delayed_ref_head *
  312. btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  313. u64 bytenr);
  314. int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
  315. struct btrfs_delayed_ref_head *head);
  316. static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
  317. {
  318. mutex_unlock(&head->mutex);
  319. }
  320. void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
  321. struct btrfs_delayed_ref_head *head);
  322. struct btrfs_delayed_ref_head *btrfs_select_ref_head(
  323. struct btrfs_delayed_ref_root *delayed_refs);
  324. int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
  325. void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr_refs, int nr_csums);
  326. void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
  327. void btrfs_inc_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
  328. void btrfs_dec_delayed_refs_rsv_bg_inserts(struct btrfs_fs_info *fs_info);
  329. void btrfs_inc_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
  330. void btrfs_dec_delayed_refs_rsv_bg_updates(struct btrfs_fs_info *fs_info);
  331. int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
  332. enum btrfs_reserve_flush_enum flush);
  333. bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
  334. bool btrfs_find_delayed_tree_ref(struct btrfs_delayed_ref_head *head,
  335. u64 root, u64 parent);
  336. static inline u64 btrfs_delayed_ref_owner(struct btrfs_delayed_ref_node *node)
  337. {
  338. if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  339. node->type == BTRFS_SHARED_DATA_REF_KEY)
  340. return node->data_ref.objectid;
  341. return node->tree_ref.level;
  342. }
  343. static inline u64 btrfs_delayed_ref_offset(struct btrfs_delayed_ref_node *node)
  344. {
  345. if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
  346. node->type == BTRFS_SHARED_DATA_REF_KEY)
  347. return node->data_ref.offset;
  348. return 0;
  349. }
  350. static inline u8 btrfs_ref_type(struct btrfs_ref *ref)
  351. {
  352. ASSERT(ref->type == BTRFS_REF_DATA || ref->type == BTRFS_REF_METADATA);
  353. if (ref->type == BTRFS_REF_DATA) {
  354. if (ref->parent)
  355. return BTRFS_SHARED_DATA_REF_KEY;
  356. else
  357. return BTRFS_EXTENT_DATA_REF_KEY;
  358. } else {
  359. if (ref->parent)
  360. return BTRFS_SHARED_BLOCK_REF_KEY;
  361. else
  362. return BTRFS_TREE_BLOCK_REF_KEY;
  363. }
  364. return 0;
  365. }
  366. #endif