]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - fs/ocfs2/xattr.c
ocfs2/xattr: Only set buffer update if it doesn't exist in cache.
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / xattr.c
index 802c41492214ca928cd30448deb972bbbbe6a05f..d8fc714e941576be4484e385c65212d6f6f05521 100644 (file)
@@ -3,25 +3,20 @@
  *
  * xattr.c
  *
- * Copyright (C) 2008 Oracle.  All rights reserved.
+ * Copyright (C) 2004, 2008 Oracle.  All rights reserved.
  *
  * CREDITS:
- * Lots of code in this file is taken from ext3.
+ * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
+ * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * License version 2 as published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 021110-1307, USA.
  */
 
 #include <linux/capability.h>
@@ -66,8 +61,14 @@ struct ocfs2_xattr_def_value_root {
 };
 
 struct ocfs2_xattr_bucket {
-       struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
-       struct ocfs2_xattr_header *xh;
+       /* The inode these xattrs are associated with */
+       struct inode *bu_inode;
+
+       /* The actual buffers that make up the bucket */
+       struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
+
+       /* How many blocks make up one bucket for this filesystem */
+       int bu_blocks;
 };
 
 #define OCFS2_XATTR_ROOT_SIZE  (sizeof(struct ocfs2_xattr_def_value_root))
@@ -83,7 +84,7 @@ struct xattr_handler *ocfs2_xattr_handlers[] = {
        NULL
 };
 
-static struct xattr_handler *ocfs2_xattr_handler_map[] = {
+static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
        [OCFS2_XATTR_INDEX_USER]        = &ocfs2_xattr_user_handler,
        [OCFS2_XATTR_INDEX_TRUSTED]     = &ocfs2_xattr_trusted_handler,
 };
@@ -103,7 +104,7 @@ struct ocfs2_xattr_search {
         */
        struct buffer_head *xattr_bh;
        struct ocfs2_xattr_header *header;
-       struct ocfs2_xattr_bucket bucket;
+       struct ocfs2_xattr_bucket *bucket;
        void *base;
        void *end;
        struct ocfs2_xattr_entry *here;
@@ -116,6 +117,10 @@ static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
                                             int *block_off,
                                             int *new_offset);
 
+static int ocfs2_xattr_block_find(struct inode *inode,
+                                 int name_index,
+                                 const char *name,
+                                 struct ocfs2_xattr_search *xs);
 static int ocfs2_xattr_index_block_find(struct inode *inode,
                                        struct buffer_head *root_bh,
                                        int name_index,
@@ -137,6 +142,149 @@ static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
 static int ocfs2_delete_xattr_index_block(struct inode *inode,
                                          struct buffer_head *xb_bh);
 
+static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
+{
+       return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
+}
+
+static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
+{
+       return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
+}
+
+static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
+{
+       u16 len = sb->s_blocksize -
+                offsetof(struct ocfs2_xattr_header, xh_entries);
+
+       return len / sizeof(struct ocfs2_xattr_entry);
+}
+
+#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
+#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
+#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
+
+static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
+{
+       struct ocfs2_xattr_bucket *bucket;
+       int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
+
+       BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
+
+       bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
+       if (bucket) {
+               bucket->bu_inode = inode;
+               bucket->bu_blocks = blks;
+       }
+
+       return bucket;
+}
+
+static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
+{
+       int i;
+
+       for (i = 0; i < bucket->bu_blocks; i++) {
+               brelse(bucket->bu_bhs[i]);
+               bucket->bu_bhs[i] = NULL;
+       }
+}
+
+static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
+{
+       if (bucket) {
+               ocfs2_xattr_bucket_relse(bucket);
+               bucket->bu_inode = NULL;
+               kfree(bucket);
+       }
+}
+
+/*
+ * A bucket that has never been written to disk doesn't need to be
+ * read.  We just need the buffer_heads.  Don't call this for
+ * buckets that are already on disk.  ocfs2_read_xattr_bucket() initializes
+ * them fully.
+ */
+static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
+                                  u64 xb_blkno)
+{
+       int i, rc = 0;
+
+       for (i = 0; i < bucket->bu_blocks; i++) {
+               bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
+                                             xb_blkno + i);
+               if (!bucket->bu_bhs[i]) {
+                       rc = -EIO;
+                       mlog_errno(rc);
+                       break;
+               }
+
+               if (!ocfs2_buffer_uptodate(bucket->bu_inode,
+                                          bucket->bu_bhs[i]))
+                       ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
+                                                     bucket->bu_bhs[i]);
+       }
+
+       if (rc)
+               ocfs2_xattr_bucket_relse(bucket);
+       return rc;
+}
+
+/* Read the xattr bucket at xb_blkno */
+static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
+                                  u64 xb_blkno)
+{
+       int rc;
+
+       rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
+                              bucket->bu_blocks, bucket->bu_bhs, 0);
+       if (rc)
+               ocfs2_xattr_bucket_relse(bucket);
+       return rc;
+}
+
+static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
+                                            struct ocfs2_xattr_bucket *bucket,
+                                            int type)
+{
+       int i, rc = 0;
+
+       for (i = 0; i < bucket->bu_blocks; i++) {
+               rc = ocfs2_journal_access(handle, bucket->bu_inode,
+                                         bucket->bu_bhs[i], type);
+               if (rc) {
+                       mlog_errno(rc);
+                       break;
+               }
+       }
+
+       return rc;
+}
+
+static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
+                                            struct ocfs2_xattr_bucket *bucket)
+{
+       int i;
+
+       for (i = 0; i < bucket->bu_blocks; i++)
+               ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
+}
+
+static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
+                                        struct ocfs2_xattr_bucket *src)
+{
+       int i;
+       int blocksize = src->bu_inode->i_sb->s_blocksize;
+
+       BUG_ON(dest->bu_blocks != src->bu_blocks);
+       BUG_ON(dest->bu_inode != src->bu_inode);
+
+       for (i = 0; i < src->bu_blocks; i++) {
+               memcpy(bucket_block(dest, i), bucket_block(src, i),
+                      blocksize);
+       }
+}
+
 static inline const char *ocfs2_xattr_prefix(int name_index)
 {
        struct xattr_handler *handler = NULL;
@@ -542,14 +690,12 @@ static int ocfs2_xattr_block_list(struct inode *inode,
                mlog_errno(ret);
                return ret;
        }
-       /*Verify the signature of xattr block*/
-       if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
-                  strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
-               ret = -EFAULT;
-               goto cleanup;
-       }
 
        xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
+       if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
+               ret = -EIO;
+               goto cleanup;
+       }
 
        if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
                struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
@@ -749,47 +895,30 @@ static int ocfs2_xattr_block_get(struct inode *inode,
                                 size_t buffer_size,
                                 struct ocfs2_xattr_search *xs)
 {
-       struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
-       struct buffer_head *blk_bh = NULL;
        struct ocfs2_xattr_block *xb;
        struct ocfs2_xattr_value_root *xv;
        size_t size;
        int ret = -ENODATA, name_offset, name_len, block_off, i;
 
-       if (!di->i_xattr_loc)
-               return ret;
-
-       memset(&xs->bucket, 0, sizeof(xs->bucket));
-
-       ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
-       if (ret < 0) {
+       xs->bucket = ocfs2_xattr_bucket_new(inode);
+       if (!xs->bucket) {
+               ret = -ENOMEM;
                mlog_errno(ret);
-               return ret;
-       }
-       /*Verify the signature of xattr block*/
-       if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
-                  strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
-               ret = -EFAULT;
                goto cleanup;
        }
 
-       xs->xattr_bh = blk_bh;
-       xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
-
-       if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
-               xs->header = &xb->xb_attrs.xb_header;
-               xs->base = (void *)xs->header;
-               xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
-               xs->here = xs->header->xh_entries;
-
-               ret = ocfs2_xattr_find_entry(name_index, name, xs);
-       } else
-               ret = ocfs2_xattr_index_block_find(inode, blk_bh,
-                                                  name_index,
-                                                  name, xs);
+       ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
+       if (ret) {
+               mlog_errno(ret);
+               goto cleanup;
+       }
 
