X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=blobdiff_plain;f=lustre%2Ftests%2Fdirectio.c;h=da359b480cd92cfe1952f52199109547e2d1b841;hp=b531c50c949346cb456eb11d8833d4db867ff14f;hb=6ac2f14f0f622863c330b8b96da9373bad1abeb5;hpb=f12449c72130061905dfa9f1a52c8488066ee2fb diff --git a/lustre/tests/directio.c b/lustre/tests/directio.c index b531c50..da359b4 100644 --- a/lustre/tests/directio.c +++ b/lustre/tests/directio.c @@ -1,58 +1,166 @@ +/* + * GPL HEADER START + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 only, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License version 2 for more details (a copy is included + * in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; If not, see + * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + * + * GPL HEADER END + */ +/* + * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. + * Use is subject to license terms. + */ +/* + * This file is part of Lustre, http://www.lustre.org/ + * Lustre is a trademark of Sun Microsystems, Inc. + */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif #include #include #include #include #include #include +#include +#include #include -// not correctly in the headers yet!! -#define O_DIRECT 040000 /* direct disk access hint */ +/* return index of the first byte not matching given byte + * or buffer size if all bytes are matching */ +static size_t check_bytes(const char *buf, int byte, size_t len) +{ + const char *p; + + for (p = buf; p < buf + len; p++) + if (*p != byte) + break; + return p - buf; +} int main(int argc, char **argv) { +#ifdef O_DIRECT int fd; - char *buf; - int pages; - int rc; + char *buf, *fname; + int blocks, seek_blocks; + long len; + off64_t seek; + struct stat64 st; + char pad = 0xba; + int action; + int rc; - if (argc != 3) { - printf("Usage: %s file nr_pages\n", argv[0]); + if (argc < 5 || argc > 6) { + printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); return 1; } - pages = strtoul(argv[2], 0, 0); - printf("directio on %s for %d pages \n", argv[1], pages); - - buf = mmap(0, pages * 4096, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANON, 0, 0); - if (!buf) { - printf("No memory %s\n", 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 if (!strcmp(argv[1], "readhole")) { + action = O_RDONLY; + pad = 0; + } else { + printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]); return 1; } - fd = open(argv[1], O_DIRECT | O_RDWR | O_CREAT); - if (fd == -1) { - printf("Cannot open %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; } - rc = read(fd, buf, pages * 4096); - if (rc != pages * 4096) { - printf("Read error: %s, rc %d\n", strerror(errno), rc); + 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; } - if ( lseek(fd, 0, SEEK_SET) != 0 ) { - printf("Cannot seek %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; } - rc = write(fd, buf, pages * 4096); - if (rc != pages * 4096) { - printf("Write error %s\n", strerror(errno)); + 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 == MAP_FAILED) { + printf("No memory %s\n", strerror(errno)); return 1; } + memset(buf, pad, len); + + if (action == O_WRONLY || action == O_RDWR) { + if (lseek64(fd, seek, SEEK_SET) < 0) { + printf("lseek64 failed: %s\n", strerror(errno)); + return 1; + } + + rc = write(fd, buf, len); + if (rc != len) { + printf("Write error %s (rc = %d, len = %ld)\n", + strerror(errno), rc, len); + return 1; + } + } + + if (action == O_RDONLY || action == O_RDWR) { + if (lseek64(fd, seek, SEEK_SET) < 0) { + printf("Cannot seek %s\n", strerror(errno)); + return 1; + } + /* reset all bytes to something nor 0x0 neither 0xab */ + memset(buf, 0x5e, len); + rc = read(fd, buf, len); + if (rc != len) { + printf("Read error: %s rc = %d\n",strerror(errno),rc); + return 1; + } + + if (check_bytes(buf, pad, len) != 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 */ }