[PATCH] move truncate_inode_pages() into ->delete_inode()
[sfrench/cifs-2.6.git] / fs / inode.c
index 6d695037a0a3df62f6f977091881ae7a596705c9..f80a79ff156b5c4feafe54d17fee693fdec6b71c 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/pagemap.h>
 #include <linux/cdev.h>
 #include <linux/bootmem.h>
+#include <linux/inotify.h>
 
 /*
  * This is needed for the following functions:
@@ -202,6 +203,10 @@ void inode_init_once(struct inode *inode)
        INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear);
        spin_lock_init(&inode->i_lock);
        i_size_ordered_init(inode);
+#ifdef CONFIG_INOTIFY
+       INIT_LIST_HEAD(&inode->inotify_watches);
+       sema_init(&inode->inotify_sem, 1);
+#endif
 }
 
 EXPORT_SYMBOL(inode_init_once);
@@ -282,6 +287,13 @@ static void dispose_list(struct list_head *head)
                if (inode->i_data.nrpages)
                        truncate_inode_pages(&inode->i_data, 0);
                clear_inode(inode);
+
+               spin_lock(&inode_lock);
+               hlist_del_init(&inode->i_hash);
+               list_del_init(&inode->i_sb_list);
+               spin_unlock(&inode_lock);
+
+               wake_up_inode(inode);
                destroy_inode(inode);
                nr_disposed++;
        }
@@ -317,8 +329,6 @@ static int invalidate_list(struct list_head *head, struct list_head *dispose)
                inode = list_entry(tmp, struct inode, i_sb_list);
                invalidate_inode_buffers(inode);
                if (!atomic_read(&inode->i_count)) {
-                       hlist_del_init(&inode->i_hash);
-                       list_del(&inode->i_sb_list);
                        list_move(&inode->i_list, dispose);
                        inode->i_state |= I_FREEING;
                        count++;
@@ -346,6 +356,7 @@ int invalidate_inodes(struct super_block * sb)
 
        down(&iprune_sem);
        spin_lock(&inode_lock);
+       inotify_unmount_inodes(&sb->s_inodes);
        busy = invalidate_list(&sb->s_inodes, &throw_away);
        spin_unlock(&inode_lock);
 
@@ -439,8 +450,6 @@ static void prune_icache(int nr_to_scan)
                        if (!can_unuse(inode))
                                continue;
                }
-               hlist_del_init(&inode->i_hash);
-               list_del_init(&inode->i_sb_list);
                list_move(&inode->i_list, &freeable);
                inode->i_state |= I_FREEING;
                nr_pruned++;
@@ -748,6 +757,7 @@ EXPORT_SYMBOL(igrab);
  * @head:       the head of the list to search
  * @test:      callback used for comparisons between inodes
  * @data:      opaque data pointer to pass to @test
+ * @wait:      if true wait for the inode to be unlocked, if false do not
  *
  * ifind() searches for the inode specified by @data in the inode
  * cache. This is a generalized version of ifind_fast() for file systems where
@@ -762,7 +772,7 @@ EXPORT_SYMBOL(igrab);
  */
 static inline struct inode *ifind(struct super_block *sb,
                struct hlist_head *head, int (*test)(struct inode *, void *),
-               void *data)
+               void *data, const int wait)
 {
        struct inode *inode;
 
@@ -771,7 +781,8 @@ static inline struct inode *ifind(struct super_block *sb,
        if (inode) {
                __iget(inode);
                spin_unlock(&inode_lock);
-               wait_on_inode(inode);
+               if (likely(wait))
+                       wait_on_inode(inode);
                return inode;
        }
        spin_unlock(&inode_lock);
@@ -811,7 +822,7 @@ static inline struct inode *ifind_fast(struct super_block *sb,
 }
 
 /**
- * ilookup5 - search for an inode in the inode cache
+ * ilookup5_nowait - search for an inode in the inode cache
  * @sb:                super block of file system to search
  * @hashval:   hash value (usually inode number) to search for
  * @test:      callback used for comparisons between inodes
@@ -823,7 +834,38 @@ static inline struct inode *ifind_fast(struct super_block *sb,
  * identification of an inode.
  *
  * If the inode is in the cache, the inode is returned with an incremented
- * reference count.
+ * reference count.  Note, the inode lock is not waited upon so you have to be
+ * very careful what you do with the returned inode.  You probably should be
+ * using ilookup5() instead.
+ *
+ * Otherwise NULL is returned.
+ *
+ * Note, @test is called with the inode_lock held, so can't sleep.
+ */
+struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
+               int (*test)(struct inode *, void *), void *data)
+{
+       struct hlist_head *head = inode_hashtable + hash(sb, hashval);
+
+       return ifind(sb, head, test, data, 0);
+}
+
+EXPORT_SYMBOL(ilookup5_nowait);
+
+/**
+ * ilookup5 - search for an inode in the inode cache
+ * @sb:                super block of file system to search
+ * @hashval:   hash value (usually inode number) to search for
+ * @test:      callback used for comparisons between inodes
+ * @data:      opaque data pointer to pass to @test
+ *
+ * ilookup5() uses ifind() to search for the inode specified by @hashval and
+ * @data in the inode cache. This is a generalized version of ilookup() for
+ * file systems where the inode number is not sufficient for unique
+ * identification of an inode.
+ *
+ * If the inode is in the cache, the inode lock is waited upon and the inode is
+ * returned with an incremented reference count.
  *
  * Otherwise NULL is returned.
  *
@@ -834,7 +876,7 @@ struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
 {
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
 
-       return ifind(sb, head, test, data);
+       return ifind(sb, head, test, data, 1);
 }
 
 EXPORT_SYMBOL(ilookup5);
@@ -891,7 +933,7 @@ struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
        struct hlist_head *head = inode_hashtable + hash(sb, hashval);
        struct inode *inode;
 
-       inode = ifind(sb, head, test, data);
+       inode = ifind(sb, head, test, data, 1);
        if (inode)
                return inode;
        /*
@@ -992,19 +1034,21 @@ void generic_delete_inode(struct inode *inode)
        inodes_stat.nr_inodes--;
        spin_unlock(&inode_lock);
 
-       if (inode->i_data.nrpages)
-               truncate_inode_pages(&inode->i_data, 0);
-
        security_inode_delete(inode);
 
        if (op->delete_inode) {
                void (*delete)(struct inode *) = op->delete_inode;
                if (!is_bad_inode(inode))
                        DQUOT_INIT(inode);
-               /* s_op->delete_inode internally recalls clear_inode() */
+               /* Filesystems implementing their own
+                * s_op->delete_inode are required to call
+                * truncate_inode_pages and clear_inode()
+                * internally */
                delete(inode);
-       } else
+       } else {
+               truncate_inode_pages(&inode->i_data, 0);
                clear_inode(inode);
+       }
        spin_lock(&inode_lock);
        hlist_del_init(&inode->i_hash);
        spin_unlock(&inode_lock);
