From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 1/4] btrfs-progs: mkfs: dynamically modify mkfs blocks array
Date: Fri, 29 Jul 2022 09:40:12 +0800 [thread overview]
Message-ID: <febf59c659d2fd8922d6e6a16c6ac63e34a0d850.1659058423.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1659058423.git.wqu@suse.com>
In mkfs_btrfs(), we have a btrfs_mkfs_block array to store how many tree
blocks we need to reserve for the initial btrfs image.
Currently we have two very similar arrays, extent_tree_v1_blocks and
extent_tree_v2_blocks.
The only difference is just v2 has an extra block for block group tree.
This patch will add two helpers, mkfs_blocks_add() and
mkfs_blocks_remove() to properly add/remove one block dynamically from
the array.
This allows 3 things:
- Merge extent_tree_v1_blocks and extent_tree_v2_blocks into one array
The new array will be the same as extent_tree_v1_blocks.
For extent-tree-v2, we just dynamically add MKFS_BLOCK_GROUP_TREE.
- Remove free space tree block on-demand
This only works for extent-tree-v1 case, as v2 has a hard requirement
on free space tree.
But this still make code much cleaner, not doing any special hacks.
- Allow future expansion without introduce new array
I strongly doubt why this is not properly done in extent-tree-v2
preparation patches.
We should not allow bad practice to sneak in just because it's some
preparation patches for a larger feature.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
mkfs/common.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++-----
mkfs/common.h | 20 +++-----------
2 files changed, 69 insertions(+), 24 deletions(-)
diff --git a/mkfs/common.c b/mkfs/common.c
index 218854491c14..d5a49ca11cde 100644
--- a/mkfs/common.c
+++ b/mkfs/common.c
@@ -260,6 +260,60 @@ next:
__builtin_unreachable();
}
+/*
+ * Add @block into the @blocks array.
+ *
+ * The @blocks should already be in ascending order and no duplicate.
+ */
+static void mkfs_blocks_add(enum btrfs_mkfs_block *blocks, int *blocks_nr,
+ enum btrfs_mkfs_block to_add)
+{
+ int i;
+
+ for (i = 0; i < *blocks_nr; i++) {
+ /* The target is already in the array. */
+ if (blocks[i] == to_add)
+ return;
+
+ /*
+ * We find the first one past @to_add, move the array one slot
+ * right, insert a new one.
+ */
+ if (blocks[i] > to_add) {
+ memmove(blocks + i + 1, blocks + i, *blocks_nr - i);
+ blocks[i] = to_add;
+ (*blocks_nr)++;
+ return;
+ }
+ /* Current one still smaller than @to_add, go to next slot. */
+ }
+ /* All slots iterated and not match, insert into the last slot. */
+ blocks[i] = to_add;
+ (*blocks_nr)++;
+ return;
+}
+
+/*
+ * Remove @block from the @blocks array.
+ *
+ * The @blocks should already be in ascending order and no duplicate.
+ */
+static void mkfs_blocks_remove(enum btrfs_mkfs_block *blocks, int *blocks_nr,
+ enum btrfs_mkfs_block to_remove)
+{
+ int i;
+
+ for (i = 0; i < *blocks_nr; i++) {
+ /* Found the target, move the array one slot left. */
+ if (blocks[i] == to_remove) {
+ memmove(blocks + i, blocks + i + 1, *blocks_nr - i - 1);
+ (*blocks_nr)--;
+ }
+ }
+ /* Nothing found, exit directly. */
+ return;
+}
+
/*
* @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
*
@@ -290,12 +344,12 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
struct btrfs_chunk *chunk;
struct btrfs_dev_item *dev_item;
struct btrfs_dev_extent *dev_extent;
- const enum btrfs_mkfs_block *blocks = extent_tree_v1_blocks;
+ enum btrfs_mkfs_block blocks[MKFS_BLOCK_COUNT];
u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
u8 *ptr;
int i;
int ret;
- int blocks_nr = ARRAY_SIZE(extent_tree_v1_blocks);
+ int blocks_nr;
int blk;
u32 itemoff;
u32 nritems = 0;
@@ -315,16 +369,21 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
bool extent_tree_v2 = !!(cfg->features &
BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2);
- /* Don't include the free space tree in the blocks to process. */
- if (!free_space_tree)
- blocks_nr--;
+ memcpy(blocks, default_blocks,
+ sizeof(enum btrfs_mkfs_block) * ARRAY_SIZE(default_blocks));
+ blocks_nr = ARRAY_SIZE(default_blocks);
+ /* Extent tree v2 needs an extra block for block group tree.*/
if (extent_tree_v2) {
- blocks = extent_tree_v2_blocks;
- blocks_nr = ARRAY_SIZE(extent_tree_v2_blocks);
+ mkfs_blocks_add(blocks, &blocks_nr, MKFS_BLOCK_GROUP_TREE);
add_block_group = false;
}
+ /* Don't include the free space tree in the blocks to process. */
+ if (!free_space_tree)
+ mkfs_blocks_remove(blocks, &blocks_nr, MKFS_FREE_SPACE_TREE);
+
+
if ((cfg->features & BTRFS_FEATURE_INCOMPAT_ZONED)) {
system_group_offset = zoned_system_group_offset(cfg->zone_size);
system_group_size = cfg->zone_size;
diff --git a/mkfs/common.h b/mkfs/common.h
index 3533e114e81c..47b14cdae2f3 100644
--- a/mkfs/common.h
+++ b/mkfs/common.h
@@ -52,25 +52,12 @@ enum btrfs_mkfs_block {
MKFS_CSUM_TREE,
MKFS_FREE_SPACE_TREE,
MKFS_BLOCK_GROUP_TREE,
- MKFS_BLOCK_COUNT
-};
-
-static const enum btrfs_mkfs_block extent_tree_v1_blocks[] = {
- MKFS_ROOT_TREE,
- MKFS_EXTENT_TREE,
- MKFS_CHUNK_TREE,
- MKFS_DEV_TREE,
- MKFS_FS_TREE,
- MKFS_CSUM_TREE,
- /*
- * Since the free space tree is optional with v1 it must always be last
- * in this array.
- */
- MKFS_FREE_SPACE_TREE,
+ /* MKFS_BLOCK_COUNT should be the max blocks we can have at mkfs time. */
+ MKFS_BLOCK_COUNT
};
-static const enum btrfs_mkfs_block extent_tree_v2_blocks[] = {
+static const enum btrfs_mkfs_block default_blocks[] = {
MKFS_ROOT_TREE,
MKFS_EXTENT_TREE,
MKFS_CHUNK_TREE,
@@ -78,7 +65,6 @@ static const enum btrfs_mkfs_block extent_tree_v2_blocks[] = {
MKFS_FS_TREE,
MKFS_CSUM_TREE,
MKFS_FREE_SPACE_TREE,
- MKFS_BLOCK_GROUP_TREE,
};
struct btrfs_mkfs_config {
--
2.37.0
next prev parent reply other threads:[~2022-07-29 1:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-29 1:40 [PATCH v2 0/4] btrfs-progs: separate BLOCK_GROUP_TREE feature from extent-tree-v2 Qu Wenruo
2022-07-29 1:40 ` Qu Wenruo [this message]
2022-07-29 1:40 ` [PATCH v2 2/4] btrfs-progs: don't save block group root into super block Qu Wenruo
2022-07-29 1:40 ` [PATCH v2 3/4] btrfs-progs: separate block group tree from extent tree v2 Qu Wenruo
2022-07-29 1:40 ` [PATCH v2 4/4] btrfs-progs: btrfstune: add the ability to convert to block group tree feature 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=febf59c659d2fd8922d6e6a16c6ac63e34a0d850.1659058423.git.wqu@suse.com \
--to=wqu@suse.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).