[PATCH] f2fs: fix data loss when zeroing post-EOF data of a compressed file

From: Hengyu Liang

Date: Mon Nov 16 2026 - 21:39:32 EST


Commit 5eced87b7d19 ("f2fs: fix to zero post-EOF data when extending
file size") made do_zero_post_eof_page() zero the rest of the last
page with fill_zero() and write the page back when a file is extended.

However, fill_zero() does not decompress. When the page belongs to a
compressed cluster and is not in the page cache, it gets a page of
zeros, and the cluster is written back with that page. As of now,
extending a compressed file with write(), truncate() or fallocate()
replaces the data from the start of its last page to the old end of
the file with zeros.

The issue can be reproduced with:

mkfs.f2fs -q -f -O extra_attr,compression /dev/vda
mount -o compress_extension=txt,fsync_mode=strict /dev/vda /mnt
yes | head -c 16000 > /mnt/a.txt
sync; echo 3 > /proc/sys/vm/drop_caches
echo end >> /mnt/a.txt
sync; echo 3 > /proc/sys/vm/drop_caches
tr -d 'y\n' < /mnt/a.txt | wc -c

Before commit 5eced87b7d19 ("f2fs: fix to zero post-EOF data when
extending file size"), the result is 3, the length of "end".

After that commit, the result is 3715. The file has 3712 zero bytes
before "end".

This patch will prepare the cluster as a write to it does, which
decompresses it, then zero the rest of the page and write the cluster
back.

Fixes: 5eced87b7d19 ("f2fs: fix to zero post-EOF data when extending file size")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hengyu Liang <hengyul@xxxxxxxxxx>
---
Commit fda0c0dcefc2 ("f2fs: do f2fs_zero_post_eof_page for all the
time") in the dev branch drops the fsync_mode=strict condition. With
it, the commands above give the same result without that option.

Application impact (Ubuntu 26.04 user space): rsyslog 8.2512.0 appends
to syslog.log on a volume mounted with compress_extension=log,
fsync_mode=strict. It is started 16 times and gets 400 messages each
time; the volume is unmounted and mounted again in between. On v7.2,
the file has all 6400 messages. On v7.3, it has 6327 of them and 9620
zero bytes.

Tested on the dev branch with and without fsync_mode=strict: files
extended by write, truncate and fallocate keep their data, and rsyslog
keeps all 6400 messages.

fs/f2fs/file.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 05061254c3b2..59480c3ba2e8 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -63,6 +63,33 @@ static int do_zero_post_eof_page(struct inode *inode, loff_t new_size)
return err;
}

+#ifdef CONFIG_F2FS_FS_COMPRESSION
+ if (f2fs_compressed_file(inode)) {
+ int log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
+ void *fsdata = NULL;
+ struct folio *folio;
+ struct page *page;
+ loff_t start;
+
+ /* a page of a compressed cluster must be decompressed first */
+ err = f2fs_prepare_compress_overwrite(inode, &page, index,
+ &fsdata);
+ if (err < 0)
+ return err;
+ if (err) {
+ folio = page_folio(page);
+ folio_zero_range(folio,
+ offset_in_folio(folio, old_size), len);
+ f2fs_compress_write_end(inode, fsdata, index, true);
+ start = round_down(old_size,
+ 1 << log_cluster_size << PAGE_SHIFT);
+ return filemap_write_and_wait_range(inode->i_mapping,
+ start,
+ old_size + len - 1);
+ }
+ }
+#endif
+
err = fill_zero(inode, index, offset, len);
if (err)
return err;

base-commit: 9b12299b437e0cc3ebf402e7b36a89cc69151238
--
2.53.0