diff options
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r-- | fs/btrfs/inode.c | 326 |
1 files changed, 164 insertions, 162 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 6e8f416773d4..ec154f954646 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c | |||
@@ -1008,9 +1008,7 @@ static noinline void async_cow_submit(struct btrfs_work *work) | |||
1008 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> | 1008 | nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >> |
1009 | PAGE_CACHE_SHIFT; | 1009 | PAGE_CACHE_SHIFT; |
1010 | 1010 | ||
1011 | atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages); | 1011 | if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < |
1012 | |||
1013 | if (atomic_read(&root->fs_info->async_delalloc_pages) < | ||
1014 | 5 * 1024 * 1024 && | 1012 | 5 * 1024 * 1024 && |
1015 | waitqueue_active(&root->fs_info->async_submit_wait)) | 1013 | waitqueue_active(&root->fs_info->async_submit_wait)) |
1016 | wake_up(&root->fs_info->async_submit_wait); | 1014 | wake_up(&root->fs_info->async_submit_wait); |
@@ -1885,8 +1883,11 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) | |||
1885 | trans = btrfs_join_transaction_nolock(root); | 1883 | trans = btrfs_join_transaction_nolock(root); |
1886 | else | 1884 | else |
1887 | trans = btrfs_join_transaction(root); | 1885 | trans = btrfs_join_transaction(root); |
1888 | if (IS_ERR(trans)) | 1886 | if (IS_ERR(trans)) { |
1889 | return PTR_ERR(trans); | 1887 | ret = PTR_ERR(trans); |
1888 | trans = NULL; | ||
1889 | goto out; | ||
1890 | } | ||
1890 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; | 1891 | trans->block_rsv = &root->fs_info->delalloc_block_rsv; |
1891 | ret = btrfs_update_inode_fallback(trans, root, inode); | 1892 | ret = btrfs_update_inode_fallback(trans, root, inode); |
1892 | if (ret) /* -ENOMEM or corruption */ | 1893 | if (ret) /* -ENOMEM or corruption */ |
@@ -3174,7 +3175,7 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans, | |||
3174 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); | 3175 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
3175 | inode_inc_iversion(dir); | 3176 | inode_inc_iversion(dir); |
3176 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; | 3177 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
3177 | ret = btrfs_update_inode(trans, root, dir); | 3178 | ret = btrfs_update_inode_fallback(trans, root, dir); |
3178 | if (ret) | 3179 | if (ret) |
3179 | btrfs_abort_transaction(trans, root, ret); | 3180 | btrfs_abort_transaction(trans, root, ret); |
3180 | out: | 3181 | out: |
@@ -5774,18 +5775,112 @@ out: | |||
5774 | return ret; | 5775 | return ret; |
5775 | } | 5776 | } |
5776 | 5777 | ||
5778 | static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend, | ||
5779 | struct extent_state **cached_state, int writing) | ||
5780 | { | ||
5781 | struct btrfs_ordered_extent *ordered; | ||
5782 | int ret = 0; | ||
5783 | |||
5784 | while (1) { | ||
5785 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | ||
5786 | 0, cached_state); | ||
5787 | /* | ||
5788 | * We're concerned with the entire range that we're going to be | ||
5789 | * doing DIO to, so we need to make sure theres no ordered | ||
5790 | * extents in this range. | ||
5791 | */ | ||
5792 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | ||
5793 | lockend - lockstart + 1); | ||
5794 | |||
5795 | /* | ||
5796 | * We need to make sure there are no buffered pages in this | ||
5797 | * range either, we could have raced between the invalidate in | ||
5798 | * generic_file_direct_write and locking the extent. The | ||
5799 | * invalidate needs to happen so that reads after a write do not | ||
5800 | * get stale data. | ||
5801 | */ | ||
5802 | if (!ordered && (!writing || | ||
5803 | !test_range_bit(&BTRFS_I(inode)->io_tree, | ||
5804 | lockstart, lockend, EXTENT_UPTODATE, 0, | ||
5805 | *cached_state))) | ||
5806 | break; | ||
5807 | |||
5808 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | ||
5809 | cached_state, GFP_NOFS); | ||
5810 | |||
5811 | if (ordered) { | ||
5812 | btrfs_start_ordered_extent(inode, ordered, 1); | ||
5813 | btrfs_put_ordered_extent(ordered); | ||
5814 | } else { | ||
5815 | /* Screw you mmap */ | ||
5816 | ret = filemap_write_and_wait_range(inode->i_mapping, | ||
5817 | lockstart, | ||
5818 | lockend); | ||
5819 | if (ret) | ||
5820 | break; | ||
5821 | |||
5822 | /* | ||
5823 | * If we found a page that couldn't be invalidated just | ||
5824 | * fall back to buffered. | ||
5825 | */ | ||
5826 | ret = invalidate_inode_pages2_range(inode->i_mapping, | ||
5827 | lockstart >> PAGE_CACHE_SHIFT, | ||
5828 | lockend >> PAGE_CACHE_SHIFT); | ||
5829 | if (ret) | ||
5830 | break; | ||
5831 | } | ||
5832 | |||
5833 | cond_resched(); | ||
5834 | } | ||
5835 | |||
5836 | return ret; | ||
5837 | } | ||
5838 | |||
5777 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, | 5839 | static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, |
5778 | struct buffer_head *bh_result, int create) | 5840 | struct buffer_head *bh_result, int create) |
5779 | { | 5841 | { |
5780 | struct extent_map *em; | 5842 | struct extent_map *em; |
5781 | struct btrfs_root *root = BTRFS_I(inode)->root; | 5843 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5844 | struct extent_state *cached_state = NULL; | ||
5782 | u64 start = iblock << inode->i_blkbits; | 5845 | u64 start = iblock << inode->i_blkbits; |
5846 | u64 lockstart, lockend; | ||
5783 | u64 len = bh_result->b_size; | 5847 | u64 len = bh_result->b_size; |
5784 | struct btrfs_trans_handle *trans; | 5848 | struct btrfs_trans_handle *trans; |
5849 | int unlock_bits = EXTENT_LOCKED; | ||
5850 | int ret; | ||
5851 | |||
5852 | if (create) { | ||
5853 | ret = btrfs_delalloc_reserve_space(inode, len); | ||
5854 | if (ret) | ||
5855 | return ret; | ||
5856 | unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; | ||
5857 | } else { | ||
5858 | len = min_t(u64, len, root->sectorsize); | ||
5859 | } | ||
5860 | |||
5861 | lockstart = start; | ||
5862 | lockend = start + len - 1; | ||
5863 | |||
5864 | /* | ||
5865 | * If this errors out it's because we couldn't invalidate pagecache for | ||
5866 | * this range and we need to fallback to buffered. | ||
5867 | */ | ||
5868 | if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create)) | ||
5869 | return -ENOTBLK; | ||
5870 | |||
5871 | if (create) { | ||
5872 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, | ||
5873 | lockend, EXTENT_DELALLOC, NULL, | ||
5874 | &cached_state, GFP_NOFS); | ||
5875 | if (ret) | ||
5876 | goto unlock_err; | ||
5877 | } | ||
5785 | 5878 | ||
5786 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); | 5879 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
5787 | if (IS_ERR(em)) | 5880 | if (IS_ERR(em)) { |
5788 | return PTR_ERR(em); | 5881 | ret = PTR_ERR(em); |
5882 | goto unlock_err; | ||
5883 | } | ||
5789 | 5884 | ||
5790 | /* | 5885 | /* |
5791 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered | 5886 | * Ok for INLINE and COMPRESSED extents we need to fallback on buffered |
@@ -5804,17 +5899,16 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, | |||
5804 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || | 5899 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) || |
5805 | em->block_start == EXTENT_MAP_INLINE) { | 5900 | em->block_start == EXTENT_MAP_INLINE) { |
5806 | free_extent_map(em); | 5901 | free_extent_map(em); |
5807 | return -ENOTBLK; | 5902 | ret = -ENOTBLK; |
5903 | goto unlock_err; | ||
5808 | } | 5904 | } |
5809 | 5905 | ||
5810 | /* Just a good old fashioned hole, return */ | 5906 | /* Just a good old fashioned hole, return */ |
5811 | if (!create && (em->block_start == EXTENT_MAP_HOLE || | 5907 | if (!create && (em->block_start == EXTENT_MAP_HOLE || |
5812 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { | 5908 | test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) { |
5813 | free_extent_map(em); | 5909 | free_extent_map(em); |
5814 | /* DIO will do one hole at a time, so just unlock a sector */ | 5910 | ret = 0; |
5815 | unlock_extent(&BTRFS_I(inode)->io_tree, start, | 5911 | goto unlock_err; |
5816 | start + root->sectorsize - 1); | ||
5817 | return 0; | ||
5818 | } | 5912 | } |
5819 | 5913 | ||
5820 | /* | 5914 | /* |
@@ -5827,8 +5921,9 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, | |||
5827 | * | 5921 | * |
5828 | */ | 5922 | */ |
5829 | if (!create) { | 5923 | if (!create) { |
5830 | len = em->len - (start - em->start); | 5924 | len = min(len, em->len - (start - em->start)); |
5831 | goto map; | 5925 | lockstart = start + len; |
5926 | goto unlock; | ||
5832 | } | 5927 | } |
5833 | 5928 | ||
5834 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || | 5929 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) || |
@@ -5860,7 +5955,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, | |||
5860 | btrfs_end_transaction(trans, root); | 5955 | btrfs_end_transaction(trans, root); |
5861 | if (ret) { | 5956 | if (ret) { |
5862 | free_extent_map(em); | 5957 | free_extent_map(em); |
5863 | return ret; | 5958 | goto unlock_err; |
5864 | } | 5959 | } |
5865 | goto unlock; | 5960 | goto unlock; |
5866 | } | 5961 | } |
@@ -5873,14 +5968,12 @@ must_cow: | |||
5873 | */ | 5968 | */ |
5874 | len = bh_result->b_size; | 5969 | len = bh_result->b_size; |
5875 | em = btrfs_new_extent_direct(inode, em, start, len); | 5970 | em = btrfs_new_extent_direct(inode, em, start, len); |
5876 | if (IS_ERR(em)) | 5971 | if (IS_ERR(em)) { |
5877 | return PTR_ERR(em); | 5972 | ret = PTR_ERR(em); |
5973 | goto unlock_err; | ||
5974 | } | ||
5878 | len = min(len, em->len - (start - em->start)); | 5975 | len = min(len, em->len - (start - em->start)); |
5879 | unlock: | 5976 | unlock: |
5880 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1, | ||
5881 | EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1, | ||
5882 | 0, NULL, GFP_NOFS); | ||
5883 | map: | ||
5884 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> | 5977 | bh_result->b_blocknr = (em->block_start + (start - em->start)) >> |
5885 | inode->i_blkbits; | 5978 | inode->i_blkbits; |
5886 | bh_result->b_size = len; | 5979 | bh_result->b_size = len; |
@@ -5898,9 +5991,44 @@ map: | |||
5898 | i_size_write(inode, start + len); | 5991 | i_size_write(inode, start + len); |
5899 | } | 5992 | } |
5900 | 5993 | ||
5994 | /* | ||
5995 | * In the case of write we need to clear and unlock the entire range, | ||
5996 | * in the case of read we need to unlock only the end area that we | ||
5997 | * aren't using if there is any left over space. | ||
5998 | */ | ||
5999 | if (lockstart < lockend) { | ||
6000 | if (create && len < lockend - lockstart) { | ||
6001 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, | ||
6002 | lockstart + len - 1, unlock_bits, 1, 0, | ||
6003 | &cached_state, GFP_NOFS); | ||
6004 | /* | ||
6005 | * Beside unlock, we also need to cleanup reserved space | ||
6006 | * for the left range by attaching EXTENT_DO_ACCOUNTING. | ||
6007 | */ | ||
6008 | clear_extent_bit(&BTRFS_I(inode)->io_tree, | ||
6009 | lockstart + len, lockend, | ||
6010 | unlock_bits | EXTENT_DO_ACCOUNTING, | ||
6011 | 1, 0, NULL, GFP_NOFS); | ||
6012 | } else { | ||
6013 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, | ||
6014 | lockend, unlock_bits, 1, 0, | ||
6015 | &cached_state, GFP_NOFS); | ||
6016 | } | ||
6017 | } else { | ||
6018 | free_extent_state(cached_state); | ||
6019 | } | ||
6020 | |||
5901 | free_extent_map(em); | 6021 | free_extent_map(em); |
5902 | 6022 | ||
5903 | return 0; | 6023 | return 0; |
6024 | |||
6025 | unlock_err: | ||
6026 | if (create) | ||
6027 | unlock_bits |= EXTENT_DO_ACCOUNTING; | ||
6028 | |||
6029 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, | ||
6030 | unlock_bits, 1, 0, &cached_state, GFP_NOFS); | ||
6031 | return ret; | ||
5904 | } | 6032 | } |
5905 | 6033 | ||
5906 | struct btrfs_dio_private { | 6034 | struct btrfs_dio_private { |
@@ -5908,7 +6036,6 @@ struct btrfs_dio_private { | |||
5908 | u64 logical_offset; | 6036 | u64 logical_offset; |
5909 | u64 disk_bytenr; | 6037 | u64 disk_bytenr; |
5910 | u64 bytes; | 6038 | u64 bytes; |
5911 | u32 *csums; | ||
5912 | void *private; | 6039 | void *private; |
5913 | 6040 | ||
5914 | /* number of bios pending for this dio */ | 6041 | /* number of bios pending for this dio */ |
@@ -5928,7 +6055,6 @@ static void btrfs_endio_direct_read(struct bio *bio, int err) | |||
5928 | struct inode *inode = dip->inode; | 6055 | struct inode *inode = dip->inode; |
5929 | struct btrfs_root *root = BTRFS_I(inode)->root; | 6056 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5930 | u64 start; | 6057 | u64 start; |
5931 | u32 *private = dip->csums; | ||
5932 | 6058 | ||
5933 | start = dip->logical_offset; | 6059 | start = dip->logical_offset; |
5934 | do { | 6060 | do { |
@@ -5936,8 +6062,12 @@ static void btrfs_endio_direct_read(struct bio *bio, int err) | |||
5936 | struct page *page = bvec->bv_page; | 6062 | struct page *page = bvec->bv_page; |
5937 | char *kaddr; | 6063 | char *kaddr; |
5938 | u32 csum = ~(u32)0; | 6064 | u32 csum = ~(u32)0; |
6065 | u64 private = ~(u32)0; | ||
5939 | unsigned long flags; | 6066 | unsigned long flags; |
5940 | 6067 | ||
6068 | if (get_state_private(&BTRFS_I(inode)->io_tree, | ||
6069 | start, &private)) | ||
6070 | goto failed; | ||
5941 | local_irq_save(flags); | 6071 | local_irq_save(flags); |
5942 | kaddr = kmap_atomic(page); | 6072 | kaddr = kmap_atomic(page); |
5943 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, | 6073 | csum = btrfs_csum_data(root, kaddr + bvec->bv_offset, |
@@ -5947,18 +6077,18 @@ static void btrfs_endio_direct_read(struct bio *bio, int err) | |||
5947 | local_irq_restore(flags); | 6077 | local_irq_restore(flags); |
5948 | 6078 | ||
5949 | flush_dcache_page(bvec->bv_page); | 6079 | flush_dcache_page(bvec->bv_page); |
5950 | if (csum != *private) { | 6080 | if (csum != private) { |
6081 | failed: | ||
5951 | printk(KERN_ERR "btrfs csum failed ino %llu off" | 6082 | printk(KERN_ERR "btrfs csum failed ino %llu off" |
5952 | " %llu csum %u private %u\n", | 6083 | " %llu csum %u private %u\n", |
5953 | (unsigned long long)btrfs_ino(inode), | 6084 | (unsigned long long)btrfs_ino(inode), |
5954 | (unsigned long long)start, | 6085 | (unsigned long long)start, |
5955 | csum, *private); | 6086 | csum, (unsigned)private); |
5956 | err = -EIO; | 6087 | err = -EIO; |
5957 | } | 6088 | } |
5958 | } | 6089 | } |
5959 | 6090 | ||
5960 | start += bvec->bv_len; | 6091 | start += bvec->bv_len; |
5961 | private++; | ||
5962 | bvec++; | 6092 | bvec++; |
5963 | } while (bvec <= bvec_end); | 6093 | } while (bvec <= bvec_end); |
5964 | 6094 | ||
@@ -5966,7 +6096,6 @@ static void btrfs_endio_direct_read(struct bio *bio, int err) | |||
5966 | dip->logical_offset + dip->bytes - 1); | 6096 | dip->logical_offset + dip->bytes - 1); |
5967 | bio->bi_private = dip->private; | 6097 | bio->bi_private = dip->private; |
5968 | 6098 | ||
5969 | kfree(dip->csums); | ||
5970 | kfree(dip); | 6099 | kfree(dip); |
5971 | 6100 | ||
5972 | /* If we had a csum failure make sure to clear the uptodate flag */ | 6101 | /* If we had a csum failure make sure to clear the uptodate flag */ |
@@ -6072,7 +6201,7 @@ static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev, | |||
6072 | 6201 | ||
6073 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, | 6202 | static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, |
6074 | int rw, u64 file_offset, int skip_sum, | 6203 | int rw, u64 file_offset, int skip_sum, |
6075 | u32 *csums, int async_submit) | 6204 | int async_submit) |
6076 | { | 6205 | { |
6077 | int write = rw & REQ_WRITE; | 6206 | int write = rw & REQ_WRITE; |
6078 | struct btrfs_root *root = BTRFS_I(inode)->root; | 6207 | struct btrfs_root *root = BTRFS_I(inode)->root; |
@@ -6105,8 +6234,7 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, | |||
6105 | if (ret) | 6234 | if (ret) |
6106 | goto err; | 6235 | goto err; |
6107 | } else if (!skip_sum) { | 6236 | } else if (!skip_sum) { |
6108 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, | 6237 | ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset); |
6109 | file_offset, csums); | ||
6110 | if (ret) | 6238 | if (ret) |
6111 | goto err; | 6239 | goto err; |
6112 | } | 6240 | } |
@@ -6132,10 +6260,8 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |||
6132 | u64 submit_len = 0; | 6260 | u64 submit_len = 0; |
6133 | u64 map_length; | 6261 | u64 map_length; |
6134 | int nr_pages = 0; | 6262 | int nr_pages = 0; |
6135 | u32 *csums = dip->csums; | ||
6136 | int ret = 0; | 6263 | int ret = 0; |
6137 | int async_submit = 0; | 6264 | int async_submit = 0; |
6138 | int write = rw & REQ_WRITE; | ||
6139 | 6265 | ||
6140 | map_length = orig_bio->bi_size; | 6266 | map_length = orig_bio->bi_size; |
6141 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, | 6267 | ret = btrfs_map_block(map_tree, READ, start_sector << 9, |
@@ -6171,16 +6297,13 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |||
6171 | atomic_inc(&dip->pending_bios); | 6297 | atomic_inc(&dip->pending_bios); |
6172 | ret = __btrfs_submit_dio_bio(bio, inode, rw, | 6298 | ret = __btrfs_submit_dio_bio(bio, inode, rw, |
6173 | file_offset, skip_sum, | 6299 | file_offset, skip_sum, |
6174 | csums, async_submit); | 6300 | async_submit); |
6175 | if (ret) { | 6301 | if (ret) { |
6176 | bio_put(bio); | 6302 | bio_put(bio); |
6177 | atomic_dec(&dip->pending_bios); | 6303 | atomic_dec(&dip->pending_bios); |
6178 | goto out_err; | 6304 | goto out_err; |
6179 | } | 6305 | } |
6180 | 6306 | ||
6181 | /* Write's use the ordered csums */ | ||
6182 | if (!write && !skip_sum) | ||
6183 | csums = csums + nr_pages; | ||
6184 | start_sector += submit_len >> 9; | 6307 | start_sector += submit_len >> 9; |
6185 | file_offset += submit_len; | 6308 | file_offset += submit_len; |
6186 | 6309 | ||
@@ -6210,7 +6333,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip, | |||
6210 | 6333 | ||
6211 | submit: | 6334 | submit: |
6212 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, | 6335 | ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum, |
6213 | csums, async_submit); | 6336 | async_submit); |
6214 | if (!ret) | 6337 | if (!ret) |
6215 | return 0; | 6338 | return 0; |
6216 | 6339 | ||
@@ -6246,17 +6369,6 @@ static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode, | |||
6246 | ret = -ENOMEM; | 6369 | ret = -ENOMEM; |
6247 | goto free_ordered; | 6370 | goto free_ordered; |
6248 | } | 6371 | } |
6249 | dip->csums = NULL; | ||
6250 | |||
6251 | /* Write's use the ordered csum stuff, so we don't need dip->csums */ | ||
6252 | if (!write && !skip_sum) { | ||
6253 | dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); | ||
6254 | if (!dip->csums) { | ||
6255 | kfree(dip); | ||
6256 | ret = -ENOMEM; | ||
6257 | goto free_ordered; | ||
6258 | } | ||
6259 | } | ||
6260 | 6372 | ||
6261 | dip->private = bio->bi_private; | 6373 | dip->private = bio->bi_private; |
6262 | dip->inode = inode; | 6374 | dip->inode = inode; |
@@ -6341,132 +6453,22 @@ static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *io | |||
6341 | out: | 6453 | out: |
6342 | return retval; | 6454 | return retval; |
6343 | } | 6455 | } |
6456 | |||
6344 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, | 6457 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, |
6345 | const struct iovec *iov, loff_t offset, | 6458 | const struct iovec *iov, loff_t offset, |
6346 | unsigned long nr_segs) | 6459 | unsigned long nr_segs) |
6347 | { | 6460 | { |
6348 | struct file *file = iocb->ki_filp; | 6461 | struct file *file = iocb->ki_filp; |
6349 | struct inode *inode = file->f_mapping->host; | 6462 | struct inode *inode = file->f_mapping->host; |
6350 | struct btrfs_ordered_extent *ordered; | ||
6351 | struct extent_state *cached_state = NULL; | ||
6352 | u64 lockstart, lockend; | ||
6353 | ssize_t ret; | ||
6354 | int writing = rw & WRITE; | ||
6355 | int write_bits = 0; | ||
6356 | size_t count = iov_length(iov, nr_segs); | ||
6357 | 6463 | ||
6358 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, | 6464 | if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov, |
6359 | offset, nr_segs)) { | 6465 | offset, nr_segs)) |
6360 | return 0; | 6466 | return 0; |
6361 | } | ||
6362 | |||
6363 | lockstart = offset; | ||
6364 | lockend = offset + count - 1; | ||
6365 | |||
6366 | if (writing) { | ||
6367 | ret = btrfs_delalloc_reserve_space(inode, count); | ||
6368 | if (ret) | ||
6369 | goto out; | ||
6370 | } | ||
6371 | |||
6372 | while (1) { | ||
6373 | lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, | ||
6374 | 0, &cached_state); | ||
6375 | /* | ||
6376 | * We're concerned with the entire range that we're going to be | ||
6377 | * doing DIO to, so we need to make sure theres no ordered | ||
6378 | * extents in this range. | ||
6379 | */ | ||
6380 | ordered = btrfs_lookup_ordered_range(inode, lockstart, | ||
6381 | lockend - lockstart + 1); | ||
6382 | |||
6383 | /* | ||
6384 | * We need to make sure there are no buffered pages in this | ||
6385 | * range either, we could have raced between the invalidate in | ||
6386 | * generic_file_direct_write and locking the extent. The | ||
6387 | * invalidate needs to happen so that reads after a write do not | ||
6388 | * get stale data. | ||
6389 | */ | ||
6390 | if (!ordered && (!writing || | ||
6391 | !test_range_bit(&BTRFS_I(inode)->io_tree, | ||
6392 | lockstart, lockend, EXTENT_UPTODATE, 0, | ||
6393 | cached_state))) | ||
6394 | break; | ||
6395 | |||
6396 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, | ||
6397 | &cached_state, GFP_NOFS); | ||
6398 | |||
6399 | if (ordered) { | ||
6400 | btrfs_start_ordered_extent(inode, ordered, 1); | ||
6401 | btrfs_put_ordered_extent(ordered); | ||
6402 | } else { | ||
6403 | /* Screw you mmap */ | ||
6404 | ret = filemap_write_and_wait_range(file->f_mapping, | ||
6405 | lockstart, | ||
6406 | lockend); | ||
6407 | if (ret) | ||
6408 | goto out; | ||
6409 | |||
6410 | /* | ||
6411 | * If we found a page that couldn't be invalidated just | ||
6412 | * fall back to buffered. | ||
6413 | */ | ||
6414 | ret = invalidate_inode_pages2_range(file->f_mapping, | ||
6415 | lockstart >> PAGE_CACHE_SHIFT, | ||
6416 | lockend >> PAGE_CACHE_SHIFT); | ||
6417 | if (ret) { | ||
6418 | if (ret == -EBUSY) | ||
6419 | ret = 0; | ||
6420 | goto out; | ||
6421 | } | ||
6422 | } | ||
6423 | |||
6424 | cond_resched(); | ||
6425 | } | ||
6426 | 6467 | ||
6427 | /* | 6468 | return __blockdev_direct_IO(rw, iocb, inode, |
6428 | * we don't use btrfs_set_extent_delalloc because we don't want | ||
6429 | * the dirty or uptodate bits | ||
6430 | */ | ||
6431 | if (writing) { | ||
6432 | write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING; | ||
6433 | ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend, | ||
6434 | EXTENT_DELALLOC, NULL, &cached_state, | ||
6435 | GFP_NOFS); | ||
6436 | if (ret) { | ||
6437 | clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, | ||
6438 | lockend, EXTENT_LOCKED | write_bits, | ||
6439 | 1, 0, &cached_state, GFP_NOFS); | ||
6440 | goto out; | ||
6441 | } | ||
6442 | } | ||
6443 | |||
6444 | free_extent_state(cached_state); | ||
6445 | cached_state = NULL; | ||
6446 | |||
6447 | ret = __blockdev_direct_IO(rw, iocb, inode, | ||
6448 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, | 6469 | BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, |
6449 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, | 6470 | iov, offset, nr_segs, btrfs_get_blocks_direct, NULL, |
6450 | btrfs_submit_direct, 0); | 6471 | btrfs_submit_direct, 0); |
6451 | |||
6452 | if (ret < 0 && ret != -EIOCBQUEUED) { | ||
6453 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset, | ||
6454 | offset + iov_length(iov, nr_segs) - 1, | ||
6455 | EXTENT_LOCKED | write_bits, 1, 0, | ||
6456 | &cached_state, GFP_NOFS); | ||
6457 | } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) { | ||
6458 | /* | ||
6459 | * We're falling back to buffered, unlock the section we didn't | ||
6460 | * do IO on. | ||
6461 | */ | ||
6462 | clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret, | ||
6463 | offset + iov_length(iov, nr_segs) - 1, | ||
6464 | EXTENT_LOCKED | write_bits, 1, 0, | ||
6465 | &cached_state, GFP_NOFS); | ||
6466 | } | ||
6467 | out: | ||
6468 | free_extent_state(cached_state); | ||
6469 | return ret; | ||
6470 | } | 6472 | } |
6471 | 6473 | ||
6472 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | 6474 | static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |