All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 07/25] btrfs-progs: utils: Introduce new function for convert
Date: Tue,  1 Dec 2015 15:11:27 +0800	[thread overview]
Message-ID: <1448953905-28673-8-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1448953905-28673-1-git-send-email-quwenruo@cn.fujitsu.com>

Introduce new function make_convert_btrfs() for convert.

This new function will have the following features:
1) Alloc temporary sb/metadata/system chunk, avoiding old used data
2) More structurizd functions
   No more over 1000 lines function, better function split and code
   reuse

This will finally replace current make_btrfs(), but now only used for
convert.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfs-convert.c | 24 +--------------
 mkfs.c          |  2 +-
 utils.c         | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 utils.h         | 26 ++++++++++++++++-
 4 files changed, 117 insertions(+), 26 deletions(-)

diff --git a/btrfs-convert.c b/btrfs-convert.c
index 8026907..1573e41 100644
--- a/btrfs-convert.c
+++ b/btrfs-convert.c
@@ -96,28 +96,6 @@ struct btrfs_convert_operations {
 	void (*close_fs)(struct btrfs_convert_context *cctx);
 };
 
-struct btrfs_convert_context {
-	u32 blocksize;
-	u32 first_data_block;
-	u32 block_count;
-	u32 inodes_count;
-	u32 free_inodes_count;
-	u64 total_bytes;
-	char *volume_name;
-	const struct btrfs_convert_operations *convert_ops;
-
-	/* The accurate used space of old filesystem */
-	struct cache_tree used;
-
-	/* Batched ranges which must be covered by data chunks */
-	struct cache_tree data_chunks;
-
-	/* Free space which is not covered by data_chunks */
-	struct cache_tree free;
-
-	void *fs_data;
-};
-
 static void init_convert_context(struct btrfs_convert_context *cctx)
 {
 	cache_tree_init(&cctx->used);
@@ -2807,7 +2785,7 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt
 	mkfs_cfg.stripesize = blocksize;
 	mkfs_cfg.features = features;
 
-	ret = make_btrfs(fd, &mkfs_cfg);
+	ret = make_btrfs(fd, &mkfs_cfg, NULL);
 	if (ret) {
 		fprintf(stderr, "unable to create initial ctree: %s\n",
 			strerror(-ret));
diff --git a/mkfs.c b/mkfs.c
index c58ab2f..3ef3890 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -1677,7 +1677,7 @@ int main(int ac, char **av)
 	mkfs_cfg.stripesize = stripesize;
 	mkfs_cfg.features = features;
 
-	ret = make_btrfs(fd, &mkfs_cfg);
+	ret = make_btrfs(fd, &mkfs_cfg, NULL);
 	if (ret) {
 		fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
 		exit(1);
diff --git a/utils.c b/utils.c
index d5f60a4..4310964 100644
--- a/utils.c
+++ b/utils.c
@@ -177,9 +177,96 @@ int test_uuid_unique(char *fs_uuid)
 }
 
 /*
+ * Reserve space from free_tree.
+ * The algorithm is very simple, find the first cache_extent with enough space
+ * and allocate from its beginning.
+ */
+static int reserve_free_space(struct cache_tree *free_tree, u64 len,
+			      u64 *ret_start)
+{
+	struct cache_extent *cache;
+	int found = 0;
+
+	BUG_ON(!ret_start);
+	cache = first_cache_extent(free_tree);
+	while (cache) {
+		if (cache->size > len) {
+			found = 1;
+			*ret_start = cache->start;
+
+			cache->size -= len;
+			if (cache->size == 0) {
+				remove_cache_extent(free_tree, cache);
+				free(cache);
+			} else {
+				cache->start += len;
+			}
+			break;
+		}
+		cache = next_cache_extent(cache);
+	}
+	if (!found)
+		return -ENOSPC;
+	return 0;
+}
+
+/*
+ * Improved version of make_btrfs().
+ *
+ * This one will
+ * 1) Do chunk allocation to avoid used data
+ *    And after this function, extent type matches chunk type
+ * 2) Better structurized code
+ *    No super long hand written codes to initialized all tree blocks
+ *    Split into small blocks and reuse codes.
+ *    TODO: Reuse tree operation facilities by introducing new flags
+ */
+static int make_convert_btrfs(int fd, struct btrfs_mkfs_config *cfg,
+			      struct btrfs_convert_context *cctx)
+{
+	struct cache_tree *free = &cctx->free;
+	struct cache_tree *used = &cctx->used;
+	u64 sys_chunk_start;
+	u64 meta_chunk_start;
+	int ret;
+
+	/* Shouldn't happen */
+	BUG_ON(cache_tree_empty(used));
+
+	/*
+	 * reserve space for temporary superblock first
+	 * Here we allocate a little larger space, to keep later
+	 * free space will be STRIPE_LEN aligned
+	 */
+	ret = reserve_free_space(free, BTRFS_STRIPE_LEN,
+				 &cfg->super_bytenr);
+	if (ret < 0)
+		goto out;
+
+	/*
+	 * Then reserve system chunk space
+	 * TODO: Change system group size depending on cctx->total_bytes.
+	 * If using current 4M, it can only handle less than one TB for
+	 * worst case and then run out of sys space.
+	 */
+	ret = reserve_free_space(free, BTRFS_MKFS_SYSTEM_GROUP_SIZE,
+				 &sys_chunk_start);
+	if (ret < 0)
+		goto out;
+	ret = reserve_free_space(free, BTRFS_CONVERT_META_GROUP_SIZE,
+				 &meta_chunk_start);
+	if (ret < 0)
+		goto out;
+
+out:
+	return ret;
+}
+
+/*
  * @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
  */
-int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
+int make_btrfs(int fd, struct btrfs_mkfs_config *cfg,
+		struct btrfs_convert_context *cctx)
 {
 	struct btrfs_super_block super;
 	struct extent_buffer *buf;
@@ -204,6 +291,8 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
 				 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
 	u64 num_bytes;
 
+	if (cctx)
+		return make_convert_btrfs(fd, cfg, cctx);
 	buf = malloc(sizeof(*buf) + max(cfg->sectorsize, cfg->nodesize));
 	if (!buf)
 		return -ENOMEM;
diff --git a/utils.h b/utils.h
index 493c2e4..bc6f931 100644
--- a/utils.h
+++ b/utils.h
@@ -43,6 +43,7 @@
 	| BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF			\
 	| BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA		\
 	| BTRFS_FEATURE_INCOMPAT_NO_HOLES)
+#define BTRFS_CONVERT_META_GROUP_SIZE (32 * 1024 * 1024)
 
 #define BTRFS_FEATURE_LIST_ALL		(1ULL << 63)
 
@@ -121,7 +122,30 @@ struct btrfs_mkfs_config {
 	u64 super_bytenr;
 };
 
-int make_btrfs(int fd, struct btrfs_mkfs_config *cfg);
+struct btrfs_convert_context {
+	u32 blocksize;
+	u32 first_data_block;
+	u32 block_count;
+	u32 inodes_count;
+	u32 free_inodes_count;
+	u64 total_bytes;
+	char *volume_name;
+	const struct btrfs_convert_operations *convert_ops;
+
+	/* The accurate used space of old filesystem */
+	struct cache_tree used;
+
+	/* Batched ranges which must be covered by data chunks */
+	struct cache_tree data_chunks;
+
+	/* Free space which is not covered by data_chunks */
+	struct cache_tree free;
+
+	void *fs_data;
+};
+
+int make_btrfs(int fd, struct btrfs_mkfs_config *cfg,
+		struct btrfs_convert_context *cctx);
 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
 			struct btrfs_root *root, u64 objectid);
 int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
-- 
2.6.2




  parent reply	other threads:[~2015-12-01  7:14 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-01  7:11 [PATCH v2 00/25] Btrfs-convert rework to support separate chunk type Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 01/25] btrfs-progs: extent-cache: Add comments for search/lookup functions Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 02/25] btrfs-progs: extent-tree: Add add_merge_cache_extent function Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 03/25] btrfs-progs: Introduce new members for btrfs_convert_context Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 04/25] btrfs-progs: convert: Introduce functions to read used space Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 05/25] btrfs-progs: convert: Introduce new function to remove reserved ranges Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 06/25] btrfs-progs: convert: Introduce function to calculate the available space Qu Wenruo
