aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c549
1 files changed, 360 insertions, 189 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 3d551231caba..2305b5c5cf00 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -105,6 +105,8 @@ static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
105 u64 num_bytes, int reserve); 105 u64 num_bytes, int reserve);
106static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, 106static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
107 u64 num_bytes); 107 u64 num_bytes);
108int btrfs_pin_extent(struct btrfs_root *root,
109 u64 bytenr, u64 num_bytes, int reserved);
108 110
109static noinline int 111static noinline int
110block_group_cache_done(struct btrfs_block_group_cache *cache) 112block_group_cache_done(struct btrfs_block_group_cache *cache)
@@ -270,9 +272,27 @@ static int exclude_super_stripes(struct btrfs_root *root,
270 return ret; 272 return ret;
271 273
272 while (nr--) { 274 while (nr--) {
273 cache->bytes_super += stripe_len; 275 u64 start, len;
274 ret = add_excluded_extent(root, logical[nr], 276
275 stripe_len); 277 if (logical[nr] > cache->key.objectid +
278 cache->key.offset)
279 continue;
280
281 if (logical[nr] + stripe_len <= cache->key.objectid)
282 continue;
283
284 start = logical[nr];
285 if (start < cache->key.objectid) {
286 start = cache->key.objectid;
287 len = (logical[nr] + stripe_len) - start;
288 } else {
289 len = min_t(u64, stripe_len,
290 cache->key.objectid +
291 cache->key.offset - start);
292 }
293
294 cache->bytes_super += len;
295 ret = add_excluded_extent(root, start, len);
276 if (ret) { 296 if (ret) {
277 kfree(logical); 297 kfree(logical);
278 return ret; 298 return ret;
@@ -419,8 +439,7 @@ again:
419 if (ret) 439 if (ret)
420 break; 440 break;
421 441
422 if (need_resched() || 442 if (need_resched()) {
423 btrfs_next_leaf(extent_root, path)) {
424 caching_ctl->progress = last; 443 caching_ctl->progress = last;
425 btrfs_release_path(path); 444 btrfs_release_path(path);
426 up_read(&fs_info->extent_commit_sem); 445 up_read(&fs_info->extent_commit_sem);
@@ -428,6 +447,12 @@ again:
428 cond_resched(); 447 cond_resched();
429 goto again; 448 goto again;
430 } 449 }
450
451 ret = btrfs_next_leaf(extent_root, path);
452 if (ret < 0)
453 goto err;
454 if (ret)
455 break;
431 leaf = path->nodes[0]; 456 leaf = path->nodes[0];
432 nritems = btrfs_header_nritems(leaf); 457 nritems = btrfs_header_nritems(leaf);
433 continue; 458 continue;
@@ -442,11 +467,16 @@ again:
442 block_group->key.offset) 467 block_group->key.offset)
443 break; 468 break;
444 469
445 if (key.type == BTRFS_EXTENT_ITEM_KEY) { 470 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
471 key.type == BTRFS_METADATA_ITEM_KEY) {
446 total_found += add_new_free_space(block_group, 472 total_found += add_new_free_space(block_group,
447 fs_info, last, 473 fs_info, last,
448 key.objectid); 474 key.objectid);
449 last = key.objectid + key.offset; 475 if (key.type == BTRFS_METADATA_ITEM_KEY)
476 last = key.objectid +
477 fs_info->tree_root->leafsize;
478 else
479 last = key.objectid + key.offset;
450 480
451 if (total_found > (1024 * 1024 * 2)) { 481 if (total_found > (1024 * 1024 * 2)) {
452 total_found = 0; 482 total_found = 0;
@@ -656,55 +686,6 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
656 rcu_read_unlock(); 686 rcu_read_unlock();
657} 687}
658 688
659u64 btrfs_find_block_group(struct btrfs_root *root,
660 u64 search_start, u64 search_hint, int owner)
661{
662 struct btrfs_block_group_cache *cache;
663 u64 used;
664 u64 last = max(search_hint, search_start);
665 u64 group_start = 0;
666 int full_search = 0;
667 int factor = 9;
668 int wrapped = 0;
669again:
670 while (1) {
671 cache = btrfs_lookup_first_block_group(root->fs_info, last);
672 if (!cache)
673 break;
674
675 spin_lock(&cache->lock);
676 last = cache->key.objectid + cache->key.offset;
677 used = btrfs_block_group_used(&cache->item);
678
679 if ((full_search || !cache->ro) &&
680 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
681 if (used + cache->pinned + cache->reserved <
682 div_factor(cache->key.offset, factor)) {
683 group_start = cache->key.objectid;
684 spin_unlock(&cache->lock);
685 btrfs_put_block_group(cache);
686 goto found;
687 }
688 }
689 spin_unlock(&cache->lock);
690 btrfs_put_block_group(cache);
691 cond_resched();
692 }
693 if (!wrapped) {
694 last = search_start;
695 wrapped = 1;
696 goto again;
697 }
698 if (!full_search && factor < 10) {
699 last = search_start;
700 full_search = 1;
701 factor = 10;
702 goto again;
703 }
704found:
705 return group_start;
706}
707
708/* simple helper to search for an existing extent at a given offset */ 689/* simple helper to search for an existing extent at a given offset */
709int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) 690int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
710{ 691{
@@ -718,15 +699,21 @@ int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
718 699
719 key.objectid = start; 700 key.objectid = start;
720 key.offset = len; 701 key.offset = len;
721 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); 702 key.type = BTRFS_EXTENT_ITEM_KEY;
722 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, 703 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
723 0, 0); 704 0, 0);
705 if (ret > 0) {
706 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
707 if (key.objectid == start &&
708 key.type == BTRFS_METADATA_ITEM_KEY)
709 ret = 0;
710 }
724 btrfs_free_path(path); 711 btrfs_free_path(path);
725 return ret; 712 return ret;
726} 713}
727 714
728/* 715/*
729 * helper function to lookup reference count and flags of extent. 716 * helper function to lookup reference count and flags of a tree block.
730 * 717 *
731 * the head node for delayed ref is used to store the sum of all the 718 * the head node for delayed ref is used to store the sum of all the
732 * reference count modifications queued up in the rbtree. the head 719 * reference count modifications queued up in the rbtree. the head
@@ -736,7 +723,7 @@ int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
736 */ 723 */
737int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, 724int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
738 struct btrfs_root *root, u64 bytenr, 725 struct btrfs_root *root, u64 bytenr,
739 u64 num_bytes, u64 *refs, u64 *flags) 726 u64 offset, int metadata, u64 *refs, u64 *flags)
740{ 727{
741 struct btrfs_delayed_ref_head *head; 728 struct btrfs_delayed_ref_head *head;
742 struct btrfs_delayed_ref_root *delayed_refs; 729 struct btrfs_delayed_ref_root *delayed_refs;
@@ -749,13 +736,29 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
749 u64 extent_flags; 736 u64 extent_flags;
750 int ret; 737 int ret;
751 738
739 /*
740 * If we don't have skinny metadata, don't bother doing anything
741 * different
742 */
743 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
744 offset = root->leafsize;
745 metadata = 0;
746 }
747
752 path = btrfs_alloc_path(); 748 path = btrfs_alloc_path();
753 if (!path) 749 if (!path)
754 return -ENOMEM; 750 return -ENOMEM;
755 751
756 key.objectid = bytenr; 752 if (metadata) {
757 key.type = BTRFS_EXTENT_ITEM_KEY; 753 key.objectid = bytenr;
758 key.offset = num_bytes; 754 key.type = BTRFS_METADATA_ITEM_KEY;
755 key.offset = offset;
756 } else {
757 key.objectid = bytenr;
758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = offset;
760 }
761
759 if (!trans) { 762 if (!trans) {
760 path->skip_locking = 1; 763 path->skip_locking = 1;
761 path->search_commit_root = 1; 764 path->search_commit_root = 1;
@@ -766,6 +769,13 @@ again:
766 if (ret < 0) 769 if (ret < 0)
767 goto out_free; 770 goto out_free;
768 771
772 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
773 key.type = BTRFS_EXTENT_ITEM_KEY;
774 key.offset = root->leafsize;
775 btrfs_release_path(path);
776 goto again;
777 }
778
769 if (ret == 0) { 779 if (ret == 0) {
770 leaf = path->nodes[0]; 780 leaf = path->nodes[0];
771 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 781 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
@@ -1001,7 +1011,7 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1001 return ret; 1011 return ret;
1002 BUG_ON(ret); /* Corruption */ 1012 BUG_ON(ret); /* Corruption */
1003 1013
1004 btrfs_extend_item(trans, root, path, new_size); 1014 btrfs_extend_item(root, path, new_size);
1005 1015
1006 leaf = path->nodes[0]; 1016 leaf = path->nodes[0];
1007 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1017 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
@@ -1453,6 +1463,8 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1453 int want; 1463 int want;
1454 int ret; 1464 int ret;
1455 int err = 0; 1465 int err = 0;
1466 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1467 SKINNY_METADATA);
1456 1468
1457 key.objectid = bytenr; 1469 key.objectid = bytenr;
1458 key.type = BTRFS_EXTENT_ITEM_KEY; 1470 key.type = BTRFS_EXTENT_ITEM_KEY;
@@ -1464,11 +1476,46 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1464 path->keep_locks = 1; 1476 path->keep_locks = 1;
1465 } else 1477 } else
1466 extra_size = -1; 1478 extra_size = -1;
1479
1480 /*
1481 * Owner is our parent level, so we can just add one to get the level
1482 * for the block we are interested in.
1483 */
1484 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1485 key.type = BTRFS_METADATA_ITEM_KEY;
1486 key.offset = owner;
1487 }
1488
1489again:
1467 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); 1490 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1468 if (ret < 0) { 1491 if (ret < 0) {
1469 err = ret; 1492 err = ret;
1470 goto out; 1493 goto out;
1471 } 1494 }
1495
1496 /*
1497 * We may be a newly converted file system which still has the old fat
1498 * extent entries for metadata, so try and see if we have one of those.
1499 */
1500 if (ret > 0 && skinny_metadata) {
1501 skinny_metadata = false;
1502 if (path->slots[0]) {
1503 path->slots[0]--;
1504 btrfs_item_key_to_cpu(path->nodes[0], &key,
1505 path->slots[0]);
1506 if (key.objectid == bytenr &&
1507 key.type == BTRFS_EXTENT_ITEM_KEY &&
1508 key.offset == num_bytes)
1509 ret = 0;
1510 }
1511 if (ret) {
1512 key.type = BTRFS_EXTENT_ITEM_KEY;
1513 key.offset = num_bytes;
1514 btrfs_release_path(path);
1515 goto again;
1516 }
1517 }
1518
1472 if (ret && !insert) { 1519 if (ret && !insert) {
1473 err = -ENOENT; 1520 err = -ENOENT;
1474 goto out; 1521 goto out;
@@ -1504,11 +1551,9 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1504 ptr = (unsigned long)(ei + 1); 1551 ptr = (unsigned long)(ei + 1);
1505 end = (unsigned long)ei + item_size; 1552 end = (unsigned long)ei + item_size;
1506 1553
1507 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 1554 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1508 ptr += sizeof(struct btrfs_tree_block_info); 1555 ptr += sizeof(struct btrfs_tree_block_info);
1509 BUG_ON(ptr > end); 1556 BUG_ON(ptr > end);
1510 } else {
1511 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1512 } 1557 }
1513 1558
1514 err = -ENOENT; 1559 err = -ENOENT;
@@ -1590,8 +1635,7 @@ out:
1590 * helper to add new inline back ref 1635 * helper to add new inline back ref
1591 */ 1636 */
1592static noinline_for_stack 1637static noinline_for_stack
1593void setup_inline_extent_backref(struct btrfs_trans_handle *trans, 1638void setup_inline_extent_backref(struct btrfs_root *root,
1594 struct btrfs_root *root,
1595 struct btrfs_path *path, 1639 struct btrfs_path *path,
1596 struct btrfs_extent_inline_ref *iref, 1640 struct btrfs_extent_inline_ref *iref,
1597 u64 parent, u64 root_objectid, 1641 u64 parent, u64 root_objectid,
@@ -1614,7 +1658,7 @@ void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1614 type = extent_ref_type(parent, owner); 1658 type = extent_ref_type(parent, owner);
1615 size = btrfs_extent_inline_ref_size(type); 1659 size = btrfs_extent_inline_ref_size(type);
1616 1660
1617 btrfs_extend_item(trans, root, path, size); 1661 btrfs_extend_item(root, path, size);
1618 1662
1619 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1663 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1620 refs = btrfs_extent_refs(leaf, ei); 1664 refs = btrfs_extent_refs(leaf, ei);
@@ -1683,8 +1727,7 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1683 * helper to update/remove inline back ref 1727 * helper to update/remove inline back ref
1684 */ 1728 */
1685static noinline_for_stack 1729static noinline_for_stack
1686void update_inline_extent_backref(struct btrfs_trans_handle *trans, 1730void update_inline_extent_backref(struct btrfs_root *root,
1687 struct btrfs_root *root,
1688 struct btrfs_path *path, 1731 struct btrfs_path *path,
1689 struct btrfs_extent_inline_ref *iref, 1732 struct btrfs_extent_inline_ref *iref,
1690 int refs_to_mod, 1733 int refs_to_mod,
@@ -1740,7 +1783,7 @@ void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1740 memmove_extent_buffer(leaf, ptr, ptr + size, 1783 memmove_extent_buffer(leaf, ptr, ptr + size,
1741 end - ptr - size); 1784 end - ptr - size);
1742 item_size -= size; 1785 item_size -= size;
1743 btrfs_truncate_item(trans, root, path, item_size, 1); 1786 btrfs_truncate_item(root, path, item_size, 1);
1744 } 1787 }
1745 btrfs_mark_buffer_dirty(leaf); 1788 btrfs_mark_buffer_dirty(leaf);
1746} 1789}
@@ -1762,10 +1805,10 @@ int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1762 root_objectid, owner, offset, 1); 1805 root_objectid, owner, offset, 1);
1763 if (ret == 0) { 1806 if (ret == 0) {
1764 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); 1807 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1765 update_inline_extent_backref(trans, root, path, iref, 1808 update_inline_extent_backref(root, path, iref,
1766 refs_to_add, extent_op); 1809 refs_to_add, extent_op);
1767 } else if (ret == -ENOENT) { 1810 } else if (ret == -ENOENT) {
1768 setup_inline_extent_backref(trans, root, path, iref, parent, 1811 setup_inline_extent_backref(root, path, iref, parent,
1769 root_objectid, owner, offset, 1812 root_objectid, owner, offset,
1770 refs_to_add, extent_op); 1813 refs_to_add, extent_op);
1771 ret = 0; 1814 ret = 0;
@@ -1802,7 +1845,7 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,
1802 1845
1803 BUG_ON(!is_data && refs_to_drop != 1); 1846 BUG_ON(!is_data && refs_to_drop != 1);
1804 if (iref) { 1847 if (iref) {
1805 update_inline_extent_backref(trans, root, path, iref, 1848 update_inline_extent_backref(root, path, iref,
1806 -refs_to_drop, NULL); 1849 -refs_to_drop, NULL);
1807 } else if (is_data) { 1850 } else if (is_data) {
1808 ret = remove_extent_data_ref(trans, root, path, refs_to_drop); 1851 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
@@ -1973,10 +2016,8 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1973 ref_root = ref->root; 2016 ref_root = ref->root;
1974 2017
1975 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { 2018 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1976 if (extent_op) { 2019 if (extent_op)
1977 BUG_ON(extent_op->update_key);
1978 flags |= extent_op->flags_to_set; 2020 flags |= extent_op->flags_to_set;
1979 }
1980 ret = alloc_reserved_file_extent(trans, root, 2021 ret = alloc_reserved_file_extent(trans, root,
1981 parent, ref_root, flags, 2022 parent, ref_root, flags,
1982 ref->objectid, ref->offset, 2023 ref->objectid, ref->offset,
@@ -2029,18 +2070,33 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2029 u32 item_size; 2070 u32 item_size;
2030 int ret; 2071 int ret;
2031 int err = 0; 2072 int err = 0;
2073 int metadata = (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2074 node->type == BTRFS_SHARED_BLOCK_REF_KEY);
2032 2075
2033 if (trans->aborted) 2076 if (trans->aborted)
2034 return 0; 2077 return 0;
2035 2078
2079 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2080 metadata = 0;
2081
2036 path = btrfs_alloc_path(); 2082 path = btrfs_alloc_path();
2037 if (!path) 2083 if (!path)
2038 return -ENOMEM; 2084 return -ENOMEM;
2039 2085
2040 key.objectid = node->bytenr; 2086 key.objectid = node->bytenr;
2041 key.type = BTRFS_EXTENT_ITEM_KEY;
2042 key.offset = node->num_bytes;
2043 2087
2088 if (metadata) {
2089 struct btrfs_delayed_tree_ref *tree_ref;
2090
2091 tree_ref = btrfs_delayed_node_to_tree_ref(node);
2092 key.type = BTRFS_METADATA_ITEM_KEY;
2093 key.offset = tree_ref->level;
2094 } else {
2095 key.type = BTRFS_EXTENT_ITEM_KEY;
2096 key.offset = node->num_bytes;
2097 }
2098
2099again:
2044 path->reada = 1; 2100 path->reada = 1;
2045 path->leave_spinning = 1; 2101 path->leave_spinning = 1;
2046 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, 2102 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
@@ -2050,6 +2106,14 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2050 goto out; 2106 goto out;
2051 } 2107 }
2052 if (ret > 0) { 2108 if (ret > 0) {
2109 if (metadata) {
2110 btrfs_release_path(path);
2111 metadata = 0;
2112
2113 key.offset = node->num_bytes;
2114 key.type = BTRFS_EXTENT_ITEM_KEY;
2115 goto again;
2116 }
2053 err = -EIO; 2117 err = -EIO;
2054 goto out; 2118 goto out;
2055 } 2119 }
@@ -2089,10 +2153,8 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2089 struct btrfs_key ins; 2153 struct btrfs_key ins;
2090 u64 parent = 0; 2154 u64 parent = 0;
2091 u64 ref_root = 0; 2155 u64 ref_root = 0;
2092 2156 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2093 ins.objectid = node->bytenr; 2157 SKINNY_METADATA);
2094 ins.offset = node->num_bytes;
2095 ins.type = BTRFS_EXTENT_ITEM_KEY;
2096 2158
2097 ref = btrfs_delayed_node_to_tree_ref(node); 2159 ref = btrfs_delayed_node_to_tree_ref(node);
2098 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) 2160 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
@@ -2100,10 +2162,18 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2100 else 2162 else
2101 ref_root = ref->root; 2163 ref_root = ref->root;
2102 2164
2165 ins.objectid = node->bytenr;
2166 if (skinny_metadata) {
2167 ins.offset = ref->level;
2168 ins.type = BTRFS_METADATA_ITEM_KEY;
2169 } else {
2170 ins.offset = node->num_bytes;
2171 ins.type = BTRFS_EXTENT_ITEM_KEY;
2172 }
2173
2103 BUG_ON(node->ref_mod != 1); 2174 BUG_ON(node->ref_mod != 1);
2104 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { 2175 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2105 BUG_ON(!extent_op || !extent_op->update_flags || 2176 BUG_ON(!extent_op || !extent_op->update_flags);
2106 !extent_op->update_key);
2107 ret = alloc_reserved_tree_block(trans, root, 2177 ret = alloc_reserved_tree_block(trans, root,
2108 parent, ref_root, 2178 parent, ref_root,
2109 extent_op->flags_to_set, 2179 extent_op->flags_to_set,
@@ -2307,9 +2377,7 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2307 btrfs_free_delayed_extent_op(extent_op); 2377 btrfs_free_delayed_extent_op(extent_op);
2308 2378
2309 if (ret) { 2379 if (ret) {
2310 printk(KERN_DEBUG 2380 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2311 "btrfs: run_delayed_extent_op "
2312 "returned %d\n", ret);
2313 spin_lock(&delayed_refs->lock); 2381 spin_lock(&delayed_refs->lock);
2314 btrfs_delayed_ref_unlock(locked_ref); 2382 btrfs_delayed_ref_unlock(locked_ref);
2315 return ret; 2383 return ret;
@@ -2348,8 +2416,7 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2348 if (ret) { 2416 if (ret) {
2349 btrfs_delayed_ref_unlock(locked_ref); 2417 btrfs_delayed_ref_unlock(locked_ref);
2350 btrfs_put_delayed_ref(ref); 2418 btrfs_put_delayed_ref(ref);
2351 printk(KERN_DEBUG 2419 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
2352 "btrfs: run_one_delayed_ref returned %d\n", ret);
2353 spin_lock(&delayed_refs->lock); 2420 spin_lock(&delayed_refs->lock);
2354 return ret; 2421 return ret;
2355 } 2422 }
@@ -2426,9 +2493,11 @@ int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2426 if (list_empty(&trans->qgroup_ref_list) != 2493 if (list_empty(&trans->qgroup_ref_list) !=
2427 !trans->delayed_ref_elem.seq) { 2494 !trans->delayed_ref_elem.seq) {
2428 /* list without seq or seq without list */ 2495 /* list without seq or seq without list */
2429 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n", 2496 btrfs_err(fs_info,
2497 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
2430 list_empty(&trans->qgroup_ref_list) ? "" : " not", 2498 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2431 trans->delayed_ref_elem.seq); 2499 (u32)(trans->delayed_ref_elem.seq >> 32),
2500 (u32)trans->delayed_ref_elem.seq);
2432 BUG(); 2501 BUG();
2433 } 2502 }
2434 2503
@@ -3337,7 +3406,7 @@ static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3337 * progress (either running or paused) picks the target profile (if it's 3406 * progress (either running or paused) picks the target profile (if it's
3338 * already available), otherwise falls back to plain reducing. 3407 * already available), otherwise falls back to plain reducing.
3339 */ 3408 */
3340u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) 3409static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3341{ 3410{
3342 /* 3411 /*
3343 * we add in the count of missing devices because we want 3412 * we add in the count of missing devices because we want
@@ -3557,6 +3626,11 @@ static void force_metadata_allocation(struct btrfs_fs_info *info)
3557 rcu_read_unlock(); 3626 rcu_read_unlock();
3558} 3627}
3559 3628
3629static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3630{
3631 return (global->size << 1);
3632}
3633
3560static int should_alloc_chunk(struct btrfs_root *root, 3634static int should_alloc_chunk(struct btrfs_root *root,
3561 struct btrfs_space_info *sinfo, int force) 3635 struct btrfs_space_info *sinfo, int force)
3562{ 3636{
@@ -3574,7 +3648,7 @@ static int should_alloc_chunk(struct btrfs_root *root,
3574 * global_rsv, it doesn't change except when the transaction commits. 3648 * global_rsv, it doesn't change except when the transaction commits.
3575 */ 3649 */
3576 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) 3650 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3577 num_allocated += global_rsv->size; 3651 num_allocated += calc_global_rsv_need_space(global_rsv);
3578 3652
3579 /* 3653 /*
3580 * in limited mode, we want to have some free space up to 3654 * in limited mode, we want to have some free space up to
@@ -3627,8 +3701,8 @@ static void check_system_chunk(struct btrfs_trans_handle *trans,
3627 3701
3628 thresh = get_system_chunk_thresh(root, type); 3702 thresh = get_system_chunk_thresh(root, type);
3629 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) { 3703 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3630 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n", 3704 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3631 left, thresh, type); 3705 left, thresh, type);
3632 dump_space_info(info, 0, 0); 3706 dump_space_info(info, 0, 0);
3633 } 3707 }
3634 3708
@@ -3746,7 +3820,7 @@ static int can_overcommit(struct btrfs_root *root,
3746{ 3820{
3747 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; 3821 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3748 u64 profile = btrfs_get_alloc_profile(root, 0); 3822 u64 profile = btrfs_get_alloc_profile(root, 0);
3749 u64 rsv_size = 0; 3823 u64 space_size;
3750 u64 avail; 3824 u64 avail;
3751 u64 used; 3825 u64 used;
3752 u64 to_add; 3826 u64 to_add;
@@ -3754,18 +3828,16 @@ static int can_overcommit(struct btrfs_root *root,
3754 used = space_info->bytes_used + space_info->bytes_reserved + 3828 used = space_info->bytes_used + space_info->bytes_reserved +
3755 space_info->bytes_pinned + space_info->bytes_readonly; 3829 space_info->bytes_pinned + space_info->bytes_readonly;
3756 3830
3757 spin_lock(&global_rsv->lock);
3758 rsv_size = global_rsv->size;
3759 spin_unlock(&global_rsv->lock);
3760
3761 /* 3831 /*
3762 * We only want to allow over committing if we have lots of actual space 3832 * We only want to allow over committing if we have lots of actual space
3763 * free, but if we don't have enough space to handle the global reserve 3833 * free, but if we don't have enough space to handle the global reserve
3764 * space then we could end up having a real enospc problem when trying 3834 * space then we could end up having a real enospc problem when trying
3765 * to allocate a chunk or some other such important allocation. 3835 * to allocate a chunk or some other such important allocation.
3766 */ 3836 */
3767 rsv_size <<= 1; 3837 spin_lock(&global_rsv->lock);
3768 if (used + rsv_size >= space_info->total_bytes) 3838 space_size = calc_global_rsv_need_space(global_rsv);
3839 spin_unlock(&global_rsv->lock);
3840 if (used + space_size >= space_info->total_bytes)
3769 return 0; 3841 return 0;
3770 3842
3771 used += space_info->bytes_may_use; 3843 used += space_info->bytes_may_use;
@@ -3808,8 +3880,8 @@ static int can_overcommit(struct btrfs_root *root,
3808 return 0; 3880 return 0;
3809} 3881}
3810 3882
3811void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, 3883static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3812 unsigned long nr_pages) 3884 unsigned long nr_pages)
3813{ 3885{
3814 struct super_block *sb = root->fs_info->sb; 3886 struct super_block *sb = root->fs_info->sb;
3815 int started; 3887 int started;
@@ -3826,7 +3898,8 @@ void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3826 * the disk). 3898 * the disk).
3827 */ 3899 */
3828 btrfs_start_delalloc_inodes(root, 0); 3900 btrfs_start_delalloc_inodes(root, 0);
3829 btrfs_wait_ordered_extents(root, 0); 3901 if (!current->journal_info)
3902 btrfs_wait_ordered_extents(root, 0);
3830 } 3903 }
3831} 3904}
3832 3905
@@ -5090,9 +5163,11 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
5090 u64 bytenr, u64 num_bytes) 5163 u64 bytenr, u64 num_bytes)
5091{ 5164{
5092 struct btrfs_block_group_cache *cache; 5165 struct btrfs_block_group_cache *cache;
5166 int ret;
5093 5167
5094 cache = btrfs_lookup_block_group(root->fs_info, bytenr); 5168 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
5095 BUG_ON(!cache); /* Logic error */ 5169 if (!cache)
5170 return -EINVAL;
5096 5171
5097 /* 5172 /*
5098 * pull in the free space cache (if any) so that our pin 5173 * pull in the free space cache (if any) so that our pin
@@ -5105,9 +5180,9 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
5105 pin_down_extent(root, cache, bytenr, num_bytes, 0); 5180 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5106 5181
5107 /* remove us from the free space cache (if we're there at all) */ 5182 /* remove us from the free space cache (if we're there at all) */
5108 btrfs_remove_free_space(cache, bytenr, num_bytes); 5183 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
5109 btrfs_put_block_group(cache); 5184 btrfs_put_block_group(cache);
5110 return 0; 5185 return ret;
5111} 5186}
5112 5187
5113/** 5188/**
@@ -5312,6 +5387,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5312 int num_to_del = 1; 5387 int num_to_del = 1;
5313 u32 item_size; 5388 u32 item_size;
5314 u64 refs; 5389 u64 refs;
5390 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5391 SKINNY_METADATA);
5315 5392
5316 path = btrfs_alloc_path(); 5393 path = btrfs_alloc_path();
5317 if (!path) 5394 if (!path)
@@ -5323,6 +5400,9 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5323 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; 5400 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5324 BUG_ON(!is_data && refs_to_drop != 1); 5401 BUG_ON(!is_data && refs_to_drop != 1);
5325 5402
5403 if (is_data)
5404 skinny_metadata = 0;
5405
5326 ret = lookup_extent_backref(trans, extent_root, path, &iref, 5406 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5327 bytenr, num_bytes, parent, 5407 bytenr, num_bytes, parent,
5328 root_objectid, owner_objectid, 5408 root_objectid, owner_objectid,
@@ -5339,6 +5419,11 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5339 found_extent = 1; 5419 found_extent = 1;
5340 break; 5420 break;
5341 } 5421 }
5422 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5423 key.offset == owner_objectid) {
5424 found_extent = 1;
5425 break;
5426 }
5342 if (path->slots[0] - extent_slot > 5) 5427 if (path->slots[0] - extent_slot > 5)
5343 break; 5428 break;
5344 extent_slot--; 5429 extent_slot--;
@@ -5364,12 +5449,39 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5364 key.type = BTRFS_EXTENT_ITEM_KEY; 5449 key.type = BTRFS_EXTENT_ITEM_KEY;
5365 key.offset = num_bytes; 5450 key.offset = num_bytes;
5366 5451
5452 if (!is_data && skinny_metadata) {
5453 key.type = BTRFS_METADATA_ITEM_KEY;
5454 key.offset = owner_objectid;
5455 }
5456
5367 ret = btrfs_search_slot(trans, extent_root, 5457 ret = btrfs_search_slot(trans, extent_root,
5368 &key, path, -1, 1); 5458 &key, path, -1, 1);
5459 if (ret > 0 && skinny_metadata && path->slots[0]) {
5460 /*
5461 * Couldn't find our skinny metadata item,
5462 * see if we have ye olde extent item.
5463 */
5464 path->slots[0]--;
5465 btrfs_item_key_to_cpu(path->nodes[0], &key,
5466 path->slots[0]);
5467 if (key.objectid == bytenr &&
5468 key.type == BTRFS_EXTENT_ITEM_KEY &&
5469 key.offset == num_bytes)
5470 ret = 0;
5471 }
5472
5473 if (ret > 0 && skinny_metadata) {
5474 skinny_metadata = false;
5475 key.type = BTRFS_EXTENT_ITEM_KEY;
5476 key.offset = num_bytes;
5477 btrfs_release_path(path);
5478 ret = btrfs_search_slot(trans, extent_root,
5479 &key, path, -1, 1);
5480 }
5481
5369 if (ret) { 5482 if (ret) {
5370 printk(KERN_ERR "umm, got %d back from search" 5483 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5371 ", was looking for %llu\n", ret, 5484 ret, (unsigned long long)bytenr);
5372 (unsigned long long)bytenr);
5373 if (ret > 0) 5485 if (ret > 0)
5374 btrfs_print_leaf(extent_root, 5486 btrfs_print_leaf(extent_root,
5375 path->nodes[0]); 5487 path->nodes[0]);
@@ -5383,13 +5495,13 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5383 } else if (ret == -ENOENT) { 5495 } else if (ret == -ENOENT) {
5384 btrfs_print_leaf(extent_root, path->nodes[0]); 5496 btrfs_print_leaf(extent_root, path->nodes[0]);
5385 WARN_ON(1); 5497 WARN_ON(1);
5386 printk(KERN_ERR "btrfs unable to find ref byte nr %llu " 5498 btrfs_err(info,
5387 "parent %llu root %llu owner %llu offset %llu\n", 5499 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5388 (unsigned long long)bytenr, 5500 (unsigned long long)bytenr,
5389 (unsigned long long)parent, 5501 (unsigned long long)parent,
5390 (unsigned long long)root_objectid, 5502 (unsigned long long)root_objectid,
5391 (unsigned long long)owner_objectid, 5503 (unsigned long long)owner_objectid,
5392 (unsigned long long)owner_offset); 5504 (unsigned long long)owner_offset);
5393 } else { 5505 } else {
5394 btrfs_abort_transaction(trans, extent_root, ret); 5506 btrfs_abort_transaction(trans, extent_root, ret);
5395 goto out; 5507 goto out;
@@ -5417,9 +5529,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5417 ret = btrfs_search_slot(trans, extent_root, &key, path, 5529 ret = btrfs_search_slot(trans, extent_root, &key, path,
5418 -1, 1); 5530 -1, 1);
5419 if (ret) { 5531 if (ret) {
5420 printk(KERN_ERR "umm, got %d back from search" 5532 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5421 ", was looking for %llu\n", ret, 5533 ret, (unsigned long long)bytenr);
5422 (unsigned long long)bytenr);
5423 btrfs_print_leaf(extent_root, path->nodes[0]); 5534 btrfs_print_leaf(extent_root, path->nodes[0]);
5424 } 5535 }
5425 if (ret < 0) { 5536 if (ret < 0) {
@@ -5435,7 +5546,8 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5435 BUG_ON(item_size < sizeof(*ei)); 5546 BUG_ON(item_size < sizeof(*ei));
5436 ei = btrfs_item_ptr(leaf, extent_slot, 5547 ei = btrfs_item_ptr(leaf, extent_slot,
5437 struct btrfs_extent_item); 5548 struct btrfs_extent_item);
5438 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { 5549 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5550 key.type == BTRFS_EXTENT_ITEM_KEY) {
5439 struct btrfs_tree_block_info *bi; 5551 struct btrfs_tree_block_info *bi;
5440 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); 5552 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5441 bi = (struct btrfs_tree_block_info *)(ei + 1); 5553 bi = (struct btrfs_tree_block_info *)(ei + 1);
@@ -5443,7 +5555,13 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5443 } 5555 }
5444 5556
5445 refs = btrfs_extent_refs(leaf, ei); 5557 refs = btrfs_extent_refs(leaf, ei);
5446 BUG_ON(refs < refs_to_drop); 5558 if (refs < refs_to_drop) {
5559 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5560 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5561 ret = -EINVAL;
5562 btrfs_abort_transaction(trans, extent_root, ret);
5563 goto out;
5564 }
5447 refs -= refs_to_drop; 5565 refs -= refs_to_drop;
5448 5566
5449 if (refs > 0) { 5567 if (refs > 0) {
@@ -5758,7 +5876,7 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5758 struct btrfs_root *orig_root, 5876 struct btrfs_root *orig_root,
5759 u64 num_bytes, u64 empty_size, 5877 u64 num_bytes, u64 empty_size,
5760 u64 hint_byte, struct btrfs_key *ins, 5878 u64 hint_byte, struct btrfs_key *ins,
5761 u64 data) 5879 u64 flags)
5762{ 5880{
5763 int ret = 0; 5881 int ret = 0;
5764 struct btrfs_root *root = orig_root->fs_info->extent_root; 5882 struct btrfs_root *root = orig_root->fs_info->extent_root;
@@ -5769,8 +5887,8 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5769 int empty_cluster = 2 * 1024 * 1024; 5887 int empty_cluster = 2 * 1024 * 1024;
5770 struct btrfs_space_info *space_info; 5888 struct btrfs_space_info *space_info;
5771 int loop = 0; 5889 int loop = 0;
5772 int index = __get_raid_index(data); 5890 int index = __get_raid_index(flags);
5773 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? 5891 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
5774 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; 5892 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
5775 bool found_uncached_bg = false; 5893 bool found_uncached_bg = false;
5776 bool failed_cluster_refill = false; 5894 bool failed_cluster_refill = false;
@@ -5783,11 +5901,11 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5783 ins->objectid = 0; 5901 ins->objectid = 0;
5784 ins->offset = 0; 5902 ins->offset = 0;
5785 5903
5786 trace_find_free_extent(orig_root, num_bytes, empty_size, data); 5904 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
5787 5905
5788 space_info = __find_space_info(root->fs_info, data); 5906 space_info = __find_space_info(root->fs_info, flags);
5789 if (!space_info) { 5907 if (!space_info) {
5790 printk(KERN_ERR "No space info for %llu\n", data); 5908 btrfs_err(root->fs_info, "No space info for %llu", flags);
5791 return -ENOSPC; 5909 return -ENOSPC;
5792 } 5910 }
5793 5911
@@ -5798,13 +5916,13 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5798 if (btrfs_mixed_space_info(space_info)) 5916 if (btrfs_mixed_space_info(space_info))
5799 use_cluster = false; 5917 use_cluster = false;
5800 5918
5801 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { 5919 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
5802 last_ptr = &root->fs_info->meta_alloc_cluster; 5920 last_ptr = &root->fs_info->meta_alloc_cluster;
5803 if (!btrfs_test_opt(root, SSD)) 5921 if (!btrfs_test_opt(root, SSD))
5804 empty_cluster = 64 * 1024; 5922 empty_cluster = 64 * 1024;
5805 } 5923 }
5806 5924
5807 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && 5925 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5808 btrfs_test_opt(root, SSD)) { 5926 btrfs_test_opt(root, SSD)) {
5809 last_ptr = &root->fs_info->data_alloc_cluster; 5927 last_ptr = &root->fs_info->data_alloc_cluster;
5810 } 5928 }
@@ -5833,7 +5951,7 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5833 * However if we are re-searching with an ideal block group 5951 * However if we are re-searching with an ideal block group
5834 * picked out then we don't care that the block group is cached. 5952 * picked out then we don't care that the block group is cached.
5835 */ 5953 */
5836 if (block_group && block_group_bits(block_group, data) && 5954 if (block_group && block_group_bits(block_group, flags) &&
5837 block_group->cached != BTRFS_CACHE_NO) { 5955 block_group->cached != BTRFS_CACHE_NO) {
5838 down_read(&space_info->groups_sem); 5956 down_read(&space_info->groups_sem);
5839 if (list_empty(&block_group->list) || 5957 if (list_empty(&block_group->list) ||
@@ -5871,7 +5989,7 @@ search:
5871 * raid types, but we want to make sure we only allocate 5989 * raid types, but we want to make sure we only allocate
5872 * for the proper type. 5990 * for the proper type.
5873 */ 5991 */
5874 if (!block_group_bits(block_group, data)) { 5992 if (!block_group_bits(block_group, flags)) {
5875 u64 extra = BTRFS_BLOCK_GROUP_DUP | 5993 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5876 BTRFS_BLOCK_GROUP_RAID1 | 5994 BTRFS_BLOCK_GROUP_RAID1 |
5877 BTRFS_BLOCK_GROUP_RAID5 | 5995 BTRFS_BLOCK_GROUP_RAID5 |
@@ -5883,7 +6001,7 @@ search:
5883 * doesn't provide them, bail. This does allow us to 6001 * doesn't provide them, bail. This does allow us to
5884 * fill raid0 from raid1. 6002 * fill raid0 from raid1.
5885 */ 6003 */
5886 if ((data & extra) && !(block_group->flags & extra)) 6004 if ((flags & extra) && !(block_group->flags & extra))
5887 goto loop; 6005 goto loop;
5888 } 6006 }
5889 6007
@@ -5914,7 +6032,7 @@ have_block_group:
5914 if (used_block_group != block_group && 6032 if (used_block_group != block_group &&
5915 (!used_block_group || 6033 (!used_block_group ||
5916 used_block_group->ro || 6034 used_block_group->ro ||
5917 !block_group_bits(used_block_group, data))) { 6035 !block_group_bits(used_block_group, flags))) {
5918 used_block_group = block_group; 6036 used_block_group = block_group;
5919 goto refill_cluster; 6037 goto refill_cluster;
5920 } 6038 }
@@ -6110,7 +6228,7 @@ loop:
6110 index = 0; 6228 index = 0;
6111 loop++; 6229 loop++;
6112 if (loop == LOOP_ALLOC_CHUNK) { 6230 if (loop == LOOP_ALLOC_CHUNK) {
6113 ret = do_chunk_alloc(trans, root, data, 6231 ret = do_chunk_alloc(trans, root, flags,
6114 CHUNK_ALLOC_FORCE); 6232 CHUNK_ALLOC_FORCE);
6115 /* 6233 /*
6116 * Do not bail out on ENOSPC since we 6234 * Do not bail out on ENOSPC since we
@@ -6188,16 +6306,17 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6188 struct btrfs_root *root, 6306 struct btrfs_root *root,
6189 u64 num_bytes, u64 min_alloc_size, 6307 u64 num_bytes, u64 min_alloc_size,
6190 u64 empty_size, u64 hint_byte, 6308 u64 empty_size, u64 hint_byte,
6191 struct btrfs_key *ins, u64 data) 6309 struct btrfs_key *ins, int is_data)
6192{ 6310{
6193 bool final_tried = false; 6311 bool final_tried = false;
6312 u64 flags;
6194 int ret; 6313 int ret;
6195 6314
6196 data = btrfs_get_alloc_profile(root, data); 6315 flags = btrfs_get_alloc_profile(root, is_data);
6197again: 6316again:
6198 WARN_ON(num_bytes < root->sectorsize); 6317 WARN_ON(num_bytes < root->sectorsize);
6199 ret = find_free_extent(trans, root, num_bytes, empty_size, 6318 ret = find_free_extent(trans, root, num_bytes, empty_size,
6200 hint_byte, ins, data); 6319 hint_byte, ins, flags);
6201 6320
6202 if (ret == -ENOSPC) { 6321 if (ret == -ENOSPC) {
6203 if (!final_tried) { 6322 if (!final_tried) {
@@ -6210,10 +6329,10 @@ again:
6210 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) { 6329 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6211 struct btrfs_space_info *sinfo; 6330 struct btrfs_space_info *sinfo;
6212 6331
6213 sinfo = __find_space_info(root->fs_info, data); 6332 sinfo = __find_space_info(root->fs_info, flags);
6214 printk(KERN_ERR "btrfs allocation failed flags %llu, " 6333 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
6215 "wanted %llu\n", (unsigned long long)data, 6334 (unsigned long long)flags,
6216 (unsigned long long)num_bytes); 6335 (unsigned long long)num_bytes);
6217 if (sinfo) 6336 if (sinfo)
6218 dump_space_info(sinfo, num_bytes, 1); 6337 dump_space_info(sinfo, num_bytes, 1);
6219 } 6338 }
@@ -6232,8 +6351,8 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6232 6351
6233 cache = btrfs_lookup_block_group(root->fs_info, start); 6352 cache = btrfs_lookup_block_group(root->fs_info, start);
6234 if (!cache) { 6353 if (!cache) {
6235 printk(KERN_ERR "Unable to find block group for %llu\n", 6354 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6236 (unsigned long long)start); 6355 (unsigned long long)start);
6237 return -ENOSPC; 6356 return -ENOSPC;
6238 } 6357 }
6239 6358
@@ -6328,9 +6447,9 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6328 6447
6329 ret = update_block_group(root, ins->objectid, ins->offset, 1); 6448 ret = update_block_group(root, ins->objectid, ins->offset, 1);
6330 if (ret) { /* -ENOENT, logic error */ 6449 if (ret) { /* -ENOENT, logic error */
6331 printk(KERN_ERR "btrfs update block group failed for %llu " 6450 btrfs_err(fs_info, "update block group failed for %llu %llu",
6332 "%llu\n", (unsigned long long)ins->objectid, 6451 (unsigned long long)ins->objectid,
6333 (unsigned long long)ins->offset); 6452 (unsigned long long)ins->offset);
6334 BUG(); 6453 BUG();
6335 } 6454 }
6336 return ret; 6455 return ret;
@@ -6349,7 +6468,12 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6349 struct btrfs_extent_inline_ref *iref; 6468 struct btrfs_extent_inline_ref *iref;
6350 struct btrfs_path *path; 6469 struct btrfs_path *path;
6351 struct extent_buffer *leaf; 6470 struct extent_buffer *leaf;
6352 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); 6471 u32 size = sizeof(*extent_item) + sizeof(*iref);
6472 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6473 SKINNY_METADATA);
6474
6475 if (!skinny_metadata)
6476 size += sizeof(*block_info);
6353 6477
6354 path = btrfs_alloc_path(); 6478 path = btrfs_alloc_path();
6355 if (!path) 6479 if (!path)
@@ -6370,12 +6494,16 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6370 btrfs_set_extent_generation(leaf, extent_item, trans->transid); 6494 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6371 btrfs_set_extent_flags(leaf, extent_item, 6495 btrfs_set_extent_flags(leaf, extent_item,
6372 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); 6496 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
6373 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6374 6497
6375 btrfs_set_tree_block_key(leaf, block_info, key); 6498 if (skinny_metadata) {
6376 btrfs_set_tree_block_level(leaf, block_info, level); 6499 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6500 } else {
6501 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6502 btrfs_set_tree_block_key(leaf, block_info, key);
6503 btrfs_set_tree_block_level(leaf, block_info, level);
6504 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6505 }
6377 6506
6378 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6379 if (parent > 0) { 6507 if (parent > 0) {
6380 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); 6508 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6381 btrfs_set_extent_inline_ref_type(leaf, iref, 6509 btrfs_set_extent_inline_ref_type(leaf, iref,
@@ -6390,11 +6518,11 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6390 btrfs_mark_buffer_dirty(leaf); 6518 btrfs_mark_buffer_dirty(leaf);
6391 btrfs_free_path(path); 6519 btrfs_free_path(path);
6392 6520
6393 ret = update_block_group(root, ins->objectid, ins->offset, 1); 6521 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
6394 if (ret) { /* -ENOENT, logic error */ 6522 if (ret) { /* -ENOENT, logic error */
6395 printk(KERN_ERR "btrfs update block group failed for %llu " 6523 btrfs_err(fs_info, "update block group failed for %llu %llu",
6396 "%llu\n", (unsigned long long)ins->objectid, 6524 (unsigned long long)ins->objectid,
6397 (unsigned long long)ins->offset); 6525 (unsigned long long)ins->offset);
6398 BUG(); 6526 BUG();
6399 } 6527 }
6400 return ret; 6528 return ret;
@@ -6439,47 +6567,48 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6439 if (!caching_ctl) { 6567 if (!caching_ctl) {
6440 BUG_ON(!block_group_cache_done(block_group)); 6568 BUG_ON(!block_group_cache_done(block_group));
6441 ret = btrfs_remove_free_space(block_group, start, num_bytes); 6569 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6442 BUG_ON(ret); /* -ENOMEM */ 6570 if (ret)
6571 goto out;
6443 } else { 6572 } else {
6444 mutex_lock(&caching_ctl->mutex); 6573 mutex_lock(&caching_ctl->mutex);
6445 6574
6446 if (start >= caching_ctl->progress) { 6575 if (start >= caching_ctl->progress) {
6447 ret = add_excluded_extent(root, start, num_bytes); 6576 ret = add_excluded_extent(root, start, num_bytes);
6448 BUG_ON(ret); /* -ENOMEM */
6449 } else if (start + num_bytes <= caching_ctl->progress) { 6577 } else if (start + num_bytes <= caching_ctl->progress) {
6450 ret = btrfs_remove_free_space(block_group, 6578 ret = btrfs_remove_free_space(block_group,
6451 start, num_bytes); 6579 start, num_bytes);
6452 BUG_ON(ret); /* -ENOMEM */
6453 } else { 6580 } else {
6454 num_bytes = caching_ctl->progress - start; 6581 num_bytes = caching_ctl->progress - start;
6455 ret = btrfs_remove_free_space(block_group, 6582 ret = btrfs_remove_free_space(block_group,
6456 start, num_bytes); 6583 start, num_bytes);
6457 BUG_ON(ret); /* -ENOMEM */ 6584 if (ret)
6585 goto out_lock;
6458 6586
6459 start = caching_ctl->progress; 6587 start = caching_ctl->progress;
6460 num_bytes = ins->objectid + ins->offset - 6588 num_bytes = ins->objectid + ins->offset -
6461 caching_ctl->progress; 6589 caching_ctl->progress;
6462 ret = add_excluded_extent(root, start, num_bytes); 6590 ret = add_excluded_extent(root, start, num_bytes);
6463 BUG_ON(ret); /* -ENOMEM */
6464 } 6591 }
6465 6592out_lock:
6466 mutex_unlock(&caching_ctl->mutex); 6593 mutex_unlock(&caching_ctl->mutex);
6467 put_caching_control(caching_ctl); 6594 put_caching_control(caching_ctl);
6595 if (ret)
6596 goto out;
6468 } 6597 }
6469 6598
6470 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 6599 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6471 RESERVE_ALLOC_NO_ACCOUNT); 6600 RESERVE_ALLOC_NO_ACCOUNT);
6472 BUG_ON(ret); /* logic error */ 6601 BUG_ON(ret); /* logic error */
6473 btrfs_put_block_group(block_group);
6474 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 6602 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6475 0, owner, offset, ins, 1); 6603 0, owner, offset, ins, 1);
6604out:
6605 btrfs_put_block_group(block_group);
6476 return ret; 6606 return ret;
6477} 6607}
6478 6608
6479struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, 6609static struct extent_buffer *
6480 struct btrfs_root *root, 6610btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6481 u64 bytenr, u32 blocksize, 6611 u64 bytenr, u32 blocksize, int level)
6482 int level)
6483{ 6612{
6484 struct extent_buffer *buf; 6613 struct extent_buffer *buf;
6485 6614
@@ -6594,7 +6723,8 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6594 struct extent_buffer *buf; 6723 struct extent_buffer *buf;
6595 u64 flags = 0; 6724 u64 flags = 0;
6596 int ret; 6725 int ret;
6597 6726 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6727 SKINNY_METADATA);
6598 6728
6599 block_rsv = use_block_rsv(trans, root, blocksize); 6729 block_rsv = use_block_rsv(trans, root, blocksize);
6600 if (IS_ERR(block_rsv)) 6730 if (IS_ERR(block_rsv))
@@ -6627,7 +6757,10 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6627 else 6757 else
6628 memset(&extent_op->key, 0, sizeof(extent_op->key)); 6758 memset(&extent_op->key, 0, sizeof(extent_op->key));
6629 extent_op->flags_to_set = flags; 6759 extent_op->flags_to_set = flags;
6630 extent_op->update_key = 1; 6760 if (skinny_metadata)
6761 extent_op->update_key = 0;
6762 else
6763 extent_op->update_key = 1;
6631 extent_op->update_flags = 1; 6764 extent_op->update_flags = 1;
6632 extent_op->is_data = 0; 6765 extent_op->is_data = 0;
6633 6766
@@ -6704,8 +6837,9 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6704 continue; 6837 continue;
6705 6838
6706 /* We don't lock the tree block, it's OK to be racy here */ 6839 /* We don't lock the tree block, it's OK to be racy here */
6707 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, 6840 ret = btrfs_lookup_extent_info(trans, root, bytenr,
6708 &refs, &flags); 6841 wc->level - 1, 1, &refs,
6842 &flags);
6709 /* We don't care about errors in readahead. */ 6843 /* We don't care about errors in readahead. */
6710 if (ret < 0) 6844 if (ret < 0)
6711 continue; 6845 continue;
@@ -6772,7 +6906,7 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6772 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { 6906 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
6773 BUG_ON(!path->locks[level]); 6907 BUG_ON(!path->locks[level]);
6774 ret = btrfs_lookup_extent_info(trans, root, 6908 ret = btrfs_lookup_extent_info(trans, root,
6775 eb->start, eb->len, 6909 eb->start, level, 1,
6776 &wc->refs[level], 6910 &wc->refs[level],
6777 &wc->flags[level]); 6911 &wc->flags[level]);
6778 BUG_ON(ret == -ENOMEM); 6912 BUG_ON(ret == -ENOMEM);
@@ -6870,7 +7004,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6870 btrfs_tree_lock(next); 7004 btrfs_tree_lock(next);
6871 btrfs_set_lock_blocking(next); 7005 btrfs_set_lock_blocking(next);
6872 7006
6873 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, 7007 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
6874 &wc->refs[level - 1], 7008 &wc->refs[level - 1],
6875 &wc->flags[level - 1]); 7009 &wc->flags[level - 1]);
6876 if (ret < 0) { 7010 if (ret < 0) {
@@ -6878,7 +7012,10 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6878 return ret; 7012 return ret;
6879 } 7013 }
6880 7014
6881 BUG_ON(wc->refs[level - 1] == 0); 7015 if (unlikely(wc->refs[level - 1] == 0)) {
7016 btrfs_err(root->fs_info, "Missing references.");
7017 BUG();
7018 }
6882 *lookup_info = 0; 7019 *lookup_info = 0;
6883 7020
6884 if (wc->stage == DROP_REFERENCE) { 7021 if (wc->stage == DROP_REFERENCE) {
@@ -6917,8 +7054,10 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6917 if (reada && level == 1) 7054 if (reada && level == 1)
6918 reada_walk_down(trans, root, wc, path); 7055 reada_walk_down(trans, root, wc, path);
6919 next = read_tree_block(root, bytenr, blocksize, generation); 7056 next = read_tree_block(root, bytenr, blocksize, generation);
6920 if (!next) 7057 if (!next || !extent_buffer_uptodate(next)) {
7058 free_extent_buffer(next);
6921 return -EIO; 7059 return -EIO;
7060 }
6922 btrfs_tree_lock(next); 7061 btrfs_tree_lock(next);
6923 btrfs_set_lock_blocking(next); 7062 btrfs_set_lock_blocking(next);
6924 } 7063 }
@@ -7001,7 +7140,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7001 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; 7140 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
7002 7141
7003 ret = btrfs_lookup_extent_info(trans, root, 7142 ret = btrfs_lookup_extent_info(trans, root,
7004 eb->start, eb->len, 7143 eb->start, level, 1,
7005 &wc->refs[level], 7144 &wc->refs[level],
7006 &wc->flags[level]); 7145 &wc->flags[level]);
7007 if (ret < 0) { 7146 if (ret < 0) {
@@ -7137,6 +7276,8 @@ static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
7137 * reference count by one. if update_ref is true, this function 7276 * reference count by one. if update_ref is true, this function
7138 * also make sure backrefs for the shared block and all lower level 7277 * also make sure backrefs for the shared block and all lower level
7139 * blocks are properly updated. 7278 * blocks are properly updated.
7279 *
7280 * If called with for_reloc == 0, may exit early with -EAGAIN
7140 */ 7281 */
7141int btrfs_drop_snapshot(struct btrfs_root *root, 7282int btrfs_drop_snapshot(struct btrfs_root *root,
7142 struct btrfs_block_rsv *block_rsv, int update_ref, 7283 struct btrfs_block_rsv *block_rsv, int update_ref,
@@ -7211,8 +7352,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
7211 7352
7212 ret = btrfs_lookup_extent_info(trans, root, 7353 ret = btrfs_lookup_extent_info(trans, root,
7213 path->nodes[level]->start, 7354 path->nodes[level]->start,
7214 path->nodes[level]->len, 7355 level, 1, &wc->refs[level],
7215 &wc->refs[level],
7216 &wc->flags[level]); 7356 &wc->flags[level]);
7217 if (ret < 0) { 7357 if (ret < 0) {
7218 err = ret; 7358 err = ret;
@@ -7238,6 +7378,12 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
7238 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); 7378 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
7239 7379
7240 while (1) { 7380 while (1) {
7381 if (!for_reloc && btrfs_fs_closing(root->fs_info)) {
7382 pr_debug("btrfs: drop snapshot early exit\n");
7383 err = -EAGAIN;
7384 goto out_end_trans;
7385 }
7386
7241 ret = walk_down_tree(trans, root, path, wc); 7387 ret = walk_down_tree(trans, root, path, wc);
7242 if (ret < 0) { 7388 if (ret < 0) {
7243 err = ret; 7389 err = ret;
@@ -8020,10 +8166,26 @@ int btrfs_read_block_groups(struct btrfs_root *root)
8020 free_excluded_extents(root, cache); 8166 free_excluded_extents(root, cache);
8021 } 8167 }
8022 8168
8169 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8170 if (ret) {
8171 btrfs_remove_free_space_cache(cache);
8172 btrfs_put_block_group(cache);
8173 goto error;
8174 }
8175
8023 ret = update_space_info(info, cache->flags, found_key.offset, 8176 ret = update_space_info(info, cache->flags, found_key.offset,
8024 btrfs_block_group_used(&cache->item), 8177 btrfs_block_group_used(&cache->item),
8025 &space_info); 8178 &space_info);
8026 BUG_ON(ret); /* -ENOMEM */ 8179 if (ret) {
8180 btrfs_remove_free_space_cache(cache);
8181 spin_lock(&info->block_group_cache_lock);
8182 rb_erase(&cache->cache_node,
8183 &info->block_group_cache_tree);
8184 spin_unlock(&info->block_group_cache_lock);
8185 btrfs_put_block_group(cache);
8186 goto error;
8187 }
8188
8027 cache->space_info = space_info; 8189 cache->space_info = space_info;
8028 spin_lock(&cache->space_info->lock); 8190 spin_lock(&cache->space_info->lock);
8029 cache->space_info->bytes_readonly += cache->bytes_super; 8191 cache->space_info->bytes_readonly += cache->bytes_super;
@@ -8031,9 +8193,6 @@ int btrfs_read_block_groups(struct btrfs_root *root)
8031 8193
8032 __link_block_group(space_info, cache); 8194 __link_block_group(space_info, cache);
8033 8195
8034 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8035 BUG_ON(ret); /* Logic error */
8036
8037 set_avail_alloc_bits(root->fs_info, cache->flags); 8196 set_avail_alloc_bits(root->fs_info, cache->flags);
8038 if (btrfs_chunk_readonly(root, cache->key.objectid)) 8197 if (btrfs_chunk_readonly(root, cache->key.objectid))
8039 set_block_group_ro(cache, 1); 8198 set_block_group_ro(cache, 1);
@@ -8156,9 +8315,24 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8156 8315
8157 free_excluded_extents(root, cache); 8316 free_excluded_extents(root, cache);
8158 8317
8318 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8319 if (ret) {
8320 btrfs_remove_free_space_cache(cache);
8321 btrfs_put_block_group(cache);
8322 return ret;
8323 }
8324
8159 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, 8325 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8160 &cache->space_info); 8326 &cache->space_info);
8161 BUG_ON(ret); /* -ENOMEM */ 8327 if (ret) {
8328 btrfs_remove_free_space_cache(cache);
8329 spin_lock(&root->fs_info->block_group_cache_lock);
8330 rb_erase(&cache->cache_node,
8331 &root->fs_info->block_group_cache_tree);
8332 spin_unlock(&root->fs_info->block_group_cache_lock);
8333 btrfs_put_block_group(cache);
8334 return ret;
8335 }
8162 update_global_block_rsv(root->fs_info); 8336 update_global_block_rsv(root->fs_info);
8163 8337
8164 spin_lock(&cache->space_info->lock); 8338 spin_lock(&cache->space_info->lock);
@@ -8167,9 +8341,6 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8167 8341
8168 __link_block_group(cache->space_info, cache); 8342 __link_block_group(cache->space_info, cache);
8169 8343
8170 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8171 BUG_ON(ret); /* Logic error */
8172
8173 list_add_tail(&cache->new_bg_list, &trans->new_bgs); 8344 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
8174 8345
8175 set_avail_alloc_bits(extent_root->fs_info, type); 8346 set_avail_alloc_bits(extent_root->fs_info, type);