]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ext4: fix incorrect punch max_end
authorZhang Yi <yi.zhang@huawei.com>
Thu, 24 Jul 2025 02:57:16 +0000 (22:57 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 1 Aug 2025 08:48:46 +0000 (09:48 +0100)
[ Upstream commit 29ec9bed2395061350249ae356fb300dd82a78e7 ]

For the extents based inodes, the maxbytes should be sb->s_maxbytes
instead of sbi->s_bitmap_maxbytes. Additionally, for the calculation of
max_end, the -sb->s_blocksize operation is necessary only for
indirect-block based inodes. Correct the maxbytes and max_end value to
correct the behavior of punch hole.

Fixes: 2da376228a24 ("ext4: limit length to bitmap_maxbytes - blocksize in punch_hole")
Signed-off-by: Zhang Yi <yi.zhang@huawei.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Baokun Li <libaokun1@huawei.com>
Link: https://patch.msgid.link/20250506012009.3896990-2-yi.zhang@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Cc: stable@kernel.org
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ext4/inode.c

index ca98f04fcf556d0511f7efceac7d9f6e123534d4..fe1d19d920a963d06649f160f7ddf6f4fb1c1134 100644 (file)
@@ -3992,7 +3992,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
        struct inode *inode = file_inode(file);
        struct super_block *sb = inode->i_sb;
        ext4_lblk_t start_lblk, end_lblk;
-       loff_t max_end = EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize;
+       loff_t max_end = sb->s_maxbytes;
        loff_t end = offset + length;
        handle_t *handle;
        unsigned int credits;
@@ -4001,14 +4001,20 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
        trace_ext4_punch_hole(inode, offset, length, 0);
        WARN_ON_ONCE(!inode_is_locked(inode));
 
+       /*
+        * For indirect-block based inodes, make sure that the hole within
+        * one block before last range.
+        */
+       if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
+               max_end = EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize;
+
        /* No need to punch hole beyond i_size */
        if (offset >= inode->i_size)
                return 0;
 
        /*
         * If the hole extends beyond i_size, set the hole to end after
-        * the page that contains i_size, and also make sure that the hole
-        * within one block before last range.
+        * the page that contains i_size.
         */
        if (end > inode->i_size)
                end = round_up(inode->i_size, PAGE_SIZE);