blob: fab60c1ba0650f1e9829190a8ca81f4c66c6494b [file] [log] [blame]
Chris Mason56bec292009-03-13 10:10:06 -04001/*
2 * Copyright (C) 2009 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason56bec292009-03-13 10:10:06 -040021#include <linux/sort.h>
Chris Mason56bec292009-03-13 10:10:06 -040022#include "ctree.h"
23#include "delayed-ref.h"
24#include "transaction.h"
25
Miao Xie78a61842012-11-21 02:21:28 +000026struct kmem_cache *btrfs_delayed_ref_head_cachep;
27struct kmem_cache *btrfs_delayed_tree_ref_cachep;
28struct kmem_cache *btrfs_delayed_data_ref_cachep;
29struct kmem_cache *btrfs_delayed_extent_op_cachep;
Chris Mason56bec292009-03-13 10:10:06 -040030/*
31 * delayed back reference update tracking. For subvolume trees
32 * we queue up extent allocations and backref maintenance for
33 * delayed processing. This avoids deep call chains where we
34 * add extents in the middle of btrfs_search_slot, and it allows
35 * us to buffer up frequently modified backrefs in an rb tree instead
36 * of hammering updates on the extent allocation tree.
Chris Mason56bec292009-03-13 10:10:06 -040037 */
38
39/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -040040 * compare two delayed tree backrefs with same bytenr and type
Chris Mason56bec292009-03-13 10:10:06 -040041 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040042static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,
Josef Bacik41b0fc42013-04-01 20:36:28 -040043 struct btrfs_delayed_tree_ref *ref1, int type)
Chris Mason56bec292009-03-13 10:10:06 -040044{
Josef Bacik41b0fc42013-04-01 20:36:28 -040045 if (type == BTRFS_TREE_BLOCK_REF_KEY) {
46 if (ref1->root < ref2->root)
47 return -1;
48 if (ref1->root > ref2->root)
49 return 1;
50 } else {
51 if (ref1->parent < ref2->parent)
52 return -1;
53 if (ref1->parent > ref2->parent)
54 return 1;
55 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -040056 return 0;
57}
58
59/*
60 * compare two delayed data backrefs with same bytenr and type
61 */
62static int comp_data_refs(struct btrfs_delayed_data_ref *ref2,
63 struct btrfs_delayed_data_ref *ref1)
64{
65 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
66 if (ref1->root < ref2->root)
67 return -1;
68 if (ref1->root > ref2->root)
69 return 1;
70 if (ref1->objectid < ref2->objectid)
71 return -1;
72 if (ref1->objectid > ref2->objectid)
73 return 1;
74 if (ref1->offset < ref2->offset)
75 return -1;
76 if (ref1->offset > ref2->offset)
77 return 1;
78 } else {
79 if (ref1->parent < ref2->parent)
80 return -1;
81 if (ref1->parent > ref2->parent)
82 return 1;
83 }
84 return 0;
85}
86
87/*
88 * entries in the rb tree are ordered by the byte number of the extent,
89 * type of the delayed backrefs and content of delayed backrefs.
90 */
91static int comp_entry(struct btrfs_delayed_ref_node *ref2,
Josef Bacikae1e2062012-08-07 16:00:32 -040092 struct btrfs_delayed_ref_node *ref1,
93 bool compare_seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -040094{
95 if (ref1->bytenr < ref2->bytenr)
Chris Mason56bec292009-03-13 10:10:06 -040096 return -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040097 if (ref1->bytenr > ref2->bytenr)
Chris Mason56bec292009-03-13 10:10:06 -040098 return 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040099 if (ref1->is_head && ref2->is_head)
100 return 0;
101 if (ref2->is_head)
Chris Mason56bec292009-03-13 10:10:06 -0400102 return -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400103 if (ref1->is_head)
Chris Mason56bec292009-03-13 10:10:06 -0400104 return 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400105 if (ref1->type < ref2->type)
106 return -1;
107 if (ref1->type > ref2->type)
108 return 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200109 /* merging of sequenced refs is not allowed */
Josef Bacikae1e2062012-08-07 16:00:32 -0400110 if (compare_seq) {
111 if (ref1->seq < ref2->seq)
112 return -1;
113 if (ref1->seq > ref2->seq)
114 return 1;
115 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400116 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
117 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
118 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
Josef Bacik41b0fc42013-04-01 20:36:28 -0400119 btrfs_delayed_node_to_tree_ref(ref1),
120 ref1->type);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400121 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
122 ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
123 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),
124 btrfs_delayed_node_to_data_ref(ref1));
125 }
126 BUG();
Chris Mason56bec292009-03-13 10:10:06 -0400127 return 0;
128}
129
130/*
131 * insert a new ref into the rbtree. This returns any existing refs
132 * for the same (bytenr,parent) tuple, or NULL if the new node was properly
133 * inserted.
134 */
135static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
Chris Mason56bec292009-03-13 10:10:06 -0400136 struct rb_node *node)
137{
138 struct rb_node **p = &root->rb_node;
139 struct rb_node *parent_node = NULL;
140 struct btrfs_delayed_ref_node *entry;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400141 struct btrfs_delayed_ref_node *ins;
Chris Mason56bec292009-03-13 10:10:06 -0400142 int cmp;
143
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400144 ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Chris Mason56bec292009-03-13 10:10:06 -0400145 while (*p) {
146 parent_node = *p;
147 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
148 rb_node);
149
Josef Bacikae1e2062012-08-07 16:00:32 -0400150 cmp = comp_entry(entry, ins, 1);
Chris Mason56bec292009-03-13 10:10:06 -0400151 if (cmp < 0)
152 p = &(*p)->rb_left;
153 else if (cmp > 0)
154 p = &(*p)->rb_right;
155 else
156 return entry;
157 }
158
Chris Mason56bec292009-03-13 10:10:06 -0400159 rb_link_node(node, parent_node, p);
160 rb_insert_color(node, root);
161 return NULL;
162}
163
Liu Boc46effa2013-10-14 12:59:45 +0800164/* insert a new ref to head ref rbtree */
165static struct btrfs_delayed_ref_head *htree_insert(struct rb_root *root,
166 struct rb_node *node)
167{
168 struct rb_node **p = &root->rb_node;
169 struct rb_node *parent_node = NULL;
170 struct btrfs_delayed_ref_head *entry;
171 struct btrfs_delayed_ref_head *ins;
172 u64 bytenr;
173
174 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
175 bytenr = ins->node.bytenr;
176 while (*p) {
177 parent_node = *p;
178 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
179 href_node);
180
181 if (bytenr < entry->node.bytenr)
182 p = &(*p)->rb_left;
183 else if (bytenr > entry->node.bytenr)
184 p = &(*p)->rb_right;
185 else
186 return entry;
187 }
188
189 rb_link_node(node, parent_node, p);
190 rb_insert_color(node, root);
191 return NULL;
192}
193
Chris Mason56bec292009-03-13 10:10:06 -0400194/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400195 * find an head entry based on bytenr. This returns the delayed ref
Arne Jansend1270cd2011-09-13 15:16:43 +0200196 * head if it was able to find one, or NULL if nothing was in that spot.
197 * If return_bigger is given, the next bigger entry is returned if no exact
198 * match is found.
Chris Mason56bec292009-03-13 10:10:06 -0400199 */
Liu Boc46effa2013-10-14 12:59:45 +0800200static struct btrfs_delayed_ref_head *
201find_ref_head(struct rb_root *root, u64 bytenr,
202 struct btrfs_delayed_ref_head **last, int return_bigger)
Chris Mason56bec292009-03-13 10:10:06 -0400203{
Arne Jansend1270cd2011-09-13 15:16:43 +0200204 struct rb_node *n;
Liu Boc46effa2013-10-14 12:59:45 +0800205 struct btrfs_delayed_ref_head *entry;
Arne Jansend1270cd2011-09-13 15:16:43 +0200206 int cmp = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400207
Arne Jansend1270cd2011-09-13 15:16:43 +0200208again:
209 n = root->rb_node;
210 entry = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400211 while (n) {
Liu Boc46effa2013-10-14 12:59:45 +0800212 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
Chris Masonc3e69d52009-03-13 10:17:05 -0400213 if (last)
214 *last = entry;
Chris Mason56bec292009-03-13 10:10:06 -0400215
Liu Boc46effa2013-10-14 12:59:45 +0800216 if (bytenr < entry->node.bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400217 cmp = -1;
Liu Boc46effa2013-10-14 12:59:45 +0800218 else if (bytenr > entry->node.bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400219 cmp = 1;
220 else
221 cmp = 0;
222
Chris Mason56bec292009-03-13 10:10:06 -0400223 if (cmp < 0)
224 n = n->rb_left;
225 else if (cmp > 0)
226 n = n->rb_right;
227 else
228 return entry;
229 }
Arne Jansend1270cd2011-09-13 15:16:43 +0200230 if (entry && return_bigger) {
231 if (cmp > 0) {
Liu Boc46effa2013-10-14 12:59:45 +0800232 n = rb_next(&entry->href_node);
Arne Jansend1270cd2011-09-13 15:16:43 +0200233 if (!n)
234 n = rb_first(root);
Liu Boc46effa2013-10-14 12:59:45 +0800235 entry = rb_entry(n, struct btrfs_delayed_ref_head,
236 href_node);
237 bytenr = entry->node.bytenr;
Arne Jansend1270cd2011-09-13 15:16:43 +0200238 return_bigger = 0;
239 goto again;
240 }
241 return entry;
242 }
Chris Mason56bec292009-03-13 10:10:06 -0400243 return NULL;
244}
245
Chris Masonc3e69d52009-03-13 10:17:05 -0400246int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
247 struct btrfs_delayed_ref_head *head)
Chris Mason56bec292009-03-13 10:10:06 -0400248{
Chris Masonc3e69d52009-03-13 10:17:05 -0400249 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400250
Chris Masonc3e69d52009-03-13 10:17:05 -0400251 delayed_refs = &trans->transaction->delayed_refs;
252 assert_spin_locked(&delayed_refs->lock);
253 if (mutex_trylock(&head->mutex))
254 return 0;
255
256 atomic_inc(&head->node.refs);
257 spin_unlock(&delayed_refs->lock);
258
259 mutex_lock(&head->mutex);
260 spin_lock(&delayed_refs->lock);
261 if (!head->node.in_tree) {
262 mutex_unlock(&head->mutex);
263 btrfs_put_delayed_ref(&head->node);
264 return -EAGAIN;
265 }
266 btrfs_put_delayed_ref(&head->node);
267 return 0;
268}
269
Stefan Behrens35a36212013-08-14 18:12:25 +0200270static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
Josef Bacikae1e2062012-08-07 16:00:32 -0400271 struct btrfs_delayed_ref_root *delayed_refs,
272 struct btrfs_delayed_ref_node *ref)
273{
274 rb_erase(&ref->rb_node, &delayed_refs->root);
Liu Boc46effa2013-10-14 12:59:45 +0800275 if (btrfs_delayed_ref_is_head(ref)) {
276 struct btrfs_delayed_ref_head *head;
277
278 head = btrfs_delayed_node_to_head(ref);
279 rb_erase(&head->href_node, &delayed_refs->href_root);
280 }
Josef Bacikae1e2062012-08-07 16:00:32 -0400281 ref->in_tree = 0;
282 btrfs_put_delayed_ref(ref);
283 delayed_refs->num_entries--;
284 if (trans->delayed_ref_updates)
285 trans->delayed_ref_updates--;
286}
287
288static int merge_ref(struct btrfs_trans_handle *trans,
289 struct btrfs_delayed_ref_root *delayed_refs,
290 struct btrfs_delayed_ref_node *ref, u64 seq)
291{
292 struct rb_node *node;
293 int merged = 0;
294 int mod = 0;
295 int done = 0;
296
297 node = rb_prev(&ref->rb_node);
298 while (node) {
299 struct btrfs_delayed_ref_node *next;
300
301 next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
302 node = rb_prev(node);
303 if (next->bytenr != ref->bytenr)
304 break;
305 if (seq && next->seq >= seq)
306 break;
307 if (comp_entry(ref, next, 0))
308 continue;
309
310 if (ref->action == next->action) {
311 mod = next->ref_mod;
312 } else {
313 if (ref->ref_mod < next->ref_mod) {
314 struct btrfs_delayed_ref_node *tmp;
315
316 tmp = ref;
317 ref = next;
318 next = tmp;
319 done = 1;
320 }
321 mod = -next->ref_mod;
322 }
323
324 merged++;
325 drop_delayed_ref(trans, delayed_refs, next);
326 ref->ref_mod += mod;
327 if (ref->ref_mod == 0) {
328 drop_delayed_ref(trans, delayed_refs, ref);
329 break;
330 } else {
331 /*
332 * You can't have multiples of the same ref on a tree
333 * block.
334 */
335 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
336 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
337 }
338
339 if (done)
340 break;
341 node = rb_prev(&ref->rb_node);
342 }
343
344 return merged;
345}
346
347void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
348 struct btrfs_fs_info *fs_info,
349 struct btrfs_delayed_ref_root *delayed_refs,
350 struct btrfs_delayed_ref_head *head)
351{
352 struct rb_node *node;
353 u64 seq = 0;
354
Liu Bo9e5ac132013-10-14 12:59:43 +0800355 /*
356 * We don't have too much refs to merge in the case of delayed data
357 * refs.
358 */
359 if (head->is_data)
360 return;
361
Josef Bacikae1e2062012-08-07 16:00:32 -0400362 spin_lock(&fs_info->tree_mod_seq_lock);
363 if (!list_empty(&fs_info->tree_mod_seq_list)) {
364 struct seq_list *elem;
365
366 elem = list_first_entry(&fs_info->tree_mod_seq_list,
367 struct seq_list, list);
368 seq = elem->seq;
369 }
370 spin_unlock(&fs_info->tree_mod_seq_lock);
371
372 node = rb_prev(&head->node.rb_node);
373 while (node) {
374 struct btrfs_delayed_ref_node *ref;
375
376 ref = rb_entry(node, struct btrfs_delayed_ref_node,
377 rb_node);
378 if (ref->bytenr != head->node.bytenr)
379 break;
380
381 /* We can't merge refs that are outside of our seq count */
382 if (seq && ref->seq >= seq)
383 break;
384 if (merge_ref(trans, delayed_refs, ref, seq))
385 node = rb_prev(&head->node.rb_node);
386 else
387 node = rb_prev(node);
388 }
389}
390
Jan Schmidt097b8a72012-06-21 11:08:04 +0200391int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
392 struct btrfs_delayed_ref_root *delayed_refs,
Arne Jansen00f04b82011-09-14 12:37:00 +0200393 u64 seq)
394{
395 struct seq_list *elem;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200396 int ret = 0;
Arne Jansen00f04b82011-09-14 12:37:00 +0200397
Jan Schmidt097b8a72012-06-21 11:08:04 +0200398 spin_lock(&fs_info->tree_mod_seq_lock);
399 if (!list_empty(&fs_info->tree_mod_seq_list)) {
400 elem = list_first_entry(&fs_info->tree_mod_seq_list,
401 struct seq_list, list);
402 if (seq >= elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000403 pr_debug("holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)\n",
404 (u32)(seq >> 32), (u32)seq,
405 (u32)(elem->seq >> 32), (u32)elem->seq,
406 delayed_refs);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200407 ret = 1;
408 }
Arne Jansen00f04b82011-09-14 12:37:00 +0200409 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200410
411 spin_unlock(&fs_info->tree_mod_seq_lock);
412 return ret;
Arne Jansen00f04b82011-09-14 12:37:00 +0200413}
414
Chris Masonc3e69d52009-03-13 10:17:05 -0400415int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
416 struct list_head *cluster, u64 start)
417{
418 int count = 0;
419 struct btrfs_delayed_ref_root *delayed_refs;
420 struct rb_node *node;
Liu Boc46effa2013-10-14 12:59:45 +0800421 struct btrfs_delayed_ref_head *head = NULL;
Chris Masonc3e69d52009-03-13 10:17:05 -0400422
423 delayed_refs = &trans->transaction->delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +0800424 node = rb_first(&delayed_refs->href_root);
425
426 if (start) {
427 find_ref_head(&delayed_refs->href_root, start + 1, &head, 1);
428 if (head)
429 node = &head->href_node;
Chris Masonc3e69d52009-03-13 10:17:05 -0400430 }
431again:
432 while (node && count < 32) {
Liu Boc46effa2013-10-14 12:59:45 +0800433 head = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
434 if (list_empty(&head->cluster)) {
435 list_add_tail(&head->cluster, cluster);
436 delayed_refs->run_delayed_start =
437 head->node.bytenr;
438 count++;
Chris Masonc3e69d52009-03-13 10:17:05 -0400439
Liu Boc46effa2013-10-14 12:59:45 +0800440 WARN_ON(delayed_refs->num_heads_ready == 0);
441 delayed_refs->num_heads_ready--;
442 } else if (count) {
443 /* the goal of the clustering is to find extents
444 * that are likely to end up in the same extent
445 * leaf on disk. So, we don't want them spread
446 * all over the tree. Stop now if we've hit
447 * a head that was already in use
448 */
449 break;
Chris Mason56bec292009-03-13 10:10:06 -0400450 }
Chris Masonc3e69d52009-03-13 10:17:05 -0400451 node = rb_next(node);
Chris Mason56bec292009-03-13 10:10:06 -0400452 }
Chris Masonc3e69d52009-03-13 10:17:05 -0400453 if (count) {
454 return 0;
455 } else if (start) {
456 /*
457 * we've gone to the end of the rbtree without finding any
458 * clusters. start from the beginning and try again
459 */
460 start = 0;
Liu Boc46effa2013-10-14 12:59:45 +0800461 node = rb_first(&delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -0400462 goto again;
463 }
464 return 1;
Chris Mason56bec292009-03-13 10:10:06 -0400465}
466
Miao Xie093486c2012-12-19 08:10:10 +0000467void btrfs_release_ref_cluster(struct list_head *cluster)
468{
469 struct list_head *pos, *q;
470
471 list_for_each_safe(pos, q, cluster)
472 list_del_init(pos);
473}
474
Chris Mason56bec292009-03-13 10:10:06 -0400475/*
Chris Mason56bec292009-03-13 10:10:06 -0400476 * helper function to update an extent delayed ref in the
477 * rbtree. existing and update must both have the same
478 * bytenr and parent
479 *
480 * This may free existing if the update cancels out whatever
481 * operation it was doing.
482 */
483static noinline void
484update_existing_ref(struct btrfs_trans_handle *trans,
485 struct btrfs_delayed_ref_root *delayed_refs,
486 struct btrfs_delayed_ref_node *existing,
487 struct btrfs_delayed_ref_node *update)
488{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400489 if (update->action != existing->action) {
Chris Mason56bec292009-03-13 10:10:06 -0400490 /*
491 * this is effectively undoing either an add or a
492 * drop. We decrement the ref_mod, and if it goes
493 * down to zero we just delete the entry without
494 * every changing the extent allocation tree.
495 */
496 existing->ref_mod--;
Josef Bacikae1e2062012-08-07 16:00:32 -0400497 if (existing->ref_mod == 0)
498 drop_delayed_ref(trans, delayed_refs, existing);
499 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400500 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
501 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400502 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400503 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
504 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400505 /*
506 * the action on the existing ref matches
507 * the action on the ref we're trying to add.
508 * Bump the ref_mod by one so the backref that
509 * is eventually added/removed has the correct
510 * reference count
511 */
512 existing->ref_mod += update->ref_mod;
513 }
514}
515
516/*
517 * helper function to update the accounting in the head ref
518 * existing and update must have the same bytenr
519 */
520static noinline void
521update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
522 struct btrfs_delayed_ref_node *update)
523{
524 struct btrfs_delayed_ref_head *existing_ref;
525 struct btrfs_delayed_ref_head *ref;
526
527 existing_ref = btrfs_delayed_node_to_head(existing);
528 ref = btrfs_delayed_node_to_head(update);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400529 BUG_ON(existing_ref->is_data != ref->is_data);
Chris Mason56bec292009-03-13 10:10:06 -0400530
531 if (ref->must_insert_reserved) {
532 /* if the extent was freed and then
533 * reallocated before the delayed ref
534 * entries were processed, we can end up
535 * with an existing head ref without
536 * the must_insert_reserved flag set.
537 * Set it again here
538 */
539 existing_ref->must_insert_reserved = ref->must_insert_reserved;
540
541 /*
542 * update the num_bytes so we make sure the accounting
543 * is done correctly
544 */
545 existing->num_bytes = update->num_bytes;
546
547 }
548
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400549 if (ref->extent_op) {
550 if (!existing_ref->extent_op) {
551 existing_ref->extent_op = ref->extent_op;
552 } else {
553 if (ref->extent_op->update_key) {
554 memcpy(&existing_ref->extent_op->key,
555 &ref->extent_op->key,
556 sizeof(ref->extent_op->key));
557 existing_ref->extent_op->update_key = 1;
558 }
559 if (ref->extent_op->update_flags) {
560 existing_ref->extent_op->flags_to_set |=
561 ref->extent_op->flags_to_set;
562 existing_ref->extent_op->update_flags = 1;
563 }
Miao Xie78a61842012-11-21 02:21:28 +0000564 btrfs_free_delayed_extent_op(ref->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400565 }
566 }
Chris Mason56bec292009-03-13 10:10:06 -0400567 /*
568 * update the reference mod on the head to reflect this new operation
569 */
570 existing->ref_mod += update->ref_mod;
571}
572
573/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400574 * helper function to actually insert a head node into the rbtree.
Chris Mason56bec292009-03-13 10:10:06 -0400575 * this does all the dirty work in terms of maintaining the correct
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400576 * overall modification count.
Chris Mason56bec292009-03-13 10:10:06 -0400577 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100578static noinline void add_delayed_ref_head(struct btrfs_fs_info *fs_info,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200579 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400580 struct btrfs_delayed_ref_node *ref,
581 u64 bytenr, u64 num_bytes,
582 int action, int is_data)
Chris Mason56bec292009-03-13 10:10:06 -0400583{
584 struct btrfs_delayed_ref_node *existing;
Chris Masonc3e69d52009-03-13 10:17:05 -0400585 struct btrfs_delayed_ref_head *head_ref = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400586 struct btrfs_delayed_ref_root *delayed_refs;
587 int count_mod = 1;
588 int must_insert_reserved = 0;
589
590 /*
591 * the head node stores the sum of all the mods, so dropping a ref
592 * should drop the sum in the head node by one.
593 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400594 if (action == BTRFS_UPDATE_DELAYED_HEAD)
595 count_mod = 0;
596 else if (action == BTRFS_DROP_DELAYED_REF)
597 count_mod = -1;
Chris Mason56bec292009-03-13 10:10:06 -0400598
599 /*
600 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
601 * the reserved accounting when the extent is finally added, or
602 * if a later modification deletes the delayed ref without ever
603 * inserting the extent into the extent allocation tree.
604 * ref->must_insert_reserved is the flag used to record
605 * that accounting mods are required.
606 *
607 * Once we record must_insert_reserved, switch the action to
608 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
609 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400610 if (action == BTRFS_ADD_DELAYED_EXTENT)
Chris Mason56bec292009-03-13 10:10:06 -0400611 must_insert_reserved = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400612 else
Chris Mason56bec292009-03-13 10:10:06 -0400613 must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400614
615 delayed_refs = &trans->transaction->delayed_refs;
616
617 /* first set the basic ref node struct up */
618 atomic_set(&ref->refs, 1);
619 ref->bytenr = bytenr;
Chris Mason56bec292009-03-13 10:10:06 -0400620 ref->num_bytes = num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400621 ref->ref_mod = count_mod;
622 ref->type = 0;
623 ref->action = 0;
624 ref->is_head = 1;
625 ref->in_tree = 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200626 ref->seq = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400627
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628 head_ref = btrfs_delayed_node_to_head(ref);
629 head_ref->must_insert_reserved = must_insert_reserved;
630 head_ref->is_data = is_data;
Chris Mason56bec292009-03-13 10:10:06 -0400631
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 INIT_LIST_HEAD(&head_ref->cluster);
633 mutex_init(&head_ref->mutex);
634
Liu Bo599c75e2013-07-16 19:03:36 +0800635 trace_add_delayed_ref_head(ref, head_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000636
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400637 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
Chris Mason56bec292009-03-13 10:10:06 -0400638
639 if (existing) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400640 update_existing_head_ref(existing, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400641 /*
642 * we've updated the existing ref, free the newly
643 * allocated ref
644 */
Miao Xie78a61842012-11-21 02:21:28 +0000645 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
Chris Mason56bec292009-03-13 10:10:06 -0400646 } else {
Liu Boc46effa2013-10-14 12:59:45 +0800647 htree_insert(&delayed_refs->href_root, &head_ref->href_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400648 delayed_refs->num_heads++;
649 delayed_refs->num_heads_ready++;
Chris Mason56bec292009-03-13 10:10:06 -0400650 delayed_refs->num_entries++;
651 trans->delayed_ref_updates++;
652 }
Chris Mason56bec292009-03-13 10:10:06 -0400653}
654
655/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400656 * helper to insert a delayed tree ref into the rbtree.
657 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100658static noinline void add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200659 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400660 struct btrfs_delayed_ref_node *ref,
661 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200662 u64 ref_root, int level, int action,
663 int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400664{
665 struct btrfs_delayed_ref_node *existing;
666 struct btrfs_delayed_tree_ref *full_ref;
667 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200668 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669
670 if (action == BTRFS_ADD_DELAYED_EXTENT)
671 action = BTRFS_ADD_DELAYED_REF;
672
673 delayed_refs = &trans->transaction->delayed_refs;
674
675 /* first set the basic ref node struct up */
676 atomic_set(&ref->refs, 1);
677 ref->bytenr = bytenr;
678 ref->num_bytes = num_bytes;
679 ref->ref_mod = 1;
680 ref->action = action;
681 ref->is_head = 0;
682 ref->in_tree = 1;
683
Jan Schmidt546adb02012-06-14 16:37:44 +0200684 if (need_ref_seq(for_cow, ref_root))
685 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
Arne Jansen00f04b82011-09-14 12:37:00 +0200686 ref->seq = seq;
687
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 full_ref = btrfs_delayed_node_to_tree_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200689 full_ref->parent = parent;
690 full_ref->root = ref_root;
691 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400692 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200693 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400694 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400695 full_ref->level = level;
696
Liu Bo599c75e2013-07-16 19:03:36 +0800697 trace_add_delayed_tree_ref(ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000698
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400699 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
700
701 if (existing) {
702 update_existing_ref(trans, delayed_refs, existing, ref);
703 /*
704 * we've updated the existing ref, free the newly
705 * allocated ref
706 */
Miao Xie78a61842012-11-21 02:21:28 +0000707 kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400708 } else {
709 delayed_refs->num_entries++;
710 trans->delayed_ref_updates++;
711 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400712}
713
714/*
715 * helper to insert a delayed data ref into the rbtree.
716 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100717static noinline void add_delayed_data_ref(struct btrfs_fs_info *fs_info,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200718 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400719 struct btrfs_delayed_ref_node *ref,
720 u64 bytenr, u64 num_bytes, u64 parent,
721 u64 ref_root, u64 owner, u64 offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200722 int action, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400723{
724 struct btrfs_delayed_ref_node *existing;
725 struct btrfs_delayed_data_ref *full_ref;
726 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200727 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400728
729 if (action == BTRFS_ADD_DELAYED_EXTENT)
730 action = BTRFS_ADD_DELAYED_REF;
731
732 delayed_refs = &trans->transaction->delayed_refs;
733
734 /* first set the basic ref node struct up */
735 atomic_set(&ref->refs, 1);
736 ref->bytenr = bytenr;
737 ref->num_bytes = num_bytes;
738 ref->ref_mod = 1;
739 ref->action = action;
740 ref->is_head = 0;
741 ref->in_tree = 1;
742
Jan Schmidt546adb02012-06-14 16:37:44 +0200743 if (need_ref_seq(for_cow, ref_root))
744 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
Arne Jansen00f04b82011-09-14 12:37:00 +0200745 ref->seq = seq;
746
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400747 full_ref = btrfs_delayed_node_to_data_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200748 full_ref->parent = parent;
749 full_ref->root = ref_root;
750 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751 ref->type = BTRFS_SHARED_DATA_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200752 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400753 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200754
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400755 full_ref->objectid = owner;
756 full_ref->offset = offset;
757
Liu Bo599c75e2013-07-16 19:03:36 +0800758 trace_add_delayed_data_ref(ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000759
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400760 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
761
762 if (existing) {
763 update_existing_ref(trans, delayed_refs, existing, ref);
764 /*
765 * we've updated the existing ref, free the newly
766 * allocated ref
767 */
Miao Xie78a61842012-11-21 02:21:28 +0000768 kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400769 } else {
770 delayed_refs->num_entries++;
771 trans->delayed_ref_updates++;
772 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400773}
774
775/*
776 * add a delayed tree ref. This does all of the accounting required
Chris Mason56bec292009-03-13 10:10:06 -0400777 * to make sure the delayed ref is eventually processed before this
778 * transaction commits.
779 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200780int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
781 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400782 u64 bytenr, u64 num_bytes, u64 parent,
783 u64 ref_root, int level, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200784 struct btrfs_delayed_extent_op *extent_op,
785 int for_cow)
Chris Mason56bec292009-03-13 10:10:06 -0400786{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400787 struct btrfs_delayed_tree_ref *ref;
Chris Mason56bec292009-03-13 10:10:06 -0400788 struct btrfs_delayed_ref_head *head_ref;
789 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400790
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 BUG_ON(extent_op && extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000792 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400793 if (!ref)
794 return -ENOMEM;
795
Miao Xie78a61842012-11-21 02:21:28 +0000796 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400797 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000798 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400799 return -ENOMEM;
800 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801
802 head_ref->extent_op = extent_op;
803
Chris Mason56bec292009-03-13 10:10:06 -0400804 delayed_refs = &trans->transaction->delayed_refs;
805 spin_lock(&delayed_refs->lock);
806
807 /*
808 * insert both the head node and the new ref without dropping
809 * the spin lock
810 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100811 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200812 num_bytes, action, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400813
Jeff Mahoney143bede2012-03-01 14:56:26 +0100814 add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200815 num_bytes, parent, ref_root, level, action,
816 for_cow);
Chris Mason56bec292009-03-13 10:10:06 -0400817 spin_unlock(&delayed_refs->lock);
Jan Schmidt546adb02012-06-14 16:37:44 +0200818 if (need_ref_seq(for_cow, ref_root))
819 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
Jan Schmidt95a06072012-05-29 17:06:54 +0200820
Chris Mason56bec292009-03-13 10:10:06 -0400821 return 0;
822}
823
824/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400825 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
826 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200827int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
828 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400829 u64 bytenr, u64 num_bytes,
830 u64 parent, u64 ref_root,
831 u64 owner, u64 offset, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200832 struct btrfs_delayed_extent_op *extent_op,
833 int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834{
835 struct btrfs_delayed_data_ref *ref;
836 struct btrfs_delayed_ref_head *head_ref;
837 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838
839 BUG_ON(extent_op && !extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000840 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400841 if (!ref)
842 return -ENOMEM;
843
Miao Xie78a61842012-11-21 02:21:28 +0000844 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000846 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 return -ENOMEM;
848 }
849
850 head_ref->extent_op = extent_op;
851
852 delayed_refs = &trans->transaction->delayed_refs;
853 spin_lock(&delayed_refs->lock);
854
855 /*
856 * insert both the head node and the new ref without dropping
857 * the spin lock
858 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100859 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200860 num_bytes, action, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861
Jeff Mahoney143bede2012-03-01 14:56:26 +0100862 add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200863 num_bytes, parent, ref_root, owner, offset,
864 action, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 spin_unlock(&delayed_refs->lock);
Jan Schmidt546adb02012-06-14 16:37:44 +0200866 if (need_ref_seq(for_cow, ref_root))
867 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
Jan Schmidt95a06072012-05-29 17:06:54 +0200868
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400869 return 0;
870}
871
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200872int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
873 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 u64 bytenr, u64 num_bytes,
875 struct btrfs_delayed_extent_op *extent_op)
876{
877 struct btrfs_delayed_ref_head *head_ref;
878 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400879
Miao Xie78a61842012-11-21 02:21:28 +0000880 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 if (!head_ref)
882 return -ENOMEM;
883
884 head_ref->extent_op = extent_op;
885
886 delayed_refs = &trans->transaction->delayed_refs;
887 spin_lock(&delayed_refs->lock);
888
Jeff Mahoney143bede2012-03-01 14:56:26 +0100889 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
891 extent_op->is_data);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892
893 spin_unlock(&delayed_refs->lock);
894 return 0;
895}
896
897/*
Chris Mason1887be62009-03-13 10:11:24 -0400898 * this does a simple search for the head node for a given extent.
899 * It must be called with the delayed ref spinlock held, and it returns
900 * the head node if any where found, or NULL if not.
901 */
902struct btrfs_delayed_ref_head *
903btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
904{
Chris Mason1887be62009-03-13 10:11:24 -0400905 struct btrfs_delayed_ref_root *delayed_refs;
906
907 delayed_refs = &trans->transaction->delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +0800908 return find_ref_head(&delayed_refs->href_root, bytenr, NULL, 0);
Chris Mason1887be62009-03-13 10:11:24 -0400909}
Miao Xie78a61842012-11-21 02:21:28 +0000910
911void btrfs_delayed_ref_exit(void)
912{
913 if (btrfs_delayed_ref_head_cachep)
914 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
915 if (btrfs_delayed_tree_ref_cachep)
916 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
917 if (btrfs_delayed_data_ref_cachep)
918 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
919 if (btrfs_delayed_extent_op_cachep)
920 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
921}
922
923int btrfs_delayed_ref_init(void)
924{
925 btrfs_delayed_ref_head_cachep = kmem_cache_create(
926 "btrfs_delayed_ref_head",
927 sizeof(struct btrfs_delayed_ref_head), 0,
928 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
929 if (!btrfs_delayed_ref_head_cachep)
930 goto fail;
931
932 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
933 "btrfs_delayed_tree_ref",
934 sizeof(struct btrfs_delayed_tree_ref), 0,
935 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
936 if (!btrfs_delayed_tree_ref_cachep)
937 goto fail;
938
939 btrfs_delayed_data_ref_cachep = kmem_cache_create(
940 "btrfs_delayed_data_ref",
941 sizeof(struct btrfs_delayed_data_ref), 0,
942 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
943 if (!btrfs_delayed_data_ref_cachep)
944 goto fail;
945
946 btrfs_delayed_extent_op_cachep = kmem_cache_create(
947 "btrfs_delayed_extent_op",
948 sizeof(struct btrfs_delayed_extent_op), 0,
949 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
950 if (!btrfs_delayed_extent_op_cachep)
951 goto fail;
952
953 return 0;
954fail:
955 btrfs_delayed_ref_exit();
956 return -ENOMEM;
957}