aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c158
1 files changed, 100 insertions, 58 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 169bd62ce776..b42efc2ded51 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);
3066alloc: 3068alloc:
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 */
3093commit_trans: 3102commit_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);
@@ -3472,7 +3482,7 @@ again:
3472 goto out; 3482 goto out;
3473 3483
3474 ret = -ENOSPC; 3484 ret = -ENOSPC;
3475 trans = btrfs_join_transaction(root, 1); 3485 trans = btrfs_join_transaction(root);
3476 if (IS_ERR(trans)) 3486 if (IS_ERR(trans))
3477 goto out; 3487 goto out;
3478 ret = btrfs_commit_transaction(trans, root); 3488 ret = btrfs_commit_transaction(trans, root);
@@ -3699,7 +3709,7 @@ int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3699 if (trans) 3709 if (trans)
3700 return -EAGAIN; 3710 return -EAGAIN;
3701 3711
3702 trans = btrfs_join_transaction(root, 1); 3712 trans = btrfs_join_transaction(root);
3703 BUG_ON(IS_ERR(trans)); 3713 BUG_ON(IS_ERR(trans));
3704 ret = btrfs_commit_transaction(trans, root); 3714 ret = btrfs_commit_transaction(trans, root);
3705 return 0; 3715 return 0;
@@ -3837,6 +3847,37 @@ static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3837 WARN_ON(fs_info->chunk_block_rsv.reserved > 0); 3847 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3838} 3848}
3839 3849
3850int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3851 struct btrfs_root *root,
3852 struct btrfs_block_rsv *rsv)
3853{
3854 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3855 u64 num_bytes;
3856 int ret;
3857
3858 /*
3859 * Truncate should be freeing data, but give us 2 items just in case it
3860 * needs to use some space. We may want to be smarter about this in the
3861 * future.
3862 */
3863 num_bytes = btrfs_calc_trans_metadata_size(root, 2);
3864
3865 /* We already have enough bytes, just return */
3866 if (rsv->reserved >= num_bytes)
3867 return 0;
3868
3869 num_bytes -= rsv->reserved;
3870
3871 /*
3872 * You should have reserved enough space before hand to do this, so this
3873 * should not fail.
3874 */
3875 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
3876 BUG_ON(ret);
3877
3878 return 0;
3879}
3880
3840int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, 3881int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3841 struct btrfs_root *root, 3882 struct btrfs_root *root,
3842 int num_items) 3883 int num_items)
@@ -3877,23 +3918,18 @@ int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3877 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; 3918 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3878 3919
3879 /* 3920 /*
3880 * one for deleting orphan item, one for updating inode and 3921 * We need to hold space in order to delete our orphan item once we've
3881 * two for calling btrfs_truncate_inode_items. 3922 * added it, so this takes the reservation so we can release it later
3882 * 3923 * 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 */ 3924 */
3889 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4); 3925 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
3890 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); 3926 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3891} 3927}
3892 3928
3893void btrfs_orphan_release_metadata(struct inode *inode) 3929void btrfs_orphan_release_metadata(struct inode *inode)
3894{ 3930{
3895 struct btrfs_root *root = BTRFS_I(inode)->root; 3931 struct btrfs_root *root = BTRFS_I(inode)->root;
3896 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4); 3932 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
3897 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); 3933 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3898} 3934}
3899 3935
@@ -4987,6 +5023,15 @@ have_block_group:
4987 if (unlikely(block_group->ro)) 5023 if (unlikely(block_group->ro))
4988 goto loop; 5024 goto loop;
4989 5025
5026 spin_lock(&block_group->free_space_ctl->tree_lock);
5027 if (cached &&
5028 block_group->free_space_ctl->free_space <
5029 num_bytes + empty_size) {
5030 spin_unlock(&block_group->free_space_ctl->tree_lock);
5031 goto loop;
5032 }
5033 spin_unlock(&block_group->free_space_ctl->tree_lock);
5034
4990 /* 5035 /*
4991 * Ok we want to try and use the cluster allocator, so lets look 5036 * 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 5037 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
@@ -5150,6 +5195,7 @@ checks:
5150 btrfs_add_free_space(block_group, offset, 5195 btrfs_add_free_space(block_group, offset,
5151 search_start - offset); 5196 search_start - offset);
5152 BUG_ON(offset > search_start); 5197 BUG_ON(offset > search_start);
5198 btrfs_put_block_group(block_group);
5153 break; 5199 break;
5154loop: 5200loop:
5155 failed_cluster_refill = false; 5201 failed_cluster_refill = false;
@@ -5172,9 +5218,7 @@ loop:
5172 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try 5218 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5173 * again 5219 * again
5174 */ 5220 */
5175 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && 5221 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
5176 (found_uncached_bg || empty_size || empty_cluster ||
5177 allowed_chunk_alloc)) {
5178 index = 0; 5222 index = 0;
5179 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { 5223 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
5180 found_uncached_bg = false; 5224 found_uncached_bg = false;
@@ -5214,42 +5258,39 @@ loop:
5214 goto search; 5258 goto search;
5215 } 5259 }
5216 5260
5217 if (loop < LOOP_CACHING_WAIT) { 5261 loop++;
5218 loop++;
5219 goto search;
5220 }
5221 5262
5222 if (loop == LOOP_ALLOC_CHUNK) { 5263 if (loop == LOOP_ALLOC_CHUNK) {
5223 empty_size = 0; 5264 if (allowed_chunk_alloc) {
5224 empty_cluster = 0; 5265 ret = do_chunk_alloc(trans, root, num_bytes +
5225 } 5266 2 * 1024 * 1024, data,
5267 CHUNK_ALLOC_LIMITED);
5268 allowed_chunk_alloc = 0;
5269 if (ret == 1)
5270 done_chunk_alloc = 1;
5271 } else if (!done_chunk_alloc &&
5272 space_info->force_alloc ==
5273 CHUNK_ALLOC_NO_FORCE) {
5274 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5275 }
5226 5276
5227 if (allowed_chunk_alloc) { 5277 /*
5228 ret = do_chunk_alloc(trans, root, num_bytes + 5278 * We didn't allocate a chunk, go ahead and drop the
5229 2 * 1024 * 1024, data, 5279 * empty size and loop again.
5230 CHUNK_ALLOC_LIMITED); 5280 */
5231 allowed_chunk_alloc = 0; 5281 if (!done_chunk_alloc)
5232 done_chunk_alloc = 1; 5282 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 } 5283 }
5237 5284
5238 if (loop < LOOP_NO_EMPTY_SIZE) { 5285 if (loop == LOOP_NO_EMPTY_SIZE) {
5239 loop++; 5286 empty_size = 0;
5240 goto search; 5287 empty_cluster = 0;
5241 } 5288 }
5242 ret = -ENOSPC; 5289
5290 goto search;
5243 } else if (!ins->objectid) { 5291 } else if (!ins->objectid) {
5244 ret = -ENOSPC; 5292 ret = -ENOSPC;
5245 } 5293 } 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; 5294 ret = 0;
5254 } 5295 }
5255 5296
@@ -6526,7 +6567,7 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
6526 6567
6527 BUG_ON(cache->ro); 6568 BUG_ON(cache->ro);
6528 6569
6529 trans = btrfs_join_transaction(root, 1); 6570 trans = btrfs_join_transaction(root);
6530 BUG_ON(IS_ERR(trans)); 6571 BUG_ON(IS_ERR(trans));
6531 6572
6532 alloc_flags = update_block_group_flags(root, cache->flags); 6573 alloc_flags = update_block_group_flags(root, cache->flags);
@@ -6882,6 +6923,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
6882 path = btrfs_alloc_path(); 6923 path = btrfs_alloc_path();
6883 if (!path) 6924 if (!path)
6884 return -ENOMEM; 6925 return -ENOMEM;
6926 path->reada = 1;
6885 6927
6886 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); 6928 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
6887 if (cache_gen != 0 && 6929 if (cache_gen != 0 &&