diff options
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r-- | fs/btrfs/inode.c | 146 |
1 files changed, 139 insertions, 7 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 43192e10cc43..56f00a25c003 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c | |||
@@ -59,6 +59,7 @@ | |||
59 | #include "backref.h" | 59 | #include "backref.h" |
60 | #include "hash.h" | 60 | #include "hash.h" |
61 | #include "props.h" | 61 | #include "props.h" |
62 | #include "qgroup.h" | ||
62 | 63 | ||
63 | struct btrfs_iget_args { | 64 | struct btrfs_iget_args { |
64 | struct btrfs_key *location; | 65 | struct btrfs_key *location; |
@@ -470,7 +471,7 @@ again: | |||
470 | */ | 471 | */ |
471 | if (inode_need_compress(inode)) { | 472 | if (inode_need_compress(inode)) { |
472 | WARN_ON(pages); | 473 | WARN_ON(pages); |
473 | pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); | 474 | pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); |
474 | if (!pages) { | 475 | if (!pages) { |
475 | /* just bail out to the uncompressed code */ | 476 | /* just bail out to the uncompressed code */ |
476 | goto cont; | 477 | goto cont; |
@@ -752,7 +753,6 @@ retry: | |||
752 | } | 753 | } |
753 | goto out_free; | 754 | goto out_free; |
754 | } | 755 | } |
755 | |||
756 | /* | 756 | /* |
757 | * here we're doing allocation and writeback of the | 757 | * here we're doing allocation and writeback of the |
758 | * compressed pages | 758 | * compressed pages |
@@ -3110,6 +3110,8 @@ void btrfs_run_delayed_iputs(struct btrfs_root *root) | |||
3110 | if (empty) | 3110 | if (empty) |
3111 | return; | 3111 | return; |
3112 | 3112 | ||
3113 | down_read(&fs_info->delayed_iput_sem); | ||
3114 | |||
3113 | spin_lock(&fs_info->delayed_iput_lock); | 3115 | spin_lock(&fs_info->delayed_iput_lock); |
3114 | list_splice_init(&fs_info->delayed_iputs, &list); | 3116 | list_splice_init(&fs_info->delayed_iputs, &list); |
3115 | spin_unlock(&fs_info->delayed_iput_lock); | 3117 | spin_unlock(&fs_info->delayed_iput_lock); |
@@ -3120,6 +3122,8 @@ void btrfs_run_delayed_iputs(struct btrfs_root *root) | |||
3120 | iput(delayed->inode); | 3122 | iput(delayed->inode); |
3121 | kfree(delayed); | 3123 | kfree(delayed); |
3122 | } | 3124 | } |
3125 | |||
3126 | up_read(&root->fs_info->delayed_iput_sem); | ||
3123 | } | 3127 | } |
3124 | 3128 | ||
3125 | /* | 3129 | /* |
@@ -4162,6 +4166,21 @@ out: | |||
4162 | return err; | 4166 | return err; |
4163 | } | 4167 | } |
4164 | 4168 | ||
4169 | static int truncate_space_check(struct btrfs_trans_handle *trans, | ||
4170 | struct btrfs_root *root, | ||
4171 | u64 bytes_deleted) | ||
4172 | { | ||
4173 | int ret; | ||
4174 | |||
4175 | bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); | ||
4176 | ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, | ||
4177 | bytes_deleted, BTRFS_RESERVE_NO_FLUSH); | ||
4178 | if (!ret) | ||
4179 | trans->bytes_reserved += bytes_deleted; | ||
4180 | return ret; | ||
4181 | |||
4182 | } | ||
4183 | |||
4165 | /* | 4184 | /* |
4166 | * this can truncate away extent items, csum items and directory items. | 4185 | * this can truncate away extent items, csum items and directory items. |
4167 | * It starts at a high offset and removes keys until it can't find | 4186 | * It starts at a high offset and removes keys until it can't find |
@@ -4197,9 +4216,21 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | |||
4197 | int ret; | 4216 | int ret; |
4198 | int err = 0; | 4217 | int err = 0; |
4199 | u64 ino = btrfs_ino(inode); | 4218 | u64 ino = btrfs_ino(inode); |
4219 | u64 bytes_deleted = 0; | ||
4220 | bool be_nice = 0; | ||
4221 | bool should_throttle = 0; | ||
4222 | bool should_end = 0; | ||
4200 | 4223 | ||
4201 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); | 4224 | BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); |
4202 | 4225 | ||
4226 | /* | ||
4227 | * for non-free space inodes and ref cows, we want to back off from | ||
4228 | * time to time | ||
4229 | */ | ||
4230 | if (!btrfs_is_free_space_inode(inode) && | ||
4231 | test_bit(BTRFS_ROOT_REF_COWS, &root->state)) | ||
4232 | be_nice = 1; | ||
4233 | |||
4203 | path = btrfs_alloc_path(); | 4234 | path = btrfs_alloc_path(); |
4204 | if (!path) | 4235 | if (!path) |
4205 | return -ENOMEM; | 4236 | return -ENOMEM; |
@@ -4229,6 +4260,19 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, | |||
4229 | key.type = (u8)-1; | 4260 | key.type = (u8)-1; |
4230 | 4261 | ||
4231 | search_again: | 4262 | search_again: |
4263 | /* | ||
4264 | * with a 16K leaf size and 128MB extents, you can actually queue | ||
4265 | * up a huge file in a single leaf. Most of the time that | ||
4266 | * bytes_deleted is > 0, it will be huge by the time we get here | ||
4267 | */ | ||
4268 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { | ||
4269 | if (btrfs_should_end_transaction(trans, root)) { | ||
4270 | err = -EAGAIN; | ||
4271 | goto error; | ||
4272 | } | ||
4273 | } | ||
4274 | |||
4275 | |||
4232 | path->leave_spinning = 1; | 4276 | path->leave_spinning = 1; |
4233 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | 4277 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
4234 | if (ret < 0) { | 4278 | if (ret < 0) { |
@@ -4371,22 +4415,39 @@ delete: | |||
4371 | } else { | 4415 | } else { |
4372 | break; | 4416 | break; |
4373 | } | 4417 | } |
4418 | should_throttle = 0; | ||
4419 | |||
4374 | if (found_extent && | 4420 | if (found_extent && |
4375 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || | 4421 | (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
4376 | root == root->fs_info->tree_root)) { | 4422 | root == root->fs_info->tree_root)) { |
4377 | btrfs_set_path_blocking(path); | 4423 | btrfs_set_path_blocking(path); |
4424 | bytes_deleted += extent_num_bytes; | ||
4378 | ret = btrfs_free_extent(trans, root, extent_start, | 4425 | ret = btrfs_free_extent(trans, root, extent_start, |
4379 | extent_num_bytes, 0, | 4426 | extent_num_bytes, 0, |
4380 | btrfs_header_owner(leaf), | 4427 | btrfs_header_owner(leaf), |
4381 | ino, extent_offset, 0); | 4428 | ino, extent_offset, 0); |
4382 | BUG_ON(ret); | 4429 | BUG_ON(ret); |
4430 | if (btrfs_should_throttle_delayed_refs(trans, root)) | ||
4431 | btrfs_async_run_delayed_refs(root, | ||
4432 | trans->delayed_ref_updates * 2, 0); | ||
4433 | if (be_nice) { | ||
4434 | if (truncate_space_check(trans, root, | ||
4435 | extent_num_bytes)) { | ||
4436 | should_end = 1; | ||
4437 | } | ||
4438 | if (btrfs_should_throttle_delayed_refs(trans, | ||
4439 | root)) { | ||
4440 | should_throttle = 1; | ||
4441 | } | ||
4442 | } | ||
4383 | } | 4443 | } |
4384 | 4444 | ||
4385 | if (found_type == BTRFS_INODE_ITEM_KEY) | 4445 | if (found_type == BTRFS_INODE_ITEM_KEY) |
4386 | break; | 4446 | break; |
4387 | 4447 | ||
4388 | if (path->slots[0] == 0 || | 4448 | if (path->slots[0] == 0 || |
4389 | path->slots[0] != pending_del_slot) { | 4449 | path->slots[0] != pending_del_slot || |
4450 | should_throttle || should_end) { | ||
4390 | if (pending_del_nr) { | 4451 | if (pending_del_nr) { |
4391 | ret = btrfs_del_items(trans, root, path, | 4452 | ret = btrfs_del_items(trans, root, path, |
4392 | pending_del_slot, | 4453 | pending_del_slot, |
@@ -4399,6 +4460,23 @@ delete: | |||
4399 | pending_del_nr = 0; | 4460 | pending_del_nr = 0; |
4400 | } | 4461 | } |
4401 | btrfs_release_path(path); | 4462 | btrfs_release_path(path); |
4463 | if (should_throttle) { | ||
4464 | unsigned long updates = trans->delayed_ref_updates; | ||
4465 | if (updates) { | ||
4466 | trans->delayed_ref_updates = 0; | ||
4467 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | ||
4468 | if (ret && !err) | ||
4469 | err = ret; | ||
4470 | } | ||
4471 | } | ||
4472 | /* | ||
4473 | * if we failed to refill our space rsv, bail out | ||
4474 | * and let the transaction restart | ||
4475 | */ | ||
4476 | if (should_end) { | ||
4477 | err = -EAGAIN; | ||
4478 | goto error; | ||
4479 | } | ||
4402 | goto search_again; | 4480 | goto search_again; |
4403 | } else { | 4481 | } else { |
4404 | path->slots[0]--; | 4482 | path->slots[0]--; |
@@ -4415,7 +4493,18 @@ error: | |||
4415 | if (last_size != (u64)-1 && | 4493 | if (last_size != (u64)-1 && |
4416 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | 4494 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
4417 | btrfs_ordered_update_i_size(inode, last_size, NULL); | 4495 | btrfs_ordered_update_i_size(inode, last_size, NULL); |
4496 | |||
4418 | btrfs_free_path(path); | 4497 | btrfs_free_path(path); |
4498 | |||
4499 | if (be_nice && bytes_deleted > 32 * 1024 * 1024) { | ||
4500 | unsigned long updates = trans->delayed_ref_updates; | ||
4501 | if (updates) { | ||
4502 | trans->delayed_ref_updates = 0; | ||
4503 | ret = btrfs_run_delayed_refs(trans, root, updates * 2); | ||
4504 | if (ret && !err) | ||
4505 | err = ret; | ||
4506 | } | ||
4507 | } | ||
4419 | return err; | 4508 | return err; |
4420 | } | 4509 | } |
4421 | 4510 | ||
@@ -4924,6 +5013,7 @@ void btrfs_evict_inode(struct inode *inode) | |||
4924 | struct btrfs_trans_handle *trans; | 5013 | struct btrfs_trans_handle *trans; |
4925 | struct btrfs_root *root = BTRFS_I(inode)->root; | 5014 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4926 | struct btrfs_block_rsv *rsv, *global_rsv; | 5015 | struct btrfs_block_rsv *rsv, *global_rsv; |
5016 | int steal_from_global = 0; | ||
4927 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); | 5017 | u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); |
4928 | int ret; | 5018 | int ret; |
4929 | 5019 | ||
@@ -4991,9 +5081,20 @@ void btrfs_evict_inode(struct inode *inode) | |||
4991 | * hard as possible to get this to work. | 5081 | * hard as possible to get this to work. |
4992 | */ | 5082 | */ |
4993 | if (ret) | 5083 | if (ret) |
4994 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size); | 5084 | steal_from_global++; |
5085 | else | ||
5086 | steal_from_global = 0; | ||
5087 | ret = 0; | ||
4995 | 5088 | ||
4996 | if (ret) { | 5089 | /* |
5090 | * steal_from_global == 0: we reserved stuff, hooray! | ||
5091 | * steal_from_global == 1: we didn't reserve stuff, boo! | ||
5092 | * steal_from_global == 2: we've committed, still not a lot of | ||
5093 | * room but maybe we'll have room in the global reserve this | ||
5094 | * time. | ||
5095 | * steal_from_global == 3: abandon all hope! | ||
5096 | */ | ||
5097 | if (steal_from_global > 2) { | ||
4997 | btrfs_warn(root->fs_info, | 5098 | btrfs_warn(root->fs_info, |
4998 | "Could not get space for a delete, will truncate on mount %d", | 5099 | "Could not get space for a delete, will truncate on mount %d", |
4999 | ret); | 5100 | ret); |
@@ -5009,10 +5110,40 @@ void btrfs_evict_inode(struct inode *inode) | |||
5009 | goto no_delete; | 5110 | goto no_delete; |
5010 | } | 5111 | } |
5011 | 5112 | ||
5113 | /* | ||
5114 | * We can't just steal from the global reserve, we need tomake | ||
5115 | * sure there is room to do it, if not we need to commit and try | ||
5116 | * again. | ||
5117 | */ | ||
5118 | if (steal_from_global) { | ||
5119 | if (!btrfs_check_space_for_delayed_refs(trans, root)) | ||
5120 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, | ||
5121 | min_size); | ||
5122 | else | ||
5123 | ret = -ENOSPC; | ||
5124 | } | ||
5125 | |||
5126 | /* | ||
5127 | * Couldn't steal from the global reserve, we have too much | ||
5128 | * pending stuff built up, commit the transaction and try it | ||
5129 | * again. | ||
5130 | */ | ||
5131 | if (ret) { | ||
5132 | ret = btrfs_commit_transaction(trans, root); | ||
5133 | if (ret) { | ||
5134 | btrfs_orphan_del(NULL, inode); | ||
5135 | btrfs_free_block_rsv(root, rsv); | ||
5136 | goto no_delete; | ||
5137 | } | ||
5138 | continue; | ||
5139 | } else { | ||
5140 | steal_from_global = 0; | ||
5141 | } | ||
5142 | |||
5012 | trans->block_rsv = rsv; | 5143 | trans->block_rsv = rsv; |
5013 | 5144 | ||
5014 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); | 5145 | ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0); |
5015 | if (ret != -ENOSPC) | 5146 | if (ret != -ENOSPC && ret != -EAGAIN) |
5016 | break; | 5147 | break; |
5017 | 5148 | ||
5018 | trans->block_rsv = &root->fs_info->trans_block_rsv; | 5149 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
@@ -8581,7 +8712,7 @@ static int btrfs_truncate(struct inode *inode) | |||
8581 | ret = btrfs_truncate_inode_items(trans, root, inode, | 8712 | ret = btrfs_truncate_inode_items(trans, root, inode, |
8582 | inode->i_size, | 8713 | inode->i_size, |
8583 | BTRFS_EXTENT_DATA_KEY); | 8714 | BTRFS_EXTENT_DATA_KEY); |
8584 | if (ret != -ENOSPC) { | 8715 | if (ret != -ENOSPC && ret != -EAGAIN) { |
8585 | err = ret; | 8716 | err = ret; |
8586 | break; | 8717 | break; |
8587 | } | 8718 | } |
@@ -9451,6 +9582,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, | |||
9451 | btrfs_end_transaction(trans, root); | 9582 | btrfs_end_transaction(trans, root); |
9452 | break; | 9583 | break; |
9453 | } | 9584 | } |
9585 | |||
9454 | btrfs_drop_extent_cache(inode, cur_offset, | 9586 | btrfs_drop_extent_cache(inode, cur_offset, |
9455 | cur_offset + ins.offset -1, 0); | 9587 | cur_offset + ins.offset -1, 0); |
9456 | 9588 | ||