aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c2062
1 files changed, 742 insertions, 1320 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index fefe83ad2059..178df4c67de4 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -31,6 +31,7 @@
31#include "volumes.h" 31#include "volumes.h"
32#include "locking.h" 32#include "locking.h"
33#include "ref-cache.h" 33#include "ref-cache.h"
34#include "free-space-cache.h"
34 35
35#define PENDING_EXTENT_INSERT 0 36#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1 37#define PENDING_EXTENT_DELETE 1
@@ -49,17 +50,23 @@ struct pending_extent_op {
49 int del; 50 int del;
50}; 51};
51 52
52static int finish_current_insert(struct btrfs_trans_handle *trans, 53static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
53 struct btrfs_root *extent_root, int all); 54 struct btrfs_root *root, u64 parent,
54static int del_pending_extents(struct btrfs_trans_handle *trans, 55 u64 root_objectid, u64 ref_generation,
55 struct btrfs_root *extent_root, int all); 56 u64 owner, struct btrfs_key *ins,
56static int pin_down_bytes(struct btrfs_trans_handle *trans, 57 int ref_mod);
57 struct btrfs_root *root, 58static int update_reserved_extents(struct btrfs_root *root,
58 u64 bytenr, u64 num_bytes, int is_data); 59 u64 bytenr, u64 num, int reserve);
59static int update_block_group(struct btrfs_trans_handle *trans, 60static int update_block_group(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root, 61 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int alloc, 62 u64 bytenr, u64 num_bytes, int alloc,
62 int mark_free); 63 int mark_free);
64static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans,
65 struct btrfs_root *root,
66 u64 bytenr, u64 num_bytes, u64 parent,
67 u64 root_objectid, u64 ref_generation,
68 u64 owner_objectid, int pin,
69 int ref_to_drop);
63 70
64static int do_chunk_alloc(struct btrfs_trans_handle *trans, 71static int do_chunk_alloc(struct btrfs_trans_handle *trans,
65 struct btrfs_root *extent_root, u64 alloc_bytes, 72 struct btrfs_root *extent_root, u64 alloc_bytes,
@@ -160,7 +167,6 @@ static int add_new_free_space(struct btrfs_block_group_cache *block_group,
160 u64 extent_start, extent_end, size; 167 u64 extent_start, extent_end, size;
161 int ret; 168 int ret;
162 169
163 mutex_lock(&info->pinned_mutex);
164 while (start < end) { 170 while (start < end) {
165 ret = find_first_extent_bit(&info->pinned_extents, start, 171 ret = find_first_extent_bit(&info->pinned_extents, start,
166 &extent_start, &extent_end, 172 &extent_start, &extent_end,
@@ -186,7 +192,6 @@ static int add_new_free_space(struct btrfs_block_group_cache *block_group,
186 ret = btrfs_add_free_space(block_group, start, size); 192 ret = btrfs_add_free_space(block_group, start, size);
187 BUG_ON(ret); 193 BUG_ON(ret);
188 } 194 }
189 mutex_unlock(&info->pinned_mutex);
190 195
191 return 0; 196 return 0;
192} 197}
@@ -285,8 +290,8 @@ next:
285 block_group->key.objectid + 290 block_group->key.objectid +
286 block_group->key.offset); 291 block_group->key.offset);
287 292
288 remove_sb_from_cache(root, block_group);
289 block_group->cached = 1; 293 block_group->cached = 1;
294 remove_sb_from_cache(root, block_group);
290 ret = 0; 295 ret = 0;
291err: 296err:
292 btrfs_free_path(path); 297 btrfs_free_path(path);
@@ -320,7 +325,7 @@ struct btrfs_block_group_cache *btrfs_lookup_block_group(
320 return cache; 325 return cache;
321} 326}
322 327
323static inline void put_block_group(struct btrfs_block_group_cache *cache) 328void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
324{ 329{
325 if (atomic_dec_and_test(&cache->count)) 330 if (atomic_dec_and_test(&cache->count))
326 kfree(cache); 331 kfree(cache);
@@ -393,12 +398,12 @@ again:
393 div_factor(cache->key.offset, factor)) { 398 div_factor(cache->key.offset, factor)) {
394 group_start = cache->key.objectid; 399 group_start = cache->key.objectid;
395 spin_unlock(&cache->lock); 400 spin_unlock(&cache->lock);
396 put_block_group(cache); 401 btrfs_put_block_group(cache);
397 goto found; 402 goto found;
398 } 403 }
399 } 404 }
400 spin_unlock(&cache->lock); 405 spin_unlock(&cache->lock);
401 put_block_group(cache); 406 btrfs_put_block_group(cache);
402 cond_resched(); 407 cond_resched();
403 } 408 }
404 if (!wrapped) { 409 if (!wrapped) {
@@ -554,262 +559,13 @@ out:
554 return ret; 559 return ret;
555} 560}
556 561
557/*
558 * updates all the backrefs that are pending on update_list for the
559 * extent_root
560 */
561static noinline int update_backrefs(struct btrfs_trans_handle *trans,
562 struct btrfs_root *extent_root,
563 struct btrfs_path *path,
564 struct list_head *update_list)
565{
566 struct btrfs_key key;
567 struct btrfs_extent_ref *ref;
568 struct btrfs_fs_info *info = extent_root->fs_info;
569 struct pending_extent_op *op;
570 struct extent_buffer *leaf;
571 int ret = 0;
572 struct list_head *cur = update_list->next;
573 u64 ref_objectid;
574 u64 ref_root = extent_root->root_key.objectid;
575
576 op = list_entry(cur, struct pending_extent_op, list);
577
578search:
579 key.objectid = op->bytenr;
580 key.type = BTRFS_EXTENT_REF_KEY;
581 key.offset = op->orig_parent;
582
583 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
584 BUG_ON(ret);
585
586 leaf = path->nodes[0];
587
588loop:
589 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
590
591 ref_objectid = btrfs_ref_objectid(leaf, ref);
592
593 if (btrfs_ref_root(leaf, ref) != ref_root ||
594 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
595 (ref_objectid != op->level &&
596 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
597 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
598 "root %llu, owner %u\n",
599 (unsigned long long)op->bytenr,
600 (unsigned long long)op->orig_parent,
601 (unsigned long long)ref_root, op->level);
602 btrfs_print_leaf(extent_root, leaf);
603 BUG();
604 }
605
606 key.objectid = op->bytenr;
607 key.offset = op->parent;
608 key.type = BTRFS_EXTENT_REF_KEY;
609 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
610 BUG_ON(ret);
611 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
612 btrfs_set_ref_generation(leaf, ref, op->generation);
613
614 cur = cur->next;
615
616 list_del_init(&op->list);
617 unlock_extent(&info->extent_ins, op->bytenr,
618 op->bytenr + op->num_bytes - 1, GFP_NOFS);
619 kfree(op);
620
621 if (cur == update_list) {
622 btrfs_mark_buffer_dirty(path->nodes[0]);
623 btrfs_release_path(extent_root, path);
624 goto out;
625 }
626
627 op = list_entry(cur, struct pending_extent_op, list);
628
629 path->slots[0]++;
630 while (path->slots[0] < btrfs_header_nritems(leaf)) {
631 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
632 if (key.objectid == op->bytenr &&
633 key.type == BTRFS_EXTENT_REF_KEY)
634 goto loop;
635 path->slots[0]++;
636 }
637
638 btrfs_mark_buffer_dirty(path->nodes[0]);
639 btrfs_release_path(extent_root, path);
640 goto search;
641
642out:
643 return 0;
644}
645
646static noinline int insert_extents(struct btrfs_trans_handle *trans,
647 struct btrfs_root *extent_root,
648 struct btrfs_path *path,
649 struct list_head *insert_list, int nr)
650{
651 struct btrfs_key *keys;
652 u32 *data_size;
653 struct pending_extent_op *op;
654 struct extent_buffer *leaf;
655 struct list_head *cur = insert_list->next;
656 struct btrfs_fs_info *info = extent_root->fs_info;
657 u64 ref_root = extent_root->root_key.objectid;
658 int i = 0, last = 0, ret;
659 int total = nr * 2;
660
661 if (!nr)
662 return 0;
663
664 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
665 if (!keys)
666 return -ENOMEM;
667
668 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
669 if (!data_size) {
670 kfree(keys);
671 return -ENOMEM;
672 }
673
674 list_for_each_entry(op, insert_list, list) {
675 keys[i].objectid = op->bytenr;
676 keys[i].offset = op->num_bytes;
677 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
678 data_size[i] = sizeof(struct btrfs_extent_item);
679 i++;
680
681 keys[i].objectid = op->bytenr;
682 keys[i].offset = op->parent;
683 keys[i].type = BTRFS_EXTENT_REF_KEY;
684 data_size[i] = sizeof(struct btrfs_extent_ref);
685 i++;
686 }
687
688 op = list_entry(cur, struct pending_extent_op, list);
689 i = 0;
690 while (i < total) {
691 int c;
692 ret = btrfs_insert_some_items(trans, extent_root, path,
693 keys+i, data_size+i, total-i);
694 BUG_ON(ret < 0);
695
696 if (last && ret > 1)
697 BUG();
698
699 leaf = path->nodes[0];
700 for (c = 0; c < ret; c++) {
701 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
702
703 /*
704 * if the first item we inserted was a backref, then
705 * the EXTENT_ITEM will be the odd c's, else it will
706 * be the even c's
707 */
708 if ((ref_first && (c % 2)) ||
709 (!ref_first && !(c % 2))) {
710 struct btrfs_extent_item *itm;
711
712 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
713 struct btrfs_extent_item);
714 btrfs_set_extent_refs(path->nodes[0], itm, 1);
715 op->del++;
716 } else {
717 struct btrfs_extent_ref *ref;
718
719 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
720 struct btrfs_extent_ref);
721 btrfs_set_ref_root(leaf, ref, ref_root);
722 btrfs_set_ref_generation(leaf, ref,
723 op->generation);
724 btrfs_set_ref_objectid(leaf, ref, op->level);
725 btrfs_set_ref_num_refs(leaf, ref, 1);
726 op->del++;
727 }
728
729 /*
730 * using del to see when its ok to free up the
731 * pending_extent_op. In the case where we insert the
732 * last item on the list in order to help do batching
733 * we need to not free the extent op until we actually
734 * insert the extent_item
735 */
736 if (op->del == 2) {
737 unlock_extent(&info->extent_ins, op->bytenr,
738 op->bytenr + op->num_bytes - 1,
739 GFP_NOFS);
740 cur = cur->next;
741 list_del_init(&op->list);
742 kfree(op);
743 if (cur != insert_list)
744 op = list_entry(cur,
745 struct pending_extent_op,
746 list);
747 }
748 }
749 btrfs_mark_buffer_dirty(leaf);
750 btrfs_release_path(extent_root, path);
751
752 /*
753 * Ok backref's and items usually go right next to eachother,
754 * but if we could only insert 1 item that means that we
755 * inserted on the end of a leaf, and we have no idea what may
756 * be on the next leaf so we just play it safe. In order to
757 * try and help this case we insert the last thing on our
758 * insert list so hopefully it will end up being the last
759 * thing on the leaf and everything else will be before it,
760 * which will let us insert a whole bunch of items at the same
761 * time.
762 */
763 if (ret == 1 && !last && (i + ret < total)) {
764 /*
765 * last: where we will pick up the next time around
766 * i: our current key to insert, will be total - 1
767 * cur: the current op we are screwing with
768 * op: duh
769 */
770 last = i + ret;
771 i = total - 1;
772 cur = insert_list->prev;
773 op = list_entry(cur, struct pending_extent_op, list);
774 } else if (last) {
775 /*
776 * ok we successfully inserted the last item on the
777 * list, lets reset everything
778 *
779 * i: our current key to insert, so where we left off
780 * last time
781 * last: done with this
782 * cur: the op we are messing with
783 * op: duh
784 * total: since we inserted the last key, we need to
785 * decrement total so we dont overflow
786 */
787 i = last;
788 last = 0;
789 total--;
790 if (i < total) {
791 cur = insert_list->next;
792 op = list_entry(cur, struct pending_extent_op,
793 list);
794 }
795 } else {
796 i += ret;
797 }
798
799 cond_resched();
800 }
801 ret = 0;
802 kfree(keys);
803 kfree(data_size);
804 return ret;
805}
806
807static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, 562static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
808 struct btrfs_root *root, 563 struct btrfs_root *root,
809 struct btrfs_path *path, 564 struct btrfs_path *path,
810 u64 bytenr, u64 parent, 565 u64 bytenr, u64 parent,
811 u64 ref_root, u64 ref_generation, 566 u64 ref_root, u64 ref_generation,
812 u64 owner_objectid) 567 u64 owner_objectid,
568 int refs_to_add)
813{ 569{
814 struct btrfs_key key; 570 struct btrfs_key key;
815 struct extent_buffer *leaf; 571 struct extent_buffer *leaf;
@@ -829,9 +585,10 @@ static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
829 btrfs_set_ref_root(leaf, ref, ref_root); 585 btrfs_set_ref_root(leaf, ref, ref_root);
830 btrfs_set_ref_generation(leaf, ref, ref_generation); 586 btrfs_set_ref_generation(leaf, ref, ref_generation);
831 btrfs_set_ref_objectid(leaf, ref, owner_objectid); 587 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
832 btrfs_set_ref_num_refs(leaf, ref, 1); 588 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
833 } else if (ret == -EEXIST) { 589 } else if (ret == -EEXIST) {
834 u64 existing_owner; 590 u64 existing_owner;
591
835 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); 592 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
836 leaf = path->nodes[0]; 593 leaf = path->nodes[0];
837 ref = btrfs_item_ptr(leaf, path->slots[0], 594 ref = btrfs_item_ptr(leaf, path->slots[0],
@@ -845,7 +602,7 @@ static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
845 602
846 num_refs = btrfs_ref_num_refs(leaf, ref); 603 num_refs = btrfs_ref_num_refs(leaf, ref);
847 BUG_ON(num_refs == 0); 604 BUG_ON(num_refs == 0);
848 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); 605 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
849 606
850 existing_owner = btrfs_ref_objectid(leaf, ref); 607 existing_owner = btrfs_ref_objectid(leaf, ref);
851 if (existing_owner != owner_objectid && 608 if (existing_owner != owner_objectid &&
@@ -857,6 +614,7 @@ static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
857 } else { 614 } else {
858 goto out; 615 goto out;
859 } 616 }
617 btrfs_unlock_up_safe(path, 1);
860 btrfs_mark_buffer_dirty(path->nodes[0]); 618 btrfs_mark_buffer_dirty(path->nodes[0]);
861out: 619out:
862 btrfs_release_path(root, path); 620 btrfs_release_path(root, path);
@@ -865,7 +623,8 @@ out:
865 623
866static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, 624static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
867 struct btrfs_root *root, 625 struct btrfs_root *root,
868 struct btrfs_path *path) 626 struct btrfs_path *path,
627 int refs_to_drop)
869{ 628{
870 struct extent_buffer *leaf; 629 struct extent_buffer *leaf;
871 struct btrfs_extent_ref *ref; 630 struct btrfs_extent_ref *ref;
@@ -875,8 +634,8 @@ static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
875 leaf = path->nodes[0]; 634 leaf = path->nodes[0];
876 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); 635 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
877 num_refs = btrfs_ref_num_refs(leaf, ref); 636 num_refs = btrfs_ref_num_refs(leaf, ref);
878 BUG_ON(num_refs == 0); 637 BUG_ON(num_refs < refs_to_drop);
879 num_refs -= 1; 638 num_refs -= refs_to_drop;
880 if (num_refs == 0) { 639 if (num_refs == 0) {
881 ret = btrfs_del_item(trans, root, path); 640 ret = btrfs_del_item(trans, root, path);
882 } else { 641 } else {
@@ -927,332 +686,28 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
927#endif 686#endif
928} 687}
929 688
930static noinline int free_extents(struct btrfs_trans_handle *trans,
931 struct btrfs_root *extent_root,
932 struct list_head *del_list)
933{
934 struct btrfs_fs_info *info = extent_root->fs_info;
935 struct btrfs_path *path;
936 struct btrfs_key key, found_key;
937 struct extent_buffer *leaf;
938 struct list_head *cur;
939 struct pending_extent_op *op;
940 struct btrfs_extent_item *ei;
941 int ret, num_to_del, extent_slot = 0, found_extent = 0;
942 u32 refs;
943 u64 bytes_freed = 0;
944
945 path = btrfs_alloc_path();
946 if (!path)
947 return -ENOMEM;
948 path->reada = 1;
949
950search:
951 /* search for the backref for the current ref we want to delete */
952 cur = del_list->next;
953 op = list_entry(cur, struct pending_extent_op, list);
954 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
955 op->orig_parent,
956 extent_root->root_key.objectid,
957 op->orig_generation, op->level, 1);
958 if (ret) {
959 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
960 "root %llu gen %llu owner %u\n",
961 (unsigned long long)op->bytenr,
962 (unsigned long long)extent_root->root_key.objectid,
963 (unsigned long long)op->orig_generation, op->level);
964 btrfs_print_leaf(extent_root, path->nodes[0]);
965 WARN_ON(1);
966 goto out;
967 }
968
969 extent_slot = path->slots[0];
970 num_to_del = 1;
971 found_extent = 0;
972
973 /*
974 * if we aren't the first item on the leaf we can move back one and see
975 * if our ref is right next to our extent item
976 */
977 if (likely(extent_slot)) {
978 extent_slot--;
979 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
980 extent_slot);
981 if (found_key.objectid == op->bytenr &&
982 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
983 found_key.offset == op->num_bytes) {
984 num_to_del++;
985 found_extent = 1;
986 }
987 }
988
989 /*
990 * if we didn't find the extent we need to delete the backref and then
991 * search for the extent item key so we can update its ref count
992 */
993 if (!found_extent) {
994 key.objectid = op->bytenr;
995 key.type = BTRFS_EXTENT_ITEM_KEY;
996 key.offset = op->num_bytes;
997
998 ret = remove_extent_backref(trans, extent_root, path);
999 BUG_ON(ret);
1000 btrfs_release_path(extent_root, path);
1001 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1002 BUG_ON(ret);
1003 extent_slot = path->slots[0];
1004 }
1005
1006 /* this is where we update the ref count for the extent */
1007 leaf = path->nodes[0];
1008 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
1009 refs = btrfs_extent_refs(leaf, ei);
1010 BUG_ON(refs == 0);
1011 refs--;
1012 btrfs_set_extent_refs(leaf, ei, refs);
1013
1014 btrfs_mark_buffer_dirty(leaf);
1015
1016 /*
1017 * This extent needs deleting. The reason cur_slot is extent_slot +
1018 * num_to_del is because extent_slot points to the slot where the extent
1019 * is, and if the backref was not right next to the extent we will be
1020 * deleting at least 1 item, and will want to start searching at the
1021 * slot directly next to extent_slot. However if we did find the
1022 * backref next to the extent item them we will be deleting at least 2
1023 * items and will want to start searching directly after the ref slot
1024 */
1025 if (!refs) {
1026 struct list_head *pos, *n, *end;
1027 int cur_slot = extent_slot+num_to_del;
1028 u64 super_used;
1029 u64 root_used;
1030
1031 path->slots[0] = extent_slot;
1032 bytes_freed = op->num_bytes;
1033
1034 mutex_lock(&info->pinned_mutex);
1035 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1036 op->num_bytes, op->level >=
1037 BTRFS_FIRST_FREE_OBJECTID);
1038 mutex_unlock(&info->pinned_mutex);
1039 BUG_ON(ret < 0);
1040 op->del = ret;
1041
1042 /*
1043 * we need to see if we can delete multiple things at once, so
1044 * start looping through the list of extents we are wanting to
1045 * delete and see if their extent/backref's are right next to
1046 * eachother and the extents only have 1 ref
1047 */
1048 for (pos = cur->next; pos != del_list; pos = pos->next) {
1049 struct pending_extent_op *tmp;
1050
1051 tmp = list_entry(pos, struct pending_extent_op, list);
1052
1053 /* we only want to delete extent+ref at this stage */
1054 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1055 break;
1056
1057 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1058 if (found_key.objectid != tmp->bytenr ||
1059 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1060 found_key.offset != tmp->num_bytes)
1061 break;
1062
1063 /* check to make sure this extent only has one ref */
1064 ei = btrfs_item_ptr(leaf, cur_slot,
1065 struct btrfs_extent_item);
1066 if (btrfs_extent_refs(leaf, ei) != 1)
1067 break;
1068
1069 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1070 if (found_key.objectid != tmp->bytenr ||
1071 found_key.type != BTRFS_EXTENT_REF_KEY ||
1072 found_key.offset != tmp->orig_parent)
1073 break;
1074
1075 /*
1076 * the ref is right next to the extent, we can set the
1077 * ref count to 0 since we will delete them both now
1078 */
1079 btrfs_set_extent_refs(leaf, ei, 0);
1080
1081 /* pin down the bytes for this extent */
1082 mutex_lock(&info->pinned_mutex);
1083 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1084 tmp->num_bytes, tmp->level >=
1085 BTRFS_FIRST_FREE_OBJECTID);
1086 mutex_unlock(&info->pinned_mutex);
1087 BUG_ON(ret < 0);
1088
1089 /*
1090 * use the del field to tell if we need to go ahead and
1091 * free up the extent when we delete the item or not.
1092 */
1093 tmp->del = ret;
1094 bytes_freed += tmp->num_bytes;
1095
1096 num_to_del += 2;
1097 cur_slot += 2;
1098 }
1099 end = pos;
1100
1101 /* update the free space counters */
1102 spin_lock(&info->delalloc_lock);
1103 super_used = btrfs_super_bytes_used(&info->super_copy);
1104 btrfs_set_super_bytes_used(&info->super_copy,
1105 super_used - bytes_freed);
1106
1107 root_used = btrfs_root_used(&extent_root->root_item);
1108 btrfs_set_root_used(&extent_root->root_item,
1109 root_used - bytes_freed);
1110 spin_unlock(&info->delalloc_lock);
1111
1112 /* delete the items */
1113 ret = btrfs_del_items(trans, extent_root, path,
1114 path->slots[0], num_to_del);
1115 BUG_ON(ret);
1116
1117 /*
1118 * loop through the extents we deleted and do the cleanup work
1119 * on them
1120 */
1121 for (pos = cur, n = pos->next; pos != end;
1122 pos = n, n = pos->next) {
1123 struct pending_extent_op *tmp;
1124 tmp = list_entry(pos, struct pending_extent_op, list);
1125
1126 /*
1127 * remember tmp->del tells us wether or not we pinned
1128 * down the extent
1129 */
1130 ret = update_block_group(trans, extent_root,
1131 tmp->bytenr, tmp->num_bytes, 0,
1132 tmp->del);
1133 BUG_ON(ret);
1134
1135 list_del_init(&tmp->list);
1136 unlock_extent(&info->extent_ins, tmp->bytenr,
1137 tmp->bytenr + tmp->num_bytes - 1,
1138 GFP_NOFS);
1139 kfree(tmp);
1140 }
1141 } else if (refs && found_extent) {
1142 /*
1143 * the ref and extent were right next to eachother, but the
1144 * extent still has a ref, so just free the backref and keep
1145 * going
1146 */
1147 ret = remove_extent_backref(trans, extent_root, path);
1148 BUG_ON(ret);
1149
1150 list_del_init(&op->list);
1151 unlock_extent(&info->extent_ins, op->bytenr,
1152 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1153 kfree(op);
1154 } else {
1155 /*
1156 * the extent has multiple refs and the backref we were looking
1157 * for was not right next to it, so just unlock and go next,
1158 * we're good to go
1159 */
1160 list_del_init(&op->list);
1161 unlock_extent(&info->extent_ins, op->bytenr,
1162 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1163 kfree(op);
1164 }
1165
1166 btrfs_release_path(extent_root, path);
1167 if (!list_empty(del_list))
1168 goto search;
1169
1170out:
1171 btrfs_free_path(path);
1172 return ret;
1173}
1174
1175static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, 689static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root, u64 bytenr, 690 struct btrfs_root *root, u64 bytenr,
691 u64 num_bytes,
1177 u64 orig_parent, u64 parent, 692 u64 orig_parent, u64 parent,
1178 u64 orig_root, u64 ref_root, 693 u64 orig_root, u64 ref_root,
1179 u64 orig_generation, u64 ref_generation, 694 u64 orig_generation, u64 ref_generation,
1180 u64 owner_objectid) 695 u64 owner_objectid)
1181{ 696{
1182 int ret; 697 int ret;
1183 struct btrfs_root *extent_root = root->fs_info->extent_root; 698 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
1184 struct btrfs_path *path;
1185 699
1186 if (root == root->fs_info->extent_root) { 700 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
1187 struct pending_extent_op *extent_op; 701 orig_parent, parent, orig_root,
1188 u64 num_bytes; 702 ref_root, orig_generation,
1189 703 ref_generation, owner_objectid, pin);
1190 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1191 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1192 mutex_lock(&root->fs_info->extent_ins_mutex);
1193 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1194 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1195 u64 priv;
1196 ret = get_state_private(&root->fs_info->extent_ins,
1197 bytenr, &priv);
1198 BUG_ON(ret);
1199 extent_op = (struct pending_extent_op *)
1200 (unsigned long)priv;
1201 BUG_ON(extent_op->parent != orig_parent);
1202 BUG_ON(extent_op->generation != orig_generation);
1203
1204 extent_op->parent = parent;
1205 extent_op->generation = ref_generation;
1206 } else {
1207 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1208 BUG_ON(!extent_op);
1209
1210 extent_op->type = PENDING_BACKREF_UPDATE;
1211 extent_op->bytenr = bytenr;
1212 extent_op->num_bytes = num_bytes;
1213 extent_op->parent = parent;
1214 extent_op->orig_parent = orig_parent;
1215 extent_op->generation = ref_generation;
1216 extent_op->orig_generation = orig_generation;
1217 extent_op->level = (int)owner_objectid;
1218 INIT_LIST_HEAD(&extent_op->list);
1219 extent_op->del = 0;
1220
1221 set_extent_bits(&root->fs_info->extent_ins,
1222 bytenr, bytenr + num_bytes - 1,
1223 EXTENT_WRITEBACK, GFP_NOFS);
1224 set_state_private(&root->fs_info->extent_ins,
1225 bytenr, (unsigned long)extent_op);
1226 }
1227 mutex_unlock(&root->fs_info->extent_ins_mutex);
1228 return 0;
1229 }
1230
1231 path = btrfs_alloc_path();
1232 if (!path)
1233 return -ENOMEM;
1234 ret = lookup_extent_backref(trans, extent_root, path,
1235 bytenr, orig_parent, orig_root,
1236 orig_generation, owner_objectid, 1);
1237 if (ret)
1238 goto out;
1239 ret = remove_extent_backref(trans, extent_root, path);
1240 if (ret)
1241 goto out;
1242 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1243 parent, ref_root, ref_generation,
1244 owner_objectid);
1245 BUG_ON(ret); 704 BUG_ON(ret);
1246 finish_current_insert(trans, extent_root, 0);
1247 del_pending_extents(trans, extent_root, 0);
1248out:
1249 btrfs_free_path(path);
1250 return ret; 705 return ret;
1251} 706}
1252 707
1253int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, 708int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1254 struct btrfs_root *root, u64 bytenr, 709 struct btrfs_root *root, u64 bytenr,
1255 u64 orig_parent, u64 parent, 710 u64 num_bytes, u64 orig_parent, u64 parent,
1256 u64 ref_root, u64 ref_generation, 711 u64 ref_root, u64 ref_generation,
1257 u64 owner_objectid) 712 u64 owner_objectid)
1258{ 713{
@@ -1260,20 +715,36 @@ int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1260 if (ref_root == BTRFS_TREE_LOG_OBJECTID && 715 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1261 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) 716 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1262 return 0; 717 return 0;
1263 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, 718
1264 parent, ref_root, ref_root, 719 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
1265 ref_generation, ref_generation, 720 orig_parent, parent, ref_root,
1266 owner_objectid); 721 ref_root, ref_generation,
722 ref_generation, owner_objectid);
1267 return ret; 723 return ret;
1268} 724}
1269
1270static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 725static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1271 struct btrfs_root *root, u64 bytenr, 726 struct btrfs_root *root, u64 bytenr,
727 u64 num_bytes,
1272 u64 orig_parent, u64 parent, 728 u64 orig_parent, u64 parent,
1273 u64 orig_root, u64 ref_root, 729 u64 orig_root, u64 ref_root,
1274 u64 orig_generation, u64 ref_generation, 730 u64 orig_generation, u64 ref_generation,
1275 u64 owner_objectid) 731 u64 owner_objectid)
1276{ 732{
733 int ret;
734
735 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
736 ref_generation, owner_objectid,
737 BTRFS_ADD_DELAYED_REF, 0);
738 BUG_ON(ret);
739 return ret;
740}
741
742static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root, u64 bytenr,
744 u64 num_bytes, u64 parent, u64 ref_root,
745 u64 ref_generation, u64 owner_objectid,
746 int refs_to_add)
747{
1277 struct btrfs_path *path; 748 struct btrfs_path *path;
1278 int ret; 749 int ret;
1279 struct btrfs_key key; 750 struct btrfs_key key;
@@ -1286,17 +757,24 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1286 return -ENOMEM; 757 return -ENOMEM;
1287 758
1288 path->reada = 1; 759 path->reada = 1;
760 path->leave_spinning = 1;
1289 key.objectid = bytenr; 761 key.objectid = bytenr;
1290 key.type = BTRFS_EXTENT_ITEM_KEY; 762 key.type = BTRFS_EXTENT_ITEM_KEY;
1291 key.offset = (u64)-1; 763 key.offset = num_bytes;
1292 764
1293 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, 765 /* first find the extent item and update its reference count */
1294 0, 1); 766 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1295 if (ret < 0) 767 path, 0, 1);
768 if (ret < 0) {
769 btrfs_set_path_blocking(path);
1296 return ret; 770 return ret;
1297 BUG_ON(ret == 0 || path->slots[0] == 0); 771 }
1298 772
1299 path->slots[0]--; 773 if (ret > 0) {
774 WARN_ON(1);
775 btrfs_free_path(path);
776 return -EIO;
777 }
1300 l = path->nodes[0]; 778 l = path->nodes[0];
1301 779
1302 btrfs_item_key_to_cpu(l, &key, path->slots[0]); 780 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
@@ -1310,21 +788,24 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1310 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); 788 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1311 789
1312 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); 790 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
791
1313 refs = btrfs_extent_refs(l, item); 792 refs = btrfs_extent_refs(l, item);
1314 btrfs_set_extent_refs(l, item, refs + 1); 793 btrfs_set_extent_refs(l, item, refs + refs_to_add);
794 btrfs_unlock_up_safe(path, 1);
795
1315 btrfs_mark_buffer_dirty(path->nodes[0]); 796 btrfs_mark_buffer_dirty(path->nodes[0]);
1316 797
1317 btrfs_release_path(root->fs_info->extent_root, path); 798 btrfs_release_path(root->fs_info->extent_root, path);
1318 799
1319 path->reada = 1; 800 path->reada = 1;
801 path->leave_spinning = 1;
802
803 /* now insert the actual backref */
1320 ret = insert_extent_backref(trans, root->fs_info->extent_root, 804 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1321 path, bytenr, parent, 805 path, bytenr, parent,
1322 ref_root, ref_generation, 806 ref_root, ref_generation,
1323 owner_objectid); 807 owner_objectid, refs_to_add);
1324 BUG_ON(ret); 808 BUG_ON(ret);
1325 finish_current_insert(trans, root->fs_info->extent_root, 0);
1326 del_pending_extents(trans, root->fs_info->extent_root, 0);
1327
1328 btrfs_free_path(path); 809 btrfs_free_path(path);
1329 return 0; 810 return 0;
1330} 811}
@@ -1339,68 +820,278 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1339 if (ref_root == BTRFS_TREE_LOG_OBJECTID && 820 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1340 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) 821 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1341 return 0; 822 return 0;
1342 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, 823
824 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
1343 0, ref_root, 0, ref_generation, 825 0, ref_root, 0, ref_generation,
1344 owner_objectid); 826 owner_objectid);
1345 return ret; 827 return ret;
1346} 828}
1347 829
1348int btrfs_extent_post_op(struct btrfs_trans_handle *trans, 830static int drop_delayed_ref(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root) 831 struct btrfs_root *root,
832 struct btrfs_delayed_ref_node *node)
833{
834 int ret = 0;
835 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
836
837 BUG_ON(node->ref_mod == 0);
838 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
839 node->parent, ref->root, ref->generation,
840 ref->owner_objectid, ref->pin, node->ref_mod);
841
842 return ret;
843}
844
845/* helper function to actually process a single delayed ref entry */
846static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
847 struct btrfs_root *root,
848 struct btrfs_delayed_ref_node *node,
849 int insert_reserved)
1350{ 850{
1351 u64 start;
1352 u64 end;
1353 int ret; 851 int ret;
852 struct btrfs_delayed_ref *ref;
1354 853
1355 while(1) { 854 if (node->parent == (u64)-1) {
1356 finish_current_insert(trans, root->fs_info->extent_root, 1); 855 struct btrfs_delayed_ref_head *head;
1357 del_pending_extents(trans, root->fs_info->extent_root, 1); 856 /*
857 * we've hit the end of the chain and we were supposed
858 * to insert this extent into the tree. But, it got
859 * deleted before we ever needed to insert it, so all
860 * we have to do is clean up the accounting
861 */
862 if (insert_reserved) {
863 update_reserved_extents(root, node->bytenr,
864 node->num_bytes, 0);
865 }
866 head = btrfs_delayed_node_to_head(node);
867 mutex_unlock(&head->mutex);
868 return 0;
869 }
1358 870
1359 /* is there more work to do? */ 871 ref = btrfs_delayed_node_to_ref(node);
1360 ret = find_first_extent_bit(&root->fs_info->pending_del, 872 if (ref->action == BTRFS_ADD_DELAYED_REF) {
1361 0, &start, &end, EXTENT_WRITEBACK); 873 if (insert_reserved) {
1362 if (!ret) 874 struct btrfs_key ins;
1363 continue; 875
1364 ret = find_first_extent_bit(&root->fs_info->extent_ins, 876 ins.objectid = node->bytenr;
1365 0, &start, &end, EXTENT_WRITEBACK); 877 ins.offset = node->num_bytes;
1366 if (!ret) 878 ins.type = BTRFS_EXTENT_ITEM_KEY;
1367 continue; 879
1368 break; 880 /* record the full extent allocation */
881 ret = __btrfs_alloc_reserved_extent(trans, root,
882 node->parent, ref->root,
883 ref->generation, ref->owner_objectid,
884 &ins, node->ref_mod);
885 update_reserved_extents(root, node->bytenr,
886 node->num_bytes, 0);
887 } else {
888 /* just add one backref */
889 ret = add_extent_ref(trans, root, node->bytenr,
890 node->num_bytes,
891 node->parent, ref->root, ref->generation,
892 ref->owner_objectid, node->ref_mod);
893 }
894 BUG_ON(ret);
895 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
896 WARN_ON(insert_reserved);
897 ret = drop_delayed_ref(trans, root, node);
1369 } 898 }
1370 return 0; 899 return 0;
1371} 900}
1372 901
1373int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, 902static noinline struct btrfs_delayed_ref_node *
1374 struct btrfs_root *root, u64 bytenr, 903select_delayed_ref(struct btrfs_delayed_ref_head *head)
1375 u64 num_bytes, u32 *refs)
1376{ 904{
1377 struct btrfs_path *path; 905 struct rb_node *node;
906 struct btrfs_delayed_ref_node *ref;
907 int action = BTRFS_ADD_DELAYED_REF;
908again:
909 /*
910 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
911 * this prevents ref count from going down to zero when
912 * there still are pending delayed ref.
913 */
914 node = rb_prev(&head->node.rb_node);
915 while (1) {
916 if (!node)
917 break;
918 ref = rb_entry(node, struct btrfs_delayed_ref_node,
919 rb_node);
920 if (ref->bytenr != head->node.bytenr)
921 break;
922 if (btrfs_delayed_node_to_ref(ref)->action == action)
923 return ref;
924 node = rb_prev(node);
925 }
926 if (action == BTRFS_ADD_DELAYED_REF) {
927 action = BTRFS_DROP_DELAYED_REF;
928 goto again;
929 }
930 return NULL;
931}
932
933static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
934 struct btrfs_root *root,
935 struct list_head *cluster)
936{
937 struct btrfs_delayed_ref_root *delayed_refs;
938 struct btrfs_delayed_ref_node *ref;
939 struct btrfs_delayed_ref_head *locked_ref = NULL;
1378 int ret; 940 int ret;
1379 struct btrfs_key key; 941 int count = 0;
1380 struct extent_buffer *l; 942 int must_insert_reserved = 0;
1381 struct btrfs_extent_item *item;
1382 943
1383 WARN_ON(num_bytes < root->sectorsize); 944 delayed_refs = &trans->transaction->delayed_refs;
1384 path = btrfs_alloc_path(); 945 while (1) {
1385 path->reada = 1; 946 if (!locked_ref) {
1386 key.objectid = bytenr; 947 /* pick a new head ref from the cluster list */
1387 key.offset = num_bytes; 948 if (list_empty(cluster))
1388 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 949 break;
1389 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, 950
1390 0, 0); 951 locked_ref = list_entry(cluster->next,
1391 if (ret < 0) 952 struct btrfs_delayed_ref_head, cluster);
1392 goto out; 953
1393 if (ret != 0) { 954 /* grab the lock that says we are going to process
1394 btrfs_print_leaf(root, path->nodes[0]); 955 * all the refs for this head */
1395 printk(KERN_INFO "btrfs failed to find block number %llu\n", 956 ret = btrfs_delayed_ref_lock(trans, locked_ref);
1396 (unsigned long long)bytenr); 957
1397 BUG(); 958 /*
959 * we may have dropped the spin lock to get the head
960 * mutex lock, and that might have given someone else
961 * time to free the head. If that's true, it has been
962 * removed from our list and we can move on.
963 */
964 if (ret == -EAGAIN) {
965 locked_ref = NULL;
966 count++;
967 continue;
968 }
969 }
970
971 /*
972 * record the must insert reserved flag before we
973 * drop the spin lock.
974 */
975 must_insert_reserved = locked_ref->must_insert_reserved;
976 locked_ref->must_insert_reserved = 0;
977
978 /*
979 * locked_ref is the head node, so we have to go one
980 * node back for any delayed ref updates
981 */
982 ref = select_delayed_ref(locked_ref);
983 if (!ref) {
984 /* All delayed refs have been processed, Go ahead
985 * and send the head node to run_one_delayed_ref,
986 * so that any accounting fixes can happen
987 */
988 ref = &locked_ref->node;
989 list_del_init(&locked_ref->cluster);
990 locked_ref = NULL;
991 }
992
993 ref->in_tree = 0;
994 rb_erase(&ref->rb_node, &delayed_refs->root);
995 delayed_refs->num_entries--;
996 spin_unlock(&delayed_refs->lock);
997
998 ret = run_one_delayed_ref(trans, root, ref,
999 must_insert_reserved);
1000 BUG_ON(ret);
1001 btrfs_put_delayed_ref(ref);
1002
1003 count++;
1004 cond_resched();
1005 spin_lock(&delayed_refs->lock);
1006 }
1007 return count;
1008}
1009
1010/*
1011 * this starts processing the delayed reference count updates and
1012 * extent insertions we have queued up so far. count can be
1013 * 0, which means to process everything in the tree at the start
1014 * of the run (but not newly added entries), or it can be some target
1015 * number you'd like to process.
1016 */
1017int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1018 struct btrfs_root *root, unsigned long count)
1019{
1020 struct rb_node *node;
1021 struct btrfs_delayed_ref_root *delayed_refs;
1022 struct btrfs_delayed_ref_node *ref;
1023 struct list_head cluster;
1024 int ret;
1025 int run_all = count == (unsigned long)-1;
1026 int run_most = 0;
1027
1028 if (root == root->fs_info->extent_root)
1029 root = root->fs_info->tree_root;
1030
1031 delayed_refs = &trans->transaction->delayed_refs;
1032 INIT_LIST_HEAD(&cluster);
1033again:
1034 spin_lock(&delayed_refs->lock);
1035 if (count == 0) {
1036 count = delayed_refs->num_entries * 2;
1037 run_most = 1;
1038 }
1039 while (1) {
1040 if (!(run_all || run_most) &&
1041 delayed_refs->num_heads_ready < 64)
1042 break;
1043
1044 /*
1045 * go find something we can process in the rbtree. We start at
1046 * the beginning of the tree, and then build a cluster
1047 * of refs to process starting at the first one we are able to
1048 * lock
1049 */
1050 ret = btrfs_find_ref_cluster(trans, &cluster,
1051 delayed_refs->run_delayed_start);
1052 if (ret)
1053 break;
1054
1055 ret = run_clustered_refs(trans, root, &cluster);
1056 BUG_ON(ret < 0);
1057
1058 count -= min_t(unsigned long, ret, count);
1059
1060 if (count == 0)
1061 break;
1062 }
1063
1064 if (run_all) {
1065 node = rb_first(&delayed_refs->root);
1066 if (!node)
1067 goto out;
1068 count = (unsigned long)-1;
1069
1070 while (node) {
1071 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1072 rb_node);
1073 if (btrfs_delayed_ref_is_head(ref)) {
1074 struct btrfs_delayed_ref_head *head;
1075
1076 head = btrfs_delayed_node_to_head(ref);
1077 atomic_inc(&ref->refs);
1078
1079 spin_unlock(&delayed_refs->lock);
1080 mutex_lock(&head->mutex);
1081 mutex_unlock(&head->mutex);
1082
1083 btrfs_put_delayed_ref(ref);
1084 cond_resched();
1085 goto again;
1086 }
1087 node = rb_next(node);
1088 }
1089 spin_unlock(&delayed_refs->lock);
1090 schedule_timeout(1);
1091 goto again;
1398 } 1092 }
1399 l = path->nodes[0];
1400 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1401 *refs = btrfs_extent_refs(l, item);
1402out: 1093out:
1403 btrfs_free_path(path); 1094 spin_unlock(&delayed_refs->lock);
1404 return 0; 1095 return 0;
1405} 1096}
1406 1097
@@ -1624,7 +1315,7 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1624 int refi = 0; 1315 int refi = 0;
1625 int slot; 1316 int slot;
1626 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, 1317 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1627 u64, u64, u64, u64, u64, u64, u64, u64); 1318 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1628 1319
1629 ref_root = btrfs_header_owner(buf); 1320 ref_root = btrfs_header_owner(buf);
1630 ref_generation = btrfs_header_generation(buf); 1321 ref_generation = btrfs_header_generation(buf);
@@ -1696,12 +1387,19 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1696 1387
1697 if (level == 0) { 1388 if (level == 0) {
1698 btrfs_item_key_to_cpu(buf, &key, slot); 1389 btrfs_item_key_to_cpu(buf, &key, slot);
1390 fi = btrfs_item_ptr(buf, slot,
1391 struct btrfs_file_extent_item);
1392
1393 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1394 if (bytenr == 0)
1395 continue;
1699 1396
1700 ret = process_func(trans, root, bytenr, 1397 ret = process_func(trans, root, bytenr,
1701 orig_buf->start, buf->start, 1398 btrfs_file_extent_disk_num_bytes(buf, fi),
1702 orig_root, ref_root, 1399 orig_buf->start, buf->start,
1703 orig_generation, ref_generation, 1400 orig_root, ref_root,
1704 key.objectid); 1401 orig_generation, ref_generation,
1402 key.objectid);
1705 1403
1706 if (ret) { 1404 if (ret) {
1707 faili = slot; 1405 faili = slot;
@@ -1709,7 +1407,7 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1709 goto fail; 1407 goto fail;
1710 } 1408 }
1711 } else { 1409 } else {
1712 ret = process_func(trans, root, bytenr, 1410 ret = process_func(trans, root, bytenr, buf->len,
1713 orig_buf->start, buf->start, 1411 orig_buf->start, buf->start,
1714 orig_root, ref_root, 1412 orig_root, ref_root,
1715 orig_generation, ref_generation, 1413 orig_generation, ref_generation,
@@ -1786,17 +1484,17 @@ int btrfs_update_ref(struct btrfs_trans_handle *trans,
1786 if (bytenr == 0) 1484 if (bytenr == 0)
1787 continue; 1485 continue;
1788 ret = __btrfs_update_extent_ref(trans, root, bytenr, 1486 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1789 orig_buf->start, buf->start, 1487 btrfs_file_extent_disk_num_bytes(buf, fi),
1790 orig_root, ref_root, 1488 orig_buf->start, buf->start,
1791 orig_generation, ref_generation, 1489 orig_root, ref_root, orig_generation,
1792 key.objectid); 1490 ref_generation, key.objectid);
1793 if (ret) 1491 if (ret)
1794 goto fail; 1492 goto fail;
1795 } else { 1493 } else {
1796 bytenr = btrfs_node_blockptr(buf, slot); 1494 bytenr = btrfs_node_blockptr(buf, slot);
1797 ret = __btrfs_update_extent_ref(trans, root, bytenr, 1495 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1798 orig_buf->start, buf->start, 1496 buf->len, orig_buf->start,
1799 orig_root, ref_root, 1497 buf->start, orig_root, ref_root,
1800 orig_generation, ref_generation, 1498 orig_generation, ref_generation,
1801 level - 1); 1499 level - 1);
1802 if (ret) 1500 if (ret)
@@ -1815,7 +1513,6 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
1815 struct btrfs_block_group_cache *cache) 1513 struct btrfs_block_group_cache *cache)
1816{ 1514{
1817 int ret; 1515 int ret;
1818 int pending_ret;
1819 struct btrfs_root *extent_root = root->fs_info->extent_root; 1516 struct btrfs_root *extent_root = root->fs_info->extent_root;
1820 unsigned long bi; 1517 unsigned long bi;
1821 struct extent_buffer *leaf; 1518 struct extent_buffer *leaf;
@@ -1831,12 +1528,8 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
1831 btrfs_mark_buffer_dirty(leaf); 1528 btrfs_mark_buffer_dirty(leaf);
1832 btrfs_release_path(extent_root, path); 1529 btrfs_release_path(extent_root, path);
1833fail: 1530fail:
1834 finish_current_insert(trans, extent_root, 0);
1835 pending_ret = del_pending_extents(trans, extent_root, 0);
1836 if (ret) 1531 if (ret)
1837 return ret; 1532 return ret;
1838 if (pending_ret)
1839 return pending_ret;
1840 return 0; 1533 return 0;
1841 1534
1842} 1535}
@@ -1900,7 +1593,7 @@ int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1900 if (!block_group || block_group->ro) 1593 if (!block_group || block_group->ro)
1901 readonly = 1; 1594 readonly = 1;
1902 if (block_group) 1595 if (block_group)
1903 put_block_group(block_group); 1596 btrfs_put_block_group(block_group);
1904 return readonly; 1597 return readonly;
1905} 1598}
1906 1599
@@ -2324,7 +2017,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
2324 WARN_ON(ret); 2017 WARN_ON(ret);
2325 } 2018 }
2326 } 2019 }
2327 put_block_group(cache); 2020 btrfs_put_block_group(cache);
2328 total -= num_bytes; 2021 total -= num_bytes;
2329 bytenr += num_bytes; 2022 bytenr += num_bytes;
2330 } 2023 }
@@ -2341,7 +2034,7 @@ static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2341 return 0; 2034 return 0;
2342 2035
2343 bytenr = cache->key.objectid; 2036 bytenr = cache->key.objectid;
2344 put_block_group(cache); 2037 btrfs_put_block_group(cache);
2345 2038
2346 return bytenr; 2039 return bytenr;
2347} 2040}
@@ -2353,7 +2046,6 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2353 struct btrfs_block_group_cache *cache; 2046 struct btrfs_block_group_cache *cache;
2354 struct btrfs_fs_info *fs_info = root->fs_info; 2047 struct btrfs_fs_info *fs_info = root->fs_info;
2355 2048
2356 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2357 if (pin) { 2049 if (pin) {
2358 set_extent_dirty(&fs_info->pinned_extents, 2050 set_extent_dirty(&fs_info->pinned_extents,
2359 bytenr, bytenr + num - 1, GFP_NOFS); 2051 bytenr, bytenr + num - 1, GFP_NOFS);
@@ -2361,6 +2053,7 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2361 clear_extent_dirty(&fs_info->pinned_extents, 2053 clear_extent_dirty(&fs_info->pinned_extents,
2362 bytenr, bytenr + num - 1, GFP_NOFS); 2054 bytenr, bytenr + num - 1, GFP_NOFS);
2363 } 2055 }
2056
2364 while (num > 0) { 2057 while (num > 0) {
2365 cache = btrfs_lookup_block_group(fs_info, bytenr); 2058 cache = btrfs_lookup_block_group(fs_info, bytenr);
2366 BUG_ON(!cache); 2059 BUG_ON(!cache);
@@ -2385,7 +2078,7 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2385 if (cache->cached) 2078 if (cache->cached)
2386 btrfs_add_free_space(cache, bytenr, len); 2079 btrfs_add_free_space(cache, bytenr, len);
2387 } 2080 }
2388 put_block_group(cache); 2081 btrfs_put_block_group(cache);
2389 bytenr += len; 2082 bytenr += len;
2390 num -= len; 2083 num -= len;
2391 } 2084 }
@@ -2416,7 +2109,7 @@ static int update_reserved_extents(struct btrfs_root *root,
2416 } 2109 }
2417 spin_unlock(&cache->lock); 2110 spin_unlock(&cache->lock);
2418 spin_unlock(&cache->space_info->lock); 2111 spin_unlock(&cache->space_info->lock);
2419 put_block_group(cache); 2112 btrfs_put_block_group(cache);
2420 bytenr += len; 2113 bytenr += len;
2421 num -= len; 2114 num -= len;
2422 } 2115 }
@@ -2431,7 +2124,6 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2431 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; 2124 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2432 int ret; 2125 int ret;
2433 2126
2434 mutex_lock(&root->fs_info->pinned_mutex);
2435 while (1) { 2127 while (1) {
2436 ret = find_first_extent_bit(pinned_extents, last, 2128 ret = find_first_extent_bit(pinned_extents, last,
2437 &start, &end, EXTENT_DIRTY); 2129 &start, &end, EXTENT_DIRTY);
@@ -2440,7 +2132,6 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2440 set_extent_dirty(copy, start, end, GFP_NOFS); 2132 set_extent_dirty(copy, start, end, GFP_NOFS);
2441 last = end + 1; 2133 last = end + 1;
2442 } 2134 }
2443 mutex_unlock(&root->fs_info->pinned_mutex);
2444 return 0; 2135 return 0;
2445} 2136}
2446 2137
@@ -2452,7 +2143,6 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2452 u64 end; 2143 u64 end;
2453 int ret; 2144 int ret;
2454 2145
2455 mutex_lock(&root->fs_info->pinned_mutex);
2456 while (1) { 2146 while (1) {
2457 ret = find_first_extent_bit(unpin, 0, &start, &end, 2147 ret = find_first_extent_bit(unpin, 0, &start, &end,
2458 EXTENT_DIRTY); 2148 EXTENT_DIRTY);
@@ -2461,209 +2151,20 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2461 2151
2462 ret = btrfs_discard_extent(root, start, end + 1 - start); 2152 ret = btrfs_discard_extent(root, start, end + 1 - start);
2463 2153
2154 /* unlocks the pinned mutex */
2464 btrfs_update_pinned_extents(root, start, end + 1 - start, 0); 2155 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2465 clear_extent_dirty(unpin, start, end, GFP_NOFS); 2156 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2466 2157
2467 if (need_resched()) { 2158 cond_resched();
2468 mutex_unlock(&root->fs_info->pinned_mutex);
2469 cond_resched();
2470 mutex_lock(&root->fs_info->pinned_mutex);
2471 }
2472 } 2159 }
2473 mutex_unlock(&root->fs_info->pinned_mutex);
2474 return ret; 2160 return ret;
2475} 2161}
2476 2162
2477static int finish_current_insert(struct btrfs_trans_handle *trans,
2478 struct btrfs_root *extent_root, int all)
2479{
2480 u64 start;
2481 u64 end;
2482 u64 priv;
2483 u64 search = 0;
2484 struct btrfs_fs_info *info = extent_root->fs_info;
2485 struct btrfs_path *path;
2486 struct pending_extent_op *extent_op, *tmp;
2487 struct list_head insert_list, update_list;
2488 int ret;
2489 int num_inserts = 0, max_inserts, restart = 0;
2490
2491 path = btrfs_alloc_path();
2492 INIT_LIST_HEAD(&insert_list);
2493 INIT_LIST_HEAD(&update_list);
2494
2495 max_inserts = extent_root->leafsize /
2496 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2497 sizeof(struct btrfs_extent_ref) +
2498 sizeof(struct btrfs_extent_item));
2499again:
2500 mutex_lock(&info->extent_ins_mutex);
2501 while (1) {
2502 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2503 &end, EXTENT_WRITEBACK);
2504 if (ret) {
2505 if (restart && !num_inserts &&
2506 list_empty(&update_list)) {
2507 restart = 0;
2508 search = 0;
2509 continue;
2510 }
2511 break;
2512 }
2513
2514 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2515 if (!ret) {
2516 if (all)
2517 restart = 1;
2518 search = end + 1;
2519 if (need_resched()) {
2520 mutex_unlock(&info->extent_ins_mutex);
2521 cond_resched();
2522 mutex_lock(&info->extent_ins_mutex);
2523 }
2524 continue;
2525 }
2526
2527 ret = get_state_private(&info->extent_ins, start, &priv);
2528 BUG_ON(ret);
2529 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2530
2531 if (extent_op->type == PENDING_EXTENT_INSERT) {
2532 num_inserts++;
2533 list_add_tail(&extent_op->list, &insert_list);
2534 search = end + 1;
2535 if (num_inserts == max_inserts) {
2536 restart = 1;
2537 break;
2538 }
2539 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2540 list_add_tail(&extent_op->list, &update_list);
2541 search = end + 1;
2542 } else {
2543 BUG();
2544 }
2545 }
2546
2547 /*
2548 * process the update list, clear the writeback bit for it, and if
2549 * somebody marked this thing for deletion then just unlock it and be
2550 * done, the free_extents will handle it
2551 */
2552 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2553 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2554 extent_op->bytenr + extent_op->num_bytes - 1,
2555 EXTENT_WRITEBACK, GFP_NOFS);
2556 if (extent_op->del) {
2557 list_del_init(&extent_op->list);
2558 unlock_extent(&info->extent_ins, extent_op->bytenr,
2559 extent_op->bytenr + extent_op->num_bytes
2560 - 1, GFP_NOFS);
2561 kfree(extent_op);
2562 }
2563 }
2564 mutex_unlock(&info->extent_ins_mutex);
2565
2566 /*
2567 * still have things left on the update list, go ahead an update
2568 * everything
2569 */
2570 if (!list_empty(&update_list)) {
2571 ret = update_backrefs(trans, extent_root, path, &update_list);
2572 BUG_ON(ret);
2573
2574 /* we may have COW'ed new blocks, so lets start over */
2575 if (all)
2576 restart = 1;
2577 }
2578
2579 /*
2580 * if no inserts need to be done, but we skipped some extents and we
2581 * need to make sure everything is cleaned then reset everything and
2582 * go back to the beginning
2583 */
2584 if (!num_inserts && restart) {
2585 search = 0;
2586 restart = 0;
2587 INIT_LIST_HEAD(&update_list);
2588 INIT_LIST_HEAD(&insert_list);
2589 goto again;
2590 } else if (!num_inserts) {
2591 goto out;
2592 }
2593
2594 /*
2595 * process the insert extents list. Again if we are deleting this
2596 * extent, then just unlock it, pin down the bytes if need be, and be
2597 * done with it. Saves us from having to actually insert the extent
2598 * into the tree and then subsequently come along and delete it
2599 */
2600 mutex_lock(&info->extent_ins_mutex);
2601 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2602 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2603 extent_op->bytenr + extent_op->num_bytes - 1,
2604 EXTENT_WRITEBACK, GFP_NOFS);
2605 if (extent_op->del) {
2606 u64 used;
2607 list_del_init(&extent_op->list);
2608 unlock_extent(&info->extent_ins, extent_op->bytenr,
2609 extent_op->bytenr + extent_op->num_bytes
2610 - 1, GFP_NOFS);
2611
2612 mutex_lock(&extent_root->fs_info->pinned_mutex);
2613 ret = pin_down_bytes(trans, extent_root,
2614 extent_op->bytenr,
2615 extent_op->num_bytes, 0);
2616 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2617
2618 spin_lock(&info->delalloc_lock);
2619 used = btrfs_super_bytes_used(&info->super_copy);
2620 btrfs_set_super_bytes_used(&info->super_copy,
2621 used - extent_op->num_bytes);
2622 used = btrfs_root_used(&extent_root->root_item);
2623 btrfs_set_root_used(&extent_root->root_item,
2624 used - extent_op->num_bytes);
2625 spin_unlock(&info->delalloc_lock);
2626
2627 ret = update_block_group(trans, extent_root,
2628 extent_op->bytenr,
2629 extent_op->num_bytes,
2630 0, ret > 0);
2631 BUG_ON(ret);
2632 kfree(extent_op);
2633 num_inserts--;
2634 }
2635 }
2636 mutex_unlock(&info->extent_ins_mutex);
2637
2638 ret = insert_extents(trans, extent_root, path, &insert_list,
2639 num_inserts);
2640 BUG_ON(ret);
2641
2642 /*
2643 * if restart is set for whatever reason we need to go back and start
2644 * searching through the pending list again.
2645 *
2646 * We just inserted some extents, which could have resulted in new
2647 * blocks being allocated, which would result in new blocks needing
2648 * updates, so if all is set we _must_ restart to get the updated
2649 * blocks.
2650 */
2651 if (restart || all) {
2652 INIT_LIST_HEAD(&insert_list);
2653 INIT_LIST_HEAD(&update_list);
2654 search = 0;
2655 restart = 0;
2656 num_inserts = 0;
2657 goto again;
2658 }
2659out:
2660 btrfs_free_path(path);
2661 return 0;
2662}
2663
2664static int pin_down_bytes(struct btrfs_trans_handle *trans, 2163static int pin_down_bytes(struct btrfs_trans_handle *trans,
2665 struct btrfs_root *root, 2164 struct btrfs_root *root,
2666 u64 bytenr, u64 num_bytes, int is_data) 2165 struct btrfs_path *path,
2166 u64 bytenr, u64 num_bytes, int is_data,
2167 struct extent_buffer **must_clean)
2667{ 2168{
2668 int err = 0; 2169 int err = 0;
2669 struct extent_buffer *buf; 2170 struct extent_buffer *buf;
@@ -2686,17 +2187,18 @@ static int pin_down_bytes(struct btrfs_trans_handle *trans,
2686 u64 header_transid = btrfs_header_generation(buf); 2187 u64 header_transid = btrfs_header_generation(buf);
2687 if (header_owner != BTRFS_TREE_LOG_OBJECTID && 2188 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2688 header_owner != BTRFS_TREE_RELOC_OBJECTID && 2189 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2190 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2689 header_transid == trans->transid && 2191 header_transid == trans->transid &&
2690 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 2192 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2691 clean_tree_block(NULL, root, buf); 2193 *must_clean = buf;
2692 btrfs_tree_unlock(buf);
2693 free_extent_buffer(buf);
2694 return 1; 2194 return 1;
2695 } 2195 }
2696 btrfs_tree_unlock(buf); 2196 btrfs_tree_unlock(buf);
2697 } 2197 }
2698 free_extent_buffer(buf); 2198 free_extent_buffer(buf);
2699pinit: 2199pinit:
2200 btrfs_set_path_blocking(path);
2201 /* unlocks the pinned mutex */
2700 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); 2202 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2701 2203
2702 BUG_ON(err < 0); 2204 BUG_ON(err < 0);
@@ -2710,7 +2212,8 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2710 struct btrfs_root *root, 2212 struct btrfs_root *root,
2711 u64 bytenr, u64 num_bytes, u64 parent, 2213 u64 bytenr, u64 num_bytes, u64 parent,
2712 u64 root_objectid, u64 ref_generation, 2214 u64 root_objectid, u64 ref_generation,
2713 u64 owner_objectid, int pin, int mark_free) 2215 u64 owner_objectid, int pin, int mark_free,
2216 int refs_to_drop)
2714{ 2217{
2715 struct btrfs_path *path; 2218 struct btrfs_path *path;
2716 struct btrfs_key key; 2219 struct btrfs_key key;
@@ -2732,6 +2235,7 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2732 return -ENOMEM; 2235 return -ENOMEM;
2733 2236
2734 path->reada = 1; 2237 path->reada = 1;
2238 path->leave_spinning = 1;
2735 ret = lookup_extent_backref(trans, extent_root, path, 2239 ret = lookup_extent_backref(trans, extent_root, path,
2736 bytenr, parent, root_objectid, 2240 bytenr, parent, root_objectid,
2737 ref_generation, owner_objectid, 1); 2241 ref_generation, owner_objectid, 1);
@@ -2753,9 +2257,11 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2753 break; 2257 break;
2754 } 2258 }
2755 if (!found_extent) { 2259 if (!found_extent) {
2756 ret = remove_extent_backref(trans, extent_root, path); 2260 ret = remove_extent_backref(trans, extent_root, path,
2261 refs_to_drop);
2757 BUG_ON(ret); 2262 BUG_ON(ret);
2758 btrfs_release_path(extent_root, path); 2263 btrfs_release_path(extent_root, path);
2264 path->leave_spinning = 1;
2759 ret = btrfs_search_slot(trans, extent_root, 2265 ret = btrfs_search_slot(trans, extent_root,
2760 &key, path, -1, 1); 2266 &key, path, -1, 1);
2761 if (ret) { 2267 if (ret) {
@@ -2771,8 +2277,9 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2771 btrfs_print_leaf(extent_root, path->nodes[0]); 2277 btrfs_print_leaf(extent_root, path->nodes[0]);
2772 WARN_ON(1); 2278 WARN_ON(1);
2773 printk(KERN_ERR "btrfs unable to find ref byte nr %llu " 2279 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2774 "root %llu gen %llu owner %llu\n", 2280 "parent %llu root %llu gen %llu owner %llu\n",
2775 (unsigned long long)bytenr, 2281 (unsigned long long)bytenr,
2282 (unsigned long long)parent,
2776 (unsigned long long)root_objectid, 2283 (unsigned long long)root_objectid,
2777 (unsigned long long)ref_generation, 2284 (unsigned long long)ref_generation,
2778 (unsigned long long)owner_objectid); 2285 (unsigned long long)owner_objectid);
@@ -2782,17 +2289,23 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2782 ei = btrfs_item_ptr(leaf, extent_slot, 2289 ei = btrfs_item_ptr(leaf, extent_slot,
2783 struct btrfs_extent_item); 2290 struct btrfs_extent_item);
2784 refs = btrfs_extent_refs(leaf, ei); 2291 refs = btrfs_extent_refs(leaf, ei);
2785 BUG_ON(refs == 0);
2786 refs -= 1;
2787 btrfs_set_extent_refs(leaf, ei, refs);
2788 2292
2293 /*
2294 * we're not allowed to delete the extent item if there
2295 * are other delayed ref updates pending
2296 */
2297
2298 BUG_ON(refs < refs_to_drop);
2299 refs -= refs_to_drop;
2300 btrfs_set_extent_refs(leaf, ei, refs);
2789 btrfs_mark_buffer_dirty(leaf); 2301 btrfs_mark_buffer_dirty(leaf);
2790 2302
2791 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { 2303 if (refs == 0 && found_extent &&
2304 path->slots[0] == extent_slot + 1) {
2792 struct btrfs_extent_ref *ref; 2305 struct btrfs_extent_ref *ref;
2793 ref = btrfs_item_ptr(leaf, path->slots[0], 2306 ref = btrfs_item_ptr(leaf, path->slots[0],
2794 struct btrfs_extent_ref); 2307 struct btrfs_extent_ref);
2795 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); 2308 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
2796 /* if the back ref and the extent are next to each other 2309 /* if the back ref and the extent are next to each other
2797 * they get deleted below in one shot 2310 * they get deleted below in one shot
2798 */ 2311 */
@@ -2800,11 +2313,13 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2800 num_to_del = 2; 2313 num_to_del = 2;
2801 } else if (found_extent) { 2314 } else if (found_extent) {
2802 /* otherwise delete the extent back ref */ 2315 /* otherwise delete the extent back ref */
2803 ret = remove_extent_backref(trans, extent_root, path); 2316 ret = remove_extent_backref(trans, extent_root, path,
2317 refs_to_drop);
2804 BUG_ON(ret); 2318 BUG_ON(ret);
2805 /* if refs are 0, we need to setup the path for deletion */ 2319 /* if refs are 0, we need to setup the path for deletion */
2806 if (refs == 0) { 2320 if (refs == 0) {
2807 btrfs_release_path(extent_root, path); 2321 btrfs_release_path(extent_root, path);
2322 path->leave_spinning = 1;
2808 ret = btrfs_search_slot(trans, extent_root, &key, path, 2323 ret = btrfs_search_slot(trans, extent_root, &key, path,
2809 -1, 1); 2324 -1, 1);
2810 BUG_ON(ret); 2325 BUG_ON(ret);
@@ -2814,16 +2329,18 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2814 if (refs == 0) { 2329 if (refs == 0) {
2815 u64 super_used; 2330 u64 super_used;
2816 u64 root_used; 2331 u64 root_used;
2332 struct extent_buffer *must_clean = NULL;
2817 2333
2818 if (pin) { 2334 if (pin) {
2819 mutex_lock(&root->fs_info->pinned_mutex); 2335 ret = pin_down_bytes(trans, root, path,
2820 ret = pin_down_bytes(trans, root, bytenr, num_bytes, 2336 bytenr, num_bytes,
2821 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); 2337 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
2822 mutex_unlock(&root->fs_info->pinned_mutex); 2338 &must_clean);
2823 if (ret > 0) 2339 if (ret > 0)
2824 mark_free = 1; 2340 mark_free = 1;
2825 BUG_ON(ret < 0); 2341 BUG_ON(ret < 0);
2826 } 2342 }
2343
2827 /* block accounting for super block */ 2344 /* block accounting for super block */
2828 spin_lock(&info->delalloc_lock); 2345 spin_lock(&info->delalloc_lock);
2829 super_used = btrfs_super_bytes_used(&info->super_copy); 2346 super_used = btrfs_super_bytes_used(&info->super_copy);
@@ -2835,14 +2352,34 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2835 btrfs_set_root_used(&root->root_item, 2352 btrfs_set_root_used(&root->root_item,
2836 root_used - num_bytes); 2353 root_used - num_bytes);
2837 spin_unlock(&info->delalloc_lock); 2354 spin_unlock(&info->delalloc_lock);
2355
2356 /*
2357 * it is going to be very rare for someone to be waiting
2358 * on the block we're freeing. del_items might need to
2359 * schedule, so rather than get fancy, just force it
2360 * to blocking here
2361 */
2362 if (must_clean)
2363 btrfs_set_lock_blocking(must_clean);
2364
2838 ret = btrfs_del_items(trans, extent_root, path, path->slots[0], 2365 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2839 num_to_del); 2366 num_to_del);
2840 BUG_ON(ret); 2367 BUG_ON(ret);
2841 btrfs_release_path(extent_root, path); 2368 btrfs_release_path(extent_root, path);
2842 2369
2370 if (must_clean) {
2371 clean_tree_block(NULL, root, must_clean);
2372 btrfs_tree_unlock(must_clean);
2373 free_extent_buffer(must_clean);
2374 }
2375
2843 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { 2376 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2844 ret = btrfs_del_csums(trans, root, bytenr, num_bytes); 2377 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2845 BUG_ON(ret); 2378 BUG_ON(ret);
2379 } else {
2380 invalidate_mapping_pages(info->btree_inode->i_mapping,
2381 bytenr >> PAGE_CACHE_SHIFT,
2382 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
2846 } 2383 }
2847 2384
2848 ret = update_block_group(trans, root, bytenr, num_bytes, 0, 2385 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
@@ -2850,218 +2387,103 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2850 BUG_ON(ret); 2387 BUG_ON(ret);
2851 } 2388 }
2852 btrfs_free_path(path); 2389 btrfs_free_path(path);
2853 finish_current_insert(trans, extent_root, 0);
2854 return ret; 2390 return ret;
2855} 2391}
2856 2392
2857/* 2393/*
2858 * find all the blocks marked as pending in the radix tree and remove 2394 * remove an extent from the root, returns 0 on success
2859 * them from the extent map
2860 */ 2395 */
2861static int del_pending_extents(struct btrfs_trans_handle *trans, 2396static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2862 struct btrfs_root *extent_root, int all) 2397 struct btrfs_root *root,
2398 u64 bytenr, u64 num_bytes, u64 parent,
2399 u64 root_objectid, u64 ref_generation,
2400 u64 owner_objectid, int pin,
2401 int refs_to_drop)
2863{ 2402{
2864 int ret; 2403 WARN_ON(num_bytes < root->sectorsize);
2865 int err = 0;
2866 u64 start;
2867 u64 end;
2868 u64 priv;
2869 u64 search = 0;
2870 int nr = 0, skipped = 0;
2871 struct extent_io_tree *pending_del;
2872 struct extent_io_tree *extent_ins;
2873 struct pending_extent_op *extent_op;
2874 struct btrfs_fs_info *info = extent_root->fs_info;
2875 struct list_head delete_list;
2876
2877 INIT_LIST_HEAD(&delete_list);
2878 extent_ins = &extent_root->fs_info->extent_ins;
2879 pending_del = &extent_root->fs_info->pending_del;
2880
2881again:
2882 mutex_lock(&info->extent_ins_mutex);
2883 while (1) {
2884 ret = find_first_extent_bit(pending_del, search, &start, &end,
2885 EXTENT_WRITEBACK);
2886 if (ret) {
2887 if (all && skipped && !nr) {
2888 search = 0;
2889 skipped = 0;
2890 continue;
2891 }
2892 mutex_unlock(&info->extent_ins_mutex);
2893 break;
2894 }
2895
2896 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2897 if (!ret) {
2898 search = end+1;
2899 skipped = 1;
2900
2901 if (need_resched()) {
2902 mutex_unlock(&info->extent_ins_mutex);
2903 cond_resched();
2904 mutex_lock(&info->extent_ins_mutex);
2905 }
2906
2907 continue;
2908 }
2909 BUG_ON(ret < 0);
2910
2911 ret = get_state_private(pending_del, start, &priv);
2912 BUG_ON(ret);
2913 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2914
2915 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2916 GFP_NOFS);
2917 if (!test_range_bit(extent_ins, start, end,
2918 EXTENT_WRITEBACK, 0)) {
2919 list_add_tail(&extent_op->list, &delete_list);
2920 nr++;
2921 } else {
2922 kfree(extent_op);
2923
2924 ret = get_state_private(&info->extent_ins, start,
2925 &priv);
2926 BUG_ON(ret);
2927 extent_op = (struct pending_extent_op *)
2928 (unsigned long)priv;
2929
2930 clear_extent_bits(&info->extent_ins, start, end,
2931 EXTENT_WRITEBACK, GFP_NOFS);
2932
2933 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2934 list_add_tail(&extent_op->list, &delete_list);
2935 search = end + 1;
2936 nr++;
2937 continue;
2938 }
2939
2940 mutex_lock(&extent_root->fs_info->pinned_mutex);
2941 ret = pin_down_bytes(trans, extent_root, start,
2942 end + 1 - start, 0);
2943 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2944
2945 ret = update_block_group(trans, extent_root, start,
2946 end + 1 - start, 0, ret > 0);
2947
2948 unlock_extent(extent_ins, start, end, GFP_NOFS);
2949 BUG_ON(ret);
2950 kfree(extent_op);
2951 }
2952 if (ret)
2953 err = ret;
2954
2955 search = end + 1;
2956
2957 if (need_resched()) {
2958 mutex_unlock(&info->extent_ins_mutex);
2959 cond_resched();
2960 mutex_lock(&info->extent_ins_mutex);
2961 }
2962 }
2963 2404
2964 if (nr) { 2405 /*
2965 ret = free_extents(trans, extent_root, &delete_list); 2406 * if metadata always pin
2966 BUG_ON(ret); 2407 * if data pin when any transaction has committed this
2967 } 2408 */
2409 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2410 ref_generation != trans->transid)
2411 pin = 1;
2968 2412
2969 if (all && skipped) { 2413 if (ref_generation != trans->transid)
2970 INIT_LIST_HEAD(&delete_list); 2414 pin = 1;
2971 search = 0;
2972 nr = 0;
2973 goto again;
2974 }
2975 2415
2976 if (!err) 2416 return __free_extent(trans, root, bytenr, num_bytes, parent,
2977 finish_current_insert(trans, extent_root, 0); 2417 root_objectid, ref_generation,
2978 return err; 2418 owner_objectid, pin, pin == 0, refs_to_drop);
2979} 2419}
2980 2420
2981/* 2421/*
2982 * remove an extent from the root, returns 0 on success 2422 * when we free an extent, it is possible (and likely) that we free the last
2423 * delayed ref for that extent as well. This searches the delayed ref tree for
2424 * a given extent, and if there are no other delayed refs to be processed, it
2425 * removes it from the tree.
2983 */ 2426 */
2984static int __btrfs_free_extent(struct btrfs_trans_handle *trans, 2427static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2985 struct btrfs_root *root, 2428 struct btrfs_root *root, u64 bytenr)
2986 u64 bytenr, u64 num_bytes, u64 parent,
2987 u64 root_objectid, u64 ref_generation,
2988 u64 owner_objectid, int pin)
2989{ 2429{
2990 struct btrfs_root *extent_root = root->fs_info->extent_root; 2430 struct btrfs_delayed_ref_head *head;
2991 int pending_ret; 2431 struct btrfs_delayed_ref_root *delayed_refs;
2432 struct btrfs_delayed_ref_node *ref;
2433 struct rb_node *node;
2992 int ret; 2434 int ret;
2993 2435
2994 WARN_ON(num_bytes < root->sectorsize); 2436 delayed_refs = &trans->transaction->delayed_refs;
2995 if (root == extent_root) { 2437 spin_lock(&delayed_refs->lock);
2996 struct pending_extent_op *extent_op = NULL; 2438 head = btrfs_find_delayed_ref_head(trans, bytenr);
2997 2439 if (!head)
2998 mutex_lock(&root->fs_info->extent_ins_mutex); 2440 goto out;
2999 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
3000 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
3001 u64 priv;
3002 ret = get_state_private(&root->fs_info->extent_ins,
3003 bytenr, &priv);
3004 BUG_ON(ret);
3005 extent_op = (struct pending_extent_op *)
3006 (unsigned long)priv;
3007 2441
3008 extent_op->del = 1; 2442 node = rb_prev(&head->node.rb_node);
3009 if (extent_op->type == PENDING_EXTENT_INSERT) { 2443 if (!node)
3010 mutex_unlock(&root->fs_info->extent_ins_mutex); 2444 goto out;
3011 return 0;
3012 }
3013 }
3014 2445
3015 if (extent_op) { 2446 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3016 ref_generation = extent_op->orig_generation;
3017 parent = extent_op->orig_parent;
3018 }
3019 2447
3020 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); 2448 /* there are still entries for this ref, we can't drop it */
3021 BUG_ON(!extent_op); 2449 if (ref->bytenr == bytenr)
3022 2450 goto out;
3023 extent_op->type = PENDING_EXTENT_DELETE;
3024 extent_op->bytenr = bytenr;
3025 extent_op->num_bytes = num_bytes;
3026 extent_op->parent = parent;
3027 extent_op->orig_parent = parent;
3028 extent_op->generation = ref_generation;
3029 extent_op->orig_generation = ref_generation;
3030 extent_op->level = (int)owner_objectid;
3031 INIT_LIST_HEAD(&extent_op->list);
3032 extent_op->del = 0;
3033
3034 set_extent_bits(&root->fs_info->pending_del,
3035 bytenr, bytenr + num_bytes - 1,
3036 EXTENT_WRITEBACK, GFP_NOFS);
3037 set_state_private(&root->fs_info->pending_del,
3038 bytenr, (unsigned long)extent_op);
3039 mutex_unlock(&root->fs_info->extent_ins_mutex);
3040 return 0;
3041 }
3042 /* if metadata always pin */
3043 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3044 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3045 mutex_lock(&root->fs_info->pinned_mutex);
3046 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3047 mutex_unlock(&root->fs_info->pinned_mutex);
3048 update_reserved_extents(root, bytenr, num_bytes, 0);
3049 return 0;
3050 }
3051 pin = 1;
3052 }
3053 2451
3054 /* if data pin when any transaction has committed this */ 2452 /*
3055 if (ref_generation != trans->transid) 2453 * waiting for the lock here would deadlock. If someone else has it
3056 pin = 1; 2454 * locked they are already in the process of dropping it anyway
2455 */
2456 if (!mutex_trylock(&head->mutex))
2457 goto out;
3057 2458
3058 ret = __free_extent(trans, root, bytenr, num_bytes, parent, 2459 /*
3059 root_objectid, ref_generation, 2460 * at this point we have a head with no other entries. Go
3060 owner_objectid, pin, pin == 0); 2461 * ahead and process it.
2462 */
2463 head->node.in_tree = 0;
2464 rb_erase(&head->node.rb_node, &delayed_refs->root);
2465
2466 delayed_refs->num_entries--;
2467
2468 /*
2469 * we don't take a ref on the node because we're removing it from the
2470 * tree, so we just steal the ref the tree was holding.
2471 */
2472 delayed_refs->num_heads--;
2473 if (list_empty(&head->cluster))
2474 delayed_refs->num_heads_ready--;
3061 2475
3062 finish_current_insert(trans, root->fs_info->extent_root, 0); 2476 list_del_init(&head->cluster);
3063 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); 2477 spin_unlock(&delayed_refs->lock);
3064 return ret ? ret : pending_ret; 2478
2479 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2480 &head->node, head->must_insert_reserved);
2481 BUG_ON(ret);
2482 btrfs_put_delayed_ref(&head->node);
2483 return 0;
2484out:
2485 spin_unlock(&delayed_refs->lock);
2486 return 0;
3065} 2487}
3066 2488
3067int btrfs_free_extent(struct btrfs_trans_handle *trans, 2489int btrfs_free_extent(struct btrfs_trans_handle *trans,
@@ -3072,9 +2494,28 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
3072{ 2494{
3073 int ret; 2495 int ret;
3074 2496
3075 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, 2497 /*
3076 root_objectid, ref_generation, 2498 * tree log blocks never actually go into the extent allocation
3077 owner_objectid, pin); 2499 * tree, just update pinning info and exit early.
2500 *
2501 * data extents referenced by the tree log do need to have
2502 * their reference counts bumped.
2503 */
2504 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2505 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2506 /* unlocks the pinned mutex */
2507 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2508 update_reserved_extents(root, bytenr, num_bytes, 0);
2509 ret = 0;
2510 } else {
2511 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2512 root_objectid, ref_generation,
2513 owner_objectid,
2514 BTRFS_DROP_DELAYED_REF, 1);
2515 BUG_ON(ret);
2516 ret = check_ref_cleanup(trans, root, bytenr);
2517 BUG_ON(ret);
2518 }
3078 return ret; 2519 return ret;
3079} 2520}
3080 2521
@@ -3103,228 +2544,237 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
3103{ 2544{
3104 int ret = 0; 2545 int ret = 0;
3105 struct btrfs_root *root = orig_root->fs_info->extent_root; 2546 struct btrfs_root *root = orig_root->fs_info->extent_root;
3106 u64 total_needed = num_bytes; 2547 struct btrfs_free_cluster *last_ptr = NULL;
3107 u64 *last_ptr = NULL;
3108 u64 last_wanted = 0;
3109 struct btrfs_block_group_cache *block_group = NULL; 2548 struct btrfs_block_group_cache *block_group = NULL;
3110 int chunk_alloc_done = 0;
3111 int empty_cluster = 2 * 1024 * 1024; 2549 int empty_cluster = 2 * 1024 * 1024;
3112 int allowed_chunk_alloc = 0; 2550 int allowed_chunk_alloc = 0;
3113 struct list_head *head = NULL, *cur = NULL;
3114 int loop = 0;
3115 int extra_loop = 0;
3116 struct btrfs_space_info *space_info; 2551 struct btrfs_space_info *space_info;
2552 int last_ptr_loop = 0;
2553 int loop = 0;
3117 2554
3118 WARN_ON(num_bytes < root->sectorsize); 2555 WARN_ON(num_bytes < root->sectorsize);
3119 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); 2556 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
3120 ins->objectid = 0; 2557 ins->objectid = 0;
3121 ins->offset = 0; 2558 ins->offset = 0;
3122 2559
2560 space_info = __find_space_info(root->fs_info, data);
2561
3123 if (orig_root->ref_cows || empty_size) 2562 if (orig_root->ref_cows || empty_size)
3124 allowed_chunk_alloc = 1; 2563 allowed_chunk_alloc = 1;
3125 2564
3126 if (data & BTRFS_BLOCK_GROUP_METADATA) { 2565 if (data & BTRFS_BLOCK_GROUP_METADATA) {
3127 last_ptr = &root->fs_info->last_alloc; 2566 last_ptr = &root->fs_info->meta_alloc_cluster;
3128 if (!btrfs_test_opt(root, SSD)) 2567 if (!btrfs_test_opt(root, SSD))
3129 empty_cluster = 64 * 1024; 2568 empty_cluster = 64 * 1024;
3130 } 2569 }
3131 2570
3132 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) 2571 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3133 last_ptr = &root->fs_info->last_data_alloc; 2572 last_ptr = &root->fs_info->data_alloc_cluster;
2573 }
3134 2574
3135 if (last_ptr) { 2575 if (last_ptr) {
3136 if (*last_ptr) { 2576 spin_lock(&last_ptr->lock);
3137 hint_byte = *last_ptr; 2577 if (last_ptr->block_group)
3138 last_wanted = *last_ptr; 2578 hint_byte = last_ptr->window_start;
3139 } else 2579 spin_unlock(&last_ptr->lock);
3140 empty_size += empty_cluster;
3141 } else {
3142 empty_cluster = 0;
3143 } 2580 }
2581
3144 search_start = max(search_start, first_logical_byte(root, 0)); 2582 search_start = max(search_start, first_logical_byte(root, 0));
3145 search_start = max(search_start, hint_byte); 2583 search_start = max(search_start, hint_byte);
3146 2584
3147 if (last_wanted && search_start != last_wanted) { 2585 if (!last_ptr) {
3148 last_wanted = 0; 2586 empty_cluster = 0;
3149 empty_size += empty_cluster; 2587 loop = 1;
3150 } 2588 }
3151 2589
3152 total_needed += empty_size; 2590 if (search_start == hint_byte) {
3153 block_group = btrfs_lookup_block_group(root->fs_info, search_start); 2591 block_group = btrfs_lookup_block_group(root->fs_info,
3154 if (!block_group) 2592 search_start);
3155 block_group = btrfs_lookup_first_block_group(root->fs_info, 2593 if (block_group && block_group_bits(block_group, data)) {
3156 search_start); 2594 down_read(&space_info->groups_sem);
3157 space_info = __find_space_info(root->fs_info, data); 2595 goto have_block_group;
2596 } else if (block_group) {
2597 btrfs_put_block_group(block_group);
2598 }
2599 }
3158 2600
2601search:
3159 down_read(&space_info->groups_sem); 2602 down_read(&space_info->groups_sem);
3160 while (1) { 2603 list_for_each_entry(block_group, &space_info->block_groups, list) {
3161 struct btrfs_free_space *free_space; 2604 u64 offset;
3162 /*
3163 * the only way this happens if our hint points to a block
3164 * group thats not of the proper type, while looping this
3165 * should never happen
3166 */
3167 if (empty_size)
3168 extra_loop = 1;
3169 2605
3170 if (!block_group) 2606 atomic_inc(&block_group->count);
3171 goto new_group_no_lock; 2607 search_start = block_group->key.objectid;
3172 2608
2609have_block_group:
3173 if (unlikely(!block_group->cached)) { 2610 if (unlikely(!block_group->cached)) {
3174 mutex_lock(&block_group->cache_mutex); 2611 mutex_lock(&block_group->cache_mutex);
3175 ret = cache_block_group(root, block_group); 2612 ret = cache_block_group(root, block_group);
3176 mutex_unlock(&block_group->cache_mutex); 2613 mutex_unlock(&block_group->cache_mutex);
3177 if (ret) 2614 if (ret) {
2615 btrfs_put_block_group(block_group);
3178 break; 2616 break;
2617 }
3179 } 2618 }
3180 2619
3181 mutex_lock(&block_group->alloc_mutex);
3182 if (unlikely(!block_group_bits(block_group, data)))
3183 goto new_group;
3184
3185 if (unlikely(block_group->ro)) 2620 if (unlikely(block_group->ro))
3186 goto new_group; 2621 goto loop;
3187 2622
3188 free_space = btrfs_find_free_space(block_group, search_start, 2623 if (last_ptr) {
3189 total_needed); 2624 /*
3190 if (free_space) { 2625 * the refill lock keeps out other
3191 u64 start = block_group->key.objectid; 2626 * people trying to start a new cluster
3192 u64 end = block_group->key.objectid + 2627 */
3193 block_group->key.offset; 2628 spin_lock(&last_ptr->refill_lock);
2629 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
2630 num_bytes, search_start);
2631 if (offset) {
2632 /* we have a block, we're done */
2633 spin_unlock(&last_ptr->refill_lock);
2634 goto checks;
2635 }
3194 2636
3195 search_start = stripe_align(root, free_space->offset); 2637 spin_lock(&last_ptr->lock);
2638 /*
2639 * whoops, this cluster doesn't actually point to
2640 * this block group. Get a ref on the block
2641 * group is does point to and try again
2642 */
2643 if (!last_ptr_loop && last_ptr->block_group &&
2644 last_ptr->block_group != block_group) {
2645
2646 btrfs_put_block_group(block_group);
2647 block_group = last_ptr->block_group;
2648 atomic_inc(&block_group->count);
2649 spin_unlock(&last_ptr->lock);
2650 spin_unlock(&last_ptr->refill_lock);
2651
2652 last_ptr_loop = 1;
2653 search_start = block_group->key.objectid;
2654 goto have_block_group;
2655 }
2656 spin_unlock(&last_ptr->lock);
3196 2657
3197 /* move on to the next group */ 2658 /*
3198 if (search_start + num_bytes >= search_end) 2659 * this cluster didn't work out, free it and
3199 goto new_group; 2660 * start over
2661 */
2662 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3200 2663
3201 /* move on to the next group */ 2664 last_ptr_loop = 0;
3202 if (search_start + num_bytes > end)
3203 goto new_group;
3204 2665
3205 if (last_wanted && search_start != last_wanted) { 2666 /* allocate a cluster in this block group */
3206 total_needed += empty_cluster; 2667 ret = btrfs_find_space_cluster(trans,
3207 empty_size += empty_cluster; 2668 block_group, last_ptr,
3208 last_wanted = 0; 2669 offset, num_bytes,
2670 empty_cluster + empty_size);
2671 if (ret == 0) {
3209 /* 2672 /*
3210 * if search_start is still in this block group 2673 * now pull our allocation out of this
3211 * then we just re-search this block group 2674 * cluster
3212 */ 2675 */
3213 if (search_start >= start && 2676 offset = btrfs_alloc_from_cluster(block_group,
3214 search_start < end) { 2677 last_ptr, num_bytes,
3215 mutex_unlock(&block_group->alloc_mutex); 2678 search_start);
3216 continue; 2679 if (offset) {
2680 /* we found one, proceed */
2681 spin_unlock(&last_ptr->refill_lock);
2682 goto checks;
3217 } 2683 }
3218
3219 /* else we go to the next block group */
3220 goto new_group;
3221 } 2684 }
3222 2685 /*
3223 if (exclude_nr > 0 && 2686 * at this point we either didn't find a cluster
3224 (search_start + num_bytes > exclude_start && 2687 * or we weren't able to allocate a block from our
3225 search_start < exclude_start + exclude_nr)) { 2688 * cluster. Free the cluster we've been trying
3226 search_start = exclude_start + exclude_nr; 2689 * to use, and go to the next block group
3227 /* 2690 */
3228 * if search_start is still in this block group 2691 if (loop < 2) {
3229 * then we just re-search this block group 2692 btrfs_return_cluster_to_free_space(NULL,
3230 */ 2693 last_ptr);
3231 if (search_start >= start && 2694 spin_unlock(&last_ptr->refill_lock);
3232 search_start < end) { 2695 goto loop;
3233 mutex_unlock(&block_group->alloc_mutex);
3234 last_wanted = 0;
3235 continue;
3236 }
3237
3238 /* else we go to the next block group */
3239 goto new_group;
3240 } 2696 }
2697 spin_unlock(&last_ptr->refill_lock);
2698 }
3241 2699
3242 ins->objectid = search_start; 2700 offset = btrfs_find_space_for_alloc(block_group, search_start,
3243 ins->offset = num_bytes; 2701 num_bytes, empty_size);
2702 if (!offset)
2703 goto loop;
2704checks:
2705 search_start = stripe_align(root, offset);
3244 2706
3245 btrfs_remove_free_space_lock(block_group, search_start, 2707 /* move on to the next group */
3246 num_bytes); 2708 if (search_start + num_bytes >= search_end) {
3247 /* we are all good, lets return */ 2709 btrfs_add_free_space(block_group, offset, num_bytes);
3248 mutex_unlock(&block_group->alloc_mutex); 2710 goto loop;
3249 break;
3250 } 2711 }
3251new_group:
3252 mutex_unlock(&block_group->alloc_mutex);
3253 put_block_group(block_group);
3254 block_group = NULL;
3255new_group_no_lock:
3256 /* don't try to compare new allocations against the
3257 * last allocation any more
3258 */
3259 last_wanted = 0;
3260 2712
3261 /* 2713 /* move on to the next group */
3262 * Here's how this works. 2714 if (search_start + num_bytes >
3263 * loop == 0: we were searching a block group via a hint 2715 block_group->key.objectid + block_group->key.offset) {
3264 * and didn't find anything, so we start at 2716 btrfs_add_free_space(block_group, offset, num_bytes);
3265 * the head of the block groups and keep searching 2717 goto loop;
3266 * loop == 1: we're searching through all of the block groups 2718 }
3267 * if we hit the head again we have searched 2719
3268 * all of the block groups for this space and we 2720 if (exclude_nr > 0 &&
3269 * need to try and allocate, if we cant error out. 2721 (search_start + num_bytes > exclude_start &&
3270 * loop == 2: we allocated more space and are looping through 2722 search_start < exclude_start + exclude_nr)) {
3271 * all of the block groups again. 2723 search_start = exclude_start + exclude_nr;
3272 */ 2724
3273 if (loop == 0) { 2725 btrfs_add_free_space(block_group, offset, num_bytes);
3274 head = &space_info->block_groups; 2726 /*
3275 cur = head->next; 2727 * if search_start is still in this block group
3276 loop++; 2728 * then we just re-search this block group
3277 } else if (loop == 1 && cur == head) {
3278 int keep_going;
3279
3280 /* at this point we give up on the empty_size
3281 * allocations and just try to allocate the min
3282 * space.
3283 *
3284 * The extra_loop field was set if an empty_size
3285 * allocation was attempted above, and if this
3286 * is try we need to try the loop again without
3287 * the additional empty_size.
3288 */ 2729 */
3289 total_needed -= empty_size; 2730 if (search_start >= block_group->key.objectid &&
3290 empty_size = 0; 2731 search_start < (block_group->key.objectid +
3291 keep_going = extra_loop; 2732 block_group->key.offset))
3292 loop++; 2733 goto have_block_group;
2734 goto loop;
2735 }
3293 2736
3294 if (allowed_chunk_alloc && !chunk_alloc_done) { 2737 ins->objectid = search_start;
3295 up_read(&space_info->groups_sem); 2738 ins->offset = num_bytes;
3296 ret = do_chunk_alloc(trans, root, num_bytes + 2739
3297 2 * 1024 * 1024, data, 1); 2740 if (offset < search_start)
3298 down_read(&space_info->groups_sem); 2741 btrfs_add_free_space(block_group, offset,
3299 if (ret < 0) 2742 search_start - offset);
3300 goto loop_check; 2743 BUG_ON(offset > search_start);
3301 head = &space_info->block_groups; 2744
3302 /* 2745 /* we are all good, lets return */
3303 * we've allocated a new chunk, keep 2746 break;
3304 * trying 2747loop:
3305 */ 2748 btrfs_put_block_group(block_group);
3306 keep_going = 1; 2749 }
3307 chunk_alloc_done = 1; 2750 up_read(&space_info->groups_sem);
3308 } else if (!allowed_chunk_alloc) { 2751
3309 space_info->force_alloc = 1; 2752 /* loop == 0, try to find a clustered alloc in every block group
3310 } 2753 * loop == 1, try again after forcing a chunk allocation
3311loop_check: 2754 * loop == 2, set empty_size and empty_cluster to 0 and try again
3312 if (keep_going) { 2755 */
3313 cur = head->next; 2756 if (!ins->objectid && loop < 3 &&
3314 extra_loop = 0; 2757 (empty_size || empty_cluster || allowed_chunk_alloc)) {
3315 } else { 2758 if (loop >= 2) {
3316 break; 2759 empty_size = 0;
3317 } 2760 empty_cluster = 0;
3318 } else if (cur == head) {
3319 break;
3320 } 2761 }
3321 2762
3322 block_group = list_entry(cur, struct btrfs_block_group_cache, 2763 if (allowed_chunk_alloc) {
3323 list); 2764 ret = do_chunk_alloc(trans, root, num_bytes +
3324 atomic_inc(&block_group->count); 2765 2 * 1024 * 1024, data, 1);
2766 allowed_chunk_alloc = 0;
2767 } else {
2768 space_info->force_alloc = 1;
2769 }
3325 2770
3326 search_start = block_group->key.objectid; 2771 if (loop < 3) {
3327 cur = cur->next; 2772 loop++;
2773 goto search;
2774 }
2775 ret = -ENOSPC;
2776 } else if (!ins->objectid) {
2777 ret = -ENOSPC;
3328 } 2778 }
3329 2779
3330 /* we found what we needed */ 2780 /* we found what we needed */
@@ -3332,21 +2782,10 @@ loop_check:
3332 if (!(data & BTRFS_BLOCK_GROUP_DATA)) 2782 if (!(data & BTRFS_BLOCK_GROUP_DATA))
3333 trans->block_group = block_group->key.objectid; 2783 trans->block_group = block_group->key.objectid;
3334 2784
3335 if (last_ptr) 2785 btrfs_put_block_group(block_group);
3336 *last_ptr = ins->objectid + ins->offset;
3337 ret = 0; 2786 ret = 0;
3338 } else if (!ret) {
3339 printk(KERN_ERR "btrfs searching for %llu bytes, "
3340 "num_bytes %llu, loop %d, allowed_alloc %d\n",
3341 (unsigned long long)total_needed,
3342 (unsigned long long)num_bytes,
3343 loop, allowed_chunk_alloc);
3344 ret = -ENOSPC;
3345 } 2787 }
3346 if (block_group)
3347 put_block_group(block_group);
3348 2788
3349 up_read(&space_info->groups_sem);
3350 return ret; 2789 return ret;
3351} 2790}
3352 2791
@@ -3451,7 +2890,7 @@ int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3451 ret = btrfs_discard_extent(root, start, len); 2890 ret = btrfs_discard_extent(root, start, len);
3452 2891
3453 btrfs_add_free_space(cache, start, len); 2892 btrfs_add_free_space(cache, start, len);
3454 put_block_group(cache); 2893 btrfs_put_block_group(cache);
3455 update_reserved_extents(root, start, len, 0); 2894 update_reserved_extents(root, start, len, 0);
3456 2895
3457 return ret; 2896 return ret;
@@ -3475,10 +2914,10 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3475static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, 2914static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3476 struct btrfs_root *root, u64 parent, 2915 struct btrfs_root *root, u64 parent,
3477 u64 root_objectid, u64 ref_generation, 2916 u64 root_objectid, u64 ref_generation,
3478 u64 owner, struct btrfs_key *ins) 2917 u64 owner, struct btrfs_key *ins,
2918 int ref_mod)
3479{ 2919{
3480 int ret; 2920 int ret;
3481 int pending_ret;
3482 u64 super_used; 2921 u64 super_used;
3483 u64 root_used; 2922 u64 root_used;
3484 u64 num_bytes = ins->offset; 2923 u64 num_bytes = ins->offset;
@@ -3503,33 +2942,6 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3503 btrfs_set_root_used(&root->root_item, root_used + num_bytes); 2942 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3504 spin_unlock(&info->delalloc_lock); 2943 spin_unlock(&info->delalloc_lock);
3505 2944
3506 if (root == extent_root) {
3507 struct pending_extent_op *extent_op;
3508
3509 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3510 BUG_ON(!extent_op);
3511
3512 extent_op->type = PENDING_EXTENT_INSERT;
3513 extent_op->bytenr = ins->objectid;
3514 extent_op->num_bytes = ins->offset;
3515 extent_op->parent = parent;
3516 extent_op->orig_parent = 0;
3517 extent_op->generation = ref_generation;
3518 extent_op->orig_generation = 0;
3519 extent_op->level = (int)owner;
3520 INIT_LIST_HEAD(&extent_op->list);
3521 extent_op->del = 0;
3522
3523 mutex_lock(&root->fs_info->extent_ins_mutex);
3524 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3525 ins->objectid + ins->offset - 1,
3526 EXTENT_WRITEBACK, GFP_NOFS);
3527 set_state_private(&root->fs_info->extent_ins,
3528 ins->objectid, (unsigned long)extent_op);
3529 mutex_unlock(&root->fs_info->extent_ins_mutex);
3530 goto update_block;
3531 }
3532
3533 memcpy(&keys[0], ins, sizeof(*ins)); 2945 memcpy(&keys[0], ins, sizeof(*ins));
3534 keys[1].objectid = ins->objectid; 2946 keys[1].objectid = ins->objectid;
3535 keys[1].type = BTRFS_EXTENT_REF_KEY; 2947 keys[1].type = BTRFS_EXTENT_REF_KEY;
@@ -3540,37 +2952,31 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3540 path = btrfs_alloc_path(); 2952 path = btrfs_alloc_path();
3541 BUG_ON(!path); 2953 BUG_ON(!path);
3542 2954
2955 path->leave_spinning = 1;
3543 ret = btrfs_insert_empty_items(trans, extent_root, path, keys, 2956 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3544 sizes, 2); 2957 sizes, 2);
3545 BUG_ON(ret); 2958 BUG_ON(ret);
3546 2959
3547 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 2960 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3548 struct btrfs_extent_item); 2961 struct btrfs_extent_item);
3549 btrfs_set_extent_refs(path->nodes[0], extent_item, 1); 2962 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
3550 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, 2963 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3551 struct btrfs_extent_ref); 2964 struct btrfs_extent_ref);
3552 2965
3553 btrfs_set_ref_root(path->nodes[0], ref, root_objectid); 2966 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3554 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); 2967 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3555 btrfs_set_ref_objectid(path->nodes[0], ref, owner); 2968 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3556 btrfs_set_ref_num_refs(path->nodes[0], ref, 1); 2969 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
3557 2970
3558 btrfs_mark_buffer_dirty(path->nodes[0]); 2971 btrfs_mark_buffer_dirty(path->nodes[0]);
3559 2972
3560 trans->alloc_exclude_start = 0; 2973 trans->alloc_exclude_start = 0;
3561 trans->alloc_exclude_nr = 0; 2974 trans->alloc_exclude_nr = 0;
3562 btrfs_free_path(path); 2975 btrfs_free_path(path);
3563 finish_current_insert(trans, extent_root, 0);
3564 pending_ret = del_pending_extents(trans, extent_root, 0);
3565 2976
3566 if (ret) 2977 if (ret)
3567 goto out; 2978 goto out;
3568 if (pending_ret) {
3569 ret = pending_ret;
3570 goto out;
3571 }
3572 2979
3573update_block:
3574 ret = update_block_group(trans, root, ins->objectid, 2980 ret = update_block_group(trans, root, ins->objectid,
3575 ins->offset, 1, 0); 2981 ins->offset, 1, 0);
3576 if (ret) { 2982 if (ret) {
@@ -3592,9 +2998,12 @@ int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3592 2998
3593 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) 2999 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3594 return 0; 3000 return 0;
3595 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, 3001
3596 ref_generation, owner, ins); 3002 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3597 update_reserved_extents(root, ins->objectid, ins->offset, 0); 3003 ins->offset, parent, root_objectid,
3004 ref_generation, owner,
3005 BTRFS_ADD_DELAYED_EXTENT, 0);
3006 BUG_ON(ret);
3598 return ret; 3007 return ret;
3599} 3008}
3600 3009
@@ -3619,9 +3028,9 @@ int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3619 ret = btrfs_remove_free_space(block_group, ins->objectid, 3028 ret = btrfs_remove_free_space(block_group, ins->objectid,
3620 ins->offset); 3029 ins->offset);
3621 BUG_ON(ret); 3030 BUG_ON(ret);
3622 put_block_group(block_group); 3031 btrfs_put_block_group(block_group);
3623 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, 3032 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3624 ref_generation, owner, ins); 3033 ref_generation, owner, ins, 1);
3625 return ret; 3034 return ret;
3626} 3035}
3627 3036
@@ -3640,20 +3049,18 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3640 u64 search_end, struct btrfs_key *ins, u64 data) 3049 u64 search_end, struct btrfs_key *ins, u64 data)
3641{ 3050{
3642 int ret; 3051 int ret;
3643
3644 ret = __btrfs_reserve_extent(trans, root, num_bytes, 3052 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3645 min_alloc_size, empty_size, hint_byte, 3053 min_alloc_size, empty_size, hint_byte,
3646 search_end, ins, data); 3054 search_end, ins, data);
3647 BUG_ON(ret); 3055 BUG_ON(ret);
3648 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { 3056 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3649 ret = __btrfs_alloc_reserved_extent(trans, root, parent, 3057 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3650 root_objectid, ref_generation, 3058 ins->offset, parent, root_objectid,
3651 owner_objectid, ins); 3059 ref_generation, owner_objectid,
3060 BTRFS_ADD_DELAYED_EXTENT, 0);
3652 BUG_ON(ret); 3061 BUG_ON(ret);
3653
3654 } else {
3655 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3656 } 3062 }
3063 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3657 return ret; 3064 return ret;
3658} 3065}
3659 3066
@@ -3789,7 +3196,7 @@ int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3789 3196
3790 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 3197 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3791 3198
3792 ret = __btrfs_free_extent(trans, root, disk_bytenr, 3199 ret = btrfs_free_extent(trans, root, disk_bytenr,
3793 btrfs_file_extent_disk_num_bytes(leaf, fi), 3200 btrfs_file_extent_disk_num_bytes(leaf, fi),
3794 leaf->start, leaf_owner, leaf_generation, 3201 leaf->start, leaf_owner, leaf_generation,
3795 key.objectid, 0); 3202 key.objectid, 0);
@@ -3829,7 +3236,7 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3829 */ 3236 */
3830 for (i = 0; i < ref->nritems; i++) { 3237 for (i = 0; i < ref->nritems; i++) {
3831 info = ref->extents + sorted[i].slot; 3238 info = ref->extents + sorted[i].slot;
3832 ret = __btrfs_free_extent(trans, root, info->bytenr, 3239 ret = btrfs_free_extent(trans, root, info->bytenr,
3833 info->num_bytes, ref->bytenr, 3240 info->num_bytes, ref->bytenr,
3834 ref->owner, ref->generation, 3241 ref->owner, ref->generation,
3835 info->objectid, 0); 3242 info->objectid, 0);
@@ -3846,12 +3253,13 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3846 return 0; 3253 return 0;
3847} 3254}
3848 3255
3849static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, 3256static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3257 struct btrfs_root *root, u64 start,
3850 u64 len, u32 *refs) 3258 u64 len, u32 *refs)
3851{ 3259{
3852 int ret; 3260 int ret;
3853 3261
3854 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); 3262 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
3855 BUG_ON(ret); 3263 BUG_ON(ret);
3856 3264
3857#if 0 /* some debugging code in case we see problems here */ 3265#if 0 /* some debugging code in case we see problems here */
@@ -3959,7 +3367,8 @@ static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3959 * we just decrement it below and don't update any 3367 * we just decrement it below and don't update any
3960 * of the refs the leaf points to. 3368 * of the refs the leaf points to.
3961 */ 3369 */
3962 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); 3370 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3371 blocksize, &refs);
3963 BUG_ON(ret); 3372 BUG_ON(ret);
3964 if (refs != 1) 3373 if (refs != 1)
3965 continue; 3374 continue;
@@ -4010,7 +3419,7 @@ static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4010 */ 3419 */
4011 for (i = 0; i < refi; i++) { 3420 for (i = 0; i < refi; i++) {
4012 bytenr = sorted[i].bytenr; 3421 bytenr = sorted[i].bytenr;
4013 ret = __btrfs_free_extent(trans, root, bytenr, 3422 ret = btrfs_free_extent(trans, root, bytenr,
4014 blocksize, eb->start, 3423 blocksize, eb->start,
4015 root_owner, root_gen, 0, 1); 3424 root_owner, root_gen, 0, 1);
4016 BUG_ON(ret); 3425 BUG_ON(ret);
@@ -4053,7 +3462,7 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4053 3462
4054 WARN_ON(*level < 0); 3463 WARN_ON(*level < 0);
4055 WARN_ON(*level >= BTRFS_MAX_LEVEL); 3464 WARN_ON(*level >= BTRFS_MAX_LEVEL);
4056 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, 3465 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
4057 path->nodes[*level]->len, &refs); 3466 path->nodes[*level]->len, &refs);
4058 BUG_ON(ret); 3467 BUG_ON(ret);
4059 if (refs > 1) 3468 if (refs > 1)
@@ -4104,7 +3513,8 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4104 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); 3513 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4105 blocksize = btrfs_level_size(root, *level - 1); 3514 blocksize = btrfs_level_size(root, *level - 1);
4106 3515
4107 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); 3516 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3517 blocksize, &refs);
4108 BUG_ON(ret); 3518 BUG_ON(ret);
4109 3519
4110 /* 3520 /*
@@ -4119,7 +3529,7 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4119 root_gen = btrfs_header_generation(parent); 3529 root_gen = btrfs_header_generation(parent);
4120 path->slots[*level]++; 3530 path->slots[*level]++;
4121 3531
4122 ret = __btrfs_free_extent(trans, root, bytenr, 3532 ret = btrfs_free_extent(trans, root, bytenr,
4123 blocksize, parent->start, 3533 blocksize, parent->start,
4124 root_owner, root_gen, 3534 root_owner, root_gen,
4125 *level - 1, 1); 3535 *level - 1, 1);
@@ -4165,7 +3575,7 @@ out:
4165 * cleanup and free the reference on the last node 3575 * cleanup and free the reference on the last node
4166 * we processed 3576 * we processed
4167 */ 3577 */
4168 ret = __btrfs_free_extent(trans, root, bytenr, blocksize, 3578 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
4169 parent->start, root_owner, root_gen, 3579 parent->start, root_owner, root_gen,
4170 *level, 1); 3580 *level, 1);
4171 free_extent_buffer(path->nodes[*level]); 3581 free_extent_buffer(path->nodes[*level]);
@@ -4354,6 +3764,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4354 struct btrfs_path *path; 3764 struct btrfs_path *path;
4355 int i; 3765 int i;
4356 int orig_level; 3766 int orig_level;
3767 int update_count;
4357 struct btrfs_root_item *root_item = &root->root_item; 3768 struct btrfs_root_item *root_item = &root->root_item;
4358 3769
4359 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); 3770 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
@@ -4395,6 +3806,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4395 } 3806 }
4396 } 3807 }
4397 while (1) { 3808 while (1) {
3809 unsigned long update;
4398 wret = walk_down_tree(trans, root, path, &level); 3810 wret = walk_down_tree(trans, root, path, &level);
4399 if (wret > 0) 3811 if (wret > 0)
4400 break; 3812 break;
@@ -4407,12 +3819,21 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4407 break; 3819 break;
4408 if (wret < 0) 3820 if (wret < 0)
4409 ret = wret; 3821 ret = wret;
4410 if (trans->transaction->in_commit) { 3822 if (trans->transaction->in_commit ||
3823 trans->transaction->delayed_refs.flushing) {
4411 ret = -EAGAIN; 3824 ret = -EAGAIN;
4412 break; 3825 break;
4413 } 3826 }
4414 atomic_inc(&root->fs_info->throttle_gen); 3827 atomic_inc(&root->fs_info->throttle_gen);
4415 wake_up(&root->fs_info->transaction_throttle); 3828 wake_up(&root->fs_info->transaction_throttle);
3829 for (update_count = 0; update_count < 16; update_count++) {
3830 update = trans->delayed_ref_updates;
3831 trans->delayed_ref_updates = 0;
3832 if (update)
3833 btrfs_run_delayed_refs(trans, root, update);
3834 else
3835 break;
3836 }
4416 } 3837 }
4417 for (i = 0; i <= orig_level; i++) { 3838 for (i = 0; i <= orig_level; i++) {
4418 if (path->nodes[i]) { 3839 if (path->nodes[i]) {
@@ -5457,6 +4878,7 @@ static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
5457 root->root_key.objectid, 4878 root->root_key.objectid,
5458 trans->transid, key.objectid); 4879 trans->transid, key.objectid);
5459 BUG_ON(ret); 4880 BUG_ON(ret);
4881
5460 ret = btrfs_free_extent(trans, root, 4882 ret = btrfs_free_extent(trans, root,
5461 bytenr, num_bytes, leaf->start, 4883 bytenr, num_bytes, leaf->start,
5462 btrfs_header_owner(leaf), 4884 btrfs_header_owner(leaf),
@@ -5768,9 +5190,6 @@ static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5768 ref_path, NULL, NULL); 5190 ref_path, NULL, NULL);
5769 BUG_ON(ret); 5191 BUG_ON(ret);
5770 5192
5771 if (root == root->fs_info->extent_root)
5772 btrfs_extent_post_op(trans, root);
5773
5774 return 0; 5193 return 0;
5775} 5194}
5776 5195
@@ -6038,6 +5457,7 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6038 if (!path) 5457 if (!path)
6039 return -ENOMEM; 5458 return -ENOMEM;
6040 5459
5460 path->leave_spinning = 1;
6041 ret = btrfs_insert_empty_inode(trans, root, path, objectid); 5461 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6042 if (ret) 5462 if (ret)
6043 goto out; 5463 goto out;
@@ -6208,6 +5628,9 @@ again:
6208 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); 5628 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
6209 mutex_unlock(&root->fs_info->cleaner_mutex); 5629 mutex_unlock(&root->fs_info->cleaner_mutex);
6210 5630
5631 trans = btrfs_start_transaction(info->tree_root, 1);
5632 btrfs_commit_transaction(trans, info->tree_root);
5633
6211 while (1) { 5634 while (1) {
6212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5635 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6213 if (ret < 0) 5636 if (ret < 0)
@@ -6294,7 +5717,7 @@ next:
6294 WARN_ON(block_group->reserved > 0); 5717 WARN_ON(block_group->reserved > 0);
6295 WARN_ON(btrfs_block_group_used(&block_group->item) > 0); 5718 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6296 spin_unlock(&block_group->lock); 5719 spin_unlock(&block_group->lock);
6297 put_block_group(block_group); 5720 btrfs_put_block_group(block_group);
6298 ret = 0; 5721 ret = 0;
6299out: 5722out:
6300 btrfs_free_path(path); 5723 btrfs_free_path(path);
@@ -6421,9 +5844,10 @@ int btrfs_read_block_groups(struct btrfs_root *root)
6421 5844
6422 atomic_set(&cache->count, 1); 5845 atomic_set(&cache->count, 1);
6423 spin_lock_init(&cache->lock); 5846 spin_lock_init(&cache->lock);
6424 mutex_init(&cache->alloc_mutex); 5847 spin_lock_init(&cache->tree_lock);
6425 mutex_init(&cache->cache_mutex); 5848 mutex_init(&cache->cache_mutex);
6426 INIT_LIST_HEAD(&cache->list); 5849 INIT_LIST_HEAD(&cache->list);
5850 INIT_LIST_HEAD(&cache->cluster_list);
6427 read_extent_buffer(leaf, &cache->item, 5851 read_extent_buffer(leaf, &cache->item,
6428 btrfs_item_ptr_offset(leaf, path->slots[0]), 5852 btrfs_item_ptr_offset(leaf, path->slots[0]),
6429 sizeof(cache->item)); 5853 sizeof(cache->item));
@@ -6466,7 +5890,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6466 5890
6467 extent_root = root->fs_info->extent_root; 5891 extent_root = root->fs_info->extent_root;
6468 5892
6469 root->fs_info->last_trans_new_blockgroup = trans->transid; 5893 root->fs_info->last_trans_log_full_commit = trans->transid;
6470 5894
6471 cache = kzalloc(sizeof(*cache), GFP_NOFS); 5895 cache = kzalloc(sizeof(*cache), GFP_NOFS);
6472 if (!cache) 5896 if (!cache)
@@ -6477,9 +5901,10 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6477 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 5901 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
6478 atomic_set(&cache->count, 1); 5902 atomic_set(&cache->count, 1);
6479 spin_lock_init(&cache->lock); 5903 spin_lock_init(&cache->lock);
6480 mutex_init(&cache->alloc_mutex); 5904 spin_lock_init(&cache->tree_lock);
6481 mutex_init(&cache->cache_mutex); 5905 mutex_init(&cache->cache_mutex);
6482 INIT_LIST_HEAD(&cache->list); 5906 INIT_LIST_HEAD(&cache->list);
5907 INIT_LIST_HEAD(&cache->cluster_list);
6483 5908
6484 btrfs_set_block_group_used(&cache->item, bytes_used); 5909 btrfs_set_block_group_used(&cache->item, bytes_used);
6485 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); 5910 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
@@ -6500,9 +5925,6 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6500 sizeof(cache->item)); 5925 sizeof(cache->item));
6501 BUG_ON(ret); 5926 BUG_ON(ret);
6502 5927
6503 finish_current_insert(trans, extent_root, 0);
6504 ret = del_pending_extents(trans, extent_root, 0);
6505 BUG_ON(ret);
6506 set_avail_alloc_bits(extent_root->fs_info, type); 5928 set_avail_alloc_bits(extent_root->fs_info, type);
6507 5929
6508 return 0; 5930 return 0;
@@ -6542,8 +5964,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6542 spin_unlock(&block_group->space_info->lock); 5964 spin_unlock(&block_group->space_info->lock);
6543 block_group->space_info->full = 0; 5965 block_group->space_info->full = 0;
6544 5966
6545 put_block_group(block_group); 5967 btrfs_put_block_group(block_group);
6546 put_block_group(block_group); 5968 btrfs_put_block_group(block_group);
6547 5969
6548 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 5970 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6549 if (ret > 0) 5971 if (ret > 0)