Whamcloud - gitweb
- test programs for directio, writing and opening
[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/mman.h>
8
9 // not correctly in the headers yet!!
10 #define O_DIRECT         040000 /* direct disk access hint */
11
12 int main(int argc, char **argv)
13 {
14         int fd;
15         char *buf;
16         int pages;
17         int rc; 
18
19         if (argc != 3) { 
20                 printf("Usage: %s file nr_pages\n", argv[0]);
21                 return 1;
22         }
23
24         pages = strtoul(argv[2], 0, 0);
25         printf("directio on %s for %d pages \n", argv[1], pages);
26
27         buf = mmap(0, pages * 4096, PROT_READ|PROT_WRITE, 
28                    MAP_PRIVATE|MAP_ANON, 0, 0);
29         if (!buf) { 
30                 printf("No memory %s\n", strerror(errno));
31                 return 1;
32         }
33
34         fd = open(argv[1], O_DIRECT | O_RDWR | O_CREAT);
35         if (fd == -1) { 
36                 printf("Cannot open %s:  %s\n", argv[1], strerror(errno));
37                 return 1;
38         }
39
40         rc = read(fd, buf, pages * 4096); 
41         if (rc != pages * 4096) { 
42                 printf("Read error: %s, rc %d\n", strerror(errno), rc);
43                 return 1;
44         }
45
46         if ( lseek(fd, 0, SEEK_SET) != 0 ) { 
47                 printf("Cannot seek %s\n", strerror(errno));
48                 return 1;
49         }
50
51         rc = write(fd, buf, pages * 4096); 
52         if (rc != pages * 4096) { 
53                 printf("Write error %s\n", strerror(errno));
54                 return 1;
55         }
56
57         return 0;
58 }