struct inode *inode, struct bio *bio,
                                   u64 logical_offset, u32 *dst, int dio)
 {
-       struct bio_vec *bvec = bio->bi_io_vec;
+       struct bio_vec *bvec;
        struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
        struct btrfs_csum_item *item = NULL;
        struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
        u64 page_bytes_left;
        u32 diff;
        int nblocks;
-       int bio_index = 0;
-       int count;
+       int count = 0, i;
        u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
 
        path = btrfs_alloc_path();
        if (dio)
                offset = logical_offset;
 
-       page_bytes_left = bvec->bv_len;
-       while (bio_index < bio->bi_vcnt) {
+       bio_for_each_segment_all(bvec, bio, i) {
+               page_bytes_left = bvec->bv_len;
+               if (count)
+                       goto next;
+
                if (!dio)
                        offset = page_offset(bvec->bv_page) + bvec->bv_offset;
                count = btrfs_find_ordered_sum(inode, offset, disk_bytenr,
 found:
                csum += count * csum_size;
                nblocks -= count;
-
+next:
                while (count--) {
                        disk_bytenr += root->sectorsize;
                        offset += root->sectorsize;
                        page_bytes_left -= root->sectorsize;
-                       if (!page_bytes_left) {
-                               bio_index++;
-                               /*
-                                * make sure we're still inside the
-                                * bio before we update page_bytes_left
-                                */
-                               if (bio_index >= bio->bi_vcnt) {
-                                       WARN_ON_ONCE(count);
-                                       goto done;
-                               }
-                               bvec++;
-                               page_bytes_left = bvec->bv_len;
-                       }
-
+                       if (!page_bytes_left)
+                               break; /* move to next bio */
                }
        }
 
-done:
+       WARN_ON_ONCE(count);
        btrfs_free_path(path);
        return 0;
 }