Skip to contentSkip to conversation

f2fs: zone: fix to calculate first_zoned_segno correctly

PATCH Superseded Chao Yu 2 participants 5 messages Mar 24, 2025 – Mar 25, 2025 F2FS file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH 1/2 f2fs: zone: fix to calculate first_zoned_segno correctly

A zoned device can has both conventional zones and sequential zones, so we should not treat first segment of zoned device as first_zoned_segno, instead, we need to check zone type for each zone during traversing zoned device to find first_zoned_segno.

Otherwise, for below case, first_zoned_segno will be 0, which could be wrong.

create_null_blk 512 2 1024 1024
mkfs.f2fs -m /dev/nullb0

Fixes 9703d69d9d15 ("f2fs: support file pinning for zoned devices")
Cc Daeho Jeong <[email protected]>
---
 fs/f2fs/f2fs.h    | 18 +++++++++++++-----
 fs/f2fs/segment.c |  2 +-
 fs/f2fs/super.c   | 32 +++++++++++++++++++++++++++-----
 3 files changed, 41 insertions(+), 11 deletions(-)
fs/f2fs/f2fs.h +13 -5
index ca884e39a5ff..3dea037faa55 100644
@@ -4630,12 +4630,16 @@ F2FS_FEATURE_FUNCS(readonly, RO);
 F2FS_FEATURE_FUNCS(device_alias, DEVICE_ALIAS);
 
 #ifdef CONFIG_BLK_DEV_ZONED
-static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
-				    block_t blkaddr)
+static inline bool f2fs_zone_is_seq(struct f2fs_sb_info *sbi, int devi,
+							unsigned int zone)
 {
-	unsigned int zno = blkaddr / sbi->blocks_per_blkz;
+	return test_bit(zone, FDEV(devi).blkz_seq);
+}
 
-	return test_bit(zno, FDEV(devi).blkz_seq);
+static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
+								block_t blkaddr)
+{
+	return f2fs_zone_is_seq(sbi, devi, blkaddr / sbi->blocks_per_blkz);
 }
 #endif
 
@@ -4711,9 +4715,13 @@ static inline bool f2fs_valid_pinned_area(struct f2fs_sb_info *sbi,
 					  block_t blkaddr)
 {
 	if (f2fs_sb_has_blkzoned(sbi)) {
+#ifdef CONFIG_BLK_DEV_ZONED
 		int devi = f2fs_target_device_index(sbi, blkaddr);
 
-		return !bdev_is_zoned(FDEV(devi).bdev);
+		return !f2fs_blkz_is_seq(sbi, devi, blkaddr);
+#else
+		return true;
+#endif
 	}
 	return true;
 }
