proc: remove obsolete comments
[safe/jmp/linux-2.6] / fs / ocfs2 / xattr.c
index e0d0fa2..e97b348 100644 (file)
@@ -79,6 +79,7 @@ struct ocfs2_xattr_set_ctxt {
        struct ocfs2_alloc_context *meta_ac;
        struct ocfs2_alloc_context *data_ac;
        struct ocfs2_cached_dealloc_ctxt dealloc;
+       int set_abort;
 };
 
 #define OCFS2_XATTR_ROOT_SIZE  (sizeof(struct ocfs2_xattr_def_value_root))
@@ -96,7 +97,7 @@ static struct ocfs2_xattr_def_value_root def_xv = {
        .xv.xr_list.l_count = cpu_to_le16(1),
 };
 
-struct xattr_handler *ocfs2_xattr_handlers[] = {
+const struct xattr_handler *ocfs2_xattr_handlers[] = {
        &ocfs2_xattr_user_handler,
        &ocfs2_xattr_acl_access_handler,
        &ocfs2_xattr_acl_default_handler,
@@ -105,7 +106,7 @@ struct xattr_handler *ocfs2_xattr_handlers[] = {
        NULL
 };
 
-static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
+static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
        [OCFS2_XATTR_INDEX_USER]        = &ocfs2_xattr_user_handler,
        [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
                                        = &ocfs2_xattr_acl_access_handler,
@@ -142,6 +143,13 @@ struct ocfs2_xattr_search {
 struct ocfs2_xa_loc;
 struct ocfs2_xa_loc_operations {
        /*
+        * Journal functions
+        */
+       int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
+                                 int type);
+       void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
+
+       /*
         * Return a pointer to the appropriate buffer in loc->xl_storage
         * at the given offset from loc->xl_header.
         */
@@ -186,6 +194,9 @@ struct ocfs2_xa_loc_operations {
  * tracking the on-disk structure.
  */
 struct ocfs2_xa_loc {
+       /* This xattr belongs to this inode */
+       struct inode *xl_inode;
+
        /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
        struct ocfs2_xattr_header *xl_header;
 
@@ -312,14 +323,6 @@ 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))
@@ -537,7 +540,7 @@ static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
 
 static inline const char *ocfs2_xattr_prefix(int name_index)
 {
-       struct xattr_handler *handler = NULL;
+       const struct xattr_handler *handler = NULL;
 
        if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
                handler = ocfs2_xattr_handler_map[name_index];
@@ -563,24 +566,6 @@ static u32 ocfs2_xattr_name_hash(struct inode *inode,
        return hash;
 }
 
-/*
- * ocfs2_xattr_hash_entry()
- *
- * Compute the hash of an extended attribute.
- */
-static void ocfs2_xattr_hash_entry(struct inode *inode,
-                                  struct ocfs2_xattr_header *header,
-                                  struct ocfs2_xattr_entry *entry)
-{
-       u32 hash = 0;
-       char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
-
-       hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
-       entry->xe_name_hash = cpu_to_le32(hash);
-
-       return;
-}
-
 static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
 {
        return namevalue_size(name_len, value_len) +
@@ -755,11 +740,7 @@ static int ocfs2_xattr_extend_allocation(struct inode *inode,
                goto leave;
        }
 
-       status = ocfs2_journal_dirty(handle, vb->vb_bh);
-       if (status < 0) {
-               mlog_errno(status);
-               goto leave;
-       }
+       ocfs2_journal_dirty(handle, vb->vb_bh);
 
        clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
 
@@ -802,12 +783,7 @@ static int __ocfs2_remove_xattr_range(struct inode *inode,
        }
 
        le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
-
-       ret = ocfs2_journal_dirty(handle, vb->vb_bh);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
+       ocfs2_journal_dirty(handle, vb->vb_bh);
 
        if (ext_flags & OCFS2_EXT_REFCOUNTED)
                ret = ocfs2_decrease_refcount(inode, handle,
@@ -1390,11 +1366,7 @@ static int __ocfs2_xattr_set_value_outside(struct inode *inode,
                                memset(bh->b_data + cp_len, 0,
                                       blocksize - cp_len);
 
-                       ret = ocfs2_journal_dirty(handle, bh);
-                       if (ret < 0) {
-                               mlog_errno(ret);
-                               goto out;
-                       }
+                       ocfs2_journal_dirty(handle, bh);
                        brelse(bh);
                        bh = NULL;
 
@@ -1413,113 +1385,6 @@ out:
        return ret;
 }
 
-static int ocfs2_xattr_cleanup(struct inode *inode,
-                              handle_t *handle,
-                              struct ocfs2_xattr_info *xi,
-                              struct ocfs2_xattr_search *xs,
-                              struct ocfs2_xattr_value_buf *vb,
-                              size_t offs)
-{
-       int ret = 0;
-       void *val = xs->base + offs;
-       size_t size = namevalue_size_xi(xi);
-
-       ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
-                           OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-       /* Decrease xattr count */
-       le16_add_cpu(&xs->header->xh_count, -1);
-       /* Remove the xattr entry and tree root which has already be set*/
-       memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
-       memset(val, 0, size);
-
-       ret = ocfs2_journal_dirty(handle, vb->vb_bh);
-       if (ret < 0)
-               mlog_errno(ret);
-out:
-       return ret;
-}
-
-static int ocfs2_xattr_update_entry(struct inode *inode,
-                                   handle_t *handle,
-                                   struct ocfs2_xattr_info *xi,
-                                   struct ocfs2_xattr_search *xs,
-                                   struct ocfs2_xattr_value_buf *vb,
-                                   size_t offs)
-{
-       int ret;
-
-       ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
-                           OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       xs->here->xe_name_offset = cpu_to_le16(offs);
-       xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
-       if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
-               ocfs2_xattr_set_local(xs->here, 1);
-       else
-               ocfs2_xattr_set_local(xs->here, 0);
-       ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
-
-       ret = ocfs2_journal_dirty(handle, vb->vb_bh);
-       if (ret < 0)
-               mlog_errno(ret);
-out:
-       return ret;
-}
-
-/*
- * ocfs2_xattr_set_value_outside()
- *
- * Set large size value in B tree.
- */
-static int ocfs2_xattr_set_value_outside(struct inode *inode,
-                                        struct ocfs2_xattr_info *xi,
-                                        struct ocfs2_xattr_search *xs,
-                                        struct ocfs2_xattr_set_ctxt *ctxt,
-                                        struct ocfs2_xattr_value_buf *vb,
-                                        size_t offs)
-{
-       void *val = xs->base + offs;
-       struct ocfs2_xattr_value_root *xv = NULL;
-       size_t size = namevalue_size_xi(xi);
-       int ret = 0;
-
-       memset(val, 0, size);
-       memcpy(val, xi->xi_name, xi->xi_name_len);
-       xv = (struct ocfs2_xattr_value_root *)
-               (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
-       xv->xr_clusters = 0;
-       xv->xr_last_eb_blk = 0;
-       xv->xr_list.l_tree_depth = 0;
-       xv->xr_list.l_count = cpu_to_le16(1);
-       xv->xr_list.l_next_free_rec = 0;
-       vb->vb_xv = xv;
-
-       ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
-       if (ret < 0) {
-               mlog_errno(ret);
-               return ret;
-       }
-       ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
-       if (ret < 0) {
-               mlog_errno(ret);
-               return ret;
-       }
-       ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
-                                             xi->xi_value, xi->xi_value_len);
-       if (ret < 0)
-               mlog_errno(ret);
-
-       return ret;
-}
-
 static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
                                       int num_entries)
 {
@@ -1540,6 +1405,17 @@ static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
        return 0;
 }
 
+static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
+                                  int type)
+{
+       return loc->xl_ops->xlo_journal_access(handle, loc, type);
+}
+
+static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
+{
+       loc->xl_ops->xlo_journal_dirty(handle, loc);
+}
+
 /* Give a pointer into the storage for the given offset */
 static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
 {
@@ -1619,6 +1495,7 @@ static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
        int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
 
        /* Value bufs are for value trees */
+       BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
        BUG_ON(namevalue_size_xe(loc->xl_entry) !=
               (name_size + OCFS2_XATTR_ROOT_SIZE));
 
@@ -1629,6 +1506,29 @@ static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
                                                        name_size);
 }
 
+static int ocfs2_xa_block_journal_access(handle_t *handle,
+                                        struct ocfs2_xa_loc *loc, int type)
+{
+       struct buffer_head *bh = loc->xl_storage;
+       ocfs2_journal_access_func access;
+
+       if (loc->xl_size == (bh->b_size -
+                            offsetof(struct ocfs2_xattr_block,
+                                     xb_attrs.xb_header)))
+               access = ocfs2_journal_access_xb;
+       else
+               access = ocfs2_journal_access_di;
+       return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
+}
+
+static void ocfs2_xa_block_journal_dirty(handle_t *handle,
+                                        struct ocfs2_xa_loc *loc)
+{
+       struct buffer_head *bh = loc->xl_storage;
+
+       ocfs2_journal_dirty(handle, bh);
+}
+
 static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
                                           int offset)
 {
@@ -1710,7 +1610,7 @@ static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
        /* Now tell xh->xh_entries about it */
        for (i = 0; i < count; i++) {
                offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
-               if (offset < namevalue_offset)
+               if (offset <= namevalue_offset)
                        le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
                                     namevalue_size);
        }
@@ -1755,6 +1655,8 @@ static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
  * storage and unindexed ocfs2_xattr_blocks.
  */
 static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
+       .xlo_journal_access     = ocfs2_xa_block_journal_access,
+       .xlo_journal_dirty      = ocfs2_xa_block_journal_dirty,
        .xlo_offset_pointer     = ocfs2_xa_block_offset_pointer,
        .xlo_check_space        = ocfs2_xa_block_check_space,
        .xlo_can_reuse          = ocfs2_xa_block_can_reuse,
@@ -1765,6 +1667,22 @@ static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
        .xlo_fill_value_buf     = ocfs2_xa_block_fill_value_buf,
 };
 
+static int ocfs2_xa_bucket_journal_access(handle_t *handle,
+                                         struct ocfs2_xa_loc *loc, int type)
+{
+       struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
+
+       return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
+}
+
+static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
+                                         struct ocfs2_xa_loc *loc)
+{
+       struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
+
+       ocfs2_xattr_bucket_journal_dirty(handle, bucket);
+}
+
 static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
                                            int offset)
 {
@@ -1772,8 +1690,8 @@ static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
        int block, block_offset;
 
        /* The header is at the front of the bucket */
-       block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits;
-       block_offset = offset % bucket->bu_inode->i_sb->s_blocksize;
+       block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
+       block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
 
        return bucket_block(bucket, block) + block_offset;
 }
