nfsd4: support putpubfh operation
[safe/jmp/linux-2.6] / fs / ocfs2 / dir.c
index 7453b70..f2c4098 100644 (file)
@@ -40,6 +40,7 @@
 #include <linux/types.h>
 #include <linux/slab.h>
 #include <linux/highmem.h>
+#include <linux/quotaops.h>
 
 #define MLOG_MASK_PREFIX ML_NAMEI
 #include <cluster/masklog.h>
@@ -47,6 +48,7 @@
 #include "ocfs2.h"
 
 #include "alloc.h"
+#include "blockcheck.h"
 #include "dir.h"
 #include "dlmglue.h"
 #include "extent_map.h"
@@ -83,6 +85,74 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
                               struct buffer_head **new_bh);
 
 /*
+ * 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)
+{
+       if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
+               return 0;
+
+       return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
+}
+
+static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
+{
+       return ocfs2_meta_ecc(osb);
+}
+
+static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
+{
+       return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
+}
+
+#define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
+
+/* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
+ * them more consistent? */
+struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
+                                                           void *data)
+{
+       char *p = data;
+
+       p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
+       return (struct ocfs2_dir_block_trailer *)p;
+}
+
+/*
+ * XXX: This is executed once on every dirent. We should consider optimizing
+ * it.
+ */
+static int ocfs2_skip_dir_trailer(struct inode *dir,
+                                 struct ocfs2_dir_entry *de,
+                                 unsigned long offset,
+                                 unsigned long blklen)
+{
+       unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
+
+       if (!ocfs2_dir_has_trailer(dir))
+               return 0;
+
+       if (offset != toff)
+               return 0;
+
+       return 1;
+}
+
+static void ocfs2_init_dir_trailer(struct inode *inode,
+                                  struct buffer_head *bh)
+{
+       struct ocfs2_dir_block_trailer *trailer;
+
+       trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
+       strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
+       trailer->db_compat_rec_len =
+                       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);
+}
+
+/*
  * bh passed here can be an inode block or a dir data block, depending
  * on the inode inline data flag.
  */
@@ -188,8 +258,7 @@ static struct buffer_head *ocfs2_find_entry_id(const char *name,
        struct ocfs2_dinode *di;
        struct ocfs2_inline_data *data;
 
-       ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
-                              &di_bh, OCFS2_BH_CACHED, dir);
+       ret = ocfs2_read_inode_block(dir, &di_bh);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -208,9 +277,111 @@ out:
        return NULL;
 }
 
-struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
-                                       struct inode *dir,
-                                       struct ocfs2_dir_entry **res_dir)
+static int ocfs2_validate_dir_block(struct super_block *sb,
+                                   struct buffer_head *bh)
+{
+       int rc;
+       struct ocfs2_dir_block_trailer *trailer =
+               ocfs2_trailer_from_bh(bh, sb);
+
+
+       /*
+        * We don't validate dirents here, that's handled
+        * in-place when the code walks them.
+        */
+       mlog(0, "Validating dirblock %llu\n",
+            (unsigned long long)bh->b_blocknr);
+
+       BUG_ON(!buffer_uptodate(bh));
+
+       /*
+        * If the ecc fails, we return the error but otherwise
+        * leave the filesystem running.  We know any error is
+        * local to this block.
+        *
+        * Note that we are safe to call this even if the directory
+        * doesn't have a trailer.  Filesystems without metaecc will do
+        * nothing, and filesystems with it will have one.
+        */
+       rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
+       if (rc)
+               mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
+                    (unsigned long long)bh->b_blocknr);
+
+       return rc;
+}
+
+/*
+ * This function forces all errors to -EIO for consistency with its
+ * predecessor, ocfs2_bread().  We haven't audited what returning the
+ * real error codes would do to callers.  We log the real codes with
+ * mlog_errno() before we squash them.
+ */
+static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
+                               struct buffer_head **bh, int flags)
+{
+       int rc = 0;
+       struct buffer_head *tmp = *bh;
+       struct ocfs2_dir_block_trailer *trailer;
+
+       rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
+                                   ocfs2_validate_dir_block);
+       if (rc) {
+               mlog_errno(rc);
+               goto out;
+       }
+
+       /*
+        * We check the trailer here rather than in
+        * ocfs2_validate_dir_block() because that function doesn't have
+        * the inode to test.
+        */
+       if (!(flags & OCFS2_BH_READAHEAD) &&
+           ocfs2_dir_has_trailer(inode)) {
+               trailer = ocfs2_trailer_from_bh(tmp, inode->i_sb);
+               if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
+                       rc = -EINVAL;
+                       ocfs2_error(inode->i_sb,
+                                   "Invalid dirblock #%llu: "
+                                   "signature = %.*s\n",
+                                   (unsigned long long)tmp->b_blocknr, 7,
+                                   trailer->db_signature);
+                       goto out;
+               }
+               if (le64_to_cpu(trailer->db_blkno) != tmp->b_blocknr) {
+                       rc = -EINVAL;
+                       ocfs2_error(inode->i_sb,
+                                   "Directory block #%llu has an invalid "
+                                   "db_blkno of %llu",
+                                   (unsigned long long)tmp->b_blocknr,
+                                   (unsigned long long)le64_to_cpu(trailer->db_blkno));
+                       goto out;
+               }
+               if (le64_to_cpu(trailer->db_parent_dinode) !=
+                   OCFS2_I(inode)->ip_blkno) {
+                       rc = -EINVAL;
+                       ocfs2_error(inode->i_sb,
+                                   "Directory block #%llu on dinode "
+                                   "#%llu has an invalid parent_dinode "
+                                   "of %llu",
+                                   (unsigned long long)tmp->b_blocknr,
+                                   (unsigned long long)OCFS2_I(inode)->ip_blkno,
+                                   (unsigned long long)le64_to_cpu(trailer->db_blkno));
+                       goto out;
+               }
+       }
+
+       /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
+       if (!*bh)
+               *bh = tmp;
+
+out:
+       return rc ? -EIO : 0;
+}
+
+static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
+                                              struct inode *dir,
+                                              struct ocfs2_dir_entry **res_dir)
 {
        struct super_block *sb;
        struct buffer_head *bh_use[NAMEI_RA_SIZE];
@@ -254,20 +425,21 @@ restart:
                                }
                                num++;
 
-                               bh = ocfs2_bread(dir, b++, &err, 1);
+                               bh = NULL;
+                               err = ocfs2_read_dir_block(dir, b++, &bh,
+                                                          OCFS2_BH_READAHEAD);
                                bh_use[ra_max] = bh;
                        }
                }
                if ((bh = bh_use[ra_ptr++]) == NULL)
                        goto next;
-               wait_on_buffer(bh);
-               if (!buffer_uptodate(bh)) {
-                       /* read error, skip block & hope for the best */
+               if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
+                       /* read error, skip block & hope for the best.
+                        * ocfs2_read_dir_block() has released the bh. */
                        ocfs2_error(dir->i_sb, "reading directory %llu, "
                                    "offset %lu\n",
                                    (unsigned long long)OCFS2_I(dir)->ip_blkno,
                                    block);
-                       brelse(bh);
                        goto next;
                }
                i = ocfs2_search_dirblock(bh, dir, name, namelen,
@@ -340,14 +512,18 @@ int ocfs2_update_entry(struct inode *dir, handle_t *handle,
                       struct inode *new_entry_inode)
 {
        int ret;
+       ocfs2_journal_access_func access = ocfs2_journal_access_db;
 
        /*
         * The same code works fine for both inline-data and extent
-        * based directories, so no need to split this up.
+        * based directories, so no need to split this up.  The only
+        * difference is the journal_access function.
         */
 
-       ret = ocfs2_journal_access(handle, dir, de_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       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);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -369,9 +545,13 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
 {
        struct ocfs2_dir_entry *de, *pde;
        int i, status = -ENOENT;
+       ocfs2_journal_access_func access = ocfs2_journal_access_db;
 
        mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
 
+       if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
+               access = ocfs2_journal_access_di;
+
        i = 0;
        pde = NULL;
        de = (struct ocfs2_dir_entry *) first_de;
@@ -382,17 +562,16 @@ static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
                        goto bail;
                }
                if (de == de_del)  {
-                       status = ocfs2_journal_access(handle, dir, bh,
-                                                     OCFS2_JOURNAL_ACCESS_WRITE);
+                       status = access(handle, dir, bh,
+                                       OCFS2_JOURNAL_ACCESS_WRITE);
                        if (status < 0) {
                                status = -EIO;
                                mlog_errno(status);
                                goto bail;
                        }
                        if (pde)
-                               pde->rec_len =
-                                       cpu_to_le16(le16_to_cpu(pde->rec_len) +
-                                                   le16_to_cpu(de->rec_len));
+                               le16_add_cpu(&pde->rec_len,
+                                               le16_to_cpu(de->rec_len));
                        else
                                de->inode = 0;
                        dir->i_version++;
@@ -418,8 +597,7 @@ static inline int ocfs2_delete_entry_id(handle_t *handle,
        struct ocfs2_dinode *di;
        struct ocfs2_inline_data *data;
 
-       ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
-                              &di_bh, OCFS2_BH_CACHED, dir);
+       ret = ocfs2_read_inode_block(dir, &di_bh);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -537,6 +715,16 @@ int __ocfs2_add_entry(handle_t *handle,
                        goto bail;
                }
 
+               /* We're guaranteed that we should have space, so we
+                * can't possibly have hit the trailer...right? */
+               mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
+                               "Hit dir trailer trying to insert %.*s "
+                               "(namelen %d) into directory %llu.  "
+                               "offset is %lu, trailer offset is %d\n",
+                               namelen, name, namelen,
+                               (unsigned long long)parent_fe_bh->b_blocknr,
+                               offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
+
                if (ocfs2_dirent_would_fit(de, rec_len)) {
                        dir->i_mtime = dir->i_ctime = CURRENT_TIME;
                        retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
@@ -545,8 +733,14 @@ int __ocfs2_add_entry(handle_t *handle,
                                goto bail;
                        }
 
-                       status = ocfs2_journal_access(handle, dir, insert_bh,
-                                                     OCFS2_JOURNAL_ACCESS_WRITE);
+                       if (insert_bh == parent_fe_bh)
+                               status = ocfs2_journal_access_di(handle, dir,
+                                                                insert_bh,
+                                                                OCFS2_JOURNAL_ACCESS_WRITE);
+                       else
+                               status = ocfs2_journal_access_db(handle, dir,
+                                                                insert_bh,
+                                                                OCFS2_JOURNAL_ACCESS_WRITE);
                        /* By now the buffer is marked for journaling */
                        offset += le16_to_cpu(de->rec_len);
                        if (le64_to_cpu(de->inode)) {
@@ -572,6 +766,7 @@ int __ocfs2_add_entry(handle_t *handle,
                        retval = 0;
                        goto bail;
                }
+
                offset += le16_to_cpu(de->rec_len);
                de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
        }
@@ -586,7 +781,7 @@ bail:
 }
 
 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
-                                   unsigned long *f_version,
+                                   u64 *f_version,
                                    loff_t *f_pos, void *priv,
                                    filldir_t filldir, int *filldir_err)
 {
@@ -597,8 +792,7 @@ static int ocfs2_dir_foreach_blk_id(struct inode *inode,
        struct ocfs2_inline_data *data;
        struct ocfs2_dir_entry *de;
 
-       ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
-                              &di_bh, OCFS2_BH_CACHED, inode);
+       ret = ocfs2_read_inode_block(inode, &di_bh);
        if (ret) {
                mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
                     (unsigned long long)OCFS2_I(inode)->ip_blkno);
@@ -648,7 +842,7 @@ revalidate:
                         * not the directory has been modified
                         * during the copy operation.
                         */
-                       unsigned long version = *f_version;
+                       u64 version = *f_version;
                        unsigned char d_type = DT_UNKNOWN;
 
                        if (de->file_type < OCFS2_FT_MAX)
@@ -677,7 +871,7 @@ out:
 }
 
 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
-                                   unsigned long *f_version,
+                                   u64 *f_version,
                                    loff_t *f_pos, void *priv,
                                    filldir_t filldir, int *filldir_err)
 {
@@ -686,7 +880,6 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
        int i, stored;
        struct buffer_head * bh, * tmp;
        struct ocfs2_dir_entry * de;
-       int err;
        struct super_block * sb = inode->i_sb;
        unsigned int ra_sectors = 16;
 
@@ -697,12 +890,8 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
 
        while (!error && !stored && *f_pos < i_size_read(inode)) {
                blk = (*f_pos) >> sb->s_blocksize_bits;
-               bh = ocfs2_bread(inode, blk, &err, 0);
-               if (!bh) {
-                       mlog(ML_ERROR,
-                            "directory #%llu contains a hole at offset %lld\n",
-                            (unsigned long long)OCFS2_I(inode)->ip_blkno,
-                            *f_pos);
+               if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
+                       /* Skip the corrupt dirblock and keep trying */
                        *f_pos += sb->s_blocksize - offset;
                        continue;
                }
@@ -716,8 +905,9 @@ static int ocfs2_dir_foreach_blk_el(struct inode *inode,
                    || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
                        for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
                             i > 0; i--) {
-                               tmp = ocfs2_bread(inode, ++blk, &err, 1);
-                               if (tmp)
+                               tmp = NULL;
+                               if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
+                                                         OCFS2_BH_READAHEAD))
                                        brelse(tmp);
                        }
                        last_ra_blk = blk;
@@ -791,6 +981,7 @@ revalidate:
                }
                offset = 0;
                brelse(bh);