fs/f2fs/segment.c +1 -1
index 396ef71f41e3..dc360b4b0569 100644
@@ -3311,7 +3311,7 @@ int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi)
 
 	if (f2fs_sb_has_blkzoned(sbi) && err == -EAGAIN && gc_required) {
 		f2fs_down_write(&sbi->gc_lock);
-		err = f2fs_gc_range(sbi, 0, GET_SEGNO(sbi, FDEV(0).end_blk),
+		err = f2fs_gc_range(sbi, 0, sbi->first_zoned_segno - 1,
 				true, ZONED_PIN_SEC_REQUIRED_COUNT);
 		f2fs_up_write(&sbi->gc_lock);
 
fs/f2fs/super.c +27 -5
index 011925ee54f8..b2342366020a 100644
@@ -4307,12 +4307,26 @@ static void f2fs_record_error_work(struct work_struct *work)
 
 static inline unsigned int get_first_zoned_segno(struct f2fs_sb_info *sbi)
 {
-	int devi;
+#ifdef CONFIG_BLK_DEV_ZONED
+	unsigned int segno;
+	int devi, i;
 
-	for (devi = 0; devi < sbi->s_ndevs; devi++)
-		if (bdev_is_zoned(FDEV(devi).bdev))
-			return GET_SEGNO(sbi, FDEV(devi).start_blk);
-	return 0;
+	if (!f2fs_sb_has_blkzoned(sbi))
+		return NULL_SEGNO;
+
+	for (devi = 0; devi < sbi->s_ndevs; devi++) {
+		if (!bdev_is_zoned(FDEV(devi).bdev))
+			continue;
+
+		segno = GET_SEGNO(sbi, FDEV(devi).start_blk);
+		for (i = 0; i < FDEV(devi).total_segments; i++) {
+			if (f2fs_zone_is_seq(sbi, devi,
+				GET_ZONE_FROM_SEG(sbi, segno + i)))
+				return segno + i;
+		}
+	}
+#endif
+	return NULL_SEGNO;
 }
 
 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
@@ -4349,6 +4363,14 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
 #endif
 
 	for (i = 0; i < max_devices; i++) {
+		if (max_devices == 1) {
+			FDEV(i).total_segments =
+				le32_to_cpu(raw_super->segment_count_main);
+			FDEV(i).start_blk = 0;
+			FDEV(i).end_blk = FDEV(i).total_segments *
+						BLKS_PER_SEG(sbi);
+		}
+
 		if (i == 0)
 			FDEV(0).bdev_file = sbi->sb->s_bdev_file;
 		else if (!RDEV(i).path[0])
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH 2/2 f2fs: zone: fix to avoid inconsistence in between SIT and SSA

w/ below testcase, it will cause inconsistence in between SIT and SSA.

create_null_blk 512 2 1024 1024
mkfs.f2fs -m /dev/nullb0
mount /dev/nullb0 /mnt/f2fs/
touch /mnt/f2fs/file
f2fs_io pinfile set /mnt/f2fs/file
fallocate -l 4GiB /mnt/f2fs/file

F2FS-fs (nullb0): Inconsistent segment (0) type [1, 0] in SSA and SIT

CPU: 5 UID: 0 PID: 2398 Comm: fallocate Tainted: G           O       6.13.0-rc1 #84
Tainted: [O]=OOT_MODULE
Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
Call Trace:
 <TASK>
 dump_stack_lvl+0xb3/0xd0
 dump_stack+0x14/0x20
 f2fs_handle_critical_error+0x18c/0x220 [f2fs]
 f2fs_stop_checkpoint+0x38/0x50 [f2fs]
 do_garbage_collect+0x674/0x6e0 [f2fs]
 f2fs_gc_range+0x12b/0x230 [f2fs]
 f2fs_allocate_pinning_section+0x5c/0x150 [f2fs]
 f2fs_expand_inode_data+0x1cc/0x3c0 [f2fs]
 f2fs_fallocate+0x3c3/0x410 [f2fs]
 vfs_fallocate+0x15f/0x4b0
 __x64_sys_fallocate+0x4a/0x80
 x64_sys_call+0x15e8/0x1b80
 do_syscall_64+0x68/0x130
 entry_SYSCALL_64_after_hwframe+0x67/0x6f
RIP: 0033:0x7f9dba5197ca

F2FS-fs (nullb0): Stopped filesystem due to reason: 4

The reason is f2fs_gc_range() may try to migrate block in curseg, however, its SSA block is not uptodate due to the last summary block data in still in cache of curseg.

In this patch, we add a condition in f2fs_gc_range() to check whether section is opened or not, and skip block migration for opened section.

Fixes 9703d69d9d15 ("f2fs: support file pinning for zoned devices")
Cc Daeho Jeong <[email protected]>
---
 fs/f2fs/gc.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/gc.c +3 -0
index 2b8f9239bede..8b5a55b72264 100644
@@ -2066,6 +2066,9 @@ int f2fs_gc_range(struct f2fs_sb_info *sbi,
 			.iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
 		};
 
+		if (IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+			continue;
+
 		do_garbage_collect(sbi, segno, &gc_list, FG_GC, true, false);
 		put_gc_inode(&gc_list);
 
Signature · 1 line
2.48.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [f2fs-dev] [PATCH 2/2] f2fs: zone: fix to avoid inconsistence in between SIT and SSA

Quoted 62 lines · On Mon, Mar 24, 2025 at 4:54 AM Chao Yu via Linux-f2fs-devel…

w/ below testcase, it will cause inconsistence in between SIT and SSA.

create_null_blk 512 2 1024 1024
mkfs.f2fs -m /dev/nullb0
mount /dev/nullb0 /mnt/f2fs/
touch /mnt/f2fs/file
f2fs_io pinfile set /mnt/f2fs/file
fallocate -l 4GiB /mnt/f2fs/file

F2FS-fs (nullb0): Inconsistent segment (0) type [1, 0] in SSA and SIT

CPU: 5 UID: 0 PID: 2398 Comm: fallocate Tainted: G           O       6.13.0-rc1 #84
Tainted: [O]=OOT_MODULE
Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
Call Trace:
 <TASK>
 dump_stack_lvl+0xb3/0xd0
 dump_stack+0x14/0x20
 f2fs_handle_critical_error+0x18c/0x220 [f2fs]
 f2fs_stop_checkpoint+0x38/0x50 [f2fs]
 do_garbage_collect+0x674/0x6e0 [f2fs]
 f2fs_gc_range+0x12b/0x230 [f2fs]
 f2fs_allocate_pinning_section+0x5c/0x150 [f2fs]
 f2fs_expand_inode_data+0x1cc/0x3c0 [f2fs]
 f2fs_fallocate+0x3c3/0x410 [f2fs]
 vfs_fallocate+0x15f/0x4b0
 __x64_sys_fallocate+0x4a/0x80
 x64_sys_call+0x15e8/0x1b80
 do_syscall_64+0x68/0x130
 entry_SYSCALL_64_after_hwframe+0x67/0x6f
RIP: 0033:0x7f9dba5197ca

F2FS-fs (nullb0): Stopped filesystem due to reason: 4

The reason is f2fs_gc_range() may try to migrate block in curseg, however, its SSA block is not uptodate due to the last summary block data in still in cache of curseg.

In this patch, we add a condition in f2fs_gc_range() to check whether section is opened or not, and skip block migration for opened section.

Fixes 9703d69d9d15 ("f2fs: support file pinning for zoned devices")
Cc Daeho Jeong <[email protected]>
---
 fs/f2fs/gc.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/gc.c +3 -1
index 2b8f9239bede..8b5a55b72264 100644
@@ -2066,6 +2066,9 @@ int f2fs_gc_range(struct f2fs_sb_info *sbi,
                        .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
                };
 
+               if (IS_CURSEC(sbi, GET_SEC_FROM_SEG(sbi, segno)))
+                       continue;
+
                do_garbage_collect(sbi, segno, &gc_list, FG_GC, true, false);
                put_gc_inode(&gc_list);
 
--
Quoted 3 lines

2.48.1

Thanks.

Quoted 5 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw
In reply to Chao Yu
Quoted 92 lines · On Mon, Mar 24, 2025 at 4:54 AM Chao Yu via Linux-f2fs-devel…

A zoned device can has both conventional zones and sequential zones, so we should not treat first segment of zoned device as first_zoned_segno, instead, we need to check zone type for each zone during traversing zoned device to find first_zoned_segno.

Otherwise, for below case, first_zoned_segno will be 0, which could be wrong.

create_null_blk 512 2 1024 1024
mkfs.f2fs -m /dev/nullb0

Fixes 9703d69d9d15 ("f2fs: support file pinning for zoned devices")
Cc Daeho Jeong <[email protected]>
---
 fs/f2fs/f2fs.h    | 18 +++++++++++++-----
 fs/f2fs/segment.c |  2 +-
 fs/f2fs/super.c   | 32 +++++++++++++++++++++++++++-----
 3 files changed, 41 insertions(+), 11 deletions(-)
fs/f2fs/f2fs.h +13 -5
index ca884e39a5ff..3dea037faa55 100644
@@ -4630,12 +4630,16 @@ F2FS_FEATURE_FUNCS(readonly, RO);
 F2FS_FEATURE_FUNCS(device_alias, DEVICE_ALIAS);
 
 #ifdef CONFIG_BLK_DEV_ZONED
-static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
-                                   block_t blkaddr)
+static inline bool f2fs_zone_is_seq(struct f2fs_sb_info *sbi, int devi,
+                                                       unsigned int zone)
 {
-       unsigned int zno = blkaddr / sbi->blocks_per_blkz;
+       return test_bit(zone, FDEV(devi).blkz_seq);
+}
 
-       return test_bit(zno, FDEV(devi).blkz_seq);
+static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
+                                                               block_t blkaddr)
+{
+       return f2fs_zone_is_seq(sbi, devi, blkaddr / sbi->blocks_per_blkz);
 }
 #endif
 
@@ -4711,9 +4715,13 @@ static inline bool f2fs_valid_pinned_area(struct f2fs_sb_info *sbi,
                                          block_t blkaddr)
 {
        if (f2fs_sb_has_blkzoned(sbi)) {
+#ifdef CONFIG_BLK_DEV_ZONED
                int devi = f2fs_target_device_index(sbi, blkaddr);
 
-               return !bdev_is_zoned(FDEV(devi).bdev);
+               return !f2fs_blkz_is_seq(sbi, devi, blkaddr);
+#else
+               return true;
+#endif
        }
        return true;
 }
fs/f2fs/segment.c +1 -1
index 396ef71f41e3..dc360b4b0569 100644
@@ -3311,7 +3311,7 @@ int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi)
 
        if (f2fs_sb_has_blkzoned(sbi) && err == -EAGAIN && gc_required) {
                f2fs_down_write(&sbi->gc_lock);
-               err = f2fs_gc_range(sbi, 0, GET_SEGNO(sbi, FDEV(0).end_blk),
+               err = f2fs_gc_range(sbi, 0, sbi->first_zoned_segno - 1,
                                true, ZONED_PIN_SEC_REQUIRED_COUNT);
                f2fs_up_write(&sbi->gc_lock);
 
fs/f2fs/super.c +4 -5
index 011925ee54f8..b2342366020a 100644
@@ -4307,12 +4307,26 @@ static void f2fs_record_error_work(struct work_struct *work)
 
 static inline unsigned int get_first_zoned_segno(struct f2fs_sb_info *sbi)
 {
-       int devi;
+#ifdef CONFIG_BLK_DEV_ZONED
+       unsigned int segno;
+       int devi, i;
 
-       for (devi = 0; devi < sbi->s_ndevs; devi++)
-               if (bdev_is_zoned(FDEV(devi).bdev))
-                       return GET_SEGNO(sbi, FDEV(devi).start_blk);
-       return 0;
+       if (!f2fs_sb_has_blkzoned(sbi))

fs/f2fs/super.c static void f2fs_record_error_work(struct work_struct *work) · line 4324

Quoted 10 lines
fs/f2fs/super.c +10 -0
+               return NULL_SEGNO;
+
+       for (devi = 0; devi < sbi->s_ndevs; devi++) {
+               if (!bdev_is_zoned(FDEV(devi).bdev))
+                       continue;
+
+               segno = GET_SEGNO(sbi, FDEV(devi).start_blk);
+               for (i = 0; i < FDEV(devi).total_segments; i++) {
+                       if (f2fs_zone_is_seq(sbi, devi,
+                               GET_ZONE_FROM_SEG(sbi, segno + i)))

Maybe we can check it with a zone unit?

Quoted 32 lines
fs/f2fs/super.c +13 -1
+                               return segno + i;
+               }
+       }
+#endif
+       return NULL_SEGNO;
 }
 
 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
@@ -4349,6 +4363,14 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
 #endif
 
        for (i = 0; i < max_devices; i++) {
+               if (max_devices == 1) {
+                       FDEV(i).total_segments =
+                               le32_to_cpu(raw_super->segment_count_main);
+                       FDEV(i).start_blk = 0;
+                       FDEV(i).end_blk = FDEV(i).total_segments *
+                                               BLKS_PER_SEG(sbi);
+               }
+
                if (i == 0)
                        FDEV(0).bdev_file = sbi->sb->s_bdev_file;
                else if (!RDEV(i).path[0])
--

2.48.1

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

author maintainer
#4 lore raw
Quoted 98 lines · Daeho Jeong
Quoted 96 lines · On Mon, Mar 24, 2025 at 4:54 AM Chao Yu via Linux-f2fs-devel…

A zoned device can has both conventional zones and sequential zones, so we should not treat first segment of zoned device as first_zoned_segno, instead, we need to check zone type for each zone during traversing zoned device to find first_zoned_segno.

Otherwise, for below case, first_zoned_segno will be 0, which could be wrong.

create_null_blk 512 2 1024 1024
mkfs.f2fs -m /dev/nullb0

Fixes 9703d69d9d15 ("f2fs: support file pinning for zoned devices")
Cc Daeho Jeong <[email protected]>
---
 fs/f2fs/f2fs.h    | 18 +++++++++++++-----
 fs/f2fs/segment.c |  2 +-
 fs/f2fs/super.c   | 32 +++++++++++++++++++++++++++-----
 3 files changed, 41 insertions(+), 11 deletions(-)
fs/f2fs/f2fs.h +13 -5
index ca884e39a5ff..3dea037faa55 100644
@@ -4630,12 +4630,16 @@ F2FS_FEATURE_FUNCS(readonly, RO);
 F2FS_FEATURE_FUNCS(device_alias, DEVICE_ALIAS);
 
 #ifdef CONFIG_BLK_DEV_ZONED
-static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
-                                   block_t blkaddr)
+static inline bool f2fs_zone_is_seq(struct f2fs_sb_info *sbi, int devi,
+                                                       unsigned int zone)
 {
-       unsigned int zno = blkaddr / sbi->blocks_per_blkz;
+       return test_bit(zone, FDEV(devi).blkz_seq);
+}
 
-       return test_bit(zno, FDEV(devi).blkz_seq);
+static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
+                                                               block_t blkaddr)
+{
+       return f2fs_zone_is_seq(sbi, devi, blkaddr / sbi->blocks_per_blkz);
 }
 #endif
 
@@ -4711,9 +4715,13 @@ static inline bool f2fs_valid_pinned_area(struct f2fs_sb_info *sbi,
                                          block_t blkaddr)
 {
        if (f2fs_sb_has_blkzoned(sbi)) {
+#ifdef CONFIG_BLK_DEV_ZONED
                int devi = f2fs_target_device_index(sbi, blkaddr);
 
-               return !bdev_is_zoned(FDEV(devi).bdev);
+               return !f2fs_blkz_is_seq(sbi, devi, blkaddr);
+#else
+               return true;
+#endif
        }
        return true;
 }
fs/f2fs/segment.c +1 -1
index 396ef71f41e3..dc360b4b0569 100644
@@ -3311,7 +3311,7 @@ int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi)
 
        if (f2fs_sb_has_blkzoned(sbi) && err == -EAGAIN && gc_required) {
                f2fs_down_write(&sbi->gc_lock);
-               err = f2fs_gc_range(sbi, 0, GET_SEGNO(sbi, FDEV(0).end_blk),
+               err = f2fs_gc_range(sbi, 0, sbi->first_zoned_segno - 1,
                                true, ZONED_PIN_SEC_REQUIRED_COUNT);
                f2fs_up_write(&sbi->gc_lock);
 
fs/f2fs/super.c +8 -5
index 011925ee54f8..b2342366020a 100644
@@ -4307,12 +4307,26 @@ static void f2fs_record_error_work(struct work_struct *work)
 
 static inline unsigned int get_first_zoned_segno(struct f2fs_sb_info *sbi)
 {
-       int devi;
+#ifdef CONFIG_BLK_DEV_ZONED
+       unsigned int segno;
+       int devi, i;
 
-       for (devi = 0; devi < sbi->s_ndevs; devi++)
-               if (bdev_is_zoned(FDEV(devi).bdev))
-                       return GET_SEGNO(sbi, FDEV(devi).start_blk);
-       return 0;
+       if (!f2fs_sb_has_blkzoned(sbi))
+               return NULL_SEGNO;
+
+       for (devi = 0; devi < sbi->s_ndevs; devi++) {
+               if (!bdev_is_zoned(FDEV(devi).bdev))
Quoted 8 lines

fs/f2fs/super.c static void f2fs_record_error_work(struct work_struct *work) · line 4324

Quoted 6 lines
fs/f2fs/super.c +6 -0
+                       continue;
+
+               segno = GET_SEGNO(sbi, FDEV(devi).start_blk);
+               for (i = 0; i < FDEV(devi).total_segments; i++) {
+                       if (f2fs_zone_is_seq(sbi, devi,
+                               GET_ZONE_FROM_SEG(sbi, segno + i)))

Maybe we can check it with a zone unit?

Yeah, better, let me update it in v2.

Thanks,

Quoted 33 lines
Quoted 33 lines
fs/f2fs/super.c +13 -1
+                               return segno + i;
+               }
+       }
+#endif
+       return NULL_SEGNO;
 }
 
 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
@@ -4349,6 +4363,14 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
 #endif
 
        for (i = 0; i < max_devices; i++) {
+               if (max_devices == 1) {
+                       FDEV(i).total_segments =
+                               le32_to_cpu(raw_super->segment_count_main);
+                       FDEV(i).start_blk = 0;
+                       FDEV(i).end_blk = FDEV(i).total_segments *
+                                               BLKS_PER_SEG(sbi);
+               }
+
                if (i == 0)
                        FDEV(0).bdev_file = sbi->sb->s_bdev_file;
                else if (!RDEV(i).path[0])
--

2.48.1

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel