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;
30 if (argc < 5 || argc > 6) {
31 printf("Usage: %s <read/write/rdwr/readhole> file seek nr_blocks [blocksize]\n", argv[0]);
35 if (!strcmp(argv[1], "read"))
37 else if (!strcmp(argv[1], "write"))
39 else if (!strcmp(argv[1], "rdwr"))
41 else if (!strcmp(argv[1], "readhole")) {
45 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
50 seek_blocks = strtoul(argv[3], 0, 0);
51 blocks = strtoul(argv[4], 0, 0);
53 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
57 fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
59 printf("Cannot open %s: %s\n", fname, strerror(errno));
64 st.st_blksize = strtoul(argv[5], 0, 0);
65 else if (fstat64(fd, &st) < 0) {
66 printf("Cannot stat %s: %s\n", fname, strerror(errno));
70 printf("directio on %s for %dx%lu bytes \n", fname, blocks,
73 seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
74 len = blocks * st.st_blksize;
76 wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
77 if (wbuf == MAP_FAILED) {
78 printf("No memory %s\n", strerror(errno));
81 memset(wbuf, pad, len);
83 if (action == O_WRONLY || action == O_RDWR) {
84 if (lseek64(fd, seek, SEEK_SET) < 0) {
85 printf("lseek64 failed: %s\n", strerror(errno));
89 rc = write(fd, wbuf, len);
91 printf("Write error %s (rc = %d, len = %ld)\n",
92 strerror(errno), rc, len);
97 if (action == O_RDONLY || action == O_RDWR) {
100 if (lseek64(fd, seek, SEEK_SET) < 0) {
101 printf("Cannot seek %s\n", strerror(errno));
105 rbuf =mmap(0,len,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,0,0);
106 if (rbuf == MAP_FAILED) {
107 printf("No memory %s\n", strerror(errno));
111 rc = read(fd, rbuf, len);
113 printf("Read error: %s rc = %d\n",strerror(errno),rc);
117 if (memcmp(wbuf, rbuf, len)) {
118 printf("Data mismatch\n");
125 #else /* !O_DIRECT */
126 #warning O_DIRECT not defined, directio test will fail
127 printf("O_DIRECT not defined\n");
129 #endif /* !O_DIRECT */