+ int rc = -1, fd = -1, i;
+ size_t lum_size;
+ struct llapi_layout *layout;
+ struct llapi_layout_comp *comp;
+ struct lov_user_md *lum = NULL;
+
+ if (path == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (!count)
+ return 0;
+
+ for (i = 0; i < count; i++) {
+ if (!ids[i] || !flags[i]) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (ids[i] > LCME_ID_MAX || (flags[i] & ~LCME_KNOWN_FLAGS)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* do not allow to set or clear INIT flag */
+ if (flags[i] & LCME_FL_INIT) {
+ errno = EINVAL;
+ return -1;
+ }
+ }
+
+ layout = __llapi_layout_alloc();
+ if (layout == NULL)
+ return -1;
+
+ layout->llot_is_composite = true;
+ for (i = 0; i < count; i++) {
+ comp = __llapi_comp_alloc(0);
+ if (comp == NULL)
+ goto out;
+
+ comp->llc_id = ids[i];
+ comp->llc_flags = flags[i];
+
+ list_add_tail(&comp->llc_list, &layout->llot_comp_list);
+ layout->llot_cur_comp = comp;
+ }
+
+ lum = llapi_layout_to_lum(layout);
+ if (lum == NULL)
+ goto out;
+
+ lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
+
+ fd = open(path, O_RDWR);
+ if (fd < 0)
+ goto out;
+
+ /* flush cached pages from clients */
+ rc = llapi_file_flush(fd);
+ if (rc) {
+ errno = -rc;
+ rc = -1;
+ goto out_close;
+ }
+
+ rc = fsetxattr(fd, XATTR_LUSTRE_LOV".set.flags", lum, lum_size, 0);
+ if (rc < 0)
+ goto out_close;
+
+ rc = 0;
+
+out_close:
+ if (fd >= 0) {
+ int tmp_errno = errno;
+ close(fd);
+ errno = tmp_errno;
+ }
+out:
+ if (lum)
+ free(lum);
+ llapi_layout_free(layout);
+ return rc;
+}
+
+/**
+ * Check if the file layout is composite.
+ *
+ * \param[in] layout the file layout to check
+ *
+ * \retval true composite
+ * \retval false not composite
+ */
+bool llapi_layout_is_composite(struct llapi_layout *layout)
+{
+ return layout->llot_is_composite;
+}
+
+/**
+ * Iterate every components in the @layout and call callback function @cb.
+ *
+ * \param[in] layout component layout list.
+ * \param[in] cb callback for each component
+ * \param[in] cbdata callback data
+ *
+ * \retval < 0 error happens during the iteration
+ * \retval LLAPI_LAYOUT_ITER_CONT finished the iteration w/o error
+ * \retval LLAPI_LAYOUT_ITER_STOP got something, stop the iteration
+ */
+int llapi_layout_comp_iterate(struct llapi_layout *layout,
+ llapi_layout_iter_cb cb, void *cbdata)
+{
+ int rc;
+
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+ if (rc < 0)
+ return rc;
+
+ /**
+ * make sure on success llapi_layout_comp_use() API returns 0 with
+ * USE_FIRST.
+ */
+ assert(rc == 0);
+
+ while (1) {
+ rc = cb(layout, cbdata);
+ if (rc != LLAPI_LAYOUT_ITER_CONT)
+ break;
+
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
+ if (rc < 0)
+ return rc;
+ else if (rc == 1) /* reached the last comp */
+ return LLAPI_LAYOUT_ITER_CONT;
+ }
+
+ return rc;
+}
+
+/**
+ * llapi_layout_merge() - Merge a composite layout into another one.
+ * @dst_layout: Destination composite layout.
+ * @src_layout: Source composite layout.
+ *
+ * This function copies all of the components from @src_layout and
+ * appends them to @dst_layout.
+ *
+ * Return: 0 on success or -1 on failure.
+ */
+int llapi_layout_merge(struct llapi_layout **dst_layout,
+ const struct llapi_layout *src_layout)
+{
+ struct llapi_layout *new_layout = *dst_layout;
+ struct llapi_layout_comp *new = NULL;
+ struct llapi_layout_comp *comp = NULL;
+ int i = 0;
+
+ if (src_layout == NULL ||
+ list_empty((struct list_head *)&src_layout->llot_comp_list))
+ return 0;
+
+ if (new_layout == NULL) {
+ new_layout = __llapi_layout_alloc();
+ if (new_layout == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+ }
+
+ list_for_each_entry(comp, &src_layout->llot_comp_list, llc_list) {
+ new = __llapi_comp_alloc(0);
+ if (new == NULL) {
+ errno = ENOMEM;
+ goto error;
+ }
+
+ new->llc_pattern = comp->llc_pattern;
+ new->llc_stripe_size = comp->llc_stripe_size;
+ new->llc_stripe_count = comp->llc_stripe_count;
+ new->llc_stripe_offset = comp->llc_stripe_offset;
+
+ if (comp->llc_pool_name[0] != '\0')
+ strncpy(new->llc_pool_name, comp->llc_pool_name,
+ sizeof(new->llc_pool_name));
+
+ for (i = 0; i < comp->llc_objects_count; i++) {
+ if (__llapi_comp_objects_realloc(new,
+ stripe_number_roundup(i)) < 0) {
+ errno = EINVAL;
+ __llapi_comp_free(new);
+ goto error;
+ }
+ new->llc_objects[i].l_ost_idx = \
+ comp->llc_objects[i].l_ost_idx;
+ }
+
+ new->llc_objects_count = comp->llc_objects_count;
+ new->llc_extent.e_start = comp->llc_extent.e_start;
+ new->llc_extent.e_end = comp->llc_extent.e_end;
+ new->llc_id = comp->llc_id;
+ new->llc_flags = comp->llc_flags;
+
+ list_add_tail(&new->llc_list, &new_layout->llot_comp_list);
+ new_layout->llot_cur_comp = new;
+ }
+ new_layout->llot_is_composite = true;
+
+ *dst_layout = new_layout;
+ return 0;
+error:
+ llapi_layout_free(new_layout);