Skip to contentSkip to conversation

f2fs: don't break allocation when crossing contiguous sections

PATCH Applied Chao Yu 3 participants 4 messages Jul 21, 2025 – Jul 30, 2025 F2FS file system

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH 1/1 f2fs: don't break allocation when crossing contiguous sections

Commit 0638a3197c19 ("f2fs: avoid unused block when dio write in LFS mode") has fixed unused block issue for dio write in lfs mode.

However, f2fs_map_blocks() may break and return smaller extent when last allocated block locates in the end of section, even allocator can allocate contiguous blocks across sections.

Actually, for the case that allocator returns a block address which is not contiguous w/ current extent, we can record the block address in iomap->private, in the next round, skip reallocating for the last allocated block, then we can fix unused block issue, meanwhile, also, we can allocates contiguous physical blocks as much as possible for dio write in lfs mode.

Testcase:
- mkfs.f2fs -f /dev/vdb
- mount -o mode=lfs /dev/vdb /mnt/f2fs
- dd if=/dev/zero of=/mnt/f2fs/file bs=1M count=3; sync;
- dd if=/dev/zero of=/mnt/f2fs/dio bs=2M count=1 oflag=direct;
- umount /mnt/f2fs

Before:
f2fs_map_blocks: dev = (253,16), ino = 4, file offset = 0, start blkaddr = 0x0, len = 0x100, flags = 1, seg_type = 8, may_create = 1, multidevice = 0, flag = 5, err = 0
f2fs_map_blocks: dev = (253,16), ino = 4, file offset = 256, start blkaddr = 0x0, len = 0x100, flags = 1, seg_type = 8, may_create = 1, multidevice = 0, flag = 5, err = 0
f2fs_map_blocks: dev = (253,16), ino = 4, file offset = 512, start blkaddr = 0x0, len = 0x100, flags = 1, seg_type = 8, may_create = 1, multidevice = 0, flag = 5, err = 0
f2fs_map_blocks: dev = (253,16), ino = 5, file offset = 0, start blkaddr = 0x4700, len = 0x100, flags = 3, seg_type = 1, may_create = 1, multidevice = 0, flag = 3, err = 0
f2fs_map_blocks: dev = (253,16), ino = 5, file offset = 256, start blkaddr = 0x4800, len = 0x100, flags = 3, seg_type = 1, may_create = 1, multidevice = 0, flag = 3, err = 0

After:
f2fs_map_blocks: dev = (253,16), ino = 4, file offset = 0, start blkaddr = 0x0, len = 0x100, flags = 1, seg_type = 8, may_create = 1, multidevice = 0, flag = 5, err = 0
f2fs_map_blocks: dev = (253,16), ino = 4, file offset = 256, start blkaddr = 0x0, len = 0x100, flags = 1, seg_type = 8, may_create = 1, multidevice = 0, flag = 5, err = 0
f2fs_map_blocks: dev = (253,16), ino = 4, file offset = 512, start blkaddr = 0x0, len = 0x100, flags = 1, seg_type = 8, may_create = 1, multidevice = 0, flag = 5, err = 0
f2fs_map_blocks: dev = (253,16), ino = 5, file offset = 0, start blkaddr = 0x4700, len = 0x200, flags = 3, seg_type = 1, may_create = 1, multidevice = 0, flag = 3, err = 0

Cc Daejun Park <[email protected]>
---
 fs/f2fs/data.c | 28 ++++++++++++++++++----------
 fs/f2fs/f2fs.h |  1 +
 2 files changed, 19 insertions(+), 10 deletions(-)
fs/f2fs/data.c +18 -10
index d1a2616d41be..4e62f7f00b70 100644
@@ -1550,10 +1550,14 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 	unsigned int start_pgofs;
 	int bidx = 0;
 	bool is_hole;
+	bool lfs_dio_write;
 
 	if (!maxblocks)
 		return 0;
 
+	lfs_dio_write = (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
+				map->m_may_create);
+
 	if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag))
 		goto out;
 
@@ -1600,7 +1604,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 	/* use out-place-update for direct IO under LFS mode */
 	if (map->m_may_create && (is_hole ||
 		(flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
-		!f2fs_is_pinned_file(inode)))) {
+		!f2fs_is_pinned_file(inode) && map->m_last_pblk != blkaddr))) {
 		if (unlikely(f2fs_cp_error(sbi))) {
 			err = -EIO;
 			goto sync_out;
@@ -1684,10 +1688,15 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 
 		if (map->m_multidev_dio)
 			map->m_bdev = FDEV(bidx).bdev;
+
+		if (lfs_dio_write)
+			map->m_last_pblk = NULL_ADDR;
 	} else if (map_is_mergeable(sbi, map, blkaddr, flag, bidx, ofs)) {
 		ofs++;
 		map->m_len++;
 	} else {
+		if (lfs_dio_write && !f2fs_is_pinned_file(inode))
+			map->m_last_pblk = blkaddr;
 		goto sync_out;
 	}
 
@@ -1712,14 +1721,6 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 		dn.ofs_in_node = end_offset;
 	}
 
-	if (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
-	    map->m_may_create) {
-		/* the next block to be allocated may not be contiguous. */
-		if (GET_SEGOFF_FROM_SEG0(sbi, blkaddr) % BLKS_PER_SEC(sbi) ==
-		    CAP_BLKS_PER_SEC(sbi) - 1)
-			goto sync_out;
-	}
-
 	if (pgofs >= end)
 		goto sync_out;
 	else if (dn.ofs_in_node < end_offset)
