Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 1/2] btrfs: lzo: fix space saving checks
Date: Mon, 21 Sep 2026 08:20:43 +0930	[thread overview]
Message-ID: <b3dc8e992c34424ce992a42c8fc48ba7ef27abbd.1789944508.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1789944508.git.wqu@suse.com>

[BUG]
Since commit 3be8a788eed3 ("btrfs: lzo: introduce lzo_compress_bio()
helper"), the space saving check on lzo is broken:

- Space saving check is always done based on block size
  This prevents lzo_compress_bio() to do any compression if the @len is
  smaller than sectorsize.

  This prevents proper inlined extent creation.

- Space saving check is not including the padding zeros
  If the remaining space in the output folio is not enough, we add
  padding zeros (up to 3 bytes), which contributes to the total
  compressed size.

  But that is not checked.

[CAUSE]

The function copy_compressed_data_to_bio() is doing the space saving
check wrong:

		/* With the range copied, we're larger than the original range. */
		if (((*total_out + copy_len) >> sectorsize_bits) >=
		    max_out >> sectorsize_bits)

- If the input length is smaller than sectorsize (inlined case)
  Then @max_out >> sectorsize_bits returns 0, the above check always
  returns true and copy_compressed_data_to_bio() always return -E2BIG and
  failed to create an lzo compressed inlined extent.

This check is LZO specific, as other algorithms do not have any block
aligned check during compression.
The remaining two algorithms are all relying on regular input/output
size check, and the final common space saving check at
compress_file_extent().

And for the extra padding bytes, there is no space saving check at all.

[FIX]
- Do a simpler space saving check
  By just check if the compressed size meets or exceeds the input size.
  If so reject it with -E2BIG.

  With this fixed, lzo_compress_bio() can properly handle a range that is
  smaller than a sector, which is required to get better compression ratio
  for inlined extents.

- Do space saving check before padding
  This will ensure that the compressed size never meets or exceeds the
  input size.

Fixes: 3be8a788eed3 ("btrfs: lzo: introduce lzo_compress_bio() helper")
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/lzo.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/lzo.c b/fs/btrfs/lzo.c
index 2f0996692da0..4e7837dec332 100644
--- a/fs/btrfs/lzo.c
+++ b/fs/btrfs/lzo.c
@@ -224,8 +224,7 @@ static int copy_compressed_data_to_bio(struct btrfs_fs_info *fs_info,
 		u32 foffset = *total_out & (fsize - 1);
 
 		/* With the range copied, we're larger than the original range. */
-		if (((*total_out + copy_len) >> sectorsize_bits) >=
-		    max_out >> sectorsize_bits)
+		if (*total_out + copy_len >= max_out)
 			return -E2BIG;
 
 		if (!*out_folio) {
@@ -253,6 +252,8 @@ static int copy_compressed_data_to_bio(struct btrfs_fs_info *fs_info,
 	ASSERT(*out_folio);
 
 	/* The remaining size is not enough, pad it with zeros */
+	if (*total_out + sector_bytes_left >= max_out)
+		return -E2BIG;
 	folio_zero_range(*out_folio, offset_in_folio(*out_folio, *total_out), sector_bytes_left);
 	return write_and_queue_folio(out_bio, out_folio, total_out, sector_bytes_left);
 }
-- 
2.55.0


  reply	other threads:[~2026-09-20 22:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-20 22:50 [PATCH v2 0/2] btrfs: remove the tailing zeros from compressed inline extents Qu Wenruo
2026-09-20 22:50 ` Qu Wenruo [this message]
2026-09-20 22:50 ` [PATCH v2 2/2] btrfs: remove the trailing zeros from compressed inline extent 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=b3dc8e992c34424ce992a42c8fc48ba7ef27abbd.1789944508.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