From: Livio Baldini Soares Subject: [PATCH] Backport inode_hash race fix Date: Thu, 14 Aug 2003 12:12:11 -0300 Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: <20030814151211.GA15650@ime.usp.br> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Neil Brown , Andreas Dilger Return-path: Received: from birosca.ime.usp.br ([143.107.45.59]:56745 "HELO birosca.ime.usp.br") by vger.kernel.org with SMTP id S272427AbTHNPUn (ORCPT ); Thu, 14 Aug 2003 11:20:43 -0400 To: linux-fsdevel@vger.kernel.org Content-Disposition: inline List-Id: linux-fsdevel.vger.kernel.org Hello, After trying to "get around" the inode_hash races when removing and iget()ing the same inode, my code got really ugly, and I got fed up. So yesterday I got Neil's 2.5 patch and backportted it to 2.4.22-rc2. The patch is very similar to Neil's, except for one (very important to me) case. Neil's patch only covered the removal of inodes in generic_delete_inode() (which in 2.4 is the case where i_nlink is zero in iput()). But, as I described in a previous post: http://marc.theaimsgroup.com/?l=linux-fsdevel&m=105547595519745&w=2 , I frequently get busted in prune_icache(). In Neil's patch prune_icache is not covered. In my opinion, this case (in prune_icache()), has to be fixed in 2.6 too. Depending on your comments, I may make a patch for 2.6 later. Please comment if you can, so that I may send this to Marcelo then 2.4.23-pre opens (which should be soon, I think). best regards, -- Livio B. Soares --- inode-orig.c Thu Aug 14 11:30:47 2003 +++ inode.c Thu Aug 14 11:57:51 2003 @@ -206,7 +206,8 @@ void __mark_inode_dirty(struct inode *in if ((inode->i_state & flags) != flags) { inode->i_state |= flags; /* Only add valid (ie hashed) inodes to the dirty list */ - if (!(inode->i_state & I_LOCK) && !list_empty(&inode->i_hash)) { + if (!(inode->i_state & (I_LOCK|I_FREEING|I_CLEAR)) && + !list_empty(&inode->i_hash)) { list_del(&inode->i_list); list_add(&inode->i_list, &sb->s_dirty); } @@ -235,6 +236,30 @@ static inline void wait_on_inode(struct __wait_on_inode(inode); } +/* + * If we try to find an inode in the inode hash while it is being deleted, we + * have to wait until the filesystem completes its deletion before reporting + * that it isn't found. This is because iget will immediately call + * ->read_inode, and we want to be sure that evidence of the deletion is found + * by ->read_inode. + * + * This call might return early if an inode which shares the waitq is woken up. + * This is most easily handled by the caller which will loop around again + * looking for the inode. + * + * This is called with inode_lock held. + */ +static void __wait_on_freeing_inode(struct inode *inode) +{ + DECLARE_WAITQUEUE(wait, current); + + add_wait_queue(&inode->i_wait, &wait); + set_current_state(TASK_UNINTERRUPTIBLE); + spin_unlock(&inode_lock); + schedule(); + remove_wait_queue(&inode->i_wait, &wait); + spin_lock(&inode_lock); +} static inline void write_inode(struct inode *inode, int sync) { @@ -596,6 +621,11 @@ static void dispose_list(struct list_hea if (inode->i_data.nrpages) truncate_inode_pages(&inode->i_data, 0); clear_inode(inode); + spin_lock(&inode_lock); + list_del(&inode->i_hash); + INIT_LIST_HEAD(&inode->i_hash); + spin_unlock(&inode_lock); + wake_up(&inode->i_wait); destroy_inode(inode); inodes_stat.nr_inodes--; } @@ -704,6 +734,14 @@ int invalidate_device(kdev_t dev, int do * * We don't expect to have to call this very often. * + * We leave the inode in the inode hash table until *after* + * the filesystem's ->delete_inode (in dispose_list) completes. + * This ensures that an iget (such as nfsd might instigate) will + * always find up-to-date information either in the hash or on disk. + * + * I_FREEING is set so that no-one will take a new reference + * to the inode while it is being deleted. + * * N.B. The spinlock is released during the call to * dispose_list. */ @@ -736,8 +774,6 @@ void prune_icache(int goal) if (atomic_read(&inode->i_count)) continue; list_del(tmp); - list_del(&inode->i_hash); - INIT_LIST_HEAD(&inode->i_hash); list_add(tmp, freeable); inode->i_state |= I_FREEING; count++; @@ -790,6 +826,7 @@ static struct inode * find_inode(struct struct list_head *tmp; struct inode * inode; +repeat: tmp = head; for (;;) { tmp = tmp->next; @@ -803,6 +840,10 @@ static struct inode * find_inode(struct continue; if (find_actor && !find_actor(inode, ino, opaque)) continue; + if (inode->i_state & (I_FREEING|I_CLEAR)) { + __wait_on_freeing_inode(inode); + goto repeat; + } break; } return inode; @@ -1042,8 +1083,6 @@ void iput(struct inode *inode) return; if (!inode->i_nlink) { - list_del(&inode->i_hash); - INIT_LIST_HEAD(&inode->i_hash); list_del(&inode->i_list); INIT_LIST_HEAD(&inode->i_list); inode->i_state|=I_FREEING; @@ -1061,6 +1100,11 @@ void iput(struct inode *inode) delete(inode); } else clear_inode(inode); + spin_lock(&inode_lock); + list_del(&inode->i_hash); + INIT_LIST_HEAD(&inode->i_hash); + spin_unlock(&inode_lock); + wake_up(&inode->i_wait); if (inode->i_state != I_CLEAR) BUG(); } else {