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