+               bh = NULL;
        }
 
        stored = 0;
@@ -798,7 +989,7 @@ out:
        return stored;
 }
 
-static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
+static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
                                 loff_t *f_pos, void *priv, filldir_t filldir,
                                 int *filldir_err)
 {
@@ -818,7 +1009,7 @@ int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
                      filldir_t filldir)
 {
        int ret = 0, filldir_err = 0;
-       unsigned long version = inode->i_version;
+       u64 version = inode->i_version;
 
        while (*f_pos < i_size_read(inode)) {
                ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
@@ -846,14 +1037,14 @@ int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
        mlog_entry("dirino=%llu\n",
                   (unsigned long long)OCFS2_I(inode)->ip_blkno);
 
-       error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
+       error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
        if (lock_level && error >= 0) {
                /* We release EX lock which used to update atime
                 * and get PR lock again to reduce contention
                 * on commonly accessed directories. */
-               ocfs2_meta_unlock(inode, 1);
+               ocfs2_inode_unlock(inode, 1);
                lock_level = 0;
-               error = ocfs2_meta_lock(inode, NULL, 0);
+               error = ocfs2_inode_lock(inode, NULL, 0);
        }
        if (error < 0) {
                if (error != -ENOENT)
@@ -865,7 +1056,7 @@ int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
        error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
                                      dirent, filldir, NULL);
 
-       ocfs2_meta_unlock(inode, lock_level);
+       ocfs2_inode_unlock(inode, lock_level);
 
 bail_nolock:
        mlog_exit(error);
@@ -900,10 +1091,8 @@ int ocfs2_find_files_on_disk(const char *name,
 leave:
        if (status < 0) {
                *dirent = NULL;
-               if (*dirent_bh) {
-                       brelse(*dirent_bh);
-                       *dirent_bh = NULL;
-               }
+               brelse(*dirent_bh);
+               *dirent_bh = NULL;
        }
 
        mlog_exit(status);
@@ -952,8 +1141,7 @@ int ocfs2_check_dir_for_entry(struct inode *dir,
 
        ret = 0;
 bail:
-       if (dirent_bh)
-               brelse(dirent_bh);
+       brelse(dirent_bh);
 
        mlog_exit(ret);
        return ret;
@@ -1016,9 +1204,15 @@ int ocfs2_empty_dir(struct inode *inode)
        return !priv.seen_other;
 }
 
-static void ocfs2_fill_initial_dirents(struct inode *inode,
-                                      struct inode *parent,
-                                      char *start, unsigned int size)
+/*
+ * Fills "." and ".." dirents in a new directory block. Returns dirent for
+ * "..", which might be used during creation of a directory with a trailing
+ * header. It is otherwise safe to ignore the return code.
+ */
+static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
+                                                         struct inode *parent,
+                                                         char *start,
+                                                         unsigned int size)
 {
        struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
 
@@ -1035,6 +1229,8 @@ static void ocfs2_fill_initial_dirents(struct inode *inode,
        de->name_len = 2;
        strcpy(de->name, "..");
        ocfs2_set_de_type(de, S_IFDIR);
+
+       return de;
 }
 
 /*
@@ -1052,8 +1248,8 @@ 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(handle, inode, di_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_journal_access_di(handle, inode, di_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out;
@@ -1087,10 +1283,15 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
                                 struct ocfs2_alloc_context *data_ac)
 {
        int status;
+       unsigned int size = osb->sb->s_blocksize;
        struct buffer_head *new_bh = NULL;
+       struct ocfs2_dir_entry *de;
 
        mlog_entry_void();
 
+       if (ocfs2_supports_dir_trailer(osb))
+               size = ocfs2_dir_trailer_blk_off(parent->i_sb);
+
        status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
                                     data_ac, NULL, &new_bh);
        if (status < 0) {
@@ -1100,16 +1301,17 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
 
        ocfs2_set_new_buffer_uptodate(inode, new_bh);
 
-       status = ocfs2_journal_access(handle, inode, new_bh,
-                                     OCFS2_JOURNAL_ACCESS_CREATE);
+       status = ocfs2_journal_access_db(handle, inode, new_bh,
+                                        OCFS2_JOURNAL_ACCESS_CREATE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
        memset(new_bh->b_data, 0, osb->sb->s_blocksize);
 
-       ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
-                                  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);
 
        status = ocfs2_journal_dirty(handle, new_bh);
        if (status < 0) {
@@ -1128,8 +1330,7 @@ static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
 
        status = 0;
 bail:
-       if (new_bh)
-               brelse(new_bh);
+       brelse(new_bh);
 
        mlog_exit(status);
        return status;
@@ -1151,13 +1352,27 @@ int ocfs2_fill_new_dir(struct ocfs2_super *osb,
                                     data_ac);
 }
 
+/*
+ * Expand rec_len of the rightmost dirent in a directory block so that it
+ * contains the end of our valid space for dirents. We do this during
+ * 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.
+ *
+ * We add the dir trailer if this filesystem wants it.
+ */
 static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
-                                    unsigned int new_size)
+                                    struct super_block *sb)
 {
        struct ocfs2_dir_entry *de;
        struct ocfs2_dir_entry *prev_de;
        char *de_buf, *limit;
-       unsigned int bytes = new_size - old_size;
+       unsigned int new_size = sb->s_blocksize;
+       unsigned int bytes;
+
+       if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
+               new_size = ocfs2_dir_trailer_blk_off(sb);
+
+       bytes = new_size - old_size;
 
        limit = start + old_size;
        de_buf = start;
@@ -1183,9 +1398,9 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                                   unsigned int blocks_wanted,
                                   struct buffer_head **first_block_bh)
 {
-       int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
        u32 alloc, bit_off, len;
        struct super_block *sb = dir->i_sb;
+       int ret, credits = ocfs2_inline_to_extents_credits(sb);
        u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
        struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
        struct ocfs2_inode_info *oi = OCFS2_I(dir);
@@ -1193,6 +1408,10 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        struct buffer_head *dirdata_bh = NULL;
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
        handle_t *handle;
+       struct ocfs2_extent_tree et;
+       int did_quota = 0;
+
+       ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
 
        alloc = ocfs2_clusters_for_bytes(sb, bytes);
 
@@ -1215,7 +1434,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        down_write(&oi->ip_alloc_sem);
 
        /*
-        * Prepare for worst case allocation scenario of two seperate
+        * Prepare for worst case allocation scenario of two separate
         * extents.
         */
        if (alloc == 2)
@@ -1228,6 +1447,12 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
                goto out_sem;
        }
 
+       if (vfs_dq_alloc_space_nodirty(dir,
+                               ocfs2_clusters_to_bytes(osb->sb, alloc))) {
+               ret = -EDQUOT;
+               goto out_commit;
+       }
+       did_quota = 1;
        /*
         * Try to claim as many clusters as the bitmap can give though
         * if we only get one now, that's enough to continue. The rest
@@ -1254,8 +1479,8 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
 
        ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
 
-       ret = ocfs2_journal_access(handle, dir, dirdata_bh,
-                                  OCFS2_JOURNAL_ACCESS_CREATE);
+       ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
+                                     OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -1264,8 +1489,9 @@ 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->s_blocksize);
+       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);
 
        ret = ocfs2_journal_dirty(handle, dirdata_bh);
        if (ret) {
@@ -1281,8 +1507,8 @@ 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(handle, dir, di_bh,
-                                  OCFS2_JOURNAL_ACCESS_CREATE);
+       ret = ocfs2_journal_access_di(handle, dir, di_bh,
+                                     OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -1301,19 +1527,24 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        di->i_size = cpu_to_le64(sb->s_blocksize);
        di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
        di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
-       dir->i_blocks = ocfs2_inode_sector_count(dir);
 
        /*
         * 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, di_bh, 0, blkno, len, 0,
-                                 NULL);
+       ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
+                                 0, NULL);
        if (ret) {
                mlog_errno(ret);
-               goto out;
+               goto out_commit;
        }
 
+       /*
+        * Set i_blocks after the extent insert for the most up to
+        * date ip_clusters value.
+        */
+       dir->i_blocks = ocfs2_inode_sector_count(dir);
+
        ret = ocfs2_journal_dirty(handle, di_bh);
        if (ret) {
                mlog_errno(ret);
@@ -1333,11 +1564,11 @@ 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, di_bh, 1, blkno,
-                                         len, 0, NULL);
+               ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
+                                         blkno, len, 0, NULL);
                if (ret) {
                        mlog_errno(ret);
-                       goto out;
+                       goto out_commit;
                }
        }
 
