Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[fs/lustre-release.git] / lustre / tests / directio.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <sys/mman.h>
10
11 // not correctly in the headers yet!!
12 #ifndef O_DIRECT
13 #define O_DIRECT         040000 /* direct disk access hint */
14 #endif
15
16 int main(int argc, char **argv)
17 {
18         int fd;
19         char *buf;
20         int blocks, seek_blocks;
21         long len;
22         off64_t seek;
23         struct stat64 st;
24         int rc;
25
26         if (argc != 4) {
27                 printf("Usage: %s file seek nr_blocks\n", argv[0]);
28                 return 1;
29         }
30
31         seek_blocks = strtoul(argv[2], 0, 0);
32         blocks = strtoul(argv[3], 0, 0);
33         fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
34         if (fd == -1) {
35                 printf("Cannot open %s:  %s\n", argv[1], strerror(errno));
36                 return 1;
37         }
38
39         if (fstat64(fd, &st) < 0) {
40                 printf("Cannot stat %s:  %s\n", argv[1], strerror(errno));
41                 return 1;
42         }
43
44         printf("directio on %s for %dx%lu bytes \n", argv[1], blocks,
45                st.st_blksize);
46
47         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
48         if (lseek64(fd, seek, SEEK_SET) < 0) {
49                 printf("lseek64 failed: %s\n", strerror(errno));
50                 return 1;
51         }
52
53         len = blocks * st.st_blksize;
54         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
55         if (!buf) {
56                 printf("No memory %s\n", strerror(errno));
57                 return 1;
58         }
59
60         memset(buf, 0xba, len);
61         rc = write(fd, buf, len);
62         if (rc != len) {
63                 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
64                 return 1;
65         }
66
67         if (lseek64(fd, seek, SEEK_SET) < 0) {
68                 printf("Cannot seek %s\n", strerror(errno));
69                 return 1;
70         }
71
72         rc = read(fd, buf, len);
73         if (rc != len) {
74                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
75                 return 1;
76         }
77
78         printf("PASS\n");
79         return 0;
80 }