]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: use blocksize to check if compression is making things larger
authorQu Wenruo <wqu@suse.com>
Mon, 11 Aug 2025 00:13:42 +0000 (09:43 +0930)
committerDavid Sterba <dsterba@suse.com>
Mon, 22 Sep 2025 08:54:31 +0000 (10:54 +0200)
[BEHAVIOR DIFFERENCE BETWEEN COMPRESSION ALGOS]
Currently LZO compression algorithm will check if we're making the
compressed data larger after compressing more than 2 blocks.

But zlib and zstd do the same checks after compressing more than 8192
bytes.

This is not a big deal, but since we're already supporting larger block
size (e.g. 64K block size if page size is also 64K), this check is not
suitable for all block sizes.

For example, if our page and block size are both 16KiB, and after the
first block compressed using zlib, the resulted compressed data is
slightly  larger than 16KiB, we will immediately abort the compression.

This makes zstd and zlib compression algorithms to behave slightly
different from LZO, which only aborts after compressing two blocks.

[ENHANCEMENT]
To unify the behavior, only abort the compression after compressing at
least two blocks.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/zlib.c
fs/btrfs/zstd.c

index 21af68f93a2d1fb4c0c5f57653d52f022688d195..33dc7e7b5c364c9ae827800800916c70d4276463 100644 (file)
@@ -148,6 +148,7 @@ int zlib_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
        unsigned long len = *total_out;
        unsigned long nr_dest_folios = *out_folios;
        const unsigned long max_out = nr_dest_folios * PAGE_SIZE;
+       const u32 blocksize = inode->root->fs_info->sectorsize;
        const u64 orig_end = start + len;
 
        *out_folios = 0;
@@ -234,7 +235,7 @@ int zlib_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
                }
 
                /* we're making it bigger, give up */
-               if (workspace->strm.total_in > 8192 &&
+               if (workspace->strm.total_in > blocksize * 2 &&
                    workspace->strm.total_in <
                    workspace->strm.total_out) {
                        ret = -E2BIG;
index 00159e0e921ea5fc290a75569a81992d834c9a37..d521187336a59f35a34d41cfb6c2aaa4712d71db 100644 (file)
@@ -400,6 +400,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
        unsigned long len = *total_out;
        const unsigned long nr_dest_folios = *out_folios;
        const u64 orig_end = start + len;
+       const u32 blocksize = inode->root->fs_info->sectorsize;
        unsigned long max_out = nr_dest_folios * PAGE_SIZE;
        unsigned int cur_len;
 
@@ -456,7 +457,7 @@ int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
                }
 
                /* Check to see if we are making it bigger */
-               if (tot_in + workspace->in_buf.pos > 8192 &&
+               if (tot_in + workspace->in_buf.pos > blocksize * 2 &&
                                tot_in + workspace->in_buf.pos <
                                tot_out + workspace->out_buf.pos) {
                        ret = -E2BIG;