+
+/**
+ * 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);
+ return -1;
+}
+
+/**
+ * Find all stale components.
+ *
+ * \param[in] layout component layout list.
+ * \param[out] comp array of stale component info.
+ * \param[in] comp_size array size of @comp.
+ * \param[in] mirror_ids array of mirror id that only components
+ * belonging to these mirror will be collected.
+ * \param[in] ids_nr number of mirror ids array.
+ *
+ * \retval number of component info collected on sucess or
+ * an error code on failure.
+ */
+int llapi_mirror_find_stale(struct llapi_layout *layout,
+ struct llapi_resync_comp *comp, size_t comp_size,
+ __u16 *mirror_ids, int ids_nr)
+{
+ int idx = 0;
+ int rc;
+
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+ if (rc < 0)
+ goto error;
+
+ while (rc == 0) {
+ uint32_t id;
+ uint32_t mirror_id;
+ uint32_t flags;
+ uint64_t start, end;
+
+ rc = llapi_layout_comp_flags_get(layout, &flags);
+ if (rc < 0)
+ goto error;
+
+ if (!(flags & LCME_FL_STALE))
+ goto next;
+
+ rc = llapi_layout_mirror_id_get(layout, &mirror_id);
+ if (rc < 0)
+ goto error;
+
+ /* the caller only wants stale components from specific
+ * mirrors */
+ if (ids_nr > 0) {
+ int j;
+
+ for (j = 0; j < ids_nr; j++) {
+ if (mirror_ids[j] == mirror_id)
+ break;
+ }
+
+ /* not in the specified mirror */
+ if (j == ids_nr)
+ goto next;
+ }
+
+ rc = llapi_layout_comp_id_get(layout, &id);
+ if (rc < 0)
+ goto error;
+
+ rc = llapi_layout_comp_extent_get(layout, &start, &end);
+ if (rc < 0)
+ goto error;
+
+ /* pack this component into @comp array */
+ comp[idx].lrc_id = id;
+ comp[idx].lrc_mirror_id = mirror_id;
+ comp[idx].lrc_start = start;
+ comp[idx].lrc_end = end;
+ idx++;
+
+ if (idx >= comp_size) {
+ rc = -EINVAL;
+ goto error;
+ }
+
+ next:
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
+ if (rc < 0) {
+ rc = -EINVAL;
+ goto error;
+ }
+ }
+error:
+ return rc < 0 ? rc : idx;
+}
+
+/* locate @layout to a valid component covering file [file_start, file_end) */
+static uint32_t llapi_mirror_find(struct llapi_layout *layout,
+ uint64_t file_start, uint64_t file_end,
+ uint64_t *endp)
+{
+ uint32_t mirror_id = 0;
+ int rc;
+
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+ if (rc < 0)
+ return rc;
+
+ *endp = 0;
+ while (rc == 0) {
+ uint64_t start, end;
+ uint32_t flags, id, rid;
+
+ rc = llapi_layout_comp_flags_get(layout, &flags);
+ if (rc < 0)
+ return rc;
+
+ if (flags & LCME_FL_STALE)
+ goto next;
+
+ rc = llapi_layout_mirror_id_get(layout, &rid);
+ if (rc < 0)
+ return rc;
+
+ rc = llapi_layout_comp_id_get(layout, &id);
+ if (rc < 0)
+ return rc;
+
+ rc = llapi_layout_comp_extent_get(layout, &start, &end);
+ if (rc < 0)
+ return rc;
+
+ if (file_start >= start && file_start < end) {
+ if (!mirror_id)
+ mirror_id = rid;
+ else if (mirror_id != rid || *endp != start)
+ break;
+
+ file_start = *endp = end;
+ if (end >= file_end)
+ break;
+ }
+
+ next:
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
+ if (rc < 0)
+ return rc;
+ }
+
+ return mirror_id;
+}
+
+ssize_t llapi_mirror_resync_one(int fd, struct llapi_layout *layout,
+ uint32_t dst, uint64_t start, uint64_t end)
+{
+ uint64_t mirror_end = 0;
+ ssize_t result = 0;
+ size_t count;
+
+ if (end == OBD_OBJECT_EOF)
+ count = OBD_OBJECT_EOF;
+ else
+ count = end - start;
+
+ while (count > 0) {
+ uint32_t src;
+ size_t to_copy;
+ ssize_t copied;
+
+ src = llapi_mirror_find(layout, start, end, &mirror_end);
+ if (src == 0)
+ return -ENOENT;
+
+ if (mirror_end == OBD_OBJECT_EOF)
+ to_copy = count;
+ else
+ to_copy = MIN(count, mirror_end - start);
+
+ copied = llapi_mirror_copy(fd, src, dst, start, to_copy);
+ if (copied < 0)
+ return copied;
+
+ result += copied;
+ if (copied < to_copy) /* end of file */
+ break;
+
+ if (count != OBD_OBJECT_EOF)
+ count -= copied;
+ start += copied;
+ }
+
+ return result;
+}