@@ -1813,8 +1731,7 @@ static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
        int free_start = ocfs2_xa_get_free_start(loc);
        int needed_space = ocfs2_xi_entry_usage(xi);
        int size = namevalue_size_xi(xi);
-       struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
-       struct super_block *sb = bucket->bu_inode->i_sb;
+       struct super_block *sb = loc->xl_inode->i_sb;
 
        /*
         * Bucket storage does not reclaim name+value pairs it cannot
@@ -1896,8 +1813,7 @@ static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
 {
        int free_start = ocfs2_xa_get_free_start(loc);
        struct ocfs2_xattr_header *xh = loc->xl_header;
-       struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
-       struct super_block *sb = bucket->bu_inode->i_sb;
+       struct super_block *sb = loc->xl_inode->i_sb;
        int nameval_offset;
 
        free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
@@ -1912,7 +1828,7 @@ static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
                                           struct ocfs2_xattr_value_buf *vb)
 {
        struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
-       struct super_block *sb = bucket->bu_inode->i_sb;
+       struct super_block *sb = loc->xl_inode->i_sb;
        int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
        int size = namevalue_size_xe(loc->xl_entry);
        int block_offset = nameval_offset >> sb->s_blocksize_bits;
@@ -1929,6 +1845,8 @@ static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
 
 /* Operations for xattrs stored in buckets. */
 static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
+       .xlo_journal_access     = ocfs2_xa_bucket_journal_access,
+       .xlo_journal_dirty      = ocfs2_xa_bucket_journal_dirty,
        .xlo_offset_pointer     = ocfs2_xa_bucket_offset_pointer,
        .xlo_check_space        = ocfs2_xa_bucket_check_space,
        .xlo_can_reuse          = ocfs2_xa_bucket_can_reuse,
@@ -1939,6 +1857,44 @@ static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
        .xlo_fill_value_buf     = ocfs2_xa_bucket_fill_value_buf,
 };
 
