f2fs: fix to adjust appropirate defragment pg_end
authorZhiguo Niu <zhiguo.niu@unisoc.com>
Wed, 27 Mar 2024 08:53:40 +0000 (16:53 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Tue, 2 Apr 2024 23:38:07 +0000 (23:38 +0000)
A length that exceeds the real size of the inode may be
specified from user, although these out-of-range areas
are not mapped, but they still need to be check in
while loop, which is unnecessary.

Signed-off-by: Zhiguo Niu <zhiguo.niu@unisoc.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/file.c

index 719064730f9a6b1605cd1d460cd7a9bb9cc8ee05..bd55bbb4fa67263cb9a0e081ab6081dfebca70ba 100644 (file)
@@ -2626,12 +2626,13 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
        bool fragmented = false;
        int err;
 
-       pg_start = range->start >> PAGE_SHIFT;
-       pg_end = (range->start + range->len) >> PAGE_SHIFT;
-
        f2fs_balance_fs(sbi, true);
 
        inode_lock(inode);
+       pg_start = range->start >> PAGE_SHIFT;
+       pg_end = min_t(pgoff_t,
+                               (range->start + range->len) >> PAGE_SHIFT,
+                               DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE));
 
        if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
                err = -EINVAL;
@@ -2646,8 +2647,9 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
        }
 
        /* writeback all dirty pages in the range */
-       err = filemap_write_and_wait_range(inode->i_mapping, range->start,
-                                               range->start + range->len - 1);
+       err = filemap_write_and_wait_range(inode->i_mapping,
+                                               pg_start << PAGE_SHIFT,
+                                               (pg_end << PAGE_SHIFT) - 1);
        if (err)
                goto out;