Use super_iter_inodes() with INO_ITER_F_NOREF | INO_ITER_F_NOIGET, which hands out only zero-count inodes and takes no reference on them. Signed-off-by: Huang Shijie --- fs/inode.c | 61 +++++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/fs/inode.c b/fs/inode.c index f63535ee3629..be21d1f10007 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -865,6 +865,30 @@ static void dispose_list(struct list_head *head) } } +static int evict_inode_fn(struct inode *inode, void *data) +{ + struct list_head *list = data; + + /* + * Only drain what previous rounds collected: @inode is the inode the + * walk is using as its cursor, and evict() would unlink it from + * s_inodes underneath the walk. + */ + if (need_resched()) { + cond_resched(); + dispose_list(list); + } + list_add(&inode->i_lru, list); + return INO_ITER_RET_NEXT; +} + +static bool evict_inode_eligible(struct inode *inode, void *data) +{ + inode_state_set(inode, I_FREEING); + inode_lru_list_del(inode); + return true; +} + /** * evict_inodes - evict all evictable inodes for a superblock * @sb: superblock to operate on @@ -876,43 +900,10 @@ static void dispose_list(struct list_head *head) */ void evict_inodes(struct super_block *sb) { - struct inode *inode; LIST_HEAD(dispose); - spin_lock(&sb->s_inode_list_lock); - list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { - if (icount_read_once(inode)) - continue; - - spin_lock(&inode->i_lock); - if (icount_read(inode)) { - spin_unlock(&inode->i_lock); - continue; - } - if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE)) { - spin_unlock(&inode->i_lock); - continue; - } - - inode_state_set(inode, I_FREEING); - inode_lru_list_del(inode); - spin_unlock(&inode->i_lock); - - /* - * Keep this inode out of dispose so it stays on s_inodes while - * the list lock is dropped. I_FREEING prevents new references - * and leaves eviction to us, so we can resume the walk from it. - */ - if (need_resched()) { - spin_unlock(&sb->s_inode_list_lock); - cond_resched(); - dispose_list(&dispose); - spin_lock(&sb->s_inode_list_lock); - } - list_add(&inode->i_lru, &dispose); - } - spin_unlock(&sb->s_inode_list_lock); - + super_iter_inodes(sb, evict_inode_fn, evict_inode_eligible, &dispose, + INO_ITER_F_NOREF | INO_ITER_F_NOIGET); dispose_list(&dispose); } EXPORT_SYMBOL_GPL(evict_inodes); -- 2.53.0