-       if (ret)
+       if (xs->not_found) {
+               ret = -ENODATA;
                goto cleanup;
+       }
+
+       xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
        size = le64_to_cpu(xs->here->xe_value_size);
        if (buffer) {
                ret = -ERANGE;
@@ -802,11 +931,11 @@ static int ocfs2_xattr_block_get(struct inode *inode,
 
                if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
                        ret = ocfs2_xattr_bucket_get_name_value(inode,
-                                                               xs->bucket.xh,
+                                                               bucket_xh(xs->bucket),
                                                                i,
                                                                &block_off,
                                                                &name_offset);
-                       xs->base = xs->bucket.bhs[block_off]->b_data;
+                       xs->base = bucket_block(xs->bucket, block_off);
                }
                if (ocfs2_xattr_is_local(xs->here)) {
                        memcpy(buffer, (void *)xs->base +
@@ -824,11 +953,10 @@ static int ocfs2_xattr_block_get(struct inode *inode,
        }
        ret = size;
 cleanup:
-       for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
-               brelse(xs->bucket.bhs[i]);
-       memset(&xs->bucket, 0, sizeof(xs->bucket));
+       ocfs2_xattr_bucket_free(xs->bucket);
 
-       brelse(blk_bh);
+       brelse(xs->xattr_bh);
+       xs->xattr_bh = NULL;
        return ret;
 }
 
@@ -837,11 +965,11 @@ cleanup:
  * Copy an extended attribute into the buffer provided.
  * Buffer is NULL to compute the size of buffer required.
  */
-int ocfs2_xattr_get(struct inode *inode,
-                   int name_index,
-                   const char *name,
-                   void *buffer,
-                   size_t buffer_size)
+static int ocfs2_xattr_get(struct inode *inode,
+                          int name_index,
+                          const char *name,
+                          void *buffer,
+                          size_t buffer_size)
 {
        int ret;
        struct ocfs2_dinode *di = NULL;
@@ -871,7 +999,7 @@ int ocfs2_xattr_get(struct inode *inode,
        down_read(&oi->ip_xattr_sem);
        ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
                                    buffer_size, &xis);
-       if (ret == -ENODATA)
+       if (ret == -ENODATA && di->i_xattr_loc)
                ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
                                            buffer_size, &xbs);
        up_read(&oi->ip_xattr_sem);
@@ -1229,7 +1357,7 @@ static int ocfs2_xattr_set_entry(struct inode *inode,
 
        free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
        if (free < 0)
-               return -EFAULT;
+               return -EIO;
 
        if (!xs->not_found) {
                size_t size = 0;
@@ -1514,10 +1642,9 @@ static int ocfs2_xattr_free_block(struct inode *inode,
                goto out;
        }
 
-       /*Verify the signature of xattr block*/
-       if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
-                  strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
-               ret = -EFAULT;
+       xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
+       if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
+               ret = -EIO;
                goto out;
        }
 
@@ -1527,7 +1654,6 @@ static int ocfs2_xattr_free_block(struct inode *inode,
                goto out;
        }
 
-       xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
        blk = le64_to_cpu(xb->xb_blkno);
        bit = le16_to_cpu(xb->xb_suballoc_bit);
        bg_blkno = ocfs2_which_suballoc_group(blk, bit);
@@ -1771,15 +1897,14 @@ static int ocfs2_xattr_block_find(struct inode *inode,
                mlog_errno(ret);
                return ret;
        }
-       /*Verify the signature of xattr block*/
-       if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
-                  strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
-                       ret = -EFAULT;
-                       goto cleanup;
+
+       xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
+       if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
+               ret = -EIO;
+               goto cleanup;
        }
 
        xs->xattr_bh = blk_bh;
-       xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
 
        if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
                xs->header = &xb->xb_attrs.xb_header;
@@ -1805,52 +1930,6 @@ cleanup:
        return ret;
 }
 
-/*
- * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
- * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
- * re-initialized.
- */
-static int ocfs2_restore_xattr_block(struct inode *inode,
-                                    struct ocfs2_xattr_search *xs)
-{
-       int ret;
-       handle_t *handle;
-       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
-       struct ocfs2_xattr_block *xb =
-               (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
-       struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
-       u16 xb_flags = le16_to_cpu(xb->xb_flags);
-
-       BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
-               le16_to_cpu(el->l_next_free_rec) != 0);
-
-       handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
-       if (IS_ERR(handle)) {
-               ret = PTR_ERR(handle);
-               handle = NULL;
-               goto out;
-       }
-
-       ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret < 0) {
-               mlog_errno(ret);
-               goto out_commit;
-       }
-
-       memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
-              offsetof(struct ocfs2_xattr_block, xb_attrs));
-
-       xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
-
-       ocfs2_journal_dirty(handle, xs->xattr_bh);
-
-out_commit:
-       ocfs2_commit_trans(osb, handle);
-out:
-       return ret;
-}
-
 /*
  * ocfs2_xattr_block_set()
  *
@@ -1961,8 +2040,6 @@ out:
        }
 
        ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
-       if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
-               ret = ocfs2_restore_xattr_block(inode, xs);
 
 end:
 
@@ -1986,7 +2063,6 @@ int ocfs2_xattr_set(struct inode *inode,
        struct buffer_head *di_bh = NULL;
        struct ocfs2_dinode *di;
        int ret;
-       u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
 
        struct ocfs2_xattr_info xi = {
                .name_index = name_index,
@@ -2006,10 +2082,20 @@ int ocfs2_xattr_set(struct inode *inode,
        if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
                return -EOPNOTSUPP;
 
+       /*
+        * Only xbs will be used on indexed trees.  xis doesn't need a
+        * bucket.
+        */
+       xbs.bucket = ocfs2_xattr_bucket_new(inode);
+       if (!xbs.bucket) {
+               mlog_errno(-ENOMEM);
+               return -ENOMEM;
+       }
+
        ret = ocfs2_inode_lock(inode, &di_bh, 1);
        if (ret < 0) {
                mlog_errno(ret);
-               return ret;
+               goto cleanup_nolock;
        }
        xis.inode_bh = xbs.inode_bh = di_bh;
        di = (struct ocfs2_dinode *)di_bh->b_data;
@@ -2086,10 +2172,10 @@ int ocfs2_xattr_set(struct inode *inode,
 cleanup:
        up_write(&OCFS2_I(inode)->ip_xattr_sem);
        ocfs2_inode_unlock(inode, 1);
+cleanup_nolock:
        brelse(di_bh);
        brelse(xbs.xattr_bh);
-       for (i = 0; i < blk_per_bucket; i++)
-               brelse(xbs.bucket.bhs[i]);
+       ocfs2_xattr_bucket_free(xbs.bucket);
 
        return ret;
 }
