xref: /kernel/linux/linux-6.6/fs/btrfs/delayed-ref.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2009 Oracle.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/sched.h>
762306a36Sopenharmony_ci#include <linux/slab.h>
862306a36Sopenharmony_ci#include <linux/sort.h>
962306a36Sopenharmony_ci#include "messages.h"
1062306a36Sopenharmony_ci#include "ctree.h"
1162306a36Sopenharmony_ci#include "delayed-ref.h"
1262306a36Sopenharmony_ci#include "transaction.h"
1362306a36Sopenharmony_ci#include "qgroup.h"
1462306a36Sopenharmony_ci#include "space-info.h"
1562306a36Sopenharmony_ci#include "tree-mod-log.h"
1662306a36Sopenharmony_ci#include "fs.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct kmem_cache *btrfs_delayed_ref_head_cachep;
1962306a36Sopenharmony_cistruct kmem_cache *btrfs_delayed_tree_ref_cachep;
2062306a36Sopenharmony_cistruct kmem_cache *btrfs_delayed_data_ref_cachep;
2162306a36Sopenharmony_cistruct kmem_cache *btrfs_delayed_extent_op_cachep;
2262306a36Sopenharmony_ci/*
2362306a36Sopenharmony_ci * delayed back reference update tracking.  For subvolume trees
2462306a36Sopenharmony_ci * we queue up extent allocations and backref maintenance for
2562306a36Sopenharmony_ci * delayed processing.   This avoids deep call chains where we
2662306a36Sopenharmony_ci * add extents in the middle of btrfs_search_slot, and it allows
2762306a36Sopenharmony_ci * us to buffer up frequently modified backrefs in an rb tree instead
2862306a36Sopenharmony_ci * of hammering updates on the extent allocation tree.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cibool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
3462306a36Sopenharmony_ci	struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
3562306a36Sopenharmony_ci	bool ret = false;
3662306a36Sopenharmony_ci	u64 reserved;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	spin_lock(&global_rsv->lock);
3962306a36Sopenharmony_ci	reserved = global_rsv->reserved;
4062306a36Sopenharmony_ci	spin_unlock(&global_rsv->lock);
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	/*
4362306a36Sopenharmony_ci	 * Since the global reserve is just kind of magic we don't really want
4462306a36Sopenharmony_ci	 * to rely on it to save our bacon, so if our size is more than the
4562306a36Sopenharmony_ci	 * delayed_refs_rsv and the global rsv then it's time to think about
4662306a36Sopenharmony_ci	 * bailing.
4762306a36Sopenharmony_ci	 */
4862306a36Sopenharmony_ci	spin_lock(&delayed_refs_rsv->lock);
4962306a36Sopenharmony_ci	reserved += delayed_refs_rsv->reserved;
5062306a36Sopenharmony_ci	if (delayed_refs_rsv->size >= reserved)
5162306a36Sopenharmony_ci		ret = true;
5262306a36Sopenharmony_ci	spin_unlock(&delayed_refs_rsv->lock);
5362306a36Sopenharmony_ci	return ret;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * Release a ref head's reservation.
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * @fs_info:  the filesystem
6062306a36Sopenharmony_ci * @nr:       number of items to drop
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Drops the delayed ref head's count from the delayed refs rsv and free any
6362306a36Sopenharmony_ci * excess reservation we had.
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_civoid btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
6862306a36Sopenharmony_ci	const u64 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr);
6962306a36Sopenharmony_ci	u64 released = 0;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL);
7262306a36Sopenharmony_ci	if (released)
7362306a36Sopenharmony_ci		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
7462306a36Sopenharmony_ci					      0, released, 0);
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/*
7862306a36Sopenharmony_ci * Adjust the size of the delayed refs rsv.
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * This is to be called anytime we may have adjusted trans->delayed_ref_updates,
8162306a36Sopenharmony_ci * it'll calculate the additional size and add it to the delayed_refs_rsv.
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_civoid btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
8662306a36Sopenharmony_ci	struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv;
8762306a36Sopenharmony_ci	u64 num_bytes;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	if (!trans->delayed_ref_updates)
9062306a36Sopenharmony_ci		return;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	num_bytes = btrfs_calc_delayed_ref_bytes(fs_info,
9362306a36Sopenharmony_ci						 trans->delayed_ref_updates);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	spin_lock(&delayed_rsv->lock);
9662306a36Sopenharmony_ci	delayed_rsv->size += num_bytes;
9762306a36Sopenharmony_ci	delayed_rsv->full = false;
9862306a36Sopenharmony_ci	spin_unlock(&delayed_rsv->lock);
9962306a36Sopenharmony_ci	trans->delayed_ref_updates = 0;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci/*
10362306a36Sopenharmony_ci * Transfer bytes to our delayed refs rsv.
10462306a36Sopenharmony_ci *
10562306a36Sopenharmony_ci * @fs_info:   the filesystem
10662306a36Sopenharmony_ci * @num_bytes: number of bytes to transfer
10762306a36Sopenharmony_ci *
10862306a36Sopenharmony_ci * This transfers up to the num_bytes amount, previously reserved, to the
10962306a36Sopenharmony_ci * delayed_refs_rsv.  Any extra bytes are returned to the space info.
11062306a36Sopenharmony_ci */
11162306a36Sopenharmony_civoid btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info,
11262306a36Sopenharmony_ci				       u64 num_bytes)
11362306a36Sopenharmony_ci{
11462306a36Sopenharmony_ci	struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv;
11562306a36Sopenharmony_ci	u64 to_free = 0;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	spin_lock(&delayed_refs_rsv->lock);
11862306a36Sopenharmony_ci	if (delayed_refs_rsv->size > delayed_refs_rsv->reserved) {
11962306a36Sopenharmony_ci		u64 delta = delayed_refs_rsv->size -
12062306a36Sopenharmony_ci			delayed_refs_rsv->reserved;
12162306a36Sopenharmony_ci		if (num_bytes > delta) {
12262306a36Sopenharmony_ci			to_free = num_bytes - delta;
12362306a36Sopenharmony_ci			num_bytes = delta;
12462306a36Sopenharmony_ci		}
12562306a36Sopenharmony_ci	} else {
12662306a36Sopenharmony_ci		to_free = num_bytes;
12762306a36Sopenharmony_ci		num_bytes = 0;
12862306a36Sopenharmony_ci	}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	if (num_bytes)
13162306a36Sopenharmony_ci		delayed_refs_rsv->reserved += num_bytes;
13262306a36Sopenharmony_ci	if (delayed_refs_rsv->reserved >= delayed_refs_rsv->size)
13362306a36Sopenharmony_ci		delayed_refs_rsv->full = true;
13462306a36Sopenharmony_ci	spin_unlock(&delayed_refs_rsv->lock);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	if (num_bytes)
13762306a36Sopenharmony_ci		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv",
13862306a36Sopenharmony_ci					      0, num_bytes, 1);
13962306a36Sopenharmony_ci	if (to_free)
14062306a36Sopenharmony_ci		btrfs_space_info_free_bytes_may_use(fs_info,
14162306a36Sopenharmony_ci				delayed_refs_rsv->space_info, to_free);
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * Refill based on our delayed refs usage.
14662306a36Sopenharmony_ci *
14762306a36Sopenharmony_ci * @fs_info: the filesystem
14862306a36Sopenharmony_ci * @flush:   control how we can flush for this reservation.
14962306a36Sopenharmony_ci *
15062306a36Sopenharmony_ci * This will refill the delayed block_rsv up to 1 items size worth of space and
15162306a36Sopenharmony_ci * will return -ENOSPC if we can't make the reservation.
15262306a36Sopenharmony_ci */
15362306a36Sopenharmony_ciint btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info,
15462306a36Sopenharmony_ci				  enum btrfs_reserve_flush_enum flush)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv;
15762306a36Sopenharmony_ci	u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1);
15862306a36Sopenharmony_ci	u64 num_bytes = 0;
15962306a36Sopenharmony_ci	u64 refilled_bytes;
16062306a36Sopenharmony_ci	u64 to_free;
16162306a36Sopenharmony_ci	int ret = -ENOSPC;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	spin_lock(&block_rsv->lock);
16462306a36Sopenharmony_ci	if (block_rsv->reserved < block_rsv->size) {
16562306a36Sopenharmony_ci		num_bytes = block_rsv->size - block_rsv->reserved;
16662306a36Sopenharmony_ci		num_bytes = min(num_bytes, limit);
16762306a36Sopenharmony_ci	}
16862306a36Sopenharmony_ci	spin_unlock(&block_rsv->lock);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	if (!num_bytes)
17162306a36Sopenharmony_ci		return 0;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush);
17462306a36Sopenharmony_ci	if (ret)
17562306a36Sopenharmony_ci		return ret;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	/*
17862306a36Sopenharmony_ci	 * We may have raced with someone else, so check again if we the block
17962306a36Sopenharmony_ci	 * reserve is still not full and release any excess space.
18062306a36Sopenharmony_ci	 */
18162306a36Sopenharmony_ci	spin_lock(&block_rsv->lock);
18262306a36Sopenharmony_ci	if (block_rsv->reserved < block_rsv->size) {
18362306a36Sopenharmony_ci		u64 needed = block_rsv->size - block_rsv->reserved;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci		if (num_bytes >= needed) {
18662306a36Sopenharmony_ci			block_rsv->reserved += needed;
18762306a36Sopenharmony_ci			block_rsv->full = true;
18862306a36Sopenharmony_ci			to_free = num_bytes - needed;
18962306a36Sopenharmony_ci			refilled_bytes = needed;
19062306a36Sopenharmony_ci		} else {
19162306a36Sopenharmony_ci			block_rsv->reserved += num_bytes;
19262306a36Sopenharmony_ci			to_free = 0;
19362306a36Sopenharmony_ci			refilled_bytes = num_bytes;
19462306a36Sopenharmony_ci		}
19562306a36Sopenharmony_ci	} else {
19662306a36Sopenharmony_ci		to_free = num_bytes;
19762306a36Sopenharmony_ci		refilled_bytes = 0;
19862306a36Sopenharmony_ci	}
19962306a36Sopenharmony_ci	spin_unlock(&block_rsv->lock);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	if (to_free > 0)
20262306a36Sopenharmony_ci		btrfs_space_info_free_bytes_may_use(fs_info, block_rsv->space_info,
20362306a36Sopenharmony_ci						    to_free);
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	if (refilled_bytes > 0)
20662306a36Sopenharmony_ci		trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", 0,
20762306a36Sopenharmony_ci					      refilled_bytes, 1);
20862306a36Sopenharmony_ci	return 0;
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci/*
21262306a36Sopenharmony_ci * compare two delayed tree backrefs with same bytenr and type
21362306a36Sopenharmony_ci */
21462306a36Sopenharmony_cistatic int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1,
21562306a36Sopenharmony_ci			  struct btrfs_delayed_tree_ref *ref2)
21662306a36Sopenharmony_ci{
21762306a36Sopenharmony_ci	if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
21862306a36Sopenharmony_ci		if (ref1->root < ref2->root)
21962306a36Sopenharmony_ci			return -1;
22062306a36Sopenharmony_ci		if (ref1->root > ref2->root)
22162306a36Sopenharmony_ci			return 1;
22262306a36Sopenharmony_ci	} else {
22362306a36Sopenharmony_ci		if (ref1->parent < ref2->parent)
22462306a36Sopenharmony_ci			return -1;
22562306a36Sopenharmony_ci		if (ref1->parent > ref2->parent)
22662306a36Sopenharmony_ci			return 1;
22762306a36Sopenharmony_ci	}
22862306a36Sopenharmony_ci	return 0;
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci/*
23262306a36Sopenharmony_ci * compare two delayed data backrefs with same bytenr and type
23362306a36Sopenharmony_ci */
23462306a36Sopenharmony_cistatic int comp_data_refs(struct btrfs_delayed_data_ref *ref1,
23562306a36Sopenharmony_ci			  struct btrfs_delayed_data_ref *ref2)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
23862306a36Sopenharmony_ci		if (ref1->root < ref2->root)
23962306a36Sopenharmony_ci			return -1;
24062306a36Sopenharmony_ci		if (ref1->root > ref2->root)
24162306a36Sopenharmony_ci			return 1;
24262306a36Sopenharmony_ci		if (ref1->objectid < ref2->objectid)
24362306a36Sopenharmony_ci			return -1;
24462306a36Sopenharmony_ci		if (ref1->objectid > ref2->objectid)
24562306a36Sopenharmony_ci			return 1;
24662306a36Sopenharmony_ci		if (ref1->offset < ref2->offset)
24762306a36Sopenharmony_ci			return -1;
24862306a36Sopenharmony_ci		if (ref1->offset > ref2->offset)
24962306a36Sopenharmony_ci			return 1;
25062306a36Sopenharmony_ci	} else {
25162306a36Sopenharmony_ci		if (ref1->parent < ref2->parent)
25262306a36Sopenharmony_ci			return -1;
25362306a36Sopenharmony_ci		if (ref1->parent > ref2->parent)
25462306a36Sopenharmony_ci			return 1;
25562306a36Sopenharmony_ci	}
25662306a36Sopenharmony_ci	return 0;
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic int comp_refs(struct btrfs_delayed_ref_node *ref1,
26062306a36Sopenharmony_ci		     struct btrfs_delayed_ref_node *ref2,
26162306a36Sopenharmony_ci		     bool check_seq)
26262306a36Sopenharmony_ci{
26362306a36Sopenharmony_ci	int ret = 0;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	if (ref1->type < ref2->type)
26662306a36Sopenharmony_ci		return -1;
26762306a36Sopenharmony_ci	if (ref1->type > ref2->type)
26862306a36Sopenharmony_ci		return 1;
26962306a36Sopenharmony_ci	if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
27062306a36Sopenharmony_ci	    ref1->type == BTRFS_SHARED_BLOCK_REF_KEY)
27162306a36Sopenharmony_ci		ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1),
27262306a36Sopenharmony_ci				     btrfs_delayed_node_to_tree_ref(ref2));
27362306a36Sopenharmony_ci	else
27462306a36Sopenharmony_ci		ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1),
27562306a36Sopenharmony_ci				     btrfs_delayed_node_to_data_ref(ref2));
27662306a36Sopenharmony_ci	if (ret)
27762306a36Sopenharmony_ci		return ret;
27862306a36Sopenharmony_ci	if (check_seq) {
27962306a36Sopenharmony_ci		if (ref1->seq < ref2->seq)
28062306a36Sopenharmony_ci			return -1;
28162306a36Sopenharmony_ci		if (ref1->seq > ref2->seq)
28262306a36Sopenharmony_ci			return 1;
28362306a36Sopenharmony_ci	}
28462306a36Sopenharmony_ci	return 0;
28562306a36Sopenharmony_ci}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci/* insert a new ref to head ref rbtree */
28862306a36Sopenharmony_cistatic struct btrfs_delayed_ref_head *htree_insert(struct rb_root_cached *root,
28962306a36Sopenharmony_ci						   struct rb_node *node)
29062306a36Sopenharmony_ci{
29162306a36Sopenharmony_ci	struct rb_node **p = &root->rb_root.rb_node;
29262306a36Sopenharmony_ci	struct rb_node *parent_node = NULL;
29362306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *entry;
29462306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *ins;
29562306a36Sopenharmony_ci	u64 bytenr;
29662306a36Sopenharmony_ci	bool leftmost = true;
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
29962306a36Sopenharmony_ci	bytenr = ins->bytenr;
30062306a36Sopenharmony_ci	while (*p) {
30162306a36Sopenharmony_ci		parent_node = *p;
30262306a36Sopenharmony_ci		entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
30362306a36Sopenharmony_ci				 href_node);
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci		if (bytenr < entry->bytenr) {
30662306a36Sopenharmony_ci			p = &(*p)->rb_left;
30762306a36Sopenharmony_ci		} else if (bytenr > entry->bytenr) {
30862306a36Sopenharmony_ci			p = &(*p)->rb_right;
30962306a36Sopenharmony_ci			leftmost = false;
31062306a36Sopenharmony_ci		} else {
31162306a36Sopenharmony_ci			return entry;
31262306a36Sopenharmony_ci		}
31362306a36Sopenharmony_ci	}
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	rb_link_node(node, parent_node, p);
31662306a36Sopenharmony_ci	rb_insert_color_cached(node, root, leftmost);
31762306a36Sopenharmony_ci	return NULL;
31862306a36Sopenharmony_ci}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_cistatic struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root,
32162306a36Sopenharmony_ci		struct btrfs_delayed_ref_node *ins)
32262306a36Sopenharmony_ci{
32362306a36Sopenharmony_ci	struct rb_node **p = &root->rb_root.rb_node;
32462306a36Sopenharmony_ci	struct rb_node *node = &ins->ref_node;
32562306a36Sopenharmony_ci	struct rb_node *parent_node = NULL;
32662306a36Sopenharmony_ci	struct btrfs_delayed_ref_node *entry;
32762306a36Sopenharmony_ci	bool leftmost = true;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	while (*p) {
33062306a36Sopenharmony_ci		int comp;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci		parent_node = *p;
33362306a36Sopenharmony_ci		entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
33462306a36Sopenharmony_ci				 ref_node);
33562306a36Sopenharmony_ci		comp = comp_refs(ins, entry, true);
33662306a36Sopenharmony_ci		if (comp < 0) {
33762306a36Sopenharmony_ci			p = &(*p)->rb_left;
33862306a36Sopenharmony_ci		} else if (comp > 0) {
33962306a36Sopenharmony_ci			p = &(*p)->rb_right;
34062306a36Sopenharmony_ci			leftmost = false;
34162306a36Sopenharmony_ci		} else {
34262306a36Sopenharmony_ci			return entry;
34362306a36Sopenharmony_ci		}
34462306a36Sopenharmony_ci	}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	rb_link_node(node, parent_node, p);
34762306a36Sopenharmony_ci	rb_insert_color_cached(node, root, leftmost);
34862306a36Sopenharmony_ci	return NULL;
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_cistatic struct btrfs_delayed_ref_head *find_first_ref_head(
35262306a36Sopenharmony_ci		struct btrfs_delayed_ref_root *dr)
35362306a36Sopenharmony_ci{
35462306a36Sopenharmony_ci	struct rb_node *n;
35562306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *entry;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	n = rb_first_cached(&dr->href_root);
35862306a36Sopenharmony_ci	if (!n)
35962306a36Sopenharmony_ci		return NULL;
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	return entry;
36462306a36Sopenharmony_ci}
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci/*
36762306a36Sopenharmony_ci * Find a head entry based on bytenr. This returns the delayed ref head if it
36862306a36Sopenharmony_ci * was able to find one, or NULL if nothing was in that spot.  If return_bigger
36962306a36Sopenharmony_ci * is given, the next bigger entry is returned if no exact match is found.
37062306a36Sopenharmony_ci */
37162306a36Sopenharmony_cistatic struct btrfs_delayed_ref_head *find_ref_head(
37262306a36Sopenharmony_ci		struct btrfs_delayed_ref_root *dr, u64 bytenr,
37362306a36Sopenharmony_ci		bool return_bigger)
37462306a36Sopenharmony_ci{
37562306a36Sopenharmony_ci	struct rb_root *root = &dr->href_root.rb_root;
37662306a36Sopenharmony_ci	struct rb_node *n;
37762306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *entry;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	n = root->rb_node;
38062306a36Sopenharmony_ci	entry = NULL;
38162306a36Sopenharmony_ci	while (n) {
38262306a36Sopenharmony_ci		entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci		if (bytenr < entry->bytenr)
38562306a36Sopenharmony_ci			n = n->rb_left;
38662306a36Sopenharmony_ci		else if (bytenr > entry->bytenr)
38762306a36Sopenharmony_ci			n = n->rb_right;
38862306a36Sopenharmony_ci		else
38962306a36Sopenharmony_ci			return entry;
39062306a36Sopenharmony_ci	}
39162306a36Sopenharmony_ci	if (entry && return_bigger) {
39262306a36Sopenharmony_ci		if (bytenr > entry->bytenr) {
39362306a36Sopenharmony_ci			n = rb_next(&entry->href_node);
39462306a36Sopenharmony_ci			if (!n)
39562306a36Sopenharmony_ci				return NULL;
39662306a36Sopenharmony_ci			entry = rb_entry(n, struct btrfs_delayed_ref_head,
39762306a36Sopenharmony_ci					 href_node);
39862306a36Sopenharmony_ci		}
39962306a36Sopenharmony_ci		return entry;
40062306a36Sopenharmony_ci	}
40162306a36Sopenharmony_ci	return NULL;
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ciint btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
40562306a36Sopenharmony_ci			   struct btrfs_delayed_ref_head *head)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	lockdep_assert_held(&delayed_refs->lock);
40862306a36Sopenharmony_ci	if (mutex_trylock(&head->mutex))
40962306a36Sopenharmony_ci		return 0;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	refcount_inc(&head->refs);
41262306a36Sopenharmony_ci	spin_unlock(&delayed_refs->lock);
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	mutex_lock(&head->mutex);
41562306a36Sopenharmony_ci	spin_lock(&delayed_refs->lock);
41662306a36Sopenharmony_ci	if (RB_EMPTY_NODE(&head->href_node)) {
41762306a36Sopenharmony_ci		mutex_unlock(&head->mutex);
41862306a36Sopenharmony_ci		btrfs_put_delayed_ref_head(head);
41962306a36Sopenharmony_ci		return -EAGAIN;
42062306a36Sopenharmony_ci	}
42162306a36Sopenharmony_ci	btrfs_put_delayed_ref_head(head);
42262306a36Sopenharmony_ci	return 0;
42362306a36Sopenharmony_ci}
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_cistatic inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs,
42662306a36Sopenharmony_ci				    struct btrfs_delayed_ref_head *head,
42762306a36Sopenharmony_ci				    struct btrfs_delayed_ref_node *ref)
42862306a36Sopenharmony_ci{
42962306a36Sopenharmony_ci	lockdep_assert_held(&head->lock);
43062306a36Sopenharmony_ci	rb_erase_cached(&ref->ref_node, &head->ref_tree);
43162306a36Sopenharmony_ci	RB_CLEAR_NODE(&ref->ref_node);
43262306a36Sopenharmony_ci	if (!list_empty(&ref->add_list))
43362306a36Sopenharmony_ci		list_del(&ref->add_list);
43462306a36Sopenharmony_ci	btrfs_put_delayed_ref(ref);
43562306a36Sopenharmony_ci	atomic_dec(&delayed_refs->num_entries);
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_cistatic bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs,
43962306a36Sopenharmony_ci		      struct btrfs_delayed_ref_head *head,
44062306a36Sopenharmony_ci		      struct btrfs_delayed_ref_node *ref,
44162306a36Sopenharmony_ci		      u64 seq)
44262306a36Sopenharmony_ci{
44362306a36Sopenharmony_ci	struct btrfs_delayed_ref_node *next;
44462306a36Sopenharmony_ci	struct rb_node *node = rb_next(&ref->ref_node);
44562306a36Sopenharmony_ci	bool done = false;
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	while (!done && node) {
44862306a36Sopenharmony_ci		int mod;
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci		next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
45162306a36Sopenharmony_ci		node = rb_next(node);
45262306a36Sopenharmony_ci		if (seq && next->seq >= seq)
45362306a36Sopenharmony_ci			break;
45462306a36Sopenharmony_ci		if (comp_refs(ref, next, false))
45562306a36Sopenharmony_ci			break;
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci		if (ref->action == next->action) {
45862306a36Sopenharmony_ci			mod = next->ref_mod;
45962306a36Sopenharmony_ci		} else {
46062306a36Sopenharmony_ci			if (ref->ref_mod < next->ref_mod) {
46162306a36Sopenharmony_ci				swap(ref, next);
46262306a36Sopenharmony_ci				done = true;
46362306a36Sopenharmony_ci			}
46462306a36Sopenharmony_ci			mod = -next->ref_mod;
46562306a36Sopenharmony_ci		}
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci		drop_delayed_ref(delayed_refs, head, next);
46862306a36Sopenharmony_ci		ref->ref_mod += mod;
46962306a36Sopenharmony_ci		if (ref->ref_mod == 0) {
47062306a36Sopenharmony_ci			drop_delayed_ref(delayed_refs, head, ref);
47162306a36Sopenharmony_ci			done = true;
47262306a36Sopenharmony_ci		} else {
47362306a36Sopenharmony_ci			/*
47462306a36Sopenharmony_ci			 * Can't have multiples of the same ref on a tree block.
47562306a36Sopenharmony_ci			 */
47662306a36Sopenharmony_ci			WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
47762306a36Sopenharmony_ci				ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
47862306a36Sopenharmony_ci		}
47962306a36Sopenharmony_ci	}
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	return done;
48262306a36Sopenharmony_ci}
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_civoid btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info,
48562306a36Sopenharmony_ci			      struct btrfs_delayed_ref_root *delayed_refs,
48662306a36Sopenharmony_ci			      struct btrfs_delayed_ref_head *head)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	struct btrfs_delayed_ref_node *ref;
48962306a36Sopenharmony_ci	struct rb_node *node;
49062306a36Sopenharmony_ci	u64 seq = 0;
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	lockdep_assert_held(&head->lock);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
49562306a36Sopenharmony_ci		return;
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	/* We don't have too many refs to merge for data. */
49862306a36Sopenharmony_ci	if (head->is_data)
49962306a36Sopenharmony_ci		return;
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	seq = btrfs_tree_mod_log_lowest_seq(fs_info);
50262306a36Sopenharmony_ciagain:
50362306a36Sopenharmony_ci	for (node = rb_first_cached(&head->ref_tree); node;
50462306a36Sopenharmony_ci	     node = rb_next(node)) {
50562306a36Sopenharmony_ci		ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
50662306a36Sopenharmony_ci		if (seq && ref->seq >= seq)
50762306a36Sopenharmony_ci			continue;
50862306a36Sopenharmony_ci		if (merge_ref(delayed_refs, head, ref, seq))
50962306a36Sopenharmony_ci			goto again;
51062306a36Sopenharmony_ci	}
51162306a36Sopenharmony_ci}
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ciint btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq)
51462306a36Sopenharmony_ci{
51562306a36Sopenharmony_ci	int ret = 0;
51662306a36Sopenharmony_ci	u64 min_seq = btrfs_tree_mod_log_lowest_seq(fs_info);
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci	if (min_seq != 0 && seq >= min_seq) {
51962306a36Sopenharmony_ci		btrfs_debug(fs_info,
52062306a36Sopenharmony_ci			    "holding back delayed_ref %llu, lowest is %llu",
52162306a36Sopenharmony_ci			    seq, min_seq);
52262306a36Sopenharmony_ci		ret = 1;
52362306a36Sopenharmony_ci	}
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	return ret;
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_cistruct btrfs_delayed_ref_head *btrfs_select_ref_head(
52962306a36Sopenharmony_ci		struct btrfs_delayed_ref_root *delayed_refs)
53062306a36Sopenharmony_ci{
53162306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *head;
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	lockdep_assert_held(&delayed_refs->lock);
53462306a36Sopenharmony_ciagain:
53562306a36Sopenharmony_ci	head = find_ref_head(delayed_refs, delayed_refs->run_delayed_start,
53662306a36Sopenharmony_ci			     true);
53762306a36Sopenharmony_ci	if (!head && delayed_refs->run_delayed_start != 0) {
53862306a36Sopenharmony_ci		delayed_refs->run_delayed_start = 0;
53962306a36Sopenharmony_ci		head = find_first_ref_head(delayed_refs);
54062306a36Sopenharmony_ci	}
54162306a36Sopenharmony_ci	if (!head)
54262306a36Sopenharmony_ci		return NULL;
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_ci	while (head->processing) {
54562306a36Sopenharmony_ci		struct rb_node *node;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci		node = rb_next(&head->href_node);
54862306a36Sopenharmony_ci		if (!node) {
54962306a36Sopenharmony_ci			if (delayed_refs->run_delayed_start == 0)
55062306a36Sopenharmony_ci				return NULL;
55162306a36Sopenharmony_ci			delayed_refs->run_delayed_start = 0;
55262306a36Sopenharmony_ci			goto again;
55362306a36Sopenharmony_ci		}
55462306a36Sopenharmony_ci		head = rb_entry(node, struct btrfs_delayed_ref_head,
55562306a36Sopenharmony_ci				href_node);
55662306a36Sopenharmony_ci	}
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	head->processing = true;
55962306a36Sopenharmony_ci	WARN_ON(delayed_refs->num_heads_ready == 0);
56062306a36Sopenharmony_ci	delayed_refs->num_heads_ready--;
56162306a36Sopenharmony_ci	delayed_refs->run_delayed_start = head->bytenr +
56262306a36Sopenharmony_ci		head->num_bytes;
56362306a36Sopenharmony_ci	return head;
56462306a36Sopenharmony_ci}
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_civoid btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
56762306a36Sopenharmony_ci			   struct btrfs_delayed_ref_head *head)
56862306a36Sopenharmony_ci{
56962306a36Sopenharmony_ci	lockdep_assert_held(&delayed_refs->lock);
57062306a36Sopenharmony_ci	lockdep_assert_held(&head->lock);
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci	rb_erase_cached(&head->href_node, &delayed_refs->href_root);
57362306a36Sopenharmony_ci	RB_CLEAR_NODE(&head->href_node);
57462306a36Sopenharmony_ci	atomic_dec(&delayed_refs->num_entries);
57562306a36Sopenharmony_ci	delayed_refs->num_heads--;
57662306a36Sopenharmony_ci	if (!head->processing)
57762306a36Sopenharmony_ci		delayed_refs->num_heads_ready--;
57862306a36Sopenharmony_ci}
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci/*
58162306a36Sopenharmony_ci * Helper to insert the ref_node to the tail or merge with tail.
58262306a36Sopenharmony_ci *
58362306a36Sopenharmony_ci * Return false if the ref was inserted.
58462306a36Sopenharmony_ci * Return true if the ref was merged into an existing one (and therefore can be
58562306a36Sopenharmony_ci * freed by the caller).
58662306a36Sopenharmony_ci */
58762306a36Sopenharmony_cistatic bool insert_delayed_ref(struct btrfs_delayed_ref_root *root,
58862306a36Sopenharmony_ci			       struct btrfs_delayed_ref_head *href,
58962306a36Sopenharmony_ci			       struct btrfs_delayed_ref_node *ref)
59062306a36Sopenharmony_ci{
59162306a36Sopenharmony_ci	struct btrfs_delayed_ref_node *exist;
59262306a36Sopenharmony_ci	int mod;
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	spin_lock(&href->lock);
59562306a36Sopenharmony_ci	exist = tree_insert(&href->ref_tree, ref);
59662306a36Sopenharmony_ci	if (!exist) {
59762306a36Sopenharmony_ci		if (ref->action == BTRFS_ADD_DELAYED_REF)
59862306a36Sopenharmony_ci			list_add_tail(&ref->add_list, &href->ref_add_list);
59962306a36Sopenharmony_ci		atomic_inc(&root->num_entries);
60062306a36Sopenharmony_ci		spin_unlock(&href->lock);
60162306a36Sopenharmony_ci		return false;
60262306a36Sopenharmony_ci	}
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci	/* Now we are sure we can merge */
60562306a36Sopenharmony_ci	if (exist->action == ref->action) {
60662306a36Sopenharmony_ci		mod = ref->ref_mod;
60762306a36Sopenharmony_ci	} else {
60862306a36Sopenharmony_ci		/* Need to change action */
60962306a36Sopenharmony_ci		if (exist->ref_mod < ref->ref_mod) {
61062306a36Sopenharmony_ci			exist->action = ref->action;
61162306a36Sopenharmony_ci			mod = -exist->ref_mod;
61262306a36Sopenharmony_ci			exist->ref_mod = ref->ref_mod;
61362306a36Sopenharmony_ci			if (ref->action == BTRFS_ADD_DELAYED_REF)
61462306a36Sopenharmony_ci				list_add_tail(&exist->add_list,
61562306a36Sopenharmony_ci					      &href->ref_add_list);
61662306a36Sopenharmony_ci			else if (ref->action == BTRFS_DROP_DELAYED_REF) {
61762306a36Sopenharmony_ci				ASSERT(!list_empty(&exist->add_list));
61862306a36Sopenharmony_ci				list_del(&exist->add_list);
61962306a36Sopenharmony_ci			} else {
62062306a36Sopenharmony_ci				ASSERT(0);
62162306a36Sopenharmony_ci			}
62262306a36Sopenharmony_ci		} else
62362306a36Sopenharmony_ci			mod = -ref->ref_mod;
62462306a36Sopenharmony_ci	}
62562306a36Sopenharmony_ci	exist->ref_mod += mod;
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci	/* remove existing tail if its ref_mod is zero */
62862306a36Sopenharmony_ci	if (exist->ref_mod == 0)
62962306a36Sopenharmony_ci		drop_delayed_ref(root, href, exist);
63062306a36Sopenharmony_ci	spin_unlock(&href->lock);
63162306a36Sopenharmony_ci	return true;
63262306a36Sopenharmony_ci}
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci/*
63562306a36Sopenharmony_ci * helper function to update the accounting in the head ref
63662306a36Sopenharmony_ci * existing and update must have the same bytenr
63762306a36Sopenharmony_ci */
63862306a36Sopenharmony_cistatic noinline void update_existing_head_ref(struct btrfs_trans_handle *trans,
63962306a36Sopenharmony_ci			 struct btrfs_delayed_ref_head *existing,
64062306a36Sopenharmony_ci			 struct btrfs_delayed_ref_head *update)
64162306a36Sopenharmony_ci{
64262306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs =
64362306a36Sopenharmony_ci		&trans->transaction->delayed_refs;
64462306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
64562306a36Sopenharmony_ci	int old_ref_mod;
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci	BUG_ON(existing->is_data != update->is_data);
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci	spin_lock(&existing->lock);
65062306a36Sopenharmony_ci	if (update->must_insert_reserved) {
65162306a36Sopenharmony_ci		/* if the extent was freed and then
65262306a36Sopenharmony_ci		 * reallocated before the delayed ref
65362306a36Sopenharmony_ci		 * entries were processed, we can end up
65462306a36Sopenharmony_ci		 * with an existing head ref without
65562306a36Sopenharmony_ci		 * the must_insert_reserved flag set.
65662306a36Sopenharmony_ci		 * Set it again here
65762306a36Sopenharmony_ci		 */
65862306a36Sopenharmony_ci		existing->must_insert_reserved = update->must_insert_reserved;
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci		/*
66162306a36Sopenharmony_ci		 * update the num_bytes so we make sure the accounting
66262306a36Sopenharmony_ci		 * is done correctly
66362306a36Sopenharmony_ci		 */
66462306a36Sopenharmony_ci		existing->num_bytes = update->num_bytes;
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ci	}
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	if (update->extent_op) {
66962306a36Sopenharmony_ci		if (!existing->extent_op) {
67062306a36Sopenharmony_ci			existing->extent_op = update->extent_op;
67162306a36Sopenharmony_ci		} else {
67262306a36Sopenharmony_ci			if (update->extent_op->update_key) {
67362306a36Sopenharmony_ci				memcpy(&existing->extent_op->key,
67462306a36Sopenharmony_ci				       &update->extent_op->key,
67562306a36Sopenharmony_ci				       sizeof(update->extent_op->key));
67662306a36Sopenharmony_ci				existing->extent_op->update_key = true;
67762306a36Sopenharmony_ci			}
67862306a36Sopenharmony_ci			if (update->extent_op->update_flags) {
67962306a36Sopenharmony_ci				existing->extent_op->flags_to_set |=
68062306a36Sopenharmony_ci					update->extent_op->flags_to_set;
68162306a36Sopenharmony_ci				existing->extent_op->update_flags = true;
68262306a36Sopenharmony_ci			}
68362306a36Sopenharmony_ci			btrfs_free_delayed_extent_op(update->extent_op);
68462306a36Sopenharmony_ci		}
68562306a36Sopenharmony_ci	}
68662306a36Sopenharmony_ci	/*
68762306a36Sopenharmony_ci	 * update the reference mod on the head to reflect this new operation,
68862306a36Sopenharmony_ci	 * only need the lock for this case cause we could be processing it
68962306a36Sopenharmony_ci	 * currently, for refs we just added we know we're a-ok.
69062306a36Sopenharmony_ci	 */
69162306a36Sopenharmony_ci	old_ref_mod = existing->total_ref_mod;
69262306a36Sopenharmony_ci	existing->ref_mod += update->ref_mod;
69362306a36Sopenharmony_ci	existing->total_ref_mod += update->ref_mod;
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci	/*
69662306a36Sopenharmony_ci	 * If we are going to from a positive ref mod to a negative or vice
69762306a36Sopenharmony_ci	 * versa we need to make sure to adjust pending_csums accordingly.
69862306a36Sopenharmony_ci	 */
69962306a36Sopenharmony_ci	if (existing->is_data) {
70062306a36Sopenharmony_ci		u64 csum_leaves =
70162306a36Sopenharmony_ci			btrfs_csum_bytes_to_leaves(fs_info,
70262306a36Sopenharmony_ci						   existing->num_bytes);
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci		if (existing->total_ref_mod >= 0 && old_ref_mod < 0) {
70562306a36Sopenharmony_ci			delayed_refs->pending_csums -= existing->num_bytes;
70662306a36Sopenharmony_ci			btrfs_delayed_refs_rsv_release(fs_info, csum_leaves);
70762306a36Sopenharmony_ci		}
70862306a36Sopenharmony_ci		if (existing->total_ref_mod < 0 && old_ref_mod >= 0) {
70962306a36Sopenharmony_ci			delayed_refs->pending_csums += existing->num_bytes;
71062306a36Sopenharmony_ci			trans->delayed_ref_updates += csum_leaves;
71162306a36Sopenharmony_ci		}
71262306a36Sopenharmony_ci	}
71362306a36Sopenharmony_ci
71462306a36Sopenharmony_ci	spin_unlock(&existing->lock);
71562306a36Sopenharmony_ci}
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_cistatic void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref,
71862306a36Sopenharmony_ci				  struct btrfs_qgroup_extent_record *qrecord,
71962306a36Sopenharmony_ci				  u64 bytenr, u64 num_bytes, u64 ref_root,
72062306a36Sopenharmony_ci				  u64 reserved, int action, bool is_data,
72162306a36Sopenharmony_ci				  bool is_system)
72262306a36Sopenharmony_ci{
72362306a36Sopenharmony_ci	int count_mod = 1;
72462306a36Sopenharmony_ci	bool must_insert_reserved = false;
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci	/* If reserved is provided, it must be a data extent. */
72762306a36Sopenharmony_ci	BUG_ON(!is_data && reserved);
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci	switch (action) {
73062306a36Sopenharmony_ci	case BTRFS_UPDATE_DELAYED_HEAD:
73162306a36Sopenharmony_ci		count_mod = 0;
73262306a36Sopenharmony_ci		break;
73362306a36Sopenharmony_ci	case BTRFS_DROP_DELAYED_REF:
73462306a36Sopenharmony_ci		/*
73562306a36Sopenharmony_ci		 * The head node stores the sum of all the mods, so dropping a ref
73662306a36Sopenharmony_ci		 * should drop the sum in the head node by one.
73762306a36Sopenharmony_ci		 */
73862306a36Sopenharmony_ci		count_mod = -1;
73962306a36Sopenharmony_ci		break;
74062306a36Sopenharmony_ci	case BTRFS_ADD_DELAYED_EXTENT:
74162306a36Sopenharmony_ci		/*
74262306a36Sopenharmony_ci		 * BTRFS_ADD_DELAYED_EXTENT means that we need to update the
74362306a36Sopenharmony_ci		 * reserved accounting when the extent is finally added, or if a
74462306a36Sopenharmony_ci		 * later modification deletes the delayed ref without ever
74562306a36Sopenharmony_ci		 * inserting the extent into the extent allocation tree.
74662306a36Sopenharmony_ci		 * ref->must_insert_reserved is the flag used to record that
74762306a36Sopenharmony_ci		 * accounting mods are required.
74862306a36Sopenharmony_ci		 *
74962306a36Sopenharmony_ci		 * Once we record must_insert_reserved, switch the action to
75062306a36Sopenharmony_ci		 * BTRFS_ADD_DELAYED_REF because other special casing is not
75162306a36Sopenharmony_ci		 * required.
75262306a36Sopenharmony_ci		 */
75362306a36Sopenharmony_ci		must_insert_reserved = true;
75462306a36Sopenharmony_ci		break;
75562306a36Sopenharmony_ci	}
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci	refcount_set(&head_ref->refs, 1);
75862306a36Sopenharmony_ci	head_ref->bytenr = bytenr;
75962306a36Sopenharmony_ci	head_ref->num_bytes = num_bytes;
76062306a36Sopenharmony_ci	head_ref->ref_mod = count_mod;
76162306a36Sopenharmony_ci	head_ref->must_insert_reserved = must_insert_reserved;
76262306a36Sopenharmony_ci	head_ref->is_data = is_data;
76362306a36Sopenharmony_ci	head_ref->is_system = is_system;
76462306a36Sopenharmony_ci	head_ref->ref_tree = RB_ROOT_CACHED;
76562306a36Sopenharmony_ci	INIT_LIST_HEAD(&head_ref->ref_add_list);
76662306a36Sopenharmony_ci	RB_CLEAR_NODE(&head_ref->href_node);
76762306a36Sopenharmony_ci	head_ref->processing = false;
76862306a36Sopenharmony_ci	head_ref->total_ref_mod = count_mod;
76962306a36Sopenharmony_ci	spin_lock_init(&head_ref->lock);
77062306a36Sopenharmony_ci	mutex_init(&head_ref->mutex);
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci	if (qrecord) {
77362306a36Sopenharmony_ci		if (ref_root && reserved) {
77462306a36Sopenharmony_ci			qrecord->data_rsv = reserved;
77562306a36Sopenharmony_ci			qrecord->data_rsv_refroot = ref_root;
77662306a36Sopenharmony_ci		}
77762306a36Sopenharmony_ci		qrecord->bytenr = bytenr;
77862306a36Sopenharmony_ci		qrecord->num_bytes = num_bytes;
77962306a36Sopenharmony_ci		qrecord->old_roots = NULL;
78062306a36Sopenharmony_ci	}
78162306a36Sopenharmony_ci}
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci/*
78462306a36Sopenharmony_ci * helper function to actually insert a head node into the rbtree.
78562306a36Sopenharmony_ci * this does all the dirty work in terms of maintaining the correct
78662306a36Sopenharmony_ci * overall modification count.
78762306a36Sopenharmony_ci */
78862306a36Sopenharmony_cistatic noinline struct btrfs_delayed_ref_head *
78962306a36Sopenharmony_ciadd_delayed_ref_head(struct btrfs_trans_handle *trans,
79062306a36Sopenharmony_ci		     struct btrfs_delayed_ref_head *head_ref,
79162306a36Sopenharmony_ci		     struct btrfs_qgroup_extent_record *qrecord,
79262306a36Sopenharmony_ci		     int action, bool *qrecord_inserted_ret)
79362306a36Sopenharmony_ci{
79462306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *existing;
79562306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
79662306a36Sopenharmony_ci	bool qrecord_inserted = false;
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
79962306a36Sopenharmony_ci
80062306a36Sopenharmony_ci	/* Record qgroup extent info if provided */
80162306a36Sopenharmony_ci	if (qrecord) {
80262306a36Sopenharmony_ci		if (btrfs_qgroup_trace_extent_nolock(trans->fs_info,
80362306a36Sopenharmony_ci					delayed_refs, qrecord))
80462306a36Sopenharmony_ci			kfree(qrecord);
80562306a36Sopenharmony_ci		else
80662306a36Sopenharmony_ci			qrecord_inserted = true;
80762306a36Sopenharmony_ci	}
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci	trace_add_delayed_ref_head(trans->fs_info, head_ref, action);
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci	existing = htree_insert(&delayed_refs->href_root,
81262306a36Sopenharmony_ci				&head_ref->href_node);
81362306a36Sopenharmony_ci	if (existing) {
81462306a36Sopenharmony_ci		update_existing_head_ref(trans, existing, head_ref);
81562306a36Sopenharmony_ci		/*
81662306a36Sopenharmony_ci		 * we've updated the existing ref, free the newly
81762306a36Sopenharmony_ci		 * allocated ref
81862306a36Sopenharmony_ci		 */
81962306a36Sopenharmony_ci		kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
82062306a36Sopenharmony_ci		head_ref = existing;
82162306a36Sopenharmony_ci	} else {
82262306a36Sopenharmony_ci		if (head_ref->is_data && head_ref->ref_mod < 0) {
82362306a36Sopenharmony_ci			delayed_refs->pending_csums += head_ref->num_bytes;
82462306a36Sopenharmony_ci			trans->delayed_ref_updates +=
82562306a36Sopenharmony_ci				btrfs_csum_bytes_to_leaves(trans->fs_info,
82662306a36Sopenharmony_ci							   head_ref->num_bytes);
82762306a36Sopenharmony_ci		}
82862306a36Sopenharmony_ci		delayed_refs->num_heads++;
82962306a36Sopenharmony_ci		delayed_refs->num_heads_ready++;
83062306a36Sopenharmony_ci		atomic_inc(&delayed_refs->num_entries);
83162306a36Sopenharmony_ci		trans->delayed_ref_updates++;
83262306a36Sopenharmony_ci	}
83362306a36Sopenharmony_ci	if (qrecord_inserted_ret)
83462306a36Sopenharmony_ci		*qrecord_inserted_ret = qrecord_inserted;
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci	return head_ref;
83762306a36Sopenharmony_ci}
83862306a36Sopenharmony_ci
83962306a36Sopenharmony_ci/*
84062306a36Sopenharmony_ci * init_delayed_ref_common - Initialize the structure which represents a
84162306a36Sopenharmony_ci *			     modification to a an extent.
84262306a36Sopenharmony_ci *
84362306a36Sopenharmony_ci * @fs_info:    Internal to the mounted filesystem mount structure.
84462306a36Sopenharmony_ci *
84562306a36Sopenharmony_ci * @ref:	The structure which is going to be initialized.
84662306a36Sopenharmony_ci *
84762306a36Sopenharmony_ci * @bytenr:	The logical address of the extent for which a modification is
84862306a36Sopenharmony_ci *		going to be recorded.
84962306a36Sopenharmony_ci *
85062306a36Sopenharmony_ci * @num_bytes:  Size of the extent whose modification is being recorded.
85162306a36Sopenharmony_ci *
85262306a36Sopenharmony_ci * @ref_root:	The id of the root where this modification has originated, this
85362306a36Sopenharmony_ci *		can be either one of the well-known metadata trees or the
85462306a36Sopenharmony_ci *		subvolume id which references this extent.
85562306a36Sopenharmony_ci *
85662306a36Sopenharmony_ci * @action:	Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or
85762306a36Sopenharmony_ci *		BTRFS_ADD_DELAYED_EXTENT
85862306a36Sopenharmony_ci *
85962306a36Sopenharmony_ci * @ref_type:	Holds the type of the extent which is being recorded, can be
86062306a36Sopenharmony_ci *		one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY
86162306a36Sopenharmony_ci *		when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/
86262306a36Sopenharmony_ci *		BTRFS_EXTENT_DATA_REF_KEY when recording data extent
86362306a36Sopenharmony_ci */
86462306a36Sopenharmony_cistatic void init_delayed_ref_common(struct btrfs_fs_info *fs_info,
86562306a36Sopenharmony_ci				    struct btrfs_delayed_ref_node *ref,
86662306a36Sopenharmony_ci				    u64 bytenr, u64 num_bytes, u64 ref_root,
86762306a36Sopenharmony_ci				    int action, u8 ref_type)
86862306a36Sopenharmony_ci{
86962306a36Sopenharmony_ci	u64 seq = 0;
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_ci	if (action == BTRFS_ADD_DELAYED_EXTENT)
87262306a36Sopenharmony_ci		action = BTRFS_ADD_DELAYED_REF;
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci	if (is_fstree(ref_root))
87562306a36Sopenharmony_ci		seq = atomic64_read(&fs_info->tree_mod_seq);
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ci	refcount_set(&ref->refs, 1);
87862306a36Sopenharmony_ci	ref->bytenr = bytenr;
87962306a36Sopenharmony_ci	ref->num_bytes = num_bytes;
88062306a36Sopenharmony_ci	ref->ref_mod = 1;
88162306a36Sopenharmony_ci	ref->action = action;
88262306a36Sopenharmony_ci	ref->seq = seq;
88362306a36Sopenharmony_ci	ref->type = ref_type;
88462306a36Sopenharmony_ci	RB_CLEAR_NODE(&ref->ref_node);
88562306a36Sopenharmony_ci	INIT_LIST_HEAD(&ref->add_list);
88662306a36Sopenharmony_ci}
88762306a36Sopenharmony_ci
88862306a36Sopenharmony_ci/*
88962306a36Sopenharmony_ci * add a delayed tree ref.  This does all of the accounting required
89062306a36Sopenharmony_ci * to make sure the delayed ref is eventually processed before this
89162306a36Sopenharmony_ci * transaction commits.
89262306a36Sopenharmony_ci */
89362306a36Sopenharmony_ciint btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
89462306a36Sopenharmony_ci			       struct btrfs_ref *generic_ref,
89562306a36Sopenharmony_ci			       struct btrfs_delayed_extent_op *extent_op)
89662306a36Sopenharmony_ci{
89762306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
89862306a36Sopenharmony_ci	struct btrfs_delayed_tree_ref *ref;
89962306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *head_ref;
90062306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
90162306a36Sopenharmony_ci	struct btrfs_qgroup_extent_record *record = NULL;
90262306a36Sopenharmony_ci	bool qrecord_inserted;
90362306a36Sopenharmony_ci	bool is_system;
90462306a36Sopenharmony_ci	bool merged;
90562306a36Sopenharmony_ci	int action = generic_ref->action;
90662306a36Sopenharmony_ci	int level = generic_ref->tree_ref.level;
90762306a36Sopenharmony_ci	u64 bytenr = generic_ref->bytenr;
90862306a36Sopenharmony_ci	u64 num_bytes = generic_ref->len;
90962306a36Sopenharmony_ci	u64 parent = generic_ref->parent;
91062306a36Sopenharmony_ci	u8 ref_type;
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci	is_system = (generic_ref->tree_ref.owning_root == BTRFS_CHUNK_TREE_OBJECTID);
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci	ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action);
91562306a36Sopenharmony_ci	ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
91662306a36Sopenharmony_ci	if (!ref)
91762306a36Sopenharmony_ci		return -ENOMEM;
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
92062306a36Sopenharmony_ci	if (!head_ref) {
92162306a36Sopenharmony_ci		kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
92262306a36Sopenharmony_ci		return -ENOMEM;
92362306a36Sopenharmony_ci	}
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
92662306a36Sopenharmony_ci	    !generic_ref->skip_qgroup) {
92762306a36Sopenharmony_ci		record = kzalloc(sizeof(*record), GFP_NOFS);
92862306a36Sopenharmony_ci		if (!record) {
92962306a36Sopenharmony_ci			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
93062306a36Sopenharmony_ci			kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
93162306a36Sopenharmony_ci			return -ENOMEM;
93262306a36Sopenharmony_ci		}
93362306a36Sopenharmony_ci	}
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci	if (parent)
93662306a36Sopenharmony_ci		ref_type = BTRFS_SHARED_BLOCK_REF_KEY;
93762306a36Sopenharmony_ci	else
93862306a36Sopenharmony_ci		ref_type = BTRFS_TREE_BLOCK_REF_KEY;
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
94162306a36Sopenharmony_ci				generic_ref->tree_ref.owning_root, action,
94262306a36Sopenharmony_ci				ref_type);
94362306a36Sopenharmony_ci	ref->root = generic_ref->tree_ref.owning_root;
94462306a36Sopenharmony_ci	ref->parent = parent;
94562306a36Sopenharmony_ci	ref->level = level;
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci	init_delayed_ref_head(head_ref, record, bytenr, num_bytes,
94862306a36Sopenharmony_ci			      generic_ref->tree_ref.owning_root, 0, action,
94962306a36Sopenharmony_ci			      false, is_system);
95062306a36Sopenharmony_ci	head_ref->extent_op = extent_op;
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
95362306a36Sopenharmony_ci	spin_lock(&delayed_refs->lock);
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci	/*
95662306a36Sopenharmony_ci	 * insert both the head node and the new ref without dropping
95762306a36Sopenharmony_ci	 * the spin lock
95862306a36Sopenharmony_ci	 */
95962306a36Sopenharmony_ci	head_ref = add_delayed_ref_head(trans, head_ref, record,
96062306a36Sopenharmony_ci					action, &qrecord_inserted);
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_ci	merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);
96362306a36Sopenharmony_ci	spin_unlock(&delayed_refs->lock);
96462306a36Sopenharmony_ci
96562306a36Sopenharmony_ci	/*
96662306a36Sopenharmony_ci	 * Need to update the delayed_refs_rsv with any changes we may have
96762306a36Sopenharmony_ci	 * made.
96862306a36Sopenharmony_ci	 */
96962306a36Sopenharmony_ci	btrfs_update_delayed_refs_rsv(trans);
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci	trace_add_delayed_tree_ref(fs_info, &ref->node, ref,
97262306a36Sopenharmony_ci				   action == BTRFS_ADD_DELAYED_EXTENT ?
97362306a36Sopenharmony_ci				   BTRFS_ADD_DELAYED_REF : action);
97462306a36Sopenharmony_ci	if (merged)
97562306a36Sopenharmony_ci		kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci	if (qrecord_inserted)
97862306a36Sopenharmony_ci		btrfs_qgroup_trace_extent_post(trans, record);
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_ci	return 0;
98162306a36Sopenharmony_ci}
98262306a36Sopenharmony_ci
98362306a36Sopenharmony_ci/*
98462306a36Sopenharmony_ci * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
98562306a36Sopenharmony_ci */
98662306a36Sopenharmony_ciint btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
98762306a36Sopenharmony_ci			       struct btrfs_ref *generic_ref,
98862306a36Sopenharmony_ci			       u64 reserved)
98962306a36Sopenharmony_ci{
99062306a36Sopenharmony_ci	struct btrfs_fs_info *fs_info = trans->fs_info;
99162306a36Sopenharmony_ci	struct btrfs_delayed_data_ref *ref;
99262306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *head_ref;
99362306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
99462306a36Sopenharmony_ci	struct btrfs_qgroup_extent_record *record = NULL;
99562306a36Sopenharmony_ci	bool qrecord_inserted;
99662306a36Sopenharmony_ci	int action = generic_ref->action;
99762306a36Sopenharmony_ci	bool merged;
99862306a36Sopenharmony_ci	u64 bytenr = generic_ref->bytenr;
99962306a36Sopenharmony_ci	u64 num_bytes = generic_ref->len;
100062306a36Sopenharmony_ci	u64 parent = generic_ref->parent;
100162306a36Sopenharmony_ci	u64 ref_root = generic_ref->data_ref.owning_root;
100262306a36Sopenharmony_ci	u64 owner = generic_ref->data_ref.ino;
100362306a36Sopenharmony_ci	u64 offset = generic_ref->data_ref.offset;
100462306a36Sopenharmony_ci	u8 ref_type;
100562306a36Sopenharmony_ci
100662306a36Sopenharmony_ci	ASSERT(generic_ref->type == BTRFS_REF_DATA && action);
100762306a36Sopenharmony_ci	ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
100862306a36Sopenharmony_ci	if (!ref)
100962306a36Sopenharmony_ci		return -ENOMEM;
101062306a36Sopenharmony_ci
101162306a36Sopenharmony_ci	if (parent)
101262306a36Sopenharmony_ci	        ref_type = BTRFS_SHARED_DATA_REF_KEY;
101362306a36Sopenharmony_ci	else
101462306a36Sopenharmony_ci	        ref_type = BTRFS_EXTENT_DATA_REF_KEY;
101562306a36Sopenharmony_ci	init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes,
101662306a36Sopenharmony_ci				ref_root, action, ref_type);
101762306a36Sopenharmony_ci	ref->root = ref_root;
101862306a36Sopenharmony_ci	ref->parent = parent;
101962306a36Sopenharmony_ci	ref->objectid = owner;
102062306a36Sopenharmony_ci	ref->offset = offset;
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
102462306a36Sopenharmony_ci	if (!head_ref) {
102562306a36Sopenharmony_ci		kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
102662306a36Sopenharmony_ci		return -ENOMEM;
102762306a36Sopenharmony_ci	}
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_ci	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
103062306a36Sopenharmony_ci	    !generic_ref->skip_qgroup) {
103162306a36Sopenharmony_ci		record = kzalloc(sizeof(*record), GFP_NOFS);
103262306a36Sopenharmony_ci		if (!record) {
103362306a36Sopenharmony_ci			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
103462306a36Sopenharmony_ci			kmem_cache_free(btrfs_delayed_ref_head_cachep,
103562306a36Sopenharmony_ci					head_ref);
103662306a36Sopenharmony_ci			return -ENOMEM;
103762306a36Sopenharmony_ci		}
103862306a36Sopenharmony_ci	}
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_ci	init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root,
104162306a36Sopenharmony_ci			      reserved, action, true, false);
104262306a36Sopenharmony_ci	head_ref->extent_op = NULL;
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
104562306a36Sopenharmony_ci	spin_lock(&delayed_refs->lock);
104662306a36Sopenharmony_ci
104762306a36Sopenharmony_ci	/*
104862306a36Sopenharmony_ci	 * insert both the head node and the new ref without dropping
104962306a36Sopenharmony_ci	 * the spin lock
105062306a36Sopenharmony_ci	 */
105162306a36Sopenharmony_ci	head_ref = add_delayed_ref_head(trans, head_ref, record,
105262306a36Sopenharmony_ci					action, &qrecord_inserted);
105362306a36Sopenharmony_ci
105462306a36Sopenharmony_ci	merged = insert_delayed_ref(delayed_refs, head_ref, &ref->node);
105562306a36Sopenharmony_ci	spin_unlock(&delayed_refs->lock);
105662306a36Sopenharmony_ci
105762306a36Sopenharmony_ci	/*
105862306a36Sopenharmony_ci	 * Need to update the delayed_refs_rsv with any changes we may have
105962306a36Sopenharmony_ci	 * made.
106062306a36Sopenharmony_ci	 */
106162306a36Sopenharmony_ci	btrfs_update_delayed_refs_rsv(trans);
106262306a36Sopenharmony_ci
106362306a36Sopenharmony_ci	trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref,
106462306a36Sopenharmony_ci				   action == BTRFS_ADD_DELAYED_EXTENT ?
106562306a36Sopenharmony_ci				   BTRFS_ADD_DELAYED_REF : action);
106662306a36Sopenharmony_ci	if (merged)
106762306a36Sopenharmony_ci		kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci	if (qrecord_inserted)
107162306a36Sopenharmony_ci		return btrfs_qgroup_trace_extent_post(trans, record);
107262306a36Sopenharmony_ci	return 0;
107362306a36Sopenharmony_ci}
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ciint btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
107662306a36Sopenharmony_ci				u64 bytenr, u64 num_bytes,
107762306a36Sopenharmony_ci				struct btrfs_delayed_extent_op *extent_op)
107862306a36Sopenharmony_ci{
107962306a36Sopenharmony_ci	struct btrfs_delayed_ref_head *head_ref;
108062306a36Sopenharmony_ci	struct btrfs_delayed_ref_root *delayed_refs;
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
108362306a36Sopenharmony_ci	if (!head_ref)
108462306a36Sopenharmony_ci		return -ENOMEM;
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_ci	init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0,
108762306a36Sopenharmony_ci			      BTRFS_UPDATE_DELAYED_HEAD, false, false);
108862306a36Sopenharmony_ci	head_ref->extent_op = extent_op;
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci	delayed_refs = &trans->transaction->delayed_refs;
109162306a36Sopenharmony_ci	spin_lock(&delayed_refs->lock);
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_ci	add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD,
109462306a36Sopenharmony_ci			     NULL);
109562306a36Sopenharmony_ci
109662306a36Sopenharmony_ci	spin_unlock(&delayed_refs->lock);
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_ci	/*
109962306a36Sopenharmony_ci	 * Need to update the delayed_refs_rsv with any changes we may have
110062306a36Sopenharmony_ci	 * made.
110162306a36Sopenharmony_ci	 */
110262306a36Sopenharmony_ci	btrfs_update_delayed_refs_rsv(trans);
110362306a36Sopenharmony_ci	return 0;
110462306a36Sopenharmony_ci}
110562306a36Sopenharmony_ci
110662306a36Sopenharmony_ci/*
110762306a36Sopenharmony_ci * This does a simple search for the head node for a given extent.  Returns the
110862306a36Sopenharmony_ci * head node if found, or NULL if not.
110962306a36Sopenharmony_ci */
111062306a36Sopenharmony_cistruct btrfs_delayed_ref_head *
111162306a36Sopenharmony_cibtrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
111262306a36Sopenharmony_ci{
111362306a36Sopenharmony_ci	lockdep_assert_held(&delayed_refs->lock);
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci	return find_ref_head(delayed_refs, bytenr, false);
111662306a36Sopenharmony_ci}
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_civoid __cold btrfs_delayed_ref_exit(void)
111962306a36Sopenharmony_ci{
112062306a36Sopenharmony_ci	kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
112162306a36Sopenharmony_ci	kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
112262306a36Sopenharmony_ci	kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
112362306a36Sopenharmony_ci	kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
112462306a36Sopenharmony_ci}
112562306a36Sopenharmony_ci
112662306a36Sopenharmony_ciint __init btrfs_delayed_ref_init(void)
112762306a36Sopenharmony_ci{
112862306a36Sopenharmony_ci	btrfs_delayed_ref_head_cachep = kmem_cache_create(
112962306a36Sopenharmony_ci				"btrfs_delayed_ref_head",
113062306a36Sopenharmony_ci				sizeof(struct btrfs_delayed_ref_head), 0,
113162306a36Sopenharmony_ci				SLAB_MEM_SPREAD, NULL);
113262306a36Sopenharmony_ci	if (!btrfs_delayed_ref_head_cachep)
113362306a36Sopenharmony_ci		goto fail;
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_ci	btrfs_delayed_tree_ref_cachep = kmem_cache_create(
113662306a36Sopenharmony_ci				"btrfs_delayed_tree_ref",
113762306a36Sopenharmony_ci				sizeof(struct btrfs_delayed_tree_ref), 0,
113862306a36Sopenharmony_ci				SLAB_MEM_SPREAD, NULL);
113962306a36Sopenharmony_ci	if (!btrfs_delayed_tree_ref_cachep)
114062306a36Sopenharmony_ci		goto fail;
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci	btrfs_delayed_data_ref_cachep = kmem_cache_create(
114362306a36Sopenharmony_ci				"btrfs_delayed_data_ref",
114462306a36Sopenharmony_ci				sizeof(struct btrfs_delayed_data_ref), 0,
114562306a36Sopenharmony_ci				SLAB_MEM_SPREAD, NULL);
114662306a36Sopenharmony_ci	if (!btrfs_delayed_data_ref_cachep)
114762306a36Sopenharmony_ci		goto fail;
114862306a36Sopenharmony_ci
114962306a36Sopenharmony_ci	btrfs_delayed_extent_op_cachep = kmem_cache_create(
115062306a36Sopenharmony_ci				"btrfs_delayed_extent_op",
115162306a36Sopenharmony_ci				sizeof(struct btrfs_delayed_extent_op), 0,
115262306a36Sopenharmony_ci				SLAB_MEM_SPREAD, NULL);
115362306a36Sopenharmony_ci	if (!btrfs_delayed_extent_op_cachep)
115462306a36Sopenharmony_ci		goto fail;
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_ci	return 0;
115762306a36Sopenharmony_cifail:
115862306a36Sopenharmony_ci	btrfs_delayed_ref_exit();
115962306a36Sopenharmony_ci	return -ENOMEM;
116062306a36Sopenharmony_ci}
1161