btrfs: add boilerplate code for directly including the crypto framework
[sfrench/cifs-2.6.git] / fs / btrfs / disk-io.c
index deb74a8c191a47583189dcc0cb9fc6ff4c8dac58..34222bbe4b482fb2cfecd8a977ee97415c34d43e 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/crc32c.h>
 #include <linux/sched/mm.h>
 #include <asm/unaligned.h>
+#include <crypto/hash.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
 #include "tree-checker.h"
 #include "ref-verify.h"
 
-#ifdef CONFIG_X86
-#include <asm/cpufeature.h>
-#endif
-
 #define BTRFS_SUPER_FLAG_SUPP  (BTRFS_HEADER_FLAG_WRITTEN |\
                                 BTRFS_HEADER_FLAG_RELOC |\
                                 BTRFS_SUPER_FLAG_ERROR |\
@@ -356,6 +353,16 @@ out:
        return ret;
 }
 
+static bool btrfs_supported_super_csum(u16 csum_type)
+{
+       switch (csum_type) {
+       case BTRFS_CSUM_TYPE_CRC32:
+               return true;
+       default:
+               return false;
+       }
+}
+
 /*
  * Return 0 if the superblock checksum type matches the checksum value of that
  * algorithm. Pass the raw disk superblock data.
@@ -365,33 +372,22 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
 {
        struct btrfs_super_block *disk_sb =
                (struct btrfs_super_block *)raw_disk_sb;
-       u16 csum_type = btrfs_super_csum_type(disk_sb);
-       int ret = 0;
-
-       if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
-               u32 crc = ~(u32)0;
-               char result[sizeof(crc)];
-
-               /*
-                * The super_block structure does not span the whole
-                * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
-                * is filled with zeros and is included in the checksum.
-                */
-               crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
-                               crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
-               btrfs_csum_final(crc, result);
+       u32 crc = ~(u32)0;
+       char result[BTRFS_CSUM_SIZE];
 
-               if (memcmp(raw_disk_sb, result, sizeof(result)))
-                       ret = 1;
-       }
+       /*
+        * The super_block structure does not span the whole
+        * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
+        * filled with zeros and is included in the checksum.
+        */
+       crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
+                             crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
+       btrfs_csum_final(crc, result);
 
-       if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
-               btrfs_err(fs_info, "unsupported checksum algorithm %u",
-                               csum_type);
-               ret = 1;
-       }
+       if (memcmp(disk_sb->csum, result, btrfs_super_csum_size(disk_sb)))
+               return 1;
 
-       return ret;
+       return 0;
 }
 
 int btrfs_verify_level_key(struct extent_buffer *eb, int level,
@@ -873,14 +869,13 @@ static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio,
        return btree_csum_one_bio(bio);
 }
 
-static int check_async_write(struct btrfs_inode *bi)
+static int check_async_write(struct btrfs_fs_info *fs_info,
+                            struct btrfs_inode *bi)
 {
        if (atomic_read(&bi->sync_writers))
                return 0;
-#ifdef CONFIG_X86
-       if (static_cpu_has(X86_FEATURE_XMM4_2))
+       if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags))
                return 0;
-#endif
        return 1;
 }
 
