2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "transaction.h"
26 #include "print-tree.h"
29 #define MAX_CSUM_ITEMS(r,size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
30 sizeof(struct btrfs_item) * 2) / \
32 int btrfs_create_file(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, u64 dirid, u64 *objectid)
38 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 u64 objectid, u64 pos, u64 offset,
41 u64 disk_num_bytes, u64 num_bytes)
44 struct btrfs_file_extent_item *item;
45 struct btrfs_key file_key;
46 struct btrfs_path *path;
47 struct extent_buffer *leaf;
49 path = btrfs_alloc_path();
51 file_key.objectid = objectid;
52 file_key.offset = pos;
53 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
55 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
60 leaf = path->nodes[0];
61 item = btrfs_item_ptr(leaf, path->slots[0],
62 struct btrfs_file_extent_item);
63 btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
64 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
65 btrfs_set_file_extent_offset(leaf, item, 0);
66 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
67 btrfs_set_file_extent_generation(leaf, item, trans->transid);
68 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
69 btrfs_mark_buffer_dirty(leaf);
71 btrfs_free_path(path);
75 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
76 struct btrfs_root *root, u64 objectid,
77 u64 offset, char *buffer, size_t size)
80 struct btrfs_path *path;
81 struct extent_buffer *leaf;
83 struct btrfs_file_extent_item *ei;
88 path = btrfs_alloc_path();
92 key.objectid = objectid;
94 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
96 datasize = btrfs_file_extent_calc_inline_size(size);
97 ret = btrfs_insert_empty_item(trans, root, path, &key, datasize);
103 leaf = path->nodes[0];
104 ei = btrfs_item_ptr(leaf, path->slots[0],
105 struct btrfs_file_extent_item);
106 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
107 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
108 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
109 btrfs_set_file_extent_compression(leaf, ei, 0);
110 btrfs_set_file_extent_encryption(leaf, ei, 0);
111 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
113 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
114 write_extent_buffer(leaf, buffer, ptr, size);
115 btrfs_mark_buffer_dirty(leaf);
117 btrfs_free_path(path);
121 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
122 struct btrfs_root *root,
123 struct btrfs_path *path,
127 struct btrfs_key file_key;
128 struct btrfs_key found_key;
129 struct btrfs_csum_item *item;
130 struct extent_buffer *leaf;
133 btrfs_super_csum_size(&root->fs_info->super_copy);
136 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
137 file_key.offset = bytenr;
138 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
139 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
142 leaf = path->nodes[0];
145 if (path->slots[0] == 0)
148 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
149 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
152 csum_offset = (bytenr - found_key.offset) / root->sectorsize;
153 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
154 csums_in_item /= csum_size;
156 if (csum_offset >= csums_in_item) {
161 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
162 item = (struct btrfs_csum_item *)((unsigned char *)item +
163 csum_offset * csum_size);
171 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root,
173 struct btrfs_path *path, u64 objectid,
177 struct btrfs_key file_key;
178 int ins_len = mod < 0 ? -1 : 0;
181 file_key.objectid = objectid;
182 file_key.offset = offset;
183 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
184 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
188 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root, u64 alloc_end,
190 u64 bytenr, char *data, size_t len)
193 struct btrfs_key file_key;
194 struct btrfs_key found_key;
195 u64 next_offset = (u64)-1;
197 struct btrfs_path *path;
198 struct btrfs_csum_item *item;
199 struct extent_buffer *leaf = NULL;
201 u32 csum_result = ~(u32)0;
205 btrfs_super_csum_size(&root->fs_info->super_copy);
207 path = btrfs_alloc_path();
210 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
211 file_key.offset = bytenr;
212 file_key.type = BTRFS_EXTENT_CSUM_KEY;
214 item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
216 leaf = path->nodes[0];
222 /* we found one, but it isn't big enough yet */
223 leaf = path->nodes[0];
224 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
225 if ((item_size / csum_size) >= MAX_CSUM_ITEMS(root, csum_size)) {
226 /* already at max size, make a new one */
230 int slot = path->slots[0] + 1;
231 /* we didn't find a csum item, insert one */
232 nritems = btrfs_header_nritems(path->nodes[0]);
233 if (path->slots[0] >= nritems - 1) {
234 ret = btrfs_next_leaf(root, path);
241 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
242 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
243 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
247 next_offset = found_key.offset;
253 * at this point, we know the tree has an item, but it isn't big
254 * enough yet to put our csum in. Grow it
256 btrfs_release_path(root, path);
257 ret = btrfs_search_slot(trans, root, &file_key, path,
264 if (path->slots[0] == 0) {
268 leaf = path->nodes[0];
269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
270 csum_offset = (file_key.offset - found_key.offset) / root->sectorsize;
271 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
272 found_key.type != BTRFS_EXTENT_CSUM_KEY ||
273 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
276 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
278 u32 diff = (csum_offset + 1) * csum_size;
279 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
280 if (diff != csum_size)
282 ret = btrfs_extend_item(trans, root, path, diff);
288 btrfs_release_path(root, path);
291 u64 tmp = min(alloc_end, next_offset);
292 tmp -= file_key.offset;
293 tmp /= root->sectorsize;
294 tmp = max((u64)1, tmp);
295 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
296 ins_size = csum_size * tmp;
298 ins_size = csum_size;
300 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
309 leaf = path->nodes[0];
310 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
312 item = (struct btrfs_csum_item *)((unsigned char *)item +
313 csum_offset * csum_size);
315 csum_result = btrfs_csum_data(root, data, csum_result, len);
316 btrfs_csum_final(csum_result, (char *)&csum_result);
317 if (csum_result == 0) {
318 printk("csum result is 0 for block %llu\n",
319 (unsigned long long)bytenr);
322 write_extent_buffer(leaf, &csum_result, (unsigned long)item,
324 btrfs_mark_buffer_dirty(path->nodes[0]);
326 btrfs_release_path(root, path);
327 btrfs_free_path(path);
332 * helper function for csum removal, this expects the
333 * key to describe the csum pointed to by the path, and it expects
334 * the csum to overlap the range [bytenr, len]
336 * The csum should not be entirely contained in the range and the
337 * range should not be entirely contained in the csum.
339 * This calls btrfs_truncate_item with the correct args based on the
340 * overlap, and fixes up the key as required.
342 static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root,
344 struct btrfs_path *path,
345 struct btrfs_key *key,
348 struct extent_buffer *leaf;
350 btrfs_super_csum_size(&root->fs_info->super_copy);
352 u64 end_byte = bytenr + len;
353 u32 blocksize = root->sectorsize;
356 leaf = path->nodes[0];
357 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
358 csum_end *= root->sectorsize;
359 csum_end += key->offset;
361 if (key->offset < bytenr && csum_end <= end_byte) {
366 * A simple truncate off the end of the item
368 u32 new_size = (bytenr - key->offset) / blocksize;
369 new_size *= csum_size;
370 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
372 } else if (key->offset >= bytenr && csum_end > end_byte &&
373 end_byte > key->offset) {
378 * we need to truncate from the beginning of the csum
380 u32 new_size = (csum_end - end_byte) / blocksize;
381 new_size *= csum_size;
383 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
386 key->offset = end_byte;
387 ret = btrfs_set_item_key_safe(trans, root, path, key);
396 * deletes the csum items from the csum tree for a given
399 int btrfs_del_csums(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root, u64 bytenr, u64 len)
402 struct btrfs_path *path;
403 struct btrfs_key key;
404 u64 end_byte = bytenr + len;
406 struct extent_buffer *leaf;
409 btrfs_super_csum_size(&root->fs_info->super_copy);
410 int blocksize = root->sectorsize;
412 root = root->fs_info->csum_root;
414 path = btrfs_alloc_path();
417 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
418 key.offset = end_byte - 1;
419 key.type = BTRFS_EXTENT_CSUM_KEY;
421 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
423 if (path->slots[0] == 0)
427 leaf = path->nodes[0];
428 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
430 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
431 key.type != BTRFS_EXTENT_CSUM_KEY) {
435 if (key.offset >= end_byte)
438 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
439 csum_end *= blocksize;
440 csum_end += key.offset;
442 /* this csum ends before we start, we're done */
443 if (csum_end <= bytenr)
446 /* delete the entire item, it is inside our range */
447 if (key.offset >= bytenr && csum_end <= end_byte) {
448 ret = btrfs_del_item(trans, root, path);
450 } else if (key.offset < bytenr && csum_end > end_byte) {
451 unsigned long offset;
452 unsigned long shift_len;
453 unsigned long item_offset;
458 * Our bytes are in the middle of the csum,
459 * we need to split this item and insert a new one.
461 * But we can't drop the path because the
462 * csum could change, get removed, extended etc.
464 * The trick here is the max size of a csum item leaves
465 * enough room in the tree block for a single
466 * item header. So, we split the item in place,
467 * adding a new header pointing to the existing
468 * bytes. Then we loop around again and we have
469 * a nicely formed csum item that we can neatly
472 offset = (bytenr - key.offset) / blocksize;
475 shift_len = (len / blocksize) * csum_size;
477 item_offset = btrfs_item_ptr_offset(leaf,
480 memset_extent_buffer(leaf, 0, item_offset + offset,
485 * btrfs_split_item returns -EAGAIN when the
486 * item changed size or key
488 ret = btrfs_split_item(trans, root, path, &key, offset);
489 BUG_ON(ret && ret != -EAGAIN);
491 key.offset = end_byte - 1;
493 ret = truncate_one_csum(trans, root, path,
497 btrfs_release_path(root, path);
500 btrfs_free_path(path);