2015-12-01  7:11 ` Qu Wenruo [this message]
2015-12-01  7:11 ` [PATCH v2 08/25] btrfs-progs: Introduce function to setup temporary superblock Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 09/25] btrfs-progs: Introduce function to setup temporary tree root Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 10/25] btrfs-progs: Introduce function to setup temporary chunk root Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 11/25] btrfs-progs: Introduce function to initialize device tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 12/25] btrfs-progs: Introduce function to initialize fs tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 13/25] btrfs-progs: Introduce function to initialize csum tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 14/25] btrfs-progs: Introduce function to setup temporary extent tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 15/25] btrfs-progs: Introduce function to create convert data chunks Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 16/25] btrfs-progs: extent-tree: Introduce function to find the first overlap extent Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 17/25] btrfs-progs: extent-tree: Enhance btrfs_record_file_extent Qu Wenruo
2016-01-12 10:17   ` David Sterba
2016-01-13  0:33     ` Qu Wenruo
2016-01-13  8:55       ` David Sterba
2015-12-01  7:11 ` [PATCH v2 18/25] btrfs-progs: convert: Introduce new function to create converted image Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 19/25] btrfs-progs: convert: Introduce function to migrate reserved ranges Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 20/25] btrfs-progs: convert: Enhance record_file_blocks to handle " Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 21/25] btrfs-progs: convert: Introduce init_btrfs_v2 function Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 22/25] btrfs-progs: Introduce do_convert_v2 function Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 23/25] btrfs-progs: Convert: Add support for rollback new convert behavior Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 24/25] btrfs-progs: convert: Strictly avoid meta or system chunk allocation Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 25/25] btrfs-progs: Cleanup old btrfs-convert Qu Wenruo
2015-12-07 15:20 ` [PATCH v2 00/25] Btrfs-convert rework to support separate chunk type David Sterba
2015-12-08  1:50   ` 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=1448953905-28673-8-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.