X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Ftests%2Fdirectio.c;h=1108cba9bdcf897aa3eb07d3329bb6e1146de7e4;hb=ed4405b2419b5e819ea9d5d37ea1dab7c6a63a57;hp=7bcc5dc2dfa9e3a6da07724d4287c56840268aa3;hpb=2dc9c16e770415d56839e1996015fec5fab93f29;p=fs%2Flustre-release.git diff --git a/lustre/tests/directio.c b/lustre/tests/directio.c index 7bcc5dc..1108cba 100644 --- a/lustre/tests/directio.c +++ b/lustre/tests/directio.c @@ -1,7 +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 @@ -12,192 +14,117 @@ #include #include -int write_buffer(char *fname, char *buffer, int len) -{ - int fd, rc; - - fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd == -1) { - printf("Cannot open %s: %s\n", fname, strerror(errno)); - exit(1); - } - rc = write(fd, buffer, len); - if (rc != len) { - printf("write: %d\n", rc); - exit(1); - } - close(fd); - return 0; -} - -void verify(char *buffer, char *compare, int length) -{ - int i; - for (i = 0; i < length; i++) { - if (buffer[i] != compare[i]) { - fprintf(stderr, "garbage read (i=%d): expected %c, found %c\n", - i, compare[i], buffer[i]); - write_buffer("/tmp/dio1", buffer, length); - write_buffer("/tmp/dio2", compare, length); - exit(1); - } - } -} - - int main(int argc, char **argv) { +#ifdef O_DIRECT int fd; - char *rbuf, *wbuf; + char *wbuf, *fname; int blocks, seek_blocks; long len; off64_t seek; struct stat64 st; + char pad = 0xba; + int action; int rc; - if (argc < 4 || argc > 5) { - printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); + if (argc < 5 || argc > 6) { + printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); + return 1; + } + + 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 if (!strcmp(argv[1], "readhole")) { + action = O_RDONLY; + pad = 0; + } else { + 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); + 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; + } - fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644); + fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644); if (fd == -1) { - printf("Cannot open %s: %s\n", argv[1], strerror(errno)); + printf("Cannot open %s: %s\n", fname, strerror(errno)); return 1; } - if (argc == 5) - st.st_blksize = strtoul(argv[4], 0, 0); + if (argc >= 6) + st.st_blksize = strtoul(argv[5], 0, 0); else if (fstat64(fd, &st) < 0) { - printf("Cannot stat %s: %s\n", argv[1], strerror(errno)); + printf("Cannot stat %s: %s\n", fname, strerror(errno)); return 1; } - fprintf(stderr, "directio on %s for %dx%lu bytes \n", argv[1], blocks, - st.st_blksize); + printf("directio on %s for %dx%lu bytes \n", fname, blocks, + st.st_blksize); seek = (off64_t)seek_blocks * (off64_t)st.st_blksize; -#if 0 - if (lseek64(fd, seek, SEEK_SET) < 0) { - printf("lseek64 failed: %s\n", strerror(errno)); - return 1; - } -#endif len = blocks * st.st_blksize; + 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, pad, len); - 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; - } - - memset(wbuf, 0xba, len); - rc = write(fd, wbuf, len); - if (rc != len) { - printf("Write error %s (rc = %d)\n", strerror(errno), rc); - return 1; - } - - if (lseek64(fd, seek, SEEK_SET) < 0) { - printf("Cannot seek %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; - } - - verify(rbuf, wbuf, len); - if (memcmp(wbuf, rbuf, len)) { - printf("Data mismatch on line %d\n", __LINE__); - return 1; - } - - /* try 512-byte buffers, and make sure that the other parts of the - * page aren't modified. */ - if (st.st_blksize < 4096) { - printf("512-byte block size tests skipped (because blocksize " - "passed is < 4k)\n"); - printf("PASS\n"); - return 0; - } - - - - /* write test */ - if (lseek64(fd, 512, SEEK_SET) < 0) { - printf("Cannot seek %s\n", strerror(errno)); - return 1; - } - - memset(wbuf, 0x44, len); - memset(wbuf + 2048, 0x69, 512); - rc = write(fd, wbuf + 2048, 512); - if (rc != 512) { - printf("Write error %s (rc = %d)\n", strerror(errno), rc); - return 1; - } - - memset(rbuf, 0x44, len); - memset(rbuf + 2048, 0x69, 512); - if (memcmp(wbuf, rbuf, len)) { - printf("Data mismatch on line %d\n", __LINE__); - 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; + } - /* read test */ - if (lseek64(fd, 512, SEEK_SET) < 0) { - printf("Cannot seek %s\n", strerror(errno)); - return 1; - } - memset(rbuf, 0xba, len); - rc = read(fd, rbuf + 1024, 512); - if (rc != 512) { - printf("Read error: %s (rc = %d)\n", strerror(errno), rc); - 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; + } } - memset(wbuf, 0xba, len); - memset(wbuf + 1024, 0x69, 512); + if (action == O_RDONLY || action == O_RDWR) { + char *rbuf; - verify(rbuf, wbuf, len); -#if 0 - if (memcmp(wbuf, rbuf, len)) { - printf("Data mismatch on line %d\n", __LINE__); - return 1; - } -#endif + if (lseek64(fd, seek, SEEK_SET) < 0) { + printf("Cannot seek %s\n", strerror(errno)); + return 1; + } - /* read back the whole block, to see that it's untouched. */ - 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; + } - memset(rbuf, 0x1, len); - rc = read(fd, rbuf, len); - if (rc != len) { - printf("Read error: %s (rc = %d)\n", strerror(errno), rc); - return 1; - } + rc = read(fd, rbuf, len); + if (rc != len) { + printf("Read error: %s rc = %d\n",strerror(errno),rc); + return 1; + } - memset(wbuf, 0xba, len); - memset(wbuf + 512, 0x69, 512); - if (memcmp(wbuf, rbuf, len)) { - printf("Data mismatch on line %d\n", __LINE__); - return 1; + if (memcmp(wbuf, rbuf, len)) { + printf("Data mismatch\n"); + return 1; + } } 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 */ }