delayed-ref.h 8.1 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4
/*
 * Copyright (C) 2008 Oracle.  All rights reserved.
 */
5 6 7

#ifndef BTRFS_DELAYED_REF_H
#define BTRFS_DELAYED_REF_H
8

9 10
#include <linux/refcount.h>

11
/* these are the possible values of struct btrfs_delayed_ref_node->action */
12 13 14
#define BTRFS_ADD_DELAYED_REF    1 /* add one backref to the tree */
#define BTRFS_DROP_DELAYED_REF   2 /* delete one backref from the tree */
#define BTRFS_ADD_DELAYED_EXTENT 3 /* record a full extent allocation */
15
#define BTRFS_UPDATE_DELAYED_HEAD 4 /* not changing ref count on head ref */
16 17

struct btrfs_delayed_ref_node {
18
	struct rb_node ref_node;
19 20 21 22 23 24
	/*
	 * If action is BTRFS_ADD_DELAYED_REF, also link this node to
	 * ref_head->ref_add_list, then we do not need to iterate the
	 * whole ref_head->ref_list to find BTRFS_ADD_DELAYED_REF nodes.
	 */
	struct list_head add_list;
25

26 27 28 29 30 31
	/* the starting bytenr of the extent */
	u64 bytenr;

	/* the size of the extent */
	u64 num_bytes;

32 33 34
	/* seq number to keep track of insertion order */
	u64 seq;

35
	/* ref count on this data structure */
36
	refcount_t refs;
37 38 39 40 41 42 43 44 45 46 47 48

	/*
	 * how many refs is this entry adding or deleting.  For
	 * head refs, this may be a negative number because it is keeping
	 * track of the total mods done to the reference count.
	 * For individual refs, this will always be a positive number
	 *
	 * It may be more than one, since it is possible for a single
	 * parent to have more than one ref on an extent
	 */
	int ref_mod;

49 50
	unsigned int action:8;
	unsigned int type:8;
51
	/* is this node still in the rbtree? */
52
	unsigned int is_head:1;
53 54 55
	unsigned int in_tree:1;
};

56 57
struct btrfs_delayed_extent_op {
	struct btrfs_disk_key key;
58 59 60 61
	u8 level;
	bool update_key;
	bool update_flags;
	bool is_data;
62 63 64
	u64 flags_to_set;
};

65 66 67 68 69 70 71
/*
 * the head refs are used to hold a lock on a given extent, which allows us
 * to make sure that only one process is running the delayed refs
 * at a time for a single extent.  They also store the sum of all the
 * reference count modifications we've queued up.
 */
struct btrfs_delayed_ref_head {
72 73 74
	u64 bytenr;
	u64 num_bytes;
	refcount_t refs;
75 76 77 78 79 80
	/*
	 * the mutex is held while running the refs, and it is also
	 * held when checking the sum of reference modifications.
	 */
	struct mutex mutex;

81
	spinlock_t lock;
82
	struct rb_root_cached ref_tree;
83 84
	/* accumulate add BTRFS_ADD_DELAYED_REF nodes to this ref_add_list. */
	struct list_head ref_add_list;
85

L
Liu Bo 已提交
86 87
	struct rb_node href_node;

88
	struct btrfs_delayed_extent_op *extent_op;
89 90 91 92 93 94 95 96

	/*
	 * This is used to track the final ref_mod from all the refs associated
	 * with this head ref, this is not adjusted as delayed refs are run,
	 * this is meant to track if we need to do the csum accounting or not.
	 */
	int total_ref_mod;

97 98 99 100 101 102 103 104
	/*
	 * This is the current outstanding mod references for this bytenr.  This
	 * is used with lookup_extent_info to get an accurate reference count
	 * for a bytenr, so it is adjusted as delayed refs are run so that any
	 * on disk reference count + ref_mod is accurate.
	 */
	int ref_mod;

105 106 107 108 109 110 111 112 113 114 115
	/*
	 * For qgroup reserved space freeing.
	 *
	 * ref_root and reserved will be recorded after
	 * BTRFS_ADD_DELAYED_EXTENT is called.
	 * And will be used to free reserved qgroup space at
	 * run_delayed_refs() time.
	 */
	u64 qgroup_ref_root;
	u64 qgroup_reserved;

116 117 118 119 120 121 122 123 124 125 126 127 128
	/*
	 * when a new extent is allocated, it is just reserved in memory
	 * The actual extent isn't inserted into the extent allocation tree
	 * until the delayed ref is processed.  must_insert_reserved is
	 * used to flag a delayed ref so the accounting can be updated
	 * when a full insert is done.
	 *
	 * It is possible the extent will be freed before it is ever
	 * inserted into the extent allocation tree.  In this case
	 * we need to update the in ram accounting to properly reflect
	 * the free has happened.
	 */
	unsigned int must_insert_reserved:1;
129
	unsigned int is_data:1;
130
	unsigned int is_system:1;
131
	unsigned int processing:1;
132 133
};

