diff mbox

btrfs-progs: Make the process of create root tree to a function

Message ID 1503645816-3450-1-git-send-email-gujx@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Gu Jinxiang Aug. 25, 2017, 7:23 a.m. UTC
make_btrfs is too long to understand, make creatation of root tree
in a function.

Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
---
 mkfs/common.c | 136 +++++++++++++++++++++++++++-------------------------------
 1 file changed, 64 insertions(+), 72 deletions(-)

Comments

David Sterba Sept. 4, 2017, 4:47 p.m. UTC | #1
On Fri, Aug 25, 2017 at 03:23:36PM +0800, Gu Jinxiang wrote:
> make_btrfs is too long to understand, make creatation of root tree
> in a function.

Good cleanup, but the changelog could be improved. There are some
changes not obvious from the changes, like that you now iterate some of
the roots and use the reference_root_table for translation. This makes
it a bit harder to review the patch as the code is not just moved to a
function but also improved on the way. Sometimes it's better to do it in
two steps, but not absolutely necessary in this case.

Nevertheless, patch applied, with the updated changelog.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/mkfs/common.c b/mkfs/common.c
index 7d482c2..78297fa 100644
--- a/mkfs/common.c
+++ b/mkfs/common.c
@@ -34,6 +34,68 @@  static u64 reference_root_table[] = {
 	[6] =	BTRFS_CSUM_TREE_OBJECTID,
 };
 
+static int btrfs_create_root_tree(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 = 0;
+	int ret = 0;
+	int blk = 0;
+
+	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 = 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
  *
@@ -44,10 +106,8 @@  int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
 {
 	struct btrfs_super_block super;
 	struct extent_buffer *buf;
-	struct btrfs_root_item root_item;
 	struct btrfs_disk_key disk_key;
 	struct btrfs_extent_item *extent_item;
-	struct btrfs_inode_item *inode_item;
 	struct btrfs_chunk *chunk;
 	struct btrfs_dev_item *dev_item;
 	struct btrfs_dev_extent *dev_extent;
@@ -133,77 +193,9 @@  int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
 			    btrfs_header_chunk_tree_uuid(buf),
 			    BTRFS_UUID_SIZE);
 
-	/* create the items for the root tree */
-	memset(&root_item, 0, sizeof(root_item));
-	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);
+	ret = btrfs_create_root_tree(fd, cfg, buf);
+	if (ret < 0)
 		goto out;
-	}
 
 	/* create the items for the extent tree */
 	memset(buf->data + sizeof(struct btrfs_header), 0,