@@ -1345,6 +1576,9 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
        dirdata_bh = NULL;
 
 out_commit:
+       if (ret < 0 && did_quota)
+               vfs_dq_free_space_nodirty(dir,
+                       ocfs2_clusters_to_bytes(osb->sb, 2));
        ocfs2_commit_trans(osb, handle);
 
 out_sem:
@@ -1369,7 +1603,7 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
                               struct buffer_head **new_bh)
 {
        int status;
-       int extend;
+       int extend, did_quota = 0;
        u64 p_blkno, v_blkno;
 
        spin_lock(&OCFS2_I(dir)->ip_lock);
@@ -1379,9 +1613,16 @@ static int ocfs2_do_extend_dir(struct super_block *sb,
        if (extend) {
                u32 offset = OCFS2_I(dir)->ip_clusters;
 
-               status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
-                                                   1, 0, parent_fe_bh, handle,
-                                                   data_ac, meta_ac, NULL);
+               if (vfs_dq_alloc_space_nodirty(dir,
+                                       ocfs2_clusters_to_bytes(sb, 1))) {
+                       status = -EDQUOT;
+                       goto bail;
+               }
+               did_quota = 1;
+
+               status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
+                                             1, 0, parent_fe_bh, handle,
+                                             data_ac, meta_ac, NULL);
                BUG_ON(status == -EAGAIN);
                if (status < 0) {
                        mlog_errno(status);
@@ -1404,6 +1645,8 @@ 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));
        mlog_exit(status);
        return status;
 }
