[Ocfs2-tools-devel] [PATCH 5/7] libocfs2: add set and get xattr operations
Tiger Yang
tiger.yang at oracle.com
Tue Oct 20 01:42:01 PDT 2009
This patch implement set and get xattr in libocfs2,
they are very similiar with the functions in kernel.
Signed-off-by: Tiger Yang <tiger.yang at oracle.com>
---
include/ocfs2/ocfs2.h | 7 +
libocfs2/ocfs2_err.et | 6 +
libocfs2/xattr.c | 2655 ++++++++++++++++++++++++++++++++++++++++++++++++-
libocfs2/xattr.h | 15 +
4 files changed, 2678 insertions(+), 5 deletions(-)
diff --git a/include/ocfs2/ocfs2.h b/include/ocfs2/ocfs2.h
index 046a2f2..20d391e 100644
--- a/include/ocfs2/ocfs2.h
+++ b/include/ocfs2/ocfs2.h
@@ -1331,5 +1331,12 @@ errcode_t ocfs2_xattr_get_clusters(ocfs2_cached_inode *cinode,
errcode_t ocfs2_xattr_value_shrink(ocfs2_filesys *fs,
struct ocfs2_xattr_value_root *xv,
uint32_t new_clusters);
+const char *ocfs2_xattr_get_prefix(int name_index);
+errcode_t ocfs2_xattr_get(ocfs2_cached_inode *ci, const char *name,
+ char *buf, size_t count, size_t *got);
+errcode_t ocfs2_xattr_set(ocfs2_cached_inode *ci,
+ const char *name,
+ const char *buf,
+ size_t count);
#endif /* _FILESYS_H */
diff --git a/libocfs2/ocfs2_err.et b/libocfs2/ocfs2_err.et
index 13b03c4..9566b5c 100644
--- a/libocfs2/ocfs2_err.et
+++ b/libocfs2/ocfs2_err.et
@@ -180,6 +180,12 @@ ec OCFS2_ET_BAD_DIR_BLOCK_MAGIC,
ec OCFS2_ET_BAD_XATTR_BLOCK_MAGIC,
"Bad magic number in xattr block"
+ec OCFS2_ET_XATTR_NODATA,
+ "No xattr data available"
+
+ec OCFS2_ET_XATTR_NOSPC,
+ "No space left for xattr"
+
ec OCFS2_ET_UNKNOWN_FEATURE,
"Unknown feature"
diff --git a/libocfs2/xattr.c b/libocfs2/xattr.c
index 0c1c68e..d3285c8 100644
--- a/libocfs2/xattr.c
+++ b/libocfs2/xattr.c
@@ -14,18 +14,82 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
+#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
+#include <assert.h>
#include "ocfs2/byteorder.h"
#include "ocfs2/ocfs2.h"
+#include "xattr.h"
+#include "extent_tree.h"
+
+#define DEBUG_XATTR 1
+#ifdef DEBUG_XATTR
+#define DEBUG(...) printf(__VA_ARGS__)
+#define ASSERT assert
+#else
+#define DEBUG(...)
+#define ASSERT
+#endif
+
+#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
+#define OCFS2_XATTR_HEADER_GAP 4
+#define HEADER_SIZE (sizeof(struct ocfs2_xattr_header))
+#define ENTRY_SIZE (sizeof(struct ocfs2_xattr_entry))
+#define bucket_xh(_b) ((struct ocfs2_xattr_header *)(_b)->xb_buf)
struct ocfs2_xattr_def_value_root {
struct ocfs2_xattr_value_root xv;
struct ocfs2_extent_rec er;
};
-#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
+static struct ocfs2_xattr_def_value_root def_xv = {
+ .xv.xr_list.l_count = 1,
+};
+
+static const char *ocfs2_xattr_prefix[OCFS2_XATTR_MAX] = {
+ [OCFS2_XATTR_INDEX_USER] = "user.",
+ [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
+ = "system.posix_acl_access",
+ [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
+ = "system.posix_acl_default",
+ [OCFS2_XATTR_INDEX_TRUSTED] = "trusted.",
+ [OCFS2_XATTR_INDEX_SECURITY] = "security.",
+};
+
+struct ocfs2_xattr_info {
+ int name_index;
+ const char *name;
+ const char *value;
+ size_t value_len;
+};
+
+struct ocfs2_xattr_buf {
+ char *xb_buf;
+ uint64_t xb_blkno;
+};
+
+struct ocfs2_xattr_search {
+ /*
+ * blkbuf point to the block buffer which contains EAs.
+ * this block may point to a inode block,
+ * a xattr block or the first block of a bucket.
+ */
+ char *blkbuf;
+ uint64_t blkno;
+ struct ocfs2_xattr_header *header;
+ struct ocfs2_xattr_entry *here;
+ char *base;
+ char *end;
+ int32_t not_found;
+};
+
+struct ocfs2_xattr_search_info {
+ struct ocfs2_xattr_search *xi_inode;
+ struct ocfs2_xattr_search *xi_block;
+ struct ocfs2_xattr_search *xi_bucket;
+};
uint32_t ocfs2_xattr_uuid_hash(unsigned char *uuid)
{
@@ -40,8 +104,8 @@ uint32_t ocfs2_xattr_uuid_hash(unsigned char *uuid)
}
uint32_t ocfs2_xattr_name_hash(uint32_t uuid_hash,
- const char *name,
- int name_len)
+ const char *name,
+ int name_len)
{
/* Get hash value of uuid from super block */
uint32_t hash = uuid_hash;
@@ -67,6 +131,14 @@ uint16_t ocfs2_blocks_per_xattr_bucket(ocfs2_filesys *fs)
return OCFS2_XATTR_BUCKET_SIZE / fs->fs_blocksize;
}
+static inline uint16_t ocfs2_xattr_max_xe_in_bucket(ocfs2_filesys *fs)
+{
+ uint16_t len = fs->fs_blocksize -
+ offsetof(struct ocfs2_xattr_header, xh_entries);
+
+ return len / ENTRY_SIZE;
+}
+
static void ocfs2_swap_xattr_entry(struct ocfs2_xattr_entry *xe)
{
xe->xe_name_hash = bswap_32(xe->xe_name_hash);
@@ -387,8 +459,7 @@ errcode_t ocfs2_xattr_get_rec(ocfs2_filesys *fs,
if (e_cpos)
*e_cpos = rec->e_cpos;
out:
- if (eb_buf)
- ocfs2_free(&eb_buf);
+ ocfs2_free(&eb_buf);
return ret;
}
@@ -505,3 +576,2577 @@ errcode_t ocfs2_write_xattr_bucket(ocfs2_filesys *fs,
ocfs2_free(&bucket);
return ret;
}
+
+const char *ocfs2_xattr_get_prefix(int name_index)
+{
+ if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
+ return ocfs2_xattr_prefix[name_index];
+ else
+ return NULL;
+}
+
+static int ocfs2_xattr_has_space_inline(ocfs2_cached_inode *ci,
+ struct ocfs2_dinode *di)
+{
+ int free = 0;
+ ocfs2_filesys *fs = ci->ci_fs;
+ uint32_t xattrsize = OCFS2_RAW_SB(fs->fs_super)->s_xattr_inline_size;
+
+ if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
+ return 0;
+
+ if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) {
+ struct ocfs2_inline_data *idata = &di->id2.i_data;
+ free = idata->id_count - di->i_size;
+ } else if (S_ISLNK(di->i_mode) && di->i_clusters == 0) {
+ free = ocfs2_fast_symlink_chars(fs->fs_blocksize) - di->i_size;
+ } else {
+ struct ocfs2_extent_list *el = &di->id2.i_list;
+ free = (el->l_count - el->l_next_free_rec) *
+ sizeof(struct ocfs2_extent_rec);
+ }
+ if (free >= xattrsize)
+ return 1;
+
+ return 0;
+}
+
+static int ocfs2_xattr_find_entry(int name_index,
+ const char *name,
+ struct ocfs2_xattr_search *xs)
+{
+ struct ocfs2_xattr_entry *entry;
+ size_t name_len;
+ int i, cmp = 1;
+
+ if (name == NULL)
+ return OCFS2_ET_INVALID_ARGUMENT;
+
+ name_len = strlen(name);
+ entry = xs->here;
+ for (i = 0; i < xs->header->xh_count; i++) {
+ cmp = name_index - ocfs2_xattr_get_type(entry);
+ if (!cmp)
+ cmp = name_len - entry->xe_name_len;
+ if (!cmp)
+ cmp = memcmp(name, xs->base + entry->xe_name_offset,
+ name_len);
+ if (cmp == 0)
+ break;
+ entry += 1;
+ }
+ xs->here = entry;
+
+ return cmp ? OCFS2_ET_XATTR_NODATA : 0;
+}
+
+static errcode_t __ocfs2_xattr_set_value_outside(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_value_root *xv,
+ char *blkbuf,
+ uint64_t blkno,
+ const char *value,
+ int value_len)
+{
+ errcode_t ret = 0;
+ int cp_len;
+ ocfs2_filesys *fs = ci->ci_fs;
+ uint16_t blocksize = fs->fs_blocksize;
+ uint16_t clustersize = fs->fs_clustersize;
+ uint32_t p_cluster, num_clusters;
+ uint32_t cpos = 0;
+ uint32_t clusters = ocfs2_clusters_in_bytes(fs, value_len);
+ uint64_t p_blkno;
+ char *blks = NULL;
+ uint32_t max_blocks = OCFS2_MAX_XATTR_TREE_LEAF_SIZE / blocksize;
+
+ ASSERT(clusters <= xv->xr_clusters);
+
+ ret = ocfs2_malloc_blocks(fs->fs_io, max_blocks, &blks);
+ if (ret)
+ return ret;
+ memset(blks, 0, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
+
+ while (cpos < clusters) {
+ ret = ocfs2_xattr_get_clusters(ci, cpos, &p_cluster,
+ &num_clusters,
+ &xv->xr_list,
+ blkno, blkbuf);
+ if (ret)
+ goto out;
+
+ p_blkno = ocfs2_clusters_to_blocks(fs, p_cluster);
+ cp_len = value_len > num_clusters * clustersize ?
+ num_clusters * clustersize : value_len;
+ memcpy(blks, value, cp_len);
+ ret = io_write_block(fs->fs_io, p_blkno,
+ (cp_len + blocksize - 1) / blocksize,
+ blks);
+ if (ret)
+ goto out;
+ value_len -= cp_len;
+ value += cp_len;
+ blks += cp_len;
+ cpos += num_clusters;
+ }
+out:
+ ocfs2_free(&blks);
+
+ return ret;
+}
+
+static errcode_t ocfs2_xattr_value_truncate(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_value_root *xv,
+ char *blkbuf,
+ uint64_t blkno,
+ int len)
+{
+ errcode_t ret;
+ ocfs2_filesys *fs = ci->ci_fs;
+ uint32_t new_clusters = ocfs2_clusters_in_bytes(fs, len);
+ uint32_t old_clusters = xv->xr_clusters;
+
+ if (new_clusters == old_clusters)
+ return 0;
+
+ if (new_clusters > old_clusters) {
+ struct ocfs2_extent_tree et;
+
+ ocfs2_init_xattr_value_extent_tree(&et, fs, blkbuf, blkno, xv);
+ ret = ocfs2_tree_extend_allocation(fs,
+ new_clusters - old_clusters,
+ &et);
+ } else
+ ret = ocfs2_xattr_value_shrink(fs, xv, new_clusters);
+
+ return ret;
+}
+
+/*
+ * ocfs2_xattr_hash_entry()
+ *
+ * Compute the hash of an extended attribute.
+ */
+static void ocfs2_xattr_hash_entry(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_header *header,
+ struct ocfs2_xattr_entry *entry)
+{
+ uint32_t hash = 0;
+ uint32_t uuid_hash = ci->ci_fs->fs_super->id2.i_super.s_uuid_hash;
+ char *name = (char *)header + entry->xe_name_offset;
+
+ hash = ocfs2_xattr_name_hash(uuid_hash, name, entry->xe_name_len);
+ entry->xe_name_hash = hash;
+
+ return;
+}
+
+static inline void ocfs2_xattr_update_entry(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs,
+ size_t offs)
+{
+ xs->here->xe_name_offset = offs;
+ xs->here->xe_value_size = xi->value_len;
+ if (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(ci, xs->header, xs->here);
+}
+
+
+static errcode_t ocfs2_xattr_set_value_outside(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs,
+ size_t offs)
+{
+ size_t name_len = strlen(xi->name);
+ char *val = xs->base + offs;
+ struct ocfs2_xattr_value_root *xv = NULL;
+ size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
+ errcode_t ret = 0;
+
+ memset(val, 0, size);
+ memcpy(val, xi->name, name_len);
+ xv = (struct ocfs2_xattr_value_root *)
+ (val + OCFS2_XATTR_SIZE(name_len));
+ xv->xr_clusters = 0;
+ xv->xr_last_eb_blk = 0;
+ xv->xr_list.l_tree_depth = 0;
+ xv->xr_list.l_count = 1;
+ xv->xr_list.l_next_free_rec = 0;
+
+ ret = ocfs2_xattr_value_truncate(ci, xv, xs->blkbuf, xs->blkno,
+ xi->value_len);
+ if (ret)
+ return ret;
+
+ ocfs2_xattr_update_entry(ci, xi, xs, offs);
+
+ ret = __ocfs2_xattr_set_value_outside(ci, xv, xs->blkbuf, xs->blkno,
+ xi->value, xi->value_len);
+ return ret;
+}
+
+/*
+ * ocfs2_xattr_set_entry_local()
+ *
+ * Set, replace or remove extended attribute in local.
+ */
+static void ocfs2_xattr_set_entry_local(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs,
+ struct ocfs2_xattr_entry *last,
+ size_t min_offs)
+{
+ size_t name_len = strlen(xi->name);
+ int i;
+
+ if (xi->value && xs->not_found) {
+ /* Insert the new xattr entry. */
+ xs->header->xh_count += 1;
+ ocfs2_xattr_set_type(last, xi->name_index);
+ ocfs2_xattr_set_local(last, 1);
+ last->xe_name_len = name_len;
+ } else {
+ void *first_val;
+ void *val;
+ size_t offs, size;
+
+ first_val = xs->base + min_offs;
+ offs = xs->here->xe_name_offset;
+ val = xs->base + offs;
+
+ if (xs->here->xe_value_size > OCFS2_XATTR_INLINE_SIZE)
+ size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_ROOT_SIZE;
+ else
+ size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xs->here->xe_value_size);
+
+ if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xi->value_len)) {
+ /* The old and the new value have the
+ same size. Just replace the value. */
+ ocfs2_xattr_set_local(xs->here, 1);
+ xs->here->xe_value_size = xi->value_len;
+ /* Clear value bytes. */
+ memset(val + OCFS2_XATTR_SIZE(name_len),
+ 0,
+ OCFS2_XATTR_SIZE(xi->value_len));
+ memcpy(val + OCFS2_XATTR_SIZE(name_len),
+ xi->value,
+ xi->value_len);
+ return;
+ }
+ /* Remove the old name+value. */
+ memmove(first_val + size, first_val, val - first_val);
+ memset(first_val, 0, size);
+ xs->here->xe_name_hash = 0;
+ xs->here->xe_name_offset = 0;
+ ocfs2_xattr_set_local(xs->here, 1);
+ xs->here->xe_value_size = 0;
+
+ min_offs += size;
+
+ /* Adjust all value offsets. */
+ last = xs->header->xh_entries;
+ for (i = 0 ; i < xs->header->xh_count; i++) {
+ size_t o = last->xe_name_offset;
+
+ if (o < offs)
+ last->xe_name_offset = o + size;
+ last += 1;
+ }
+
+ if (!xi->value) {
+ /* Remove the old entry. */
+ last -= 1;
+ memmove(xs->here, xs->here + 1,
+ (void *)last - (void *)xs->here);
+ memset(last, 0, ENTRY_SIZE);
+ xs->header->xh_count -= 1;
+ }
+ }
+ if (xi->value) {
+ /* Insert the new name+value. */
+ size_t size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xi->value_len);
+ void *val = xs->base + min_offs - size;
+
+ xs->here->xe_name_offset = min_offs - size;
+ memset(val, 0, size);
+ memcpy(val, xi->name, name_len);
+ memcpy(val + OCFS2_XATTR_SIZE(name_len),
+ xi->value,
+ xi->value_len);
+ xs->here->xe_value_size = xi->value_len;
+ ocfs2_xattr_set_local(xs->here, 1);
+ ocfs2_xattr_hash_entry(ci, xs->header, xs->here);
+ }
+
+ return;
+}
+
+static errcode_t ocfs2_xattr_set_entry(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs,
+ int flag)
+{
+ errcode_t ret = 0;
+ ocfs2_filesys *fs = ci->ci_fs;
+ struct ocfs2_dinode *di = ci->ci_inode;
+ struct ocfs2_xattr_entry *last;
+ size_t min_offs = xs->end - xs->base;
+ size_t name_len = strlen(xi->name);
+ size_t size_l = 0;
+ int free, i;
+ struct ocfs2_xattr_info xi_l = {
+ .name_index = xi->name_index,
+ .name = xi->name,
+ .value = xi->value,
+ .value_len = xi->value_len,
+ };
+
+ /* Compute min_offs, last and free space. */
+ last = xs->header->xh_entries;
+
+ for (i = 0 ; i < xs->header->xh_count; i++) {
+ size_t offs = last->xe_name_offset;
+ if (offs < min_offs)
+ min_offs = offs;
+ last += 1;
+ }
+
+ free = min_offs - ((char *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
+ if (free < 0)
+ return OCFS2_ET_IO;
+
+ if (!xs->not_found) {
+ size_t size = 0;
+ if (ocfs2_xattr_is_local(xs->here))
+ size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xs->here->xe_value_size);
+ else
+ size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_ROOT_SIZE;
+ free += (size + ENTRY_SIZE);
+ }
+
+ /* Check free space in inode or block */
+ if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
+ if (free < ENTRY_SIZE + OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_ROOT_SIZE) {
+ ret = OCFS2_ET_XATTR_NOSPC;
+ goto out;
+ }
+ size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
+ xi_l.value = (const char *)&def_xv;
+ xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
+ } else if (xi->value) {
+ if (free < ENTRY_SIZE + OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xi->value_len)) {
+ ret = OCFS2_ET_XATTR_NOSPC;
+ goto out;
+ }
+ }
+
+ if (!xs->not_found) {
+ /* For existing extended attribute */
+ size_t size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xs->here->xe_value_size);
+ size_t offs = 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(ci, xi, xs,
+ offs);
+ goto out;
+ } else if (!ocfs2_xattr_is_local(xs->here)) {
+ /* For existing xattr which has value outside */
+ struct ocfs2_xattr_value_root *xv;
+
+ xv = (struct ocfs2_xattr_value_root *)
+ (val + OCFS2_XATTR_SIZE(name_len));
+
+ if (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(ci,
+ xv,
+ xs->blkbuf,
+ xs->blkno,
+ xi->value_len);
+ if (ret < 0)
+ goto out;
+
+ ocfs2_xattr_update_entry(ci, xi, xs, offs);
+
+ ret = __ocfs2_xattr_set_value_outside(ci,
+ xv,
+ xs->blkbuf,
+ xs->blkno,
+ xi->value,
+ xi->value_len);
+ goto out;
+ } else {
+ /*
+ * If new value need set in local,
+ * just trucate old value to zero.
+ */
+ ret = ocfs2_xattr_value_truncate(ci,
+ xv,
+ xs->blkbuf,
+ xs->blkno,
+ 0);
+ }
+ }
+ }
+ /*
+ * Set value in local, include set tree root in local.
+ * This is the first step for value size >INLINE_SIZE.
+ */
+ ocfs2_xattr_set_entry_local(ci, &xi_l, xs, last, min_offs);
+
+
+ if (!(di->i_dyn_features & OCFS2_INLINE_XATTR_FL) &&
+ (flag & OCFS2_INLINE_XATTR_FL)) {
+ uint32_t xattrsize = OCFS2_RAW_SB(fs->fs_super)->
+ s_xattr_inline_size;
+
+ /*
+ * Adjust extent record count or inline data size
+ * to reserve space for extended attribute.
+ */
+ if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) {
+ struct ocfs2_inline_data *idata = &di->id2.i_data;
+ idata->id_count -= xattrsize;
+ } else if (!(S_ISLNK(di->i_mode) && di->i_clusters == 0)) {
+ struct ocfs2_extent_list *el = &di->id2.i_list;
+ el->l_count -= (xattrsize /
+ sizeof(struct ocfs2_extent_rec));
+ }
+ di->i_xattr_inline_size = xattrsize;
+ }
+ /* Update xattr flag */
+ di->i_dyn_features |= flag;
+
+ if (!ret && 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 = xs->here->xe_name_offset;
+ ret = ocfs2_xattr_set_value_outside(ci, xi, xs, offs);
+ if (ret < 0) {
+ /*
+ * If set value outside failed, we have to clean
+ * the junk tree root we have already set in local.
+ */
+ size_t name_len = strlen(xi->name);
+ void *val = xs->base + offs;
+ size_t size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_ROOT_SIZE;
+ /* Decrease xattr count */
+ xs->header->xh_count -= 1;
+ /*
+ * Remove the xattr entry and tree root
+ * which has already be set
+ */
+ memset(xs->here, 0, ENTRY_SIZE);
+ memset(val, 0, size);
+ }
+ }
+out:
+ return ret;
+}
+
+static errcode_t ocfs2_xattr_ibody_find(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ struct ocfs2_xattr_search *xs)
+{
+ struct ocfs2_dinode *di = ci->ci_inode;
+ ocfs2_filesys *fs = ci->ci_fs;
+ errcode_t ret = 0;
+
+ if (fs->fs_blocksize == OCFS2_MIN_BLOCKSIZE)
+ return 0;
+
+ if (!(di->i_dyn_features & OCFS2_INLINE_XATTR_FL) &&
+ !ocfs2_xattr_has_space_inline(ci, di))
+ return 0;
+
+ xs->end = (char *)di + fs->fs_blocksize;
+ if (di->i_dyn_features & OCFS2_INLINE_XATTR_FL)
+ xs->header = (struct ocfs2_xattr_header *)
+ (xs->end - di->i_xattr_inline_size);
+ else
+ xs->header = (struct ocfs2_xattr_header *)(xs->end -
+ OCFS2_RAW_SB(fs->fs_super)->s_xattr_inline_size);
+ xs->base = (char *)xs->header;
+ xs->here = xs->header->xh_entries;
+
+ /* Find the named attribute. */
+ if (di->i_dyn_features & OCFS2_INLINE_XATTR_FL) {
+ ret = ocfs2_xattr_find_entry(name_index, name, xs);
+ if (ret && ret != OCFS2_ET_XATTR_NODATA)
+ return ret;
+ xs->not_found = ret;
+ }
+
+ return 0;
+}
+
+/*
+ * ocfs2_xattr_ibody_set()
+ *
+ * Set, replace or remove an extended attribute into inode block.
+ *
+ */
+static errcode_t ocfs2_xattr_ibody_set(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs)
+{
+ errcode_t ret;
+ struct ocfs2_dinode *di = ci->ci_inode;
+
+ if (ci->ci_fs->fs_blocksize == OCFS2_MIN_BLOCKSIZE)
+ return OCFS2_ET_XATTR_NOSPC;
+
+
+ if (!(di->i_dyn_features & OCFS2_INLINE_XATTR_FL) &&
+ !ocfs2_xattr_has_space_inline(ci, di))
+ return 0;
+
+ ret = ocfs2_xattr_set_entry(ci, xi, xs, (OCFS2_INLINE_XATTR_FL |
+ OCFS2_HAS_XATTR_FL));
+ if (!ret)
+ ret = ocfs2_write_cached_inode(ci->ci_fs, ci);
+ return ret;
+}
+
+/*
+ * Find the specified xattr entry in a series of buckets.
+ * This series start from p_blkno and last for num_clusters.
+ * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
+ * the num of the valid buckets.
+ */
+static errcode_t ocfs2_xattr_bucket_find(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ uint32_t name_hash,
+ uint64_t p_blkno,
+ uint32_t num_clusters,
+ struct ocfs2_xattr_search *xbks)
+{
+ int i;
+ char *bucket = xbks->blkbuf;
+ struct ocfs2_xattr_header *xh = NULL;
+ ocfs2_filesys *fs = ci->ci_fs;
+ uint16_t bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ uint32_t num_buckets = ocfs2_xattr_buckets_per_cluster(fs);
+ uint64_t blk = 0;
+ errcode_t ret = 0;
+
+ blk = p_blkno;
+ for (i = 0; i < num_buckets; i++, blk += bpb) {
+ struct ocfs2_xattr_entry *first_xe;
+ struct ocfs2_xattr_entry *last_xe;
+
+ ret = ocfs2_read_xattr_bucket(fs, blk, bucket);
+ if (ret)
+ goto out;
+ xh = (struct ocfs2_xattr_header *)bucket;
+ xbks->blkno = blk;
+ xbks->header = xh;
+ xbks->base = (char *)xh;
+ xbks->end = xbks->base + fs->fs_blocksize;
+ xbks->here = xbks->header->xh_entries;
+ if (i == 0)
+ num_buckets = xh->xh_num_buckets;
+
+ last_xe = first_xe = xh->xh_entries;
+ if (xh->xh_count)
+ last_xe = &xh->xh_entries[xh->xh_count - 1];
+
+ if (name_hash > last_xe->xe_name_hash)
+ continue;
+ ret = ocfs2_xattr_find_entry(name_index, name, xbks);
+ if (ret && ret != OCFS2_ET_XATTR_NODATA)
+ goto out;
+ xbks->not_found = ret;
+ return 0;
+ }
+out:
+ return ret;
+}
+
+static errcode_t ocfs2_xattr_index_block_find(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ struct ocfs2_xattr_search_info *xsi)
+{
+ errcode_t ret;
+ struct ocfs2_xattr_block *xb = (struct ocfs2_xattr_block *)
+ xsi->xi_block->blkbuf;
+ struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
+ struct ocfs2_extent_list *el = &xb_root->xt_list;
+ uint64_t p_blkno = 0;
+ uint32_t first_hash, num_clusters = 0;
+ uint32_t uuid_hash, name_hash;
+
+ if (el->l_next_free_rec == 0) {
+ xsi->xi_bucket->not_found = OCFS2_ET_XATTR_NODATA;
+ return 0;
+ }
+
+ uuid_hash = ci->ci_fs->fs_super->id2.i_super.s_uuid_hash;
+ name_hash = ocfs2_xattr_name_hash(uuid_hash, name, strlen(name));
+ DEBUG("try to find xattr %s, hash = %u, index = %d in xattr tree.\n",
+ name, name_hash, name_index);
+
+ ret = ocfs2_xattr_get_rec(ci->ci_fs, xb, name_hash, &p_blkno,
+ &first_hash, &num_clusters);
+ if (ret)
+ return ret;
+
+ ASSERT(p_blkno != 0 || num_clusters != 0 || first_hash < name_hash);
+
+ DEBUG("find xattr extent rec %u clusters from #%"PRIu64", the first"
+ " hash in the rec is %u\n", num_clusters, p_blkno, first_hash);
+
+ return ocfs2_xattr_bucket_find(ci, name_index, name, name_hash,
+ p_blkno, num_clusters, xsi->xi_bucket);
+}
+
+
+/*
+ * ocfs2_xattr_block_find()
+ *
+ * Find extended attribute in external block and
+ * fill search info into struct ocfs2_xattr_search.
+ */
+static errcode_t ocfs2_xattr_block_find(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ struct ocfs2_xattr_search_info *xsi)
+{
+ ocfs2_filesys *fs = ci->ci_fs;
+ struct ocfs2_dinode *di = ci->ci_inode;
+ struct ocfs2_xattr_block *xb = NULL;
+ char *blk = xsi->xi_block->blkbuf;
+ errcode_t ret = 0;
+
+ if (!di->i_xattr_loc)
+ return 0;
+
+ ret = ocfs2_read_xattr_block(fs, di->i_xattr_loc, blk);
+ if (ret)
+ return ret;
+
+ xsi->xi_block->blkno = di->i_xattr_loc;
+
+ xb = (struct ocfs2_xattr_block *)blk;
+
+ if (xb->xb_flags & OCFS2_XATTR_INDEXED) {
+ ret = ocfs2_xattr_index_block_find(ci, name_index, name, xsi);
+ } else {
+ xsi->xi_block->header = &xb->xb_attrs.xb_header;
+ xsi->xi_block->base = (char *)xsi->xi_block->header;
+ xsi->xi_block->end = blk + fs->fs_blocksize;
+ xsi->xi_block->here = xsi->xi_block->header->xh_entries;
+ ret = ocfs2_xattr_find_entry(name_index, name, xsi->xi_block);
+ if (ret && ret != OCFS2_ET_XATTR_NODATA)
+ goto out;
+ xsi->xi_block->not_found = ret;
+ return 0;
+ }
+out:
+ return ret;
+}
+
+static errcode_t ocfs2_xattr_get_value_outside(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_value_root *xv,
+ struct ocfs2_xattr_search *xs,
+ void *buffer,
+ size_t value_len)
+{
+ uint32_t cpos, p_cluster, num_clusters;
+ uint64_t p_blkno;
+ size_t cp_len;
+ char *buf = NULL;
+ ocfs2_filesys *fs = ci->ci_fs;
+ size_t blocksize = fs->fs_blocksize;
+ uint16_t clustersize = fs->fs_clustersize;
+ uint32_t clusters = xv->xr_clusters;
+ uint32_t bpc = ocfs2_clusters_to_blocks(fs, 1);
+ uint32_t max_blocks = OCFS2_MAX_XATTR_TREE_LEAF_SIZE / blocksize;
+ errcode_t ret = 0;
+
+
+ ret = ocfs2_malloc_blocks(fs->fs_io, max_blocks, &buf);
+ if (ret)
+ return ret;
+
+ cpos = 0;
+ while (cpos < clusters) {
+ ret = ocfs2_xattr_get_clusters(ci, cpos, &p_cluster,
+ &num_clusters,
+ &xv->xr_list,
+ xs->blkno, xs->blkbuf);
+ if (ret)
+ goto out;
+
+ p_blkno = ocfs2_clusters_to_blocks(fs, p_cluster);
+ memset(buf, 0, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
+
+ ret = io_read_block(fs->fs_io, p_blkno,
+ num_clusters * bpc, buf);
+ if (ret)
+ goto out;
+
+ cp_len = value_len > num_clusters * clustersize ?
+ num_clusters * clustersize : value_len;
+ memcpy(buffer, buf, cp_len);
+
+ value_len -= cp_len;
+ cpos += num_clusters;
+ }
+out:
+ ocfs2_free(&buf);
+ return ret;
+}
+
+
+static int ocfs2_xattr_ibody_get(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ char *buffer,
+ size_t buffer_size,
+ struct ocfs2_xattr_search *xs,
+ size_t *got)
+{
+ struct ocfs2_xattr_value_root *xv;
+ size_t size;
+ struct ocfs2_dinode *di = ci->ci_inode;
+ ocfs2_filesys *fs = ci->ci_fs;
+ errcode_t ret = 0;
+
+ if (!(di->i_dyn_features & OCFS2_INLINE_XATTR_FL))
+ return OCFS2_ET_XATTR_NODATA;
+
+ xs->end = (void *)di + fs->fs_blocksize;
+ xs->header = (struct ocfs2_xattr_header *)
+ (xs->end - di->i_xattr_inline_size);
+ xs->base = (void *)xs->header;
+ xs->here = xs->header->xh_entries;
+
+ ret = ocfs2_xattr_find_entry(name_index, name, xs);
+ if (ret)
+ return ret;
+ size = xs->here->xe_value_size;
+ if (buffer) {
+ if (size > buffer_size)
+ return OCFS2_ET_XATTR_NOSPC;
+ if (ocfs2_xattr_is_local(xs->here)) {
+ memcpy(buffer, (void *)xs->base +
+ xs->here->xe_name_offset +
+ OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
+ } else {
+ xv = (struct ocfs2_xattr_value_root *)
+ (xs->base + xs->here->xe_name_offset +
+ OCFS2_XATTR_SIZE(xs->here->xe_name_len));
+ ret = ocfs2_xattr_get_value_outside(ci, xv, xs,
+ buffer, size);
+ if (ret < 0)
+ return ret;
+ }
+ }
+
+ return size;
+}
+
+static errcode_t ocfs2_xattr_block_get(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ char *buffer,
+ size_t buffer_size,
+ struct ocfs2_xattr_search_info *xsi,
+ size_t *got)
+{
+ struct ocfs2_xattr_value_root *xv;
+ size_t size;
+ struct ocfs2_xattr_search *xs = NULL;
+ errcode_t ret = 0;
+
+ ret = ocfs2_xattr_block_find(ci, name_index, name, xsi);
+ if (ret)
+ return ret;
+
+ if (xsi->xi_block->not_found && xsi->xi_bucket->not_found)
+ return OCFS2_ET_XATTR_NODATA;
+
+ if (!xsi->xi_block->not_found) {
+ size = xsi->xi_block->here->xe_value_size;
+ xs = xsi->xi_block;
+ } else {
+ size = xsi->xi_bucket->here->xe_value_size;
+ xs = xsi->xi_bucket;
+ }
+
+ if (buffer) {
+ if (size > buffer_size)
+ return OCFS2_ET_XATTR_NOSPC;
+
+ if (ocfs2_xattr_is_local(xs->here)) {
+ memcpy(buffer, (void *)xs->base +
+ xs->here->xe_name_offset +
+ OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
+ } else {
+ xv = (struct ocfs2_xattr_value_root *)
+ (xs->base + xs->here->xe_name_offset +
+ OCFS2_XATTR_SIZE(xs->here->xe_name_len));
+ ret = ocfs2_xattr_get_value_outside(ci, xv, xs,
+ buffer, size);
+ if (ret < 0)
+ return ret;
+ }
+ }
+ return size;
+}
+
+static errcode_t ocfs2_xattr_get_generic(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ char *buffer,
+ size_t buffer_size,
+ size_t *got)
+{
+ errcode_t ret = 0;
+ struct ocfs2_dinode *di = ci->ci_inode;
+ ocfs2_filesys *fs = ci->ci_fs;
+ int bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ struct ocfs2_xattr_search xis = {
+ .blkbuf = (char *)ci->ci_inode,
+ .blkno = ci->ci_blkno,
+ .not_found = OCFS2_ET_XATTR_NODATA,
+ };
+ struct ocfs2_xattr_search xbs = {.not_found = OCFS2_ET_XATTR_NODATA,};
+ struct ocfs2_xattr_search xbks = {.not_found = OCFS2_ET_XATTR_NODATA,};
+ struct ocfs2_xattr_search_info xsi = {
+ .xi_inode = &xis,
+ .xi_block = &xbs,
+ .xi_bucket = &xbks,
+ };
+
+ /* alloc block buffer for xattr block */
+ ret = ocfs2_malloc_block(fs->fs_io, &xbs.blkbuf);
+ if (ret)
+ return ret;
+
+ /* alloc block buffer for xattr bucket */
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &xbks.blkbuf);
+ if (ret)
+ return ret;
+
+
+ ret = ocfs2_xattr_ibody_get(ci, name_index, name, buffer,
+ buffer_size, &xis, got);
+ if (ret == OCFS2_ET_XATTR_NODATA && di->i_xattr_loc)
+ ret = ocfs2_xattr_block_get(ci, name_index, name, buffer,
+ buffer_size, &xsi, got);
+
+ if (xbs.blkbuf)
+ ocfs2_free(&xbs.blkbuf);
+ if (xbks.blkbuf)
+ ocfs2_free(&xbks.blkbuf);
+ return ret;
+}
+
+static int cmp_xe(const void *a, const void *b)
+{
+ const struct ocfs2_xattr_entry *l = a, *r = b;
+ uint32_t l_hash = l->xe_name_hash;
+ uint32_t r_hash = r->xe_name_hash;
+
+ if (l_hash > r_hash)
+ return 1;
+ if (l_hash < r_hash)
+ return -1;
+ return 0;
+}
+
+static int cmp_xe_offset(const void *a, const void *b)
+{
+ const struct ocfs2_xattr_entry *l = a, *r = b;
+ uint32_t l_name_offset = l->xe_name_offset;
+ uint32_t r_name_offset = r->xe_name_offset;
+
+ if (l_name_offset < r_name_offset)
+ return 1;
+ if (l_name_offset > r_name_offset)
+ return -1;
+ return 0;
+}
+
+/*
+ * 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(ocfs2_cached_inode *ci,
+ char *block_buf,
+ char *bucket_buf)
+{
+ int i, blocksize = ci->ci_fs->fs_blocksize;
+ int blks = ocfs2_blocks_per_xattr_bucket(ci->ci_fs);
+ uint16_t offset, size, off_change;
+ struct ocfs2_xattr_entry *xe;
+ struct ocfs2_xattr_block *xb = (struct ocfs2_xattr_block *)block_buf;
+ struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
+ struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)bucket_buf;
+ uint16_t count = xb_xh->xh_count;
+ char *src = block_buf;
+ char *target = bucket_buf + (blks - 1) * blocksize;
+
+ DEBUG("cp xattr from block #%"PRIu64" to bucket.\n", xb->xb_blkno);
+
+ memset(bucket_buf, 0, OCFS2_XATTR_BUCKET_SIZE);
+
+ /*
+ * Since the xe_name_offset is based on ocfs2_xattr_header,
+ * there is a offset change corresponding to the change of
+ * ocfs2_xattr_header's position.
+ */
+ off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
+ xe = &xb_xh->xh_entries[count - 1];
+ offset = xe->xe_name_offset + off_change;
+ size = blocksize - offset;
+
+ /* copy all the names and values. */
+ memcpy(target + offset, src + offset, size);
+
+ /* Init new header now. */
+ xh->xh_count = xb_xh->xh_count;
+ xh->xh_num_buckets = 1;
+ xh->xh_name_value_len = size;
+ xh->xh_free_start = OCFS2_XATTR_BUCKET_SIZE - size;
+
+ /* copy all the entries. */
+ target = bucket_buf;
+ offset = offsetof(struct ocfs2_xattr_header, xh_entries);
+ size = count * ENTRY_SIZE;
+ memcpy(target + offset, (char *)xb_xh + offset, size);
+
+ /* Change the xe offset for all the xe because of the move. */
+ off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
+ offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
+ for (i = 0; i < count; i++)
+ xh->xh_entries[i].xe_name_offset += off_change;
+
+ DEBUG("copy entry: start = %u, size = %u, offset_change = %u\n",
+ offset, size, off_change);
+
+ qsort(target + offset, count, ENTRY_SIZE, cmp_xe);
+}
+
+static errcode_t ocfs2_xattr_create_index_block(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_search_info *xsi)
+{
+ errcode_t ret;
+ uint32_t len;
+ uint64_t blkno;
+ ocfs2_filesys *fs = ci->ci_fs;
+ struct ocfs2_xattr_block *xb = (struct ocfs2_xattr_block *)
+ xsi->xi_block->blkbuf;
+ uint16_t xb_flags = xb->xb_flags;
+ struct ocfs2_xattr_tree_root *xr = NULL;
+ char *bucket = xsi->xi_bucket->blkbuf;
+ size_t offs;
+
+ DEBUG("create xattr index block for #%"PRIu64"\n", xb->xb_blkno);
+
+ ASSERT(!(xb_flags & OCFS2_XATTR_INDEXED));
+
+ ret = ocfs2_new_clusters(fs, 1, 1, &blkno, &len);
+ if (ret)
+ return ret;
+
+ DEBUG("allocate 1 cluster from #%"PRIu64" to xattr block\n", blkno);
+
+ ocfs2_cp_xattr_block_to_bucket(ci, (char *)xb, bucket);
+
+ /* update xattr search info for xattr block and xattr bucket*/
+ offs = (char *)xsi->xi_block->here - (char *)xsi->xi_block->header;
+ xsi->xi_block->header = NULL;
+ xsi->xi_block->base = NULL;
+ xsi->xi_block->end = NULL;
+ xsi->xi_block->here = NULL;
+
+ xsi->xi_bucket->blkno = blkno;
+ xsi->xi_bucket->header = (struct ocfs2_xattr_header *)bucket;
+ xsi->xi_bucket->base = bucket;
+ xsi->xi_bucket->end = xsi->xi_bucket->base + fs->fs_blocksize;
+ xsi->xi_bucket->here = (struct ocfs2_xattr_entry *)(bucket + offs);
+
+ /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
+ memset(&xb->xb_attrs, 0, fs->fs_blocksize -
+ offsetof(struct ocfs2_xattr_block, xb_attrs));
+
+ xr = &xb->xb_attrs.xb_root;
+ xr->xt_clusters = 1;
+ xr->xt_last_eb_blk = 0;
+ xr->xt_list.l_tree_depth = 0;
+ xr->xt_list.l_count = ocfs2_xattr_recs_per_xb(fs->fs_blocksize);
+ xr->xt_list.l_next_free_rec = 1;
+
+ xr->xt_list.l_recs[0].e_cpos = 0;
+ xr->xt_list.l_recs[0].e_blkno = blkno;
+ xr->xt_list.l_recs[0].e_leaf_clusters = 1;
+
+ xb->xb_flags = xb_flags | OCFS2_XATTR_INDEXED;
+
+ ret = ocfs2_write_xattr_bucket(fs, blkno, bucket);
+ if (ret)
+ return ret;
+
+ ret = ocfs2_write_xattr_block(fs, xsi->xi_block->blkno,
+ xsi->xi_block->blkbuf);
+ return ret;
+}
+
+/*
+ * defrag a xattr bucket if we find that the bucket has some
+ * holes beteen name/value pairs.
+ * We will move all the name/value pairs to the end of the bucket
+ * so that we can spare some space for insertion.
+ */
+static errcode_t ocfs2_defrag_xattr_bucket(ocfs2_cached_inode *ci,
+ char *blkbuf,
+ uint64_t blkno)
+{
+ errcode_t ret;
+ int i;
+ size_t end, offset, len, value_len;
+ char *entries = NULL;
+ char *bucket_buf = NULL;
+ uint16_t xh_free_start;
+ ocfs2_filesys *fs = ci->ci_fs;
+ size_t blocksize = fs->fs_blocksize;
+ struct ocfs2_xattr_entry *xe;
+ struct ocfs2_xattr_header *xh;
+ uint16_t bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ /*
+ * In order to make the operation more efficient and generic,
+ * we copy all the blocks into a contiguous memory and do the
+ * defragment there, so if anything is error, we will not touch
+ * the real block.
+ */
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &bucket_buf);
+ if (ret)
+ goto out;
+
+ memcpy(bucket_buf, blkbuf, OCFS2_XATTR_BUCKET_SIZE);
+
+ xh = (struct ocfs2_xattr_header *)bucket_buf;
+ entries = (char *)xh->xh_entries;
+ xh_free_start = xh->xh_free_start;
+
+ DEBUG("Adjust xattr bucket in #%"PRIu64", count = %u,"
+ " xh_free_start = %u, xh_name_value_len = %u.\n",
+ blkno, xh->xh_count, xh_free_start,
+ xh->xh_name_value_len);
+ /*
+ * sort all the entries by their offset.
+ * the largest will be the first, so that we can
+ * move them to the end one by one.
+ */
+ qsort(entries, xh->xh_count, ENTRY_SIZE, cmp_xe_offset);
+
+ /* Move all name/values to the end of the bucket. */
+ xe = xh->xh_entries;
+ end = OCFS2_XATTR_BUCKET_SIZE;
+ for (i = 0; i < xh->xh_count; i++, xe++) {
+ offset = xe->xe_name_offset;
+ if (ocfs2_xattr_is_local(xe))
+ value_len = OCFS2_XATTR_SIZE(xe->xe_value_size);
+ else
+ value_len = OCFS2_XATTR_ROOT_SIZE;
+ len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
+
+ /*
+ * We must make sure that the name/value pair
+ * exist in the same block. So adjust end to
+ * the previous block end if needed.
+ */
+ if (((end - len) / blocksize !=
+ (end - 1) / blocksize))
+ end = end - end % blocksize;
+
+ if (end > offset + len) {
+ memmove(bucket_buf + end - len,
+ bucket_buf + offset, len);
+ xe->xe_name_offset = end - len;
+ }
+
+ if (end < offset + len) {
+ DEBUG("Defrag check failed for bucket #%"PRIu64"\n",
+ blkno);
+ ASSERT(end >= offset + len);
+ }
+ end -= len;
+ }
+
+ if (xh_free_start > end) {
+ DEBUG("Defrag check failed for bucket #%"PRIu64"\n", blkno);
+ ASSERT(xh_free_start <= end);
+ }
+
+ if (xh_free_start == end)
+ goto out;
+
+ memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
+ xh->xh_free_start = end;
+
+ /* sort the entries by their name_hash. */
+ qsort(entries, xh->xh_count, ENTRY_SIZE, cmp_xe);
+
+ memcpy(blkbuf, bucket_buf, OCFS2_XATTR_BUCKET_SIZE);
+ ret = ocfs2_write_xattr_bucket(fs, blkno, blkbuf);
+
+out:
+ if (bucket_buf)
+ ocfs2_free(&bucket_buf);
+ 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 XATTR_NOSPC.
+ * If we want to insert a xattr with different hash value, go ahead
+ * and ocfs2_divide_xattr_bucket will handle this.
+ */
+static errcode_t ocfs2_check_xattr_bucket_collision(ocfs2_cached_inode *ci,
+ char *blkbuf,
+ uint64_t blkno,
+ const char *name)
+{
+ struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)blkbuf;
+ uint32_t uuid_hash = ci->ci_fs->fs_super->id2.i_super.s_uuid_hash;
+ uint32_t name_hash = ocfs2_xattr_name_hash(uuid_hash,
+ name, strlen(name));
+
+ if (name_hash != xh->xh_entries[0].xe_name_hash)
+ return 0;
+
+ if (xh->xh_entries[xh->xh_count - 1].xe_name_hash ==
+ xh->xh_entries[0].xe_name_hash) {
+ DEBUG("Too much hash collision in xattr bucket #%"PRIu64","
+ "hash = %u\n", blkno, xh->xh_entries[0].xe_name_hash);
+ return OCFS2_ET_XATTR_NOSPC;
+ }
+
+ return 0;
+}
+
+/*
+ * Copy xattr from one bucket to another bucket.
+ */
+static errcode_t ocfs2_cp_xattr_bucket(ocfs2_cached_inode *ci,
+ uint64_t s_blkno,
+ uint64_t t_blkno)
+{
+ errcode_t ret;
+ ocfs2_filesys *fs = ci->ci_fs;
+ int bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ struct ocfs2_xattr_buf s_bucket = {
+ .xb_buf = NULL,
+ .xb_blkno = 0,
+ };
+ struct ocfs2_xattr_buf t_bucket = {
+ .xb_buf = NULL,
+ .xb_blkno = 0,
+ };
+
+ ASSERT(s_blkno != t_blkno);
+
+ DEBUG("cp bucket #%"PRIu64" to #%"PRIu64"\n", s_blkno, t_blkno);
+
+
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &s_bucket.xb_buf);
+ if (ret)
+ return ret;
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &t_bucket.xb_buf);
+ if (ret)
+ goto cleanup;
+
+ ret = ocfs2_read_xattr_bucket(fs, s_blkno, s_bucket.xb_buf);
+ if (ret)
+ goto cleanup;
+
+ memcpy(t_bucket.xb_buf, s_bucket.xb_buf, OCFS2_XATTR_BUCKET_SIZE);
+ ret = ocfs2_write_xattr_bucket(fs, t_blkno, t_bucket.xb_buf);
+
+cleanup:
+ if (s_bucket.xb_buf)
+ ocfs2_free(&s_bucket.xb_buf);
+ if (t_bucket.xb_buf)
+ ocfs2_free(&t_bucket.xb_buf);
+
+ return ret;
+}
+
+/*
+ * old_first_blkno points to the start of an existing extent. last_blk
+ * points to last cluster in that extent. to_blk points to a newly allocated
+ * extent. We copy the buckets from the cluster at last_blk to the new
+ * extent. If start_bucket is non-zero, we skip that many buckets before
+ * we start copying. The new extent's xh_num_buckets gets set to the
+ * number of buckets we copied. The old extent's xh_num_buckets shrinks
+ * by the same amount.
+ */
+static errcode_t ocfs2_mv_xattr_buckets(ocfs2_cached_inode *ci,
+ uint64_t old_first_blkno,
+ struct ocfs2_xattr_buf *target,
+ uint64_t last_blk,
+ uint64_t to_blk,
+ uint32_t start_bucket,
+ uint32_t *first_hash)
+{
+ errcode_t ret;
+ int i;
+ ocfs2_filesys *fs = ci->ci_fs;
+ int bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ int num_buckets = ocfs2_xattr_buckets_per_cluster(fs);
+ char *old_first = NULL;
+ char *new_first = NULL;
+ struct ocfs2_xattr_header *xh = NULL;
+
+ DEBUG("mv xattrs from cluster #%"PRIu64" to #%"PRIu64"\n",
+ last_blk, to_blk);
+
+ ASSERT(start_bucket < num_buckets);
+ if (start_bucket) {
+ num_buckets -= start_bucket;
+ last_blk += (start_bucket * bpb);
+ }
+
+ /*
+ * We need to update the first bucket of the old extent and all
+ * the buckets going to the new extent.
+ */
+ for (i = 0; i < num_buckets; i++) {
+ ret = ocfs2_cp_xattr_bucket(ci, last_blk + (i * bpb),
+ to_blk + (i * bpb));
+ if (ret)
+ goto cleanup;
+ }
+
+ if (old_first_blkno == target->xb_blkno)
+ old_first = target->xb_buf;
+ else {
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &old_first);
+ if (ret)
+ goto cleanup;
+ ret = ocfs2_read_xattr_bucket(fs, old_first_blkno, old_first);
+ if (ret)
+ goto cleanup;
+ }
+
+ /* The first bucket of the new extent */
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &new_first);
+ if (ret)
+ goto cleanup;
+ ret = ocfs2_read_xattr_bucket(fs, to_blk, new_first);
+ if (ret)
+ goto cleanup;
+
+ /* Now update the headers */
+ xh = (struct ocfs2_xattr_header *)old_first;
+ xh->xh_num_buckets -= num_buckets;
+ ret = ocfs2_write_xattr_bucket(fs, old_first_blkno, old_first);
+ if (ret)
+ goto cleanup;
+
+ xh = (struct ocfs2_xattr_header *)new_first;
+ xh->xh_num_buckets = num_buckets;
+ ret = ocfs2_write_xattr_bucket(fs, to_blk, new_first);
+
+ if (first_hash)
+ *first_hash = xh->xh_entries[0].xe_name_hash;
+
+cleanup:
+ if (old_first_blkno != target->xb_blkno)
+ ocfs2_free(&old_first);
+ if (new_first)
+ ocfs2_free(&new_first);
+ return ret;
+}
+
+/*
+ * First_blkno points to the start of an existing extent. new_blkno
+ * points to a newly allocated extent. Because we know each of our
+ * clusters contains more than bucket, we can easily split one cluster
+ * at a bucket boundary. So we take the last cluster of the existing
+ * extent and split it down the middle. We move the last half of the
+ * buckets in the last cluster of the existing extent over to the new
+ * extent.
+ *
+ * first_blkno indecates to the first bucket so we can update the
+ * existing extent's bucket count. Target point to the bucket buffer
+ * which we were hoping to insert our xattr. If the bucket move places
+ * the target in the new extent, we'll update first bucket and
+ * target bucket after modifying the old extent.
+ *
+ * First_hash will be set as the 1st xe's name_hash in the new extent.
+ */
+static errcode_t ocfs2_mv_xattr_bucket_cross_cluster(ocfs2_cached_inode *ci,
+ uint64_t *first_blkno,
+ struct ocfs2_xattr_buf *target,
+ uint64_t new_blkno,
+ uint32_t num_clusters,
+ uint32_t *first_hash)
+{
+ errcode_t ret;
+ ocfs2_filesys *fs = ci->ci_fs;
+ int bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ int num_buckets = ocfs2_xattr_buckets_per_cluster(fs);
+ int to_move = num_buckets / 2;
+ uint64_t src_blkno;
+ uint64_t last_cluster_blkno = *first_blkno +
+ ((num_clusters - 1) * ocfs2_clusters_to_blocks(fs, 1));
+
+ ASSERT(OCFS2_XATTR_BUCKET_SIZE != fs->fs_clustersize);
+
+ DEBUG("move half of xattrs in cluster #%"PRIu64" to #%"PRIu64"\n",
+ last_cluster_blkno, new_blkno);
+
+ ret = ocfs2_mv_xattr_buckets(ci, *first_blkno, target,
+ last_cluster_blkno,
+ new_blkno, to_move, first_hash);
+ if (ret)
+ goto out;
+
+ /* This is the first bucket that got moved */
+ src_blkno = last_cluster_blkno + (to_move * bpb);
+
+ /*
+ * If the target bucket was part of the moved buckets, we need to
+ * update first and target.
+ */
+ if (target->xb_blkno >= src_blkno) {
+ /* Find the block for the new target bucket */
+ src_blkno = new_blkno + target->xb_blkno - src_blkno;
+
+ ret = ocfs2_read_xattr_bucket(fs, src_blkno, target->xb_buf);
+ if (ret)
+ goto out;
+ target->xb_blkno = src_blkno;
+ *first_blkno = new_blkno;
+ }
+out:
+ return ret;
+}
+
+/*
+ * 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)
+{
+ struct ocfs2_xattr_entry *entries = xh->xh_entries;
+ int count = xh->xh_count;
+ int delta, middle = count / 2;
+
+ /*
+ * 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;
+
+ /* Now try delta past middle */
+ if (cmp_xe(&entries[middle + delta],
+ &entries[middle + delta + 1]))
+ return middle + delta + 1;
+ }
+
+ /* Every entry had the same hash */
+ return count;
+}
+
+/*
+ * 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 errcode_t ocfs2_divide_xattr_bucket(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_buf *s_bucket,
+ uint64_t new_blk,
+ uint32_t *first_hash,
+ uint32_t is_new)
+{
+ errcode_t ret;
+ int i;
+ ocfs2_filesys *fs = ci->ci_fs;
+ int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
+ uint16_t bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ struct ocfs2_xattr_header *xh;
+ struct ocfs2_xattr_entry *xe;
+ char *temp = NULL;
+
+ DEBUG("move some of xattrs from bucket #%"PRIu64" to #%"PRIu64"\n",
+ s_bucket->xb_blkno, new_blk);
+
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &temp);
+ if (ret)
+ return ret;
+
+ xh = bucket_xh(s_bucket);
+ count = xh->xh_count;
+ start = ocfs2_xattr_find_divide_pos(xh);
+ DEBUG("count = %d, start = %d\n", count, start);
+
+ 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.
+ */
+ memset(temp, 0, OCFS2_XATTR_BUCKET_SIZE);
+
+ xh = (struct ocfs2_xattr_header *)temp;
+ xh->xh_free_start = fs->fs_blocksize;
+ xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
+ xh->xh_entries[0].xe_name_hash += 1;
+
+ goto set_num_buckets;
+ }
+
+ /* copy the whole bucket to the new first. */
+ memcpy(temp, s_bucket->xb_buf, OCFS2_XATTR_BUCKET_SIZE);
+
+ /* update the new bucket. */
+ xh = (struct ocfs2_xattr_header *)temp;
+
+ /*
+ * Calculate the total name/value len and xh_free_start for
+ * the old bucket first.
+ */
+ name_offset = OCFS2_XATTR_BUCKET_SIZE;
+ name_value_len = 0;
+ for (i = 0; i < start; i++) {
+ xe = &xh->xh_entries[i];
+ xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
+ if (ocfs2_xattr_is_local(xe))
+ xe_len += OCFS2_XATTR_SIZE(xe->xe_value_size);
+ else
+ xe_len += OCFS2_XATTR_ROOT_SIZE;
+ name_value_len += xe_len;
+ if (xe->xe_name_offset < name_offset)
+ name_offset = xe->xe_name_offset;
+ }
+
+ /*
+ * Now begin the modification to the new bucket.
+ *
+ * In the new bucket, We just move the xattr entry to the beginning
+ * and don't touch the name/value. So there will be some holes in the
+ * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
+ * called.
+ */
+ xe = &xh->xh_entries[start];
+ len = sizeof(struct ocfs2_xattr_entry) * (count - start);
+ DEBUG("mv xattr entry len %d from %d to %d\n", len,
+ (int)((char *)xe - (char *)xh),
+ (int)((char *)xh->xh_entries - (char *)xh));
+ memmove((char *)xh->xh_entries, (char *)xe, len);
+ xe = &xh->xh_entries[count - start];
+ len = sizeof(struct ocfs2_xattr_entry) * start;
+ memset((char *)xe, 0, len);
+
+ xh->xh_count -= start;
+ xh->xh_name_value_len -= name_value_len;
+
+ /* Calculate xh_free_start for the new bucket. */
+ xh->xh_free_start = OCFS2_XATTR_BUCKET_SIZE;
+ for (i = 0; i < xh->xh_count; i++) {
+ xe = &xh->xh_entries[i];
+ xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
+ if (ocfs2_xattr_is_local(xe))
+ xe_len +=
+ OCFS2_XATTR_SIZE(xe->xe_value_size);
+ else
+ xe_len += OCFS2_XATTR_ROOT_SIZE;
+ if (xe->xe_name_offset < xh->xh_free_start)
+ xh->xh_free_start = xe->xe_name_offset;
+ }
+
+set_num_buckets:
+ /* set xh->xh_num_buckets for the new xh. */
+ if (is_new)
+ xh->xh_num_buckets = 1;
+ else
+ xh->xh_num_buckets = 0;
+
+ ret = ocfs2_write_xattr_bucket(fs, new_blk, temp);
+ if (ret)
+ goto out;
+
+ /* store the first_hash of the new bucket. */
+ if (first_hash)
+ *first_hash = xh->xh_entries[0].xe_name_hash;
+
+ /*
+ * 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.
+ */
+ 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 = start;
+ xh->xh_free_start = name_offset;
+ xh->xh_name_value_len = name_value_len;
+
+ ret = ocfs2_write_xattr_bucket(fs, s_bucket->xb_blkno,
+ s_bucket->xb_buf);
+ xh = (struct ocfs2_xattr_header *)s_bucket->xb_buf;
+out:
+ ocfs2_free(&temp);
+
+ return ret;
+}
+
+
+/*
+ * 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 errcode_t ocfs2_divide_xattr_cluster(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_buf *target,
+ uint64_t new_blk,
+ uint32_t *first_hash)
+{
+ /* Move half of the xattr in start_blk to the next bucket. */
+ return ocfs2_divide_xattr_bucket(ci, target,
+ new_blk, first_hash, 1);
+}
+
+/*
+ * Move some xattrs from the old cluster to the new one since they are not
+ * contiguous in ocfs2 xattr tree.
+ *
+ * new_blk starts a new separate cluster, and we will move some xattrs from
+ * prev_blk to it. v_start will be set as the first name hash value in this
+ * new cluster so that it can be used as e_cpos during tree insertion and
+ * don't collide with our original b-tree operations. first and target buckets
+ * will also be updated since they will be used in ocfs2_extend_xattr_bucket
+ * to extend the insert bucket.
+ *
+ * The problem is how much xattr should we move to the new one and when should
+ * we update first and target buckets?
+ * 1. If cluster size > bucket size, that means the previous cluster has more
+ * than 1 bucket, so just move half nums of bucket into the new cluster and
+ * update the first and target buckets if the insert bucket has been moved
+ * to the new cluster.
+ * 2. If cluster_size == bucket_size:
+ * a) If the previous extent rec has more than one cluster and the insert
+ * place isn't in the last cluster, copy the entire last cluster to the
+ * new one. This time, we don't need to upate the first and target buckets
+ * since they will not be moved into the new cluster.
+ * b) Otherwise, move the bottom half of the xattrs in the last cluster into
+ * the new one. And we set the extend flag to zero if the insert place is
+ * moved into the new allocated cluster since no extend is needed.
+ */
+static errcode_t ocfs2_adjust_xattr_cross_cluster(ocfs2_cached_inode *ci,
+ uint64_t *first_blkno,
+ struct ocfs2_xattr_buf *target,
+ uint64_t new_blk,
+ uint32_t prev_clusters,
+ uint32_t *v_start,
+ int *extend)
+{
+ errcode_t ret;
+ uint64_t last_blk = *first_blkno + ((prev_clusters - 1) *
+ ocfs2_clusters_to_blocks(ci->ci_fs, 1));
+
+ DEBUG("adjust xattrs from cluster #%"PRIu64" len %u to #%"PRIu64
+ "first bucket at #%"PRIu64"\n",
+ target->xb_blkno, prev_clusters, new_blk, *first_blkno);
+
+ if (ocfs2_xattr_buckets_per_cluster(ci->ci_fs) > 1)
+ return ocfs2_mv_xattr_bucket_cross_cluster(ci, first_blkno,
+ target,
+ new_blk,
+ prev_clusters,
+ v_start);
+
+ /* The start of the last cluster in the first extent */
+ if (prev_clusters > 1 && target->xb_blkno != last_blk) {
+ ret = ocfs2_mv_xattr_buckets(ci, *first_blkno, target, last_blk,
+ new_blk, 0, v_start);
+ } else {
+ ret = ocfs2_divide_xattr_cluster(ci, target,
+ new_blk, v_start);
+ if (target->xb_blkno == last_blk && extend)
+ *extend = 0;
+ }
+
+ return ret;
+}
+
+
+/*
+ * Add a new cluster for xattr storage.
+ *
+ * If the new cluster is contiguous with the previous one, it will be
+ * appended to the same extent record, and num_clusters will be updated.
+ * If not, we will insert a new extent for it and move some xattrs in
+ * the last cluster into the new allocated one.
+ * We also need to limit the maximum size of a btree leaf, otherwise we'll
+ * lose the benefits of hashing because we'll have to search large leaves.
+ * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
+ * if it's bigger).
+ *
+ * first_blkno indicates the first bucket of the previous extent rec and target
+ * indicates the bucket we will insert the new xattrs. They will be updated
+ * when the target bucket is moved into the new cluster.
+ */
+static errcode_t ocfs2_add_new_xattr_cluster(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_buf *block,
+ uint64_t *first_blkno,
+ struct ocfs2_xattr_buf *target,
+ uint32_t *num_clusters,
+ uint32_t prev_cpos,
+ int *extend)
+{
+ int ret;
+ uint16_t bpc = ocfs2_clusters_to_blocks(ci->ci_fs, 1);
+ uint32_t prev_clusters = *num_clusters;
+ uint32_t num_bits, v_start = 0;
+ uint64_t blkno;
+ struct ocfs2_extent_tree et;
+ DEBUG("Add new xattr cluster for inode #%"PRIu64", previous xattr hash"
+ " = %u, previous xattr blkno #%"PRIu64"\n", ci->ci_blkno,
+ prev_cpos, *first_blkno);
+
+ ocfs2_init_xattr_tree_extent_tree(&et, ci->ci_fs, block->xb_buf,
+ block->xb_blkno);
+ ret = ocfs2_new_clusters(ci->ci_fs, 1, 1, &blkno, &num_bits);
+ if (ret)
+ return ret;
+ DEBUG("Allocating %u clusters at block #%"PRIu64" for xattr in"
+ " inode #%"PRIu64"\n", num_bits, blkno, ci->ci_blkno);
+
+ if (*first_blkno + (prev_clusters * bpc) == blkno &&
+ (prev_clusters + num_bits) * ci->ci_fs->fs_clustersize <=
+ OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
+ /*
+ * If this cluster is contiguous with the old one and
+ * adding this new cluster, we don't surpass the limit of
+ * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
+ * initialized and used like other buckets in the previous
+ * cluster.
+ * So add it as a contiguous one. The caller will handle
+ * its init process.
+ */
+ v_start = prev_cpos + prev_clusters;
+ *num_clusters = prev_clusters + num_bits;
+ DEBUG("Add contiguous %u clusters to previous extent rec.\n",
+ num_bits);
+ } else {
+ ret = ocfs2_adjust_xattr_cross_cluster(ci,
+ first_blkno,
+ target,
+ blkno,
+ prev_clusters,
+ &v_start,
+ extend);
+ if (ret)
+ return ret;
+ }
+
+ DEBUG("Insert %u clusters at block #%"PRIu64" for xattr at %u\n",
+ num_bits, blkno, v_start);
+ ret = ocfs2_tree_insert_extent(ci->ci_fs, &et, v_start, blkno,
+ num_bits, 0);
+ if (ret)
+ return ret;
+ ret = ocfs2_write_xattr_block(ci->ci_fs, block->xb_blkno,
+ block->xb_buf);
+
+ return ret;
+}
+
+/*
+ * We are given an extent. 'first' is the bucket at the very front of
+ * the extent. The extent has space for an additional bucket past
+ * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
+ * of the target bucket. We wish to shift every bucket past the target
+ * down one, filling in that additional space. When we get back to the
+ * target, we split the target between itself and the now-empty bucket
+ * at target+1 (aka, target_blkno + blks_per_bucket).
+ */
+static errcode_t ocfs2_extend_xattr_bucket(ocfs2_cached_inode *ci,
+ uint64_t first_blkno,
+ struct ocfs2_xattr_buf *target,
+ uint32_t num_clusters)
+{
+ errcode_t ret;
+ ocfs2_filesys *fs = ci->ci_fs;
+ uint16_t bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ uint64_t end_blk;
+ char *first_bucket = NULL;
+ uint16_t new_bucket;
+ struct ocfs2_xattr_header *xh = NULL;
+
+ DEBUG("extend xattr bucket in #%"PRIu64", xattr extend rec starting"
+ " from #%"PRIu64", len = %u\n", target->xb_blkno,
+ first_blkno, num_clusters);
+
+ if (first_blkno == target->xb_blkno)
+ first_bucket = target->xb_buf;
+ else {
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &first_bucket);
+ if (ret)
+ return ret;
+ ret = ocfs2_read_xattr_bucket(fs, first_blkno, first_bucket);
+ if (ret)
+ goto out;
+ }
+
+ xh = (struct ocfs2_xattr_header *)first_bucket;
+ new_bucket = xh->xh_num_buckets;
+ /* The extent must have room for an additional bucket */
+ ASSERT(new_bucket <
+ (num_clusters * ocfs2_xattr_buckets_per_cluster(fs)));
+
+ /* end_blk points to the last existing bucket */
+ end_blk = first_blkno + ((new_bucket - 1) * bpb);
+
+ /*
+ * end_blk is the start of the last existing bucket.
+ * Thus, (end_blk - target_blk) covers the target bucket and
+ * every bucket after it up to, but not including, the last
+ * existing bucket. Then we add the last existing bucket, the
+ * new bucket, and the first bucket (3 * blk_per_bucket).
+ */
+
+ while (end_blk != target->xb_blkno) {
+ ret = ocfs2_cp_xattr_bucket(ci, end_blk, end_blk + bpb);
+ if (ret)
+ goto out;
+ end_blk -= bpb;
+ }
+
+ /* Move half of the xattr in target_blkno to the next bucket. */
+ ret = ocfs2_divide_xattr_bucket(ci, target, target->xb_blkno + bpb,
+ NULL, 0);
+ if (ret)
+ goto out;
+
+ xh->xh_num_buckets += 1;
+ ret = ocfs2_write_xattr_bucket(fs, first_blkno, first_bucket);
+
+out:
+ if (first_bucket)
+ ocfs2_free(&first_bucket);
+ return ret;
+}
+
+static errcode_t ocfs2_add_new_xattr_bucket(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_buf *block,
+ struct ocfs2_xattr_buf *target)
+{
+ errcode_t ret;
+ int extend = 1;
+ struct ocfs2_xattr_block *xb =
+ (struct ocfs2_xattr_block *)block->xb_buf;
+ uint32_t name_hash = bucket_xh(target)->xh_entries[0].xe_name_hash;
+ uint32_t e_cpos, num_clusters;
+ ocfs2_filesys *fs = ci->ci_fs;
+ uint16_t bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ char *first_bucket = NULL;
+ uint64_t first_blkno;
+ uint16_t num_buckets, first_num_buckets;
+
+ DEBUG("Add new xattr bucket starting from #%"PRIu64"\n",
+ target->xb_blkno);
+
+ ret = ocfs2_xattr_get_rec(fs, xb, name_hash, &first_blkno, &e_cpos,
+ &num_clusters);
+ if (ret)
+ return ret;
+
+ if (first_blkno == target->xb_blkno) {
+ first_num_buckets = bucket_xh(target)->xh_num_buckets;
+ } else {
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &first_bucket);
+ if (ret)
+ return ret;
+
+ ret = ocfs2_read_xattr_bucket(ci->ci_fs, first_blkno,
+ first_bucket);
+ if (ret)
+ goto out;
+ first_num_buckets = ((struct ocfs2_xattr_header *)first_bucket)
+ ->xh_num_buckets;
+ }
+
+ num_buckets = ocfs2_xattr_buckets_per_cluster(fs) * num_clusters;
+ if (num_buckets == first_num_buckets) {
+ /*
+ * This can move first+target if the target bucket moves
+ * to the new extent.
+ */
+ ret = ocfs2_add_new_xattr_cluster(ci,
+ block,
+ &first_blkno,
+ target,
+ &num_clusters,
+ e_cpos,
+ &extend);
+ if (ret)
+ goto out;
+ }
+
+ if (extend) {
+ ret = ocfs2_extend_xattr_bucket(ci,
+ first_blkno,
+ target,
+ num_clusters);
+ }
+
+out:
+ if (first_bucket)
+ ocfs2_free(&first_bucket);
+
+ return ret;
+}
+
+static int ocfs2_xattr_bucket_value_truncate(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_search *xs,
+ int len)
+{
+ int ret, offset;
+ uint64_t value_blk;
+ size_t blocksize = ci->ci_fs->fs_blocksize;
+ struct ocfs2_xattr_value_root *xv;
+
+ offset = xs->here->xe_name_offset +
+ OCFS2_XATTR_SIZE(xs->here->xe_name_len);
+
+ value_blk = offset / blocksize;
+
+ /* We don't allow ocfs2_xattr_value to be stored in different block. */
+ ASSERT(value_blk == (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
+
+ xv = (struct ocfs2_xattr_value_root *)(xs->blkbuf + offset);
+
+ DEBUG("truncate in xattr bucket #%"PRIu64" to %d bytes.\n",
+ xs->blkno, len);
+ ret = ocfs2_xattr_value_truncate(ci, xv, xs->blkbuf, xs->blkno, len);
+ if (ret)
+ goto out;
+
+ xs->here->xe_value_size = len;
+ ret = ocfs2_write_xattr_bucket(ci->ci_fs, xs->blkno, xs->blkbuf);
+
+out:
+ return ret;
+}
+
+static void ocfs2_xattr_set_entry_normal(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs,
+ uint32_t name_hash,
+ int local)
+{
+ struct ocfs2_xattr_entry *last, *xe;
+ int name_len = strlen(xi->name);
+ struct ocfs2_xattr_header *xh = xs->header;
+ uint16_t count = xh->xh_count, start;
+ size_t blocksize = ci->ci_fs->fs_blocksize;
+ char *val;
+ size_t offs, size, new_size;
+
+ last = &xh->xh_entries[count];
+ if (!xs->not_found) {
+ xe = xs->here;
+ offs = xe->xe_name_offset;
+ if (ocfs2_xattr_is_local(xe))
+ size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xe->xe_value_size);
+ else
+ size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
+
+ /*
+ * If the new value will be stored outside, xi->value has been
+ * initalized as an empty ocfs2_xattr_value_root, and the same
+ * goes with xi->value_len, so we can set new_size safely here.
+ * See ocfs2_xattr_set_in_bucket.
+ */
+ new_size = OCFS2_XATTR_SIZE(name_len) +
+ OCFS2_XATTR_SIZE(xi->value_len);
+
+ xh->xh_name_value_len -= size;
+ if (xi->value) {
+ if (new_size > size)
+ goto set_new_name_value;
+
+ /* Now replace the old value with new one. */
+ if (local)
+ xe->xe_value_size = xi->value_len;
+ else
+ xe->xe_value_size = 0;
+
+ val = xs->blkbuf + offs;
+ memset(val + OCFS2_XATTR_SIZE(name_len), 0,
+ size - OCFS2_XATTR_SIZE(name_len));
+ if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
+ memcpy(val + OCFS2_XATTR_SIZE(name_len),
+ xi->value, xi->value_len);
+
+ xh->xh_name_value_len += new_size;
+ ocfs2_xattr_set_local(xe, local);
+ return;
+ } else {
+ /*
+ * Remove the old entry if there is more than one.
+ * We don't remove the last entry so that we can
+ * use it to indicate the hash value of the empty
+ * bucket.
+ */
+ last -= 1;
+ xh->xh_count -= 1;
+ if (xh->xh_count) {
+ memmove(xe, xe + 1,
+ (void *)last - (void *)xe);
+ memset(last, 0, ENTRY_SIZE);
+ } else
+ xh->xh_free_start =
+ OCFS2_XATTR_BUCKET_SIZE;
+
+ return;
+ }
+ } else {
+ /* find a new entry for insert. */
+ int low = 0, high = count - 1, tmp;
+ struct ocfs2_xattr_entry *tmp_xe;
+
+ while (low <= high && count) {
+ tmp = (low + high) / 2;
+ tmp_xe = &xh->xh_entries[tmp];
+
+ if (name_hash > tmp_xe->xe_name_hash)
+ low = tmp + 1;
+ else if (name_hash <
+ tmp_xe->xe_name_hash)
+ high = tmp - 1;
+ else {
+ low = tmp;
+ break;
+ }
+ }
+
+ xe = &xh->xh_entries[low];
+ if (low != count)
+ memmove(xe + 1, xe, (void *)last - (void *)xe);
+
+ xh->xh_count += 1;
+ memset(xe, 0, ENTRY_SIZE);
+ xe->xe_name_hash = name_hash;
+ xe->xe_name_len = name_len;
+ ocfs2_xattr_set_type(xe, xi->name_index);
+ }
+
+set_new_name_value:
+ /* Insert the new name+value. */
+ size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
+
+ /*
+ * We must make sure that the name/value pair
+ * exists in the same block.
+ */
+ offs = xh->xh_free_start;
+ start = offs - size;
+
+ if (start / ci->ci_fs->fs_blocksize !=
+ (offs - 1) / ci->ci_fs->fs_blocksize) {
+ offs = offs - offs % blocksize;
+ xh->xh_free_start = offs;
+ }
+
+ val = xs->blkbuf + offs - size;
+ xe->xe_name_offset = offs - size;
+
+ memset(val, 0, size);
+ memcpy(val, xi->name, name_len);
+ memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
+
+ xe->xe_value_size = xi->value_len;
+ ocfs2_xattr_set_local(xe, local);
+ xs->here = xe;
+ xh->xh_free_start -= size;
+ xh->xh_name_value_len += size;
+
+ return;
+}
+
+static errcode_t ocfs2_xattr_bucket_remove_xs(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_search *xs)
+{
+ struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
+ xs->blkbuf;
+ struct ocfs2_xattr_entry *last = &xh->xh_entries[xh->xh_count - 1];
+
+ /* Remove the old entry. */
+ memmove(xs->here, xs->here + 1,
+ (void *)last - (void *)xs->here);
+ memset(last, 0, ENTRY_SIZE);
+ xh->xh_count -= 1;
+
+ return ocfs2_write_xattr_bucket(ci->ci_fs, xs->blkno, xs->blkbuf);
+}
+
+
+static int ocfs2_xattr_bucket_set_value_outside(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_search *xs,
+ char *val,
+ int value_len)
+{
+ struct ocfs2_xattr_value_root *xv;
+
+ xv = (struct ocfs2_xattr_value_root *)(xs->blkbuf +
+ xs->here->xe_name_offset +
+ OCFS2_XATTR_SIZE(xs->here->xe_name_len));
+
+ return __ocfs2_xattr_set_value_outside(ci, xv, xs->blkbuf, xs->blkno,
+ val, value_len);
+}
+
+/*
+ * 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 errcode_t ocfs2_xattr_set_in_bucket(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search *xs)
+{
+ errcode_t ret = 0;
+ int local = 1;
+ size_t value_len;
+ char *val = (char *)xi->value;
+ struct ocfs2_xattr_entry *xe = xs->here;
+ uint32_t uuid_hash = ci->ci_fs->fs_super->id2.i_super.s_uuid_hash;
+ uint32_t name_hash = ocfs2_xattr_name_hash(uuid_hash, xi->name,
+ strlen(xi->name));
+
+ 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->value_len > OCFS2_XATTR_INLINE_SIZE)
+ value_len = xi->value_len;
+ else
+ value_len = 0;
+
+ ret = ocfs2_xattr_bucket_value_truncate(ci, xs, value_len);
+ if (ret)
+ goto out;
+
+ if (value_len)
+ goto set_value_outside;
+ }
+
+ value_len = xi->value_len;
+ /* So we have to handle the inside block change now. */
+ if (value_len > OCFS2_XATTR_INLINE_SIZE) {
+ /*
+ * If the new value will be stored outside of block,
+ * initalize a new empty value root and insert it first.
+ */
+ local = 0;
+ xi->value = (const char *)&def_xv;
+ xi->value_len = OCFS2_XATTR_ROOT_SIZE;
+ }
+
+ ocfs2_xattr_set_entry_normal(ci, xi, xs, name_hash, local);
+ ret = ocfs2_write_xattr_bucket(ci->ci_fs, xs->blkno, xs->blkbuf);
+ if (ret)
+ goto out;
+
+ if (value_len <= OCFS2_XATTR_INLINE_SIZE)
+ goto out;
+
+ /* allocate the space now for the outside block storage. */
+ ret = ocfs2_xattr_bucket_value_truncate(ci, xs, value_len);
+ if (ret) {
+ errcode_t ret2;
+ if (xs->not_found) {
+ /*
+ * We can't allocate enough clusters for outside
+ * storage and we have allocated xattr already,
+ * so need to remove it.
+ */
+ ret2 = ocfs2_xattr_bucket_remove_xs(ci, xs);
+ }
+ goto out;
+ }
+
+set_value_outside:
+ ret = ocfs2_xattr_bucket_set_value_outside(ci, xs, val, value_len);
+out:
+ return ret;
+}
+
+
+static errcode_t ocfs2_xattr_set_entry_index_block(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search_info *xsi)
+{
+ struct ocfs2_xattr_header *xh;
+ struct ocfs2_xattr_entry *xe;
+ uint16_t count, header_size, xh_free_start;
+ int free, max_free, need, old;
+ size_t value_size = 0, name_len = strlen(xi->name);
+ ocfs2_filesys *fs = ci->ci_fs;
+ size_t blocksize = fs->fs_blocksize;
+ int allocation = 0;
+ errcode_t ret = 0;
+ struct ocfs2_xattr_search *xbks = NULL;
+ struct ocfs2_xattr_buf bucket;
+ struct ocfs2_xattr_buf block = {
+ .xb_buf = xsi->xi_block->blkbuf,
+ .xb_blkno = xsi->xi_block->blkno,
+ };
+
+ DEBUG("Set xattr %s in xattr index block\n", xi->name);
+
+try_again:
+ xbks = xsi->xi_bucket;
+ bucket.xb_buf = xbks->blkbuf;
+ bucket.xb_blkno = xbks->blkno;
+
+ xh = xbks->header;
+ count = xh->xh_count;
+ xh_free_start = xh->xh_free_start;
+ header_size = HEADER_SIZE + count * ENTRY_SIZE;
+ max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
+ xh->xh_name_value_len - OCFS2_XATTR_HEADER_GAP;
+
+ if (header_size > blocksize) {
+ DEBUG("bucket #%"PRIu64" has header size of %u which exceed"
+ " block size\n", xbks->blkno, header_size);
+ ASSERT(header_size <= blocksize);
+ }
+ if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
+ value_size = OCFS2_XATTR_ROOT_SIZE;
+ else if (xi->value)
+ value_size = OCFS2_XATTR_SIZE(xi->value_len);
+
+ if (xbks->not_found)
+ need = ENTRY_SIZE + OCFS2_XATTR_SIZE(name_len) + value_size;
+ else {
+ need = value_size + OCFS2_XATTR_SIZE(name_len);
+
+ /*
+ * 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 = xbks->here;
+ if (ocfs2_xattr_is_local(xe))
+ old = OCFS2_XATTR_SIZE(xe->xe_value_size);
+ else
+ old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
+
+ if (old >= value_size)
+ need = 0;
+ }
+
+ 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;
+
+ DEBUG("xbks->not_found = %d, in xattr bucket #%"PRIu64": free = %d,"
+ " need = %d, max_free = %d, xh_free_start = %u,"
+ " xh_name_value_len = %u\n", xbks->not_found,
+ xbks->blkno, free, need, max_free, xh->xh_free_start,
+ xh->xh_name_value_len);
+
+ if (free < need ||
+ (xbks->not_found && count == ocfs2_xattr_max_xe_in_bucket(fs))) {
+ if (need <= max_free &&
+ count < ocfs2_xattr_max_xe_in_bucket(fs)) {
+ /*
+ * 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(ci, xbks->blkbuf,
+ xbks->blkno);
+ if (ret)
+ goto out;
+
+ xh_free_start = 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;
+
+ if (free >= need)
+ goto xattr_set;
+
+ DEBUG("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);
+ }
+
+ /*
+ * We have to add new buckets or clusters and one
+ * allocation should leave us enough space for insert.
+ */
+ ASSERT(!allocation);
+
+ /*
+ * 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(ci, xbks->blkbuf,
+ xbks->blkno,
+ xi->name);
+ if (ret)
+ goto out;
+ ret = ocfs2_add_new_xattr_bucket(ci, &block, &bucket);
+ if (ret)
+ goto out;
+
+ /*
+ * ocfs2_add_new_xattr_bucket() will have updated
+ * xs->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.
+ */
+ ret = ocfs2_xattr_index_block_find(ci, xi->name_index,
+ xi->name, xsi);
+ if (ret)
+ goto out;
+ allocation = 1;
+ goto try_again;
+ }
+
+xattr_set:
+ ret = ocfs2_xattr_set_in_bucket(ci, xi, xsi->xi_bucket);
+out:
+ return ret;
+}
+
+
+/*
+ * ocfs2_xattr_block_set()
+ *
+ * Set, replace or remove an extended attribute into external block.
+ *
+ */
+static errcode_t ocfs2_xattr_block_set(ocfs2_cached_inode *ci,
+ struct ocfs2_xattr_info *xi,
+ struct ocfs2_xattr_search_info *xsi)
+{
+ char *buf = xsi->xi_block->blkbuf;
+ ocfs2_filesys *fs = ci->ci_fs;
+ struct ocfs2_xattr_block *xblk = NULL;
+ uint64_t blkno;
+ int ret;
+
+ if (!xsi->xi_block->blkno) {
+ /* Get and initialize ocfs2_xattr_block */
+ ret = ocfs2_new_xattr_block(fs, &blkno);
+ if (ret)
+ return ret;
+ /*
+ * ocfs2_new_xattr_block already format
+ * that block to xattr block.
+ */
+ ret = ocfs2_read_xattr_block(fs, blkno, buf);
+ if (ret)
+ goto out;
+
+ xsi->xi_block->blkno = blkno;
+
+ xblk = (struct ocfs2_xattr_block *)buf;
+ xsi->xi_block->header = &xblk->xb_attrs.xb_header;
+ xsi->xi_block->base = (char *)xsi->xi_block->header;
+ xsi->xi_block->end = buf + fs->fs_blocksize;
+ xsi->xi_block->here = xsi->xi_block->header->xh_entries;
+
+ ci->ci_inode->i_xattr_loc = blkno;
+ ret = ocfs2_write_cached_inode(fs, ci);
+ if (ret)
+ goto out;
+ ret = ocfs2_write_xattr_block(fs, blkno, buf);
+ if (ret)
+ goto out;
+ } else
+ xblk = (struct ocfs2_xattr_block *)xsi->xi_block->blkbuf;
+
+ if (!(xblk->xb_flags & OCFS2_XATTR_INDEXED)) {
+ /* Set extended attribute into external block */
+ ret = ocfs2_xattr_set_entry(ci, xi, xsi->xi_block,
+ OCFS2_HAS_XATTR_FL);
+ if (!ret)
+ ret = ocfs2_write_xattr_block(fs, xsi->xi_block->blkno,
+ xsi->xi_block->blkbuf);
+ if (ret != OCFS2_ET_XATTR_NOSPC)
+ goto out;
+
+ ret = ocfs2_xattr_create_index_block(ci, xsi);
+ if (ret)
+ goto out;
+ }
+
+ ret = ocfs2_xattr_set_entry_index_block(ci, xi, xsi);
+ if (ret)
+ goto out;
+ ret = ocfs2_write_xattr_block(fs, xsi->xi_block->blkno,
+ xsi->xi_block->blkbuf);
+ if (ret)
+ goto out;
+ ret = ocfs2_write_xattr_bucket(fs, xsi->xi_bucket->blkno,
+ xsi->xi_bucket->blkbuf);
+
+out:
+ return ret;
+}
+
+static errcode_t ocfs2_xattr_set_generic(ocfs2_cached_inode *ci,
+ int name_index,
+ const char *name,
+ const char *value,
+ size_t value_len)
+{
+ errcode_t ret = 0;
+ ocfs2_filesys *fs = ci->ci_fs;
+ int bpb = ocfs2_blocks_per_xattr_bucket(fs);
+ struct ocfs2_xattr_info xi = {
+ .name_index = name_index,
+ .name = name,
+ .value = value,
+ .value_len = value_len,
+ };
+ struct ocfs2_xattr_search xis = {
+ .blkbuf = (char *)ci->ci_inode,
+ .blkno = ci->ci_blkno,
+ .not_found = OCFS2_ET_XATTR_NODATA,
+ };
+ struct ocfs2_xattr_search xbs = {.not_found = OCFS2_ET_XATTR_NODATA,};
+ struct ocfs2_xattr_search xbks = {.not_found = OCFS2_ET_XATTR_NODATA,};
+ struct ocfs2_xattr_search_info xsi = {
+ .xi_inode = &xis,
+ .xi_block = &xbs,
+ .xi_bucket = &xbks,
+ };
+
+ /* alloc block buffer for xattr block */
+ ret = ocfs2_malloc_block(fs->fs_io, &xbs.blkbuf);
+ if (ret)
+ return ret;
+
+ /* alloc block buffer for xattr bucket */
+ ret = ocfs2_malloc_blocks(fs->fs_io, bpb, &xbks.blkbuf);
+ if (ret)
+ return ret;
+
+ /*
+ * Scan inode and external block to find the same name
+ * extended attribute and collect search infomation.
+ */
+ ret = ocfs2_xattr_ibody_find(ci, name_index, name, &xis);
+ if (ret)
+ goto cleanup;
+ if (xis.not_found) {
+ ret = ocfs2_xattr_block_find(ci, name_index, name, &xsi);
+ if (ret)
+ goto cleanup;
+ }
+ if (!xi.value) {
+ /* Remove existing extended attribute */
+ if (!xis.not_found)
+ ret = ocfs2_xattr_ibody_set(ci, &xi, &xis);
+ else if (!xbs.not_found)
+ ret = ocfs2_xattr_block_set(ci, &xi, &xsi);
+ } else {
+ /* We always try to set extended attribute into inode first*/
+ ret = ocfs2_xattr_ibody_set(ci, &xi, &xis);
+ if (!ret && !xbs.not_found) {
+ /*
+ * If succeed and that extended attribute existing in
+ * external block, then we will remove it.
+ */
+ xi.value = NULL;
+ xi.value_len = 0;
+ ret = ocfs2_xattr_block_set(ci, &xi, &xsi);
+ } else if (ret == OCFS2_ET_XATTR_NOSPC) {
+ if (ci->ci_inode->i_xattr_loc && !xsi.xi_block->blkno) {
+ ret = ocfs2_xattr_block_find(ci, name_index,
+ name, &xsi);
+ if (ret)
+ goto cleanup;
+ }
+ /*
+ * If no space in inode, we will set extended attribute
+ * into external block.
+ */
+ ret = ocfs2_xattr_block_set(ci, &xi, &xsi);
+ if (ret)
+ goto cleanup;
+ if (!xis.not_found) {
+ /*
+ * If succeed and that extended attribute
+ * existing in inode, we will remove it.
+ */
+ xi.value = NULL;
+ xi.value_len = 0;
+ ret = ocfs2_xattr_ibody_set(ci, &xi, &xis);
+ }
+ }
+ }
+cleanup:
+ if (xbs.blkbuf)
+ ocfs2_free(&xbs.blkbuf);
+ if (xbks.blkbuf)
+ ocfs2_free(&xbks.blkbuf);
+ return ret;
+}
+
+static const char *strcmp_prefix(const char *a, const char *a_prefix)
+{
+ while (*a_prefix && *a == *a_prefix) {
+ a++;
+ a_prefix++;
+ }
+ return *a_prefix ? NULL : a;
+}
+
+/*
+ * Find the xattr_handler with the matching prefix.
+ */
+static int ocfs2_xattr_resolve_name(const char **name)
+{
+ int i;
+
+ if (!*name)
+ return -1;
+
+ for (i = 1; i < OCFS2_XATTR_MAX; i++) {
+ const char *n = strcmp_prefix(*name, ocfs2_xattr_prefix[i]);
+ if (n) {
+ *name = n;
+ return i;
+ }
+ }
+ return -1;
+}
+
+/*
+ * Read the value of xattr 'name' from the inode 'ci'. Fill 'buf' up
+ * to 'count' bytes. If 'got' is not NULL, fill it with how many bytes
+ * were actually read.
+ */
+errcode_t ocfs2_xattr_get(ocfs2_cached_inode *ci, const char *name,
+ char *buf, size_t count, size_t *got)
+{
+ int name_index;
+ struct ocfs2_super_block *sb = OCFS2_RAW_SB(ci->ci_fs->fs_super);
+ struct ocfs2_dinode *di = ci->ci_inode;
+
+ if (!(sb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
+ return OCFS2_ET_UNSUPP_FEATURE;
+ if (!(di->i_dyn_features & OCFS2_HAS_XATTR_FL))
+ return OCFS2_ET_XATTR_NODATA;
+
+ name_index = ocfs2_xattr_resolve_name(&name);
+ if (name_index < 0)
+ return OCFS2_ET_INVALID_ARGUMENT;
+ return ocfs2_xattr_get_generic(ci, name_index, name, buf, count, got);
+}
+
+/*
+ * Set the value of xattr 'name' on the inode 'ci'. The value is 'count'
+ * bytes long in 'buf'.
+ */
+errcode_t ocfs2_xattr_set(ocfs2_cached_inode *ci,
+ const char *name,
+ const char *buf,
+ size_t count)
+{
+ int name_index = 0;
+ struct ocfs2_super_block *sb = OCFS2_RAW_SB(ci->ci_fs->fs_super);
+
+ if (!(sb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_XATTR))
+ return OCFS2_ET_UNSUPP_FEATURE;
+
+ name_index = ocfs2_xattr_resolve_name(&name);
+ if (name_index < 0)
+ return OCFS2_ET_INVALID_ARGUMENT;
+ return ocfs2_xattr_set_generic(ci, name_index, name, buf, count);
+}
diff --git a/libocfs2/xattr.h b/libocfs2/xattr.h
index 475f0f2..a66b0c0 100644
--- a/libocfs2/xattr.h
+++ b/libocfs2/xattr.h
@@ -17,3 +17,18 @@
#define OCFS2_MAX_XATTR_TREE_LEAF_SIZE 65536
+enum ocfs2_xattr_type {
+ OCFS2_XATTR_INDEX_USER = 1,
+ OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS,
+ OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
+ OCFS2_XATTR_INDEX_TRUSTED,
+ OCFS2_XATTR_INDEX_SECURITY,
+ OCFS2_XATTR_MAX
+};
+
+struct ocfs2_xattr_value_buf {
+ char *vb_buf;
+ uint64_t vb_blkno;
+ struct ocfs2_xattr_value_root *vb_xv;
+};
+
--
1.5.4.1
More information about the Ocfs2-tools-devel
mailing list