Skip to contentSkip to conversation

f2fs: introduce metadata cache

PATCH Superseded Chao Yu 3 participants 27 messages Sep 16 – Sep 17 Page 2 of 2

Subsystems (2)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
author maintainer
#15 lore raw

Re: [f2fs-dev] [PATCH v8 07/12] f2fs: cache: use node cache

In reply to Zhiguo Niu
Quoted 7 lines · Zhiguo Niu

fs/f2fs/data.c static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, · line 735

Quoted 4 lines

static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
struct inode *inode, struct folio *folio,

fs/f2fs/data.c +1 -1
-                               nid_t ino, enum page_type type, bool writeback)
+                               bool writeback)

Hi Chao,
seems this function just only handle DATA type now, so can we rename it xxx_data_xxx for easy reading?

Zhiguo,

I rename it to __submit_merged_data_write_cond() for readability.

Thanks,

#16 lore raw

Re: [f2fs-dev] [PATCH v8 07/12] f2fs: cache: use node cache

Quoted 6 lines · Chao Yu <[email protected]> 于2026年9月17日周四 10:55写道:
Quoted 4 lines · Zhiguo Niu
Quoted 4 lines

static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
struct inode *inode, struct folio *folio,

fs/f2fs/data.c +1 -1
-                               nid_t ino, enum page_type type, bool writeback)
+                               bool writeback)
Quoted 7 lines
Quoted 3 lines

Hi Chao,
seems this function just only handle DATA type now, so can we rename it xxx_data_xxx for easy reading?

Zhiguo,

I rename it to __submit_merged_data_write_cond() for readability.

Hi Chao,
It is good to me.
Thanks!

Quoted 3 lines

Thanks,

author maintainer
#17 lore raw

PATCH v8 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 |  7 +++++--
 4 files changed, 29 insertions(+), 7 deletions(-)
fs/f2fs/cache.c +8 -1
index 3abd469dc011..3e6a198ef3b3 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 a36ee57adb1a..a8c24f5282fa 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 {
@@ -196,6 +201,7 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
 
 #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 +5 -2
index bf42fbffa348..47259e8172fc 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));
 
@@ -5263,11 +5264,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 
 	f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE);
 	f2fs_init_cache(sbi, NODE_CACHE(sbi), F2FS_NODE_CACHE);
+	f2fs_init_cache(sbi, COMPRESS_CACHE(sbi), F2FS_COMPRESS_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))
@@ -5582,7 +5584,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:
+free_compress_cache:
+	f2fs_destroy_cache(COMPRESS_CACHE(sbi));
 	f2fs_destroy_cache(NODE_CACHE(sbi));
 	f2fs_destroy_cache(META_CACHE(sbi));
 	f2fs_destroy_page_array_cache(sbi);
Signature · 1 line
2.49.0
Reviewed-by on v8 8/12
#18 lore raw

Re: [f2fs-dev] [PATCH v8 08/12] f2fs: cache: initialize compress cache

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

Quoted 11 lines · 于2026年9月16日周三 09:57写道:

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.

thanks!

Quoted 157 lines
---
 fs/f2fs/cache.c |  9 ++++++++-
 fs/f2fs/cache.h | 14 ++++++++++----
 fs/f2fs/f2fs.h  |  6 ++++++
 fs/f2fs/super.c |  7 +++++--
 4 files changed, 29 insertions(+), 7 deletions(-)
fs/f2fs/cache.c +8 -1
index 3abd469dc011..3e6a198ef3b3 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 a36ee57adb1a..a8c24f5282fa 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 {
@@ -196,6 +201,7 @@ void f2fs_drop_cache_range(struct f2fs_cached_block_list *cache,
 
 #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 +5 -3
index bf42fbffa348..47259e8172fc 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));
 
@@ -5263,11 +5264,12 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 
        f2fs_init_cache(sbi, META_CACHE(sbi), F2FS_META_CACHE);
        f2fs_init_cache(sbi, NODE_CACHE(sbi), F2FS_NODE_CACHE);
+       f2fs_init_cache(sbi, COMPRESS_CACHE(sbi), F2FS_COMPRESS_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))
@@ -5582,7 +5584,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:
+free_compress_cache:
+       f2fs_destroy_cache(COMPRESS_CACHE(sbi));
        f2fs_destroy_cache(NODE_CACHE(sbi));
        f2fs_destroy_cache(META_CACHE(sbi));
        f2fs_destroy_page_array_cache(sbi);
