+/**
+ * Parse a string containing an OST index list into an array of integers.
+ *
+ * The input string contains a comma delimited list of individual
+ * indices and ranges, for example "1,2-4,7". Add the indices into the
+ * \a osts array and remove duplicates.
+ *
+ * \param[out] osts array to store indices in
+ * \param[in] size size of \a osts array
+ * \param[in] offset starting index in \a osts
+ * \param[in] arg string containing OST index list
+ *
+ * \retval positive number of indices in \a osts
+ * \retval -EINVAL unable to parse \a arg
+ */
+static int parse_targets(__u32 *osts, int size, int offset, char *arg)
+{
+ int rc;
+ int nr = offset;
+ int slots = size - offset;
+ char *ptr = NULL;
+ bool end_of_loop;
+
+ if (arg == NULL)
+ return -EINVAL;
+
+ end_of_loop = false;
+ while (!end_of_loop) {
+ int start_index;
+ int end_index;
+ int i;
+ char *endptr = NULL;
+
+ rc = -EINVAL;
+
+ ptr = strchrnul(arg, ',');
+
+ end_of_loop = *ptr == '\0';
+ *ptr = '\0';
+
+ start_index = strtol(arg, &endptr, 0);
+ if (endptr == arg) /* no data at all */
+ break;
+ if (*endptr != '-' && *endptr != '\0') /* has invalid data */
+ break;
+ if (start_index < 0)
+ break;
+
+ end_index = start_index;
+ if (*endptr == '-') {
+ end_index = strtol(endptr + 1, &endptr, 0);
+ if (*endptr != '\0')
+ break;
+ if (end_index < start_index)
+ break;
+ }
+
+ for (i = start_index; i <= end_index && slots > 0; i++) {
+ int j;
+
+ /* remove duplicate */
+ for (j = 0; j < offset; j++) {
+ if (osts[j] == i)
+ break;
+ }
+ if (j == offset) { /* no duplicate */
+ osts[nr++] = i;
+ --slots;
+ }
+ }
+ if (slots == 0 && i < end_index)
+ break;
+
+ *ptr = ',';
+ arg = ++ptr;
+ offset = nr;
+ rc = 0;
+ }
+ if (!end_of_loop && ptr != NULL)
+ *ptr = ',';
+
+ return rc < 0 ? rc : nr;
+}
+