On 10.05.19 г. 14:15 ч., Johannes Thumshirn wrote:
> Now that we everything in place, we can add SHA-256 as another checksum
> algorithm.
>
> SHA-256 was taken as it was the cryptographically strongest algorithm that
> can fit into the 32 Bytes we have left.
>
> Signed-off-by: Johannes Thumshirn <jthumshirn@xxxxxxx>
LGTM:
Reviewed-by: Nikolay Borisov <nborisov@xxxxxxxx>
> ---
> fs/btrfs/btrfs_inode.h | 3 +++
> fs/btrfs/ctree.h | 4 ++--
> fs/btrfs/disk-io.c | 2 ++
> include/uapi/linux/btrfs_tree.h | 1 +
> 4 files changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
> index e79fd9129075..fccc372ef719 100644
> --- a/fs/btrfs/btrfs_inode.h
> +++ b/fs/btrfs/btrfs_inode.h
> @@ -346,6 +346,9 @@ static inline void btrfs_csum_format(struct btrfs_super_block *sb,
> case BTRFS_CSUM_TYPE_CRC32:
> snprintf(cbuf, size, "0x%08x", *(u32 *)csum);
> break;
> + case BTRFS_CSUM_TYPE_SHA256:
> + memcpy(cbuf, csum, size);
> + break;
> default: /* can't happen - csum type is validated at mount time */
> break;
> }
> diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> index 8733c55ed686..d60138208dd4 100644
> --- a/fs/btrfs/ctree.h
> +++ b/fs/btrfs/ctree.h
> @@ -72,8 +72,8 @@ struct btrfs_ref;
> #define BTRFS_LINK_MAX 65535U
>
> /* four bytes for CRC32 */
> -static const int btrfs_csum_sizes[] = { 4 };
> -static char *btrfs_csum_names[] = { "crc32c" };
> +static const int btrfs_csum_sizes[] = { 4, 32 };
> +static char *btrfs_csum_names[] = { "crc32c", "sha256" };
>
> #define BTRFS_EMPTY_DIR_SIZE 0
>
> diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
> index 2be8f05be1e6..bdcffa0d6b13 100644
> --- a/fs/btrfs/disk-io.c
> +++ b/fs/btrfs/disk-io.c
> @@ -390,6 +390,8 @@ static bool btrfs_supported_super_csum(struct btrfs_super_block *sb)
> switch (btrfs_super_csum_type(sb)) {
> case BTRFS_CSUM_TYPE_CRC32:
> return true;
> + case BTRFS_CSUM_TYPE_SHA256:
> + return true;
nit: case BTRFS_CSUM_TYPE_CRC32:
CASE BTRFS_CSUM_TYPE_SHA256:
return true;
> default:
> return false;
> }
> diff --git a/include/uapi/linux/btrfs_tree.h b/include/uapi/linux/btrfs_tree.h
> index 421239b98db2..3667ab4bc215 100644
> --- a/include/uapi/linux/btrfs_tree.h
> +++ b/include/uapi/linux/btrfs_tree.h
> @@ -301,6 +301,7 @@
>
> /* csum types */
> #define BTRFS_CSUM_TYPE_CRC32 0
> +#define BTRFS_CSUM_TYPE_SHA256 1
nit: Might be a good idea to turn that into an enum for self-documenting
purposes. Perhaps in a different patch.
>
> /*
> * flags definitions for directory entry item type
>