@@ -2164,7 +2250,7 @@ typedef int (xattr_bucket_func)(struct inode *inode,
                                void *para);
 
 static int ocfs2_find_xe_in_bucket(struct inode *inode,
-                                  struct buffer_head *header_bh,
+                                  struct ocfs2_xattr_bucket *bucket,
                                   int name_index,
                                   const char *name,
                                   u32 name_hash,
@@ -2172,11 +2258,9 @@ static int ocfs2_find_xe_in_bucket(struct inode *inode,
                                   int *found)
 {
        int i, ret = 0, cmp = 1, block_off, new_offset;
-       struct ocfs2_xattr_header *xh =
-                       (struct ocfs2_xattr_header *)header_bh->b_data;
+       struct ocfs2_xattr_header *xh = bucket_xh(bucket);
        size_t name_len = strlen(name);
        struct ocfs2_xattr_entry *xe = NULL;
-       struct buffer_head *name_bh = NULL;
        char *xe_name;
 
        /*
@@ -2207,19 +2291,8 @@ static int ocfs2_find_xe_in_bucket(struct inode *inode,
                        break;
                }
 
-               ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
-                                      &name_bh);
-               if (ret) {
-                       mlog_errno(ret);
-                       break;
-               }
-               xe_name = name_bh->b_data + new_offset;
-
-               cmp = memcmp(name, xe_name, name_len);
-               brelse(name_bh);
-               name_bh = NULL;
-
-               if (cmp == 0) {
+               xe_name = bucket_block(bucket, block_off) + new_offset;
+               if (!memcmp(name, xe_name, name_len)) {
                        *xe_index = i;
                        *found = 1;
                        ret = 0;
@@ -2249,39 +2322,42 @@ static int ocfs2_xattr_bucket_find(struct inode *inode,
                                   struct ocfs2_xattr_search *xs)
 {
        int ret, found = 0;
-       struct buffer_head *bh = NULL;
-       struct buffer_head *lower_bh = NULL;
        struct ocfs2_xattr_header *xh = NULL;
        struct ocfs2_xattr_entry *xe = NULL;
        u16 index = 0;
        u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
        int low_bucket = 0, bucket, high_bucket;
+       struct ocfs2_xattr_bucket *search;
        u32 last_hash;
-       u64 blkno;
+       u64 blkno, lower_blkno = 0;
+
+       search = ocfs2_xattr_bucket_new(inode);
+       if (!search) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto out;
+       }
 
-       ret = ocfs2_read_block(inode, p_blkno, &bh);
+       ret = ocfs2_read_xattr_bucket(search, p_blkno);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       xh = (struct ocfs2_xattr_header *)bh->b_data;
+       xh = bucket_xh(search);
        high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
-
        while (low_bucket <= high_bucket) {
-               brelse(bh);
-               bh = NULL;
-               bucket = (low_bucket + high_bucket) / 2;
+               ocfs2_xattr_bucket_relse(search);
 
+               bucket = (low_bucket + high_bucket) / 2;
                blkno = p_blkno + bucket * blk_per_bucket;
-
-               ret = ocfs2_read_block(inode, blkno, &bh);
+               ret = ocfs2_read_xattr_bucket(search, blkno);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               xh = (struct ocfs2_xattr_header *)bh->b_data;
+               xh = bucket_xh(search);
                xe = &xh->xh_entries[0];
                if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
                        high_bucket = bucket - 1;
@@ -2298,10 +2374,8 @@ static int ocfs2_xattr_bucket_find(struct inode *inode,
 
                last_hash = le32_to_cpu(xe->xe_name_hash);
 
-               /* record lower_bh which may be the insert place. */
-               brelse(lower_bh);
-               lower_bh = bh;
-               bh = NULL;
+               /* record lower_blkno which may be the insert place. */
+               lower_blkno = blkno;
 
                if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
                        low_bucket = bucket + 1;
@@ -2309,7 +2383,7 @@ static int ocfs2_xattr_bucket_find(struct inode *inode,
                }
 
                /* the searched xattr should reside in this bucket if exists. */
-               ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
+               ret = ocfs2_find_xe_in_bucket(inode, search,
                                              name_index, name, name_hash,
                                              &index, &found);
                if (ret) {
@@ -2324,46 +2398,29 @@ static int ocfs2_xattr_bucket_find(struct inode *inode,
         * When the xattr's hash value is in the gap of 2 buckets, we will
         * always set it to the previous bucket.
         */
-       if (!lower_bh) {
-               /*
-                * We can't find any bucket whose first name_hash is less
-                * than the find name_hash.
-                */
-               BUG_ON(bh->b_blocknr != p_blkno);
-               lower_bh = bh;
-               bh = NULL;
+       if (!lower_blkno)
+               lower_blkno = p_blkno;
+
+       /* This should be in cache - we just read it during the search */
+       ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
        }
-       xs->bucket.bhs[0] = lower_bh;
-       xs->bucket.xh = (struct ocfs2_xattr_header *)
-                                       xs->bucket.bhs[0]->b_data;
-       lower_bh = NULL;
 
-       xs->header = xs->bucket.xh;
-       xs->base = xs->bucket.bhs[0]->b_data;
+       xs->header = bucket_xh(xs->bucket);
+       xs->base = bucket_block(xs->bucket, 0);
        xs->end = xs->base + inode->i_sb->s_blocksize;
 
        if (found) {
-               /*
-                * If we have found the xattr enty, read all the blocks in
-                * this bucket.
-                */
-               ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
-                                       blk_per_bucket - 1, &xs->bucket.bhs[1],
-                                       0);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
-
                xs->here = &xs->header->xh_entries[index];
                mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
-                    (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
+                    (unsigned long long)bucket_blkno(xs->bucket), index);
        } else
                ret = -ENODATA;
 
 out:
-       brelse(bh);
-       brelse(lower_bh);
+       ocfs2_xattr_bucket_free(search);
        return ret;
 }
 
@@ -2398,7 +2455,8 @@ static int ocfs2_xattr_index_block_find(struct inode *inode,
        BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
 
        mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
-            "in the rec is %u\n", num_clusters, p_blkno, first_hash);
+            "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
+            first_hash);
 
        ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
                                      p_blkno, first_hash, num_clusters, xs);
@@ -2413,52 +2471,50 @@ static int ocfs2_iterate_xattr_buckets(struct inode *inode,
                                       xattr_bucket_func *func,
                                       void *para)
 {
-       int i, j, ret = 0;
-       int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
+       int i, ret = 0;
        u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
        u32 num_buckets = clusters * bpc;
-       struct ocfs2_xattr_bucket bucket;
+       struct ocfs2_xattr_bucket *bucket;
 
-       memset(&bucket, 0, sizeof(bucket));
+       bucket = ocfs2_xattr_bucket_new(inode);
+       if (!bucket) {
+               mlog_errno(-ENOMEM);
+               return -ENOMEM;
+       }
 
        mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
-            clusters, blkno);
+            clusters, (unsigned long long)blkno);
 
-       for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
-               ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
-                                       bucket.bhs, 0);
+       for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
+               ret = ocfs2_read_xattr_bucket(bucket, blkno);
                if (ret) {
                        mlog_errno(ret);
-                       goto out;
+                       break;
                }
 
-               bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
                /*
                 * The real bucket num in this series of blocks is stored
                 * in the 1st bucket.
                 */
                if (i == 0)
