Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / tests / directio.c
index cc92c80..51315bc 100644 (file)
@@ -1,3 +1,7 @@
+/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
+ * vim:expandtab:shiftwidth=8:tabstop=8:
+ */
+#define  _GNU_SOURCE
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-// not correctly in the headers yet!!
-#ifndef O_DIRECT
-#define O_DIRECT         040000 /* direct disk access hint */
-#endif
-
 int main(int argc, char **argv)
 {
         int fd;
-        char *buf;
+        char *rbuf, *wbuf;
         int blocks, seek_blocks;
         long len;
         off64_t seek;
         struct stat64 st;
         int rc;
 
-        if (argc != 4) {
-                printf("Usage: %s file seek nr_blocks\n", argv[0]);
+        if (argc < 4 || argc > 5) {
+                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));
                 return 1;
         }
 
-        if (fstat64(fd, &st) < 0) {
+        if (argc == 5)
+                st.st_blksize = strtoul(argv[4], 0, 0);
+        else if (fstat64(fd, &st) < 0) {
                 printf("Cannot stat %s:  %s\n", argv[1], strerror(errno));
                 return 1;
         }
@@ -51,14 +53,20 @@ int main(int argc, char **argv)
         }
 
         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(buf, 0xba, len);
-        rc = write(fd, buf, 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;
@@ -69,12 +77,17 @@ int main(int argc, char **argv)
                 return 1;
         }
 
-        rc = read(fd, buf, len);
+        rc = read(fd, rbuf, len);
         if (rc != len) {
                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
                 return 1;
         }
 
-       printf("PASS\n");
+        if (memcmp(wbuf, rbuf, len)) {
+                printf("Data mismatch\n");
+                return 1;
+        }
+
+        printf("PASS\n");
         return 0;
 }