+static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
+{
+       struct ocfs2_xattr_value_buf vb;
+
+       if (ocfs2_xattr_is_local(loc->xl_entry))
+               return 0;
+
+       ocfs2_xa_fill_value_buf(loc, &vb);
+       return le32_to_cpu(vb.vb_xv->xr_clusters);
+}
+
+static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
+                                  struct ocfs2_xattr_set_ctxt *ctxt)
+{
+       int trunc_rc, access_rc;
+       struct ocfs2_xattr_value_buf vb;
+
+       ocfs2_xa_fill_value_buf(loc, &vb);
+       trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
+                                             ctxt);
+
+       /*
+        * The caller of ocfs2_xa_value_truncate() has already called
+        * ocfs2_xa_journal_access on the loc.  However, The truncate code
+        * calls ocfs2_extend_trans().  This may commit the previous
+        * transaction and open a new one.  If this is a bucket, truncate
+        * could leave only vb->vb_bh set up for journaling.  Meanwhile,
+        * the caller is expecting to dirty the entire bucket.  So we must
+        * reset the journal work.  We do this even if truncate has failed,
+        * as it could have failed after committing the extend.
+        */
+       access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
+                                           OCFS2_JOURNAL_ACCESS_WRITE);
+
+       /* Errors in truncate take precedence */
+       return trunc_rc ? trunc_rc : access_rc;
+}
+
 static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
 {
        int index, count;
@@ -1967,27 +1923,177 @@ static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
 }
 
 /*
+ * If we have a problem adjusting the size of an external value during
+ * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
+ * in an intermediate state.  For example, the value may be partially
+ * truncated.
+ *
+ * If the value tree hasn't changed, the extend/truncate went nowhere.
+ * We have nothing to do.  The caller can treat it as a straight error.
+ *
+ * If the value tree got partially truncated, we now have a corrupted
+ * extended attribute.  We're going to wipe its entry and leak the
+ * clusters.  Better to leak some storage than leave a corrupt entry.
+ *
+ * If the value tree grew, it obviously didn't grow enough for the
+ * new entry.  We're not going to try and reclaim those clusters either.
+ * If there was already an external value there (orig_clusters != 0),
+ * the new clusters are attached safely and we can just leave the old
+ * value in place.  If there was no external value there, we remove
+ * the entry.
+ *
+ * This way, the xattr block we store in the journal will be consistent.
+ * If the size change broke because of the journal, no changes will hit
+ * disk anyway.
+ */
+static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
+                                           const char *what,
+                                           unsigned int orig_clusters)
+{
+       unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
+       char *nameval_buf = ocfs2_xa_offset_pointer(loc,
+                               le16_to_cpu(loc->xl_entry->xe_name_offset));
+
+       if (new_clusters < orig_clusters) {
+               mlog(ML_ERROR,
+                    "Partial truncate while %s xattr %.*s.  Leaking "
+                    "%u clusters and removing the entry\n",
+                    what, loc->xl_entry->xe_name_len, nameval_buf,
+                    orig_clusters - new_clusters);
+               ocfs2_xa_remove_entry(loc);
+       } else if (!orig_clusters) {
+               mlog(ML_ERROR,
+                    "Unable to allocate an external value for xattr "
+                    "%.*s safely.  Leaking %u clusters and removing the "
+                    "entry\n",
+                    loc->xl_entry->xe_name_len, nameval_buf,
+                    new_clusters - orig_clusters);
+               ocfs2_xa_remove_entry(loc);
+       } else if (new_clusters > orig_clusters)
+               mlog(ML_ERROR,
+                    "Unable to grow xattr %.*s safely.  %u new clusters "
+                    "have been added, but the value will not be "
+                    "modified\n",
+                    loc->xl_entry->xe_name_len, nameval_buf,
+                    new_clusters - orig_clusters);
+}
+
+static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
+                          struct ocfs2_xattr_set_ctxt *ctxt)
+{
+       int rc = 0;
+       unsigned int orig_clusters;
+
+       if (!ocfs2_xattr_is_local(loc->xl_entry)) {
+               orig_clusters = ocfs2_xa_value_clusters(loc);
+               rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
+               if (rc) {
+                       mlog_errno(rc);
+                       /*
+                        * Since this is remove, we can return 0 if
+                        * ocfs2_xa_cleanup_value_truncate() is going to
+                        * wipe the entry anyway.  So we check the
+                        * cluster count as well.
+                        */
+                       if (orig_clusters != ocfs2_xa_value_clusters(loc))
+                               rc = 0;
+                       ocfs2_xa_cleanup_value_truncate(loc, "removing",
+                                                       orig_clusters);
+                       if (rc)
+                               goto out;
+               }
+       }
+
+       ocfs2_xa_remove_entry(loc);
+
+out:
+       return rc;
+}
+
+static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
+{
+       int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
+       char *nameval_buf;
+
+       nameval_buf = ocfs2_xa_offset_pointer(loc,
+                               le16_to_cpu(loc->xl_entry->xe_name_offset));
+       memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
+}
+
+/*
+ * Take an existing entry and make it ready for the new value.  This
+ * won't allocate space, but it may free space.  It should be ready for
+ * ocfs2_xa_prepare_entry() to finish the work.
+ */
+static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
+                               struct ocfs2_xattr_info *xi,
+                               struct ocfs2_xattr_set_ctxt *ctxt)
+{
+       int rc = 0;
+       int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
+       unsigned int orig_clusters;
+       char *nameval_buf;
+       int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
+       int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
+
+       BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
+              name_size);
+
+       nameval_buf = ocfs2_xa_offset_pointer(loc,
+                               le16_to_cpu(loc->xl_entry->xe_name_offset));
+       if (xe_local) {
+               memset(nameval_buf + name_size, 0,
+                      namevalue_size_xe(loc->xl_entry) - name_size);
+               if (!xi_local)
+                       ocfs2_xa_install_value_root(loc);
+       } else {
+               orig_clusters = ocfs2_xa_value_clusters(loc);
+               if (xi_local) {
+                       rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
+                       if (rc < 0)
+                               mlog_errno(rc);
+                       else
+                               memset(nameval_buf + name_size, 0,
+                                      namevalue_size_xe(loc->xl_entry) -
+                                      name_size);
+               } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
+                          xi->xi_value_len) {
+                       rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
+                                                    ctxt);
+                       if (rc < 0)
+                               mlog_errno(rc);
+               }
+
+               if (rc) {
+                       ocfs2_xa_cleanup_value_truncate(loc, "reusing",
+                                                       orig_clusters);
+                       goto out;
+               }
+       }
+
+       loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
+       ocfs2_xattr_set_local(loc->xl_entry, xi_local);
+
+out:
+       return rc;
+}
+
+/*
  * Prepares loc->xl_entry to receive the new xattr.  This includes
  * properly setting up the name+value pair region.  If loc->xl_entry
  * already exists, it will take care of modifying it appropriately.
- * This also includes deleting entries, but don't call this to remove
- * a non-existant entry.  That's just a bug.
  *
  * Note that this modifies the data.  You did journal_access already,
  * right?
  */
 static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
                                  struct ocfs2_xattr_info *xi,
-                                 u32 name_hash)
+                                 u32 name_hash,
+                                 struct ocfs2_xattr_set_ctxt *ctxt)
 {
        int rc = 0;
-       int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
-       char *nameval_buf;
-
-       if (!xi->xi_value) {
-               ocfs2_xa_remove_entry(loc);
-               goto out;
-       }
+       unsigned int orig_clusters;
+       __le64 orig_value_size = 0;
 
        rc = ocfs2_xa_check_space(loc, xi);
        if (rc)
@@ -1995,15 +2101,24 @@ static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
 
        if (loc->xl_entry) {
                if (ocfs2_xa_can_reuse_entry(loc, xi)) {
-                       nameval_buf = ocfs2_xa_offset_pointer(loc,
-                               le16_to_cpu(loc->xl_entry->xe_name_offset));
-                       memset(nameval_buf + name_size, 0,
-                              namevalue_size_xe(loc->xl_entry) - name_size);
-                       loc->xl_entry->xe_value_size =
-                               cpu_to_le64(xi->xi_value_len);
-                       goto out;
+                       orig_value_size = loc->xl_entry->xe_value_size;
+                       rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
+                       if (rc)
+                               goto out;
+                       goto alloc_value;
                }
 
+               if (!ocfs2_xattr_is_local(loc->xl_entry)) {
+                       orig_clusters = ocfs2_xa_value_clusters(loc);
+                       rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
+                       if (rc) {
+                               mlog_errno(rc);
+                               ocfs2_xa_cleanup_value_truncate(loc,
+                                                               "overwriting",
+                                                               orig_clusters);
+                               goto out;
+                       }
+               }
                ocfs2_xa_wipe_namevalue(loc);
        } else
                ocfs2_xa_add_entry(loc, name_hash);
@@ -2013,33 +2128,106 @@ static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
         * name+value pair back from the end.
         */
        ocfs2_xa_add_namevalue(loc, xi);
+       if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
+               ocfs2_xa_install_value_root(loc);
+
+alloc_value:
+       if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
+               orig_clusters = ocfs2_xa_value_clusters(loc);
+               rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
+               if (rc < 0) {
+                       ctxt->set_abort = 1;
+                       ocfs2_xa_cleanup_value_truncate(loc, "growing",
+                                                       orig_clusters);
+                       /*
+                        * If we were growing an existing value,
+                        * ocfs2_xa_cleanup_value_truncate() won't remove
+                        * the entry. We need to restore the original value
+                        * size.
+                        */
+                       if (loc->xl_entry) {
+                               BUG_ON(!orig_value_size);
+                               loc->xl_entry->xe_value_size = orig_value_size;
+                       }
+                       mlog_errno(rc);
+               }
+       }
 
 out:
        return rc;
 }
 
 /*
- * Store the value portion of the name+value pair.  This is either an
- * inline value or the tree root of an external value.
+ * Store the value portion of the name+value pair.  This will skip
+ * values that are stored externally.  Their tree roots were set up
+ * by ocfs2_xa_prepare_entry().
  */
