Skip to content

f2fs: introduce metadata cache

Chao Yu linux-f2fs-devel lkml 9 versions – Applied

Status from this message: This series was applied to jaegeuk/f2fs.git (dev)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v9 Latest12 patches 0 repliesApplied 12/12
  2. v812 patches 14 repliesSuperseded
  3. v712 patches 14 repliesSuperseded
Earlier versions · 6
VersionPostedPatchesRepliesStatus
v612 patches2 repliesSuperseded
v512 patches4 repliesSuperseded
v412 patches0 repliesSuperseded
v312 patches29 repliesSuperseded
v212 patches14 repliesSuperseded 1 of 12 applied
v11 patch0 repliesSuperseded

Changes in v9 from the cover letter

- add a boundary for cache_wb_interval sysfs node
- avoid to exceed 80 column in cache.c in Patch 1/12
- fix to alloc folio for entry->data in Patch 1/12 to avoid accessing incorrect address w/ cache_folio(entry) in IS_INODE(),ino_of_node().. recovery to use kmalloc after dropping cache_folio() in Patch 7/12
- fix to show consistent nr_to_write in trace_f2fs_write_caches() in f2fs_writeback_node_caches()
- :%s/__submit_merged_write_cond/__submit_merged_data_write_cond/g

Patches in v9 All 12 applied

#Subject and filesSizeRepliesApplied
1/12f2fs: cache: implement metadata cachefs/f2fs/Makefile fs/f2fs/cache.c fs/f2fs/cache.h +3 more+1028 -270 repliesApplied
2/12f2fs: cache: initialize meta cachefs/f2fs/cache.h fs/f2fs/data.c fs/f2fs/f2fs.h +1 more+22 -20 repliesApplied
3/12f2fs: cache: introduce shrinkerfs/f2fs/cache.c fs/f2fs/cache.h fs/f2fs/shrinker.c+104 -00 repliesApplied
4/12f2fs: cache: introduce writeback threadDocumentation/ABI/testing/sysfs-fs-f2fs fs/f2fs/cache.c fs/f2fs/cache.h +3 more+142 -30 repliesApplied
5/12f2fs: cache: use meta cachefs/f2fs/cache.c fs/f2fs/cache.h fs/f2fs/checkpoint.c +16 more+658 -7210 repliesApplied
6/12f2fs: cache: initialize node cachefs/f2fs/cache.c fs/f2fs/cache.h fs/f2fs/f2fs.h +1 more+25 -30 repliesApplied
7/12f2fs: cache: use node cachefs/f2fs/acl.c fs/f2fs/acl.h fs/f2fs/cache.c +23 more+1424 -15100 repliesApplied
8/12f2fs: cache: initialize compress cachefs/f2fs/cache.c fs/f2fs/cache.h fs/f2fs/f2fs.h +1 more+29 -70 repliesApplied
9/12f2fs: cache: use compress cachefs/f2fs/compress.c fs/f2fs/debug.c fs/f2fs/f2fs.h +6 more+74 -1600 repliesApplied
10/12f2fs: cache: support fault injectionfs/f2fs/cache.c+14 -100 repliesApplied
11/12f2fs: cache: introduce tracepointsfs/f2fs/cache.c fs/f2fs/checkpoint.c fs/f2fs/node.c +2 more+87 -00 repliesApplied
12/12f2fs: cache: show per-cache usage in debugfsfs/f2fs/cache.h fs/f2fs/debug.c fs/f2fs/f2fs.h+43 -90 repliesApplied

Reviews trailers per patch

All 12 patches have a Reviewed-by or Acked-by.

PatchZhiguo Niu
1/12R-b
2/12R-b
3/12R-b
4/12R-b
5/12R-b
6/12R-b
7/12R-b
8/12R-b
9/12R-b
10/12R-b
11/12R-b
12/12R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v9 thread

Cover letter [PATCH v9 00/12] 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:

v8->v9
- add a boundary for cache_wb_interval sysfs node
- avoid to exceed 80 column in cache.c in Patch 1/12
- fix to alloc folio for entry->data in Patch 1/12 to avoid accessing incorrect address w/ cache_folio(entry) in IS_INODE(),ino_of_node().. recovery to use kmalloc after dropping cache_folio() in Patch 7/12
- fix to show consistent nr_to_write in trace_f2fs_write_caches() in f2fs_writeback_node_caches()
- :%s/__submit_merged_write_cond/__submit_merged_data_write_cond/g

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 |    7 +
 fs/f2fs/Makefile                        |    2 +-
 fs/f2fs/acl.c                           |   26 +-
 fs/f2fs/acl.h                           |    8 +-
 fs/f2fs/cache.c                         |  720 +++++++++++++++
 fs/f2fs/cache.h                         |  240 +++++
 fs/f2fs/checkpoint.c                    |  448 +++++----
 fs/f2fs/compress.c                      |  165 ++--
 fs/f2fs/data.c                          |  523 +++++++----
 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                          | 1104 +++++++++++------------
 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                         |  140 ++-
 fs/f2fs/sysfs.c                         |   22 +-
 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, 3530 insertions(+), 2332 deletions(-)
 create mode 100644 fs/f2fs/cache.c
 create mode 100644 fs/f2fs/cache.h
Signature · 1 line
2.49.0