History

Merge branch 'for-linus-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs

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

Linus Torvalds committed 10 years ago

Commit 27c9d772 parents dfc8528 bc4ef758 files · +131 −71Browse files

Unified · Split

8 files
  1. fs/btrfs/backref.c +6 −4
  2. fs/btrfs/compression.c +1 −5
  3. fs/btrfs/delayed-inode.c +2 −1
  4. fs/btrfs/delayed-inode.h +1 −1
  5. fs/btrfs/extent_io.c +11 −34
  6. fs/btrfs/extent_io.h +0 −3
  7. fs/btrfs/inode.c +13 −1
  8. fs/btrfs/ioctl.c +97 −22
fs/btrfs/backref.c+6−4
@@ -1406,7 +1406,8 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, · 1405 unmodified lines
14061406 read_extent_buffer(eb, dest + bytes_left,
14071407 name_off, name_len);
14081408 if (eb != eb_in) {
1409− btrfs_tree_read_unlock_blocking(eb);
1409+ if (!path->skip_locking)
1410+ btrfs_tree_read_unlock_blocking(eb);
14101411 free_extent_buffer(eb);
14111412 }
14121413 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
14261427 eb = path->nodes[0];
14271428 /* make sure we can use eb after releasing the path */
14281429 if (eb != eb_in) {
1429− atomic_inc(&eb->refs);
1430− btrfs_tree_read_lock(eb);
1431− btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1430+ if (!path->skip_locking)
1431+ btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1432+ path->nodes[0] = NULL;
1433+ path->locks[0] = 0;
14321434 }
14331435 btrfs_release_path(path);
14341436 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
fs/btrfs/compression.c+1−5
@@ -637,11 +637,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, · 636 unmodified lines
637637 faili = nr_pages - 1;
638638 cb->nr_pages = nr_pages;
639639
640− /* In the parent-locked case, we only locked the range we are
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);
640+ add_ra_bio_pages(inode, em_start + em_len, cb);
645641
646642 /* include any pages we added in add_ra-bio_pages */
647643 uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
fs/btrfs/delayed-inode.c+2−1
@@ -1689,7 +1689,7 @@ int btrfs_should_delete_dir_index(struct list_head *del_list, · 1688 unmodified lines
16891689 *
16901690 */
16911691 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1692− struct list_head *ins_list)
1692+ struct list_head *ins_list, bool *emitted)
16931693 {
16941694 struct btrfs_dir_item *di;
16951695 struct btrfs_delayed_item *curr, *next;
@@ -1733,6 +1733,7 @@ int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, · 37 unmodified lines
17331733
17341734 if (over)
17351735 return 1;
1736+ *emitted = true;
17361737 }
17371738 return 0;
17381739 }
fs/btrfs/delayed-inode.h+1−1
@@ -144,7 +144,7 @@ void btrfs_put_delayed_items(struct list_head *ins_list, · 143 unmodified lines
144144 int btrfs_should_delete_dir_index(struct list_head *del_list,
145145 u64 index);
146146 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
147− struct list_head *ins_list);
147+ struct list_head *ins_list, bool *emitted);
148148
149149 /* for init */
150150 int __init btrfs_delayed_inode_init(void);
fs/btrfs/extent_io.c+11−34
@@ -2897,12 +2897,11 @@ static int __do_readpage(struct extent_io_tree *tree, · 2896 unmodified lines
28972897 struct block_device *bdev;
28982898 int ret;
28992899 int nr = 0;
2900− int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
29012900 size_t pg_offset = 0;
29022901 size_t iosize;
29032902 size_t disk_io_size;
29042903 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;
29062905
29072906 set_page_extent_mapped(page);
29082907
@@ -2942,18 +2941,16 @@ static int __do_readpage(struct extent_io_tree *tree, · 33 unmodified lines
29422941 kunmap_atomic(userpage);
29432942 set_extent_uptodate(tree, cur, cur + iosize - 1,
29442943 &cached, GFP_NOFS);
2945− if (!parent_locked)
2946− unlock_extent_cached(tree, cur,
2947− cur + iosize - 1,
2948− &cached, GFP_NOFS);
2944+ unlock_extent_cached(tree, cur,
2945+ cur + iosize - 1,
2946+ &cached, GFP_NOFS);
29492947 break;
29502948 }
29512949 em = __get_extent_map(inode, page, pg_offset, cur,
29522950 end - cur + 1, get_extent, em_cached);
29532951 if (IS_ERR_OR_NULL(em)) {
29542952 SetPageError(page);
2955− if (!parent_locked)
2956− unlock_extent(tree, cur, end);
2953+ unlock_extent(tree, cur, end);
29572954 break;
29582955 }
29592956 extent_offset = cur - em->start;
@@ -3038,12 +3035,9 @@ static int __do_readpage(struct extent_io_tree *tree, · 78 unmodified lines
30383035
30393036 set_extent_uptodate(tree, cur, cur + iosize - 1,
30403037 &cached, GFP_NOFS);
3041− if (parent_locked)
3042− free_extent_state(cached);
3043− else
3044− unlock_extent_cached(tree, cur,
3045− cur + iosize - 1,
3046− &cached, GFP_NOFS);
3038+ unlock_extent_cached(tree, cur,
3039+ cur + iosize - 1,
3040+ &cached, GFP_NOFS);
30473041 cur = cur + iosize;
30483042 pg_offset += iosize;
30493043 continue;
@@ -3052,8 +3046,7 @@ static int __do_readpage(struct extent_io_tree *tree, · 2 unmodified lines
30523046 if (test_range_bit(tree, cur, cur_end,
30533047 EXTENT_UPTODATE, 1, NULL)) {
30543048 check_page_uptodate(tree, page);
3055− if (!parent_locked)
3056− unlock_extent(tree, cur, cur + iosize - 1);
3049+ unlock_extent(tree, cur, cur + iosize - 1);
30573050 cur = cur + iosize;
30583051 pg_offset += iosize;
30593052 continue;
@@ -3063,8 +3056,7 @@ static int __do_readpage(struct extent_io_tree *tree, · 3 unmodified lines
30633056 */
30643057 if (block_start == EXTENT_MAP_INLINE) {
30653058 SetPageError(page);
3066− if (!parent_locked)
3067− unlock_extent(tree, cur, cur + iosize - 1);
3059+ unlock_extent(tree, cur, cur + iosize - 1);
30683060 cur = cur + iosize;
30693061 pg_offset += iosize;
30703062 continue;
@@ -3083,8 +3075,7 @@ static int __do_readpage(struct extent_io_tree *tree, · 12 unmodified lines
30833075 *bio_flags = this_bio_flag;
30843076 } else {
30853077 SetPageError(page);
3086− if (!parent_locked)
3087− unlock_extent(tree, cur, cur + iosize - 1);
3078+ unlock_extent(tree, cur, cur + iosize - 1);
30883079 }
30893080 cur = cur + iosize;
30903081 pg_offset += iosize;
@@ -3213,20 +3204,6 @@ int extent_read_full_page(struct extent_io_tree *tree, struct page *page, · 122 unmodified lines
32133204 return ret;
32143205 }
32153206
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−
32303207 static noinline void update_nr_written(struct page *page,
32313208 struct writeback_control *wbc,
32323209 unsigned long nr_written)
fs/btrfs/extent_io.h+0−3
@@ -29,7 +29,6 @@ · 28 unmodified lines
2929 */
3030 #define EXTENT_BIO_COMPRESSED 1
3131 #define EXTENT_BIO_TREE_LOG 2
32−#define EXTENT_BIO_PARENT_LOCKED 4
3332 #define EXTENT_BIO_FLAG_SHIFT 16
3433
3534 /* 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
210209 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
211210 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
212211 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);
215212 int __init extent_io_init(void);
216213 void extent_io_exit(void);
217214
fs/btrfs/inode.c+13−1
@@ -5717,6 +5717,7 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) · 5716 unmodified lines
57175717 char *name_ptr;
57185718 int name_len;
57195719 int is_curr = 0; /* ctx->pos points to the current index? */
5720+ bool emitted;
57205721
57215722 /* FIXME, use a real flag for deciding about the key type */
57225723 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
57455746 if (ret < 0)
57465747 goto err;
57475748
5749+ emitted = false;
57485750 while (1) {
57495751 leaf = path->nodes[0];
57505752 slot = path->slots[0];
@@ -5824,6 +5826,7 @@ skip: · 73 unmodified lines
58245826
58255827 if (over)
58265828 goto nopos;
5829+ emitted = true;
58275830 di_len = btrfs_dir_name_len(leaf, di) +
58285831 btrfs_dir_data_len(leaf, di) + sizeof(*di);
58295832 di_cur += di_len;
@@ -5836,11 +5839,20 @@ next: · 6 unmodified lines
58365839 if (key_type == BTRFS_DIR_INDEX_KEY) {
58375840 if (is_curr)
58385841 ctx->pos++;
5839− ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5842+ ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
58405843 if (ret)
58415844 goto nopos;
58425845 }
58435846
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+
58445856 /* Reached end of directory/root. Bump pos past the last item. */
58455857 ctx->pos++;
58465858
fs/btrfs/ioctl.c+97−22
@@ -2794,24 +2794,29 @@ out: · 2793 unmodified lines
27942794 static struct page *extent_same_get_page(struct inode *inode, pgoff_t index)
27952795 {
27962796 struct page *page;
2797− struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
27982797
27992798 page = grab_cache_page(inode->i_mapping, index);
28002799 if (!page)
2801− return NULL;
2800+ return ERR_PTR(-ENOMEM);
28022801
28032802 if (!PageUptodate(page)) {
2804− if (extent_read_full_page_nolock(tree, page, btrfs_get_extent,
2805− 0))
2806− return NULL;
2803+ int ret;
2804+
2805+ ret = btrfs_readpage(NULL, page);
2806+ if (ret)
2807+ return ERR_PTR(ret);
28072808 lock_page(page);
28082809 if (!PageUptodate(page)) {
28092810 unlock_page(page);
28102811 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);
28122818 }
28132819 }
2814− unlock_page(page);
28152820
28162821 return page;
28172822 }
@@ -2823,17 +2828,31 @@ static int gather_extent_pages(struct inode *inode, struct page **pages, · 5 unmodified lines
28232828 pgoff_t index = off >> PAGE_CACHE_SHIFT;
28242829
28252830 for (i = 0; i < num_pages; i++) {
2831+again:
28262832 pages[i] = extent_same_get_page(inode, index + i);
2827− if (!pages[i])
2828− return -ENOMEM;
2833+ if (IS_ERR(pages[i])) {
2834+ int err = PTR_ERR(pages[i]);
2835+
2836+ if (err == -EAGAIN)
2837+ goto again;
2838+ pages[i] = NULL;
2839+ return err;
2840+ }
28292841 }
28302842 return 0;
28312843 }
28322844
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)
28342847 {
2835− /* do any pending delalloc/csum calc on src, one way or
2836− another, and lock file content */
2848+ /*
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+ */
28372856 while (1) {
28382857 struct btrfs_ordered_extent *ordered;
28392858 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
28512870 unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1);
28522871 if (ordered)
28532872 btrfs_put_ordered_extent(ordered);
2873+ if (!retry_range_locking)
2874+ return -EAGAIN;
28542875 btrfs_wait_ordered_range(inode, off, len);
28552876 }
2877+ return 0;
28562878 }
28572879
28582880 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
28772899 unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1);
28782900 }
28792901
2880−static void btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2881− struct inode *inode2, u64 loff2, u64 len)
2902+static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1,
2903+ struct inode *inode2, u64 loff2, u64 len,
2904+ bool retry_range_locking)
28822905 {
2906+ int ret;
2907+
28832908 if (inode1 < inode2) {
28842909 swap(inode1, inode2);
28852910 swap(loff1, loff2);
28862911 }
2887− lock_extent_range(inode1, loff1, len);
2888− lock_extent_range(inode2, loff2, len);
2912+ ret = lock_extent_range(inode1, loff1, len, retry_range_locking);
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;
28892920 }
28902921
28912922 struct cmp_pages {
@@ -2901,11 +2932,15 @@ static void btrfs_cmp_data_free(struct cmp_pages *cmp) · 9 unmodified lines
29012932
29022933 for (i = 0; i < cmp->num_pages; i++) {
29032934 pg = cmp->src_pages[i];
2904− if (pg)
2935+ if (pg) {
2936+ unlock_page(pg);
29052937 page_cache_release(pg);
2938+ }
29062939 pg = cmp->dst_pages[i];
2907− if (pg)
2940+ if (pg) {
2941+ unlock_page(pg);
29082942 page_cache_release(pg);
2943+ }
29092944 }
29102945 kfree(cmp->src_pages);
29112946 kfree(cmp->dst_pages);
@@ -2966,6 +3001,8 @@ static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, · 54 unmodified lines
29663001
29673002 src_page = cmp->src_pages[i];
29683003 dst_page = cmp->dst_pages[i];
3004+ ASSERT(PageLocked(src_page));
3005+ ASSERT(PageLocked(dst_page));
29693006
29703007 addr = kmap_atomic(src_page);
29713008 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
30783115 goto out_unlock;
30793116 }
30803117
3118+again:
30813119 ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, &cmp);
30823120 if (ret)
30833121 goto out_unlock;
30843122
30853123 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);
30873126 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+ }
30893158
30903159 /* pass original length for comparison so we stay within i_size */
30913160 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
37953864 u64 lock_start = min_t(u64, off, destoff);
37963865 u64 lock_len = max_t(u64, off, destoff) + len - lock_start;
37973866
3798− lock_extent_range(src, lock_start, lock_len);
3867+ ret = lock_extent_range(src, lock_start, lock_len, true);
37993868 } 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;
38013876 }
38023877
38033878 ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);