btrfs-progs: mkfs: Move the tree root creation to own function
make_btrfs is too long to understand, make creatation of root tree in a function. Some of the tree roots are now created in a loop, where the code is just copypasted. We now make use of the reference_root_table to translate block index to root objectid. Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com> [ updated changelog ] Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
parent
df4a04484a
commit
944e14da14
135
mkfs/common.c
135
mkfs/common.c
|
@ -34,6 +34,67 @@ static u64 reference_root_table[] = {
|
||||||
[6] = BTRFS_CSUM_TREE_OBJECTID,
|
[6] = BTRFS_CSUM_TREE_OBJECTID,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
|
||||||
|
struct extent_buffer *buf)
|
||||||
|
{
|
||||||
|
struct btrfs_root_item root_item;
|
||||||
|
struct btrfs_inode_item *inode_item;
|
||||||
|
struct btrfs_disk_key disk_key;
|
||||||
|
u32 nritems = 0;
|
||||||
|
u32 itemoff;
|
||||||
|
int ret = 0;
|
||||||
|
int blk;
|
||||||
|
|
||||||
|
memset(buf->data + sizeof(struct btrfs_header), 0,
|
||||||
|
cfg->nodesize - sizeof(struct btrfs_header));
|
||||||
|
memset(&root_item, 0, sizeof(root_item));
|
||||||
|
memset(&disk_key, 0, sizeof(disk_key));
|
||||||
|
|
||||||
|
/* create the items for the root tree */
|
||||||
|
inode_item = &root_item.inode;
|
||||||
|
btrfs_set_stack_inode_generation(inode_item, 1);
|
||||||
|
btrfs_set_stack_inode_size(inode_item, 3);
|
||||||
|
btrfs_set_stack_inode_nlink(inode_item, 1);
|
||||||
|
btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
|
||||||
|
btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
|
||||||
|
btrfs_set_root_refs(&root_item, 1);
|
||||||
|
btrfs_set_root_used(&root_item, cfg->nodesize);
|
||||||
|
btrfs_set_root_generation(&root_item, 1);
|
||||||
|
|
||||||
|
btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
|
||||||
|
btrfs_set_disk_key_offset(&disk_key, 0);
|
||||||
|
itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
|
||||||
|
|
||||||
|
for (blk = 0; blk < MKFS_BLOCK_COUNT; blk++) {
|
||||||
|
if (blk == MKFS_SUPER_BLOCK || blk == MKFS_ROOT_TREE
|
||||||
|
|| blk == MKFS_CHUNK_TREE)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
btrfs_set_root_bytenr(&root_item, cfg->blocks[blk]);
|
||||||
|
btrfs_set_disk_key_objectid(&disk_key,
|
||||||
|
reference_root_table[blk]);
|
||||||
|
btrfs_set_item_key(buf, &disk_key, nritems);
|
||||||
|
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
|
||||||
|
btrfs_set_item_size(buf, btrfs_item_nr(nritems),
|
||||||
|
sizeof(root_item));
|
||||||
|
write_extent_buffer(buf, &root_item,
|
||||||
|
btrfs_item_ptr_offset(buf, nritems),
|
||||||
|
sizeof(root_item));
|
||||||
|
nritems++;
|
||||||
|
itemoff -= sizeof(root_item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* generate checksum */
|
||||||
|
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
|
||||||
|
|
||||||
|
/* write back root tree */
|
||||||
|
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_ROOT_TREE]);
|
||||||
|
if (ret != cfg->nodesize)
|
||||||
|
return (ret < 0 ? -errno : -EIO);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
|
* @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
|
||||||
*
|
*
|
||||||
|
@ -44,10 +105,8 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
|
||||||
{
|
{
|
||||||
struct btrfs_super_block super;
|
struct btrfs_super_block super;
|
||||||
struct extent_buffer *buf;
|
struct extent_buffer *buf;
|
||||||
struct btrfs_root_item root_item;
|
|
||||||
struct btrfs_disk_key disk_key;
|
struct btrfs_disk_key disk_key;
|
||||||
struct btrfs_extent_item *extent_item;
|
struct btrfs_extent_item *extent_item;
|
||||||
struct btrfs_inode_item *inode_item;
|
|
||||||
struct btrfs_chunk *chunk;
|
struct btrfs_chunk *chunk;
|
||||||
struct btrfs_dev_item *dev_item;
|
struct btrfs_dev_item *dev_item;
|
||||||
struct btrfs_dev_extent *dev_extent;
|
struct btrfs_dev_extent *dev_extent;
|
||||||
|
@ -133,77 +192,9 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
|
||||||
btrfs_header_chunk_tree_uuid(buf),
|
btrfs_header_chunk_tree_uuid(buf),
|
||||||
BTRFS_UUID_SIZE);
|
BTRFS_UUID_SIZE);
|
||||||
|
|
||||||
/* create the items for the root tree */
|
ret = btrfs_create_tree_root(fd, cfg, buf);
|
||||||
memset(&root_item, 0, sizeof(root_item));
|
if (ret < 0)
|
||||||
inode_item = &root_item.inode;
|
|
||||||
btrfs_set_stack_inode_generation(inode_item, 1);
|
|
||||||
btrfs_set_stack_inode_size(inode_item, 3);
|
|
||||||
btrfs_set_stack_inode_nlink(inode_item, 1);
|
|
||||||
btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
|
|
||||||
btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
|
|
||||||
btrfs_set_root_refs(&root_item, 1);
|
|
||||||
btrfs_set_root_used(&root_item, cfg->nodesize);
|
|
||||||
btrfs_set_root_generation(&root_item, 1);
|
|
||||||
|
|
||||||
memset(&disk_key, 0, sizeof(disk_key));
|
|
||||||
btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
|
|
||||||
btrfs_set_disk_key_offset(&disk_key, 0);
|
|
||||||
nritems = 0;
|
|
||||||
|
|
||||||
itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
|
|
||||||
btrfs_set_root_bytenr(&root_item, cfg->blocks[MKFS_EXTENT_TREE]);
|
|
||||||
btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
|
|
||||||
btrfs_set_item_key(buf, &disk_key, nritems);
|
|
||||||
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
|
|
||||||
btrfs_set_item_size(buf, btrfs_item_nr(nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
|
|
||||||
nritems), sizeof(root_item));
|
|
||||||
nritems++;
|
|
||||||
|
|
||||||
itemoff = itemoff - sizeof(root_item);
|
|
||||||
btrfs_set_root_bytenr(&root_item, cfg->blocks[MKFS_DEV_TREE]);
|
|
||||||
btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
|
|
||||||
btrfs_set_item_key(buf, &disk_key, nritems);
|
|
||||||
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
|
|
||||||
btrfs_set_item_size(buf, btrfs_item_nr(nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
write_extent_buffer(buf, &root_item,
|
|
||||||
btrfs_item_ptr_offset(buf, nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
nritems++;
|
|
||||||
|
|
||||||
itemoff = itemoff - sizeof(root_item);
|
|
||||||
btrfs_set_root_bytenr(&root_item, cfg->blocks[MKFS_FS_TREE]);
|
|
||||||
btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
|
|
||||||
btrfs_set_item_key(buf, &disk_key, nritems);
|
|
||||||
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
|
|
||||||
btrfs_set_item_size(buf, btrfs_item_nr(nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
write_extent_buffer(buf, &root_item,
|
|
||||||
btrfs_item_ptr_offset(buf, nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
nritems++;
|
|
||||||
|
|
||||||
itemoff = itemoff - sizeof(root_item);
|
|
||||||
btrfs_set_root_bytenr(&root_item, cfg->blocks[MKFS_CSUM_TREE]);
|
|
||||||
btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
|
|
||||||
btrfs_set_item_key(buf, &disk_key, nritems);
|
|
||||||
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
|
|
||||||
btrfs_set_item_size(buf, btrfs_item_nr(nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
write_extent_buffer(buf, &root_item,
|
|
||||||
btrfs_item_ptr_offset(buf, nritems),
|
|
||||||
sizeof(root_item));
|
|
||||||
nritems++;
|
|
||||||
|
|
||||||
|
|
||||||
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
|
|
||||||
ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_ROOT_TREE]);
|
|
||||||
if (ret != cfg->nodesize) {
|
|
||||||
ret = (ret < 0 ? -errno : -EIO);
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
|
||||||
|
|
||||||
/* create the items for the extent tree */
|
/* create the items for the extent tree */
|
||||||
memset(buf->data + sizeof(struct btrfs_header), 0,
|
memset(buf->data + sizeof(struct btrfs_header), 0,
|
||||||
|
|
Loading…
Reference in New Issue