diff options
Diffstat (limited to 'fs/btrfs/relocation.c')
-rw-r--r-- | fs/btrfs/relocation.c | 74 |
1 files changed, 57 insertions, 17 deletions
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 50695dc5e2ab..b67171e6d688 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c | |||
@@ -1269,6 +1269,8 @@ static int __update_reloc_root(struct btrfs_root *root, int del) | |||
1269 | } | 1269 | } |
1270 | spin_unlock(&rc->reloc_root_tree.lock); | 1270 | spin_unlock(&rc->reloc_root_tree.lock); |
1271 | 1271 | ||
1272 | if (!node) | ||
1273 | return 0; | ||
1272 | BUG_ON((struct btrfs_root *)node->data != root); | 1274 | BUG_ON((struct btrfs_root *)node->data != root); |
1273 | 1275 | ||
1274 | if (!del) { | 1276 | if (!del) { |
@@ -2238,13 +2240,28 @@ again: | |||
2238 | } | 2240 | } |
2239 | 2241 | ||
2240 | static noinline_for_stack | 2242 | static noinline_for_stack |
2243 | void free_reloc_roots(struct list_head *list) | ||
2244 | { | ||
2245 | struct btrfs_root *reloc_root; | ||
2246 | |||
2247 | while (!list_empty(list)) { | ||
2248 | reloc_root = list_entry(list->next, struct btrfs_root, | ||
2249 | root_list); | ||
2250 | __update_reloc_root(reloc_root, 1); | ||
2251 | free_extent_buffer(reloc_root->node); | ||
2252 | free_extent_buffer(reloc_root->commit_root); | ||
2253 | kfree(reloc_root); | ||
2254 | } | ||
2255 | } | ||
2256 | |||
2257 | static noinline_for_stack | ||
2241 | int merge_reloc_roots(struct reloc_control *rc) | 2258 | int merge_reloc_roots(struct reloc_control *rc) |
2242 | { | 2259 | { |
2243 | struct btrfs_root *root; | 2260 | struct btrfs_root *root; |
2244 | struct btrfs_root *reloc_root; | 2261 | struct btrfs_root *reloc_root; |
2245 | LIST_HEAD(reloc_roots); | 2262 | LIST_HEAD(reloc_roots); |
2246 | int found = 0; | 2263 | int found = 0; |
2247 | int ret; | 2264 | int ret = 0; |
2248 | again: | 2265 | again: |
2249 | root = rc->extent_root; | 2266 | root = rc->extent_root; |
2250 | 2267 | ||
@@ -2270,20 +2287,33 @@ again: | |||
2270 | BUG_ON(root->reloc_root != reloc_root); | 2287 | BUG_ON(root->reloc_root != reloc_root); |
2271 | 2288 | ||
2272 | ret = merge_reloc_root(rc, root); | 2289 | ret = merge_reloc_root(rc, root); |
2273 | BUG_ON(ret); | 2290 | if (ret) |
2291 | goto out; | ||
2274 | } else { | 2292 | } else { |
2275 | list_del_init(&reloc_root->root_list); | 2293 | list_del_init(&reloc_root->root_list); |
2276 | } | 2294 | } |
2277 | ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1); | 2295 | ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1); |
2278 | BUG_ON(ret < 0); | 2296 | if (ret < 0) { |
2297 | if (list_empty(&reloc_root->root_list)) | ||
2298 | list_add_tail(&reloc_root->root_list, | ||
2299 | &reloc_roots); | ||
2300 | goto out; | ||
2301 | } | ||
2279 | } | 2302 | } |
2280 | 2303 | ||
2281 | if (found) { | 2304 | if (found) { |
2282 | found = 0; | 2305 | found = 0; |
2283 | goto again; | 2306 | goto again; |
2284 | } | 2307 | } |
2308 | out: | ||
2309 | if (ret) { | ||
2310 | btrfs_std_error(root->fs_info, ret); | ||
2311 | if (!list_empty(&reloc_roots)) | ||
2312 | free_reloc_roots(&reloc_roots); | ||
2313 | } | ||
2314 | |||
2285 | BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); | 2315 | BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); |
2286 | return 0; | 2316 | return ret; |
2287 | } | 2317 | } |
2288 | 2318 | ||
2289 | static void free_block_list(struct rb_root *blocks) | 2319 | static void free_block_list(struct rb_root *blocks) |
@@ -2818,8 +2848,10 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans, | |||
2818 | int err = 0; | 2848 | int err = 0; |
2819 | 2849 | ||
2820 | path = btrfs_alloc_path(); | 2850 | path = btrfs_alloc_path(); |
2821 | if (!path) | 2851 | if (!path) { |
2822 | return -ENOMEM; | 2852 | err = -ENOMEM; |
2853 | goto out_path; | ||
2854 | } | ||
2823 | 2855 | ||
2824 | rb_node = rb_first(blocks); | 2856 | rb_node = rb_first(blocks); |
2825 | while (rb_node) { | 2857 | while (rb_node) { |
@@ -2858,10 +2890,11 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans, | |||
2858 | rb_node = rb_next(rb_node); | 2890 | rb_node = rb_next(rb_node); |
2859 | } | 2891 | } |
2860 | out: | 2892 | out: |
2861 | free_block_list(blocks); | ||
2862 | err = finish_pending_nodes(trans, rc, path, err); | 2893 | err = finish_pending_nodes(trans, rc, path, err); |
2863 | 2894 | ||
2864 | btrfs_free_path(path); | 2895 | btrfs_free_path(path); |
2896 | out_path: | ||
2897 | free_block_list(blocks); | ||
2865 | return err; | 2898 | return err; |
2866 | } | 2899 | } |
2867 | 2900 | ||
@@ -3698,7 +3731,15 @@ int prepare_to_relocate(struct reloc_control *rc) | |||
3698 | set_reloc_control(rc); | 3731 | set_reloc_control(rc); |
3699 | 3732 | ||
3700 | trans = btrfs_join_transaction(rc->extent_root); | 3733 | trans = btrfs_join_transaction(rc->extent_root); |
3701 | BUG_ON(IS_ERR(trans)); | 3734 | if (IS_ERR(trans)) { |
3735 | unset_reloc_control(rc); | ||
3736 | /* | ||
3737 | * extent tree is not a ref_cow tree and has no reloc_root to | ||
3738 | * cleanup. And callers are responsible to free the above | ||
3739 | * block rsv. | ||
3740 | */ | ||
3741 | return PTR_ERR(trans); | ||
3742 | } | ||
3702 | btrfs_commit_transaction(trans, rc->extent_root); | 3743 | btrfs_commit_transaction(trans, rc->extent_root); |
3703 | return 0; | 3744 | return 0; |
3704 | } | 3745 | } |
@@ -3730,7 +3771,11 @@ static noinline_for_stack int relocate_block_group(struct reloc_control *rc) | |||
3730 | while (1) { | 3771 | while (1) { |
3731 | progress++; | 3772 | progress++; |
3732 | trans = btrfs_start_transaction(rc->extent_root, 0); | 3773 | trans = btrfs_start_transaction(rc->extent_root, 0); |
3733 | BUG_ON(IS_ERR(trans)); | 3774 | if (IS_ERR(trans)) { |
3775 | err = PTR_ERR(trans); | ||
3776 | trans = NULL; | ||
3777 | break; | ||
3778 | } | ||
3734 | restart: | 3779 | restart: |
3735 | if (update_backref_cache(trans, &rc->backref_cache)) { | 3780 | if (update_backref_cache(trans, &rc->backref_cache)) { |
3736 | btrfs_end_transaction(trans, rc->extent_root); | 3781 | btrfs_end_transaction(trans, rc->extent_root); |
@@ -4264,14 +4309,9 @@ int btrfs_recover_relocation(struct btrfs_root *root) | |||
4264 | out_free: | 4309 | out_free: |
4265 | kfree(rc); | 4310 | kfree(rc); |
4266 | out: | 4311 | out: |
4267 | while (!list_empty(&reloc_roots)) { | 4312 | if (!list_empty(&reloc_roots)) |
4268 | reloc_root = list_entry(reloc_roots.next, | 4313 | free_reloc_roots(&reloc_roots); |
4269 | struct btrfs_root, root_list); | 4314 | |
4270 | list_del(&reloc_root->root_list); | ||
4271 | free_extent_buffer(reloc_root->node); | ||
4272 | free_extent_buffer(reloc_root->commit_root); | ||
4273 | kfree(reloc_root); | ||
4274 | } | ||
4275 | btrfs_free_path(path); | 4315 | btrfs_free_path(path); |
4276 | 4316 | ||
4277 | if (err == 0) { | 4317 | if (err == 0) { |