Merge ../linus
[powerpc.git] / fs / ocfs2 / namei.c
index 0d3e939..9637039 100644 (file)
@@ -75,12 +75,12 @@ static int inline ocfs2_search_dirblock(struct buffer_head *bh,
                                        unsigned long offset,
                                        struct ocfs2_dir_entry **res_dir);
 
-static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
+static int ocfs2_delete_entry(handle_t *handle,
                              struct inode *dir,
                              struct ocfs2_dir_entry *de_del,
                              struct buffer_head *bh);
 
-static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
+static int __ocfs2_add_entry(handle_t *handle,
                             struct inode *dir,
                             const char *name, int namelen,
                             struct inode *inode, u64 blkno,
@@ -93,43 +93,37 @@ static int ocfs2_mknod_locked(struct ocfs2_super *osb,
                              dev_t dev,
                              struct buffer_head **new_fe_bh,
                              struct buffer_head *parent_fe_bh,
-                             struct ocfs2_journal_handle *handle,
+                             handle_t *handle,
                              struct inode **ret_inode,
                              struct ocfs2_alloc_context *inode_ac);
 
 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
-                             struct ocfs2_journal_handle *handle,
+                             handle_t *handle,
                              struct inode *parent,
                              struct inode *inode,
                              struct buffer_head *fe_bh,
                              struct ocfs2_alloc_context *data_ac);
 
-static int ocfs2_double_lock(struct ocfs2_super *osb,
-                            struct ocfs2_journal_handle *handle,
-                            struct buffer_head **bh1,
-                            struct inode *inode1,
-                            struct buffer_head **bh2,
-                            struct inode *inode2);
-
 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
-                                   struct ocfs2_journal_handle *handle,
+                                   struct inode **ret_orphan_dir,
                                    struct inode *inode,
                                    char *name,
                                    struct buffer_head **de_bh);
 
 static int ocfs2_orphan_add(struct ocfs2_super *osb,
-                           struct ocfs2_journal_handle *handle,
+                           handle_t *handle,
                            struct inode *inode,
                            struct ocfs2_dinode *fe,
                            char *name,
-                           struct buffer_head *de_bh);
+                           struct buffer_head *de_bh,
+                           struct inode *orphan_dir_inode);
 
 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
-                                    struct ocfs2_journal_handle *handle,
+                                    handle_t *handle,
                                     struct inode *inode,
                                     const char *symname);
 
