tunnels: fix netns vs proto registration ordering
[safe/jmp/linux-2.6] / fs / ocfs2 / suballoc.c
index 2a817bc..c30b644 100644 (file)
@@ -35,6 +35,7 @@
 #include "ocfs2.h"
 
 #include "alloc.h"
+#include "blockcheck.h"
 #include "dlmglue.h"
 #include "inode.h"
 #include "journal.h"
@@ -47,7 +48,8 @@
 #include "buffer_head_io.h"
 
 #define NOT_ALLOC_NEW_GROUP            0
-#define ALLOC_NEW_GROUP                        1
+#define ALLOC_NEW_GROUP                        0x1
+#define ALLOC_GROUPS_FROM_GLOBAL       0x2
 
 #define OCFS2_MAX_INODES_TO_STEAL      1024
 
@@ -62,15 +64,20 @@ static int ocfs2_block_group_fill(handle_t *handle,
                                  struct ocfs2_chain_list *cl);
 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
                                   struct inode *alloc_inode,
-                                  struct buffer_head *bh);
+                                  struct buffer_head *bh,
+                                  u64 max_block,
+                                  u64 *last_alloc_group,
+                                  int flags);
 
 static int ocfs2_cluster_group_search(struct inode *inode,
                                      struct buffer_head *group_bh,
                                      u32 bits_wanted, u32 min_bits,
+                                     u64 max_block,
                                      u16 *bit_off, u16 *bits_found);
 static int ocfs2_block_group_search(struct inode *inode,
                                    struct buffer_head *group_bh,
                                    u32 bits_wanted, u32 min_bits,
+                                   u64 max_block,
                                    u16 *bit_off, u16 *bits_found);
 static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
                                     struct ocfs2_alloc_context *ac,
@@ -110,6 +117,10 @@ static inline void ocfs2_block_to_cluster_group(struct inode *inode,
                                                u64 data_blkno,
                                                u64 *bg_blkno,
                                                u16 *bg_bit_off);
+static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
+                                            u32 bits_wanted, u64 max_block,
+                                            int flags,
+                                            struct ocfs2_alloc_context **ac);
 
 void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
 {
@@ -124,10 +135,8 @@ void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
                iput(inode);
                ac->ac_inode = NULL;
        }
-       if (ac->ac_bh) {
-               brelse(ac->ac_bh);
-               ac->ac_bh = NULL;
-       }
+       brelse(ac->ac_bh);
+       ac->ac_bh = NULL;
 }
 
 void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
@@ -141,62 +150,183 @@ static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
        return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
 }
 
-/* somewhat more expensive than our other checks, so use sparingly. */
-int ocfs2_check_group_descriptor(struct super_block *sb,
-                                struct ocfs2_dinode *di,
-                                struct ocfs2_group_desc *gd)
+#define do_error(fmt, ...)                                             \
+       do{                                                             \
+               if (clean_error)                                        \
+                       mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__);        \
+               else                                                    \
+                       ocfs2_error(sb, fmt, ##__VA_ARGS__);            \
+       } while (0)
+
+static int ocfs2_validate_gd_self(struct super_block *sb,
+                                 struct buffer_head *bh,
+                                 int clean_error)
 {
-       unsigned int max_bits;
+       struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
 
        if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
-               return -EIO;
+               do_error("Group descriptor #%llu has bad signature %.*s",
+                        (unsigned long long)bh->b_blocknr, 7,
+                        gd->bg_signature);
+               return -EINVAL;
+       }
+
+       if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
+               do_error("Group descriptor #%llu has an invalid bg_blkno "
+                        "of %llu",
+                        (unsigned long long)bh->b_blocknr,
+                        (unsigned long long)le64_to_cpu(gd->bg_blkno));
+               return -EINVAL;
+       }
+
+       if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
+               do_error("Group descriptor #%llu has an invalid "
+                        "fs_generation of #%u",
+                        (unsigned long long)bh->b_blocknr,
+                        le32_to_cpu(gd->bg_generation));
+               return -EINVAL;
+       }
+
+       if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
+               do_error("Group descriptor #%llu has bit count %u but "
+                        "claims that %u are free",
+                        (unsigned long long)bh->b_blocknr,
+                        le16_to_cpu(gd->bg_bits),
+                        le16_to_cpu(gd->bg_free_bits_count));
+               return -EINVAL;
+       }
+
+       if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
+               do_error("Group descriptor #%llu has bit count %u but "
+                        "max bitmap bits of %u",
+                        (unsigned long long)bh->b_blocknr,
+                        le16_to_cpu(gd->bg_bits),
+                        8 * le16_to_cpu(gd->bg_size));
+               return -EINVAL;
        }
 
