Merge branch 'pm-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/suspe...
[safe/jmp/linux-2.6] / fs / ocfs2 / dir.c
index 47de649..efd77d0 100644 (file)
@@ -80,22 +80,36 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
                               struct ocfs2_alloc_context *data_ac,
                               struct ocfs2_alloc_context *meta_ac,
                               struct buffer_head **new_bh);
+static int ocfs2_dir_indexed(struct inode *inode);
 
 /*
  * These are distinct checks because future versions of the file system will
  * want to have a trailing dirent structure independent of indexing.
  */
-static int ocfs2_dir_has_trailer(struct inode *dir)
+static int ocfs2_supports_dir_trailer(struct inode *dir)
 {
+       struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
+
        if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
                return 0;
 
-       return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
+       return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
 }
 
-static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
+/*
+ * "new' here refers to the point at which we're creating a new
+ * directory via "mkdir()", but also when we're expanding an inline
+ * directory. In either case, we don't yet have the indexing bit set
+ * on the directory, so the standard checks will fail in when metaecc
+ * is turned off. Only directory-initialization type functions should
+ * use this then. Everything else wants ocfs2_supports_dir_trailer()
+ */
+static int ocfs2_new_dir_wants_trailer(struct inode *dir)
 {
-       return ocfs2_meta_ecc(osb);
+       struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
+
+       return ocfs2_meta_ecc(osb) ||
+               ocfs2_supports_indexed_dirs(osb);
 }
 
 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
@@ -127,7 +141,7 @@ static int ocfs2_skip_dir_trailer(struct inode *dir,
 {
        unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
 
-       if (!ocfs2_dir_has_trailer(dir))
+       if (!ocfs2_supports_dir_trailer(dir))
                return 0;
 
        if (offset != toff)
@@ -137,7 +151,7 @@ static int ocfs2_skip_dir_trailer(struct inode *dir,
 }
 
 static void ocfs2_init_dir_trailer(struct inode *inode,
-                                  struct buffer_head *bh)
+                                  struct buffer_head *bh, u16 rec_len)
 {
        struct ocfs2_dir_block_trailer *trailer;
 
@@ -147,6 +161,42 @@ static void ocfs2_init_dir_trailer(struct inode *inode,
                        cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
        trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
        trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
+       trailer->db_free_rec_len = cpu_to_le16(rec_len);
+}
+/*
+ * Link an unindexed block with a dir trailer structure into the index free
+ * list. This function will modify dirdata_bh, but assumes you've already
+ * passed it to the journal.
+ */
+static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
+                                    struct buffer_head *dx_root_bh,
+                                    struct buffer_head *dirdata_bh)
+{
+       int ret;
+       struct ocfs2_dx_root_block *dx_root;
+       struct ocfs2_dir_block_trailer *trailer;
+
+       ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+       trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
+       dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
+
+       trailer->db_free_next = dx_root->dr_free_blk;
+       dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
+
+       ocfs2_journal_dirty(handle, dx_root_bh);
+
+out:
+       return ret;
+}
+
+static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
+{
+       return res->dl_prev_leaf_bh == NULL;
 }
 
 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
@@ -154,6 +204,7 @@ void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
        brelse(res->dl_dx_root_bh);
        brelse(res->dl_leaf_bh);
        brelse(res->dl_dx_leaf_bh);
+       brelse(res->dl_prev_leaf_bh);
 }
 
 static int ocfs2_dir_indexed(struct inode *inode)
@@ -484,7 +535,7 @@ static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
        }
 
        if (!(flags & OCFS2_BH_READAHEAD) &&
-           ocfs2_dir_has_trailer(inode)) {
+           ocfs2_supports_dir_trailer(inode)) {
                rc = ocfs2_check_dir_trailer(inode, tmp);
                if (rc) {
                        if (!*bh)
@@ -513,7 +564,8 @@ static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
        int ret;
        struct buffer_head *tmp = *bh;
 
-       ret = ocfs2_read_block(dir, phys, &tmp, ocfs2_validate_dir_block);
+       ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
+                              ocfs2_validate_dir_block);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -571,7 +623,8 @@ static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
        u64 blkno = le64_to_cpu(di->i_dx_root);
        struct buffer_head *tmp = *dx_root_bh;
 
-       ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_root);
+       ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
+                              ocfs2_validate_dx_root);
 
        /* If ocfs2_read_block() got us a new bh, pass it up. */
        if (!ret && !*dx_root_bh)
@@ -611,7 +664,8 @@ static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
        int ret;
        struct buffer_head *tmp = *dx_leaf_bh;
 
-       ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_leaf);
+       ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
+                              ocfs2_validate_dx_leaf);
 
        /* If ocfs2_read_block() got us a new bh, pass it up. */
        if (!ret && !*dx_leaf_bh)
