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