]> git.hungrycats.org Git - linux/commit
btrfs: fix off-by-one end related to inode_need_compress()
authorQu Wenruo <wqu@suse.com>
Thu, 10 Sep 2026 02:05:52 +0000 (11:35 +0930)
committerDavid Sterba <dsterba@suse.com>
Wed, 16 Sep 2026 13:26:16 +0000 (15:26 +0200)
commit9dc38f249a02e99124058caf6d4926fa0e0032fe
treeb49ded5cb89ce70932b31393d9c4cb49d2e67118
parent448c6f99ce61d5dc258823e9b834dbfeafddd7c5
btrfs: fix off-by-one end related to inode_need_compress()

In most cases btrfs uses @end as the inclusive end bytenr for a range,
and this applies to inode_need_compress().

However we have several sites not following the inclusive bytenr:

- run_delalloc_inline()
  Which assigned @blocksize as @end for inode_need_compress()

  This makes inode_need_compress() always skip the disk_i_size check.

- heuristic_collect_sample()
  Which assigned "start + BTRFS_MAX_UNCOMPRESSED" to @end, which is
  the exclusive bytenr.

Neither is really causing any real problem, as
heuristic_collect_sample() has proper checks to avoid reading anything
beyond @end, and the sampling read size is 16 bytes, so it has enough
headroom to handle that off-by-one problem.

But still I do not like anything out of the common scheme, so fix the
off-by-one @end for both call sites, and add extra ASSERT()s to catch
such unaligned parameters.

Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/compression.c
fs/btrfs/inode.c