@@ -1426,12 +1669,14 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
        int credits, num_free_extents, drop_alloc_sem = 0;
        loff_t dir_i_size;
        struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
+       struct ocfs2_extent_list *el = &fe->id2.i_list;
        struct ocfs2_alloc_context *data_ac = NULL;
        struct ocfs2_alloc_context *meta_ac = NULL;
        handle_t *handle = NULL;
        struct buffer_head *new_bh = NULL;
        struct ocfs2_dir_entry * de;
        struct super_block *sb = osb->sb;
+       struct ocfs2_extent_tree et;
 
        mlog_entry_void();
 
@@ -1475,7 +1720,8 @@ 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);
-               num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
+               ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
+               num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
                if (num_free_extents < 0) {
                        status = num_free_extents;
                        mlog_errno(status);
@@ -1483,7 +1729,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                }
 
                if (!num_free_extents) {
-                       status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
+                       status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
                        if (status < 0) {
                                if (status != -ENOSPC)
                                        mlog_errno(status);
@@ -1498,7 +1744,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
                        goto bail;
                }
 
-               credits = ocfs2_calc_extend_credits(sb, fe, 1);
+               credits = ocfs2_calc_extend_credits(sb, el, 1);
        } else {
                spin_unlock(&OCFS2_I(dir)->ip_lock);
                credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
@@ -1525,16 +1771,22 @@ do_extend:
 
        ocfs2_set_new_buffer_uptodate(dir, new_bh);
 
-       status = ocfs2_journal_access(handle, dir, new_bh,
-                                     OCFS2_JOURNAL_ACCESS_CREATE);
+       status = ocfs2_journal_access_db(handle, dir, new_bh,
+                                        OCFS2_JOURNAL_ACCESS_CREATE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
        memset(new_bh->b_data, 0, sb->s_blocksize);
+
        de = (struct ocfs2_dir_entry *) new_bh->b_data;
        de->inode = 0;
-       de->rec_len = cpu_to_le16(sb->s_blocksize);
+       if (ocfs2_dir_has_trailer(dir)) {
+               de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
+               ocfs2_init_dir_trailer(dir, new_bh);
+       } else {
+               de->rec_len = cpu_to_le16(sb->s_blocksize);
+       }
        status = ocfs2_journal_dirty(handle, new_bh);
        if (status < 0) {
                mlog_errno(status);
@@ -1564,8 +1816,7 @@ bail:
        if (meta_ac)
                ocfs2_free_alloc_context(meta_ac);
 
-       if (new_bh)
-               brelse(new_bh);
+       brelse(new_bh);
 
        mlog_exit(status);
        return status;
@@ -1577,11 +1828,21 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
                                   unsigned int *blocks_wanted)
 {
        int ret;
+       struct super_block *sb = dir->i_sb;
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
        struct ocfs2_dir_entry *de, *last_de = NULL;
        char *de_buf, *limit;
        unsigned long offset = 0;
-       unsigned int rec_len, new_rec_len;
+       unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
+
+       /*
+        * 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)))
+               free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
+       else
+               free_space = dir->i_sb->s_blocksize - i_size_read(dir);
 
        de_buf = di->id2.i_data.id_data;
        limit = de_buf + i_size_read(dir);
@@ -1598,6 +1859,11 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
                        ret = -EEXIST;
                        goto out;
                }
+               /*
+                * No need to check for a trailing dirent record here as
+                * they're not used for inline dirs.
+                */
+
                if (ocfs2_dirent_would_fit(de, rec_len)) {
                        /* Ok, we found a spot. Return this bh and let
                         * the caller actually fill it in. */
@@ -1618,7 +1884,7 @@ static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
         * dirent can be found.
         */
        *blocks_wanted = 1;
-       new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
+       new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
        if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
                *blocks_wanted = 2;
 
@@ -1636,9 +1902,10 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
        struct ocfs2_dir_entry *de;
        struct super_block *sb = dir->i_sb;
        int status;
+       int blocksize = dir->i_sb->s_blocksize;
 
-       bh = ocfs2_bread(dir, 0, &status, 0);
-       if (!bh) {
+       status = ocfs2_read_dir_block(dir, 0, &bh, 0);
+       if (status) {
                mlog_errno(status);
                goto bail;
        }
@@ -1659,11 +1926,10 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
                                status = -ENOSPC;
                                goto bail;
                        }
-                       bh = ocfs2_bread(dir,
-                                        offset >> sb->s_blocksize_bits,
-                                        &status,
-                                        0);
-                       if (!bh) {
+                       status = ocfs2_read_dir_block(dir,
+                                            offset >> sb->s_blocksize_bits,
+                                            &bh, 0);
+                       if (status) {
                                mlog_errno(status);
                                goto bail;
                        }
@@ -1678,6 +1944,11 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
                        status = -EEXIST;
                        goto bail;
                }
+
+               if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
+                                          blocksize))
+                       goto next;
+
                if (ocfs2_dirent_would_fit(de, rec_len)) {
                        /* Ok, we found a spot. Return this bh and let
                         * the caller actually fill it in. */
@@ -1686,14 +1957,14 @@ static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
                        status = 0;
                        goto bail;
                }
+next:
                offset += le16_to_cpu(de->rec_len);
                de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
        }
 
        status = 0;
 bail:
-       if (bh)
-               brelse(bh);
+       brelse(bh);
 
        mlog_exit(status);
        return status;
@@ -1752,7 +2023,6 @@ int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
        *ret_de_bh = bh;
        bh = NULL;
 out:
-       if (bh)
-               brelse(bh);
+       brelse(bh);
        return ret;
 }