-static void ocfs2_xa_store_inline_value(struct ocfs2_xa_loc *loc,
-                                       struct ocfs2_xattr_info *xi)
+static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
+                               struct ocfs2_xattr_info *xi,
+                               struct ocfs2_xattr_set_ctxt *ctxt)
 {
+       int rc = 0;
        int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
        int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
-       int inline_value_size = namevalue_size_xi(xi) - name_size;
-       const void *value = xi->xi_value;
        char *nameval_buf;
+       struct ocfs2_xattr_value_buf vb;
 
-       if (!xi->xi_value)
-               return;
-
+       nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
        if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
-               value = &def_xv;
-               inline_value_size = OCFS2_XATTR_ROOT_SIZE;
+               ocfs2_xa_fill_value_buf(loc, &vb);
+               rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
+                                                    ctxt->handle, &vb,
+                                                    xi->xi_value,
+                                                    xi->xi_value_len);
+       } else
+               memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
+
+       return rc;
+}
+
+static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
+                       struct ocfs2_xattr_info *xi,
+                       struct ocfs2_xattr_set_ctxt *ctxt)
+{
+       int ret;
+       u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
+                                             xi->xi_name_len);
+
+       ret = ocfs2_xa_journal_access(ctxt->handle, loc,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
        }
-       nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
-       memcpy(nameval_buf + name_size, value, inline_value_size);
+
+       /*
+        * From here on out, everything is going to modify the buffer a
+        * little.  Errors are going to leave the xattr header in a
+        * sane state.  Thus, even with errors we dirty the sucker.
+        */
+
+       /* Don't worry, we are never called with !xi_value and !xl_entry */
+       if (!xi->xi_value) {
+               ret = ocfs2_xa_remove(loc, ctxt);
+               goto out_dirty;
+       }
+
+       ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
+       if (ret) {
+               if (ret != -ENOSPC)
+                       mlog_errno(ret);
+               goto out_dirty;
+       }
+
+       ret = ocfs2_xa_store_value(loc, xi, ctxt);
+       if (ret)
+               mlog_errno(ret);
+
+out_dirty:
+       ocfs2_xa_journal_dirty(ctxt->handle, loc);
+
+out:
+       return ret;
 }
 
 static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
@@ -2049,22 +2237,20 @@ static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
 {
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
 
+       BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
+
+       loc->xl_inode = inode;
        loc->xl_ops = &ocfs2_xa_block_loc_ops;
        loc->xl_storage = bh;
        loc->xl_entry = entry;
-
-       if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
-               loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
-       else {
-               BUG_ON(entry);
-               loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
-       }
+       loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
        loc->xl_header =
                (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
                                              loc->xl_size);
 }
 
 static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
+                                         struct inode *inode,
                                          struct buffer_head *bh,
                                          struct ocfs2_xattr_entry *entry)
 {
@@ -2073,6 +2259,7 @@ static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
 
        BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
 
+       loc->xl_inode = inode;
        loc->xl_ops = &ocfs2_xa_block_loc_ops;
        loc->xl_storage = bh;
        loc->xl_header = &(xb->xb_attrs.xb_header);
@@ -2085,6 +2272,7 @@ static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
                                           struct ocfs2_xattr_bucket *bucket,
                                           struct ocfs2_xattr_entry *entry)
 {
+       loc->xl_inode = bucket->bu_inode;
        loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
        loc->xl_storage = bucket;
        loc->xl_header = bucket_xh(bucket);
@@ -2092,235 +2280,6 @@ static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
        loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
 }
 
