]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: do not mark inode incompressible after inline attempt fails
authorQu Wenruo <wqu@suse.com>
Mon, 16 Feb 2026 02:49:38 +0000 (13:19 +1030)
committerDavid Sterba <dsterba@suse.com>
Tue, 7 Apr 2026 16:55:57 +0000 (18:55 +0200)
[BUG]
The following sequence will set the file with nocompress flag:

  # mkfs.btrfs -f $dev
  # mount $dev $mnt -o max_inline=4,compress
  # xfs_io -f -c "pwrite 0 2k" -c sync $mnt/foobar

The inode will have NOCOMPRESS flag, even if the content itself (all 0xcd)
can still be compressed very well:

item 4 key (257 INODE_ITEM 0) itemoff 15879 itemsize 160
generation 9 transid 10 size 2097152 nbytes 1052672
block group 0 mode 100600 links 1 uid 0 gid 0 rdev 0
sequence 257 flags 0x8(NOCOMPRESS)

Please note that, this behavior is there even before commit 59615e2c1f63
("btrfs: reject single block sized compression early").

[CAUSE]
At compress_file_range(), after btrfs_compress_folios() call, we try
making an inlined extent by calling cow_file_range_inline().

But cow_file_range_inline() calls can_cow_file_range_inline() which has
more accurate checks on if the range can be inlined.

One of the user configurable conditions is the "max_inline=" mount
option. If that value is set low (like the example, 4 bytes, which
cannot store any header), or the compressed content is just slightly
larger than 2K (the default value, meaning a 50% compression ratio),
cow_file_range_inline() will return 1 immediately.

And since we're here only to try inline the compressed data, the range
is no larger than a single fs block.

Thus compression is never going to make it a win, we fall back to
marking the inode incompressible unavoidably.

[FIX]
Just add an extra check after inline attempt, so that if the inline
attempt failed, do not set the nocompress flag.

As there is no way to remove that flag, and the default 50% compression
ratio is way too strict for the whole inode.

CC: stable@vger.kernel.org # 6.12+
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/inode.c

index 6daa7bb027fc98107a629d60b0a20a15597caccd..2ae5a9b2f9511bf70786ca497f3d8c7d0d9eca42 100644 (file)
@@ -1061,6 +1061,12 @@ again:
                        mapping_set_error(mapping, -EIO);
                return;
        }
+       /*
+        * If a single block at file offset 0 cannot be inlined, fall back to
+        * regular writes without marking the file incompressible.
+        */
+       if (start == 0 && end <= blocksize)
+               goto cleanup_and_bail_uncompressed;
 
        /*
         * We aren't doing an inline extent. Round the compressed size up to a