+ found = d_splice_alias(inode, new);
+ if (found) {
+ dput(new);
+ return found;
+ }
+ return new;
+ }
+
+ /*
+ * If a matching dentry exists, and it's not negative use it.
+ *
+ * Decrement the reference count to balance the iget() done
+ * earlier on.
+ */
+ if (found->d_inode) {
+ if (unlikely(found->d_inode != inode)) {
+ /* This can't happen because bad inodes are unhashed. */
+ BUG_ON(!is_bad_inode(inode));
+ BUG_ON(!is_bad_inode(found->d_inode));
+ }
+ iput(inode);
+ return found;
+ }
+
+ /*
+ * Negative dentry: instantiate it unless the inode is a directory and
+ * already has a dentry.
+ */
+ spin_lock(&dcache_lock);
+ if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
+ __d_instantiate(found, inode);
+ spin_unlock(&dcache_lock);
+ security_d_instantiate(found, inode);
+ return found;
+ }
+
+ /*
+ * In case a directory already has a (disconnected) entry grab a
+ * reference to it, move it in place and use it.
+ */
+ new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
+ dget_locked(new);
+ spin_unlock(&dcache_lock);
+ security_d_instantiate(found, inode);
+ d_move(new, found);
+ iput(inode);
+ dput(found);
+ return new;
+
+err_out:
+ iput(inode);
+ return ERR_PTR(error);
+}
+EXPORT_SYMBOL(d_add_ci);