Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc
@ 2026-06-23 11:37 Qu Wenruo
  2026-06-23 11:37 ` [PATCH v3 1/2] btrfs: do not try compression for data reloc inodes Qu Wenruo
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-06-23 11:37 UTC (permalink / raw)
  To: linux-btrfs

[CHANGELOG]
v3:
- Use btrfs_is_data_reloc_root()

- Reword the cause analyze to avoid confusion

- Expand the [FIX] section to explain why disabling compression will
  avoid inlined extents

v2:
- Add more explanation on why we can created inlined extents even if
  relocation is preallocating space

- Add the fixes: tag since the behavior is caused by a specific commit

There is a syzbot report that an inlined file extent item in a data
reloc inode triggered a sanity check in get_new_location().

It turns out that we can create inlined file extents for data reloc
inodes in the first place after commit 3eaf5f082c4c ("btrfs: extract
inlined creation into a dedicated delalloc helper").

So the first patch will avoid compression for data reloc inodes first,
then the second patch to reject inlined file extent items in
get_new_location(), making the checks more robust.

Qu Wenruo (2):
  btrfs: do not try compression for data reloc inodes
  btrfs: reject inline file extents item in get_new_location()

 fs/btrfs/btrfs_inode.h | 2 ++
 fs/btrfs/relocation.c  | 7 +++++++
 2 files changed, 9 insertions(+)

-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/2] btrfs: do not try compression for data reloc inodes
  2026-06-23 11:37 [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Qu Wenruo
@ 2026-06-23 11:37 ` Qu Wenruo
  2026-06-23 11:37 ` [PATCH v3 2/2] btrfs: reject inline file extents item in get_new_location() Qu Wenruo
  2026-06-23 12:16 ` [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Filipe Manana
  2 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-06-23 11:37 UTC (permalink / raw)
  To: linux-btrfs; +Cc: syzbot+d950c6ba09b79f6e1864, stable

[BUG]
There is a syzbot report that the check inside get_new_location()
triggered:

 BTRFS info (device loop0): found 31 extents, stage: move data extents
 BTRFS info (device loop0): leaf 8908800 gen 16 total ptrs 28 free space 1676 owner 18446744073709551607
        item 0 key (256 INODE_ITEM 0) itemoff 3835 itemsize 160
                inode generation 5 transid 0 size 0 nbytes 0
                block group 0 mode 40755 links 1 uid 0 gid 0
                rdev 0 sequence 0 flags 0x0
                atime 1669132761.0
                ctime 1669132761.0
                mtime 1669132761.0
                otime 0.0
        item 1 key (256 INODE_REF 256) itemoff 3823 itemsize 12
                index 0 name_len 2
        item 2 key (258 INODE_ITEM 0) itemoff 3663 itemsize 160
                inode generation 1 transid 16 size 733184 nbytes 106496
                block group 0 mode 100600 links 0 uid 0 gid 0
                rdev 0 sequence 24 flags 0x18
        item 3 key (258 EXTENT_DATA 0) itemoff 3595 itemsize 68
                generation 16 type 0
                inline extent data size 47 ram_bytes 4096 compression 1
 [...]
        item 27 key (18446744073709551611 ORPHAN_ITEM 258) itemoff 2376 itemsize 0
 BTRFS error (device loop0): unexpected non-zero offset in file extent item for data reloc inode 258 key offset 0 offset 9277520992061368337
 ------------[ cut here ]------------
 btrfs_abort_should_print_stack(__error)

[CAUSE]
The above dump tree shows the first file extent item is inlined, which
should make no sense for data reloc inodes, as such inodes just
represent where the data extents are in the relocation destination chunk.

However the relocation path preallocates space for each block,
then dirties them, cluster by cluster.
It's possible to have a single block at the beginning of the block
group, and no other block in the same cluster.

So relocation will preallocate a file extent for that block and dirty
the first block.
Then memory pressure forces the data reloc inode to be written back, before
any other blocks are dirtied/allocated.

Finally commit 3eaf5f082c4c ("btrfs: extract inlined creation into a dedicated
delalloc helper") changed the sequence of delalloc. Before that commit we
always tried NOCOW first, so that dirtied block will be written back into
the preallocated space, and appear as a regular extent.

But with that commit, we always try inline first, and since compression
is forced, we try compressing the first block, and then inline the
compressed data, resulting in the above inlined file extent in the data
reloc tree.

Then the check in get_new_location() will check the file offset, without
checking if the file extent is inlined or not, resulting in the above
failure.

[FIX]
Do not allow compression for data reloc inodes.

Since data reloc inode sizes are always block aligned, as long as we do
not compress, @data_len will always be at least one block, and
that will cause can_cow_file_range_inline() to return false, thus no
inlined extent will be created.

Reported-by: syzbot+d950c6ba09b79f6e1864@syzkaller.appspotmail.com
Link: https://lore.kernel.org/linux-btrfs/6a373dc5.764cf64f.168fbe.0001.GAE@google.com/
Fixes: 3eaf5f082c4c ("btrfs: extract inlined creation into a dedicated delalloc helper")
Cc: stable@vger.kernel.org
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/btrfs_inode.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index d5d81f9546c3..7fdc6c3fd066 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -476,6 +476,8 @@ static inline bool btrfs_inode_can_compress(const struct btrfs_inode *inode)
 	if (inode->flags & BTRFS_INODE_NODATACOW ||
 	    inode->flags & BTRFS_INODE_NODATASUM)
 		return false;
+	if (btrfs_is_data_reloc_root(inode->root))
+		return false;
 	return true;
 }
 
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/2] btrfs: reject inline file extents item in get_new_location()
  2026-06-23 11:37 [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Qu Wenruo
  2026-06-23 11:37 ` [PATCH v3 1/2] btrfs: do not try compression for data reloc inodes Qu Wenruo
@ 2026-06-23 11:37 ` Qu Wenruo
  2026-06-23 12:16 ` [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Filipe Manana
  2 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-06-23 11:37 UTC (permalink / raw)
  To: linux-btrfs; +Cc: stable

Commit a6908f88c9da ("btrfs: validate data reloc tree file extent item
members") introduced extra checks on file extent items for data reloc
inodes, but it checks the file extent offset without checking if the file
extent is inlined.

This can lead to either false alerts (as the offset member is inside the
inlined data) or even reading beyond the item range.

This has already triggered a warning in a syzbot report.
Although the root fix is to avoid compression for data reloc inodes, for
the sake of consistency, reject inlined file extents first.

Fixes: a6908f88c9da ("btrfs: validate data reloc tree file extent item members")
Cc: stable@vger.kernel.org
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/relocation.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index e1dd9939f4f1..f5fac0e34494 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -893,6 +893,13 @@ static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
 	leaf = path->nodes[0];
 	fi = btrfs_item_ptr(leaf, path->slots[0],
 			    struct btrfs_file_extent_item);
+	if (unlikely(btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE)) {
+		btrfs_print_leaf(leaf);
+		btrfs_err(fs_info,
+	"unexpected inline file extent item for data reloc inode %llu key offset %llu",
+			  btrfs_ino(BTRFS_I(reloc_inode)), bytenr);
+		return -EUCLEAN;
+	}
 
 	/*
 	 * The cluster-boundary key searched above is always written by
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc
  2026-06-23 11:37 [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Qu Wenruo
  2026-06-23 11:37 ` [PATCH v3 1/2] btrfs: do not try compression for data reloc inodes Qu Wenruo
  2026-06-23 11:37 ` [PATCH v3 2/2] btrfs: reject inline file extents item in get_new_location() Qu Wenruo
@ 2026-06-23 12:16 ` Filipe Manana
  2 siblings, 0 replies; 4+ messages in thread
From: Filipe Manana @ 2026-06-23 12:16 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Tue, Jun 23, 2026 at 12:38 PM Qu Wenruo <wqu@suse.com> wrote:
>
> [CHANGELOG]
> v3:
> - Use btrfs_is_data_reloc_root()
>
> - Reword the cause analyze to avoid confusion
>
> - Expand the [FIX] section to explain why disabling compression will
>   avoid inlined extents
>
> v2:
> - Add more explanation on why we can created inlined extents even if
>   relocation is preallocating space
>
> - Add the fixes: tag since the behavior is caused by a specific commit
>
> There is a syzbot report that an inlined file extent item in a data
> reloc inode triggered a sanity check in get_new_location().
>
> It turns out that we can create inlined file extents for data reloc
> inodes in the first place after commit 3eaf5f082c4c ("btrfs: extract
> inlined creation into a dedicated delalloc helper").
>
> So the first patch will avoid compression for data reloc inodes first,
> then the second patch to reject inlined file extent items in
> get_new_location(), making the checks more robust.
>
> Qu Wenruo (2):
>   btrfs: do not try compression for data reloc inodes
>   btrfs: reject inline file extents item in get_new_location()

Reviewed-by: Filipe Manana <fdmanana@suse.com>

Thanks.

>
>  fs/btrfs/btrfs_inode.h | 2 ++
>  fs/btrfs/relocation.c  | 7 +++++++
>  2 files changed, 9 insertions(+)
>
> --
> 2.54.0
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-23 12:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-23 11:37 [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Qu Wenruo
2026-06-23 11:37 ` [PATCH v3 1/2] btrfs: do not try compression for data reloc inodes Qu Wenruo
2026-06-23 11:37 ` [PATCH v3 2/2] btrfs: reject inline file extents item in get_new_location() Qu Wenruo
2026-06-23 12:16 ` [PATCH v3 0/2] btrfs: fix inlined file extent items in data reloc Filipe Manana

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox