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 * 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 const int misc_intvl = 10; /* 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
main(int ac,char * av[])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
setup(void)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
143 if (sigset(SIGTERM, term) == SIG_ERR) {
144 tst_brkm(TBROK | TERRNO, NULL, "sigset failed");
145 }
146
147 local_flag = PASSED;
148 }
149
runtest(void)150 static void runtest(void)
151 {
152 pid_t pid;
153 int i, child, count, nwait, status;
154
155 nwait = 0;
156
157 for (i = 0; i < nchild; i++) {
158
159 test_name[0] = 'a' + i;
160 test_name[1] = '\0';
161 fd = SAFE_OPEN(NULL, test_name, O_RDWR | O_CREAT | O_TRUNC,
162 0666);
163
164 if ((child = fork()) == 0) {
165 dotest(nchild, i, fd);
166 exit(0);
167 }
168
169 close(fd);
170
171 if (child < 0) {
172 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
173 } else {
174 pidlist[i] = child;
175 nwait++;
176 }
177 }
178
179 /*
180 * Wait for children to finish.
181 */
182 count = 0;
183 while (1) {
184 if ((child = wait(&status)) >= 0) {
185 if (status) {
186 tst_resm(TFAIL,
187 "Test{%d} failed, expected 0 exit",
188 child);
189 local_flag = FAILED;
190 }
191 ++count;
192 } else {
193 if (errno != EINTR)
194 break;
195 }
196 }
197
198 /*
199 * Should have collected all children.
200 */
201 if (count != nwait) {
202 tst_resm(TFAIL, "Wrong # children waited on, count = %d",
203 count);
204 local_flag = FAILED;
205 }
206
207 if (local_flag == PASSED)
208 tst_resm(TPASS, "Test passed in fork and wait.");
209 else
210 tst_resm(TFAIL, "Test failed in fork and wait.");
211
212 chdir(homedir);
213 pid = fork();
214
215 if (pid < 0) {
216 tst_brkm(TBROK | TERRNO, sync, "fork failed");
217 tst_exit();
218 }
219
220 if (pid == 0) {
221 execl("/bin/rm", "rm", "-rf", fuss, NULL);
222 exit(1);
223 }
224
225 wait(&status);
226
227 if (status)
228 tst_resm(TINFO, "CAUTION - ftest1, '%s' may not be removed",
229 fuss);
230
231 sync();
232 }
233
234 /*
235 * dotest()
236 * Children execute this.
237 *
238 * Randomly read/mod/write chunks with known pattern and check.
239 * When fill sectors, iterate.
240 */
241
242 #define NMISC 4
243 enum m_type { m_fsync, m_trunc, m_sync, m_fstat };
244 char *m_str[] = { "fsync", "trunc", "sync", "fstat" };
245
246 int misc_cnt[NMISC]; /* counts # of each kind of misc */
247 int file_max; /* file-max size */
248 int nchunks;
249 int last_trunc = -1;
250 int tr_flag;
251 enum m_type type = m_fsync;
252
253 #define CHUNK(i) ((i) * csize)
254 #define NEXTMISC ((rand() % misc_intvl) + 5)
255
256 /* XXX (garrcoop): should not be using libltp as it runs forked. */
dotest(int testers,int me,int fd)257 static void dotest(int testers, int me, int fd)
258 {
259 char *bits, *hold_bits, *buf, *val_buf, *zero_buf;
260 char val;
261 int count, collide, chunk, whenmisc, xfr, i;
262 struct stat stat;
263
264 nchunks = max_size / csize;
265
266 if ((bits = calloc((nchunks + 7) / 8, 1)) == 0) {
267 tst_brkm(TBROK,
268 NULL,
269 "Test broken due to inability of malloc(bits).");
270 }
271
272 if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == 0) {
273 tst_brkm(TBROK,
274 NULL,
275 "Test broken due to inability of malloc(hold_bits).");
276 }
277
278 if ((buf = (calloc(csize, 1))) == 0) {
279 tst_brkm(TBROK, NULL,
280 "Test broken due to inability of malloc(buf).");
281 }
282
283 if ((val_buf = (calloc(csize, 1))) == 0) {
284 tst_brkm(TBROK,
285 NULL,
286 "Test broken due to inability of malloc(val_buf).");
287 }
288
289 if ((zero_buf = (calloc(csize, 1))) == 0) {
290 tst_brkm(TBROK,
291 NULL,
292 "Test broken due to inability of malloc(zero_buf).");
293 }
294
295 /*
296 * No init sectors; allow file to be sparse.
297 */
298 val = (64 / testers) * me + 1;
299
300 /*
301 * For each iteration:
302 * zap bits array
303 * loop:
304 * pick random chunk, read it.
305 * if corresponding bit off {
306 * verify == 0. (sparse file)
307 * ++count;
308 * } else
309 * verify == val.
310 * write "val" on it.
311 * repeat until count = nchunks.
312 * ++val.
313 */
314 srand(getpid());
315
316 if (misc_intvl)
317 whenmisc = NEXTMISC;
318
319 while (iterations-- > 0) {
320 for (i = 0; i < NMISC; i++)
321 misc_cnt[i] = 0;
322 ftruncate(fd, 0);
323 file_max = 0;
324 memset(bits, 0, (nchunks + 7) / 8);
325 memset(hold_bits, 0, (nchunks + 7) / 8);
326 memset(val_buf, val, csize);
327 memset(zero_buf, 0, csize);
328 count = 0;
329 collide = 0;
330 while (count < nchunks) {
331 chunk = rand() % nchunks;
332 /*
333 * Read it.
334 */
335 if (lseek(fd, CHUNK(chunk), 0) < 0) {
336 tst_brkm(TFAIL,
337 NULL,
338 "Test[%d]: lseek(0) fail at %x, errno = %d.",
339 me, CHUNK(chunk), errno);
340 }
341 if ((xfr = read(fd, buf, csize)) < 0) {
342 tst_brkm(TFAIL,
343 NULL,
344 "Test[%d]: read fail at %x, errno = %d.",
345 me, CHUNK(chunk), errno);
346 }
347 /*
348 * If chunk beyond EOF just write on it.
349 * Else if bit off, haven't seen it yet.
350 * Else, have. Verify values.
351 */
352 if (CHUNK(chunk) >= file_max) {
353 bits[chunk / 8] |= (1 << (chunk % 8));
354 ++count;
355 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
356 if (xfr != csize) {
357 tst_brkm(TFAIL,
358 NULL,
359 "Test[%d]: xfr=%d != %d, zero read.",
360 me, xfr, csize);
361 }
362 if (memcmp(buf, zero_buf, csize)) {
363 tst_resm(TFAIL,
364 "Test[%d] bad verify @ 0x%x for val %d "
365 "count %d xfr %d file_max 0x%x, should be %d.",
366 me, CHUNK(chunk), val, count,
367 xfr, file_max, zero_buf[0]);
368 tst_resm(TINFO,
369 "Test[%d]: last_trunc = 0x%x",
370 me, last_trunc);
371 fstat(fd, &stat);
372 tst_resm(TINFO,
373 "\tStat: size=%llx, ino=%x",
374 stat.st_size, (unsigned)stat.st_ino);
375 sync();
376 ft_dumpbuf(buf, csize);
377 ft_dumpbits(bits, (nchunks + 7) / 8);
378 ft_orbits(hold_bits, bits,
379 (nchunks + 7) / 8);
380 tst_resm(TINFO, "Hold ");
381 ft_dumpbits(hold_bits,
382 (nchunks + 7) / 8);
383 tst_exit();
384 }
385 bits[chunk / 8] |= (1 << (chunk % 8));
386 ++count;
387 } else {
388 if (xfr != csize) {
389 tst_brkm(TFAIL,
390 NULL,
391 "\tTest[%d]: xfr=%d != %d, val read.",
392 me, xfr, csize);
393 }
394 ++collide;
395 if (memcmp(buf, val_buf, csize)) {
396 tst_resm(TFAIL,
397 "Test[%d] bad verify @ 0x%x for val %d "
398 "count %d xfr %d file_max 0x%x.",
399 me, CHUNK(chunk), val, count,
400 xfr, file_max);
401 tst_resm(TINFO,
402 "Test[%d]: last_trunc = 0x%x",
403 me, last_trunc);
404 fstat(fd, &stat);
405 tst_resm(TINFO,
406 "\tStat: size=%llx, ino=%x",
407 stat.st_size, (unsigned)stat.st_ino);
408 sync();
409 ft_dumpbuf(buf, csize);
410 ft_dumpbits(bits, (nchunks + 7) / 8);
411 ft_orbits(hold_bits, bits,
412 (nchunks + 7) / 8);
413 tst_resm(TINFO, "Hold ");
414 ft_dumpbits(hold_bits,
415 (nchunks + 7) / 8);
416 tst_exit();
417 }
418 }
419 /*
420 * Write it.
421 */
422 if (lseek(fd, -xfr, 1) < 0) {
423 tst_brkm(TFAIL,
424 NULL,
425 "Test[%d]: lseek(1) fail at %x, errno = %d.",
426 me, CHUNK(chunk), errno);
427 }
428 if ((xfr = write(fd, val_buf, csize)) < csize) {
429 if (errno == ENOSPC) {
430 tst_resm(TFAIL,
431 "Test[%d]: no space, exiting.",
432 me);
433 fsync(fd);
434 tst_exit();
435 }
436 tst_brkm(TFAIL,
437 NULL,
438 "Test[%d]: write fail at %x xfr %d, errno = %d.",
439 me, CHUNK(chunk), xfr, errno);
440 }
441 if (CHUNK(chunk) + csize > file_max)
442 file_max = CHUNK(chunk) + csize;
443 /*
444 * If hit "misc" interval, do it.
445 */
446 if (misc_intvl && --whenmisc <= 0) {
447 ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
448 domisc(me, fd, bits);
449 whenmisc = NEXTMISC;
450 }
451 if (count + collide > 2 * nchunks)
452 break;
453 }
454
455 /*
456 * End of iteration, maybe before doing all chunks.
457 */
458 fsync(fd);
459 ++misc_cnt[m_fsync];
460 //tst_resm(TINFO, "Test{%d} val %d done, count = %d, collide = {%d}",
461 // me, val, count, collide);
462 //for (i = 0; i < NMISC; i++)
463 // tst_resm(TINFO, "Test{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
464 ++val;
465 }
466 }
467
468 /*
469 * domisc()
470 * Inject misc syscalls into the thing.
471 */
domisc(int me,int fd,char * bits)472 static void domisc(int me, int fd, char *bits)
473 {
474 int chunk;
475 struct stat sb;
476
477 if (type > m_fstat)
478 type = m_fsync;
479 switch (type) {
480 case m_fsync:
481 if (fsync(fd) < 0) {
482 tst_brkm(TFAIL | TERRNO, NULL,
483 "Test[%d]: fsync failed.", me);
484 }
485 break;
486 case m_trunc:
487 chunk = rand() % (file_max / csize);
488 file_max = CHUNK(chunk);
489 last_trunc = file_max;
490 if (tr_flag) {
491 if (ftruncate(fd, file_max) < 0) {
492 tst_brkm(TFAIL | TERRNO, NULL,
493 "Test[%d]: ftruncate failed @ 0x%x.",
494 me, file_max);
495 }
496 tr_flag = 0;
497 } else {
498 if (truncate(test_name, file_max) < 0) {
499 tst_brkm(TFAIL | TERRNO, NULL,
500 "Test[%d]: truncate failed @ 0x%x.",
501 me, file_max);
502 }
503 tr_flag = 1;
504 }
505 for (; chunk % 8 != 0; chunk++)
506 bits[chunk / 8] &= ~(1 << (chunk % 8));
507 for (; chunk < nchunks; chunk += 8)
508 bits[chunk / 8] = 0;
509 break;
510 case m_sync:
511 sync();
512 break;
513 case m_fstat:
514 if (fstat(fd, &sb) < 0)
515 tst_brkm(TFAIL | TERRNO, NULL,
516 "\tTest[%d]: fstat failed", me);
517 if (sb.st_size != file_max)
518 tst_brkm(TFAIL, NULL,
519 "\tTest[%d]: fstat() mismatch; st_size=%lu, "
520 "file_max=%x.", me, sb.st_size, file_max);
521 break;
522 }
523
524 ++misc_cnt[type];
525 ++type;
526 }
527
528 /*
529 * SIGTERM signal handler.
530 */
term(int sig LTP_ATTRIBUTE_UNUSED)531 static void term(int sig LTP_ATTRIBUTE_UNUSED)
532 {
533 int i;
534
535 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
536
537 /*
538 * If run by hand we like to have the parent send the signal to
539 * the child processes.
540 */
541 if (parent_pid == getpid()) {
542 for (i = 0; i < nchild; i++)
543 if (pidlist[i])
544 kill(pidlist[i], SIGTERM);
545 tst_exit();
546 }
547
548 tst_resm(TINFO, "\tunlinking '%s'", test_name);
549
550 close(fd);
551
552 if (unlink(test_name) == -1)
553 tst_resm(TBROK | TERRNO, "unlink failed");
554
555 tst_exit();
556 }
557
cleanup(void)558 static void cleanup(void)
559 {
560
561 tst_rmdir();
562 }
563