Whamcloud - gitweb
LU-8454 llite: normal user can't set FS default stripe
[fs/lustre-release.git] / lustre / tests / llapi_layout_test.c
index 98a812a..9d4dac6 100644 (file)
@@ -70,8 +70,8 @@ static int num_osts = -1;
 
 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);
 }
 
@@ -173,7 +173,6 @@ void test1(void)
        llapi_layout_free(layout);
 }
 
-
 #define T2_DESC                "Read test0 file by FD and verify attributes"
 void test2(void)
 {
@@ -219,7 +218,6 @@ void test3(void)
        llapi_layout_free(layout);
 }
 
-
 #define T4FILE                 "t4"
 #define T4_STRIPE_COUNT                2
 #define T4_STRIPE_SIZE         2097152
@@ -302,7 +300,6 @@ void test6(void)
        ASSERTF(layout == NULL && errno == EBADF, "errno = %d", errno);
 }
 
-
 #define T7FILE         "t7"
 #define T7_DESC                "llapi_layout_get_by_path EACCES handling"
 void test7(void)
@@ -345,7 +342,6 @@ void test7(void)
        ASSERTF(rc == 0, "errno = %d", errno);
 }
 
-
 /* llapi_layout_get_by_path() returns default layout for file with no
  * striping attributes. */
 #define T8FILE         "t8"
@@ -388,8 +384,8 @@ void test8(void)
        llapi_layout_free(layout);
 }
 
-/* Setting pattern > 0 returns EOPNOTSUPP in errno. */
-#define T9_DESC                "llapi_layout_pattern_set() EOPNOTSUPP handling"
+/* Verify llapi_layout_patter_set() return values for various inputs. */
+#define T9_DESC                "verify llapi_layout_pattern_set() return values"
 void test9(void)
 {
        struct llapi_layout *layout;
@@ -397,14 +393,28 @@ void test9(void)
 
        layout = llapi_layout_alloc();
        ASSERTF(layout != NULL, "errno = %d\n", errno);
+
        errno = 0;
-       rc = llapi_layout_pattern_set(layout, 1);
+       rc = llapi_layout_pattern_set(layout, LLAPI_LAYOUT_INVALID);
        ASSERTF(rc == -1 && errno == EOPNOTSUPP, "rc = %d, errno = %d", rc,
                errno);
+
+       errno = 0;
+       rc = llapi_layout_pattern_set(NULL, LLAPI_LAYOUT_DEFAULT);
+       ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc,
+               errno);
+
+       errno = 0;
+       rc = llapi_layout_pattern_set(layout, LLAPI_LAYOUT_DEFAULT);
+       ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
+
+       errno = 0;
+       rc = llapi_layout_pattern_set(layout, LLAPI_LAYOUT_RAID0);
+       ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
+
        llapi_layout_free(layout);
 }
 
-
 /* Verify stripe_count interfaces return errors as expected */
 #define T10_DESC       "stripe_count error handling"
 void test10(void)
@@ -523,7 +533,7 @@ void test12(void)
 
        /* Pool name too long*/
        errno = 0;
-       rc = llapi_layout_pool_name_set(layout, "0123456789abcdef0");
+       rc = llapi_layout_pool_name_set(layout, "0123456789abcdef");
        ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
 
        llapi_layout_free(layout);
@@ -546,12 +556,6 @@ void test13(void)
        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);
@@ -824,7 +828,7 @@ void test18(void)
 void test19(void)
 {
        struct llapi_layout *layout;
-       char *name = "0123456789abcdef";
+       char *name = "0123456789abcde";
        char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
        int rc;
 
@@ -1192,6 +1196,119 @@ void test28(void)
        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);
@@ -1229,6 +1346,7 @@ static struct test_tbl_entry test_tbl[] = {
        { &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))
 
@@ -1286,11 +1404,31 @@ int test(void (*test_fn)(), const char *test_desc, bool test_skip, int test_num)
        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;
@@ -1301,6 +1439,9 @@ static void process_args(int argc, char *argv[])
                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]);
@@ -1313,7 +1454,7 @@ int main(int argc, char *argv[])
        int rc = 0;
        int i;
        struct stat s;
-       char fsname[8];
+       char fsname[8 + 1];
 
        llapi_msg_set_level(LLAPI_MSG_OFF);