void usage(char *prog)
{
- printf("Usage: %s [-d lustre_dir] [-p pool_name] [-o num_osts]\n",
- prog);
+ printf("Usage: %s [-d lustre_dir] [-p pool_name] [-o num_osts] "
+ "[-s $n,$m,..]\n", prog);
exit(0);
}
layout = llapi_layout_alloc();
ASSERTF(layout != NULL, "errno = %d", errno);
- /* Only setting OST index for stripe 0 is supported for now. */
- errno = 0;
- rc = llapi_layout_ost_index_set(layout, 1, 1);
- ASSERTF(rc == -1 && errno == EOPNOTSUPP, "rc = %d, errno = %d",
- rc, errno);
-
/* invalid OST index */
errno = 0;
rc = llapi_layout_ost_index_set(layout, 0, LLAPI_LAYOUT_INVALID);
llapi_layout_free(layout);
}
+#define T29FILE "f29"
+#define T29_DESC "set ost index to non-zero stripe number"
+void test29(void)
+{
+ int rc, fd, i;
+ uint64_t ost0, ost1, nost;
+ struct llapi_layout *layout;
+ char path[PATH_MAX];
+
+ if (num_osts < 2)
+ return;
+
+ layout = llapi_layout_alloc();
+ ASSERTF(layout != NULL, "errno %d", errno);
+
+ snprintf(path, sizeof(path), "%s/%s", lustre_dir, T29FILE);
+
+ rc = unlink(path);
+ ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
+
+ /* set ost index to LLAPI_LAYOUT_IDX_MAX should fail */
+ rc = llapi_layout_ost_index_set(layout, 1, LLAPI_LAYOUT_IDX_MAX);
+ ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d\n",
+ rc, errno);
+
+ /* specify ost index partially */
+ rc = llapi_layout_ost_index_set(layout, 1, 0);
+ ASSERTF(rc == 0, "errno = %d", errno);
+
+ /* create a partially specified layout will fail */
+ fd = llapi_layout_file_create(path, 0, 0660, layout);
+ ASSERTF(fd == -1 && errno == EINVAL, "path = %s, fd = %d, errno = %d",
+ path, fd, errno);
+
+ rc = unlink(path);
+ ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
+
+ /* specify all stripes */
+ rc = llapi_layout_ost_index_set(layout, 0, 1);
+ ASSERTF(rc == 0, "errno = %d", errno);
+
+ /* create */
+ fd = llapi_layout_file_create(path, 0, 0660, layout);
+ ASSERTF(fd >= 0, "path = %s, fd = %d, errno = %d", path, fd, errno);
+
+ rc = close(fd);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ llapi_layout_free(layout);
+
+ /* get layout from file */
+ layout = llapi_layout_get_by_path(path, 0);
+ ASSERTF(layout != NULL, "errno = %d", errno);
+
+ rc = llapi_layout_ost_index_get(layout, 0, &ost0);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ rc = llapi_layout_ost_index_get(layout, 1, &ost1);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ ASSERTF(ost0 == 1, "%"PRIu64" != %d", ost0, 1);
+ ASSERTF(ost1 == 0, "%"PRIu64" != %d", ost1, 0);
+ llapi_layout_free(layout);
+
+ /* specify more ost indexes to test realloc */
+ nost = 0;
+ layout = llapi_layout_alloc();
+ ASSERTF(layout != NULL, "errno %d", errno);
+ for (i = 0; i < LOV_MAX_STRIPE_COUNT; i++) {
+ rc = llapi_layout_ost_index_set(layout, i, nost);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ rc = llapi_layout_ost_index_get(layout, i, &ost0);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ nost++;
+ if (nost == num_osts)
+ nost = 0;
+ }
+
+ nost = 0;
+ for (i = 0; i < LOV_MAX_STRIPE_COUNT; i++) {
+ rc = llapi_layout_ost_index_get(layout, i, &ost0);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ ASSERTF(ost0 == nost, "ost=%"PRIu64" nost=%"PRIu64"",
+ ost0, nost);
+ nost++;
+ if (nost == num_osts)
+ nost = 0;
+ }
+ llapi_layout_free(layout);
+
+ nost = 0;
+ layout = llapi_layout_alloc();
+ ASSERTF(layout != NULL, "errno %d", errno);
+ for (i = LOV_MAX_STRIPE_COUNT-1; i >= 0; i--) {
+ rc = llapi_layout_ost_index_set(layout, i, nost);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ rc = llapi_layout_ost_index_get(layout, i, &ost0);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ nost++;
+ if (nost == num_osts)
+ nost = 0;
+ }
+
+ nost = 0;
+ for (i = LOV_MAX_STRIPE_COUNT-1; i <= 0; i--) {
+ rc = llapi_layout_ost_index_get(layout, i, &ost0);
+ ASSERTF(rc == 0, "errno = %d", errno);
+ ASSERTF(ost0 == nost, "ost=%"PRIu64", nost=%"PRIu64"",
+ ost0, nost);
+ nost++;
+ if (nost == num_osts)
+ nost = 0;
+ }
+ llapi_layout_free(layout);
+}
+
#define TEST_DESC_LEN 50
struct test_tbl_entry {
void (*tte_fn)(void);
{ &test26, T26_DESC, false },
{ &test27, T27_DESC, false },
{ &test28, T28_DESC, false },
+ { &test29, T29_DESC, false },
};
#define NUM_TESTS (sizeof(test_tbl) / sizeof(struct test_tbl_entry))
return rc;
}
+/* 'str_tests' are the tests to be skipped, such as "1,3,4,.." */
+static void set_tests_skipped(char *str_tests)
+{
+ char *ptr = str_tests;
+ int tstno;
+
+ if (ptr == NULL || strlen(ptr) == 0)
+ return;
+
+ while (*ptr != '\0') {
+ tstno = strtoul(ptr, &ptr, 0);
+ if (tstno >= 0 && tstno < NUM_TESTS)
+ test_tbl[tstno].tte_skip = true;
+ if (*ptr == ',')
+ ptr++;
+ else
+ break;
+ }
+}
+
static void process_args(int argc, char *argv[])
{
int c;
- while ((c = getopt(argc, argv, "d:p:o:")) != -1) {
+ while ((c = getopt(argc, argv, "d:p:o:s:")) != -1) {
switch (c) {
case 'd':
lustre_dir = optarg;
case 'o':
num_osts = atoi(optarg);
break;
+ case 's':
+ set_tests_skipped(optarg);
+ break;
case '?':
fprintf(stderr, "Unknown option '%c'\n", optopt);
usage(argv[0]);
uint64_t llot_stripe_size;
uint64_t llot_stripe_count;
uint64_t llot_stripe_offset;
- /** Indicates if llot_objects array has been initialized. */
- bool llot_objects_are_valid;
/* Add 1 so user always gets back a null terminated string. */
char llot_pool_name[LOV_MAXPOOLNAME + 1];
- struct lov_user_ost_data_v1 llot_objects[0];
+ /** Number of objects in llot_objects array if was initialized. */
+ uint32_t llot_objects_count;
+ struct lov_user_ost_data_v1 *llot_objects;
};
/**
}
/**
+ * (Re-)allocate llot_objects[] to \a num_stripes stripes.
+ *
+ * Copy over existing llot_objects[], if any, to the new llot_objects[].
+ *
+ * \param[in] layout existing layout to be modified
+ * \param[in] num_stripes number of stripes in new layout
+ *
+ * \retval 0 if the objects are re-allocated successfully
+ * \retval -1 on error with errno set
+ */
+static int __llapi_layout_objects_realloc(struct llapi_layout *layout,
+ unsigned int new_stripes)
+{
+ struct lov_user_ost_data_v1 *new_objects;
+ unsigned int i;
+
+ if (new_stripes > LOV_MAX_STRIPE_COUNT) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (new_stripes == 0 && layout->llot_objects_count == 0)
+ return 0;
+
+ if (new_stripes != 0 && new_stripes <= layout->llot_objects_count)
+ return 0;
+
+ new_objects = realloc(layout->llot_objects,
+ sizeof(*new_objects) * new_stripes);
+ if (new_objects == NULL && new_stripes != 0) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ for (i = layout->llot_objects_count; i < new_stripes; i++)
+ new_objects[i].l_ost_idx = LLAPI_LAYOUT_IDX_MAX;
+
+ if (new_stripes == 0)
+ layout->llot_objects = NULL;
+ else
+ layout->llot_objects = new_objects;
+ layout->llot_objects_count = new_stripes;
+
+ return 0;
+}
+
+/**
* Allocate storage for a llapi_layout with \a num_stripes stripes.
*
* \param[in] num_stripes number of stripes in new layout
*/
static struct llapi_layout *__llapi_layout_alloc(unsigned int num_stripes)
{
- struct llapi_layout *layout = NULL;
- size_t size = sizeof(*layout) +
- (num_stripes * sizeof(layout->llot_objects[0]));
+ struct llapi_layout *layout;
- if (num_stripes > LOV_MAX_STRIPE_COUNT)
+ if (num_stripes > LOV_MAX_STRIPE_COUNT) {
errno = EINVAL;
- else
- layout = calloc(1, size);
+ return NULL;
+ }
+
+ layout = calloc(1, sizeof(*layout));
+ if (layout == NULL) {
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ layout->llot_objects = NULL;
+ layout->llot_objects_count = 0;
+
+ if (__llapi_layout_objects_realloc(layout, num_stripes) < 0) {
+ free(layout);
+ layout = NULL;
+ }
return layout;
}
else
layout->llot_stripe_count = lum->lmm_stripe_count;
- /* Don't copy lmm_stripe_offset: it is always zero
- * when reading attributes. */
+ if (lum->lmm_stripe_offset == (typeof(lum->lmm_stripe_offset))-1)
+ layout->llot_stripe_offset = LLAPI_LAYOUT_DEFAULT;
+ else
+ layout->llot_stripe_offset = lum->lmm_stripe_offset;
if (lum->lmm_magic != LOV_USER_MAGIC_V1) {
const struct lov_user_md_v3 *lumv3;
lumv1 = (struct lov_user_md_v1 *)lum;
memcpy(layout->llot_objects, lumv1->lmm_objects, objects_sz);
}
- if (object_count > 0)
- layout->llot_objects_are_valid = true;
+
+ if (object_count != 0)
+ layout->llot_stripe_offset = layout->llot_objects[0].l_ost_idx;
return layout;
}
* \param[in] layout the layout to copy from
*
* \retval valid lov_user_md pointer on success
- * \retval NULL if memory allocation fails
+ * \retval NULL if memory allocation fails or the layout is invalid
*/
static struct lov_user_md *
llapi_layout_to_lum(const struct llapi_layout *layout)
struct lov_user_md *lum;
size_t lum_size;
uint32_t magic = LOV_USER_MAGIC_V1;
+ uint64_t pattern = layout->llot_pattern;
+ struct lov_user_ost_data *lmm_objects;
+ int obj_count = 0, i;
- if (strlen(layout->llot_pool_name) != 0)
+ if ((pattern & LLAPI_LAYOUT_SPECIFIC) != 0) {
+ if (layout->llot_objects_count < layout->llot_stripe_count) {
+ errno = EINVAL;
+ return NULL;
+ }
+ magic = LOV_USER_MAGIC_SPECIFIC;
+ obj_count = layout->llot_stripe_count;
+ pattern &= ~LLAPI_LAYOUT_SPECIFIC;
+ } else if (strlen(layout->llot_pool_name) != 0) {
magic = LOV_USER_MAGIC_V3;
+ }
- /* The lum->lmm_objects array won't be
- * sent to the kernel when we write the lum, so
- * we don't allocate storage for it.
+ /*
+ * All stripes must be specified when the pattern contains
+ * LLAPI_LAYOUT_SPECIFIC
*/
- lum_size = lov_user_md_size(0, magic);
+ for (i = 0; i < obj_count; i++) {
+ if (layout->llot_objects[i].l_ost_idx ==
+ LLAPI_LAYOUT_IDX_MAX) {
+ errno = EINVAL;
+ return NULL;
+ }
+ }
+
+ lum_size = lov_user_md_size(obj_count, magic);
lum = malloc(lum_size);
if (lum == NULL)
return NULL;
lum->lmm_magic = magic;
- if (layout->llot_pattern == LLAPI_LAYOUT_DEFAULT)
+ if (pattern == LLAPI_LAYOUT_DEFAULT)
lum->lmm_pattern = 0;
- else if (layout->llot_pattern == LLAPI_LAYOUT_RAID0)
- lum->lmm_pattern = 1;
+ else if (pattern == LLAPI_LAYOUT_RAID0)
+ lum->lmm_pattern = LOV_PATTERN_RAID0;
else
- lum->lmm_pattern = layout->llot_pattern;
+ lum->lmm_pattern = pattern;
if (layout->llot_stripe_size == LLAPI_LAYOUT_DEFAULT)
lum->lmm_stripe_size = 0;
if (layout->llot_stripe_count == LLAPI_LAYOUT_DEFAULT)
lum->lmm_stripe_count = 0;
else if (layout->llot_stripe_count == LLAPI_LAYOUT_WIDE)
- lum->lmm_stripe_count = -1;
+ lum->lmm_stripe_count = LOV_ALL_STRIPES;
else
lum->lmm_stripe_count = layout->llot_stripe_count;
else
lum->lmm_stripe_offset = layout->llot_stripe_offset;
- if (lum->lmm_magic != LOV_USER_MAGIC_V1) {
+ if (magic == LOV_USER_MAGIC_V3 || magic == LOV_USER_MAGIC_SPECIFIC) {
struct lov_user_md_v3 *lumv3 = (struct lov_user_md_v3 *)lum;
- strncpy(lumv3->lmm_pool_name, layout->llot_pool_name,
- sizeof(lumv3->lmm_pool_name));
+ if (strlen(layout->llot_pool_name)) {
+ strncpy(lumv3->lmm_pool_name, layout->llot_pool_name,
+ sizeof(lumv3->lmm_pool_name));
+ } else {
+ memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
+ }
+ lmm_objects = lumv3->lmm_objects;
+ } else {
+ lmm_objects = lum->lmm_objects;
}
+ for (i = 0; i < obj_count; i++)
+ lmm_objects[i].l_ost_idx = layout->llot_objects[i].l_ost_idx;
+
return lum;
}
if (dest->llot_stripe_count == LLAPI_LAYOUT_DEFAULT)
dest->llot_stripe_count = src->llot_stripe_count;
+
+ if (dest->llot_stripe_offset == LLAPI_LAYOUT_DEFAULT)
+ dest->llot_stripe_offset = src->llot_stripe_offset;
}
/**
layout->llot_stripe_size = LLAPI_LAYOUT_DEFAULT;
layout->llot_stripe_count = LLAPI_LAYOUT_DEFAULT;
layout->llot_stripe_offset = LLAPI_LAYOUT_DEFAULT;
- layout->llot_objects_are_valid = false;
layout->llot_pool_name[0] = '\0';
return layout;
return layout;
}
-/** * Free memory allocated for \a layout. */
+/**
+ * Free memory allocated for \a layout.
+ *
+ * \param[in] layout previously allocated by llapi_layout_alloc()
+ */
void llapi_layout_free(struct llapi_layout *layout)
{
+ if (layout->llot_objects != NULL)
+ free(layout->llot_objects);
free(layout);
}
return -1;
}
- layout->llot_pattern = pattern;
+ layout->llot_pattern = pattern |
+ (layout->llot_pattern & LLAPI_LAYOUT_SPECIFIC);
return 0;
}
+static inline int stripe_number_roundup(int stripe_number)
+{
+ unsigned int round_up = (stripe_number + 8) & ~7;
+ return round_up > LOV_MAX_STRIPE_COUNT ?
+ LOV_MAX_STRIPE_COUNT : round_up;
+}
+
/**
* Set the OST index of stripe number \a stripe_number to \a ost_index.
*
- * The index may only be set for stripe number 0 for now.
+ * If only the starting stripe's OST index is specified, then this can use
+ * the normal LOV_MAGIC_{V1,V3} layout type. If multiple OST indices are
+ * given, then allocate an array to hold the list of indices and ensure that
+ * the LOV_USER_MAGIC_SPECIFIC layout is used when creating the file.
*
* \param[in] layout layout to set OST index in
* \param[in] stripe_number stripe number to set index for
*
* \retval 0 on success
* \retval -1 if arguments are invalid or an unsupported stripe number
- * was specified
+ * was specified, error returned in errno
*/
int llapi_layout_ost_index_set(struct llapi_layout *layout, int stripe_number,
uint64_t ost_index)
{
- if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
- !llapi_layout_stripe_index_is_valid(ost_index)) {
+ if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
errno = EINVAL;
return -1;
}
-
- if (stripe_number != 0) {
- errno = EOPNOTSUPP;
+ if (!llapi_layout_stripe_index_is_valid(ost_index)) {
+ errno = EINVAL;
return -1;
}
- layout->llot_stripe_offset = ost_index;
+ if (stripe_number == 0 && ost_index == LLAPI_LAYOUT_DEFAULT) {
+ layout->llot_stripe_offset = ost_index;
+ layout->llot_pattern &= ~LLAPI_LAYOUT_SPECIFIC;
+ __llapi_layout_objects_realloc(layout, 0);
+ } else if (stripe_number >= 0 &&
+ stripe_number < LOV_MAX_STRIPE_COUNT) {
+ if (ost_index >= LLAPI_LAYOUT_IDX_MAX) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* Preallocate a few more stripes to avoid realloc() overhead.*/
+ if (__llapi_layout_objects_realloc(layout,
+ stripe_number_roundup(stripe_number)) < 0)
+ return -1;
+
+ layout->llot_objects[stripe_number].l_ost_idx = ost_index;
+
+ if (stripe_number == 0)
+ layout->llot_stripe_offset = ost_index;
+ else
+ layout->llot_pattern |= LLAPI_LAYOUT_SPECIFIC;
+
+ if (layout->llot_stripe_count == LLAPI_LAYOUT_DEFAULT ||
+ layout->llot_stripe_count <= stripe_number)
+ layout->llot_stripe_count = stripe_number + 1;
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
return 0;
}
{
if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
stripe_number >= layout->llot_stripe_count ||
- index == NULL || layout->llot_objects_are_valid == 0) {
+ stripe_number >= layout->llot_objects_count || index == NULL) {
errno = EINVAL;
return -1;
}
- if (layout->llot_objects[stripe_number].l_ost_idx == -1)
+ if (layout->llot_stripe_offset == LLAPI_LAYOUT_DEFAULT)
*index = LLAPI_LAYOUT_DEFAULT;
else
*index = layout->llot_objects[stripe_number].l_ost_idx;
return -1;
}
- lum_size = lov_user_md_size(0, lum->lmm_magic);
+ if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)
+ lum_size = lov_user_md_size(lum->lmm_stripe_count,
+ lum->lmm_magic);
+ else
+ lum_size = lov_user_md_size(0, lum->lmm_magic);
rc = fsetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_size, 0);
if (rc < 0) {