Commit 509659cd authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: switch to crc32c instead of sha256

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 8d7be552
...@@ -30,6 +30,8 @@ extern struct kmem_cache *btrfs_path_cachep; ...@@ -30,6 +30,8 @@ extern struct kmem_cache *btrfs_path_cachep;
/* 32 bytes in various csum fields */ /* 32 bytes in various csum fields */
#define BTRFS_CSUM_SIZE 32 #define BTRFS_CSUM_SIZE 32
/* four bytes for CRC32 */
#define BTRFS_CRC32_SIZE 4
/* /*
* the key defines the order in the tree, and so it also defines (optimal) * the key defines the order in the tree, and so it also defines (optimal)
...@@ -235,7 +237,7 @@ struct btrfs_file_extent_item { ...@@ -235,7 +237,7 @@ struct btrfs_file_extent_item {
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
struct btrfs_csum_item { struct btrfs_csum_item {
u8 csum[BTRFS_CSUM_SIZE]; u8 csum;
} __attribute__ ((__packed__)); } __attribute__ ((__packed__));
struct btrfs_device_item { struct btrfs_device_item {
......
...@@ -196,14 +196,14 @@ int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len, ...@@ -196,14 +196,14 @@ int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
ret = crypto_hash_digest(&desc, &sg, 1, result); ret = crypto_hash_digest(&desc, &sg, 1, result);
spin_unlock(&root->fs_info->hash_lock); spin_unlock(&root->fs_info->hash_lock);
if (ret) { if (ret) {
printk("sha256 digest failed\n"); printk("digest failed\n");
} }
return ret; return ret;
} }
static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
int verify) int verify)
{ {
char result[BTRFS_CSUM_SIZE]; char result[BTRFS_CRC32_SIZE];
int ret; int ret;
struct btrfs_node *node; struct btrfs_node *node;
...@@ -212,14 +212,14 @@ static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, ...@@ -212,14 +212,14 @@ static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
if (ret) if (ret)
return ret; return ret;
if (verify) { if (verify) {
if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
printk("checksum verify failed on %Lu\n", printk("checksum verify failed on %Lu\n",
bh_blocknr(bh)); bh_blocknr(bh));
return 1; return 1;
} }
} else { } else {
node = btrfs_buffer_node(bh); node = btrfs_buffer_node(bh);
memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
} }
return 0; return 0;
} }
...@@ -576,10 +576,10 @@ struct btrfs_root *open_ctree(struct super_block *sb) ...@@ -576,10 +576,10 @@ struct btrfs_root *open_ctree(struct super_block *sb)
sizeof(struct btrfs_key)); sizeof(struct btrfs_key));
insert_inode_hash(fs_info->btree_inode); insert_inode_hash(fs_info->btree_inode);
mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
spin_lock_init(&fs_info->hash_lock); spin_lock_init(&fs_info->hash_lock);
if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
printk("failed to allocate sha256 hash\n"); printk("failed to allocate digest hash\n");
return NULL; return NULL;
} }
mutex_init(&fs_info->trans_mutex); mutex_init(&fs_info->trans_mutex);
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
#include "transaction.h" #include "transaction.h"
#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
sizeof(struct btrfs_item) * 2) / \ sizeof(struct btrfs_item) * 2) / \
sizeof(struct btrfs_csum_item)) - 1)) BTRFS_CRC32_SIZE) - 1))
int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root, struct btrfs_root *root,
u64 objectid, u64 pos, u64 objectid, u64 pos,
...@@ -78,7 +78,7 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, ...@@ -78,7 +78,7 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
csum_offset = (offset - found_key.offset) >> csum_offset = (offset - found_key.offset) >>
root->fs_info->sb->s_blocksize_bits; root->fs_info->sb->s_blocksize_bits;
csums_in_item = btrfs_item_size(leaf->items + path->slots[0]); csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
csums_in_item /= sizeof(struct btrfs_csum_item); csums_in_item /= BTRFS_CRC32_SIZE;
if (csum_offset >= csums_in_item) { if (csum_offset >= csums_in_item) {
ret = -EFBIG; ret = -EFBIG;
...@@ -86,7 +86,8 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, ...@@ -86,7 +86,8 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
} }
} }
item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
item += csum_offset; item = (struct btrfs_csum_item *)((unsigned char *)item +
csum_offset * BTRFS_CRC32_SIZE);
return item; return item;
fail: fail:
if (ret > 0) if (ret > 0)
...@@ -143,8 +144,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -143,8 +144,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
/* we found one, but it isn't big enough yet */ /* we found one, but it isn't big enough yet */
leaf = btrfs_buffer_leaf(path->nodes[0]); leaf = btrfs_buffer_leaf(path->nodes[0]);
item_size = btrfs_item_size(leaf->items + path->slots[0]); item_size = btrfs_item_size(leaf->items + path->slots[0]);
if ((item_size / sizeof(struct btrfs_csum_item)) >= if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
MAX_CSUM_ITEMS(root)) {
/* already at max size, make a new one */ /* already at max size, make a new one */
goto insert; goto insert;
} }
...@@ -159,7 +159,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -159,7 +159,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
*/ */
btrfs_release_path(root, path); btrfs_release_path(root, path);
ret = btrfs_search_slot(trans, root, &file_key, path, ret = btrfs_search_slot(trans, root, &file_key, path,
sizeof(struct btrfs_csum_item), 1); BTRFS_CRC32_SIZE, 1);
if (ret < 0) if (ret < 0)
goto fail; goto fail;
if (ret == 0) { if (ret == 0) {
...@@ -180,10 +180,10 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -180,10 +180,10 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
goto insert; goto insert;
} }
if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
sizeof(struct btrfs_csum_item)) { BTRFS_CRC32_SIZE) {
u32 diff = (csum_offset + 1) * sizeof(struct btrfs_csum_item); u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
diff = diff - btrfs_item_size(leaf->items + path->slots[0]); diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
WARN_ON(diff != sizeof(struct btrfs_csum_item)); WARN_ON(diff != BTRFS_CRC32_SIZE);
ret = btrfs_extend_item(trans, root, path, diff); ret = btrfs_extend_item(trans, root, path, diff);
BUG_ON(ret); BUG_ON(ret);
goto csum; goto csum;
...@@ -193,7 +193,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -193,7 +193,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
btrfs_release_path(root, path); btrfs_release_path(root, path);
csum_offset = 0; csum_offset = 0;
ret = btrfs_insert_empty_item(trans, root, path, &file_key, ret = btrfs_insert_empty_item(trans, root, path, &file_key,
sizeof(struct btrfs_csum_item)); BTRFS_CRC32_SIZE);
if (ret != 0) { if (ret != 0) {
printk("at insert for %Lu %u %Lu ret is %d\n", file_key.objectid, file_key.flags, file_key.offset, ret); printk("at insert for %Lu %u %Lu ret is %d\n", file_key.objectid, file_key.flags, file_key.offset, ret);
WARN_ON(1); WARN_ON(1);
...@@ -203,10 +203,13 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans, ...@@ -203,10 +203,13 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
struct btrfs_csum_item); struct btrfs_csum_item);
ret = 0; ret = 0;
item += csum_offset; item = (struct btrfs_csum_item *)((unsigned char *)item +
csum_offset * BTRFS_CRC32_SIZE);
found: found:
btrfs_check_bounds(item->csum, BTRFS_CSUM_SIZE, path->nodes[0]->b_data, root->fs_info->sb->s_blocksize); btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
ret = btrfs_csum_data(root, data, len, item->csum); path->nodes[0]->b_data,
root->fs_info->sb->s_blocksize);
ret = btrfs_csum_data(root, data, len, &item->csum);
btrfs_mark_buffer_dirty(path->nodes[0]); btrfs_mark_buffer_dirty(path->nodes[0]);
fail: fail:
btrfs_release_path(root, path); btrfs_release_path(root, path);
...@@ -222,7 +225,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root, ...@@ -222,7 +225,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root,
struct btrfs_key file_key; struct btrfs_key file_key;
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_csum_item *item; struct btrfs_csum_item *item;
char result[BTRFS_CSUM_SIZE]; char result[BTRFS_CRC32_SIZE];
path = btrfs_alloc_path(); path = btrfs_alloc_path();
BUG_ON(!path); BUG_ON(!path);
...@@ -244,7 +247,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root, ...@@ -244,7 +247,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root,
ret = btrfs_csum_data(root, data, len, result); ret = btrfs_csum_data(root, data, len, result);
WARN_ON(ret); WARN_ON(ret);
if (memcmp(result, item->csum, BTRFS_CSUM_SIZE)) if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE))
ret = 1; ret = 1;
fail: fail:
btrfs_release_path(root, path); btrfs_release_path(root, path);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment