All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Subject: [RFC PATCH 21/22] btrfs-progs: convert: Introduce init_btrfs_v2 function.
Date: Wed, 18 Nov 2015 15:22:21 +0800	[thread overview]
Message-ID: <1447831342-15056-22-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1447831342-15056-1-git-send-email-quwenruo@cn.fujitsu.com>

Introduce new init_btrfs_v2() function for later newer do_convert().

Since we have good enough chunk allocation, a lot of wired chunk hack
won't ever be used.
We only need to insert data chunks and create needed subvolume/inode.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfs-convert.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)

diff --git a/btrfs-convert.c b/btrfs-convert.c
index 993ee80..9e8158d 100644
--- a/btrfs-convert.c
+++ b/btrfs-convert.c
@@ -2092,6 +2092,102 @@ static int make_convert_data_block_groups(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+/*
+ * Init the temp btrfs to a operational status.
+ *
+ * It will fix the extent usage accounting and insert needed data chunks, to
+ * ensure all ext* data extents are covered by DATA chunks, preventing wrong
+ * chunks are allocated.
+ * And also create convert image subvolume and relocation tree.
+ */
+static int init_btrfs_v2(struct btrfs_mkfs_config *cfg, ext2_filsys ext2_fs,
+			 struct btrfs_root *root, int datacsum, int packing,
+			 int noxattr)
+{
+	struct btrfs_key location;
+	struct btrfs_trans_handle *trans;
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct ext2_inode ext2_inode;
+	ext2_inode_scan ext2_scan;
+	ext2_ino_t ext2_ino;
+	int found_root = 0;
+	int ret;
+
+	trans = btrfs_start_transaction(root, 1);
+	BUG_ON(!trans);
+	ret = btrfs_fix_block_accounting(trans, root);
+	if (ret)
+		goto err;
+	ret = make_convert_data_block_groups(trans, fs_info, cfg);
+	if (ret)
+		goto err;
+	ret = btrfs_make_root_dir(trans, fs_info->tree_root,
+				  BTRFS_ROOT_TREE_DIR_OBJECTID);
+	if (ret)
+		goto err;
+	memcpy(&location, &root->root_key, sizeof(location));
+	location.offset = (u64)-1;
+	ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
+				btrfs_super_root_dir(fs_info->super_copy),
+				&location, BTRFS_FT_DIR, 0);
+	if (ret)
+		goto err;
+	ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
+				location.objectid,
+				btrfs_super_root_dir(fs_info->super_copy), 0);
+	if (ret)
+		goto err;
+	btrfs_set_root_dirid(&fs_info->fs_root->root_item,
+			     BTRFS_FIRST_FREE_OBJECTID);
+
+	/* subvol for ext2 image file */
+	ret = create_subvol(trans, root, CONV_IMAGE_SUBVOL_OBJECTID);
+	BUG_ON(ret);
+	/* subvol for data relocation */
+	ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
+	BUG_ON(ret);
+
+	/*
+	 * Copy root inode for fs_root. Or we will be unable to do
+	 * link_subvol().
+	 * And it can't be done before create_subvol(), as it will copy
+	 * tree root, including all its contents.
+	 */
+	ret = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
+	if (ret) {
+		fprintf(stderr, "ext2fs_open_inode_scan: %s\n",
+			error_message(ret));
+		return -EIO;
+	}
+
+	while (!(ret = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
+					     &ext2_inode))) {
+		if (ext2_ino == 0 || ext2_ino >= EXT2_GOOD_OLD_FIRST_INO)
+			break;
+		if (ext2_ino == EXT2_ROOT_INO) {
+			found_root = 1;
+			break;
+		}
+	}
+	ext2fs_close_inode_scan(ext2_scan);
+
+	if (found_root) {
+		ret = copy_single_inode(trans, root, BTRFS_FIRST_FREE_OBJECTID,
+					ext2_fs, ext2_ino, &ext2_inode,
+					datacsum, packing, noxattr);
+		if (ret < 0)
+			goto err;
+	} else {
+		ret = -ENOENT;
+		goto err;
+	}
+
+	ret = btrfs_commit_transaction(trans, root);
+	BUG_ON(ret);
+err:
+	return ret;
+}
+
 static int init_btrfs(struct btrfs_root *root)
 {
 	int ret;
-- 
2.6.2


  parent reply	other threads:[~2015-11-18  7:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-18  7:22 [RFC PATCH 00/22] Btrfs-convert rework part 1 Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 01/22] btrfs-progs: extent-cache: Add comments for search/lookup functions Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 02/22] btrfs-progs: extent-tree: Add add_merge_cache_extent function Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 03/22] btrfs-progs: Add new init/free function and member for mkfs_config Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 04/22] btrfs-progs: convert: Read and build up used space tree Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 05/22] btrfs-progs: utils: Introduce new function to remove reserved ranges Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 06/22] btrfs-progs: utils: Introduce function to calculate the available space Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 07/22] btrfs-progs: Reserve space for system/meta chunks and superblock Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 08/22] btrfs-progs: Introduce function to setup temporary superblock Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 09/22] btrfs-progs: Introduce function to setup temporary tree root Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 10/22] btrfs-progs: Introduce function to setup temporary chunk root Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 11/22] btrfs-progs: Introduce function to initialize device tree Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 12/22] btrfs-progs: Introduce function to initialize fs tree Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 13/22] btrfs-progs: Introduce function to initialize csum tree Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 14/22] btrfs-progs: Introduce function to setup temporary extent tree Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 15/22] btrfs-progs: Introduce function to create convert data chunks Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 16/22] btrfs-progs: extent-tree: Introduce function to find the first overlap extent Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 17/22] btrfs-progs: extent-tree: Enhance btrfs_record_file_extent Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 18/22] btrfs-progs: convert: Introduce new function to create ext2 image Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 19/22] btrfs-progs: convert: Introduce function to migrate reserved ranges Qu Wenruo
2015-11-18  7:22 ` [RFC PATCH 20/22] btrfs-progs: Enhance record_file_blocks to handle " Qu Wenruo
2015-11-18  7:22 ` Qu Wenruo [this message]
2015-11-18  7:22 ` [RFC PATCH 22/22] btrfs-progs: Introduce do_convert_v2 function Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447831342-15056-22-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.