btrfs-progs: add table for checksum type and name

Adding this table will make extending btrfs-progs with new checksum types
easier.

Also add accessor functions to access the table fields.

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Johannes Thumshirn 2019-09-25 15:37:24 +02:00 committed by David Sterba
parent f3d7675c08
commit e4a8e1916d
8 changed files with 51 additions and 30 deletions

View File

@ -339,12 +339,8 @@ static void dump_superblock(struct btrfs_super_block *sb, int full)
if (!is_valid_csum_type(csum_type)) { if (!is_valid_csum_type(csum_type)) {
printf("INVALID"); printf("INVALID");
} else { } else {
if (csum_type == BTRFS_CSUM_TYPE_CRC32) { printf("%s", btrfs_super_csum_name(csum_type));
printf("crc32c"); csum_size = btrfs_super_csum_size(sb);
csum_size = btrfs_csum_sizes[csum_type];
} else {
printf("unknown");
}
} }
printf(")\n"); printf(")\n");
printf("csum_size\t\t%llu\n", (unsigned long long)csum_size); printf("csum_size\t\t%llu\n", (unsigned long long)csum_size);

View File

@ -224,7 +224,7 @@ static inline int write_temp_extent_buffer(int fd, struct extent_buffer *buf,
{ {
int ret; int ret;
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
/* Temporary extent buffer is always mapped 1:1 on disk */ /* Temporary extent buffer is always mapped 1:1 on disk */

View File

@ -1058,8 +1058,8 @@ static int migrate_super_block(int fd, u64 old_bytenr)
BUG_ON(btrfs_super_bytenr(super) != old_bytenr); BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET); btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
csum_tree_block_size(buf, btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32], 0, csum_tree_block_size(buf, btrfs_super_csum_size(super),
btrfs_super_csum_type(super)); 0, btrfs_super_csum_type(super));
ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
BTRFS_SUPER_INFO_OFFSET); BTRFS_SUPER_INFO_OFFSET);
if (ret != BTRFS_SUPER_INFO_SIZE) if (ret != BTRFS_SUPER_INFO_SIZE)

29
ctree.c
View File

@ -38,6 +38,35 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
struct extent_buffer *dst_buf, struct extent_buffer *dst_buf,
struct extent_buffer *src_buf); struct extent_buffer *src_buf);
static struct btrfs_csum {
u16 size;
const char *name;
} btrfs_csums[] = {
[BTRFS_CSUM_TYPE_CRC32] = { 4, "crc32c" },
};
u16 btrfs_super_csum_size(const struct btrfs_super_block *sb)
{
const u16 csum_type = btrfs_super_csum_type(sb);
return btrfs_csums[csum_type].size;
}
const char *btrfs_super_csum_name(u16 csum_type)
{
return btrfs_csums[csum_type].name;
}
size_t btrfs_super_num_csums(void)
{
return ARRAY_SIZE(btrfs_csums);
}
u16 btrfs_csum_type_size(u16 csum_type)
{
return btrfs_csums[csum_type].size;
}
inline void btrfs_init_path(struct btrfs_path *p) inline void btrfs_init_path(struct btrfs_path *p)
{ {
memset(p, 0, sizeof(*p)); memset(p, 0, sizeof(*p));

15
ctree.h
View File

@ -169,9 +169,6 @@ enum btrfs_csum_type {
BTRFS_CSUM_TYPE_CRC32 = 0, BTRFS_CSUM_TYPE_CRC32 = 0,
}; };
/* four bytes for CRC32 */
static int btrfs_csum_sizes[] = { 4 };
#define BTRFS_EMPTY_DIR_SIZE 0 #define BTRFS_EMPTY_DIR_SIZE 0
#define BTRFS_FT_UNKNOWN 0 #define BTRFS_FT_UNKNOWN 0
@ -2263,13 +2260,6 @@ BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
uuid_tree_generation, 64); uuid_tree_generation, 64);
BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64); BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
static inline int btrfs_super_csum_size(struct btrfs_super_block *s)
{
int t = btrfs_super_csum_type(s);
BUG_ON(t >= ARRAY_SIZE(btrfs_csum_sizes));
return btrfs_csum_sizes[t];
}
static inline unsigned long btrfs_leaf_data(struct extent_buffer *l) static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
{ {
return offsetof(struct btrfs_leaf, items); return offsetof(struct btrfs_leaf, items);
@ -2700,6 +2690,11 @@ void btrfs_set_item_key_unsafe(struct btrfs_root *root,
struct btrfs_path *path, struct btrfs_path *path,
struct btrfs_key *new_key); struct btrfs_key *new_key);
u16 btrfs_super_csum_size(const struct btrfs_super_block *s);
const char *btrfs_super_csum_name(u16 csum_type);
u16 btrfs_csum_type_size(u16 csum_type);
size_t btrfs_super_num_csums(void);
/* root-item.c */ /* root-item.c */
int btrfs_add_root_ref(struct btrfs_trans_handle *trans, int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
struct btrfs_root *tree_root, struct btrfs_root *tree_root,

View File

@ -1377,11 +1377,11 @@ int btrfs_check_super(struct btrfs_super_block *sb, unsigned sbflags)
} }
csum_type = btrfs_super_csum_type(sb); csum_type = btrfs_super_csum_type(sb);
if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) { if (csum_type >= btrfs_super_num_csums()) {
error("unsupported checksum algorithm %u", csum_type); error("unsupported checksum algorithm %u", csum_type);
return -EIO; return -EIO;
} }
csum_size = btrfs_csum_sizes[csum_type]; csum_size = btrfs_super_csum_size(sb);
btrfs_csum_data(csum_type, (u8 *)sb + BTRFS_CSUM_SIZE, btrfs_csum_data(csum_type, (u8 *)sb + BTRFS_CSUM_SIZE,
result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE); result, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);

