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.c617
1 files changed, 387 insertions, 230 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95542a1b3dfc..cc93b23ca352 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -71,6 +71,7 @@ static const struct file_operations btrfs_dir_file_operations;
71static struct extent_io_ops btrfs_extent_io_ops; 71static struct extent_io_ops btrfs_extent_io_ops;
72 72
73static struct kmem_cache *btrfs_inode_cachep; 73static struct kmem_cache *btrfs_inode_cachep;
74static struct kmem_cache *btrfs_delalloc_work_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep; 75struct kmem_cache *btrfs_trans_handle_cachep;
75struct kmem_cache *btrfs_transaction_cachep; 76struct kmem_cache *btrfs_transaction_cachep;
76struct kmem_cache *btrfs_path_cachep; 77struct kmem_cache *btrfs_path_cachep;
@@ -87,13 +88,17 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
88}; 89};
89 90
90static int btrfs_setsize(struct inode *inode, loff_t newsize); 91static int btrfs_setsize(struct inode *inode, struct iattr *attr);
91static int btrfs_truncate(struct inode *inode); 92static int btrfs_truncate(struct inode *inode);
92static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); 93static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
93static noinline int cow_file_range(struct inode *inode, 94static noinline int cow_file_range(struct inode *inode,
94 struct page *locked_page, 95 struct page *locked_page,
95 u64 start, u64 end, int *page_started, 96 u64 start, u64 end, int *page_started,
96 unsigned long *nr_written, int unlock); 97 unsigned long *nr_written, int unlock);
98static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
97 102
98static int btrfs_init_inode_security(struct btrfs_trans_handle *trans, 103static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
99 struct inode *inode, struct inode *dir, 104 struct inode *inode, struct inode *dir,
@@ -698,14 +703,19 @@ retry:
698 703
699 em->block_start = ins.objectid; 704 em->block_start = ins.objectid;
700 em->block_len = ins.offset; 705 em->block_len = ins.offset;
706 em->orig_block_len = ins.offset;
701 em->bdev = root->fs_info->fs_devices->latest_bdev; 707 em->bdev = root->fs_info->fs_devices->latest_bdev;
702 em->compress_type = async_extent->compress_type; 708 em->compress_type = async_extent->compress_type;
703 set_bit(EXTENT_FLAG_PINNED, &em->flags); 709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
704 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 710 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
711 em->generation = -1;
705 712
706 while (1) { 713 while (1) {
707 write_lock(&em_tree->lock); 714 write_lock(&em_tree->lock);
708 ret = add_extent_mapping(em_tree, em); 715 ret = add_extent_mapping(em_tree, em);
716 if (!ret)
717 list_move(&em->list,
718 &em_tree->modified_extents);
709 write_unlock(&em_tree->lock); 719 write_unlock(&em_tree->lock);
710 if (ret != -EEXIST) { 720 if (ret != -EEXIST) {
711 free_extent_map(em); 721 free_extent_map(em);
@@ -803,14 +813,14 @@ static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
803 * required to start IO on it. It may be clean and already done with 813 * required to start IO on it. It may be clean and already done with
804 * IO when we return. 814 * IO when we return.
805 */ 815 */
806static noinline int cow_file_range(struct inode *inode, 816static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
807 struct page *locked_page, 817 struct inode *inode,
808 u64 start, u64 end, int *page_started, 818 struct btrfs_root *root,
809 unsigned long *nr_written, 819 struct page *locked_page,
810 int unlock) 820 u64 start, u64 end, int *page_started,
821 unsigned long *nr_written,
822 int unlock)
811{ 823{
812 struct btrfs_root *root = BTRFS_I(inode)->root;
813 struct btrfs_trans_handle *trans;
814 u64 alloc_hint = 0; 824 u64 alloc_hint = 0;
815 u64 num_bytes; 825 u64 num_bytes;
816 unsigned long ram_size; 826 unsigned long ram_size;
@@ -823,25 +833,10 @@ static noinline int cow_file_range(struct inode *inode,
823 int ret = 0; 833 int ret = 0;
824 834
825 BUG_ON(btrfs_is_free_space_inode(inode)); 835 BUG_ON(btrfs_is_free_space_inode(inode));
826 trans = btrfs_join_transaction(root);
827 if (IS_ERR(trans)) {
828 extent_clear_unlock_delalloc(inode,
829 &BTRFS_I(inode)->io_tree,
830 start, end, locked_page,
831 EXTENT_CLEAR_UNLOCK_PAGE |
832 EXTENT_CLEAR_UNLOCK |
833 EXTENT_CLEAR_DELALLOC |
834 EXTENT_CLEAR_DIRTY |
835 EXTENT_SET_WRITEBACK |
836 EXTENT_END_WRITEBACK);
837 return PTR_ERR(trans);
838 }
839 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
840 836
841 num_bytes = (end - start + blocksize) & ~(blocksize - 1); 837 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
842 num_bytes = max(blocksize, num_bytes); 838 num_bytes = max(blocksize, num_bytes);
843 disk_num_bytes = num_bytes; 839 disk_num_bytes = num_bytes;
844 ret = 0;
845 840
846 /* if this is a small write inside eof, kick off defrag */ 841 /* if this is a small write inside eof, kick off defrag */
847 if (num_bytes < 64 * 1024 && 842 if (num_bytes < 64 * 1024 &&
@@ -900,12 +895,17 @@ static noinline int cow_file_range(struct inode *inode,
900 895
901 em->block_start = ins.objectid; 896 em->block_start = ins.objectid;
902 em->block_len = ins.offset; 897 em->block_len = ins.offset;
898 em->orig_block_len = ins.offset;
903 em->bdev = root->fs_info->fs_devices->latest_bdev; 899 em->bdev = root->fs_info->fs_devices->latest_bdev;
904 set_bit(EXTENT_FLAG_PINNED, &em->flags); 900 set_bit(EXTENT_FLAG_PINNED, &em->flags);
901 em->generation = -1;
905 902
906 while (1) { 903 while (1) {
907 write_lock(&em_tree->lock); 904 write_lock(&em_tree->lock);
908 ret = add_extent_mapping(em_tree, em); 905 ret = add_extent_mapping(em_tree, em);
906 if (!ret)
907 list_move(&em->list,
908 &em_tree->modified_extents);
909 write_unlock(&em_tree->lock); 909 write_unlock(&em_tree->lock);
910 if (ret != -EEXIST) { 910 if (ret != -EEXIST) {
911 free_extent_map(em); 911 free_extent_map(em);
@@ -952,11 +952,9 @@ static noinline int cow_file_range(struct inode *inode,
952 alloc_hint = ins.objectid + ins.offset; 952 alloc_hint = ins.objectid + ins.offset;
953 start += cur_alloc_size; 953 start += cur_alloc_size;
954 } 954 }
955 ret = 0;
956out: 955out:
957 btrfs_end_transaction(trans, root);
958
959 return ret; 956 return ret;
957
960out_unlock: 958out_unlock:
961 extent_clear_unlock_delalloc(inode, 959 extent_clear_unlock_delalloc(inode,
962 &BTRFS_I(inode)->io_tree, 960 &BTRFS_I(inode)->io_tree,
@@ -971,6 +969,39 @@ out_unlock:
971 goto out; 969 goto out;
972} 970}
973 971
972static noinline int cow_file_range(struct inode *inode,
973 struct page *locked_page,
974 u64 start, u64 end, int *page_started,
975 unsigned long *nr_written,
976 int unlock)
977{
978 struct btrfs_trans_handle *trans;
979 struct btrfs_root *root = BTRFS_I(inode)->root;
980 int ret;
981
982 trans = btrfs_join_transaction(root);
983 if (IS_ERR(trans)) {
984 extent_clear_unlock_delalloc(inode,
985 &BTRFS_I(inode)->io_tree,
986 start, end, locked_page,
987 EXTENT_CLEAR_UNLOCK_PAGE |
988 EXTENT_CLEAR_UNLOCK |
989 EXTENT_CLEAR_DELALLOC |
990 EXTENT_CLEAR_DIRTY |
991 EXTENT_SET_WRITEBACK |
992 EXTENT_END_WRITEBACK);
993 return PTR_ERR(trans);
994 }
995 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
996
997 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
998 page_started, nr_written, unlock);
999
1000 btrfs_end_transaction(trans, root);
1001
1002 return ret;
1003}
1004
974/* 1005/*
975 * work queue call back to started compression on a file and pages 1006 * work queue call back to started compression on a file and pages
976 */ 1007 */
@@ -1126,6 +1157,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1126 u64 extent_offset; 1157 u64 extent_offset;
1127 u64 disk_bytenr; 1158 u64 disk_bytenr;
1128 u64 num_bytes; 1159 u64 num_bytes;
1160 u64 disk_num_bytes;
1129 int extent_type; 1161 int extent_type;
1130 int ret, err; 1162 int ret, err;
1131 int type; 1163 int type;
@@ -1228,6 +1260,8 @@ next_slot:
1228 extent_offset = btrfs_file_extent_offset(leaf, fi); 1260 extent_offset = btrfs_file_extent_offset(leaf, fi);
1229 extent_end = found_key.offset + 1261 extent_end = found_key.offset +
1230 btrfs_file_extent_num_bytes(leaf, fi); 1262 btrfs_file_extent_num_bytes(leaf, fi);
1263 disk_num_bytes =
1264 btrfs_file_extent_disk_num_bytes(leaf, fi);
1231 if (extent_end <= start) { 1265 if (extent_end <= start) {
1232 path->slots[0]++; 1266 path->slots[0]++;
1233 goto next_slot; 1267 goto next_slot;
@@ -1281,9 +1315,9 @@ out_check:
1281 1315
1282 btrfs_release_path(path); 1316 btrfs_release_path(path);
1283 if (cow_start != (u64)-1) { 1317 if (cow_start != (u64)-1) {
1284 ret = cow_file_range(inode, locked_page, cow_start, 1318 ret = __cow_file_range(trans, inode, root, locked_page,
1285 found_key.offset - 1, page_started, 1319 cow_start, found_key.offset - 1,
1286 nr_written, 1); 1320 page_started, nr_written, 1);
1287 if (ret) { 1321 if (ret) {
1288 btrfs_abort_transaction(trans, root, ret); 1322 btrfs_abort_transaction(trans, root, ret);
1289 goto error; 1323 goto error;
@@ -1298,16 +1332,21 @@ out_check:
1298 em = alloc_extent_map(); 1332 em = alloc_extent_map();
1299 BUG_ON(!em); /* -ENOMEM */ 1333 BUG_ON(!em); /* -ENOMEM */
1300 em->start = cur_offset; 1334 em->start = cur_offset;
1301 em->orig_start = em->start; 1335 em->orig_start = found_key.offset - extent_offset;
1302 em->len = num_bytes; 1336 em->len = num_bytes;
1303 em->block_len = num_bytes; 1337 em->block_len = num_bytes;
1304 em->block_start = disk_bytenr; 1338 em->block_start = disk_bytenr;
1339 em->orig_block_len = disk_num_bytes;
1305 em->bdev = root->fs_info->fs_devices->latest_bdev; 1340 em->bdev = root->fs_info->fs_devices->latest_bdev;
1306 set_bit(EXTENT_FLAG_PINNED, &em->flags); 1341 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1307 set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 1342 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1343 em->generation = -1;
1308 while (1) { 1344 while (1) {
1309 write_lock(&em_tree->lock); 1345 write_lock(&em_tree->lock);
1310 ret = add_extent_mapping(em_tree, em); 1346 ret = add_extent_mapping(em_tree, em);
1347 if (!ret)
1348 list_move(&em->list,
1349 &em_tree->modified_extents);
1311 write_unlock(&em_tree->lock); 1350 write_unlock(&em_tree->lock);
1312 if (ret != -EEXIST) { 1351 if (ret != -EEXIST) {
1313 free_extent_map(em); 1352 free_extent_map(em);
@@ -1352,8 +1391,9 @@ out_check:
1352 } 1391 }
1353 1392
1354 if (cow_start != (u64)-1) { 1393 if (cow_start != (u64)-1) {
1355 ret = cow_file_range(inode, locked_page, cow_start, end, 1394 ret = __cow_file_range(trans, inode, root, locked_page,
1356 page_started, nr_written, 1); 1395 cow_start, end,
1396 page_started, nr_written, 1);
1357 if (ret) { 1397 if (ret) {
1358 btrfs_abort_transaction(trans, root, ret); 1398 btrfs_abort_transaction(trans, root, ret);
1359 goto error; 1399 goto error;
@@ -1531,7 +1571,6 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1531 unsigned long bio_flags) 1571 unsigned long bio_flags)
1532{ 1572{
1533 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 1573 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1534 struct btrfs_mapping_tree *map_tree;
1535 u64 logical = (u64)bio->bi_sector << 9; 1574 u64 logical = (u64)bio->bi_sector << 9;
1536 u64 length = 0; 1575 u64 length = 0;
1537 u64 map_length; 1576 u64 map_length;
@@ -1541,11 +1580,10 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1541 return 0; 1580 return 0;
1542 1581
1543 length = bio->bi_size; 1582 length = bio->bi_size;
1544 map_tree = &root->fs_info->mapping_tree;
1545 map_length = length; 1583 map_length = length;
1546 ret = btrfs_map_block(map_tree, READ, logical, 1584 ret = btrfs_map_block(root->fs_info, READ, logical,
1547 &map_length, NULL, 0); 1585 &map_length, NULL, 0);
1548 /* Will always return 0 or 1 with map_multi == NULL */ 1586 /* Will always return 0 with map_multi == NULL */
1549 BUG_ON(ret < 0); 1587 BUG_ON(ret < 0);
1550 if (map_length < length + size) 1588 if (map_length < length + size)
1551 return 1; 1589 return 1;
@@ -1586,7 +1624,12 @@ static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1586 u64 bio_offset) 1624 u64 bio_offset)
1587{ 1625{
1588 struct btrfs_root *root = BTRFS_I(inode)->root; 1626 struct btrfs_root *root = BTRFS_I(inode)->root;
1589 return btrfs_map_bio(root, rw, bio, mirror_num, 1); 1627 int ret;
1628
1629 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1630 if (ret)
1631 bio_endio(bio, ret);
1632 return ret;
1590} 1633}
1591 1634
1592/* 1635/*
@@ -1601,6 +1644,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1601 int ret = 0; 1644 int ret = 0;
1602 int skip_sum; 1645 int skip_sum;
1603 int metadata = 0; 1646 int metadata = 0;
1647 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1604 1648
1605 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 1649 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1606 1650
@@ -1610,31 +1654,43 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1610 if (!(rw & REQ_WRITE)) { 1654 if (!(rw & REQ_WRITE)) {
1611 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); 1655 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1612 if (ret) 1656 if (ret)
1613 return ret; 1657 goto out;
1614 1658
1615 if (bio_flags & EXTENT_BIO_COMPRESSED) { 1659 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1616 return btrfs_submit_compressed_read(inode, bio, 1660 ret = btrfs_submit_compressed_read(inode, bio,
1617 mirror_num, bio_flags); 1661 mirror_num,
1662 bio_flags);
1663 goto out;
1618 } else if (!skip_sum) { 1664 } else if (!skip_sum) {
1619 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); 1665 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1620 if (ret) 1666 if (ret)
1621 return ret; 1667 goto out;
1622 } 1668 }
1623 goto mapit; 1669 goto mapit;
1624 } else if (!skip_sum) { 1670 } else if (async && !skip_sum) {
1625 /* csum items have already been cloned */ 1671 /* csum items have already been cloned */
1626 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) 1672 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1627 goto mapit; 1673 goto mapit;
1628 /* we're doing a write, do the async checksumming */ 1674 /* we're doing a write, do the async checksumming */
1629 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 1675 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1630 inode, rw, bio, mirror_num, 1676 inode, rw, bio, mirror_num,
1631 bio_flags, bio_offset, 1677 bio_flags, bio_offset,
1632 __btrfs_submit_bio_start, 1678 __btrfs_submit_bio_start,
1633 __btrfs_submit_bio_done); 1679 __btrfs_submit_bio_done);
1680 goto out;
1681 } else if (!skip_sum) {
1682 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1683 if (ret)
1684 goto out;
1634 } 1685 }
1635 1686
1636mapit: 1687mapit:
1637 return btrfs_map_bio(root, rw, bio, mirror_num, 0); 1688 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1689
1690out:
1691 if (ret < 0)
1692 bio_endio(bio, ret);
1693 return ret;
1638} 1694}
1639 1695
1640/* 1696/*
@@ -1657,8 +1713,7 @@ static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1657int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, 1713int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1658 struct extent_state **cached_state) 1714 struct extent_state **cached_state)
1659{ 1715{
1660 if ((end & (PAGE_CACHE_SIZE - 1)) == 0) 1716 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
1661 WARN_ON(1);
1662 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, 1717 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1663 cached_state, GFP_NOFS); 1718 cached_state, GFP_NOFS);
1664} 1719}
@@ -1867,22 +1922,20 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
1867 1922
1868 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { 1923 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1869 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */ 1924 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
1870 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1925 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1871 if (!ret) { 1926 if (nolock)
1872 if (nolock) 1927 trans = btrfs_join_transaction_nolock(root);
1873 trans = btrfs_join_transaction_nolock(root); 1928 else
1874 else 1929 trans = btrfs_join_transaction(root);
1875 trans = btrfs_join_transaction(root); 1930 if (IS_ERR(trans)) {
1876 if (IS_ERR(trans)) { 1931 ret = PTR_ERR(trans);
1877 ret = PTR_ERR(trans); 1932 trans = NULL;
1878 trans = NULL; 1933 goto out;
1879 goto out;
1880 }
1881 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1882 ret = btrfs_update_inode_fallback(trans, root, inode);
1883 if (ret) /* -ENOMEM or corruption */
1884 btrfs_abort_transaction(trans, root, ret);
1885 } 1934 }
1935 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1936 ret = btrfs_update_inode_fallback(trans, root, inode);
1937 if (ret) /* -ENOMEM or corruption */
1938 btrfs_abort_transaction(trans, root, ret);
1886 goto out; 1939 goto out;
1887 } 1940 }
1888 1941
@@ -1931,15 +1984,11 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
1931 add_pending_csums(trans, inode, ordered_extent->file_offset, 1984 add_pending_csums(trans, inode, ordered_extent->file_offset,
1932 &ordered_extent->list); 1985 &ordered_extent->list);
1933 1986
1934 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1987 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1935 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { 1988 ret = btrfs_update_inode_fallback(trans, root, inode);
1936 ret = btrfs_update_inode_fallback(trans, root, inode); 1989 if (ret) { /* -ENOMEM or corruption */
1937 if (ret) { /* -ENOMEM or corruption */ 1990 btrfs_abort_transaction(trans, root, ret);
1938 btrfs_abort_transaction(trans, root, ret); 1991 goto out_unlock;
1939 goto out_unlock;
1940 }
1941 } else {
1942 btrfs_set_inode_last_trans(trans, inode);
1943 } 1992 }
1944 ret = 0; 1993 ret = 0;
1945out_unlock: 1994out_unlock:
@@ -2429,6 +2478,18 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2429 continue; 2478 continue;
2430 } 2479 }
2431 nr_truncate++; 2480 nr_truncate++;
2481
2482 /* 1 for the orphan item deletion. */
2483 trans = btrfs_start_transaction(root, 1);
2484 if (IS_ERR(trans)) {
2485 ret = PTR_ERR(trans);
2486 goto out;
2487 }
2488 ret = btrfs_orphan_add(trans, inode);
2489 btrfs_end_transaction(trans, root);
2490 if (ret)
2491 goto out;
2492
2432 ret = btrfs_truncate(inode); 2493 ret = btrfs_truncate(inode);
2433 } else { 2494 } else {
2434 nr_unlink++; 2495 nr_unlink++;
@@ -3074,7 +3135,6 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3074 struct btrfs_trans_handle *trans; 3135 struct btrfs_trans_handle *trans;
3075 struct inode *inode = dentry->d_inode; 3136 struct inode *inode = dentry->d_inode;
3076 int ret; 3137 int ret;
3077 unsigned long nr = 0;
3078 3138
3079 trans = __unlink_start_trans(dir, dentry); 3139 trans = __unlink_start_trans(dir, dentry);
3080 if (IS_ERR(trans)) 3140 if (IS_ERR(trans))
@@ -3094,9 +3154,8 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3094 } 3154 }
3095 3155
3096out: 3156out:
3097 nr = trans->blocks_used;
3098 __unlink_end_trans(trans, root); 3157 __unlink_end_trans(trans, root);
3099 btrfs_btree_balance_dirty(root, nr); 3158 btrfs_btree_balance_dirty(root);
3100 return ret; 3159 return ret;
3101} 3160}
3102 3161
@@ -3186,7 +3245,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3186 int err = 0; 3245 int err = 0;
3187 struct btrfs_root *root = BTRFS_I(dir)->root; 3246 struct btrfs_root *root = BTRFS_I(dir)->root;
3188 struct btrfs_trans_handle *trans; 3247 struct btrfs_trans_handle *trans;
3189 unsigned long nr = 0;
3190 3248
3191 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) 3249 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
3192 return -ENOTEMPTY; 3250 return -ENOTEMPTY;
@@ -3215,9 +3273,8 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3215 if (!err) 3273 if (!err)
3216 btrfs_i_size_write(inode, 0); 3274 btrfs_i_size_write(inode, 0);
3217out: 3275out:
3218 nr = trans->blocks_used;
3219 __unlink_end_trans(trans, root); 3276 __unlink_end_trans(trans, root);
3220 btrfs_btree_balance_dirty(root, nr); 3277 btrfs_btree_balance_dirty(root);
3221 3278
3222 return err; 3279 return err;
3223} 3280}
@@ -3497,11 +3554,11 @@ int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3497 if (ret) 3554 if (ret)
3498 goto out; 3555 goto out;
3499 3556
3500 ret = -ENOMEM;
3501again: 3557again:
3502 page = find_or_create_page(mapping, index, mask); 3558 page = find_or_create_page(mapping, index, mask);
3503 if (!page) { 3559 if (!page) {
3504 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE); 3560 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3561 ret = -ENOMEM;
3505 goto out; 3562 goto out;
3506 } 3563 }
3507 3564
@@ -3550,7 +3607,6 @@ again:
3550 goto out_unlock; 3607 goto out_unlock;
3551 } 3608 }
3552 3609
3553 ret = 0;
3554 if (offset != PAGE_CACHE_SIZE) { 3610 if (offset != PAGE_CACHE_SIZE) {
3555 if (!len) 3611 if (!len)
3556 len = PAGE_CACHE_SIZE - offset; 3612 len = PAGE_CACHE_SIZE - offset;
@@ -3621,6 +3677,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3621 block_end - cur_offset, 0); 3677 block_end - cur_offset, 0);
3622 if (IS_ERR(em)) { 3678 if (IS_ERR(em)) {
3623 err = PTR_ERR(em); 3679 err = PTR_ERR(em);
3680 em = NULL;
3624 break; 3681 break;
3625 } 3682 }
3626 last_byte = min(extent_map_end(em), block_end); 3683 last_byte = min(extent_map_end(em), block_end);
@@ -3668,6 +3725,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3668 3725
3669 hole_em->block_start = EXTENT_MAP_HOLE; 3726 hole_em->block_start = EXTENT_MAP_HOLE;
3670 hole_em->block_len = 0; 3727 hole_em->block_len = 0;
3728 hole_em->orig_block_len = 0;
3671 hole_em->bdev = root->fs_info->fs_devices->latest_bdev; 3729 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3672 hole_em->compress_type = BTRFS_COMPRESS_NONE; 3730 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3673 hole_em->generation = trans->transid; 3731 hole_em->generation = trans->transid;
@@ -3703,16 +3761,27 @@ next:
3703 return err; 3761 return err;
3704} 3762}
3705 3763
3706static int btrfs_setsize(struct inode *inode, loff_t newsize) 3764static int btrfs_setsize(struct inode *inode, struct iattr *attr)
3707{ 3765{
3708 struct btrfs_root *root = BTRFS_I(inode)->root; 3766 struct btrfs_root *root = BTRFS_I(inode)->root;
3709 struct btrfs_trans_handle *trans; 3767 struct btrfs_trans_handle *trans;
3710 loff_t oldsize = i_size_read(inode); 3768 loff_t oldsize = i_size_read(inode);
3769 loff_t newsize = attr->ia_size;
3770 int mask = attr->ia_valid;
3711 int ret; 3771 int ret;
3712 3772
3713 if (newsize == oldsize) 3773 if (newsize == oldsize)
3714 return 0; 3774 return 0;
3715 3775
3776 /*
3777 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
3778 * special case where we need to update the times despite not having
3779 * these flags set. For all other operations the VFS set these flags
3780 * explicitly if it wants a timestamp update.
3781 */
3782 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
3783 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
3784
3716 if (newsize > oldsize) { 3785 if (newsize > oldsize) {
3717 truncate_pagecache(inode, oldsize, newsize); 3786 truncate_pagecache(inode, oldsize, newsize);
3718 ret = btrfs_cont_expand(inode, oldsize, newsize); 3787 ret = btrfs_cont_expand(inode, oldsize, newsize);
@@ -3738,9 +3807,34 @@ static int btrfs_setsize(struct inode *inode, loff_t newsize)
3738 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, 3807 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3739 &BTRFS_I(inode)->runtime_flags); 3808 &BTRFS_I(inode)->runtime_flags);
3740 3809
3810 /*
3811 * 1 for the orphan item we're going to add
3812 * 1 for the orphan item deletion.
3813 */
3814 trans = btrfs_start_transaction(root, 2);
3815 if (IS_ERR(trans))
3816 return PTR_ERR(trans);
3817
3818 /*
3819 * We need to do this in case we fail at _any_ point during the
3820 * actual truncate. Once we do the truncate_setsize we could
3821 * invalidate pages which forces any outstanding ordered io to
3822 * be instantly completed which will give us extents that need
3823 * to be truncated. If we fail to get an orphan inode down we
3824 * could have left over extents that were never meant to live,
3825 * so we need to garuntee from this point on that everything
3826 * will be consistent.
3827 */
3828 ret = btrfs_orphan_add(trans, inode);
3829 btrfs_end_transaction(trans, root);
3830 if (ret)
3831 return ret;
3832
3741 /* we don't support swapfiles, so vmtruncate shouldn't fail */ 3833 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3742 truncate_setsize(inode, newsize); 3834 truncate_setsize(inode, newsize);
3743 ret = btrfs_truncate(inode); 3835 ret = btrfs_truncate(inode);
3836 if (ret && inode->i_nlink)
3837 btrfs_orphan_del(NULL, inode);
3744 } 3838 }
3745 3839
3746 return ret; 3840 return ret;
@@ -3760,7 +3854,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3760 return err; 3854 return err;
3761 3855
3762 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3856 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3763 err = btrfs_setsize(inode, attr->ia_size); 3857 err = btrfs_setsize(inode, attr);
3764 if (err) 3858 if (err)
3765 return err; 3859 return err;
3766 } 3860 }
@@ -3783,7 +3877,6 @@ void btrfs_evict_inode(struct inode *inode)
3783 struct btrfs_root *root = BTRFS_I(inode)->root; 3877 struct btrfs_root *root = BTRFS_I(inode)->root;
3784 struct btrfs_block_rsv *rsv, *global_rsv; 3878 struct btrfs_block_rsv *rsv, *global_rsv;
3785 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); 3879 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
3786 unsigned long nr;
3787 int ret; 3880 int ret;
3788 3881
3789 trace_btrfs_inode_evict(inode); 3882 trace_btrfs_inode_evict(inode);
@@ -3829,7 +3922,8 @@ void btrfs_evict_inode(struct inode *inode)
3829 * inode item when doing the truncate. 3922 * inode item when doing the truncate.
3830 */ 3923 */
3831 while (1) { 3924 while (1) {
3832 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size); 3925 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3926 BTRFS_RESERVE_FLUSH_LIMIT);
3833 3927
3834 /* 3928 /*
3835 * Try and steal from the global reserve since we will 3929 * Try and steal from the global reserve since we will
@@ -3847,7 +3941,7 @@ void btrfs_evict_inode(struct inode *inode)
3847 goto no_delete; 3941 goto no_delete;
3848 } 3942 }
3849 3943
3850 trans = btrfs_start_transaction_noflush(root, 1); 3944 trans = btrfs_start_transaction_lflush(root, 1);
3851 if (IS_ERR(trans)) { 3945 if (IS_ERR(trans)) {
3852 btrfs_orphan_del(NULL, inode); 3946 btrfs_orphan_del(NULL, inode);
3853 btrfs_free_block_rsv(root, rsv); 3947 btrfs_free_block_rsv(root, rsv);
@@ -3864,10 +3958,9 @@ void btrfs_evict_inode(struct inode *inode)
3864 ret = btrfs_update_inode(trans, root, inode); 3958 ret = btrfs_update_inode(trans, root, inode);
3865 BUG_ON(ret); 3959 BUG_ON(ret);
3866 3960
3867 nr = trans->blocks_used;
3868 btrfs_end_transaction(trans, root); 3961 btrfs_end_transaction(trans, root);
3869 trans = NULL; 3962 trans = NULL;
3870 btrfs_btree_balance_dirty(root, nr); 3963 btrfs_btree_balance_dirty(root);
3871 } 3964 }
3872 3965
3873 btrfs_free_block_rsv(root, rsv); 3966 btrfs_free_block_rsv(root, rsv);
@@ -3883,9 +3976,8 @@ void btrfs_evict_inode(struct inode *inode)
3883 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) 3976 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3884 btrfs_return_ino(root, btrfs_ino(inode)); 3977 btrfs_return_ino(root, btrfs_ino(inode));
3885 3978
3886 nr = trans->blocks_used;
3887 btrfs_end_transaction(trans, root); 3979 btrfs_end_transaction(trans, root);
3888 btrfs_btree_balance_dirty(root, nr); 3980 btrfs_btree_balance_dirty(root);
3889no_delete: 3981no_delete:
3890 clear_inode(inode); 3982 clear_inode(inode);
3891 return; 3983 return;
@@ -4219,16 +4311,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4219 if (dentry->d_name.len > BTRFS_NAME_LEN) 4311 if (dentry->d_name.len > BTRFS_NAME_LEN)
4220 return ERR_PTR(-ENAMETOOLONG); 4312 return ERR_PTR(-ENAMETOOLONG);
4221 4313
4222 if (unlikely(d_need_lookup(dentry))) { 4314 ret = btrfs_inode_by_name(dir, dentry, &location);
4223 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4224 kfree(dentry->d_fsdata);
4225 dentry->d_fsdata = NULL;
4226 /* This thing is hashed, drop it for now */
4227 d_drop(dentry);
4228 } else {
4229 ret = btrfs_inode_by_name(dir, dentry, &location);
4230 }
4231
4232 if (ret < 0) 4315 if (ret < 0)
4233 return ERR_PTR(ret); 4316 return ERR_PTR(ret);
4234 4317
@@ -4298,11 +4381,6 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4298 struct dentry *ret; 4381 struct dentry *ret;
4299 4382
4300 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); 4383 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4301 if (unlikely(d_need_lookup(dentry))) {
4302 spin_lock(&dentry->d_lock);
4303 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4304 spin_unlock(&dentry->d_lock);
4305 }
4306 return ret; 4384 return ret;
4307} 4385}
4308 4386
@@ -4775,8 +4853,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4775 if (S_ISREG(mode)) { 4853 if (S_ISREG(mode)) {
4776 if (btrfs_test_opt(root, NODATASUM)) 4854 if (btrfs_test_opt(root, NODATASUM))
4777 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 4855 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4778 if (btrfs_test_opt(root, NODATACOW) || 4856 if (btrfs_test_opt(root, NODATACOW))
4779 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4780 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 4857 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4781 } 4858 }
4782 4859
@@ -4842,7 +4919,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
4842 ret = btrfs_insert_dir_item(trans, root, name, name_len, 4919 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4843 parent_inode, &key, 4920 parent_inode, &key,
4844 btrfs_inode_type(inode), index); 4921 btrfs_inode_type(inode), index);
4845 if (ret == -EEXIST) 4922 if (ret == -EEXIST || ret == -EOVERFLOW)
4846 goto fail_dir_item; 4923 goto fail_dir_item;
4847 else if (ret) { 4924 else if (ret) {
4848 btrfs_abort_transaction(trans, root, ret); 4925 btrfs_abort_transaction(trans, root, ret);
@@ -4897,7 +4974,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4897 int err; 4974 int err;
4898 int drop_inode = 0; 4975 int drop_inode = 0;
4899 u64 objectid; 4976 u64 objectid;
4900 unsigned long nr = 0;
4901 u64 index = 0; 4977 u64 index = 0;
4902 4978
4903 if (!new_valid_dev(rdev)) 4979 if (!new_valid_dev(rdev))
@@ -4930,6 +5006,12 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4930 goto out_unlock; 5006 goto out_unlock;
4931 } 5007 }
4932 5008
5009 err = btrfs_update_inode(trans, root, inode);
5010 if (err) {
5011 drop_inode = 1;
5012 goto out_unlock;
5013 }
5014
4933 /* 5015 /*
4934 * If the active LSM wants to access the inode during 5016 * If the active LSM wants to access the inode during
4935 * d_instantiate it needs these. Smack checks to see 5017 * d_instantiate it needs these. Smack checks to see
@@ -4947,9 +5029,8 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4947 d_instantiate(dentry, inode); 5029 d_instantiate(dentry, inode);
4948 } 5030 }
4949out_unlock: 5031out_unlock:
4950 nr = trans->blocks_used;
4951 btrfs_end_transaction(trans, root); 5032 btrfs_end_transaction(trans, root);
4952 btrfs_btree_balance_dirty(root, nr); 5033 btrfs_btree_balance_dirty(root);
4953 if (drop_inode) { 5034 if (drop_inode) {
4954 inode_dec_link_count(inode); 5035 inode_dec_link_count(inode);
4955 iput(inode); 5036 iput(inode);
@@ -4963,9 +5044,8 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4963 struct btrfs_trans_handle *trans; 5044 struct btrfs_trans_handle *trans;
4964 struct btrfs_root *root = BTRFS_I(dir)->root; 5045 struct btrfs_root *root = BTRFS_I(dir)->root;
4965 struct inode *inode = NULL; 5046 struct inode *inode = NULL;
4966 int drop_inode = 0; 5047 int drop_inode_on_err = 0;
4967 int err; 5048 int err;
4968 unsigned long nr = 0;
4969 u64 objectid; 5049 u64 objectid;
4970 u64 index = 0; 5050 u64 index = 0;
4971 5051
@@ -4989,12 +5069,15 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4989 err = PTR_ERR(inode); 5069 err = PTR_ERR(inode);
4990 goto out_unlock; 5070 goto out_unlock;
4991 } 5071 }
5072 drop_inode_on_err = 1;
4992 5073
4993 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name); 5074 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4994 if (err) { 5075 if (err)
4995 drop_inode = 1; 5076 goto out_unlock;
5077
5078 err = btrfs_update_inode(trans, root, inode);
5079 if (err)
4996 goto out_unlock; 5080 goto out_unlock;
4997 }
4998 5081
4999 /* 5082 /*
5000 * If the active LSM wants to access the inode during 5083 * If the active LSM wants to access the inode during
@@ -5007,21 +5090,20 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
5007 5090
5008 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 5091 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5009 if (err) 5092 if (err)
5010 drop_inode = 1; 5093 goto out_unlock;
5011 else { 5094
5012 inode->i_mapping->a_ops = &btrfs_aops; 5095 inode->i_mapping->a_ops = &btrfs_aops;
5013 inode->i_mapping->backing_dev_info = &root->fs_info->bdi; 5096 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5014 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 5097 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5015 d_instantiate(dentry, inode); 5098 d_instantiate(dentry, inode);
5016 } 5099
5017out_unlock: 5100out_unlock:
5018 nr = trans->blocks_used;
5019 btrfs_end_transaction(trans, root); 5101 btrfs_end_transaction(trans, root);
5020 if (drop_inode) { 5102 if (err && drop_inode_on_err) {
5021 inode_dec_link_count(inode); 5103 inode_dec_link_count(inode);
5022 iput(inode); 5104 iput(inode);
5023 } 5105 }
5024 btrfs_btree_balance_dirty(root, nr); 5106 btrfs_btree_balance_dirty(root);
5025 return err; 5107 return err;
5026} 5108}
5027 5109
@@ -5032,7 +5114,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5032 struct btrfs_root *root = BTRFS_I(dir)->root; 5114 struct btrfs_root *root = BTRFS_I(dir)->root;
5033 struct inode *inode = old_dentry->d_inode; 5115 struct inode *inode = old_dentry->d_inode;
5034 u64 index; 5116 u64 index;
5035 unsigned long nr = 0;
5036 int err; 5117 int err;
5037 int drop_inode = 0; 5118 int drop_inode = 0;
5038 5119
@@ -5062,6 +5143,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5062 inode_inc_iversion(inode); 5143 inode_inc_iversion(inode);
5063 inode->i_ctime = CURRENT_TIME; 5144 inode->i_ctime = CURRENT_TIME;
5064 ihold(inode); 5145 ihold(inode);
5146 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
5065 5147
5066 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); 5148 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
5067 5149
@@ -5076,14 +5158,13 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5076 btrfs_log_new_name(trans, inode, NULL, parent); 5158 btrfs_log_new_name(trans, inode, NULL, parent);
5077 } 5159 }
5078 5160
5079 nr = trans->blocks_used;
5080 btrfs_end_transaction(trans, root); 5161 btrfs_end_transaction(trans, root);
5081fail: 5162fail:
5082 if (drop_inode) { 5163 if (drop_inode) {
5083 inode_dec_link_count(inode); 5164 inode_dec_link_count(inode);
5084 iput(inode); 5165 iput(inode);
5085 } 5166 }
5086 btrfs_btree_balance_dirty(root, nr); 5167 btrfs_btree_balance_dirty(root);
5087 return err; 5168 return err;
5088} 5169}
5089 5170
@@ -5096,7 +5177,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
5096 int drop_on_err = 0; 5177 int drop_on_err = 0;
5097 u64 objectid = 0; 5178 u64 objectid = 0;
5098 u64 index = 0; 5179 u64 index = 0;
5099 unsigned long nr = 1;
5100 5180
5101 /* 5181 /*
5102 * 2 items for inode and ref 5182 * 2 items for inode and ref
@@ -5142,11 +5222,10 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
5142 drop_on_err = 0; 5222 drop_on_err = 0;
5143 5223
5144out_fail: 5224out_fail:
5145 nr = trans->blocks_used;
5146 btrfs_end_transaction(trans, root); 5225 btrfs_end_transaction(trans, root);
5147 if (drop_on_err) 5226 if (drop_on_err)
5148 iput(inode); 5227 iput(inode);
5149 btrfs_btree_balance_dirty(root, nr); 5228 btrfs_btree_balance_dirty(root);
5150 return err; 5229 return err;
5151} 5230}
5152 5231
@@ -5340,6 +5419,7 @@ again:
5340 if (start + len <= found_key.offset) 5419 if (start + len <= found_key.offset)
5341 goto not_found; 5420 goto not_found;
5342 em->start = start; 5421 em->start = start;
5422 em->orig_start = start;
5343 em->len = found_key.offset - start; 5423 em->len = found_key.offset - start;
5344 goto not_found_em; 5424 goto not_found_em;
5345 } 5425 }
@@ -5350,6 +5430,8 @@ again:
5350 em->len = extent_end - extent_start; 5430 em->len = extent_end - extent_start;
5351 em->orig_start = extent_start - 5431 em->orig_start = extent_start -
5352 btrfs_file_extent_offset(leaf, item); 5432 btrfs_file_extent_offset(leaf, item);
5433 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5434 item);
5353 bytenr = btrfs_file_extent_disk_bytenr(leaf, item); 5435 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5354 if (bytenr == 0) { 5436 if (bytenr == 0) {
5355 em->block_start = EXTENT_MAP_HOLE; 5437 em->block_start = EXTENT_MAP_HOLE;
@@ -5359,8 +5441,7 @@ again:
5359 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 5441 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5360 em->compress_type = compress_type; 5442 em->compress_type = compress_type;
5361 em->block_start = bytenr; 5443 em->block_start = bytenr;
5362 em->block_len = btrfs_file_extent_disk_num_bytes(leaf, 5444 em->block_len = em->orig_block_len;
5363 item);
5364 } else { 5445 } else {
5365 bytenr += btrfs_file_extent_offset(leaf, item); 5446 bytenr += btrfs_file_extent_offset(leaf, item);
5366 em->block_start = bytenr; 5447 em->block_start = bytenr;
@@ -5390,7 +5471,8 @@ again:
5390 em->start = extent_start + extent_offset; 5471 em->start = extent_start + extent_offset;
5391 em->len = (copy_size + root->sectorsize - 1) & 5472 em->len = (copy_size + root->sectorsize - 1) &
5392 ~((u64)root->sectorsize - 1); 5473 ~((u64)root->sectorsize - 1);
5393 em->orig_start = EXTENT_MAP_INLINE; 5474 em->orig_block_len = em->len;
5475 em->orig_start = em->start;
5394 if (compress_type) { 5476 if (compress_type) {
5395 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 5477 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5396 em->compress_type = compress_type; 5478 em->compress_type = compress_type;
@@ -5439,11 +5521,11 @@ again:
5439 extent_map_end(em) - 1, NULL, GFP_NOFS); 5521 extent_map_end(em) - 1, NULL, GFP_NOFS);
5440 goto insert; 5522 goto insert;
5441 } else { 5523 } else {
5442 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type); 5524 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
5443 WARN_ON(1);
5444 } 5525 }
5445not_found: 5526not_found:
5446 em->start = start; 5527 em->start = start;
5528 em->orig_start = start;
5447 em->len = len; 5529 em->len = len;
5448not_found_em: 5530not_found_em:
5449 em->block_start = EXTENT_MAP_HOLE; 5531 em->block_start = EXTENT_MAP_HOLE;
@@ -5539,10 +5621,13 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5539 return em; 5621 return em;
5540 if (em) { 5622 if (em) {
5541 /* 5623 /*
5542 * if our em maps to a hole, there might 5624 * if our em maps to
5543 * actually be delalloc bytes behind it 5625 * - a hole or
5626 * - a pre-alloc extent,
5627 * there might actually be delalloc bytes behind it.
5544 */ 5628 */
5545 if (em->block_start != EXTENT_MAP_HOLE) 5629 if (em->block_start != EXTENT_MAP_HOLE &&
5630 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5546 return em; 5631 return em;
5547 else 5632 else
5548 hole_em = em; 5633 hole_em = em;
@@ -5624,6 +5709,8 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5624 */ 5709 */
5625 em->block_start = hole_em->block_start; 5710 em->block_start = hole_em->block_start;
5626 em->block_len = hole_len; 5711 em->block_len = hole_len;
5712 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
5713 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5627 } else { 5714 } else {
5628 em->start = range_start; 5715 em->start = range_start;
5629 em->len = found; 5716 em->len = found;
@@ -5645,38 +5732,19 @@ out:
5645} 5732}
5646 5733
5647static struct extent_map *btrfs_new_extent_direct(struct inode *inode, 5734static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5648 struct extent_map *em,
5649 u64 start, u64 len) 5735 u64 start, u64 len)
5650{ 5736{
5651 struct btrfs_root *root = BTRFS_I(inode)->root; 5737 struct btrfs_root *root = BTRFS_I(inode)->root;
5652 struct btrfs_trans_handle *trans; 5738 struct btrfs_trans_handle *trans;
5653 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 5739 struct extent_map *em;
5654 struct btrfs_key ins; 5740 struct btrfs_key ins;
5655 u64 alloc_hint; 5741 u64 alloc_hint;
5656 int ret; 5742 int ret;
5657 bool insert = false;
5658
5659 /*
5660 * Ok if the extent map we looked up is a hole and is for the exact
5661 * range we want, there is no reason to allocate a new one, however if
5662 * it is not right then we need to free this one and drop the cache for
5663 * our range.
5664 */
5665 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5666 em->len != len) {
5667 free_extent_map(em);
5668 em = NULL;
5669 insert = true;
5670 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5671 }
5672 5743
5673 trans = btrfs_join_transaction(root); 5744 trans = btrfs_join_transaction(root);
5674 if (IS_ERR(trans)) 5745 if (IS_ERR(trans))
5675 return ERR_CAST(trans); 5746 return ERR_CAST(trans);
5676 5747
5677 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5678 btrfs_add_inode_defrag(trans, inode);
5679
5680 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 5748 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5681 5749
5682 alloc_hint = get_extent_allocation_hint(inode, start, len); 5750 alloc_hint = get_extent_allocation_hint(inode, start, len);
@@ -5687,37 +5755,10 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5687 goto out; 5755 goto out;
5688 } 5756 }
5689 5757
5690 if (!em) { 5758 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5691 em = alloc_extent_map(); 5759 ins.offset, ins.offset, 0);
5692 if (!em) { 5760 if (IS_ERR(em))
5693 em = ERR_PTR(-ENOMEM); 5761 goto out;
5694 goto out;
5695 }
5696 }
5697
5698 em->start = start;
5699 em->orig_start = em->start;
5700 em->len = ins.offset;
5701
5702 em->block_start = ins.objectid;
5703 em->block_len = ins.offset;
5704 em->bdev = root->fs_info->fs_devices->latest_bdev;
5705
5706 /*
5707 * We need to do this because if we're using the original em we searched
5708 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5709 */
5710 em->flags = 0;
5711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5712
5713 while (insert) {
5714 write_lock(&em_tree->lock);
5715 ret = add_extent_mapping(em_tree, em);
5716 write_unlock(&em_tree->lock);
5717 if (ret != -EEXIST)
5718 break;
5719 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5720 }
5721 5762
5722 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, 5763 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5723 ins.offset, ins.offset, 0); 5764 ins.offset, ins.offset, 0);
@@ -5894,7 +5935,7 @@ static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5894static struct extent_map *create_pinned_em(struct inode *inode, u64 start, 5935static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5895 u64 len, u64 orig_start, 5936 u64 len, u64 orig_start,
5896 u64 block_start, u64 block_len, 5937 u64 block_start, u64 block_len,
5897 int type) 5938 u64 orig_block_len, int type)
5898{ 5939{
5899 struct extent_map_tree *em_tree; 5940 struct extent_map_tree *em_tree;
5900 struct extent_map *em; 5941 struct extent_map *em;
@@ -5912,15 +5953,20 @@ static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5912 em->block_len = block_len; 5953 em->block_len = block_len;
5913 em->block_start = block_start; 5954 em->block_start = block_start;
5914 em->bdev = root->fs_info->fs_devices->latest_bdev; 5955 em->bdev = root->fs_info->fs_devices->latest_bdev;
5956 em->orig_block_len = orig_block_len;
5957 em->generation = -1;
5915 set_bit(EXTENT_FLAG_PINNED, &em->flags); 5958 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5916 if (type == BTRFS_ORDERED_PREALLOC) 5959 if (type == BTRFS_ORDERED_PREALLOC)
5917 set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 5960 set_bit(EXTENT_FLAG_FILLING, &em->flags);
5918 5961
5919 do { 5962 do {
5920 btrfs_drop_extent_cache(inode, em->start, 5963 btrfs_drop_extent_cache(inode, em->start,
5921 em->start + em->len - 1, 0); 5964 em->start + em->len - 1, 0);
5922 write_lock(&em_tree->lock); 5965 write_lock(&em_tree->lock);
5923 ret = add_extent_mapping(em_tree, em); 5966 ret = add_extent_mapping(em_tree, em);
5967 if (!ret)
5968 list_move(&em->list,
5969 &em_tree->modified_extents);
5924 write_unlock(&em_tree->lock); 5970 write_unlock(&em_tree->lock);
5925 } while (ret == -EEXIST); 5971 } while (ret == -EEXIST);
5926 5972
@@ -6047,13 +6093,15 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6047 goto must_cow; 6093 goto must_cow;
6048 6094
6049 if (can_nocow_odirect(trans, inode, start, len) == 1) { 6095 if (can_nocow_odirect(trans, inode, start, len) == 1) {
6050 u64 orig_start = em->start; 6096 u64 orig_start = em->orig_start;
6097 u64 orig_block_len = em->orig_block_len;
6051 6098
6052 if (type == BTRFS_ORDERED_PREALLOC) { 6099 if (type == BTRFS_ORDERED_PREALLOC) {
6053 free_extent_map(em); 6100 free_extent_map(em);
6054 em = create_pinned_em(inode, start, len, 6101 em = create_pinned_em(inode, start, len,
6055 orig_start, 6102 orig_start,
6056 block_start, len, type); 6103 block_start, len,
6104 orig_block_len, type);
6057 if (IS_ERR(em)) { 6105 if (IS_ERR(em)) {
6058 btrfs_end_transaction(trans, root); 6106 btrfs_end_transaction(trans, root);
6059 goto unlock_err; 6107 goto unlock_err;
@@ -6077,7 +6125,8 @@ must_cow:
6077 * it above 6125 * it above
6078 */ 6126 */
6079 len = bh_result->b_size; 6127 len = bh_result->b_size;
6080 em = btrfs_new_extent_direct(inode, em, start, len); 6128 free_extent_map(em);
6129 em = btrfs_new_extent_direct(inode, start, len);
6081 if (IS_ERR(em)) { 6130 if (IS_ERR(em)) {
6082 ret = PTR_ERR(em); 6131 ret = PTR_ERR(em);
6083 goto unlock_err; 6132 goto unlock_err;
@@ -6318,6 +6367,9 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6318 struct btrfs_root *root = BTRFS_I(inode)->root; 6367 struct btrfs_root *root = BTRFS_I(inode)->root;
6319 int ret; 6368 int ret;
6320 6369
6370 if (async_submit)
6371 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6372
6321 bio_get(bio); 6373 bio_get(bio);
6322 6374
6323 if (!write) { 6375 if (!write) {
@@ -6362,7 +6414,6 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6362{ 6414{
6363 struct inode *inode = dip->inode; 6415 struct inode *inode = dip->inode;
6364 struct btrfs_root *root = BTRFS_I(inode)->root; 6416 struct btrfs_root *root = BTRFS_I(inode)->root;
6365 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6366 struct bio *bio; 6417 struct bio *bio;
6367 struct bio *orig_bio = dip->orig_bio; 6418 struct bio *orig_bio = dip->orig_bio;
6368 struct bio_vec *bvec = orig_bio->bi_io_vec; 6419 struct bio_vec *bvec = orig_bio->bi_io_vec;
@@ -6375,7 +6426,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6375 int async_submit = 0; 6426 int async_submit = 0;
6376 6427
6377 map_length = orig_bio->bi_size; 6428 map_length = orig_bio->bi_size;
6378 ret = btrfs_map_block(map_tree, READ, start_sector << 9, 6429 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
6379 &map_length, NULL, 0); 6430 &map_length, NULL, 0);
6380 if (ret) { 6431 if (ret) {
6381 bio_put(orig_bio); 6432 bio_put(orig_bio);
@@ -6429,7 +6480,8 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6429 bio->bi_end_io = btrfs_end_dio_bio; 6480 bio->bi_end_io = btrfs_end_dio_bio;
6430 6481
6431 map_length = orig_bio->bi_size; 6482 map_length = orig_bio->bi_size;
6432 ret = btrfs_map_block(map_tree, READ, start_sector << 9, 6483 ret = btrfs_map_block(root->fs_info, READ,
6484 start_sector << 9,
6433 &map_length, NULL, 0); 6485 &map_length, NULL, 0);
6434 if (ret) { 6486 if (ret) {
6435 bio_put(bio); 6487 bio_put(bio);
@@ -6582,9 +6634,17 @@ static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6582 btrfs_submit_direct, 0); 6634 btrfs_submit_direct, 0);
6583} 6635}
6584 6636
6637#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6638
6585static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 6639static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6586 __u64 start, __u64 len) 6640 __u64 start, __u64 len)
6587{ 6641{
6642 int ret;
6643
6644 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6645 if (ret)
6646 return ret;
6647
6588 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap); 6648 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6589} 6649}
6590 6650
@@ -6855,7 +6915,6 @@ static int btrfs_truncate(struct inode *inode)
6855 int ret; 6915 int ret;
6856 int err = 0; 6916 int err = 0;
6857 struct btrfs_trans_handle *trans; 6917 struct btrfs_trans_handle *trans;
6858 unsigned long nr;
6859 u64 mask = root->sectorsize - 1; 6918 u64 mask = root->sectorsize - 1;
6860 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); 6919 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
6861 6920
@@ -6910,11 +6969,9 @@ static int btrfs_truncate(struct inode *inode)
6910 6969
6911 /* 6970 /*
6912 * 1 for the truncate slack space 6971 * 1 for the truncate slack space
6913 * 1 for the orphan item we're going to add
6914 * 1 for the orphan item deletion
6915 * 1 for updating the inode. 6972 * 1 for updating the inode.
6916 */ 6973 */
6917 trans = btrfs_start_transaction(root, 4); 6974 trans = btrfs_start_transaction(root, 2);
6918 if (IS_ERR(trans)) { 6975 if (IS_ERR(trans)) {
6919 err = PTR_ERR(trans); 6976 err = PTR_ERR(trans);
6920 goto out; 6977 goto out;
@@ -6925,12 +6982,6 @@ static int btrfs_truncate(struct inode *inode)
6925 min_size); 6982 min_size);
6926 BUG_ON(ret); 6983 BUG_ON(ret);
6927 6984
6928 ret = btrfs_orphan_add(trans, inode);
6929 if (ret) {
6930 btrfs_end_transaction(trans, root);
6931 goto out;
6932 }
6933
6934 /* 6985 /*
6935 * setattr is responsible for setting the ordered_data_close flag, 6986 * setattr is responsible for setting the ordered_data_close flag,
6936 * but that is only tested during the last file release. That 6987 * but that is only tested during the last file release. That
@@ -6978,9 +7029,8 @@ static int btrfs_truncate(struct inode *inode)
6978 break; 7029 break;
6979 } 7030 }
6980 7031
6981 nr = trans->blocks_used;
6982 btrfs_end_transaction(trans, root); 7032 btrfs_end_transaction(trans, root);
6983 btrfs_btree_balance_dirty(root, nr); 7033 btrfs_btree_balance_dirty(root);
6984 7034
6985 trans = btrfs_start_transaction(root, 2); 7035 trans = btrfs_start_transaction(root, 2);
6986 if (IS_ERR(trans)) { 7036 if (IS_ERR(trans)) {
@@ -7000,12 +7050,6 @@ static int btrfs_truncate(struct inode *inode)
7000 ret = btrfs_orphan_del(trans, inode); 7050 ret = btrfs_orphan_del(trans, inode);
7001 if (ret) 7051 if (ret)
7002 err = ret; 7052 err = ret;
7003 } else if (ret && inode->i_nlink > 0) {
7004 /*
7005 * Failed to do the truncate, remove us from the in memory
7006 * orphan list.
7007 */
7008 ret = btrfs_orphan_del(NULL, inode);
7009 } 7053 }
7010 7054
7011 if (trans) { 7055 if (trans) {
@@ -7014,9 +7058,8 @@ static int btrfs_truncate(struct inode *inode)
7014 if (ret && !err) 7058 if (ret && !err)
7015 err = ret; 7059 err = ret;
7016 7060
7017 nr = trans->blocks_used;
7018 ret = btrfs_end_transaction(trans, root); 7061 ret = btrfs_end_transaction(trans, root);
7019 btrfs_btree_balance_dirty(root, nr); 7062 btrfs_btree_balance_dirty(root);
7020 } 7063 }
7021 7064
7022out: 7065out:
@@ -7093,6 +7136,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
7093 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); 7136 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
7094 ei->io_tree.track_uptodate = 1; 7137 ei->io_tree.track_uptodate = 1;
7095 ei->io_failure_tree.track_uptodate = 1; 7138 ei->io_failure_tree.track_uptodate = 1;
7139 atomic_set(&ei->sync_writers, 0);
7096 mutex_init(&ei->log_mutex); 7140 mutex_init(&ei->log_mutex);
7097 mutex_init(&ei->delalloc_mutex); 7141 mutex_init(&ei->delalloc_mutex);
7098 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 7142 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
@@ -7203,6 +7247,8 @@ void btrfs_destroy_cachep(void)
7203 kmem_cache_destroy(btrfs_path_cachep); 7247 kmem_cache_destroy(btrfs_path_cachep);
7204 if (btrfs_free_space_cachep) 7248 if (btrfs_free_space_cachep)
7205 kmem_cache_destroy(btrfs_free_space_cachep); 7249 kmem_cache_destroy(btrfs_free_space_cachep);
7250 if (btrfs_delalloc_work_cachep)
7251 kmem_cache_destroy(btrfs_delalloc_work_cachep);
7206} 7252}
7207 7253
7208int btrfs_init_cachep(void) 7254int btrfs_init_cachep(void)
@@ -7237,6 +7283,13 @@ int btrfs_init_cachep(void)
7237 if (!btrfs_free_space_cachep) 7283 if (!btrfs_free_space_cachep)
7238 goto fail; 7284 goto fail;
7239 7285
7286 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7287 sizeof(struct btrfs_delalloc_work), 0,
7288 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7289 NULL);
7290 if (!btrfs_delalloc_work_cachep)
7291 goto fail;
7292
7240 return 0; 7293 return 0;
7241fail: 7294fail:
7242 btrfs_destroy_cachep(); 7295 btrfs_destroy_cachep();
@@ -7308,6 +7361,28 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7308 if (S_ISDIR(old_inode->i_mode) && new_inode && 7361 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7309 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) 7362 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7310 return -ENOTEMPTY; 7363 return -ENOTEMPTY;
7364
7365
7366 /* check for collisions, even if the name isn't there */
7367 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
7368 new_dentry->d_name.name,
7369 new_dentry->d_name.len);
7370
7371 if (ret) {
7372 if (ret == -EEXIST) {
7373 /* we shouldn't get
7374 * eexist without a new_inode */
7375 if (!new_inode) {
7376 WARN_ON(1);
7377 return ret;
7378 }
7379 } else {
7380 /* maybe -EOVERFLOW */
7381 return ret;
7382 }
7383 }
7384 ret = 0;
7385
7311 /* 7386 /*
7312 * we're using rename to replace one file with another. 7387 * we're using rename to replace one file with another.
7313 * and the replacement file is large. Start IO on it now so 7388 * and the replacement file is large. Start IO on it now so
@@ -7447,39 +7522,110 @@ out_notrans:
7447 return ret; 7522 return ret;
7448} 7523}
7449 7524
7525static void btrfs_run_delalloc_work(struct btrfs_work *work)
7526{
7527 struct btrfs_delalloc_work *delalloc_work;
7528
7529 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7530 work);
7531 if (delalloc_work->wait)
7532 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7533 else
7534 filemap_flush(delalloc_work->inode->i_mapping);
7535
7536 if (delalloc_work->delay_iput)
7537 btrfs_add_delayed_iput(delalloc_work->inode);
7538 else
7539 iput(delalloc_work->inode);
7540 complete(&delalloc_work->completion);
7541}
7542
7543struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7544 int wait, int delay_iput)
7545{
7546 struct btrfs_delalloc_work *work;
7547
7548 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7549 if (!work)
7550 return NULL;
7551
7552 init_completion(&work->completion);
7553 INIT_LIST_HEAD(&work->list);
7554 work->inode = inode;
7555 work->wait = wait;
7556 work->delay_iput = delay_iput;
7557 work->work.func = btrfs_run_delalloc_work;
7558
7559 return work;
7560}
7561
7562void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7563{
7564 wait_for_completion(&work->completion);
7565 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7566}
7567
7450/* 7568/*
7451 * some fairly slow code that needs optimization. This walks the list 7569 * some fairly slow code that needs optimization. This walks the list
7452 * of all the inodes with pending delalloc and forces them to disk. 7570 * of all the inodes with pending delalloc and forces them to disk.
7453 */ 7571 */
7454int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) 7572int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7455{ 7573{
7456 struct list_head *head = &root->fs_info->delalloc_inodes;
7457 struct btrfs_inode *binode; 7574 struct btrfs_inode *binode;
7458 struct inode *inode; 7575 struct inode *inode;
7576 struct btrfs_delalloc_work *work, *next;
7577 struct list_head works;
7578 struct list_head splice;
7579 int ret = 0;
7459 7580
7460 if (root->fs_info->sb->s_flags & MS_RDONLY) 7581 if (root->fs_info->sb->s_flags & MS_RDONLY)
7461 return -EROFS; 7582 return -EROFS;
7462 7583
7584 INIT_LIST_HEAD(&works);
7585 INIT_LIST_HEAD(&splice);
7586again:
7463 spin_lock(&root->fs_info->delalloc_lock); 7587 spin_lock(&root->fs_info->delalloc_lock);
7464 while (!list_empty(head)) { 7588 list_splice_init(&root->fs_info->delalloc_inodes, &splice);
7465 binode = list_entry(head->next, struct btrfs_inode, 7589 while (!list_empty(&splice)) {
7590 binode = list_entry(splice.next, struct btrfs_inode,
7466 delalloc_inodes); 7591 delalloc_inodes);
7592
7593 list_del_init(&binode->delalloc_inodes);
7594
7467 inode = igrab(&binode->vfs_inode); 7595 inode = igrab(&binode->vfs_inode);
7468 if (!inode) 7596 if (!inode)
7469 list_del_init(&binode->delalloc_inodes); 7597 continue;
7598
7599 list_add_tail(&binode->delalloc_inodes,
7600 &root->fs_info->delalloc_inodes);
7470 spin_unlock(&root->fs_info->delalloc_lock); 7601 spin_unlock(&root->fs_info->delalloc_lock);
7471 if (inode) { 7602
7472 filemap_flush(inode->i_mapping); 7603 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7473 if (delay_iput) 7604 if (unlikely(!work)) {
7474 btrfs_add_delayed_iput(inode); 7605 ret = -ENOMEM;
7475 else 7606 goto out;
7476 iput(inode);
7477 } 7607 }
7608 list_add_tail(&work->list, &works);
7609 btrfs_queue_worker(&root->fs_info->flush_workers,
7610 &work->work);
7611
7478 cond_resched(); 7612 cond_resched();
7479 spin_lock(&root->fs_info->delalloc_lock); 7613 spin_lock(&root->fs_info->delalloc_lock);
7480 } 7614 }
7481 spin_unlock(&root->fs_info->delalloc_lock); 7615 spin_unlock(&root->fs_info->delalloc_lock);
7482 7616
7617 list_for_each_entry_safe(work, next, &works, list) {
7618 list_del_init(&work->list);
7619 btrfs_wait_and_free_delalloc_work(work);
7620 }
7621
7622 spin_lock(&root->fs_info->delalloc_lock);
7623 if (!list_empty(&root->fs_info->delalloc_inodes)) {
7624 spin_unlock(&root->fs_info->delalloc_lock);
7625 goto again;
7626 }
7627 spin_unlock(&root->fs_info->delalloc_lock);
7628
7483 /* the filemap_flush will queue IO into the worker threads, but 7629 /* the filemap_flush will queue IO into the worker threads, but
7484 * we have to make sure the IO is actually started and that 7630 * we have to make sure the IO is actually started and that
7485 * ordered extents get created before we return 7631 * ordered extents get created before we return
@@ -7493,6 +7639,18 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7493 } 7639 }
7494 atomic_dec(&root->fs_info->async_submit_draining); 7640 atomic_dec(&root->fs_info->async_submit_draining);
7495 return 0; 7641 return 0;
7642out:
7643 list_for_each_entry_safe(work, next, &works, list) {
7644 list_del_init(&work->list);
7645 btrfs_wait_and_free_delalloc_work(work);
7646 }
7647
7648 if (!list_empty_careful(&splice)) {
7649 spin_lock(&root->fs_info->delalloc_lock);
7650 list_splice_tail(&splice, &root->fs_info->delalloc_inodes);
7651 spin_unlock(&root->fs_info->delalloc_lock);
7652 }
7653 return ret;
7496} 7654}
7497 7655
7498static int btrfs_symlink(struct inode *dir, struct dentry *dentry, 7656static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
@@ -7512,7 +7670,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7512 unsigned long ptr; 7670 unsigned long ptr;
7513 struct btrfs_file_extent_item *ei; 7671 struct btrfs_file_extent_item *ei;
7514 struct extent_buffer *leaf; 7672 struct extent_buffer *leaf;
7515 unsigned long nr = 0;
7516 7673
7517 name_len = strlen(symname) + 1; 7674 name_len = strlen(symname) + 1;
7518 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 7675 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
@@ -7610,13 +7767,12 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7610out_unlock: 7767out_unlock:
7611 if (!err) 7768 if (!err)
7612 d_instantiate(dentry, inode); 7769 d_instantiate(dentry, inode);
7613 nr = trans->blocks_used;
7614 btrfs_end_transaction(trans, root); 7770 btrfs_end_transaction(trans, root);
7615 if (drop_inode) { 7771 if (drop_inode) {
7616 inode_dec_link_count(inode); 7772 inode_dec_link_count(inode);
7617 iput(inode); 7773 iput(inode);
7618 } 7774 }
7619 btrfs_btree_balance_dirty(root, nr); 7775 btrfs_btree_balance_dirty(root);
7620 return err; 7776 return err;
7621} 7777}
7622 7778
@@ -7679,6 +7835,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7679 em->len = ins.offset; 7835 em->len = ins.offset;
7680 em->block_start = ins.objectid; 7836 em->block_start = ins.objectid;
7681 em->block_len = ins.offset; 7837 em->block_len = ins.offset;
7838 em->orig_block_len = ins.offset;
7682 em->bdev = root->fs_info->fs_devices->latest_bdev; 7839 em->bdev = root->fs_info->fs_devices->latest_bdev;
7683 set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 7840 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7684 em->generation = trans->transid; 7841 em->generation = trans->transid;