* [PATCH 00/14] f2fs: support & optimize large folios for writable files
@ 2026-08-26 8:26 Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 01/14] f2fs: extend folio state for large folio write path Nanzhe Zhao
` (13 more replies)
0 siblings, 14 replies; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
This series supports large folios for most readable/writable files
in buffered I/O paths, including normal files, block-layer encrypted
files, and atomic files except for inline-data files, compressed files,
and quota files.
It also extends f2fs_folio_state to coexist with f2fs page-private
flags and to handle large folio write path and partial truncate of
large folios correctly.
Benchmark details can be found in the v2 cover letter [1].
[1] https://lore.kernel.org/linux-f2fs-devel/20260622160830.324455-1-zhaonanzhe@xiaomi.com/
Nanzhe Zhao (14):
f2fs: extend folio state for large folio write path
f2fs: carry subpage offset and count in write IO
f2fs: support regular file buffered writes on large folios
f2fs: support atomic file large folios buffered write
f2fs: support large folio writeback
f2fs: prepare mmap write faults for large folios
f2fs: make GC migration large-folio aware
f2fs: optimize small block size large folio read
f2fs: support partial uptodate large folio read
f2fs: handle partial truncate of large folio dirty subpages
f2fs: fix zeroing paths for large folios
f2fs: handle block cloning within the same large folio
f2fs: allow large folio support to writeable files
f2fs: make compressed files compatible with large folio
fs/f2fs/compress.c | 2 +
fs/f2fs/data.c | 1270 +++++++++++++++++++++++++++++++++++++++++++++++-----
fs/f2fs/f2fs.h | 123 ++++-
fs/f2fs/file.c | 191 +++++---
fs/f2fs/gc.c | 30 +-
fs/f2fs/inode.c | 4 +-
fs/f2fs/namei.c | 1 +
fs/f2fs/segment.c | 5 +-
8 files changed, 1431 insertions(+), 195 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 01/14] f2fs: extend folio state for large folio write path
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-27 6:57 ` Chao Yu
2026-08-26 8:26 ` [PATCH 02/14] f2fs: carry subpage offset and count in write IO Nanzhe Zhao
` (12 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Large folio write path needs a subpage status bitmap and write
pages pending counter, while keeping compatible with f2fs private
flags.
Move struct f2fs_folio_state to f2fs.h, add private_flags and
subpage state bitmap, and change PAGE_PRIVATE functions to be
compatible with f2fs_folio_state. Allocate f2fs_folio_state via kzalloc
instead of kmem_cache, since the state size depends on the folio order.
Note: Now if a path wants to use f2fs_folio_state, it must call
`folio_has_ffs` instead of `folio_test_large`` to make check.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/compress.c | 2 +
fs/f2fs/data.c | 60 ++++++++++++++++--------------
fs/f2fs/f2fs.h | 92 ++++++++++++++++++++++++++++++++++++----------
fs/f2fs/segment.c | 2 +-
4 files changed, 108 insertions(+), 48 deletions(-)
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index 91855d91bbdd..84da3e39efb4 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -78,6 +78,8 @@ bool f2fs_is_compressed_page(struct folio *folio)
return false;
if (folio_test_f2fs_nonpointer(folio))
return false;
+ if (f2fs_folio_has_ffs(folio))
+ return false;
f2fs_bug_on(F2FS_F_SB(folio),
*((u32 *)folio->private) != F2FS_COMPRESSED_PAGE_MAGIC);
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 6ae0eb37d20f..578a90d427e2 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -32,20 +32,13 @@
static struct kmem_cache *bio_post_read_ctx_cache;
static struct kmem_cache *bio_entry_slab;
-static struct kmem_cache *ffs_entry_slab;
static mempool_t *bio_post_read_ctx_pool;
static struct bio_set f2fs_bioset;
-struct f2fs_folio_state {
- spinlock_t state_lock;
- unsigned int read_pages_pending;
-};
-
struct f2fs_bio {
struct work_struct work;
struct bio bio;
};
-
#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
int __init f2fs_init_bioset(void)
@@ -133,6 +126,9 @@ struct bio_post_read_ctx {
block_t fs_blkaddr;
};
+static bool __ffs_mark_subrange_uptodate(struct folio *folio,
+ struct f2fs_folio_state *ffs, size_t offset, size_t len);
+
/*
* Update and unlock a bio's pages, and free the bio.
*
@@ -155,7 +151,7 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
bio_for_each_folio_all(fi, bio) {
struct folio *folio = fi.folio;
- unsigned nr_pages = fi.length >> PAGE_SHIFT;
+ unsigned int nr_pages = fi.length >> PAGE_SHIFT;
bool finished = true;
if (!folio_test_large(folio) &&
@@ -360,6 +356,7 @@ static void f2fs_write_end_bio(struct bio *bio)
bio_for_each_folio_all(fi, bio) {
struct folio *folio = fi.folio;
+ unsigned int nr_pages = fi.length >> PAGE_SHIFT;
enum count_type type;
if (fscrypt_is_bounce_folio(folio)) {
@@ -2516,17 +2513,32 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
}
#endif
-static struct f2fs_folio_state *ffs_find_or_alloc(struct folio *folio)
+struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio)
{
- struct f2fs_folio_state *ffs = folio->private;
+ struct f2fs_folio_state *ffs;
+ unsigned int nr_subpages = folio_nr_pages(folio);
+ unsigned long private_flags = 0;
+
+ f2fs_bug_on(F2FS_F_SB(folio), !folio_test_large(folio));
- if (ffs)
- return ffs;
+ if (f2fs_folio_has_ffs(folio))
+ return (struct f2fs_folio_state *)folio->private;
- ffs = f2fs_kmem_cache_alloc(ffs_entry_slab,
- GFP_NOIO | __GFP_ZERO, true, NULL);
+ if (folio_test_private(folio) && folio_test_f2fs_nonpointer(folio))
+ private_flags = (unsigned long)folio->private;
+
+ ffs = kzalloc(struct_size(ffs, state, BITS_TO_LONGS(2 * nr_subpages)),
+ GFP_NOIO | __GFP_NOFAIL);
spin_lock_init(&ffs->state_lock);
+ ffs->private_flags = private_flags;
+ if (folio_test_uptodate(folio))
+ bitmap_set(ffs->state, 0, nr_subpages);
+ if (folio_test_dirty(folio))
+ bitmap_set(ffs->state, nr_subpages, nr_subpages);
+
+ if (folio_test_private(folio))
+ folio_detach_private(folio);
folio_attach_private(folio, ffs);
return ffs;
}
@@ -2535,7 +2547,7 @@ static void ffs_detach_free(struct folio *folio)
{
struct f2fs_folio_state *ffs;
- if (!folio_test_large(folio)) {
+ if (!f2fs_folio_has_ffs(folio)) {
folio_detach_private(folio);
return;
}
@@ -2545,7 +2557,8 @@ static void ffs_detach_free(struct folio *folio)
return;
WARN_ON_ONCE(ffs->read_pages_pending != 0);
- kmem_cache_free(ffs_entry_slab, ffs);
+ WARN_ON_ONCE(atomic_read(&ffs->write_pages_pending));
+ kfree(ffs);
}
static int f2fs_read_data_large_folio(struct inode *inode,
@@ -2558,7 +2571,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
pgoff_t index, offset, next_pgofs = 0;
unsigned max_nr_pages = rac ? readahead_count(rac) :
folio_nr_pages(folio);
- unsigned nrpages;
+ unsigned int nrpages, len_blks;
struct f2fs_folio_state *ffs;
int ret = 0;
bool folio_in_bio = false;
@@ -2634,7 +2647,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
* to prevent from premature folio_end_read() call on folio
*/
if (folio_test_large(folio)) {
- ffs = ffs_find_or_alloc(folio);
+ ffs = f2fs_ffs_find_or_alloc(folio);
/* set the bitmap to wait */
spin_lock_irq(&ffs->state_lock);
@@ -2987,7 +3000,7 @@ bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
return true;
if (fio) {
- if (page_private_gcing(fio->page))
+ if (folio_test_f2fs_gcing(fio->folio))
return true;
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
@@ -4586,21 +4599,12 @@ int __init f2fs_init_bio_entry_cache(void)
if (!bio_entry_slab)
return -ENOMEM;
- ffs_entry_slab = f2fs_kmem_cache_create("f2fs_ffs_slab",
- sizeof(struct f2fs_folio_state));
-
- if (!ffs_entry_slab) {
- kmem_cache_destroy(bio_entry_slab);
- return -ENOMEM;
- }
-
return 0;
}
void f2fs_destroy_bio_entry_cache(void)
{
kmem_cache_destroy(bio_entry_slab);
- kmem_cache_destroy(ffs_entry_slab);
}
static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 16720f1f0a9c..2e8f85cea6d0 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -1628,6 +1628,18 @@ static inline void f2fs_clear_bit(unsigned int nr, char *addr);
* Layout B: lowest bit should be 0
* page.private is a wrapped pointer.
*/
+
+struct f2fs_folio_state {
+ spinlock_t state_lock;
+ unsigned int read_pages_pending;
+ atomic_t write_pages_pending;
+ unsigned long private_flags;
+ /* state[0..nr_subpages - 1] tracks uptodate subpages.
+ * state[nr_subpages..2 * nr_subpages - 1] tracks dirty subpages.
+ */
+ unsigned long state[];
+};
+
enum {
PAGE_PRIVATE_NOT_POINTER, /* private contains non-pointer data */
PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */
@@ -1637,6 +1649,14 @@ enum {
PAGE_PRIVATE_MAX
};
+static inline bool f2fs_folio_has_ffs(const struct folio *folio)
+{
+ unsigned long private = (unsigned long)folio->private;
+
+ return folio_test_large(folio) && private &&
+ !(private & BIT(PAGE_PRIVATE_NOT_POINTER));
+}
+
/* For compression */
enum compress_algorithm_type {
COMPRESS_LZO,
@@ -2682,10 +2702,57 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
return -ENOSPC;
}
+static inline unsigned long f2fs_folio_get_private_flags(const struct folio *folio)
+{
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs = folio->private;
+
+ return ffs->private_flags;
+ }
+
+ return (unsigned long)folio->private;
+}
+
+static inline void f2fs_folio_set_private_flags(struct folio *folio,
+ unsigned long flags)
+{
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs = folio->private;
+
+ ffs->private_flags |= flags;
+ return;
+ }
+
+ if (!folio_test_private(folio))
+ folio_attach_private(folio, (void *)flags);
+ else
+ folio->private = (void *)((unsigned long)folio->private | flags);
+}
+
+static inline void f2fs_folio_clear_private_flags(struct folio *folio,
+ unsigned long flags)
+{
+ unsigned long private;
+
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs = folio->private;
+
+ ffs->private_flags &= ~flags;
+ return;
+ }
+
+ private = (unsigned long)folio->private;
+ private &= ~flags;
+ if (private == BIT(PAGE_PRIVATE_NOT_POINTER))
+ folio_detach_private(folio);
+ else
+ folio->private = (void *)private;
+}
+
#define PAGE_PRIVATE_GET_FUNC(name, flagname) \
static inline bool folio_test_f2fs_##name(const struct folio *folio) \
{ \
- unsigned long priv = (unsigned long)folio->private; \
+ unsigned long priv = f2fs_folio_get_private_flags(folio); \
unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
(1UL << PAGE_PRIVATE_##flagname); \
return (priv & v) == v; \
@@ -2702,12 +2769,7 @@ static inline void folio_set_f2fs_##name(struct folio *folio) \
{ \
unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
(1UL << PAGE_PRIVATE_##flagname); \
- if (!folio->private) \
- folio_attach_private(folio, (void *)v); \
- else { \
- v |= (unsigned long)folio->private; \
- folio->private = (void *)v; \
- } \
+ f2fs_folio_set_private_flags(folio, v); \
} \
static inline void set_page_private_##name(struct page *page) \
{ \
@@ -2720,13 +2782,8 @@ static inline void set_page_private_##name(struct page *page) \
#define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
static inline void folio_clear_f2fs_##name(struct folio *folio) \
{ \
- unsigned long v = (unsigned long)folio->private; \
- \
- v &= ~(1UL << PAGE_PRIVATE_##flagname); \
- if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \
- folio_detach_private(folio); \
- else \
- folio->private = (void *)v; \
+ f2fs_folio_clear_private_flags(folio, \
+ 1UL << PAGE_PRIVATE_##flagname); \
} \
static inline void clear_page_private_##name(struct page *page) \
{ \
@@ -2752,7 +2809,7 @@ PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
static inline unsigned long folio_get_f2fs_data(struct folio *folio)
{
- unsigned long data = (unsigned long)folio->private;
+ unsigned long data = f2fs_folio_get_private_flags(folio);
if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
return 0;
@@ -2763,10 +2820,7 @@ static inline void folio_set_f2fs_data(struct folio *folio, unsigned long data)
{
data = (1UL << PAGE_PRIVATE_NOT_POINTER) | (data << PAGE_PRIVATE_MAX);
- if (!folio_test_private(folio))
- folio_attach_private(folio, (void *)data);
- else
- folio->private = (void *)((unsigned long)folio->private | data);
+ f2fs_folio_set_private_flags(folio, data);
}
static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index ed6f2947210b..df10119d94ad 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -3803,7 +3803,7 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
return CURSEG_COLD_DATA_PINNED;
- if (page_private_gcing(fio->page)) {
+ if (folio_test_f2fs_gcing(fio->folio)) {
if (fio->sbi->am.atgc_enabled &&
(fio->io_type == FS_DATA_IO) &&
(fio->sbi->gc_mode != GC_URGENT_HIGH) &&
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 02/14] f2fs: carry subpage offset and count in write IO
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 01/14] f2fs: extend folio state for large folio write path Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-27 7:16 ` Chao Yu
2026-08-26 8:26 ` [PATCH 03/14] f2fs: support regular file buffered writes on large folios Nanzhe Zhao
` (11 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Large folio write paths need to submit I/O for a range inside a
folio instead of always submitting the whole folio from offset zero.
Add folio_offset and folio_blkcnt to f2fs_io_info to describe the
block offset inside the folio and the number of contiguous blocks
covered by the I/O.
Apply the new fields to the bio submit paths that need the subpage
offset or contiguous block count.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 59 ++++++++++++++++++++++++++++++++---------------
fs/f2fs/f2fs.h | 11 +++++++++
fs/f2fs/segment.c | 3 ++-
3 files changed, 54 insertions(+), 19 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 578a90d427e2..8649d443ca74 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -783,6 +783,9 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
struct folio *fio_folio = fio->folio;
struct folio *data_folio = fio->encrypted_page ?
page_folio(fio->encrypted_page) : fio_folio;
+ pgoff_t fio_lblk = F2FS_FIO_LBLK(fio_folio, fio);
+ size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
+ size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
fio->is_por ? META_POR : (__is_meta_io(fio) ?
@@ -795,11 +798,11 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
bio = __bio_alloc(fio, 1);
f2fs_set_bio_crypt_ctx(bio, fio_folio->mapping->host,
- fio_folio->index, fio, GFP_NOIO);
- bio_add_folio_nofail(bio, data_folio, folio_size(data_folio), 0);
+ fio_lblk, fio, GFP_NOIO);
+ bio_add_folio_nofail(bio, data_folio, bio_len, bio_offset);
if (fio->io_wbc && !is_read_io(fio->op))
- wbc_account_cgroup_owner(fio->io_wbc, fio_folio, PAGE_SIZE);
+ wbc_account_cgroup_owner(fio->io_wbc, fio_folio, bio_len);
inc_page_count(fio->sbi, is_read_io(fio->op) ?
__read_io_type(data_folio) : WB_DATA_TYPE(fio->folio, false));
@@ -844,7 +847,8 @@ static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
}
static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
- struct folio *folio, enum temp_type temp)
+ struct folio *folio, size_t len, size_t offset,
+ enum temp_type temp)
{
struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
struct bio_entry *be;
@@ -853,7 +857,7 @@ static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
be->bio = bio;
bio_get(bio);
- bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
+ bio_add_folio_nofail(bio, folio, len, offset);
f2fs_down_write(&io->bio_list_lock);
list_add_tail(&be->list, &io->bio_list);
@@ -870,6 +874,9 @@ static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
struct folio *folio)
{
struct folio *fio_folio = fio->folio;
+ pgoff_t fio_lblk = F2FS_FIO_LBLK(fio_folio, fio);
+ size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
+ size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
struct f2fs_sb_info *sbi = fio->sbi;
enum temp_type temp;
bool found = false;
@@ -892,8 +899,8 @@ static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
fio->new_blkaddr));
if (f2fs_crypt_mergeable_bio(*bio,
fio_folio->mapping->host,
- fio_folio->index, fio) &&
- bio_add_folio(*bio, folio, folio_size(folio), 0)) {
+ fio_lblk, fio) &&
+ bio_add_folio(*bio, folio, bio_len, bio_offset)) {
ret = 0;
break;
}
@@ -1007,6 +1014,10 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
struct folio *data_folio = fio->encrypted_page ?
page_folio(fio->encrypted_page) : fio->folio;
struct folio *folio = fio->folio;
+ pgoff_t fio_lblk = F2FS_FIO_LBLK(folio, fio);
+ unsigned int fio_cnt = F2FS_FIO_BLKCNT(fio);
+ size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
+ size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
__is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
@@ -1021,9 +1032,10 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
if (!bio) {
bio = __bio_alloc(fio, BIO_MAX_VECS);
f2fs_set_bio_crypt_ctx(bio, folio->mapping->host,
- folio->index, fio, GFP_NOIO);
+ fio_lblk, fio, GFP_NOIO);
- add_bio_entry(fio->sbi, bio, data_folio, fio->temp);
+ add_bio_entry(fio->sbi, bio, data_folio, bio_len,
+ bio_offset, fio->temp);
} else {
if (add_ipu_page(fio, &bio, data_folio))
goto alloc_new;
@@ -1034,7 +1046,7 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
inc_page_count(fio->sbi, WB_DATA_TYPE(folio, false));
- *fio->last_block = fio->new_blkaddr;
+ *fio->last_block = fio->new_blkaddr + fio_cnt - 1;
*fio->bio = bio;
return 0;
@@ -1070,6 +1082,10 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
struct folio *bio_folio;
struct f2fs_lock_context lc;
enum count_type type;
+ pgoff_t fio_lblk;
+ unsigned int fio_cnt;
+ size_t bio_offset;
+ size_t bio_len;
f2fs_bug_on(sbi, is_read_io(fio->op));
@@ -1108,6 +1124,9 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
/* set submitted = true as a return value */
fio->submitted = 1;
+ fio_lblk = F2FS_FIO_LBLK(fio->folio, fio);
+ fio_cnt = F2FS_FIO_BLKCNT(fio);
+
type = WB_DATA_TYPE(bio_folio, fio->compressed_page);
inc_page_count(sbi, type);
@@ -1115,26 +1134,28 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
(!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
fio->new_blkaddr) ||
!f2fs_crypt_mergeable_bio(io->bio, fio_inode(fio),
- bio_folio->index, fio)))
+ fio_lblk, fio)))
__submit_merged_bio(io);
alloc_new:
if (io->bio == NULL) {
io->bio = __bio_alloc(fio, BIO_MAX_VECS);
f2fs_set_bio_crypt_ctx(io->bio, fio_inode(fio),
- bio_folio->index, fio, GFP_NOIO);
+ fio_lblk, fio, GFP_NOIO);
io->fio = *fio;
}
- if (!bio_add_folio(io->bio, bio_folio, folio_size(bio_folio), 0)) {
+ bio_offset = F2FS_FIO_BIO_OFFSET(fio);
+ bio_len = F2FS_FIO_BIO_SIZE(fio);
+
+ if (!bio_add_folio(io->bio, bio_folio, bio_len, bio_offset)) {
__submit_merged_bio(io);
goto alloc_new;
}
if (fio->io_wbc)
- wbc_account_cgroup_owner(fio->io_wbc, fio->folio,
- folio_size(fio->folio));
+ wbc_account_cgroup_owner(fio->io_wbc, fio->folio, bio_len);
- io->last_block_in_bio = fio->new_blkaddr;
+ io->last_block_in_bio = fio->new_blkaddr + fio_cnt - 1;
trace_f2fs_submit_folio_write(fio->folio, fio);
#ifdef CONFIG_BLK_DEV_ZONED
@@ -3039,7 +3060,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
set_new_dnode(&dn, inode, NULL, NULL, 0);
if (need_inplace_update(fio) &&
- f2fs_lookup_read_extent_cache_block(inode, folio->index,
+ f2fs_lookup_read_extent_cache_block(inode,
+ F2FS_FIO_LBLK(folio, fio),
&fio->old_blkaddr)) {
if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
DATA_GENERIC_ENHANCE))
@@ -3058,7 +3080,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi, &lc))
return -EAGAIN;
- err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
+ err = f2fs_get_dnode_of_data(&dn, F2FS_FIO_LBLK(folio, fio),
+ LOOKUP_NODE);
if (err)
goto out;
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 2e8f85cea6d0..31ddd321daf6 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -1357,6 +1357,8 @@ struct f2fs_io_info {
blk_opf_t op_flags; /* req_flag_bits */
block_t new_blkaddr; /* new block address to be written */
block_t old_blkaddr; /* old block address before Cow */
+ pgoff_t folio_offset; /* offset in large folio */
+ unsigned int folio_blkcnt; /* block count in large folio */
union {
struct page *page; /* page to be written */
struct folio *folio;
@@ -1378,6 +1380,15 @@ struct f2fs_io_info {
sector_t *last_block; /* last block number in bio */
};
+#define F2FS_FIO_LBLK(folio, fio) \
+ ((folio)->index + (fio)->folio_offset)
+#define F2FS_FIO_BLKCNT(fio) \
+ ((fio)->folio_blkcnt ? (fio)->folio_blkcnt : 1)
+#define F2FS_FIO_BIO_OFFSET(fio) \
+ ((fio)->folio_offset << PAGE_SHIFT)
+#define F2FS_FIO_BIO_SIZE(fio) \
+ F2FS_BLK_TO_BYTES(F2FS_FIO_BLKCNT(fio))
+
struct bio_entry {
struct bio *bio;
struct list_head list;
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index df10119d94ad..5bb1e51a38a0 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -3816,7 +3816,8 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
if (file_is_cold(inode) || f2fs_need_compress_data(inode))
return CURSEG_COLD_DATA;
- type = __get_age_segment_type(inode, fio->folio->index);
+ type = __get_age_segment_type(inode,
+ F2FS_FIO_LBLK(fio->folio, fio));
if (type != NO_CHECK_TYPE)
return type;
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 03/14] f2fs: support regular file buffered writes on large folios
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 01/14] f2fs: extend folio state for large folio write path Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 02/14] f2fs: carry subpage offset and count in write IO Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-27 8:56 ` Chao Yu
2026-08-26 8:26 ` [PATCH 04/14] f2fs: support atomic file large folios buffered write Nanzhe Zhao
` (10 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
To avoid the complexity of unlocking a large folio in write_begin,
preallocate partial blocks for inodes that can use large folios.
During write_begin, read only the partial head and tail 4K subpages
that need read-before-write, and skip read I/O for the full middle
subpages covered by the write.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++--
fs/f2fs/f2fs.h | 1 +
fs/f2fs/file.c | 17 ++-
3 files changed, 293 insertions(+), 14 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 8649d443ca74..7ce96ae02cfc 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -1249,6 +1249,40 @@ static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
f2fs_submit_read_bio(sbi, bio, DATA);
}
+/*
+ * Synchronously read a single 4K subpage by reusing f2fs_submit_page_read()
+ * so that iostat, trace, blk-crypto and post-read handling are all preserved.
+ * The caller must have already allocated ffs for the folio.
+ */
+static int f2fs_submit_page_read_sync(struct inode *inode, struct folio *folio,
+ pgoff_t index, block_t blkaddr)
+{
+ struct f2fs_folio_state *ffs = folio->private;
+ unsigned long flags;
+
+ /* Add bias so end_io does not call folio_end_read(). */
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ ffs->read_pages_pending++;
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+
+ f2fs_submit_page_read(inode, NULL, folio, index, blkaddr,
+ REQ_OP_READ, false);
+
+ /* Wait for pending drops back to bias which indicates all bio have completed. */
+ while (READ_ONCE(ffs->read_pages_pending) != 1)
+ f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
+
+ /* Remove the bias. */
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ ffs->read_pages_pending--;
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+
+ if (!f2fs_ffs_test_blk_uptodate(folio, index))
+ return -EIO;
+
+ return 0;
+}
+
static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
{
__le32 *addr = get_dnode_addr(dn->inode, dn->node_folio);
@@ -2582,6 +2616,112 @@ static void ffs_detach_free(struct folio *folio)
kfree(ffs);
}
+bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int idx;
+
+ if (!f2fs_folio_has_ffs(folio))
+ return folio_test_uptodate(folio);
+
+ if (!folio_contains(folio, index))
+ return false;
+
+ ffs = (struct f2fs_folio_state *)folio->private;
+ idx = index - folio->index;
+ return test_bit(idx, ffs->state);
+}
+
+static bool __ffs_mark_subrange_uptodate(struct folio *folio,
+ struct f2fs_folio_state *ffs, size_t offset, size_t len)
+{
+ unsigned int nr_subpages = folio_nr_pages(folio);
+ unsigned int start, end;
+
+ start = offset >> PAGE_SHIFT;
+ end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ end = min(end, nr_subpages);
+
+ bitmap_set(ffs->state, start, end - start);
+ return bitmap_full(ffs->state, nr_subpages);
+}
+
+static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
+ size_t len)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned long flags;
+ bool mark_uptodate = false;
+
+ f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
+
+ if (!f2fs_folio_has_ffs(folio)) {
+ folio_mark_uptodate(folio);
+ return;
+ }
+
+ ffs = (struct f2fs_folio_state *)folio->private;
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ mark_uptodate = __ffs_mark_subrange_uptodate(folio, ffs, offset, len) &&
+ !ffs->read_pages_pending;
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+ if (mark_uptodate)
+ folio_mark_uptodate(folio);
+}
+
+static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
+ size_t offset, size_t len)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int nr_subpages, start, end;
+ unsigned long flags;
+
+ f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
+
+ if (!f2fs_folio_has_ffs(folio))
+ return;
+
+ ffs = (struct f2fs_folio_state *)folio->private;
+ nr_subpages = folio_nr_pages(folio);
+ start = offset >> PAGE_SHIFT;
+ end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ end = min(end, nr_subpages);
+
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ bitmap_set(ffs->state, nr_subpages + start, end - start);
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+}
+
+static bool find_next_valid_block(const struct folio *folio,
+ size_t orig_off, size_t *need_off,
+ size_t len)
+{
+ size_t start = orig_off;
+ size_t end = start + len;
+ size_t head, tail;
+ pgoff_t index;
+
+ if (start & (PAGE_SIZE - 1)) {
+ head = round_down(start, PAGE_SIZE);
+ index = folio->index + (head >> PAGE_SHIFT);
+ if (!f2fs_ffs_test_blk_uptodate(folio, index)) {
+ *need_off = head;
+ return true;
+ }
+ }
+
+ if (end & (PAGE_SIZE - 1)) {
+ tail = round_down(end - 1, PAGE_SIZE);
+ index = folio->index + (tail >> PAGE_SHIFT);
+ if (!f2fs_ffs_test_blk_uptodate(folio, index)) {
+ *need_off = tail;
+ return true;
+ }
+ }
+
+ return false;
+}
+
static int f2fs_read_data_large_folio(struct inode *inode,
struct fsverity_info *vi,
struct readahead_control *rac, struct folio *folio)
@@ -3975,6 +4115,102 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
return 0;
}
+static int prepare_large_folio_write_begin(struct inode *inode,
+ struct folio *folio, loff_t pos,
+ unsigned int len)
+{
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ struct f2fs_folio_state *ffs;
+ size_t ori_off = offset_in_folio(folio, pos);
+ size_t need_off = ori_off;
+ int err = 0;
+
+ len = min_t(unsigned int, len, folio_size(folio) - ori_off);
+
+ /*
+ * When folio minimum order is non-zero, the fsverity
+ * page_cache_write() path enters f2fs_write_begin() via
+ * aops->write_begin without going through f2fs_write_iter(),
+ * so preallocation from f2fs_write_iter() is skipped. In that
+ * case, if FI_PREALLOCATED_ALL is not set, we must preallocate
+ * the write blocks here.
+ */
+ if (!is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) {
+ struct f2fs_map_blocks map = {};
+
+ map.m_lblk = F2FS_BYTES_TO_BLK(pos);
+ map.m_len = F2FS_BLK_ALIGN(pos + len) - map.m_lblk;
+
+ if (!IS_DEVICE_ALIASING(inode))
+ map.m_may_create = true;
+ map.m_seg_type = NO_CHECK_TYPE;
+
+ err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
+ if (err)
+ return err;
+ }
+
+ /*
+ * Skip read and ffs allocation if the folio is already fully
+ * uptodate or the write covers the entire folio.
+ */
+ if (folio_test_uptodate(folio) || len == folio_size(folio))
+ return 0;
+
+ ffs = f2fs_ffs_find_or_alloc(folio);
+ if (!ffs)
+ return -ENOMEM;
+
+ /* Inline data must have been converted before reaching here. */
+ f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
+
+ while (find_next_valid_block(folio, ori_off,
+ &need_off, len)) {
+ struct dnode_of_data dn;
+ pgoff_t index = folio->index + (need_off >> PAGE_SHIFT);
+ block_t blkaddr;
+ bool get_dn = false;
+
+ if (!f2fs_lookup_read_extent_cache_block(inode, index,
+ &blkaddr)) {
+ if (IS_DEVICE_ALIASING(inode))
+ return -ENODATA;
+
+ set_new_dnode(&dn, inode, NULL, NULL, 0);
+ err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
+ if (err)
+ return err;
+ get_dn = true;
+ blkaddr = dn.data_blkaddr;
+
+ if (blkaddr == NEW_ADDR) {
+ size_t off = offset_in_folio(folio,
+ index << PAGE_SHIFT);
+
+ folio_zero_segment(folio, off, off + PAGE_SIZE);
+ f2fs_ffs_mark_subrange_uptodate(folio, off,
+ PAGE_SIZE);
+ goto out;
+ }
+
+ if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
+ DATA_GENERIC_ENHANCE_READ)) {
+ err = -EFSCORRUPTED;
+ goto out;
+ }
+ }
+
+ err = f2fs_submit_page_read_sync(inode, folio, index, blkaddr);
+out:
+ if (get_dn)
+ f2fs_put_dnode(&dn);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
static int f2fs_write_begin(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned len, struct folio **foliop,
@@ -3986,6 +4222,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
pgoff_t index = pos >> PAGE_SHIFT;
bool need_balance = false;
block_t blkaddr = NULL_ADDR;
+ fgf_t fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT;
int err = 0;
trace_f2fs_write_begin(inode, pos, len);
@@ -4033,9 +4270,9 @@ static int f2fs_write_begin(const struct kiocb *iocb,
* Do not use FGP_STABLE to avoid deadlock.
* Will wait that below with our IO control.
*/
- folio = f2fs_filemap_get_folio(mapping, index,
- FGP_LOCK | FGP_WRITE | FGP_CREAT,
- mapping_gfp_mask(mapping));
+ fgp |= fgf_set_order(len);
+ folio = __filemap_get_folio(mapping, index, fgp,
+ mapping_gfp_mask(mapping));
if (IS_ERR(folio)) {
err = PTR_ERR(folio);
goto fail;
@@ -4048,7 +4285,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
if (f2fs_is_atomic_file(inode))
err = prepare_atomic_write_begin(sbi, folio, pos, len,
&blkaddr, &need_balance);
- else
+ else if (!folio_test_large(folio))
err = prepare_write_begin(sbi, folio, pos, len,
&blkaddr, &need_balance);
if (err)
@@ -4069,6 +4306,14 @@ static int f2fs_write_begin(const struct kiocb *iocb,
f2fs_folio_wait_writeback(folio, DATA, false, true);
+ if (folio_test_large(folio)) {
+ err = prepare_large_folio_write_begin(inode,
+ folio, pos, len);
+ if (!err)
+ return 0;
+ goto put_folio;
+ }
+
if (len == folio_size(folio) || folio_test_uptodate(folio))
return 0;
@@ -4129,15 +4374,20 @@ static int f2fs_write_end(const struct kiocb *iocb,
trace_f2fs_write_end(inode, pos, len, copied);
/*
- * This should be come from len == PAGE_SIZE, and we expect copied
- * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
- * let generic_perform_write() try to copy data again through copied=0.
+ * If a short copy happens on a folio that isn't uptodate, we treat
+ * it with zero copied and let generic_perform_write() try to copy
+ * data again through copied=0.
*/
if (!folio_test_uptodate(folio)) {
- if (unlikely(copied != len))
+ if (unlikely(copied != len)) {
copied = 0;
- else
+ } else if (folio_test_large(folio)) {
+ f2fs_ffs_mark_subrange_uptodate(folio,
+ offset_in_folio(folio, pos), len);
+ } else {
+ /* This should be come from len == PAGE_SIZE */
folio_mark_uptodate(folio);
+ }
}
#ifdef CONFIG_F2FS_FS_COMPRESSION
@@ -4156,6 +4406,9 @@ static int f2fs_write_end(const struct kiocb *iocb,
if (!copied)
goto unlock_out;
+ if (folio_test_large(folio))
+ f2fs_ffs_mark_subrange_dirty(folio, offset_in_folio(folio, pos),
+ copied);
folio_mark_dirty(folio);
if (f2fs_is_atomic_file(inode))
@@ -4218,8 +4471,22 @@ static bool f2fs_dirty_data_folio(struct address_space *mapping,
trace_f2fs_set_page_dirty(folio, DATA);
- if (!folio_test_uptodate(folio))
- folio_mark_uptodate(folio);
+ if (!folio_test_uptodate(folio)) {
+ bool uptodate = true;
+
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs =
+ (struct f2fs_folio_state *)folio->private;
+ unsigned long flags;
+
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ uptodate = bitmap_full(ffs->state, folio_nr_pages(folio)) &&
+ !ffs->read_pages_pending;
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+ }
+ if (uptodate)
+ folio_mark_uptodate(folio);
+ }
BUG_ON(folio_test_swapcache(folio));
if (filemap_dirty_folio(mapping, folio)) {
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 31ddd321daf6..ae6031693700 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4342,6 +4342,7 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
struct writeback_control *wbc,
enum iostat_type io_type,
int compr_blocks, bool allow_balance);
+bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
void f2fs_write_failed(struct inode *inode, loff_t to);
void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
bool f2fs_release_folio(struct folio *folio, gfp_t wait);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index d440231b8cb9..e40285d4f9c2 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -5405,9 +5405,20 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
return ret;
}
- /* Do not preallocate blocks that will be written partially in 4KB. */
- map.m_lblk = F2FS_BLK_ALIGN(pos);
- map.m_len = F2FS_BYTES_TO_BLK(pos + count);
+ if (mapping_large_folio_support(inode->i_mapping)) {
+ /*
+ * Preallocate all blocks touched by a large-folio buffered write so
+ * the regular write_begin path does not need to unlock the folio for
+ * f2fs_balance_fs(). Rechecking large-folio state after unlock is
+ * unreliable since partial truncation can split the folio.
+ */
+ map.m_lblk = F2FS_BYTES_TO_BLK(pos);
+ map.m_len = F2FS_BLK_ALIGN(pos + count);
+ } else {
+ /* Do not preallocate blocks that will be written partially in 4KB. */
+ map.m_lblk = F2FS_BLK_ALIGN(pos);
+ map.m_len = F2FS_BYTES_TO_BLK(pos + count);
+ }
if (map.m_len > map.m_lblk)
map.m_len -= map.m_lblk;
else
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 04/14] f2fs: support atomic file large folios buffered write
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (2 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 03/14] f2fs: support regular file buffered writes on large folios Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-27 9:24 ` Chao Yu
2026-08-26 8:26 ` [PATCH 05/14] f2fs: support large folio writeback Nanzhe Zhao
` (9 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
ioctl can convert an inode with large folio support into an atomic
file. Support large folio buffered writes for atomic files as well.
Add a large folio atomic write_begin helper that reserves COW mappings
for the write range. For partial head and tail subpages, read the
existing data from either the COW inode or the original inode before
marking the subpage uptodate.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 121 insertions(+), 2 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 7ce96ae02cfc..dff802725544 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -4211,6 +4211,117 @@ static int prepare_large_folio_write_begin(struct inode *inode,
return 0;
}
+static int prepare_large_folio_atomic_write_begin(struct inode *inode,
+ struct address_space *mapping, struct folio *folio, loff_t pos,
+ unsigned int len)
+{
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ struct inode *cow_inode = F2FS_I(inode)->cow_inode;
+ size_t ori_off = offset_in_folio(folio, pos);
+ size_t need_off = ori_off;
+ pgoff_t index;
+ int err = 0;
+ unsigned int orig_order;
+ bool need_balance = false;
+
+ len = min_t(unsigned int, len, folio_size(folio) - ori_off);
+
+ f2fs_ffs_find_or_alloc(folio);
+
+ /* Inline data must have been converted before reaching here. */
+ f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
+
+ /* Reserve COW blocks for all covered 4K subpages first. */
+ pgoff_t start_index = folio->index + (ori_off >> PAGE_SHIFT);
+ pgoff_t end_index = folio->index +
+ ((ori_off + len - 1) >> PAGE_SHIFT);
+
+ for (index = start_index; index <= end_index; index++) {
+ block_t cow_blkaddr = NULL_ADDR;
+ bool node_changed = false;
+ int ret;
+
+ ret = __find_data_block(cow_inode, index, &cow_blkaddr);
+ if (ret)
+ return ret;
+ if (cow_blkaddr != NULL_ADDR)
+ continue;
+
+ ret = __reserve_data_block(cow_inode, index, &cow_blkaddr,
+ &node_changed);
+ if (ret)
+ return ret;
+
+ inc_atomic_write_cnt(inode);
+ need_balance |= node_changed;
+ }
+
+ if (need_balance && !IS_NOQUOTA(inode) &&
+ has_not_enough_free_secs(sbi, 0, 0)) {
+ orig_order = folio_order(folio);
+ folio_unlock(folio);
+ f2fs_balance_fs(sbi, true);
+ folio_lock(folio);
+ if (unlikely(folio->mapping != mapping ||
+ folio_order(folio) != orig_order))
+ return -EAGAIN;
+ }
+
+ if (folio_test_uptodate(folio) || len == folio_size(folio))
+ return 0;
+
+ /* Then read partial 4K subpages. */
+ while (find_next_valid_block(folio, ori_off, &need_off, len)) {
+ size_t off;
+ block_t cow_blkaddr = NULL_ADDR;
+ block_t ori_blkaddr = NULL_ADDR;
+ struct inode *read_inode = NULL;
+ block_t read_blkaddr = NULL_ADDR;
+
+ index = folio->index + (need_off >> PAGE_SHIFT);
+ off = offset_in_folio(folio, index << PAGE_SHIFT);
+
+ err = __find_data_block(cow_inode, index, &cow_blkaddr);
+ if (err)
+ return err;
+
+ if (__is_valid_data_blkaddr(cow_blkaddr)) {
+ if (!f2fs_is_valid_blkaddr(sbi, cow_blkaddr,
+ DATA_GENERIC_ENHANCE_READ))
+ return -EFSCORRUPTED;
+ read_inode = cow_inode;
+ read_blkaddr = cow_blkaddr;
+ } else if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) {
+ folio_zero_segment(folio, off, off + PAGE_SIZE);
+ f2fs_ffs_mark_subrange_uptodate(folio, off, PAGE_SIZE);
+ continue;
+ } else {
+ err = __find_data_block(inode, index, &ori_blkaddr);
+ if (err)
+ return err;
+
+ if (!__is_valid_data_blkaddr(ori_blkaddr)) {
+ folio_zero_segment(folio, off, off + PAGE_SIZE);
+ f2fs_ffs_mark_subrange_uptodate(folio, off, PAGE_SIZE);
+ continue;
+ }
+
+ if (!f2fs_is_valid_blkaddr(sbi, ori_blkaddr,
+ DATA_GENERIC_ENHANCE_READ))
+ return -EFSCORRUPTED;
+ read_inode = inode;
+ read_blkaddr = ori_blkaddr;
+ }
+
+ err = f2fs_submit_page_read_sync(read_inode, folio,
+ index, read_blkaddr);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
static int f2fs_write_begin(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned len, struct folio **foliop,
@@ -4282,7 +4393,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
*foliop = folio;
- if (f2fs_is_atomic_file(inode))
+ if (f2fs_is_atomic_file(inode) && !folio_test_large(folio))
err = prepare_atomic_write_begin(sbi, folio, pos, len,
&blkaddr, &need_balance);
else if (!folio_test_large(folio))
@@ -4307,10 +4418,18 @@ static int f2fs_write_begin(const struct kiocb *iocb,
f2fs_folio_wait_writeback(folio, DATA, false, true);
if (folio_test_large(folio)) {
- err = prepare_large_folio_write_begin(inode,
+ if (f2fs_is_atomic_file(inode))
+ err = prepare_large_folio_atomic_write_begin(inode,
+ mapping, folio, pos, len);
+ else
+ err = prepare_large_folio_write_begin(inode,
folio, pos, len);
if (!err)
return 0;
+ if (err == -EAGAIN) {
+ f2fs_folio_put(folio, true);
+ goto repeat;
+ }
goto put_folio;
}
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 05/14] f2fs: support large folio writeback
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (3 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 04/14] f2fs: support atomic file large folios buffered write Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-27 11:17 ` Chao Yu
2026-08-26 8:26 ` [PATCH 06/14] f2fs: prepare mmap write faults for large folios Nanzhe Zhao
` (8 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Large folio can contain multiple dirty ranges.
Add a folio-based writeback path for large-folio mapping files
and keep the legacy f2fs_write_cache_pages() path unchanged for
non large-folio mapping files.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 398 insertions(+), 6 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index dff802725544..608d8ea8e607 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -358,6 +358,7 @@ static void f2fs_write_end_bio(struct bio *bio)
struct folio *folio = fi.folio;
unsigned int nr_pages = fi.length >> PAGE_SHIFT;
enum count_type type;
+ bool finished = true;
if (fscrypt_is_bounce_folio(folio)) {
struct folio *io_folio = folio;
@@ -388,11 +389,20 @@ static void f2fs_write_end_bio(struct bio *bio)
folio->index, NODE_TYPE_REGULAR, true);
f2fs_bug_on(sbi, folio->index != nid_of_node(folio));
}
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs =
+ (struct f2fs_folio_state *)folio->private;
+
+ finished = atomic_sub_and_test(nr_pages,
+ &ffs->write_pages_pending);
+ }
+
+ while (nr_pages--)
+ dec_page_count(sbi, type);
+
if (f2fs_in_warm_node_list(folio))
f2fs_del_fsync_node_entry(sbi, folio);
- dec_page_count(sbi, type);
-
/*
* we should access sbi before folio_end_writeback() to
* avoid racing w/ kill_f2fs_super()
@@ -401,8 +411,10 @@ static void f2fs_write_end_bio(struct bio *bio)
wq_has_sleeper(&sbi->cp_wait))
wake_up(&sbi->cp_wait);
- folio_clear_f2fs_gcing(folio);
- folio_end_writeback(folio);
+ if (finished) {
+ folio_clear_f2fs_gcing(folio);
+ folio_end_writeback(folio);
+ }
}
bio_put(bio);
@@ -2669,7 +2681,7 @@ static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
folio_mark_uptodate(folio);
}
-static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
+void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
size_t offset, size_t len)
{
struct f2fs_folio_state *ffs;
@@ -2692,6 +2704,86 @@ static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
spin_unlock_irqrestore(&ffs->state_lock, flags);
}
+static bool __ffs_clear_subrange_dirty(struct folio *folio,
+ struct f2fs_folio_state *ffs, size_t offset, size_t len)
+{
+ unsigned int nr_subpages = folio_nr_pages(folio);
+ unsigned int start, end;
+
+ start = offset >> PAGE_SHIFT;
+ end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ end = min(end, nr_subpages);
+
+ bitmap_clear(ffs->state, nr_subpages + start, end - start);
+ return find_next_bit(ffs->state, 2 * nr_subpages, nr_subpages) <
+ 2 * nr_subpages;
+}
+
+void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned long flags;
+
+ if (!f2fs_folio_has_ffs(folio))
+ return;
+
+ ffs = (struct f2fs_folio_state *)folio->private;
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ __ffs_clear_subrange_dirty(folio, ffs, offset, len);
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+}
+
+static unsigned int ffs_next_dirty_subpage(struct f2fs_folio_state *ffs,
+ const struct folio *folio, unsigned int start,
+ unsigned int end)
+{
+ unsigned int nr_subpages = folio_nr_pages(folio);
+
+ return find_next_bit(ffs->state, nr_subpages + end + 1,
+ nr_subpages + start) - nr_subpages;
+}
+
+static unsigned int ffs_next_clean_subpage(struct f2fs_folio_state *ffs,
+ const struct folio *folio, unsigned int start,
+ unsigned int end)
+{
+ unsigned int nr_subpages = folio_nr_pages(folio);
+
+ return find_next_zero_bit(ffs->state, nr_subpages + end + 1,
+ nr_subpages + start) - nr_subpages;
+}
+
+static unsigned int ffs_find_dirty_range(struct folio *folio,
+ u64 *range_start, u64 range_end)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int start, end, nr_pages;
+
+ if (*range_start >= range_end)
+ return 0;
+
+ if (!f2fs_folio_has_ffs(folio))
+ return range_end - *range_start;
+
+ ffs = (struct f2fs_folio_state *)folio->private;
+ start = offset_in_folio(folio, *range_start) >> PAGE_SHIFT;
+ end = DIV_ROUND_UP(min_not_zero(offset_in_folio(folio, range_end),
+ folio_size(folio)), PAGE_SIZE) - 1;
+
+ start = ffs_next_dirty_subpage(ffs, folio, start, end);
+ if (start > end)
+ return 0;
+
+ if (start == end)
+ nr_pages = 1;
+ else
+ nr_pages = ffs_next_clean_subpage(ffs, folio,
+ start + 1, end) - start;
+
+ *range_start = folio_pos(folio) + ((u64)start << PAGE_SHIFT);
+ return (u64)nr_pages << PAGE_SHIFT;
+}
+
static bool find_next_valid_block(const struct folio *folio,
size_t orig_off, size_t *need_off,
size_t len)
@@ -3309,6 +3401,139 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
return err;
}
+static int f2fs_write_single_data_folio(struct folio *folio, int *submitted,
+ struct writeback_control *wbc,
+ enum iostat_type io_type,
+ u64 start, u64 end)
+{
+ struct inode *inode = folio->mapping->host;
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ bool atomic_commit = f2fs_is_atomic_file(inode) &&
+ folio_test_f2fs_atomic(folio);
+ struct inode *dn_inode = atomic_commit ?
+ F2FS_I(inode)->cow_inode : inode;
+ u64 pos = folio_pos(folio);
+ pgoff_t start_idx = (start - pos) >> PAGE_SHIFT;
+ pgoff_t end_idx = (end - 1 - pos) >> PAGE_SHIFT;
+ int local_submitted = 0;
+ int err = 0;
+
+ for (pgoff_t i = start_idx; i <= end_idx; i++) {
+ struct dnode_of_data dn;
+ struct node_info ni;
+ pgoff_t data_idx = folio->index + i;
+ bool ipu_force = false;
+ struct f2fs_io_info fio = {
+ .sbi = sbi,
+ .ino = inode->i_ino,
+ .type = DATA,
+ .op = REQ_OP_WRITE,
+ .op_flags = wbc_to_write_flags(wbc),
+ .old_blkaddr = NULL_ADDR,
+ .folio = folio,
+ .folio_offset = i,
+ .folio_blkcnt = 1,
+ .encrypted_page = NULL,
+ .submitted = 0,
+ .need_lock = LOCK_DONE,
+ .meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0,
+ .io_type = io_type,
+ .io_wbc = wbc,
+ };
+
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs =
+ (struct f2fs_folio_state *)folio->private;
+
+ atomic_inc(&ffs->write_pages_pending);
+ }
+
+ set_new_dnode(&dn, dn_inode, NULL, NULL, 0);
+
+ if (!atomic_commit && need_inplace_update(&fio) &&
+ f2fs_lookup_read_extent_cache_block(inode, data_idx,
+ &fio.old_blkaddr)) {
+ if (!f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
+ DATA_GENERIC_ENHANCE)) {
+ err = -EFSCORRUPTED;
+ goto rollback;
+ }
+ ipu_force = true;
+ goto got_it;
+ }
+
+ err = f2fs_get_dnode_of_data(&dn, data_idx, LOOKUP_NODE);
+ if (err)
+ goto rollback;
+
+ fio.old_blkaddr = dn.data_blkaddr;
+
+got_it:
+ if (__is_valid_data_blkaddr(fio.old_blkaddr) &&
+ !f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
+ DATA_GENERIC_ENHANCE)) {
+ err = -EFSCORRUPTED;
+ goto rollback;
+ }
+
+ if (fio.meta_gc)
+ f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
+
+ if (!atomic_commit && (ipu_force ||
+ (__is_valid_data_blkaddr(fio.old_blkaddr) &&
+ need_inplace_update(&fio)))) {
+ err = f2fs_encrypt_one_page(&fio);
+ if (err)
+ goto rollback;
+
+ f2fs_put_dnode(&dn);
+ err = f2fs_inplace_write_data(&fio);
+ if (err) {
+ if (fscrypt_inode_uses_fs_layer_crypto(inode))
+ fscrypt_finalize_bounce_page(
+ &fio.encrypted_page);
+ goto rollback_no_dnode;
+ }
+
+ local_submitted++;
+ set_inode_flag(inode, FI_UPDATE_WRITE);
+ continue;
+ }
+
+ err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
+ if (err)
+ goto rollback;
+
+ fio.version = ni.version;
+
+ err = f2fs_encrypt_one_page(&fio);
+ if (err)
+ goto rollback;
+
+ f2fs_outplace_write_data(&dn, &fio);
+ local_submitted++;
+ set_inode_flag(inode, FI_APPEND_WRITE);
+ trace_f2fs_do_write_data_page(folio, OPU);
+ f2fs_put_dnode(&dn);
+ continue;
+
+rollback:
+ f2fs_put_dnode(&dn);
+rollback_no_dnode:
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs =
+ (struct f2fs_folio_state *)folio->private;
+
+ atomic_dec(&ffs->write_pages_pending);
+ }
+ break;
+ }
+
+ if (submitted)
+ *submitted = local_submitted;
+ return err;
+}
+
int f2fs_write_single_data_page(struct folio *folio, int *submitted,
struct bio **bio,
sector_t *last_block,
@@ -3757,6 +3982,170 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
return ret;
}
+static int f2fs_write_cache_folios(struct address_space *mapping,
+ struct writeback_control *wbc,
+ enum iostat_type io_type)
+{
+ struct folio *folio = NULL;
+ struct inode *inode = mapping->host;
+ struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+ struct f2fs_lock_context lc;
+ u64 pos = 0;
+ u64 end_pos = 0;
+ u32 r_len = 0;
+ int err = 0;
+ int submitted = 0;
+ int nwritten = 0;
+ bool op_locked = false;
+ bool next = false;
+ bool retry = false;
+
+ if (get_dirty_pages(inode) <= SM_I(sbi)->min_hot_blocks)
+ set_inode_flag(inode, FI_HOT_DATA);
+ else
+ clear_inode_flag(inode, FI_HOT_DATA);
+
+ while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
+ struct f2fs_folio_state *ffs = NULL;
+ u64 isize;
+ size_t poff;
+ pgoff_t end_index;
+ bool verity_in_progress;
+ int folio_submitted = 0;
+ bool bias_added = false;
+
+ submitted = 0;
+ next = true;
+ retry = false;
+
+ if (atomic_read(&sbi->wb_sync_req[DATA]) &&
+ wbc->sync_mode == WB_SYNC_NONE) {
+ folio_redirty_for_writepage(wbc, folio);
+ next = false;
+ goto retry_out;
+ }
+retry:
+ pos = folio_pos(folio);
+ end_pos = pos + folio_size(folio);
+ isize = i_size_read(inode);
+ verity_in_progress = f2fs_verity_in_progress(inode);
+ poff = 0;
+ end_index = 0;
+
+ if (retry) {
+ if (unlikely(folio->mapping != mapping))
+ goto retry_out;
+
+ if (!folio_test_dirty(folio))
+ goto retry_out;
+
+ if (folio_test_writeback(folio)) {
+ if (wbc->sync_mode == WB_SYNC_NONE)
+ goto retry_out;
+ f2fs_folio_wait_writeback(folio, DATA, true, true);
+ }
+
+ if (!folio_clear_dirty_for_io(folio))
+ goto retry_out;
+ }
+
+ /* To avoid dealing with the complexity for one subrange is in bio
+ * while we trylock_op failed before writing another subrange.
+ * Try to lock_op before any subrange write for the folio.
+ */
+ if (!op_locked) {
+ if (!f2fs_trylock_op(sbi, &lc)) {
+ folio_redirty_for_writepage(wbc, folio);
+ err = 0;
+ if (wbc->sync_mode != WB_SYNC_ALL)
+ goto retry_out;
+
+ retry = true;
+ folio_unlock(folio);
+ f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
+ folio_lock(folio);
+ goto retry;
+ }
+ op_locked = true;
+ }
+
+ if (!verity_in_progress) {
+ poff = offset_in_folio(folio, isize);
+ end_index = isize >> PAGE_SHIFT;
+
+ if (folio->index > end_index ||
+ (folio->index == end_index && poff == 0))
+ goto out;
+
+ if (end_pos > isize) {
+ folio_zero_segment(folio, poff, folio_size(folio));
+ end_pos = isize;
+ }
+ }
+
+ folio_start_writeback(folio);
+
+ if (folio_test_large(folio)) {
+ if (!f2fs_folio_has_ffs(folio)) {
+ ffs = f2fs_ffs_find_or_alloc(folio);
+ f2fs_ffs_mark_subrange_dirty(folio, 0, end_pos - pos);
+ } else {
+ ffs = (struct f2fs_folio_state *)folio->private;
+ }
+ if (f2fs_folio_has_ffs(folio) && !bias_added) {
+ WARN_ON_ONCE(atomic_read(&ffs->write_pages_pending) != 0);
+ atomic_inc(&ffs->write_pages_pending);
+ bias_added = true;
+ }
+ }
+
+ while ((r_len = ffs_find_dirty_range(folio, &pos, end_pos))) {
+ err = f2fs_write_single_data_folio(folio, &submitted,
+ wbc, io_type, pos, pos + r_len);
+ folio_submitted += submitted;
+ if (err)
+ goto out;
+
+ nwritten += submitted;
+ pos += r_len;
+ }
+
+ if (!err && folio_submitted &&
+ f2fs_is_atomic_file(inode) &&
+ folio_test_f2fs_atomic(folio))
+ folio_clear_f2fs_atomic(folio);
+
+out:
+ f2fs_ffs_clear_subrange_dirty(folio, 0, folio_size(folio));
+ inode_dec_dirty_pages(inode);
+
+ if (bias_added) {
+ if (atomic_dec_and_test(&ffs->write_pages_pending))
+ folio_end_writeback(folio);
+ } else if (!folio_submitted && folio_test_writeback(folio)) {
+ folio_end_writeback(folio);
+ }
+
+retry_out:
+ if (folio_test_locked(folio))
+ folio_unlock(folio);
+
+ if (op_locked) {
+ f2fs_unlock_op(sbi, &lc);
+ op_locked = false;
+ }
+
+ if (err || !next)
+ break;
+ }
+
+ if (nwritten)
+ f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
+ NULL, 0, DATA);
+
+ return err;
+}
+
static inline bool __should_serialize_io(struct inode *inode,
struct writeback_control *wbc)
{
@@ -3851,7 +4240,10 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
account_writeback(inode, true);
blk_start_plug(&plug);
- ret = f2fs_write_cache_pages(mapping, wbc, io_type);
+ if (mapping_large_folio_support(inode->i_mapping))
+ ret = f2fs_write_cache_folios(mapping, wbc, io_type);
+ else
+ ret = f2fs_write_cache_pages(mapping, wbc, io_type);
blk_finish_plug(&plug);
account_writeback(inode, false);
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 06/14] f2fs: prepare mmap write faults for large folios
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (4 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 05/14] f2fs: support large folio writeback Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-27 12:36 ` Chao Yu
2026-08-26 8:26 ` [PATCH 07/14] f2fs: make GC migration large-folio aware Nanzhe Zhao
` (7 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Now write protect `mmap` also need to support large folio,
Change `f2fs_vm_page_mkwrite` to acheive that.
Note it currently marks the whole large folio dirty
to avoid data loss which causes write amplification.
Further optimization is welcome.
PG_mappedtodisk is useless in f2fs, so drop the
folio_test_mappedtodisk() check and its goto out_sem
shortcut in f2fs_vm_page_mkwrite(). We extend the
folio_zero_segment() in mkwrite to zero the post-EOF part
of the faulted folio for both order-0 and large folios, so
the f2fs_zero_post_eof_page() call added to cover that
shortcut is no longer needed.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/f2fs.h | 5 +++++
fs/f2fs/file.c | 59 +++++++++++++++++++++++++++++++-------------------
2 files changed, 42 insertions(+), 22 deletions(-)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index ae6031693700..71e6d7e34c7b 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4343,6 +4343,11 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
enum iostat_type io_type,
int compr_blocks, bool allow_balance);
bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
+struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio);
+void f2fs_ffs_mark_subrange_dirty(struct folio *folio, size_t offset, size_t len);
+bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,
+ size_t len);
+void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len);
void f2fs_write_failed(struct inode *inode, loff_t to);
void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
bool f2fs_release_folio(struct folio *folio, gfp_t wait);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index e40285d4f9c2..02d687527241 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -124,6 +124,13 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct dnode_of_data dn;
bool need_alloc = !f2fs_is_pinned_file(inode);
+ pgoff_t pidx = folio->index + folio_page_idx(folio, vmf->page);
+ loff_t pos = (loff_t)pidx << PAGE_SHIFT;
+ loff_t isize;
+ loff_t folio_start;
+ loff_t valid_end;
+ size_t dirty_len;
+ size_t subpage_off;
int err = 0;
vm_fault_t ret;
@@ -160,7 +167,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
#ifdef CONFIG_F2FS_FS_COMPRESSION
if (f2fs_compressed_file(inode)) {
- int ret = f2fs_is_compressed_cluster(inode, folio->index);
+ int ret = f2fs_is_compressed_cluster(inode, pidx);
if (ret < 0) {
err = ret;
@@ -178,18 +185,20 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
- err = f2fs_zero_post_eof_page(inode,
- (folio->index + 1) << PAGE_SHIFT, true, false);
- if (err)
- goto out_pagefault;
-
file_update_time(vmf->vma->vm_file);
filemap_invalidate_lock_shared(inode->i_mapping);
folio_lock(folio);
+ isize = i_size_read(inode);
+ folio_start = folio_pos(folio);
+ subpage_off = offset_in_folio(folio, pos);
+ valid_end = min_t(loff_t, folio_start + folio_size(folio), isize);
+ dirty_len = valid_end > folio_start ? valid_end - folio_start : 0;
+
if (unlikely(folio->mapping != inode->i_mapping ||
- folio_pos(folio) > i_size_read(inode) ||
- !folio_test_uptodate(folio))) {
+ pos >= isize ||
+ !f2fs_ffs_test_blk_uptodate(folio,
+ folio->index + (subpage_off >> PAGE_SHIFT)))) {
folio_unlock(folio);
err = -EFAULT;
goto out_sem;
@@ -198,9 +207,19 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
set_new_dnode(&dn, inode, NULL, NULL, 0);
if (need_alloc) {
/* block allocation */
- err = f2fs_get_block_locked(&dn, folio->index);
+ if (folio_test_large(folio)) {
+ pgoff_t i, nr = DIV_ROUND_UP(dirty_len, PAGE_SIZE);
+
+ for (i = 0; i < nr; i++) {
+ err = f2fs_get_block_locked(&dn, folio->index + i);
+ if (err)
+ break;
+ }
+ } else {
+ err = f2fs_get_block_locked(&dn, pidx);
+ }
} else {
- err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
+ err = f2fs_get_dnode_of_data(&dn, pidx, LOOKUP_NODE);
f2fs_put_dnode(&dn);
if (f2fs_is_pinned_file(inode) &&
!__is_valid_data_blkaddr(dn.data_blkaddr))
@@ -217,20 +236,17 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
/* wait for GCed page writeback via META_MAPPING */
f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
- /*
- * check to see if the page is mapped already (no holes)
- */
- if (folio_test_mappedtodisk(folio))
- goto out_sem;
-
/* page is wholly or partially inside EOF */
- if (((loff_t)(folio->index + 1) << PAGE_SHIFT) >
- i_size_read(inode)) {
- loff_t offset;
+ if (folio_start + folio_size(folio) > isize) {
+ size_t offset = offset_in_folio(folio, isize);
- offset = i_size_read(inode) & ~PAGE_MASK;
folio_zero_segment(folio, offset, folio_size(folio));
}
+
+ if (folio_test_large(folio)) {
+ f2fs_ffs_find_or_alloc(folio);
+ f2fs_ffs_mark_subrange_dirty(folio, 0, dirty_len);
+ }
folio_mark_dirty(folio);
f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
@@ -238,12 +254,11 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
out_sem:
filemap_invalidate_unlock_shared(inode->i_mapping);
-out_pagefault:
sb_end_pagefault(inode->i_sb);
out:
ret = vmf_fs_error(err);
- trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
+ trace_f2fs_vm_page_mkwrite(inode, pidx, vmf->vma->vm_flags, ret);
return ret;
}
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 07/14] f2fs: make GC migration large-folio aware
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (5 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 06/14] f2fs: prepare mmap write faults for large folios Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-28 17:20 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 08/14] f2fs: optimize small block size large folio read Nanzhe Zhao
` (6 subsequent siblings)
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
GC can operate on a 4K block that is cached inside a large folio.
The data lookup helpers therefore need to test and update uptodate
state for the addressed subpage instead of rejecting large folios or
treating the whole folio as the target block.
Let f2fs_get_read_data_folio(), f2fs_find_data_folio(), and
f2fs_get_lock_data_folio() to use subpage uptodate state. Submit
single-block reads at the requested folio offset and zero only the
addressed 4K range for NEW_ADDR.
Also update `move_data_page` to mark, clear, and restore dirty
state for the target subpage, and submit write I/O with the subpage
offset recorded in f2fs_io_info.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 96 ++++++++++++++++++++++++++++++++++----------------
fs/f2fs/f2fs.h | 6 ++--
fs/f2fs/gc.c | 30 ++++++++++++++--
3 files changed, 97 insertions(+), 35 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 608d8ea8e607..0e54b1e25893 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -1241,19 +1241,31 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode,
/* This can handle encryption stuffs */
static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
- struct folio *folio, block_t blkaddr,
- blk_opf_t op_flags, bool for_write)
+ struct folio *folio, pgoff_t index,
+ block_t blkaddr, blk_opf_t op_flags,
+ bool for_write)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct bio *bio;
+ size_t offset = 0;
- bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, folio->index,
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs = folio->private;
+ unsigned long flags;
+
+ offset = offset_in_folio(folio, (loff_t)index << PAGE_SHIFT);
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ ffs->read_pages_pending++;
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+ }
+
+ bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, index,
for_write);
/* wait for GCed page writeback via META_MAPPING */
f2fs_wait_on_block_writeback(inode, blkaddr);
- if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
+ if (!bio_add_folio(bio, folio, PAGE_SIZE, offset))
f2fs_bug_on(sbi, 1);
inc_page_count(sbi, F2FS_RD_DATA);
@@ -1399,21 +1411,13 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
struct dnode_of_data dn;
struct folio *folio;
int err;
-retry:
+
folio = f2fs_grab_cache_folio(mapping, index, for_write);
if (IS_ERR(folio))
return folio;
- if (folio_test_large(folio)) {
- pgoff_t folio_index = mapping_align_index(mapping, index);
- unsigned long nr_pages = folio_nr_pages(folio);
-
- f2fs_folio_put(folio, true);
- invalidate_inode_pages2_range(mapping, folio_index,
- folio_index + nr_pages - 1);
- f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
- goto retry;
- }
+ if (folio_test_large(folio))
+ f2fs_ffs_find_or_alloc(folio);
if (f2fs_lookup_read_extent_cache_block(inode, index,
&dn.data_blkaddr)) {
@@ -1448,7 +1452,7 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
goto put_err;
}
got_it:
- if (folio_test_uptodate(folio)) {
+ if (f2fs_ffs_test_blk_uptodate(folio, index)) {
folio_unlock(folio);
return folio;
}
@@ -1461,15 +1465,17 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
* f2fs_init_inode_metadata.
*/
if (dn.data_blkaddr == NEW_ADDR) {
- folio_zero_segment(folio, 0, folio_size(folio));
- if (!folio_test_uptodate(folio))
- folio_mark_uptodate(folio);
+ size_t offset = offset_in_folio(folio,
+ (loff_t)index << PAGE_SHIFT);
+
+ folio_zero_segment(folio, offset, offset + PAGE_SIZE);
+ f2fs_ffs_mark_subrange_uptodate(folio, offset, PAGE_SIZE);
folio_unlock(folio);
return folio;
}
- f2fs_submit_page_read(inode, f2fs_need_verity(inode, folio->index),
- folio, dn.data_blkaddr, op_flags, for_write);
+ f2fs_submit_page_read(inode, f2fs_need_verity(inode, index),
+ folio, index, dn.data_blkaddr, op_flags, for_write);
return folio;
put_err:
@@ -1486,7 +1492,7 @@ struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,
folio = f2fs_filemap_get_folio(mapping, index, FGP_ACCESSED, 0);
if (IS_ERR(folio))
goto read;
- if (folio_test_uptodate(folio))
+ if (f2fs_ffs_test_blk_uptodate(folio, index))
return folio;
f2fs_folio_put(folio, false);
@@ -1495,11 +1501,11 @@ struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,
if (IS_ERR(folio))
return folio;
- if (folio_test_uptodate(folio))
+ if (f2fs_ffs_test_blk_uptodate(folio, index))
return folio;
folio_wait_locked(folio);
- if (unlikely(!folio_test_uptodate(folio))) {
+ if (unlikely(!f2fs_ffs_test_blk_uptodate(folio, index))) {
f2fs_folio_put(folio, false);
return ERR_PTR(-EIO);
}
@@ -1523,7 +1529,8 @@ struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,
/* wait for read completion */
folio_lock(folio);
- if (unlikely(folio->mapping != mapping || !folio_test_uptodate(folio))) {
+ if (unlikely(folio->mapping != mapping ||
+ !f2fs_ffs_test_blk_uptodate(folio, index))) {
f2fs_folio_put(folio, true);
return ERR_PTR(-EIO);
}
@@ -2681,6 +2688,23 @@ static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
folio_mark_uptodate(folio);
}
+bool f2fs_ffs_test_blk_dirty(const struct folio *folio, pgoff_t index)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int idx, nr_subpages;
+
+ if (!f2fs_folio_has_ffs(folio))
+ return folio_test_dirty(folio);
+
+ if (!folio_contains(folio, index))
+ return false;
+
+ ffs = folio->private;
+ nr_subpages = folio_nr_pages(folio);
+ idx = index - folio->index;
+ return test_bit(nr_subpages + idx, ffs->state);
+}
+
void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
size_t offset, size_t len)
{
@@ -2719,18 +2743,30 @@ static bool __ffs_clear_subrange_dirty(struct folio *folio,
2 * nr_subpages;
}
-void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len)
+/**
+ * f2fs_ffs_clear_subrange_dirty - clear the dirty bits of a folio subrange
+ * @folio: the large folio
+ * @offset: start byte offset within the folio
+ * @len: byte length of the subrange
+ *
+ * Clear the dirty bits of the 4K subpages covered by [offset, offset + len),
+ * and return whether the folio still has any dirty subpage left.
+ */
+bool f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset,
+ size_t len)
{
struct f2fs_folio_state *ffs;
unsigned long flags;
+ bool dirty;
if (!f2fs_folio_has_ffs(folio))
- return;
+ return false;
- ffs = (struct f2fs_folio_state *)folio->private;
+ ffs = folio->private;
spin_lock_irqsave(&ffs->state_lock, flags);
- __ffs_clear_subrange_dirty(folio, ffs, offset, len);
+ dirty = __ffs_clear_subrange_dirty(folio, ffs, offset, len);
spin_unlock_irqrestore(&ffs->state_lock, flags);
+ return dirty;
}
static unsigned int ffs_next_dirty_subpage(struct f2fs_folio_state *ffs,
@@ -4853,7 +4889,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
*/
f2fs_submit_page_read(inode,
NULL, /* can't write to fsverity files */
- folio, blkaddr, 0, true);
+ folio, index, blkaddr, 0, true);
folio_lock(folio);
if (unlikely(folio->mapping != mapping)) {
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 71e6d7e34c7b..0ffbe2bd04c8 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4344,10 +4344,10 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
int compr_blocks, bool allow_balance);
bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio);
+bool f2fs_ffs_test_blk_dirty(const struct folio *folio, pgoff_t index);
void f2fs_ffs_mark_subrange_dirty(struct folio *folio, size_t offset, size_t len);
-bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,
- size_t len);
-void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len);
+bool f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset,
+ size_t len);
void f2fs_write_failed(struct inode *inode, loff_t to);
void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
bool f2fs_release_folio(struct folio *folio, gfp_t wait);
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index 0c17038fcfd7..42e6dfcbf98d 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1504,12 +1504,19 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
unsigned int segno, int off)
{
struct folio *folio;
+ size_t foff = 0;
+ bool large = false;
int err = 0;
folio = f2fs_get_lock_data_folio(inode, bidx, true);
if (IS_ERR(folio))
return PTR_ERR(folio);
+ if (f2fs_folio_has_ffs(folio)) {
+ large = true;
+ foff = offset_in_folio(folio, (loff_t)bidx << PAGE_SHIFT);
+ }
+
if (!check_valid_map(F2FS_I_SB(inode), segno, off)) {
err = -ENOENT;
goto out;
@@ -1524,6 +1531,8 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
err = -EAGAIN;
goto out;
}
+ if (large)
+ f2fs_ffs_mark_subrange_dirty(folio, foff, PAGE_SIZE);
folio_mark_dirty(folio);
folio_set_f2fs_gcing(folio);
} else {
@@ -1536,32 +1545,49 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
.op_flags = REQ_SYNC,
.old_blkaddr = NULL_ADDR,
.folio = folio,
+ .folio_offset = bidx - folio->index,
+ .folio_blkcnt = 1,
.encrypted_page = NULL,
.need_lock = LOCK_REQ,
.io_type = FS_GC_DATA_IO,
};
- bool is_dirty = folio_test_dirty(folio);
+ struct f2fs_folio_state *ffs = NULL;
+ bool is_dirty = f2fs_ffs_test_blk_dirty(folio, bidx);
retry:
f2fs_folio_wait_writeback(folio, DATA, true, true);
+ if (large) {
+ ffs = folio->private;
+ f2fs_ffs_mark_subrange_dirty(folio, foff, PAGE_SIZE);
+ }
folio_mark_dirty(folio);
if (folio_clear_dirty_for_io(folio)) {
inode_dec_dirty_pages(inode);
f2fs_remove_dirty_inode(inode);
+ if (large &&
+ f2fs_ffs_clear_subrange_dirty(folio, foff, PAGE_SIZE))
+ folio_mark_dirty(folio);
}
+ if (large)
+ atomic_inc(&ffs->write_pages_pending);
folio_set_f2fs_gcing(folio);
err = f2fs_do_write_data_page(&fio);
if (err) {
folio_clear_f2fs_gcing(folio);
+ if (large)
+ atomic_dec(&ffs->write_pages_pending);
if (err == -ENOMEM) {
memalloc_retry_wait(GFP_NOFS);
goto retry;
}
- if (is_dirty)
+ if (is_dirty) {
+ if (large)
+ f2fs_ffs_mark_subrange_dirty(folio, foff, PAGE_SIZE);
folio_mark_dirty(folio);
+ }
}
}
out:
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 08/14] f2fs: optimize small block size large folio read
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (6 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 07/14] f2fs: make GC migration large-folio aware Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 09/14] f2fs: support partial uptodate " Nanzhe Zhao
` (5 subsequent siblings)
13 siblings, 0 replies; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
The original f2fs_read_data_large_folio() implementation has limited
benefit with a 4KB block size, mainly because updating
read_pages_pending greatly increases the number of spinlock
operations.
Use len_blks to batch read_pages_pending and iostat updates for
contiguous mapped blocks. If the contiguous mapping covers the whole
folio, skip f2fs_folio_state allocation for that folio.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 62 ++++++++++++++++++++++++++++++++++++++------------
1 file changed, 47 insertions(+), 15 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 0e54b1e25893..48c1bb6c02e3 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -153,6 +153,7 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
struct folio *folio = fi.folio;
unsigned int nr_pages = fi.length >> PAGE_SHIFT;
bool finished = true;
+ bool uptodate = bio->bi_status == BLK_STS_OK;
if (!folio_test_large(folio) &&
f2fs_is_compressed_page(folio)) {
@@ -163,10 +164,14 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
continue;
}
- if (folio_test_large(folio)) {
- struct f2fs_folio_state *ffs = folio->private;
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs =
+ (struct f2fs_folio_state *)folio->private;
spin_lock_irqsave(&ffs->state_lock, flags);
+ if (bio->bi_status == BLK_STS_OK)
+ uptodate = __ffs_mark_subrange_uptodate(folio, ffs,
+ fi.offset, fi.length);
ffs->read_pages_pending -= nr_pages;
finished = !ffs->read_pages_pending;
spin_unlock_irqrestore(&ffs->state_lock, flags);
@@ -182,7 +187,7 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
bio->bi_status = BLK_STS_IOERR;
if (finished)
- folio_end_read(folio, bio->bi_status == BLK_STS_OK);
+ folio_end_read(folio, uptodate);
}
if (ctx)
@@ -2887,8 +2892,15 @@ static int f2fs_read_data_large_folio(struct inode *inode,
ffs = NULL;
nrpages = folio_nr_pages(folio);
- for (; nrpages; nrpages--, max_nr_pages--, index++, offset++) {
+ for (; nrpages;
+ nrpages -= len_blks, max_nr_pages -= len_blks,
+ index += len_blks, offset += len_blks) {
sector_t block_nr;
+ bool whole_folio_in_bio;
+ unsigned int i;
+
+ len_blks = 1;
+
/*
* Map blocks using the previous result first.
*/
@@ -2917,13 +2929,31 @@ static int f2fs_read_data_large_folio(struct inode *inode,
got_it:
if ((map.m_flags & F2FS_MAP_MAPPED)) {
block_nr = map.m_pblk + index - map.m_lblk;
- if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
+
+ len_blks = min_t(unsigned int, nrpages, max_nr_pages);
+ len_blks = min_t(unsigned int, len_blks,
+ (unsigned int)(map.m_lblk + map.m_len - index));
+
+ for (i = 0; i < len_blks; i++) {
+ if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
+ block_nr + i,
DATA_GENERIC_ENHANCE_READ)) {
- ret = -EFSCORRUPTED;
- goto err_out;
+ ret = -EFSCORRUPTED;
+ goto err_out;
+ }
}
+
+ /*
+ * If an entire folio is added to one bio,
+ * folio_end_read() can complete the folio read status
+ * without relying on f2fs_folio_state.
+ */
+ whole_folio_in_bio = offset == 0 &&
+ len_blks == folio_nr_pages(folio);
+
} else {
size_t page_offset = offset << PAGE_SHIFT;
+
folio_zero_range(folio, page_offset, PAGE_SIZE);
if (vi && !fsverity_verify_blocks(vi, folio, PAGE_SIZE, page_offset)) {
ret = -EIO;
@@ -2933,14 +2963,14 @@ static int f2fs_read_data_large_folio(struct inode *inode,
}
/* We must increment read_pages_pending before possible BIOs submitting
- * to prevent from premature folio_end_read() call on folio
+ * to prevent from premature folio_end_read() call on folio.
*/
- if (folio_test_large(folio)) {
+ if (folio_test_large(folio) && !whole_folio_in_bio) {
ffs = f2fs_ffs_find_or_alloc(folio);
/* set the bitmap to wait */
spin_lock_irq(&ffs->state_lock);
- ffs->read_pages_pending++;
+ ffs->read_pages_pending += len_blks;
spin_unlock_irq(&ffs->state_lock);
}
@@ -2965,17 +2995,19 @@ static int f2fs_read_data_large_folio(struct inode *inode,
* If the page is under writeback, we need to wait for
* its completion to see the correct decrypted data.
*/
- f2fs_wait_on_block_writeback(inode, block_nr);
+ for (i = 0; i < len_blks; i++)
+ f2fs_wait_on_block_writeback(inode, block_nr + i);
- if (!bio_add_folio(bio, folio, F2FS_BLKSIZE,
+ if (!bio_add_folio(bio, folio, len_blks * F2FS_BLKSIZE,
offset << PAGE_SHIFT))
goto submit_and_realloc;
folio_in_bio = true;
- inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
+ for (i = 0; i < len_blks; i++)
+ inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO,
- F2FS_BLKSIZE);
- last_block_in_bio = block_nr;
+ len_blks * F2FS_BLKSIZE);
+ last_block_in_bio = block_nr + len_blks - 1;
}
trace_f2fs_read_folio(folio, DATA);
err_out:
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 09/14] f2fs: support partial uptodate large folio read
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (7 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 08/14] f2fs: optimize small block size large folio read Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 10/14] f2fs: handle partial truncate of large folio dirty subpages Nanzhe Zhao
` (4 subsequent siblings)
13 siblings, 0 replies; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Buffered write can have write bytes smaller than folio
size for cases when folio minimum order is not zero.
This can left partially uptodate folio in page cache.
So we skip uptodate subpage read in read_data_large_folio.
Also mark hole subpage uptodate in uptodate bitmap.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 48c1bb6c02e3..59b07b83b216 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -2855,6 +2855,56 @@ static bool find_next_valid_block(const struct folio *folio,
return false;
}
+static unsigned int ffs_next_uptodate_subpage(struct f2fs_folio_state *ffs,
+ unsigned int start, unsigned int end)
+{
+ return find_next_bit(ffs->state, end + 1, start);
+}
+
+static unsigned int ffs_next_nonuptodate_subpage(struct f2fs_folio_state *ffs,
+ unsigned int start, unsigned int end)
+{
+ return find_next_zero_bit(ffs->state, end + 1, start);
+}
+
+static void f2fs_skip_fully_uptodate_front(struct folio *folio,
+ pgoff_t *index, pgoff_t *offset, unsigned int *nrpages,
+ unsigned int *max_nr_pages)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int next, skipped;
+
+ if (!f2fs_folio_has_ffs(folio) || !*nrpages)
+ return;
+
+ ffs = folio->private;
+ next = ffs_next_nonuptodate_subpage(ffs, *offset,
+ *offset + *nrpages - 1);
+ skipped = next - *offset;
+ if (!skipped)
+ return;
+
+ *index += skipped;
+ *offset += skipped;
+ *nrpages -= skipped;
+ *max_nr_pages -= skipped;
+}
+
+static void f2fs_truncate_read_extent(struct folio *folio, pgoff_t offset,
+ unsigned int *len_blks)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int next, end;
+
+ if (!f2fs_folio_has_ffs(folio) || *len_blks <= 1)
+ return;
+
+ ffs = folio->private;
+ end = offset + *len_blks - 1;
+ next = ffs_next_uptodate_subpage(ffs, offset + 1, end);
+ if (next <= end)
+ *len_blks = next - offset;
+}
static int f2fs_read_data_large_folio(struct inode *inode,
struct fsverity_info *vi,
struct readahead_control *rac, struct folio *folio)
@@ -2901,6 +2951,11 @@ static int f2fs_read_data_large_folio(struct inode *inode,
len_blks = 1;
+ f2fs_skip_fully_uptodate_front(folio, &index, &offset,
+ &nrpages, &max_nr_pages);
+ if (!nrpages)
+ break;
+
/*
* Map blocks using the previous result first.
*/
@@ -2933,6 +2988,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
len_blks = min_t(unsigned int, nrpages, max_nr_pages);
len_blks = min_t(unsigned int, len_blks,
(unsigned int)(map.m_lblk + map.m_len - index));
+ f2fs_truncate_read_extent(folio, offset, &len_blks);
for (i = 0; i < len_blks; i++) {
if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
@@ -2959,6 +3015,13 @@ static int f2fs_read_data_large_folio(struct inode *inode,
ret = -EIO;
goto err_out;
}
+ if (folio_test_large(folio)) {
+ ffs = f2fs_ffs_find_or_alloc(folio);
+ spin_lock_irq(&ffs->state_lock);
+ __ffs_mark_subrange_uptodate(folio, ffs,
+ page_offset, PAGE_SIZE);
+ spin_unlock_irq(&ffs->state_lock);
+ }
continue;
}
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 10/14] f2fs: handle partial truncate of large folio dirty subpages
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (8 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 09/14] f2fs: support partial uptodate " Nanzhe Zhao
@ 2026-08-26 8:26 ` Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 11/14] f2fs: fix zeroing paths for large folios Nanzhe Zhao
` (3 subsequent siblings)
13 siblings, 0 replies; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 8:26 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
A large folio can be partial truncated and stays in folio mapping, we
need to clear the subrange dirty bits and uptodate bits that the partial
truncate covers. If this partial truncate happens to clear the last
subrange dirty bits, then cancel the whole folio dirty state.
Also add a guard in f2fs_write_single_data_folio() so a large folio
subpage whose disk block was already truncated (NULL_ADDR) is skipped
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 73 insertions(+), 1 deletion(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 59b07b83b216..15774e8e8795 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -2693,6 +2693,34 @@ static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
folio_mark_uptodate(folio);
}
+static void ffs_clear_subrange_uptodate(struct folio *folio,
+ size_t offset, size_t len)
+{
+ struct f2fs_folio_state *ffs;
+ unsigned int nr_subpages, start, end;
+ unsigned long flags;
+
+ f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
+
+ if (!f2fs_folio_has_ffs(folio)) {
+ if (folio_test_uptodate(folio))
+ folio_clear_uptodate(folio);
+ return;
+ }
+
+ ffs = (struct f2fs_folio_state *)folio->private;
+ nr_subpages = folio_nr_pages(folio);
+ start = offset >> PAGE_SHIFT;
+ end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+ end = min(end, nr_subpages);
+
+ spin_lock_irqsave(&ffs->state_lock, flags);
+ bitmap_clear(ffs->state, start, end - start);
+ spin_unlock_irqrestore(&ffs->state_lock, flags);
+ if (folio_test_uptodate(folio))
+ folio_clear_uptodate(folio);
+}
+
bool f2fs_ffs_test_blk_dirty(const struct folio *folio, pgoff_t index)
{
struct f2fs_folio_state *ffs;
@@ -3599,6 +3627,21 @@ static int f2fs_write_single_data_folio(struct folio *folio, int *submitted,
fio.old_blkaddr = dn.data_blkaddr;
+ /* This page is already truncated */
+ if (fio.old_blkaddr == NULL_ADDR) {
+ ffs_clear_subrange_uptodate(folio,
+ i << PAGE_SHIFT, PAGE_SIZE);
+ folio_clear_f2fs_gcing(folio);
+ f2fs_put_dnode(&dn);
+ if (f2fs_folio_has_ffs(folio)) {
+ struct f2fs_folio_state *ffs =
+ (struct f2fs_folio_state *)folio->private;
+
+ atomic_dec(&ffs->write_pages_pending);
+ }
+ continue;
+ }
+
got_it:
if (__is_valid_data_blkaddr(fio.old_blkaddr) &&
!f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
@@ -5075,8 +5118,37 @@ void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
- (offset || length != folio_size(folio)))
+ (offset || length != folio_size(folio))) {
+ size_t clear_start = ALIGN(offset, PAGE_SIZE);
+ size_t clear_end = round_down(offset + length, PAGE_SIZE);
+ size_t clear_length = 0;
+
+ /*
+ * If the truncated range falls within a single subpage, no
+ * subpage state needs to be cleared.
+ */
+ if (clear_start < clear_end && f2fs_folio_has_ffs(folio)) {
+ bool dirty;
+
+ clear_length = clear_end - clear_start;
+ dirty = f2fs_ffs_clear_subrange_dirty(folio,
+ clear_start, clear_length);
+ ffs_clear_subrange_uptodate(folio, clear_start,
+ clear_length);
+
+ /*
+ * If the truncated subrange happens to clear the
+ * remaining dirty bitmap of the whole folio, cancel
+ * the folio-level dirty state.
+ */
+ if (!dirty && folio_test_dirty(folio)) {
+ inode_dec_dirty_pages(inode);
+ f2fs_remove_dirty_inode(inode);
+ folio_cancel_dirty(folio);
+ }
+ }
return;
+ }
if (folio_test_dirty(folio)) {
if (inode->i_ino == F2FS_META_INO(sbi)) {
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 11/14] f2fs: fix zeroing paths for large folios
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (9 preceding siblings ...)
2026-08-26 8:26 ` [PATCH 10/14] f2fs: handle partial truncate of large folio dirty subpages Nanzhe Zhao
@ 2026-08-26 13:09 ` Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 12/14] f2fs: handle block cloning within the same large folio Nanzhe Zhao
` (2 subsequent siblings)
13 siblings, 0 replies; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 13:09 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Several f2fs zeroing paths still use PAGE_SIZE based offsets after
a file mapping can contain large folios. This is fine for order-0
folios, but it zeros the wrong range once the target block is not at
offset 0 in a large folio.
Use offset_in_folio() to translate the file block index to the folio
offset before zeroing data in truncate_partial_data_page(), fill_zero(),
and f2fs_get_new_data_folio().
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 13 ++++++++++---
fs/f2fs/f2fs.h | 1 +
fs/f2fs/file.c | 27 ++++++++++++++++++++++-----
3 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 15774e8e8795..0b167b14a9a5 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -1582,9 +1582,16 @@ struct folio *f2fs_get_new_data_folio(struct inode *inode,
goto got_it;
if (dn.data_blkaddr == NEW_ADDR) {
- folio_zero_segment(folio, 0, folio_size(folio));
- if (!folio_test_uptodate(folio))
+ size_t off = offset_in_folio(folio,
+ (loff_t)index << PAGE_SHIFT);
+
+ folio_zero_segment(folio, off, off + PAGE_SIZE);
+ if (folio_test_large(folio)) {
+ f2fs_ffs_find_or_alloc(folio);
+ f2fs_ffs_mark_subrange_uptodate(folio, off, PAGE_SIZE);
+ } else if (!folio_test_uptodate(folio)) {
folio_mark_uptodate(folio);
+ }
} else {
f2fs_folio_put(folio, true);
@@ -2670,7 +2677,7 @@ static bool __ffs_mark_subrange_uptodate(struct folio *folio,
return bitmap_full(ffs->state, nr_subpages);
}
-static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
+void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
size_t len)
{
struct f2fs_folio_state *ffs;
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 0ffbe2bd04c8..6ae249bf9aa1 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4330,6 +4330,7 @@ struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,
bool for_write);
struct folio *f2fs_get_new_data_folio(struct inode *inode,
struct folio *ifolio, pgoff_t index, bool new_i_size);
+void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset, size_t len);
int f2fs_do_write_data_page(struct f2fs_io_info *fio);
int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag);
int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 02d687527241..4272013dbe38 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -811,12 +811,12 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
static int truncate_partial_data_page(struct inode *inode, u64 from,
bool cache_only)
{
- loff_t offset = from & (PAGE_SIZE - 1);
pgoff_t index = from >> PAGE_SHIFT;
struct address_space *mapping = inode->i_mapping;
struct folio *folio;
+ size_t folio_off;
- if (!offset && !cache_only)
+ if (!(from & (PAGE_SIZE - 1)) && !cache_only)
return 0;
if (cache_only) {
@@ -834,12 +834,21 @@ static int truncate_partial_data_page(struct inode *inode, u64 from,
return PTR_ERR(folio) == -ENOENT ? 0 : PTR_ERR(folio);
truncate_out:
f2fs_folio_wait_writeback(folio, DATA, true, true);
- folio_zero_segment(folio, offset, folio_size(folio));
+ folio_off = offset_in_folio(folio, from);
+ folio_zero_segment(folio, folio_off, folio_size(folio));
/* An encrypted inode should have a key and truncate the last page. */
f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
- if (!cache_only)
+ if (!cache_only) {
+ if (folio_test_large(folio)) {
+ f2fs_ffs_find_or_alloc(folio);
+ f2fs_ffs_mark_subrange_uptodate(folio, folio_off,
+ folio_size(folio) - folio_off);
+ f2fs_ffs_mark_subrange_dirty(folio, folio_off,
+ folio_size(folio) - folio_off);
+ }
folio_mark_dirty(folio);
+ }
f2fs_folio_put(folio, true);
return 0;
}
@@ -1310,6 +1319,7 @@ static int fill_zero(struct inode *inode, pgoff_t index,
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct folio *folio;
struct f2fs_lock_context lc;
+ size_t folio_off;
if (!len)
return 0;
@@ -1324,7 +1334,14 @@ static int fill_zero(struct inode *inode, pgoff_t index,
return PTR_ERR(folio);
f2fs_folio_wait_writeback(folio, DATA, true, true);
- folio_zero_range(folio, start, len);
+ folio_off = offset_in_folio(folio,
+ (loff_t)index << PAGE_SHIFT) + start;
+ folio_zero_range(folio, folio_off, len);
+ if (folio_test_large(folio)) {
+ f2fs_ffs_find_or_alloc(folio);
+ f2fs_ffs_mark_subrange_uptodate(folio, folio_off, len);
+ f2fs_ffs_mark_subrange_dirty(folio, folio_off, len);
+ }
folio_mark_dirty(folio);
f2fs_folio_put(folio, true);
return 0;
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 12/14] f2fs: handle block cloning within the same large folio
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (10 preceding siblings ...)
2026-08-26 13:09 ` [PATCH 11/14] f2fs: fix zeroing paths for large folios Nanzhe Zhao
@ 2026-08-26 13:09 ` Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 13/14] f2fs: allow large folio support to writeable files Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 14/14] f2fs: make compressed files compatible with large folio Nanzhe Zhao
13 siblings, 0 replies; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 13:09 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
With large folios, the source and destination block in __clone_blkaddrs()
can belong to the same folio during same-inode operations such as
collapse range and insert range.
In that case, locking the source folio and then looking up the
destination folio can try to lock the same folio again. It also copies
from and to offset 0, which is only correct for order-0 folios.
Detect the same-inode, same-folio case before looking up the destination
folio. Then in this case,we reserve the destination block explicitly.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/file.c | 76 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 64 insertions(+), 12 deletions(-)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 4272013dbe38..b82acbc3240f 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1581,25 +1581,77 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
f2fs_put_dnode(&dn);
} else {
struct folio *fsrc, *fdst;
+ size_t src_off, dst_off;
fsrc = f2fs_get_lock_data_folio(src_inode,
src + i, true);
if (IS_ERR(fsrc))
return PTR_ERR(fsrc);
- fdst = f2fs_get_new_data_folio(dst_inode, NULL, dst + i,
- true);
- if (IS_ERR(fdst)) {
- f2fs_folio_put(fsrc, true);
- return PTR_ERR(fdst);
- }
- f2fs_folio_wait_writeback(fdst, DATA, true, true);
+ src_off = offset_in_folio(fsrc,
+ (loff_t)(src + i) << PAGE_SHIFT);
+
+ /*
+ * For same-inode operations (collapse/insert), src and
+ * dst may fall within the same large folio. Detect this
+ * to avoid self-deadlock on folio lock.
+ */
+ if (src_inode == dst_inode &&
+ folio_contains(fsrc, dst + i)) {
+ struct dnode_of_data dn;
+
+ /* Reserve block for dst before copying */
+ set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
+ ret = f2fs_reserve_block(&dn, dst + i);
+ if (ret) {
+ f2fs_folio_put(fsrc, true);
+ return ret;
+ }
+
+ dst_off = offset_in_folio(fsrc,
+ (loff_t)(dst + i) << PAGE_SHIFT);
+ f2fs_folio_wait_writeback(fsrc, DATA, true, true);
+ memcpy_folio(fsrc, dst_off, fsrc, src_off,
+ PAGE_SIZE);
+ if (folio_test_large(fsrc)) {
+ f2fs_ffs_find_or_alloc(fsrc);
+ f2fs_ffs_mark_subrange_uptodate(fsrc,
+ dst_off, PAGE_SIZE);
+ f2fs_ffs_mark_subrange_dirty(fsrc, dst_off,
+ PAGE_SIZE);
+ }
+ folio_mark_dirty(fsrc);
+ if (i_size_read(dst_inode) <
+ ((loff_t)(dst + i + 1) << PAGE_SHIFT))
+ f2fs_i_size_write(dst_inode,
+ ((loff_t)(dst + i + 1) << PAGE_SHIFT));
+ folio_set_f2fs_gcing(fsrc);
+ f2fs_folio_put(fsrc, true);
+ } else {
+ fdst = f2fs_get_new_data_folio(dst_inode, NULL,
+ dst + i, true);
+ if (IS_ERR(fdst)) {
+ f2fs_folio_put(fsrc, true);
+ return PTR_ERR(fdst);
+ }
- memcpy_folio(fdst, 0, fsrc, 0, PAGE_SIZE);
- folio_mark_dirty(fdst);
- folio_set_f2fs_gcing(fdst);
- f2fs_folio_put(fdst, true);
- f2fs_folio_put(fsrc, true);
+ dst_off = offset_in_folio(fdst,
+ (loff_t)(dst + i) << PAGE_SHIFT);
+ f2fs_folio_wait_writeback(fdst, DATA, true, true);
+ memcpy_folio(fdst, dst_off, fsrc, src_off,
+ PAGE_SIZE);
+ if (folio_test_large(fdst)) {
+ f2fs_ffs_find_or_alloc(fdst);
+ f2fs_ffs_mark_subrange_uptodate(fdst,
+ dst_off, PAGE_SIZE);
+ f2fs_ffs_mark_subrange_dirty(fdst, dst_off,
+ PAGE_SIZE);
+ }
+ folio_mark_dirty(fdst);
+ folio_set_f2fs_gcing(fdst);
+ f2fs_folio_put(fdst, true);
+ f2fs_folio_put(fsrc, true);
+ }
ret = f2fs_truncate_hole(src_inode,
src + i, src + i + 1);
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 13/14] f2fs: allow large folio support to writeable files
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (11 preceding siblings ...)
2026-08-26 13:09 ` [PATCH 12/14] f2fs: handle block cloning within the same large folio Nanzhe Zhao
@ 2026-08-26 13:09 ` Nanzhe Zhao
2026-08-28 17:44 ` [f2fs-dev] " Daeho Jeong
2026-08-26 13:09 ` [PATCH 14/14] f2fs: make compressed files compatible with large folio Nanzhe Zhao
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 13:09 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
Now we make all write path support large folios,
so we open permission to let writeable file set
large folio mapping.
Keep fs-layer encrypted files excluded unless inline encryption is
enabled, since f2fs_encrypt_one_page() still encrypts one PAGE_SIZE
page and cannot handle large folios.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 9 ++++++++-
fs/f2fs/f2fs.h | 13 +++++++++++++
fs/f2fs/file.c | 16 ----------------
fs/f2fs/inode.c | 4 +---
fs/f2fs/namei.c | 1 +
5 files changed, 23 insertions(+), 20 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 0b167b14a9a5..2e20833c6417 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -2955,7 +2955,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
int ret = 0;
bool folio_in_bio = false;
- if (!IS_IMMUTABLE(inode) || f2fs_compressed_file(inode)) {
+ if (f2fs_compressed_file(inode)) {
if (folio)
folio_unlock(folio);
return -EOPNOTSUPP;
@@ -3321,6 +3321,13 @@ int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
if (fscrypt_inode_uses_inline_crypto(inode))
return 0;
+ if (folio_test_large(page_folio(page))) {
+ f2fs_warn_ratelimited(F2FS_I_SB(inode),
+ "large folio does not support fs-layer encryption, ino=%llu",
+ (unsigned long long)inode->i_ino);
+ return -EOPNOTSUPP;
+ }
+
fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page_folio(page),
PAGE_SIZE, 0, GFP_NOFS);
if (IS_ERR(fio->encrypted_page))
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 6ae249bf9aa1..04ed0ea92b26 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -5201,6 +5201,19 @@ static inline bool f2fs_quota_file(struct f2fs_sb_info *sbi, nid_t ino)
return false;
}
+static inline void f2fs_mapping_set_large_folio(struct inode *inode)
+{
+ if (!S_ISREG(inode->i_mode) ||
+ f2fs_has_inline_data(inode) ||
+ f2fs_compressed_file(inode) ||
+ f2fs_quota_file(F2FS_I_SB(inode), inode->i_ino) ||
+ (f2fs_encrypted_file(inode) &&
+ !(inode->i_sb->s_flags & SB_INLINECRYPT)))
+ return;
+
+ mapping_set_folio_min_order(inode->i_mapping, 0);
+}
+
static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
{
return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index b82acbc3240f..c8426821e701 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -134,17 +134,6 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
int err = 0;
vm_fault_t ret;
- /*
- * We only support large folio on the read case.
- * Don't make any dirty pages.
- */
- if (unlikely(IS_IMMUTABLE(inode)) ||
- mapping_large_folio_support(inode->i_mapping)) {
- f2fs_err(sbi, "Not expected: immutable: %d large_folio: %d",
- IS_IMMUTABLE(inode),
- mapping_large_folio_support(inode->i_mapping));
- return VM_FAULT_SIGBUS;
- }
if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
err = -EIO;
@@ -698,9 +687,6 @@ static int f2fs_file_open(struct inode *inode, struct file *filp)
if (!f2fs_is_compress_backend_ready(inode))
return -EOPNOTSUPP;
- if (mapping_large_folio_support(inode->i_mapping) &&
- filp->f_mode & FMODE_WRITE)
- return -EOPNOTSUPP;
err = fsverity_file_open(inode, filp);
if (err)
@@ -1177,8 +1163,6 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
return -EPERM;
if ((attr->ia_valid & ATTR_SIZE)) {
- if (mapping_large_folio_support(inode->i_mapping))
- return -EOPNOTSUPP;
if (IS_DEVICE_ALIASING(inode))
return -EPERM;
if (!f2fs_is_compress_backend_ready(inode))
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
index 96cc0e777567..7c4b5cdfe276 100644
--- a/fs/f2fs/inode.c
+++ b/fs/f2fs/inode.c
@@ -642,9 +642,7 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
inode->i_op = &f2fs_file_inode_operations;
inode->i_fop = &f2fs_file_operations;
inode->i_mapping->a_ops = &f2fs_dblock_aops;
- if (IS_IMMUTABLE(inode) && !f2fs_compressed_file(inode) &&
- !f2fs_quota_file(sbi, inode->i_ino))
- mapping_set_folio_min_order(inode->i_mapping, 0);
+ f2fs_mapping_set_large_folio(inode);
} else if (S_ISDIR(inode->i_mode)) {
inode->i_op = &f2fs_dir_inode_operations;
inode->i_fop = &f2fs_dir_operations;
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index afaab7739283..bdf4e586bfcf 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -341,6 +341,7 @@ static struct inode *f2fs_new_inode(struct mnt_idmap *idmap,
f2fs_set_inode_flags(inode);
f2fs_init_extent_tree(inode);
+ f2fs_mapping_set_large_folio(inode);
trace_f2fs_new_inode(inode, 0);
return inode;
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* [PATCH 14/14] f2fs: make compressed files compatible with large folio
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
` (12 preceding siblings ...)
2026-08-26 13:09 ` [PATCH 13/14] f2fs: allow large folio support to writeable files Nanzhe Zhao
@ 2026-08-26 13:09 ` Nanzhe Zhao
2026-08-28 17:52 ` [f2fs-dev] " Daeho Jeong
13 siblings, 1 reply; 29+ messages in thread
From: Nanzhe Zhao @ 2026-08-26 13:09 UTC (permalink / raw)
To: linux-f2fs-devel, Jaegeuk Kim, Chao Yu
Cc: Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li
The compression flag is the hint indicates that the inode can be
compressed, when the inode is using large folio, we expected it keeps
using the large folio read/write paths and its data stays uncompressed
on disk until the inode is evicted and re-read
Let f2fs_write_begin() skip the compression overwrite preparation for
such inodes and remove the compressed-file gate in
f2fs_read_data_large_folio() so the data is simply read/written as
regular blocks.
Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
---
fs/f2fs/data.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 2e20833c6417..befcdc3742f6 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -2955,12 +2955,6 @@ static int f2fs_read_data_large_folio(struct inode *inode,
int ret = 0;
bool folio_in_bio = false;
- if (f2fs_compressed_file(inode)) {
- if (folio)
- folio_unlock(folio);
- return -EOPNOTSUPP;
- }
-
map.m_seg_type = NO_CHECK_TYPE;
if (rac)
@@ -4935,7 +4929,8 @@ static int f2fs_write_begin(const struct kiocb *iocb,
}
#ifdef CONFIG_F2FS_FS_COMPRESSION
- if (f2fs_compressed_file(inode)) {
+ if (f2fs_compressed_file(inode) &&
+ !mapping_large_folio_support(inode->i_mapping)) {
int ret;
struct page *page;
--
2.43.0
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 01/14] f2fs: extend folio state for large folio write path
2026-08-26 8:26 ` [PATCH 01/14] f2fs: extend folio state for large folio write path Nanzhe Zhao
@ 2026-08-27 6:57 ` Chao Yu
2026-08-27 20:51 ` [f2fs-dev] " Daeho Jeong
0 siblings, 1 reply; 29+ messages in thread
From: Chao Yu @ 2026-08-27 6:57 UTC (permalink / raw)
To: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim
Cc: chao, Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain,
linux-kernel, David Hildenbrand, Bo Zhang, Kalesh Singh,
Ryan Roberts, Pengfei Li
On 8/26/26 16:26, Nanzhe Zhao wrote:
> Large folio write path needs a subpage status bitmap and write
> pages pending counter, while keeping compatible with f2fs private
> flags.
>
> Move struct f2fs_folio_state to f2fs.h, add private_flags and
> subpage state bitmap, and change PAGE_PRIVATE functions to be
> compatible with f2fs_folio_state. Allocate f2fs_folio_state via kzalloc
> instead of kmem_cache, since the state size depends on the folio order.
>
> Note: Now if a path wants to use f2fs_folio_state, it must call
> `folio_has_ffs` instead of `folio_test_large`` to make check.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/compress.c | 2 +
> fs/f2fs/data.c | 60 ++++++++++++++++--------------
> fs/f2fs/f2fs.h | 92 ++++++++++++++++++++++++++++++++++++----------
> fs/f2fs/segment.c | 2 +-
> 4 files changed, 108 insertions(+), 48 deletions(-)
>
> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> index 91855d91bbdd..84da3e39efb4 100644
> --- a/fs/f2fs/compress.c
> +++ b/fs/f2fs/compress.c
> @@ -78,6 +78,8 @@ bool f2fs_is_compressed_page(struct folio *folio)
> return false;
> if (folio_test_f2fs_nonpointer(folio))
> return false;
> + if (f2fs_folio_has_ffs(folio))
> + return false;
Shouldn't this be changed in "f2fs: make compressed files compatible with
large folio" or other patch? I guess in this patch we only introduce new
ffs facilities.
>
> f2fs_bug_on(F2FS_F_SB(folio),
> *((u32 *)folio->private) != F2FS_COMPRESSED_PAGE_MAGIC);
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 6ae0eb37d20f..578a90d427e2 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -32,20 +32,13 @@
>
> static struct kmem_cache *bio_post_read_ctx_cache;
> static struct kmem_cache *bio_entry_slab;
> -static struct kmem_cache *ffs_entry_slab;
> static mempool_t *bio_post_read_ctx_pool;
> static struct bio_set f2fs_bioset;
>
> -struct f2fs_folio_state {
> - spinlock_t state_lock;
> - unsigned int read_pages_pending;
> -};
> -
> struct f2fs_bio {
> struct work_struct work;
> struct bio bio;
> };
> -
Unnecessary change.
> #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
>
> int __init f2fs_init_bioset(void)
> @@ -133,6 +126,9 @@ struct bio_post_read_ctx {
> block_t fs_blkaddr;
> };
>
> +static bool __ffs_mark_subrange_uptodate(struct folio *folio,
> + struct f2fs_folio_state *ffs, size_t offset, size_t len);
> +
> /*
> * Update and unlock a bio's pages, and free the bio.
> *
> @@ -155,7 +151,7 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
>
> bio_for_each_folio_all(fi, bio) {
> struct folio *folio = fi.folio;
> - unsigned nr_pages = fi.length >> PAGE_SHIFT;
> + unsigned int nr_pages = fi.length >> PAGE_SHIFT;
No need to change in this patch?
> bool finished = true;
>
> if (!folio_test_large(folio) &&
> @@ -360,6 +356,7 @@ static void f2fs_write_end_bio(struct bio *bio)
>
> bio_for_each_folio_all(fi, bio) {
> struct folio *folio = fi.folio;
> + unsigned int nr_pages = fi.length >> PAGE_SHIFT;
No need to change in this patch?
> enum count_type type;
>
> if (fscrypt_is_bounce_folio(folio)) {
> @@ -2516,17 +2513,32 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
> }
> #endif
>
> -static struct f2fs_folio_state *ffs_find_or_alloc(struct folio *folio)
> +struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio)
> {
> - struct f2fs_folio_state *ffs = folio->private;
> + struct f2fs_folio_state *ffs;
> + unsigned int nr_subpages = folio_nr_pages(folio);
> + unsigned long private_flags = 0;
> +
> + f2fs_bug_on(F2FS_F_SB(folio), !folio_test_large(folio));
>
> - if (ffs)
> - return ffs;
> + if (f2fs_folio_has_ffs(folio))
> + return (struct f2fs_folio_state *)folio->private;
>
> - ffs = f2fs_kmem_cache_alloc(ffs_entry_slab,
> - GFP_NOIO | __GFP_ZERO, true, NULL);
> + if (folio_test_private(folio) && folio_test_f2fs_nonpointer(folio))
> + private_flags = (unsigned long)folio->private;
> +
> + ffs = kzalloc(struct_size(ffs, state, BITS_TO_LONGS(2 * nr_subpages)),
> + GFP_NOIO | __GFP_NOFAIL);
>
> spin_lock_init(&ffs->state_lock);
> + ffs->private_flags = private_flags;
> + if (folio_test_uptodate(folio))
> + bitmap_set(ffs->state, 0, nr_subpages);
> + if (folio_test_dirty(folio))
> + bitmap_set(ffs->state, nr_subpages, nr_subpages);
> +
> + if (folio_test_private(folio))
> + folio_detach_private(folio);
> folio_attach_private(folio, ffs);
> return ffs;
> }
> @@ -2535,7 +2547,7 @@ static void ffs_detach_free(struct folio *folio)
> {
> struct f2fs_folio_state *ffs;
>
> - if (!folio_test_large(folio)) {
> + if (!f2fs_folio_has_ffs(folio)) {
> folio_detach_private(folio);
> return;
> }
> @@ -2545,7 +2557,8 @@ static void ffs_detach_free(struct folio *folio)
> return;
>
> WARN_ON_ONCE(ffs->read_pages_pending != 0);
> - kmem_cache_free(ffs_entry_slab, ffs);
> + WARN_ON_ONCE(atomic_read(&ffs->write_pages_pending));
> + kfree(ffs);
> }
>
> static int f2fs_read_data_large_folio(struct inode *inode,
> @@ -2558,7 +2571,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
> pgoff_t index, offset, next_pgofs = 0;
> unsigned max_nr_pages = rac ? readahead_count(rac) :
> folio_nr_pages(folio);
> - unsigned nrpages;
> + unsigned int nrpages, len_blks;
> struct f2fs_folio_state *ffs;
> int ret = 0;
> bool folio_in_bio = false;
> @@ -2634,7 +2647,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
> * to prevent from premature folio_end_read() call on folio
> */
> if (folio_test_large(folio)) {
> - ffs = ffs_find_or_alloc(folio);
> + ffs = f2fs_ffs_find_or_alloc(folio);
>
> /* set the bitmap to wait */
> spin_lock_irq(&ffs->state_lock);
> @@ -2987,7 +3000,7 @@ bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
> return true;
>
> if (fio) {
> - if (page_private_gcing(fio->page))
> + if (folio_test_f2fs_gcing(fio->folio))
Shouldn't this be changed in "f2fs: make GC migration large-folio aware"?
> return true;
> if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
> f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
> @@ -4586,21 +4599,12 @@ int __init f2fs_init_bio_entry_cache(void)
> if (!bio_entry_slab)
> return -ENOMEM;
>
> - ffs_entry_slab = f2fs_kmem_cache_create("f2fs_ffs_slab",
> - sizeof(struct f2fs_folio_state));
> -
> - if (!ffs_entry_slab) {
> - kmem_cache_destroy(bio_entry_slab);
> - return -ENOMEM;
> - }
> -
> return 0;
> }
>
> void f2fs_destroy_bio_entry_cache(void)
> {
> kmem_cache_destroy(bio_entry_slab);
> - kmem_cache_destroy(ffs_entry_slab);
> }
>
> static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 16720f1f0a9c..2e8f85cea6d0 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -1628,6 +1628,18 @@ static inline void f2fs_clear_bit(unsigned int nr, char *addr);
> * Layout B: lowest bit should be 0
> * page.private is a wrapped pointer.
> */
> +
> +struct f2fs_folio_state {
> + spinlock_t state_lock;
> + unsigned int read_pages_pending;
> + atomic_t write_pages_pending;
> + unsigned long private_flags;
> + /* state[0..nr_subpages - 1] tracks uptodate subpages.
> + * state[nr_subpages..2 * nr_subpages - 1] tracks dirty subpages.
> + */
> + unsigned long state[];
> +};
> +
> enum {
> PAGE_PRIVATE_NOT_POINTER, /* private contains non-pointer data */
> PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */
> @@ -1637,6 +1649,14 @@ enum {
> PAGE_PRIVATE_MAX
> };
>
> +static inline bool f2fs_folio_has_ffs(const struct folio *folio)
> +{
> + unsigned long private = (unsigned long)folio->private;
> +
> + return folio_test_large(folio) && private &&
> + !(private & BIT(PAGE_PRIVATE_NOT_POINTER));
Is this a bug? in which case we will set PAGE_PRIVATE_NOT_POINTER in
a large folio? maybe I missed some cases...
folio_test_large(folio) && private is true and
(private & BIT(PAGE_PRIVATE_NOT_POINTER) is true?
> +}
> +
> /* For compression */
> enum compress_algorithm_type {
> COMPRESS_LZO,
> @@ -2682,10 +2702,57 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
> return -ENOSPC;
> }
>
> +static inline unsigned long f2fs_folio_get_private_flags(const struct folio *folio)
> +{
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs = folio->private;
> +
> + return ffs->private_flags;
> + }
> +
> + return (unsigned long)folio->private;
> +}
> +
> +static inline void f2fs_folio_set_private_flags(struct folio *folio,
> + unsigned long flags)
> +{
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs = folio->private;
> +
> + ffs->private_flags |= flags;
> + return;
> + }
> +
> + if (!folio_test_private(folio))
> + folio_attach_private(folio, (void *)flags);
> + else
> + folio->private = (void *)((unsigned long)folio->private | flags);
> +}
> +
> +static inline void f2fs_folio_clear_private_flags(struct folio *folio,
> + unsigned long flags)
> +{
> + unsigned long private;
> +
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs = folio->private;
> +
> + ffs->private_flags &= ~flags;
> + return;
> + }
> +
> + private = (unsigned long)folio->private;
> + private &= ~flags;
> + if (private == BIT(PAGE_PRIVATE_NOT_POINTER))
> + folio_detach_private(folio);
> + else
> + folio->private = (void *)private;
> +}
> +
> #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
> static inline bool folio_test_f2fs_##name(const struct folio *folio) \
> { \
> - unsigned long priv = (unsigned long)folio->private; \
> + unsigned long priv = f2fs_folio_get_private_flags(folio); \
> unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
> (1UL << PAGE_PRIVATE_##flagname); \
> return (priv & v) == v; \
> @@ -2702,12 +2769,7 @@ static inline void folio_set_f2fs_##name(struct folio *folio) \
> { \
> unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
> (1UL << PAGE_PRIVATE_##flagname); \
> - if (!folio->private) \
> - folio_attach_private(folio, (void *)v); \
> - else { \
> - v |= (unsigned long)folio->private; \
> - folio->private = (void *)v; \
> - } \
> + f2fs_folio_set_private_flags(folio, v); \
> } \
> static inline void set_page_private_##name(struct page *page) \
> { \
> @@ -2720,13 +2782,8 @@ static inline void set_page_private_##name(struct page *page) \
> #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
> static inline void folio_clear_f2fs_##name(struct folio *folio) \
> { \
> - unsigned long v = (unsigned long)folio->private; \
> - \
> - v &= ~(1UL << PAGE_PRIVATE_##flagname); \
> - if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \
> - folio_detach_private(folio); \
> - else \
> - folio->private = (void *)v; \
> + f2fs_folio_clear_private_flags(folio, \
> + 1UL << PAGE_PRIVATE_##flagname); \
> } \
> static inline void clear_page_private_##name(struct page *page) \
> { \
> @@ -2752,7 +2809,7 @@ PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
>
> static inline unsigned long folio_get_f2fs_data(struct folio *folio)
> {
> - unsigned long data = (unsigned long)folio->private;
> + unsigned long data = f2fs_folio_get_private_flags(folio);
>
> if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
> return 0;
> @@ -2763,10 +2820,7 @@ static inline void folio_set_f2fs_data(struct folio *folio, unsigned long data)
> {
> data = (1UL << PAGE_PRIVATE_NOT_POINTER) | (data << PAGE_PRIVATE_MAX);
>
> - if (!folio_test_private(folio))
> - folio_attach_private(folio, (void *)data);
> - else
> - folio->private = (void *)((unsigned long)folio->private | data);
> + f2fs_folio_set_private_flags(folio, data);
> }
>
> static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index ed6f2947210b..df10119d94ad 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -3803,7 +3803,7 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
> if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
> return CURSEG_COLD_DATA_PINNED;
>
> - if (page_private_gcing(fio->page)) {
> + if (folio_test_f2fs_gcing(fio->folio)) {
Shouldn't this be changed in "f2fs: make GC migration large-folio aware"?
Thanks,
> if (fio->sbi->am.atgc_enabled &&
> (fio->io_type == FS_DATA_IO) &&
> (fio->sbi->gc_mode != GC_URGENT_HIGH) &&
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 02/14] f2fs: carry subpage offset and count in write IO
2026-08-26 8:26 ` [PATCH 02/14] f2fs: carry subpage offset and count in write IO Nanzhe Zhao
@ 2026-08-27 7:16 ` Chao Yu
2026-08-27 21:06 ` [f2fs-dev] " Daeho Jeong
0 siblings, 1 reply; 29+ messages in thread
From: Chao Yu @ 2026-08-27 7:16 UTC (permalink / raw)
To: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim
Cc: chao, Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain,
linux-kernel, David Hildenbrand, Bo Zhang, Kalesh Singh,
Ryan Roberts, Pengfei Li
On 8/26/26 16:26, Nanzhe Zhao wrote:
> Large folio write paths need to submit I/O for a range inside a
> folio instead of always submitting the whole folio from offset zero.
> Add folio_offset and folio_blkcnt to f2fs_io_info to describe the
> block offset inside the folio and the number of contiguous blocks
> covered by the I/O.
>
> Apply the new fields to the bio submit paths that need the subpage
> offset or contiguous block count.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 59 ++++++++++++++++++++++++++++++++---------------
> fs/f2fs/f2fs.h | 11 +++++++++
> fs/f2fs/segment.c | 3 ++-
> 3 files changed, 54 insertions(+), 19 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 578a90d427e2..8649d443ca74 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -783,6 +783,9 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
> struct folio *fio_folio = fio->folio;
> struct folio *data_folio = fio->encrypted_page ?
> page_folio(fio->encrypted_page) : fio_folio;
> + pgoff_t fio_lblk = F2FS_FIO_LBLK(fio_folio, fio);
> + size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> + size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
>
> if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
> fio->is_por ? META_POR : (__is_meta_io(fio) ?
> @@ -795,11 +798,11 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
> bio = __bio_alloc(fio, 1);
>
> f2fs_set_bio_crypt_ctx(bio, fio_folio->mapping->host,
> - fio_folio->index, fio, GFP_NOIO);
> - bio_add_folio_nofail(bio, data_folio, folio_size(data_folio), 0);
> + fio_lblk, fio, GFP_NOIO);
> + bio_add_folio_nofail(bio, data_folio, bio_len, bio_offset);
>
> if (fio->io_wbc && !is_read_io(fio->op))
> - wbc_account_cgroup_owner(fio->io_wbc, fio_folio, PAGE_SIZE);
> + wbc_account_cgroup_owner(fio->io_wbc, fio_folio, bio_len);
>
> inc_page_count(fio->sbi, is_read_io(fio->op) ?
> __read_io_type(data_folio) : WB_DATA_TYPE(fio->folio, false));
> @@ -844,7 +847,8 @@ static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
> }
>
> static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
> - struct folio *folio, enum temp_type temp)
> + struct folio *folio, size_t len, size_t offset,
> + enum temp_type temp)
> {
> struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
> struct bio_entry *be;
> @@ -853,7 +857,7 @@ static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
> be->bio = bio;
> bio_get(bio);
>
> - bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
> + bio_add_folio_nofail(bio, folio, len, offset);
>
> f2fs_down_write(&io->bio_list_lock);
> list_add_tail(&be->list, &io->bio_list);
> @@ -870,6 +874,9 @@ static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
> struct folio *folio)
> {
> struct folio *fio_folio = fio->folio;
> + pgoff_t fio_lblk = F2FS_FIO_LBLK(fio_folio, fio);
> + size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> + size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
> struct f2fs_sb_info *sbi = fio->sbi;
> enum temp_type temp;
> bool found = false;
> @@ -892,8 +899,8 @@ static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
> fio->new_blkaddr));
> if (f2fs_crypt_mergeable_bio(*bio,
> fio_folio->mapping->host,
> - fio_folio->index, fio) &&
> - bio_add_folio(*bio, folio, folio_size(folio), 0)) {
> + fio_lblk, fio) &&
> + bio_add_folio(*bio, folio, bio_len, bio_offset)) {
> ret = 0;
> break;
> }
> @@ -1007,6 +1014,10 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
> struct folio *data_folio = fio->encrypted_page ?
> page_folio(fio->encrypted_page) : fio->folio;
> struct folio *folio = fio->folio;
> + pgoff_t fio_lblk = F2FS_FIO_LBLK(folio, fio);
> + unsigned int fio_cnt = F2FS_FIO_BLKCNT(fio);
> + size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> + size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
>
> if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
> __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
> @@ -1021,9 +1032,10 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
> if (!bio) {
> bio = __bio_alloc(fio, BIO_MAX_VECS);
> f2fs_set_bio_crypt_ctx(bio, folio->mapping->host,
> - folio->index, fio, GFP_NOIO);
> + fio_lblk, fio, GFP_NOIO);
>
> - add_bio_entry(fio->sbi, bio, data_folio, fio->temp);
> + add_bio_entry(fio->sbi, bio, data_folio, bio_len,
> + bio_offset, fio->temp);
> } else {
> if (add_ipu_page(fio, &bio, data_folio))
> goto alloc_new;
> @@ -1034,7 +1046,7 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
>
> inc_page_count(fio->sbi, WB_DATA_TYPE(folio, false));
>
> - *fio->last_block = fio->new_blkaddr;
> + *fio->last_block = fio->new_blkaddr + fio_cnt - 1;
> *fio->bio = bio;
>
> return 0;
> @@ -1070,6 +1082,10 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
> struct folio *bio_folio;
> struct f2fs_lock_context lc;
> enum count_type type;
> + pgoff_t fio_lblk;
> + unsigned int fio_cnt;
> + size_t bio_offset;
> + size_t bio_len;
>
> f2fs_bug_on(sbi, is_read_io(fio->op));
>
> @@ -1108,6 +1124,9 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
> /* set submitted = true as a return value */
> fio->submitted = 1;
>
> + fio_lblk = F2FS_FIO_LBLK(fio->folio, fio);
> + fio_cnt = F2FS_FIO_BLKCNT(fio);
> +
> type = WB_DATA_TYPE(bio_folio, fio->compressed_page);
> inc_page_count(sbi, type);
>
> @@ -1115,26 +1134,28 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
> (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
> fio->new_blkaddr) ||
> !f2fs_crypt_mergeable_bio(io->bio, fio_inode(fio),
> - bio_folio->index, fio)))
> + fio_lblk, fio)))
> __submit_merged_bio(io);
> alloc_new:
> if (io->bio == NULL) {
> io->bio = __bio_alloc(fio, BIO_MAX_VECS);
> f2fs_set_bio_crypt_ctx(io->bio, fio_inode(fio),
> - bio_folio->index, fio, GFP_NOIO);
> + fio_lblk, fio, GFP_NOIO);
> io->fio = *fio;
> }
>
> - if (!bio_add_folio(io->bio, bio_folio, folio_size(bio_folio), 0)) {
> + bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> + bio_len = F2FS_FIO_BIO_SIZE(fio);
> +
> + if (!bio_add_folio(io->bio, bio_folio, bio_len, bio_offset)) {
> __submit_merged_bio(io);
> goto alloc_new;
> }
>
> if (fio->io_wbc)
> - wbc_account_cgroup_owner(fio->io_wbc, fio->folio,
> - folio_size(fio->folio));
> + wbc_account_cgroup_owner(fio->io_wbc, fio->folio, bio_len);
>
> - io->last_block_in_bio = fio->new_blkaddr;
> + io->last_block_in_bio = fio->new_blkaddr + fio_cnt - 1;
>
> trace_f2fs_submit_folio_write(fio->folio, fio);
> #ifdef CONFIG_BLK_DEV_ZONED
> @@ -3039,7 +3060,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
> set_new_dnode(&dn, inode, NULL, NULL, 0);
>
> if (need_inplace_update(fio) &&
> - f2fs_lookup_read_extent_cache_block(inode, folio->index,
> + f2fs_lookup_read_extent_cache_block(inode,
> + F2FS_FIO_LBLK(folio, fio),
> &fio->old_blkaddr)) {
> if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
> DATA_GENERIC_ENHANCE))
> @@ -3058,7 +3080,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
> if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi, &lc))
> return -EAGAIN;
>
> - err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
> + err = f2fs_get_dnode_of_data(&dn, F2FS_FIO_LBLK(folio, fio),
> + LOOKUP_NODE);
> if (err)
> goto out;
>
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 2e8f85cea6d0..31ddd321daf6 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -1357,6 +1357,8 @@ struct f2fs_io_info {
> blk_opf_t op_flags; /* req_flag_bits */
> block_t new_blkaddr; /* new block address to be written */
> block_t old_blkaddr; /* old block address before Cow */
> + pgoff_t folio_offset; /* offset in large folio */
> + unsigned int folio_blkcnt; /* block count in large folio */
> union {
> struct page *page; /* page to be written */
> struct folio *folio;
> @@ -1378,6 +1380,15 @@ struct f2fs_io_info {
> sector_t *last_block; /* last block number in bio */
> };
>
> +#define F2FS_FIO_LBLK(folio, fio) \
> + ((folio)->index + (fio)->folio_offset)
#define F2FS_FIO_LBLK(fio) \
(((fio)->folio)->index + (fio)->folio_offset)
> +#define F2FS_FIO_BLKCNT(fio) \
> + ((fio)->folio_blkcnt ? (fio)->folio_blkcnt : 1)
Hmm, it's better to not leaving fio.folio_blkcnt as zero, can we set
fio.folio_blkcnt to 1 by default? and use fio->folio_blkcnt.
Thanks,
> +#define F2FS_FIO_BIO_OFFSET(fio) \
> + ((fio)->folio_offset << PAGE_SHIFT)
> +#define F2FS_FIO_BIO_SIZE(fio) \
> + F2FS_BLK_TO_BYTES(F2FS_FIO_BLKCNT(fio))
> +
> struct bio_entry {
> struct bio *bio;
> struct list_head list;
> diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> index df10119d94ad..5bb1e51a38a0 100644
> --- a/fs/f2fs/segment.c
> +++ b/fs/f2fs/segment.c
> @@ -3816,7 +3816,8 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
> if (file_is_cold(inode) || f2fs_need_compress_data(inode))
> return CURSEG_COLD_DATA;
>
> - type = __get_age_segment_type(inode, fio->folio->index);
> + type = __get_age_segment_type(inode,
> + F2FS_FIO_LBLK(fio->folio, fio));
> if (type != NO_CHECK_TYPE)
> return type;
>
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 03/14] f2fs: support regular file buffered writes on large folios
2026-08-26 8:26 ` [PATCH 03/14] f2fs: support regular file buffered writes on large folios Nanzhe Zhao
@ 2026-08-27 8:56 ` Chao Yu
2026-08-27 21:13 ` [f2fs-dev] " Daeho Jeong
0 siblings, 1 reply; 29+ messages in thread
From: Chao Yu @ 2026-08-27 8:56 UTC (permalink / raw)
To: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim
Cc: chao, Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain,
linux-kernel, David Hildenbrand, Bo Zhang, Kalesh Singh,
Ryan Roberts, Pengfei Li
On 8/26/26 16:26, Nanzhe Zhao wrote:
> To avoid the complexity of unlocking a large folio in write_begin,
> preallocate partial blocks for inodes that can use large folios.
> During write_begin, read only the partial head and tail 4K subpages
> that need read-before-write, and skip read I/O for the full middle
> subpages covered by the write.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++--
> fs/f2fs/f2fs.h | 1 +
> fs/f2fs/file.c | 17 ++-
> 3 files changed, 293 insertions(+), 14 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 8649d443ca74..7ce96ae02cfc 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -1249,6 +1249,40 @@ static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
> f2fs_submit_read_bio(sbi, bio, DATA);
> }
>
> +/*
> + * Synchronously read a single 4K subpage by reusing f2fs_submit_page_read()
> + * so that iostat, trace, blk-crypto and post-read handling are all preserved.
> + * The caller must have already allocated ffs for the folio.
> + */
> +static int f2fs_submit_page_read_sync(struct inode *inode, struct folio *folio,
> + pgoff_t index, block_t blkaddr)
> +{
> + struct f2fs_folio_state *ffs = folio->private;
> + unsigned long flags;
> +
> + /* Add bias so end_io does not call folio_end_read(). */
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + ffs->read_pages_pending++;
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
> +
> + f2fs_submit_page_read(inode, NULL, folio, index, blkaddr,
> + REQ_OP_READ, false);
> +
> + /* Wait for pending drops back to bias which indicates all bio have completed. */
> + while (READ_ONCE(ffs->read_pages_pending) != 1)
> + f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
Suspect there will be potential performance issue, as timeout interval is
large.
> +
> + /* Remove the bias. */
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + ffs->read_pages_pending--;
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
Looks hacky.
If large folio has 4 sub pages, first 3 pages are uptodate.
Then we use f2fs_submit_page_read_sync() to read last page,
in f2fs_finish_read_bio(), we update state bitmap of last page to 1,
but as read_pages_pending is 1, so folio won't be set as uptodate.
Oh, in f2fs_write_end(), we have another change to set folio uptodate
though, so the flow is a little bit different here.
> +
> + if (!f2fs_ffs_test_blk_uptodate(folio, index))
> + return -EIO;
> +
> + return 0;
> +}
> +
> static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
> {
> __le32 *addr = get_dnode_addr(dn->inode, dn->node_folio);
> @@ -2582,6 +2616,112 @@ static void ffs_detach_free(struct folio *folio)
> kfree(ffs);
> }
>
> +bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index)
> +{
> + struct f2fs_folio_state *ffs;
> + unsigned int idx;
> +
> + if (!f2fs_folio_has_ffs(folio))
> + return folio_test_uptodate(folio);
> +
> + if (!folio_contains(folio, index))
> + return false;
Can we check this out of f2fs_ffs_test_blk_uptodate()? and add a sanity
check here:
f2fs_bug_on(sbi, !folio_contains(folio, index));
> +
> + ffs = (struct f2fs_folio_state *)folio->private;
> + idx = index - folio->index;
> + return test_bit(idx, ffs->state);
> +}
> +
> +static bool __ffs_mark_subrange_uptodate(struct folio *folio,
> + struct f2fs_folio_state *ffs, size_t offset, size_t len)
> +{
> + unsigned int nr_subpages = folio_nr_pages(folio);
> + unsigned int start, end;
> +
> + start = offset >> PAGE_SHIFT;
> + end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> + end = min(end, nr_subpages);
> +
> + bitmap_set(ffs->state, start, end - start);
> + return bitmap_full(ffs->state, nr_subpages);
> +}
> +
> +static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
> + size_t len)
> +{
> + struct f2fs_folio_state *ffs;
> + unsigned long flags;
> + bool mark_uptodate = false;
> +
> + f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
> +
> + if (!f2fs_folio_has_ffs(folio)) {
> + folio_mark_uptodate(folio);
> + return;
> + }
> +
> + ffs = (struct f2fs_folio_state *)folio->private;
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + mark_uptodate = __ffs_mark_subrange_uptodate(folio, ffs, offset, len) &&
> + !ffs->read_pages_pending;
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
> + if (mark_uptodate)
> + folio_mark_uptodate(folio);
> +}
> +
> +static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> + size_t offset, size_t len)
> +{
> + struct f2fs_folio_state *ffs;
> + unsigned int nr_subpages, start, end;
> + unsigned long flags;
> +
> + f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
> +
> + if (!f2fs_folio_has_ffs(folio))
> + return;
> +
> + ffs = (struct f2fs_folio_state *)folio->private;
> + nr_subpages = folio_nr_pages(folio);
> + start = offset >> PAGE_SHIFT;
> + end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> + end = min(end, nr_subpages);
> +
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + bitmap_set(ffs->state, nr_subpages + start, end - start);
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
> +}
> +
> +static bool find_next_valid_block(const struct folio *folio,
> + size_t orig_off, size_t *need_off,
> + size_t len)
> +{
> + size_t start = orig_off;
> + size_t end = start + len;
> + size_t head, tail;
> + pgoff_t index;
> +
> + if (start & (PAGE_SIZE - 1)) {
> + head = round_down(start, PAGE_SIZE);
> + index = folio->index + (head >> PAGE_SHIFT);
> + if (!f2fs_ffs_test_blk_uptodate(folio, index)) {
> + *need_off = head;
> + return true;
> + }
> + }
> +
> + if (end & (PAGE_SIZE - 1)) {
> + tail = round_down(end - 1, PAGE_SIZE);
> + index = folio->index + (tail >> PAGE_SHIFT);
> + if (!f2fs_ffs_test_blk_uptodate(folio, index)) {
> + *need_off = tail;
> + return true;
> + }
> + }
> +
> + return false;
> +}
> +
> static int f2fs_read_data_large_folio(struct inode *inode,
> struct fsverity_info *vi,
> struct readahead_control *rac, struct folio *folio)
> @@ -3975,6 +4115,102 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
> return 0;
> }
>
> +static int prepare_large_folio_write_begin(struct inode *inode,
> + struct folio *folio, loff_t pos,
> + unsigned int len)
> +{
> + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> + struct f2fs_folio_state *ffs;
> + size_t ori_off = offset_in_folio(folio, pos);
> + size_t need_off = ori_off;
> + int err = 0;
> +
> + len = min_t(unsigned int, len, folio_size(folio) - ori_off);
> +
> + /*
> + * When folio minimum order is non-zero, the fsverity
> + * page_cache_write() path enters f2fs_write_begin() via
> + * aops->write_begin without going through f2fs_write_iter(),
> + * so preallocation from f2fs_write_iter() is skipped. In that
> + * case, if FI_PREALLOCATED_ALL is not set, we must preallocate
> + * the write blocks here.
> + */
> + if (!is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) {
> + struct f2fs_map_blocks map = {};
> +
> + map.m_lblk = F2FS_BYTES_TO_BLK(pos);
> + map.m_len = F2FS_BLK_ALIGN(pos + len) - map.m_lblk;
> +
> + if (!IS_DEVICE_ALIASING(inode))
> + map.m_may_create = true;
> + map.m_seg_type = NO_CHECK_TYPE;
> +
> + err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
> + if (err)
> + return err;
> + }
> +
> + /*
> + * Skip read and ffs allocation if the folio is already fully
> + * uptodate or the write covers the entire folio.
> + */
> + if (folio_test_uptodate(folio) || len == folio_size(folio))
> + return 0;
Do we need to adopt fsverity like we did in f2fs_write_begin()?
if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
!f2fs_verity_in_progress(inode)) {
folio_zero_segment(folio, len, folio_size(folio));
return 0;
}
> +
> + ffs = f2fs_ffs_find_or_alloc(folio);
f2fs_ffs_find_or_alloc() will never fail.
> + if (!ffs)
> + return -ENOMEM;
> +
> + /* Inline data must have been converted before reaching here. */
> + f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
> +
> + while (find_next_valid_block(folio, ori_off,
> + &need_off, len)) {
> + struct dnode_of_data dn;
> + pgoff_t index = folio->index + (need_off >> PAGE_SHIFT);
> + block_t blkaddr;
> + bool get_dn = false;
> +
> + if (!f2fs_lookup_read_extent_cache_block(inode, index,
> + &blkaddr)) {
> + if (IS_DEVICE_ALIASING(inode))
> + return -ENODATA;
> +
> + set_new_dnode(&dn, inode, NULL, NULL, 0);
> + err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
> + if (err)
> + return err;
> + get_dn = true;
> + blkaddr = dn.data_blkaddr;
> +
> + if (blkaddr == NEW_ADDR) {
> + size_t off = offset_in_folio(folio,
> + index << PAGE_SHIFT);
> +
> + folio_zero_segment(folio, off, off + PAGE_SIZE);
> + f2fs_ffs_mark_subrange_uptodate(folio, off,
> + PAGE_SIZE);
> + goto out;
> + }
> +
> + if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
> + DATA_GENERIC_ENHANCE_READ)) {
> + err = -EFSCORRUPTED;
> + goto out;
> + }
> + }
> +
> + err = f2fs_submit_page_read_sync(inode, folio, index, blkaddr);
> +out:
> + if (get_dn)
> + f2fs_put_dnode(&dn);
> + if (err)
> + return err;
> + }
> +
> + return 0;
> +}
> +
> static int f2fs_write_begin(const struct kiocb *iocb,
> struct address_space *mapping,
> loff_t pos, unsigned len, struct folio **foliop,
> @@ -3986,6 +4222,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> pgoff_t index = pos >> PAGE_SHIFT;
> bool need_balance = false;
> block_t blkaddr = NULL_ADDR;
> + fgf_t fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT;
> int err = 0;
>
> trace_f2fs_write_begin(inode, pos, len);
> @@ -4033,9 +4270,9 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> * Do not use FGP_STABLE to avoid deadlock.
> * Will wait that below with our IO control.
> */
> - folio = f2fs_filemap_get_folio(mapping, index,
> - FGP_LOCK | FGP_WRITE | FGP_CREAT,
> - mapping_gfp_mask(mapping));
> + fgp |= fgf_set_order(len);
> + folio = __filemap_get_folio(mapping, index, fgp,
> + mapping_gfp_mask(mapping));
It detaches from fault injection, why we need to change to use
__filemap_get_folio()?
> if (IS_ERR(folio)) {
> err = PTR_ERR(folio);
> goto fail;
> @@ -4048,7 +4285,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> if (f2fs_is_atomic_file(inode))
> err = prepare_atomic_write_begin(sbi, folio, pos, len,
> &blkaddr, &need_balance);
> - else
> + else if (!folio_test_large(folio))
> err = prepare_write_begin(sbi, folio, pos, len,
> &blkaddr, &need_balance);
> if (err)
> @@ -4069,6 +4306,14 @@ static int f2fs_write_begin(const struct kiocb *iocb,
>
> f2fs_folio_wait_writeback(folio, DATA, false, true);
>
> + if (folio_test_large(folio)) {
> + err = prepare_large_folio_write_begin(inode,
> + folio, pos, len);
> + if (!err)
> + return 0;
> + goto put_folio;
> + }
> +
> if (len == folio_size(folio) || folio_test_uptodate(folio))
> return 0;
>
> @@ -4129,15 +4374,20 @@ static int f2fs_write_end(const struct kiocb *iocb,
> trace_f2fs_write_end(inode, pos, len, copied);
>
> /*
> - * This should be come from len == PAGE_SIZE, and we expect copied
> - * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
> - * let generic_perform_write() try to copy data again through copied=0.
> + * If a short copy happens on a folio that isn't uptodate, we treat
> + * it with zero copied and let generic_perform_write() try to copy
> + * data again through copied=0.
> */
> if (!folio_test_uptodate(folio)) {
> - if (unlikely(copied != len))
> + if (unlikely(copied != len)) {
> copied = 0;
> - else
> + } else if (folio_test_large(folio)) {
> + f2fs_ffs_mark_subrange_uptodate(folio,
> + offset_in_folio(folio, pos), len);
> + } else {
> + /* This should be come from len == PAGE_SIZE */
> folio_mark_uptodate(folio);
> + }
> }
>
> #ifdef CONFIG_F2FS_FS_COMPRESSION
> @@ -4156,6 +4406,9 @@ static int f2fs_write_end(const struct kiocb *iocb,
> if (!copied)
> goto unlock_out;
>
> + if (folio_test_large(folio))
> + f2fs_ffs_mark_subrange_dirty(folio, offset_in_folio(folio, pos),
> + copied);
> folio_mark_dirty(folio);
>
> if (f2fs_is_atomic_file(inode))
> @@ -4218,8 +4471,22 @@ static bool f2fs_dirty_data_folio(struct address_space *mapping,
>
> trace_f2fs_set_page_dirty(folio, DATA);
>
> - if (!folio_test_uptodate(folio))
> - folio_mark_uptodate(folio);
> + if (!folio_test_uptodate(folio)) {
> + bool uptodate = true;
> +
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs =
> + (struct f2fs_folio_state *)folio->private;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + uptodate = bitmap_full(ffs->state, folio_nr_pages(folio)) &&
> + !ffs->read_pages_pending;
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
f2fs_ffs_mark_subrange_uptodate(folio, 0, folio_size(folio)); ?
> + }
> + if (uptodate)
> + folio_mark_uptodate(folio);
> + }
> BUG_ON(folio_test_swapcache(folio));
>
> if (filemap_dirty_folio(mapping, folio)) {
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 31ddd321daf6..ae6031693700 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -4342,6 +4342,7 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> struct writeback_control *wbc,
> enum iostat_type io_type,
> int compr_blocks, bool allow_balance);
> +bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
> void f2fs_write_failed(struct inode *inode, loff_t to);
> void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
> bool f2fs_release_folio(struct folio *folio, gfp_t wait);
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index d440231b8cb9..e40285d4f9c2 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -5405,9 +5405,20 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
> return ret;
> }
>
> - /* Do not preallocate blocks that will be written partially in 4KB. */
> - map.m_lblk = F2FS_BLK_ALIGN(pos);
> - map.m_len = F2FS_BYTES_TO_BLK(pos + count);
> + if (mapping_large_folio_support(inode->i_mapping)) {
> + /*
> + * Preallocate all blocks touched by a large-folio buffered write so
> + * the regular write_begin path does not need to unlock the folio for
> + * f2fs_balance_fs(). Rechecking large-folio state after unlock is
> + * unreliable since partial truncation can split the folio.
You mean buffered io case, right? so we need to check !dio as well?
Thanks,
> + */
> + map.m_lblk = F2FS_BYTES_TO_BLK(pos);
> + map.m_len = F2FS_BLK_ALIGN(pos + count);
> + } else {
> + /* Do not preallocate blocks that will be written partially in 4KB. */
> + map.m_lblk = F2FS_BLK_ALIGN(pos);
> + map.m_len = F2FS_BYTES_TO_BLK(pos + count);
> + }
> if (map.m_len > map.m_lblk)
> map.m_len -= map.m_lblk;
> else
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 04/14] f2fs: support atomic file large folios buffered write
2026-08-26 8:26 ` [PATCH 04/14] f2fs: support atomic file large folios buffered write Nanzhe Zhao
@ 2026-08-27 9:24 ` Chao Yu
0 siblings, 0 replies; 29+ messages in thread
From: Chao Yu @ 2026-08-27 9:24 UTC (permalink / raw)
To: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim
Cc: chao, Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain,
linux-kernel, David Hildenbrand, Bo Zhang, Kalesh Singh,
Ryan Roberts, Pengfei Li
On 8/26/26 16:26, Nanzhe Zhao wrote:
> ioctl can convert an inode with large folio support into an atomic
> file. Support large folio buffered writes for atomic files as well.
>
> Add a large folio atomic write_begin helper that reserves COW mappings
> for the write range. For partial head and tail subpages, read the
> existing data from either the COW inode or the original inode before
> marking the subpage uptodate.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 121 insertions(+), 2 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 7ce96ae02cfc..dff802725544 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -4211,6 +4211,117 @@ static int prepare_large_folio_write_begin(struct inode *inode,
> return 0;
> }
>
> +static int prepare_large_folio_atomic_write_begin(struct inode *inode,
> + struct address_space *mapping, struct folio *folio, loff_t pos,
> + unsigned int len)
> +{
> + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> + struct inode *cow_inode = F2FS_I(inode)->cow_inode;
> + size_t ori_off = offset_in_folio(folio, pos);
> + size_t need_off = ori_off;
> + pgoff_t index;
> + int err = 0;
> + unsigned int orig_order;
> + bool need_balance = false;
> +
> + len = min_t(unsigned int, len, folio_size(folio) - ori_off);
> +
> + f2fs_ffs_find_or_alloc(folio);
> +
> + /* Inline data must have been converted before reaching here. */
> + f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
> +
> + /* Reserve COW blocks for all covered 4K subpages first. */
> + pgoff_t start_index = folio->index + (ori_off >> PAGE_SHIFT);
> + pgoff_t end_index = folio->index +
> + ((ori_off + len - 1) >> PAGE_SHIFT);
> +
> + for (index = start_index; index <= end_index; index++) {
> + block_t cow_blkaddr = NULL_ADDR;
> + bool node_changed = false;
> + int ret;
> +
> + ret = __find_data_block(cow_inode, index, &cow_blkaddr);
> + if (ret)
> + return ret;
> + if (cow_blkaddr != NULL_ADDR)
> + continue;
> +
> + ret = __reserve_data_block(cow_inode, index, &cow_blkaddr,
> + &node_changed);
> + if (ret)
> + return ret;
> +
> + inc_atomic_write_cnt(inode);
In order to not miss any logic in prepare_atomic_write_begin(), can you reuse
prepare_atomic_write_begin() as much as possible? needs to change parameters
a bit there.
for (...) {
err = prepare_atomic_write_begin(sbi, inode, index, pos, len, ...);
...
}
> + need_balance |= node_changed;
> + }
> +
> + if (need_balance && !IS_NOQUOTA(inode) &&
> + has_not_enough_free_secs(sbi, 0, 0)) {
> + orig_order = folio_order(folio);
> + folio_unlock(folio);
> + f2fs_balance_fs(sbi, true);
> + folio_lock(folio);
> + if (unlikely(folio->mapping != mapping ||
> + folio_order(folio) != orig_order))
> + return -EAGAIN;
> + }
> +
> + if (folio_test_uptodate(folio) || len == folio_size(folio))
> + return 0;
> +
> + /* Then read partial 4K subpages. */
> + while (find_next_valid_block(folio, ori_off, &need_off, len)) {
Can we record first and last blkaddr in first loop of
prepare_large_folio_atomic_write_begin(), and then we can avoid lot of complexity
below to find target blkaddr?
We really don't want to miss any corner case of atomic write + large folio, please
consider that.
Thanks,
> + size_t off;
> + block_t cow_blkaddr = NULL_ADDR;
> + block_t ori_blkaddr = NULL_ADDR;
> + struct inode *read_inode = NULL;
> + block_t read_blkaddr = NULL_ADDR;
> +
> + index = folio->index + (need_off >> PAGE_SHIFT);
> + off = offset_in_folio(folio, index << PAGE_SHIFT);
> +
> + err = __find_data_block(cow_inode, index, &cow_blkaddr);
> + if (err)
> + return err;
> +
> + if (__is_valid_data_blkaddr(cow_blkaddr)) {
> + if (!f2fs_is_valid_blkaddr(sbi, cow_blkaddr,
> + DATA_GENERIC_ENHANCE_READ))
> + return -EFSCORRUPTED;
> + read_inode = cow_inode;
> + read_blkaddr = cow_blkaddr;
> + } else if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) {
> + folio_zero_segment(folio, off, off + PAGE_SIZE);
> + f2fs_ffs_mark_subrange_uptodate(folio, off, PAGE_SIZE);
> + continue;
> + } else {
> + err = __find_data_block(inode, index, &ori_blkaddr);
> + if (err)
> + return err;
> +
> + if (!__is_valid_data_blkaddr(ori_blkaddr)) {
> + folio_zero_segment(folio, off, off + PAGE_SIZE);
> + f2fs_ffs_mark_subrange_uptodate(folio, off, PAGE_SIZE);
> + continue;
> + }
> +
> + if (!f2fs_is_valid_blkaddr(sbi, ori_blkaddr,
> + DATA_GENERIC_ENHANCE_READ))
> + return -EFSCORRUPTED;
> + read_inode = inode;
> + read_blkaddr = ori_blkaddr;
> + }
> +
> + err = f2fs_submit_page_read_sync(read_inode, folio,
> + index, read_blkaddr);
> + if (err)
> + return err;
> + }
> +
> + return 0;
> +}
> +
> static int f2fs_write_begin(const struct kiocb *iocb,
> struct address_space *mapping,
> loff_t pos, unsigned len, struct folio **foliop,
> @@ -4282,7 +4393,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
>
> *foliop = folio;
>
> - if (f2fs_is_atomic_file(inode))
> + if (f2fs_is_atomic_file(inode) && !folio_test_large(folio))
> err = prepare_atomic_write_begin(sbi, folio, pos, len,
> &blkaddr, &need_balance);
> else if (!folio_test_large(folio))
> @@ -4307,10 +4418,18 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> f2fs_folio_wait_writeback(folio, DATA, false, true);
>
> if (folio_test_large(folio)) {
> - err = prepare_large_folio_write_begin(inode,
> + if (f2fs_is_atomic_file(inode))
> + err = prepare_large_folio_atomic_write_begin(inode,
> + mapping, folio, pos, len);
> + else
> + err = prepare_large_folio_write_begin(inode,
> folio, pos, len);
> if (!err)
> return 0;
> + if (err == -EAGAIN) {
> + f2fs_folio_put(folio, true);
> + goto repeat;
> + }
> goto put_folio;
> }
>
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 05/14] f2fs: support large folio writeback
2026-08-26 8:26 ` [PATCH 05/14] f2fs: support large folio writeback Nanzhe Zhao
@ 2026-08-27 11:17 ` Chao Yu
2026-08-27 22:39 ` [f2fs-dev] " Daeho Jeong
0 siblings, 1 reply; 29+ messages in thread
From: Chao Yu @ 2026-08-27 11:17 UTC (permalink / raw)
To: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim
Cc: chao, Barry Song, Nanzhe Zhao, Juan Yescas, Dev Jain,
linux-kernel, David Hildenbrand, Bo Zhang, Kalesh Singh,
Ryan Roberts, Pengfei Li
On 8/26/26 16:26, Nanzhe Zhao wrote:
> Large folio can contain multiple dirty ranges.
> Add a folio-based writeback path for large-folio mapping files
> and keep the legacy f2fs_write_cache_pages() path unchanged for
> non large-folio mapping files.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 398 insertions(+), 6 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index dff802725544..608d8ea8e607 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -358,6 +358,7 @@ static void f2fs_write_end_bio(struct bio *bio)
> struct folio *folio = fi.folio;
> unsigned int nr_pages = fi.length >> PAGE_SHIFT;
> enum count_type type;
> + bool finished = true;
>
> if (fscrypt_is_bounce_folio(folio)) {
> struct folio *io_folio = folio;
> @@ -388,11 +389,20 @@ static void f2fs_write_end_bio(struct bio *bio)
> folio->index, NODE_TYPE_REGULAR, true);
> f2fs_bug_on(sbi, folio->index != nid_of_node(folio));
> }
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs =
> + (struct f2fs_folio_state *)folio->private;
> +
> + finished = atomic_sub_and_test(nr_pages,
> + &ffs->write_pages_pending);
> + }
> +
> + while (nr_pages--)
> + dec_page_count(sbi, type);
> +
> if (f2fs_in_warm_node_list(folio))
> f2fs_del_fsync_node_entry(sbi, folio);
>
> - dec_page_count(sbi, type);
We should not relocate dec_page_count() above f2fs_in_warm_node_list() to
avoid racing bugs.
> -
> /*
> * we should access sbi before folio_end_writeback() to
> * avoid racing w/ kill_f2fs_super()
> @@ -401,8 +411,10 @@ static void f2fs_write_end_bio(struct bio *bio)
> wq_has_sleeper(&sbi->cp_wait))
> wake_up(&sbi->cp_wait);
>
> - folio_clear_f2fs_gcing(folio);
> - folio_end_writeback(folio);
> + if (finished) {
> + folio_clear_f2fs_gcing(folio);
> + folio_end_writeback(folio);
> + }
> }
>
> bio_put(bio);
> @@ -2669,7 +2681,7 @@ static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
> folio_mark_uptodate(folio);
> }
>
> -static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> +void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> size_t offset, size_t len)
> {
> struct f2fs_folio_state *ffs;
> @@ -2692,6 +2704,86 @@ static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> spin_unlock_irqrestore(&ffs->state_lock, flags);
> }
>
> +static bool __ffs_clear_subrange_dirty(struct folio *folio,
> + struct f2fs_folio_state *ffs, size_t offset, size_t len)
> +{
> + unsigned int nr_subpages = folio_nr_pages(folio);
> + unsigned int start, end;
> +
> + start = offset >> PAGE_SHIFT;
> + end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> + end = min(end, nr_subpages);
> +
> + bitmap_clear(ffs->state, nr_subpages + start, end - start);
> + return find_next_bit(ffs->state, 2 * nr_subpages, nr_subpages) <
> + 2 * nr_subpages;
> +}
> +
> +void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len)
> +{
> + struct f2fs_folio_state *ffs;
> + unsigned long flags;
> +
> + if (!f2fs_folio_has_ffs(folio))
> + return;
> +
> + ffs = (struct f2fs_folio_state *)folio->private;
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + __ffs_clear_subrange_dirty(folio, ffs, offset, len);
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
> +}
> +
> +static unsigned int ffs_next_dirty_subpage(struct f2fs_folio_state *ffs,
> + const struct folio *folio, unsigned int start,
> + unsigned int end)
> +{
> + unsigned int nr_subpages = folio_nr_pages(folio);
> +
> + return find_next_bit(ffs->state, nr_subpages + end + 1,
> + nr_subpages + start) - nr_subpages;
> +}
> +
> +static unsigned int ffs_next_clean_subpage(struct f2fs_folio_state *ffs,
> + const struct folio *folio, unsigned int start,
> + unsigned int end)
> +{
> + unsigned int nr_subpages = folio_nr_pages(folio);
> +
> + return find_next_zero_bit(ffs->state, nr_subpages + end + 1,
> + nr_subpages + start) - nr_subpages;
> +}
> +
> +static unsigned int ffs_find_dirty_range(struct folio *folio,
> + u64 *range_start, u64 range_end)
> +{
> + struct f2fs_folio_state *ffs;
> + unsigned int start, end, nr_pages;
> +
> + if (*range_start >= range_end)
> + return 0;
> +
> + if (!f2fs_folio_has_ffs(folio))
> + return range_end - *range_start;
> +
> + ffs = (struct f2fs_folio_state *)folio->private;
> + start = offset_in_folio(folio, *range_start) >> PAGE_SHIFT;
> + end = DIV_ROUND_UP(min_not_zero(offset_in_folio(folio, range_end),
> + folio_size(folio)), PAGE_SIZE) - 1;
> +
> + start = ffs_next_dirty_subpage(ffs, folio, start, end);
> + if (start > end)
> + return 0;
> +
> + if (start == end)
> + nr_pages = 1;
> + else
> + nr_pages = ffs_next_clean_subpage(ffs, folio,
> + start + 1, end) - start;
> +
> + *range_start = folio_pos(folio) + ((u64)start << PAGE_SHIFT);
> + return (u64)nr_pages << PAGE_SHIFT;
> +}
> +
> static bool find_next_valid_block(const struct folio *folio,
> size_t orig_off, size_t *need_off,
> size_t len)
> @@ -3309,6 +3401,139 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
> return err;
> }
>
> +static int f2fs_write_single_data_folio(struct folio *folio, int *submitted,
> + struct writeback_control *wbc,
> + enum iostat_type io_type,
> + u64 start, u64 end)
> +{
> + struct inode *inode = folio->mapping->host;
> + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> + bool atomic_commit = f2fs_is_atomic_file(inode) &&
> + folio_test_f2fs_atomic(folio);
> + struct inode *dn_inode = atomic_commit ?
> + F2FS_I(inode)->cow_inode : inode;
> + u64 pos = folio_pos(folio);
> + pgoff_t start_idx = (start - pos) >> PAGE_SHIFT;
> + pgoff_t end_idx = (end - 1 - pos) >> PAGE_SHIFT;
> + int local_submitted = 0;
> + int err = 0;
> +
> + for (pgoff_t i = start_idx; i <= end_idx; i++) {
> + struct dnode_of_data dn;
> + struct node_info ni;
> + pgoff_t data_idx = folio->index + i;
> + bool ipu_force = false;
> + struct f2fs_io_info fio = {
> + .sbi = sbi,
> + .ino = inode->i_ino,
> + .type = DATA,
> + .op = REQ_OP_WRITE,
> + .op_flags = wbc_to_write_flags(wbc),
> + .old_blkaddr = NULL_ADDR,
> + .folio = folio,
> + .folio_offset = i,
> + .folio_blkcnt = 1,
> + .encrypted_page = NULL,
> + .submitted = 0,
> + .need_lock = LOCK_DONE,
> + .meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0,
> + .io_type = io_type,
> + .io_wbc = wbc,
> + };
> +
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs =
> + (struct f2fs_folio_state *)folio->private;
> +
> + atomic_inc(&ffs->write_pages_pending);
> + }
> +
> + set_new_dnode(&dn, dn_inode, NULL, NULL, 0);
> +
> + if (!atomic_commit && need_inplace_update(&fio) &&
In f2fs_write_single_data_page(), we didn't have !atomic_commit condition
in the check? can we drop !atomic_commit check?
need_inplace_update() has checked w/ f2fs_used_in_atomic_write().
- need_inplace_update
- f2fs_should_update_outplace
- f2fs_used_in_atomic_write
static inline bool f2fs_used_in_atomic_write(struct inode *inode)
{
return f2fs_is_atomic_file(inode) || f2fs_is_cow_file(inode);
}
> + f2fs_lookup_read_extent_cache_block(inode, data_idx,
> + &fio.old_blkaddr)) {
> + if (!f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
> + DATA_GENERIC_ENHANCE)) {
> + err = -EFSCORRUPTED;
> + goto rollback;
> + }
> + ipu_force = true;
> + goto got_it;
> + }
> +
> + err = f2fs_get_dnode_of_data(&dn, data_idx, LOOKUP_NODE);
> + if (err)
> + goto rollback;
> +
> + fio.old_blkaddr = dn.data_blkaddr;
> +
> +got_it:
> + if (__is_valid_data_blkaddr(fio.old_blkaddr) &&
> + !f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
> + DATA_GENERIC_ENHANCE)) {
> + err = -EFSCORRUPTED;
> + goto rollback;
> + }
> +
> + if (fio.meta_gc)
> + f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
> +
> + if (!atomic_commit && (ipu_force ||
> + (__is_valid_data_blkaddr(fio.old_blkaddr) &&
> + need_inplace_update(&fio)))) {
> + err = f2fs_encrypt_one_page(&fio);
> + if (err)
> + goto rollback;
> +
> + f2fs_put_dnode(&dn);
> + err = f2fs_inplace_write_data(&fio);
> + if (err) {
> + if (fscrypt_inode_uses_fs_layer_crypto(inode))
> + fscrypt_finalize_bounce_page(
> + &fio.encrypted_page);
> + goto rollback_no_dnode;
> + }
> +
> + local_submitted++;
> + set_inode_flag(inode, FI_UPDATE_WRITE);
trace_f2fs_do_write_data_page(folio, IPU);
> + continue;
> + }
> +
> + err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
> + if (err)
> + goto rollback;
> +
> + fio.version = ni.version;
> +
> + err = f2fs_encrypt_one_page(&fio);
> + if (err)
> + goto rollback;
> +
> + f2fs_outplace_write_data(&dn, &fio);
> + local_submitted++;
> + set_inode_flag(inode, FI_APPEND_WRITE);
> + trace_f2fs_do_write_data_page(folio, OPU);
> + f2fs_put_dnode(&dn);
> + continue;
> +
> +rollback:
> + f2fs_put_dnode(&dn);
> +rollback_no_dnode:
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs =
> + (struct f2fs_folio_state *)folio->private;
> +
> + atomic_dec(&ffs->write_pages_pending);
> + }
> + break;
> + }
> +
> + if (submitted)
> + *submitted = local_submitted;
> + return err;
> +}
> +
> int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> struct bio **bio,
> sector_t *last_block,
> @@ -3757,6 +3982,170 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
> return ret;
> }
>
> +static int f2fs_write_cache_folios(struct address_space *mapping,
> + struct writeback_control *wbc,
> + enum iostat_type io_type)
> +{
> + struct folio *folio = NULL;
> + struct inode *inode = mapping->host;
> + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> + struct f2fs_lock_context lc;
> + u64 pos = 0;
> + u64 end_pos = 0;
> + u32 r_len = 0;
> + int err = 0;
> + int submitted = 0;
> + int nwritten = 0;
> + bool op_locked = false;
> + bool next = false;
> + bool retry = false;
> +
> + if (get_dirty_pages(inode) <= SM_I(sbi)->min_hot_blocks)
> + set_inode_flag(inode, FI_HOT_DATA);
> + else
> + clear_inode_flag(inode, FI_HOT_DATA);
> +
> + while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
> + struct f2fs_folio_state *ffs = NULL;
> + u64 isize;
> + size_t poff;
> + pgoff_t end_index;
> + bool verity_in_progress;
> + int folio_submitted = 0;
> + bool bias_added = false;
> +
> + submitted = 0;
> + next = true;
> + retry = false;
> +
> + if (atomic_read(&sbi->wb_sync_req[DATA]) &&
> + wbc->sync_mode == WB_SYNC_NONE) {
> + folio_redirty_for_writepage(wbc, folio);
> + next = false;
> + goto retry_out;
> + }
> +retry:
> + pos = folio_pos(folio);
> + end_pos = pos + folio_size(folio);
> + isize = i_size_read(inode);
> + verity_in_progress = f2fs_verity_in_progress(inode);
> + poff = 0;
> + end_index = 0;
> +
> + if (retry) {
> + if (unlikely(folio->mapping != mapping))
> + goto retry_out;
> +
> + if (!folio_test_dirty(folio))
> + goto retry_out;
> +
> + if (folio_test_writeback(folio)) {
> + if (wbc->sync_mode == WB_SYNC_NONE)
> + goto retry_out;
> + f2fs_folio_wait_writeback(folio, DATA, true, true);
> + }
> +
> + if (!folio_clear_dirty_for_io(folio))
> + goto retry_out;
> + }
> +
> + /* To avoid dealing with the complexity for one subrange is in bio
> + * while we trylock_op failed before writing another subrange.
> + * Try to lock_op before any subrange write for the folio.
> + */
> + if (!op_locked) {
> + if (!f2fs_trylock_op(sbi, &lc)) {
> + folio_redirty_for_writepage(wbc, folio);
> + err = 0;
> + if (wbc->sync_mode != WB_SYNC_ALL)
> + goto retry_out;
> +
> + retry = true;
> + folio_unlock(folio);
> + f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
> + folio_lock(folio);
> + goto retry;
> + }
> + op_locked = true;
> + }
> +
> + if (!verity_in_progress) {
> + poff = offset_in_folio(folio, isize);
> + end_index = isize >> PAGE_SHIFT;
> +
> + if (folio->index > end_index ||
> + (folio->index == end_index && poff == 0))
> + goto out;
> +
> + if (end_pos > isize) {
> + folio_zero_segment(folio, poff, folio_size(folio));
> + end_pos = isize;
> + }
> + }
> +
> + folio_start_writeback(folio);
> +
> + if (folio_test_large(folio)) {
> + if (!f2fs_folio_has_ffs(folio)) {
Can you explain why there is no ffs for a dirty large folio?
Thanks,
> + ffs = f2fs_ffs_find_or_alloc(folio);
> + f2fs_ffs_mark_subrange_dirty(folio, 0, end_pos - pos);
> + } else {
> + ffs = (struct f2fs_folio_state *)folio->private;
> + }
> + if (f2fs_folio_has_ffs(folio) && !bias_added) {
> + WARN_ON_ONCE(atomic_read(&ffs->write_pages_pending) != 0);
> + atomic_inc(&ffs->write_pages_pending);
> + bias_added = true;
> + }
> + }
> +
> + while ((r_len = ffs_find_dirty_range(folio, &pos, end_pos))) {
> + err = f2fs_write_single_data_folio(folio, &submitted,
> + wbc, io_type, pos, pos + r_len);
> + folio_submitted += submitted;
> + if (err)
> + goto out;
> +
> + nwritten += submitted;
> + pos += r_len;
> + }
> +
> + if (!err && folio_submitted &&
> + f2fs_is_atomic_file(inode) &&
> + folio_test_f2fs_atomic(folio))
> + folio_clear_f2fs_atomic(folio);
> +
> +out:
> + f2fs_ffs_clear_subrange_dirty(folio, 0, folio_size(folio));
> + inode_dec_dirty_pages(inode);
> +
> + if (bias_added) {
> + if (atomic_dec_and_test(&ffs->write_pages_pending))
> + folio_end_writeback(folio);
> + } else if (!folio_submitted && folio_test_writeback(folio)) {
> + folio_end_writeback(folio);
> + }
> +
> +retry_out:
> + if (folio_test_locked(folio))
> + folio_unlock(folio);
> +
> + if (op_locked) {
> + f2fs_unlock_op(sbi, &lc);
> + op_locked = false;
> + }
> +
> + if (err || !next)
> + break;
> + }
> +
> + if (nwritten)
> + f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
> + NULL, 0, DATA);
> +
> + return err;
> +}
> +
> static inline bool __should_serialize_io(struct inode *inode,
> struct writeback_control *wbc)
> {
> @@ -3851,7 +4240,10 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
> account_writeback(inode, true);
>
> blk_start_plug(&plug);
> - ret = f2fs_write_cache_pages(mapping, wbc, io_type);
> + if (mapping_large_folio_support(inode->i_mapping))
> + ret = f2fs_write_cache_folios(mapping, wbc, io_type);
> + else
> + ret = f2fs_write_cache_pages(mapping, wbc, io_type);
> blk_finish_plug(&plug);
>
> account_writeback(inode, false);
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [PATCH 06/14] f2fs: prepare mmap write faults for large folios
2026-08-26 8:26 ` [PATCH 06/14] f2fs: prepare mmap write faults for large folios Nanzhe Zhao
@ 2026-08-27 12:36 ` Chao Yu
2026-08-28 17:18 ` [f2fs-dev] " Daeho Jeong
0 siblings, 1 reply; 29+ messages in thread
From: Chao Yu @ 2026-08-27 12:36 UTC (permalink / raw)
To: Nanzhe Zhao, Barry Song
Cc: chao, Nanzhe Zhao, Juan Yescas, Dev Jain, linux-kernel,
David Hildenbrand, Bo Zhang, Kalesh Singh, Ryan Roberts,
Pengfei Li, linux-f2fs-devel, Jaegeuk Kim
On 8/26/26 16:26, Nanzhe Zhao wrote:
> Now write protect `mmap` also need to support large folio,
> Change `f2fs_vm_page_mkwrite` to acheive that.
>
> Note it currently marks the whole large folio dirty
> to avoid data loss which causes write amplification.
> Further optimization is welcome.
>
> PG_mappedtodisk is useless in f2fs, so drop the
> folio_test_mappedtodisk() check and its goto out_sem
Can you send a separated patch to clean up logic on mappedtodisk flag?
> shortcut in f2fs_vm_page_mkwrite(). We extend the
> folio_zero_segment() in mkwrite to zero the post-EOF part
> of the faulted folio for both order-0 and large folios, so
> the f2fs_zero_post_eof_page() call added to cover that
> shortcut is no longer needed.
> > Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/f2fs.h | 5 +++++
> fs/f2fs/file.c | 59 +++++++++++++++++++++++++++++++-------------------
> 2 files changed, 42 insertions(+), 22 deletions(-)
>
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index ae6031693700..71e6d7e34c7b 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -4343,6 +4343,11 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> enum iostat_type io_type,
> int compr_blocks, bool allow_balance);
> bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
> +struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio);
> +void f2fs_ffs_mark_subrange_dirty(struct folio *folio, size_t offset, size_t len);
> +bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,
> + size_t len);
> +void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len);
> void f2fs_write_failed(struct inode *inode, loff_t to);
> void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
> bool f2fs_release_folio(struct folio *folio, gfp_t wait);
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index e40285d4f9c2..02d687527241 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -124,6 +124,13 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> struct dnode_of_data dn;
> bool need_alloc = !f2fs_is_pinned_file(inode);
> + pgoff_t pidx = folio->index + folio_page_idx(folio, vmf->page);
> + loff_t pos = (loff_t)pidx << PAGE_SHIFT;
> + loff_t isize;
> + loff_t folio_start;
> + loff_t valid_end;
> + size_t dirty_len;
> + size_t subpage_off;
> int err = 0;
> vm_fault_t ret;
>
> @@ -160,7 +167,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
>
> #ifdef CONFIG_F2FS_FS_COMPRESSION
> if (f2fs_compressed_file(inode)) {
> - int ret = f2fs_is_compressed_cluster(inode, folio->index);
> + int ret = f2fs_is_compressed_cluster(inode, pidx);
>
> if (ret < 0) {
> err = ret;
> @@ -178,18 +185,20 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
>
> f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
>
> - err = f2fs_zero_post_eof_page(inode,
> - (folio->index + 1) << PAGE_SHIFT, true, false);
> - if (err)
> - goto out_pagefault;
> -
> file_update_time(vmf->vma->vm_file);
> filemap_invalidate_lock_shared(inode->i_mapping);
>
> folio_lock(folio);
> + isize = i_size_read(inode);
> + folio_start = folio_pos(folio);
> + subpage_off = offset_in_folio(folio, pos);
> + valid_end = min_t(loff_t, folio_start + folio_size(folio), isize);
> + dirty_len = valid_end > folio_start ? valid_end - folio_start : 0;
> +
> if (unlikely(folio->mapping != inode->i_mapping ||
> - folio_pos(folio) > i_size_read(inode) ||
> - !folio_test_uptodate(folio))) {
> + pos >= isize ||
> + !f2fs_ffs_test_blk_uptodate(folio,
> + folio->index + (subpage_off >> PAGE_SHIFT)))) {
> folio_unlock(folio);
> err = -EFAULT;
> goto out_sem;
> @@ -198,9 +207,19 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> set_new_dnode(&dn, inode, NULL, NULL, 0);
> if (need_alloc) {
> /* block allocation */
> - err = f2fs_get_block_locked(&dn, folio->index);
> + if (folio_test_large(folio)) {
> + pgoff_t i, nr = DIV_ROUND_UP(dirty_len, PAGE_SIZE);
> +
> + for (i = 0; i < nr; i++) {
> + err = f2fs_get_block_locked(&dn, folio->index + i);
> + if (err)
> + break;
We only need to call f2fs_get_block_locked() for vmf->page?
Hi Barry, could you please help to confirm this? Only vmf->page contain dirty
data, rather than whole large folio contain dirty data?
Thanks,
> + }
> + } else {
> + err = f2fs_get_block_locked(&dn, pidx);
> + }
> } else {
> - err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
> + err = f2fs_get_dnode_of_data(&dn, pidx, LOOKUP_NODE);
> f2fs_put_dnode(&dn);
> if (f2fs_is_pinned_file(inode) &&
> !__is_valid_data_blkaddr(dn.data_blkaddr))
> @@ -217,20 +236,17 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> /* wait for GCed page writeback via META_MAPPING */
> f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
>
> - /*
> - * check to see if the page is mapped already (no holes)
> - */
> - if (folio_test_mappedtodisk(folio))
> - goto out_sem;
> -
> /* page is wholly or partially inside EOF */
> - if (((loff_t)(folio->index + 1) << PAGE_SHIFT) >
> - i_size_read(inode)) {
> - loff_t offset;
> + if (folio_start + folio_size(folio) > isize) {
> + size_t offset = offset_in_folio(folio, isize);
>
> - offset = i_size_read(inode) & ~PAGE_MASK;
> folio_zero_segment(folio, offset, folio_size(folio));
> }
> +
> + if (folio_test_large(folio)) {
> + f2fs_ffs_find_or_alloc(folio);
> + f2fs_ffs_mark_subrange_dirty(folio, 0, dirty_len);
> + }
> folio_mark_dirty(folio);
>
> f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
> @@ -238,12 +254,11 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
>
> out_sem:
> filemap_invalidate_unlock_shared(inode->i_mapping);
> -out_pagefault:
> sb_end_pagefault(inode->i_sb);
> out:
> ret = vmf_fs_error(err);
>
> - trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
> + trace_f2fs_vm_page_mkwrite(inode, pidx, vmf->vma->vm_flags, ret);
> return ret;
> }
>
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 01/14] f2fs: extend folio state for large folio write path
2026-08-27 6:57 ` Chao Yu
@ 2026-08-27 20:51 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-27 20:51 UTC (permalink / raw)
To: Chao Yu
Cc: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim, Barry Song,
Juan Yescas, Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Wed, Aug 26, 2026 at 11:59 PM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> On 8/26/26 16:26, Nanzhe Zhao wrote:
> > Large folio write path needs a subpage status bitmap and write
> > pages pending counter, while keeping compatible with f2fs private
> > flags.
> >
> > Move struct f2fs_folio_state to f2fs.h, add private_flags and
> > subpage state bitmap, and change PAGE_PRIVATE functions to be
> > compatible with f2fs_folio_state. Allocate f2fs_folio_state via kzalloc
> > instead of kmem_cache, since the state size depends on the folio order.
> >
> > Note: Now if a path wants to use f2fs_folio_state, it must call
> > `folio_has_ffs` instead of `folio_test_large`` to make check.
> >
> > Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> > ---
> > fs/f2fs/compress.c | 2 +
> > fs/f2fs/data.c | 60 ++++++++++++++++--------------
> > fs/f2fs/f2fs.h | 92 ++++++++++++++++++++++++++++++++++++----------
> > fs/f2fs/segment.c | 2 +-
> > 4 files changed, 108 insertions(+), 48 deletions(-)
> >
> > diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> > index 91855d91bbdd..84da3e39efb4 100644
> > --- a/fs/f2fs/compress.c
> > +++ b/fs/f2fs/compress.c
> > @@ -78,6 +78,8 @@ bool f2fs_is_compressed_page(struct folio *folio)
> > return false;
> > if (folio_test_f2fs_nonpointer(folio))
> > return false;
> > + if (f2fs_folio_has_ffs(folio))
> > + return false;
>
> Shouldn't this be changed in "f2fs: make compressed files compatible with
> large folio" or other patch? I guess in this patch we only introduce new
> ffs facilities.
>
> >
> > f2fs_bug_on(F2FS_F_SB(folio),
> > *((u32 *)folio->private) != F2FS_COMPRESSED_PAGE_MAGIC);
> > diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> > index 6ae0eb37d20f..578a90d427e2 100644
> > --- a/fs/f2fs/data.c
> > +++ b/fs/f2fs/data.c
> > @@ -32,20 +32,13 @@
> >
> > static struct kmem_cache *bio_post_read_ctx_cache;
> > static struct kmem_cache *bio_entry_slab;
> > -static struct kmem_cache *ffs_entry_slab;
> > static mempool_t *bio_post_read_ctx_pool;
> > static struct bio_set f2fs_bioset;
> >
> > -struct f2fs_folio_state {
> > - spinlock_t state_lock;
> > - unsigned int read_pages_pending;
> > -};
> > -
> > struct f2fs_bio {
> > struct work_struct work;
> > struct bio bio;
> > };
> > -
>
> Unnecessary change.
>
> > #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
> >
> > int __init f2fs_init_bioset(void)
> > @@ -133,6 +126,9 @@ struct bio_post_read_ctx {
> > block_t fs_blkaddr;
> > };
> >
> > +static bool __ffs_mark_subrange_uptodate(struct folio *folio,
> > + struct f2fs_folio_state *ffs, size_t offset, size_t len);
> > +
> > /*
> > * Update and unlock a bio's pages, and free the bio.
> > *
> > @@ -155,7 +151,7 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)
> >
> > bio_for_each_folio_all(fi, bio) {
> > struct folio *folio = fi.folio;
> > - unsigned nr_pages = fi.length >> PAGE_SHIFT;
> > + unsigned int nr_pages = fi.length >> PAGE_SHIFT;
>
> No need to change in this patch?
>
> > bool finished = true;
> >
> > if (!folio_test_large(folio) &&
> > @@ -360,6 +356,7 @@ static void f2fs_write_end_bio(struct bio *bio)
> >
> > bio_for_each_folio_all(fi, bio) {
> > struct folio *folio = fi.folio;
> > + unsigned int nr_pages = fi.length >> PAGE_SHIFT;
>
> No need to change in this patch?
>
> > enum count_type type;
> >
> > if (fscrypt_is_bounce_folio(folio)) {
> > @@ -2516,17 +2513,32 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
> > }
> > #endif
> >
> > -static struct f2fs_folio_state *ffs_find_or_alloc(struct folio *folio)
> > +struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio)
> > {
> > - struct f2fs_folio_state *ffs = folio->private;
> > + struct f2fs_folio_state *ffs;
> > + unsigned int nr_subpages = folio_nr_pages(folio);
> > + unsigned long private_flags = 0;
> > +
> > + f2fs_bug_on(F2FS_F_SB(folio), !folio_test_large(folio));
> >
> > - if (ffs)
> > - return ffs;
> > + if (f2fs_folio_has_ffs(folio))
> > + return (struct f2fs_folio_state *)folio->private;
> >
> > - ffs = f2fs_kmem_cache_alloc(ffs_entry_slab,
> > - GFP_NOIO | __GFP_ZERO, true, NULL);
> > + if (folio_test_private(folio) && folio_test_f2fs_nonpointer(folio))
> > + private_flags = (unsigned long)folio->private;
> > +
> > + ffs = kzalloc(struct_size(ffs, state, BITS_TO_LONGS(2 * nr_subpages)),
> > + GFP_NOIO | __GFP_NOFAIL);
Regarding __GFP_NOFAIL in f2fs_ffs_find_or_alloc():
I understand it is used to avoid failing in-flight I/O or writeback
when allocating
the subpage state. However, invoking __GFP_NOFAIL in memory
reclaim/writeback paths
risks OOM livelocks under extreme memory pressure.
Ideally, we should ensure ffs is always pre-allocated during write_begin() and
page_mkwrite() (where returning -ENOMEM to userspace is safe), so that
the writeback
path never has to allocate ffs on the fly with __GFP_NOFAIL.
WDYT?
> >
> > spin_lock_init(&ffs->state_lock);
> > + ffs->private_flags = private_flags;
> > + if (folio_test_uptodate(folio))
> > + bitmap_set(ffs->state, 0, nr_subpages);
> > + if (folio_test_dirty(folio))
> > + bitmap_set(ffs->state, nr_subpages, nr_subpages);
> > +
> > + if (folio_test_private(folio))
> > + folio_detach_private(folio);
> > folio_attach_private(folio, ffs);
> > return ffs;
> > }
> > @@ -2535,7 +2547,7 @@ static void ffs_detach_free(struct folio *folio)
> > {
> > struct f2fs_folio_state *ffs;
> >
> > - if (!folio_test_large(folio)) {
> > + if (!f2fs_folio_has_ffs(folio)) {
> > folio_detach_private(folio);
> > return;
> > }
> > @@ -2545,7 +2557,8 @@ static void ffs_detach_free(struct folio *folio)
> > return;
> >
> > WARN_ON_ONCE(ffs->read_pages_pending != 0);
> > - kmem_cache_free(ffs_entry_slab, ffs);
> > + WARN_ON_ONCE(atomic_read(&ffs->write_pages_pending));
> > + kfree(ffs);
> > }
> >
> > static int f2fs_read_data_large_folio(struct inode *inode,
> > @@ -2558,7 +2571,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
> > pgoff_t index, offset, next_pgofs = 0;
> > unsigned max_nr_pages = rac ? readahead_count(rac) :
> > folio_nr_pages(folio);
> > - unsigned nrpages;
> > + unsigned int nrpages, len_blks;
> > struct f2fs_folio_state *ffs;
> > int ret = 0;
> > bool folio_in_bio = false;
> > @@ -2634,7 +2647,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
> > * to prevent from premature folio_end_read() call on folio
> > */
> > if (folio_test_large(folio)) {
> > - ffs = ffs_find_or_alloc(folio);
> > + ffs = f2fs_ffs_find_or_alloc(folio);
> >
> > /* set the bitmap to wait */
> > spin_lock_irq(&ffs->state_lock);
> > @@ -2987,7 +3000,7 @@ bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
> > return true;
> >
> > if (fio) {
> > - if (page_private_gcing(fio->page))
> > + if (folio_test_f2fs_gcing(fio->folio))
>
> Shouldn't this be changed in "f2fs: make GC migration large-folio aware"?
>
> > return true;
> > if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
> > f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
> > @@ -4586,21 +4599,12 @@ int __init f2fs_init_bio_entry_cache(void)
> > if (!bio_entry_slab)
> > return -ENOMEM;
> >
> > - ffs_entry_slab = f2fs_kmem_cache_create("f2fs_ffs_slab",
> > - sizeof(struct f2fs_folio_state));
> > -
> > - if (!ffs_entry_slab) {
> > - kmem_cache_destroy(bio_entry_slab);
> > - return -ENOMEM;
> > - }
> > -
> > return 0;
> > }
> >
> > void f2fs_destroy_bio_entry_cache(void)
> > {
> > kmem_cache_destroy(bio_entry_slab);
> > - kmem_cache_destroy(ffs_entry_slab);
> > }
> >
> > static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index 16720f1f0a9c..2e8f85cea6d0 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -1628,6 +1628,18 @@ static inline void f2fs_clear_bit(unsigned int nr, char *addr);
> > * Layout B: lowest bit should be 0
> > * page.private is a wrapped pointer.
> > */
> > +
> > +struct f2fs_folio_state {
> > + spinlock_t state_lock;
> > + unsigned int read_pages_pending;
> > + atomic_t write_pages_pending;
> > + unsigned long private_flags;
> > + /* state[0..nr_subpages - 1] tracks uptodate subpages.
> > + * state[nr_subpages..2 * nr_subpages - 1] tracks dirty subpages.
> > + */
> > + unsigned long state[];
> > +};
> > +
> > enum {
> > PAGE_PRIVATE_NOT_POINTER, /* private contains non-pointer data */
> > PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */
> > @@ -1637,6 +1649,14 @@ enum {
> > PAGE_PRIVATE_MAX
> > };
> >
> > +static inline bool f2fs_folio_has_ffs(const struct folio *folio)
> > +{
> > + unsigned long private = (unsigned long)folio->private;
> > +
> > + return folio_test_large(folio) && private &&
> > + !(private & BIT(PAGE_PRIVATE_NOT_POINTER));
>
> Is this a bug? in which case we will set PAGE_PRIVATE_NOT_POINTER in
> a large folio? maybe I missed some cases...
>
> folio_test_large(folio) && private is true and
> (private & BIT(PAGE_PRIVATE_NOT_POINTER) is true?
>
> > +}
> > +
> > /* For compression */
> > enum compress_algorithm_type {
> > COMPRESS_LZO,
> > @@ -2682,10 +2702,57 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
> > return -ENOSPC;
> > }
> >
> > +static inline unsigned long f2fs_folio_get_private_flags(const struct folio *folio)
> > +{
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs = folio->private;
> > +
> > + return ffs->private_flags;
> > + }
> > +
> > + return (unsigned long)folio->private;
> > +}
> > +
> > +static inline void f2fs_folio_set_private_flags(struct folio *folio,
> > + unsigned long flags)
> > +{
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs = folio->private;
> > +
> > + ffs->private_flags |= flags;
Is this field protected by holding a lock properly?
Thanks,
> > + return;
> > + }
> > +
> > + if (!folio_test_private(folio))
> > + folio_attach_private(folio, (void *)flags);
> > + else
> > + folio->private = (void *)((unsigned long)folio->private | flags);
> > +}
> > +
> > +static inline void f2fs_folio_clear_private_flags(struct folio *folio,
> > + unsigned long flags)
> > +{
> > + unsigned long private;
> > +
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs = folio->private;
> > +
> > + ffs->private_flags &= ~flags;
> > + return;
> > + }
> > +
> > + private = (unsigned long)folio->private;
> > + private &= ~flags;
> > + if (private == BIT(PAGE_PRIVATE_NOT_POINTER))
> > + folio_detach_private(folio);
> > + else
> > + folio->private = (void *)private;
> > +}
> > +
> > #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
> > static inline bool folio_test_f2fs_##name(const struct folio *folio) \
> > { \
> > - unsigned long priv = (unsigned long)folio->private; \
> > + unsigned long priv = f2fs_folio_get_private_flags(folio); \
> > unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
> > (1UL << PAGE_PRIVATE_##flagname); \
> > return (priv & v) == v; \
> > @@ -2702,12 +2769,7 @@ static inline void folio_set_f2fs_##name(struct folio *folio) \
> > { \
> > unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
> > (1UL << PAGE_PRIVATE_##flagname); \
> > - if (!folio->private) \
> > - folio_attach_private(folio, (void *)v); \
> > - else { \
> > - v |= (unsigned long)folio->private; \
> > - folio->private = (void *)v; \
> > - } \
> > + f2fs_folio_set_private_flags(folio, v); \
> > } \
> > static inline void set_page_private_##name(struct page *page) \
> > { \
> > @@ -2720,13 +2782,8 @@ static inline void set_page_private_##name(struct page *page) \
> > #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
> > static inline void folio_clear_f2fs_##name(struct folio *folio) \
> > { \
> > - unsigned long v = (unsigned long)folio->private; \
> > - \
> > - v &= ~(1UL << PAGE_PRIVATE_##flagname); \
> > - if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \
> > - folio_detach_private(folio); \
> > - else \
> > - folio->private = (void *)v; \
> > + f2fs_folio_clear_private_flags(folio, \
> > + 1UL << PAGE_PRIVATE_##flagname); \
> > } \
> > static inline void clear_page_private_##name(struct page *page) \
> > { \
> > @@ -2752,7 +2809,7 @@ PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
> >
> > static inline unsigned long folio_get_f2fs_data(struct folio *folio)
> > {
> > - unsigned long data = (unsigned long)folio->private;
> > + unsigned long data = f2fs_folio_get_private_flags(folio);
> >
> > if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
> > return 0;
> > @@ -2763,10 +2820,7 @@ static inline void folio_set_f2fs_data(struct folio *folio, unsigned long data)
> > {
> > data = (1UL << PAGE_PRIVATE_NOT_POINTER) | (data << PAGE_PRIVATE_MAX);
> >
> > - if (!folio_test_private(folio))
> > - folio_attach_private(folio, (void *)data);
> > - else
> > - folio->private = (void *)((unsigned long)folio->private | data);
> > + f2fs_folio_set_private_flags(folio, data);
> > }
> >
> > static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
> > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > index ed6f2947210b..df10119d94ad 100644
> > --- a/fs/f2fs/segment.c
> > +++ b/fs/f2fs/segment.c
> > @@ -3803,7 +3803,7 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
> > if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
> > return CURSEG_COLD_DATA_PINNED;
> >
> > - if (page_private_gcing(fio->page)) {
> > + if (folio_test_f2fs_gcing(fio->folio)) {
>
> Shouldn't this be changed in "f2fs: make GC migration large-folio aware"?
>
> Thanks,
>
> > if (fio->sbi->am.atgc_enabled &&
> > (fio->io_type == FS_DATA_IO) &&
> > (fio->sbi->gc_mode != GC_URGENT_HIGH) &&
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 02/14] f2fs: carry subpage offset and count in write IO
2026-08-27 7:16 ` Chao Yu
@ 2026-08-27 21:06 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-27 21:06 UTC (permalink / raw)
To: Chao Yu
Cc: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim, Barry Song,
Juan Yescas, Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Thu, Aug 27, 2026 at 12:18 AM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> On 8/26/26 16:26, Nanzhe Zhao wrote:
> > Large folio write paths need to submit I/O for a range inside a
> > folio instead of always submitting the whole folio from offset zero.
> > Add folio_offset and folio_blkcnt to f2fs_io_info to describe the
> > block offset inside the folio and the number of contiguous blocks
> > covered by the I/O.
> >
> > Apply the new fields to the bio submit paths that need the subpage
> > offset or contiguous block count.
> >
> > Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> > ---
> > fs/f2fs/data.c | 59 ++++++++++++++++++++++++++++++++---------------
> > fs/f2fs/f2fs.h | 11 +++++++++
> > fs/f2fs/segment.c | 3 ++-
> > 3 files changed, 54 insertions(+), 19 deletions(-)
> >
> > diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> > index 578a90d427e2..8649d443ca74 100644
> > --- a/fs/f2fs/data.c
> > +++ b/fs/f2fs/data.c
> > @@ -783,6 +783,9 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
> > struct folio *fio_folio = fio->folio;
> > struct folio *data_folio = fio->encrypted_page ?
> > page_folio(fio->encrypted_page) : fio_folio;
> > + pgoff_t fio_lblk = F2FS_FIO_LBLK(fio_folio, fio);
> > + size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> > + size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
> >
> > if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
> > fio->is_por ? META_POR : (__is_meta_io(fio) ?
> > @@ -795,11 +798,11 @@ int f2fs_submit_page_bio(struct f2fs_io_info *fio)
> > bio = __bio_alloc(fio, 1);
> >
> > f2fs_set_bio_crypt_ctx(bio, fio_folio->mapping->host,
> > - fio_folio->index, fio, GFP_NOIO);
> > - bio_add_folio_nofail(bio, data_folio, folio_size(data_folio), 0);
> > + fio_lblk, fio, GFP_NOIO);
> > + bio_add_folio_nofail(bio, data_folio, bio_len, bio_offset);
> >
> > if (fio->io_wbc && !is_read_io(fio->op))
> > - wbc_account_cgroup_owner(fio->io_wbc, fio_folio, PAGE_SIZE);
> > + wbc_account_cgroup_owner(fio->io_wbc, fio_folio, bio_len);
> >
> > inc_page_count(fio->sbi, is_read_io(fio->op) ?
> > __read_io_type(data_folio) : WB_DATA_TYPE(fio->folio, false));
> > @@ -844,7 +847,8 @@ static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
> > }
> >
> > static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
> > - struct folio *folio, enum temp_type temp)
> > + struct folio *folio, size_t len, size_t offset,
> > + enum temp_type temp)
> > {
> > struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
> > struct bio_entry *be;
> > @@ -853,7 +857,7 @@ static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
> > be->bio = bio;
> > bio_get(bio);
> >
> > - bio_add_folio_nofail(bio, folio, folio_size(folio), 0);
> > + bio_add_folio_nofail(bio, folio, len, offset);
> >
> > f2fs_down_write(&io->bio_list_lock);
> > list_add_tail(&be->list, &io->bio_list);
> > @@ -870,6 +874,9 @@ static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
> > struct folio *folio)
> > {
> > struct folio *fio_folio = fio->folio;
> > + pgoff_t fio_lblk = F2FS_FIO_LBLK(fio_folio, fio);
> > + size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> > + size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
> > struct f2fs_sb_info *sbi = fio->sbi;
> > enum temp_type temp;
> > bool found = false;
> > @@ -892,8 +899,8 @@ static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
> > fio->new_blkaddr));
> > if (f2fs_crypt_mergeable_bio(*bio,
> > fio_folio->mapping->host,
> > - fio_folio->index, fio) &&
> > - bio_add_folio(*bio, folio, folio_size(folio), 0)) {
> > + fio_lblk, fio) &&
> > + bio_add_folio(*bio, folio, bio_len, bio_offset)) {
> > ret = 0;
> > break;
> > }
> > @@ -1007,6 +1014,10 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
> > struct folio *data_folio = fio->encrypted_page ?
> > page_folio(fio->encrypted_page) : fio->folio;
> > struct folio *folio = fio->folio;
> > + pgoff_t fio_lblk = F2FS_FIO_LBLK(folio, fio);
> > + unsigned int fio_cnt = F2FS_FIO_BLKCNT(fio);
> > + size_t bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> > + size_t bio_len = F2FS_FIO_BIO_SIZE(fio);
> >
> > if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
> > __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
> > @@ -1021,9 +1032,10 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
> > if (!bio) {
> > bio = __bio_alloc(fio, BIO_MAX_VECS);
> > f2fs_set_bio_crypt_ctx(bio, folio->mapping->host,
> > - folio->index, fio, GFP_NOIO);
> > + fio_lblk, fio, GFP_NOIO);
> >
> > - add_bio_entry(fio->sbi, bio, data_folio, fio->temp);
> > + add_bio_entry(fio->sbi, bio, data_folio, bio_len,
> > + bio_offset, fio->temp);
> > } else {
> > if (add_ipu_page(fio, &bio, data_folio))
> > goto alloc_new;
> > @@ -1034,7 +1046,7 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
> >
> > inc_page_count(fio->sbi, WB_DATA_TYPE(folio, false));
> >
> > - *fio->last_block = fio->new_blkaddr;
> > + *fio->last_block = fio->new_blkaddr + fio_cnt - 1;
> > *fio->bio = bio;
> >
> > return 0;
> > @@ -1070,6 +1082,10 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
> > struct folio *bio_folio;
> > struct f2fs_lock_context lc;
> > enum count_type type;
> > + pgoff_t fio_lblk;
> > + unsigned int fio_cnt;
> > + size_t bio_offset;
> > + size_t bio_len;
> >
> > f2fs_bug_on(sbi, is_read_io(fio->op));
> >
> > @@ -1108,6 +1124,9 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
> > /* set submitted = true as a return value */
> > fio->submitted = 1;
> >
> > + fio_lblk = F2FS_FIO_LBLK(fio->folio, fio);
> > + fio_cnt = F2FS_FIO_BLKCNT(fio);
> > +
> > type = WB_DATA_TYPE(bio_folio, fio->compressed_page);
> > inc_page_count(sbi, type);
inc_page_count() is called only once, but the completion
path in f2fs_write_end_bio() decrements the counter by `fi.length >>
PAGE_SHIFT`.
Even though folio_blkcnt is currently always 1, for robustness and
future multi-block
support, please account for fio_cnt:
for (i = 0; i < fio_cnt; i++)
inc_page_count(sbi, type);
Thanks,
> >
> > @@ -1115,26 +1134,28 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
> > (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
> > fio->new_blkaddr) ||
> > !f2fs_crypt_mergeable_bio(io->bio, fio_inode(fio),
> > - bio_folio->index, fio)))
> > + fio_lblk, fio)))
> > __submit_merged_bio(io);
> > alloc_new:
> > if (io->bio == NULL) {
> > io->bio = __bio_alloc(fio, BIO_MAX_VECS);
> > f2fs_set_bio_crypt_ctx(io->bio, fio_inode(fio),
> > - bio_folio->index, fio, GFP_NOIO);
> > + fio_lblk, fio, GFP_NOIO);
> > io->fio = *fio;
> > }
> >
> > - if (!bio_add_folio(io->bio, bio_folio, folio_size(bio_folio), 0)) {
> > + bio_offset = F2FS_FIO_BIO_OFFSET(fio);
> > + bio_len = F2FS_FIO_BIO_SIZE(fio);
> > +
> > + if (!bio_add_folio(io->bio, bio_folio, bio_len, bio_offset)) {
> > __submit_merged_bio(io);
> > goto alloc_new;
> > }
> >
> > if (fio->io_wbc)
> > - wbc_account_cgroup_owner(fio->io_wbc, fio->folio,
> > - folio_size(fio->folio));
> > + wbc_account_cgroup_owner(fio->io_wbc, fio->folio, bio_len);
> >
> > - io->last_block_in_bio = fio->new_blkaddr;
> > + io->last_block_in_bio = fio->new_blkaddr + fio_cnt - 1;
> >
> > trace_f2fs_submit_folio_write(fio->folio, fio);
> > #ifdef CONFIG_BLK_DEV_ZONED
> > @@ -3039,7 +3060,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
> > set_new_dnode(&dn, inode, NULL, NULL, 0);
> >
> > if (need_inplace_update(fio) &&
> > - f2fs_lookup_read_extent_cache_block(inode, folio->index,
> > + f2fs_lookup_read_extent_cache_block(inode,
> > + F2FS_FIO_LBLK(folio, fio),
> > &fio->old_blkaddr)) {
> > if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
> > DATA_GENERIC_ENHANCE))
> > @@ -3058,7 +3080,8 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
> > if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi, &lc))
> > return -EAGAIN;
> >
> > - err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
> > + err = f2fs_get_dnode_of_data(&dn, F2FS_FIO_LBLK(folio, fio),
> > + LOOKUP_NODE);
> > if (err)
> > goto out;
> >
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index 2e8f85cea6d0..31ddd321daf6 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -1357,6 +1357,8 @@ struct f2fs_io_info {
> > blk_opf_t op_flags; /* req_flag_bits */
> > block_t new_blkaddr; /* new block address to be written */
> > block_t old_blkaddr; /* old block address before Cow */
> > + pgoff_t folio_offset; /* offset in large folio */
> > + unsigned int folio_blkcnt; /* block count in large folio */
> > union {
> > struct page *page; /* page to be written */
> > struct folio *folio;
> > @@ -1378,6 +1380,15 @@ struct f2fs_io_info {
> > sector_t *last_block; /* last block number in bio */
> > };
> >
> > +#define F2FS_FIO_LBLK(folio, fio) \
> > + ((folio)->index + (fio)->folio_offset)
>
> #define F2FS_FIO_LBLK(fio) \
> (((fio)->folio)->index + (fio)->folio_offset)
>
> > +#define F2FS_FIO_BLKCNT(fio) \
> > + ((fio)->folio_blkcnt ? (fio)->folio_blkcnt : 1)
>
> Hmm, it's better to not leaving fio.folio_blkcnt as zero, can we set
> fio.folio_blkcnt to 1 by default? and use fio->folio_blkcnt.
>
> Thanks,
>
> > +#define F2FS_FIO_BIO_OFFSET(fio) \
> > + ((fio)->folio_offset << PAGE_SHIFT)
> > +#define F2FS_FIO_BIO_SIZE(fio) \
> > + F2FS_BLK_TO_BYTES(F2FS_FIO_BLKCNT(fio))
> > +
> > struct bio_entry {
> > struct bio *bio;
> > struct list_head list;
> > diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
> > index df10119d94ad..5bb1e51a38a0 100644
> > --- a/fs/f2fs/segment.c
> > +++ b/fs/f2fs/segment.c
> > @@ -3816,7 +3816,8 @@ static int __get_segment_type_6(struct f2fs_io_info *fio)
> > if (file_is_cold(inode) || f2fs_need_compress_data(inode))
> > return CURSEG_COLD_DATA;
> >
> > - type = __get_age_segment_type(inode, fio->folio->index);
> > + type = __get_age_segment_type(inode,
> > + F2FS_FIO_LBLK(fio->folio, fio));
> > if (type != NO_CHECK_TYPE)
> > return type;
> >
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 03/14] f2fs: support regular file buffered writes on large folios
2026-08-27 8:56 ` Chao Yu
@ 2026-08-27 21:13 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-27 21:13 UTC (permalink / raw)
To: Chao Yu
Cc: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim, Barry Song,
Juan Yescas, Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Thu, Aug 27, 2026 at 2:00 AM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> On 8/26/26 16:26, Nanzhe Zhao wrote:
> > To avoid the complexity of unlocking a large folio in write_begin,
> > preallocate partial blocks for inodes that can use large folios.
> > During write_begin, read only the partial head and tail 4K subpages
> > that need read-before-write, and skip read I/O for the full middle
> > subpages covered by the write.
> >
> > Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> > ---
> > fs/f2fs/data.c | 289 +++++++++++++++++++++++++++++++++++++++++++++++--
> > fs/f2fs/f2fs.h | 1 +
> > fs/f2fs/file.c | 17 ++-
> > 3 files changed, 293 insertions(+), 14 deletions(-)
> >
> > diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> > index 8649d443ca74..7ce96ae02cfc 100644
> > --- a/fs/f2fs/data.c
> > +++ b/fs/f2fs/data.c
> > @@ -1249,6 +1249,40 @@ static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
> > f2fs_submit_read_bio(sbi, bio, DATA);
> > }
> >
> > +/*
> > + * Synchronously read a single 4K subpage by reusing f2fs_submit_page_read()
> > + * so that iostat, trace, blk-crypto and post-read handling are all preserved.
> > + * The caller must have already allocated ffs for the folio.
> > + */
> > +static int f2fs_submit_page_read_sync(struct inode *inode, struct folio *folio,
> > + pgoff_t index, block_t blkaddr)
> > +{
> > + struct f2fs_folio_state *ffs = folio->private;
> > + unsigned long flags;
> > +
> > + /* Add bias so end_io does not call folio_end_read(). */
> > + spin_lock_irqsave(&ffs->state_lock, flags);
> > + ffs->read_pages_pending++;
> > + spin_unlock_irqrestore(&ffs->state_lock, flags);
> > +
> > + f2fs_submit_page_read(inode, NULL, folio, index, blkaddr,
> > + REQ_OP_READ, false);
> > +
> > + /* Wait for pending drops back to bias which indicates all bio have completed. */
> > + while (READ_ONCE(ffs->read_pages_pending) != 1)
> > + f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
>
> Suspect there will be potential performance issue, as timeout interval is
> large.
>
> > +
> > + /* Remove the bias. */
> > + spin_lock_irqsave(&ffs->state_lock, flags);
> > + ffs->read_pages_pending--;
> > + spin_unlock_irqrestore(&ffs->state_lock, flags);
>
> Looks hacky.
>
> If large folio has 4 sub pages, first 3 pages are uptodate.
> Then we use f2fs_submit_page_read_sync() to read last page,
> in f2fs_finish_read_bio(), we update state bitmap of last page to 1,
> but as read_pages_pending is 1, so folio won't be set as uptodate.
>
> Oh, in f2fs_write_end(), we have another change to set folio uptodate
> though, so the flow is a little bit different here.
>
> > +
> > + if (!f2fs_ffs_test_blk_uptodate(folio, index))
> > + return -EIO;
> > +
> > + return 0;
> > +}
> > +
> > static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
> > {
> > __le32 *addr = get_dnode_addr(dn->inode, dn->node_folio);
> > @@ -2582,6 +2616,112 @@ static void ffs_detach_free(struct folio *folio)
> > kfree(ffs);
> > }
> >
> > +bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index)
> > +{
> > + struct f2fs_folio_state *ffs;
> > + unsigned int idx;
> > +
> > + if (!f2fs_folio_has_ffs(folio))
> > + return folio_test_uptodate(folio);
> > +
> > + if (!folio_contains(folio, index))
> > + return false;
>
> Can we check this out of f2fs_ffs_test_blk_uptodate()? and add a sanity
> check here:
>
> f2fs_bug_on(sbi, !folio_contains(folio, index));
>
> > +
> > + ffs = (struct f2fs_folio_state *)folio->private;
> > + idx = index - folio->index;
> > + return test_bit(idx, ffs->state);
> > +}
> > +
> > +static bool __ffs_mark_subrange_uptodate(struct folio *folio,
> > + struct f2fs_folio_state *ffs, size_t offset, size_t len)
> > +{
> > + unsigned int nr_subpages = folio_nr_pages(folio);
> > + unsigned int start, end;
> > +
> > + start = offset >> PAGE_SHIFT;
> > + end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> > + end = min(end, nr_subpages);
> > +
> > + bitmap_set(ffs->state, start, end - start);
> > + return bitmap_full(ffs->state, nr_subpages);
> > +}
> > +
> > +static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
> > + size_t len)
> > +{
> > + struct f2fs_folio_state *ffs;
> > + unsigned long flags;
> > + bool mark_uptodate = false;
> > +
> > + f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
> > +
> > + if (!f2fs_folio_has_ffs(folio)) {
> > + folio_mark_uptodate(folio);
> > + return;
> > + }
> > +
> > + ffs = (struct f2fs_folio_state *)folio->private;
> > + spin_lock_irqsave(&ffs->state_lock, flags);
> > + mark_uptodate = __ffs_mark_subrange_uptodate(folio, ffs, offset, len) &&
> > + !ffs->read_pages_pending;
> > + spin_unlock_irqrestore(&ffs->state_lock, flags);
> > + if (mark_uptodate)
> > + folio_mark_uptodate(folio);
> > +}
> > +
> > +static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> > + size_t offset, size_t len)
> > +{
> > + struct f2fs_folio_state *ffs;
> > + unsigned int nr_subpages, start, end;
> > + unsigned long flags;
> > +
> > + f2fs_bug_on(F2FS_F_SB(folio), offset + len > folio_size(folio));
> > +
> > + if (!f2fs_folio_has_ffs(folio))
> > + return;
> > +
> > + ffs = (struct f2fs_folio_state *)folio->private;
> > + nr_subpages = folio_nr_pages(folio);
> > + start = offset >> PAGE_SHIFT;
> > + end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> > + end = min(end, nr_subpages);
> > +
> > + spin_lock_irqsave(&ffs->state_lock, flags);
> > + bitmap_set(ffs->state, nr_subpages + start, end - start);
> > + spin_unlock_irqrestore(&ffs->state_lock, flags);
> > +}
> > +
> > +static bool find_next_valid_block(const struct folio *folio,
> > + size_t orig_off, size_t *need_off,
> > + size_t len)
> > +{
> > + size_t start = orig_off;
> > + size_t end = start + len;
> > + size_t head, tail;
> > + pgoff_t index;
> > +
> > + if (start & (PAGE_SIZE - 1)) {
> > + head = round_down(start, PAGE_SIZE);
> > + index = folio->index + (head >> PAGE_SHIFT);
> > + if (!f2fs_ffs_test_blk_uptodate(folio, index)) {
> > + *need_off = head;
> > + return true;
> > + }
> > + }
> > +
> > + if (end & (PAGE_SIZE - 1)) {
> > + tail = round_down(end - 1, PAGE_SIZE);
> > + index = folio->index + (tail >> PAGE_SHIFT);
> > + if (!f2fs_ffs_test_blk_uptodate(folio, index)) {
> > + *need_off = tail;
> > + return true;
> > + }
> > + }
> > +
> > + return false;
> > +}
> > +
> > static int f2fs_read_data_large_folio(struct inode *inode,
> > struct fsverity_info *vi,
> > struct readahead_control *rac, struct folio *folio)
> > @@ -3975,6 +4115,102 @@ static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
> > return 0;
> > }
> >
> > +static int prepare_large_folio_write_begin(struct inode *inode,
> > + struct folio *folio, loff_t pos,
> > + unsigned int len)
> > +{
> > + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> > + struct f2fs_folio_state *ffs;
> > + size_t ori_off = offset_in_folio(folio, pos);
> > + size_t need_off = ori_off;
> > + int err = 0;
> > +
> > + len = min_t(unsigned int, len, folio_size(folio) - ori_off);
> > +
> > + /*
> > + * When folio minimum order is non-zero, the fsverity
> > + * page_cache_write() path enters f2fs_write_begin() via
> > + * aops->write_begin without going through f2fs_write_iter(),
> > + * so preallocation from f2fs_write_iter() is skipped. In that
> > + * case, if FI_PREALLOCATED_ALL is not set, we must preallocate
> > + * the write blocks here.
> > + */
> > + if (!is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) {
> > + struct f2fs_map_blocks map = {};
> > +
> > + map.m_lblk = F2FS_BYTES_TO_BLK(pos);
> > + map.m_len = F2FS_BLK_ALIGN(pos + len) - map.m_lblk;
> > +
> > + if (!IS_DEVICE_ALIASING(inode))
> > + map.m_may_create = true;
> > + map.m_seg_type = NO_CHECK_TYPE;
> > +
> > + err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
> > + if (err)
> > + return err;
> > + }
> > +
> > + /*
> > + * Skip read and ffs allocation if the folio is already fully
> > + * uptodate or the write covers the entire folio.
> > + */
> > + if (folio_test_uptodate(folio) || len == folio_size(folio))
> > + return 0;
If a large folio is already uptodate by read in the page cache and a
buffered write updates
a 4KB subrange (len < folio_size), this early return skips allocating ffs.
Then in f2fs_write_end():
f2fs_ffs_mark_subrange_dirty(folio, ...);
returns immediately because !f2fs_folio_has_ffs(folio).
Subsequently, in f2fs_write_cache_folios(), writeback sees no ffs,
allocates one,
and marks the ENTIRE large folio dirty
(`f2fs_ffs_mark_subrange_dirty(folio, 0, end_pos - pos)`),
causing every 4KB partial write to rewrite the full large folio to disk.
Fix: If len < folio_size(folio), we MUST ensure
`f2fs_ffs_find_or_alloc(folio)` is
called even when folio_test_uptodate(folio) is true, so
f2fs_write_end() can track
the dirty subpage.
Thanks,
>
> Do we need to adopt fsverity like we did in f2fs_write_begin()?
>
> if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
> !f2fs_verity_in_progress(inode)) {
> folio_zero_segment(folio, len, folio_size(folio));
> return 0;
> }
>
> > +
> > + ffs = f2fs_ffs_find_or_alloc(folio);
>
> f2fs_ffs_find_or_alloc() will never fail.
>
> > + if (!ffs)
> > + return -ENOMEM;
> > +
> > + /* Inline data must have been converted before reaching here. */
> > + f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
> > +
> > + while (find_next_valid_block(folio, ori_off,
> > + &need_off, len)) {
> > + struct dnode_of_data dn;
> > + pgoff_t index = folio->index + (need_off >> PAGE_SHIFT);
> > + block_t blkaddr;
> > + bool get_dn = false;
> > +
> > + if (!f2fs_lookup_read_extent_cache_block(inode, index,
> > + &blkaddr)) {
> > + if (IS_DEVICE_ALIASING(inode))
> > + return -ENODATA;
> > +
> > + set_new_dnode(&dn, inode, NULL, NULL, 0);
> > + err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
> > + if (err)
> > + return err;
> > + get_dn = true;
> > + blkaddr = dn.data_blkaddr;
> > +
> > + if (blkaddr == NEW_ADDR) {
> > + size_t off = offset_in_folio(folio,
> > + index << PAGE_SHIFT);
> > +
> > + folio_zero_segment(folio, off, off + PAGE_SIZE);
> > + f2fs_ffs_mark_subrange_uptodate(folio, off,
> > + PAGE_SIZE);
> > + goto out;
> > + }
> > +
> > + if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
> > + DATA_GENERIC_ENHANCE_READ)) {
> > + err = -EFSCORRUPTED;
> > + goto out;
> > + }
> > + }
> > +
> > + err = f2fs_submit_page_read_sync(inode, folio, index, blkaddr);
> > +out:
> > + if (get_dn)
> > + f2fs_put_dnode(&dn);
> > + if (err)
> > + return err;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > static int f2fs_write_begin(const struct kiocb *iocb,
> > struct address_space *mapping,
> > loff_t pos, unsigned len, struct folio **foliop,
> > @@ -3986,6 +4222,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> > pgoff_t index = pos >> PAGE_SHIFT;
> > bool need_balance = false;
> > block_t blkaddr = NULL_ADDR;
> > + fgf_t fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT;
> > int err = 0;
> >
> > trace_f2fs_write_begin(inode, pos, len);
> > @@ -4033,9 +4270,9 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> > * Do not use FGP_STABLE to avoid deadlock.
> > * Will wait that below with our IO control.
> > */
> > - folio = f2fs_filemap_get_folio(mapping, index,
> > - FGP_LOCK | FGP_WRITE | FGP_CREAT,
> > - mapping_gfp_mask(mapping));
> > + fgp |= fgf_set_order(len);
> > + folio = __filemap_get_folio(mapping, index, fgp,
> > + mapping_gfp_mask(mapping));
>
> It detaches from fault injection, why we need to change to use
> __filemap_get_folio()?
>
> > if (IS_ERR(folio)) {
> > err = PTR_ERR(folio);
> > goto fail;
> > @@ -4048,7 +4285,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> > if (f2fs_is_atomic_file(inode))
> > err = prepare_atomic_write_begin(sbi, folio, pos, len,
> > &blkaddr, &need_balance);
> > - else
> > + else if (!folio_test_large(folio))
> > err = prepare_write_begin(sbi, folio, pos, len,
> > &blkaddr, &need_balance);
> > if (err)
> > @@ -4069,6 +4306,14 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> >
> > f2fs_folio_wait_writeback(folio, DATA, false, true);
> >
> > + if (folio_test_large(folio)) {
> > + err = prepare_large_folio_write_begin(inode,
> > + folio, pos, len);
> > + if (!err)
> > + return 0;
> > + goto put_folio;
> > + }
> > +
> > if (len == folio_size(folio) || folio_test_uptodate(folio))
> > return 0;
> >
> > @@ -4129,15 +4374,20 @@ static int f2fs_write_end(const struct kiocb *iocb,
> > trace_f2fs_write_end(inode, pos, len, copied);
> >
> > /*
> > - * This should be come from len == PAGE_SIZE, and we expect copied
> > - * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
> > - * let generic_perform_write() try to copy data again through copied=0.
> > + * If a short copy happens on a folio that isn't uptodate, we treat
> > + * it with zero copied and let generic_perform_write() try to copy
> > + * data again through copied=0.
> > */
> > if (!folio_test_uptodate(folio)) {
> > - if (unlikely(copied != len))
> > + if (unlikely(copied != len)) {
> > copied = 0;
> > - else
> > + } else if (folio_test_large(folio)) {
> > + f2fs_ffs_mark_subrange_uptodate(folio,
> > + offset_in_folio(folio, pos), len);
> > + } else {
> > + /* This should be come from len == PAGE_SIZE */
> > folio_mark_uptodate(folio);
> > + }
> > }
> >
> > #ifdef CONFIG_F2FS_FS_COMPRESSION
> > @@ -4156,6 +4406,9 @@ static int f2fs_write_end(const struct kiocb *iocb,
> > if (!copied)
> > goto unlock_out;
> >
> > + if (folio_test_large(folio))
> > + f2fs_ffs_mark_subrange_dirty(folio, offset_in_folio(folio, pos),
> > + copied);
> > folio_mark_dirty(folio);
> >
> > if (f2fs_is_atomic_file(inode))
> > @@ -4218,8 +4471,22 @@ static bool f2fs_dirty_data_folio(struct address_space *mapping,
> >
> > trace_f2fs_set_page_dirty(folio, DATA);
> >
> > - if (!folio_test_uptodate(folio))
> > - folio_mark_uptodate(folio);
> > + if (!folio_test_uptodate(folio)) {
> > + bool uptodate = true;
> > +
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs =
> > + (struct f2fs_folio_state *)folio->private;
> > + unsigned long flags;
> > +
> > + spin_lock_irqsave(&ffs->state_lock, flags);
> > + uptodate = bitmap_full(ffs->state, folio_nr_pages(folio)) &&
> > + !ffs->read_pages_pending;
> > + spin_unlock_irqrestore(&ffs->state_lock, flags);
>
> f2fs_ffs_mark_subrange_uptodate(folio, 0, folio_size(folio)); ?
>
> > + }
> > + if (uptodate)
> > + folio_mark_uptodate(folio);
> > + }
> > BUG_ON(folio_test_swapcache(folio));
> >
> > if (filemap_dirty_folio(mapping, folio)) {
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index 31ddd321daf6..ae6031693700 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -4342,6 +4342,7 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> > struct writeback_control *wbc,
> > enum iostat_type io_type,
> > int compr_blocks, bool allow_balance);
> > +bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
> > void f2fs_write_failed(struct inode *inode, loff_t to);
> > void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
> > bool f2fs_release_folio(struct folio *folio, gfp_t wait);
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index d440231b8cb9..e40285d4f9c2 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -5405,9 +5405,20 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
> > return ret;
> > }
> >
> > - /* Do not preallocate blocks that will be written partially in 4KB. */
> > - map.m_lblk = F2FS_BLK_ALIGN(pos);
> > - map.m_len = F2FS_BYTES_TO_BLK(pos + count);
> > + if (mapping_large_folio_support(inode->i_mapping)) {
> > + /*
> > + * Preallocate all blocks touched by a large-folio buffered write so
> > + * the regular write_begin path does not need to unlock the folio for
> > + * f2fs_balance_fs(). Rechecking large-folio state after unlock is
> > + * unreliable since partial truncation can split the folio.
>
> You mean buffered io case, right? so we need to check !dio as well?
>
> Thanks,
>
> > + */
> > + map.m_lblk = F2FS_BYTES_TO_BLK(pos);
> > + map.m_len = F2FS_BLK_ALIGN(pos + count);
> > + } else {
> > + /* Do not preallocate blocks that will be written partially in 4KB. */
> > + map.m_lblk = F2FS_BLK_ALIGN(pos);
> > + map.m_len = F2FS_BYTES_TO_BLK(pos + count);
> > + }
> > if (map.m_len > map.m_lblk)
> > map.m_len -= map.m_lblk;
> > else
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 05/14] f2fs: support large folio writeback
2026-08-27 11:17 ` Chao Yu
@ 2026-08-27 22:39 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-27 22:39 UTC (permalink / raw)
To: Chao Yu
Cc: Nanzhe Zhao, linux-f2fs-devel, Jaegeuk Kim, Barry Song,
Juan Yescas, Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Thu, Aug 27, 2026 at 4:24 AM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> On 8/26/26 16:26, Nanzhe Zhao wrote:
> > Large folio can contain multiple dirty ranges.
> > Add a folio-based writeback path for large-folio mapping files
> > and keep the legacy f2fs_write_cache_pages() path unchanged for
> > non large-folio mapping files.
> >
> > Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> > ---
> > fs/f2fs/data.c | 404 ++++++++++++++++++++++++++++++++++++++++++++++++-
> > 1 file changed, 398 insertions(+), 6 deletions(-)
> >
> > diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> > index dff802725544..608d8ea8e607 100644
> > --- a/fs/f2fs/data.c
> > +++ b/fs/f2fs/data.c
> > @@ -358,6 +358,7 @@ static void f2fs_write_end_bio(struct bio *bio)
> > struct folio *folio = fi.folio;
> > unsigned int nr_pages = fi.length >> PAGE_SHIFT;
> > enum count_type type;
> > + bool finished = true;
> >
> > if (fscrypt_is_bounce_folio(folio)) {
> > struct folio *io_folio = folio;
> > @@ -388,11 +389,20 @@ static void f2fs_write_end_bio(struct bio *bio)
> > folio->index, NODE_TYPE_REGULAR, true);
> > f2fs_bug_on(sbi, folio->index != nid_of_node(folio));
> > }
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs =
> > + (struct f2fs_folio_state *)folio->private;
> > +
> > + finished = atomic_sub_and_test(nr_pages,
> > + &ffs->write_pages_pending);
> > + }
> > +
> > + while (nr_pages--)
> > + dec_page_count(sbi, type);
> > +
> > if (f2fs_in_warm_node_list(folio))
> > f2fs_del_fsync_node_entry(sbi, folio);
> >
> > - dec_page_count(sbi, type);
>
> We should not relocate dec_page_count() above f2fs_in_warm_node_list() to
> avoid racing bugs.
>
> > -
> > /*
> > * we should access sbi before folio_end_writeback() to
> > * avoid racing w/ kill_f2fs_super()
> > @@ -401,8 +411,10 @@ static void f2fs_write_end_bio(struct bio *bio)
> > wq_has_sleeper(&sbi->cp_wait))
> > wake_up(&sbi->cp_wait);
> >
> > - folio_clear_f2fs_gcing(folio);
> > - folio_end_writeback(folio);
> > + if (finished) {
> > + folio_clear_f2fs_gcing(folio);
> > + folio_end_writeback(folio);
> > + }
> > }
> >
> > bio_put(bio);
> > @@ -2669,7 +2681,7 @@ static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
> > folio_mark_uptodate(folio);
> > }
> >
> > -static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> > +void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> > size_t offset, size_t len)
> > {
> > struct f2fs_folio_state *ffs;
> > @@ -2692,6 +2704,86 @@ static void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> > spin_unlock_irqrestore(&ffs->state_lock, flags);
> > }
> >
> > +static bool __ffs_clear_subrange_dirty(struct folio *folio,
> > + struct f2fs_folio_state *ffs, size_t offset, size_t len)
> > +{
> > + unsigned int nr_subpages = folio_nr_pages(folio);
> > + unsigned int start, end;
> > +
> > + start = offset >> PAGE_SHIFT;
> > + end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
> > + end = min(end, nr_subpages);
> > +
> > + bitmap_clear(ffs->state, nr_subpages + start, end - start);
> > + return find_next_bit(ffs->state, 2 * nr_subpages, nr_subpages) <
> > + 2 * nr_subpages;
> > +}
> > +
> > +void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len)
> > +{
> > + struct f2fs_folio_state *ffs;
> > + unsigned long flags;
> > +
> > + if (!f2fs_folio_has_ffs(folio))
> > + return;
> > +
> > + ffs = (struct f2fs_folio_state *)folio->private;
> > + spin_lock_irqsave(&ffs->state_lock, flags);
> > + __ffs_clear_subrange_dirty(folio, ffs, offset, len);
> > + spin_unlock_irqrestore(&ffs->state_lock, flags);
> > +}
> > +
> > +static unsigned int ffs_next_dirty_subpage(struct f2fs_folio_state *ffs,
> > + const struct folio *folio, unsigned int start,
> > + unsigned int end)
> > +{
> > + unsigned int nr_subpages = folio_nr_pages(folio);
> > +
> > + return find_next_bit(ffs->state, nr_subpages + end + 1,
> > + nr_subpages + start) - nr_subpages;
> > +}
> > +
> > +static unsigned int ffs_next_clean_subpage(struct f2fs_folio_state *ffs,
> > + const struct folio *folio, unsigned int start,
> > + unsigned int end)
> > +{
> > + unsigned int nr_subpages = folio_nr_pages(folio);
> > +
> > + return find_next_zero_bit(ffs->state, nr_subpages + end + 1,
> > + nr_subpages + start) - nr_subpages;
> > +}
> > +
> > +static unsigned int ffs_find_dirty_range(struct folio *folio,
> > + u64 *range_start, u64 range_end)
> > +{
> > + struct f2fs_folio_state *ffs;
> > + unsigned int start, end, nr_pages;
> > +
> > + if (*range_start >= range_end)
> > + return 0;
> > +
> > + if (!f2fs_folio_has_ffs(folio))
> > + return range_end - *range_start;
> > +
> > + ffs = (struct f2fs_folio_state *)folio->private;
> > + start = offset_in_folio(folio, *range_start) >> PAGE_SHIFT;
> > + end = DIV_ROUND_UP(min_not_zero(offset_in_folio(folio, range_end),
> > + folio_size(folio)), PAGE_SIZE) - 1;
> > +
> > + start = ffs_next_dirty_subpage(ffs, folio, start, end);
> > + if (start > end)
> > + return 0;
> > +
> > + if (start == end)
> > + nr_pages = 1;
> > + else
> > + nr_pages = ffs_next_clean_subpage(ffs, folio,
> > + start + 1, end) - start;
> > +
> > + *range_start = folio_pos(folio) + ((u64)start << PAGE_SHIFT);
> > + return (u64)nr_pages << PAGE_SHIFT;
> > +}
> > +
> > static bool find_next_valid_block(const struct folio *folio,
> > size_t orig_off, size_t *need_off,
> > size_t len)
> > @@ -3309,6 +3401,139 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
> > return err;
> > }
> >
> > +static int f2fs_write_single_data_folio(struct folio *folio, int *submitted,
> > + struct writeback_control *wbc,
> > + enum iostat_type io_type,
> > + u64 start, u64 end)
> > +{
> > + struct inode *inode = folio->mapping->host;
> > + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> > + bool atomic_commit = f2fs_is_atomic_file(inode) &&
> > + folio_test_f2fs_atomic(folio);
> > + struct inode *dn_inode = atomic_commit ?
> > + F2FS_I(inode)->cow_inode : inode;
> > + u64 pos = folio_pos(folio);
> > + pgoff_t start_idx = (start - pos) >> PAGE_SHIFT;
> > + pgoff_t end_idx = (end - 1 - pos) >> PAGE_SHIFT;
> > + int local_submitted = 0;
> > + int err = 0;
> > +
> > + for (pgoff_t i = start_idx; i <= end_idx; i++) {
> > + struct dnode_of_data dn;
> > + struct node_info ni;
> > + pgoff_t data_idx = folio->index + i;
> > + bool ipu_force = false;
> > + struct f2fs_io_info fio = {
> > + .sbi = sbi,
> > + .ino = inode->i_ino,
> > + .type = DATA,
> > + .op = REQ_OP_WRITE,
> > + .op_flags = wbc_to_write_flags(wbc),
> > + .old_blkaddr = NULL_ADDR,
> > + .folio = folio,
> > + .folio_offset = i,
> > + .folio_blkcnt = 1,
> > + .encrypted_page = NULL,
> > + .submitted = 0,
> > + .need_lock = LOCK_DONE,
> > + .meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0,
> > + .io_type = io_type,
> > + .io_wbc = wbc,
> > + };
> > +
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs =
> > + (struct f2fs_folio_state *)folio->private;
> > +
> > + atomic_inc(&ffs->write_pages_pending);
> > + }
> > +
> > + set_new_dnode(&dn, dn_inode, NULL, NULL, 0);
> > +
> > + if (!atomic_commit && need_inplace_update(&fio) &&
>
> In f2fs_write_single_data_page(), we didn't have !atomic_commit condition
> in the check? can we drop !atomic_commit check?
>
> need_inplace_update() has checked w/ f2fs_used_in_atomic_write().
>
> - need_inplace_update
> - f2fs_should_update_outplace
> - f2fs_used_in_atomic_write
>
> static inline bool f2fs_used_in_atomic_write(struct inode *inode)
> {
> return f2fs_is_atomic_file(inode) || f2fs_is_cow_file(inode);
> }
>
> > + f2fs_lookup_read_extent_cache_block(inode, data_idx,
> > + &fio.old_blkaddr)) {
> > + if (!f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
> > + DATA_GENERIC_ENHANCE)) {
> > + err = -EFSCORRUPTED;
> > + goto rollback;
> > + }
> > + ipu_force = true;
> > + goto got_it;
> > + }
> > +
> > + err = f2fs_get_dnode_of_data(&dn, data_idx, LOOKUP_NODE);
> > + if (err)
> > + goto rollback;
> > +
> > + fio.old_blkaddr = dn.data_blkaddr;
> > +
> > +got_it:
> > + if (__is_valid_data_blkaddr(fio.old_blkaddr) &&
> > + !f2fs_is_valid_blkaddr(sbi, fio.old_blkaddr,
> > + DATA_GENERIC_ENHANCE)) {
> > + err = -EFSCORRUPTED;
> > + goto rollback;
> > + }
> > +
> > + if (fio.meta_gc)
> > + f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
> > +
> > + if (!atomic_commit && (ipu_force ||
> > + (__is_valid_data_blkaddr(fio.old_blkaddr) &&
> > + need_inplace_update(&fio)))) {
> > + err = f2fs_encrypt_one_page(&fio);
> > + if (err)
> > + goto rollback;
> > +
> > + f2fs_put_dnode(&dn);
> > + err = f2fs_inplace_write_data(&fio);
> > + if (err) {
> > + if (fscrypt_inode_uses_fs_layer_crypto(inode))
> > + fscrypt_finalize_bounce_page(
> > + &fio.encrypted_page);
> > + goto rollback_no_dnode;
> > + }
> > +
> > + local_submitted++;
> > + set_inode_flag(inode, FI_UPDATE_WRITE);
>
> trace_f2fs_do_write_data_page(folio, IPU);
>
> > + continue;
> > + }
> > +
> > + err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
> > + if (err)
> > + goto rollback;
> > +
> > + fio.version = ni.version;
> > +
> > + err = f2fs_encrypt_one_page(&fio);
> > + if (err)
> > + goto rollback;
> > +
> > + f2fs_outplace_write_data(&dn, &fio);
> > + local_submitted++;
> > + set_inode_flag(inode, FI_APPEND_WRITE);
> > + trace_f2fs_do_write_data_page(folio, OPU);
> > + f2fs_put_dnode(&dn);
> > + continue;
> > +
> > +rollback:
> > + f2fs_put_dnode(&dn);
> > +rollback_no_dnode:
> > + if (f2fs_folio_has_ffs(folio)) {
> > + struct f2fs_folio_state *ffs =
> > + (struct f2fs_folio_state *)folio->private;
> > +
> > + atomic_dec(&ffs->write_pages_pending);
> > + }
> > + break;
> > + }
> > +
> > + if (submitted)
> > + *submitted = local_submitted;
> > + return err;
> > +}
> > +
> > int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> > struct bio **bio,
> > sector_t *last_block,
> > @@ -3757,6 +3982,170 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
> > return ret;
> > }
> >
> > +static int f2fs_write_cache_folios(struct address_space *mapping,
> > + struct writeback_control *wbc,
> > + enum iostat_type io_type)
> > +{
> > + struct folio *folio = NULL;
> > + struct inode *inode = mapping->host;
> > + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> > + struct f2fs_lock_context lc;
> > + u64 pos = 0;
> > + u64 end_pos = 0;
> > + u32 r_len = 0;
> > + int err = 0;
> > + int submitted = 0;
> > + int nwritten = 0;
> > + bool op_locked = false;
> > + bool next = false;
> > + bool retry = false;
> > +
> > + if (get_dirty_pages(inode) <= SM_I(sbi)->min_hot_blocks)
> > + set_inode_flag(inode, FI_HOT_DATA);
> > + else
> > + clear_inode_flag(inode, FI_HOT_DATA);
> > +
> > + while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
> > + struct f2fs_folio_state *ffs = NULL;
> > + u64 isize;
> > + size_t poff;
> > + pgoff_t end_index;
> > + bool verity_in_progress;
> > + int folio_submitted = 0;
> > + bool bias_added = false;
> > +
> > + submitted = 0;
> > + next = true;
> > + retry = false;
> > +
> > + if (atomic_read(&sbi->wb_sync_req[DATA]) &&
> > + wbc->sync_mode == WB_SYNC_NONE) {
> > + folio_redirty_for_writepage(wbc, folio);
> > + next = false;
> > + goto retry_out;
> > + }
> > +retry:
> > + pos = folio_pos(folio);
> > + end_pos = pos + folio_size(folio);
> > + isize = i_size_read(inode);
> > + verity_in_progress = f2fs_verity_in_progress(inode);
> > + poff = 0;
> > + end_index = 0;
> > +
> > + if (retry) {
> > + if (unlikely(folio->mapping != mapping))
> > + goto retry_out;
> > +
> > + if (!folio_test_dirty(folio))
> > + goto retry_out;
> > +
> > + if (folio_test_writeback(folio)) {
> > + if (wbc->sync_mode == WB_SYNC_NONE)
> > + goto retry_out;
> > + f2fs_folio_wait_writeback(folio, DATA, true, true);
> > + }
> > +
> > + if (!folio_clear_dirty_for_io(folio))
> > + goto retry_out;
> > + }
> > +
> > + /* To avoid dealing with the complexity for one subrange is in bio
> > + * while we trylock_op failed before writing another subrange.
> > + * Try to lock_op before any subrange write for the folio.
> > + */
> > + if (!op_locked) {
> > + if (!f2fs_trylock_op(sbi, &lc)) {
> > + folio_redirty_for_writepage(wbc, folio);
> > + err = 0;
> > + if (wbc->sync_mode != WB_SYNC_ALL)
> > + goto retry_out;
> > +
> > + retry = true;
> > + folio_unlock(folio);
> > + f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
> > + folio_lock(folio);
> > + goto retry;
> > + }
> > + op_locked = true;
> > + }
> > +
> > + if (!verity_in_progress) {
> > + poff = offset_in_folio(folio, isize);
> > + end_index = isize >> PAGE_SHIFT;
> > +
> > + if (folio->index > end_index ||
> > + (folio->index == end_index && poff == 0))
> > + goto out;
> > +
> > + if (end_pos > isize) {
> > + folio_zero_segment(folio, poff, folio_size(folio));
> > + end_pos = isize;
> > + }
> > + }
> > +
> > + folio_start_writeback(folio);
> > +
> > + if (folio_test_large(folio)) {
> > + if (!f2fs_folio_has_ffs(folio)) {
>
> Can you explain why there is no ffs for a dirty large folio?
>
> Thanks,
>
> > + ffs = f2fs_ffs_find_or_alloc(folio);
> > + f2fs_ffs_mark_subrange_dirty(folio, 0, end_pos - pos);
> > + } else {
> > + ffs = (struct f2fs_folio_state *)folio->private;
> > + }
> > + if (f2fs_folio_has_ffs(folio) && !bias_added) {
> > + WARN_ON_ONCE(atomic_read(&ffs->write_pages_pending) != 0);
> > + atomic_inc(&ffs->write_pages_pending);
> > + bias_added = true;
> > + }
> > + }
> > +
> > + while ((r_len = ffs_find_dirty_range(folio, &pos, end_pos))) {
> > + err = f2fs_write_single_data_folio(folio, &submitted,
> > + wbc, io_type, pos, pos + r_len);
> > + folio_submitted += submitted;
> > + if (err)
> > + goto out;
> > +
> > + nwritten += submitted;
> > + pos += r_len;
> > + }
> > +
> > + if (!err && folio_submitted &&
> > + f2fs_is_atomic_file(inode) &&
> > + folio_test_f2fs_atomic(folio))
> > + folio_clear_f2fs_atomic(folio);
> > +
> > +out:
> > + f2fs_ffs_clear_subrange_dirty(folio, 0, folio_size(folio));
> > + inode_dec_dirty_pages(inode);
If f2fs_write_single_data_folio() fails on subpage 1, jumping
to `out` unconditionally clears the dirty bitmap for the ENTIRE folio
(0 to folio_size),
and decrements the inode dirty pages.
The unwritten dirty subpages lose their dirty tracking and will be
discarded cleanly
under memory pressure, causing silent data loss.
Fix: Only clear dirty bits for successfully submitted subranges (`0,
pos`), and redirty
the folio if unwritten dirty subpages remain when an error occurs.
Thanks,
> > +
> > + if (bias_added) {
> > + if (atomic_dec_and_test(&ffs->write_pages_pending))
> > + folio_end_writeback(folio);
> > + } else if (!folio_submitted && folio_test_writeback(folio)) {
> > + folio_end_writeback(folio);
> > + }
> > +
> > +retry_out:
> > + if (folio_test_locked(folio))
> > + folio_unlock(folio);
> > +
> > + if (op_locked) {
> > + f2fs_unlock_op(sbi, &lc);
> > + op_locked = false;
> > + }
> > +
> > + if (err || !next)
> > + break;
> > + }
> > +
> > + if (nwritten)
> > + f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
> > + NULL, 0, DATA);
> > +
> > + return err;
> > +}
> > +
> > static inline bool __should_serialize_io(struct inode *inode,
> > struct writeback_control *wbc)
> > {
> > @@ -3851,7 +4240,10 @@ static int __f2fs_write_data_pages(struct address_space *mapping,
> > account_writeback(inode, true);
> >
> > blk_start_plug(&plug);
> > - ret = f2fs_write_cache_pages(mapping, wbc, io_type);
> > + if (mapping_large_folio_support(inode->i_mapping))
> > + ret = f2fs_write_cache_folios(mapping, wbc, io_type);
> > + else
> > + ret = f2fs_write_cache_pages(mapping, wbc, io_type);
> > blk_finish_plug(&plug);
> >
> > account_writeback(inode, false);
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 06/14] f2fs: prepare mmap write faults for large folios
2026-08-27 12:36 ` Chao Yu
@ 2026-08-28 17:18 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-28 17:18 UTC (permalink / raw)
To: Chao Yu
Cc: Nanzhe Zhao, Barry Song, linux-f2fs-devel, Juan Yescas, Dev Jain,
linux-kernel, David Hildenbrand, Bo Zhang, Kalesh Singh,
Nanzhe Zhao, Jaegeuk Kim, Pengfei Li, Ryan Roberts
On Thu, Aug 27, 2026 at 5:37 AM Chao Yu via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> On 8/26/26 16:26, Nanzhe Zhao wrote:
> > Now write protect `mmap` also need to support large folio,
> > Change `f2fs_vm_page_mkwrite` to acheive that.
> >
> > Note it currently marks the whole large folio dirty
> > to avoid data loss which causes write amplification.
> > Further optimization is welcome.
> >
> > PG_mappedtodisk is useless in f2fs, so drop the
> > folio_test_mappedtodisk() check and its goto out_sem
>
> Can you send a separated patch to clean up logic on mappedtodisk flag?
>
> > shortcut in f2fs_vm_page_mkwrite(). We extend the
> > folio_zero_segment() in mkwrite to zero the post-EOF part
> > of the faulted folio for both order-0 and large folios, so
> > the f2fs_zero_post_eof_page() call added to cover that
> > shortcut is no longer needed.
> > > Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> > ---
> > fs/f2fs/f2fs.h | 5 +++++
> > fs/f2fs/file.c | 59 +++++++++++++++++++++++++++++++-------------------
> > 2 files changed, 42 insertions(+), 22 deletions(-)
> >
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index ae6031693700..71e6d7e34c7b 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -4343,6 +4343,11 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> > enum iostat_type io_type,
> > int compr_blocks, bool allow_balance);
> > bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
> > +struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio);
> > +void f2fs_ffs_mark_subrange_dirty(struct folio *folio, size_t offset, size_t len);
> > +bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,
> > + size_t len);
> > +void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len);
> > void f2fs_write_failed(struct inode *inode, loff_t to);
> > void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
> > bool f2fs_release_folio(struct folio *folio, gfp_t wait);
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index e40285d4f9c2..02d687527241 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -124,6 +124,13 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> > struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> > struct dnode_of_data dn;
> > bool need_alloc = !f2fs_is_pinned_file(inode);
> > + pgoff_t pidx = folio->index + folio_page_idx(folio, vmf->page);
> > + loff_t pos = (loff_t)pidx << PAGE_SHIFT;
> > + loff_t isize;
> > + loff_t folio_start;
> > + loff_t valid_end;
> > + size_t dirty_len;
> > + size_t subpage_off;
> > int err = 0;
> > vm_fault_t ret;
> >
> > @@ -160,7 +167,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> >
> > #ifdef CONFIG_F2FS_FS_COMPRESSION
> > if (f2fs_compressed_file(inode)) {
> > - int ret = f2fs_is_compressed_cluster(inode, folio->index);
> > + int ret = f2fs_is_compressed_cluster(inode, pidx);
> >
> > if (ret < 0) {
> > err = ret;
> > @@ -178,18 +185,20 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> >
> > f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
> >
> > - err = f2fs_zero_post_eof_page(inode,
> > - (folio->index + 1) << PAGE_SHIFT, true, false);
> > - if (err)
> > - goto out_pagefault;
> > -
> > file_update_time(vmf->vma->vm_file);
> > filemap_invalidate_lock_shared(inode->i_mapping);
> >
> > folio_lock(folio);
> > + isize = i_size_read(inode);
> > + folio_start = folio_pos(folio);
> > + subpage_off = offset_in_folio(folio, pos);
> > + valid_end = min_t(loff_t, folio_start + folio_size(folio), isize);
> > + dirty_len = valid_end > folio_start ? valid_end - folio_start : 0;
> > +
> > if (unlikely(folio->mapping != inode->i_mapping ||
> > - folio_pos(folio) > i_size_read(inode) ||
> > - !folio_test_uptodate(folio))) {
> > + pos >= isize ||
> > + !f2fs_ffs_test_blk_uptodate(folio,
> > + folio->index + (subpage_off >> PAGE_SHIFT)))) {
> > folio_unlock(folio);
> > err = -EFAULT;
> > goto out_sem;
> > @@ -198,9 +207,19 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> > set_new_dnode(&dn, inode, NULL, NULL, 0);
> > if (need_alloc) {
> > /* block allocation */
> > - err = f2fs_get_block_locked(&dn, folio->index);
> > + if (folio_test_large(folio)) {
> > + pgoff_t i, nr = DIV_ROUND_UP(dirty_len, PAGE_SIZE);
> > +
> > + for (i = 0; i < nr; i++) {
> > + err = f2fs_get_block_locked(&dn, folio->index + i);
> > + if (err)
> > + break;
>
> We only need to call f2fs_get_block_locked() for vmf->page?
>
> Hi Barry, could you please help to confirm this? Only vmf->page contain dirty
> data, rather than whole large folio contain dirty data?
>
> Thanks,
>
> > + }
> > + } else {
> > + err = f2fs_get_block_locked(&dn, pidx);
> > + }
> > } else {
> > - err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
> > + err = f2fs_get_dnode_of_data(&dn, pidx, LOOKUP_NODE);
> > f2fs_put_dnode(&dn);
> > if (f2fs_is_pinned_file(inode) &&
> > !__is_valid_data_blkaddr(dn.data_blkaddr))
> > @@ -217,20 +236,17 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> > /* wait for GCed page writeback via META_MAPPING */
> > f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
Is it okay to wait for the last block of the large folio here?
> >
> > - /*
> > - * check to see if the page is mapped already (no holes)
> > - */
> > - if (folio_test_mappedtodisk(folio))
> > - goto out_sem;
> > -
> > /* page is wholly or partially inside EOF */
> > - if (((loff_t)(folio->index + 1) << PAGE_SHIFT) >
> > - i_size_read(inode)) {
> > - loff_t offset;
> > + if (folio_start + folio_size(folio) > isize) {
> > + size_t offset = offset_in_folio(folio, isize);
> >
> > - offset = i_size_read(inode) & ~PAGE_MASK;
> > folio_zero_segment(folio, offset, folio_size(folio));
> > }
> > +
> > + if (folio_test_large(folio)) {
> > + f2fs_ffs_find_or_alloc(folio);
> > + f2fs_ffs_mark_subrange_dirty(folio, 0, dirty_len);
> > + }
> > folio_mark_dirty(folio);
> >
> > f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
Need to update for dirty_len?
> > @@ -238,12 +254,11 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> >
> > out_sem:
> > filemap_invalidate_unlock_shared(inode->i_mapping);
> > -out_pagefault:
> > sb_end_pagefault(inode->i_sb);
> > out:
> > ret = vmf_fs_error(err);
> >
> > - trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
> > + trace_f2fs_vm_page_mkwrite(inode, pidx, vmf->vma->vm_flags, ret);
> > return ret;
> > }
> >
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 07/14] f2fs: make GC migration large-folio aware
2026-08-26 8:26 ` [PATCH 07/14] f2fs: make GC migration large-folio aware Nanzhe Zhao
@ 2026-08-28 17:20 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-28 17:20 UTC (permalink / raw)
To: Nanzhe Zhao
Cc: linux-f2fs-devel, Jaegeuk Kim, Chao Yu, Barry Song, Juan Yescas,
Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Wed, Aug 26, 2026 at 1:33 AM Nanzhe Zhao via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> GC can operate on a 4K block that is cached inside a large folio.
> The data lookup helpers therefore need to test and update uptodate
> state for the addressed subpage instead of rejecting large folios or
> treating the whole folio as the target block.
>
> Let f2fs_get_read_data_folio(), f2fs_find_data_folio(), and
> f2fs_get_lock_data_folio() to use subpage uptodate state. Submit
> single-block reads at the requested folio offset and zero only the
> addressed 4K range for NEW_ADDR.
>
> Also update `move_data_page` to mark, clear, and restore dirty
> state for the target subpage, and submit write I/O with the subpage
> offset recorded in f2fs_io_info.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 96 ++++++++++++++++++++++++++++++++++----------------
> fs/f2fs/f2fs.h | 6 ++--
> fs/f2fs/gc.c | 30 ++++++++++++++--
> 3 files changed, 97 insertions(+), 35 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 608d8ea8e607..0e54b1e25893 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -1241,19 +1241,31 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode,
>
> /* This can handle encryption stuffs */
> static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
> - struct folio *folio, block_t blkaddr,
> - blk_opf_t op_flags, bool for_write)
> + struct folio *folio, pgoff_t index,
> + block_t blkaddr, blk_opf_t op_flags,
> + bool for_write)
> {
> struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
> struct bio *bio;
> + size_t offset = 0;
>
> - bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, folio->index,
> + if (f2fs_folio_has_ffs(folio)) {
> + struct f2fs_folio_state *ffs = folio->private;
> + unsigned long flags;
> +
> + offset = offset_in_folio(folio, (loff_t)index << PAGE_SHIFT);
> + spin_lock_irqsave(&ffs->state_lock, flags);
> + ffs->read_pages_pending++;
> + spin_unlock_irqrestore(&ffs->state_lock, flags);
> + }
> +
> + bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, index,
> for_write);
>
> /* wait for GCed page writeback via META_MAPPING */
> f2fs_wait_on_block_writeback(inode, blkaddr);
>
> - if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
> + if (!bio_add_folio(bio, folio, PAGE_SIZE, offset))
> f2fs_bug_on(sbi, 1);
>
> inc_page_count(sbi, F2FS_RD_DATA);
> @@ -1399,21 +1411,13 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
> struct dnode_of_data dn;
> struct folio *folio;
> int err;
> -retry:
> +
> folio = f2fs_grab_cache_folio(mapping, index, for_write);
> if (IS_ERR(folio))
> return folio;
>
> - if (folio_test_large(folio)) {
> - pgoff_t folio_index = mapping_align_index(mapping, index);
> - unsigned long nr_pages = folio_nr_pages(folio);
> -
> - f2fs_folio_put(folio, true);
> - invalidate_inode_pages2_range(mapping, folio_index,
> - folio_index + nr_pages - 1);
> - f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
> - goto retry;
> - }
> + if (folio_test_large(folio))
> + f2fs_ffs_find_or_alloc(folio);
>
> if (f2fs_lookup_read_extent_cache_block(inode, index,
> &dn.data_blkaddr)) {
> @@ -1448,7 +1452,7 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
> goto put_err;
> }
> got_it:
> - if (folio_test_uptodate(folio)) {
> + if (f2fs_ffs_test_blk_uptodate(folio, index)) {
> folio_unlock(folio);
> return folio;
> }
> @@ -1461,15 +1465,17 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
> * f2fs_init_inode_metadata.
> */
> if (dn.data_blkaddr == NEW_ADDR) {
> - folio_zero_segment(folio, 0, folio_size(folio));
> - if (!folio_test_uptodate(folio))
> - folio_mark_uptodate(folio);
> + size_t offset = offset_in_folio(folio,
> + (loff_t)index << PAGE_SHIFT);
> +
> + folio_zero_segment(folio, offset, offset + PAGE_SIZE);
> + f2fs_ffs_mark_subrange_uptodate(folio, offset, PAGE_SIZE);
> folio_unlock(folio);
> return folio;
> }
>
> - f2fs_submit_page_read(inode, f2fs_need_verity(inode, folio->index),
> - folio, dn.data_blkaddr, op_flags, for_write);
> + f2fs_submit_page_read(inode, f2fs_need_verity(inode, index),
> + folio, index, dn.data_blkaddr, op_flags, for_write);
> return folio;
>
> put_err:
> @@ -1486,7 +1492,7 @@ struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,
> folio = f2fs_filemap_get_folio(mapping, index, FGP_ACCESSED, 0);
> if (IS_ERR(folio))
> goto read;
> - if (folio_test_uptodate(folio))
> + if (f2fs_ffs_test_blk_uptodate(folio, index))
> return folio;
> f2fs_folio_put(folio, false);
>
> @@ -1495,11 +1501,11 @@ struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,
> if (IS_ERR(folio))
> return folio;
>
> - if (folio_test_uptodate(folio))
> + if (f2fs_ffs_test_blk_uptodate(folio, index))
> return folio;
>
> folio_wait_locked(folio);
> - if (unlikely(!folio_test_uptodate(folio))) {
> + if (unlikely(!f2fs_ffs_test_blk_uptodate(folio, index))) {
> f2fs_folio_put(folio, false);
> return ERR_PTR(-EIO);
> }
> @@ -1523,7 +1529,8 @@ struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,
>
> /* wait for read completion */
> folio_lock(folio);
> - if (unlikely(folio->mapping != mapping || !folio_test_uptodate(folio))) {
> + if (unlikely(folio->mapping != mapping ||
> + !f2fs_ffs_test_blk_uptodate(folio, index))) {
> f2fs_folio_put(folio, true);
> return ERR_PTR(-EIO);
> }
> @@ -2681,6 +2688,23 @@ static void f2fs_ffs_mark_subrange_uptodate(struct folio *folio, size_t offset,
> folio_mark_uptodate(folio);
> }
>
> +bool f2fs_ffs_test_blk_dirty(const struct folio *folio, pgoff_t index)
> +{
> + struct f2fs_folio_state *ffs;
> + unsigned int idx, nr_subpages;
> +
> + if (!f2fs_folio_has_ffs(folio))
> + return folio_test_dirty(folio);
> +
> + if (!folio_contains(folio, index))
> + return false;
> +
> + ffs = folio->private;
> + nr_subpages = folio_nr_pages(folio);
> + idx = index - folio->index;
> + return test_bit(nr_subpages + idx, ffs->state);
> +}
> +
> void f2fs_ffs_mark_subrange_dirty(struct folio *folio,
> size_t offset, size_t len)
> {
> @@ -2719,18 +2743,30 @@ static bool __ffs_clear_subrange_dirty(struct folio *folio,
> 2 * nr_subpages;
> }
>
> -void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len)
> +/**
> + * f2fs_ffs_clear_subrange_dirty - clear the dirty bits of a folio subrange
> + * @folio: the large folio
> + * @offset: start byte offset within the folio
> + * @len: byte length of the subrange
> + *
> + * Clear the dirty bits of the 4K subpages covered by [offset, offset + len),
> + * and return whether the folio still has any dirty subpage left.
> + */
> +bool f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset,
> + size_t len)
> {
> struct f2fs_folio_state *ffs;
> unsigned long flags;
> + bool dirty;
>
> if (!f2fs_folio_has_ffs(folio))
> - return;
> + return false;
>
> - ffs = (struct f2fs_folio_state *)folio->private;
> + ffs = folio->private;
> spin_lock_irqsave(&ffs->state_lock, flags);
> - __ffs_clear_subrange_dirty(folio, ffs, offset, len);
> + dirty = __ffs_clear_subrange_dirty(folio, ffs, offset, len);
> spin_unlock_irqrestore(&ffs->state_lock, flags);
> + return dirty;
> }
>
> static unsigned int ffs_next_dirty_subpage(struct f2fs_folio_state *ffs,
> @@ -4853,7 +4889,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> */
> f2fs_submit_page_read(inode,
> NULL, /* can't write to fsverity files */
> - folio, blkaddr, 0, true);
> + folio, index, blkaddr, 0, true);
>
> folio_lock(folio);
> if (unlikely(folio->mapping != mapping)) {
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 71e6d7e34c7b..0ffbe2bd04c8 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -4344,10 +4344,10 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,
> int compr_blocks, bool allow_balance);
> bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index);
> struct f2fs_folio_state *f2fs_ffs_find_or_alloc(struct folio *folio);
> +bool f2fs_ffs_test_blk_dirty(const struct folio *folio, pgoff_t index);
> void f2fs_ffs_mark_subrange_dirty(struct folio *folio, size_t offset, size_t len);
> -bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,
> - size_t len);
> -void f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset, size_t len);
> +bool f2fs_ffs_clear_subrange_dirty(struct folio *folio, size_t offset,
> + size_t len);
> void f2fs_write_failed(struct inode *inode, loff_t to);
> void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
> bool f2fs_release_folio(struct folio *folio, gfp_t wait);
> diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
> index 0c17038fcfd7..42e6dfcbf98d 100644
> --- a/fs/f2fs/gc.c
> +++ b/fs/f2fs/gc.c
> @@ -1504,12 +1504,19 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
> unsigned int segno, int off)
> {
> struct folio *folio;
> + size_t foff = 0;
> + bool large = false;
> int err = 0;
>
> folio = f2fs_get_lock_data_folio(inode, bidx, true);
> if (IS_ERR(folio))
> return PTR_ERR(folio);
>
> + if (f2fs_folio_has_ffs(folio)) {
> + large = true;
> + foff = offset_in_folio(folio, (loff_t)bidx << PAGE_SHIFT);
> + }
> +
> if (!check_valid_map(F2FS_I_SB(inode), segno, off)) {
> err = -ENOENT;
> goto out;
> @@ -1524,6 +1531,8 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
> err = -EAGAIN;
> goto out;
> }
> + if (large)
> + f2fs_ffs_mark_subrange_dirty(folio, foff, PAGE_SIZE);
> folio_mark_dirty(folio);
> folio_set_f2fs_gcing(folio);
> } else {
> @@ -1536,32 +1545,49 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
> .op_flags = REQ_SYNC,
> .old_blkaddr = NULL_ADDR,
> .folio = folio,
> + .folio_offset = bidx - folio->index,
> + .folio_blkcnt = 1,
> .encrypted_page = NULL,
> .need_lock = LOCK_REQ,
> .io_type = FS_GC_DATA_IO,
> };
> - bool is_dirty = folio_test_dirty(folio);
> + struct f2fs_folio_state *ffs = NULL;
> + bool is_dirty = f2fs_ffs_test_blk_dirty(folio, bidx);
>
> retry:
> f2fs_folio_wait_writeback(folio, DATA, true, true);
>
> + if (large) {
> + ffs = folio->private;
> + f2fs_ffs_mark_subrange_dirty(folio, foff, PAGE_SIZE);
> + }
> folio_mark_dirty(folio);
> if (folio_clear_dirty_for_io(folio)) {
> inode_dec_dirty_pages(inode);
> f2fs_remove_dirty_inode(inode);
> + if (large &&
> + f2fs_ffs_clear_subrange_dirty(folio, foff, PAGE_SIZE))
> + folio_mark_dirty(folio);
> }
>
> + if (large)
> + atomic_inc(&ffs->write_pages_pending);
Under Patch 08, a cleanly read large folio may NOT have
f2fs_folio_state allocated
(folio->private == NULL), right?
When foreground/background GC tries to migrate a 4KB block in such a folio,
`ffs` is NULL, and `atomic_inc(&ffs->write_pages_pending)` will instantly crash
with a NULL pointer dereference!
Fix: Call `ffs = f2fs_ffs_find_or_alloc(folio);` before accessing `ffs`.
Thanks,
> folio_set_f2fs_gcing(folio);
>
> err = f2fs_do_write_data_page(&fio);
> if (err) {
> folio_clear_f2fs_gcing(folio);
> + if (large)
> + atomic_dec(&ffs->write_pages_pending);
> if (err == -ENOMEM) {
> memalloc_retry_wait(GFP_NOFS);
> goto retry;
> }
> - if (is_dirty)
> + if (is_dirty) {
> + if (large)
> + f2fs_ffs_mark_subrange_dirty(folio, foff, PAGE_SIZE);
> folio_mark_dirty(folio);
> + }
> }
> }
> out:
> --
> 2.43.0
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 13/14] f2fs: allow large folio support to writeable files
2026-08-26 13:09 ` [PATCH 13/14] f2fs: allow large folio support to writeable files Nanzhe Zhao
@ 2026-08-28 17:44 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-28 17:44 UTC (permalink / raw)
To: Nanzhe Zhao
Cc: linux-f2fs-devel, Jaegeuk Kim, Chao Yu, Barry Song, Juan Yescas,
Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Wed, Aug 26, 2026 at 8:07 AM Nanzhe Zhao via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> Now we make all write path support large folios,
> so we open permission to let writeable file set
> large folio mapping.
>
> Keep fs-layer encrypted files excluded unless inline encryption is
> enabled, since f2fs_encrypt_one_page() still encrypts one PAGE_SIZE
> page and cannot handle large folios.
>
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 9 ++++++++-
> fs/f2fs/f2fs.h | 13 +++++++++++++
> fs/f2fs/file.c | 16 ----------------
> fs/f2fs/inode.c | 4 +---
> fs/f2fs/namei.c | 1 +
> 5 files changed, 23 insertions(+), 20 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 0b167b14a9a5..2e20833c6417 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -2955,7 +2955,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,
> int ret = 0;
> bool folio_in_bio = false;
>
> - if (!IS_IMMUTABLE(inode) || f2fs_compressed_file(inode)) {
> + if (f2fs_compressed_file(inode)) {
> if (folio)
> folio_unlock(folio);
> return -EOPNOTSUPP;
> @@ -3321,6 +3321,13 @@ int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
> if (fscrypt_inode_uses_inline_crypto(inode))
> return 0;
>
> + if (folio_test_large(page_folio(page))) {
> + f2fs_warn_ratelimited(F2FS_I_SB(inode),
> + "large folio does not support fs-layer encryption, ino=%llu",
> + (unsigned long long)inode->i_ino);
> + return -EOPNOTSUPP;
> + }
> +
> fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page_folio(page),
> PAGE_SIZE, 0, GFP_NOFS);
> if (IS_ERR(fio->encrypted_page))
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 6ae249bf9aa1..04ed0ea92b26 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -5201,6 +5201,19 @@ static inline bool f2fs_quota_file(struct f2fs_sb_info *sbi, nid_t ino)
> return false;
> }
>
> +static inline void f2fs_mapping_set_large_folio(struct inode *inode)
> +{
> + if (!S_ISREG(inode->i_mode) ||
> + f2fs_has_inline_data(inode) ||
> + f2fs_compressed_file(inode) ||
> + f2fs_quota_file(F2FS_I_SB(inode), inode->i_ino) ||
> + (f2fs_encrypted_file(inode) &&
> + !(inode->i_sb->s_flags & SB_INLINECRYPT)))
> + return;
> +
> + mapping_set_folio_min_order(inode->i_mapping, 0);
> +}
In F2FS, newly created files almost always start with inline_data
(`FI_INLINE_DATA`).
Because f2fs_has_inline_data() is true during f2fs_new_inode() / f2fs_iget(),
mapping_set_large_folios is skipped.
When the file later expands and is converted to regular blocks in
f2fs_convert_inline_inode(),
`f2fs_mapping_set_large_folio()` is NEVER called.
As a result, almost NO newly created file will ever use Large Folios
during its lifetime.
Fix: Call `f2fs_mapping_set_large_folio(inode)` inside
`f2fs_convert_inline_inode()` once
inline data is converted. Also consider using the standard
`mapping_set_large_folios(mapping)`
helper instead of `mapping_set_folio_min_order(mapping, 0)`.
Thanks,
> +
> static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
> {
> return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
> diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> index b82acbc3240f..c8426821e701 100644
> --- a/fs/f2fs/file.c
> +++ b/fs/f2fs/file.c
> @@ -134,17 +134,6 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
> int err = 0;
> vm_fault_t ret;
>
> - /*
> - * We only support large folio on the read case.
> - * Don't make any dirty pages.
> - */
> - if (unlikely(IS_IMMUTABLE(inode)) ||
> - mapping_large_folio_support(inode->i_mapping)) {
> - f2fs_err(sbi, "Not expected: immutable: %d large_folio: %d",
> - IS_IMMUTABLE(inode),
> - mapping_large_folio_support(inode->i_mapping));
> - return VM_FAULT_SIGBUS;
> - }
>
> if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
> err = -EIO;
> @@ -698,9 +687,6 @@ static int f2fs_file_open(struct inode *inode, struct file *filp)
> if (!f2fs_is_compress_backend_ready(inode))
> return -EOPNOTSUPP;
>
> - if (mapping_large_folio_support(inode->i_mapping) &&
> - filp->f_mode & FMODE_WRITE)
> - return -EOPNOTSUPP;
>
> err = fsverity_file_open(inode, filp);
> if (err)
> @@ -1177,8 +1163,6 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
> return -EPERM;
>
> if ((attr->ia_valid & ATTR_SIZE)) {
> - if (mapping_large_folio_support(inode->i_mapping))
> - return -EOPNOTSUPP;
> if (IS_DEVICE_ALIASING(inode))
> return -EPERM;
> if (!f2fs_is_compress_backend_ready(inode))
> diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c
> index 96cc0e777567..7c4b5cdfe276 100644
> --- a/fs/f2fs/inode.c
> +++ b/fs/f2fs/inode.c
> @@ -642,9 +642,7 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
> inode->i_op = &f2fs_file_inode_operations;
> inode->i_fop = &f2fs_file_operations;
> inode->i_mapping->a_ops = &f2fs_dblock_aops;
> - if (IS_IMMUTABLE(inode) && !f2fs_compressed_file(inode) &&
> - !f2fs_quota_file(sbi, inode->i_ino))
> - mapping_set_folio_min_order(inode->i_mapping, 0);
> + f2fs_mapping_set_large_folio(inode);
> } else if (S_ISDIR(inode->i_mode)) {
> inode->i_op = &f2fs_dir_inode_operations;
> inode->i_fop = &f2fs_dir_operations;
> diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
> index afaab7739283..bdf4e586bfcf 100644
> --- a/fs/f2fs/namei.c
> +++ b/fs/f2fs/namei.c
> @@ -341,6 +341,7 @@ static struct inode *f2fs_new_inode(struct mnt_idmap *idmap,
> f2fs_set_inode_flags(inode);
>
> f2fs_init_extent_tree(inode);
> + f2fs_mapping_set_large_folio(inode);
>
> trace_f2fs_new_inode(inode, 0);
> return inode;
> --
> 2.43.0
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
* Re: [f2fs-dev] [PATCH 14/14] f2fs: make compressed files compatible with large folio
2026-08-26 13:09 ` [PATCH 14/14] f2fs: make compressed files compatible with large folio Nanzhe Zhao
@ 2026-08-28 17:52 ` Daeho Jeong
0 siblings, 0 replies; 29+ messages in thread
From: Daeho Jeong @ 2026-08-28 17:52 UTC (permalink / raw)
To: Nanzhe Zhao
Cc: linux-f2fs-devel, Jaegeuk Kim, Chao Yu, Barry Song, Juan Yescas,
Dev Jain, linux-kernel, David Hildenbrand, Bo Zhang,
Kalesh Singh, Nanzhe Zhao, Pengfei Li, Ryan Roberts
On Wed, Aug 26, 2026 at 8:10 AM Nanzhe Zhao via Linux-f2fs-devel
<linux-f2fs-devel@lists.sourceforge.net> wrote:
>
> The compression flag is the hint indicates that the inode can be
> compressed, when the inode is using large folio, we expected it keeps
> using the large folio read/write paths and its data stays uncompressed
> on disk until the inode is evicted and re-read
>
> Let f2fs_write_begin() skip the compression overwrite preparation for
> such inodes and remove the compressed-file gate in
> f2fs_read_data_large_folio() so the data is simply read/written as
> regular blocks.
>
Patch 14 removes the compressed-file gate in
f2fs_read_data_large_folio() under the
assumption that "data stays uncompressed on disk".
However, if a file already contains existing compressed clusters on
disk (e.g. from
earlier writes before large folios or created without large folios),
`f2fs_read_data_large_folio()` will issue regular bio reads without
decompressing them.
Userspace reading from an existing compressed cluster will receive raw
compressed
bytes instead of plaintext data.
Fix: If an inode has existing compressed clusters (or until large
folio read path
supports decompression), I think we must not read compressed clusters
via f2fs_read_data_large_folio().
Thanks,
> Signed-off-by: Nanzhe Zhao <zhaonanzhe@xiaomi.com>
> ---
> fs/f2fs/data.c | 9 ++-------
> 1 file changed, 2 insertions(+), 7 deletions(-)
>
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index 2e20833c6417..befcdc3742f6 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -2955,12 +2955,6 @@ static int f2fs_read_data_large_folio(struct inode *inode,
> int ret = 0;
> bool folio_in_bio = false;
>
> - if (f2fs_compressed_file(inode)) {
> - if (folio)
> - folio_unlock(folio);
> - return -EOPNOTSUPP;
> - }
> -
> map.m_seg_type = NO_CHECK_TYPE;
>
> if (rac)
> @@ -4935,7 +4929,8 @@ static int f2fs_write_begin(const struct kiocb *iocb,
> }
>
> #ifdef CONFIG_F2FS_FS_COMPRESSION
> - if (f2fs_compressed_file(inode)) {
> + if (f2fs_compressed_file(inode) &&
> + !mapping_large_folio_support(inode->i_mapping)) {
> int ret;
> struct page *page;
>
> --
> 2.43.0
>
>
>
> _______________________________________________
> Linux-f2fs-devel mailing list
> Linux-f2fs-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
^ permalink raw reply [flat|nested] 29+ messages in thread
end of thread, other threads:[~2026-08-28 17:52 UTC | newest]
Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-26 8:26 [PATCH 00/14] f2fs: support & optimize large folios for writable files Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 01/14] f2fs: extend folio state for large folio write path Nanzhe Zhao
2026-08-27 6:57 ` Chao Yu
2026-08-27 20:51 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 02/14] f2fs: carry subpage offset and count in write IO Nanzhe Zhao
2026-08-27 7:16 ` Chao Yu
2026-08-27 21:06 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 03/14] f2fs: support regular file buffered writes on large folios Nanzhe Zhao
2026-08-27 8:56 ` Chao Yu
2026-08-27 21:13 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 04/14] f2fs: support atomic file large folios buffered write Nanzhe Zhao
2026-08-27 9:24 ` Chao Yu
2026-08-26 8:26 ` [PATCH 05/14] f2fs: support large folio writeback Nanzhe Zhao
2026-08-27 11:17 ` Chao Yu
2026-08-27 22:39 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 06/14] f2fs: prepare mmap write faults for large folios Nanzhe Zhao
2026-08-27 12:36 ` Chao Yu
2026-08-28 17:18 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 07/14] f2fs: make GC migration large-folio aware Nanzhe Zhao
2026-08-28 17:20 ` [f2fs-dev] " Daeho Jeong
2026-08-26 8:26 ` [PATCH 08/14] f2fs: optimize small block size large folio read Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 09/14] f2fs: support partial uptodate " Nanzhe Zhao
2026-08-26 8:26 ` [PATCH 10/14] f2fs: handle partial truncate of large folio dirty subpages Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 11/14] f2fs: fix zeroing paths for large folios Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 12/14] f2fs: handle block cloning within the same large folio Nanzhe Zhao
2026-08-26 13:09 ` [PATCH 13/14] f2fs: allow large folio support to writeable files Nanzhe Zhao
2026-08-28 17:44 ` [f2fs-dev] " Daeho Jeong
2026-08-26 13:09 ` [PATCH 14/14] f2fs: make compressed files compatible with large folio Nanzhe Zhao
2026-08-28 17:52 ` [f2fs-dev] " Daeho Jeong
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®