1 /* 2 * 3 * Copyright (c) International Business Machines Corp., 2002 4 * Copyright (c) Cyril Hrubis chrubis (at) 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 * ftest01.c -- test file I/O (ported from SPIE section2, filesuite, by Airong Zhang) 24 * 25 * CALLS 26 * lseek, read, write 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 * 39 * RESTRICTIONS 40 * Runs a long time with default args - can take others on input 41 * line. Use with "term mode". 42 * If run on vax the ftruncate will not be random - will always go to 43 * start of file. NOTE: produces a very high load average!! 44 * 45 * CAUTION!! 46 * If a file is supplied to this program with the "-f" option 47 * it will be removed with a system("rm -rf filename") call. 48 * 49 */ 50 #define _GNU_SOURCE 1 51 #include <stdio.h> 52 #include <sys/types.h> 53 #include <sys/wait.h> 54 #include <sys/stat.h> 55 #include <sys/param.h> 56 #include <errno.h> 57 #include <fcntl.h> 58 #include <signal.h> 59 #include <unistd.h> 60 #include <inttypes.h> 61 #include "test.h" 62 #include "safe_macros.h" 63 #include "libftest.h" 64 65 char *TCID = "ftest01"; 66 int TST_TOTAL = 1; 67 68 static void setup(void); 69 static void runtest(void); 70 static void dotest(int, int, int); 71 static void domisc(int, int, char *); 72 static void cleanup(void); 73 static void term(int sig); 74 75 #define PASSED 1 76 #define FAILED 0 77 78 #define MAXCHILD 25 79 #define K_1 1024 80 #define K_2 2048 81 #define K_4 4096 82 83 static int csize; /* chunk size */ 84 static int iterations; /* # total iterations */ 85 static int max_size; /* max file size */ 86 static int misc_intvl; /* for doing misc things; 0 ==> no */ 87 static int nchild; /* how many children */ 88 static int fd; /* file descriptor used by child */ 89 static int parent_pid; 90 static int pidlist[MAXCHILD]; 91 static char test_name[2]; 92 93 static char fuss[MAXPATHLEN]; /* directory to do this in */ 94 static char homedir[MAXPATHLEN]; /* where we started */ 95 96 static int local_flag; 97 98 int main(int ac, char *av[]) 99 { 100 int lc; 101 102 tst_parse_opts(ac, av, NULL, NULL); 103 104 setup(); 105 106 for (lc = 0; TEST_LOOPING(lc); lc++) { 107 108 runtest(); 109 110 if (local_flag == PASSED) 111 tst_resm(TPASS, "Test passed."); 112 else 113 tst_resm(TFAIL, "Test failed."); 114 } 115 116 cleanup(); 117 tst_exit(); 118 119 } 120 121 static void setup(void) 122 { 123 124 tst_tmpdir(); 125 getcwd(homedir, sizeof(homedir)); 126 parent_pid = getpid(); 127 128 if (!fuss[0]) 129 sprintf(fuss, "./ftest1.%d", getpid()); 130 131 mkdir(fuss, 0755); 132 133 SAFE_CHDIR(NULL, fuss); 134 135 /* 136 * Default values for run conditions. 137 */ 138 iterations = 10; 139 nchild = 5; 140 csize = K_2; /* should run with 1, 2, and 4 K sizes */ 141 max_size = K_1 * K_1; 142 misc_intvl = 10; 143 144 if (sigset(SIGTERM, term) == SIG_ERR) { 145 tst_brkm(TBROK | TERRNO, NULL, "sigset failed"); 146 } 147 148 local_flag = PASSED; 149 } 150 151 static void runtest(void) 152 { 153 pid_t pid; 154 int i, child, count, nwait, status; 155 156 nwait = 0; 157 158 for (i = 0; i < nchild; i++) { 159 160 test_name[0] = 'a' + i; 161 test_name[1] = '\0'; 162 fd = SAFE_OPEN(NULL, test_name, O_RDWR | O_CREAT | O_TRUNC, 163 0666); 164 165 if ((child = fork()) == 0) { 166 dotest(nchild, i, fd); 167 exit(0); 168 } 169 170 close(fd); 171 172 if (child < 0) { 173 tst_brkm(TBROK | TERRNO, NULL, "fork failed"); 174 } else { 175 pidlist[i] = child; 176 nwait++; 177 } 178 } 179 180 /* 181 * Wait for children to finish. 182 */ 183 count = 0; 184 while (1) { 185 if ((child = wait(&status)) >= 0) { 186 if (status) { 187 tst_resm(TFAIL, 188 "Test{%d} failed, expected 0 exit", 189 child); 190 local_flag = FAILED; 191 } 192 ++count; 193 } else { 194 if (errno != EINTR) 195 break; 196 } 197 } 198 199 /* 200 * Should have collected all children. 201 */ 202 if (count != nwait) { 203 tst_resm(TFAIL, "Wrong # children waited on, count = %d", 204 count); 205 local_flag = FAILED; 206 } 207 208 if (local_flag == PASSED) 209 tst_resm(TPASS, "Test passed in fork and wait."); 210 else 211 tst_resm(TFAIL, "Test failed in fork and wait."); 212 213 chdir(homedir); 214 pid = fork(); 215 216 if (pid < 0) { 217 tst_brkm(TBROK | TERRNO, sync, "fork failed"); 218 tst_exit(); 219 } 220 221 if (pid == 0) { 222 execl("/bin/rm", "rm", "-rf", fuss, NULL); 223 exit(1); 224 } 225 226 wait(&status); 227 228 if (status) 229 tst_resm(TINFO, "CAUTION - ftest1, '%s' may not be removed", 230 fuss); 231 232 sync(); 233 } 234 235 /* 236 * dotest() 237 * Children execute this. 238 * 239 * Randomly read/mod/write chunks with known pattern and check. 240 * When fill sectors, iterate. 241 */ 242 243 #define NMISC 4 244 enum m_type { m_fsync, m_trunc, m_sync, m_fstat }; 245 char *m_str[] = { "fsync", "trunc", "sync", "fstat" }; 246 247 int misc_cnt[NMISC]; /* counts # of each kind of misc */ 248 int file_max; /* file-max size */ 249 int nchunks; 250 int last_trunc = -1; 251 int tr_flag; 252 enum m_type type = m_fsync; 253 254 #define CHUNK(i) ((i) * csize) 255 #define NEXTMISC ((rand() % misc_intvl) + 5) 256 257 /* XXX (garrcoop): should not be using libltp as it runs forked. */ 258 static void dotest(int testers, int me, int fd) 259 { 260 char *bits, *hold_bits, *buf, *val_buf, *zero_buf; 261 char val; 262 int count, collide, chunk, whenmisc, xfr, i; 263 struct stat stat; 264 265 nchunks = max_size / csize; 266 267 if ((bits = calloc((nchunks + 7) / 8, 1)) == 0) { 268 tst_brkm(TBROK, 269 NULL, 270 "Test broken due to inability of malloc(bits)."); 271 } 272 273 if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == 0) { 274 tst_brkm(TBROK, 275 NULL, 276 "Test broken due to inability of malloc(hold_bits)."); 277 } 278 279 if ((buf = (calloc(csize, 1))) == 0) { 280 tst_brkm(TBROK, NULL, 281 "Test broken due to inability of malloc(buf)."); 282 } 283 284 if ((val_buf = (calloc(csize, 1))) == 0) { 285 tst_brkm(TBROK, 286 NULL, 287 "Test broken due to inability of malloc(val_buf)."); 288 } 289 290 if ((zero_buf = (calloc(csize, 1))) == 0) { 291 tst_brkm(TBROK, 292 NULL, 293 "Test broken due to inability of malloc(zero_buf)."); 294 } 295 296 /* 297 * No init sectors; allow file to be sparse. 298 */ 299 val = (64 / testers) * me + 1; 300 301 /* 302 * For each iteration: 303 * zap bits array 304 * loop: 305 * pick random chunk, read it. 306 * if corresponding bit off { 307 * verify == 0. (sparse file) 308 * ++count; 309 * } else 310 * verify == val. 311 * write "val" on it. 312 * repeat until count = nchunks. 313 * ++val. 314 */ 315 srand(getpid()); 316 317 if (misc_intvl) 318 whenmisc = NEXTMISC; 319 320 while (iterations-- > 0) { 321 for (i = 0; i < NMISC; i++) 322 misc_cnt[i] = 0; 323 ftruncate(fd, 0); 324 file_max = 0; 325 memset(bits, 0, (nchunks + 7) / 8); 326 memset(hold_bits, 0, (nchunks + 7) / 8); 327 memset(val_buf, val, csize); 328 memset(zero_buf, 0, csize); 329 count = 0; 330 collide = 0; 331 while (count < nchunks) { 332 chunk = rand() % nchunks; 333 /* 334 * Read it. 335 */ 336 if (lseek(fd, CHUNK(chunk), 0) < 0) { 337 tst_brkm(TFAIL, 338 NULL, 339 "Test[%d]: lseek(0) fail at %x, errno = %d.", 340 me, CHUNK(chunk), errno); 341 } 342 if ((xfr = read(fd, buf, csize)) < 0) { 343 tst_brkm(TFAIL, 344 NULL, 345 "Test[%d]: read fail at %x, errno = %d.", 346 me, CHUNK(chunk), errno); 347 } 348 /* 349 * If chunk beyond EOF just write on it. 350 * Else if bit off, haven't seen it yet. 351 * Else, have. Verify values. 352 */ 353 if (CHUNK(chunk) >= file_max) { 354 bits[chunk / 8] |= (1 << (chunk % 8)); 355 ++count; 356 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) { 357 if (xfr != csize) { 358 tst_brkm(TFAIL, 359 NULL, 360 "Test[%d]: xfr=%d != %d, zero read.", 361 me, xfr, csize); 362 } 363 if (memcmp(buf, zero_buf, csize)) { 364 tst_resm(TFAIL, 365 "Test[%d] bad verify @ 0x%x for val %d " 366 "count %d xfr %d file_max 0x%x, should be %d.", 367 me, CHUNK(chunk), val, count, 368 xfr, file_max, zero_buf[0]); 369 tst_resm(TINFO, 370 "Test[%d]: last_trunc = 0x%x", 371 me, last_trunc); 372 fstat(fd, &stat); 373 tst_resm(TINFO, 374 "\tStat: size=%llx, ino=%x", 375 stat.st_size, (unsigned)stat.st_ino); 376 sync(); 377 ft_dumpbuf(buf, csize); 378 ft_dumpbits(bits, (nchunks + 7) / 8); 379 ft_orbits(hold_bits, bits, 380 (nchunks + 7) / 8); 381 tst_resm(TINFO, "Hold "); 382 ft_dumpbits(hold_bits, 383 (nchunks + 7) / 8); 384 tst_exit(); 385 } 386 bits[chunk / 8] |= (1 << (chunk % 8)); 387 ++count; 388 } else { 389 if (xfr != csize) { 390 tst_brkm(TFAIL, 391 NULL, 392 "\tTest[%d]: xfr=%d != %d, val read.", 393 me, xfr, csize); 394 } 395 ++collide; 396 if (memcmp(buf, val_buf, csize)) { 397 tst_resm(TFAIL, 398 "Test[%d] bad verify @ 0x%x for val %d " 399 "count %d xfr %d file_max 0x%x.", 400 me, CHUNK(chunk), val, count, 401 xfr, file_max); 402 tst_resm(TINFO, 403 "Test[%d]: last_trunc = 0x%x", 404 me, last_trunc); 405 fstat(fd, &stat); 406 tst_resm(TINFO, 407 "\tStat: size=%llx, ino=%x", 408 stat.st_size, (unsigned)stat.st_ino); 409 sync(); 410 ft_dumpbuf(buf, csize); 411 ft_dumpbits(bits, (nchunks + 7) / 8); 412 ft_orbits(hold_bits, bits, 413 (nchunks + 7) / 8); 414 tst_resm(TINFO, "Hold "); 415 ft_dumpbits(hold_bits, 416 (nchunks + 7) / 8); 417 tst_exit(); 418 } 419 } 420 /* 421 * Write it. 422 */ 423 if (lseek(fd, -xfr, 1) < 0) { 424 tst_brkm(TFAIL, 425 NULL, 426 "Test[%d]: lseek(1) fail at %x, errno = %d.", 427 me, CHUNK(chunk), errno); 428 } 429 if ((xfr = write(fd, val_buf, csize)) < csize) { 430 if (errno == ENOSPC) { 431 tst_resm(TFAIL, 432 "Test[%d]: no space, exiting.", 433 me); 434 fsync(fd); 435 tst_exit(); 436 } 437 tst_brkm(TFAIL, 438 NULL, 439 "Test[%d]: write fail at %x xfr %d, errno = %d.", 440 me, CHUNK(chunk), xfr, errno); 441 } 442 if (CHUNK(chunk) + csize > file_max) 443 file_max = CHUNK(chunk) + csize; 444 /* 445 * If hit "misc" interval, do it. 446 */ 447 if (misc_intvl && --whenmisc <= 0) { 448 ft_orbits(hold_bits, bits, (nchunks + 7) / 8); 449 domisc(me, fd, bits); 450 whenmisc = NEXTMISC; 451 } 452 if (count + collide > 2 * nchunks) 453 break; 454 } 455 456 /* 457 * End of iteration, maybe before doing all chunks. 458 */ 459 fsync(fd); 460 ++misc_cnt[m_fsync]; 461 //tst_resm(TINFO, "Test{%d} val %d done, count = %d, collide = {%d}", 462 // me, val, count, collide); 463 //for (i = 0; i < NMISC; i++) 464 // tst_resm(TINFO, "Test{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]); 465 ++val; 466 } 467 } 468 469 /* 470 * domisc() 471 * Inject misc syscalls into the thing. 472 */ 473 static void domisc(int me, int fd, char *bits) 474 { 475 int chunk; 476 struct stat sb; 477 478 if (type > m_fstat) 479 type = m_fsync; 480 switch (type) { 481 case m_fsync: 482 if (fsync(fd) < 0) { 483 tst_brkm(TFAIL | TERRNO, NULL, 484 "Test[%d]: fsync failed.", me); 485 } 486 break; 487 case m_trunc: 488 chunk = rand() % (file_max / csize); 489 file_max = CHUNK(chunk); 490 last_trunc = file_max; 491 if (tr_flag) { 492 if (ftruncate(fd, file_max) < 0) { 493 tst_brkm(TFAIL | TERRNO, NULL, 494 "Test[%d]: ftruncate failed @ 0x%x.", 495 me, file_max); 496 } 497 tr_flag = 0; 498 } else { 499 if (truncate(test_name, file_max) < 0) { 500 tst_brkm(TFAIL | TERRNO, NULL, 501 "Test[%d]: truncate failed @ 0x%x.", 502 me, file_max); 503 } 504 tr_flag = 1; 505 } 506 for (; chunk % 8 != 0; chunk++) 507 bits[chunk / 8] &= ~(1 << (chunk % 8)); 508 for (; chunk < nchunks; chunk += 8) 509 bits[chunk / 8] = 0; 510 break; 511 case m_sync: 512 sync(); 513 break; 514 case m_fstat: 515 if (fstat(fd, &sb) < 0) 516 tst_brkm(TFAIL | TERRNO, NULL, 517 "\tTest[%d]: fstat failed", me); 518 if (sb.st_size != file_max) 519 tst_brkm(TFAIL, NULL, 520 "\tTest[%d]: fstat() mismatch; st_size=%lu, " 521 "file_max=%x.", me, sb.st_size, file_max); 522 break; 523 } 524 525 ++misc_cnt[type]; 526 ++type; 527 } 528 529 /* 530 * SIGTERM signal handler. 531 */ 532 static void term(int sig LTP_ATTRIBUTE_UNUSED) 533 { 534 int i; 535 536 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid()); 537 538 /* 539 * If run by hand we like to have the parent send the signal to 540 * the child processes. 541 */ 542 if (parent_pid == getpid()) { 543 for (i = 0; i < nchild; i++) 544 if (pidlist[i]) 545 kill(pidlist[i], SIGTERM); 546 tst_exit(); 547 } 548 549 tst_resm(TINFO, "\tunlinking '%s'", test_name); 550 551 close(fd); 552 553 if (unlink(test_name) == -1) 554 tst_resm(TBROK | TERRNO, "unlink failed"); 555 556 tst_exit(); 557 } 558 559 static void cleanup(void) 560 { 561 562 tst_rmdir(); 563 } 564