-                       num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
+                       num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
 
-               mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
-                    le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
+               mlog(0, "iterating xattr bucket %llu, first hash %u\n",
+                    (unsigned long long)blkno,
+                    le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
                if (func) {
-                       ret = func(inode, &bucket, para);
-                       if (ret) {
+                       ret = func(inode, bucket, para);
+                       if (ret)
                                mlog_errno(ret);
-                               break;
-                       }
+                       /* Fall through to bucket_relse() */
                }
 
-               for (j = 0; j < blk_per_bucket; j++)
-                       brelse(bucket.bhs[j]);
-               memset(&bucket, 0, sizeof(bucket));
+               ocfs2_xattr_bucket_relse(bucket);
+               if (ret)
+                       break;
        }
 
-out:
-       for (j = 0; j < blk_per_bucket; j++)
-               brelse(bucket.bhs[j]);
-
+       ocfs2_xattr_bucket_free(bucket);
        return ret;
 }
 
@@ -2496,21 +2552,21 @@ static int ocfs2_list_xattr_bucket(struct inode *inode,
        int i, block_off, new_offset;
        const char *prefix, *name;
 
-       for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
-               struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
+       for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
+               struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
                type = ocfs2_xattr_get_type(entry);
                prefix = ocfs2_xattr_prefix(type);
 
                if (prefix) {
                        ret = ocfs2_xattr_bucket_get_name_value(inode,
-                                                               bucket->xh,
+                                                               bucket_xh(bucket),
                                                                i,
                                                                &block_off,
                                                                &new_offset);
                        if (ret)
                                break;
 
-                       name = (const char *)bucket->bhs[block_off]->b_data +
+                       name = (const char *)bucket_block(bucket, block_off) +
                                new_offset;
                        ret = ocfs2_xattr_list_entry(xl->buffer,
                                                     xl->buffer_size,
@@ -2595,32 +2651,34 @@ static void swap_xe(void *a, void *b, int size)
 /*
  * When the ocfs2_xattr_block is filled up, new bucket will be created
  * and all the xattr entries will be moved to the new bucket.
+ * The header goes at the start of the bucket, and the names+values are
+ * filled from the end.  This is why *target starts as the last buffer.
  * Note: we need to sort the entries since they are not saved in order
  * in the ocfs2_xattr_block.
  */
 static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
                                           struct buffer_head *xb_bh,
-                                          struct buffer_head *xh_bh,
-                                          struct buffer_head *data_bh)
+                                          struct ocfs2_xattr_bucket *bucket)
 {
        int i, blocksize = inode->i_sb->s_blocksize;
+       int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
        u16 offset, size, off_change;
        struct ocfs2_xattr_entry *xe;
        struct ocfs2_xattr_block *xb =
                                (struct ocfs2_xattr_block *)xb_bh->b_data;
        struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
-       struct ocfs2_xattr_header *xh =
-                               (struct ocfs2_xattr_header *)xh_bh->b_data;
+       struct ocfs2_xattr_header *xh = bucket_xh(bucket);
        u16 count = le16_to_cpu(xb_xh->xh_count);
-       char *target = xh_bh->b_data, *src = xb_bh->b_data;
+       char *src = xb_bh->b_data;
+       char *target = bucket_block(bucket, blks - 1);
 
        mlog(0, "cp xattr from block %llu to bucket %llu\n",
             (unsigned long long)xb_bh->b_blocknr,
-            (unsigned long long)xh_bh->b_blocknr);
+            (unsigned long long)bucket_blkno(bucket));
+
+       for (i = 0; i < blks; i++)
+               memset(bucket_block(bucket, i), 0, blocksize);
 
-       memset(xh_bh->b_data, 0, blocksize);
-       if (data_bh)
-               memset(data_bh->b_data, 0, blocksize);
        /*
         * Since the xe_name_offset is based on ocfs2_xattr_header,
         * there is a offset change corresponding to the change of
@@ -2632,8 +2690,6 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
        size = blocksize - offset;
 
        /* copy all the names and values. */
-       if (data_bh)
-               target = data_bh->b_data;
        memcpy(target + offset, src + offset, size);
 
        /* Init new header now. */
@@ -2643,7 +2699,7 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
        xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
 
        /* copy all the entries. */
-       target = xh_bh->b_data;
+       target = bucket_block(bucket, 0);
        offset = offsetof(struct ocfs2_xattr_header, xh_entries);
        size = count * sizeof(struct ocfs2_xattr_entry);
        memcpy(target + offset, (char *)xb_xh + offset, size);
@@ -2669,43 +2725,24 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  * While if the entry is in index b-tree, "bucket" indicates the
  * real place of the xattr.
  */
-static int ocfs2_xattr_update_xattr_search(struct inode *inode,
-                                          struct ocfs2_xattr_search *xs,
-                                          struct buffer_head *old_bh,
-                                          struct buffer_head *new_bh)
+static void ocfs2_xattr_update_xattr_search(struct inode *inode,
+                                           struct ocfs2_xattr_search *xs,
+                                           struct buffer_head *old_bh)
 {
-       int ret = 0;
        char *buf = old_bh->b_data;
        struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
        struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
-       int i, blocksize = inode->i_sb->s_blocksize;
-       u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
-
-       xs->bucket.bhs[0] = new_bh;
-       get_bh(new_bh);
-       xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
-       xs->header = xs->bucket.xh;
+       int i;
 
-       xs->base = new_bh->b_data;
+       xs->header = bucket_xh(xs->bucket);
+       xs->base = bucket_block(xs->bucket, 0);
        xs->end = xs->base + inode->i_sb->s_blocksize;
 
-       if (!xs->not_found) {
-               if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
-                       ret = ocfs2_read_blocks(inode,
-                                       xs->bucket.bhs[0]->b_blocknr + 1,
-                                       blk_per_bucket - 1, &xs->bucket.bhs[1],
-                                       0);
-                       if (ret) {
-                               mlog_errno(ret);
-                               return ret;
-                       }
-
-                       i = xs->here - old_xh->xh_entries;
-                       xs->here = &xs->header->xh_entries[i];
-               }
-       }
+       if (xs->not_found)
+               return;
 
-       return ret;
+       i = xs->here - old_xh->xh_entries;
+       xs->here = &xs->header->xh_entries[i];
 }
 
 static int ocfs2_xattr_create_index_block(struct inode *inode,
@@ -2718,18 +2755,17 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct ocfs2_inode_info *oi = OCFS2_I(inode);
        struct ocfs2_alloc_context *data_ac;
-       struct buffer_head *xh_bh = NULL, *data_bh = NULL;
        struct buffer_head *xb_bh = xs->xattr_bh;
        struct ocfs2_xattr_block *xb =
                        (struct ocfs2_xattr_block *)xb_bh->b_data;
        struct ocfs2_xattr_tree_root *xr;
        u16 xb_flags = le16_to_cpu(xb->xb_flags);
-       u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
 
        mlog(0, "create xattr index block for %llu\n",
             (unsigned long long)xb_bh->b_blocknr);
 
        BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
+       BUG_ON(!xs->bucket);
 
        ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
        if (ret) {
@@ -2745,10 +2781,10 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
        down_write(&oi->ip_alloc_sem);
 
        /*
-        * 3 more credits, one for xattr block update, one for the 1st block
-        * of the new xattr bucket and one for the value/data.
+        * We need more credits.  One for the xattr block update and one
+        * for each block of the new xattr bucket.
         */
-       credits += 3;
+       credits += 1 + ocfs2_blocks_per_xattr_bucket(inode->i_sb);
        handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
@@ -2776,49 +2812,26 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
         */
        blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
 
-       mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
+       mlog(0, "allocate 1 cluster from %llu to xattr block\n",
+            (unsigned long long)blkno);
 
-       xh_bh = sb_getblk(inode->i_sb, blkno);
-       if (!xh_bh) {
-               ret = -EIO;
+       ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
+       if (ret) {
                mlog_errno(ret);
                goto out_commit;
        }
 
-       ocfs2_set_new_buffer_uptodate(inode, xh_bh);
-
-       ret = ocfs2_journal_access(handle, inode, xh_bh,
-                                  OCFS2_JOURNAL_ACCESS_CREATE);
+       ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
+                                               OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
        }
 
-       if (bpb > 1) {
-               data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
-               if (!data_bh) {
-                       ret = -EIO;
-                       mlog_errno(ret);
-                       goto out_commit;
-               }
-
-               ocfs2_set_new_buffer_uptodate(inode, data_bh);
+       ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
+       ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
 
-               ret = ocfs2_journal_access(handle, inode, data_bh,
-                                          OCFS2_JOURNAL_ACCESS_CREATE);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out_commit;
-               }
-       }
-
-       ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
-
-       ocfs2_journal_dirty(handle, xh_bh);
-       if (data_bh)
-               ocfs2_journal_dirty(handle, data_bh);
-
-       ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
+       ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
 
        /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
        memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
@@ -2853,9 +2866,6 @@ out:
        if (data_ac)
                ocfs2_free_alloc_context(data_ac);
 
-       brelse(xh_bh);
-       brelse(data_bh);
-
        return ret;
 }
 
