f2fs: don't break allocation when crossing contiguous sections
authorChao Yu <chao@kernel.org>
Mon, 21 Jul 2025 02:02:31 +0000 (10:02 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Thu, 24 Jul 2025 20:18:59 +0000 (20:18 +0000)
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>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/data.c
fs/f2fs/f2fs.h

index 0acc25f996b3c10819a427ab91ae2e36ff5d2c83..e11dd1431e5b537d02ba5ae500d506638dc2a32d 100644 (file)
@@ -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 @@ next_block:
        /* 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 @@ next_block:
 
                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 @@ skip:
                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;
index dfddb66910b3f1ecc305d8241cf9ba2f28431ccf..97c1a2a3fbd7e0aadcf64b63517a3b0586f916ec 100644 (file)
@@ -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;