diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 162 |
1 files changed, 100 insertions, 62 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 169bd62ce776..1f61bf5b4960 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -348,7 +348,7 @@ static int caching_kthread(void *data) | |||
348 | */ | 348 | */ |
349 | path->skip_locking = 1; | 349 | path->skip_locking = 1; |
350 | path->search_commit_root = 1; | 350 | path->search_commit_root = 1; |
351 | path->reada = 2; | 351 | path->reada = 1; |
352 | 352 | ||
353 | key.objectid = last; | 353 | key.objectid = last; |
354 | key.offset = 0; | 354 | key.offset = 0; |
@@ -366,8 +366,7 @@ again: | |||
366 | nritems = btrfs_header_nritems(leaf); | 366 | nritems = btrfs_header_nritems(leaf); |
367 | 367 | ||
368 | while (1) { | 368 | while (1) { |
369 | smp_mb(); | 369 | if (btrfs_fs_closing(fs_info) > 1) { |
370 | if (fs_info->closing > 1) { | ||
371 | last = (u64)-1; | 370 | last = (u64)-1; |
372 | break; | 371 | break; |
373 | } | 372 | } |
@@ -379,15 +378,18 @@ again: | |||
379 | if (ret) | 378 | if (ret) |
380 | break; | 379 | break; |
381 | 380 | ||
382 | caching_ctl->progress = last; | 381 | if (need_resched() || |
383 | btrfs_release_path(path); | 382 | btrfs_next_leaf(extent_root, path)) { |
384 | up_read(&fs_info->extent_commit_sem); | 383 | caching_ctl->progress = last; |
385 | mutex_unlock(&caching_ctl->mutex); | 384 | btrfs_release_path(path); |
386 | if (btrfs_transaction_in_commit(fs_info)) | 385 | up_read(&fs_info->extent_commit_sem); |
387 | schedule_timeout(1); | 386 | mutex_unlock(&caching_ctl->mutex); |
388 | else | ||
389 | cond_resched(); | 387 | cond_resched(); |
390 | goto again; | 388 | goto again; |
389 | } | ||
390 | leaf = path->nodes[0]; | ||
391 | nritems = btrfs_header_nritems(leaf); | ||
392 | continue; | ||
391 | } | 393 | } |
392 | 394 | ||
393 | if (key.objectid < block_group->key.objectid) { | 395 | if (key.objectid < block_group->key.objectid) { |
@@ -3065,7 +3067,7 @@ again: | |||
3065 | spin_unlock(&data_sinfo->lock); | 3067 | spin_unlock(&data_sinfo->lock); |
3066 | alloc: | 3068 | alloc: |
3067 | alloc_target = btrfs_get_alloc_profile(root, 1); | 3069 | alloc_target = btrfs_get_alloc_profile(root, 1); |
3068 | trans = btrfs_join_transaction(root, 1); | 3070 | trans = btrfs_join_transaction(root); |
3069 | if (IS_ERR(trans)) | 3071 | if (IS_ERR(trans)) |
3070 | return PTR_ERR(trans); | 3072 | return PTR_ERR(trans); |
3071 | 3073 | ||
@@ -3087,13 +3089,21 @@ alloc: | |||
3087 | } | 3089 | } |
3088 | goto again; | 3090 | goto again; |
3089 | } | 3091 | } |
3092 | |||
3093 | /* | ||
3094 | * If we have less pinned bytes than we want to allocate then | ||
3095 | * don't bother committing the transaction, it won't help us. | ||
3096 | */ | ||
3097 | if (data_sinfo->bytes_pinned < bytes) | ||
3098 | committed = 1; | ||
3090 | spin_unlock(&data_sinfo->lock); | 3099 | spin_unlock(&data_sinfo->lock); |
3091 | 3100 | ||
3092 | /* commit the current transaction and try again */ | 3101 | /* commit the current transaction and try again */ |
3093 | commit_trans: | 3102 | commit_trans: |
3094 | if (!committed && !root->fs_info->open_ioctl_trans) { | 3103 | if (!committed && |
3104 | !atomic_read(&root->fs_info->open_ioctl_trans)) { | ||
3095 | committed = 1; | 3105 | committed = 1; |
3096 | trans = btrfs_join_transaction(root, 1); | 3106 | trans = btrfs_join_transaction(root); |
3097 | if (IS_ERR(trans)) | 3107 | if (IS_ERR(trans)) |
3098 | return PTR_ERR(trans); | 3108 | return PTR_ERR(trans); |
3099 | ret = btrfs_commit_transaction(trans, root); | 3109 | ret = btrfs_commit_transaction(trans, root); |
@@ -3304,10 +3314,6 @@ static int shrink_delalloc(struct btrfs_trans_handle *trans, | |||
3304 | if (reserved == 0) | 3314 | if (reserved == 0) |
3305 | return 0; | 3315 | return 0; |
3306 | 3316 | ||
3307 | /* nothing to shrink - nothing to reclaim */ | ||
3308 | if (root->fs_info->delalloc_bytes == 0) | ||
3309 | return 0; | ||
3310 | |||
3311 | max_reclaim = min(reserved, to_reclaim); | 3317 | max_reclaim = min(reserved, to_reclaim); |
3312 | 3318 | ||
3313 | while (loops < 1024) { | 3319 | while (loops < 1024) { |
@@ -3472,7 +3478,7 @@ again: | |||
3472 | goto out; | 3478 | goto out; |
3473 | 3479 | ||
3474 | ret = -ENOSPC; | 3480 | ret = -ENOSPC; |
3475 | trans = btrfs_join_transaction(root, 1); | 3481 | trans = btrfs_join_transaction(root); |
3476 | if (IS_ERR(trans)) | 3482 | if (IS_ERR(trans)) |
3477 | goto out; | 3483 | goto out; |
3478 | ret = btrfs_commit_transaction(trans, root); | 3484 | ret = btrfs_commit_transaction(trans, root); |
@@ -3699,7 +3705,7 @@ int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, | |||
3699 | if (trans) | 3705 | if (trans) |
3700 | return -EAGAIN; | 3706 | return -EAGAIN; |
3701 | 3707 | ||
3702 | trans = btrfs_join_transaction(root, 1); | 3708 | trans = btrfs_join_transaction(root); |
3703 | BUG_ON(IS_ERR(trans)); | 3709 | BUG_ON(IS_ERR(trans)); |
3704 | ret = btrfs_commit_transaction(trans, root); | 3710 | ret = btrfs_commit_transaction(trans, root); |
3705 | return 0; | 3711 | return 0; |
@@ -3837,6 +3843,37 @@ static void release_global_block_rsv(struct btrfs_fs_info *fs_info) | |||
3837 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); | 3843 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
3838 | } | 3844 | } |
3839 | 3845 | ||
3846 | int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans, | ||
3847 | struct btrfs_root *root, | ||
3848 | struct btrfs_block_rsv *rsv) | ||
3849 | { | ||
3850 | struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv; | ||
3851 | u64 num_bytes; | ||
3852 | int ret; | ||
3853 | |||
3854 | /* | ||
3855 | * Truncate should be freeing data, but give us 2 items just in case it | ||
3856 | * needs to use some space. We may want to be smarter about this in the | ||
3857 | * future. | ||
3858 | */ | ||
3859 | num_bytes = btrfs_calc_trans_metadata_size(root, 2); | ||
3860 | |||
3861 | /* We already have enough bytes, just return */ | ||
3862 | if (rsv->reserved >= num_bytes) | ||
3863 | return 0; | ||
3864 | |||
3865 | num_bytes -= rsv->reserved; | ||
3866 | |||
3867 | /* | ||
3868 | * You should have reserved enough space before hand to do this, so this | ||
3869 | * should not fail. | ||
3870 | */ | ||
3871 | ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes); | ||
3872 | BUG_ON(ret); | ||
3873 | |||
3874 | return 0; | ||
3875 | } | ||
3876 | |||
3840 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, | 3877 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
3841 | struct btrfs_root *root, | 3878 | struct btrfs_root *root, |
3842 | int num_items) | 3879 | int num_items) |
@@ -3877,23 +3914,18 @@ int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, | |||
3877 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; | 3914 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
3878 | 3915 | ||
3879 | /* | 3916 | /* |
3880 | * one for deleting orphan item, one for updating inode and | 3917 | * We need to hold space in order to delete our orphan item once we've |
3881 | * two for calling btrfs_truncate_inode_items. | 3918 | * added it, so this takes the reservation so we can release it later |
3882 | * | 3919 | * when we are truly done with the orphan item. |
3883 | * btrfs_truncate_inode_items is a delete operation, it frees | ||
3884 | * more space than it uses in most cases. So two units of | ||
3885 | * metadata space should be enough for calling it many times. | ||
3886 | * If all of the metadata space is used, we can commit | ||
3887 | * transaction and use space it freed. | ||
3888 | */ | 3920 | */ |
3889 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4); | 3921 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
3890 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); | 3922 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
3891 | } | 3923 | } |
3892 | 3924 | ||
3893 | void btrfs_orphan_release_metadata(struct inode *inode) | 3925 | void btrfs_orphan_release_metadata(struct inode *inode) |
3894 | { | 3926 | { |
3895 | struct btrfs_root *root = BTRFS_I(inode)->root; | 3927 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3896 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4); | 3928 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
3897 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); | 3929 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
3898 | } | 3930 | } |
3899 | 3931 | ||
@@ -4987,6 +5019,15 @@ have_block_group: | |||
4987 | if (unlikely(block_group->ro)) | 5019 | if (unlikely(block_group->ro)) |
4988 | goto loop; | 5020 | goto loop; |
4989 | 5021 | ||
5022 | spin_lock(&block_group->free_space_ctl->tree_lock); | ||
5023 | if (cached && | ||
5024 | block_group->free_space_ctl->free_space < | ||
5025 | num_bytes + empty_size) { | ||
5026 | spin_unlock(&block_group->free_space_ctl->tree_lock); | ||
5027 | goto loop; | ||
5028 | } | ||
5029 | spin_unlock(&block_group->free_space_ctl->tree_lock); | ||
5030 | |||
4990 | /* | 5031 | /* |
4991 | * Ok we want to try and use the cluster allocator, so lets look | 5032 | * Ok we want to try and use the cluster allocator, so lets look |
4992 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will | 5033 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
@@ -5150,6 +5191,7 @@ checks: | |||
5150 | btrfs_add_free_space(block_group, offset, | 5191 | btrfs_add_free_space(block_group, offset, |
5151 | search_start - offset); | 5192 | search_start - offset); |
5152 | BUG_ON(offset > search_start); | 5193 | BUG_ON(offset > search_start); |
5194 | btrfs_put_block_group(block_group); | ||
5153 | break; | 5195 | break; |
5154 | loop: | 5196 | loop: |
5155 | failed_cluster_refill = false; | 5197 | failed_cluster_refill = false; |
@@ -5172,9 +5214,7 @@ loop: | |||
5172 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try | 5214 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
5173 | * again | 5215 | * again |
5174 | */ | 5216 | */ |
5175 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && | 5217 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
5176 | (found_uncached_bg || empty_size || empty_cluster || | ||
5177 | allowed_chunk_alloc)) { | ||
5178 | index = 0; | 5218 | index = 0; |
5179 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { | 5219 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
5180 | found_uncached_bg = false; | 5220 | found_uncached_bg = false; |
@@ -5214,42 +5254,39 @@ loop: | |||
5214 | goto search; | 5254 | goto search; |
5215 | } | 5255 | } |
5216 | 5256 | ||
5217 | if (loop < LOOP_CACHING_WAIT) { | 5257 | loop++; |
5218 | loop++; | ||
5219 | goto search; | ||
5220 | } | ||
5221 | 5258 | ||
5222 | if (loop == LOOP_ALLOC_CHUNK) { | 5259 | if (loop == LOOP_ALLOC_CHUNK) { |
5223 | empty_size = 0; | 5260 | if (allowed_chunk_alloc) { |
5224 | empty_cluster = 0; | 5261 | ret = do_chunk_alloc(trans, root, num_bytes + |
5225 | } | 5262 | 2 * 1024 * 1024, data, |
5263 | CHUNK_ALLOC_LIMITED); | ||
5264 | allowed_chunk_alloc = 0; | ||
5265 | if (ret == 1) | ||
5266 | done_chunk_alloc = 1; | ||
5267 | } else if (!done_chunk_alloc && | ||
5268 | space_info->force_alloc == | ||
5269 | CHUNK_ALLOC_NO_FORCE) { | ||
5270 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; | ||
5271 | } | ||
5226 | 5272 | ||
5227 | if (allowed_chunk_alloc) { | 5273 | /* |
5228 | ret = do_chunk_alloc(trans, root, num_bytes + | 5274 | * We didn't allocate a chunk, go ahead and drop the |
5229 | 2 * 1024 * 1024, data, | 5275 | * empty size and loop again. |
5230 | CHUNK_ALLOC_LIMITED); | 5276 | */ |
5231 | allowed_chunk_alloc = 0; | 5277 | if (!done_chunk_alloc) |
5232 | done_chunk_alloc = 1; | 5278 | loop = LOOP_NO_EMPTY_SIZE; |
5233 | } else if (!done_chunk_alloc && | ||
5234 | space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) { | ||
5235 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; | ||
5236 | } | 5279 | } |
5237 | 5280 | ||
5238 | if (loop < LOOP_NO_EMPTY_SIZE) { | 5281 | if (loop == LOOP_NO_EMPTY_SIZE) { |
5239 | loop++; | 5282 | empty_size = 0; |
5240 | goto search; | 5283 | empty_cluster = 0; |
5241 | } | 5284 | } |
5242 | ret = -ENOSPC; | 5285 | |
5286 | goto search; | ||
5243 | } else if (!ins->objectid) { | 5287 | } else if (!ins->objectid) { |
5244 | ret = -ENOSPC; | 5288 | ret = -ENOSPC; |
5245 | } | 5289 | } else if (ins->objectid) { |
5246 | |||
5247 | /* we found what we needed */ | ||
5248 | if (ins->objectid) { | ||
5249 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) | ||
5250 | trans->block_group = block_group->key.objectid; | ||
5251 | |||
5252 | btrfs_put_block_group(block_group); | ||
5253 | ret = 0; | 5290 | ret = 0; |
5254 | } | 5291 | } |
5255 | 5292 | ||
@@ -6526,7 +6563,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root, | |||
6526 | 6563 | ||
6527 | BUG_ON(cache->ro); | 6564 | BUG_ON(cache->ro); |
6528 | 6565 | ||
6529 | trans = btrfs_join_transaction(root, 1); | 6566 | trans = btrfs_join_transaction(root); |
6530 | BUG_ON(IS_ERR(trans)); | 6567 | BUG_ON(IS_ERR(trans)); |
6531 | 6568 | ||
6532 | alloc_flags = update_block_group_flags(root, cache->flags); | 6569 | alloc_flags = update_block_group_flags(root, cache->flags); |
@@ -6882,6 +6919,7 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
6882 | path = btrfs_alloc_path(); | 6919 | path = btrfs_alloc_path(); |
6883 | if (!path) | 6920 | if (!path) |
6884 | return -ENOMEM; | 6921 | return -ENOMEM; |
6922 | path->reada = 1; | ||
6885 | 6923 | ||
6886 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); | 6924 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
6887 | if (cache_gen != 0 && | 6925 | if (cache_gen != 0 && |