[PATCH v3 08/12] f2fs: cache: initialize compress cache
From: Chao Yu
Date: Tue Aug 25 2026 - 09:05:54 EST
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.
Signed-off-by: Chao Yu <chao@xxxxxxxxxx>
---
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(-)
diff --git a/fs/f2fs/cache.c b/fs/f2fs/cache.c
index 4bda32817e94..8431180bb1f8 100644
--- a/fs/f2fs/cache.c
+++ b/fs/f2fs/cache.c
@@ -195,7 +195,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;
@@ -620,6 +623,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;
}
diff --git a/fs/f2fs/cache.h b/fs/f2fs/cache.h
index cac2810d3dbb..b87feb3eb896 100644
--- a/fs/f2fs/cache.h
+++ b/fs/f2fs/cache.h
@@ -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 {
@@ -178,6 +183,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)
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index ce7885864982..5b1b3104ec20 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2107,6 +2107,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;
@@ -2328,6 +2329,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);
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 60498877df88..6b42b736a662 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2052,6 +2052,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));
@@ -5231,10 +5232,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))
@@ -5555,6 +5560,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:
--
2.49.0