@@ -4162,7 +4163,7 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 			    unsigned int flags, struct iomap *iomap,
 			    struct iomap *srcmap)
 {
-	struct f2fs_map_blocks map = {};
+	struct f2fs_map_blocks map = { NULL, };
 	pgoff_t next_pgofs = 0;
 	int err;
 
@@ -4171,6 +4172,10 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 	map.m_next_pgofs = &next_pgofs;
 	map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode),
 						inode->i_write_hint);
+	if (flags & IOMAP_WRITE && iomap->private) {
+		map.m_last_pblk = (unsigned long)iomap->private;
+		iomap->private = NULL;
+	}
 
 	/*
 	 * If the blocks being overwritten are already allocated,
@@ -4209,6 +4214,9 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		iomap->flags |= IOMAP_F_MERGED;
 		iomap->bdev = map.m_bdev;
 		iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk);
+
+		if (flags & IOMAP_WRITE && map.m_last_pblk)
+			iomap->private = (void *)map.m_last_pblk;
 	} else {
 		if (flags & IOMAP_WRITE)
 			return -ENOTBLK;
fs/f2fs/f2fs.h +1 -0
index dfddb66910b3..97c1a2a3fbd7 100644
@@ -732,6 +732,7 @@ struct f2fs_map_blocks {
 	block_t m_lblk;
 	unsigned int m_len;
 	unsigned int m_flags;
+	unsigned long m_last_pblk;	/* last allocated block, only used for DIO in LFS mode */
 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
 	pgoff_t *m_next_extent;		/* point to next possible extent */
 	int m_seg_type;
Signature · 1 line
2.49.0
#1 lore raw
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 8 lines · On Mon, 21 Jul 2025 10:02:31 +0800 you wrote:

Commit 0638a3197c19 ("f2fs: avoid unused block when dio write in LFS mode") has fixed unused block issue for dio write in lfs mode.

However, f2fs_map_blocks() may break and return smaller extent when last allocated block locates in the end of section, even allocator can allocate contiguous blocks across sections.

[...]

Here is the summary with links:
- [f2fs-dev] f2fs: don't break allocation when crossing contiguous sections

    https://git.kernel.org/jaegeuk/f2fs/c/f0a7adfedcc8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Reviewed-by on v1 1/1
#2 lore raw
In reply to Chao Yu

Hi Chao Yu,

Quoted 3 lines

+        lfs_dio_write = (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
+                                map->m_may_create);
+

Consider moving the lfs_dio_write assignment to just after the if (!map->m_may_create …) check so it isn’t evaluated when creation isn’t allowed.

fs/f2fs/data.c static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 4175

Quoted 5 lines
fs/f2fs/data.c +1 -0
@@ -4171,6 +4172,10 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
          map.m_next_pgofs = &next_pgofs;
          map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode),
                                                  inode->i_write_hint);
+        if (flags & IOMAP_WRITE && iomap->private) {

Since iomap->private is only set on the LFS DIO path, you can drop the flags & IOMAP_WRITE and test the pointer directly.

fs/f2fs/data.c static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 4219

Quoted 13 lines
fs/f2fs/data.c +6 -0
+                map.m_last_pblk = (unsigned long)iomap->private;
+                iomap->private = NULL;
+        }
 
          /*
           * If the blocks being overwritten are already allocated,
@@ -4209,6 +4214,9 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
                  iomap->flags = IOMAP_F_MERGED;
                  iomap->bdev = map.m_bdev;
                  iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk);
+
+                if (flags & IOMAP_WRITE && map.m_last_pblk)
+                        iomap->private = (void *)map.m_last_pblk;

Likewise, checking only map.m_last_pblk is sufficient for restoring.

author maintainer
#3 lore raw

Hi Daejun

Thanks for the comments.

Quoted 8 lines · Daejun Park

Hi Chao Yu,

Quoted 3 lines

+        lfs_dio_write = (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) &&
+                                map->m_may_create);
+

Consider moving the lfs_dio_write assignment to just after the if (!map->m_may_create …) check so it isn’t evaluated when creation isn’t allowed.

Correct.

It's too late to clean up the code as it is near to the merge window.

Quoted 9 lines

fs/f2fs/data.c static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 4175

Quoted 6 lines
fs/f2fs/data.c +1 -0
@@ -4171,6 +4172,10 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
          map.m_next_pgofs = &next_pgofs;
          map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode),
                                                  inode->i_write_hint);
+        if (flags & IOMAP_WRITE && iomap->private) {

Since iomap->private is only set on the LFS DIO path, you can drop the flags & IOMAP_WRITE and test the pointer directly.

Well, that's right, but I still want to check this condition explicitly to avoid any missing case or further misunderstanding on the code. :)

Thanks,

Quoted 19 lines
Quoted 8 lines
fs/f2fs/data.c +3 -0
+                map.m_last_pblk = (unsigned long)iomap->private;
+                iomap->private = NULL;
+        }
 
          /*
           * If the blocks being overwritten are already allocated,

fs/f2fs/data.c static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, · line 4219

Quoted 6 lines
fs/f2fs/data.c +3 -0
@@ -4209,6 +4214,9 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
                  iomap->flags = IOMAP_F_MERGED;
                  iomap->bdev = map.m_bdev;
                  iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk);
+
+                if (flags & IOMAP_WRITE && map.m_last_pblk)
+                        iomap->private = (void *)map.m_last_pblk;

Likewise, checking only map.m_last_pblk is sufficient for restoring.