btrfs-progs: pass checksum type to btrfs_csum_data()/btrfs_csum_final()

In preparation to supporting new checksum algorithm pass the checksum type
to btrfs_csum_data/btrfs_csum_final, this allows us to encapsulate any
differences in processing into the respective functions

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Johannes Thumshirn 2019-09-03 17:00:39 +02:00 committed by David Sterba
parent de68086e35
commit 7b4f1035a6
10 changed files with 63 additions and 35 deletions

View File

@ -35,11 +35,13 @@ static int check_csum_superblock(void *sb)
{
u8 result[csum_size];
u32 crc = ~(u32)0;
u16 csum_type = btrfs_super_csum_type(sb);
crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE,
crc = btrfs_csum_data(csum_type,
(char *)sb + BTRFS_CSUM_SIZE,
(u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, result);
btrfs_csum_final(csum_type, crc, result);
return !memcmp(sb, &result, csum_size);
}
@ -49,17 +51,20 @@ static void update_block_csum(void *block, int is_sb)
u8 result[csum_size];
struct btrfs_header *hdr;
u32 crc = ~(u32)0;
u16 csum_type = btrfs_super_csum_type(block);
if (is_sb) {
crc = btrfs_csum_data((char *)block + BTRFS_CSUM_SIZE,
crc = btrfs_csum_data(csum_type,
(char *)block + BTRFS_CSUM_SIZE,
(u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
} else {
crc = btrfs_csum_data((char *)block + BTRFS_CSUM_SIZE,
crc = btrfs_csum_data(csum_type,
(char *)block + BTRFS_CSUM_SIZE,
(u8 *)&crc,
BLOCKSIZE - BTRFS_CSUM_SIZE);
}
btrfs_csum_final(crc, result);
btrfs_csum_final(csum_type, crc, result);
memset(block, 0, BTRFS_CSUM_SIZE);
hdr = (struct btrfs_header *)block;
memcpy(&hdr->csum, result, csum_size);

View File

@ -5619,6 +5619,7 @@ static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
struct btrfs_fs_info *fs_info = root->fs_info;
u64 offset = 0;
u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
char *data;
unsigned long csum_offset;
u32 csum;
@ -5659,9 +5660,10 @@ static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
csum = ~(u32)0;
tmp = offset + data_checked;
csum = btrfs_csum_data((char *)data + tmp,
csum = btrfs_csum_data(csum_type,
(char *)data + tmp,
(u8 *)&csum, fs_info->sectorsize);
btrfs_csum_final(csum, (u8 *)&csum);
btrfs_csum_final(csum_type, csum, (u8 *)&csum);
csum_offset = leaf_offset +
tmp / fs_info->sectorsize * csum_size;

View File

@ -35,15 +35,15 @@
#include "kernel-lib/crc32c.h"
#include "common/help.h"
static int check_csum_sblock(void *sb, int csum_size)
static int check_csum_sblock(void *sb, int csum_size, u16 csum_type)
{
u8 result[BTRFS_CSUM_SIZE];
u32 crc = ~(u32)0;
crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE,
crc = btrfs_csum_data(csum_type, (char *)sb + BTRFS_CSUM_SIZE,
(u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, result);
btrfs_csum_final(csum_type, crc, result);
return !memcmp(sb, &result, csum_size);
}
@ -348,7 +348,7 @@ static void dump_superblock(struct btrfs_super_block *sb, int full)
if (csum_type != BTRFS_CSUM_TYPE_CRC32 ||
csum_size != btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32])
printf(" [UNKNOWN CSUM TYPE OR SIZE]");
else if (check_csum_sblock(sb, csum_size))
else if (check_csum_sblock(sb, csum_size, csum_type))
printf(" [match]");
else
printf(" [DON'T MATCH]");

View File

@ -1887,7 +1887,8 @@ static u64 calc_data_offset(struct btrfs_key *key,
return dev_offset + data_offset;
}
static int check_one_csum(int fd, u64 start, u32 len, u32 tree_csum)
static int check_one_csum(int fd, u64 start, u32 len, u32 tree_csum,
u16 csum_type)
{
char *data;
int ret = 0;
@ -1902,8 +1903,8 @@ static int check_one_csum(int fd, u64 start, u32 len, u32 tree_csum)
goto out;
}
ret = 0;
csum_result = btrfs_csum_data(data, (u8 *)&csum_result, len);
btrfs_csum_final(csum_result, (u8 *)&csum_result);
csum_result = btrfs_csum_data(csum_type, data, (u8 *)&csum_result, len);
btrfs_csum_final(csum_type, csum_result, (u8 *)&csum_result);
if (csum_result != tree_csum)
ret = 1;
out:
@ -2102,7 +2103,8 @@ next_csum:
devext->objectid, 1));
ret = check_one_csum(dev->fd, data_offset, blocksize,
tree_csum);
tree_csum,
btrfs_super_csum_type(root->fs_info->super_copy));
if (ret < 0)
goto fail_out;
else if (ret > 0)

View File

@ -63,12 +63,13 @@ static inline int write_temp_super(int fd, struct btrfs_super_block *sb,
u64 sb_bytenr)
{
u32 crc = ~(u32)0;
u16 csum_type = btrfs_super_csum_type(sb);
int ret;
crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE,
crc = btrfs_csum_data(csum_type, (char *)sb + BTRFS_CSUM_SIZE,
(u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, &sb->csum[0]);
btrfs_csum_final(csum_type, crc, &sb->csum[0]);
ret = pwrite(fd, sb, BTRFS_SUPER_INFO_SIZE, sb_bytenr);
if (ret < BTRFS_SUPER_INFO_SIZE)
ret = (ret < 0 ? -errno : -EIO);

View File

@ -138,14 +138,26 @@ static void print_tree_block_error(struct btrfs_fs_info *fs_info,
}
}
u32 btrfs_csum_data(char *data, u8 *seed, size_t len)
u32 btrfs_csum_data(u16 csum_type, char *data, u8 *seed, size_t len)
{
return crc32c(*(u32*)seed, data, len);
switch (csum_type) {
case BTRFS_CSUM_TYPE_CRC32:
return crc32c(*(u32*)seed, data, len);
default: /* Not reached */
return ~(u32)0;
}
}
void btrfs_csum_final(u32 crc, u8 *result)
void btrfs_csum_final(u16 csum_type, u32 crc, u8 *result)
{
put_unaligned_le32(~crc, result);
switch (csum_type) {
case BTRFS_CSUM_TYPE_CRC32:
put_unaligned_le32(~crc, result);
break;
default: /* Not reached */
break;
}
}
static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
@ -156,8 +168,9 @@ static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
u32 crc = ~(u32)0;
len = buf->len - BTRFS_CSUM_SIZE;
crc = btrfs_csum_data(buf->data + BTRFS_CSUM_SIZE, (u8 *)&crc, len);
btrfs_csum_final(crc, result);
crc = btrfs_csum_data(csum_type, buf->data + BTRFS_CSUM_SIZE,
(u8 *)&crc, len);
btrfs_csum_final(csum_type, crc, result);
if (verify) {
if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
@ -1376,9 +1389,10 @@ int btrfs_check_super(struct btrfs_super_block *sb, unsigned sbflags)
csum_size = btrfs_csum_sizes[csum_type];
crc = ~(u32)0;
crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, (u8 *)&crc,
crc = btrfs_csum_data(csum_type,(char *)sb + BTRFS_CSUM_SIZE,
(u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, result);
btrfs_csum_final(csum_type, crc, result);
if (memcmp(result, sb->csum, csum_size)) {
error("superblock checksum mismatch");
@ -1616,6 +1630,7 @@ static int write_dev_supers(struct btrfs_fs_info *fs_info,
u64 bytenr;
u32 crc;
int i, ret;
u16 csum_type = btrfs_super_csum_type(sb);
/*
* We need to write super block after all metadata written.
@ -1631,9 +1646,9 @@ static int write_dev_supers(struct btrfs_fs_info *fs_info,
if (fs_info->super_bytenr != BTRFS_SUPER_INFO_OFFSET) {
btrfs_set_super_bytenr(sb, fs_info->super_bytenr);
crc = ~(u32)0;
crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, (u8 *)&crc,
crc = btrfs_csum_data(csum_type, (char *)sb + BTRFS_CSUM_SIZE, (u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, &sb->csum[0]);
btrfs_csum_final(csum_type, crc, &sb->csum[0]);
/*
* super_copy is BTRFS_SUPER_INFO_SIZE bytes and is
@ -1667,9 +1682,9 @@ static int write_dev_supers(struct btrfs_fs_info *fs_info,
btrfs_set_super_bytenr(sb, bytenr);
crc = ~(u32)0;
crc = btrfs_csum_data((char *)sb + BTRFS_CSUM_SIZE, (u8 *)&crc,
crc = btrfs_csum_data(csum_type, (char *)sb + BTRFS_CSUM_SIZE, (u8 *)&crc,
BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, &sb->csum[0]);
btrfs_csum_final(csum_type, crc, &sb->csum[0]);
/*
* super_copy is BTRFS_SUPER_INFO_SIZE bytes and is

View File

@ -186,8 +186,8 @@ int btrfs_free_fs_root(struct btrfs_root *root);
void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid);
int btrfs_set_buffer_uptodate(struct extent_buffer *buf);
u32 btrfs_csum_data(char *data, u8 *seed, size_t len);
void btrfs_csum_final(u32 crc, u8 *result);
u32 btrfs_csum_data(u16 csum_type, char *data, u8 *seed, size_t len);
void btrfs_csum_final(u16 csum_type, u32 crc, u8 *result);
int btrfs_open_device(struct btrfs_device *dev);
int csum_tree_block_size(struct extent_buffer *buf, u16 csum_sectorsize,

View File

@ -202,6 +202,9 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
u16 csum_size =
btrfs_super_csum_size(root->fs_info->super_copy);
u16 csum_type =
btrfs_super_csum_type(root->fs_info->super_copy);
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
@ -312,8 +315,8 @@ csum:
item = (struct btrfs_csum_item *)((unsigned char *)item +
csum_offset * csum_size);
found:
csum_result = btrfs_csum_data(data, (u8 *)&csum_result, len);
btrfs_csum_final(csum_result, (u8 *)&csum_result);
csum_result = btrfs_csum_data(csum_type, data, (u8 *)&csum_result, len);
btrfs_csum_final(csum_type, csum_result, (u8 *)&csum_result);
if (csum_result == 0) {
printk("csum result is 0 for block %llu\n",
(unsigned long long)bytenr);

View File

@ -213,7 +213,7 @@ static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
io_ctl_map_page(io_ctl, 0);
crc = crc32c(crc, io_ctl->orig + offset,
io_ctl->root->fs_info->sectorsize - offset);
btrfs_csum_final(crc, (u8 *)&crc);
put_unaligned_le32(~crc, (u8 *)&crc);
if (val != crc) {
printk("btrfs: csum mismatch on free space cache\n");
io_ctl_unmap_page(io_ctl);

View File

@ -124,7 +124,7 @@ static void csum_block(u8 *buf, size_t len)
u8 result[btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32]];
u32 crc = ~(u32)0;
crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
btrfs_csum_final(crc, result);
put_unaligned_le32(~crc, result);
memcpy(buf, result, btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32]);
}