aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/btrfs/ctree.h4
-rw-r--r--fs/btrfs/disk-io.c12
-rw-r--r--fs/btrfs/file-item.c35
3 files changed, 28 insertions, 23 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 92a6078de827..d8caeba5d758 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -30,6 +30,8 @@ extern struct kmem_cache *btrfs_path_cachep;
30 30
31/* 32 bytes in various csum fields */ 31/* 32 bytes in various csum fields */
32#define BTRFS_CSUM_SIZE 32 32#define BTRFS_CSUM_SIZE 32
33/* four bytes for CRC32 */
34#define BTRFS_CRC32_SIZE 4
33 35
34/* 36/*
35 * the key defines the order in the tree, and so it also defines (optimal) 37 * the key defines the order in the tree, and so it also defines (optimal)
@@ -235,7 +237,7 @@ struct btrfs_file_extent_item {
235} __attribute__ ((__packed__)); 237} __attribute__ ((__packed__));
236 238
237struct btrfs_csum_item { 239struct btrfs_csum_item {
238 u8 csum[BTRFS_CSUM_SIZE]; 240 u8 csum;
239} __attribute__ ((__packed__)); 241} __attribute__ ((__packed__));
240 242
241struct btrfs_device_item { 243struct btrfs_device_item {
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 2dbf422a2b9a..678456d1e2f9 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -196,14 +196,14 @@ int btrfs_csum_data(struct btrfs_root * root, char *data, size_t len,
196 ret = crypto_hash_digest(&desc, &sg, 1, result); 196 ret = crypto_hash_digest(&desc, &sg, 1, result);
197 spin_unlock(&root->fs_info->hash_lock); 197 spin_unlock(&root->fs_info->hash_lock);
198 if (ret) { 198 if (ret) {
199 printk("sha256 digest failed\n"); 199 printk("digest failed\n");
200 } 200 }
201 return ret; 201 return ret;
202} 202}
203static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh, 203static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
204 int verify) 204 int verify)
205{ 205{
206 char result[BTRFS_CSUM_SIZE]; 206 char result[BTRFS_CRC32_SIZE];
207 int ret; 207 int ret;
208 struct btrfs_node *node; 208 struct btrfs_node *node;
209 209
@@ -212,14 +212,14 @@ static int csum_tree_block(struct btrfs_root *root, struct buffer_head *bh,
212 if (ret) 212 if (ret)
213 return ret; 213 return ret;
214 if (verify) { 214 if (verify) {
215 if (memcmp(bh->b_data, result, BTRFS_CSUM_SIZE)) { 215 if (memcmp(bh->b_data, result, BTRFS_CRC32_SIZE)) {
216 printk("checksum verify failed on %Lu\n", 216 printk("checksum verify failed on %Lu\n",
217 bh_blocknr(bh)); 217 bh_blocknr(bh));
218 return 1; 218 return 1;
219 } 219 }
220 } else { 220 } else {
221 node = btrfs_buffer_node(bh); 221 node = btrfs_buffer_node(bh);
222 memcpy(node->header.csum, result, BTRFS_CSUM_SIZE); 222 memcpy(node->header.csum, result, BTRFS_CRC32_SIZE);
223 } 223 }
224 return 0; 224 return 0;
225} 225}
@@ -576,10 +576,10 @@ struct btrfs_root *open_ctree(struct super_block *sb)
576 sizeof(struct btrfs_key)); 576 sizeof(struct btrfs_key));
577 insert_inode_hash(fs_info->btree_inode); 577 insert_inode_hash(fs_info->btree_inode);
578 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 578 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
579 fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); 579 fs_info->hash_tfm = crypto_alloc_hash("crc32c", 0, CRYPTO_ALG_ASYNC);
580 spin_lock_init(&fs_info->hash_lock); 580 spin_lock_init(&fs_info->hash_lock);
581 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) { 581 if (!fs_info->hash_tfm || IS_ERR(fs_info->hash_tfm)) {
582 printk("failed to allocate sha256 hash\n"); 582 printk("failed to allocate digest hash\n");
583 return NULL; 583 return NULL;
584 } 584 }
585 mutex_init(&fs_info->trans_mutex); 585 mutex_init(&fs_info->trans_mutex);
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 10e4cf08e9ed..a66709e6d036 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -4,8 +4,8 @@
4#include "transaction.h" 4#include "transaction.h"
5 5
6#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 6#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
7 sizeof(struct btrfs_item) * 2) / \ 7 sizeof(struct btrfs_item) * 2) / \
8 sizeof(struct btrfs_csum_item)) - 1)) 8 BTRFS_CRC32_SIZE) - 1))
9int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 9int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
10 struct btrfs_root *root, 10 struct btrfs_root *root,
11 u64 objectid, u64 pos, 11 u64 objectid, u64 pos,
@@ -78,7 +78,7 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
78 csum_offset = (offset - found_key.offset) >> 78 csum_offset = (offset - found_key.offset) >>
79 root->fs_info->sb->s_blocksize_bits; 79 root->fs_info->sb->s_blocksize_bits;
80 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]); 80 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
81 csums_in_item /= sizeof(struct btrfs_csum_item); 81 csums_in_item /= BTRFS_CRC32_SIZE;
82 82
83 if (csum_offset >= csums_in_item) { 83 if (csum_offset >= csums_in_item) {
84 ret = -EFBIG; 84 ret = -EFBIG;
@@ -86,7 +86,8 @@ struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
86 } 86 }
87 } 87 }
88 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); 88 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
89 item += csum_offset; 89 item = (struct btrfs_csum_item *)((unsigned char *)item +
90 csum_offset * BTRFS_CRC32_SIZE);
90 return item; 91 return item;
91fail: 92fail:
92 if (ret > 0) 93 if (ret > 0)
@@ -143,8 +144,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
143 /* we found one, but it isn't big enough yet */ 144 /* we found one, but it isn't big enough yet */
144 leaf = btrfs_buffer_leaf(path->nodes[0]); 145 leaf = btrfs_buffer_leaf(path->nodes[0]);
145 item_size = btrfs_item_size(leaf->items + path->slots[0]); 146 item_size = btrfs_item_size(leaf->items + path->slots[0]);
146 if ((item_size / sizeof(struct btrfs_csum_item)) >= 147 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
147 MAX_CSUM_ITEMS(root)) {
148 /* already at max size, make a new one */ 148 /* already at max size, make a new one */
149 goto insert; 149 goto insert;
150 } 150 }
@@ -159,7 +159,7 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
159 */ 159 */
160 btrfs_release_path(root, path); 160 btrfs_release_path(root, path);
161 ret = btrfs_search_slot(trans, root, &file_key, path, 161 ret = btrfs_search_slot(trans, root, &file_key, path,
162 sizeof(struct btrfs_csum_item), 1); 162 BTRFS_CRC32_SIZE, 1);
163 if (ret < 0) 163 if (ret < 0)
164 goto fail; 164 goto fail;
165 if (ret == 0) { 165 if (ret == 0) {
@@ -180,10 +180,10 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
180 goto insert; 180 goto insert;
181 } 181 }
182 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / 182 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
183 sizeof(struct btrfs_csum_item)) { 183 BTRFS_CRC32_SIZE) {
184 u32 diff = (csum_offset + 1) * sizeof(struct btrfs_csum_item); 184 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
185 diff = diff - btrfs_item_size(leaf->items + path->slots[0]); 185 diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
186 WARN_ON(diff != sizeof(struct btrfs_csum_item)); 186 WARN_ON(diff != BTRFS_CRC32_SIZE);
187 ret = btrfs_extend_item(trans, root, path, diff); 187 ret = btrfs_extend_item(trans, root, path, diff);
188 BUG_ON(ret); 188 BUG_ON(ret);
189 goto csum; 189 goto csum;
@@ -193,7 +193,7 @@ insert:
193 btrfs_release_path(root, path); 193 btrfs_release_path(root, path);
194 csum_offset = 0; 194 csum_offset = 0;
195 ret = btrfs_insert_empty_item(trans, root, path, &file_key, 195 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
196 sizeof(struct btrfs_csum_item)); 196 BTRFS_CRC32_SIZE);
197 if (ret != 0) { 197 if (ret != 0) {
198 printk("at insert for %Lu %u %Lu ret is %d\n", file_key.objectid, file_key.flags, file_key.offset, ret); 198 printk("at insert for %Lu %u %Lu ret is %d\n", file_key.objectid, file_key.flags, file_key.offset, ret);
199 WARN_ON(1); 199 WARN_ON(1);
@@ -203,10 +203,13 @@ csum:
203 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 203 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
204 struct btrfs_csum_item); 204 struct btrfs_csum_item);
205 ret = 0; 205 ret = 0;
206 item += csum_offset; 206 item = (struct btrfs_csum_item *)((unsigned char *)item +
207 csum_offset * BTRFS_CRC32_SIZE);
207found: 208found:
208 btrfs_check_bounds(item->csum, BTRFS_CSUM_SIZE, path->nodes[0]->b_data, root->fs_info->sb->s_blocksize); 209 btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
209 ret = btrfs_csum_data(root, data, len, item->csum); 210 path->nodes[0]->b_data,
211 root->fs_info->sb->s_blocksize);
212 ret = btrfs_csum_data(root, data, len, &item->csum);
210 btrfs_mark_buffer_dirty(path->nodes[0]); 213 btrfs_mark_buffer_dirty(path->nodes[0]);
211fail: 214fail:
212 btrfs_release_path(root, path); 215 btrfs_release_path(root, path);
@@ -222,7 +225,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root,
222 struct btrfs_key file_key; 225 struct btrfs_key file_key;
223 struct btrfs_path *path; 226 struct btrfs_path *path;
224 struct btrfs_csum_item *item; 227 struct btrfs_csum_item *item;
225 char result[BTRFS_CSUM_SIZE]; 228 char result[BTRFS_CRC32_SIZE];
226 229
227 path = btrfs_alloc_path(); 230 path = btrfs_alloc_path();
228 BUG_ON(!path); 231 BUG_ON(!path);
@@ -244,7 +247,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root,
244 247
245 ret = btrfs_csum_data(root, data, len, result); 248 ret = btrfs_csum_data(root, data, len, result);
246 WARN_ON(ret); 249 WARN_ON(ret);
247 if (memcmp(result, item->csum, BTRFS_CSUM_SIZE)) 250 if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE))
248 ret = 1; 251 ret = 1;
249fail: 252fail:
250 btrfs_release_path(root, path); 253 btrfs_release_path(root, path);