blob: a405db0320e80994602127c32af5ec71c502b277 [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
26/*
27 * delayed back reference update tracking. For subvolume trees
28 * we queue up extent allocations and backref maintenance for
29 * delayed processing. This avoids deep call chains where we
30 * add extents in the middle of btrfs_search_slot, and it allows
31 * us to buffer up frequently modified backrefs in an rb tree instead
32 * of hammering updates on the extent allocation tree.
Chris Mason56bec292009-03-13 10:10:06 -040033 */
34
35/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -040036 * compare two delayed tree backrefs with same bytenr and type
Chris Mason56bec292009-03-13 10:10:06 -040037 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -040038static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,
39 struct btrfs_delayed_tree_ref *ref1)
Chris Mason56bec292009-03-13 10:10:06 -040040{
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041 if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
42 if (ref1->root < ref2->root)
43 return -1;
44 if (ref1->root > ref2->root)
45 return 1;
46 } else {
47 if (ref1->parent < ref2->parent)
48 return -1;
49 if (ref1->parent > ref2->parent)
50 return 1;
51 }
52 return 0;
53}
54
55/*
56 * compare two delayed data backrefs with same bytenr and type
57 */
58static int comp_data_refs(struct btrfs_delayed_data_ref *ref2,
59 struct btrfs_delayed_data_ref *ref1)
60{
61 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
62 if (ref1->root < ref2->root)
63 return -1;
64 if (ref1->root > ref2->root)
65 return 1;
66 if (ref1->objectid < ref2->objectid)
67 return -1;
68 if (ref1->objectid > ref2->objectid)
69 return 1;
70 if (ref1->offset < ref2->offset)
71 return -1;
72 if (ref1->offset > ref2->offset)
73 return 1;
74 } else {
75 if (ref1->parent < ref2->parent)
76 return -1;
77 if (ref1->parent > ref2->parent)
78 return 1;
79 }
80 return 0;
81}
82
83/*
84 * entries in the rb tree are ordered by the byte number of the extent,
85 * type of the delayed backrefs and content of delayed backrefs.
86 */
87static int comp_entry(struct btrfs_delayed_ref_node *ref2,
88 struct btrfs_delayed_ref_node *ref1)
89{
90 if (ref1->bytenr < ref2->bytenr)
Chris Mason56bec292009-03-13 10:10:06 -040091 return -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040092 if (ref1->bytenr > ref2->bytenr)
Chris Mason56bec292009-03-13 10:10:06 -040093 return 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040094 if (ref1->is_head && ref2->is_head)
95 return 0;
96 if (ref2->is_head)
Chris Mason56bec292009-03-13 10:10:06 -040097 return -1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -040098 if (ref1->is_head)
Chris Mason56bec292009-03-13 10:10:06 -040099 return 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400100 if (ref1->type < ref2->type)
101 return -1;
102 if (ref1->type > ref2->type)
103 return 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200104 /* merging of sequenced refs is not allowed */
105 if (ref1->seq < ref2->seq)
106 return -1;
107 if (ref1->seq > ref2->seq)
108 return 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400109 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
110 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
111 return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
112 btrfs_delayed_node_to_tree_ref(ref1));
113 } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
114 ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
115 return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),
116 btrfs_delayed_node_to_data_ref(ref1));
117 }
118 BUG();
Chris Mason56bec292009-03-13 10:10:06 -0400119 return 0;
120}
121
122/*
123 * insert a new ref into the rbtree. This returns any existing refs
124 * for the same (bytenr,parent) tuple, or NULL if the new node was properly
125 * inserted.
126 */
127static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
Chris Mason56bec292009-03-13 10:10:06 -0400128 struct rb_node *node)
129{
130 struct rb_node **p = &root->rb_node;
131 struct rb_node *parent_node = NULL;
132 struct btrfs_delayed_ref_node *entry;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400133 struct btrfs_delayed_ref_node *ins;
Chris Mason56bec292009-03-13 10:10:06 -0400134 int cmp;
135
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400136 ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Chris Mason56bec292009-03-13 10:10:06 -0400137 while (*p) {
138 parent_node = *p;
139 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
140 rb_node);
141
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400142 cmp = comp_entry(entry, ins);
Chris Mason56bec292009-03-13 10:10:06 -0400143 if (cmp < 0)
144 p = &(*p)->rb_left;
145 else if (cmp > 0)
146 p = &(*p)->rb_right;
147 else
148 return entry;
149 }
150
Chris Mason56bec292009-03-13 10:10:06 -0400151 rb_link_node(node, parent_node, p);
152 rb_insert_color(node, root);
153 return NULL;
154}
155
156/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400157 * find an head entry based on bytenr. This returns the delayed ref
158 * head if it was able to find one, or NULL if nothing was in that spot
Chris Mason56bec292009-03-13 10:10:06 -0400159 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400160static struct btrfs_delayed_ref_node *find_ref_head(struct rb_root *root,
161 u64 bytenr,
Chris Masonc3e69d52009-03-13 10:17:05 -0400162 struct btrfs_delayed_ref_node **last)
Chris Mason56bec292009-03-13 10:10:06 -0400163{
164 struct rb_node *n = root->rb_node;
165 struct btrfs_delayed_ref_node *entry;
166 int cmp;
167
168 while (n) {
169 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
170 WARN_ON(!entry->in_tree);
Chris Masonc3e69d52009-03-13 10:17:05 -0400171 if (last)
172 *last = entry;
Chris Mason56bec292009-03-13 10:10:06 -0400173
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400174 if (bytenr < entry->bytenr)
175 cmp = -1;
176 else if (bytenr > entry->bytenr)
177 cmp = 1;
178 else if (!btrfs_delayed_ref_is_head(entry))
179 cmp = 1;
180 else
181 cmp = 0;
182
Chris Mason56bec292009-03-13 10:10:06 -0400183 if (cmp < 0)
184 n = n->rb_left;
185 else if (cmp > 0)
186 n = n->rb_right;
187 else
188 return entry;
189 }
190 return NULL;
191}
192
Chris Masonc3e69d52009-03-13 10:17:05 -0400193int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
194 struct btrfs_delayed_ref_head *head)
Chris Mason56bec292009-03-13 10:10:06 -0400195{
Chris Masonc3e69d52009-03-13 10:17:05 -0400196 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400197
Chris Masonc3e69d52009-03-13 10:17:05 -0400198 delayed_refs = &trans->transaction->delayed_refs;
199 assert_spin_locked(&delayed_refs->lock);
200 if (mutex_trylock(&head->mutex))
201 return 0;
202
203 atomic_inc(&head->node.refs);
204 spin_unlock(&delayed_refs->lock);
205
206 mutex_lock(&head->mutex);
207 spin_lock(&delayed_refs->lock);
208 if (!head->node.in_tree) {
209 mutex_unlock(&head->mutex);
210 btrfs_put_delayed_ref(&head->node);
211 return -EAGAIN;
212 }
213 btrfs_put_delayed_ref(&head->node);
214 return 0;
215}
216
Arne Jansen00f04b82011-09-14 12:37:00 +0200217int btrfs_check_delayed_seq(struct btrfs_delayed_ref_root *delayed_refs,
218 u64 seq)
219{
220 struct seq_list *elem;
221
222 assert_spin_locked(&delayed_refs->lock);
223 if (list_empty(&delayed_refs->seq_head))
224 return 0;
225
226 elem = list_first_entry(&delayed_refs->seq_head, struct seq_list, list);
227 if (seq >= elem->seq) {
228 pr_debug("holding back delayed_ref %llu, lowest is %llu (%p)\n",
229 seq, elem->seq, delayed_refs);
230 return 1;
231 }
232 return 0;
233}
234
Chris Masonc3e69d52009-03-13 10:17:05 -0400235int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
236 struct list_head *cluster, u64 start)
237{
238 int count = 0;
239 struct btrfs_delayed_ref_root *delayed_refs;
240 struct rb_node *node;
241 struct btrfs_delayed_ref_node *ref;
242 struct btrfs_delayed_ref_head *head;
243
244 delayed_refs = &trans->transaction->delayed_refs;
245 if (start == 0) {
246 node = rb_first(&delayed_refs->root);
247 } else {
248 ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400249 find_ref_head(&delayed_refs->root, start, &ref);
Chris Masonc3e69d52009-03-13 10:17:05 -0400250 if (ref) {
251 struct btrfs_delayed_ref_node *tmp;
252
253 node = rb_prev(&ref->rb_node);
254 while (node) {
255 tmp = rb_entry(node,
256 struct btrfs_delayed_ref_node,
257 rb_node);
258 if (tmp->bytenr < start)
259 break;
260 ref = tmp;
261 node = rb_prev(&ref->rb_node);
262 }
263 node = &ref->rb_node;
264 } else
265 node = rb_first(&delayed_refs->root);
266 }
267again:
268 while (node && count < 32) {
269 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Chris Mason56bec292009-03-13 10:10:06 -0400270 if (btrfs_delayed_ref_is_head(ref)) {
271 head = btrfs_delayed_node_to_head(ref);
Chris Masonc3e69d52009-03-13 10:17:05 -0400272 if (list_empty(&head->cluster)) {
273 list_add_tail(&head->cluster, cluster);
274 delayed_refs->run_delayed_start =
275 head->node.bytenr;
276 count++;
277
278 WARN_ON(delayed_refs->num_heads_ready == 0);
279 delayed_refs->num_heads_ready--;
280 } else if (count) {
281 /* the goal of the clustering is to find extents
282 * that are likely to end up in the same extent
283 * leaf on disk. So, we don't want them spread
284 * all over the tree. Stop now if we've hit
285 * a head that was already in use
286 */
Chris Mason56bec292009-03-13 10:10:06 -0400287 break;
288 }
289 }
Chris Masonc3e69d52009-03-13 10:17:05 -0400290 node = rb_next(node);
Chris Mason56bec292009-03-13 10:10:06 -0400291 }
Chris Masonc3e69d52009-03-13 10:17:05 -0400292 if (count) {
293 return 0;
294 } else if (start) {
295 /*
296 * we've gone to the end of the rbtree without finding any
297 * clusters. start from the beginning and try again
298 */
299 start = 0;
300 node = rb_first(&delayed_refs->root);
301 goto again;
302 }
303 return 1;
Chris Mason56bec292009-03-13 10:10:06 -0400304}
305
306/*
Chris Mason56bec292009-03-13 10:10:06 -0400307 * helper function to update an extent delayed ref in the
308 * rbtree. existing and update must both have the same
309 * bytenr and parent
310 *
311 * This may free existing if the update cancels out whatever
312 * operation it was doing.
313 */
314static noinline void
315update_existing_ref(struct btrfs_trans_handle *trans,
316 struct btrfs_delayed_ref_root *delayed_refs,
317 struct btrfs_delayed_ref_node *existing,
318 struct btrfs_delayed_ref_node *update)
319{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400320 if (update->action != existing->action) {
Chris Mason56bec292009-03-13 10:10:06 -0400321 /*
322 * this is effectively undoing either an add or a
323 * drop. We decrement the ref_mod, and if it goes
324 * down to zero we just delete the entry without
325 * every changing the extent allocation tree.
326 */
327 existing->ref_mod--;
328 if (existing->ref_mod == 0) {
329 rb_erase(&existing->rb_node,
330 &delayed_refs->root);
331 existing->in_tree = 0;
332 btrfs_put_delayed_ref(existing);
333 delayed_refs->num_entries--;
334 if (trans->delayed_ref_updates)
335 trans->delayed_ref_updates--;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400336 } else {
337 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
338 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400339 }
340 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400341 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
342 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
Chris Mason56bec292009-03-13 10:10:06 -0400343 /*
344 * the action on the existing ref matches
345 * the action on the ref we're trying to add.
346 * Bump the ref_mod by one so the backref that
347 * is eventually added/removed has the correct
348 * reference count
349 */
350 existing->ref_mod += update->ref_mod;
351 }
352}
353
354/*
355 * helper function to update the accounting in the head ref
356 * existing and update must have the same bytenr
357 */
358static noinline void
359update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
360 struct btrfs_delayed_ref_node *update)
361{
362 struct btrfs_delayed_ref_head *existing_ref;
363 struct btrfs_delayed_ref_head *ref;
364
365 existing_ref = btrfs_delayed_node_to_head(existing);
366 ref = btrfs_delayed_node_to_head(update);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400367 BUG_ON(existing_ref->is_data != ref->is_data);
Chris Mason56bec292009-03-13 10:10:06 -0400368
369 if (ref->must_insert_reserved) {
370 /* if the extent was freed and then
371 * reallocated before the delayed ref
372 * entries were processed, we can end up
373 * with an existing head ref without
374 * the must_insert_reserved flag set.
375 * Set it again here
376 */
377 existing_ref->must_insert_reserved = ref->must_insert_reserved;
378
379 /*
380 * update the num_bytes so we make sure the accounting
381 * is done correctly
382 */
383 existing->num_bytes = update->num_bytes;
384
385 }
386
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 if (ref->extent_op) {
388 if (!existing_ref->extent_op) {
389 existing_ref->extent_op = ref->extent_op;
390 } else {
391 if (ref->extent_op->update_key) {
392 memcpy(&existing_ref->extent_op->key,
393 &ref->extent_op->key,
394 sizeof(ref->extent_op->key));
395 existing_ref->extent_op->update_key = 1;
396 }
397 if (ref->extent_op->update_flags) {
398 existing_ref->extent_op->flags_to_set |=
399 ref->extent_op->flags_to_set;
400 existing_ref->extent_op->update_flags = 1;
401 }
402 kfree(ref->extent_op);
403 }
404 }
Chris Mason56bec292009-03-13 10:10:06 -0400405 /*
406 * update the reference mod on the head to reflect this new operation
407 */
408 existing->ref_mod += update->ref_mod;
409}
410
411/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 * helper function to actually insert a head node into the rbtree.
Chris Mason56bec292009-03-13 10:10:06 -0400413 * this does all the dirty work in terms of maintaining the correct
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400414 * overall modification count.
Chris Mason56bec292009-03-13 10:10:06 -0400415 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200416static noinline int add_delayed_ref_head(struct btrfs_fs_info *fs_info,
417 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400418 struct btrfs_delayed_ref_node *ref,
419 u64 bytenr, u64 num_bytes,
420 int action, int is_data)
Chris Mason56bec292009-03-13 10:10:06 -0400421{
422 struct btrfs_delayed_ref_node *existing;
Chris Masonc3e69d52009-03-13 10:17:05 -0400423 struct btrfs_delayed_ref_head *head_ref = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400424 struct btrfs_delayed_ref_root *delayed_refs;
425 int count_mod = 1;
426 int must_insert_reserved = 0;
427
428 /*
429 * the head node stores the sum of all the mods, so dropping a ref
430 * should drop the sum in the head node by one.
431 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400432 if (action == BTRFS_UPDATE_DELAYED_HEAD)
433 count_mod = 0;
434 else if (action == BTRFS_DROP_DELAYED_REF)
435 count_mod = -1;
Chris Mason56bec292009-03-13 10:10:06 -0400436
437 /*
438 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
439 * the reserved accounting when the extent is finally added, or
440 * if a later modification deletes the delayed ref without ever
441 * inserting the extent into the extent allocation tree.
442 * ref->must_insert_reserved is the flag used to record
443 * that accounting mods are required.
444 *
445 * Once we record must_insert_reserved, switch the action to
446 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
447 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400448 if (action == BTRFS_ADD_DELAYED_EXTENT)
Chris Mason56bec292009-03-13 10:10:06 -0400449 must_insert_reserved = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 else
Chris Mason56bec292009-03-13 10:10:06 -0400451 must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400452
453 delayed_refs = &trans->transaction->delayed_refs;
454
455 /* first set the basic ref node struct up */
456 atomic_set(&ref->refs, 1);
457 ref->bytenr = bytenr;
Chris Mason56bec292009-03-13 10:10:06 -0400458 ref->num_bytes = num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400459 ref->ref_mod = count_mod;
460 ref->type = 0;
461 ref->action = 0;
462 ref->is_head = 1;
463 ref->in_tree = 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200464 ref->seq = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400465
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400466 head_ref = btrfs_delayed_node_to_head(ref);
467 head_ref->must_insert_reserved = must_insert_reserved;
468 head_ref->is_data = is_data;
Chris Mason56bec292009-03-13 10:10:06 -0400469
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400470 INIT_LIST_HEAD(&head_ref->cluster);
471 mutex_init(&head_ref->mutex);
472
liubo1abe9b82011-03-24 11:18:59 +0000473 trace_btrfs_delayed_ref_head(ref, head_ref, action);
474
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400475 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
Chris Mason56bec292009-03-13 10:10:06 -0400476
477 if (existing) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400478 update_existing_head_ref(existing, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400479 /*
480 * we've updated the existing ref, free the newly
481 * allocated ref
482 */
483 kfree(ref);
484 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400485 delayed_refs->num_heads++;
486 delayed_refs->num_heads_ready++;
Chris Mason56bec292009-03-13 10:10:06 -0400487 delayed_refs->num_entries++;
488 trans->delayed_ref_updates++;
489 }
490 return 0;
491}
492
493/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400494 * helper to insert a delayed tree ref into the rbtree.
495 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200496static noinline int add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
497 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400498 struct btrfs_delayed_ref_node *ref,
499 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200500 u64 ref_root, int level, int action,
501 int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400502{
503 struct btrfs_delayed_ref_node *existing;
504 struct btrfs_delayed_tree_ref *full_ref;
505 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200506 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400507
508 if (action == BTRFS_ADD_DELAYED_EXTENT)
509 action = BTRFS_ADD_DELAYED_REF;
510
511 delayed_refs = &trans->transaction->delayed_refs;
512
513 /* first set the basic ref node struct up */
514 atomic_set(&ref->refs, 1);
515 ref->bytenr = bytenr;
516 ref->num_bytes = num_bytes;
517 ref->ref_mod = 1;
518 ref->action = action;
519 ref->is_head = 0;
520 ref->in_tree = 1;
521
Arne Jansen00f04b82011-09-14 12:37:00 +0200522 if (need_ref_seq(for_cow, ref_root))
523 seq = inc_delayed_seq(delayed_refs);
524 ref->seq = seq;
525
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400526 full_ref = btrfs_delayed_node_to_tree_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200527 full_ref->parent = parent;
528 full_ref->root = ref_root;
529 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400530 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200531 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400533 full_ref->level = level;
534
liubo1abe9b82011-03-24 11:18:59 +0000535 trace_btrfs_delayed_tree_ref(ref, full_ref, action);
536
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400537 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
538
539 if (existing) {
540 update_existing_ref(trans, delayed_refs, existing, ref);
541 /*
542 * we've updated the existing ref, free the newly
543 * allocated ref
544 */
545 kfree(ref);
546 } else {
547 delayed_refs->num_entries++;
548 trans->delayed_ref_updates++;
549 }
550 return 0;
551}
552
553/*
554 * helper to insert a delayed data ref into the rbtree.
555 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200556static noinline int add_delayed_data_ref(struct btrfs_fs_info *fs_info,
557 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400558 struct btrfs_delayed_ref_node *ref,
559 u64 bytenr, u64 num_bytes, u64 parent,
560 u64 ref_root, u64 owner, u64 offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200561 int action, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400562{
563 struct btrfs_delayed_ref_node *existing;
564 struct btrfs_delayed_data_ref *full_ref;
565 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200566 u64 seq = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400567
568 if (action == BTRFS_ADD_DELAYED_EXTENT)
569 action = BTRFS_ADD_DELAYED_REF;
570
571 delayed_refs = &trans->transaction->delayed_refs;
572
573 /* first set the basic ref node struct up */
574 atomic_set(&ref->refs, 1);
575 ref->bytenr = bytenr;
576 ref->num_bytes = num_bytes;
577 ref->ref_mod = 1;
578 ref->action = action;
579 ref->is_head = 0;
580 ref->in_tree = 1;
581
Arne Jansen00f04b82011-09-14 12:37:00 +0200582 if (need_ref_seq(for_cow, ref_root))
583 seq = inc_delayed_seq(delayed_refs);
584 ref->seq = seq;
585
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400586 full_ref = btrfs_delayed_node_to_data_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200587 full_ref->parent = parent;
588 full_ref->root = ref_root;
589 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 ref->type = BTRFS_SHARED_DATA_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200591 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400592 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200593
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400594 full_ref->objectid = owner;
595 full_ref->offset = offset;
596
liubo1abe9b82011-03-24 11:18:59 +0000597 trace_btrfs_delayed_data_ref(ref, full_ref, action);
598
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400599 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
600
601 if (existing) {
602 update_existing_ref(trans, delayed_refs, existing, ref);
603 /*
604 * we've updated the existing ref, free the newly
605 * allocated ref
606 */
607 kfree(ref);
608 } else {
609 delayed_refs->num_entries++;
610 trans->delayed_ref_updates++;
611 }
612 return 0;
613}
614
615/*
616 * add a delayed tree ref. This does all of the accounting required
Chris Mason56bec292009-03-13 10:10:06 -0400617 * to make sure the delayed ref is eventually processed before this
618 * transaction commits.
619 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200620int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
621 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400622 u64 bytenr, u64 num_bytes, u64 parent,
623 u64 ref_root, int level, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200624 struct btrfs_delayed_extent_op *extent_op,
625 int for_cow)
Chris Mason56bec292009-03-13 10:10:06 -0400626{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400627 struct btrfs_delayed_tree_ref *ref;
Chris Mason56bec292009-03-13 10:10:06 -0400628 struct btrfs_delayed_ref_head *head_ref;
629 struct btrfs_delayed_ref_root *delayed_refs;
630 int ret;
631
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400632 BUG_ON(extent_op && extent_op->is_data);
Chris Mason56bec292009-03-13 10:10:06 -0400633 ref = kmalloc(sizeof(*ref), GFP_NOFS);
634 if (!ref)
635 return -ENOMEM;
636
Chris Mason56bec292009-03-13 10:10:06 -0400637 head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
638 if (!head_ref) {
639 kfree(ref);
640 return -ENOMEM;
641 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400642
643 head_ref->extent_op = extent_op;
644
Chris Mason56bec292009-03-13 10:10:06 -0400645 delayed_refs = &trans->transaction->delayed_refs;
646 spin_lock(&delayed_refs->lock);
647
648 /*
649 * insert both the head node and the new ref without dropping
650 * the spin lock
651 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200652 ret = add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
653 num_bytes, action, 0);
Chris Mason56bec292009-03-13 10:10:06 -0400654 BUG_ON(ret);
655
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200656 ret = add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
657 num_bytes, parent, ref_root, level, action,
658 for_cow);
Chris Mason56bec292009-03-13 10:10:06 -0400659 BUG_ON(ret);
660 spin_unlock(&delayed_refs->lock);
661 return 0;
662}
663
664/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400665 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
666 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200667int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
668 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669 u64 bytenr, u64 num_bytes,
670 u64 parent, u64 ref_root,
671 u64 owner, u64 offset, int action,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200672 struct btrfs_delayed_extent_op *extent_op,
673 int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400674{
675 struct btrfs_delayed_data_ref *ref;
676 struct btrfs_delayed_ref_head *head_ref;
677 struct btrfs_delayed_ref_root *delayed_refs;
678 int ret;
679
680 BUG_ON(extent_op && !extent_op->is_data);
681 ref = kmalloc(sizeof(*ref), GFP_NOFS);
682 if (!ref)
683 return -ENOMEM;
684
685 head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
686 if (!head_ref) {
687 kfree(ref);
688 return -ENOMEM;
689 }
690
691 head_ref->extent_op = extent_op;
692
693 delayed_refs = &trans->transaction->delayed_refs;
694 spin_lock(&delayed_refs->lock);
695
696 /*
697 * insert both the head node and the new ref without dropping
698 * the spin lock
699 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200700 ret = add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
701 num_bytes, action, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400702 BUG_ON(ret);
703
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200704 ret = add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
705 num_bytes, parent, ref_root, owner, offset,
706 action, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400707 BUG_ON(ret);
708 spin_unlock(&delayed_refs->lock);
709 return 0;
710}
711
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200712int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
713 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400714 u64 bytenr, u64 num_bytes,
715 struct btrfs_delayed_extent_op *extent_op)
716{
717 struct btrfs_delayed_ref_head *head_ref;
718 struct btrfs_delayed_ref_root *delayed_refs;
719 int ret;
720
721 head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
722 if (!head_ref)
723 return -ENOMEM;
724
725 head_ref->extent_op = extent_op;
726
727 delayed_refs = &trans->transaction->delayed_refs;
728 spin_lock(&delayed_refs->lock);
729
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200730 ret = add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400731 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
732 extent_op->is_data);
733 BUG_ON(ret);
734
735 spin_unlock(&delayed_refs->lock);
736 return 0;
737}
738
739/*
Chris Mason1887be62009-03-13 10:11:24 -0400740 * this does a simple search for the head node for a given extent.
741 * It must be called with the delayed ref spinlock held, and it returns
742 * the head node if any where found, or NULL if not.
743 */
744struct btrfs_delayed_ref_head *
745btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
746{
747 struct btrfs_delayed_ref_node *ref;
748 struct btrfs_delayed_ref_root *delayed_refs;
749
750 delayed_refs = &trans->transaction->delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751 ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
Chris Mason1887be62009-03-13 10:11:24 -0400752 if (ref)
753 return btrfs_delayed_node_to_head(ref);
754 return NULL;
755}