[PATCH v2 12/20] f2fs: support subpage blocks in defragmentation

From: Daeho Jeong

Date: Sun Nov 15 2026 - 23:10:12 EST


From: Daeho Jeong <daehojeong@xxxxxxxxxx>

Mark only the target block, not its whole page, dirty when redirtying
data for defragmentation. Remove the -EOPNOTSUPP check.

Read a multi-block folio once through ->read_folio() instead of each
block synchronously, and redirty the folio that was read after locking
it. This also applies to large folios when the block size equals
PAGE_SIZE.

Read compressed files through ->read_folio() as well, with any block
size. f2fs_get_lock_data_folio() reads the mapped block as it is, so a
block of a compressed cluster that was not in the page cache was read
without decompression, and defragmentation wrote the compressed data
back as file data.

Signed-off-by: Daeho Jeong <daehojeong@xxxxxxxxxx>
---
fs/f2fs/file.c | 44 ++++++++++++++++++++++++++++++++++++++------
1 file changed, 38 insertions(+), 6 deletions(-)

diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 4499a461be86..26dd06961389 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -3202,6 +3202,41 @@ static int f2fs_ioc_write_checkpoint(struct file *filp)
return ret;
}

+/*
+ * Return the locked folio of a block to redirty it for defragmentation.
+ *
+ * A compressed file is read through ->read_folio(), which decompresses the
+ * cluster: f2fs_get_lock_data_folio() would read the mapped block as it is
+ * and leave compressed data in the page cache. A multi-block folio is read
+ * the same way, in one I/O instead of one synchronous read per block.
+ */
+static struct folio *f2fs_get_defrag_folio(struct file *filp, pgoff_t bidx)
+{
+ struct inode *inode = file_inode(filp);
+ struct address_space *mapping = inode->i_mapping;
+ pgoff_t index = f2fs_lblk_to_folio_index(F2FS_I_SB(inode), bidx);
+ struct folio *folio;
+
+ if (!f2fs_compressed_file(inode) && !f2fs_has_multiblock_folios(inode))
+ return f2fs_get_lock_data_folio(inode, bidx, true);
+
+repeat:
+ folio = read_mapping_folio(mapping, index, filp);
+ if (IS_ERR(folio))
+ return folio;
+
+ folio_lock(folio);
+ if (unlikely(folio->mapping != mapping)) {
+ f2fs_folio_put(folio, true);
+ goto repeat;
+ }
+ if (unlikely(!folio_test_uptodate(folio))) {
+ f2fs_folio_put(folio, true);
+ return ERR_PTR(-EIO);
+ }
+ return folio;
+}
+
static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
struct file *filp,
struct f2fs_defragment *range)
@@ -3327,7 +3362,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
cnt < BLKS_PER_SEG(sbi)) {
struct folio *folio;

- folio = f2fs_get_lock_data_folio(inode, idx, true);
+ folio = f2fs_get_defrag_folio(filp, idx);
if (IS_ERR(folio)) {
err = PTR_ERR(folio);
goto clear_out;
@@ -3336,8 +3371,8 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
f2fs_folio_wait_writeback(folio, true, true);

f2fs_ffs_mark_subrange_dirty(folio,
- offset_in_folio(folio, (loff_t)idx << PAGE_SHIFT),
- PAGE_SIZE);
+ f2fs_folio_byte_offset(sbi, folio, idx),
+ F2FS_BLKSIZE(sbi));
f2fs_folio_mark_dirty(folio);
folio_set_f2fs_gcing(folio);
f2fs_folio_put(folio, true);
@@ -3378,9 +3413,6 @@ static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
struct f2fs_defragment range;
int err;

- if (f2fs_has_subpage_blocks(sbi))
- return -EOPNOTSUPP;
-
if (!capable(CAP_SYS_ADMIN))
return -EPERM;

--
2.56.0.611.g1030f0b940-goog