xref: /openbmc/linux/fs/btrfs/delayed-ref.h (revision a7ddeeb0)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
256bec294SChris Mason /*
356bec294SChris Mason  * Copyright (C) 2008 Oracle.  All rights reserved.
456bec294SChris Mason  */
59888c340SDavid Sterba 
69888c340SDavid Sterba #ifndef BTRFS_DELAYED_REF_H
79888c340SDavid Sterba #define BTRFS_DELAYED_REF_H
856bec294SChris Mason 
96df8cdf5SElena Reshetova #include <linux/refcount.h>
106df8cdf5SElena Reshetova 
1144a075bdSWang Sheng-Hui /* these are the possible values of struct btrfs_delayed_ref_node->action */
1256bec294SChris Mason #define BTRFS_ADD_DELAYED_REF    1 /* add one backref to the tree */
1356bec294SChris Mason #define BTRFS_DROP_DELAYED_REF   2 /* delete one backref from the tree */
1456bec294SChris Mason #define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
151a81af4dSChris Mason #define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
1656bec294SChris Mason 
1756bec294SChris Mason struct btrfs_delayed_ref_node {
180e0adbcfSJosef Bacik 	struct rb_node ref_node;
191d57ee94SWang Xiaoguang 	/*
201d57ee94SWang Xiaoguang 	 * If action is BTRFS_ADD_DELAYED_REF, also link this node to
211d57ee94SWang Xiaoguang 	 * ref_head->ref_add_list, then we do not need to iterate the
221d57ee94SWang Xiaoguang 	 * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes.
231d57ee94SWang Xiaoguang 	 */
241d57ee94SWang Xiaoguang 	struct list_head add_list;
25c6fc2454SQu Wenruo 
2656bec294SChris Mason 	/* the starting bytenr of the extent */
2756bec294SChris Mason 	u64 bytenr;
2856bec294SChris Mason 
2956bec294SChris Mason 	/* the size of the extent */
3056bec294SChris Mason 	u64 num_bytes;
3156bec294SChris Mason 
3200f04b88SArne Jansen 	/* seq number to keep track of insertion order */
3300f04b88SArne Jansen 	u64 seq;
3400f04b88SArne Jansen 
3556bec294SChris Mason 	/* ref count on this data structure */
366df8cdf5SElena Reshetova 	refcount_t refs;
3756bec294SChris Mason 
3856bec294SChris Mason 	/*
3956bec294SChris Mason 	 * how many refs is this entry adding or deleting.  For
4056bec294SChris Mason 	 * head refs, this may be a negative number because it is keeping
4156bec294SChris Mason 	 * track of the total mods done to the reference count.
4256bec294SChris Mason 	 * For individual refs, this will always be a positive number
4356bec294SChris Mason 	 *
4456bec294SChris Mason 	 * It may be more than one, since it is possible for a single
4556bec294SChris Mason 	 * parent to have more than one ref on an extent
4656bec294SChris Mason 	 */
4756bec294SChris Mason 	int ref_mod;
4856bec294SChris Mason 
495d4f98a2SYan Zheng 	unsigned int action:8;
505d4f98a2SYan Zheng 	unsigned int type:8;
5156bec294SChris Mason };
5256bec294SChris Mason 
535d4f98a2SYan Zheng struct btrfs_delayed_extent_op {
545d4f98a2SYan Zheng 	struct btrfs_disk_key key;
5535b3ad50SDavid Sterba 	u8 level;
5635b3ad50SDavid Sterba 	bool update_key;
5735b3ad50SDavid Sterba 	bool update_flags;
585d4f98a2SYan Zheng 	u64 flags_to_set;
595d4f98a2SYan Zheng };
605d4f98a2SYan Zheng 
6156bec294SChris Mason /*
6256bec294SChris Mason  * the head refs are used to hold a lock on a given extent, which allows us
6356bec294SChris Mason  * to make sure that only one process is running the delayed refs
6456bec294SChris Mason  * at a time for a single extent.  They also store the sum of all the
6556bec294SChris Mason  * reference count modifications we've queued up.
6656bec294SChris Mason  */
6756bec294SChris Mason struct btrfs_delayed_ref_head {
68d278850eSJosef Bacik 	u64 bytenr;
69d278850eSJosef Bacik 	u64 num_bytes;
70315dd5ccSFilipe Manana 	/*
71315dd5ccSFilipe Manana 	 * For insertion into struct btrfs_delayed_ref_root::href_root.
72315dd5ccSFilipe Manana 	 * Keep it in the same cache line as 'bytenr' for more efficient
73315dd5ccSFilipe Manana 	 * searches in the rbtree.
74315dd5ccSFilipe Manana 	 */
75315dd5ccSFilipe Manana 	struct rb_node href_node;
7656bec294SChris Mason 	/*
7756bec294SChris Mason 	 * the mutex is held while running the refs, and it is also
7856bec294SChris Mason 	 * held when checking the sum of reference modifications.
7956bec294SChris Mason 	 */
8056bec294SChris Mason 	struct mutex mutex;
8156bec294SChris Mason 
82315dd5ccSFilipe Manana 	refcount_t refs;
83315dd5ccSFilipe Manana 
84315dd5ccSFilipe Manana 	/* Protects 'ref_tree' and 'ref_add_list'. */
85d7df2c79SJosef Bacik 	spinlock_t lock;
86e3d03965SLiu Bo 	struct rb_root_cached ref_tree;
871d57ee94SWang Xiaoguang 	/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
881d57ee94SWang Xiaoguang 	struct list_head ref_add_list;
89c3e69d58SChris Mason 
905d4f98a2SYan Zheng 	struct btrfs_delayed_extent_op *extent_op;
911262133bSJosef Bacik 
921262133bSJosef Bacik 	/*
931262133bSJosef Bacik 	 * This is used to track the final ref_mod from all the refs associated
941262133bSJosef Bacik 	 * with this head ref, this is not adjusted as delayed refs are run,
951262133bSJosef Bacik 	 * this is meant to track if we need to do the csum accounting or not.
961262133bSJosef Bacik 	 */
971262133bSJosef Bacik 	int total_ref_mod;
981262133bSJosef Bacik 
9956bec294SChris Mason 	/*
100d278850eSJosef Bacik 	 * This is the current outstanding mod references for this bytenr.  This
101d278850eSJosef Bacik 	 * is used with lookup_extent_info to get an accurate reference count
102d278850eSJosef Bacik 	 * for a bytenr, so it is adjusted as delayed refs are run so that any
103d278850eSJosef Bacik 	 * on disk reference count + ref_mod is accurate.
104d278850eSJosef Bacik 	 */
105d278850eSJosef Bacik 	int ref_mod;
106d278850eSJosef Bacik 
107d278850eSJosef Bacik 	/*
10856bec294SChris Mason 	 * when a new extent is allocated, it is just reserved in memory
10956bec294SChris Mason 	 * The actual extent isn't inserted into the extent allocation tree
11056bec294SChris Mason 	 * until the delayed ref is processed.  must_insert_reserved is
11156bec294SChris Mason 	 * used to flag a delayed ref so the accounting can be updated
11256bec294SChris Mason 	 * when a full insert is done.
11356bec294SChris Mason 	 *
11456bec294SChris Mason 	 * It is possible the extent will be freed before it is ever
11556bec294SChris Mason 	 * inserted into the extent allocation tree.  In this case
11656bec294SChris Mason 	 * we need to update the in ram accounting to properly reflect
11756bec294SChris Mason 	 * the free has happened.
11856bec294SChris Mason 	 */
119*61c681feSFilipe Manana 	bool must_insert_reserved;
120*61c681feSFilipe Manana 	bool is_data;
121*61c681feSFilipe Manana 	bool is_system;
122*61c681feSFilipe Manana 	bool processing;
12356bec294SChris Mason };
12456bec294SChris Mason 
1255d4f98a2SYan Zheng struct btrfs_delayed_tree_ref {
12656bec294SChris Mason 	struct btrfs_delayed_ref_node node;
12756bec294SChris Mason 	u64 root;
1285d4f98a2SYan Zheng 	u64 parent;
1295d4f98a2SYan Zheng 	int level;
1305d4f98a2SYan Zheng };
13156bec294SChris Mason 
1325d4f98a2SYan Zheng struct btrfs_delayed_data_ref {
1335d4f98a2SYan Zheng 	struct btrfs_delayed_ref_node node;
1345d4f98a2SYan Zheng 	u64 root;
1355d4f98a2SYan Zheng 	u64 parent;
1365d4f98a2SYan Zheng 	u64 objectid;
1375d4f98a2SYan Zheng 	u64 offset;
13856bec294SChris Mason };
13956bec294SChris Mason 
140e19eb11fSJosef Bacik enum btrfs_delayed_ref_flags {
141e19eb11fSJosef Bacik 	/* Indicate that we are flushing delayed refs for the commit */
142e19eb11fSJosef Bacik 	BTRFS_DELAYED_REFS_FLUSHING,
143e19eb11fSJosef Bacik };
144e19eb11fSJosef Bacik 
14556bec294SChris Mason struct btrfs_delayed_ref_root {
146c46effa6SLiu Bo 	/* head ref rbtree */
1475c9d028bSLiu Bo 	struct rb_root_cached href_root;
148c46effa6SLiu Bo 
1493368d001SQu Wenruo 	/* dirty extent records */
1503368d001SQu Wenruo 	struct rb_root dirty_extent_root;
1513368d001SQu Wenruo 
15256bec294SChris Mason 	/* this spin lock protects the rbtree and the entries inside */
15356bec294SChris Mason 	spinlock_t lock;
15456bec294SChris Mason 
15556bec294SChris Mason 	/* how many delayed ref updates we've queued, used by the
15656bec294SChris Mason 	 * throttling code
15756bec294SChris Mason 	 */
158d7df2c79SJosef Bacik 	atomic_t num_entries;
15956bec294SChris Mason 
160c3e69d58SChris Mason 	/* total number of head nodes in tree */
161c3e69d58SChris Mason 	unsigned long num_heads;
162c3e69d58SChris Mason 
163c3e69d58SChris Mason 	/* total number of head nodes ready for processing */
164c3e69d58SChris Mason 	unsigned long num_heads_ready;
165c3e69d58SChris Mason 
1661262133bSJosef Bacik 	u64 pending_csums;
1671262133bSJosef Bacik 
168e19eb11fSJosef Bacik 	unsigned long flags;
169c3e69d58SChris Mason 
170c3e69d58SChris Mason 	u64 run_delayed_start;
1719086db86SQu Wenruo 
1729086db86SQu Wenruo 	/*
1739086db86SQu Wenruo 	 * To make qgroup to skip given root.
17401327610SNicholas D Steeves 	 * This is for snapshot, as btrfs_qgroup_inherit() will manually
1759086db86SQu Wenruo 	 * modify counters for snapshot and its source, so we should skip
1769086db86SQu Wenruo 	 * the snapshot in new_root/old_roots or it will get calculated twice
1779086db86SQu Wenruo 	 */
1789086db86SQu Wenruo 	u64 qgroup_to_skip;
17956bec294SChris Mason };
18056bec294SChris Mason 
181b28b1f0cSQu Wenruo enum btrfs_ref_type {
182b28b1f0cSQu Wenruo 	BTRFS_REF_NOT_SET,
183b28b1f0cSQu Wenruo 	BTRFS_REF_DATA,
184b28b1f0cSQu Wenruo 	BTRFS_REF_METADATA,
185b28b1f0cSQu Wenruo 	BTRFS_REF_LAST,
186b28b1f0cSQu Wenruo };
187b28b1f0cSQu Wenruo 
188b28b1f0cSQu Wenruo struct btrfs_data_ref {
189b28b1f0cSQu Wenruo 	/* For EXTENT_DATA_REF */
190b28b1f0cSQu Wenruo 
191113479d5SNikolay Borisov 	/* Original root this data extent belongs to */
192113479d5SNikolay Borisov 	u64 owning_root;
193b28b1f0cSQu Wenruo 
194b28b1f0cSQu Wenruo 	/* Inode which refers to this data extent */
195b28b1f0cSQu Wenruo 	u64 ino;
196b28b1f0cSQu Wenruo 
197b28b1f0cSQu Wenruo 	/*
198b28b1f0cSQu Wenruo 	 * file_offset - extent_offset
199b28b1f0cSQu Wenruo 	 *
200b28b1f0cSQu Wenruo 	 * file_offset is the key.offset of the EXTENT_DATA key.
201b28b1f0cSQu Wenruo 	 * extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
202b28b1f0cSQu Wenruo 	 */
203b28b1f0cSQu Wenruo 	u64 offset;
204b28b1f0cSQu Wenruo };
205b28b1f0cSQu Wenruo 
206b28b1f0cSQu Wenruo struct btrfs_tree_ref {
207b28b1f0cSQu Wenruo 	/*
208b28b1f0cSQu Wenruo 	 * Level of this tree block
209b28b1f0cSQu Wenruo 	 *
210b28b1f0cSQu Wenruo 	 * Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
211b28b1f0cSQu Wenruo 	 */
212b28b1f0cSQu Wenruo 	int level;
213b28b1f0cSQu Wenruo 
214b28b1f0cSQu Wenruo 	/*
215113479d5SNikolay Borisov 	 * Root which owns this tree block.
216b28b1f0cSQu Wenruo 	 *
217b28b1f0cSQu Wenruo 	 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
218b28b1f0cSQu Wenruo 	 */
219113479d5SNikolay Borisov 	u64 owning_root;
220b28b1f0cSQu Wenruo 
221b28b1f0cSQu Wenruo 	/* For non-skinny metadata, no special member needed */
222b28b1f0cSQu Wenruo };
223b28b1f0cSQu Wenruo 
224b28b1f0cSQu Wenruo struct btrfs_ref {
225b28b1f0cSQu Wenruo 	enum btrfs_ref_type type;
226b28b1f0cSQu Wenruo 	int action;
227b28b1f0cSQu Wenruo 
228b28b1f0cSQu Wenruo 	/*
229b28b1f0cSQu Wenruo 	 * Whether this extent should go through qgroup record.
230b28b1f0cSQu Wenruo 	 *
231b28b1f0cSQu Wenruo 	 * Normally false, but for certain cases like delayed subtree scan,
232b28b1f0cSQu Wenruo 	 * setting this flag can hugely reduce qgroup overhead.
233b28b1f0cSQu Wenruo 	 */
234b28b1f0cSQu Wenruo 	bool skip_qgroup;
235b28b1f0cSQu Wenruo 
236eed2037fSNikolay Borisov #ifdef CONFIG_BTRFS_FS_REF_VERIFY
237eed2037fSNikolay Borisov 	/* Through which root is this modification. */
238b28b1f0cSQu Wenruo 	u64 real_root;
239eed2037fSNikolay Borisov #endif
240b28b1f0cSQu Wenruo 	u64 bytenr;
241b28b1f0cSQu Wenruo 	u64 len;
242b28b1f0cSQu Wenruo 
243b28b1f0cSQu Wenruo 	/* Bytenr of the parent tree block */
244b28b1f0cSQu Wenruo 	u64 parent;
245b28b1f0cSQu Wenruo 	union {
246b28b1f0cSQu Wenruo 		struct btrfs_data_ref data_ref;
247b28b1f0cSQu Wenruo 		struct btrfs_tree_ref tree_ref;
248b28b1f0cSQu Wenruo 	};
249b28b1f0cSQu Wenruo };
250b28b1f0cSQu Wenruo 
25178a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
25278a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
25378a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
25478a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
25578a6184aSMiao Xie 
256f5c29bd9SLiu Bo int __init btrfs_delayed_ref_init(void);
257e67c718bSDavid Sterba void __cold btrfs_delayed_ref_exit(void);
25878a6184aSMiao Xie 
btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info * fs_info,int num_delayed_refs)2590e55a545SFilipe Manana static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,
2600e55a545SFilipe Manana 					       int num_delayed_refs)
2610e55a545SFilipe Manana {
2620e55a545SFilipe Manana 	u64 num_bytes;
2630e55a545SFilipe Manana 
2640e55a545SFilipe Manana 	num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);
2650e55a545SFilipe Manana 
2660e55a545SFilipe Manana 	/*
2670e55a545SFilipe Manana 	 * We have to check the mount option here because we could be enabling
2680e55a545SFilipe Manana 	 * the free space tree for the first time and don't have the compat_ro
2690e55a545SFilipe Manana 	 * option set yet.
2700e55a545SFilipe Manana 	 *
2710e55a545SFilipe Manana 	 * We need extra reservations if we have the free space tree because
2720e55a545SFilipe Manana 	 * we'll have to modify that tree as well.
2730e55a545SFilipe Manana 	 */
2740e55a545SFilipe Manana 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
2750e55a545SFilipe Manana 		num_bytes *= 2;
2760e55a545SFilipe Manana 
2770e55a545SFilipe Manana 	return num_bytes;
2780e55a545SFilipe Manana }
2790e55a545SFilipe Manana 
btrfs_init_generic_ref(struct btrfs_ref * generic_ref,int action,u64 bytenr,u64 len,u64 parent)280b28b1f0cSQu Wenruo static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
281b28b1f0cSQu Wenruo 				int action, u64 bytenr, u64 len, u64 parent)
282b28b1f0cSQu Wenruo {
283b28b1f0cSQu Wenruo 	generic_ref->action = action;
284b28b1f0cSQu Wenruo 	generic_ref->bytenr = bytenr;
285b28b1f0cSQu Wenruo 	generic_ref->len = len;
286b28b1f0cSQu Wenruo 	generic_ref->parent = parent;
287b28b1f0cSQu Wenruo }
288b28b1f0cSQu Wenruo 
btrfs_init_tree_ref(struct btrfs_ref * generic_ref,int level,u64 root,u64 mod_root,bool skip_qgroup)289b28b1f0cSQu Wenruo static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
290f42c5da6SNikolay Borisov 				int level, u64 root, u64 mod_root, bool skip_qgroup)
291b28b1f0cSQu Wenruo {
292eed2037fSNikolay Borisov #ifdef CONFIG_BTRFS_FS_REF_VERIFY
293b28b1f0cSQu Wenruo 	/* If @real_root not set, use @root as fallback */
294eed2037fSNikolay Borisov 	generic_ref->real_root = mod_root ?: root;
295eed2037fSNikolay Borisov #endif
296b28b1f0cSQu Wenruo 	generic_ref->tree_ref.level = level;
297113479d5SNikolay Borisov 	generic_ref->tree_ref.owning_root = root;
298b28b1f0cSQu Wenruo 	generic_ref->type = BTRFS_REF_METADATA;
299681145d4SNikolay Borisov 	if (skip_qgroup || !(is_fstree(root) &&
300681145d4SNikolay Borisov 			     (!mod_root || is_fstree(mod_root))))
301681145d4SNikolay Borisov 		generic_ref->skip_qgroup = true;
302681145d4SNikolay Borisov 	else
303681145d4SNikolay Borisov 		generic_ref->skip_qgroup = false;
304681145d4SNikolay Borisov 
305b28b1f0cSQu Wenruo }
306b28b1f0cSQu Wenruo 
btrfs_init_data_ref(struct btrfs_ref * generic_ref,u64 ref_root,u64 ino,u64 offset,u64 mod_root,bool skip_qgroup)307b28b1f0cSQu Wenruo static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
308f42c5da6SNikolay Borisov 				u64 ref_root, u64 ino, u64 offset, u64 mod_root,
309f42c5da6SNikolay Borisov 				bool skip_qgroup)
310b28b1f0cSQu Wenruo {
311eed2037fSNikolay Borisov #ifdef CONFIG_BTRFS_FS_REF_VERIFY
312b28b1f0cSQu Wenruo 	/* If @real_root not set, use @root as fallback */
313eed2037fSNikolay Borisov 	generic_ref->real_root = mod_root ?: ref_root;
314eed2037fSNikolay Borisov #endif
315113479d5SNikolay Borisov 	generic_ref->data_ref.owning_root = ref_root;
316b28b1f0cSQu Wenruo 	generic_ref->data_ref.ino = ino;
317b28b1f0cSQu Wenruo 	generic_ref->data_ref.offset = offset;
318b28b1f0cSQu Wenruo 	generic_ref->type = BTRFS_REF_DATA;
319681145d4SNikolay Borisov 	if (skip_qgroup || !(is_fstree(ref_root) &&
320681145d4SNikolay Borisov 			     (!mod_root || is_fstree(mod_root))))
321681145d4SNikolay Borisov 		generic_ref->skip_qgroup = true;
322681145d4SNikolay Borisov 	else
323681145d4SNikolay Borisov 		generic_ref->skip_qgroup = false;
324b28b1f0cSQu Wenruo }
325b28b1f0cSQu Wenruo 
32678a6184aSMiao Xie static inline struct btrfs_delayed_extent_op *
btrfs_alloc_delayed_extent_op(void)32778a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void)
32878a6184aSMiao Xie {
32978a6184aSMiao Xie 	return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
33078a6184aSMiao Xie }
33178a6184aSMiao Xie 
33278a6184aSMiao Xie static inline void
btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op * op)33378a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
33478a6184aSMiao Xie {
33578a6184aSMiao Xie 	if (op)
33678a6184aSMiao Xie 		kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
33778a6184aSMiao Xie }
33878a6184aSMiao Xie 
btrfs_put_delayed_ref(struct btrfs_delayed_ref_node * ref)33956bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
34056bec294SChris Mason {
3416df8cdf5SElena Reshetova 	WARN_ON(refcount_read(&ref->refs) == 0);
3426df8cdf5SElena Reshetova 	if (refcount_dec_and_test(&ref->refs)) {
3434d34ad34SFilipe Manana 		WARN_ON(!RB_EMPTY_NODE(&ref->ref_node));
34478a6184aSMiao Xie 		switch (ref->type) {
34578a6184aSMiao Xie 		case BTRFS_TREE_BLOCK_REF_KEY:
34678a6184aSMiao Xie 		case BTRFS_SHARED_BLOCK_REF_KEY:
34778a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
34878a6184aSMiao Xie 			break;
34978a6184aSMiao Xie 		case BTRFS_EXTENT_DATA_REF_KEY:
35078a6184aSMiao Xie 		case BTRFS_SHARED_DATA_REF_KEY:
35178a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
35278a6184aSMiao Xie 			break;
35378a6184aSMiao Xie 		default:
35478a6184aSMiao Xie 			BUG();
35578a6184aSMiao Xie 		}
35656bec294SChris Mason 	}
35756bec294SChris Mason }
35856bec294SChris Mason 
btrfs_ref_head_to_space_flags(struct btrfs_delayed_ref_head * head_ref)3592187374fSJosef Bacik static inline u64 btrfs_ref_head_to_space_flags(
3602187374fSJosef Bacik 				struct btrfs_delayed_ref_head *head_ref)
3612187374fSJosef Bacik {
3622187374fSJosef Bacik 	if (head_ref->is_data)
3632187374fSJosef Bacik 		return BTRFS_BLOCK_GROUP_DATA;
3642187374fSJosef Bacik 	else if (head_ref->is_system)
3652187374fSJosef Bacik 		return BTRFS_BLOCK_GROUP_SYSTEM;
3662187374fSJosef Bacik 	return BTRFS_BLOCK_GROUP_METADATA;
3672187374fSJosef Bacik }
3682187374fSJosef Bacik 
btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head * head)369d278850eSJosef Bacik static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
370d278850eSJosef Bacik {
371d278850eSJosef Bacik 	if (refcount_dec_and_test(&head->refs))
372d278850eSJosef Bacik 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
373d278850eSJosef Bacik }
374d278850eSJosef Bacik 
37544e1c47dSNikolay Borisov int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
376ed4f255bSQu Wenruo 			       struct btrfs_ref *generic_ref,
3772187374fSJosef Bacik 			       struct btrfs_delayed_extent_op *extent_op);
37888a979c6SNikolay Borisov int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
37976675593SQu Wenruo 			       struct btrfs_ref *generic_ref,
3802187374fSJosef Bacik 			       u64 reserved);
381c6e340bcSDavid Sterba int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
3825d4f98a2SYan Zheng 				u64 bytenr, u64 num_bytes,
3835d4f98a2SYan Zheng 				struct btrfs_delayed_extent_op *extent_op);
3840c555c97SJohannes Thumshirn void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
385ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_root *delayed_refs,
386ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_head *head);
38756bec294SChris Mason 
3881887be66SChris Mason struct btrfs_delayed_ref_head *
389f72ad18eSLiu Bo btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
390f72ad18eSLiu Bo 			    u64 bytenr);
3919e920a6fSLu Fengqi int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
392c3e69d58SChris Mason 			   struct btrfs_delayed_ref_head *head);
btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head * head)393093486c4SMiao Xie static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
394093486c4SMiao Xie {
395093486c4SMiao Xie 	mutex_unlock(&head->mutex);
396093486c4SMiao Xie }
397d7baffdaSJosef Bacik void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
398d7baffdaSJosef Bacik 			   struct btrfs_delayed_ref_head *head);
399d7df2c79SJosef Bacik 
4005637c74bSLu Fengqi struct btrfs_delayed_ref_head *btrfs_select_ref_head(
4015637c74bSLu Fengqi 		struct btrfs_delayed_ref_root *delayed_refs);
40200f04b88SArne Jansen 
40341d0bd3bSNikolay Borisov int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
40400f04b88SArne Jansen 
4056ef03debSJosef Bacik void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
4066ef03debSJosef Bacik void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
4076ef03debSJosef Bacik int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
4086ef03debSJosef Bacik 				  enum btrfs_reserve_flush_enum flush);
4096ef03debSJosef Bacik void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
4106ef03debSJosef Bacik 				       u64 num_bytes);
4116ef03debSJosef Bacik bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
4126ef03debSJosef Bacik 
41300f04b88SArne Jansen /*
41456bec294SChris Mason  * helper functions to cast a node into its container
41556bec294SChris Mason  */
4165d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref *
btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node * node)4175d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
41856bec294SChris Mason {
4195d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_tree_ref, node);
4205d4f98a2SYan Zheng }
42156bec294SChris Mason 
4225d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref *
btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node * node)4235d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
4245d4f98a2SYan Zheng {
4255d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_data_ref, node);
42656bec294SChris Mason }
4279888c340SDavid Sterba 
42856bec294SChris Mason #endif
429