-static inline int ocfs2_add_entry(struct ocfs2_journal_handle *handle,
+static inline int ocfs2_add_entry(handle_t *handle,
                                  struct dentry *dentry,
                                  struct inode *inode, u64 blkno,
                                  struct buffer_head *parent_fe_bh,
@@ -165,7 +159,7 @@ static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
        mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len,
             dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno);
 
-       status = ocfs2_meta_lock(dir, NULL, NULL, 0);
+       status = ocfs2_meta_lock(dir, NULL, 0);
        if (status < 0) {
                if (status != -ENOENT)
                        mlog_errno(status);
@@ -179,7 +173,7 @@ static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
        if (status < 0)
                goto bail_add;
 
-       inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno);
+       inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
        if (IS_ERR(inode)) {
                mlog(ML_ERROR, "Unable to create inode %llu\n",
                     (unsigned long long)blkno);
@@ -199,10 +193,32 @@ static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry,
        spin_unlock(&oi->ip_lock);
 
 bail_add:
-
        dentry->d_op = &ocfs2_dentry_ops;
        ret = d_splice_alias(inode, dentry);
 
+       if (inode) {
+               /*
+                * If d_splice_alias() finds a DCACHE_DISCONNECTED
+                * dentry, it will d_move() it on top of ourse. The
+                * return value will indicate this however, so in
+                * those cases, we switch them around for the locking
+                * code.
+                *
+                * NOTE: This dentry already has ->d_op set from
+                * ocfs2_get_parent() and ocfs2_get_dentry()
+                */
+               if (ret)
+                       dentry = ret;
+
+               status = ocfs2_dentry_attach_lock(dentry, inode,
+                                                 OCFS2_I(dir)->ip_blkno);
+               if (status) {
+                       mlog_errno(status);
+                       ret = ERR_PTR(status);
+                       goto bail_unlock;
+               }
+       }
+
 bail_unlock:
        /* Don't drop the cluster lock until *after* the d_add --
         * unlink on another node will message us to remove that
@@ -220,7 +236,7 @@ bail:
 }
 
 static int ocfs2_fill_new_dir(struct ocfs2_super *osb,
-                             struct ocfs2_journal_handle *handle,
+                             handle_t *handle,
                              struct inode *parent,
                              struct inode *inode,
                              struct buffer_head *fe_bh,
@@ -295,7 +311,7 @@ static int ocfs2_mknod(struct inode *dir,
 {
        int status = 0;
        struct buffer_head *parent_fe_bh = NULL;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle = NULL;
        struct ocfs2_super *osb;
        struct ocfs2_dinode *dirfe;
        struct buffer_head *new_fe_bh = NULL;
@@ -311,18 +327,11 @@ static int ocfs2_mknod(struct inode *dir,
        /* get our super block */
        osb = OCFS2_SB(dir->i_sb);
 
-       handle = ocfs2_alloc_handle(osb);
-       if (handle == NULL) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto leave;
-       }
-
-       status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
+       status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
        if (status < 0) {
                if (status != -ENOENT)
                        mlog_errno(status);
-               goto leave;
+               return status;
        }
 
        if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) {
@@ -352,7 +361,7 @@ static int ocfs2_mknod(struct inode *dir,
        }
 
        /* reserve an inode spot */
-       status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
+       status = ocfs2_reserve_new_inode(osb, &inode_ac);
        if (status < 0) {
                if (status != -ENOSPC)
                        mlog_errno(status);
@@ -362,7 +371,7 @@ static int ocfs2_mknod(struct inode *dir,
        /* are we making a directory? If so, reserve a cluster for his
         * 1st extent. */
        if (S_ISDIR(mode)) {
-               status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
+               status = ocfs2_reserve_clusters(osb, 1, &data_ac);
                if (status < 0) {
                        if (status != -ENOSPC)
                                mlog_errno(status);
@@ -370,7 +379,7 @@ static int ocfs2_mknod(struct inode *dir,
                }
        }
 
-       handle = ocfs2_start_trans(osb, handle, OCFS2_MKNOD_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
@@ -407,7 +416,7 @@ static int ocfs2_mknod(struct inode *dir,
                        mlog_errno(status);
                        goto leave;
                }
-               dir->i_nlink++;
+               inc_nlink(dir);
        }
 
        status = ocfs2_add_entry(handle, dentry, inode,
@@ -418,13 +427,22 @@ static int ocfs2_mknod(struct inode *dir,
                goto leave;
        }
 
+       status = ocfs2_dentry_attach_lock(dentry, inode,
+                                         OCFS2_I(dir)->ip_blkno);
+       if (status) {
+               mlog_errno(status);
+               goto leave;
+       }
+
        insert_inode_hash(inode);
        dentry->d_op = &ocfs2_dentry_ops;
        d_instantiate(dentry, inode);
        status = 0;
 leave:
        if (handle)
-               ocfs2_commit_trans(handle);
+               ocfs2_commit_trans(osb, handle);
+
+       ocfs2_meta_unlock(dir, 1);
 
        if (status == -ENOSPC)
                mlog(0, "Disk is full\n");
@@ -458,7 +476,7 @@ static int ocfs2_mknod_locked(struct ocfs2_super *osb,
                              dev_t dev,
                              struct buffer_head **new_fe_bh,
                              struct buffer_head *parent_fe_bh,
-                             struct ocfs2_journal_handle *handle,
+                             handle_t *handle,
                              struct inode **ret_inode,
                              struct ocfs2_alloc_context *inode_ac)
 {
@@ -569,9 +587,11 @@ static int ocfs2_mknod_locked(struct ocfs2_super *osb,
        }
 
        ocfs2_inode_set_new(osb, inode);
-       status = ocfs2_create_new_inode_locks(inode);
-       if (status < 0)
-               mlog_errno(status);
+       if (!ocfs2_mount_local(osb)) {
+               status = ocfs2_create_new_inode_locks(inode);
+               if (status < 0)
+                       mlog_errno(status);
+       }
 
        status = 0; /* error in ocfs2_create_new_inode_locks is not
                     * critical */
@@ -624,7 +644,7 @@ static int ocfs2_link(struct dentry *old_dentry,
                      struct inode *dir,
                      struct dentry *dentry)
 {
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle;
        struct inode *inode = old_dentry->d_inode;
        int err;
        struct buffer_head *fe_bh = NULL;
@@ -637,71 +657,63 @@ static int ocfs2_link(struct dentry *old_dentry,
                   old_dentry->d_name.len, old_dentry->d_name.name,
                   dentry->d_name.len, dentry->d_name.name);
 
-       if (S_ISDIR(inode->i_mode)) {
-               err = -EPERM;
-               goto bail;
-       }
-
-       handle = ocfs2_alloc_handle(osb);
-       if (handle == NULL) {
-               err = -ENOMEM;
-               goto bail;
-       }
+       if (S_ISDIR(inode->i_mode))
+               return -EPERM;
 
-       err = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
+       err = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
        if (err < 0) {
                if (err != -ENOENT)
                        mlog_errno(err);
-               goto bail;
+               return err;
        }
 
        if (!dir->i_nlink) {
                err = -ENOENT;
-               goto bail;
+               goto out;
        }
 
        err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name,
                                        dentry->d_name.len);
        if (err)
-               goto bail;
+               goto out;
 
        err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh,
                                           dentry->d_name.name,
                                           dentry->d_name.len, &de_bh);
        if (err < 0) {
                mlog_errno(err);
-               goto bail;
+               goto out;
        }
 
-       err = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
+       err = ocfs2_meta_lock(inode, &fe_bh, 1);
        if (err < 0) {
                if (err != -ENOENT)
                        mlog_errno(err);
-               goto bail;
+               goto out;
        }
 
        fe = (struct ocfs2_dinode *) fe_bh->b_data;
        if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) {
                err = -EMLINK;
-               goto bail;
+               goto out_unlock_inode;
        }
 
-       handle = ocfs2_start_trans(osb, handle, OCFS2_LINK_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS);
        if (IS_ERR(handle)) {
                err = PTR_ERR(handle);
                handle = NULL;
                mlog_errno(err);
-               goto bail;
+               goto out_unlock_inode;
        }
 
        err = ocfs2_journal_access(handle, inode, fe_bh,
                                   OCFS2_JOURNAL_ACCESS_WRITE);
        if (err < 0) {
                mlog_errno(err);
-               goto bail;
+               goto out_commit;
        }
 
-       inode->i_nlink++;
+       inc_nlink(inode);
        inode->i_ctime = CURRENT_TIME;
        fe->i_links_count = cpu_to_le16(inode->i_nlink);
        fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
@@ -710,9 +722,9 @@ static int ocfs2_link(struct dentry *old_dentry,
        err = ocfs2_journal_dirty(handle, fe_bh);
        if (err < 0) {
                le16_add_cpu(&fe->i_links_count, -1);
-               inode->i_nlink--;
+               drop_nlink(inode);
                mlog_errno(err);
-               goto bail;
+               goto out_commit;
        }
 
        err = ocfs2_add_entry(handle, dentry, inode,
@@ -720,17 +732,29 @@ static int ocfs2_link(struct dentry *old_dentry,
                              parent_fe_bh, de_bh);
        if (err) {
                le16_add_cpu(&fe->i_links_count, -1);
-               inode->i_nlink--;
+               drop_nlink(inode);
                mlog_errno(err);
-               goto bail;
+               goto out_commit;
+       }
+
+       err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
+       if (err) {
+               mlog_errno(err);
+               goto out_commit;
        }
 
        atomic_inc(&inode->i_count);
        dentry->d_op = &ocfs2_dentry_ops;
        d_instantiate(dentry, inode);
-bail:
-       if (handle)
-               ocfs2_commit_trans(handle);
+
+out_commit:
+       ocfs2_commit_trans(osb, handle);
+out_unlock_inode:
+       ocfs2_meta_unlock(inode, 1);
+
+out:
+       ocfs2_meta_unlock(dir, 1);
+
        if (de_bh)
                brelse(de_bh);
        if (fe_bh)
@@ -743,18 +767,49 @@ bail:
        return err;
 }
 
+/*
+ * Takes and drops an exclusive lock on the given dentry. This will
+ * force other nodes to drop it.
+ */
+static int ocfs2_remote_dentry_delete(struct dentry *dentry)
+{
+       int ret;
+
+       ret = ocfs2_dentry_lock(dentry, 1);
+       if (ret)
+               mlog_errno(ret);
+       else
+               ocfs2_dentry_unlock(dentry, 1);
+
+       return ret;
+}
+
+static inline int inode_is_unlinkable(struct inode *inode)
+{
+       if (S_ISDIR(inode->i_mode)) {
+               if (inode->i_nlink == 2)
+                       return 1;
+               return 0;
+       }
+
+       if (inode->i_nlink == 1)
+               return 1;
+       return 0;
+}
+
 static int ocfs2_unlink(struct inode *dir,
                        struct dentry *dentry)
 {
        int status;
-       unsigned int saved_nlink = 0;
+       int child_locked = 0;
        struct inode *inode = dentry->d_inode;
+       struct inode *orphan_dir = NULL;
        struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
        u64 blkno;
        struct ocfs2_dinode *fe = NULL;
        struct buffer_head *fe_bh = NULL;
        struct buffer_head *parent_node_bh = NULL;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle = NULL;
        struct ocfs2_dir_entry *dirent = NULL;
        struct buffer_head *dirent_bh = NULL;
        char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
@@ -769,22 +824,14 @@ static int ocfs2_unlink(struct inode *dir,
 
        if (inode == osb->root_inode) {
                mlog(0, "Cannot delete the root directory\n");
-               status = -EPERM;
-               goto leave;
+               return -EPERM;
        }
 
-       handle = ocfs2_alloc_handle(osb);
-       if (handle == NULL) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto leave;
-       }
-
-       status = ocfs2_meta_lock(dir, handle, &parent_node_bh, 1);
+       status = ocfs2_meta_lock(dir, &parent_node_bh, 1);
        if (status < 0) {
                if (status != -ENOENT)
                        mlog_errno(status);
-               goto leave;
+               return status;
        }
 
        status = ocfs2_find_files_on_disk(dentry->d_name.name,
@@ -805,12 +852,13 @@ static int ocfs2_unlink(struct inode *dir,
                goto leave;
        }
 
-       status = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
+       status = ocfs2_meta_lock(inode, &fe_bh, 1);
        if (status < 0) {
                if (status != -ENOENT)
                        mlog_errno(status);
                goto leave;
        }
+       child_locked = 1;
 
        if (S_ISDIR(inode->i_mode)) {
                if (!ocfs2_empty_dir(inode)) {
@@ -822,18 +870,7 @@ static int ocfs2_unlink(struct inode *dir,
                }
        }
 
-       /* There are still a few steps left until we can consider the
-        * unlink to have succeeded. Save off nlink here before
-        * modification so we can set it back in case we hit an issue
-        * before commit. */
-       saved_nlink = inode->i_nlink;
-       if (S_ISDIR(inode->i_mode))
-               inode->i_nlink = 0;
-       else
-               inode->i_nlink--;
-
-       status = ocfs2_request_unlink_vote(inode, dentry,
-                                          (unsigned int) inode->i_nlink);
+       status = ocfs2_remote_dentry_delete(dentry);
        if (status < 0) {
                /* This vote should succeed under all normal
                 * circumstances. */
@@ -841,8 +878,8 @@ static int ocfs2_unlink(struct inode *dir,
                goto leave;
        }
 
-       if (!inode->i_nlink) {
-               status = ocfs2_prepare_orphan_dir(osb, handle, inode,
+       if (inode_is_unlinkable(inode)) {
+               status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode,
                                                  orphan_name,
                                                  &orphan_entry_bh);
                if (status < 0) {
@@ -851,7 +888,7 @@ static int ocfs2_unlink(struct inode *dir,
                }
        }
 
-       handle = ocfs2_start_trans(osb, handle, OCFS2_UNLINK_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
@@ -868,9 +905,9 @@ static int ocfs2_unlink(struct inode *dir,
 
        fe = (struct ocfs2_dinode *) fe_bh->b_data;
 
-       if (!inode->i_nlink) {
+       if (inode_is_unlinkable(inode)) {
                status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
-                                         orphan_entry_bh);
+                                         orphan_entry_bh, orphan_dir);
                if (status < 0) {
                        mlog_errno(status);
                        goto leave;
@@ -884,10 +921,10 @@ static int ocfs2_unlink(struct inode *dir,
                goto leave;
        }
 
-       /* We can set nlink on the dinode now. clear the saved version
-        * so that it doesn't get set later. */
+       if (S_ISDIR(inode->i_mode))
+               drop_nlink(inode);
+       drop_nlink(inode);
        fe->i_links_count = cpu_to_le16(inode->i_nlink);
-       saved_nlink = 0;
 
        status = ocfs2_journal_dirty(handle, fe_bh);
        if (status < 0) {
@@ -896,21 +933,30 @@ static int ocfs2_unlink(struct inode *dir,
        }
 
        if (S_ISDIR(inode->i_mode)) {
-               dir->i_nlink--;
+               drop_nlink(dir);
                status = ocfs2_mark_inode_dirty(handle, dir,
                                                parent_node_bh);
                if (status < 0) {
                        mlog_errno(status);
-                       dir->i_nlink++;
+                       inc_nlink(dir);
                }
        }
 
 leave:
-       if (status < 0 && saved_nlink)
-               inode->i_nlink = saved_nlink;
-
        if (handle)
-               ocfs2_commit_trans(handle);
+               ocfs2_commit_trans(osb, handle);
+
+       if (child_locked)
+               ocfs2_meta_unlock(inode, 1);
+
+       ocfs2_meta_unlock(dir, 1);
+
+       if (orphan_dir) {
+               /* This was locked for us in ocfs2_prepare_orphan_dir() */
+               ocfs2_meta_unlock(orphan_dir, 1);
+               mutex_unlock(&orphan_dir->i_mutex);
+               iput(orphan_dir);
+       }
 
        if (fe_bh)
                brelse(fe_bh);
@@ -934,7 +980,6 @@ leave:
  * if they have the same id, then the 1st one is the only one locked.
  */
 static int ocfs2_double_lock(struct ocfs2_super *osb,
-                            struct ocfs2_journal_handle *handle,
                             struct buffer_head **bh1,
                             struct inode *inode1,
                             struct buffer_head **bh2,
@@ -950,8 +995,6 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
                   (unsigned long long)oi1->ip_blkno,
                   (unsigned long long)oi2->ip_blkno);
 
-       BUG_ON(!handle);
-
        if (*bh1)
                *bh1 = NULL;
        if (*bh2)
@@ -971,25 +1014,41 @@ static int ocfs2_double_lock(struct ocfs2_super *osb,
                        inode1 = tmpinode;
                }
                /* lock id2 */
-               status = ocfs2_meta_lock(inode2, handle, bh2, 1);
+               status = ocfs2_meta_lock(inode2, bh2, 1);
                if (status < 0) {
                        if (status != -ENOENT)
                                mlog_errno(status);
                        goto bail;
                }
        }
+
        /* lock id1 */
-       status = ocfs2_meta_lock(inode1, handle, bh1, 1);
+       status = ocfs2_meta_lock(inode1, bh1, 1);
        if (status < 0) {
+               /*
+                * An error return must mean that no cluster locks
+                * were held on function exit.
+                */
+               if (oi1->ip_blkno != oi2->ip_blkno)
+                       ocfs2_meta_unlock(inode2, 1);
+
                if (status != -ENOENT)
                        mlog_errno(status);
-               goto bail;
        }
+
 bail:
        mlog_exit(status);
        return status;
 }
 
+static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2)
+{
+       ocfs2_meta_unlock(inode1, 1);
+
+       if (inode1 != inode2)
+               ocfs2_meta_unlock(inode2, 1);
+}
+
 #define PARENT_INO(buffer) \
        ((struct ocfs2_dir_entry *) \
         ((char *)buffer + \
@@ -1000,9 +1059,11 @@ static int ocfs2_rename(struct inode *old_dir,
                        struct inode *new_dir,
                        struct dentry *new_dentry)
 {
-       int status = 0, rename_lock = 0;
+       int status = 0, rename_lock = 0, parents_locked = 0;
+       int old_child_locked = 0, new_child_locked = 0;
        struct inode *old_inode = old_dentry->d_inode;
        struct inode *new_inode = new_dentry->d_inode;
+       struct inode *orphan_dir = NULL;
        struct ocfs2_dinode *newfe = NULL;
        char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
        struct buffer_head *orphan_entry_bh = NULL;
@@ -1010,7 +1071,7 @@ static int ocfs2_rename(struct inode *old_dir,
        struct buffer_head *insert_entry_bh = NULL;
        struct ocfs2_super *osb = NULL;
        u64 newfe_blkno;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle = NULL;
        struct buffer_head *old_dir_bh = NULL;
        struct buffer_head *new_dir_bh = NULL;
        struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
@@ -1019,7 +1080,6 @@ static int ocfs2_rename(struct inode *old_dir,
        struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
                                                    // this is the 1st dirent bh
        nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
-       unsigned int links_count;
 
        /* At some point it might be nice to break this function up a
         * bit. */
@@ -1036,14 +1096,6 @@ static int ocfs2_rename(struct inode *old_dir,
                        BUG();
        }
 
-       if (atomic_read(&old_dentry->d_count) > 2) {
-               shrink_dcache_parent(old_dentry);
-               if (atomic_read(&old_dentry->d_count) > 2) {
-                       status = -EBUSY;
-                       goto bail;
-               }
-       }
-
        /* Assume a directory heirarchy thusly:
         * a/b/c
         * a/d
@@ -1064,21 +1116,14 @@ static int ocfs2_rename(struct inode *old_dir,
                rename_lock = 1;
        }
 
-       handle = ocfs2_alloc_handle(osb);
-       if (handle == NULL) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
        /* if old and new are the same, this'll just do one lock. */
-       status = ocfs2_double_lock(osb, handle,
-                                 &old_dir_bh, old_dir,
-                                 &new_dir_bh, new_dir);
+       status = ocfs2_double_lock(osb, &old_dir_bh, old_dir,
+                                  &new_dir_bh, new_dir);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
+       parents_locked = 1;
 
        /* make sure both dirs have bhs
         * get an extra ref on old_dir_bh if old==new */
@@ -1093,23 +1138,27 @@ static int ocfs2_rename(struct inode *old_dir,
                }
        }
 
-       if (S_ISDIR(old_inode->i_mode)) {
-               /* Directories actually require metadata updates to
-                * the directory info so we can't get away with not
-                * doing node locking on it. */
-               status = ocfs2_meta_lock(old_inode, handle, NULL, 1);
-               if (status < 0) {
-                       if (status != -ENOENT)
-                               mlog_errno(status);
-                       goto bail;
-               }
-
-               status = ocfs2_request_rename_vote(old_inode, old_dentry);
-               if (status < 0) {
+       /*
+        * Though we don't require an inode meta data update if
+        * old_inode is not a directory, we lock anyway here to ensure
+        * the vote thread on other nodes won't have to concurrently
+        * downconvert the inode and the dentry locks.
+        */
+       status = ocfs2_meta_lock(old_inode, NULL, 1);
+       if (status < 0) {
+               if (status != -ENOENT)
                        mlog_errno(status);
-                       goto bail;
-               }
+               goto bail;
+       }
+       old_child_locked = 1;
+
+       status = ocfs2_remote_dentry_delete(old_dentry);
+       if (status < 0) {
+               mlog_errno(status);
+               goto bail;
+       }
 
+       if (S_ISDIR(old_inode->i_mode)) {
                status = -EIO;
                old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0);
                if (!old_inode_de_bh)
@@ -1123,14 +1172,6 @@ static int ocfs2_rename(struct inode *old_dir,
                if (!new_inode && new_dir!=old_dir &&
                    new_dir->i_nlink >= OCFS2_LINK_MAX)
                        goto bail;
-       } else {
-               /* Ah, the simple case - we're a file so just send a
-                * message. */
-               status = ocfs2_request_rename_vote(old_inode, old_dentry);
-               if (status < 0) {
-                       mlog_errno(status);
-                       goto bail;
-               }
        }
 
        status = -ENOENT;
@@ -1195,20 +1236,15 @@ static int ocfs2_rename(struct inode *old_dir,
                        goto bail;
                }
 
-               status = ocfs2_meta_lock(new_inode, handle, &newfe_bh, 1);
+               status = ocfs2_meta_lock(new_inode, &newfe_bh, 1);
                if (status < 0) {
                        if (status != -ENOENT)
                                mlog_errno(status);
                        goto bail;
                }
+               new_child_locked = 1;
 
-               if (S_ISDIR(new_inode->i_mode))
-                       links_count = 0;
-               else
-                       links_count = (unsigned int) (new_inode->i_nlink - 1);
-
-               status = ocfs2_request_unlink_vote(new_inode, new_dentry,
-                                                  links_count);
+               status = ocfs2_remote_dentry_delete(new_dentry);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
@@ -1222,7 +1258,7 @@ static int ocfs2_rename(struct inode *old_dir,
                     (unsigned long long)newfe_bh->b_blocknr : 0ULL);
 
                if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) {
-                       status = ocfs2_prepare_orphan_dir(osb, handle,
+                       status = ocfs2_prepare_orphan_dir(osb, &orphan_dir,
                                                          new_inode,
                                                          orphan_name,
                                                          &orphan_entry_bh);
@@ -1250,7 +1286,7 @@ static int ocfs2_rename(struct inode *old_dir,
                }
        }
 
-       handle = ocfs2_start_trans(osb, handle, OCFS2_RENAME_CREDITS);
+       handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
@@ -1277,7 +1313,7 @@ static int ocfs2_rename(struct inode *old_dir,
                    (newfe->i_links_count == cpu_to_le16(1))){
                        status = ocfs2_orphan_add(osb, handle, new_inode,
                                                  newfe, orphan_name,
-                                                 orphan_entry_bh);
+                                                 orphan_entry_bh, orphan_dir);
                        if (status < 0) {
                                mlog_errno(status);
                                goto bail;
@@ -1343,7 +1379,7 @@ static int ocfs2_rename(struct inode *old_dir,
                if (new_inode) {
                        new_inode->i_nlink--;
                } else {
-                       new_dir->i_nlink++;
+                       inc_nlink(new_dir);
                        mark_inode_dirty(new_dir);
                }
        }
@@ -1387,13 +1423,30 @@ static int ocfs2_rename(struct inode *old_dir,
                }
        }
 
+       ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir);
        status = 0;
 bail:
        if (rename_lock)
                ocfs2_rename_unlock(osb);
 
        if (handle)
-               ocfs2_commit_trans(handle);
+               ocfs2_commit_trans(osb, handle);
+
+       if (parents_locked)
+               ocfs2_double_unlock(old_dir, new_dir);
+
+       if (old_child_locked)
+               ocfs2_meta_unlock(old_inode, 1);
+
+       if (new_child_locked)
+               ocfs2_meta_unlock(new_inode, 1);
+
+       if (orphan_dir) {
+               /* This was locked for us in ocfs2_prepare_orphan_dir() */
+               ocfs2_meta_unlock(orphan_dir, 1);
+               mutex_unlock(&orphan_dir->i_mutex);
+               iput(orphan_dir);
+       }
 
        if (new_inode)
                sync_mapping_buffers(old_inode->i_mapping);
@@ -1427,7 +1480,7 @@ bail:
  * data, including the null terminator.
  */
 static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
-                                    struct ocfs2_journal_handle *handle,
+                                    handle_t *handle,
                                     struct inode *inode,
                                     const char *symname)
 {
@@ -1542,7 +1595,7 @@ static int ocfs2_symlink(struct inode *dir,
        struct buffer_head *parent_fe_bh = NULL;
        struct ocfs2_dinode *fe = NULL;
        struct ocfs2_dinode *dirfe;
-       struct ocfs2_journal_handle *handle = NULL;
+       handle_t *handle = NULL;
        struct ocfs2_alloc_context *inode_ac = NULL;
        struct ocfs2_alloc_context *data_ac = NULL;
 
@@ -1556,19 +1609,12 @@ static int ocfs2_symlink(struct inode *dir,
 
        credits = ocfs2_calc_symlink_credits(sb);
 
-       handle = ocfs2_alloc_handle(osb);
-       if (handle == NULL) {
-               status = -ENOMEM;
-               mlog_errno(status);
-               goto bail;
-       }
-
        /* lock the parent directory */
-       status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
+       status = ocfs2_meta_lock(dir, &parent_fe_bh, 1);
        if (status < 0) {
                if (status != -ENOENT)
                        mlog_errno(status);
-               goto bail;
+               return status;
        }
 
        dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
@@ -1591,7 +1637,7 @@ static int ocfs2_symlink(struct inode *dir,
                goto bail;
        }
 
-       status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
+       status = ocfs2_reserve_new_inode(osb, &inode_ac);
        if (status < 0) {
                if (status != -ENOSPC)
                        mlog_errno(status);
@@ -1600,7 +1646,7 @@ static int ocfs2_symlink(struct inode *dir,
 
        /* don't reserve bitmap space for fast symlinks. */
        if (l > ocfs2_fast_symlink_chars(sb)) {
-               status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
+               status = ocfs2_reserve_clusters(osb, 1, &data_ac);
                if (status < 0) {
                        if (status != -ENOSPC)
                                mlog_errno(status);
@@ -1608,7 +1654,7 @@ static int ocfs2_symlink(struct inode *dir,
                }
        }
 
-       handle = ocfs2_start_trans(osb, handle, credits);
+       handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
                status = PTR_ERR(handle);
                handle = NULL;
@@ -1675,12 +1721,21 @@ static int ocfs2_symlink(struct inode *dir,
                goto bail;
        }
 
+       status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
+       if (status) {
+               mlog_errno(status);
+               goto bail;
+       }
+
        insert_inode_hash(inode);
        dentry->d_op = &ocfs2_dentry_ops;
        d_instantiate(dentry, inode);
 bail:
        if (handle)
-               ocfs2_commit_trans(handle);
+               ocfs2_commit_trans(osb, handle);
+
+       ocfs2_meta_unlock(dir, 1);
+
        if (new_fe_bh)
                brelse(new_fe_bh);
        if (parent_fe_bh)
@@ -1731,7 +1786,7 @@ int ocfs2_check_dir_entry(struct inode * dir,
  * If you pass me insert_bh, I'll skip the search of the other dir
  * blocks and put the record in there.
  */
-static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
+static int __ocfs2_add_entry(handle_t *handle,
                             struct inode *dir,
                             const char *name, int namelen,
                             struct inode *inode, u64 blkno,
@@ -1817,7 +1872,7 @@ bail:
  * ocfs2_delete_entry deletes a directory entry by merging it with the
  * previous entry
  */
-static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
+static int ocfs2_delete_entry(handle_t *handle,
                              struct inode *dir,
                              struct ocfs2_dir_entry *de_del,
                              struct buffer_head *bh)
@@ -2048,19 +2103,19 @@ bail:
 }
 
 static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
-                                   struct ocfs2_journal_handle *handle,
+                                   struct inode **ret_orphan_dir,
                                    struct inode *inode,
                                    char *name,
                                    struct buffer_head **de_bh)
 {
-       struct inode *orphan_dir_inode = NULL;
+       struct inode *orphan_dir_inode;
        struct buffer_head *orphan_dir_bh = NULL;
        int status = 0;
 
        status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
        if (status < 0) {
                mlog_errno(status);
-               goto leave;
+               return status;
        }
 
        orphan_dir_inode = ocfs2_get_system_file_inode(osb,
@@ -2069,11 +2124,12 @@ static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
        if (!orphan_dir_inode) {
                status = -ENOENT;
                mlog_errno(status);
-               goto leave;
+               return status;
        }
 
-       ocfs2_handle_add_inode(handle, orphan_dir_inode);
-       status = ocfs2_meta_lock(orphan_dir_inode, handle, &orphan_dir_bh, 1);
+       mutex_lock(&orphan_dir_inode->i_mutex);
+
+       status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
        if (status < 0) {
                mlog_errno(status);
                goto leave;
@@ -2083,13 +2139,19 @@ static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
                                              orphan_dir_bh, name,
                                              OCFS2_ORPHAN_NAMELEN, de_bh);
        if (status < 0) {
+               ocfs2_meta_unlock(orphan_dir_inode, 1);
+
                mlog_errno(status);
                goto leave;
        }
 
+       *ret_orphan_dir = orphan_dir_inode;
+
 leave:
-       if (orphan_dir_inode)
+       if (status) {
+               mutex_unlock(&orphan_dir_inode->i_mutex);
                iput(orphan_dir_inode);
+       }
 
        if (orphan_dir_bh)
                brelse(orphan_dir_bh);
@@ -2099,28 +2161,19 @@ leave:
 }
 
 static int ocfs2_orphan_add(struct ocfs2_super *osb,
-                           struct ocfs2_journal_handle *handle,
+                           handle_t *handle,
                            struct inode *inode,
                            struct ocfs2_dinode *fe,
                            char *name,
-                           struct buffer_head *de_bh)
+                           struct buffer_head *de_bh,
+                           struct inode *orphan_dir_inode)
 {
-       struct inode *orphan_dir_inode = NULL;
        struct buffer_head *orphan_dir_bh = NULL;
        int status = 0;
        struct ocfs2_dinode *orphan_fe;
 
        mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
 
-       orphan_dir_inode = ocfs2_get_system_file_inode(osb,
-                                                      ORPHAN_DIR_SYSTEM_INODE,
-                                                      osb->slot_num);
-       if (!orphan_dir_inode) {
-               status = -ENOENT;
-               mlog_errno(status);
-               goto leave;
-       }
-
        status = ocfs2_read_block(osb,
                                  OCFS2_I(orphan_dir_inode)->ip_blkno,
                                  &orphan_dir_bh, OCFS2_BH_CACHED,
@@ -2172,9 +2225,6 @@ static int ocfs2_orphan_add(struct ocfs2_super *osb,
             (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
 
 leave:
-       if (orphan_dir_inode)
-               iput(orphan_dir_inode);
-
        if (orphan_dir_bh)
                brelse(orphan_dir_bh);
 
@@ -2184,7 +2234,7 @@ leave:
 
 /* unlike orphan_add, we expect the orphan dir to already be locked here. */
 int ocfs2_orphan_del(struct ocfs2_super *osb,
-                    struct ocfs2_journal_handle *handle,
+                    handle_t *handle,
                     struct inode *orphan_dir_inode,
                     struct inode *inode,
                     struct buffer_head *orphan_dir_bh)
@@ -2263,4 +2313,5 @@ struct inode_operations ocfs2_dir_iops = {
        .rename         = ocfs2_rename,
        .setattr        = ocfs2_setattr,
        .getattr        = ocfs2_getattr,
+       .permission     = ocfs2_permission,
 };