View File

@ -121,11 +121,12 @@ static struct extent_buffer *alloc_dummy_eb(u64 bytenr, u32 size);
static void csum_block(u8 *buf, size_t len) static void csum_block(u8 *buf, size_t len)
{ {
u8 result[btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32]]; u16 csum_size = btrfs_csum_type_size(BTRFS_CSUM_TYPE_CRC32);
u8 result[csum_size];
u32 crc = ~(u32)0; u32 crc = ~(u32)0;
crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE); crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len - BTRFS_CSUM_SIZE);
put_unaligned_le32(~crc, result); put_unaligned_le32(~crc, result);
memcpy(buf, result, btrfs_csum_sizes[BTRFS_CSUM_TYPE_CRC32]); memcpy(buf, result, csum_size);
} }
static int has_name(struct btrfs_key *key) static int has_name(struct btrfs_key *key)

View File

@ -101,7 +101,7 @@ static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
} }
/* generate checksum */ /* generate checksum */
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
/* write back root tree */ /* write back root tree */
@ -293,7 +293,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]); btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]);
btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID); btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
btrfs_set_header_nritems(buf, nritems); btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_EXTENT_TREE]); ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_EXTENT_TREE]);
if (ret != cfg->nodesize) { if (ret != cfg->nodesize) {
@ -382,7 +382,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CHUNK_TREE]); btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CHUNK_TREE]);
btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID); btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
btrfs_set_header_nritems(buf, nritems); btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CHUNK_TREE]); ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CHUNK_TREE]);
if (ret != cfg->nodesize) { if (ret != cfg->nodesize) {
@ -423,7 +423,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_DEV_TREE]); btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_DEV_TREE]);
btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID); btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
btrfs_set_header_nritems(buf, nritems); btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_DEV_TREE]); ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_DEV_TREE]);
if (ret != cfg->nodesize) { if (ret != cfg->nodesize) {
@ -437,7 +437,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_FS_TREE]); btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_FS_TREE]);
btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID); btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
btrfs_set_header_nritems(buf, 0); btrfs_set_header_nritems(buf, 0);
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_FS_TREE]); ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_FS_TREE]);
if (ret != cfg->nodesize) { if (ret != cfg->nodesize) {
@ -450,7 +450,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CSUM_TREE]); btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CSUM_TREE]);
btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID); btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
btrfs_set_header_nritems(buf, 0); btrfs_set_header_nritems(buf, 0);
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CSUM_TREE]); ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CSUM_TREE]);
if (ret != cfg->nodesize) { if (ret != cfg->nodesize) {
@ -462,7 +462,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE); memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
memcpy(buf->data, &super, sizeof(super)); memcpy(buf->data, &super, sizeof(super));
buf->len = BTRFS_SUPER_INFO_SIZE; buf->len = BTRFS_SUPER_INFO_SIZE;
csum_tree_block_size(buf, btrfs_csum_sizes[cfg->csum_type], 0, csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
cfg->csum_type); cfg->csum_type);
ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
cfg->blocks[MKFS_SUPER_BLOCK]); cfg->blocks[MKFS_SUPER_BLOCK]);