[PATCH 1/8] nilfs2: fix leak of gc inodes on error

From: Ryusuke Konishi

Date: Thu Nov 26 2026 - 23:42:46 EST


From: Wang Jianjian <wangjianjian3@xxxxxxxxxx>

Zhengzheng reported below syz problems:

NILFS (loop5): disposed unprocessed dirty file(s) when stopping log writer
VFS: Busy inodes after unmount of loop5 (nilfs2)

The reproducer called ioctl clean_segments on a corrupted image. It's
first argument is { ino = 8, cno = 0xffffffffffffffff, vd_vblocknr = 34,
vd_period = [0x30000000000000, 0x10), vd_blocknr = 6, vd_offset =
7,...}.

nilfs_ioctl_clean_segments
nilfs_ioctl_move_blocks // gc iget 8, get one inode reference
nilfs_clean_segments
nilfs->ns_gc_inodes move to sci->sc_gc_inodes
nilfs_segctor_construct
nilfs_segctor_do_construct
nilfs_segctor_assign
nilfs_segctor_update_payload_blocknr
nilfs_bmap_assign
nilfs_btree_assign_gc
nilfs_dat_move
// since there is no dat entry for vblknr 34
// (entry->de_blocknr == 0), return -EINVAL
// fs will be remounted to readonly
// clean segments loop will be break because readonly fs
nilfs_remove_all_gcinodes
// since nilfs->ns_gc_inodes empty, it
// clean nothing

ioctl_clean_segment will get one extra reference of inode 8 and when
segments construct do GC move vblock 34 and do bmap assign, it lookup
DAT table and get de_blocknr = 0, so it return -EINVAL. And since
nilfs->ns_gc_inodes is empty, nilfs_remove_all_gcinodes will do nothing
and the extra inode reference won't be released.

To fix this, splice sci->sc_gc_inodes back to nilfs->ns_gc_inodes when
got error.

Note that, when normal files add to nilfs dirty list by
nilfs_set_file_dirty, it will got one reference and when
nilfs_bmap_assign got error, nilfs_segctor_do_construct() also doesn't
put the inode, but as a last resort, nilfs_segctor_destroy will do
it(however, it doesn't clean sci->sc_gc_inodes).

Fixes: ce5a5ad1a833 ("nilfs2: fix infinite loop in nilfs_clean_segments()")
Signed-off-by: Wang Jianjian <wangjianjian3@xxxxxxxxxx>
Reported-by: fengaojigui88@xxxxxxxxx
Closes: https://lore.kernel.org/linux-nilfs/CAO-4RZ2=4okHYZ-xCkpV-ekj-aSqKLnYGOsRM9A2dD8R4Ozzeg@xxxxxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
---
fs/nilfs2/segment.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
index 68b640a7aa5b..4c2d51f6550c 100644
--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -2586,6 +2586,7 @@ int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
out_unlock:
sci->sc_freesegs = NULL;
sci->sc_nfreesegs = 0;
+ list_splice_tail_init(&sci->sc_gc_inodes, &nilfs->ns_gc_inodes);
nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
bail_unlock:
nilfs_transaction_unlock(sb);
--
2.53.0