Btrfs: add an assert to btrfs_lookup_csums_range for alignment
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / btrfs / file-item.c
index a7bfc95..ae8a513 100644 (file)
@@ -23,6 +23,7 @@
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
+#include "volumes.h"
 #include "print-tree.h"
 
 #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
@@ -152,28 +153,54 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
        return ret;
 }
 
+static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err)
+{
+       kfree(bio->csum_allocated);
+}
+
 static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
                                   struct inode *inode, struct bio *bio,
                                   u64 logical_offset, u32 *dst, int dio)
 {
-       u32 sum[16];
-       int len;
        struct bio_vec *bvec = bio->bi_io_vec;
-       int bio_index = 0;
+       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;
+       struct btrfs_path *path;
+       u8 *csum;
        u64 offset = 0;
        u64 item_start_offset = 0;
        u64 item_last_offset = 0;
        u64 disk_bytenr;
        u32 diff;
-       u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
+       int nblocks;
+       int bio_index = 0;
        int count;
-       struct btrfs_path *path;
-       struct btrfs_csum_item *item = NULL;
-       struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
+       u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
 
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
+
+       nblocks = bio->bi_size >> inode->i_sb->s_blocksize_bits;
+       if (!dst) {
+               if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
+                       btrfs_bio->csum_allocated = kmalloc(nblocks * csum_size,
+                                                           GFP_NOFS);
+                       if (!btrfs_bio->csum_allocated) {
+                               btrfs_free_path(path);
+                               return -ENOMEM;
+                       }
+                       btrfs_bio->csum = btrfs_bio->csum_allocated;
+                       btrfs_bio->end_io = btrfs_io_bio_endio_readpage;
+               } else {
+                       btrfs_bio->csum = btrfs_bio->csum_inline;
+               }
+               csum = btrfs_bio->csum;
+       } else {
+               csum = (u8 *)dst;
+       }
+
        if (bio->bi_size > PAGE_CACHE_SIZE * 8)
                path->reada = 2;
 
@@ -194,11 +221,10 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
        if (dio)
                offset = logical_offset;
        while (bio_index < bio->bi_vcnt) {
-               len = min_t(int, ARRAY_SIZE(sum), bio->bi_vcnt - bio_index);
                if (!dio)
                        offset = page_offset(bvec->bv_page) + bvec->bv_offset;
-               count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, sum,
-                                              len);
+               count = btrfs_find_ordered_sum(inode, offset, disk_bytenr,
+                                              (u32 *)csum, nblocks);
                if (count)
                        goto found;
 
@@ -213,7 +239,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
                                                 path, disk_bytenr, 0);
                        if (IS_ERR(item)) {
                                count = 1;
-                               sum[0] = 0;
+                               memset(csum, 0, csum_size);
                                if (BTRFS_I(inode)->root->root_key.objectid ==
                                    BTRFS_DATA_RELOC_TREE_OBJECTID) {
                                        set_extent_bits(io_tree, offset,
@@ -222,9 +248,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
                                } else {
                                        printk(KERN_INFO "btrfs no csum found "
                                               "for inode %llu start %llu\n",
-                                              (unsigned long long)
-                                              btrfs_ino(inode),
-                                              (unsigned long long)offset);
+                                              btrfs_ino(inode), offset);
                                }
                                item = NULL;
                                btrfs_release_path(path);
@@ -249,23 +273,14 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
                diff = disk_bytenr - item_start_offset;
                diff = diff / root->sectorsize;
                diff = diff * csum_size;
-               count = min_t(int, len, (item_last_offset - disk_bytenr) >>
-                                       inode->i_sb->s_blocksize_bits);
-               read_extent_buffer(path->nodes[0], sum,
+               count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >>
+                                           inode->i_sb->s_blocksize_bits);
+               read_extent_buffer(path->nodes[0], csum,
                                   ((unsigned long)item) + diff,
                                   csum_size * count);
 found:
-               if (dst) {
-                       memcpy(dst, sum, count * csum_size);
-                       dst += count;
-               } else {
-                       if (dio)
-                               extent_cache_csums_dio(io_tree, offset, sum,
-                                                      count);
-                       else
-                               extent_cache_csums(io_tree, bio, bio_index, sum,
-                                           count);
-               }
+               csum += count * csum_size;
+               nblocks -= count;
                while (count--) {
                        disk_bytenr += bvec->bv_len;
                        offset += bvec->bv_len;
@@ -284,9 +299,19 @@ int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
 }
 
 int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
-                             struct bio *bio, u64 offset)
+                             struct btrfs_dio_private *dip, struct bio *bio,
+                             u64 offset)
 {
-       return __btrfs_lookup_bio_sums(root, inode, bio, offset, NULL, 1);
+       int len = (bio->bi_sector << 9) - dip->disk_bytenr;
+       u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
+       int ret;
+
+       len >>= inode->i_sb->s_blocksize_bits;
+       len *= csum_size;
+
+       ret = __btrfs_lookup_bio_sums(root, inode, bio, offset,
+                                     (u32 *)(dip->csum + len), 1);
+       return ret;
 }
 
 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
@@ -304,6 +329,9 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
        u64 csum_end;
        u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
 
+       ASSERT(start == ALIGN(start, root->sectorsize) &&
+              (end + 1) == ALIGN(end + 1, root->sectorsize));
+
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;