/* * LTP diotest02.c alt */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #define BUFSIZE 4096 #define LEN 30 char filename[LEN]; int bufsize = BUFSIZE; void fillbuf(char *buf, int count, char value) { while (count > 0) { strncpy(buf, &value, 1); buf++; count = count - 1; } } /* * bufcmp: Compare two buffers * vbufcmp: Compare two buffers of two io arrays */ int bufcmp(char *b1, char *b2, int bsize) { int i; for (i = 0; i < bsize; i++) { if (strncmp(&b1[i], &b2[i], 1)) { fprintf(stderr, "bufcmp: offset %d: Expected: 0x%x, got 0x%x\n", i, b1[i], b2[i]); return (-1); } } return (0); } /* * runtest: write the data to the file. Read the data from the file and compare. */ int runtest(int fd_r, int fd_w) { char *buf1; char *buf2; int i = 0, ret; /* Allocate for buffers */ if ((buf1 = valloc(bufsize)) == 0) { printf("valloc() buf1 failed: %s\n", strerror(errno)); return (-1); } if ((buf2 = valloc(bufsize)) == 0) { printf("valloc() buf2 failed: %s\n", strerror(errno)); return (-1); } fillbuf(buf1, bufsize, i); #if 0 if (lseek(fd_w, i * bufsize, SEEK_SET) < 0) { printf("lseek before write failed: %s\n", strerror(errno)); return (-1); } #endif ret = write(fd_w, buf1, bufsize); if (ret < bufsize) { printf("%d write failed: %s\n", ret, strerror(errno)); return (-1); } #if 0 if (lseek(fd_r, i * bufsize, SEEK_SET) < 0) { printf("lseek before read failed: %s", strerror(errno)); return (-1); } #endif ret = read(fd_r, buf2, bufsize); if (ret < bufsize) { printf("i %d ret %d read failed: %s\n", i, ret, strerror(errno)); return (-1); } if (bufcmp(buf1, buf2, bufsize) != 0) { printf("read/write comparision failed"); return (-1); } } int main(int argc, char *argv[]) { long offset = 0; /* Offset. Default 0 */ int i, fd_r, fd_w; /* Options */ sprintf(filename, "testdata-2.%ld", syscall(__NR_gettid)); #if 0 /* Testblock-1: Read with Direct IO, Write without */ if ((fd_w = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) printf("open(%s, O_WRONLY..) failed\n", filename); if ((fd_r = open(filename, O_DIRECT | O_RDONLY, 0666)) < 0) printf("open(%s, O_DIRECT|O_RDONLY..) failed\n", filename); if (runtest(fd_r, fd_w) < 0) { printf("Read with Direct IO, Write without. FAIL\n"); } else printf("Read with Direct IO, Write without. PASS\n"); close(fd_w); close(fd_r); unlink(filename); #endif /* Testblock-2: Write with Direct IO, Read without */ if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) == -1) printf( "open(%s, O_DIRECT|O_WRONLY..) failed\n", filename); if ((fd_r = open(filename, O_RDONLY | O_CREAT, 0666)) == -1) printf( "open(%s, O_RDONLY..) failed\n", filename); if (runtest(fd_r, fd_w) < 0) { printf("Write with Direct IO, Read without. FAIL\n"); } else printf("Write with Direct IO, Read without. PASS\n"); close(fd_w); close(fd_r); unlink(filename); #if 0 /* Testblock-3: Read, Write with Direct IO. */ if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) == -1) printf( "open(%s, O_DIRECT|O_WRONLY|O_CREAT, ..) failed\n", filename); if ((fd_r = open(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) == -1) printf( "open(%s, O_DIRECT|O_RDONLY|O_CREAT, ..) failed\n", filename); if (runtest(fd_r, fd_w) < 0) { printf("Read, Write with Direct IO. FAIL\n"); } else printf("Read, Write with Direct IO. PASS\n"); close(fd_w); close(fd_r); unlink(filename); #endif }