[f2fs-dev] [PATCH v2] f2fs: zone: don't block IO if there is remained open zone

2024-03-28 Thread Chao Yu
max open zone may be larger than log header number of f2fs, for
such case, it doesn't need to wait last IO in previous zone, let's
introduce available_open_zone semaphore, and reduce it once we
submit first write IO in a zone, and increase it after completion
of last IO in the zone.

Cc: Daeho Jeong 
Signed-off-by: Chao Yu 
---
v2:
- remove unneeded declaration.
 fs/f2fs/data.c| 80 +--
 fs/f2fs/f2fs.h| 33 ---
 fs/f2fs/iostat.c  |  7 +
 fs/f2fs/iostat.h  |  2 ++
 fs/f2fs/segment.c | 58 ++
 fs/f2fs/segment.h | 12 ++-
 fs/f2fs/super.c   |  2 ++
 7 files changed, 145 insertions(+), 49 deletions(-)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 0d88649c60a5..132a3ede60b1 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -373,11 +373,10 @@ static void f2fs_write_end_io(struct bio *bio)
 #ifdef CONFIG_BLK_DEV_ZONED
 static void f2fs_zone_write_end_io(struct bio *bio)
 {
-   struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private;
+   struct f2fs_sb_info *sbi = iostat_get_bio_private(bio);
 
-   bio->bi_private = io->bi_private;
-   complete(>zone_wait);
f2fs_write_end_io(bio);
+   up(>available_open_zones);
 }
 #endif
 
@@ -531,6 +530,24 @@ static void __submit_merged_bio(struct f2fs_bio_info *io)
if (!io->bio)
return;
 
+#ifdef CONFIG_BLK_DEV_ZONED
+   if (io->open_zone) {
+   /*
+* if there is no open zone, it will wait for last IO in
+* previous zone before submitting new IO.
+*/
+   down(>sbi->available_open_zones);
+   io->open_zone = false;
+   io->zone_openned = true;
+   }
+
+   if (io->close_zone) {
+   io->bio->bi_end_io = f2fs_zone_write_end_io;
+   io->zone_openned = false;
+   io->close_zone = false;
+   }
+#endif
+
if (is_read_io(fio->op)) {
trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
f2fs_submit_read_bio(io->sbi, io->bio, fio->type);
@@ -601,9 +618,9 @@ int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
INIT_LIST_HEAD(>write_io[i][j].bio_list);
init_f2fs_rwsem(>write_io[i][j].bio_list_lock);
 #ifdef CONFIG_BLK_DEV_ZONED
-   init_completion(>write_io[i][j].zone_wait);
-   sbi->write_io[i][j].zone_pending_bio = NULL;
-   sbi->write_io[i][j].bi_private = NULL;
+   sbi->write_io[i][j].open_zone = false;
+   sbi->write_io[i][j].zone_openned = false;
+   sbi->write_io[i][j].close_zone = false;
 #endif
}
}
@@ -918,22 +935,16 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
 }
 
 #ifdef CONFIG_BLK_DEV_ZONED
