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.c581
1 files changed, 370 insertions, 211 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index b3ad168a0bfc..8cd109972fa6 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -88,13 +88,14 @@ static noinline int cow_file_range(struct inode *inode,
88 u64 start, u64 end, int *page_started, 88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock); 89 unsigned long *nr_written, int unlock);
90 90
91static int btrfs_init_inode_security(struct inode *inode, struct inode *dir) 91static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
92 struct inode *inode, struct inode *dir)
92{ 93{
93 int err; 94 int err;
94 95
95 err = btrfs_init_acl(inode, dir); 96 err = btrfs_init_acl(trans, inode, dir);
96 if (!err) 97 if (!err)
97 err = btrfs_xattr_security_init(inode, dir); 98 err = btrfs_xattr_security_init(trans, inode, dir);
98 return err; 99 return err;
99} 100}
100 101
@@ -188,8 +189,18 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
188 btrfs_mark_buffer_dirty(leaf); 189 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path); 190 btrfs_free_path(path);
190 191
192 /*
193 * we're an inline extent, so nobody can
194 * extend the file past i_size without locking
195 * a page we already have locked.
196 *
197 * We must do any isize and inode updates
198 * before we unlock the pages. Otherwise we
199 * could end up racing with unlink.
200 */
191 BTRFS_I(inode)->disk_i_size = inode->i_size; 201 BTRFS_I(inode)->disk_i_size = inode->i_size;
192 btrfs_update_inode(trans, root, inode); 202 btrfs_update_inode(trans, root, inode);
203
193 return 0; 204 return 0;
194fail: 205fail:
195 btrfs_free_path(path); 206 btrfs_free_path(path);
@@ -230,8 +241,7 @@ static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
230 return 1; 241 return 1;
231 } 242 }
232 243
233 ret = btrfs_drop_extents(trans, root, inode, start, 244 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
234 aligned_end, aligned_end, start,
235 &hint_byte, 1); 245 &hint_byte, 1);
236 BUG_ON(ret); 246 BUG_ON(ret);
237 247
@@ -416,7 +426,6 @@ again:
416 start, end, 426 start, end,
417 total_compressed, pages); 427 total_compressed, pages);
418 } 428 }
419 btrfs_end_transaction(trans, root);
420 if (ret == 0) { 429 if (ret == 0) {
421 /* 430 /*
422 * inline extent creation worked, we don't need 431 * inline extent creation worked, we don't need
@@ -430,9 +439,11 @@ again:
430 EXTENT_CLEAR_DELALLOC | 439 EXTENT_CLEAR_DELALLOC |
431 EXTENT_CLEAR_ACCOUNTING | 440 EXTENT_CLEAR_ACCOUNTING |
432 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK); 441 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
433 ret = 0; 442
443 btrfs_end_transaction(trans, root);
434 goto free_pages_out; 444 goto free_pages_out;
435 } 445 }
446 btrfs_end_transaction(trans, root);
436 } 447 }
437 448
438 if (will_compress) { 449 if (will_compress) {
@@ -472,7 +483,8 @@ again:
472 nr_pages_ret = 0; 483 nr_pages_ret = 0;
473 484
474 /* flag the file so we don't compress in the future */ 485 /* flag the file so we don't compress in the future */
475 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 486 if (!btrfs_test_opt(root, FORCE_COMPRESS))
487 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
476 } 488 }
477 if (will_compress) { 489 if (will_compress) {
478 *num_added += 1; 490 *num_added += 1;
@@ -543,7 +555,6 @@ static noinline int submit_compressed_extents(struct inode *inode,
543 if (list_empty(&async_cow->extents)) 555 if (list_empty(&async_cow->extents))
544 return 0; 556 return 0;
545 557
546 trans = btrfs_join_transaction(root, 1);
547 558
548 while (!list_empty(&async_cow->extents)) { 559 while (!list_empty(&async_cow->extents)) {
549 async_extent = list_entry(async_cow->extents.next, 560 async_extent = list_entry(async_cow->extents.next,
@@ -590,19 +601,15 @@ retry:
590 lock_extent(io_tree, async_extent->start, 601 lock_extent(io_tree, async_extent->start,
591 async_extent->start + async_extent->ram_size - 1, 602 async_extent->start + async_extent->ram_size - 1,
592 GFP_NOFS); 603 GFP_NOFS);
593 /*
594 * here we're doing allocation and writeback of the
595 * compressed pages
596 */
597 btrfs_drop_extent_cache(inode, async_extent->start,
598 async_extent->start +
599 async_extent->ram_size - 1, 0);
600 604
605 trans = btrfs_join_transaction(root, 1);
601 ret = btrfs_reserve_extent(trans, root, 606 ret = btrfs_reserve_extent(trans, root,
602 async_extent->compressed_size, 607 async_extent->compressed_size,
603 async_extent->compressed_size, 608 async_extent->compressed_size,
604 0, alloc_hint, 609 0, alloc_hint,
605 (u64)-1, &ins, 1); 610 (u64)-1, &ins, 1);
611 btrfs_end_transaction(trans, root);
612
606 if (ret) { 613 if (ret) {
607 int i; 614 int i;
608 for (i = 0; i < async_extent->nr_pages; i++) { 615 for (i = 0; i < async_extent->nr_pages; i++) {
@@ -618,6 +625,14 @@ retry:
618 goto retry; 625 goto retry;
619 } 626 }
620 627
628 /*
629 * here we're doing allocation and writeback of the
630 * compressed pages
631 */
632 btrfs_drop_extent_cache(inode, async_extent->start,
633 async_extent->start +
634 async_extent->ram_size - 1, 0);
635
621 em = alloc_extent_map(GFP_NOFS); 636 em = alloc_extent_map(GFP_NOFS);
622 em->start = async_extent->start; 637 em->start = async_extent->start;
623 em->len = async_extent->ram_size; 638 em->len = async_extent->ram_size;
@@ -649,8 +664,6 @@ retry:
649 BTRFS_ORDERED_COMPRESSED); 664 BTRFS_ORDERED_COMPRESSED);
650 BUG_ON(ret); 665 BUG_ON(ret);
651 666
652 btrfs_end_transaction(trans, root);
653
654 /* 667 /*
655 * clear dirty, set writeback and unlock the pages. 668 * clear dirty, set writeback and unlock the pages.
656 */ 669 */
@@ -672,13 +685,11 @@ retry:
672 async_extent->nr_pages); 685 async_extent->nr_pages);
673 686
674 BUG_ON(ret); 687 BUG_ON(ret);
675 trans = btrfs_join_transaction(root, 1);
676 alloc_hint = ins.objectid + ins.offset; 688 alloc_hint = ins.objectid + ins.offset;
677 kfree(async_extent); 689 kfree(async_extent);
678 cond_resched(); 690 cond_resched();
679 } 691 }
680 692
681 btrfs_end_transaction(trans, root);
682 return 0; 693 return 0;
683} 694}
684 695
@@ -742,6 +753,7 @@ static noinline int cow_file_range(struct inode *inode,
742 EXTENT_CLEAR_DIRTY | 753 EXTENT_CLEAR_DIRTY |
743 EXTENT_SET_WRITEBACK | 754 EXTENT_SET_WRITEBACK |
744 EXTENT_END_WRITEBACK); 755 EXTENT_END_WRITEBACK);
756
745 *nr_written = *nr_written + 757 *nr_written = *nr_written +
746 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE; 758 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
747 *page_started = 1; 759 *page_started = 1;
@@ -1596,7 +1608,6 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1596 struct inode *inode, u64 file_pos, 1608 struct inode *inode, u64 file_pos,
1597 u64 disk_bytenr, u64 disk_num_bytes, 1609 u64 disk_bytenr, u64 disk_num_bytes,
1598 u64 num_bytes, u64 ram_bytes, 1610 u64 num_bytes, u64 ram_bytes,
1599 u64 locked_end,
1600 u8 compression, u8 encryption, 1611 u8 compression, u8 encryption,
1601 u16 other_encoding, int extent_type) 1612 u16 other_encoding, int extent_type)
1602{ 1613{
@@ -1622,9 +1633,8 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1622 * the caller is expected to unpin it and allow it to be merged 1633 * the caller is expected to unpin it and allow it to be merged
1623 * with the others. 1634 * with the others.
1624 */ 1635 */
1625 ret = btrfs_drop_extents(trans, root, inode, file_pos, 1636 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1626 file_pos + num_bytes, locked_end, 1637 &hint, 0);
1627 file_pos, &hint, 0);
1628 BUG_ON(ret); 1638 BUG_ON(ret);
1629 1639
1630 ins.objectid = inode->i_ino; 1640 ins.objectid = inode->i_ino;
@@ -1730,23 +1740,32 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1730 } 1740 }
1731 } 1741 }
1732 1742
1733 trans = btrfs_join_transaction(root, 1);
1734
1735 if (!ordered_extent) 1743 if (!ordered_extent)
1736 ordered_extent = btrfs_lookup_ordered_extent(inode, start); 1744 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1737 BUG_ON(!ordered_extent); 1745 BUG_ON(!ordered_extent);
1738 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) 1746 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1739 goto nocow; 1747 BUG_ON(!list_empty(&ordered_extent->list));
1748 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1749 if (!ret) {
1750 trans = btrfs_join_transaction(root, 1);
1751 ret = btrfs_update_inode(trans, root, inode);
1752 BUG_ON(ret);
1753 btrfs_end_transaction(trans, root);
1754 }
1755 goto out;
1756 }
1740 1757
1741 lock_extent(io_tree, ordered_extent->file_offset, 1758 lock_extent(io_tree, ordered_extent->file_offset,
1742 ordered_extent->file_offset + ordered_extent->len - 1, 1759 ordered_extent->file_offset + ordered_extent->len - 1,
1743 GFP_NOFS); 1760 GFP_NOFS);
1744 1761
1762 trans = btrfs_join_transaction(root, 1);
1763
1745 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) 1764 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1746 compressed = 1; 1765 compressed = 1;
1747 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) { 1766 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1748 BUG_ON(compressed); 1767 BUG_ON(compressed);
1749 ret = btrfs_mark_extent_written(trans, root, inode, 1768 ret = btrfs_mark_extent_written(trans, inode,
1750 ordered_extent->file_offset, 1769 ordered_extent->file_offset,
1751 ordered_extent->file_offset + 1770 ordered_extent->file_offset +
1752 ordered_extent->len); 1771 ordered_extent->len);
@@ -1758,8 +1777,6 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1758 ordered_extent->disk_len, 1777 ordered_extent->disk_len,
1759 ordered_extent->len, 1778 ordered_extent->len,
1760 ordered_extent->len, 1779 ordered_extent->len,
1761 ordered_extent->file_offset +
1762 ordered_extent->len,
1763 compressed, 0, 0, 1780 compressed, 0, 0,
1764 BTRFS_FILE_EXTENT_REG); 1781 BTRFS_FILE_EXTENT_REG);
1765 unpin_extent_cache(&BTRFS_I(inode)->extent_tree, 1782 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
@@ -1770,22 +1787,20 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1770 unlock_extent(io_tree, ordered_extent->file_offset, 1787 unlock_extent(io_tree, ordered_extent->file_offset,
1771 ordered_extent->file_offset + ordered_extent->len - 1, 1788 ordered_extent->file_offset + ordered_extent->len - 1,
1772 GFP_NOFS); 1789 GFP_NOFS);
1773nocow:
1774 add_pending_csums(trans, inode, ordered_extent->file_offset, 1790 add_pending_csums(trans, inode, ordered_extent->file_offset,
1775 &ordered_extent->list); 1791 &ordered_extent->list);
1776 1792
1777 mutex_lock(&BTRFS_I(inode)->extent_mutex); 1793 /* this also removes the ordered extent from the tree */
1778 btrfs_ordered_update_i_size(inode, ordered_extent); 1794 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1779 btrfs_update_inode(trans, root, inode); 1795 ret = btrfs_update_inode(trans, root, inode);
1780 btrfs_remove_ordered_extent(inode, ordered_extent); 1796 BUG_ON(ret);
1781 mutex_unlock(&BTRFS_I(inode)->extent_mutex); 1797 btrfs_end_transaction(trans, root);
1782 1798out:
1783 /* once for us */ 1799 /* once for us */
1784 btrfs_put_ordered_extent(ordered_extent); 1800 btrfs_put_ordered_extent(ordered_extent);
1785 /* once for the tree */ 1801 /* once for the tree */
1786 btrfs_put_ordered_extent(ordered_extent); 1802 btrfs_put_ordered_extent(ordered_extent);
1787 1803
1788 btrfs_end_transaction(trans, root);
1789 return 0; 1804 return 0;
1790} 1805}
1791 1806
@@ -2008,6 +2023,54 @@ zeroit:
2008 return -EIO; 2023 return -EIO;
2009} 2024}
2010 2025
2026struct delayed_iput {
2027 struct list_head list;
2028 struct inode *inode;
2029};
2030
2031void btrfs_add_delayed_iput(struct inode *inode)
2032{
2033 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2034 struct delayed_iput *delayed;
2035
2036 if (atomic_add_unless(&inode->i_count, -1, 1))
2037 return;
2038
2039 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2040 delayed->inode = inode;
2041
2042 spin_lock(&fs_info->delayed_iput_lock);
2043 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2044 spin_unlock(&fs_info->delayed_iput_lock);
2045}
2046
2047void btrfs_run_delayed_iputs(struct btrfs_root *root)
2048{
2049 LIST_HEAD(list);
2050 struct btrfs_fs_info *fs_info = root->fs_info;
2051 struct delayed_iput *delayed;
2052 int empty;
2053
2054 spin_lock(&fs_info->delayed_iput_lock);
2055 empty = list_empty(&fs_info->delayed_iputs);
2056 spin_unlock(&fs_info->delayed_iput_lock);
2057 if (empty)
2058 return;
2059
2060 down_read(&root->fs_info->cleanup_work_sem);
2061 spin_lock(&fs_info->delayed_iput_lock);
2062 list_splice_init(&fs_info->delayed_iputs, &list);
2063 spin_unlock(&fs_info->delayed_iput_lock);
2064
2065 while (!list_empty(&list)) {
2066 delayed = list_entry(list.next, struct delayed_iput, list);
2067 list_del(&delayed->list);
2068 iput(delayed->inode);
2069 kfree(delayed);
2070 }
2071 up_read(&root->fs_info->cleanup_work_sem);
2072}
2073
2011/* 2074/*
2012 * This creates an orphan entry for the given inode in case something goes 2075 * This creates an orphan entry for the given inode in case something goes
2013 * wrong in the middle of an unlink/truncate. 2076 * wrong in the middle of an unlink/truncate.
@@ -2080,16 +2143,17 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2080 struct inode *inode; 2143 struct inode *inode;
2081 int ret = 0, nr_unlink = 0, nr_truncate = 0; 2144 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2082 2145
2083 path = btrfs_alloc_path(); 2146 if (!xchg(&root->clean_orphans, 0))
2084 if (!path)
2085 return; 2147 return;
2148
2149 path = btrfs_alloc_path();
2150 BUG_ON(!path);
2086 path->reada = -1; 2151 path->reada = -1;
2087 2152
2088 key.objectid = BTRFS_ORPHAN_OBJECTID; 2153 key.objectid = BTRFS_ORPHAN_OBJECTID;
2089 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); 2154 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2090 key.offset = (u64)-1; 2155 key.offset = (u64)-1;
2091 2156
2092
2093 while (1) { 2157 while (1) {
2094 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2158 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2095 if (ret < 0) { 2159 if (ret < 0) {
@@ -2834,37 +2898,40 @@ out:
2834 * min_type is the minimum key type to truncate down to. If set to 0, this 2898 * min_type is the minimum key type to truncate down to. If set to 0, this
2835 * will kill all the items on this inode, including the INODE_ITEM_KEY. 2899 * will kill all the items on this inode, including the INODE_ITEM_KEY.
2836 */ 2900 */
2837noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans, 2901int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2838 struct btrfs_root *root, 2902 struct btrfs_root *root,
2839 struct inode *inode, 2903 struct inode *inode,
2840 u64 new_size, u32 min_type) 2904 u64 new_size, u32 min_type)
2841{ 2905{
2842 int ret;
2843 struct btrfs_path *path; 2906 struct btrfs_path *path;
2844 struct btrfs_key key;
2845 struct btrfs_key found_key;
2846 u32 found_type = (u8)-1;
2847 struct extent_buffer *leaf; 2907 struct extent_buffer *leaf;
2848 struct btrfs_file_extent_item *fi; 2908 struct btrfs_file_extent_item *fi;
2909 struct btrfs_key key;
2910 struct btrfs_key found_key;
2849 u64 extent_start = 0; 2911 u64 extent_start = 0;
2850 u64 extent_num_bytes = 0; 2912 u64 extent_num_bytes = 0;
2851 u64 extent_offset = 0; 2913 u64 extent_offset = 0;
2852 u64 item_end = 0; 2914 u64 item_end = 0;
2915 u64 mask = root->sectorsize - 1;
2916 u32 found_type = (u8)-1;
2853 int found_extent; 2917 int found_extent;
2854 int del_item; 2918 int del_item;
2855 int pending_del_nr = 0; 2919 int pending_del_nr = 0;
2856 int pending_del_slot = 0; 2920 int pending_del_slot = 0;
2857 int extent_type = -1; 2921 int extent_type = -1;
2858 int encoding; 2922 int encoding;
2859 u64 mask = root->sectorsize - 1; 2923 int ret;
2924 int err = 0;
2925
2926 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
2860 2927
2861 if (root->ref_cows) 2928 if (root->ref_cows)
2862 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); 2929 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2930
2863 path = btrfs_alloc_path(); 2931 path = btrfs_alloc_path();
2864 BUG_ON(!path); 2932 BUG_ON(!path);
2865 path->reada = -1; 2933 path->reada = -1;
2866 2934
2867 /* FIXME, add redo link to tree so we don't leak on crash */
2868 key.objectid = inode->i_ino; 2935 key.objectid = inode->i_ino;
2869 key.offset = (u64)-1; 2936 key.offset = (u64)-1;
2870 key.type = (u8)-1; 2937 key.type = (u8)-1;
@@ -2872,17 +2939,17 @@ noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2872search_again: 2939search_again:
2873 path->leave_spinning = 1; 2940 path->leave_spinning = 1;
2874 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 2941 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2875 if (ret < 0) 2942 if (ret < 0) {
2876 goto error; 2943 err = ret;
2944 goto out;
2945 }
2877 2946
2878 if (ret > 0) { 2947 if (ret > 0) {
2879 /* there are no items in the tree for us to truncate, we're 2948 /* there are no items in the tree for us to truncate, we're
2880 * done 2949 * done
2881 */ 2950 */
2882 if (path->slots[0] == 0) { 2951 if (path->slots[0] == 0)
2883 ret = 0; 2952 goto out;
2884 goto error;
2885 }
2886 path->slots[0]--; 2953 path->slots[0]--;
2887 } 2954 }
2888 2955
@@ -2917,28 +2984,17 @@ search_again:
2917 } 2984 }
2918 item_end--; 2985 item_end--;
2919 } 2986 }
2920 if (item_end < new_size) { 2987 if (found_type > min_type) {
2921 if (found_type == BTRFS_DIR_ITEM_KEY) 2988 del_item = 1;
2922 found_type = BTRFS_INODE_ITEM_KEY; 2989 } else {
2923 else if (found_type == BTRFS_EXTENT_ITEM_KEY) 2990 if (item_end < new_size)
2924 found_type = BTRFS_EXTENT_DATA_KEY;
2925 else if (found_type == BTRFS_EXTENT_DATA_KEY)
2926 found_type = BTRFS_XATTR_ITEM_KEY;
2927 else if (found_type == BTRFS_XATTR_ITEM_KEY)
2928 found_type = BTRFS_INODE_REF_KEY;
2929 else if (found_type)
2930 found_type--;
2931 else
2932 break; 2991 break;
2933 btrfs_set_key_type(&key, found_type); 2992 if (found_key.offset >= new_size)
2934 goto next; 2993 del_item = 1;
2994 else
2995 del_item = 0;
2935 } 2996 }
2936 if (found_key.offset >= new_size)
2937 del_item = 1;
2938 else
2939 del_item = 0;
2940 found_extent = 0; 2997 found_extent = 0;
2941
2942 /* FIXME, shrink the extent if the ref count is only 1 */ 2998 /* FIXME, shrink the extent if the ref count is only 1 */
2943 if (found_type != BTRFS_EXTENT_DATA_KEY) 2999 if (found_type != BTRFS_EXTENT_DATA_KEY)
2944 goto delete; 3000 goto delete;
@@ -3025,42 +3081,36 @@ delete:
3025 inode->i_ino, extent_offset); 3081 inode->i_ino, extent_offset);
3026 BUG_ON(ret); 3082 BUG_ON(ret);
3027 } 3083 }
3028next:
3029 if (path->slots[0] == 0) {
3030 if (pending_del_nr)
3031 goto del_pending;
3032 btrfs_release_path(root, path);
3033 if (found_type == BTRFS_INODE_ITEM_KEY)
3034 break;
3035 goto search_again;
3036 }
3037 3084
3038 path->slots[0]--; 3085 if (found_type == BTRFS_INODE_ITEM_KEY)
3039 if (pending_del_nr && 3086 break;
3040 path->slots[0] + 1 != pending_del_slot) { 3087
3041 struct btrfs_key debug; 3088 if (path->slots[0] == 0 ||
3042del_pending: 3089 path->slots[0] != pending_del_slot) {
3043 btrfs_item_key_to_cpu(path->nodes[0], &debug, 3090 if (root->ref_cows) {
3044 pending_del_slot); 3091 err = -EAGAIN;
3045 ret = btrfs_del_items(trans, root, path, 3092 goto out;
3046 pending_del_slot, 3093 }
3047 pending_del_nr); 3094 if (pending_del_nr) {
3048 BUG_ON(ret); 3095 ret = btrfs_del_items(trans, root, path,
3049 pending_del_nr = 0; 3096 pending_del_slot,
3097 pending_del_nr);
3098 BUG_ON(ret);
3099 pending_del_nr = 0;
3100 }
3050 btrfs_release_path(root, path); 3101 btrfs_release_path(root, path);
3051 if (found_type == BTRFS_INODE_ITEM_KEY)
3052 break;
3053 goto search_again; 3102 goto search_again;
3103 } else {
3104 path->slots[0]--;
3054 } 3105 }
3055 } 3106 }
3056 ret = 0; 3107out:
3057error:
3058 if (pending_del_nr) { 3108 if (pending_del_nr) {
3059 ret = btrfs_del_items(trans, root, path, pending_del_slot, 3109 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3060 pending_del_nr); 3110 pending_del_nr);
3061 } 3111 }
3062 btrfs_free_path(path); 3112 btrfs_free_path(path);
3063 return ret; 3113 return err;
3064} 3114}
3065 3115
3066/* 3116/*
@@ -3180,10 +3230,6 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3180 if (size <= hole_start) 3230 if (size <= hole_start)
3181 return 0; 3231 return 0;
3182 3232
3183 err = btrfs_truncate_page(inode->i_mapping, inode->i_size);
3184 if (err)
3185 return err;
3186
3187 while (1) { 3233 while (1) {
3188 struct btrfs_ordered_extent *ordered; 3234 struct btrfs_ordered_extent *ordered;
3189 btrfs_wait_ordered_range(inode, hole_start, 3235 btrfs_wait_ordered_range(inode, hole_start,
@@ -3196,9 +3242,6 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3196 btrfs_put_ordered_extent(ordered); 3242 btrfs_put_ordered_extent(ordered);
3197 } 3243 }
3198 3244
3199 trans = btrfs_start_transaction(root, 1);
3200 btrfs_set_trans_block_group(trans, inode);
3201
3202 cur_offset = hole_start; 3245 cur_offset = hole_start;
3203 while (1) { 3246 while (1) {
3204 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 3247 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
@@ -3206,40 +3249,120 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3206 BUG_ON(IS_ERR(em) || !em); 3249 BUG_ON(IS_ERR(em) || !em);
3207 last_byte = min(extent_map_end(em), block_end); 3250 last_byte = min(extent_map_end(em), block_end);
3208 last_byte = (last_byte + mask) & ~mask; 3251 last_byte = (last_byte + mask) & ~mask;
3209 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) { 3252 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3210 u64 hint_byte = 0; 3253 u64 hint_byte = 0;
3211 hole_size = last_byte - cur_offset; 3254 hole_size = last_byte - cur_offset;
3212 err = btrfs_drop_extents(trans, root, inode,
3213 cur_offset,
3214 cur_offset + hole_size,
3215 block_end,
3216 cur_offset, &hint_byte, 1);
3217 if (err)
3218 break;
3219 3255
3220 err = btrfs_reserve_metadata_space(root, 1); 3256 err = btrfs_reserve_metadata_space(root, 2);
3221 if (err) 3257 if (err)
3222 break; 3258 break;
3223 3259
3260 trans = btrfs_start_transaction(root, 1);
3261 btrfs_set_trans_block_group(trans, inode);
3262
3263 err = btrfs_drop_extents(trans, inode, cur_offset,
3264 cur_offset + hole_size,
3265 &hint_byte, 1);
3266 BUG_ON(err);
3267
3224 err = btrfs_insert_file_extent(trans, root, 3268 err = btrfs_insert_file_extent(trans, root,
3225 inode->i_ino, cur_offset, 0, 3269 inode->i_ino, cur_offset, 0,
3226 0, hole_size, 0, hole_size, 3270 0, hole_size, 0, hole_size,
3227 0, 0, 0); 3271 0, 0, 0);
3272 BUG_ON(err);
3273
3228 btrfs_drop_extent_cache(inode, hole_start, 3274 btrfs_drop_extent_cache(inode, hole_start,
3229 last_byte - 1, 0); 3275 last_byte - 1, 0);
3230 btrfs_unreserve_metadata_space(root, 1); 3276
3277 btrfs_end_transaction(trans, root);
3278 btrfs_unreserve_metadata_space(root, 2);
3231 } 3279 }
3232 free_extent_map(em); 3280 free_extent_map(em);
3233 cur_offset = last_byte; 3281 cur_offset = last_byte;
3234 if (err || cur_offset >= block_end) 3282 if (cur_offset >= block_end)
3235 break; 3283 break;
3236 } 3284 }
3237 3285
3238 btrfs_end_transaction(trans, root);
3239 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); 3286 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3240 return err; 3287 return err;
3241} 3288}
3242 3289
3290static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3291{
3292 struct btrfs_root *root = BTRFS_I(inode)->root;
3293 struct btrfs_trans_handle *trans;
3294 unsigned long nr;
3295 int ret;
3296
3297 if (attr->ia_size == inode->i_size)
3298 return 0;
3299
3300 if (attr->ia_size > inode->i_size) {
3301 unsigned long limit;
3302 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3303 if (attr->ia_size > inode->i_sb->s_maxbytes)
3304 return -EFBIG;
3305 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3306 send_sig(SIGXFSZ, current, 0);
3307 return -EFBIG;
3308 }
3309 }
3310
3311 ret = btrfs_reserve_metadata_space(root, 1);
3312 if (ret)
3313 return ret;
3314
3315 trans = btrfs_start_transaction(root, 1);
3316 btrfs_set_trans_block_group(trans, inode);
3317
3318 ret = btrfs_orphan_add(trans, inode);
3319 BUG_ON(ret);
3320
3321 nr = trans->blocks_used;
3322 btrfs_end_transaction(trans, root);
3323 btrfs_unreserve_metadata_space(root, 1);
3324 btrfs_btree_balance_dirty(root, nr);
3325
3326 if (attr->ia_size > inode->i_size) {
3327 ret = btrfs_cont_expand(inode, attr->ia_size);
3328 if (ret) {
3329 btrfs_truncate(inode);
3330 return ret;
3331 }
3332
3333 i_size_write(inode, attr->ia_size);
3334 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3335
3336 trans = btrfs_start_transaction(root, 1);
3337 btrfs_set_trans_block_group(trans, inode);
3338
3339 ret = btrfs_update_inode(trans, root, inode);
3340 BUG_ON(ret);
3341 if (inode->i_nlink > 0) {
3342 ret = btrfs_orphan_del(trans, inode);
3343 BUG_ON(ret);
3344 }
3345 nr = trans->blocks_used;
3346 btrfs_end_transaction(trans, root);
3347 btrfs_btree_balance_dirty(root, nr);
3348 return 0;
3349 }
3350
3351 /*
3352 * We're truncating a file that used to have good data down to
3353 * zero. Make sure it gets into the ordered flush list so that
3354 * any new writes get down to disk quickly.
3355 */
3356 if (attr->ia_size == 0)
3357 BTRFS_I(inode)->ordered_data_close = 1;
3358
3359 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3360 ret = vmtruncate(inode, attr->ia_size);
3361 BUG_ON(ret);
3362
3363 return 0;
3364}
3365
3243static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) 3366static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3244{ 3367{
3245 struct inode *inode = dentry->d_inode; 3368 struct inode *inode = dentry->d_inode;
@@ -3250,23 +3373,14 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3250 return err; 3373 return err;
3251 3374
3252 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3375 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3253 if (attr->ia_size > inode->i_size) { 3376 err = btrfs_setattr_size(inode, attr);
3254 err = btrfs_cont_expand(inode, attr->ia_size); 3377 if (err)
3255 if (err) 3378 return err;
3256 return err;
3257 } else if (inode->i_size > 0 &&
3258 attr->ia_size == 0) {
3259
3260 /* we're truncating a file that used to have good
3261 * data down to zero. Make sure it gets into
3262 * the ordered flush list so that any new writes
3263 * get down to disk quickly.
3264 */
3265 BTRFS_I(inode)->ordered_data_close = 1;
3266 }
3267 } 3379 }
3380 attr->ia_valid &= ~ATTR_SIZE;
3268 3381
3269 err = inode_setattr(inode, attr); 3382 if (attr->ia_valid)
3383 err = inode_setattr(inode, attr);
3270 3384
3271 if (!err && ((attr->ia_valid & ATTR_MODE))) 3385 if (!err && ((attr->ia_valid & ATTR_MODE)))
3272 err = btrfs_acl_chmod(inode); 3386 err = btrfs_acl_chmod(inode);
@@ -3287,36 +3401,43 @@ void btrfs_delete_inode(struct inode *inode)
3287 } 3401 }
3288 btrfs_wait_ordered_range(inode, 0, (u64)-1); 3402 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3289 3403
3404 if (root->fs_info->log_root_recovering) {
3405 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3406 goto no_delete;
3407 }
3408
3290 if (inode->i_nlink > 0) { 3409 if (inode->i_nlink > 0) {
3291 BUG_ON(btrfs_root_refs(&root->root_item) != 0); 3410 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3292 goto no_delete; 3411 goto no_delete;
3293 } 3412 }
3294 3413
3295 btrfs_i_size_write(inode, 0); 3414 btrfs_i_size_write(inode, 0);
3296 trans = btrfs_join_transaction(root, 1);
3297 3415
3298 btrfs_set_trans_block_group(trans, inode); 3416 while (1) {
3299 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0); 3417 trans = btrfs_start_transaction(root, 1);
3300 if (ret) { 3418 btrfs_set_trans_block_group(trans, inode);
3301 btrfs_orphan_del(NULL, inode); 3419 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3302 goto no_delete_lock;
3303 }
3304 3420
3305 btrfs_orphan_del(trans, inode); 3421 if (ret != -EAGAIN)
3422 break;
3306 3423
3307 nr = trans->blocks_used; 3424 nr = trans->blocks_used;
3308 clear_inode(inode); 3425 btrfs_end_transaction(trans, root);
3426 trans = NULL;
3427 btrfs_btree_balance_dirty(root, nr);
3428 }
3309 3429
3310 btrfs_end_transaction(trans, root); 3430 if (ret == 0) {
3311 btrfs_btree_balance_dirty(root, nr); 3431 ret = btrfs_orphan_del(trans, inode);
3312 return; 3432 BUG_ON(ret);
3433 }
3313 3434
3314no_delete_lock:
3315 nr = trans->blocks_used; 3435 nr = trans->blocks_used;
3316 btrfs_end_transaction(trans, root); 3436 btrfs_end_transaction(trans, root);
3317 btrfs_btree_balance_dirty(root, nr); 3437 btrfs_btree_balance_dirty(root, nr);
3318no_delete: 3438no_delete:
3319 clear_inode(inode); 3439 clear_inode(inode);
3440 return;
3320} 3441}
3321 3442
3322/* 3443/*
@@ -3569,7 +3690,6 @@ static noinline void init_btrfs_i(struct inode *inode)
3569 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations); 3690 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
3570 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node); 3691 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3571 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); 3692 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3572 mutex_init(&BTRFS_I(inode)->extent_mutex);
3573 mutex_init(&BTRFS_I(inode)->log_mutex); 3693 mutex_init(&BTRFS_I(inode)->log_mutex);
3574} 3694}
3575 3695
@@ -3695,6 +3815,13 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3695 } 3815 }
3696 srcu_read_unlock(&root->fs_info->subvol_srcu, index); 3816 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3697 3817
3818 if (root != sub_root) {
3819 down_read(&root->fs_info->cleanup_work_sem);
3820 if (!(inode->i_sb->s_flags & MS_RDONLY))
3821 btrfs_orphan_cleanup(sub_root);
3822 up_read(&root->fs_info->cleanup_work_sem);
3823 }
3824
3698 return inode; 3825 return inode;
3699} 3826}
3700 3827
@@ -3869,7 +3996,11 @@ skip:
3869 3996
3870 /* Reached end of directory/root. Bump pos past the last item. */ 3997 /* Reached end of directory/root. Bump pos past the last item. */
3871 if (key_type == BTRFS_DIR_INDEX_KEY) 3998 if (key_type == BTRFS_DIR_INDEX_KEY)
3872 filp->f_pos = INT_LIMIT(off_t); 3999 /*
4000 * 32-bit glibc will use getdents64, but then strtol -
4001 * so the last number we can serve is this.
4002 */
4003 filp->f_pos = 0x7fffffff;
3873 else 4004 else
3874 filp->f_pos++; 4005 filp->f_pos++;
3875nopos: 4006nopos:
@@ -4219,7 +4350,7 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4219 if (IS_ERR(inode)) 4350 if (IS_ERR(inode))
4220 goto out_unlock; 4351 goto out_unlock;
4221 4352
4222 err = btrfs_init_inode_security(inode, dir); 4353 err = btrfs_init_inode_security(trans, inode, dir);
4223 if (err) { 4354 if (err) {
4224 drop_inode = 1; 4355 drop_inode = 1;
4225 goto out_unlock; 4356 goto out_unlock;
@@ -4290,7 +4421,7 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4290 if (IS_ERR(inode)) 4421 if (IS_ERR(inode))
4291 goto out_unlock; 4422 goto out_unlock;
4292 4423
4293 err = btrfs_init_inode_security(inode, dir); 4424 err = btrfs_init_inode_security(trans, inode, dir);
4294 if (err) { 4425 if (err) {
4295 drop_inode = 1; 4426 drop_inode = 1;
4296 goto out_unlock; 4427 goto out_unlock;
@@ -4336,6 +4467,10 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4336 if (inode->i_nlink == 0) 4467 if (inode->i_nlink == 0)
4337 return -ENOENT; 4468 return -ENOENT;
4338 4469
4470 /* do not allow sys_link's with other subvols of the same device */
4471 if (root->objectid != BTRFS_I(inode)->root->objectid)
4472 return -EPERM;
4473
4339 /* 4474 /*
4340 * 1 item for inode ref 4475 * 1 item for inode ref
4341 * 2 items for dir items 4476 * 2 items for dir items
@@ -4423,7 +4558,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4423 4558
4424 drop_on_err = 1; 4559 drop_on_err = 1;
4425 4560
4426 err = btrfs_init_inode_security(inode, dir); 4561 err = btrfs_init_inode_security(trans, inode, dir);
4427 if (err) 4562 if (err)
4428 goto out_fail; 4563 goto out_fail;
4429 4564
@@ -5074,17 +5209,20 @@ static void btrfs_truncate(struct inode *inode)
5074 unsigned long nr; 5209 unsigned long nr;
5075 u64 mask = root->sectorsize - 1; 5210 u64 mask = root->sectorsize - 1;
5076 5211
5077 if (!S_ISREG(inode->i_mode)) 5212 if (!S_ISREG(inode->i_mode)) {
5078 return; 5213 WARN_ON(1);
5079 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
5080 return; 5214 return;
5215 }
5081 5216
5082 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); 5217 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5083 if (ret) 5218 if (ret)
5084 return; 5219 return;
5220
5085 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 5221 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
5222 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
5086 5223
5087 trans = btrfs_start_transaction(root, 1); 5224 trans = btrfs_start_transaction(root, 1);
5225 btrfs_set_trans_block_group(trans, inode);
5088 5226
5089 /* 5227 /*
5090 * setattr is responsible for setting the ordered_data_close flag, 5228 * setattr is responsible for setting the ordered_data_close flag,
@@ -5106,21 +5244,32 @@ static void btrfs_truncate(struct inode *inode)
5106 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close) 5244 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5107 btrfs_add_ordered_operation(trans, root, inode); 5245 btrfs_add_ordered_operation(trans, root, inode);
5108 5246
5109 btrfs_set_trans_block_group(trans, inode); 5247 while (1) {
5110 btrfs_i_size_write(inode, inode->i_size); 5248 ret = btrfs_truncate_inode_items(trans, root, inode,
5249 inode->i_size,
5250 BTRFS_EXTENT_DATA_KEY);
5251 if (ret != -EAGAIN)
5252 break;
5111 5253
5112 ret = btrfs_orphan_add(trans, inode); 5254 ret = btrfs_update_inode(trans, root, inode);
5113 if (ret) 5255 BUG_ON(ret);
5114 goto out; 5256
5115 /* FIXME, add redo link to tree so we don't leak on crash */ 5257 nr = trans->blocks_used;
5116 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 5258 btrfs_end_transaction(trans, root);
5117 BTRFS_EXTENT_DATA_KEY); 5259 btrfs_btree_balance_dirty(root, nr);
5118 btrfs_update_inode(trans, root, inode); 5260
5261 trans = btrfs_start_transaction(root, 1);
5262 btrfs_set_trans_block_group(trans, inode);
5263 }
5119 5264
5120 ret = btrfs_orphan_del(trans, inode); 5265 if (ret == 0 && inode->i_nlink > 0) {
5266 ret = btrfs_orphan_del(trans, inode);
5267 BUG_ON(ret);
5268 }
5269
5270 ret = btrfs_update_inode(trans, root, inode);
5121 BUG_ON(ret); 5271 BUG_ON(ret);
5122 5272
5123out:
5124 nr = trans->blocks_used; 5273 nr = trans->blocks_used;
5125 ret = btrfs_end_transaction_throttle(trans, root); 5274 ret = btrfs_end_transaction_throttle(trans, root);
5126 BUG_ON(ret); 5275 BUG_ON(ret);
@@ -5217,9 +5366,9 @@ void btrfs_destroy_inode(struct inode *inode)
5217 5366
5218 spin_lock(&root->list_lock); 5367 spin_lock(&root->list_lock);
5219 if (!list_empty(&BTRFS_I(inode)->i_orphan)) { 5368 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5220 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan" 5369 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5221 " list\n", inode->i_ino); 5370 inode->i_ino);
5222 dump_stack(); 5371 list_del_init(&BTRFS_I(inode)->i_orphan);
5223 } 5372 }
5224 spin_unlock(&root->list_lock); 5373 spin_unlock(&root->list_lock);
5225 5374
@@ -5476,7 +5625,7 @@ out_fail:
5476 * some fairly slow code that needs optimization. This walks the list 5625 * some fairly slow code that needs optimization. This walks the list
5477 * of all the inodes with pending delalloc and forces them to disk. 5626 * of all the inodes with pending delalloc and forces them to disk.
5478 */ 5627 */
5479int btrfs_start_delalloc_inodes(struct btrfs_root *root) 5628int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
5480{ 5629{
5481 struct list_head *head = &root->fs_info->delalloc_inodes; 5630 struct list_head *head = &root->fs_info->delalloc_inodes;
5482 struct btrfs_inode *binode; 5631 struct btrfs_inode *binode;
@@ -5495,7 +5644,10 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5495 spin_unlock(&root->fs_info->delalloc_lock); 5644 spin_unlock(&root->fs_info->delalloc_lock);
5496 if (inode) { 5645 if (inode) {
5497 filemap_flush(inode->i_mapping); 5646 filemap_flush(inode->i_mapping);
5498 iput(inode); 5647 if (delay_iput)
5648 btrfs_add_delayed_iput(inode);
5649 else
5650 iput(inode);
5499 } 5651 }
5500 cond_resched(); 5652 cond_resched();
5501 spin_lock(&root->fs_info->delalloc_lock); 5653 spin_lock(&root->fs_info->delalloc_lock);
@@ -5569,7 +5721,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5569 if (IS_ERR(inode)) 5721 if (IS_ERR(inode))
5570 goto out_unlock; 5722 goto out_unlock;
5571 5723
5572 err = btrfs_init_inode_security(inode, dir); 5724 err = btrfs_init_inode_security(trans, inode, dir);
5573 if (err) { 5725 if (err) {
5574 drop_inode = 1; 5726 drop_inode = 1;
5575 goto out_unlock; 5727 goto out_unlock;
@@ -5641,57 +5793,75 @@ out_fail:
5641 return err; 5793 return err;
5642} 5794}
5643 5795
5644static int prealloc_file_range(struct btrfs_trans_handle *trans, 5796static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5645 struct inode *inode, u64 start, u64 end, 5797 u64 alloc_hint, int mode, loff_t actual_len)
5646 u64 locked_end, u64 alloc_hint, int mode)
5647{ 5798{
5799 struct btrfs_trans_handle *trans;
5648 struct btrfs_root *root = BTRFS_I(inode)->root; 5800 struct btrfs_root *root = BTRFS_I(inode)->root;
5649 struct btrfs_key ins; 5801 struct btrfs_key ins;
5650 u64 alloc_size; 5802 u64 alloc_size;
5651 u64 cur_offset = start; 5803 u64 cur_offset = start;
5652 u64 num_bytes = end - start; 5804 u64 num_bytes = end - start;
5653 int ret = 0; 5805 int ret = 0;
5806 u64 i_size;
5654 5807
5655 while (num_bytes > 0) { 5808 while (num_bytes > 0) {
5656 alloc_size = min(num_bytes, root->fs_info->max_extent); 5809 alloc_size = min(num_bytes, root->fs_info->max_extent);
5657 5810
5658 ret = btrfs_reserve_metadata_space(root, 1); 5811 trans = btrfs_start_transaction(root, 1);
5659 if (ret)
5660 goto out;
5661 5812
5662 ret = btrfs_reserve_extent(trans, root, alloc_size, 5813 ret = btrfs_reserve_extent(trans, root, alloc_size,
5663 root->sectorsize, 0, alloc_hint, 5814 root->sectorsize, 0, alloc_hint,
5664 (u64)-1, &ins, 1); 5815 (u64)-1, &ins, 1);
5665 if (ret) { 5816 if (ret) {
5666 WARN_ON(1); 5817 WARN_ON(1);
5667 goto out; 5818 goto stop_trans;
5819 }
5820
5821 ret = btrfs_reserve_metadata_space(root, 3);
5822 if (ret) {
5823 btrfs_free_reserved_extent(root, ins.objectid,
5824 ins.offset);
5825 goto stop_trans;
5668 } 5826 }
5827
5669 ret = insert_reserved_file_extent(trans, inode, 5828 ret = insert_reserved_file_extent(trans, inode,
5670 cur_offset, ins.objectid, 5829 cur_offset, ins.objectid,
5671 ins.offset, ins.offset, 5830 ins.offset, ins.offset,
5672 ins.offset, locked_end, 5831 ins.offset, 0, 0, 0,
5673 0, 0, 0,
5674 BTRFS_FILE_EXTENT_PREALLOC); 5832 BTRFS_FILE_EXTENT_PREALLOC);
5675 BUG_ON(ret); 5833 BUG_ON(ret);
5676 btrfs_drop_extent_cache(inode, cur_offset, 5834 btrfs_drop_extent_cache(inode, cur_offset,
5677 cur_offset + ins.offset -1, 0); 5835 cur_offset + ins.offset -1, 0);
5836
5678 num_bytes -= ins.offset; 5837 num_bytes -= ins.offset;
5679 cur_offset += ins.offset; 5838 cur_offset += ins.offset;
5680 alloc_hint = ins.objectid + ins.offset; 5839 alloc_hint = ins.objectid + ins.offset;
5681 btrfs_unreserve_metadata_space(root, 1); 5840
5682 }
5683out:
5684 if (cur_offset > start) {
5685 inode->i_ctime = CURRENT_TIME; 5841 inode->i_ctime = CURRENT_TIME;
5686 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC; 5842 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
5687 if (!(mode & FALLOC_FL_KEEP_SIZE) && 5843 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5688 cur_offset > i_size_read(inode)) 5844 cur_offset > inode->i_size) {
5689 btrfs_i_size_write(inode, cur_offset); 5845 if (cur_offset > actual_len)
5846 i_size = actual_len;
5847 else
5848 i_size = cur_offset;
5849 i_size_write(inode, i_size);
5850 btrfs_ordered_update_i_size(inode, i_size, NULL);
5851 }
5852
5690 ret = btrfs_update_inode(trans, root, inode); 5853 ret = btrfs_update_inode(trans, root, inode);
5691 BUG_ON(ret); 5854 BUG_ON(ret);
5855
5856 btrfs_end_transaction(trans, root);
5857 btrfs_unreserve_metadata_space(root, 3);
5692 } 5858 }
5859 return ret;
5693 5860
5861stop_trans:
5862 btrfs_end_transaction(trans, root);
5694 return ret; 5863 return ret;
5864
5695} 5865}
5696 5866
5697static long btrfs_fallocate(struct inode *inode, int mode, 5867static long btrfs_fallocate(struct inode *inode, int mode,
@@ -5705,8 +5875,6 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5705 u64 locked_end; 5875 u64 locked_end;
5706 u64 mask = BTRFS_I(inode)->root->sectorsize - 1; 5876 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5707 struct extent_map *em; 5877 struct extent_map *em;
5708 struct btrfs_trans_handle *trans;
5709 struct btrfs_root *root;
5710 int ret; 5878 int ret;
5711 5879
5712 alloc_start = offset & ~mask; 5880 alloc_start = offset & ~mask;
@@ -5725,9 +5893,7 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5725 goto out; 5893 goto out;
5726 } 5894 }
5727 5895
5728 root = BTRFS_I(inode)->root; 5896 ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
5729
5730 ret = btrfs_check_data_free_space(root, inode,
5731 alloc_end - alloc_start); 5897 alloc_end - alloc_start);
5732 if (ret) 5898 if (ret)
5733 goto out; 5899 goto out;
@@ -5736,12 +5902,6 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5736 while (1) { 5902 while (1) {
5737 struct btrfs_ordered_extent *ordered; 5903 struct btrfs_ordered_extent *ordered;
5738 5904
5739 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5740 if (!trans) {
5741 ret = -EIO;
5742 goto out_free;
5743 }
5744
5745 /* the extent lock is ordered inside the running 5905 /* the extent lock is ordered inside the running
5746 * transaction 5906 * transaction
5747 */ 5907 */
@@ -5755,8 +5915,6 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5755 btrfs_put_ordered_extent(ordered); 5915 btrfs_put_ordered_extent(ordered);
5756 unlock_extent(&BTRFS_I(inode)->io_tree, 5916 unlock_extent(&BTRFS_I(inode)->io_tree,
5757 alloc_start, locked_end, GFP_NOFS); 5917 alloc_start, locked_end, GFP_NOFS);
5758 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5759
5760 /* 5918 /*
5761 * we can't wait on the range with the transaction 5919 * we can't wait on the range with the transaction
5762 * running or with the extent lock held 5920 * running or with the extent lock held
@@ -5777,10 +5935,12 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5777 BUG_ON(IS_ERR(em) || !em); 5935 BUG_ON(IS_ERR(em) || !em);
5778 last_byte = min(extent_map_end(em), alloc_end); 5936 last_byte = min(extent_map_end(em), alloc_end);
5779 last_byte = (last_byte + mask) & ~mask; 5937 last_byte = (last_byte + mask) & ~mask;
5780 if (em->block_start == EXTENT_MAP_HOLE) { 5938 if (em->block_start == EXTENT_MAP_HOLE ||
5781 ret = prealloc_file_range(trans, inode, cur_offset, 5939 (cur_offset >= inode->i_size &&
5782 last_byte, locked_end + 1, 5940 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5783 alloc_hint, mode); 5941 ret = prealloc_file_range(inode,
5942 cur_offset, last_byte,
5943 alloc_hint, mode, offset+len);
5784 if (ret < 0) { 5944 if (ret < 0) {
5785 free_extent_map(em); 5945 free_extent_map(em);
5786 break; 5946 break;
@@ -5799,9 +5959,8 @@ static long btrfs_fallocate(struct inode *inode, int mode,
5799 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, 5959 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5800 GFP_NOFS); 5960 GFP_NOFS);
5801 5961
5802 btrfs_end_transaction(trans, BTRFS_I(inode)->root); 5962 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5803out_free: 5963 alloc_end - alloc_start);
5804 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
5805out: 5964out:
5806 mutex_unlock(&inode->i_mutex); 5965 mutex_unlock(&inode->i_mutex);
5807 return ret; 5966 return ret;