20 #include <linux/slab.h>
28 #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
29 sizeof(struct btrfs_item) * 2) / \
32 #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
35 #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
36 sizeof(struct btrfs_ordered_sum)) / \
37 sizeof(struct btrfs_sector_sum) * \
38 (r)->sectorsize - (r)->sectorsize)
61 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
66 leaf = path->
nodes[0];
69 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
70 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
71 btrfs_set_file_extent_offset(leaf, item, offset);
72 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
73 btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
74 btrfs_set_file_extent_generation(leaf, item, trans->
transid);
76 btrfs_set_file_extent_compression(leaf, item, compression);
77 btrfs_set_file_extent_encryption(leaf, item, encryption);
78 btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
97 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
106 leaf = path->
nodes[0];
109 if (path->
slots[0] == 0)
112 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
116 csum_offset = (bytenr - found_key.
offset) >>
117 root->
fs_info->sb->s_blocksize_bits;
118 csums_in_item = btrfs_item_size_nr(leaf, path->
slots[0]);
119 csums_in_item /= csum_size;
121 if (csum_offset >= csums_in_item) {
128 csum_offset * csum_size);
144 int ins_len = mod < 0 ? -1 : 0;
155 static int __btrfs_lookup_bio_sums(
struct btrfs_root *root,
160 struct bio_vec *bvec = bio->bi_io_vec;
163 u64 item_start_offset = 0;
164 u64 item_last_offset = 0;
167 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
187 if (btrfs_is_free_space_inode(inode)) {
192 disk_bytenr = (
u64)bio->bi_sector << 9;
194 offset = logical_offset;
195 while (bio_index < bio->bi_vcnt) {
197 offset =
page_offset(bvec->bv_page) + bvec->bv_offset;
202 if (!item || disk_bytenr < item_start_offset ||
203 disk_bytenr >= item_last_offset) {
210 path, disk_bytenr, 0);
216 if (BTRFS_I(inode)->root->
root_key.objectid ==
219 offset + bvec->bv_len - 1,
223 "for inode %llu start %llu\n",
226 (
unsigned long long)offset);
232 btrfs_item_key_to_cpu(path->
nodes[0], &found_key,
235 item_start_offset = found_key.offset;
236 item_size = btrfs_item_size_nr(path->
nodes[0],
238 item_last_offset = item_start_offset +
239 (item_size / csum_size) *
248 diff = disk_bytenr - item_start_offset;
250 diff = diff * csum_size;
253 ((
unsigned long)item) + diff,
260 disk_bytenr += bvec->bv_len;
261 offset += bvec->bv_len;
270 struct bio *bio,
u32 *dst)
272 return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0);
276 struct bio *bio,
u64 offset)
278 return __btrfs_lookup_bio_sums(root, inode, bio, offset,
NULL, 1);
295 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
314 if (ret > 0 && path->
slots[0] > 0) {
315 leaf = path->
nodes[0];
316 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0] - 1);
319 offset = (start - key.
offset) >>
320 root->
fs_info->sb->s_blocksize_bits;
321 if (offset * csum_size <
322 btrfs_item_size_nr(leaf, path->
slots[0] - 1))
327 while (start <= end) {
328 leaf = path->
nodes[0];
329 if (path->
slots[0] >= btrfs_header_nritems(leaf)) {
335 leaf = path->
nodes[0];
338 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
343 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
350 size = btrfs_item_size_nr(leaf, path->
slots[0]);
352 if (csum_end <= start) {
357 csum_end =
min(csum_end, end + 1);
360 while (start < csum_end) {
361 size =
min_t(
size_t, csum_end - start,
363 sums = kzalloc(btrfs_ordered_sum_size(root, size),
370 sector_sum = sums->
sums;
374 offset = (start - key.
offset) >>
375 root->
fs_info->sb->s_blocksize_bits;
381 ((
unsigned long)item) +
396 while (ret < 0 && !list_empty(&tmplist)) {
401 list_splice_tail(&tmplist, list);
408 struct bio *bio,
u64 file_start,
int contig)
414 struct bio_vec *bvec = bio->bi_io_vec;
417 unsigned long this_sum_bytes = 0;
422 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size),
GFP_NOFS);
426 sector_sum = sums->
sums;
427 disk_bytenr = (
u64)bio->bi_sector << 9;
428 sums->
len = bio->bi_size;
429 INIT_LIST_HEAD(&sums->
list);
434 offset =
page_offset(bvec->bv_page) + bvec->bv_offset;
440 while (bio_index < bio->bi_vcnt) {
442 offset =
page_offset(bvec->bv_page) + bvec->bv_offset;
446 unsigned long bytes_left;
447 sums->
len = this_sum_bytes;
454 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
457 sector_sum = sums->
sums;
458 sums->
len = bytes_left;
465 sector_sum->
sum = ~(
u32)0;
467 data + bvec->bv_offset,
472 (
char *)§or_sum->
sum);
477 total_bytes += bvec->bv_len;
478 this_sum_bytes += bvec->bv_len;
479 disk_bytenr += bvec->bv_len;
480 offset += bvec->bv_len;
507 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
509 u64 end_byte = bytenr +
len;
510 u32 blocksize_bits = root->
fs_info->sb->s_blocksize_bits;
512 leaf = path->
nodes[0];
513 csum_end = btrfs_item_size_nr(leaf, path->
slots[0]) / csum_size;
514 csum_end <<= root->
fs_info->sb->s_blocksize_bits;
517 if (key->
offset < bytenr && csum_end <= end_byte) {
524 u32 new_size = (bytenr - key->
offset) >> blocksize_bits;
525 new_size *= csum_size;
527 }
else if (key->
offset >= bytenr && csum_end > end_byte &&
535 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
536 new_size *= csum_size;
556 u64 end_byte = bytenr + len;
560 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
561 int blocksize_bits = root->
fs_info->sb->s_blocksize_bits;
563 root = root->
fs_info->csum_root;
571 key.
offset = end_byte - 1;
577 if (path->
slots[0] == 0)
580 }
else if (ret < 0) {
584 leaf = path->
nodes[0];
585 btrfs_item_key_to_cpu(leaf, &key, path->
slots[0]);
592 if (key.
offset >= end_byte)
595 csum_end = btrfs_item_size_nr(leaf, path->
slots[0]) / csum_size;
596 csum_end <<= blocksize_bits;
600 if (csum_end <= bytenr)
604 if (key.
offset >= bytenr && csum_end <= end_byte) {
605 ret = btrfs_del_item(trans, root, path);
610 }
else if (key.
offset < bytenr && csum_end > end_byte) {
612 unsigned long shift_len;
613 unsigned long item_offset;
632 offset = (bytenr - key.
offset) >> blocksize_bits;
635 shift_len = (len >> blocksize_bits) * csum_size;
649 if (ret && ret != -
EAGAIN) {
654 key.
offset = end_byte - 1;
656 truncate_one_csum(trans, root, path, &key, bytenr, len);
687 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
693 sector_sum = sums->
sums;
696 next_offset = (
u64)-1;
700 bytenr = sector_sum->
bytenr;
705 leaf = path->
nodes[0];
716 leaf = path->
nodes[0];
717 item_size = btrfs_item_size_nr(leaf, path->
slots[0]);
718 if ((item_size / csum_size) >=
726 nritems = btrfs_header_nritems(path->
nodes[0]);
727 if (path->
slots[0] >= nritems - 1) {
735 btrfs_item_key_to_cpu(path->
nodes[0], &found_key, slot);
741 next_offset = found_key.
offset;
757 if (path->
slots[0] == 0)
762 leaf = path->
nodes[0];
763 btrfs_item_key_to_cpu(leaf, &found_key, path->
slots[0]);
764 csum_offset = (bytenr - found_key.
offset) >>
765 root->
fs_info->sb->s_blocksize_bits;
773 if (csum_offset >= btrfs_item_size_nr(leaf, path->
slots[0]) /
775 u32 diff = (csum_offset + 1) * csum_size;
781 if (diff < btrfs_item_size_nr(leaf, path->
slots[0]))
784 diff = diff - btrfs_item_size_nr(leaf, path->
slots[0]);
785 if (diff != csum_size)
800 while (tmp < sums->len) {
804 next_sector = next->
bytenr;
807 tmp =
min(tmp, next_offset - file_key.
offset);
808 tmp >>= root->
fs_info->sb->s_blocksize_bits;
811 ins_size = csum_size *
tmp;
813 ins_size = csum_size;
816 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
826 leaf = path->
nodes[0];
830 csum_offset * csum_size);
834 btrfs_item_size_nr(leaf, path->
slots[0]));
841 if (total_bytes < sums->len) {
846 bytenr = sector_sum->
bytenr;
852 if (total_bytes < sums->len) {