return ptr;
}
+static inline void *xstrndup1(const char *file, int line, const char *func,
+ const char *s, size_t n)
+{
+ void *ptr;
+
+ if (s == NULL)
+ LS3_FATAL("NULL pointer at (%s:%d:%s)\n", file, line, func);
+
+ ptr = strndup(s, n);
+
+ if (ptr == NULL)
+ LS3_OOM_AT(file, line, func, strnlen(s, n));
+
+ return ptr;
+}
+
#define xmalloc(size) (xmalloc1(__FILE__, __LINE__, __func__, (size)))
#define xcalloc(nmemb, size) (xcalloc1(__FILE__, __LINE__, __func__, (nmemb), (size)))
#define xstrdup(s) (xstrdup1(__FILE__, __LINE__, __func__, (s)))
+#define xstrndup(s, n) (xstrndup1(__FILE__, __LINE__, __func__, (s), (n)))
static inline const char *xstrerror(intmax_t err)
{
assert(lipe_list_empty(&attrs->loa_xattrs));
}
-void lipe_object_attrs_reset(struct ls3_object_attrs *attrs)
+void lipe_object_attrs_reset(struct ls3_object_attrs *loa)
{
- struct lov_user_md *lum = attrs->loa_lum;
- int lum_size = attrs->loa_lum_size;
-
- lipe_object_attrs_links_fini(attrs);
- lipe_object_attrs_paths_fini(attrs);
- lipe_object_attrs_xattrs_fini(attrs);
- memset(lum, 0, lum_size);
- memset(attrs, 0, sizeof(*attrs));
- attrs->loa_lum = lum;
- attrs->loa_lum_size = lum_size;
- LIPE_INIT_LIST_HEAD(&attrs->loa_links);
- LIPE_INIT_LIST_HEAD(&attrs->loa_paths);
- LIPE_INIT_LIST_HEAD(&attrs->loa_xattrs);
+ loa->loa_attr_bits = 0;
+ loa->loa_ino = 0;
+ memset(&loa->loa_file_fid, 0, sizeof(loa->loa_file_fid));
+ memset(&loa->loa_self_fid, 0, sizeof(loa->loa_self_fid));
+ lipe_object_attrs_links_fini(loa);
+ lipe_object_attrs_paths_fini(loa);
+ lipe_object_attrs_xattrs_fini(loa);
}
-int lipe_object_attrs_init(struct ls3_object_attrs *attrs)
+int lipe_object_attrs_init(struct ls3_object_attrs *loa)
{
- memset(attrs, 0, sizeof(*attrs));
- attrs->loa_lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT,
- LOV_USER_MAGIC_V3);
+ memset(loa, 0, sizeof(*loa));
+ loa->loa_lum_size = lov_user_md_size(LOV_MAX_STRIPE_COUNT,
+ LOV_USER_MAGIC_V3);
+ loa->loa_lum = xmalloc(loa->loa_lum_size);
- attrs->loa_lum = xcalloc(1, attrs->loa_lum_size);
+ loa->loa_link_buf_size = XATTR_SIZE_MAX;
+ loa->loa_link_buf = xmalloc(loa->loa_link_buf_size);
- LIPE_INIT_LIST_HEAD(&attrs->loa_links);
- LIPE_INIT_LIST_HEAD(&attrs->loa_paths);
- LIPE_INIT_LIST_HEAD(&attrs->loa_xattrs);
+ loa->loa_lmv_buf_size = XATTR_SIZE_MAX;
+ loa->loa_lmv_buf = xmalloc(loa->loa_lmv_buf_size);
+
+ LIPE_INIT_LIST_HEAD(&loa->loa_links);
+ LIPE_INIT_LIST_HEAD(&loa->loa_paths);
+ LIPE_INIT_LIST_HEAD(&loa->loa_xattrs);
return 0;
}
-void lipe_object_attrs_fini(struct ls3_object_attrs *attrs)
+void lipe_object_attrs_fini(struct ls3_object_attrs *loa)
{
- lipe_object_attrs_links_fini(attrs);
- lipe_object_attrs_paths_fini(attrs);
- lipe_object_attrs_xattrs_fini(attrs);
- free(attrs->loa_lum);
+ lipe_object_attrs_links_fini(loa);
+ lipe_object_attrs_paths_fini(loa);
+ lipe_object_attrs_xattrs_fini(loa);
+ free(loa->loa_lum);
+ free(loa->loa_link_buf);
+ free(loa->loa_lmv_buf);
}
-int lipe_object_attrs_add_link(struct ls3_object_attrs *attrs,
- const struct lu_fid *parent_fid,
- const char *name)
+static int lipe_object_attrs_add_link(struct ls3_object_attrs *attrs,
+ const struct lu_fid *parent_fid,
+ const char *name, int namelen)
{
struct lipe_link_entry *lle = NULL;
+ if (namelen <= 0) {
+ LS3_ERROR("invalid link xattr: namelen = %d\n", namelen);
+ return -EOVERFLOW; /* ? */
+ }
+
lle = xcalloc(1, sizeof(*lle));
lle->lle_parent_fid = *parent_fid;
- lle->lle_name = xstrdup(name);
+ lle->lle_name = xstrndup(name, namelen);
lipe_list_add_tail(&lle->lle_linkage, &attrs->loa_links);
for (i = 0; i < reccount; i++) {
unsigned int reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
struct lu_fid parent_fid;
+ int namelen;
pos += reclen;
if (pos > len) {
fid_be_to_cpu(&parent_fid, (const struct lu_fid *)&lee->lee_parent_fid);
- rc = lipe_object_attrs_add_link(attrs, &parent_fid, lee->lee_name);
+ namelen = reclen - offsetof(struct link_ea_entry, lee_name[0]);
+ rc = lipe_object_attrs_add_link(attrs, &parent_fid, lee->lee_name, namelen);
if (rc < 0)
return rc;
return -EINVAL;
}
+ assert(0 <= *value_size && *value_size <= buf_size);
+
return 0;
}
if (rc < 0)
return rc;
- if (loa->loa_lma_incompat & LMAI_ORPHAN)
+ if (loa->loa_lma_incompat & LMAI_ORPHAN) {
+ /* Then no links and loa_links is empty so good. */
goto out_ok;
+ }
- rc = ldiskfs_trusted_xattr_get(lo, XATTR_NAME_LINK, loa->loa_leh_buf, sizeof(loa->loa_leh_buf), &size);
+ rc = ldiskfs_trusted_xattr_get(lo, XATTR_NAME_LINK,
+ loa->loa_link_buf, loa->loa_link_buf_size, &size);
if (rc < 0)
return rc;
- rc = lipe_object_attrs_set_links(loa, loa->loa_leh_buf, size);
+ rc = lipe_object_attrs_set_links(loa, loa->loa_link_buf, size);
if (rc) {
LS3_ERROR_OBJ(lo, "cannot decode link xattr: rc = %d\n", rc);
return rc;
if (loa->loa_attr_bits & LS3_OBJECT_ATTR_LMV)
return 0;
- memset(loa->loa_lmv_buf, 0, sizeof(loa->loa_lmv_buf));
-
- rc = ldiskfs_trusted_xattr_get(lo, XATTR_NAME_LMV, loa->loa_lmv_buf, sizeof(loa->loa_lmv_buf), &size);
+ rc = ldiskfs_trusted_xattr_get(lo, XATTR_NAME_LMV,
+ loa->loa_lmv_buf, loa->loa_lmv_buf_size, &size);
if (rc < 0)
return rc;
+ /* TODO Store and check size. */
+
loa->loa_attr_bits |= LS3_OBJECT_ATTR_LMV;
return 0;
return false;
lmm = (union lmv_mds_md *)loa->loa_lmv_buf;
- if (lmm->lmv_magic == LMV_MAGIC_STRIPE)
- return true;
- return false;
+ return lmm->lmv_magic == LMV_MAGIC_STRIPE;
}
static int ldiskfs_read_attr_hsm(struct ls3_instance *li,