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