From: Chao Yu <chao@kernel.org>
To: Daeho Jeong <daeho43@gmail.com>
Cc: chao@kernel.org, jaegeuk@kernel.org,
Daeho Jeong <daehojeong@google.com>,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net
Subject: Re: [f2fs-dev] [PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate
Date: Sat, 5 Dec 2026 15:45:17 +0800 [thread overview]
Message-ID: <50959303-c31a-4bd8-abb1-a0fb153571c0@kernel.org> (raw)
In-Reply-To: <CACOAw_w2SJMeKaf6B+=7OEYjfzPpF2rBiyX0HZwN726j5mFbyA@mail.gmail.com>
On 12/4/26 02:08, Daeho Jeong wrote:
> On Tue, Dec 1, 2026 at 3:09 AM Chao Yu via Linux-f2fs-devel
> <linux-f2fs-devel@lists.sourceforge.net> wrote:
>>
>> From: Chao Yu <chao@kernel.org>
>>
>> In f2fs_setattr(), truncating an atomic file to a smaller or the same size
>> calls f2fs_truncate() on the original inode, which can't be rolled back by
>> aborting the atomic write:
>>
>> 1. It zeroes and dirties the partial last block via the original inode,
>> while the folio may hold uncommitted data and have lost its atomic flag
>> after it was written back to the COW inode, and it frees committed
>> blocks beyond the new size:
>>
>> write 10000 bytes of 0x41, fsync committed data
>> ioctl(F2FS_IOC_START_ATOMIC_WRITE)
>> pwrite(904 bytes of 0x55, 4096) uncommitted data
>> sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER)
>> ftruncate(5000)
>> sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER)
>> ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE)
>>
>> After the abort, the original file has 904 bytes of aborted 0x55, and
>> one 4KB block of zeroes, w/ 4KB or 16KB blocks.
>>
>> 2. In atomic replace mode, i_size only covers the new data, so even
>> truncating to the current i_size frees committed data:
>>
>> write 512KB, fsync committed blocks 0..127
>> ioctl(F2FS_IOC_START_ATOMIC_REPLACE) i_size = 0
>> pwrite(0, 100000) written to COW inode
>> ftruncate(100000) frees committed blocks 25..127
>> ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE)
>>
>> After the abort, 103 of 128 blocks of the original file are zeroes,
>> w/ 4KB blocks.
>>
>> SQLite, the major user of atomic write, never shrinks the database file
>> in an atomic write, it only shrinks it after commit. So let's return
>> -EOPNOTSUPP for shrinking an atomic file, as commit 06dfe4a7eb69 ("f2fs:
>> disallow mmap write and data-modifying fallocate on atomic files") does
>> for FALLOC_FL_PUNCH_HOLE and FALLOC_FL_COLLAPSE_RANGE, and skip
>> f2fs_truncate() when truncating it to its current size. Extending an
>> atomic file is still allowed. Then ftruncate() returns -EOPNOTSUPP in 1.,
>> and the original data is intact in both cases.
>>
>> Fixes: 3db1de0e582c ("f2fs: change the current atomic write way")
>> Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE")
>> Cc: stable@vger.kernel.org
>> Cc: Daeho Jeong <daehojeong@google.com>
>> Signed-off-by: Chao Yu <chao@kernel.org>
>> ---
>> fs/f2fs/file.c | 15 ++++++++++++++-
>> 1 file changed, 14 insertions(+), 1 deletion(-)
>>
>
> Hi Chao,
>
> Shrinking is rejected w/ this patch, but extending has the same problem as
> 1.: f2fs_setattr() calls f2fs_zero_post_eof_page() w/ writeback, so
> fill_zero() dirties the EOF block via the original inode w/o the atomic
> flag, and do_zero_post_eof_page() writes it back. If the folio holds
> uncommitted data and has lost its atomic flag, the data is written to the
> original inode:
>
> write 10000 bytes of 0x41, fsync committed data
> ioctl(F2FS_IOC_START_ATOMIC_WRITE)
> pwrite(904 bytes of 0x55, 8192) uncommitted data
> sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER)
> ftruncate(20000)
> ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE)
>
> After the abort, the original file has 904 bytes of aborted 0x55 at 8192,
> and still has them after a remount. This is on dev-test w/ this series
> applied, w/ 4KB blocks, and it is the same w/o the series. W/o the
> sync_file_range() or the ftruncate(), the data is intact.
>
> So how about rejecting any size change of an atomic file here, as in [1]?
> SQLite doesn't seem to need to extend it via truncate either, as it extends
> the database file by writing the last page.
>
> [1] https://patchwork.kernel.org/project/f2fs/patch/tencent_13F84AEDEBCC170BE121ADA2D44EA69CEA07@qq.com/
Hi Daeho,
Actually, I kept extend truncation intentionally, as I investigated that SQLITE w/
atomic write will do this during commit phase, I don't want to break this to cause
any regression.
And previously, I noticed the post eof issue, and fixed in another 'post eof' patchset,
which was in my tree below, I have not yet had time to send them to mailing list, sorry.
Please take a look.
https://git.kernel.org/pub/scm/linux/kernel/git/chao/linux.git/commit/?h=bugfix/common&id=2ecf6804d6b58b01402260c140115a6a357ca6f5
Thanks,
>
> Thanks,
>
>
>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
>> index 5ad772a41f9b..fcf326e3d19b 100644
>> --- a/fs/f2fs/file.c
>> +++ b/fs/f2fs/file.c
>> @@ -1279,6 +1279,14 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>> F2FS_BLK_TO_BYTES(sbi, CAP_BLKS_PER_SEC(sbi))))
>> return -EINVAL;
>> }
>> +
>> + /*
>> + * Shrinking an atomic file would change the original inode,
>> + * which can't be rolled back by abort, as for fallocate().
>> + */
>> + if (f2fs_is_atomic_file(inode) &&
>> + attr->ia_size < i_size_read(inode))
>> + return -EOPNOTSUPP;
>> }
>>
>> if (is_quota_modification(idmap, inode, attr)) {
>> @@ -1338,7 +1346,12 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>> }
>> truncate_setsize(inode, attr->ia_size);
>>
>> - if (attr->ia_size <= old_size)
>> + /*
>> + * Nothing to truncate for an atomic file w/ the same size, and
>> + * f2fs_truncate() would change the original inode.
>> + */
>> + if (attr->ia_size < old_size ||
>> + (attr->ia_size == old_size && !f2fs_is_atomic_file(inode)))
>> err = f2fs_truncate(inode);
>> /*
>> * do not trim all blocks after i_size if target size is
>> --
>> 2.49.0
>>
>>
>>
>> _______________________________________________
>> Linux-f2fs-devel mailing list
>> Linux-f2fs-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
WARNING: multiple messages have this Message-ID (diff)
From: Chao Yu via Linux-f2fs-devel <linux-f2fs-devel@lists.sourceforge.net>
To: Daeho Jeong <daeho43@gmail.com>
Cc: Daeho Jeong <daehojeong@google.com>,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net, jaegeuk@kernel.org
Subject: Re: [f2fs-dev] [PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate
Date: Sat, 5 Dec 2026 15:45:17 +0800 [thread overview]
Message-ID: <50959303-c31a-4bd8-abb1-a0fb153571c0@kernel.org> (raw)
In-Reply-To: <CACOAw_w2SJMeKaf6B+=7OEYjfzPpF2rBiyX0HZwN726j5mFbyA@mail.gmail.com>
On 12/4/26 02:08, Daeho Jeong wrote:
> On Tue, Dec 1, 2026 at 3:09 AM Chao Yu via Linux-f2fs-devel
> <linux-f2fs-devel@lists.sourceforge.net> wrote:
>>
>> From: Chao Yu <chao@kernel.org>
>>
>> In f2fs_setattr(), truncating an atomic file to a smaller or the same size
>> calls f2fs_truncate() on the original inode, which can't be rolled back by
>> aborting the atomic write:
>>
>> 1. It zeroes and dirties the partial last block via the original inode,
>> while the folio may hold uncommitted data and have lost its atomic flag
>> after it was written back to the COW inode, and it frees committed
>> blocks beyond the new size:
>>
>> write 10000 bytes of 0x41, fsync committed data
>> ioctl(F2FS_IOC_START_ATOMIC_WRITE)
>> pwrite(904 bytes of 0x55, 4096) uncommitted data
>> sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER)
>> ftruncate(5000)
>> sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER)
>> ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE)
>>
>> After the abort, the original file has 904 bytes of aborted 0x55, and
>> one 4KB block of zeroes, w/ 4KB or 16KB blocks.
>>
>> 2. In atomic replace mode, i_size only covers the new data, so even
>> truncating to the current i_size frees committed data:
>>
>> write 512KB, fsync committed blocks 0..127
>> ioctl(F2FS_IOC_START_ATOMIC_REPLACE) i_size = 0
>> pwrite(0, 100000) written to COW inode
>> ftruncate(100000) frees committed blocks 25..127
>> ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE)
>>
>> After the abort, 103 of 128 blocks of the original file are zeroes,
>> w/ 4KB blocks.
>>
>> SQLite, the major user of atomic write, never shrinks the database file
>> in an atomic write, it only shrinks it after commit. So let's return
>> -EOPNOTSUPP for shrinking an atomic file, as commit 06dfe4a7eb69 ("f2fs:
>> disallow mmap write and data-modifying fallocate on atomic files") does
>> for FALLOC_FL_PUNCH_HOLE and FALLOC_FL_COLLAPSE_RANGE, and skip
>> f2fs_truncate() when truncating it to its current size. Extending an
>> atomic file is still allowed. Then ftruncate() returns -EOPNOTSUPP in 1.,
>> and the original data is intact in both cases.
>>
>> Fixes: 3db1de0e582c ("f2fs: change the current atomic write way")
>> Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE")
>> Cc: stable@vger.kernel.org
>> Cc: Daeho Jeong <daehojeong@google.com>
>> Signed-off-by: Chao Yu <chao@kernel.org>
>> ---
>> fs/f2fs/file.c | 15 ++++++++++++++-
>> 1 file changed, 14 insertions(+), 1 deletion(-)
>>
>
> Hi Chao,
>
> Shrinking is rejected w/ this patch, but extending has the same problem as
> 1.: f2fs_setattr() calls f2fs_zero_post_eof_page() w/ writeback, so
> fill_zero() dirties the EOF block via the original inode w/o the atomic
> flag, and do_zero_post_eof_page() writes it back. If the folio holds
> uncommitted data and has lost its atomic flag, the data is written to the
> original inode:
>
> write 10000 bytes of 0x41, fsync committed data
> ioctl(F2FS_IOC_START_ATOMIC_WRITE)
> pwrite(904 bytes of 0x55, 8192) uncommitted data
> sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER)
> ftruncate(20000)
> ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE)
>
> After the abort, the original file has 904 bytes of aborted 0x55 at 8192,
> and still has them after a remount. This is on dev-test w/ this series
> applied, w/ 4KB blocks, and it is the same w/o the series. W/o the
> sync_file_range() or the ftruncate(), the data is intact.
>
> So how about rejecting any size change of an atomic file here, as in [1]?
> SQLite doesn't seem to need to extend it via truncate either, as it extends
> the database file by writing the last page.
>
> [1] https://patchwork.kernel.org/project/f2fs/patch/tencent_13F84AEDEBCC170BE121ADA2D44EA69CEA07@qq.com/
Hi Daeho,
Actually, I kept extend truncation intentionally, as I investigated that SQLITE w/
atomic write will do this during commit phase, I don't want to break this to cause
any regression.
And previously, I noticed the post eof issue, and fixed in another 'post eof' patchset,
which was in my tree below, I have not yet had time to send them to mailing list, sorry.
Please take a look.
https://git.kernel.org/pub/scm/linux/kernel/git/chao/linux.git/commit/?h=bugfix/common&id=2ecf6804d6b58b01402260c140115a6a357ca6f5
Thanks,
>
> Thanks,
>
>
>> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
>> index 5ad772a41f9b..fcf326e3d19b 100644
>> --- a/fs/f2fs/file.c
>> +++ b/fs/f2fs/file.c
>> @@ -1279,6 +1279,14 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>> F2FS_BLK_TO_BYTES(sbi, CAP_BLKS_PER_SEC(sbi))))
>> return -EINVAL;
>> }
>> +
>> + /*
>> + * Shrinking an atomic file would change the original inode,
>> + * which can't be rolled back by abort, as for fallocate().
>> + */
>> + if (f2fs_is_atomic_file(inode) &&
>> + attr->ia_size < i_size_read(inode))
>> + return -EOPNOTSUPP;
>> }
>>
>> if (is_quota_modification(idmap, inode, attr)) {
>> @@ -1338,7 +1346,12 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>> }
>> truncate_setsize(inode, attr->ia_size);
>>
>> - if (attr->ia_size <= old_size)
>> + /*
>> + * Nothing to truncate for an atomic file w/ the same size, and
>> + * f2fs_truncate() would change the original inode.
>> + */
>> + if (attr->ia_size < old_size ||
>> + (attr->ia_size == old_size && !f2fs_is_atomic_file(inode)))
>> err = f2fs_truncate(inode);
>> /*
>> * do not trim all blocks after i_size if target size is
>> --
>> 2.49.0
>>
>>
>>
>> _______________________________________________
>> Linux-f2fs-devel mailing list
>> Linux-f2fs-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
next prev parent reply other threads:[~2026-12-05 7:45 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 11:08 [PATCH 1/6] f2fs: fix to clear SBI_IS_CLOSE in __f2fs_remount() Chao Yu
2026-12-01 11:08 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-12-01 11:08 ` [PATCH 2/6] f2fs: quota: fix to recheck quota flush after taking node_change in checkpoint Chao Yu
2026-12-01 11:08 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-12-01 11:08 ` [PATCH 3/6] f2fs: cache: fix to submit cached DATA bio when waiting writeback of meta cache Chao Yu
2026-12-01 11:08 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-12-01 11:08 ` [PATCH 4/6] f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed() Chao Yu
2026-12-01 11:08 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-12-03 18:07 ` Daeho Jeong
2026-12-03 18:07 ` Daeho Jeong
2026-12-05 7:43 ` Chao Yu
2026-12-05 7:43 ` Chao Yu via Linux-f2fs-devel
2026-12-05 7:44 ` Chao Yu
2026-12-05 7:44 ` Chao Yu via Linux-f2fs-devel
2026-12-01 11:08 ` [PATCH 5/6] f2fs: atomic: fix to avoid preallocating blocks in original inode for atomic file Chao Yu
2026-12-01 11:08 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-12-03 18:07 ` Daeho Jeong
2026-12-03 18:07 ` Daeho Jeong
2026-12-01 11:08 ` [PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate Chao Yu
2026-12-01 11:08 ` [f2fs-dev] " Chao Yu via Linux-f2fs-devel
2026-12-03 18:08 ` Daeho Jeong
2026-12-03 18:08 ` Daeho Jeong
2026-12-05 7:45 ` Chao Yu [this message]
2026-12-05 7:45 ` Chao Yu via Linux-f2fs-devel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=50959303-c31a-4bd8-abb1-a0fb153571c0@kernel.org \
--to=chao@kernel.org \
--cc=daeho43@gmail.com \
--cc=daehojeong@google.com \
--cc=jaegeuk@kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.