ext4: prevent stale extent cache entries caused by concurrent fiemap
authorZhang Yi <yi.zhang@huawei.com>
Wed, 23 Apr 2025 08:52:52 +0000 (16:52 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 14 May 2025 14:42:12 +0000 (10:42 -0400)
commit151ff9325e5e17c97839a00b740726656b04647b
tree99a1e8872870295a56bef15de744f55f9b4c1414
parent402e38e6b71f5739119ca3107f375e112d63c7c5
ext4: prevent stale extent cache entries caused by concurrent fiemap

The ext4_fiemap() currently invokes ext4_ext_precache() and
iomap_fiemap() to preload the extent cache and query mapping information
without holding the inode's i_rwsem. This can result in stale extent
cache entries when competing with operations such as
ext4_collapse_range() which calls ext4_ext_remove_space() or
ext4_ext_shift_extents().

The problem arises when ext4_ext_remove_space() temporarily releases
i_data_sem due to insufficient journal credits. During this interval, a
concurrent ext4_fiemap() may cache extent entries that are about to be
deleted. As a result, these cached entries become stale and inconsistent
with the actual extents.

Loading the extents cache without holding the inode's i_rwsem or the
mapping's invalidate_lock is not permitted besides during the writeback.
Fix this by holding the i_rwsem in ext4_fiemap().

Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Link: https://patch.msgid.link/20250423085257.122685-5-yi.zhang@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/extents.c