btrfs: remove always true if branch in find_delalloc_range
[sfrench/cifs-2.6.git] / fs / btrfs / extent_io.c
index 247e38ef49a6571781fb24c23f3318158f8f5118..eb820154521f7aae881d891d3ea29bd8fa808c26 100644 (file)
@@ -1452,16 +1452,16 @@ out:
  * find a contiguous range of bytes in the file marked as delalloc, not
  * more than 'max_bytes'.  start and end are used to return the range,
  *
- * 1 is returned if we find something, 0 if nothing was in the tree
+ * true is returned if we find something, false if nothing was in the tree
  */
-static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
+static noinline bool find_delalloc_range(struct extent_io_tree *tree,
                                        u64 *start, u64 *end, u64 max_bytes,
                                        struct extent_state **cached_state)
 {
        struct rb_node *node;
        struct extent_state *state;
        u64 cur_start = *start;
-       u64 found = 0;
+       bool found = false;
        u64 total_bytes = 0;
 
        spin_lock(&tree->lock);
@@ -1472,8 +1472,7 @@ static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
         */
        node = tree_search(tree, cur_start);
        if (!node) {
-               if (!found)
-                       *end = (u64)-1;
+               *end = (u64)-1;
                goto out;
        }
 
@@ -1493,7 +1492,7 @@ static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
                        *cached_state = state;
                        refcount_inc(&state->refs);
                }
-               found++;
+               found = true;
                *end = state->end;
                cur_start = state->end + 1;
                node = rb_next(node);
@@ -1551,19 +1550,22 @@ static noinline int lock_delalloc_pages(struct inode *inode,
 }
 
 /*
- * find a contiguous range of bytes in the file marked as delalloc, not
- * more than 'max_bytes'.  start and end are used to return the range,
+ * Find and lock a contiguous range of bytes in the file marked as delalloc, no
+ * more than @max_bytes.  @Start and @end are used to return the range,
  *
- * 1 is returned if we find something, 0 if nothing was in the tree
+ * Return: true if we find something
+ *         false if nothing was in the tree
  */
-static noinline_for_stack u64 find_lock_delalloc_range(struct inode *inode,
+EXPORT_FOR_TESTS
+noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
                                    struct extent_io_tree *tree,
                                    struct page *locked_page, u64 *start,
-                                   u64 *end, u64 max_bytes)
+                                   u64 *end)
 {
+       u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
        u64 delalloc_start;
        u64 delalloc_end;
-       u64 found;
+       bool found;
        struct extent_state *cached_state = NULL;
        int ret;
        int loops = 0;
@@ -1578,7 +1580,7 @@ again:
                *start = delalloc_start;
                *end = delalloc_end;
                free_extent_state(cached_state);
-               return 0;
+               return false;
        }
 
        /*
@@ -1598,6 +1600,7 @@ again:
        /* step two, lock all the pages after the page that has start */
        ret = lock_delalloc_pages(inode, locked_page,
                                  delalloc_start, delalloc_end);
+       ASSERT(!ret || ret == -EAGAIN);
        if (ret == -EAGAIN) {
                /* some of the pages are gone, lets avoid looping by
                 * shortening the size of the delalloc range we're searching
@@ -1609,11 +1612,10 @@ again:
                        loops = 1;
                        goto again;
                } else {
-                       found = 0;
+                       found = false;
                        goto out_failed;
                }
        }
-       BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
 
        /* step three, lock the state bits for the whole range */
        lock_extent_bits(tree, delalloc_start, delalloc_end, &cached_state);
@@ -1636,17 +1638,6 @@ out_failed:
        return found;
 }
 
-#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
-u64 btrfs_find_lock_delalloc_range(struct inode *inode,
-                                   struct extent_io_tree *tree,
-                                   struct page *locked_page, u64 *start,
-                                   u64 *end, u64 max_bytes)
-{
-       return find_lock_delalloc_range(inode, tree, locked_page, start, end,
-                       max_bytes);
-}
-#endif
-
 static int __process_pages_contig(struct address_space *mapping,
                                  struct page *locked_page,
                                  pgoff_t start_index, pgoff_t end_index,
@@ -2342,13 +2333,11 @@ struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
 }
 
 /*
- * this is a generic handler for readpage errors (default
- * readpage_io_failed_hook). if other copies exist, read those and write back
- * good data to the failed position. does not investigate in remapping the
- * failed extent elsewhere, hoping the device will be smart enough to do this as
- * needed
+ * This is a generic handler for readpage errors. If other copies exist, read
+ * those and write back good data to the failed position. Does not investigate
+ * in remapping the failed extent elsewhere, hoping the device will be smart
+ * enough to do this as needed
  */
