[PATCH v2 10/20] f2fs: support subpage blocks in collapse and insert range
From: Daeho Jeong
Date: Sun Nov 15 2026 - 23:09:26 EST
From: Daeho Jeong <daehojeong@xxxxxxxxxx>
The folio holding offset may hold blocks before the shifted range.
Write back the page cache from the PAGE_SIZE boundary below offset
under invalidate_lock before truncating it from there, so that no
data is lost. Detect src and dst in the same folio by folio index
instead of block index, as a folio holds several blocks. Remove the
-EOPNOTSUPP check.
Also take i_gc_rwsem[WRITE] and invalidate_lock before that writeback
and keep them until the moved blocks are written back and the page
cache is dropped, as move range does. After the block exchange, the
page cache holds the old data of the source blocks until then. With
subpage blocks, page_mkwrite dirties every block of a folio up to EOF,
so a store to a neighbouring block in between writes the old data back,
over a hole or over the moved data. This also keeps GC away from the
inode until then, for any block size.
Signed-off-by: Daeho Jeong <daehojeong@xxxxxxxxxx>
---
fs/f2fs/file.c | 115 ++++++++++++++++++++++++++++---------------------
1 file changed, 65 insertions(+), 50 deletions(-)
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 72c601d240d4..d4472143dea0 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -1671,6 +1671,7 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
} else {
struct folio *fsrc, *fdst;
size_t src_off, dst_off;
+ pgoff_t dst_fidx = f2fs_lblk_to_folio_index(sbi, dst + i);
bool same_folio;
fsrc = f2fs_get_lock_data_folio(src_inode,
@@ -1683,11 +1684,11 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
/*
* For same-inode operations (collapse/insert), src and
- * dst may fall within the same large folio. Detect this
+ * dst may fall within the same folio. Detect this
* to avoid self-deadlock on folio lock.
*/
same_folio = src_inode == dst_inode &&
- folio_contains(fsrc, dst + i);
+ folio_contains(fsrc, dst_fidx);
if (same_folio) {
struct dnode_of_data dn;
@@ -1795,30 +1796,30 @@ static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct f2fs_lock_context lc;
- pgoff_t nrpages = F2FS_BLK_ALIGN(sbi, i_size_read(inode));
+ pgoff_t nrblocks = F2FS_BLK_ALIGN(sbi, i_size_read(inode));
pgoff_t start = F2FS_BYTES_TO_BLK(sbi, offset);
pgoff_t end = F2FS_BYTES_TO_BLK(sbi, offset + len);
int ret;
- f2fs_balance_fs(sbi, true);
-
- /* avoid gc operation during block exchange */
- f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
- filemap_invalidate_lock(inode->i_mapping);
+ /*
+ * Write back under invalidate_lock, which blocks page_mkwrite, so
+ * that truncate_pagecache() below cannot drop a racing mmap store to
+ * the blocks before @offset in the head folio.
+ */
+ ret = filemap_write_and_wait_range(inode->i_mapping,
+ round_down(offset, PAGE_SIZE), LLONG_MAX);
+ if (ret)
+ return ret;
ret = f2fs_zero_post_eof_page(inode, offset + len, false, false);
if (ret)
- goto out_unlock;
+ return ret;
f2fs_lock_op(sbi, &lc);
f2fs_drop_extent_tree(inode);
- truncate_pagecache(inode, offset);
- ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
+ truncate_pagecache(inode, round_down(offset, PAGE_SIZE));
+ ret = __exchange_data_block(inode, inode, end, start, nrblocks - end, true);
f2fs_unlock_op(sbi, &lc);
-
-out_unlock:
- filemap_invalidate_unlock(inode->i_mapping);
- f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
return ret;
}
@@ -1839,28 +1840,35 @@ static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
if (ret)
return ret;
- /* write out all dirty pages from offset */
- ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
- if (ret)
- return ret;
+ f2fs_balance_fs(F2FS_I_SB(inode), true);
+
+ /*
+ * Avoid GC during the block exchange, and keep both locks until the
+ * moved blocks are written back and the page cache is dropped. Until
+ * then, it holds the old data of the source blocks, which neither
+ * page_mkwrite nor GC may write back.
+ */
+ f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
+ filemap_invalidate_lock(inode->i_mapping);
ret = f2fs_do_collapse(inode, offset, len);
if (ret)
- return ret;
+ goto out_unlock;
/* write out all moved pages, if possible */
- filemap_invalidate_lock(inode->i_mapping);
- ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
+ ret = filemap_write_and_wait_range(inode->i_mapping,
+ round_down(offset, PAGE_SIZE), LLONG_MAX);
if (ret)
goto out_unlock;
- truncate_pagecache(inode, offset);
+ truncate_pagecache(inode, round_down(offset, PAGE_SIZE));
new_size = i_size_read(inode) - len;
ret = f2fs_truncate_blocks(inode, new_size, true);
-out_unlock:
- filemap_invalidate_unlock(inode->i_mapping);
if (!ret)
f2fs_i_size_write(inode, new_size);
+out_unlock:
+ filemap_invalidate_unlock(inode->i_mapping);
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
return ret;
}
@@ -2040,7 +2048,7 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct address_space *mapping = inode->i_mapping;
- pgoff_t nr, pg_start, pg_end, delta, idx;
+ pgoff_t nr, blk_start, blk_end, delta, idx;
loff_t new_size;
int ret = 0;
@@ -2053,8 +2061,8 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
return -EINVAL;
/* insert range should be aligned to block size of f2fs. */
- if (offset & F2FS_BLKSIZE_MASK(F2FS_I_SB(inode)) ||
- len & F2FS_BLKSIZE_MASK(F2FS_I_SB(inode)))
+ if (offset & F2FS_BLKSIZE_MASK(sbi) ||
+ len & F2FS_BLKSIZE_MASK(sbi))
return -EINVAL;
ret = f2fs_convert_inline_inode(inode);
@@ -2069,29 +2077,34 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
if (ret)
return ret;
- /* write out all dirty pages from offset */
- ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
- if (ret)
- return ret;
-
- pg_start = F2FS_BYTES_TO_BLK(sbi, offset);
- pg_end = F2FS_BYTES_TO_BLK(sbi, offset + len);
- delta = pg_end - pg_start;
+ blk_start = F2FS_BYTES_TO_BLK(sbi, offset);
+ blk_end = F2FS_BYTES_TO_BLK(sbi, offset + len);
+ delta = blk_end - blk_start;
idx = F2FS_BLK_ALIGN(sbi, i_size_read(inode));
/* avoid gc operation during block exchange */
f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
filemap_invalidate_lock(mapping);
+ /*
+ * Write back under invalidate_lock, which blocks page_mkwrite, so
+ * that truncate_pagecache() below cannot drop a racing mmap store to
+ * the blocks before @offset in the head folio.
+ */
+ ret = filemap_write_and_wait_range(mapping,
+ round_down(offset, PAGE_SIZE), LLONG_MAX);
+ if (ret)
+ goto out_unlock;
+
ret = f2fs_zero_post_eof_page(inode, offset + len, false, false);
if (ret)
goto out_unlock;
- truncate_pagecache(inode, offset);
+ truncate_pagecache(inode, round_down(offset, PAGE_SIZE));
- while (!ret && idx > pg_start) {
+ while (!ret && idx > blk_start) {
struct f2fs_lock_context lc;
- nr = idx - pg_start;
+ nr = idx - blk_start;
if (nr > delta)
nr = delta;
idx -= nr;
@@ -2103,20 +2116,23 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
idx + delta, nr, false);
f2fs_unlock_op(sbi, &lc);
}
-out_unlock:
- filemap_invalidate_unlock(mapping);
- f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
if (ret)
- return ret;
+ goto out_unlock;
- /* write out all moved pages, if possible */
- filemap_invalidate_lock(mapping);
- ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
- truncate_pagecache(inode, offset);
- filemap_invalidate_unlock(mapping);
+ /*
+ * Write out all moved pages, if possible. Keep both locks until the
+ * page cache is dropped. Until then, it holds the old data of the
+ * source blocks, which neither page_mkwrite nor GC may write back.
+ */
+ ret = filemap_write_and_wait_range(mapping,
+ round_down(offset, PAGE_SIZE), LLONG_MAX);
+ truncate_pagecache(inode, round_down(offset, PAGE_SIZE));
if (!ret)
f2fs_i_size_write(inode, new_size);
+out_unlock:
+ filemap_invalidate_unlock(mapping);
+ f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
return ret;
}
@@ -2298,8 +2314,7 @@ static long f2fs_fallocate(struct file *file, int mode,
}
if (f2fs_has_subpage_blocks(F2FS_I_SB(inode))) {
- if ((mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) ||
- f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode)) {
+ if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode)) {
ret = -EOPNOTSUPP;
goto out;
}
--
2.56.0.611.g1030f0b940-goog