@@ -629,7 +683,7 @@ static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
 {
        int ret;
 
-       ret = ocfs2_read_blocks(dir, start, num, dx_leaf_bhs, 0,
+       ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
                                ocfs2_validate_dx_leaf);
        if (ret)
                mlog_errno(ret);
@@ -751,7 +805,8 @@ static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
        struct ocfs2_extent_rec *rec = NULL;
 
        if (el->l_tree_depth) {
-               ret = ocfs2_find_leaf(inode, el, major_hash, &eb_bh);
+               ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
+                                     &eb_bh);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -1082,7 +1137,8 @@ int ocfs2_update_entry(struct inode *dir, handle_t *handle,
        if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
                access = ocfs2_journal_access_di;
 
-       ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = access(handle, INODE_CACHE(dir), de_bh,
+                    OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -1125,7 +1181,7 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
                        goto bail;
                }
                if (de == de_del)  {
-                       status = access(handle, dir, bh,
+                       status = access(handle, INODE_CACHE(dir), bh,
                                        OCFS2_JOURNAL_ACCESS_WRITE);
                        if (status < 0) {
                                status = -EIO;
@@ -1150,6 +1206,47 @@ bail:
        return status;
 }
 
+static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
+{
+       unsigned int hole;
+
+       if (le64_to_cpu(de->inode) == 0)
+               hole = le16_to_cpu(de->rec_len);
+       else
+               hole = le16_to_cpu(de->rec_len) -
+                       OCFS2_DIR_REC_LEN(de->name_len);
+
+       return hole;
+}
+
+static int ocfs2_find_max_rec_len(struct super_block *sb,
+                                 struct buffer_head *dirblock_bh)
+{
+       int size, this_hole, largest_hole = 0;
+       char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
+       struct ocfs2_dir_entry *de;
+
+       trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
+       size = ocfs2_dir_trailer_blk_off(sb);
+       limit = start + size;
+       de_buf = start;
+       de = (struct ocfs2_dir_entry *)de_buf;
+       do {
+               if (de_buf != trailer) {
+                       this_hole = ocfs2_figure_dirent_hole(de);
+                       if (this_hole > largest_hole)
+                               largest_hole = this_hole;
+               }
+
+               de_buf += le16_to_cpu(de->rec_len);
+               de = (struct ocfs2_dir_entry *)de_buf;
+       } while (de_buf < limit);
+
+       if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
+               return largest_hole;
+       return 0;
+}
+
 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
                                       int index)
 {
@@ -1171,14 +1268,26 @@ clear:
 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
                                 struct ocfs2_dir_lookup_result *lookup)
 {
-       int ret, index;
+       int ret, index, max_rec_len, add_to_free_list = 0;
        struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
        struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
        struct ocfs2_dx_leaf *dx_leaf;
        struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
+       struct ocfs2_dir_block_trailer *trailer;
        struct ocfs2_dx_root_block *dx_root;
        struct ocfs2_dx_entry_list *entry_list;
 
+       /*
+        * This function gets a bit messy because we might have to
+        * modify the root block, regardless of whether the indexed
+        * entries are stored inline.
+        */
+
+       /*
+        * *Only* set 'entry_list' here, based on where we're looking
+        * for the indexed entries. Later, we might still want to
+        * journal both blocks, based on free list state.
+        */
        dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
        if (ocfs2_dx_root_inline(dx_root)) {
                entry_list = &dx_root->dr_entries;
@@ -1203,24 +1312,34 @@ static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
        }
 
        /*
+        * We know that removal of this dirent will leave enough room
+        * for a new one, so add this block to the free list if it
+        * isn't already there.
+        */
+       trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
+       if (trailer->db_free_rec_len == 0)
+               add_to_free_list = 1;
+
+       /*
         * Add the block holding our index into the journal before
         * removing the unindexed entry. If we get an error return
         * from __ocfs2_delete_entry(), then it hasn't removed the
         * entry yet. Likewise, successful return means we *must*
         * remove the indexed entry.
         *
-        * We're also careful to journal the root tree block here if
-        * we're going to be adding to the start of the free list.
+        * We're also careful to journal the root tree block here as
+        * the entry count needs to be updated. Also, we might be
+        * adding to the start of the free list.
         */
-       if (ocfs2_dx_root_inline(dx_root)) {
-               ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
-                                             OCFS2_JOURNAL_ACCESS_WRITE);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
-       } else {
-               ret = ocfs2_journal_access_dl(handle, dir,
+       ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       if (!ocfs2_dx_root_inline(dx_root)) {
+               ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
                                              lookup->dl_dx_leaf_bh,
                                              OCFS2_JOURNAL_ACCESS_WRITE);
                if (ret) {
@@ -1239,11 +1358,23 @@ static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
                goto out;
        }
 
+       max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
+       trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
+       if (add_to_free_list) {
+               trailer->db_free_next = dx_root->dr_free_blk;
+               dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
+               ocfs2_journal_dirty(handle, dx_root_bh);
+       }
+
+       /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
+       ocfs2_journal_dirty(handle, leaf_bh);
+
+       le32_add_cpu(&dx_root->dr_num_entries, -1);
+       ocfs2_journal_dirty(handle, dx_root_bh);
+
        ocfs2_dx_list_remove_entry(entry_list, index);
 
-       if (ocfs2_dx_root_inline(dx_root))
-               ocfs2_journal_dirty(handle, dx_root_bh);
-       else
+       if (!ocfs2_dx_root_inline(dx_root))
                ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
 
 out:
@@ -1367,7 +1498,7 @@ static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
        int ret;
        struct ocfs2_dx_leaf *dx_leaf;
 
-       ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
+       ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -1382,44 +1513,100 @@ out:
        return ret;
 }
 
-static int ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
-                                      struct ocfs2_dx_hinfo *hinfo,
-                                      u64 dirent_blk,
-                                      struct buffer_head *dx_root_bh)
+static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
+                                       struct ocfs2_dx_hinfo *hinfo,
+                                       u64 dirent_blk,
+                                       struct ocfs2_dx_root_block *dx_root)
 {
-       int ret;
+       ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
+}
+
+static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
+                              struct ocfs2_dir_lookup_result *lookup)
+{
+       int ret = 0;
        struct ocfs2_dx_root_block *dx_root;
+       struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
 
-       ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
+       ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
-       ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
+       dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
+       if (ocfs2_dx_root_inline(dx_root)) {
+               ocfs2_dx_inline_root_insert(dir, handle,
+                                           &lookup->dl_hinfo,
+                                           lookup->dl_leaf_bh->b_blocknr,
+                                           dx_root);
+       } else {
+               ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
+                                                lookup->dl_leaf_bh->b_blocknr,
+                                                lookup->dl_dx_leaf_bh);
+               if (ret)
+                       goto out;
+       }
+
+       le32_add_cpu(&dx_root->dr_num_entries, 1);
        ocfs2_journal_dirty(handle, dx_root_bh);
 
 out:
        return ret;
 }
 
