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 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, disk_offset);
64 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
65 btrfs_set_file_extent_offset(leaf, item, offset);
66 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
67 btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
68 btrfs_set_file_extent_generation(leaf, item, trans->transid);
69 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
70 btrfs_set_file_extent_compression(leaf, item, compression);
71 btrfs_set_file_extent_encryption(leaf, item, encryption);
72 btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
74 btrfs_mark_buffer_dirty(leaf);
76 btrfs_free_path(path);
80 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
81 struct btrfs_root *root,
82 struct btrfs_path *path,
86 struct btrfs_key file_key;
87 struct btrfs_key found_key;
88 struct btrfs_csum_item *item;
89 struct extent_buffer *leaf;
92 btrfs_super_csum_size(&root->fs_info->super_copy);
95 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
96 file_key.offset = bytenr;
97 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
98 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
101 leaf = path->nodes[0];
104 if (path->slots[0] == 0)
107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
108 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
111 csum_offset = (bytenr - found_key.offset) >>
112 root->fs_info->sb->s_blocksize_bits;
113 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
114 csums_in_item /= csum_size;
116 if (csum_offset >= csums_in_item) {
121 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
122 item = (struct btrfs_csum_item *)((unsigned char *)item +
123 csum_offset * csum_size);
132 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
133 struct btrfs_root *root,
134 struct btrfs_path *path, u64 objectid,
138 struct btrfs_key file_key;
139 int ins_len = mod < 0 ? -1 : 0;
142 file_key.objectid = objectid;
143 file_key.offset = offset;
144 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
145 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
150 int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
151 struct bio *bio, u32 *dst)
154 struct bio_vec *bvec = bio->bi_io_vec;
157 u64 item_start_offset = 0;
158 u64 item_last_offset = 0;
162 btrfs_super_csum_size(&root->fs_info->super_copy);
164 struct btrfs_path *path;
165 struct btrfs_csum_item *item = NULL;
166 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
168 path = btrfs_alloc_path();
169 if (bio->bi_size > PAGE_CACHE_SIZE * 8)
172 WARN_ON(bio->bi_vcnt <= 0);
174 disk_bytenr = (u64)bio->bi_sector << 9;
175 while (bio_index < bio->bi_vcnt) {
176 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
177 ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
181 if (!item || disk_bytenr < item_start_offset ||
182 disk_bytenr >= item_last_offset) {
183 struct btrfs_key found_key;
187 btrfs_release_path(root, path);
188 item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
189 path, disk_bytenr, 0);
192 if (ret == -ENOENT || ret == -EFBIG)
195 if (BTRFS_I(inode)->root->root_key.objectid ==
196 BTRFS_DATA_RELOC_TREE_OBJECTID) {
197 set_extent_bits(io_tree, offset,
198 offset + bvec->bv_len - 1,
199 EXTENT_NODATASUM, GFP_NOFS);
201 printk(KERN_INFO "btrfs no csum found "
202 "for inode %lu start %llu\n",
204 (unsigned long long)offset);
207 btrfs_release_path(root, path);
210 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
213 item_start_offset = found_key.offset;
214 item_size = btrfs_item_size_nr(path->nodes[0],
216 item_last_offset = item_start_offset +
217 (item_size / csum_size) *
219 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
220 struct btrfs_csum_item);
223 * this byte range must be able to fit inside
224 * a single leaf so it will also fit inside a u32
226 diff = disk_bytenr - item_start_offset;
227 diff = diff / root->sectorsize;
228 diff = diff * csum_size;
230 read_extent_buffer(path->nodes[0], &sum,
231 ((unsigned long)item) + diff,
237 set_state_private(io_tree, offset, sum);
238 disk_bytenr += bvec->bv_len;
242 btrfs_free_path(path);
246 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
247 struct list_head *list)
249 struct btrfs_key key;
250 struct btrfs_path *path;
251 struct extent_buffer *leaf;
252 struct btrfs_ordered_sum *sums;
253 struct btrfs_sector_sum *sector_sum;
254 struct btrfs_csum_item *item;
255 unsigned long offset;
259 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
261 path = btrfs_alloc_path();
264 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
266 key.type = BTRFS_EXTENT_CSUM_KEY;
268 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
271 if (ret > 0 && path->slots[0] > 0) {
272 leaf = path->nodes[0];
273 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
274 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
275 key.type == BTRFS_EXTENT_CSUM_KEY) {
276 offset = (start - key.offset) >>
277 root->fs_info->sb->s_blocksize_bits;
278 if (offset * csum_size <
279 btrfs_item_size_nr(leaf, path->slots[0] - 1))
284 while (start <= end) {
285 leaf = path->nodes[0];
286 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
287 ret = btrfs_next_leaf(root, path);
292 leaf = path->nodes[0];
295 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
296 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
297 key.type != BTRFS_EXTENT_CSUM_KEY)
300 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
301 if (key.offset > end)
304 if (key.offset > start)
307 size = btrfs_item_size_nr(leaf, path->slots[0]);
308 csum_end = key.offset + (size / csum_size) * root->sectorsize;
309 if (csum_end <= start) {
314 csum_end = min(csum_end, end + 1);
315 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
316 struct btrfs_csum_item);
317 while (start < csum_end) {
318 size = min_t(size_t, csum_end - start,
319 MAX_ORDERED_SUM_BYTES(root));
320 sums = kzalloc(btrfs_ordered_sum_size(root, size),
324 sector_sum = sums->sums;
325 sums->bytenr = start;
328 offset = (start - key.offset) >>
329 root->fs_info->sb->s_blocksize_bits;
333 read_extent_buffer(path->nodes[0],
335 ((unsigned long)item) +
337 sector_sum->bytenr = start;
339 size -= root->sectorsize;
340 start += root->sectorsize;
344 list_add_tail(&sums->list, list);
350 btrfs_free_path(path);
354 int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
355 struct bio *bio, u64 file_start, int contig)
357 struct btrfs_ordered_sum *sums;
358 struct btrfs_sector_sum *sector_sum;
359 struct btrfs_ordered_extent *ordered;
361 struct bio_vec *bvec = bio->bi_io_vec;
363 unsigned long total_bytes = 0;
364 unsigned long this_sum_bytes = 0;
368 WARN_ON(bio->bi_vcnt <= 0);
369 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
373 sector_sum = sums->sums;
374 disk_bytenr = (u64)bio->bi_sector << 9;
375 sums->len = bio->bi_size;
376 INIT_LIST_HEAD(&sums->list);
381 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
383 ordered = btrfs_lookup_ordered_extent(inode, offset);
385 sums->bytenr = ordered->start;
387 while (bio_index < bio->bi_vcnt) {
389 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
391 if (!contig && (offset >= ordered->file_offset + ordered->len ||
392 offset < ordered->file_offset)) {
393 unsigned long bytes_left;
394 sums->len = this_sum_bytes;
396 btrfs_add_ordered_sum(inode, ordered, sums);
397 btrfs_put_ordered_extent(ordered);
399 bytes_left = bio->bi_size - total_bytes;
401 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
404 sector_sum = sums->sums;
405 sums->len = bytes_left;
406 ordered = btrfs_lookup_ordered_extent(inode, offset);
408 sums->bytenr = ordered->start;
411 data = kmap_atomic(bvec->bv_page, KM_USER0);
412 sector_sum->sum = ~(u32)0;
413 sector_sum->sum = btrfs_csum_data(root,
414 data + bvec->bv_offset,
417 kunmap_atomic(data, KM_USER0);
418 btrfs_csum_final(sector_sum->sum,
419 (char *)§or_sum->sum);
420 sector_sum->bytenr = disk_bytenr;
424 total_bytes += bvec->bv_len;
425 this_sum_bytes += bvec->bv_len;
426 disk_bytenr += bvec->bv_len;
427 offset += bvec->bv_len;
431 btrfs_add_ordered_sum(inode, ordered, sums);
432 btrfs_put_ordered_extent(ordered);
437 * helper function for csum removal, this expects the
438 * key to describe the csum pointed to by the path, and it expects
439 * the csum to overlap the range [bytenr, len]
441 * The csum should not be entirely contained in the range and the
442 * range should not be entirely contained in the csum.
444 * This calls btrfs_truncate_item with the correct args based on the
445 * overlap, and fixes up the key as required.
447 static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
448 struct btrfs_root *root,
449 struct btrfs_path *path,
450 struct btrfs_key *key,
453 struct extent_buffer *leaf;
455 btrfs_super_csum_size(&root->fs_info->super_copy);
457 u64 end_byte = bytenr + len;
458 u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
461 leaf = path->nodes[0];
462 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
463 csum_end <<= root->fs_info->sb->s_blocksize_bits;
464 csum_end += key->offset;
466 if (key->offset < bytenr && csum_end <= end_byte) {
471 * A simple truncate off the end of the item
473 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
474 new_size *= csum_size;
475 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
477 } else if (key->offset >= bytenr && csum_end > end_byte &&
478 end_byte > key->offset) {
483 * we need to truncate from the beginning of the csum
485 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
486 new_size *= csum_size;
488 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
491 key->offset = end_byte;
492 ret = btrfs_set_item_key_safe(trans, root, path, key);
501 * deletes the csum items from the csum tree for a given
504 int btrfs_del_csums(struct btrfs_trans_handle *trans,
505 struct btrfs_root *root, u64 bytenr, u64 len)
507 struct btrfs_path *path;
508 struct btrfs_key key;
509 u64 end_byte = bytenr + len;
511 struct extent_buffer *leaf;
514 btrfs_super_csum_size(&root->fs_info->super_copy);
515 int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
517 root = root->fs_info->csum_root;
519 path = btrfs_alloc_path();
522 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
523 key.offset = end_byte - 1;
524 key.type = BTRFS_EXTENT_CSUM_KEY;
526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
528 if (path->slots[0] == 0)
532 leaf = path->nodes[0];
533 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
535 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
536 key.type != BTRFS_EXTENT_CSUM_KEY) {
540 if (key.offset >= end_byte)
543 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
544 csum_end <<= blocksize_bits;
545 csum_end += key.offset;
547 /* this csum ends before we start, we're done */
548 if (csum_end <= bytenr)
551 /* delete the entire item, it is inside our range */
552 if (key.offset >= bytenr && csum_end <= end_byte) {
553 ret = btrfs_del_item(trans, root, path);
555 if (key.offset == bytenr)
557 } else if (key.offset < bytenr && csum_end > end_byte) {
558 unsigned long offset;
559 unsigned long shift_len;
560 unsigned long item_offset;
565 * Our bytes are in the middle of the csum,
566 * we need to split this item and insert a new one.
568 * But we can't drop the path because the
569 * csum could change, get removed, extended etc.
571 * The trick here is the max size of a csum item leaves
572 * enough room in the tree block for a single
573 * item header. So, we split the item in place,
574 * adding a new header pointing to the existing
575 * bytes. Then we loop around again and we have
576 * a nicely formed csum item that we can neatly
579 offset = (bytenr - key.offset) >> blocksize_bits;
582 shift_len = (len >> blocksize_bits) * csum_size;
584 item_offset = btrfs_item_ptr_offset(leaf,
587 memset_extent_buffer(leaf, 0, item_offset + offset,
592 * btrfs_split_item returns -EAGAIN when the
593 * item changed size or key
595 ret = btrfs_split_item(trans, root, path, &key, offset);
596 BUG_ON(ret && ret != -EAGAIN);
598 key.offset = end_byte - 1;
600 ret = truncate_one_csum(trans, root, path,
603 if (key.offset < bytenr)
606 btrfs_release_path(root, path);
609 btrfs_free_path(path);
613 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
614 struct btrfs_root *root,
615 struct btrfs_ordered_sum *sums)
619 struct btrfs_key file_key;
620 struct btrfs_key found_key;
624 struct btrfs_path *path;
625 struct btrfs_csum_item *item;
626 struct btrfs_csum_item *item_end;
627 struct extent_buffer *leaf = NULL;
629 struct btrfs_sector_sum *sector_sum;
634 unsigned long map_len;
635 unsigned long map_start;
637 btrfs_super_csum_size(&root->fs_info->super_copy);
639 path = btrfs_alloc_path();
641 sector_sum = sums->sums;
643 next_offset = (u64)-1;
645 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
646 file_key.offset = sector_sum->bytenr;
647 bytenr = sector_sum->bytenr;
648 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
650 item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
652 leaf = path->nodes[0];
659 /* we found one, but it isn't big enough yet */
660 leaf = path->nodes[0];
661 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
662 if ((item_size / csum_size) >=
663 MAX_CSUM_ITEMS(root, csum_size)) {
664 /* already at max size, make a new one */
668 int slot = path->slots[0] + 1;
669 /* we didn't find a csum item, insert one */
670 nritems = btrfs_header_nritems(path->nodes[0]);
671 if (path->slots[0] >= nritems - 1) {
672 ret = btrfs_next_leaf(root, path);
679 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
680 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
681 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
685 next_offset = found_key.offset;
691 * at this point, we know the tree has an item, but it isn't big
692 * enough yet to put our csum in. Grow it
694 btrfs_release_path(root, path);
695 ret = btrfs_search_slot(trans, root, &file_key, path,
701 if (path->slots[0] == 0)
706 leaf = path->nodes[0];
707 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
708 csum_offset = (bytenr - found_key.offset) >>
709 root->fs_info->sb->s_blocksize_bits;
711 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
712 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
713 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
717 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
719 u32 diff = (csum_offset + 1) * csum_size;
722 * is the item big enough already? we dropped our lock
723 * before and need to recheck
725 if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
728 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
729 if (diff != csum_size)
732 ret = btrfs_extend_item(trans, root, path, diff);
738 btrfs_release_path(root, path);
741 u64 tmp = total_bytes + root->sectorsize;
742 u64 next_sector = sector_sum->bytenr;
743 struct btrfs_sector_sum *next = sector_sum + 1;
745 while (tmp < sums->len) {
746 if (next_sector + root->sectorsize != next->bytenr)
748 tmp += root->sectorsize;
749 next_sector = next->bytenr;
752 tmp = min(tmp, next_offset - file_key.offset);
753 tmp >>= root->fs_info->sb->s_blocksize_bits;
754 tmp = max((u64)1, tmp);
755 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
756 ins_size = csum_size * tmp;
758 ins_size = csum_size;
760 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
769 leaf = path->nodes[0];
770 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
772 item = (struct btrfs_csum_item *)((unsigned char *)item +
773 csum_offset * csum_size);
775 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
776 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
777 btrfs_item_size_nr(leaf, path->slots[0]));
783 (unsigned long)item + csum_size >= map_start + map_len) {
787 unmap_extent_buffer(leaf, eb_token, KM_USER1);
789 err = map_private_extent_buffer(leaf, (unsigned long)item,
792 &map_start, &map_len, KM_USER1);
797 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
798 §or_sum->sum, csum_size);
800 write_extent_buffer(leaf, §or_sum->sum,
801 (unsigned long)item, csum_size);
804 total_bytes += root->sectorsize;
806 if (total_bytes < sums->len) {
807 item = (struct btrfs_csum_item *)((char *)item +
809 if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
810 sector_sum->bytenr) {
811 bytenr = sector_sum->bytenr;
816 unmap_extent_buffer(leaf, eb_token, KM_USER1);
819 btrfs_mark_buffer_dirty(path->nodes[0]);
821 if (total_bytes < sums->len) {
822 btrfs_release_path(root, path);
826 btrfs_free_path(path);