blob: 56cdfe988d69e82b0b5942a5169713c918c98a50 [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 +0200208 n = root->rb_node;
209 entry = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400210 while (n) {
Liu Boc46effa2013-10-14 12:59:45 +0800211 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
Chris Masonc3e69d52009-03-13 10:17:05 -0400212 if (last)
213 *last = entry;
Chris Mason56bec292009-03-13 10:10:06 -0400214
Liu Boc46effa2013-10-14 12:59:45 +0800215 if (bytenr < entry->node.bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400216 cmp = -1;
Liu Boc46effa2013-10-14 12:59:45 +0800217 else if (bytenr > entry->node.bytenr)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400218 cmp = 1;
219 else
220 cmp = 0;
221
Chris Mason56bec292009-03-13 10:10:06 -0400222 if (cmp < 0)
223 n = n->rb_left;
224 else if (cmp > 0)
225 n = n->rb_right;
226 else
227 return entry;
228 }
Arne Jansend1270cd2011-09-13 15:16:43 +0200229 if (entry && return_bigger) {
230 if (cmp > 0) {
Liu Boc46effa2013-10-14 12:59:45 +0800231 n = rb_next(&entry->href_node);
Arne Jansend1270cd2011-09-13 15:16:43 +0200232 if (!n)
233 n = rb_first(root);
Liu Boc46effa2013-10-14 12:59:45 +0800234 entry = rb_entry(n, struct btrfs_delayed_ref_head,
235 href_node);
Filipe Manana6103fb42014-02-12 15:07:52 +0000236 if (last)
237 *last = entry;
238 return entry;
Arne Jansend1270cd2011-09-13 15:16:43 +0200239 }
240 return entry;
241 }
Chris Mason56bec292009-03-13 10:10:06 -0400242 return NULL;
243}
244
Chris Masonc3e69d52009-03-13 10:17:05 -0400245int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
246 struct btrfs_delayed_ref_head *head)
Chris Mason56bec292009-03-13 10:10:06 -0400247{
Chris Masonc3e69d52009-03-13 10:17:05 -0400248 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400249
Chris Masonc3e69d52009-03-13 10:17:05 -0400250 delayed_refs = &trans->transaction->delayed_refs;
251 assert_spin_locked(&delayed_refs->lock);
252 if (mutex_trylock(&head->mutex))
253 return 0;
254
255 atomic_inc(&head->node.refs);
256 spin_unlock(&delayed_refs->lock);
257
258 mutex_lock(&head->mutex);
259 spin_lock(&delayed_refs->lock);
260 if (!head->node.in_tree) {
261 mutex_unlock(&head->mutex);
262 btrfs_put_delayed_ref(&head->node);
263 return -EAGAIN;
264 }
265 btrfs_put_delayed_ref(&head->node);
266 return 0;
267}
268
Stefan Behrens35a36212013-08-14 18:12:25 +0200269static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
Josef Bacikae1e2062012-08-07 16:00:32 -0400270 struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500271 struct btrfs_delayed_ref_head *head,
Josef Bacikae1e2062012-08-07 16:00:32 -0400272 struct btrfs_delayed_ref_node *ref)
273{
Liu Boc46effa2013-10-14 12:59:45 +0800274 if (btrfs_delayed_ref_is_head(ref)) {
Liu Boc46effa2013-10-14 12:59:45 +0800275 head = btrfs_delayed_node_to_head(ref);
276 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500277 } else {
278 assert_spin_locked(&head->lock);
279 rb_erase(&ref->rb_node, &head->ref_root);
Liu Boc46effa2013-10-14 12:59:45 +0800280 }
Josef Bacikae1e2062012-08-07 16:00:32 -0400281 ref->in_tree = 0;
282 btrfs_put_delayed_ref(ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500283 atomic_dec(&delayed_refs->num_entries);
Josef Bacikae1e2062012-08-07 16:00:32 -0400284 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,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500290 struct btrfs_delayed_ref_head *head,
Josef Bacikae1e2062012-08-07 16:00:32 -0400291 struct btrfs_delayed_ref_node *ref, u64 seq)
292{
293 struct rb_node *node;
Josef Bacikae1e2062012-08-07 16:00:32 -0400294 int mod = 0;
295 int done = 0;
296
Josef Bacikd7df2c72014-01-23 09:21:38 -0500297 node = rb_next(&ref->rb_node);
298 while (!done && node) {
Josef Bacikae1e2062012-08-07 16:00:32 -0400299 struct btrfs_delayed_ref_node *next;
300
301 next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500302 node = rb_next(node);
Josef Bacikae1e2062012-08-07 16:00:32 -0400303 if (seq && next->seq >= seq)
304 break;
305 if (comp_entry(ref, next, 0))
306 continue;
307
308 if (ref->action == next->action) {
309 mod = next->ref_mod;
310 } else {
311 if (ref->ref_mod < next->ref_mod) {
312 struct btrfs_delayed_ref_node *tmp;
313
314 tmp = ref;
315 ref = next;
316 next = tmp;
317 done = 1;
318 }
319 mod = -next->ref_mod;
320 }
321
Josef Bacikd7df2c72014-01-23 09:21:38 -0500322 drop_delayed_ref(trans, delayed_refs, head, next);
Josef Bacikae1e2062012-08-07 16:00:32 -0400323 ref->ref_mod += mod;
324 if (ref->ref_mod == 0) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500325 drop_delayed_ref(trans, delayed_refs, head, ref);
326 done = 1;
Josef Bacikae1e2062012-08-07 16:00:32 -0400327 } else {
328 /*
329 * You can't have multiples of the same ref on a tree
330 * block.
331 */
332 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
333 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
334 }
Josef Bacikae1e2062012-08-07 16:00:32 -0400335 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500336 return done;
Josef Bacikae1e2062012-08-07 16:00:32 -0400337}
338
339void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
340 struct btrfs_fs_info *fs_info,
341 struct btrfs_delayed_ref_root *delayed_refs,
342 struct btrfs_delayed_ref_head *head)
343{
344 struct rb_node *node;
345 u64 seq = 0;
346
Josef Bacikd7df2c72014-01-23 09:21:38 -0500347 assert_spin_locked(&head->lock);
Liu Bo9e5ac132013-10-14 12:59:43 +0800348 /*
349 * We don't have too much refs to merge in the case of delayed data
350 * refs.
351 */
352 if (head->is_data)
353 return;
354
Josef Bacikae1e2062012-08-07 16:00:32 -0400355 spin_lock(&fs_info->tree_mod_seq_lock);
356 if (!list_empty(&fs_info->tree_mod_seq_list)) {
357 struct seq_list *elem;
358
359 elem = list_first_entry(&fs_info->tree_mod_seq_list,
360 struct seq_list, list);
361 seq = elem->seq;
362 }
363 spin_unlock(&fs_info->tree_mod_seq_lock);
364
Josef Bacikd7df2c72014-01-23 09:21:38 -0500365 node = rb_first(&head->ref_root);
Josef Bacikae1e2062012-08-07 16:00:32 -0400366 while (node) {
367 struct btrfs_delayed_ref_node *ref;
368
369 ref = rb_entry(node, struct btrfs_delayed_ref_node,
370 rb_node);
Josef Bacikae1e2062012-08-07 16:00:32 -0400371 /* We can't merge refs that are outside of our seq count */
372 if (seq && ref->seq >= seq)
373 break;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500374 if (merge_ref(trans, delayed_refs, head, ref, seq))
375 node = rb_first(&head->ref_root);
Josef Bacikae1e2062012-08-07 16:00:32 -0400376 else
Josef Bacikd7df2c72014-01-23 09:21:38 -0500377 node = rb_next(&ref->rb_node);
Josef Bacikae1e2062012-08-07 16:00:32 -0400378 }
379}
380
Jan Schmidt097b8a72012-06-21 11:08:04 +0200381int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
382 struct btrfs_delayed_ref_root *delayed_refs,
Arne Jansen00f04b82011-09-14 12:37:00 +0200383 u64 seq)
384{
385 struct seq_list *elem;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200386 int ret = 0;
Arne Jansen00f04b82011-09-14 12:37:00 +0200387
Jan Schmidt097b8a72012-06-21 11:08:04 +0200388 spin_lock(&fs_info->tree_mod_seq_lock);
389 if (!list_empty(&fs_info->tree_mod_seq_list)) {
390 elem = list_first_entry(&fs_info->tree_mod_seq_list,
391 struct seq_list, list);
392 if (seq >= elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000393 pr_debug("holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)\n",
394 (u32)(seq >> 32), (u32)seq,
395 (u32)(elem->seq >> 32), (u32)elem->seq,
396 delayed_refs);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397 ret = 1;
398 }
Arne Jansen00f04b82011-09-14 12:37:00 +0200399 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200400
401 spin_unlock(&fs_info->tree_mod_seq_lock);
402 return ret;
Arne Jansen00f04b82011-09-14 12:37:00 +0200403}
404
Josef Bacikd7df2c72014-01-23 09:21:38 -0500405struct btrfs_delayed_ref_head *
406btrfs_select_ref_head(struct btrfs_trans_handle *trans)
Chris Masonc3e69d52009-03-13 10:17:05 -0400407{
Chris Masonc3e69d52009-03-13 10:17:05 -0400408 struct btrfs_delayed_ref_root *delayed_refs;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500409 struct btrfs_delayed_ref_head *head;
410 u64 start;
411 bool loop = false;
Chris Masonc3e69d52009-03-13 10:17:05 -0400412
413 delayed_refs = &trans->transaction->delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +0800414
Chris Masonc3e69d52009-03-13 10:17:05 -0400415again:
Josef Bacikd7df2c72014-01-23 09:21:38 -0500416 start = delayed_refs->run_delayed_start;
417 head = find_ref_head(&delayed_refs->href_root, start, NULL, 1);
418 if (!head && !loop) {
419 delayed_refs->run_delayed_start = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -0400420 start = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500421 loop = true;
422 head = find_ref_head(&delayed_refs->href_root, start, NULL, 1);
423 if (!head)
424 return NULL;
425 } else if (!head && loop) {
426 return NULL;
Chris Masonc3e69d52009-03-13 10:17:05 -0400427 }
Chris Mason56bec292009-03-13 10:10:06 -0400428
Josef Bacikd7df2c72014-01-23 09:21:38 -0500429 while (head->processing) {
430 struct rb_node *node;
Miao Xie093486c2012-12-19 08:10:10 +0000431
Josef Bacikd7df2c72014-01-23 09:21:38 -0500432 node = rb_next(&head->href_node);
433 if (!node) {
434 if (loop)
435 return NULL;
436 delayed_refs->run_delayed_start = 0;
437 start = 0;
438 loop = true;
439 goto again;
440 }
441 head = rb_entry(node, struct btrfs_delayed_ref_head,
442 href_node);
443 }
444
445 head->processing = 1;
446 WARN_ON(delayed_refs->num_heads_ready == 0);
447 delayed_refs->num_heads_ready--;
448 delayed_refs->run_delayed_start = head->node.bytenr +
449 head->node.num_bytes;
450 return head;
Miao Xie093486c2012-12-19 08:10:10 +0000451}
452
Chris Mason56bec292009-03-13 10:10:06 -0400453/*
Chris Mason56bec292009-03-13 10:10:06 -0400454 * helper function to update an extent delayed ref in the
455 * rbtree. existing and update must both have the same
456 * bytenr and parent
457 *
458 * This may free existing if the update cancels out whatever
459 * operation it was doing.
460 */
461static noinline void
462update_existing_ref(struct btrfs_trans_handle *trans,
463 struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500464 struct btrfs_delayed_ref_head *head,
Chris Mason56bec292009-03-13 10:10:06 -0400465 struct btrfs_delayed_ref_node *existing,
466 struct btrfs_delayed_ref_node *update)
467{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400468 if (update->action != existing->action) {
Chris Mason56bec292009-03-13 10:10:06 -0400469 /*
470 * this is effectively undoing either an add or a
471 * drop. We decrement the ref_mod, and if it goes
472 * down to zero we just delete the entry without
473 * every changing the extent allocation tree.
474 */
475 existing->ref_mod--;
Josef Bacikae1e2062012-08-07 16:00:32 -0400476 if (existing->ref_mod == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -0500477 drop_delayed_ref(trans, delayed_refs, head, existing);
Josef Bacikae1e2062012-08-07 16:00:32 -0400478 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400479 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
480 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400481 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400482 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
483 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400484 /*
485 * the action on the existing ref matches
486 * the action on the ref we're trying to add.
487 * Bump the ref_mod by one so the backref that
488 * is eventually added/removed has the correct
489 * reference count
490 */
491 existing->ref_mod += update->ref_mod;
492 }
493}
494
495/*
496 * helper function to update the accounting in the head ref
497 * existing and update must have the same bytenr
498 */
499static noinline void
500update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
501 struct btrfs_delayed_ref_node *update)
502{
503 struct btrfs_delayed_ref_head *existing_ref;
504 struct btrfs_delayed_ref_head *ref;
505
506 existing_ref = btrfs_delayed_node_to_head(existing);
507 ref = btrfs_delayed_node_to_head(update);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400508 BUG_ON(existing_ref->is_data != ref->is_data);
Chris Mason56bec292009-03-13 10:10:06 -0400509
510 if (ref->must_insert_reserved) {
511 /* if the extent was freed and then
512 * reallocated before the delayed ref
513 * entries were processed, we can end up
514 * with an existing head ref without
515 * the must_insert_reserved flag set.
516 * Set it again here
517 */
518 existing_ref->must_insert_reserved = ref->must_insert_reserved;
519
520 /*
521 * update the num_bytes so we make sure the accounting
522 * is done correctly
523 */
524 existing->num_bytes = update->num_bytes;
525
526 }
527
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400528 if (ref->extent_op) {
529 if (!existing_ref->extent_op) {
530 existing_ref->extent_op = ref->extent_op;
531 } else {
532 if (ref->extent_op->update_key) {
533 memcpy(&existing_ref->extent_op->key,
534 &ref->extent_op->key,
535 sizeof(ref->extent_op->key));
536 existing_ref->extent_op->update_key = 1;
537 }
538 if (ref->extent_op->update_flags) {
539 existing_ref->extent_op->flags_to_set |=
540 ref->extent_op->flags_to_set;
541 existing_ref->extent_op->update_flags = 1;
542 }
Miao Xie78a61842012-11-21 02:21:28 +0000543 btrfs_free_delayed_extent_op(ref->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400544 }
545 }
Chris Mason56bec292009-03-13 10:10:06 -0400546 /*
Josef Bacikd7df2c72014-01-23 09:21:38 -0500547 * update the reference mod on the head to reflect this new operation,
548 * only need the lock for this case cause we could be processing it
549 * currently, for refs we just added we know we're a-ok.
Chris Mason56bec292009-03-13 10:10:06 -0400550 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500551 spin_lock(&existing_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -0400552 existing->ref_mod += update->ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500553 spin_unlock(&existing_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -0400554}
555
556/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400557 * helper function to actually insert a head node into the rbtree.
Chris Mason56bec292009-03-13 10:10:06 -0400558 * this does all the dirty work in terms of maintaining the correct
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400559 * overall modification count.
Chris Mason56bec292009-03-13 10:10:06 -0400560 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500561static noinline struct btrfs_delayed_ref_head *
562add_delayed_ref_head(struct btrfs_fs_info *fs_info,
563 struct btrfs_trans_handle *trans,
564 struct btrfs_delayed_ref_node *ref, u64 bytenr,
565 u64 num_bytes, int action, int is_data)
Chris Mason56bec292009-03-13 10:10:06 -0400566{
Josef Bacikd7df2c72014-01-23 09:21:38 -0500567 struct btrfs_delayed_ref_head *existing;
Chris Masonc3e69d52009-03-13 10:17:05 -0400568 struct btrfs_delayed_ref_head *head_ref = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400569 struct btrfs_delayed_ref_root *delayed_refs;
570 int count_mod = 1;
571 int must_insert_reserved = 0;
572
573 /*
574 * the head node stores the sum of all the mods, so dropping a ref
575 * should drop the sum in the head node by one.
576 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400577 if (action == BTRFS_UPDATE_DELAYED_HEAD)
578 count_mod = 0;
579 else if (action == BTRFS_DROP_DELAYED_REF)
580 count_mod = -1;
Chris Mason56bec292009-03-13 10:10:06 -0400581
582 /*
583 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
584 * the reserved accounting when the extent is finally added, or
585 * if a later modification deletes the delayed ref without ever
586 * inserting the extent into the extent allocation tree.
587 * ref->must_insert_reserved is the flag used to record
588 * that accounting mods are required.
589 *
590 * Once we record must_insert_reserved, switch the action to
591 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
592 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400593 if (action == BTRFS_ADD_DELAYED_EXTENT)
Chris Mason56bec292009-03-13 10:10:06 -0400594 must_insert_reserved = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400595 else
Chris Mason56bec292009-03-13 10:10:06 -0400596 must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400597
598 delayed_refs = &trans->transaction->delayed_refs;
599
600 /* first set the basic ref node struct up */
601 atomic_set(&ref->refs, 1);
602 ref->bytenr = bytenr;
Chris Mason56bec292009-03-13 10:10:06 -0400603 ref->num_bytes = num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400604 ref->ref_mod = count_mod;
605 ref->type = 0;
606 ref->action = 0;
607 ref->is_head = 1;
608 ref->in_tree = 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200609 ref->seq = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400610
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400611 head_ref = btrfs_delayed_node_to_head(ref);
612 head_ref->must_insert_reserved = must_insert_reserved;
613 head_ref->is_data = is_data;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500614 head_ref->ref_root = RB_ROOT;
615 head_ref->processing = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400616
Josef Bacikd7df2c72014-01-23 09:21:38 -0500617 spin_lock_init(&head_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400618 mutex_init(&head_ref->mutex);
619
Liu Bo599c75e2013-07-16 19:03:36 +0800620 trace_add_delayed_ref_head(ref, head_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000621
Josef Bacikd7df2c72014-01-23 09:21:38 -0500622 existing = htree_insert(&delayed_refs->href_root,
623 &head_ref->href_node);
Chris Mason56bec292009-03-13 10:10:06 -0400624 if (existing) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500625 update_existing_head_ref(&existing->node, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400626 /*
627 * we've updated the existing ref, free the newly
628 * allocated ref
629 */
Miao Xie78a61842012-11-21 02:21:28 +0000630 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500631 head_ref = existing;
Chris Mason56bec292009-03-13 10:10:06 -0400632 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400633 delayed_refs->num_heads++;
634 delayed_refs->num_heads_ready++;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500635 atomic_inc(&delayed_refs->num_entries);
Chris Mason56bec292009-03-13 10:10:06 -0400636 trans->delayed_ref_updates++;
637 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500638 return head_ref;
Chris Mason56bec292009-03-13 10:10:06 -0400639}
640
641/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400642 * helper to insert a delayed tree ref into the rbtree.
643 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500644static noinline void
645add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
646 struct btrfs_trans_handle *trans,
647 struct btrfs_delayed_ref_head *head_ref,
648 struct btrfs_delayed_ref_node *ref, u64 bytenr,
649 u64 num_bytes, u64 parent, u64 ref_root, int level,
650 int action, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400651{
652 struct btrfs_delayed_ref_node *existing;
653 struct btrfs_delayed_tree_ref *full_ref;
654 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200655 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400656
657 if (action == BTRFS_ADD_DELAYED_EXTENT)
658 action = BTRFS_ADD_DELAYED_REF;
659
660 delayed_refs = &trans->transaction->delayed_refs;
661
662 /* first set the basic ref node struct up */
663 atomic_set(&ref->refs, 1);
664 ref->bytenr = bytenr;
665 ref->num_bytes = num_bytes;
666 ref->ref_mod = 1;
667 ref->action = action;
668 ref->is_head = 0;
669 ref->in_tree = 1;
670
Jan Schmidt546adb02012-06-14 16:37:44 +0200671 if (need_ref_seq(for_cow, ref_root))
672 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
Arne Jansen00f04b82011-09-14 12:37:00 +0200673 ref->seq = seq;
674
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400675 full_ref = btrfs_delayed_node_to_tree_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200676 full_ref->parent = parent;
677 full_ref->root = ref_root;
678 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400679 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200680 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400681 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 full_ref->level = level;
683
Liu Bo599c75e2013-07-16 19:03:36 +0800684 trace_add_delayed_tree_ref(ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000685
Josef Bacikd7df2c72014-01-23 09:21:38 -0500686 spin_lock(&head_ref->lock);
687 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400688 if (existing) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500689 update_existing_ref(trans, delayed_refs, head_ref, existing,
690 ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 /*
692 * we've updated the existing ref, free the newly
693 * allocated ref
694 */
Miao Xie78a61842012-11-21 02:21:28 +0000695 kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 } else {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500697 atomic_inc(&delayed_refs->num_entries);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400698 trans->delayed_ref_updates++;
699 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500700 spin_unlock(&head_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400701}
702
703/*
704 * helper to insert a delayed data ref into the rbtree.
705 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500706static noinline void
707add_delayed_data_ref(struct btrfs_fs_info *fs_info,
708 struct btrfs_trans_handle *trans,
709 struct btrfs_delayed_ref_head *head_ref,
710 struct btrfs_delayed_ref_node *ref, u64 bytenr,
711 u64 num_bytes, u64 parent, u64 ref_root, u64 owner,
712 u64 offset, int action, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400713{
714 struct btrfs_delayed_ref_node *existing;
715 struct btrfs_delayed_data_ref *full_ref;
716 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200717 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400718
719 if (action == BTRFS_ADD_DELAYED_EXTENT)
720 action = BTRFS_ADD_DELAYED_REF;
721
722 delayed_refs = &trans->transaction->delayed_refs;
723
724 /* first set the basic ref node struct up */
725 atomic_set(&ref->refs, 1);
726 ref->bytenr = bytenr;
727 ref->num_bytes = num_bytes;
728 ref->ref_mod = 1;
729 ref->action = action;
730 ref->is_head = 0;
731 ref->in_tree = 1;
732
Jan Schmidt546adb02012-06-14 16:37:44 +0200733 if (need_ref_seq(for_cow, ref_root))
734 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
Arne Jansen00f04b82011-09-14 12:37:00 +0200735 ref->seq = seq;
736
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400737 full_ref = btrfs_delayed_node_to_data_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200738 full_ref->parent = parent;
739 full_ref->root = ref_root;
740 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400741 ref->type = BTRFS_SHARED_DATA_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200742 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400743 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200744
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400745 full_ref->objectid = owner;
746 full_ref->offset = offset;
747
Liu Bo599c75e2013-07-16 19:03:36 +0800748 trace_add_delayed_data_ref(ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000749
Josef Bacikd7df2c72014-01-23 09:21:38 -0500750 spin_lock(&head_ref->lock);
751 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400752 if (existing) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500753 update_existing_ref(trans, delayed_refs, head_ref, existing,
754 ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400755 /*
756 * we've updated the existing ref, free the newly
757 * allocated ref
758 */
Miao Xie78a61842012-11-21 02:21:28 +0000759 kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400760 } else {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500761 atomic_inc(&delayed_refs->num_entries);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400762 trans->delayed_ref_updates++;
763 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500764 spin_unlock(&head_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400765}
766
767/*
768 * add a delayed tree ref. This does all of the accounting required
Chris Mason56bec292009-03-13 10:10:06 -0400769 * to make sure the delayed ref is eventually processed before this
770 * transaction commits.
771 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200772int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
773 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400774 u64 bytenr, u64 num_bytes, u64 parent,
775 u64 ref_root, int level, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200776 struct btrfs_delayed_extent_op *extent_op,
777 int for_cow)
Chris Mason56bec292009-03-13 10:10:06 -0400778{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400779 struct btrfs_delayed_tree_ref *ref;
Chris Mason56bec292009-03-13 10:10:06 -0400780 struct btrfs_delayed_ref_head *head_ref;
781 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400782
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400783 BUG_ON(extent_op && extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000784 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400785 if (!ref)
786 return -ENOMEM;
787
Miao Xie78a61842012-11-21 02:21:28 +0000788 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400789 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000790 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400791 return -ENOMEM;
792 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400793
794 head_ref->extent_op = extent_op;
795
Chris Mason56bec292009-03-13 10:10:06 -0400796 delayed_refs = &trans->transaction->delayed_refs;
797 spin_lock(&delayed_refs->lock);
798
799 /*
800 * insert both the head node and the new ref without dropping
801 * the spin lock
802 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500803 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
804 bytenr, num_bytes, action, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400805
Josef Bacikd7df2c72014-01-23 09:21:38 -0500806 add_delayed_tree_ref(fs_info, trans, head_ref, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200807 num_bytes, parent, ref_root, level, action,
808 for_cow);
Chris Mason56bec292009-03-13 10:10:06 -0400809 spin_unlock(&delayed_refs->lock);
Jan Schmidt546adb02012-06-14 16:37:44 +0200810 if (need_ref_seq(for_cow, ref_root))
811 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
Jan Schmidt95a06072012-05-29 17:06:54 +0200812
Chris Mason56bec292009-03-13 10:10:06 -0400813 return 0;
814}
815
816/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400817 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
818 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200819int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
820 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821 u64 bytenr, u64 num_bytes,
822 u64 parent, u64 ref_root,
823 u64 owner, u64 offset, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200824 struct btrfs_delayed_extent_op *extent_op,
825 int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400826{
827 struct btrfs_delayed_data_ref *ref;
828 struct btrfs_delayed_ref_head *head_ref;
829 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830
831 BUG_ON(extent_op && !extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000832 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400833 if (!ref)
834 return -ENOMEM;
835
Miao Xie78a61842012-11-21 02:21:28 +0000836 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400837 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000838 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839 return -ENOMEM;
840 }
841
842 head_ref->extent_op = extent_op;
843
844 delayed_refs = &trans->transaction->delayed_refs;
845 spin_lock(&delayed_refs->lock);
846
847 /*
848 * insert both the head node and the new ref without dropping
849 * the spin lock
850 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500851 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
852 bytenr, num_bytes, action, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853
Josef Bacikd7df2c72014-01-23 09:21:38 -0500854 add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200855 num_bytes, parent, ref_root, owner, offset,
856 action, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 spin_unlock(&delayed_refs->lock);
Jan Schmidt546adb02012-06-14 16:37:44 +0200858 if (need_ref_seq(for_cow, ref_root))
859 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
Jan Schmidt95a06072012-05-29 17:06:54 +0200860
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400861 return 0;
862}
863
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200864int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
865 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 u64 bytenr, u64 num_bytes,
867 struct btrfs_delayed_extent_op *extent_op)
868{
869 struct btrfs_delayed_ref_head *head_ref;
870 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400871
Miao Xie78a61842012-11-21 02:21:28 +0000872 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400873 if (!head_ref)
874 return -ENOMEM;
875
876 head_ref->extent_op = extent_op;
877
878 delayed_refs = &trans->transaction->delayed_refs;
879 spin_lock(&delayed_refs->lock);
880
Jeff Mahoney143bede2012-03-01 14:56:26 +0100881 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400882 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
883 extent_op->is_data);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884
885 spin_unlock(&delayed_refs->lock);
886 return 0;
887}
888
889/*
Chris Mason1887be62009-03-13 10:11:24 -0400890 * this does a simple search for the head node for a given extent.
891 * It must be called with the delayed ref spinlock held, and it returns
892 * the head node if any where found, or NULL if not.
893 */
894struct btrfs_delayed_ref_head *
895btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
896{
Chris Mason1887be62009-03-13 10:11:24 -0400897 struct btrfs_delayed_ref_root *delayed_refs;
898
899 delayed_refs = &trans->transaction->delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +0800900 return find_ref_head(&delayed_refs->href_root, bytenr, NULL, 0);
Chris Mason1887be62009-03-13 10:11:24 -0400901}
Miao Xie78a61842012-11-21 02:21:28 +0000902
903void btrfs_delayed_ref_exit(void)
904{
905 if (btrfs_delayed_ref_head_cachep)
906 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
907 if (btrfs_delayed_tree_ref_cachep)
908 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
909 if (btrfs_delayed_data_ref_cachep)
910 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
911 if (btrfs_delayed_extent_op_cachep)
912 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
913}
914
915int btrfs_delayed_ref_init(void)
916{
917 btrfs_delayed_ref_head_cachep = kmem_cache_create(
918 "btrfs_delayed_ref_head",
919 sizeof(struct btrfs_delayed_ref_head), 0,
920 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
921 if (!btrfs_delayed_ref_head_cachep)
922 goto fail;
923
924 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
925 "btrfs_delayed_tree_ref",
926 sizeof(struct btrfs_delayed_tree_ref), 0,
927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
928 if (!btrfs_delayed_tree_ref_cachep)
929 goto fail;
930
931 btrfs_delayed_data_ref_cachep = kmem_cache_create(
932 "btrfs_delayed_data_ref",
933 sizeof(struct btrfs_delayed_data_ref), 0,
934 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
935 if (!btrfs_delayed_data_ref_cachep)
936 goto fail;
937
938 btrfs_delayed_extent_op_cachep = kmem_cache_create(
939 "btrfs_delayed_extent_op",
940 sizeof(struct btrfs_delayed_extent_op), 0,
941 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
942 if (!btrfs_delayed_extent_op_cachep)
943 goto fail;
944
945 return 0;
946fail:
947 btrfs_delayed_ref_exit();
948 return -ENOMEM;
949}