-static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
-                              struct ocfs2_dir_lookup_result *lookup)
+static void ocfs2_remove_block_from_free_list(struct inode *dir,
+                                      handle_t *handle,
+                                      struct ocfs2_dir_lookup_result *lookup)
 {
+       struct ocfs2_dir_block_trailer *trailer, *prev;
        struct ocfs2_dx_root_block *dx_root;
+       struct buffer_head *bh;
 
-       dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
-       if (ocfs2_dx_root_inline(dx_root))
-               return ocfs2_dx_inline_root_insert(dir, handle,
-                                                  &lookup->dl_hinfo,
-                                                  lookup->dl_leaf_bh->b_blocknr,
-                                                  lookup->dl_dx_root_bh);
-
-       return __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
-                                         lookup->dl_leaf_bh->b_blocknr,
-                                         lookup->dl_dx_leaf_bh);
+       trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
+
+       if (ocfs2_free_list_at_root(lookup)) {
+               bh = lookup->dl_dx_root_bh;
+               dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
+               dx_root->dr_free_blk = trailer->db_free_next;
+       } else {
+               bh = lookup->dl_prev_leaf_bh;
+               prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
+               prev->db_free_next = trailer->db_free_next;
+       }
+
+       trailer->db_free_rec_len = cpu_to_le16(0);
+       trailer->db_free_next = cpu_to_le64(0);
+
+       ocfs2_journal_dirty(handle, bh);
+       ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
+}
+
+/*
+ * This expects that a journal write has been reserved on
+ * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
+ */
+static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
+                                  struct ocfs2_dir_lookup_result *lookup)
+{
+       int max_rec_len;
+       struct ocfs2_dir_block_trailer *trailer;
+
+       /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
+       max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
+       if (max_rec_len) {
+               /*
+                * There's still room in this block, so no need to remove it
+                * from the free list. In this case, we just want to update
+                * the rec len accounting.
+                */
+               trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
+               trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
+               ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
+       } else {
+               ocfs2_remove_block_from_free_list(dir, handle, lookup);
+       }
 }
 
 /* we don't always have a dentry for what we want to add, so people
@@ -1450,7 +1637,33 @@ int __ocfs2_add_entry(handle_t *handle,
        if (!namelen)
                return -EINVAL;
 
-       if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
+       if (ocfs2_dir_indexed(dir)) {
+               struct buffer_head *bh;
+
+               /*
+                * An indexed dir may require that we update the free space
+                * list. Reserve a write to the previous node in the list so
+                * that we don't fail later.
+                *
+                * XXX: This can be either a dx_root_block, or an unindexed
+                * directory tree leaf block.
+                */
+               if (ocfs2_free_list_at_root(lookup)) {
+                       bh = lookup->dl_dx_root_bh;
+                       retval = ocfs2_journal_access_dr(handle,
+                                                INODE_CACHE(dir), bh,
+                                                OCFS2_JOURNAL_ACCESS_WRITE);
+               } else {
+                       bh = lookup->dl_prev_leaf_bh;
+                       retval = ocfs2_journal_access_db(handle,
+                                                INODE_CACHE(dir), bh,
+                                                OCFS2_JOURNAL_ACCESS_WRITE);
+               }
+               if (retval) {
+                       mlog_errno(retval);
+                       return retval;
+               }
+       } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
                data_start = di->id2.i_data.id_data;
                size = i_size_read(dir);
 
@@ -1494,11 +1707,13 @@ int __ocfs2_add_entry(handle_t *handle,
                        }
 
                        if (insert_bh == parent_fe_bh)