-
-/*
- * ocfs2_xattr_set_entry()
- *
- * Set extended attribute entry into inode or block.
- *
- * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
- * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
- * then set value in B tree with set_value_outside().
- */
-static int ocfs2_xattr_set_entry(struct inode *inode,
-                                struct ocfs2_xattr_info *xi,
-                                struct ocfs2_xattr_search *xs,
-                                struct ocfs2_xattr_set_ctxt *ctxt,
-                                int flag)
-{
-       struct ocfs2_xattr_entry *last;
-       struct ocfs2_inode_info *oi = OCFS2_I(inode);
-       struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
-       size_t min_offs = xs->end - xs->base;
-       size_t size_l = 0;
-       handle_t *handle = ctxt->handle;
-       int free, i, ret;
-       u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
-                                             xi->xi_name_len);
-       struct ocfs2_xa_loc loc;
-       struct ocfs2_xattr_value_buf vb = {
-               .vb_bh = xs->xattr_bh,
-               .vb_access = ocfs2_journal_access_di,
-       };
-
-       if (!(flag & OCFS2_INLINE_XATTR_FL)) {
-               BUG_ON(xs->xattr_bh == xs->inode_bh);
-               vb.vb_access = ocfs2_journal_access_xb;
-       } else
-               BUG_ON(xs->xattr_bh != xs->inode_bh);
-
-       /* Compute min_offs, last and free space. */
-       last = xs->header->xh_entries;
-
-       for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
-               size_t offs = le16_to_cpu(last->xe_name_offset);
-               if (offs < min_offs)
-                       min_offs = offs;
-               last += 1;
-       }
-
-       free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
-       if (free < 0)
-               return -EIO;
-
-       if (!xs->not_found)
-               free += ocfs2_xe_entry_usage(xs->here);
-
-       /* Check free space in inode or block */
-       if (xi->xi_value && (free < ocfs2_xi_entry_usage(xi))) {
-               ret = -ENOSPC;
-               goto out;
-       }
-
-       if (!xs->not_found) {
-               /* For existing extended attribute */
-               size_t size = namevalue_size_xe(xs->here);
-               size_t offs = le16_to_cpu(xs->here->xe_name_offset);
-               void *val = xs->base + offs;
-
-               if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
-                       /* Replace existing local xattr with tree root */
-                       ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
-                                                           ctxt, &vb, offs);
-                       if (ret < 0)
-                               mlog_errno(ret);
-                       goto out;
-               } else if (!ocfs2_xattr_is_local(xs->here)) {
-                       /* For existing xattr which has value outside */
-                       vb.vb_xv = (struct ocfs2_xattr_value_root *)
-                               (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
-
-                       if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
-                               /*
-                                * If new value need set outside also,
-                                * first truncate old value to new value,
-                                * then set new value with set_value_outside().
-                                */
-                               ret = ocfs2_xattr_value_truncate(inode,
-                                                       &vb,
-                                                       xi->xi_value_len,
-                                                       ctxt);
-                               if (ret < 0) {
-                                       mlog_errno(ret);
-                                       goto out;
-                               }
-
-                               ret = ocfs2_xattr_update_entry(inode,
-                                                              handle,
-                                                              xi,
-                                                              xs,
-                                                              &vb,
-                                                              offs);
-                               if (ret < 0) {
-                                       mlog_errno(ret);
-                                       goto out;
-                               }
-
-                               ret = __ocfs2_xattr_set_value_outside(inode,
-                                                       handle,
-                                                       &vb,
-                                                       xi->xi_value,
-                                                       xi->xi_value_len);
-                               if (ret < 0)
-                                       mlog_errno(ret);
-                               goto out;
-                       } else {
-                               /*
-                                * If new value need set in local,
-                                * just trucate old value to zero.
-                                */
-                                ret = ocfs2_xattr_value_truncate(inode,
-                                                                 &vb,
-                                                                 0,
-                                                                 ctxt);
-                               if (ret < 0)
-                                       mlog_errno(ret);
-                       }
-               }
-       }
-
-       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
-                                     OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       if (!(flag & OCFS2_INLINE_XATTR_FL)) {
-               ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
-                                  OCFS2_JOURNAL_ACCESS_WRITE);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
-       }
-
-       if (xs->xattr_bh == xs->inode_bh)
-               ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
-                                        xs->not_found ? NULL : xs->here);
-       else
-               ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh,
-                                             xs->not_found ? NULL : xs->here);
-
-       /*
-        * Prepare our entry and insert the inline value.  This will
-        * be a value tree root for values that are larger than
-        * OCFS2_XATTR_INLINE_SIZE.
-        */
-       ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
-       if (ret) {
-               if (ret != -ENOSPC)
-                       mlog_errno(ret);
-               goto out;
-       }
-       /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
-       BUG_ON(ret == -ENOSPC);
-       ocfs2_xa_store_inline_value(&loc, xi);
-       xs->here = loc.xl_entry;
-
-       if (!(flag & OCFS2_INLINE_XATTR_FL)) {
-               ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
-               if (ret < 0) {
-                       mlog_errno(ret);
-                       goto out;
-               }
-       }
-
-       if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
-           (flag & OCFS2_INLINE_XATTR_FL)) {
-               struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
-               unsigned int xattrsize = osb->s_xattr_inline_size;
-
-               /*
-                * Adjust extent record count or inline data size
-                * to reserve space for extended attribute.
-                */
-               if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
-                       struct ocfs2_inline_data *idata = &di->id2.i_data;
-                       le16_add_cpu(&idata->id_count, -xattrsize);
-               } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
-                       struct ocfs2_extent_list *el = &di->id2.i_list;
-                       le16_add_cpu(&el->l_count, -(xattrsize /
-                                       sizeof(struct ocfs2_extent_rec)));
-               }
-               di->i_xattr_inline_size = cpu_to_le16(xattrsize);
-       }
-       /* Update xattr flag */
-       spin_lock(&oi->ip_lock);
-       oi->ip_dyn_features |= flag;
-       di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
-       spin_unlock(&oi->ip_lock);
-
-       ret = ocfs2_journal_dirty(handle, xs->inode_bh);
-       if (ret < 0)
-               mlog_errno(ret);
-
-       if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
-               /*
-                * Set value outside in B tree.
-                * This is the second step for value size > INLINE_SIZE.
-                */
-               size_t offs = le16_to_cpu(xs->here->xe_name_offset);
-               ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
-                                                   &vb, offs);
-               if (ret < 0) {
-                       int ret2;
-
-                       mlog_errno(ret);
-                       /*
-                        * If set value outside failed, we have to clean
-                        * the junk tree root we have already set in local.
-                        */
-                       ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
-                                                  xi, xs, &vb, offs);
-                       if (ret2 < 0)
-                               mlog_errno(ret2);
-               }
-       }
-out:
-       return ret;
-}
-
 /*
  * In xattr remove, if it is stored outside and refcounted, we may have
  * the chance to split the refcount tree. So need the allocators.
@@ -2512,7 +2471,10 @@ static int ocfs2_xattr_free_block(struct inode *inode,
        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);
+       if (xb->xb_suballoc_loc)
+               bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
+       else
+               bg_blkno = ocfs2_which_suballoc_group(blk, bit);
 
        xb_alloc_inode = ocfs2_get_system_file_inode(osb,
                                EXTENT_ALLOC_SYSTEM_INODE,
@@ -2627,9 +2589,7 @@ int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
        di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
        spin_unlock(&oi->ip_lock);
 
-       ret = ocfs2_journal_dirty(handle, di_bh);
-       if (ret < 0)
-               mlog_errno(ret);
+       ocfs2_journal_dirty(handle, di_bh);
 out_commit:
        ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
 out:
@@ -2716,6 +2676,53 @@ static int ocfs2_xattr_ibody_find(struct inode *inode,
        return 0;
 }
 
+static int ocfs2_xattr_ibody_init(struct inode *inode,
+                                 struct buffer_head *di_bh,
+                                 struct ocfs2_xattr_set_ctxt *ctxt)
+{
+       int ret;
+       struct ocfs2_inode_info *oi = OCFS2_I(inode);
+       struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
+       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       unsigned int xattrsize = osb->s_xattr_inline_size;
+
+       if (!ocfs2_xattr_has_space_inline(inode, di)) {
+               ret = -ENOSPC;
+               goto out;
+       }
+
+       ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
+                                     OCFS2_JOURNAL_ACCESS_WRITE);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
+
+       /*
+        * Adjust extent record count or inline data size
+        * to reserve space for extended attribute.
+        */
+       if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
+               struct ocfs2_inline_data *idata = &di->id2.i_data;
+               le16_add_cpu(&idata->id_count, -xattrsize);
+       } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
+               struct ocfs2_extent_list *el = &di->id2.i_list;
+               le16_add_cpu(&el->l_count, -(xattrsize /
+                                            sizeof(struct ocfs2_extent_rec)));
+       }
+       di->i_xattr_inline_size = cpu_to_le16(xattrsize);
+
+       spin_lock(&oi->ip_lock);
+       oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
+       di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
+       spin_unlock(&oi->ip_lock);
+
+       ocfs2_journal_dirty(ctxt->handle, di_bh);
+
+out:
+       return ret;
+}
+
 /*
  * ocfs2_xattr_ibody_set()
  *
@@ -2727,9 +2734,10 @@ static int ocfs2_xattr_ibody_set(struct inode *inode,
                                 struct ocfs2_xattr_search *xs,
                                 struct ocfs2_xattr_set_ctxt *ctxt)
 {
+       int ret;
        struct ocfs2_inode_info *oi = OCFS2_I(inode);
        struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
-       int ret;
+       struct ocfs2_xa_loc loc;
 
        if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
                return -ENOSPC;
@@ -2742,8 +2750,25 @@ static int ocfs2_xattr_ibody_set(struct inode *inode,
                }
        }
 
-       ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
-                               (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
+       if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
+               ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
+               if (ret) {
+                       if (ret != -ENOSPC)
+                               mlog_errno(ret);
+                       goto out;
+               }
+       }
+
+       ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
+                                xs->not_found ? NULL : xs->here);
+       ret = ocfs2_xa_set(&loc, xi, ctxt);
+       if (ret) {
+               if (ret != -ENOSPC)
+                       mlog_errno(ret);
+               goto out;
+       }
+       xs->here = loc.xl_entry;
+
 out:
        up_write(&oi->ip_alloc_sem);
 
@@ -2803,32 +2828,30 @@ cleanup:
        return ret;
 }
 
-static int ocfs2_create_xattr_block(handle_t *handle,
-                                   struct inode *inode,
+static int ocfs2_create_xattr_block(struct inode *inode,
                                    struct buffer_head *inode_bh,
-                                   struct ocfs2_alloc_context *meta_ac,
-                                   struct buffer_head **ret_bh,
-                                   int indexed)
+                                   struct ocfs2_xattr_set_ctxt *ctxt,
+                                   int indexed,
+                                   struct buffer_head **ret_bh)
 {
        int ret;
        u16 suballoc_bit_start;
        u32 num_got;
-       u64 first_blkno;
+       u64 suballoc_loc, first_blkno;
        struct ocfs2_dinode *di =  (struct ocfs2_dinode *)inode_bh->b_data;
-       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct buffer_head *new_bh = NULL;
        struct ocfs2_xattr_block *xblk;
 
-       ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
-                                     OCFS2_JOURNAL_ACCESS_CREATE);
+       ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
+                                     inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret < 0) {
                mlog_errno(ret);
                goto end;
        }
 
-       ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
-                                  &suballoc_bit_start, &num_got,
-                                  &first_blkno);
+       ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
+                                  &suballoc_loc, &suballoc_bit_start,
+                                  &num_got, &first_blkno);
        if (ret < 0) {
                mlog_errno(ret);
                goto end;
@@ -2837,7 +2860,7 @@ static int ocfs2_create_xattr_block(handle_t *handle,
        new_bh = sb_getblk(inode->i_sb, first_blkno);
        ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
 
-       ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
+       ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
                                      new_bh,
                                      OCFS2_JOURNAL_ACCESS_CREATE);
        if (ret < 0) {
@@ -2849,11 +2872,12 @@ static int ocfs2_create_xattr_block(handle_t *handle,
        xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
        memset(xblk, 0, inode->i_sb->s_blocksize);
        strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
-       xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
+       xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
+       xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
        xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
-       xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
+       xblk->xb_fs_generation =
+               cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
        xblk->xb_blkno = cpu_to_le64(first_blkno);
-
        if (indexed) {
                struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
                xr->xt_clusters = cpu_to_le32(1);
@@ -2864,14 +2888,17 @@ static int ocfs2_create_xattr_block(handle_t *handle,
                xr->xt_list.l_next_free_rec = cpu_to_le16(1);
                xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
        }
+       ocfs2_journal_dirty(ctxt->handle, new_bh);
 
-       ret = ocfs2_journal_dirty(handle, new_bh);
-       if (ret < 0) {
-               mlog_errno(ret);
-               goto end;
-       }
+       /* Add it to the inode */
        di->i_xattr_loc = cpu_to_le64(first_blkno);