--

2.49.0

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

author maintainer
#19 lore raw

PATCH v8 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 d7f7c35e710f..55931fbcb0d1 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 47259e8172fc..6873bad73d7a 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));
@@ -5315,6 +5313,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) &&
@@ -5387,13 +5387,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
@@ -5561,8 +5557,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;
@@ -5676,7 +5670,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
Reviewed-by on v8 9/12
#20 lore raw

Re: [f2fs-dev] [PATCH v8 09/12] f2fs: cache: use compress cache

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

Quoted 11 lines · 于2026年9月16日周三 09:57写道:

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.

Thanks!

Quoted 526 lines
---
 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 d7f7c35e710f..55931fbcb0d1 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
index c822ca2dce20..9164a2b5d9f0 100644
@@ -574,15 +574,6 @@ static int do_read_inode(struct inode *inode)
        return 0;
 }
 
-static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
-{
-#ifdef CONFIG_F2FS_FS_COMPRESSION
-       if (test_opt(sbi, COMPRESS_CACHE) && ino == F2FS_COMPRESS_INO(sbi))
-               return true;
-#endif
-       return false;
-}
-
 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
 {
        struct f2fs_sb_info *sbi = F2FS_SB(sb);
@@ -594,42 +585,17 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
                return ERR_PTR(-ENOMEM);
 
        if (!(inode_state_read_once(inode) & I_NEW)) {
-               if (is_meta_ino(sbi, ino)) {
-                       f2fs_err(sbi, "inaccessible inode: %lu, run fsck to repair", ino);
-                       set_sbi_flag(sbi, SBI_NEED_FSCK);
-                       ret = -EFSCORRUPTED;
-                       trace_f2fs_iget_exit(inode, ret);
-                       iput(inode);
-                       f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
-                       fserror_report_file_metadata(inode, ret, GFP_NOFS);
-                       return ERR_PTR(ret);
-               }
-
                trace_f2fs_iget(inode);
                return inode;
        }
 
-       if (is_meta_ino(sbi, ino))
-               goto make_now;
-
        ret = do_read_inode(inode);
        if (ret)
                goto bad_inode;
-make_now:
+
        f2fs_set_inode_flags(inode);
 
-       if (ino == F2FS_COMPRESS_INO(sbi)) {
-#ifdef CONFIG_F2FS_FS_COMPRESSION
-               inode->i_mapping->a_ops = &f2fs_compress_aops;
-               /*
-                * generic_error_remove_folio only truncates pages of regular
-                * inode
-                */
-               inode->i_mode |= S_IFREG;
-#endif
-               mapping_set_gfp_mask(inode->i_mapping,
-                       GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
-       } else if (S_ISREG(inode->i_mode)) {
+       if (S_ISREG(inode->i_mode)) {
                inode->i_op = &f2fs_file_inode_operations;
                inode->i_fop = &f2fs_file_operations;
                inode->i_mapping->a_ops = &f2fs_dblock_aops;
@@ -882,7 +848,7 @@ static void f2fs_evict_inode_work(struct work_struct *work)
 /*
  * Return true, if we shouldn't go through post_evict_inode.
  */
-static bool f2fs_pre_evict_inode(struct inode *inode)
+static void f2fs_pre_evict_inode(struct inode *inode)
 {
        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
        struct f2fs_inode_info *fi = F2FS_I(inode);
@@ -908,17 +874,12 @@ static bool f2fs_pre_evict_inode(struct inode *inode)
            test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))
                f2fs_invalidate_compress_pages(sbi, inode->i_ino);
 
-       if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
-               return true;
-
        f2fs_bug_on(sbi, get_dirty_pages(inode));
        f2fs_remove_dirty_inode(inode);
        f2fs_remove_donate_inode(inode);
 
        if (!IS_DEVICE_ALIASING(inode))
                f2fs_destroy_extent_tree(inode);
-
-       return false;
 }
 
 static void f2fs_delete_inode(struct inode *inode)
@@ -1080,15 +1041,13 @@ static void f2fs_post_evict_inode(struct inode *inode)
  */
 void f2fs_evict_inode(struct inode *inode)
 {
-       if (f2fs_pre_evict_inode(inode))
-               goto clear_out;
+       f2fs_pre_evict_inode(inode);
 
        if (!inode->i_nlink && !is_bad_inode(inode))
                f2fs_delete_inode(inode);
 
        f2fs_post_evict_inode(inode);
 
-clear_out:
        fscrypt_put_encryption_info(inode);
        clear_inode(inode);
 }
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 47259e8172fc..6873bad73d7a 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));
@@ -5315,6 +5313,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) &&
@@ -5387,13 +5387,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
@@ -5561,8 +5557,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;
@@ -5676,7 +5670,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 -2
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
 
