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