-       ocfs2_journal_dirty(handle, inode_bh);
+
+       spin_lock(&OCFS2_I(inode)->ip_lock);
+       OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
+       di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
+       spin_unlock(&OCFS2_I(inode)->ip_lock);
+
+       ocfs2_journal_dirty(ctxt->handle, inode_bh);
 
        *ret_bh = new_bh;
        new_bh = NULL;
@@ -2893,13 +2920,13 @@ static int ocfs2_xattr_block_set(struct inode *inode,
                                 struct ocfs2_xattr_set_ctxt *ctxt)
 {
        struct buffer_head *new_bh = NULL;
-       handle_t *handle = ctxt->handle;
        struct ocfs2_xattr_block *xblk = NULL;
        int ret;
+       struct ocfs2_xa_loc loc;
 
        if (!xs->xattr_bh) {
-               ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
-                                              ctxt->meta_ac, &new_bh, 0);
+               ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
+                                              0, &new_bh);
                if (ret) {
                        mlog_errno(ret);
                        goto end;
@@ -2915,21 +2942,25 @@ static int ocfs2_xattr_block_set(struct inode *inode,
                xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
 
        if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
-               /* Set extended attribute into external block */
-               ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
-                                           OCFS2_HAS_XATTR_FL);
-               if (!ret || ret != -ENOSPC)
-                       goto end;
+               ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
+                                             xs->not_found ? NULL : xs->here);
 
-               ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
-               if (ret)
+               ret = ocfs2_xa_set(&loc, xi, ctxt);
+               if (!ret)
+                       xs->here = loc.xl_entry;
+               else if ((ret != -ENOSPC) || ctxt->set_abort)
                        goto end;
+               else {
+                       ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
+                       if (ret)
+                               goto end;
+               }
        }
 
-       ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
+       if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
+               ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
 
 end:
-
        return ret;
 }
 
@@ -3273,14 +3304,13 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
                                goto out;
                        }
 
-                       ret = ocfs2_extend_trans(ctxt->handle, credits +
-                                       ctxt->handle->h_buffer_credits);
+                       ret = ocfs2_extend_trans(ctxt->handle, credits);
                        if (ret) {
                                mlog_errno(ret);
                                goto out;
                        }
                        ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
