diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 1674 |
1 files changed, 553 insertions, 1121 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index fefe83ad2059..f5e7cae63d80 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -49,17 +49,23 @@ struct pending_extent_op { | |||
49 | int del; | 49 | int del; |
50 | }; | 50 | }; |
51 | 51 | ||
52 | static int finish_current_insert(struct btrfs_trans_handle *trans, | 52 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
53 | struct btrfs_root *extent_root, int all); | 53 | struct btrfs_root *root, u64 parent, |
54 | static int del_pending_extents(struct btrfs_trans_handle *trans, | 54 | u64 root_objectid, u64 ref_generation, |
55 | struct btrfs_root *extent_root, int all); | 55 | u64 owner, struct btrfs_key *ins, |
56 | static int pin_down_bytes(struct btrfs_trans_handle *trans, | 56 | int ref_mod); |
57 | struct btrfs_root *root, | 57 | static int update_reserved_extents(struct btrfs_root *root, |
58 | u64 bytenr, u64 num_bytes, int is_data); | 58 | u64 bytenr, u64 num, int reserve); |
59 | static int update_block_group(struct btrfs_trans_handle *trans, | 59 | static int update_block_group(struct btrfs_trans_handle *trans, |
60 | struct btrfs_root *root, | 60 | struct btrfs_root *root, |
61 | u64 bytenr, u64 num_bytes, int alloc, | 61 | u64 bytenr, u64 num_bytes, int alloc, |
62 | int mark_free); | 62 | int mark_free); |
63 | static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans, | ||
64 | struct btrfs_root *root, | ||
65 | u64 bytenr, u64 num_bytes, u64 parent, | ||
66 | u64 root_objectid, u64 ref_generation, | ||
67 | u64 owner_objectid, int pin, | ||
68 | int ref_to_drop); | ||
63 | 69 | ||
64 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, | 70 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
65 | struct btrfs_root *extent_root, u64 alloc_bytes, | 71 | struct btrfs_root *extent_root, u64 alloc_bytes, |
@@ -554,262 +560,13 @@ out: | |||
554 | return ret; | 560 | return ret; |
555 | } | 561 | } |
556 | 562 | ||
557 | /* | ||
558 | * updates all the backrefs that are pending on update_list for the | ||
559 | * extent_root | ||
560 | */ | ||
561 | static 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 | |||
578 | search: | ||
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 | |||
588 | loop: | ||
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 | |||
642 | out: | ||
643 | return 0; | ||
644 | } | ||
645 | |||
646 | static 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 | |||
807 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, | 563 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, |
808 | struct btrfs_root *root, | 564 | struct btrfs_root *root, |
809 | struct btrfs_path *path, | 565 | struct btrfs_path *path, |
810 | u64 bytenr, u64 parent, | 566 | u64 bytenr, u64 parent, |
811 | u64 ref_root, u64 ref_generation, | 567 | u64 ref_root, u64 ref_generation, |
812 | u64 owner_objectid) | 568 | u64 owner_objectid, |
569 | int refs_to_add) | ||
813 | { | 570 | { |
814 | struct btrfs_key key; | 571 | struct btrfs_key key; |
815 | struct extent_buffer *leaf; | 572 | struct extent_buffer *leaf; |
@@ -829,9 +586,10 @@ static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, | |||
829 | btrfs_set_ref_root(leaf, ref, ref_root); | 586 | btrfs_set_ref_root(leaf, ref, ref_root); |
830 | btrfs_set_ref_generation(leaf, ref, ref_generation); | 587 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
831 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); | 588 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
832 | btrfs_set_ref_num_refs(leaf, ref, 1); | 589 | btrfs_set_ref_num_refs(leaf, ref, refs_to_add); |
833 | } else if (ret == -EEXIST) { | 590 | } else if (ret == -EEXIST) { |
834 | u64 existing_owner; | 591 | u64 existing_owner; |
592 | |||
835 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); | 593 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
836 | leaf = path->nodes[0]; | 594 | leaf = path->nodes[0]; |
837 | ref = btrfs_item_ptr(leaf, path->slots[0], | 595 | ref = btrfs_item_ptr(leaf, path->slots[0], |
@@ -845,7 +603,7 @@ static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, | |||
845 | 603 | ||
846 | num_refs = btrfs_ref_num_refs(leaf, ref); | 604 | num_refs = btrfs_ref_num_refs(leaf, ref); |
847 | BUG_ON(num_refs == 0); | 605 | BUG_ON(num_refs == 0); |
848 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); | 606 | btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add); |
849 | 607 | ||
850 | existing_owner = btrfs_ref_objectid(leaf, ref); | 608 | existing_owner = btrfs_ref_objectid(leaf, ref); |
851 | if (existing_owner != owner_objectid && | 609 | if (existing_owner != owner_objectid && |
@@ -857,6 +615,7 @@ static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, | |||
857 | } else { | 615 | } else { |
858 | goto out; | 616 | goto out; |
859 | } | 617 | } |
618 | btrfs_unlock_up_safe(path, 1); | ||
860 | btrfs_mark_buffer_dirty(path->nodes[0]); | 619 | btrfs_mark_buffer_dirty(path->nodes[0]); |
861 | out: | 620 | out: |
862 | btrfs_release_path(root, path); | 621 | btrfs_release_path(root, path); |
@@ -865,7 +624,8 @@ out: | |||
865 | 624 | ||
866 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, | 625 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, |
867 | struct btrfs_root *root, | 626 | struct btrfs_root *root, |
868 | struct btrfs_path *path) | 627 | struct btrfs_path *path, |
628 | int refs_to_drop) | ||
869 | { | 629 | { |
870 | struct extent_buffer *leaf; | 630 | struct extent_buffer *leaf; |
871 | struct btrfs_extent_ref *ref; | 631 | struct btrfs_extent_ref *ref; |
@@ -875,8 +635,8 @@ static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, | |||
875 | leaf = path->nodes[0]; | 635 | leaf = path->nodes[0]; |
876 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | 636 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
877 | num_refs = btrfs_ref_num_refs(leaf, ref); | 637 | num_refs = btrfs_ref_num_refs(leaf, ref); |
878 | BUG_ON(num_refs == 0); | 638 | BUG_ON(num_refs < refs_to_drop); |
879 | num_refs -= 1; | 639 | num_refs -= refs_to_drop; |
880 | if (num_refs == 0) { | 640 | if (num_refs == 0) { |
881 | ret = btrfs_del_item(trans, root, path); | 641 | ret = btrfs_del_item(trans, root, path); |
882 | } else { | 642 | } else { |
@@ -927,332 +687,28 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, | |||
927 | #endif | 687 | #endif |
928 | } | 688 | } |
929 | 689 | ||
930 | static 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 | |||
950 | search: | ||
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 | |||
1170 | out: | ||
1171 | btrfs_free_path(path); | ||
1172 | return ret; | ||
1173 | } | ||
1174 | |||
1175 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | 690 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
1176 | struct btrfs_root *root, u64 bytenr, | 691 | struct btrfs_root *root, u64 bytenr, |
692 | u64 num_bytes, | ||
1177 | u64 orig_parent, u64 parent, | 693 | u64 orig_parent, u64 parent, |
1178 | u64 orig_root, u64 ref_root, | 694 | u64 orig_root, u64 ref_root, |
1179 | u64 orig_generation, u64 ref_generation, | 695 | u64 orig_generation, u64 ref_generation, |
1180 | u64 owner_objectid) | 696 | u64 owner_objectid) |
1181 | { | 697 | { |
1182 | int ret; | 698 | int ret; |
1183 | struct btrfs_root *extent_root = root->fs_info->extent_root; | 699 | 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 | 700 | ||
1204 | extent_op->parent = parent; | 701 | ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes, |
1205 | extent_op->generation = ref_generation; | 702 | orig_parent, parent, orig_root, |
1206 | } else { | 703 | ref_root, orig_generation, |
1207 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | 704 | 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); | 705 | BUG_ON(ret); |
1246 | finish_current_insert(trans, extent_root, 0); | ||
1247 | del_pending_extents(trans, extent_root, 0); | ||
1248 | out: | ||
1249 | btrfs_free_path(path); | ||
1250 | return ret; | 706 | return ret; |
1251 | } | 707 | } |
1252 | 708 | ||
1253 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | 709 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
1254 | struct btrfs_root *root, u64 bytenr, | 710 | struct btrfs_root *root, u64 bytenr, |
1255 | u64 orig_parent, u64 parent, | 711 | u64 num_bytes, u64 orig_parent, u64 parent, |
1256 | u64 ref_root, u64 ref_generation, | 712 | u64 ref_root, u64 ref_generation, |
1257 | u64 owner_objectid) | 713 | u64 owner_objectid) |
1258 | { | 714 | { |
@@ -1260,20 +716,36 @@ int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | |||
1260 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && | 716 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
1261 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | 717 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
1262 | return 0; | 718 | return 0; |
1263 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, | 719 | |
1264 | parent, ref_root, ref_root, | 720 | ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes, |
1265 | ref_generation, ref_generation, | 721 | orig_parent, parent, ref_root, |
1266 | owner_objectid); | 722 | ref_root, ref_generation, |
723 | ref_generation, owner_objectid); | ||
1267 | return ret; | 724 | return ret; |
1268 | } | 725 | } |
1269 | |||
1270 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | 726 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
1271 | struct btrfs_root *root, u64 bytenr, | 727 | struct btrfs_root *root, u64 bytenr, |
728 | u64 num_bytes, | ||
1272 | u64 orig_parent, u64 parent, | 729 | u64 orig_parent, u64 parent, |
1273 | u64 orig_root, u64 ref_root, | 730 | u64 orig_root, u64 ref_root, |
1274 | u64 orig_generation, u64 ref_generation, | 731 | u64 orig_generation, u64 ref_generation, |
1275 | u64 owner_objectid) | 732 | u64 owner_objectid) |
1276 | { | 733 | { |
734 | int ret; | ||
735 | |||
736 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root, | ||
737 | ref_generation, owner_objectid, | ||
738 | BTRFS_ADD_DELAYED_REF, 0); | ||
739 | BUG_ON(ret); | ||
740 | return ret; | ||
741 | } | ||
742 | |||
743 | static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans, | ||
744 | struct btrfs_root *root, u64 bytenr, | ||
745 | u64 num_bytes, u64 parent, u64 ref_root, | ||
746 | u64 ref_generation, u64 owner_objectid, | ||
747 | int refs_to_add) | ||
748 | { | ||
1277 | struct btrfs_path *path; | 749 | struct btrfs_path *path; |
1278 | int ret; | 750 | int ret; |
1279 | struct btrfs_key key; | 751 | struct btrfs_key key; |
@@ -1286,17 +758,24 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |||
1286 | return -ENOMEM; | 758 | return -ENOMEM; |
1287 | 759 | ||
1288 | path->reada = 1; | 760 | path->reada = 1; |
761 | path->leave_spinning = 1; | ||
1289 | key.objectid = bytenr; | 762 | key.objectid = bytenr; |
1290 | key.type = BTRFS_EXTENT_ITEM_KEY; | 763 | key.type = BTRFS_EXTENT_ITEM_KEY; |
1291 | key.offset = (u64)-1; | 764 | key.offset = num_bytes; |
1292 | 765 | ||
1293 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, | 766 | /* first find the extent item and update its reference count */ |
1294 | 0, 1); | 767 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
1295 | if (ret < 0) | 768 | path, 0, 1); |
769 | if (ret < 0) { | ||
770 | btrfs_set_path_blocking(path); | ||
1296 | return ret; | 771 | return ret; |
1297 | BUG_ON(ret == 0 || path->slots[0] == 0); | 772 | } |
1298 | 773 | ||
1299 | path->slots[0]--; | 774 | if (ret > 0) { |
775 | WARN_ON(1); | ||
776 | btrfs_free_path(path); | ||
777 | return -EIO; | ||
778 | } | ||
1300 | l = path->nodes[0]; | 779 | l = path->nodes[0]; |
1301 | 780 | ||
1302 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | 781 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
@@ -1310,21 +789,24 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |||
1310 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); | 789 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
1311 | 790 | ||
1312 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); | 791 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
792 | |||
1313 | refs = btrfs_extent_refs(l, item); | 793 | refs = btrfs_extent_refs(l, item); |
1314 | btrfs_set_extent_refs(l, item, refs + 1); | 794 | btrfs_set_extent_refs(l, item, refs + refs_to_add); |
795 | btrfs_unlock_up_safe(path, 1); | ||
796 | |||
1315 | btrfs_mark_buffer_dirty(path->nodes[0]); | 797 | btrfs_mark_buffer_dirty(path->nodes[0]); |
1316 | 798 | ||
1317 | btrfs_release_path(root->fs_info->extent_root, path); | 799 | btrfs_release_path(root->fs_info->extent_root, path); |
1318 | 800 | ||
1319 | path->reada = 1; | 801 | path->reada = 1; |
802 | path->leave_spinning = 1; | ||
803 | |||
804 | /* now insert the actual backref */ | ||
1320 | ret = insert_extent_backref(trans, root->fs_info->extent_root, | 805 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
1321 | path, bytenr, parent, | 806 | path, bytenr, parent, |
1322 | ref_root, ref_generation, | 807 | ref_root, ref_generation, |
1323 | owner_objectid); | 808 | owner_objectid, refs_to_add); |
1324 | BUG_ON(ret); | 809 | 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); | 810 | btrfs_free_path(path); |
1329 | return 0; | 811 | return 0; |
1330 | } | 812 | } |
@@ -1339,68 +821,278 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |||
1339 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && | 821 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
1340 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | 822 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
1341 | return 0; | 823 | return 0; |
1342 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, | 824 | |
825 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent, | ||
1343 | 0, ref_root, 0, ref_generation, | 826 | 0, ref_root, 0, ref_generation, |
1344 | owner_objectid); | 827 | owner_objectid); |
1345 | return ret; | 828 | return ret; |
1346 | } | 829 | } |
1347 | 830 | ||
1348 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, | 831 | static int drop_delayed_ref(struct btrfs_trans_handle *trans, |
1349 | struct btrfs_root *root) | 832 | struct btrfs_root *root, |
833 | struct btrfs_delayed_ref_node *node) | ||
834 | { | ||
835 | int ret = 0; | ||
836 | struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node); | ||
837 | |||
838 | BUG_ON(node->ref_mod == 0); | ||
839 | ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes, | ||
840 | node->parent, ref->root, ref->generation, | ||
841 | ref->owner_objectid, ref->pin, node->ref_mod); | ||
842 | |||
843 | return ret; | ||
844 | } | ||
845 | |||
846 | /* helper function to actually process a single delayed ref entry */ | ||
847 | static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans, | ||
848 | struct btrfs_root *root, | ||
849 | struct btrfs_delayed_ref_node *node, | ||
850 | int insert_reserved) | ||
1350 | { | 851 | { |
1351 | u64 start; | ||
1352 | u64 end; | ||
1353 | int ret; | 852 | int ret; |
853 | struct btrfs_delayed_ref *ref; | ||
854 | |||
855 | if (node->parent == (u64)-1) { | ||
856 | struct btrfs_delayed_ref_head *head; | ||
857 | /* | ||
858 | * we've hit the end of the chain and we were supposed | ||
859 | * to insert this extent into the tree. But, it got | ||
860 | * deleted before we ever needed to insert it, so all | ||
861 | * we have to do is clean up the accounting | ||
862 | */ | ||
863 | if (insert_reserved) { | ||
864 | update_reserved_extents(root, node->bytenr, | ||
865 | node->num_bytes, 0); | ||
866 | } | ||
867 | head = btrfs_delayed_node_to_head(node); | ||
868 | mutex_unlock(&head->mutex); | ||
869 | return 0; | ||
870 | } | ||
1354 | 871 | ||
1355 | while(1) { | 872 | ref = btrfs_delayed_node_to_ref(node); |
1356 | finish_current_insert(trans, root->fs_info->extent_root, 1); | 873 | if (ref->action == BTRFS_ADD_DELAYED_REF) { |
1357 | del_pending_extents(trans, root->fs_info->extent_root, 1); | 874 | if (insert_reserved) { |
875 | struct btrfs_key ins; | ||
1358 | 876 | ||
1359 | /* is there more work to do? */ | 877 | ins.objectid = node->bytenr; |
1360 | ret = find_first_extent_bit(&root->fs_info->pending_del, | 878 | ins.offset = node->num_bytes; |
1361 | 0, &start, &end, EXTENT_WRITEBACK); | 879 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
1362 | if (!ret) | 880 | |
1363 | continue; | 881 | /* record the full extent allocation */ |
1364 | ret = find_first_extent_bit(&root->fs_info->extent_ins, | 882 | ret = __btrfs_alloc_reserved_extent(trans, root, |
1365 | 0, &start, &end, EXTENT_WRITEBACK); | 883 | node->parent, ref->root, |
1366 | if (!ret) | 884 | ref->generation, ref->owner_objectid, |
1367 | continue; | 885 | &ins, node->ref_mod); |
1368 | break; | 886 | update_reserved_extents(root, node->bytenr, |
887 | node->num_bytes, 0); | ||
888 | } else { | ||
889 | /* just add one backref */ | ||
890 | ret = add_extent_ref(trans, root, node->bytenr, | ||
891 | node->num_bytes, | ||
892 | node->parent, ref->root, ref->generation, | ||
893 | ref->owner_objectid, node->ref_mod); | ||
894 | } | ||
895 | BUG_ON(ret); | ||
896 | } else if (ref->action == BTRFS_DROP_DELAYED_REF) { | ||
897 | WARN_ON(insert_reserved); | ||
898 | ret = drop_delayed_ref(trans, root, node); | ||
1369 | } | 899 | } |
1370 | return 0; | 900 | return 0; |
1371 | } | 901 | } |
1372 | 902 | ||
1373 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, | 903 | static noinline struct btrfs_delayed_ref_node * |
1374 | struct btrfs_root *root, u64 bytenr, | 904 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
1375 | u64 num_bytes, u32 *refs) | ||
1376 | { | 905 | { |
1377 | struct btrfs_path *path; | 906 | struct rb_node *node; |
907 | struct btrfs_delayed_ref_node *ref; | ||
908 | int action = BTRFS_ADD_DELAYED_REF; | ||
909 | again: | ||
910 | /* | ||
911 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. | ||
912 | * this prevents ref count from going down to zero when | ||
913 | * there still are pending delayed ref. | ||
914 | */ | ||
915 | node = rb_prev(&head->node.rb_node); | ||
916 | while (1) { | ||
917 | if (!node) | ||
918 | break; | ||
919 | ref = rb_entry(node, struct btrfs_delayed_ref_node, | ||
920 | rb_node); | ||
921 | if (ref->bytenr != head->node.bytenr) | ||
922 | break; | ||
923 | if (btrfs_delayed_node_to_ref(ref)->action == action) | ||
924 | return ref; | ||
925 | node = rb_prev(node); | ||
926 | } | ||
927 | if (action == BTRFS_ADD_DELAYED_REF) { | ||
928 | action = BTRFS_DROP_DELAYED_REF; | ||
929 | goto again; | ||
930 | } | ||
931 | return NULL; | ||
932 | } | ||
933 | |||
934 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, | ||
935 | struct btrfs_root *root, | ||
936 | struct list_head *cluster) | ||
937 | { | ||
938 | struct btrfs_delayed_ref_root *delayed_refs; | ||
939 | struct btrfs_delayed_ref_node *ref; | ||
940 | struct btrfs_delayed_ref_head *locked_ref = NULL; | ||
1378 | int ret; | 941 | int ret; |
1379 | struct btrfs_key key; | 942 | int count = 0; |
1380 | struct extent_buffer *l; | 943 | int must_insert_reserved = 0; |
1381 | struct btrfs_extent_item *item; | ||
1382 | 944 | ||
1383 | WARN_ON(num_bytes < root->sectorsize); | 945 | delayed_refs = &trans->transaction->delayed_refs; |
1384 | path = btrfs_alloc_path(); | 946 | while (1) { |
1385 | path->reada = 1; | 947 | if (!locked_ref) { |
1386 | key.objectid = bytenr; | 948 | /* pick a new head ref from the cluster list */ |
1387 | key.offset = num_bytes; | 949 | if (list_empty(cluster)) |
1388 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | 950 | break; |
1389 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, | 951 | |
1390 | 0, 0); | 952 | locked_ref = list_entry(cluster->next, |
1391 | if (ret < 0) | 953 | struct btrfs_delayed_ref_head, cluster); |
1392 | goto out; | 954 | |
1393 | if (ret != 0) { | 955 | /* grab the lock that says we are going to process |
1394 | btrfs_print_leaf(root, path->nodes[0]); | 956 | * all the refs for this head */ |
1395 | printk(KERN_INFO "btrfs failed to find block number %llu\n", | 957 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
1396 | (unsigned long long)bytenr); | 958 | |
1397 | BUG(); | 959 | /* |
960 | * we may have dropped the spin lock to get the head | ||
961 | * mutex lock, and that might have given someone else | ||
962 | * time to free the head. If that's true, it has been | ||
963 | * removed from our list and we can move on. | ||
964 | */ | ||
965 | if (ret == -EAGAIN) { | ||
966 | locked_ref = NULL; | ||
967 | count++; | ||
968 | continue; | ||
969 | } | ||
970 | } | ||
971 | |||
972 | /* | ||
973 | * record the must insert reserved flag before we | ||
974 | * drop the spin lock. | ||
975 | */ | ||
976 | must_insert_reserved = locked_ref->must_insert_reserved; | ||
977 | locked_ref->must_insert_reserved = 0; | ||
978 | |||
979 | /* | ||
980 | * locked_ref is the head node, so we have to go one | ||
981 | * node back for any delayed ref updates | ||
982 | */ | ||
983 | ref = select_delayed_ref(locked_ref); | ||
984 | if (!ref) { | ||
985 | /* All delayed refs have been processed, Go ahead | ||
986 | * and send the head node to run_one_delayed_ref, | ||
987 | * so that any accounting fixes can happen | ||
988 | */ | ||
989 | ref = &locked_ref->node; | ||
990 | list_del_init(&locked_ref->cluster); | ||
991 | locked_ref = NULL; | ||
992 | } | ||
993 | |||
994 | ref->in_tree = 0; | ||
995 | rb_erase(&ref->rb_node, &delayed_refs->root); | ||
996 | delayed_refs->num_entries--; | ||
997 | spin_unlock(&delayed_refs->lock); | ||
998 | |||
999 | ret = run_one_delayed_ref(trans, root, ref, | ||
1000 | must_insert_reserved); | ||
1001 | BUG_ON(ret); | ||
1002 | btrfs_put_delayed_ref(ref); | ||
1003 | |||
1004 | count++; | ||
1005 | cond_resched(); | ||
1006 | spin_lock(&delayed_refs->lock); | ||
1007 | } | ||
1008 | return count; | ||
1009 | } | ||
1010 | |||
1011 | /* | ||
1012 | * this starts processing the delayed reference count updates and | ||
1013 | * extent insertions we have queued up so far. count can be | ||
1014 | * 0, which means to process everything in the tree at the start | ||
1015 | * of the run (but not newly added entries), or it can be some target | ||
1016 | * number you'd like to process. | ||
1017 | */ | ||
1018 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, | ||
1019 | struct btrfs_root *root, unsigned long count) | ||
1020 | { | ||
1021 | struct rb_node *node; | ||
1022 | struct btrfs_delayed_ref_root *delayed_refs; | ||
1023 | struct btrfs_delayed_ref_node *ref; | ||
1024 | struct list_head cluster; | ||
1025 | int ret; | ||
1026 | int run_all = count == (unsigned long)-1; | ||
1027 | int run_most = 0; | ||
1028 | |||
1029 | if (root == root->fs_info->extent_root) | ||
1030 | root = root->fs_info->tree_root; | ||
1031 | |||
1032 | delayed_refs = &trans->transaction->delayed_refs; | ||
1033 | INIT_LIST_HEAD(&cluster); | ||
1034 | again: | ||
1035 | spin_lock(&delayed_refs->lock); | ||
1036 | if (count == 0) { | ||
1037 | count = delayed_refs->num_entries * 2; | ||
1038 | run_most = 1; | ||
1039 | } | ||
1040 | while (1) { | ||
1041 | if (!(run_all || run_most) && | ||
1042 | delayed_refs->num_heads_ready < 64) | ||
1043 | break; | ||
1044 | |||
1045 | /* | ||
1046 | * go find something we can process in the rbtree. We start at | ||
1047 | * the beginning of the tree, and then build a cluster | ||
1048 | * of refs to process starting at the first one we are able to | ||
1049 | * lock | ||
1050 | */ | ||
1051 | ret = btrfs_find_ref_cluster(trans, &cluster, | ||
1052 | delayed_refs->run_delayed_start); | ||
1053 | if (ret) | ||
1054 | break; | ||
1055 | |||
1056 | ret = run_clustered_refs(trans, root, &cluster); | ||
1057 | BUG_ON(ret < 0); | ||
1058 | |||
1059 | count -= min_t(unsigned long, ret, count); | ||
1060 | |||
1061 | if (count == 0) | ||
1062 | break; | ||
1063 | } | ||
1064 | |||
1065 | if (run_all) { | ||
1066 | node = rb_first(&delayed_refs->root); | ||
1067 | if (!node) | ||
1068 | goto out; | ||
1069 | count = (unsigned long)-1; | ||
1070 | |||
1071 | while (node) { | ||
1072 | ref = rb_entry(node, struct btrfs_delayed_ref_node, | ||
1073 | rb_node); | ||
1074 | if (btrfs_delayed_ref_is_head(ref)) { | ||
1075 | struct btrfs_delayed_ref_head *head; | ||
1076 | |||
1077 | head = btrfs_delayed_node_to_head(ref); | ||
1078 | atomic_inc(&ref->refs); | ||
1079 | |||
1080 | spin_unlock(&delayed_refs->lock); | ||
1081 | mutex_lock(&head->mutex); | ||
1082 | mutex_unlock(&head->mutex); | ||
1083 | |||
1084 | btrfs_put_delayed_ref(ref); | ||
1085 | cond_resched(); | ||
1086 | goto again; | ||
1087 | } | ||
1088 | node = rb_next(node); | ||
1089 | } | ||
1090 | spin_unlock(&delayed_refs->lock); | ||
1091 | schedule_timeout(1); | ||
1092 | goto again; | ||
1398 | } | 1093 | } |
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); | ||
1402 | out: | 1094 | out: |
1403 | btrfs_free_path(path); | 1095 | spin_unlock(&delayed_refs->lock); |
1404 | return 0; | 1096 | return 0; |
1405 | } | 1097 | } |
1406 | 1098 | ||
@@ -1624,7 +1316,7 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, | |||
1624 | int refi = 0; | 1316 | int refi = 0; |
1625 | int slot; | 1317 | int slot; |
1626 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, | 1318 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
1627 | u64, u64, u64, u64, u64, u64, u64, u64); | 1319 | u64, u64, u64, u64, u64, u64, u64, u64, u64); |
1628 | 1320 | ||
1629 | ref_root = btrfs_header_owner(buf); | 1321 | ref_root = btrfs_header_owner(buf); |
1630 | ref_generation = btrfs_header_generation(buf); | 1322 | ref_generation = btrfs_header_generation(buf); |
@@ -1696,12 +1388,19 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, | |||
1696 | 1388 | ||
1697 | if (level == 0) { | 1389 | if (level == 0) { |
1698 | btrfs_item_key_to_cpu(buf, &key, slot); | 1390 | btrfs_item_key_to_cpu(buf, &key, slot); |
1391 | fi = btrfs_item_ptr(buf, slot, | ||
1392 | struct btrfs_file_extent_item); | ||
1393 | |||
1394 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | ||
1395 | if (bytenr == 0) | ||
1396 | continue; | ||
1699 | 1397 | ||
1700 | ret = process_func(trans, root, bytenr, | 1398 | ret = process_func(trans, root, bytenr, |
1701 | orig_buf->start, buf->start, | 1399 | btrfs_file_extent_disk_num_bytes(buf, fi), |
1702 | orig_root, ref_root, | 1400 | orig_buf->start, buf->start, |
1703 | orig_generation, ref_generation, | 1401 | orig_root, ref_root, |
1704 | key.objectid); | 1402 | orig_generation, ref_generation, |
1403 | key.objectid); | ||
1705 | 1404 | ||
1706 | if (ret) { | 1405 | if (ret) { |
1707 | faili = slot; | 1406 | faili = slot; |
@@ -1709,7 +1408,7 @@ noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, | |||
1709 | goto fail; | 1408 | goto fail; |
1710 | } | 1409 | } |
1711 | } else { | 1410 | } else { |
1712 | ret = process_func(trans, root, bytenr, | 1411 | ret = process_func(trans, root, bytenr, buf->len, |
1713 | orig_buf->start, buf->start, | 1412 | orig_buf->start, buf->start, |
1714 | orig_root, ref_root, | 1413 | orig_root, ref_root, |
1715 | orig_generation, ref_generation, | 1414 | orig_generation, ref_generation, |
@@ -1786,17 +1485,17 @@ int btrfs_update_ref(struct btrfs_trans_handle *trans, | |||
1786 | if (bytenr == 0) | 1485 | if (bytenr == 0) |
1787 | continue; | 1486 | continue; |
1788 | ret = __btrfs_update_extent_ref(trans, root, bytenr, | 1487 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
1789 | orig_buf->start, buf->start, | 1488 | btrfs_file_extent_disk_num_bytes(buf, fi), |
1790 | orig_root, ref_root, | 1489 | orig_buf->start, buf->start, |
1791 | orig_generation, ref_generation, | 1490 | orig_root, ref_root, orig_generation, |
1792 | key.objectid); | 1491 | ref_generation, key.objectid); |
1793 | if (ret) | 1492 | if (ret) |
1794 | goto fail; | 1493 | goto fail; |
1795 | } else { | 1494 | } else { |
1796 | bytenr = btrfs_node_blockptr(buf, slot); | 1495 | bytenr = btrfs_node_blockptr(buf, slot); |
1797 | ret = __btrfs_update_extent_ref(trans, root, bytenr, | 1496 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
1798 | orig_buf->start, buf->start, | 1497 | buf->len, orig_buf->start, |
1799 | orig_root, ref_root, | 1498 | buf->start, orig_root, ref_root, |
1800 | orig_generation, ref_generation, | 1499 | orig_generation, ref_generation, |
1801 | level - 1); | 1500 | level - 1); |
1802 | if (ret) | 1501 | if (ret) |
@@ -1815,7 +1514,6 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans, | |||
1815 | struct btrfs_block_group_cache *cache) | 1514 | struct btrfs_block_group_cache *cache) |
1816 | { | 1515 | { |
1817 | int ret; | 1516 | int ret; |
1818 | int pending_ret; | ||
1819 | struct btrfs_root *extent_root = root->fs_info->extent_root; | 1517 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
1820 | unsigned long bi; | 1518 | unsigned long bi; |
1821 | struct extent_buffer *leaf; | 1519 | struct extent_buffer *leaf; |
@@ -1831,12 +1529,8 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans, | |||
1831 | btrfs_mark_buffer_dirty(leaf); | 1529 | btrfs_mark_buffer_dirty(leaf); |
1832 | btrfs_release_path(extent_root, path); | 1530 | btrfs_release_path(extent_root, path); |
1833 | fail: | 1531 | fail: |
1834 | finish_current_insert(trans, extent_root, 0); | ||
1835 | pending_ret = del_pending_extents(trans, extent_root, 0); | ||
1836 | if (ret) | 1532 | if (ret) |
1837 | return ret; | 1533 | return ret; |
1838 | if (pending_ret) | ||
1839 | return pending_ret; | ||
1840 | return 0; | 1534 | return 0; |
1841 | 1535 | ||
1842 | } | 1536 | } |
@@ -2361,6 +2055,8 @@ int btrfs_update_pinned_extents(struct btrfs_root *root, | |||
2361 | clear_extent_dirty(&fs_info->pinned_extents, | 2055 | clear_extent_dirty(&fs_info->pinned_extents, |
2362 | bytenr, bytenr + num - 1, GFP_NOFS); | 2056 | bytenr, bytenr + num - 1, GFP_NOFS); |
2363 | } | 2057 | } |
2058 | mutex_unlock(&root->fs_info->pinned_mutex); | ||
2059 | |||
2364 | while (num > 0) { | 2060 | while (num > 0) { |
2365 | cache = btrfs_lookup_block_group(fs_info, bytenr); | 2061 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
2366 | BUG_ON(!cache); | 2062 | BUG_ON(!cache); |
@@ -2452,8 +2148,8 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |||
2452 | u64 end; | 2148 | u64 end; |
2453 | int ret; | 2149 | int ret; |
2454 | 2150 | ||
2455 | mutex_lock(&root->fs_info->pinned_mutex); | ||
2456 | while (1) { | 2151 | while (1) { |
2152 | mutex_lock(&root->fs_info->pinned_mutex); | ||
2457 | ret = find_first_extent_bit(unpin, 0, &start, &end, | 2153 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
2458 | EXTENT_DIRTY); | 2154 | EXTENT_DIRTY); |
2459 | if (ret) | 2155 | if (ret) |
@@ -2461,209 +2157,21 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |||
2461 | 2157 | ||
2462 | ret = btrfs_discard_extent(root, start, end + 1 - start); | 2158 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
2463 | 2159 | ||
2160 | /* unlocks the pinned mutex */ | ||
2464 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); | 2161 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
2465 | clear_extent_dirty(unpin, start, end, GFP_NOFS); | 2162 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
2466 | 2163 | ||
2467 | if (need_resched()) { | 2164 | cond_resched(); |
2468 | mutex_unlock(&root->fs_info->pinned_mutex); | ||
2469 | cond_resched(); | ||
2470 | mutex_lock(&root->fs_info->pinned_mutex); | ||
2471 | } | ||
2472 | } | 2165 | } |
2473 | mutex_unlock(&root->fs_info->pinned_mutex); | 2166 | mutex_unlock(&root->fs_info->pinned_mutex); |
2474 | return ret; | 2167 | return ret; |
2475 | } | 2168 | } |
2476 | 2169 | ||
2477 | static 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)); | ||
2499 | again: | ||
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 | } | ||
2659 | out: | ||
2660 | btrfs_free_path(path); | ||
2661 | return 0; | ||
2662 | } | ||
2663 | |||
2664 | static int pin_down_bytes(struct btrfs_trans_handle *trans, | 2170 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
2665 | struct btrfs_root *root, | 2171 | struct btrfs_root *root, |
2666 | u64 bytenr, u64 num_bytes, int is_data) | 2172 | struct btrfs_path *path, |
2173 | u64 bytenr, u64 num_bytes, int is_data, | ||
2174 | struct extent_buffer **must_clean) | ||
2667 | { | 2175 | { |
2668 | int err = 0; | 2176 | int err = 0; |
2669 | struct extent_buffer *buf; | 2177 | struct extent_buffer *buf; |
@@ -2686,17 +2194,19 @@ static int pin_down_bytes(struct btrfs_trans_handle *trans, | |||
2686 | u64 header_transid = btrfs_header_generation(buf); | 2194 | u64 header_transid = btrfs_header_generation(buf); |
2687 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && | 2195 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
2688 | header_owner != BTRFS_TREE_RELOC_OBJECTID && | 2196 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
2197 | header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID && | ||
2689 | header_transid == trans->transid && | 2198 | header_transid == trans->transid && |
2690 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { | 2199 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
2691 | clean_tree_block(NULL, root, buf); | 2200 | *must_clean = buf; |
2692 | btrfs_tree_unlock(buf); | ||
2693 | free_extent_buffer(buf); | ||
2694 | return 1; | 2201 | return 1; |
2695 | } | 2202 | } |
2696 | btrfs_tree_unlock(buf); | 2203 | btrfs_tree_unlock(buf); |
2697 | } | 2204 | } |
2698 | free_extent_buffer(buf); | 2205 | free_extent_buffer(buf); |
2699 | pinit: | 2206 | pinit: |
2207 | btrfs_set_path_blocking(path); | ||
2208 | mutex_lock(&root->fs_info->pinned_mutex); | ||
2209 | /* unlocks the pinned mutex */ | ||
2700 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); | 2210 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
2701 | 2211 | ||
2702 | BUG_ON(err < 0); | 2212 | BUG_ON(err < 0); |
@@ -2710,7 +2220,8 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2710 | struct btrfs_root *root, | 2220 | struct btrfs_root *root, |
2711 | u64 bytenr, u64 num_bytes, u64 parent, | 2221 | u64 bytenr, u64 num_bytes, u64 parent, |
2712 | u64 root_objectid, u64 ref_generation, | 2222 | u64 root_objectid, u64 ref_generation, |
2713 | u64 owner_objectid, int pin, int mark_free) | 2223 | u64 owner_objectid, int pin, int mark_free, |
2224 | int refs_to_drop) | ||
2714 | { | 2225 | { |
2715 | struct btrfs_path *path; | 2226 | struct btrfs_path *path; |
2716 | struct btrfs_key key; | 2227 | struct btrfs_key key; |
@@ -2732,6 +2243,7 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2732 | return -ENOMEM; | 2243 | return -ENOMEM; |
2733 | 2244 | ||
2734 | path->reada = 1; | 2245 | path->reada = 1; |
2246 | path->leave_spinning = 1; | ||
2735 | ret = lookup_extent_backref(trans, extent_root, path, | 2247 | ret = lookup_extent_backref(trans, extent_root, path, |
2736 | bytenr, parent, root_objectid, | 2248 | bytenr, parent, root_objectid, |
2737 | ref_generation, owner_objectid, 1); | 2249 | ref_generation, owner_objectid, 1); |
@@ -2753,9 +2265,11 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2753 | break; | 2265 | break; |
2754 | } | 2266 | } |
2755 | if (!found_extent) { | 2267 | if (!found_extent) { |
2756 | ret = remove_extent_backref(trans, extent_root, path); | 2268 | ret = remove_extent_backref(trans, extent_root, path, |
2269 | refs_to_drop); | ||
2757 | BUG_ON(ret); | 2270 | BUG_ON(ret); |
2758 | btrfs_release_path(extent_root, path); | 2271 | btrfs_release_path(extent_root, path); |
2272 | path->leave_spinning = 1; | ||
2759 | ret = btrfs_search_slot(trans, extent_root, | 2273 | ret = btrfs_search_slot(trans, extent_root, |
2760 | &key, path, -1, 1); | 2274 | &key, path, -1, 1); |
2761 | if (ret) { | 2275 | if (ret) { |
@@ -2771,8 +2285,9 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2771 | btrfs_print_leaf(extent_root, path->nodes[0]); | 2285 | btrfs_print_leaf(extent_root, path->nodes[0]); |
2772 | WARN_ON(1); | 2286 | WARN_ON(1); |
2773 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " | 2287 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
2774 | "root %llu gen %llu owner %llu\n", | 2288 | "parent %llu root %llu gen %llu owner %llu\n", |
2775 | (unsigned long long)bytenr, | 2289 | (unsigned long long)bytenr, |
2290 | (unsigned long long)parent, | ||
2776 | (unsigned long long)root_objectid, | 2291 | (unsigned long long)root_objectid, |
2777 | (unsigned long long)ref_generation, | 2292 | (unsigned long long)ref_generation, |
2778 | (unsigned long long)owner_objectid); | 2293 | (unsigned long long)owner_objectid); |
@@ -2782,17 +2297,23 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2782 | ei = btrfs_item_ptr(leaf, extent_slot, | 2297 | ei = btrfs_item_ptr(leaf, extent_slot, |
2783 | struct btrfs_extent_item); | 2298 | struct btrfs_extent_item); |
2784 | refs = btrfs_extent_refs(leaf, ei); | 2299 | refs = btrfs_extent_refs(leaf, ei); |
2785 | BUG_ON(refs == 0); | ||
2786 | refs -= 1; | ||
2787 | btrfs_set_extent_refs(leaf, ei, refs); | ||
2788 | 2300 | ||
2301 | /* | ||
2302 | * we're not allowed to delete the extent item if there | ||
2303 | * are other delayed ref updates pending | ||
2304 | */ | ||
2305 | |||
2306 | BUG_ON(refs < refs_to_drop); | ||
2307 | refs -= refs_to_drop; | ||
2308 | btrfs_set_extent_refs(leaf, ei, refs); | ||
2789 | btrfs_mark_buffer_dirty(leaf); | 2309 | btrfs_mark_buffer_dirty(leaf); |
2790 | 2310 | ||
2791 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { | 2311 | if (refs == 0 && found_extent && |
2312 | path->slots[0] == extent_slot + 1) { | ||
2792 | struct btrfs_extent_ref *ref; | 2313 | struct btrfs_extent_ref *ref; |
2793 | ref = btrfs_item_ptr(leaf, path->slots[0], | 2314 | ref = btrfs_item_ptr(leaf, path->slots[0], |
2794 | struct btrfs_extent_ref); | 2315 | struct btrfs_extent_ref); |
2795 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); | 2316 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop); |
2796 | /* if the back ref and the extent are next to each other | 2317 | /* if the back ref and the extent are next to each other |
2797 | * they get deleted below in one shot | 2318 | * they get deleted below in one shot |
2798 | */ | 2319 | */ |
@@ -2800,11 +2321,13 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2800 | num_to_del = 2; | 2321 | num_to_del = 2; |
2801 | } else if (found_extent) { | 2322 | } else if (found_extent) { |
2802 | /* otherwise delete the extent back ref */ | 2323 | /* otherwise delete the extent back ref */ |
2803 | ret = remove_extent_backref(trans, extent_root, path); | 2324 | ret = remove_extent_backref(trans, extent_root, path, |
2325 | refs_to_drop); | ||
2804 | BUG_ON(ret); | 2326 | BUG_ON(ret); |
2805 | /* if refs are 0, we need to setup the path for deletion */ | 2327 | /* if refs are 0, we need to setup the path for deletion */ |
2806 | if (refs == 0) { | 2328 | if (refs == 0) { |
2807 | btrfs_release_path(extent_root, path); | 2329 | btrfs_release_path(extent_root, path); |
2330 | path->leave_spinning = 1; | ||
2808 | ret = btrfs_search_slot(trans, extent_root, &key, path, | 2331 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
2809 | -1, 1); | 2332 | -1, 1); |
2810 | BUG_ON(ret); | 2333 | BUG_ON(ret); |
@@ -2814,16 +2337,18 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2814 | if (refs == 0) { | 2337 | if (refs == 0) { |
2815 | u64 super_used; | 2338 | u64 super_used; |
2816 | u64 root_used; | 2339 | u64 root_used; |
2340 | struct extent_buffer *must_clean = NULL; | ||
2817 | 2341 | ||
2818 | if (pin) { | 2342 | if (pin) { |
2819 | mutex_lock(&root->fs_info->pinned_mutex); | 2343 | ret = pin_down_bytes(trans, root, path, |
2820 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, | 2344 | bytenr, num_bytes, |
2821 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); | 2345 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, |
2822 | mutex_unlock(&root->fs_info->pinned_mutex); | 2346 | &must_clean); |
2823 | if (ret > 0) | 2347 | if (ret > 0) |
2824 | mark_free = 1; | 2348 | mark_free = 1; |
2825 | BUG_ON(ret < 0); | 2349 | BUG_ON(ret < 0); |
2826 | } | 2350 | } |
2351 | |||
2827 | /* block accounting for super block */ | 2352 | /* block accounting for super block */ |
2828 | spin_lock(&info->delalloc_lock); | 2353 | spin_lock(&info->delalloc_lock); |
2829 | super_used = btrfs_super_bytes_used(&info->super_copy); | 2354 | super_used = btrfs_super_bytes_used(&info->super_copy); |
@@ -2835,14 +2360,34 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2835 | btrfs_set_root_used(&root->root_item, | 2360 | btrfs_set_root_used(&root->root_item, |
2836 | root_used - num_bytes); | 2361 | root_used - num_bytes); |
2837 | spin_unlock(&info->delalloc_lock); | 2362 | spin_unlock(&info->delalloc_lock); |
2363 | |||
2364 | /* | ||
2365 | * it is going to be very rare for someone to be waiting | ||
2366 | * on the block we're freeing. del_items might need to | ||
2367 | * schedule, so rather than get fancy, just force it | ||
2368 | * to blocking here | ||
2369 | */ | ||
2370 | if (must_clean) | ||
2371 | btrfs_set_lock_blocking(must_clean); | ||
2372 | |||
2838 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], | 2373 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
2839 | num_to_del); | 2374 | num_to_del); |
2840 | BUG_ON(ret); | 2375 | BUG_ON(ret); |
2841 | btrfs_release_path(extent_root, path); | 2376 | btrfs_release_path(extent_root, path); |
2842 | 2377 | ||
2378 | if (must_clean) { | ||
2379 | clean_tree_block(NULL, root, must_clean); | ||
2380 | btrfs_tree_unlock(must_clean); | ||
2381 | free_extent_buffer(must_clean); | ||
2382 | } | ||
2383 | |||
2843 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | 2384 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
2844 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); | 2385 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
2845 | BUG_ON(ret); | 2386 | BUG_ON(ret); |
2387 | } else { | ||
2388 | invalidate_mapping_pages(info->btree_inode->i_mapping, | ||
2389 | bytenr >> PAGE_CACHE_SHIFT, | ||
2390 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); | ||
2846 | } | 2391 | } |
2847 | 2392 | ||
2848 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, | 2393 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
@@ -2850,218 +2395,103 @@ static int __free_extent(struct btrfs_trans_handle *trans, | |||
2850 | BUG_ON(ret); | 2395 | BUG_ON(ret); |
2851 | } | 2396 | } |
2852 | btrfs_free_path(path); | 2397 | btrfs_free_path(path); |
2853 | finish_current_insert(trans, extent_root, 0); | ||
2854 | return ret; | 2398 | return ret; |
2855 | } | 2399 | } |
2856 | 2400 | ||
2857 | /* | 2401 | /* |
2858 | * find all the blocks marked as pending in the radix tree and remove | 2402 | * remove an extent from the root, returns 0 on success |
2859 | * them from the extent map | ||
2860 | */ | 2403 | */ |
2861 | static int del_pending_extents(struct btrfs_trans_handle *trans, | 2404 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
2862 | struct btrfs_root *extent_root, int all) | 2405 | struct btrfs_root *root, |
2406 | u64 bytenr, u64 num_bytes, u64 parent, | ||
2407 | u64 root_objectid, u64 ref_generation, | ||
2408 | u64 owner_objectid, int pin, | ||
2409 | int refs_to_drop) | ||
2863 | { | 2410 | { |
2864 | int ret; | 2411 | 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 | |||
2881 | again: | ||
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 | 2412 | ||
2964 | if (nr) { | 2413 | /* |
2965 | ret = free_extents(trans, extent_root, &delete_list); | 2414 | * if metadata always pin |
2966 | BUG_ON(ret); | 2415 | * if data pin when any transaction has committed this |
2967 | } | 2416 | */ |
2417 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID || | ||
2418 | ref_generation != trans->transid) | ||
2419 | pin = 1; | ||
2968 | 2420 | ||
2969 | if (all && skipped) { | 2421 | if (ref_generation != trans->transid) |
2970 | INIT_LIST_HEAD(&delete_list); | 2422 | pin = 1; |
2971 | search = 0; | ||
2972 | nr = 0; | ||
2973 | goto again; | ||
2974 | } | ||
2975 | 2423 | ||
2976 | if (!err) | 2424 | return __free_extent(trans, root, bytenr, num_bytes, parent, |
2977 | finish_current_insert(trans, extent_root, 0); | 2425 | root_objectid, ref_generation, |
2978 | return err; | 2426 | owner_objectid, pin, pin == 0, refs_to_drop); |
2979 | } | 2427 | } |
2980 | 2428 | ||
2981 | /* | 2429 | /* |
2982 | * remove an extent from the root, returns 0 on success | 2430 | * when we free an extent, it is possible (and likely) that we free the last |
2431 | * delayed ref for that extent as well. This searches the delayed ref tree for | ||
2432 | * a given extent, and if there are no other delayed refs to be processed, it | ||
2433 | * removes it from the tree. | ||
2983 | */ | 2434 | */ |
2984 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | 2435 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
2985 | struct btrfs_root *root, | 2436 | 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 | { | 2437 | { |
2990 | struct btrfs_root *extent_root = root->fs_info->extent_root; | 2438 | struct btrfs_delayed_ref_head *head; |
2991 | int pending_ret; | 2439 | struct btrfs_delayed_ref_root *delayed_refs; |
2440 | struct btrfs_delayed_ref_node *ref; | ||
2441 | struct rb_node *node; | ||
2992 | int ret; | 2442 | int ret; |
2993 | 2443 | ||
2994 | WARN_ON(num_bytes < root->sectorsize); | 2444 | delayed_refs = &trans->transaction->delayed_refs; |
2995 | if (root == extent_root) { | 2445 | spin_lock(&delayed_refs->lock); |
2996 | struct pending_extent_op *extent_op = NULL; | 2446 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
2997 | 2447 | if (!head) | |
2998 | mutex_lock(&root->fs_info->extent_ins_mutex); | 2448 | 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 | 2449 | ||
3008 | extent_op->del = 1; | 2450 | node = rb_prev(&head->node.rb_node); |
3009 | if (extent_op->type == PENDING_EXTENT_INSERT) { | 2451 | if (!node) |
3010 | mutex_unlock(&root->fs_info->extent_ins_mutex); | 2452 | goto out; |
3011 | return 0; | ||
3012 | } | ||
3013 | } | ||
3014 | 2453 | ||
3015 | if (extent_op) { | 2454 | 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 | 2455 | ||
3020 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | 2456 | /* there are still entries for this ref, we can't drop it */ |
3021 | BUG_ON(!extent_op); | 2457 | if (ref->bytenr == bytenr) |
3022 | 2458 | 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 | 2459 | ||
3054 | /* if data pin when any transaction has committed this */ | 2460 | /* |
3055 | if (ref_generation != trans->transid) | 2461 | * waiting for the lock here would deadlock. If someone else has it |
3056 | pin = 1; | 2462 | * locked they are already in the process of dropping it anyway |
2463 | */ | ||
2464 | if (!mutex_trylock(&head->mutex)) | ||
2465 | goto out; | ||
3057 | 2466 | ||
3058 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, | 2467 | /* |
3059 | root_objectid, ref_generation, | 2468 | * at this point we have a head with no other entries. Go |
3060 | owner_objectid, pin, pin == 0); | 2469 | * ahead and process it. |
2470 | */ | ||
2471 | head->node.in_tree = 0; | ||
2472 | rb_erase(&head->node.rb_node, &delayed_refs->root); | ||
3061 | 2473 | ||
3062 | finish_current_insert(trans, root->fs_info->extent_root, 0); | 2474 | delayed_refs->num_entries--; |
3063 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); | 2475 | |
3064 | return ret ? ret : pending_ret; | 2476 | /* |
2477 | * we don't take a ref on the node because we're removing it from the | ||
2478 | * tree, so we just steal the ref the tree was holding. | ||
2479 | */ | ||
2480 | delayed_refs->num_heads--; | ||
2481 | if (list_empty(&head->cluster)) | ||
2482 | delayed_refs->num_heads_ready--; | ||
2483 | |||
2484 | list_del_init(&head->cluster); | ||
2485 | spin_unlock(&delayed_refs->lock); | ||
2486 | |||
2487 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, | ||
2488 | &head->node, head->must_insert_reserved); | ||
2489 | BUG_ON(ret); | ||
2490 | btrfs_put_delayed_ref(&head->node); | ||
2491 | return 0; | ||
2492 | out: | ||
2493 | spin_unlock(&delayed_refs->lock); | ||
2494 | return 0; | ||
3065 | } | 2495 | } |
3066 | 2496 | ||
3067 | int btrfs_free_extent(struct btrfs_trans_handle *trans, | 2497 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
@@ -3072,9 +2502,30 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, | |||
3072 | { | 2502 | { |
3073 | int ret; | 2503 | int ret; |
3074 | 2504 | ||
3075 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, | 2505 | /* |
3076 | root_objectid, ref_generation, | 2506 | * tree log blocks never actually go into the extent allocation |
3077 | owner_objectid, pin); | 2507 | * tree, just update pinning info and exit early. |
2508 | * | ||
2509 | * data extents referenced by the tree log do need to have | ||
2510 | * their reference counts bumped. | ||
2511 | */ | ||
2512 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID && | ||
2513 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { | ||
2514 | mutex_lock(&root->fs_info->pinned_mutex); | ||
2515 | |||
2516 | /* unlocks the pinned mutex */ | ||
2517 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); | ||
2518 | update_reserved_extents(root, bytenr, num_bytes, 0); | ||
2519 | ret = 0; | ||
2520 | } else { | ||
2521 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, | ||
2522 | root_objectid, ref_generation, | ||
2523 | owner_objectid, | ||
2524 | BTRFS_DROP_DELAYED_REF, 1); | ||
2525 | BUG_ON(ret); | ||
2526 | ret = check_ref_cleanup(trans, root, bytenr); | ||
2527 | BUG_ON(ret); | ||
2528 | } | ||
3078 | return ret; | 2529 | return ret; |
3079 | } | 2530 | } |
3080 | 2531 | ||
@@ -3475,10 +2926,10 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans, | |||
3475 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | 2926 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
3476 | struct btrfs_root *root, u64 parent, | 2927 | struct btrfs_root *root, u64 parent, |
3477 | u64 root_objectid, u64 ref_generation, | 2928 | u64 root_objectid, u64 ref_generation, |
3478 | u64 owner, struct btrfs_key *ins) | 2929 | u64 owner, struct btrfs_key *ins, |
2930 | int ref_mod) | ||
3479 | { | 2931 | { |
3480 | int ret; | 2932 | int ret; |
3481 | int pending_ret; | ||
3482 | u64 super_used; | 2933 | u64 super_used; |
3483 | u64 root_used; | 2934 | u64 root_used; |
3484 | u64 num_bytes = ins->offset; | 2935 | u64 num_bytes = ins->offset; |
@@ -3503,33 +2954,6 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |||
3503 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); | 2954 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
3504 | spin_unlock(&info->delalloc_lock); | 2955 | spin_unlock(&info->delalloc_lock); |
3505 | 2956 | ||
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)); | 2957 | memcpy(&keys[0], ins, sizeof(*ins)); |
3534 | keys[1].objectid = ins->objectid; | 2958 | keys[1].objectid = ins->objectid; |
3535 | keys[1].type = BTRFS_EXTENT_REF_KEY; | 2959 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
@@ -3540,37 +2964,31 @@ static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |||
3540 | path = btrfs_alloc_path(); | 2964 | path = btrfs_alloc_path(); |
3541 | BUG_ON(!path); | 2965 | BUG_ON(!path); |
3542 | 2966 | ||
2967 | path->leave_spinning = 1; | ||
3543 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, | 2968 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
3544 | sizes, 2); | 2969 | sizes, 2); |
3545 | BUG_ON(ret); | 2970 | BUG_ON(ret); |
3546 | 2971 | ||
3547 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], | 2972 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
3548 | struct btrfs_extent_item); | 2973 | struct btrfs_extent_item); |
3549 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); | 2974 | btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod); |
3550 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | 2975 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
3551 | struct btrfs_extent_ref); | 2976 | struct btrfs_extent_ref); |
3552 | 2977 | ||
3553 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); | 2978 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
3554 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); | 2979 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
3555 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); | 2980 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
3556 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); | 2981 | btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod); |
3557 | 2982 | ||
3558 | btrfs_mark_buffer_dirty(path->nodes[0]); | 2983 | btrfs_mark_buffer_dirty(path->nodes[0]); |
3559 | 2984 | ||
3560 | trans->alloc_exclude_start = 0; | 2985 | trans->alloc_exclude_start = 0; |
3561 | trans->alloc_exclude_nr = 0; | 2986 | trans->alloc_exclude_nr = 0; |
3562 | btrfs_free_path(path); | 2987 | btrfs_free_path(path); |
3563 | finish_current_insert(trans, extent_root, 0); | ||
3564 | pending_ret = del_pending_extents(trans, extent_root, 0); | ||
3565 | 2988 | ||
3566 | if (ret) | 2989 | if (ret) |
3567 | goto out; | 2990 | goto out; |
3568 | if (pending_ret) { | ||
3569 | ret = pending_ret; | ||
3570 | goto out; | ||
3571 | } | ||
3572 | 2991 | ||
3573 | update_block: | ||
3574 | ret = update_block_group(trans, root, ins->objectid, | 2992 | ret = update_block_group(trans, root, ins->objectid, |
3575 | ins->offset, 1, 0); | 2993 | ins->offset, 1, 0); |
3576 | if (ret) { | 2994 | if (ret) { |
@@ -3592,9 +3010,12 @@ int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |||
3592 | 3010 | ||
3593 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) | 3011 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
3594 | return 0; | 3012 | return 0; |
3595 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, | 3013 | |
3596 | ref_generation, owner, ins); | 3014 | ret = btrfs_add_delayed_ref(trans, ins->objectid, |
3597 | update_reserved_extents(root, ins->objectid, ins->offset, 0); | 3015 | ins->offset, parent, root_objectid, |
3016 | ref_generation, owner, | ||
3017 | BTRFS_ADD_DELAYED_EXTENT, 0); | ||
3018 | BUG_ON(ret); | ||
3598 | return ret; | 3019 | return ret; |
3599 | } | 3020 | } |
3600 | 3021 | ||
@@ -3621,7 +3042,7 @@ int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | |||
3621 | BUG_ON(ret); | 3042 | BUG_ON(ret); |
3622 | put_block_group(block_group); | 3043 | put_block_group(block_group); |
3623 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, | 3044 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
3624 | ref_generation, owner, ins); | 3045 | ref_generation, owner, ins, 1); |
3625 | return ret; | 3046 | return ret; |
3626 | } | 3047 | } |
3627 | 3048 | ||
@@ -3640,20 +3061,18 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans, | |||
3640 | u64 search_end, struct btrfs_key *ins, u64 data) | 3061 | u64 search_end, struct btrfs_key *ins, u64 data) |
3641 | { | 3062 | { |
3642 | int ret; | 3063 | int ret; |
3643 | |||
3644 | ret = __btrfs_reserve_extent(trans, root, num_bytes, | 3064 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
3645 | min_alloc_size, empty_size, hint_byte, | 3065 | min_alloc_size, empty_size, hint_byte, |
3646 | search_end, ins, data); | 3066 | search_end, ins, data); |
3647 | BUG_ON(ret); | 3067 | BUG_ON(ret); |
3648 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { | 3068 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
3649 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, | 3069 | ret = btrfs_add_delayed_ref(trans, ins->objectid, |
3650 | root_objectid, ref_generation, | 3070 | ins->offset, parent, root_objectid, |
3651 | owner_objectid, ins); | 3071 | ref_generation, owner_objectid, |
3072 | BTRFS_ADD_DELAYED_EXTENT, 0); | ||
3652 | BUG_ON(ret); | 3073 | BUG_ON(ret); |
3653 | |||
3654 | } else { | ||
3655 | update_reserved_extents(root, ins->objectid, ins->offset, 1); | ||
3656 | } | 3074 | } |
3075 | update_reserved_extents(root, ins->objectid, ins->offset, 1); | ||
3657 | return ret; | 3076 | return ret; |
3658 | } | 3077 | } |
3659 | 3078 | ||
@@ -3789,7 +3208,7 @@ int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, | |||
3789 | 3208 | ||
3790 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | 3209 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
3791 | 3210 | ||
3792 | ret = __btrfs_free_extent(trans, root, disk_bytenr, | 3211 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
3793 | btrfs_file_extent_disk_num_bytes(leaf, fi), | 3212 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
3794 | leaf->start, leaf_owner, leaf_generation, | 3213 | leaf->start, leaf_owner, leaf_generation, |
3795 | key.objectid, 0); | 3214 | key.objectid, 0); |
@@ -3829,7 +3248,7 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, | |||
3829 | */ | 3248 | */ |
3830 | for (i = 0; i < ref->nritems; i++) { | 3249 | for (i = 0; i < ref->nritems; i++) { |
3831 | info = ref->extents + sorted[i].slot; | 3250 | info = ref->extents + sorted[i].slot; |
3832 | ret = __btrfs_free_extent(trans, root, info->bytenr, | 3251 | ret = btrfs_free_extent(trans, root, info->bytenr, |
3833 | info->num_bytes, ref->bytenr, | 3252 | info->num_bytes, ref->bytenr, |
3834 | ref->owner, ref->generation, | 3253 | ref->owner, ref->generation, |
3835 | info->objectid, 0); | 3254 | info->objectid, 0); |
@@ -3846,12 +3265,13 @@ static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, | |||
3846 | return 0; | 3265 | return 0; |
3847 | } | 3266 | } |
3848 | 3267 | ||
3849 | static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, | 3268 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, |
3269 | struct btrfs_root *root, u64 start, | ||
3850 | u64 len, u32 *refs) | 3270 | u64 len, u32 *refs) |
3851 | { | 3271 | { |
3852 | int ret; | 3272 | int ret; |
3853 | 3273 | ||
3854 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); | 3274 | ret = btrfs_lookup_extent_ref(trans, root, start, len, refs); |
3855 | BUG_ON(ret); | 3275 | BUG_ON(ret); |
3856 | 3276 | ||
3857 | #if 0 /* some debugging code in case we see problems here */ | 3277 | #if 0 /* some debugging code in case we see problems here */ |
@@ -3959,7 +3379,8 @@ static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, | |||
3959 | * we just decrement it below and don't update any | 3379 | * we just decrement it below and don't update any |
3960 | * of the refs the leaf points to. | 3380 | * of the refs the leaf points to. |
3961 | */ | 3381 | */ |
3962 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); | 3382 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
3383 | blocksize, &refs); | ||
3963 | BUG_ON(ret); | 3384 | BUG_ON(ret); |
3964 | if (refs != 1) | 3385 | if (refs != 1) |
3965 | continue; | 3386 | continue; |
@@ -4010,7 +3431,7 @@ static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, | |||
4010 | */ | 3431 | */ |
4011 | for (i = 0; i < refi; i++) { | 3432 | for (i = 0; i < refi; i++) { |
4012 | bytenr = sorted[i].bytenr; | 3433 | bytenr = sorted[i].bytenr; |
4013 | ret = __btrfs_free_extent(trans, root, bytenr, | 3434 | ret = btrfs_free_extent(trans, root, bytenr, |
4014 | blocksize, eb->start, | 3435 | blocksize, eb->start, |
4015 | root_owner, root_gen, 0, 1); | 3436 | root_owner, root_gen, 0, 1); |
4016 | BUG_ON(ret); | 3437 | BUG_ON(ret); |
@@ -4053,7 +3474,7 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans, | |||
4053 | 3474 | ||
4054 | WARN_ON(*level < 0); | 3475 | WARN_ON(*level < 0); |
4055 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | 3476 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
4056 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, | 3477 | ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start, |
4057 | path->nodes[*level]->len, &refs); | 3478 | path->nodes[*level]->len, &refs); |
4058 | BUG_ON(ret); | 3479 | BUG_ON(ret); |
4059 | if (refs > 1) | 3480 | if (refs > 1) |
@@ -4104,7 +3525,8 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans, | |||
4104 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | 3525 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
4105 | blocksize = btrfs_level_size(root, *level - 1); | 3526 | blocksize = btrfs_level_size(root, *level - 1); |
4106 | 3527 | ||
4107 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); | 3528 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
3529 | blocksize, &refs); | ||
4108 | BUG_ON(ret); | 3530 | BUG_ON(ret); |
4109 | 3531 | ||
4110 | /* | 3532 | /* |
@@ -4119,7 +3541,7 @@ static noinline int walk_down_tree(struct btrfs_trans_handle *trans, | |||
4119 | root_gen = btrfs_header_generation(parent); | 3541 | root_gen = btrfs_header_generation(parent); |
4120 | path->slots[*level]++; | 3542 | path->slots[*level]++; |
4121 | 3543 | ||
4122 | ret = __btrfs_free_extent(trans, root, bytenr, | 3544 | ret = btrfs_free_extent(trans, root, bytenr, |
4123 | blocksize, parent->start, | 3545 | blocksize, parent->start, |
4124 | root_owner, root_gen, | 3546 | root_owner, root_gen, |
4125 | *level - 1, 1); | 3547 | *level - 1, 1); |
@@ -4165,7 +3587,7 @@ out: | |||
4165 | * cleanup and free the reference on the last node | 3587 | * cleanup and free the reference on the last node |
4166 | * we processed | 3588 | * we processed |
4167 | */ | 3589 | */ |
4168 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, | 3590 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
4169 | parent->start, root_owner, root_gen, | 3591 | parent->start, root_owner, root_gen, |
4170 | *level, 1); | 3592 | *level, 1); |
4171 | free_extent_buffer(path->nodes[*level]); | 3593 | free_extent_buffer(path->nodes[*level]); |
@@ -4354,6 +3776,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
4354 | struct btrfs_path *path; | 3776 | struct btrfs_path *path; |
4355 | int i; | 3777 | int i; |
4356 | int orig_level; | 3778 | int orig_level; |
3779 | int update_count; | ||
4357 | struct btrfs_root_item *root_item = &root->root_item; | 3780 | struct btrfs_root_item *root_item = &root->root_item; |
4358 | 3781 | ||
4359 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); | 3782 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
@@ -4395,6 +3818,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
4395 | } | 3818 | } |
4396 | } | 3819 | } |
4397 | while (1) { | 3820 | while (1) { |
3821 | unsigned long update; | ||
4398 | wret = walk_down_tree(trans, root, path, &level); | 3822 | wret = walk_down_tree(trans, root, path, &level); |
4399 | if (wret > 0) | 3823 | if (wret > 0) |
4400 | break; | 3824 | break; |
@@ -4407,12 +3831,21 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root | |||
4407 | break; | 3831 | break; |
4408 | if (wret < 0) | 3832 | if (wret < 0) |
4409 | ret = wret; | 3833 | ret = wret; |
4410 | if (trans->transaction->in_commit) { | 3834 | if (trans->transaction->in_commit || |
3835 | trans->transaction->delayed_refs.flushing) { | ||
4411 | ret = -EAGAIN; | 3836 | ret = -EAGAIN; |
4412 | break; | 3837 | break; |
4413 | } | 3838 | } |
4414 | atomic_inc(&root->fs_info->throttle_gen); | 3839 | atomic_inc(&root->fs_info->throttle_gen); |
4415 | wake_up(&root->fs_info->transaction_throttle); | 3840 | wake_up(&root->fs_info->transaction_throttle); |
3841 | for (update_count = 0; update_count < 16; update_count++) { | ||
3842 | update = trans->delayed_ref_updates; | ||
3843 | trans->delayed_ref_updates = 0; | ||
3844 | if (update) | ||
3845 | btrfs_run_delayed_refs(trans, root, update); | ||
3846 | else | ||
3847 | break; | ||
3848 | } | ||
4416 | } | 3849 | } |
4417 | for (i = 0; i <= orig_level; i++) { | 3850 | for (i = 0; i <= orig_level; i++) { |
4418 | if (path->nodes[i]) { | 3851 | if (path->nodes[i]) { |
@@ -5457,6 +4890,7 @@ static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, | |||
5457 | root->root_key.objectid, | 4890 | root->root_key.objectid, |
5458 | trans->transid, key.objectid); | 4891 | trans->transid, key.objectid); |
5459 | BUG_ON(ret); | 4892 | BUG_ON(ret); |
4893 | |||
5460 | ret = btrfs_free_extent(trans, root, | 4894 | ret = btrfs_free_extent(trans, root, |
5461 | bytenr, num_bytes, leaf->start, | 4895 | bytenr, num_bytes, leaf->start, |
5462 | btrfs_header_owner(leaf), | 4896 | btrfs_header_owner(leaf), |
@@ -5768,9 +5202,6 @@ static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, | |||
5768 | ref_path, NULL, NULL); | 5202 | ref_path, NULL, NULL); |
5769 | BUG_ON(ret); | 5203 | BUG_ON(ret); |
5770 | 5204 | ||
5771 | if (root == root->fs_info->extent_root) | ||
5772 | btrfs_extent_post_op(trans, root); | ||
5773 | |||
5774 | return 0; | 5205 | return 0; |
5775 | } | 5206 | } |
5776 | 5207 | ||
@@ -6038,6 +5469,7 @@ static int __insert_orphan_inode(struct btrfs_trans_handle *trans, | |||
6038 | if (!path) | 5469 | if (!path) |
6039 | return -ENOMEM; | 5470 | return -ENOMEM; |
6040 | 5471 | ||
5472 | path->leave_spinning = 1; | ||
6041 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); | 5473 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
6042 | if (ret) | 5474 | if (ret) |
6043 | goto out; | 5475 | goto out; |
@@ -6208,6 +5640,9 @@ again: | |||
6208 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); | 5640 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
6209 | mutex_unlock(&root->fs_info->cleaner_mutex); | 5641 | mutex_unlock(&root->fs_info->cleaner_mutex); |
6210 | 5642 | ||
5643 | trans = btrfs_start_transaction(info->tree_root, 1); | ||
5644 | btrfs_commit_transaction(trans, info->tree_root); | ||
5645 | |||
6211 | while (1) { | 5646 | while (1) { |
6212 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 5647 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6213 | if (ret < 0) | 5648 | if (ret < 0) |
@@ -6466,7 +5901,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |||
6466 | 5901 | ||
6467 | extent_root = root->fs_info->extent_root; | 5902 | extent_root = root->fs_info->extent_root; |
6468 | 5903 | ||
6469 | root->fs_info->last_trans_new_blockgroup = trans->transid; | 5904 | root->fs_info->last_trans_log_full_commit = trans->transid; |
6470 | 5905 | ||
6471 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | 5906 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
6472 | if (!cache) | 5907 | if (!cache) |
@@ -6500,9 +5935,6 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |||
6500 | sizeof(cache->item)); | 5935 | sizeof(cache->item)); |
6501 | BUG_ON(ret); | 5936 | BUG_ON(ret); |
6502 | 5937 | ||
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); | 5938 | set_avail_alloc_bits(extent_root->fs_info, type); |
6507 | 5939 | ||
6508 | return 0; | 5940 | return 0; |