linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [f2fs-dev] [PATCH] f2fs: don't break allocation when crossing contiguous sections
@ 2025-07-21  2:02 Chao Yu via Linux-f2fs-devel
  2025-07-24 20:30 ` patchwork-bot+f2fs--- via Linux-f2fs-devel
       [not found] ` <CGME20250721020241epcas2p1481962181d9f81f127e122736923fe20@epcms2p6>
  0 siblings, 2 replies; 4+ messages in thread
From: Chao Yu via Linux-f2fs-devel @ 2025-07-21  2:02 UTC (permalink / raw)
  To: jaegeuk; +Cc: linux-kernel, linux-f2fs-devel

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 <daejun7.park@samsung.com>
Signed-off-by: Chao Yu <chao@kernel.org>
---
 fs/f2fs/data.c | 28 ++++++++++++++++++----------
 fs/f2fs/f2fs.h |  1 +
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index d1a2616d41be..4e62f7f00b70 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -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;
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index dfddb66910b3..97c1a2a3fbd7 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -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;
-- 
2.49.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [f2fs-dev] [PATCH] f2fs: don't break allocation when crossing contiguous sections
  2025-07-21  2:02 [f2fs-dev] [PATCH] f2fs: don't break allocation when crossing contiguous sections Chao Yu via Linux-f2fs-devel
@ 2025-07-24 20:30 ` patchwork-bot+f2fs--- via Linux-f2fs-devel
       [not found] ` <CGME20250721020241epcas2p1481962181d9f81f127e122736923fe20@epcms2p6>
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+f2fs--- via Linux-f2fs-devel @ 2025-07-24 20:30 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, linux-kernel, linux-f2fs-devel

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <jaegeuk@kernel.org>:

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html




_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [f2fs-dev] [PATCH] f2fs: don't break allocation when crossing contiguous sections
       [not found] ` <CGME20250721020241epcas2p1481962181d9f81f127e122736923fe20@epcms2p6>
@ 2025-07-30  1:53   ` Daejun Park
  2025-07-30  7:39     ` Chao Yu via Linux-f2fs-devel
  0 siblings, 1 reply; 4+ messages in thread
From: Daejun Park @ 2025-07-30  1:53 UTC (permalink / raw)
  To: Chao Yu, jaegeuk@kernel.org
  Cc: linux-kernel@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net

Hi Chao Yu,

> +        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.

> @@ -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.

> +                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.

Reviewed-by: Daejun Park <daejun7.park@samsung.com>



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [f2fs-dev] [PATCH] f2fs: don't break allocation when crossing contiguous sections
  2025-07-30  1:53   ` Daejun Park
@ 2025-07-30  7:39     ` Chao Yu via Linux-f2fs-devel
  0 siblings, 0 replies; 4+ messages in thread
From: Chao Yu via Linux-f2fs-devel @ 2025-07-30  7:39 UTC (permalink / raw)
  To: daejun7.park, jaegeuk@kernel.org
  Cc: linux-kernel@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net

Hi Daejun

Thanks for the comments.

On 7/30/25 09:53, Daejun Park wrote:
> Hi Chao Yu,
> 
>> +        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.

> 
>> @@ -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,

> 
>> +                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.
> 
> Reviewed-by: Daejun Park <daejun7.park@samsung.com>
> 



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-07-30  7:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-21  2:02 [f2fs-dev] [PATCH] f2fs: don't break allocation when crossing contiguous sections Chao Yu via Linux-f2fs-devel
2025-07-24 20:30 ` patchwork-bot+f2fs--- via Linux-f2fs-devel
     [not found] ` <CGME20250721020241epcas2p1481962181d9f81f127e122736923fe20@epcms2p6>
2025-07-30  1:53   ` Daejun Park
2025-07-30  7:39     ` Chao Yu via Linux-f2fs-devel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).