On 22.05.19 г. 11:19 ч., Johannes Thumshirn wrote:
> Currently btrfs_csum_data() relied on the crc32c() wrapper around the crypto
> framework for calculating the CRCs.
>
> As we have our own crypto_shash structure in the fs_info now, we can
> directly call into the crypto framework without going trough the wrapper.
>
> This way we can even remove the btrfs_csum_data() and btrfs_csum_final()
> wrappers.
>
> Signed-off-by: Johannes Thumshirn <jthumsh...@suse.de>
>
> ---
> Changes to v2:
> - Also select CRYPTO in Kconfig
> - Add pre dependency on crc32c
> Changes to v1:
> - merge with 'btrfs: pass in an fs_info to btrfs_csum_{data,final}()'
> - Remove btrfs_csum_data() and btrfs_csum_final() alltogether
> - don't use LIBCRC32C but CRYPTO_CRC32C in KConfig
> ---
> fs/btrfs/Kconfig | 3 ++-
> fs/btrfs/check-integrity.c | 12 +++++++----
> fs/btrfs/compression.c | 19 +++++++++++------
> fs/btrfs/disk-io.c | 51
> +++++++++++++++++++++++++---------------------
> fs/btrfs/disk-io.h | 2 --
> fs/btrfs/file-item.c | 18 ++++++++--------
> fs/btrfs/inode.c | 24 ++++++++++++++--------
> fs/btrfs/scrub.c | 37 +++++++++++++++++++++++++--------
> fs/btrfs/super.c | 1 +
> 9 files changed, 106 insertions(+), 61 deletions(-)
>
<snip>
> @@ -1799,16 +1801,22 @@ static int scrub_checksum_data(struct scrub_block
> *sblock)
> if (!sblock->pagev[0]->have_csum)
> return 0;
>
> + shash->tfm = fs_info->csum_shash;
> + shash->flags = 0;
> +
> + crypto_shash_init(shash);
> +
> on_disk_csum = sblock->pagev[0]->csum;
> page = sblock->pagev[0]->page;
> buffer = kmap_atomic(page);
>
> + memset(csum, 0xff, btrfs_super_csum_size(sctx->fs_info->super_copy));
Is this required? You don't do it in other place like
scrub_checksum_tree_block/scrub_checksum_super/__readpage_endio_check.
If it's not strictly require just drop it.
> len = sctx->fs_info->sectorsize;
> index = 0;
> for (;;) {
> u64 l = min_t(u64, len, PAGE_SIZE);
>
> - crc = btrfs_csum_data(buffer, crc, l);
> + crypto_shash_update(shash, buffer, l);
> kunmap_atomic(buffer);
> len -= l;
> if (len == 0)
> @@ -1820,7 +1828,7 @@ static int scrub_checksum_data(struct scrub_block
> *sblock)
> buffer = kmap_atomic(page);
> }
>
> - btrfs_csum_final(crc, csum);
> + crypto_shash_final(shash, csum);
> if (memcmp(csum, on_disk_csum, sctx->csum_size))
> sblock->checksum_error = 1;
>
> @@ -1832,16 +1840,21 @@ static int scrub_checksum_tree_block(struct
> scrub_block *sblock)
> struct scrub_ctx *sctx = sblock->sctx;
> struct btrfs_header *h;
> struct btrfs_fs_info *fs_info = sctx->fs_info;
> + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
> u8 calculated_csum[BTRFS_CSUM_SIZE];
> u8 on_disk_csum[BTRFS_CSUM_SIZE];
> struct page *page;
> void *mapped_buffer;
> u64 mapped_size;
> void *p;
> - u32 crc = ~(u32)0;
> u64 len;
> int index;
>
> + shash->tfm = fs_info->csum_shash;
> + shash->flags = 0;
> +
> + crypto_shash_init(shash);
> +
> BUG_ON(sblock->page_count < 1);
> page = sblock->pagev[0]->page;
> mapped_buffer = kmap_atomic(page);
> @@ -1875,7 +1888,7 @@ static int scrub_checksum_tree_block(struct scrub_block
> *sblock)
> for (;;) {
> u64 l = min_t(u64, len, mapped_size);
>
> - crc = btrfs_csum_data(p, crc, l);
> + crypto_shash_update(shash, p, l);
> kunmap_atomic(mapped_buffer);
> len -= l;
> if (len == 0)
> @@ -1889,7 +1902,7 @@ static int scrub_checksum_tree_block(struct scrub_block
> *sblock)
> p = mapped_buffer;
> }
>
> - btrfs_csum_final(crc, calculated_csum);
> + crypto_shash_final(shash, calculated_csum);
> if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
> sblock->checksum_error = 1;
>
> @@ -1900,18 +1913,24 @@ static int scrub_checksum_super(struct scrub_block
> *sblock)
> {
> struct btrfs_super_block *s;
> struct scrub_ctx *sctx = sblock->sctx;
> + struct btrfs_fs_info *fs_info = sctx->fs_info;
> + SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
> u8 calculated_csum[BTRFS_CSUM_SIZE];
> u8 on_disk_csum[BTRFS_CSUM_SIZE];
> struct page *page;
> void *mapped_buffer;
> u64 mapped_size;
> void *p;
> - u32 crc = ~(u32)0;
> int fail_gen = 0;
> int fail_cor = 0;
> u64 len;
> int index;
>
> + shash->tfm = fs_info->csum_shash;
> + shash->flags = 0;
> +
> + crypto_shash_init(shash);
> +
> BUG_ON(sblock->page_count < 1);
> page = sblock->pagev[0]->page;
> mapped_buffer = kmap_atomic(page);
> @@ -1934,7 +1953,7 @@ static int scrub_checksum_super(struct scrub_block
> *sblock)
> for (;;) {
> u64 l = min_t(u64, len, mapped_size);
>
> - crc = btrfs_csum_data(p, crc, l);
> + crypto_shash_update(shash, p, l);
> kunmap_atomic(mapped_buffer);
> len -= l;
> if (len == 0)
> @@ -1948,7 +1967,7 @@ static int scrub_checksum_super(struct scrub_block
> *sblock)
> p = mapped_buffer;
> }
>
> - btrfs_csum_final(crc, calculated_csum);
> + crypto_shash_final(shash, calculated_csum);
> if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
> ++fail_cor;
>
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 2c66d9ea6a3b..f40516ca5963 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -2465,3 +2465,4 @@ late_initcall(init_btrfs_fs);
> module_exit(exit_btrfs_fs)
>
> MODULE_LICENSE("GPL");
> +MODULE_SOFTDEP("pre: crc32c");
>