Whamcloud - gitweb
Land b_smallfix onto HEAD (20040512_1806)
[fs/lustre-release.git] / lustre / tests / directio.c
index 51315bc..7bcc5dc 100644 (file)
 #include <sys/stat.h>
 #include <sys/mman.h>
 
+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)
 {
         int fd;
@@ -43,15 +76,16 @@ int main(int argc, char **argv)
                 return 1;
         }
 
-        printf("directio on %s for %dx%lu bytes \n", argv[1], blocks,
-               st.st_blksize);
+        fprintf(stderr, "directio on %s for %dx%lu bytes \n", argv[1], 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) {
@@ -83,8 +117,84 @@ int main(int argc, char **argv)
                 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;
+        }
+
+        /* 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;
+        }
+
+        memset(wbuf, 0xba, len);
+        memset(wbuf + 1024, 0x69, 512);
+
+        verify(rbuf, wbuf, len);
+#if 0
+        if (memcmp(wbuf, rbuf, len)) {
+                printf("Data mismatch on line %d\n", __LINE__);
+                return 1;
+        }
+#endif
+
+        /* 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;
+        }
+
+        memset(rbuf, 0x1, len);
+        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\n");
+                printf("Data mismatch on line %d\n", __LINE__);
                 return 1;
         }