xref: /openbmc/linux/fs/btrfs/delayed-ref.h (revision 315dd5cc)
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 	/* is this node still in the rbtree? */
525d4f98a2SYan Zheng 	unsigned int is_head:1;
5356bec294SChris Mason 	unsigned int in_tree:1;
5456bec294SChris Mason };
5556bec294SChris Mason 
565d4f98a2SYan Zheng struct btrfs_delayed_extent_op {
575d4f98a2SYan Zheng 	struct btrfs_disk_key key;
5835b3ad50SDavid Sterba 	u8 level;
5935b3ad50SDavid Sterba 	bool update_key;
6035b3ad50SDavid Sterba 	bool update_flags;
615d4f98a2SYan Zheng 	u64 flags_to_set;
625d4f98a2SYan Zheng };
635d4f98a2SYan Zheng 
6456bec294SChris Mason /*
6556bec294SChris Mason  * the head refs are used to hold a lock on a given extent, which allows us
6656bec294SChris Mason  * to make sure that only one process is running the delayed refs
6756bec294SChris Mason  * at a time for a single extent.  They also store the sum of all the
6856bec294SChris Mason  * reference count modifications we've queued up.
6956bec294SChris Mason  */
7056bec294SChris Mason struct btrfs_delayed_ref_head {
71d278850eSJosef Bacik 	u64 bytenr;
72d278850eSJosef Bacik 	u64 num_bytes;
73*315dd5ccSFilipe Manana 	/*
74*315dd5ccSFilipe Manana 	 * For insertion into struct btrfs_delayed_ref_root::href_root.
75*315dd5ccSFilipe Manana 	 * Keep it in the same cache line as 'bytenr' for more efficient
76*315dd5ccSFilipe Manana 	 * searches in the rbtree.
77*315dd5ccSFilipe Manana 	 */
78*315dd5ccSFilipe Manana 	struct rb_node href_node;
7956bec294SChris Mason 	/*
8056bec294SChris Mason 	 * the mutex is held while running the refs, and it is also
8156bec294SChris Mason 	 * held when checking the sum of reference modifications.
8256bec294SChris Mason 	 */
8356bec294SChris Mason 	struct mutex mutex;
8456bec294SChris Mason 
85*315dd5ccSFilipe Manana 	refcount_t refs;
86*315dd5ccSFilipe Manana 
87*315dd5ccSFilipe Manana 	/* Protects 'ref_tree' and 'ref_add_list'. */
88d7df2c79SJosef Bacik 	spinlock_t lock;
89e3d03965SLiu Bo 	struct rb_root_cached ref_tree;
901d57ee94SWang Xiaoguang 	/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
911d57ee94SWang Xiaoguang 	struct list_head ref_add_list;
92c3e69d58SChris Mason 
935d4f98a2SYan Zheng 	struct btrfs_delayed_extent_op *extent_op;
941262133bSJosef Bacik 
951262133bSJosef Bacik 	/*
961262133bSJosef Bacik 	 * This is used to track the final ref_mod from all the refs associated
971262133bSJosef Bacik 	 * with this head ref, this is not adjusted as delayed refs are run,
981262133bSJosef Bacik 	 * this is meant to track if we need to do the csum accounting or not.
991262133bSJosef Bacik 	 */
1001262133bSJosef Bacik 	int total_ref_mod;
1011262133bSJosef Bacik 
10256bec294SChris Mason 	/*
103d278850eSJosef Bacik 	 * This is the current outstanding mod references for this bytenr.  This
104d278850eSJosef Bacik 	 * is used with lookup_extent_info to get an accurate reference count
105d278850eSJosef Bacik 	 * for a bytenr, so it is adjusted as delayed refs are run so that any
106d278850eSJosef Bacik 	 * on disk reference count + ref_mod is accurate.
107d278850eSJosef Bacik 	 */
108d278850eSJosef Bacik 	int ref_mod;
109d278850eSJosef Bacik 
110d278850eSJosef Bacik 	/*
11156bec294SChris Mason 	 * when a new extent is allocated, it is just reserved in memory
11256bec294SChris Mason 	 * The actual extent isn't inserted into the extent allocation tree
11356bec294SChris Mason 	 * until the delayed ref is processed.  must_insert_reserved is
11456bec294SChris Mason 	 * used to flag a delayed ref so the accounting can be updated
11556bec294SChris Mason 	 * when a full insert is done.
11656bec294SChris Mason 	 *
11756bec294SChris Mason 	 * It is possible the extent will be freed before it is ever
11856bec294SChris Mason 	 * inserted into the extent allocation tree.  In this case
11956bec294SChris Mason 	 * we need to update the in ram accounting to properly reflect
12056bec294SChris Mason 	 * the free has happened.
12156bec294SChris Mason 	 */
12256bec294SChris Mason 	unsigned int must_insert_reserved:1;
1235d4f98a2SYan Zheng 	unsigned int is_data:1;
1245e388e95SNikolay Borisov 	unsigned int is_system:1;
125d7df2c79SJosef Bacik 	unsigned int processing:1;
12656bec294SChris Mason };
12756bec294SChris Mason 
1285d4f98a2SYan Zheng struct btrfs_delayed_tree_ref {
12956bec294SChris Mason 	struct btrfs_delayed_ref_node node;
13056bec294SChris Mason 	u64 root;
1315d4f98a2SYan Zheng 	u64 parent;
1325d4f98a2SYan Zheng 	int level;
1335d4f98a2SYan Zheng };
13456bec294SChris Mason 
1355d4f98a2SYan Zheng struct btrfs_delayed_data_ref {
1365d4f98a2SYan Zheng 	struct btrfs_delayed_ref_node node;
1375d4f98a2SYan Zheng 	u64 root;
1385d4f98a2SYan Zheng 	u64 parent;
1395d4f98a2SYan Zheng 	u64 objectid;
1405d4f98a2SYan Zheng 	u64 offset;
14156bec294SChris Mason };
14256bec294SChris Mason 
143e19eb11fSJosef Bacik enum btrfs_delayed_ref_flags {
144e19eb11fSJosef Bacik 	/* Indicate that we are flushing delayed refs for the commit */
145e19eb11fSJosef Bacik 	BTRFS_DELAYED_REFS_FLUSHING,
146e19eb11fSJosef Bacik };
147e19eb11fSJosef Bacik 
14856bec294SChris Mason struct btrfs_delayed_ref_root {
149c46effa6SLiu Bo 	/* head ref rbtree */
1505c9d028bSLiu Bo 	struct rb_root_cached href_root;
151c46effa6SLiu Bo 
1523368d001SQu Wenruo 	/* dirty extent records */
1533368d001SQu Wenruo 	struct rb_root dirty_extent_root;
1543368d001SQu Wenruo 
15556bec294SChris Mason 	/* this spin lock protects the rbtree and the entries inside */
15656bec294SChris Mason 	spinlock_t lock;
15756bec294SChris Mason 
15856bec294SChris Mason 	/* how many delayed ref updates we've queued, used by the
15956bec294SChris Mason 	 * throttling code
16056bec294SChris Mason 	 */
161d7df2c79SJosef Bacik 	atomic_t num_entries;
16256bec294SChris Mason 
163c3e69d58SChris Mason 	/* total number of head nodes in tree */
164c3e69d58SChris Mason 	unsigned long num_heads;
165c3e69d58SChris Mason 
166c3e69d58SChris Mason 	/* total number of head nodes ready for processing */
167c3e69d58SChris Mason 	unsigned long num_heads_ready;
168c3e69d58SChris Mason 
1691262133bSJosef Bacik 	u64 pending_csums;
1701262133bSJosef Bacik 
171e19eb11fSJosef Bacik 	unsigned long flags;
172c3e69d58SChris Mason 
173c3e69d58SChris Mason 	u64 run_delayed_start;
1749086db86SQu Wenruo 
1759086db86SQu Wenruo 	/*
1769086db86SQu Wenruo 	 * To make qgroup to skip given root.
17701327610SNicholas D Steeves 	 * This is for snapshot, as btrfs_qgroup_inherit() will manually
1789086db86SQu Wenruo 	 * modify counters for snapshot and its source, so we should skip
1799086db86SQu Wenruo 	 * the snapshot in new_root/old_roots or it will get calculated twice
1809086db86SQu Wenruo 	 */
1819086db86SQu Wenruo 	u64 qgroup_to_skip;
18256bec294SChris Mason };
18356bec294SChris Mason 
184b28b1f0cSQu Wenruo enum btrfs_ref_type {
185b28b1f0cSQu Wenruo 	BTRFS_REF_NOT_SET,
186b28b1f0cSQu Wenruo 	BTRFS_REF_DATA,
187b28b1f0cSQu Wenruo 	BTRFS_REF_METADATA,
188b28b1f0cSQu Wenruo 	BTRFS_REF_LAST,
189b28b1f0cSQu Wenruo };
190b28b1f0cSQu Wenruo 
191b28b1f0cSQu Wenruo struct btrfs_data_ref {
192b28b1f0cSQu Wenruo 	/* For EXTENT_DATA_REF */
193b28b1f0cSQu Wenruo 
194113479d5SNikolay Borisov 	/* Original root this data extent belongs to */
195113479d5SNikolay Borisov 	u64 owning_root;
196b28b1f0cSQu Wenruo 
197b28b1f0cSQu Wenruo 	/* Inode which refers to this data extent */
198b28b1f0cSQu Wenruo 	u64 ino;
199b28b1f0cSQu Wenruo 
200b28b1f0cSQu Wenruo 	/*
201b28b1f0cSQu Wenruo 	 * file_offset - extent_offset
202b28b1f0cSQu Wenruo 	 *
203b28b1f0cSQu Wenruo 	 * file_offset is the key.offset of the EXTENT_DATA key.
204b28b1f0cSQu Wenruo 	 * extent_offset is btrfs_file_extent_offset() of the EXTENT_DATA data.
205b28b1f0cSQu Wenruo 	 */
206b28b1f0cSQu Wenruo 	u64 offset;
207b28b1f0cSQu Wenruo };
208b28b1f0cSQu Wenruo 
209b28b1f0cSQu Wenruo struct btrfs_tree_ref {
210b28b1f0cSQu Wenruo 	/*
211b28b1f0cSQu Wenruo 	 * Level of this tree block
212b28b1f0cSQu Wenruo 	 *
213b28b1f0cSQu Wenruo 	 * Shared for skinny (TREE_BLOCK_REF) and normal tree ref.
214b28b1f0cSQu Wenruo 	 */
215b28b1f0cSQu Wenruo 	int level;
216b28b1f0cSQu Wenruo 
217b28b1f0cSQu Wenruo 	/*
218113479d5SNikolay Borisov 	 * Root which owns this tree block.
219b28b1f0cSQu Wenruo 	 *
220b28b1f0cSQu Wenruo 	 * For TREE_BLOCK_REF (skinny metadata, either inline or keyed)
221b28b1f0cSQu Wenruo 	 */
222113479d5SNikolay Borisov 	u64 owning_root;
223b28b1f0cSQu Wenruo 
224b28b1f0cSQu Wenruo 	/* For non-skinny metadata, no special member needed */
225b28b1f0cSQu Wenruo };
226b28b1f0cSQu Wenruo 
227b28b1f0cSQu Wenruo struct btrfs_ref {
228b28b1f0cSQu Wenruo 	enum btrfs_ref_type type;
229b28b1f0cSQu Wenruo 	int action;
230b28b1f0cSQu Wenruo 
231b28b1f0cSQu Wenruo 	/*
232b28b1f0cSQu Wenruo 	 * Whether this extent should go through qgroup record.
233b28b1f0cSQu Wenruo 	 *
234b28b1f0cSQu Wenruo 	 * Normally false, but for certain cases like delayed subtree scan,
235b28b1f0cSQu Wenruo 	 * setting this flag can hugely reduce qgroup overhead.
236b28b1f0cSQu Wenruo 	 */
237b28b1f0cSQu Wenruo 	bool skip_qgroup;
238b28b1f0cSQu Wenruo 
239eed2037fSNikolay Borisov #ifdef CONFIG_BTRFS_FS_REF_VERIFY
240eed2037fSNikolay Borisov 	/* Through which root is this modification. */
241b28b1f0cSQu Wenruo 	u64 real_root;
242eed2037fSNikolay Borisov #endif
243b28b1f0cSQu Wenruo 	u64 bytenr;
244b28b1f0cSQu Wenruo 	u64 len;
245b28b1f0cSQu Wenruo 
246b28b1f0cSQu Wenruo 	/* Bytenr of the parent tree block */
247b28b1f0cSQu Wenruo 	u64 parent;
248b28b1f0cSQu Wenruo 	union {
249b28b1f0cSQu Wenruo 		struct btrfs_data_ref data_ref;
250b28b1f0cSQu Wenruo 		struct btrfs_tree_ref tree_ref;
251b28b1f0cSQu Wenruo 	};
252b28b1f0cSQu Wenruo };
253b28b1f0cSQu Wenruo 
25478a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
25578a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
25678a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
25778a6184aSMiao Xie extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
25878a6184aSMiao Xie 
259f5c29bd9SLiu Bo int __init btrfs_delayed_ref_init(void);
260e67c718bSDavid Sterba void __cold btrfs_delayed_ref_exit(void);
26178a6184aSMiao Xie 
2620e55a545SFilipe Manana static inline u64 btrfs_calc_delayed_ref_bytes(const struct btrfs_fs_info *fs_info,
2630e55a545SFilipe Manana 					       int num_delayed_refs)
2640e55a545SFilipe Manana {
2650e55a545SFilipe Manana 	u64 num_bytes;
2660e55a545SFilipe Manana 
2670e55a545SFilipe Manana 	num_bytes = btrfs_calc_insert_metadata_size(fs_info, num_delayed_refs);
2680e55a545SFilipe Manana 
2690e55a545SFilipe Manana 	/*
2700e55a545SFilipe Manana 	 * We have to check the mount option here because we could be enabling
2710e55a545SFilipe Manana 	 * the free space tree for the first time and don't have the compat_ro
2720e55a545SFilipe Manana 	 * option set yet.
2730e55a545SFilipe Manana 	 *
2740e55a545SFilipe Manana 	 * We need extra reservations if we have the free space tree because
2750e55a545SFilipe Manana 	 * we'll have to modify that tree as well.
2760e55a545SFilipe Manana 	 */
2770e55a545SFilipe Manana 	if (btrfs_test_opt(fs_info, FREE_SPACE_TREE))
2780e55a545SFilipe Manana 		num_bytes *= 2;
2790e55a545SFilipe Manana 
2800e55a545SFilipe Manana 	return num_bytes;
2810e55a545SFilipe Manana }
2820e55a545SFilipe Manana 
283b28b1f0cSQu Wenruo static inline void btrfs_init_generic_ref(struct btrfs_ref *generic_ref,
284b28b1f0cSQu Wenruo 				int action, u64 bytenr, u64 len, u64 parent)
285b28b1f0cSQu Wenruo {
286b28b1f0cSQu Wenruo 	generic_ref->action = action;
287b28b1f0cSQu Wenruo 	generic_ref->bytenr = bytenr;
288b28b1f0cSQu Wenruo 	generic_ref->len = len;
289b28b1f0cSQu Wenruo 	generic_ref->parent = parent;
290b28b1f0cSQu Wenruo }
291b28b1f0cSQu Wenruo 
292b28b1f0cSQu Wenruo static inline void btrfs_init_tree_ref(struct btrfs_ref *generic_ref,
293f42c5da6SNikolay Borisov 				int level, u64 root, u64 mod_root, bool skip_qgroup)
294b28b1f0cSQu Wenruo {
295eed2037fSNikolay Borisov #ifdef CONFIG_BTRFS_FS_REF_VERIFY
296b28b1f0cSQu Wenruo 	/* If @real_root not set, use @root as fallback */
297eed2037fSNikolay Borisov 	generic_ref->real_root = mod_root ?: root;
298eed2037fSNikolay Borisov #endif
299b28b1f0cSQu Wenruo 	generic_ref->tree_ref.level = level;
300113479d5SNikolay Borisov 	generic_ref->tree_ref.owning_root = root;
301b28b1f0cSQu Wenruo 	generic_ref->type = BTRFS_REF_METADATA;
302681145d4SNikolay Borisov 	if (skip_qgroup || !(is_fstree(root) &&
303681145d4SNikolay Borisov 			     (!mod_root || is_fstree(mod_root))))
304681145d4SNikolay Borisov 		generic_ref->skip_qgroup = true;
305681145d4SNikolay Borisov 	else
306681145d4SNikolay Borisov 		generic_ref->skip_qgroup = false;
307681145d4SNikolay Borisov 
308b28b1f0cSQu Wenruo }
309b28b1f0cSQu Wenruo 
310b28b1f0cSQu Wenruo static inline void btrfs_init_data_ref(struct btrfs_ref *generic_ref,
311f42c5da6SNikolay Borisov 				u64 ref_root, u64 ino, u64 offset, u64 mod_root,
312f42c5da6SNikolay Borisov 				bool skip_qgroup)
313b28b1f0cSQu Wenruo {
314eed2037fSNikolay Borisov #ifdef CONFIG_BTRFS_FS_REF_VERIFY
315b28b1f0cSQu Wenruo 	/* If @real_root not set, use @root as fallback */
316eed2037fSNikolay Borisov 	generic_ref->real_root = mod_root ?: ref_root;
317eed2037fSNikolay Borisov #endif
318113479d5SNikolay Borisov 	generic_ref->data_ref.owning_root = ref_root;
319b28b1f0cSQu Wenruo 	generic_ref->data_ref.ino = ino;
320b28b1f0cSQu Wenruo 	generic_ref->data_ref.offset = offset;
321b28b1f0cSQu Wenruo 	generic_ref->type = BTRFS_REF_DATA;
322681145d4SNikolay Borisov 	if (skip_qgroup || !(is_fstree(ref_root) &&
323681145d4SNikolay Borisov 			     (!mod_root || is_fstree(mod_root))))
324681145d4SNikolay Borisov 		generic_ref->skip_qgroup = true;
325681145d4SNikolay Borisov 	else
326681145d4SNikolay Borisov 		generic_ref->skip_qgroup = false;
327b28b1f0cSQu Wenruo }
328b28b1f0cSQu Wenruo 
32978a6184aSMiao Xie static inline struct btrfs_delayed_extent_op *
33078a6184aSMiao Xie btrfs_alloc_delayed_extent_op(void)
33178a6184aSMiao Xie {
33278a6184aSMiao Xie 	return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
33378a6184aSMiao Xie }
33478a6184aSMiao Xie 
33578a6184aSMiao Xie static inline void
33678a6184aSMiao Xie btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
33778a6184aSMiao Xie {
33878a6184aSMiao Xie 	if (op)
33978a6184aSMiao Xie 		kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
34078a6184aSMiao Xie }
34178a6184aSMiao Xie 
34256bec294SChris Mason static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
34356bec294SChris Mason {
3446df8cdf5SElena Reshetova 	WARN_ON(refcount_read(&ref->refs) == 0);
3456df8cdf5SElena Reshetova 	if (refcount_dec_and_test(&ref->refs)) {
34656bec294SChris Mason 		WARN_ON(ref->in_tree);
34778a6184aSMiao Xie 		switch (ref->type) {
34878a6184aSMiao Xie 		case BTRFS_TREE_BLOCK_REF_KEY:
34978a6184aSMiao Xie 		case BTRFS_SHARED_BLOCK_REF_KEY:
35078a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
35178a6184aSMiao Xie 			break;
35278a6184aSMiao Xie 		case BTRFS_EXTENT_DATA_REF_KEY:
35378a6184aSMiao Xie 		case BTRFS_SHARED_DATA_REF_KEY:
35478a6184aSMiao Xie 			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
35578a6184aSMiao Xie 			break;
35678a6184aSMiao Xie 		default:
35778a6184aSMiao Xie 			BUG();
35878a6184aSMiao Xie 		}
35956bec294SChris Mason 	}
36056bec294SChris Mason }
36156bec294SChris Mason 
3622187374fSJosef Bacik static inline u64 btrfs_ref_head_to_space_flags(
3632187374fSJosef Bacik 				struct btrfs_delayed_ref_head *head_ref)
3642187374fSJosef Bacik {
3652187374fSJosef Bacik 	if (head_ref->is_data)
3662187374fSJosef Bacik 		return BTRFS_BLOCK_GROUP_DATA;
3672187374fSJosef Bacik 	else if (head_ref->is_system)
3682187374fSJosef Bacik 		return BTRFS_BLOCK_GROUP_SYSTEM;
3692187374fSJosef Bacik 	return BTRFS_BLOCK_GROUP_METADATA;
3702187374fSJosef Bacik }
3712187374fSJosef Bacik 
372d278850eSJosef Bacik static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
373d278850eSJosef Bacik {
374d278850eSJosef Bacik 	if (refcount_dec_and_test(&head->refs))
375d278850eSJosef Bacik 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
376d278850eSJosef Bacik }
377d278850eSJosef Bacik 
37844e1c47dSNikolay Borisov int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
379ed4f255bSQu Wenruo 			       struct btrfs_ref *generic_ref,
3802187374fSJosef Bacik 			       struct btrfs_delayed_extent_op *extent_op);
38188a979c6SNikolay Borisov int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
38276675593SQu Wenruo 			       struct btrfs_ref *generic_ref,
3832187374fSJosef Bacik 			       u64 reserved);
384c6e340bcSDavid Sterba int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
3855d4f98a2SYan Zheng 				u64 bytenr, u64 num_bytes,
3865d4f98a2SYan Zheng 				struct btrfs_delayed_extent_op *extent_op);
3870c555c97SJohannes Thumshirn void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
388ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_root *delayed_refs,
389ae1e206bSJosef Bacik 			      struct btrfs_delayed_ref_head *head);
39056bec294SChris Mason 
3911887be66SChris Mason struct btrfs_delayed_ref_head *
392f72ad18eSLiu Bo btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
393f72ad18eSLiu Bo 			    u64 bytenr);
3949e920a6fSLu Fengqi int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
395c3e69d58SChris Mason 			   struct btrfs_delayed_ref_head *head);
396093486c4SMiao Xie static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
397093486c4SMiao Xie {
398093486c4SMiao Xie 	mutex_unlock(&head->mutex);
399093486c4SMiao Xie }
400d7baffdaSJosef Bacik void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
401d7baffdaSJosef Bacik 			   struct btrfs_delayed_ref_head *head);
402d7df2c79SJosef Bacik 
4035637c74bSLu Fengqi struct btrfs_delayed_ref_head *btrfs_select_ref_head(
4045637c74bSLu Fengqi 		struct btrfs_delayed_ref_root *delayed_refs);
40500f04b88SArne Jansen 
40641d0bd3bSNikolay Borisov int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
40700f04b88SArne Jansen 
4086ef03debSJosef Bacik void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr);
4096ef03debSJosef Bacik void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans);
4106ef03debSJosef Bacik int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
4116ef03debSJosef Bacik 				  enum btrfs_reserve_flush_enum flush);
4126ef03debSJosef Bacik void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
4136ef03debSJosef Bacik 				       struct btrfs_block_rsv *src,
4146ef03debSJosef Bacik 				       u64 num_bytes);
4156ef03debSJosef Bacik bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info);
4166ef03debSJosef Bacik 
41700f04b88SArne Jansen /*
41856bec294SChris Mason  * helper functions to cast a node into its container
41956bec294SChris Mason  */
4205d4f98a2SYan Zheng static inline struct btrfs_delayed_tree_ref *
4215d4f98a2SYan Zheng btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
42256bec294SChris Mason {
4235d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_tree_ref, node);
4245d4f98a2SYan Zheng }
42556bec294SChris Mason 
4265d4f98a2SYan Zheng static inline struct btrfs_delayed_data_ref *
4275d4f98a2SYan Zheng btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
4285d4f98a2SYan Zheng {
4295d4f98a2SYan Zheng 	return container_of(node, struct btrfs_delayed_data_ref, node);
43056bec294SChris Mason }
4319888c340SDavid Sterba 
43256bec294SChris Mason #endif
433