Whamcloud - gitweb
f529fb08ea710eb5e2bba09d1777b1f553d1ad15
[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;
21         long len;
22         struct stat st;
23         int rc;
24
25         if (argc != 3) {
26                 printf("Usage: %s file nr_blocks\n", argv[0]);
27                 return 1;
28         }
29
30         blocks = strtoul(argv[2], 0, 0);
31         fd = open(argv[1], O_DIRECT | O_RDWR | O_CREAT, 0644);
32         if (fd == -1) {
33                 printf("Cannot open %s:  %s\n", argv[1], strerror(errno));
34                 return 1;
35         }
36
37         if (fstat(fd, &st) < 0) {
38                 printf("Cannot stat %s:  %s\n", argv[1], strerror(errno));
39                 return 1;
40         }
41
42         printf("directio on %s for %dx%lu blocks \n", argv[1], blocks,
43                st.st_blksize);
44
45         len = blocks * st.st_blksize;
46         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
47         if (!buf) {
48                 printf("No memory %s\n", strerror(errno));
49                 return 1;
50         }
51
52         memset(buf, 0xba, len);
53         rc = write(fd, buf, len);
54         if (rc != len) {
55                 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
56                 return 1;
57         }
58
59         if (lseek(fd, 0, SEEK_SET) != 0) {
60                 printf("Cannot seek %s\n", strerror(errno));
61                 return 1;
62         }
63
64         rc = read(fd, buf, len);
65         if (rc != len) {
66                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
67                 return 1;
68         }
69
70         return 0;
71 }