Whamcloud - gitweb
b=11564
[fs/lustre-release.git] / lustre / tests / directio.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #define  _GNU_SOURCE
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/mman.h>
14
15 int main(int argc, char **argv)
16 {
17 #ifdef O_DIRECT
18         int fd;
19         char *wbuf, *fname;
20         int blocks, seek_blocks;
21         long len;
22         off64_t seek;
23         struct stat64 st;
24         int action;
25         int rc;
26
27         if (argc < 5 || argc > 6) {
28                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
29                 return 1;
30         }
31
32         if (!strcmp(argv[1], "read"))
33                 action = O_RDONLY;
34         else if (!strcmp(argv[1], "write"))
35                 action = O_WRONLY;
36         else if (!strcmp(argv[1], "rdwr"))
37                 action = O_RDWR;
38         else {
39                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
40                 return 1;
41         }
42
43         fname = argv[2];
44         seek_blocks = strtoul(argv[3], 0, 0);
45         blocks = strtoul(argv[4], 0, 0);
46         if (!blocks) {
47                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
48                 return 1;
49         }
50
51         fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
52         if (fd == -1) {
53                 printf("Cannot open %s:  %s\n", fname, strerror(errno));
54                 return 1;
55         }
56
57         if (argc >= 6)
58                 st.st_blksize = strtoul(argv[5], 0, 0);
59         else if (fstat64(fd, &st) < 0) {
60                 printf("Cannot stat %s:  %s\n", fname, strerror(errno));
61                 return 1;
62         }
63
64         printf("directio on %s for %dx%lu bytes \n", fname, blocks,
65                st.st_blksize);
66
67         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
68         len = blocks * st.st_blksize;
69
70         wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
71         if (wbuf == MAP_FAILED) {
72                 printf("No memory %s\n", strerror(errno));
73                 return 1;
74         }
75         memset(wbuf, 0xba, len);
76
77         if (action == O_WRONLY || action == O_RDWR) {
78                 if (lseek64(fd, seek, SEEK_SET) < 0) {
79                         printf("lseek64 failed: %s\n", strerror(errno));
80                         return 1;
81                 }
82
83                 rc = write(fd, wbuf, len);
84                 if (rc != len) {
85                         printf("Write error %s (rc = %d, len = %ld)\n",
86                                strerror(errno), rc, len);
87                         return 1;
88                 }
89         }
90
91         if (action == O_RDONLY || action == O_RDWR) {
92                 char *rbuf;
93
94                 if (lseek64(fd, seek, SEEK_SET) < 0) {
95                         printf("Cannot seek %s\n", strerror(errno));
96                         return 1;
97                 }
98
99                 rbuf =mmap(0,len,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,0,0);
100                 if (rbuf == MAP_FAILED) {
101                         printf("No memory %s\n", strerror(errno));
102                         return 1;
103                 }
104
105                 rc = read(fd, rbuf, len);
106                 if (rc != len) {
107                         printf("Read error: %s (rc = %d)\n",strerror(errno),rc);
108                         return 1;
109                 }
110
111                 if (memcmp(wbuf, rbuf, len)) {
112                         printf("Data mismatch\n");
113                         return 1;
114                 }
115         }
116
117         printf("PASS\n");
118         return 0;
119 #else /* !O_DIRECT */
120 #warning O_DIRECT not defined, directio test will fail
121         printf("O_DIRECT not defined\n");
122         return 1;
123 #endif /* !O_DIRECT */
124 }