int main(int argc, char *argv[]) { int *pidlst; int numchild = 1; /* Number of children. Default 5 */ int i, fail_count = 0, failed = 0, total = 0; /* Options */ sprintf(filename,"testdata-6.%ld", syscall(__NR_gettid)); while ((i = getopt(argc, argv, "b:o:i:n:v:f:")) != -1) { switch(i) { case 'b': if ((bufsize = atoi(optarg)) <= 0) { fprintf(stderr, "bufsize must be > 0\n"); prg_usage(); } if (bufsize % 4096 != 0) { fprintf(stderr, "bufsize must be multiple of 4k\n"); prg_usage(); } break; case 'o': if ((offset = atoi(optarg)) <= 0) { fprintf(stderr, "offset must be > 0\n"); prg_usage(); } break; case 'i': if ((iter = atoi(optarg)) <= 0) { fprintf(stderr, "iterations must be > 0\n"); prg_usage(); } break; case 'n': if ((numchild = atoi(optarg)) <= 0) { fprintf(stderr, "no of children must be > 0\n"); prg_usage(); } break; case 'v': if ((nvector = atoi(optarg)) <= 0) { fprintf(stderr, "vectory array must be > 0\n"); prg_usage(); } break; case 'f': strcpy(filename, optarg); break; default: prg_usage(); } } setup(); /* Testblock-1: Read with Direct IO, Write without */ if (forkchldrn(&pidlst, numchild, READ_DIRECT, child_function) < 0) { failed = TRUE; fail_count++; tst_resm (TFAIL, "Read with Direct IO, Write without"); } else { if (waitchldrn(&pidlst, numchild) < 0) { failed = TRUE; fail_count++; tst_resm (TFAIL, "Read with Direct IO, Write without"); } else tst_resm (TPASS, "Read with Direct IO, Write without"); } unlink(filename); free(pidlst); total++; /* Testblock-2: Write with Direct IO, Read without */ if (forkchldrn(&pidlst, numchild, WRITE_DIRECT, child_function) < 0) { failed = TRUE; fail_count++; tst_resm (TFAIL, "Write with Direct IO, Read without"); } else { if (waitchldrn(&pidlst, numchild) < 0) { failed = TRUE; fail_count++; tst_resm (TFAIL, "Write with Direct IO, Read without"); } else tst_resm (TPASS, "Write with Direct IO, Read without"); } unlink(filename); free(pidlst); total++; /* Testblock-3: Read, Write with Direct IO. */ if (forkchldrn(&pidlst, numchild, RDWR_DIRECT, child_function) < 0) { failed = TRUE; fail_count++; tst_resm (TFAIL, "Read, Write with Direct IO"); } else { if (waitchldrn(&pidlst, numchild) < 0) { failed = TRUE; fail_count++; tst_resm (TFAIL, "Read, Write with Direct IO"); } else tst_resm (TPASS, "Read, Write with Direct IO"); } unlink(filename); free(pidlst); total++; if (failed) tst_resm(TINFO, "%d/%d testblocks failed", fail_count, total); else tst_resm(TINFO, "%d testblocks %d iterations with %d children completed", total, iter, numchild); cleanup(); tst_exit(); }
/* 校验读出的与写入的是否致 */ int main(int argc, char *argv[]) { int bufsize = BUFSIZE; /* Buffer size. Default 8k */ int numblks = NBLKS; /* Number of blocks. Default 20 */ char infile[LEN]; /* Input file. Default "infile" */ char outfile[LEN]; /* Output file. Default "outfile" */ int fd, fd1, fd2; int i, n, offset; char *buf; /* Options */ strcpy(infile, "infile"); /* Default input file */ strcpy(outfile, "outfile"); /* Default outfile file */ while ((i = getopt(argc, argv, "b:n:i:o:")) != -1) { switch (i) { case 'b': if ((bufsize = atoi(optarg)) <= 0) { fprintf(stderr, "bufsize must be > 0\n"); prg_usage(); } if (bufsize % 4096 != 0) { fprintf(stderr, "bufsize must be multiple of 4k\n"); prg_usage(); } break; case 'n': if ((numblks = atoi(optarg)) <= 0) { fprintf(stderr, "numblks must be > 0\n"); prg_usage(); } break; case 'i': strcpy(infile, optarg); break; case 'o': strcpy(outfile, optarg); break; default: prg_usage(); } } /* 检测系统是否支持O_DIRECT标志 */ /* Test for filesystem support of O_DIRECT */ if ((fd = open(infile, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) { tst_brkm(TCONF, NULL, "O_DIRECT is not supported by this filesystem."); } else { close(fd); } /* 创建文件1 */ /* Open files */ if ((fd1 = open(infile, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) { tst_brkm(TFAIL, NULL, "open infile failed: %s", strerror(errno)); } /* 创建文件2 */ if ((fd2 = open(outfile, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) { close(fd1); unlink(infile); tst_brkm(TFAIL, NULL, "open outfile failed: %s", strerror(errno)); } /* Allocate for buf, Create input file */ if ((buf = valloc(bufsize)) == 0) { tst_resm(TFAIL, "valloc() failed: %s", strerror(errno)); fail_clean(fd1, fd2, infile, outfile); } /* 将数据写入到文件1 */ for (i = 0; i < numblks; i++) { fillbuf(buf, bufsize, (char)(i % 256)); if (write(fd1, buf, bufsize) < 0) { tst_resm(TFAIL, "write infile failed: %s", strerror(errno)); fail_clean(fd1, fd2, infile, outfile); } } /* Copy infile to outfile using direct read and direct write */ offset = 0; if (lseek(fd1, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek(infd) failed: %s", strerror(errno)); fail_clean(fd1, fd2, infile, outfile); } /* 从文件1读取数据,并写入文件2 */ while ((n = read(fd1, buf, bufsize)) > 0) { if (lseek(fd2, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek(outfd) failed: %s", strerror(errno)); fail_clean(fd1, fd2, infile, outfile); } if (write(fd2, buf, n) < n) { tst_resm(TFAIL, "write(outfd) failed: %s", strerror(errno)); fail_clean(fd1, fd2, infile, outfile); } offset += n; if (lseek(fd1, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek(infd) failed: %s", strerror(errno)); fail_clean(fd1, fd2, infile, outfile); } } /* 检测两文件的内容是否一致 */ /* Verify */ if (filecmp(infile, outfile) != 0) { tst_resm(TFAIL, "file compare failed for %s and %s", infile, outfile); fail_clean(fd1, fd2, infile, outfile); } /* Cleanup */ close(fd1); close(fd2); unlink(infile); unlink(outfile); tst_resm(TPASS, "Test passed"); tst_exit(); }
int main(int argc, char *argv[]) { int i, action, fd_r, fd_w; int fail_count = 0, total = 0, failed = 0; /* Options */ sprintf(filename, "testdata-5.%ld", syscall(__NR_gettid)); while ((i = getopt(argc, argv, "b:o:i:v:f:")) != -1) { switch (i) { case 'b': if ((bufsize = atoi(optarg)) <= 0) { fprintf(stderr, "bufsize must be > 0"); prg_usage(); } if (bufsize % 4096 != 0) { fprintf(stderr, "bufsize must be > 0"); prg_usage(); } break; case 'o': if ((offset = atoll(optarg)) <= 0) { fprintf(stderr, "offset must be > 0"); prg_usage(); } break; case 'i': if ((iter = atoi(optarg)) <= 0) { fprintf(stderr, "iterations must be > 0"); prg_usage(); } break; case 'v': if ((nvector = atoi(optarg)) <= 0) { fprintf(stderr, "vector array must be > 0"); prg_usage(); } break; case 'f': strcpy(filename, optarg); break; default: prg_usage(); } } setup(); /* Testblock-1: Read with Direct IO, Write without */ action = READ_DIRECT; if ((fd_w = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s", filename, strerror(errno)); } if ((fd_r = open64(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s", filename, strerror(errno)); } if (runtest(fd_r, fd_w, iter, offset, action) < 0) { failed = TRUE; fail_count++; tst_resm(TFAIL, "Read with Direct IO, Write without"); } else tst_resm(TPASS, "Read with Direct IO, Write without"); unlink(filename); close(fd_r); close(fd_w); total++; /* Testblock-2: Write with Direct IO, Read without */ action = WRITE_DIRECT; if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s", filename, strerror(errno)); } if ((fd_r = open64(filename, O_RDONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s", filename, strerror(errno)); } if (runtest(fd_r, fd_w, iter, offset, action) < 0) { failed = TRUE; fail_count++; tst_resm(TFAIL, "Write with Direct IO, Read without"); } else tst_resm(TPASS, "Write with Direct IO, Read without"); unlink(filename); close(fd_r); close(fd_w); total++; /* Testblock-3: Read, Write with Direct IO */ action = RDWR_DIRECT; if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s", filename, strerror(errno)); } if ((fd_r = open64(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s", filename, strerror(errno)); } if (runtest(fd_r, fd_w, iter, offset, action) < 0) { failed = TRUE; fail_count++; tst_resm(TFAIL, "Read, Write with Direct IO"); } else tst_resm(TPASS, "Read, Write with Direct IO"); unlink(filename); close(fd_r); close(fd_w); total++; if (failed) tst_resm(TINFO, "%d/%d testblocks failed", fail_count, total); else tst_resm(TINFO, "%d testblocks %d iterations with %d vector array completed", total, iter, nvector); cleanup(); tst_exit(); }
int main(int argc, char *argv[]) { int fblocks = 1; /* Iterations. Default 1 */ int bufsize = BUFSIZE; int count, ret; int offset; int fd, newfd; int i, l_fail = 0, fail_count = 0, total = 0; int failed = 0; int shmsz = MMAP_GRANULARITY; int pagemask = ~(sysconf(_SC_PAGE_SIZE) - 1); char *buf0, *buf1, *buf2; caddr_t shm_base; /* Options */ while ((i = getopt(argc, argv, "b:")) != -1) { switch (i) { case 'b': if ((fblocks = atoi(optarg)) <= 0) { fprintf(stderr, "fblocks must be > 0\n"); prg_usage(); } break; default: prg_usage(); } } setup(); /* Open file and fill, allocate for buffer */ if ((fd = open(filename, O_DIRECT | O_RDWR | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "open failed for %s: %s", filename, strerror(errno)); } if ((buf0 = valloc(bufsize)) == NULL) { tst_brkm(TBROK, cleanup, "valloc() buf0 failed: %s", strerror(errno)); } for (i = 1; i < fblocks; i++) { fillbuf(buf0, bufsize, (char)i); if (write(fd, buf0, bufsize) < 0) { tst_brkm(TBROK, cleanup, "write failed for %s: %s", filename, strerror(errno)); } } close(fd); if ((buf2 = valloc(bufsize)) == NULL) { tst_brkm(TBROK, cleanup, "valloc() buf2 failed: %s", strerror(errno)); } if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) { tst_brkm(TBROK, cleanup, "open failed for %s: %s", filename, strerror(errno)); } /* Test-1: Negative Offset */ offset = -1; count = bufsize; errno = 0; ret = lseek(fd, offset, SEEK_SET); if ((ret >= 0) || (errno != EINVAL)) { tst_resm(TFAIL, "lseek allows negative offset. returns %d:%s", ret, strerror(errno)); failed = TRUE; fail_count++; } else tst_resm(TPASS, "Negative Offset"); total++; /* Test-2: Removed */ tst_resm(TPASS, "removed"); /* Test-3: Odd count of read and write */ offset = 0; count = 1; lseek(fd, 0, SEEK_SET); if (write(fd, buf2, 4096) == -1) { tst_resm(TFAIL, "can't write to file %d", ret); } switch (fs_type) { case TST_NFS_MAGIC: case TST_BTRFS_MAGIC: tst_resm(TCONF, "%s supports odd count IO", tst_fs_type_name(fs_type)); break; default: ret = runtest_f(fd, buf2, offset, count, EINVAL, 3, "odd count"); testcheck_end(ret, &failed, &fail_count, "Odd count of read and write"); } total++; /* Test-4: Read beyond the file size */ offset = bufsize * (fblocks + 10); count = bufsize; if (lseek(fd, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek failed: %s", strerror(errno)); failed = TRUE; fail_count++; tst_resm(TFAIL, "Read beyond the file size"); } else { errno = 0; ret = read(fd, buf2, count); if (ret > 0 || (ret < 0 && errno != EINVAL)) { tst_resm(TFAIL, "allows read beyond file size. returns %d: %s", ret, strerror(errno)); failed = TRUE; fail_count++; } else tst_resm(TPASS, "Read beyond the file size"); } total++; /* Test-5: Invalid file descriptor */ offset = 4096; count = bufsize; newfd = -1; ret = runtest_f(newfd, buf2, offset, count, EBADF, 5, "negative fd"); testcheck_end(ret, &failed, &fail_count, "Invalid file descriptor"); total++; /* Test-6: Out of range file descriptor */ count = bufsize; offset = 4096; if ((newfd = getdtablesize()) < 0) { tst_resm(TFAIL, "getdtablesize() failed: %s", strerror(errno)); failed = TRUE; tst_resm(TFAIL, "Out of range file descriptor"); } else { ret = runtest_f(newfd, buf2, offset, count, EBADF, 6, "out of range fd"); testcheck_end(ret, &failed, &fail_count, "Out of range file descriptor"); } close(newfd); total++; /* Test-7: Closed file descriptor */ offset = 4096; count = bufsize; SAFE_CLOSE(cleanup, fd); ret = runtest_f(fd, buf2, offset, count, EBADF, 7, "closed fd"); testcheck_end(ret, &failed, &fail_count, "Closed file descriptor"); total++; /* Test-9: removed */ tst_resm(TPASS, "removed"); /* Test-9: Character device (/dev/null) read, write */ offset = 0; count = bufsize; if ((newfd = open("/dev/null", O_DIRECT | O_RDWR)) < 0) { tst_resm(TCONF, "Direct I/O on /dev/null is not supported"); } else { ret = runtest_s(newfd, buf2, offset, count, 9, "/dev/null"); testcheck_end(ret, &failed, &fail_count, "character device read, write"); } close(newfd); total++; /* Test-10: read, write to a mmaped file */ shm_base = (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1)); if (shm_base == NULL) { tst_brkm(TBROK, cleanup, "sbrk failed: %s", strerror(errno)); } offset = 4096; count = bufsize; if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) { tst_brkm(TBROK, cleanup, "can't open %s: %s", filename, strerror(errno)); } shm_base = mmap(shm_base, 0x100000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0); if (shm_base == (caddr_t) - 1) { tst_brkm(TBROK, cleanup, "can't mmap file: %s", strerror(errno)); } ret = runtest_s(fd, buf2, offset, count, 10, "mmapped file"); testcheck_end(ret, &failed, &fail_count, "read, write to a mmaped file"); total++; /* Test-11: read, write to an unmaped file with munmap */ if ((ret = munmap(shm_base, 0x100000)) < 0) { tst_brkm(TBROK, cleanup, "can't unmap file: %s", strerror(errno)); } ret = runtest_s(fd, buf2, offset, count, 11, "unmapped file"); testcheck_end(ret, &failed, &fail_count, "read, write to an unmapped file"); close(fd); total++; /* Test-12: read from file not open for reading */ offset = 4096; count = bufsize; if ((fd = open(filename, O_DIRECT | O_WRONLY)) < 0) { tst_brkm(TBROK, cleanup, "can't open %s: %s", filename, strerror(errno)); } if (lseek(fd, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek failed: %s", strerror(errno)); failed = TRUE; fail_count++; } else { errno = 0; ret = read(fd, buf2, count); if (ret >= 0 || errno != EBADF) { tst_resm(TFAIL, "allows read on file not open for reading. returns %d: %s", ret, strerror(errno)); failed = TRUE; fail_count++; } else tst_resm(TPASS, "read from file not open for reading"); } close(fd); total++; /* Test-13: write to file not open for writing */ offset = 4096; count = bufsize; if ((fd = open(filename, O_DIRECT | O_RDONLY)) < 0) { tst_brkm(TBROK, cleanup, "can't open %s: %s", filename, strerror(errno)); } if (lseek(fd, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek failed: %s", strerror(errno)); failed = TRUE; fail_count++; } else { errno = 0; ret = write(fd, buf2, count); if (ret >= 0 || errno != EBADF) { tst_resm(TFAIL, "allows write on file not open for writing. returns %d: %s", ret, strerror(errno)); failed = TRUE; fail_count++; } else tst_resm(TPASS, "write to file not open for writing"); } close(fd); total++; /* Test-14: read, write with non-aligned buffer */ offset = 4096; count = bufsize; if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) { tst_brkm(TBROK, cleanup, "can't open %s: %s", filename, strerror(errno)); } switch (fs_type) { case TST_NFS_MAGIC: case TST_BTRFS_MAGIC: tst_resm(TCONF, "%s supports non-aligned buffer", tst_fs_type_name(fs_type)); break; default: ret = runtest_f(fd, buf2 + 1, offset, count, EINVAL, 14, " nonaligned buf"); testcheck_end(ret, &failed, &fail_count, "read, write with non-aligned buffer"); } close(fd); total++; /* Test-15: read, write buffer in read-only space */ offset = 4096; count = bufsize; l_fail = 0; if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) { tst_brkm(TBROK, cleanup, "can't open %s: %s", filename, strerror(errno)); } if (lseek(fd, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek before read failed: %s", strerror(errno)); l_fail = TRUE; } else { errno = 0; ret = read(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask), count); if (ret >= 0 || errno != EFAULT) { tst_resm(TFAIL, "read to read-only space. returns %d: %s", ret, strerror(errno)); l_fail = TRUE; } } if (lseek(fd, offset, SEEK_SET) < 0) { tst_resm(TFAIL, "lseek before write failed: %s", strerror(errno)); l_fail = TRUE; } else { ret = write(fd, (char *)((ulong) ADDRESS_OF_MAIN & pagemask), count); if (ret < 0) { tst_resm(TFAIL, "write to read-only space. returns %d: %s", ret, strerror(errno)); l_fail = TRUE; } } testcheck_end(l_fail, &failed, &fail_count, "read, write buffer in read-only space"); close(fd); total++; /* Test-16: read, write in non-existant space */ offset = 4096; count = bufsize; if ((buf1 = (char *)(((long)sbrk(0) + (shmsz - 1)) & ~(shmsz - 1))) == NULL) { tst_brkm(TBROK | TERRNO, cleanup, "sbrk failed"); } if ((fd = open(filename, O_DIRECT | O_RDWR)) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "open(%s, O_DIRECT|O_RDWR) failed", filename); } ret = runtest_f(fd, buf1, offset, count, EFAULT, 16, " nonexistant space"); testcheck_end(ret, &failed, &fail_count, "read, write in non-existant space"); total++; close(fd); /* Test-17: read, write for file with O_SYNC */ offset = 4096; count = bufsize; if ((fd = open(filename, O_DIRECT | O_RDWR | O_SYNC)) < 0) { tst_brkm(TBROK, cleanup, "open(%s, O_DIRECT|O_RDWR|O_SYNC failed)", filename); } ret = runtest_s(fd, buf2, offset, count, 17, "opened with O_SYNC"); testcheck_end(ret, &failed, &fail_count, "read, write for file with O_SYNC"); total++; close(fd); unlink(filename); if (failed) tst_resm(TINFO, "%d/%d test blocks failed", fail_count, total); else tst_resm(TINFO, "%d testblocks completed", total); cleanup(); tst_exit(); }