-               } else if (ret == -ENOSPC) {
+               } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
                        if (di->i_xattr_loc && !xbs->xattr_bh) {
                                ret = ocfs2_xattr_block_find(inode,
                                                             xi->xi_name_index,
@@ -3304,8 +3334,7 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
                                        goto out;
                                }
 
-                               ret = ocfs2_extend_trans(ctxt->handle, credits +
-                                       ctxt->handle->h_buffer_credits);
+                               ret = ocfs2_extend_trans(ctxt->handle, credits);
                                if (ret) {
                                        mlog_errno(ret);
                                        goto out;
@@ -3339,8 +3368,7 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
                                        goto out;
                                }
 
-                               ret = ocfs2_extend_trans(ctxt->handle, credits +
-                                               ctxt->handle->h_buffer_credits);
+                               ret = ocfs2_extend_trans(ctxt->handle, credits);
                                if (ret) {
                                        mlog_errno(ret);
                                        goto out;
@@ -4210,7 +4238,6 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
        u32 bit_off, len;
        u64 blkno;
        handle_t *handle = ctxt->handle;
-       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct ocfs2_inode_info *oi = OCFS2_I(inode);
        struct buffer_head *xb_bh = xs->xattr_bh;
        struct ocfs2_xattr_block *xb =
@@ -4238,7 +4265,7 @@ static int ocfs2_xattr_create_index_block(struct inode *inode,
                goto out;
        }
 
-       ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
+       ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
                                     1, 1, &bit_off, &len);
        if (ret) {
                mlog_errno(ret);
@@ -4848,8 +4875,7 @@ static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
         * We need to update the first bucket of the old extent and all
         * the buckets going to the new extent.
         */
-       credits = ((num_buckets + 1) * blks_per_bucket) +
-               handle->h_buffer_credits;
+       credits = ((num_buckets + 1) * blks_per_bucket);
        ret = ocfs2_extend_trans(handle, credits);
        if (ret) {
                mlog_errno(ret);
@@ -4919,7 +4945,7 @@ static int ocfs2_divide_xattr_cluster(struct inode *inode,
                                      u32 *first_hash)
 {
        u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
-       int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
+       int ret, credits = 2 * blk_per_bucket;
 
        BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
 
@@ -5060,7 +5086,7 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
                goto leave;
        }
 
-       ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
+       ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
                                     clusters_to_add, &bit_off, &num_bits);
        if (ret < 0) {
                if (ret != -ENOSPC)
@@ -5114,9 +5140,7 @@ static int ocfs2_add_new_xattr_cluster(struct inode *inode,
                goto leave;
        }
 
-       ret = ocfs2_journal_dirty(handle, root_bh);
-       if (ret < 0)
-               mlog_errno(ret);
+       ocfs2_journal_dirty(handle, root_bh);
 
 leave:
        return ret;
@@ -5161,8 +5185,7 @@ static int ocfs2_extend_xattr_bucket(struct inode *inode,
         * existing bucket.  Then we add the last existing bucket, the
         * new bucket, and the first bucket (3 * blk_per_bucket).
         */
-       credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
-                 handle->h_buffer_credits;
+       credits = (end_blk - target_blk) + (3 * blk_per_bucket);
        ret = ocfs2_extend_trans(handle, credits);
        if (ret) {
                mlog_errno(ret);
@@ -5297,61 +5320,6 @@ static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
 }
 
 /*
- * Set the xattr entry in the specified bucket.
- * The bucket is indicated by xs->bucket and it should have the enough
- * space for the xattr insertion.
- */
-static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
-                                          handle_t *handle,
-                                          struct ocfs2_xattr_info *xi,
-                                          struct ocfs2_xattr_search *xs,
-                                          u32 name_hash)
-{
-       int ret;
-       u64 blkno;
-       struct ocfs2_xa_loc loc;
-
-       mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
-            (unsigned long)xi->xi_value_len, xi->xi_name_index,
-            (unsigned long long)bucket_blkno(xs->bucket));
-
-       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;
-               }
-       }
-
-       ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
-                                               OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret < 0) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
-                                      xs->not_found ? NULL : xs->here);
-       ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
-       if (ret) {
-               if (ret != -ENOSPC)
-                       mlog_errno(ret);
-               goto out;
-       }
-       /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
-       BUG_ON(ret == -ENOSPC);
-       ocfs2_xa_store_inline_value(&loc, xi);
-       xs->here = loc.xl_entry;
-
-       ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
-
-out:
-       return ret;
-}
-
-/*
  * Truncate the specified xe_off entry in xattr bucket.
  * bucket is indicated by header_bh and len is the new length.
  * Both the ocfs2_xattr_value_root and the entry will be updated here.
@@ -5421,66 +5389,6 @@ out:
        return ret;
 }
 
-static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
-                                       struct ocfs2_xattr_search *xs,
-                                       int len,
-                                       struct ocfs2_xattr_set_ctxt *ctxt)
-{
-       int ret, offset;
-       struct ocfs2_xattr_entry *xe = xs->here;
-       struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
-
-       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,
-                                               offset, len, ctxt);
-       if (ret)
-               mlog_errno(ret);
-
-       return ret;
-}
-
-static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
-                                               handle_t *handle,
-                                               struct ocfs2_xattr_search *xs,
-                                               char *val,
-                                               int value_len)
-{
-       int ret, offset, block_off;
-       struct ocfs2_xattr_value_root *xv;
-       struct ocfs2_xattr_entry *xe = xs->here;
-       struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
-       void *base;
-       struct ocfs2_xattr_value_buf vb = {
-               .vb_access = ocfs2_journal_access,
-       };
-
-       BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
-
-       ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
-                                               xe - xh->xh_entries,
-                                               &block_off,
-                                               &offset);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       base = bucket_block(xs->bucket, block_off);
-       xv = (struct ocfs2_xattr_value_root *)(base + offset +
-                OCFS2_XATTR_SIZE(xe->xe_name_len));
-
-       vb.vb_xv = xv;
-       vb.vb_bh = xs->bucket->bu_bhs[block_off];
-       ret = __ocfs2_xattr_set_value_outside(inode, handle,
-                                             &vb, val, value_len);
-       if (ret)
-               mlog_errno(ret);
-out:
-       return ret;
-}
-
 static int ocfs2_rm_xattr_cluster(struct inode *inode,
                                  struct buffer_head *root_bh,
                                  u64 blkno,
@@ -5553,12 +5461,7 @@ static int ocfs2_rm_xattr_cluster(struct inode *inode,
        }
 
        le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
-
-       ret = ocfs2_journal_dirty(handle, root_bh);
-       if (ret) {
-               mlog_errno(ret);
-               goto out_commit;
-       }
+       ocfs2_journal_dirty(handle, root_bh);
 
        ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
        if (ret)
@@ -5579,116 +5482,6 @@ out:
        return ret;
 }
 
-static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
-                                        handle_t *handle,
-                                        struct ocfs2_xattr_search *xs)
-{
-       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;
-
-       ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
-                                               OCFS2_JOURNAL_ACCESS_WRITE);
-       if (ret) {
-               mlog_errno(ret);
-               return;
-       }
-
-       /* Remove the old entry. */
-       memmove(xs->here, xs->here + 1,
-               (void *)last - (void *)xs->here);
-       memset(last, 0, sizeof(struct ocfs2_xattr_entry));
-       le16_add_cpu(&xh->xh_count, -1);
-
-       ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
-}
-
-/*
- * Set the xattr name/value in the bucket specified in xs.
- *
- * As the new value in xi may be stored in the bucket or in an outside cluster,
- * we divide the whole process into 3 steps:
- * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
- * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
- * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
- * 4. If the clusters for the new outside value can't be allocated, we need
- *    to free the xattr we allocated in set.
- */
-static int ocfs2_xattr_set_in_bucket(struct inode *inode,
-                                    struct ocfs2_xattr_info *xi,
-                                    struct ocfs2_xattr_search *xs,
-                                    struct ocfs2_xattr_set_ctxt *ctxt)
-{
-       int ret;
-       size_t value_len;
-       char *val = (char *)xi->xi_value;
-       struct ocfs2_xattr_entry *xe = xs->here;
-       u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
-                                             xi->xi_name_len);
-
-       value_len = xi->xi_value_len;
-       if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
-               /*
-                * We need to truncate the xattr storage first.
-                *
-                * If both the old and new value are stored to
-                * outside block, we only need to truncate
-                * the storage and then set the value outside.
-                *
-                * If the new value should be stored within block,
-                * we should free all the outside block first and
-                * the modification to the xattr block will be done
-                * by following steps.
-                */
-               if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
-                       value_len = 0;
-
-               ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
-                                                          value_len,
-                                                          ctxt);
-               if (ret)
-                       goto out;
-
-               if (value_len)
-                       goto set_value_outside;
-       }
-
-       /* So we have to handle the inside block change now. */
-       ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
-                                             name_hash);
-       if (ret) {
-               mlog_errno(ret);
-               goto out;
-       }
-
-       if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
-               goto out;
-
-       /* allocate the space now for the outside block storage. */
-       ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
-                                                  value_len, ctxt);
-       if (ret) {
-               mlog_errno(ret);
-
-               if (xs->not_found) {
-                       /*
-                        * We can't allocate enough clusters for outside
-                        * storage and we have allocated xattr already,
-                        * so need to remove it.
-                        */
-                       ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
-               }
-               goto out;
-       }
-
-set_value_outside:
-       ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
-                                                  xs, val, value_len);
-out:
-       return ret;
-}
-
 /*
  * 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.
@@ -5717,156 +5510,116 @@ static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
        return 0;
 }
 
-static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
-                                            struct ocfs2_xattr_info *xi,
-                                            struct ocfs2_xattr_search *xs,
-                                            struct ocfs2_xattr_set_ctxt *ctxt)
+/*
+ * Try to set the entry in the current bucket.  If we fail, the caller
+ * will handle getting us another bucket.
+ */
+static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
+                                       struct ocfs2_xattr_info *xi,
+                                       struct ocfs2_xattr_search *xs,
+                                       struct ocfs2_xattr_set_ctxt *ctxt)
 {
-       struct ocfs2_xattr_header *xh;
-       struct ocfs2_xattr_entry *xe;
-       u16 count, header_size, xh_free_start;
-       int free, max_free, need, old;
-       size_t value_size = 0;
-       size_t blocksize = inode->i_sb->s_blocksize;
-       int ret, allocation = 0;
-
-       mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
-
-try_again:
-       xh = xs->header;
-       count = le16_to_cpu(xh->xh_count);
-       xh_free_start = le16_to_cpu(xh->xh_free_start);
-       header_size = sizeof(struct ocfs2_xattr_header) +
-                       count * sizeof(struct ocfs2_xattr_entry);
-       max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
-               le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
-
-       mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
-                       "of %u which exceed block size\n",
-                       (unsigned long long)bucket_blkno(xs->bucket),
-                       header_size);
+       int ret;
+       struct ocfs2_xa_loc loc;
 
-       if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
-               value_size = OCFS2_XATTR_ROOT_SIZE;
-       else if (xi->xi_value)
-               value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
+       mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
 
-       if (xs->not_found)
-               need = sizeof(struct ocfs2_xattr_entry) +
-                       OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
-       else {
-               need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
+       ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
+                                      xs->not_found ? NULL : xs->here);
+       ret = ocfs2_xa_set(&loc, xi, ctxt);
+       if (!ret) {
+               xs->here = loc.xl_entry;
+               goto out;
+       }
+       if (ret != -ENOSPC) {
+               mlog_errno(ret);
+               goto out;
+       }
 
