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