[PATCH 07/13] writeback: modify sync related functions to iterate over all writeback contexts

Kundan Kumar
2025-05-29T11:34:05Z

Modify sync related functions to iterate over all writeback contexts.

Signed-off-by: Kundan Kumar <kundan.kumar@samsung.com>
Signed-off-by: Anuj Gupta <anuj20.g@samsung.com>
---
fs/fs-writeback.c | 66 +++++++++++++++++++++++++++++++----------------
1 file changed, 44 insertions(+), 22 deletions(-)

diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index 9b0940a6fe78..7558b8a33fe0 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -2752,11 +2752,13 @@ static void wait_sb_inodes(struct super_block *sb)
mutex_unlock(&sb->s_sync_lock);
}

-static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
- enum wb_reason reason, bool skip_if_busy)
+static void __writeback_inodes_sb_nr_ctx(struct super_block *sb,
+ unsigned long nr,
+ enum wb_reason reason,
+ bool skip_if_busy,
+ struct bdi_writeback_ctx *bdi_wb_ctx)
{
- struct backing_dev_info *bdi = sb->s_bdi;
- DEFINE_WB_COMPLETION(done, bdi->wb_ctx_arr[0]);
+ DEFINE_WB_COMPLETION(done, bdi_wb_ctx);
struct wb_writeback_work work = {
.sb = sb,
.sync_mode = WB_SYNC_NONE,
@@ -2766,13 +2768,23 @@ static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
.reason = reason,
};

+ bdi_split_work_to_wbs(sb->s_bdi, bdi_wb_ctx, &work, skip_if_busy);
+ wb_wait_for_completion(&done);
+}
+
+static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr,
+ enum wb_reason reason, bool skip_if_busy)
+{
+ struct backing_dev_info *bdi = sb->s_bdi;
+ struct bdi_writeback_ctx *bdi_wb_ctx;
+
if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info)
return;
WARN_ON(!rwsem_is_locked(&sb->s_umount));

- bdi_split_work_to_wbs(sb->s_bdi, bdi->wb_ctx_arr[0], &work,
- skip_if_busy);
- wb_wait_for_completion(&done);
+ for_each_bdi_wb_ctx(bdi, bdi_wb_ctx)
+ __writeback_inodes_sb_nr_ctx(sb, nr, reason, skip_if_busy,
+ bdi_wb_ctx);
}

/**
@@ -2825,17 +2837,11 @@ void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason)
}
EXPORT_SYMBOL(try_to_writeback_inodes_sb);

-/**
- * sync_inodes_sb - sync sb inode pages
- * @sb: the superblock
- *
- * This function writes and waits on any dirty inode belonging to this
- * super_block.
- */
-void sync_inodes_sb(struct super_block *sb)
+static void sync_inodes_bdi_wb_ctx(struct super_block *sb,
+ struct backing_dev_info *bdi,
+ struct bdi_writeback_ctx *bdi_wb_ctx)
{
- struct backing_dev_info *bdi = sb->s_bdi;
- DEFINE_WB_COMPLETION(done, bdi->wb_ctx_arr[0]);
+ DEFINE_WB_COMPLETION(done, bdi_wb_ctx);
struct wb_writeback_work work = {
.sb = sb,
.sync_mode = WB_SYNC_ALL,
@@ -2846,6 +2852,25 @@ void sync_inodes_sb(struct super_block *sb)
.for_sync = 1,
};

+ /* protect against inode wb switch, see inode_switch_wbs_work_fn() */
+ bdi_down_write_wb_ctx_switch_rwsem(bdi_wb_ctx);
+ bdi_split_work_to_wbs(bdi, bdi_wb_ctx, &work, false);
+ wb_wait_for_completion(&done);
+ bdi_up_write_wb_ctx_switch_rwsem(bdi_wb_ctx);
+}
+
+/**
+ * sync_inodes_sb - sync sb inode pages
+ * @sb: the superblock
+ *
+ * This function writes and waits on any dirty inode belonging to this
+ * super_block.
+ */
+void sync_inodes_sb(struct super_block *sb)
+{
+ struct backing_dev_info *bdi = sb->s_bdi;
+ struct bdi_writeback_ctx *bdi_wb_ctx;
+
/*
* Can't skip on !bdi_has_dirty() because we should wait for !dirty
* inodes under writeback and I_DIRTY_TIME inodes ignored by
@@ -2855,11 +2880,8 @@ void sync_inodes_sb(struct super_block *sb)
return;
WARN_ON(!rwsem_is_locked(&sb->s_umount));

- /* protect against inode wb switch, see inode_switch_wbs_work_fn() */
- bdi_down_write_wb_ctx_switch_rwsem(bdi->wb_ctx_arr[0]);
- bdi_split_work_to_wbs(bdi, bdi->wb_ctx_arr[0], &work, false);
- wb_wait_for_completion(&done);
- bdi_up_write_wb_ctx_switch_rwsem(bdi->wb_ctx_arr[0]);
+ for_each_bdi_wb_ctx(bdi, bdi_wb_ctx)
+ sync_inodes_bdi_wb_ctx(sb, bdi, bdi_wb_ctx);

wait_sb_inodes(sb);
}
--
2.25.1