134
struct btrfs_delayed_tree_ref {
135
	struct btrfs_delayed_ref_node node;
136 137
	u64 root;
	u64 parent;
138 139
	int level;
};
140

141 142
struct btrfs_delayed_data_ref {
	struct btrfs_delayed_ref_node node;
143 144
	u64 root;
	u64 parent;
145 146
	u64 objectid;
	u64 offset;
147 148 149
};

struct btrfs_delayed_ref_root {
L
Liu Bo 已提交
150
	/* head ref rbtree */
151
	struct rb_root_cached href_root;
L
Liu Bo 已提交
152

153 154 155
	/* dirty extent records */
	struct rb_root dirty_extent_root;

156 157 158 159 160 161
	/* this spin lock protects the rbtree and the entries inside */
	spinlock_t lock;

	/* how many delayed ref updates we've queued, used by the
	 * throttling code
	 */
162
	atomic_t num_entries;
163

164 165 166 167 168 169
	/* total number of head nodes in tree */
	unsigned long num_heads;

	/* total number of head nodes ready for processing */
	unsigned long num_heads_ready;

170 171
	u64 pending_csums;

172 173 174 175 176 177
	/*
	 * set when the tree is flushing before a transaction commit,
	 * used by the throttling code to decide if new updates need
	 * to be run right away
	 */
	int flushing;
178 179

	u64 run_delayed_start;
180 181 182

	/*
	 * To make qgroup to skip given root.
183
	 * This is for snapshot, as btrfs_qgroup_inherit() will manually
184 185 186 187
	 * modify counters for snapshot and its source, so we should skip
	 * the snapshot in new_root/old_roots or it will get calculated twice
	 */
	u64 qgroup_to_skip;
188 189
};

190 191 192 193 194
extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
extern struct kmem_cache *btrfs_delayed_extent_op_cachep;

195
int __init btrfs_delayed_ref_init(void);
196
void __cold btrfs_delayed_ref_exit(void);
197 198 199 200 201 202 203 204 205 206 207 208 209 210

static inline struct btrfs_delayed_extent_op *
btrfs_alloc_delayed_extent_op(void)
{
	return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
}

static inline void
btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
{
	if (op)
		kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
}

211 212
static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
{
213 214
	WARN_ON(refcount_read(&ref->refs) == 0);
	if (refcount_dec_and_test(&ref->refs)) {
215
		WARN_ON(ref->in_tree);
216 217 218 219 220 221 222 223 224 225 226 227
		switch (ref->type) {
		case BTRFS_TREE_BLOCK_REF_KEY:
		case BTRFS_SHARED_BLOCK_REF_KEY:
			kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
			break;
		case BTRFS_EXTENT_DATA_REF_KEY:
		case BTRFS_SHARED_DATA_REF_KEY:
			kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
			break;
		default:
			BUG();
		}
228 229 230
	}
}

231 232 233 234 235 236
static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
{
	if (refcount_dec_and_test(&head->refs))
		kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
}

237
int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
238 239
			       u64 bytenr, u64 num_bytes, u64 parent,
			       u64 ref_root, int level, int action,
240 241
			       struct btrfs_delayed_extent_op *extent_op,
			       int *old_ref_mod, int *new_ref_mod);
242
int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
243 244
			       u64 bytenr, u64 num_bytes,
			       u64 parent, u64 ref_root,
245 246
			       u64 owner, u64 offset, u64 reserved, int action,
			       int *old_ref_mod, int *new_ref_mod);
A
Arne Jansen 已提交
247 248
int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
				struct btrfs_trans_handle *trans,
249 250
				u64 bytenr, u64 num_bytes,
				struct btrfs_delayed_extent_op *extent_op);
251 252 253
void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
			      struct btrfs_delayed_ref_root *delayed_refs,
			      struct btrfs_delayed_ref_head *head);
254

255
struct btrfs_delayed_ref_head *
256 257
btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
			    u64 bytenr);
258 259
int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
			   struct btrfs_delayed_ref_head *head);
260 261 262 263 264
static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
{
	mutex_unlock(&head->mutex);
}

265

266 267
struct btrfs_delayed_ref_head *btrfs_select_ref_head(
		struct btrfs_delayed_ref_root *delayed_refs);
268

269
int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
270

271 272 273
/*
 * helper functions to cast a node into its container
 */
274 275
static inline struct btrfs_delayed_tree_ref *
btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
276
{
277 278
	return container_of(node, struct btrfs_delayed_tree_ref, node);
}
279

280 281 282 283
static inline struct btrfs_delayed_data_ref *
btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
{
	return container_of(node, struct btrfs_delayed_data_ref, node);
284
}
285

286
#endif