Skip to contentSkip to conversation

f2fs: introduce metadata cache

PATCH Superseded Chao Yu 2 participants 27 messages Sep 11 – Sep 15

Subsystems (2)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

v7 cover f2fs: introduce metadata cache

This patchset introduces a self-managed metadata block cache in f2fs, decoupling meta blocks, node blocks, and compressed data blocks from the Linux VFS page cache and fake internal inodes.

=== 1. Background & Motivation ===

Currently, F2FS uses fake VFS inodes (meta_inode, node_inode, and compress_inode) to manage internal block caching through the VFS page cache. Because of this implementation, the f2fs block size was historically coupled to the kernel page size.

We now want to unbind block size from page size to support configurations where block size <= PAGE_SIZE (e.g., mounting a 4KB-block F2FS image on a 16KB or 64KB page system).

One possible approach is to continue using the VFS page cache to store metadata blocks. However, doing so introduces three major architectural issues (illustrated by a 4KB block on a 16KB page system):

1. Memory Overhead:
Metadata access patterns are typically random and sparse. Caching a single 4KB metadata block inside a page cache folio forces the allocation of an entire 16KB folio, resulting in 4x memory waste.

2. Folio and Sub-block Conversion Complexity:
Using larger folios requires tracking individual sub-block dirty/uptodate states within each folio and performing index-to-offset conversions across function boundaries. Because core metadata structures (e.g., f2fs_checkpoint, f2fs_sit_block, f2fs_nat_block, f2fs_summary_block, f2fs_node) are accessed extensively throughout the filesystem, this sub-block management and offset calculation complexity would spread across the entire F2FS codebase.

3. Lock Contention:
Multiple independent node blocks (e.g., dnode blocks belonging to different files) can reside within the same folio. Concurrent fsync() calls on unrelated files would contend on the same folio_lock(), serializing metadata updates and degrading multi-threaded performance.

Decoupling metadata caching from PAGE_SIZE by allocating exact block-sized cache entries is the critical first step toward supporting 4KB-block F2FS images on 16KB/64KB page systems.

=== 2. Metadata Cache Architecture & Design ===

This patchset introduces a dedicated, block-size-aligned caching infrastructure with the following key components:

- Block-Size Aligned Allocation:
Allocates memory buffers matching exactly the filesystem block size (4KB or 16KB) via kzalloc(), fully independent of the host architecture's PAGE_SIZE.

- Radix Tree Indexing with Fast Tag Scanning:
Each cache instance (META_CACHE, NODE_CACHE, COMPRESS_CACHE) indexes cached blocks via a radix tree (keyed by Physical Block Address for meta/ compress cache, and Node ID for node cache). Radix tree tags (F2FS_CACHE_TAG_DIRTY, F2FS_CACHE_TAG_WRITEBACK) provide O(1) batch gang lookups for flushing and writeback without dual-list shuffling.

- Lightweight Bit-Locking:
Individual entries use atomic bit locks (F2FS_BLOCK_LOCKED via wait_on_bit_lock() / clear_and_wake_up_bit()) rather than heavyweight embedded mutexes/semaphores, minimizing memory footprint per entry.

- Direct BIO Read/Write & BIO Merging:
Decouples metadata/node I/O from VFS address spaces by submitting direct BIOs (f2fs_submit_cache_read / f2fs_submit_cache_write) with chained adjacent vector merging (entry->next_entry) and dedicated completion handlers.

- Memory Reclamation Shrinker:
Integrates with the kernel shrinker subsystem via a 3-phase isolation algorithm (isolate unreferenced clean entries -> truncate from radix tree under lock -> splice un-reclaimed entries back to LRU) to safely reclaim clean cached blocks under system memory pressure.

- Background Writeback Kthread & Checkpoint Integration:
Provides a dedicated background kthread (f2fs_writeback-X:Y) for periodic dirty cache flushing, combined with synchronous flushing during checkpoint commit.

- Fault Injection, Tracepoints & Debugfs Observability:
Integrates FAULT_KALLOC fault injection, tracepoints for cache state transitions and batch writeback, and per-cache memory breakdowns in debugfs.

=== 3. Patchset Organization ===

- Patch 01: Implement the core metadata cache infrastructure & direct BIO I/O.
- Patch 02: Initialize and teardown META_CACHE in sb_info.
- Patch 03: Integrate metadata cache into the memory shrinker subsystem.
- Patch 04: Introduce the background writeback kernel thread.
- Patch 05: Migrate metadata block caching (SIT, NAT, SSA, CP, recovery, GC) from meta_inode to META_CACHE.
- Patch 06: Initialize and teardown NODE_CACHE in sb_info.
- Patch 07: Migrate node and inode block caching from node_inode to NODE_CACHE.
- Patch 08: Initialize and teardown COMPRESS_CACHE in sb_info.
- Patch 09: Migrate compressed cluster caching from compress_inode to COMPRESS_CACHE.
- Patch 10: Add fault injection support for cache allocation paths.
- Patch 11: Introduce ftrace tracepoints for cache dirty and writeback events.
- Patch 12: Expose per-cache memory usage in debugfs.

Changelog:

v6->v7:
- rebase on last dev-test
- clean up comments, functions and macro names from page to cache/block based on Jaegeuk's patches

Chao Yu (12):
f2fs: cache: implement metadata cache
f2fs: cache: initialize meta cache
f2fs: cache: introduce shrinker
f2fs: cache: introduce writeback thread
f2fs: cache: use meta cache
f2fs: cache: initialize node cache
f2fs: cache: use node cache
f2fs: cache: initialize compress cache
f2fs: cache: use compress cache
f2fs: cache: support fault injection
f2fs: cache: introduce tracepoints
f2fs: cache: show per-cache usage in debugfs

 Documentation/ABI/testing/sysfs-fs-f2fs |    6 +
 fs/f2fs/Makefile                        |    2 +-
 fs/f2fs/acl.c                           |   26 +-
 fs/f2fs/acl.h                           |    8 +-
 fs/f2fs/cache.c                         |  708 +++++++++++++++
 fs/f2fs/cache.h                         |  243 +++++
 fs/f2fs/checkpoint.c                    |  434 +++++----
 fs/f2fs/compress.c                      |  165 ++--
 fs/f2fs/data.c                          |  521 +++++++----
 fs/f2fs/debug.c                         |   70 +-
 fs/f2fs/dir.c                           |  170 ++--
 fs/f2fs/extent_cache.c                  |   14 +-
 fs/f2fs/f2fs.h                          |  376 ++++----
 fs/f2fs/file.c                          |   82 +-
 fs/f2fs/gc.c                            |  185 ++--
 fs/f2fs/inline.c                        |  284 +++---
 fs/f2fs/inode.c                         |  217 ++---
 fs/f2fs/iostat.h                        |   11 +
 fs/f2fs/namei.c                         |  114 +--
 fs/f2fs/node.c                          | 1102 +++++++++++------------
 fs/f2fs/node.h                          |  118 +--
 fs/f2fs/recovery.c                      |  251 +++---
 fs/f2fs/segment.c                       |  305 ++++---
 fs/f2fs/segment.h                       |   37 +-
 fs/f2fs/shrinker.c                      |   14 +
 fs/f2fs/super.c                         |  147 ++-
 fs/f2fs/sysfs.c                         |   15 +-
 fs/f2fs/xattr.c                         |  123 ++-
 fs/f2fs/xattr.h                         |   12 +-
 include/linux/f2fs_fs.h                 |    3 -
 include/trace/events/f2fs.h             |   71 ++
 31 files changed, 3511 insertions(+), 2323 deletions(-)
 create mode 100644 fs/f2fs/cache.c
 create mode 100644 fs/f2fs/cache.h
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH v7 1/12 f2fs: cache: implement metadata cache

From: Chao Yu <[email protected]>

This patch introduces the core metadata block caching infrastructure to manage f2fs metadata independently of the page cache.

It implements:
- core cache APIs: get, create, put, drop, backed by a radix tree and a single global LRU list.
- support multiple status of cached block: LOCKED, UPTODATE, DIRTY, WRITEBACK, INLINE.
- internal bio based read/write helpers with adjacent block vector merging.

---
 fs/f2fs/Makefile |   2 +-
 fs/f2fs/cache.c  | 539 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/f2fs/cache.h  | 197 +++++++++++++++++
 fs/f2fs/data.c   | 281 +++++++++++++++++++++---
 fs/f2fs/f2fs.h   |  24 +++
 fs/f2fs/iostat.h |  11 +
 6 files changed, 1027 insertions(+), 27 deletions(-)
 create mode 100644 fs/f2fs/cache.c
 create mode 100644 fs/f2fs/cache.h
fs/f2fs/Makefile +1 -1
index 8a7322d229e4..fbf49c30b066 100644
@@ -3,7 +3,7 @@ obj-$(CONFIG_F2FS_FS) += f2fs.o
 
 f2fs-y		:= dir.o file.o inode.o namei.o hash.o super.o inline.o
 f2fs-y		+= checkpoint.o gc.o data.o node.o segment.o recovery.o
-f2fs-y		+= shrinker.o extent_cache.o sysfs.o
+f2fs-y		+= shrinker.o extent_cache.o sysfs.o cache.o
 f2fs-$(CONFIG_F2FS_STAT_FS) += debug.o
 f2fs-$(CONFIG_F2FS_FS_XATTR) += xattr.o
 f2fs-$(CONFIG_F2FS_FS_POSIX_ACL) += acl.o
fs/f2fs/cache.c new file +539 -0

539 lines not on this page · raw message

fs/f2fs/cache.h new file +197 -0

197 lines not on this page · raw message

fs/f2fs/data.c +255 -26

346 lines not on this page · raw message

fs/f2fs/f2fs.h +24 -0
index ebc621f302e1..c0c7d51c830f 100644
@@ -222,6 +222,8 @@ struct f2fs_rwsem {
 #endif
 };
 
+#include "cache.h"
+
 struct f2fs_mount_info {
 	unsigned long long opt;
 	block_t root_reserved_blocks;	/* root reserved blocks */
@@ -1362,8 +1364,10 @@ struct f2fs_io_info {
 	unsigned int in_list:1;		/* indicate fio is in io_list */
 	unsigned int is_por:1;		/* indicate IO is from recovery or not */
 	unsigned int meta_gc:1;		/* require meta inode GC */
+	unsigned int is_cache:1;	/* indicate IO is from internal cache */
 	enum iostat_type io_type;	/* io type */
 	struct writeback_control *io_wbc; /* writeback control */
+	struct f2fs_cached_block *cache_entry;
 	struct bio **bio;		/* bio for ipu */
 	sector_t *last_block;		/* last block number in bio */
 };
@@ -1773,6 +1777,12 @@ struct f2fs_gc_kthread {
 	unsigned int boost_gc_greedy;
 };
 
+struct f2fs_bio {
+	struct work_struct work;
+	struct f2fs_cached_block *entry;
+	struct bio bio;
+};
+
 struct f2fs_sb_info {
 	struct super_block *sb;			/* pointer to VFS super block */
 	struct proc_dir_entry *s_proc;		/* proc entry */
@@ -2381,6 +2391,16 @@ static inline bool is_node_folio(struct f2fs_sb_info *sbi, struct folio *folio)
 	return folio->mapping == NODE_MAPPING(sbi);
 }
 
+static inline struct f2fs_bio *F2FS_BIO(struct bio *bio)
+{
+	return container_of(bio, struct f2fs_bio, bio);
+}
+
+static inline bool f2fs_is_cache_bio(struct bio *bio)
+{
+	return F2FS_BIO(bio)->entry != NULL;
+}
+
 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
 {
 	return test_bit(type, &sbi->s_flag);
@@ -4312,6 +4332,8 @@ void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
 				nid_t ino, enum page_type type);
 void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi,
 				struct folio *folio, enum page_type type);
+bool f2fs_submit_merged_write_cache(struct f2fs_cached_block *entry,
+				enum page_type type);
 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
 					struct bio **bio, struct folio *folio);
 void f2fs_submit_all_merged_ipu_writes(struct f2fs_sb_info *sbi);
@@ -4319,6 +4341,8 @@ void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
 int f2fs_merge_page_bio(struct f2fs_io_info *fio);
 void f2fs_submit_page_write(struct f2fs_io_info *fio);
+int f2fs_submit_cache_read(struct f2fs_io_info *fio);
+void f2fs_submit_cache_write(struct f2fs_io_info *fio);
 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
 		block_t blk_addr, sector_t *sector);
 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
fs/f2fs/iostat.h +11 -0
index 2025225b5bed..61c6bc8e3119 100644
@@ -60,6 +60,13 @@ static inline struct bio_post_read_ctx *get_post_read_ctx(struct bio *bio)
 	return iostat_ctx->post_read_ctx;
 }
 
+static inline void iostat_set_post_read_ctx(struct bio *bio, void *ctx)
+{
+	struct bio_iostat_ctx *iostat_ctx = bio->bi_private;
+
+	iostat_ctx->post_read_ctx = ctx;
+}
+
 extern void iostat_update_and_unbind_ctx(struct bio *bio);
 extern void iostat_alloc_and_bind_ctx(struct f2fs_sb_info *sbi,
 		struct bio *bio, struct bio_post_read_ctx *ctx);
@@ -81,6 +88,10 @@ static inline struct bio_post_read_ctx *get_post_read_ctx(struct bio *bio)
 {
 	return bio->bi_private;
 }
+static inline void iostat_set_post_read_ctx(struct bio *bio, void *ctx)
+{
+	bio->bi_private = ctx;
+}
 static inline int f2fs_init_iostat_processing(void) { return 0; }
 static inline void f2fs_destroy_iostat_processing(void) {}
 static inline int f2fs_init_iostat(struct f2fs_sb_info *sbi) { return 0; }
Signature · 1 line
2.49.0
#2 lore raw

Re: [f2fs-dev] [PATCH v7 01/12] f2fs: cache: implement metadata cache

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 629 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces the core metadata block caching infrastructure to manage f2fs metadata independently of the page cache.

It implements:
- core cache APIs: get, create, put, drop, backed by a radix tree and a single global LRU list.
- support multiple status of cached block: LOCKED, UPTODATE, DIRTY, WRITEBACK, INLINE.
- internal bio based read/write helpers with adjacent block vector merging.

---
 fs/f2fs/Makefile |   2 +-
 fs/f2fs/cache.c  | 539 +++++++++++++++++++++++++++++++++++++++++++++++
 fs/f2fs/cache.h  | 197 +++++++++++++++++
 fs/f2fs/data.c   | 281 +++++++++++++++++++++---
 fs/f2fs/f2fs.h   |  24 +++
 fs/f2fs/iostat.h |  11 +
 6 files changed, 1027 insertions(+), 27 deletions(-)
 create mode 100644 fs/f2fs/cache.c
 create mode 100644 fs/f2fs/cache.h
fs/f2fs/Makefile +1 -1
index 8a7322d229e4..fbf49c30b066 100644
@@ -3,7 +3,7 @@ obj-$(CONFIG_F2FS_FS) += f2fs.o
 
 f2fs-y         := dir.o file.o inode.o namei.o hash.o super.o inline.o
 f2fs-y         += checkpoint.o gc.o data.o node.o segment.o recovery.o
-f2fs-y         += shrinker.o extent_cache.o sysfs.o
+f2fs-y         += shrinker.o extent_cache.o sysfs.o cache.o
 f2fs-$(CONFIG_F2FS_STAT_FS) += debug.o
 f2fs-$(CONFIG_F2FS_FS_XATTR) += xattr.o
 f2fs-$(CONFIG_F2FS_FS_POSIX_ACL) += acl.o
fs/f2fs/cache.c new file +539 -0
new file mode 100644
index 000000000000..fb0ab77d3b49
@@ -0,0 +1,539 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 Google LLC
+ * Author: Chao Yu <[email protected]>
+ */
+#include <linux/fs.h>
+#include <linux/f2fs_fs.h>
+#include <linux/radix-tree.h>
+#include <linux/slab.h>
+#include <linux/list.h>
+#include <linux/pagemap.h>
+#include <linux/kthread.h>
+#include <linux/freezer.h>
+#include <linux/delay.h>
+#include "f2fs.h"
+#include "cache.h"
+#include "node.h"
+#include "segment.h"
+
+void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
+                                       enum page_type type)
+{
+       /* in case the entry was truncated or on-going shrink */
+       if (!entry->cache)
+               return;
+
+       if (!f2fs_cache_test_writeback(entry))
+               return;
+
+       /* submit cached bio */
+       f2fs_submit_merged_write_cache(entry, type);
+
+       wait_on_bit_io(&entry->state, F2FS_BLOCK_WRITEBACK,
+                                       TASK_UNINTERRUPTIBLE);
+}
+
+void f2fs_cache_wait_writeback(struct f2fs_cached_block *entry)
+{
+       /* in case the entry was truncated or on-going shrink */
+       if (!entry->cache)
+               return;
+
+       f2fs_cache_wait_writeback_cond(entry,
+                       IS_META_CACHE(entry->cache) ? META : NODE);
+}
+
+void f2fs_cache_update_tag(struct f2fs_cached_block *entry,
+               unsigned int clear_from, unsigned int set_to)
+{
+       struct f2fs_cached_block_list *cache = entry->cache;
+       unsigned long flags;
+
+       spin_lock_irqsave(&cache->tree_lock, flags);
+       if (clear_from != F2FS_CACHE_TAG_NONE)
+               radix_tree_tag_clear(&cache->root, entry->index, clear_from);
+       if (set_to != F2FS_CACHE_TAG_NONE)
+               radix_tree_tag_set(&cache->root, entry->index, set_to);
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+}
+
+bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry)
+{
+       struct f2fs_cached_block_list *cache = entry->cache;
+
+       f2fs_cache_set_uptodate(entry);
+
+#ifdef CONFIG_F2FS_CHECK_FS
+       if (cache->type == F2FS_NODE_CACHE && IS_INODE(cache->sbi, cache_folio(entry)))
+               f2fs_inode_chksum_set(cache->sbi, cache_folio(entry));
+#endif
+
+       if (!f2fs_cache_test_and_set_dirty(entry)) {
+               enum count_type type = IS_META_CACHE(cache) ?
+                               F2FS_DIRTY_META : F2FS_DIRTY_NODES;
+
+               f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_NONE,
+                                               F2FS_CACHE_TAG_DIRTY);
+               inc_page_count(cache->sbi, type);
+               return true;
+       }
+
+       return false;
+}
+
+void f2fs_drop_cache_dirty(struct f2fs_cached_block *entry)
+{
+
+       struct f2fs_cached_block_list *cache = entry->cache;
+       enum count_type type = IS_META_CACHE(cache) ?
+                               F2FS_DIRTY_META : F2FS_DIRTY_NODES;
+
+       f2fs_cache_clear_uptodate(entry);
+
+       if (!f2fs_cache_test_and_clear_dirty(entry))
+               return;
+
+       f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_DIRTY,
+                                       F2FS_CACHE_TAG_NONE);
+       dec_page_count(cache->sbi, type);
+}
+
+void f2fs_start_cache_writeback(struct f2fs_cached_block *entry)
+{
+       f2fs_cache_set_writeback(entry);
+       f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_DIRTY,
+                               F2FS_CACHE_TAG_WRITEBACK);
+}
+
+void f2fs_end_cache_writeback(struct f2fs_cached_block *entry)
+{
+       /*
+        * should call f2fs_cache_update_tag() before clearing writeback bit,
+        * in case f2fs_truncate_cache() set entry->cache to NULL.
+        */
+       f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_WRITEBACK,
+                                               F2FS_CACHE_TAG_NONE);
+       clear_and_wake_up_bit(F2FS_BLOCK_WRITEBACK, &entry->state);
+}
+
+static int f2fs_cache_refcount(struct f2fs_cached_block *entry)
+{
+       return atomic_read(&entry->refcount);
+}
+
+static void f2fs_do_free_cache(struct f2fs_cached_block *entry)
+{
+       kfree(entry->data);
+       kfree(entry);
+}
+
+static void f2fs_free_cache(struct f2fs_cached_block *entry)
+{
+       WARN_ON_ONCE(!list_empty(&entry->list));
+       WARN_ON_ONCE(f2fs_cache_refcount(entry));
+       f2fs_do_free_cache(entry);
+}
+
+void f2fs_cache_get(struct f2fs_cached_block *entry)
+{
+       atomic_inc(&entry->refcount);
+}
+
+static bool f2fs_cache_put(struct f2fs_cached_block *entry)
+{
+       WARN_ON_ONCE(!f2fs_cache_refcount(entry));
+       if (atomic_dec_and_test(&entry->refcount)) {
+               f2fs_free_cache(entry);
+               return true;
+       }
+       return false;
+}
+
+static struct f2fs_cached_block *f2fs_create_cache(
+               struct f2fs_cached_block_list *cache,
+               unsigned long index, bool nofail)
+{
+       struct f2fs_cached_block *entry;
+       unsigned int flags = GFP_NOFS;
+
+       if (index == ULONG_MAX)
+               return ERR_PTR(-ERANGE);
+
+       if (nofail)
+               flags |= __GFP_NOFAIL;
+
+       entry = kzalloc_obj(*entry, flags);
+       if (!entry)
+               return ERR_PTR(-ENOMEM);
+
+       entry->data = kmalloc(cache->sbi->blocksize, flags);
+       if (!entry->data) {
+               kfree(entry);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       entry->index = index;
+
+       atomic_set(&entry->refcount, 0);
+       entry->next_entry = NULL;
+       INIT_LIST_HEAD(&entry->list);
+
+       entry->cache = cache;
+
+       return entry;
+}
+
+static struct f2fs_cached_block *f2fs_insert_cache(
+                       struct f2fs_cached_block_list *cache,
+                       unsigned long index,
+                       struct f2fs_cached_block *new)
+{
+       struct f2fs_cached_block *e;
+       int ret;
+       unsigned long flags;
+
+       ret = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
+       f2fs_bug_on(cache->sbi, ret);
+
+       spin_lock(&cache->list_lock);
+       spin_lock_irqsave(&cache->tree_lock, flags);
+       e = radix_tree_lookup(&cache->root, index);
+       if (!e) {
+               e = new;
+               f2fs_bug_on(cache->sbi, f2fs_cache_refcount(e));
+
+               ret = radix_tree_insert(&cache->root, index, e);
+               f2fs_bug_on(cache->sbi, ret);
+
+               /* radix tree referenced cache entry */
+               f2fs_cache_get(e);
+               f2fs_bug_on(cache->sbi, !list_empty(&e->list));
+               list_add_tail(&e->list, &cache->lru_list);
+               cache->num_entries++;
+       }
+       f2fs_cache_get(e);
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+       spin_unlock(&cache->list_lock);
+       radix_tree_preload_end();
+
+       if (new != e) {
+               f2fs_bug_on(cache->sbi, f2fs_cache_refcount(new));
+               f2fs_do_free_cache(new);
+       }
+
+       return e;
+}
+
+struct f2fs_cached_block *f2fs_find_cache(
+                       struct f2fs_cached_block_list *cache,
+                       unsigned long index,
+                       enum f2fs_cache_request_flag rflag)
+{
+       struct f2fs_cached_block *entry;
+       unsigned long flags;
+       bool access = rflag & F2FS_CACHE_ACCESS;
+
+       spin_lock_irqsave(&cache->tree_lock, flags);
+       entry = radix_tree_lookup(&cache->root, index);
+       if (entry) {
+               f2fs_bug_on(cache->sbi, !f2fs_cache_refcount(entry));
+               f2fs_cache_get(entry);
+               if (access && !f2fs_cache_test_referenced(entry))
+                       f2fs_cache_set_referenced(entry);
+       } else {
+               entry = ERR_PTR(-ENOENT);
+       }
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+
+       return entry;
+}
+
+struct f2fs_cached_block *f2fs_grab_cache(
+                       struct f2fs_cached_block_list *cache,
+                       unsigned long index, int flags)
+
+{
+       struct f2fs_cached_block *entry, *new;
+       bool create = flags & F2FS_CACHE_CREATE;
+       bool nofail = flags & F2FS_CACHE_NOFAIL;
+       bool lock = flags & F2FS_CACHE_LOCK;
+
+repeat:
+       entry = f2fs_find_cache(cache, index, F2FS_CACHE_ACCESS);
+       if (!IS_ERR(entry))
+               goto found;
+
+       if (!create)
+               return ERR_PTR(-ENOENT);
+
+       new = f2fs_create_cache(cache, index, nofail);
+       if (IS_ERR(new))
+               return new;
+
+       entry = f2fs_insert_cache(cache, index, new);
+found:
+       if (lock) {
+               f2fs_lock_cache(entry);
+               /* has been truncated */
+               if (entry->cache != cache) {
+                       f2fs_put_cache(entry, true);
+                       goto repeat;
+               }
+       }
+       return entry;
+}
+
+bool f2fs_trylock_cache(struct f2fs_cached_block *entry)
+{
+       return !test_and_set_bit(F2FS_BLOCK_LOCKED, &entry->state);
+}
+
+void f2fs_lock_cache(struct f2fs_cached_block *entry)
+{
+       wait_on_bit_lock(&entry->state, F2FS_BLOCK_LOCKED,
+                                       TASK_UNINTERRUPTIBLE);
+}
+
+void f2fs_unlock_cache(struct f2fs_cached_block *entry)
+{
+       clear_and_wake_up_bit(F2FS_BLOCK_LOCKED, &entry->state);
+}
+
+bool f2fs_put_cache(struct f2fs_cached_block *entry, bool unlock)
+{
+       if (IS_ERR_OR_NULL(entry))
+               return false;
+       if (unlock)
+               f2fs_unlock_cache(entry);
+       return f2fs_cache_put(entry);
+}
+
+unsigned int f2fs_cache_gang_lookup(struct f2fs_cached_block_list *cache,
+                               struct f2fs_cached_block **entries,
+                               pgoff_t *index, unsigned long end)
+{
+       unsigned long flags;
+       unsigned int max_nr = min((unsigned long)F2FS_ONSTACK_CACHES, end - *index);
+       int nr, i;
+
+       if (*index >= end || *index == ULONG_MAX)
+               return 0;
+
+       spin_lock_irqsave(&cache->tree_lock, flags);
+       nr = radix_tree_gang_lookup(&cache->root, (void **)entries,
+                               *index, max_nr);
+       if (!nr)
+               goto out_unlock;
+
+       for (i = 0; i < nr; i++) {
+               struct f2fs_cached_block *entry = entries[i];
+
+               if (entry->index >= end) {
+                       nr = i;
+                       break;
+               }
+               f2fs_cache_get(entry);
+       }
+       if (nr)
+               *index = entries[nr - 1]->index + 1;
+out_unlock:
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+       return nr;
+}
+
+unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache,
+                               struct f2fs_cached_block **entries,
+                               pgoff_t *index, unsigned int max_nr,
+                               int tag)
+{
+       unsigned long flags;
+       int nr, i;
+
+       if (*index == ULONG_MAX)
+               return 0;
+
+       spin_lock_irqsave(&cache->tree_lock, flags);
+       nr = radix_tree_gang_lookup_tag(&cache->root, (void **)entries,
+                               *index, max_nr, tag);
+       if (!nr)
+               goto out;
+
+       for (i = 0; i < nr; i++)
+               f2fs_cache_get(entries[i]);
+       *index = entries[nr - 1]->index + 1;
+out:
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+       return nr;
+}
+
+void f2fs_cache_gang_release(struct f2fs_cached_block **entries,
+                               unsigned int nr_entries)
+{
+       int i;
+
+       for (i = 0; i < nr_entries; i++)
+               f2fs_put_cache(entries[i], false);
+}
+
+void f2fs_cache_wait_on_all_writeback(struct f2fs_cached_block_list *cache)
+{
+       unsigned long index = 0;
+       struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES];
+       int nr, i;
+
+next:
+       nr = f2fs_cache_gang_lookup_tag(cache, entries, &index,
+                       F2FS_ONSTACK_CACHES, F2FS_CACHE_TAG_WRITEBACK);
+       if (!nr)
+               return;
+
+       for (i = 0; i < nr; i++)
+               f2fs_cache_wait_writeback(entries[i]);
+       f2fs_cache_gang_release(entries, nr);
+
+       cond_resched();
+       goto next;
+}
+
+static void f2fs_do_truncate_cache(struct f2fs_cached_block *entry,
+                                               bool drop_dirty)
+{
+       struct f2fs_cached_block_list *cache = entry->cache;
+       unsigned long flags;
+
+       if (drop_dirty)
+               goto drop_it;
+
+       if (f2fs_cache_test_dirty(entry) ||
+           f2fs_cache_test_writeback(entry))
+               return;
+
+drop_it:
+       f2fs_cache_wait_writeback(entry);
+       f2fs_drop_cache_dirty(entry);
+
+       spin_lock(&cache->list_lock);
+       spin_lock_irqsave(&cache->tree_lock, flags);
+
+       f2fs_bug_on(cache->sbi, !entry->cache);
+       if (!radix_tree_delete(&cache->root, entry->index))
+               f2fs_bug_on(cache->sbi, !entry->cache);
+
+       entry->cache = NULL;
+       cache->num_entries--;
+
+       atomic_dec(&entry->refcount);
+       f2fs_bug_on(cache->sbi, !f2fs_cache_refcount(entry));
+
+       f2fs_bug_on(cache->sbi, list_empty(&entry->list));
+       list_del_init(&entry->list);
+
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+       spin_unlock(&cache->list_lock);
+}
+
+void f2fs_truncate_locked_cache(struct f2fs_cached_block *entry,
+                               bool drop_dirty)
+{
+       if (!entry->cache)
+               return;
+       f2fs_do_truncate_cache(entry, drop_dirty);
+}
+
+void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty)
+{
+       f2fs_lock_cache(entry);
+       f2fs_truncate_locked_cache(entry, drop_dirty);
+       f2fs_unlock_cache(entry);
+}
+
+static void f2fs_drop_cache(struct f2fs_cached_block_list *cache,
+                       block_t blkaddr, bool drop_dirty)
+{
+       struct f2fs_cached_block *entry;
+
+       entry = f2fs_find_cache(cache, blkaddr, 0);
+       if (IS_ERR(entry))
+               return;
+
+       f2fs_truncate_cache(entry, drop_dirty);
+       f2fs_put_cache(entry, false);
+}
+
+void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
+               unsigned long start, unsigned long len, bool drop_dirty)
+{
+       unsigned long index = start;
+       unsigned long end = (ULONG_MAX - start < len) ?
+                               ULONG_MAX : (start + len);
+       struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES];
+       int nr, i;
+
+       if (len == 1)
+               return f2fs_drop_cache(cache, index, drop_dirty);
+
+next:
+       nr = f2fs_cache_gang_lookup(cache, entries, &index, end);
+       if (!nr)
+               return;
+
+       for (i = 0; i < nr; i++)
+               f2fs_truncate_cache(entries[i], drop_dirty);
+       f2fs_cache_gang_release(entries, nr);
+
+       if (index < end) {
+               cond_resched();
+               goto next;
+       }
+}
+
+int f2fs_init_cache(struct f2fs_sb_info *sbi,
+               struct f2fs_cached_block_list *cache,
+               enum f2fs_cache_type type)
+{
+       cache->sbi = sbi;
+       cache->type = type;
+       INIT_RADIX_TREE(&cache->root, GFP_ATOMIC);
+       spin_lock_init(&cache->tree_lock);
+       spin_lock_init(&cache->list_lock);
+       INIT_LIST_HEAD(&cache->lru_list);
+       cache->num_entries = 0;
+
+       return 0;
+}
+
+void f2fs_destroy_cache(struct f2fs_cached_block_list *cache)
+{
+       struct list_head *head = &cache->lru_list;
+       struct f2fs_cached_block *entry;
+       unsigned long flags;
+
+       f2fs_cache_wait_on_all_writeback(cache);
+next:
+       spin_lock(&cache->list_lock);
+       if (list_empty(head)) {
+               spin_unlock(&cache->list_lock);
+               return;
+       }
+       entry = list_first_entry(head, struct f2fs_cached_block, list);
+
+       spin_lock_irqsave(&cache->tree_lock, flags);
+       radix_tree_delete(&cache->root, entry->index);
+       cache->num_entries--;
+       list_del_init(&entry->list);
+       spin_unlock_irqrestore(&cache->tree_lock, flags);
+
+       spin_unlock(&cache->list_lock);
+
+       /* wait on read cache IO */
+       f2fs_lock_cache(entry);
+       /* wait on write cache IO */
+       f2fs_cache_wait_writeback(entry);
+       f2fs_bug_on(cache->sbi, f2fs_cache_test_dirty(entry));
+       f2fs_bug_on(cache->sbi, f2fs_cache_test_writeback(entry));
+       f2fs_bug_on(cache->sbi, !list_empty(&entry->list));
+       f2fs_bug_on(cache->sbi, f2fs_cache_refcount(entry) != 1);
+       f2fs_put_cache(entry, true);
+       goto next;
+}
fs/f2fs/cache.h new file +40 -0
new file mode 100644
index 000000000000..72dbf26ab26e
@@ -0,0 +1,197 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2026 Google LLC
+ * Author: Chao Yu <[email protected]>
+ */
+#ifndef _LINUX_F2FS_CACHE_H
+#define _LINUX_F2FS_CACHE_H
+
+#include <linux/pagemap.h>
+#include <linux/mm.h>
+#include <linux/list.h>
+#include <linux/radix-tree.h>
+#include <linux/spinlock.h>
+#include <linux/wait.h>
+#include <linux/types.h>
+
+struct f2fs_rwsem;
+struct f2fs_io_info;
+enum page_type;
+
+/* Represents a single cached block (meta, node) */
+struct f2fs_cached_block {
+       struct list_head list;          /* LRU list head */
+       struct f2fs_cached_block_list *cache;   /* parent cache list */
+       struct f2fs_cached_block *next_entry;   /* chain for merged BIO */
+       unsigned long index;            /* key in radix tree, (meta: pba, node: nid) */
+       unsigned long state;            /* cache entry state (e.g., Dirty, UpToDate) */
+       void *data;                     /* blocksize-aligned memory (4KB or 16KB) */
+       atomic_t refcount;              /* reference count */
+};
+
+struct f2fs_sb_info;
+
+enum f2fs_cache_type {
+       F2FS_META_CACHE,
+       F2FS_NODE_CACHE,
+};
+
+/* Main cache control structure (per sb_info) */
+struct f2fs_cached_block_list {

fs/f2fs/cache.h line 50

Quoted 10 lines
fs/f2fs/cache.h new file +10 -0
+       struct f2fs_sb_info *sbi;       /* Pointer to f2fs_sb_info */
+       struct radix_tree_root root;    /* Radix tree for cache lookup */
+       spinlock_t tree_lock;           /* Lock for radix tree */
+       struct list_head lru_list;      /* Single global LRU list */
+       spinlock_t list_lock;           /* Lock for LRU list */
+       enum f2fs_cache_type type;      /* Cache type (Node or Meta) */
+       unsigned long num_entries;      /* Current number of entries */
+};
+
+#define IS_META_CACHE(cache) (cache->type == F2FS_META_CACHE)

#define IS_META_CACHE(cache) ((cache)->type == F2FS_META_CACHE)

Quoted 122 lines
fs/f2fs/cache.h new file +122 -0
+
+/* Flags for f2fs_cached_block state */
+enum f2fs_cached_state {
+       F2FS_BLOCK_LOCKED,              /* cache entry is locked */
+       F2FS_BLOCK_UPTODATE,            /* cache data is valid */
+       F2FS_BLOCK_DIRTY,               /* cache data is dirty, need to writeback the data */
+       F2FS_BLOCK_WRITEBACK,           /* cache data is writeback state */
+       F2FS_BLOCK_REFERENCED,          /* cache was accessed recently, shrinker will skip it for once */
+       F2FS_BLOCK_INLINE_DATA,         /* indicate inline data */
+};
+
+enum {
+       __F2FS_CACHE_CREATE,            /* create the cache if there is no cache entry */
+       __F2FS_CACHE_LOCK,              /* get and lock the cache entry */
+       __F2FS_CACHE_NOFAIL,            /* do not allow failure */
+       __F2FS_CACHE_ACCESS,            /* give a chance to add referenced tag */
+};
+
+enum f2fs_cache_request_flag {
+       F2FS_CACHE_CREATE       = 1 << __F2FS_CACHE_CREATE,
+       F2FS_CACHE_LOCK         = 1 << __F2FS_CACHE_LOCK,
+       F2FS_CACHE_NOFAIL       = 1 << __F2FS_CACHE_NOFAIL,
+       F2FS_CACHE_ACCESS       = 1 << __F2FS_CACHE_ACCESS,
+};
+
+#define F2FS_CACHE_LOCK_CREATE (F2FS_CACHE_LOCK | F2FS_CACHE_CREATE)
+
+#define F2FS_ONSTACK_CACHES            (32)
+
+#define F2FS_CACHE_FLAG_TEST_FUNC(name, flagname)                      \
+static inline bool f2fs_cache_test_##name(                             \
+                       const struct f2fs_cached_block *entry)          \
+{                                                                      \
+       return test_bit(F2FS_BLOCK_##flagname, &entry->state);          \
+}                                                                      \
+
+#define F2FS_CACHE_FLAG_SET_FUNC(name, flagname)                       \
+static inline void f2fs_cache_set_##name(                              \
+                       struct f2fs_cached_block *entry)                \
+{                                                                      \
+       set_bit(F2FS_BLOCK_##flagname, &entry->state);                  \
+}                                                                      \
+
+#define F2FS_CACHE_FLAG_CLEAR_FUNC(name, flagname)                     \
+static inline void f2fs_cache_clear_##name(                            \
+                       struct f2fs_cached_block *entry)                \
+{                                                                      \
+       clear_bit(F2FS_BLOCK_##flagname, &entry->state);                \
+}                                                                      \
+
+#define F2FS_CACHE_FLAG_TEST_AND_SET_FUNC(name, flagname)              \
+static inline bool f2fs_cache_test_and_set_##name(                     \
+                       struct f2fs_cached_block *entry)                \
+{                                                                      \
+       return test_and_set_bit(F2FS_BLOCK_##flagname, &entry->state);  \
+}                                                                      \
+
+#define F2FS_CACHE_FLAG_TEST_AND_CLEAR_FUNC(name, flagname)            \
+static inline bool f2fs_cache_test_and_clear_##name(                   \
+                       struct f2fs_cached_block *entry)                \
+{                                                                      \
+       return test_and_clear_bit(F2FS_BLOCK_##flagname, &entry->state);\
+}                                                                      \
+
+F2FS_CACHE_FLAG_TEST_FUNC(locked, LOCKED);
+F2FS_CACHE_FLAG_SET_FUNC(locked, LOCKED);
+F2FS_CACHE_FLAG_CLEAR_FUNC(locked, LOCKED);
+
+F2FS_CACHE_FLAG_TEST_FUNC(uptodate, UPTODATE);
+F2FS_CACHE_FLAG_SET_FUNC(uptodate, UPTODATE);
+F2FS_CACHE_FLAG_CLEAR_FUNC(uptodate, UPTODATE);
+
+F2FS_CACHE_FLAG_TEST_FUNC(dirty, DIRTY);
+F2FS_CACHE_FLAG_SET_FUNC(dirty, DIRTY);
+F2FS_CACHE_FLAG_CLEAR_FUNC(dirty, DIRTY);
+F2FS_CACHE_FLAG_TEST_AND_SET_FUNC(dirty, DIRTY);
+F2FS_CACHE_FLAG_TEST_AND_CLEAR_FUNC(dirty, DIRTY);
+
+F2FS_CACHE_FLAG_TEST_FUNC(writeback, WRITEBACK);
+F2FS_CACHE_FLAG_SET_FUNC(writeback, WRITEBACK);
+F2FS_CACHE_FLAG_CLEAR_FUNC(writeback, WRITEBACK);
+
+F2FS_CACHE_FLAG_TEST_FUNC(inline, INLINE_DATA);
+F2FS_CACHE_FLAG_SET_FUNC(inline, INLINE_DATA);
+F2FS_CACHE_FLAG_CLEAR_FUNC(inline, INLINE_DATA);
+
+F2FS_CACHE_FLAG_TEST_FUNC(referenced, REFERENCED);
+F2FS_CACHE_FLAG_SET_FUNC(referenced, REFERENCED);
+F2FS_CACHE_FLAG_TEST_AND_CLEAR_FUNC(referenced, REFERENCED);
+
+static inline void *cache_address(const struct f2fs_cached_block *entry)
+{
+       return entry->data;
+}
+
+#define CACHED_NODE(entry)     ((struct f2fs_node *)(cache_address(entry)))
+
+static inline struct folio *cache_folio(const struct f2fs_cached_block *entry)
+{
+       return virt_to_folio(entry->data);
+}
+
+int f2fs_init_cache(struct f2fs_sb_info *sbi,
+                               struct f2fs_cached_block_list *cache,
+                               enum f2fs_cache_type type);
+void f2fs_destroy_cache(struct f2fs_cached_block_list *cache);
+void f2fs_cache_get(struct f2fs_cached_block *entry);
+struct f2fs_cached_block *f2fs_find_cache(
+                       struct f2fs_cached_block_list *cache,
+                       unsigned long index,
+                       enum f2fs_cache_request_flag rflag);
+#define F2FS_CACHE_TAG_NONE            0
+#define F2FS_CACHE_TAG_DIRTY           1
+#define F2FS_CACHE_TAG_WRITEBACK       2
+
+bool f2fs_trylock_cache(struct f2fs_cached_block *entry);
+void f2fs_lock_cache(struct f2fs_cached_block *entry);
+void f2fs_unlock_cache(struct f2fs_cached_block *entry);
+bool f2fs_put_cache(struct f2fs_cached_block *entry, bool unlock);
+bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry);
+void f2fs_drop_cache_dirty(struct f2fs_cached_block *entry);
+void f2fs_start_cache_writeback(struct f2fs_cached_block *entry);

fs/f2fs/cache.h line 182

Quoted 10 lines
fs/f2fs/cache.h new file +10 -0
+void f2fs_end_cache_writeback(struct f2fs_cached_block *entry);
+unsigned int f2fs_cache_gang_lookup(struct f2fs_cached_block_list *cache,
+                               struct f2fs_cached_block **entries,
+                               pgoff_t *index, unsigned long end);
+unsigned int f2fs_cache_gang_lookup_tag(struct f2fs_cached_block_list *cache,
+               struct f2fs_cached_block **results, pgoff_t *first_index,
+               unsigned int max_items, int tag);
+void f2fs_cache_gang_release(struct f2fs_cached_block **entries,
+                               unsigned int nr_entries);
+int f2fs_writeback_cache(struct f2fs_cached_block_list *cache, bool sync);

not defined and used?

Quoted 485 lines
fs/f2fs/cache.h new file +15 -0
+void f2fs_cache_wait_on_all_writeback(struct f2fs_cached_block_list *cache);
+void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
+                                       enum page_type type);
+void f2fs_cache_wait_writeback(struct f2fs_cached_block *entry);
+void f2fs_cache_update_tag(struct f2fs_cached_block *entry,
+               unsigned int clear_from, unsigned int set_to);
+struct f2fs_cached_block *f2fs_grab_cache(struct f2fs_cached_block_list *cache,
+                               unsigned long index, int flags);
+void f2fs_truncate_locked_cache(struct f2fs_cached_block *entry,
+                               bool drop_dirty);
+void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty);
+void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
+               unsigned long start, unsigned long len, bool drop_dirty);
+
+#endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/data.c +255 -26
index a3b02c067e89..d868b939caee 100644
@@ -41,11 +41,6 @@ struct f2fs_folio_state {
        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)
@@ -411,6 +406,65 @@ static void f2fs_write_end_io(struct bio *bio)
        }
 }
 
+static void f2fs_cache_read_end_io(struct bio *bio)
+{
+       struct f2fs_cached_block *entry = F2FS_BIO(bio)->entry;
+       struct f2fs_sb_info *sbi = entry->cache->sbi;
+       enum count_type io_type = IS_META_CACHE(entry->cache) ?
+                                       F2FS_RD_META : F2FS_RD_NODE;
+       struct f2fs_cached_block *next;
+
+       iostat_update_and_unbind_ctx(bio);
+
+       if (time_to_inject(sbi, FAULT_READ_IO))
+               bio->bi_status = BLK_STS_IOERR;
+
+       while (entry) {
+               next = entry->next_entry;
+               entry->next_entry = NULL;
+
+               if (bio->bi_status == BLK_STS_OK)
+                       f2fs_cache_set_uptodate(entry);
+
+               dec_page_count(sbi, io_type);
+
+               f2fs_unlock_cache(entry);
+               entry = next;
+       }
+       bio_put(bio);
+}
+
+static void f2fs_cache_write_end_io(struct bio *bio)
+{
+       struct f2fs_cached_block *entry = F2FS_BIO(bio)->entry;
+       struct f2fs_sb_info *sbi = entry->cache->sbi;
+       struct f2fs_cached_block *next;
+
+       iostat_update_and_unbind_ctx(bio);
+
+       if (time_to_inject(sbi, FAULT_WRITE_IO))
+               bio->bi_status = BLK_STS_IOERR;
+
+       if (bio->bi_status != BLK_STS_OK)
+               f2fs_stop_checkpoint(sbi, true,
+                       STOP_CP_REASON_WRITE_FAIL);
+
+       while (entry) {
+               next = entry->next_entry;
+               entry->next_entry = NULL;
+
+               dec_page_count(sbi, F2FS_WB_CP_DATA);
+
+               if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
+                               wq_has_sleeper(&sbi->cp_wait))
+                       wake_up(&sbi->cp_wait);
+
+               f2fs_end_cache_writeback(entry);
+               entry = next;
+       }
+       bio_put(bio);
+}
+
 #ifdef CONFIG_BLK_DEV_ZONED
 static void f2fs_zone_write_end_io(struct bio *bio)
 {
@@ -418,7 +472,10 @@ static void f2fs_zone_write_end_io(struct bio *bio)
 
        bio->bi_private = io->bi_private;
        complete(&io->zone_wait);
-       f2fs_write_end_io(bio);
+       if (f2fs_is_cache_bio(bio))
+               f2fs_cache_write_end_io(bio);
+       else
+               f2fs_write_end_io(bio);
 }
 #endif
 
@@ -505,12 +562,21 @@ static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
                                fio->op | fio->op_flags | f2fs_io_flags(fio),
                                GFP_NOIO, &f2fs_bioset);
        bio->bi_iter.bi_sector = sector;
+       F2FS_BIO(bio)->entry = NULL;
+       bio->bi_private = NULL;
        if (is_read_io(fio->op)) {
-               bio->bi_end_io = f2fs_read_end_io;
-               bio->bi_private = NULL;
+               if (fio->is_cache)
+                       bio->bi_end_io = f2fs_cache_read_end_io;
+               else
+                       bio->bi_end_io = f2fs_read_end_io;
        } else {
-               bio->bi_end_io = f2fs_write_end_io;
-               bio->bi_private = sbi;
+               if (fio->is_cache) {
+                       bio->bi_end_io = f2fs_cache_write_end_io;
+               } else {
+                       bio->bi_end_io = f2fs_write_end_io;
+                       bio->bi_private = sbi;
+               }
+
                bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
                                                fio->type, fio->temp);
                bio->bi_write_stream = f2fs_io_type_to_write_stream(bdev, fio->type,
@@ -533,7 +599,7 @@ static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
         * The f2fs garbage collector sets ->encrypted_page when it wants to
         * read/write raw data without encryption.
         */
-       if (!fio || !fio->encrypted_page)
+       if (!fio || (!fio->encrypted_page && !fio->is_cache))
                fscrypt_set_bio_crypt_ctx(bio, inode,
                                (loff_t)first_idx << inode->i_blkbits,
                                gfp_mask);
@@ -738,6 +804,53 @@ void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi,
        __submit_merged_write_cond(sbi, NULL, folio, 0, type, true);
 }
 
+static bool __has_merged_cache(struct bio *bio,
+               struct f2fs_cached_block *target)
+{
+       struct f2fs_cached_block *entry;
+
+       if (!bio)
+               return false;
+
+       entry = F2FS_BIO(bio)->entry;
+
+       while (entry) {
+               if (target && entry == target)
+                       return true;
+               entry = entry->next_entry;
+       }
+       return false;
+}
+
+bool f2fs_submit_merged_write_cache(struct f2fs_cached_block *entry,
+                               enum page_type type)
+{
+       struct f2fs_sb_info *sbi = entry->cache->sbi;
+       enum temp_type temp;
+       bool ret = false;
+
+       for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
+               enum page_type btype = PAGE_TYPE_OF_BIO(type);
+               struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
+               struct f2fs_lock_context lc;
+               bool merged;
+
+               f2fs_down_read_trace(&io->io_rwsem, &lc);
+               merged = __has_merged_cache(io->bio, entry);
+               f2fs_up_read_trace(&io->io_rwsem, &lc);
+
+               if (merged) {
+                       __f2fs_submit_merged_write(sbi, type, temp);
+                       ret = true;
+               }
+
+               /* TODO: use HOT temp only for meta pages now. */
+               if (type >= META)
+                       break;
+       }
+       return ret;
+}
+
 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
 {
        f2fs_submit_merged_write(sbi, DATA);
@@ -801,6 +914,8 @@ static bool io_type_is_mergeable(struct f2fs_bio_info *io,
 
        if (io->fio.op != fio->op)
                return false;
+       if (io->fio.is_cache != fio->is_cache)
+               return false;
        return (io->fio.op_flags & mask) == (fio->op_flags & mask);
 }
 
@@ -1032,6 +1147,33 @@ static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr)
                f2fs_blkz_is_seq(sbi, devi, blkaddr) &&
                (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
 }
+
+static void f2fs_wait_zone_io_completion(struct f2fs_sb_info *sbi,
+                       struct f2fs_bio_info *io, enum page_type btype)
+{
+       if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
+               wait_for_completion_io(&io->zone_wait);
+               bio_put(io->zone_pending_bio);
+               io->zone_pending_bio = NULL;
+               io->bi_private = NULL;
+       }
+}
+
+static void f2fs_submit_zone_io(struct f2fs_sb_info *sbi,
+                       struct f2fs_io_info *fio, struct f2fs_bio_info *io,
+                       enum page_type btype)
+{
+       if (f2fs_sb_has_blkzoned(sbi) && btype < META &&
+                       is_end_zone_blkaddr(sbi, fio->new_blkaddr)) {
+               bio_get(io->bio);
+               reinit_completion(&io->zone_wait);
+               io->bi_private = io->bio->bi_private;
+               io->bio->bi_private = io;
+               io->bio->bi_end_io = f2fs_zone_write_end_io;
+               io->zone_pending_bio = io->bio;
+               __submit_merged_bio(io);
+       }
+}
 #endif
 
 void f2fs_submit_page_write(struct f2fs_io_info *fio)
@@ -1048,14 +1190,8 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
        f2fs_down_write_trace(&io->io_rwsem, &lc);
 next:
 #ifdef CONFIG_BLK_DEV_ZONED
-       if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
-               wait_for_completion_io(&io->zone_wait);
-               bio_put(io->zone_pending_bio);
-               io->zone_pending_bio = NULL;
-               io->bi_private = NULL;
-       }
+       f2fs_wait_zone_io_completion(sbi, io, btype);
 #endif
-
        if (fio->in_list) {
                spin_lock(&io->io_lock);
                if (list_empty(&io->io_list)) {
@@ -1110,16 +1246,109 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
 
        trace_f2fs_submit_folio_write(fio->folio, fio);
 #ifdef CONFIG_BLK_DEV_ZONED
-       if (f2fs_sb_has_blkzoned(sbi) && btype < META &&
-                       is_end_zone_blkaddr(sbi, fio->new_blkaddr)) {
-               bio_get(io->bio);
-               reinit_completion(&io->zone_wait);
-               io->bi_private = io->bio->bi_private;
-               io->bio->bi_private = io;
-               io->bio->bi_end_io = f2fs_zone_write_end_io;
-               io->zone_pending_bio = io->bio;
+       f2fs_submit_zone_io(sbi, fio, io, btype);
+#endif
+
+       if (fio->in_list)
+               goto next;
+out:
+       if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
+                               !f2fs_is_checkpoint_ready(sbi))
                __submit_merged_bio(io);
+       f2fs_up_write_trace(&io->io_rwsem, &lc);
+}
+
+static void f2fs_bio_add_cache(struct f2fs_io_info *fio, struct bio *bio)
+{
+       struct f2fs_bio *fbio = F2FS_BIO(bio);
+       struct f2fs_cached_block *head = fbio->entry;
+       struct f2fs_cached_block *new = fio->cache_entry;
+
+       new->next_entry = head;
+       fbio->entry = new;
+}
+
+int f2fs_submit_cache_read(struct f2fs_io_info *fio)
+{
+       struct f2fs_sb_info *sbi = fio->sbi;
+       struct f2fs_cached_block *entry = fio->cache_entry;
+       struct bio *bio;
+       enum count_type io_type = IS_META_CACHE(entry->cache) ?
+                               F2FS_RD_META : F2FS_RD_NODE;
+
+       if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
+                       fio->is_por ? META_POR : (__is_meta_io(fio) ?
+                       META_GENERIC : DATA_GENERIC_ENHANCE)))
+               return -EFSCORRUPTED;
+
+       bio = __bio_alloc(fio, 1);
+
+       bio_add_virt_nofail(bio, cache_address(entry), sbi->blocksize);
+       f2fs_bio_add_cache(fio, bio);
+       inc_page_count(sbi, io_type);
+
+       f2fs_submit_read_bio(sbi, bio, fio->type);
+       return 0;
+}
+
+void f2fs_submit_cache_write(struct f2fs_io_info *fio)
+{
+       struct f2fs_sb_info *sbi = fio->sbi;
+       enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
+       struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
+       struct f2fs_lock_context lc;
+       struct folio *folio;
+
+       f2fs_bug_on(sbi, is_read_io(fio->op));
+
+       f2fs_down_write_trace(&io->io_rwsem, &lc);
+next:
+#ifdef CONFIG_BLK_DEV_ZONED
+       f2fs_wait_zone_io_completion(sbi, io, btype);
+#endif
+       if (fio->in_list) {
+               spin_lock(&io->io_lock);
+               if (list_empty(&io->io_list)) {
+                       spin_unlock(&io->io_lock);
+                       goto out;
+               }
+               fio = list_first_entry(&io->io_list,
+                                               struct f2fs_io_info, list);
+               list_del(&fio->list);
+               spin_unlock(&io->io_lock);
        }
+
+       verify_fio_blkaddr(fio);
+
+       fio->submitted = 1;
+       inc_page_count(sbi, F2FS_WB_CP_DATA);
+
+       if (io->bio &&
+           (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
+                             fio->new_blkaddr)))
+               __submit_merged_bio(io);
+alloc_new:
+       if (io->bio == NULL) {
+               io->bio = __bio_alloc(fio, BIO_MAX_VECS);
+               io->fio = *fio;
+       }
+
+       folio = cache_folio(fio->cache_entry);
+
+       if (!bio_add_folio(io->bio, folio, sbi->blocksize,
+                       offset_in_folio(folio, cache_address(fio->cache_entry)))) {
+               f2fs_bug_on(sbi, !F2FS_BIO(io->bio)->entry);
+
+               __submit_merged_bio(io);
+               goto alloc_new;
+       }
+
+       f2fs_bio_add_cache(fio, io->bio);
+
+       io->last_block_in_bio = fio->new_blkaddr;
+
+#ifdef CONFIG_BLK_DEV_ZONED
+       f2fs_submit_zone_io(sbi, fio, io, btype);
 #endif
        if (fio->in_list)
                goto next;
fs/f2fs/f2fs.h +24 -0
index ebc621f302e1..c0c7d51c830f 100644
@@ -222,6 +222,8 @@ struct f2fs_rwsem {
 #endif
 };
 
+#include "cache.h"
+
 struct f2fs_mount_info {
        unsigned long long opt;
        block_t root_reserved_blocks;   /* root reserved blocks */
@@ -1362,8 +1364,10 @@ struct f2fs_io_info {
        unsigned int in_list:1;         /* indicate fio is in io_list */
        unsigned int is_por:1;          /* indicate IO is from recovery or not */
        unsigned int meta_gc:1;         /* require meta inode GC */
+       unsigned int is_cache:1;        /* indicate IO is from internal cache */
        enum iostat_type io_type;       /* io type */
        struct writeback_control *io_wbc; /* writeback control */
+       struct f2fs_cached_block *cache_entry;
        struct bio **bio;               /* bio for ipu */
        sector_t *last_block;           /* last block number in bio */
 };
@@ -1773,6 +1777,12 @@ struct f2fs_gc_kthread {
        unsigned int boost_gc_greedy;
 };
 
+struct f2fs_bio {
+       struct work_struct work;
+       struct f2fs_cached_block *entry;
+       struct bio bio;
+};
+
 struct f2fs_sb_info {
        struct super_block *sb;                 /* pointer to VFS super block */
        struct proc_dir_entry *s_proc;          /* proc entry */
@@ -2381,6 +2391,16 @@ static inline bool is_node_folio(struct f2fs_sb_info *sbi, struct folio *folio)
        return folio->mapping == NODE_MAPPING(sbi);
 }
 
+static inline struct f2fs_bio *F2FS_BIO(struct bio *bio)
+{
+       return container_of(bio, struct f2fs_bio, bio);
+}
+
+static inline bool f2fs_is_cache_bio(struct bio *bio)
+{
+       return F2FS_BIO(bio)->entry != NULL;
+}
+
 static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
 {
        return test_bit(type, &sbi->s_flag);
@@ -4312,6 +4332,8 @@ void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
                                nid_t ino, enum page_type type);
 void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi,
                                struct folio *folio, enum page_type type);
+bool f2fs_submit_merged_write_cache(struct f2fs_cached_block *entry,
+                               enum page_type type);
 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
                                        struct bio **bio, struct folio *folio);
 void f2fs_submit_all_merged_ipu_writes(struct f2fs_sb_info *sbi);
@@ -4319,6 +4341,8 @@ void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
 int f2fs_submit_page_bio(struct f2fs_io_info *fio);
 int f2fs_merge_page_bio(struct f2fs_io_info *fio);
 void f2fs_submit_page_write(struct f2fs_io_info *fio);
+int f2fs_submit_cache_read(struct f2fs_io_info *fio);
+void f2fs_submit_cache_write(struct f2fs_io_info *fio);
 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
                block_t blk_addr, sector_t *sector);
 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
fs/f2fs/iostat.h +11 -1
index 2025225b5bed..61c6bc8e3119 100644
@@ -60,6 +60,13 @@ static inline struct bio_post_read_ctx *get_post_read_ctx(struct bio *bio)
        return iostat_ctx->post_read_ctx;
 }
 
+static inline void iostat_set_post_read_ctx(struct bio *bio, void *ctx)
+{
+       struct bio_iostat_ctx *iostat_ctx = bio->bi_private;
+
+       iostat_ctx->post_read_ctx = ctx;
+}
+
 extern void iostat_update_and_unbind_ctx(struct bio *bio);
 extern void iostat_alloc_and_bind_ctx(struct f2fs_sb_info *sbi,
                struct bio *bio, struct bio_post_read_ctx *ctx);
@@ -81,6 +88,10 @@ static inline struct bio_post_read_ctx *get_post_read_ctx(struct bio *bio)
 {
        return bio->bi_private;
 }
+static inline void iostat_set_post_read_ctx(struct bio *bio, void *ctx)
+{
+       bio->bi_private = ctx;
+}
 static inline int f2fs_init_iostat_processing(void) { return 0; }
 static inline void f2fs_destroy_iostat_processing(void) {}
 static inline int f2fs_init_iostat(struct f2fs_sb_info *sbi) { return 0; }
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#3 lore raw

PATCH v7 2/12 f2fs: cache: initialize meta cache

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch introduces meta_blocks in f2fs_sb_info structure, initializes and destroys the meta cache during filesystem mount and unmount.

It also introduces helper wrappers for meta cache operation.

---
 fs/f2fs/cache.h | 12 ++++++++++++
 fs/f2fs/data.c  |  3 +++
 fs/f2fs/f2fs.h  |  3 +++
 fs/f2fs/super.c | 10 +++++++++-
 4 files changed, 27 insertions(+), 1 deletion(-)
fs/f2fs/cache.h +12 -0
index 72dbf26ab26e..d5ee51691fac 100644
@@ -194,4 +194,16 @@ void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty);
 void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
 		unsigned long start, unsigned long len, bool drop_dirty);
 
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
+
+#define META_CACHE(sbi)		(&(sbi)->meta_blocks)
+
+#define f2fs_find_meta_cache(sbi, blkaddr)		\
+	f2fs_find_cache(META_CACHE(sbi), blkaddr)
+#define f2fs_invalidate_meta_caches(sbi, start, len)	\
+	f2fs_drop_cache_range(META_CACHE(sbi), start, len, false)
+#define f2fs_truncate_meta_caches(sbi, start, len)	\
+	f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/data.c +3 -0
index d868b939caee..59fef2132177 100644
@@ -670,6 +670,9 @@ static bool __has_merged_page(struct f2fs_sb_info *sbi, struct bio *bio,
 	if (!inode && !folio && !ino)
 		return true;
 
+	if (f2fs_is_cache_bio(bio))
+		return false;
+
 	bio_for_each_folio_all(fi, bio) {
 		struct folio *target = fi.folio;
 
fs/f2fs/f2fs.h +3 -0
index c0c7d51c830f..85c4bf40b4aa 100644
@@ -2107,6 +2107,9 @@ struct f2fs_sb_info {
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 	struct lock_class_key cp_global_sem_key;
 #endif
+
+	/* f2fs internal cache */
+	struct f2fs_cached_block_list meta_blocks;
 };
 
 /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +9 -1
index 6a2f09c61dcd..b731b9bb7b54 100644
@@ -2094,6 +2094,8 @@ static void f2fs_put_super(struct super_block *sb)
 	iput(sbi->meta_inode);
 	sbi->meta_inode = NULL;
 
+	f2fs_destroy_cache(META_CACHE(sbi));
+
 	/* Should check the page counts after dropping all node/meta pages */
 	for (i = 0; i < NR_COUNT_TYPE; i++) {
 		if (!get_pages(sbi, i))
@@ -5263,12 +5265,16 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (err)
 		goto free_percpu;
 
+	err = f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE);
+	if (err)
+		goto free_page_array_cache;
+
 	/* get an inode for meta space */
 	sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
 	if (IS_ERR(sbi->meta_inode)) {
 		f2fs_err(sbi, "Failed to read F2FS meta data inode");
 		err = PTR_ERR(sbi->meta_inode);
-		goto free_page_array_cache;
+		goto free_meta_cache;
 	}
 
 	err = f2fs_get_valid_checkpoint(sbi);
@@ -5596,6 +5602,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	make_bad_inode(sbi->meta_inode);
 	iput(sbi->meta_inode);
 	sbi->meta_inode = NULL;
+free_meta_cache:
+	f2fs_destroy_cache(META_CACHE(sbi));
 free_page_array_cache:
 	f2fs_destroy_page_array_cache(sbi);
 free_percpu:
Signature · 1 line
2.49.0
#4 lore raw

Re: [f2fs-dev] [PATCH v7 02/12] f2fs: cache: initialize meta cache

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 79 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces meta_blocks in f2fs_sb_info structure, initializes and destroys the meta cache during filesystem mount and unmount.

It also introduces helper wrappers for meta cache operation.

---
 fs/f2fs/cache.h | 12 ++++++++++++
 fs/f2fs/data.c  |  3 +++
 fs/f2fs/f2fs.h  |  3 +++
 fs/f2fs/super.c | 10 +++++++++-
 4 files changed, 27 insertions(+), 1 deletion(-)
fs/f2fs/cache.h +12 -0
index 72dbf26ab26e..d5ee51691fac 100644
@@ -194,4 +194,16 @@ void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty);
 void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
                unsigned long start, unsigned long len, bool drop_dirty);
 
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
+
+#define META_CACHE(sbi)                (&(sbi)->meta_blocks)
+
+#define f2fs_find_meta_cache(sbi, blkaddr)             \
+       f2fs_find_cache(META_CACHE(sbi), blkaddr)
+#define f2fs_invalidate_meta_caches(sbi, start, len)   \
+       f2fs_drop_cache_range(META_CACHE(sbi), start, len, false)
+#define f2fs_truncate_meta_caches(sbi, start, len)     \
+       f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/data.c +3 -0
index d868b939caee..59fef2132177 100644
@@ -670,6 +670,9 @@ static bool __has_merged_page(struct f2fs_sb_info *sbi, struct bio *bio,
        if (!inode && !folio && !ino)
                return true;
 
+       if (f2fs_is_cache_bio(bio))
+               return false;
+
        bio_for_each_folio_all(fi, bio) {
                struct folio *target = fi.folio;
 
fs/f2fs/f2fs.h +3 -0
index c0c7d51c830f..85c4bf40b4aa 100644
@@ -2107,6 +2107,9 @@ struct f2fs_sb_info {
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
        struct lock_class_key cp_global_sem_key;
 #endif
+
+       /* f2fs internal cache */
+       struct f2fs_cached_block_list meta_blocks;
 };
 
 /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +2 -0
index 6a2f09c61dcd..b731b9bb7b54 100644
@@ -2094,6 +2094,8 @@ static void f2fs_put_super(struct super_block *sb)
        iput(sbi->meta_inode);
        sbi->meta_inode = NULL;
 
+       f2fs_destroy_cache(META_CACHE(sbi));
+
        /* Should check the page counts after dropping all node/meta pages */
        for (i = 0; i < NR_COUNT_TYPE; i++) {
                if (!get_pages(sbi, i))

fs/f2fs/super.c static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) · line 5268

Quoted 4 lines
fs/f2fs/super.c +1 -0
@@ -5263,12 +5265,16 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
        if (err)
                goto free_percpu;
 
+       err = f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE);

never fail now?

Quoted 31 lines
fs/f2fs/super.c +6 -2
+       if (err)
+               goto free_page_array_cache;
+
        /* get an inode for meta space */
        sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
        if (IS_ERR(sbi->meta_inode)) {
                f2fs_err(sbi, "Failed to read F2FS meta data inode");
                err = PTR_ERR(sbi->meta_inode);
-               goto free_page_array_cache;
+               goto free_meta_cache;
        }
 
        err = f2fs_get_valid_checkpoint(sbi);
@@ -5596,6 +5602,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
        make_bad_inode(sbi->meta_inode);
        iput(sbi->meta_inode);
        sbi->meta_inode = NULL;
+free_meta_cache:
+       f2fs_destroy_cache(META_CACHE(sbi));
 free_page_array_cache:
        f2fs_destroy_page_array_cache(sbi);
 free_percpu:
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#5 lore raw

Re: [f2fs-dev] [PATCH v7 02/12] f2fs: cache: initialize meta cache

Quoted 84 lines · Zhiguo Niu

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 82 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces meta_blocks in f2fs_sb_info structure, initializes and destroys the meta cache during filesystem mount and unmount.

It also introduces helper wrappers for meta cache operation.

---
  fs/f2fs/cache.h | 12 ++++++++++++
  fs/f2fs/data.c  |  3 +++
  fs/f2fs/f2fs.h  |  3 +++
  fs/f2fs/super.c | 10 +++++++++-
  4 files changed, 27 insertions(+), 1 deletion(-)
fs/f2fs/cache.h +12 -0
index 72dbf26ab26e..d5ee51691fac 100644
@@ -194,4 +194,16 @@ void f2fs_truncate_cache(struct f2fs_cached_block *entry, bool drop_dirty);
  void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
                 unsigned long start, unsigned long len, bool drop_dirty);
 
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
+
+#define META_CACHE(sbi)                (&(sbi)->meta_blocks)
+
+#define f2fs_find_meta_cache(sbi, blkaddr)             \
+       f2fs_find_cache(META_CACHE(sbi), blkaddr)
+#define f2fs_invalidate_meta_caches(sbi, start, len)   \
+       f2fs_drop_cache_range(META_CACHE(sbi), start, len, false)
+#define f2fs_truncate_meta_caches(sbi, start, len)     \
+       f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
+
  #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/data.c +3 -0
index d868b939caee..59fef2132177 100644
@@ -670,6 +670,9 @@ static bool __has_merged_page(struct f2fs_sb_info *sbi, struct bio *bio,
         if (!inode && !folio && !ino)
                 return true;
 
+       if (f2fs_is_cache_bio(bio))
+               return false;
+
         bio_for_each_folio_all(fi, bio) {
                 struct folio *target = fi.folio;
 
fs/f2fs/f2fs.h +3 -0
index c0c7d51c830f..85c4bf40b4aa 100644
@@ -2107,6 +2107,9 @@ struct f2fs_sb_info {
  #ifdef CONFIG_DEBUG_LOCK_ALLOC
         struct lock_class_key cp_global_sem_key;
  #endif
+
+       /* f2fs internal cache */
+       struct f2fs_cached_block_list meta_blocks;
  };
 
  /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +2 -0
index 6a2f09c61dcd..b731b9bb7b54 100644
@@ -2094,6 +2094,8 @@ static void f2fs_put_super(struct super_block *sb)
         iput(sbi->meta_inode);
         sbi->meta_inode = NULL;
 
+       f2fs_destroy_cache(META_CACHE(sbi));
+
         /* Should check the page counts after dropping all node/meta pages */
         for (i = 0; i < NR_COUNT_TYPE; i++) {
                 if (!get_pages(sbi, i))
@@ -5263,12 +5265,16 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
         if (err)
                 goto free_percpu;
 
Quoted 2 lines

fs/f2fs/super.c static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc) · line 5268

Quoted 1 line
fs/f2fs/super.c +1 -0
+       err = f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE);

never fail now?

Yes, will clean up.

Thanks,

Quoted 31 lines
Quoted 31 lines
fs/f2fs/super.c +6 -2
+       if (err)
+               goto free_page_array_cache;
+
         /* get an inode for meta space */
         sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
         if (IS_ERR(sbi->meta_inode)) {
                 f2fs_err(sbi, "Failed to read F2FS meta data inode");
                 err = PTR_ERR(sbi->meta_inode);
-               goto free_page_array_cache;
+               goto free_meta_cache;
         }
 
         err = f2fs_get_valid_checkpoint(sbi);
@@ -5596,6 +5602,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
         make_bad_inode(sbi->meta_inode);
         iput(sbi->meta_inode);
         sbi->meta_inode = NULL;
+free_meta_cache:
+       f2fs_destroy_cache(META_CACHE(sbi));
  free_page_array_cache:
         f2fs_destroy_page_array_cache(sbi);
  free_percpu:
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#6 lore raw

PATCH v7 3/12 f2fs: cache: introduce shrinker

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch integrates the metadata cache into the F2FS memory shrinker subsystem to reclaim clean, unreferenced cached blocks under memory pressure.

It implements f2fs_shrink_cache() using a 3-phase cache reclamin method:
1. isolate clean entries from lru list
2. truncate from radix tree under lock
3. splice un-reclaimed entries back

And hooks the new interface into f2fs_shrink_count() and f2fs_shrink_scan().

---
 fs/f2fs/cache.c    | 89 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/f2fs/cache.h    |  3 ++
 fs/f2fs/shrinker.c | 12 +++++++
 3 files changed, 104 insertions(+)
fs/f2fs/cache.c +89 -0
index fb0ab77d3b49..563615761d11 100644
@@ -537,3 +537,92 @@ void f2fs_destroy_cache(struct f2fs_cached_block_list *cache)
 	f2fs_put_cache(entry, true);
 	goto next;
 }
+
+static unsigned long f2fs_do_shrink_cache(struct f2fs_cached_block_list *cache,
+						unsigned long nr_to_scan)
+{
+	struct f2fs_cached_block *entry, *next;
+	LIST_HEAD(dispose_list);
+	LIST_HEAD(keep_list);
+	unsigned long freed = 0;
+	unsigned long scanned = 0;
+
+	/* Phase 1: Isolate candidate entries from LRU list into dispose_list */
+	spin_lock(&cache->list_lock);
+	list_for_each_entry_safe(entry, next, &cache->lru_list, list) {
+		if (scanned >= cache->num_entries)
+			break;
+		if (scanned++ >= nr_to_scan)
+			break;
+
+		/* If accessed, give it a second chance to rotate to tail */
+		if (f2fs_cache_test_and_clear_referenced(entry)) {
+			list_move_tail(&entry->list, &cache->lru_list);
+			continue;
+		}
+
+		if (f2fs_cache_test_dirty(entry) ||
+		    f2fs_cache_test_writeback(entry) ||
+		    f2fs_cache_test_locked(entry))
+			continue;
+
+		if (f2fs_cache_refcount(entry) != 1)
+			continue;
+
+		list_move_tail(&entry->list, &dispose_list);
+	}
+	spin_unlock(&cache->list_lock);
+
+	/* Phase 2: Process isolated candidates one by one */
+	while (1) {
+		spin_lock(&cache->list_lock);
+		entry = list_first_entry_or_null(&dispose_list,
+						struct f2fs_cached_block, list);
+		if (!entry) {
+			spin_unlock(&cache->list_lock);
+			break;
+		}
+		f2fs_cache_get(entry);
+		list_move_tail(&entry->list, &keep_list);
+		spin_unlock(&cache->list_lock);
+
+		if (!f2fs_trylock_cache(entry)) {
+			f2fs_put_cache(entry, false);
+			continue;
+		}
+
+		/* the entry has been truncated */
+		if (!entry->cache) {
+			f2fs_put_cache(entry, true);
+			continue;
+		}
+		/*
+		 * at least there are shrinker, radix tree and another user
+		 * has referenced the entry.
+		 */
+		if (f2fs_cache_refcount(entry) >= 3) {
+			f2fs_put_cache(entry, true);
+			continue;
+		}
+
+		f2fs_do_truncate_cache(entry, false);
+
+		if (f2fs_put_cache(entry, true))
+			freed++;
+	}
+
+	/* Phase 3: Splice un-reclaimed entries back onto cache->lru_list */
+	if (!list_empty(&keep_list)) {
+		spin_lock(&cache->list_lock);
+		list_splice_tail(&keep_list, &cache->lru_list);
+		spin_unlock(&cache->list_lock);
+	}
+
+	return freed;
+}
+
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+					unsigned long nr_to_scan)
+{
+	return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+}
fs/f2fs/cache.h +3 -0
index d5ee51691fac..5728e2225465 100644
@@ -206,4 +206,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 #define f2fs_truncate_meta_caches(sbi, start, len)	\
 	f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
 
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+				unsigned long nr_to_scan);
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/shrinker.c +12 -0
index e3e0a7e89406..baa09bc63585 100644
@@ -37,6 +37,11 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
 				atomic_read(&eti->total_ext_node);
 }
 
+static unsigned long __count_cache(struct f2fs_sb_info *sbi)
+{
+	return META_CACHE(sbi)->num_entries;
+}
+
 unsigned long f2fs_shrink_count(struct shrinker *shrink,
 				struct shrink_control *sc)
 {
@@ -68,6 +73,9 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
 		/* count free nids cache entries */
 		count += __count_free_nids(sbi);
 
+		/* count generic cache entries */
+		count += __count_cache(sbi);
+
 		spin_lock(&f2fs_list_lock);
 		p = p->next;
 		mutex_unlock(&sbi->umount_mutex);
@@ -120,6 +128,10 @@ unsigned long f2fs_shrink_scan(struct shrinker *shrink,
 		if (freed < nr)
 			freed += f2fs_try_to_free_nids(sbi, nr - freed);
 
+		/* shrink generic cache entries */
+		if (freed < nr)
+			freed += f2fs_shrink_cache(sbi, nr - freed);
+
 		spin_lock(&f2fs_list_lock);
 		p = p->next;
 		list_move_tail(&sbi->s_list, &f2fs_list);
Signature · 1 line
2.49.0
Reviewed-by on v7 3/12
#7 lore raw

Re: [f2fs-dev] [PATCH v7 03/12] f2fs: cache: introduce shrinker

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 7 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch integrates the metadata cache into the F2FS memory shrinker subsystem to reclaim clean, unreferenced cached blocks under memory pressure.

Quoted 8 lines

It implements f2fs_shrink_cache() using a 3-phase cache reclamin method:
1. isolate clean entries from lru list
2. truncate from radix tree under lock
3. splice un-reclaimed entries back

And hooks the new interface into f2fs_shrink_count() and f2fs_shrink_scan().

thanks!

Quoted 161 lines
---
 fs/f2fs/cache.c    | 89 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/f2fs/cache.h    |  3 ++
 fs/f2fs/shrinker.c | 12 +++++++
 3 files changed, 104 insertions(+)
fs/f2fs/cache.c +89 -0
index fb0ab77d3b49..563615761d11 100644
@@ -537,3 +537,92 @@ void f2fs_destroy_cache(struct f2fs_cached_block_list *cache)
        f2fs_put_cache(entry, true);
        goto next;
 }
+
+static unsigned long f2fs_do_shrink_cache(struct f2fs_cached_block_list *cache,
+                                               unsigned long nr_to_scan)
+{
+       struct f2fs_cached_block *entry, *next;
+       LIST_HEAD(dispose_list);
+       LIST_HEAD(keep_list);
+       unsigned long freed = 0;
+       unsigned long scanned = 0;
+
+       /* Phase 1: Isolate candidate entries from LRU list into dispose_list */
+       spin_lock(&cache->list_lock);
+       list_for_each_entry_safe(entry, next, &cache->lru_list, list) {
+               if (scanned >= cache->num_entries)
+                       break;
+               if (scanned++ >= nr_to_scan)
+                       break;
+
+               /* If accessed, give it a second chance to rotate to tail */
+               if (f2fs_cache_test_and_clear_referenced(entry)) {
+                       list_move_tail(&entry->list, &cache->lru_list);
+                       continue;
+               }
+
+               if (f2fs_cache_test_dirty(entry) ||
+                   f2fs_cache_test_writeback(entry) ||
+                   f2fs_cache_test_locked(entry))
+                       continue;
+
+               if (f2fs_cache_refcount(entry) != 1)
+                       continue;
+
+               list_move_tail(&entry->list, &dispose_list);
+       }
+       spin_unlock(&cache->list_lock);
+
+       /* Phase 2: Process isolated candidates one by one */
+       while (1) {
+               spin_lock(&cache->list_lock);
+               entry = list_first_entry_or_null(&dispose_list,
+                                               struct f2fs_cached_block, list);
+               if (!entry) {
+                       spin_unlock(&cache->list_lock);
+                       break;
+               }
+               f2fs_cache_get(entry);
+               list_move_tail(&entry->list, &keep_list);
+               spin_unlock(&cache->list_lock);
+
+               if (!f2fs_trylock_cache(entry)) {
+                       f2fs_put_cache(entry, false);
+                       continue;
+               }
+
+               /* the entry has been truncated */
+               if (!entry->cache) {
+                       f2fs_put_cache(entry, true);
+                       continue;
+               }
+               /*
+                * at least there are shrinker, radix tree and another user
+                * has referenced the entry.
+                */
+               if (f2fs_cache_refcount(entry) >= 3) {
+                       f2fs_put_cache(entry, true);
+                       continue;
+               }
+
+               f2fs_do_truncate_cache(entry, false);
+
+               if (f2fs_put_cache(entry, true))
+                       freed++;
+       }
+
+       /* Phase 3: Splice un-reclaimed entries back onto cache->lru_list */
+       if (!list_empty(&keep_list)) {
+               spin_lock(&cache->list_lock);
+               list_splice_tail(&keep_list, &cache->lru_list);
+               spin_unlock(&cache->list_lock);
+       }
+
+       return freed;
+}
+
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+                                       unsigned long nr_to_scan)
+{
+       return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+}
fs/f2fs/cache.h +3 -0
index d5ee51691fac..5728e2225465 100644
@@ -206,4 +206,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 #define f2fs_truncate_meta_caches(sbi, start, len)     \
        f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
 
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+                               unsigned long nr_to_scan);
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/shrinker.c +12 -1
index e3e0a7e89406..baa09bc63585 100644
@@ -37,6 +37,11 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
                                atomic_read(&eti->total_ext_node);
 }
 
+static unsigned long __count_cache(struct f2fs_sb_info *sbi)
+{
+       return META_CACHE(sbi)->num_entries;
+}
+
 unsigned long f2fs_shrink_count(struct shrinker *shrink,
                                struct shrink_control *sc)
 {
@@ -68,6 +73,9 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
                /* count free nids cache entries */
                count += __count_free_nids(sbi);
 
+               /* count generic cache entries */
+               count += __count_cache(sbi);
+
                spin_lock(&f2fs_list_lock);
                p = p->next;
                mutex_unlock(&sbi->umount_mutex);
@@ -120,6 +128,10 @@ unsigned long f2fs_shrink_scan(struct shrinker *shrink,
                if (freed < nr)
                        freed += f2fs_try_to_free_nids(sbi, nr - freed);
 
+               /* shrink generic cache entries */
+               if (freed < nr)
+                       freed += f2fs_shrink_cache(sbi, nr - freed);
+
                spin_lock(&f2fs_list_lock);
                p = p->next;
                list_move_tail(&sbi->s_list, &f2fs_list);
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#8 lore raw

Re: [f2fs-dev] [PATCH v7 03/12] f2fs: cache: introduce shrinker

Quoted 15 lines · Zhiguo Niu <[email protected]> 于2026年9月15日周二 10:34写道:

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 12 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch integrates the metadata cache into the F2FS memory shrinker subsystem to reclaim clean, unreferenced cached blocks under memory pressure.

It implements f2fs_shrink_cache() using a 3-phase cache reclamin method:
1. isolate clean entries from lru list
2. truncate from radix tree under lock
3. splice un-reclaimed entries back

Quoted 4 lines
Quoted 3 lines

And hooks the new interface into f2fs_shrink_count() and f2fs_shrink_scan().

Hi Chao,
sorry, pls ignore the attachment in the last email. its Operational error by myself.
thanks!

Quoted 162 lines

thanks!

Quoted 161 lines
---
 fs/f2fs/cache.c    | 89 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/f2fs/cache.h    |  3 ++
 fs/f2fs/shrinker.c | 12 +++++++
 3 files changed, 104 insertions(+)
fs/f2fs/cache.c +89 -0
index fb0ab77d3b49..563615761d11 100644
@@ -537,3 +537,92 @@ void f2fs_destroy_cache(struct f2fs_cached_block_list *cache)
        f2fs_put_cache(entry, true);
        goto next;
 }
+
+static unsigned long f2fs_do_shrink_cache(struct f2fs_cached_block_list *cache,
+                                               unsigned long nr_to_scan)
+{
+       struct f2fs_cached_block *entry, *next;
+       LIST_HEAD(dispose_list);
+       LIST_HEAD(keep_list);
+       unsigned long freed = 0;
+       unsigned long scanned = 0;
+
+       /* Phase 1: Isolate candidate entries from LRU list into dispose_list */
+       spin_lock(&cache->list_lock);
+       list_for_each_entry_safe(entry, next, &cache->lru_list, list) {
+               if (scanned >= cache->num_entries)
+                       break;
+               if (scanned++ >= nr_to_scan)
+                       break;
+
+               /* If accessed, give it a second chance to rotate to tail */
+               if (f2fs_cache_test_and_clear_referenced(entry)) {
+                       list_move_tail(&entry->list, &cache->lru_list);
+                       continue;
+               }
+
+               if (f2fs_cache_test_dirty(entry) ||
+                   f2fs_cache_test_writeback(entry) ||
+                   f2fs_cache_test_locked(entry))
+                       continue;
+
+               if (f2fs_cache_refcount(entry) != 1)
+                       continue;
+
+               list_move_tail(&entry->list, &dispose_list);
+       }
+       spin_unlock(&cache->list_lock);
+
+       /* Phase 2: Process isolated candidates one by one */
+       while (1) {
+               spin_lock(&cache->list_lock);
+               entry = list_first_entry_or_null(&dispose_list,
+                                               struct f2fs_cached_block, list);
+               if (!entry) {
+                       spin_unlock(&cache->list_lock);
+                       break;
+               }
+               f2fs_cache_get(entry);
+               list_move_tail(&entry->list, &keep_list);
+               spin_unlock(&cache->list_lock);
+
+               if (!f2fs_trylock_cache(entry)) {
+                       f2fs_put_cache(entry, false);
+                       continue;
+               }
+
+               /* the entry has been truncated */
+               if (!entry->cache) {
+                       f2fs_put_cache(entry, true);
+                       continue;
+               }
+               /*
+                * at least there are shrinker, radix tree and another user
+                * has referenced the entry.
+                */
+               if (f2fs_cache_refcount(entry) >= 3) {
+                       f2fs_put_cache(entry, true);
+                       continue;
+               }
+
+               f2fs_do_truncate_cache(entry, false);
+
+               if (f2fs_put_cache(entry, true))
+                       freed++;
+       }
+
+       /* Phase 3: Splice un-reclaimed entries back onto cache->lru_list */
+       if (!list_empty(&keep_list)) {
+               spin_lock(&cache->list_lock);
+               list_splice_tail(&keep_list, &cache->lru_list);
+               spin_unlock(&cache->list_lock);
+       }
+
+       return freed;
+}
+
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+                                       unsigned long nr_to_scan)
+{
+       return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+}
fs/f2fs/cache.h +3 -0
index d5ee51691fac..5728e2225465 100644
@@ -206,4 +206,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 #define f2fs_truncate_meta_caches(sbi, start, len)     \
        f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
 
+unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
+                               unsigned long nr_to_scan);
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/shrinker.c +12 -1
index e3e0a7e89406..baa09bc63585 100644
@@ -37,6 +37,11 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
                                atomic_read(&eti->total_ext_node);
 }
 
+static unsigned long __count_cache(struct f2fs_sb_info *sbi)
+{
+       return META_CACHE(sbi)->num_entries;
+}
+
 unsigned long f2fs_shrink_count(struct shrinker *shrink,
                                struct shrink_control *sc)
 {
@@ -68,6 +73,9 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
                /* count free nids cache entries */
                count += __count_free_nids(sbi);
 
+               /* count generic cache entries */
+               count += __count_cache(sbi);
+
                spin_lock(&f2fs_list_lock);
                p = p->next;
                mutex_unlock(&sbi->umount_mutex);
@@ -120,6 +128,10 @@ unsigned long f2fs_shrink_scan(struct shrinker *shrink,
                if (freed < nr)
                        freed += f2fs_try_to_free_nids(sbi, nr - freed);
 
+               /* shrink generic cache entries */
+               if (freed < nr)
+                       freed += f2fs_shrink_cache(sbi, nr - freed);
+
                spin_lock(&f2fs_list_lock);
                p = p->next;
                list_move_tail(&sbi->s_list, &f2fs_list);
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#9 lore raw

PATCH v7 4/12 f2fs: cache: introduce writeback thread

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch introduces a background writeback kthread (f2fs_writeback-x:y) to periodically flush dirty metadata cache entries with a default interval of 5 seconds.

It manages thread lifecycle across mount, unmount, and remount (rw/ro) transitions.

It introduces a sysfs entry /sys/fs/f2fs/<disk>/cache_wb_interval to control writeback interval.

---
 Documentation/ABI/testing/sysfs-fs-f2fs |  6 +++
 fs/f2fs/cache.c                         | 56 +++++++++++++++++++++++++
 fs/f2fs/cache.h                         | 12 ++++++
 fs/f2fs/f2fs.h                          |  3 ++
 fs/f2fs/super.c                         | 33 ++++++++++++++-
 fs/f2fs/sysfs.c                         | 13 +++++-
 6 files changed, 120 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-fs-f2fs +6 -0
index 0cebc89799dd..d2665b7f1f14 100644
@@ -1020,3 +1020,9 @@ Contact:	"Daeho Jeong" <[email protected]>
 Description:	This is a read-only entry to show the upper bound section number
 		for pinned files. Pinned files will only be allocated within
 		sections 0 to pinned_area_max_secno - 1.
+
+What:		/sys/fs/f2fs/<disk>/cache_wb_interval
+Date:		August 2026
+Contact:	"Chao Yu" <[email protected]>
+Description:	This is a writable entry to control writeback interval of
+		f2fs_writeback-x:y, by default the value is 5000, unit is ms.
fs/f2fs/cache.c +56 -0

59 lines not on this page · raw message

fs/f2fs/cache.h +12 -0
index 5728e2225465..ea0cf9adf194 100644
@@ -209,4 +209,16 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
 				unsigned long nr_to_scan);
 
+#define DEF_DIRTY_CACHE_TIMEOUT 5000
+
+struct f2fs_cache_kthread {
+	struct task_struct *cache_wb_task;
+	wait_queue_head_t cache_wb_wq;
+	atomic_t cache_wb_trigger;
+	unsigned int cache_wb_interval;
+};
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/f2fs.h +3 -0
index 85c4bf40b4aa..c0f82b63d732 100644
@@ -2110,6 +2110,9 @@ struct f2fs_sb_info {
 
 	/* f2fs internal cache */
 	struct f2fs_cached_block_list meta_blocks;
+
+	/* internal cache flush thread */
+	struct f2fs_cache_kthread cache_thread;
 };
 
 /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +31 -2
index b731b9bb7b54..516b4ee12564 100644
@@ -2039,6 +2039,7 @@ static void f2fs_put_super(struct super_block *sb)
 	 * flush all issued checkpoints and stop checkpoint issue thread.
 	 * after then, all checkpoints should be done by each process context.
 	 */
+	f2fs_stop_cache_wb_thread(sbi);
 	f2fs_stop_ckpt_thread(sbi);
 
 	/*
@@ -2845,6 +2846,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
 	unsigned int flags = fc->sb_flags;
 	int err;
 	bool need_restart_gc = false, need_stop_gc = false;
+	bool need_restart_wb = false, need_stop_wb = false;
 	bool need_restart_flush = false, need_stop_flush = false;
 	bool need_restart_discard = false, need_stop_discard = false;
 	bool need_enable_checkpoint = false, need_disable_checkpoint = false;
@@ -3004,6 +3006,18 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
 		need_stop_gc = true;
 	}
 
+	if (flags & SB_RDONLY) {
+		if (sbi->cache_thread.cache_wb_task) {
+			f2fs_stop_cache_wb_thread(sbi);
+			need_restart_wb = true;
+		}
+	} else if (!sbi->cache_thread.cache_wb_task) {
+		err = f2fs_start_cache_wb_thread(sbi);
+		if (err)
+			goto restore_gc;
+		need_stop_wb = true;
+	}
+
 	if (flags & SB_RDONLY) {
 		sync_inodes_sb(sb);
 
@@ -3011,7 +3025,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
 		set_sbi_flag(sbi, SBI_IS_CLOSE);
 		err = f2fs_sync_fs(sb, 1);
 		if (err)
-			goto restore_gc;
+			goto restore_wb;
 		clear_sbi_flag(sbi, SBI_IS_CLOSE);
 	}
 
@@ -3026,7 +3040,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
 	} else {
 		err = f2fs_create_flush_cmd_control(sbi);
 		if (err)
-			goto restore_gc;
+			goto restore_wb;
 		need_stop_flush = true;
 	}
 
@@ -3123,6 +3137,13 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
 		clear_opt(sbi, FLUSH_MERGE);
 		f2fs_destroy_flush_cmd_control(sbi, false);
 	}
+restore_wb:
+	if (need_restart_wb) {
+		if (f2fs_start_cache_wb_thread(sbi))
+			f2fs_warn(sbi, "background cache writeback thread has stopped");
+	} else if (need_stop_wb) {
+		f2fs_stop_cache_wb_thread(sbi);
+	}
 restore_gc:
 	if (need_restart_gc) {
 		if (f2fs_start_gc_thread(sbi))
@@ -5529,6 +5550,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 			goto sync_free_meta;
 	}
 
+	if (!f2fs_readonly(sb)) {
+		err = f2fs_start_cache_wb_thread(sbi);
+		if (err)
+			goto stop_gc_thread;
+	}
+
 	/* recover broken superblock */
 	if (recovery) {
 		err = f2fs_commit_super(sbi, true);
@@ -5551,6 +5578,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	sbi->umount_lock_holder = NULL;
 	return 0;
 
+stop_gc_thread:
+	f2fs_stop_gc_thread(sbi);
 sync_free_meta:
 	/* safe to flush all the data */
 	sync_filesystem(sbi->sb);
fs/f2fs/sysfs.c +12 -1
index aaca9ed9b169..ad25733e32c0 100644
@@ -40,6 +40,7 @@ enum {
 	RESERVED_BLOCKS,	/* struct f2fs_sb_info */
 	CPRC_INFO,	/* struct ckpt_req_control */
 	ATGC_INFO,	/* struct atgc_management */
+	WB_THREAD,	/* struct f2fs_cache_kthread */
 };
 
 static const char *gc_mode_names[MAX_GC_MODE] = {
@@ -98,6 +99,8 @@ static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
 		return (unsigned char *)&sbi->cprc_info;
 	else if (struct_type == ATGC_INFO)
 		return (unsigned char *)&sbi->am;
+	else if (struct_type == WB_THREAD)
+		return (unsigned char *)&sbi->cache_thread;
 	return NULL;
 }
 
@@ -1008,7 +1011,8 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
 	bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
 					a->struct_type == GC_THREAD);
 	bool thread_entry = !strcmp(a->attr.name, "ckpt_thread_ioprio") ||
-			!strcmp(a->attr.name, "critical_task_priority");
+			!strcmp(a->attr.name, "critical_task_priority") ||
+			!strcmp(a->attr.name, "cache_wb_interval");
 
 	if (gc_entry || thread_entry) {
 		if (!down_read_trylock(&sbi->sb->s_umount))
@@ -1218,6 +1222,9 @@ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
 #define ATGC_INFO_RW_ATTR(name, elname)				\
 	F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
 
+#define WB_THREAD_RW_ATTR(name, elname)				\
+	F2FS_RW_ATTR(WB_THREAD, f2fs_cache_kthread, name, elname)
+
 /* GC_THREAD ATTR */
 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
@@ -1347,6 +1354,9 @@ ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
 
+/* WB_THREAD ATTR */
+WB_THREAD_RW_ATTR(cache_wb_interval, cache_wb_interval);
+
 F2FS_GENERAL_RO_ATTR(dirty_segments);
 F2FS_GENERAL_RO_ATTR(free_segments);
 F2FS_GENERAL_RO_ATTR(ovp_segments);
@@ -1533,6 +1543,7 @@ static struct attribute *f2fs_attrs[] = {
 	ATTR_LIST(lock_duration_priority),
 	ATTR_LIST(adjust_lock_priority),
 	ATTR_LIST(critical_task_priority),
+	ATTR_LIST(cache_wb_interval),
 	NULL,
 };
 ATTRIBUTE_GROUPS(f2fs);
Signature · 1 line
2.49.0
#10 lore raw

Re: [f2fs-dev] [PATCH v7 04/12] f2fs: cache: introduce writeback thread

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 42 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces a background writeback kthread (f2fs_writeback-x:y) to periodically flush dirty metadata cache entries with a default interval of 5 seconds.

It manages thread lifecycle across mount, unmount, and remount (rw/ro) transitions.

It introduces a sysfs entry /sys/fs/f2fs/<disk>/cache_wb_interval to control writeback interval.

---
 Documentation/ABI/testing/sysfs-fs-f2fs |  6 +++
 fs/f2fs/cache.c                         | 56 +++++++++++++++++++++++++
 fs/f2fs/cache.h                         | 12 ++++++
 fs/f2fs/f2fs.h                          |  3 ++
 fs/f2fs/super.c                         | 33 ++++++++++++++-
 fs/f2fs/sysfs.c                         | 13 +++++-
 6 files changed, 120 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-fs-f2fs +6 -0
index 0cebc89799dd..d2665b7f1f14 100644
@@ -1020,3 +1020,9 @@ Contact:  "Daeho Jeong" <[email protected]>
 Description:   This is a read-only entry to show the upper bound section number
                for pinned files. Pinned files will only be allocated within
                sections 0 to pinned_area_max_secno - 1.
+
+What:          /sys/fs/f2fs/<disk>/cache_wb_interval
+Date:          August 2026
+Contact:       "Chao Yu" <[email protected]>
+Description:   This is a writable entry to control writeback interval of
+               f2fs_writeback-x:y, by default the value is 5000, unit is ms.
fs/f2fs/cache.c +0 -0
index 563615761d11..404ceac9e6bf 100644

fs/f2fs/cache.c unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi, · line 629

Quoted 4 lines
fs/f2fs/cache.c +1 -0
@@ -626,3 +626,59 @@ unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
 {
        return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
 }
+

Hi Chao

fs/f2fs/cache.c unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi, · line 642

Quoted 13 lines
fs/f2fs/cache.c +13 -0
+static int f2fs_cache_writeback_kthread(void *data)
+{
+       struct f2fs_sb_info *sbi = data;
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+       wait_queue_head_t *wq = &cache_thread->cache_wb_wq;
+       unsigned int interval = cache_thread->cache_wb_interval;
+
+       set_freezable();
+
+       while (!kthread_should_stop()) {
+               wait_event_freezable_timeout(*wq,
+                               kthread_should_stop(),
+                               msecs_to_jiffies(interval));

if wb thread is running and user set cache_wb_interval at the same time, the wb thread can not get the new wb interval value for next round? because local varible is used here

Quoted 47 lines
fs/f2fs/cache.c +42 -0
+
+               if (kthread_should_stop())
+                       break;
+               if (f2fs_cp_error(sbi))
+                       continue;
+       }
+       return 0;
+}
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi)
+{
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+       struct task_struct *task;
+       dev_t dev = sbi->sb->s_dev;
+       char name[36];
+
+       if (cache_thread->cache_wb_task)
+               return 0;
+
+       init_waitqueue_head(&cache_thread->cache_wb_wq);
+       cache_thread->cache_wb_interval = DEF_DIRTY_CACHE_TIMEOUT;
+       snprintf(name, sizeof(name), "f2fs_writeback-%u:%u",
+                       MAJOR(dev), MINOR(dev));
+
+       task = kthread_run(f2fs_cache_writeback_kthread, sbi, "%s", name);
+       if (IS_ERR(task))
+               return PTR_ERR(task);
+
+       cache_thread->cache_wb_task = task;
+       return 0;
+}
+
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi)
+{
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+
+       if (!cache_thread->cache_wb_task)
+               return;
+
+       kthread_stop(cache_thread->cache_wb_task);
+       cache_thread->cache_wb_task = NULL;
+}
fs/f2fs/cache.h +0 -0
index 5728e2225465..ea0cf9adf194 100644

fs/f2fs/cache.h void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi); · line 217

Quoted 9 lines
fs/f2fs/cache.h +6 -0
@@ -209,4 +209,16 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
                                unsigned long nr_to_scan);
 
+#define DEF_DIRTY_CACHE_TIMEOUT 5000
+
+struct f2fs_cache_kthread {
+       struct task_struct *cache_wb_task;
+       wait_queue_head_t cache_wb_wq;
+       atomic_t cache_wb_trigger;

not used?
thanks!

Quoted 182 lines
fs/f2fs/cache.h +6 -0
+       unsigned int cache_wb_interval;
+};
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
+
 #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/f2fs.h +3 -0
index 85c4bf40b4aa..c0f82b63d732 100644
@@ -2110,6 +2110,9 @@ struct f2fs_sb_info {
 
        /* f2fs internal cache */
        struct f2fs_cached_block_list meta_blocks;
+
+       /* internal cache flush thread */
+       struct f2fs_cache_kthread cache_thread;
 };
 
 /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +31 -2
index b731b9bb7b54..516b4ee12564 100644
@@ -2039,6 +2039,7 @@ static void f2fs_put_super(struct super_block *sb)
         * flush all issued checkpoints and stop checkpoint issue thread.
         * after then, all checkpoints should be done by each process context.
         */
+       f2fs_stop_cache_wb_thread(sbi);
        f2fs_stop_ckpt_thread(sbi);
 
        /*
@@ -2845,6 +2846,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
        unsigned int flags = fc->sb_flags;
        int err;
        bool need_restart_gc = false, need_stop_gc = false;
+       bool need_restart_wb = false, need_stop_wb = false;
        bool need_restart_flush = false, need_stop_flush = false;
        bool need_restart_discard = false, need_stop_discard = false;
        bool need_enable_checkpoint = false, need_disable_checkpoint = false;
@@ -3004,6 +3006,18 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                need_stop_gc = true;
        }
 
+       if (flags & SB_RDONLY) {
+               if (sbi->cache_thread.cache_wb_task) {
+                       f2fs_stop_cache_wb_thread(sbi);
+                       need_restart_wb = true;
+               }
+       } else if (!sbi->cache_thread.cache_wb_task) {
+               err = f2fs_start_cache_wb_thread(sbi);
+               if (err)
+                       goto restore_gc;
+               need_stop_wb = true;
+       }
+
        if (flags & SB_RDONLY) {
                sync_inodes_sb(sb);
 
@@ -3011,7 +3025,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                set_sbi_flag(sbi, SBI_IS_CLOSE);
                err = f2fs_sync_fs(sb, 1);
                if (err)
-                       goto restore_gc;
+                       goto restore_wb;
                clear_sbi_flag(sbi, SBI_IS_CLOSE);
        }
 
@@ -3026,7 +3040,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
        } else {
                err = f2fs_create_flush_cmd_control(sbi);
                if (err)
-                       goto restore_gc;
+                       goto restore_wb;
                need_stop_flush = true;
        }
 
@@ -3123,6 +3137,13 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                clear_opt(sbi, FLUSH_MERGE);
                f2fs_destroy_flush_cmd_control(sbi, false);
        }
+restore_wb:
+       if (need_restart_wb) {
+               if (f2fs_start_cache_wb_thread(sbi))
+                       f2fs_warn(sbi, "background cache writeback thread has stopped");
+       } else if (need_stop_wb) {
+               f2fs_stop_cache_wb_thread(sbi);
+       }
 restore_gc:
        if (need_restart_gc) {
                if (f2fs_start_gc_thread(sbi))
@@ -5529,6 +5550,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
                        goto sync_free_meta;
        }
 
+       if (!f2fs_readonly(sb)) {
+               err = f2fs_start_cache_wb_thread(sbi);
+               if (err)
+                       goto stop_gc_thread;
+       }
+
        /* recover broken superblock */
        if (recovery) {
                err = f2fs_commit_super(sbi, true);
@@ -5551,6 +5578,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
        sbi->umount_lock_holder = NULL;
        return 0;
 
+stop_gc_thread:
+       f2fs_stop_gc_thread(sbi);
 sync_free_meta:
        /* safe to flush all the data */
        sync_filesystem(sbi->sb);
fs/f2fs/sysfs.c +12 -2
index aaca9ed9b169..ad25733e32c0 100644
@@ -40,6 +40,7 @@ enum {
        RESERVED_BLOCKS,        /* struct f2fs_sb_info */
        CPRC_INFO,      /* struct ckpt_req_control */
        ATGC_INFO,      /* struct atgc_management */
+       WB_THREAD,      /* struct f2fs_cache_kthread */
 };
 
 static const char *gc_mode_names[MAX_GC_MODE] = {
@@ -98,6 +99,8 @@ static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
                return (unsigned char *)&sbi->cprc_info;
        else if (struct_type == ATGC_INFO)
                return (unsigned char *)&sbi->am;
+       else if (struct_type == WB_THREAD)
+               return (unsigned char *)&sbi->cache_thread;
        return NULL;
 }
 
@@ -1008,7 +1011,8 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
        bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
                                        a->struct_type == GC_THREAD);
        bool thread_entry = !strcmp(a->attr.name, "ckpt_thread_ioprio") ||
-                       !strcmp(a->attr.name, "critical_task_priority");
+                       !strcmp(a->attr.name, "critical_task_priority") ||
+                       !strcmp(a->attr.name, "cache_wb_interval");
 
        if (gc_entry || thread_entry) {
                if (!down_read_trylock(&sbi->sb->s_umount))
@@ -1218,6 +1222,9 @@ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
 #define ATGC_INFO_RW_ATTR(name, elname)                                \
        F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
 
+#define WB_THREAD_RW_ATTR(name, elname)                                \
+       F2FS_RW_ATTR(WB_THREAD, f2fs_cache_kthread, name, elname)
+
 /* GC_THREAD ATTR */
 GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
 GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
@@ -1347,6 +1354,9 @@ ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
 ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
 ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
 
+/* WB_THREAD ATTR */
+WB_THREAD_RW_ATTR(cache_wb_interval, cache_wb_interval);
+
 F2FS_GENERAL_RO_ATTR(dirty_segments);
 F2FS_GENERAL_RO_ATTR(free_segments);
 F2FS_GENERAL_RO_ATTR(ovp_segments);
@@ -1533,6 +1543,7 @@ static struct attribute *f2fs_attrs[] = {
        ATTR_LIST(lock_duration_priority),
        ATTR_LIST(adjust_lock_priority),
        ATTR_LIST(critical_task_priority),
+       ATTR_LIST(cache_wb_interval),
        NULL,
 };
 ATTRIBUTE_GROUPS(f2fs);
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#11 lore raw

Re: [f2fs-dev] [PATCH v7 04/12] f2fs: cache: introduce writeback thread

Zhiguo,

Thanks for the review, let fix them all.

Thanks,

Quoted 305 lines · Zhiguo Niu

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 42 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces a background writeback kthread (f2fs_writeback-x:y) to periodically flush dirty metadata cache entries with a default interval of 5 seconds.

It manages thread lifecycle across mount, unmount, and remount (rw/ro) transitions.

It introduces a sysfs entry /sys/fs/f2fs/<disk>/cache_wb_interval to control writeback interval.

---
  Documentation/ABI/testing/sysfs-fs-f2fs |  6 +++
  fs/f2fs/cache.c                         | 56 +++++++++++++++++++++++++
  fs/f2fs/cache.h                         | 12 ++++++
  fs/f2fs/f2fs.h                          |  3 ++
  fs/f2fs/super.c                         | 33 ++++++++++++++-
  fs/f2fs/sysfs.c                         | 13 +++++-
  6 files changed, 120 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-fs-f2fs +6 -0
index 0cebc89799dd..d2665b7f1f14 100644
@@ -1020,3 +1020,9 @@ Contact:  "Daeho Jeong" <[email protected]>
  Description:   This is a read-only entry to show the upper bound section number
                 for pinned files. Pinned files will only be allocated within
                 sections 0 to pinned_area_max_secno - 1.
+
+What:          /sys/fs/f2fs/<disk>/cache_wb_interval
+Date:          August 2026
+Contact:       "Chao Yu" <[email protected]>
+Description:   This is a writable entry to control writeback interval of
+               f2fs_writeback-x:y, by default the value is 5000, unit is ms.
fs/f2fs/cache.c +0 -0
index 563615761d11..404ceac9e6bf 100644

fs/f2fs/cache.c unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi, · line 629

Quoted 4 lines
fs/f2fs/cache.c +1 -0
@@ -626,3 +626,59 @@ unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
  {
         return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
  }
+

Hi Chao

fs/f2fs/cache.c unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi, · line 642

Quoted 13 lines
fs/f2fs/cache.c +13 -0
+static int f2fs_cache_writeback_kthread(void *data)
+{
+       struct f2fs_sb_info *sbi = data;
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+       wait_queue_head_t *wq = &cache_thread->cache_wb_wq;
+       unsigned int interval = cache_thread->cache_wb_interval;
+
+       set_freezable();
+
+       while (!kthread_should_stop()) {
+               wait_event_freezable_timeout(*wq,
+                               kthread_should_stop(),
+                               msecs_to_jiffies(interval));

if wb thread is running and user set cache_wb_interval at the same time, the wb thread can not get the new wb interval value for next round? because local varible is used here

Quoted 47 lines
fs/f2fs/cache.c +42 -0
+
+               if (kthread_should_stop())
+                       break;
+               if (f2fs_cp_error(sbi))
+                       continue;
+       }
+       return 0;
+}
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi)
+{
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+       struct task_struct *task;
+       dev_t dev = sbi->sb->s_dev;
+       char name[36];
+
+       if (cache_thread->cache_wb_task)
+               return 0;
+
+       init_waitqueue_head(&cache_thread->cache_wb_wq);
+       cache_thread->cache_wb_interval = DEF_DIRTY_CACHE_TIMEOUT;
+       snprintf(name, sizeof(name), "f2fs_writeback-%u:%u",
+                       MAJOR(dev), MINOR(dev));
+
+       task = kthread_run(f2fs_cache_writeback_kthread, sbi, "%s", name);
+       if (IS_ERR(task))
+               return PTR_ERR(task);
+
+       cache_thread->cache_wb_task = task;
+       return 0;
+}
+
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi)
+{
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+
+       if (!cache_thread->cache_wb_task)
+               return;
+
+       kthread_stop(cache_thread->cache_wb_task);
+       cache_thread->cache_wb_task = NULL;
+}
fs/f2fs/cache.h +0 -0
index 5728e2225465..ea0cf9adf194 100644

fs/f2fs/cache.h void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi); · line 217

Quoted 9 lines
fs/f2fs/cache.h +6 -0
@@ -209,4 +209,16 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
  unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
                                 unsigned long nr_to_scan);
 
+#define DEF_DIRTY_CACHE_TIMEOUT 5000
+
+struct f2fs_cache_kthread {
+       struct task_struct *cache_wb_task;
+       wait_queue_head_t cache_wb_wq;
+       atomic_t cache_wb_trigger;

not used?
thanks!

Quoted 182 lines
fs/f2fs/cache.h +6 -0
+       unsigned int cache_wb_interval;
+};
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
+
  #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/f2fs.h +3 -0
index 85c4bf40b4aa..c0f82b63d732 100644
@@ -2110,6 +2110,9 @@ struct f2fs_sb_info {
 
         /* f2fs internal cache */
         struct f2fs_cached_block_list meta_blocks;
+
+       /* internal cache flush thread */
+       struct f2fs_cache_kthread cache_thread;
  };
 
  /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +31 -2
index b731b9bb7b54..516b4ee12564 100644
@@ -2039,6 +2039,7 @@ static void f2fs_put_super(struct super_block *sb)
          * flush all issued checkpoints and stop checkpoint issue thread.
          * after then, all checkpoints should be done by each process context.
          */
+       f2fs_stop_cache_wb_thread(sbi);
         f2fs_stop_ckpt_thread(sbi);
 
         /*
@@ -2845,6 +2846,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
         unsigned int flags = fc->sb_flags;
         int err;
         bool need_restart_gc = false, need_stop_gc = false;
+       bool need_restart_wb = false, need_stop_wb = false;
         bool need_restart_flush = false, need_stop_flush = false;
         bool need_restart_discard = false, need_stop_discard = false;
         bool need_enable_checkpoint = false, need_disable_checkpoint = false;
@@ -3004,6 +3006,18 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                 need_stop_gc = true;
         }
 
+       if (flags & SB_RDONLY) {
+               if (sbi->cache_thread.cache_wb_task) {
+                       f2fs_stop_cache_wb_thread(sbi);
+                       need_restart_wb = true;
+               }
+       } else if (!sbi->cache_thread.cache_wb_task) {
+               err = f2fs_start_cache_wb_thread(sbi);
+               if (err)
+                       goto restore_gc;
+               need_stop_wb = true;
+       }
+
         if (flags & SB_RDONLY) {
                 sync_inodes_sb(sb);
 
@@ -3011,7 +3025,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                 set_sbi_flag(sbi, SBI_IS_CLOSE);
                 err = f2fs_sync_fs(sb, 1);
                 if (err)
-                       goto restore_gc;
+                       goto restore_wb;
                 clear_sbi_flag(sbi, SBI_IS_CLOSE);
         }
 
@@ -3026,7 +3040,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
         } else {
                 err = f2fs_create_flush_cmd_control(sbi);
                 if (err)
-                       goto restore_gc;
+                       goto restore_wb;
                 need_stop_flush = true;
         }
 
@@ -3123,6 +3137,13 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                 clear_opt(sbi, FLUSH_MERGE);
                 f2fs_destroy_flush_cmd_control(sbi, false);
         }
+restore_wb:
+       if (need_restart_wb) {
+               if (f2fs_start_cache_wb_thread(sbi))
+                       f2fs_warn(sbi, "background cache writeback thread has stopped");
+       } else if (need_stop_wb) {
+               f2fs_stop_cache_wb_thread(sbi);
+       }
  restore_gc:
         if (need_restart_gc) {
                 if (f2fs_start_gc_thread(sbi))
@@ -5529,6 +5550,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
                         goto sync_free_meta;
         }
 
+       if (!f2fs_readonly(sb)) {
+               err = f2fs_start_cache_wb_thread(sbi);
+               if (err)
+                       goto stop_gc_thread;
+       }
+
         /* recover broken superblock */
         if (recovery) {
                 err = f2fs_commit_super(sbi, true);
@@ -5551,6 +5578,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
         sbi->umount_lock_holder = NULL;
         return 0;
 
+stop_gc_thread:
+       f2fs_stop_gc_thread(sbi);
  sync_free_meta:
         /* safe to flush all the data */
         sync_filesystem(sbi->sb);
fs/f2fs/sysfs.c +12 -2
index aaca9ed9b169..ad25733e32c0 100644
@@ -40,6 +40,7 @@ enum {
         RESERVED_BLOCKS,        /* struct f2fs_sb_info */
         CPRC_INFO,      /* struct ckpt_req_control */
         ATGC_INFO,      /* struct atgc_management */
+       WB_THREAD,      /* struct f2fs_cache_kthread */
  };
 
  static const char *gc_mode_names[MAX_GC_MODE] = {
@@ -98,6 +99,8 @@ static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
                 return (unsigned char *)&sbi->cprc_info;
         else if (struct_type == ATGC_INFO)
                 return (unsigned char *)&sbi->am;
+       else if (struct_type == WB_THREAD)
+               return (unsigned char *)&sbi->cache_thread;
         return NULL;
  }
 
@@ -1008,7 +1011,8 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
         bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
                                         a->struct_type == GC_THREAD);
         bool thread_entry = !strcmp(a->attr.name, "ckpt_thread_ioprio") ||
-                       !strcmp(a->attr.name, "critical_task_priority");
+                       !strcmp(a->attr.name, "critical_task_priority") ||
+                       !strcmp(a->attr.name, "cache_wb_interval");
 
         if (gc_entry || thread_entry) {
                 if (!down_read_trylock(&sbi->sb->s_umount))
@@ -1218,6 +1222,9 @@ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  #define ATGC_INFO_RW_ATTR(name, elname)                                \
         F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
 
+#define WB_THREAD_RW_ATTR(name, elname)                                \
+       F2FS_RW_ATTR(WB_THREAD, f2fs_cache_kthread, name, elname)
+
  /* GC_THREAD ATTR */
  GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
  GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
@@ -1347,6 +1354,9 @@ ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
  ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
  ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
 
+/* WB_THREAD ATTR */
+WB_THREAD_RW_ATTR(cache_wb_interval, cache_wb_interval);
+
  F2FS_GENERAL_RO_ATTR(dirty_segments);
  F2FS_GENERAL_RO_ATTR(free_segments);
  F2FS_GENERAL_RO_ATTR(ovp_segments);
@@ -1533,6 +1543,7 @@ static struct attribute *f2fs_attrs[] = {
         ATTR_LIST(lock_duration_priority),
         ATTR_LIST(adjust_lock_priority),
         ATTR_LIST(critical_task_priority),
+       ATTR_LIST(cache_wb_interval),
         NULL,
  };
  ATTRIBUTE_GROUPS(f2fs);
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#12 lore raw

Re: [f2fs-dev] [PATCH v7 04/12] f2fs: cache: introduce writeback thread

Quoted 131 lines · Chao Yu <[email protected]> 于2026年9月15日周二 10:18写道:

Zhiguo,

Thanks for the review, let fix them all.

Thanks,

Quoted 123 lines · Zhiguo Niu

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 42 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces a background writeback kthread (f2fs_writeback-x:y) to periodically flush dirty metadata cache entries with a default interval of 5 seconds.

It manages thread lifecycle across mount, unmount, and remount (rw/ro) transitions.

It introduces a sysfs entry /sys/fs/f2fs/<disk>/cache_wb_interval to control writeback interval.

---
  Documentation/ABI/testing/sysfs-fs-f2fs |  6 +++
  fs/f2fs/cache.c                         | 56 +++++++++++++++++++++++++
  fs/f2fs/cache.h                         | 12 ++++++
  fs/f2fs/f2fs.h                          |  3 ++
  fs/f2fs/super.c                         | 33 ++++++++++++++-
  fs/f2fs/sysfs.c                         | 13 +++++-
  6 files changed, 120 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-fs-f2fs +6 -0
index 0cebc89799dd..d2665b7f1f14 100644
@@ -1020,3 +1020,9 @@ Contact:  "Daeho Jeong" <[email protected]>
  Description:   This is a read-only entry to show the upper bound section number
                 for pinned files. Pinned files will only be allocated within
                 sections 0 to pinned_area_max_secno - 1.
+
+What:          /sys/fs/f2fs/<disk>/cache_wb_interval
+Date:          August 2026
+Contact:       "Chao Yu" <[email protected]>
+Description:   This is a writable entry to control writeback interval of
+               f2fs_writeback-x:y, by default the value is 5000, unit is ms.
fs/f2fs/cache.c +0 -0
index 563615761d11..404ceac9e6bf 100644

fs/f2fs/cache.c unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi, · line 629

Quoted 4 lines
fs/f2fs/cache.c +1 -0
@@ -626,3 +626,59 @@ unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
  {
         return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
  }
+

Hi Chao

fs/f2fs/cache.c unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi, · line 642

Quoted 13 lines
fs/f2fs/cache.c +13 -0
+static int f2fs_cache_writeback_kthread(void *data)
+{
+       struct f2fs_sb_info *sbi = data;
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+       wait_queue_head_t *wq = &cache_thread->cache_wb_wq;
+       unsigned int interval = cache_thread->cache_wb_interval;
+
+       set_freezable();
+
+       while (!kthread_should_stop()) {
+               wait_event_freezable_timeout(*wq,
+                               kthread_should_stop(),
+                               msecs_to_jiffies(interval));

if wb thread is running and user set cache_wb_interval at the same time, the wb thread can not get the new wb interval value for next round? because local varible is used here

Quoted 47 lines
fs/f2fs/cache.c +42 -0
+
+               if (kthread_should_stop())
+                       break;
+               if (f2fs_cp_error(sbi))
+                       continue;
+       }
+       return 0;
+}
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi)
+{
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+       struct task_struct *task;
+       dev_t dev = sbi->sb->s_dev;
+       char name[36];
+
+       if (cache_thread->cache_wb_task)
+               return 0;
+
+       init_waitqueue_head(&cache_thread->cache_wb_wq);
+       cache_thread->cache_wb_interval = DEF_DIRTY_CACHE_TIMEOUT;
+       snprintf(name, sizeof(name), "f2fs_writeback-%u:%u",
+                       MAJOR(dev), MINOR(dev));
+
+       task = kthread_run(f2fs_cache_writeback_kthread, sbi, "%s", name);
+       if (IS_ERR(task))
+               return PTR_ERR(task);
+
+       cache_thread->cache_wb_task = task;
+       return 0;
+}
+
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi)
+{
+       struct f2fs_cache_kthread *cache_thread = &sbi->cache_thread;
+
+       if (!cache_thread->cache_wb_task)
+               return;
+
+       kthread_stop(cache_thread->cache_wb_task);
+       cache_thread->cache_wb_task = NULL;
+}
fs/f2fs/cache.h +0 -0
index 5728e2225465..ea0cf9adf194 100644

fs/f2fs/cache.h void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi); · line 217

Quoted 9 lines
fs/f2fs/cache.h +6 -0
@@ -209,4 +209,16 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
  unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
                                 unsigned long nr_to_scan);
 
+#define DEF_DIRTY_CACHE_TIMEOUT 5000
+
+struct f2fs_cache_kthread {
+       struct task_struct *cache_wb_task;
+       wait_queue_head_t cache_wb_wq;
+       atomic_t cache_wb_trigger;

not used?
thanks!

Quoted 5 lines
Quoted 5 lines

fs/f2fs/cache.h void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi); · line 222

Quoted 5 lines
fs/f2fs/cache.h +5 -0
+       unsigned int cache_wb_interval;
+};
+
+int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
+void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);

hi Chao,
Another minor issue: seems these 2 function has been declared twice. thanks!

Quoted 178 lines
Quoted 177 lines
Quoted 177 lines
fs/f2fs/cache.h +1 -0
+
  #endif /* _LINUX_F2FS_CACHE_H */
fs/f2fs/f2fs.h +3 -0
index 85c4bf40b4aa..c0f82b63d732 100644
@@ -2110,6 +2110,9 @@ struct f2fs_sb_info {
 
         /* f2fs internal cache */
         struct f2fs_cached_block_list meta_blocks;
+
+       /* internal cache flush thread */
+       struct f2fs_cache_kthread cache_thread;
  };
 
  /* Definitions to access f2fs_sb_info */
fs/f2fs/super.c +31 -2
index b731b9bb7b54..516b4ee12564 100644
@@ -2039,6 +2039,7 @@ static void f2fs_put_super(struct super_block *sb)
          * flush all issued checkpoints and stop checkpoint issue thread.
          * after then, all checkpoints should be done by each process context.
          */
+       f2fs_stop_cache_wb_thread(sbi);
         f2fs_stop_ckpt_thread(sbi);
 
         /*
@@ -2845,6 +2846,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
         unsigned int flags = fc->sb_flags;
         int err;
         bool need_restart_gc = false, need_stop_gc = false;
+       bool need_restart_wb = false, need_stop_wb = false;
         bool need_restart_flush = false, need_stop_flush = false;
         bool need_restart_discard = false, need_stop_discard = false;
         bool need_enable_checkpoint = false, need_disable_checkpoint = false;
@@ -3004,6 +3006,18 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                 need_stop_gc = true;
         }
 
+       if (flags & SB_RDONLY) {
+               if (sbi->cache_thread.cache_wb_task) {
+                       f2fs_stop_cache_wb_thread(sbi);
+                       need_restart_wb = true;
+               }
+       } else if (!sbi->cache_thread.cache_wb_task) {
+               err = f2fs_start_cache_wb_thread(sbi);
+               if (err)
+                       goto restore_gc;
+               need_stop_wb = true;
+       }
+
         if (flags & SB_RDONLY) {
                 sync_inodes_sb(sb);
 
@@ -3011,7 +3025,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                 set_sbi_flag(sbi, SBI_IS_CLOSE);
                 err = f2fs_sync_fs(sb, 1);
                 if (err)
-                       goto restore_gc;
+                       goto restore_wb;
                 clear_sbi_flag(sbi, SBI_IS_CLOSE);
         }
 
@@ -3026,7 +3040,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
         } else {
                 err = f2fs_create_flush_cmd_control(sbi);
                 if (err)
-                       goto restore_gc;
+                       goto restore_wb;
                 need_stop_flush = true;
         }
 
@@ -3123,6 +3137,13 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
                 clear_opt(sbi, FLUSH_MERGE);
                 f2fs_destroy_flush_cmd_control(sbi, false);
         }
+restore_wb:
+       if (need_restart_wb) {
+               if (f2fs_start_cache_wb_thread(sbi))
+                       f2fs_warn(sbi, "background cache writeback thread has stopped");
+       } else if (need_stop_wb) {
+               f2fs_stop_cache_wb_thread(sbi);
+       }
  restore_gc:
         if (need_restart_gc) {
                 if (f2fs_start_gc_thread(sbi))
@@ -5529,6 +5550,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
                         goto sync_free_meta;
         }
 
+       if (!f2fs_readonly(sb)) {
+               err = f2fs_start_cache_wb_thread(sbi);
+               if (err)
+                       goto stop_gc_thread;
+       }
+
         /* recover broken superblock */
         if (recovery) {
                 err = f2fs_commit_super(sbi, true);
@@ -5551,6 +5578,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
         sbi->umount_lock_holder = NULL;
         return 0;
 
+stop_gc_thread:
+       f2fs_stop_gc_thread(sbi);
  sync_free_meta:
         /* safe to flush all the data */
         sync_filesystem(sbi->sb);
fs/f2fs/sysfs.c +12 -2
index aaca9ed9b169..ad25733e32c0 100644
@@ -40,6 +40,7 @@ enum {
         RESERVED_BLOCKS,        /* struct f2fs_sb_info */
         CPRC_INFO,      /* struct ckpt_req_control */
         ATGC_INFO,      /* struct atgc_management */
+       WB_THREAD,      /* struct f2fs_cache_kthread */
  };
 
  static const char *gc_mode_names[MAX_GC_MODE] = {
@@ -98,6 +99,8 @@ static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
                 return (unsigned char *)&sbi->cprc_info;
         else if (struct_type == ATGC_INFO)
                 return (unsigned char *)&sbi->am;
+       else if (struct_type == WB_THREAD)
+               return (unsigned char *)&sbi->cache_thread;
         return NULL;
  }
 
@@ -1008,7 +1011,8 @@ static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
         bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
                                         a->struct_type == GC_THREAD);
         bool thread_entry = !strcmp(a->attr.name, "ckpt_thread_ioprio") ||
-                       !strcmp(a->attr.name, "critical_task_priority");
+                       !strcmp(a->attr.name, "critical_task_priority") ||
+                       !strcmp(a->attr.name, "cache_wb_interval");
 
         if (gc_entry || thread_entry) {
                 if (!down_read_trylock(&sbi->sb->s_umount))
@@ -1218,6 +1222,9 @@ static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  #define ATGC_INFO_RW_ATTR(name, elname)                                \
         F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
 
+#define WB_THREAD_RW_ATTR(name, elname)                                \
+       F2FS_RW_ATTR(WB_THREAD, f2fs_cache_kthread, name, elname)
+
  /* GC_THREAD ATTR */
  GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
  GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
@@ -1347,6 +1354,9 @@ ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
  ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
  ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
 
+/* WB_THREAD ATTR */
+WB_THREAD_RW_ATTR(cache_wb_interval, cache_wb_interval);
+
  F2FS_GENERAL_RO_ATTR(dirty_segments);
  F2FS_GENERAL_RO_ATTR(free_segments);
  F2FS_GENERAL_RO_ATTR(ovp_segments);
@@ -1533,6 +1543,7 @@ static struct attribute *f2fs_attrs[] = {
         ATTR_LIST(lock_duration_priority),
         ATTR_LIST(adjust_lock_priority),
         ATTR_LIST(critical_task_priority),
+       ATTR_LIST(cache_wb_interval),
         NULL,
  };
  ATTRIBUTE_GROUPS(f2fs);
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#13 lore raw

PATCH v7 5/12 f2fs: cache: use meta cache

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch migrates F2FS meta block caching from the fake VFS inode page cache (sbi->meta_inode) to meta cache (sbi->meta_blocks).

It converts CP, SIT, NAT, SSA, recovery, and GC metadata I/O paths to operate on struct f2fs_cached_block instead of folio, and removes sbi->meta_inode.

---
 fs/f2fs/cache.c         |   2 +
 fs/f2fs/cache.h         |   2 +-
 fs/f2fs/checkpoint.c    | 417 ++++++++++++++++++----------------------
 fs/f2fs/compress.c      |   4 +-
 fs/f2fs/data.c          |  14 +-
 fs/f2fs/debug.c         |  12 +-
 fs/f2fs/f2fs.h          |  90 +++------
 fs/f2fs/file.c          |   4 +-
 fs/f2fs/gc.c            | 135 +++++++------
 fs/f2fs/inline.c        |   9 +-
 fs/f2fs/inode.c         |   9 +-
 fs/f2fs/node.c          | 157 ++++++++-------
 fs/f2fs/node.h          |   8 +-
 fs/f2fs/recovery.c      | 196 +++++++++----------
 fs/f2fs/segment.c       | 243 +++++++++++------------
 fs/f2fs/segment.h       |  29 ++-
 fs/f2fs/super.c         |  33 +---
 fs/f2fs/sysfs.c         |   2 +-
 include/linux/f2fs_fs.h |   1 -
 19 files changed, 652 insertions(+), 715 deletions(-)
fs/f2fs/cache.c +2 -0
index 404ceac9e6bf..e99165e1c785 100644
@@ -645,6 +645,8 @@ static int f2fs_cache_writeback_kthread(void *data)
 			break;
 		if (f2fs_cp_error(sbi))
 			continue;
+
+		f2fs_write_meta_caches(sbi);
 	}
 	return 0;
 }
fs/f2fs/cache.h +1 -1
index ea0cf9adf194..a0218c235c39 100644
@@ -200,7 +200,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 #define META_CACHE(sbi)		(&(sbi)->meta_blocks)
 
 #define f2fs_find_meta_cache(sbi, blkaddr)		\
-	f2fs_find_cache(META_CACHE(sbi), blkaddr)
+	f2fs_find_cache(META_CACHE(sbi), blkaddr, 0)
 #define f2fs_invalidate_meta_caches(sbi, start, len)	\
 	f2fs_drop_cache_range(META_CACHE(sbi), start, len, false)
 #define f2fs_truncate_meta_caches(sbi, start, len)	\
fs/f2fs/checkpoint.c +191 -226

840 lines not on this page · raw message

fs/f2fs/compress.c +2 -2
index ce88092d9ce2..991b7738c645 100644
@@ -1150,7 +1150,7 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
 		f2fs_compress_ctx_add_page(cc, folio);
 
 		if (!folio_test_uptodate(folio)) {
-			f2fs_handle_page_eio(sbi, folio, DATA);
+			f2fs_handle_page_eio(sbi, folio->index, DATA);
 release_and_retry:
 			f2fs_put_rpages(cc);
 			f2fs_unlock_rpages(cc, i + 1);
@@ -1357,7 +1357,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_folio,
 						dn.ofs_in_node + i + 1);
 
-		/* wait for GCed page writeback via META_MAPPING */
+		/* wait for GCed page writeback via generic cache */
 		f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
 	}
 
fs/f2fs/data.c +4 -10
index 59fef2132177..674d1b8e33d8 100644
@@ -63,8 +63,7 @@ bool f2fs_is_cp_guaranteed(const struct folio *folio)
 	inode = mapping->host;
 	sbi = F2FS_I_SB(inode);
 
-	if (inode->i_ino == F2FS_META_INO(sbi) ||
-			inode->i_ino == F2FS_NODE_INO(sbi) ||
+	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
 			S_ISDIR(inode->i_mode))
 		return true;
 
@@ -82,9 +81,6 @@ static enum count_type __read_io_type(struct folio *folio)
 		struct inode *inode = mapping->host;
 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 
-		if (inode->i_ino == F2FS_META_INO(sbi))
-			return F2FS_RD_META;
-
 		if (inode->i_ino == F2FS_NODE_INO(sbi))
 			return F2FS_RD_NODE;
 	}
@@ -1418,7 +1414,7 @@ static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
 	bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, folio->index,
 				 for_write);
 
-	/* wait for GCed page writeback via META_MAPPING */
+	/* wait for GCed page writeback via generic cache */
 	f2fs_wait_on_block_writeback(inode, blkaddr);
 
 	if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
@@ -3241,7 +3237,7 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
 		goto out_writepage;
 	}
 
-	/* wait for GCed page writeback via META_MAPPING */
+	/* wait for GCed page writeback via generic cache */
 	if (fio->meta_gc)
 		f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
 
@@ -4314,9 +4310,7 @@ void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
 		return;
 
 	if (folio_test_dirty(folio)) {
-		if (inode->i_ino == F2FS_META_INO(sbi)) {
-			dec_page_count(sbi, F2FS_DIRTY_META);
-		} else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
+		if (inode->i_ino == F2FS_NODE_INO(sbi)) {
 			dec_page_count(sbi, F2FS_DIRTY_NODES);
 		} else {
 			inode_dec_dirty_pages(inode);
fs/f2fs/debug.c +4 -8
index a6cfceafe2fe..24819d4c0d55 100644
@@ -224,8 +224,7 @@ static void update_general_status(struct f2fs_sb_info *sbi)
 	si->dirty_count = dirty_segments(sbi);
 	if (sbi->node_inode)
 		si->node_pages = NODE_MAPPING(sbi)->nrpages;
-	if (sbi->meta_inode)
-		si->meta_pages = META_MAPPING(sbi)->nrpages;
+	si->meta_caches = META_CACHE(sbi)->num_entries;
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 	if (sbi->compress_inode) {
 		si->compress_pages = COMPRESS_MAPPING(sbi)->nrpages;
@@ -388,11 +387,8 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
 
 		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
 	}
-	if (sbi->meta_inode) {
-		unsigned long npages = META_MAPPING(sbi)->nrpages;
-
-		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
-	}
+	si->page_mem += (unsigned long long)META_CACHE(sbi)->num_entries << PAGE_SHIFT;
+	si->cache_mem += META_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 	if (sbi->compress_inode) {
 		unsigned long npages = COMPRESS_MAPPING(sbi)->nrpages;
@@ -708,7 +704,7 @@ static int stat_show(struct seq_file *s, void *v)
 		seq_printf(s, "  - quota data: %4d in quota files:%4d\n",
 			   si->ndirty_qdata, si->nquota_files);
 		seq_printf(s, "  - meta: %4d in %4d\n",
-			   si->ndirty_meta, si->meta_pages);
+			   si->ndirty_meta, si->meta_caches);
 		seq_printf(s, "  - imeta: %4d\n",
 			   si->ndirty_imeta);
 		seq_printf(s, "  - fsync mark: %4lld\n",
fs/f2fs/f2fs.h +27 -63

198 lines not on this page · raw message

fs/f2fs/file.c +2 -2
index b0bdc7a977b9..7c2bfa0e63d2 100644
@@ -215,7 +215,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
 
 	f2fs_folio_wait_writeback(folio, DATA, false, true);
 
-	/* wait for GCed page writeback via META_MAPPING */
+	/* wait for GCed page writeback via generic cache */
 	f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
 
 	/*
@@ -2567,7 +2567,7 @@ int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
 		break;
 	case F2FS_GOING_DOWN_METAFLUSH:
-		f2fs_sync_meta_pages(sbi, LONG_MAX, FS_META_IO);
+		f2fs_sync_meta_caches(sbi, LONG_MAX, true, FS_META_IO);
 		f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
 		break;
 	case F2FS_GOING_DOWN_NEED_FSCK:
fs/f2fs/gc.c +74 -61

292 lines not on this page · raw message

fs/f2fs/inline.c +5 -4
index 925bdaad013c..d10acb5042e3 100644
@@ -293,7 +293,7 @@ int f2fs_write_inline_data(struct inode *inode, struct folio *folio)
 	return 0;
 }
 
-int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio)
+int f2fs_recover_inline_data(struct inode *inode, struct f2fs_cached_block *entry)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	struct f2fs_inode *ri = NULL;
@@ -307,12 +307,13 @@ int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio)
 	 *    x       o  -> remove data blocks, and then recover inline_data
 	 *    x       x  -> recover data blocks
 	 */
-	if (IS_INODE(F2FS_I_SB(inode), nfolio))
-		ri = F2FS_INODE(nfolio);
+	if (IS_INODE(sbi, cache_folio(entry)))
+		ri = &CACHED_NODE(entry)->i;
 
 	if (f2fs_has_inline_data(inode) &&
 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
 		struct folio *ifolio;
+
 process_inline:
 		ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
 		if (IS_ERR(ifolio))
@@ -320,7 +321,7 @@ int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio)
 
 		f2fs_folio_wait_writeback(ifolio, NODE, true, true);
 
-		src_addr = inline_data_addr(inode, nfolio);
+		src_addr = inline_data_addr(inode, cache_folio(entry));
 		dst_addr = inline_data_addr(inode, ifolio);
 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
 
fs/f2fs/inode.c +2 -7
index 24205a35be04..f2e1d8b50f8c 100644
@@ -578,7 +578,7 @@ static int do_read_inode(struct inode *inode)
 
 static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
 {
-	if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
+	if (ino == F2FS_NODE_INO(sbi))
 		return true;
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 	if (test_opt(sbi, COMPRESS_CACHE) && ino == F2FS_COMPRESS_INO(sbi))
@@ -625,9 +625,6 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
 	if (ino == F2FS_NODE_INO(sbi)) {
 		inode->i_mapping->a_ops = &f2fs_node_aops;
 		mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
-	} else if (ino == F2FS_META_INO(sbi)) {
-		inode->i_mapping->a_ops = &f2fs_meta_aops;
-		mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
 	} else if (ino == F2FS_COMPRESS_INO(sbi)) {
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 		inode->i_mapping->a_ops = &f2fs_compress_aops;
@@ -827,8 +824,7 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 
-	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-			inode->i_ino == F2FS_META_INO(sbi))
+	if (inode->i_ino == F2FS_NODE_INO(sbi))
 		return 0;
 
 	/*
@@ -922,7 +918,6 @@ static bool f2fs_pre_evict_inode(struct inode *inode)
 		f2fs_invalidate_compress_pages(sbi, inode->i_ino);
 
 	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-	    inode->i_ino == F2FS_META_INO(sbi) ||
 	    inode->i_ino == F2FS_COMPRESS_INO(sbi))
 		return true;
 
fs/f2fs/node.c +78 -79

363 lines not on this page · raw message

fs/f2fs/node.h +4 -4
index 901b9e60fed5..e37fb0d63120 100644
@@ -17,15 +17,15 @@ static inline nid_t f2fs_start_nid(struct f2fs_sb_info *sbi, nid_t nid)
 #define	NAT_BLOCK_OFFSET(sbi, start_nid) \
 	((start_nid) / NAT_ENTRY_PER_BLOCK(sbi))
 
-/* # of pages to perform synchronous readahead before building free nids */
-#define FREE_NID_PAGES	8
+/* # of blocks to perform synchronous readahead before building free nids */
+#define FREE_NID_BLOCKS	8
 #define MAX_FREE_NIDS(sbi)	((unsigned long)NAT_ENTRY_PER_BLOCK(sbi) * \
-				 FREE_NID_PAGES)
+				 FREE_NID_BLOCKS)
 
 /* size of free nid batch when shrinking */
 #define SHRINK_NID_BATCH_SIZE	8
 
-#define DEF_RA_NID_PAGES	0	/* # of nid pages to be readaheaded */
+#define DEF_RA_NID_BLOCKS	0	/* # of nid blocks to be readaheaded */
 
 /* maximum readahead size for node during getting data blocks */
 #define MAX_RA_NODE		128
fs/f2fs/recovery.c +98 -98

427 lines not on this page · raw message

fs/f2fs/segment.c +123 -120

538 lines not on this page · raw message

fs/f2fs/segment.h +26 -3
index cc0428b614da..3083069c9092 100644
@@ -96,6 +96,8 @@ static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
 #define GET_SUM_BLKOFF(sbi, segno) (segno % (sbi)->sums_per_block)
 #define SUM_BLK_PAGE_ADDR(sbi, folio, segno)	\
 	(folio_address(folio) + GET_SUM_BLKOFF(sbi, segno) * (sbi)->sum_blocksize)
+#define SUM_BLK_ENTRY_ADDR(sbi, entry, segno)	\
+	(cache_address(entry) + GET_SUM_BLKOFF(sbi, segno) * (sbi)->sum_blocksize)
 
 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
@@ -421,8 +423,8 @@ static inline void __seg_info_to_raw_sit(struct seg_entry *se,
 	rs->mtime = cpu_to_le64(se->mtime);
 }
 
-static inline void seg_info_to_sit_folio(struct f2fs_sb_info *sbi,
-				struct folio *folio, unsigned int start)
+static inline void seg_info_to_sit_block(struct f2fs_sb_info *sbi,
+			struct f2fs_cached_block *entry, unsigned int start)
 {
 	struct f2fs_sit_block *raw_sit;
 	struct seg_entry *se;
@@ -431,7 +433,7 @@ static inline void seg_info_to_sit_folio(struct f2fs_sb_info *sbi,
 					(unsigned long)MAIN_SEGS(sbi));
 	int i;
 
-	raw_sit = folio_address(folio);
+	raw_sit = cache_address(entry);
 	memset(raw_sit, 0, F2FS_BLKSIZE(sbi));
 	for (i = 0; i < end - start; i++) {
 		rs = &raw_sit->entries[i];
@@ -996,6 +998,27 @@ static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
 		return 0;
 }
 
+/*
+ * When writing cache asynchronously, align nr_to_write to BIO_MAX_VECS.
+ */
+static inline long adjust_flush_cache_number(struct f2fs_sb_info *sbi, int type)
+{
+	long nr_to_write;
+
+	switch (type) {
+	case META:
+		nr_to_write = BIO_MAX_VECS;
+		break;
+	case NODE:
+		nr_to_write = BIO_MAX_VECS << 1;
+		break;
+	default:
+		f2fs_bug_on(sbi, 1);
+		return 0;
+	}
+	return nr_to_write;
+}
+
 /*
  * When writing pages, it'd better align nr_to_write for segment size.
  */
fs/f2fs/super.c +8 -25
index 516b4ee12564..0361e4cc0e0a 100644
@@ -1880,8 +1880,7 @@ static int f2fs_drop_inode(struct inode *inode)
 	 * drop useless meta/node dirty pages.
 	 */
 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
-		if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-			inode->i_ino == F2FS_META_INO(sbi)) {
+		if (inode->i_ino == F2FS_NODE_INO(sbi)) {
 			trace_f2fs_drop_inode(inode, 1);
 			return 1;
 		}
@@ -1983,8 +1982,7 @@ static void f2fs_dirty_inode(struct inode *inode, int flags)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 
-	if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-			inode->i_ino == F2FS_META_INO(sbi))
+	if (inode->i_ino == F2FS_NODE_INO(sbi))
 		return;
 
 	if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
@@ -2078,11 +2076,12 @@ static void f2fs_put_super(struct super_block *sb)
 	/* our cp_error case, we can wait for any writeback page */
 	f2fs_flush_merged_writes(sbi);
 
-	f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
+	f2fs_sync_dirty_data(sbi, F2FS_WB_CP_DATA);
 
-	if (err || f2fs_cp_error(sbi)) {
+	if (err || f2fs_cp_error(sbi) ||
+		unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
 		truncate_inode_pages_final(NODE_MAPPING(sbi));
-		truncate_inode_pages_final(META_MAPPING(sbi));
+		f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX);
 	}
 
 	f2fs_bug_on(sbi, sbi->fsync_node_num);
@@ -2092,9 +2091,6 @@ static void f2fs_put_super(struct super_block *sb)
 	iput(sbi->node_inode);
 	sbi->node_inode = NULL;
 
-	iput(sbi->meta_inode);
-	sbi->meta_inode = NULL;
-
 	f2fs_destroy_cache(META_CACHE(sbi));
 
 	/* Should check the page counts after dropping all node/meta pages */
@@ -4449,7 +4445,6 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
 	sbi->allocate_section_policy = ALLOCATE_FORWARD_NOHINT;
 	F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
 	F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
-	F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
 	sbi->cur_victim_sec = NULL_SECNO;
 	sbi->gc_mode = GC_NORMAL;
 	sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
@@ -5290,18 +5285,10 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (err)
 		goto free_page_array_cache;
 
-	/* get an inode for meta space */
-	sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
-	if (IS_ERR(sbi->meta_inode)) {
-		f2fs_err(sbi, "Failed to read F2FS meta data inode");
-		err = PTR_ERR(sbi->meta_inode);
-		goto free_meta_cache;
-	}
-
 	err = f2fs_get_valid_checkpoint(sbi);
 	if (err) {
 		f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
-		goto free_meta_inode;
+		goto free_meta_cache;
 	}
 
 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
@@ -5597,7 +5584,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	 * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
 	 * falls into an infinite loop in f2fs_sync_meta_pages().
 	 */
-	truncate_inode_pages_final(META_MAPPING(sbi));
+	f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX);
 	/* evict some inodes being cached by GC */
 	evict_inodes(sb);
 	f2fs_unregister_sysfs(sbi);
@@ -5627,10 +5614,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 free_devices:
 	destroy_device_list(sbi);
 	kvfree(sbi->ckpt);
-free_meta_inode:
-	make_bad_inode(sbi->meta_inode);
-	iput(sbi->meta_inode);
-	sbi->meta_inode = NULL;
 free_meta_cache:
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

fs/f2fs/sysfs.c +1 -1
index ad25733e32c0..cd7ed210cad5 100644

Lines of this file are not shown. View the raw message.

include/linux/f2fs_fs.h +0 -1
index f38558279e86..c325904f461e 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.49.0

Part of the diff is not shown here. View the raw message.

Reviewed-by on v7 5/12
#14 lore raw

Re: [f2fs-dev] [PATCH v7 05/12] f2fs: cache: use meta cache

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 11 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch migrates F2FS meta block caching from the fake VFS inode page cache (sbi->meta_inode) to meta cache (sbi->meta_blocks).

It converts CP, SIT, NAT, SSA, recovery, and GC metadata I/O paths to operate on struct f2fs_cached_block instead of folio, and removes sbi->meta_inode.

Thanks!

Quoted 3,304 lines
---
 fs/f2fs/cache.c         |   2 +
 fs/f2fs/cache.h         |   2 +-
 fs/f2fs/checkpoint.c    | 417 ++++++++++++++++++----------------------
 fs/f2fs/compress.c      |   4 +-
 fs/f2fs/data.c          |  14 +-
 fs/f2fs/debug.c         |  12 +-
 fs/f2fs/f2fs.h          |  90 +++------
 fs/f2fs/file.c          |   4 +-
 fs/f2fs/gc.c            | 135 +++++++------
 fs/f2fs/inline.c        |   9 +-
 fs/f2fs/inode.c         |   9 +-
 fs/f2fs/node.c          | 157 ++++++++-------
 fs/f2fs/node.h          |   8 +-
 fs/f2fs/recovery.c      | 196 +++++++++----------
 fs/f2fs/segment.c       | 243 +++++++++++------------
 fs/f2fs/segment.h       |  29 ++-
 fs/f2fs/super.c         |  33 +---
 fs/f2fs/sysfs.c         |   2 +-
 include/linux/f2fs_fs.h |   1 -
 19 files changed, 652 insertions(+), 715 deletions(-)
fs/f2fs/cache.c +2 -0
index 404ceac9e6bf..e99165e1c785 100644
@@ -645,6 +645,8 @@ static int f2fs_cache_writeback_kthread(void *data)
                        break;
                if (f2fs_cp_error(sbi))
                        continue;
+
+               f2fs_write_meta_caches(sbi);
        }
        return 0;
 }
fs/f2fs/cache.h +1 -1
index ea0cf9adf194..a0218c235c39 100644
@@ -200,7 +200,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 #define META_CACHE(sbi)                (&(sbi)->meta_blocks)
 
 #define f2fs_find_meta_cache(sbi, blkaddr)             \
-       f2fs_find_cache(META_CACHE(sbi), blkaddr)
+       f2fs_find_cache(META_CACHE(sbi), blkaddr, 0)
 #define f2fs_invalidate_meta_caches(sbi, start, len)   \
        f2fs_drop_cache_range(META_CACHE(sbi), start, len, false)
 #define f2fs_truncate_meta_caches(sbi, start, len)     \
fs/f2fs/checkpoint.c +191 -226
index e95cd2118750..e3d14a6c136c 100644
@@ -7,7 +7,6 @@
  */
 #include <linux/fs.h>
 #include <linux/bio.h>
-#include <linux/mpage.h>
 #include <linux/writeback.h>
 #include <linux/blkdev.h>
 #include <linux/f2fs_fs.h>
@@ -17,6 +16,7 @@
 #include <linux/delayacct.h>
 #include <linux/ioprio.h>
 #include <linux/math64.h>
+#include <linux/freezer.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -234,29 +234,29 @@ static struct kmem_cache *ino_entry_slab;
 struct kmem_cache *f2fs_inode_entry_slab;
 
 /*
- * We guarantee no failure on the returned page.
+ * We guarantee no failure on the returned cache.
  */
-struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index)
+struct f2fs_cached_block *f2fs_grab_meta_cache(struct f2fs_sb_info *sbi,
+                                                       pgoff_t index)
 {
-       struct address_space *mapping = META_MAPPING(sbi);
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
 repeat:
-       folio = f2fs_grab_cache_folio(mapping, index, false);
-       if (IS_ERR(folio)) {
+       entry = f2fs_grab_cache(META_CACHE(sbi), index,
+                                       F2FS_CACHE_LOCK_CREATE);
+       if (IS_ERR(entry)) {
                cond_resched();
                goto repeat;
        }
-       f2fs_folio_wait_writeback(folio, META, true, true);
-       if (!folio_test_uptodate(folio))
-               folio_mark_uptodate(folio);
-       return folio;
+       f2fs_cache_wait_writeback(entry);
+       if (!f2fs_cache_test_uptodate(entry))
+               f2fs_cache_set_uptodate(entry);
+       return entry;
 }
 
-static struct folio *__get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index,
-                                                       bool is_meta)
+static struct f2fs_cached_block *__get_meta_cache(struct f2fs_sb_info *sbi,
+                                       pgoff_t index, bool is_meta)
 {
-       struct address_space *mapping = META_MAPPING(sbi);
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
        struct f2fs_io_info fio = {
                .sbi = sbi,
                .type = META,
@@ -266,70 +266,75 @@ static struct folio *__get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index,
                .new_blkaddr = index,
                .encrypted_page = NULL,
                .is_por = !is_meta ? 1 : 0,
+               .is_cache = 1,
        };
        int err;
 
        if (unlikely(!is_meta))
                fio.op_flags &= ~REQ_META;
 repeat:
-       folio = f2fs_grab_cache_folio(mapping, index, false);
-       if (IS_ERR(folio)) {
+       entry = f2fs_grab_cache(META_CACHE(sbi), index,
+                                       F2FS_CACHE_LOCK_CREATE);
+       if (IS_ERR(entry)) {
                cond_resched();
                goto repeat;
        }
-       if (folio_test_uptodate(folio))
+       if (f2fs_cache_test_uptodate(entry))
                goto out;
 
-       fio.folio = folio;
+       fio.cache_entry = entry;
 
-       err = f2fs_submit_page_bio(&fio);
+       err = f2fs_submit_cache_read(&fio);
        if (err) {
-               f2fs_folio_put(folio, true);
+               f2fs_put_cache(entry, true);
                return ERR_PTR(err);
        }
 
        f2fs_update_iostat(sbi, NULL, FS_META_READ_IO, F2FS_BLKSIZE(sbi));
 
-       folio_lock(folio);
-       if (unlikely(!is_meta_folio(sbi, folio))) {
-               f2fs_folio_put(folio, true);
+       f2fs_lock_cache(entry);
+       if (unlikely(!f2fs_is_meta_cache(entry))) {
+               f2fs_put_cache(entry, true);
                goto repeat;
        }
 
-       if (unlikely(!folio_test_uptodate(folio))) {
-               f2fs_handle_page_eio(sbi, folio, META);
-               f2fs_folio_put(folio, true);
+       if (unlikely(!f2fs_cache_test_uptodate(entry))) {
+               f2fs_handle_page_eio(sbi, entry->index, META);
+               f2fs_put_cache(entry, true);
                return ERR_PTR(-EIO);
        }
 out:
-       return folio;
+       return entry;
 }
 
-struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index)
+struct f2fs_cached_block *f2fs_get_meta_cache(struct f2fs_sb_info *sbi,
+                                                       pgoff_t index)
 {
-       return __get_meta_folio(sbi, index, true);
+       return __get_meta_cache(sbi, index, true);
 }
 
-struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index)
+struct f2fs_cached_block *f2fs_get_meta_cache_retry(struct f2fs_sb_info *sbi,
+                                                       pgoff_t index)
 {
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
        int count = 0;
 
 retry:
-       folio = __get_meta_folio(sbi, index, true);
-       if (IS_ERR(folio)) {
-               if (PTR_ERR(folio) == -EIO &&
+       entry = __get_meta_cache(sbi, index, true);
+       if (IS_ERR(entry)) {
+               if (PTR_ERR(entry) == -EIO &&
                                ++count <= DEFAULT_RETRY_IO_COUNT)
                        goto retry;
                f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_META_PAGE);
        }
-       return folio;
+       return entry;
 }
 
 /* for POR only */
-struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index)
+struct f2fs_cached_block *f2fs_get_tmp_cache(struct f2fs_sb_info *sbi,
+                                                       pgoff_t index)
 {
-       return __get_meta_folio(sbi, index, false);
+       return __get_meta_cache(sbi, index, false);
 }
 
 static bool __is_bitmap_valid(struct f2fs_sb_info *sbi, block_t blkaddr,
@@ -445,11 +450,12 @@ bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi,
 }
 
 /*
- * Readahead CP/NAT/SIT/SSA/POR pages
+ * Readahead CP/NAT/SIT/SSA/POR blocks
  */
-int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
-                                                       int type, bool sync)
+int f2fs_ra_meta_caches(struct f2fs_sb_info *sbi, block_t start,
+                               int nrblocks, int type, bool sync)
 {
+       struct f2fs_cached_block_list *cache = META_CACHE(sbi);
        block_t blkno = start;
        struct f2fs_io_info fio = {
                .sbi = sbi,
@@ -459,6 +465,7 @@ int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
                .encrypted_page = NULL,
                .in_list = 0,
                .is_por = (type == META_POR) ? 1 : 0,
+               .is_cache = 1,
        };
        struct blk_plug plug;
        int err;
@@ -467,8 +474,8 @@ int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
                fio.op_flags &= ~REQ_META;
 
        blk_start_plug(&plug);
-       for (; nrpages-- > 0; blkno++) {
-               struct folio *folio;
+       for (; nrblocks-- > 0; blkno++) {
+               struct f2fs_cached_block *entry;
 
                if (!f2fs_is_valid_blkaddr(sbi, blkno, type))
                        goto out;
@@ -495,21 +502,21 @@ int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
                        fio.new_blkaddr = blkno;
                        break;
                default:
-                       BUG();
+                       f2fs_bug_on(sbi, 1);
                }
 
-               folio = f2fs_grab_cache_folio(META_MAPPING(sbi),
-                                               fio.new_blkaddr, false);
-               if (IS_ERR(folio))
+               entry = f2fs_grab_cache(cache, fio.new_blkaddr,
+                                               F2FS_CACHE_LOCK_CREATE);
+               if (IS_ERR(entry))
                        continue;
-               if (folio_test_uptodate(folio)) {
-                       f2fs_folio_put(folio, true);
+               if (f2fs_cache_test_uptodate(entry)) {
+                       f2fs_put_cache(entry, true);
                        continue;
                }
 
-               fio.folio = folio;
-               err = f2fs_submit_page_bio(&fio);
-               f2fs_folio_put(folio, err ? true : false);
+               fio.cache_entry = entry;
+               err = f2fs_submit_cache_read(&fio);
+               f2fs_put_cache(entry, err ? true : false);
 
                if (!err)
                        f2fs_update_iostat(sbi, NULL, FS_META_READ_IO,
@@ -520,37 +527,36 @@ int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
        return blkno - start;
 }
 
-void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
-                                                       unsigned int ra_blocks)
+void f2fs_ra_meta_caches_cond(struct f2fs_sb_info *sbi, pgoff_t index,
+                                               unsigned int ra_blocks)
 {
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
        bool readahead = false;
 
        if (ra_blocks == RECOVERY_MIN_RA_BLOCKS)
                return;
 
-       folio = filemap_get_folio(META_MAPPING(sbi), index);
-       if (IS_ERR(folio) || !folio_test_uptodate(folio))
+       entry = f2fs_find_cache(META_CACHE(sbi), index, 0);
+       if (IS_ERR(entry) || !f2fs_cache_test_uptodate(entry))
                readahead = true;
-       f2fs_folio_put(folio, false);
+       f2fs_put_cache(entry, false);
 
        if (readahead)
-               f2fs_ra_meta_pages(sbi, index, ra_blocks, META_POR, true);
+               f2fs_ra_meta_caches(sbi, index, ra_blocks, META_POR, true);
 }
 
-static bool __f2fs_write_meta_folio(struct folio *folio,
-                               struct writeback_control *wbc,
+static bool __f2fs_write_meta_cache(struct f2fs_cached_block *entry,
                                enum iostat_type io_type)
 {
-       struct f2fs_sb_info *sbi = F2FS_F_SB(folio);
-
-       trace_f2fs_writepage(folio, META);
+       struct f2fs_sb_info *sbi = entry->cache->sbi;
 
        if (unlikely(f2fs_cp_error(sbi))) {
                if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
-                       folio_clear_uptodate(folio);
+                       f2fs_cache_clear_uptodate(entry);
                        dec_page_count(sbi, F2FS_DIRTY_META);
-                       folio_unlock(folio);
+                       f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_DIRTY,
+                                               F2FS_CACHE_TAG_NONE);
+                       f2fs_unlock_cache(entry);
                        return true;
                }
                goto redirty_out;
@@ -558,10 +564,10 @@ static bool __f2fs_write_meta_folio(struct folio *folio,
        if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
                goto redirty_out;
 
-       f2fs_do_write_meta_page(sbi, folio, io_type);
+       f2fs_do_write_meta_cache(sbi, entry, io_type);
        dec_page_count(sbi, F2FS_DIRTY_META);
 
-       folio_unlock(folio);
+       f2fs_unlock_cache(entry);
 
        if (unlikely(f2fs_cp_error(sbi)))
                f2fs_submit_merged_write(sbi, META);
@@ -569,101 +575,89 @@ static bool __f2fs_write_meta_folio(struct folio *folio,
        return true;
 
 redirty_out:
-       folio_redirty_for_writepage(wbc, folio);
+       f2fs_cache_set_dirty(entry);
        return false;
 }
 
-static int f2fs_write_meta_pages(struct address_space *mapping,
-                               struct writeback_control *wbc)
+void f2fs_write_meta_caches(struct f2fs_sb_info *sbi)
 {
-       struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
        struct f2fs_lock_context lc;
-       long diff, written;
+       long nr_to_write = LONG_MAX;
 
        if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
-               goto skip_write;
+               return;
 
-       /* collect a number of dirty meta pages and write together */
-       if (wbc->sync_mode != WB_SYNC_ALL &&
-                       get_pages(sbi, F2FS_DIRTY_META) <
-                                       nr_pages_to_skip(sbi, META))
-               goto skip_write;
+       /* collect a number of dirty meta caches and write together */
+       if (get_pages(sbi, F2FS_DIRTY_META) <
+                       nr_pages_to_skip(sbi, META))
+               return;
 
-       /* if locked failed, cp will flush dirty pages instead */
+       /* if locked failed, cp will flush dirty caches instead */
        if (!f2fs_down_write_trylock_trace(&sbi->cp_global_sem, &lc))
-               goto skip_write;
+               return;
 
-       trace_f2fs_writepages(mapping->host, wbc, META);
-       diff = nr_pages_to_write(sbi, META, wbc);
-       written = f2fs_sync_meta_pages(sbi, wbc->nr_to_write, FS_META_IO);
+       nr_to_write = adjust_flush_cache_number(sbi, META);
+       f2fs_sync_meta_caches(sbi, nr_to_write, false, FS_META_IO);
        f2fs_up_write_trace(&sbi->cp_global_sem, &lc);
-       wbc->nr_to_write = max((long)0, wbc->nr_to_write - written - diff);
-       return 0;
-
-skip_write:
-       wbc->pages_skipped += get_pages(sbi, F2FS_DIRTY_META);
-       trace_f2fs_writepages(mapping->host, wbc, META);
-       return 0;
 }
 
-long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, long nr_to_write,
-                               enum iostat_type io_type)
+long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
+                               bool sync, enum iostat_type io_type)
 {
-       struct address_space *mapping = META_MAPPING(sbi);
        pgoff_t index = 0, prev = ULONG_MAX;
-       struct folio_batch fbatch;
+       struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES];
        long nwritten = 0;
-       int nr_folios;
-       struct writeback_control wbc = {};
+       int nr;
        struct blk_plug plug;
-
-       folio_batch_init(&fbatch);
+       bool background = nr_to_write != LONG_MAX;
 
        blk_start_plug(&plug);
 
-       while ((nr_folios = filemap_get_folios_tag(mapping, &index,
-                                       (pgoff_t)-1,
-                                       PAGECACHE_TAG_DIRTY, &fbatch))) {
+       while ((nr = f2fs_cache_gang_lookup_tag(META_CACHE(sbi), entries,
+                       &index, F2FS_ONSTACK_CACHES, F2FS_CACHE_TAG_DIRTY))) {
                int i;
 
-               for (i = 0; i < nr_folios; i++) {
-                       struct folio *folio = fbatch.folios[i];
+               for (i = 0; i < nr; i++) {
+                       struct f2fs_cached_block *entry = entries[i];
+
+                       if (background && unlikely(freezing(current))) {
+                               f2fs_cache_gang_release(entries, nr);
+                               goto stop;
+                       }
 
-                       if (nr_to_write != LONG_MAX && i != 0 &&
-                                       folio->index != prev +
-                                       folio_nr_pages(fbatch.folios[i-1])) {
-                               folio_batch_release(&fbatch);
+                       if (background && i != 0 &&
+                                       entry->index != prev + 1) {
+                               f2fs_cache_gang_release(entries, nr);
                                goto stop;
                        }
 
-                       folio_lock(folio);
+                       f2fs_lock_cache(entry);
 
-                       if (unlikely(!is_meta_folio(sbi, folio))) {
+                       if (unlikely(!f2fs_is_meta_cache(entry))) {
 continue_unlock:
-                               folio_unlock(folio);
+                               f2fs_unlock_cache(entry);
                                continue;
                        }
-                       if (!folio_test_dirty(folio)) {
+                       if (!f2fs_cache_test_dirty(entry)) {
                                /* someone wrote it for us */
                                goto continue_unlock;
                        }
 
-                       f2fs_folio_wait_writeback(folio, META, true, true);
+                       f2fs_cache_wait_writeback(entry);
 
-                       if (!folio_clear_dirty_for_io(folio))
+                       if (!f2fs_cache_test_and_clear_dirty(entry))
                                goto continue_unlock;
 
-                       if (!__f2fs_write_meta_folio(folio, &wbc,
-                                               io_type)) {
-                               folio_unlock(folio);
+                       if (!__f2fs_write_meta_cache(entry, io_type)) {
+                               f2fs_unlock_cache(entry);
                                break;
                        }
-                       nwritten += folio_nr_pages(folio);
-                       prev = folio->index;
+                       nwritten++;
+                       prev = entry->index;
                        if (unlikely(nwritten >= nr_to_write))
                                break;
                }
-               folio_batch_release(&fbatch);
+               f2fs_cache_gang_release(entries, nr);
                cond_resched();
        }
 stop:
@@ -675,29 +669,6 @@ long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, long nr_to_write,
        return nwritten;
 }
 
-static bool f2fs_dirty_meta_folio(struct address_space *mapping,
-               struct folio *folio)
-{
-       trace_f2fs_set_page_dirty(folio, META);
-
-       if (!folio_test_uptodate(folio))
-               folio_mark_uptodate(folio);
-       if (filemap_dirty_folio(mapping, folio)) {
-               inc_page_count(F2FS_M_SB(mapping), F2FS_DIRTY_META);
-               folio_set_f2fs_reference(folio);
-               return true;
-       }
-       return false;
-}
-
-const struct address_space_operations f2fs_meta_aops = {
-       .writepages     = f2fs_write_meta_pages,
-       .dirty_folio    = f2fs_dirty_meta_folio,
-       .invalidate_folio = f2fs_invalidate_folio,
-       .release_folio  = f2fs_release_folio,
-       .migrate_folio  = filemap_migrate_folio,
-};
-
 static void __add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino,
                                                unsigned int devidx, int type)
 {
@@ -1029,21 +1000,21 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
        start_blk = __start_cp_addr(sbi) + 1 + __cp_payload(sbi);
        orphan_blocks = __start_sum_addr(sbi) - 1 - __cp_payload(sbi);
 
-       f2fs_ra_meta_pages(sbi, start_blk, orphan_blocks, META_CP, true);
+       f2fs_ra_meta_caches(sbi, start_blk, orphan_blocks, META_CP, true);
 
        for (i = 0; i < orphan_blocks; i++) {
-               struct folio *folio;
+               struct f2fs_cached_block *entry;
                struct f2fs_orphan_block *orphan_blk;
                struct f2fs_orphan_footer *footer;
                unsigned int entry_count;
 
-               folio = f2fs_get_meta_folio(sbi, start_blk + i);
-               if (IS_ERR(folio)) {
-                       err = PTR_ERR(folio);
+               entry = f2fs_get_meta_cache(sbi, start_blk + i);
+               if (IS_ERR(entry)) {
+                       err = PTR_ERR(entry);
                        goto out;
                }
 
-               orphan_blk = folio_address(folio);
+               orphan_blk = cache_address(entry);
                footer = f2fs_orphan_footer(orphan_blk, sbi);
                entry_count = le32_to_cpu(footer->entry_count);
                if (entry_count > F2FS_ORPHANS_PER_BLOCK(sbi)) {
@@ -1052,7 +1023,7 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
                        set_sbi_flag(sbi, SBI_NEED_FSCK);
                        f2fs_handle_error(sbi, ERROR_INCONSISTENT_ORPHAN);
                        err = -EFSCORRUPTED;
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
                        goto out;
                }
 
@@ -1061,11 +1032,11 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
 
                        err = recover_orphan_inode(sbi, ino);
                        if (err) {
-                               f2fs_folio_put(folio, true);
+                               f2fs_put_cache(entry, true);
                                goto out;
                        }
                }
-               f2fs_folio_put(folio, true);
+               f2fs_put_cache(entry, true);
        }
        /* clear Orphan Flag */
        clear_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG);
@@ -1083,24 +1054,24 @@ static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
        unsigned int nentries = 0;
        unsigned short index = 1;
        unsigned short orphan_blocks;
-       struct folio *folio = NULL;
        struct ino_entry *orphan = NULL;
        struct inode_management *im = &sbi->im[ORPHAN_INO];
+       struct f2fs_cached_block *entry = NULL;
 
        orphan_blocks = GET_ORPHAN_BLOCKS(sbi, im->ino_num);
 
        /*
         * we don't need to do spin_lock(&im->ino_lock) here, since all the
         * orphan inode operations are covered under f2fs_lock_op().
-        * And, spin_lock should be avoided due to page operations below.
+        * And, spin_lock should be avoided due to cache operations below.
         */
        head = &im->ino_list;
 
        /* loop for each orphan inode entry and write them in journal block */
        list_for_each_entry(orphan, head, list) {
-               if (!folio) {
-                       folio = f2fs_grab_meta_folio(sbi, start_blk++);
-                       orphan_blk = folio_address(folio);
+               if (!entry) {
+                       entry = f2fs_grab_meta_cache(sbi, start_blk++);
+                       orphan_blk = cache_address(entry);
                        footer = f2fs_orphan_footer(orphan_blk, sbi);
                        memset(orphan_blk, 0, sbi->blocksize);
                }
@@ -1116,20 +1087,20 @@ static void write_orphan_inodes(struct f2fs_sb_info *sbi, block_t start_blk)
                        footer->blk_addr = cpu_to_le16(index);
                        footer->blk_count = cpu_to_le16(orphan_blocks);
                        footer->entry_count = cpu_to_le32(nentries);
-                       folio_mark_dirty(folio);
-                       f2fs_folio_put(folio, true);
+                       f2fs_mark_cache_dirty(entry);
+                       f2fs_put_cache(entry, true);
                        index++;
                        nentries = 0;
-                       folio = NULL;
+                       entry = NULL;
                }
        }
 
-       if (folio) {
+       if (entry) {
                footer->blk_addr = cpu_to_le16(index);
                footer->blk_count = cpu_to_le16(orphan_blocks);
                footer->entry_count = cpu_to_le32(nentries);
-               folio_mark_dirty(folio);
-               f2fs_folio_put(folio, true);
+               f2fs_mark_cache_dirty(entry);
+               f2fs_put_cache(entry, true);
        }
 }
 
@@ -1149,29 +1120,29 @@ static __u32 f2fs_checkpoint_chksum(struct f2fs_sb_info *sbi,
 }
 
 static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
-               struct f2fs_checkpoint **cp_block, struct folio **cp_folio,
+               struct f2fs_checkpoint **cp_block, struct f2fs_cached_block **cp_entry,
                unsigned long long *version)
 {
        size_t crc_offset = 0;
        __u32 crc;
 
-       *cp_folio = f2fs_get_meta_folio(sbi, cp_addr);
-       if (IS_ERR(*cp_folio))
-               return PTR_ERR(*cp_folio);
+       *cp_entry = f2fs_get_meta_cache(sbi, cp_addr);
+       if (IS_ERR(*cp_entry))
+               return PTR_ERR(*cp_entry);
 
-       *cp_block = folio_address(*cp_folio);
+       *cp_block = cache_address(*cp_entry);
 
        crc_offset = le32_to_cpu((*cp_block)->checksum_offset);
        if (crc_offset < CP_MIN_CHKSUM_OFFSET ||
                        crc_offset > CP_CHKSUM_OFFSET(sbi)) {
-               f2fs_folio_put(*cp_folio, true);
+               f2fs_put_cache(*cp_entry, true);
                f2fs_warn(sbi, "invalid crc_offset: %zu", crc_offset);
                return -EINVAL;
        }
 
        crc = f2fs_checkpoint_chksum(sbi, *cp_block);
        if (crc != cur_cp_crc(*cp_block)) {
-               f2fs_folio_put(*cp_folio, true);
+               f2fs_put_cache(*cp_entry, true);
                f2fs_warn(sbi, "invalid crc value");
                return -EINVAL;
        }
@@ -1180,17 +1151,17 @@ static int get_checkpoint_version(struct f2fs_sb_info *sbi, block_t cp_addr,
        return 0;
 }
 
-static struct folio *validate_checkpoint(struct f2fs_sb_info *sbi,
+static struct f2fs_cached_block *validate_checkpoint(struct f2fs_sb_info *sbi,
                                block_t cp_addr, unsigned long long *version)
 {
-       struct folio *cp_folio_1 = NULL, *cp_folio_2 = NULL;
+       struct f2fs_cached_block *cp_entry_1 = NULL, *cp_entry_2 = NULL;
        struct f2fs_checkpoint *cp_block = NULL;
        unsigned long long cur_version = 0, pre_version = 0;
        unsigned int cp_blocks;
        int err;
 
        err = get_checkpoint_version(sbi, cp_addr, &cp_block,
-                                       &cp_folio_1, version);
+                                       &cp_entry_1, version);
        if (err)
                return NULL;
 
@@ -1205,19 +1176,19 @@ static struct folio *validate_checkpoint(struct f2fs_sb_info *sbi,
 
        cp_addr += cp_blocks - 1;
        err = get_checkpoint_version(sbi, cp_addr, &cp_block,
-                                       &cp_folio_2, version);
+                                       &cp_entry_2, version);
        if (err)
                goto invalid_cp;
        cur_version = *version;
 
        if (cur_version == pre_version) {
                *version = cur_version;
-               f2fs_folio_put(cp_folio_2, true);
-               return cp_folio_1;
+               f2fs_put_cache(cp_entry_2, true);
+               return cp_entry_1;
        }
-       f2fs_folio_put(cp_folio_2, true);
+       f2fs_put_cache(cp_entry_2, true);
 invalid_cp:
-       f2fs_folio_put(cp_folio_1, true);
+       f2fs_put_cache(cp_entry_1, true);
        return NULL;
 }
 
@@ -1225,7 +1196,7 @@ int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
 {
        struct f2fs_checkpoint *cp_block;
        struct f2fs_super_block *fsb = sbi->raw_super;
-       struct folio *cp1, *cp2, *cur_folio;
+       struct f2fs_cached_block *cp1 = NULL, *cp2 = NULL, *cur_entry;
        unsigned long blk_size = sbi->blocksize;
        unsigned long long cp1_version = 0, cp2_version = 0;
        unsigned long long cp_start_blk_no;
@@ -1252,22 +1223,22 @@ int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
 
        if (cp1 && cp2) {
                if (ver_after(cp2_version, cp1_version))
-                       cur_folio = cp2;
+                       cur_entry = cp2;
                else
-                       cur_folio = cp1;
+                       cur_entry = cp1;
        } else if (cp1) {
-               cur_folio = cp1;
+               cur_entry = cp1;
        } else if (cp2) {
-               cur_folio = cp2;
+               cur_entry = cp2;
        } else {
                err = -EFSCORRUPTED;
                goto fail_no_cp;
        }
 
-       cp_block = folio_address(cur_folio);
+       cp_block = cache_address(cur_entry);
        memcpy(sbi->ckpt, cp_block, blk_size);
 
-       if (cur_folio == cp1)
+       if (cur_entry == cp1)
                sbi->cur_cp_pack = 1;
        else
                sbi->cur_cp_pack = 2;
@@ -1282,30 +1253,31 @@ int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi)
                goto done;
 
        cp_blk_no = le32_to_cpu(fsb->cp_blkaddr);
-       if (cur_folio == cp2)
+       if (cur_entry == cp2)
                cp_blk_no += BIT(le32_to_cpu(fsb->log_blocks_per_seg));
 
        for (i = 1; i < cp_blks; i++) {
+               struct f2fs_cached_block *cur_entry_payload;
                void *sit_bitmap_ptr;
                unsigned char *ckpt = (unsigned char *)sbi->ckpt;
 
-               cur_folio = f2fs_get_meta_folio(sbi, cp_blk_no + i);
-               if (IS_ERR(cur_folio)) {
-                       err = PTR_ERR(cur_folio);
+               cur_entry_payload = f2fs_get_meta_cache(sbi, cp_blk_no + i);
+               if (IS_ERR(cur_entry_payload)) {
+                       err = PTR_ERR(cur_entry_payload);
                        goto free_fail_no_cp;
                }
-               sit_bitmap_ptr = folio_address(cur_folio);
+               sit_bitmap_ptr = cache_address(cur_entry_payload);
                memcpy(ckpt + i * blk_size, sit_bitmap_ptr, blk_size);
-               f2fs_folio_put(cur_folio, true);
+               f2fs_put_cache(cur_entry_payload, true);
        }
 done:
-       f2fs_folio_put(cp1, true);
-       f2fs_folio_put(cp2, true);
+       f2fs_put_cache(cp1, true);
+       f2fs_put_cache(cp2, true);
        return 0;
 
 free_fail_no_cp:
-       f2fs_folio_put(cp1, true);
-       f2fs_folio_put(cp2, true);
+       f2fs_put_cache(cp1, true);
+       f2fs_put_cache(cp2, true);
 fail_no_cp:
        kvfree(sbi->ckpt);
        return err;
@@ -1604,7 +1576,7 @@ static void unblock_operations(struct f2fs_sb_info *sbi)
        f2fs_unlock_all(sbi);
 }
 
-void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
+void f2fs_sync_dirty_data(struct f2fs_sb_info *sbi, int type)
 {
        DEFINE_WAIT(wait);
 
@@ -1617,7 +1589,7 @@ void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type)
                        break;
 
                if (type == F2FS_DIRTY_META)
-                       f2fs_sync_meta_pages(sbi, LONG_MAX, FS_CP_META_IO);
+                       f2fs_sync_meta_caches(sbi, LONG_MAX, true, FS_META_IO);
                else if (type == F2FS_WB_CP_DATA)
                        f2fs_submit_merged_write(sbi, DATA);
 
@@ -1696,31 +1668,24 @@ static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
 static void commit_checkpoint(struct f2fs_sb_info *sbi,
        void *src, block_t blk_addr)
 {
-       struct writeback_control wbc = {};
-
-       /*
-        * filemap_get_folios_tag and folio_lock again will take
-        * some extra time. Therefore, f2fs_update_meta_pages and
-        * f2fs_sync_meta_pages are combined in this function.
-        */
-       struct folio *folio = f2fs_grab_meta_folio(sbi, blk_addr);
+       struct f2fs_cached_block *entry = f2fs_grab_meta_cache(sbi, blk_addr);
 
-       memcpy(folio_address(folio), src, F2FS_BLKSIZE(sbi));
+       memcpy(cache_address(entry), src, F2FS_BLKSIZE(sbi));
 
-       folio_mark_dirty(folio);
-       if (unlikely(!folio_clear_dirty_for_io(folio)))
+       f2fs_mark_cache_dirty(entry);
+       if (unlikely(!f2fs_cache_test_and_clear_dirty(entry)))
                f2fs_bug_on(sbi, 1);
 
-       /* writeout cp pack 2 page */
-       if (unlikely(!__f2fs_write_meta_folio(folio, &wbc, FS_CP_META_IO))) {
+       /* writeout cp pack 2 cache */
+       if (unlikely(!__f2fs_write_meta_cache(entry, FS_CP_META_IO))) {
                if (f2fs_cp_error(sbi)) {
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
                        return;
                }
                f2fs_bug_on(sbi, true);
        }
 
-       f2fs_folio_put(folio, false);
+       f2fs_put_cache(entry, false);
 
        /* submit checkpoint (with barrier if NOBARRIER is not set) */
        f2fs_submit_merged_write(sbi, META_FLUSH);
@@ -1788,8 +1753,8 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
        u64 kbytes_written;
        int err;
 
-       /* Flush all the NAT/SIT pages */
-       f2fs_sync_meta_pages(sbi, LONG_MAX, FS_CP_META_IO);
+       /* Flush all the NAT/SIT caches */
+       f2fs_sync_meta_caches(sbi, LONG_MAX, true, FS_CP_META_IO);
 
        stat_cp_time(cpc, CP_TIME_SYNC_META);
 
@@ -1812,7 +1777,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
        }
 
        /* 2 cp + n data seg summary + orphan inode blocks */
-       data_sum_blocks = f2fs_npages_for_summary_flush(sbi, false);
+       data_sum_blocks = f2fs_nblocks_for_summary_flush(sbi, false);
        spin_lock_irqsave(&sbi->cp_lock, flags);
        if (data_sum_blocks < NR_CURSEG_DATA_TYPE)
                __set_ckpt_flags(ckpt, CP_COMPACT_SUM_FLAG);
@@ -1857,15 +1822,15 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
 
                blk = start_blk + BLKS_PER_SEG(sbi) - nm_i->nat_bits_blocks;
                for (i = 0; i < nm_i->nat_bits_blocks; i++)
-                       f2fs_update_meta_page(sbi, nm_i->nat_bits +
+                       f2fs_update_meta_block(sbi, nm_i->nat_bits +
                                        F2FS_BLK_TO_BYTES(sbi, i), blk + i);
        }
 
        /* write out checkpoint buffer at block 0 */
-       f2fs_update_meta_page(sbi, ckpt, start_blk++);
+       f2fs_update_meta_block(sbi, ckpt, start_blk++);
 
        for (i = 1; i < 1 + cp_payload_blks; i++)
-               f2fs_update_meta_page(sbi, (char *)ckpt +
+               f2fs_update_meta_block(sbi, (char *)ckpt +
                                i * F2FS_BLKSIZE(sbi), start_blk++);
 
        if (orphan_num) {
@@ -1888,15 +1853,15 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
        }
 
        /* Here, we have one bio having CP pack except cp pack 2 page */
-       f2fs_sync_meta_pages(sbi, LONG_MAX, FS_CP_META_IO);
+       f2fs_sync_meta_caches(sbi, LONG_MAX, true, FS_CP_META_IO);
        stat_cp_time(cpc, CP_TIME_SYNC_CP_META);
 
        /* Wait for all dirty meta pages to be submitted for IO */
-       f2fs_wait_on_all_pages(sbi, F2FS_DIRTY_META);
+       f2fs_sync_dirty_data(sbi, F2FS_DIRTY_META);
        stat_cp_time(cpc, CP_TIME_WAIT_DIRTY_META);
 
        /* wait for previous submitted meta pages writeback */
-       f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
+       f2fs_sync_dirty_data(sbi, F2FS_WB_CP_DATA);
        stat_cp_time(cpc, CP_TIME_WAIT_CP_DATA);
 
        /* flush all device cache */
@@ -1907,7 +1872,7 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
 
        /* barrier and flush checkpoint cp pack 2 page if it can */
        commit_checkpoint(sbi, ckpt, start_blk);
-       f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
+       f2fs_sync_dirty_data(sbi, F2FS_WB_CP_DATA);
        stat_cp_time(cpc, CP_TIME_WAIT_LAST_CP);
 
        /*
@@ -1915,10 +1880,10 @@ static int do_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
         * used for migration of encrypted, verity or compressed inode's blocks.
         */
        if (f2fs_sb_has_encrypt(sbi) || f2fs_sb_has_verity(sbi) ||
-               f2fs_sb_has_compression(sbi))
-               f2fs_bug_on(sbi,
-                       invalidate_inode_pages2_range(META_MAPPING(sbi),
-                               MAIN_BLKADDR(sbi), MAX_BLKADDR(sbi) - 1));
+               f2fs_sb_has_compression(sbi)) {
+               f2fs_truncate_meta_caches(sbi, MAIN_BLKADDR(sbi),
+                               MAX_BLKADDR(sbi) - MAIN_BLKADDR(sbi));
+       }
 
        f2fs_release_ino_entry(sbi, false);
 
fs/f2fs/compress.c +2 -2
index ce88092d9ce2..991b7738c645 100644
@@ -1150,7 +1150,7 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
                f2fs_compress_ctx_add_page(cc, folio);
 
                if (!folio_test_uptodate(folio)) {
-                       f2fs_handle_page_eio(sbi, folio, DATA);
+                       f2fs_handle_page_eio(sbi, folio->index, DATA);
 release_and_retry:
                        f2fs_put_rpages(cc);
                        f2fs_unlock_rpages(cc, i + 1);
@@ -1357,7 +1357,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
                fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_folio,
                                                dn.ofs_in_node + i + 1);
 
-               /* wait for GCed page writeback via META_MAPPING */
+               /* wait for GCed page writeback via generic cache */
                f2fs_wait_on_block_writeback(inode, fio.old_blkaddr);
        }
 
fs/f2fs/data.c +4 -10
index 59fef2132177..674d1b8e33d8 100644
@@ -63,8 +63,7 @@ bool f2fs_is_cp_guaranteed(const struct folio *folio)
        inode = mapping->host;
        sbi = F2FS_I_SB(inode);
 
-       if (inode->i_ino == F2FS_META_INO(sbi) ||
-                       inode->i_ino == F2FS_NODE_INO(sbi) ||
+       if (inode->i_ino == F2FS_NODE_INO(sbi) ||
                        S_ISDIR(inode->i_mode))
                return true;
 
@@ -82,9 +81,6 @@ static enum count_type __read_io_type(struct folio *folio)
                struct inode *inode = mapping->host;
                struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 
-               if (inode->i_ino == F2FS_META_INO(sbi))
-                       return F2FS_RD_META;
-
                if (inode->i_ino == F2FS_NODE_INO(sbi))
                        return F2FS_RD_NODE;
        }
@@ -1418,7 +1414,7 @@ static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi,
        bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, folio->index,
                                 for_write);
 
-       /* wait for GCed page writeback via META_MAPPING */
+       /* wait for GCed page writeback via generic cache */
        f2fs_wait_on_block_writeback(inode, blkaddr);
 
        if (!bio_add_folio(bio, folio, PAGE_SIZE, 0))
@@ -3241,7 +3237,7 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)
                goto out_writepage;
        }
 
-       /* wait for GCed page writeback via META_MAPPING */
+       /* wait for GCed page writeback via generic cache */
        if (fio->meta_gc)
                f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
 
@@ -4314,9 +4310,7 @@ void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
                return;
 
        if (folio_test_dirty(folio)) {
-               if (inode->i_ino == F2FS_META_INO(sbi)) {
-                       dec_page_count(sbi, F2FS_DIRTY_META);
-               } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
+               if (inode->i_ino == F2FS_NODE_INO(sbi)) {
                        dec_page_count(sbi, F2FS_DIRTY_NODES);
                } else {
                        inode_dec_dirty_pages(inode);
fs/f2fs/debug.c +4 -8
index a6cfceafe2fe..24819d4c0d55 100644
@@ -224,8 +224,7 @@ static void update_general_status(struct f2fs_sb_info *sbi)
        si->dirty_count = dirty_segments(sbi);
        if (sbi->node_inode)
                si->node_pages = NODE_MAPPING(sbi)->nrpages;
-       if (sbi->meta_inode)
-               si->meta_pages = META_MAPPING(sbi)->nrpages;
+       si->meta_caches = META_CACHE(sbi)->num_entries;
 #ifdef CONFIG_F2FS_FS_COMPRESSION
        if (sbi->compress_inode) {
                si->compress_pages = COMPRESS_MAPPING(sbi)->nrpages;
@@ -388,11 +387,8 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
 
                si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
        }
-       if (sbi->meta_inode) {
-               unsigned long npages = META_MAPPING(sbi)->nrpages;
-
-               si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
-       }
+       si->page_mem += (unsigned long long)META_CACHE(sbi)->num_entries << PAGE_SHIFT;
+       si->cache_mem += META_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
 #ifdef CONFIG_F2FS_FS_COMPRESSION
        if (sbi->compress_inode) {
                unsigned long npages = COMPRESS_MAPPING(sbi)->nrpages;
@@ -708,7 +704,7 @@ static int stat_show(struct seq_file *s, void *v)
                seq_printf(s, "  - quota data: %4d in quota files:%4d\n",
                           si->ndirty_qdata, si->nquota_files);
                seq_printf(s, "  - meta: %4d in %4d\n",
-                          si->ndirty_meta, si->meta_pages);
+                          si->ndirty_meta, si->meta_caches);
                seq_printf(s, "  - imeta: %4d\n",
                           si->ndirty_imeta);
                seq_printf(s, "  - fsync mark: %4lld\n",
fs/f2fs/f2fs.h +27 -63
index c0f82b63d732..e2b29067eb0a 100644
@@ -1074,7 +1074,7 @@ struct f2fs_nm_info {
        nid_t next_scan_nid;            /* the next nid to be scanned */
        nid_t max_rf_node_blocks;       /* max # of nodes for recovery */
        unsigned int ram_thresh;        /* control the memory footprint */
-       unsigned int ra_nid_pages;      /* # of nid pages to be readaheaded */
+       unsigned int ra_nid_blocks;     /* # of nid blocks to be readaheaded */
        unsigned int dirty_nats_ratio;  /* control dirty nats ratio threshold */
 
        /* NAT cache management */
@@ -1820,7 +1820,6 @@ struct f2fs_sb_info {
        struct f2fs_checkpoint *ckpt;           /* raw checkpoint pointer */
        int cur_cp_pack;                        /* remain current cp pack */
        spinlock_t cp_lock;                     /* for flag in ckpt */
-       struct inode *meta_inode;               /* cache meta blocks */
        struct f2fs_rwsem cp_global_sem;        /* checkpoint procedure lock */
        struct f2fs_rwsem cp_rwsem;             /* blocking FS operations */
        struct f2fs_rwsem node_write;           /* locking node writes */
@@ -1874,7 +1873,6 @@ struct f2fs_sb_info {
        unsigned int dentry_reserved_size;      /* dentry reserved bytes */
        unsigned int root_ino_num;              /* root inode number*/
        unsigned int node_ino_num;              /* node inode number*/
-       unsigned int meta_ino_num;              /* meta inode number*/
        unsigned int log_blocks_per_seg;        /* log2 blocks per segment */
        unsigned int blocks_per_seg;            /* blocks per segment */
        unsigned int segs_per_sec;              /* segments per section */
@@ -2377,9 +2375,9 @@ static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
        return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
 }
 
-static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
+static inline bool f2fs_is_meta_cache(struct f2fs_cached_block *entry)
 {
-       return sbi->meta_inode->i_mapping;
+       return entry->cache && entry->cache == META_CACHE(entry->cache->sbi);
 }
 
 static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
@@ -2387,11 +2385,6 @@ static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
        return sbi->node_inode->i_mapping;
 }
 
-static inline bool is_meta_folio(struct f2fs_sb_info *sbi, struct folio *folio)
-{
-       return folio->mapping == META_MAPPING(sbi);
-}
-
 static inline bool is_node_folio(struct f2fs_sb_info *sbi, struct folio *folio)
 {
        return folio->mapping == NODE_MAPPING(sbi);
@@ -4130,9 +4123,9 @@ bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
 void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
 void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
 int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
-int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio);
-int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio);
-int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct folio *folio);
+int f2fs_recover_inline_xattr(struct inode *inode, struct f2fs_cached_block *entry);
+int f2fs_recover_xattr_data(struct inode *inode, struct f2fs_cached_block *entry);
+int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct f2fs_cached_block *entry);
 int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
                        unsigned int segno, struct f2fs_summary_block *sum);
 int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
@@ -4170,7 +4163,7 @@ void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
-int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
+int f2fs_nblocks_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
 int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
 int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi);
@@ -4184,11 +4177,13 @@ int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
                                        struct cp_control *cpc);
-struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno);
-void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
+struct f2fs_cached_block *f2fs_get_sum_cache(struct f2fs_sb_info *sbi,
+               unsigned int segno);
+void f2fs_update_meta_block(struct f2fs_sb_info *sbi, void *src,
                                        block_t blk_addr);
-void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio,
-                                               enum iostat_type io_type);
+void f2fs_do_write_meta_cache(struct f2fs_sb_info *sbi,
+                       struct f2fs_cached_block *entry,
+                       enum iostat_type io_type);
 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
 void f2fs_outplace_write_data(struct dnode_of_data *dn,
                        struct f2fs_io_info *fio);
@@ -4211,8 +4206,6 @@ void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
                                        block_t blkaddr, unsigned int blkcnt);
 void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type,
                bool ordered, bool locked);
-#define f2fs_wait_on_page_writeback(page, type, ordered, locked)       \
-               f2fs_folio_wait_writeback(page_folio(page), type, ordered, locked)
 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
                                                                block_t len);
@@ -4278,20 +4271,21 @@ void f2fs_unlock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc);
 void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
                                                        unsigned char reason);
 void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi);
-struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index);
-struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index);
-struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index);
-struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index);
+struct f2fs_cached_block *f2fs_grab_meta_cache(struct f2fs_sb_info *sbi, pgoff_t index);
+struct f2fs_cached_block *f2fs_get_meta_cache(struct f2fs_sb_info *sbi, pgoff_t index);
+struct f2fs_cached_block *f2fs_get_meta_cache_retry(struct f2fs_sb_info *sbi, pgoff_t index);
+struct f2fs_cached_block *f2fs_get_tmp_cache(struct f2fs_sb_info *sbi, pgoff_t index);
 bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
                                        block_t blkaddr, int type);
 bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi,
                                        block_t blkaddr, int type);
-int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
+int f2fs_ra_meta_caches(struct f2fs_sb_info *sbi, block_t start, int nrpages,
                        int type, bool sync);
-void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
+void f2fs_ra_meta_caches_cond(struct f2fs_sb_info *sbi, pgoff_t index,
                                                        unsigned int ra_blocks);
-long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, long nr_to_write,
-                       enum iostat_type io_type);
+void f2fs_write_meta_caches(struct f2fs_sb_info *sbi);
+long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
+                               bool sync, enum iostat_type io_type);
 void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
 void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
 void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
@@ -4310,7 +4304,7 @@ void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
 void f2fs_remove_dirty_inode(struct inode *inode);
 int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
                                                                bool from_cp);
-void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
+void f2fs_sync_dirty_data(struct f2fs_sb_info *sbi, int type);
 u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
 int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
 void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
@@ -4477,7 +4471,7 @@ struct f2fs_stat_info {
        unsigned int bimodal, avg_vblocks;
        int util_free, util_valid, util_invalid;
        int rsvd_segs, overp_segs;
-       int dirty_count, node_pages, meta_pages, compress_pages;
+       int dirty_count, node_pages, meta_caches, compress_pages;
        int compress_page_hit;
        int prefree_count, free_segs, free_secs;
        int cp_call_count[MAX_CALL_TYPE], cp_count;
@@ -4679,7 +4673,6 @@ extern const struct file_operations f2fs_file_operations;
 extern const struct inode_operations f2fs_file_inode_operations;
 extern const struct address_space_operations f2fs_dblock_aops;
 extern const struct address_space_operations f2fs_node_aops;
-extern const struct address_space_operations f2fs_meta_aops;
 extern const struct inode_operations f2fs_dir_inode_operations;
 extern const struct inode_operations f2fs_symlink_inode_operations;
 extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
@@ -4700,7 +4693,7 @@ int f2fs_convert_inline_folio(struct dnode_of_data *dn, struct folio *folio);
 int f2fs_convert_inline_inode(struct inode *inode);
 int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
 int f2fs_write_inline_data(struct inode *inode, struct folio *folio);
-int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio);
+int f2fs_recover_inline_data(struct inode *inode, struct f2fs_cached_block *entry);
 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
                const struct f2fs_filename *fname, struct folio **res_folio,
                bool use_hash);
@@ -5260,10 +5253,8 @@ static inline void f2fs_schedule_timeout_killable(long timeout, bool io)
 }
 
 static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi,
-                               struct folio *folio, enum page_type type)
+                               pgoff_t ofs, enum page_type type)
 {
-       pgoff_t ofs = folio->index;
-
        if (unlikely(f2fs_cp_error(sbi)))
                return;
 
@@ -5298,37 +5289,10 @@ static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
        return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
 }
 
-static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi,
-                                       block_t blkaddr, unsigned int cnt)
-{
-       bool need_submit = false;
-       int i = 0;
-
-       do {
-               struct folio *folio;
-
-               folio = filemap_get_folio(META_MAPPING(sbi), blkaddr + i);
-               if (!IS_ERR(folio)) {
-                       if (folio_test_writeback(folio))
-                               need_submit = true;
-                       f2fs_folio_put(folio, false);
-               }
-       } while (++i < cnt && !need_submit);
-
-       if (need_submit)
-               f2fs_submit_merged_write_cond(sbi, sbi->meta_inode,
-                                                       NULL, 0, DATA);
-
-       truncate_inode_pages_range(META_MAPPING(sbi),
-                       F2FS_BLK_TO_BYTES(sbi, (loff_t)blkaddr),
-                       F2FS_BLK_END_BYTES(sbi,
-                                           (loff_t)(blkaddr + cnt - 1)));
-}
-
 static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
                                                block_t blkaddr, unsigned int len)
 {
-       f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
+       f2fs_truncate_meta_caches(sbi, blkaddr, len);
        f2fs_invalidate_compress_pages_range(sbi, blkaddr, len);
 }
 
fs/f2fs/file.c +2 -2
index b0bdc7a977b9..7c2bfa0e63d2 100644
@@ -215,7 +215,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
 
        f2fs_folio_wait_writeback(folio, DATA, false, true);
 
-       /* wait for GCed page writeback via META_MAPPING */
+       /* wait for GCed page writeback via generic cache */
        f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
 
        /*
@@ -2567,7 +2567,7 @@ int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
                f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
                break;
        case F2FS_GOING_DOWN_METAFLUSH:
-               f2fs_sync_meta_pages(sbi, LONG_MAX, FS_META_IO);
+               f2fs_sync_meta_caches(sbi, LONG_MAX, true, FS_META_IO);
                f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
                break;
        case F2FS_GOING_DOWN_NEED_FSCK:
fs/f2fs/gc.c +74 -61
index c70deac9f1a3..bed9056d6b6e 100644
@@ -1066,7 +1066,7 @@ static int gc_node_segment(struct f2fs_sb_info *sbi,
                        continue;
 
                if (phase == 0) {
-                       f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(sbi, nid), 1,
+                       f2fs_ra_meta_caches(sbi, NAT_BLOCK_OFFSET(sbi, nid), 1,
                                                        META_NAT, true);
                        continue;
                }
@@ -1219,7 +1219,8 @@ static int ra_data_block(struct inode *inode, pgoff_t index)
        struct address_space *mapping = inode->i_mapping;
        struct inode *atomic_inode = NULL;
        struct dnode_of_data dn;
-       struct folio *folio, *efolio;
+       struct folio *folio;
+       struct f2fs_cached_block *entry;
        struct f2fs_io_info fio = {
                .sbi = sbi,
                .ino = inode->i_ino,
@@ -1229,6 +1230,7 @@ static int ra_data_block(struct inode *inode, pgoff_t index)
                .op_flags = 0,
                .encrypted_page = NULL,
                .in_list = 0,
+               .is_cache = 1,
        };
        int err = 0;
 
@@ -1287,22 +1289,21 @@ static int ra_data_block(struct inode *inode, pgoff_t index)
 
        f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
 
-       efolio = f2fs_filemap_get_folio(META_MAPPING(sbi), dn.data_blkaddr,
-                                       FGP_LOCK | FGP_CREAT, GFP_NOFS);
-       if (IS_ERR(efolio)) {
-               err = PTR_ERR(efolio);
+       entry = f2fs_grab_cache(META_CACHE(sbi), dn.data_blkaddr,
+                                               F2FS_CACHE_LOCK_CREATE);
+       if (IS_ERR(entry)) {
+               err = PTR_ERR(entry);
                goto put_folio;
        }
 
-       fio.encrypted_page = &efolio->page;
-
-       if (folio_test_uptodate(efolio))
-               goto put_encrypted_page;
+       if (f2fs_cache_test_uptodate(entry))
+               goto put_cache;
 
-       err = f2fs_submit_page_bio(&fio);
+       fio.cache_entry = entry;
+       err = f2fs_submit_cache_read(&fio);
        if (err)
-               goto put_encrypted_page;
-       f2fs_put_page(fio.encrypted_page, false);
+               goto put_cache;
+       f2fs_put_cache(entry, false);
        f2fs_folio_put(folio, true);
 
        f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE(sbi));
@@ -1311,8 +1312,8 @@ static int ra_data_block(struct inode *inode, pgoff_t index)
        if (atomic_inode)
                iput(atomic_inode);
        return 0;
-put_encrypted_page:
-       f2fs_put_page(fio.encrypted_page, true);
+put_cache:
+       f2fs_put_cache(entry, true);
 put_folio:
        f2fs_folio_put(folio, true);
 out_iput:
@@ -1322,7 +1323,7 @@ static int ra_data_block(struct inode *inode, pgoff_t index)
 }
 
 /*
- * Move data block via META_MAPPING while keeping locked data page.
+ * Move data block via meta cache while keeping locked data page.
  * This can be used to move blocks, aka LBAs, directly on disk.
  */
 static int move_data_block(struct inode *inode, block_t bidx,
@@ -1339,11 +1340,13 @@ static int move_data_block(struct inode *inode, block_t bidx,
                .op_flags = 0,
                .encrypted_page = NULL,
                .in_list = 0,
+               .is_cache = 1,
        };
        struct dnode_of_data dn;
        struct f2fs_summary sum;
        struct node_info ni;
-       struct folio *folio, *mfolio, *efolio;
+       struct folio *folio;
+       struct f2fs_cached_block *sentry, *tentry;
        block_t newaddr;
        int err = 0;
        bool lfs_mode = f2fs_lfs_mode(fio.sbi);
@@ -1408,20 +1411,20 @@ static int move_data_block(struct inode *inode, block_t bidx,
        if (lfs_mode)
                f2fs_down_write(&fio.sbi->io_order_lock);
 
-       mfolio = f2fs_grab_cache_folio(META_MAPPING(fio.sbi),
-                                       fio.old_blkaddr, false);
-       if (IS_ERR(mfolio)) {
-               err = PTR_ERR(mfolio);
+       sentry = f2fs_grab_cache(META_CACHE(fio.sbi), fio.old_blkaddr,
+                                               F2FS_CACHE_LOCK_CREATE);
+       if (IS_ERR(sentry)) {
+               err = PTR_ERR(sentry);
                goto up_out;
        }
 
-       fio.encrypted_page = folio_file_page(mfolio, fio.old_blkaddr);
+       fio.cache_entry = sentry;
 
-       /* read source block in mfolio */
-       if (!folio_test_uptodate(mfolio)) {
-               err = f2fs_submit_page_bio(&fio);
+       /* read source block in cache */
+       if (!f2fs_cache_test_uptodate(sentry)) {
+               err = f2fs_submit_cache_read(&fio);
                if (err) {
-                       f2fs_folio_put(mfolio, true);
+                       f2fs_put_cache(sentry, true);
                        goto up_out;
                }
 
@@ -1430,11 +1433,11 @@ static int move_data_block(struct inode *inode, block_t bidx,
                f2fs_update_iostat(fio.sbi, NULL, FS_GDATA_READ_IO,
                                                F2FS_BLKSIZE(fio.sbi));
 
-               folio_lock(mfolio);
-               if (unlikely(!is_meta_folio(fio.sbi, mfolio) ||
-                            !folio_test_uptodate(mfolio))) {
+               f2fs_lock_cache(sentry);
+               if (unlikely(!f2fs_is_meta_cache(sentry) ||
+                               !f2fs_cache_test_uptodate(sentry))) {
                        err = -EIO;
-                       f2fs_folio_put(mfolio, true);
+                       f2fs_put_cache(sentry, true);
                        goto up_out;
                }
        }
@@ -1445,39 +1448,39 @@ static int move_data_block(struct inode *inode, block_t bidx,
        err = f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr,
                                &sum, type, NULL);
        if (err) {
-               f2fs_folio_put(mfolio, true);
+               f2fs_put_cache(sentry, true);
                /* filesystem should shutdown, no need to recovery block */
                goto up_out;
        }
 
-       efolio = f2fs_filemap_get_folio(META_MAPPING(fio.sbi), newaddr,
-                                       FGP_LOCK | FGP_CREAT, GFP_NOFS);
-       if (IS_ERR(efolio)) {
-               err = PTR_ERR(efolio);
-               f2fs_folio_put(mfolio, true);
+       tentry = f2fs_grab_cache(META_CACHE(fio.sbi), newaddr,
+                                       F2FS_CACHE_LOCK_CREATE);
+       if (IS_ERR(tentry)) {
+               err = PTR_ERR(tentry);
+               f2fs_put_cache(sentry, true);
                goto recover_block;
        }
 
-       fio.encrypted_page = &efolio->page;
+       fio.cache_entry = tentry;
 
        /* write target block */
-       f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true, true);
-       memcpy(page_address(fio.encrypted_page),
-                               folio_address(mfolio), PAGE_SIZE);
-       f2fs_folio_put(mfolio, true);
+       f2fs_cache_wait_writeback_cond(tentry, DATA);
+       memcpy(cache_address(tentry), cache_address(sentry),
+                                                       fio.sbi->blocksize);
+       f2fs_put_cache(sentry, true);
 
        f2fs_invalidate_internal_cache(fio.sbi, fio.old_blkaddr, 1);
 
-       set_page_dirty(fio.encrypted_page);
-       if (clear_page_dirty_for_io(fio.encrypted_page))
+       f2fs_mark_cache_dirty(tentry);
+       if (f2fs_cache_test_and_clear_dirty(tentry))
                dec_page_count(fio.sbi, F2FS_DIRTY_META);
 
-       set_page_writeback(fio.encrypted_page);
+       f2fs_start_cache_writeback(tentry);
 
        fio.op = REQ_OP_WRITE;
        fio.op_flags = REQ_SYNC;
        fio.new_blkaddr = newaddr;
-       f2fs_submit_page_write(&fio);
+       f2fs_submit_cache_write(&fio);
 
        f2fs_update_iostat(fio.sbi, NULL, FS_GC_DATA_IO,
                                        F2FS_BLKSIZE(fio.sbi));
@@ -1485,7 +1488,7 @@ static int move_data_block(struct inode *inode, block_t bidx,
        f2fs_update_data_blkaddr(&dn, newaddr);
        set_inode_flag(inode, FI_APPEND_WRITE);
 
-       f2fs_put_page(fio.encrypted_page, true);
+       f2fs_put_cache(tentry, true);
 recover_block:
        if (err)
                f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr,
@@ -1617,7 +1620,7 @@ static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
                        continue;
 
                if (phase == 0) {
-                       f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(sbi, nid), 1,
+                       f2fs_ra_meta_caches(sbi, NAT_BLOCK_OFFSET(sbi, nid), 1,
                                                        META_NAT, true);
                        continue;
                }
@@ -1821,28 +1824,31 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
        sum_blk_cnt = DIV_ROUND_UP(end_segno - segno, sbi->sums_per_block);
        /* readahead multi ssa blocks those have contiguous address */
        if (__is_large_section(sbi))
-               f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno),
+               f2fs_ra_meta_caches(sbi, GET_SUM_BLOCK(sbi, segno),
                                        sum_blk_cnt, META_SSA, true);
 
        /* reference all summary page */
        while (segno < end_segno) {
-               struct folio *sum_folio = f2fs_get_sum_folio(sbi, segno);
+               struct f2fs_cached_block *sum_entry =
+                               f2fs_get_sum_cache(sbi, segno);
 
                segno += sbi->sums_per_block;
-               if (IS_ERR(sum_folio)) {
-                       int err = PTR_ERR(sum_folio);
+               if (IS_ERR(sum_entry)) {
+                       int err = PTR_ERR(sum_entry);
 
                        end_segno = segno - sbi->sums_per_block;
                        segno = rounddown(start_segno, sbi->sums_per_block);
                        while (segno < end_segno) {
-                               sum_folio = filemap_get_folio(META_MAPPING(sbi),
+                               sum_entry = f2fs_find_meta_cache(sbi,
                                                GET_SUM_BLOCK(sbi, segno));
-                               folio_put_refs(sum_folio, 2);
+                               f2fs_put_cache(sum_entry, false);
+                               f2fs_put_cache(sum_entry, false);
                                segno += sbi->sums_per_block;
                        }
                        return err;
                }
-               folio_unlock(sum_folio);
+               f2fs_unlock_cache(sum_entry);
+
        }
 
        blk_start_plug(&plug);
@@ -1850,11 +1856,16 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
        segno = start_segno;
        while (segno < end_segno) {
                unsigned int cur_segno;
+               unsigned int block_end_segno;
 
                /* find segment summary of victim */
-               struct folio *sum_folio = filemap_get_folio(META_MAPPING(sbi),
+               struct f2fs_cached_block *sum_entry =
+                               f2fs_find_meta_cache(sbi,
                                        GET_SUM_BLOCK(sbi, segno));
-               unsigned int block_end_segno = rounddown(segno, sbi->sums_per_block)
+
+               f2fs_bug_on(sbi, IS_ERR(sum_entry));
+
+               block_end_segno = rounddown(segno, sbi->sums_per_block)
                                        + sbi->sums_per_block;
 
                if (block_end_segno > end_segno)
@@ -1867,8 +1878,8 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
                        goto next_block;
                }
 
-               if (!folio_test_uptodate(sum_folio) ||
-                   unlikely(f2fs_cp_error(sbi)))
+               if (!f2fs_cache_test_uptodate(sum_entry) ||
+                       unlikely(f2fs_cp_error(sbi)))
                        goto next_block;
 
                for (cur_segno = segno; cur_segno < block_end_segno;
@@ -1887,7 +1898,7 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
                                data_type = (type == SUM_TYPE_DATA) ? DATA : NODE;
                        }
 
-                       sum = SUM_BLK_PAGE_ADDR(sbi, sum_folio, cur_segno);
+                       sum = SUM_BLK_ENTRY_ADDR(sbi, sum_entry, cur_segno);
                        if (type != GET_SUM_TYPE(sum_footer(sbi, sum))) {
                                f2fs_err(sbi, "Inconsistent segment (%u) type "
                                                "[%d, %d] in SIT and SSA",
@@ -1929,12 +1940,14 @@ static int do_garbage_collect(struct f2fs_sb_info *sbi,
                                        cur_segno + 1 : NULL_SEGNO;
 
                        if (unlikely(freezing(current))) {
-                               folio_put_refs(sum_folio, 2);
+                               f2fs_put_cache(sum_entry, false);
+                               f2fs_put_cache(sum_entry, false);
                                goto stop;
                        }
                }
 next_block:
-               folio_put_refs(sum_folio, 2);
+               f2fs_put_cache(sum_entry, false);
+               f2fs_put_cache(sum_entry, false);
                segno = block_end_segno;
        }
 
fs/f2fs/inline.c +5 -4
index 925bdaad013c..d10acb5042e3 100644
@@ -293,7 +293,7 @@ int f2fs_write_inline_data(struct inode *inode, struct folio *folio)
        return 0;
 }
 
-int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio)
+int f2fs_recover_inline_data(struct inode *inode, struct f2fs_cached_block *entry)
 {
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
        struct f2fs_inode *ri = NULL;
@@ -307,12 +307,13 @@ int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio)
         *    x       o  -> remove data blocks, and then recover inline_data
         *    x       x  -> recover data blocks
         */
-       if (IS_INODE(F2FS_I_SB(inode), nfolio))
-               ri = F2FS_INODE(nfolio);
+       if (IS_INODE(sbi, cache_folio(entry)))
+               ri = &CACHED_NODE(entry)->i;
 
        if (f2fs_has_inline_data(inode) &&
                        ri && (ri->i_inline & F2FS_INLINE_DATA)) {
                struct folio *ifolio;
+
 process_inline:
                ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
                if (IS_ERR(ifolio))
@@ -320,7 +321,7 @@ int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio)
 
                f2fs_folio_wait_writeback(ifolio, NODE, true, true);
 
-               src_addr = inline_data_addr(inode, nfolio);
+               src_addr = inline_data_addr(inode, cache_folio(entry));
                dst_addr = inline_data_addr(inode, ifolio);
                memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
 
fs/f2fs/inode.c +2 -7
index 24205a35be04..f2e1d8b50f8c 100644
@@ -578,7 +578,7 @@ static int do_read_inode(struct inode *inode)
 
 static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
 {
-       if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
+       if (ino == F2FS_NODE_INO(sbi))
                return true;
 #ifdef CONFIG_F2FS_FS_COMPRESSION
        if (test_opt(sbi, COMPRESS_CACHE) && ino == F2FS_COMPRESS_INO(sbi))
@@ -625,9 +625,6 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
        if (ino == F2FS_NODE_INO(sbi)) {
                inode->i_mapping->a_ops = &f2fs_node_aops;
                mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
-       } else if (ino == F2FS_META_INO(sbi)) {
-               inode->i_mapping->a_ops = &f2fs_meta_aops;
-               mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
        } else if (ino == F2FS_COMPRESS_INO(sbi)) {
 #ifdef CONFIG_F2FS_FS_COMPRESSION
                inode->i_mapping->a_ops = &f2fs_compress_aops;
@@ -827,8 +824,7 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
 {
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 
-       if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-                       inode->i_ino == F2FS_META_INO(sbi))
+       if (inode->i_ino == F2FS_NODE_INO(sbi))
                return 0;
 
        /*
@@ -922,7 +918,6 @@ static bool f2fs_pre_evict_inode(struct inode *inode)
                f2fs_invalidate_compress_pages(sbi, inode->i_ino);
 
        if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-           inode->i_ino == F2FS_META_INO(sbi) ||
            inode->i_ino == F2FS_COMPRESS_INO(sbi))
                return true;
 
fs/f2fs/node.c +78 -79
index f16144b75d11..197ce2f5149a 100644
@@ -140,38 +140,36 @@ static void clear_node_folio_dirty(struct folio *folio)
        folio_clear_uptodate(folio);
 }
 
-static struct folio *get_current_nat_folio(struct f2fs_sb_info *sbi, nid_t nid)
+static struct f2fs_cached_block *get_current_nat_cache(struct f2fs_sb_info *sbi,
+               nid_t nid)
 {
-       return f2fs_get_meta_folio_retry(sbi, current_nat_addr(sbi, nid));
+       return f2fs_get_meta_cache_retry(sbi, current_nat_addr(sbi, nid));
 }
 
-static struct folio *get_next_nat_folio(struct f2fs_sb_info *sbi, nid_t nid)
+static struct f2fs_cached_block *get_next_nat_cache(struct f2fs_sb_info *sbi,
+               nid_t nid)
 {
-       struct folio *src_folio;
-       struct folio *dst_folio;
+       struct f2fs_cached_block *src_entry;
+       struct f2fs_cached_block *dst_entry;
        pgoff_t dst_off;
-       void *src_addr;
-       void *dst_addr;
        struct f2fs_nm_info *nm_i = NM_I(sbi);
 
        dst_off = next_nat_addr(sbi, current_nat_addr(sbi, nid));
 
-       /* get current nat block page with lock */
-       src_folio = get_current_nat_folio(sbi, nid);
-       if (IS_ERR(src_folio))
-               return src_folio;
-       dst_folio = f2fs_grab_meta_folio(sbi, dst_off);
-       f2fs_bug_on(sbi, folio_test_dirty(src_folio));
+       /* get current nat cached block with lock */
+       src_entry = get_current_nat_cache(sbi, nid);
+       if (IS_ERR(src_entry))
+               return src_entry;
+       dst_entry = f2fs_grab_meta_cache(sbi, dst_off);
+       f2fs_bug_on(sbi, f2fs_cache_test_dirty(src_entry));
 
-       src_addr = folio_address(src_folio);
-       dst_addr = folio_address(dst_folio);
-       memcpy(dst_addr, src_addr, F2FS_BLKSIZE(sbi));
-       folio_mark_dirty(dst_folio);
-       f2fs_folio_put(src_folio, true);
+       memcpy(cache_address(dst_entry), cache_address(src_entry), F2FS_BLKSIZE(sbi));
+       f2fs_mark_cache_dirty(dst_entry);
+       f2fs_put_cache(src_entry, true);
 
        set_to_next_nat(sbi, nm_i, nid);
 
-       return dst_folio;
+       return dst_entry;
 }
 
 static struct nat_entry *__alloc_nat_entry(struct f2fs_sb_info *sbi,
@@ -583,7 +581,7 @@ int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
        struct f2fs_journal *journal = curseg->journal;
        nid_t start_nid = f2fs_start_nid(sbi, nid);
        struct f2fs_nat_block *nat_blk;
-       struct folio *folio = NULL;
+       struct f2fs_cached_block *entry = NULL;
        struct f2fs_nat_entry ne;
        struct nat_entry *e;
        pgoff_t index;
@@ -634,18 +632,18 @@ int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
                goto sanity_check;
        }
 
-       /* Fill node_info from nat page */
+       /* Fill node_info from nat block */
        index = current_nat_addr(sbi, nid);
        f2fs_up_read_trace(&nm_i->nat_tree_lock, &lc);
 
-       folio = f2fs_get_meta_folio(sbi, index);
-       if (IS_ERR(folio))
-               return PTR_ERR(folio);
+       entry = f2fs_get_meta_cache(sbi, index);
+       if (IS_ERR(entry))
+               return PTR_ERR(entry);
 
-       nat_blk = folio_address(folio);
+       nat_blk = cache_address(entry);
        ne = nat_blk->entries[nid - start_nid];
        node_info_from_raw_nat(ni, &ne);
-       f2fs_folio_put(folio, true);
+       f2fs_put_cache(entry, true);
 sanity_check:
        if (__is_valid_data_blkaddr(ni->blk_addr) &&
                !f2fs_is_valid_blkaddr(sbi, ni->blk_addr,
@@ -1635,7 +1633,7 @@ static struct folio *__get_node_folio(struct f2fs_sb_info *sbi, pgoff_t nid,
 out_put_err:
        /* ENOENT comes from read_node_folio which is not an error. */
        if (err != -ENOENT)
-               f2fs_handle_page_eio(sbi, folio, NODE);
+               f2fs_handle_page_eio(sbi, folio->index, NODE);
        f2fs_folio_put(folio, true);
        return ERR_PTR(err);
 }
@@ -2468,7 +2466,7 @@ static bool add_free_nid(struct f2fs_sb_info *sbi,
                 *                     - f2fs_balance_fs_bg
                 *                      - f2fs_build_free_nids
                 *                       - __f2fs_build_free_nids
-                *                        - scan_nat_page
+                *                        - scan_nat_blocks
                 *                         - add_free_nid
                 *                          - __lookup_nat_cache
                 *  - f2fs_add_link
@@ -2526,7 +2524,7 @@ static void remove_free_nid(struct f2fs_sb_info *sbi, nid_t nid)
                kmem_cache_free(free_nid_slab, i);
 }
 
-static int scan_nat_page(struct f2fs_sb_info *sbi,
+static int scan_nat_block(struct f2fs_sb_info *sbi,
                        struct f2fs_nat_block *nat_blk, nid_t start_nid)
 {
        struct f2fs_nm_info *nm_i = NM_I(sbi);
@@ -2617,6 +2615,7 @@ static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
                                                bool sync, bool mount)
 {
        struct f2fs_nm_info *nm_i = NM_I(sbi);
+       struct f2fs_cached_block *entry = NULL;
        int i = 0, ret;
        nid_t nid = nm_i->next_scan_nid;
        struct f2fs_lock_context lc;
@@ -2642,8 +2641,8 @@ static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
                        return 0;
        }
 
-       /* readahead nat pages to be scanned */
-       f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(sbi, nid), FREE_NID_PAGES,
+       /* readahead nat blocks to be scanned */
+       f2fs_ra_meta_caches(sbi, NAT_BLOCK_OFFSET(sbi, nid), FREE_NID_BLOCKS,
                                                        META_NAT, true);
 
        f2fs_down_read_trace(&nm_i->nat_tree_lock, &lc);
@@ -2651,14 +2650,14 @@ static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
        while (1) {
                if (!test_bit_le(NAT_BLOCK_OFFSET(sbi, nid),
                                                nm_i->nat_block_bitmap)) {
-                       struct folio *folio = get_current_nat_folio(sbi, nid);
+                       entry = get_current_nat_cache(sbi, nid);
 
-                       if (IS_ERR(folio)) {
-                               ret = PTR_ERR(folio);
+                       if (IS_ERR(entry)) {
+                               ret = PTR_ERR(entry);
                        } else {
-                               ret = scan_nat_page(sbi, folio_address(folio),
+                               ret = scan_nat_block(sbi, cache_address(entry),
                                                nid);
-                               f2fs_folio_put(folio, true);
+                               f2fs_put_cache(entry, true);
                        }
 
                        if (ret) {
@@ -2680,20 +2679,20 @@ static int __f2fs_build_free_nids(struct f2fs_sb_info *sbi,
                if (unlikely(nid >= nm_i->max_nid))
                        nid = 0;
 
-               if (++i >= FREE_NID_PAGES)
+               if (++i >= FREE_NID_BLOCKS)
                        break;
        }
 
-       /* go to the next free nat pages to find free nids abundantly */
+       /* go to the next free nat blocks to find free nids abundantly */
        nm_i->next_scan_nid = nid;
 
-       /* find free nids from current sum_pages */
+       /* find free nids from current sum_blocks */
        scan_curseg_cache(sbi);
 
        f2fs_up_read_trace(&nm_i->nat_tree_lock, &lc);
 
-       f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(sbi, nm_i->next_scan_nid),
-                                       nm_i->ra_nid_pages, META_NAT, false);
+       f2fs_ra_meta_caches(sbi, NAT_BLOCK_OFFSET(sbi, nm_i->next_scan_nid),
+                                       nm_i->ra_nid_blocks, META_NAT, false);
 
        return 0;
 }
@@ -2758,7 +2757,7 @@ bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
        }
        spin_unlock(&nm_i->nid_list_lock);
 
-       /* Let's scan nat pages and its caches to get free nids */
+       /* Let's scan nat blocks and its caches to get free nids */
        if (!f2fs_build_free_nids(sbi, true, false))
                goto retry;
        return false;
@@ -2847,7 +2846,7 @@ int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink)
        return nr - nr_shrink;
 }
 
-int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio)
+int f2fs_recover_inline_xattr(struct inode *inode, struct f2fs_cached_block *entry)
 {
        void *src_addr, *dst_addr;
        size_t inline_size;
@@ -2858,7 +2857,7 @@ int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio)
        if (IS_ERR(ifolio))
                return PTR_ERR(ifolio);
 
-       ri = F2FS_INODE(folio);
+       ri = &CACHED_NODE(entry)->i;
        if (ri->i_inline & F2FS_INLINE_XATTR) {
                if (!f2fs_has_inline_xattr(inode)) {
                        set_inode_flag(inode, FI_INLINE_XATTR);
@@ -2873,7 +2872,7 @@ int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio)
        }
 
        dst_addr = inline_xattr_addr(inode, ifolio);
-       src_addr = inline_xattr_addr(inode, folio);
+       src_addr = inline_xattr_addr(inode, cache_folio(entry));
        inline_size = inline_xattr_size(inode);
 
        f2fs_folio_wait_writeback(ifolio, NODE, true, true);
@@ -2884,7 +2883,7 @@ int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio)
        return 0;
 }
 
-int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio)
+int f2fs_recover_xattr_data(struct inode *inode, struct f2fs_cached_block *entry)
 {
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
        nid_t prev_xnid = F2FS_I(inode)->i_xattr_nid;
@@ -2922,8 +2921,8 @@ int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio)
        f2fs_update_inode_page(inode);
 
        /* 3: update and set xattr node page dirty */
-       if (folio) {
-               memcpy(F2FS_NODE(xfolio), F2FS_NODE(folio),
+       if (entry) {
+               memcpy(F2FS_NODE(xfolio), CACHED_NODE(entry),
                                VALID_XATTR_BLOCK_SIZE(inode));
                folio_mark_dirty(xfolio);
        }
@@ -2932,10 +2931,10 @@ int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio)
        return 0;
 }
 
-int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct folio *folio)
+int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct f2fs_cached_block *entry)
 {
        struct f2fs_inode *src, *dst;
-       nid_t ino = ino_of_node(sbi, folio);
+       nid_t ino = ino_of_node(sbi, cache_folio(entry));
        struct node_info old_ni, new_ni;
        struct folio *ifolio;
        int err;
@@ -2961,7 +2960,7 @@ int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct folio *folio)
        fill_node_footer(sbi, ifolio, ino, ino, 0, true);
        set_cold_node(sbi, ifolio, false);
 
-       src = F2FS_INODE(folio);
+       src = &CACHED_NODE(entry)->i;
        dst = F2FS_INODE(ifolio);
 
        memcpy(dst, src, offsetof(struct f2fs_inode, i_ext));
@@ -3008,34 +3007,34 @@ int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
 {
        struct f2fs_summary *sum_entry;
        block_t addr;
-       int i, idx, last_offset, nrpages;
+       int i, idx, last_offset, nrblocks;
 
        /* scan the node segment */
        last_offset = BLKS_PER_SEG(sbi);
        addr = START_BLOCK(sbi, segno);
        sum_entry = sum_entries(sum);
 
-       for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
-               nrpages = bio_max_segs(last_offset - i);
+       for (i = 0; i < last_offset; i += nrblocks, addr += nrblocks) {
+               nrblocks = bio_max_segs(last_offset - i);
 
-               /* readahead node pages */
-               f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
+               /* readahead node blocks */
+               f2fs_ra_meta_caches(sbi, addr, nrblocks, META_POR, true);
 
-               for (idx = addr; idx < addr + nrpages; idx++) {
-                       struct folio *folio = f2fs_get_tmp_folio(sbi, idx);
+               for (idx = addr; idx < addr + nrblocks; idx++) {
+                       struct f2fs_cached_block *entry =
+                               f2fs_get_tmp_cache(sbi, idx);
 
-                       if (IS_ERR(folio))
-                               return PTR_ERR(folio);
+                       if (IS_ERR(entry))
+                               return PTR_ERR(entry);
 
-                       sum_entry->nid = F2FS_NODE_FOOTER(sbi, folio)->nid;
+                       sum_entry->nid = F2FS_NODE_FOOTER(sbi, cache_folio(entry))->nid;
                        sum_entry->version = 0;
                        sum_entry->ofs_in_node = 0;
                        sum_entry++;
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
                }
 
-               invalidate_mapping_pages(META_MAPPING(sbi), addr,
-                                                       addr + nrpages);
+               f2fs_truncate_meta_caches(sbi, addr, nrblocks);
        }
        return 0;
 }
@@ -3145,12 +3144,12 @@ static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
        bool to_journal = true;
        struct f2fs_nat_block *nat_blk;
        struct nat_entry *ne, *cur;
-       struct folio *folio = NULL;
+       struct f2fs_cached_block *entry = NULL;
 
        /*
         * there are two steps to flush nat entries:
         * #1, flush nat entries to journal in current hot data summary block.
-        * #2, flush nat entries to nat page.
+        * #2, flush nat entries to nat block.
         */
        if (enabled_nat_bits(sbi, cpc) ||
                !__has_cursum_space(sbi, journal, set->entry_cnt, NAT_JOURNAL))
@@ -3159,11 +3158,11 @@ static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
        if (to_journal) {
                down_write(&curseg->journal_rwsem);
        } else {
-               folio = get_next_nat_folio(sbi, start_nid);
-               if (IS_ERR(folio))
-                       return PTR_ERR(folio);
+               entry = get_next_nat_cache(sbi, start_nid);
+               if (IS_ERR(entry))
+                       return PTR_ERR(entry);
 
-               nat_blk = folio_address(folio);
+               nat_blk = cache_address(entry);
                f2fs_bug_on(sbi, !nat_blk);
        }
 
@@ -3200,7 +3199,7 @@ static int __flush_nat_entry_set(struct f2fs_sb_info *sbi,
                up_write(&curseg->journal_rwsem);
        } else {
                __update_nat_bits(sbi, start_nid, nat_blk);
-               f2fs_folio_put(folio, true);
+               f2fs_put_cache(entry, true);
        }
 
        /* Allow dirty nats by node block allocation in write_begin */
@@ -3272,7 +3271,7 @@ int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
                        __has_cursum_space(sbi, journal,
                                        entry_count, NAT_JOURNAL))
                        continue;
-               f2fs_ra_meta_pages(sbi, set->set, 1, META_NAT, true);
+               f2fs_ra_meta_caches(sbi, set->set, 1, META_NAT, true);
        }
        /* flush dirty nats in nat entry set */
        list_for_each_entry_safe(set, tmp, &sets, set_list) {
@@ -3309,15 +3308,15 @@ static int __get_nat_bitmaps(struct f2fs_sb_info *sbi)
        nat_bits_addr = __start_cp_addr(sbi) + BLKS_PER_SEG(sbi) -
                                                nm_i->nat_bits_blocks;
        for (i = 0; i < nm_i->nat_bits_blocks; i++) {
-               struct folio *folio;
+               struct f2fs_cached_block *entry;
 
-               folio = f2fs_get_meta_folio(sbi, nat_bits_addr++);
-               if (IS_ERR(folio))
-                       return PTR_ERR(folio);
+               entry = f2fs_get_meta_cache(sbi, nat_bits_addr++);
+               if (IS_ERR(entry))
+                       return PTR_ERR(entry);
 
                memcpy(nm_i->nat_bits + F2FS_BLK_TO_BYTES(sbi, i),
-                                       folio_address(folio), F2FS_BLKSIZE(sbi));
-               f2fs_folio_put(folio, true);
+                                       cache_address(entry), F2FS_BLKSIZE(sbi));
+               f2fs_put_cache(entry, true);
        }
 
        cp_ver |= (cur_cp_crc(ckpt) << 32);
@@ -3388,7 +3387,7 @@ static int init_node_manager(struct f2fs_sb_info *sbi)
        nm_i->nid_cnt[FREE_NID] = 0;
        nm_i->nid_cnt[PREALLOC_NID] = 0;
        nm_i->ram_thresh = DEF_RAM_THRESHOLD;
-       nm_i->ra_nid_pages = DEF_RA_NID_PAGES;
+       nm_i->ra_nid_blocks = DEF_RA_NID_BLOCKS;
        nm_i->dirty_nats_ratio = DEF_DIRTY_NAT_RATIO_THRESHOLD;
        nm_i->max_rf_node_blocks = DEF_RF_NODE_BLOCKS;
 
fs/f2fs/node.h +4 -4
index 901b9e60fed5..e37fb0d63120 100644
@@ -17,15 +17,15 @@ static inline nid_t f2fs_start_nid(struct f2fs_sb_info *sbi, nid_t nid)
 #define        NAT_BLOCK_OFFSET(sbi, start_nid) \
        ((start_nid) / NAT_ENTRY_PER_BLOCK(sbi))
 
-/* # of pages to perform synchronous readahead before building free nids */
-#define FREE_NID_PAGES 8
+/* # of blocks to perform synchronous readahead before building free nids */
+#define FREE_NID_BLOCKS        8
 #define MAX_FREE_NIDS(sbi)     ((unsigned long)NAT_ENTRY_PER_BLOCK(sbi) * \
-                                FREE_NID_PAGES)
+                                FREE_NID_BLOCKS)
 
 /* size of free nid batch when shrinking */
 #define SHRINK_NID_BATCH_SIZE  8
 
-#define DEF_RA_NID_PAGES       0       /* # of nid pages to be readaheaded */
+#define DEF_RA_NID_BLOCKS      0       /* # of nid blocks to be readaheaded */
 
 /* maximum readahead size for node during getting data blocks */
 #define MAX_RA_NODE            128
fs/f2fs/recovery.c +98 -98
index ba3649e6190e..93564e532f2f 100644
@@ -182,33 +182,33 @@ static const char *recover_printable_name(struct inode *inode,
        return raw->i_name;
 }
 
-static int recover_dentry(struct inode *inode, struct folio *ifolio,
+static int recover_dentry(struct inode *inode, struct f2fs_cached_block *entry,
                                                struct list_head *dir_list)
 {
-       struct f2fs_inode *raw_inode = F2FS_INODE(ifolio);
+       struct f2fs_inode *raw_inode = &CACHED_NODE(entry)->i;
        nid_t pino = le32_to_cpu(raw_inode->i_pino);
        struct f2fs_dir_entry *de;
        struct f2fs_filename fname;
        struct qstr usr_fname;
        struct folio *folio;
        struct inode *dir, *einode;
-       struct fsync_inode_entry *entry;
+       struct fsync_inode_entry *fsync_entry;
        int err = 0;
        const char *name;
        int name_len;
 
-       entry = get_fsync_inode(dir_list, pino);
-       if (!entry) {
-               entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
+       fsync_entry = get_fsync_inode(dir_list, pino);
+       if (!fsync_entry) {
+               fsync_entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
                                                        pino, false);
-               if (IS_ERR(entry)) {
-                       dir = ERR_CAST(entry);
-                       err = PTR_ERR(entry);
+               if (IS_ERR(fsync_entry)) {
+                       dir = ERR_CAST(fsync_entry);
+                       err = PTR_ERR(fsync_entry);
                        goto out;
                }
        }
 
-       dir = entry->inode;
+       dir = fsync_entry->inode;
        err = init_recovered_filename(dir, inode, raw_inode, &fname, &usr_fname);
        if (err)
                goto out;
@@ -256,14 +256,14 @@ static int recover_dentry(struct inode *inode, struct folio *ifolio,
 out:
        name = recover_printable_name(inode, raw_inode, &name_len);
        f2fs_notice(F2FS_I_SB(inode), "%s: ino = %x, name = %.*s, dir = %llu, err = %d",
-                   __func__, ino_of_node(F2FS_I_SB(inode), ifolio), name_len, name,
+                   __func__, ino_of_node(F2FS_I_SB(inode), cache_folio(entry)), name_len, name,
                    IS_ERR(dir) ? 0 : dir->i_ino, err);
        return err;
 }
 
-static int recover_quota_data(struct inode *inode, struct folio *folio)
+static int recover_quota_data(struct inode *inode, struct f2fs_cached_block *entry)
 {
-       struct f2fs_inode *raw = F2FS_INODE(folio);
+       struct f2fs_inode *raw = &CACHED_NODE(entry)->i;
        struct iattr attr;
        uid_t i_uid = le32_to_cpu(raw->i_uid);
        gid_t i_gid = le32_to_cpu(raw->i_gid);
@@ -300,9 +300,9 @@ static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
                clear_inode_flag(inode, FI_DATA_EXIST);
 }
 
-static int recover_inode(struct inode *inode, struct folio *folio)
+static int recover_inode(struct inode *inode, struct f2fs_cached_block *entry)
 {
-       struct f2fs_inode *raw = F2FS_INODE(folio);
+       struct f2fs_inode *raw = &CACHED_NODE(entry)->i;
        struct f2fs_inode_info *fi = F2FS_I(inode);
        const char *name;
        int name_len;
@@ -310,7 +310,7 @@ static int recover_inode(struct inode *inode, struct folio *folio)
 
        inode->i_mode = le16_to_cpu(raw->i_mode);
 
-       err = recover_quota_data(inode, folio);
+       err = recover_quota_data(inode, entry);
        if (err)
                return err;
 
@@ -357,7 +357,7 @@ static int recover_inode(struct inode *inode, struct folio *folio)
        name = recover_printable_name(inode, raw, &name_len);
 
        f2fs_notice(F2FS_I_SB(inode), "%s: ino = %x, name = %.*s, inline = %x",
-                   __func__, ino_of_node(F2FS_I_SB(inode), folio), name_len, name,
+                   __func__, ino_of_node(F2FS_I_SB(inode), cache_folio(entry)), name_len, name,
                    raw->i_inline);
        return 0;
 }
@@ -386,30 +386,30 @@ static int sanity_check_node_chain(struct f2fs_sb_info *sbi, block_t blkaddr,
                return 0;
 
        for (i = 0; i < 2; i++) {
-               struct folio *folio;
+               struct f2fs_cached_block *entry;
 
                if (!f2fs_is_valid_blkaddr(sbi, *blkaddr_fast, META_POR)) {
                        *is_detecting = false;
                        return 0;
                }
 
-               folio = f2fs_get_tmp_folio(sbi, *blkaddr_fast);
-               if (IS_ERR(folio))
-                       return PTR_ERR(folio);
+               entry = f2fs_get_tmp_cache(sbi, *blkaddr_fast);
+               if (IS_ERR(entry))
+                       return PTR_ERR(entry);
 
-               if (!is_recoverable_dnode(sbi, folio)) {
-                       f2fs_folio_put(folio, true);
+               if (!is_recoverable_dnode(sbi, cache_folio(entry))) {
+                       f2fs_put_cache(entry, true);
                        *is_detecting = false;
                        return 0;
                }
 
                ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, *blkaddr_fast,
-                                       next_blkaddr_of_node(sbi, folio));
+                                       next_blkaddr_of_node(sbi, cache_folio(entry)));
 
-               *blkaddr_fast = next_blkaddr_of_node(sbi, folio);
-               f2fs_folio_put(folio, true);
+               *blkaddr_fast = next_blkaddr_of_node(sbi, cache_folio(entry));
+               f2fs_put_cache(entry, true);
 
-               f2fs_ra_meta_pages_cond(sbi, *blkaddr_fast, ra_blocks);
+               f2fs_ra_meta_caches_cond(sbi, *blkaddr_fast, ra_blocks);
        }
 
        if (*blkaddr_fast == blkaddr) {
@@ -434,45 +434,45 @@ static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
        blkaddr_fast = blkaddr;
 
        while (1) {
-               struct fsync_inode_entry *entry;
-               struct folio *folio;
+               struct fsync_inode_entry *fsync_entry;
+               struct f2fs_cached_block *entry;
 
                if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
                        return 0;
 
-               folio = f2fs_get_tmp_folio(sbi, blkaddr);
-               if (IS_ERR(folio)) {
-                       err = PTR_ERR(folio);
+               entry = f2fs_get_tmp_cache(sbi, blkaddr);
+               if (IS_ERR(entry)) {
+                       err = PTR_ERR(entry);
                        break;
                }
 
-               if (!is_recoverable_dnode(sbi, folio)) {
-                       f2fs_folio_put(folio, true);
+               if (!is_recoverable_dnode(sbi, cache_folio(entry))) {
+                       f2fs_put_cache(entry, true);
                        break;
                }
 
-               if (!is_fsync_dnode(sbi, folio))
+               if (!is_fsync_dnode(sbi, cache_folio(entry)))
                        goto next;
 
-               entry = get_fsync_inode(head, ino_of_node(sbi, folio));
-               if (!entry) {
+               fsync_entry = get_fsync_inode(head, ino_of_node(sbi, cache_folio(entry)));
+               if (!fsync_entry) {
                        bool quota_inode = false;
 
                        if (!check_only &&
-                                       IS_INODE(sbi, folio) &&
-                                       is_dent_dnode(sbi, folio)) {
-                               err = f2fs_recover_inode_page(sbi, folio);
+                                       IS_INODE(sbi, cache_folio(entry)) &&
+                                       is_dent_dnode(sbi, cache_folio(entry))) {
+                               err = f2fs_recover_inode_page(sbi, entry);
                                if (err) {
-                                       f2fs_folio_put(folio, true);
+                                       f2fs_put_cache(entry, true);
                                        break;
                                }
                                quota_inode = true;
                        }
 
-                       entry = add_fsync_inode(sbi, head, ino_of_node(sbi, folio),
+                       fsync_entry = add_fsync_inode(sbi, head, ino_of_node(sbi, cache_folio(entry)),
                                                                quota_inode);
-                       if (IS_ERR(entry)) {
-                               err = PTR_ERR(entry);
+                       if (IS_ERR(fsync_entry)) {
+                               err = PTR_ERR(fsync_entry);
                                /*
                                 * CP | dnode(F) | inode(DF)
                                 * For this case, we should not give up now.
@@ -482,18 +482,18 @@ static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
                                                *new_inode = true;
                                        goto next;
                                }
-                               f2fs_folio_put(folio, true);
+                               f2fs_put_cache(entry, true);
                                break;
                        }
                }
-               entry->blkaddr = blkaddr;
+               fsync_entry->blkaddr = blkaddr;
 
-               if (IS_INODE(sbi, folio) && is_dent_dnode(sbi, folio))
-                       entry->last_dentry = blkaddr;
+               if (IS_INODE(sbi, cache_folio(entry)) && is_dent_dnode(sbi, cache_folio(entry)))
+                       fsync_entry->last_dentry = blkaddr;
 next:
                /* check next segment */
-               blkaddr = next_blkaddr_of_node(sbi, folio);
-               f2fs_folio_put(folio, true);
+               blkaddr = next_blkaddr_of_node(sbi, cache_folio(entry));
+               f2fs_put_cache(entry, true);
 
                err = sanity_check_node_chain(sbi, blkaddr, &blkaddr_fast,
                                &is_detecting);
@@ -519,7 +519,8 @@ static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
        unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
        struct f2fs_summary_block *sum_node;
        struct f2fs_summary sum;
-       struct folio *sum_folio, *node_folio;
+       struct f2fs_cached_block *entry = NULL;
+       struct folio *node_folio;
        struct dnode_of_data tdn = *dn;
        nid_t ino, nid;
        struct inode *inode;
@@ -541,12 +542,12 @@ static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
                }
        }
 
-       sum_folio = f2fs_get_sum_folio(sbi, segno);
-       if (IS_ERR(sum_folio))
-               return PTR_ERR(sum_folio);
-       sum_node = SUM_BLK_PAGE_ADDR(sbi, sum_folio, segno);
+       entry = f2fs_get_sum_cache(sbi, segno);
+       if (IS_ERR(entry))
+               return PTR_ERR(entry);
+       sum_node = SUM_BLK_ENTRY_ADDR(sbi, entry, segno);
        sum = sum_entries(sum_node)[blkoff];
-       f2fs_folio_put(sum_folio, true);
+       f2fs_put_cache(entry, true);
 got_it:
        /* Use the locked dnode page and inode */
        nid = le32_to_cpu(sum.nid);
@@ -645,7 +646,7 @@ static int f2fs_reserve_new_block_retry(struct dnode_of_data *dn)
 }
 
 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
-                                       struct folio *folio)
+                                       struct f2fs_cached_block *entry)
 {
        struct dnode_of_data dn;
        struct node_info ni;
@@ -653,19 +654,19 @@ static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
        int err = 0, recovered = 0;
 
        /* step 1: recover xattr */
-       if (IS_INODE(sbi, folio)) {
-               err = f2fs_recover_inline_xattr(inode, folio);
+       if (IS_INODE(sbi, cache_folio(entry))) {
+               err = f2fs_recover_inline_xattr(inode, entry);
                if (err)
                        goto out;
-       } else if (f2fs_has_xattr_block(ofs_of_node(sbi, folio))) {
-               err = f2fs_recover_xattr_data(inode, folio);
+       } else if (f2fs_has_xattr_block(ofs_of_node(sbi, cache_folio(entry)))) {
+               err = f2fs_recover_xattr_data(inode, entry);
                if (!err)
                        recovered++;
                goto out;
        }
 
        /* step 2: recover inline data */
-       err = f2fs_recover_inline_data(inode, folio);
+       err = f2fs_recover_inline_data(inode, entry);
        if (err) {
                if (err == 1)
                        err = 0;
@@ -673,8 +674,8 @@ static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
        }
 
        /* step 3: recover data indices */
-       start = f2fs_start_bidx_of_node(ofs_of_node(sbi, folio), inode);
-       end = start + ADDRS_PER_PAGE(folio, inode);
+       start = f2fs_start_bidx_of_node(ofs_of_node(sbi, cache_folio(entry)), inode);
+       end = start + addrs_per_page(inode, IS_INODE(sbi, cache_folio(entry)));
 
        set_new_dnode(&dn, inode, NULL, NULL, 0);
 retry_dn:
@@ -693,12 +694,12 @@ static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
        if (err)
                goto err;
 
-       f2fs_bug_on(sbi, ni.ino != ino_of_node(sbi, folio));
+       f2fs_bug_on(sbi, ni.ino != ino_of_node(sbi, cache_folio(entry)));
 
-       if (ofs_of_node(sbi, dn.node_folio) != ofs_of_node(sbi, folio)) {
+       if (ofs_of_node(sbi, dn.node_folio) != ofs_of_node(sbi, cache_folio(entry))) {
                f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%llu, ofs:%u, %u",
                          inode->i_ino, ofs_of_node(sbi, dn.node_folio),
-                         ofs_of_node(sbi, folio));
+                         ofs_of_node(sbi, cache_folio(entry)));
                err = -EFSCORRUPTED;
                f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
                fserror_report_file_metadata(dn.inode, err, GFP_NOFS);
@@ -709,7 +710,7 @@ static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
                block_t src, dest;
 
                src = f2fs_data_blkaddr(&dn);
-               dest = data_blkaddr(dn.inode, folio, dn.ofs_in_node);
+               dest = data_blkaddr(dn.inode, cache_folio(entry), dn.ofs_in_node);
 
                if (__is_valid_data_blkaddr(src) &&
                        !f2fs_is_valid_blkaddr(sbi, src, META_POR)) {
@@ -784,16 +785,16 @@ static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
                }
        }
 
-       copy_node_footer(sbi, dn.node_folio, folio);
+       copy_node_footer(sbi, dn.node_folio, cache_folio(entry));
        fill_node_footer(sbi, dn.node_folio, dn.nid, ni.ino,
-                                       ofs_of_node(sbi, folio), false);
+                                       ofs_of_node(sbi, cache_folio(entry)), false);
        folio_mark_dirty(dn.node_folio);
 err:
        f2fs_put_dnode(&dn);
 out:
        f2fs_notice(sbi, "recover_data: ino = %llx, nid = %x (i_size: %s), "
                    "range (%u, %u), recovered = %d, err = %d",
-                   inode->i_ino, nid_of_node(sbi, folio),
+                   inode->i_ino, nid_of_node(sbi, cache_folio(entry)),
                    file_keep_isize(inode) ? "keep" : "recover",
                    start, end, recovered, err);
        return err;
@@ -820,26 +821,26 @@ static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
        blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
 
        while (1) {
-               struct fsync_inode_entry *entry;
-               struct folio *folio;
+               struct fsync_inode_entry *fsync_entry;
+               struct f2fs_cached_block *entry;
 
                if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
                        break;
 
-               folio = f2fs_get_tmp_folio(sbi, blkaddr);
-               if (IS_ERR(folio)) {
-                       err = PTR_ERR(folio);
+               entry = f2fs_get_tmp_cache(sbi, blkaddr);
+               if (IS_ERR(entry)) {
+                       err = PTR_ERR(entry);
                        break;
                }
 
-               if (!is_recoverable_dnode(sbi, folio)) {
-                       f2fs_folio_put(folio, true);
+               if (!is_recoverable_dnode(sbi, cache_folio(entry))) {
+                       f2fs_put_cache(entry, true);
                        break;
                }
                recoverable_dnode++;
 
-               entry = get_fsync_inode(inode_list, ino_of_node(sbi, folio));
-               if (!entry)
+               fsync_entry = get_fsync_inode(inode_list, ino_of_node(sbi, cache_folio(entry)));
+               if (!fsync_entry)
                        goto next;
                fsynced_dnode++;
                /*
@@ -847,40 +848,40 @@ static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
                 * In this case, we can lose the latest inode(x).
                 * So, call recover_inode for the inode update.
                 */
-               if (IS_INODE(sbi, folio)) {
-                       err = recover_inode(entry->inode, folio);
+               if (IS_INODE(sbi, cache_folio(entry))) {
+                       err = recover_inode(fsync_entry->inode, entry);
                        if (err) {
-                               f2fs_folio_put(folio, true);
+                               f2fs_put_cache(entry, true);
                                break;
                        }
                        recovered_inode++;
                }
-               if (entry->last_dentry == blkaddr) {
-                       err = recover_dentry(entry->inode, folio, dir_list);
+               if (fsync_entry->last_dentry == blkaddr) {
+                       err = recover_dentry(fsync_entry->inode, entry, dir_list);
                        if (err) {
-                               f2fs_folio_put(folio, true);
+                               f2fs_put_cache(entry, true);
                                break;
                        }
                        recovered_dentry++;
                }
-               err = do_recover_data(sbi, entry->inode, folio);
+               err = do_recover_data(sbi, fsync_entry->inode, entry);
                if (err) {
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
                        break;
                }
                recovered_dnode++;
 
-               if (entry->blkaddr == blkaddr)
-                       list_move_tail(&entry->list, tmp_inode_list);
+               if (fsync_entry->blkaddr == blkaddr)
+                       list_move_tail(&fsync_entry->list, tmp_inode_list);
 next:
                ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, blkaddr,
-                                       next_blkaddr_of_node(sbi, folio));
+                                       next_blkaddr_of_node(sbi, cache_folio(entry)));
 
                /* check next segment */
-               blkaddr = next_blkaddr_of_node(sbi, folio);
-               f2fs_folio_put(folio, true);
+               blkaddr = next_blkaddr_of_node(sbi, cache_folio(entry));
+               f2fs_put_cache(entry, true);
 
-               f2fs_ra_meta_pages_cond(sbi, blkaddr, ra_blocks);
+               f2fs_ra_meta_caches_cond(sbi, blkaddr, ra_blocks);
                total_dnode++;
        }
        if (!err)
@@ -937,12 +938,11 @@ int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
        destroy_fsync_dnodes(&tmp_inode_list, err);
 
        /* truncate meta pages to be used by the recovery */
-       truncate_inode_pages_range(META_MAPPING(sbi),
-                       (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
-
+       f2fs_truncate_meta_caches(sbi, MAIN_BLKADDR(sbi),
+                               MAX_BLKADDR(sbi) - MAIN_BLKADDR(sbi));
        if (err) {
                truncate_inode_pages_final(NODE_MAPPING(sbi));
-               truncate_inode_pages_final(META_MAPPING(sbi));
+               f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX);
        }
 
        /*
fs/f2fs/segment.c +123 -120
index ffdd2c510b3c..9d08e762b131 100644
@@ -2805,12 +2805,12 @@ static unsigned short f2fs_curseg_valid_blocks(struct f2fs_sb_info *sbi, int typ
 }
 
 /*
- * Calculate the number of current summary pages for writing
+ * Calculate the number of current summary blocks for writing
  */
-int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
+int f2fs_nblocks_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
 {
        int valid_sum_count = 0;
-       int i, sum_in_page;
+       int i, sum_in_block;
 
        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
                if (sbi->ckpt->alloc_type[i] != SSR && for_ra)
@@ -2820,73 +2820,72 @@ int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
                        valid_sum_count += f2fs_curseg_valid_blocks(sbi, i);
        }
 
-       sum_in_page = (sbi->blocksize - 2 * sbi->sum_journal_size -
+       sum_in_block = (sbi->blocksize - 2 * sbi->sum_journal_size -
                        SUM_FOOTER_SIZE) / SUMMARY_SIZE;
-       if (valid_sum_count <= sum_in_page)
+       if (valid_sum_count <= sum_in_block)
                return 1;
-       else if ((valid_sum_count - sum_in_page) <=
+       else if ((valid_sum_count - sum_in_block) <=
                (sbi->blocksize - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
                return 2;
        return 3;
 }
 
-/*
- * Caller should put this summary folio
- */
-struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno)
+
+struct f2fs_cached_block *f2fs_get_sum_cache(struct f2fs_sb_info *sbi,
+               unsigned int segno)
 {
        if (unlikely(f2fs_cp_error(sbi)))
                return ERR_PTR(-EIO);
-       return f2fs_get_meta_folio_retry(sbi, GET_SUM_BLOCK(sbi, segno));
+       return f2fs_get_meta_cache_retry(sbi, GET_SUM_BLOCK(sbi, segno));
 }
 
-void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
+void f2fs_update_meta_block(struct f2fs_sb_info *sbi,
                                        void *src, block_t blk_addr)
 {
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
 
        if (!f2fs_sb_has_packed_ssa(sbi))
-               folio = f2fs_grab_meta_folio(sbi, blk_addr);
+               entry = f2fs_grab_meta_cache(sbi, blk_addr);
        else
-               folio = f2fs_get_meta_folio_retry(sbi, blk_addr);
+               entry = f2fs_get_meta_cache_retry(sbi, blk_addr);
 
-       if (IS_ERR(folio))
+       if (IS_ERR(entry))
                return;
 
-       memcpy(folio_address(folio), src, F2FS_BLKSIZE(sbi));
-       folio_mark_dirty(folio);
-       f2fs_folio_put(folio, true);
+       memcpy(cache_address(entry), src, F2FS_BLKSIZE(sbi));
+       f2fs_mark_cache_dirty(entry);
+       f2fs_put_cache(entry, true);
 }
 
-static void write_sum_page(struct f2fs_sb_info *sbi,
+static void write_sum_block(struct f2fs_sb_info *sbi,
                struct f2fs_summary_block *sum_blk, unsigned int segno)
 {
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
 
        if (!f2fs_sb_has_packed_ssa(sbi))
-               return f2fs_update_meta_page(sbi, (void *)sum_blk,
+               return f2fs_update_meta_block(sbi, (void *)sum_blk,
                                GET_SUM_BLOCK(sbi, segno));
 
-       folio = f2fs_get_sum_folio(sbi, segno);
-       if (IS_ERR(folio))
+       entry = f2fs_get_sum_cache(sbi, segno);
+       if (IS_ERR(entry))
                return;
 
-       memcpy(SUM_BLK_PAGE_ADDR(sbi, folio, segno), sum_blk,
+       memcpy(SUM_BLK_ENTRY_ADDR(sbi, entry, segno), sum_blk,
                        sbi->sum_blocksize);
-       folio_mark_dirty(folio);
-       f2fs_folio_put(folio, true);
+       f2fs_mark_cache_dirty(entry);
+       f2fs_put_cache(entry, true);
 }
 
-static void write_current_sum_page(struct f2fs_sb_info *sbi,
+static void write_current_sum_block(struct f2fs_sb_info *sbi,
                                                int type, block_t blk_addr)
 {
        struct curseg_info *curseg = CURSEG_I(sbi, type);
-       struct folio *folio = f2fs_grab_meta_folio(sbi, blk_addr);
+       struct f2fs_cached_block *entry = f2fs_grab_meta_cache(sbi, blk_addr);
        struct f2fs_summary_block *src = curseg->sum_blk;
        struct f2fs_summary_block *dst;
 
-       dst = folio_address(folio);
-       memset(dst, 0, PAGE_SIZE);
+       dst = cache_address(entry);
+       memset(dst, 0, sbi->blocksize);
 
        mutex_lock(&curseg->curseg_mutex);
 
@@ -2899,8 +2898,8 @@ static void write_current_sum_page(struct f2fs_sb_info *sbi,
 
        mutex_unlock(&curseg->curseg_mutex);
 
-       folio_mark_dirty(folio);
-       f2fs_folio_put(folio, true);
+       f2fs_mark_cache_dirty(entry);
+       f2fs_put_cache(entry, true);
 }
 
 static int is_next_segment_free(struct f2fs_sb_info *sbi,
@@ -3160,7 +3159,7 @@ static int new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
        int ret;
 
        if (curseg->inited)
-               write_sum_page(sbi, curseg->sum_blk, segno);
+               write_sum_block(sbi, curseg->sum_blk, segno);
 
        segno = __get_next_segno(sbi, type);
        ret = get_new_segment(sbi, &segno, new_sec, pinning);
@@ -3216,10 +3215,10 @@ static int change_curseg(struct f2fs_sb_info *sbi, int type)
        struct curseg_info *curseg = CURSEG_I(sbi, type);
        unsigned int new_segno = curseg->next_segno;
        struct f2fs_summary_block *sum_node;
-       struct folio *sum_folio;
+       struct f2fs_cached_block *entry = NULL;
 
        if (curseg->inited)
-               write_sum_page(sbi, curseg->sum_blk, curseg->segno);
+               write_sum_block(sbi, curseg->sum_blk, curseg->segno);
 
        __set_test_and_inuse(sbi, new_segno);
 
@@ -3232,15 +3231,15 @@ static int change_curseg(struct f2fs_sb_info *sbi, int type)
        curseg->alloc_type = SSR;
        curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
 
-       sum_folio = f2fs_get_sum_folio(sbi, new_segno);
-       if (IS_ERR(sum_folio)) {
-               /* GC won't be able to use stale summary pages by cp_error */
+       entry = f2fs_get_sum_cache(sbi, new_segno);
+       if (IS_ERR(entry)) {
+               /* GC won't be able to use stale summary blocks by cp_error */
                memset(curseg->sum_blk, 0, sbi->sum_entry_size);
-               return PTR_ERR(sum_folio);
+               return PTR_ERR(entry);
        }
-       sum_node = SUM_BLK_PAGE_ADDR(sbi, sum_folio, new_segno);
+       sum_node = SUM_BLK_ENTRY_ADDR(sbi, entry, new_segno);
        memcpy(curseg->sum_blk, sum_node, sbi->sum_entry_size);
-       f2fs_folio_put(sum_folio, true);
+       f2fs_put_cache(entry, true);
        return 0;
 }
 
@@ -3327,7 +3326,7 @@ static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
                goto out;
 
        if (get_valid_blocks(sbi, curseg->segno, false)) {
-               write_sum_page(sbi, curseg->sum_blk, curseg->segno);
+               write_sum_block(sbi, curseg->sum_blk, curseg->segno);
        } else {
                mutex_lock(&DIRTY_I(sbi)->seglist_lock);
                __set_test_and_free(sbi, curseg->segno, true);
@@ -4023,7 +4022,7 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
        if (segment_full) {
                if (type == CURSEG_COLD_DATA_PINNED &&
                    !((curseg->segno + 1) % sbi->segs_per_sec)) {
-                       write_sum_page(sbi, curseg->sum_blk, curseg->segno);
+                       write_sum_block(sbi, curseg->sum_blk, curseg->segno);
                        reset_curseg_fields(curseg);
                        goto skip_new_segment;
                }
@@ -4141,7 +4140,7 @@ static int log_type_to_seg_type(enum log_type type)
        return seg_type;
 }
 
-static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
+static void do_write_block(struct f2fs_summary *sum, struct f2fs_io_info *fio)
 {
        struct folio *folio = fio->folio;
        enum log_type type = __get_segment_type(fio);
@@ -4183,8 +4182,9 @@ static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
                f2fs_up_read(&fio->sbi->io_order_lock);
 }
 
-void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio,
-                                       enum iostat_type io_type)
+void f2fs_do_write_meta_cache(struct f2fs_sb_info *sbi,
+                       struct f2fs_cached_block *entry,
+                       enum iostat_type io_type)
 {
        struct f2fs_io_info fio = {
                .sbi = sbi,
@@ -4192,20 +4192,21 @@ void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio,
                .temp = HOT,
                .op = REQ_OP_WRITE,
                .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
-               .old_blkaddr = folio->index,
-               .new_blkaddr = folio->index,
-               .folio = folio,
+               .old_blkaddr = entry->index,
+               .new_blkaddr = entry->index,
                .encrypted_page = NULL,
                .in_list = 0,
+               .cache_entry = entry,
+               .is_cache = 1,
        };
 
-       if (unlikely(folio->index >= MAIN_BLKADDR(sbi)))
+       if (unlikely(entry->index >= MAIN_BLKADDR(sbi)))
                fio.op_flags &= ~REQ_META;
 
-       folio_start_writeback(folio);
-       f2fs_submit_page_write(&fio);
+       f2fs_start_cache_writeback(entry);
+       f2fs_submit_cache_write(&fio);
 
-       stat_inc_meta_count(sbi, folio->index);
+       stat_inc_meta_count(sbi, entry->index);
        f2fs_update_iostat(sbi, NULL, io_type, F2FS_BLKSIZE(sbi));
 }
 
@@ -4214,7 +4215,7 @@ void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
        struct f2fs_summary sum;
 
        set_summary(&sum, nid, 0, 0);
-       do_write_page(&sum, fio);
+       do_write_block(&sum, fio);
 
        f2fs_update_iostat(fio->sbi, NULL, fio->io_type,
                                        F2FS_BLKSIZE(fio->sbi));
@@ -4230,7 +4231,7 @@ void f2fs_outplace_write_data(struct dnode_of_data *dn,
        if (fio->io_type == FS_DATA_IO || fio->io_type == FS_CP_DATA_IO)
                f2fs_update_age_extent_cache(dn);
        set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
-       do_write_page(&sum, fio);
+       do_write_block(&sum, fio);
        f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
 
        f2fs_update_iostat(sbi, dn->inode, fio->io_type,
@@ -4264,7 +4265,7 @@ int f2fs_inplace_write_data(struct f2fs_io_info *fio)
        }
 
        if (fio->meta_gc)
-               f2fs_truncate_meta_inode_pages(sbi, fio->new_blkaddr, 1);
+               f2fs_truncate_meta_caches(sbi, fio->new_blkaddr, 1);
 
        stat_inc_inplace_blocks(fio->sbi);
 
@@ -4430,7 +4431,7 @@ void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type,
 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
 {
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-       struct folio *cfolio;
+       struct f2fs_cached_block *entry;
 
        if (!f2fs_meta_inode_gc_required(inode))
                return;
@@ -4438,11 +4439,12 @@ void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
        if (!__is_valid_data_blkaddr(blkaddr))
                return;
 
-       cfolio = filemap_lock_folio(META_MAPPING(sbi), blkaddr);
-       if (!IS_ERR(cfolio)) {
-               f2fs_folio_wait_writeback(cfolio, DATA, true, true);
-               f2fs_folio_put(cfolio, true);
-       }
+       entry = f2fs_find_cache(META_CACHE(sbi), blkaddr, 0);
+       if (IS_ERR(entry))
+               return;
+       f2fs_lock_cache(entry);
+       f2fs_cache_wait_writeback_cond(entry, DATA);
+       f2fs_put_cache(entry, true);
 }
 
 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
@@ -4457,7 +4459,7 @@ void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
        for (i = 0; i < len; i++)
                f2fs_wait_on_block_writeback(inode, blkaddr + i);
 
-       f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
+       f2fs_truncate_meta_caches(sbi, blkaddr, len);
 }
 
 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
@@ -4465,16 +4467,16 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)
        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
        struct curseg_info *seg_i;
        unsigned char *kaddr;
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
        block_t start;
        int i, j, offset;
 
        start = start_sum_block(sbi);
 
-       folio = f2fs_get_meta_folio(sbi, start++);
-       if (IS_ERR(folio))
-               return PTR_ERR(folio);
-       kaddr = folio_address(folio);
+       entry = f2fs_get_meta_cache(sbi, start++);
+       if (IS_ERR(entry))
+               return PTR_ERR(entry);
+       kaddr = cache_address(entry);
 
        /* Step 1: restore nat cache */
        seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
@@ -4511,16 +4513,16 @@ static int read_compacted_summaries(struct f2fs_sb_info *sbi)
                                                SUM_FOOTER_SIZE)
                                continue;
 
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
 
-                       folio = f2fs_get_meta_folio(sbi, start++);
-                       if (IS_ERR(folio))
-                               return PTR_ERR(folio);
-                       kaddr = folio_address(folio);
+                       entry = f2fs_get_meta_cache(sbi, start++);
+                       if (IS_ERR(entry))
+                               return PTR_ERR(entry);
+                       kaddr = cache_address(entry);
                        offset = 0;
                }
        }
-       f2fs_folio_put(folio, true);
+       f2fs_put_cache(entry, true);
        return 0;
 }
 
@@ -4529,7 +4531,7 @@ static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
        struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
        struct f2fs_summary_block *sum;
        struct curseg_info *curseg;
-       struct folio *new;
+       struct f2fs_cached_block *entry;
        unsigned short blk_off;
        unsigned int segno = 0;
        block_t blk_addr = 0;
@@ -4556,10 +4558,10 @@ static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
                        blk_addr = GET_SUM_BLOCK(sbi, segno);
        }
 
-       new = f2fs_get_meta_folio(sbi, blk_addr);
-       if (IS_ERR(new))
-               return PTR_ERR(new);
-       sum = folio_address(new);
+       entry = f2fs_get_meta_cache(sbi, blk_addr);
+       if (IS_ERR(entry))
+               return PTR_ERR(entry);
+       sum = cache_address(entry);
 
        if (IS_NODESEG(type)) {
                if (__exist_node_summaries(sbi)) {
@@ -4596,7 +4598,7 @@ static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
        curseg->next_blkoff = blk_off;
        mutex_unlock(&curseg->curseg_mutex);
 out:
-       f2fs_folio_put(new, true);
+       f2fs_put_cache(entry, true);
        return err;
 }
 
@@ -4608,11 +4610,11 @@ static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
        int err;
 
        if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
-               int npages = f2fs_npages_for_summary_flush(sbi, true);
+               int nblocks = f2fs_nblocks_for_summary_flush(sbi, true);
 
-               if (npages >= 2)
-                       f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
-                                                       META_CP, true);
+               if (nblocks >= 2)
+                       f2fs_ra_meta_caches(sbi, start_sum_block(sbi),
+                                       nblocks, META_CP, true);
 
                /* restore for compacted data summary */
                err = read_compacted_summaries(sbi);
@@ -4622,7 +4624,7 @@ static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
        }
 
        if (__exist_node_summaries(sbi))
-               f2fs_ra_meta_pages(sbi,
+               f2fs_ra_meta_caches(sbi,
                                sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
                                NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
 
@@ -4645,16 +4647,16 @@ static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
 
 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
 {
-       struct folio *folio;
+       struct f2fs_cached_block *entry = NULL;
        unsigned char *kaddr;
        struct f2fs_summary *summary;
        struct curseg_info *seg_i;
        int written_size = 0;
        int i, j;
 
-       folio = f2fs_grab_meta_folio(sbi, blkaddr++);
-       kaddr = folio_address(folio);
-       memset(kaddr, 0, PAGE_SIZE);
+       entry = f2fs_grab_meta_cache(sbi, blkaddr++);
+       kaddr = cache_address(entry);
+       memset(kaddr, 0, sbi->blocksize);
 
        /* Step 1: write nat cache */
        seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
@@ -4670,10 +4672,10 @@ static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
                seg_i = CURSEG_I(sbi, i);
                for (j = 0; j < f2fs_curseg_valid_blocks(sbi, i); j++) {
-                       if (!folio) {
-                               folio = f2fs_grab_meta_folio(sbi, blkaddr++);
-                               kaddr = folio_address(folio);
-                               memset(kaddr, 0, PAGE_SIZE);
+                       if (!entry) {
+                               entry = f2fs_grab_meta_cache(sbi, blkaddr++);
+                               kaddr = cache_address(entry);
+                               memset(kaddr, 0, sbi->blocksize);
                                written_size = 0;
                        }
                        summary = (struct f2fs_summary *)(kaddr + written_size);
@@ -4684,14 +4686,14 @@ static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
                                                        SUM_FOOTER_SIZE)
                                continue;
 
-                       folio_mark_dirty(folio);
-                       f2fs_folio_put(folio, true);
-                       folio = NULL;
+                       f2fs_mark_cache_dirty(entry);
+                       f2fs_put_cache(entry, true);
+                       entry = NULL;
                }
        }
-       if (folio) {
-               folio_mark_dirty(folio);
-               f2fs_folio_put(folio, true);
+       if (entry) {
+               f2fs_mark_cache_dirty(entry);
+               f2fs_put_cache(entry, true);
        }
 }
 
@@ -4706,7 +4708,7 @@ static void write_normal_summaries(struct f2fs_sb_info *sbi,
                end = type + NR_CURSEG_NODE_TYPE;
 
        for (i = type; i < end; i++)
-               write_current_sum_page(sbi, i, blkaddr + (i - type));
+               write_current_sum_block(sbi, i, blkaddr + (i - type));
 }
 
 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
@@ -4745,29 +4747,29 @@ int f2fs_lookup_journal_in_cursum(struct f2fs_sb_info *sbi,
        return -1;
 }
 
-static struct folio *get_current_sit_folio(struct f2fs_sb_info *sbi,
+static struct f2fs_cached_block *get_current_sit_cache(struct f2fs_sb_info *sbi,
                                        unsigned int segno)
 {
-       return f2fs_get_meta_folio(sbi, current_sit_addr(sbi, segno));
+       return f2fs_get_meta_cache(sbi, current_sit_addr(sbi, segno));
 }
 
-static struct folio *get_next_sit_folio(struct f2fs_sb_info *sbi,
+static struct f2fs_cached_block *get_next_sit_cache(struct f2fs_sb_info *sbi,
                                        unsigned int start)
 {
        struct sit_info *sit_i = SIT_I(sbi);
-       struct folio *folio;
+       struct f2fs_cached_block *entry;
        pgoff_t src_off, dst_off;
 
        src_off = current_sit_addr(sbi, start);
        dst_off = next_sit_addr(sbi, src_off);
 
-       folio = f2fs_grab_meta_folio(sbi, dst_off);
-       seg_info_to_sit_folio(sbi, folio, start);
+       entry = f2fs_grab_meta_cache(sbi, dst_off);
+       seg_info_to_sit_block(sbi, entry, start);
 
-       folio_mark_dirty(folio);
+       f2fs_mark_cache_dirty(entry);
        set_to_next_sit(sbi, sit_i, start);
 
-       return folio;
+       return entry;
 }
 
 static struct sit_entry_set *grab_sit_entry_set(void)
@@ -4898,8 +4900,9 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
         * #2, flush sit entries to sit page.
         */
        list_for_each_entry_safe(ses, tmp, head, set_list) {
-               struct folio *folio = NULL;
+               struct f2fs_cached_block *entry = NULL;
                struct f2fs_sit_block *raw_sit = NULL;
+
                unsigned int start_segno = ses->start_segno;
                unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK(sbi),
                                                (unsigned long)MAIN_SEGS(sbi));
@@ -4913,8 +4916,8 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
                if (to_journal) {
                        down_write(&curseg->journal_rwsem);
                } else {
-                       folio = get_next_sit_folio(sbi, start_segno);
-                       raw_sit = folio_address(folio);
+                       entry = get_next_sit_cache(sbi, start_segno);
+                       raw_sit = cache_address(entry);
                }
 
                /* flush dirty sit entries in region of current sit set */
@@ -4959,7 +4962,7 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
                if (to_journal)
                        up_write(&curseg->journal_rwsem);
                else
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
 
                f2fs_bug_on(sbi, ses->entry_cnt);
                release_sit_entry_set(ses);
@@ -5150,7 +5153,7 @@ static int build_sit_entries(struct f2fs_sb_info *sbi)
        block_t sit_valid_blocks[2] = {0, 0};
 
        do {
-               readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
+               readed = f2fs_ra_meta_caches(sbi, start_blk, BIO_MAX_VECS,
                                                        META_SIT, true);
 
                start = start_blk * sit_i->sents_per_block;
@@ -5158,15 +5161,15 @@ static int build_sit_entries(struct f2fs_sb_info *sbi)
 
                for (; start < end && start < MAIN_SEGS(sbi); start++) {
                        struct f2fs_sit_block *sit_blk;
-                       struct folio *folio;
+                       struct f2fs_cached_block *entry;
 
                        se = &sit_i->sentries[start];
-                       folio = get_current_sit_folio(sbi, start);
-                       if (IS_ERR(folio))
-                               return PTR_ERR(folio);
-                       sit_blk = folio_address(folio);
+                       entry = get_current_sit_cache(sbi, start);
+                       if (IS_ERR(entry))
+                               return PTR_ERR(entry);
+                       sit_blk = cache_address(entry);
                        sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
-                       f2fs_folio_put(folio, true);
+                       f2fs_put_cache(entry, true);
 
                        err = check_block_count(sbi, start, &sit);
                        if (err)
fs/f2fs/segment.h +26 -3
index cc0428b614da..3083069c9092 100644
@@ -96,6 +96,8 @@ static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
 #define GET_SUM_BLKOFF(sbi, segno) (segno % (sbi)->sums_per_block)
 #define SUM_BLK_PAGE_ADDR(sbi, folio, segno)   \
        (folio_address(folio) + GET_SUM_BLKOFF(sbi, segno) * (sbi)->sum_blocksize)
+#define SUM_BLK_ENTRY_ADDR(sbi, entry, segno)  \
+       (cache_address(entry) + GET_SUM_BLKOFF(sbi, segno) * (sbi)->sum_blocksize)
 
 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
@@ -421,8 +423,8 @@ static inline void __seg_info_to_raw_sit(struct seg_entry *se,
        rs->mtime = cpu_to_le64(se->mtime);
 }
 
-static inline void seg_info_to_sit_folio(struct f2fs_sb_info *sbi,
-                               struct folio *folio, unsigned int start)
+static inline void seg_info_to_sit_block(struct f2fs_sb_info *sbi,
+                       struct f2fs_cached_block *entry, unsigned int start)
 {
        struct f2fs_sit_block *raw_sit;
        struct seg_entry *se;
@@ -431,7 +433,7 @@ static inline void seg_info_to_sit_folio(struct f2fs_sb_info *sbi,
                                        (unsigned long)MAIN_SEGS(sbi));
        int i;
 
-       raw_sit = folio_address(folio);
+       raw_sit = cache_address(entry);
        memset(raw_sit, 0, F2FS_BLKSIZE(sbi));
        for (i = 0; i < end - start; i++) {
                rs = &raw_sit->entries[i];
@@ -996,6 +998,27 @@ static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
                return 0;
 }
 
+/*
+ * When writing cache asynchronously, align nr_to_write to BIO_MAX_VECS.
+ */
+static inline long adjust_flush_cache_number(struct f2fs_sb_info *sbi, int type)
+{
+       long nr_to_write;
+
+       switch (type) {
+       case META:
+               nr_to_write = BIO_MAX_VECS;
+               break;
+       case NODE:
+               nr_to_write = BIO_MAX_VECS << 1;
+               break;
+       default:
+               f2fs_bug_on(sbi, 1);
+               return 0;
+       }
+       return nr_to_write;
+}
+
 /*
  * When writing pages, it'd better align nr_to_write for segment size.
  */
fs/f2fs/super.c +8 -25
index 516b4ee12564..0361e4cc0e0a 100644
@@ -1880,8 +1880,7 @@ static int f2fs_drop_inode(struct inode *inode)
         * drop useless meta/node dirty pages.
         */
        if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
-               if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-                       inode->i_ino == F2FS_META_INO(sbi)) {
+               if (inode->i_ino == F2FS_NODE_INO(sbi)) {
                        trace_f2fs_drop_inode(inode, 1);
                        return 1;
                }
@@ -1983,8 +1982,7 @@ static void f2fs_dirty_inode(struct inode *inode, int flags)
 {
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 
-       if (inode->i_ino == F2FS_NODE_INO(sbi) ||
-                       inode->i_ino == F2FS_META_INO(sbi))
+       if (inode->i_ino == F2FS_NODE_INO(sbi))
                return;
 
        if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
@@ -2078,11 +2076,12 @@ static void f2fs_put_super(struct super_block *sb)
        /* our cp_error case, we can wait for any writeback page */
        f2fs_flush_merged_writes(sbi);
 
-       f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
+       f2fs_sync_dirty_data(sbi, F2FS_WB_CP_DATA);
 
-       if (err || f2fs_cp_error(sbi)) {
+       if (err || f2fs_cp_error(sbi) ||
+               unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
                truncate_inode_pages_final(NODE_MAPPING(sbi));
-               truncate_inode_pages_final(META_MAPPING(sbi));
+               f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX);
        }
 
        f2fs_bug_on(sbi, sbi->fsync_node_num);
@@ -2092,9 +2091,6 @@ static void f2fs_put_super(struct super_block *sb)
        iput(sbi->node_inode);
        sbi->node_inode = NULL;
 
-       iput(sbi->meta_inode);
-       sbi->meta_inode = NULL;
-
        f2fs_destroy_cache(META_CACHE(sbi));
 
        /* Should check the page counts after dropping all node/meta pages */
@@ -4449,7 +4445,6 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
        sbi->allocate_section_policy = ALLOCATE_FORWARD_NOHINT;
        F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
        F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
-       F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
        sbi->cur_victim_sec = NULL_SECNO;
        sbi->gc_mode = GC_NORMAL;
        sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
@@ -5290,18 +5285,10 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
        if (err)
                goto free_page_array_cache;
 
-       /* get an inode for meta space */
-       sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
-       if (IS_ERR(sbi->meta_inode)) {
-               f2fs_err(sbi, "Failed to read F2FS meta data inode");
-               err = PTR_ERR(sbi->meta_inode);
-               goto free_meta_cache;
-       }
-
        err = f2fs_get_valid_checkpoint(sbi);
        if (err) {
                f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
-               goto free_meta_inode;
+               goto free_meta_cache;
        }
 
        if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
@@ -5597,7 +5584,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
         * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
         * falls into an infinite loop in f2fs_sync_meta_pages().
         */
-       truncate_inode_pages_final(META_MAPPING(sbi));
+       f2fs_truncate_meta_caches(sbi, 0, ULONG_MAX);
        /* evict some inodes being cached by GC */
        evict_inodes(sb);
        f2fs_unregister_sysfs(sbi);
@@ -5627,10 +5614,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 free_devices:
        destroy_device_list(sbi);
        kvfree(sbi->ckpt);
-free_meta_inode:
-       make_bad_inode(sbi->meta_inode);
-       iput(sbi->meta_inode);
-       sbi->meta_inode = NULL;
 free_meta_cache:
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

fs/f2fs/sysfs.c +1 -1
index ad25733e32c0..cd7ed210cad5 100644

Lines of this file are not shown. View the raw message.

include/linux/f2fs_fs.h +0 -2
index f38558279e86..c325904f461e 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Part of the diff is not shown here. View the raw message.

author maintainer
#15 lore raw

PATCH v7 6/12 f2fs: cache: initialize node cache

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch introduces node_blocks in f2fs_sb_info structure, initializes and destroys the node cache during filesystem mount and unmount.

It also introduces helper wrappers for node cache operation, and registers node cache into the memory shrinker and writeback kthread.

---
 fs/f2fs/cache.c |  9 ++++++++-
 fs/f2fs/cache.h | 11 +++++++++++
 fs/f2fs/f2fs.h  |  1 +
 fs/f2fs/super.c |  9 ++++++++-
 4 files changed, 28 insertions(+), 2 deletions(-)
fs/f2fs/cache.c +8 -1
index e99165e1c785..f265e45ee210 100644
@@ -624,7 +624,14 @@ static unsigned long f2fs_do_shrink_cache(struct f2fs_cached_block_list *cache,
 unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
 					unsigned long nr_to_scan)
 {
-	return f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+	unsigned long freed;
+
+	freed = f2fs_do_shrink_cache(META_CACHE(sbi), nr_to_scan);
+	if (freed >= nr_to_scan)
+		return freed;
+
+	freed += f2fs_do_shrink_cache(NODE_CACHE(sbi), nr_to_scan - freed);
+	return freed;
 }
 
 static int f2fs_cache_writeback_kthread(void *data)
fs/f2fs/cache.h +11 -0
index a0218c235c39..f02ea0638930 100644
@@ -198,6 +198,7 @@ int f2fs_start_cache_wb_thread(struct f2fs_sb_info *sbi);
 void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 
 #define META_CACHE(sbi)		(&(sbi)->meta_blocks)
+#define NODE_CACHE(sbi)		(&(sbi)->node_blocks)
 
 #define f2fs_find_meta_cache(sbi, blkaddr)		\
 	f2fs_find_cache(META_CACHE(sbi), blkaddr, 0)
@@ -206,6 +207,16 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 #define f2fs_truncate_meta_caches(sbi, start, len)	\
 	f2fs_drop_cache_range(META_CACHE(sbi), start, len, true)
 
+#define f2fs_grab_node_cache(sbi, blkaddr)		\
+	f2fs_grab_cache(NODE_CACHE(sbi), blkaddr,	\
+	F2FS_CACHE_LOCK_CREATE)
+#define f2fs_find_node_cache(sbi, blkaddr)		\
+	f2fs_find_cache(NODE_CACHE(sbi), blkaddr)
+#define f2fs_invalidate_node_cache(sbi, blkaddr)	\
+	f2fs_drop_cache_range(NODE_CACHE(sbi), blkaddr, 1, false)
+#define f2fs_truncate_node_caches(sbi, start, len)	\
+	f2fs_drop_cache_range(NODE_CACHE(sbi), start, len, true)
+
 unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
 				unsigned long nr_to_scan);
 
fs/f2fs/f2fs.h +1 -0
index e2b29067eb0a..51c191c514bc 100644
@@ -2108,6 +2108,7 @@ struct f2fs_sb_info {
 
 	/* f2fs internal cache */
 	struct f2fs_cached_block_list meta_blocks;
+	struct f2fs_cached_block_list node_blocks;
 
 	/* internal cache flush thread */
 	struct f2fs_cache_kthread cache_thread;
fs/f2fs/super.c +8 -1
index 0361e4cc0e0a..d1ed5d0f5335 100644
@@ -2092,6 +2092,7 @@ static void f2fs_put_super(struct super_block *sb)
 	sbi->node_inode = NULL;
 
 	f2fs_destroy_cache(META_CACHE(sbi));
+	f2fs_destroy_cache(NODE_CACHE(sbi));
 
 	/* Should check the page counts after dropping all node/meta pages */
 	for (i = 0; i < NR_COUNT_TYPE; i++) {
@@ -5285,10 +5286,14 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (err)
 		goto free_page_array_cache;
 
+	err = f2fs_init_cache(sbi, NODE_CACHE(sbi), F2FS_NODE_CACHE);
+	if (err)
+		goto free_meta_cache;
+
 	err = f2fs_get_valid_checkpoint(sbi);
 	if (err) {
 		f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
-		goto free_meta_cache;
+		goto free_node_cache;
 	}
 
 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
@@ -5614,6 +5619,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 free_devices:
 	destroy_device_list(sbi);
 	kvfree(sbi->ckpt);
+free_node_cache:
+	f2fs_destroy_cache(NODE_CACHE(sbi));
 free_meta_cache:
 	f2fs_destroy_cache(META_CACHE(sbi));
 free_page_array_cache:
Signature · 1 line
2.49.0
author maintainer
#16 lore raw

PATCH v7 7/12 f2fs: cache: use node cache

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch migrates F2FS node block caching from the fake VFS inode page cache (sbi->node_inode) to node cache (sbi->node_blocks).

It updates node related helpers to use node cache APIs and reference to struct f2fs_cached_block, and removes sbi->node_inode, especially, unifies inline and regular dentry block handling via struct f2fs_dentry_block_ref.

---
 fs/f2fs/acl.c           |  26 +-
 fs/f2fs/acl.h           |   8 +-
 fs/f2fs/cache.c         |   7 +-
 fs/f2fs/cache.h         |   3 +-
 fs/f2fs/checkpoint.c    |  15 +-
 fs/f2fs/compress.c      |  18 +-
 fs/f2fs/data.c          | 249 +++++------
 fs/f2fs/debug.c         |  12 +-
 fs/f2fs/dir.c           | 170 +++----
 fs/f2fs/extent_cache.c  |  14 +-
 fs/f2fs/f2fs.h          | 259 ++++++-----
 fs/f2fs/file.c          |  78 ++--
 fs/f2fs/gc.c            |  50 +--
 fs/f2fs/inline.c        | 279 ++++++------
 fs/f2fs/inode.c         | 167 ++++---
 fs/f2fs/namei.c         | 114 ++---
 fs/f2fs/node.c          | 953 +++++++++++++++++++---------------------
 fs/f2fs/node.h          | 108 ++---
 fs/f2fs/recovery.c      | 117 ++---
 fs/f2fs/segment.c       |  59 ++-
 fs/f2fs/segment.h       |  20 -
 fs/f2fs/shrinker.c      |   3 +-
 fs/f2fs/super.c         |  56 +--
 fs/f2fs/xattr.c         | 123 +++---
 fs/f2fs/xattr.h         |  12 +-
 include/linux/f2fs_fs.h |   1 -
 26 files changed, 1419 insertions(+), 1502 deletions(-)
fs/f2fs/acl.c +13 -13
index d3253549173e..34c9aa279040 100644
@@ -181,7 +181,7 @@ static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
 }
 
 static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
-						struct folio *dfolio)
+						struct f2fs_cached_block *entry)
 {
 	int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
 	void *value = NULL;
@@ -191,13 +191,13 @@ static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
 	if (type == ACL_TYPE_ACCESS)
 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
 
-	retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dfolio);
+	retval = f2fs_getxattr(inode, name_index, "", NULL, 0, entry);
 	if (retval > 0) {
 		value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
 		if (!value)
 			return ERR_PTR(-ENOMEM);
 		retval = f2fs_getxattr(inode, name_index, "", value,
-							retval, dfolio);
+							retval, entry);
 	}
 
 	if (retval > 0)
@@ -242,7 +242,7 @@ static int f2fs_acl_update_mode(struct mnt_idmap *idmap,
 
 static int __f2fs_set_acl(struct mnt_idmap *idmap,
 			struct inode *inode, int type,
-			struct posix_acl *acl, struct folio *ifolio)
+			struct posix_acl *acl, struct f2fs_cached_block *ientry)
 {
 	int name_index;
 	void *value = NULL;
@@ -253,7 +253,7 @@ static int __f2fs_set_acl(struct mnt_idmap *idmap,
 	switch (type) {
 	case ACL_TYPE_ACCESS:
 		name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
-		if (acl && !ifolio) {
+		if (acl && !ientry) {
 			error = f2fs_acl_update_mode(idmap, inode, &mode, &acl);
 			if (error)
 				return error;
@@ -279,7 +279,7 @@ static int __f2fs_set_acl(struct mnt_idmap *idmap,
 		}
 	}
 
-	error = f2fs_setxattr(inode, name_index, "", value, size, ifolio, 0);
+	error = f2fs_setxattr(inode, name_index, "", value, size, ientry, 0);
 
 	kfree(value);
 	if (!error)
@@ -374,7 +374,7 @@ static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
 
 static int f2fs_acl_create(struct inode *dir, umode_t *mode,
 		struct posix_acl **default_acl, struct posix_acl **acl,
-		struct folio *dfolio)
+		struct f2fs_cached_block *entry)
 {
 	struct posix_acl *p;
 	struct posix_acl *clone;
@@ -386,7 +386,7 @@ static int f2fs_acl_create(struct inode *dir, umode_t *mode,
 	if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
 		return 0;
 
-	p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dfolio);
+	p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, entry);
 	if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
 		*mode &= ~current_umask();
 		return 0;
@@ -423,13 +423,13 @@ static int f2fs_acl_create(struct inode *dir, umode_t *mode,
 	return ret;
 }
 
-int f2fs_init_acl(struct inode *inode, struct inode *dir, struct folio *ifolio,
-							struct folio *dfolio)
+int f2fs_init_acl(struct inode *inode, struct inode *dir, struct f2fs_cached_block *ientry,
+							struct f2fs_cached_block *dentry)
 {
 	struct posix_acl *default_acl = NULL, *acl = NULL;
 	int error;
 
-	error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dfolio);
+	error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dentry);
 	if (error)
 		return error;
 
@@ -437,7 +437,7 @@ int f2fs_init_acl(struct inode *inode, struct inode *dir, struct folio *ifolio,
 
 	if (default_acl) {
 		error = __f2fs_set_acl(NULL, inode, ACL_TYPE_DEFAULT,
-				default_acl, ifolio);
+				default_acl, ientry);
 		posix_acl_release(default_acl);
 	} else {
 		inode->i_default_acl = NULL;
@@ -445,7 +445,7 @@ int f2fs_init_acl(struct inode *inode, struct inode *dir, struct folio *ifolio,
 	if (acl) {
 		if (!error)
 			error = __f2fs_set_acl(NULL, inode, ACL_TYPE_ACCESS,
-					acl, ifolio);
+					acl, ientry);
 		posix_acl_release(acl);
 	} else {
 		inode->i_acl = NULL;
fs/f2fs/acl.h +5 -3
index 20e87e63c089..0f639367a0ab 100644
@@ -36,14 +36,16 @@ struct f2fs_acl_header {
 struct posix_acl *f2fs_get_acl(struct inode *, int, bool);
 int f2fs_set_acl(struct mnt_idmap *, struct dentry *,
 			struct posix_acl *, int);
-int f2fs_init_acl(struct inode *, struct inode *, struct folio *ifolio,
-		struct folio *dfolio);
+int f2fs_init_acl(struct inode *inode, struct inode *dir,
+			struct f2fs_cached_block *ientry,
+			struct f2fs_cached_block *dentry);
 #else
 #define f2fs_get_acl	NULL
 #define f2fs_set_acl	NULL
 
 static inline int f2fs_init_acl(struct inode *inode, struct inode *dir,
-				struct folio *ifolio, struct folio *dfolio)
+				struct f2fs_cached_block *ientry,
+				struct f2fs_cached_block *dentry)
 {
 	return 0;
 }
fs/f2fs/cache.c +4 -3
index f265e45ee210..7c809773356a 100644
@@ -28,7 +28,7 @@ void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
 		return;
 
 	/* submit cached bio */
-	f2fs_submit_merged_write_cache(entry, type);
+	f2fs_submit_merged_write_cache(entry->cache->sbi, entry, 0, type);
 
 	wait_on_bit_io(&entry->state, F2FS_BLOCK_WRITEBACK,
 					TASK_UNINTERRUPTIBLE);
@@ -65,8 +65,8 @@ bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry)
 	f2fs_cache_set_uptodate(entry);
 
 #ifdef CONFIG_F2FS_CHECK_FS
-	if (cache->type == F2FS_NODE_CACHE && IS_INODE(cache->sbi, cache_folio(entry)))
-		f2fs_inode_chksum_set(cache->sbi, cache_folio(entry));
+	if (f2fs_is_node_cache(entry) && IS_INODE(cache->sbi, entry))
+		f2fs_inode_chksum_set(cache->sbi, entry);
 #endif
 
 	if (!f2fs_cache_test_and_set_dirty(entry)) {
@@ -654,6 +654,7 @@ static int f2fs_cache_writeback_kthread(void *data)
 			continue;
 
 		f2fs_write_meta_caches(sbi);
+		f2fs_write_node_caches(sbi);
 	}
 	return 0;
 }
fs/f2fs/cache.h +2 -1
index f02ea0638930..143f82b588d1 100644
@@ -48,6 +48,7 @@ struct f2fs_cached_block_list {
 };
 
 #define IS_META_CACHE(cache) (cache->type == F2FS_META_CACHE)
+#define IS_NODE_CACHE(cache) (cache->type == F2FS_NODE_CACHE)
 
 /* Flags for f2fs_cached_block state */
 enum f2fs_cached_state {
@@ -211,7 +212,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 	f2fs_grab_cache(NODE_CACHE(sbi), blkaddr,	\
 	F2FS_CACHE_LOCK_CREATE)
 #define f2fs_find_node_cache(sbi, blkaddr)		\
-	f2fs_find_cache(NODE_CACHE(sbi), blkaddr)
+	f2fs_find_cache(NODE_CACHE(sbi), blkaddr, F2FS_CACHE_ACCESS)
 #define f2fs_invalidate_node_cache(sbi, blkaddr)	\
 	f2fs_drop_cache_range(NODE_CACHE(sbi), blkaddr, 1, false)
 #define f2fs_truncate_node_caches(sbi, start, len)	\
fs/f2fs/checkpoint.c +6 -9
index e3d14a6c136c..40a96eb9179c 100644
@@ -927,7 +927,7 @@ void f2fs_add_orphan_inode(struct inode *inode)
 {
 	/* add new orphan ino entry into list */
 	f2fs_add_ino_entry(F2FS_I_SB(inode), inode->i_ino, ORPHAN_INO);
-	f2fs_update_inode_page(inode);
+	f2fs_update_inode_cache(inode);
 }
 
 void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino)
@@ -1428,7 +1428,7 @@ static int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi)
 
 			/* it's on eviction */
 			if (is_inode_flag_set(inode, FI_DIRTY_INODE))
-				f2fs_update_inode_page(inode);
+				f2fs_update_inode_cache(inode);
 			iput(inode);
 		} else {
 			cond_resched();
@@ -1483,14 +1483,10 @@ static bool __need_flush_quota(struct f2fs_sb_info *sbi)
  */
 static int block_operations(struct f2fs_sb_info *sbi)
 {
-	struct writeback_control wbc = {
-		.sync_mode = WB_SYNC_ALL,
-		.nr_to_write = LONG_MAX,
-	};
 	int err = 0, cnt = 0;
 
 	/*
-	 * Let's flush inline_data in dirty node pages.
+	 * Let's flush inline_data in dirty node caches.
 	 */
 	f2fs_flush_inline_data(sbi);
 
@@ -1529,7 +1525,7 @@ static int block_operations(struct f2fs_sb_info *sbi)
 	}
 
 	/*
-	 * POR: we should ensure that there are no dirty node pages
+	 * POR: we should ensure that there are no dirty node caches
 	 * until finishing nat/sit flush. inode->i_blocks can be updated.
 	 */
 	f2fs_down_write(&sbi->node_change);
@@ -1550,7 +1546,8 @@ static int block_operations(struct f2fs_sb_info *sbi)
 	if (get_pages(sbi, F2FS_DIRTY_NODES)) {
 		f2fs_up_write(&sbi->node_write);
 		atomic_inc(&sbi->wb_sync_req[NODE]);
-		err = f2fs_sync_node_pages(sbi, &wbc, false, FS_CP_NODE_IO);
+		err = f2fs_writeback_node_caches(sbi, LONG_MAX,
+			true, false, FS_CP_NODE_IO);
 		atomic_dec(&sbi->wb_sync_req[NODE]);
 		if (err) {
 			f2fs_up_write(&sbi->node_change);
fs/f2fs/compress.c +9 -9
index 991b7738c645..0e55c03663a9 100644
@@ -909,7 +909,7 @@ bool f2fs_sanity_check_cluster(struct dnode_of_data *dn)
 	}
 
 	for (i = 1, count = 1; i < cluster_size; i++, count++) {
-		block_t blkaddr = data_blkaddr(dn->inode, dn->node_folio,
+		block_t blkaddr = data_blkaddr(dn->inode, dn->node_entry,
 							dn->ofs_in_node + i);
 
 		/* [COMPR_ADDR, ..., COMPR_ADDR] */
@@ -950,7 +950,7 @@ static int __f2fs_get_cluster_blocks(struct inode *inode,
 	int count, i;
 
 	for (i = 0, count = 0; i < cluster_size; i++) {
-		block_t blkaddr = data_blkaddr(dn->inode, dn->node_folio,
+		block_t blkaddr = data_blkaddr(dn->inode, dn->node_entry,
 							dn->ofs_in_node + i);
 
 		if (__is_valid_data_blkaddr(blkaddr))
@@ -1146,7 +1146,7 @@ static int prepare_compress_overwrite(struct compress_ctx *cc,
 			goto release_and_retry;
 		}
 
-		f2fs_folio_wait_writeback(folio, DATA, true, true);
+		f2fs_folio_wait_writeback(folio, true, true);
 		f2fs_compress_ctx_add_page(cc, folio);
 
 		if (!folio_test_uptodate(folio)) {
@@ -1322,7 +1322,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 		goto out_unlock_op;
 
 	for (i = 0; i < cc->cluster_size; i++) {
-		if (data_blkaddr(dn.inode, dn.node_folio,
+		if (data_blkaddr(dn.inode, dn.node_entry,
 					dn.ofs_in_node + i) == NULL_ADDR)
 			goto out_put_dnode;
 	}
@@ -1354,7 +1354,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
 				page_folio(cc->rpages[i + 1])->index, cic);
 		fio.compressed_page = cc->cpages[i];
 
-		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_folio,
+		fio.old_blkaddr = data_blkaddr(dn.inode, dn.node_entry,
 						dn.ofs_in_node + i + 1);
 
 		/* wait for GCed page writeback via generic cache */
@@ -1542,7 +1542,7 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc,
 		if (folio_test_writeback(folio)) {
 			if (wbc->sync_mode == WB_SYNC_NONE)
 				goto continue_unlock;
-			f2fs_folio_wait_writeback(folio, DATA, true, true);
+			f2fs_folio_wait_writeback(folio, true, true);
 		}
 
 		if (!folio_clear_dirty_for_io(folio))
@@ -1874,14 +1874,14 @@ void f2fs_put_folio_dic(struct folio *folio, bool in_task)
 unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
 						unsigned int ofs_in_node)
 {
-	bool compressed = data_blkaddr(dn->inode, dn->node_folio,
+	bool compressed = data_blkaddr(dn->inode, dn->node_entry,
 					ofs_in_node) == COMPRESS_ADDR;
 	int i = compressed ? 1 : 0;
-	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_folio,
+	block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_entry,
 							ofs_in_node + i);
 
 	for (i += 1; i < F2FS_I(dn->inode)->i_cluster_size; i++) {
-		block_t blkaddr = data_blkaddr(dn->inode, dn->node_folio,
+		block_t blkaddr = data_blkaddr(dn->inode, dn->node_entry,
 							ofs_in_node + i);
 
 		if (!__is_valid_data_blkaddr(blkaddr))
fs/f2fs/data.c +117 -132

566 lines not on this page · raw message

fs/f2fs/debug.c +4 -8
index 24819d4c0d55..c09c5f700c89 100644
@@ -222,8 +222,7 @@ static void update_general_status(struct f2fs_sb_info *sbi)
 	si->free_secs = free_sections(sbi);
 	si->prefree_count = prefree_segments(sbi);
 	si->dirty_count = dirty_segments(sbi);
-	if (sbi->node_inode)
-		si->node_pages = NODE_MAPPING(sbi)->nrpages;
+	si->node_caches = NODE_CACHE(sbi)->num_entries;
 	si->meta_caches = META_CACHE(sbi)->num_entries;
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 	if (sbi->compress_inode) {
@@ -382,13 +381,10 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
 	}
 
 	si->page_mem = 0;
-	if (sbi->node_inode) {
-		unsigned long npages = NODE_MAPPING(sbi)->nrpages;
-
-		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
-	}
 	si->page_mem += (unsigned long long)META_CACHE(sbi)->num_entries << PAGE_SHIFT;
 	si->cache_mem += META_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
+	si->page_mem += (unsigned long long)NODE_CACHE(sbi)->num_entries << PAGE_SHIFT;
+	si->cache_mem += NODE_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 	if (sbi->compress_inode) {
 		unsigned long npages = COMPRESS_MAPPING(sbi)->nrpages;
@@ -696,7 +692,7 @@ static int stat_show(struct seq_file *s, void *v)
 			   si->aw_cnt, si->max_aw_cnt);
 		seq_printf(s, "  - compress: %4d, hit:%8d\n", si->compress_pages, si->compress_page_hit);
 		seq_printf(s, "  - nodes: %4d in %4d\n",
-			   si->ndirty_node, si->node_pages);
+			   si->ndirty_node, si->node_caches);
 		seq_printf(s, "  - dents: %4d in dirs:%4d (%4d)\n",
 			   si->ndirty_dent, si->ndirty_dirs, si->ndirty_all);
 		seq_printf(s, "  - data: %4d in files:%4d\n",
fs/f2fs/dir.c +93 -77

416 lines not on this page · raw message

fs/f2fs/extent_cache.c +7 -7
index 40cd87940961..5a944428aca0 100644
@@ -20,10 +20,10 @@
 #include "segment.h"
 #include <trace/events/f2fs.h>
 
-bool sanity_check_extent_cache(struct inode *inode, struct folio *ifolio)
+bool sanity_check_extent_cache(struct inode *inode, struct f2fs_cached_block *ientry)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-	struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
+	struct f2fs_extent *i_ext = &F2FS_INODE(ientry)->i_ext;
 	struct extent_info ei;
 	int devi;
 
@@ -416,11 +416,11 @@ static void __drop_largest_extent(struct extent_tree *et,
 	}
 }
 
-void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
+void f2fs_init_read_extent_tree(struct inode *inode, struct f2fs_cached_block *ientry)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
-	struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
+	struct f2fs_extent *i_ext = &F2FS_INODE(ientry)->i_ext;
 	struct extent_tree *et;
 	struct extent_node *en;
 	struct extent_info ei = {0};
@@ -428,9 +428,9 @@ void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
 	if (!__may_extent_tree(inode, EX_READ)) {
 		/* drop largest read extent */
 		if (i_ext->len) {
-			f2fs_folio_wait_writeback(ifolio, NODE, true, true);
+			f2fs_cache_wait_writeback(ientry);
 			i_ext->len = 0;
-			folio_mark_dirty(ifolio);
+			f2fs_mark_cache_dirty(ientry);
 		}
 		set_inode_flag(inode, FI_NO_EXTENT);
 		return;
@@ -958,7 +958,7 @@ static void __update_extent_cache(struct dnode_of_data *dn, enum extent_type typ
 		return;
 
 	ei.fofs = f2fs_start_bidx_of_node(ofs_of_node(F2FS_I_SB(dn->inode),
-				dn->node_folio), dn->inode) + dn->ofs_in_node;
+				dn->node_entry), dn->inode) + dn->ofs_in_node;
 	ei.len = 1;
 
 	if (type == EX_READ) {
fs/f2fs/f2fs.h +142 -117

524 lines not on this page · raw message

fs/f2fs/file.c +37 -41

274 lines not on this page · raw message

fs/f2fs/gc.c +25 -25

150 lines not on this page · raw message

fs/f2fs/inline.c +141 -138

674 lines not on this page · raw message

fs/f2fs/inode.c +78 -89

53 lines not on this page · raw message

fs/f2fs/namei.c +59 -55

114 lines not on this page · raw message

fs/f2fs/node.c +446 -507

953 lines not on this page · raw message

fs/f2fs/node.h +56 -52

108 lines not on this page · raw message

fs/f2fs/recovery.c +59 -58

117 lines not on this page · raw message

fs/f2fs/segment.c +35 -24

59 lines not on this page · raw message

fs/f2fs/segment.h +0 -20
index 3083069c9092..b11461acc520 100644

Lines of this file are not shown. View the raw message.

fs/f2fs/shrinker.c +2 -1
index baa09bc63585..499717cd693c 100644

Lines of this file are not shown. View the raw message.

fs/f2fs/super.c +12 -44

56 lines not on this page · raw message

fs/f2fs/xattr.c +61 -62

123 lines not on this page · raw message

fs/f2fs/xattr.h +6 -6
index 75beab2d1ee3..870524b667ed 100644

Lines of this file are not shown. View the raw message.

include/linux/f2fs_fs.h +0 -1
index c325904f461e..ac87446cc086 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.49.0

Part of the diff is not shown here. View the raw message.

author maintainer
#17 lore raw

PATCH v7 8/12 f2fs: cache: initialize compress cache

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch introduces compress_blocks in f2fs_sb_info structure, initializes and destroys the compress cache during filesystem mount and unmount.

It adds an ino union field in struct f2fs_cached_block (sharing space with writeback linkage for zero memory overhead) to track per-inode cached blocks, and registers compress cache into the memory shrinker.

---
 fs/f2fs/cache.c |  9 ++++++++-
 fs/f2fs/cache.h | 14 ++++++++++----
 fs/f2fs/f2fs.h  |  6 ++++++
 fs/f2fs/super.c |  9 ++++++++-
 4 files changed, 32 insertions(+), 6 deletions(-)
fs/f2fs/cache.c +8 -1
index 7c809773356a..7260c157654b 100644
@@ -176,7 +176,10 @@ static struct f2fs_cached_block *f2fs_create_cache(
 	entry->index = index;
 
 	atomic_set(&entry->refcount, 0);
-	entry->next_entry = NULL;
+	if (!IS_COMPRESS_CACHE(cache))
+		entry->next_entry = NULL;
+	else
+		entry->ino = 0;
 	INIT_LIST_HEAD(&entry->list);
 
 	entry->cache = cache;
@@ -631,6 +634,10 @@ unsigned long f2fs_shrink_cache(struct f2fs_sb_info *sbi,
 		return freed;
 
 	freed += f2fs_do_shrink_cache(NODE_CACHE(sbi), nr_to_scan - freed);
+	if (freed >= nr_to_scan)
+		return freed;
+
+	freed += f2fs_do_shrink_cache(COMPRESS_CACHE(sbi), nr_to_scan - freed);
 	return freed;
 }
 
fs/f2fs/cache.h +10 -4
index 143f82b588d1..d2dd77ca2bc1 100644
@@ -18,12 +18,15 @@ struct f2fs_rwsem;
 struct f2fs_io_info;
 enum page_type;
 
-/* Represents a single cached block (meta, node) */
+/* Represents a single cached block (meta, node or compress) */
 struct f2fs_cached_block {
 	struct list_head list;		/* LRU list head */
 	struct f2fs_cached_block_list *cache;	/* parent cache list */
-	struct f2fs_cached_block *next_entry;	/* chain for merged BIO */
-	unsigned long index;		/* key in radix tree, (meta: pba, node: nid) */
+	union {
+		struct f2fs_cached_block *next_entry;/* chain for merged BIO */
+		nid_t ino;		/* inode number for compress cache */
+	};
+	unsigned long index;		/* key in radix tree, (meta/compress: pba, node: nid) */
 	unsigned long state;		/* cache entry state (e.g., Dirty, UpToDate) */
 	void *data;			/* blocksize-aligned memory (4KB or 16KB) */
 	atomic_t refcount;		/* reference count */
@@ -34,6 +37,7 @@ struct f2fs_sb_info;
 enum f2fs_cache_type {
 	F2FS_META_CACHE,
 	F2FS_NODE_CACHE,
+	F2FS_COMPRESS_CACHE,
 };
 
 /* Main cache control structure (per sb_info) */
@@ -43,12 +47,13 @@ struct f2fs_cached_block_list {
 	spinlock_t tree_lock;		/* Lock for radix tree */
 	struct list_head lru_list;	/* Single global LRU list */
 	spinlock_t list_lock;		/* Lock for LRU list */
-	enum f2fs_cache_type type;	/* Cache type (Node or Meta) */
+	enum f2fs_cache_type type;	/* Cache type (Node, Meta, Compress) */
 	unsigned long num_entries;	/* Current number of entries */
 };
 
 #define IS_META_CACHE(cache) (cache->type == F2FS_META_CACHE)
 #define IS_NODE_CACHE(cache) (cache->type == F2FS_NODE_CACHE)
+#define IS_COMPRESS_CACHE(cache) (cache->type == F2FS_COMPRESS_CACHE)
 
 /* Flags for f2fs_cached_block state */
 enum f2fs_cached_state {
@@ -200,6 +205,7 @@ void f2fs_stop_cache_wb_thread(struct f2fs_sb_info *sbi);
 
 #define META_CACHE(sbi)		(&(sbi)->meta_blocks)
 #define NODE_CACHE(sbi)		(&(sbi)->node_blocks)
+#define COMPRESS_CACHE(sbi)	(&(sbi)->compress_blocks)
 
 #define f2fs_find_meta_cache(sbi, blkaddr)		\
 	f2fs_find_cache(META_CACHE(sbi), blkaddr, 0)
fs/f2fs/f2fs.h +6 -0
index d6681276708e..1e23e02cac8b 100644
@@ -2106,6 +2106,7 @@ struct f2fs_sb_info {
 	/* f2fs internal cache */
 	struct f2fs_cached_block_list meta_blocks;
 	struct f2fs_cached_block_list node_blocks;
+	struct f2fs_cached_block_list compress_blocks;
 
 	/* internal cache flush thread */
 	struct f2fs_cache_kthread cache_thread;
@@ -2384,6 +2385,11 @@ static inline bool f2fs_is_node_cache(struct f2fs_cached_block *entry)
 	return entry->cache && entry->cache == NODE_CACHE(entry->cache->sbi);
 }
 
+static inline bool f2fs_is_compress_cache(struct f2fs_cached_block *entry)
+{
+	return entry->cache && entry->cache == COMPRESS_CACHE(entry->cache->sbi);
+}
+
 static inline struct f2fs_bio *F2FS_BIO(struct bio *bio)
 {
 	return container_of(bio, struct f2fs_bio, bio);
fs/f2fs/super.c +8 -1
index 73f94134d354..5f21a3813671 100644
@@ -2071,6 +2071,7 @@ static void f2fs_put_super(struct super_block *sb)
 
 	f2fs_destroy_compress_inode(sbi);
 
+	f2fs_destroy_cache(COMPRESS_CACHE(sbi));
 	f2fs_destroy_cache(NODE_CACHE(sbi));
 	f2fs_destroy_cache(META_CACHE(sbi));
 
@@ -5269,10 +5270,14 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (err)
 		goto free_meta_cache;
 
+	err = f2fs_init_cache(sbi, COMPRESS_CACHE(sbi), F2FS_COMPRESS_CACHE);
+	if (err)
+		goto free_node_cache;
+
 	err = f2fs_get_valid_checkpoint(sbi);
 	if (err) {
 		f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
-		goto free_node_cache;
+		goto free_compress_cache;
 	}
 
 	if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
@@ -5587,6 +5592,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 free_devices:
 	destroy_device_list(sbi);
 	kvfree(sbi->ckpt);
+free_compress_cache:
+	f2fs_destroy_cache(COMPRESS_CACHE(sbi));
 free_node_cache:
 	f2fs_destroy_cache(NODE_CACHE(sbi));
 free_meta_cache:
Signature · 1 line
2.49.0
author maintainer
#18 lore raw

PATCH v7 9/12 f2fs: cache: use compress cache

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch migrates compressed cluster caching from the fake VFS inode page cache (sbi->compress_inode) to compress cache (sbi->compress_blocks).

It converts compression caching and decompression paths to use compress cache APIs, uses entry->ino for per-inode invalidation, and removes sbi->compress_inode.

---
 fs/f2fs/compress.c      | 143 +++++++++++++++-------------------------
 fs/f2fs/debug.c         |   9 +--
 fs/f2fs/f2fs.h          |   8 +--
 fs/f2fs/inode.c         |  49 ++------------
 fs/f2fs/node.c          |   4 +-
 fs/f2fs/node.h          |   2 +-
 fs/f2fs/shrinker.c      |   3 +-
 fs/f2fs/super.c         |  15 ++---
 include/linux/f2fs_fs.h |   1 -
 9 files changed, 74 insertions(+), 160 deletions(-)
fs/f2fs/compress.c +53 -90
index 0e55c03663a9..b053fd188d3c 100644
@@ -1893,30 +1893,19 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
 	return compressed ? i - 1 : i;
 }
 
-const struct address_space_operations f2fs_compress_aops = {
-	.release_folio = f2fs_release_folio,
-	.invalidate_folio = f2fs_invalidate_folio,
-	.migrate_folio	= filemap_migrate_folio,
-};
-
-struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi)
-{
-	return sbi->compress_inode->i_mapping;
-}
-
 void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
 				block_t blkaddr, unsigned int len)
 {
-	if (!sbi->compress_inode)
+	if (!test_opt(sbi, COMPRESS_CACHE))
 		return;
-	invalidate_mapping_pages(COMPRESS_MAPPING(sbi), blkaddr, blkaddr + len - 1);
+
+	f2fs_drop_cache_range(COMPRESS_CACHE(sbi), blkaddr, len, false);
 }
 
 static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
 		struct folio *folio, nid_t ino, block_t blkaddr)
 {
-	struct folio *cfolio;
-	int ret;
+	struct f2fs_cached_block *entry;
 
 	if (!test_opt(sbi, COMPRESS_CACHE))
 		return;
@@ -1924,52 +1913,48 @@ static void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE_READ))
 		return;
 
-	if (!f2fs_available_free_memory(sbi, COMPRESS_PAGE))
+	if (!f2fs_available_free_memory(sbi, COMPRESS_BLOCK))
 		return;
 
-	cfolio = filemap_get_folio(COMPRESS_MAPPING(sbi), blkaddr);
-	if (!IS_ERR(cfolio)) {
-		f2fs_folio_put(cfolio, false);
+	entry = f2fs_find_cache(COMPRESS_CACHE(sbi), blkaddr,
+						F2FS_CACHE_ACCESS);
+	if (!IS_ERR(entry)) {
+		f2fs_put_cache(entry, false);
 		return;
 	}
 
-	cfolio = filemap_alloc_folio(__GFP_NOWARN | __GFP_IO, 0, NULL);
-	if (!cfolio)
+	entry = f2fs_grab_cache(COMPRESS_CACHE(sbi), blkaddr,
+					F2FS_CACHE_LOCK_CREATE);
+	if (IS_ERR(entry))
 		return;
 
-	ret = filemap_add_folio(COMPRESS_MAPPING(sbi), cfolio,
-						blkaddr, GFP_NOFS);
-	if (ret) {
-		f2fs_folio_put(cfolio, false);
-		return;
-	}
-
-	folio_set_f2fs_data(cfolio, ino);
-
-	memcpy(folio_address(cfolio), folio_address(folio), PAGE_SIZE);
-	folio_mark_uptodate(cfolio);
-	f2fs_folio_put(cfolio, true);
+	entry->ino = ino;
+	memcpy(cache_address(entry), folio_address(folio), sbi->blocksize);
+	f2fs_cache_set_uptodate(entry);
+	f2fs_put_cache(entry, true);
 }
 
 bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,
 								block_t blkaddr)
 {
-	struct folio *cfolio;
+	struct f2fs_cached_block *entry;
 	bool hitted = false;
 
 	if (!test_opt(sbi, COMPRESS_CACHE))
 		return false;
 
-	cfolio = f2fs_filemap_get_folio(COMPRESS_MAPPING(sbi),
-				blkaddr, FGP_LOCK | FGP_NOWAIT, GFP_NOFS);
-	if (!IS_ERR(cfolio)) {
-		if (folio_test_uptodate(cfolio)) {
+	entry = f2fs_find_cache(COMPRESS_CACHE(sbi), blkaddr,
+						F2FS_CACHE_ACCESS);
+	if (!IS_ERR(entry)) {
+		f2fs_lock_cache(entry);
+		if (f2fs_is_compress_cache(entry) &&
+				f2fs_cache_test_uptodate(entry)) {
 			atomic_inc(&sbi->compress_page_hit);
 			memcpy(folio_address(folio),
-				folio_address(cfolio), folio_size(folio));
+				cache_address(entry), folio_size(folio));
 			hitted = true;
 		}
-		f2fs_folio_put(cfolio, true);
+		f2fs_put_cache(entry, true);
 	}
 
 	return hitted;
@@ -1977,71 +1962,49 @@ bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,
 
 void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino)
 {
-	struct address_space *mapping = COMPRESS_MAPPING(sbi);
-	struct folio_batch fbatch;
-	pgoff_t index = 0;
-	pgoff_t end = MAX_BLKADDR(sbi);
+	struct f2fs_cached_block_list *cache = COMPRESS_CACHE(sbi);
+	struct f2fs_cached_block *entries[F2FS_ONSTACK_CACHES];
+	pgoff_t index = 0, end = ULONG_MAX;
+	int nr;
+	int i;
 
-	if (!mapping->nrpages)
+	if (!cache->num_entries)
 		return;
+next:
+	nr = f2fs_cache_gang_lookup(cache, entries, &index, end);
+	if (!nr)
+		return;
+	for (i = 0; i < nr; i++) {
+		struct f2fs_cached_block *entry = entries[i];
 
-	folio_batch_init(&fbatch);
-
-	do {
-		unsigned int nr, i;
-
-		nr = filemap_get_folios(mapping, &index, end - 1, &fbatch);
-		if (!nr)
-			break;
-
-		for (i = 0; i < nr; i++) {
-			struct folio *folio = fbatch.folios[i];
+		index = entry->index + 1;
 
-			folio_lock(folio);
-			if (folio->mapping != mapping) {
-				folio_unlock(folio);
-				continue;
-			}
+		f2fs_lock_cache(entry);
+		if (unlikely(!f2fs_is_compress_cache(entry)))
+			goto unlock;
+		if (entry->ino != ino)
+			goto unlock;
 
-			if (ino != folio_get_f2fs_data(folio)) {
-				folio_unlock(folio);
-				continue;
-			}
+		f2fs_truncate_locked_cache(entry, false);
+unlock:
+		f2fs_unlock_cache(entry);
+	}
+	f2fs_cache_gang_release(entries, nr);
 
-			generic_error_remove_folio(mapping, folio);
-			folio_unlock(folio);
-		}
-		folio_batch_release(&fbatch);
+	if (index < end) {
 		cond_resched();
-	} while (index < end);
+		goto next;
+	}
 }
 
-int f2fs_init_compress_inode(struct f2fs_sb_info *sbi)
+void f2fs_init_compress_cache_context(struct f2fs_sb_info *sbi)
 {
-	struct inode *inode;
-
 	if (!test_opt(sbi, COMPRESS_CACHE))
-		return 0;
-
-	inode = f2fs_iget(sbi->sb, F2FS_COMPRESS_INO(sbi));
-	if (IS_ERR(inode))
-		return PTR_ERR(inode);
-	sbi->compress_inode = inode;
+		return;
 
 	sbi->compress_percent = COMPRESS_PERCENT;
 	sbi->compress_watermark = COMPRESS_WATERMARK;
-
 	atomic_set(&sbi->compress_page_hit, 0);
-
-	return 0;
-}
-
-void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi)
-{
-	if (!sbi->compress_inode)
-		return;
-	iput(sbi->compress_inode);
-	sbi->compress_inode = NULL;
 }
 
 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi)
fs/f2fs/debug.c +5 -4
index c09c5f700c89..4ed5c271dba3 100644
@@ -225,8 +225,8 @@ static void update_general_status(struct f2fs_sb_info *sbi)
 	si->node_caches = NODE_CACHE(sbi)->num_entries;
 	si->meta_caches = META_CACHE(sbi)->num_entries;
 #ifdef CONFIG_F2FS_FS_COMPRESSION
-	if (sbi->compress_inode) {
-		si->compress_pages = COMPRESS_MAPPING(sbi)->nrpages;
+	if (test_opt(sbi, COMPRESS_CACHE)) {
+		si->compress_pages = COMPRESS_CACHE(sbi)->num_entries;
 		si->compress_page_hit = atomic_read(&sbi->compress_page_hit);
 	}
 #endif
@@ -386,10 +386,11 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
 	si->page_mem += (unsigned long long)NODE_CACHE(sbi)->num_entries << PAGE_SHIFT;
 	si->cache_mem += NODE_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
 #ifdef CONFIG_F2FS_FS_COMPRESSION
-	if (sbi->compress_inode) {
-		unsigned long npages = COMPRESS_MAPPING(sbi)->nrpages;
+	if (test_opt(sbi, COMPRESS_CACHE)) {
+		unsigned long npages = COMPRESS_CACHE(sbi)->num_entries;
 
 		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
+		si->cache_mem += npages * sizeof(struct f2fs_cached_block);
 	}
 #endif
 }
fs/f2fs/f2fs.h +2 -6
index 1e23e02cac8b..69690b061236 100644
@@ -2077,7 +2077,6 @@ struct f2fs_sb_info {
 	u32 compr_new_inode;
 
 	/* For compressed block cache */
-	struct inode *compress_inode;		/* cache compressed blocks */
 	unsigned int compress_percent;		/* cache page percentage */
 	unsigned int compress_watermark;	/* cache page watermark */
 	atomic_t compress_page_hit;		/* cache hit count */
@@ -4889,13 +4888,11 @@ unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
 int f2fs_init_compress_ctx(struct compress_ctx *cc);
 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
 void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
-int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
-void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
+void f2fs_init_compress_cache_context(struct f2fs_sb_info *sbi);
 int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
 void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
 int __init f2fs_init_compress_cache(void);
 void f2fs_destroy_compress_cache(void);
-struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
 void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
 					block_t blkaddr, unsigned int len);
 bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,
@@ -4944,8 +4941,7 @@ static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)
 static inline unsigned int f2fs_cluster_blocks_are_contiguous(
 			struct dnode_of_data *dn, unsigned int ofs_in_node) { return 0; }
 static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
-static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
-static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
+static inline void f2fs_init_compress_cache_context(struct f2fs_sb_info *sbi) { }
 static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
 static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
 static inline int __init f2fs_init_compress_cache(void) { return 0; }
fs/f2fs/inode.c +4 -45

100 lines not on this page · raw message

fs/f2fs/node.c +2 -2
index 1af118858fd1..fe5e4c4eff23 100644
@@ -109,7 +109,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
 		mem_size = (atomic_read(&dcc->discard_cmd_cnt) *
 				sizeof(struct discard_cmd)) >> PAGE_SHIFT;
 		res = mem_size < (avail_ram * nm_i->ram_thresh / 100);
-	} else if (type == COMPRESS_PAGE) {
+	} else if (type == COMPRESS_BLOCK) {
 #ifdef CONFIG_F2FS_FS_COMPRESSION
 		unsigned long free_ram = val.freeram;
 
@@ -118,7 +118,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)
 		 * exceed threshold, deny caching compress page.
 		 */
 		res = (free_ram > avail_ram * sbi->compress_watermark / 100) &&
-			(COMPRESS_MAPPING(sbi)->nrpages <
+			(COMPRESS_CACHE(sbi)->num_entries <
 			 free_ram * sbi->compress_percent / 100);
 #else
 		res = false;
fs/f2fs/node.h +1 -1
index c8d66ca54e94..2704a5c6a54d 100644
@@ -157,7 +157,7 @@ enum mem_type {
 	READ_EXTENT_CACHE,	/* indicates read extent cache */
 	AGE_EXTENT_CACHE,	/* indicates age extent cache */
 	DISCARD_CACHE,	/* indicates memory of cached discard cmds */
-	COMPRESS_PAGE,	/* indicates memory of cached compressed pages */
+	COMPRESS_BLOCK,	/* indicates memory of cached compressed blocks */
 	BASE_CHECK,	/* check kernel status */
 };
 
fs/f2fs/shrinker.c +2 -1
index 499717cd693c..29f488531492 100644
@@ -40,7 +40,8 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi,
 static unsigned long __count_cache(struct f2fs_sb_info *sbi)
 {
 	return META_CACHE(sbi)->num_entries +
-		NODE_CACHE(sbi)->num_entries;
+		NODE_CACHE(sbi)->num_entries +
+		COMPRESS_CACHE(sbi)->num_entries;
 }
 
 unsigned long f2fs_shrink_count(struct shrinker *shrink,
fs/f2fs/super.c +5 -10
index 5f21a3813671..9f6a6a6ebaff 100644
@@ -2069,8 +2069,6 @@ static void f2fs_put_super(struct super_block *sb)
 
 	f2fs_bug_on(sbi, sbi->fsync_node_num);
 
-	f2fs_destroy_compress_inode(sbi);
-
 	f2fs_destroy_cache(COMPRESS_CACHE(sbi));
 	f2fs_destroy_cache(NODE_CACHE(sbi));
 	f2fs_destroy_cache(META_CACHE(sbi));
@@ -5323,6 +5321,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 
 	f2fs_init_fsync_node_info(sbi);
 
+	f2fs_init_compress_cache_context(sbi);
+
 	/* setup checkpoint request control and start checkpoint issue thread */
 	f2fs_init_ckpt_req_control(sbi);
 	if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
@@ -5395,13 +5395,9 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 		goto free_ino_entry;
 	}
 
-	err = f2fs_init_compress_inode(sbi);
-	if (err)
-		goto free_root_inode;
-
 	err = f2fs_register_sysfs(sbi);
 	if (err)
-		goto free_compress_inode;
+		goto free_root_inode;
 
 	sbi->umount_lock_holder = current;
 #ifdef CONFIG_QUOTA
@@ -5569,8 +5565,6 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	/* evict some inodes being cached by GC */
 	evict_inodes(sb);
 	f2fs_unregister_sysfs(sbi);
-free_compress_inode:
-	f2fs_destroy_compress_inode(sbi);
 free_root_inode:
 	dput(sb->s_root);
 	sb->s_root = NULL;
@@ -5687,7 +5681,8 @@ static void kill_f2fs_super(struct super_block *sb)
 		 * compress inode cache.
 		 */
 		if (test_opt(sbi, COMPRESS_CACHE))
-			truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
+			f2fs_invalidate_compress_pages_range(sbi,
+					0, UINT_MAX);
 #endif
 
 		if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
include/linux/f2fs_fs.h +0 -1
index ac87446cc086..3081702b1ddb 100644
@@ -43,7 +43,6 @@
 #define F2FS_RESERVED_NODE_NUM		3
 
 #define F2FS_ROOT_INO(sbi)	((sbi)->root_ino_num)
-#define F2FS_COMPRESS_INO(sbi)	(NM_I(sbi)->max_nid)
 
 #define F2FS_MAX_QUOTAS		3
 
Signature · 1 line
2.49.0
author maintainer
#19 lore raw

PATCH v7 10/12 f2fs: cache: support fault injection

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch adds fault injection support for metadata cache allocations to improve error-path test coverage.
- it integrates entry allocations with FAULT_KALLOC

---
 fs/f2fs/cache.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
fs/f2fs/cache.c +14 -10
index 7260c157654b..dcab0f321730 100644
@@ -154,23 +154,27 @@ static struct f2fs_cached_block *f2fs_create_cache(
 		struct f2fs_cached_block_list *cache,
 		unsigned long index, bool nofail)
 {
+	struct f2fs_sb_info *sbi = cache->sbi;
 	struct f2fs_cached_block *entry;
 	unsigned int flags = GFP_NOFS;
 
 	if (index == ULONG_MAX)
 		return ERR_PTR(-ERANGE);
 
-	if (nofail)
+	if (nofail) {
 		flags |= __GFP_NOFAIL;
-
-	entry = kzalloc_obj(*entry, flags);
-	if (!entry)
-		return ERR_PTR(-ENOMEM);
-
-	entry->data = kmalloc(cache->sbi->blocksize, flags);
-	if (!entry->data) {
-		kfree(entry);
-		return ERR_PTR(-ENOMEM);
+		entry = kzalloc_obj(*entry, flags);
+		entry->data = kmalloc(sbi->blocksize, flags);
+	} else {
+		entry = f2fs_kzalloc(sbi, sizeof(*entry), flags);
+		if (!entry)
+			return ERR_PTR(-ENOMEM);
+
+		entry->data = f2fs_kmalloc(sbi, sbi->blocksize, flags);
+		if (!entry->data) {
+			kfree(entry);
+			return ERR_PTR(-ENOMEM);
+		}
 	}
 
 	entry->index = index;
Signature · 1 line
2.49.0
#20 lore raw

Re: [f2fs-dev] [PATCH v7 10/12] f2fs: cache: support fault injection

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 6 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch adds fault injection support for metadata cache allocations to improve error-path test coverage.
- it integrates entry allocations with FAULT_KALLOC

FAULT_KALLOC -> FAULT_KMALLOC?

Quoted 57 lines
---
 fs/f2fs/cache.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
fs/f2fs/cache.c +14 -11
index 7260c157654b..dcab0f321730 100644
@@ -154,23 +154,27 @@ static struct f2fs_cached_block *f2fs_create_cache(
                struct f2fs_cached_block_list *cache,
                unsigned long index, bool nofail)
 {
+       struct f2fs_sb_info *sbi = cache->sbi;
        struct f2fs_cached_block *entry;
        unsigned int flags = GFP_NOFS;
 
        if (index == ULONG_MAX)
                return ERR_PTR(-ERANGE);
 
-       if (nofail)
+       if (nofail) {
                flags |= __GFP_NOFAIL;
-
-       entry = kzalloc_obj(*entry, flags);
-       if (!entry)
-               return ERR_PTR(-ENOMEM);
-
-       entry->data = kmalloc(cache->sbi->blocksize, flags);
-       if (!entry->data) {
-               kfree(entry);
-               return ERR_PTR(-ENOMEM);
+               entry = kzalloc_obj(*entry, flags);
+               entry->data = kmalloc(sbi->blocksize, flags);
+       } else {
+               entry = f2fs_kzalloc(sbi, sizeof(*entry), flags);
+               if (!entry)
+                       return ERR_PTR(-ENOMEM);
+
+               entry->data = f2fs_kmalloc(sbi, sbi->blocksize, flags);
+               if (!entry->data) {
+                       kfree(entry);
+                       return ERR_PTR(-ENOMEM);
+               }
        }
 
        entry->index = index;
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#21 lore raw

Re: [f2fs-dev] [PATCH v7 10/12] f2fs: cache: support fault injection

Quoted 9 lines · Zhiguo Niu

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 6 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch adds fault injection support for metadata cache allocations to improve error-path test coverage.
- it integrates entry allocations with FAULT_KALLOC

FAULT_KALLOC -> FAULT_KMALLOC?

Will fix.

Thanks,

Quoted 57 lines
Quoted 57 lines
---
  fs/f2fs/cache.c | 24 ++++++++++++++----------
  1 file changed, 14 insertions(+), 10 deletions(-)
fs/f2fs/cache.c +14 -11
index 7260c157654b..dcab0f321730 100644
@@ -154,23 +154,27 @@ static struct f2fs_cached_block *f2fs_create_cache(
                 struct f2fs_cached_block_list *cache,
                 unsigned long index, bool nofail)
  {
+       struct f2fs_sb_info *sbi = cache->sbi;
         struct f2fs_cached_block *entry;
         unsigned int flags = GFP_NOFS;
 
         if (index == ULONG_MAX)
                 return ERR_PTR(-ERANGE);
 
-       if (nofail)
+       if (nofail) {
                 flags |= __GFP_NOFAIL;
-
-       entry = kzalloc_obj(*entry, flags);
-       if (!entry)
-               return ERR_PTR(-ENOMEM);
-
-       entry->data = kmalloc(cache->sbi->blocksize, flags);
-       if (!entry->data) {
-               kfree(entry);
-               return ERR_PTR(-ENOMEM);
+               entry = kzalloc_obj(*entry, flags);
+               entry->data = kmalloc(sbi->blocksize, flags);
+       } else {
+               entry = f2fs_kzalloc(sbi, sizeof(*entry), flags);
+               if (!entry)
+                       return ERR_PTR(-ENOMEM);
+
+               entry->data = f2fs_kmalloc(sbi, sbi->blocksize, flags);
+               if (!entry->data) {
+                       kfree(entry);
+                       return ERR_PTR(-ENOMEM);
+               }
         }
 
         entry->index = index;
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#22 lore raw

PATCH v7 11/12 f2fs: cache: introduce tracepoints

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch introduces ftrace tracepoints to observe and profile metadata cache operations:
- trace_f2fs_cache_set_dirty to trace marking a cached block dirty
- trace_f2fs_write_cache to trace single block writeback submission
- trace_f2fs_write_caches to tracesbatch writeback and sync sessions

---
 fs/f2fs/cache.c             |  3 ++
 fs/f2fs/checkpoint.c        |  6 ++++
 fs/f2fs/segment.c           |  3 ++
 include/trace/events/f2fs.h | 71 +++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+)
fs/f2fs/cache.c +3 -0
index dcab0f321730..33da3f243783 100644
@@ -15,6 +15,7 @@
 #include "f2fs.h"
 #include "cache.h"
 #include "node.h"
+#include <trace/events/f2fs.h>
 #include "segment.h"
 
 void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
@@ -73,6 +74,8 @@ bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry)
 		enum count_type type = IS_META_CACHE(cache) ?
 				F2FS_DIRTY_META : F2FS_DIRTY_NODES;
 
+		trace_f2fs_cache_set_dirty(entry,
+				IS_META_CACHE(cache) ? META : NODE);
 		f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_NONE,
 						F2FS_CACHE_TAG_DIRTY);
 		inc_page_count(cache->sbi, type);
fs/f2fs/checkpoint.c +6 -0
index 40a96eb9179c..2542b1a29989 100644
@@ -550,6 +550,8 @@ static bool __f2fs_write_meta_cache(struct f2fs_cached_block *entry,
 {
 	struct f2fs_sb_info *sbi = entry->cache->sbi;
 
+	trace_f2fs_write_cache(entry, META);
+
 	if (unlikely(f2fs_cp_error(sbi))) {
 		if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
 			f2fs_cache_clear_uptodate(entry);
@@ -611,6 +613,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
 	struct blk_plug plug;
 	bool background = nr_to_write != LONG_MAX;
 
+	trace_f2fs_write_caches(sbi, nr_to_write, 0, META);
+
 	blk_start_plug(&plug);
 
 	while ((nr = f2fs_cache_gang_lookup_tag(META_CACHE(sbi), entries,
@@ -666,6 +670,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
 
 	blk_finish_plug(&plug);
 
+	trace_f2fs_write_caches(sbi, nr_to_write, nwritten, META);
+
 	return nwritten;
 }
 
fs/f2fs/segment.c +3 -0
index 87e24d0306e8..15cffbb5b407 100644
@@ -4226,6 +4226,9 @@ void f2fs_do_write_node_cache(unsigned int nid, struct f2fs_io_info *fio)
 {
 	struct f2fs_summary sum;
 
+	if (fio->is_cache)
+		trace_f2fs_write_cache(fio->cache_entry, NODE);
+
 	set_summary(&sum, nid, 0, 0);
 	do_write_block(&sum, fio);
 
include/trace/events/f2fs.h +71 -0

83 lines not on this page · raw message

Signature · 1 line
2.49.0
#23 lore raw

Re: [f2fs-dev] [PATCH v7 11/12] f2fs: cache: introduce tracepoints

Chao Yu via Linux-f2fs-devel <[email protected]>

fs/f2fs/segment.c void f2fs_do_write_node_cache(unsigned int nid, struct f2fs_io_info *fio)

Quoted 74 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces ftrace tracepoints to observe and profile metadata cache operations:
- trace_f2fs_cache_set_dirty to trace marking a cached block dirty
- trace_f2fs_write_cache to trace single block writeback submission
- trace_f2fs_write_caches to tracesbatch writeback and sync sessions

---
 fs/f2fs/cache.c             |  3 ++
 fs/f2fs/checkpoint.c        |  6 ++++
 fs/f2fs/segment.c           |  3 ++
 include/trace/events/f2fs.h | 71 +++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+)
fs/f2fs/cache.c +3 -0
index dcab0f321730..33da3f243783 100644
@@ -15,6 +15,7 @@
 #include "f2fs.h"
 #include "cache.h"
 #include "node.h"
+#include <trace/events/f2fs.h>
 #include "segment.h"
 
 void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
@@ -73,6 +74,8 @@ bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry)
                enum count_type type = IS_META_CACHE(cache) ?
                                F2FS_DIRTY_META : F2FS_DIRTY_NODES;
 
+               trace_f2fs_cache_set_dirty(entry,
+                               IS_META_CACHE(cache) ? META : NODE);
                f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_NONE,
                                                F2FS_CACHE_TAG_DIRTY);
                inc_page_count(cache->sbi, type);
fs/f2fs/checkpoint.c +6 -0
index 40a96eb9179c..2542b1a29989 100644
@@ -550,6 +550,8 @@ static bool __f2fs_write_meta_cache(struct f2fs_cached_block *entry,
 {
        struct f2fs_sb_info *sbi = entry->cache->sbi;
 
+       trace_f2fs_write_cache(entry, META);
+
        if (unlikely(f2fs_cp_error(sbi))) {
                if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
                        f2fs_cache_clear_uptodate(entry);
@@ -611,6 +613,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
        struct blk_plug plug;
        bool background = nr_to_write != LONG_MAX;
 
+       trace_f2fs_write_caches(sbi, nr_to_write, 0, META);
+
        blk_start_plug(&plug);
 
        while ((nr = f2fs_cache_gang_lookup_tag(META_CACHE(sbi), entries,
@@ -666,6 +670,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
 
        blk_finish_plug(&plug);
 
+       trace_f2fs_write_caches(sbi, nr_to_write, nwritten, META);
+
        return nwritten;
 }
 
fs/f2fs/segment.c +0 -0
index 87e24d0306e8..15cffbb5b407 100644
@@ -4226,6 +4226,9 @@ void f2fs_do_write_node_cache(unsigned int nid, struct f2fs_io_info *fio)

Hi Chao,
Do we also need to add tracepoints in f2fs_writeback_node_caches?? thanks!

Quoted 107 lines
fs/f2fs/segment.c +3 -0
 {
        struct f2fs_summary sum;
 
+       if (fio->is_cache)
+               trace_f2fs_write_cache(fio->cache_entry, NODE);
+
        set_summary(&sum, nid, 0, 0);
        do_write_block(&sum, fio);
 
include/trace/events/f2fs.h +71 -1
index d53be932df01..df4cd346ae72 100644
@@ -1430,6 +1430,50 @@ DEFINE_EVENT(f2fs__folio, f2fs_set_page_dirty,
        TP_ARGS(folio, type)
 );
 
+DECLARE_EVENT_CLASS(f2fs__cached_block,
+
+       TP_PROTO(struct f2fs_cached_block *block, int type),
+
+       TP_ARGS(block, type),
+
+       TP_STRUCT__entry(
+               __field(dev_t,  dev)
+               __field(pgoff_t, index)
+               __field(int,    type)
+               __field(int,    dirty)
+               __field(int,    uptodate)
+       ),
+
+       TP_fast_assign(
+               __entry->dev    = block->cache->sbi->sb->s_dev;
+               __entry->index  = block->index;
+               __entry->type   = type;
+               __entry->dirty  = f2fs_cache_test_dirty(block);
+               __entry->uptodate = f2fs_cache_test_uptodate(block);
+       ),
+
+       TP_printk("dev = (%d,%d), %s, index = %lu, dirty = %d, uptodate = %d",
+               show_dev(__entry->dev),
+               show_block_type(__entry->type),
+               (unsigned long)__entry->index,
+               __entry->dirty,
+               __entry->uptodate)
+);
+
+DEFINE_EVENT(f2fs__cached_block, f2fs_write_cache,
+
+       TP_PROTO(struct f2fs_cached_block *block, int type),
+
+       TP_ARGS(block, type)
+);
+
+DEFINE_EVENT(f2fs__cached_block, f2fs_cache_set_dirty,
+
+       TP_PROTO(struct f2fs_cached_block *block, int type),
+
+       TP_ARGS(block, type)
+);
+
 TRACE_EVENT(f2fs_replace_atomic_write_block,
 
        TP_PROTO(struct inode *inode, struct inode *cow_inode, pgoff_t index,
@@ -1574,6 +1618,33 @@ TRACE_EVENT(f2fs_writepages,
                __entry->for_sync)
 );
 
+TRACE_EVENT(f2fs_write_caches,
+
+       TP_PROTO(struct f2fs_sb_info *sbi, long nr_to_write, long nwritten, int type),
+
+       TP_ARGS(sbi, nr_to_write, nwritten, type),
+
+       TP_STRUCT__entry(
+               __field(dev_t,  dev)
+               __field(long,   nr_to_write)
+               __field(long,   nwritten)
+               __field(int,    type)
+       ),
+
+       TP_fast_assign(
+               __entry->dev            = sbi->sb->s_dev;
+               __entry->nr_to_write    = nr_to_write;
+               __entry->nwritten       = nwritten;
+               __entry->type           = type;
+       ),
+
+       TP_printk("dev = (%d,%d), %s, nr_to_write = %ld, nwritten = %ld",
+               show_dev(__entry->dev),
+               show_block_type(__entry->type),
+               __entry->nr_to_write,
+               __entry->nwritten)
+);
+
 TRACE_EVENT(f2fs_readpages,
 
        TP_PROTO(struct inode *inode, pgoff_t start, unsigned int nrpage),
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#24 lore raw

Re: [f2fs-dev] [PATCH v7 11/12] f2fs: cache: introduce tracepoints

Quoted 71 lines · Zhiguo Niu

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 69 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch introduces ftrace tracepoints to observe and profile metadata cache operations:
- trace_f2fs_cache_set_dirty to trace marking a cached block dirty
- trace_f2fs_write_cache to trace single block writeback submission
- trace_f2fs_write_caches to tracesbatch writeback and sync sessions

---
  fs/f2fs/cache.c             |  3 ++
  fs/f2fs/checkpoint.c        |  6 ++++
  fs/f2fs/segment.c           |  3 ++
  include/trace/events/f2fs.h | 71 +++++++++++++++++++++++++++++++++++++
  4 files changed, 83 insertions(+)
fs/f2fs/cache.c +3 -0
index dcab0f321730..33da3f243783 100644
@@ -15,6 +15,7 @@
  #include "f2fs.h"
  #include "cache.h"
  #include "node.h"
+#include <trace/events/f2fs.h>
  #include "segment.h"
 
  void f2fs_cache_wait_writeback_cond(struct f2fs_cached_block *entry,
@@ -73,6 +74,8 @@ bool f2fs_mark_cache_dirty(struct f2fs_cached_block *entry)
                 enum count_type type = IS_META_CACHE(cache) ?
                                 F2FS_DIRTY_META : F2FS_DIRTY_NODES;
 
+               trace_f2fs_cache_set_dirty(entry,
+                               IS_META_CACHE(cache) ? META : NODE);
                 f2fs_cache_update_tag(entry, F2FS_CACHE_TAG_NONE,
                                                 F2FS_CACHE_TAG_DIRTY);
                 inc_page_count(cache->sbi, type);
fs/f2fs/checkpoint.c +6 -0
index 40a96eb9179c..2542b1a29989 100644
@@ -550,6 +550,8 @@ static bool __f2fs_write_meta_cache(struct f2fs_cached_block *entry,
  {
         struct f2fs_sb_info *sbi = entry->cache->sbi;
 
+       trace_f2fs_write_cache(entry, META);
+
         if (unlikely(f2fs_cp_error(sbi))) {
                 if (is_sbi_flag_set(sbi, SBI_IS_CLOSE)) {
                         f2fs_cache_clear_uptodate(entry);
@@ -611,6 +613,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
         struct blk_plug plug;
         bool background = nr_to_write != LONG_MAX;
 
+       trace_f2fs_write_caches(sbi, nr_to_write, 0, META);
+
         blk_start_plug(&plug);
 
         while ((nr = f2fs_cache_gang_lookup_tag(META_CACHE(sbi), entries,
@@ -666,6 +670,8 @@ long f2fs_sync_meta_caches(struct f2fs_sb_info *sbi, long nr_to_write,
 
         blk_finish_plug(&plug);
 
+       trace_f2fs_write_caches(sbi, nr_to_write, nwritten, META);
+
         return nwritten;
  }
 
Quoted 7 lines

fs/f2fs/segment.c void f2fs_do_write_node_cache(unsigned int nid, struct f2fs_io_info *fio)

Quoted 5 lines
fs/f2fs/segment.c +0 -0
index 87e24d0306e8..15cffbb5b407 100644
@@ -4226,6 +4226,9 @@ void f2fs_do_write_node_cache(unsigned int nid, struct f2fs_io_info *fio)

Hi Chao,
Do we also need to add tracepoints in f2fs_writeback_node_caches??

You're right, will fix.

Thanks,

Quoted 108 lines

thanks!

Quoted 107 lines
fs/f2fs/segment.c +3 -0
  {
         struct f2fs_summary sum;
 
+       if (fio->is_cache)
+               trace_f2fs_write_cache(fio->cache_entry, NODE);
+
         set_summary(&sum, nid, 0, 0);
         do_write_block(&sum, fio);
 
include/trace/events/f2fs.h +71 -1
index d53be932df01..df4cd346ae72 100644
@@ -1430,6 +1430,50 @@ DEFINE_EVENT(f2fs__folio, f2fs_set_page_dirty,
         TP_ARGS(folio, type)
  );
 
+DECLARE_EVENT_CLASS(f2fs__cached_block,
+
+       TP_PROTO(struct f2fs_cached_block *block, int type),
+
+       TP_ARGS(block, type),
+
+       TP_STRUCT__entry(
+               __field(dev_t,  dev)
+               __field(pgoff_t, index)
+               __field(int,    type)
+               __field(int,    dirty)
+               __field(int,    uptodate)
+       ),
+
+       TP_fast_assign(
+               __entry->dev    = block->cache->sbi->sb->s_dev;
+               __entry->index  = block->index;
+               __entry->type   = type;
+               __entry->dirty  = f2fs_cache_test_dirty(block);
+               __entry->uptodate = f2fs_cache_test_uptodate(block);
+       ),
+
+       TP_printk("dev = (%d,%d), %s, index = %lu, dirty = %d, uptodate = %d",
+               show_dev(__entry->dev),
+               show_block_type(__entry->type),
+               (unsigned long)__entry->index,
+               __entry->dirty,
+               __entry->uptodate)
+);
+
+DEFINE_EVENT(f2fs__cached_block, f2fs_write_cache,
+
+       TP_PROTO(struct f2fs_cached_block *block, int type),
+
+       TP_ARGS(block, type)
+);
+
+DEFINE_EVENT(f2fs__cached_block, f2fs_cache_set_dirty,
+
+       TP_PROTO(struct f2fs_cached_block *block, int type),
+
+       TP_ARGS(block, type)
+);
+
  TRACE_EVENT(f2fs_replace_atomic_write_block,
 
         TP_PROTO(struct inode *inode, struct inode *cow_inode, pgoff_t index,
@@ -1574,6 +1618,33 @@ TRACE_EVENT(f2fs_writepages,
                 __entry->for_sync)
  );
 
+TRACE_EVENT(f2fs_write_caches,
+
+       TP_PROTO(struct f2fs_sb_info *sbi, long nr_to_write, long nwritten, int type),
+
+       TP_ARGS(sbi, nr_to_write, nwritten, type),
+
+       TP_STRUCT__entry(
+               __field(dev_t,  dev)
+               __field(long,   nr_to_write)
+               __field(long,   nwritten)
+               __field(int,    type)
+       ),
+
+       TP_fast_assign(
+               __entry->dev            = sbi->sb->s_dev;
+               __entry->nr_to_write    = nr_to_write;
+               __entry->nwritten       = nwritten;
+               __entry->type           = type;
+       ),
+
+       TP_printk("dev = (%d,%d), %s, nr_to_write = %ld, nwritten = %ld",
+               show_dev(__entry->dev),
+               show_block_type(__entry->type),
+               __entry->nr_to_write,
+               __entry->nwritten)
+);
+
  TRACE_EVENT(f2fs_readpages,
 
         TP_PROTO(struct inode *inode, pgoff_t start, unsigned int nrpage),
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#25 lore raw

PATCH v7 12/12 f2fs: cache: show per-cache usage in debugfs

In reply to Chao Yu

From: Chao Yu <[email protected]>

This patch exposes per-cache memory usage (entry struct memory vs. cached block buffer memory) for meta, node, and compress caches under the memory section in debugfs:
- meta entry: xxx KB, meta cache: xxx KB
- node entry: xxx KB, node cache: xxx KB
- compress entry: xxx KB, compress cache: xxx KB

---
 fs/f2fs/cache.h |  1 +
 fs/f2fs/debug.c | 49 ++++++++++++++++++++++++++++++++++++++++---------
 fs/f2fs/f2fs.h  |  2 ++
 3 files changed, 43 insertions(+), 9 deletions(-)
fs/f2fs/cache.h +1 -0
index d2dd77ca2bc1..42e7c49cef47 100644
@@ -38,6 +38,7 @@ enum f2fs_cache_type {
 	F2FS_META_CACHE,
 	F2FS_NODE_CACHE,
 	F2FS_COMPRESS_CACHE,
+	NR_CACHE_TYPES,
 };
 
 /* Main cache control structure (per sb_info) */
fs/f2fs/debug.c +40 -9

63 lines not on this page · raw message

fs/f2fs/f2fs.h +2 -0
index 69690b061236..34878d99a2f0 100644
@@ -4525,6 +4525,8 @@ struct f2fs_stat_info {
 	unsigned int block_count[2];
 	unsigned int inplace_count;
 	unsigned long long base_mem, cache_mem, page_mem;
+	unsigned long long cache_entry_mem[NR_CACHE_TYPES];
+	unsigned long long cache_data_mem[NR_CACHE_TYPES];
 	struct f2fs_dev_stats *dev_stats;
 };
 
Signature · 1 line
2.49.0
Reviewed-by on v7 12/12
#26 lore raw

Re: [f2fs-dev] [PATCH v7 12/12] f2fs: cache: show per-cache usage in debugfs

Chao Yu via Linux-f2fs-devel <[email protected]>

Quoted 11 lines · 于2026年9月12日周六 07:28写道:

From: Chao Yu <[email protected]>

This patch exposes per-cache memory usage (entry struct memory vs. cached block buffer memory) for meta, node, and compress caches under the memory section in debugfs:
- meta entry: xxx KB, meta cache: xxx KB
- node entry: xxx KB, node cache: xxx KB
- compress entry: xxx KB, compress cache: xxx KB

thanks!

Quoted 109 lines
---
 fs/f2fs/cache.h |  1 +
 fs/f2fs/debug.c | 49 ++++++++++++++++++++++++++++++++++++++++---------
 fs/f2fs/f2fs.h  |  2 ++
 3 files changed, 43 insertions(+), 9 deletions(-)
fs/f2fs/cache.h +1 -0
index d2dd77ca2bc1..42e7c49cef47 100644
@@ -38,6 +38,7 @@ enum f2fs_cache_type {
        F2FS_META_CACHE,
        F2FS_NODE_CACHE,
        F2FS_COMPRESS_CACHE,
+       NR_CACHE_TYPES,
 };
 
 /* Main cache control structure (per sb_info) */
fs/f2fs/debug.c +40 -9
index 4ed5c271dba3..8c0cffde2457 100644
@@ -380,17 +380,36 @@ static void update_mem_info(struct f2fs_sb_info *sbi)
                si->cache_mem += si->ext_mem[i];
        }
 
-       si->page_mem = 0;
-       si->page_mem += (unsigned long long)META_CACHE(sbi)->num_entries << PAGE_SHIFT;
-       si->cache_mem += META_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
-       si->page_mem += (unsigned long long)NODE_CACHE(sbi)->num_entries << PAGE_SHIFT;
-       si->cache_mem += NODE_CACHE(sbi)->num_entries * sizeof(struct f2fs_cached_block);
+       si->cache_entry_mem[F2FS_META_CACHE] =
+               (unsigned long long)META_CACHE(sbi)->num_entries *
+               sizeof(struct f2fs_cached_block);
+       si->cache_data_mem[F2FS_META_CACHE] =
+               (unsigned long long)META_CACHE(sbi)->num_entries * sbi->blocksize;
+
+       si->cache_entry_mem[F2FS_NODE_CACHE] =
+               (unsigned long long)NODE_CACHE(sbi)->num_entries *
+               sizeof(struct f2fs_cached_block);
+       si->cache_data_mem[F2FS_NODE_CACHE] =
+               (unsigned long long)NODE_CACHE(sbi)->num_entries * sbi->blocksize;
+
+       si->cache_mem += si->cache_entry_mem[F2FS_META_CACHE] +
+                        si->cache_entry_mem[F2FS_NODE_CACHE];
+       si->page_mem = si->cache_data_mem[F2FS_META_CACHE] +
+                       si->cache_data_mem[F2FS_NODE_CACHE];
 #ifdef CONFIG_F2FS_FS_COMPRESSION
        if (test_opt(sbi, COMPRESS_CACHE)) {
-               unsigned long npages = COMPRESS_CACHE(sbi)->num_entries;
-
-               si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
-               si->cache_mem += npages * sizeof(struct f2fs_cached_block);
+               si->cache_entry_mem[F2FS_COMPRESS_CACHE] =
+                       (unsigned long long)COMPRESS_CACHE(sbi)->num_entries *
+                       sizeof(struct f2fs_cached_block);
+               si->cache_data_mem[F2FS_COMPRESS_CACHE] =
+                       (unsigned long long)COMPRESS_CACHE(sbi)->num_entries *
+                       sbi->blocksize;
+
+               si->cache_mem += si->cache_entry_mem[F2FS_COMPRESS_CACHE];
+               si->page_mem += si->cache_data_mem[F2FS_COMPRESS_CACHE];
+       } else {
+               si->cache_entry_mem[F2FS_COMPRESS_CACHE] = 0;
+               si->cache_data_mem[F2FS_COMPRESS_CACHE] = 0;
        }
 #endif
 }
@@ -749,6 +768,18 @@ static int stat_show(struct seq_file *s, void *v)
                                si->ext_mem[EX_READ] >> 10);
                seq_printf(s, "  - block age extent cache: %llu KB\n",
                                si->ext_mem[EX_BLOCK_AGE] >> 10);
+               seq_printf(s, "  - meta entry: %llu KB, meta cache: %llu KB\n",
+                               si->cache_entry_mem[F2FS_META_CACHE] >> 10,
+                               si->cache_data_mem[F2FS_META_CACHE] >> 10);
+               seq_printf(s, "  - node entry: %llu KB, node cache: %llu KB\n",
+                               si->cache_entry_mem[F2FS_NODE_CACHE] >> 10,
+                               si->cache_data_mem[F2FS_NODE_CACHE] >> 10);
+#ifdef CONFIG_F2FS_FS_COMPRESSION
+               if (test_opt(sbi, COMPRESS_CACHE))
+                       seq_printf(s, "  - compress entry: %llu KB, compress cache: %llu KB\n",
+                                       si->cache_entry_mem[F2FS_COMPRESS_CACHE] >> 10,
+                                       si->cache_data_mem[F2FS_COMPRESS_CACHE] >> 10);
+#endif
                seq_printf(s, "  - paged : %llu KB\n",
                                si->page_mem >> 10);
        }
fs/f2fs/f2fs.h +2 -1
index 69690b061236..34878d99a2f0 100644
@@ -4525,6 +4525,8 @@ struct f2fs_stat_info {
        unsigned int block_count[2];
        unsigned int inplace_count;
        unsigned long long base_mem, cache_mem, page_mem;
+       unsigned long long cache_entry_mem[NR_CACHE_TYPES];
+       unsigned long long cache_data_mem[NR_CACHE_TYPES];
        struct f2fs_dev_stats *dev_stats;
 };
 
--

2.49.0

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel