f2fs: don't allow unaligned truncation to smaller/equal size on pinned file
authorwangzijie <wangzijie1@honor.com>
Tue, 24 Jun 2025 03:59:38 +0000 (11:59 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Mon, 30 Jun 2025 16:44:52 +0000 (16:44 +0000)
To prevent scattered pin block generation, don't allow non-section aligned truncation
to smaller or equal size on pinned file. But for truncation to larger size, after
commit 3fdd89b452c2("f2fs: prevent writing without fallocate() for pinned files"),
we only support overwrite IO to pinned file, so we don't need to consider
attr->ia_size > i_size case.

Signed-off-by: wangzijie <wangzijie1@honor.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/file.c

index 63e9fb5a1c5950526f5594281bb2d72c63f8d077..bc0ca697e06449133278e795d535e368bc18ad06 100644 (file)
@@ -1064,6 +1064,17 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
                        !IS_ALIGNED(attr->ia_size,
                        F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
                        return -EINVAL;
+               /*
+                * To prevent scattered pin block generation, we don't allow
+                * smaller/equal size unaligned truncation for pinned file.
+                * We only support overwrite IO to pinned file, so don't
+                * care about larger size truncation.
+                */
+               if (f2fs_is_pinned_file(inode) &&
+                       attr->ia_size <= i_size_read(inode) &&
+                       !IS_ALIGNED(attr->ia_size,
+                       F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
+                       return -EINVAL;
        }
 
        err = setattr_prepare(idmap, dentry, attr);