blob: 10e4cf08e9ed1843f65b82995a19a4373480fa80 [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 Mason1e1d2702007-03-15 19:03:33 -04005
Chris Mason6567e832007-04-16 09:22:45 -04006#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Chris Masona429e512007-04-18 16:15:28 -04007 sizeof(struct btrfs_item) * 2) / \
Chris Mason6567e832007-04-16 09:22:45 -04008 sizeof(struct btrfs_csum_item)) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -04009int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Chris Masondee26a92007-03-26 16:00:06 -040010 struct btrfs_root *root,
Chris Masonb18c6682007-04-17 13:26:50 -040011 u64 objectid, u64 pos,
12 u64 offset, u64 num_blocks)
Chris Mason9f5fae22007-03-20 14:38:32 -040013{
Chris Masondee26a92007-03-26 16:00:06 -040014 int ret = 0;
15 struct btrfs_file_extent_item *item;
16 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040017 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -040018
Chris Mason5caf2a02007-04-02 11:20:42 -040019 path = btrfs_alloc_path();
20 BUG_ON(!path);
21 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -040022 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040023 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040024 file_key.flags = 0;
25 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
26
Chris Mason5caf2a02007-04-02 11:20:42 -040027 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040028 sizeof(*item));
Chris Mason9773a782007-03-27 11:26:26 -040029 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040030 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040031 struct btrfs_file_extent_item);
Chris Masonb18c6682007-04-17 13:26:50 -040032 btrfs_set_file_extent_disk_blocknr(item, offset);
33 btrfs_set_file_extent_disk_num_blocks(item, num_blocks);
Chris Masondee26a92007-03-26 16:00:06 -040034 btrfs_set_file_extent_offset(item, 0);
Chris Masonb18c6682007-04-17 13:26:50 -040035 btrfs_set_file_extent_num_blocks(item, num_blocks);
Chris Mason71951f32007-03-27 09:16:29 -040036 btrfs_set_file_extent_generation(item, trans->transid);
Chris Mason236454d2007-04-19 13:37:44 -040037 btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG);
Chris Mason5caf2a02007-04-02 11:20:42 -040038 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona429e512007-04-18 16:15:28 -040039
Chris Mason5caf2a02007-04-02 11:20:42 -040040 btrfs_release_path(root, path);
41 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -040042 return 0;
43}
Chris Masondee26a92007-03-26 16:00:06 -040044
Chris Masonb18c6682007-04-17 13:26:50 -040045struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_path *path,
48 u64 objectid, u64 offset,
49 int cow)
Chris Mason6567e832007-04-16 09:22:45 -040050{
51 int ret;
52 struct btrfs_key file_key;
53 struct btrfs_key found_key;
54 struct btrfs_csum_item *item;
55 struct btrfs_leaf *leaf;
56 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040057 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040058
59 file_key.objectid = objectid;
60 file_key.offset = offset;
61 file_key.flags = 0;
62 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040063 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040064 if (ret < 0)
65 goto fail;
66 leaf = btrfs_buffer_leaf(path->nodes[0]);
67 if (ret > 0) {
68 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040069 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040070 goto fail;
71 path->slots[0]--;
72 btrfs_disk_key_to_cpu(&found_key,
73 &leaf->items[path->slots[0]].key);
74 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
75 found_key.objectid != objectid) {
76 goto fail;
77 }
78 csum_offset = (offset - found_key.offset) >>
79 root->fs_info->sb->s_blocksize_bits;
Chris Masona429e512007-04-18 16:15:28 -040080 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
81 csums_in_item /= sizeof(struct btrfs_csum_item);
82
83 if (csum_offset >= csums_in_item) {
84 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -040085 goto fail;
86 }
87 }
88 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
89 item += csum_offset;
90 return item;
91fail:
92 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -040093 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -040094 return ERR_PTR(ret);
95}
96
97
Chris Masondee26a92007-03-26 16:00:06 -040098int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
99 struct btrfs_root *root,
100 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400101 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400102{
103 int ret;
104 struct btrfs_key file_key;
105 int ins_len = mod < 0 ? -1 : 0;
106 int cow = mod != 0;
107
108 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400109 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400110 file_key.flags = 0;
111 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
112 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
113 return ret;
114}
Chris Masonf254e522007-03-29 15:15:27 -0400115
116int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
117 struct btrfs_root *root,
118 u64 objectid, u64 offset,
119 char *data, size_t len)
120{
121 int ret;
122 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400123 struct btrfs_key found_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400124 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400125 struct btrfs_csum_item *item;
Chris Mason6567e832007-04-16 09:22:45 -0400126 struct btrfs_leaf *leaf;
127 u64 csum_offset;
Chris Masonf254e522007-03-29 15:15:27 -0400128
Chris Mason5caf2a02007-04-02 11:20:42 -0400129 path = btrfs_alloc_path();
130 BUG_ON(!path);
Chris Masonb18c6682007-04-17 13:26:50 -0400131
Chris Masonf254e522007-03-29 15:15:27 -0400132 file_key.objectid = objectid;
133 file_key.offset = offset;
134 file_key.flags = 0;
135 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400136
137 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
138 if (!IS_ERR(item))
139 goto found;
140 ret = PTR_ERR(item);
141 if (ret == -EFBIG) {
142 u32 item_size;
143 /* we found one, but it isn't big enough yet */
144 leaf = btrfs_buffer_leaf(path->nodes[0]);
145 item_size = btrfs_item_size(leaf->items + path->slots[0]);
146 if ((item_size / sizeof(struct btrfs_csum_item)) >=
147 MAX_CSUM_ITEMS(root)) {
148 /* already at max size, make a new one */
149 goto insert;
150 }
151 } else {
152 /* we didn't find a csum item, insert one */
153 goto insert;
154 }
155
156 /*
157 * at this point, we know the tree has an item, but it isn't big
158 * enough yet to put our csum in. Grow it
159 */
160 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400161 ret = btrfs_search_slot(trans, root, &file_key, path,
162 sizeof(struct btrfs_csum_item), 1);
163 if (ret < 0)
164 goto fail;
165 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400166 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400167 }
168 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400169 goto insert;
170 }
171 path->slots[0]--;
172 leaf = btrfs_buffer_leaf(path->nodes[0]);
173 btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
174 csum_offset = (offset - found_key.offset) >>
175 root->fs_info->sb->s_blocksize_bits;
176 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
177 found_key.objectid != objectid ||
178 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400179 WARN_ON(1);
Chris Mason6567e832007-04-16 09:22:45 -0400180 goto insert;
181 }
182 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
183 sizeof(struct btrfs_csum_item)) {
Chris Masona429e512007-04-18 16:15:28 -0400184 u32 diff = (csum_offset + 1) * sizeof(struct btrfs_csum_item);
185 diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
186 WARN_ON(diff != sizeof(struct btrfs_csum_item));
187 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400188 BUG_ON(ret);
189 goto csum;
190 }
191
192insert:
Chris Masona429e512007-04-18 16:15:28 -0400193 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400194 csum_offset = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400195 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Mason6567e832007-04-16 09:22:45 -0400196 sizeof(struct btrfs_csum_item));
Chris Masona429e512007-04-18 16:15:28 -0400197 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);
199 WARN_ON(1);
Chris Masonf254e522007-03-29 15:15:27 -0400200 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400201 }
Chris Mason6567e832007-04-16 09:22:45 -0400202csum:
Chris Mason5caf2a02007-04-02 11:20:42 -0400203 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400204 struct btrfs_csum_item);
205 ret = 0;
Chris Mason6567e832007-04-16 09:22:45 -0400206 item += csum_offset;
Chris Masonb18c6682007-04-17 13:26:50 -0400207found:
Chris Masona429e512007-04-18 16:15:28 -0400208 btrfs_check_bounds(item->csum, BTRFS_CSUM_SIZE, path->nodes[0]->b_data, root->fs_info->sb->s_blocksize);
Chris Masonf254e522007-03-29 15:15:27 -0400209 ret = btrfs_csum_data(root, data, len, item->csum);
Chris Mason5caf2a02007-04-02 11:20:42 -0400210 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masonf254e522007-03-29 15:15:27 -0400211fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400212 btrfs_release_path(root, path);
213 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400214 return ret;
215}
216
217int btrfs_csum_verify_file_block(struct btrfs_root *root,
218 u64 objectid, u64 offset,
219 char *data, size_t len)
220{
221 int ret;
222 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400223 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400224 struct btrfs_csum_item *item;
225 char result[BTRFS_CSUM_SIZE];
226
Chris Mason5caf2a02007-04-02 11:20:42 -0400227 path = btrfs_alloc_path();
228 BUG_ON(!path);
229 btrfs_init_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400230 file_key.objectid = objectid;
231 file_key.offset = offset;
232 file_key.flags = 0;
233 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Mason2da566e2007-04-02 15:43:21 -0400234 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason6567e832007-04-16 09:22:45 -0400235
Chris Masonb18c6682007-04-17 13:26:50 -0400236 item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0);
Chris Mason6567e832007-04-16 09:22:45 -0400237 if (IS_ERR(item)) {
238 ret = PTR_ERR(item);
Chris Masona429e512007-04-18 16:15:28 -0400239 /* a csum that isn't present is a preallocated region. */
240 if (ret == -ENOENT || ret == -EFBIG)
241 ret = 1;
Chris Masonf254e522007-03-29 15:15:27 -0400242 goto fail;
Chris Mason6567e832007-04-16 09:22:45 -0400243 }
244
Chris Masonf254e522007-03-29 15:15:27 -0400245 ret = btrfs_csum_data(root, data, len, result);
246 WARN_ON(ret);
247 if (memcmp(result, item->csum, BTRFS_CSUM_SIZE))
248 ret = 1;
249fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400250 btrfs_release_path(root, path);
251 btrfs_free_path(path);
Chris Mason2da566e2007-04-02 15:43:21 -0400252 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonf254e522007-03-29 15:15:27 -0400253 return ret;
254}
255