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