diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 1723 |
1 files changed, 38 insertions, 1685 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 810d1f80b497..98ca149bdbc8 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -94,7 +94,7 @@ static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) | |||
94 | return (cache->flags & bits) == bits; | 94 | return (cache->flags & bits) == bits; |
95 | } | 95 | } |
96 | 96 | ||
97 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) | 97 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
98 | { | 98 | { |
99 | atomic_inc(&cache->count); | 99 | atomic_inc(&cache->count); |
100 | } | 100 | } |
@@ -380,7 +380,7 @@ again: | |||
380 | break; | 380 | break; |
381 | 381 | ||
382 | caching_ctl->progress = last; | 382 | caching_ctl->progress = last; |
383 | btrfs_release_path(extent_root, path); | 383 | btrfs_release_path(path); |
384 | up_read(&fs_info->extent_commit_sem); | 384 | up_read(&fs_info->extent_commit_sem); |
385 | mutex_unlock(&caching_ctl->mutex); | 385 | mutex_unlock(&caching_ctl->mutex); |
386 | if (btrfs_transaction_in_commit(fs_info)) | 386 | if (btrfs_transaction_in_commit(fs_info)) |
@@ -755,8 +755,12 @@ again: | |||
755 | atomic_inc(&head->node.refs); | 755 | atomic_inc(&head->node.refs); |
756 | spin_unlock(&delayed_refs->lock); | 756 | spin_unlock(&delayed_refs->lock); |
757 | 757 | ||
758 | btrfs_release_path(root->fs_info->extent_root, path); | 758 | btrfs_release_path(path); |
759 | 759 | ||
760 | /* | ||
761 | * Mutex was contended, block until it's released and try | ||
762 | * again | ||
763 | */ | ||
760 | mutex_lock(&head->mutex); | 764 | mutex_lock(&head->mutex); |
761 | mutex_unlock(&head->mutex); | 765 | mutex_unlock(&head->mutex); |
762 | btrfs_put_delayed_ref(&head->node); | 766 | btrfs_put_delayed_ref(&head->node); |
@@ -935,7 +939,7 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans, | |||
935 | break; | 939 | break; |
936 | } | 940 | } |
937 | } | 941 | } |
938 | btrfs_release_path(root, path); | 942 | btrfs_release_path(path); |
939 | 943 | ||
940 | if (owner < BTRFS_FIRST_FREE_OBJECTID) | 944 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
941 | new_size += sizeof(*bi); | 945 | new_size += sizeof(*bi); |
@@ -1043,7 +1047,7 @@ again: | |||
1043 | return 0; | 1047 | return 0; |
1044 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | 1048 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
1045 | key.type = BTRFS_EXTENT_REF_V0_KEY; | 1049 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
1046 | btrfs_release_path(root, path); | 1050 | btrfs_release_path(path); |
1047 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | 1051 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1048 | if (ret < 0) { | 1052 | if (ret < 0) { |
1049 | err = ret; | 1053 | err = ret; |
@@ -1081,7 +1085,7 @@ again: | |||
1081 | if (match_extent_data_ref(leaf, ref, root_objectid, | 1085 | if (match_extent_data_ref(leaf, ref, root_objectid, |
1082 | owner, offset)) { | 1086 | owner, offset)) { |
1083 | if (recow) { | 1087 | if (recow) { |
1084 | btrfs_release_path(root, path); | 1088 | btrfs_release_path(path); |
1085 | goto again; | 1089 | goto again; |
1086 | } | 1090 | } |
1087 | err = 0; | 1091 | err = 0; |
@@ -1142,7 +1146,7 @@ static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, | |||
1142 | if (match_extent_data_ref(leaf, ref, root_objectid, | 1146 | if (match_extent_data_ref(leaf, ref, root_objectid, |
1143 | owner, offset)) | 1147 | owner, offset)) |
1144 | break; | 1148 | break; |
1145 | btrfs_release_path(root, path); | 1149 | btrfs_release_path(path); |
1146 | key.offset++; | 1150 | key.offset++; |
1147 | ret = btrfs_insert_empty_item(trans, root, path, &key, | 1151 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
1148 | size); | 1152 | size); |
@@ -1168,7 +1172,7 @@ static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, | |||
1168 | btrfs_mark_buffer_dirty(leaf); | 1172 | btrfs_mark_buffer_dirty(leaf); |
1169 | ret = 0; | 1173 | ret = 0; |
1170 | fail: | 1174 | fail: |
1171 | btrfs_release_path(root, path); | 1175 | btrfs_release_path(path); |
1172 | return ret; | 1176 | return ret; |
1173 | } | 1177 | } |
1174 | 1178 | ||
@@ -1294,7 +1298,7 @@ static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, | |||
1294 | ret = -ENOENT; | 1298 | ret = -ENOENT; |
1295 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | 1299 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
1296 | if (ret == -ENOENT && parent) { | 1300 | if (ret == -ENOENT && parent) { |
1297 | btrfs_release_path(root, path); | 1301 | btrfs_release_path(path); |
1298 | key.type = BTRFS_EXTENT_REF_V0_KEY; | 1302 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
1299 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | 1303 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1300 | if (ret > 0) | 1304 | if (ret > 0) |
@@ -1323,7 +1327,7 @@ static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, | |||
1323 | } | 1327 | } |
1324 | 1328 | ||
1325 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | 1329 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
1326 | btrfs_release_path(root, path); | 1330 | btrfs_release_path(path); |
1327 | return ret; | 1331 | return ret; |
1328 | } | 1332 | } |
1329 | 1333 | ||
@@ -1609,7 +1613,7 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans, | |||
1609 | if (ret != -ENOENT) | 1613 | if (ret != -ENOENT) |
1610 | return ret; | 1614 | return ret; |
1611 | 1615 | ||
1612 | btrfs_release_path(root, path); | 1616 | btrfs_release_path(path); |
1613 | *ref_ret = NULL; | 1617 | *ref_ret = NULL; |
1614 | 1618 | ||
1615 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | 1619 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
@@ -1863,7 +1867,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |||
1863 | __run_delayed_extent_op(extent_op, leaf, item); | 1867 | __run_delayed_extent_op(extent_op, leaf, item); |
1864 | 1868 | ||
1865 | btrfs_mark_buffer_dirty(leaf); | 1869 | btrfs_mark_buffer_dirty(leaf); |
1866 | btrfs_release_path(root->fs_info->extent_root, path); | 1870 | btrfs_release_path(path); |
1867 | 1871 | ||
1868 | path->reada = 1; | 1872 | path->reada = 1; |
1869 | path->leave_spinning = 1; | 1873 | path->leave_spinning = 1; |
@@ -2298,6 +2302,10 @@ again: | |||
2298 | atomic_inc(&ref->refs); | 2302 | atomic_inc(&ref->refs); |
2299 | 2303 | ||
2300 | spin_unlock(&delayed_refs->lock); | 2304 | spin_unlock(&delayed_refs->lock); |
2305 | /* | ||
2306 | * Mutex was contended, block until it's | ||
2307 | * released and try again | ||
2308 | */ | ||
2301 | mutex_lock(&head->mutex); | 2309 | mutex_lock(&head->mutex); |
2302 | mutex_unlock(&head->mutex); | 2310 | mutex_unlock(&head->mutex); |
2303 | 2311 | ||
@@ -2362,8 +2370,12 @@ static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, | |||
2362 | atomic_inc(&head->node.refs); | 2370 | atomic_inc(&head->node.refs); |
2363 | spin_unlock(&delayed_refs->lock); | 2371 | spin_unlock(&delayed_refs->lock); |
2364 | 2372 | ||
2365 | btrfs_release_path(root->fs_info->extent_root, path); | 2373 | btrfs_release_path(path); |
2366 | 2374 | ||
2375 | /* | ||
2376 | * Mutex was contended, block until it's released and let | ||
2377 | * caller try again | ||
2378 | */ | ||
2367 | mutex_lock(&head->mutex); | 2379 | mutex_lock(&head->mutex); |
2368 | mutex_unlock(&head->mutex); | 2380 | mutex_unlock(&head->mutex); |
2369 | btrfs_put_delayed_ref(&head->node); | 2381 | btrfs_put_delayed_ref(&head->node); |
@@ -2511,126 +2523,6 @@ out: | |||
2511 | return ret; | 2523 | return ret; |
2512 | } | 2524 | } |
2513 | 2525 | ||
2514 | #if 0 | ||
2515 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | ||
2516 | struct extent_buffer *buf, u32 nr_extents) | ||
2517 | { | ||
2518 | struct btrfs_key key; | ||
2519 | struct btrfs_file_extent_item *fi; | ||
2520 | u64 root_gen; | ||
2521 | u32 nritems; | ||
2522 | int i; | ||
2523 | int level; | ||
2524 | int ret = 0; | ||
2525 | int shared = 0; | ||
2526 | |||
2527 | if (!root->ref_cows) | ||
2528 | return 0; | ||
2529 | |||
2530 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { | ||
2531 | shared = 0; | ||
2532 | root_gen = root->root_key.offset; | ||
2533 | } else { | ||
2534 | shared = 1; | ||
2535 | root_gen = trans->transid - 1; | ||
2536 | } | ||
2537 | |||
2538 | level = btrfs_header_level(buf); | ||
2539 | nritems = btrfs_header_nritems(buf); | ||
2540 | |||
2541 | if (level == 0) { | ||
2542 | struct btrfs_leaf_ref *ref; | ||
2543 | struct btrfs_extent_info *info; | ||
2544 | |||
2545 | ref = btrfs_alloc_leaf_ref(root, nr_extents); | ||
2546 | if (!ref) { | ||
2547 | ret = -ENOMEM; | ||
2548 | goto out; | ||
2549 | } | ||
2550 | |||
2551 | ref->root_gen = root_gen; | ||
2552 | ref->bytenr = buf->start; | ||
2553 | ref->owner = btrfs_header_owner(buf); | ||
2554 | ref->generation = btrfs_header_generation(buf); | ||
2555 | ref->nritems = nr_extents; | ||
2556 | info = ref->extents; | ||
2557 | |||
2558 | for (i = 0; nr_extents > 0 && i < nritems; i++) { | ||
2559 | u64 disk_bytenr; | ||
2560 | btrfs_item_key_to_cpu(buf, &key, i); | ||
2561 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | ||
2562 | continue; | ||
2563 | fi = btrfs_item_ptr(buf, i, | ||
2564 | struct btrfs_file_extent_item); | ||
2565 | if (btrfs_file_extent_type(buf, fi) == | ||
2566 | BTRFS_FILE_EXTENT_INLINE) | ||
2567 | continue; | ||
2568 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | ||
2569 | if (disk_bytenr == 0) | ||
2570 | continue; | ||
2571 | |||
2572 | info->bytenr = disk_bytenr; | ||
2573 | info->num_bytes = | ||
2574 | btrfs_file_extent_disk_num_bytes(buf, fi); | ||
2575 | info->objectid = key.objectid; | ||
2576 | info->offset = key.offset; | ||
2577 | info++; | ||
2578 | } | ||
2579 | |||
2580 | ret = btrfs_add_leaf_ref(root, ref, shared); | ||
2581 | if (ret == -EEXIST && shared) { | ||
2582 | struct btrfs_leaf_ref *old; | ||
2583 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); | ||
2584 | BUG_ON(!old); | ||
2585 | btrfs_remove_leaf_ref(root, old); | ||
2586 | btrfs_free_leaf_ref(root, old); | ||
2587 | ret = btrfs_add_leaf_ref(root, ref, shared); | ||
2588 | } | ||
2589 | WARN_ON(ret); | ||
2590 | btrfs_free_leaf_ref(root, ref); | ||
2591 | } | ||
2592 | out: | ||
2593 | return ret; | ||
2594 | } | ||
2595 | |||
2596 | /* when a block goes through cow, we update the reference counts of | ||
2597 | * everything that block points to. The internal pointers of the block | ||
2598 | * can be in just about any order, and it is likely to have clusters of | ||
2599 | * things that are close together and clusters of things that are not. | ||
2600 | * | ||
2601 | * To help reduce the seeks that come with updating all of these reference | ||
2602 | * counts, sort them by byte number before actual updates are done. | ||
2603 | * | ||
2604 | * struct refsort is used to match byte number to slot in the btree block. | ||
2605 | * we sort based on the byte number and then use the slot to actually | ||
2606 | * find the item. | ||
2607 | * | ||
2608 | * struct refsort is smaller than strcut btrfs_item and smaller than | ||
2609 | * struct btrfs_key_ptr. Since we're currently limited to the page size | ||
2610 | * for a btree block, there's no way for a kmalloc of refsorts for a | ||
2611 | * single node to be bigger than a page. | ||
2612 | */ | ||
2613 | struct refsort { | ||
2614 | u64 bytenr; | ||
2615 | u32 slot; | ||
2616 | }; | ||
2617 | |||
2618 | /* | ||
2619 | * for passing into sort() | ||
2620 | */ | ||
2621 | static int refsort_cmp(const void *a_void, const void *b_void) | ||
2622 | { | ||
2623 | const struct refsort *a = a_void; | ||
2624 | const struct refsort *b = b_void; | ||
2625 | |||
2626 | if (a->bytenr < b->bytenr) | ||
2627 | return -1; | ||
2628 | if (a->bytenr > b->bytenr) | ||
2629 | return 1; | ||
2630 | return 0; | ||
2631 | } | ||
2632 | #endif | ||
2633 | |||
2634 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, | 2526 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
2635 | struct btrfs_root *root, | 2527 | struct btrfs_root *root, |
2636 | struct extent_buffer *buf, | 2528 | struct extent_buffer *buf, |
@@ -2733,7 +2625,7 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans, | |||
2733 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | 2625 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
2734 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | 2626 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
2735 | btrfs_mark_buffer_dirty(leaf); | 2627 | btrfs_mark_buffer_dirty(leaf); |
2736 | btrfs_release_path(extent_root, path); | 2628 | btrfs_release_path(path); |
2737 | fail: | 2629 | fail: |
2738 | if (ret) | 2630 | if (ret) |
2739 | return ret; | 2631 | return ret; |
@@ -2786,7 +2678,7 @@ again: | |||
2786 | inode = lookup_free_space_inode(root, block_group, path); | 2678 | inode = lookup_free_space_inode(root, block_group, path); |
2787 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { | 2679 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
2788 | ret = PTR_ERR(inode); | 2680 | ret = PTR_ERR(inode); |
2789 | btrfs_release_path(root, path); | 2681 | btrfs_release_path(path); |
2790 | goto out; | 2682 | goto out; |
2791 | } | 2683 | } |
2792 | 2684 | ||
@@ -2855,7 +2747,7 @@ again: | |||
2855 | out_put: | 2747 | out_put: |
2856 | iput(inode); | 2748 | iput(inode); |
2857 | out_free: | 2749 | out_free: |
2858 | btrfs_release_path(root, path); | 2750 | btrfs_release_path(path); |
2859 | out: | 2751 | out: |
2860 | spin_lock(&block_group->lock); | 2752 | spin_lock(&block_group->lock); |
2861 | block_group->disk_cache_state = dcs; | 2753 | block_group->disk_cache_state = dcs; |
@@ -3213,18 +3105,6 @@ commit_trans: | |||
3213 | goto again; | 3105 | goto again; |
3214 | } | 3106 | } |
3215 | 3107 | ||
3216 | #if 0 /* I hope we never need this code again, just in case */ | ||
3217 | printk(KERN_ERR "no space left, need %llu, %llu bytes_used, " | ||
3218 | "%llu bytes_reserved, " "%llu bytes_pinned, " | ||
3219 | "%llu bytes_readonly, %llu may use %llu total\n", | ||
3220 | (unsigned long long)bytes, | ||
3221 | (unsigned long long)data_sinfo->bytes_used, | ||
3222 | (unsigned long long)data_sinfo->bytes_reserved, | ||
3223 | (unsigned long long)data_sinfo->bytes_pinned, | ||
3224 | (unsigned long long)data_sinfo->bytes_readonly, | ||
3225 | (unsigned long long)data_sinfo->bytes_may_use, | ||
3226 | (unsigned long long)data_sinfo->total_bytes); | ||
3227 | #endif | ||
3228 | return -ENOSPC; | 3108 | return -ENOSPC; |
3229 | } | 3109 | } |
3230 | data_sinfo->bytes_may_use += bytes; | 3110 | data_sinfo->bytes_may_use += bytes; |
@@ -3653,8 +3533,8 @@ static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, | |||
3653 | spin_unlock(&block_rsv->lock); | 3533 | spin_unlock(&block_rsv->lock); |
3654 | } | 3534 | } |
3655 | 3535 | ||
3656 | void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, | 3536 | static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
3657 | struct btrfs_block_rsv *dest, u64 num_bytes) | 3537 | struct btrfs_block_rsv *dest, u64 num_bytes) |
3658 | { | 3538 | { |
3659 | struct btrfs_space_info *space_info = block_rsv->space_info; | 3539 | struct btrfs_space_info *space_info = block_rsv->space_info; |
3660 | 3540 | ||
@@ -3857,23 +3737,7 @@ static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) | |||
3857 | u64 meta_used; | 3737 | u64 meta_used; |
3858 | u64 data_used; | 3738 | u64 data_used; |
3859 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); | 3739 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
3860 | #if 0 | ||
3861 | /* | ||
3862 | * per tree used space accounting can be inaccuracy, so we | ||
3863 | * can't rely on it. | ||
3864 | */ | ||
3865 | spin_lock(&fs_info->extent_root->accounting_lock); | ||
3866 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item); | ||
3867 | spin_unlock(&fs_info->extent_root->accounting_lock); | ||
3868 | |||
3869 | spin_lock(&fs_info->csum_root->accounting_lock); | ||
3870 | num_bytes += btrfs_root_used(&fs_info->csum_root->root_item); | ||
3871 | spin_unlock(&fs_info->csum_root->accounting_lock); | ||
3872 | 3740 | ||
3873 | spin_lock(&fs_info->tree_root->accounting_lock); | ||
3874 | num_bytes += btrfs_root_used(&fs_info->tree_root->root_item); | ||
3875 | spin_unlock(&fs_info->tree_root->accounting_lock); | ||
3876 | #endif | ||
3877 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); | 3741 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
3878 | spin_lock(&sinfo->lock); | 3742 | spin_lock(&sinfo->lock); |
3879 | data_used = sinfo->bytes_used; | 3743 | data_used = sinfo->bytes_used; |
@@ -3926,10 +3790,7 @@ static void update_global_block_rsv(struct btrfs_fs_info *fs_info) | |||
3926 | block_rsv->reserved = block_rsv->size; | 3790 | block_rsv->reserved = block_rsv->size; |
3927 | block_rsv->full = 1; | 3791 | block_rsv->full = 1; |
3928 | } | 3792 | } |
3929 | #if 0 | 3793 | |
3930 | printk(KERN_INFO"global block rsv size %llu reserved %llu\n", | ||
3931 | block_rsv->size, block_rsv->reserved); | ||
3932 | #endif | ||
3933 | spin_unlock(&sinfo->lock); | 3794 | spin_unlock(&sinfo->lock); |
3934 | spin_unlock(&block_rsv->lock); | 3795 | spin_unlock(&block_rsv->lock); |
3935 | } | 3796 | } |
@@ -4537,7 +4398,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | |||
4537 | NULL, refs_to_drop, | 4398 | NULL, refs_to_drop, |
4538 | is_data); | 4399 | is_data); |
4539 | BUG_ON(ret); | 4400 | BUG_ON(ret); |
4540 | btrfs_release_path(extent_root, path); | 4401 | btrfs_release_path(path); |
4541 | path->leave_spinning = 1; | 4402 | path->leave_spinning = 1; |
4542 | 4403 | ||
4543 | key.objectid = bytenr; | 4404 | key.objectid = bytenr; |
@@ -4576,7 +4437,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | |||
4576 | owner_objectid, 0); | 4437 | owner_objectid, 0); |
4577 | BUG_ON(ret < 0); | 4438 | BUG_ON(ret < 0); |
4578 | 4439 | ||
4579 | btrfs_release_path(extent_root, path); | 4440 | btrfs_release_path(path); |
4580 | path->leave_spinning = 1; | 4441 | path->leave_spinning = 1; |
4581 | 4442 | ||
4582 | key.objectid = bytenr; | 4443 | key.objectid = bytenr; |
@@ -4646,7 +4507,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | |||
4646 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], | 4507 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
4647 | num_to_del); | 4508 | num_to_del); |
4648 | BUG_ON(ret); | 4509 | BUG_ON(ret); |
4649 | btrfs_release_path(extent_root, path); | 4510 | btrfs_release_path(path); |
4650 | 4511 | ||
4651 | if (is_data) { | 4512 | if (is_data) { |
4652 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); | 4513 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
@@ -6476,7 +6337,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, | |||
6476 | trans->block_rsv = block_rsv; | 6337 | trans->block_rsv = block_rsv; |
6477 | } | 6338 | } |
6478 | } | 6339 | } |
6479 | btrfs_release_path(root, path); | 6340 | btrfs_release_path(path); |
6480 | BUG_ON(err); | 6341 | BUG_ON(err); |
6481 | 6342 | ||
6482 | ret = btrfs_del_root(trans, tree_root, &root->root_key); | 6343 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
@@ -6580,1514 +6441,6 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans, | |||
6580 | return ret; | 6441 | return ret; |
6581 | } | 6442 | } |
6582 | 6443 | ||
6583 | #if 0 | ||
6584 | static unsigned long calc_ra(unsigned long start, unsigned long last, | ||
6585 | unsigned long nr) | ||
6586 | { | ||
6587 | return min(last, start + nr - 1); | ||
6588 | } | ||
6589 | |||
6590 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, | ||
6591 | u64 len) | ||
6592 | { | ||
6593 | u64 page_start; | ||
6594 | u64 page_end; | ||
6595 | unsigned long first_index; | ||
6596 | unsigned long last_index; | ||
6597 | unsigned long i; | ||
6598 | struct page *page; | ||
6599 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | ||
6600 | struct file_ra_state *ra; | ||
6601 | struct btrfs_ordered_extent *ordered; | ||
6602 | unsigned int total_read = 0; | ||
6603 | unsigned int total_dirty = 0; | ||
6604 | int ret = 0; | ||
6605 | |||
6606 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | ||
6607 | if (!ra) | ||
6608 | return -ENOMEM; | ||
6609 | |||
6610 | mutex_lock(&inode->i_mutex); | ||
6611 | first_index = start >> PAGE_CACHE_SHIFT; | ||
6612 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; | ||
6613 | |||
6614 | /* make sure the dirty trick played by the caller work */ | ||
6615 | ret = invalidate_inode_pages2_range(inode->i_mapping, | ||
6616 | first_index, last_index); | ||
6617 | if (ret) | ||
6618 | goto out_unlock; | ||
6619 | |||
6620 | file_ra_state_init(ra, inode->i_mapping); | ||
6621 | |||
6622 | for (i = first_index ; i <= last_index; i++) { | ||
6623 | if (total_read % ra->ra_pages == 0) { | ||
6624 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, | ||
6625 | calc_ra(i, last_index, ra->ra_pages)); | ||
6626 | } | ||
6627 | total_read++; | ||
6628 | again: | ||
6629 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) | ||
6630 | BUG_ON(1); | ||
6631 | page = grab_cache_page(inode->i_mapping, i); | ||
6632 | if (!page) { | ||
6633 | ret = -ENOMEM; | ||
6634 | goto out_unlock; | ||
6635 | } | ||
6636 | if (!PageUptodate(page)) { | ||
6637 | btrfs_readpage(NULL, page); | ||
6638 | lock_page(page); | ||
6639 | if (!PageUptodate(page)) { | ||
6640 | unlock_page(page); | ||
6641 | page_cache_release(page); | ||
6642 | ret = -EIO; | ||
6643 | goto out_unlock; | ||
6644 | } | ||
6645 | } | ||
6646 | wait_on_page_writeback(page); | ||
6647 | |||
6648 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; | ||
6649 | page_end = page_start + PAGE_CACHE_SIZE - 1; | ||
6650 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
6651 | |||
6652 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | ||
6653 | if (ordered) { | ||
6654 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
6655 | unlock_page(page); | ||
6656 | page_cache_release(page); | ||
6657 | btrfs_start_ordered_extent(inode, ordered, 1); | ||
6658 | btrfs_put_ordered_extent(ordered); | ||
6659 | goto again; | ||
6660 | } | ||
6661 | set_page_extent_mapped(page); | ||
6662 | |||
6663 | if (i == first_index) | ||
6664 | set_extent_bits(io_tree, page_start, page_end, | ||
6665 | EXTENT_BOUNDARY, GFP_NOFS); | ||
6666 | btrfs_set_extent_delalloc(inode, page_start, page_end); | ||
6667 | |||
6668 | set_page_dirty(page); | ||
6669 | total_dirty++; | ||
6670 | |||
6671 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | ||
6672 | unlock_page(page); | ||
6673 | page_cache_release(page); | ||
6674 | } | ||
6675 | |||
6676 | out_unlock: | ||
6677 | kfree(ra); | ||
6678 | mutex_unlock(&inode->i_mutex); | ||
6679 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); | ||
6680 | return ret; | ||
6681 | } | ||
6682 | |||
6683 | static noinline int relocate_data_extent(struct inode *reloc_inode, | ||
6684 | struct btrfs_key *extent_key, | ||
6685 | u64 offset) | ||
6686 | { | ||
6687 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; | ||
6688 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; | ||
6689 | struct extent_map *em; | ||
6690 | u64 start = extent_key->objectid - offset; | ||
6691 | u64 end = start + extent_key->offset - 1; | ||
6692 | |||
6693 | em = alloc_extent_map(GFP_NOFS); | ||
6694 | BUG_ON(!em); | ||
6695 | |||
6696 | em->start = start; | ||
6697 | em->len = extent_key->offset; | ||
6698 | em->block_len = extent_key->offset; | ||
6699 | em->block_start = extent_key->objectid; | ||
6700 | em->bdev = root->fs_info->fs_devices->latest_bdev; | ||
6701 | set_bit(EXTENT_FLAG_PINNED, &em->flags); | ||
6702 | |||
6703 | /* setup extent map to cheat btrfs_readpage */ | ||
6704 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); | ||
6705 | while (1) { | ||
6706 | int ret; | ||
6707 | write_lock(&em_tree->lock); | ||
6708 | ret = add_extent_mapping(em_tree, em); | ||
6709 | write_unlock(&em_tree->lock); | ||
6710 | if (ret != -EEXIST) { | ||
6711 | free_extent_map(em); | ||
6712 | break; | ||
6713 | } | ||
6714 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); | ||
6715 | } | ||
6716 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); | ||
6717 | |||
6718 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); | ||
6719 | } | ||
6720 | |||
6721 | struct btrfs_ref_path { | ||
6722 | u64 extent_start; | ||
6723 | u64 nodes[BTRFS_MAX_LEVEL]; | ||
6724 | u64 root_objectid; | ||
6725 | u64 root_generation; | ||
6726 | u64 owner_objectid; | ||
6727 | u32 num_refs; | ||
6728 | int lowest_level; | ||
6729 | int current_level; | ||
6730 | int shared_level; | ||
6731 | |||
6732 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; | ||
6733 | u64 new_nodes[BTRFS_MAX_LEVEL]; | ||
6734 | }; | ||
6735 | |||
6736 | struct disk_extent { | ||
6737 | u64 ram_bytes; | ||
6738 | u64 disk_bytenr; | ||
6739 | u64 disk_num_bytes; | ||
6740 | u64 offset; | ||
6741 | u64 num_bytes; | ||
6742 | u8 compression; | ||
6743 | u8 encryption; | ||
6744 | u16 other_encoding; | ||
6745 | }; | ||
6746 | |||
6747 | static int is_cowonly_root(u64 root_objectid) | ||
6748 | { | ||
6749 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || | ||
6750 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || | ||
6751 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || | ||
6752 | root_objectid == BTRFS_DEV_TREE_OBJECTID || | ||
6753 | root_objectid == BTRFS_TREE_LOG_OBJECTID || | ||
6754 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) | ||
6755 | return 1; | ||
6756 | return 0; | ||
6757 | } | ||
6758 | |||
6759 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, | ||
6760 | struct btrfs_root *extent_root, | ||
6761 | struct btrfs_ref_path *ref_path, | ||
6762 | int first_time) | ||
6763 | { | ||
6764 | struct extent_buffer *leaf; | ||
6765 | struct btrfs_path *path; | ||
6766 | struct btrfs_extent_ref *ref; | ||
6767 | struct btrfs_key key; | ||
6768 | struct btrfs_key found_key; | ||
6769 | u64 bytenr; | ||
6770 | u32 nritems; | ||
6771 | int level; | ||
6772 | int ret = 1; | ||
6773 | |||
6774 | path = btrfs_alloc_path(); | ||
6775 | if (!path) | ||
6776 | return -ENOMEM; | ||
6777 | |||
6778 | if (first_time) { | ||
6779 | ref_path->lowest_level = -1; | ||
6780 | ref_path->current_level = -1; | ||
6781 | ref_path->shared_level = -1; | ||
6782 | goto walk_up; | ||
6783 | } | ||
6784 | walk_down: | ||
6785 | level = ref_path->current_level - 1; | ||
6786 | while (level >= -1) { | ||
6787 | u64 parent; | ||
6788 | if (level < ref_path->lowest_level) | ||
6789 | break; | ||
6790 | |||
6791 | if (level >= 0) | ||
6792 | bytenr = ref_path->nodes[level]; | ||
6793 | else | ||
6794 | bytenr = ref_path->extent_start; | ||
6795 | BUG_ON(bytenr == 0); | ||
6796 | |||
6797 | parent = ref_path->nodes[level + 1]; | ||
6798 | ref_path->nodes[level + 1] = 0; | ||
6799 | ref_path->current_level = level; | ||
6800 | BUG_ON(parent == 0); | ||
6801 | |||
6802 | key.objectid = bytenr; | ||
6803 | key.offset = parent + 1; | ||
6804 | key.type = BTRFS_EXTENT_REF_KEY; | ||
6805 | |||
6806 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); | ||
6807 | if (ret < 0) | ||
6808 | goto out; | ||
6809 | BUG_ON(ret == 0); | ||
6810 | |||
6811 | leaf = path->nodes[0]; | ||
6812 | nritems = btrfs_header_nritems(leaf); | ||
6813 | if (path->slots[0] >= nritems) { | ||
6814 | ret = btrfs_next_leaf(extent_root, path); | ||
6815 | if (ret < 0) | ||
6816 | goto out; | ||
6817 | if (ret > 0) | ||
6818 | goto next; | ||
6819 | leaf = path->nodes[0]; | ||
6820 | } | ||
6821 | |||
6822 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | ||
6823 | if (found_key.objectid == bytenr && | ||
6824 | found_key.type == BTRFS_EXTENT_REF_KEY) { | ||
6825 | if (level < ref_path->shared_level) | ||
6826 | ref_path->shared_level = level; | ||
6827 | goto found; | ||
6828 | } | ||
6829 | next: | ||
6830 | level--; | ||
6831 | btrfs_release_path(extent_root, path); | ||
6832 | cond_resched(); | ||
6833 | } | ||
6834 | /* reached lowest level */ | ||
6835 | ret = 1; | ||
6836 | goto out; | ||
6837 | walk_up: | ||
6838 | level = ref_path->current_level; | ||
6839 | while (level < BTRFS_MAX_LEVEL - 1) { | ||
6840 | u64 ref_objectid; | ||
6841 | |||
6842 | if (level >= 0) | ||
6843 | bytenr = ref_path->nodes[level]; | ||
6844 | else | ||
6845 | bytenr = ref_path->extent_start; | ||
6846 | |||
6847 | BUG_ON(bytenr == 0); | ||
6848 | |||
6849 | key.objectid = bytenr; | ||
6850 | key.offset = 0; | ||
6851 | key.type = BTRFS_EXTENT_REF_KEY; | ||
6852 | |||
6853 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); | ||
6854 | if (ret < 0) | ||
6855 | goto out; | ||
6856 | |||
6857 | leaf = path->nodes[0]; | ||
6858 | nritems = btrfs_header_nritems(leaf); | ||
6859 | if (path->slots[0] >= nritems) { | ||
6860 | ret = btrfs_next_leaf(extent_root, path); | ||
6861 | if (ret < 0) | ||
6862 | goto out; | ||
6863 | if (ret > 0) { | ||
6864 | /* the extent was freed by someone */ | ||
6865 | if (ref_path->lowest_level == level) | ||
6866 | goto out; | ||
6867 | btrfs_release_path(extent_root, path); | ||
6868 | goto walk_down; | ||
6869 | } | ||
6870 | leaf = path->nodes[0]; | ||
6871 | } | ||
6872 | |||
6873 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | ||
6874 | if (found_key.objectid != bytenr || | ||
6875 | found_key.type != BTRFS_EXTENT_REF_KEY) { | ||
6876 | /* the extent was freed by someone */ | ||
6877 | if (ref_path->lowest_level == level) { | ||
6878 | ret = 1; | ||
6879 | goto out; | ||
6880 | } | ||
6881 | btrfs_release_path(extent_root, path); | ||
6882 | goto walk_down; | ||
6883 | } | ||
6884 | found: | ||
6885 | ref = btrfs_item_ptr(leaf, path->slots[0], | ||
6886 | struct btrfs_extent_ref); | ||
6887 | ref_objectid = btrfs_ref_objectid(leaf, ref); | ||
6888 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { | ||
6889 | if (first_time) { | ||
6890 | level = (int)ref_objectid; | ||
6891 | BUG_ON(level >= BTRFS_MAX_LEVEL); | ||
6892 | ref_path->lowest_level = level; | ||
6893 | ref_path->current_level = level; | ||
6894 | ref_path->nodes[level] = bytenr; | ||
6895 | } else { | ||
6896 | WARN_ON(ref_objectid != level); | ||
6897 | } | ||
6898 | } else { | ||
6899 | WARN_ON(level != -1); | ||
6900 | } | ||
6901 | first_time = 0; | ||
6902 | |||
6903 | if (ref_path->lowest_level == level) { | ||
6904 | ref_path->owner_objectid = ref_objectid; | ||
6905 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); | ||
6906 | } | ||
6907 | |||
6908 | /* | ||
6909 | * the block is tree root or the block isn't in reference | ||
6910 | * counted tree. | ||
6911 | */ | ||
6912 | if (found_key.objectid == found_key.offset || | ||
6913 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { | ||
6914 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); | ||
6915 | ref_path->root_generation = | ||
6916 | btrfs_ref_generation(leaf, ref); | ||
6917 | if (level < 0) { | ||
6918 | /* special reference from the tree log */ | ||
6919 | ref_path->nodes[0] = found_key.offset; | ||
6920 | ref_path->current_level = 0; | ||
6921 | } | ||
6922 | ret = 0; | ||
6923 | goto out; | ||
6924 | } | ||
6925 | |||
6926 | level++; | ||
6927 | BUG_ON(ref_path->nodes[level] != 0); | ||
6928 | ref_path->nodes[level] = found_key.offset; | ||
6929 | ref_path->current_level = level; | ||
6930 | |||
6931 | /* | ||
6932 | * the reference was created in the running transaction, | ||
6933 | * no need to continue walking up. | ||
6934 | */ | ||
6935 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { | ||
6936 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); | ||
6937 | ref_path->root_generation = | ||
6938 | btrfs_ref_generation(leaf, ref); | ||
6939 | ret = 0; | ||
6940 | goto out; | ||
6941 | } | ||
6942 | |||
6943 | btrfs_release_path(extent_root, path); | ||
6944 | cond_resched(); | ||
6945 | } | ||
6946 | /* reached max tree level, but no tree root found. */ | ||
6947 | BUG(); | ||
6948 | out: | ||
6949 | btrfs_free_path(path); | ||
6950 | return ret; | ||
6951 | } | ||
6952 | |||
6953 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, | ||
6954 | struct btrfs_root *extent_root, | ||
6955 | struct btrfs_ref_path *ref_path, | ||
6956 | u64 extent_start) | ||
6957 | { | ||
6958 | memset(ref_path, 0, sizeof(*ref_path)); | ||
6959 | ref_path->extent_start = extent_start; | ||
6960 | |||
6961 | return __next_ref_path(trans, extent_root, ref_path, 1); | ||
6962 | } | ||
6963 | |||
6964 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, | ||
6965 | struct btrfs_root *extent_root, | ||
6966 | struct btrfs_ref_path *ref_path) | ||
6967 | { | ||
6968 | return __next_ref_path(trans, extent_root, ref_path, 0); | ||
6969 | } | ||
6970 | |||
6971 | static noinline int get_new_locations(struct inode *reloc_inode, | ||
6972 | struct btrfs_key *extent_key, | ||
6973 | u64 offset, int no_fragment, | ||
6974 | struct disk_extent **extents, | ||
6975 | int *nr_extents) | ||
6976 | { | ||
6977 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; | ||
6978 | struct btrfs_path *path; | ||
6979 | struct btrfs_file_extent_item *fi; | ||
6980 | struct extent_buffer *leaf; | ||
6981 | struct disk_extent *exts = *extents; | ||
6982 | struct btrfs_key found_key; | ||
6983 | u64 cur_pos; | ||
6984 | u64 last_byte; | ||
6985 | u32 nritems; | ||
6986 | int nr = 0; | ||
6987 | int max = *nr_extents; | ||
6988 | int ret; | ||
6989 | |||
6990 | WARN_ON(!no_fragment && *extents); | ||
6991 | if (!exts) { | ||
6992 | max = 1; | ||
6993 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); | ||
6994 | if (!exts) | ||
6995 | return -ENOMEM; | ||
6996 | } | ||
6997 | |||
6998 | path = btrfs_alloc_path(); | ||
6999 | if (!path) { | ||
7000 | if (exts != *extents) | ||
7001 | kfree(exts); | ||
7002 | return -ENOMEM; | ||
7003 | } | ||
7004 | |||
7005 | cur_pos = extent_key->objectid - offset; | ||
7006 | last_byte = extent_key->objectid + extent_key->offset; | ||
7007 | ret = btrfs_lookup_file_extent(NULL, root, path, | ||
7008 | btrfs_ino(reloc_inode), cur_pos, 0); | ||
7009 | if (ret < 0) | ||
7010 | goto out; | ||
7011 | if (ret > 0) { | ||
7012 | ret = -ENOENT; | ||
7013 | goto out; | ||
7014 | } | ||
7015 | |||
7016 | while (1) { | ||
7017 | leaf = path->nodes[0]; | ||
7018 | nritems = btrfs_header_nritems(leaf); | ||
7019 | if (path->slots[0] >= nritems) { | ||
7020 | ret = btrfs_next_leaf(root, path); | ||
7021 | if (ret < 0) | ||
7022 | goto out; | ||
7023 | if (ret > 0) | ||
7024 | break; | ||
7025 | leaf = path->nodes[0]; | ||
7026 | } | ||
7027 | |||
7028 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | ||
7029 | if (found_key.offset != cur_pos || | ||
7030 | found_key.type != BTRFS_EXTENT_DATA_KEY || | ||
7031 | found_key.objectid != btrfs_ino(reloc_inode)) | ||
7032 | break; | ||
7033 | |||
7034 | fi = btrfs_item_ptr(leaf, path->slots[0], | ||
7035 | struct btrfs_file_extent_item); | ||
7036 | if (btrfs_file_extent_type(leaf, fi) != | ||
7037 | BTRFS_FILE_EXTENT_REG || | ||
7038 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) | ||
7039 | break; | ||
7040 | |||
7041 | if (nr == max) { | ||
7042 | struct disk_extent *old = exts; | ||
7043 | max *= 2; | ||
7044 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); | ||
7045 | if (!exts) { | ||
7046 | ret = -ENOMEM; | ||
7047 | goto out; | ||
7048 | } | ||
7049 | memcpy(exts, old, sizeof(*exts) * nr); | ||
7050 | if (old != *extents) | ||
7051 | kfree(old); | ||
7052 | } | ||
7053 | |||
7054 | exts[nr].disk_bytenr = | ||
7055 | btrfs_file_extent_disk_bytenr(leaf, fi); | ||
7056 | exts[nr].disk_num_bytes = | ||
7057 | btrfs_file_extent_disk_num_bytes(leaf, fi); | ||
7058 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); | ||
7059 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); | ||
7060 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); | ||
7061 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); | ||
7062 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); | ||
7063 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, | ||
7064 | fi); | ||
7065 | BUG_ON(exts[nr].offset > 0); | ||
7066 | BUG_ON(exts[nr].compression || exts[nr].encryption); | ||
7067 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); | ||
7068 | |||
7069 | cur_pos += exts[nr].num_bytes; | ||
7070 | nr++; | ||
7071 | |||
7072 | if (cur_pos + offset >= last_byte) | ||
7073 | break; | ||
7074 | |||
7075 | if (no_fragment) { | ||
7076 | ret = 1; | ||
7077 | goto out; | ||
7078 | } | ||
7079 | path->slots[0]++; | ||
7080 | } | ||
7081 | |||
7082 | BUG_ON(cur_pos + offset > last_byte); | ||
7083 | if (cur_pos + offset < last_byte) { | ||
7084 | ret = -ENOENT; | ||
7085 | goto out; | ||
7086 | } | ||
7087 | ret = 0; | ||
7088 | out: | ||
7089 | btrfs_free_path(path); | ||
7090 | if (ret) { | ||
7091 | if (exts != *extents) | ||
7092 | kfree(exts); | ||
7093 | } else { | ||
7094 | *extents = exts; | ||
7095 | *nr_extents = nr; | ||
7096 | } | ||
7097 | return ret; | ||
7098 | } | ||
7099 | |||
7100 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, | ||
7101 | struct btrfs_root *root, | ||
7102 | struct btrfs_path *path, | ||
7103 | struct btrfs_key *extent_key, | ||
7104 | struct btrfs_key *leaf_key, | ||
7105 | struct btrfs_ref_path *ref_path, | ||
7106 | struct disk_extent *new_extents, | ||
7107 | int nr_extents) | ||
7108 | { | ||
7109 | struct extent_buffer *leaf; | ||
7110 | struct btrfs_file_extent_item *fi; | ||
7111 | struct inode *inode = NULL; | ||
7112 | struct btrfs_key key; | ||
7113 | u64 lock_start = 0; | ||
7114 | u64 lock_end = 0; | ||
7115 | u64 num_bytes; | ||
7116 | u64 ext_offset; | ||
7117 | u64 search_end = (u64)-1; | ||
7118 | u32 nritems; | ||
7119 | int nr_scaned = 0; | ||
7120 | int extent_locked = 0; | ||
7121 | int extent_type; | ||
7122 | int ret; | ||
7123 | |||
7124 | memcpy(&key, leaf_key, sizeof(key)); | ||
7125 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { | ||
7126 | if (key.objectid < ref_path->owner_objectid || | ||
7127 | (key.objectid == ref_path->owner_objectid && | ||
7128 | key.type < BTRFS_EXTENT_DATA_KEY)) { | ||
7129 | key.objectid = ref_path->owner_objectid; | ||
7130 | key.type = BTRFS_EXTENT_DATA_KEY; | ||
7131 | key.offset = 0; | ||
7132 | } | ||
7133 | } | ||
7134 | |||
7135 | while (1) { | ||
7136 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | ||
7137 | if (ret < 0) | ||
7138 | goto out; | ||
7139 | |||
7140 | leaf = path->nodes[0]; | ||
7141 | nritems = btrfs_header_nritems(leaf); | ||
7142 | next: | ||
7143 | if (extent_locked && ret > 0) { | ||
7144 | /* | ||
7145 | * the file extent item was modified by someone | ||
7146 | * before the extent got locked. | ||
7147 | */ | ||
7148 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, | ||
7149 | lock_end, GFP_NOFS); | ||
7150 | extent_locked = 0; | ||
7151 | } | ||
7152 | |||
7153 | if (path->slots[0] >= nritems) { | ||
7154 | if (++nr_scaned > 2) | ||
7155 | break; | ||
7156 | |||
7157 | BUG_ON(extent_locked); | ||
7158 | ret = btrfs_next_leaf(root, path); | ||
7159 | if (ret < 0) | ||
7160 | goto out; | ||
7161 | if (ret > 0) | ||
7162 | break; | ||
7163 | leaf = path->nodes[0]; | ||
7164 | nritems = btrfs_header_nritems(leaf); | ||
7165 | } | ||
7166 | |||
7167 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | ||
7168 | |||
7169 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { | ||
7170 | if ((key.objectid > ref_path->owner_objectid) || | ||
7171 | (key.objectid == ref_path->owner_objectid && | ||
7172 | key.type > BTRFS_EXTENT_DATA_KEY) || | ||
7173 | key.offset >= search_end) | ||
7174 | break; | ||
7175 | } | ||
7176 | |||
7177 | if (inode && key.objectid != btrfs_ino(inode)) { | ||
7178 | BUG_ON(extent_locked); | ||
7179 | btrfs_release_path(root, path); | ||
7180 | mutex_unlock(&inode->i_mutex); | ||
7181 | iput(inode); | ||
7182 | inode = NULL; | ||
7183 | continue; | ||
7184 | } | ||
7185 | |||
7186 | if (key.type != BTRFS_EXTENT_DATA_KEY) { | ||
7187 | path->slots[0]++; | ||
7188 | ret = 1; | ||
7189 | goto next; | ||
7190 | } | ||
7191 | fi = btrfs_item_ptr(leaf, path->slots[0], | ||
7192 | struct btrfs_file_extent_item); | ||
7193 | extent_type = btrfs_file_extent_type(leaf, fi); | ||
7194 | if ((extent_type != BTRFS_FILE_EXTENT_REG && | ||
7195 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || | ||
7196 | (btrfs_file_extent_disk_bytenr(leaf, fi) != | ||
7197 | extent_key->objectid)) { | ||
7198 | path->slots[0]++; | ||
7199 | ret = 1; | ||
7200 | goto next; | ||
7201 | } | ||
7202 | |||
7203 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); | ||
7204 | ext_offset = btrfs_file_extent_offset(leaf, fi); | ||
7205 | |||
7206 | if (search_end == (u64)-1) { | ||
7207 | search_end = key.offset - ext_offset + | ||
7208 | btrfs_file_extent_ram_bytes(leaf, fi); | ||
7209 | } | ||
7210 | |||
7211 | if (!extent_locked) { | ||
7212 | lock_start = key.offset; | ||
7213 | lock_end = lock_start + num_bytes - 1; | ||
7214 | } else { | ||
7215 | if (lock_start > key.offset || | ||
7216 | lock_end + 1 < key.offset + num_bytes) { | ||
7217 | unlock_extent(&BTRFS_I(inode)->io_tree, | ||
7218 | lock_start, lock_end, GFP_NOFS); | ||
7219 | extent_locked = 0; | ||
7220 | } | ||
7221 | } | ||
7222 | |||
7223 | if (!inode) { | ||
7224 | btrfs_release_path(root, path); | ||
7225 | |||
7226 | inode = btrfs_iget_locked(root->fs_info->sb, | ||
7227 | key.objectid, root); | ||
7228 | if (inode->i_state & I_NEW) { | ||
7229 | BTRFS_I(inode)->root = root; | ||
7230 | BTRFS_I(inode)->location.objectid = | ||
7231 | key.objectid; | ||
7232 | BTRFS_I(inode)->location.type = | ||
7233 | BTRFS_INODE_ITEM_KEY; | ||
7234 | BTRFS_I(inode)->location.offset = 0; | ||
7235 | btrfs_read_locked_inode(inode); | ||
7236 | unlock_new_inode(inode); | ||
7237 | } | ||
7238 | /* | ||
7239 | * some code call btrfs_commit_transaction while | ||
7240 | * holding the i_mutex, so we can't use mutex_lock | ||
7241 | * here. | ||
7242 | */ | ||
7243 | if (is_bad_inode(inode) || | ||
7244 | !mutex_trylock(&inode->i_mutex)) { | ||
7245 | iput(inode); | ||
7246 | inode = NULL; | ||
7247 | key.offset = (u64)-1; | ||
7248 | goto skip; | ||
7249 | } | ||
7250 | } | ||
7251 | |||
7252 | if (!extent_locked) { | ||
7253 | struct btrfs_ordered_extent *ordered; | ||
7254 | |||
7255 | btrfs_release_path(root, path); | ||
7256 | |||
7257 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, | ||
7258 | lock_end, GFP_NOFS); | ||
7259 | ordered = btrfs_lookup_first_ordered_extent(inode, | ||
7260 | lock_end); | ||
7261 | if (ordered && | ||
7262 | ordered->file_offset <= lock_end && | ||
7263 | ordered->file_offset + ordered->len > lock_start) { | ||
7264 | unlock_extent(&BTRFS_I(inode)->io_tree, | ||
7265 | lock_start, lock_end, GFP_NOFS); | ||
7266 | btrfs_start_ordered_extent(inode, ordered, 1); | ||
7267 | btrfs_put_ordered_extent(ordered); | ||
7268 | key.offset += num_bytes; | ||
7269 | goto skip; | ||
7270 | } | ||
7271 | if (ordered) | ||
7272 | btrfs_put_ordered_extent(ordered); | ||
7273 | |||
7274 | extent_locked = 1; | ||
7275 | continue; | ||
7276 | } | ||
7277 | |||
7278 | if (nr_extents == 1) { | ||
7279 | /* update extent pointer in place */ | ||
7280 | btrfs_set_file_extent_disk_bytenr(leaf, fi, | ||
7281 | new_extents[0].disk_bytenr); | ||
7282 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, | ||
7283 | new_extents[0].disk_num_bytes); | ||
7284 | btrfs_mark_buffer_dirty(leaf); | ||
7285 | |||
7286 | btrfs_drop_extent_cache(inode, key.offset, | ||
7287 | key.offset + num_bytes - 1, 0); | ||
7288 | |||
7289 | ret = btrfs_inc_extent_ref(trans, root, | ||
7290 | new_extents[0].disk_bytenr, | ||
7291 | new_extents[0].disk_num_bytes, | ||
7292 | leaf->start, | ||
7293 | root->root_key.objectid, | ||
7294 | trans->transid, | ||
7295 | key.objectid); | ||
7296 | BUG_ON(ret); | ||
7297 | |||
7298 | ret = btrfs_free_extent(trans, root, | ||
7299 | extent_key->objectid, | ||
7300 | extent_key->offset, | ||
7301 | leaf->start, | ||
7302 | btrfs_header_owner(leaf), | ||
7303 | btrfs_header_generation(leaf), | ||
7304 | key.objectid, 0); | ||
7305 | BUG_ON(ret); | ||
7306 | |||
7307 | btrfs_release_path(root, path); | ||
7308 | key.offset += num_bytes; | ||
7309 | } else { | ||
7310 | BUG_ON(1); | ||
7311 | #if 0 | ||
7312 | u64 alloc_hint; | ||
7313 | u64 extent_len; | ||
7314 | int i; | ||
7315 | /* | ||
7316 | * drop old extent pointer at first, then insert the | ||
7317 | * new pointers one bye one | ||
7318 | */ | ||
7319 | btrfs_release_path(root, path); | ||
7320 | ret = btrfs_drop_extents(trans, root, inode, key.offset, | ||
7321 | key.offset + num_bytes, | ||
7322 | key.offset, &alloc_hint); | ||
7323 | BUG_ON(ret); | ||
7324 | |||
7325 | for (i = 0; i < nr_extents; i++) { | ||
7326 | if (ext_offset >= new_extents[i].num_bytes) { | ||
7327 | ext_offset -= new_extents[i].num_bytes; | ||
7328 | continue; | ||
7329 | } | ||
7330 | extent_len = min(new_extents[i].num_bytes - | ||
7331 | ext_offset, num_bytes); | ||
7332 | |||
7333 | ret = btrfs_insert_empty_item(trans, root, | ||
7334 | path, &key, | ||
7335 | sizeof(*fi)); | ||
7336 | BUG_ON(ret); | ||
7337 | |||
7338 | leaf = path->nodes[0]; | ||
7339 | fi = btrfs_item_ptr(leaf, path->slots[0], | ||
7340 | struct btrfs_file_extent_item); | ||
7341 | btrfs_set_file_extent_generation(leaf, fi, | ||
7342 | trans->transid); | ||
7343 | btrfs_set_file_extent_type(leaf, fi, | ||
7344 | BTRFS_FILE_EXTENT_REG); | ||
7345 | btrfs_set_file_extent_disk_bytenr(leaf, fi, | ||
7346 | new_extents[i].disk_bytenr); | ||
7347 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, | ||
7348 | new_extents[i].disk_num_bytes); | ||
7349 | btrfs_set_file_extent_ram_bytes(leaf, fi, | ||
7350 | new_extents[i].ram_bytes); | ||
7351 | |||
7352 | btrfs_set_file_extent_compression(leaf, fi, | ||
7353 | new_extents[i].compression); | ||
7354 | btrfs_set_file_extent_encryption(leaf, fi, | ||
7355 | new_extents[i].encryption); | ||
7356 | btrfs_set_file_extent_other_encoding(leaf, fi, | ||
7357 | new_extents[i].other_encoding); | ||
7358 | |||
7359 | btrfs_set_file_extent_num_bytes(leaf, fi, | ||
7360 | extent_len); | ||
7361 | ext_offset += new_extents[i].offset; | ||
7362 | btrfs_set_file_extent_offset(leaf, fi, | ||
7363 | ext_offset); | ||
7364 | btrfs_mark_buffer_dirty(leaf); | ||
7365 | |||
7366 | btrfs_drop_extent_cache(inode, key.offset, | ||
7367 | key.offset + extent_len - 1, 0); | ||
7368 | |||
7369 | ret = btrfs_inc_extent_ref(trans, root, | ||
7370 | new_extents[i].disk_bytenr, | ||
7371 | new_extents[i].disk_num_bytes, | ||
7372 | leaf->start, | ||
7373 | root->root_key.objectid, | ||
7374 | trans->transid, key.objectid); | ||
7375 | BUG_ON(ret); | ||
7376 | btrfs_release_path(root, path); | ||
7377 | |||
7378 | inode_add_bytes(inode, extent_len); | ||
7379 | |||
7380 | ext_offset = 0; | ||
7381 | num_bytes -= extent_len; | ||
7382 | key.offset += extent_len; | ||
7383 | |||
7384 | if (num_bytes == 0) | ||
7385 | break; | ||
7386 | } | ||
7387 | BUG_ON(i >= nr_extents); | ||
7388 | #endif | ||
7389 | } | ||
7390 | |||
7391 | if (extent_locked) { | ||
7392 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, | ||
7393 | lock_end, GFP_NOFS); | ||
7394 | extent_locked = 0; | ||
7395 | } | ||
7396 | skip: | ||
7397 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && | ||
7398 | key.offset >= search_end) | ||
7399 | break; | ||
7400 | |||
7401 | cond_resched(); | ||
7402 | } | ||
7403 | ret = 0; | ||
7404 | out: | ||
7405 | btrfs_release_path(root, path); | ||
7406 | if (inode) { | ||
7407 | mutex_unlock(&inode->i_mutex); | ||
7408 | if (extent_locked) { | ||
7409 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, | ||
7410 | lock_end, GFP_NOFS); | ||
7411 | } | ||
7412 | iput(inode); | ||
7413 | } | ||
7414 | return ret; | ||
7415 | } | ||
7416 | |||
7417 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, | ||
7418 | struct btrfs_root *root, | ||
7419 | struct extent_buffer *buf, u64 orig_start) | ||
7420 | { | ||
7421 | int level; | ||
7422 | int ret; | ||
7423 | |||
7424 | BUG_ON(btrfs_header_generation(buf) != trans->transid); | ||
7425 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); | ||
7426 | |||
7427 | level = btrfs_header_level(buf); | ||
7428 | if (level == 0) { | ||
7429 | struct btrfs_leaf_ref *ref; | ||
7430 | struct btrfs_leaf_ref *orig_ref; | ||
7431 | |||
7432 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); | ||
7433 | if (!orig_ref) | ||
7434 | return -ENOENT; | ||
7435 | |||
7436 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); | ||
7437 | if (!ref) { | ||
7438 | btrfs_free_leaf_ref(root, orig_ref); | ||
7439 | return -ENOMEM; | ||
7440 | } | ||
7441 | |||
7442 | ref->nritems = orig_ref->nritems; | ||
7443 | memcpy(ref->extents, orig_ref->extents, | ||
7444 | sizeof(ref->extents[0]) * ref->nritems); | ||
7445 | |||
7446 | btrfs_free_leaf_ref(root, orig_ref); | ||
7447 | |||
7448 | ref->root_gen = trans->transid; | ||
7449 | ref->bytenr = buf->start; | ||
7450 | ref->owner = btrfs_header_owner(buf); | ||
7451 | ref->generation = btrfs_header_generation(buf); | ||
7452 | |||
7453 | ret = btrfs_add_leaf_ref(root, ref, 0); | ||
7454 | WARN_ON(ret); | ||
7455 | btrfs_free_leaf_ref(root, ref); | ||
7456 | } | ||
7457 | return 0; | ||
7458 | } | ||
7459 | |||
7460 | static noinline int invalidate_extent_cache(struct btrfs_root *root, | ||
7461 | struct extent_buffer *leaf, | ||
7462 | struct btrfs_block_group_cache *group, | ||
7463 | struct btrfs_root *target_root) | ||
7464 | { | ||
7465 | struct btrfs_key key; | ||
7466 | struct inode *inode = NULL; | ||
7467 | struct btrfs_file_extent_item *fi; | ||
7468 | struct extent_state *cached_state = NULL; | ||
7469 | u64 num_bytes; | ||
7470 | u64 skip_objectid = 0; | ||
7471 | u32 nritems; | ||
7472 | u32 i; | ||
7473 | |||
7474 | nritems = btrfs_header_nritems(leaf); | ||
7475 | for (i = 0; i < nritems; i++) { | ||
7476 | btrfs_item_key_to_cpu(leaf, &key, i); | ||
7477 | if (key.objectid == skip_objectid || | ||
7478 | key.type != BTRFS_EXTENT_DATA_KEY) | ||
7479 | continue; | ||
7480 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | ||
7481 | if (btrfs_file_extent_type(leaf, fi) == | ||
7482 | BTRFS_FILE_EXTENT_INLINE) | ||
7483 | continue; | ||
7484 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) | ||
7485 | continue; | ||
7486 | if (!inode || btrfs_ino(inode) != key.objectid) { | ||
7487 | iput(inode); | ||
7488 | inode = btrfs_ilookup(target_root->fs_info->sb, | ||
7489 | key.objectid, target_root, 1); | ||
7490 | } | ||
7491 | if (!inode) { | ||
7492 | skip_objectid = key.objectid; | ||
7493 | continue; | ||
7494 | } | ||
7495 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); | ||
7496 | |||
7497 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, | ||
7498 | key.offset + num_bytes - 1, 0, &cached_state, | ||
7499 | GFP_NOFS); | ||
7500 | btrfs_drop_extent_cache(inode, key.offset, | ||
7501 | key.offset + num_bytes - 1, 1); | ||
7502 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, | ||
7503 | key.offset + num_bytes - 1, &cached_state, | ||
7504 | GFP_NOFS); | ||
7505 | cond_resched(); | ||
7506 | } | ||
7507 | iput(inode); | ||
7508 | return 0; | ||
7509 | } | ||
7510 | |||
7511 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, | ||
7512 | struct btrfs_root *root, | ||
7513 | struct extent_buffer *leaf, | ||
7514 | struct btrfs_block_group_cache *group, | ||
7515 | struct inode *reloc_inode) | ||
7516 | { | ||
7517 | struct btrfs_key key; | ||
7518 | struct btrfs_key extent_key; | ||
7519 | struct btrfs_file_extent_item *fi; | ||
7520 | struct btrfs_leaf_ref *ref; | ||
7521 | struct disk_extent *new_extent; | ||
7522 | u64 bytenr; | ||
7523 | u64 num_bytes; | ||
7524 | u32 nritems; | ||
7525 | u32 i; | ||
7526 | int ext_index; | ||
7527 | int nr_extent; | ||
7528 | int ret; | ||
7529 | |||
7530 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); | ||
7531 | if (!new_extent) | ||
7532 | return -ENOMEM; | ||
7533 | |||
7534 | ref = btrfs_lookup_leaf_ref(root, leaf->start); | ||
7535 | BUG_ON(!ref); | ||
7536 | |||
7537 | ext_index = -1; | ||
7538 | nritems = btrfs_header_nritems(leaf); | ||
7539 | for (i = 0; i < nritems; i++) { | ||
7540 | btrfs_item_key_to_cpu(leaf, &key, i); | ||
7541 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | ||
7542 | continue; | ||
7543 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | ||
7544 | if (btrfs_file_extent_type(leaf, fi) == | ||
7545 | BTRFS_FILE_EXTENT_INLINE) | ||
7546 | continue; | ||
7547 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | ||
7548 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | ||
7549 | if (bytenr == 0) | ||
7550 | continue; | ||
7551 | |||
7552 | ext_index++; | ||
7553 | if (bytenr >= group->key.objectid + group->key.offset || | ||
7554 | bytenr + num_bytes <= group->key.objectid) | ||
7555 | continue; | ||
7556 | |||
7557 | extent_key.objectid = bytenr; | ||
7558 | extent_key.offset = num_bytes; | ||
7559 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; | ||
7560 | nr_extent = 1; | ||
7561 | ret = get_new_locations(reloc_inode, &extent_key, | ||
7562 | group->key.objectid, 1, | ||
7563 | &new_extent, &nr_extent); | ||
7564 | if (ret > 0) | ||
7565 | continue; | ||
7566 | BUG_ON(ret < 0); | ||
7567 | |||
7568 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); | ||
7569 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); | ||
7570 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; | ||
7571 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; | ||
7572 | |||
7573 | btrfs_set_file_extent_disk_bytenr(leaf, fi, | ||
7574 | new_extent->disk_bytenr); | ||
7575 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, | ||
7576 | new_extent->disk_num_bytes); | ||
7577 | btrfs_mark_buffer_dirty(leaf); | ||
7578 | |||
7579 | ret = btrfs_inc_extent_ref(trans, root, | ||
7580 | new_extent->disk_bytenr, | ||
7581 | new_extent->disk_num_bytes, | ||
7582 | leaf->start, | ||
7583 | root->root_key.objectid, | ||
7584 | trans->transid, key.objectid); | ||
7585 | BUG_ON(ret); | ||
7586 | |||
7587 | ret = btrfs_free_extent(trans, root, | ||
7588 | bytenr, num_bytes, leaf->start, | ||
7589 | btrfs_header_owner(leaf), | ||
7590 | btrfs_header_generation(leaf), | ||
7591 | key.objectid, 0); | ||
7592 | BUG_ON(ret); | ||
7593 | cond_resched(); | ||
7594 | } | ||
7595 | kfree(new_extent); | ||
7596 | BUG_ON(ext_index + 1 != ref->nritems); | ||
7597 | btrfs_free_leaf_ref(root, ref); | ||
7598 | return 0; | ||
7599 | } | ||
7600 | |||
7601 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, | ||
7602 | struct btrfs_root *root) | ||
7603 | { | ||
7604 | struct btrfs_root *reloc_root; | ||
7605 | int ret; | ||
7606 | |||
7607 | if (root->reloc_root) { | ||
7608 | reloc_root = root->reloc_root; | ||
7609 | root->reloc_root = NULL; | ||
7610 | list_add(&reloc_root->dead_list, | ||
7611 | &root->fs_info->dead_reloc_roots); | ||
7612 | |||
7613 | btrfs_set_root_bytenr(&reloc_root->root_item, | ||
7614 | reloc_root->node->start); | ||
7615 | btrfs_set_root_level(&root->root_item, | ||
7616 | btrfs_header_level(reloc_root->node)); | ||
7617 | memset(&reloc_root->root_item.drop_progress, 0, | ||
7618 | sizeof(struct btrfs_disk_key)); | ||
7619 | reloc_root->root_item.drop_level = 0; | ||
7620 | |||
7621 | ret = btrfs_update_root(trans, root->fs_info->tree_root, | ||
7622 | &reloc_root->root_key, | ||
7623 | &reloc_root->root_item); | ||
7624 | BUG_ON(ret); | ||
7625 | } | ||
7626 | return 0; | ||
7627 | } | ||
7628 | |||
7629 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) | ||
7630 | { | ||
7631 | struct btrfs_trans_handle *trans; | ||
7632 | struct btrfs_root *reloc_root; | ||
7633 | struct btrfs_root *prev_root = NULL; | ||
7634 | struct list_head dead_roots; | ||
7635 | int ret; | ||
7636 | unsigned long nr; | ||
7637 | |||
7638 | INIT_LIST_HEAD(&dead_roots); | ||
7639 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); | ||
7640 | |||
7641 | while (!list_empty(&dead_roots)) { | ||
7642 | reloc_root = list_entry(dead_roots.prev, | ||
7643 | struct btrfs_root, dead_list); | ||
7644 | list_del_init(&reloc_root->dead_list); | ||
7645 | |||
7646 | BUG_ON(reloc_root->commit_root != NULL); | ||
7647 | while (1) { | ||
7648 | trans = btrfs_join_transaction(root, 1); | ||
7649 | BUG_ON(IS_ERR(trans)); | ||
7650 | |||
7651 | mutex_lock(&root->fs_info->drop_mutex); | ||
7652 | ret = btrfs_drop_snapshot(trans, reloc_root); | ||
7653 | if (ret != -EAGAIN) | ||
7654 | break; | ||
7655 | mutex_unlock(&root->fs_info->drop_mutex); | ||
7656 | |||
7657 | nr = trans->blocks_used; | ||
7658 | ret = btrfs_end_transaction(trans, root); | ||
7659 | BUG_ON(ret); | ||
7660 | btrfs_btree_balance_dirty(root, nr); | ||
7661 | } | ||
7662 | |||
7663 | free_extent_buffer(reloc_root->node); | ||
7664 | |||
7665 | ret = btrfs_del_root(trans, root->fs_info->tree_root, | ||
7666 | &reloc_root->root_key); | ||
7667 | BUG_ON(ret); | ||
7668 | mutex_unlock(&root->fs_info->drop_mutex); | ||
7669 | |||
7670 | nr = trans->blocks_used; | ||
7671 | ret = btrfs_end_transaction(trans, root); | ||
7672 | BUG_ON(ret); | ||
7673 | btrfs_btree_balance_dirty(root, nr); | ||
7674 | |||
7675 | kfree(prev_root); | ||
7676 | prev_root = reloc_root; | ||
7677 | } | ||
7678 | if (prev_root) { | ||
7679 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); | ||
7680 | kfree(prev_root); | ||
7681 | } | ||
7682 | return 0; | ||
7683 | } | ||
7684 | |||
7685 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) | ||
7686 | { | ||
7687 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); | ||
7688 | return 0; | ||
7689 | } | ||
7690 | |||
7691 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) | ||
7692 | { | ||
7693 | struct btrfs_root *reloc_root; | ||
7694 | struct btrfs_trans_handle *trans; | ||
7695 | struct btrfs_key location; | ||
7696 | int found; | ||
7697 | int ret; | ||
7698 | |||
7699 | mutex_lock(&root->fs_info->tree_reloc_mutex); | ||
7700 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); | ||
7701 | BUG_ON(ret); | ||
7702 | found = !list_empty(&root->fs_info->dead_reloc_roots); | ||
7703 | mutex_unlock(&root->fs_info->tree_reloc_mutex); | ||
7704 | |||
7705 | if (found) { | ||
7706 | trans = btrfs_start_transaction(root, 1); | ||
7707 | BUG_ON(IS_ERR(trans)); | ||
7708 | ret = btrfs_commit_transaction(trans, root); | ||
7709 | BUG_ON(ret); | ||
7710 | } | ||
7711 | |||
7712 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; | ||
7713 | location.offset = (u64)-1; | ||
7714 | location.type = BTRFS_ROOT_ITEM_KEY; | ||
7715 | |||
7716 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); | ||
7717 | BUG_ON(!reloc_root); | ||
7718 | ret = btrfs_orphan_cleanup(reloc_root); | ||
7719 | BUG_ON(ret); | ||
7720 | return 0; | ||
7721 | } | ||
7722 | |||
7723 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, | ||
7724 | struct btrfs_root *root) | ||
7725 | { | ||
7726 | struct btrfs_root *reloc_root; | ||
7727 | struct extent_buffer *eb; | ||
7728 | struct btrfs_root_item *root_item; | ||
7729 | struct btrfs_key root_key; | ||
7730 | int ret; | ||
7731 | |||
7732 | BUG_ON(!root->ref_cows); | ||
7733 | if (root->reloc_root) | ||
7734 | return 0; | ||
7735 | |||
7736 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); | ||
7737 | if (!root_item) | ||
7738 | return -ENOMEM; | ||
7739 | |||
7740 | ret = btrfs_copy_root(trans, root, root->commit_root, | ||
7741 | &eb, BTRFS_TREE_RELOC_OBJECTID); | ||
7742 | BUG_ON(ret); | ||
7743 | |||
7744 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; | ||
7745 | root_key.offset = root->root_key.objectid; | ||
7746 | root_key.type = BTRFS_ROOT_ITEM_KEY; | ||
7747 | |||
7748 | memcpy(root_item, &root->root_item, sizeof(root_item)); | ||
7749 | btrfs_set_root_refs(root_item, 0); | ||
7750 | btrfs_set_root_bytenr(root_item, eb->start); | ||
7751 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); | ||
7752 | btrfs_set_root_generation(root_item, trans->transid); | ||
7753 | |||
7754 | btrfs_tree_unlock(eb); | ||
7755 | free_extent_buffer(eb); | ||
7756 | |||
7757 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, | ||
7758 | &root_key, root_item); | ||
7759 | BUG_ON(ret); | ||
7760 | kfree(root_item); | ||
7761 | |||
7762 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, | ||
7763 | &root_key); | ||
7764 | BUG_ON(IS_ERR(reloc_root)); | ||
7765 | reloc_root->last_trans = trans->transid; | ||
7766 | reloc_root->commit_root = NULL; | ||
7767 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; | ||
7768 | |||
7769 | root->reloc_root = reloc_root; | ||
7770 | return 0; | ||
7771 | } | ||
7772 | |||
7773 | /* | ||
7774 | * Core function of space balance. | ||
7775 | * | ||
7776 | * The idea is using reloc trees to relocate tree blocks in reference | ||
7777 | * counted roots. There is one reloc tree for each subvol, and all | ||
7778 | * reloc trees share same root key objectid. Reloc trees are snapshots | ||
7779 | * of the latest committed roots of subvols (root->commit_root). | ||
7780 | * | ||
7781 | * To relocate a tree block referenced by a subvol, there are two steps. | ||
7782 | * COW the block through subvol's reloc tree, then update block pointer | ||
7783 | * in the subvol to point to the new block. Since all reloc trees share | ||
7784 | * same root key objectid, doing special handing for tree blocks owned | ||
7785 | * by them is easy. Once a tree block has been COWed in one reloc tree, | ||
7786 | * we can use the resulting new block directly when the same block is | ||
7787 | * required to COW again through other reloc trees. By this way, relocated | ||
7788 | * tree blocks are shared between reloc trees, so they are also shared | ||
7789 | * between subvols. | ||
7790 | */ | ||
7791 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, | ||
7792 | struct btrfs_root *root, | ||
7793 | struct btrfs_path *path, | ||
7794 | struct btrfs_key *first_key, | ||
7795 | struct btrfs_ref_path *ref_path, | ||
7796 | struct btrfs_block_group_cache *group, | ||
7797 | struct inode *reloc_inode) | ||
7798 | { | ||
7799 | struct btrfs_root *reloc_root; | ||
7800 | struct extent_buffer *eb = NULL; | ||
7801 | struct btrfs_key *keys; | ||
7802 | u64 *nodes; | ||
7803 | int level; | ||
7804 | int shared_level; | ||
7805 | int lowest_level = 0; | ||
7806 | int ret; | ||
7807 | |||
7808 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | ||
7809 | lowest_level = ref_path->owner_objectid; | ||
7810 | |||
7811 | if (!root->ref_cows) { | ||
7812 | path->lowest_level = lowest_level; | ||
7813 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); | ||
7814 | BUG_ON(ret < 0); | ||
7815 | path->lowest_level = 0; | ||
7816 | btrfs_release_path(root, path); | ||
7817 | return 0; | ||
7818 | } | ||
7819 | |||
7820 | mutex_lock(&root->fs_info->tree_reloc_mutex); | ||
7821 | ret = init_reloc_tree(trans, root); | ||
7822 | BUG_ON(ret); | ||
7823 | reloc_root = root->reloc_root; | ||
7824 | |||
7825 | shared_level = ref_path->shared_level; | ||
7826 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; | ||
7827 | |||
7828 | keys = ref_path->node_keys; | ||
7829 | nodes = ref_path->new_nodes; | ||
7830 | memset(&keys[shared_level + 1], 0, | ||
7831 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); | ||
7832 | memset(&nodes[shared_level + 1], 0, | ||
7833 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); | ||
7834 | |||
7835 | if (nodes[lowest_level] == 0) { | ||
7836 | path->lowest_level = lowest_level; | ||
7837 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, | ||
7838 | 0, 1); | ||
7839 | BUG_ON(ret); | ||
7840 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { | ||
7841 | eb = path->nodes[level]; | ||
7842 | if (!eb || eb == reloc_root->node) | ||
7843 | break; | ||
7844 | nodes[level] = eb->start; | ||
7845 | if (level == 0) | ||
7846 | btrfs_item_key_to_cpu(eb, &keys[level], 0); | ||
7847 | else | ||
7848 | btrfs_node_key_to_cpu(eb, &keys[level], 0); | ||
7849 | } | ||
7850 | if (nodes[0] && | ||
7851 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | ||
7852 | eb = path->nodes[0]; | ||
7853 | ret = replace_extents_in_leaf(trans, reloc_root, eb, | ||
7854 | group, reloc_inode); | ||
7855 | BUG_ON(ret); | ||
7856 | } | ||
7857 | btrfs_release_path(reloc_root, path); | ||
7858 | } else { | ||
7859 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, | ||
7860 | lowest_level); | ||
7861 | BUG_ON(ret); | ||
7862 | } | ||
7863 | |||
7864 | /* | ||
7865 | * replace tree blocks in the fs tree with tree blocks in | ||
7866 | * the reloc tree. | ||
7867 | */ | ||
7868 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); | ||
7869 | BUG_ON(ret < 0); | ||
7870 | |||
7871 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | ||
7872 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, | ||
7873 | 0, 0); | ||
7874 | BUG_ON(ret); | ||
7875 | extent_buffer_get(path->nodes[0]); | ||
7876 | eb = path->nodes[0]; | ||
7877 | btrfs_release_path(reloc_root, path); | ||
7878 | ret = invalidate_extent_cache(reloc_root, eb, group, root); | ||
7879 | BUG_ON(ret); | ||
7880 | free_extent_buffer(eb); | ||
7881 | } | ||
7882 | |||
7883 | mutex_unlock(&root->fs_info->tree_reloc_mutex); | ||
7884 | path->lowest_level = 0; | ||
7885 | return 0; | ||
7886 | } | ||
7887 | |||
7888 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, | ||
7889 | struct btrfs_root *root, | ||
7890 | struct btrfs_path *path, | ||
7891 | struct btrfs_key *first_key, | ||
7892 | struct btrfs_ref_path *ref_path) | ||
7893 | { | ||
7894 | int ret; | ||
7895 | |||
7896 | ret = relocate_one_path(trans, root, path, first_key, | ||
7897 | ref_path, NULL, NULL); | ||
7898 | BUG_ON(ret); | ||
7899 | |||
7900 | return 0; | ||
7901 | } | ||
7902 | |||
7903 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, | ||
7904 | struct btrfs_root *extent_root, | ||
7905 | struct btrfs_path *path, | ||
7906 | struct btrfs_key *extent_key) | ||
7907 | { | ||
7908 | int ret; | ||
7909 | |||
7910 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); | ||
7911 | if (ret) | ||
7912 | goto out; | ||
7913 | ret = btrfs_del_item(trans, extent_root, path); | ||
7914 | out: | ||
7915 | btrfs_release_path(extent_root, path); | ||
7916 | return ret; | ||
7917 | } | ||
7918 | |||
7919 | static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info, | ||
7920 | struct btrfs_ref_path *ref_path) | ||
7921 | { | ||
7922 | struct btrfs_key root_key; | ||
7923 | |||
7924 | root_key.objectid = ref_path->root_objectid; | ||
7925 | root_key.type = BTRFS_ROOT_ITEM_KEY; | ||
7926 | if (is_cowonly_root(ref_path->root_objectid)) | ||
7927 | root_key.offset = 0; | ||
7928 | else | ||
7929 | root_key.offset = (u64)-1; | ||
7930 | |||
7931 | return btrfs_read_fs_root_no_name(fs_info, &root_key); | ||
7932 | } | ||
7933 | |||
7934 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, | ||
7935 | struct btrfs_path *path, | ||
7936 | struct btrfs_key *extent_key, | ||
7937 | struct btrfs_block_group_cache *group, | ||
7938 | struct inode *reloc_inode, int pass) | ||
7939 | { | ||
7940 | struct btrfs_trans_handle *trans; | ||
7941 | struct btrfs_root *found_root; | ||
7942 | struct btrfs_ref_path *ref_path = NULL; | ||
7943 | struct disk_extent *new_extents = NULL; | ||
7944 | int nr_extents = 0; | ||
7945 | int loops; | ||
7946 | int ret; | ||
7947 | int level; | ||
7948 | struct btrfs_key first_key; | ||
7949 | u64 prev_block = 0; | ||
7950 | |||
7951 | |||
7952 | trans = btrfs_start_transaction(extent_root, 1); | ||
7953 | BUG_ON(IS_ERR(trans)); | ||
7954 | |||
7955 | if (extent_key->objectid == 0) { | ||
7956 | ret = del_extent_zero(trans, extent_root, path, extent_key); | ||
7957 | goto out; | ||
7958 | } | ||
7959 | |||
7960 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); | ||
7961 | if (!ref_path) { | ||
7962 | ret = -ENOMEM; | ||
7963 | goto out; | ||
7964 | } | ||
7965 | |||
7966 | for (loops = 0; ; loops++) { | ||
7967 | if (loops == 0) { | ||
7968 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, | ||
7969 | extent_key->objectid); | ||
7970 | } else { | ||
7971 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); | ||
7972 | } | ||
7973 | if (ret < 0) | ||
7974 | goto out; | ||
7975 | if (ret > 0) | ||
7976 | break; | ||
7977 | |||
7978 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || | ||
7979 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) | ||
7980 | continue; | ||
7981 | |||
7982 | found_root = read_ref_root(extent_root->fs_info, ref_path); | ||
7983 | BUG_ON(!found_root); | ||
7984 | /* | ||
7985 | * for reference counted tree, only process reference paths | ||
7986 | * rooted at the latest committed root. | ||
7987 | */ | ||
7988 | if (found_root->ref_cows && | ||
7989 | ref_path->root_generation != found_root->root_key.offset) | ||
7990 | continue; | ||
7991 | |||
7992 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | ||
7993 | if (pass == 0) { | ||
7994 | /* | ||
7995 | * copy data extents to new locations | ||
7996 | */ | ||
7997 | u64 group_start = group->key.objectid; | ||
7998 | ret = relocate_data_extent(reloc_inode, | ||
7999 | extent_key, | ||
8000 | group_start); | ||
8001 | if (ret < 0) | ||
8002 | goto out; | ||
8003 | break; | ||
8004 | } | ||
8005 | level = 0; | ||
8006 | } else { | ||
8007 | level = ref_path->owner_objectid; | ||
8008 | } | ||
8009 | |||
8010 | if (prev_block != ref_path->nodes[level]) { | ||
8011 | struct extent_buffer *eb; | ||
8012 | u64 block_start = ref_path->nodes[level]; | ||
8013 | u64 block_size = btrfs_level_size(found_root, level); | ||
8014 | |||
8015 | eb = read_tree_block(found_root, block_start, | ||
8016 | block_size, 0); | ||
8017 | if (!eb) { | ||
8018 | ret = -EIO; | ||
8019 | goto out; | ||
8020 | } | ||
8021 | btrfs_tree_lock(eb); | ||
8022 | BUG_ON(level != btrfs_header_level(eb)); | ||
8023 | |||
8024 | if (level == 0) | ||
8025 | btrfs_item_key_to_cpu(eb, &first_key, 0); | ||
8026 | else | ||
8027 | btrfs_node_key_to_cpu(eb, &first_key, 0); | ||
8028 | |||
8029 | btrfs_tree_unlock(eb); | ||
8030 | free_extent_buffer(eb); | ||
8031 | prev_block = block_start; | ||
8032 | } | ||
8033 | |||
8034 | mutex_lock(&extent_root->fs_info->trans_mutex); | ||
8035 | btrfs_record_root_in_trans(found_root); | ||
8036 | mutex_unlock(&extent_root->fs_info->trans_mutex); | ||
8037 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | ||
8038 | /* | ||
8039 | * try to update data extent references while | ||
8040 | * keeping metadata shared between snapshots. | ||
8041 | */ | ||
8042 | if (pass == 1) { | ||
8043 | ret = relocate_one_path(trans, found_root, | ||
8044 | path, &first_key, ref_path, | ||
8045 | group, reloc_inode); | ||
8046 | if (ret < 0) | ||
8047 | goto out; | ||
8048 | continue; | ||
8049 | } | ||
8050 | /* | ||
8051 | * use fallback method to process the remaining | ||
8052 | * references. | ||
8053 | */ | ||
8054 | if (!new_extents) { | ||
8055 | u64 group_start = group->key.objectid; | ||
8056 | new_extents = kmalloc(sizeof(*new_extents), | ||
8057 | GFP_NOFS); | ||
8058 | if (!new_extents) { | ||
8059 | ret = -ENOMEM; | ||
8060 | goto out; | ||
8061 | } | ||
8062 | nr_extents = 1; | ||
8063 | ret = get_new_locations(reloc_inode, | ||
8064 | extent_key, | ||
8065 | group_start, 1, | ||
8066 | &new_extents, | ||
8067 | &nr_extents); | ||
8068 | if (ret) | ||
8069 | goto out; | ||
8070 | } | ||
8071 | ret = replace_one_extent(trans, found_root, | ||
8072 | path, extent_key, | ||
8073 | &first_key, ref_path, | ||
8074 | new_extents, nr_extents); | ||
8075 | } else { | ||
8076 | ret = relocate_tree_block(trans, found_root, path, | ||
8077 | &first_key, ref_path); | ||
8078 | } | ||
8079 | if (ret < 0) | ||
8080 | goto out; | ||
8081 | } | ||
8082 | ret = 0; | ||
8083 | out: | ||
8084 | btrfs_end_transaction(trans, extent_root); | ||
8085 | kfree(new_extents); | ||
8086 | kfree(ref_path); | ||
8087 | return ret; | ||
8088 | } | ||
8089 | #endif | ||
8090 | |||
8091 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) | 6444 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
8092 | { | 6445 | { |
8093 | u64 num_devices; | 6446 | u64 num_devices; |
@@ -8574,7 +6927,7 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
8574 | memcpy(&cache->key, &found_key, sizeof(found_key)); | 6927 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
8575 | 6928 | ||
8576 | key.objectid = found_key.objectid + found_key.offset; | 6929 | key.objectid = found_key.objectid + found_key.offset; |
8577 | btrfs_release_path(root, path); | 6930 | btrfs_release_path(path); |
8578 | cache->flags = btrfs_block_group_flags(&cache->item); | 6931 | cache->flags = btrfs_block_group_flags(&cache->item); |
8579 | cache->sectorsize = root->sectorsize; | 6932 | cache->sectorsize = root->sectorsize; |
8580 | 6933 | ||
@@ -8798,12 +7151,12 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, | |||
8798 | if (ret < 0) | 7151 | if (ret < 0) |
8799 | goto out; | 7152 | goto out; |
8800 | if (ret > 0) | 7153 | if (ret > 0) |
8801 | btrfs_release_path(tree_root, path); | 7154 | btrfs_release_path(path); |
8802 | if (ret == 0) { | 7155 | if (ret == 0) { |
8803 | ret = btrfs_del_item(trans, tree_root, path); | 7156 | ret = btrfs_del_item(trans, tree_root, path); |
8804 | if (ret) | 7157 | if (ret) |
8805 | goto out; | 7158 | goto out; |
8806 | btrfs_release_path(tree_root, path); | 7159 | btrfs_release_path(path); |
8807 | } | 7160 | } |
8808 | 7161 | ||
8809 | spin_lock(&root->fs_info->block_group_cache_lock); | 7162 | spin_lock(&root->fs_info->block_group_cache_lock); |