@@ -2885,23 +2895,12 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
        size_t end, offset, len, value_len;
        struct ocfs2_xattr_header *xh;
        char *entries, *buf, *bucket_buf = NULL;
-       u64 blkno = bucket->bhs[0]->b_blocknr;
-       u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
+       u64 blkno = bucket_blkno(bucket);
        u16 xh_free_start;
        size_t blocksize = inode->i_sb->s_blocksize;
        handle_t *handle;
-       struct buffer_head **bhs;
        struct ocfs2_xattr_entry *xe;
 
-       bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
-                       GFP_NOFS);
-       if (!bhs)
-               return -ENOMEM;
-
-       ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
-       if (ret)
-               goto out;
-
        /*
         * In order to make the operation more efficient and generic,
         * we copy all the blocks into a contiguous memory and do the
@@ -2915,10 +2914,10 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
        }
 
        buf = bucket_buf;
-       for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
-               memcpy(buf, bhs[i]->b_data, blocksize);
+       for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
+               memcpy(buf, bucket_block(bucket, i), blocksize);
 
-       handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
+       handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), bucket->bu_blocks);
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
                handle = NULL;
@@ -2926,13 +2925,11 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
                goto out;
        }
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               ret = ocfs2_journal_access(handle, inode, bhs[i],
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
-               if (ret < 0) {
-                       mlog_errno(ret);
-                       goto commit;
-               }
+       ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto commit;
        }
 
        xh = (struct ocfs2_xattr_header *)bucket_buf;
@@ -2941,8 +2938,8 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
 
        mlog(0, "adjust xattr bucket in %llu, count = %u, "
             "xh_free_start = %u, xh_name_value_len = %u.\n",
-            blkno, le16_to_cpu(xh->xh_count), xh_free_start,
-            le16_to_cpu(xh->xh_name_value_len));
+            (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
+            xh_free_start, le16_to_cpu(xh->xh_name_value_len));
 
        /*
         * sort all the entries by their offset.
@@ -3001,21 +2998,13 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
             cmp_xe, swap_xe);
 
        buf = bucket_buf;
-       for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
-               memcpy(bhs[i]->b_data, buf, blocksize);
-               ocfs2_journal_dirty(handle, bhs[i]);
-       }
+       for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
+               memcpy(bucket_block(bucket, i), buf, blocksize);
+       ocfs2_xattr_bucket_journal_dirty(handle, bucket);
 
 commit:
        ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
 out:
-
-       if (bhs) {
-               for (i = 0; i < blk_per_bucket; i++)
-                       brelse(bhs[i]);
-       }
-       kfree(bhs);
-
        kfree(bucket_buf);
        return ret;
 }
@@ -3058,7 +3047,7 @@ static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
        prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
 
        mlog(0, "move half of xattrs in cluster %llu to %llu\n",
-            prev_blkno, new_blkno);
+            (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
 
        /*
         * We need to update the 1st half of the new cluster and
@@ -3142,99 +3131,143 @@ out:
        return ret;
 }
 
-static int ocfs2_read_xattr_bucket(struct inode *inode,
-                                  u64 blkno,
-                                  struct buffer_head **bhs,
-                                  int new)
+/*
+ * Find the suitable pos when we divide a bucket into 2.
+ * We have to make sure the xattrs with the same hash value exist
+ * in the same bucket.
+ *
+ * If this ocfs2_xattr_header covers more than one hash value, find a
+ * place where the hash value changes.  Try to find the most even split.
+ * The most common case is that all entries have different hash values,
+ * and the first check we make will find a place to split.
+ */
+static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
 {
-       int ret = 0;
-       u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
+       struct ocfs2_xattr_entry *entries = xh->xh_entries;
+       int count = le16_to_cpu(xh->xh_count);
+       int delta, middle = count / 2;
 
-       if (!new)
-               return ocfs2_read_blocks(inode, blkno,
-                                        blk_per_bucket, bhs, 0);
+       /*
+        * We start at the middle.  Each step gets farther away in both
+        * directions.  We therefore hit the change in hash value
+        * nearest to the middle.  Note that this loop does not execute for
+        * count < 2.
+        */
+       for (delta = 0; delta < middle; delta++) {
+               /* Let's check delta earlier than middle */
+               if (cmp_xe(&entries[middle - delta - 1],
+                          &entries[middle - delta]))
+                       return middle - delta;
+
+               /* For even counts, don't walk off the end */
+               if ((middle + delta + 1) == count)
+                       continue;
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               bhs[i] = sb_getblk(inode->i_sb, blkno + i);
-               if (bhs[i] == NULL) {
-                       ret = -EIO;
-                       mlog_errno(ret);
-                       break;
-               }
-               ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
+               /* Now try delta past middle */
+               if (cmp_xe(&entries[middle + delta],
+                          &entries[middle + delta + 1]))
+                       return middle + delta + 1;
        }
 
-       return ret;
+       /* Every entry had the same hash */
+       return count;
 }
 
 /*
- * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
+ * Move some xattrs in old bucket(blk) to new bucket(new_blk).
  * first_hash will record the 1st hash of the new bucket.
+ *
+ * Normally half of the xattrs will be moved.  But we have to make
+ * sure that the xattrs with the same hash value are stored in the
+ * same bucket. If all the xattrs in this bucket have the same hash
+ * value, the new bucket will be initialized as an empty one and the
+ * first_hash will be initialized as (hash_value+1).
  */
