aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c144
1 files changed, 103 insertions, 41 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 112e5aa85892..dae12dc7e159 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -424,9 +424,12 @@ again:
424 * and free up our temp pages. 424 * and free up our temp pages.
425 */ 425 */
426 extent_clear_unlock_delalloc(inode, 426 extent_clear_unlock_delalloc(inode,
427 &BTRFS_I(inode)->io_tree, 427 &BTRFS_I(inode)->io_tree,
428 start, end, NULL, 1, 0, 428 start, end, NULL,
429 0, 1, 1, 1, 0); 429 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
430 EXTENT_CLEAR_DELALLOC |
431 EXTENT_CLEAR_ACCOUNTING |
432 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
430 ret = 0; 433 ret = 0;
431 goto free_pages_out; 434 goto free_pages_out;
432 } 435 }
@@ -637,11 +640,14 @@ static noinline int submit_compressed_extents(struct inode *inode,
637 * clear dirty, set writeback and unlock the pages. 640 * clear dirty, set writeback and unlock the pages.
638 */ 641 */
639 extent_clear_unlock_delalloc(inode, 642 extent_clear_unlock_delalloc(inode,
640 &BTRFS_I(inode)->io_tree, 643 &BTRFS_I(inode)->io_tree,
641 async_extent->start, 644 async_extent->start,
642 async_extent->start + 645 async_extent->start +
643 async_extent->ram_size - 1, 646 async_extent->ram_size - 1,
644 NULL, 1, 1, 0, 1, 1, 0, 0); 647 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
648 EXTENT_CLEAR_UNLOCK |
649 EXTENT_CLEAR_DELALLOC |
650 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
645 651
646 ret = btrfs_submit_compressed_write(inode, 652 ret = btrfs_submit_compressed_write(inode,
647 async_extent->start, 653 async_extent->start,
@@ -712,9 +718,15 @@ static noinline int cow_file_range(struct inode *inode,
712 start, end, 0, NULL); 718 start, end, 0, NULL);
713 if (ret == 0) { 719 if (ret == 0) {
714 extent_clear_unlock_delalloc(inode, 720 extent_clear_unlock_delalloc(inode,
715 &BTRFS_I(inode)->io_tree, 721 &BTRFS_I(inode)->io_tree,
716 start, end, NULL, 1, 1, 722 start, end, NULL,
717 1, 1, 1, 1, 0); 723 EXTENT_CLEAR_UNLOCK_PAGE |
724 EXTENT_CLEAR_UNLOCK |
725 EXTENT_CLEAR_DELALLOC |
726 EXTENT_CLEAR_ACCOUNTING |
727 EXTENT_CLEAR_DIRTY |
728 EXTENT_SET_WRITEBACK |
729 EXTENT_END_WRITEBACK);
718 *nr_written = *nr_written + 730 *nr_written = *nr_written +
719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 731 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
720 *page_started = 1; 732 *page_started = 1;
@@ -738,6 +750,8 @@ static noinline int cow_file_range(struct inode *inode,
738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); 750 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
739 751
740 while (disk_num_bytes > 0) { 752 while (disk_num_bytes > 0) {
753 unsigned long op;
754
741 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); 755 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
742 ret = btrfs_reserve_extent(trans, root, cur_alloc_size, 756 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
743 root->sectorsize, 0, alloc_hint, 757 root->sectorsize, 0, alloc_hint,
@@ -789,10 +803,13 @@ static noinline int cow_file_range(struct inode *inode,
789 * Do set the Private2 bit so we know this page was properly 803 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage 804 * setup for writepage
791 */ 805 */
806 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
807 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
808 EXTENT_SET_PRIVATE2;
809
792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 810 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 start, start + ram_size - 1, 811 start, start + ram_size - 1,
794 locked_page, unlock, 1, 812 locked_page, op);
795 1, 0, 0, 0, 1);
796 disk_num_bytes -= cur_alloc_size; 813 disk_num_bytes -= cur_alloc_size;
797 num_bytes -= cur_alloc_size; 814 num_bytes -= cur_alloc_size;
798 alloc_hint = ins.objectid + ins.offset; 815 alloc_hint = ins.objectid + ins.offset;
@@ -864,8 +881,8 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
864 u64 cur_end; 881 u64 cur_end;
865 int limit = 10 * 1024 * 1042; 882 int limit = 10 * 1024 * 1042;
866 883
867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | 884 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS); 885 1, 0, NULL, GFP_NOFS);
869 while (start < end) { 886 while (start < end) {
870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); 887 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode; 888 async_cow->inode = inode;
@@ -1006,6 +1023,7 @@ next_slot:
1006 1023
1007 if (found_key.offset > cur_offset) { 1024 if (found_key.offset > cur_offset) {
1008 extent_end = found_key.offset; 1025 extent_end = found_key.offset;
1026 extent_type = 0;
1009 goto out_check; 1027 goto out_check;
1010 } 1028 }
1011 1029
@@ -1112,8 +1130,10 @@ out_check:
1112 BUG_ON(ret); 1130 BUG_ON(ret);
1113 1131
1114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 1132 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1115 cur_offset, cur_offset + num_bytes - 1, 1133 cur_offset, cur_offset + num_bytes - 1,
1116 locked_page, 1, 1, 1, 0, 0, 0, 1); 1134 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1135 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1136 EXTENT_SET_PRIVATE2);
1117 cur_offset = extent_end; 1137 cur_offset = extent_end;
1118 if (cur_offset > end) 1138 if (cur_offset > end)
1119 break; 1139 break;
@@ -1178,15 +1198,17 @@ static int btrfs_split_extent_hook(struct inode *inode,
1178 root->fs_info->max_extent); 1198 root->fs_info->max_extent);
1179 1199
1180 /* 1200 /*
1181 * if we break a large extent up then leave delalloc_extents be, 1201 * if we break a large extent up then leave oustanding_extents
1182 * since we've already accounted for the large extent. 1202 * be, since we've already accounted for the large extent.
1183 */ 1203 */
1184 if (div64_u64(new_size + root->fs_info->max_extent - 1, 1204 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1185 root->fs_info->max_extent) < num_extents) 1205 root->fs_info->max_extent) < num_extents)
1186 return 0; 1206 return 0;
1187 } 1207 }
1188 1208
1189 BTRFS_I(inode)->delalloc_extents++; 1209 spin_lock(&BTRFS_I(inode)->accounting_lock);
1210 BTRFS_I(inode)->outstanding_extents++;
1211 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1190 1212
1191 return 0; 1213 return 0;
1192} 1214}
@@ -1217,7 +1239,9 @@ static int btrfs_merge_extent_hook(struct inode *inode,
1217 1239
1218 /* we're not bigger than the max, unreserve the space and go */ 1240 /* we're not bigger than the max, unreserve the space and go */
1219 if (new_size <= root->fs_info->max_extent) { 1241 if (new_size <= root->fs_info->max_extent) {
1220 BTRFS_I(inode)->delalloc_extents--; 1242 spin_lock(&BTRFS_I(inode)->accounting_lock);
1243 BTRFS_I(inode)->outstanding_extents--;
1244 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1221 return 0; 1245 return 0;
1222 } 1246 }
1223 1247
@@ -1231,7 +1255,9 @@ static int btrfs_merge_extent_hook(struct inode *inode,
1231 root->fs_info->max_extent) > num_extents) 1255 root->fs_info->max_extent) > num_extents)
1232 return 0; 1256 return 0;
1233 1257
1234 BTRFS_I(inode)->delalloc_extents--; 1258 spin_lock(&BTRFS_I(inode)->accounting_lock);
1259 BTRFS_I(inode)->outstanding_extents--;
1260 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1235 1261
1236 return 0; 1262 return 0;
1237} 1263}
@@ -1253,7 +1279,9 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1253 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1279 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1254 struct btrfs_root *root = BTRFS_I(inode)->root; 1280 struct btrfs_root *root = BTRFS_I(inode)->root;
1255 1281
1256 BTRFS_I(inode)->delalloc_extents++; 1282 spin_lock(&BTRFS_I(inode)->accounting_lock);
1283 BTRFS_I(inode)->outstanding_extents++;
1284 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1257 btrfs_delalloc_reserve_space(root, inode, end - start + 1); 1285 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1258 spin_lock(&root->fs_info->delalloc_lock); 1286 spin_lock(&root->fs_info->delalloc_lock);
1259 BTRFS_I(inode)->delalloc_bytes += end - start + 1; 1287 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
@@ -1281,8 +1309,12 @@ static int btrfs_clear_bit_hook(struct inode *inode,
1281 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1309 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1282 struct btrfs_root *root = BTRFS_I(inode)->root; 1310 struct btrfs_root *root = BTRFS_I(inode)->root;
1283 1311
1284 BTRFS_I(inode)->delalloc_extents--; 1312 if (bits & EXTENT_DO_ACCOUNTING) {
1285 btrfs_unreserve_metadata_for_delalloc(root, inode, 1); 1313 spin_lock(&BTRFS_I(inode)->accounting_lock);
1314 BTRFS_I(inode)->outstanding_extents--;
1315 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1316 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1317 }
1286 1318
1287 spin_lock(&root->fs_info->delalloc_lock); 1319 spin_lock(&root->fs_info->delalloc_lock);
1288 if (state->end - state->start + 1 > 1320 if (state->end - state->start + 1 >
@@ -3000,12 +3032,22 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3000 3032
3001 if ((offset & (blocksize - 1)) == 0) 3033 if ((offset & (blocksize - 1)) == 0)
3002 goto out; 3034 goto out;
3035 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3036 if (ret)
3037 goto out;
3038
3039 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3040 if (ret)
3041 goto out;
3003 3042
3004 ret = -ENOMEM; 3043 ret = -ENOMEM;
3005again: 3044again:
3006 page = grab_cache_page(mapping, index); 3045 page = grab_cache_page(mapping, index);
3007 if (!page) 3046 if (!page) {
3047 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3048 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3008 goto out; 3049 goto out;
3050 }
3009 3051
3010 page_start = page_offset(page); 3052 page_start = page_offset(page);
3011 page_end = page_start + PAGE_CACHE_SIZE - 1; 3053 page_end = page_start + PAGE_CACHE_SIZE - 1;
@@ -3038,6 +3080,10 @@ again:
3038 goto again; 3080 goto again;
3039 } 3081 }
3040 3082
3083 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3084 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3085 GFP_NOFS);
3086
3041 ret = btrfs_set_extent_delalloc(inode, page_start, page_end); 3087 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3042 if (ret) { 3088 if (ret) {
3043 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3089 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
@@ -3056,6 +3102,9 @@ again:
3056 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3102 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3057 3103
3058out_unlock: 3104out_unlock:
3105 if (ret)
3106 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3107 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3059 unlock_page(page); 3108 unlock_page(page);
3060 page_cache_release(page); 3109 page_cache_release(page);
3061out: 3110out:
@@ -3079,7 +3128,9 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3079 if (size <= hole_start) 3128 if (size <= hole_start)
3080 return 0; 3129 return 0;
3081 3130
3082 btrfs_truncate_page(inode->i_mapping, inode->i_size); 3131 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3132 if (err)
3133 return err;
3083 3134
3084 while (1) { 3135 while (1) {
3085 struct btrfs_ordered_extent *ordered; 3136 struct btrfs_ordered_extent *ordered;
@@ -3448,6 +3499,7 @@ static noinline void init_btrfs_i(struct inode *inode)
3448 bi->generation = 0; 3499 bi->generation = 0;
3449 bi->sequence = 0; 3500 bi->sequence = 0;
3450 bi->last_trans = 0; 3501 bi->last_trans = 0;
3502 bi->last_sub_trans = 0;
3451 bi->logged_trans = 0; 3503 bi->logged_trans = 0;
3452 bi->delalloc_bytes = 0; 3504 bi->delalloc_bytes = 0;
3453 bi->reserved_bytes = 0; 3505 bi->reserved_bytes = 0;
@@ -3598,12 +3650,14 @@ static int btrfs_dentry_delete(struct dentry *dentry)
3598{ 3650{
3599 struct btrfs_root *root; 3651 struct btrfs_root *root;
3600 3652
3601 if (!dentry->d_inode) 3653 if (!dentry->d_inode && !IS_ROOT(dentry))
3602 return 0; 3654 dentry = dentry->d_parent;
3603 3655
3604 root = BTRFS_I(dentry->d_inode)->root; 3656 if (dentry->d_inode) {
3605 if (btrfs_root_refs(&root->root_item) == 0) 3657 root = BTRFS_I(dentry->d_inode)->root;
3606 return 1; 3658 if (btrfs_root_refs(&root->root_item) == 0)
3659 return 1;
3660 }
3607 return 0; 3661 return 0;
3608} 3662}
3609 3663
@@ -4808,7 +4862,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4808 */ 4862 */
4809 clear_extent_bit(tree, page_start, page_end, 4863 clear_extent_bit(tree, page_start, page_end,
4810 EXTENT_DIRTY | EXTENT_DELALLOC | 4864 EXTENT_DIRTY | EXTENT_DELALLOC |
4811 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); 4865 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4866 NULL, GFP_NOFS);
4812 /* 4867 /*
4813 * whoever cleared the private bit is responsible 4868 * whoever cleared the private bit is responsible
4814 * for the finish_ordered_io 4869 * for the finish_ordered_io
@@ -4821,8 +4876,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4821 lock_extent(tree, page_start, page_end, GFP_NOFS); 4876 lock_extent(tree, page_start, page_end, GFP_NOFS);
4822 } 4877 }
4823 clear_extent_bit(tree, page_start, page_end, 4878 clear_extent_bit(tree, page_start, page_end,
4824 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, 4879 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4825 1, 1, NULL, GFP_NOFS); 4880 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
4826 __btrfs_releasepage(page, GFP_NOFS); 4881 __btrfs_releasepage(page, GFP_NOFS);
4827 4882
4828 ClearPageChecked(page); 4883 ClearPageChecked(page);
@@ -4917,7 +4972,8 @@ again:
4917 * prepare_pages in the normal write path. 4972 * prepare_pages in the normal write path.
4918 */ 4973 */
4919 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 4974 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
4920 EXTENT_DIRTY | EXTENT_DELALLOC, GFP_NOFS); 4975 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
4976 GFP_NOFS);
4921 4977
4922 ret = btrfs_set_extent_delalloc(inode, page_start, page_end); 4978 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
4923 if (ret) { 4979 if (ret) {
@@ -4944,7 +5000,9 @@ again:
4944 set_page_dirty(page); 5000 set_page_dirty(page);
4945 SetPageUptodate(page); 5001 SetPageUptodate(page);
4946 5002
4947 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; 5003 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5004 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5005
4948 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 5006 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4949 5007
4950out_unlock: 5008out_unlock:
@@ -4969,7 +5027,9 @@ static void btrfs_truncate(struct inode *inode)
4969 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 5027 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4970 return; 5028 return;
4971 5029
4972 btrfs_truncate_page(inode->i_mapping, inode->i_size); 5030 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5031 if (ret)
5032 return;
4973 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 5033 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
4974 5034
4975 trans = btrfs_start_transaction(root, 1); 5035 trans = btrfs_start_transaction(root, 1);
@@ -5064,9 +5124,11 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
5064 if (!ei) 5124 if (!ei)
5065 return NULL; 5125 return NULL;
5066 ei->last_trans = 0; 5126 ei->last_trans = 0;
5127 ei->last_sub_trans = 0;
5067 ei->logged_trans = 0; 5128 ei->logged_trans = 0;
5068 ei->delalloc_extents = 0; 5129 ei->outstanding_extents = 0;
5069 ei->delalloc_reserved_extents = 0; 5130 ei->reserved_extents = 0;
5131 spin_lock_init(&ei->accounting_lock);
5070 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 5132 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
5071 INIT_LIST_HEAD(&ei->i_orphan); 5133 INIT_LIST_HEAD(&ei->i_orphan);
5072 INIT_LIST_HEAD(&ei->ordered_operations); 5134 INIT_LIST_HEAD(&ei->ordered_operations);
@@ -5805,6 +5867,6 @@ static const struct inode_operations btrfs_symlink_inode_operations = {
5805 .removexattr = btrfs_removexattr, 5867 .removexattr = btrfs_removexattr,
5806}; 5868};
5807 5869
5808struct dentry_operations btrfs_dentry_operations = { 5870const struct dentry_operations btrfs_dentry_operations = {
5809 .d_delete = btrfs_dentry_delete, 5871 .d_delete = btrfs_dentry_delete,
5810}; 5872};