Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
56bec294 CM |
2 | /* |
3 | * Copyright (C) 2009 Oracle. All rights reserved. | |
56bec294 CM |
4 | */ |
5 | ||
6 | #include <linux/sched.h> | |
5a0e3ad6 | 7 | #include <linux/slab.h> |
56bec294 | 8 | #include <linux/sort.h> |
9b569ea0 | 9 | #include "messages.h" |
56bec294 CM |
10 | #include "ctree.h" |
11 | #include "delayed-ref.h" | |
12 | #include "transaction.h" | |
3368d001 | 13 | #include "qgroup.h" |
6ef03deb | 14 | #include "space-info.h" |
f3a84ccd | 15 | #include "tree-mod-log.h" |
fc97a410 | 16 | #include "fs.h" |
56bec294 | 17 | |
78a6184a MX |
18 | struct kmem_cache *btrfs_delayed_ref_head_cachep; |
19 | struct kmem_cache *btrfs_delayed_tree_ref_cachep; | |
20 | struct kmem_cache *btrfs_delayed_data_ref_cachep; | |
21 | struct kmem_cache *btrfs_delayed_extent_op_cachep; | |
56bec294 CM |
22 | /* |
23 | * delayed back reference update tracking. For subvolume trees | |
24 | * we queue up extent allocations and backref maintenance for | |
25 | * delayed processing. This avoids deep call chains where we | |
26 | * add extents in the middle of btrfs_search_slot, and it allows | |
27 | * us to buffer up frequently modified backrefs in an rb tree instead | |
28 | * of hammering updates on the extent allocation tree. | |
56bec294 CM |
29 | */ |
30 | ||
6ef03deb JB |
31 | bool btrfs_check_space_for_delayed_refs(struct btrfs_fs_info *fs_info) |
32 | { | |
33 | struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; | |
34 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
35 | bool ret = false; | |
36 | u64 reserved; | |
37 | ||
38 | spin_lock(&global_rsv->lock); | |
39 | reserved = global_rsv->reserved; | |
40 | spin_unlock(&global_rsv->lock); | |
41 | ||
42 | /* | |
43 | * Since the global reserve is just kind of magic we don't really want | |
44 | * to rely on it to save our bacon, so if our size is more than the | |
45 | * delayed_refs_rsv and the global rsv then it's time to think about | |
46 | * bailing. | |
47 | */ | |
48 | spin_lock(&delayed_refs_rsv->lock); | |
49 | reserved += delayed_refs_rsv->reserved; | |
50 | if (delayed_refs_rsv->size >= reserved) | |
51 | ret = true; | |
52 | spin_unlock(&delayed_refs_rsv->lock); | |
53 | return ret; | |
54 | } | |
55 | ||
43dd529a DS |
56 | /* |
57 | * Release a ref head's reservation. | |
696eb22b NB |
58 | * |
59 | * @fs_info: the filesystem | |
60 | * @nr: number of items to drop | |
6ef03deb | 61 | * |
43dd529a DS |
62 | * Drops the delayed ref head's count from the delayed refs rsv and free any |
63 | * excess reservation we had. | |
6ef03deb JB |
64 | */ |
65 | void btrfs_delayed_refs_rsv_release(struct btrfs_fs_info *fs_info, int nr) | |
66 | { | |
67 | struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv; | |
0e55a545 | 68 | const u64 num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, nr); |
6ef03deb JB |
69 | u64 released = 0; |
70 | ||
63f018be | 71 | released = btrfs_block_rsv_release(fs_info, block_rsv, num_bytes, NULL); |
6ef03deb JB |
72 | if (released) |
73 | trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", | |
74 | 0, released, 0); | |
75 | } | |
76 | ||
77 | /* | |
43dd529a | 78 | * Adjust the size of the delayed refs rsv. |
6ef03deb JB |
79 | * |
80 | * This is to be called anytime we may have adjusted trans->delayed_ref_updates, | |
81 | * it'll calculate the additional size and add it to the delayed_refs_rsv. | |
82 | */ | |
83 | void btrfs_update_delayed_refs_rsv(struct btrfs_trans_handle *trans) | |
84 | { | |
85 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
86 | struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv; | |
87 | u64 num_bytes; | |
88 | ||
89 | if (!trans->delayed_ref_updates) | |
90 | return; | |
91 | ||
0e55a545 FM |
92 | num_bytes = btrfs_calc_delayed_ref_bytes(fs_info, |
93 | trans->delayed_ref_updates); | |
c18e3235 | 94 | |
6ef03deb JB |
95 | spin_lock(&delayed_rsv->lock); |
96 | delayed_rsv->size += num_bytes; | |
c70c2c5b | 97 | delayed_rsv->full = false; |
6ef03deb JB |
98 | spin_unlock(&delayed_rsv->lock); |
99 | trans->delayed_ref_updates = 0; | |
100 | } | |
101 | ||
43dd529a DS |
102 | /* |
103 | * Transfer bytes to our delayed refs rsv. | |
696eb22b NB |
104 | * |
105 | * @fs_info: the filesystem | |
106 | * @src: source block rsv to transfer from | |
107 | * @num_bytes: number of bytes to transfer | |
6ef03deb JB |
108 | * |
109 | * This transfers up to the num_bytes amount from the src rsv to the | |
110 | * delayed_refs_rsv. Any extra bytes are returned to the space info. | |
111 | */ | |
112 | void btrfs_migrate_to_delayed_refs_rsv(struct btrfs_fs_info *fs_info, | |
113 | struct btrfs_block_rsv *src, | |
114 | u64 num_bytes) | |
115 | { | |
116 | struct btrfs_block_rsv *delayed_refs_rsv = &fs_info->delayed_refs_rsv; | |
117 | u64 to_free = 0; | |
118 | ||
119 | spin_lock(&src->lock); | |
120 | src->reserved -= num_bytes; | |
121 | src->size -= num_bytes; | |
122 | spin_unlock(&src->lock); | |
123 | ||
124 | spin_lock(&delayed_refs_rsv->lock); | |
125 | if (delayed_refs_rsv->size > delayed_refs_rsv->reserved) { | |
126 | u64 delta = delayed_refs_rsv->size - | |
127 | delayed_refs_rsv->reserved; | |
128 | if (num_bytes > delta) { | |
129 | to_free = num_bytes - delta; | |
130 | num_bytes = delta; | |
131 | } | |
132 | } else { | |
133 | to_free = num_bytes; | |
134 | num_bytes = 0; | |
135 | } | |
136 | ||
137 | if (num_bytes) | |
138 | delayed_refs_rsv->reserved += num_bytes; | |
139 | if (delayed_refs_rsv->reserved >= delayed_refs_rsv->size) | |
c70c2c5b | 140 | delayed_refs_rsv->full = true; |
6ef03deb JB |
141 | spin_unlock(&delayed_refs_rsv->lock); |
142 | ||
143 | if (num_bytes) | |
144 | trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", | |
145 | 0, num_bytes, 1); | |
146 | if (to_free) | |
d05e4649 | 147 | btrfs_space_info_free_bytes_may_use(fs_info, |
6ef03deb JB |
148 | delayed_refs_rsv->space_info, to_free); |
149 | } | |
150 | ||
43dd529a DS |
151 | /* |
152 | * Refill based on our delayed refs usage. | |
696eb22b NB |
153 | * |
154 | * @fs_info: the filesystem | |
155 | * @flush: control how we can flush for this reservation. | |
6ef03deb JB |
156 | * |
157 | * This will refill the delayed block_rsv up to 1 items size worth of space and | |
158 | * will return -ENOSPC if we can't make the reservation. | |
159 | */ | |
160 | int btrfs_delayed_refs_rsv_refill(struct btrfs_fs_info *fs_info, | |
161 | enum btrfs_reserve_flush_enum flush) | |
162 | { | |
163 | struct btrfs_block_rsv *block_rsv = &fs_info->delayed_refs_rsv; | |
0e55a545 | 164 | u64 limit = btrfs_calc_delayed_ref_bytes(fs_info, 1); |
6ef03deb JB |
165 | u64 num_bytes = 0; |
166 | int ret = -ENOSPC; | |
167 | ||
168 | spin_lock(&block_rsv->lock); | |
169 | if (block_rsv->reserved < block_rsv->size) { | |
170 | num_bytes = block_rsv->size - block_rsv->reserved; | |
171 | num_bytes = min(num_bytes, limit); | |
172 | } | |
173 | spin_unlock(&block_rsv->lock); | |
174 | ||
175 | if (!num_bytes) | |
176 | return 0; | |
177 | ||
9270501c | 178 | ret = btrfs_reserve_metadata_bytes(fs_info, block_rsv, num_bytes, flush); |
6ef03deb JB |
179 | if (ret) |
180 | return ret; | |
5c1f2c6b | 181 | btrfs_block_rsv_add_bytes(block_rsv, num_bytes, false); |
6ef03deb JB |
182 | trace_btrfs_space_reservation(fs_info, "delayed_refs_rsv", |
183 | 0, num_bytes, 1); | |
184 | return 0; | |
185 | } | |
186 | ||
56bec294 | 187 | /* |
5d4f98a2 YZ |
188 | * compare two delayed tree backrefs with same bytenr and type |
189 | */ | |
c7ad7c84 JB |
190 | static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1, |
191 | struct btrfs_delayed_tree_ref *ref2) | |
5d4f98a2 | 192 | { |
3b60d436 | 193 | if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) { |
41b0fc42 JB |
194 | if (ref1->root < ref2->root) |
195 | return -1; | |
196 | if (ref1->root > ref2->root) | |
197 | return 1; | |
198 | } else { | |
199 | if (ref1->parent < ref2->parent) | |
200 | return -1; | |
201 | if (ref1->parent > ref2->parent) | |
202 | return 1; | |
203 | } | |
5d4f98a2 YZ |
204 | return 0; |
205 | } | |
206 | ||
207 | /* | |
208 | * compare two delayed data backrefs with same bytenr and type | |
56bec294 | 209 | */ |
c7ad7c84 JB |
210 | static int comp_data_refs(struct btrfs_delayed_data_ref *ref1, |
211 | struct btrfs_delayed_data_ref *ref2) | |
56bec294 | 212 | { |
5d4f98a2 YZ |
213 | if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) { |
214 | if (ref1->root < ref2->root) | |
215 | return -1; | |
216 | if (ref1->root > ref2->root) | |
217 | return 1; | |
218 | if (ref1->objectid < ref2->objectid) | |
219 | return -1; | |
220 | if (ref1->objectid > ref2->objectid) | |
221 | return 1; | |
222 | if (ref1->offset < ref2->offset) | |
223 | return -1; | |
224 | if (ref1->offset > ref2->offset) | |
225 | return 1; | |
226 | } else { | |
227 | if (ref1->parent < ref2->parent) | |
228 | return -1; | |
229 | if (ref1->parent > ref2->parent) | |
230 | return 1; | |
231 | } | |
232 | return 0; | |
233 | } | |
234 | ||
1d148e59 JB |
235 | static int comp_refs(struct btrfs_delayed_ref_node *ref1, |
236 | struct btrfs_delayed_ref_node *ref2, | |
237 | bool check_seq) | |
238 | { | |
239 | int ret = 0; | |
240 | ||
241 | if (ref1->type < ref2->type) | |
242 | return -1; | |
243 | if (ref1->type > ref2->type) | |
244 | return 1; | |
245 | if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY || | |
246 | ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) | |
247 | ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1), | |
248 | btrfs_delayed_node_to_tree_ref(ref2)); | |
249 | else | |
250 | ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1), | |
251 | btrfs_delayed_node_to_data_ref(ref2)); | |
252 | if (ret) | |
253 | return ret; | |
254 | if (check_seq) { | |
255 | if (ref1->seq < ref2->seq) | |
256 | return -1; | |
257 | if (ref1->seq > ref2->seq) | |
258 | return 1; | |
259 | } | |
260 | return 0; | |
261 | } | |
262 | ||
c46effa6 | 263 | /* insert a new ref to head ref rbtree */ |
5c9d028b | 264 | static struct btrfs_delayed_ref_head *htree_insert(struct rb_root_cached *root, |
c46effa6 LB |
265 | struct rb_node *node) |
266 | { | |
5c9d028b | 267 | struct rb_node **p = &root->rb_root.rb_node; |
c46effa6 LB |
268 | struct rb_node *parent_node = NULL; |
269 | struct btrfs_delayed_ref_head *entry; | |
270 | struct btrfs_delayed_ref_head *ins; | |
271 | u64 bytenr; | |
5c9d028b | 272 | bool leftmost = true; |
c46effa6 LB |
273 | |
274 | ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node); | |
d278850e | 275 | bytenr = ins->bytenr; |
c46effa6 LB |
276 | while (*p) { |
277 | parent_node = *p; | |
278 | entry = rb_entry(parent_node, struct btrfs_delayed_ref_head, | |
279 | href_node); | |
280 | ||
5c9d028b | 281 | if (bytenr < entry->bytenr) { |
c46effa6 | 282 | p = &(*p)->rb_left; |
5c9d028b | 283 | } else if (bytenr > entry->bytenr) { |
c46effa6 | 284 | p = &(*p)->rb_right; |
5c9d028b LB |
285 | leftmost = false; |
286 | } else { | |
c46effa6 | 287 | return entry; |
5c9d028b | 288 | } |
c46effa6 LB |
289 | } |
290 | ||
291 | rb_link_node(node, parent_node, p); | |
5c9d028b | 292 | rb_insert_color_cached(node, root, leftmost); |
c46effa6 LB |
293 | return NULL; |
294 | } | |
295 | ||
e3d03965 | 296 | static struct btrfs_delayed_ref_node* tree_insert(struct rb_root_cached *root, |
0e0adbcf JB |
297 | struct btrfs_delayed_ref_node *ins) |
298 | { | |
e3d03965 | 299 | struct rb_node **p = &root->rb_root.rb_node; |
0e0adbcf JB |
300 | struct rb_node *node = &ins->ref_node; |
301 | struct rb_node *parent_node = NULL; | |
302 | struct btrfs_delayed_ref_node *entry; | |
e3d03965 | 303 | bool leftmost = true; |
0e0adbcf JB |
304 | |
305 | while (*p) { | |
306 | int comp; | |
307 | ||
308 | parent_node = *p; | |
309 | entry = rb_entry(parent_node, struct btrfs_delayed_ref_node, | |
310 | ref_node); | |
311 | comp = comp_refs(ins, entry, true); | |
e3d03965 | 312 | if (comp < 0) { |
0e0adbcf | 313 | p = &(*p)->rb_left; |
e3d03965 | 314 | } else if (comp > 0) { |
0e0adbcf | 315 | p = &(*p)->rb_right; |
e3d03965 LB |
316 | leftmost = false; |
317 | } else { | |
0e0adbcf | 318 | return entry; |
e3d03965 | 319 | } |
0e0adbcf JB |
320 | } |
321 | ||
322 | rb_link_node(node, parent_node, p); | |
e3d03965 | 323 | rb_insert_color_cached(node, root, leftmost); |
0e0adbcf JB |
324 | return NULL; |
325 | } | |
326 | ||
0a9df0df LF |
327 | static struct btrfs_delayed_ref_head *find_first_ref_head( |
328 | struct btrfs_delayed_ref_root *dr) | |
329 | { | |
330 | struct rb_node *n; | |
331 | struct btrfs_delayed_ref_head *entry; | |
332 | ||
333 | n = rb_first_cached(&dr->href_root); | |
334 | if (!n) | |
335 | return NULL; | |
336 | ||
337 | entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node); | |
338 | ||
339 | return entry; | |
340 | } | |
341 | ||
56bec294 | 342 | /* |
0a9df0df LF |
343 | * Find a head entry based on bytenr. This returns the delayed ref head if it |
344 | * was able to find one, or NULL if nothing was in that spot. If return_bigger | |
345 | * is given, the next bigger entry is returned if no exact match is found. | |
56bec294 | 346 | */ |
0a9df0df | 347 | static struct btrfs_delayed_ref_head *find_ref_head( |
5c9d028b | 348 | struct btrfs_delayed_ref_root *dr, u64 bytenr, |
d9352794 | 349 | bool return_bigger) |
56bec294 | 350 | { |
5c9d028b | 351 | struct rb_root *root = &dr->href_root.rb_root; |
d1270cd9 | 352 | struct rb_node *n; |
c46effa6 | 353 | struct btrfs_delayed_ref_head *entry; |
56bec294 | 354 | |
d1270cd9 AJ |
355 | n = root->rb_node; |
356 | entry = NULL; | |
56bec294 | 357 | while (n) { |
c46effa6 | 358 | entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node); |
56bec294 | 359 | |
d278850e | 360 | if (bytenr < entry->bytenr) |
56bec294 | 361 | n = n->rb_left; |
d278850e | 362 | else if (bytenr > entry->bytenr) |
56bec294 CM |
363 | n = n->rb_right; |
364 | else | |
365 | return entry; | |
366 | } | |
d1270cd9 | 367 | if (entry && return_bigger) { |
d278850e | 368 | if (bytenr > entry->bytenr) { |
c46effa6 | 369 | n = rb_next(&entry->href_node); |
d1270cd9 | 370 | if (!n) |
0a9df0df | 371 | return NULL; |
c46effa6 LB |
372 | entry = rb_entry(n, struct btrfs_delayed_ref_head, |
373 | href_node); | |
d1270cd9 AJ |
374 | } |
375 | return entry; | |
376 | } | |
56bec294 CM |
377 | return NULL; |
378 | } | |
379 | ||
9e920a6f | 380 | int btrfs_delayed_ref_lock(struct btrfs_delayed_ref_root *delayed_refs, |
c3e69d58 | 381 | struct btrfs_delayed_ref_head *head) |
56bec294 | 382 | { |
a4666e68 | 383 | lockdep_assert_held(&delayed_refs->lock); |
c3e69d58 CM |
384 | if (mutex_trylock(&head->mutex)) |
385 | return 0; | |
386 | ||
d278850e | 387 | refcount_inc(&head->refs); |
c3e69d58 CM |
388 | spin_unlock(&delayed_refs->lock); |
389 | ||
390 | mutex_lock(&head->mutex); | |
391 | spin_lock(&delayed_refs->lock); | |
d278850e | 392 | if (RB_EMPTY_NODE(&head->href_node)) { |
c3e69d58 | 393 | mutex_unlock(&head->mutex); |
d278850e | 394 | btrfs_put_delayed_ref_head(head); |
c3e69d58 CM |
395 | return -EAGAIN; |
396 | } | |
d278850e | 397 | btrfs_put_delayed_ref_head(head); |
c3e69d58 CM |
398 | return 0; |
399 | } | |
400 | ||
4c89493f | 401 | static inline void drop_delayed_ref(struct btrfs_delayed_ref_root *delayed_refs, |
d7df2c79 | 402 | struct btrfs_delayed_ref_head *head, |
ae1e206b JB |
403 | struct btrfs_delayed_ref_node *ref) |
404 | { | |
a4666e68 | 405 | lockdep_assert_held(&head->lock); |
e3d03965 | 406 | rb_erase_cached(&ref->ref_node, &head->ref_tree); |
0e0adbcf | 407 | RB_CLEAR_NODE(&ref->ref_node); |
d278850e JB |
408 | if (!list_empty(&ref->add_list)) |
409 | list_del(&ref->add_list); | |
ae1e206b JB |
410 | ref->in_tree = 0; |
411 | btrfs_put_delayed_ref(ref); | |
d7df2c79 | 412 | atomic_dec(&delayed_refs->num_entries); |
ae1e206b JB |
413 | } |
414 | ||
f09f7851 | 415 | static bool merge_ref(struct btrfs_delayed_ref_root *delayed_refs, |
2c3cf7d5 FM |
416 | struct btrfs_delayed_ref_head *head, |
417 | struct btrfs_delayed_ref_node *ref, | |
418 | u64 seq) | |
419 | { | |
420 | struct btrfs_delayed_ref_node *next; | |
0e0adbcf | 421 | struct rb_node *node = rb_next(&ref->ref_node); |
2c3cf7d5 FM |
422 | bool done = false; |
423 | ||
0e0adbcf | 424 | while (!done && node) { |
2c3cf7d5 | 425 | int mod; |
2c3cf7d5 | 426 | |
0e0adbcf JB |
427 | next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
428 | node = rb_next(node); | |
2c3cf7d5 | 429 | if (seq && next->seq >= seq) |
0e0adbcf | 430 | break; |
1d148e59 | 431 | if (comp_refs(ref, next, false)) |
0e0adbcf | 432 | break; |
2c3cf7d5 FM |
433 | |
434 | if (ref->action == next->action) { | |
435 | mod = next->ref_mod; | |
436 | } else { | |
437 | if (ref->ref_mod < next->ref_mod) { | |
438 | swap(ref, next); | |
439 | done = true; | |
440 | } | |
441 | mod = -next->ref_mod; | |
442 | } | |
443 | ||
4c89493f | 444 | drop_delayed_ref(delayed_refs, head, next); |
2c3cf7d5 FM |
445 | ref->ref_mod += mod; |
446 | if (ref->ref_mod == 0) { | |
4c89493f | 447 | drop_delayed_ref(delayed_refs, head, ref); |
2c3cf7d5 FM |
448 | done = true; |
449 | } else { | |
450 | /* | |
451 | * Can't have multiples of the same ref on a tree block. | |
452 | */ | |
453 | WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY || | |
454 | ref->type == BTRFS_SHARED_BLOCK_REF_KEY); | |
455 | } | |
2c3cf7d5 FM |
456 | } |
457 | ||
458 | return done; | |
459 | } | |
460 | ||
0c555c97 | 461 | void btrfs_merge_delayed_refs(struct btrfs_fs_info *fs_info, |
2c3cf7d5 FM |
462 | struct btrfs_delayed_ref_root *delayed_refs, |
463 | struct btrfs_delayed_ref_head *head) | |
464 | { | |
465 | struct btrfs_delayed_ref_node *ref; | |
0e0adbcf | 466 | struct rb_node *node; |
2c3cf7d5 FM |
467 | u64 seq = 0; |
468 | ||
a4666e68 | 469 | lockdep_assert_held(&head->lock); |
2c3cf7d5 | 470 | |
e3d03965 | 471 | if (RB_EMPTY_ROOT(&head->ref_tree.rb_root)) |
2c3cf7d5 FM |
472 | return; |
473 | ||
474 | /* We don't have too many refs to merge for data. */ | |
475 | if (head->is_data) | |
476 | return; | |
477 | ||
4bae7880 | 478 | seq = btrfs_tree_mod_log_lowest_seq(fs_info); |
0e0adbcf | 479 | again: |
e3d03965 LB |
480 | for (node = rb_first_cached(&head->ref_tree); node; |
481 | node = rb_next(node)) { | |
0e0adbcf | 482 | ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node); |
2c3cf7d5 | 483 | if (seq && ref->seq >= seq) |
2c3cf7d5 | 484 | continue; |
f09f7851 | 485 | if (merge_ref(delayed_refs, head, ref, seq)) |
0e0adbcf | 486 | goto again; |
2c3cf7d5 FM |
487 | } |
488 | } | |
489 | ||
41d0bd3b | 490 | int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, u64 seq) |
00f04b88 | 491 | { |
097b8a7c | 492 | int ret = 0; |
4bae7880 FM |
493 | u64 min_seq = btrfs_tree_mod_log_lowest_seq(fs_info); |
494 | ||
495 | if (min_seq != 0 && seq >= min_seq) { | |
496 | btrfs_debug(fs_info, | |
ffbc10a1 FM |
497 | "holding back delayed_ref %llu, lowest is %llu", |
498 | seq, min_seq); | |
4bae7880 | 499 | ret = 1; |
00f04b88 | 500 | } |
097b8a7c | 501 | |
097b8a7c | 502 | return ret; |
00f04b88 AJ |
503 | } |
504 | ||
5637c74b LF |
505 | struct btrfs_delayed_ref_head *btrfs_select_ref_head( |
506 | struct btrfs_delayed_ref_root *delayed_refs) | |
c3e69d58 | 507 | { |
d7df2c79 | 508 | struct btrfs_delayed_ref_head *head; |
56bec294 | 509 | |
c3e69d58 | 510 | again: |
0a9df0df LF |
511 | head = find_ref_head(delayed_refs, delayed_refs->run_delayed_start, |
512 | true); | |
513 | if (!head && delayed_refs->run_delayed_start != 0) { | |
d7df2c79 | 514 | delayed_refs->run_delayed_start = 0; |
0a9df0df | 515 | head = find_first_ref_head(delayed_refs); |
c3e69d58 | 516 | } |
0a9df0df LF |
517 | if (!head) |
518 | return NULL; | |
56bec294 | 519 | |
d7df2c79 JB |
520 | while (head->processing) { |
521 | struct rb_node *node; | |
522 | ||
523 | node = rb_next(&head->href_node); | |
524 | if (!node) { | |
0a9df0df | 525 | if (delayed_refs->run_delayed_start == 0) |
d7df2c79 JB |
526 | return NULL; |
527 | delayed_refs->run_delayed_start = 0; | |
d7df2c79 JB |
528 | goto again; |
529 | } | |
530 | head = rb_entry(node, struct btrfs_delayed_ref_head, | |
531 | href_node); | |
532 | } | |
093486c4 | 533 | |
d7df2c79 JB |
534 | head->processing = 1; |
535 | WARN_ON(delayed_refs->num_heads_ready == 0); | |
536 | delayed_refs->num_heads_ready--; | |
d278850e JB |
537 | delayed_refs->run_delayed_start = head->bytenr + |
538 | head->num_bytes; | |
d7df2c79 | 539 | return head; |
093486c4 MX |
540 | } |
541 | ||
d7baffda JB |
542 | void btrfs_delete_ref_head(struct btrfs_delayed_ref_root *delayed_refs, |
543 | struct btrfs_delayed_ref_head *head) | |
544 | { | |
545 | lockdep_assert_held(&delayed_refs->lock); | |
546 | lockdep_assert_held(&head->lock); | |
547 | ||
548 | rb_erase_cached(&head->href_node, &delayed_refs->href_root); | |
549 | RB_CLEAR_NODE(&head->href_node); | |
550 | atomic_dec(&delayed_refs->num_entries); | |
551 | delayed_refs->num_heads--; | |
552 | if (head->processing == 0) | |
553 | delayed_refs->num_heads_ready--; | |
554 | } | |
555 | ||
c6fc2454 QW |
556 | /* |
557 | * Helper to insert the ref_node to the tail or merge with tail. | |
558 | * | |
559 | * Return 0 for insert. | |
560 | * Return >0 for merge. | |
561 | */ | |
afe2d748 | 562 | static int insert_delayed_ref(struct btrfs_delayed_ref_root *root, |
0e0adbcf JB |
563 | struct btrfs_delayed_ref_head *href, |
564 | struct btrfs_delayed_ref_node *ref) | |
c6fc2454 QW |
565 | { |
566 | struct btrfs_delayed_ref_node *exist; | |
567 | int mod; | |
568 | int ret = 0; | |
569 | ||
570 | spin_lock(&href->lock); | |
0e0adbcf JB |
571 | exist = tree_insert(&href->ref_tree, ref); |
572 | if (!exist) | |
573 | goto inserted; | |
c6fc2454 QW |
574 | |
575 | /* Now we are sure we can merge */ | |
576 | ret = 1; | |
577 | if (exist->action == ref->action) { | |
578 | mod = ref->ref_mod; | |
579 | } else { | |
580 | /* Need to change action */ | |
581 | if (exist->ref_mod < ref->ref_mod) { | |
582 | exist->action = ref->action; | |
583 | mod = -exist->ref_mod; | |
584 | exist->ref_mod = ref->ref_mod; | |
1d57ee94 WX |
585 | if (ref->action == BTRFS_ADD_DELAYED_REF) |
586 | list_add_tail(&exist->add_list, | |
587 | &href->ref_add_list); | |
588 | else if (ref->action == BTRFS_DROP_DELAYED_REF) { | |
589 | ASSERT(!list_empty(&exist->add_list)); | |
590 | list_del(&exist->add_list); | |
591 | } else { | |
592 | ASSERT(0); | |
593 | } | |
c6fc2454 QW |
594 | } else |
595 | mod = -ref->ref_mod; | |
596 | } | |
597 | exist->ref_mod += mod; | |
598 | ||
599 | /* remove existing tail if its ref_mod is zero */ | |
600 | if (exist->ref_mod == 0) | |
4c89493f | 601 | drop_delayed_ref(root, href, exist); |
c6fc2454 QW |
602 | spin_unlock(&href->lock); |
603 | return ret; | |
0e0adbcf | 604 | inserted: |
1d57ee94 WX |
605 | if (ref->action == BTRFS_ADD_DELAYED_REF) |
606 | list_add_tail(&ref->add_list, &href->ref_add_list); | |
c6fc2454 | 607 | atomic_inc(&root->num_entries); |
c6fc2454 QW |
608 | spin_unlock(&href->lock); |
609 | return ret; | |
610 | } | |
611 | ||
56bec294 CM |
612 | /* |
613 | * helper function to update the accounting in the head ref | |
614 | * existing and update must have the same bytenr | |
615 | */ | |
ba2c4d4e | 616 | static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans, |
d278850e | 617 | struct btrfs_delayed_ref_head *existing, |
2187374f | 618 | struct btrfs_delayed_ref_head *update) |
56bec294 | 619 | { |
ba2c4d4e JB |
620 | struct btrfs_delayed_ref_root *delayed_refs = |
621 | &trans->transaction->delayed_refs; | |
622 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
1262133b | 623 | int old_ref_mod; |
56bec294 | 624 | |
d278850e | 625 | BUG_ON(existing->is_data != update->is_data); |
56bec294 | 626 | |
d278850e JB |
627 | spin_lock(&existing->lock); |
628 | if (update->must_insert_reserved) { | |
56bec294 CM |
629 | /* if the extent was freed and then |
630 | * reallocated before the delayed ref | |
631 | * entries were processed, we can end up | |
632 | * with an existing head ref without | |
633 | * the must_insert_reserved flag set. | |
634 | * Set it again here | |
635 | */ | |
d278850e | 636 | existing->must_insert_reserved = update->must_insert_reserved; |
56bec294 CM |
637 | |
638 | /* | |
639 | * update the num_bytes so we make sure the accounting | |
640 | * is done correctly | |
641 | */ | |
642 | existing->num_bytes = update->num_bytes; | |
643 | ||
644 | } | |
645 | ||
d278850e JB |
646 | if (update->extent_op) { |
647 | if (!existing->extent_op) { | |
648 | existing->extent_op = update->extent_op; | |
5d4f98a2 | 649 | } else { |
d278850e JB |
650 | if (update->extent_op->update_key) { |
651 | memcpy(&existing->extent_op->key, | |
652 | &update->extent_op->key, | |
653 | sizeof(update->extent_op->key)); | |
654 | existing->extent_op->update_key = true; | |
5d4f98a2 | 655 | } |
d278850e JB |
656 | if (update->extent_op->update_flags) { |
657 | existing->extent_op->flags_to_set |= | |
658 | update->extent_op->flags_to_set; | |
659 | existing->extent_op->update_flags = true; | |
5d4f98a2 | 660 | } |
d278850e | 661 | btrfs_free_delayed_extent_op(update->extent_op); |
5d4f98a2 YZ |
662 | } |
663 | } | |
56bec294 | 664 | /* |
d7df2c79 JB |
665 | * update the reference mod on the head to reflect this new operation, |
666 | * only need the lock for this case cause we could be processing it | |
667 | * currently, for refs we just added we know we're a-ok. | |
56bec294 | 668 | */ |
d278850e | 669 | old_ref_mod = existing->total_ref_mod; |
56bec294 | 670 | existing->ref_mod += update->ref_mod; |
d278850e | 671 | existing->total_ref_mod += update->ref_mod; |
1262133b JB |
672 | |
673 | /* | |
674 | * If we are going to from a positive ref mod to a negative or vice | |
675 | * versa we need to make sure to adjust pending_csums accordingly. | |
676 | */ | |
d278850e | 677 | if (existing->is_data) { |
ba2c4d4e JB |
678 | u64 csum_leaves = |
679 | btrfs_csum_bytes_to_leaves(fs_info, | |
680 | existing->num_bytes); | |
681 | ||
682 | if (existing->total_ref_mod >= 0 && old_ref_mod < 0) { | |
1262133b | 683 | delayed_refs->pending_csums -= existing->num_bytes; |
ba2c4d4e JB |
684 | btrfs_delayed_refs_rsv_release(fs_info, csum_leaves); |
685 | } | |
686 | if (existing->total_ref_mod < 0 && old_ref_mod >= 0) { | |
1262133b | 687 | delayed_refs->pending_csums += existing->num_bytes; |
ba2c4d4e JB |
688 | trans->delayed_ref_updates += csum_leaves; |
689 | } | |
1262133b | 690 | } |
2187374f | 691 | |
d278850e | 692 | spin_unlock(&existing->lock); |
56bec294 CM |
693 | } |
694 | ||
a2e569b3 NB |
695 | static void init_delayed_ref_head(struct btrfs_delayed_ref_head *head_ref, |
696 | struct btrfs_qgroup_extent_record *qrecord, | |
697 | u64 bytenr, u64 num_bytes, u64 ref_root, | |
698 | u64 reserved, int action, bool is_data, | |
699 | bool is_system) | |
700 | { | |
701 | int count_mod = 1; | |
702 | int must_insert_reserved = 0; | |
703 | ||
704 | /* If reserved is provided, it must be a data extent. */ | |
705 | BUG_ON(!is_data && reserved); | |
706 | ||
707 | /* | |
708 | * The head node stores the sum of all the mods, so dropping a ref | |
709 | * should drop the sum in the head node by one. | |
710 | */ | |
711 | if (action == BTRFS_UPDATE_DELAYED_HEAD) | |
712 | count_mod = 0; | |
713 | else if (action == BTRFS_DROP_DELAYED_REF) | |
714 | count_mod = -1; | |
715 | ||
716 | /* | |
717 | * BTRFS_ADD_DELAYED_EXTENT means that we need to update the reserved | |
718 | * accounting when the extent is finally added, or if a later | |
719 | * modification deletes the delayed ref without ever inserting the | |
720 | * extent into the extent allocation tree. ref->must_insert_reserved | |
721 | * is the flag used to record that accounting mods are required. | |
722 | * | |
723 | * Once we record must_insert_reserved, switch the action to | |
724 | * BTRFS_ADD_DELAYED_REF because other special casing is not required. | |
725 | */ | |
726 | if (action == BTRFS_ADD_DELAYED_EXTENT) | |
727 | must_insert_reserved = 1; | |
728 | else | |
729 | must_insert_reserved = 0; | |
730 | ||
731 | refcount_set(&head_ref->refs, 1); | |
732 | head_ref->bytenr = bytenr; | |
733 | head_ref->num_bytes = num_bytes; | |
734 | head_ref->ref_mod = count_mod; | |
735 | head_ref->must_insert_reserved = must_insert_reserved; | |
736 | head_ref->is_data = is_data; | |
737 | head_ref->is_system = is_system; | |
e3d03965 | 738 | head_ref->ref_tree = RB_ROOT_CACHED; |
a2e569b3 NB |
739 | INIT_LIST_HEAD(&head_ref->ref_add_list); |
740 | RB_CLEAR_NODE(&head_ref->href_node); | |
741 | head_ref->processing = 0; | |
742 | head_ref->total_ref_mod = count_mod; | |
a2e569b3 NB |
743 | spin_lock_init(&head_ref->lock); |
744 | mutex_init(&head_ref->mutex); | |
745 | ||
746 | if (qrecord) { | |
747 | if (ref_root && reserved) { | |
1418bae1 QW |
748 | qrecord->data_rsv = reserved; |
749 | qrecord->data_rsv_refroot = ref_root; | |
a2e569b3 | 750 | } |
a2e569b3 NB |
751 | qrecord->bytenr = bytenr; |
752 | qrecord->num_bytes = num_bytes; | |
753 | qrecord->old_roots = NULL; | |
754 | } | |
755 | } | |
756 | ||
56bec294 | 757 | /* |
5d4f98a2 | 758 | * helper function to actually insert a head node into the rbtree. |
56bec294 | 759 | * this does all the dirty work in terms of maintaining the correct |
5d4f98a2 | 760 | * overall modification count. |
56bec294 | 761 | */ |
d7df2c79 | 762 | static noinline struct btrfs_delayed_ref_head * |
1acda0c2 | 763 | add_delayed_ref_head(struct btrfs_trans_handle *trans, |
d278850e | 764 | struct btrfs_delayed_ref_head *head_ref, |
3368d001 | 765 | struct btrfs_qgroup_extent_record *qrecord, |
2187374f | 766 | int action, int *qrecord_inserted_ret) |
56bec294 | 767 | { |
d7df2c79 | 768 | struct btrfs_delayed_ref_head *existing; |
56bec294 | 769 | struct btrfs_delayed_ref_root *delayed_refs; |
fb235dc0 | 770 | int qrecord_inserted = 0; |
56bec294 | 771 | |
56bec294 | 772 | delayed_refs = &trans->transaction->delayed_refs; |
2335efaf | 773 | |
3368d001 QW |
774 | /* Record qgroup extent info if provided */ |
775 | if (qrecord) { | |
eb86ec73 | 776 | if (btrfs_qgroup_trace_extent_nolock(trans->fs_info, |
cb93b52c | 777 | delayed_refs, qrecord)) |
3368d001 | 778 | kfree(qrecord); |
fb235dc0 QW |
779 | else |
780 | qrecord_inserted = 1; | |
3368d001 QW |
781 | } |
782 | ||
1acda0c2 | 783 | trace_add_delayed_ref_head(trans->fs_info, head_ref, action); |
1abe9b8a | 784 | |
d7df2c79 JB |
785 | existing = htree_insert(&delayed_refs->href_root, |
786 | &head_ref->href_node); | |
5d4f98a2 | 787 | if (existing) { |
2187374f | 788 | update_existing_head_ref(trans, existing, head_ref); |
5d4f98a2 YZ |
789 | /* |
790 | * we've updated the existing ref, free the newly | |
791 | * allocated ref | |
792 | */ | |
78a6184a | 793 | kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref); |
d7df2c79 | 794 | head_ref = existing; |
5d4f98a2 | 795 | } else { |
ba2c4d4e | 796 | if (head_ref->is_data && head_ref->ref_mod < 0) { |
2335efaf | 797 | delayed_refs->pending_csums += head_ref->num_bytes; |
ba2c4d4e JB |
798 | trans->delayed_ref_updates += |
799 | btrfs_csum_bytes_to_leaves(trans->fs_info, | |
800 | head_ref->num_bytes); | |
801 | } | |
5d4f98a2 YZ |
802 | delayed_refs->num_heads++; |
803 | delayed_refs->num_heads_ready++; | |
d7df2c79 | 804 | atomic_inc(&delayed_refs->num_entries); |
5d4f98a2 YZ |
805 | trans->delayed_ref_updates++; |
806 | } | |
fb235dc0 QW |
807 | if (qrecord_inserted_ret) |
808 | *qrecord_inserted_ret = qrecord_inserted; | |
2335efaf | 809 | |
d7df2c79 | 810 | return head_ref; |
5d4f98a2 YZ |
811 | } |
812 | ||
cb49a87b NB |
813 | /* |
814 | * init_delayed_ref_common - Initialize the structure which represents a | |
815 | * modification to a an extent. | |
816 | * | |
817 | * @fs_info: Internal to the mounted filesystem mount structure. | |
818 | * | |
819 | * @ref: The structure which is going to be initialized. | |
820 | * | |
821 | * @bytenr: The logical address of the extent for which a modification is | |
822 | * going to be recorded. | |
823 | * | |
824 | * @num_bytes: Size of the extent whose modification is being recorded. | |
825 | * | |
826 | * @ref_root: The id of the root where this modification has originated, this | |
827 | * can be either one of the well-known metadata trees or the | |
828 | * subvolume id which references this extent. | |
829 | * | |
830 | * @action: Can be one of BTRFS_ADD_DELAYED_REF/BTRFS_DROP_DELAYED_REF or | |
831 | * BTRFS_ADD_DELAYED_EXTENT | |
832 | * | |
833 | * @ref_type: Holds the type of the extent which is being recorded, can be | |
834 | * one of BTRFS_SHARED_BLOCK_REF_KEY/BTRFS_TREE_BLOCK_REF_KEY | |
835 | * when recording a metadata extent or BTRFS_SHARED_DATA_REF_KEY/ | |
836 | * BTRFS_EXTENT_DATA_REF_KEY when recording data extent | |
837 | */ | |
838 | static void init_delayed_ref_common(struct btrfs_fs_info *fs_info, | |
839 | struct btrfs_delayed_ref_node *ref, | |
840 | u64 bytenr, u64 num_bytes, u64 ref_root, | |
841 | int action, u8 ref_type) | |
842 | { | |
843 | u64 seq = 0; | |
844 | ||
845 | if (action == BTRFS_ADD_DELAYED_EXTENT) | |
846 | action = BTRFS_ADD_DELAYED_REF; | |
847 | ||
848 | if (is_fstree(ref_root)) | |
849 | seq = atomic64_read(&fs_info->tree_mod_seq); | |
850 | ||
851 | refcount_set(&ref->refs, 1); | |
852 | ref->bytenr = bytenr; | |
853 | ref->num_bytes = num_bytes; | |
854 | ref->ref_mod = 1; | |
855 | ref->action = action; | |
856 | ref->is_head = 0; | |
857 | ref->in_tree = 1; | |
858 | ref->seq = seq; | |
859 | ref->type = ref_type; | |
860 | RB_CLEAR_NODE(&ref->ref_node); | |
861 | INIT_LIST_HEAD(&ref->add_list); | |
862 | } | |
863 | ||
56bec294 | 864 | /* |
5d4f98a2 | 865 | * add a delayed tree ref. This does all of the accounting required |
56bec294 CM |
866 | * to make sure the delayed ref is eventually processed before this |
867 | * transaction commits. | |
868 | */ | |
44e1c47d | 869 | int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans, |
ed4f255b | 870 | struct btrfs_ref *generic_ref, |
2187374f | 871 | struct btrfs_delayed_extent_op *extent_op) |
56bec294 | 872 | { |
44e1c47d | 873 | struct btrfs_fs_info *fs_info = trans->fs_info; |
5d4f98a2 | 874 | struct btrfs_delayed_tree_ref *ref; |
56bec294 CM |
875 | struct btrfs_delayed_ref_head *head_ref; |
876 | struct btrfs_delayed_ref_root *delayed_refs; | |
3368d001 | 877 | struct btrfs_qgroup_extent_record *record = NULL; |
fb235dc0 | 878 | int qrecord_inserted; |
ed4f255b QW |
879 | bool is_system; |
880 | int action = generic_ref->action; | |
881 | int level = generic_ref->tree_ref.level; | |
70d64000 | 882 | int ret; |
ed4f255b QW |
883 | u64 bytenr = generic_ref->bytenr; |
884 | u64 num_bytes = generic_ref->len; | |
885 | u64 parent = generic_ref->parent; | |
70d64000 | 886 | u8 ref_type; |
56bec294 | 887 | |
d55b9e68 | 888 | is_system = (generic_ref->tree_ref.owning_root == BTRFS_CHUNK_TREE_OBJECTID); |
ed4f255b QW |
889 | |
890 | ASSERT(generic_ref->type == BTRFS_REF_METADATA && generic_ref->action); | |
78a6184a | 891 | ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS); |
56bec294 CM |
892 | if (!ref) |
893 | return -ENOMEM; | |
894 | ||
7b4284de NB |
895 | head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS); |
896 | if (!head_ref) { | |
897 | kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); | |
898 | return -ENOMEM; | |
899 | } | |
900 | ||
901 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) && | |
ed4f255b | 902 | !generic_ref->skip_qgroup) { |
1418bae1 | 903 | record = kzalloc(sizeof(*record), GFP_NOFS); |
7b4284de NB |
904 | if (!record) { |
905 | kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); | |
906 | kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref); | |
907 | return -ENOMEM; | |
908 | } | |
909 | } | |
910 | ||
70d64000 NB |
911 | if (parent) |
912 | ref_type = BTRFS_SHARED_BLOCK_REF_KEY; | |
913 | else | |
914 | ref_type = BTRFS_TREE_BLOCK_REF_KEY; | |
7b4284de | 915 | |
70d64000 | 916 | init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes, |
113479d5 NB |
917 | generic_ref->tree_ref.owning_root, action, |
918 | ref_type); | |
919 | ref->root = generic_ref->tree_ref.owning_root; | |
70d64000 NB |
920 | ref->parent = parent; |
921 | ref->level = level; | |
922 | ||
2335efaf | 923 | init_delayed_ref_head(head_ref, record, bytenr, num_bytes, |
113479d5 NB |
924 | generic_ref->tree_ref.owning_root, 0, action, |
925 | false, is_system); | |
5d4f98a2 YZ |
926 | head_ref->extent_op = extent_op; |
927 | ||
928 | delayed_refs = &trans->transaction->delayed_refs; | |
929 | spin_lock(&delayed_refs->lock); | |
930 | ||
56bec294 | 931 | /* |
5d4f98a2 YZ |
932 | * insert both the head node and the new ref without dropping |
933 | * the spin lock | |
56bec294 | 934 | */ |
2335efaf | 935 | head_ref = add_delayed_ref_head(trans, head_ref, record, |
2187374f | 936 | action, &qrecord_inserted); |
5d4f98a2 | 937 | |
afe2d748 | 938 | ret = insert_delayed_ref(delayed_refs, head_ref, &ref->node); |
5d4f98a2 | 939 | spin_unlock(&delayed_refs->lock); |
95a06077 | 940 | |
ba2c4d4e JB |
941 | /* |
942 | * Need to update the delayed_refs_rsv with any changes we may have | |
943 | * made. | |
944 | */ | |
945 | btrfs_update_delayed_refs_rsv(trans); | |
946 | ||
70d64000 NB |
947 | trace_add_delayed_tree_ref(fs_info, &ref->node, ref, |
948 | action == BTRFS_ADD_DELAYED_EXTENT ? | |
949 | BTRFS_ADD_DELAYED_REF : action); | |
950 | if (ret > 0) | |
951 | kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); | |
952 | ||
fb235dc0 | 953 | if (qrecord_inserted) |
8949b9a1 | 954 | btrfs_qgroup_trace_extent_post(trans, record); |
952bd3db | 955 | |
5d4f98a2 YZ |
956 | return 0; |
957 | } | |
958 | ||
959 | /* | |
960 | * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref. | |
961 | */ | |
88a979c6 | 962 | int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans, |
76675593 | 963 | struct btrfs_ref *generic_ref, |
2187374f | 964 | u64 reserved) |
5d4f98a2 | 965 | { |
88a979c6 | 966 | struct btrfs_fs_info *fs_info = trans->fs_info; |
5d4f98a2 YZ |
967 | struct btrfs_delayed_data_ref *ref; |
968 | struct btrfs_delayed_ref_head *head_ref; | |
969 | struct btrfs_delayed_ref_root *delayed_refs; | |
3368d001 | 970 | struct btrfs_qgroup_extent_record *record = NULL; |
fb235dc0 | 971 | int qrecord_inserted; |
76675593 | 972 | int action = generic_ref->action; |
cd7f9699 | 973 | int ret; |
76675593 QW |
974 | u64 bytenr = generic_ref->bytenr; |
975 | u64 num_bytes = generic_ref->len; | |
976 | u64 parent = generic_ref->parent; | |
113479d5 | 977 | u64 ref_root = generic_ref->data_ref.owning_root; |
76675593 QW |
978 | u64 owner = generic_ref->data_ref.ino; |
979 | u64 offset = generic_ref->data_ref.offset; | |
cd7f9699 | 980 | u8 ref_type; |
5d4f98a2 | 981 | |
76675593 | 982 | ASSERT(generic_ref->type == BTRFS_REF_DATA && action); |
78a6184a | 983 | ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS); |
5d4f98a2 YZ |
984 | if (!ref) |
985 | return -ENOMEM; | |
56bec294 | 986 | |
cd7f9699 NB |
987 | if (parent) |
988 | ref_type = BTRFS_SHARED_DATA_REF_KEY; | |
989 | else | |
990 | ref_type = BTRFS_EXTENT_DATA_REF_KEY; | |
991 | init_delayed_ref_common(fs_info, &ref->node, bytenr, num_bytes, | |
992 | ref_root, action, ref_type); | |
993 | ref->root = ref_root; | |
994 | ref->parent = parent; | |
995 | ref->objectid = owner; | |
996 | ref->offset = offset; | |
997 | ||
998 | ||
78a6184a | 999 | head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS); |
56bec294 | 1000 | if (!head_ref) { |
78a6184a | 1001 | kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); |
56bec294 CM |
1002 | return -ENOMEM; |
1003 | } | |
5d4f98a2 | 1004 | |
afcdd129 | 1005 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) && |
76675593 | 1006 | !generic_ref->skip_qgroup) { |
1418bae1 | 1007 | record = kzalloc(sizeof(*record), GFP_NOFS); |
3368d001 QW |
1008 | if (!record) { |
1009 | kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); | |
1010 | kmem_cache_free(btrfs_delayed_ref_head_cachep, | |
1011 | head_ref); | |
1012 | return -ENOMEM; | |
1013 | } | |
1014 | } | |
1015 | ||
2335efaf NB |
1016 | init_delayed_ref_head(head_ref, record, bytenr, num_bytes, ref_root, |
1017 | reserved, action, true, false); | |
fef394f7 | 1018 | head_ref->extent_op = NULL; |
5d4f98a2 | 1019 | |
56bec294 CM |
1020 | delayed_refs = &trans->transaction->delayed_refs; |
1021 | spin_lock(&delayed_refs->lock); | |
1022 | ||
1023 | /* | |
1024 | * insert both the head node and the new ref without dropping | |
1025 | * the spin lock | |
1026 | */ | |
2335efaf | 1027 | head_ref = add_delayed_ref_head(trans, head_ref, record, |
2187374f | 1028 | action, &qrecord_inserted); |
56bec294 | 1029 | |
afe2d748 | 1030 | ret = insert_delayed_ref(delayed_refs, head_ref, &ref->node); |
5d4f98a2 | 1031 | spin_unlock(&delayed_refs->lock); |
95a06077 | 1032 | |
ba2c4d4e JB |
1033 | /* |
1034 | * Need to update the delayed_refs_rsv with any changes we may have | |
1035 | * made. | |
1036 | */ | |
1037 | btrfs_update_delayed_refs_rsv(trans); | |
1038 | ||
cd7f9699 NB |
1039 | trace_add_delayed_data_ref(trans->fs_info, &ref->node, ref, |
1040 | action == BTRFS_ADD_DELAYED_EXTENT ? | |
1041 | BTRFS_ADD_DELAYED_REF : action); | |
1042 | if (ret > 0) | |
1043 | kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); | |
1044 | ||
1045 | ||
fb235dc0 | 1046 | if (qrecord_inserted) |
8949b9a1 | 1047 | return btrfs_qgroup_trace_extent_post(trans, record); |
5d4f98a2 YZ |
1048 | return 0; |
1049 | } | |
1050 | ||
c6e340bc | 1051 | int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
1052 | u64 bytenr, u64 num_bytes, |
1053 | struct btrfs_delayed_extent_op *extent_op) | |
1054 | { | |
1055 | struct btrfs_delayed_ref_head *head_ref; | |
1056 | struct btrfs_delayed_ref_root *delayed_refs; | |
5d4f98a2 | 1057 | |
78a6184a | 1058 | head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS); |
5d4f98a2 YZ |
1059 | if (!head_ref) |
1060 | return -ENOMEM; | |
1061 | ||
2335efaf | 1062 | init_delayed_ref_head(head_ref, NULL, bytenr, num_bytes, 0, 0, |
0e3696f8 | 1063 | BTRFS_UPDATE_DELAYED_HEAD, false, false); |
5d4f98a2 YZ |
1064 | head_ref->extent_op = extent_op; |
1065 | ||
1066 | delayed_refs = &trans->transaction->delayed_refs; | |
1067 | spin_lock(&delayed_refs->lock); | |
1068 | ||
2335efaf | 1069 | add_delayed_ref_head(trans, head_ref, NULL, BTRFS_UPDATE_DELAYED_HEAD, |
2187374f | 1070 | NULL); |
5d4f98a2 | 1071 | |
56bec294 | 1072 | spin_unlock(&delayed_refs->lock); |
ba2c4d4e JB |
1073 | |
1074 | /* | |
1075 | * Need to update the delayed_refs_rsv with any changes we may have | |
1076 | * made. | |
1077 | */ | |
1078 | btrfs_update_delayed_refs_rsv(trans); | |
56bec294 CM |
1079 | return 0; |
1080 | } | |
1081 | ||
1887be66 | 1082 | /* |
38e9372e DS |
1083 | * This does a simple search for the head node for a given extent. Returns the |
1084 | * head node if found, or NULL if not. | |
1887be66 CM |
1085 | */ |
1086 | struct btrfs_delayed_ref_head * | |
f72ad18e | 1087 | btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr) |
1887be66 | 1088 | { |
38e9372e DS |
1089 | lockdep_assert_held(&delayed_refs->lock); |
1090 | ||
d9352794 | 1091 | return find_ref_head(delayed_refs, bytenr, false); |
1887be66 | 1092 | } |
78a6184a | 1093 | |
e67c718b | 1094 | void __cold btrfs_delayed_ref_exit(void) |
78a6184a | 1095 | { |
5598e900 KM |
1096 | kmem_cache_destroy(btrfs_delayed_ref_head_cachep); |
1097 | kmem_cache_destroy(btrfs_delayed_tree_ref_cachep); | |
1098 | kmem_cache_destroy(btrfs_delayed_data_ref_cachep); | |
1099 | kmem_cache_destroy(btrfs_delayed_extent_op_cachep); | |
78a6184a MX |
1100 | } |
1101 | ||
f5c29bd9 | 1102 | int __init btrfs_delayed_ref_init(void) |
78a6184a MX |
1103 | { |
1104 | btrfs_delayed_ref_head_cachep = kmem_cache_create( | |
1105 | "btrfs_delayed_ref_head", | |
1106 | sizeof(struct btrfs_delayed_ref_head), 0, | |
fba4b697 | 1107 | SLAB_MEM_SPREAD, NULL); |
78a6184a MX |
1108 | if (!btrfs_delayed_ref_head_cachep) |
1109 | goto fail; | |
1110 | ||
1111 | btrfs_delayed_tree_ref_cachep = kmem_cache_create( | |
1112 | "btrfs_delayed_tree_ref", | |
1113 | sizeof(struct btrfs_delayed_tree_ref), 0, | |
fba4b697 | 1114 | SLAB_MEM_SPREAD, NULL); |
78a6184a MX |
1115 | if (!btrfs_delayed_tree_ref_cachep) |
1116 | goto fail; | |
1117 | ||
1118 | btrfs_delayed_data_ref_cachep = kmem_cache_create( | |
1119 | "btrfs_delayed_data_ref", | |
1120 | sizeof(struct btrfs_delayed_data_ref), 0, | |
fba4b697 | 1121 | SLAB_MEM_SPREAD, NULL); |
78a6184a MX |
1122 | if (!btrfs_delayed_data_ref_cachep) |
1123 | goto fail; | |
1124 | ||
1125 | btrfs_delayed_extent_op_cachep = kmem_cache_create( | |
1126 | "btrfs_delayed_extent_op", | |
1127 | sizeof(struct btrfs_delayed_extent_op), 0, | |
fba4b697 | 1128 | SLAB_MEM_SPREAD, NULL); |
78a6184a MX |
1129 | if (!btrfs_delayed_extent_op_cachep) |
1130 | goto fail; | |
1131 | ||
1132 | return 0; | |
1133 | fail: | |
1134 | btrfs_delayed_ref_exit(); | |
1135 | return -ENOMEM; | |
1136 | } |