+       return 0;
+}
+
+static int ocfs2_validate_gd_parent(struct super_block *sb,
+                                   struct ocfs2_dinode *di,
+                                   struct buffer_head *bh,
+                                   int clean_error)
+{
+       unsigned int max_bits;
+       struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
+
        if (di->i_blkno != gd->bg_parent_dinode) {
-               ocfs2_error(sb, "Group descriptor # %llu has bad parent "
-                           "pointer (%llu, expected %llu)",
-                           (unsigned long long)le64_to_cpu(gd->bg_blkno),
-                           (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
-                           (unsigned long long)le64_to_cpu(di->i_blkno));
-               return -EIO;
+               do_error("Group descriptor #%llu has bad parent "
+                        "pointer (%llu, expected %llu)",
+                        (unsigned long long)bh->b_blocknr,
+                        (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
+                        (unsigned long long)le64_to_cpu(di->i_blkno));
+               return -EINVAL;
        }
 
        max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
        if (le16_to_cpu(gd->bg_bits) > max_bits) {
-               ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
-                           (unsigned long long)le64_to_cpu(gd->bg_blkno),
-                           le16_to_cpu(gd->bg_bits));
-               return -EIO;
+               do_error("Group descriptor #%llu has bit count of %u",
+                        (unsigned long long)bh->b_blocknr,
+                        le16_to_cpu(gd->bg_bits));
+               return -EINVAL;
        }
 
        if (le16_to_cpu(gd->bg_chain) >=
            le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
-               ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
-                           (unsigned long long)le64_to_cpu(gd->bg_blkno),
-                           le16_to_cpu(gd->bg_chain));
-               return -EIO;
+               do_error("Group descriptor #%llu has bad chain %u",
+                        (unsigned long long)bh->b_blocknr,
+                        le16_to_cpu(gd->bg_chain));
+               return -EINVAL;
        }
 
-       if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
-               ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
-                           "claims that %u are free",
-                           (unsigned long long)le64_to_cpu(gd->bg_blkno),
-                           le16_to_cpu(gd->bg_bits),
-                           le16_to_cpu(gd->bg_free_bits_count));
-               return -EIO;
-       }
+       return 0;
+}
 
-       if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
-               ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
-                           "max bitmap bits of %u",
-                           (unsigned long long)le64_to_cpu(gd->bg_blkno),
-                           le16_to_cpu(gd->bg_bits),
-                           8 * le16_to_cpu(gd->bg_size));
-               return -EIO;
+#undef do_error
+
+/*
+ * This version only prints errors.  It does not fail the filesystem, and
+ * exists only for resize.
+ */
+int ocfs2_check_group_descriptor(struct super_block *sb,
+                                struct ocfs2_dinode *di,
+                                struct buffer_head *bh)
+{
+       int rc;
+       struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
+
+       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.
+        */
+       rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
+       if (rc) {
+               mlog(ML_ERROR,
+                    "Checksum failed for group descriptor %llu\n",
+                    (unsigned long long)bh->b_blocknr);
+       } else
+               rc = ocfs2_validate_gd_self(sb, bh, 1);
+       if (!rc)
+               rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
+
+       return rc;
+}
+
+static int ocfs2_validate_group_descriptor(struct super_block *sb,
+                                          struct buffer_head *bh)
+{
+       int rc;
+       struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
+
+       mlog(0, "Validating group descriptor %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.
+        */
+       rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
+       if (rc)
+               return rc;
+
+       /*
+        * Errors after here are fatal.
+        */
+
+       return ocfs2_validate_gd_self(sb, bh, 0);
+}
+
+int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
+                               u64 gd_blkno, struct buffer_head **bh)
+{
+       int rc;
+       struct buffer_head *tmp = *bh;
+
+       rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
+                             ocfs2_validate_group_descriptor);
+       if (rc)
+               goto out;
+
+       rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
+       if (rc) {
+               brelse(tmp);
+               goto out;
        }
 
