1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
11 #include <sys/types.h>
15 int write_buffer(char *fname, char *buffer, int len)
19 fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
21 printf("Cannot open %s: %s\n", fname, strerror(errno));
24 rc = write(fd, buffer, len);
26 printf("write: %d\n", rc);
33 void verify(char *buffer, char *compare, int length)
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);
48 int main(int argc, char **argv)
52 int blocks, seek_blocks;
58 if (argc < 4 || argc > 5) {
59 printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]);
63 seek_blocks = strtoul(argv[2], 0, 0);
64 blocks = strtoul(argv[3], 0, 0);
66 fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
68 printf("Cannot open %s: %s\n", argv[1], strerror(errno));
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));
79 fprintf(stderr, "directio on %s for %dx%lu bytes \n", argv[1], blocks,
82 seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
84 if (lseek64(fd, seek, SEEK_SET) < 0) {
85 printf("lseek64 failed: %s\n", strerror(errno));
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));
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));
102 memset(wbuf, 0xba, len);
103 rc = write(fd, wbuf, len);
105 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
109 if (lseek64(fd, seek, SEEK_SET) < 0) {
110 printf("Cannot seek %s\n", strerror(errno));
114 rc = read(fd, rbuf, len);
116 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
120 verify(rbuf, wbuf, len);
121 if (memcmp(wbuf, rbuf, len)) {
122 printf("Data mismatch on line %d\n", __LINE__);
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");
138 if (lseek64(fd, 512, SEEK_SET) < 0) {
139 printf("Cannot seek %s\n", strerror(errno));
143 memset(wbuf, 0x44, len);
144 memset(wbuf + 2048, 0x69, 512);
145 rc = write(fd, wbuf + 2048, 512);
147 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
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__);
159 if (lseek64(fd, 512, SEEK_SET) < 0) {
160 printf("Cannot seek %s\n", strerror(errno));
163 memset(rbuf, 0xba, len);
164 rc = read(fd, rbuf + 1024, 512);
166 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
170 memset(wbuf, 0xba, len);
171 memset(wbuf + 1024, 0x69, 512);
173 verify(rbuf, wbuf, len);
175 if (memcmp(wbuf, rbuf, len)) {
176 printf("Data mismatch on line %d\n", __LINE__);
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));
187 memset(rbuf, 0x1, len);
188 rc = read(fd, rbuf, len);
190 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
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__);