1 /*
2 * Copyright (c) International Business Machines Corp., 2002
3 * 04/30/2002 Narasimha Sharoff nsharoff@us.ibm.com
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * DESCRIPTION
22 * Fork given number of children. Each child opens the same file, but
23 * uses its own file descriptior. The child does writes and reads from
24 * its segment in the file. The segment to which the child writes is
25 * determined by childnumber * bufsize. There is no need to use any locks.
26 * Tests the combinations of buffered/direct readv(), writev() calls.
27 * Test program contains the following test blocks:
28 * [1] Direct Read, Buffered write
29 * [2] Direct Write, Buffered read
30 * [3] Direct Read, Direct Write
31 *
32 * USAGE
33 * diotest6 [-b bufsize] [-o offset] [-n numchild] [-i iterations]
34 * [-v nvector] [-f fileaname]
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <sys/file.h>
42 #include <sys/fcntl.h>
43 #include <sys/syscall.h>
44 #include <sys/uio.h>
45 #include <errno.h>
46
47 #include "diotest_routines.h"
48
49 #include "test.h"
50
51 char *TCID = "diotest06";
52 int TST_TOTAL = 3;
53
54 #ifdef O_DIRECT
55
56 #define BUFSIZE 4096
57 #define TRUE 1
58 #define LEN 30
59 #define READ_DIRECT 1
60 #define WRITE_DIRECT 2
61 #define RDWR_DIRECT 3
62
63 static int iter = 100;
64 static int bufsize = BUFSIZE;
65 static off64_t offset = 0;
66 static int nvector = 20;
67 static char filename[LEN];
68 static int fd1 = -1;
69
70 static void setup(void);
71 static void cleanup(void);
72
prg_usage(void)73 static void prg_usage(void)
74 {
75 fprintf(stderr,
76 "Usage: diotest6 [-b bufsize] [-o offset] [-n numchild] [-i iterations] [-v nvector] [-f filename]\n");
77 exit(1);
78 }
79
80 /*
81 * runtest: write the data to the file. Read the data from the file and compare.
82 * For each iteration, write data starting at offse+iter*bufsize
83 * location in the file and read from there.
84 */
runtest(int fd_r,int fd_w,int childnum,int action)85 int runtest(int fd_r, int fd_w, int childnum, int action)
86 {
87 off64_t seekoff;
88 int i, bufsize = BUFSIZE;
89 char *buf1, *buf2;
90
91 buf1 = valloc(BUFSIZE);
92 buf2 = valloc(BUFSIZE);
93
94 if (!buf1 || !buf2) {
95 tst_resm(TBROK | TERRNO, "valloc() failed");
96 free(buf1);
97 free(buf2);
98 return -1;
99 }
100
101 /* Allocate for buffers and data pointers */
102 seekoff = offset + bufsize * childnum;
103
104 /* seek, write, read and verify */
105 for (i = 0; i < iter; i++) {
106 fillbuf(buf1, bufsize, childnum+i);
107
108 if (lseek(fd_w, seekoff, SEEK_SET) < 0) {
109 tst_resm(TFAIL, "lseek before write failed: %s",
110 strerror(errno));
111 return (-1);
112 }
113 if (write(fd_w, buf1, bufsize) < bufsize) {
114 tst_resm(TFAIL, "write failed: %s", strerror(errno));
115 return (-1);
116 }
117 if (action == READ_DIRECT) {
118 /* Make sure data is on to disk before read */
119 if (fsync(fd_w) < 0) {
120 tst_resm(TFAIL, "fsync failed: %s",
121 strerror(errno));
122 return (-1);
123 }
124 }
125 if (lseek(fd_r, seekoff, SEEK_SET) < 0) {
126 tst_resm(TFAIL, "lseek before read failed: %s",
127 strerror(errno));
128 return (-1);
129 }
130 int ret;
131 if ((ret = read(fd_r, buf2, bufsize)) < bufsize) {
132 tst_resm(TFAIL, "read failed: %s", strerror(errno));
133 return (-1);
134 }
135 if (bufcmp(buf1, buf2, bufsize) != 0) {
136 tst_resm(TFAIL, "comparsion failed. Child=%d offset=%d",
137 childnum, (int)seekoff);
138 return (-1);
139 }
140 }
141 return 0;
142 }
143
144 /*
145 * child_function: open the file for read and write. Call the runtest routine.
146 */
child_function(int childnum,int action)147 int child_function(int childnum, int action)
148 {
149 int fd_w, fd_r;
150
151 switch (action) {
152 case READ_DIRECT:
153 if ((fd_w = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) {
154 tst_resm(TFAIL, "fd_w open failed for %s: %s",
155 filename, strerror(errno));
156 return (-1);
157 }
158 if ((fd_r = open(filename, O_DIRECT | O_RDONLY, 0666)) < 0) {
159 tst_resm(TFAIL, "fd_r open failed for %s: %s",
160 filename, strerror(errno));
161 close(fd_w);
162 unlink(filename);
163 return (-1);
164 }
165 if (runtest(fd_r, fd_w, childnum, action) == -1) {
166 tst_resm(TFAIL, "Read Direct-child %d failed",
167 childnum);
168 close(fd_w);
169 close(fd_r);
170 return (-1);
171 }
172 break;
173 case WRITE_DIRECT:
174 if ((fd_w =
175 open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) {
176 tst_resm(TFAIL, "fd_w open failed for %s: %s", filename,
177 strerror(errno));
178 return (-1);
179 }
180 if ((fd_r = open(filename, O_RDONLY, 0666)) < 0) {
181 tst_resm(TFAIL, "fd_r open failed for %s: %s",
182 filename, strerror(errno));
183 close(fd_w);
184 unlink(filename);
185 return (-1);
186 }
187 if (runtest(fd_r, fd_w, childnum, action) == -1) {
188 tst_resm(TFAIL, "Write Direct-child %d failed",
189 childnum);
190 close(fd_w);
191 close(fd_r);
192 return (-1);
193 }
194 break;
195 case RDWR_DIRECT:
196 if ((fd_w =
197 open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666)) < 0) {
198 tst_resm(TFAIL, "fd_w open failed for %s: %s", filename,
199 strerror(errno));
200 return (-1);
201 }
202 if ((fd_r = open(filename, O_DIRECT | O_RDONLY, 0666)) < 0) {
203 tst_resm(TFAIL, "fd_r open failed for %s: %s",
204 filename, strerror(errno));
205 close(fd_w);
206 return (-1);
207 }
208 if (runtest(fd_r, fd_w, childnum, action) == -1) {
209 tst_resm(TFAIL, "RDWR Direct-child %d failed",
210 childnum);
211 close(fd_w);
212 close(fd_r);
213 return (-1);
214 }
215 break;
216 default:
217 fprintf(stderr, "Invalid Action Value\n");
218 return (-1);
219 }
220 close(fd_w);
221 close(fd_r);
222 exit(0);
223 }
224
main(int argc,char * argv[])225 int main(int argc, char *argv[])
226 {
227 int *pidlst;
228 int numchild = 1;
229 int i, fail_count = 0, failed = 0, total = 0;
230
231 /* Options */
232 sprintf(filename, "testdata-6.%ld", syscall(__NR_gettid));
233 while ((i = getopt(argc, argv, "b:o:i:n:v:f:")) != -1) {
234 switch (i) {
235 case 'b':
236 if ((bufsize = atoi(optarg)) <= 0) {
237 fprintf(stderr, "bufsize must be > 0\n");
238 prg_usage();
239 }
240 if (bufsize % 4096 != 0) {
241 fprintf(stderr,
242 "bufsize must be multiple of 4k\n");
243 prg_usage();
244 }
245 break;
246 case 'o':
247 if ((offset = atoi(optarg)) <= 0) {
248 fprintf(stderr, "offset must be > 0\n");
249 prg_usage();
250 }
251 break;
252 case 'i':
253 if ((iter = atoi(optarg)) <= 0) {
254 fprintf(stderr, "iterations must be > 0\n");
255 prg_usage();
256 }
257 break;
258 case 'n':
259 if ((numchild = atoi(optarg)) <= 0) {
260 fprintf(stderr, "no of children must be > 0\n");
261 prg_usage();
262 }
263 break;
264 case 'v':
265 if ((nvector = atoi(optarg)) <= 0) {
266 fprintf(stderr, "vectory array must be > 0\n");
267 prg_usage();
268 }
269 break;
270 case 'f':
271 strcpy(filename, optarg);
272 break;
273 default:
274 prg_usage();
275 }
276 }
277
278 setup();
279
280 /* Testblock-1: Read with Direct IO, Write without */
281 if (forkchldrn(&pidlst, numchild, READ_DIRECT, child_function) < 0) {
282 failed = TRUE;
283 fail_count++;
284 tst_resm(TFAIL, "Read with Direct IO, Write without");
285 } else {
286 if (waitchldrn(&pidlst, numchild) < 0) {
287 failed = TRUE;
288 fail_count++;
289 tst_resm(TFAIL, "Read with Direct IO, Write without");
290 } else
291 tst_resm(TPASS, "Read with Direct IO, Write without");
292
293 }
294 unlink(filename);
295 free(pidlst);
296 total++;
297
298 /* Testblock-2: Write with Direct IO, Read without */
299 if (forkchldrn(&pidlst, numchild, WRITE_DIRECT, child_function) < 0) {
300 failed = TRUE;
301 fail_count++;
302 tst_resm(TFAIL, "Write with Direct IO, Read without");
303 } else {
304 if (waitchldrn(&pidlst, numchild) < 0) {
305 failed = TRUE;
306 fail_count++;
307 tst_resm(TFAIL, "Write with Direct IO, Read without");
308 } else
309 tst_resm(TPASS, "Write with Direct IO, Read without");
310 }
311 unlink(filename);
312 free(pidlst);
313 total++;
314
315 /* Testblock-3: Read, Write with Direct IO. */
316 if (forkchldrn(&pidlst, numchild, RDWR_DIRECT, child_function) < 0) {
317 failed = TRUE;
318 fail_count++;
319 tst_resm(TFAIL, "Read, Write with Direct IO");
320 } else {
321 if (waitchldrn(&pidlst, numchild) < 0) {
322 failed = TRUE;
323 fail_count++;
324 tst_resm(TFAIL, "Read, Write with Direct IO");
325 } else
326 tst_resm(TPASS, "Read, Write with Direct IO");
327 }
328 unlink(filename);
329 free(pidlst);
330 total++;
331
332 if (failed)
333 tst_resm(TINFO, "%d/%d testblocks failed", fail_count, total);
334 else
335 tst_resm(TINFO,
336 "%d testblocks %d iterations with %d children completed",
337 total, iter, numchild);
338 cleanup();
339 tst_exit();
340 }
341
setup(void)342 static void setup(void)
343 {
344 tst_tmpdir();
345
346 if ((fd1 = open(filename, O_CREAT | O_EXCL, 0600)) < 0) {
347 tst_brkm(TBROK, cleanup, "Couldn't create test file %s: %s",
348 filename, strerror(errno));
349 }
350 close(fd1);
351
352 /* Test for filesystem support of O_DIRECT */
353 if ((fd1 = open(filename, O_DIRECT, 0600)) < 0) {
354 tst_brkm(TCONF, cleanup,
355 "O_DIRECT is not supported by this filesystem. %s",
356 strerror(errno));
357 }
358 close(fd1);
359 }
360
cleanup(void)361 static void cleanup(void)
362 {
363 if (fd1 != -1)
364 unlink(filename);
365
366 tst_rmdir();
367 }
368
369 #else /* O_DIRECT */
370
main(void)371 int main(void)
372 {
373 tst_brkm(TCONF, NULL, "O_DIRECT is not defined.");
374 }
375
376 #endif /* O_DIRECT */
377