From: Josef Bacik <josef@toxicpanda.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH 12/15] btrfs-progs: mkfs: don't populate extent tree with extent tree v2
Date: Wed, 15 Dec 2021 15:14:50 -0500 [thread overview]
Message-ID: <6f989e8fa1c898707865feac6c3bee61376588b3.1639598612.git.josef@toxicpanda.com> (raw)
In-Reply-To: <cover.1639598612.git.josef@toxicpanda.com>
If we have extent tree v2 set then we don't track metadata in the extent
tree, and we can skip creating these items at mkfs time.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
mkfs/common.c | 207 +++++++++++++++++++++++++++-----------------------
1 file changed, 113 insertions(+), 94 deletions(-)
diff --git a/mkfs/common.c b/mkfs/common.c
index a4412a1f..e4435aaa 100644
--- a/mkfs/common.c
+++ b/mkfs/common.c
@@ -233,6 +233,109 @@ static int create_block_group_tree(int fd, struct btrfs_mkfs_config *cfg,
return 0;
}
+static int fill_extent_tree(int fd, struct btrfs_mkfs_config *cfg,
+ struct extent_buffer *buf, u64 bg_offset,
+ u64 bg_size, u64 bg_used,
+ const enum btrfs_mkfs_block *blocks, int blocks_nr)
+{
+ struct btrfs_extent_item *extent_item;
+ struct btrfs_disk_key disk_key;
+ u64 first_free, ref_root;
+ u32 nritems = 0, itemoff, item_size;
+ int i, ret, blk;
+ bool add_block_group = true;
+ bool skinny_metadata = !!(cfg->features &
+ BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
+
+ first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
+ first_free &= ~((u64)cfg->sectorsize - 1);
+
+ /* create the items for the extent tree */
+ memset(buf->data + sizeof(struct btrfs_header), 0,
+ cfg->nodesize - sizeof(struct btrfs_header));
+ itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize);
+ for (i = 0; i < blocks_nr; i++) {
+ blk = blocks[i];
+
+ /* Add the block group item for our temporary chunk. */
+ if (cfg->blocks[blk] > bg_offset && add_block_group) {
+ itemoff -= sizeof(struct btrfs_block_group_item);
+ write_block_group_item(buf, nritems, bg_offset, bg_size,
+ bg_used,
+ BTRFS_FIRST_CHUNK_TREE_OBJECTID,
+ itemoff);
+ add_block_group = false;
+ nritems++;
+ }
+
+ item_size = sizeof(struct btrfs_extent_item);
+ if (!skinny_metadata)
+ item_size += sizeof(struct btrfs_tree_block_info);
+
+ if (cfg->blocks[blk] < first_free) {
+ error("block[%d] below first free: %llu < %llu",
+ i, (unsigned long long)cfg->blocks[blk],
+ (unsigned long long)first_free);
+ return -EINVAL;
+ }
+ if (i > 0 && cfg->blocks[blk] < cfg->blocks[blocks[i - 1]]) {
+ error("blocks %d and %d in reverse order: %llu < %llu",
+ blk, blocks[i - 1],
+ (unsigned long long)cfg->blocks[blk],
+ (unsigned long long)cfg->blocks[blocks[i - 1]]);
+ return -EINVAL;
+ }
+
+ /* create extent item */
+ itemoff -= item_size;
+ btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[blk]);
+ if (skinny_metadata) {
+ btrfs_set_disk_key_type(&disk_key,
+ BTRFS_METADATA_ITEM_KEY);
+ btrfs_set_disk_key_offset(&disk_key, 0);
+ } else {
+ btrfs_set_disk_key_type(&disk_key,
+ BTRFS_EXTENT_ITEM_KEY);
+ btrfs_set_disk_key_offset(&disk_key, cfg->nodesize);
+ }
+ 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),
+ item_size);
+ extent_item = btrfs_item_ptr(buf, nritems,
+ struct btrfs_extent_item);
+ btrfs_set_extent_refs(buf, extent_item, 1);
+ btrfs_set_extent_generation(buf, extent_item, 1);
+ btrfs_set_extent_flags(buf, extent_item,
+ BTRFS_EXTENT_FLAG_TREE_BLOCK);
+ nritems++;
+
+ /* create extent ref */
+ ref_root = reference_root_table[blk];
+ btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[blk]);
+ btrfs_set_disk_key_offset(&disk_key, ref_root);
+ btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
+ 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), 0);
+ nritems++;
+ }
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]);
+ btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
+ btrfs_set_header_nritems(buf, nritems);
+ csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
+ cfg->csum_type);
+ ret = btrfs_pwrite(fd, buf->data, cfg->nodesize,
+ cfg->blocks[MKFS_EXTENT_TREE], cfg->zone_size);
+ if (ret != cfg->nodesize)
+ ret = (ret < 0 ? -errno : -EIO);
+ else
+ ret = 0;
+ return ret;
+}
+
/*
* @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
*
@@ -259,7 +362,6 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
struct btrfs_super_block super;
struct extent_buffer *buf;
struct btrfs_disk_key disk_key;
- struct btrfs_extent_item *extent_item;
struct btrfs_chunk *chunk;
struct btrfs_dev_item *dev_item;
struct btrfs_dev_extent *dev_extent;
@@ -272,17 +374,12 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
int blk;
u32 itemoff;
u32 nritems = 0;
- u64 first_free;
- u64 ref_root;
u32 array_size;
u32 item_size;
u64 total_used = 0;
- int skinny_metadata = !!(cfg->features &
- BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
u64 num_bytes;
u64 system_group_offset = BTRFS_BLOCK_RESERVED_1M_FOR_SUPER;
u64 system_group_size = BTRFS_MKFS_SYSTEM_GROUP_SIZE;
- bool add_block_group = true;
bool free_space_tree = !!(cfg->runtime_features &
BTRFS_RUNTIME_FEATURE_FREE_SPACE_TREE);
bool extent_tree_v2 = !!(cfg->features &
@@ -295,7 +392,6 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
if (extent_tree_v2) {
blocks = extent_tree_v2_blocks;
blocks_nr = ARRAY_SIZE(extent_tree_v2_blocks);
- add_block_group = false;
}
if ((cfg->features & BTRFS_FEATURE_INCOMPAT_ZONED)) {
@@ -307,9 +403,6 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
if (!buf)
return -ENOMEM;
- first_free = BTRFS_SUPER_INFO_OFFSET + cfg->sectorsize * 2 - 1;
- first_free &= ~((u64)cfg->sectorsize - 1);
-
memset(&super, 0, sizeof(super));
num_bytes = (cfg->num_bytes / cfg->sectorsize) * cfg->sectorsize;
@@ -390,92 +483,18 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
if (ret < 0)
goto out;
- /* create the items for the extent tree */
- memset(buf->data + sizeof(struct btrfs_header), 0,
- cfg->nodesize - sizeof(struct btrfs_header));
- nritems = 0;
- itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize);
- for (i = 0; i < blocks_nr; i++) {
- blk = blocks[i];
-
- /* Add the block group item for our temporary chunk. */
- if (cfg->blocks[blk] > system_group_offset && add_block_group) {
- itemoff -= sizeof(struct btrfs_block_group_item);
- write_block_group_item(buf, nritems,
- system_group_offset,
- system_group_size, total_used,
- BTRFS_FIRST_CHUNK_TREE_OBJECTID,
- itemoff);
- add_block_group = false;
- nritems++;
- }
-
- item_size = sizeof(struct btrfs_extent_item);
- if (!skinny_metadata)
- item_size += sizeof(struct btrfs_tree_block_info);
-
- if (cfg->blocks[blk] < first_free) {
- error("block[%d] below first free: %llu < %llu",
- i, (unsigned long long)cfg->blocks[blk],
- (unsigned long long)first_free);
- ret = -EINVAL;
+ if (extent_tree_v2) {
+ ret = btrfs_write_empty_tree(fd, cfg, buf,
+ BTRFS_EXTENT_TREE_OBJECTID,
+ cfg->blocks[MKFS_EXTENT_TREE]);
+ if (ret)
goto out;
- }
- if (i > 0 && cfg->blocks[blk] < cfg->blocks[blocks[i - 1]]) {
- error("blocks %d and %d in reverse order: %llu < %llu",
- blk, blocks[i - 1],
- (unsigned long long)cfg->blocks[blk],
- (unsigned long long)cfg->blocks[blocks[i - 1]]);
- ret = -EINVAL;
+ } else {
+ ret = fill_extent_tree(fd, cfg, buf, system_group_offset,
+ system_group_size, total_used, blocks,
+ blocks_nr);
+ if (ret)
goto out;
- }
-
- /* create extent item */
- itemoff -= item_size;
- btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[blk]);
- if (skinny_metadata) {
- btrfs_set_disk_key_type(&disk_key,
- BTRFS_METADATA_ITEM_KEY);
- btrfs_set_disk_key_offset(&disk_key, 0);
- } else {
- btrfs_set_disk_key_type(&disk_key,
- BTRFS_EXTENT_ITEM_KEY);
- btrfs_set_disk_key_offset(&disk_key, cfg->nodesize);
- }
- 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),
- item_size);
- extent_item = btrfs_item_ptr(buf, nritems,
- struct btrfs_extent_item);
- btrfs_set_extent_refs(buf, extent_item, 1);
- btrfs_set_extent_generation(buf, extent_item, 1);
- btrfs_set_extent_flags(buf, extent_item,
- BTRFS_EXTENT_FLAG_TREE_BLOCK);
- nritems++;
-
- /* create extent ref */
- ref_root = reference_root_table[blk];
- btrfs_set_disk_key_objectid(&disk_key, cfg->blocks[blk]);
- btrfs_set_disk_key_offset(&disk_key, ref_root);
- btrfs_set_disk_key_type(&disk_key, BTRFS_TREE_BLOCK_REF_KEY);
- 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), 0);
- nritems++;
- }
- btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]);
- btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
- btrfs_set_header_nritems(buf, nritems);
- csum_tree_block_size(buf, btrfs_csum_type_size(cfg->csum_type), 0,
- cfg->csum_type);
- ret = btrfs_pwrite(fd, buf->data, cfg->nodesize,
- cfg->blocks[MKFS_EXTENT_TREE], cfg->zone_size);
- if (ret != cfg->nodesize) {
- ret = (ret < 0 ? -errno : -EIO);
- goto out;
}
/* create the chunk tree */
--
2.26.3
next prev parent reply other threads:[~2021-12-15 20:15 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-15 20:14 [PATCH 00/15] btrfs-progs: extent tree v2 gc tree and metadata ref changes Josef Bacik
2021-12-15 20:14 ` [PATCH 01/15] btrfs-progs: extract out free extent accounting handling Josef Bacik
2021-12-15 20:14 ` [PATCH 02/15] btrfs-progs: check: skip owner ref check for extent tree v2 Josef Bacik
2021-12-15 20:14 ` [PATCH 03/15] btrfs-progs: check: skip extent backref for metadata in " Josef Bacik
2021-12-15 20:14 ` [PATCH 04/15] btrfs-progs: check: calculate normal flags for extent tree v2 metadata Josef Bacik
2021-12-15 20:14 ` [PATCH 05/15] btrfs-progs: check: make metadata ref counting extent tree v2 aware Josef Bacik
2021-12-15 20:14 ` [PATCH 06/15] btrfs-progs: check: update block group used properly for extent tree v2 Josef Bacik
2021-12-15 20:14 ` [PATCH 07/15] btrfs-progs: do not insert extent items for metadata " Josef Bacik
2021-12-15 20:14 ` [PATCH 08/15] btrfs-progs: do not remove metadata backrefs " Josef Bacik
2021-12-15 20:14 ` [PATCH 09/15] btrfs-progs: repair: traverse tree blocks " Josef Bacik
2021-12-15 20:14 ` [PATCH 10/15] btrfs-progs: cache the block group with the free space tree if possible Josef Bacik
2021-12-15 20:14 ` [PATCH 11/15] btrfs-progs: make btrfs_lookup_extent_info extent tree v2 aware Josef Bacik
2021-12-15 20:14 ` Josef Bacik [this message]
2021-12-15 20:14 ` [PATCH 13/15] btrfs-progs: add on-disk items and read support for the gc tree Josef Bacik
2021-12-15 20:14 ` [PATCH 14/15] btrfs-progs: mkfs: create the gc tree at mkfs time Josef Bacik
2021-12-15 20:14 ` [PATCH 15/15] btrfs-progs: deal with GC items in check Josef Bacik
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=6f989e8fa1c898707865feac6c3bee61376588b3.1639598612.git.josef@toxicpanda.com \
--to=josef@toxicpanda.com \
--cc=kernel-team@fb.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).