blob: d5a98827e38aca1ed219a1b2dceb2b8ade74209a [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Mason1e1d2702007-03-15 19:03:33 -04002#include "ctree.h"
Chris Masondee26a92007-03-26 16:00:06 -04003#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -04004#include "transaction.h"
Chris Mason1de037a2007-05-29 15:17:08 -04005#include "print-tree.h"
Chris Mason1e1d2702007-03-15 19:03:33 -04006
Chris Mason6567e832007-04-16 09:22:45 -04007#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Chris Mason509659c2007-05-10 12:36:17 -04008 sizeof(struct btrfs_item) * 2) / \
9 BTRFS_CRC32_SIZE) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -040010int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Chris Masondee26a92007-03-26 16:00:06 -040011 struct btrfs_root *root,
Chris Masonb18c6682007-04-17 13:26:50 -040012 u64 objectid, u64 pos,
Chris Mason3a686372007-05-24 13:35:57 -040013 u64 offset, u64 disk_num_blocks,
14 u64 num_blocks)
Chris Mason9f5fae22007-03-20 14:38:32 -040015{
Chris Masondee26a92007-03-26 16:00:06 -040016 int ret = 0;
17 struct btrfs_file_extent_item *item;
18 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040019 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -040020
Chris Mason5caf2a02007-04-02 11:20:42 -040021 path = btrfs_alloc_path();
22 BUG_ON(!path);
23 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -040024 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040025 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040026 file_key.flags = 0;
27 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
28
Chris Mason5caf2a02007-04-02 11:20:42 -040029 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040030 sizeof(*item));
Chris Mason9773a782007-03-27 11:26:26 -040031 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040032 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040033 struct btrfs_file_extent_item);
Chris Masonb18c6682007-04-17 13:26:50 -040034 btrfs_set_file_extent_disk_blocknr(item, offset);
Chris Mason3a686372007-05-24 13:35:57 -040035 btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks);
Chris Masondee26a92007-03-26 16:00:06 -040036 btrfs_set_file_extent_offset(item, 0);
Chris Masonb18c6682007-04-17 13:26:50 -040037 btrfs_set_file_extent_num_blocks(item, num_blocks);
Chris Mason71951f32007-03-27 09:16:29 -040038 btrfs_set_file_extent_generation(item, trans->transid);
Chris Mason236454d2007-04-19 13:37:44 -040039 btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG);
Chris Mason5caf2a02007-04-02 11:20:42 -040040 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona429e512007-04-18 16:15:28 -040041
Chris Mason5caf2a02007-04-02 11:20:42 -040042 btrfs_release_path(root, path);
43 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040044 return 0;
45}
Chris Masondee26a92007-03-26 16:00:06 -040046
Chris Masonb18c6682007-04-17 13:26:50 -040047struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
48 struct btrfs_root *root,
49 struct btrfs_path *path,
50 u64 objectid, u64 offset,
51 int cow)
Chris Mason6567e832007-04-16 09:22:45 -040052{
53 int ret;
54 struct btrfs_key file_key;
55 struct btrfs_key found_key;
56 struct btrfs_csum_item *item;
57 struct btrfs_leaf *leaf;
58 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040059 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040060
61 file_key.objectid = objectid;
62 file_key.offset = offset;
63 file_key.flags = 0;
64 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040065 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040066 if (ret < 0)
67 goto fail;
68 leaf = btrfs_buffer_leaf(path->nodes[0]);
69 if (ret > 0) {
70 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040071 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040072 goto fail;
73 path->slots[0]--;
74 btrfs_disk_key_to_cpu(&found_key,
75 &leaf->items[path->slots[0]].key);
76 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
77 found_key.objectid != objectid) {
78 goto fail;
79 }
80 csum_offset = (offset - found_key.offset) >>
81 root->fs_info->sb->s_blocksize_bits;
Chris Masona429e512007-04-18 16:15:28 -040082 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -040083 csums_in_item /= BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -040084
85 if (csum_offset >= csums_in_item) {
86 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -040087 goto fail;
88 }
89 }
90 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -040091 item = (struct btrfs_csum_item *)((unsigned char *)item +
92 csum_offset * BTRFS_CRC32_SIZE);
Chris Mason6567e832007-04-16 09:22:45 -040093 return item;
94fail:
95 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -040096 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -040097 return ERR_PTR(ret);
98}
99
100
Chris Masondee26a92007-03-26 16:00:06 -0400101int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root,
103 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400104 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400105{
106 int ret;
107 struct btrfs_key file_key;
108 int ins_len = mod < 0 ? -1 : 0;
109 int cow = mod != 0;
110
111 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400112 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400113 file_key.flags = 0;
114 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
115 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
116 return ret;
117}
Chris Masonf254e522007-03-29 15:15:27 -0400118
119int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root,
121 u64 objectid, u64 offset,
122 char *data, size_t len)
123{
124 int ret;
125 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400126 struct btrfs_key found_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400127 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400128 struct btrfs_csum_item *item;
Chris Mason6567e832007-04-16 09:22:45 -0400129 struct btrfs_leaf *leaf;
130 u64 csum_offset;
Chris Masonf254e522007-03-29 15:15:27 -0400131
Chris Mason5caf2a02007-04-02 11:20:42 -0400132 path = btrfs_alloc_path();
133 BUG_ON(!path);
Chris Masonb18c6682007-04-17 13:26:50 -0400134
Chris Masonf254e522007-03-29 15:15:27 -0400135 file_key.objectid = objectid;
136 file_key.offset = offset;
137 file_key.flags = 0;
138 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400139
140 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
141 if (!IS_ERR(item))
142 goto found;
143 ret = PTR_ERR(item);
144 if (ret == -EFBIG) {
145 u32 item_size;
146 /* we found one, but it isn't big enough yet */
147 leaf = btrfs_buffer_leaf(path->nodes[0]);
148 item_size = btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400149 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400150 /* already at max size, make a new one */
151 goto insert;
152 }
153 } else {
154 /* we didn't find a csum item, insert one */
155 goto insert;
156 }
157
158 /*
159 * at this point, we know the tree has an item, but it isn't big
160 * enough yet to put our csum in. Grow it
161 */
162 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400163 ret = btrfs_search_slot(trans, root, &file_key, path,
Chris Mason509659c2007-05-10 12:36:17 -0400164 BTRFS_CRC32_SIZE, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400165 if (ret < 0)
166 goto fail;
167 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400168 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400169 }
170 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400171 goto insert;
172 }
173 path->slots[0]--;
174 leaf = btrfs_buffer_leaf(path->nodes[0]);
175 btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
176 csum_offset = (offset - found_key.offset) >>
177 root->fs_info->sb->s_blocksize_bits;
178 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
179 found_key.objectid != objectid ||
180 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Mason6567e832007-04-16 09:22:45 -0400181 goto insert;
182 }
183 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
Chris Mason509659c2007-05-10 12:36:17 -0400184 BTRFS_CRC32_SIZE) {
185 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400186 diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason3a686372007-05-24 13:35:57 -0400187 if (diff != BTRFS_CRC32_SIZE)
188 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400189 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400190 BUG_ON(ret);
191 goto csum;
192 }
193
194insert:
Chris Masona429e512007-04-18 16:15:28 -0400195 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400196 csum_offset = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400197 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Mason509659c2007-05-10 12:36:17 -0400198 BTRFS_CRC32_SIZE);
Chris Masona429e512007-04-18 16:15:28 -0400199 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400200 WARN_ON(1);
Chris Masonf254e522007-03-29 15:15:27 -0400201 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400202 }
Chris Mason6567e832007-04-16 09:22:45 -0400203csum:
Chris Mason5caf2a02007-04-02 11:20:42 -0400204 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400205 struct btrfs_csum_item);
206 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400207 item = (struct btrfs_csum_item *)((unsigned char *)item +
208 csum_offset * BTRFS_CRC32_SIZE);
Chris Masonb18c6682007-04-17 13:26:50 -0400209found:
Chris Mason509659c2007-05-10 12:36:17 -0400210 btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
211 path->nodes[0]->b_data,
212 root->fs_info->sb->s_blocksize);
213 ret = btrfs_csum_data(root, data, len, &item->csum);
Chris Mason5caf2a02007-04-02 11:20:42 -0400214 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masonf254e522007-03-29 15:15:27 -0400215fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400216 btrfs_release_path(root, path);
217 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400218 return ret;
219}
220
Chris Mason1de037a2007-05-29 15:17:08 -0400221int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root, struct btrfs_path *path,
223 u64 isize)
224{
225 struct btrfs_key key;
226 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]);
227 int slot = path->slots[0];
228 int ret;
229 u32 new_item_size;
230 u64 new_item_span;
231 u64 blocks;
232
233 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
234 if (isize <= key.offset)
235 return 0;
236 new_item_span = isize - key.offset;
Chris Mason84f54cf2007-06-12 07:43:08 -0400237 blocks = (new_item_span + root->blocksize - 1) >>
238 root->fs_info->sb->s_blocksize_bits;
Chris Mason1de037a2007-05-29 15:17:08 -0400239 new_item_size = blocks * BTRFS_CRC32_SIZE;
240 if (new_item_size >= btrfs_item_size(leaf->items + slot))
241 return 0;
242 ret = btrfs_truncate_item(trans, root, path, new_item_size);
243 BUG_ON(ret);
244 return ret;
245}
246
Chris Masonf254e522007-03-29 15:15:27 -0400247int btrfs_csum_verify_file_block(struct btrfs_root *root,
248 u64 objectid, u64 offset,
249 char *data, size_t len)
250{
251 int ret;
252 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400253 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400254 struct btrfs_csum_item *item;
Chris Mason509659c2007-05-10 12:36:17 -0400255 char result[BTRFS_CRC32_SIZE];
Chris Masonf254e522007-03-29 15:15:27 -0400256
Chris Mason5caf2a02007-04-02 11:20:42 -0400257 path = btrfs_alloc_path();
258 BUG_ON(!path);
259 btrfs_init_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400260 file_key.objectid = objectid;
261 file_key.offset = offset;
262 file_key.flags = 0;
263 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Mason2da566e2007-04-02 15:43:21 -0400264 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -0400265
Chris Masonb18c6682007-04-17 13:26:50 -0400266 item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0);
Chris Mason6567e832007-04-16 09:22:45 -0400267 if (IS_ERR(item)) {
268 ret = PTR_ERR(item);
Chris Masona429e512007-04-18 16:15:28 -0400269 /* a csum that isn't present is a preallocated region. */
270 if (ret == -ENOENT || ret == -EFBIG)
Chris Mason3a686372007-05-24 13:35:57 -0400271 ret = -ENOENT;
Chris Masonf254e522007-03-29 15:15:27 -0400272 goto fail;
Chris Mason6567e832007-04-16 09:22:45 -0400273 }
274
Chris Masonf254e522007-03-29 15:15:27 -0400275 ret = btrfs_csum_data(root, data, len, result);
276 WARN_ON(ret);
Chris Mason509659c2007-05-10 12:36:17 -0400277 if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE))
Chris Masonf254e522007-03-29 15:15:27 -0400278 ret = 1;
279fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400280 btrfs_release_path(root, path);
281 btrfs_free_path(path);
Chris Mason2da566e2007-04-02 15:43:21 -0400282 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonf254e522007-03-29 15:15:27 -0400283 return ret;
284}
285