resize.f2fs: fix wrong sit/nat bitmap during rebuild_checkpoint()
authorChao Yu <yuchao0@huawei.com>
Fri, 2 Apr 2021 07:12:43 +0000 (15:12 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Mon, 12 Apr 2021 18:20:56 +0000 (11:20 -0700)
As beroal <me@beroal.in.ua> reported: f2fs image can be corrupted
after below testcase:

1. truncate -s $((256*1024*1024)) img
2. mkfs.f2fs -f img
3. mount -t f2fs -o loop img /mnt
4. xfs_io -f /mnt/file -c "pwrite 0 2M" -c "fsync"
5. umount /mnt
6. resize.f2fs -s -t 262144 img
7. fsck.f2fs img

The root cause is we forgot to copy original sit/nat bitmap to
new checkpoint during rebuild_checkpoint(), fix it.

Reported-and-testd-by: beroal <me@beroal.in.ua>
Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fsck/resize.c

index 0425498..a083f68 100644 (file)
@@ -526,6 +526,11 @@ static void rebuild_checkpoint(struct f2fs_sb_info *sbi,
 
        memcpy(new_cp, cp, (unsigned char *)cp->sit_nat_version_bitmap -
                                                (unsigned char *)cp);
+       if (c.safe_resize)
+               memcpy((void *)new_cp + CP_BITMAP_OFFSET,
+                       (void *)cp + CP_BITMAP_OFFSET,
+                       F2FS_BLKSIZE - CP_BITMAP_OFFSET);
+
        new_cp->checkpoint_ver = cpu_to_le64(cp_ver + 1);
 
        crc = f2fs_checkpoint_chksum(new_cp);