[PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate

From: Chao Yu

Date: Tue Dec 01 2026 - 06:09:16 EST


From: Chao Yu <chao@xxxxxxxxxx>

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@xxxxxxxxxxxxxxx
Cc: Daeho Jeong <daehojeong@xxxxxxxxxx>
Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
---
fs/f2fs/file.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

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