-static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr)
+static bool is_blkaddr_zone_boundary(struct f2fs_sb_info *sbi,
+   block_t blkaddr, bool start)
 {
-   int devi = 0;
+   if (!f2fs_blkaddr_in_seqzone(sbi, blkaddr))
+   return false;
+
+   if (start)
+   return (blkaddr % sbi->blocks_per_blkz) == 0;
+   return (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
 
-   if (f2fs_is_multi_device(sbi)) {
-   devi = f2fs_target_device_index(sbi, blkaddr);
-   if (blkaddr < FDEV(devi).start_blk ||
-   blkaddr > FDEV(devi).end_blk) {
-   f2fs_err(sbi, "Invalid block %x", blkaddr);
-   return false;
-   }
-   blkaddr -= FDEV(devi).start_blk;
-   }
-   return bdev_is_zoned(FDEV(devi).bdev) &&
-   f2fs_blkz_is_seq(sbi, devi, blkaddr) &&
-   (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
 }
 #endif
 
@@ -944,20 +955,14 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
struct page *bio_page;
enum count_type type;
+#ifdef CONFIG_BLK_DEV_ZONED
+   bool blkzoned = f2fs_sb_has_blkzoned(sbi) && btype < META;
+#endif
 
f2fs_bug_on(sbi, is_read_io(fio->op));
 
f2fs_down_write(>io_rwsem);
 next:
-#ifdef CONFIG_BLK_DEV_ZONED
-   if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
-   wait_for_completion_io(>zone_wait);
-   bio_put(io->zone_pending_bio);
-   io->zone_pending_bio = NULL;
-   io->bi_private = NULL;
-   }
-#endif
-
if (fio->in_list) {
spin_lock(>io_lock);
if (list_empty(>io_list)) {
@@ -985,6 +990,11 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
type = WB_DATA_TYPE(bio_page, fio->compressed_page);
inc_page_count(sbi, type);
 

[f2fs-dev] [PATCH] f2fs: zone: don't block IO if there is remained open zone

2024-03-28 Thread Chao Yu
max open zone may be larger than log header number of f2fs, for
such case, it doesn't need to wait last IO in previous zone, let's
introduce available_open_zone semaphore, and reduce it once we
submit first write IO in a zone, and increase it after completion
of last IO in the zone.

Cc: Daeho Jeong 
Signed-off-by: Chao Yu 
---
 fs/f2fs/data.c| 80 +--
 fs/f2fs/f2fs.h| 34 +---
 fs/f2fs/iostat.c  |  7 +
 fs/f2fs/iostat.h  |  2 ++
 fs/f2fs/segment.c | 58 ++
 fs/f2fs/segment.h | 12 ++-
 fs/f2fs/super.c   |  2 ++
 7 files changed, 146 insertions(+), 49 deletions(-)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 0d88649c60a5..132a3ede60b1 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -373,11 +373,10 @@ static void f2fs_write_end_io(struct bio *bio)
 #ifdef CONFIG_BLK_DEV_ZONED
 static void f2fs_zone_write_end_io(struct bio *bio)
 {
-   struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private;
+   struct f2fs_sb_info *sbi = iostat_get_bio_private(bio);
 
-   bio->bi_private = io->bi_private;
-   complete(>zone_wait);
f2fs_write_end_io(bio);
+   up(>available_open_zones);
 }
 #endif
 
@@ -531,6 +530,24 @@ static void __submit_merged_bio(struct f2fs_bio_info *io)
if (!io->bio)
return;
 
+#ifdef CONFIG_BLK_DEV_ZONED
+   if (io->open_zone) {
+   /*
+* if there is no open zone, it will wait for last IO in
+* previous zone before submitting new IO.
+*/
+   down(>sbi->available_open_zones);
+   io->open_zone = false;
+   io->zone_openned = true;
+   }
+
+   if (io->close_zone) {
+   io->bio->bi_end_io = f2fs_zone_write_end_io;
+   io->zone_openned = false;
+   io->close_zone = false;
+   }
+#endif
+
if (is_read_io(fio->op)) {
trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
f2fs_submit_read_bio(io->sbi, io->bio, fio->type);
@@ -601,9 +618,9 @@ int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
INIT_LIST_HEAD(>write_io[i][j].bio_list);
init_f2fs_rwsem(>write_io[i][j].bio_list_lock);
 #ifdef CONFIG_BLK_DEV_ZONED
-   init_completion(>write_io[i][j].zone_wait);
-   sbi->write_io[i][j].zone_pending_bio = NULL;
-   sbi->write_io[i][j].bi_private = NULL;
+   sbi->write_io[i][j].open_zone = false;
+   sbi->write_io[i][j].zone_openned = false;
+   sbi->write_io[i][j].close_zone = false;
 #endif
}
}
@@ -918,22 +935,16 @@ int f2fs_merge_page_bio(struct f2fs_io_info *fio)
 }
 
 #ifdef CONFIG_BLK_DEV_ZONED
-static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr)
+static bool is_blkaddr_zone_boundary(struct f2fs_sb_info *sbi,
+   block_t blkaddr, bool start)
 {
-   int devi = 0;
+   if (!f2fs_blkaddr_in_seqzone(sbi, blkaddr))
+   return false;
+
+   if (start)
+   return (blkaddr % sbi->blocks_per_blkz) == 0;
+   return (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
 
-   if (f2fs_is_multi_device(sbi)) {
-   devi = f2fs_target_device_index(sbi, blkaddr);
-   if (blkaddr < FDEV(devi).start_blk ||
-   blkaddr > FDEV(devi).end_blk) {
-   f2fs_err(sbi, "Invalid block %x", blkaddr);
-   return false;
-   }
-   blkaddr -= FDEV(devi).start_blk;
-   }
-   return bdev_is_zoned(FDEV(devi).bdev) &&
-   f2fs_blkz_is_seq(sbi, devi, blkaddr) &&
-   (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1);
 }
 #endif
 
@@ -944,20 +955,14 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
struct page *bio_page;
enum count_type type;
+#ifdef CONFIG_BLK_DEV_ZONED
+   bool blkzoned = f2fs_sb_has_blkzoned(sbi) && btype < META;
+#endif
 
f2fs_bug_on(sbi, is_read_io(fio->op));
 
f2fs_down_write(>io_rwsem);
 next:
-#ifdef CONFIG_BLK_DEV_ZONED
-   if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) {
-   wait_for_completion_io(>zone_wait);
-   bio_put(io->zone_pending_bio);
-   io->zone_pending_bio = NULL;
-   io->bi_private = NULL;
-   }
-#endif
-
if (fio->in_list) {
spin_lock(>io_lock);
if (list_empty(>io_list)) {
@@ -985,6 +990,11 @@ void f2fs_submit_page_write(struct f2fs_io_info *fio)
type = WB_DATA_TYPE(bio_page, fio->compressed_page);
inc_page_count(sbi, type);
 
+#ifdef CONFIG_BLK_DEV_ZONED
+

[f2fs-dev] [PATCH v6] f2fs: fix zoned block device information initialization

2024-03-28 Thread Chao Yu
From: Wenjie Qi 

If the max open zones of zoned devices are less than
the active logs of F2FS, the device may error due to
insufficient zone resources when multiple active logs
are being written at the same time.

Signed-off-by: Wenjie Qi 
Signed-off-by: Chao Yu 
---
v6:
- add check condition to avoid remount failure.
 fs/f2fs/f2fs.h  |  1 +
 fs/f2fs/super.c | 27 +++
 2 files changed, 28 insertions(+)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 0550929dc6e5..694f8a52cb84 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -1559,6 +1559,7 @@ struct f2fs_sb_info {
 
 #ifdef CONFIG_BLK_DEV_ZONED
unsigned int blocks_per_blkz;   /* F2FS blocks per zone */
+   unsigned int max_open_zones;/* max open zone resources of 
the zoned device */
 #endif
 
/* for node-related operations */
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 7c45929671ad..642540782471 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -2326,6 +2326,17 @@ static int f2fs_remount(struct super_block *sb, int 
*flags, char *data)
if (err)
goto restore_opts;
 
+#ifdef CONFIG_BLK_DEV_ZONED
+   if (f2fs_is_multi_device(sbi) &&
+   sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
+   f2fs_err(sbi,
+   "zoned: max open zones %u is too small, need at least 
%u open zones",
+sbi->max_open_zones, 
F2FS_OPTION(sbi).active_logs);
+   err = -EINVAL;
+   goto restore_opts;
+   }
+#endif
+
/* flush outstanding errors before changing fs state */
flush_work(>s_error_work);
 
@@ -3868,11 +3879,24 @@ static int init_blkz_info(struct f2fs_sb_info *sbi, int 
devi)
sector_t nr_sectors = bdev_nr_sectors(bdev);
struct f2fs_report_zones_args rep_zone_arg;
u64 zone_sectors;
+   unsigned int max_open_zones;
int ret;
 
if (!f2fs_sb_has_blkzoned(sbi))
return 0;
 
+   if (bdev_is_zoned(FDEV(devi).bdev)) {
+   max_open_zones = bdev_max_open_zones(bdev);
+   if (max_open_zones && (max_open_zones < sbi->max_open_zones))
+   sbi->max_open_zones = max_open_zones;
+   if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
+   f2fs_err(sbi,
+   "zoned: max open zones %u is too small, need at 
least %u open zones",
+   sbi->max_open_zones, 
F2FS_OPTION(sbi).active_logs);
+   return -EINVAL;
+   }
+   }
+
zone_sectors = bdev_zone_sectors(bdev);
if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
SECTOR_TO_BLOCK(zone_sectors))
@@ -4186,6 +4210,9 @@ static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
 
logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
sbi->aligned_blksize = true;
+#ifdef CONFIG_BLK_DEV_ZONED
+   sbi->max_open_zones = UINT_MAX;
+#endif
 
for (i = 0; i < max_devices; i++) {
if (i == 0)
-- 
2.40.1



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