@@ -889,7 +884,7 @@ static blk_status_t btree_submit_bio_hook(struct inode *inode, struct bio *bio,
                                          unsigned long bio_flags)
 {
        struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
-       int async = check_async_write(BTRFS_I(inode));
+       int async = check_async_write(fs_info, BTRFS_I(inode));
        blk_status_t ret;
 
        if (bio_op(bio) != REQ_OP_WRITE) {
@@ -2262,6 +2257,29 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
        return 0;
 }
 
+static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
+{
+       struct crypto_shash *csum_shash;
+       const char *csum_name = btrfs_super_csum_name(csum_type);
+
+       csum_shash = crypto_alloc_shash(csum_name, 0, 0);
+
+       if (IS_ERR(csum_shash)) {
+               btrfs_err(fs_info, "error allocating %s hash for checksum",
+                         csum_name);
+               return PTR_ERR(csum_shash);
+       }
+
+       fs_info->csum_shash = csum_shash;
+
+       return 0;
+}
+
+static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
+{
+       crypto_free_shash(fs_info->csum_shash);
+}
+
 static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
                            struct btrfs_fs_devices *fs_devices)
 {
@@ -2577,7 +2595,7 @@ static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
        ret = validate_super(fs_info, sb, -1);
        if (ret < 0)
                goto out;
-       if (btrfs_super_csum_type(sb) != BTRFS_CSUM_TYPE_CRC32) {
+       if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
                ret = -EUCLEAN;
                btrfs_err(fs_info, "invalid csum type, has %u want %u",
                          btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
@@ -2607,6 +2625,7 @@ int open_ctree(struct super_block *sb,
        u32 stripesize;
        u64 generation;
        u64 features;
+       u16 csum_type;
        struct btrfs_key location;
        struct buffer_head *bh;
        struct btrfs_super_block *disk_super;
@@ -2689,7 +2708,7 @@ int open_ctree(struct super_block *sb,
        INIT_LIST_HEAD(&fs_info->space_info);
        INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
        INIT_LIST_HEAD(&fs_info->unused_bgs);
-       btrfs_mapping_init(&fs_info->mapping_tree);
+       extent_map_tree_init(&fs_info->mapping_tree);
        btrfs_init_block_rsv(&fs_info->global_block_rsv,
                             BTRFS_BLOCK_RSV_GLOBAL);
        btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
@@ -2812,6 +2831,25 @@ int open_ctree(struct super_block *sb,
                goto fail_alloc;
        }
 
+       /*
+        * Verify the type first, if that or the the checksum value are
+        * corrupted, we'll find out
+        */
+       csum_type = btrfs_super_csum_type((struct btrfs_super_block *)bh->b_data);
+       if (!btrfs_supported_super_csum(csum_type)) {
+               btrfs_err(fs_info, "unsupported checksum algorithm: %u",
+                         csum_type);
+               err = -EINVAL;
+               brelse(bh);
+               goto fail_alloc;
+       }
+
+       ret = btrfs_init_csum_hash(fs_info, csum_type);
+       if (ret) {
+               err = ret;
+               goto fail_alloc;
+       }
+
        /*
         * We want to check superblock checksum, the type is stored inside.
         * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
@@ -2820,7 +2858,7 @@ int open_ctree(struct super_block *sb,
                btrfs_err(fs_info, "superblock checksum mismatch");
                err = -EINVAL;
                brelse(bh);
-               goto fail_alloc;
+               goto fail_csum;
        }
 
        /*
@@ -2857,11 +2895,11 @@ int open_ctree(struct super_block *sb,
        if (ret) {
                btrfs_err(fs_info, "superblock contains fatal errors");
                err = -EINVAL;
-               goto fail_alloc;
+               goto fail_csum;
        }
 
        if (!btrfs_super_root(disk_super))
-               goto fail_alloc;
+               goto fail_csum;
 
        /* check FS state, whether FS is broken. */
        if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
@@ -2883,7 +2921,7 @@ int open_ctree(struct super_block *sb,
        ret = btrfs_parse_options(fs_info, options, sb->s_flags);
        if (ret) {
                err = ret;
-               goto fail_alloc;
+               goto fail_csum;
        }
 
        features = btrfs_super_incompat_flags(disk_super) &
@@ -2893,7 +2931,7 @@ int open_ctree(struct super_block *sb,
                    "cannot mount because of unsupported optional features (%llx)",
                    features);
                err = -EINVAL;
-               goto fail_alloc;
+               goto fail_csum;
        }
 
        features = btrfs_super_incompat_flags(disk_super);
@@ -2937,7 +2975,7 @@ int open_ctree(struct super_block *sb,
                btrfs_err(fs_info,
 "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
                        nodesize, sectorsize);
-               goto fail_alloc;
+               goto fail_csum;
        }
 
        /*
@@ -2953,7 +2991,7 @@ int open_ctree(struct super_block *sb,
        "cannot mount read-write because of unsupported optional features (%llx)",
                       features);
                err = -EINVAL;
-               goto fail_alloc;
+               goto fail_csum;
        }
 
        ret = btrfs_init_workqueues(fs_info, fs_devices);
@@ -3331,6 +3369,8 @@ fail_tree_roots:
 fail_sb_buffer:
        btrfs_stop_all_workers(fs_info);
        btrfs_free_block_groups(fs_info);
+fail_csum:
+       btrfs_free_csum_hash(fs_info);
 fail_alloc:
 fail_iput:
        btrfs_mapping_tree_free(&fs_info->mapping_tree);
@@ -3709,7 +3749,7 @@ int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
 
        if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
            (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
-               min_tolerated = min(min_tolerated,
+               min_tolerated = min_t(int, min_tolerated,
                                    btrfs_raid_array[BTRFS_RAID_SINGLE].
                                    tolerated_failures);
 
@@ -3718,7 +3758,7 @@ int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
                        continue;
                if (!(flags & btrfs_raid_array[raid_type].bg_flag))
                        continue;
-               min_tolerated = min(min_tolerated,
+               min_tolerated = min_t(int, min_tolerated,
                                    btrfs_raid_array[raid_type].
                                    tolerated_failures);
        }