blob: fadcce27d1cdb1c54f4b206ec900e4ad285a0151 [file] [log] [blame]
robbiewbfb98f92002-06-13 05:26:55 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewbfb98f92002-06-13 05:26:55 +000018 */
19
robbiewbfb98f92002-06-13 05:26:55 +000020/*
21 * NAME
22 * diotest5.c
23 *
24 * DESCRIPTION
25 * The programs test buffered and direct IO with vector arrays using
26 * readv() and writev() calls.
27 * Test blocks
28 * [1] Direct readv, Buffered writev
29 * [2] Direct writev, Buffered readv
30 * [3] Direct readv, Direct writev
31 * The bufsize should be in n*4k size for direct readv, writev. The offset
32 * value marks the starting position in file from where to start the
33 * write and read. (Using larger offset, larger files can be tested).
subrata_modak4bb656a2009-02-26 12:02:09 +000034 * The nvector gives vector array size. Test data file can be
35 * specified through commandline and is useful for running test with
robbiewbfb98f92002-06-13 05:26:55 +000036 * raw devices as a file.
37 *
38 * USAGE
subrata_modak4bb656a2009-02-26 12:02:09 +000039 * diotest5 [-b bufsize] [-o offset] [-i iterations]
robbiewbfb98f92002-06-13 05:26:55 +000040 * [-v nvector] [-f filename]
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
robbiewbfb98f92002-06-13 05:26:55 +000042 * History
43 * 04/29/2002 Narasimha Sharoff nsharoff@us.ibm.com
44 *
45 * RESTRICTIONS
46 * None
47*/
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <unistd.h>
52#include <string.h>
53#include <sys/file.h>
54#include <sys/fcntl.h>
mridgeb48651d2004-05-18 17:05:51 +000055#include <sys/syscall.h>
robbiewbfb98f92002-06-13 05:26:55 +000056#include <sys/uio.h>
57#include <errno.h>
58
robbiewf7dc1be2003-03-14 16:20:37 +000059#include "diotest_routines.h"
60
robbiew96f4bb32003-03-13 15:39:53 +000061#include "test.h"
robbiew96f4bb32003-03-13 15:39:53 +000062
Wanlong Gao354ebb42012-12-07 10:10:04 +080063char *TCID = "diotest05"; /* Test program identifier. */
64int TST_TOTAL = 3; /* Total number of test conditions */
robbiew8680e9b2003-11-19 15:29:01 +000065
robbiew96f4bb32003-03-13 15:39:53 +000066#ifdef O_DIRECT
67
robbiewbfb98f92002-06-13 05:26:55 +000068#define BUFSIZE 4096
69#define TRUE 1
70#define LEN 30
71#define READ_DIRECT 1
72#define WRITE_DIRECT 2
73#define RDWR_DIRECT 3
74
Wanlong Gao354ebb42012-12-07 10:10:04 +080075static int bufsize = BUFSIZE; /* Buffer size. Default 4k */
76static int iter = 20; /* Iterations. Default 20 */
77static int nvector = 20; /* Vector array. Default 20 */
78static off64_t offset = 0; /* Start offset. Default 0 */
79static char filename[LEN]; /* Test data file */
subrata_modak04e0bfa2009-06-23 14:11:03 +000080static int fd1 = -1;
robbiewbfb98f92002-06-13 05:26:55 +000081/*
82 * runtest: Write the data in vector array to the file. Read the data
83 * from the file into another vectory array and verify. Repeat the test.
84*/
Wanlong Gao354ebb42012-12-07 10:10:04 +080085int runtest(int fd_r, int fd_w, int iter, off64_t offset, int action)
robbiewbfb98f92002-06-13 05:26:55 +000086{
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 int i, bufsize = BUFSIZE;
88 struct iovec *iov1, *iov2, *iovp;
robbiewbfb98f92002-06-13 05:26:55 +000089
90 /* Allocate for buffers and data pointers */
Wanlong Gao354ebb42012-12-07 10:10:04 +080091 if ((iov1 =
92 (struct iovec *)valloc(sizeof(struct iovec) * nvector)) == NULL) {
vapier0bdbaa32006-02-12 10:56:55 +000093 tst_resm(TFAIL, "valloc() buf1 failed: %s", strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +000095 }
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 if ((iov2 =
97 (struct iovec *)valloc(sizeof(struct iovec) * nvector)) == NULL) {
vapier0bdbaa32006-02-12 10:56:55 +000098 tst_resm(TFAIL, "valloc buf2 failed: %s", strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000100 }
101 for (i = 0, iovp = iov1; i < nvector; iovp++, i++) {
102 if ((iovp->iov_base = valloc(bufsize)) == NULL) {
vapier0bdbaa32006-02-12 10:56:55 +0000103 tst_resm(TFAIL, "valloc for iovp->iov_base: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 strerror(errno));
105 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000106 }
107 iovp->iov_len = bufsize;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 }
robbiewbfb98f92002-06-13 05:26:55 +0000109 for (i = 0, iovp = iov2; i < nvector; iovp++, i++) {
110 if ((iovp->iov_base = valloc(bufsize)) == NULL) {
vapier0bdbaa32006-02-12 10:56:55 +0000111 tst_resm(TFAIL, "valloc, iov2 for iovp->iov_base: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 strerror(errno));
113 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000114 }
115 iovp->iov_len = bufsize;
116 }
mridgeb48651d2004-05-18 17:05:51 +0000117
robbiewbfb98f92002-06-13 05:26:55 +0000118 /* Test */
119 for (i = 0; i < iter; i++) {
mridgeb48651d2004-05-18 17:05:51 +0000120 vfillbuf(iov1, nvector, i);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 vfillbuf(iov2, nvector, i + 1);
robbiewbfb98f92002-06-13 05:26:55 +0000122 if (lseek(fd_w, offset, SEEK_SET) < 0) {
vapier0bdbaa32006-02-12 10:56:55 +0000123 tst_resm(TFAIL, "lseek before writev failed: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 strerror(errno));
125 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000126 }
127 if (writev(fd_w, iov1, nvector) < 0) {
vapier0bdbaa32006-02-12 10:56:55 +0000128 tst_resm(TFAIL, "writev failed: %s", strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000130 }
131 if (lseek(fd_r, offset, SEEK_SET) < 0) {
vapier0bdbaa32006-02-12 10:56:55 +0000132 tst_resm(TFAIL, "lseek before readv failed: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 strerror(errno));
134 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000135 }
136 if (readv(fd_r, iov2, nvector) < 0) {
vapier0bdbaa32006-02-12 10:56:55 +0000137 tst_resm(TFAIL, "readv failed: %s", strerror(errno));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000139 }
140 if (vbufcmp(iov1, iov2, nvector) != 0) {
vapier0bdbaa32006-02-12 10:56:55 +0000141 tst_resm(TFAIL, "readv/writev comparision failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 return (-1);
robbiewbfb98f92002-06-13 05:26:55 +0000143 }
144 }
145
146 /* Cleanup */
147 for (i = 0, iovp = iov1; i < nvector; iovp++, i++) {
148 free(iovp->iov_base);
149 }
150 for (i = 0, iovp = iov2; i < nvector; iovp++, i++) {
151 free(iovp->iov_base);
152 }
153 free(iov1);
154 free(iov2);
subrata_modak43337a32009-02-26 11:43:51 +0000155 return 0;
robbiewbfb98f92002-06-13 05:26:55 +0000156}
157
158/*
159 * prg_usage: Display the program usage
160*/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161void prg_usage()
robbiewbfb98f92002-06-13 05:26:55 +0000162{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 fprintf(stderr,
164 "Usage: diotest5 [-b bufsize] [-o offset] [ -i iteration] [ -v nvector] [-f filename]\n");
robbiewbfb98f92002-06-13 05:26:55 +0000165 exit(1);
166}
167
subrata_modak04e0bfa2009-06-23 14:11:03 +0000168static void setup(void);
169static void cleanup(void);
170
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171int main(int argc, char *argv[])
robbiewbfb98f92002-06-13 05:26:55 +0000172{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800173 int i, action, fd_r, fd_w;
174 int fail_count = 0, total = 0, failed = 0;
robbiewbfb98f92002-06-13 05:26:55 +0000175
176 /* Options */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 sprintf(filename, "testdata-5.%ld", syscall(__NR_gettid));
robbiewbfb98f92002-06-13 05:26:55 +0000178 while ((i = getopt(argc, argv, "b:o:i:v:f:")) != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800179 switch (i) {
robbiewbfb98f92002-06-13 05:26:55 +0000180 case 'b':
181 if ((bufsize = atoi(optarg)) <= 0) {
182 fprintf(stderr, "bufsize must be > 0");
183 prg_usage();
184 }
185 if (bufsize % 4096 != 0) {
186 fprintf(stderr, "bufsize must be > 0");
187 prg_usage();
188 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000189 break;
robbiewbfb98f92002-06-13 05:26:55 +0000190 case 'o':
191 if ((offset = atoll(optarg)) <= 0) {
192 fprintf(stderr, "offset must be > 0");
193 prg_usage();
194 }
195 break;
196 case 'i':
197 if ((iter = atoi(optarg)) <= 0) {
198 fprintf(stderr, "iterations must be > 0");
199 prg_usage();
200 }
201 break;
202 case 'v':
203 if ((nvector = atoi(optarg)) <= 0) {
204 fprintf(stderr, "vector array must be > 0");
205 prg_usage();
206 }
207 break;
208 case 'f':
209 strcpy(filename, optarg);
210 break;
211 default:
212 prg_usage();
213 }
214 }
215
subrata_modak04e0bfa2009-06-23 14:11:03 +0000216 setup();
robbiew8680e9b2003-11-19 15:29:01 +0000217
robbiewbfb98f92002-06-13 05:26:55 +0000218 /* Testblock-1: Read with Direct IO, Write without */
219 action = READ_DIRECT;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220 if ((fd_w = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) {
subrata_modak04e0bfa2009-06-23 14:11:03 +0000221 tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 filename, strerror(errno));
robbiewbfb98f92002-06-13 05:26:55 +0000223 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800224 if ((fd_r = open64(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) < 0) {
subrata_modak04e0bfa2009-06-23 14:11:03 +0000225 tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800226 filename, strerror(errno));
robbiewbfb98f92002-06-13 05:26:55 +0000227 }
228 if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
robbiewbfb98f92002-06-13 05:26:55 +0000229 failed = TRUE;
230 fail_count++;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800231 tst_resm(TFAIL, "Read with Direct IO, Write without");
232 } else
233 tst_resm(TPASS, "Read with Direct IO, Write without");
robbiew92b688b2004-03-01 22:36:38 +0000234
robbiewbfb98f92002-06-13 05:26:55 +0000235 unlink(filename);
236 close(fd_r);
237 close(fd_w);
238 total++;
239
240 /* Testblock-2: Write with Direct IO, Read without */
241 action = WRITE_DIRECT;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800242 if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) {
subrata_modak04e0bfa2009-06-23 14:11:03 +0000243 tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 filename, strerror(errno));
robbiewbfb98f92002-06-13 05:26:55 +0000245 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800246 if ((fd_r = open64(filename, O_RDONLY | O_CREAT, 0666)) < 0) {
subrata_modak04e0bfa2009-06-23 14:11:03 +0000247 tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248 filename, strerror(errno));
robbiewbfb98f92002-06-13 05:26:55 +0000249 }
250 if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
robbiewbfb98f92002-06-13 05:26:55 +0000251 failed = TRUE;
252 fail_count++;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800253 tst_resm(TFAIL, "Write with Direct IO, Read without");
254 } else
255 tst_resm(TPASS, "Write with Direct IO, Read without");
robbiewbfb98f92002-06-13 05:26:55 +0000256 unlink(filename);
257 close(fd_r);
258 close(fd_w);
259 total++;
260
261 /* Testblock-3: Read, Write with Direct IO */
262 action = RDWR_DIRECT;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800263 if ((fd_w = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) {
subrata_modak04e0bfa2009-06-23 14:11:03 +0000264 tst_brkm(TBROK, cleanup, "fd_w open failed for %s: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800265 filename, strerror(errno));
robbiewbfb98f92002-06-13 05:26:55 +0000266 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800267 if ((fd_r = open64(filename, O_DIRECT | O_RDONLY | O_CREAT, 0666)) < 0) {
subrata_modak04e0bfa2009-06-23 14:11:03 +0000268 tst_brkm(TBROK, cleanup, "fd_r open failed for %s: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800269 filename, strerror(errno));
robbiewbfb98f92002-06-13 05:26:55 +0000270 }
271 if (runtest(fd_r, fd_w, iter, offset, action) < 0) {
robbiewbfb98f92002-06-13 05:26:55 +0000272 failed = TRUE;
273 fail_count++;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800274 tst_resm(TFAIL, "Read, Write with Direct IO");
275 } else
276 tst_resm(TPASS, "Read, Write with Direct IO");
robbiewbfb98f92002-06-13 05:26:55 +0000277 unlink(filename);
278 close(fd_r);
279 close(fd_w);
280 total++;
281
robbiew92b688b2004-03-01 22:36:38 +0000282 if (failed)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800283 tst_resm(TINFO, "%d/%d testblocks failed", fail_count, total);
robbiew92b688b2004-03-01 22:36:38 +0000284 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800285 tst_resm(TINFO,
286 "%d testblocks %d iterations with %d vector array completed",
287 total, iter, nvector);
robbiewbfb98f92002-06-13 05:26:55 +0000288
subrata_modak04e0bfa2009-06-23 14:11:03 +0000289 cleanup();
robbiew92b688b2004-03-01 22:36:38 +0000290
Garrett Cooper72521cf2010-12-18 06:31:47 -0800291 tst_exit();
robbiewbfb98f92002-06-13 05:26:55 +0000292}
robbiew96f4bb32003-03-13 15:39:53 +0000293
subrata_modak04e0bfa2009-06-23 14:11:03 +0000294static void setup(void)
295{
296 tst_tmpdir();
297
Wanlong Gao354ebb42012-12-07 10:10:04 +0800298 if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
299 tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
300 filename, strerror(errno));
subrata_modak04e0bfa2009-06-23 14:11:03 +0000301 }
302 close(fd1);
303
304 /* Test for filesystem support of O_DIRECT */
305 if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800306 tst_brkm(TCONF, cleanup,
307 "O_DIRECT is not supported by this filesystem. %s",
308 strerror(errno));
subrata_modak04e0bfa2009-06-23 14:11:03 +0000309 }
Subrata Modak76a720a2010-07-03 21:08:18 +0530310 close(fd1);
subrata_modak04e0bfa2009-06-23 14:11:03 +0000311}
312
313static void cleanup(void)
314{
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800315 if (fd1 != -1)
subrata_modak04e0bfa2009-06-23 14:11:03 +0000316 unlink(filename);
317
318 tst_rmdir();
319
subrata_modak04e0bfa2009-06-23 14:11:03 +0000320}
robbiew96f4bb32003-03-13 15:39:53 +0000321#else /* O_DIRECT */
322
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323int main()
324{
robbiew96f4bb32003-03-13 15:39:53 +0000325
Wanlong Gao354ebb42012-12-07 10:10:04 +0800326 tst_resm(TCONF, "O_DIRECT is not defined.");
327 return 0;
robbiew96f4bb32003-03-13 15:39:53 +0000328}
Chris Dearmanec6edca2012-10-17 19:54:01 -0700329#endif /* O_DIRECT */