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.c239
1 files changed, 183 insertions, 56 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 112e5aa85892..b3ad168a0bfc 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 }
@@ -535,7 +538,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
535 struct btrfs_root *root = BTRFS_I(inode)->root; 538 struct btrfs_root *root = BTRFS_I(inode)->root;
536 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 539 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
537 struct extent_io_tree *io_tree; 540 struct extent_io_tree *io_tree;
538 int ret; 541 int ret = 0;
539 542
540 if (list_empty(&async_cow->extents)) 543 if (list_empty(&async_cow->extents))
541 return 0; 544 return 0;
@@ -549,6 +552,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
549 552
550 io_tree = &BTRFS_I(inode)->io_tree; 553 io_tree = &BTRFS_I(inode)->io_tree;
551 554
555retry:
552 /* did the compression code fall back to uncompressed IO? */ 556 /* did the compression code fall back to uncompressed IO? */
553 if (!async_extent->pages) { 557 if (!async_extent->pages) {
554 int page_started = 0; 558 int page_started = 0;
@@ -559,11 +563,11 @@ static noinline int submit_compressed_extents(struct inode *inode,
559 async_extent->ram_size - 1, GFP_NOFS); 563 async_extent->ram_size - 1, GFP_NOFS);
560 564
561 /* allocate blocks */ 565 /* allocate blocks */
562 cow_file_range(inode, async_cow->locked_page, 566 ret = cow_file_range(inode, async_cow->locked_page,
563 async_extent->start, 567 async_extent->start,
564 async_extent->start + 568 async_extent->start +
565 async_extent->ram_size - 1, 569 async_extent->ram_size - 1,
566 &page_started, &nr_written, 0); 570 &page_started, &nr_written, 0);
567 571
568 /* 572 /*
569 * if page_started, cow_file_range inserted an 573 * if page_started, cow_file_range inserted an
@@ -571,7 +575,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
571 * and IO for us. Otherwise, we need to submit 575 * and IO for us. Otherwise, we need to submit
572 * all those pages down to the drive. 576 * all those pages down to the drive.
573 */ 577 */
574 if (!page_started) 578 if (!page_started && !ret)
575 extent_write_locked_range(io_tree, 579 extent_write_locked_range(io_tree,
576 inode, async_extent->start, 580 inode, async_extent->start,
577 async_extent->start + 581 async_extent->start +
@@ -599,7 +603,21 @@ static noinline int submit_compressed_extents(struct inode *inode,
599 async_extent->compressed_size, 603 async_extent->compressed_size,
600 0, alloc_hint, 604 0, alloc_hint,
601 (u64)-1, &ins, 1); 605 (u64)-1, &ins, 1);
602 BUG_ON(ret); 606 if (ret) {
607 int i;
608 for (i = 0; i < async_extent->nr_pages; i++) {
609 WARN_ON(async_extent->pages[i]->mapping);
610 page_cache_release(async_extent->pages[i]);
611 }
612 kfree(async_extent->pages);
613 async_extent->nr_pages = 0;
614 async_extent->pages = NULL;
615 unlock_extent(io_tree, async_extent->start,
616 async_extent->start +
617 async_extent->ram_size - 1, GFP_NOFS);
618 goto retry;
619 }
620
603 em = alloc_extent_map(GFP_NOFS); 621 em = alloc_extent_map(GFP_NOFS);
604 em->start = async_extent->start; 622 em->start = async_extent->start;
605 em->len = async_extent->ram_size; 623 em->len = async_extent->ram_size;
@@ -637,11 +655,14 @@ static noinline int submit_compressed_extents(struct inode *inode,
637 * clear dirty, set writeback and unlock the pages. 655 * clear dirty, set writeback and unlock the pages.
638 */ 656 */
639 extent_clear_unlock_delalloc(inode, 657 extent_clear_unlock_delalloc(inode,
640 &BTRFS_I(inode)->io_tree, 658 &BTRFS_I(inode)->io_tree,
641 async_extent->start, 659 async_extent->start,
642 async_extent->start + 660 async_extent->start +
643 async_extent->ram_size - 1, 661 async_extent->ram_size - 1,
644 NULL, 1, 1, 0, 1, 1, 0, 0); 662 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
663 EXTENT_CLEAR_UNLOCK |
664 EXTENT_CLEAR_DELALLOC |
665 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
645 666
646 ret = btrfs_submit_compressed_write(inode, 667 ret = btrfs_submit_compressed_write(inode,
647 async_extent->start, 668 async_extent->start,
@@ -712,9 +733,15 @@ static noinline int cow_file_range(struct inode *inode,
712 start, end, 0, NULL); 733 start, end, 0, NULL);
713 if (ret == 0) { 734 if (ret == 0) {
714 extent_clear_unlock_delalloc(inode, 735 extent_clear_unlock_delalloc(inode,
715 &BTRFS_I(inode)->io_tree, 736 &BTRFS_I(inode)->io_tree,
716 start, end, NULL, 1, 1, 737 start, end, NULL,
717 1, 1, 1, 1, 0); 738 EXTENT_CLEAR_UNLOCK_PAGE |
739 EXTENT_CLEAR_UNLOCK |
740 EXTENT_CLEAR_DELALLOC |
741 EXTENT_CLEAR_ACCOUNTING |
742 EXTENT_CLEAR_DIRTY |
743 EXTENT_SET_WRITEBACK |
744 EXTENT_END_WRITEBACK);
718 *nr_written = *nr_written + 745 *nr_written = *nr_written +
719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 746 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
720 *page_started = 1; 747 *page_started = 1;
@@ -731,13 +758,29 @@ static noinline int cow_file_range(struct inode *inode,
731 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree, 758 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
732 start, num_bytes); 759 start, num_bytes);
733 if (em) { 760 if (em) {
734 alloc_hint = em->block_start; 761 /*
735 free_extent_map(em); 762 * if block start isn't an actual block number then find the
763 * first block in this inode and use that as a hint. If that
764 * block is also bogus then just don't worry about it.
765 */
766 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
767 free_extent_map(em);
768 em = search_extent_mapping(em_tree, 0, 0);
769 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
770 alloc_hint = em->block_start;
771 if (em)
772 free_extent_map(em);
773 } else {
774 alloc_hint = em->block_start;
775 free_extent_map(em);
776 }
736 } 777 }
737 read_unlock(&BTRFS_I(inode)->extent_tree.lock); 778 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); 779 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
739 780
740 while (disk_num_bytes > 0) { 781 while (disk_num_bytes > 0) {
782 unsigned long op;
783
741 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent); 784 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
742 ret = btrfs_reserve_extent(trans, root, cur_alloc_size, 785 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
743 root->sectorsize, 0, alloc_hint, 786 root->sectorsize, 0, alloc_hint,
@@ -789,10 +832,13 @@ static noinline int cow_file_range(struct inode *inode,
789 * Do set the Private2 bit so we know this page was properly 832 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage 833 * setup for writepage
791 */ 834 */
835 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
836 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
837 EXTENT_SET_PRIVATE2;
838
792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 839 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 start, start + ram_size - 1, 840 start, start + ram_size - 1,
794 locked_page, unlock, 1, 841 locked_page, op);
795 1, 0, 0, 0, 1);
796 disk_num_bytes -= cur_alloc_size; 842 disk_num_bytes -= cur_alloc_size;
797 num_bytes -= cur_alloc_size; 843 num_bytes -= cur_alloc_size;
798 alloc_hint = ins.objectid + ins.offset; 844 alloc_hint = ins.objectid + ins.offset;
@@ -864,8 +910,8 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
864 u64 cur_end; 910 u64 cur_end;
865 int limit = 10 * 1024 * 1042; 911 int limit = 10 * 1024 * 1042;
866 912
867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED | 913 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS); 914 1, 0, NULL, GFP_NOFS);
869 while (start < end) { 915 while (start < end) {
870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); 916 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode; 917 async_cow->inode = inode;
@@ -1006,6 +1052,7 @@ next_slot:
1006 1052
1007 if (found_key.offset > cur_offset) { 1053 if (found_key.offset > cur_offset) {
1008 extent_end = found_key.offset; 1054 extent_end = found_key.offset;
1055 extent_type = 0;
1009 goto out_check; 1056 goto out_check;
1010 } 1057 }
1011 1058
@@ -1112,8 +1159,10 @@ out_check:
1112 BUG_ON(ret); 1159 BUG_ON(ret);
1113 1160
1114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 1161 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1115 cur_offset, cur_offset + num_bytes - 1, 1162 cur_offset, cur_offset + num_bytes - 1,
1116 locked_page, 1, 1, 1, 0, 0, 0, 1); 1163 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1164 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1165 EXTENT_SET_PRIVATE2);
1117 cur_offset = extent_end; 1166 cur_offset = extent_end;
1118 if (cur_offset > end) 1167 if (cur_offset > end)
1119 break; 1168 break;
@@ -1178,15 +1227,17 @@ static int btrfs_split_extent_hook(struct inode *inode,
1178 root->fs_info->max_extent); 1227 root->fs_info->max_extent);
1179 1228
1180 /* 1229 /*
1181 * if we break a large extent up then leave delalloc_extents be, 1230 * if we break a large extent up then leave oustanding_extents
1182 * since we've already accounted for the large extent. 1231 * be, since we've already accounted for the large extent.
1183 */ 1232 */
1184 if (div64_u64(new_size + root->fs_info->max_extent - 1, 1233 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1185 root->fs_info->max_extent) < num_extents) 1234 root->fs_info->max_extent) < num_extents)
1186 return 0; 1235 return 0;
1187 } 1236 }
1188 1237
1189 BTRFS_I(inode)->delalloc_extents++; 1238 spin_lock(&BTRFS_I(inode)->accounting_lock);
1239 BTRFS_I(inode)->outstanding_extents++;
1240 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1190 1241
1191 return 0; 1242 return 0;
1192} 1243}
@@ -1217,7 +1268,9 @@ static int btrfs_merge_extent_hook(struct inode *inode,
1217 1268
1218 /* we're not bigger than the max, unreserve the space and go */ 1269 /* we're not bigger than the max, unreserve the space and go */
1219 if (new_size <= root->fs_info->max_extent) { 1270 if (new_size <= root->fs_info->max_extent) {
1220 BTRFS_I(inode)->delalloc_extents--; 1271 spin_lock(&BTRFS_I(inode)->accounting_lock);
1272 BTRFS_I(inode)->outstanding_extents--;
1273 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1221 return 0; 1274 return 0;
1222 } 1275 }
1223 1276
@@ -1231,7 +1284,9 @@ static int btrfs_merge_extent_hook(struct inode *inode,
1231 root->fs_info->max_extent) > num_extents) 1284 root->fs_info->max_extent) > num_extents)
1232 return 0; 1285 return 0;
1233 1286
1234 BTRFS_I(inode)->delalloc_extents--; 1287 spin_lock(&BTRFS_I(inode)->accounting_lock);
1288 BTRFS_I(inode)->outstanding_extents--;
1289 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1235 1290
1236 return 0; 1291 return 0;
1237} 1292}
@@ -1253,7 +1308,9 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1253 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1308 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1254 struct btrfs_root *root = BTRFS_I(inode)->root; 1309 struct btrfs_root *root = BTRFS_I(inode)->root;
1255 1310
1256 BTRFS_I(inode)->delalloc_extents++; 1311 spin_lock(&BTRFS_I(inode)->accounting_lock);
1312 BTRFS_I(inode)->outstanding_extents++;
1313 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1257 btrfs_delalloc_reserve_space(root, inode, end - start + 1); 1314 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1258 spin_lock(&root->fs_info->delalloc_lock); 1315 spin_lock(&root->fs_info->delalloc_lock);
1259 BTRFS_I(inode)->delalloc_bytes += end - start + 1; 1316 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
@@ -1281,8 +1338,12 @@ static int btrfs_clear_bit_hook(struct inode *inode,
1281 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1338 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1282 struct btrfs_root *root = BTRFS_I(inode)->root; 1339 struct btrfs_root *root = BTRFS_I(inode)->root;
1283 1340
1284 BTRFS_I(inode)->delalloc_extents--; 1341 if (bits & EXTENT_DO_ACCOUNTING) {
1285 btrfs_unreserve_metadata_for_delalloc(root, inode, 1); 1342 spin_lock(&BTRFS_I(inode)->accounting_lock);
1343 BTRFS_I(inode)->outstanding_extents--;
1344 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1345 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1346 }
1286 1347
1287 spin_lock(&root->fs_info->delalloc_lock); 1348 spin_lock(&root->fs_info->delalloc_lock);
1288 if (state->end - state->start + 1 > 1349 if (state->end - state->start + 1 >
@@ -2442,7 +2503,19 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2442 2503
2443 root = BTRFS_I(dir)->root; 2504 root = BTRFS_I(dir)->root;
2444 2505
2506 /*
2507 * 5 items for unlink inode
2508 * 1 for orphan
2509 */
2510 ret = btrfs_reserve_metadata_space(root, 6);
2511 if (ret)
2512 return ret;
2513
2445 trans = btrfs_start_transaction(root, 1); 2514 trans = btrfs_start_transaction(root, 1);
2515 if (IS_ERR(trans)) {
2516 btrfs_unreserve_metadata_space(root, 6);
2517 return PTR_ERR(trans);
2518 }
2446 2519
2447 btrfs_set_trans_block_group(trans, dir); 2520 btrfs_set_trans_block_group(trans, dir);
2448 2521
@@ -2457,6 +2530,7 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2457 nr = trans->blocks_used; 2530 nr = trans->blocks_used;
2458 2531
2459 btrfs_end_transaction_throttle(trans, root); 2532 btrfs_end_transaction_throttle(trans, root);
2533 btrfs_unreserve_metadata_space(root, 6);
2460 btrfs_btree_balance_dirty(root, nr); 2534 btrfs_btree_balance_dirty(root, nr);
2461 return ret; 2535 return ret;
2462} 2536}
@@ -2537,7 +2611,16 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2537 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 2611 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2538 return -ENOTEMPTY; 2612 return -ENOTEMPTY;
2539 2613
2614 ret = btrfs_reserve_metadata_space(root, 5);
2615 if (ret)
2616 return ret;
2617
2540 trans = btrfs_start_transaction(root, 1); 2618 trans = btrfs_start_transaction(root, 1);
2619 if (IS_ERR(trans)) {
2620 btrfs_unreserve_metadata_space(root, 5);
2621 return PTR_ERR(trans);
2622 }
2623
2541 btrfs_set_trans_block_group(trans, dir); 2624 btrfs_set_trans_block_group(trans, dir);
2542 2625
2543 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 2626 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
@@ -2560,6 +2643,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2560out: 2643out:
2561 nr = trans->blocks_used; 2644 nr = trans->blocks_used;
2562 ret = btrfs_end_transaction_throttle(trans, root); 2645 ret = btrfs_end_transaction_throttle(trans, root);
2646 btrfs_unreserve_metadata_space(root, 5);
2563 btrfs_btree_balance_dirty(root, nr); 2647 btrfs_btree_balance_dirty(root, nr);
2564 2648
2565 if (ret && !err) 2649 if (ret && !err)
@@ -3000,12 +3084,22 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3000 3084
3001 if ((offset & (blocksize - 1)) == 0) 3085 if ((offset & (blocksize - 1)) == 0)
3002 goto out; 3086 goto out;
3087 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3088 if (ret)
3089 goto out;
3090
3091 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3092 if (ret)
3093 goto out;
3003 3094
3004 ret = -ENOMEM; 3095 ret = -ENOMEM;
3005again: 3096again:
3006 page = grab_cache_page(mapping, index); 3097 page = grab_cache_page(mapping, index);
3007 if (!page) 3098 if (!page) {
3099 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3100 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3008 goto out; 3101 goto out;
3102 }
3009 3103
3010 page_start = page_offset(page); 3104 page_start = page_offset(page);
3011 page_end = page_start + PAGE_CACHE_SIZE - 1; 3105 page_end = page_start + PAGE_CACHE_SIZE - 1;
@@ -3038,6 +3132,10 @@ again:
3038 goto again; 3132 goto again;
3039 } 3133 }
3040 3134
3135 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3136 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3137 GFP_NOFS);
3138
3041 ret = btrfs_set_extent_delalloc(inode, page_start, page_end); 3139 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3042 if (ret) { 3140 if (ret) {
3043 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3141 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
@@ -3056,6 +3154,9 @@ again:
3056 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3154 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3057 3155
3058out_unlock: 3156out_unlock:
3157 if (ret)
3158 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3159 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3059 unlock_page(page); 3160 unlock_page(page);
3060 page_cache_release(page); 3161 page_cache_release(page);
3061out: 3162out:
@@ -3079,7 +3180,9 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3079 if (size <= hole_start) 3180 if (size <= hole_start)
3080 return 0; 3181 return 0;
3081 3182
3082 btrfs_truncate_page(inode->i_mapping, inode->i_size); 3183 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3184 if (err)
3185 return err;
3083 3186
3084 while (1) { 3187 while (1) {
3085 struct btrfs_ordered_extent *ordered; 3188 struct btrfs_ordered_extent *ordered;
@@ -3448,6 +3551,7 @@ static noinline void init_btrfs_i(struct inode *inode)
3448 bi->generation = 0; 3551 bi->generation = 0;
3449 bi->sequence = 0; 3552 bi->sequence = 0;
3450 bi->last_trans = 0; 3553 bi->last_trans = 0;
3554 bi->last_sub_trans = 0;
3451 bi->logged_trans = 0; 3555 bi->logged_trans = 0;
3452 bi->delalloc_bytes = 0; 3556 bi->delalloc_bytes = 0;
3453 bi->reserved_bytes = 0; 3557 bi->reserved_bytes = 0;
@@ -3598,12 +3702,14 @@ static int btrfs_dentry_delete(struct dentry *dentry)
3598{ 3702{
3599 struct btrfs_root *root; 3703 struct btrfs_root *root;
3600 3704
3601 if (!dentry->d_inode) 3705 if (!dentry->d_inode && !IS_ROOT(dentry))
3602 return 0; 3706 dentry = dentry->d_parent;
3603 3707
3604 root = BTRFS_I(dentry->d_inode)->root; 3708 if (dentry->d_inode) {
3605 if (btrfs_root_refs(&root->root_item) == 0) 3709 root = BTRFS_I(dentry->d_inode)->root;
3606 return 1; 3710 if (btrfs_root_refs(&root->root_item) == 0)
3711 return 1;
3712 }
3607 return 0; 3713 return 0;
3608} 3714}
3609 3715
@@ -4808,7 +4914,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4808 */ 4914 */
4809 clear_extent_bit(tree, page_start, page_end, 4915 clear_extent_bit(tree, page_start, page_end,
4810 EXTENT_DIRTY | EXTENT_DELALLOC | 4916 EXTENT_DIRTY | EXTENT_DELALLOC |
4811 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); 4917 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4918 NULL, GFP_NOFS);
4812 /* 4919 /*
4813 * whoever cleared the private bit is responsible 4920 * whoever cleared the private bit is responsible
4814 * for the finish_ordered_io 4921 * for the finish_ordered_io
@@ -4821,8 +4928,8 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4821 lock_extent(tree, page_start, page_end, GFP_NOFS); 4928 lock_extent(tree, page_start, page_end, GFP_NOFS);
4822 } 4929 }
4823 clear_extent_bit(tree, page_start, page_end, 4930 clear_extent_bit(tree, page_start, page_end,
4824 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC, 4931 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4825 1, 1, NULL, GFP_NOFS); 4932 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
4826 __btrfs_releasepage(page, GFP_NOFS); 4933 __btrfs_releasepage(page, GFP_NOFS);
4827 4934
4828 ClearPageChecked(page); 4935 ClearPageChecked(page);
@@ -4917,7 +5024,8 @@ again:
4917 * prepare_pages in the normal write path. 5024 * prepare_pages in the normal write path.
4918 */ 5025 */
4919 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 5026 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
4920 EXTENT_DIRTY | EXTENT_DELALLOC, GFP_NOFS); 5027 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5028 GFP_NOFS);
4921 5029
4922 ret = btrfs_set_extent_delalloc(inode, page_start, page_end); 5030 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
4923 if (ret) { 5031 if (ret) {
@@ -4944,7 +5052,9 @@ again:
4944 set_page_dirty(page); 5052 set_page_dirty(page);
4945 SetPageUptodate(page); 5053 SetPageUptodate(page);
4946 5054
4947 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; 5055 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5056 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5057
4948 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 5058 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4949 5059
4950out_unlock: 5060out_unlock:
@@ -4969,7 +5079,9 @@ static void btrfs_truncate(struct inode *inode)
4969 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 5079 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4970 return; 5080 return;
4971 5081
4972 btrfs_truncate_page(inode->i_mapping, inode->i_size); 5082 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5083 if (ret)
5084 return;
4973 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 5085 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
4974 5086
4975 trans = btrfs_start_transaction(root, 1); 5087 trans = btrfs_start_transaction(root, 1);
@@ -5064,9 +5176,12 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
5064 if (!ei) 5176 if (!ei)
5065 return NULL; 5177 return NULL;
5066 ei->last_trans = 0; 5178 ei->last_trans = 0;
5179 ei->last_sub_trans = 0;
5067 ei->logged_trans = 0; 5180 ei->logged_trans = 0;
5068 ei->delalloc_extents = 0; 5181 ei->outstanding_extents = 0;
5069 ei->delalloc_reserved_extents = 0; 5182 ei->reserved_extents = 0;
5183 ei->root = NULL;
5184 spin_lock_init(&ei->accounting_lock);
5070 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 5185 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
5071 INIT_LIST_HEAD(&ei->i_orphan); 5186 INIT_LIST_HEAD(&ei->i_orphan);
5072 INIT_LIST_HEAD(&ei->ordered_operations); 5187 INIT_LIST_HEAD(&ei->ordered_operations);
@@ -5082,6 +5197,14 @@ void btrfs_destroy_inode(struct inode *inode)
5082 WARN_ON(inode->i_data.nrpages); 5197 WARN_ON(inode->i_data.nrpages);
5083 5198
5084 /* 5199 /*
5200 * This can happen where we create an inode, but somebody else also
5201 * created the same inode and we need to destroy the one we already
5202 * created.
5203 */
5204 if (!root)
5205 goto free;
5206
5207 /*
5085 * Make sure we're properly removed from the ordered operation 5208 * Make sure we're properly removed from the ordered operation
5086 * lists. 5209 * lists.
5087 */ 5210 */
@@ -5116,6 +5239,7 @@ void btrfs_destroy_inode(struct inode *inode)
5116 } 5239 }
5117 inode_tree_del(inode); 5240 inode_tree_del(inode);
5118 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 5241 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
5242free:
5119 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); 5243 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5120} 5244}
5121 5245
@@ -5221,11 +5345,14 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5221 return -ENOTEMPTY; 5345 return -ENOTEMPTY;
5222 5346
5223 /* 5347 /*
5224 * 2 items for dir items 5348 * We want to reserve the absolute worst case amount of items. So if
5225 * 1 item for orphan entry 5349 * both inodes are subvols and we need to unlink them then that would
5226 * 1 item for ref 5350 * require 4 item modifications, but if they are both normal inodes it
5351 * would require 5 item modifications, so we'll assume their normal
5352 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5353 * should cover the worst case number of items we'll modify.
5227 */ 5354 */
5228 ret = btrfs_reserve_metadata_space(root, 4); 5355 ret = btrfs_reserve_metadata_space(root, 11);
5229 if (ret) 5356 if (ret)
5230 return ret; 5357 return ret;
5231 5358
@@ -5341,7 +5468,7 @@ out_fail:
5341 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5468 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5342 up_read(&root->fs_info->subvol_sem); 5469 up_read(&root->fs_info->subvol_sem);
5343 5470
5344 btrfs_unreserve_metadata_space(root, 4); 5471 btrfs_unreserve_metadata_space(root, 11);
5345 return ret; 5472 return ret;
5346} 5473}
5347 5474
@@ -5805,6 +5932,6 @@ static const struct inode_operations btrfs_symlink_inode_operations = {
5805 .removexattr = btrfs_removexattr, 5932 .removexattr = btrfs_removexattr,
5806}; 5933};
5807 5934
5808struct dentry_operations btrfs_dentry_operations = { 5935const struct dentry_operations btrfs_dentry_operations = {
5809 .d_delete = btrfs_dentry_delete, 5936 .d_delete = btrfs_dentry_delete,
5810}; 5937};