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.c1096
1 files changed, 861 insertions, 235 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 59cba180fe83..b3ad168a0bfc 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -55,14 +55,14 @@ struct btrfs_iget_args {
55 struct btrfs_root *root; 55 struct btrfs_root *root;
56}; 56};
57 57
58static struct inode_operations btrfs_dir_inode_operations; 58static const struct inode_operations btrfs_dir_inode_operations;
59static struct inode_operations btrfs_symlink_inode_operations; 59static const struct inode_operations btrfs_symlink_inode_operations;
60static struct inode_operations btrfs_dir_ro_inode_operations; 60static const struct inode_operations btrfs_dir_ro_inode_operations;
61static struct inode_operations btrfs_special_inode_operations; 61static const struct inode_operations btrfs_special_inode_operations;
62static struct inode_operations btrfs_file_inode_operations; 62static const struct inode_operations btrfs_file_inode_operations;
63static struct address_space_operations btrfs_aops; 63static const struct address_space_operations btrfs_aops;
64static struct address_space_operations btrfs_symlink_aops; 64static const struct address_space_operations btrfs_symlink_aops;
65static struct file_operations btrfs_dir_file_operations; 65static const struct file_operations btrfs_dir_file_operations;
66static struct extent_io_ops btrfs_extent_io_ops; 66static struct extent_io_ops btrfs_extent_io_ops;
67 67
68static struct kmem_cache *btrfs_inode_cachep; 68static struct kmem_cache *btrfs_inode_cachep;
@@ -231,7 +231,8 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
231 } 231 }
232 232
233 ret = btrfs_drop_extents(trans, root, inode, start, 233 ret = btrfs_drop_extents(trans, root, inode, start,
234 aligned_end, aligned_end, start, &hint_byte); 234 aligned_end, aligned_end, start,
235 &hint_byte, 1);
235 BUG_ON(ret); 236 BUG_ON(ret);
236 237
237 if (isize > actual_end) 238 if (isize > actual_end)
@@ -240,7 +241,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
240 inline_len, compressed_size, 241 inline_len, compressed_size,
241 compressed_pages); 242 compressed_pages);
242 BUG_ON(ret); 243 BUG_ON(ret);
243 btrfs_drop_extent_cache(inode, start, aligned_end, 0); 244 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
244 return 0; 245 return 0;
245} 246}
246 247
@@ -423,9 +424,12 @@ again:
423 * and free up our temp pages. 424 * and free up our temp pages.
424 */ 425 */
425 extent_clear_unlock_delalloc(inode, 426 extent_clear_unlock_delalloc(inode,
426 &BTRFS_I(inode)->io_tree, 427 &BTRFS_I(inode)->io_tree,
427 start, end, NULL, 1, 0, 428 start, end, NULL,
428 0, 1, 1, 1); 429 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
430 EXTENT_CLEAR_DELALLOC |
431 EXTENT_CLEAR_ACCOUNTING |
432 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
429 ret = 0; 433 ret = 0;
430 goto free_pages_out; 434 goto free_pages_out;
431 } 435 }
@@ -534,7 +538,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
534 struct btrfs_root *root = BTRFS_I(inode)->root; 538 struct btrfs_root *root = BTRFS_I(inode)->root;
535 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 539 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
536 struct extent_io_tree *io_tree; 540 struct extent_io_tree *io_tree;
537 int ret; 541 int ret = 0;
538 542
539 if (list_empty(&async_cow->extents)) 543 if (list_empty(&async_cow->extents))
540 return 0; 544 return 0;
@@ -548,6 +552,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
548 552
549 io_tree = &BTRFS_I(inode)->io_tree; 553 io_tree = &BTRFS_I(inode)->io_tree;
550 554
555retry:
551 /* did the compression code fall back to uncompressed IO? */ 556 /* did the compression code fall back to uncompressed IO? */
552 if (!async_extent->pages) { 557 if (!async_extent->pages) {
553 int page_started = 0; 558 int page_started = 0;
@@ -558,11 +563,11 @@ static noinline int submit_compressed_extents(struct inode *inode,
558 async_extent->ram_size - 1, GFP_NOFS); 563 async_extent->ram_size - 1, GFP_NOFS);
559 564
560 /* allocate blocks */ 565 /* allocate blocks */
561 cow_file_range(inode, async_cow->locked_page, 566 ret = cow_file_range(inode, async_cow->locked_page,
562 async_extent->start, 567 async_extent->start,
563 async_extent->start + 568 async_extent->start +
564 async_extent->ram_size - 1, 569 async_extent->ram_size - 1,
565 &page_started, &nr_written, 0); 570 &page_started, &nr_written, 0);
566 571
567 /* 572 /*
568 * if page_started, cow_file_range inserted an 573 * if page_started, cow_file_range inserted an
@@ -570,7 +575,7 @@ static noinline int submit_compressed_extents(struct inode *inode,
570 * and IO for us. Otherwise, we need to submit 575 * and IO for us. Otherwise, we need to submit
571 * all those pages down to the drive. 576 * all those pages down to the drive.
572 */ 577 */
573 if (!page_started) 578 if (!page_started && !ret)
574 extent_write_locked_range(io_tree, 579 extent_write_locked_range(io_tree,
575 inode, async_extent->start, 580 inode, async_extent->start,
576 async_extent->start + 581 async_extent->start +
@@ -598,7 +603,21 @@ static noinline int submit_compressed_extents(struct inode *inode,
598 async_extent->compressed_size, 603 async_extent->compressed_size,
599 0, alloc_hint, 604 0, alloc_hint,
600 (u64)-1, &ins, 1); 605 (u64)-1, &ins, 1);
601 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
602 em = alloc_extent_map(GFP_NOFS); 621 em = alloc_extent_map(GFP_NOFS);
603 em->start = async_extent->start; 622 em->start = async_extent->start;
604 em->len = async_extent->ram_size; 623 em->len = async_extent->ram_size;
@@ -611,9 +630,9 @@ static noinline int submit_compressed_extents(struct inode *inode,
611 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 630 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
612 631
613 while (1) { 632 while (1) {
614 spin_lock(&em_tree->lock); 633 write_lock(&em_tree->lock);
615 ret = add_extent_mapping(em_tree, em); 634 ret = add_extent_mapping(em_tree, em);
616 spin_unlock(&em_tree->lock); 635 write_unlock(&em_tree->lock);
617 if (ret != -EEXIST) { 636 if (ret != -EEXIST) {
618 free_extent_map(em); 637 free_extent_map(em);
619 break; 638 break;
@@ -636,11 +655,14 @@ static noinline int submit_compressed_extents(struct inode *inode,
636 * clear dirty, set writeback and unlock the pages. 655 * clear dirty, set writeback and unlock the pages.
637 */ 656 */
638 extent_clear_unlock_delalloc(inode, 657 extent_clear_unlock_delalloc(inode,
639 &BTRFS_I(inode)->io_tree, 658 &BTRFS_I(inode)->io_tree,
640 async_extent->start, 659 async_extent->start,
641 async_extent->start + 660 async_extent->start +
642 async_extent->ram_size - 1, 661 async_extent->ram_size - 1,
643 NULL, 1, 1, 0, 1, 1, 0); 662 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
663 EXTENT_CLEAR_UNLOCK |
664 EXTENT_CLEAR_DELALLOC |
665 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
644 666
645 ret = btrfs_submit_compressed_write(inode, 667 ret = btrfs_submit_compressed_write(inode,
646 async_extent->start, 668 async_extent->start,
@@ -711,9 +733,15 @@ static noinline int cow_file_range(struct inode *inode,
711 start, end, 0, NULL); 733 start, end, 0, NULL);
712 if (ret == 0) { 734 if (ret == 0) {
713 extent_clear_unlock_delalloc(inode, 735 extent_clear_unlock_delalloc(inode,
714 &BTRFS_I(inode)->io_tree, 736 &BTRFS_I(inode)->io_tree,
715 start, end, NULL, 1, 1, 737 start, end, NULL,
716 1, 1, 1, 1); 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);
717 *nr_written = *nr_written + 745 *nr_written = *nr_written +
718 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 746 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
719 *page_started = 1; 747 *page_started = 1;
@@ -725,9 +753,34 @@ static noinline int cow_file_range(struct inode *inode,
725 BUG_ON(disk_num_bytes > 753 BUG_ON(disk_num_bytes >
726 btrfs_super_total_bytes(&root->fs_info->super_copy)); 754 btrfs_super_total_bytes(&root->fs_info->super_copy));
727 755
756
757 read_lock(&BTRFS_I(inode)->extent_tree.lock);
758 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
759 start, num_bytes);
760 if (em) {
761 /*
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 }
777 }
778 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
728 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); 779 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
729 780
730 while (disk_num_bytes > 0) { 781 while (disk_num_bytes > 0) {
782 unsigned long op;
783
731 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);
732 ret = btrfs_reserve_extent(trans, root, cur_alloc_size, 785 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
733 root->sectorsize, 0, alloc_hint, 786 root->sectorsize, 0, alloc_hint,
@@ -737,7 +790,6 @@ static noinline int cow_file_range(struct inode *inode,
737 em = alloc_extent_map(GFP_NOFS); 790 em = alloc_extent_map(GFP_NOFS);
738 em->start = start; 791 em->start = start;
739 em->orig_start = em->start; 792 em->orig_start = em->start;
740
741 ram_size = ins.offset; 793 ram_size = ins.offset;
742 em->len = ins.offset; 794 em->len = ins.offset;
743 795
@@ -747,9 +799,9 @@ static noinline int cow_file_range(struct inode *inode,
747 set_bit(EXTENT_FLAG_PINNED, &em->flags); 799 set_bit(EXTENT_FLAG_PINNED, &em->flags);
748 800
749 while (1) { 801 while (1) {
750 spin_lock(&em_tree->lock); 802 write_lock(&em_tree->lock);
751 ret = add_extent_mapping(em_tree, em); 803 ret = add_extent_mapping(em_tree, em);
752 spin_unlock(&em_tree->lock); 804 write_unlock(&em_tree->lock);
753 if (ret != -EEXIST) { 805 if (ret != -EEXIST) {
754 free_extent_map(em); 806 free_extent_map(em);
755 break; 807 break;
@@ -776,11 +828,17 @@ static noinline int cow_file_range(struct inode *inode,
776 /* we're not doing compressed IO, don't unlock the first 828 /* we're not doing compressed IO, don't unlock the first
777 * page (which the caller expects to stay locked), don't 829 * page (which the caller expects to stay locked), don't
778 * clear any dirty bits and don't set any writeback bits 830 * clear any dirty bits and don't set any writeback bits
831 *
832 * Do set the Private2 bit so we know this page was properly
833 * setup for writepage
779 */ 834 */
835 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
836 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
837 EXTENT_SET_PRIVATE2;
838
780 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 839 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
781 start, start + ram_size - 1, 840 start, start + ram_size - 1,
782 locked_page, unlock, 1, 841 locked_page, op);
783 1, 0, 0, 0);
784 disk_num_bytes -= cur_alloc_size; 842 disk_num_bytes -= cur_alloc_size;
785 num_bytes -= cur_alloc_size; 843 num_bytes -= cur_alloc_size;
786 alloc_hint = ins.objectid + ins.offset; 844 alloc_hint = ins.objectid + ins.offset;
@@ -852,8 +910,8 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
852 u64 cur_end; 910 u64 cur_end;
853 int limit = 10 * 1024 * 1042; 911 int limit = 10 * 1024 * 1042;
854 912
855 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,
856 EXTENT_DELALLOC, 1, 0, GFP_NOFS); 914 1, 0, NULL, GFP_NOFS);
857 while (start < end) { 915 while (start < end) {
858 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS); 916 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
859 async_cow->inode = inode; 917 async_cow->inode = inode;
@@ -994,6 +1052,7 @@ next_slot:
994 1052
995 if (found_key.offset > cur_offset) { 1053 if (found_key.offset > cur_offset) {
996 extent_end = found_key.offset; 1054 extent_end = found_key.offset;
1055 extent_type = 0;
997 goto out_check; 1056 goto out_check;
998 } 1057 }
999 1058
@@ -1080,9 +1139,9 @@ out_check:
1080 em->bdev = root->fs_info->fs_devices->latest_bdev; 1139 em->bdev = root->fs_info->fs_devices->latest_bdev;
1081 set_bit(EXTENT_FLAG_PINNED, &em->flags); 1140 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1082 while (1) { 1141 while (1) {
1083 spin_lock(&em_tree->lock); 1142 write_lock(&em_tree->lock);
1084 ret = add_extent_mapping(em_tree, em); 1143 ret = add_extent_mapping(em_tree, em);
1085 spin_unlock(&em_tree->lock); 1144 write_unlock(&em_tree->lock);
1086 if (ret != -EEXIST) { 1145 if (ret != -EEXIST) {
1087 free_extent_map(em); 1146 free_extent_map(em);
1088 break; 1147 break;
@@ -1100,8 +1159,10 @@ out_check:
1100 BUG_ON(ret); 1159 BUG_ON(ret);
1101 1160
1102 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree, 1161 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1103 cur_offset, cur_offset + num_bytes - 1, 1162 cur_offset, cur_offset + num_bytes - 1,
1104 locked_page, 1, 1, 1, 0, 0, 0); 1163 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1164 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1165 EXTENT_SET_PRIVATE2);
1105 cur_offset = extent_end; 1166 cur_offset = extent_end;
1106 if (cur_offset > end) 1167 if (cur_offset > end)
1107 break; 1168 break;
@@ -1147,6 +1208,89 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1147 return ret; 1208 return ret;
1148} 1209}
1149 1210
1211static int btrfs_split_extent_hook(struct inode *inode,
1212 struct extent_state *orig, u64 split)
1213{
1214 struct btrfs_root *root = BTRFS_I(inode)->root;
1215 u64 size;
1216
1217 if (!(orig->state & EXTENT_DELALLOC))
1218 return 0;
1219
1220 size = orig->end - orig->start + 1;
1221 if (size > root->fs_info->max_extent) {
1222 u64 num_extents;
1223 u64 new_size;
1224
1225 new_size = orig->end - split + 1;
1226 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1227 root->fs_info->max_extent);
1228
1229 /*
1230 * if we break a large extent up then leave oustanding_extents
1231 * be, since we've already accounted for the large extent.
1232 */
1233 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1234 root->fs_info->max_extent) < num_extents)
1235 return 0;
1236 }
1237
1238 spin_lock(&BTRFS_I(inode)->accounting_lock);
1239 BTRFS_I(inode)->outstanding_extents++;
1240 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1241
1242 return 0;
1243}
1244
1245/*
1246 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1247 * extents so we can keep track of new extents that are just merged onto old
1248 * extents, such as when we are doing sequential writes, so we can properly
1249 * account for the metadata space we'll need.
1250 */
1251static int btrfs_merge_extent_hook(struct inode *inode,
1252 struct extent_state *new,
1253 struct extent_state *other)
1254{
1255 struct btrfs_root *root = BTRFS_I(inode)->root;
1256 u64 new_size, old_size;
1257 u64 num_extents;
1258
1259 /* not delalloc, ignore it */
1260 if (!(other->state & EXTENT_DELALLOC))
1261 return 0;
1262
1263 old_size = other->end - other->start + 1;
1264 if (new->start < other->start)
1265 new_size = other->end - new->start + 1;
1266 else
1267 new_size = new->end - other->start + 1;
1268
1269 /* we're not bigger than the max, unreserve the space and go */
1270 if (new_size <= root->fs_info->max_extent) {
1271 spin_lock(&BTRFS_I(inode)->accounting_lock);
1272 BTRFS_I(inode)->outstanding_extents--;
1273 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1274 return 0;
1275 }
1276
1277 /*
1278 * If we grew by another max_extent, just return, we want to keep that
1279 * reserved amount.
1280 */
1281 num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1282 root->fs_info->max_extent);
1283 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1284 root->fs_info->max_extent) > num_extents)
1285 return 0;
1286
1287 spin_lock(&BTRFS_I(inode)->accounting_lock);
1288 BTRFS_I(inode)->outstanding_extents--;
1289 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1290
1291 return 0;
1292}
1293
1150/* 1294/*
1151 * extent_io.c set_bit_hook, used to track delayed allocation 1295 * extent_io.c set_bit_hook, used to track delayed allocation
1152 * bytes in this file, and to maintain the list of inodes that 1296 * bytes in this file, and to maintain the list of inodes that
@@ -1155,6 +1299,7 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1155static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, 1299static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1156 unsigned long old, unsigned long bits) 1300 unsigned long old, unsigned long bits)
1157{ 1301{
1302
1158 /* 1303 /*
1159 * set_bit and clear bit hooks normally require _irqsave/restore 1304 * set_bit and clear bit hooks normally require _irqsave/restore
1160 * but in this case, we are only testeing for the DELALLOC 1305 * but in this case, we are only testeing for the DELALLOC
@@ -1162,6 +1307,10 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1162 */ 1307 */
1163 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1308 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1164 struct btrfs_root *root = BTRFS_I(inode)->root; 1309 struct btrfs_root *root = BTRFS_I(inode)->root;
1310
1311 spin_lock(&BTRFS_I(inode)->accounting_lock);
1312 BTRFS_I(inode)->outstanding_extents++;
1313 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1165 btrfs_delalloc_reserve_space(root, inode, end - start + 1); 1314 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1166 spin_lock(&root->fs_info->delalloc_lock); 1315 spin_lock(&root->fs_info->delalloc_lock);
1167 BTRFS_I(inode)->delalloc_bytes += end - start + 1; 1316 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
@@ -1178,22 +1327,31 @@ static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1178/* 1327/*
1179 * extent_io.c clear_bit_hook, see set_bit_hook for why 1328 * extent_io.c clear_bit_hook, see set_bit_hook for why
1180 */ 1329 */
1181static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, 1330static int btrfs_clear_bit_hook(struct inode *inode,
1182 unsigned long old, unsigned long bits) 1331 struct extent_state *state, unsigned long bits)
1183{ 1332{
1184 /* 1333 /*
1185 * set_bit and clear bit hooks normally require _irqsave/restore 1334 * set_bit and clear bit hooks normally require _irqsave/restore
1186 * but in this case, we are only testeing for the DELALLOC 1335 * but in this case, we are only testeing for the DELALLOC
1187 * bit, which is only set or cleared with irqs on 1336 * bit, which is only set or cleared with irqs on
1188 */ 1337 */
1189 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { 1338 if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1190 struct btrfs_root *root = BTRFS_I(inode)->root; 1339 struct btrfs_root *root = BTRFS_I(inode)->root;
1191 1340
1341 if (bits & EXTENT_DO_ACCOUNTING) {
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 }
1347
1192 spin_lock(&root->fs_info->delalloc_lock); 1348 spin_lock(&root->fs_info->delalloc_lock);
1193 if (end - start + 1 > root->fs_info->delalloc_bytes) { 1349 if (state->end - state->start + 1 >
1350 root->fs_info->delalloc_bytes) {
1194 printk(KERN_INFO "btrfs warning: delalloc account " 1351 printk(KERN_INFO "btrfs warning: delalloc account "
1195 "%llu %llu\n", 1352 "%llu %llu\n",
1196 (unsigned long long)end - start + 1, 1353 (unsigned long long)
1354 state->end - state->start + 1,
1197 (unsigned long long) 1355 (unsigned long long)
1198 root->fs_info->delalloc_bytes); 1356 root->fs_info->delalloc_bytes);
1199 btrfs_delalloc_free_space(root, inode, (u64)-1); 1357 btrfs_delalloc_free_space(root, inode, (u64)-1);
@@ -1201,9 +1359,12 @@ static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
1201 BTRFS_I(inode)->delalloc_bytes = 0; 1359 BTRFS_I(inode)->delalloc_bytes = 0;
1202 } else { 1360 } else {
1203 btrfs_delalloc_free_space(root, inode, 1361 btrfs_delalloc_free_space(root, inode,
1204 end - start + 1); 1362 state->end -
1205 root->fs_info->delalloc_bytes -= end - start + 1; 1363 state->start + 1);
1206 BTRFS_I(inode)->delalloc_bytes -= end - start + 1; 1364 root->fs_info->delalloc_bytes -= state->end -
1365 state->start + 1;
1366 BTRFS_I(inode)->delalloc_bytes -= state->end -
1367 state->start + 1;
1207 } 1368 }
1208 if (BTRFS_I(inode)->delalloc_bytes == 0 && 1369 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1209 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { 1370 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
@@ -1374,10 +1535,8 @@ again:
1374 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); 1535 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1375 1536
1376 /* already ordered? We're done */ 1537 /* already ordered? We're done */
1377 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, 1538 if (PagePrivate2(page))
1378 EXTENT_ORDERED, 0)) {
1379 goto out; 1539 goto out;
1380 }
1381 1540
1382 ordered = btrfs_lookup_ordered_extent(inode, page_start); 1541 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1383 if (ordered) { 1542 if (ordered) {
@@ -1413,11 +1572,9 @@ static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1413 struct inode *inode = page->mapping->host; 1572 struct inode *inode = page->mapping->host;
1414 struct btrfs_writepage_fixup *fixup; 1573 struct btrfs_writepage_fixup *fixup;
1415 struct btrfs_root *root = BTRFS_I(inode)->root; 1574 struct btrfs_root *root = BTRFS_I(inode)->root;
1416 int ret;
1417 1575
1418 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, 1576 /* this page is properly in the ordered list */
1419 EXTENT_ORDERED, 0); 1577 if (TestClearPagePrivate2(page))
1420 if (ret)
1421 return 0; 1578 return 0;
1422 1579
1423 if (PageChecked(page)) 1580 if (PageChecked(page))
@@ -1455,9 +1612,19 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1455 BUG_ON(!path); 1612 BUG_ON(!path);
1456 1613
1457 path->leave_spinning = 1; 1614 path->leave_spinning = 1;
1615
1616 /*
1617 * we may be replacing one extent in the tree with another.
1618 * The new extent is pinned in the extent map, and we don't want
1619 * to drop it from the cache until it is completely in the btree.
1620 *
1621 * So, tell btrfs_drop_extents to leave this extent in the cache.
1622 * the caller is expected to unpin it and allow it to be merged
1623 * with the others.
1624 */
1458 ret = btrfs_drop_extents(trans, root, inode, file_pos, 1625 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1459 file_pos + num_bytes, locked_end, 1626 file_pos + num_bytes, locked_end,
1460 file_pos, &hint); 1627 file_pos, &hint, 0);
1461 BUG_ON(ret); 1628 BUG_ON(ret);
1462 1629
1463 ins.objectid = inode->i_ino; 1630 ins.objectid = inode->i_ino;
@@ -1485,7 +1652,6 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1485 btrfs_mark_buffer_dirty(leaf); 1652 btrfs_mark_buffer_dirty(leaf);
1486 1653
1487 inode_add_bytes(inode, num_bytes); 1654 inode_add_bytes(inode, num_bytes);
1488 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1489 1655
1490 ins.objectid = disk_bytenr; 1656 ins.objectid = disk_bytenr;
1491 ins.offset = disk_num_bytes; 1657 ins.offset = disk_num_bytes;
@@ -1596,6 +1762,9 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1596 ordered_extent->len, 1762 ordered_extent->len,
1597 compressed, 0, 0, 1763 compressed, 0, 0,
1598 BTRFS_FILE_EXTENT_REG); 1764 BTRFS_FILE_EXTENT_REG);
1765 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1766 ordered_extent->file_offset,
1767 ordered_extent->len);
1599 BUG_ON(ret); 1768 BUG_ON(ret);
1600 } 1769 }
1601 unlock_extent(io_tree, ordered_extent->file_offset, 1770 unlock_extent(io_tree, ordered_extent->file_offset,
@@ -1623,6 +1792,7 @@ nocow:
1623static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, 1792static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1624 struct extent_state *state, int uptodate) 1793 struct extent_state *state, int uptodate)
1625{ 1794{
1795 ClearPagePrivate2(page);
1626 return btrfs_finish_ordered_io(page->mapping->host, start, end); 1796 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1627} 1797}
1628 1798
@@ -1669,13 +1839,13 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1669 failrec->last_mirror = 0; 1839 failrec->last_mirror = 0;
1670 failrec->bio_flags = 0; 1840 failrec->bio_flags = 0;
1671 1841
1672 spin_lock(&em_tree->lock); 1842 read_lock(&em_tree->lock);
1673 em = lookup_extent_mapping(em_tree, start, failrec->len); 1843 em = lookup_extent_mapping(em_tree, start, failrec->len);
1674 if (em->start > start || em->start + em->len < start) { 1844 if (em->start > start || em->start + em->len < start) {
1675 free_extent_map(em); 1845 free_extent_map(em);
1676 em = NULL; 1846 em = NULL;
1677 } 1847 }
1678 spin_unlock(&em_tree->lock); 1848 read_unlock(&em_tree->lock);
1679 1849
1680 if (!em || IS_ERR(em)) { 1850 if (!em || IS_ERR(em)) {
1681 kfree(failrec); 1851 kfree(failrec);
@@ -1794,7 +1964,7 @@ static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1794 return 0; 1964 return 0;
1795 1965
1796 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID && 1966 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1797 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) { 1967 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1798 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM, 1968 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1799 GFP_NOFS); 1969 GFP_NOFS);
1800 return 0; 1970 return 0;
@@ -2333,7 +2503,19 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2333 2503
2334 root = BTRFS_I(dir)->root; 2504 root = BTRFS_I(dir)->root;
2335 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
2336 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 }
2337 2519
2338 btrfs_set_trans_block_group(trans, dir); 2520 btrfs_set_trans_block_group(trans, dir);
2339 2521
@@ -2348,10 +2530,74 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2348 nr = trans->blocks_used; 2530 nr = trans->blocks_used;
2349 2531
2350 btrfs_end_transaction_throttle(trans, root); 2532 btrfs_end_transaction_throttle(trans, root);
2533 btrfs_unreserve_metadata_space(root, 6);
2351 btrfs_btree_balance_dirty(root, nr); 2534 btrfs_btree_balance_dirty(root, nr);
2352 return ret; 2535 return ret;
2353} 2536}
2354 2537
2538int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2539 struct btrfs_root *root,
2540 struct inode *dir, u64 objectid,
2541 const char *name, int name_len)
2542{
2543 struct btrfs_path *path;
2544 struct extent_buffer *leaf;
2545 struct btrfs_dir_item *di;
2546 struct btrfs_key key;
2547 u64 index;
2548 int ret;
2549
2550 path = btrfs_alloc_path();
2551 if (!path)
2552 return -ENOMEM;
2553
2554 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2555 name, name_len, -1);
2556 BUG_ON(!di || IS_ERR(di));
2557
2558 leaf = path->nodes[0];
2559 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2560 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2561 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2562 BUG_ON(ret);
2563 btrfs_release_path(root, path);
2564
2565 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2566 objectid, root->root_key.objectid,
2567 dir->i_ino, &index, name, name_len);
2568 if (ret < 0) {
2569 BUG_ON(ret != -ENOENT);
2570 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2571 name, name_len);
2572 BUG_ON(!di || IS_ERR(di));
2573
2574 leaf = path->nodes[0];
2575 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2576 btrfs_release_path(root, path);
2577 index = key.offset;
2578 }
2579
2580 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2581 index, name, name_len, -1);
2582 BUG_ON(!di || IS_ERR(di));
2583
2584 leaf = path->nodes[0];
2585 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2586 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2587 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2588 BUG_ON(ret);
2589 btrfs_release_path(root, path);
2590
2591 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2592 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2593 ret = btrfs_update_inode(trans, root, dir);
2594 BUG_ON(ret);
2595 dir->i_sb->s_dirt = 1;
2596
2597 btrfs_free_path(path);
2598 return 0;
2599}
2600
2355static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) 2601static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2356{ 2602{
2357 struct inode *inode = dentry->d_inode; 2603 struct inode *inode = dentry->d_inode;
@@ -2361,31 +2607,43 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2361 struct btrfs_trans_handle *trans; 2607 struct btrfs_trans_handle *trans;
2362 unsigned long nr = 0; 2608 unsigned long nr = 0;
2363 2609
2364 /*
2365 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2366 * the root of a subvolume or snapshot
2367 */
2368 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || 2610 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2369 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) { 2611 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2370 return -ENOTEMPTY; 2612 return -ENOTEMPTY;
2371 } 2613
2614 ret = btrfs_reserve_metadata_space(root, 5);
2615 if (ret)
2616 return ret;
2372 2617
2373 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
2374 btrfs_set_trans_block_group(trans, dir); 2624 btrfs_set_trans_block_group(trans, dir);
2375 2625
2626 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2627 err = btrfs_unlink_subvol(trans, root, dir,
2628 BTRFS_I(inode)->location.objectid,
2629 dentry->d_name.name,
2630 dentry->d_name.len);
2631 goto out;
2632 }
2633
2376 err = btrfs_orphan_add(trans, inode); 2634 err = btrfs_orphan_add(trans, inode);
2377 if (err) 2635 if (err)
2378 goto fail_trans; 2636 goto out;
2379 2637
2380 /* now the directory is empty */ 2638 /* now the directory is empty */
2381 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, 2639 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2382 dentry->d_name.name, dentry->d_name.len); 2640 dentry->d_name.name, dentry->d_name.len);
2383 if (!err) 2641 if (!err)
2384 btrfs_i_size_write(inode, 0); 2642 btrfs_i_size_write(inode, 0);
2385 2643out:
2386fail_trans:
2387 nr = trans->blocks_used; 2644 nr = trans->blocks_used;
2388 ret = btrfs_end_transaction_throttle(trans, root); 2645 ret = btrfs_end_transaction_throttle(trans, root);
2646 btrfs_unreserve_metadata_space(root, 5);
2389 btrfs_btree_balance_dirty(root, nr); 2647 btrfs_btree_balance_dirty(root, nr);
2390 2648
2391 if (ret && !err) 2649 if (ret && !err)
@@ -2826,12 +3084,22 @@ static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2826 3084
2827 if ((offset & (blocksize - 1)) == 0) 3085 if ((offset & (blocksize - 1)) == 0)
2828 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;
2829 3094
2830 ret = -ENOMEM; 3095 ret = -ENOMEM;
2831again: 3096again:
2832 page = grab_cache_page(mapping, index); 3097 page = grab_cache_page(mapping, index);
2833 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);
2834 goto out; 3101 goto out;
3102 }
2835 3103
2836 page_start = page_offset(page); 3104 page_start = page_offset(page);
2837 page_end = page_start + PAGE_CACHE_SIZE - 1; 3105 page_end = page_start + PAGE_CACHE_SIZE - 1;
@@ -2864,7 +3132,16 @@ again:
2864 goto again; 3132 goto again;
2865 } 3133 }
2866 3134
2867 btrfs_set_extent_delalloc(inode, page_start, page_end); 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
3139 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3140 if (ret) {
3141 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3142 goto out_unlock;
3143 }
3144
2868 ret = 0; 3145 ret = 0;
2869 if (offset != PAGE_CACHE_SIZE) { 3146 if (offset != PAGE_CACHE_SIZE) {
2870 kaddr = kmap(page); 3147 kaddr = kmap(page);
@@ -2877,6 +3154,9 @@ again:
2877 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3154 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2878 3155
2879out_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);
2880 unlock_page(page); 3160 unlock_page(page);
2881 page_cache_release(page); 3161 page_cache_release(page);
2882out: 3162out:
@@ -2895,17 +3175,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
2895 u64 last_byte; 3175 u64 last_byte;
2896 u64 cur_offset; 3176 u64 cur_offset;
2897 u64 hole_size; 3177 u64 hole_size;
2898 int err; 3178 int err = 0;
2899 3179
2900 if (size <= hole_start) 3180 if (size <= hole_start)
2901 return 0; 3181 return 0;
2902 3182
2903 err = btrfs_check_metadata_free_space(root); 3183 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
2904 if (err) 3184 if (err)
2905 return err; 3185 return err;
2906 3186
2907 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2908
2909 while (1) { 3187 while (1) {
2910 struct btrfs_ordered_extent *ordered; 3188 struct btrfs_ordered_extent *ordered;
2911 btrfs_wait_ordered_range(inode, hole_start, 3189 btrfs_wait_ordered_range(inode, hole_start,
@@ -2935,15 +3213,21 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
2935 cur_offset, 3213 cur_offset,
2936 cur_offset + hole_size, 3214 cur_offset + hole_size,
2937 block_end, 3215 block_end,
2938 cur_offset, &hint_byte); 3216 cur_offset, &hint_byte, 1);
2939 if (err) 3217 if (err)
2940 break; 3218 break;
3219
3220 err = btrfs_reserve_metadata_space(root, 1);
3221 if (err)
3222 break;
3223
2941 err = btrfs_insert_file_extent(trans, root, 3224 err = btrfs_insert_file_extent(trans, root,
2942 inode->i_ino, cur_offset, 0, 3225 inode->i_ino, cur_offset, 0,
2943 0, hole_size, 0, hole_size, 3226 0, hole_size, 0, hole_size,
2944 0, 0, 0); 3227 0, 0, 0);
2945 btrfs_drop_extent_cache(inode, hole_start, 3228 btrfs_drop_extent_cache(inode, hole_start,
2946 last_byte - 1, 0); 3229 last_byte - 1, 0);
3230 btrfs_unreserve_metadata_space(root, 1);
2947 } 3231 }
2948 free_extent_map(em); 3232 free_extent_map(em);
2949 cur_offset = last_byte; 3233 cur_offset = last_byte;
@@ -3003,6 +3287,11 @@ void btrfs_delete_inode(struct inode *inode)
3003 } 3287 }
3004 btrfs_wait_ordered_range(inode, 0, (u64)-1); 3288 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3005 3289
3290 if (inode->i_nlink > 0) {
3291 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3292 goto no_delete;
3293 }
3294
3006 btrfs_i_size_write(inode, 0); 3295 btrfs_i_size_write(inode, 0);
3007 trans = btrfs_join_transaction(root, 1); 3296 trans = btrfs_join_transaction(root, 1);
3008 3297
@@ -3070,29 +3359,67 @@ out_err:
3070 * is kind of like crossing a mount point. 3359 * is kind of like crossing a mount point.
3071 */ 3360 */
3072static int fixup_tree_root_location(struct btrfs_root *root, 3361static int fixup_tree_root_location(struct btrfs_root *root,
3073 struct btrfs_key *location, 3362 struct inode *dir,
3074 struct btrfs_root **sub_root, 3363 struct dentry *dentry,
3075 struct dentry *dentry) 3364 struct btrfs_key *location,
3365 struct btrfs_root **sub_root)
3076{ 3366{
3077 struct btrfs_root_item *ri; 3367 struct btrfs_path *path;
3368 struct btrfs_root *new_root;
3369 struct btrfs_root_ref *ref;
3370 struct extent_buffer *leaf;
3371 int ret;
3372 int err = 0;
3078 3373
3079 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) 3374 path = btrfs_alloc_path();
3080 return 0; 3375 if (!path) {
3081 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) 3376 err = -ENOMEM;
3082 return 0; 3377 goto out;
3378 }
3083 3379
3084 *sub_root = btrfs_read_fs_root(root->fs_info, location, 3380 err = -ENOENT;
3085 dentry->d_name.name, 3381 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3086 dentry->d_name.len); 3382 BTRFS_I(dir)->root->root_key.objectid,
3087 if (IS_ERR(*sub_root)) 3383 location->objectid);
3088 return PTR_ERR(*sub_root); 3384 if (ret) {
3385 if (ret < 0)
3386 err = ret;
3387 goto out;
3388 }
3089 3389
3090 ri = &(*sub_root)->root_item; 3390 leaf = path->nodes[0];
3091 location->objectid = btrfs_root_dirid(ri); 3391 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3092 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); 3392 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3093 location->offset = 0; 3393 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3394 goto out;
3094 3395
3095 return 0; 3396 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3397 (unsigned long)(ref + 1),
3398 dentry->d_name.len);
3399 if (ret)
3400 goto out;
3401
3402 btrfs_release_path(root->fs_info->tree_root, path);
3403
3404 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3405 if (IS_ERR(new_root)) {
3406 err = PTR_ERR(new_root);
3407 goto out;
3408 }
3409
3410 if (btrfs_root_refs(&new_root->root_item) == 0) {
3411 err = -ENOENT;
3412 goto out;
3413 }
3414
3415 *sub_root = new_root;
3416 location->objectid = btrfs_root_dirid(&new_root->root_item);
3417 location->type = BTRFS_INODE_ITEM_KEY;
3418 location->offset = 0;
3419 err = 0;
3420out:
3421 btrfs_free_path(path);
3422 return err;
3096} 3423}
3097 3424
3098static void inode_tree_add(struct inode *inode) 3425static void inode_tree_add(struct inode *inode)
@@ -3101,11 +3428,13 @@ static void inode_tree_add(struct inode *inode)
3101 struct btrfs_inode *entry; 3428 struct btrfs_inode *entry;
3102 struct rb_node **p; 3429 struct rb_node **p;
3103 struct rb_node *parent; 3430 struct rb_node *parent;
3104
3105again: 3431again:
3106 p = &root->inode_tree.rb_node; 3432 p = &root->inode_tree.rb_node;
3107 parent = NULL; 3433 parent = NULL;
3108 3434
3435 if (hlist_unhashed(&inode->i_hash))
3436 return;
3437
3109 spin_lock(&root->inode_lock); 3438 spin_lock(&root->inode_lock);
3110 while (*p) { 3439 while (*p) {
3111 parent = *p; 3440 parent = *p;
@@ -3132,13 +3461,87 @@ again:
3132static void inode_tree_del(struct inode *inode) 3461static void inode_tree_del(struct inode *inode)
3133{ 3462{
3134 struct btrfs_root *root = BTRFS_I(inode)->root; 3463 struct btrfs_root *root = BTRFS_I(inode)->root;
3464 int empty = 0;
3135 3465
3136 spin_lock(&root->inode_lock); 3466 spin_lock(&root->inode_lock);
3137 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) { 3467 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3138 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree); 3468 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3139 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 3469 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3470 empty = RB_EMPTY_ROOT(&root->inode_tree);
3471 }
3472 spin_unlock(&root->inode_lock);
3473
3474 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3475 synchronize_srcu(&root->fs_info->subvol_srcu);
3476 spin_lock(&root->inode_lock);
3477 empty = RB_EMPTY_ROOT(&root->inode_tree);
3478 spin_unlock(&root->inode_lock);
3479 if (empty)
3480 btrfs_add_dead_root(root);
3481 }
3482}
3483
3484int btrfs_invalidate_inodes(struct btrfs_root *root)
3485{
3486 struct rb_node *node;
3487 struct rb_node *prev;
3488 struct btrfs_inode *entry;
3489 struct inode *inode;
3490 u64 objectid = 0;
3491
3492 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3493
3494 spin_lock(&root->inode_lock);
3495again:
3496 node = root->inode_tree.rb_node;
3497 prev = NULL;
3498 while (node) {
3499 prev = node;
3500 entry = rb_entry(node, struct btrfs_inode, rb_node);
3501
3502 if (objectid < entry->vfs_inode.i_ino)
3503 node = node->rb_left;
3504 else if (objectid > entry->vfs_inode.i_ino)
3505 node = node->rb_right;
3506 else
3507 break;
3508 }
3509 if (!node) {
3510 while (prev) {
3511 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3512 if (objectid <= entry->vfs_inode.i_ino) {
3513 node = prev;
3514 break;
3515 }
3516 prev = rb_next(prev);
3517 }
3518 }
3519 while (node) {
3520 entry = rb_entry(node, struct btrfs_inode, rb_node);
3521 objectid = entry->vfs_inode.i_ino + 1;
3522 inode = igrab(&entry->vfs_inode);
3523 if (inode) {
3524 spin_unlock(&root->inode_lock);
3525 if (atomic_read(&inode->i_count) > 1)
3526 d_prune_aliases(inode);
3527 /*
3528 * btrfs_drop_inode will remove it from
3529 * the inode cache when its usage count
3530 * hits zero.
3531 */
3532 iput(inode);
3533 cond_resched();
3534 spin_lock(&root->inode_lock);
3535 goto again;
3536 }
3537
3538 if (cond_resched_lock(&root->inode_lock))
3539 goto again;
3540
3541 node = rb_next(node);
3140 } 3542 }
3141 spin_unlock(&root->inode_lock); 3543 spin_unlock(&root->inode_lock);
3544 return 0;
3142} 3545}
3143 3546
3144static noinline void init_btrfs_i(struct inode *inode) 3547static noinline void init_btrfs_i(struct inode *inode)
@@ -3148,6 +3551,7 @@ static noinline void init_btrfs_i(struct inode *inode)
3148 bi->generation = 0; 3551 bi->generation = 0;
3149 bi->sequence = 0; 3552 bi->sequence = 0;
3150 bi->last_trans = 0; 3553 bi->last_trans = 0;
3554 bi->last_sub_trans = 0;
3151 bi->logged_trans = 0; 3555 bi->logged_trans = 0;
3152 bi->delalloc_bytes = 0; 3556 bi->delalloc_bytes = 0;
3153 bi->reserved_bytes = 0; 3557 bi->reserved_bytes = 0;
@@ -3225,15 +3629,41 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3225 return inode; 3629 return inode;
3226} 3630}
3227 3631
3632static struct inode *new_simple_dir(struct super_block *s,
3633 struct btrfs_key *key,
3634 struct btrfs_root *root)
3635{
3636 struct inode *inode = new_inode(s);
3637
3638 if (!inode)
3639 return ERR_PTR(-ENOMEM);
3640
3641 init_btrfs_i(inode);
3642
3643 BTRFS_I(inode)->root = root;
3644 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3645 BTRFS_I(inode)->dummy_inode = 1;
3646
3647 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3648 inode->i_op = &simple_dir_inode_operations;
3649 inode->i_fop = &simple_dir_operations;
3650 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3651 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3652
3653 return inode;
3654}
3655
3228struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) 3656struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3229{ 3657{
3230 struct inode *inode; 3658 struct inode *inode;
3231 struct btrfs_inode *bi = BTRFS_I(dir); 3659 struct btrfs_root *root = BTRFS_I(dir)->root;
3232 struct btrfs_root *root = bi->root;
3233 struct btrfs_root *sub_root = root; 3660 struct btrfs_root *sub_root = root;
3234 struct btrfs_key location; 3661 struct btrfs_key location;
3662 int index;
3235 int ret; 3663 int ret;
3236 3664
3665 dentry->d_op = &btrfs_dentry_operations;
3666
3237 if (dentry->d_name.len > BTRFS_NAME_LEN) 3667 if (dentry->d_name.len > BTRFS_NAME_LEN)
3238 return ERR_PTR(-ENAMETOOLONG); 3668 return ERR_PTR(-ENAMETOOLONG);
3239 3669
@@ -3242,29 +3672,52 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3242 if (ret < 0) 3672 if (ret < 0)
3243 return ERR_PTR(ret); 3673 return ERR_PTR(ret);
3244 3674
3245 inode = NULL; 3675 if (location.objectid == 0)
3246 if (location.objectid) { 3676 return NULL;
3247 ret = fixup_tree_root_location(root, &location, &sub_root, 3677
3248 dentry); 3678 if (location.type == BTRFS_INODE_ITEM_KEY) {
3249 if (ret < 0) 3679 inode = btrfs_iget(dir->i_sb, &location, root);
3250 return ERR_PTR(ret); 3680 return inode;
3251 if (ret > 0) 3681 }
3252 return ERR_PTR(-ENOENT); 3682
3683 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3684
3685 index = srcu_read_lock(&root->fs_info->subvol_srcu);
3686 ret = fixup_tree_root_location(root, dir, dentry,
3687 &location, &sub_root);
3688 if (ret < 0) {
3689 if (ret != -ENOENT)
3690 inode = ERR_PTR(ret);
3691 else
3692 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3693 } else {
3253 inode = btrfs_iget(dir->i_sb, &location, sub_root); 3694 inode = btrfs_iget(dir->i_sb, &location, sub_root);
3254 if (IS_ERR(inode))
3255 return ERR_CAST(inode);
3256 } 3695 }
3696 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3697
3257 return inode; 3698 return inode;
3258} 3699}
3259 3700
3701static int btrfs_dentry_delete(struct dentry *dentry)
3702{
3703 struct btrfs_root *root;
3704
3705 if (!dentry->d_inode && !IS_ROOT(dentry))
3706 dentry = dentry->d_parent;
3707
3708 if (dentry->d_inode) {
3709 root = BTRFS_I(dentry->d_inode)->root;
3710 if (btrfs_root_refs(&root->root_item) == 0)
3711 return 1;
3712 }
3713 return 0;
3714}
3715
3260static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, 3716static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3261 struct nameidata *nd) 3717 struct nameidata *nd)
3262{ 3718{
3263 struct inode *inode; 3719 struct inode *inode;
3264 3720
3265 if (dentry->d_name.len > BTRFS_NAME_LEN)
3266 return ERR_PTR(-ENAMETOOLONG);
3267
3268 inode = btrfs_lookup_dentry(dir, dentry); 3721 inode = btrfs_lookup_dentry(dir, dentry);
3269 if (IS_ERR(inode)) 3722 if (IS_ERR(inode))
3270 return ERR_CAST(inode); 3723 return ERR_CAST(inode);
@@ -3603,9 +4056,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3603 if (ret != 0) 4056 if (ret != 0)
3604 goto fail; 4057 goto fail;
3605 4058
3606 if (objectid > root->highest_inode)
3607 root->highest_inode = objectid;
3608
3609 inode->i_uid = current_fsuid(); 4059 inode->i_uid = current_fsuid();
3610 4060
3611 if (dir && (dir->i_mode & S_ISGID)) { 4061 if (dir && (dir->i_mode & S_ISGID)) {
@@ -3673,26 +4123,35 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
3673 struct inode *parent_inode, struct inode *inode, 4123 struct inode *parent_inode, struct inode *inode,
3674 const char *name, int name_len, int add_backref, u64 index) 4124 const char *name, int name_len, int add_backref, u64 index)
3675{ 4125{
3676 int ret; 4126 int ret = 0;
3677 struct btrfs_key key; 4127 struct btrfs_key key;
3678 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 4128 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
3679 4129
3680 key.objectid = inode->i_ino; 4130 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
3681 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 4131 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
3682 key.offset = 0; 4132 } else {
4133 key.objectid = inode->i_ino;
4134 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4135 key.offset = 0;
4136 }
4137
4138 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4139 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4140 key.objectid, root->root_key.objectid,
4141 parent_inode->i_ino,
4142 index, name, name_len);
4143 } else if (add_backref) {
4144 ret = btrfs_insert_inode_ref(trans, root,
4145 name, name_len, inode->i_ino,
4146 parent_inode->i_ino, index);
4147 }
3683 4148
3684 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3685 parent_inode->i_ino,
3686 &key, btrfs_inode_type(inode),
3687 index);
3688 if (ret == 0) { 4149 if (ret == 0) {
3689 if (add_backref) { 4150 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3690 ret = btrfs_insert_inode_ref(trans, root, 4151 parent_inode->i_ino, &key,
3691 name, name_len, 4152 btrfs_inode_type(inode), index);
3692 inode->i_ino, 4153 BUG_ON(ret);
3693 parent_inode->i_ino, 4154
3694 index);
3695 }
3696 btrfs_i_size_write(parent_inode, parent_inode->i_size + 4155 btrfs_i_size_write(parent_inode, parent_inode->i_size +
3697 name_len * 2); 4156 name_len * 2);
3698 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; 4157 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
@@ -3732,11 +4191,18 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3732 if (!new_valid_dev(rdev)) 4191 if (!new_valid_dev(rdev))
3733 return -EINVAL; 4192 return -EINVAL;
3734 4193
3735 err = btrfs_check_metadata_free_space(root); 4194 /*
4195 * 2 for inode item and ref
4196 * 2 for dir items
4197 * 1 for xattr if selinux is on
4198 */
4199 err = btrfs_reserve_metadata_space(root, 5);
3736 if (err) 4200 if (err)
3737 goto fail; 4201 return err;
3738 4202
3739 trans = btrfs_start_transaction(root, 1); 4203 trans = btrfs_start_transaction(root, 1);
4204 if (!trans)
4205 goto fail;
3740 btrfs_set_trans_block_group(trans, dir); 4206 btrfs_set_trans_block_group(trans, dir);
3741 4207
3742 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4208 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -3774,6 +4240,7 @@ out_unlock:
3774 nr = trans->blocks_used; 4240 nr = trans->blocks_used;
3775 btrfs_end_transaction_throttle(trans, root); 4241 btrfs_end_transaction_throttle(trans, root);
3776fail: 4242fail:
4243 btrfs_unreserve_metadata_space(root, 5);
3777 if (drop_inode) { 4244 if (drop_inode) {
3778 inode_dec_link_count(inode); 4245 inode_dec_link_count(inode);
3779 iput(inode); 4246 iput(inode);
@@ -3794,10 +4261,18 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
3794 u64 objectid; 4261 u64 objectid;
3795 u64 index = 0; 4262 u64 index = 0;
3796 4263
3797 err = btrfs_check_metadata_free_space(root); 4264 /*
4265 * 2 for inode item and ref
4266 * 2 for dir items
4267 * 1 for xattr if selinux is on
4268 */
4269 err = btrfs_reserve_metadata_space(root, 5);
3798 if (err) 4270 if (err)
3799 goto fail; 4271 return err;
4272
3800 trans = btrfs_start_transaction(root, 1); 4273 trans = btrfs_start_transaction(root, 1);
4274 if (!trans)
4275 goto fail;
3801 btrfs_set_trans_block_group(trans, dir); 4276 btrfs_set_trans_block_group(trans, dir);
3802 4277
3803 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4278 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -3838,6 +4313,7 @@ out_unlock:
3838 nr = trans->blocks_used; 4313 nr = trans->blocks_used;
3839 btrfs_end_transaction_throttle(trans, root); 4314 btrfs_end_transaction_throttle(trans, root);
3840fail: 4315fail:
4316 btrfs_unreserve_metadata_space(root, 5);
3841 if (drop_inode) { 4317 if (drop_inode) {
3842 inode_dec_link_count(inode); 4318 inode_dec_link_count(inode);
3843 iput(inode); 4319 iput(inode);
@@ -3860,10 +4336,16 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3860 if (inode->i_nlink == 0) 4336 if (inode->i_nlink == 0)
3861 return -ENOENT; 4337 return -ENOENT;
3862 4338
3863 btrfs_inc_nlink(inode); 4339 /*
3864 err = btrfs_check_metadata_free_space(root); 4340 * 1 item for inode ref
4341 * 2 items for dir items
4342 */
4343 err = btrfs_reserve_metadata_space(root, 3);
3865 if (err) 4344 if (err)
3866 goto fail; 4345 return err;
4346
4347 btrfs_inc_nlink(inode);
4348
3867 err = btrfs_set_inode_index(dir, &index); 4349 err = btrfs_set_inode_index(dir, &index);
3868 if (err) 4350 if (err)
3869 goto fail; 4351 goto fail;
@@ -3875,20 +4357,19 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3875 4357
3876 err = btrfs_add_nondir(trans, dentry, inode, 1, index); 4358 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
3877 4359
3878 if (err) 4360 if (err) {
3879 drop_inode = 1;
3880
3881 btrfs_update_inode_block_group(trans, dir);
3882 err = btrfs_update_inode(trans, root, inode);
3883
3884 if (err)
3885 drop_inode = 1; 4361 drop_inode = 1;
4362 } else {
4363 btrfs_update_inode_block_group(trans, dir);
4364 err = btrfs_update_inode(trans, root, inode);
4365 BUG_ON(err);
4366 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4367 }
3886 4368
3887 nr = trans->blocks_used; 4369 nr = trans->blocks_used;
3888
3889 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
3890 btrfs_end_transaction_throttle(trans, root); 4370 btrfs_end_transaction_throttle(trans, root);
3891fail: 4371fail:
4372 btrfs_unreserve_metadata_space(root, 3);
3892 if (drop_inode) { 4373 if (drop_inode) {
3893 inode_dec_link_count(inode); 4374 inode_dec_link_count(inode);
3894 iput(inode); 4375 iput(inode);
@@ -3908,17 +4389,21 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3908 u64 index = 0; 4389 u64 index = 0;
3909 unsigned long nr = 1; 4390 unsigned long nr = 1;
3910 4391
3911 err = btrfs_check_metadata_free_space(root); 4392 /*
4393 * 2 items for inode and ref
4394 * 2 items for dir items
4395 * 1 for xattr if selinux is on
4396 */
4397 err = btrfs_reserve_metadata_space(root, 5);
3912 if (err) 4398 if (err)
3913 goto out_unlock; 4399 return err;
3914 4400
3915 trans = btrfs_start_transaction(root, 1); 4401 trans = btrfs_start_transaction(root, 1);
3916 btrfs_set_trans_block_group(trans, dir); 4402 if (!trans) {
3917 4403 err = -ENOMEM;
3918 if (IS_ERR(trans)) {
3919 err = PTR_ERR(trans);
3920 goto out_unlock; 4404 goto out_unlock;
3921 } 4405 }
4406 btrfs_set_trans_block_group(trans, dir);
3922 4407
3923 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4408 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3924 if (err) { 4409 if (err) {
@@ -3967,6 +4452,7 @@ out_fail:
3967 btrfs_end_transaction_throttle(trans, root); 4452 btrfs_end_transaction_throttle(trans, root);
3968 4453
3969out_unlock: 4454out_unlock:
4455 btrfs_unreserve_metadata_space(root, 5);
3970 if (drop_on_err) 4456 if (drop_on_err)
3971 iput(inode); 4457 iput(inode);
3972 btrfs_btree_balance_dirty(root, nr); 4458 btrfs_btree_balance_dirty(root, nr);
@@ -4064,11 +4550,11 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4064 int compressed; 4550 int compressed;
4065 4551
4066again: 4552again:
4067 spin_lock(&em_tree->lock); 4553 read_lock(&em_tree->lock);
4068 em = lookup_extent_mapping(em_tree, start, len); 4554 em = lookup_extent_mapping(em_tree, start, len);
4069 if (em) 4555 if (em)
4070 em->bdev = root->fs_info->fs_devices->latest_bdev; 4556 em->bdev = root->fs_info->fs_devices->latest_bdev;
4071 spin_unlock(&em_tree->lock); 4557 read_unlock(&em_tree->lock);
4072 4558
4073 if (em) { 4559 if (em) {
4074 if (em->start > start || em->start + em->len <= start) 4560 if (em->start > start || em->start + em->len <= start)
@@ -4215,6 +4701,11 @@ again:
4215 map = kmap(page); 4701 map = kmap(page);
4216 read_extent_buffer(leaf, map + pg_offset, ptr, 4702 read_extent_buffer(leaf, map + pg_offset, ptr,
4217 copy_size); 4703 copy_size);
4704 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4705 memset(map + pg_offset + copy_size, 0,
4706 PAGE_CACHE_SIZE - pg_offset -
4707 copy_size);
4708 }
4218 kunmap(page); 4709 kunmap(page);
4219 } 4710 }
4220 flush_dcache_page(page); 4711 flush_dcache_page(page);
@@ -4259,7 +4750,7 @@ insert:
4259 } 4750 }
4260 4751
4261 err = 0; 4752 err = 0;
4262 spin_lock(&em_tree->lock); 4753 write_lock(&em_tree->lock);
4263 ret = add_extent_mapping(em_tree, em); 4754 ret = add_extent_mapping(em_tree, em);
4264 /* it is possible that someone inserted the extent into the tree 4755 /* it is possible that someone inserted the extent into the tree
4265 * while we had the lock dropped. It is also possible that 4756 * while we had the lock dropped. It is also possible that
@@ -4299,7 +4790,7 @@ insert:
4299 err = 0; 4790 err = 0;
4300 } 4791 }
4301 } 4792 }
4302 spin_unlock(&em_tree->lock); 4793 write_unlock(&em_tree->lock);
4303out: 4794out:
4304 if (path) 4795 if (path)
4305 btrfs_free_path(path); 4796 btrfs_free_path(path);
@@ -4398,13 +4889,21 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4398 u64 page_start = page_offset(page); 4889 u64 page_start = page_offset(page);
4399 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 4890 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
4400 4891
4892
4893 /*
4894 * we have the page locked, so new writeback can't start,
4895 * and the dirty bit won't be cleared while we are here.
4896 *
4897 * Wait for IO on this page so that we can safely clear
4898 * the PagePrivate2 bit and do ordered accounting
4899 */
4401 wait_on_page_writeback(page); 4900 wait_on_page_writeback(page);
4901
4402 tree = &BTRFS_I(page->mapping->host)->io_tree; 4902 tree = &BTRFS_I(page->mapping->host)->io_tree;
4403 if (offset) { 4903 if (offset) {
4404 btrfs_releasepage(page, GFP_NOFS); 4904 btrfs_releasepage(page, GFP_NOFS);
4405 return; 4905 return;
4406 } 4906 }
4407
4408 lock_extent(tree, page_start, page_end, GFP_NOFS); 4907 lock_extent(tree, page_start, page_end, GFP_NOFS);
4409 ordered = btrfs_lookup_ordered_extent(page->mapping->host, 4908 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4410 page_offset(page)); 4909 page_offset(page));
@@ -4415,16 +4914,22 @@ static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4415 */ 4914 */
4416 clear_extent_bit(tree, page_start, page_end, 4915 clear_extent_bit(tree, page_start, page_end,
4417 EXTENT_DIRTY | EXTENT_DELALLOC | 4916 EXTENT_DIRTY | EXTENT_DELALLOC |
4418 EXTENT_LOCKED, 1, 0, GFP_NOFS); 4917 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
4419 btrfs_finish_ordered_io(page->mapping->host, 4918 NULL, GFP_NOFS);
4420 page_start, page_end); 4919 /*
4920 * whoever cleared the private bit is responsible
4921 * for the finish_ordered_io
4922 */
4923 if (TestClearPagePrivate2(page)) {
4924 btrfs_finish_ordered_io(page->mapping->host,
4925 page_start, page_end);
4926 }
4421 btrfs_put_ordered_extent(ordered); 4927 btrfs_put_ordered_extent(ordered);
4422 lock_extent(tree, page_start, page_end, GFP_NOFS); 4928 lock_extent(tree, page_start, page_end, GFP_NOFS);
4423 } 4929 }
4424 clear_extent_bit(tree, page_start, page_end, 4930 clear_extent_bit(tree, page_start, page_end,
4425 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | 4931 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4426 EXTENT_ORDERED, 4932 EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
4427 1, 1, GFP_NOFS);
4428 __btrfs_releasepage(page, GFP_NOFS); 4933 __btrfs_releasepage(page, GFP_NOFS);
4429 4934
4430 ClearPageChecked(page); 4935 ClearPageChecked(page);
@@ -4473,6 +4978,13 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
4473 goto out; 4978 goto out;
4474 } 4979 }
4475 4980
4981 ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
4982 if (ret) {
4983 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
4984 ret = VM_FAULT_SIGBUS;
4985 goto out;
4986 }
4987
4476 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */ 4988 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
4477again: 4989again:
4478 lock_page(page); 4990 lock_page(page);
@@ -4504,7 +5016,24 @@ again:
4504 goto again; 5016 goto again;
4505 } 5017 }
4506 5018
4507 btrfs_set_extent_delalloc(inode, page_start, page_end); 5019 /*
5020 * XXX - page_mkwrite gets called every time the page is dirtied, even
5021 * if it was already dirty, so for space accounting reasons we need to
5022 * clear any delalloc bits for the range we are fixing to save. There
5023 * is probably a better way to do this, but for now keep consistent with
5024 * prepare_pages in the normal write path.
5025 */
5026 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
5027 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5028 GFP_NOFS);
5029
5030 ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5031 if (ret) {
5032 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5033 ret = VM_FAULT_SIGBUS;
5034 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5035 goto out_unlock;
5036 }
4508 ret = 0; 5037 ret = 0;
4509 5038
4510 /* page is wholly or partially inside EOF */ 5039 /* page is wholly or partially inside EOF */
@@ -4521,11 +5050,17 @@ again:
4521 } 5050 }
4522 ClearPageChecked(page); 5051 ClearPageChecked(page);
4523 set_page_dirty(page); 5052 set_page_dirty(page);
5053 SetPageUptodate(page);
5054
5055 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5056 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
4524 5057
4525 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
4526 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 5058 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4527 5059
4528out_unlock: 5060out_unlock:
5061 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
5062 if (!ret)
5063 return VM_FAULT_LOCKED;
4529 unlock_page(page); 5064 unlock_page(page);
4530out: 5065out:
4531 return ret; 5066 return ret;
@@ -4544,7 +5079,9 @@ static void btrfs_truncate(struct inode *inode)
4544 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 5079 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4545 return; 5080 return;
4546 5081
4547 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;
4548 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 5085 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
4549 5086
4550 trans = btrfs_start_transaction(root, 1); 5087 trans = btrfs_start_transaction(root, 1);
@@ -4594,11 +5131,11 @@ out:
4594 * create a new subvolume directory/inode (helper for the ioctl). 5131 * create a new subvolume directory/inode (helper for the ioctl).
4595 */ 5132 */
4596int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 5133int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4597 struct btrfs_root *new_root, struct dentry *dentry, 5134 struct btrfs_root *new_root,
4598 u64 new_dirid, u64 alloc_hint) 5135 u64 new_dirid, u64 alloc_hint)
4599{ 5136{
4600 struct inode *inode; 5137 struct inode *inode;
4601 int error; 5138 int err;
4602 u64 index = 0; 5139 u64 index = 0;
4603 5140
4604 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, 5141 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
@@ -4611,11 +5148,10 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4611 inode->i_nlink = 1; 5148 inode->i_nlink = 1;
4612 btrfs_i_size_write(inode, 0); 5149 btrfs_i_size_write(inode, 0);
4613 5150
4614 error = btrfs_update_inode(trans, new_root, inode); 5151 err = btrfs_update_inode(trans, new_root, inode);
4615 if (error) 5152 BUG_ON(err);
4616 return error;
4617 5153
4618 d_instantiate(dentry, inode); 5154 iput(inode);
4619 return 0; 5155 return 0;
4620} 5156}
4621 5157
@@ -4640,7 +5176,12 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
4640 if (!ei) 5176 if (!ei)
4641 return NULL; 5177 return NULL;
4642 ei->last_trans = 0; 5178 ei->last_trans = 0;
5179 ei->last_sub_trans = 0;
4643 ei->logged_trans = 0; 5180 ei->logged_trans = 0;
5181 ei->outstanding_extents = 0;
5182 ei->reserved_extents = 0;
5183 ei->root = NULL;
5184 spin_lock_init(&ei->accounting_lock);
4644 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 5185 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
4645 INIT_LIST_HEAD(&ei->i_orphan); 5186 INIT_LIST_HEAD(&ei->i_orphan);
4646 INIT_LIST_HEAD(&ei->ordered_operations); 5187 INIT_LIST_HEAD(&ei->ordered_operations);
@@ -4656,6 +5197,14 @@ void btrfs_destroy_inode(struct inode *inode)
4656 WARN_ON(inode->i_data.nrpages); 5197 WARN_ON(inode->i_data.nrpages);
4657 5198
4658 /* 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 /*
4659 * Make sure we're properly removed from the ordered operation 5208 * Make sure we're properly removed from the ordered operation
4660 * lists. 5209 * lists.
4661 */ 5210 */
@@ -4690,9 +5239,20 @@ void btrfs_destroy_inode(struct inode *inode)
4690 } 5239 }
4691 inode_tree_del(inode); 5240 inode_tree_del(inode);
4692 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 5241 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
5242free:
4693 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); 5243 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4694} 5244}
4695 5245
5246void btrfs_drop_inode(struct inode *inode)
5247{
5248 struct btrfs_root *root = BTRFS_I(inode)->root;
5249
5250 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5251 generic_delete_inode(inode);
5252 else
5253 generic_drop_inode(inode);
5254}
5255
4696static void init_once(void *foo) 5256static void init_once(void *foo)
4697{ 5257{
4698 struct btrfs_inode *ei = (struct btrfs_inode *) foo; 5258 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
@@ -4761,31 +5321,40 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4761{ 5321{
4762 struct btrfs_trans_handle *trans; 5322 struct btrfs_trans_handle *trans;
4763 struct btrfs_root *root = BTRFS_I(old_dir)->root; 5323 struct btrfs_root *root = BTRFS_I(old_dir)->root;
5324 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
4764 struct inode *new_inode = new_dentry->d_inode; 5325 struct inode *new_inode = new_dentry->d_inode;
4765 struct inode *old_inode = old_dentry->d_inode; 5326 struct inode *old_inode = old_dentry->d_inode;
4766 struct timespec ctime = CURRENT_TIME; 5327 struct timespec ctime = CURRENT_TIME;
4767 u64 index = 0; 5328 u64 index = 0;
5329 u64 root_objectid;
4768 int ret; 5330 int ret;
4769 5331
4770 /* we're not allowed to rename between subvolumes */ 5332 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4771 if (BTRFS_I(old_inode)->root->root_key.objectid != 5333 return -EPERM;
4772 BTRFS_I(new_dir)->root->root_key.objectid) 5334
5335 /* we only allow rename subvolume link between subvolumes */
5336 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
4773 return -EXDEV; 5337 return -EXDEV;
4774 5338
5339 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5340 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5341 return -ENOTEMPTY;
5342
4775 if (S_ISDIR(old_inode->i_mode) && new_inode && 5343 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4776 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { 5344 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4777 return -ENOTEMPTY; 5345 return -ENOTEMPTY;
4778 }
4779 5346
4780 /* to rename a snapshot or subvolume, we need to juggle the 5347 /*
4781 * backrefs. This isn't coded yet 5348 * We want to reserve the absolute worst case amount of items. So if
5349 * both inodes are subvols and we need to unlink them then that would
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.
4782 */ 5354 */
4783 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5355 ret = btrfs_reserve_metadata_space(root, 11);
4784 return -EXDEV;
4785
4786 ret = btrfs_check_metadata_free_space(root);
4787 if (ret) 5356 if (ret)
4788 goto out_unlock; 5357 return ret;
4789 5358
4790 /* 5359 /*
4791 * we're using rename to replace one file with another. 5360 * we're using rename to replace one file with another.
@@ -4796,8 +5365,40 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4796 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) 5365 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
4797 filemap_flush(old_inode->i_mapping); 5366 filemap_flush(old_inode->i_mapping);
4798 5367
5368 /* close the racy window with snapshot create/destroy ioctl */
5369 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5370 down_read(&root->fs_info->subvol_sem);
5371
4799 trans = btrfs_start_transaction(root, 1); 5372 trans = btrfs_start_transaction(root, 1);
5373 btrfs_set_trans_block_group(trans, new_dir);
5374
5375 if (dest != root)
5376 btrfs_record_root_in_trans(trans, dest);
4800 5377
5378 ret = btrfs_set_inode_index(new_dir, &index);
5379 if (ret)
5380 goto out_fail;
5381
5382 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5383 /* force full log commit if subvolume involved. */
5384 root->fs_info->last_trans_log_full_commit = trans->transid;
5385 } else {
5386 ret = btrfs_insert_inode_ref(trans, dest,
5387 new_dentry->d_name.name,
5388 new_dentry->d_name.len,
5389 old_inode->i_ino,
5390 new_dir->i_ino, index);
5391 if (ret)
5392 goto out_fail;
5393 /*
5394 * this is an ugly little race, but the rename is required
5395 * to make sure that if we crash, the inode is either at the
5396 * old name or the new one. pinning the log transaction lets
5397 * us make sure we don't allow a log commit to come in after
5398 * we unlink the name but before we add the new name back in.
5399 */
5400 btrfs_pin_log_trans(root);
5401 }
4801 /* 5402 /*
4802 * make sure the inode gets flushed if it is replacing 5403 * make sure the inode gets flushed if it is replacing
4803 * something. 5404 * something.
@@ -4807,18 +5408,6 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4807 btrfs_add_ordered_operation(trans, root, old_inode); 5408 btrfs_add_ordered_operation(trans, root, old_inode);
4808 } 5409 }
4809 5410
4810 /*
4811 * this is an ugly little race, but the rename is required to make
4812 * sure that if we crash, the inode is either at the old name
4813 * or the new one. pinning the log transaction lets us make sure
4814 * we don't allow a log commit to come in after we unlink the
4815 * name but before we add the new name back in.
4816 */
4817 btrfs_pin_log_trans(root);
4818
4819 btrfs_set_trans_block_group(trans, new_dir);
4820
4821 btrfs_inc_nlink(old_dentry->d_inode);
4822 old_dir->i_ctime = old_dir->i_mtime = ctime; 5411 old_dir->i_ctime = old_dir->i_mtime = ctime;
4823 new_dir->i_ctime = new_dir->i_mtime = ctime; 5412 new_dir->i_ctime = new_dir->i_mtime = ctime;
4824 old_inode->i_ctime = ctime; 5413 old_inode->i_ctime = ctime;
@@ -4826,47 +5415,60 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4826 if (old_dentry->d_parent != new_dentry->d_parent) 5415 if (old_dentry->d_parent != new_dentry->d_parent)
4827 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); 5416 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
4828 5417
4829 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode, 5418 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4830 old_dentry->d_name.name, 5419 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
4831 old_dentry->d_name.len); 5420 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
4832 if (ret) 5421 old_dentry->d_name.name,
4833 goto out_fail; 5422 old_dentry->d_name.len);
5423 } else {
5424 btrfs_inc_nlink(old_dentry->d_inode);
5425 ret = btrfs_unlink_inode(trans, root, old_dir,
5426 old_dentry->d_inode,
5427 old_dentry->d_name.name,
5428 old_dentry->d_name.len);
5429 }
5430 BUG_ON(ret);
4834 5431
4835 if (new_inode) { 5432 if (new_inode) {
4836 new_inode->i_ctime = CURRENT_TIME; 5433 new_inode->i_ctime = CURRENT_TIME;
4837 ret = btrfs_unlink_inode(trans, root, new_dir, 5434 if (unlikely(new_inode->i_ino ==
4838 new_dentry->d_inode, 5435 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4839 new_dentry->d_name.name, 5436 root_objectid = BTRFS_I(new_inode)->location.objectid;
4840 new_dentry->d_name.len); 5437 ret = btrfs_unlink_subvol(trans, dest, new_dir,
4841 if (ret) 5438 root_objectid,
4842 goto out_fail; 5439 new_dentry->d_name.name,
5440 new_dentry->d_name.len);
5441 BUG_ON(new_inode->i_nlink == 0);
5442 } else {
5443 ret = btrfs_unlink_inode(trans, dest, new_dir,
5444 new_dentry->d_inode,
5445 new_dentry->d_name.name,
5446 new_dentry->d_name.len);
5447 }
5448 BUG_ON(ret);
4843 if (new_inode->i_nlink == 0) { 5449 if (new_inode->i_nlink == 0) {
4844 ret = btrfs_orphan_add(trans, new_dentry->d_inode); 5450 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4845 if (ret) 5451 BUG_ON(ret);
4846 goto out_fail;
4847 } 5452 }
4848
4849 } 5453 }
4850 ret = btrfs_set_inode_index(new_dir, &index);
4851 if (ret)
4852 goto out_fail;
4853 5454
4854 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode, 5455 ret = btrfs_add_link(trans, new_dir, old_inode,
4855 old_inode, new_dentry->d_name.name, 5456 new_dentry->d_name.name,
4856 new_dentry->d_name.len, 1, index); 5457 new_dentry->d_name.len, 0, index);
4857 if (ret) 5458 BUG_ON(ret);
4858 goto out_fail;
4859 5459
4860 btrfs_log_new_name(trans, old_inode, old_dir, 5460 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
4861 new_dentry->d_parent); 5461 btrfs_log_new_name(trans, old_inode, old_dir,
5462 new_dentry->d_parent);
5463 btrfs_end_log_trans(root);
5464 }
4862out_fail: 5465out_fail:
4863
4864 /* this btrfs_end_log_trans just allows the current
4865 * log-sub transaction to complete
4866 */
4867 btrfs_end_log_trans(root);
4868 btrfs_end_transaction_throttle(trans, root); 5466 btrfs_end_transaction_throttle(trans, root);
4869out_unlock: 5467
5468 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5469 up_read(&root->fs_info->subvol_sem);
5470
5471 btrfs_unreserve_metadata_space(root, 11);
4870 return ret; 5472 return ret;
4871} 5473}
4872 5474
@@ -4938,11 +5540,18 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4938 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 5540 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4939 return -ENAMETOOLONG; 5541 return -ENAMETOOLONG;
4940 5542
4941 err = btrfs_check_metadata_free_space(root); 5543 /*
5544 * 2 items for inode item and ref
5545 * 2 items for dir items
5546 * 1 item for xattr if selinux is on
5547 */
5548 err = btrfs_reserve_metadata_space(root, 5);
4942 if (err) 5549 if (err)
4943 goto out_fail; 5550 return err;
4944 5551
4945 trans = btrfs_start_transaction(root, 1); 5552 trans = btrfs_start_transaction(root, 1);
5553 if (!trans)
5554 goto out_fail;
4946 btrfs_set_trans_block_group(trans, dir); 5555 btrfs_set_trans_block_group(trans, dir);
4947 5556
4948 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 5557 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
@@ -5023,6 +5632,7 @@ out_unlock:
5023 nr = trans->blocks_used; 5632 nr = trans->blocks_used;
5024 btrfs_end_transaction_throttle(trans, root); 5633 btrfs_end_transaction_throttle(trans, root);
5025out_fail: 5634out_fail:
5635 btrfs_unreserve_metadata_space(root, 5);
5026 if (drop_inode) { 5636 if (drop_inode) {
5027 inode_dec_link_count(inode); 5637 inode_dec_link_count(inode);
5028 iput(inode); 5638 iput(inode);
@@ -5044,6 +5654,11 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5044 5654
5045 while (num_bytes > 0) { 5655 while (num_bytes > 0) {
5046 alloc_size = min(num_bytes, root->fs_info->max_extent); 5656 alloc_size = min(num_bytes, root->fs_info->max_extent);
5657
5658 ret = btrfs_reserve_metadata_space(root, 1);
5659 if (ret)
5660 goto out;
5661
5047 ret = btrfs_reserve_extent(trans, root, alloc_size, 5662 ret = btrfs_reserve_extent(trans, root, alloc_size,
5048 root->sectorsize, 0, alloc_hint, 5663 root->sectorsize, 0, alloc_hint,
5049 (u64)-1, &ins, 1); 5664 (u64)-1, &ins, 1);
@@ -5058,9 +5673,12 @@ static int prealloc_file_range(struct btrfs_trans_handle *trans,
5058 0, 0, 0, 5673 0, 0, 0,
5059 BTRFS_FILE_EXTENT_PREALLOC); 5674 BTRFS_FILE_EXTENT_PREALLOC);
5060 BUG_ON(ret); 5675 BUG_ON(ret);
5676 btrfs_drop_extent_cache(inode, cur_offset,
5677 cur_offset + ins.offset -1, 0);
5061 num_bytes -= ins.offset; 5678 num_bytes -= ins.offset;
5062 cur_offset += ins.offset; 5679 cur_offset += ins.offset;
5063 alloc_hint = ins.objectid + ins.offset; 5680 alloc_hint = ins.objectid + ins.offset;
5681 btrfs_unreserve_metadata_space(root, 1);
5064 } 5682 }
5065out: 5683out:
5066 if (cur_offset > start) { 5684 if (cur_offset > start) {
@@ -5201,7 +5819,7 @@ static int btrfs_permission(struct inode *inode, int mask)
5201 return generic_permission(inode, mask, btrfs_check_acl); 5819 return generic_permission(inode, mask, btrfs_check_acl);
5202} 5820}
5203 5821
5204static struct inode_operations btrfs_dir_inode_operations = { 5822static const struct inode_operations btrfs_dir_inode_operations = {
5205 .getattr = btrfs_getattr, 5823 .getattr = btrfs_getattr,
5206 .lookup = btrfs_lookup, 5824 .lookup = btrfs_lookup,
5207 .create = btrfs_create, 5825 .create = btrfs_create,
@@ -5219,11 +5837,12 @@ static struct inode_operations btrfs_dir_inode_operations = {
5219 .removexattr = btrfs_removexattr, 5837 .removexattr = btrfs_removexattr,
5220 .permission = btrfs_permission, 5838 .permission = btrfs_permission,
5221}; 5839};
5222static struct inode_operations btrfs_dir_ro_inode_operations = { 5840static const struct inode_operations btrfs_dir_ro_inode_operations = {
5223 .lookup = btrfs_lookup, 5841 .lookup = btrfs_lookup,
5224 .permission = btrfs_permission, 5842 .permission = btrfs_permission,
5225}; 5843};
5226static struct file_operations btrfs_dir_file_operations = { 5844
5845static const struct file_operations btrfs_dir_file_operations = {
5227 .llseek = generic_file_llseek, 5846 .llseek = generic_file_llseek,
5228 .read = generic_read_dir, 5847 .read = generic_read_dir,
5229 .readdir = btrfs_real_readdir, 5848 .readdir = btrfs_real_readdir,
@@ -5245,6 +5864,8 @@ static struct extent_io_ops btrfs_extent_io_ops = {
5245 .readpage_io_failed_hook = btrfs_io_failed_hook, 5864 .readpage_io_failed_hook = btrfs_io_failed_hook,
5246 .set_bit_hook = btrfs_set_bit_hook, 5865 .set_bit_hook = btrfs_set_bit_hook,
5247 .clear_bit_hook = btrfs_clear_bit_hook, 5866 .clear_bit_hook = btrfs_clear_bit_hook,
5867 .merge_extent_hook = btrfs_merge_extent_hook,
5868 .split_extent_hook = btrfs_split_extent_hook,
5248}; 5869};
5249 5870
5250/* 5871/*
@@ -5259,7 +5880,7 @@ static struct extent_io_ops btrfs_extent_io_ops = {
5259 * 5880 *
5260 * For now we're avoiding this by dropping bmap. 5881 * For now we're avoiding this by dropping bmap.
5261 */ 5882 */
5262static struct address_space_operations btrfs_aops = { 5883static const struct address_space_operations btrfs_aops = {
5263 .readpage = btrfs_readpage, 5884 .readpage = btrfs_readpage,
5264 .writepage = btrfs_writepage, 5885 .writepage = btrfs_writepage,
5265 .writepages = btrfs_writepages, 5886 .writepages = btrfs_writepages,
@@ -5269,16 +5890,17 @@ static struct address_space_operations btrfs_aops = {
5269 .invalidatepage = btrfs_invalidatepage, 5890 .invalidatepage = btrfs_invalidatepage,
5270 .releasepage = btrfs_releasepage, 5891 .releasepage = btrfs_releasepage,
5271 .set_page_dirty = btrfs_set_page_dirty, 5892 .set_page_dirty = btrfs_set_page_dirty,
5893 .error_remove_page = generic_error_remove_page,
5272}; 5894};
5273 5895
5274static struct address_space_operations btrfs_symlink_aops = { 5896static const struct address_space_operations btrfs_symlink_aops = {
5275 .readpage = btrfs_readpage, 5897 .readpage = btrfs_readpage,
5276 .writepage = btrfs_writepage, 5898 .writepage = btrfs_writepage,
5277 .invalidatepage = btrfs_invalidatepage, 5899 .invalidatepage = btrfs_invalidatepage,
5278 .releasepage = btrfs_releasepage, 5900 .releasepage = btrfs_releasepage,
5279}; 5901};
5280 5902
5281static struct inode_operations btrfs_file_inode_operations = { 5903static const struct inode_operations btrfs_file_inode_operations = {
5282 .truncate = btrfs_truncate, 5904 .truncate = btrfs_truncate,
5283 .getattr = btrfs_getattr, 5905 .getattr = btrfs_getattr,
5284 .setattr = btrfs_setattr, 5906 .setattr = btrfs_setattr,
@@ -5290,7 +5912,7 @@ static struct inode_operations btrfs_file_inode_operations = {
5290 .fallocate = btrfs_fallocate, 5912 .fallocate = btrfs_fallocate,
5291 .fiemap = btrfs_fiemap, 5913 .fiemap = btrfs_fiemap,
5292}; 5914};
5293static struct inode_operations btrfs_special_inode_operations = { 5915static const struct inode_operations btrfs_special_inode_operations = {
5294 .getattr = btrfs_getattr, 5916 .getattr = btrfs_getattr,
5295 .setattr = btrfs_setattr, 5917 .setattr = btrfs_setattr,
5296 .permission = btrfs_permission, 5918 .permission = btrfs_permission,
@@ -5299,7 +5921,7 @@ static struct inode_operations btrfs_special_inode_operations = {
5299 .listxattr = btrfs_listxattr, 5921 .listxattr = btrfs_listxattr,
5300 .removexattr = btrfs_removexattr, 5922 .removexattr = btrfs_removexattr,
5301}; 5923};
5302static struct inode_operations btrfs_symlink_inode_operations = { 5924static const struct inode_operations btrfs_symlink_inode_operations = {
5303 .readlink = generic_readlink, 5925 .readlink = generic_readlink,
5304 .follow_link = page_follow_link_light, 5926 .follow_link = page_follow_link_light,
5305 .put_link = page_put_link, 5927 .put_link = page_put_link,
@@ -5309,3 +5931,7 @@ static struct inode_operations btrfs_symlink_inode_operations = {
5309 .listxattr = btrfs_listxattr, 5931 .listxattr = btrfs_listxattr,
5310 .removexattr = btrfs_removexattr, 5932 .removexattr = btrfs_removexattr,
5311}; 5933};
5934
5935const struct dentry_operations btrfs_dentry_operations = {
5936 .d_delete = btrfs_dentry_delete,
5937};