-
 static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
                              struct page *page, u64 start, u64 end,
                              int failed_mirror)
@@ -2407,7 +2396,7 @@ void end_extent_writepage(struct page *page, int err, u64 start, u64 end)
        int uptodate = (err == 0);
        int ret = 0;
 
-       btrfs_writepage_endio_finish_ordered(page, start, end, NULL, uptodate);
+       btrfs_writepage_endio_finish_ordered(page, start, end, uptodate);
 
        if (!uptodate) {
                ClearPageUptodate(page);
@@ -2510,6 +2499,8 @@ static void end_bio_extent_readpage(struct bio *bio)
                struct page *page = bvec->bv_page;
                struct inode *inode = page->mapping->host;
                struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
+               bool data_inode = btrfs_ino(BTRFS_I(inode))
+                       != BTRFS_BTREE_INODE_OBJECTID;
 
                btrfs_debug(fs_info,
                        "end_bio_extent_readpage: bi_sector=%llu, err=%d, mirror=%u",
@@ -2539,7 +2530,7 @@ static void end_bio_extent_readpage(struct bio *bio)
                len = bvec->bv_len;
 
                mirror = io_bio->mirror_num;
-               if (likely(uptodate && tree->ops)) {
+               if (likely(uptodate)) {
                        ret = tree->ops->readpage_end_io_hook(io_bio, offset,
                                                              page, start, end,
                                                              mirror);
@@ -2555,38 +2546,37 @@ static void end_bio_extent_readpage(struct bio *bio)
                if (likely(uptodate))
                        goto readpage_ok;
 
-               if (tree->ops) {
-                       ret = tree->ops->readpage_io_failed_hook(page, mirror);
-                       if (ret == -EAGAIN) {
-                               /*
-                                * Data inode's readpage_io_failed_hook() always
-                                * returns -EAGAIN.
-                                *
-                                * The generic bio_readpage_error handles errors
-                                * the following way: If possible, new read
-                                * requests are created and submitted and will
-                                * end up in end_bio_extent_readpage as well (if
-                                * we're lucky, not in the !uptodate case). In
-                                * that case it returns 0 and we just go on with
-                                * the next page in our bio. If it can't handle
-                                * the error it will return -EIO and we remain
-                                * responsible for that page.
-                                */
-                               ret = bio_readpage_error(bio, offset, page,
-                                                        start, end, mirror);
-                               if (ret == 0) {
-                                       uptodate = !bio->bi_status;
-                                       offset += len;
-                                       continue;
-                               }
-                       }
+               if (data_inode) {
 
                        /*
-                        * metadata's readpage_io_failed_hook() always returns
-                        * -EIO and fixes nothing.  -EIO is also returned if
-                        * data inode error could not be fixed.
+                        * The generic bio_readpage_error handles errors the
+                        * following way: If possible, new read requests are
+                        * created and submitted and will end up in
+                        * end_bio_extent_readpage as well (if we're lucky,
+                        * not in the !uptodate case). In that case it returns
+                        * 0 and we just go on with the next page in our bio.
+                        * If it can't handle the error it will return -EIO and
+                        * we remain responsible for that page.
                         */
-                       ASSERT(ret == -EIO);
+                       ret = bio_readpage_error(bio, offset, page, start, end,
+                                                mirror);
+                       if (ret == 0) {
+                               uptodate = !bio->bi_status;
+                               offset += len;
+                               continue;
+                       }
+               } else {
+                       struct extent_buffer *eb;
+
+                       eb = (struct extent_buffer *)page->private;
+                       set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
+                       eb->read_mirror = mirror;
+                       atomic_dec(&eb->io_pages);
+                       if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD,
+                                              &eb->bflags))
+                               btree_readahead_hook(eb, -EIO);
+
+                       ret = -EIO;
                }
 readpage_ok:
                if (likely(uptodate)) {
@@ -2632,8 +2622,7 @@ readpage_ok:
        if (extent_len)
                endio_readpage_release_extent(tree, extent_start, extent_len,
                                              uptodate);
-       if (io_bio->end_io)
-               io_bio->end_io(io_bio, blk_status_to_errno(bio->bi_status));
+       btrfs_io_bio_free_csum(io_bio);
        bio_put(bio);
 }
 
@@ -2770,8 +2759,8 @@ static int submit_extent_page(unsigned int opf, struct extent_io_tree *tree,
                else
                        contig = bio_end_sector(bio) == sector;
 
-               if (tree->ops && btrfs_merge_bio_hook(page, offset, page_size,
-                                                     bio, bio_flags))
+               ASSERT(tree->ops);
+               if (btrfs_bio_fits_in_stripe(page, page_size, bio, bio_flags))
                        can_merge = false;
 
                if (prev_bio_flags != bio_flags || !contig || !can_merge ||
@@ -3206,7 +3195,7 @@ static noinline_for_stack int writepage_delalloc(struct inode *inode,
 {
        struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
        u64 page_end = delalloc_start + PAGE_SIZE - 1;
-       u64 nr_delalloc;
+       bool found;
        u64 delalloc_to_write = 0;
        u64 delalloc_end = 0;
        int ret;
@@ -3214,12 +3203,11 @@ static noinline_for_stack int writepage_delalloc(struct inode *inode,
 
 
        while (delalloc_end < page_end) {
-               nr_delalloc = find_lock_delalloc_range(inode, tree,
+               found = find_lock_delalloc_range(inode, tree,
                                               page,
                                               &delalloc_start,
-                                              &delalloc_end,
-                                              BTRFS_MAX_EXTENT_SIZE);
-               if (nr_delalloc == 0) {
+                                              &delalloc_end);
+               if (!found) {
                        delalloc_start = delalloc_end + 1;
                        continue;
                }
@@ -3326,8 +3314,7 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,
 
        end = page_end;
        if (i_size <= start) {
-               btrfs_writepage_endio_finish_ordered(page, start, page_end,
-                                                    NULL, 1);
+               btrfs_writepage_endio_finish_ordered(page, start, page_end, 1);
                goto done;
        }
 
@@ -3339,7 +3326,7 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,
 
                if (cur >= i_size) {
                        btrfs_writepage_endio_finish_ordered(page, cur,
-                                                            page_end, NULL, 1);
+                                                            page_end, 1);
                        break;
                }
                em = btrfs_get_extent(BTRFS_I(inode), page, pg_offset, cur,
@@ -3376,7 +3363,7 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,
                        if (!compressed)
                                btrfs_writepage_endio_finish_ordered(page, cur,
                                                            cur + iosize - 1,
-                                                           NULL, 1);
+                                                           1);
                        else if (compressed) {
                                /* we don't want to end_page_writeback on
                                 * a compressed extent.  this happens
@@ -3912,12 +3899,25 @@ static int extent_write_cache_pages(struct address_space *mapping,
                        range_whole = 1;
                scanned = 1;
        }
-       if (wbc->sync_mode == WB_SYNC_ALL)
+
+       /*
+        * We do the tagged writepage as long as the snapshot flush bit is set
+        * and we are the first one who do the filemap_flush() on this inode.
+        *
+        * The nr_to_write == LONG_MAX is needed to make sure other flushers do
+        * not race in and drop the bit.
+        */
+       if (range_whole && wbc->nr_to_write == LONG_MAX &&
+           test_and_clear_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
+                              &BTRFS_I(inode)->runtime_flags))
+               wbc->tagged_writepages = 1;
+
+       if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
                tag = PAGECACHE_TAG_TOWRITE;
        else
                tag = PAGECACHE_TAG_DIRTY;
 retry:
-       if (wbc->sync_mode == WB_SYNC_ALL)
+       if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
                tag_pages_for_writeback(mapping, index, end);
        done_index = index;
        while (!done && !nr_to_write_done && (index <= end) &&
@@ -4063,8 +4063,7 @@ int extent_write_locked_range(struct inode *inode, u64 start, u64 end,
                        ret = __extent_writepage(page, &wbc_writepages, &epd);
                else {
                        btrfs_writepage_endio_finish_ordered(page, start,
-                                                   start + PAGE_SIZE - 1,
-                                                   NULL, 1);
+                                                   start + PAGE_SIZE - 1, 1);
                        unlock_page(page);
                }
                put_page(page);
@@ -5381,7 +5380,7 @@ int map_private_extent_buffer(const struct extent_buffer *eb,
                              char **map, unsigned long *map_start,
                              unsigned long *map_len)
 {
-       size_t offset = start & (PAGE_SIZE - 1);
+       size_t offset;
        char *kaddr;
        struct page *p;
        size_t start_offset = eb->start & ((u64)PAGE_SIZE - 1);