From: Hungyu Lin <dennylin0707@gmail.com>
To: jaegeuk@kernel.org, chao@kernel.org
Cc: daehojeong@google.com, linux-f2fs-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org, dennylin0707@gmail.com
Subject: [PATCH v2] f2fs: read atomic replacements through the COW inode
Date: Fri, 20 Nov 2026 11:02:13 +0000 [thread overview]
Message-ID: <20261120110213.68391-1-dennylin0707@gmail.com> (raw)
Starting an atomic replacement drops the page cache and resets i_size,
but keeps the original block mappings so the operation can be aborted.
After a sparse write extends the replacement, buffered reads still use
those original mappings for the skipped range and cache the old data.
Commit removes the old gap mappings, but the cached data remains visible
even after reopening the file. A subsequent partial write to the gap can
write those stale bytes back to disk.
Resolve buffered reads through the COW inode during atomic replacement,
in both f2fs_read_single_page() and f2fs_read_data_large_folio(), so
unwritten ranges are read as holes. Keep using the original inode for
the read bio and its encryption context. Once FI_ATOMIC_COMMITTED is set,
use the newly committed original mappings again.
f2fs_commit_atomic_write() sets FI_ATOMIC_COMMITTED before it removes
the old gap mappings. Hold the mapping invalidate lock while committing,
as f2fs_abort_atomic_write_locked() does, so that buffered reads cannot
map those blocks in between.
Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE")
Cc: stable@kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
v1 -> v2:
- Rebase onto the f2fs dev branch.
- Also map through the COW inode in f2fs_read_data_large_folio().
- Drop the invalidate lock from f2fs_abort_atomic_write(); the callers
take it since "f2fs: support subpage blocks in atomic writes".
v1: https://lore.kernel.org/linux-f2fs-devel/20261104191621.89561-1-dennylin0707@gmail.com/
fs/f2fs/data.c | 14 ++++++++++++--
fs/f2fs/segment.c | 2 ++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index cdc9e6e8633c..b006f92b3c70 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -2650,6 +2650,14 @@ static inline blk_opf_t f2fs_ra_op_flags(struct readahead_control *rac)
return rac ? REQ_RAHEAD : 0;
}
+static struct inode *f2fs_read_map_inode(struct inode *inode)
+{
+ if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE) &&
+ !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
+ return F2FS_I(inode)->cow_inode;
+ return inode;
+}
+
static int f2fs_read_single_page(struct inode *inode, struct fsverity_info *vi,
struct folio *folio, unsigned int nr_pages,
struct f2fs_map_blocks *map,
@@ -2695,7 +2703,8 @@ static int f2fs_read_single_page(struct inode *inode, struct fsverity_info *vi,
map->m_lblk = block_in_file;
map->m_len = last_block - block_in_file;
- ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT);
+ ret = f2fs_map_blocks(f2fs_read_map_inode(inode), map,
+ F2FS_GET_BLOCK_DEFAULT);
if (ret)
goto out;
got_it:
@@ -3490,7 +3499,8 @@ static int f2fs_read_data_large_folio(struct inode *inode,
map.m_lblk = index;
map.m_len = max_nr_blocks;
- ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
+ ret = f2fs_map_blocks(f2fs_read_map_inode(inode), &map,
+ F2FS_GET_BLOCK_DEFAULT);
if (ret)
goto err_out;
got_it:
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 066b7cf813d7..b3554b27751c 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -417,11 +417,13 @@ int f2fs_commit_atomic_write(struct inode *inode)
return err;
f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
+ filemap_invalidate_lock(inode->i_mapping);
f2fs_lock_op(sbi, &lc);
err = __f2fs_commit_atomic_write(inode);
f2fs_unlock_op(sbi, &lc);
+ filemap_invalidate_unlock(inode->i_mapping);
f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
return err;
base-commit: b61a029304bb5531312ebc133b2a895d14dc3fee
--
2.43.0
reply other threads:[~2026-11-20 11:02 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261120110213.68391-1-dennylin0707@gmail.com \
--to=dennylin0707@gmail.com \
--cc=chao@kernel.org \
--cc=daehojeong@google.com \
--cc=jaegeuk@kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@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
all inboxes | Powered by JetHome®