1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
13 #include <sys/types.h>
17 int main(int argc, char **argv)
22 int blocks, seek_blocks;
29 if (argc < 5 || argc > 6) {
30 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
34 if (!strcmp(argv[1], "read"))
36 else if (!strcmp(argv[1], "write"))
38 else if (!strcmp(argv[1], "rdwr"))
41 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
46 seek_blocks = strtoul(argv[3], 0, 0);
47 blocks = strtoul(argv[4], 0, 0);
49 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
53 fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
55 printf("Cannot open %s: %s\n", fname, strerror(errno));
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));
66 printf("directio on %s for %dx%lu bytes \n", fname, blocks,
69 seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
70 len = blocks * st.st_blksize;
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));
77 memset(wbuf, 0xba, len);
79 if (action == O_WRONLY || action == O_RDWR) {
80 if (lseek64(fd, seek, SEEK_SET) < 0) {
81 printf("lseek64 failed: %s\n", strerror(errno));
85 rc = write(fd, wbuf, len);
87 printf("Write error %s (rc = %d, len = %ld)\n",
88 strerror(errno), rc, len);
93 if (action == O_RDONLY || action == O_RDWR) {
96 if (lseek64(fd, seek, SEEK_SET) < 0) {
97 printf("Cannot seek %s\n", strerror(errno));
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));
107 rc = read(fd, rbuf, len);
109 printf("Read error: %s rc = %d\n",strerror(errno),rc);
113 if (memcmp(wbuf, rbuf, len)) {
114 printf("Data mismatch\n");
121 #else /* !O_DIRECT */
122 #warning O_DIRECT not defined, directio test will fail
123 printf("O_DIRECT not defined\n");
125 #endif /* !O_DIRECT */