-static int ocfs2_half_xattr_bucket(struct inode *inode,
-                                  handle_t *handle,
-                                  u64 blk,
-                                  u64 new_blk,
-                                  u32 *first_hash,
-                                  int new_bucket_head)
+static int ocfs2_divide_xattr_bucket(struct inode *inode,
+                                   handle_t *handle,
+                                   u64 blk,
+                                   u64 new_blk,
+                                   u32 *first_hash,
+                                   int new_bucket_head)
 {
        int ret, i;
-       u16 count, start, len, name_value_len, xe_len, name_offset;
-       u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
-       struct buffer_head **s_bhs, **t_bhs = NULL;
+       int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
+       struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
        struct ocfs2_xattr_header *xh;
        struct ocfs2_xattr_entry *xe;
        int blocksize = inode->i_sb->s_blocksize;
 
-       mlog(0, "move half of xattrs from bucket %llu to %llu\n",
-            blk, new_blk);
+       mlog(0, "move some of xattrs from bucket %llu to %llu\n",
+            (unsigned long long)blk, (unsigned long long)new_blk);
 
-       s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
-       if (!s_bhs)
-               return -ENOMEM;
+       s_bucket = ocfs2_xattr_bucket_new(inode);
+       t_bucket = ocfs2_xattr_bucket_new(inode);
+       if (!s_bucket || !t_bucket) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto out;
+       }
 
-       ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
+       ret = ocfs2_read_xattr_bucket(s_bucket, blk);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_journal_access(handle, inode, s_bhs[0],
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
-       if (!t_bhs) {
-               ret = -ENOMEM;
+       /*
+        * Even if !new_bucket_head, we're overwriting t_bucket.  Thus,
+        * there's no need to read it.
+        */
+       ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
+       if (ret) {
+               mlog_errno(ret);
                goto out;
        }
 
-       ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
+       ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
+                                               new_bucket_head ?
+                                               OCFS2_JOURNAL_ACCESS_CREATE :
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out;
        }
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               ret = ocfs2_journal_access(handle, inode, t_bhs[i],
-                                          OCFS2_JOURNAL_ACCESS_CREATE);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
+       xh = bucket_xh(s_bucket);
+       count = le16_to_cpu(xh->xh_count);
+       start = ocfs2_xattr_find_divide_pos(xh);
+
+       if (start == count) {
+               xe = &xh->xh_entries[start-1];
+
+               /*
+                * initialized a new empty bucket here.
+                * The hash value is set as one larger than
+                * that of the last entry in the previous bucket.
+                */
+               for (i = 0; i < t_bucket->bu_blocks; i++)
+                       memset(bucket_block(t_bucket, i), 0, blocksize);
+
+               xh = bucket_xh(t_bucket);
+               xh->xh_free_start = cpu_to_le16(blocksize);
+               xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
+               le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
+
+               goto set_num_buckets;
        }
 
        /* copy the whole bucket to the new first. */
-       for (i = 0; i < blk_per_bucket; i++)
-               memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
+       ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
 
        /* update the new bucket. */
-       xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
-       count = le16_to_cpu(xh->xh_count);
-       start = count / 2;
+       xh = bucket_xh(t_bucket);
 
        /*
         * Calculate the total name/value len and xh_free_start for
@@ -3291,49 +3324,39 @@ static int ocfs2_half_xattr_bucket(struct inode *inode,
                        xh->xh_free_start = xe->xe_name_offset;
        }
 
+set_num_buckets:
        /* set xh->xh_num_buckets for the new xh. */
        if (new_bucket_head)
                xh->xh_num_buckets = cpu_to_le16(1);
        else
                xh->xh_num_buckets = 0;
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               ocfs2_journal_dirty(handle, t_bhs[i]);
-               if (ret)
-                       mlog_errno(ret);
-       }
+       ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
 
        /* store the first_hash of the new bucket. */
        if (first_hash)
                *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
 
        /*
-        * Now only update the 1st block of the old bucket.
-        * Please note that the entry has been sorted already above.
+        * Now only update the 1st block of the old bucket.  If we
+        * just added a new empty bucket, there is no need to modify
+        * it.
         */
-       xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
+       if (start == count)
+               goto out;
+
+       xh = bucket_xh(s_bucket);
        memset(&xh->xh_entries[start], 0,
               sizeof(struct ocfs2_xattr_entry) * (count - start));
        xh->xh_count = cpu_to_le16(start);
        xh->xh_free_start = cpu_to_le16(name_offset);
        xh->xh_name_value_len = cpu_to_le16(name_value_len);
 
-       ocfs2_journal_dirty(handle, s_bhs[0]);
-       if (ret)
-               mlog_errno(ret);
+       ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
 
 out:
-       if (s_bhs) {
-               for (i = 0; i < blk_per_bucket; i++)
-                       brelse(s_bhs[i]);
-       }
-       kfree(s_bhs);
-
-       if (t_bhs) {
-               for (i = 0; i < blk_per_bucket; i++)
-                       brelse(t_bhs[i]);
-       }
-       kfree(t_bhs);
+       ocfs2_xattr_bucket_free(s_bucket);
+       ocfs2_xattr_bucket_free(t_bucket);
 
        return ret;
 }
@@ -3350,60 +3373,48 @@ static int ocfs2_cp_xattr_bucket(struct inode *inode,
                                 u64 t_blkno,
                                 int t_is_new)
 {
-       int ret, i;
-       int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
-       int blocksize = inode->i_sb->s_blocksize;
-       struct buffer_head **s_bhs, **t_bhs = NULL;
+       int ret;
+       struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
 
        BUG_ON(s_blkno == t_blkno);
 
        mlog(0, "cp bucket %llu to %llu, target is %d\n",
-            s_blkno, t_blkno, t_is_new);
-
-       s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
-                       GFP_NOFS);
-       if (!s_bhs)
-               return -ENOMEM;
+            (unsigned long long)s_blkno, (unsigned long long)t_blkno,
+            t_is_new);
 
-       ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
-       if (ret)
-               goto out;
-
-       t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
-                       GFP_NOFS);
-       if (!t_bhs) {
+       s_bucket = ocfs2_xattr_bucket_new(inode);
+       t_bucket = ocfs2_xattr_bucket_new(inode);
+       if (!s_bucket || !t_bucket) {
                ret = -ENOMEM;
+               mlog_errno(ret);
                goto out;
        }
+  
+       ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
+       if (ret)
+               goto out;
 
-       ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
+       /*
+        * Even if !t_is_new, we're overwriting t_bucket.  Thus,
+        * there's no need to read it.
+        */
+       ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
        if (ret)
                goto out;
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               ret = ocfs2_journal_access(handle, inode, t_bhs[i],
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
-               if (ret)
-                       goto out;
-       }
+       ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
+                                               t_is_new ?
+                                               OCFS2_JOURNAL_ACCESS_CREATE :
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret)
+               goto out;
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
-               ocfs2_journal_dirty(handle, t_bhs[i]);
-       }
+       ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
+       ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
 
 out:
-       if (s_bhs) {
-               for (i = 0; i < blk_per_bucket; i++)
-                       brelse(s_bhs[i]);
-       }
-       kfree(s_bhs);
-
-       if (t_bhs) {
-               for (i = 0; i < blk_per_bucket; i++)
-                       brelse(t_bhs[i]);
-       }
-       kfree(t_bhs);
+       ocfs2_xattr_bucket_free(t_bucket);
+       ocfs2_xattr_bucket_free(s_bucket);
 
        return ret;
 }