-               /*
-                * We only replace the old value if the new length is smaller
-                * than the old one. Otherwise we will allocate new space in the
-                * bucket to store it.
-                */
-               xe = xs->here;
-               if (ocfs2_xattr_is_local(xe))
-                       old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
-               else
-                       old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
+       /* Ok, we need space.  Let's try defragmenting the bucket. */
+       ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
+                                       xs->bucket);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
 
-               if (old >= value_size)
-                       need = 0;
+       ret = ocfs2_xa_set(&loc, xi, ctxt);
+       if (!ret) {
+               xs->here = loc.xl_entry;
+               goto out;
        }
+       if (ret != -ENOSPC)
+               mlog_errno(ret);
 
-       free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
-       /*
-        * We need to make sure the new name/value pair
-        * can exist in the same block.
-        */
-       if (xh_free_start % blocksize < need)
-               free -= xh_free_start % blocksize;
-
-       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)bucket_blkno(xs->bucket),
-            free, need, max_free, le16_to_cpu(xh->xh_free_start),
-            le16_to_cpu(xh->xh_name_value_len));
-
-       if (free < need ||
-           (xs->not_found &&
-            count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
-               if (need <= max_free &&
-                   count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
-                       /*
-                        * We can create the space by defragment. Since only the
-                        * name/value will be moved, the xe shouldn't be changed
-                        * in xs.
-                        */
-                       ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
-                                                       xs->bucket);
-                       if (ret) {
-                               mlog_errno(ret);
-                               goto out;
-                       }
 
-                       xh_free_start = le16_to_cpu(xh->xh_free_start);
-                       free = xh_free_start - header_size
-                               - OCFS2_XATTR_HEADER_GAP;
-                       if (xh_free_start % blocksize < need)
-                               free -= xh_free_start % blocksize;
+out:
+       mlog_exit(ret);
+       return ret;
+}
 
-                       if (free >= need)
-                               goto xattr_set;
+static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
+                                            struct ocfs2_xattr_info *xi,
+                                            struct ocfs2_xattr_search *xs,
+                                            struct ocfs2_xattr_set_ctxt *ctxt)
+{
+       int ret;
 
-                       mlog(0, "Can't get enough space for xattr insert by "
-                            "defragment. Need %u bytes, but we have %d, so "
-                            "allocate new bucket for it.\n", need, free);
-               }
+       mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
 
-               /*
-                * We have to add new buckets or clusters and one
-                * allocation should leave us enough space for insert.
-                */
-               BUG_ON(allocation);
+       ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
+       if (!ret)
+               goto out;
+       if (ret != -ENOSPC) {
+               mlog_errno(ret);
+               goto out;
+       }
 
-               /*
-                * We do not allow for overlapping ranges between buckets. And
-                * the maximum number of collisions we will allow for then is
-                * 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,
-                                                        xi->xi_name);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
+       /* Ack, need more space.  Let's try to get another bucket! */
 
-               ret = ocfs2_add_new_xattr_bucket(inode,
-                                                xs->xattr_bh,
+       /*
+        * We do not allow for overlapping ranges between buckets. And
+        * the maximum number of collisions we will allow for then is
+        * 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,
-                                                ctxt);
-               if (ret) {
-                       mlog_errno(ret);
-                       goto out;
-               }
+                                                xi->xi_name);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
+       }
 
-               /*
-                * ocfs2_add_new_xattr_bucket() will have updated
-                * xs->bucket if it moved, but it will not have updated
-                * any of the other search fields.  Thus, we drop it and
-                * re-search.  Everything should be cached, so it'll be
-                * quick.
-                */
-               ocfs2_xattr_bucket_relse(xs->bucket);
-               ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
-                                                  xi->xi_name_index,
-                                                  xi->xi_name, xs);
-               if (ret && ret != -ENODATA)
-                       goto out;
-               xs->not_found = ret;
-               allocation = 1;
-               goto try_again;
+       ret = ocfs2_add_new_xattr_bucket(inode,
+                                        xs->xattr_bh,
+                                        xs->bucket,
+                                        ctxt);
+       if (ret) {
+               mlog_errno(ret);
+               goto out;
        }
 
-xattr_set:
-       ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
+       /*
+        * ocfs2_add_new_xattr_bucket() will have updated
+        * xs->bucket if it moved, but it will not have updated
+        * any of the other search fields.  Thus, we drop it and
+        * re-search.  Everything should be cached, so it'll be
+        * quick.
+        */
+       ocfs2_xattr_bucket_relse(xs->bucket);
+       ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
+                                          xi->xi_name_index,
+                                          xi->xi_name, xs);
+       if (ret && ret != -ENODATA)
+               goto out;
+       xs->not_found = ret;
+
+       /* Ok, we have a new bucket, let's try again */
+       ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
+       if (ret && (ret != -ENOSPC))
+               mlog_errno(ret);
+
 out:
        mlog_exit(ret);
        return ret;
@@ -6754,33 +6507,33 @@ static int ocfs2_create_empty_xattr_block(struct inode *inode,
                                          int indexed)
 {
        int ret;
-       handle_t *handle;
-       struct ocfs2_alloc_context *meta_ac;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+       struct ocfs2_xattr_set_ctxt ctxt;
 
-       ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
+       memset(&ctxt, 0, sizeof(ctxt));
+       ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
        if (ret < 0) {
                mlog_errno(ret);
                return ret;
        }
 
-       handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
-       if (IS_ERR(handle)) {
-               ret = PTR_ERR(handle);
+       ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
+       if (IS_ERR(ctxt.handle)) {
+               ret = PTR_ERR(ctxt.handle);
                mlog_errno(ret);
                goto out;
        }
 
        mlog(0, "create new xattr block for inode %llu, index = %d\n",
             (unsigned long long)fe_bh->b_blocknr, indexed);
-       ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
-                                      meta_ac, ret_bh, indexed);
+       ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
+                                      ret_bh);
        if (ret)
                mlog_errno(ret);
 
-       ocfs2_commit_trans(osb, handle);
+       ocfs2_commit_trans(osb, ctxt.handle);
 out:
-       ocfs2_free_alloc_context(meta_ac);
+       ocfs2_free_alloc_context(ctxt.meta_ac);
        return ret;
 }
 
@@ -7161,7 +6914,7 @@ static int ocfs2_reflink_xattr_rec(struct inode *inode,
                goto out;
        }
 
-       ret = ocfs2_claim_clusters(osb, handle, data_ac,
+       ret = ocfs2_claim_clusters(handle, data_ac,
                                   len, &p_cluster, &num_clusters);
        if (ret) {
                mlog_errno(ret);
@@ -7460,7 +7213,7 @@ int ocfs2_init_security_set(handle_t *handle,
                                     xattr_ac, data_ac);
 }
 
-struct xattr_handler ocfs2_xattr_security_handler = {
+const struct xattr_handler ocfs2_xattr_security_handler = {
        .prefix = XATTR_SECURITY_PREFIX,
        .list   = ocfs2_xattr_security_list,
        .get    = ocfs2_xattr_security_get,
@@ -7504,7 +7257,7 @@ static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
                               name, value, size, flags);
 }
 
-struct xattr_handler ocfs2_xattr_trusted_handler = {
+const struct xattr_handler ocfs2_xattr_trusted_handler = {
        .prefix = XATTR_TRUSTED_PREFIX,
        .list   = ocfs2_xattr_trusted_list,
        .get    = ocfs2_xattr_trusted_get,
@@ -7560,7 +7313,7 @@ static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
                               name, value, size, flags);
 }
 
-struct xattr_handler ocfs2_xattr_user_handler = {
+const struct xattr_handler ocfs2_xattr_user_handler = {
        .prefix = XATTR_USER_PREFIX,
        .list   = ocfs2_xattr_user_list,
        .get    = ocfs2_xattr_user_get,