xref: /openbmc/linux/fs/btrfs/delayed-ref.c (revision a7ddeeb0)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
256bec294SChris Mason /*
356bec294SChris Mason  * Copyright (C) 2009 Oracle.  All rights reserved.
456bec294SChris Mason  */
556bec294SChris Mason 
656bec294SChris Mason #include <linux/sched.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
856bec294SChris Mason #include <linux/sort.h>
99b569ea0SJosef Bacik #include "messages.h"
1056bec294SChris Mason #include "ctree.h"
1156bec294SChris Mason #include "delayed-ref.h"
1256bec294SChris Mason #include "transaction.h"
133368d001SQu Wenruo #include "qgroup.h"
146ef03debSJosef Bacik #include "space-info.h"
15f3a84ccdSFilipe Manana #include "tree-mod-log.h"
16fc97a410SJosef Bacik #include "fs.h"
1756bec294SChris Mason 
1878a6184aSMiao Xie struct kmem_cache *btrfs_delayed_ref_head_cachep;
1978a6184aSMiao Xie struct kmem_cache *btrfs_delayed_tree_ref_cachep;
2078a6184aSMiao Xie struct kmem_cache *btrfs_delayed_data_ref_cachep;
2178a6184aSMiao Xie struct kmem_cache *btrfs_delayed_extent_op_cachep;
2256bec294SChris Mason /*
2356bec294SChris Mason  * delayed back reference update tracking.  For subvolume trees
2456bec294SChris Mason  * we queue up extent allocations and backref maintenance for
2556bec294SChris Mason  * delayed processing.   This avoids deep call chains where we
2656bec294SChris Mason  * add extents in the middle of btrfs_search_slot, and it allows
2756bec294SChris Mason  * us to buffer up frequently modified backrefs in an rb tree instead
2856bec294SChris Mason  * of hammering updates on the extent allocation tree.
2956bec294SChris Mason  */
3056bec294SChris Mason 
btrfs_check_space_for_delayed_refs(struct btrfs_fs_info * fs_info)316ef03debSJosef Bacik bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info)
326ef03debSJosef Bacik {
336ef03debSJosef Bacik 	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
346ef03debSJosef Bacik 	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
356ef03debSJosef Bacik 	bool ret = false;
366ef03debSJosef Bacik 	u64 reserved;
376ef03debSJosef Bacik 
386ef03debSJosef Bacik 	spin_lock(&global_rsv->lock);
396ef03debSJosef Bacik 	reserved = global_rsv->reserved;
406ef03debSJosef Bacik 	spin_unlock(&global_rsv->lock);
416ef03debSJosef Bacik 
426ef03debSJosef Bacik 	/*
436ef03debSJosef Bacik 	 * Since the global reserve is just kind of magic we don't really want
446ef03debSJosef Bacik 	 * to rely on it to save our bacon, so if our size is more than the
456ef03debSJosef Bacik 	 * delayed_refs_rsv and the global rsv then it's time to think about
466ef03debSJosef Bacik 	 * bailing.
476ef03debSJosef Bacik 	 */
486ef03debSJosef Bacik 	spin_lock(&delayed_refs_rsv->lock);
496ef03debSJosef Bacik 	reserved += delayed_refs_rsv->reserved;
506ef03debSJosef Bacik 	if (delayed_refs_rsv->size >= reserved)
516ef03debSJosef Bacik 		ret = true;
526ef03debSJosef Bacik 	spin_unlock(&delayed_refs_rsv->lock);
536ef03debSJosef Bacik 	return ret;
546ef03debSJosef Bacik }
556ef03debSJosef Bacik 
5643dd529aSDavid Sterba /*
5743dd529aSDavid Sterba  * Release a ref head's reservation.
58696eb22bSNikolay Borisov  *
59696eb22bSNikolay Borisov  * @fs_info:  the filesystem
60696eb22bSNikolay Borisov  * @nr:       number of items to drop
616ef03debSJosef Bacik  *
6243dd529aSDavid Sterba  * Drops the delayed ref head's count from the delayed refs rsv and free any
6343dd529aSDavid Sterba  * excess reservation we had.
646ef03debSJosef Bacik  */
btrfs_delayed_refs_rsv_release(struct btrfs_fs_info * fs_info,int nr)656ef03debSJosef Bacik void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr)
666ef03debSJosef Bacik {
676ef03debSJosef Bacik 	struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
680e55a545SFilipe Manana 	const u64 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr);
696ef03debSJosef Bacik 	u64 released = 0;
706ef03debSJosef Bacik 
7163f018beSNikolay Borisov 	released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
726ef03debSJosef Bacik 	if (released)
736ef03debSJosef Bacik 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
746ef03debSJosef Bacik 					      0, released, 0);
756ef03debSJosef Bacik }
766ef03debSJosef Bacik 
776ef03debSJosef Bacik /*
7843dd529aSDavid Sterba  * Adjust the size of the delayed refs rsv.
796ef03debSJosef Bacik  *
806ef03debSJosef Bacik  * This is to be called anytime we may have adjusted trans->delayed_ref_updates,
816ef03debSJosef Bacik  * it'll calculate the additional size and add it to the delayed_refs_rsv.
826ef03debSJosef Bacik  */
btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle * trans)836ef03debSJosef Bacik void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
846ef03debSJosef Bacik {
856ef03debSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
866ef03debSJosef Bacik 	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
876ef03debSJosef Bacik 	u64 num_bytes;
886ef03debSJosef Bacik 
896ef03debSJosef Bacik 	if (!trans->delayed_ref_updates)
906ef03debSJosef Bacik 		return;
916ef03debSJosef Bacik 
920e55a545SFilipe Manana 	num_bytes = btrfs_calc_delayed_ref_bytes(fs_info,
936ef03debSJosef Bacik 						 trans->delayed_ref_updates);
94c18e3235SJosef Bacik 
956ef03debSJosef Bacik 	spin_lock(&delayed_rsv->lock);
966ef03debSJosef Bacik 	delayed_rsv->size += num_bytes;
97c70c2c5bSDavid Sterba 	delayed_rsv->full = false;
986ef03debSJosef Bacik 	spin_unlock(&delayed_rsv->lock);
996ef03debSJosef Bacik 	trans->delayed_ref_updates = 0;
1006ef03debSJosef Bacik }
1016ef03debSJosef Bacik 
10243dd529aSDavid Sterba /*
10343dd529aSDavid Sterba  * Transfer bytes to our delayed refs rsv.
104696eb22bSNikolay Borisov  *
105696eb22bSNikolay Borisov  * @fs_info:   the filesystem
106696eb22bSNikolay Borisov  * @num_bytes: number of bytes to transfer
1076ef03debSJosef Bacik  *
108*a7ddeeb0SFilipe Manana  * This transfers up to the num_bytes amount, previously reserved, to the
1096ef03debSJosef Bacik  * delayed_refs_rsv.  Any extra bytes are returned to the space info.
1106ef03debSJosef Bacik  */
btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info * fs_info,u64 num_bytes)1116ef03debSJosef Bacik void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
1126ef03debSJosef Bacik 				       u64 num_bytes)
1136ef03debSJosef Bacik {
1146ef03debSJosef Bacik 	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
1156ef03debSJosef Bacik 	u64 to_free = 0;
1166ef03debSJosef Bacik 
1176ef03debSJosef Bacik 	spin_lock(&delayed_refs_rsv->lock);
1186ef03debSJosef Bacik 	if (delayed_refs_rsv->size > delayed_refs_rsv->reserved) {
1196ef03debSJosef Bacik 		u64 delta = delayed_refs_rsv->size -
1206ef03debSJosef Bacik 			delayed_refs_rsv->reserved;
1216ef03debSJosef Bacik 		if (num_bytes > delta) {
1226ef03debSJosef Bacik 			to_free = num_bytes - delta;
1236ef03debSJosef Bacik 			num_bytes = delta;
1246ef03debSJosef Bacik 		}
1256ef03debSJosef Bacik 	} else {
1266ef03debSJosef Bacik 		to_free = num_bytes;
1276ef03debSJosef Bacik 		num_bytes = 0;
1286ef03debSJosef Bacik 	}
1296ef03debSJosef Bacik 
1306ef03debSJosef Bacik 	if (num_bytes)
1316ef03debSJosef Bacik 		delayed_refs_rsv->reserved += num_bytes;
1326ef03debSJosef Bacik 	if (delayed_refs_rsv->reserved >= delayed_refs_rsv->size)
133c70c2c5bSDavid Sterba 		delayed_refs_rsv->full = true;
1346ef03debSJosef Bacik 	spin_unlock(&delayed_refs_rsv->lock);
1356ef03debSJosef Bacik 
1366ef03debSJosef Bacik 	if (num_bytes)
1376ef03debSJosef Bacik 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
1386ef03debSJosef Bacik 					      0, num_bytes, 1);
1396ef03debSJosef Bacik 	if (to_free)
140d05e4649SJosef Bacik 		btrfs_space_info_free_bytes_may_use(fs_info,
1416ef03debSJosef Bacik 				delayed_refs_rsv->space_info, to_free);
1426ef03debSJosef Bacik }
1436ef03debSJosef Bacik 
14443dd529aSDavid Sterba /*
14543dd529aSDavid Sterba  * Refill based on our delayed refs usage.
146696eb22bSNikolay Borisov  *
147696eb22bSNikolay Borisov  * @fs_info: the filesystem
148696eb22bSNikolay Borisov  * @flush:   control how we can flush for this reservation.
1496ef03debSJosef Bacik  *
1506ef03debSJosef Bacik  * This will refill the delayed block_rsv up to 1 items size worth of space and
1516ef03debSJosef Bacik  * will return -ENOSPC if we can't make the reservation.
1526ef03debSJosef Bacik  */
btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info * fs_info,enum btrfs_reserve_flush_enum flush)1536ef03debSJosef Bacik int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
1546ef03debSJosef Bacik 				  enum btrfs_reserve_flush_enum flush)
1556ef03debSJosef Bacik {
1566ef03debSJosef Bacik 	struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
1570e55a545SFilipe Manana 	u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);
1586ef03debSJosef Bacik 	u64 num_bytes = 0;
1592ed45c0fSFilipe Manana 	u64 refilled_bytes;
1602ed45c0fSFilipe Manana 	u64 to_free;
1616ef03debSJosef Bacik 	int ret = -ENOSPC;
1626ef03debSJosef Bacik 
1636ef03debSJosef Bacik 	spin_lock(&block_rsv->lock);
1646ef03debSJosef Bacik 	if (block_rsv->reserved < block_rsv->size) {
1656ef03debSJosef Bacik 		num_bytes = block_rsv->size - block_rsv->reserved;
1666ef03debSJosef Bacik 		num_bytes = min(num_bytes, limit);
1676ef03debSJosef Bacik 	}
1686ef03debSJosef Bacik 	spin_unlock(&block_rsv->lock);
1696ef03debSJosef Bacik 
1706ef03debSJosef Bacik 	if (!num_bytes)
1716ef03debSJosef Bacik 		return 0;
1726ef03debSJosef Bacik 
1739270501cSJosef Bacik 	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
1746ef03debSJosef Bacik 	if (ret)
1756ef03debSJosef Bacik 		return ret;
1762ed45c0fSFilipe Manana 
1772ed45c0fSFilipe Manana 	/*
1782ed45c0fSFilipe Manana 	 * We may have raced with someone else, so check again if we the block
1792ed45c0fSFilipe Manana 	 * reserve is still not full and release any excess space.
1802ed45c0fSFilipe Manana 	 */
1812ed45c0fSFilipe Manana 	spin_lock(&block_rsv->lock);
1822ed45c0fSFilipe Manana 	if (block_rsv->reserved < block_rsv->size) {
1832ed45c0fSFilipe Manana 		u64 needed = block_rsv->size - block_rsv->reserved;
1842ed45c0fSFilipe Manana 
1852ed45c0fSFilipe Manana 		if (num_bytes >= needed) {
1862ed45c0fSFilipe Manana 			block_rsv->reserved += needed;
1872ed45c0fSFilipe Manana 			block_rsv->full = true;
1882ed45c0fSFilipe Manana 			to_free = num_bytes - needed;
1892ed45c0fSFilipe Manana 			refilled_bytes = needed;
1902ed45c0fSFilipe Manana 		} else {
1912ed45c0fSFilipe Manana 			block_rsv->reserved += num_bytes;
1922ed45c0fSFilipe Manana 			to_free = 0;
1932ed45c0fSFilipe Manana 			refilled_bytes = num_bytes;
1942ed45c0fSFilipe Manana 		}
1952ed45c0fSFilipe Manana 	} else {
1962ed45c0fSFilipe Manana 		to_free = num_bytes;
1972ed45c0fSFilipe Manana 		refilled_bytes = 0;
1982ed45c0fSFilipe Manana 	}
1992ed45c0fSFilipe Manana 	spin_unlock(&block_rsv->lock);
2002ed45c0fSFilipe Manana 
2012ed45c0fSFilipe Manana 	if (to_free > 0)
2022ed45c0fSFilipe Manana 		btrfs_space_info_free_bytes_may_use(fs_info, block_rsv->space_info,
2032ed45c0fSFilipe Manana 						    to_free);
2042ed45c0fSFilipe Manana 
2052ed45c0fSFilipe Manana 	if (refilled_bytes > 0)
2062ed45c0fSFilipe Manana 		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", 0,
2072ed45c0fSFilipe Manana 					      refilled_bytes, 1);
2086ef03debSJosef Bacik 	return 0;
2096ef03debSJosef Bacik }
2106ef03debSJosef Bacik 
21156bec294SChris Mason /*
2125d4f98a2SYan Zheng  * compare two delayed tree backrefs with same bytenr and type
21356bec294SChris Mason  */
comp_tree_refs(struct btrfs_delayed_tree_ref * ref1,struct btrfs_delayed_tree_ref * ref2)214c7ad7c84SJosef Bacik static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1,
215c7ad7c84SJosef Bacik 			  struct btrfs_delayed_tree_ref *ref2)
21656bec294SChris Mason {
2173b60d436SJosef Bacik 	if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
2185d4f98a2SYan Zheng 		if (ref1->root < ref2->root)
21956bec294SChris Mason 			return -1;
2205d4f98a2SYan Zheng 		if (ref1->root > ref2->root)
22156bec294SChris Mason 			return 1;
22241b0fc42SJosef Bacik 	} else {
2235d4f98a2SYan Zheng 		if (ref1->parent < ref2->parent)
22456bec294SChris Mason 			return -1;
2255d4f98a2SYan Zheng 		if (ref1->parent > ref2->parent)
22656bec294SChris Mason 			return 1;
22741b0fc42SJosef Bacik 	}
2285d4f98a2SYan Zheng 	return 0;
2295d4f98a2SYan Zheng }
2305d4f98a2SYan Zheng 
2315d4f98a2SYan Zheng /*
2325d4f98a2SYan Zheng  * compare two delayed data backrefs with same bytenr and type
2335d4f98a2SYan Zheng  */
comp_data_refs(struct btrfs_delayed_data_ref * ref1,struct btrfs_delayed_data_ref * ref2)234c7ad7c84SJosef Bacik static int comp_data_refs(struct btrfs_delayed_data_ref *ref1,
235c7ad7c84SJosef Bacik 			  struct btrfs_delayed_data_ref *ref2)
2365d4f98a2SYan Zheng {
2375d4f98a2SYan Zheng 	if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
2385d4f98a2SYan Zheng 		if (ref1->root < ref2->root)
2395d4f98a2SYan Zheng 			return -1;
2405d4f98a2SYan Zheng 		if (ref1->root > ref2->root)
2415d4f98a2SYan Zheng 			return 1;
2425d4f98a2SYan Zheng 		if (ref1->objectid < ref2->objectid)
2435d4f98a2SYan Zheng 			return -1;
2445d4f98a2SYan Zheng 		if (ref1->objectid > ref2->objectid)
2455d4f98a2SYan Zheng 			return 1;
2465d4f98a2SYan Zheng 		if (ref1->offset < ref2->offset)
2475d4f98a2SYan Zheng 			return -1;
2485d4f98a2SYan Zheng 		if (ref1->offset > ref2->offset)
2495d4f98a2SYan Zheng 			return 1;
2505d4f98a2SYan Zheng 	} else {
2515d4f98a2SYan Zheng 		if (ref1->parent < ref2->parent)
2525d4f98a2SYan Zheng 			return -1;
2535d4f98a2SYan Zheng 		if (ref1->parent > ref2->parent)
2545d4f98a2SYan Zheng 			return 1;
2555d4f98a2SYan Zheng 	}
2565d4f98a2SYan Zheng 	return 0;
2575d4f98a2SYan Zheng }
2585d4f98a2SYan Zheng 
comp_refs(struct btrfs_delayed_ref_node * ref1,struct btrfs_delayed_ref_node * ref2,bool check_seq)2591d148e59SJosef Bacik static int comp_refs(struct btrfs_delayed_ref_node *ref1,
2601d148e59SJosef Bacik 		     struct btrfs_delayed_ref_node *ref2,
2611d148e59SJosef Bacik 		     bool check_seq)
2621d148e59SJosef Bacik {
2631d148e59SJosef Bacik 	int ret = 0;
2641d148e59SJosef Bacik 
2651d148e59SJosef Bacik 	if (ref1->type < ref2->type)
2661d148e59SJosef Bacik 		return -1;
2671d148e59SJosef Bacik 	if (ref1->type > ref2->type)
2681d148e59SJosef Bacik 		return 1;
2691d148e59SJosef Bacik 	if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
2701d148e59SJosef Bacik 	    ref1->type == BTRFS_SHARED_BLOCK_REF_KEY)
2711d148e59SJosef Bacik 		ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1),
2721d148e59SJosef Bacik 				     btrfs_delayed_node_to_tree_ref(ref2));
2731d148e59SJosef Bacik 	else
2741d148e59SJosef Bacik 		ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1),
2751d148e59SJosef Bacik 				     btrfs_delayed_node_to_data_ref(ref2));
2761d148e59SJosef Bacik 	if (ret)
2771d148e59SJosef Bacik 		return ret;
2781d148e59SJosef Bacik 	if (check_seq) {
2791d148e59SJosef Bacik 		if (ref1->seq < ref2->seq)
2801d148e59SJosef Bacik 			return -1;
2811d148e59SJosef Bacik 		if (ref1->seq > ref2->seq)
2821d148e59SJosef Bacik 			return 1;
2831d148e59SJosef Bacik 	}
2841d148e59SJosef Bacik 	return 0;
2851d148e59SJosef Bacik }
2861d148e59SJosef Bacik 
287c46effa6SLiu Bo /* insert a new ref to head ref rbtree */
htree_insert(struct rb_root_cached * root,struct rb_node * node)2885c9d028bSLiu Bo static struct btrfs_delayed_ref_head *htree_insert(struct rb_root_cached *root,
289c46effa6SLiu Bo 						   struct rb_node *node)
290c46effa6SLiu Bo {
2915c9d028bSLiu Bo 	struct rb_node **p = &root->rb_root.rb_node;
292c46effa6SLiu Bo 	struct rb_node *parent_node = NULL;
293c46effa6SLiu Bo 	struct btrfs_delayed_ref_head *entry;
294c46effa6SLiu Bo 	struct btrfs_delayed_ref_head *ins;
295c46effa6SLiu Bo 	u64 bytenr;
2965c9d028bSLiu Bo 	bool leftmost = true;
297c46effa6SLiu Bo 
298c46effa6SLiu Bo 	ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
299d278850eSJosef Bacik 	bytenr = ins->bytenr;
300c46effa6SLiu Bo 	while (*p) {
301c46effa6SLiu Bo 		parent_node = *p;
302c46effa6SLiu Bo 		entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
303c46effa6SLiu Bo 				 href_node);
304c46effa6SLiu Bo 
3055c9d028bSLiu Bo 		if (bytenr < entry->bytenr) {
306c46effa6SLiu Bo 			p = &(*p)->rb_left;
3075c9d028bSLiu Bo 		} else if (bytenr > entry->bytenr) {
308c46effa6SLiu Bo 			p = &(*p)->rb_right;
3095c9d028bSLiu Bo 			leftmost = false;
3105c9d028bSLiu Bo 		} else {
311c46effa6SLiu Bo 			return entry;
312c46effa6SLiu Bo 		}
3135c9d028bSLiu Bo 	}
314c46effa6SLiu Bo 
315c46effa6SLiu Bo 	rb_link_node(node, parent_node, p);
3165c9d028bSLiu Bo 	rb_insert_color_cached(node, root, leftmost);
317c46effa6SLiu Bo 	return NULL;
318c46effa6SLiu Bo }
319c46effa6SLiu Bo 
tree_insert(struct rb_root_cached * root,struct btrfs_delayed_ref_node * ins)320e3d03965SLiu Bo static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root,
3210e0adbcfSJosef Bacik 		struct btrfs_delayed_ref_node *ins)
3220e0adbcfSJosef Bacik {
323e3d03965SLiu Bo 	struct rb_node **p = &root->rb_root.rb_node;
3240e0adbcfSJosef Bacik 	struct rb_node *node = &ins->ref_node;
3250e0adbcfSJosef Bacik 	struct rb_node *parent_node = NULL;
3260e0adbcfSJosef Bacik 	struct btrfs_delayed_ref_node *entry;
327e3d03965SLiu Bo 	bool leftmost = true;
3280e0adbcfSJosef Bacik 
3290e0adbcfSJosef Bacik 	while (*p) {
3300e0adbcfSJosef Bacik 		int comp;
3310e0adbcfSJosef Bacik 
3320e0adbcfSJosef Bacik 		parent_node = *p;
3330e0adbcfSJosef Bacik 		entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
3340e0adbcfSJosef Bacik 				 ref_node);
3350e0adbcfSJosef Bacik 		comp = comp_refs(ins, entry, true);
336e3d03965SLiu Bo 		if (comp < 0) {
3370e0adbcfSJosef Bacik 			p = &(*p)->rb_left;
338e3d03965SLiu Bo 		} else if (comp > 0) {
3390e0adbcfSJosef Bacik 			p = &(*p)->rb_right;
340e3d03965SLiu Bo 			leftmost = false;
341e3d03965SLiu Bo 		} else {
3420e0adbcfSJosef Bacik 			return entry;
3430e0adbcfSJosef Bacik 		}
344e3d03965SLiu Bo 	}
3450e0adbcfSJosef Bacik 
3460e0adbcfSJosef Bacik 	rb_link_node(node, parent_node, p);
347e3d03965SLiu Bo 	rb_insert_color_cached(node, root, leftmost);
3480e0adbcfSJosef Bacik 	return NULL;
3490e0adbcfSJosef Bacik }
3500e0adbcfSJosef Bacik 
find_first_ref_head(struct btrfs_delayed_ref_root * dr)3510a9df0dfSLu Fengqi static struct btrfs_delayed_ref_head *find_first_ref_head(
3520a9df0dfSLu Fengqi 		struct btrfs_delayed_ref_root *dr)
3530a9df0dfSLu Fengqi {
3540a9df0dfSLu Fengqi 	struct rb_node *n;
3550a9df0dfSLu Fengqi 	struct btrfs_delayed_ref_head *entry;
3560a9df0dfSLu Fengqi 
3570a9df0dfSLu Fengqi 	n = rb_first_cached(&dr->href_root);
3580a9df0dfSLu Fengqi 	if (!n)
3590a9df0dfSLu Fengqi 		return NULL;
3600a9df0dfSLu Fengqi 
3610a9df0dfSLu Fengqi 	entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
3620a9df0dfSLu Fengqi 
3630a9df0dfSLu Fengqi 	return entry;
3640a9df0dfSLu Fengqi }
3650a9df0dfSLu Fengqi 
36656bec294SChris Mason /*
3670a9df0dfSLu Fengqi  * Find a head entry based on bytenr. This returns the delayed ref head if it
3680a9df0dfSLu Fengqi  * was able to find one, or NULL if nothing was in that spot.  If return_bigger
3690a9df0dfSLu Fengqi  * is given, the next bigger entry is returned if no exact match is found.
37056bec294SChris Mason  */
find_ref_head(struct btrfs_delayed_ref_root * dr,u64 bytenr,bool return_bigger)3715c9d028bSLiu Bo static struct btrfs_delayed_ref_head *find_ref_head(
3725c9d028bSLiu Bo 		struct btrfs_delayed_ref_root *dr, u64 bytenr,
373d9352794SLu Fengqi 		bool return_bigger)
37456bec294SChris Mason {
3755c9d028bSLiu Bo 	struct rb_root *root = &dr->href_root.rb_root;
376d1270cd9SArne Jansen 	struct rb_node *n;
377c46effa6SLiu Bo 	struct btrfs_delayed_ref_head *entry;
37856bec294SChris Mason 
379d1270cd9SArne Jansen 	n = root->rb_node;
380d1270cd9SArne Jansen 	entry = NULL;
38156bec294SChris Mason 	while (n) {
382c46effa6SLiu Bo 		entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
38356bec294SChris Mason 
384d278850eSJosef Bacik 		if (bytenr < entry->bytenr)
38556bec294SChris Mason 			n = n->rb_left;
386d278850eSJosef Bacik 		else if (bytenr > entry->bytenr)
38756bec294SChris Mason 			n = n->rb_right;
38856bec294SChris Mason 		else
38956bec294SChris Mason 			return entry;
39056bec294SChris Mason 	}
391d1270cd9SArne Jansen 	if (entry && return_bigger) {
392d278850eSJosef Bacik 		if (bytenr > entry->bytenr) {
393c46effa6SLiu Bo 			n = rb_next(&entry->href_node);
394d1270cd9SArne Jansen 			if (!n)
3950a9df0dfSLu Fengqi 				return NULL;
396c46effa6SLiu Bo 			entry = rb_entry(n, struct btrfs_delayed_ref_head,
397c46effa6SLiu Bo 					 href_node);
398d1270cd9SArne Jansen 		}
399d1270cd9SArne Jansen 		return entry;
400d1270cd9SArne Jansen 	}
40156bec294SChris Mason 	return NULL;
40256bec294SChris Mason }
40356bec294SChris Mason 
btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root * delayed_refs,struct btrfs_delayed_ref_head * head)4049e920a6fSLu Fengqi int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
405c3e69d58SChris Mason 			   struct btrfs_delayed_ref_head *head)
40656bec294SChris Mason {
407a4666e68SDavid Sterba 	lockdep_assert_held(&delayed_refs->lock);
408c3e69d58SChris Mason 	if (mutex_trylock(&head->mutex))
409c3e69d58SChris Mason 		return 0;
410c3e69d58SChris Mason 
411d278850eSJosef Bacik 	refcount_inc(&head->refs);
412c3e69d58SChris Mason 	spin_unlock(&delayed_refs->lock);
413c3e69d58SChris Mason 
414c3e69d58SChris Mason 	mutex_lock(&head->mutex);
415c3e69d58SChris Mason 	spin_lock(&delayed_refs->lock);
416d278850eSJosef Bacik 	if (RB_EMPTY_NODE(&head->href_node)) {
417c3e69d58SChris Mason 		mutex_unlock(&head->mutex);
418d278850eSJosef Bacik 		btrfs_put_delayed_ref_head(head);
419c3e69d58SChris Mason 		return -EAGAIN;
420c3e69d58SChris Mason 	}
421d278850eSJosef Bacik 	btrfs_put_delayed_ref_head(head);
422c3e69d58SChris Mason 	return 0;
423c3e69d58SChris Mason }
424c3e69d58SChris Mason 
drop_delayed_ref(struct btrfs_delayed_ref_root * delayed_refs,struct btrfs_delayed_ref_head * head,struct btrfs_delayed_ref_node * ref)4254c89493fSJohannes Thumshirn static inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs,
426d7df2c79SJosef Bacik 				    struct btrfs_delayed_ref_head *head,
427ae1e206bSJosef Bacik 				    struct btrfs_delayed_ref_node *ref)
428ae1e206bSJosef Bacik {
429a4666e68SDavid Sterba 	lockdep_assert_held(&head->lock);
430e3d03965SLiu Bo 	rb_erase_cached(&ref->ref_node, &head->ref_tree);
4310e0adbcfSJosef Bacik 	RB_CLEAR_NODE(&ref->ref_node);
4321d57ee94SWang Xiaoguang 	if (!list_empty(&ref->add_list))
4331d57ee94SWang Xiaoguang 		list_del(&ref->add_list);
434ae1e206bSJosef Bacik 	btrfs_put_delayed_ref(ref);
435d7df2c79SJosef Bacik 	atomic_dec(&delayed_refs->num_entries);
436ae1e206bSJosef Bacik }
437ae1e206bSJosef Bacik 
merge_ref(struct btrfs_delayed_ref_root * delayed_refs,struct btrfs_delayed_ref_head * head,struct btrfs_delayed_ref_node * ref,u64 seq)438f09f7851SJohannes Thumshirn static bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs,
4392c3cf7d5SFilipe Manana 		      struct btrfs_delayed_ref_head *head,
4402c3cf7d5SFilipe Manana 		      struct btrfs_delayed_ref_node *ref,
4412c3cf7d5SFilipe Manana 		      u64 seq)
4422c3cf7d5SFilipe Manana {
4432c3cf7d5SFilipe Manana 	struct btrfs_delayed_ref_node *next;
4440e0adbcfSJosef Bacik 	struct rb_node *node = rb_next(&ref->ref_node);
4452c3cf7d5SFilipe Manana 	bool done = false;
4462c3cf7d5SFilipe Manana 
4470e0adbcfSJosef Bacik 	while (!done && node) {
4482c3cf7d5SFilipe Manana 		int mod;
4492c3cf7d5SFilipe Manana 
4500e0adbcfSJosef Bacik 		next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
4510e0adbcfSJosef Bacik 		node = rb_next(node);
4522c3cf7d5SFilipe Manana 		if (seq && next->seq >= seq)
4530e0adbcfSJosef Bacik 			break;
4541d148e59SJosef Bacik 		if (comp_refs(ref, next, false))
4550e0adbcfSJosef Bacik 			break;
4562c3cf7d5SFilipe Manana 
4572c3cf7d5SFilipe Manana 		if (ref->action == next->action) {
4582c3cf7d5SFilipe Manana 			mod = next->ref_mod;
4592c3cf7d5SFilipe Manana 		} else {
4602c3cf7d5SFilipe Manana 			if (ref->ref_mod < next->ref_mod) {
4612c3cf7d5SFilipe Manana 				swap(ref, next);
4622c3cf7d5SFilipe Manana 				done = true;
4632c3cf7d5SFilipe Manana 			}
4642c3cf7d5SFilipe Manana 			mod = -next->ref_mod;
4652c3cf7d5SFilipe Manana 		}
4662c3cf7d5SFilipe Manana 
4674c89493fSJohannes Thumshirn 		drop_delayed_ref(delayed_refs, head, next);
4682c3cf7d5SFilipe Manana 		ref->ref_mod += mod;
4692c3cf7d5SFilipe Manana 		if (ref->ref_mod == 0) {
4704c89493fSJohannes Thumshirn 			drop_delayed_ref(delayed_refs, head, ref);
4712c3cf7d5SFilipe Manana 			done = true;
4722c3cf7d5SFilipe Manana 		} else {
4732c3cf7d5SFilipe Manana 			/*
4742c3cf7d5SFilipe Manana 			 * Can't have multiples of the same ref on a tree block.
4752c3cf7d5SFilipe Manana 			 */
4762c3cf7d5SFilipe Manana 			WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
4772c3cf7d5SFilipe Manana 				ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
4782c3cf7d5SFilipe Manana 		}
4792c3cf7d5SFilipe Manana 	}
4802c3cf7d5SFilipe Manana 
4812c3cf7d5SFilipe Manana 	return done;
4822c3cf7d5SFilipe Manana }
4832c3cf7d5SFilipe Manana 
btrfs_merge_delayed_refs(struct btrfs_fs_info * fs_info,struct btrfs_delayed_ref_root * delayed_refs,struct btrfs_delayed_ref_head * head)4840c555c97SJohannes Thumshirn void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
4852c3cf7d5SFilipe Manana 			      struct btrfs_delayed_ref_root *delayed_refs,
4862c3cf7d5SFilipe Manana 			      struct btrfs_delayed_ref_head *head)
4872c3cf7d5SFilipe Manana {
4882c3cf7d5SFilipe Manana 	struct btrfs_delayed_ref_node *ref;
4890e0adbcfSJosef Bacik 	struct rb_node *node;
4902c3cf7d5SFilipe Manana 	u64 seq = 0;
4912c3cf7d5SFilipe Manana 
492a4666e68SDavid Sterba 	lockdep_assert_held(&head->lock);
4932c3cf7d5SFilipe Manana 
494e3d03965SLiu Bo 	if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
4952c3cf7d5SFilipe Manana 		return;
4962c3cf7d5SFilipe Manana 
4972c3cf7d5SFilipe Manana 	/* We don't have too many refs to merge for data. */
4982c3cf7d5SFilipe Manana 	if (head->is_data)
4992c3cf7d5SFilipe Manana 		return;
5002c3cf7d5SFilipe Manana 
5014bae7880SFilipe Manana 	seq = btrfs_tree_mod_log_lowest_seq(fs_info);
5020e0adbcfSJosef Bacik again:
503e3d03965SLiu Bo 	for (node = rb_first_cached(&head->ref_tree); node;
504e3d03965SLiu Bo 	     node = rb_next(node)) {
5050e0adbcfSJosef Bacik 		ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
5062c3cf7d5SFilipe Manana 		if (seq && ref->seq >= seq)
5072c3cf7d5SFilipe Manana 			continue;
508f09f7851SJohannes Thumshirn 		if (merge_ref(delayed_refs, head, ref, seq))
5090e0adbcfSJosef Bacik 			goto again;
5102c3cf7d5SFilipe Manana 	}
5112c3cf7d5SFilipe Manana }
5122c3cf7d5SFilipe Manana 
btrfs_check_delayed_seq(struct btrfs_fs_info * fs_info,u64 seq)51341d0bd3bSNikolay Borisov int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
51400f04b88SArne Jansen {
515097b8a7cSJan Schmidt 	int ret = 0;
5164bae7880SFilipe Manana 	u64 min_seq = btrfs_tree_mod_log_lowest_seq(fs_info);
51700f04b88SArne Jansen 
5184bae7880SFilipe Manana 	if (min_seq != 0 && seq >= min_seq) {
519ab8d0fc4SJeff Mahoney 		btrfs_debug(fs_info,
520ffbc10a1SFilipe Manana 			    "holding back delayed_ref %llu, lowest is %llu",
521ffbc10a1SFilipe Manana 			    seq, min_seq);
522097b8a7cSJan Schmidt 		ret = 1;
52300f04b88SArne Jansen 	}
524097b8a7cSJan Schmidt 
525097b8a7cSJan Schmidt 	return ret;
52600f04b88SArne Jansen }
52700f04b88SArne Jansen 
btrfs_select_ref_head(struct btrfs_delayed_ref_root * delayed_refs)5285637c74bSLu Fengqi struct btrfs_delayed_ref_head *btrfs_select_ref_head(
5295637c74bSLu Fengqi 		struct btrfs_delayed_ref_root *delayed_refs)
530c3e69d58SChris Mason {
531d7df2c79SJosef Bacik 	struct btrfs_delayed_ref_head *head;
532c3e69d58SChris Mason 
5331e6b71c3SFilipe Manana 	lockdep_assert_held(&delayed_refs->lock);
534c3e69d58SChris Mason again:
5350a9df0dfSLu Fengqi 	head = find_ref_head(delayed_refs, delayed_refs->run_delayed_start,
5360a9df0dfSLu Fengqi 			     true);
5370a9df0dfSLu Fengqi 	if (!head && delayed_refs->run_delayed_start != 0) {
538d7df2c79SJosef Bacik 		delayed_refs->run_delayed_start = 0;
5390a9df0dfSLu Fengqi 		head = find_first_ref_head(delayed_refs);
5400a9df0dfSLu Fengqi 	}
541d7df2c79SJosef Bacik 	if (!head)
542d7df2c79SJosef Bacik 		return NULL;
543d7df2c79SJosef Bacik 
544d7df2c79SJosef Bacik 	while (head->processing) {
545d7df2c79SJosef Bacik 		struct rb_node *node;
546d7df2c79SJosef Bacik 
547d7df2c79SJosef Bacik 		node = rb_next(&head->href_node);
548d7df2c79SJosef Bacik 		if (!node) {
5490a9df0dfSLu Fengqi 			if (delayed_refs->run_delayed_start == 0)
550d7df2c79SJosef Bacik 				return NULL;
551d7df2c79SJosef Bacik 			delayed_refs->run_delayed_start = 0;
552c3e69d58SChris Mason 			goto again;
55356bec294SChris Mason 		}
554d7df2c79SJosef Bacik 		head = rb_entry(node, struct btrfs_delayed_ref_head,
555d7df2c79SJosef Bacik 				href_node);
55656bec294SChris Mason 	}
55756bec294SChris Mason 
55861c681feSFilipe Manana 	head->processing = true;
559d7df2c79SJosef Bacik 	WARN_ON(delayed_refs->num_heads_ready == 0);
560d7df2c79SJosef Bacik 	delayed_refs->num_heads_ready--;
561d278850eSJosef Bacik 	delayed_refs->run_delayed_start = head->bytenr +
562d278850eSJosef Bacik 		head->num_bytes;
563d7df2c79SJosef Bacik 	return head;
564093486c4SMiao Xie }
565093486c4SMiao Xie 
btrfs_delete_ref_head(struct btrfs_delayed_ref_root * delayed_refs,struct btrfs_delayed_ref_head * head)566d7baffdaSJosef Bacik void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
567d7baffdaSJosef Bacik 			   struct btrfs_delayed_ref_head *head)
568d7baffdaSJosef Bacik {
569d7baffdaSJosef Bacik 	lockdep_assert_held(&delayed_refs->lock);
570d7baffdaSJosef Bacik 	lockdep_assert_held(&head->lock);
571d7baffdaSJosef Bacik 
572d7baffdaSJosef Bacik 	rb_erase_cached(&head->href_node, &delayed_refs->href_root);
573d7baffdaSJosef Bacik 	RB_CLEAR_NODE(&head->href_node);
574d7baffdaSJosef Bacik 	atomic_dec(&delayed_refs->num_entries);
575d7baffdaSJosef Bacik 	delayed_refs->num_heads--;
57661c681feSFilipe Manana 	if (!head->processing)
577d7baffdaSJosef Bacik 		delayed_refs->num_heads_ready--;
578d7baffdaSJosef Bacik }
579d7baffdaSJosef Bacik 
58056bec294SChris Mason /*
581c6fc2454SQu Wenruo  * Helper to insert the ref_node to the tail or merge with tail.
582c6fc2454SQu Wenruo  *
583f38462c4SFilipe Manana  * Return false if the ref was inserted.
584f38462c4SFilipe Manana  * Return true if the ref was merged into an existing one (and therefore can be
585f38462c4SFilipe Manana  * freed by the caller).
586c6fc2454SQu Wenruo  */
insert_delayed_ref(struct btrfs_delayed_ref_root * root,struct btrfs_delayed_ref_head * href,struct btrfs_delayed_ref_node * ref)587f38462c4SFilipe Manana static bool insert_delayed_ref(struct btrfs_delayed_ref_root *root,
588c6fc2454SQu Wenruo 			       struct btrfs_delayed_ref_head *href,
589c6fc2454SQu Wenruo 			       struct btrfs_delayed_ref_node *ref)
590c6fc2454SQu Wenruo {
591c6fc2454SQu Wenruo 	struct btrfs_delayed_ref_node *exist;
592c6fc2454SQu Wenruo 	int mod;
593c6fc2454SQu Wenruo 
594c6fc2454SQu Wenruo 	spin_lock(&href->lock);
5950e0adbcfSJosef Bacik 	exist = tree_insert(&href->ref_tree, ref);
596798f4d95SFilipe Manana 	if (!exist) {
597798f4d95SFilipe Manana 		if (ref->action == BTRFS_ADD_DELAYED_REF)
598798f4d95SFilipe Manana 			list_add_tail(&ref->add_list, &href->ref_add_list);
599798f4d95SFilipe Manana 		atomic_inc(&root->num_entries);
600798f4d95SFilipe Manana 		spin_unlock(&href->lock);
601798f4d95SFilipe Manana 		return false;
602798f4d95SFilipe Manana 	}
603c6fc2454SQu Wenruo 
604c6fc2454SQu Wenruo 	/* Now we are sure we can merge */
605c6fc2454SQu Wenruo 	if (exist->action == ref->action) {
606c6fc2454SQu Wenruo 		mod = ref->ref_mod;
607c6fc2454SQu Wenruo 	} else {
608c6fc2454SQu Wenruo 		/* Need to change action */
609c6fc2454SQu Wenruo 		if (exist->ref_mod < ref->ref_mod) {
610c6fc2454SQu Wenruo 			exist->action = ref->action;
611c6fc2454SQu Wenruo 			mod = -exist->ref_mod;
612c6fc2454SQu Wenruo 			exist->ref_mod = ref->ref_mod;
6131d57ee94SWang Xiaoguang 			if (ref->action == BTRFS_ADD_DELAYED_REF)
6141d57ee94SWang Xiaoguang 				list_add_tail(&exist->add_list,
6151d57ee94SWang Xiaoguang 					      &href->ref_add_list);
6161d57ee94SWang Xiaoguang 			else if (ref->action == BTRFS_DROP_DELAYED_REF) {
6171d57ee94SWang Xiaoguang 				ASSERT(!list_empty(&exist->add_list));
6181d57ee94SWang Xiaoguang 				list_del(&exist->add_list);
6191d57ee94SWang Xiaoguang 			} else {
6201d57ee94SWang Xiaoguang 				ASSERT(0);
6211d57ee94SWang Xiaoguang 			}
622c6fc2454SQu Wenruo 		} else
623c6fc2454SQu Wenruo 			mod = -ref->ref_mod;
624c6fc2454SQu Wenruo 	}
625c6fc2454SQu Wenruo 	exist->ref_mod += mod;
626c6fc2454SQu Wenruo 
627c6fc2454SQu Wenruo 	/* remove existing tail if its ref_mod is zero */
628c6fc2454SQu Wenruo 	if (exist->ref_mod == 0)
6294c89493fSJohannes Thumshirn 		drop_delayed_ref(root, href, exist);
630c6fc2454SQu Wenruo 	spin_unlock(&href->lock);
631798f4d95SFilipe Manana 	return true;
632c6fc2454SQu Wenruo }
633c6fc2454SQu Wenruo 
634c6fc2454SQu Wenruo /*
63556bec294SChris Mason  * helper function to update the accounting in the head ref
63656bec294SChris Mason  * existing and update must have the same bytenr
63756bec294SChris Mason  */
update_existing_head_ref(struct btrfs_trans_handle * trans,struct btrfs_delayed_ref_head * existing,struct btrfs_delayed_ref_head * update)638ba2c4d4eSJosef Bacik static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans,
639d278850eSJosef Bacik 			 struct btrfs_delayed_ref_head *existing,
6402187374fSJosef Bacik 			 struct btrfs_delayed_ref_head *update)
64156bec294SChris Mason {
642ba2c4d4eSJosef Bacik 	struct btrfs_delayed_ref_root *delayed_refs =
643ba2c4d4eSJosef Bacik 		&trans->transaction->delayed_refs;
644ba2c4d4eSJosef Bacik 	struct btrfs_fs_info *fs_info = trans->fs_info;
6451262133bSJosef Bacik 	int old_ref_mod;
64656bec294SChris Mason 
647d278850eSJosef Bacik 	BUG_ON(existing->is_data != update->is_data);
64856bec294SChris Mason 
649d278850eSJosef Bacik 	spin_lock(&existing->lock);
650d278850eSJosef Bacik 	if (update->must_insert_reserved) {
65156bec294SChris Mason 		/* if the extent was freed and then
65256bec294SChris Mason 		 * reallocated before the delayed ref
65356bec294SChris Mason 		 * entries were processed, we can end up
65456bec294SChris Mason 		 * with an existing head ref without
65556bec294SChris Mason 		 * the must_insert_reserved flag set.
65656bec294SChris Mason 		 * Set it again here
65756bec294SChris Mason 		 */
658d278850eSJosef Bacik 		existing->must_insert_reserved = update->must_insert_reserved;
65956bec294SChris Mason 
66056bec294SChris Mason 		/*
66156bec294SChris Mason 		 * update the num_bytes so we make sure the accounting
66256bec294SChris Mason 		 * is done correctly
66356bec294SChris Mason 		 */
66456bec294SChris Mason 		existing->num_bytes = update->num_bytes;
66556bec294SChris Mason 
66656bec294SChris Mason 	}
66756bec294SChris Mason 
668d278850eSJosef Bacik 	if (update->extent_op) {
669d278850eSJosef Bacik 		if (!existing->extent_op) {
670d278850eSJosef Bacik 			existing->extent_op = update->extent_op;
6715d4f98a2SYan Zheng 		} else {
672d278850eSJosef Bacik 			if (update->extent_op->update_key) {
673d278850eSJosef Bacik 				memcpy(&existing->extent_op->key,
674d278850eSJosef Bacik 				       &update->extent_op->key,
675d278850eSJosef Bacik 				       sizeof(update->extent_op->key));
676d278850eSJosef Bacik 				existing->extent_op->update_key = true;
6775d4f98a2SYan Zheng 			}
678d278850eSJosef Bacik 			if (update->extent_op->update_flags) {
679d278850eSJosef Bacik 				existing->extent_op->flags_to_set |=
680d278850eSJosef Bacik 					update->extent_op->flags_to_set;
681d278850eSJosef Bacik 				existing->extent_op->update_flags = true;
6825d4f98a2SYan Zheng 			}
683d278850eSJosef Bacik 			btrfs_free_delayed_extent_op(update->extent_op);
6845d4f98a2SYan Zheng 		}
6855d4f98a2SYan Zheng 	}
68656bec294SChris Mason 	/*
687d7df2c79SJosef Bacik 	 * update the reference mod on the head to reflect this new operation,
688d7df2c79SJosef Bacik 	 * only need the lock for this case cause we could be processing it
689d7df2c79SJosef Bacik 	 * currently, for refs we just added we know we're a-ok.
69056bec294SChris Mason 	 */
691d278850eSJosef Bacik 	old_ref_mod = existing->total_ref_mod;
69256bec294SChris Mason 	existing->ref_mod += update->ref_mod;
693d278850eSJosef Bacik 	existing->total_ref_mod += update->ref_mod;
6941262133bSJosef Bacik 
6951262133bSJosef Bacik 	/*
6961262133bSJosef Bacik 	 * If we are going to from a positive ref mod to a negative or vice
6971262133bSJosef Bacik 	 * versa we need to make sure to adjust pending_csums accordingly.
6981262133bSJosef Bacik 	 */
699d278850eSJosef Bacik 	if (existing->is_data) {
700ba2c4d4eSJosef Bacik 		u64 csum_leaves =
701ba2c4d4eSJosef Bacik 			btrfs_csum_bytes_to_leaves(fs_info,
702ba2c4d4eSJosef Bacik 						   existing->num_bytes);
703ba2c4d4eSJosef Bacik 
704ba2c4d4eSJosef Bacik 		if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
7051262133bSJosef Bacik 			delayed_refs->pending_csums -= existing->num_bytes;
706ba2c4d4eSJosef Bacik 			btrfs_delayed_refs_rsv_release(fs_info, csum_leaves);
707ba2c4d4eSJosef Bacik 		}
708ba2c4d4eSJosef Bacik 		if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
7091262133bSJosef Bacik 			delayed_refs->pending_csums += existing->num_bytes;
710ba2c4d4eSJosef Bacik 			trans->delayed_ref_updates += csum_leaves;
711ba2c4d4eSJosef Bacik 		}
7121262133bSJosef Bacik 	}
7132187374fSJosef Bacik 
714d278850eSJosef Bacik 	spin_unlock(&existing->lock);
71556bec294SChris Mason }
71656bec294SChris Mason 
init_delayed_ref_head(struct btrfs_delayed_ref_head * head_ref,struct btrfs_qgroup_extent_record * qrecord,u64 bytenr,u64 num_bytes,u64 ref_root,u64 reserved,int action,bool is_data,bool is_system)717a2e569b3SNikolay Borisov static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
718a2e569b3SNikolay Borisov 				  struct btrfs_qgroup_extent_record *qrecord,
719a2e569b3SNikolay Borisov 				  u64 bytenr, u64 num_bytes, u64 ref_root,
720a2e569b3SNikolay Borisov 				  u64 reserved, int action, bool is_data,
721a2e569b3SNikolay Borisov 				  bool is_system)
722a2e569b3SNikolay Borisov {
723a2e569b3SNikolay Borisov 	int count_mod = 1;
72461c681feSFilipe Manana 	bool must_insert_reserved = false;
725a2e569b3SNikolay Borisov 
726a2e569b3SNikolay Borisov 	/* If reserved is provided, it must be a data extent. */
727a2e569b3SNikolay Borisov 	BUG_ON(!is_data && reserved);
728a2e569b3SNikolay Borisov 
729f1ed785aSFilipe Manana 	switch (action) {
730f1ed785aSFilipe Manana 	case BTRFS_UPDATE_DELAYED_HEAD:
731f1ed785aSFilipe Manana 		count_mod = 0;
732f1ed785aSFilipe Manana 		break;
733f1ed785aSFilipe Manana 	case BTRFS_DROP_DELAYED_REF:
734a2e569b3SNikolay Borisov 		/*
735a2e569b3SNikolay Borisov 		 * The head node stores the sum of all the mods, so dropping a ref
736a2e569b3SNikolay Borisov 		 * should drop the sum in the head node by one.
737a2e569b3SNikolay Borisov 		 */
738a2e569b3SNikolay Borisov 		count_mod = -1;
739f1ed785aSFilipe Manana 		break;
740f1ed785aSFilipe Manana 	case BTRFS_ADD_DELAYED_EXTENT:
741a2e569b3SNikolay Borisov 		/*
742f1ed785aSFilipe Manana 		 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the
743f1ed785aSFilipe Manana 		 * reserved accounting when the extent is finally added, or if a
744f1ed785aSFilipe Manana 		 * later modification deletes the delayed ref without ever
745f1ed785aSFilipe Manana 		 * inserting the extent into the extent allocation tree.
746f1ed785aSFilipe Manana 		 * ref->must_insert_reserved is the flag used to record that
747f1ed785aSFilipe Manana 		 * accounting mods are required.
748a2e569b3SNikolay Borisov 		 *
749a2e569b3SNikolay Borisov 		 * Once we record must_insert_reserved, switch the action to
750f1ed785aSFilipe Manana 		 * BTRFS_ADD_DELAYED_REF because other special casing is not
751f1ed785aSFilipe Manana 		 * required.
752a2e569b3SNikolay Borisov 		 */
75361c681feSFilipe Manana 		must_insert_reserved = true;
754f1ed785aSFilipe Manana 		break;
755f1ed785aSFilipe Manana 	}
756a2e569b3SNikolay Borisov 
757a2e569b3SNikolay Borisov 	refcount_set(&head_ref->refs, 1);
758a2e569b3SNikolay Borisov 	head_ref->bytenr = bytenr;
759a2e569b3SNikolay Borisov 	head_ref->num_bytes = num_bytes;
760a2e569b3SNikolay Borisov 	head_ref->ref_mod = count_mod;
761a2e569b3SNikolay Borisov 	head_ref->must_insert_reserved = must_insert_reserved;
762a2e569b3SNikolay Borisov 	head_ref->is_data = is_data;
763a2e569b3SNikolay Borisov 	head_ref->is_system = is_system;
764e3d03965SLiu Bo 	head_ref->ref_tree = RB_ROOT_CACHED;
765a2e569b3SNikolay Borisov 	INIT_LIST_HEAD(&head_ref->ref_add_list);
766a2e569b3SNikolay Borisov 	RB_CLEAR_NODE(&head_ref->href_node);
76761c681feSFilipe Manana 	head_ref->processing = false;
768a2e569b3SNikolay Borisov 	head_ref->total_ref_mod = count_mod;
769a2e569b3SNikolay Borisov 	spin_lock_init(&head_ref->lock);
770a2e569b3SNikolay Borisov 	mutex_init(&head_ref->mutex);
771a2e569b3SNikolay Borisov 
772a2e569b3SNikolay Borisov 	if (qrecord) {
773a2e569b3SNikolay Borisov 		if (ref_root && reserved) {
7741418bae1SQu Wenruo 			qrecord->data_rsv = reserved;
7751418bae1SQu Wenruo 			qrecord->data_rsv_refroot = ref_root;
776a2e569b3SNikolay Borisov 		}
777a2e569b3SNikolay Borisov 		qrecord->bytenr = bytenr;
778a2e569b3SNikolay Borisov 		qrecord->num_bytes = num_bytes;
779a2e569b3SNikolay Borisov 		qrecord->old_roots = NULL;
780a2e569b3SNikolay Borisov 	}
781a2e569b3SNikolay Borisov }
782a2e569b3SNikolay Borisov 
78356bec294SChris Mason /*
7845d4f98a2SYan Zheng  * helper function to actually insert a head node into the rbtree.
78556bec294SChris Mason  * this does all the dirty work in terms of maintaining the correct
7865d4f98a2SYan Zheng  * overall modification count.
78756bec294SChris Mason  */
788d7df2c79SJosef Bacik static noinline struct btrfs_delayed_ref_head *
add_delayed_ref_head(struct btrfs_trans_handle * trans,struct btrfs_delayed_ref_head * head_ref,struct btrfs_qgroup_extent_record * qrecord,int action,bool * qrecord_inserted_ret)7891acda0c2SNikolay Borisov add_delayed_ref_head(struct btrfs_trans_handle *trans,
790d278850eSJosef Bacik 		     struct btrfs_delayed_ref_head *head_ref,
7913368d001SQu Wenruo 		     struct btrfs_qgroup_extent_record *qrecord,
792293f8197SFilipe Manana 		     int action, bool *qrecord_inserted_ret)
79356bec294SChris Mason {
794d7df2c79SJosef Bacik 	struct btrfs_delayed_ref_head *existing;
79556bec294SChris Mason 	struct btrfs_delayed_ref_root *delayed_refs;
796293f8197SFilipe Manana 	bool qrecord_inserted = false;
79756bec294SChris Mason 
79856bec294SChris Mason 	delayed_refs = &trans->transaction->delayed_refs;
7992335efafSNikolay Borisov 
8003368d001SQu Wenruo 	/* Record qgroup extent info if provided */
8013368d001SQu Wenruo 	if (qrecord) {
8021acda0c2SNikolay Borisov 		if (btrfs_qgroup_trace_extent_nolock(trans->fs_info,
803cb93b52cSQu Wenruo 					delayed_refs, qrecord))
8043368d001SQu Wenruo 			kfree(qrecord);
805fb235dc0SQu Wenruo 		else
806293f8197SFilipe Manana 			qrecord_inserted = true;
8073368d001SQu Wenruo 	}
8083368d001SQu Wenruo 
8091acda0c2SNikolay Borisov 	trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
8101abe9b8aSliubo 
811d7df2c79SJosef Bacik 	existing = htree_insert(&delayed_refs->href_root,
812d7df2c79SJosef Bacik 				&head_ref->href_node);
81356bec294SChris Mason 	if (existing) {
8142187374fSJosef Bacik 		update_existing_head_ref(trans, existing, head_ref);
81556bec294SChris Mason 		/*
81656bec294SChris Mason 		 * we've updated the existing ref, free the newly
81756bec294SChris Mason 		 * allocated ref
81856bec294SChris Mason 		 */
81978a6184aSMiao Xie 		kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
820d7df2c79SJosef Bacik 		head_ref = existing;
82156bec294SChris Mason 	} else {
822ba2c4d4eSJosef Bacik 		if (head_ref->is_data && head_ref->ref_mod < 0) {
8232335efafSNikolay Borisov 			delayed_refs->pending_csums += head_ref->num_bytes;
824ba2c4d4eSJosef Bacik 			trans->delayed_ref_updates +=
825ba2c4d4eSJosef Bacik 				btrfs_csum_bytes_to_leaves(trans->fs_info,
826ba2c4d4eSJosef Bacik 							   head_ref->num_bytes);
827ba2c4d4eSJosef Bacik 		}
828c3e69d58SChris Mason 		delayed_refs->num_heads++;
829c3e69d58SChris Mason 		delayed_refs->num_heads_ready++;
830d7df2c79SJosef Bacik 		atomic_inc(&delayed_refs->num_entries);
83156bec294SChris Mason 		trans->delayed_ref_updates++;
83256bec294SChris Mason 	}
833fb235dc0SQu Wenruo 	if (qrecord_inserted_ret)
834fb235dc0SQu Wenruo 		*qrecord_inserted_ret = qrecord_inserted;
8352335efafSNikolay Borisov 
836d7df2c79SJosef Bacik 	return head_ref;
83756bec294SChris Mason }
83856bec294SChris Mason 
83956bec294SChris Mason /*
840cb49a87bSNikolay Borisov  * init_delayed_ref_common - Initialize the structure which represents a
841cb49a87bSNikolay Borisov  *			     modification to a an extent.
842cb49a87bSNikolay Borisov  *
843cb49a87bSNikolay Borisov  * @fs_info:    Internal to the mounted filesystem mount structure.
844cb49a87bSNikolay Borisov  *
845cb49a87bSNikolay Borisov  * @ref:	The structure which is going to be initialized.
846cb49a87bSNikolay Borisov  *
847cb49a87bSNikolay Borisov  * @bytenr:	The logical address of the extent for which a modification is
848cb49a87bSNikolay Borisov  *		going to be recorded.
849cb49a87bSNikolay Borisov  *
850cb49a87bSNikolay Borisov  * @num_bytes:  Size of the extent whose modification is being recorded.
851cb49a87bSNikolay Borisov  *
852cb49a87bSNikolay Borisov  * @ref_root:	The id of the root where this modification has originated, this
853cb49a87bSNikolay Borisov  *		can be either one of the well-known metadata trees or the
854cb49a87bSNikolay Borisov  *		subvolume id which references this extent.
855cb49a87bSNikolay Borisov  *
856cb49a87bSNikolay Borisov  * @action:	Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
857cb49a87bSNikolay Borisov  *		BTRFS_ADD_DELAYED_EXTENT
858cb49a87bSNikolay Borisov  *
859cb49a87bSNikolay Borisov  * @ref_type:	Holds the type of the extent which is being recorded, can be
860cb49a87bSNikolay Borisov  *		one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
861cb49a87bSNikolay Borisov  *		when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
862cb49a87bSNikolay Borisov  *		BTRFS_EXTENT_DATA_REF_KEY when recording data extent
863cb49a87bSNikolay Borisov  */
init_delayed_ref_common(struct btrfs_fs_info * fs_info,struct btrfs_delayed_ref_node * ref,u64 bytenr,u64 num_bytes,u64 ref_root,int action,u8 ref_type)864cb49a87bSNikolay Borisov static void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
865cb49a87bSNikolay Borisov 				    struct btrfs_delayed_ref_node *ref,
866cb49a87bSNikolay Borisov 				    u64 bytenr, u64 num_bytes, u64 ref_root,
867cb49a87bSNikolay Borisov 				    int action, u8 ref_type)
868cb49a87bSNikolay Borisov {
869cb49a87bSNikolay Borisov 	u64 seq = 0;
870cb49a87bSNikolay Borisov 
871cb49a87bSNikolay Borisov 	if (action == BTRFS_ADD_DELAYED_EXTENT)
872cb49a87bSNikolay Borisov 		action = BTRFS_ADD_DELAYED_REF;
873cb49a87bSNikolay Borisov 
874cb49a87bSNikolay Borisov 	if (is_fstree(ref_root))
875cb49a87bSNikolay Borisov 		seq = atomic64_read(&fs_info->tree_mod_seq);
876cb49a87bSNikolay Borisov 
877cb49a87bSNikolay Borisov 	refcount_set(&ref->refs, 1);
878cb49a87bSNikolay Borisov 	ref->bytenr = bytenr;
879cb49a87bSNikolay Borisov 	ref->num_bytes = num_bytes;
880cb49a87bSNikolay Borisov 	ref->ref_mod = 1;
881cb49a87bSNikolay Borisov 	ref->action = action;
882cb49a87bSNikolay Borisov 	ref->seq = seq;
883cb49a87bSNikolay Borisov 	ref->type = ref_type;
884cb49a87bSNikolay Borisov 	RB_CLEAR_NODE(&ref->ref_node);
885cb49a87bSNikolay Borisov 	INIT_LIST_HEAD(&ref->add_list);
886cb49a87bSNikolay Borisov }
887cb49a87bSNikolay Borisov 
888cb49a87bSNikolay Borisov /*
8895d4f98a2SYan Zheng  * add a delayed tree ref.  This does all of the accounting required
89056bec294SChris Mason  * to make sure the delayed ref is eventually processed before this
89156bec294SChris Mason  * transaction commits.
89256bec294SChris Mason  */
btrfs_add_delayed_tree_ref(struct btrfs_trans_handle * trans,struct btrfs_ref * generic_ref,struct btrfs_delayed_extent_op * extent_op)89344e1c47dSNikolay Borisov int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
894ed4f255bSQu Wenruo 			       struct btrfs_ref *generic_ref,
8952187374fSJosef Bacik 			       struct btrfs_delayed_extent_op *extent_op)
89656bec294SChris Mason {
89744e1c47dSNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
8985d4f98a2SYan Zheng 	struct btrfs_delayed_tree_ref *ref;
89956bec294SChris Mason 	struct btrfs_delayed_ref_head *head_ref;
90056bec294SChris Mason 	struct btrfs_delayed_ref_root *delayed_refs;
9013368d001SQu Wenruo 	struct btrfs_qgroup_extent_record *record = NULL;
902293f8197SFilipe Manana 	bool qrecord_inserted;
903ed4f255bSQu Wenruo 	bool is_system;
904f38462c4SFilipe Manana 	bool merged;
905ed4f255bSQu Wenruo 	int action = generic_ref->action;
906ed4f255bSQu Wenruo 	int level = generic_ref->tree_ref.level;
907ed4f255bSQu Wenruo 	u64 bytenr = generic_ref->bytenr;
908ed4f255bSQu Wenruo 	u64 num_bytes = generic_ref->len;
909ed4f255bSQu Wenruo 	u64 parent = generic_ref->parent;
91070d64000SNikolay Borisov 	u8 ref_type;
91156bec294SChris Mason 
912d55b9e68SNikolay Borisov 	is_system = (generic_ref->tree_ref.owning_root == BTRFS_CHUNK_TREE_OBJECTID);
913ed4f255bSQu Wenruo 
914ed4f255bSQu Wenruo 	ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
91578a6184aSMiao Xie 	ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
91656bec294SChris Mason 	if (!ref)
91756bec294SChris Mason 		return -ENOMEM;
91856bec294SChris Mason 
9197b4284deSNikolay Borisov 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
9207b4284deSNikolay Borisov 	if (!head_ref) {
9217b4284deSNikolay Borisov 		kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
9227b4284deSNikolay Borisov 		return -ENOMEM;
9237b4284deSNikolay Borisov 	}
9247b4284deSNikolay Borisov 
9257b4284deSNikolay Borisov 	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
926ed4f255bSQu Wenruo 	    !generic_ref->skip_qgroup) {
9271418bae1SQu Wenruo 		record = kzalloc(sizeof(*record), GFP_NOFS);
9287b4284deSNikolay Borisov 		if (!record) {
9297b4284deSNikolay Borisov 			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
9307b4284deSNikolay Borisov 			kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
9317b4284deSNikolay Borisov 			return -ENOMEM;
9327b4284deSNikolay Borisov 		}
9337b4284deSNikolay Borisov 	}
9347b4284deSNikolay Borisov 
93570d64000SNikolay Borisov 	if (parent)
93670d64000SNikolay Borisov 		ref_type = BTRFS_SHARED_BLOCK_REF_KEY;
93770d64000SNikolay Borisov 	else
93870d64000SNikolay Borisov 		ref_type = BTRFS_TREE_BLOCK_REF_KEY;
9397b4284deSNikolay Borisov 
94070d64000SNikolay Borisov 	init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
941113479d5SNikolay Borisov 				generic_ref->tree_ref.owning_root, action,
942113479d5SNikolay Borisov 				ref_type);
943113479d5SNikolay Borisov 	ref->root = generic_ref->tree_ref.owning_root;
94470d64000SNikolay Borisov 	ref->parent = parent;
94570d64000SNikolay Borisov 	ref->level = level;
94670d64000SNikolay Borisov 
9472335efafSNikolay Borisov 	init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
948113479d5SNikolay Borisov 			      generic_ref->tree_ref.owning_root, 0, action,
949113479d5SNikolay Borisov 			      false, is_system);
9505d4f98a2SYan Zheng 	head_ref->extent_op = extent_op;
9515d4f98a2SYan Zheng 
95256bec294SChris Mason 	delayed_refs = &trans->transaction->delayed_refs;
95356bec294SChris Mason 	spin_lock(&delayed_refs->lock);
95456bec294SChris Mason 
95556bec294SChris Mason 	/*
95656bec294SChris Mason 	 * insert both the head node and the new ref without dropping
95756bec294SChris Mason 	 * the spin lock
95856bec294SChris Mason 	 */
9592335efafSNikolay Borisov 	head_ref = add_delayed_ref_head(trans, head_ref, record,
9602187374fSJosef Bacik 					action, &qrecord_inserted);
96156bec294SChris Mason 
962f38462c4SFilipe Manana 	merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);
96356bec294SChris Mason 	spin_unlock(&delayed_refs->lock);
96495a06077SJan Schmidt 
965ba2c4d4eSJosef Bacik 	/*
966ba2c4d4eSJosef Bacik 	 * Need to update the delayed_refs_rsv with any changes we may have
967ba2c4d4eSJosef Bacik 	 * made.
968ba2c4d4eSJosef Bacik 	 */
969ba2c4d4eSJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
970ba2c4d4eSJosef Bacik 
97170d64000SNikolay Borisov 	trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
97270d64000SNikolay Borisov 				   action == BTRFS_ADD_DELAYED_EXTENT ?
97370d64000SNikolay Borisov 				   BTRFS_ADD_DELAYED_REF : action);
974f38462c4SFilipe Manana 	if (merged)
97570d64000SNikolay Borisov 		kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
97670d64000SNikolay Borisov 
977fb235dc0SQu Wenruo 	if (qrecord_inserted)
9788949b9a1SFilipe Manana 		btrfs_qgroup_trace_extent_post(trans, record);
979952bd3dbSNikolay Borisov 
98056bec294SChris Mason 	return 0;
98156bec294SChris Mason }
98256bec294SChris Mason 
98356bec294SChris Mason /*
9845d4f98a2SYan Zheng  * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
9855d4f98a2SYan Zheng  */
btrfs_add_delayed_data_ref(struct btrfs_trans_handle * trans,struct btrfs_ref * generic_ref,u64 reserved)98688a979c6SNikolay Borisov int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
98776675593SQu Wenruo 			       struct btrfs_ref *generic_ref,
9882187374fSJosef Bacik 			       u64 reserved)
9895d4f98a2SYan Zheng {
99088a979c6SNikolay Borisov 	struct btrfs_fs_info *fs_info = trans->fs_info;
9915d4f98a2SYan Zheng 	struct btrfs_delayed_data_ref *ref;
9925d4f98a2SYan Zheng 	struct btrfs_delayed_ref_head *head_ref;
9935d4f98a2SYan Zheng 	struct btrfs_delayed_ref_root *delayed_refs;
9943368d001SQu Wenruo 	struct btrfs_qgroup_extent_record *record = NULL;
995293f8197SFilipe Manana 	bool qrecord_inserted;
99676675593SQu Wenruo 	int action = generic_ref->action;
997f38462c4SFilipe Manana 	bool merged;
99876675593SQu Wenruo 	u64 bytenr = generic_ref->bytenr;
99976675593SQu Wenruo 	u64 num_bytes = generic_ref->len;
100076675593SQu Wenruo 	u64 parent = generic_ref->parent;
1001113479d5SNikolay Borisov 	u64 ref_root = generic_ref->data_ref.owning_root;
100276675593SQu Wenruo 	u64 owner = generic_ref->data_ref.ino;
100376675593SQu Wenruo 	u64 offset = generic_ref->data_ref.offset;
1004cd7f9699SNikolay Borisov 	u8 ref_type;
10055d4f98a2SYan Zheng 
100676675593SQu Wenruo 	ASSERT(generic_ref->type == BTRFS_REF_DATA && action);
100778a6184aSMiao Xie 	ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
10085d4f98a2SYan Zheng 	if (!ref)
10095d4f98a2SYan Zheng 		return -ENOMEM;
10105d4f98a2SYan Zheng 
1011cd7f9699SNikolay Borisov 	if (parent)
1012cd7f9699SNikolay Borisov 	        ref_type = BTRFS_SHARED_DATA_REF_KEY;
1013cd7f9699SNikolay Borisov 	else
1014cd7f9699SNikolay Borisov 	        ref_type = BTRFS_EXTENT_DATA_REF_KEY;
1015cd7f9699SNikolay Borisov 	init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
1016cd7f9699SNikolay Borisov 				ref_root, action, ref_type);
1017cd7f9699SNikolay Borisov 	ref->root = ref_root;
1018cd7f9699SNikolay Borisov 	ref->parent = parent;
1019cd7f9699SNikolay Borisov 	ref->objectid = owner;
1020cd7f9699SNikolay Borisov 	ref->offset = offset;
1021cd7f9699SNikolay Borisov 
1022cd7f9699SNikolay Borisov 
102378a6184aSMiao Xie 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
10245d4f98a2SYan Zheng 	if (!head_ref) {
102578a6184aSMiao Xie 		kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
10265d4f98a2SYan Zheng 		return -ENOMEM;
10275d4f98a2SYan Zheng 	}
10285d4f98a2SYan Zheng 
1029afcdd129SJosef Bacik 	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
103076675593SQu Wenruo 	    !generic_ref->skip_qgroup) {
10311418bae1SQu Wenruo 		record = kzalloc(sizeof(*record), GFP_NOFS);
10323368d001SQu Wenruo 		if (!record) {
10333368d001SQu Wenruo 			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
10343368d001SQu Wenruo 			kmem_cache_free(btrfs_delayed_ref_head_cachep,
10353368d001SQu Wenruo 					head_ref);
10363368d001SQu Wenruo 			return -ENOMEM;
10373368d001SQu Wenruo 		}
10383368d001SQu Wenruo 	}
10393368d001SQu Wenruo 
10402335efafSNikolay Borisov 	init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
10412335efafSNikolay Borisov 			      reserved, action, true, false);
1042fef394f7SJeff Mahoney 	head_ref->extent_op = NULL;
10435d4f98a2SYan Zheng 
10445d4f98a2SYan Zheng 	delayed_refs = &trans->transaction->delayed_refs;
10455d4f98a2SYan Zheng 	spin_lock(&delayed_refs->lock);
10465d4f98a2SYan Zheng 
10475d4f98a2SYan Zheng 	/*
10485d4f98a2SYan Zheng 	 * insert both the head node and the new ref without dropping
10495d4f98a2SYan Zheng 	 * the spin lock
10505d4f98a2SYan Zheng 	 */
10512335efafSNikolay Borisov 	head_ref = add_delayed_ref_head(trans, head_ref, record,
10522187374fSJosef Bacik 					action, &qrecord_inserted);
10535d4f98a2SYan Zheng 
1054f38462c4SFilipe Manana 	merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);
10555d4f98a2SYan Zheng 	spin_unlock(&delayed_refs->lock);
105695a06077SJan Schmidt 
1057ba2c4d4eSJosef Bacik 	/*
1058ba2c4d4eSJosef Bacik 	 * Need to update the delayed_refs_rsv with any changes we may have
1059ba2c4d4eSJosef Bacik 	 * made.
1060ba2c4d4eSJosef Bacik 	 */
1061ba2c4d4eSJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
1062ba2c4d4eSJosef Bacik 
1063cd7f9699SNikolay Borisov 	trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
1064cd7f9699SNikolay Borisov 				   action == BTRFS_ADD_DELAYED_EXTENT ?
1065cd7f9699SNikolay Borisov 				   BTRFS_ADD_DELAYED_REF : action);
1066f38462c4SFilipe Manana 	if (merged)
1067cd7f9699SNikolay Borisov 		kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
1068cd7f9699SNikolay Borisov 
1069cd7f9699SNikolay Borisov 
1070fb235dc0SQu Wenruo 	if (qrecord_inserted)
10718949b9a1SFilipe Manana 		return btrfs_qgroup_trace_extent_post(trans, record);
10725d4f98a2SYan Zheng 	return 0;
10735d4f98a2SYan Zheng }
10745d4f98a2SYan Zheng 
btrfs_add_delayed_extent_op(struct btrfs_trans_handle * trans,u64 bytenr,u64 num_bytes,struct btrfs_delayed_extent_op * extent_op)1075c6e340bcSDavid Sterba int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
10765d4f98a2SYan Zheng 				u64 bytenr, u64 num_bytes,
10775d4f98a2SYan Zheng 				struct btrfs_delayed_extent_op *extent_op)
10785d4f98a2SYan Zheng {
10795d4f98a2SYan Zheng 	struct btrfs_delayed_ref_head *head_ref;
10805d4f98a2SYan Zheng 	struct btrfs_delayed_ref_root *delayed_refs;
10815d4f98a2SYan Zheng 
108278a6184aSMiao Xie 	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
10835d4f98a2SYan Zheng 	if (!head_ref)
10845d4f98a2SYan Zheng 		return -ENOMEM;
10855d4f98a2SYan Zheng 
10862335efafSNikolay Borisov 	init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
10870e3696f8SDavid Sterba 			      BTRFS_UPDATE_DELAYED_HEAD, false, false);
10885d4f98a2SYan Zheng 	head_ref->extent_op = extent_op;
10895d4f98a2SYan Zheng 
10905d4f98a2SYan Zheng 	delayed_refs = &trans->transaction->delayed_refs;
10915d4f98a2SYan Zheng 	spin_lock(&delayed_refs->lock);
10925d4f98a2SYan Zheng 
10932335efafSNikolay Borisov 	add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD,
10942187374fSJosef Bacik 			     NULL);
10955d4f98a2SYan Zheng 
10965d4f98a2SYan Zheng 	spin_unlock(&delayed_refs->lock);
1097ba2c4d4eSJosef Bacik 
1098ba2c4d4eSJosef Bacik 	/*
1099ba2c4d4eSJosef Bacik 	 * Need to update the delayed_refs_rsv with any changes we may have
1100ba2c4d4eSJosef Bacik 	 * made.
1101ba2c4d4eSJosef Bacik 	 */
1102ba2c4d4eSJosef Bacik 	btrfs_update_delayed_refs_rsv(trans);
11035d4f98a2SYan Zheng 	return 0;
11045d4f98a2SYan Zheng }
11055d4f98a2SYan Zheng 
11065d4f98a2SYan Zheng /*
110738e9372eSDavid Sterba  * This does a simple search for the head node for a given extent.  Returns the
110838e9372eSDavid Sterba  * head node if found, or NULL if not.
11091887be66SChris Mason  */
11101887be66SChris Mason struct btrfs_delayed_ref_head *
btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root * delayed_refs,u64 bytenr)1111f72ad18eSLiu Bo btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
11121887be66SChris Mason {
111338e9372eSDavid Sterba 	lockdep_assert_held(&delayed_refs->lock);
111438e9372eSDavid Sterba 
1115d9352794SLu Fengqi 	return find_ref_head(delayed_refs, bytenr, false);
11161887be66SChris Mason }
111778a6184aSMiao Xie 
btrfs_delayed_ref_exit(void)1118e67c718bSDavid Sterba void __cold btrfs_delayed_ref_exit(void)
111978a6184aSMiao Xie {
112078a6184aSMiao Xie 	kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
112178a6184aSMiao Xie 	kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
112278a6184aSMiao Xie 	kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
112378a6184aSMiao Xie 	kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
112478a6184aSMiao Xie }
112578a6184aSMiao Xie 
btrfs_delayed_ref_init(void)1126f5c29bd9SLiu Bo int __init btrfs_delayed_ref_init(void)
112778a6184aSMiao Xie {
112878a6184aSMiao Xie 	btrfs_delayed_ref_head_cachep = kmem_cache_create(
112978a6184aSMiao Xie 				"btrfs_delayed_ref_head",
113078a6184aSMiao Xie 				sizeof(struct btrfs_delayed_ref_head), 0,
1131fba4b697SNikolay Borisov 				SLAB_MEM_SPREAD, NULL);
113278a6184aSMiao Xie 	if (!btrfs_delayed_ref_head_cachep)
113378a6184aSMiao Xie 		goto fail;
113478a6184aSMiao Xie 
113578a6184aSMiao Xie 	btrfs_delayed_tree_ref_cachep = kmem_cache_create(
113678a6184aSMiao Xie 				"btrfs_delayed_tree_ref",
113778a6184aSMiao Xie 				sizeof(struct btrfs_delayed_tree_ref), 0,
1138fba4b697SNikolay Borisov 				SLAB_MEM_SPREAD, NULL);
113978a6184aSMiao Xie 	if (!btrfs_delayed_tree_ref_cachep)
114078a6184aSMiao Xie 		goto fail;
114178a6184aSMiao Xie 
114278a6184aSMiao Xie 	btrfs_delayed_data_ref_cachep = kmem_cache_create(
114378a6184aSMiao Xie 				"btrfs_delayed_data_ref",
114478a6184aSMiao Xie 				sizeof(struct btrfs_delayed_data_ref), 0,
1145fba4b697SNikolay Borisov 				SLAB_MEM_SPREAD, NULL);
114678a6184aSMiao Xie 	if (!btrfs_delayed_data_ref_cachep)
114778a6184aSMiao Xie 		goto fail;
114878a6184aSMiao Xie 
114978a6184aSMiao Xie 	btrfs_delayed_extent_op_cachep = kmem_cache_create(
115078a6184aSMiao Xie 				"btrfs_delayed_extent_op",
115178a6184aSMiao Xie 				sizeof(struct btrfs_delayed_extent_op), 0,
1152fba4b697SNikolay Borisov 				SLAB_MEM_SPREAD, NULL);
115378a6184aSMiao Xie 	if (!btrfs_delayed_extent_op_cachep)
115478a6184aSMiao Xie 		goto fail;
115578a6184aSMiao Xie 
115678a6184aSMiao Xie 	return 0;
115778a6184aSMiao Xie fail:
115878a6184aSMiao Xie 	btrfs_delayed_ref_exit();
115978a6184aSMiao Xie 	return -ENOMEM;
116078a6184aSMiao Xie }
1161