Pull btrfs fixes from Chris Mason: "This has a few fixes from Filipe, along with a readdir fix from Dave that we've been testing for some time" * 'for-linus-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: btrfs: properly set the termination value of ctx->pos in readdir Btrfs: fix hang on extent buffer lock caused by the inode_paths ioctl Btrfs: remove no longer used function extent_read_full_page_nolock() Btrfs: fix page reading in extent_same ioctl leading to csum errors Btrfs: fix invalid page accesses in extent_same (dedup) ioctl
Unified · Split
| @@ -1406,7 +1406,8 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, · 1405 unmodified lines | |||
| 1406 | read_extent_buffer(eb, dest + bytes_left, | 1406 | read_extent_buffer(eb, dest + bytes_left, |
| 1407 | name_off, name_len); | 1407 | name_off, name_len); |
| 1408 | if (eb != eb_in) { | 1408 | if (eb != eb_in) { |
| 1409 | btrfs_tree_read_unlock_blocking(eb); | 1409 | if (!path->skip_locking) |
| 1410 | btrfs_tree_read_unlock_blocking(eb); | ||
| 1410 | free_extent_buffer(eb); | 1411 | free_extent_buffer(eb); |
| 1411 | } | 1412 | } |
| 1412 | ret = btrfs_find_item(fs_root, path, parent, 0, | 1413 | ret = btrfs_find_item(fs_root, path, parent, 0, |
| @@ -1426,9 +1427,10 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, · 13 unmodified lines | |||
| 1426 | eb = path->nodes[0]; | 1427 | eb = path->nodes[0]; |
| 1427 | /* make sure we can use eb after releasing the path */ | 1428 | /* make sure we can use eb after releasing the path */ |
| 1428 | if (eb != eb_in) { | 1429 | if (eb != eb_in) { |
| 1429 | atomic_inc(&eb->refs); | 1430 | if (!path->skip_locking) |
| 1430 | btrfs_tree_read_lock(eb); | 1431 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1431 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); | 1432 | path->nodes[0] = NULL; |
| 1433 | path->locks[0] = 0; | ||
| 1432 | } | 1434 | } |
| 1433 | btrfs_release_path(path); | 1435 | btrfs_release_path(path); |
| 1434 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); | 1436 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
| @@ -637,11 +637,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, · 636 unmodified lines | |||
| 637 | faili = nr_pages - 1; | 637 | faili = nr_pages - 1; |
| 638 | cb->nr_pages = nr_pages; | 638 | cb->nr_pages = nr_pages; |
| 639 | 639 | ||
| 640 | /* In the parent-locked case, we only locked the range we are | 640 | add_ra_bio_pages(inode, em_start + em_len, cb); |
| 641 | * interested in. In all other cases, we can opportunistically | ||
| 642 | * cache decompressed data that goes beyond the requested range. */ | ||
| 643 | if (!(bio_flags & EXTENT_BIO_PARENT_LOCKED)) | ||
| 644 | add_ra_bio_pages(inode, em_start + em_len, cb); | ||
| 645 | 641 | ||
| 646 | /* include any pages we added in add_ra-bio_pages */ | 642 | /* include any pages we added in add_ra-bio_pages */ |
| 647 | uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE; | 643 | uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE; |
| @@ -1689,7 +1689,7 @@ int btrfs_should_delete_dir_index(struct list_head *del_list, · 1688 unmodified lines | |||
| 1689 | * | 1689 | * |
| 1690 | */ | 1690 | */ |
| 1691 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, | 1691 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, |
| 1692 | struct list_head *ins_list) | 1692 | struct list_head *ins_list, bool *emitted) |
| 1693 | { | 1693 | { |
| 1694 | struct btrfs_dir_item *di; | 1694 | struct btrfs_dir_item *di; |
| 1695 | struct btrfs_delayed_item *curr, *next; | 1695 | struct btrfs_delayed_item *curr, *next; |
| @@ -1733,6 +1733,7 @@ int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, · 37 unmodified lines | |||
| 1733 | 1733 | ||
| 1734 | if (over) | 1734 | if (over) |
| 1735 | return 1; | 1735 | return 1; |
| 1736 | *emitted = true; | ||
| 1736 | } | 1737 | } |
| 1737 | return 0; | 1738 | return 0; |
| 1738 | } | 1739 | } |
| @@ -144,7 +144,7 @@ void btrfs_put_delayed_items(struct list_head *ins_list, · 143 unmodified lines | |||
| 144 | int btrfs_should_delete_dir_index(struct list_head *del_list, | 144 | int btrfs_should_delete_dir_index(struct list_head *del_list, |
| 145 | u64 index); | 145 | u64 index); |
| 146 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, | 146 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, |
| 147 | struct list_head *ins_list); | 147 | struct list_head *ins_list, bool *emitted); |
| 148 | 148 | ||
| 149 | /* for init */ | 149 | /* for init */ |
| 150 | int __init btrfs_delayed_inode_init(void); | 150 | int __init btrfs_delayed_inode_init(void); |
| @@ -2897,12 +2897,11 @@ static int __do_readpage(struct extent_io_tree *tree, · 2896 unmodified lines | |||
| 2897 | struct block_device *bdev; | 2897 | struct block_device *bdev; |
| 2898 | int ret; | 2898 | int ret; |
| 2899 | int nr = 0; | 2899 | int nr = 0; |
| 2900 | int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED; | ||
| 2901 | size_t pg_offset = 0; | 2900 | size_t pg_offset = 0; |
| 2902 | size_t iosize; | 2901 | size_t iosize; |
| 2903 | size_t disk_io_size; | 2902 | size_t disk_io_size; |
| 2904 | size_t blocksize = inode->i_sb->s_blocksize; | 2903 | size_t blocksize = inode->i_sb->s_blocksize; |
| 2905 | unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED; | 2904 | unsigned long this_bio_flag = 0; |
| 2906 | 2905 | ||
| 2907 | set_page_extent_mapped(page); | 2906 | set_page_extent_mapped(page); |
| 2908 | 2907 | ||
| @@ -2942,18 +2941,16 @@ static int __do_readpage(struct extent_io_tree *tree, · 33 unmodified lines | |||
| 2942 | kunmap_atomic(userpage); | 2941 | kunmap_atomic(userpage); |
| 2943 | set_extent_uptodate(tree, cur, cur + iosize - 1, | 2942 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
| 2944 | &cached, GFP_NOFS); | 2943 | &cached, GFP_NOFS); |
| 2945 | if (!parent_locked) | 2944 | unlock_extent_cached(tree, cur, |
| 2946 | unlock_extent_cached(tree, cur, | 2945 | cur + iosize - 1, |
| 2947 | cur + iosize - 1, | 2946 | &cached, GFP_NOFS); |
| 2948 | &cached, GFP_NOFS); | ||
| 2949 | break; | 2947 | break; |
| 2950 | } | 2948 | } |
| 2951 | em = __get_extent_map(inode, page, pg_offset, cur, | 2949 | em = __get_extent_map(inode, page, pg_offset, cur, |
| 2952 | end - cur + 1, get_extent, em_cached); | 2950 | end - cur + 1, get_extent, em_cached); |
| 2953 | if (IS_ERR_OR_NULL(em)) { | 2951 | if (IS_ERR_OR_NULL(em)) { |
| 2954 | SetPageError(page); | 2952 | SetPageError(page); |
| 2955 | if (!parent_locked) | 2953 | unlock_extent(tree, cur, end); |
| 2956 | unlock_extent(tree, cur, end); | ||
| 2957 | break; | 2954 | break; |
| 2958 | } | 2955 | } |
| 2959 | extent_offset = cur - em->start; | 2956 | extent_offset = cur - em->start; |
| @@ -3038,12 +3035,9 @@ static int __do_readpage(struct extent_io_tree *tree, · 78 unmodified lines | |||
| 3038 | 3035 | ||
| 3039 | set_extent_uptodate(tree, cur, cur + iosize - 1, | 3036 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
| 3040 | &cached, GFP_NOFS); | 3037 | &cached, GFP_NOFS); |
| 3041 | if (parent_locked) | 3038 | unlock_extent_cached(tree, cur, |
| 3042 | free_extent_state(cached); | 3039 | cur + iosize - 1, |
| 3043 | else | 3040 | &cached, GFP_NOFS); |
| 3044 | unlock_extent_cached(tree, cur, | ||
| 3045 | cur + iosize - 1, | ||
| 3046 | &cached, GFP_NOFS); | ||
| 3047 | cur = cur + iosize; | 3041 | cur = cur + iosize; |
| 3048 | pg_offset += iosize; | 3042 | pg_offset += iosize; |
| 3049 | continue; | 3043 | continue; |
| @@ -3052,8 +3046,7 @@ static int __do_readpage(struct extent_io_tree *tree, · 2 unmodified lines | |||
| 3052 | if (test_range_bit(tree, cur, cur_end, | 3046 | if (test_range_bit(tree, cur, cur_end, |
| 3053 | EXTENT_UPTODATE, 1, NULL)) { | 3047 | EXTENT_UPTODATE, 1, NULL)) { |
| 3054 | check_page_uptodate(tree, page); | 3048 | check_page_uptodate(tree, page); |
| 3055 | if (!parent_locked) | 3049 | unlock_extent(tree, cur, cur + iosize - 1); |
| 3056 | unlock_extent(tree, cur, cur + iosize - 1); | ||
| 3057 | cur = cur + iosize; | 3050 | cur = cur + iosize; |
| 3058 | pg_offset += iosize; | 3051 | pg_offset += iosize; |
| 3059 | continue; | 3052 | continue; |
| @@ -3063,8 +3056,7 @@ static int __do_readpage(struct extent_io_tree *tree, · 3 unmodified lines | |||
| 3063 | */ | 3056 | */ |
| 3064 | if (block_start == EXTENT_MAP_INLINE) { | 3057 | if (block_start == EXTENT_MAP_INLINE) { |
| 3065 | SetPageError(page); | 3058 | SetPageError(page); |
| 3066 | if (!parent_locked) | 3059 | unlock_extent(tree, cur, cur + iosize - 1); |
| 3067 | unlock_extent(tree, cur, cur + iosize - 1); | ||
| 3068 | cur = cur + iosize; | 3060 | cur = cur + iosize; |
| 3069 | pg_offset += iosize; | 3061 | pg_offset += iosize; |
| 3070 | continue; | 3062 | continue; |
| @@ -3083,8 +3075,7 @@ static int __do_readpage(struct extent_io_tree *tree, · 12 unmodified lines | |||
| 3083 | *bio_flags = this_bio_flag; | 3075 | *bio_flags = this_bio_flag; |
| 3084 | } else { | 3076 | } else { |
| 3085 | SetPageError(page); | 3077 | SetPageError(page); |
| 3086 | if (!parent_locked) | 3078 | unlock_extent(tree, cur, cur + iosize - 1); |
| 3087 | unlock_extent(tree, cur, cur + iosize - 1); | ||
| 3088 | } | 3079 | } |
| 3089 | cur = cur + iosize; | 3080 | cur = cur + iosize; |
| 3090 | pg_offset += iosize; | 3081 | pg_offset += iosize; |
| @@ -3213,20 +3204,6 @@ int extent_read_full_page(struct extent_io_tree *tree, struct page *page, · 122 unmodified lines | |||
| 3213 | return ret; | 3204 | return ret; |
| 3214 | } | 3205 | } |
| 3215 | 3206 | ||
| 3216 | int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page, | ||
| 3217 | get_extent_t *get_extent, int mirror_num) | ||
| 3218 | { | ||
| 3219 | struct bio *bio = NULL; | ||
| 3220 | unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED; | ||
| 3221 | int ret; | ||
| 3222 | |||
| 3223 | ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num, | ||
| 3224 | &bio_flags, READ, NULL); | ||
| 3225 | if (bio) | ||
| 3226 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); | ||
| 3227 | return ret; | ||
| 3228 | } | ||
| 3229 | |||
| 3230 | static noinline void update_nr_written(struct page *page, | 3207 | static noinline void update_nr_written(struct page *page, |
| 3231 | struct writeback_control *wbc, | 3208 | struct writeback_control *wbc, |
| 3232 | unsigned long nr_written) | 3209 | unsigned long nr_written) |
| @@ -29,7 +29,6 @@ · 28 unmodified lines | |||
| 29 | */ | 29 | */ |
| 30 | #define EXTENT_BIO_COMPRESSED 1 | 30 | #define EXTENT_BIO_COMPRESSED 1 |
| 31 | #define EXTENT_BIO_TREE_LOG 2 | 31 | #define EXTENT_BIO_TREE_LOG 2 |
| 32 | #define EXTENT_BIO_PARENT_LOCKED 4 | ||
| 33 | #define EXTENT_BIO_FLAG_SHIFT 16 | 32 | #define EXTENT_BIO_FLAG_SHIFT 16 |
| 34 | 33 | ||
| 35 | /* these are bit numbers for test/set bit */ | 34 | /* these are bit numbers for test/set bit */ |
| @@ -210,8 +209,6 @@ static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end) · 174 unmodified lines | |||
| 210 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end); | 209 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end); |
| 211 | int extent_read_full_page(struct extent_io_tree *tree, struct page *page, | 210 | int extent_read_full_page(struct extent_io_tree *tree, struct page *page, |
| 212 | get_extent_t *get_extent, int mirror_num); | 211 | get_extent_t *get_extent, int mirror_num); |
| 213 | int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page, | ||
| 214 | get_extent_t *get_extent, int mirror_num); | ||
| 215 | int __init extent_io_init(void); | 212 | int __init extent_io_init(void); |
| 216 | void extent_io_exit(void); | 213 | void extent_io_exit(void); |
| 217 | 214 | ||
| @@ -5717,6 +5717,7 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) · 5716 unmodified lines | |||
| 5717 | char *name_ptr; | 5717 | char *name_ptr; |
| 5718 | int name_len; | 5718 | int name_len; |
| 5719 | int is_curr = 0; /* ctx->pos points to the current index? */ | 5719 | int is_curr = 0; /* ctx->pos points to the current index? */ |
| 5720 | bool emitted; | ||
| 5720 | 5721 | ||
| 5721 | /* FIXME, use a real flag for deciding about the key type */ | 5722 | /* FIXME, use a real flag for deciding about the key type */ |
| 5722 | if (root->fs_info->tree_root == root) | 5723 | if (root->fs_info->tree_root == root) |
| @@ -5745,6 +5746,7 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) · 22 unmodified lines | |||
| 5745 | if (ret < 0) | 5746 | if (ret < 0) |
| 5746 | goto err; | 5747 | goto err; |
| 5747 | 5748 | ||
| 5749 | emitted = false; | ||
| 5748 | while (1) { | 5750 | while (1) { |
| 5749 | leaf = path->nodes[0]; | 5751 | leaf = path->nodes[0]; |
| 5750 | slot = path->slots[0]; | 5752 | slot = path->slots[0]; |
| @@ -5824,6 +5826,7 @@ skip: · 73 unmodified lines | |||
| 5824 | 5826 | ||
| 5825 | if (over) | 5827 | if (over) |
| 5826 | goto nopos; | 5828 | goto nopos; |
| 5829 | emitted = true; | ||
| 5827 | di_len = btrfs_dir_name_len(leaf, di) + | 5830 | di_len = btrfs_dir_name_len(leaf, di) + |
| 5828 | btrfs_dir_data_len(leaf, di) + sizeof(*di); | 5831 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
| 5829 | di_cur += di_len; | 5832 | di_cur += di_len; |
| @@ -5836,11 +5839,20 @@ next: · 6 unmodified lines | |||
| 5836 | if (key_type == BTRFS_DIR_INDEX_KEY) { | 5839 | if (key_type == BTRFS_DIR_INDEX_KEY) { |
| 5837 | if (is_curr) | 5840 | if (is_curr) |
| 5838 | ctx->pos++; | 5841 | ctx->pos++; |
| 5839 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); | 5842 | ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted); |
| 5840 | if (ret) | 5843 | if (ret) |
| 5841 | goto nopos; | 5844 | goto nopos; |
| 5842 | } | 5845 | } |
| 5843 | 5846 | ||
| 5847 | /* | ||
| 5848 | * If we haven't emitted any dir entry, we must not touch ctx->pos as | ||
| 5849 | * it was was set to the termination value in previous call. We assume | ||
| 5850 | * that "." and ".." were emitted if we reach this point and set the | ||
| 5851 | * termination value as well for an empty directory. | ||
| 5852 | */ | ||
| 5853 | if (ctx->pos > 2 && !emitted) | ||
| 5854 | goto nopos; | ||
| 5855 | |||
| 5844 | /* Reached end of directory/root. Bump pos past the last item. */ | 5856 | /* Reached end of directory/root. Bump pos past the last item. */ |
| 5845 | ctx->pos++; | 5857 | ctx->pos++; |
| 5846 | 5858 | ||
| @@ -2794,24 +2794,29 @@ out: · 2793 unmodified lines | |||
| 2794 | static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) | 2794 | static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) |
| 2795 | { | 2795 | { |
| 2796 | struct page *page; | 2796 | struct page *page; |
| 2797 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | ||
| 2798 | 2797 | ||
| 2799 | page = grab_cache_page(inode->i_mapping, index); | 2798 | page = grab_cache_page(inode->i_mapping, index); |
| 2800 | if (!page) | 2799 | if (!page) |
| 2801 | return NULL; | 2800 | return ERR_PTR(-ENOMEM); |
| 2802 | 2801 | ||
| 2803 | if (!PageUptodate(page)) { | 2802 | if (!PageUptodate(page)) { |
| 2804 | if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, | 2803 | int ret; |
| 2805 | 0)) | 2804 | |
| 2806 | return NULL; | 2805 | ret = btrfs_readpage(NULL, page); |
| 2806 | if (ret) | ||
| 2807 | return ERR_PTR(ret); | ||
| 2807 | lock_page(page); | 2808 | lock_page(page); |
| 2808 | if (!PageUptodate(page)) { | 2809 | if (!PageUptodate(page)) { |
| 2809 | unlock_page(page); | 2810 | unlock_page(page); |
| 2810 | page_cache_release(page); | 2811 | page_cache_release(page); |
| 2811 | return NULL; | 2812 | return ERR_PTR(-EIO); |
| 2813 | } | ||
| 2814 | if (page->mapping != inode->i_mapping) { | ||
| 2815 | unlock_page(page); | ||
| 2816 | page_cache_release(page); | ||
| 2817 | return ERR_PTR(-EAGAIN); | ||
| 2812 | } | 2818 | } |
| 2813 | } | 2819 | } |
| 2814 | unlock_page(page); | ||
| 2815 | 2820 | ||
| 2816 | return page; | 2821 | return page; |
| 2817 | } | 2822 | } |
| @@ -2823,17 +2828,31 @@ static int gather_extent_pages(struct inode *inode, struct page **pages, · 5 unmodified lines | |||
| 2823 | pgoff_t index = off >> PAGE_CACHE_SHIFT; | 2828 | pgoff_t index = off >> PAGE_CACHE_SHIFT; |
| 2824 | 2829 | ||
| 2825 | for (i = 0; i < num_pages; i++) { | 2830 | for (i = 0; i < num_pages; i++) { |
| 2831 | again: | ||
| 2826 | pages[i] = extent_same_get_page(inode, index + i); | 2832 | pages[i] = extent_same_get_page(inode, index + i); |
| 2827 | if (!pages[i]) | 2833 | if (IS_ERR(pages[i])) { |
| 2828 | return -ENOMEM; | 2834 | int err = PTR_ERR(pages[i]); |
| 2835 | |||
| 2836 | if (err == -EAGAIN) | ||
| 2837 | goto again; | ||
| 2838 | pages[i] = NULL; | ||
| 2839 | return err; | ||
| 2840 | } | ||
| 2829 | } | 2841 | } |
| 2830 | return 0; | 2842 | return 0; |
| 2831 | } | 2843 | } |
| 2832 | 2844 | ||
| 2833 | static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) | 2845 | static int lock_extent_range(struct inode *inode, u64 off, u64 len, |
| 2846 | bool retry_range_locking) | ||
| 2834 | { | 2847 | { |
| 2835 | /* do any pending delalloc/csum calc on src, one way or | 2848 | /* |
| 2836 | another, and lock file content */ | 2849 | * Do any pending delalloc/csum calculations on inode, one way or |
| 2850 | * another, and lock file content. | ||
| 2851 | * The locking order is: | ||
| 2852 | * | ||
| 2853 | * 1) pages | ||
| 2854 | * 2) range in the inode's io tree | ||
| 2855 | */ | ||
| 2837 | while (1) { | 2856 | while (1) { |
| 2838 | struct btrfs_ordered_extent *ordered; | 2857 | struct btrfs_ordered_extent *ordered; |
| 2839 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); | 2858 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
| @@ -2851,8 +2870,11 @@ static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) · 11 unmodified lines | |||
| 2851 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); | 2870 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
| 2852 | if (ordered) | 2871 | if (ordered) |
| 2853 | btrfs_put_ordered_extent(ordered); | 2872 | btrfs_put_ordered_extent(ordered); |
| 2873 | if (!retry_range_locking) | ||
| 2874 | return -EAGAIN; | ||
| 2854 | btrfs_wait_ordered_range(inode, off, len); | 2875 | btrfs_wait_ordered_range(inode, off, len); |
| 2855 | } | 2876 | } |
| 2877 | return 0; | ||
| 2856 | } | 2878 | } |
| 2857 | 2879 | ||
| 2858 | static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) | 2880 | static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) |
| @@ -2877,15 +2899,24 @@ static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, · 18 unmodified lines | |||
| 2877 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); | 2899 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); |
| 2878 | } | 2900 | } |
| 2879 | 2901 | ||
| 2880 | static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1, | 2902 | static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, |
| 2881 | struct inode *inode2, u64 loff2, u64 len) | 2903 | struct inode *inode2, u64 loff2, u64 len, |
| 2904 | bool retry_range_locking) | ||
| 2882 | { | 2905 | { |
| 2906 | int ret; | ||
| 2907 | |||
| 2883 | if (inode1 < inode2) { | 2908 | if (inode1 < inode2) { |
| 2884 | swap(inode1, inode2); | 2909 | swap(inode1, inode2); |
| 2885 | swap(loff1, loff2); | 2910 | swap(loff1, loff2); |
| 2886 | } | 2911 | } |
| 2887 | lock_extent_range(inode1, loff1, len); | 2912 | ret = lock_extent_range(inode1, loff1, len, retry_range_locking); |
| 2888 | lock_extent_range(inode2, loff2, len); | 2913 | if (ret) |
| 2914 | return ret; | ||
| 2915 | ret = lock_extent_range(inode2, loff2, len, retry_range_locking); | ||
| 2916 | if (ret) | ||
| 2917 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, | ||
| 2918 | loff1 + len - 1); | ||
| 2919 | return ret; | ||
| 2889 | } | 2920 | } |
| 2890 | 2921 | ||
| 2891 | struct cmp_pages { | 2922 | struct cmp_pages { |
| @@ -2901,11 +2932,15 @@ static void btrfs_cmp_data_free(struct cmp_pages *cmp) · 9 unmodified lines | |||
| 2901 | 2932 | ||
| 2902 | for (i = 0; i < cmp->num_pages; i++) { | 2933 | for (i = 0; i < cmp->num_pages; i++) { |
| 2903 | pg = cmp->src_pages[i]; | 2934 | pg = cmp->src_pages[i]; |
| 2904 | if (pg) | 2935 | if (pg) { |
| 2936 | unlock_page(pg); | ||
| 2905 | page_cache_release(pg); | 2937 | page_cache_release(pg); |
| 2938 | } | ||
| 2906 | pg = cmp->dst_pages[i]; | 2939 | pg = cmp->dst_pages[i]; |
| 2907 | if (pg) | 2940 | if (pg) { |
| 2941 | unlock_page(pg); | ||
| 2908 | page_cache_release(pg); | 2942 | page_cache_release(pg); |
| 2943 | } | ||
| 2909 | } | 2944 | } |
| 2910 | kfree(cmp->src_pages); | 2945 | kfree(cmp->src_pages); |
| 2911 | kfree(cmp->dst_pages); | 2946 | kfree(cmp->dst_pages); |
| @@ -2966,6 +3001,8 @@ static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, · 54 unmodified lines | |||
| 2966 | 3001 | ||
| 2967 | src_page = cmp->src_pages[i]; | 3002 | src_page = cmp->src_pages[i]; |
| 2968 | dst_page = cmp->dst_pages[i]; | 3003 | dst_page = cmp->dst_pages[i]; |
| 3004 | ASSERT(PageLocked(src_page)); | ||
| 3005 | ASSERT(PageLocked(dst_page)); | ||
| 2969 | 3006 | ||
| 2970 | addr = kmap_atomic(src_page); | 3007 | addr = kmap_atomic(src_page); |
| 2971 | dst_addr = kmap_atomic(dst_page); | 3008 | dst_addr = kmap_atomic(dst_page); |
| @@ -3078,14 +3115,46 @@ static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, · 106 unmodified lines | |||
| 3078 | goto out_unlock; | 3115 | goto out_unlock; |
| 3079 | } | 3116 | } |
| 3080 | 3117 | ||
| 3118 | again: | ||
| 3081 | ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp); | 3119 | ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp); |
| 3082 | if (ret) | 3120 | if (ret) |
| 3083 | goto out_unlock; | 3121 | goto out_unlock; |
| 3084 | 3122 | ||
| 3085 | if (same_inode) | 3123 | if (same_inode) |
| 3086 | lock_extent_range(src, same_lock_start, same_lock_len); | 3124 | ret = lock_extent_range(src, same_lock_start, same_lock_len, |
| 3125 | false); | ||
| 3087 | else | 3126 | else |
| 3088 | btrfs_double_extent_lock(src, loff, dst, dst_loff, len); | 3127 | ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, |
| 3128 | false); | ||
| 3129 | /* | ||
| 3130 | * If one of the inodes has dirty pages in the respective range or | ||
| 3131 | * ordered extents, we need to flush dellaloc and wait for all ordered | ||
| 3132 | * extents in the range. We must unlock the pages and the ranges in the | ||
| 3133 | * io trees to avoid deadlocks when flushing delalloc (requires locking | ||
| 3134 | * pages) and when waiting for ordered extents to complete (they require | ||
| 3135 | * range locking). | ||
| 3136 | */ | ||
| 3137 | if (ret == -EAGAIN) { | ||
| 3138 | /* | ||
| 3139 | * Ranges in the io trees already unlocked. Now unlock all | ||
| 3140 | * pages before waiting for all IO to complete. | ||
| 3141 | */ | ||
| 3142 | btrfs_cmp_data_free(&cmp); | ||
| 3143 | if (same_inode) { | ||
| 3144 | btrfs_wait_ordered_range(src, same_lock_start, | ||
| 3145 | same_lock_len); | ||
| 3146 | } else { | ||
| 3147 | btrfs_wait_ordered_range(src, loff, len); | ||
| 3148 | btrfs_wait_ordered_range(dst, dst_loff, len); | ||
| 3149 | } | ||
| 3150 | goto again; | ||
| 3151 | } | ||
| 3152 | ASSERT(ret == 0); | ||
| 3153 | if (WARN_ON(ret)) { | ||
| 3154 | /* ranges in the io trees already unlocked */ | ||
| 3155 | btrfs_cmp_data_free(&cmp); | ||
| 3156 | return ret; | ||
| 3157 | } | ||
| 3089 | 3158 | ||
| 3090 | /* pass original length for comparison so we stay within i_size */ | 3159 | /* pass original length for comparison so we stay within i_size */ |
| 3091 | ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp); | 3160 | ret = btrfs_cmp_data(src, loff, dst, dst_loff, olen, &cmp); |
| @@ -3795,9 +3864,15 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · 703 unmodified lines | |||
| 3795 | u64 lock_start = min_t(u64, off, destoff); | 3864 | u64 lock_start = min_t(u64, off, destoff); |
| 3796 | u64 lock_len = max_t(u64, off, destoff) + len - lock_start; | 3865 | u64 lock_len = max_t(u64, off, destoff) + len - lock_start; |
| 3797 | 3866 | ||
| 3798 | lock_extent_range(src, lock_start, lock_len); | 3867 | ret = lock_extent_range(src, lock_start, lock_len, true); |
| 3799 | } else { | 3868 | } else { |
| 3800 | btrfs_double_extent_lock(src, off, inode, destoff, len); | 3869 | ret = btrfs_double_extent_lock(src, off, inode, destoff, len, |
| 3870 | true); | ||
| 3871 | } | ||
| 3872 | ASSERT(ret == 0); | ||
| 3873 | if (WARN_ON(ret)) { | ||
| 3874 | /* ranges in the io trees already unlocked */ | ||
| 3875 | goto out_unlock; | ||
| 3801 | } | 3876 | } |
| 3802 | 3877 | ||
| 3803 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); | 3878 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); |