blob: 8f8ed7d20bac5f4e058fc693d0fdc24fde7abeda [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;
Josef Bacikfcebe452014-05-13 17:30:47 -0700109 if (ref1->no_quota > ref2->no_quota)
110 return 1;
111 if (ref1->no_quota < ref2->no_quota)
112 return -1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200113 /* merging of sequenced refs is not allowed */
Josef Bacikae1e2062012-08-07 16:00:32 -0400114 if (compare_seq) {
115 if (ref1->seq < ref2->seq)
116 return -1;
117 if (ref1->seq > ref2->seq)
118 return 1;
119 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400120 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
121 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
122 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
Josef Bacik41b0fc42013-04-01 20:36:28 -0400123 btrfs_delayed_node_to_tree_ref(ref1),
124 ref1->type);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400125 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
126 ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
127 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),
128 btrfs_delayed_node_to_data_ref(ref1));
129 }
130 BUG();
Chris Mason56bec292009-03-13 10:10:06 -0400131 return 0;
132}
133
134/*
135 * insert a new ref into the rbtree. This returns any existing refs
136 * for the same (bytenr,parent) tuple, or NULL if the new node was properly
137 * inserted.
138 */
139static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
Chris Mason56bec292009-03-13 10:10:06 -0400140 struct rb_node *node)
141{
142 struct rb_node **p = &root->rb_node;
143 struct rb_node *parent_node = NULL;
144 struct btrfs_delayed_ref_node *entry;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400145 struct btrfs_delayed_ref_node *ins;
Chris Mason56bec292009-03-13 10:10:06 -0400146 int cmp;
147
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400148 ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Chris Mason56bec292009-03-13 10:10:06 -0400149 while (*p) {
150 parent_node = *p;
151 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
152 rb_node);
153
Josef Bacikae1e2062012-08-07 16:00:32 -0400154 cmp = comp_entry(entry, ins, 1);
Chris Mason56bec292009-03-13 10:10:06 -0400155 if (cmp < 0)
156 p = &(*p)->rb_left;
157 else if (cmp > 0)
158 p = &(*p)->rb_right;
159 else
160 return entry;
161 }
162
Chris Mason56bec292009-03-13 10:10:06 -0400163 rb_link_node(node, parent_node, p);
164 rb_insert_color(node, root);
165 return NULL;
166}
167
Liu Boc46effa2013-10-14 12:59:45 +0800168/* insert a new ref to head ref rbtree */
169static struct btrfs_delayed_ref_head *htree_insert(struct rb_root *root,
170 struct rb_node *node)
171{
172 struct rb_node **p = &root->rb_node;
173 struct rb_node *parent_node = NULL;
174 struct btrfs_delayed_ref_head *entry;
175 struct btrfs_delayed_ref_head *ins;
176 u64 bytenr;
177
178 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
179 bytenr = ins->node.bytenr;
180 while (*p) {
181 parent_node = *p;
182 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
183 href_node);
184
185 if (bytenr < entry->node.bytenr)
186 p = &(*p)->rb_left;
187 else if (bytenr > entry->node.bytenr)
188 p = &(*p)->rb_right;
189 else
190 return entry;
191 }
192
193 rb_link_node(node, parent_node, p);
194 rb_insert_color(node, root);
195 return NULL;
196}
197
Chris Mason56bec292009-03-13 10:10:06 -0400198/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400199 * find an head entry based on bytenr. This returns the delayed ref
Arne Jansend1270cd2011-09-13 15:16:43 +0200200 * head if it was able to find one, or NULL if nothing was in that spot.
201 * If return_bigger is given, the next bigger entry is returned if no exact
202 * match is found.
Chris Mason56bec292009-03-13 10:10:06 -0400203 */
Liu Boc46effa2013-10-14 12:59:45 +0800204static struct btrfs_delayed_ref_head *
205find_ref_head(struct rb_root *root, u64 bytenr,
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000206 int return_bigger)
Chris Mason56bec292009-03-13 10:10:06 -0400207{
Arne Jansend1270cd2011-09-13 15:16:43 +0200208 struct rb_node *n;
Liu Boc46effa2013-10-14 12:59:45 +0800209 struct btrfs_delayed_ref_head *entry;
Chris Mason56bec292009-03-13 10:10:06 -0400210
Arne Jansend1270cd2011-09-13 15:16:43 +0200211 n = root->rb_node;
212 entry = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400213 while (n) {
Liu Boc46effa2013-10-14 12:59:45 +0800214 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
Chris Mason56bec292009-03-13 10:10:06 -0400215
Liu Boc46effa2013-10-14 12:59:45 +0800216 if (bytenr < entry->node.bytenr)
Chris Mason56bec292009-03-13 10:10:06 -0400217 n = n->rb_left;
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000218 else if (bytenr > entry->node.bytenr)
Chris Mason56bec292009-03-13 10:10:06 -0400219 n = n->rb_right;
220 else
221 return entry;
222 }
Arne Jansend1270cd2011-09-13 15:16:43 +0200223 if (entry && return_bigger) {
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000224 if (bytenr > entry->node.bytenr) {
Liu Boc46effa2013-10-14 12:59:45 +0800225 n = rb_next(&entry->href_node);
Arne Jansend1270cd2011-09-13 15:16:43 +0200226 if (!n)
227 n = rb_first(root);
Liu Boc46effa2013-10-14 12:59:45 +0800228 entry = rb_entry(n, struct btrfs_delayed_ref_head,
229 href_node);
Filipe Manana6103fb42014-02-12 15:07:52 +0000230 return entry;
Arne Jansend1270cd2011-09-13 15:16:43 +0200231 }
232 return entry;
233 }
Chris Mason56bec292009-03-13 10:10:06 -0400234 return NULL;
235}
236
Chris Masonc3e69d52009-03-13 10:17:05 -0400237int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
238 struct btrfs_delayed_ref_head *head)
Chris Mason56bec292009-03-13 10:10:06 -0400239{
Chris Masonc3e69d52009-03-13 10:17:05 -0400240 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400241
Chris Masonc3e69d52009-03-13 10:17:05 -0400242 delayed_refs = &trans->transaction->delayed_refs;
243 assert_spin_locked(&delayed_refs->lock);
244 if (mutex_trylock(&head->mutex))
245 return 0;
246
247 atomic_inc(&head->node.refs);
248 spin_unlock(&delayed_refs->lock);
249
250 mutex_lock(&head->mutex);
251 spin_lock(&delayed_refs->lock);
252 if (!head->node.in_tree) {
253 mutex_unlock(&head->mutex);
254 btrfs_put_delayed_ref(&head->node);
255 return -EAGAIN;
256 }
257 btrfs_put_delayed_ref(&head->node);
258 return 0;
259}
260
Stefan Behrens35a36212013-08-14 18:12:25 +0200261static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
Josef Bacikae1e2062012-08-07 16:00:32 -0400262 struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500263 struct btrfs_delayed_ref_head *head,
Josef Bacikae1e2062012-08-07 16:00:32 -0400264 struct btrfs_delayed_ref_node *ref)
265{
Liu Boc46effa2013-10-14 12:59:45 +0800266 if (btrfs_delayed_ref_is_head(ref)) {
Liu Boc46effa2013-10-14 12:59:45 +0800267 head = btrfs_delayed_node_to_head(ref);
268 rb_erase(&head->href_node, &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500269 } else {
270 assert_spin_locked(&head->lock);
271 rb_erase(&ref->rb_node, &head->ref_root);
Liu Boc46effa2013-10-14 12:59:45 +0800272 }
Josef Bacikae1e2062012-08-07 16:00:32 -0400273 ref->in_tree = 0;
274 btrfs_put_delayed_ref(ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500275 atomic_dec(&delayed_refs->num_entries);
Josef Bacikae1e2062012-08-07 16:00:32 -0400276 if (trans->delayed_ref_updates)
277 trans->delayed_ref_updates--;
278}
279
280static int merge_ref(struct btrfs_trans_handle *trans,
281 struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500282 struct btrfs_delayed_ref_head *head,
Josef Bacikae1e2062012-08-07 16:00:32 -0400283 struct btrfs_delayed_ref_node *ref, u64 seq)
284{
285 struct rb_node *node;
Josef Bacikae1e2062012-08-07 16:00:32 -0400286 int mod = 0;
287 int done = 0;
288
Josef Bacikd7df2c72014-01-23 09:21:38 -0500289 node = rb_next(&ref->rb_node);
290 while (!done && node) {
Josef Bacikae1e2062012-08-07 16:00:32 -0400291 struct btrfs_delayed_ref_node *next;
292
293 next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500294 node = rb_next(node);
Josef Bacikae1e2062012-08-07 16:00:32 -0400295 if (seq && next->seq >= seq)
296 break;
297 if (comp_entry(ref, next, 0))
298 continue;
299
300 if (ref->action == next->action) {
301 mod = next->ref_mod;
302 } else {
303 if (ref->ref_mod < next->ref_mod) {
304 struct btrfs_delayed_ref_node *tmp;
305
306 tmp = ref;
307 ref = next;
308 next = tmp;
309 done = 1;
310 }
311 mod = -next->ref_mod;
312 }
313
Josef Bacikd7df2c72014-01-23 09:21:38 -0500314 drop_delayed_ref(trans, delayed_refs, head, next);
Josef Bacikae1e2062012-08-07 16:00:32 -0400315 ref->ref_mod += mod;
316 if (ref->ref_mod == 0) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500317 drop_delayed_ref(trans, delayed_refs, head, ref);
318 done = 1;
Josef Bacikae1e2062012-08-07 16:00:32 -0400319 } else {
320 /*
321 * You can't have multiples of the same ref on a tree
322 * block.
323 */
324 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
325 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
326 }
Josef Bacikae1e2062012-08-07 16:00:32 -0400327 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500328 return done;
Josef Bacikae1e2062012-08-07 16:00:32 -0400329}
330
331void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
332 struct btrfs_fs_info *fs_info,
333 struct btrfs_delayed_ref_root *delayed_refs,
334 struct btrfs_delayed_ref_head *head)
335{
336 struct rb_node *node;
337 u64 seq = 0;
338
Josef Bacikd7df2c72014-01-23 09:21:38 -0500339 assert_spin_locked(&head->lock);
Liu Bo9e5ac132013-10-14 12:59:43 +0800340 /*
341 * We don't have too much refs to merge in the case of delayed data
342 * refs.
343 */
344 if (head->is_data)
345 return;
346
Josef Bacikae1e2062012-08-07 16:00:32 -0400347 spin_lock(&fs_info->tree_mod_seq_lock);
348 if (!list_empty(&fs_info->tree_mod_seq_list)) {
349 struct seq_list *elem;
350
351 elem = list_first_entry(&fs_info->tree_mod_seq_list,
352 struct seq_list, list);
353 seq = elem->seq;
354 }
355 spin_unlock(&fs_info->tree_mod_seq_lock);
356
Josef Bacikd7df2c72014-01-23 09:21:38 -0500357 node = rb_first(&head->ref_root);
Josef Bacikae1e2062012-08-07 16:00:32 -0400358 while (node) {
359 struct btrfs_delayed_ref_node *ref;
360
361 ref = rb_entry(node, struct btrfs_delayed_ref_node,
362 rb_node);
Josef Bacikae1e2062012-08-07 16:00:32 -0400363 /* We can't merge refs that are outside of our seq count */
364 if (seq && ref->seq >= seq)
365 break;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500366 if (merge_ref(trans, delayed_refs, head, ref, seq))
367 node = rb_first(&head->ref_root);
Josef Bacikae1e2062012-08-07 16:00:32 -0400368 else
Josef Bacikd7df2c72014-01-23 09:21:38 -0500369 node = rb_next(&ref->rb_node);
Josef Bacikae1e2062012-08-07 16:00:32 -0400370 }
371}
372
Jan Schmidt097b8a72012-06-21 11:08:04 +0200373int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
374 struct btrfs_delayed_ref_root *delayed_refs,
Arne Jansen00f04b82011-09-14 12:37:00 +0200375 u64 seq)
376{
377 struct seq_list *elem;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200378 int ret = 0;
Arne Jansen00f04b82011-09-14 12:37:00 +0200379
Jan Schmidt097b8a72012-06-21 11:08:04 +0200380 spin_lock(&fs_info->tree_mod_seq_lock);
381 if (!list_empty(&fs_info->tree_mod_seq_list)) {
382 elem = list_first_entry(&fs_info->tree_mod_seq_list,
383 struct seq_list, list);
384 if (seq >= elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000385 pr_debug("holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)\n",
386 (u32)(seq >> 32), (u32)seq,
387 (u32)(elem->seq >> 32), (u32)elem->seq,
388 delayed_refs);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200389 ret = 1;
390 }
Arne Jansen00f04b82011-09-14 12:37:00 +0200391 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200392
393 spin_unlock(&fs_info->tree_mod_seq_lock);
394 return ret;
Arne Jansen00f04b82011-09-14 12:37:00 +0200395}
396
Josef Bacikd7df2c72014-01-23 09:21:38 -0500397struct btrfs_delayed_ref_head *
398btrfs_select_ref_head(struct btrfs_trans_handle *trans)
Chris Masonc3e69d52009-03-13 10:17:05 -0400399{
Chris Masonc3e69d52009-03-13 10:17:05 -0400400 struct btrfs_delayed_ref_root *delayed_refs;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500401 struct btrfs_delayed_ref_head *head;
402 u64 start;
403 bool loop = false;
Chris Masonc3e69d52009-03-13 10:17:05 -0400404
405 delayed_refs = &trans->transaction->delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +0800406
Chris Masonc3e69d52009-03-13 10:17:05 -0400407again:
Josef Bacikd7df2c72014-01-23 09:21:38 -0500408 start = delayed_refs->run_delayed_start;
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000409 head = find_ref_head(&delayed_refs->href_root, start, 1);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500410 if (!head && !loop) {
411 delayed_refs->run_delayed_start = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -0400412 start = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500413 loop = true;
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000414 head = find_ref_head(&delayed_refs->href_root, start, 1);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500415 if (!head)
416 return NULL;
417 } else if (!head && loop) {
418 return NULL;
Chris Masonc3e69d52009-03-13 10:17:05 -0400419 }
Chris Mason56bec292009-03-13 10:10:06 -0400420
Josef Bacikd7df2c72014-01-23 09:21:38 -0500421 while (head->processing) {
422 struct rb_node *node;
Miao Xie093486c2012-12-19 08:10:10 +0000423
Josef Bacikd7df2c72014-01-23 09:21:38 -0500424 node = rb_next(&head->href_node);
425 if (!node) {
426 if (loop)
427 return NULL;
428 delayed_refs->run_delayed_start = 0;
429 start = 0;
430 loop = true;
431 goto again;
432 }
433 head = rb_entry(node, struct btrfs_delayed_ref_head,
434 href_node);
435 }
436
437 head->processing = 1;
438 WARN_ON(delayed_refs->num_heads_ready == 0);
439 delayed_refs->num_heads_ready--;
440 delayed_refs->run_delayed_start = head->node.bytenr +
441 head->node.num_bytes;
442 return head;
Miao Xie093486c2012-12-19 08:10:10 +0000443}
444
Chris Mason56bec292009-03-13 10:10:06 -0400445/*
Chris Mason56bec292009-03-13 10:10:06 -0400446 * helper function to update an extent delayed ref in the
447 * rbtree. existing and update must both have the same
448 * bytenr and parent
449 *
450 * This may free existing if the update cancels out whatever
451 * operation it was doing.
452 */
453static noinline void
454update_existing_ref(struct btrfs_trans_handle *trans,
455 struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500456 struct btrfs_delayed_ref_head *head,
Chris Mason56bec292009-03-13 10:10:06 -0400457 struct btrfs_delayed_ref_node *existing,
458 struct btrfs_delayed_ref_node *update)
459{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400460 if (update->action != existing->action) {
Chris Mason56bec292009-03-13 10:10:06 -0400461 /*
462 * this is effectively undoing either an add or a
463 * drop. We decrement the ref_mod, and if it goes
464 * down to zero we just delete the entry without
465 * every changing the extent allocation tree.
466 */
467 existing->ref_mod--;
Josef Bacikae1e2062012-08-07 16:00:32 -0400468 if (existing->ref_mod == 0)
Josef Bacikd7df2c72014-01-23 09:21:38 -0500469 drop_delayed_ref(trans, delayed_refs, head, existing);
Josef Bacikae1e2062012-08-07 16:00:32 -0400470 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400471 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
472 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400473 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400474 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
475 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400476 /*
477 * the action on the existing ref matches
478 * the action on the ref we're trying to add.
479 * Bump the ref_mod by one so the backref that
480 * is eventually added/removed has the correct
481 * reference count
482 */
483 existing->ref_mod += update->ref_mod;
484 }
485}
486
487/*
488 * helper function to update the accounting in the head ref
489 * existing and update must have the same bytenr
490 */
491static noinline void
Josef Bacik12621332015-02-03 07:50:16 -0800492update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
493 struct btrfs_delayed_ref_node *existing,
Chris Mason56bec292009-03-13 10:10:06 -0400494 struct btrfs_delayed_ref_node *update)
495{
496 struct btrfs_delayed_ref_head *existing_ref;
497 struct btrfs_delayed_ref_head *ref;
Josef Bacik12621332015-02-03 07:50:16 -0800498 int old_ref_mod;
Chris Mason56bec292009-03-13 10:10:06 -0400499
500 existing_ref = btrfs_delayed_node_to_head(existing);
501 ref = btrfs_delayed_node_to_head(update);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400502 BUG_ON(existing_ref->is_data != ref->is_data);
Chris Mason56bec292009-03-13 10:10:06 -0400503
Filipe Manana21543ba2014-03-14 20:55:01 +0000504 spin_lock(&existing_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -0400505 if (ref->must_insert_reserved) {
506 /* if the extent was freed and then
507 * reallocated before the delayed ref
508 * entries were processed, we can end up
509 * with an existing head ref without
510 * the must_insert_reserved flag set.
511 * Set it again here
512 */
513 existing_ref->must_insert_reserved = ref->must_insert_reserved;
514
515 /*
516 * update the num_bytes so we make sure the accounting
517 * is done correctly
518 */
519 existing->num_bytes = update->num_bytes;
520
521 }
522
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400523 if (ref->extent_op) {
524 if (!existing_ref->extent_op) {
525 existing_ref->extent_op = ref->extent_op;
526 } else {
527 if (ref->extent_op->update_key) {
528 memcpy(&existing_ref->extent_op->key,
529 &ref->extent_op->key,
530 sizeof(ref->extent_op->key));
531 existing_ref->extent_op->update_key = 1;
532 }
533 if (ref->extent_op->update_flags) {
534 existing_ref->extent_op->flags_to_set |=
535 ref->extent_op->flags_to_set;
536 existing_ref->extent_op->update_flags = 1;
537 }
Miao Xie78a61842012-11-21 02:21:28 +0000538 btrfs_free_delayed_extent_op(ref->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400539 }
540 }
Chris Mason56bec292009-03-13 10:10:06 -0400541 /*
Josef Bacikd7df2c72014-01-23 09:21:38 -0500542 * update the reference mod on the head to reflect this new operation,
543 * only need the lock for this case cause we could be processing it
544 * currently, for refs we just added we know we're a-ok.
Chris Mason56bec292009-03-13 10:10:06 -0400545 */
Josef Bacik12621332015-02-03 07:50:16 -0800546 old_ref_mod = existing_ref->total_ref_mod;
Chris Mason56bec292009-03-13 10:10:06 -0400547 existing->ref_mod += update->ref_mod;
Josef Bacik12621332015-02-03 07:50:16 -0800548 existing_ref->total_ref_mod += update->ref_mod;
549
550 /*
551 * If we are going to from a positive ref mod to a negative or vice
552 * versa we need to make sure to adjust pending_csums accordingly.
553 */
554 if (existing_ref->is_data) {
555 if (existing_ref->total_ref_mod >= 0 && old_ref_mod < 0)
556 delayed_refs->pending_csums -= existing->num_bytes;
557 if (existing_ref->total_ref_mod < 0 && old_ref_mod >= 0)
558 delayed_refs->pending_csums += existing->num_bytes;
559 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500560 spin_unlock(&existing_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -0400561}
562
563/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400564 * helper function to actually insert a head node into the rbtree.
Chris Mason56bec292009-03-13 10:10:06 -0400565 * this does all the dirty work in terms of maintaining the correct
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400566 * overall modification count.
Chris Mason56bec292009-03-13 10:10:06 -0400567 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500568static noinline struct btrfs_delayed_ref_head *
569add_delayed_ref_head(struct btrfs_fs_info *fs_info,
570 struct btrfs_trans_handle *trans,
571 struct btrfs_delayed_ref_node *ref, u64 bytenr,
572 u64 num_bytes, int action, int is_data)
Chris Mason56bec292009-03-13 10:10:06 -0400573{
Josef Bacikd7df2c72014-01-23 09:21:38 -0500574 struct btrfs_delayed_ref_head *existing;
Chris Masonc3e69d52009-03-13 10:17:05 -0400575 struct btrfs_delayed_ref_head *head_ref = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400576 struct btrfs_delayed_ref_root *delayed_refs;
577 int count_mod = 1;
578 int must_insert_reserved = 0;
579
580 /*
581 * the head node stores the sum of all the mods, so dropping a ref
582 * should drop the sum in the head node by one.
583 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400584 if (action == BTRFS_UPDATE_DELAYED_HEAD)
585 count_mod = 0;
586 else if (action == BTRFS_DROP_DELAYED_REF)
587 count_mod = -1;
Chris Mason56bec292009-03-13 10:10:06 -0400588
589 /*
590 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
591 * the reserved accounting when the extent is finally added, or
592 * if a later modification deletes the delayed ref without ever
593 * inserting the extent into the extent allocation tree.
594 * ref->must_insert_reserved is the flag used to record
595 * that accounting mods are required.
596 *
597 * Once we record must_insert_reserved, switch the action to
598 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
599 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400600 if (action == BTRFS_ADD_DELAYED_EXTENT)
Chris Mason56bec292009-03-13 10:10:06 -0400601 must_insert_reserved = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400602 else
Chris Mason56bec292009-03-13 10:10:06 -0400603 must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400604
605 delayed_refs = &trans->transaction->delayed_refs;
606
607 /* first set the basic ref node struct up */
608 atomic_set(&ref->refs, 1);
609 ref->bytenr = bytenr;
Chris Mason56bec292009-03-13 10:10:06 -0400610 ref->num_bytes = num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400611 ref->ref_mod = count_mod;
612 ref->type = 0;
613 ref->action = 0;
614 ref->is_head = 1;
615 ref->in_tree = 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200616 ref->seq = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400617
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400618 head_ref = btrfs_delayed_node_to_head(ref);
619 head_ref->must_insert_reserved = must_insert_reserved;
620 head_ref->is_data = is_data;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500621 head_ref->ref_root = RB_ROOT;
622 head_ref->processing = 0;
Josef Bacik12621332015-02-03 07:50:16 -0800623 head_ref->total_ref_mod = count_mod;
Chris Mason56bec292009-03-13 10:10:06 -0400624
Josef Bacikd7df2c72014-01-23 09:21:38 -0500625 spin_lock_init(&head_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400626 mutex_init(&head_ref->mutex);
627
Liu Bo599c75e2013-07-16 19:03:36 +0800628 trace_add_delayed_ref_head(ref, head_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000629
Josef Bacikd7df2c72014-01-23 09:21:38 -0500630 existing = htree_insert(&delayed_refs->href_root,
631 &head_ref->href_node);
Chris Mason56bec292009-03-13 10:10:06 -0400632 if (existing) {
Josef Bacik12621332015-02-03 07:50:16 -0800633 update_existing_head_ref(delayed_refs, &existing->node, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400634 /*
635 * we've updated the existing ref, free the newly
636 * allocated ref
637 */
Miao Xie78a61842012-11-21 02:21:28 +0000638 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500639 head_ref = existing;
Chris Mason56bec292009-03-13 10:10:06 -0400640 } else {
Josef Bacik12621332015-02-03 07:50:16 -0800641 if (is_data && count_mod < 0)
642 delayed_refs->pending_csums += num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400643 delayed_refs->num_heads++;
644 delayed_refs->num_heads_ready++;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500645 atomic_inc(&delayed_refs->num_entries);
Chris Mason56bec292009-03-13 10:10:06 -0400646 trans->delayed_ref_updates++;
647 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500648 return head_ref;
Chris Mason56bec292009-03-13 10:10:06 -0400649}
650
651/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400652 * helper to insert a delayed tree ref into the rbtree.
653 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500654static noinline void
655add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
656 struct btrfs_trans_handle *trans,
657 struct btrfs_delayed_ref_head *head_ref,
658 struct btrfs_delayed_ref_node *ref, u64 bytenr,
659 u64 num_bytes, u64 parent, u64 ref_root, int level,
Josef Bacikfcebe452014-05-13 17:30:47 -0700660 int action, int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400661{
662 struct btrfs_delayed_ref_node *existing;
663 struct btrfs_delayed_tree_ref *full_ref;
664 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200665 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400666
667 if (action == BTRFS_ADD_DELAYED_EXTENT)
668 action = BTRFS_ADD_DELAYED_REF;
669
Josef Bacikfcebe452014-05-13 17:30:47 -0700670 if (is_fstree(ref_root))
671 seq = atomic64_read(&fs_info->tree_mod_seq);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 delayed_refs = &trans->transaction->delayed_refs;
673
674 /* first set the basic ref node struct up */
675 atomic_set(&ref->refs, 1);
676 ref->bytenr = bytenr;
677 ref->num_bytes = num_bytes;
678 ref->ref_mod = 1;
679 ref->action = action;
680 ref->is_head = 0;
681 ref->in_tree = 1;
Josef Bacikfcebe452014-05-13 17:30:47 -0700682 ref->no_quota = no_quota;
Arne Jansen00f04b82011-09-14 12:37:00 +0200683 ref->seq = seq;
684
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400685 full_ref = btrfs_delayed_node_to_tree_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200686 full_ref->parent = parent;
687 full_ref->root = ref_root;
688 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400689 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200690 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400691 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400692 full_ref->level = level;
693
Liu Bo599c75e2013-07-16 19:03:36 +0800694 trace_add_delayed_tree_ref(ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000695
Josef Bacikd7df2c72014-01-23 09:21:38 -0500696 spin_lock(&head_ref->lock);
697 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400698 if (existing) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500699 update_existing_ref(trans, delayed_refs, head_ref, existing,
700 ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400701 /*
702 * we've updated the existing ref, free the newly
703 * allocated ref
704 */
Miao Xie78a61842012-11-21 02:21:28 +0000705 kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400706 } else {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500707 atomic_inc(&delayed_refs->num_entries);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400708 trans->delayed_ref_updates++;
709 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500710 spin_unlock(&head_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400711}
712
713/*
714 * helper to insert a delayed data ref into the rbtree.
715 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500716static noinline void
717add_delayed_data_ref(struct btrfs_fs_info *fs_info,
718 struct btrfs_trans_handle *trans,
719 struct btrfs_delayed_ref_head *head_ref,
720 struct btrfs_delayed_ref_node *ref, u64 bytenr,
721 u64 num_bytes, u64 parent, u64 ref_root, u64 owner,
Josef Bacikfcebe452014-05-13 17:30:47 -0700722 u64 offset, int action, int no_quota)
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
Josef Bacikfcebe452014-05-13 17:30:47 -0700734 if (is_fstree(ref_root))
735 seq = atomic64_read(&fs_info->tree_mod_seq);
736
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400737 /* first set the basic ref node struct up */
738 atomic_set(&ref->refs, 1);
739 ref->bytenr = bytenr;
740 ref->num_bytes = num_bytes;
741 ref->ref_mod = 1;
742 ref->action = action;
743 ref->is_head = 0;
744 ref->in_tree = 1;
Josef Bacikfcebe452014-05-13 17:30:47 -0700745 ref->no_quota = no_quota;
Arne Jansen00f04b82011-09-14 12:37:00 +0200746 ref->seq = seq;
747
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400748 full_ref = btrfs_delayed_node_to_data_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200749 full_ref->parent = parent;
750 full_ref->root = ref_root;
751 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400752 ref->type = BTRFS_SHARED_DATA_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200753 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400754 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200755
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400756 full_ref->objectid = owner;
757 full_ref->offset = offset;
758
Liu Bo599c75e2013-07-16 19:03:36 +0800759 trace_add_delayed_data_ref(ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000760
Josef Bacikd7df2c72014-01-23 09:21:38 -0500761 spin_lock(&head_ref->lock);
762 existing = tree_insert(&head_ref->ref_root, &ref->rb_node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400763 if (existing) {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500764 update_existing_ref(trans, delayed_refs, head_ref, existing,
765 ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400766 /*
767 * we've updated the existing ref, free the newly
768 * allocated ref
769 */
Miao Xie78a61842012-11-21 02:21:28 +0000770 kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400771 } else {
Josef Bacikd7df2c72014-01-23 09:21:38 -0500772 atomic_inc(&delayed_refs->num_entries);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400773 trans->delayed_ref_updates++;
774 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500775 spin_unlock(&head_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400776}
777
778/*
779 * add a delayed tree ref. This does all of the accounting required
Chris Mason56bec292009-03-13 10:10:06 -0400780 * to make sure the delayed ref is eventually processed before this
781 * transaction commits.
782 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200783int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
784 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400785 u64 bytenr, u64 num_bytes, u64 parent,
786 u64 ref_root, int level, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200787 struct btrfs_delayed_extent_op *extent_op,
Josef Bacikfcebe452014-05-13 17:30:47 -0700788 int no_quota)
Chris Mason56bec292009-03-13 10:10:06 -0400789{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400790 struct btrfs_delayed_tree_ref *ref;
Chris Mason56bec292009-03-13 10:10:06 -0400791 struct btrfs_delayed_ref_head *head_ref;
792 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400793
Josef Bacikfcebe452014-05-13 17:30:47 -0700794 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
795 no_quota = 0;
796
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400797 BUG_ON(extent_op && extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000798 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400799 if (!ref)
800 return -ENOMEM;
801
Miao Xie78a61842012-11-21 02:21:28 +0000802 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400803 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000804 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400805 return -ENOMEM;
806 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400807
808 head_ref->extent_op = extent_op;
809
Chris Mason56bec292009-03-13 10:10:06 -0400810 delayed_refs = &trans->transaction->delayed_refs;
811 spin_lock(&delayed_refs->lock);
812
813 /*
814 * insert both the head node and the new ref without dropping
815 * the spin lock
816 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500817 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
818 bytenr, num_bytes, action, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400819
Josef Bacikd7df2c72014-01-23 09:21:38 -0500820 add_delayed_tree_ref(fs_info, trans, head_ref, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200821 num_bytes, parent, ref_root, level, action,
Josef Bacikfcebe452014-05-13 17:30:47 -0700822 no_quota);
Chris Mason56bec292009-03-13 10:10:06 -0400823 spin_unlock(&delayed_refs->lock);
Jan Schmidt95a06072012-05-29 17:06:54 +0200824
Chris Mason56bec292009-03-13 10:10:06 -0400825 return 0;
826}
827
828/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400829 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
830 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200831int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
832 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400833 u64 bytenr, u64 num_bytes,
834 u64 parent, u64 ref_root,
835 u64 owner, u64 offset, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200836 struct btrfs_delayed_extent_op *extent_op,
Josef Bacikfcebe452014-05-13 17:30:47 -0700837 int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838{
839 struct btrfs_delayed_data_ref *ref;
840 struct btrfs_delayed_ref_head *head_ref;
841 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400842
Josef Bacikfcebe452014-05-13 17:30:47 -0700843 if (!is_fstree(ref_root) || !fs_info->quota_enabled)
844 no_quota = 0;
845
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400846 BUG_ON(extent_op && !extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000847 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400848 if (!ref)
849 return -ENOMEM;
850
Miao Xie78a61842012-11-21 02:21:28 +0000851 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400852 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000853 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400854 return -ENOMEM;
855 }
856
857 head_ref->extent_op = extent_op;
858
859 delayed_refs = &trans->transaction->delayed_refs;
860 spin_lock(&delayed_refs->lock);
861
862 /*
863 * insert both the head node and the new ref without dropping
864 * the spin lock
865 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500866 head_ref = add_delayed_ref_head(fs_info, trans, &head_ref->node,
867 bytenr, num_bytes, action, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400868
Josef Bacikd7df2c72014-01-23 09:21:38 -0500869 add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200870 num_bytes, parent, ref_root, owner, offset,
Josef Bacikfcebe452014-05-13 17:30:47 -0700871 action, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400872 spin_unlock(&delayed_refs->lock);
Jan Schmidt95a06072012-05-29 17:06:54 +0200873
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 return 0;
875}
876
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200877int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
878 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400879 u64 bytenr, u64 num_bytes,
880 struct btrfs_delayed_extent_op *extent_op)
881{
882 struct btrfs_delayed_ref_head *head_ref;
883 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884
Miao Xie78a61842012-11-21 02:21:28 +0000885 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 if (!head_ref)
887 return -ENOMEM;
888
889 head_ref->extent_op = extent_op;
890
891 delayed_refs = &trans->transaction->delayed_refs;
892 spin_lock(&delayed_refs->lock);
893
Jeff Mahoney143bede2012-03-01 14:56:26 +0100894 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400895 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
896 extent_op->is_data);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400897
898 spin_unlock(&delayed_refs->lock);
899 return 0;
900}
901
902/*
Chris Mason1887be62009-03-13 10:11:24 -0400903 * this does a simple search for the head node for a given extent.
904 * It must be called with the delayed ref spinlock held, and it returns
905 * the head node if any where found, or NULL if not.
906 */
907struct btrfs_delayed_ref_head *
908btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
909{
Chris Mason1887be62009-03-13 10:11:24 -0400910 struct btrfs_delayed_ref_root *delayed_refs;
911
912 delayed_refs = &trans->transaction->delayed_refs;
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000913 return find_ref_head(&delayed_refs->href_root, bytenr, 0);
Chris Mason1887be62009-03-13 10:11:24 -0400914}
Miao Xie78a61842012-11-21 02:21:28 +0000915
916void btrfs_delayed_ref_exit(void)
917{
918 if (btrfs_delayed_ref_head_cachep)
919 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
920 if (btrfs_delayed_tree_ref_cachep)
921 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
922 if (btrfs_delayed_data_ref_cachep)
923 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
924 if (btrfs_delayed_extent_op_cachep)
925 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
926}
927
928int btrfs_delayed_ref_init(void)
929{
930 btrfs_delayed_ref_head_cachep = kmem_cache_create(
931 "btrfs_delayed_ref_head",
932 sizeof(struct btrfs_delayed_ref_head), 0,
933 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
934 if (!btrfs_delayed_ref_head_cachep)
935 goto fail;
936
937 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
938 "btrfs_delayed_tree_ref",
939 sizeof(struct btrfs_delayed_tree_ref), 0,
940 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
941 if (!btrfs_delayed_tree_ref_cachep)
942 goto fail;
943
944 btrfs_delayed_data_ref_cachep = kmem_cache_create(
945 "btrfs_delayed_data_ref",
946 sizeof(struct btrfs_delayed_data_ref), 0,
947 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
948 if (!btrfs_delayed_data_ref_cachep)
949 goto fail;
950
951 btrfs_delayed_extent_op_cachep = kmem_cache_create(
952 "btrfs_delayed_extent_op",
953 sizeof(struct btrfs_delayed_extent_op), 0,
954 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
955 if (!btrfs_delayed_extent_op_cachep)
956 goto fail;
957
958 return 0;
959fail:
960 btrfs_delayed_ref_exit();
961 return -ENOMEM;
962}