]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ext4: correct the journal credits calculations of allocating blocks
authorZhang Yi <yi.zhang@huawei.com>
Mon, 12 May 2025 06:33:16 +0000 (14:33 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 20 May 2025 14:31:12 +0000 (10:31 -0400)
The journal credits calculation in ext4_ext_index_trans_blocks() is
currently inadequate. It only multiplies the depth of the extents tree
and doesn't account for the blocks that may be required for adding the
leaf extents themselves.

After enabling large folios, we can easily run out of handle credits,
triggering a warning in jbd2_journal_dirty_metadata() on filesystems
with a 1KB block size. This occurs because we may need more extents when
iterating through each large folio in
ext4_do_writepages()->mpage_map_and_submit_extent(). Therefore, we
should modify ext4_ext_index_trans_blocks() to include a count of the
leaf extents in the worst case as well.

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

index c42472fd4b63e3fa636371879c78de54edad1f56..ddc3a752996132bbbbf67ecd23eb835ec584a7d8 100644 (file)
@@ -2409,9 +2409,10 @@ int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
         * the time we actually modify the tree. Assume the worst case.
         */
        if (extents <= 1)
-               index = EXT4_MAX_EXTENT_DEPTH * 2;
+               index = (EXT4_MAX_EXTENT_DEPTH * 2) + extents;
        else
-               index = EXT4_MAX_EXTENT_DEPTH * 3;
+               index = (EXT4_MAX_EXTENT_DEPTH * 3) +
+                       DIV_ROUND_UP(extents, ext4_ext_space_block(inode, 0));
 
        return index;
 }
index 3a46f2822cf02f6dd57a96ddf4a18bb5febce4e6..18651f39a80bb514f890a2724c2ce5cbe9d775f9 100644 (file)
@@ -5844,18 +5844,16 @@ static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
        int ret;
 
        /*
-        * How many index blocks need to touch to map @lblocks logical blocks
-        * to @pextents physical extents?
+        * How many index and lead blocks need to touch to map @lblocks
+        * logical blocks to @pextents physical extents?
         */
        idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
 
-       ret = idxblocks;
-
        /*
         * Now let's see how many group bitmaps and group descriptors need
         * to account
         */
-       groups = idxblocks + pextents;
+       groups = idxblocks;
        gdpblocks = groups;
        if (groups > ngroups)
                groups = ngroups;
@@ -5863,7 +5861,7 @@ static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
                gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
 
        /* bitmaps and block group descriptor blocks */
-       ret += groups + gdpblocks;
+       ret = idxblocks + groups + gdpblocks;
 
        /* Blocks for super block, inode, quota and xattr blocks */
        ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);