@@ -1153,9 +1197,6 @@ void update_atime(struct inode *inode)
        if (!timespec_equal(&inode->i_atime, &now)) {
                inode->i_atime = now;
                mark_inode_dirty_sync(inode);
-       } else {
-               if (!timespec_equal(&inode->i_atime, &now))
-                       inode->i_atime = now;
        }
 }
 
@@ -1244,29 +1285,21 @@ int inode_wait(void *word)
 }
 
 /*
- * 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.
+ * 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 function waits
+ * until the deletion _might_ have completed.  Callers are responsible
+ * to recheck inode state.
+ *
+ * It doesn't matter if I_LOCK is not set initially, a call to
+ * wake_up_inode() after removing from the hash list will DTRT.
+ *
  * This is called with inode_lock held.
  */
 static void __wait_on_freeing_inode(struct inode *inode)
 {
        wait_queue_head_t *wq;
        DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK);
-
-       /*
-        * I_FREEING and I_CLEAR are cleared in process context under
-        * inode_lock, so we have to give the tasks who would clear them
-        * a chance to run and acquire inode_lock.
-        */
-       if (!(inode->i_state & I_LOCK)) {
-               spin_unlock(&inode_lock);
-               yield();
-               spin_lock(&inode_lock);
-               return;
-       }
        wq = bit_waitqueue(&inode->i_state, __I_LOCK);
        prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
        spin_unlock(&inode_lock);