-                               status = ocfs2_journal_access_di(handle, dir,
+                               status = ocfs2_journal_access_di(handle,
+                                                                INODE_CACHE(dir),
                                                                 insert_bh,
                                                                 OCFS2_JOURNAL_ACCESS_WRITE);
                        else {
-                               status = ocfs2_journal_access_db(handle, dir,
+                               status = ocfs2_journal_access_db(handle,
+                                                                INODE_CACHE(dir),
                                                                 insert_bh,
                                              OCFS2_JOURNAL_ACCESS_WRITE);
 
@@ -1533,6 +1748,9 @@ int __ocfs2_add_entry(handle_t *handle,
                        de->name_len = namelen;
                        memcpy(de->name, name, namelen);
 
+                       if (ocfs2_dir_indexed(dir))
+                               ocfs2_recalc_free_list(dir, handle, lookup);
+
                        dir->i_version++;
                        status = ocfs2_journal_dirty(handle, insert_bh);
                        retval = 0;
@@ -1915,6 +2133,7 @@ struct ocfs2_empty_dir_priv {
        unsigned seen_dot;
        unsigned seen_dot_dot;
        unsigned seen_other;
+       unsigned dx_dir;
 };
 static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
                                   loff_t pos, u64 ino, unsigned type)
@@ -1924,6 +2143,13 @@ static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
        /*
         * Check the positions of "." and ".." records to be sure
         * they're in the correct place.
+        *
+        * Indexed directories don't need to proceed past the first
+        * two entries, so we end the scan after seeing '..'. Despite
+        * that, we allow the scan to proceed In the event that we
+        * have a corrupted indexed directory (no dot or dot dot
+        * entries). This allows us to double check for existing
+        * entries which might not have been found in the index.
         */
        if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
                p->seen_dot = 1;
@@ -1933,18 +2159,57 @@ static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
        if (name_len == 2 && !strncmp("..", name, 2) &&
            pos == OCFS2_DIR_REC_LEN(1)) {
                p->seen_dot_dot = 1;
+
+               if (p->dx_dir && p->seen_dot)
+                       return 1;
+
                return 0;
        }
 
        p->seen_other = 1;
        return 1;
 }
+
+static int ocfs2_empty_dir_dx(struct inode *inode,
+                             struct ocfs2_empty_dir_priv *priv)
+{
+       int ret;
+       struct buffer_head *di_bh = NULL;
+       struct buffer_head *dx_root_bh = NULL;
+       struct ocfs2_dinode *di;
+       struct ocfs2_dx_root_block *dx_root;
+
+       priv->dx_dir = 1;
+
+       ret = ocfs2_read_inode_block(inode, &di_bh);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+       di = (struct ocfs2_dinode *)di_bh->b_data;
+
+       ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+       dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
+
+       if (le32_to_cpu(dx_root->dr_num_entries) != 2)
+               priv->seen_other = 1;
+
+out:
+       brelse(di_bh);
+       brelse(dx_root_bh);
+       return ret;
+}
+
 /*
  * routine to check that the specified directory is empty (for rmdir)
  *
  * Returns 1 if dir is empty, zero otherwise.
  *
- * XXX: This is a performance problem
+ * XXX: This is a performance problem for unindexed directories.
  */
 int ocfs2_empty_dir(struct inode *inode)
 {
@@ -1954,6 +2219,16 @@ int ocfs2_empty_dir(struct inode *inode)
 
        memset(&priv, 0, sizeof(priv));
 
+       if (ocfs2_dir_indexed(inode)) {
+               ret = ocfs2_empty_dir_dx(inode, &priv);
+               if (ret)
+                       mlog_errno(ret);
+               /*
+                * We still run ocfs2_dir_foreach to get the checks
+                * for "." and "..".
+                */
+       }
+
        ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
        if (ret)
                mlog_errno(ret);
@@ -2014,7 +2289,7 @@ static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
        struct ocfs2_inline_data *data = &di->id2.i_data;
        unsigned int size = le16_to_cpu(data->id_count);
 
-       ret = ocfs2_journal_access_di(handle, inode, di_bh,
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -2056,7 +2331,7 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
 
        mlog_entry_void();
 
-       if (ocfs2_supports_dir_trailer(osb))
+       if (ocfs2_new_dir_wants_trailer(inode))
                size = ocfs2_dir_trailer_blk_off(parent->i_sb);
 
        status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
@@ -2066,9 +2341,9 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
                goto bail;
        }
 
-       ocfs2_set_new_buffer_uptodate(inode, new_bh);
+       ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
 
-       status = ocfs2_journal_access_db(handle, inode, new_bh,
+       status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
                                         OCFS2_JOURNAL_ACCESS_CREATE);
        if (status < 0) {
                mlog_errno(status);
@@ -2077,8 +2352,19 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
        memset(new_bh->b_data, 0, osb->sb->s_blocksize);
 
        de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
-       if (ocfs2_supports_dir_trailer(osb))
-               ocfs2_init_dir_trailer(inode, new_bh);
+       if (ocfs2_new_dir_wants_trailer(inode)) {
+               int size = le16_to_cpu(de->rec_len);
+
+               /*
+                * Figure out the size of the hole left over after
+                * insertion of '.' and '..'. The trailer wants this
+                * information.
+                */
+               size -= OCFS2_DIR_REC_LEN(2);
+               size -= sizeof(struct ocfs2_dir_block_trailer);
+
+               ocfs2_init_dir_trailer(inode, new_bh, size);
+       }
 
        status = ocfs2_journal_dirty(handle, new_bh);
        if (status < 0) {
@@ -2110,8 +2396,9 @@ bail:
 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
                                     handle_t *handle, struct inode *dir,
                                     struct buffer_head *di_bh,
+                                    struct buffer_head *dirdata_bh,
                                     struct ocfs2_alloc_context *meta_ac,
-                                    int dx_inline,
+                                    int dx_inline, u32 num_entries,
                                     struct buffer_head **ret_dx_root_bh)
 {
        int ret;
@@ -2121,6 +2408,8 @@ static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
        unsigned int num_bits;
        struct buffer_head *dx_root_bh = NULL;
        struct ocfs2_dx_root_block *dx_root;
+       struct ocfs2_dir_block_trailer *trailer =
+               ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
 
        ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, &dr_suballoc_bit,
                                   &num_bits, &dr_blkno);
@@ -2138,9 +2427,9 @@ static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
                ret = -EIO;
                goto out;
        }
-       ocfs2_set_new_buffer_uptodate(dir, dx_root_bh);
+       ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
 
-       ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
+       ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
                                      OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret < 0) {
                mlog_errno(ret);
@@ -2150,11 +2439,16 @@ static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
        dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
        memset(dx_root, 0, osb->sb->s_blocksize);
        strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
-       dx_root->dr_suballoc_slot = cpu_to_le16(osb->slot_num);
+       dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
        dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
        dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
        dx_root->dr_blkno = cpu_to_le64(dr_blkno);
        dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
+       dx_root->dr_num_entries = cpu_to_le32(num_entries);
+       if (le16_to_cpu(trailer->db_free_rec_len))
+               dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
+       else
+               dx_root->dr_free_blk = cpu_to_le64(0);
 
        if (dx_inline) {
                dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
@@ -2169,7 +2463,7 @@ static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
        if (ret)
                mlog_errno(ret);
 
-       ret = ocfs2_journal_access_di(handle, dir, di_bh,
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
                                      OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret) {
                mlog_errno(ret);
@@ -2210,9 +2504,9 @@ static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
                }
                dx_leaves[i] = bh;
 
-               ocfs2_set_new_buffer_uptodate(dir, bh);
+               ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
 
-               ret = ocfs2_journal_access_dl(handle, dir, bh,
+               ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
                                              OCFS2_JOURNAL_ACCESS_CREATE);
                if (ret < 0) {
                        mlog_errno(ret);
@@ -2297,7 +2591,6 @@ static int ocfs2_dx_dir_new_cluster(struct inode *dir,
 {
        int ret;
        u64 phys_blkno;
-       struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
 
        ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
                                         num_dx_leaves, &phys_blkno);
@@ -2306,7 +2599,7 @@ static int ocfs2_dx_dir_new_cluster(struct inode *dir,
                goto out;
        }
 
-       ret = ocfs2_insert_extent(osb, handle, dir, et, cpos, phys_blkno, 1, 0,
+       ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
                                  meta_ac);
        if (ret)
                mlog_errno(ret);
@@ -2361,8 +2654,8 @@ static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
                goto out;
        }
 
-       ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh,
-                                       meta_ac, 1, &dx_root_bh);
+       ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
+                                       meta_ac, 1, 2, &dx_root_bh);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -2371,6 +2664,7 @@ static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
        entry_list = &dx_root->dr_entries;
 
        /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
+       ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
        ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
 
        ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
@@ -2408,9 +2702,10 @@ static int ocfs2_dx_dir_index_block(struct inode *dir,
                                    handle_t *handle,
                                    struct buffer_head **dx_leaves,
                                    int num_dx_leaves,
+                                   u32 *num_dx_entries,
                                    struct buffer_head *dirent_bh)
 {
-       int ret, namelen, i;
+       int ret = 0, namelen, i;
        char *de_buf, *limit;
        struct ocfs2_dir_entry *de;
        struct buffer_head *dx_leaf_bh;
@@ -2439,6 +2734,8 @@ static int ocfs2_dx_dir_index_block(struct inode *dir,
                        goto out;
                }
 
+               *num_dx_entries = *num_dx_entries + 1;
+
 inc:
                de_buf += le16_to_cpu(de->rec_len);
        }
@@ -2446,7 +2743,8 @@ inc:
 out:
        return ret;
 }
- /*
+
+/*
  * XXX: This expects dx_root_bh to already be part of the transaction.
  */
 static void ocfs2_dx_dir_index_root_block(struct inode *dir,
@@ -2481,6 +2779,8 @@ static void ocfs2_dx_dir_index_root_block(struct inode *dir,
 
                ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
                                           dirent_blk);
+
+               le32_add_cpu(&dx_root->dr_num_entries, 1);
 inc:
                de_buf += le16_to_cpu(de->rec_len);
        }
@@ -2521,18 +2821,26 @@ static int ocfs2_new_dx_should_be_inline(struct inode *dir,
  * expansion from an inline directory to one with extents. The first dir block
  * in that case is taken from the inline data portion of the inode block.
  *
+ * This will also return the largest amount of contiguous space for a dirent
+ * in the block. That value is *not* necessarily the last dirent, even after
+ * expansion. The directory indexing code wants this value for free space
+ * accounting. We do this here since we're already walking the entire dir
+ * block.
+ *
  * We add the dir trailer if this filesystem wants it.
  */
-static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
-                                    struct super_block *sb)
+static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
+                                            struct inode *dir)
 {
+       struct super_block *sb = dir->i_sb;
        struct ocfs2_dir_entry *de;
        struct ocfs2_dir_entry *prev_de;
        char *de_buf, *limit;
        unsigned int new_size = sb->s_blocksize;
-       unsigned int bytes;
+       unsigned int bytes, this_hole;
+       unsigned int largest_hole = 0;
 
-       if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
+       if (ocfs2_new_dir_wants_trailer(dir))
                new_size = ocfs2_dir_trailer_blk_off(sb);
 
        bytes = new_size - old_size;
@@ -2541,12 +2849,26 @@ static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
        de_buf = start;
        de = (struct ocfs2_dir_entry *)de_buf;
        do {
+               this_hole = ocfs2_figure_dirent_hole(de);
+               if (this_hole > largest_hole)
+                       largest_hole = this_hole;
+
                prev_de = de;
                de_buf += le16_to_cpu(de->rec_len);
                de = (struct ocfs2_dir_entry *)de_buf;
        } while (de_buf < limit);
 
        le16_add_cpu(&prev_de->rec_len, bytes);
+
+       /* We need to double check this after modification of the final
+        * dirent. */
+       this_hole = ocfs2_figure_dirent_hole(prev_de);
+       if (this_hole > largest_hole)
+               largest_hole = this_hole;
+
+       if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
+               return largest_hole;
+       return 0;
 }
 
 /*
@@ -2562,7 +2884,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                                   struct ocfs2_dir_lookup_result *lookup,
                                   struct buffer_head **first_block_bh)
 {
-       u32 alloc, dx_alloc, bit_off, len;
+       u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
        struct super_block *sb = dir->i_sb;
        int ret, i, num_dx_leaves = 0, dx_inline = 0,
                credits = ocfs2_inline_to_extents_credits(sb);
@@ -2581,11 +2903,13 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        struct ocfs2_extent_tree dx_et;
        int did_quota = 0, bytes_allocated = 0;
 
-       ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
+       ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
 
        alloc = ocfs2_clusters_for_bytes(sb, bytes);
        dx_alloc = 0;
 
+       down_write(&oi->ip_alloc_sem);
+
        if (ocfs2_supports_indexed_dirs(osb)) {
                credits += ocfs2_add_dir_index_credits(sb);
 
@@ -2620,14 +2944,12 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
         */
        BUG_ON(alloc > 2);
 
-       ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
+       ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       down_write(&oi->ip_alloc_sem);
-
        /*
         * Prepare for worst case allocation scenario of two separate
         * extents in the unindexed tree.
@@ -2639,15 +2961,13 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
                mlog_errno(ret);
-               goto out_sem;
+               goto out;
        }
 
-       if (vfs_dq_alloc_space_nodirty(dir,
-                               ocfs2_clusters_to_bytes(osb->sb,
-                                                       alloc + dx_alloc))) {
-               ret = -EDQUOT;
+       ret = dquot_alloc_space_nodirty(dir,
+               ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
+       if (ret)
                goto out_commit;
-       }
        did_quota = 1;
 
        if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
@@ -2691,9 +3011,9 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                goto out_commit;
        }
 
-       ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
+       ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
 
-       ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
+       ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
                                      OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret) {
                mlog_errno(ret);
@@ -2703,9 +3023,16 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
        memset(dirdata_bh->b_data + i_size_read(dir), 0,
               sb->s_blocksize - i_size_read(dir));
-       ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
-       if (ocfs2_supports_dir_trailer(osb))
-               ocfs2_init_dir_trailer(dir, dirdata_bh);
+       i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
+       if (ocfs2_new_dir_wants_trailer(dir)) {
+               /*
+                * Prepare the dir trailer up front. It will otherwise look
+                * like a valid dirent. Even if inserting the index fails
+                * (unlikely), then all we'll have done is given first dir
+                * block a small amount of fragmentation.
+                */
+               ocfs2_init_dir_trailer(dir, dirdata_bh, i);
+       }
 
        ret = ocfs2_journal_dirty(handle, dirdata_bh);
        if (ret) {
@@ -2717,10 +3044,14 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                /*
                 * Dx dirs with an external cluster need to do this up
                 * front. Inline dx root's get handled later, after
-                * we've allocated our root block.
+                * we've allocated our root block. We get passed back
+                * a total number of items so that dr_num_entries can
+                * be correctly set once the dx_root has been
+                * allocated.
                 */
                ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
-                                              num_dx_leaves, dirdata_bh);
+                                              num_dx_leaves, &num_dx_entries,
+                                              dirdata_bh);
                if (ret) {
                        mlog_errno(ret);
                        goto out_commit;
@@ -2735,7 +3066,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
         * We let the later dirent insert modify c/mtime - to the user
         * the data hasn't changed.
         */
-       ret = ocfs2_journal_access_di(handle, dir, di_bh,
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
                                      OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret) {
                mlog_errno(ret);
@@ -2760,7 +3091,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
         * This should never fail as our extent list is empty and all
         * related blocks have been journaled already.
         */
-       ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
+       ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
                                  0, NULL);
        if (ret) {
                mlog_errno(ret);
@@ -2781,8 +3112,8 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
 
        if (ocfs2_supports_indexed_dirs(osb)) {
                ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
-                                               meta_ac, dx_inline,
-                                               &dx_root_bh);
+                                               dirdata_bh, meta_ac, dx_inline,
+                                               num_dx_entries, &dx_root_bh);
                if (ret) {
                        mlog_errno(ret);
                        goto out_commit;
@@ -2792,8 +3123,10 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                        ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
                                                      dirdata_bh);
                } else {
-                       ocfs2_init_dx_root_extent_tree(&dx_et, dir, dx_root_bh);
-                       ret = ocfs2_insert_extent(osb, handle, dir, &dx_et, 0,
+                       ocfs2_init_dx_root_extent_tree(&dx_et,
+                                                      INODE_CACHE(dir),
+                                                      dx_root_bh);
+                       ret = ocfs2_insert_extent(handle, &dx_et, 0,
                                                  dx_insert_blkno, 1, 0, NULL);
                        if (ret)
                                mlog_errno(ret);
@@ -2813,7 +3146,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                }
                blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
 
-               ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
+               ret = ocfs2_insert_extent(handle, &et, 1,
                                          blkno, len, 0, NULL);
                if (ret) {
                        mlog_errno(ret);
@@ -2843,14 +3176,12 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
 
 out_commit:
        if (ret < 0 && did_quota)
-               vfs_dq_free_space_nodirty(dir, bytes_allocated);
+               dquot_free_space_nodirty(dir, bytes_allocated);
 
        ocfs2_commit_trans(osb, handle);
 
-out_sem:
-       up_write(&oi->ip_alloc_sem);
-
 out:
+       up_write(&oi->ip_alloc_sem);
        if (data_ac)
                ocfs2_free_alloc_context(data_ac);
        if (meta_ac)
@@ -2888,11 +3219,10 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
        if (extend) {
                u32 offset = OCFS2_I(dir)->ip_clusters;
 
-               if (vfs_dq_alloc_space_nodirty(dir,
-                                       ocfs2_clusters_to_bytes(sb, 1))) {
-                       status = -EDQUOT;
+               status = dquot_alloc_space_nodirty(dir,
+                                       ocfs2_clusters_to_bytes(sb, 1));
+               if (status)
                        goto bail;
-               }
                did_quota = 1;
 
                status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
@@ -2921,7 +3251,7 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
        status = 0;
 bail:
        if (did_quota && status < 0)
-               vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
+               dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
        mlog_exit(status);
        return status;
 }
@@ -2933,6 +3263,8 @@ bail:
  * is to be turned into an extent based one. The size of the dirent to
  * insert might be larger than the space gained by growing to just one
  * block, so we may have to grow the inode by two blocks in that case.
+ *
+ * If the directory is already indexed, dx_root_bh must be provided.
  */
 static int ocfs2_extend_dir(struct ocfs2_super *osb,
                            struct inode *dir,
@@ -2953,10 +3285,17 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
        struct ocfs2_dir_entry * de;
        struct super_block *sb = osb->sb;
        struct ocfs2_extent_tree et;
+       struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
 
        mlog_entry_void();
 
        if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
+               /*
+                * This would be a code error as an inline directory should
+                * never have an index root.
+                */
+               BUG_ON(dx_root_bh);
+
                status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
                                                 blocks_wanted, lookup,
                                                 &new_bh);
@@ -2965,6 +3304,10 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                        goto bail;
                }
 
+               /* Expansion from inline to an indexed directory will
+                * have given us this. */
+               dx_root_bh = lookup->dl_dx_root_bh;
+
                if (blocks_wanted == 1) {
                        /*
                         * If the new dirent will fit inside the space
@@ -2984,11 +3327,15 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                brelse(new_bh);
                new_bh = NULL;
 
+               down_write(&OCFS2_I(dir)->ip_alloc_sem);
+               drop_alloc_sem = 1;
                dir_i_size = i_size_read(dir);
                credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
                goto do_extend;
        }
 
+       down_write(&OCFS2_I(dir)->ip_alloc_sem);
+       drop_alloc_sem = 1;
        dir_i_size = i_size_read(dir);
        mlog(0, "extending dir %llu (i_size = %lld)\n",
             (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
@@ -2997,8 +3344,9 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
        spin_lock(&OCFS2_I(dir)->ip_lock);
        if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
                spin_unlock(&OCFS2_I(dir)->ip_lock);
-               ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
-               num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
+               ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
+                                             parent_fe_bh);
+               num_free_extents = ocfs2_num_free_extents(osb, &et);
                if (num_free_extents < 0) {
                        status = num_free_extents;
                        mlog_errno(status);
@@ -3028,8 +3376,9 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
        }
 
 do_extend:
-       down_write(&OCFS2_I(dir)->ip_alloc_sem);
-       drop_alloc_sem = 1;
+       if (ocfs2_dir_indexed(dir))
+               credits++; /* For attaching the new dirent block to the
+                           * dx_root */
 
        handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
@@ -3046,9 +3395,9 @@ do_extend:
                goto bail;
        }
 
-       ocfs2_set_new_buffer_uptodate(dir, new_bh);
+       ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
 
-       status = ocfs2_journal_access_db(handle, dir, new_bh,
+       status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
                                         OCFS2_JOURNAL_ACCESS_CREATE);
        if (status < 0) {
                mlog_errno(status);
@@ -3058,9 +3407,19 @@ do_extend:
 
        de = (struct ocfs2_dir_entry *) new_bh->b_data;
        de->inode = 0;
-       if (ocfs2_dir_has_trailer(dir)) {
+       if (ocfs2_supports_dir_trailer(dir)) {
                de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
-               ocfs2_init_dir_trailer(dir, new_bh);
+
+               ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
+
+               if (ocfs2_dir_indexed(dir)) {
+                       status = ocfs2_dx_dir_link_trailer(dir, handle,
+                                                          dx_root_bh, new_bh);
+                       if (status) {
+                               mlog_errno(status);
+                               goto bail;
+                       }
+               }
        } else {
                de->rec_len = cpu_to_le16(sb->s_blocksize);
        }
@@ -3083,10 +3442,10 @@ bail_bh:
        *new_de_bh = new_bh;
        get_bh(*new_de_bh);
 bail:
-       if (drop_alloc_sem)
-               up_write(&OCFS2_I(dir)->ip_alloc_sem);
        if (handle)
                ocfs2_commit_trans(osb, handle);
+       if (drop_alloc_sem)
+               up_write(&OCFS2_I(dir)->ip_alloc_sem);
 
        if (data_ac)
                ocfs2_free_alloc_context(data_ac);
@@ -3116,7 +3475,7 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
         * This calculates how many free bytes we'd have in block zero, should
         * this function force expansion to an extent tree.
         */
-       if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
+       if (ocfs2_new_dir_wants_trailer(dir))
                free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
        else
                free_space = dir->i_sb->s_blocksize - i_size_read(dir);
@@ -3478,7 +3837,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
             (unsigned long long)OCFS2_I(dir)->ip_blkno,
             (unsigned long long)leaf_blkno, insert_hash);
 
-       ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
+       ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
 
        dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
        /*
@@ -3527,14 +3886,13 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
                goto out;
        }
 
-       if (vfs_dq_alloc_space_nodirty(dir,
-                                      ocfs2_clusters_to_bytes(dir->i_sb, 1))) {
-               ret = -EDQUOT;
+       ret = dquot_alloc_space_nodirty(dir,
+                                      ocfs2_clusters_to_bytes(dir->i_sb, 1));
+       if (ret)
                goto out_commit;
-       }
        did_quota = 1;
 
-       ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
+       ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -3589,8 +3947,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
                goto out_commit;
        }
 
-       orig_leaves_start = leaf_blkno & ~(osb->s_clustersize_bits -
-                                     osb->sb->s_blocksize_bits);
+       orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
        ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
                                   orig_dx_leaves);
        if (ret) {
@@ -3599,7 +3956,8 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
        }
 
        for (i = 0; i < num_dx_leaves; i++) {
-               ret = ocfs2_journal_access_dl(handle, dir, orig_dx_leaves[i],
+               ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
+                                             orig_dx_leaves[i],
                                              OCFS2_JOURNAL_ACCESS_WRITE);
                if (ret) {
                        mlog_errno(ret);
@@ -3621,7 +3979,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
 
 out_commit:
        if (ret < 0 && did_quota)
-               vfs_dq_free_space_nodirty(dir,
+               dquot_free_space_nodirty(dir,
                                ocfs2_clusters_to_bytes(dir->i_sb, 1));
 
        ocfs2_commit_trans(osb, handle);
@@ -3647,6 +4005,127 @@ out:
        return ret;
 }
 
+static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
+                                  struct buffer_head *di_bh,
+                                  struct buffer_head *dx_root_bh,
+                                  const char *name, int namelen,
+                                  struct ocfs2_dir_lookup_result *lookup)
+{
+       int ret, rebalanced = 0;
+       struct ocfs2_dx_root_block *dx_root;
+       struct buffer_head *dx_leaf_bh = NULL;
+       struct ocfs2_dx_leaf *dx_leaf;
+       u64 blkno;
+       u32 leaf_cpos;
+
+       dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
+
+restart_search:
+       ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
+                                 &leaf_cpos, &blkno);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
+
+       if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
+           le16_to_cpu(dx_leaf->dl_list.de_count)) {
+               if (rebalanced) {
+                       /*
+                        * Rebalancing should have provided us with
+                        * space in an appropriate leaf.
+                        *
+                        * XXX: Is this an abnormal condition then?
+                        * Should we print a message here?
+                        */
+                       ret = -ENOSPC;
+                       goto out;
+               }
+
+               ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
+                                            &lookup->dl_hinfo, leaf_cpos,
+                                            blkno);
+               if (ret) {
+                       if (ret != -ENOSPC)
+                               mlog_errno(ret);
+                       goto out;
+               }
+
+               /*
+                * Restart the lookup. The rebalance might have
+                * changed which block our item fits into. Mark our
+                * progress, so we only execute this once.
+                */
+               brelse(dx_leaf_bh);
+               dx_leaf_bh = NULL;
+               rebalanced = 1;
+               goto restart_search;
+       }
+
+       lookup->dl_dx_leaf_bh = dx_leaf_bh;
+       dx_leaf_bh = NULL;
+
+out:
+       brelse(dx_leaf_bh);
+       return ret;
+}
+
+static int ocfs2_search_dx_free_list(struct inode *dir,
+                                    struct buffer_head *dx_root_bh,
+                                    int namelen,
+                                    struct ocfs2_dir_lookup_result *lookup)
+{
+       int ret = -ENOSPC;
+       struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
+       struct ocfs2_dir_block_trailer *db;
+       u64 next_block;
+       int rec_len = OCFS2_DIR_REC_LEN(namelen);
+       struct ocfs2_dx_root_block *dx_root;
+
+       dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
+       next_block = le64_to_cpu(dx_root->dr_free_blk);
+
+       while (next_block) {
+               brelse(prev_leaf_bh);
+               prev_leaf_bh = leaf_bh;
+               leaf_bh = NULL;
+
+               ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
+               if (ret) {
+                       mlog_errno(ret);
+                       goto out;
+               }
+
+               db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
+               if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
+                       lookup->dl_leaf_bh = leaf_bh;
+                       lookup->dl_prev_leaf_bh = prev_leaf_bh;
+                       leaf_bh = NULL;
+                       prev_leaf_bh = NULL;
+                       break;
+               }
+
+               next_block = le64_to_cpu(db->db_free_next);
+       }
+
+       if (!next_block)
+               ret = -ENOSPC;
+
+out:
+
+       brelse(leaf_bh);
+       brelse(prev_leaf_bh);
+       return ret;
+}
+
 static int ocfs2_expand_inline_dx_root(struct inode *dir,
                                       struct buffer_head *dx_root_bh)
 {
@@ -3682,11 +4161,10 @@ static int ocfs2_expand_inline_dx_root(struct inode *dir,
                goto out;
        }
 
-       if (vfs_dq_alloc_space_nodirty(dir,
-                                      ocfs2_clusters_to_bytes(osb->sb, 1))) {
-               ret = -EDQUOT;
+       ret = dquot_alloc_space_nodirty(dir,
+                                      ocfs2_clusters_to_bytes(osb->sb, 1));
+       if (ret)
                goto out_commit;
-       }
        did_quota = 1;
 
        /*
@@ -3694,7 +4172,7 @@ static int ocfs2_expand_inline_dx_root(struct inode *dir,
         * failure to add the dx_root_bh to the journal won't result
         * us losing clusters.
         */
-       ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
+       ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -3736,9 +4214,8 @@ static int ocfs2_expand_inline_dx_root(struct inode *dir,
 
        /* This should never fail considering we start with an empty
         * dx_root. */
-       ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
-       ret = ocfs2_insert_extent(osb, handle, dir, &et, 0,
-                                 insert_blkno, 1, 0, NULL);
+       ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
+       ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
        if (ret)
                mlog_errno(ret);
        did_quota = 0;
@@ -3747,7 +4224,7 @@ static int ocfs2_expand_inline_dx_root(struct inode *dir,
 
 out_commit:
        if (ret < 0 && did_quota)
-               vfs_dq_free_space_nodirty(dir,
+               dquot_free_space_nodirty(dir,
                                          ocfs2_clusters_to_bytes(dir->i_sb, 1));
 
        ocfs2_commit_trans(osb, handle);
@@ -3779,19 +4256,18 @@ static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
        return 0;
 }
 
-static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
-                                  struct buffer_head *di_bh, const char *name,
-                                  int namelen,
-                                  struct ocfs2_dir_lookup_result *lookup)
+static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
+                                          struct buffer_head *di_bh,
+                                          const char *name,
+                                          int namelen,
+                                          struct ocfs2_dir_lookup_result *lookup)
 {
-       int ret, rebalanced = 0;
+       int ret, free_dx_root = 1;
+       struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
        struct buffer_head *dx_root_bh = NULL;
-       struct ocfs2_dx_root_block *dx_root;
-       struct buffer_head *dx_leaf_bh = NULL;
-       struct ocfs2_dx_leaf *dx_leaf;
+       struct buffer_head *leaf_bh = NULL;
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
-       u64 blkno;
-       u32 leaf_cpos;
+       struct ocfs2_dx_root_block *dx_root;
 
        ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
        if (ret) {
@@ -3800,6 +4276,12 @@ static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
        }
 
        dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
+       if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
+               ret = -ENOSPC;
+               mlog_errno(ret);
+               goto out;
+       }
+
        if (ocfs2_dx_root_inline(dx_root)) {
                ret = ocfs2_inline_dx_has_space(dx_root_bh);
 
@@ -3818,65 +4300,55 @@ static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
                }
        }
 
