4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * These tests exercise the llapi_layout API which abstracts the layout
24 * of a Lustre file behind an opaque data type. They assume a Lustre
25 * file system with at least 2 OSTs and a pool containing at least the
26 * first 2 OSTs. For example,
28 * sudo lctl pool_new lustre.testpool
29 * sudo lctl pool_add lustre.testpool OST[0-1]
30 * gcc -Wall -g -Werror -o llapi_layout_test llapi_layout_test.c -llustreapi
31 * sudo ./llapi_layout_test
40 #include <sys/signal.h>
41 #include <sys/types.h>
43 #include <lustre/lustreapi.h>
50 #define ERROR(fmt, ...) \
51 fprintf(stderr, "%s: %s:%d: %s: " fmt "\n", \
52 program_invocation_short_name, __FILE__, __LINE__, \
53 __func__, ## __VA_ARGS__);
55 #define DIE(fmt, ...) \
57 ERROR(fmt, ## __VA_ARGS__); \
61 #define ASSERTF(cond, fmt, ...) \
64 DIE("assertion '%s' failed: "fmt, #cond, ## __VA_ARGS__);\
67 static char *lustre_dir;
68 static char *poolname;
69 static int num_osts = -1;
71 void usage(char *prog)
73 printf("Usage: %s [-d lustre_dir] [-p pool_name] [-o num_osts]\n",
79 #define T0_STRIPE_COUNT num_osts
80 #define T0_STRIPE_SIZE 1048576
81 #define T0_OST_OFFSET (num_osts - 1)
82 #define T0_DESC "Read/write layout attributes then create a file"
89 struct llapi_layout *layout = llapi_layout_alloc();
91 char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
93 ASSERTF(layout != NULL, "errno %d", errno);
95 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T0FILE);
98 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
101 rc = llapi_layout_stripe_count_set(layout, T0_STRIPE_COUNT);
102 ASSERTF(rc == 0, "errno = %d", errno);
103 rc = llapi_layout_stripe_count_get(layout, &count);
104 ASSERTF(rc == 0 && count == T0_STRIPE_COUNT, "%"PRIu64" != %d", count,
108 rc = llapi_layout_stripe_size_set(layout, T0_STRIPE_SIZE);
109 ASSERTF(rc == 0, "errno = %d", errno);
110 rc = llapi_layout_stripe_size_get(layout, &size);
111 ASSERTF(rc == 0 && size == T0_STRIPE_SIZE, "%"PRIu64" != %d", size,
115 rc = llapi_layout_pool_name_set(layout, poolname);
116 ASSERTF(rc == 0, "errno = %d", errno);
117 rc = llapi_layout_pool_name_get(layout, mypool, sizeof(mypool));
118 ASSERTF(rc == 0, "errno = %d", errno);
119 rc = strcmp(mypool, poolname);
120 ASSERTF(rc == 0, "%s != %s", mypool, poolname);
123 rc = llapi_layout_ost_index_set(layout, 0, T0_OST_OFFSET);
124 ASSERTF(rc == 0, "errno = %d", errno);
127 fd = llapi_layout_file_create(path, 0, 0660, layout);
128 ASSERTF(fd >= 0, "path = %s, errno = %d", path, errno);
130 ASSERTF(rc == 0, "errno = %d", errno);
131 llapi_layout_free(layout);
134 void __test1_helper(struct llapi_layout *layout)
141 char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
143 rc = llapi_layout_stripe_count_get(layout, &count);
144 ASSERTF(count == T0_STRIPE_COUNT, "%"PRIu64" != %d", count,
147 rc = llapi_layout_stripe_size_get(layout, &size);
148 ASSERTF(size == T0_STRIPE_SIZE, "%"PRIu64" != %d", size,
151 rc = llapi_layout_pool_name_get(layout, mypool, sizeof(mypool));
152 ASSERTF(rc == 0, "errno = %d", errno);
153 rc = strcmp(mypool, poolname);
154 ASSERTF(rc == 0, "%s != %s", mypool, poolname);
156 rc = llapi_layout_ost_index_get(layout, 0, &ost0);
157 ASSERTF(rc == 0, "errno = %d", errno);
158 rc = llapi_layout_ost_index_get(layout, 1, &ost1);
159 ASSERTF(rc == 0, "errno = %d", errno);
160 ASSERTF(ost0 == T0_OST_OFFSET, "%"PRIu64" != %d", ost0, T0_OST_OFFSET);
161 ASSERTF(ost1 != ost0, "%"PRIu64" == %"PRIu64, ost0, ost1);
164 #define T1_DESC "Read test0 file by path and verify attributes"
169 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T0FILE);
170 struct llapi_layout *layout = llapi_layout_get_by_path(path, 0);
171 ASSERTF(layout != NULL, "errno = %d", errno);
172 __test1_helper(layout);
173 llapi_layout_free(layout);
176 #define T2_DESC "Read test0 file by FD and verify attributes"
183 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T0FILE);
185 fd = open(path, O_RDONLY);
186 ASSERTF(fd >= 0, "open(%s): errno = %d", path, errno);
188 struct llapi_layout *layout = llapi_layout_get_by_fd(fd, 0);
189 ASSERTF(layout != NULL, "errno = %d", errno);
192 ASSERTF(rc == 0, "close(%s): errno = %d", path, errno);
194 __test1_helper(layout);
195 llapi_layout_free(layout);
198 #define T3_DESC "Read test0 file by FID and verify attributes"
202 struct llapi_layout *layout;
207 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T0FILE);
209 rc = llapi_path2fid(path, &fid);
210 ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
211 snprintf(fidstr, sizeof(fidstr), "0x%"PRIx64":0x%x:0x%x",
212 (uint64_t)fid.f_seq, fid.f_oid, fid.f_ver);
214 layout = llapi_layout_get_by_fid(path, &fid, 0);
215 ASSERTF(layout != NULL, "fidstr = %s, errno = %d", fidstr, errno);
217 __test1_helper(layout);
218 llapi_layout_free(layout);
222 #define T4_STRIPE_COUNT 2
223 #define T4_STRIPE_SIZE 2097152
224 #define T4_DESC "Verify compatibility with 'lfs setstripe'"
232 const char *lfs = getenv("LFS");
233 char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
237 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T4FILE);
240 lfs = "/usr/bin/lfs";
243 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
245 snprintf(cmd, sizeof(cmd), "%s setstripe %s %s -c %d -s %d %s", lfs,
246 strlen(poolname) > 0 ? "-p" : "", poolname, T4_STRIPE_COUNT,
247 T4_STRIPE_SIZE, path);
249 ASSERTF(rc == 0, "system(%s): exit status %d", cmd, WEXITSTATUS(rc));
252 struct llapi_layout *layout = llapi_layout_get_by_path(path, 0);
253 ASSERTF(layout != NULL, "errno = %d", errno);
255 rc = llapi_layout_stripe_count_get(layout, &count);
256 ASSERTF(count == T4_STRIPE_COUNT, "%"PRIu64" != %d", count,
259 rc = llapi_layout_stripe_size_get(layout, &size);
260 ASSERTF(size == T4_STRIPE_SIZE, "%"PRIu64" != %d", size,
263 rc = llapi_layout_pool_name_get(layout, mypool, sizeof(mypool));
264 ASSERTF(rc == 0, "errno = %d", errno);
265 rc = strcmp(mypool, poolname);
266 ASSERTF(rc == 0, "%s != %s", mypool, poolname);
268 rc = llapi_layout_ost_index_get(layout, 0, &ost0);
269 ASSERTF(rc == 0, "errno = %d", errno);
270 rc = llapi_layout_ost_index_get(layout, 1, &ost1);
271 ASSERTF(rc == 0, "errno = %d", errno);
272 ASSERTF(ost1 != ost0, "%"PRIu64" == %"PRIu64, ost0, ost1);
274 llapi_layout_free(layout);
278 #define T5_DESC "llapi_layout_get_by_path ENOENT handling"
283 struct llapi_layout *layout;
285 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T5FILE);
288 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
291 layout = llapi_layout_get_by_path(path, 0);
292 ASSERTF(layout == NULL && errno == ENOENT, "errno = %d", errno);
295 #define T6_DESC "llapi_layout_get_by_fd EBADF handling"
299 struct llapi_layout *layout = llapi_layout_get_by_fd(9999, 0);
300 ASSERTF(layout == NULL && errno == EBADF, "errno = %d", errno);
304 #define T7_DESC "llapi_layout_get_by_path EACCES handling"
309 uid_t myuid = getuid();
311 const char *runas = getenv("RUNAS_ID");
315 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T7FILE);
316 ASSERTF(myuid == 0, "myuid = %d", myuid); /* Need root for this test. */
318 /* Create file as root */
320 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
322 fd = open(path, O_CREAT, 0400);
323 ASSERTF(fd > 0, "errno = %d", errno);
325 ASSERTF(rc == 0, "errno = %d", errno);
327 /* Become unprivileged user */
330 ASSERTF(uid != 0, "runas = %s", runas);
332 pw = getpwnam("nobody");
333 ASSERTF(pw != NULL, "errno = %d", errno);
337 ASSERTF(rc == 0, "errno = %d", errno);
339 struct llapi_layout *layout = llapi_layout_get_by_path(path, 0);
340 ASSERTF(layout == NULL && errno == EACCES, "errno = %d", errno);
342 ASSERTF(rc == 0, "errno = %d", errno);
345 /* llapi_layout_get_by_path() returns default layout for file with no
346 * striping attributes. */
348 #define T8_DESC "llapi_layout_get_by_path ENODATA handling"
353 struct llapi_layout *layout;
359 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T8FILE);
362 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
363 fd = open(path, O_CREAT, 0640);
364 ASSERTF(fd >= 0, "errno = %d", errno);
366 ASSERTF(rc == 0, "errno = %d", errno);
368 layout = llapi_layout_get_by_path(path, 0);
369 ASSERTF(layout != NULL, "errno = %d\n", errno);
371 rc = llapi_layout_stripe_count_get(layout, &count);
372 ASSERTF(rc == 0, "errno = %d\n", errno);
373 ASSERTF(count == LLAPI_LAYOUT_DEFAULT, "count = %"PRIu64"\n", count);
375 rc = llapi_layout_stripe_size_get(layout, &size);
376 ASSERTF(rc == 0, "errno = %d\n", errno);
377 ASSERTF(size == LLAPI_LAYOUT_DEFAULT, "size = %"PRIu64"\n", size);
379 rc = llapi_layout_pattern_get(layout, &pattern);
380 ASSERTF(rc == 0, "errno = %d\n", errno);
381 ASSERTF(pattern == LLAPI_LAYOUT_DEFAULT, "pattern = %"PRIu64"\n",
384 llapi_layout_free(layout);
387 /* Verify llapi_layout_patter_set() return values for various inputs. */
388 #define T9_DESC "verify llapi_layout_pattern_set() return values"
391 struct llapi_layout *layout;
394 layout = llapi_layout_alloc();
395 ASSERTF(layout != NULL, "errno = %d\n", errno);
398 rc = llapi_layout_pattern_set(layout, LLAPI_LAYOUT_INVALID);
399 ASSERTF(rc == -1 && errno == EOPNOTSUPP, "rc = %d, errno = %d", rc,
403 rc = llapi_layout_pattern_set(NULL, LLAPI_LAYOUT_DEFAULT);
404 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc,
408 rc = llapi_layout_pattern_set(layout, LLAPI_LAYOUT_DEFAULT);
409 ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
412 rc = llapi_layout_pattern_set(layout, LLAPI_LAYOUT_RAID0);
413 ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
415 llapi_layout_free(layout);
418 /* Verify stripe_count interfaces return errors as expected */
419 #define T10_DESC "stripe_count error handling"
424 struct llapi_layout *layout;
426 layout = llapi_layout_alloc();
427 ASSERTF(layout != NULL, "errno = %d", errno);
429 /* invalid stripe count */
431 rc = llapi_layout_stripe_count_set(layout, LLAPI_LAYOUT_INVALID);
432 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
435 rc = llapi_layout_stripe_count_set(layout, -1);
436 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
440 rc = llapi_layout_stripe_count_set(NULL, 2);
441 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
445 rc = llapi_layout_stripe_count_get(NULL, &count);
446 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
450 rc = llapi_layout_stripe_count_get(layout, NULL);
451 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
453 /* stripe count too large */
455 rc = llapi_layout_stripe_count_set(layout, LOV_MAX_STRIPE_COUNT + 1);
456 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
457 llapi_layout_free(layout);
460 /* Verify stripe_size interfaces return errors as expected */
461 #define T11_DESC "stripe_size error handling"
466 struct llapi_layout *layout;
468 layout = llapi_layout_alloc();
469 ASSERTF(layout != NULL, "errno = %d", errno);
471 /* negative stripe size */
473 rc = llapi_layout_stripe_size_set(layout, -1);
474 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
476 /* invalid stripe size */
478 rc = llapi_layout_stripe_size_set(layout, LLAPI_LAYOUT_INVALID);
479 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
481 /* stripe size too big */
483 rc = llapi_layout_stripe_size_set(layout, (1ULL << 33));
484 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
488 rc = llapi_layout_stripe_size_set(NULL, 1048576);
489 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
492 rc = llapi_layout_stripe_size_get(NULL, &size);
493 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
497 rc = llapi_layout_stripe_size_get(layout, NULL);
498 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
500 llapi_layout_free(layout);
503 /* Verify pool_name interfaces return errors as expected */
504 #define T12_DESC "pool_name error handling"
508 struct llapi_layout *layout;
509 char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
511 layout = llapi_layout_alloc();
512 ASSERTF(layout != NULL, "errno = %d", errno);
516 rc = llapi_layout_pool_name_set(NULL, "foo");
517 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
521 rc = llapi_layout_pool_name_set(layout, NULL);
522 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
526 rc = llapi_layout_pool_name_get(NULL, mypool, sizeof(mypool));
527 ASSERTF(errno == EINVAL, "poolname = %s, errno = %d", poolname, errno);
531 rc = llapi_layout_pool_name_get(layout, NULL, sizeof(mypool));
532 ASSERTF(errno == EINVAL, "poolname = %s, errno = %d", poolname, errno);
534 /* Pool name too long*/
536 rc = llapi_layout_pool_name_set(layout, "0123456789abcdef");
537 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
539 llapi_layout_free(layout);
542 /* Verify ost_index interface returns errors as expected */
543 #define T13FILE "t13"
544 #define T13_STRIPE_COUNT 2
545 #define T13_DESC "ost_index error handling"
551 struct llapi_layout *layout;
554 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T13FILE);
556 layout = llapi_layout_alloc();
557 ASSERTF(layout != NULL, "errno = %d", errno);
559 /* Only setting OST index for stripe 0 is supported for now. */
561 rc = llapi_layout_ost_index_set(layout, 1, 1);
562 ASSERTF(rc == -1 && errno == EOPNOTSUPP, "rc = %d, errno = %d",
565 /* invalid OST index */
567 rc = llapi_layout_ost_index_set(layout, 0, LLAPI_LAYOUT_INVALID);
568 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
571 rc = llapi_layout_ost_index_set(layout, 0, -1);
572 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
576 rc = llapi_layout_ost_index_set(NULL, 0, 1);
577 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
580 rc = llapi_layout_ost_index_get(NULL, 0, &idx);
581 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
585 rc = llapi_layout_ost_index_get(layout, 0, NULL);
586 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
588 /* Layout not read from file so has no OST data. */
590 rc = llapi_layout_stripe_count_set(layout, T13_STRIPE_COUNT);
591 ASSERTF(rc == 0, "errno = %d", errno);
592 rc = llapi_layout_ost_index_get(layout, 0, &idx);
593 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
595 /* n greater than stripe count*/
597 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
598 rc = llapi_layout_stripe_count_set(layout, T13_STRIPE_COUNT);
599 ASSERTF(rc == 0, "errno = %d", errno);
600 fd = llapi_layout_file_create(path, 0, 0644, layout);
601 ASSERTF(fd >= 0, "errno = %d", errno);
603 ASSERTF(rc == 0, "errno = %d", errno);
604 llapi_layout_free(layout);
606 layout = llapi_layout_get_by_path(path, 0);
607 ASSERTF(layout != NULL, "errno = %d", errno);
609 rc = llapi_layout_ost_index_get(layout, T13_STRIPE_COUNT + 1, &idx);
610 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
612 llapi_layout_free(layout);
615 /* Verify llapi_layout_file_create() returns errors as expected */
616 #define T14_DESC "llapi_layout_file_create error handling"
620 struct llapi_layout *layout = llapi_layout_alloc();
624 rc = llapi_layout_file_create(NULL, 0, 0, layout);
625 ASSERTF(rc == -1 && errno == EINVAL, "rc = %d, errno = %d", rc, errno);
627 llapi_layout_free(layout);
630 /* Can't change striping attributes of existing file. */
631 #define T15FILE "t15"
632 #define T15_STRIPE_COUNT 2
633 #define T15_DESC "Can't change striping attributes of existing file"
639 struct llapi_layout *layout;
642 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T15FILE);
645 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
647 layout = llapi_layout_alloc();
648 ASSERTF(layout != NULL, "errno = %d", errno);
649 rc = llapi_layout_stripe_count_set(layout, T15_STRIPE_COUNT);
650 ASSERTF(rc == 0, "errno = %d", errno);
653 fd = llapi_layout_file_create(path, 0, 0640, layout);
654 ASSERTF(fd >= 0, "fd = %d, errno = %d", fd, errno);
656 ASSERTF(rc == 0, "errno = %d", errno);
658 rc = llapi_layout_stripe_count_set(layout, T15_STRIPE_COUNT - 1);
660 fd = llapi_layout_file_open(path, 0, 0640, layout);
661 ASSERTF(fd >= 0, "fd = %d, errno = %d", fd, errno);
663 ASSERTF(rc == 0, "errno = %d", errno);
664 llapi_layout_free(layout);
666 layout = llapi_layout_get_by_path(path, 0);
667 ASSERTF(layout != NULL, "errno = %d", errno);
668 rc = llapi_layout_stripe_count_get(layout, &count);
669 ASSERTF(rc == 0 && count == T15_STRIPE_COUNT,
670 "rc = %d, %"PRIu64" != %d", rc, count, T15_STRIPE_COUNT);
671 llapi_layout_free(layout);
674 /* Default stripe attributes are applied as expected. */
675 #define T16FILE "t16"
676 #define T16_DESC "Default stripe attributes are applied as expected"
681 struct llapi_layout *deflayout;
682 struct llapi_layout *filelayout;
689 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T16FILE);
692 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
694 deflayout = llapi_layout_get_by_path(lustre_dir, LAYOUT_GET_EXPECTED);
695 ASSERTF(deflayout != NULL, "errno = %d", errno);
696 rc = llapi_layout_stripe_size_get(deflayout, &dsize);
697 ASSERTF(rc == 0, "errno = %d", errno);
698 rc = llapi_layout_stripe_count_get(deflayout, &dcount);
699 ASSERTF(rc == 0, "errno = %d", errno);
701 /* First, with a default struct llapi_layout */
702 filelayout = llapi_layout_alloc();
703 ASSERTF(filelayout != NULL, "errno = %d", errno);
705 fd = llapi_layout_file_create(path, 0, 0640, filelayout);
706 ASSERTF(fd >= 0, "errno = %d", errno);
709 ASSERTF(rc == 0, "errno = %d", errno);
711 llapi_layout_free(filelayout);
713 filelayout = llapi_layout_get_by_path(path, 0);
714 ASSERTF(filelayout != NULL, "errno = %d", errno);
716 rc = llapi_layout_stripe_count_get(filelayout, &fcount);
717 ASSERTF(rc == 0, "errno = %d", errno);
718 ASSERTF(fcount == dcount, "%"PRIu64" != %"PRIu64, fcount, dcount);
720 rc = llapi_layout_stripe_size_get(filelayout, &fsize);
721 ASSERTF(rc == 0, "errno = %d", errno);
722 ASSERTF(fsize == dsize, "%"PRIu64" != %"PRIu64, fsize, dsize);
724 /* NULL layout also implies default layout */
726 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
728 fd = llapi_layout_file_create(path, 0, 0640, filelayout);
729 ASSERTF(fd >= 0, "errno = %d", errno);
731 ASSERTF(rc == 0, "errno = %d", errno);
732 filelayout = llapi_layout_get_by_path(path, 0);
733 ASSERTF(filelayout != NULL, "errno = %d", errno);
735 rc = llapi_layout_stripe_count_get(filelayout, &fcount);
736 ASSERTF(rc == 0, "errno = %d", errno);
737 rc = llapi_layout_stripe_size_get(filelayout, &fsize);
738 ASSERTF(rc == 0, "errno = %d", errno);
739 ASSERTF(fcount == dcount, "%"PRIu64" != %"PRIu64, fcount, dcount);
740 ASSERTF(fsize == dsize, "%"PRIu64" != %"PRIu64, fsize, dsize);
742 llapi_layout_free(filelayout);
743 llapi_layout_free(deflayout);
746 /* Setting stripe count to LLAPI_LAYOUT_WIDE uses all available OSTs. */
747 #define T17FILE "t17"
748 #define T17_DESC "LLAPI_LAYOUT_WIDE is honored"
754 uint64_t osts_layout;
755 struct llapi_layout *layout;
758 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T17FILE);
761 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
762 layout = llapi_layout_alloc();
763 ASSERTF(layout != NULL, "errno = %d", errno);
764 rc = llapi_layout_stripe_count_set(layout, LLAPI_LAYOUT_WIDE);
765 ASSERTF(rc == 0, "errno = %d", errno);
766 fd = llapi_layout_file_create(path, 0, 0640, layout);
767 ASSERTF(fd >= 0, "errno = %d", errno);
769 ASSERTF(rc == 0, "errno = %d", errno);
770 llapi_layout_free(layout);
772 /* Get number of available OSTs */
773 fd = open(path, O_RDONLY);
774 ASSERTF(fd >= 0, "errno = %d", errno);
775 rc = llapi_lov_get_uuids(fd, NULL, &osts_all);
776 ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
778 ASSERTF(rc == 0, "errno = %d", errno);
780 layout = llapi_layout_get_by_path(path, 0);
781 ASSERTF(layout != NULL, "errno = %d", errno);
782 rc = llapi_layout_stripe_count_get(layout, &osts_layout);
783 ASSERTF(osts_layout == osts_all, "%"PRIu64" != %d", osts_layout,
786 llapi_layout_free(layout);
789 /* Setting pool with "fsname.pool" notation. */
790 #define T18FILE "t18"
791 #define T18_DESC "Setting pool with fsname.pool notation"
796 struct llapi_layout *layout = llapi_layout_alloc();
798 char pool[LOV_MAXPOOLNAME*2 + 1];
799 char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
801 snprintf(pool, sizeof(pool), "lustre.%s", poolname);
803 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T18FILE);
805 ASSERTF(layout != NULL, "errno = %d", errno);
808 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
810 rc = llapi_layout_pool_name_set(layout, pool);
811 ASSERTF(rc == 0, "errno = %d", errno);
813 rc = llapi_layout_pool_name_get(layout, mypool, sizeof(mypool));
814 ASSERTF(rc == 0, "errno = %d", errno);
815 rc = strcmp(mypool, poolname);
816 ASSERTF(rc == 0, "%s != %s", mypool, poolname);
817 fd = llapi_layout_file_create(path, 0, 0640, layout);
818 ASSERTF(fd >= 0, "errno = %d", errno);
820 ASSERTF(rc == 0, "errno = %d", errno);
822 llapi_layout_free(layout);
824 layout = llapi_layout_get_by_path(path, 0);
825 ASSERTF(layout != NULL, "errno = %d", errno);
826 rc = llapi_layout_pool_name_get(layout, mypool, sizeof(mypool));
827 ASSERTF(rc == 0, "errno = %d", errno);
828 rc = strcmp(mypool, poolname);
829 ASSERTF(rc == 0, "%s != %s", mypool, poolname);
830 llapi_layout_free(layout);
833 #define T19_DESC "Maximum length pool name is NULL-terminated"
836 struct llapi_layout *layout;
837 char *name = "0123456789abcde";
838 char mypool[LOV_MAXPOOLNAME + 1] = { '\0' };
841 layout = llapi_layout_alloc();
842 ASSERTF(layout != NULL, "errno = %d", errno);
843 rc = llapi_layout_pool_name_set(layout, name);
844 ASSERTF(rc == 0, "errno = %d", errno);
845 rc = llapi_layout_pool_name_get(layout, mypool, sizeof(mypool));
846 ASSERTF(strlen(name) == strlen(mypool), "name = %s, str = %s", name,
848 llapi_layout_free(layout);
851 #define T20FILE "t20"
852 #define T20_DESC "LLAPI_LAYOUT_DEFAULT is honored"
857 struct llapi_layout *deflayout;
858 struct llapi_layout *filelayout;
865 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T20FILE);
868 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
870 filelayout = llapi_layout_alloc();
871 ASSERTF(filelayout != NULL, "errno = %d", errno);
873 rc = llapi_layout_stripe_size_set(filelayout, LLAPI_LAYOUT_DEFAULT);
874 ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
876 rc = llapi_layout_stripe_count_set(filelayout, LLAPI_LAYOUT_DEFAULT);
877 ASSERTF(rc == 0, "rc = %d, errno = %d", rc, errno);
879 fd = llapi_layout_file_create(path, 0, 0640, filelayout);
880 ASSERTF(fd >= 0, "errno = %d", errno);
883 ASSERTF(rc == 0, "errno = %d", errno);
885 llapi_layout_free(filelayout);
887 deflayout = llapi_layout_get_by_path(lustre_dir, LAYOUT_GET_EXPECTED);
888 ASSERTF(deflayout != NULL, "errno = %d", errno);
890 filelayout = llapi_layout_get_by_path(path, 0);
891 ASSERTF(filelayout != NULL, "errno = %d", errno);
893 rc = llapi_layout_stripe_count_get(filelayout, &fcount);
894 ASSERTF(rc == 0, "errno = %d", errno);
895 rc = llapi_layout_stripe_count_get(deflayout, &dcount);
896 ASSERTF(rc == 0, "errno = %d", errno);
897 ASSERTF(fcount == dcount, "%"PRIu64" != %"PRIu64, fcount, dcount);
899 rc = llapi_layout_stripe_size_get(filelayout, &fsize);
900 ASSERTF(rc == 0, "errno = %d", errno);
901 rc = llapi_layout_stripe_size_get(deflayout, &dsize);
902 ASSERTF(rc == 0, "errno = %d", errno);
903 ASSERTF(fsize == dsize, "%"PRIu64" != %"PRIu64, fsize, dsize);
905 llapi_layout_free(filelayout);
906 llapi_layout_free(deflayout);
909 #define T21_DESC "llapi_layout_file_create fails for non-Lustre file"
912 struct llapi_layout *layout;
913 char template[PATH_MAX];
917 snprintf(template, sizeof(template), "%s/XXXXXX", P_tmpdir);
918 fd = mkstemp(template);
919 ASSERTF(fd >= 0, "template = %s, errno = %d", template, errno);
921 ASSERTF(rc == 0, "errno = %d", fd);
922 rc = unlink(template);
923 ASSERTF(rc == 0, "errno = %d", errno);
925 layout = llapi_layout_alloc();
926 ASSERTF(layout != NULL, "errno = %d", errno);
928 fd = llapi_layout_file_create(template, 0, 0640, layout);
929 ASSERTF(fd == -1 && errno == ENOTTY,
930 "fd = %d, errno = %d, template = %s", fd, errno, template);
931 llapi_layout_free(layout);
934 #define T22FILE "t22"
935 #define T22_DESC "llapi_layout_file_create applied mode correctly"
942 mode_t mode_in = 0640;
946 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T22FILE);
949 ASSERTF(rc == 0 || errno == ENOENT, "errno = %d", errno);
951 umask_orig = umask(S_IWGRP | S_IWOTH);
953 fd = llapi_layout_file_create(path, 0, mode_in, NULL);
954 ASSERTF(fd >= 0, "errno = %d", errno);
956 (void) umask(umask_orig);
959 ASSERTF(rc == 0, "errno = %d", errno);
962 ASSERTF(rc == 0, "errno = %d", fd);
964 mode_out = st.st_mode & ~S_IFMT;
965 ASSERTF(mode_in == mode_out, "%o != %o", mode_in, mode_out);
968 #define T23_DESC "llapi_layout_get_by_path fails for non-Lustre file"
971 struct llapi_layout *layout;
972 char template[PATH_MAX];
976 snprintf(template, sizeof(template), "%s/XXXXXX", P_tmpdir);
977 fd = mkstemp(template);
978 ASSERTF(fd >= 0, "template = %s, errno = %d", template, errno);
980 ASSERTF(rc == 0, "errno = %d", fd);
982 layout = llapi_layout_get_by_path(template, 0);
983 ASSERTF(layout == NULL && errno == ENOTTY,
984 "errno = %d, template = %s", errno, template);
986 rc = unlink(template);
987 ASSERTF(rc == 0, "errno = %d", errno);
990 /* llapi_layout_get_by_path(path, LAYOUT_GET_EXPECTED) returns expected layout
991 * for file with unspecified layout. */
992 #define T24FILE "t24"
993 #define T24_DESC "LAYOUT_GET_EXPECTED works with existing file"
998 struct llapi_layout *layout;
1002 char path[PATH_MAX];
1004 snprintf(path, sizeof(path), "%s/%s", lustre_dir, T24FILE);
1007 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
1008 fd = open(path, O_CREAT, 0640);
1009 ASSERTF(fd >= 0, "errno = %d", errno);
1011 ASSERTF(rc == 0, "errno = %d", errno);
1013 layout = llapi_layout_get_by_path(path, LAYOUT_GET_EXPECTED);
1014 ASSERTF(layout != NULL, "errno = %d\n", errno);
1016 rc = llapi_layout_stripe_count_get(layout, &count);
1017 ASSERTF(rc == 0, "errno = %d\n", errno);
1018 ASSERTF(count != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1020 rc = llapi_layout_stripe_size_get(layout, &size);
1021 ASSERTF(rc == 0, "errno = %d\n", errno);
1022 ASSERTF(size != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1024 rc = llapi_layout_pattern_get(layout, &pattern);
1025 ASSERTF(rc == 0, "errno = %d\n", errno);
1026 ASSERTF(pattern != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1028 llapi_layout_free(layout);
1031 /* llapi_layout_get_by_path(path, LAYOUT_GET_EXPECTED) returns expected layout
1032 * for directory with unspecified layout. */
1033 #define T25DIR "d25"
1034 #define T25_DESC "LAYOUT_GET_EXPECTED works with directory"
1038 struct llapi_layout *layout;
1044 snprintf(dir, sizeof(dir), "%s/%s", lustre_dir, T25DIR);
1047 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
1048 rc = mkdir(dir, 0750);
1049 ASSERTF(rc == 0, "errno = %d", errno);
1051 layout = llapi_layout_get_by_path(dir, LAYOUT_GET_EXPECTED);
1052 ASSERTF(layout != NULL, "errno = %d\n", errno);
1054 rc = llapi_layout_stripe_count_get(layout, &count);
1055 ASSERTF(rc == 0, "errno = %d\n", errno);
1056 ASSERTF(count != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1058 rc = llapi_layout_stripe_size_get(layout, &size);
1059 ASSERTF(rc == 0, "errno = %d\n", errno);
1060 ASSERTF(size != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1062 rc = llapi_layout_pattern_get(layout, &pattern);
1063 ASSERTF(rc == 0, "errno = %d\n", errno);
1064 ASSERTF(pattern != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1066 llapi_layout_free(layout);
1069 /* llapi_layout_get_by_path(path, LAYOUT_GET_EXPECTED) correctly combines
1070 * specified attributes of parent directory with attributes filesystem root. */
1071 #define T26DIR "d26"
1072 #define T26_DESC "LAYOUT_GET_EXPECTED partially specified parent"
1073 #define T26_STRIPE_SIZE (1048576 * 4)
1077 struct llapi_layout *layout;
1078 const char *lfs = getenv("LFS");
1085 snprintf(dir, sizeof(dir), "%s/%s", lustre_dir, T26DIR);
1087 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
1088 rc = mkdir(dir, 0750);
1089 ASSERTF(rc == 0, "errno = %d", errno);
1092 lfs = "/usr/bin/lfs";
1094 snprintf(cmd, sizeof(cmd), "%s setstripe -s %d %s", lfs,
1095 T26_STRIPE_SIZE, dir);
1097 ASSERTF(rc == 0, "system(%s): exit status %d", cmd, WEXITSTATUS(rc));
1099 layout = llapi_layout_get_by_path(dir, LAYOUT_GET_EXPECTED);
1100 ASSERTF(layout != NULL, "errno = %d\n", errno);
1102 rc = llapi_layout_stripe_count_get(layout, &count);
1103 ASSERTF(rc == 0, "errno = %d\n", errno);
1104 ASSERTF(count != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1106 rc = llapi_layout_stripe_size_get(layout, &size);
1107 ASSERTF(rc == 0, "errno = %d\n", errno);
1108 ASSERTF(size == T26_STRIPE_SIZE, "size = %"PRIu64, size);
1110 rc = llapi_layout_pattern_get(layout, &pattern);
1111 ASSERTF(rc == 0, "errno = %d\n", errno);
1112 ASSERTF(pattern != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1114 llapi_layout_free(layout);
1117 /* llapi_layout_get_by_path(path, LAYOUT_GET_EXPECTED) work with
1118 * non existing file. */
1119 #define T27DIR "d27"
1120 #define T27_DESC "LAYOUT_GET_EXPECTED with non existing file"
1121 #define T27_STRIPE_SIZE (1048576 * 3)
1125 struct llapi_layout *layout;
1126 const char *lfs = getenv("LFS");
1130 char dirpath[PATH_MAX];
1131 char filepath[PATH_MAX];
1134 snprintf(dirpath, sizeof(dirpath), "%s/%s", lustre_dir, T27DIR);
1135 snprintf(filepath, sizeof(filepath), "%s/nonesuch", dirpath);
1137 rc = rmdir(dirpath);
1138 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
1139 rc = mkdir(dirpath, 0750);
1140 ASSERTF(rc == 0, "errno = %d", errno);
1143 lfs = "/usr/bin/lfs";
1145 snprintf(cmd, sizeof(cmd), "%s setstripe -s %d %s", lfs,
1146 T27_STRIPE_SIZE, dirpath);
1148 ASSERTF(rc == 0, "system(%s): exit status %d", cmd, WEXITSTATUS(rc));
1150 layout = llapi_layout_get_by_path(filepath, LAYOUT_GET_EXPECTED);
1151 ASSERTF(layout != NULL, "errno = %d\n", errno);
1153 rc = llapi_layout_stripe_count_get(layout, &count);
1154 ASSERTF(rc == 0, "errno = %d\n", errno);
1155 ASSERTF(count != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1157 rc = llapi_layout_stripe_size_get(layout, &size);
1158 ASSERTF(rc == 0, "errno = %d\n", errno);
1159 ASSERTF(size == T27_STRIPE_SIZE, "size = %"PRIu64, size);
1161 rc = llapi_layout_pattern_get(layout, &pattern);
1162 ASSERTF(rc == 0, "errno = %d\n", errno);
1163 ASSERTF(pattern != LLAPI_LAYOUT_DEFAULT, "expected literal value");
1165 llapi_layout_free(layout);
1168 /* llapi_layout_stripe_count_get returns LLAPI_LAYOUT_WIDE for a directory
1169 * with a stripe_count of -1. */
1170 #define T28DIR "d28"
1171 #define T28_DESC "LLAPI_LAYOUT_WIDE returned as expected"
1175 struct llapi_layout *layout;
1176 const char *lfs = getenv("LFS");
1178 char dirpath[PATH_MAX];
1181 snprintf(dirpath, sizeof(dirpath), "%s/%s", lustre_dir, T28DIR);
1183 rc = rmdir(dirpath);
1184 ASSERTF(rc >= 0 || errno == ENOENT, "errno = %d", errno);
1185 rc = mkdir(dirpath, 0750);
1186 ASSERTF(rc == 0, "errno = %d", errno);
1189 lfs = "/usr/bin/lfs";
1191 snprintf(cmd, sizeof(cmd), "%s setstripe -c -1 %s", lfs, dirpath);
1193 ASSERTF(rc == 0, "system(%s): exit status %d", cmd, WEXITSTATUS(rc));
1195 layout = llapi_layout_get_by_path(dirpath, 0);
1196 ASSERTF(layout != NULL, "errno = %d\n", errno);
1198 rc = llapi_layout_stripe_count_get(layout, &count);
1199 ASSERTF(rc == 0, "errno = %d\n", errno);
1200 ASSERTF(count == LLAPI_LAYOUT_WIDE, "count = %"PRIu64"\n", count);
1202 llapi_layout_free(layout);
1205 #define TEST_DESC_LEN 50
1206 struct test_tbl_entry {
1207 void (*tte_fn)(void);
1208 char tte_desc[TEST_DESC_LEN];
1212 static struct test_tbl_entry test_tbl[] = {
1213 { &test0, T0_DESC, false },
1214 { &test1, T1_DESC, false },
1215 { &test2, T2_DESC, false },
1216 { &test3, T3_DESC, false },
1217 { &test4, T4_DESC, false },
1218 { &test5, T5_DESC, false },
1219 { &test6, T6_DESC, false },
1220 { &test7, T7_DESC, false },
1221 { &test8, T8_DESC, false },
1222 { &test9, T9_DESC, false },
1223 { &test10, T10_DESC, false },
1224 { &test11, T11_DESC, false },
1225 { &test12, T12_DESC, false },
1226 { &test13, T13_DESC, false },
1227 { &test14, T14_DESC, false },
1228 { &test15, T15_DESC, false },
1229 { &test16, T16_DESC, false },
1230 { &test17, T17_DESC, false },
1231 { &test18, T18_DESC, false },
1232 { &test19, T19_DESC, false },
1233 { &test20, T20_DESC, false },
1234 { &test21, T21_DESC, false },
1235 { &test22, T22_DESC, false },
1236 { &test23, T23_DESC, false },
1237 { &test24, T24_DESC, false },
1238 { &test25, T25_DESC, false },
1239 { &test26, T26_DESC, false },
1240 { &test27, T27_DESC, false },
1241 { &test28, T28_DESC, false },
1243 #define NUM_TESTS (sizeof(test_tbl) / sizeof(struct test_tbl_entry))
1245 void print_test_desc(int test_num, const char *test_desc, const char *status)
1249 printf(" test %2d: %s ", test_num, test_desc);
1250 for (i = 0; i < TEST_DESC_LEN - strlen(test_desc); i++)
1252 printf(" %s\n", status);
1255 /* This function runs a single test by forking the process. This way,
1256 * if there is a segfault during a test, the test program won't crash. */
1257 int test(void (*test_fn)(), const char *test_desc, bool test_skip, int test_num)
1261 char status_buf[128];
1264 print_test_desc(test_num, test_desc, "skip");
1270 ERROR("cannot fork: %s", strerror(errno));
1271 } else if (pid > 0) {
1274 /* Non-zero value indicates failure. */
1277 strncpy(status_buf, "pass", sizeof(status_buf));
1278 } else if WIFSIGNALED(status) {
1279 snprintf(status_buf, sizeof(status_buf),
1280 "fail (exit status %d, killed by SIG%d)",
1281 WEXITSTATUS(status), WTERMSIG(status));
1284 snprintf(status_buf, sizeof(status_buf),
1285 "fail (exit status %d)", WEXITSTATUS(status));
1288 print_test_desc(test_num, test_desc, status_buf);
1289 } else if (pid == 0) {
1290 /* Run the test in the child process. Exit with 0 for success,
1291 * non-zero for failure */
1299 static void process_args(int argc, char *argv[])
1303 while ((c = getopt(argc, argv, "d:p:o:")) != -1) {
1306 lustre_dir = optarg;
1312 num_osts = atoi(optarg);
1315 fprintf(stderr, "Unknown option '%c'\n", optopt);
1321 int main(int argc, char *argv[])
1328 llapi_msg_set_level(LLAPI_MSG_OFF);
1330 process_args(argc, argv);
1331 if (lustre_dir == NULL)
1332 lustre_dir = "/mnt/lustre";
1333 if (poolname == NULL)
1334 poolname = "testpool";
1339 DIE("Error: at least 2 OSTS are required\n");
1341 if (stat(lustre_dir, &s) < 0)
1342 DIE("cannot stat %s: %s\n", lustre_dir, strerror(errno));
1343 else if (!S_ISDIR(s.st_mode))
1344 DIE("%s: not a directory\n", lustre_dir);
1346 rc = llapi_search_fsname(lustre_dir, fsname);
1348 fprintf(stderr, "Error: %s: not a Lustre filesystem\n",
1353 /* Play nice with Lustre test scripts. Non-line buffered output
1354 * stream under I/O redirection may appear incorrectly. */
1355 setvbuf(stdout, NULL, _IOLBF, 0);
1357 for (i = 0; i < NUM_TESTS; i++) {
1358 struct test_tbl_entry *tst = &test_tbl[i];
1359 if (test(tst->tte_fn, tst->tte_desc, tst->tte_skip, i) != 0)