X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Ftests%2Fdirectio.c;h=ebcedb2c781063e69a3716234228b480efd27622;hb=27443f4a9453a3956fb111252324f5b53e708f0b;hp=cc92c80254299ad8866b877b41ba6ab6a53da686;hpb=a2a0746305449dbd925879b14dc2c0d6040bb8bf;p=fs%2Flustre-release.git diff --git a/lustre/tests/directio.c b/lustre/tests/directio.c index cc92c80..ebcedb2 100644 --- a/lustre/tests/directio.c +++ b/lustre/tests/directio.c @@ -1,3 +1,9 @@ +/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- + * vim:expandtab:shiftwidth=8:tabstop=8: + */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif #include #include #include @@ -8,73 +14,113 @@ #include #include -// not correctly in the headers yet!! -#ifndef O_DIRECT -#define O_DIRECT 040000 /* direct disk access hint */ -#endif - int main(int argc, char **argv) { +#ifdef O_DIRECT int fd; - char *buf; + char *wbuf, *fname; int blocks, seek_blocks; long len; off64_t seek; struct stat64 st; + int action; int rc; - if (argc != 4) { - printf("Usage: %s file seek nr_blocks\n", argv[0]); + if (argc < 5 || argc > 6) { + printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); return 1; } - seek_blocks = strtoul(argv[2], 0, 0); - blocks = strtoul(argv[3], 0, 0); - fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644); - if (fd == -1) { - printf("Cannot open %s: %s\n", argv[1], strerror(errno)); + if (!strcmp(argv[1], "read")) + action = O_RDONLY; + else if (!strcmp(argv[1], "write")) + action = O_WRONLY; + else if (!strcmp(argv[1], "rdwr")) + action = O_RDWR; + else { + printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); return 1; } - if (fstat64(fd, &st) < 0) { - printf("Cannot stat %s: %s\n", argv[1], strerror(errno)); + fname = argv[2]; + seek_blocks = strtoul(argv[3], 0, 0); + blocks = strtoul(argv[4], 0, 0); + if (!blocks) { + printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); return 1; } - printf("directio on %s for %dx%lu bytes \n", argv[1], blocks, - st.st_blksize); + fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644); + if (fd == -1) { + printf("Cannot open %s: %s\n", fname, strerror(errno)); + return 1; + } - seek = (off64_t)seek_blocks * (off64_t)st.st_blksize; - if (lseek64(fd, seek, SEEK_SET) < 0) { - printf("lseek64 failed: %s\n", strerror(errno)); + if (argc >= 6) + st.st_blksize = strtoul(argv[5], 0, 0); + else if (fstat64(fd, &st) < 0) { + printf("Cannot stat %s: %s\n", fname, strerror(errno)); return 1; } + printf("directio on %s for %dx%lu bytes \n", fname, blocks, + st.st_blksize); + + seek = (off64_t)seek_blocks * (off64_t)st.st_blksize; len = blocks * st.st_blksize; - buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); - if (!buf) { + + wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); + if (wbuf == MAP_FAILED) { printf("No memory %s\n", strerror(errno)); return 1; } + memset(wbuf, 0xba, len); - memset(buf, 0xba, len); - rc = write(fd, buf, len); - if (rc != len) { - printf("Write error %s (rc = %d)\n", strerror(errno), rc); - return 1; - } + if (action == O_WRONLY || action == O_RDWR) { + if (lseek64(fd, seek, SEEK_SET) < 0) { + printf("lseek64 failed: %s\n", strerror(errno)); + return 1; + } - if (lseek64(fd, seek, SEEK_SET) < 0) { - printf("Cannot seek %s\n", strerror(errno)); - return 1; + rc = write(fd, wbuf, len); + if (rc != len) { + printf("Write error %s (rc = %d, len = %ld)\n", + strerror(errno), rc, len); + return 1; + } } - rc = read(fd, buf, len); - if (rc != len) { - printf("Read error: %s (rc = %d)\n", strerror(errno), rc); - return 1; + if (action == O_RDONLY || action == O_RDWR) { + char *rbuf; + + if (lseek64(fd, seek, SEEK_SET) < 0) { + printf("Cannot seek %s\n", strerror(errno)); + return 1; + } + + rbuf =mmap(0,len,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,0,0); + if (rbuf == MAP_FAILED) { + printf("No memory %s\n", strerror(errno)); + return 1; + } + + rc = read(fd, rbuf, len); + if (rc != len) { + printf("Read error: %s rc = %d\n",strerror(errno),rc); + return 1; + } + + if (memcmp(wbuf, rbuf, len)) { + printf("Data mismatch\n"); + return 1; + } } - printf("PASS\n"); + printf("PASS\n"); return 0; +#else /* !O_DIRECT */ +#warning O_DIRECT not defined, directio test will fail + printf("O_DIRECT not defined\n"); + return 1; +#endif /* !O_DIRECT */ }