--

2.49.0

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

author maintainer
#21 lore raw

PATCH v8 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_KMALLOC

---
 fs/f2fs/cache.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
fs/f2fs/cache.c +14 -10
index 3e6a198ef3b3..27624b6799a9 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
Reviewed-by on v8 10/12
#22 lore raw

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

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

Quoted 8 lines · 于2026年9月16日周三 09:57写道:

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_KMALLOC

thanks!

Quoted 55 lines
---
 fs/f2fs/cache.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
fs/f2fs/cache.c +14 -11
index 3e6a198ef3b3..27624b6799a9 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
#23 lore raw

PATCH v8 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/node.c              |  4 +++
 fs/f2fs/segment.c           |  3 ++
 include/trace/events/f2fs.h | 71 +++++++++++++++++++++++++++++++++++++
 5 files changed, 87 insertions(+)
fs/f2fs/cache.c +3 -0
index 27624b6799a9..3f345e5d5186 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 fe7e8bab644b..406bd3b1c77f 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/node.c +4 -0
index fe5e4c4eff23..aa485a09ea6c 100644
@@ -2110,6 +2110,8 @@ int f2fs_writeback_node_caches(struct f2fs_sb_info *sbi, long nr_to_write,
 	int ret = 0;
 	int nr, done = 0;
 
+	trace_f2fs_write_caches(sbi, nr_to_write, 0, NODE);
+
 next_step:
 	index = 0;
 
@@ -2217,6 +2219,8 @@ int f2fs_writeback_node_caches(struct f2fs_sb_info *sbi, long nr_to_write,
 	if (nwritten)
 		f2fs_submit_merged_write(sbi, NODE);
 
+	trace_f2fs_write_caches(sbi, nr_to_write, nwritten, NODE);
+
 	if (unlikely(f2fs_cp_error(sbi)))
 		return -EIO;
 	return ret;
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
Reviewed-by on v8 11/12
#24 lore raw

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

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

Quoted 10 lines · 于2026年9月16日周三 09:57写道:

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

thanks!

Quoted 194 lines
---
 fs/f2fs/cache.c             |  3 ++
 fs/f2fs/checkpoint.c        |  6 ++++
 fs/f2fs/node.c              |  4 +++
 fs/f2fs/segment.c           |  3 ++
 include/trace/events/f2fs.h | 71 +++++++++++++++++++++++++++++++++++++
 5 files changed, 87 insertions(+)
fs/f2fs/cache.c +3 -0
index 27624b6799a9..3f345e5d5186 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 fe7e8bab644b..406bd3b1c77f 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/node.c +4 -0
index fe5e4c4eff23..aa485a09ea6c 100644
@@ -2110,6 +2110,8 @@ int f2fs_writeback_node_caches(struct f2fs_sb_info *sbi, long nr_to_write,
        int ret = 0;
        int nr, done = 0;
 
+       trace_f2fs_write_caches(sbi, nr_to_write, 0, NODE);
+
 next_step:
        index = 0;
 
@@ -2217,6 +2219,8 @@ int f2fs_writeback_node_caches(struct f2fs_sb_info *sbi, long nr_to_write,
        if (nwritten)
                f2fs_submit_merged_write(sbi, NODE);
 
+       trace_f2fs_write_caches(sbi, nr_to_write, nwritten, NODE);
+
        if (unlikely(f2fs_cp_error(sbi)))
                return -EIO;
        return ret;
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 -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 v8 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 a8c24f5282fa..d7983ee70337 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 v8 12/12
#26 lore raw

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

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

Quoted 11 lines · 于2026年9月16日周三 09:57写道:

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 a8c24f5282fa..d7983ee70337 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