mirror of
https://github.com/kdave/btrfs-progs
synced 2025-02-16 09:46:55 +00:00
btrfs-progs: mkfs: add helper for writing empty tree nodes
With extent tree v2 we're going to be writing some more empty trees for the initial mkfs step, so take this common code and make it a helper. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
9b4207a0f9
commit
41c3fa5d0b
@ -39,6 +39,25 @@ static u64 reference_root_table[] = {
|
||||
[MKFS_CSUM_TREE] = BTRFS_CSUM_TREE_OBJECTID,
|
||||
};
|
||||
|
||||
static int btrfs_write_empty_tree(int fd, struct btrfs_mkfs_config *cfg,
|
||||
struct extent_buffer *buf, u64 objectid,
|
||||
u64 block)
|
||||
{
|
||||
int ret;
|
||||
|
||||
memset(buf->data + sizeof(struct btrfs_header), 0,
|
||||
cfg->nodesize - sizeof(struct btrfs_header));
|
||||
btrfs_set_header_bytenr(buf, block);
|
||||
btrfs_set_header_owner(buf, objectid);
|
||||
btrfs_set_header_nritems(buf, 0);
|
||||
csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
|
||||
cfg->csum_type);
|
||||
ret = pwrite(fd, buf->data, cfg->nodesize, block);
|
||||
if (ret != cfg->nodesize)
|
||||
return ret < 0 ? -errno : -EIO;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
|
||||
struct extent_buffer *buf,
|
||||
const enum btrfs_mkfs_block *blocks,
|
||||
@ -453,31 +472,15 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
|
||||
}
|
||||
|
||||
/* create the FS root */
|
||||
memset(buf->data + sizeof(struct btrfs_header), 0,
|
||||
cfg->nodesize - sizeof(struct btrfs_header));
|
||||
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_FS_TREE]);
|
||||
btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
|
||||
btrfs_set_header_nritems(buf, 0);
|
||||
csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
|
||||
cfg->csum_type);
|
||||
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_FS_TREE]);
|
||||
if (ret != cfg->nodesize) {
|
||||
ret = (ret < 0 ? -errno : -EIO);
|
||||
ret = btrfs_write_empty_tree(fd, cfg, buf, BTRFS_FS_TREE_OBJECTID,
|
||||
cfg->blocks[MKFS_FS_TREE]);
|
||||
if (ret)
|
||||
goto out;
|
||||
}
|
||||
/* finally create the csum root */
|
||||
memset(buf->data + sizeof(struct btrfs_header), 0,
|
||||
cfg->nodesize - sizeof(struct btrfs_header));
|
||||
btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CSUM_TREE]);
|
||||
btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
|
||||
btrfs_set_header_nritems(buf, 0);
|
||||
csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
|
||||
cfg->csum_type);
|
||||
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CSUM_TREE]);
|
||||
if (ret != cfg->nodesize) {
|
||||
ret = (ret < 0 ? -errno : -EIO);
|
||||
ret = btrfs_write_empty_tree(fd, cfg, buf, BTRFS_CSUM_TREE_OBJECTID,
|
||||
cfg->blocks[MKFS_CSUM_TREE]);
|
||||
if (ret)
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* and write out the super block */
|
||||
memset(buf->data, 0, BTRFS_SUPER_INFO_SIZE);
|
||||
|
Loading…
Reference in New Issue
Block a user