-restart_search:
-       ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
-                                 &leaf_cpos, &blkno);
+       /*
+        * Insert preparation for an indexed directory is split into two
+        * steps. The call to find_dir_space_dx reserves room in the index for
+        * an additional item. If we run out of space there, it's a real error
+        * we can't continue on.
+        */
+       ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
+                                     namelen, lookup);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
-       if (ret) {
+search_el:
+       /*
+        * Next, we need to find space in the unindexed tree. This call
+        * searches using the free space linked list. If the unindexed tree
+        * lacks sufficient space, we'll expand it below. The expansion code
+        * is smart enough to add any new blocks to the free space list.
+        */
+       ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
+       if (ret && ret != -ENOSPC) {
                mlog_errno(ret);
                goto out;
        }
 
-       dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
+       /* Do this up here - ocfs2_extend_dir might need the dx_root */
+       lookup->dl_dx_root_bh = dx_root_bh;
+       free_dx_root = 0;
 
-       if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
-           le16_to_cpu(dx_leaf->dl_list.de_count)) {
-               if (rebalanced) {
-                       /*
-                        * Rebalancing should have provided us with
-                        * space in an appropriate leaf.
-                        *
-                        * XXX: Is this an abnormal condition then?
-                        * Should we print a message here?
-                        */
-                       ret = -ENOSPC;
-                       goto out;
-               }
+       if (ret == -ENOSPC) {
+               ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
 
-               ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
-                                            &lookup->dl_hinfo, leaf_cpos,
-                                            blkno);
                if (ret) {
-                       if (ret != -ENOSPC)
-                               mlog_errno(ret);
+                       mlog_errno(ret);
                        goto out;
                }
 
                /*
-                * Restart the lookup. The rebalance might have
-                * changed which block our item fits into. Mark our
-                * progress, so we only execute this once.
+                * We make the assumption here that new leaf blocks are added
+                * to the front of our free list.
                 */
-               brelse(dx_leaf_bh);
-               dx_leaf_bh = NULL;
-               rebalanced = 1;
-               goto restart_search;
+               lookup->dl_prev_leaf_bh = NULL;
+               lookup->dl_leaf_bh = leaf_bh;
        }
 
