From: Daeho Jeong <daeho43@gmail.com>
To: linux-kernel@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com
Cc: Daeho Jeong <daehojeong@google.com>
Subject: [PATCH v2 15/20] f2fs: support subpage blocks in atomic writes
Date: Sun, 15 Nov 2026 20:08:46 -0800 [thread overview]
Message-ID: <20261116040851.2849696-16-daeho43@gmail.com> (raw)
In-Reply-To: <20261116040851.2849696-1-daeho43@gmail.com>
From: Daeho Jeong <daehojeong@google.com>
Writeback already writes dirty blocks to the COW inode per block. Make
write_begin of atomic files reserve and read COW blocks per block as
well, and remove the -EOPNOTSUPP checks. Skipping the read of the
blocks that a write fully covers also applies to large folios when the
block size equals PAGE_SIZE.
Aborting atomic writes drops the page cache and restores i_size without
i_gc_rwsem[WRITE] and invalidate_lock. With subpage blocks, a page fault
in between can make a folio writable with only the blocks within the
smaller i_size prepared, and a read can zero-fill the blocks beyond it
and mark them uptodate, while they are inside the restored i_size. GC
reads through the page cache as well once FI_ATOMIC_FILE is cleared, and
then moves the zeroes. Hold both locks across it as
f2fs_ioc_start_atomic_write() does, except at eviction, where no one
else uses the inode.
Signed-off-by: Daeho Jeong <daehojeong@google.com>
---
fs/f2fs/data.c | 24 ++++++++++++++++--------
fs/f2fs/file.c | 30 ++++++++++++++++++++++--------
fs/f2fs/inode.c | 1 +
fs/f2fs/segment.c | 7 +++++++
4 files changed, 46 insertions(+), 16 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 9f7f8214d10e..b4f16afccc51 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -4698,7 +4698,7 @@ static int f2fs_write_cache_folios(struct address_space *mapping,
}
if (f2fs_has_subpage_blocks(sbi) &&
- (f2fs_compressed_file(inode) || f2fs_is_atomic_file(inode))) {
+ f2fs_compressed_file(inode)) {
WARN_ON_ONCE(1);
mapping_set_error(mapping, -EOPNOTSUPP);
folio_clear_dirty_for_io(folio);
@@ -5325,7 +5325,7 @@ static int prepare_large_folio_write_begin(struct inode *inode,
int err = 0;
if (f2fs_has_subpage_blocks(sbi) &&
- (f2fs_compressed_file(inode) || f2fs_is_atomic_file(inode)))
+ f2fs_compressed_file(inode))
return -EOPNOTSUPP;
/*
@@ -5441,13 +5441,16 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode,
/* Inline data must have been converted before reaching here. */
f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
- start = folio->index + (ori_off >> PAGE_SHIFT);
- end = folio->index + ((ori_off + len - 1) >> PAGE_SHIFT);
+ start = f2fs_folio_lblk(sbi, folio) + F2FS_BYTES_TO_BLK(sbi, ori_off);
+ end = f2fs_folio_lblk(sbi, folio) +
+ F2FS_BYTES_TO_BLK(sbi, ori_off + len - 1);
for (index = start; index <= end; index++) {
+ loff_t block_start = F2FS_BLK_TO_BYTES(sbi, index);
+ loff_t block_end = block_start + F2FS_BLKSIZE(sbi);
block_t blkaddr = NULL_ADDR;
bool node_changed = false;
- size_t off = (index - folio->index) << PAGE_SHIFT;
+ size_t off = f2fs_folio_byte_offset(sbi, folio, index);
err = prepare_atomic_write_begin(sbi, inode, index,
&blkaddr, &node_changed,
@@ -5455,12 +5458,17 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode,
if (err)
return err;
+ /* Skip read if the write covers the entire block. */
+ if (pos <= block_start && pos + len >= block_end)
+ goto balance;
+
if (f2fs_ffs_test_blk_uptodate(folio, index))
goto balance;
if (blkaddr == NEW_ADDR) {
- folio_zero_segment(folio, off, off + PAGE_SIZE);
- f2fs_ffs_mark_subrange_uptodate(folio, off, PAGE_SIZE);
+ folio_zero_segment(folio, off, off + F2FS_BLKSIZE(sbi));
+ f2fs_ffs_mark_subrange_uptodate(folio, off,
+ F2FS_BLKSIZE(sbi));
goto balance;
}
@@ -5517,7 +5525,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
}
if (f2fs_has_subpage_blocks(sbi) &&
- (f2fs_compressed_file(inode) || f2fs_is_atomic_file(inode))) {
+ f2fs_compressed_file(inode)) {
err = -EOPNOTSUPP;
goto fail;
}
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 8540613e3b22..d6304b705e06 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -2314,7 +2314,7 @@ static long f2fs_fallocate(struct file *file, int mode,
}
if (f2fs_has_subpage_blocks(F2FS_I_SB(inode))) {
- if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode)) {
+ if (f2fs_compressed_file(inode)) {
ret = -EOPNOTSUPP;
goto out;
}
@@ -2358,6 +2358,23 @@ static long f2fs_fallocate(struct file *file, int mode,
return ret;
}
+/*
+ * Abort atomic writes with inode_lock held. Until i_size is restored, GC,
+ * page faults and reads would see the i_size of the aborted writes, so keep
+ * them away as f2fs_ioc_start_atomic_write() does.
+ */
+static void f2fs_abort_atomic_write_locked(struct inode *inode, bool clean)
+{
+ if (!f2fs_is_atomic_file(inode))
+ return;
+
+ f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
+ filemap_invalidate_lock(inode->i_mapping);
+ f2fs_abort_atomic_write(inode, clean);
+ filemap_invalidate_unlock(inode->i_mapping);
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
+}
+
static int f2fs_release_file(struct inode *inode, struct file *filp)
{
if (atomic_dec_and_test(&F2FS_I(inode)->open_count))
@@ -2372,7 +2389,7 @@ static int f2fs_release_file(struct inode *inode, struct file *filp)
return 0;
inode_lock(inode);
- f2fs_abort_atomic_write(inode, true);
+ f2fs_abort_atomic_write_locked(inode, true);
inode_unlock(inode);
return 0;
@@ -2391,7 +2408,7 @@ static int f2fs_file_flush(struct file *file, fl_owner_t id)
if (F2FS_I(inode)->atomic_write_task == current &&
(current->flags & PF_EXITING)) {
inode_lock(inode);
- f2fs_abort_atomic_write(inode, true);
+ f2fs_abort_atomic_write_locked(inode, true);
inode_unlock(inode);
}
@@ -2568,9 +2585,6 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
loff_t isize;
int ret;
- if (f2fs_has_subpage_blocks(sbi))
- return -EOPNOTSUPP;
-
if (!(filp->f_mode & FMODE_WRITE))
return -EBADF;
@@ -2702,7 +2716,7 @@ static int f2fs_ioc_commit_atomic_write(struct file *filp)
if (!ret)
ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
- f2fs_abort_atomic_write(inode, ret);
+ f2fs_abort_atomic_write_locked(inode, ret);
} else {
ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
}
@@ -2730,7 +2744,7 @@ static int f2fs_ioc_abort_atomic_write(struct file *filp)
inode_lock(inode);
- f2fs_abort_atomic_write(inode, true);
+ f2fs_abort_atomic_write_locked(inode, true);
inode_unlock(inode);
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 5c6a64b31b05..2bc15fa192be 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -851,6 +851,7 @@ static void f2fs_pre_evict_inode(struct inode *inode)
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct f2fs_inode_info *fi = F2FS_I(inode);
+ /* No one else uses the inode now, so the locks are not needed. */
f2fs_abort_atomic_write(inode, true);
if (fi->cow_inode && f2fs_is_cow_file(fi->cow_inode)) {
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 93f165151c0f..5bff57723e2d 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -186,6 +186,13 @@ bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
}
+/*
+ * The callers hold i_gc_rwsem[WRITE] and invalidate_lock as
+ * f2fs_ioc_start_atomic_write() does, so that no page is read, by GC either,
+ * or made writable between dropping the page cache and restoring i_size, i.e.
+ * with the i_size of the aborted writes. Only the eviction does not, as no one
+ * else uses the inode then.
+ */
void f2fs_abort_atomic_write(struct inode *inode, bool clean)
{
struct f2fs_inode_info *fi = F2FS_I(inode);
--
2.56.0.611.g1030f0b940-goog
next prev parent reply other threads:[~2026-11-16 4:09 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 4:08 [PATCH v2 00/20] f2fs: support block size smaller than PAGE_SIZE Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 01/20] f2fs: use block-based index and size calculations in more paths Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 02/20] f2fs: support subpage block size superblock and mount Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 03/20] f2fs: extend f2fs_folio_state for subpage blocks Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 04/20] f2fs: support subpage blocks in directory operations Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 05/20] f2fs: convert inode dirty page counters to dirty block counters Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 06/20] f2fs: support subpage blocks in data read path and GC Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 07/20] f2fs: support subpage blocks in data write path and writeback Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 08/20] f2fs: adapt range operations and mmap for subpage blocks Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 09/20] f2fs: support subpage blocks in pinned files Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 10/20] f2fs: support subpage blocks in collapse and insert range Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 11/20] f2fs: support subpage blocks in move range Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 12/20] f2fs: support subpage blocks in defragmentation Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 13/20] f2fs: support subpage blocks in swapfile activation Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 14/20] f2fs: support subpage blocks in fs-verity enablement Daeho Jeong
2026-11-16 4:08 ` Daeho Jeong [this message]
2026-11-16 4:08 ` [PATCH v2 16/20] f2fs: preserve inline data in subpage block mode Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 17/20] f2fs: support subpage blocks in online resize Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 18/20] f2fs: support subpage blocks in compression Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 19/20] f2fs: count compress cache entries in pages for its memory limit Daeho Jeong
2026-11-16 4:08 ` [PATCH v2 20/20] f2fs: enable subpage block size support Daeho Jeong
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=20261116040851.2849696-16-daeho43@gmail.com \
--to=daeho43@gmail.com \
--cc=daehojeong@google.com \
--cc=kernel-team@android.com \
--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®