ext4: restart handle if credits are insufficient during allocating blocks
authorZhang Yi <yi.zhang@huawei.com>
Mon, 7 Jul 2025 14:08:08 +0000 (22:08 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 14 Jul 2025 03:41:51 +0000 (23:41 -0400)
commite2c4c49dee64ca2f42ad2958cbe1805de96b6732
tree98717d178e033671abffb48449e125f97c5196d8
parent2bddafea3d0d85ee9ac3cf5ba9a4b2f2d2f50257
ext4: restart handle if credits are insufficient during allocating blocks

After large folios are supported on ext4, writing back a sufficiently
large and discontinuous folio may consume a significant number of
journal credits, placing considerable strain on the journal. For
example, in a 20GB filesystem with 1K block size and 1MB journal size,
writing back a 2MB folio could require thousands of credits in the
worst-case scenario (when each block is discontinuous and distributed
across different block groups), potentially exceeding the journal size.
This issue can also occur in ext4_write_begin() and ext4_page_mkwrite()
when delalloc is not enabled.

Fix this by ensuring that there are sufficient journal credits before
allocating an extent in mpage_map_one_extent() and
ext4_block_write_begin(). If there are not enough credits, return
-EAGAIN, exit the current mapping loop, restart a new handle and a new
transaction, and allocating blocks on this folio again in the next
iteration.

Suggested-by: Jan Kara <jack@suse.cz>
Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20250707140814.542883-6-yi.zhang@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/inode.c