linux-yocto/fs/btrfs
Filipe Manana 7c77df2332 btrfs: fix assertion when building free space tree
[ Upstream commit 1961d20f6f ]

When building the free space tree with the block group tree feature
enabled, we can hit an assertion failure like this:

  BTRFS info (device loop0 state M): rebuilding free space tree
  assertion failed: ret == 0, in fs/btrfs/free-space-tree.c:1102
  ------------[ cut here ]------------
  kernel BUG at fs/btrfs/free-space-tree.c:1102!
  Internal error: Oops - BUG: 00000000f2000800 [#1]  SMP
  Modules linked in:
  CPU: 1 UID: 0 PID: 6592 Comm: syz-executor322 Not tainted 6.15.0-rc7-syzkaller-gd7fa1af5b33e #0 PREEMPT
  Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/07/2025
  pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
  pc : populate_free_space_tree+0x514/0x518 fs/btrfs/free-space-tree.c:1102
  lr : populate_free_space_tree+0x514/0x518 fs/btrfs/free-space-tree.c:1102
  sp : ffff8000a4ce7600
  x29: ffff8000a4ce76e0 x28: ffff0000c9bc6000 x27: ffff0000ddfff3d8
  x26: ffff0000ddfff378 x25: dfff800000000000 x24: 0000000000000001
  x23: ffff8000a4ce7660 x22: ffff70001499cecc x21: ffff0000e1d8c160
  x20: ffff0000e1cb7800 x19: ffff0000e1d8c0b0 x18: 00000000ffffffff
  x17: ffff800092f39000 x16: ffff80008ad27e48 x15: ffff700011e740c0
  x14: 1ffff00011e740c0 x13: 0000000000000004 x12: ffffffffffffffff
  x11: ffff700011e740c0 x10: 0000000000ff0100 x9 : 94ef24f55d2dbc00
  x8 : 94ef24f55d2dbc00 x7 : 0000000000000001 x6 : 0000000000000001
  x5 : ffff8000a4ce6f98 x4 : ffff80008f415ba0 x3 : ffff800080548ef0
  x2 : 0000000000000000 x1 : 0000000100000000 x0 : 000000000000003e
  Call trace:
   populate_free_space_tree+0x514/0x518 fs/btrfs/free-space-tree.c:1102 (P)
   btrfs_rebuild_free_space_tree+0x14c/0x54c fs/btrfs/free-space-tree.c:1337
   btrfs_start_pre_rw_mount+0xa78/0xe10 fs/btrfs/disk-io.c:3074
   btrfs_remount_rw fs/btrfs/super.c:1319 [inline]
   btrfs_reconfigure+0x828/0x2418 fs/btrfs/super.c:1543
   reconfigure_super+0x1d4/0x6f0 fs/super.c:1083
   do_remount fs/namespace.c:3365 [inline]
   path_mount+0xb34/0xde0 fs/namespace.c:4200
   do_mount fs/namespace.c:4221 [inline]
   __do_sys_mount fs/namespace.c:4432 [inline]
   __se_sys_mount fs/namespace.c:4409 [inline]
   __arm64_sys_mount+0x3e8/0x468 fs/namespace.c:4409
   __invoke_syscall arch/arm64/kernel/syscall.c:35 [inline]
   invoke_syscall+0x98/0x2b8 arch/arm64/kernel/syscall.c:49
   el0_svc_common+0x130/0x23c arch/arm64/kernel/syscall.c:132
   do_el0_svc+0x48/0x58 arch/arm64/kernel/syscall.c:151
   el0_svc+0x58/0x17c arch/arm64/kernel/entry-common.c:767
   el0t_64_sync_handler+0x78/0x108 arch/arm64/kernel/entry-common.c:786
   el0t_64_sync+0x198/0x19c arch/arm64/kernel/entry.S:600
  Code: f0047182 91178042 528089c3 9771d47b (d4210000)
  ---[ end trace 0000000000000000 ]---

This happens because we are processing an empty block group, which has
no extents allocated from it, there are no items for this block group,
including the block group item since block group items are stored in a
dedicated tree when using the block group tree feature. It also means
this is the block group with the highest start offset, so there are no
higher keys in the extent root, hence btrfs_search_slot_for_read()
returns 1 (no higher key found).

Fix this by asserting 'ret' is 0 only if the block group tree feature
is not enabled, in which case we should find a block group item for
the block group since it's stored in the extent root and block group
item keys are greater than extent item keys (the value for
BTRFS_BLOCK_GROUP_ITEM_KEY is 192 and for BTRFS_EXTENT_ITEM_KEY and
BTRFS_METADATA_ITEM_KEY the values are 168 and 169 respectively).
In case 'ret' is 1, we just need to add a record to the free space
tree which spans the whole block group, and we can achieve this by
making 'ret == 0' as the while loop's condition.

Reported-by: syzbot+36fae25c35159a763a2a@syzkaller.appspotmail.com
Link: https://lore.kernel.org/linux-btrfs/6841dca8.a00a0220.d4325.0020.GAE@google.com/
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2025-07-17 18:32:14 +02:00
..
tests btrfs: tests: allocate dummy fs_info and root in test_find_delalloc() 2024-08-29 17:30:37 +02:00
acl.c
async-thread.c
async-thread.h
backref.c btrfs: fix information leak in btrfs_ioctl_logical_to_ino() 2024-05-02 16:29:28 +02:00
backref.h
block-group.c btrfs: get zone unusable bytes while holding lock at btrfs_reclaim_bgs_work() 2025-06-04 14:40:04 +02:00
block-group.h btrfs: add and use helper to check if block group is used 2024-02-23 09:12:28 +01:00
block-rsv.c btrfs: calculate the right space for delayed refs when updating global reserve 2024-09-30 16:23:55 +02:00
block-rsv.h btrfs: calculate the right space for delayed refs when updating global reserve 2024-09-30 16:23:55 +02:00
btrfs_inode.h
check-integrity.c
check-integrity.h
compression.c btrfs: fix extent map use-after-free when adding pages to compressed bio 2024-09-04 13:25:00 +02:00
compression.h
ctree.c btrfs: fix use-after-free when COWing tree bock and tracing is enabled 2025-01-09 13:30:03 +01:00
ctree.h btrfs: rename and export __btrfs_cow_block() 2025-01-09 13:30:03 +01:00
delalloc-space.c btrfs: don't reserve space for checksums when writing to nocow files 2024-02-23 09:12:29 +01:00
delalloc-space.h
delayed-inode.c btrfs: change BUG_ON to assertion when checking for delayed_node root 2024-08-29 17:30:37 +02:00
delayed-inode.h btrfs: fix infinite directory reads 2024-01-31 16:17:05 -08:00
delayed-ref.c btrfs: reinitialize delayed ref list after deleting it from the list 2024-11-14 13:15:17 +01:00
delayed-ref.h btrfs: calculate the right space for delayed refs when updating global reserve 2024-09-30 16:23:55 +02:00
dev-replace.c btrfs: dev-replace: properly validate device names 2024-03-06 14:45:10 +00:00
dev-replace.h
dir-item.c btrfs: fix passing 0 to ERR_PTR in btrfs_search_dir_index_item() 2024-11-01 01:56:06 +01:00
discard.c btrfs: make btrfs_discard_workfn() block_group ref explicit 2025-06-04 14:40:04 +02:00
discard.h
disk-io.c btrfs: handle csum tree error with rescue=ibadroots correctly 2025-07-06 10:57:56 +02:00
disk-io.h btrfs: fix double free of anonymous device after snapshot creation failure 2024-03-06 14:45:10 +00:00
export.c btrfs: export: handle invalid inode or root reference in btrfs_get_parent() 2024-04-13 13:05:01 +02:00
export.h
extent_io.c btrfs: avoid linker error in btrfs_find_create_tree_block() 2025-06-04 14:40:04 +02:00
extent_io.h
extent_map.c btrfs: fix wrong block_start calculation for btrfs_drop_extent_map_range() 2024-06-21 14:35:38 +02:00
extent_map.h btrfs: get the next extent map during fiemap/lseek more efficiently 2023-04-26 14:28:38 +02:00
extent-io-tree.c
extent-io-tree.h
extent-tree.c btrfs: don't BUG_ON() when 0 reference count at btrfs_lookup_extent_info() 2025-05-22 14:10:09 +02:00
file-item.c btrfs: mark the len field in struct btrfs_ordered_sum as unsigned 2024-01-10 17:10:35 +01:00
file.c btrfs: avoid page_lockend underflow in btrfs_punch_hole_lock_range() 2025-05-02 07:46:54 +02:00
free-space-cache.c btrfs: zoned: properly take lock to read/update block group's zoned variables 2024-08-29 17:30:15 +02:00
free-space-cache.h
free-space-tree.c btrfs: fix assertion when building free space tree 2025-07-17 18:32:14 +02:00
free-space-tree.h btrfs: make clear_cache mount option to rebuild FST without disabling it 2023-05-17 11:53:42 +02:00
inode-item.c btrfs: use struct fscrypt_str instead of struct qstr 2023-10-10 22:00:36 +02:00
inode-item.h btrfs: use struct fscrypt_str instead of struct qstr 2023-10-10 22:00:36 +02:00
inode.c btrfs: propagate last_unlink_trans earlier when doing a rmdir 2025-07-17 18:32:10 +02:00
ioctl.c btrfs: qgroup: fix qgroup prealloc rsv leak in subvolume operations 2024-12-14 19:53:56 +01:00
Kconfig
locking.c btrfs: add block-group tree to lockdep classes 2023-07-19 16:22:13 +02:00
locking.h
lzo.c
Makefile
misc.h
ordered-data.c btrfs: fix qgroup_free_reserved_data int overflow 2024-01-10 17:10:35 +01:00
ordered-data.h btrfs: mark the len field in struct btrfs_ordered_sum as unsigned 2024-01-10 17:10:35 +01:00
orphan.c
print-tree.c btrfs: avoid using fixed char array size for tree names 2024-08-14 13:52:59 +02:00
print-tree.h
props.c
props.h
qgroup.c btrfs: run delayed iputs when flushing delalloc 2024-09-04 13:24:55 +02:00
qgroup.h btrfs: fix qgroup_free_reserved_data int overflow 2024-01-10 17:10:35 +01:00
raid56.c
raid56.h
rcu-string.h
ref-verify.c btrfs: ref-verify: fix use-after-free after invalid ref action 2024-12-14 19:54:10 +01:00
ref-verify.h
reflink.c btrfs: replace sb::s_blocksize by fs_info::sectorsize 2024-08-29 17:30:42 +02:00
reflink.h
relocation.c btrfs: check folio mapping after unlock in relocate_one_folio() 2025-06-04 14:40:22 +02:00
root-tree.c btrfs: qgroup: fix qgroup prealloc rsv leak in subvolume operations 2024-12-14 19:53:56 +01:00
scrub.c btrfs: scrub: initialize ret in scrub_simple_mirror() to fix compilation warning 2024-07-11 12:47:10 +02:00
send.c btrfs: send: return -ENAMETOOLONG when attempting a path that is too long 2025-06-04 14:40:05 +02:00
send.h
space-info.c btrfs: zoned: fix zone_unusable accounting on making block group read-write again 2024-08-11 12:36:00 +02:00
space-info.h btrfs: zoned: fix zone_unusable accounting on making block group read-write again 2024-08-11 12:36:00 +02:00
struct-funcs.c
subpage.c
subpage.h
super.c btrfs: correctly escape subvol in btrfs_show_options() 2025-04-25 10:43:53 +02:00
sysfs.c btrfs: sysfs: fix direct super block member reads 2025-01-02 10:30:55 +01:00
sysfs.h
transaction.c btrfs: fix use-after-free when attempting to join an aborted transaction 2025-02-21 13:49:29 +01:00
transaction.h btrfs: fix race between direct IO write and fsync when using same fd 2024-09-12 11:10:29 +02:00
tree-checker.c btrfs: tree-checker: reject inline extent items with 0 ref count 2024-12-27 13:52:59 +01:00
tree-checker.h
tree-defrag.c
tree-log.c btrfs: fix iteration of extrefs during log replay 2025-07-10 15:59:47 +02:00
tree-log.h btrfs: use struct fscrypt_str instead of struct qstr 2023-10-10 22:00:36 +02:00
tree-mod-log.c
tree-mod-log.h
ulist.c
ulist.h
uuid-tree.c
verity.c
volumes.c btrfs: update superblock's device bytes_used when dropping chunk 2025-07-06 10:58:01 +02:00
volumes.h btrfs: add a helper to read the superblock metadata_uuid 2023-09-23 11:11:08 +02:00
xattr.c
xattr.h
zlib.c
zoned.c btrfs: zoned: fix zone finishing with missing devices 2025-04-25 10:44:02 +02:00
zoned.h
zstd.c