Whamcloud - gitweb
LU-8788 tests: modify create_pool to use as wrapper
[fs/lustre-release.git] / lustre / tests / directio.c
index ebcedb2..df467e6 100644 (file)
@@ -1,6 +1,33 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * 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.gnu.org/licenses/gpl-2.0.html
+ *
+ * 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 <sys/stat.h>
 #include <sys/mman.h>
 
+/* 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 *wbuf, *fname;
+        char *buf, *fname;
         int blocks, seek_blocks;
         long len;
         off64_t seek;
         struct stat64 st;
+        char pad = 0xba;
         int action;
         int rc;
 
         if (argc < 5 || argc > 6) {
-                printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
+                printf("Usage: %s <read/write/rdwr/readhole> file seek nr_blocks [blocksize]\n", argv[0]);
                 return 1;
         }
 
@@ -37,7 +77,10 @@ int main(int argc, char **argv)
                 action = O_WRONLY;
         else if (!strcmp(argv[1], "rdwr"))
                 action = O_RDWR;
-        else {
+        else if (!strcmp(argv[1], "readhole")) {
+                action = O_RDONLY;
+                pad = 0;
+        } else {
                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
                 return 1;
         }
@@ -69,12 +112,12 @@ int main(int argc, char **argv)
         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
         len = blocks * st.st_blksize;
 
-        wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
-        if (wbuf == MAP_FAILED) {
+        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(wbuf, 0xba, len);
+        memset(buf, pad, len);
 
         if (action == O_WRONLY || action == O_RDWR) {
                 if (lseek64(fd, seek, SEEK_SET) < 0) {
@@ -82,7 +125,7 @@ int main(int argc, char **argv)
                         return 1;
                 }
 
-                rc = write(fd, wbuf, len);
+                rc = write(fd, buf, len);
                 if (rc != len) {
                         printf("Write error %s (rc = %d, len = %ld)\n",
                                strerror(errno), rc, len);
@@ -91,26 +134,19 @@ int main(int argc, char **argv)
         }
 
         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);
+                /* 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 (memcmp(wbuf, rbuf, len)) {
+                if (check_bytes(buf, pad, len) != len) {
                         printf("Data mismatch\n");
                         return 1;
                 }