1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 * Copyright (c) Cyril Hrubis chrubis@suse.cz 2009
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * NAME
23 * ftest04.c -- test single file io (tsfio.c by rbk) (ported from SPIE, section2/filesuite/ftest5.c, by Airong Zhang)
24 *
25 * CALLS
26 * fsync, sync, lseek, read, write
27 *
28 *
29 * ALGORITHM
30 * Several child processes doing random seeks, read/write
31 * operations on the same file.
32 *
33 *
34 * RESTRICTIONS
35 * Runs a long time with default args - can take others on input
36 * line. Use with "term mode".
37 *
38 */
39 #define _XOPEN_SOURCE 500
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/wait.h>
44 #include <sys/file.h>
45 #include <fcntl.h>
46 #include <sys/stat.h>
47 #include <sys/uio.h>
48 #include <errno.h>
49 #include <signal.h>
50 #include "test.h"
51 #include "libftest.h"
52
53 char *TCID = "ftest04";
54 int TST_TOTAL = 1;
55
56 static void setup(void);
57 static void runtest(void);
58 static void dotest(int, int, int);
59 static void domisc(int, int);
60 static void term(int sig);
61
62 #define PASSED 1
63 #define FAILED 0
64
65 #define MAXCHILD 25
66 #define K_1 1024
67 #define K_2 2048
68 #define K_4 4096
69 #define MAXIOVCNT 16
70
71 static int csize; /* chunk size */
72 static int iterations; /* # total iterations */
73 static int max_size; /* max file size */
74 static int misc_intvl; /* for doing misc things; 0 ==> no */
75 static int nchild; /* number of child processes */
76 static int parent_pid;
77 static int pidlist[MAXCHILD];
78
79 static char filename[MAXPATHLEN];
80
81 static int local_flag;
82
main(int ac,char * av[])83 int main(int ac, char *av[])
84 {
85 int lc;
86
87 tst_parse_opts(ac, av, NULL, NULL);
88
89 setup();
90
91 for (lc = 0; TEST_LOOPING(lc); lc++) {
92
93 runtest();
94
95 if (local_flag == PASSED)
96 tst_resm(TPASS, "Test passed.");
97 else
98 tst_resm(TFAIL, "Test failed.");
99
100 /* ??? only one loop ??? */
101 tst_rmdir();
102 tst_exit();
103 }
104
105 tst_exit();
106 }
107
setup(void)108 static void setup(void)
109 {
110 int fd;
111 char wdbuf[MAXPATHLEN];
112
113 parent_pid = getpid();
114
115 /*
116 * Make a filename for the test.
117 */
118 tst_tmpdir();
119 if (!filename[0])
120 sprintf(filename, "%s/ftest04.%d", getcwd(wdbuf, MAXPATHLEN),
121 getpid());
122
123 fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
124 if (fd < 0) {
125 tst_brkm(TBROK, NULL, "Error %d creating file %s", errno,
126 filename);
127 }
128 close(fd);
129
130 /*
131 * Default values for run conditions.
132 */
133 iterations = 10;
134 nchild = 5;
135 csize = K_2; /* should run with 1, 2, and 4 K sizes */
136 max_size = K_1 * K_1;
137 misc_intvl = 10;
138
139 if (sigset(SIGTERM, term) == SIG_ERR) {
140 tst_brkm(TFAIL, NULL, "first sigset failed");
141 }
142
143 local_flag = PASSED;
144 }
145
runtest(void)146 static void runtest(void)
147 {
148 int count, child, fd, i, nwait, status;
149
150 nwait = 0;
151
152 for (i = 0; i < nchild; i++) {
153 if ((child = fork()) == 0) {
154 fd = open(filename, O_RDWR);
155 if (fd < 0) {
156 tst_brkm(TBROK,
157 NULL,
158 "\tTest[%d]: error %d openning %s.",
159 i,
160 errno, filename);
161 }
162 dotest(nchild, i, fd);
163 close(fd);
164 tst_exit();
165 }
166 if (child < 0) {
167 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
168 } else {
169 pidlist[i] = child;
170 nwait++;
171 }
172 }
173
174 /*
175 * Wait for children to finish.
176 */
177 count = 0;
178 while ((child = wait(&status)) != -1 || errno == EINTR) {
179 if (child > 0) {
180 //tst_resm(TINFO, "\tTest{%d} exited status = 0x%x", child, status);
181 if (status) {
182 tst_resm(TFAIL,
183 "\tExpected 0 exit status - failed.");
184 local_flag = FAILED;
185 }
186 ++count;
187 }
188 }
189
190 /*
191 * Should have collected all children.
192 */
193 if (count != nwait) {
194 tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
195 count);
196 local_flag = FAILED;
197 }
198
199 unlink(filename);
200 sync();
201 }
202
203 /*
204 * dotest()
205 * Children execute this.
206 *
207 * Randomly read/mod/write chunks with known pattern and check.
208 * When fill sectors, iterate.
209 */
210 #define NMISC 2
211 enum m_type { m_fsync, m_sync };
212 char *m_str[] = { "fsync", "sync" };
213
214 int misc_cnt[NMISC]; /* counts # of each kind of misc */
215 int misc_flag;
216 int nchunks;
217
218 #define CHUNK(i) (((i) * testers + me) * csize)
219 #define NEXTMISC ((rand() % misc_intvl) + 5)
220
dotest(int testers,int me,int fd)221 static void dotest(int testers, int me, int fd)
222 {
223 char *bits;
224 char val, val0;
225 int count, collide, chunk, whenmisc, xfr, i;
226
227 /* Stuff for the readv call */
228 struct iovec r_iovec[MAXIOVCNT];
229 int r_ioveclen;
230
231 /* Stuff for the writev call */
232 struct iovec val0_iovec[MAXIOVCNT];
233 struct iovec val_iovec[MAXIOVCNT];
234 int w_ioveclen;
235 struct stat stat;
236
237 nchunks = max_size / (testers * csize);
238 whenmisc = 0;
239
240 if ((bits = malloc((nchunks + 7) / 8)) == NULL) {
241 tst_brkm(TBROK, NULL, "\tmalloc failed(bits)");
242 }
243
244 /*Allocate memory for the iovec buffers and init the iovec arrays
245 */
246 r_ioveclen = w_ioveclen = csize / MAXIOVCNT;
247
248 /* Please note that the above statement implies that csize
249 * be evenly divisible by MAXIOVCNT.
250 */
251
252 for (i = 0; i < MAXIOVCNT; i++) {
253 if ((r_iovec[i].iov_base = malloc(r_ioveclen)) == NULL) {
254 tst_brkm(TBROK, NULL, "\tmalloc failed(r_iovec[])");
255 }
256 r_iovec[i].iov_len = r_ioveclen;
257
258 /* Allocate unused memory areas between all the buffers to
259 * make things more diffult for the OS.
260 */
261 if (malloc((i + 1) * 8) == NULL) {
262 tst_brkm(TBROK, NULL, "\tmalloc failed");
263 }
264
265 if ((val0_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) {
266 tst_brkm(TBROK, NULL, "\tmalloc failed(val0_iovec[])");
267 }
268
269 val0_iovec[i].iov_len = w_ioveclen;
270
271 if (malloc((i + 1) * 8) == NULL) {
272 tst_brkm(TBROK, NULL, "\tmalloc failed");
273 }
274
275 if ((val_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) {
276 tst_brkm(TBROK, NULL, "\tmalloc failed(iov_base)");
277 }
278
279 val_iovec[i].iov_len = w_ioveclen;
280
281 if (malloc((i + 1) * 8) == NULL) {
282 tst_brkm(TBROK, NULL, "\tmalloc failed");
283 }
284 }
285
286 /*
287 * No init sectors; file-sys makes 0 to start.
288 */
289 val = (64 / testers) * me + 1;
290 val0 = 0;
291
292 /*
293 * For each iteration:
294 * zap bits array
295 * loop:
296 * pick random chunk, read it.
297 * if corresponding bit off {
298 * verify == 0. (sparse file)
299 * ++count;
300 * } else
301 * verify == val.
302 * write "val" on it.
303 * repeat until count = nchunks.
304 * ++val.
305 */
306 srand(getpid());
307
308 if (misc_intvl)
309 whenmisc = NEXTMISC;
310
311 while (iterations-- > 0) {
312 for (i = 0; i < NMISC; i++)
313 misc_cnt[i] = 0;
314 memset(bits, 0, (nchunks + 7) / 8);
315 /* Have to fill the val0 and val iov buffers in a different manner */
316 for (i = 0; i < MAXIOVCNT; i++) {
317 memset(val0_iovec[i].iov_base, val0,
318 val0_iovec[i].iov_len);
319 memset(val_iovec[i].iov_base, val,
320 val_iovec[i].iov_len);
321
322 }
323 count = 0;
324 collide = 0;
325 while (count < nchunks) {
326 chunk = rand() % nchunks;
327 /*
328 * Read it.
329 */
330 if (lseek(fd, CHUNK(chunk), 0) < 0) {
331 tst_brkm(TFAIL,
332 NULL,
333 "\tTest[%d]: lseek(0) fail at %x, errno = %d.",
334 me, CHUNK(chunk), errno);
335 }
336 if ((xfr = readv(fd, &r_iovec[0], MAXIOVCNT)) < 0) {
337 tst_brkm(TFAIL,
338 NULL,
339 "\tTest[%d]: readv fail at %x, errno = %d.",
340 me, CHUNK(chunk), errno);
341 }
342 /*
343 * If chunk beyond EOF just write on it.
344 * Else if bit off, haven't seen it yet.
345 * Else, have. Verify values.
346 */
347 if (xfr == 0) {
348 bits[chunk / 8] |= (1 << (chunk % 8));
349 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
350 if (xfr != csize) {
351 tst_brkm(TFAIL,
352 NULL,
353 "\tTest[%d]: xfr=%d != %d, zero read.",
354 me, xfr, csize);
355 }
356 for (i = 0; i < MAXIOVCNT; i++) {
357 if (memcmp
358 (r_iovec[i].iov_base,
359 val0_iovec[i].iov_base,
360 r_iovec[i].iov_len)) {
361 tst_resm(TFAIL,
362 "\tTest[%d] bad verify @ 0x%x for val %d count %d xfr %d.",
363 me, CHUNK(chunk), val0,
364 count, xfr);
365 fstat(fd, &stat);
366 tst_resm(TINFO,
367 "\tStat: size=%llx, ino=%x",
368 stat.st_size, (unsigned)stat.st_ino);
369 ft_dumpiov(&r_iovec[i]);
370 ft_dumpbits(bits,
371 (nchunks + 7) / 8);
372 tst_exit();
373 }
374 }
375 bits[chunk / 8] |= (1 << (chunk % 8));
376 ++count;
377 } else {
378 if (xfr != csize) {
379 tst_brkm(TFAIL,
380 NULL,
381 "\tTest[%d]: xfr=%d != %d, val read.",
382 me, xfr, csize);
383 }
384 ++collide;
385 for (i = 0; i < MAXIOVCNT; i++) {
386 if (memcmp
387 (r_iovec[i].iov_base,
388 val_iovec[i].iov_base,
389 r_iovec[i].iov_len)) {
390 tst_resm(TFAIL,
391 "\tTest[%d] bad verify @ 0x%x for val %d count %d xfr %d.",
392 me, CHUNK(chunk), val,
393 count, xfr);
394 fstat(fd, &stat);
395 tst_resm(TINFO,
396 "\tStat: size=%llx, ino=%x",
397 stat.st_size, (unsigned)stat.st_ino);
398 ft_dumpiov(&r_iovec[i]);
399 ft_dumpbits(bits,
400 (nchunks + 7) / 8);
401 tst_exit();
402 }
403 }
404 }
405 /*
406 * Write it.
407 */
408 if (lseek(fd, -xfr, 1) < 0) {
409 tst_brkm(TFAIL,
410 NULL,
411 "\tTest[%d]: lseek(1) fail at %x, errno = %d.",
412 me, CHUNK(chunk), errno);
413 }
414 if ((xfr =
415 writev(fd, &val_iovec[0], MAXIOVCNT)) < csize) {
416 if (errno == ENOSPC) {
417 tst_resm(TFAIL,
418 "\tTest[%d]: no space, exiting.",
419 me);
420 fsync(fd);
421 tst_exit();
422 }
423 tst_brkm(TFAIL,
424 NULL,
425 "\tTest[%d]: writev fail at %x xfr %d, errno = %d.",
426 me, CHUNK(chunk), xfr, errno);
427 }
428 /*
429 * If hit "misc" interval, do it.
430 */
431 if (misc_intvl && --whenmisc <= 0) {
432 domisc(me, fd);
433 whenmisc = NEXTMISC;
434 }
435 if (count + collide > 2 * nchunks)
436 break;
437 }
438
439 /*
440 * End of iteration, maybe before doing all chunks.
441 */
442
443 if (count < nchunks) {
444 //tst_resm(TINFO, "\tTest{%d} val %d stopping @ %d, collide = {%d}.",
445 // me, val, count, collide);
446 for (i = 0; i < nchunks; i++) {
447 if ((bits[i / 8] & (1 << (i % 8))) == 0) {
448 if (lseek(fd, CHUNK(i), 0) < 0) {
449 tst_brkm(TFAIL,
450 NULL,
451 "\tTest[%d]: lseek fail at %x, errno = %d.",
452 me, CHUNK(i), errno);
453 }
454 if (writev(fd, &val_iovec[0], MAXIOVCNT)
455 != csize) {
456 tst_brkm(TFAIL,
457 NULL,
458 "\tTest[%d]: writev fail at %x, errno = %d.",
459 me, CHUNK(i), errno);
460 }
461 }
462 }
463 }
464
465 fsync(fd);
466 ++misc_cnt[m_fsync];
467 //tst_resm(TINFO, "\tTest[%d] val %d done, count = %d, collide = %d.",
468 // me, val, count, collide);
469 //for (i = 0; i < NMISC; i++)
470 // tst_resm(TINFO, "\t\tTest[%d]: %d %s's.", me, misc_cnt[i], m_str[i]);
471 val0 = val++;
472 }
473 }
474
475 /*
476 * domisc()
477 * Inject misc syscalls into the thing.
478 */
domisc(int me,int fd)479 static void domisc(int me, int fd)
480 {
481 if (fsync(fd) < 0) {
482 tst_brkm(TFAIL, NULL, "\tTest[%d]: fsync error %d.", me,
483 errno);
484 }
485
486 ++misc_cnt[1];
487 }
488
term(int sig LTP_ATTRIBUTE_UNUSED)489 static void term(int sig LTP_ATTRIBUTE_UNUSED)
490 {
491 int i;
492
493 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
494
495 if (parent_pid == getpid()) {
496 for (i = 0; i < nchild; i++)
497 if (pidlist[i])
498 kill(pidlist[i], SIGTERM);
499 }
500
501 exit(0);
502 }
503