__wait_on_freeing_inode(inode);
                        goto repeat;
                }
+               if (unlikely(inode->i_state & I_CREATING)) {
+                       spin_unlock(&inode->i_lock);
+                       return ERR_PTR(-ESTALE);
+               }
                __iget(inode);
                spin_unlock(&inode->i_lock);
                return inode;
                        __wait_on_freeing_inode(inode);
                        goto repeat;
                }
+               if (unlikely(inode->i_state & I_CREATING)) {
+                       spin_unlock(&inode->i_lock);
+                       return ERR_PTR(-ESTALE);
+               }
                __iget(inode);
                spin_unlock(&inode->i_lock);
                return inode;
        lockdep_annotate_inode_mutex_key(inode);
        spin_lock(&inode->i_lock);
        WARN_ON(!(inode->i_state & I_NEW));
-       inode->i_state &= ~I_NEW;
+       inode->i_state &= ~I_NEW & ~I_CREATING;
        smp_mb();
        wake_up_bit(&inode->i_state, __I_NEW);
        spin_unlock(&inode->i_lock);
 }
 EXPORT_SYMBOL(unlock_new_inode);
 
+void discard_new_inode(struct inode *inode)
+{
+       lockdep_annotate_inode_mutex_key(inode);
+       spin_lock(&inode->i_lock);
+       WARN_ON(!(inode->i_state & I_NEW));
+       inode->i_state &= ~I_NEW;
+       smp_mb();
+       wake_up_bit(&inode->i_state, __I_NEW);
+       spin_unlock(&inode->i_lock);
+       iput(inode);
+}
+EXPORT_SYMBOL(discard_new_inode);
+
 /**
  * lock_two_nondirectories - take two i_mutexes on non-directory objects
  *
                 * Use the old inode instead of the preallocated one.
                 */
                spin_unlock(&inode_hash_lock);
+               if (IS_ERR(old))
+                       return NULL;
                wait_on_inode(old);
                if (unlikely(inode_unhashed(old))) {
                        iput(old);
        inode = find_inode_fast(sb, head, ino);
        spin_unlock(&inode_hash_lock);
        if (inode) {
+               if (IS_ERR(inode))
+                       return NULL;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
                        iput(inode);
                 */
                spin_unlock(&inode_hash_lock);
                destroy_inode(inode);
+               if (IS_ERR(old))
+                       return NULL;
                inode = old;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
        inode = find_inode(sb, head, test, data);
        spin_unlock(&inode_hash_lock);
 
-       return inode;
+       return IS_ERR(inode) ? NULL : inode;
 }
 EXPORT_SYMBOL(ilookup5_nowait);
 
        spin_unlock(&inode_hash_lock);
 
        if (inode) {
+               if (IS_ERR(inode))
+                       return NULL;
                wait_on_inode(inode);
                if (unlikely(inode_unhashed(inode))) {
                        iput(inode);
                }
                if (likely(!old)) {
                        spin_lock(&inode->i_lock);
-                       inode->i_state |= I_NEW;
+                       inode->i_state |= I_NEW | I_CREATING;
                        hlist_add_head(&inode->i_hash, head);
                        spin_unlock(&inode->i_lock);
                        spin_unlock(&inode_hash_lock);
                        return 0;
                }
+               if (unlikely(old->i_state & I_CREATING)) {
+                       spin_unlock(&old->i_lock);
+                       spin_unlock(&inode_hash_lock);
+                       return -EBUSY;
+               }
                __iget(old);
                spin_unlock(&old->i_lock);
                spin_unlock(&inode_hash_lock);
 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
                int (*test)(struct inode *, void *), void *data)
 {
-       struct inode *old = inode_insert5(inode, hashval, test, NULL, data);
+       struct inode *old;
+
+       inode->i_state |= I_CREATING;
+       old = inode_insert5(inode, hashval, test, NULL, data);
 
        if (old != inode) {
                iput(old);
 
  * I_OVL_INUSE         Used by overlayfs to get exclusive ownership on upper
  *                     and work dirs among overlayfs mounts.
  *
+ * I_CREATING          New object's inode in the middle of setting up.
+ *
  * Q: What is the difference between I_WILL_FREE and I_FREEING?
  */
 #define I_DIRTY_SYNC           (1 << 0)
 #define __I_DIRTY_TIME_EXPIRED 12
 #define I_DIRTY_TIME_EXPIRED   (1 << __I_DIRTY_TIME_EXPIRED)
 #define I_WB_SWITCH            (1 << 13)
-#define I_OVL_INUSE                    (1 << 14)
+#define I_OVL_INUSE            (1 << 14)
+#define I_CREATING             (1 << 15)
 
 #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
 #define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)
 static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };
 #endif
 extern void unlock_new_inode(struct inode *);
+extern void discard_new_inode(struct inode *);
 extern unsigned int get_next_ino(void);
 extern void evict_inodes(struct super_block *sb);