-       return 0;
+       /* If ocfs2_read_block() got us a new bh, pass it up. */
+       if (!*bh)
+               *bh = tmp;
+
+out:
+       return rc;
 }
 
 static int ocfs2_block_group_fill(handle_t *handle,
@@ -221,10 +351,10 @@ static int ocfs2_block_group_fill(handle_t *handle,
                goto bail;
        }
 
-       status = ocfs2_journal_access(handle,
-                                     alloc_inode,
-                                     bg_bh,
-                                     OCFS2_JOURNAL_ACCESS_CREATE);
+       status = ocfs2_journal_access_gd(handle,
+                                        INODE_CACHE(alloc_inode),
+                                        bg_bh,
+                                        OCFS2_JOURNAL_ACCESS_CREATE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -276,7 +406,10 @@ static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
  */
 static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
                                   struct inode *alloc_inode,
-                                  struct buffer_head *bh)
+                                  struct buffer_head *bh,
+                                  u64 max_block,
+                                  u64 *last_alloc_group,
+                                  int flags)
 {
        int status, credits;
        struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
@@ -294,9 +427,9 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
        mlog_entry_void();
 
        cl = &fe->id2.i_chain;
-       status = ocfs2_reserve_clusters(osb,
-                                       le16_to_cpu(cl->cl_cpg),
-                                       &ac);
+       status = ocfs2_reserve_clusters_with_limit(osb,
+                                                  le16_to_cpu(cl->cl_cpg),
+                                                  max_block, flags, &ac);
        if (status < 0) {
                if (status != -ENOSPC)
                        mlog_errno(status);
@@ -313,6 +446,11 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
                goto bail;
        }
 
+       if (last_alloc_group && *last_alloc_group != 0) {
+               mlog(0, "use old allocation group %llu for block group alloc\n",
+                    (unsigned long long)*last_alloc_group);
+               ac->ac_last_group = *last_alloc_group;
+       }
        status = ocfs2_claim_clusters(osb,
                                      handle,
                                      ac,
@@ -338,7 +476,7 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
                mlog_errno(status);
                goto bail;
        }
-       ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
+       ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
 
        status = ocfs2_block_group_fill(handle,
                                        alloc_inode,
@@ -353,8 +491,8 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
 
        bg = (struct ocfs2_group_desc *) bg_bh->b_data;
 
-       status = ocfs2_journal_access(handle, alloc_inode,
-                                     bh, OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
+                                        bh, OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -387,6 +525,11 @@ static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
        alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
 
        status = 0;
+
+       /* save the new last alloc group so that the caller can cache it. */
+       if (last_alloc_group)
+               *last_alloc_group = ac->ac_last_group;
+
 bail:
        if (handle)
                ocfs2_commit_trans(osb, handle);
@@ -394,8 +537,7 @@ bail:
        if (ac)
                ocfs2_free_alloc_context(ac);
 
-       if (bg_bh)
-               brelse(bg_bh);
+       brelse(bg_bh);
 
        mlog_exit(status);
        return status;
@@ -405,7 +547,8 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
                                       struct ocfs2_alloc_context *ac,
                                       int type,
                                       u32 slot,
-                                      int alloc_new_group)
+                                      u64 *last_alloc_group,
+                                      int flags)
 {
        int status;
        u32 bits_wanted = ac->ac_bits_wanted;
@@ -437,11 +580,11 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
        ac->ac_alloc_slot = slot;
 
        fe = (struct ocfs2_dinode *) bh->b_data;
-       if (!OCFS2_IS_VALID_DINODE(fe)) {
-               OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
-               status = -EIO;
-               goto bail;
-       }
+
+       /* The bh was validated by the inode read inside
+        * ocfs2_inode_lock().  Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
+
        if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
                ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
                            (unsigned long long)le64_to_cpu(fe->i_blkno));
@@ -461,7 +604,7 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
                        goto bail;
                }
 
-               if (alloc_new_group != ALLOC_NEW_GROUP) {
+               if (!(flags & ALLOC_NEW_GROUP)) {
                        mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
                             "and we don't alloc a new group for it.\n",
                             slot, bits_wanted, free_bits);
@@ -469,7 +612,9 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
                        goto bail;
                }
 
-               status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
+               status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
+                                                ac->ac_max_block,
+                                                last_alloc_group, flags);
                if (status < 0) {
                        if (status != -ENOSPC)
                                mlog_errno(status);
@@ -486,16 +631,15 @@ static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
        get_bh(bh);
        ac->ac_bh = bh;
 bail:
-       if (bh)
-               brelse(bh);
+       brelse(bh);
 
        mlog_exit(status);
        return status;
 }
 
-int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
-                              struct ocfs2_extent_list *root_el,
-                              struct ocfs2_alloc_context **ac)
+int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
+                                     int blocks,
+                                     struct ocfs2_alloc_context **ac)
 {
        int status;
        u32 slot;
@@ -507,14 +651,14 @@ int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
                goto bail;
        }
 
-       (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(root_el);
+       (*ac)->ac_bits_wanted = blocks;
        (*ac)->ac_which = OCFS2_AC_USE_META;
        slot = osb->slot_num;
        (*ac)->ac_group_search = ocfs2_block_group_search;
 
        status = ocfs2_reserve_suballoc_bits(osb, (*ac),
                                             EXTENT_ALLOC_SYSTEM_INODE,
-                                            slot, ALLOC_NEW_GROUP);
+                                            slot, NULL, ALLOC_NEW_GROUP);
        if (status < 0) {
                if (status != -ENOSPC)
                        mlog_errno(status);
@@ -532,6 +676,15 @@ bail:
        return status;
 }
 
+int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
+                              struct ocfs2_extent_list *root_el,
+                              struct ocfs2_alloc_context **ac)
+{
+       return ocfs2_reserve_new_metadata_blocks(osb,
+                                       ocfs2_extend_meta_needed(root_el),
+                                       ac);
+}
+
 static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
                                              struct ocfs2_alloc_context *ac)
 {
@@ -551,7 +704,8 @@ static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
 
                status = ocfs2_reserve_suballoc_bits(osb, ac,
                                                     INODE_ALLOC_SYSTEM_INODE,
-                                                    slot, NOT_ALLOC_NEW_GROUP);
+                                                    slot, NULL,
+                                                    NOT_ALLOC_NEW_GROUP);
                if (status >= 0) {
                        ocfs2_set_inode_steal_slot(osb, slot);
                        break;
@@ -568,6 +722,7 @@ int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
 {
        int status;
        s16 slot = ocfs2_get_inode_steal_slot(osb);
+       u64 alloc_group;
 
        *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
        if (!(*ac)) {
@@ -582,6 +737,14 @@ int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
        (*ac)->ac_group_search = ocfs2_block_group_search;
 
        /*
+        * stat(2) can't handle i_ino > 32bits, so we tell the
+        * lower levels not to allocate us a block group past that
+        * limit.  The 'inode64' mount option avoids this behavior.
+        */
+       if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
+               (*ac)->ac_max_block = (u32)~0U;
+
+       /*
         * slot is set when we successfully steal inode from other nodes.
         * It is reset in 3 places:
         * 1. when we flush the truncate log
@@ -595,12 +758,22 @@ int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
                goto inode_steal;
 
        atomic_set(&osb->s_num_inodes_stolen, 0);
+       alloc_group = osb->osb_inode_alloc_group;
        status = ocfs2_reserve_suballoc_bits(osb, *ac,
                                             INODE_ALLOC_SYSTEM_INODE,
-                                            osb->slot_num, ALLOC_NEW_GROUP);
+                                            osb->slot_num,
+                                            &alloc_group,
+                                            ALLOC_NEW_GROUP |
+                                            ALLOC_GROUPS_FROM_GLOBAL);
        if (status >= 0) {
                status = 0;
 
+               spin_lock(&osb->osb_lock);
+               osb->osb_inode_alloc_group = alloc_group;
+               spin_unlock(&osb->osb_lock);
+               mlog(0, "after reservation, new allocation group is "
+                    "%llu\n", (unsigned long long)alloc_group);
+
                /*
                 * Some inodes must be freed by us, so try to allocate
                 * from our own next time.
@@ -647,7 +820,7 @@ int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
 
        status = ocfs2_reserve_suballoc_bits(osb, ac,
                                             GLOBAL_BITMAP_SYSTEM_INODE,
-                                            OCFS2_INVALID_SLOT,
+                                            OCFS2_INVALID_SLOT, NULL,
                                             ALLOC_NEW_GROUP);
        if (status < 0 && status != -ENOSPC) {
                mlog_errno(status);
@@ -661,9 +834,10 @@ bail:
 /* Callers don't need to care which bitmap (local alloc or main) to
  * use so we figure it out for them, but unfortunately this clutters
  * things a bit. */
-int ocfs2_reserve_clusters(struct ocfs2_super *osb,
-                          u32 bits_wanted,
-                          struct ocfs2_alloc_context **ac)
+static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
+                                            u32 bits_wanted, u64 max_block,
+                                            int flags,
+                                            struct ocfs2_alloc_context **ac)
 {
        int status;
 
@@ -677,13 +851,19 @@ int ocfs2_reserve_clusters(struct ocfs2_super *osb,
        }
 
        (*ac)->ac_bits_wanted = bits_wanted;
+       (*ac)->ac_max_block = max_block;
 
        status = -ENOSPC;
-       if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
+       if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
+           ocfs2_alloc_should_use_local(osb, bits_wanted)) {
                status = ocfs2_reserve_local_alloc_bits(osb,
                                                        bits_wanted,
                                                        *ac);
-               if ((status < 0) && (status != -ENOSPC)) {
+               if (status == -EFBIG) {
+                       /* The local alloc window is outside ac_max_block.
+                        * use the main bitmap. */
+                       status = -ENOSPC;
+               } else if ((status < 0) && (status != -ENOSPC)) {
                        mlog_errno(status);
                        goto bail;
                }
@@ -709,6 +889,14 @@ bail:
        return status;
 }
 
+int ocfs2_reserve_clusters(struct ocfs2_super *osb,
+                          u32 bits_wanted,
+                          struct ocfs2_alloc_context **ac)
+{
+       return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
+                                                ALLOC_NEW_GROUP, ac);
+}
+
 /*
  * More or less lifted from ext3. I'll leave their description below:
  *
@@ -735,14 +923,23 @@ static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
                                         int nr)
 {
        struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
+       int ret;
 
        if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
                return 0;
-       if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
+
+       if (!buffer_jbd(bg_bh))
                return 1;
 
+       jbd_lock_bh_state(bg_bh);
        bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
-       return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
+       if (bg)
+               ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
+       else
+               ret = 1;
+       jbd_unlock_bh_state(bg_bh);
+
+       return ret;
 }
 
 static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
@@ -757,10 +954,9 @@ static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
        int offset, start, found, status = 0;
        struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
 
-       if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
-               return -EIO;
-       }
+       /* Callers got this descriptor from
+        * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
 
        found = start = best_offset = best_size = 0;
        bitmap = bg->bg_bitmap;
@@ -825,11 +1021,9 @@ static inline int ocfs2_block_group_set_bits(handle_t *handle,
 
        mlog_entry_void();
 
-       if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
-               status = -EIO;
-               goto bail;
-       }
+       /* All callers get the descriptor via
+        * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
        BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
 
        mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
@@ -838,10 +1032,10 @@ static inline int ocfs2_block_group_set_bits(handle_t *handle,
        if (ocfs2_is_cluster_bitmap(alloc_inode))
                journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
 
-       status = ocfs2_journal_access(handle,
-                                     alloc_inode,
-                                     group_bh,
-                                     journal_type);
+       status = ocfs2_journal_access_gd(handle,
+                                        INODE_CACHE(alloc_inode),
+                                        group_bh,
+                                        journal_type);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -898,21 +1092,10 @@ static int ocfs2_relink_block_group(handle_t *handle,
        struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
        struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
 
-       if (!OCFS2_IS_VALID_DINODE(fe)) {
-               OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
-               status = -EIO;
-               goto out;
-       }
-       if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
-               status = -EIO;
-               goto out;
-       }
-       if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
-               status = -EIO;
-               goto out;
-       }
+       /* The caller got these descriptors from
+        * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
+       BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
 
        mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
             (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
@@ -923,8 +1106,9 @@ static int ocfs2_relink_block_group(handle_t *handle,
        bg_ptr = le64_to_cpu(bg->bg_next_group);
        prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
 
-       status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
+                                        prev_bg_bh,
+                                        OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto out_rollback;
@@ -938,8 +1122,8 @@ static int ocfs2_relink_block_group(handle_t *handle,
                goto out_rollback;
        }
 
-       status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
+                                        bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto out_rollback;
@@ -953,8 +1137,8 @@ static int ocfs2_relink_block_group(handle_t *handle,
                goto out_rollback;
        }
 
-       status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
+                                        fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto out_rollback;
@@ -975,7 +1159,7 @@ out_rollback:
                bg->bg_next_group = cpu_to_le64(bg_ptr);
                prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
        }
-out:
+
        mlog_exit(status);
        return status;
 }
@@ -991,10 +1175,12 @@ static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg
 static int ocfs2_cluster_group_search(struct inode *inode,
                                      struct buffer_head *group_bh,
                                      u32 bits_wanted, u32 min_bits,
+                                     u64 max_block,
                                      u16 *bit_off, u16 *bits_found)
 {
        int search = -ENOSPC;
        int ret;
+       u64 blkoff;
        struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        u16 tmp_off, tmp_found;
@@ -1029,6 +1215,17 @@ static int ocfs2_cluster_group_search(struct inode *inode,
                if (ret)
                        return ret;
 
+               if (max_block) {
+                       blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
+                                                         gd_cluster_off +
+                                                         tmp_off + tmp_found);
+                       mlog(0, "Checking %llu against %llu\n",
+                            (unsigned long long)blkoff,
+                            (unsigned long long)max_block);
+                       if (blkoff > max_block)
+                               return -ENOSPC;
+               }
+
                /* ocfs2_block_group_find_clear_bits() might
                 * return success, but we still want to return
                 * -ENOSPC unless it found the minimum number
@@ -1052,19 +1249,31 @@ static int ocfs2_cluster_group_search(struct inode *inode,
 static int ocfs2_block_group_search(struct inode *inode,
                                    struct buffer_head *group_bh,
                                    u32 bits_wanted, u32 min_bits,
+                                   u64 max_block,
                                    u16 *bit_off, u16 *bits_found)
 {
        int ret = -ENOSPC;
+       u64 blkoff;
        struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
 
        BUG_ON(min_bits != 1);
        BUG_ON(ocfs2_is_cluster_bitmap(inode));
 
-       if (bg->bg_free_bits_count)
+       if (bg->bg_free_bits_count) {
                ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
                                                        group_bh, bits_wanted,
                                                        le16_to_cpu(bg->bg_bits),
                                                        bit_off, bits_found);
+               if (!ret && max_block) {
+                       blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
+                               *bits_found;
+                       mlog(0, "Checking %llu against %llu\n",
+                            (unsigned long long)blkoff,
+                            (unsigned long long)max_block);
+                       if (blkoff > max_block)
+                               ret = -ENOSPC;
+               }
+       }
 
        return ret;
 }
@@ -1080,8 +1289,8 @@ static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
        struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
        struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
 
-       ret = ocfs2_journal_access(handle, inode, di_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret < 0) {
                mlog_errno(ret);
                goto out;
@@ -1112,24 +1321,19 @@ static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
        u16 found;
        struct buffer_head *group_bh = NULL;
        struct ocfs2_group_desc *gd;
+       struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
        struct inode *alloc_inode = ac->ac_inode;
 
-       ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
-                              &group_bh, OCFS2_BH_CACHED, alloc_inode);
+       ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
+                                         &group_bh);
        if (ret < 0) {
                mlog_errno(ret);
                return ret;
        }
 
        gd = (struct ocfs2_group_desc *) group_bh->b_data;
-       if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
-               ret = -EIO;
-               goto out;
-       }
-
        ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
-                                 bit_off, &found);
+                                 ac->ac_max_block, bit_off, &found);
        if (ret < 0) {
                if (ret != -ENOSPC)
                        mlog_errno(ret);
@@ -1184,48 +1388,38 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
             bits_wanted, chain,
             (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
 
-       status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
-                                 le64_to_cpu(cl->cl_recs[chain].c_blkno),
-                                 &group_bh, OCFS2_BH_CACHED, alloc_inode);
+       status = ocfs2_read_group_descriptor(alloc_inode, fe,
+                                            le64_to_cpu(cl->cl_recs[chain].c_blkno),
+                                            &group_bh);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
        bg = (struct ocfs2_group_desc *) group_bh->b_data;
-       status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
-       if (status) {
-               mlog_errno(status);
-               goto bail;
-       }
 
        status = -ENOSPC;
        /* for now, the chain search is a bit simplistic. We just use
         * the 1st group with any empty bits. */
-       while ((status = ac->ac_group_search(alloc_inode, group_bh, bits_wanted,
-                                            min_bits, bit_off, &tmp_bits)) == -ENOSPC) {
+       while ((status = ac->ac_group_search(alloc_inode, group_bh,
+                                            bits_wanted, min_bits,
+                                            ac->ac_max_block, bit_off,
+                                            &tmp_bits)) == -ENOSPC) {
                if (!bg->bg_next_group)
                        break;
 
-               if (prev_group_bh) {
-                       brelse(prev_group_bh);
-                       prev_group_bh = NULL;
-               }
+               brelse(prev_group_bh);
+               prev_group_bh = NULL;
+
                next_group = le64_to_cpu(bg->bg_next_group);
                prev_group_bh = group_bh;
                group_bh = NULL;
-               status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
-                                         next_group, &group_bh,
-                                         OCFS2_BH_CACHED, alloc_inode);
+               status = ocfs2_read_group_descriptor(alloc_inode, fe,
+                                                    next_group, &group_bh);
                if (status < 0) {
                        mlog_errno(status);
                        goto bail;
                }
                bg = (struct ocfs2_group_desc *) group_bh->b_data;
-               status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
-               if (status) {
-                       mlog_errno(status);
-                       goto bail;
-               }
        }
        if (status < 0) {
                if (status != -ENOSPC)
@@ -1267,10 +1461,10 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
 
        /* Ok, claim our bits now: set the info on dinode, chainlist
         * and then the group */
-       status = ocfs2_journal_access(handle,
-                                     alloc_inode,
-                                     ac->ac_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle,
+                                        INODE_CACHE(alloc_inode),
+                                        ac->ac_bh,
+                                        OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -1304,10 +1498,8 @@ static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
        *bg_blkno = le64_to_cpu(bg->bg_blkno);
        *bits_left = le16_to_cpu(bg->bg_free_bits_count);
 bail:
-       if (group_bh)
-               brelse(group_bh);
-       if (prev_group_bh)
-               brelse(prev_group_bh);
+       brelse(group_bh);
+       brelse(prev_group_bh);
 
        mlog_exit(status);
        return status;
@@ -1337,11 +1529,11 @@ static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
        BUG_ON(!ac->ac_bh);
 
        fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
-       if (!OCFS2_IS_VALID_DINODE(fe)) {
-               OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
-               status = -EIO;
-               goto bail;
-       }
+
+       /* The bh was validated by the inode read during
+        * ocfs2_reserve_suballoc_bits().  Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
+
        if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
            le32_to_cpu(fe->id1.bitmap1.i_total)) {
                ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
@@ -1469,8 +1661,41 @@ bail:
        return status;
 }
 
+static void ocfs2_init_inode_ac_group(struct inode *dir,
+                                     struct buffer_head *parent_fe_bh,
+                                     struct ocfs2_alloc_context *ac)
+{
+       struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
+       /*
+        * Try to allocate inodes from some specific group.
+        *
+        * If the parent dir has recorded the last group used in allocation,
+        * cool, use it. Otherwise if we try to allocate new inode from the
+        * same slot the parent dir belongs to, use the same chunk.
+        *
+        * We are very careful here to avoid the mistake of setting
+        * ac_last_group to a group descriptor from a different (unlocked) slot.
+        */
+       if (OCFS2_I(dir)->ip_last_used_group &&
+           OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
+               ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
+       else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
+               ac->ac_last_group = ocfs2_which_suballoc_group(
+                                       le64_to_cpu(fe->i_blkno),
+                                       le16_to_cpu(fe->i_suballoc_bit));
+}
+
+static inline void ocfs2_save_inode_ac_group(struct inode *dir,
+                                            struct ocfs2_alloc_context *ac)
+{
+       OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
+       OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
+}
+
 int ocfs2_claim_new_inode(struct ocfs2_super *osb,
                          handle_t *handle,
+                         struct inode *dir,
+                         struct buffer_head *parent_fe_bh,
                          struct ocfs2_alloc_context *ac,
                          u16 *suballoc_bit,
                          u64 *fe_blkno)
@@ -1486,6 +1711,8 @@ int ocfs2_claim_new_inode(struct ocfs2_super *osb,
        BUG_ON(ac->ac_bits_wanted != 1);
        BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
 
+       ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
+
        status = ocfs2_claim_suballoc_bits(osb,
                                           ac,
                                           handle,
@@ -1504,6 +1731,7 @@ int ocfs2_claim_new_inode(struct ocfs2_super *osb,
 
        *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
        ac->ac_bits_given++;
+       ocfs2_save_inode_ac_group(dir, ac);
        status = 0;
 bail:
        mlog_exit(status);
@@ -1667,40 +1895,49 @@ static inline int ocfs2_block_group_clear_bits(handle_t *handle,
        unsigned int tmp;
        int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
        struct ocfs2_group_desc *undo_bg = NULL;
+       int cluster_bitmap = 0;
 
        mlog_entry_void();
 
-       if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
-               OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
-               status = -EIO;
-               goto bail;
-       }
+       /* The caller got this descriptor from
+        * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
 
        mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
 
        if (ocfs2_is_cluster_bitmap(alloc_inode))
                journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
 
-       status = ocfs2_journal_access(handle, alloc_inode, group_bh,
-                                     journal_type);
+       status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
+                                        group_bh, journal_type);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
 
        if (ocfs2_is_cluster_bitmap(alloc_inode))
-               undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
+               cluster_bitmap = 1;
+
+       if (cluster_bitmap) {
+               jbd_lock_bh_state(group_bh);
+               undo_bg = (struct ocfs2_group_desc *)
+                                       bh2jh(group_bh)->b_committed_data;
+               BUG_ON(!undo_bg);
+       }
 
        tmp = num_bits;
        while(tmp--) {
                ocfs2_clear_bit((bit_off + tmp),
                                (unsigned long *) bg->bg_bitmap);
-               if (ocfs2_is_cluster_bitmap(alloc_inode))
+               if (cluster_bitmap)
                        ocfs2_set_bit(bit_off + tmp,
                                      (unsigned long *) undo_bg->bg_bitmap);
        }
        le16_add_cpu(&bg->bg_free_bits_count, num_bits);
 
+       if (cluster_bitmap)
+               jbd_unlock_bh_state(group_bh);
+
        status = ocfs2_journal_dirty(handle, group_bh);
        if (status < 0)
                mlog_errno(status);
@@ -1720,7 +1957,6 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
 {
        int status = 0;
        u32 tmp_used;
-       struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
        struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
        struct ocfs2_chain_list *cl = &fe->id2.i_chain;
        struct buffer_head *group_bh = NULL;
@@ -1728,30 +1964,26 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
 
        mlog_entry_void();
 
-       if (!OCFS2_IS_VALID_DINODE(fe)) {
-               OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
-               status = -EIO;
-               goto bail;
-       }
+       /* The alloc_bh comes from ocfs2_free_dinode() or
+        * ocfs2_free_clusters().  The callers have all locked the
+        * allocator and gotten alloc_bh from the lock call.  This
+        * validates the dinode buffer.  Any corruption that has happended
+        * is a code bug. */
+       BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
        BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
 
        mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
             (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
             (unsigned long long)bg_blkno, start_bit);
 
-       status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
-                                 alloc_inode);
+       status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
+                                            &group_bh);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
        }
-
        group = (struct ocfs2_group_desc *) group_bh->b_data;
-       status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
-       if (status) {
-               mlog_errno(status);
-               goto bail;
-       }
+
        BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
 
        status = ocfs2_block_group_clear_bits(handle, alloc_inode,
@@ -1762,8 +1994,8 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
                goto bail;
        }
 
-       status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
+                                        alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
        if (status < 0) {
                mlog_errno(status);
                goto bail;
@@ -1781,8 +2013,7 @@ int ocfs2_free_suballoc_bits(handle_t *handle,
        }
 
 bail:
-       if (group_bh)
-               brelse(group_bh);
+       brelse(group_bh);
 
        mlog_exit(status);
        return status;
@@ -1894,3 +2125,248 @@ static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
                       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
        }
 }
+
+/*
+ * For a given allocation, determine which allocators will need to be
+ * accessed, and lock them, reserving the appropriate number of bits.
+ *
+ * Sparse file systems call this from ocfs2_write_begin_nolock()
+ * and ocfs2_allocate_unwritten_extents().
+ *
+ * File systems which don't support holes call this from
+ * ocfs2_extend_allocation().
+ */
+int ocfs2_lock_allocators(struct inode *inode,
+                         struct ocfs2_extent_tree *et,
+                         u32 clusters_to_add, u32 extents_to_split,
+                         struct ocfs2_alloc_context **data_ac,
+                         struct ocfs2_alloc_context **meta_ac)
+{
+       int ret = 0, num_free_extents;
+       unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+
+       *meta_ac = NULL;
+       if (data_ac)
+               *data_ac = NULL;
+
+       BUG_ON(clusters_to_add != 0 && data_ac == NULL);
+
+       num_free_extents = ocfs2_num_free_extents(osb, et);
+       if (num_free_extents < 0) {
+               ret = num_free_extents;
+               mlog_errno(ret);
+               goto out;
+       }
+
+       /*
+        * Sparse allocation file systems need to be more conservative
+        * with reserving room for expansion - the actual allocation
+        * happens while we've got a journal handle open so re-taking
+        * a cluster lock (because we ran out of room for another
+        * extent) will violate ordering rules.
+        *
+        * Most of the time we'll only be seeing this 1 cluster at a time
+        * anyway.
+        *
+        * Always lock for any unwritten extents - we might want to
+        * add blocks during a split.
+        */
+       if (!num_free_extents ||
+           (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
+               ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
+               if (ret < 0) {
+                       if (ret != -ENOSPC)
+                               mlog_errno(ret);
+                       goto out;
+               }
+       }
+
+       if (clusters_to_add == 0)
+               goto out;
+
+       ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
+       if (ret < 0) {
+               if (ret != -ENOSPC)
+                       mlog_errno(ret);
+               goto out;
+       }
+
+out:
+       if (ret) {
+               if (*meta_ac) {
+                       ocfs2_free_alloc_context(*meta_ac);
+                       *meta_ac = NULL;
+               }
+
+               /*
+                * We cannot have an error and a non null *data_ac.
+                */
+       }
+
+       return ret;
+}
+
+/*
+ * Read the inode specified by blkno to get suballoc_slot and
+ * suballoc_bit.
+ */
+static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
+                                      u16 *suballoc_slot, u16 *suballoc_bit)
+{
+       int status;
+       struct buffer_head *inode_bh = NULL;
+       struct ocfs2_dinode *inode_fe;
+
+       mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
+
+       /* dirty read disk */
+       status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
+       if (status < 0) {
+               mlog(ML_ERROR, "read block %llu failed %d\n",
+                    (unsigned long long)blkno, status);
+               goto bail;
+       }
+
+       inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
+       if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
+               mlog(ML_ERROR, "invalid inode %llu requested\n",
+                    (unsigned long long)blkno);
+               status = -EINVAL;
+               goto bail;
+       }
+
+       if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
+           (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
+               mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
+                    (unsigned long long)blkno,
+                    (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
+               status = -EINVAL;
+               goto bail;
+       }
+
+       if (suballoc_slot)
+               *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
+       if (suballoc_bit)
+               *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
+
+bail:
+       brelse(inode_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * test whether bit is SET in allocator bitmap or not.  on success, 0
+ * is returned and *res is 1 for SET; 0 otherwise.  when fails, errno
+ * is returned and *res is meaningless.  Call this after you have
+ * cluster locked against suballoc, or you may get a result based on
+ * non-up2date contents
+ */
+static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
+                                  struct inode *suballoc,
+                                  struct buffer_head *alloc_bh, u64 blkno,
+                                  u16 bit, int *res)
+{
+       struct ocfs2_dinode *alloc_fe;
+       struct ocfs2_group_desc *group;
+       struct buffer_head *group_bh = NULL;
+       u64 bg_blkno;
+       int status;
+
+       mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
+                  (unsigned int)bit);
+
+       alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
+       if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
+               mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
+                    (unsigned int)bit,
+                    ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
+               status = -EINVAL;
+               goto bail;
+       }
+
+       bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
+       status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
+                                            &group_bh);
+       if (status < 0) {
+               mlog(ML_ERROR, "read group %llu failed %d\n",
+                    (unsigned long long)bg_blkno, status);
+               goto bail;
+       }
+
+       group = (struct ocfs2_group_desc *) group_bh->b_data;
+       *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
+
+bail:
+       brelse(group_bh);
+
+       mlog_exit(status);
+       return status;
+}
+
+/*
+ * Test if the bit representing this inode (blkno) is set in the
+ * suballocator.
+ *
+ * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
+ *
+ * In the event of failure, a negative value is returned and *res is
+ * meaningless.
+ *
+ * Callers must make sure to hold nfs_sync_lock to prevent
+ * ocfs2_delete_inode() on another node from accessing the same
+ * suballocator concurrently.
+ */
+int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
+{
+       int status;
+       u16 suballoc_bit = 0, suballoc_slot = 0;
+       struct inode *inode_alloc_inode;
+       struct buffer_head *alloc_bh = NULL;
+
+       mlog_entry("blkno: %llu", (unsigned long long)blkno);
+
+       status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
+                                            &suballoc_bit);
+       if (status < 0) {
+               mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
+               goto bail;
+       }
+
+       inode_alloc_inode =
+               ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
+                                           suballoc_slot);
+       if (!inode_alloc_inode) {
+               /* the error code could be inaccurate, but we are not able to
+                * get the correct one. */
+               status = -EINVAL;
+               mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
+                    (u32)suballoc_slot);
+               goto bail;
+       }
+
+       mutex_lock(&inode_alloc_inode->i_mutex);
+       status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
+       if (status < 0) {
+               mutex_unlock(&inode_alloc_inode->i_mutex);
+               mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
+                    (u32)suballoc_slot, status);
+               goto bail;
+       }
+
+       status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
+                                        blkno, suballoc_bit, res);
+       if (status < 0)
+               mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
+
+       ocfs2_inode_unlock(inode_alloc_inode, 0);
+       mutex_unlock(&inode_alloc_inode->i_mutex);
+
+       iput(inode_alloc_inode);
+       brelse(alloc_bh);
+bail:
+       mlog_exit(status);
+       return status;
+}