aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/file-item.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-03-29 15:15:27 -0400
committerDavid Woodhouse <dwmw2@hera.kernel.org>2007-03-29 15:15:27 -0400
commitf254e52c1ce550fdaa0d31f5e068f0d67c2485d4 (patch)
tree1232b7e16b6f0b1b1b150c5df396452cac5e3853 /fs/btrfs/file-item.c
parent75dfe3960e602e63ea42ac7a2a0520832b189ffa (diff)
Btrfs: verify csums on read
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/file-item.c')
-rw-r--r--fs/btrfs/file-item.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 5230a44cb19..2d2c23ca7cb 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -57,3 +57,63 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
57 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 57 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
58 return ret; 58 return ret;
59} 59}
60
61int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
62 struct btrfs_root *root,
63 u64 objectid, u64 offset,
64 char *data, size_t len)
65{
66 int ret;
67 struct btrfs_key file_key;
68 struct btrfs_path path;
69 struct btrfs_csum_item *item;
70
71 btrfs_init_path(&path);
72 file_key.objectid = objectid;
73 file_key.offset = offset;
74 file_key.flags = 0;
75 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
76 ret = btrfs_insert_empty_item(trans, root, &path, &file_key,
77 BTRFS_CSUM_SIZE);
78 if (ret != 0 && ret != -EEXIST)
79 goto fail;
80 item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
81 struct btrfs_csum_item);
82 ret = 0;
83 ret = btrfs_csum_data(root, data, len, item->csum);
84 mark_buffer_dirty(path.nodes[0]);
85fail:
86 btrfs_release_path(root, &path);
87 return ret;
88}
89
90int btrfs_csum_verify_file_block(struct btrfs_root *root,
91 u64 objectid, u64 offset,
92 char *data, size_t len)
93{
94 int ret;
95 struct btrfs_key file_key;
96 struct btrfs_path path;
97 struct btrfs_csum_item *item;
98 char result[BTRFS_CSUM_SIZE];
99
100 btrfs_init_path(&path);
101 file_key.objectid = objectid;
102 file_key.offset = offset;
103 file_key.flags = 0;
104 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
105 ret = btrfs_search_slot(NULL, root, &file_key, &path, 0, 0);
106 if (ret)
107 goto fail;
108 item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
109 struct btrfs_csum_item);
110 ret = 0;
111 ret = btrfs_csum_data(root, data, len, result);
112 WARN_ON(ret);
113 if (memcmp(result, item->csum, BTRFS_CSUM_SIZE))
114 ret = 1;
115fail:
116 btrfs_release_path(root, &path);
117 return ret;
118}
119