-search_el:
-       lookup->dl_dx_leaf_bh = dx_leaf_bh;
-       dx_leaf_bh = NULL;
-       lookup->dl_dx_root_bh = dx_root_bh;
-       dx_root_bh = NULL;
-
 out:
-       brelse(dx_leaf_bh);
-       brelse(dx_root_bh);
+       if (free_dx_root)
+               brelse(dx_root_bh);
        return ret;
 }
 
@@ -3921,17 +4393,11 @@ int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
                ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
 
        if (ocfs2_dir_indexed(dir)) {
-               ret = ocfs2_find_dir_space_dx(osb, dir, parent_fe_bh, name,
-                                             namelen, lookup);
-               if (ret) {
+               ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
+                                                     name, namelen, lookup);
+               if (ret)
                        mlog_errno(ret);
-                       goto out;
-               }
-
-               /*
-                * We intentionally fall through so that the unindexed
-                * tree can also be prepared.
-                */
+               goto out;
        }
 
        if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
@@ -4009,7 +4475,7 @@ static int ocfs2_dx_dir_remove_index(struct inode *dir,
                goto out_unlock;
        }
 
-       ret = ocfs2_journal_access_di(handle, dir, di_bh,
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
                                      OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
@@ -4072,7 +4538,7 @@ int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
        if (ocfs2_dx_root_inline(dx_root))
                goto remove_index;
 
-       ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
+       ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
 
        /* XXX: What if dr_clusters is too large? */
        while (le32_to_cpu(dx_root->dr_clusters)) {
@@ -4105,7 +4571,7 @@ remove_index:
                goto out;
        }
 
-       ocfs2_remove_from_cache(dir, dx_root_bh);
+       ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
 out:
        ocfs2_schedule_truncate_log_flush(osb, 1);
        ocfs2_run_deallocs(osb, &dealloc);