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 * ftest07.c -- test file I/O with readv and writev (ported from SPIE,
24 * section2/filesuite/ftest9.c
25 *
26 * this is the same as ftest4, except that it uses lseek64
27 *
28 * CALLS
29 * lseek64, readv, writev,
30 * truncate, ftruncate, fsync, sync, fstat
31 *
32 * ALGORITHM
33 * A bitmap is used to map pieces of a file.
34 * Loop: pick a random piece of the file
35 * if we haven't seen it before make sure it is zero,
36 * write pattern
37 * if we have seen it before make sure correct pattern.
38 *
39 * This was originally written by rbk - was program tfio.c
40 * Modified by dale to integrate with test suites.
41 * Modified by G. Stevens to use readv and writev.
42 * Modofied by K. Hakim to integrate with SPIES.
43 *
44 * RESTRICTIONS
45 * 1. Runs a long time with default args - can take others on input
46 * line. Use with "term mode".
47 * If run on vax the ftruncate will not be random - will always go to
48 * start of file. NOTE: produces a very high load average!!
49 *
50 * 2. The "csize" argument must be evenly divisible by MAXIOVCNT.
51 *
52 * CAUTION!!
53 * If a file is supplied to this program with the "-f" option
54 * it will be removed with a system("rm -rf filename") call.
55 *
56 *
57 */
58
59 #define _XOPEN_SOURCE 500
60 #define _LARGEFILE64_SOURCE 1
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/wait.h>
64 #include <sys/stat.h>
65 #include <errno.h>
66 #include <sys/uio.h>
67 #include <fcntl.h>
68 #include <signal.h>
69 #include <stdio.h>
70 #include <unistd.h>
71 #include <inttypes.h>
72 #include "test.h"
73 #include "safe_macros.h"
74 #include "libftest.h"
75
76 char *TCID = "ftest07";
77 int TST_TOTAL = 1;
78
79 #define PASSED 1
80 #define FAILED 0
81
82 #define MAXCHILD 25
83 #define K_1 1024
84 #define K_2 2048
85 #define K_4 4096
86 #define MAXIOVCNT 16
87
88 static void setup(void);
89 static void runtest(void);
90 static void dotest(int, int, int);
91 static void domisc(int, int, char *);
92 static void term(int sig);
93
94 static int csize; /* chunk size */
95 static int iterations; /* # total iterations */
96 static off64_t max_size; /* max file size */
97 static int misc_intvl; /* for doing misc things; 0 ==> no */
98 static int nchild; /* how many children */
99 static int fd; /* file descriptor used by child */
100 static int parent_pid;
101 static int pidlist[MAXCHILD];
102 static char test_name[2]; /* childs test directory name */
103
104 static char fuss[MAXPATHLEN]; /* directory to do this in */
105 static char homedir[MAXPATHLEN]; /* where we started */
106
107 static int local_flag;
108
main(int ac,char * av[])109 int main(int ac, char *av[])
110 {
111 int lc;
112
113 tst_parse_opts(ac, av, NULL, NULL);
114
115 setup();
116
117 for (lc = 0; TEST_LOOPING(lc); lc++) {
118
119 local_flag = PASSED;
120
121 runtest();
122
123 if (local_flag == PASSED)
124 tst_resm(TPASS, "Test passed.");
125 else
126 tst_resm(TFAIL, "Test failed.");
127
128 tst_rmdir();
129 tst_exit();
130
131 }
132
133 tst_exit();
134 }
135
setup(void)136 static void setup(void)
137 {
138 char wdbuf[MAXPATHLEN], *cwd;
139
140 /*
141 * Make a directory to do this in; ignore error if already exists.
142 * Save starting directory.
143 */
144 if ((cwd = getcwd(homedir, sizeof(homedir))) == NULL) {
145 tst_brkm(TBROK, NULL, "Failed to get corrent directory");
146 }
147
148 parent_pid = getpid();
149 tst_tmpdir();
150 if (!fuss[0])
151 sprintf(fuss, "%s/ftest07.%d", getcwd(wdbuf, sizeof(wdbuf)),
152 getpid());
153
154 mkdir(fuss, 0755);
155
156 SAFE_CHDIR(NULL, fuss);
157
158 /*
159 * Default values for run conditions.
160 */
161
162 iterations = 10;
163 nchild = 5;
164 csize = K_2; /* should run with 1, 2, and 4 K sizes */
165 max_size = K_1 * K_1;
166 misc_intvl = 10;
167
168 if (sigset(SIGTERM, term) == SIG_ERR) {
169 tst_brkm(TBROK | TERRNO, NULL, "sigset (signo=SIGTERM) failed");
170 }
171
172 }
173
runtest(void)174 static void runtest(void)
175 {
176 pid_t pid;
177 int child, count, i, nwait, status;
178
179 nwait = 0;
180
181 for (i = 0; i < nchild; i++) {
182 test_name[0] = 'a' + i;
183 test_name[1] = '\0';
184 fd = SAFE_OPEN(NULL, test_name, O_RDWR | O_CREAT | O_TRUNC,
185 0666);
186
187 if ((child = fork()) == 0) {
188 dotest(nchild, i, fd);
189 tst_exit();
190 }
191
192 close(fd);
193
194 if (child < 0) {
195 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
196 } else {
197 pidlist[i] = child;
198 nwait++;
199 }
200 }
201
202 /*
203 * Wait for children to finish.
204 */
205 count = 0;
206 while (1) {
207 if ((child = wait(&status)) >= 0) {
208 //tst_resm(TINFO, "\tTest{%d} exited status = 0x%x", child, status);
209 if (status) {
210 tst_resm(TFAIL,
211 "\tTest{%d} failed, expected 0 exit.",
212 child);
213 local_flag = FAILED;
214 }
215 ++count;
216 } else {
217 if (errno != EINTR)
218 break;
219 }
220 }
221
222 /*
223 * Should have collected all children.
224 */
225
226 if (count != nwait) {
227 tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
228 count);
229 local_flag = FAILED;
230 }
231
232 chdir(homedir);
233
234 pid = fork();
235 if (pid < 0) {
236 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
237 }
238
239 if (pid == 0) {
240 execl("/bin/rm", "rm", "-rf", fuss, NULL);
241 exit(1);
242 } else
243 wait(&status);
244 if (status) {
245 tst_resm(TINFO, "CAUTION - ftest07, '%s' may not be removed",
246 fuss);
247 }
248
249 sync();
250 }
251
252 /*
253 * dotest()
254 * Children execute this.
255 *
256 * Randomly read/mod/write chunks with known pattern and check.
257 * When fill sectors, iterate.
258 */
259
260 #define NMISC 4
261 enum m_type { m_fsync, m_trunc, m_sync, m_fstat };
262 char *m_str[] = { "fsync", "trunc", "sync", "fstat" };
263
264 int misc_cnt[NMISC]; /* counts # of each kind of misc */
265 long long unsigned int file_max; /* file-max size */
266 int nchunks;
267 int last_trunc = -1;
268 int tr_flag;
269 enum m_type type = m_fsync;
270
CHUNK(off64_t i)271 static inline long long unsigned int CHUNK(off64_t i)
272 {
273 return (long long unsigned int) i * csize;
274 }
275 #define NEXTMISC ((rand() % misc_intvl) + 5)
276
dotest(int testers,int me,int fd)277 static void dotest(int testers, int me, int fd)
278 {
279 char *bits, *hold_bits;
280 char val;
281 int count, collide, chunk, whenmisc, xfr, i;
282
283 /* Stuff for the readv call */
284 struct iovec r_iovec[MAXIOVCNT];
285 int r_ioveclen;
286
287 /* Stuff for the writev call */
288 struct iovec val_iovec[MAXIOVCNT];
289
290 struct iovec zero_iovec[MAXIOVCNT];
291 int w_ioveclen;
292 struct stat stat;
293
294 nchunks = max_size / csize;
295 whenmisc = 0;
296
297 if ((bits = malloc((nchunks + 7) / 8)) == NULL) {
298 tst_brkm(TBROK, NULL, "\tmalloc failed(bits)");
299 }
300 if ((hold_bits = malloc((nchunks + 7) / 8)) == NULL) {
301 tst_brkm(TBROK, NULL, "\tmalloc failed(hlod_bits)");
302 }
303
304 /*Allocate memory for the iovec buffers and init the iovec arrays
305 */
306 r_ioveclen = w_ioveclen = csize / MAXIOVCNT;
307
308 /* Please note that the above statement implies that csize
309 * be evenly divisible by MAXIOVCNT.
310 */
311
312 for (i = 0; i < MAXIOVCNT; i++) {
313 if ((r_iovec[i].iov_base = calloc(r_ioveclen, 1)) == NULL) {
314 tst_brkm(TFAIL, NULL,
315 "\tmalloc failed(r_iovec[i].iov_base)");
316 }
317 r_iovec[i].iov_len = r_ioveclen;
318
319 /* Allocate unused memory areas between all the buffers to
320 * make things more diffult for the OS.
321 */
322
323 if (malloc((i + 1) * 8) == NULL) {
324 tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)");
325 }
326 if ((val_iovec[i].iov_base = calloc(w_ioveclen, 1)) == NULL) {
327 tst_resm(TBROK, "\tmalloc failed(val_iovec[i]");
328 exit(1);
329 }
330 val_iovec[i].iov_len = w_ioveclen;
331
332 if (malloc((i + 1) * 8) == NULL) {
333 tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)");
334 }
335 if ((zero_iovec[i].iov_base = calloc(w_ioveclen, 1)) == NULL) {
336 tst_brkm(TBROK, NULL, "\tmalloc failed(zero_iover)");
337 }
338 zero_iovec[i].iov_len = w_ioveclen;
339
340 if (malloc((i + 1) * 8) == NULL) {
341 tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)");
342 }
343 }
344 /*
345 * No init sectors; allow file to be sparse.
346 */
347 val = (64 / testers) * me + 1;
348
349 /*
350 * For each iteration:
351 * zap bits array
352 * loop
353 * pick random chunk, read it.
354 * if corresponding bit off {
355 * verify = 0. (sparse file)
356 * ++count;
357 * } else
358 * verify = val.
359 * write "val" on it.
360 * repeat unitl count = nchunks.
361 * ++val.
362 */
363
364 srand(getpid());
365 if (misc_intvl)
366 whenmisc = NEXTMISC;
367
368 while (iterations-- > 0) {
369 for (i = 0; i < NMISC; i++)
370 misc_cnt[i] = 0;
371 ftruncate(fd, 0);
372 file_max = 0;
373 memset(bits, 0, (nchunks + 7) / 8);
374 memset(hold_bits, 0, (nchunks + 7) / 8);
375
376 /* Have to fill the val and zero iov buffers in a different manner
377 */
378 for (i = 0; i < MAXIOVCNT; i++) {
379 memset(val_iovec[i].iov_base, val,
380 val_iovec[i].iov_len);
381 memset(zero_iovec[i].iov_base, 0,
382 zero_iovec[i].iov_len);
383
384 }
385 count = 0;
386 collide = 0;
387 while (count < nchunks) {
388 chunk = rand() % nchunks;
389 /*
390 * Read it.
391 */
392 if (lseek64(fd, CHUNK(chunk), 0) < 0) {
393 tst_brkm(TFAIL,
394 NULL,
395 "\tTest[%d]: lseek64(0) fail at %Lx, errno = %d.",
396 me, CHUNK(chunk), errno);
397 }
398 if ((xfr = readv(fd, &r_iovec[0], MAXIOVCNT)) < 0) {
399 tst_brkm(TFAIL,
400 NULL,
401 "\tTest[%d]: readv fail at %Lx, errno = %d.",
402 me, CHUNK(chunk), errno);
403 }
404 /*
405 * If chunk beyond EOF just write on it.
406 * Else if bit off, haven't seen it yet.
407 * Else, have. Verify values.
408 */
409 if (CHUNK(chunk) >= file_max) {
410 bits[chunk / 8] |= (1 << (chunk % 8));
411 ++count;
412 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
413 if (xfr != csize) {
414 tst_brkm(TFAIL,
415 NULL,
416 "\tTest[%d]: xfr=%d != %d, zero read.",
417 me, xfr, csize);
418 }
419 for (i = 0; i < MAXIOVCNT; i++) {
420 if (memcmp
421 (r_iovec[i].iov_base,
422 zero_iovec[i].iov_base,
423 r_iovec[i].iov_len)) {
424 tst_resm(TFAIL,
425 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%llx, should be 0.",
426 me, CHUNK(chunk), val,
427 count, xfr, file_max);
428 tst_resm(TINFO,
429 "\tTest[%d]: last_trunc = 0x%x",
430 me, last_trunc);
431 fstat(fd, &stat);
432 tst_resm(TINFO,
433 "\tStat: size=%llx, ino=%x",
434 stat.st_size, (unsigned)stat.st_ino);
435 sync();
436 ft_dumpiov(&r_iovec[i]);
437 ft_dumpbits(bits,
438 (nchunks + 7) / 8);
439 ft_orbits(hold_bits, bits,
440 (nchunks + 7) / 8);
441 tst_resm(TINFO, "\tHold ");
442 ft_dumpbits(hold_bits,
443 (nchunks + 7) / 8);
444 tst_exit();
445 }
446 }
447 bits[chunk / 8] |= (1 << (chunk % 8));
448 ++count;
449 } else {
450 if (xfr != csize) {
451 tst_brkm(TFAIL,
452 NULL,
453 "\tTest[%d]: xfr=%d != %d, val read.",
454 me, xfr, csize);
455 }
456 ++collide;
457 for (i = 0; i < MAXIOVCNT; i++) {
458 if (memcmp
459 (r_iovec[i].iov_base,
460 val_iovec[i].iov_base,
461 r_iovec[i].iov_len)) {
462 tst_resm(TFAIL,
463 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%llx.",
464 me, CHUNK(chunk), val,
465 count, xfr, file_max);
466 tst_resm(TINFO,
467 "\tTest[%d]: last_trunc = 0x%x",
468 me, last_trunc);
469 fstat(fd, &stat);
470 tst_resm(TINFO,
471 "\tStat: size=%llx, ino=%x",
472 stat.st_size, (unsigned)stat.st_ino);
473 sync();
474 ft_dumpiov(&r_iovec[i]);
475 ft_dumpbits(bits,
476 (nchunks + 7) / 8);
477 ft_orbits(hold_bits, bits,
478 (nchunks + 7) / 8);
479 tst_resm(TINFO, "\tHold ");
480 ft_dumpbits(hold_bits,
481 (nchunks + 7) / 8);
482 tst_exit();
483 }
484 }
485 }
486 /*
487 * Writev it.
488 */
489 if (lseek64(fd, -((off64_t) xfr), 1) < 0) {
490 tst_brkm(TFAIL,
491 NULL,
492 "\tTest[%d]: lseek64(1) fail at %Lx, errno = %d.",
493 me, CHUNK(chunk), errno);
494 }
495 if ((xfr =
496 writev(fd, &val_iovec[0], MAXIOVCNT)) < csize) {
497 if (errno == ENOSPC) {
498 tst_resm(TFAIL,
499 "\tTest[%d]: no space, exiting.",
500 me);
501 fsync(fd);
502 tst_exit();
503 }
504 tst_brkm(TFAIL,
505 NULL,
506 "\tTest[%d]: writev fail at %Lx xfr %d, errno = %d.",
507 me, CHUNK(chunk), xfr, errno);
508 }
509 if (CHUNK(chunk) + csize > file_max)
510 file_max = CHUNK(chunk) + csize;
511 /*
512 * If hit "misc" interval, do it.
513 */
514 if (misc_intvl && --whenmisc <= 0) {
515 ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
516 domisc(me, fd, bits);
517 whenmisc = NEXTMISC;
518 }
519 if (count + collide > 2 * nchunks)
520 break;
521 }
522
523 /*
524 * End of iteration, maybe before doing all chunks.
525 */
526 fsync(fd);
527 ++misc_cnt[m_fsync];
528 //tst_resm(TINFO, "\tTest{%d} val %d done, count = %d, collide = {%d}",
529 // me, val, count, collide);
530 //for (i = 0; i < NMISC; i++)
531 // tst_resm(TINFO, "\t\tTest{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
532 ++val;
533 }
534 }
535
536 /*
537 * domisc()
538 * Inject misc syscalls into the thing.
539 */
domisc(int me,int fd,char * bits)540 static void domisc(int me, int fd, char *bits)
541 {
542 int chunk;
543 struct stat sb;
544
545 if (type > m_fstat)
546 type = m_fsync;
547 switch (type) {
548 case m_fsync:
549 if (fsync(fd) < 0) {
550 tst_brkm(TFAIL, NULL, "\tTest[%d]: fsync error %d.",
551 me,
552 errno);
553 }
554 break;
555 case m_trunc:
556 chunk = rand() % (file_max / csize);
557 file_max = CHUNK(chunk);
558 last_trunc = file_max;
559 if (tr_flag) {
560 if (ftruncate(fd, file_max) < 0) {
561 tst_brkm(TFAIL,
562 NULL,
563 "\tTest[%d]: ftruncate error %d @ 0x%llx.",
564 me, errno, file_max);
565 }
566 tr_flag = 0;
567 } else {
568 if (truncate(test_name, file_max) < 0) {
569 tst_brkm(TFAIL,
570 NULL,
571 "\tTest[%d]: truncate error %d @ 0x%llx.",
572 me, errno, file_max);
573 }
574 tr_flag = 1;
575 }
576 for (; chunk % 8 != 0; chunk++)
577 bits[chunk / 8] &= ~(1 << (chunk % 8));
578 for (; chunk < nchunks; chunk += 8)
579 bits[chunk / 8] = 0;
580 break;
581 case m_sync:
582 sync();
583 break;
584 case m_fstat:
585 if (fstat(fd, &sb) < 0) {
586 tst_brkm(TFAIL, NULL, "\tTest[%d]: fstat() error %d.",
587 me,
588 errno);
589 }
590 if (sb.st_size != file_max) {
591 tst_brkm(TFAIL,
592 NULL, "\tTest[%d]: fstat() mismatch; st_size=%"
593 PRIx64 ",file_max=%llx.", me,
594 (int64_t) sb.st_size, file_max);
595 }
596 break;
597 }
598
599 ++misc_cnt[type];
600 ++type;
601 }
602
603 /* term()
604 *
605 * This is called when a SIGTERM signal arrives.
606 */
term(int sig LTP_ATTRIBUTE_UNUSED)607 static void term(int sig LTP_ATTRIBUTE_UNUSED)
608 {
609 int i;
610
611 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
612
613 /*
614 * If run by hand we like to have the parent send the signal to
615 * the child processes. This makes life easy.
616 */
617 if (parent_pid == getpid()) {
618 for (i = 0; i < nchild; i++)
619 if (pidlist[i])
620 kill(pidlist[i], SIGTERM);
621 return;
622 }
623
624 tst_resm(TINFO, "\tunlinking '%s'", test_name);
625
626 close(fd);
627 if (unlink(test_name))
628 tst_resm(TBROK, "Unlink of '%s' failed, errno = %d.",
629 test_name, errno);
630 else
631 tst_resm(TINFO, "Unlink of '%s' successful.", test_name);
632
633 tst_exit();
634 }
635