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
24 * Copyright 2015 Cray Inc, all rights reserved.
27 * A few portions are extracted from llapi_layout_test.c
29 * The purpose of this test is to exert the layout swap function, with
32 * The program will exit as soon as a non zero error code is returned.
39 #include <sys/types.h>
45 #include <lustre/lustreapi.h>
46 #include <lustre/lustre_idl.h>
48 #define ERROR(fmt, ...) \
49 fprintf(stderr, "%s: %s:%d: %s: " fmt "\n", \
50 program_invocation_short_name, __FILE__, __LINE__, \
51 __func__, ## __VA_ARGS__);
53 #define DIE(fmt, ...) \
55 ERROR(fmt, ## __VA_ARGS__); \
59 #define ASSERTF(cond, fmt, ...) \
62 DIE("assertion '%s' failed: "fmt, \
63 #cond, ## __VA_ARGS__); \
66 #define PERFORM(testfn) \
68 fprintf(stderr, "Starting test " #testfn " at %lld\n", \
69 (unsigned long long)time(NULL)); \
73 fprintf(stderr, "Finishing test " #testfn " at %lld\n", \
74 (unsigned long long)time(NULL)); \
77 /* Name of file/directory. Will be set once and will not change. */
78 static char mainpath[PATH_MAX];
79 static const char *maindir = "swap_lock_test_dir_4525654";
81 static char fsmountdir[PATH_MAX]; /* Lustre mountpoint */
82 static char *lustre_dir; /* Test directory inside Lustre */
84 /* Cleanup our test directory. */
85 static void cleanup(void)
90 rc = snprintf(cmd, sizeof(cmd), "rm -rf -- '%s'", mainpath);
91 ASSERTF(rc > 0 && rc < sizeof(cmd),
92 "invalid delete command for path '%s'", mainpath);
94 ASSERTF(rc != -1, "Cannot execute rm command");
95 ASSERTF(WEXITSTATUS(rc) == 0,
96 "rm command returned %d", WEXITSTATUS(rc));
99 /* Create a filename inside the test directory. Will assert on
101 static char *create_file_name(const char *name)
106 rc = asprintf(&filename, "%s/%s/%s", lustre_dir, maindir, name);
107 ASSERTF(rc > 0, "can't make filename for '%s'", name);
112 /* Create a file of a given size in the test directory, filed with
113 * c. Will assert on error. */
114 int create_file(const char *name, size_t size, unsigned char c)
121 filename = create_file_name(name);
123 fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0600);
124 ASSERTF(fd >= 0, "open failed for '%s': %s",
125 filename, strerror(errno));
129 /* Fill-up the new file. */
130 memset(buf, c, sizeof(buf));
133 size_t to_write = size;
135 if (to_write > sizeof(buf))
136 to_write = sizeof(buf);
138 rc = write(fd, buf, to_write);
139 ASSERTF(rc > 0, "writing %zu bytes to '%s' failed: %s",
140 to_write, name, strerror(errno));
148 /* Test basic swap */
149 static void test10(void)
154 size_t foo1_size = 2000;
155 size_t foo2_size = 5000;
158 rc = mkdir(mainpath, 0);
159 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
160 mainpath, strerror(errno));
162 fd1 = create_file("foo1", foo1_size, 'x');
163 fd2 = create_file("foo2", foo2_size, 'y');
165 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, 0);
166 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
169 rc = fstat(fd1, &stbuf);
170 ASSERTF(rc == 0, "stat failed on 'foo1': %s", strerror(errno));
171 ASSERTF(stbuf.st_size == foo2_size,
172 "invalid size found: %llu instead of %zu",
173 (unsigned long long)stbuf.st_size, foo2_size);
175 rc = fstat(fd2, &stbuf);
176 ASSERTF(rc == 0, "stat failed on 'foo2': %s", strerror(errno));
177 ASSERTF(stbuf.st_size == foo1_size,
178 "invalid size found: %llu instead of %zu",
179 (unsigned long long)stbuf.st_size, foo1_size);
185 /* Test self swap. It's a no-op and will always succeed. */
186 static void test11(void)
190 size_t foo1_size = 2000;
192 rc = mkdir(mainpath, 0);
193 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
194 mainpath, strerror(errno));
196 fd1 = create_file("foo1", foo1_size, 'x');
198 rc = llapi_fswap_layouts(fd1, fd1, 0, 0, 0);
199 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
205 /* Test self swap, on different handles. It's a no-op and will always
207 static void test12(void)
212 size_t foo1_size = 2000;
214 rc = mkdir(mainpath, 0);
215 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
216 mainpath, strerror(errno));
218 fd1 = create_file("foo1", foo1_size, 'x');
220 ASSERTF(fd2 != -1, "dup failed: %s", strerror(errno));
222 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, 0);
223 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
230 /* Swap with a non Lustre file */
231 static void test13(void)
236 size_t foo1_size = 2000;
238 rc = mkdir(mainpath, 0);
239 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
240 mainpath, strerror(errno));
242 fd1 = create_file("foo1", foo1_size, 'x');
244 fd2 = open("/dev/null", O_RDWR);
245 ASSERTF(fd2 != -1, "open '/dev/null/' failed: %s", strerror(errno));
247 /* Note that the returned error will be different for both
248 * operations. In the first swap, fd1 is on Lustre, so the
249 * ioctl will succeed, but its processing will eventually fail
250 * because fd2 is not on Lustre. In the second swap, the ioctl
251 * request is unknown, so ioctl() will directly fail. */
252 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, 0);
253 ASSERTF(rc == -EINVAL, "llapi_fswap_layouts failed: %s",
256 rc = llapi_fswap_layouts(fd2, fd1, 0, 0, 0);
257 ASSERTF(rc == -ENOTTY, "llapi_fswap_layouts failed: %s",
264 /* Swap with bogus values */
265 static void test14(void)
269 rc = llapi_fswap_layouts(-6, -2, 0, 0, 0);
270 ASSERTF(rc == -EBADF, "llapi_fswap_layouts failed: %s",
273 /* When run under a shell, rc is -EINVAL. When run under
274 * Lustre test suite, stdin is redirected, and rc is
275 * -ENOTTY. Catch both cases. */
276 rc = llapi_fswap_layouts(0, 0, 0, 0, 0);
277 ASSERTF(rc == -EINVAL || rc == -ENOTTY,
278 "llapi_fswap_layouts failed: %s",
281 rc = llapi_fswap_layouts(456789076, 234567895, 0, 0, 0);
282 ASSERTF(rc == -EBADF, "llapi_fswap_layouts failed: %s",
286 /* Lease only test. */
287 static void test15(void)
294 rc = mkdir(mainpath, 0);
295 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
296 mainpath, strerror(errno));
298 filename = create_file_name("foo1");
300 fd = create_file("foo1", 1000, 'x');
302 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
303 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
307 /* Read lease on read file */
308 fd = open(filename, O_RDONLY);
309 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
311 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
312 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
314 rc = llapi_lease_check(fd);
315 ASSERTF(rc == LL_LEASE_RDLCK,
316 "invalid lease type on '%s': %s", filename, strerror(-rc));
320 /* Write lease on write file */
321 fd = open(filename, O_WRONLY);
322 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
324 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
325 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
327 rc = llapi_lease_check(fd);
328 ASSERTF(rc == LL_LEASE_WRLCK,
329 "invalid lease type on '%s': %s", filename, strerror(-rc));
333 /* Read lease on read/write file */
334 fd = open(filename, O_RDWR);
335 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
337 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
338 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
340 rc = llapi_lease_check(fd);
341 ASSERTF(rc == LL_LEASE_RDLCK,
342 "invalid lease type on '%s': %s", filename, strerror(-rc));
346 /* Write lease on read/write file */
347 fd = open(filename, O_RDWR);
348 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
350 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
351 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
353 rc = llapi_lease_check(fd);
354 ASSERTF(rc == LL_LEASE_WRLCK,
355 "invalid lease type on '%s': %s", filename, strerror(-rc));
359 /* Read lease on write only file */
360 fd = open(filename, O_WRONLY);
361 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
363 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
364 ASSERTF(rc == -EPERM, "cannot get lease '%s': %s",
365 filename, strerror(-rc));
367 rc = llapi_lease_check(fd);
369 "invalid lease type on '%s': %s", filename, strerror(-rc));
373 /* Write lease on read only file */
374 fd = open(filename, O_RDONLY);
375 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
377 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
378 ASSERTF(rc == -EPERM, "cannot get lease '%s': %s",
379 filename, strerror(-rc));
381 rc = llapi_lease_check(fd);
383 "invalid lease type on '%s': %s", filename, strerror(-rc));
387 /* Get read lease again */
388 fd = open(filename, O_RDWR);
389 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
391 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
392 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
394 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
395 ASSERTF(rc == -EBUSY, "can get lease '%s': %s",
396 filename, strerror(-rc));
398 rc = llapi_lease_check(fd);
399 ASSERTF(rc == LL_LEASE_RDLCK,
400 "invalid lease type on '%s': %s", filename, strerror(-rc));
404 /* Get write lease again */
405 fd = open(filename, O_RDWR);
406 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
408 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
409 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
411 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
412 ASSERTF(rc == -EBUSY, "can get lease '%s': %s",
413 filename, strerror(-rc));
415 rc = llapi_lease_check(fd);
416 ASSERTF(rc == LL_LEASE_WRLCK,
417 "invalid lease type on '%s': %s", filename, strerror(-rc));
421 /* Get a lease, release and get again */
422 fd = open(filename, O_RDWR);
423 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
425 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
426 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
428 rc = llapi_lease_check(fd);
429 ASSERTF(rc == LL_LEASE_RDLCK,
430 "invalid lease type on '%s': %s", filename, strerror(-rc));
432 rc = llapi_lease_put(fd);
433 ASSERTF(rc == LL_LEASE_RDLCK, "was not able to put back lease '%s': %s",
434 filename, strerror(-rc));
436 rc = llapi_lease_check(fd);
438 "invalid lease type on '%s': %s", filename, strerror(-rc));
441 /* BUG! This returns EBUSY but there's no lease. */
442 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
443 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
448 /* Get a write lease, release and get again */
449 fd = open(filename, O_RDWR);
450 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
452 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
453 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
455 rc = llapi_lease_check(fd);
456 ASSERTF(rc == LL_LEASE_WRLCK,
457 "invalid lease type on '%s': %s", filename, strerror(-rc));
459 rc = llapi_lease_put(fd);
460 ASSERTF(rc == LL_LEASE_WRLCK, "was not able to put back lease '%s': %s",
461 filename, strerror(-rc));
463 rc = llapi_lease_check(fd);
465 "invalid lease type on '%s': %s", filename, strerror(-rc));
468 /* BUG! This returns EBUSY but there's no lease. */
469 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
470 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
475 /* Get and put lease in a loop */
476 fd = open(filename, O_RDWR);
477 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
479 for (i = 0; i < 1000; i++) {
481 /* BUG! Same as above */
482 rc = llapi_lease_get(fd, LL_LEASE_WRLCK);
483 ASSERTF(rc == 0, "cannot get lease '%s': %s",
484 filename, strerror(-rc));
486 rc = llapi_lease_put(fd);
487 ASSERTF(rc == LL_LEASE_WRLCK,
488 "was not able to put back lease '%s': %s",
489 filename, strerror(-rc));
491 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
492 ASSERTF(rc == 0, "cannot get lease '%s': %s",
493 filename, strerror(-rc));
495 rc = llapi_lease_put(fd);
496 ASSERTF(rc == LL_LEASE_RDLCK,
497 "was not able to put back lease '%s': %s",
498 filename, strerror(-rc));
507 /* Lease on file opened by FID */
508 static void test16(void)
515 rc = mkdir(mainpath, 0);
516 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
517 mainpath, strerror(errno));
519 filename = create_file_name("foo1");
521 fd = create_file("foo1", 1000, 'x');
523 rc = llapi_path2fid(filename, &fid);
524 ASSERTF(rc == 0, "llapi_path2fid failed for '%s': %s",
525 filename, strerror(-rc));
529 fd = llapi_open_by_fid(fsmountdir, &fid,
530 O_RDWR | O_NOATIME | O_NONBLOCK | O_NOFOLLOW);
531 ASSERTF(fd >= 0, "open failed for '%s': %s", filename, strerror(errno));
533 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
534 ASSERTF(rc == 0, "cannot get lease '%s': %s", filename, strerror(-rc));
541 /* Lease on directories */
542 static void test17(void)
548 rc = mkdir(mainpath, 0);
549 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
550 mainpath, strerror(errno));
552 fd = open(mainpath, O_DIRECTORY);
553 ASSERTF(fd >= 0, "open failed for '%s': %s", mainpath, strerror(errno));
555 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
556 ASSERTF(rc == -ENOTTY, "can get lease on directory '%s': %s",
557 mainpath, strerror(-rc));
561 /* On lustre mountpoint */
562 fd = open(fsmountdir, O_DIRECTORY);
563 ASSERTF(fd >= 0, "open failed for '%s': %s", mainpath, strerror(errno));
565 rc = llapi_lease_get(fd, LL_LEASE_RDLCK);
566 ASSERTF(rc == -ENOTTY, "can get lease on directory '%s': %s",
567 mainpath, strerror(-rc));
573 static void test20(void)
578 size_t foo1_size = 2000;
579 size_t foo2_size = 5000;
584 rc = mkdir(mainpath, 0);
585 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
586 mainpath, strerror(errno));
588 fd1 = create_file("foo1", foo1_size, 'x');
589 fd2 = create_file("foo2", foo2_size, 'y');
591 /* foo2 is bigger than foo1. Read a byte in foo2, past foo1_size. */
592 offset = lseek(fd2, foo1_size + 100, SEEK_SET);
593 ASSERTF(offset == foo1_size + 100, "lseek to pos %zu failed: %s",
594 foo1_size + 100, strerror(errno));
596 rc = read(fd2, buf, 1);
597 ASSERTF(rc == 1, "read 1 byte on foo2 failed: %s, rc=%d",
598 strerror(errno), rc);
599 ASSERTF(buf[0] == 'y', "invalid data found on foo2: %x", buf[0]);
602 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, 0);
603 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
606 /* Read from fd1. Its file pointer is now positioned inside
608 rc = read(fd1, buf, 1);
609 ASSERTF(rc == 1, "read 1 byte on foo1 failed: %s", strerror(errno));
610 ASSERTF(buf[0] == 'y', "invalid data found on foo2: %x", buf[0]);
612 rc = fstat(fd2, &stbuf);
613 ASSERTF(rc == 0, "stat failed on 'foo2': %s", strerror(errno));
614 ASSERTF(stbuf.st_size == foo1_size,
615 "invalid size found: %llu instead of %zu",
616 (unsigned long long)stbuf.st_size, foo1_size);
618 /* Read from fd2. After the swap, the file pointer is past the
620 rc = read(fd2, buf, 1);
621 ASSERTF(rc == 0, "unexpected read returned rc=%d (errno %s)",
622 rc, strerror(errno));
625 ASSERTF(rc == 0, "close failed: %s", strerror(errno));
628 ASSERTF(rc == 0, "close failed: %s", strerror(errno));
631 /* Test multiple swaps between 2 files */
632 static void test30(void)
637 size_t foo1_size = 2000;
638 size_t foo2_size = 5000;
642 rc = mkdir(mainpath, 0);
643 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
644 mainpath, strerror(errno));
646 fd1 = create_file("foo1", foo1_size, 'x');
647 fd2 = create_file("foo2", foo2_size, 'y');
649 for (i = 0; i < 1000; i++) {
650 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, 0);
651 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
654 rc = fstat(fd1, &stbuf);
655 ASSERTF(rc == 0, "stat failed on 'foo1': %s", strerror(errno));
656 ASSERTF(stbuf.st_size == i % 2 ? foo2_size : foo1_size,
657 "invalid size found: %llu instead of %zu",
658 (unsigned long long)stbuf.st_size,
659 i % 2 ? foo2_size : foo1_size);
661 rc = fstat(fd2, &stbuf);
662 ASSERTF(rc == 0, "stat failed on 'foo2': %s", strerror(errno));
663 ASSERTF(stbuf.st_size == i % 2 ? foo1_size : foo2_size,
664 "invalid size found: %llu instead of %zu",
665 (unsigned long long)stbuf.st_size,
666 i % 2 ? foo1_size : foo2_size);
673 /* Test multiple swaps between 3 files */
674 static void test31(void)
680 size_t foo1_size = 2000;
681 size_t foo2_size = 5000;
682 size_t foo3_size = 8000;
687 rc = mkdir(mainpath, 0);
688 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
689 mainpath, strerror(errno));
691 fd1 = create_file("foo1", foo1_size, 'x');
692 fd2 = create_file("foo2", foo2_size, 'y');
693 fd3 = create_file("foo3", foo3_size, 'z');
695 /* Note: swapping 3 fd this way will be back to original
696 * layouts every 2 loops. */
697 for (i = 0; i < 999; i++) {
698 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, 0);
699 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
702 rc = llapi_fswap_layouts(fd2, fd3, 0, 0, 0);
703 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
706 rc = llapi_fswap_layouts(fd1, fd3, 0, 0, 0);
707 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
711 rc = fstat(fd1, &stbuf);
712 ASSERTF(rc == 0, "stat failed on 'foo2': %s", strerror(errno));
713 ASSERTF(stbuf.st_size == foo1_size,
714 "invalid size found: %llu instead of %zu",
715 (unsigned long long)stbuf.st_size, foo1_size);
717 rc = fstat(fd2, &stbuf);
718 ASSERTF(rc == 0, "stat failed on 'foo2': %s", strerror(errno));
719 ASSERTF(stbuf.st_size == foo3_size,
720 "invalid size found: %llu instead of %zu",
721 (unsigned long long)stbuf.st_size, foo3_size);
723 rc = fstat(fd3, &stbuf);
724 ASSERTF(rc == 0, "stat failed on 'foo2': %s", strerror(errno));
725 ASSERTF(stbuf.st_size == foo2_size,
726 "invalid size found: %llu instead of %zu",
727 (unsigned long long)stbuf.st_size, foo2_size);
734 /* Swap with lease */
735 static void test40(void)
740 size_t foo1_size = 2000;
741 size_t foo2_size = 5000;
743 rc = mkdir(mainpath, 0);
744 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
745 mainpath, strerror(errno));
747 fd1 = create_file("foo1", foo1_size, 'x');
748 fd2 = create_file("foo2", foo2_size, 'y');
750 rc = llapi_lease_get(fd1, LL_LEASE_RDLCK);
751 ASSERTF(rc == 0, "cannot get lease '%s': %s", mainpath, strerror(-rc));
753 rc = llapi_lease_check(fd1);
754 ASSERTF(rc == LL_LEASE_RDLCK,
755 "invalid lease type on '%s': %s", mainpath, strerror(-rc));
757 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, SWAP_LAYOUTS_CLOSE);
758 ASSERTF(rc == 0, "llapi_fswap_layouts failed: %s",
761 rc = llapi_lease_check(fd1);
762 ASSERTF(rc == 0, "lease not lost on '%s': %s", mainpath, strerror(-rc));
764 rc = llapi_lease_put(fd1);
765 ASSERTF(rc == -ENOLCK,
766 "was able to put back lease: %s", strerror(-rc));
768 rc = llapi_lease_check(fd1);
769 ASSERTF(rc == 0, "lease not lost on '%s': %s", mainpath, strerror(-rc));
772 ASSERTF(rc == 0, "close failed: %s", strerror(errno));
775 ASSERTF(rc == 0, "close failed: %s", strerror(errno));
778 /* Swap with close but no lease */
779 static void test41(void)
784 size_t foo1_size = 2000;
785 size_t foo2_size = 5000;
787 rc = mkdir(mainpath, 0);
788 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
789 mainpath, strerror(errno));
791 fd1 = create_file("foo1", foo1_size, 'x');
792 fd2 = create_file("foo2", foo2_size, 'y');
794 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, SWAP_LAYOUTS_CLOSE);
795 ASSERTF(rc == -ENOLCK, "llapi_fswap_layouts failed: %s",
798 /* swap failed, so fd1 has to be closed. */
800 ASSERTF(rc == 0, "close failed: %s", strerror(errno));
803 ASSERTF(rc == 0, "close failed: %s", strerror(errno));
806 /* swap with data versions */
807 static void test42(void)
812 size_t foo1_size = 2000;
813 size_t foo2_size = 5000;
818 __u64 new_new_dv1 = 0;
819 __u64 new_new_dv2 = 0;
823 rc = mkdir(mainpath, 0);
824 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
825 mainpath, strerror(errno));
827 /* Get dataversion for two files.
828 * Make sure values are different so that the following checks make
830 fd1 = create_file("foo1", foo1_size, 'x');
832 rc = llapi_get_data_version(fd1, &dv1, LL_DV_RD_FLUSH);
833 ASSERTF(rc == 0, "cannot get dataversion for fd1: %s", strerror(-rc));
834 ASSERTF(dv1 != 0, "got dataversion 0 for fd1");
837 fd2 = create_file("foo2", foo2_size, 'y');
839 rc = llapi_get_data_version(fd2, &dv2, LL_DV_RD_FLUSH);
840 ASSERTF(rc == 0, "cannot get dataversion for fd2: %s",
842 ASSERTF(dv2 != 0, "got dataversion 0 for fd2");
850 /* swaps that should fail */
851 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, SWAP_LAYOUTS_CHECK_DV1);
852 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
854 rc = llapi_fswap_layouts(fd1, fd2, dv1 + 456789, 0,
855 SWAP_LAYOUTS_CHECK_DV1);
856 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
858 rc = llapi_fswap_layouts(fd1, fd2, 0, 0, SWAP_LAYOUTS_CHECK_DV2);
859 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
861 rc = llapi_fswap_layouts(fd1, fd2, 0, dv2 + 987654,
862 SWAP_LAYOUTS_CHECK_DV2);
863 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
865 rc = llapi_fswap_layouts(fd1, fd2, 0, 0,
866 SWAP_LAYOUTS_CHECK_DV1 |
867 SWAP_LAYOUTS_CHECK_DV2);
868 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
870 rc = llapi_fswap_layouts(fd1, fd2, dv1 + 456789, 0,
871 SWAP_LAYOUTS_CHECK_DV1 |
872 SWAP_LAYOUTS_CHECK_DV2);
873 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
875 rc = llapi_fswap_layouts(fd1, fd2, dv1 + 456789, dv2 + 987654,
876 SWAP_LAYOUTS_CHECK_DV1 |
877 SWAP_LAYOUTS_CHECK_DV2);
878 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
880 rc = llapi_fswap_layouts(fd1, fd2, dv1, 0,
881 SWAP_LAYOUTS_CHECK_DV1 |
882 SWAP_LAYOUTS_CHECK_DV2);
883 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
885 rc = llapi_fswap_layouts(fd1, fd2, 0, dv2,
886 SWAP_LAYOUTS_CHECK_DV1 |
887 SWAP_LAYOUTS_CHECK_DV2);
888 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
890 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2 + 567,
891 SWAP_LAYOUTS_CHECK_DV1 |
892 SWAP_LAYOUTS_CHECK_DV2);
893 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
895 printf("DV = %llx and %llx\n", dv1, dv2);
897 /* Finally, a good swap */
898 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2,
899 SWAP_LAYOUTS_CHECK_DV1 |
900 SWAP_LAYOUTS_CHECK_DV2);
901 ASSERTF(rc == 0, "incorrect return from swap: %s", strerror(-rc));
903 /* Check dataversion. */
904 rc = llapi_get_data_version(fd1, &new_dv1, LL_DV_RD_FLUSH);
906 "cannot get new dataversion for fd1: %s", strerror(-rc));
907 ASSERTF(dv1 != 0, "got dataversion 0 for fd1");
908 ASSERTF(dv1 != new_dv1, "got identical dataversion for fd1: %llx", dv1);
910 rc = llapi_get_data_version(fd2, &new_dv2, LL_DV_RD_FLUSH);
912 "cannot get new dataversion for fd2: %s", strerror(-rc));
913 ASSERTF(dv2 != 0, "got dataversion 0 for fd2");
914 ASSERTF(dv2 != new_dv2, "got identical dataversion for fd2: %llx", dv1);
916 printf("new DV = %llx and %llx\n", new_dv1, new_dv2);
918 /* Try again with same parameters. */
919 rc = llapi_fswap_layouts(fd1, fd2, dv1, dv2,
920 SWAP_LAYOUTS_CHECK_DV1 |
921 SWAP_LAYOUTS_CHECK_DV2);
922 ASSERTF(rc == -EAGAIN, "incorrect return from swap: %s", strerror(-rc));
927 /* Reopen the files and check again the dataversion */
928 name_fd1 = create_file_name("foo1");
929 fd1 = open(name_fd1, O_RDONLY);
931 "open failed for '%s': %s", name_fd1, strerror(errno));
933 rc = llapi_get_data_version(fd1, &new_new_dv1, LL_DV_RD_FLUSH);
934 ASSERTF(rc == 0, "cannot get dataversion for fd1: %s", strerror(-rc));
935 ASSERTF(new_new_dv1 != 0, "got dataversion 0 for fd1");
936 ASSERTF(new_dv1 == new_new_dv1,
937 "dataversion changed after re-opening: %llx and %llx",
938 new_dv1, new_new_dv1);
940 name_fd2 = create_file_name("foo2");
941 fd2 = open(name_fd2, O_RDONLY);
943 "open failed for '%s': %s", name_fd2, strerror(errno));
945 rc = llapi_get_data_version(fd2, &new_new_dv2, LL_DV_RD_FLUSH);
946 ASSERTF(rc == 0, "cannot get dataversion for fd2: %s", strerror(-rc));
947 ASSERTF(new_new_dv2 != 0, "got dataversion 0 for fd2");
948 ASSERTF(new_dv2 == new_new_dv2,
949 "dataversion changed after re-opening: %llx and %llx",
950 new_dv2, new_new_dv2);
952 printf("DV= %llx and %llx\n", new_new_dv1, new_new_dv2);
961 /* swap group lock, no group */
962 static void test50(void)
967 size_t foo1_size = 2000;
968 size_t foo2_size = 5000;
970 rc = mkdir(mainpath, 0);
971 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
972 mainpath, strerror(errno));
974 fd1 = create_file("foo1", foo1_size, 'x');
975 fd2 = create_file("foo2", foo2_size, 'y');
977 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, 0, 0);
978 ASSERTF(rc == 0, "llapi_fswap_layouts_grouplock failed: %s",
985 /* swap group lock, with group */
986 static void test51(void)
991 size_t foo1_size = 2000;
992 size_t foo2_size = 5000;
994 rc = mkdir(mainpath, 0);
995 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
996 mainpath, strerror(errno));
998 fd1 = create_file("foo1", foo1_size, 'x');
999 fd2 = create_file("foo2", foo2_size, 'y');
1001 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, 456789, 0);
1002 ASSERTF(rc == 0, "llapi_fswap_layouts_grouplock failed: %s",
1009 /* swap group lock, with existing group locks */
1010 static void test52(void)
1015 size_t foo1_size = 2000;
1016 size_t foo2_size = 5000;
1019 rc = mkdir(mainpath, 0);
1020 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1021 mainpath, strerror(errno));
1023 fd1 = create_file("foo1", foo1_size, 'x');
1024 fd2 = create_file("foo2", foo2_size, 'y');
1026 /* lock a descriptor, but swap without */
1027 rc = llapi_group_lock(fd1, gid);
1028 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1030 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, 0, 0);
1031 ASSERTF(rc == 0, "llapi_fswap_layouts_grouplock failed: %s",
1034 rc = llapi_group_unlock(fd1, gid);
1035 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1041 /* Swap group lock, with existing group locks, on second descriptor */
1042 static void test53(void)
1047 size_t foo1_size = 2000;
1048 size_t foo2_size = 5000;
1051 rc = mkdir(mainpath, 0);
1052 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1053 mainpath, strerror(errno));
1055 fd1 = create_file("foo1", foo1_size, 'x');
1056 fd2 = create_file("foo2", foo2_size, 'y');
1058 /* lock a descriptor, but swap without */
1059 rc = llapi_group_lock(fd2, gid);
1060 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1062 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, 0, 0);
1063 ASSERTF(rc == 0, "llapi_fswap_layouts_grouplock failed: %s",
1066 rc = llapi_group_unlock(fd2, gid);
1067 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1073 /* swap group lock, lock a descriptor, and try to swap with it. */
1074 static void test54(void)
1079 size_t foo1_size = 2000;
1080 size_t foo2_size = 5000;
1083 rc = mkdir(mainpath, 0);
1084 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1085 mainpath, strerror(errno));
1087 fd1 = create_file("foo1", foo1_size, 'x');
1088 fd2 = create_file("foo2", foo2_size, 'y');
1091 rc = llapi_group_lock(fd1, gid);
1092 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1094 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, gid, 0);
1095 ASSERTF(rc == -EINVAL, "llapi_fswap_layouts_grouplock failed: %s",
1098 rc = llapi_group_unlock(fd1, gid);
1099 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1105 /* Swap group lock, lock a descriptor, and try to swap with it, on
1106 * second descriptor. */
1107 static void test55(void)
1112 size_t foo1_size = 2000;
1113 size_t foo2_size = 5000;
1116 rc = mkdir(mainpath, 0);
1117 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1118 mainpath, strerror(errno));
1120 fd1 = create_file("foo1", foo1_size, 'x');
1121 fd2 = create_file("foo2", foo2_size, 'y');
1123 rc = llapi_group_lock(fd2, gid);
1124 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1126 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, gid, 0);
1127 ASSERTF(rc == -EINVAL, "llapi_fswap_layouts_grouplock failed: %s",
1130 rc = llapi_group_unlock(fd2, gid);
1131 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1137 /* Swap group lock, lock a descriptor, and try to swap with another
1139 static void test56(void)
1144 size_t foo1_size = 2000;
1145 size_t foo2_size = 5000;
1149 rc = mkdir(mainpath, 0);
1150 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1151 mainpath, strerror(errno));
1153 fd1 = create_file("foo1", foo1_size, 'x');
1154 fd2 = create_file("foo2", foo2_size, 'y');
1156 rc = llapi_group_lock(fd1, gid1);
1157 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1159 rc = llapi_group_lock(fd2, gid2);
1160 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1162 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, 0, 0);
1163 ASSERTF(rc == 0, "llapi_fswap_layouts_grouplock failed: %s",
1166 rc = llapi_group_unlock(fd1, gid1);
1167 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1169 rc = llapi_group_unlock(fd2, gid2);
1170 ASSERTF(rc == 0, "cannot unlock 'foo2': %s", strerror(-rc));
1176 /* Swap group lock, lock both descriptor, and try to swap with another
1178 static void test57(void)
1183 size_t foo1_size = 2000;
1184 size_t foo2_size = 5000;
1188 rc = mkdir(mainpath, 0);
1189 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1190 mainpath, strerror(errno));
1192 fd1 = create_file("foo1", foo1_size, 'x');
1193 fd2 = create_file("foo2", foo2_size, 'y');
1195 rc = llapi_group_lock(fd1, gid1);
1196 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1198 rc = llapi_group_lock(fd2, gid2);
1199 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1201 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, gid1+gid2, 0);
1202 ASSERTF(rc == -EINVAL, "llapi_fswap_layouts_grouplock failed: %s",
1205 rc = llapi_group_unlock(fd1, gid1);
1206 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1208 rc = llapi_group_unlock(fd2, gid2);
1209 ASSERTF(rc == 0, "cannot unlock 'foo2': %s", strerror(-rc));
1215 /* Swap group lock, lock both descriptor with same gid, and try to
1217 static void test58(void)
1222 size_t foo1_size = 2000;
1223 size_t foo2_size = 5000;
1226 rc = mkdir(mainpath, 0);
1227 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1228 mainpath, strerror(errno));
1230 fd1 = create_file("foo1", foo1_size, 'x');
1231 fd2 = create_file("foo2", foo2_size, 'y');
1233 rc = llapi_group_lock(fd1, gid);
1234 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1236 rc = llapi_group_lock(fd2, gid);
1237 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1239 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, gid, 0);
1240 ASSERTF(rc == -EINVAL, "llapi_fswap_layouts_grouplock failed: %s",
1243 rc = llapi_group_unlock(fd1, gid);
1244 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1246 rc = llapi_group_unlock(fd2, gid);
1247 ASSERTF(rc == 0, "cannot unlock 'foo2': %s", strerror(-rc));
1253 /* Swap group lock, lock both descriptor with same gid, and swap with
1255 static void test59(void)
1260 size_t foo1_size = 2000;
1261 size_t foo2_size = 5000;
1264 rc = mkdir(mainpath, 0);
1265 ASSERTF(rc == 0, "mkdir failed for '%s': %s",
1266 mainpath, strerror(errno));
1268 fd1 = create_file("foo1", foo1_size, 'x');
1269 fd2 = create_file("foo2", foo2_size, 'y');
1271 rc = llapi_group_lock(fd1, gid);
1272 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1274 rc = llapi_group_lock(fd2, gid);
1275 ASSERTF(rc == 0, "cannot lock 'foo1': %s", strerror(-rc));
1277 rc = llapi_fswap_layouts_grouplock(fd1, fd2, 0, 0, 0, 0);
1278 ASSERTF(rc == 0, "llapi_fswap_layouts_grouplock failed: %s",
1281 rc = llapi_group_unlock(fd1, gid);
1282 ASSERTF(rc == 0, "cannot unlock 'foo1': %s", strerror(-rc));
1284 rc = llapi_group_unlock(fd2, gid);
1285 ASSERTF(rc == 0, "cannot unlock 'foo2': %s", strerror(-rc));
1291 static void usage(char *prog)
1293 fprintf(stderr, "Usage: %s [-d lustre_dir]\n", prog);
1297 static void process_args(int argc, char *argv[])
1301 while ((c = getopt(argc, argv, "d:")) != -1) {
1304 lustre_dir = optarg;
1308 fprintf(stderr, "Unknown option '%c'\n", optopt);
1315 int main(int argc, char *argv[])
1320 process_args(argc, argv);
1321 if (lustre_dir == NULL)
1322 lustre_dir = "/mnt/lustre";
1324 rc = llapi_search_mounts(lustre_dir, 0, fsmountdir, fsname);
1326 fprintf(stderr, "Error: '%s': not a Lustre filesystem\n",
1328 return EXIT_FAILURE;
1331 /* Play nice with Lustre test scripts. Non-line buffered output
1332 * stream under I/O redirection may appear incorrectly. */
1333 setvbuf(stdout, NULL, _IOLBF, 0);
1335 /* Create a test filename and reuse it. Remove possibly old files. */
1336 rc = snprintf(mainpath, sizeof(mainpath), "%s/%s", lustre_dir, maindir);
1337 ASSERTF(rc > 0 && rc < sizeof(mainpath), "invalid name for mainpath");
1366 return EXIT_SUCCESS;