Whamcloud - gitweb
This has a kernel patch for handle validation. All that is missing
[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 #ifndef O_DIRECT
11 #define O_DIRECT         040000 /* direct disk access hint */
12 #endif
13
14 #define BLOCKSIZE 4096
15
16 int main(int argc, char **argv)
17 {
18         int fd;
19         char *buf;
20         int pages;
21         int rc;
22
23         if (argc != 3) {
24                 printf("Usage: %s file nr_pages\n", argv[0]);
25                 return 1;
26         }
27
28         pages = strtoul(argv[2], 0, 0);
29         printf("directio on %s for %d pages \n", argv[1], pages);
30
31         buf = mmap(0, pages * BLOCKSIZE, PROT_READ|PROT_WRITE,
32                    MAP_PRIVATE|MAP_ANON, 0, 0);
33         if (!buf) {
34                 printf("No memory %s\n", strerror(errno));
35                 return 1;
36         }
37
38         fd = open(argv[1], O_DIRECT | O_RDWR | O_CREAT);
39         if (fd == -1) {
40                 printf("Cannot open %s:  %s\n", argv[1], strerror(errno));
41                 return 1;
42         }
43
44         rc = read(fd, buf, pages * BLOCKSIZE);
45         if (rc != pages * BLOCKSIZE) {
46                 printf("Read error: %s, rc %d\n", strerror(errno), rc);
47                 return 1;
48         }
49
50         if ( lseek(fd, 0, SEEK_SET) != 0 ) {
51                 printf("Cannot seek %s\n", strerror(errno));
52                 return 1;
53         }
54
55         rc = write(fd, buf, pages * BLOCKSIZE);
56         if (rc != pages * BLOCKSIZE) {
57                 printf("Write error %s\n", strerror(errno));
58                 return 1;
59         }
60
61         return 0;
62 }