]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
f2fs: optimize f2fs_overwrite_io() for f2fs_iomap_begin
authorYeongjin Gil <youngjin.gil@samsung.com>
Thu, 22 Jan 2026 10:45:27 +0000 (19:45 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 19 Feb 2026 15:31:36 +0000 (16:31 +0100)
commit d860974a7e38d35e9e2c4dc8a9f4223b38b6ad99 upstream.

When overwriting already allocated blocks, f2fs_iomap_begin() calls
f2fs_overwrite_io() to check block mappings. However,
f2fs_overwrite_io() iterates through all mapped blocks in the range,
which can be inefficient for fragmented files with large I/O requests.

This patch optimizes f2fs_overwrite_io() by adding a 'check_first'
parameter and introducing __f2fs_overwrite_io() helper. When called from
f2fs_iomap_begin(), we only check the first mapping to determine if the
range is already allocated, which is sufficient for setting
map.m_may_create.

This optimization significantly reduces the number of f2fs_map_blocks()
calls in f2fs_overwrite_io() when called from f2fs_iomap_begin(),
especially for fragmented files with large I/O requests.

Cc: stable@kernel.org
Fixes: 351bc761338d ("f2fs: optimize f2fs DIO overwrites")
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>
Reviewed-by: Sunmin Jeong <s_min.jeong@samsung.com>
Signed-off-by: Yeongjin Gil <youngjin.gil@samsung.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/f2fs/data.c

index f14ee3d0aaa2d17238bde9e47353557ba2571cc5..debdc6ae6eb9d8542492d9512ae4a2a6268c5fef 100644 (file)
@@ -1799,7 +1799,8 @@ out:
        return err;
 }
 
-bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
+static bool __f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len,
+                               bool check_first)
 {
        struct f2fs_map_blocks map;
        block_t last_lblk;
@@ -1821,10 +1822,17 @@ bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
                if (err || map.m_len == 0)
                        return false;
                map.m_lblk += map.m_len;
+               if (check_first)
+                       break;
        }
        return true;
 }
 
+bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
+{
+       return __f2fs_overwrite_io(inode, pos, len, false);
+}
+
 static int f2fs_xattr_fiemap(struct inode *inode,
                                struct fiemap_extent_info *fieinfo)
 {
@@ -4191,7 +4199,7 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
         * f2fs_map_lock and f2fs_balance_fs are not necessary.
         */
        if ((flags & IOMAP_WRITE) &&
-               !f2fs_overwrite_io(inode, offset, length))
+               !__f2fs_overwrite_io(inode, offset, length, true))
                map.m_may_create = true;
 
        err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO);