Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Chao Yu <chao@kernel.org>
To: jaegeuk@kernel.org
Cc: linux-f2fs-devel@lists.sourceforge.net,
	linux-kernel@vger.kernel.org, Chao Yu <chao@kernel.org>,
	stable@vger.kernel.org, Daeho Jeong <daehojeong@google.com>
Subject: [PATCH 4/6] f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed()
Date: Tue,  1 Dec 2026 11:08:46 +0000	[thread overview]
Message-ID: <20261201110842.1825797-10-chao@kernel.org> (raw)
In-Reply-To: <20261201110842.1825797-7-chao@kernel.org>

From: Chao Yu <chao@kernel.org>

With F2FS_IOC_START_ATOMIC_REPLACE, i_size of the original inode is set
to zero, and the old i_size is saved in fi->original_i_size. If a later
write past i_size fails in f2fs_write_begin(), f2fs_write_failed() will
truncate blocks of the original inode beyond i_size, i.e. committed data:

f2fs_ioc_start_atomic_write(truncate=true)
 original_i_size = 512KB, i_size = 0
pwrite(0, 100000)
 i_size = 100000, blocks are reserved in COW inode
pwrite(300000, 4096)
 f2fs_write_begin
  f2fs_filemap_get_folio                -ENOMEM
  f2fs_write_failed(inode, 304096)
   f2fs_truncate_blocks(inode, 100000)  frees committed blocks 25..127
f2fs_ioc_abort_atomic_write
 i_size = original_i_size (512KB)

Then, aborting the atomic write, or a crash before commit, leaves holes
in the original file, or loses the whole file if the failing write comes
first, while fsck.f2fs reports nothing.

Reproducer (4KB blocks):
  write 512KB to a file, fsync
  ioctl(fd, F2FS_IOC_START_ATOMIC_REPLACE)
  pwrite(fd, buf, 100000, 0)
  pwrite(fd, buf, 4096, 300000)  -> -ENOMEM w/ FAULT_PAGE_GET injected
  ioctl(fd, F2FS_IOC_ABORT_ATOMIC_WRITE)

Before: 103 of 128 blocks are zeroed, and freed on disk
After:  original data is intact

Fix it by never truncating an atomic file below original_i_size in
f2fs_write_failed(), so that only blocks preallocated w/
FALLOC_FL_KEEP_SIZE beyond it are freed, as for a regular file.

Also round it up to the block size, otherwise truncate_partial_data_block()
zeroes and dirties the partial 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, then the uncommitted data would be
written to the original inode.

For non-replace atomic write, i_size never goes below original_i_size,
so only the round up applies. Regular files are not affected.

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/data.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index aecedd3a4298..5f6ad38c0de1 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -5168,6 +5168,17 @@ void f2fs_write_failed(struct inode *inode, loff_t to)
 		filemap_invalidate_lock(inode->i_mapping);
 
 		truncate_pagecache(inode, i_size);
+		/*
+		 * The original inode of an atomic file holds committed data
+		 * below original_i_size, don't truncate it, as i_size can be
+		 * smaller in atomic replace mode. Round it up as well, so the
+		 * partial block, which may hold uncommitted data, won't be
+		 * dirtied via the original inode.
+		 */
+		if (f2fs_is_atomic_file(inode))
+			i_size = round_up(max_t(loff_t, i_size,
+					F2FS_I(inode)->original_i_size),
+					F2FS_BLKSIZE(F2FS_I_SB(inode)));
 		f2fs_truncate_blocks(inode, i_size, true);
 
 		filemap_invalidate_unlock(inode->i_mapping);
-- 
2.49.0


  parent reply	other threads:[~2026-12-01 11:08 UTC|newest]

Thread overview: 6+ 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 ` [PATCH 2/6] f2fs: quota: fix to recheck quota flush after taking node_change in checkpoint Chao Yu
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 ` Chao Yu [this message]
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 ` [PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate Chao Yu

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=20261201110842.1825797-10-chao@kernel.org \
    --to=chao@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox