From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 2/4] btrfs-progs: mkfs/rootdir: extract compressed write path
Date: Mon, 30 Mar 2026 17:39:36 +1030 [thread overview]
Message-ID: <aa9f61869c51782ea4ae7496d1ca3c5863e3d16e.1774854459.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1774854459.git.wqu@suse.com>
Currently add_file_item_extent() have several different
optimizations/handlings:
- Hole detection
Which happens before any writes.
Thus brings the minimal impact to the remaining methods.
- Compressed write
- Reflink from source fs
- Regular read/writes
The last 3 share the same extent reservation, but with quite some extra
handling.
E.g. for compressed writes if the compression failed, we need to reset
the buffer size and fallback to regular read/writes.
This makes the code much harder to read, and the shared code is minimal,
only sharing the same btrfs_reserve_extent() and
insert_reserved_file_extent() calls.
Extract compressed write into its dedicated helper so that the fallback
logic is much easier to understand.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
mkfs/rootdir.c | 193 ++++++++++++++++++++++++++++---------------------
1 file changed, 112 insertions(+), 81 deletions(-)
diff --git a/mkfs/rootdir.c b/mkfs/rootdir.c
index f7712d75aa6f..cc80ef67683f 100644
--- a/mkfs/rootdir.c
+++ b/mkfs/rootdir.c
@@ -803,6 +803,102 @@ static int read_from_source(const struct btrfs_fs_info *fs_info,
return length;
}
+/*
+ * Return >0 for the number of bytes read from @source and submittted as
+ * compressed write.
+ * Return <0 for errors, including non-fatal ones, e.g. -E2BIG if compression
+ * ratio is bad.
+ */
+static int try_compressed_write(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_inode_item *btrfs_inode,
+ u64 objectid,
+ const struct source_descriptor *source,
+ u64 filepos, u32 length)
+{
+ struct btrfs_fs_info *fs_info = root->fs_info;
+ struct btrfs_file_extent_item stack_fi = { 0 };
+ struct btrfs_key key;
+ const u32 blocksize = fs_info->sectorsize;
+ const bool first_sector = !(btrfs_stack_inode_flags(btrfs_inode) &
+ BTRFS_INODE_COMPRESS);
+ u64 inode_flags = btrfs_stack_inode_flags(btrfs_inode);
+ u64 sb_flags = btrfs_super_incompat_flags(fs_info->super_copy);
+ u32 to_write;
+ ssize_t comp_ret;
+ int ret;
+
+ UASSERT(length > 0);
+ length = min_t(u32, length, BTRFS_MAX_COMPRESSED);
+ if (length <= root->fs_info->sectorsize)
+ return -E2BIG;
+ ret = read_from_source(root->fs_info, source->path_name, source->fd,
+ source->buf, filepos, length);
+ if (ret < 0)
+ return ret;
+ switch (g_compression) {
+ case BTRFS_COMPRESS_ZLIB:
+ comp_ret = zlib_compress_extent(first_sector, blocksize,
+ source->buf, length,
+ source->comp_buf);
+ break;
+#if COMPRESSION_LZO
+ case BTRFS_COMPRESS_LZO:
+ sb_flags |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
+ comp_ret = lzo_compress_extent(first_sector, source->buf,
+ length, source->comp_buf,
+ source->wrkmem);
+ break;
+#endif
+#if COMPRESSION_ZSTD
+ case BTRFS_COMPRESS_ZSTD:
+ sb_flags |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
+ comp_ret = zstd_compress_extent(first_sector, blocksize,
+ source->buf, length,
+ source->comp_buf);
+ break;
+#endif
+ default:
+ comp_ret = -EINVAL;
+ break;
+ }
+ if (comp_ret < 0)
+ return comp_ret;
+
+ to_write = round_up(comp_ret, blocksize);
+ memset(source->comp_buf + comp_ret, 0, to_write - comp_ret);
+ inode_flags |= BTRFS_INODE_COMPRESS;
+ btrfs_set_stack_inode_flags(btrfs_inode, inode_flags);
+ btrfs_set_super_incompat_flags(fs_info->super_copy, sb_flags);
+
+ ret = btrfs_reserve_extent(trans, root, to_write, 0, 0, (u64)-1, &key, 1);
+ if (ret < 0)
+ return ret;
+ ret = write_data_to_disk(fs_info, source->comp_buf, key.objectid, to_write);
+ if (ret < 0)
+ return ret;
+ for (unsigned int i = 0; i < to_write / blocksize; i++) {
+ ret = btrfs_csum_file_block(trans, key.objectid + (i * blocksize),
+ BTRFS_EXTENT_CSUM_OBJECTID,
+ root->fs_info->csum_type,
+ source->comp_buf + (i * blocksize));
+ if (ret)
+ return ret;
+ }
+ btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
+ btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, key.objectid);
+ btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, to_write);
+ btrfs_set_stack_file_extent_num_bytes(&stack_fi, round_up(length, blocksize));
+ btrfs_set_stack_file_extent_ram_bytes(&stack_fi, round_up(length, blocksize));
+ btrfs_set_stack_file_extent_compression(&stack_fi, g_compression);
+
+ ret = insert_reserved_file_extent(trans, root, objectid, btrfs_inode,
+ filepos, &stack_fi);
+ if (ret < 0)
+ return ret;
+ return length;
+}
+
static int add_file_item_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_inode_item *btrfs_inode,
@@ -819,7 +915,6 @@ static int add_file_item_extent(struct btrfs_trans_handle *trans,
char *write_buf;
bool do_comp = g_compression != BTRFS_COMPRESS_NONE;
bool datasum = true;
- ssize_t comp_ret;
u64 flags = btrfs_stack_inode_flags(btrfs_inode);
off64_t next;
@@ -861,92 +956,28 @@ static int add_file_item_extent(struct btrfs_trans_handle *trans,
if (next == (off64_t)-1 || !IS_ALIGNED(next, sectorsize) || next > source->size)
next = source->size;
- buf_size = do_comp ? BTRFS_MAX_COMPRESSED : MAX_EXTENT_SIZE;
+ if (do_comp && next - file_pos > sectorsize) {
+ ret = try_compressed_write(trans, root, btrfs_inode, objectid,
+ source, file_pos, next - file_pos);
+ if (ret > 0)
+ return ret;
+ if (ret < 0 && ret != -E2BIG)
+ return ret;
+ flags |= BTRFS_INODE_NOCOMPRESS;
+ btrfs_set_stack_inode_flags(btrfs_inode, flags);
+ /* Fallback to other methods. */
+ }
+
+ buf_size = MAX_EXTENT_SIZE;
to_read = min_t(u64, file_pos + buf_size, next) - file_pos;
ret = read_from_source(root->fs_info, source->path_name, source->fd,
source->buf, file_pos, to_read);
if (ret < 0)
return ret;
- if (to_read <= sectorsize)
- do_comp = false;
- if (do_comp) {
- bool first_sector = !(flags & BTRFS_INODE_COMPRESS);
-
- switch (g_compression) {
- case BTRFS_COMPRESS_ZLIB:
- comp_ret = zlib_compress_extent(first_sector, sectorsize,
- source->buf, to_read,
- source->comp_buf);
- break;
-#if COMPRESSION_LZO
- case BTRFS_COMPRESS_LZO:
- comp_ret = lzo_compress_extent(sectorsize, source->buf,
- to_read, source->comp_buf,
- source->wrkmem);
- break;
-#endif
-#if COMPRESSION_ZSTD
- case BTRFS_COMPRESS_ZSTD:
- comp_ret = zstd_compress_extent(first_sector, sectorsize,
- source->buf, to_read,
- source->comp_buf);
- break;
-#endif
- default:
- comp_ret = -EINVAL;
- break;
- }
-
- /*
- * If the function returned -E2BIG, the extent is incompressible.
- * If this is the first sector, add the nocompress flag,
- * increase the buffer size, and read the rest of the extent.
- */
- if (comp_ret == -E2BIG)
- do_comp = false;
- else if (comp_ret < 0)
- return comp_ret;
-
- if (comp_ret == -E2BIG && first_sector) {
- flags |= BTRFS_INODE_NOCOMPRESS;
- btrfs_set_stack_inode_flags(btrfs_inode, flags);
-
- buf_size = MAX_EXTENT_SIZE;
- to_read = min_t(u64, file_pos + buf_size, next) - file_pos;
- ret = read_from_source(root->fs_info, source->path_name, source->fd,
- source->buf, file_pos, to_read);
- if (ret < 0)
- return ret;
- }
- }
-
- if (do_comp) {
- u64 features;
-
- to_write = round_up(comp_ret, sectorsize);
- write_buf = source->comp_buf;
- memset(write_buf + comp_ret, 0, to_write - comp_ret);
-
- flags |= BTRFS_INODE_COMPRESS;
- btrfs_set_stack_inode_flags(btrfs_inode, flags);
-
- if (g_compression == BTRFS_COMPRESS_ZSTD) {
- features = btrfs_super_incompat_flags(trans->fs_info->super_copy);
- features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
- btrfs_set_super_incompat_flags(trans->fs_info->super_copy,
- features);
- } else if (g_compression == BTRFS_COMPRESS_LZO) {
- features = btrfs_super_incompat_flags(trans->fs_info->super_copy);
- features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
- btrfs_set_super_incompat_flags(trans->fs_info->super_copy,
- features);
- }
- } else {
- to_write = round_up(to_read, sectorsize);
- write_buf = source->buf;
- memset(write_buf + to_read, 0, to_write - to_read);
- }
+ to_write = round_up(to_read, sectorsize);
+ write_buf = source->buf;
+ memset(write_buf + to_read, 0, to_write - to_read);
ret = btrfs_reserve_extent(trans, root, to_write, 0, 0,
(u64)-1, &key, 1);
--
2.53.0
next prev parent reply other threads:[~2026-03-30 7:10 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-30 7:09 [PATCH 0/4] btrfs-progs: mkfs/rootdir: cleanup and new fiemap based prealloc detection Qu Wenruo
2026-03-30 7:09 ` [PATCH 1/4] btrfs-progs: mkfs/rootdir: use a helper to read from source fs Qu Wenruo
2026-03-30 7:09 ` Qu Wenruo [this message]
2026-03-30 7:09 ` [PATCH 3/4] btrfs-progs: mkfs/rootdir: use fiemap to do prealloc detection Qu Wenruo
2026-03-30 7:09 ` [PATCH 4/4] btrfs-progs: mkfs-tests: add a new test case for fiemap based detection 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=aa9f61869c51782ea4ae7496d1ca3c5863e3d16e.1774854459.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