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.c2154
1 files changed, 827 insertions, 1327 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index fefe83ad2059..35af93355063 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);
@@ -307,7 +312,7 @@ btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
307} 312}
308 313
309/* 314/*
310 * return the block group that contains teh given bytenr 315 * return the block group that contains the given bytenr
311 */ 316 */
312struct btrfs_block_group_cache *btrfs_lookup_block_group( 317struct btrfs_block_group_cache *btrfs_lookup_block_group(
313 struct btrfs_fs_info *info, 318 struct btrfs_fs_info *info,
@@ -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
1186 if (root == root->fs_info->extent_root) {
1187 struct pending_extent_op *extent_op;
1188 u64 num_bytes;
1189
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 699
1204 extent_op->parent = parent; 700 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
1205 extent_op->generation = ref_generation; 701 orig_parent, parent, orig_root,
1206 } else { 702 ref_root, orig_generation,
1207 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); 703 ref_generation, owner_objectid, pin);
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
@@ -2151,10 +1844,14 @@ again:
2151 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" 1844 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2152 ", %llu bytes_used, %llu bytes_reserved, " 1845 ", %llu bytes_used, %llu bytes_reserved, "
2153 "%llu bytes_pinned, %llu bytes_readonly, %llu may use" 1846 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
2154 "%llu total\n", bytes, data_sinfo->bytes_delalloc, 1847 "%llu total\n", (unsigned long long)bytes,
2155 data_sinfo->bytes_used, data_sinfo->bytes_reserved, 1848 (unsigned long long)data_sinfo->bytes_delalloc,
2156 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly, 1849 (unsigned long long)data_sinfo->bytes_used,
2157 data_sinfo->bytes_may_use, data_sinfo->total_bytes); 1850 (unsigned long long)data_sinfo->bytes_reserved,
1851 (unsigned long long)data_sinfo->bytes_pinned,
1852 (unsigned long long)data_sinfo->bytes_readonly,
1853 (unsigned long long)data_sinfo->bytes_may_use,
1854 (unsigned long long)data_sinfo->total_bytes);
2158 return -ENOSPC; 1855 return -ENOSPC;
2159 } 1856 }
2160 data_sinfo->bytes_may_use += bytes; 1857 data_sinfo->bytes_may_use += bytes;
@@ -2225,15 +1922,29 @@ void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2225 spin_unlock(&info->lock); 1922 spin_unlock(&info->lock);
2226} 1923}
2227 1924
1925static void force_metadata_allocation(struct btrfs_fs_info *info)
1926{
1927 struct list_head *head = &info->space_info;
1928 struct btrfs_space_info *found;
1929
1930 rcu_read_lock();
1931 list_for_each_entry_rcu(found, head, list) {
1932 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
1933 found->force_alloc = 1;
1934 }
1935 rcu_read_unlock();
1936}
1937
2228static int do_chunk_alloc(struct btrfs_trans_handle *trans, 1938static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *extent_root, u64 alloc_bytes, 1939 struct btrfs_root *extent_root, u64 alloc_bytes,
2230 u64 flags, int force) 1940 u64 flags, int force)
2231{ 1941{
2232 struct btrfs_space_info *space_info; 1942 struct btrfs_space_info *space_info;
1943 struct btrfs_fs_info *fs_info = extent_root->fs_info;
2233 u64 thresh; 1944 u64 thresh;
2234 int ret = 0; 1945 int ret = 0;
2235 1946
2236 mutex_lock(&extent_root->fs_info->chunk_mutex); 1947 mutex_lock(&fs_info->chunk_mutex);
2237 1948
2238 flags = btrfs_reduce_alloc_profile(extent_root, flags); 1949 flags = btrfs_reduce_alloc_profile(extent_root, flags);
2239 1950
@@ -2265,6 +1976,18 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2265 } 1976 }
2266 spin_unlock(&space_info->lock); 1977 spin_unlock(&space_info->lock);
2267 1978
1979 /*
1980 * if we're doing a data chunk, go ahead and make sure that
1981 * we keep a reasonable number of metadata chunks allocated in the
1982 * FS as well.
1983 */
1984 if (flags & BTRFS_BLOCK_GROUP_DATA) {
1985 fs_info->data_chunk_allocations++;
1986 if (!(fs_info->data_chunk_allocations %
1987 fs_info->metadata_ratio))
1988 force_metadata_allocation(fs_info);
1989 }
1990
2268 ret = btrfs_alloc_chunk(trans, extent_root, flags); 1991 ret = btrfs_alloc_chunk(trans, extent_root, flags);
2269 if (ret) 1992 if (ret)
2270 space_info->full = 1; 1993 space_info->full = 1;
@@ -2324,7 +2047,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
2324 WARN_ON(ret); 2047 WARN_ON(ret);
2325 } 2048 }
2326 } 2049 }
2327 put_block_group(cache); 2050 btrfs_put_block_group(cache);
2328 total -= num_bytes; 2051 total -= num_bytes;
2329 bytenr += num_bytes; 2052 bytenr += num_bytes;
2330 } 2053 }
@@ -2341,7 +2064,7 @@ static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2341 return 0; 2064 return 0;
2342 2065
2343 bytenr = cache->key.objectid; 2066 bytenr = cache->key.objectid;
2344 put_block_group(cache); 2067 btrfs_put_block_group(cache);
2345 2068
2346 return bytenr; 2069 return bytenr;
2347} 2070}
@@ -2353,7 +2076,6 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2353 struct btrfs_block_group_cache *cache; 2076 struct btrfs_block_group_cache *cache;
2354 struct btrfs_fs_info *fs_info = root->fs_info; 2077 struct btrfs_fs_info *fs_info = root->fs_info;
2355 2078
2356 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2357 if (pin) { 2079 if (pin) {
2358 set_extent_dirty(&fs_info->pinned_extents, 2080 set_extent_dirty(&fs_info->pinned_extents,
2359 bytenr, bytenr + num - 1, GFP_NOFS); 2081 bytenr, bytenr + num - 1, GFP_NOFS);
@@ -2361,6 +2083,7 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2361 clear_extent_dirty(&fs_info->pinned_extents, 2083 clear_extent_dirty(&fs_info->pinned_extents,
2362 bytenr, bytenr + num - 1, GFP_NOFS); 2084 bytenr, bytenr + num - 1, GFP_NOFS);
2363 } 2085 }
2086
2364 while (num > 0) { 2087 while (num > 0) {
2365 cache = btrfs_lookup_block_group(fs_info, bytenr); 2088 cache = btrfs_lookup_block_group(fs_info, bytenr);
2366 BUG_ON(!cache); 2089 BUG_ON(!cache);
@@ -2385,7 +2108,7 @@ int btrfs_update_pinned_extents(struct btrfs_root *root,
2385 if (cache->cached) 2108 if (cache->cached)
2386 btrfs_add_free_space(cache, bytenr, len); 2109 btrfs_add_free_space(cache, bytenr, len);
2387 } 2110 }
2388 put_block_group(cache); 2111 btrfs_put_block_group(cache);
2389 bytenr += len; 2112 bytenr += len;
2390 num -= len; 2113 num -= len;
2391 } 2114 }
@@ -2416,7 +2139,7 @@ static int update_reserved_extents(struct btrfs_root *root,
2416 } 2139 }
2417 spin_unlock(&cache->lock); 2140 spin_unlock(&cache->lock);
2418 spin_unlock(&cache->space_info->lock); 2141 spin_unlock(&cache->space_info->lock);
2419 put_block_group(cache); 2142 btrfs_put_block_group(cache);
2420 bytenr += len; 2143 bytenr += len;
2421 num -= len; 2144 num -= len;
2422 } 2145 }
@@ -2431,7 +2154,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; 2154 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2432 int ret; 2155 int ret;
2433 2156
2434 mutex_lock(&root->fs_info->pinned_mutex);
2435 while (1) { 2157 while (1) {
2436 ret = find_first_extent_bit(pinned_extents, last, 2158 ret = find_first_extent_bit(pinned_extents, last,
2437 &start, &end, EXTENT_DIRTY); 2159 &start, &end, EXTENT_DIRTY);
@@ -2440,7 +2162,6 @@ int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2440 set_extent_dirty(copy, start, end, GFP_NOFS); 2162 set_extent_dirty(copy, start, end, GFP_NOFS);
2441 last = end + 1; 2163 last = end + 1;
2442 } 2164 }
2443 mutex_unlock(&root->fs_info->pinned_mutex);
2444 return 0; 2165 return 0;
2445} 2166}
2446 2167
@@ -2452,7 +2173,6 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2452 u64 end; 2173 u64 end;
2453 int ret; 2174 int ret;
2454 2175
2455 mutex_lock(&root->fs_info->pinned_mutex);
2456 while (1) { 2176 while (1) {
2457 ret = find_first_extent_bit(unpin, 0, &start, &end, 2177 ret = find_first_extent_bit(unpin, 0, &start, &end,
2458 EXTENT_DIRTY); 2178 EXTENT_DIRTY);
@@ -2461,209 +2181,20 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2461 2181
2462 ret = btrfs_discard_extent(root, start, end + 1 - start); 2182 ret = btrfs_discard_extent(root, start, end + 1 - start);
2463 2183
2184 /* unlocks the pinned mutex */
2464 btrfs_update_pinned_extents(root, start, end + 1 - start, 0); 2185 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2465 clear_extent_dirty(unpin, start, end, GFP_NOFS); 2186 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2466 2187
2467 if (need_resched()) { 2188 cond_resched();
2468 mutex_unlock(&root->fs_info->pinned_mutex);
2469 cond_resched();
2470 mutex_lock(&root->fs_info->pinned_mutex);
2471 }
2472 } 2189 }
2473 mutex_unlock(&root->fs_info->pinned_mutex);
2474 return ret; 2190 return ret;
2475} 2191}
2476 2192
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, 2193static int pin_down_bytes(struct btrfs_trans_handle *trans,
2665 struct btrfs_root *root, 2194 struct btrfs_root *root,
2666 u64 bytenr, u64 num_bytes, int is_data) 2195 struct btrfs_path *path,
2196 u64 bytenr, u64 num_bytes, int is_data,
2197 struct extent_buffer **must_clean)
2667{ 2198{
2668 int err = 0; 2199 int err = 0;
2669 struct extent_buffer *buf; 2200 struct extent_buffer *buf;
@@ -2686,17 +2217,18 @@ static int pin_down_bytes(struct btrfs_trans_handle *trans,
2686 u64 header_transid = btrfs_header_generation(buf); 2217 u64 header_transid = btrfs_header_generation(buf);
2687 if (header_owner != BTRFS_TREE_LOG_OBJECTID && 2218 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2688 header_owner != BTRFS_TREE_RELOC_OBJECTID && 2219 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2220 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2689 header_transid == trans->transid && 2221 header_transid == trans->transid &&
2690 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 2222 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2691 clean_tree_block(NULL, root, buf); 2223 *must_clean = buf;
2692 btrfs_tree_unlock(buf);
2693 free_extent_buffer(buf);
2694 return 1; 2224 return 1;
2695 } 2225 }
2696 btrfs_tree_unlock(buf); 2226 btrfs_tree_unlock(buf);
2697 } 2227 }
2698 free_extent_buffer(buf); 2228 free_extent_buffer(buf);
2699pinit: 2229pinit:
2230 btrfs_set_path_blocking(path);
2231 /* unlocks the pinned mutex */
2700 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); 2232 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2701 2233
2702 BUG_ON(err < 0); 2234 BUG_ON(err < 0);
@@ -2710,7 +2242,8 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2710 struct btrfs_root *root, 2242 struct btrfs_root *root,
2711 u64 bytenr, u64 num_bytes, u64 parent, 2243 u64 bytenr, u64 num_bytes, u64 parent,
2712 u64 root_objectid, u64 ref_generation, 2244 u64 root_objectid, u64 ref_generation,
2713 u64 owner_objectid, int pin, int mark_free) 2245 u64 owner_objectid, int pin, int mark_free,
2246 int refs_to_drop)
2714{ 2247{
2715 struct btrfs_path *path; 2248 struct btrfs_path *path;
2716 struct btrfs_key key; 2249 struct btrfs_key key;
@@ -2732,6 +2265,7 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2732 return -ENOMEM; 2265 return -ENOMEM;
2733 2266
2734 path->reada = 1; 2267 path->reada = 1;
2268 path->leave_spinning = 1;
2735 ret = lookup_extent_backref(trans, extent_root, path, 2269 ret = lookup_extent_backref(trans, extent_root, path,
2736 bytenr, parent, root_objectid, 2270 bytenr, parent, root_objectid,
2737 ref_generation, owner_objectid, 1); 2271 ref_generation, owner_objectid, 1);
@@ -2753,9 +2287,11 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2753 break; 2287 break;
2754 } 2288 }
2755 if (!found_extent) { 2289 if (!found_extent) {
2756 ret = remove_extent_backref(trans, extent_root, path); 2290 ret = remove_extent_backref(trans, extent_root, path,
2291 refs_to_drop);
2757 BUG_ON(ret); 2292 BUG_ON(ret);
2758 btrfs_release_path(extent_root, path); 2293 btrfs_release_path(extent_root, path);
2294 path->leave_spinning = 1;
2759 ret = btrfs_search_slot(trans, extent_root, 2295 ret = btrfs_search_slot(trans, extent_root,
2760 &key, path, -1, 1); 2296 &key, path, -1, 1);
2761 if (ret) { 2297 if (ret) {
@@ -2771,8 +2307,9 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2771 btrfs_print_leaf(extent_root, path->nodes[0]); 2307 btrfs_print_leaf(extent_root, path->nodes[0]);
2772 WARN_ON(1); 2308 WARN_ON(1);
2773 printk(KERN_ERR "btrfs unable to find ref byte nr %llu " 2309 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2774 "root %llu gen %llu owner %llu\n", 2310 "parent %llu root %llu gen %llu owner %llu\n",
2775 (unsigned long long)bytenr, 2311 (unsigned long long)bytenr,
2312 (unsigned long long)parent,
2776 (unsigned long long)root_objectid, 2313 (unsigned long long)root_objectid,
2777 (unsigned long long)ref_generation, 2314 (unsigned long long)ref_generation,
2778 (unsigned long long)owner_objectid); 2315 (unsigned long long)owner_objectid);
@@ -2782,17 +2319,23 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2782 ei = btrfs_item_ptr(leaf, extent_slot, 2319 ei = btrfs_item_ptr(leaf, extent_slot,
2783 struct btrfs_extent_item); 2320 struct btrfs_extent_item);
2784 refs = btrfs_extent_refs(leaf, ei); 2321 refs = btrfs_extent_refs(leaf, ei);
2785 BUG_ON(refs == 0);
2786 refs -= 1;
2787 btrfs_set_extent_refs(leaf, ei, refs);
2788 2322
2323 /*
2324 * we're not allowed to delete the extent item if there
2325 * are other delayed ref updates pending
2326 */
2327
2328 BUG_ON(refs < refs_to_drop);
2329 refs -= refs_to_drop;
2330 btrfs_set_extent_refs(leaf, ei, refs);
2789 btrfs_mark_buffer_dirty(leaf); 2331 btrfs_mark_buffer_dirty(leaf);
2790 2332
2791 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { 2333 if (refs == 0 && found_extent &&
2334 path->slots[0] == extent_slot + 1) {
2792 struct btrfs_extent_ref *ref; 2335 struct btrfs_extent_ref *ref;
2793 ref = btrfs_item_ptr(leaf, path->slots[0], 2336 ref = btrfs_item_ptr(leaf, path->slots[0],
2794 struct btrfs_extent_ref); 2337 struct btrfs_extent_ref);
2795 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); 2338 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
2796 /* if the back ref and the extent are next to each other 2339 /* if the back ref and the extent are next to each other
2797 * they get deleted below in one shot 2340 * they get deleted below in one shot
2798 */ 2341 */
@@ -2800,11 +2343,13 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2800 num_to_del = 2; 2343 num_to_del = 2;
2801 } else if (found_extent) { 2344 } else if (found_extent) {
2802 /* otherwise delete the extent back ref */ 2345 /* otherwise delete the extent back ref */
2803 ret = remove_extent_backref(trans, extent_root, path); 2346 ret = remove_extent_backref(trans, extent_root, path,
2347 refs_to_drop);
2804 BUG_ON(ret); 2348 BUG_ON(ret);
2805 /* if refs are 0, we need to setup the path for deletion */ 2349 /* if refs are 0, we need to setup the path for deletion */
2806 if (refs == 0) { 2350 if (refs == 0) {
2807 btrfs_release_path(extent_root, path); 2351 btrfs_release_path(extent_root, path);
2352 path->leave_spinning = 1;
2808 ret = btrfs_search_slot(trans, extent_root, &key, path, 2353 ret = btrfs_search_slot(trans, extent_root, &key, path,
2809 -1, 1); 2354 -1, 1);
2810 BUG_ON(ret); 2355 BUG_ON(ret);
@@ -2814,16 +2359,18 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2814 if (refs == 0) { 2359 if (refs == 0) {
2815 u64 super_used; 2360 u64 super_used;
2816 u64 root_used; 2361 u64 root_used;
2362 struct extent_buffer *must_clean = NULL;
2817 2363
2818 if (pin) { 2364 if (pin) {
2819 mutex_lock(&root->fs_info->pinned_mutex); 2365 ret = pin_down_bytes(trans, root, path,
2820 ret = pin_down_bytes(trans, root, bytenr, num_bytes, 2366 bytenr, num_bytes,
2821 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); 2367 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
2822 mutex_unlock(&root->fs_info->pinned_mutex); 2368 &must_clean);
2823 if (ret > 0) 2369 if (ret > 0)
2824 mark_free = 1; 2370 mark_free = 1;
2825 BUG_ON(ret < 0); 2371 BUG_ON(ret < 0);
2826 } 2372 }
2373
2827 /* block accounting for super block */ 2374 /* block accounting for super block */
2828 spin_lock(&info->delalloc_lock); 2375 spin_lock(&info->delalloc_lock);
2829 super_used = btrfs_super_bytes_used(&info->super_copy); 2376 super_used = btrfs_super_bytes_used(&info->super_copy);
@@ -2835,14 +2382,34 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2835 btrfs_set_root_used(&root->root_item, 2382 btrfs_set_root_used(&root->root_item,
2836 root_used - num_bytes); 2383 root_used - num_bytes);
2837 spin_unlock(&info->delalloc_lock); 2384 spin_unlock(&info->delalloc_lock);
2385
2386 /*
2387 * it is going to be very rare for someone to be waiting
2388 * on the block we're freeing. del_items might need to
2389 * schedule, so rather than get fancy, just force it
2390 * to blocking here
2391 */
2392 if (must_clean)
2393 btrfs_set_lock_blocking(must_clean);
2394
2838 ret = btrfs_del_items(trans, extent_root, path, path->slots[0], 2395 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2839 num_to_del); 2396 num_to_del);
2840 BUG_ON(ret); 2397 BUG_ON(ret);
2841 btrfs_release_path(extent_root, path); 2398 btrfs_release_path(extent_root, path);
2842 2399
2400 if (must_clean) {
2401 clean_tree_block(NULL, root, must_clean);
2402 btrfs_tree_unlock(must_clean);
2403 free_extent_buffer(must_clean);
2404 }
2405
2843 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { 2406 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2844 ret = btrfs_del_csums(trans, root, bytenr, num_bytes); 2407 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2845 BUG_ON(ret); 2408 BUG_ON(ret);
2409 } else {
2410 invalidate_mapping_pages(info->btree_inode->i_mapping,
2411 bytenr >> PAGE_CACHE_SHIFT,
2412 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
2846 } 2413 }
2847 2414
2848 ret = update_block_group(trans, root, bytenr, num_bytes, 0, 2415 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
@@ -2850,218 +2417,103 @@ static int __free_extent(struct btrfs_trans_handle *trans,
2850 BUG_ON(ret); 2417 BUG_ON(ret);
2851 } 2418 }
2852 btrfs_free_path(path); 2419 btrfs_free_path(path);
2853 finish_current_insert(trans, extent_root, 0);
2854 return ret; 2420 return ret;
2855} 2421}
2856 2422
2857/* 2423/*
2858 * find all the blocks marked as pending in the radix tree and remove 2424 * remove an extent from the root, returns 0 on success
2859 * them from the extent map
2860 */ 2425 */
2861static int del_pending_extents(struct btrfs_trans_handle *trans, 2426static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2862 struct btrfs_root *extent_root, int all) 2427 struct btrfs_root *root,
2428 u64 bytenr, u64 num_bytes, u64 parent,
2429 u64 root_objectid, u64 ref_generation,
2430 u64 owner_objectid, int pin,
2431 int refs_to_drop)
2863{ 2432{
2864 int ret; 2433 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 2434
2964 if (nr) { 2435 /*
2965 ret = free_extents(trans, extent_root, &delete_list); 2436 * if metadata always pin
2966 BUG_ON(ret); 2437 * if data pin when any transaction has committed this
2967 } 2438 */
2439 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2440 ref_generation != trans->transid)
2441 pin = 1;
2968 2442
2969 if (all && skipped) { 2443 if (ref_generation != trans->transid)
2970 INIT_LIST_HEAD(&delete_list); 2444 pin = 1;
2971 search = 0;
2972 nr = 0;
2973 goto again;
2974 }
2975 2445
2976 if (!err) 2446 return __free_extent(trans, root, bytenr, num_bytes, parent,
2977 finish_current_insert(trans, extent_root, 0); 2447 root_objectid, ref_generation,
2978 return err; 2448 owner_objectid, pin, pin == 0, refs_to_drop);
2979} 2449}
2980 2450
2981/* 2451/*
2982 * remove an extent from the root, returns 0 on success 2452 * when we free an extent, it is possible (and likely) that we free the last
2453 * delayed ref for that extent as well. This searches the delayed ref tree for
2454 * a given extent, and if there are no other delayed refs to be processed, it
2455 * removes it from the tree.
2983 */ 2456 */
2984static int __btrfs_free_extent(struct btrfs_trans_handle *trans, 2457static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2985 struct btrfs_root *root, 2458 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{ 2459{
2990 struct btrfs_root *extent_root = root->fs_info->extent_root; 2460 struct btrfs_delayed_ref_head *head;
2991 int pending_ret; 2461 struct btrfs_delayed_ref_root *delayed_refs;
2462 struct btrfs_delayed_ref_node *ref;
2463 struct rb_node *node;
2992 int ret; 2464 int ret;
2993 2465
2994 WARN_ON(num_bytes < root->sectorsize); 2466 delayed_refs = &trans->transaction->delayed_refs;
2995 if (root == extent_root) { 2467 spin_lock(&delayed_refs->lock);
2996 struct pending_extent_op *extent_op = NULL; 2468 head = btrfs_find_delayed_ref_head(trans, bytenr);
2997 2469 if (!head)
2998 mutex_lock(&root->fs_info->extent_ins_mutex); 2470 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 2471
3008 extent_op->del = 1; 2472 node = rb_prev(&head->node.rb_node);
3009 if (extent_op->type == PENDING_EXTENT_INSERT) { 2473 if (!node)
3010 mutex_unlock(&root->fs_info->extent_ins_mutex); 2474 goto out;
3011 return 0;
3012 }
3013 }
3014 2475
3015 if (extent_op) { 2476 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 2477
3020 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); 2478 /* there are still entries for this ref, we can't drop it */
3021 BUG_ON(!extent_op); 2479 if (ref->bytenr == bytenr)
3022 2480 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 2481
3054 /* if data pin when any transaction has committed this */ 2482 /*
3055 if (ref_generation != trans->transid) 2483 * waiting for the lock here would deadlock. If someone else has it
3056 pin = 1; 2484 * locked they are already in the process of dropping it anyway
2485 */
2486 if (!mutex_trylock(&head->mutex))
2487 goto out;
3057 2488
3058 ret = __free_extent(trans, root, bytenr, num_bytes, parent, 2489 /*
3059 root_objectid, ref_generation, 2490 * at this point we have a head with no other entries. Go
3060 owner_objectid, pin, pin == 0); 2491 * ahead and process it.
2492 */
2493 head->node.in_tree = 0;
2494 rb_erase(&head->node.rb_node, &delayed_refs->root);
2495
2496 delayed_refs->num_entries--;
2497
2498 /*
2499 * we don't take a ref on the node because we're removing it from the
2500 * tree, so we just steal the ref the tree was holding.
2501 */
2502 delayed_refs->num_heads--;
2503 if (list_empty(&head->cluster))
2504 delayed_refs->num_heads_ready--;
2505
2506 list_del_init(&head->cluster);
2507 spin_unlock(&delayed_refs->lock);
3061 2508
3062 finish_current_insert(trans, root->fs_info->extent_root, 0); 2509 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
3063 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); 2510 &head->node, head->must_insert_reserved);
3064 return ret ? ret : pending_ret; 2511 BUG_ON(ret);
2512 btrfs_put_delayed_ref(&head->node);
2513 return 0;
2514out:
2515 spin_unlock(&delayed_refs->lock);
2516 return 0;
3065} 2517}
3066 2518
3067int btrfs_free_extent(struct btrfs_trans_handle *trans, 2519int btrfs_free_extent(struct btrfs_trans_handle *trans,
@@ -3072,9 +2524,28 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
3072{ 2524{
3073 int ret; 2525 int ret;
3074 2526
3075 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, 2527 /*
3076 root_objectid, ref_generation, 2528 * tree log blocks never actually go into the extent allocation
3077 owner_objectid, pin); 2529 * tree, just update pinning info and exit early.
2530 *
2531 * data extents referenced by the tree log do need to have
2532 * their reference counts bumped.
2533 */
2534 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2535 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2536 /* unlocks the pinned mutex */
2537 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2538 update_reserved_extents(root, bytenr, num_bytes, 0);
2539 ret = 0;
2540 } else {
2541 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2542 root_objectid, ref_generation,
2543 owner_objectid,
2544 BTRFS_DROP_DELAYED_REF, 1);
2545 BUG_ON(ret);
2546 ret = check_ref_cleanup(trans, root, bytenr);
2547 BUG_ON(ret);
2548 }
3078 return ret; 2549 return ret;
3079} 2550}
3080 2551
@@ -3103,228 +2574,262 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
3103{ 2574{
3104 int ret = 0; 2575 int ret = 0;
3105 struct btrfs_root *root = orig_root->fs_info->extent_root; 2576 struct btrfs_root *root = orig_root->fs_info->extent_root;
3106 u64 total_needed = num_bytes; 2577 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; 2578 struct btrfs_block_group_cache *block_group = NULL;
3110 int chunk_alloc_done = 0;
3111 int empty_cluster = 2 * 1024 * 1024; 2579 int empty_cluster = 2 * 1024 * 1024;
3112 int allowed_chunk_alloc = 0; 2580 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; 2581 struct btrfs_space_info *space_info;
2582 int last_ptr_loop = 0;
2583 int loop = 0;
3117 2584
3118 WARN_ON(num_bytes < root->sectorsize); 2585 WARN_ON(num_bytes < root->sectorsize);
3119 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); 2586 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
3120 ins->objectid = 0; 2587 ins->objectid = 0;
3121 ins->offset = 0; 2588 ins->offset = 0;
3122 2589
2590 space_info = __find_space_info(root->fs_info, data);
2591
3123 if (orig_root->ref_cows || empty_size) 2592 if (orig_root->ref_cows || empty_size)
3124 allowed_chunk_alloc = 1; 2593 allowed_chunk_alloc = 1;
3125 2594
3126 if (data & BTRFS_BLOCK_GROUP_METADATA) { 2595 if (data & BTRFS_BLOCK_GROUP_METADATA) {
3127 last_ptr = &root->fs_info->last_alloc; 2596 last_ptr = &root->fs_info->meta_alloc_cluster;
3128 if (!btrfs_test_opt(root, SSD)) 2597 if (!btrfs_test_opt(root, SSD))
3129 empty_cluster = 64 * 1024; 2598 empty_cluster = 64 * 1024;
3130 } 2599 }
3131 2600
3132 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) 2601 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
3133 last_ptr = &root->fs_info->last_data_alloc; 2602 last_ptr = &root->fs_info->data_alloc_cluster;
2603 }
3134 2604
3135 if (last_ptr) { 2605 if (last_ptr) {
3136 if (*last_ptr) { 2606 spin_lock(&last_ptr->lock);
3137 hint_byte = *last_ptr; 2607 if (last_ptr->block_group)
3138 last_wanted = *last_ptr; 2608 hint_byte = last_ptr->window_start;
3139 } else 2609 spin_unlock(&last_ptr->lock);
3140 empty_size += empty_cluster;
3141 } else {
3142 empty_cluster = 0;
3143 } 2610 }
2611
3144 search_start = max(search_start, first_logical_byte(root, 0)); 2612 search_start = max(search_start, first_logical_byte(root, 0));
3145 search_start = max(search_start, hint_byte); 2613 search_start = max(search_start, hint_byte);
3146 2614
3147 if (last_wanted && search_start != last_wanted) { 2615 if (!last_ptr) {
3148 last_wanted = 0; 2616 empty_cluster = 0;
3149 empty_size += empty_cluster; 2617 loop = 1;
3150 } 2618 }
3151 2619
3152 total_needed += empty_size; 2620 if (search_start == hint_byte) {
3153 block_group = btrfs_lookup_block_group(root->fs_info, search_start); 2621 block_group = btrfs_lookup_block_group(root->fs_info,
3154 if (!block_group) 2622 search_start);
3155 block_group = btrfs_lookup_first_block_group(root->fs_info, 2623 if (block_group && block_group_bits(block_group, data)) {
3156 search_start); 2624 down_read(&space_info->groups_sem);
3157 space_info = __find_space_info(root->fs_info, data); 2625 if (list_empty(&block_group->list) ||
2626 block_group->ro) {
2627 /*
2628 * someone is removing this block group,
2629 * we can't jump into the have_block_group
2630 * target because our list pointers are not
2631 * valid
2632 */
2633 btrfs_put_block_group(block_group);
2634 up_read(&space_info->groups_sem);
2635 } else
2636 goto have_block_group;
2637 } else if (block_group) {
2638 btrfs_put_block_group(block_group);
2639 }
2640 }
3158 2641
2642search:
3159 down_read(&space_info->groups_sem); 2643 down_read(&space_info->groups_sem);
3160 while (1) { 2644 list_for_each_entry(block_group, &space_info->block_groups, list) {
3161 struct btrfs_free_space *free_space; 2645 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 2646
3170 if (!block_group) 2647 atomic_inc(&block_group->count);
3171 goto new_group_no_lock; 2648 search_start = block_group->key.objectid;
3172 2649
2650have_block_group:
3173 if (unlikely(!block_group->cached)) { 2651 if (unlikely(!block_group->cached)) {
3174 mutex_lock(&block_group->cache_mutex); 2652 mutex_lock(&block_group->cache_mutex);
3175 ret = cache_block_group(root, block_group); 2653 ret = cache_block_group(root, block_group);
3176 mutex_unlock(&block_group->cache_mutex); 2654 mutex_unlock(&block_group->cache_mutex);
3177 if (ret) 2655 if (ret) {
2656 btrfs_put_block_group(block_group);
3178 break; 2657 break;
2658 }
3179 } 2659 }
3180 2660
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)) 2661 if (unlikely(block_group->ro))
3186 goto new_group; 2662 goto loop;
3187 2663
3188 free_space = btrfs_find_free_space(block_group, search_start, 2664 if (last_ptr) {
3189 total_needed); 2665 /*
3190 if (free_space) { 2666 * the refill lock keeps out other
3191 u64 start = block_group->key.objectid; 2667 * people trying to start a new cluster
3192 u64 end = block_group->key.objectid + 2668 */
3193 block_group->key.offset; 2669 spin_lock(&last_ptr->refill_lock);
2670 if (last_ptr->block_group &&
2671 (last_ptr->block_group->ro ||
2672 !block_group_bits(last_ptr->block_group, data))) {
2673 offset = 0;
2674 goto refill_cluster;
2675 }
3194 2676
3195 search_start = stripe_align(root, free_space->offset); 2677 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
2678 num_bytes, search_start);
2679 if (offset) {
2680 /* we have a block, we're done */
2681 spin_unlock(&last_ptr->refill_lock);
2682 goto checks;
2683 }
3196 2684
3197 /* move on to the next group */ 2685 spin_lock(&last_ptr->lock);
3198 if (search_start + num_bytes >= search_end) 2686 /*
3199 goto new_group; 2687 * whoops, this cluster doesn't actually point to
2688 * this block group. Get a ref on the block
2689 * group is does point to and try again
2690 */
2691 if (!last_ptr_loop && last_ptr->block_group &&
2692 last_ptr->block_group != block_group) {
3200 2693
3201 /* move on to the next group */ 2694 btrfs_put_block_group(block_group);
3202 if (search_start + num_bytes > end) 2695 block_group = last_ptr->block_group;
3203 goto new_group; 2696 atomic_inc(&block_group->count);
2697 spin_unlock(&last_ptr->lock);
2698 spin_unlock(&last_ptr->refill_lock);
3204 2699
3205 if (last_wanted && search_start != last_wanted) { 2700 last_ptr_loop = 1;
3206 total_needed += empty_cluster; 2701 search_start = block_group->key.objectid;
3207 empty_size += empty_cluster;
3208 last_wanted = 0;
3209 /* 2702 /*
3210 * if search_start is still in this block group 2703 * we know this block group is properly
3211 * then we just re-search this block group 2704 * in the list because
2705 * btrfs_remove_block_group, drops the
2706 * cluster before it removes the block
2707 * group from the list
3212 */ 2708 */
3213 if (search_start >= start && 2709 goto have_block_group;
3214 search_start < end) {
3215 mutex_unlock(&block_group->alloc_mutex);
3216 continue;
3217 }
3218
3219 /* else we go to the next block group */
3220 goto new_group;
3221 } 2710 }
2711 spin_unlock(&last_ptr->lock);
2712refill_cluster:
2713 /*
2714 * this cluster didn't work out, free it and
2715 * start over
2716 */
2717 btrfs_return_cluster_to_free_space(NULL, last_ptr);
2718
2719 last_ptr_loop = 0;
3222 2720
3223 if (exclude_nr > 0 && 2721 /* allocate a cluster in this block group */
3224 (search_start + num_bytes > exclude_start && 2722 ret = btrfs_find_space_cluster(trans,
3225 search_start < exclude_start + exclude_nr)) { 2723 block_group, last_ptr,
3226 search_start = exclude_start + exclude_nr; 2724 offset, num_bytes,
2725 empty_cluster + empty_size);
2726 if (ret == 0) {
3227 /* 2727 /*
3228 * if search_start is still in this block group 2728 * now pull our allocation out of this
3229 * then we just re-search this block group 2729 * cluster
3230 */ 2730 */
3231 if (search_start >= start && 2731 offset = btrfs_alloc_from_cluster(block_group,
3232 search_start < end) { 2732 last_ptr, num_bytes,
3233 mutex_unlock(&block_group->alloc_mutex); 2733 search_start);
3234 last_wanted = 0; 2734 if (offset) {
3235 continue; 2735 /* we found one, proceed */
2736 spin_unlock(&last_ptr->refill_lock);
2737 goto checks;
3236 } 2738 }
3237
3238 /* else we go to the next block group */
3239 goto new_group;
3240 } 2739 }
2740 /*
2741 * at this point we either didn't find a cluster
2742 * or we weren't able to allocate a block from our
2743 * cluster. Free the cluster we've been trying
2744 * to use, and go to the next block group
2745 */
2746 if (loop < 2) {
2747 btrfs_return_cluster_to_free_space(NULL,
2748 last_ptr);
2749 spin_unlock(&last_ptr->refill_lock);
2750 goto loop;
2751 }
2752 spin_unlock(&last_ptr->refill_lock);
2753 }
3241 2754
3242 ins->objectid = search_start; 2755 offset = btrfs_find_space_for_alloc(block_group, search_start,
3243 ins->offset = num_bytes; 2756 num_bytes, empty_size);
2757 if (!offset)
2758 goto loop;
2759checks:
2760 search_start = stripe_align(root, offset);
3244 2761
3245 btrfs_remove_free_space_lock(block_group, search_start, 2762 /* move on to the next group */
3246 num_bytes); 2763 if (search_start + num_bytes >= search_end) {
3247 /* we are all good, lets return */ 2764 btrfs_add_free_space(block_group, offset, num_bytes);
3248 mutex_unlock(&block_group->alloc_mutex); 2765 goto loop;
3249 break;
3250 } 2766 }
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 2767
3261 /* 2768 /* move on to the next group */
3262 * Here's how this works. 2769 if (search_start + num_bytes >
3263 * loop == 0: we were searching a block group via a hint 2770 block_group->key.objectid + block_group->key.offset) {
3264 * and didn't find anything, so we start at 2771 btrfs_add_free_space(block_group, offset, num_bytes);
3265 * the head of the block groups and keep searching 2772 goto loop;
3266 * loop == 1: we're searching through all of the block groups 2773 }
3267 * if we hit the head again we have searched 2774
3268 * all of the block groups for this space and we 2775 if (exclude_nr > 0 &&
3269 * need to try and allocate, if we cant error out. 2776 (search_start + num_bytes > exclude_start &&
3270 * loop == 2: we allocated more space and are looping through 2777 search_start < exclude_start + exclude_nr)) {
3271 * all of the block groups again. 2778 search_start = exclude_start + exclude_nr;
3272 */ 2779
3273 if (loop == 0) { 2780 btrfs_add_free_space(block_group, offset, num_bytes);
3274 head = &space_info->block_groups; 2781 /*
3275 cur = head->next; 2782 * if search_start is still in this block group
3276 loop++; 2783 * 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 */ 2784 */
3289 total_needed -= empty_size; 2785 if (search_start >= block_group->key.objectid &&
3290 empty_size = 0; 2786 search_start < (block_group->key.objectid +
3291 keep_going = extra_loop; 2787 block_group->key.offset))
3292 loop++; 2788 goto have_block_group;
2789 goto loop;
2790 }
3293 2791
3294 if (allowed_chunk_alloc && !chunk_alloc_done) { 2792 ins->objectid = search_start;
3295 up_read(&space_info->groups_sem); 2793 ins->offset = num_bytes;
3296 ret = do_chunk_alloc(trans, root, num_bytes + 2794
3297 2 * 1024 * 1024, data, 1); 2795 if (offset < search_start)
3298 down_read(&space_info->groups_sem); 2796 btrfs_add_free_space(block_group, offset,
3299 if (ret < 0) 2797 search_start - offset);
3300 goto loop_check; 2798 BUG_ON(offset > search_start);
3301 head = &space_info->block_groups; 2799
3302 /* 2800 /* we are all good, lets return */
3303 * we've allocated a new chunk, keep 2801 break;
3304 * trying 2802loop:
3305 */ 2803 btrfs_put_block_group(block_group);
3306 keep_going = 1; 2804 }
3307 chunk_alloc_done = 1; 2805 up_read(&space_info->groups_sem);
3308 } else if (!allowed_chunk_alloc) { 2806
3309 space_info->force_alloc = 1; 2807 /* loop == 0, try to find a clustered alloc in every block group
3310 } 2808 * loop == 1, try again after forcing a chunk allocation
3311loop_check: 2809 * loop == 2, set empty_size and empty_cluster to 0 and try again
3312 if (keep_going) { 2810 */
3313 cur = head->next; 2811 if (!ins->objectid && loop < 3 &&
3314 extra_loop = 0; 2812 (empty_size || empty_cluster || allowed_chunk_alloc)) {
3315 } else { 2813 if (loop >= 2) {
3316 break; 2814 empty_size = 0;
3317 } 2815 empty_cluster = 0;
3318 } else if (cur == head) {
3319 break;
3320 } 2816 }
3321 2817
3322 block_group = list_entry(cur, struct btrfs_block_group_cache, 2818 if (allowed_chunk_alloc) {
3323 list); 2819 ret = do_chunk_alloc(trans, root, num_bytes +
3324 atomic_inc(&block_group->count); 2820 2 * 1024 * 1024, data, 1);
2821 allowed_chunk_alloc = 0;
2822 } else {
2823 space_info->force_alloc = 1;
2824 }
3325 2825
3326 search_start = block_group->key.objectid; 2826 if (loop < 3) {
3327 cur = cur->next; 2827 loop++;
2828 goto search;
2829 }
2830 ret = -ENOSPC;
2831 } else if (!ins->objectid) {
2832 ret = -ENOSPC;
3328 } 2833 }
3329 2834
3330 /* we found what we needed */ 2835 /* we found what we needed */
@@ -3332,21 +2837,10 @@ loop_check:
3332 if (!(data & BTRFS_BLOCK_GROUP_DATA)) 2837 if (!(data & BTRFS_BLOCK_GROUP_DATA))
3333 trans->block_group = block_group->key.objectid; 2838 trans->block_group = block_group->key.objectid;
3334 2839
3335 if (last_ptr) 2840 btrfs_put_block_group(block_group);
3336 *last_ptr = ins->objectid + ins->offset;
3337 ret = 0; 2841 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 } 2842 }
3346 if (block_group)
3347 put_block_group(block_group);
3348 2843
3349 up_read(&space_info->groups_sem);
3350 return ret; 2844 return ret;
3351} 2845}
3352 2846
@@ -3359,9 +2853,12 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3359 info->bytes_pinned - info->bytes_reserved), 2853 info->bytes_pinned - info->bytes_reserved),
3360 (info->full) ? "" : "not "); 2854 (info->full) ? "" : "not ");
3361 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," 2855 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
3362 " may_use=%llu, used=%llu\n", info->total_bytes, 2856 " may_use=%llu, used=%llu\n",
3363 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use, 2857 (unsigned long long)info->total_bytes,
3364 info->bytes_used); 2858 (unsigned long long)info->bytes_pinned,
2859 (unsigned long long)info->bytes_delalloc,
2860 (unsigned long long)info->bytes_may_use,
2861 (unsigned long long)info->bytes_used);
3365 2862
3366 down_read(&info->groups_sem); 2863 down_read(&info->groups_sem);
3367 list_for_each_entry(cache, &info->block_groups, list) { 2864 list_for_each_entry(cache, &info->block_groups, list) {
@@ -3451,7 +2948,7 @@ int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3451 ret = btrfs_discard_extent(root, start, len); 2948 ret = btrfs_discard_extent(root, start, len);
3452 2949
3453 btrfs_add_free_space(cache, start, len); 2950 btrfs_add_free_space(cache, start, len);
3454 put_block_group(cache); 2951 btrfs_put_block_group(cache);
3455 update_reserved_extents(root, start, len, 0); 2952 update_reserved_extents(root, start, len, 0);
3456 2953
3457 return ret; 2954 return ret;
@@ -3475,10 +2972,10 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3475static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, 2972static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3476 struct btrfs_root *root, u64 parent, 2973 struct btrfs_root *root, u64 parent,
3477 u64 root_objectid, u64 ref_generation, 2974 u64 root_objectid, u64 ref_generation,
3478 u64 owner, struct btrfs_key *ins) 2975 u64 owner, struct btrfs_key *ins,
2976 int ref_mod)
3479{ 2977{
3480 int ret; 2978 int ret;
3481 int pending_ret;
3482 u64 super_used; 2979 u64 super_used;
3483 u64 root_used; 2980 u64 root_used;
3484 u64 num_bytes = ins->offset; 2981 u64 num_bytes = ins->offset;
@@ -3503,33 +3000,6 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3503 btrfs_set_root_used(&root->root_item, root_used + num_bytes); 3000 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3504 spin_unlock(&info->delalloc_lock); 3001 spin_unlock(&info->delalloc_lock);
3505 3002
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)); 3003 memcpy(&keys[0], ins, sizeof(*ins));
3534 keys[1].objectid = ins->objectid; 3004 keys[1].objectid = ins->objectid;
3535 keys[1].type = BTRFS_EXTENT_REF_KEY; 3005 keys[1].type = BTRFS_EXTENT_REF_KEY;
@@ -3540,37 +3010,31 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3540 path = btrfs_alloc_path(); 3010 path = btrfs_alloc_path();
3541 BUG_ON(!path); 3011 BUG_ON(!path);
3542 3012
3013 path->leave_spinning = 1;
3543 ret = btrfs_insert_empty_items(trans, extent_root, path, keys, 3014 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3544 sizes, 2); 3015 sizes, 2);
3545 BUG_ON(ret); 3016 BUG_ON(ret);
3546 3017
3547 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 3018 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3548 struct btrfs_extent_item); 3019 struct btrfs_extent_item);
3549 btrfs_set_extent_refs(path->nodes[0], extent_item, 1); 3020 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
3550 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, 3021 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3551 struct btrfs_extent_ref); 3022 struct btrfs_extent_ref);
3552 3023
3553 btrfs_set_ref_root(path->nodes[0], ref, root_objectid); 3024 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3554 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); 3025 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3555 btrfs_set_ref_objectid(path->nodes[0], ref, owner); 3026 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3556 btrfs_set_ref_num_refs(path->nodes[0], ref, 1); 3027 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
3557 3028
3558 btrfs_mark_buffer_dirty(path->nodes[0]); 3029 btrfs_mark_buffer_dirty(path->nodes[0]);
3559 3030
3560 trans->alloc_exclude_start = 0; 3031 trans->alloc_exclude_start = 0;
3561 trans->alloc_exclude_nr = 0; 3032 trans->alloc_exclude_nr = 0;
3562 btrfs_free_path(path); 3033 btrfs_free_path(path);
3563 finish_current_insert(trans, extent_root, 0);
3564 pending_ret = del_pending_extents(trans, extent_root, 0);
3565 3034
3566 if (ret) 3035 if (ret)
3567 goto out; 3036 goto out;
3568 if (pending_ret) {
3569 ret = pending_ret;
3570 goto out;
3571 }
3572 3037
3573update_block:
3574 ret = update_block_group(trans, root, ins->objectid, 3038 ret = update_block_group(trans, root, ins->objectid,
3575 ins->offset, 1, 0); 3039 ins->offset, 1, 0);
3576 if (ret) { 3040 if (ret) {
@@ -3592,9 +3056,12 @@ int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3592 3056
3593 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) 3057 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3594 return 0; 3058 return 0;
3595 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, 3059
3596 ref_generation, owner, ins); 3060 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3597 update_reserved_extents(root, ins->objectid, ins->offset, 0); 3061 ins->offset, parent, root_objectid,
3062 ref_generation, owner,
3063 BTRFS_ADD_DELAYED_EXTENT, 0);
3064 BUG_ON(ret);
3598 return ret; 3065 return ret;
3599} 3066}
3600 3067
@@ -3619,9 +3086,9 @@ int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3619 ret = btrfs_remove_free_space(block_group, ins->objectid, 3086 ret = btrfs_remove_free_space(block_group, ins->objectid,
3620 ins->offset); 3087 ins->offset);
3621 BUG_ON(ret); 3088 BUG_ON(ret);
3622 put_block_group(block_group); 3089 btrfs_put_block_group(block_group);
3623 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, 3090 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3624 ref_generation, owner, ins); 3091 ref_generation, owner, ins, 1);
3625 return ret; 3092 return ret;
3626} 3093}
3627 3094
@@ -3640,20 +3107,18 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3640 u64 search_end, struct btrfs_key *ins, u64 data) 3107 u64 search_end, struct btrfs_key *ins, u64 data)
3641{ 3108{
3642 int ret; 3109 int ret;
3643
3644 ret = __btrfs_reserve_extent(trans, root, num_bytes, 3110 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3645 min_alloc_size, empty_size, hint_byte, 3111 min_alloc_size, empty_size, hint_byte,
3646 search_end, ins, data); 3112 search_end, ins, data);
3647 BUG_ON(ret); 3113 BUG_ON(ret);
3648 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { 3114 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3649 ret = __btrfs_alloc_reserved_extent(trans, root, parent, 3115 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3650 root_objectid, ref_generation, 3116 ins->offset, parent, root_objectid,
3651 owner_objectid, ins); 3117 ref_generation, owner_objectid,
3118 BTRFS_ADD_DELAYED_EXTENT, 0);
3652 BUG_ON(ret); 3119 BUG_ON(ret);
3653
3654 } else {
3655 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3656 } 3120 }
3121 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3657 return ret; 3122 return ret;
3658} 3123}
3659 3124
@@ -3789,7 +3254,7 @@ int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3789 3254
3790 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 3255 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3791 3256
3792 ret = __btrfs_free_extent(trans, root, disk_bytenr, 3257 ret = btrfs_free_extent(trans, root, disk_bytenr,
3793 btrfs_file_extent_disk_num_bytes(leaf, fi), 3258 btrfs_file_extent_disk_num_bytes(leaf, fi),
3794 leaf->start, leaf_owner, leaf_generation, 3259 leaf->start, leaf_owner, leaf_generation,
3795 key.objectid, 0); 3260 key.objectid, 0);
@@ -3829,7 +3294,7 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3829 */ 3294 */
3830 for (i = 0; i < ref->nritems; i++) { 3295 for (i = 0; i < ref->nritems; i++) {
3831 info = ref->extents + sorted[i].slot; 3296 info = ref->extents + sorted[i].slot;
3832 ret = __btrfs_free_extent(trans, root, info->bytenr, 3297 ret = btrfs_free_extent(trans, root, info->bytenr,
3833 info->num_bytes, ref->bytenr, 3298 info->num_bytes, ref->bytenr,
3834 ref->owner, ref->generation, 3299 ref->owner, ref->generation,
3835 info->objectid, 0); 3300 info->objectid, 0);
@@ -3846,12 +3311,13 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3846 return 0; 3311 return 0;
3847} 3312}
3848 3313
3849static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, 3314static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3315 struct btrfs_root *root, u64 start,
3850 u64 len, u32 *refs) 3316 u64 len, u32 *refs)
3851{ 3317{
3852 int ret; 3318 int ret;
3853 3319
3854 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); 3320 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
3855 BUG_ON(ret); 3321 BUG_ON(ret);
3856 3322
3857#if 0 /* some debugging code in case we see problems here */ 3323#if 0 /* some debugging code in case we see problems here */
@@ -3959,7 +3425,8 @@ static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3959 * we just decrement it below and don't update any 3425 * we just decrement it below and don't update any
3960 * of the refs the leaf points to. 3426 * of the refs the leaf points to.
3961 */ 3427 */
3962 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); 3428 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3429 blocksize, &refs);
3963 BUG_ON(ret); 3430 BUG_ON(ret);
3964 if (refs != 1) 3431 if (refs != 1)
3965 continue; 3432 continue;
@@ -4010,7 +3477,7 @@ static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
4010 */ 3477 */
4011 for (i = 0; i < refi; i++) { 3478 for (i = 0; i < refi; i++) {
4012 bytenr = sorted[i].bytenr; 3479 bytenr = sorted[i].bytenr;
4013 ret = __btrfs_free_extent(trans, root, bytenr, 3480 ret = btrfs_free_extent(trans, root, bytenr,
4014 blocksize, eb->start, 3481 blocksize, eb->start,
4015 root_owner, root_gen, 0, 1); 3482 root_owner, root_gen, 0, 1);
4016 BUG_ON(ret); 3483 BUG_ON(ret);
@@ -4053,7 +3520,7 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4053 3520
4054 WARN_ON(*level < 0); 3521 WARN_ON(*level < 0);
4055 WARN_ON(*level >= BTRFS_MAX_LEVEL); 3522 WARN_ON(*level >= BTRFS_MAX_LEVEL);
4056 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, 3523 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
4057 path->nodes[*level]->len, &refs); 3524 path->nodes[*level]->len, &refs);
4058 BUG_ON(ret); 3525 BUG_ON(ret);
4059 if (refs > 1) 3526 if (refs > 1)
@@ -4104,7 +3571,8 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4104 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); 3571 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4105 blocksize = btrfs_level_size(root, *level - 1); 3572 blocksize = btrfs_level_size(root, *level - 1);
4106 3573
4107 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); 3574 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3575 blocksize, &refs);
4108 BUG_ON(ret); 3576 BUG_ON(ret);
4109 3577
4110 /* 3578 /*
@@ -4119,7 +3587,7 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
4119 root_gen = btrfs_header_generation(parent); 3587 root_gen = btrfs_header_generation(parent);
4120 path->slots[*level]++; 3588 path->slots[*level]++;
4121 3589
4122 ret = __btrfs_free_extent(trans, root, bytenr, 3590 ret = btrfs_free_extent(trans, root, bytenr,
4123 blocksize, parent->start, 3591 blocksize, parent->start,
4124 root_owner, root_gen, 3592 root_owner, root_gen,
4125 *level - 1, 1); 3593 *level - 1, 1);
@@ -4165,7 +3633,7 @@ out:
4165 * cleanup and free the reference on the last node 3633 * cleanup and free the reference on the last node
4166 * we processed 3634 * we processed
4167 */ 3635 */
4168 ret = __btrfs_free_extent(trans, root, bytenr, blocksize, 3636 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
4169 parent->start, root_owner, root_gen, 3637 parent->start, root_owner, root_gen,
4170 *level, 1); 3638 *level, 1);
4171 free_extent_buffer(path->nodes[*level]); 3639 free_extent_buffer(path->nodes[*level]);
@@ -4354,6 +3822,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4354 struct btrfs_path *path; 3822 struct btrfs_path *path;
4355 int i; 3823 int i;
4356 int orig_level; 3824 int orig_level;
3825 int update_count;
4357 struct btrfs_root_item *root_item = &root->root_item; 3826 struct btrfs_root_item *root_item = &root->root_item;
4358 3827
4359 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); 3828 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
@@ -4395,6 +3864,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4395 } 3864 }
4396 } 3865 }
4397 while (1) { 3866 while (1) {
3867 unsigned long update;
4398 wret = walk_down_tree(trans, root, path, &level); 3868 wret = walk_down_tree(trans, root, path, &level);
4399 if (wret > 0) 3869 if (wret > 0)
4400 break; 3870 break;
@@ -4407,12 +3877,21 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4407 break; 3877 break;
4408 if (wret < 0) 3878 if (wret < 0)
4409 ret = wret; 3879 ret = wret;
4410 if (trans->transaction->in_commit) { 3880 if (trans->transaction->in_commit ||
3881 trans->transaction->delayed_refs.flushing) {
4411 ret = -EAGAIN; 3882 ret = -EAGAIN;
4412 break; 3883 break;
4413 } 3884 }
4414 atomic_inc(&root->fs_info->throttle_gen); 3885 atomic_inc(&root->fs_info->throttle_gen);
4415 wake_up(&root->fs_info->transaction_throttle); 3886 wake_up(&root->fs_info->transaction_throttle);
3887 for (update_count = 0; update_count < 16; update_count++) {
3888 update = trans->delayed_ref_updates;
3889 trans->delayed_ref_updates = 0;
3890 if (update)
3891 btrfs_run_delayed_refs(trans, root, update);
3892 else
3893 break;
3894 }
4416 } 3895 }
4417 for (i = 0; i <= orig_level; i++) { 3896 for (i = 0; i <= orig_level; i++) {
4418 if (path->nodes[i]) { 3897 if (path->nodes[i]) {
@@ -5457,6 +4936,7 @@ static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
5457 root->root_key.objectid, 4936 root->root_key.objectid,
5458 trans->transid, key.objectid); 4937 trans->transid, key.objectid);
5459 BUG_ON(ret); 4938 BUG_ON(ret);
4939
5460 ret = btrfs_free_extent(trans, root, 4940 ret = btrfs_free_extent(trans, root,
5461 bytenr, num_bytes, leaf->start, 4941 bytenr, num_bytes, leaf->start,
5462 btrfs_header_owner(leaf), 4942 btrfs_header_owner(leaf),
@@ -5768,9 +5248,6 @@ static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5768 ref_path, NULL, NULL); 5248 ref_path, NULL, NULL);
5769 BUG_ON(ret); 5249 BUG_ON(ret);
5770 5250
5771 if (root == root->fs_info->extent_root)
5772 btrfs_extent_post_op(trans, root);
5773
5774 return 0; 5251 return 0;
5775} 5252}
5776 5253
@@ -6038,6 +5515,7 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6038 if (!path) 5515 if (!path)
6039 return -ENOMEM; 5516 return -ENOMEM;
6040 5517
5518 path->leave_spinning = 1;
6041 ret = btrfs_insert_empty_inode(trans, root, path, objectid); 5519 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6042 if (ret) 5520 if (ret)
6043 goto out; 5521 goto out;
@@ -6208,6 +5686,9 @@ again:
6208 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); 5686 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
6209 mutex_unlock(&root->fs_info->cleaner_mutex); 5687 mutex_unlock(&root->fs_info->cleaner_mutex);
6210 5688
5689 trans = btrfs_start_transaction(info->tree_root, 1);
5690 btrfs_commit_transaction(trans, info->tree_root);
5691
6211 while (1) { 5692 while (1) {
6212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 5693 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6213 if (ret < 0) 5694 if (ret < 0)
@@ -6294,7 +5775,7 @@ next:
6294 WARN_ON(block_group->reserved > 0); 5775 WARN_ON(block_group->reserved > 0);
6295 WARN_ON(btrfs_block_group_used(&block_group->item) > 0); 5776 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6296 spin_unlock(&block_group->lock); 5777 spin_unlock(&block_group->lock);
6297 put_block_group(block_group); 5778 btrfs_put_block_group(block_group);
6298 ret = 0; 5779 ret = 0;
6299out: 5780out:
6300 btrfs_free_path(path); 5781 btrfs_free_path(path);
@@ -6421,9 +5902,10 @@ int btrfs_read_block_groups(struct btrfs_root *root)
6421 5902
6422 atomic_set(&cache->count, 1); 5903 atomic_set(&cache->count, 1);
6423 spin_lock_init(&cache->lock); 5904 spin_lock_init(&cache->lock);
6424 mutex_init(&cache->alloc_mutex); 5905 spin_lock_init(&cache->tree_lock);
6425 mutex_init(&cache->cache_mutex); 5906 mutex_init(&cache->cache_mutex);
6426 INIT_LIST_HEAD(&cache->list); 5907 INIT_LIST_HEAD(&cache->list);
5908 INIT_LIST_HEAD(&cache->cluster_list);
6427 read_extent_buffer(leaf, &cache->item, 5909 read_extent_buffer(leaf, &cache->item,
6428 btrfs_item_ptr_offset(leaf, path->slots[0]), 5910 btrfs_item_ptr_offset(leaf, path->slots[0]),
6429 sizeof(cache->item)); 5911 sizeof(cache->item));
@@ -6466,7 +5948,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6466 5948
6467 extent_root = root->fs_info->extent_root; 5949 extent_root = root->fs_info->extent_root;
6468 5950
6469 root->fs_info->last_trans_new_blockgroup = trans->transid; 5951 root->fs_info->last_trans_log_full_commit = trans->transid;
6470 5952
6471 cache = kzalloc(sizeof(*cache), GFP_NOFS); 5953 cache = kzalloc(sizeof(*cache), GFP_NOFS);
6472 if (!cache) 5954 if (!cache)
@@ -6477,9 +5959,10 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6477 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 5959 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
6478 atomic_set(&cache->count, 1); 5960 atomic_set(&cache->count, 1);
6479 spin_lock_init(&cache->lock); 5961 spin_lock_init(&cache->lock);
6480 mutex_init(&cache->alloc_mutex); 5962 spin_lock_init(&cache->tree_lock);
6481 mutex_init(&cache->cache_mutex); 5963 mutex_init(&cache->cache_mutex);
6482 INIT_LIST_HEAD(&cache->list); 5964 INIT_LIST_HEAD(&cache->list);
5965 INIT_LIST_HEAD(&cache->cluster_list);
6483 5966
6484 btrfs_set_block_group_used(&cache->item, bytes_used); 5967 btrfs_set_block_group_used(&cache->item, bytes_used);
6485 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); 5968 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
@@ -6500,9 +5983,6 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6500 sizeof(cache->item)); 5983 sizeof(cache->item));
6501 BUG_ON(ret); 5984 BUG_ON(ret);
6502 5985
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); 5986 set_avail_alloc_bits(extent_root->fs_info, type);
6507 5987
6508 return 0; 5988 return 0;
@@ -6513,6 +5993,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6513{ 5993{
6514 struct btrfs_path *path; 5994 struct btrfs_path *path;
6515 struct btrfs_block_group_cache *block_group; 5995 struct btrfs_block_group_cache *block_group;
5996 struct btrfs_free_cluster *cluster;
6516 struct btrfs_key key; 5997 struct btrfs_key key;
6517 int ret; 5998 int ret;
6518 5999
@@ -6524,6 +6005,21 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6524 6005
6525 memcpy(&key, &block_group->key, sizeof(key)); 6006 memcpy(&key, &block_group->key, sizeof(key));
6526 6007
6008 /* make sure this block group isn't part of an allocation cluster */
6009 cluster = &root->fs_info->data_alloc_cluster;
6010 spin_lock(&cluster->refill_lock);
6011 btrfs_return_cluster_to_free_space(block_group, cluster);
6012 spin_unlock(&cluster->refill_lock);
6013
6014 /*
6015 * make sure this block group isn't part of a metadata
6016 * allocation cluster
6017 */
6018 cluster = &root->fs_info->meta_alloc_cluster;
6019 spin_lock(&cluster->refill_lock);
6020 btrfs_return_cluster_to_free_space(block_group, cluster);
6021 spin_unlock(&cluster->refill_lock);
6022
6527 path = btrfs_alloc_path(); 6023 path = btrfs_alloc_path();
6528 BUG_ON(!path); 6024 BUG_ON(!path);
6529 6025
@@ -6533,7 +6029,11 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6533 spin_unlock(&root->fs_info->block_group_cache_lock); 6029 spin_unlock(&root->fs_info->block_group_cache_lock);
6534 btrfs_remove_free_space_cache(block_group); 6030 btrfs_remove_free_space_cache(block_group);
6535 down_write(&block_group->space_info->groups_sem); 6031 down_write(&block_group->space_info->groups_sem);
6536 list_del(&block_group->list); 6032 /*
6033 * we must use list_del_init so people can check to see if they
6034 * are still on the list after taking the semaphore
6035 */
6036 list_del_init(&block_group->list);
6537 up_write(&block_group->space_info->groups_sem); 6037 up_write(&block_group->space_info->groups_sem);
6538 6038
6539 spin_lock(&block_group->space_info->lock); 6039 spin_lock(&block_group->space_info->lock);
@@ -6542,8 +6042,8 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6542 spin_unlock(&block_group->space_info->lock); 6042 spin_unlock(&block_group->space_info->lock);
6543 block_group->space_info->full = 0; 6043 block_group->space_info->full = 0;
6544 6044
6545 put_block_group(block_group); 6045 btrfs_put_block_group(block_group);
6546 put_block_group(block_group); 6046 btrfs_put_block_group(block_group);
6547 6047
6548 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 6048 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6549 if (ret > 0) 6049 if (ret > 0)