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)
19 int blocks, seek_blocks;
25 if (argc < 4 || argc > 5) {
26 printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]);
30 seek_blocks = strtoul(argv[2], 0, 0);
31 blocks = strtoul(argv[3], 0, 0);
33 fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
35 printf("Cannot open %s: %s\n", argv[1], strerror(errno));
40 st.st_blksize = strtoul(argv[4], 0, 0);
41 else if (fstat64(fd, &st) < 0) {
42 printf("Cannot stat %s: %s\n", argv[1], strerror(errno));
46 printf("directio on %s for %dx%lu bytes \n", argv[1], blocks,
49 seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
50 if (lseek64(fd, seek, SEEK_SET) < 0) {
51 printf("lseek64 failed: %s\n", strerror(errno));
55 len = blocks * st.st_blksize;
56 wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
57 if (wbuf == MAP_FAILED) {
58 printf("No memory %s\n", strerror(errno));
62 rbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
63 if (rbuf == MAP_FAILED) {
64 printf("No memory %s\n", strerror(errno));
68 memset(wbuf, 0xba, len);
69 rc = write(fd, wbuf, len);
71 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
75 if (lseek64(fd, seek, SEEK_SET) < 0) {
76 printf("Cannot seek %s\n", strerror(errno));
80 rc = read(fd, rbuf, len);
82 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
86 if (memcmp(wbuf, rbuf, len)) {
87 printf("Data mismatch\n");