diff options
| author | Chris Mason <chris.mason@oracle.com> | 2007-03-28 19:44:27 -0400 |
|---|---|---|
| committer | David Woodhouse <dwmw2@hera.kernel.org> | 2007-03-28 19:44:27 -0400 |
| commit | 87cbda5c1f54aba67b1e318a4a4824c9e9f16324 (patch) | |
| tree | ba2917a8bcd00272c77c93236006fd8562566946 /fs | |
| parent | d98237b3ede7ab98892f7fa62201a13694c526e2 (diff) | |
Btrfs: sha256 csums on metadata
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs')
| -rw-r--r-- | fs/btrfs/ctree.h | 8 | ||||
| -rw-r--r-- | fs/btrfs/disk-io.c | 62 | ||||
| -rw-r--r-- | fs/btrfs/super.c | 4 |
3 files changed, 67 insertions, 7 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index b2ebed756c13..6ff87f44c5d8 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h | |||
| @@ -51,11 +51,11 @@ struct btrfs_key { | |||
| 51 | * every tree block (leaf or node) starts with this header. | 51 | * every tree block (leaf or node) starts with this header. |
| 52 | */ | 52 | */ |
| 53 | struct btrfs_header { | 53 | struct btrfs_header { |
| 54 | __le32 csum[8]; | ||
| 54 | u8 fsid[16]; /* FS specific uuid */ | 55 | u8 fsid[16]; /* FS specific uuid */ |
| 55 | __le64 blocknr; /* which block this node is supposed to live in */ | 56 | __le64 blocknr; /* which block this node is supposed to live in */ |
| 56 | __le64 generation; | 57 | __le64 generation; |
| 57 | __le64 parentid; /* objectid of the tree root */ | 58 | __le64 parentid; /* objectid of the tree root */ |
| 58 | __le32 csum; | ||
| 59 | __le32 ham; | 59 | __le32 ham; |
| 60 | __le16 nritems; | 60 | __le16 nritems; |
| 61 | __le16 flags; | 61 | __le16 flags; |
| @@ -75,9 +75,10 @@ struct buffer_head; | |||
| 75 | * it currently lacks any block count etc etc | 75 | * it currently lacks any block count etc etc |
| 76 | */ | 76 | */ |
| 77 | struct btrfs_super_block { | 77 | struct btrfs_super_block { |
| 78 | __le32 csum[8]; | ||
| 79 | /* the first 3 fields must match struct btrfs_header */ | ||
| 78 | u8 fsid[16]; /* FS specific uuid */ | 80 | u8 fsid[16]; /* FS specific uuid */ |
| 79 | __le64 blocknr; /* this block number */ | 81 | __le64 blocknr; /* this block number */ |
| 80 | __le32 csum; | ||
| 81 | __le64 magic; | 82 | __le64 magic; |
| 82 | __le32 blocksize; | 83 | __le32 blocksize; |
| 83 | __le64 generation; | 84 | __le64 generation; |
| @@ -217,6 +218,7 @@ struct btrfs_inode_map_item { | |||
| 217 | struct btrfs_disk_key key; | 218 | struct btrfs_disk_key key; |
| 218 | } __attribute__ ((__packed__)); | 219 | } __attribute__ ((__packed__)); |
| 219 | 220 | ||
| 221 | struct crypto_hash; | ||
| 220 | struct btrfs_fs_info { | 222 | struct btrfs_fs_info { |
| 221 | struct btrfs_root *fs_root; | 223 | struct btrfs_root *fs_root; |
| 222 | struct btrfs_root *extent_root; | 224 | struct btrfs_root *extent_root; |
| @@ -236,6 +238,8 @@ struct btrfs_fs_info { | |||
| 236 | struct inode *btree_inode; | 238 | struct inode *btree_inode; |
| 237 | struct mutex trans_mutex; | 239 | struct mutex trans_mutex; |
| 238 | struct mutex fs_mutex; | 240 | struct mutex fs_mutex; |
| 241 | struct crypto_hash *hash_tfm; | ||
| 242 | spinlock_t hash_lock; | ||
| 239 | }; | 243 | }; |
| 240 | 244 | ||
| 241 | /* | 245 | /* |
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 970103f2cacd..2afb7922b062 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | #include <linux/module.h> | 1 | #include <linux/module.h> |
| 2 | #include <linux/fs.h> | 2 | #include <linux/fs.h> |
| 3 | #include <linux/blkdev.h> | 3 | #include <linux/blkdev.h> |
| 4 | #include <linux/crypto.h> | ||
| 5 | #include <linux/scatterlist.h> | ||
| 4 | #include "ctree.h" | 6 | #include "ctree.h" |
| 5 | #include "disk-io.h" | 7 | #include "disk-io.h" |
| 6 | #include "transaction.h" | 8 | #include "transaction.h" |
| @@ -126,8 +128,51 @@ static int btree_get_block(struct inode *inode, sector_t iblock, | |||
| 126 | return 0; | 128 | return 0; |
| 127 | } | 129 | } |
| 128 | 130 | ||
| 131 | static int csum_tree_block(struct btrfs_root * root, struct buffer_head *bh, | ||
| 132 | int verify) | ||
| 133 | { | ||
| 134 | struct btrfs_node *node = btrfs_buffer_node(bh); | ||
| 135 | struct scatterlist sg; | ||
| 136 | struct crypto_hash *tfm = root->fs_info->hash_tfm; | ||
| 137 | struct hash_desc desc; | ||
| 138 | int ret; | ||
| 139 | char result[32]; | ||
| 140 | |||
| 141 | desc.tfm = tfm; | ||
| 142 | desc.flags = 0; | ||
| 143 | sg_init_one(&sg, bh->b_data + 32, bh->b_size - 32); | ||
| 144 | spin_lock(&root->fs_info->hash_lock); | ||
| 145 | ret = crypto_hash_digest(&desc, &sg, bh->b_size - 32, result); | ||
| 146 | spin_unlock(&root->fs_info->hash_lock); | ||
| 147 | if (ret) { | ||
| 148 | printk("sha256 digest failed\n"); | ||
| 149 | } | ||
| 150 | if (verify) { | ||
| 151 | if (memcmp(node->header.csum, result, sizeof(result))) | ||
| 152 | printk("csum verify failed on %Lu\n", bh->b_blocknr); | ||
| 153 | return -EINVAL; | ||
| 154 | } else | ||
| 155 | memcpy(node->header.csum, result, sizeof(node->header.csum)); | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 129 | static int btree_writepage(struct page *page, struct writeback_control *wbc) | 159 | static int btree_writepage(struct page *page, struct writeback_control *wbc) |
| 130 | { | 160 | { |
| 161 | struct buffer_head *bh; | ||
| 162 | struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb); | ||
| 163 | struct buffer_head *head; | ||
| 164 | |||
| 165 | if (!page_has_buffers(page)) { | ||
| 166 | create_empty_buffers(page, root->fs_info->sb->s_blocksize, | ||
| 167 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | ||
| 168 | } | ||
| 169 | head = page_buffers(page); | ||
| 170 | bh = head; | ||
| 171 | do { | ||
| 172 | if (buffer_dirty(bh)) | ||
| 173 | csum_tree_block(root, bh, 0); | ||
| 174 | bh = bh->b_this_page; | ||
| 175 | } while (bh != head); | ||
| 131 | return block_write_full_page(page, btree_get_block, wbc); | 176 | return block_write_full_page(page, btree_get_block, wbc); |
| 132 | } | 177 | } |
| 133 | 178 | ||
| @@ -157,6 +202,7 @@ struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr) | |||
| 157 | wait_on_buffer(bh); | 202 | wait_on_buffer(bh); |
| 158 | if (!buffer_uptodate(bh)) | 203 | if (!buffer_uptodate(bh)) |
| 159 | goto fail; | 204 | goto fail; |
| 205 | csum_tree_block(root, bh, 1); | ||
| 160 | } else { | 206 | } else { |
| 161 | unlock_buffer(bh); | 207 | unlock_buffer(bh); |
| 162 | } | 208 | } |
| @@ -233,8 +279,9 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
| 233 | GFP_NOFS); | 279 | GFP_NOFS); |
| 234 | int ret; | 280 | int ret; |
| 235 | 281 | ||
| 236 | if (!btrfs_super_root(disk_super)) | 282 | if (!btrfs_super_root(disk_super)) { |
| 237 | return NULL; | 283 | return NULL; |
| 284 | } | ||
| 238 | init_bit_radix(&fs_info->pinned_radix); | 285 | init_bit_radix(&fs_info->pinned_radix); |
| 239 | init_bit_radix(&fs_info->pending_del_radix); | 286 | init_bit_radix(&fs_info->pending_del_radix); |
| 240 | sb_set_blocksize(sb, sb_buffer->b_size); | 287 | sb_set_blocksize(sb, sb_buffer->b_size); |
| @@ -252,6 +299,12 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
| 252 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; | 299 | fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size; |
| 253 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; | 300 | fs_info->btree_inode->i_mapping->a_ops = &btree_aops; |
| 254 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); | 301 | mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); |
| 302 | fs_info->hash_tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC); | ||
| 303 | if (!fs_info->hash_tfm) { | ||
| 304 | printk("failed to allocate sha256 hash\n"); | ||
| 305 | return NULL; | ||
| 306 | } | ||
| 307 | spin_lock_init(&fs_info->hash_lock); | ||
| 255 | 308 | ||
| 256 | mutex_init(&fs_info->trans_mutex); | 309 | mutex_init(&fs_info->trans_mutex); |
| 257 | mutex_init(&fs_info->fs_mutex); | 310 | mutex_init(&fs_info->fs_mutex); |
| @@ -262,9 +315,10 @@ struct btrfs_root *open_ctree(struct super_block *sb, | |||
| 262 | 315 | ||
| 263 | fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr); | 316 | fs_info->sb_buffer = read_tree_block(tree_root, sb_buffer->b_blocknr); |
| 264 | 317 | ||
| 265 | if (!fs_info->sb_buffer) | 318 | if (!fs_info->sb_buffer) { |
| 319 | printk("failed2\n"); | ||
| 266 | return NULL; | 320 | return NULL; |
| 267 | 321 | } | |
| 268 | brelse(sb_buffer); | 322 | brelse(sb_buffer); |
| 269 | sb_buffer = NULL; | 323 | sb_buffer = NULL; |
| 270 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; | 324 | disk_super = (struct btrfs_super_block *)fs_info->sb_buffer->b_data; |
| @@ -300,6 +354,7 @@ int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root | |||
| 300 | root->fs_info->tree_root->node->b_blocknr); | 354 | root->fs_info->tree_root->node->b_blocknr); |
| 301 | lock_buffer(bh); | 355 | lock_buffer(bh); |
| 302 | clear_buffer_dirty(bh); | 356 | clear_buffer_dirty(bh); |
| 357 | csum_tree_block(root, bh, 0); | ||
| 303 | bh->b_end_io = end_buffer_write_sync; | 358 | bh->b_end_io = end_buffer_write_sync; |
| 304 | get_bh(bh); | 359 | get_bh(bh); |
| 305 | submit_bh(WRITE, bh); | 360 | submit_bh(WRITE, bh); |
| @@ -338,6 +393,7 @@ int close_ctree(struct btrfs_root *root) | |||
| 338 | root->fs_info->tree_root->node); | 393 | root->fs_info->tree_root->node); |
| 339 | btrfs_block_release(root, root->commit_root); | 394 | btrfs_block_release(root, root->commit_root); |
| 340 | btrfs_block_release(root, root->fs_info->sb_buffer); | 395 | btrfs_block_release(root, root->fs_info->sb_buffer); |
| 396 | crypto_free_hash(root->fs_info->hash_tfm); | ||
| 341 | iput(root->fs_info->btree_inode); | 397 | iput(root->fs_info->btree_inode); |
| 342 | kfree(root->fs_info->extent_root); | 398 | kfree(root->fs_info->extent_root); |
| 343 | kfree(root->fs_info->inode_root); | 399 | kfree(root->fs_info->inode_root); |
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 8f07f462236a..151c1002d74e 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c | |||
| @@ -473,13 +473,13 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent) | |||
| 473 | } | 473 | } |
| 474 | disk_super = (struct btrfs_super_block *)bh->b_data; | 474 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 475 | root = open_ctree(sb, bh, disk_super); | 475 | root = open_ctree(sb, bh, disk_super); |
| 476 | sb->s_fs_info = root; | ||
| 477 | disk_super = root->fs_info->disk_super; | ||
| 478 | 476 | ||
| 479 | if (!root) { | 477 | if (!root) { |
| 480 | printk("btrfs: open_ctree failed\n"); | 478 | printk("btrfs: open_ctree failed\n"); |
| 481 | return -EIO; | 479 | return -EIO; |
| 482 | } | 480 | } |
| 481 | sb->s_fs_info = root; | ||
| 482 | disk_super = root->fs_info->disk_super; | ||
| 483 | printk("read in super total blocks %Lu root %Lu\n", | 483 | printk("read in super total blocks %Lu root %Lu\n", |
| 484 | btrfs_super_total_blocks(disk_super), | 484 | btrfs_super_total_blocks(disk_super), |
| 485 | btrfs_super_root_dir(disk_super)); | 485 | btrfs_super_root_dir(disk_super)); |
