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