Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[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 write_buffer(char *fname, char *buffer, int len)
16 {
17         int fd, rc;
18
19         fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
20         if (fd == -1) {
21                 printf("Cannot open %s:  %s\n", fname, strerror(errno));
22                 exit(1);
23         }
24         rc = write(fd, buffer, len);
25         if (rc != len) {
26                 printf("write: %d\n", rc);
27                 exit(1);
28         }
29         close(fd);
30         return 0;
31 }
32
33 void verify(char *buffer, char *compare, int length)
34 {
35         int i;
36         for (i = 0; i < length; i++) {
37                 if (buffer[i] != compare[i]) {
38                         fprintf(stderr, "garbage read (i=%d): expected %c, found %c\n",
39                                i, compare[i], buffer[i]);
40                         write_buffer("/tmp/dio1", buffer, length);
41                         write_buffer("/tmp/dio2", compare, length);
42                         exit(1);
43                 }
44         }
45 }
46
47
48 int main(int argc, char **argv)
49 {
50         int fd;
51         char *rbuf, *wbuf;
52         int blocks, seek_blocks;
53         long len;
54         off64_t seek;
55         struct stat64 st;
56         int rc;
57
58         if (argc < 4 || argc > 5) {
59                 printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]);
60                 return 1;
61         }
62
63         seek_blocks = strtoul(argv[2], 0, 0);
64         blocks = strtoul(argv[3], 0, 0);
65
66         fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
67         if (fd == -1) {
68                 printf("Cannot open %s:  %s\n", argv[1], strerror(errno));
69                 return 1;
70         }
71
72         if (argc == 5)
73                 st.st_blksize = strtoul(argv[4], 0, 0);
74         else if (fstat64(fd, &st) < 0) {
75                 printf("Cannot stat %s:  %s\n", argv[1], strerror(errno));
76                 return 1;
77         }
78
79         fprintf(stderr, "directio on %s for %dx%lu bytes \n", argv[1], blocks,
80                 st.st_blksize);
81
82         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
83 #if 0
84         if (lseek64(fd, seek, SEEK_SET) < 0) {
85                 printf("lseek64 failed: %s\n", strerror(errno));
86                 return 1;
87         }
88 #endif
89         len = blocks * st.st_blksize;
90         wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
91         if (wbuf == MAP_FAILED) {
92                 printf("No memory %s\n", strerror(errno));
93                 return 1;
94         }
95
96         rbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
97         if (rbuf == MAP_FAILED) {
98                 printf("No memory %s\n", strerror(errno));
99                 return 1;
100         }
101
102         memset(wbuf, 0xba, len);
103         rc = write(fd, wbuf, len);
104         if (rc != len) {
105                 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
106                 return 1;
107         }
108
109         if (lseek64(fd, seek, SEEK_SET) < 0) {
110                 printf("Cannot seek %s\n", strerror(errno));
111                 return 1;
112         }
113
114         rc = read(fd, rbuf, len);
115         if (rc != len) {
116                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
117                 return 1;
118         }
119
120         verify(rbuf, wbuf, len);
121         if (memcmp(wbuf, rbuf, len)) {
122                 printf("Data mismatch on line %d\n", __LINE__);
123                 return 1;
124         }
125
126         /* try 512-byte buffers, and make sure that the other parts of the
127          * page aren't modified. */
128         if (st.st_blksize < 4096) {
129                 printf("512-byte block size tests skipped (because blocksize "
130                        "passed is < 4k)\n");
131                 printf("PASS\n");
132                 return 0;
133         }
134
135
136
137         /* write test */
138         if (lseek64(fd, 512, SEEK_SET) < 0) {
139                 printf("Cannot seek %s\n", strerror(errno));
140                 return 1;
141         }
142
143         memset(wbuf, 0x44, len);
144         memset(wbuf + 2048, 0x69, 512);
145         rc = write(fd, wbuf + 2048, 512);
146         if (rc != 512) {
147                 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
148                 return 1;
149         }
150
151         memset(rbuf, 0x44, len);
152         memset(rbuf + 2048, 0x69, 512);
153         if (memcmp(wbuf, rbuf, len)) {
154                 printf("Data mismatch on line %d\n", __LINE__);
155                 return 1;
156         }
157
158         /* read test */
159         if (lseek64(fd, 512, SEEK_SET) < 0) {
160                 printf("Cannot seek %s\n", strerror(errno));
161                 return 1;
162         }
163         memset(rbuf, 0xba, len);
164         rc = read(fd, rbuf + 1024, 512);
165         if (rc != 512) {
166                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
167                 return 1;
168         }
169
170         memset(wbuf, 0xba, len);
171         memset(wbuf + 1024, 0x69, 512);
172
173         verify(rbuf, wbuf, len);
174 #if 0
175         if (memcmp(wbuf, rbuf, len)) {
176                 printf("Data mismatch on line %d\n", __LINE__);
177                 return 1;
178         }
179 #endif
180
181         /* read back the whole block, to see that it's untouched. */
182         if (lseek64(fd, seek, SEEK_SET) < 0) {
183                 printf("Cannot seek %s\n", strerror(errno));
184                 return 1;
185         }
186
187         memset(rbuf, 0x1, len);
188         rc = read(fd, rbuf, len);
189         if (rc != len) {
190                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
191                 return 1;
192         }
193
194         memset(wbuf, 0xba, len);
195         memset(wbuf + 512, 0x69, 512);
196         if (memcmp(wbuf, rbuf, len)) {
197                 printf("Data mismatch on line %d\n", __LINE__);
198                 return 1;
199         }
200
201         printf("PASS\n");
202         return 0;
203 }