@@ -3428,7 +3439,8 @@ static int ocfs2_cp_xattr_cluster(struct inode *inode,
        struct ocfs2_xattr_header *xh;
        u64 to_blk_start = to_blk;
 
-       mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
+       mlog(0, "cp xattrs from cluster %llu to %llu\n",
+            (unsigned long long)src_blk, (unsigned long long)to_blk);
 
        /*
         * We need to update the new cluster and 1 more for the update of
@@ -3493,15 +3505,15 @@ out:
 }
 
 /*
- * Move half of the xattrs in this cluster to the new cluster.
+ * Move some xattrs in this cluster to the new cluster.
  * This function should only be called when bucket size == cluster size.
  * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  */
-static int ocfs2_half_xattr_cluster(struct inode *inode,
-                                   handle_t *handle,
-                                   u64 prev_blk,
-                                   u64 new_blk,
-                                   u32 *first_hash)
+static int ocfs2_divide_xattr_cluster(struct inode *inode,
+                                     handle_t *handle,
+                                     u64 prev_blk,
+                                     u64 new_blk,
+                                     u32 *first_hash)
 {
        u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
        int ret, credits = 2 * blk_per_bucket;
@@ -3515,8 +3527,8 @@ static int ocfs2_half_xattr_cluster(struct inode *inode,
        }
 
        /* Move half of the xattr in start_blk to the next bucket. */
-       return  ocfs2_half_xattr_bucket(inode, handle, prev_blk,
-                                       new_blk, first_hash, 1);
+       return  ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
+                                         new_blk, first_hash, 1);
 }
 
 /*
@@ -3559,7 +3571,8 @@ static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
        int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
 
        mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
-            prev_blk, prev_clusters, new_blk);
+            (unsigned long long)prev_blk, prev_clusters,
+            (unsigned long long)new_blk);
 
        if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
                ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
@@ -3578,9 +3591,9 @@ static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
                                                     last_blk, new_blk,
                                                     v_start);
                else {
-                       ret = ocfs2_half_xattr_cluster(inode, handle,
-                                                      last_blk, new_blk,
-                                                      v_start);
+                       ret = ocfs2_divide_xattr_cluster(inode, handle,
+                                                        last_blk, new_blk,
+                                                        v_start);
 
                        if ((*header_bh)->b_blocknr == last_blk && extend)
                                *extend = 0;
@@ -3629,7 +3642,7 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
        mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
             "previous xattr blkno = %llu\n",
             (unsigned long long)OCFS2_I(inode)->ip_blkno,
-            prev_cpos, prev_blkno);
+            prev_cpos, (unsigned long long)prev_blkno);
 
        ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
 
@@ -3716,7 +3729,7 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
                }
        }
        mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
-            num_bits, block, v_start);
+            num_bits, (unsigned long long)block, v_start);
        ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
                                  num_bits, 0, meta_ac);
        if (ret < 0) {
@@ -3761,7 +3774,7 @@ static int ocfs2_extend_xattr_bucket(struct inode *inode,
        u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
 
        mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
-            "from %llu, len = %u\n", start_blk,
+            "from %llu, len = %u\n", (unsigned long long)start_blk,
             (unsigned long long)first_bh->b_blocknr, num_clusters);
 
        BUG_ON(bucket >= num_buckets);
@@ -3770,9 +3783,9 @@ static int ocfs2_extend_xattr_bucket(struct inode *inode,
 
        /*
         * We will touch all the buckets after the start_bh(include it).
-        * Add one more bucket and modify the first_bh.
+        * Then we add one more bucket.
         */
-       credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
+       credits = end_blk - start_blk + 3 * blk_per_bucket + 1;
        handle = ocfs2_start_trans(osb, credits);
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
@@ -3797,8 +3810,8 @@ static int ocfs2_extend_xattr_bucket(struct inode *inode,
        }
 
        /* Move half of the xattr in start_blk to the next bucket. */
-       ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
-                                     start_blk + blk_per_bucket, NULL, 0);
+       ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
+                                       start_blk + blk_per_bucket, NULL, 0);
 
        le16_add_cpu(&first_xh->xh_num_buckets, 1);
        ocfs2_journal_dirty(handle, first_bh);
@@ -3898,7 +3911,7 @@ static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
        int block_off = offs >> inode->i_sb->s_blocksize_bits;
 
        offs = offs % inode->i_sb->s_blocksize;
-       return bucket->bhs[block_off]->b_data + offs;
+       return bucket_block(bucket, block_off) + offs;
 }
 
 /*
@@ -3953,7 +3966,7 @@ static void ocfs2_xattr_set_entry_normal(struct inode *inode,
                                xe->xe_value_size = 0;
 
                        val = ocfs2_xattr_bucket_get_val(inode,
-                                                        &xs->bucket, offs);
+                                                        xs->bucket, offs);
                        memset(val + OCFS2_XATTR_SIZE(name_len), 0,
                               size - OCFS2_XATTR_SIZE(name_len));
                        if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
@@ -4031,8 +4044,7 @@ set_new_name_value:
                xh->xh_free_start = cpu_to_le16(offs);
        }
 
-       val = ocfs2_xattr_bucket_get_val(inode,
-                                        &xs->bucket, offs - size);
+       val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
        xe->xe_name_offset = cpu_to_le16(offs - size);
 
        memset(val, 0, size);
@@ -4048,33 +4060,6 @@ set_new_name_value:
        return;
 }
 
-static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
-                                            handle_t *handle,
-                                            struct ocfs2_xattr_search *xs,
-                                            struct buffer_head **bhs,
-                                            u16 bh_num)
-{
-       int ret = 0, off, block_off;
-       struct ocfs2_xattr_entry *xe = xs->here;
-
-       /*
-        * First calculate all the blocks we should journal_access
-        * and journal_dirty. The first block should always be touched.
-        */
-       ret = ocfs2_journal_dirty(handle, bhs[0]);
-       if (ret)
-               mlog_errno(ret);
-
-       /* calc the data. */
-       off = le16_to_cpu(xe->xe_name_offset);
-       block_off = off >> inode->i_sb->s_blocksize_bits;
-       ret = ocfs2_journal_dirty(handle, bhs[block_off]);
-       if (ret)
-               mlog_errno(ret);
-
-       return ret;
-}
-
 /*
  * Set the xattr entry in the specified bucket.
  * The bucket is indicated by xs->bucket and it should have the enough
@@ -4086,27 +4071,26 @@ static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
                                           u32 name_hash,
                                           int local)
 {
-       int i, ret;
+       int ret;
        handle_t *handle = NULL;
-       u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       u64 blkno;
 
        mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
             (unsigned long)xi->value_len, xi->name_index,
-            (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
+            (unsigned long long)bucket_blkno(xs->bucket));
 
-       if (!xs->bucket.bhs[1]) {
-               ret = ocfs2_read_blocks(inode,
-                                       xs->bucket.bhs[0]->b_blocknr + 1,
-                                       blk_per_bucket - 1, &xs->bucket.bhs[1],
-                                       0);
+       if (!xs->bucket->bu_bhs[1]) {
+               blkno = bucket_blkno(xs->bucket);
+               ocfs2_xattr_bucket_relse(xs->bucket);
+               ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
        }
 
-       handle = ocfs2_start_trans(osb, blk_per_bucket);
+       handle = ocfs2_start_trans(osb, xs->bucket->bu_blocks);
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
                handle = NULL;
@@ -4114,22 +4098,16 @@ static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
                goto out;
        }
 
-       for (i = 0; i < blk_per_bucket; i++) {
-               ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
-                                          OCFS2_JOURNAL_ACCESS_WRITE);
-               if (ret < 0) {
-                       mlog_errno(ret);
-                       goto out;
-               }
+       ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret < 0) {
+               mlog_errno(ret);
+               goto out;
        }
 
        ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
+       ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
 
-       /*Only dirty the blocks we have touched in set xattr. */
-       ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
-                                               xs->bucket.bhs, blk_per_bucket);
-       if (ret)
-               mlog_errno(ret);
 out:
        ocfs2_commit_trans(osb, handle);
 
