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.
19 #include <linux/bio.h>
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
24 #include "transaction.h"
25 #include "print-tree.h"
27 #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
28 sizeof(struct btrfs_item) * 2) / \
31 #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
32 sizeof(struct btrfs_ordered_sum)) / \
33 sizeof(struct btrfs_sector_sum) * \
34 (r)->sectorsize - (r)->sectorsize)
36 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 objectid, u64 pos,
39 u64 disk_offset, u64 disk_num_bytes,
40 u64 num_bytes, u64 offset, u64 ram_bytes,
41 u8 compression, u8 encryption, u16 other_encoding)
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 path->leave_spinning = 1;
56 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
61 leaf = path->nodes[0];
62 item = btrfs_item_ptr(leaf, path->slots[0],
63 struct btrfs_file_extent_item);
64 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
65 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
66 btrfs_set_file_extent_offset(leaf, item, offset);
67 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
68 btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
69 btrfs_set_file_extent_generation(leaf, item, trans->transid);
70 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
71 btrfs_set_file_extent_compression(leaf, item, compression);
72 btrfs_set_file_extent_encryption(leaf, item, encryption);
73 btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
75 btrfs_mark_buffer_dirty(leaf);
77 btrfs_free_path(path);
81 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
82 struct btrfs_root *root,
83 struct btrfs_path *path,
87 struct btrfs_key file_key;
88 struct btrfs_key found_key;
89 struct btrfs_csum_item *item;
90 struct extent_buffer *leaf;
93 btrfs_super_csum_size(&root->fs_info->super_copy);
96 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
97 file_key.offset = bytenr;
98 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
99 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
102 leaf = path->nodes[0];
105 if (path->slots[0] == 0)
108 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
109 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
112 csum_offset = (bytenr - found_key.offset) >>
113 root->fs_info->sb->s_blocksize_bits;
114 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
115 csums_in_item /= csum_size;
117 if (csum_offset >= csums_in_item) {
122 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
123 item = (struct btrfs_csum_item *)((unsigned char *)item +
124 csum_offset * csum_size);
133 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root,
135 struct btrfs_path *path, u64 objectid,
139 struct btrfs_key file_key;
140 int ins_len = mod < 0 ? -1 : 0;
143 file_key.objectid = objectid;
144 file_key.offset = offset;
145 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
146 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
151 int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
152 struct bio *bio, u32 *dst)
155 struct bio_vec *bvec = bio->bi_io_vec;
158 u64 item_start_offset = 0;
159 u64 item_last_offset = 0;
163 btrfs_super_csum_size(&root->fs_info->super_copy);
165 struct btrfs_path *path;
166 struct btrfs_csum_item *item = NULL;
167 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
169 path = btrfs_alloc_path();
170 if (bio->bi_size > PAGE_CACHE_SIZE * 8)
173 WARN_ON(bio->bi_vcnt <= 0);
175 disk_bytenr = (u64)bio->bi_sector << 9;
176 while (bio_index < bio->bi_vcnt) {
177 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
178 ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
182 if (!item || disk_bytenr < item_start_offset ||
183 disk_bytenr >= item_last_offset) {
184 struct btrfs_key found_key;
188 btrfs_release_path(root, path);
189 item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
190 path, disk_bytenr, 0);
193 if (ret == -ENOENT || ret == -EFBIG)
196 if (BTRFS_I(inode)->root->root_key.objectid ==
197 BTRFS_DATA_RELOC_TREE_OBJECTID) {
198 set_extent_bits(io_tree, offset,
199 offset + bvec->bv_len - 1,
200 EXTENT_NODATASUM, GFP_NOFS);
202 printk(KERN_INFO "btrfs no csum found "
203 "for inode %lu start %llu\n",
205 (unsigned long long)offset);
208 btrfs_release_path(root, path);
211 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
214 item_start_offset = found_key.offset;
215 item_size = btrfs_item_size_nr(path->nodes[0],
217 item_last_offset = item_start_offset +
218 (item_size / csum_size) *
220 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
221 struct btrfs_csum_item);
224 * this byte range must be able to fit inside
225 * a single leaf so it will also fit inside a u32
227 diff = disk_bytenr - item_start_offset;
228 diff = diff / root->sectorsize;
229 diff = diff * csum_size;
231 read_extent_buffer(path->nodes[0], &sum,
232 ((unsigned long)item) + diff,
238 set_state_private(io_tree, offset, sum);
239 disk_bytenr += bvec->bv_len;
243 btrfs_free_path(path);
247 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
248 struct list_head *list)
250 struct btrfs_key key;
251 struct btrfs_path *path;
252 struct extent_buffer *leaf;
253 struct btrfs_ordered_sum *sums;
254 struct btrfs_sector_sum *sector_sum;
255 struct btrfs_csum_item *item;
256 unsigned long offset;
260 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
262 path = btrfs_alloc_path();
265 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
267 key.type = BTRFS_EXTENT_CSUM_KEY;
269 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
272 if (ret > 0 && path->slots[0] > 0) {
273 leaf = path->nodes[0];
274 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
275 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
276 key.type == BTRFS_EXTENT_CSUM_KEY) {
277 offset = (start - key.offset) >>
278 root->fs_info->sb->s_blocksize_bits;
279 if (offset * csum_size <
280 btrfs_item_size_nr(leaf, path->slots[0] - 1))
285 while (start <= end) {
286 leaf = path->nodes[0];
287 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
288 ret = btrfs_next_leaf(root, path);
293 leaf = path->nodes[0];
296 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
297 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
298 key.type != BTRFS_EXTENT_CSUM_KEY)
301 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
302 if (key.offset > end)
305 if (key.offset > start)
308 size = btrfs_item_size_nr(leaf, path->slots[0]);
309 csum_end = key.offset + (size / csum_size) * root->sectorsize;
310 if (csum_end <= start) {
315 csum_end = min(csum_end, end + 1);
316 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
317 struct btrfs_csum_item);
318 while (start < csum_end) {
319 size = min_t(size_t, csum_end - start,
320 MAX_ORDERED_SUM_BYTES(root));
321 sums = kzalloc(btrfs_ordered_sum_size(root, size),
325 sector_sum = sums->sums;
326 sums->bytenr = start;
329 offset = (start - key.offset) >>
330 root->fs_info->sb->s_blocksize_bits;
334 read_extent_buffer(path->nodes[0],
336 ((unsigned long)item) +
338 sector_sum->bytenr = start;
340 size -= root->sectorsize;
341 start += root->sectorsize;
345 list_add_tail(&sums->list, list);
351 btrfs_free_path(path);
355 int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
356 struct bio *bio, u64 file_start, int contig)
358 struct btrfs_ordered_sum *sums;
359 struct btrfs_sector_sum *sector_sum;
360 struct btrfs_ordered_extent *ordered;
362 struct bio_vec *bvec = bio->bi_io_vec;
364 unsigned long total_bytes = 0;
365 unsigned long this_sum_bytes = 0;
369 WARN_ON(bio->bi_vcnt <= 0);
370 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
374 sector_sum = sums->sums;
375 disk_bytenr = (u64)bio->bi_sector << 9;
376 sums->len = bio->bi_size;
377 INIT_LIST_HEAD(&sums->list);
382 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
384 ordered = btrfs_lookup_ordered_extent(inode, offset);
386 sums->bytenr = ordered->start;
388 while (bio_index < bio->bi_vcnt) {
390 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
392 if (!contig && (offset >= ordered->file_offset + ordered->len ||
393 offset < ordered->file_offset)) {
394 unsigned long bytes_left;
395 sums->len = this_sum_bytes;
397 btrfs_add_ordered_sum(inode, ordered, sums);
398 btrfs_put_ordered_extent(ordered);
400 bytes_left = bio->bi_size - total_bytes;
402 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
405 sector_sum = sums->sums;
406 sums->len = bytes_left;
407 ordered = btrfs_lookup_ordered_extent(inode, offset);
409 sums->bytenr = ordered->start;
412 data = kmap_atomic(bvec->bv_page, KM_USER0);
413 sector_sum->sum = ~(u32)0;
414 sector_sum->sum = btrfs_csum_data(root,
415 data + bvec->bv_offset,
418 kunmap_atomic(data, KM_USER0);
419 btrfs_csum_final(sector_sum->sum,
420 (char *)§or_sum->sum);
421 sector_sum->bytenr = disk_bytenr;
425 total_bytes += bvec->bv_len;
426 this_sum_bytes += bvec->bv_len;
427 disk_bytenr += bvec->bv_len;
428 offset += bvec->bv_len;
432 btrfs_add_ordered_sum(inode, ordered, sums);
433 btrfs_put_ordered_extent(ordered);
438 * helper function for csum removal, this expects the
439 * key to describe the csum pointed to by the path, and it expects
440 * the csum to overlap the range [bytenr, len]
442 * The csum should not be entirely contained in the range and the
443 * range should not be entirely contained in the csum.
445 * This calls btrfs_truncate_item with the correct args based on the
446 * overlap, and fixes up the key as required.
448 static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
449 struct btrfs_root *root,
450 struct btrfs_path *path,
451 struct btrfs_key *key,
454 struct extent_buffer *leaf;
456 btrfs_super_csum_size(&root->fs_info->super_copy);
458 u64 end_byte = bytenr + len;
459 u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
462 leaf = path->nodes[0];
463 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
464 csum_end <<= root->fs_info->sb->s_blocksize_bits;
465 csum_end += key->offset;
467 if (key->offset < bytenr && csum_end <= end_byte) {
472 * A simple truncate off the end of the item
474 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
475 new_size *= csum_size;
476 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
478 } else if (key->offset >= bytenr && csum_end > end_byte &&
479 end_byte > key->offset) {
484 * we need to truncate from the beginning of the csum
486 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
487 new_size *= csum_size;
489 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
492 key->offset = end_byte;
493 ret = btrfs_set_item_key_safe(trans, root, path, key);
502 * deletes the csum items from the csum tree for a given
505 int btrfs_del_csums(struct btrfs_trans_handle *trans,
506 struct btrfs_root *root, u64 bytenr, u64 len)
508 struct btrfs_path *path;
509 struct btrfs_key key;
510 u64 end_byte = bytenr + len;
512 struct extent_buffer *leaf;
515 btrfs_super_csum_size(&root->fs_info->super_copy);
516 int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
518 root = root->fs_info->csum_root;
520 path = btrfs_alloc_path();
523 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
524 key.offset = end_byte - 1;
525 key.type = BTRFS_EXTENT_CSUM_KEY;
527 path->leave_spinning = 1;
528 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
530 if (path->slots[0] == 0)
534 leaf = path->nodes[0];
535 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
537 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
538 key.type != BTRFS_EXTENT_CSUM_KEY) {
542 if (key.offset >= end_byte)
545 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
546 csum_end <<= blocksize_bits;
547 csum_end += key.offset;
549 /* this csum ends before we start, we're done */
550 if (csum_end <= bytenr)
553 /* delete the entire item, it is inside our range */
554 if (key.offset >= bytenr && csum_end <= end_byte) {
555 ret = btrfs_del_item(trans, root, path);
557 if (key.offset == bytenr)
559 } else if (key.offset < bytenr && csum_end > end_byte) {
560 unsigned long offset;
561 unsigned long shift_len;
562 unsigned long item_offset;
567 * Our bytes are in the middle of the csum,
568 * we need to split this item and insert a new one.
570 * But we can't drop the path because the
571 * csum could change, get removed, extended etc.
573 * The trick here is the max size of a csum item leaves
574 * enough room in the tree block for a single
575 * item header. So, we split the item in place,
576 * adding a new header pointing to the existing
577 * bytes. Then we loop around again and we have
578 * a nicely formed csum item that we can neatly
581 offset = (bytenr - key.offset) >> blocksize_bits;
584 shift_len = (len >> blocksize_bits) * csum_size;
586 item_offset = btrfs_item_ptr_offset(leaf,
589 memset_extent_buffer(leaf, 0, item_offset + offset,
594 * btrfs_split_item returns -EAGAIN when the
595 * item changed size or key
597 ret = btrfs_split_item(trans, root, path, &key, offset);
598 BUG_ON(ret && ret != -EAGAIN);
600 key.offset = end_byte - 1;
602 ret = truncate_one_csum(trans, root, path,
605 if (key.offset < bytenr)
608 btrfs_release_path(root, path);
611 btrfs_free_path(path);
615 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
616 struct btrfs_root *root,
617 struct btrfs_ordered_sum *sums)
621 struct btrfs_key file_key;
622 struct btrfs_key found_key;
626 struct btrfs_path *path;
627 struct btrfs_csum_item *item;
628 struct btrfs_csum_item *item_end;
629 struct extent_buffer *leaf = NULL;
631 struct btrfs_sector_sum *sector_sum;
636 unsigned long map_len;
637 unsigned long map_start;
639 btrfs_super_csum_size(&root->fs_info->super_copy);
641 path = btrfs_alloc_path();
643 sector_sum = sums->sums;
645 next_offset = (u64)-1;
647 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
648 file_key.offset = sector_sum->bytenr;
649 bytenr = sector_sum->bytenr;
650 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
652 item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
654 leaf = path->nodes[0];
661 /* we found one, but it isn't big enough yet */
662 leaf = path->nodes[0];
663 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
664 if ((item_size / csum_size) >=
665 MAX_CSUM_ITEMS(root, csum_size)) {
666 /* already at max size, make a new one */
670 int slot = path->slots[0] + 1;
671 /* we didn't find a csum item, insert one */
672 nritems = btrfs_header_nritems(path->nodes[0]);
673 if (path->slots[0] >= nritems - 1) {
674 ret = btrfs_next_leaf(root, path);
681 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
682 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
683 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
687 next_offset = found_key.offset;
693 * at this point, we know the tree has an item, but it isn't big
694 * enough yet to put our csum in. Grow it
696 btrfs_release_path(root, path);
697 ret = btrfs_search_slot(trans, root, &file_key, path,
703 if (path->slots[0] == 0)
708 leaf = path->nodes[0];
709 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
710 csum_offset = (bytenr - found_key.offset) >>
711 root->fs_info->sb->s_blocksize_bits;
713 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
714 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
715 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
719 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
721 u32 diff = (csum_offset + 1) * csum_size;
724 * is the item big enough already? we dropped our lock
725 * before and need to recheck
727 if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
730 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
731 if (diff != csum_size)
734 ret = btrfs_extend_item(trans, root, path, diff);
740 btrfs_release_path(root, path);
743 u64 tmp = total_bytes + root->sectorsize;
744 u64 next_sector = sector_sum->bytenr;
745 struct btrfs_sector_sum *next = sector_sum + 1;
747 while (tmp < sums->len) {
748 if (next_sector + root->sectorsize != next->bytenr)
750 tmp += root->sectorsize;
751 next_sector = next->bytenr;
754 tmp = min(tmp, next_offset - file_key.offset);
755 tmp >>= root->fs_info->sb->s_blocksize_bits;
756 tmp = max((u64)1, tmp);
757 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
758 ins_size = csum_size * tmp;
760 ins_size = csum_size;
762 path->leave_spinning = 1;
763 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
765 path->leave_spinning = 0;
773 leaf = path->nodes[0];
774 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
776 item = (struct btrfs_csum_item *)((unsigned char *)item +
777 csum_offset * csum_size);
779 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
780 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
781 btrfs_item_size_nr(leaf, path->slots[0]));
786 (unsigned long)item + csum_size >= map_start + map_len) {
790 unmap_extent_buffer(leaf, eb_token, KM_USER1);
792 err = map_private_extent_buffer(leaf, (unsigned long)item,
795 &map_start, &map_len, KM_USER1);
800 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
801 §or_sum->sum, csum_size);
803 write_extent_buffer(leaf, §or_sum->sum,
804 (unsigned long)item, csum_size);
807 total_bytes += root->sectorsize;
809 if (total_bytes < sums->len) {
810 item = (struct btrfs_csum_item *)((char *)item +
812 if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
813 sector_sum->bytenr) {
814 bytenr = sector_sum->bytenr;
819 unmap_extent_buffer(leaf, eb_token, KM_USER1);
822 btrfs_mark_buffer_dirty(path->nodes[0]);
823 if (total_bytes < sums->len) {
824 btrfs_release_path(root, path);
829 btrfs_free_path(path);