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 main(int argc, char **argv)
20 int blocks, seek_blocks;
27 if (argc < 5 || argc > 6) {
28 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
32 if (!strcmp(argv[1], "read"))
34 else if (!strcmp(argv[1], "write"))
36 else if (!strcmp(argv[1], "rdwr"))
39 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
43 seek_blocks = strtoul(argv[3], 0, 0);
44 blocks = strtoul(argv[4], 0, 0);
46 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
50 fd = open(argv[2], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
52 printf("Cannot open %s: %s\n", argv[1], strerror(errno));
57 st.st_blksize = strtoul(argv[4], 0, 0);
58 else if (fstat64(fd, &st) < 0) {
59 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
63 printf("directio on %s for %dx%lu bytes \n", argv[1], blocks,
66 seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
67 len = blocks * st.st_blksize;
69 wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
70 if (wbuf == MAP_FAILED) {
71 printf("No memory %s\n", strerror(errno));
74 memset(wbuf, 0xba, len);
76 if (action == O_WRONLY || action == O_RDWR) {
77 if (lseek64(fd, seek, SEEK_SET) < 0) {
78 printf("lseek64 failed: %s\n", strerror(errno));
82 rc = write(fd, wbuf, len);
84 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
89 if (action == O_RDONLY || action == O_RDWR) {
92 if (lseek64(fd, seek, SEEK_SET) < 0) {
93 printf("Cannot seek %s\n", strerror(errno));
97 rbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
98 if (rbuf == MAP_FAILED) {
99 printf("No memory %s\n", strerror(errno));
103 rc = read(fd, rbuf, len);
105 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
109 if (memcmp(wbuf, rbuf, len)) {
110 printf("Data mismatch\n");
117 #else /* !O_DIRECT */
118 #warning O_DIRECT not defined, directio test will fail
119 printf("O_DIRECT not defined\n");
121 #endif /* !O_DIRECT */