+
+/**
+ * Get the last initialized component
+ *
+ * \param[in] layout component layout list.
+ *
+ * \retval 0 found
+ * \retval -EINVAL not found
+ * \retval -EISDIR directory layout
+ */
+int llapi_layout_get_last_init_comp(struct llapi_layout *layout)
+{
+ struct llapi_layout_comp *comp = NULL, *head = NULL;
+
+ if (!layout->llot_is_composite)
+ return 0;
+
+ head = list_entry(layout->llot_comp_list.next, typeof(*comp), llc_list);
+ if (head == NULL)
+ return -EINVAL;
+ if (head->llc_id == 0 && !(head->llc_flags & LCME_FL_INIT))
+ /* a directory */
+ return -EISDIR;
+
+ /* traverse the components from the tail to find the last init one */
+ comp = list_entry(layout->llot_comp_list.prev, typeof(*comp), llc_list);
+ while (comp != head) {
+ if (comp->llc_flags & LCME_FL_INIT)
+ break;
+ comp = list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
+ }
+
+ layout->llot_cur_comp = comp;
+
+ return comp->llc_flags & LCME_FL_INIT ? 0 : -EINVAL;
+}
+
+/**
+ * Interit stripe info from the file's component to the mirror
+ *
+ * \param[in] layout file component layout list.
+ * \param[in] layout mirro component layout list.
+ *
+ * \retval 0 on success
+ * \retval -EINVAL on error
+ */
+int llapi_layout_mirror_inherit(struct llapi_layout *f_layout,
+ struct llapi_layout *m_layout)
+{
+ struct llapi_layout_comp *m_comp = NULL;
+ struct llapi_layout_comp *f_comp = NULL;
+ int rc = 0;
+
+ f_comp = __llapi_layout_cur_comp(f_layout);
+ if (f_comp == NULL)
+ return -EINVAL;
+ m_comp = __llapi_layout_cur_comp(m_layout);
+ if (m_comp == NULL)
+ return -EINVAL;
+
+ m_comp->llc_stripe_size = f_comp->llc_stripe_size;
+ m_comp->llc_stripe_count = f_comp->llc_stripe_count;
+
+ return rc;
+}
+
+/**
+ * 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;
+ } else if (flags & LCME_FL_NOSYNC) {
+ /* if not specified mirrors, do not resync "nosync"
+ * mirrors */
+ 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) */
+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;
+ }
+ if (!mirror_id)
+ return -ENOENT;
+
+ return mirror_id;
+}
+
+int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
+ struct llapi_resync_comp *comp_array,
+ int comp_size, uint64_t start, uint64_t end)
+{
+ size_t page_size = sysconf(_SC_PAGESIZE);
+ const size_t buflen = 4 << 20; /* 4M */
+ void *buf;
+ uint64_t pos = start;
+ uint64_t data_off = pos, data_end = pos;
+ uint32_t src = 0;
+ int i;
+ int rc;
+ int rc2 = 0;
+
+ rc = posix_memalign(&buf, page_size, buflen);
+ if (rc)
+ return -rc;
+
+ while (pos < end) {
+ uint64_t mirror_end;
+ ssize_t bytes_read;
+ size_t to_read;
+ size_t to_write;
+
+ if (pos >= data_end) {
+ off_t tmp_off;
+ size_t data_size;
+
+ if (pos >= mirror_end || !src) {
+ rc = llapi_mirror_find(layout, pos, end,
+ &mirror_end);
+ if (rc < 0)
+ return rc;
+ src = rc;
+ /* restrict mirror end by resync end */
+ mirror_end = MIN(end, mirror_end);
+ }
+
+ tmp_off = llapi_mirror_data_seek(fd, src, pos,
+ &data_size);
+ if (tmp_off < 0) {
+ /* switch to full copy */
+ to_read = mirror_end - pos;
+ goto do_read;
+ }
+ data_off = tmp_off;
+ data_end = data_off + data_size;
+
+ data_off = MIN(data_off, mirror_end);
+ data_end = MIN(data_end, mirror_end);
+
+ /* align by page, if there is data block to copy */
+ if (data_size)
+ data_off &= ~(page_size - 1);
+ }
+
+ if (pos < data_off) {
+ for (i = 0; i < comp_size; i++) {
+ uint64_t cur_pos;
+ size_t to_punch;
+ uint32_t mid = comp_array[i].lrc_mirror_id;
+
+ /* skip non-overlapped component */
+ if (pos >= comp_array[i].lrc_end ||
+ data_off <= comp_array[i].lrc_start)
+ continue;
+
+ if (pos < comp_array[i].lrc_start)
+ cur_pos = comp_array[i].lrc_start;
+ else
+ cur_pos = pos;
+
+ if (data_off > comp_array[i].lrc_end)
+ to_punch = comp_array[i].lrc_end -
+ cur_pos;
+ else
+ to_punch = data_off - cur_pos;
+
+ if (comp_array[i].lrc_end == OBD_OBJECT_EOF) {
+ /* the last component can be truncated
+ * safely
+ */
+ rc = llapi_mirror_truncate(fd, mid,
+ cur_pos);
+ /* hole at the end of file, so just
+ * truncate up to set size.
+ */
+ if (!rc && data_off == data_end)
+ rc = llapi_mirror_truncate(fd,
+ mid, data_end);
+ } else {
+ rc = llapi_mirror_punch(fd,
+ comp_array[i].lrc_mirror_id,
+ cur_pos, to_punch);
+ }
+ /* if failed then read failed hole range */
+ if (rc < 0) {
+ rc = 0;
+ pos = cur_pos;
+ if (pos + to_punch == data_off)
+ to_read = data_end - pos;
+ else
+ to_read = to_punch;
+ goto do_read;
+ }
+ }
+ pos = data_off;
+ }
+ if (pos == mirror_end)
+ continue;
+ to_read = data_end - pos;
+do_read:
+ if (!to_read)
+ break;
+
+ assert(data_end <= mirror_end);
+
+ to_read = MIN(buflen, to_read);
+ to_read = ((to_read - 1) | (page_size - 1)) + 1;
+ bytes_read = llapi_mirror_read(fd, src, buf, to_read, pos);
+ if (bytes_read == 0) {
+ /* end of file */
+ break;
+ }
+ if (bytes_read < 0) {
+ rc = bytes_read;
+ break;
+ }
+
+ /* round up to page align to make direct IO happy. */
+ to_write = ((bytes_read - 1) | (page_size - 1)) + 1;
+
+ for (i = 0; i < comp_size; i++) {
+ ssize_t written;
+ off_t pos2 = pos;
+ size_t to_write2 = to_write;
+
+ /* skip non-overlapped component */
+ if (pos >= comp_array[i].lrc_end ||
+ pos + to_write <= comp_array[i].lrc_start)
+ continue;
+
+ if (pos < comp_array[i].lrc_start)
+ pos2 = comp_array[i].lrc_start;
+
+ to_write2 -= pos2 - pos;
+
+ if ((pos + to_write) > comp_array[i].lrc_end)
+ to_write2 -= pos + to_write -
+ comp_array[i].lrc_end;
+
+ written = llapi_mirror_write(fd,
+ comp_array[i].lrc_mirror_id,
+ buf + pos2 - pos,
+ to_write2, pos2);
+ if (written < 0) {
+ /**
+ * this component is not written successfully,
+ * mark it using its lrc_synced, it is supposed
+ * to be false before getting here.
+ *
+ * And before this function returns, all
+ * elements of comp_array will reverse their
+ * lrc_synced flag to reflect their true
+ * meanings.
+ */
+ comp_array[i].lrc_synced = true;
+ llapi_error(LLAPI_MSG_ERROR, written,
+ "component %u not synced",
+ comp_array[i].lrc_id);
+ if (rc2 == 0)
+ rc2 = (int)written;
+ continue;
+ }
+ assert(written == to_write2);
+ }
+ pos += bytes_read;
+ }
+
+ free(buf);
+
+ if (rc < 0) {
+ /* fatal error happens */
+ for (i = 0; i < comp_size; i++)
+ comp_array[i].lrc_synced = false;
+ return rc;
+ }
+
+ /**
+ * no fatal error happens, each lrc_synced tells whether the component
+ * has been resync successfully (note: we'd reverse the value to
+ * reflect its true meaning.
+ */
+ for (i = 0; i < comp_size; i++) {
+ comp_array[i].lrc_synced = !comp_array[i].lrc_synced;
+ if (comp_array[i].lrc_synced && pos & (page_size - 1)) {
+ rc = llapi_mirror_truncate(fd,
+ comp_array[i].lrc_mirror_id, pos);
+ if (rc < 0)
+ comp_array[i].lrc_synced = false;
+ }
+ }
+
+ /**
+ * returns the first error code for partially successful resync if
+ * possible.
+ */
+ return rc2;
+}
+
+enum llapi_layout_comp_sanity_error {
+ LSE_OK,
+ LSE_INCOMPLETE_MIRROR,
+ LSE_ADJACENT_EXTENSION,
+ LSE_INIT_EXTENSION,
+ LSE_FLAGS,
+ LSE_DOM_EXTENSION,
+ LSE_DOM_EXTENSION_FOLLOWING,
+ LSE_DOM_FIRST,
+ LSE_SET_COMP_START,
+ LSE_NOT_ZERO_LENGTH_EXTENDABLE,
+ LSE_END_NOT_GREATER,
+ LSE_ZERO_LENGTH_NORMAL,
+ LSE_NOT_ADJACENT_PREV,
+ LSE_START_GT_END,
+ LSE_ALIGN_END,
+ LSE_ALIGN_EXT,
+ LSE_UNKNOWN_OST,
+ LSE_LAST,
+};
+
+const char *const llapi_layout_strerror[] =
+{
+ [LSE_OK] = "",
+ [LSE_INCOMPLETE_MIRROR] =
+ "Incomplete mirror - must go to EOF",
+ [LSE_ADJACENT_EXTENSION] =
+ "No adjacent extension space components",
+ [LSE_INIT_EXTENSION] =
+ "Cannot apply extension flag to init components",
+ [LSE_FLAGS] =
+ "Wrong flags",
+ [LSE_DOM_EXTENSION] =
+ "DoM components can't be extension space",
+ [LSE_DOM_EXTENSION_FOLLOWING] =
+ "DoM components cannot be followed by extension space",
+ [LSE_DOM_FIRST] =
+ "DoM component should be the first one in a file/mirror",
+ [LSE_SET_COMP_START] =
+ "Must set previous component extent before adding next",
+ [LSE_NOT_ZERO_LENGTH_EXTENDABLE] =
+ "Extendable component must start out zero-length",
+ [LSE_END_NOT_GREATER] =
+ "Component end is before end of previous component",
+ [LSE_ZERO_LENGTH_NORMAL] =
+ "Zero length components must be followed by extension",
+ [LSE_NOT_ADJACENT_PREV] =
+ "Components not adjacent (end != next->start",
+ [LSE_START_GT_END] =
+ "Component start is > end",
+ [LSE_ALIGN_END] =
+ "The component end must be aligned by the stripe size",
+ [LSE_ALIGN_EXT] =
+ "The extension size must be aligned by the stripe size",
+ [LSE_UNKNOWN_OST] =
+ "An unknown OST idx is specified",
+};
+
+struct llapi_layout_sanity_args {
+ char lsa_fsname[MAX_OBD_NAME + 1];
+ bool lsa_incomplete;
+ bool lsa_flr;
+ bool lsa_ondisk;
+ int lsa_rc;
+};
+
+/* The component flags can be set by users at creation/modification time. */
+#define LCME_USER_COMP_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
+ LCME_FL_EXTENSION)
+
+/**
+ * When modified, adjust llapi_stripe_param_verify() if needed as well.
+ */
+static int llapi_layout_sanity_cb(struct llapi_layout *layout,
+ void *arg)
+{
+ struct llapi_layout_comp *comp, *next, *prev;
+ struct llapi_layout_sanity_args *args = arg;
+ bool first_comp = false;
+
+ comp = __llapi_layout_cur_comp(layout);
+ if (comp == NULL) {
+ args->lsa_rc = -1;
+ goto out_err;
+ }
+
+ if (comp->llc_list.prev != &layout->llot_comp_list)
+ prev = list_entry(comp->llc_list.prev, typeof(*prev),
+ llc_list);
+ else
+ prev = NULL;
+
+ if (comp->llc_list.next != &layout->llot_comp_list)
+ next = list_entry(comp->llc_list.next, typeof(*next),
+ llc_list);
+ else
+ next = NULL;
+
+ /* Start of zero implies a new mirror */
+ if (comp->llc_extent.e_start == 0) {
+ first_comp = true;
+ /* Most checks apply only within one mirror, this is an
+ * exception. */
+ if (prev && prev->llc_extent.e_end != LUSTRE_EOF) {
+ args->lsa_rc = LSE_INCOMPLETE_MIRROR;
+ goto out_err;
+ }
+
+ prev = NULL;
+ }
+
+ if (next && next->llc_extent.e_start == 0)
+ next = NULL;
+
+ /* Flag sanity checks */
+ /* No adjacent extension components */
+ if ((comp->llc_flags & LCME_FL_EXTENSION) && next &&
+ (next->llc_flags & LCME_FL_EXTENSION)) {
+ args->lsa_rc = LSE_ADJACENT_EXTENSION;
+ goto out_err;
+ }
+
+ /* Extension flag cannot be applied to init components and the first
+ * component of each mirror is automatically init */
+ if ((comp->llc_flags & LCME_FL_EXTENSION) &&
+ (comp->llc_flags & LCME_FL_INIT || first_comp)) {
+ args->lsa_rc = LSE_INIT_EXTENSION;
+ goto out_err;
+ }
+
+ if (comp->llc_ondisk) {
+ if (comp->llc_flags & LCME_FL_NEG)
+ args->lsa_rc = LSE_FLAGS;
+ } else if (!args->lsa_incomplete) {
+ if (args->lsa_flr) {
+ if (comp->llc_flags & ~LCME_USER_COMP_FLAGS)
+ args->lsa_rc = LSE_FLAGS;
+ } else {
+ if (comp->llc_flags &
+ ~(LCME_FL_EXTENSION | LCME_FL_PREF_RW))
+ args->lsa_rc = LSE_FLAGS;
+ }
+ }
+ if (args->lsa_rc)
+ goto out_err;
+
+ /* DoM sanity checks */
+ if (comp->llc_pattern == LLAPI_LAYOUT_MDT ||
+ comp->llc_pattern == LOV_PATTERN_MDT) {
+ /* DoM components can't be extension components */
+ if (comp->llc_flags & LCME_FL_EXTENSION) {
+ args->lsa_rc = LSE_DOM_EXTENSION;
+ goto out_err;
+ }
+ /* DoM components cannot be followed by an extension comp */
+ if (next && (next->llc_flags & LCME_FL_EXTENSION)) {
+ args->lsa_rc = LSE_DOM_EXTENSION_FOLLOWING;
+ goto out_err;
+ }
+
+ /* DoM should be the first component in a mirror */
+ if (!first_comp) {
+ args->lsa_rc = LSE_DOM_FIRST;
+ errno = EINVAL;
+ goto out_err;
+ }
+ }
+
+ /* Extent sanity checks */
+ /* Must set previous component extent before adding another */
+ if (prev && prev->llc_extent.e_start == 0 &&
+ prev->llc_extent.e_end == 0) {
+ args->lsa_rc = LSE_SET_COMP_START;
+ goto out_err;
+ }
+
+ if (!args->lsa_incomplete) {
+ /* Components followed by extension space (extendable
+ * components) must be zero length before initialization.
+ * (Except for first comp, which will be initialized on
+ * creation). */
+ if (next && (next->llc_flags & LCME_FL_EXTENSION) &&
+ !first_comp && !(comp->llc_flags & LCME_FL_INIT) &&
+ comp->llc_extent.e_start != comp->llc_extent.e_end) {
+ args->lsa_rc = LSE_NOT_ZERO_LENGTH_EXTENDABLE;
+ goto out_err;
+ }
+
+ /* End must come after end of previous comp */
+ if (prev && comp->llc_extent.e_end < prev->llc_extent.e_end) {
+ args->lsa_rc = LSE_END_NOT_GREATER;
+ goto out_err;
+ }
+
+ /* Components not followed by ext space must have length > 0. */
+ if (comp->llc_extent.e_start == comp->llc_extent.e_end &&
+ (next == NULL || !(next->llc_flags & LCME_FL_EXTENSION))) {
+ args->lsa_rc = LSE_ZERO_LENGTH_NORMAL;
+ goto out_err;
+ }
+
+ /* The component end must be aligned by the stripe size */
+ if ((comp->llc_flags & LCME_FL_EXTENSION) &&
+ (prev->llc_stripe_size != LLAPI_LAYOUT_DEFAULT)) {
+ if (comp->llc_extent.e_end != LUSTRE_EOF &&
+ comp->llc_extent.e_end % prev->llc_stripe_size) {
+ args->lsa_rc = LSE_ALIGN_END;
+ goto out_err;
+ }
+ if ((comp->llc_stripe_size * SEL_UNIT_SIZE) %
+ prev->llc_stripe_size) {
+ args->lsa_rc = LSE_ALIGN_EXT;
+ goto out_err;
+ }
+ } else if (!(comp->llc_flags & LCME_FL_EXTENSION) &&
+ (comp->llc_stripe_size != LLAPI_LAYOUT_DEFAULT)) {
+ if (comp->llc_extent.e_end != LUSTRE_EOF &&
+ comp->llc_extent.e_end !=
+ comp->llc_extent.e_start &&
+ comp->llc_extent.e_end % comp->llc_stripe_size) {
+ args->lsa_rc = LSE_ALIGN_END;
+ goto out_err;
+ }
+ }
+ }
+
+ /* Components must have start == prev->end */
+ if (prev && comp->llc_extent.e_start != 0 &&
+ comp->llc_extent.e_start != prev->llc_extent.e_end) {
+ args->lsa_rc = LSE_NOT_ADJACENT_PREV;
+ goto out_err;
+ }
+
+ /* Components must have start <= end */
+ if (comp->llc_extent.e_start > comp->llc_extent.e_end) {
+ args->lsa_rc = LSE_START_GT_END;
+ goto out_err;
+ }
+
+ if (args->lsa_fsname[0] != '\0') {
+ int i, rc = 0;
+
+ if (comp->llc_pattern & LLAPI_LAYOUT_SPECIFIC) {
+ assert(comp->llc_stripe_count <=
+ comp->llc_objects_count);
+
+ for (i = 0; i < comp->llc_stripe_count && rc == 0; i++){
+ if (comp->llc_objects[i].l_ost_idx ==
+ LLAPI_LAYOUT_IDX_MAX) {
+ args->lsa_rc = -1;
+ goto out_err;
+ }
+ rc = llapi_layout_search_ost(
+ comp->llc_objects[i].l_ost_idx,
+ comp->llc_pool_name, args->lsa_fsname);
+ }
+ } else if (comp->llc_stripe_offset != LLAPI_LAYOUT_DEFAULT) {
+ rc = llapi_layout_search_ost(
+ comp->llc_stripe_offset,
+ comp->llc_pool_name, args->lsa_fsname);
+ }
+ if (rc) {
+ args->lsa_rc = LSE_UNKNOWN_OST;
+ goto out_err;
+ }
+ }
+
+ return LLAPI_LAYOUT_ITER_CONT;
+
+out_err:
+ errno = errno ? errno : EINVAL;
+ return LLAPI_LAYOUT_ITER_STOP;
+}
+
+/* Print explanation of layout error */
+void llapi_layout_sanity_perror(int error)
+{
+ if (error >= LSE_LAST || error < 0) {
+ fprintf(stdout, "Invalid layout, unrecognized error: %d\n",
+ error);
+ } else {
+ fprintf(stdout, "Invalid layout: %s\n",
+ llapi_layout_strerror[error]);
+ }
+}
+
+/* Walk a layout and enforce sanity checks that apply to > 1 component
+ *
+ * The core idea here is that of sanity checking individual tokens vs semantic
+ * checking.
+ * We cannot check everything at the individual component level ('token'),
+ * instead we must check whether or not the full layout has a valid meaning.
+ *
+ * An example of a component level check is "is stripe size valid?". That is
+ * handled when setting stripe size.
+ *
+ * An example of a layout level check is "are the extents of these components
+ * valid when adjacent to one another", or "can we set these flags on adjacent
+ * components"?
+ *
+ * \param[in] layout component layout list.
+ * \param[in] fname file the layout to be checked for
+ * \param[in] incomplete if layout is complete or not - some checks can
+ * only be done on complete layouts.
+ * \param[in] flr set when this is called from FLR mirror create
+ *
+ * \retval 0, success, positive: various errors, see
+ * llapi_layout_sanity_perror, -1, failure
+ */
+int llapi_layout_sanity(struct llapi_layout *layout,
+ const char *fname,
+ bool incomplete,
+ bool flr)
+{
+ struct llapi_layout_sanity_args args = { { 0 } };
+ struct llapi_layout_comp *curr;
+ int rc = 0;
+
+ if (!layout)
+ return 0;
+
+ curr = layout->llot_cur_comp;
+ if (!curr)
+ return 0;
+
+ /* Make sure we are on a Lustre file system */
+ if (fname) {
+ rc = llapi_search_fsname(fname, args.lsa_fsname);
+ if (rc) {
+ llapi_error(LLAPI_MSG_ERROR, rc,
+ "'%s' is not on a Lustre filesystem",
+ fname);
+ return rc;
+ }
+ }
+
+ /* Set up args */
+ args.lsa_rc = 0;
+ args.lsa_flr = flr;
+ args.lsa_incomplete = incomplete;
+
+ /* When we modify an existing layout, this tells us if it's FLR */
+ if (mirror_id_of(curr->llc_id) > 0)
+ args.lsa_flr = true;
+
+ errno = 0;
+ rc = llapi_layout_comp_iterate(layout,
+ llapi_layout_sanity_cb,
+ &args);
+ if (errno == ENOENT)
+ errno = 0;
+
+ if (rc != LLAPI_LAYOUT_ITER_CONT)
+ rc = args.lsa_rc;
+
+ layout->llot_cur_comp = curr;
+
+ return rc;
+}
+
+int llapi_layout_dom_size(struct llapi_layout *layout, uint64_t *size)
+{
+ uint64_t pattern, start;
+ int rc;
+
+ if (!layout || !llapi_layout_is_composite(layout)) {
+ *size = 0;
+ return 0;
+ }
+
+ rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
+ if (rc)
+ return -errno;
+
+ rc = llapi_layout_pattern_get(layout, &pattern);
+ if (rc)
+ return -errno;
+
+ if (pattern != LOV_PATTERN_MDT && pattern != LLAPI_LAYOUT_MDT) {
+ *size = 0;
+ return 0;
+ }
+
+ rc = llapi_layout_comp_extent_get(layout, &start, size);
+ if (rc)
+ return -errno;
+ if (start)
+ return -ERANGE;
+ return 0;
+}
+
+int lov_comp_md_size(struct lov_comp_md_v1 *lcm)
+{
+ if (lcm->lcm_magic == LOV_MAGIC_V1 || lcm->lcm_magic == LOV_MAGIC_V3) {
+ struct lov_user_md *lum = (void *)lcm;
+
+ return lov_user_md_size(lum->lmm_stripe_count, lum->lmm_magic);
+ }
+
+ if (lcm->lcm_magic == LOV_MAGIC_FOREIGN) {
+ struct lov_foreign_md *lfm = (void *)lcm;
+
+ return lfm->lfm_length;
+ }
+
+ if (lcm->lcm_magic != LOV_MAGIC_COMP_V1)
+ return -EOPNOTSUPP;
+
+ return lcm->lcm_size;
+}
+
+int llapi_get_lum_file_fd(int dir_fd, const char *fname, __u64 *valid,
+ lstatx_t *statx, struct lov_user_md *lum,
+ size_t lumsize)
+{
+ struct lov_user_mds_data *lmd;
+ char buf[65536 + offsetof(typeof(*lmd), lmd_lmm)];
+ int parent_fd = -1;
+ int rc;
+
+ if (lum && lumsize < sizeof(*lum))
+ return -EINVAL;
+
+ /* If a file name is provided, it is relative to the parent directory */
+ if (fname) {
+ parent_fd = dir_fd;
+ dir_fd = -1;
+ }
+
+ lmd = (struct lov_user_mds_data *)buf;
+ rc = get_lmd_info_fd(fname, parent_fd, dir_fd, buf, sizeof(buf),
+ GET_LMD_INFO);
+ if (rc)
+ return rc;
+
+ if (valid)
+ *valid = lmd->lmd_flags;
+
+ if (statx)
+ memcpy(statx, &lmd->lmd_stx, sizeof(*statx));
+
+ if (lum) {
+ if (lmd->lmd_lmmsize > lumsize)
+ return -EOVERFLOW;
+ memcpy(lum, &lmd->lmd_lmm, lmd->lmd_lmmsize);
+ }
+
+ return 0;
+}
+
+int llapi_get_lum_dir_fd(int dir_fd, __u64 *valid, lstatx_t *statx,
+ struct lov_user_md *lum, size_t lumsize)
+{
+ return llapi_get_lum_file_fd(dir_fd, NULL, valid, statx, lum, lumsize);
+}
+
+int llapi_get_lum_file(const char *path, __u64 *valid, lstatx_t *statx,
+ struct lov_user_md *lum, size_t lumsize)
+{
+ char parent[PATH_MAX];
+ const char *fname;
+ char *tmp;
+ int offset;
+ int dir_fd;
+ int rc;
+
+ tmp = strrchr(path, '/');
+ if (!tmp) {
+ strncpy(parent, ".", sizeof(parent) - 1);
+ offset = -1;
+ } else {
+ strncpy(parent, path, tmp - path);
+ offset = tmp - path - 1;
+ parent[tmp - path] = 0;
+ }
+
+ fname = path;
+ if (offset >= 0)
+ fname += offset + 2;
+
+ dir_fd = open(parent, O_RDONLY);
+ if (dir_fd < 0) {
+ rc = -errno;
+ llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
+ return rc;
+ }
+
+ rc = llapi_get_lum_file_fd(dir_fd, fname, valid, statx, lum, lumsize);
+ close(dir_fd);
+ return rc;
+}
+
+int llapi_get_lum_dir(const char *path, __u64 *valid, lstatx_t *statx,
+ struct lov_user_md *lum, size_t lumsize)
+{
+ int dir_fd;
+ int rc;
+
+ dir_fd = open(path, O_RDONLY);
+ if (dir_fd < 0) {
+ rc = -errno;
+ llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
+ return rc;
+ }
+
+ rc = llapi_get_lum_dir_fd(dir_fd, valid, statx, lum, lumsize);
+ close(dir_fd);
+ return rc;
+}