btrfs-progs: mkfs: dynamically modify mkfs blocks array

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>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2022-08-09 14:03:51 +08:00 committed by David Sterba
parent c7d5638824
commit cc4a249e99
2 changed files with 68 additions and 24 deletions

View File

@ -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,20 @@ 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;

View File

@ -52,25 +52,12 @@ enum btrfs_mkfs_block {
MKFS_CSUM_TREE,
MKFS_FREE_SPACE_TREE,
MKFS_BLOCK_GROUP_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_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,
};
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 {