diff options
author | Miao Xie <miaox@cn.fujitsu.com> | 2013-06-18 22:36:09 -0400 |
---|---|---|
committer | Josef Bacik <jbacik@fusionio.com> | 2013-07-02 11:50:47 -0400 |
commit | f51a4a1826ff810eb9c00cadff8978b028c40756 (patch) | |
tree | 20185a1b681194a39513f346f72e6d8ef605b8e1 /fs/btrfs | |
parent | 7ee9e4405f264e9eda808aa5ca4522746a1af9c1 (diff) |
Btrfs: remove btrfs_sector_sum structure
Using the structure btrfs_sector_sum to keep the checksum value is
unnecessary, because the extents that btrfs_sector_sum points to are
continuous, we can find out the expected checksums by btrfs_ordered_sum's
bytenr and the offset, so we can remove btrfs_sector_sum's bytenr. After
removing bytenr, there is only one member in the structure, so it makes
no sense to keep the structure, just remove it, and use a u32 array to
store the checksum value.
By this change, we don't use the while loop to get the checksums one by
one. Now, we can get several checksum value at one time, it improved the
performance by ~74% on my SSD (31MB/s -> 54MB/s).
test command:
# dd if=/dev/zero of=/mnt/btrfs/file0 bs=1M count=1024 oflag=sync
Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Josef Bacik <jbacik@fusionio.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r-- | fs/btrfs/file-item.c | 144 | ||||
-rw-r--r-- | fs/btrfs/ordered-data.c | 19 | ||||
-rw-r--r-- | fs/btrfs/ordered-data.h | 25 | ||||
-rw-r--r-- | fs/btrfs/relocation.c | 14 | ||||
-rw-r--r-- | fs/btrfs/scrub.c | 16 |
5 files changed, 76 insertions, 142 deletions
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index b193bf324a41..a7bfc9541803 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c | |||
@@ -34,8 +34,7 @@ | |||
34 | 34 | ||
35 | #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \ | 35 | #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \ |
36 | sizeof(struct btrfs_ordered_sum)) / \ | 36 | sizeof(struct btrfs_ordered_sum)) / \ |
37 | sizeof(struct btrfs_sector_sum) * \ | 37 | sizeof(u32) * (r)->sectorsize) |
38 | (r)->sectorsize - (r)->sectorsize) | ||
39 | 38 | ||
40 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, | 39 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, |
41 | struct btrfs_root *root, | 40 | struct btrfs_root *root, |
@@ -297,7 +296,6 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, | |||
297 | struct btrfs_path *path; | 296 | struct btrfs_path *path; |
298 | struct extent_buffer *leaf; | 297 | struct extent_buffer *leaf; |
299 | struct btrfs_ordered_sum *sums; | 298 | struct btrfs_ordered_sum *sums; |
300 | struct btrfs_sector_sum *sector_sum; | ||
301 | struct btrfs_csum_item *item; | 299 | struct btrfs_csum_item *item; |
302 | LIST_HEAD(tmplist); | 300 | LIST_HEAD(tmplist); |
303 | unsigned long offset; | 301 | unsigned long offset; |
@@ -368,34 +366,28 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, | |||
368 | struct btrfs_csum_item); | 366 | struct btrfs_csum_item); |
369 | while (start < csum_end) { | 367 | while (start < csum_end) { |
370 | size = min_t(size_t, csum_end - start, | 368 | size = min_t(size_t, csum_end - start, |
371 | MAX_ORDERED_SUM_BYTES(root)); | 369 | MAX_ORDERED_SUM_BYTES(root)); |
372 | sums = kzalloc(btrfs_ordered_sum_size(root, size), | 370 | sums = kzalloc(btrfs_ordered_sum_size(root, size), |
373 | GFP_NOFS); | 371 | GFP_NOFS); |
374 | if (!sums) { | 372 | if (!sums) { |
375 | ret = -ENOMEM; | 373 | ret = -ENOMEM; |
376 | goto fail; | 374 | goto fail; |
377 | } | 375 | } |
378 | 376 | ||
379 | sector_sum = sums->sums; | ||
380 | sums->bytenr = start; | 377 | sums->bytenr = start; |
381 | sums->len = size; | 378 | sums->len = (int)size; |
382 | 379 | ||
383 | offset = (start - key.offset) >> | 380 | offset = (start - key.offset) >> |
384 | root->fs_info->sb->s_blocksize_bits; | 381 | root->fs_info->sb->s_blocksize_bits; |
385 | offset *= csum_size; | 382 | offset *= csum_size; |
383 | size >>= root->fs_info->sb->s_blocksize_bits; | ||
386 | 384 | ||
387 | while (size > 0) { | 385 | read_extent_buffer(path->nodes[0], |
388 | read_extent_buffer(path->nodes[0], | 386 | sums->sums, |
389 | §or_sum->sum, | 387 | ((unsigned long)item) + offset, |
390 | ((unsigned long)item) + | 388 | csum_size * size); |
391 | offset, csum_size); | 389 | |
392 | sector_sum->bytenr = start; | 390 | start += root->sectorsize * size; |
393 | |||
394 | size -= root->sectorsize; | ||
395 | start += root->sectorsize; | ||
396 | offset += csum_size; | ||
397 | sector_sum++; | ||
398 | } | ||
399 | list_add_tail(&sums->list, &tmplist); | 391 | list_add_tail(&sums->list, &tmplist); |
400 | } | 392 | } |
401 | path->slots[0]++; | 393 | path->slots[0]++; |
@@ -417,23 +409,20 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, | |||
417 | struct bio *bio, u64 file_start, int contig) | 409 | struct bio *bio, u64 file_start, int contig) |
418 | { | 410 | { |
419 | struct btrfs_ordered_sum *sums; | 411 | struct btrfs_ordered_sum *sums; |
420 | struct btrfs_sector_sum *sector_sum; | ||
421 | struct btrfs_ordered_extent *ordered; | 412 | struct btrfs_ordered_extent *ordered; |
422 | char *data; | 413 | char *data; |
423 | struct bio_vec *bvec = bio->bi_io_vec; | 414 | struct bio_vec *bvec = bio->bi_io_vec; |
424 | int bio_index = 0; | 415 | int bio_index = 0; |
416 | int index; | ||
425 | unsigned long total_bytes = 0; | 417 | unsigned long total_bytes = 0; |
426 | unsigned long this_sum_bytes = 0; | 418 | unsigned long this_sum_bytes = 0; |
427 | u64 offset; | 419 | u64 offset; |
428 | u64 disk_bytenr; | ||
429 | 420 | ||
430 | WARN_ON(bio->bi_vcnt <= 0); | 421 | WARN_ON(bio->bi_vcnt <= 0); |
431 | sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); | 422 | sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); |
432 | if (!sums) | 423 | if (!sums) |
433 | return -ENOMEM; | 424 | return -ENOMEM; |
434 | 425 | ||
435 | sector_sum = sums->sums; | ||
436 | disk_bytenr = (u64)bio->bi_sector << 9; | ||
437 | sums->len = bio->bi_size; | 426 | sums->len = bio->bi_size; |
438 | INIT_LIST_HEAD(&sums->list); | 427 | INIT_LIST_HEAD(&sums->list); |
439 | 428 | ||
@@ -444,7 +433,8 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, | |||
444 | 433 | ||
445 | ordered = btrfs_lookup_ordered_extent(inode, offset); | 434 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
446 | BUG_ON(!ordered); /* Logic error */ | 435 | BUG_ON(!ordered); /* Logic error */ |
447 | sums->bytenr = ordered->start; | 436 | sums->bytenr = (u64)bio->bi_sector << 9; |
437 | index = 0; | ||
448 | 438 | ||
449 | while (bio_index < bio->bi_vcnt) { | 439 | while (bio_index < bio->bi_vcnt) { |
450 | if (!contig) | 440 | if (!contig) |
@@ -463,28 +453,27 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, | |||
463 | sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), | 453 | sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), |
464 | GFP_NOFS); | 454 | GFP_NOFS); |
465 | BUG_ON(!sums); /* -ENOMEM */ | 455 | BUG_ON(!sums); /* -ENOMEM */ |
466 | sector_sum = sums->sums; | ||
467 | sums->len = bytes_left; | 456 | sums->len = bytes_left; |
468 | ordered = btrfs_lookup_ordered_extent(inode, offset); | 457 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
469 | BUG_ON(!ordered); /* Logic error */ | 458 | BUG_ON(!ordered); /* Logic error */ |
470 | sums->bytenr = ordered->start; | 459 | sums->bytenr = ((u64)bio->bi_sector << 9) + |
460 | total_bytes; | ||
461 | index = 0; | ||
471 | } | 462 | } |
472 | 463 | ||
473 | data = kmap_atomic(bvec->bv_page); | 464 | data = kmap_atomic(bvec->bv_page); |
474 | sector_sum->sum = ~(u32)0; | 465 | sums->sums[index] = ~(u32)0; |
475 | sector_sum->sum = btrfs_csum_data(data + bvec->bv_offset, | 466 | sums->sums[index] = btrfs_csum_data(data + bvec->bv_offset, |
476 | sector_sum->sum, | 467 | sums->sums[index], |
477 | bvec->bv_len); | 468 | bvec->bv_len); |
478 | kunmap_atomic(data); | 469 | kunmap_atomic(data); |
479 | btrfs_csum_final(sector_sum->sum, | 470 | btrfs_csum_final(sums->sums[index], |
480 | (char *)§or_sum->sum); | 471 | (char *)(sums->sums + index)); |
481 | sector_sum->bytenr = disk_bytenr; | ||
482 | 472 | ||
483 | sector_sum++; | ||
484 | bio_index++; | 473 | bio_index++; |
474 | index++; | ||
485 | total_bytes += bvec->bv_len; | 475 | total_bytes += bvec->bv_len; |
486 | this_sum_bytes += bvec->bv_len; | 476 | this_sum_bytes += bvec->bv_len; |
487 | disk_bytenr += bvec->bv_len; | ||
488 | offset += bvec->bv_len; | 477 | offset += bvec->bv_len; |
489 | bvec++; | 478 | bvec++; |
490 | } | 479 | } |
@@ -672,62 +661,46 @@ out: | |||
672 | return ret; | 661 | return ret; |
673 | } | 662 | } |
674 | 663 | ||
675 | static u64 btrfs_sector_sum_left(struct btrfs_ordered_sum *sums, | ||
676 | struct btrfs_sector_sum *sector_sum, | ||
677 | u64 total_bytes, u64 sectorsize) | ||
678 | { | ||
679 | u64 tmp = sectorsize; | ||
680 | u64 next_sector = sector_sum->bytenr; | ||
681 | struct btrfs_sector_sum *next = sector_sum + 1; | ||
682 | |||
683 | while ((tmp + total_bytes) < sums->len) { | ||
684 | if (next_sector + sectorsize != next->bytenr) | ||
685 | break; | ||
686 | tmp += sectorsize; | ||
687 | next_sector = next->bytenr; | ||
688 | next++; | ||
689 | } | ||
690 | return tmp; | ||
691 | } | ||
692 | |||
693 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, | 664 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, |
694 | struct btrfs_root *root, | 665 | struct btrfs_root *root, |
695 | struct btrfs_ordered_sum *sums) | 666 | struct btrfs_ordered_sum *sums) |
696 | { | 667 | { |
697 | u64 bytenr; | ||
698 | int ret; | ||
699 | struct btrfs_key file_key; | 668 | struct btrfs_key file_key; |
700 | struct btrfs_key found_key; | 669 | struct btrfs_key found_key; |
701 | u64 next_offset; | ||
702 | u64 total_bytes = 0; | ||
703 | int found_next; | ||
704 | struct btrfs_path *path; | 670 | struct btrfs_path *path; |
705 | struct btrfs_csum_item *item; | 671 | struct btrfs_csum_item *item; |
706 | struct btrfs_csum_item *item_end; | 672 | struct btrfs_csum_item *item_end; |
707 | struct extent_buffer *leaf = NULL; | 673 | struct extent_buffer *leaf = NULL; |
674 | u64 next_offset; | ||
675 | u64 total_bytes = 0; | ||
708 | u64 csum_offset; | 676 | u64 csum_offset; |
709 | struct btrfs_sector_sum *sector_sum; | 677 | u64 bytenr; |
710 | u32 nritems; | 678 | u32 nritems; |
711 | u32 ins_size; | 679 | u32 ins_size; |
680 | int index = 0; | ||
681 | int found_next; | ||
682 | int ret; | ||
712 | u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); | 683 | u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); |
713 | 684 | ||
714 | path = btrfs_alloc_path(); | 685 | path = btrfs_alloc_path(); |
715 | if (!path) | 686 | if (!path) |
716 | return -ENOMEM; | 687 | return -ENOMEM; |
717 | |||
718 | sector_sum = sums->sums; | ||
719 | again: | 688 | again: |
720 | next_offset = (u64)-1; | 689 | next_offset = (u64)-1; |
721 | found_next = 0; | 690 | found_next = 0; |
691 | bytenr = sums->bytenr + total_bytes; | ||
722 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; | 692 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
723 | file_key.offset = sector_sum->bytenr; | 693 | file_key.offset = bytenr; |
724 | bytenr = sector_sum->bytenr; | ||
725 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); | 694 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); |
726 | 695 | ||
727 | item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1); | 696 | item = btrfs_lookup_csum(trans, root, path, bytenr, 1); |
728 | if (!IS_ERR(item)) { | 697 | if (!IS_ERR(item)) { |
729 | leaf = path->nodes[0]; | ||
730 | ret = 0; | 698 | ret = 0; |
699 | leaf = path->nodes[0]; | ||
700 | item_end = btrfs_item_ptr(leaf, path->slots[0], | ||
701 | struct btrfs_csum_item); | ||
702 | item_end = (struct btrfs_csum_item *)((char *)item_end + | ||
703 | btrfs_item_size_nr(leaf, path->slots[0])); | ||
731 | goto found; | 704 | goto found; |
732 | } | 705 | } |
733 | ret = PTR_ERR(item); | 706 | ret = PTR_ERR(item); |
@@ -807,8 +780,7 @@ again: | |||
807 | 780 | ||
808 | free_space = btrfs_leaf_free_space(root, leaf) - | 781 | free_space = btrfs_leaf_free_space(root, leaf) - |
809 | sizeof(struct btrfs_item) - csum_size; | 782 | sizeof(struct btrfs_item) - csum_size; |
810 | tmp = btrfs_sector_sum_left(sums, sector_sum, total_bytes, | 783 | tmp = sums->len - total_bytes; |
811 | root->sectorsize); | ||
812 | tmp >>= root->fs_info->sb->s_blocksize_bits; | 784 | tmp >>= root->fs_info->sb->s_blocksize_bits; |
813 | WARN_ON(tmp < 1); | 785 | WARN_ON(tmp < 1); |
814 | 786 | ||
@@ -822,6 +794,7 @@ again: | |||
822 | diff *= csum_size; | 794 | diff *= csum_size; |
823 | 795 | ||
824 | btrfs_extend_item(root, path, diff); | 796 | btrfs_extend_item(root, path, diff); |
797 | ret = 0; | ||
825 | goto csum; | 798 | goto csum; |
826 | } | 799 | } |
827 | 800 | ||
@@ -831,8 +804,7 @@ insert: | |||
831 | if (found_next) { | 804 | if (found_next) { |
832 | u64 tmp; | 805 | u64 tmp; |
833 | 806 | ||
834 | tmp = btrfs_sector_sum_left(sums, sector_sum, total_bytes, | 807 | tmp = sums->len - total_bytes; |
835 | root->sectorsize); | ||
836 | tmp >>= root->fs_info->sb->s_blocksize_bits; | 808 | tmp >>= root->fs_info->sb->s_blocksize_bits; |
837 | tmp = min(tmp, (next_offset - file_key.offset) >> | 809 | tmp = min(tmp, (next_offset - file_key.offset) >> |
838 | root->fs_info->sb->s_blocksize_bits); | 810 | root->fs_info->sb->s_blocksize_bits); |
@@ -853,31 +825,25 @@ insert: | |||
853 | WARN_ON(1); | 825 | WARN_ON(1); |
854 | goto fail_unlock; | 826 | goto fail_unlock; |
855 | } | 827 | } |
856 | csum: | ||
857 | leaf = path->nodes[0]; | 828 | leaf = path->nodes[0]; |
829 | csum: | ||
858 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); | 830 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
859 | ret = 0; | 831 | item_end = (struct btrfs_csum_item *)((unsigned char *)item + |
832 | btrfs_item_size_nr(leaf, path->slots[0])); | ||
860 | item = (struct btrfs_csum_item *)((unsigned char *)item + | 833 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
861 | csum_offset * csum_size); | 834 | csum_offset * csum_size); |
862 | found: | 835 | found: |
863 | item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); | 836 | ins_size = (u32)(sums->len - total_bytes) >> |
864 | item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + | 837 | root->fs_info->sb->s_blocksize_bits; |
865 | btrfs_item_size_nr(leaf, path->slots[0])); | 838 | ins_size *= csum_size; |
866 | next_sector: | 839 | ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, |
867 | 840 | ins_size); | |
868 | write_extent_buffer(leaf, §or_sum->sum, (unsigned long)item, csum_size); | 841 | write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, |
869 | 842 | ins_size); | |
870 | total_bytes += root->sectorsize; | 843 | |
871 | sector_sum++; | 844 | ins_size /= csum_size; |
872 | if (total_bytes < sums->len) { | 845 | total_bytes += ins_size * root->sectorsize; |
873 | item = (struct btrfs_csum_item *)((char *)item + | 846 | index += ins_size; |
874 | csum_size); | ||
875 | if (item < item_end && bytenr + PAGE_CACHE_SIZE == | ||
876 | sector_sum->bytenr) { | ||
877 | bytenr = sector_sum->bytenr; | ||
878 | goto next_sector; | ||
879 | } | ||
880 | } | ||
881 | 847 | ||
882 | btrfs_mark_buffer_dirty(path->nodes[0]); | 848 | btrfs_mark_buffer_dirty(path->nodes[0]); |
883 | if (total_bytes < sums->len) { | 849 | if (total_bytes < sums->len) { |
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c index 665c640e3ea6..81369827e514 100644 --- a/fs/btrfs/ordered-data.c +++ b/fs/btrfs/ordered-data.c | |||
@@ -1032,7 +1032,6 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, | |||
1032 | u32 *sum, int len) | 1032 | u32 *sum, int len) |
1033 | { | 1033 | { |
1034 | struct btrfs_ordered_sum *ordered_sum; | 1034 | struct btrfs_ordered_sum *ordered_sum; |
1035 | struct btrfs_sector_sum *sector_sums; | ||
1036 | struct btrfs_ordered_extent *ordered; | 1035 | struct btrfs_ordered_extent *ordered; |
1037 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; | 1036 | struct btrfs_ordered_inode_tree *tree = &BTRFS_I(inode)->ordered_tree; |
1038 | unsigned long num_sectors; | 1037 | unsigned long num_sectors; |
@@ -1050,18 +1049,16 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr, | |||
1050 | disk_bytenr < ordered_sum->bytenr + ordered_sum->len) { | 1049 | disk_bytenr < ordered_sum->bytenr + ordered_sum->len) { |
1051 | i = (disk_bytenr - ordered_sum->bytenr) >> | 1050 | i = (disk_bytenr - ordered_sum->bytenr) >> |
1052 | inode->i_sb->s_blocksize_bits; | 1051 | inode->i_sb->s_blocksize_bits; |
1053 | sector_sums = ordered_sum->sums + i; | ||
1054 | num_sectors = ordered_sum->len >> | 1052 | num_sectors = ordered_sum->len >> |
1055 | inode->i_sb->s_blocksize_bits; | 1053 | inode->i_sb->s_blocksize_bits; |
1056 | for (; i < num_sectors; i++) { | 1054 | num_sectors = min_t(int, len - index, num_sectors - i); |
1057 | if (sector_sums[i].bytenr == disk_bytenr) { | 1055 | memcpy(sum + index, ordered_sum->sums + i, |
1058 | sum[index] = sector_sums[i].sum; | 1056 | num_sectors); |
1059 | index++; | 1057 | |
1060 | if (index == len) | 1058 | index += (int)num_sectors; |
1061 | goto out; | 1059 | if (index == len) |
1062 | disk_bytenr += sectorsize; | 1060 | goto out; |
1063 | } | 1061 | disk_bytenr += num_sectors * sectorsize; |
1064 | } | ||
1065 | } | 1062 | } |
1066 | } | 1063 | } |
1067 | out: | 1064 | out: |
diff --git a/fs/btrfs/ordered-data.h b/fs/btrfs/ordered-data.h index d082d43e00e5..68844d59ee6f 100644 --- a/fs/btrfs/ordered-data.h +++ b/fs/btrfs/ordered-data.h | |||
@@ -26,18 +26,6 @@ struct btrfs_ordered_inode_tree { | |||
26 | struct rb_node *last; | 26 | struct rb_node *last; |
27 | }; | 27 | }; |
28 | 28 | ||
29 | /* | ||
30 | * these are used to collect checksums done just before bios submission. | ||
31 | * They are attached via a list into the ordered extent, and | ||
32 | * checksum items are inserted into the tree after all the blocks in | ||
33 | * the ordered extent are on disk | ||
34 | */ | ||
35 | struct btrfs_sector_sum { | ||
36 | /* bytenr on disk */ | ||
37 | u64 bytenr; | ||
38 | u32 sum; | ||
39 | }; | ||
40 | |||
41 | struct btrfs_ordered_sum { | 29 | struct btrfs_ordered_sum { |
42 | /* bytenr is the start of this extent on disk */ | 30 | /* bytenr is the start of this extent on disk */ |
43 | u64 bytenr; | 31 | u64 bytenr; |
@@ -45,10 +33,10 @@ struct btrfs_ordered_sum { | |||
45 | /* | 33 | /* |
46 | * this is the length in bytes covered by the sums array below. | 34 | * this is the length in bytes covered by the sums array below. |
47 | */ | 35 | */ |
48 | unsigned long len; | 36 | int len; |
49 | struct list_head list; | 37 | struct list_head list; |
50 | /* last field is a variable length array of btrfs_sector_sums */ | 38 | /* last field is a variable length array of csums */ |
51 | struct btrfs_sector_sum sums[]; | 39 | u32 sums[]; |
52 | }; | 40 | }; |
53 | 41 | ||
54 | /* | 42 | /* |
@@ -149,11 +137,8 @@ struct btrfs_ordered_extent { | |||
149 | static inline int btrfs_ordered_sum_size(struct btrfs_root *root, | 137 | static inline int btrfs_ordered_sum_size(struct btrfs_root *root, |
150 | unsigned long bytes) | 138 | unsigned long bytes) |
151 | { | 139 | { |
152 | unsigned long num_sectors = (bytes + root->sectorsize - 1) / | 140 | int num_sectors = (int)DIV_ROUND_UP(bytes, root->sectorsize); |
153 | root->sectorsize; | 141 | return sizeof(struct btrfs_ordered_sum) + num_sectors * sizeof(u32); |
154 | num_sectors++; | ||
155 | return sizeof(struct btrfs_ordered_sum) + | ||
156 | num_sectors * sizeof(struct btrfs_sector_sum); | ||
157 | } | 142 | } |
158 | 143 | ||
159 | static inline void | 144 | static inline void |
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index d91f106df665..12096496cc99 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c | |||
@@ -4458,10 +4458,8 @@ out: | |||
4458 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) | 4458 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
4459 | { | 4459 | { |
4460 | struct btrfs_ordered_sum *sums; | 4460 | struct btrfs_ordered_sum *sums; |
4461 | struct btrfs_sector_sum *sector_sum; | ||
4462 | struct btrfs_ordered_extent *ordered; | 4461 | struct btrfs_ordered_extent *ordered; |
4463 | struct btrfs_root *root = BTRFS_I(inode)->root; | 4462 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4464 | size_t offset; | ||
4465 | int ret; | 4463 | int ret; |
4466 | u64 disk_bytenr; | 4464 | u64 disk_bytenr; |
4467 | LIST_HEAD(list); | 4465 | LIST_HEAD(list); |
@@ -4475,19 +4473,13 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) | |||
4475 | if (ret) | 4473 | if (ret) |
4476 | goto out; | 4474 | goto out; |
4477 | 4475 | ||
4476 | disk_bytenr = ordered->start; | ||
4478 | while (!list_empty(&list)) { | 4477 | while (!list_empty(&list)) { |
4479 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | 4478 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
4480 | list_del_init(&sums->list); | 4479 | list_del_init(&sums->list); |
4481 | 4480 | ||
4482 | sector_sum = sums->sums; | 4481 | sums->bytenr = disk_bytenr; |
4483 | sums->bytenr = ordered->start; | 4482 | disk_bytenr += sums->len; |
4484 | |||
4485 | offset = 0; | ||
4486 | while (offset < sums->len) { | ||
4487 | sector_sum->bytenr += ordered->start - disk_bytenr; | ||
4488 | sector_sum++; | ||
4489 | offset += root->sectorsize; | ||
4490 | } | ||
4491 | 4483 | ||
4492 | btrfs_add_ordered_sum(inode, ordered, sums); | 4484 | btrfs_add_ordered_sum(inode, ordered, sums); |
4493 | } | 4485 | } |
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index cb308a3a9300..63144e4ca9e1 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c | |||
@@ -2126,8 +2126,7 @@ static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len, | |||
2126 | u8 *csum) | 2126 | u8 *csum) |
2127 | { | 2127 | { |
2128 | struct btrfs_ordered_sum *sum = NULL; | 2128 | struct btrfs_ordered_sum *sum = NULL; |
2129 | int ret = 0; | 2129 | unsigned long index; |
2130 | unsigned long i; | ||
2131 | unsigned long num_sectors; | 2130 | unsigned long num_sectors; |
2132 | 2131 | ||
2133 | while (!list_empty(&sctx->csum_list)) { | 2132 | while (!list_empty(&sctx->csum_list)) { |
@@ -2146,19 +2145,14 @@ static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len, | |||
2146 | if (!sum) | 2145 | if (!sum) |
2147 | return 0; | 2146 | return 0; |
2148 | 2147 | ||
2148 | index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize; | ||
2149 | num_sectors = sum->len / sctx->sectorsize; | 2149 | num_sectors = sum->len / sctx->sectorsize; |
2150 | for (i = 0; i < num_sectors; ++i) { | 2150 | memcpy(csum, sum->sums + index, sctx->csum_size); |
2151 | if (sum->sums[i].bytenr == logical) { | 2151 | if (index == num_sectors - 1) { |
2152 | memcpy(csum, &sum->sums[i].sum, sctx->csum_size); | ||
2153 | ret = 1; | ||
2154 | break; | ||
2155 | } | ||
2156 | } | ||
2157 | if (ret && i == num_sectors - 1) { | ||
2158 | list_del(&sum->list); | 2152 | list_del(&sum->list); |
2159 | kfree(sum); | 2153 | kfree(sum); |
2160 | } | 2154 | } |
2161 | return ret; | 2155 | return 1; |
2162 | } | 2156 | } |
2163 | 2157 | ||
2164 | /* scrub extent tries to collect up to 64 kB for each bio */ | 2158 | /* scrub extent tries to collect up to 64 kB for each bio */ |