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 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #include <sys/types.h>
47 #include <libcfs/util/param.h>
49 char *dir = NULL, *dir2 = NULL;
51 char mmap_sanity[256];
53 static void usage(void)
55 printf("Usage: mmap_sanity -d dir [-m dir2] [-e <test cases>]\n");
56 printf(" -d dir lustre mount point\n");
57 printf(" -m dir2 another mount point\n");
58 printf(" -e testcases skipped test cases, -e 1 -e 2 to exclude test cases 1 and 2.\n");
62 static int remote_tst(int tc, char *mnt);
63 static int mmap_run(int tc)
75 rc = remote_tst(tc, dir2);
78 fprintf(stderr, "invalid argument!\n");
83 static int mmap_initialize(char *myself)
85 char buf[1024], *file;
86 int fdr, fdw, count, rc = 0;
88 page_size = sysconf(_SC_PAGESIZE);
89 if (page_size == -1) {
90 perror("sysconf(_SC_PAGESIZE)");
94 /* copy myself to lustre for another client */
95 fdr = open(myself, O_RDONLY);
100 file = strrchr(myself, '/');
102 fprintf(stderr, "can't get test filename\n");
107 sprintf(mmap_sanity, "%s/%s", dir, file);
109 fdw = open(mmap_sanity, O_CREAT | O_WRONLY, 0777);
115 while ((count = read(fdr, buf, sizeof(buf))) != 0) {
123 writes = write(fdw, buf, count);
124 if (writes != count) {
135 static void mmap_finalize()
140 /* basic mmap operation on single node */
141 static int mmap_tst1(char *mnt)
143 char *ptr, mmap_file[256];
144 int region, fd, rc = 0;
146 region = page_size * 10;
147 sprintf(mmap_file, "%s/%s", mnt, "mmap_file1");
149 if (unlink(mmap_file) && errno != ENOENT) {
154 fd = open(mmap_file, O_CREAT | O_RDWR, 0600);
159 if (ftruncate(fd, region) < 0) {
160 perror("ftruncate()");
165 ptr = mmap(NULL, region, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
166 if (ptr == MAP_FAILED) {
171 memset(ptr, 'a', region);
180 /* MAP_PRIVATE create a copy-on-write mmap */
181 static int mmap_tst2(char *mnt)
183 char *ptr, mmap_file[256], buf[256];
186 sprintf(mmap_file, "%s/%s", mnt, "mmap_file2");
188 if (unlink(mmap_file) && errno != ENOENT) {
193 fd = open(mmap_file, O_CREAT | O_RDWR, 0600);
198 if (ftruncate(fd, page_size) < 0) {
199 perror("ftruncate()");
204 ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
205 if (ptr == MAP_FAILED) {
210 memcpy(ptr, "blah", strlen("blah"));
212 munmap(ptr, page_size);
218 fd = open(mmap_file, O_RDONLY);
223 rc = read(fd, buf, sizeof(buf));
231 if (strncmp("blah", buf, strlen("blah")) == 0) {
232 fprintf(stderr, "mmap write back with MAP_PRIVATE!\n");
240 /* concurrent mmap operations on two nodes */
241 static int mmap_tst3(char *mnt)
243 char *ptr, mmap_file[256];
244 int region, fd, rc = 0;
246 region = page_size * 100;
247 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
249 if (unlink(mmap_file) && errno != ENOENT) {
254 fd = open(mmap_file, O_CREAT | O_RDWR, 0600);
259 if (ftruncate(fd, region) < 0) {
260 perror("ftruncate()");
265 ptr = mmap(NULL, region, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
266 if (ptr == MAP_FAILED) {
276 memset(ptr, 'a', region);
277 sleep(2); /* wait for remote test finish */
286 static int remote_tst3(char *mnt)
288 char *ptr, mmap_file[256];
289 int region, fd, rc = 0;
291 region = page_size * 100;
292 sprintf(mmap_file, "%s/%s", mnt, "mmap_file3");
294 fd = open(mmap_file, O_RDWR, 0600);
300 ptr = mmap(NULL, region, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
301 if (ptr == MAP_FAILED) {
306 memset(ptr, 'b', region);
307 memset(ptr, 'c', region);
316 * client1 write to file_4a from mmap()ed file_4b;
317 * client2 write to file_4b from mmap()ed file_4a.
319 static int mmap_tst4(char *mnt)
321 char *ptr, filea[256], fileb[256];
322 int region, fdr, fdw, rc = 0;
324 region = page_size * 100;
325 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
326 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
328 if (unlink(filea) && errno != ENOENT) {
332 if (unlink(fileb) && errno != ENOENT) {
338 fdr = open(fileb, O_CREAT | O_RDWR, 0600);
343 if (ftruncate(fdr, region) < 0) {
344 perror("ftruncate()");
348 fdw = open(filea, O_CREAT | O_RDWR, 0600);
354 if (ftruncate(fdw, region) < 0) {
355 perror("ftruncate()");
360 ptr = mmap(NULL, region, PROT_READ | PROT_WRITE, MAP_SHARED, fdr, 0);
361 if (ptr == MAP_FAILED) {
371 memset(ptr, '1', region);
373 rc = write(fdw, ptr, region);
381 sleep(2); /* wait for remote test finish */
394 static int remote_tst4(char *mnt)
396 char *ptr, filea[256], fileb[256];
397 int region, fdr, fdw, rc = 0;
399 region = page_size * 100;
400 sprintf(filea, "%s/%s", mnt, "mmap_file_4a");
401 sprintf(fileb, "%s/%s", mnt, "mmap_file_4b");
404 fdr = open(filea, O_RDWR, 0600);
409 fdw = open(fileb, O_RDWR, 0600);
416 ptr = mmap(NULL, region, PROT_READ | PROT_WRITE, MAP_SHARED, fdr, 0);
417 if (ptr == MAP_FAILED) {
423 memset(ptr, '2', region);
425 rc = write(fdw, ptr, region);
442 static int cancel_lru_locks(char *filter)
454 rc = waitpid(child, &status, WNOHANG);
461 rc = cfs_get_param_paths(&paths,
462 "ldlm/namespaces/*-%s-*/lru_size",
465 rc = cfs_get_param_paths(&paths,
466 "ldlm/namespaces/*/lru_size");
470 for (i = 0; i < paths.gl_pathc; i++) {
471 FILE *f = fopen(paths.gl_pathv[i], "r");
475 fprintf(stderr, "cannot open '%s': %s\n",
476 paths.gl_pathv[i], strerror(errno));
480 rc = fwrite("clear", strlen("clear") + 1, 1, f);
483 fprintf(stderr, "fwrite failed for '%s': %s\n",
484 paths.gl_pathv[i], strerror(errno));
491 cfs_free_param_data(&paths);
496 * don't dead lock while read/write file to/from the buffer which
497 * mmaped to just this file
499 static int mmap_tst5(char *mnt)
501 char *ptr, mmap_file[256];
502 int region, fd, off, rc = 0;
504 region = page_size * 40;
505 off = page_size * 10;
506 sprintf(mmap_file, "%s/%s", mnt, "mmap_file5");
508 if (unlink(mmap_file) && errno != ENOENT) {
513 fd = open(mmap_file, O_CREAT | O_RDWR, 0600);
518 if (ftruncate(fd, region) < 0) {
519 perror("ftruncate()");
524 ptr = mmap(NULL, region, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
525 if (ptr == MAP_FAILED) {
530 memset(ptr, 'a', region);
532 /* cancel unused locks */
533 rc = cancel_lru_locks("osc");
537 /* read/write region of file and buffer should be overlap */
538 rc = read(fd, ptr + off, off * 2);
544 rc = write(fd, ptr + off, off * 2);
558 /* mmap write to a file form client1 then mmap read from client2 */
559 static int mmap_tst6(char *mnt)
561 char mmap_file[256], mmap_file2[256];
562 char *ptr = NULL, *ptr2 = NULL;
563 int fd = 0, fd2 = 0, rc = 0;
565 sprintf(mmap_file, "%s/%s", mnt, "mmap_file6");
566 sprintf(mmap_file2, "%s/%s", dir2, "mmap_file6");
567 if (unlink(mmap_file) && errno != ENOENT) {
572 fd = open(mmap_file, O_CREAT | O_RDWR, 0600);
577 if (ftruncate(fd, page_size) < 0) {
578 perror("ftruncate()");
583 fd2 = open(mmap_file2, O_RDWR, 0600);
590 ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
591 if (ptr == MAP_FAILED) {
597 ptr2 = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
599 if (ptr2 == MAP_FAILED) {
605 rc = cancel_lru_locks("osc");
609 memcpy(ptr, "blah", strlen("blah"));
610 if (strncmp(ptr, ptr2, strlen("blah"))) {
611 fprintf(stderr, "client2 mmap mismatch!\n");
615 memcpy(ptr2, "foo", strlen("foo"));
616 if (strncmp(ptr, ptr2, strlen("foo"))) {
617 fprintf(stderr, "client1 mmap mismatch!\n");
622 munmap(ptr2, page_size);
624 munmap(ptr, page_size);
633 static int mmap_tst7_func(char *mnt, int rw)
636 char *buf = MAP_FAILED;
641 if (snprintf(fname, 256, "%s/mmap_tst7.%s", mnt,
642 (rw == 0) ? "read" : "write") >= 256) {
643 fprintf(stderr, "dir name too long\n");
647 fd = open(fname, O_RDWR | O_DIRECT | O_CREAT, 0644);
653 if (ftruncate(fd, 2 * page_size) == -1) {
658 buf = mmap(NULL, page_size * 2,
659 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
660 if (buf == MAP_FAILED) {
665 /* ensure the second page isn't mapped */
666 munmap(buf + page_size, page_size);
667 bytes = (rw == 0) ? read(fd, buf, 2 * page_size) :
668 write(fd, buf, 2 * page_size);
669 /* Expected behavior */
670 if (bytes == page_size)
673 fprintf(stderr, "%s returned %zd, errno = %d\n",
674 (rw == 0) ? "read" : "write", bytes, errno);
677 if (buf != MAP_FAILED)
678 munmap(buf, page_size);
684 static int mmap_tst7(char *mnt)
688 rc = mmap_tst7_func(mnt, 0);
691 rc = mmap_tst7_func(mnt, 1);
695 static int mmap_tst8(char *mnt)
698 char *buf = MAP_FAILED;
702 char xyz[page_size * 2];
704 if (snprintf(fname, 256, "%s/mmap_tst8", mnt) >= 256) {
705 fprintf(stderr, "dir name too long\n");
709 fd = open(fname, O_RDWR | O_CREAT, 0644);
715 if (ftruncate(fd, page_size) == -1) {
720 buf = mmap(NULL, page_size * 2,
721 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
722 if (buf == MAP_FAILED) {
729 if (pid == 0) { /* child */
730 memcpy(xyz, buf, page_size * 2);
731 /* shouldn't reach here. */
733 } else if (pid > 0) { /* parent */
736 pid = waitpid(pid, &status, 0);
744 if (WIFSIGNALED(status) && SIGBUS == WTERMSIG(status))
752 if (buf != MAP_FAILED)
753 munmap(buf, page_size);
759 static int mmap_tst9(char *mnt)
762 char *buf = MAP_FAILED;
766 if (snprintf(fname, 256, "%s/mmap_tst9", mnt) >= 256) {
767 fprintf(stderr, "dir name too long\n");
771 if (unlink(fname) == -1 && errno != ENOENT) {
776 fd = open(fname, O_RDWR | O_CREAT, 0644);
782 buf = mmap(NULL, page_size * 2,
783 PROT_READ, MAP_PRIVATE, fd, (loff_t)(-10 * page_size));
784 if (buf == MAP_FAILED) {
789 rc = write(STDOUT_FILENO, buf, 2 * page_size);
791 fprintf(stderr, "write succeded with %d instead of failing\n",
795 } else if (errno != EFAULT) {
796 fprintf(stderr, "write failed with %d instead of EFAULT(%d)\n",
803 if (buf != MAP_FAILED)
804 munmap(buf, page_size * 2);
810 static int remote_tst(int tc, char *mnt)
816 rc = remote_tst3(mnt);
819 rc = remote_tst4(mnt);
822 fprintf(stderr, "wrong test case number %d\n", tc);
830 int tc; /* test case number */
831 char *desc; /* test description */
832 int (*test_fn)(char *mnt); /* test function */
833 int node_cnt; /* node count */
834 int skipped; /* skipped by caller */
837 struct test_case tests[] = {
840 .desc = "mmap test1: basic mmap operation",
841 .test_fn = mmap_tst1,
846 .desc = "mmap test2: MAP_PRIVATE not write back",
847 .test_fn = mmap_tst2,
852 .desc = "mmap test3: concurrent mmap ops on two nodes",
853 .test_fn = mmap_tst3,
858 .desc = "mmap test4: c1 write to f1 from mmapped f2, c2 write to f1 from mmapped f1",
859 .test_fn = mmap_tst4,
864 .desc = "mmap test5: read/write file to/from the buffer which mmapped to just this file",
865 .test_fn = mmap_tst5,
870 .desc = "mmap test6: check mmap write/read content on two nodes",
871 .test_fn = mmap_tst6,
876 .desc = "mmap test7: file i/o with an unmapped buffer",
877 .test_fn = mmap_tst7,
882 .desc = "mmap test8: SIGBUS for beyond file size",
883 .test_fn = mmap_tst8,
888 .desc = "mmap test9: SIGBUS for negative file offset",
889 .test_fn = mmap_tst9,
897 int main(int argc, char **argv)
899 struct test_case *test;
900 int nr_cases = sizeof(tests) / sizeof(*test);
903 while ((c = getopt(argc, argv, "d:m:e:")) != -1) {
914 rc = strtol(optarg, &endptr, 10);
915 if (endptr && *endptr != '\0')
917 if (rc > 0 && rc < nr_cases)
918 tests[rc - 1].skipped = 1;
930 if (mmap_initialize(argv[0]) != 0) {
931 fprintf(stderr, "mmap_initialize failed!\n");
936 for (test = tests; test->tc; test++) {
937 double duration = 0.0;
938 char *rs = "SKIPPED";
940 if (!test->skipped && (test->node_cnt == 1 || dir2)) {
941 struct timeval start, end;
943 gettimeofday(&start, NULL);
944 rc = test->test_fn(dir);
945 gettimeofday(&end, NULL);
947 duration = (double)(end.tv_sec - start.tv_sec) +
948 (double)(end.tv_usec - start.tv_usec) / 1000000;
949 rs = rc ? "FAIL" : "PASS";
952 fprintf(stderr, "%s (%s, %.5gs)\n", test->desc, rs, duration);