[PATCH v4 07/11] block: convert sync_bdevs() to sb_inode_iter

From: Huang Shijie

Date: Wed Nov 25 2026 - 04:13:03 EST


Convert sync_bdevs() to sb_inode_iter.
No functional change.

Signed-off-by: Huang Shijie <huangsj@xxxxxxxx>
---
block/bdev.c | 43 +++++++++++--------------------------------
1 file changed, 11 insertions(+), 32 deletions(-)

diff --git a/block/bdev.c b/block/bdev.c
index 938396c6559d..5b399fb7cd63 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -1339,44 +1339,27 @@ void bdev_mark_dead(struct block_device *bdev, bool surprise)
*/
EXPORT_SYMBOL_GPL(bdev_mark_dead);

+static bool sync_bdev_eligible(struct inode *inode, void *priv)
+{
+ return inode->i_mapping->nrpages != 0;
+}
+
void sync_bdevs(bool wait)
{
- struct inode *inode, *old_inode = NULL;
+ struct inode *inode;

- spin_lock(&blockdev_superblock->s_inode_list_lock);
- list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
- struct address_space *mapping = inode->i_mapping;
- struct block_device *bdev;
+ CLASS(sb_inode_iter, iter)(blockdev_superblock, sync_bdev_eligible,
+ NULL, 0);

- spin_lock(&inode->i_lock);
- if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW) ||
- mapping->nrpages == 0) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- /*
- * We hold a reference to 'inode' so it couldn't have been
- * removed from s_inodes list while we dropped the
- * s_inode_list_lock We cannot iput the inode now as we can
- * be holding the last reference and we cannot iput it under
- * s_inode_list_lock. So we keep the reference and iput it
- * later.
- */
- iput(old_inode);
- old_inode = inode;
- bdev = I_BDEV(inode);
+ while ((inode = sb_inode_iter_next(&iter))) {
+ struct block_device *bdev = I_BDEV(inode);

/*
* The inode does not keep the gendisk of a partition alive, the
* device does.
*/
- if (!kobject_get_unless_zero(&bdev->bd_device.kobj)) {
- spin_lock(&blockdev_superblock->s_inode_list_lock);
+ if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
continue;
- }

mutex_lock(&bdev->bd_disk->open_mutex);
if (!atomic_read(&bdev->bd_openers)) {
@@ -1394,11 +1377,7 @@ void sync_bdevs(bool wait)
}
mutex_unlock(&bdev->bd_disk->open_mutex);
put_device(&bdev->bd_device);
-
- spin_lock(&blockdev_superblock->s_inode_list_lock);
}
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- iput(old_inode);
}

/*
--
2.53.0