1
2
3
4
5
6#ifndef BTRFS_DELAYED_REF_H
7#define BTRFS_DELAYED_REF_H
8
9#include <linux/refcount.h>
10
11
12#define BTRFS_ADD_DELAYED_REF 1
13#define BTRFS_DROP_DELAYED_REF 2
14#define BTRFS_ADD_DELAYED_EXTENT 3
15#define BTRFS_UPDATE_DELAYED_HEAD 4
16
17struct btrfs_delayed_ref_node {
18 struct rb_node ref_node;
19
20
21
22
23
24 struct list_head add_list;
25
26
27 u64 bytenr;
28
29
30 u64 num_bytes;
31
32
33 u64 seq;
34
35
36 refcount_t refs;
37
38
39
40
41
42
43
44
45
46
47 int ref_mod;
48
49 unsigned int action:8;
50 unsigned int type:8;
51
52 unsigned int is_head:1;
53 unsigned int in_tree:1;
54};
55
56struct btrfs_delayed_extent_op {
57 struct btrfs_disk_key key;
58 u8 level;
59 bool update_key;
60 bool update_flags;
61 bool is_data;
62 u64 flags_to_set;
63};
64
65
66
67
68
69
70
71struct btrfs_delayed_ref_head {
72 u64 bytenr;
73 u64 num_bytes;
74 refcount_t refs;
75
76
77
78
79 struct mutex mutex;
80
81 spinlock_t lock;
82 struct rb_root_cached ref_tree;
83
84 struct list_head ref_add_list;
85
86 struct rb_node href_node;
87
88 struct btrfs_delayed_extent_op *extent_op;
89
90
91
92
93
94
95 int total_ref_mod;
96
97
98
99
100
101
102
103 int ref_mod;
104
105
106
107
108
109
110
111
112
113 u64 qgroup_ref_root;
114 u64 qgroup_reserved;
115
116
117
118
119
120
121
122
123
124
125
126
127
128 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
134struct btrfs_delayed_tree_ref {
135 struct btrfs_delayed_ref_node node;
136 u64 root;
137 u64 parent;
138 int level;
139};
140
141struct btrfs_delayed_data_ref {
142 struct btrfs_delayed_ref_node node;
143 u64 root;
144 u64 parent;
145 u64 objectid;
146 u64 offset;
147};
148
149struct btrfs_delayed_ref_root {
150
151 struct rb_root_cached href_root;
152
153
154 struct rb_root dirty_extent_root;
155
156
157 spinlock_t lock;
158
159
160
161
162 atomic_t num_entries;
163
164
165 unsigned long num_heads;
166
167
168 unsigned long num_heads_ready;
169
170 u64 pending_csums;
171
172
173
174
175
176
177 int flushing;
178
179 u64 run_delayed_start;
180
181
182
183
184
185
186
187 u64 qgroup_to_skip;
188};
189
190extern struct kmem_cache *btrfs_delayed_ref_head_cachep;
191extern struct kmem_cache *btrfs_delayed_tree_ref_cachep;
192extern struct kmem_cache *btrfs_delayed_data_ref_cachep;
193extern struct kmem_cache *btrfs_delayed_extent_op_cachep;
194
195int __init btrfs_delayed_ref_init(void);
196void __cold btrfs_delayed_ref_exit(void);
197
198static inline struct btrfs_delayed_extent_op *
199btrfs_alloc_delayed_extent_op(void)
200{
201 return kmem_cache_alloc(btrfs_delayed_extent_op_cachep, GFP_NOFS);
202}
203
204static inline void
205btrfs_free_delayed_extent_op(struct btrfs_delayed_extent_op *op)
206{
207 if (op)
208 kmem_cache_free(btrfs_delayed_extent_op_cachep, op);
209}
210
211static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref)
212{
213 WARN_ON(refcount_read(&ref->refs) == 0);
214 if (refcount_dec_and_test(&ref->refs)) {
215 WARN_ON(ref->in_tree);
216 switch (ref->type) {
217 case BTRFS_TREE_BLOCK_REF_KEY:
218 case BTRFS_SHARED_BLOCK_REF_KEY:
219 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
220 break;
221 case BTRFS_EXTENT_DATA_REF_KEY:
222 case BTRFS_SHARED_DATA_REF_KEY:
223 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
224 break;
225 default:
226 BUG();
227 }
228 }
229}
230
231static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *head)
232{
233 if (refcount_dec_and_test(&head->refs))
234 kmem_cache_free(btrfs_delayed_ref_head_cachep, head);
235}
236
237int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
238 u64 bytenr, u64 num_bytes, u64 parent,
239 u64 ref_root, int level, int action,
240 struct btrfs_delayed_extent_op *extent_op,
241 int *old_ref_mod, int *new_ref_mod);
242int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
243 u64 bytenr, u64 num_bytes,
244 u64 parent, u64 ref_root,
245 u64 owner, u64 offset, u64 reserved, int action,
246 int *old_ref_mod, int *new_ref_mod);
247int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
248 struct btrfs_trans_handle *trans,
249 u64 bytenr, u64 num_bytes,
250 struct btrfs_delayed_extent_op *extent_op);
251void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
252 struct btrfs_delayed_ref_root *delayed_refs,
253 struct btrfs_delayed_ref_head *head);
254
255struct btrfs_delayed_ref_head *
256btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
257 u64 bytenr);
258int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs,
259 struct btrfs_delayed_ref_head *head);
260static inline void btrfs_delayed_ref_unlock(struct btrfs_delayed_ref_head *head)
261{
262 mutex_unlock(&head->mutex);
263}
264void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
265 struct btrfs_delayed_ref_head *head);
266
267struct btrfs_delayed_ref_head *btrfs_select_ref_head(
268 struct btrfs_delayed_ref_root *delayed_refs);
269
270int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq);
271
272
273
274
275static inline struct btrfs_delayed_tree_ref *
276btrfs_delayed_node_to_tree_ref(struct btrfs_delayed_ref_node *node)
277{
278 return container_of(node, struct btrfs_delayed_tree_ref, node);
279}
280
281static inline struct btrfs_delayed_data_ref *
282btrfs_delayed_node_to_data_ref(struct btrfs_delayed_ref_node *node)
283{
284 return container_of(node, struct btrfs_delayed_data_ref, node);
285}
286
287#endif
288