2 * Copyright (C) 2009 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/sort.h>
23 #include "delayed-ref.h"
24 #include "transaction.h"
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.
36 * compare two delayed tree backrefs with same bytenr and type
38 static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,
39 struct btrfs_delayed_tree_ref *ref1)
41 if (ref1->root < ref2->root)
43 if (ref1->root > ref2->root)
45 if (ref1->parent < ref2->parent)
47 if (ref1->parent > ref2->parent)
53 * compare two delayed data backrefs with same bytenr and type
55 static int comp_data_refs(struct btrfs_delayed_data_ref *ref2,
56 struct btrfs_delayed_data_ref *ref1)
58 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
59 if (ref1->root < ref2->root)
61 if (ref1->root > ref2->root)
63 if (ref1->objectid < ref2->objectid)
65 if (ref1->objectid > ref2->objectid)
67 if (ref1->offset < ref2->offset)
69 if (ref1->offset > ref2->offset)
72 if (ref1->parent < ref2->parent)
74 if (ref1->parent > ref2->parent)
81 * entries in the rb tree are ordered by the byte number of the extent,
82 * type of the delayed backrefs and content of delayed backrefs.
84 static int comp_entry(struct btrfs_delayed_ref_node *ref2,
85 struct btrfs_delayed_ref_node *ref1,
88 if (ref1->bytenr < ref2->bytenr)
90 if (ref1->bytenr > ref2->bytenr)
92 if (ref1->is_head && ref2->is_head)
98 if (ref1->type < ref2->type)
100 if (ref1->type > ref2->type)
102 /* merging of sequenced refs is not allowed */
104 if (ref1->seq < ref2->seq)
106 if (ref1->seq > ref2->seq)
109 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));
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
127 static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
128 struct rb_node *node)
130 struct rb_node **p = &root->rb_node;
131 struct rb_node *parent_node = NULL;
132 struct btrfs_delayed_ref_node *entry;
133 struct btrfs_delayed_ref_node *ins;
136 ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
139 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
142 cmp = comp_entry(entry, ins, 1);
151 rb_link_node(node, parent_node, p);
152 rb_insert_color(node, root);
157 * 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.
159 * If return_bigger is given, the next bigger entry is returned if no exact
162 static struct btrfs_delayed_ref_node *find_ref_head(struct rb_root *root,
164 struct btrfs_delayed_ref_node **last,
168 struct btrfs_delayed_ref_node *entry;
175 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
176 WARN_ON(!entry->in_tree);
180 if (bytenr < entry->bytenr)
182 else if (bytenr > entry->bytenr)
184 else if (!btrfs_delayed_ref_is_head(entry))
196 if (entry && return_bigger) {
198 n = rb_next(&entry->rb_node);
201 entry = rb_entry(n, struct btrfs_delayed_ref_node,
203 bytenr = entry->bytenr;
212 int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
213 struct btrfs_delayed_ref_head *head)
215 struct btrfs_delayed_ref_root *delayed_refs;
217 delayed_refs = &trans->transaction->delayed_refs;
218 assert_spin_locked(&delayed_refs->lock);
219 if (mutex_trylock(&head->mutex))
222 atomic_inc(&head->node.refs);
223 spin_unlock(&delayed_refs->lock);
225 mutex_lock(&head->mutex);
226 spin_lock(&delayed_refs->lock);
227 if (!head->node.in_tree) {
228 mutex_unlock(&head->mutex);
229 btrfs_put_delayed_ref(&head->node);
232 btrfs_put_delayed_ref(&head->node);
236 static void inline drop_delayed_ref(struct btrfs_trans_handle *trans,
237 struct btrfs_delayed_ref_root *delayed_refs,
238 struct btrfs_delayed_ref_node *ref)
240 rb_erase(&ref->rb_node, &delayed_refs->root);
242 btrfs_put_delayed_ref(ref);
243 delayed_refs->num_entries--;
244 if (trans->delayed_ref_updates)
245 trans->delayed_ref_updates--;
248 static int merge_ref(struct btrfs_trans_handle *trans,
249 struct btrfs_delayed_ref_root *delayed_refs,
250 struct btrfs_delayed_ref_node *ref, u64 seq)
252 struct rb_node *node;
257 node = rb_prev(&ref->rb_node);
259 struct btrfs_delayed_ref_node *next;
261 next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
262 node = rb_prev(node);
263 if (next->bytenr != ref->bytenr)
265 if (seq && next->seq >= seq)
267 if (comp_entry(ref, next, 0))
270 if (ref->action == next->action) {
273 if (ref->ref_mod < next->ref_mod) {
274 struct btrfs_delayed_ref_node *tmp;
281 mod = -next->ref_mod;
285 drop_delayed_ref(trans, delayed_refs, next);
287 if (ref->ref_mod == 0) {
288 drop_delayed_ref(trans, delayed_refs, ref);
292 * You can't have multiples of the same ref on a tree
295 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
296 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
301 node = rb_prev(&ref->rb_node);
307 void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
308 struct btrfs_fs_info *fs_info,
309 struct btrfs_delayed_ref_root *delayed_refs,
310 struct btrfs_delayed_ref_head *head)
312 struct rb_node *node;
315 spin_lock(&fs_info->tree_mod_seq_lock);
316 if (!list_empty(&fs_info->tree_mod_seq_list)) {
317 struct seq_list *elem;
319 elem = list_first_entry(&fs_info->tree_mod_seq_list,
320 struct seq_list, list);
323 spin_unlock(&fs_info->tree_mod_seq_lock);
325 node = rb_prev(&head->node.rb_node);
327 struct btrfs_delayed_ref_node *ref;
329 ref = rb_entry(node, struct btrfs_delayed_ref_node,
331 if (ref->bytenr != head->node.bytenr)
334 /* We can't merge refs that are outside of our seq count */
335 if (seq && ref->seq >= seq)
337 if (merge_ref(trans, delayed_refs, ref, seq))
338 node = rb_prev(&head->node.rb_node);
340 node = rb_prev(node);
344 int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
345 struct btrfs_delayed_ref_root *delayed_refs,
348 struct seq_list *elem;
351 spin_lock(&fs_info->tree_mod_seq_lock);
352 if (!list_empty(&fs_info->tree_mod_seq_list)) {
353 elem = list_first_entry(&fs_info->tree_mod_seq_list,
354 struct seq_list, list);
355 if (seq >= elem->seq) {
356 pr_debug("holding back delayed_ref %llu, lowest is "
357 "%llu (%p)\n", seq, elem->seq, delayed_refs);
362 spin_unlock(&fs_info->tree_mod_seq_lock);
366 int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
367 struct list_head *cluster, u64 start)
370 struct btrfs_delayed_ref_root *delayed_refs;
371 struct rb_node *node;
372 struct btrfs_delayed_ref_node *ref;
373 struct btrfs_delayed_ref_head *head;
375 delayed_refs = &trans->transaction->delayed_refs;
377 node = rb_first(&delayed_refs->root);
380 find_ref_head(&delayed_refs->root, start + 1, &ref, 1);
382 node = &ref->rb_node;
384 node = rb_first(&delayed_refs->root);
387 while (node && count < 32) {
388 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
389 if (btrfs_delayed_ref_is_head(ref)) {
390 head = btrfs_delayed_node_to_head(ref);
391 if (list_empty(&head->cluster)) {
392 list_add_tail(&head->cluster, cluster);
393 delayed_refs->run_delayed_start =
397 WARN_ON(delayed_refs->num_heads_ready == 0);
398 delayed_refs->num_heads_ready--;
400 /* the goal of the clustering is to find extents
401 * that are likely to end up in the same extent
402 * leaf on disk. So, we don't want them spread
403 * all over the tree. Stop now if we've hit
404 * a head that was already in use
409 node = rb_next(node);
415 * we've gone to the end of the rbtree without finding any
416 * clusters. start from the beginning and try again
419 node = rb_first(&delayed_refs->root);
426 * helper function to update an extent delayed ref in the
427 * rbtree. existing and update must both have the same
430 * This may free existing if the update cancels out whatever
431 * operation it was doing.
434 update_existing_ref(struct btrfs_trans_handle *trans,
435 struct btrfs_delayed_ref_root *delayed_refs,
436 struct btrfs_delayed_ref_node *existing,
437 struct btrfs_delayed_ref_node *update)
439 if (update->action != existing->action) {
441 * this is effectively undoing either an add or a
442 * drop. We decrement the ref_mod, and if it goes
443 * down to zero we just delete the entry without
444 * every changing the extent allocation tree.
447 if (existing->ref_mod == 0)
448 drop_delayed_ref(trans, delayed_refs, existing);
450 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
451 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
453 WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
454 existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
456 * the action on the existing ref matches
457 * the action on the ref we're trying to add.
458 * Bump the ref_mod by one so the backref that
459 * is eventually added/removed has the correct
462 existing->ref_mod += update->ref_mod;
467 * helper function to update the accounting in the head ref
468 * existing and update must have the same bytenr
471 update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
472 struct btrfs_delayed_ref_node *update)
474 struct btrfs_delayed_ref_head *existing_ref;
475 struct btrfs_delayed_ref_head *ref;
477 existing_ref = btrfs_delayed_node_to_head(existing);
478 ref = btrfs_delayed_node_to_head(update);
479 BUG_ON(existing_ref->is_data != ref->is_data);
481 if (ref->must_insert_reserved) {
482 /* if the extent was freed and then
483 * reallocated before the delayed ref
484 * entries were processed, we can end up
485 * with an existing head ref without
486 * the must_insert_reserved flag set.
489 existing_ref->must_insert_reserved = ref->must_insert_reserved;
492 * update the num_bytes so we make sure the accounting
495 existing->num_bytes = update->num_bytes;
499 if (ref->extent_op) {
500 if (!existing_ref->extent_op) {
501 existing_ref->extent_op = ref->extent_op;
503 if (ref->extent_op->update_key) {
504 memcpy(&existing_ref->extent_op->key,
505 &ref->extent_op->key,
506 sizeof(ref->extent_op->key));
507 existing_ref->extent_op->update_key = 1;
509 if (ref->extent_op->update_flags) {
510 existing_ref->extent_op->flags_to_set |=
511 ref->extent_op->flags_to_set;
512 existing_ref->extent_op->update_flags = 1;
514 kfree(ref->extent_op);
518 * update the reference mod on the head to reflect this new operation
520 existing->ref_mod += update->ref_mod;
524 * helper function to actually insert a head node into the rbtree.
525 * this does all the dirty work in terms of maintaining the correct
526 * overall modification count.
528 static noinline void add_delayed_ref_head(struct btrfs_fs_info *fs_info,
529 struct btrfs_trans_handle *trans,
530 struct btrfs_delayed_ref_node *ref,
531 u64 bytenr, u64 num_bytes,
532 int action, int is_data)
534 struct btrfs_delayed_ref_node *existing;
535 struct btrfs_delayed_ref_head *head_ref = NULL;
536 struct btrfs_delayed_ref_root *delayed_refs;
538 int must_insert_reserved = 0;
541 * the head node stores the sum of all the mods, so dropping a ref
542 * should drop the sum in the head node by one.
544 if (action == BTRFS_UPDATE_DELAYED_HEAD)
546 else if (action == BTRFS_DROP_DELAYED_REF)
550 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
551 * the reserved accounting when the extent is finally added, or
552 * if a later modification deletes the delayed ref without ever
553 * inserting the extent into the extent allocation tree.
554 * ref->must_insert_reserved is the flag used to record
555 * that accounting mods are required.
557 * Once we record must_insert_reserved, switch the action to
558 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
560 if (action == BTRFS_ADD_DELAYED_EXTENT)
561 must_insert_reserved = 1;
563 must_insert_reserved = 0;
565 delayed_refs = &trans->transaction->delayed_refs;
567 /* first set the basic ref node struct up */
568 atomic_set(&ref->refs, 1);
569 ref->bytenr = bytenr;
570 ref->num_bytes = num_bytes;
571 ref->ref_mod = count_mod;
578 head_ref = btrfs_delayed_node_to_head(ref);
579 head_ref->must_insert_reserved = must_insert_reserved;
580 head_ref->is_data = is_data;
582 INIT_LIST_HEAD(&head_ref->cluster);
583 mutex_init(&head_ref->mutex);
585 trace_btrfs_delayed_ref_head(ref, head_ref, action);
587 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
590 update_existing_head_ref(existing, ref);
592 * we've updated the existing ref, free the newly
597 delayed_refs->num_heads++;
598 delayed_refs->num_heads_ready++;
599 delayed_refs->num_entries++;
600 trans->delayed_ref_updates++;
605 * helper to insert a delayed tree ref into the rbtree.
607 static noinline void add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
608 struct btrfs_trans_handle *trans,
609 struct btrfs_delayed_ref_node *ref,
610 u64 bytenr, u64 num_bytes, u64 parent,
611 u64 ref_root, int level, int action,
614 struct btrfs_delayed_ref_node *existing;
615 struct btrfs_delayed_tree_ref *full_ref;
616 struct btrfs_delayed_ref_root *delayed_refs;
619 if (action == BTRFS_ADD_DELAYED_EXTENT)
620 action = BTRFS_ADD_DELAYED_REF;
622 delayed_refs = &trans->transaction->delayed_refs;
624 /* first set the basic ref node struct up */
625 atomic_set(&ref->refs, 1);
626 ref->bytenr = bytenr;
627 ref->num_bytes = num_bytes;
629 ref->action = action;
633 if (need_ref_seq(for_cow, ref_root))
634 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
637 full_ref = btrfs_delayed_node_to_tree_ref(ref);
638 full_ref->parent = parent;
639 full_ref->root = ref_root;
641 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
643 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
644 full_ref->level = level;
646 trace_btrfs_delayed_tree_ref(ref, full_ref, action);
648 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
651 update_existing_ref(trans, delayed_refs, existing, ref);
653 * we've updated the existing ref, free the newly
658 delayed_refs->num_entries++;
659 trans->delayed_ref_updates++;
664 * helper to insert a delayed data ref into the rbtree.
666 static noinline void add_delayed_data_ref(struct btrfs_fs_info *fs_info,
667 struct btrfs_trans_handle *trans,
668 struct btrfs_delayed_ref_node *ref,
669 u64 bytenr, u64 num_bytes, u64 parent,
670 u64 ref_root, u64 owner, u64 offset,
671 int action, int for_cow)
673 struct btrfs_delayed_ref_node *existing;
674 struct btrfs_delayed_data_ref *full_ref;
675 struct btrfs_delayed_ref_root *delayed_refs;
678 if (action == BTRFS_ADD_DELAYED_EXTENT)
679 action = BTRFS_ADD_DELAYED_REF;
681 delayed_refs = &trans->transaction->delayed_refs;
683 /* first set the basic ref node struct up */
684 atomic_set(&ref->refs, 1);
685 ref->bytenr = bytenr;
686 ref->num_bytes = num_bytes;
688 ref->action = action;
692 if (need_ref_seq(for_cow, ref_root))
693 seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
696 full_ref = btrfs_delayed_node_to_data_ref(ref);
697 full_ref->parent = parent;
698 full_ref->root = ref_root;
700 ref->type = BTRFS_SHARED_DATA_REF_KEY;
702 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
704 full_ref->objectid = owner;
705 full_ref->offset = offset;
707 trace_btrfs_delayed_data_ref(ref, full_ref, action);
709 existing = tree_insert(&delayed_refs->root, &ref->rb_node);
712 update_existing_ref(trans, delayed_refs, existing, ref);
714 * we've updated the existing ref, free the newly
719 delayed_refs->num_entries++;
720 trans->delayed_ref_updates++;
725 * add a delayed tree ref. This does all of the accounting required
726 * to make sure the delayed ref is eventually processed before this
727 * transaction commits.
729 int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
730 struct btrfs_trans_handle *trans,
731 u64 bytenr, u64 num_bytes, u64 parent,
732 u64 ref_root, int level, int action,
733 struct btrfs_delayed_extent_op *extent_op,
736 struct btrfs_delayed_tree_ref *ref;
737 struct btrfs_delayed_ref_head *head_ref;
738 struct btrfs_delayed_ref_root *delayed_refs;
740 BUG_ON(extent_op && extent_op->is_data);
741 ref = kmalloc(sizeof(*ref), GFP_NOFS);
745 head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
751 head_ref->extent_op = extent_op;
753 delayed_refs = &trans->transaction->delayed_refs;
754 spin_lock(&delayed_refs->lock);
757 * insert both the head node and the new ref without dropping
760 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
761 num_bytes, action, 0);
763 add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
764 num_bytes, parent, ref_root, level, action,
766 spin_unlock(&delayed_refs->lock);
767 if (need_ref_seq(for_cow, ref_root))
768 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
774 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
776 int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
777 struct btrfs_trans_handle *trans,
778 u64 bytenr, u64 num_bytes,
779 u64 parent, u64 ref_root,
780 u64 owner, u64 offset, int action,
781 struct btrfs_delayed_extent_op *extent_op,
784 struct btrfs_delayed_data_ref *ref;
785 struct btrfs_delayed_ref_head *head_ref;
786 struct btrfs_delayed_ref_root *delayed_refs;
788 BUG_ON(extent_op && !extent_op->is_data);
789 ref = kmalloc(sizeof(*ref), GFP_NOFS);
793 head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
799 head_ref->extent_op = extent_op;
801 delayed_refs = &trans->transaction->delayed_refs;
802 spin_lock(&delayed_refs->lock);
805 * insert both the head node and the new ref without dropping
808 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
809 num_bytes, action, 1);
811 add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
812 num_bytes, parent, ref_root, owner, offset,
814 spin_unlock(&delayed_refs->lock);
815 if (need_ref_seq(for_cow, ref_root))
816 btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
821 int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
822 struct btrfs_trans_handle *trans,
823 u64 bytenr, u64 num_bytes,
824 struct btrfs_delayed_extent_op *extent_op)
826 struct btrfs_delayed_ref_head *head_ref;
827 struct btrfs_delayed_ref_root *delayed_refs;
829 head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
833 head_ref->extent_op = extent_op;
835 delayed_refs = &trans->transaction->delayed_refs;
836 spin_lock(&delayed_refs->lock);
838 add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
839 num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
842 spin_unlock(&delayed_refs->lock);
847 * this does a simple search for the head node for a given extent.
848 * It must be called with the delayed ref spinlock held, and it returns
849 * the head node if any where found, or NULL if not.
851 struct btrfs_delayed_ref_head *
852 btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
854 struct btrfs_delayed_ref_node *ref;
855 struct btrfs_delayed_ref_root *delayed_refs;
857 delayed_refs = &trans->transaction->delayed_refs;
858 ref = find_ref_head(&delayed_refs->root, bytenr, NULL, 0);
860 return btrfs_delayed_node_to_head(ref);