[PATCH v2 1/2] f2fs: extract do_migrate_one_data_block() helper for GC migration

From: Daejun Park

Date: Fri Jun 05 2026 - 05:28:10 EST


Pull gc_data_segment()'s per-block migration body out into a static
helper. The lock acquisition, move_data_{page,block}() dispatch,
i_gc_rwsem release and stat_inc_data_blk_count() call are now shared
through a single point so future migration paths (e.g. inode-local
packing) can reuse them instead of duplicating the sequence.

While here, change add_gc_inode() to return the inserted (or already
present) inode_entry pointer. The caller still discards it for now;
upcoming work needs the pointer to attach per-inode state to the
entry without an extra radix-tree lookup.

No behavioral change.

Signed-off-by: Daejun Park <daejun7.park@xxxxxxxxxxx>
---
fs/f2fs/gc.c | 109 ++++++++++++++++++++++++++++++---------------------
1 file changed, 64 insertions(+), 45 deletions(-)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index 69e0a8672..e232dff72 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -991,13 +991,15 @@ static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino)
return NULL;
}

-static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)
+static struct inode_entry *add_gc_inode(struct gc_inode_list *gc_list,
+ struct inode *inode)
{
struct inode_entry *new_ie;

- if (inode == find_gc_inode(gc_list, inode->i_ino)) {
+ new_ie = radix_tree_lookup(&gc_list->iroot, inode->i_ino);
+ if (new_ie && new_ie->inode == inode) {
iput(inode);
- return;
+ return new_ie;
}
new_ie = f2fs_kmem_cache_alloc(f2fs_inode_entry_slab,
GFP_NOFS, true, NULL);
@@ -1005,6 +1007,7 @@ static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode)

f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie);
list_add_tail(&new_ie->list, &gc_list->ilist);
+ return new_ie;
}

static void put_gc_inode(struct gc_inode_list *gc_list)
@@ -1579,6 +1582,61 @@ static int move_data_page(struct inode *inode, block_t bidx, int gc_type,
return err;
}

+/*
+ * do_migrate_one_data_block - migrate one valid data block at @segno+@off,
+ * identified by (@nofs, @ofs_in_node) on @inode, into the destination
+ * curseg via move_data_{page,block}().
+ *
+ * Takes i_gc_rwsem for regular files; on rwsem contention the block is
+ * skipped and sbi->skipped_gc_rwsem is incremented. Returns the number
+ * of blocks submitted for write (0 or 1).
+ */
+static int do_migrate_one_data_block(struct f2fs_sb_info *sbi,
+ struct inode *inode,
+ unsigned int segno, int off,
+ unsigned int nofs,
+ unsigned int ofs_in_node, int gc_type)
+{
+ struct f2fs_inode_info *fi = F2FS_I(inode);
+ bool locked = false;
+ block_t start_bidx;
+ int err;
+ int submitted = 0;
+
+ if (S_ISREG(inode->i_mode)) {
+ if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[WRITE])) {
+ sbi->skipped_gc_rwsem++;
+ return 0;
+ }
+ if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[READ])) {
+ sbi->skipped_gc_rwsem++;
+ f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
+ return 0;
+ }
+ locked = true;
+
+ /* wait for all inflight aio data */
+ inode_dio_wait(inode);
+ }
+
+ start_bidx = f2fs_start_bidx_of_node(nofs, inode) + ofs_in_node;
+ if (f2fs_meta_inode_gc_required(inode))
+ err = move_data_block(inode, start_bidx, gc_type, segno, off);
+ else
+ err = move_data_page(inode, start_bidx, gc_type, segno, off);
+
+ if (!err && (gc_type == FG_GC || f2fs_meta_inode_gc_required(inode)))
+ submitted = 1;
+
+ if (locked) {
+ f2fs_up_write(&fi->i_gc_rwsem[READ]);
+ f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
+ }
+
+ stat_inc_data_blk_count(sbi, 1, gc_type);
+ return submitted;
+}
+
/*
* This function tries to get parent node of victim data block, and identifies
* data block validity. If the block is valid, copy that with cold status and
@@ -1712,48 +1770,9 @@ static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,

/* phase 4 */
inode = find_gc_inode(gc_list, dni.ino);
- if (inode) {
- struct f2fs_inode_info *fi = F2FS_I(inode);
- bool locked = false;
- int err;
-
- if (S_ISREG(inode->i_mode)) {
- if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[WRITE])) {
- sbi->skipped_gc_rwsem++;
- continue;
- }
- if (!f2fs_down_write_trylock(
- &fi->i_gc_rwsem[READ])) {
- sbi->skipped_gc_rwsem++;
- f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
- continue;
- }
- locked = true;
-
- /* wait for all inflight aio data */
- inode_dio_wait(inode);
- }
-
- start_bidx = f2fs_start_bidx_of_node(nofs, inode)
- + ofs_in_node;
- if (f2fs_meta_inode_gc_required(inode))
- err = move_data_block(inode, start_bidx,
- gc_type, segno, off);
- else
- err = move_data_page(inode, start_bidx, gc_type,
- segno, off);
-
- if (!err && (gc_type == FG_GC ||
- f2fs_meta_inode_gc_required(inode)))
- submitted++;
-
- if (locked) {
- f2fs_up_write(&fi->i_gc_rwsem[READ]);
- f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
- }
-
- stat_inc_data_blk_count(sbi, 1, gc_type);
- }
+ if (inode)
+ submitted += do_migrate_one_data_block(sbi, inode,
+ segno, off, nofs, ofs_in_node, gc_type);
}

if (++phase < 5) {
--
2.43.0