@@ -4146,7 +4124,7 @@ static int ocfs2_xattr_value_update_size(struct inode *inode,
        handle_t *handle = NULL;
 
        handle = ocfs2_start_trans(osb, 1);
-       if (handle == NULL) {
+       if (IS_ERR(handle)) {
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
@@ -4241,10 +4219,10 @@ static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
        struct ocfs2_xattr_entry *xe = xs->here;
        struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
 
-       BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
+       BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
 
        offset = xe - xh->xh_entries;
-       ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
+       ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
                                                offset, len);
        if (ret)
                mlog_errno(ret);
@@ -4313,7 +4291,7 @@ static int ocfs2_rm_xattr_cluster(struct inode *inode,
        }
 
        handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
-       if (handle == NULL) {
+       if (IS_ERR(handle)) {
                ret = -ENOMEM;
                mlog_errno(ret);
                goto out;
@@ -4364,20 +4342,21 @@ static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
                                         struct ocfs2_xattr_search *xs)
 {
        handle_t *handle = NULL;
-       struct ocfs2_xattr_header *xh = xs->bucket.xh;
+       struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
        struct ocfs2_xattr_entry *last = &xh->xh_entries[
                                                le16_to_cpu(xh->xh_count) - 1];
        int ret = 0;
 
-       handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
+       handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
+                                  ocfs2_blocks_per_xattr_bucket(inode->i_sb));
        if (IS_ERR(handle)) {
                ret = PTR_ERR(handle);
                mlog_errno(ret);
                return;
        }
 
-       ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
+       ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
+                                               OCFS2_JOURNAL_ACCESS_WRITE);
        if (ret) {
                mlog_errno(ret);
                goto out_commit;
@@ -4389,9 +4368,8 @@ static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
        memset(last, 0, sizeof(struct ocfs2_xattr_entry));
        le16_add_cpu(&xh->xh_count, -1);
 
-       ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
-       if (ret < 0)
-               mlog_errno(ret);
+       ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
+
 out_commit:
        ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
 }
@@ -4489,17 +4467,27 @@ out:
        return ret;
 }
 
-/* check whether the xattr bucket is filled up with the same hash value. */
+/*
+ * check whether the xattr bucket is filled up with the same hash value.
+ * If we want to insert the xattr with the same hash, return -ENOSPC.
+ * If we want to insert a xattr with different hash value, go ahead
+ * and ocfs2_divide_xattr_bucket will handle this.
+ */
 static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
-                                             struct ocfs2_xattr_bucket *bucket)
+                                             struct ocfs2_xattr_bucket *bucket,
+                                             const char *name)
 {
-       struct ocfs2_xattr_header *xh = bucket->xh;
+       struct ocfs2_xattr_header *xh = bucket_xh(bucket);
+       u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
+
+       if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
+               return 0;
 
        if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
            xh->xh_entries[0].xe_name_hash) {
                mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
                     "hash = %u\n",
-                    (unsigned long long)bucket->bhs[0]->b_blocknr,
+                    (unsigned long long)bucket_blkno(bucket),
                     le32_to_cpu(xh->xh_entries[0].xe_name_hash));
                return -ENOSPC;
        }
@@ -4514,11 +4502,10 @@ static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
        struct ocfs2_xattr_header *xh;
        struct ocfs2_xattr_entry *xe;
        u16 count, header_size, xh_free_start;
-       int i, free, max_free, need, old;
+       int free, max_free, need, old;
        size_t value_size = 0, name_len = strlen(xi->name);
        size_t blocksize = inode->i_sb->s_blocksize;
        int ret, allocation = 0;
-       u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
 
        mlog_entry("Set xattr %s in xattr index block\n", xi->name);
 
@@ -4533,7 +4520,7 @@ try_again:
 
        mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
                        "of %u which exceed block size\n",
-                       (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
+                       (unsigned long long)bucket_blkno(xs->bucket),
                        header_size);
 
        if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
@@ -4573,7 +4560,7 @@ try_again:
        mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
             "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
             " %u\n", xs->not_found,
-            (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
+            (unsigned long long)bucket_blkno(xs->bucket),
             free, need, max_free, le16_to_cpu(xh->xh_free_start),
             le16_to_cpu(xh->xh_name_value_len));
 
@@ -4585,7 +4572,7 @@ try_again:
                         * name/value will be moved, the xe shouldn't be changed
                         * in xs.
                         */
-                       ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
+                       ret = ocfs2_defrag_xattr_bucket(inode, xs->bucket);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
@@ -4616,7 +4603,9 @@ try_again:
                 * one bucket's worth, so check it here whether we need to
                 * add a new bucket for the insert.
                 */
-               ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
+               ret = ocfs2_check_xattr_bucket_collision(inode,
+                                                        xs->bucket,
+                                                        xi->name);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
@@ -4624,16 +4613,13 @@ try_again:
 
                ret = ocfs2_add_new_xattr_bucket(inode,
                                                 xs->xattr_bh,
-                                                xs->bucket.bhs[0]);
+                                                xs->bucket->bu_bhs[0]);
                if (ret) {
                        mlog_errno(ret);
                        goto out;
                }
 
-               for (i = 0; i < blk_per_bucket; i++)
-                       brelse(xs->bucket.bhs[i]);
-
-               memset(&xs->bucket, 0, sizeof(xs->bucket));
+               ocfs2_xattr_bucket_relse(xs->bucket);
 
                ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
                                                   xi->name_index,
@@ -4657,7 +4643,7 @@ static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
                                        void *para)
 {
        int ret = 0;
-       struct ocfs2_xattr_header *xh = bucket->xh;
+       struct ocfs2_xattr_header *xh = bucket_xh(bucket);
        u16 i;
        struct ocfs2_xattr_entry *xe;
 
@@ -4667,7 +4653,7 @@ static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
                        continue;
 
                ret = ocfs2_xattr_bucket_value_truncate(inode,
-                                                       bucket->bhs[0],
+                                                       bucket->bu_bhs[0],
                                                        i, 0);
                if (ret) {
                        mlog_errno(ret);
@@ -4727,14 +4713,11 @@ out:
 /*
  * 'trusted' attributes support
  */
-
-#define XATTR_TRUSTED_PREFIX "trusted."
-
 static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
                                       size_t list_size, const char *name,
                                       size_t name_len)
 {
-       const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
+       const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
        const size_t total_len = prefix_len + name_len + 1;
 
        if (list && total_len <= list_size) {
@@ -4771,18 +4754,14 @@ struct xattr_handler ocfs2_xattr_trusted_handler = {
        .set    = ocfs2_xattr_trusted_set,
 };
 
-
 /*
  * 'user' attributes support
  */
-
-#define XATTR_USER_PREFIX "user."
-
 static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
                                    size_t list_size, const char *name,
                                    size_t name_len)
 {
-       const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
+       const size_t prefix_len = XATTR_USER_PREFIX_LEN;
        const size_t total_len = prefix_len + name_len + 1;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);