]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
f2fs: prevent writing without fallocate() for pinned files
authorDaeho Jeong <daehojeong@google.com>
Thu, 11 Apr 2024 17:54:10 +0000 (10:54 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 10 Jul 2025 14:03:14 +0000 (16:03 +0200)
[ Upstream commit 3fdd89b452c2ea5e2195d6e315bef122769584c9 ]

In a case writing without fallocate(), we can't guarantee it's allocated
in the conventional area for zoned stroage. To make it consistent across
storage devices, we disallow it regardless of storage device types.

Signed-off-by: Daeho Jeong <daehojeong@google.com>
Reviewed-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Stable-dep-of: ba8dac350faf ("f2fs: fix to zero post-eof page")
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/f2fs/file.c

index 3e3822115d1e440c9fe48a016cea641342b8c944..2eff37cc37601fe25ed8d4d6743f06e74e82854e 100644 (file)
@@ -57,7 +57,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
        struct inode *inode = file_inode(vmf->vma->vm_file);
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
        struct dnode_of_data dn;
-       bool need_alloc = true;
+       bool need_alloc = !f2fs_is_pinned_file(inode);
        int err = 0;
        vm_fault_t ret;
 
@@ -114,19 +114,18 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
                goto out_sem;
        }
 
+       set_new_dnode(&dn, inode, NULL, NULL, 0);
        if (need_alloc) {
                /* block allocation */
-               set_new_dnode(&dn, inode, NULL, NULL, 0);
                err = f2fs_get_block_locked(&dn, page->index);
-       }
-
-#ifdef CONFIG_F2FS_FS_COMPRESSION
-       if (!need_alloc) {
-               set_new_dnode(&dn, inode, NULL, NULL, 0);
+       } else {
                err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
                f2fs_put_dnode(&dn);
+               if (f2fs_is_pinned_file(inode) &&
+                   !__is_valid_data_blkaddr(dn.data_blkaddr))
+                       err = -EIO;
        }
-#endif
+
        if (err) {
                unlock_page(page);
                goto out_sem;
@@ -3332,7 +3331,7 @@ static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
                goto done;
        }
 
-       if (f2fs_sb_has_blkzoned(sbi) && F2FS_HAS_BLOCKS(inode)) {
+       if (F2FS_HAS_BLOCKS(inode)) {
                ret = -EFBIG;
                goto out;
        }
@@ -4919,6 +4918,8 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
        bool dio;
        bool may_need_sync = true;
        int preallocated;
+       const loff_t pos = iocb->ki_pos;
+       const ssize_t count = iov_iter_count(from);
        ssize_t ret;
 
        if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
@@ -4940,6 +4941,12 @@ static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
                inode_lock(inode);
        }
 
+       if (f2fs_is_pinned_file(inode) &&
+           !f2fs_overwrite_io(inode, pos, count)) {
+               ret = -EIO;
+               goto out_unlock;
+       }
+
        ret = f2fs_write_checks(iocb, from);
        if (ret <= 0)
                goto out_unlock;