124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew/*
224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   Copyright (c) International Business Machines  Corp., 2002
404f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modak *   Copyright (c) Cyril Hrubis chrubis@suse.cz 2009
524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   This program is free software;  you can redistribute it and/or modify
724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   it under the terms of the GNU General Public License as published by
824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   the Free Software Foundation; either version 2 of the License, or
924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   (at your option) any later version.
1024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
1124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   This program is distributed in the hope that it will be useful,
1224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
1324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
1424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   the GNU General Public License for more details.
1524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
1624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   You should have received a copy of the GNU General Public License
1724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *   along with this program;  if not, write to the Free Software
184548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew */
2024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
2124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew/*
2224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * NAME
2324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	ftest03.c -- test file I/O with readv and writev (ported from SPIE section2/filesuite/ftest4.c, by Airong Zhang)
2424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
2524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * CALLS
2624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	lseek, readv, writev,
2724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	truncate, ftruncate, fsync, sync, fstat
2824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
2924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * ALGORITHM
3024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	A bitmap is used to map pieces of a file.
3124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *      Loop: pick a random piece of the file
3224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *            if we haven't seen it before make sure it is zero,
3324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *            write pattern
3424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *            if we have seen it before make sure correct pattern.
3524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
3624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *      This was originally written by rbk - was program tfio.c
3724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	Modified by dale to integrate with test suites.
3824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	Modified by G. Stevens to use readv and writev.
3924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	Modofied by K. Hakim to integrate with SPIES.
4024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
4124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * RESTRICTIONS
4224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *  1.  Runs a long time with default args - can take others on input
4324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	line.  Use with "term mode".
4424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	If run on vax the ftruncate will not be random - will always go to
4524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	start of file.  NOTE: produces a very high load average!!
4624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
4724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *  2.  The "csize" argument must be evenly divisible by MAXIOVCNT.
4824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
4924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * CAUTION!!
5024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	If a file is supplied to this program with the "-f" option
5124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	it will be removed with a system("rm -rf filename") call.
52bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak *
5324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew */
5424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
5524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define _XOPEN_SOURCE 500
5624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include <sys/types.h>
5724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include <sys/param.h>
5824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include <sys/wait.h>
5924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include <sys/stat.h>
60f81795e0876f9dd0d95b42aec99a9a5de04d883dvapier#include <errno.h>
6124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include <sys/uio.h>
6224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include <fcntl.h>
63f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak#include <signal.h>
64f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak#include <stdio.h>
65f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak#include <inttypes.h>
6624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#include "test.h"
6704f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modak#include "libftest.h"
6824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
6924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiewchar *TCID = "ftest03";
7024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiewint TST_TOTAL = 1;
7124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
7224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define PASSED 1
7324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define FAILED 0
7424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
75f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void setup(void);
76f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void runtest(void);
77f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void dotest(int, int, int);
78354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic void domisc(int, int, char *);
79f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void term(int sig);
8024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
8104f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modak#define MAXCHILD	25
8224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define K_1		1024
8324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define K_2		2048
8424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define K_4		4096
8524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define	MAXIOVCNT	16
8624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
87354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic int csize;		/* chunk size */
88354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic int iterations;		/* # total iterations */
89354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic int max_size;		/* max file size */
90354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic int misc_intvl;		/* for doing misc things; 0 ==> no */
91354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic int nchild;		/* how many children */
92354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic int fd;			/* file descriptor used by child */
9304f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modakstatic int parent_pid;
9404f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modakstatic int pidlist[MAXCHILD];
95354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic char test_name[2];	/* childs test directory name */
9624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
97354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic char fuss[MAXPATHLEN];	/* directory to do this in */
98354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaostatic char homedir[MAXPATHLEN];	/* where we started */
9924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
10004f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modakstatic int local_flag;
101f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
102354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint main(int ac, char *av[])
10324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew{
104354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int lc;
10524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
106d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
10724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
10824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	setup();
109bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
11024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	for (lc = 0; TEST_LOOPING(lc); lc++) {
11124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
11224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		local_flag = PASSED;
11324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
114354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		runtest();
11524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
11624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (local_flag == PASSED) {
117354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TPASS, "Test passed.");
118354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else {
119354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TFAIL, "Test failed.");
120354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		}
121bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
12224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		tst_rmdir();
123f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		/* ??? so we are doing only one loop here ??? */
12424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		tst_exit();
125f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	}
126f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
1272c28215423293e443469a07ae7011135d058b671Garrett Cooper	tst_exit();
12824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew}
12924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
130f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void setup(void)
13124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew{
132f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	char wdbuf[MAXPATHLEN];
13324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
13424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/*
13524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * Make a directory to do this in; ignore error if already exists.
13624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * Save starting directory.
13724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 */
13824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	tst_tmpdir();
139f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
140f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	if (getcwd(homedir, sizeof(homedir)) == NULL) {
141354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, NULL, "getcwd() failed");
14224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
14324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
14424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	parent_pid = getpid();
14524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
14624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (!fuss[0])
147354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		sprintf(fuss, "%s/ftest03.%d", getcwd(wdbuf, sizeof(wdbuf)),
148354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			getpid());
14924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
15024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	mkdir(fuss, 0755);
15124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
15224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (chdir(fuss) < 0) {
153526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis		tst_brkm(TBROK, NULL, "\tCan't chdir(%s), error %d.", fuss,
154526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			 errno);
15524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
15624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
15724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/*
15824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * Default values for run conditions.
15924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 */
16024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	iterations = 10;
16124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	nchild = 5;
16224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	csize = K_2;		/* should run with 1, 2, and 4 K sizes */
16324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	max_size = K_1 * K_1;
16424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	misc_intvl = 10;
16524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
166f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	if (sigset(SIGTERM, term) == SIG_ERR) {
16724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		perror("sigset failed");
168526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis		tst_brkm(TBROK, NULL, " sigset failed: signo = 15");
16924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
17024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew}
17124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
172f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void runtest(void)
17324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew{
1747a73eab97ffec8b44079586a2cc74b28b52f9959Garrett Cooper	pid_t pid;
1757a73eab97ffec8b44079586a2cc74b28b52f9959Garrett Cooper	int child, count, i, nwait, status;
1767a73eab97ffec8b44079586a2cc74b28b52f9959Garrett Cooper
1777a73eab97ffec8b44079586a2cc74b28b52f9959Garrett Cooper	nwait = 0;
17824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
179df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	for (i = 0; i < nchild; i++) {
180f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
18124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		test_name[0] = 'a' + i;
18224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		test_name[1] = '\0';
183f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
184354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fd = open(test_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
185f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
18624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (fd < 0) {
187526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TBROK, NULL, "\tError %d creating %s/%s.",
188526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				 errno,
189354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 fuss, test_name);
19024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
191f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
192f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		if ((child = fork()) == 0) {
193f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			dotest(nchild, i, fd);
194f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			tst_exit();
19524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
19624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
19724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		close(fd);
19824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
19924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (child < 0) {
200354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_brkm(TBROK | TERRNO, NULL, "fork failed");
20124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		} else {
20224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			pidlist[i] = child;
20324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			nwait++;
20424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
20524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
20624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
20724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/*
20824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * Wait for children to finish.
20924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 */
21024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	count = 0;
211f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
212f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	while (1) {
213f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		if ((child = wait(&status)) >= 0) {
214f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			//tst_resm(TINFO, "\tTest{%d} exited status = 0x%x", child, status);
215f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			if (status) {
216354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				tst_resm(TFAIL,
217354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest{%d} failed, expected 0 exit.",
218354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 child);
219f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak				local_flag = FAILED;
220f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			}
221f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			++count;
222f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		} else {
223f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			if (errno != EINTR)
224f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak				break;
22524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
22624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
22724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
22824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/*
22924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * Should have collected all children.
23024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 */
23124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (count != nwait) {
232354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
233354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 count);
23424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		local_flag = FAILED;
23524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
23624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
23724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	chdir(homedir);
23824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
23924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	pid = fork();
240f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
24124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (pid < 0) {
242354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, sync, "fork failed");
24324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		tst_exit();
24424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
24524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
24624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (pid == 0) {
2477fdd5140a541cba0961c35161e2fa43426dce653robbiew		execl("/bin/rm", "rm", "-rf", fuss, NULL);
2487a73eab97ffec8b44079586a2cc74b28b52f9959Garrett Cooper		exit(1);
24924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	} else
25024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		wait(&status);
251f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
25224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (status) {
253354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TINFO, "CAUTION - ftest03, '%s' may not be removed",
254354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 fuss);
25524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
256bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
257f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	sync();
25824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew}
25924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
26024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew/*
26124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * dotest()
26224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	Children execute this.
26324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *
26424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * Randomly read/mod/write chunks with known pattern and check.
26524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew * When fill sectors, iterate.
26624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew */
26724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
26824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define	NMISC	4
269354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoenum m_type { m_fsync, m_trunc, m_fstat };
270354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaochar *m_str[] = {
271354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	"fsync", "trunc", "sync", "fstat"
27224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew};
27324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
274354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint misc_cnt[NMISC];		/* counts # of each kind of misc */
275354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint file_max;			/* file-max size */
276354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint nchunks;
277354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint last_trunc = -1;
278354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint tr_flag;
279354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoenum m_type type = m_fsync;
28024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
28124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define	CHUNK(i)	((i) * csize)
28224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew#define	NEXTMISC	((rand() % misc_intvl) + 5)
28324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
284f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void dotest(int testers, int me, int fd)
28524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew{
286f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	char *bits, *hold_bits;
287f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	char val;
288f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	int chunk, whenmisc, xfr, count, collide, i;
28924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
29024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/* Stuff for the readv call */
291354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	struct iovec r_iovec[MAXIOVCNT];
292354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int r_ioveclen;
29324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
29424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/* Stuff for the writev call */
295354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	struct iovec val_iovec[MAXIOVCNT];
296354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	struct iovec zero_iovec[MAXIOVCNT];
297354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int w_ioveclen;
2983d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik	struct stat stat;
29924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
30024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	nchunks = max_size / csize;
301f01306bdffecc3a419acf0d6f2ce5ea248a0617fGarrett Cooper	whenmisc = 0;
302f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
303354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((bits = malloc((nchunks + 7) / 8)) == 0) {
304526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis		tst_brkm(TBROK, NULL, "\tmalloc failed");
30524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
306f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
307354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((hold_bits = malloc((nchunks + 7) / 8)) == 0) {
308526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis		tst_brkm(TBROK, NULL, "\tmalloc failed");
30924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
31024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
311354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	/*Allocate memory for the iovec buffers and init the iovec arrays */
31224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	r_ioveclen = w_ioveclen = csize / MAXIOVCNT;
31324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
314f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	/* Please note that the above statement implies that csize
315f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	 * be evenly divisible by MAXIOVCNT.
316f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	 */
31724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	for (i = 0; i < MAXIOVCNT; i++) {
318f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		if ((r_iovec[i].iov_base = calloc(r_ioveclen, 1)) == 0) {
319af64a878a5f86f98675e1d00691f12f9fa57bd8fvapier			tst_brkm(TBROK, NULL, "\tmalloc failed");
32024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			/* tst_exit(); */
32124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
32224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		r_iovec[i].iov_len = r_ioveclen;
32324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
32424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		/* Allocate unused memory areas between all the buffers to
32524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		 * make things more diffult for the OS.
32624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		 */
327354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (malloc((i + 1) * 8) == NULL) {
328354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_brkm(TBROK, NULL, "\tmalloc failed");
32924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
330f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
331f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		if ((val_iovec[i].iov_base = calloc(w_ioveclen, 1)) == 0) {
332526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TBROK, NULL, "\tmalloc failed");
33324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
334f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
33524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		val_iovec[i].iov_len = w_ioveclen;
3362c28215423293e443469a07ae7011135d058b671Garrett Cooper
337354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (malloc((i + 1) * 8) == NULL) {
338526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TBROK, NULL, "\tmalloc failed");
33924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
340f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
341f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		if ((zero_iovec[i].iov_base = calloc(w_ioveclen, 1)) == 0) {
342526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TBROK, NULL, "\tmalloc failed");
34324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
344f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
34524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		zero_iovec[i].iov_len = w_ioveclen;
34624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
347354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (malloc((i + 1) * 8) == NULL) {
348526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TBROK, NULL, "\tmalloc failed");
34924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
35024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
35124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/*
35224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * No init sectors; allow file to be sparse.
35324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 */
354354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	val = (64 / testers) * me + 1;
35524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
3564bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	/*
3574bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	 * For each iteration:
358354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      zap bits array
359354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      loop
360354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *              pick random chunk, read it.
361354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *              if corresponding bit off {
362354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *                      verify = 0. (sparse file)
363354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *                      ++count;
364354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *              } else
365354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *                      verify = val.
366354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *              write "val" on it.
367354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *              repeat unitl count = nchunks.
368354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      ++val.
369354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 */
37024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
371f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	srand(getpid());
372f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
373f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	if (misc_intvl)
374f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		whenmisc = NEXTMISC;
375f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
376f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	while (iterations-- > 0) {
377f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
378df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		for (i = 0; i < NMISC; i++)
37924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			misc_cnt[i] = 0;
380f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
381354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		ftruncate(fd, 0);
38224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		file_max = 0;
383354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		memset(bits, 0, (nchunks + 7) / 8);
384354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		memset(hold_bits, 0, (nchunks + 7) / 8);
38524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
38624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		/* Have to fill the val and zero iov buffers in a different manner
38724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		 */
388f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		for (i = 0; i < MAXIOVCNT; i++) {
389354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			memset(val_iovec[i].iov_base, val,
390354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			       val_iovec[i].iov_len);
391354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			memset(zero_iovec[i].iov_base, 0,
392354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			       zero_iovec[i].iov_len);
39324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
39424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
395f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
39624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		count = 0;
39724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		collide = 0;
398f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
399f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		while (count < nchunks) {
40024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			chunk = rand() % nchunks;
40124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			/*
40224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 * Read it.
40324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 */
404f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			if (lseek(fd, CHUNK(chunk), 0) < 0) {
405526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				tst_brkm(TFAIL,
406526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					 NULL,
407354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest[%d]: lseek(0) fail at %x, errno = %d.",
408354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 me, CHUNK(chunk), errno);
40924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
41024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if ((xfr = readv(fd, &r_iovec[0], MAXIOVCNT)) < 0) {
411526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				tst_brkm(TFAIL,
412526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					 NULL,
413354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest[%d]: readv fail at %x, errno = %d.",
414354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 me, CHUNK(chunk), errno);
41524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
41624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			/*
41724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 * If chunk beyond EOF just write on it.
41824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 * Else if bit off, haven't seen it yet.
41924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 * Else, have.  Verify values.
42024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 */
42124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if (CHUNK(chunk) >= file_max) {
422354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				bits[chunk / 8] |= (1 << (chunk % 8));
42324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				++count;
424354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
42524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				if (xfr != csize) {
426526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					tst_brkm(TFAIL,
427526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis						 NULL,
428354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 "\tTest[%d]: xfr=%d != %d, zero read.",
429354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 me, xfr, csize);
43024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				}
431354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				for (i = 0; i < MAXIOVCNT; i++) {
432354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					if (memcmp
433354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					    (r_iovec[i].iov_base,
434354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					     zero_iovec[i].iov_base,
435354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					     r_iovec[i].iov_len)) {
43624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew						tst_resm(TFAIL,
437354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 "\tTest[%d] bad verify @ 0x%x for val %d count %d xfr %d file_max 0x%x, should be 0.",
438354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 me, CHUNK(chunk), val,
439354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 count, xfr, file_max);
440354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						tst_resm(TINFO,
441354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 "\tTest[%d]: last_trunc = 0x%x.",
442354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 me, last_trunc);
4433d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik						fstat(fd, &stat);
4443d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik						tst_resm(TINFO,
4453d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik							 "\tStat: size=%llx, ino=%x",
4463d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik							 stat.st_size, (unsigned)stat.st_ino);
44724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew						sync();
44804f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modak						ft_dumpiov(&r_iovec[i]);
449354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						ft_dumpbits(bits,
450354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							    (nchunks + 7) / 8);
451354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						ft_orbits(hold_bits, bits,
452354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							  (nchunks + 7) / 8);
4534bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak						tst_resm(TINFO, "\tHold ");
454354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						ft_dumpbits(hold_bits,
455354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							    (nchunks + 7) / 8);
45624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew						tst_exit();
45724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew					}
45824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				}
459354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				bits[chunk / 8] |= (1 << (chunk % 8));
46024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				++count;
46124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			} else {
46224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				if (xfr != csize) {
463526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					tst_brkm(TFAIL,
464526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis						 NULL,
465354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 "\tTest[%d]: xfr=%d != %d, val read.",
466354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 me, xfr, csize);
46724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				}
46824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				++collide;
469354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				for (i = 0; i < MAXIOVCNT; i++) {
470354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					if (memcmp
471354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					    (r_iovec[i].iov_base,
472354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					     val_iovec[i].iov_base,
473354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					     r_iovec[i].iov_len)) {
474354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						tst_resm(TFAIL,
475354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 "\tTest[%d] bad verify @ 0x%x for val %d count %d xfr %d file_max 0x%x.",
476354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 me, CHUNK(chunk), val,
477354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 count, xfr, file_max);
478354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						tst_resm(TINFO,
479354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 "\tTest[%d]: last_trunc = 0x%x.",
480354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							 me, last_trunc);
4813d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik						fstat(fd, &stat);
4823d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik						tst_resm(TINFO,
4833d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik							 "\tStat: size=%llx, ino=%x",
4843d0ea116b7b6cab0351a0f2edf1cde33185c5028Leonid V. Fedorenchik							 stat.st_size, (unsigned)stat.st_ino);
48524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew						sync();
48604f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modak						ft_dumpiov(&r_iovec[i]);
487354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						ft_dumpbits(bits,
488354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							    (nchunks + 7) / 8);
489354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						ft_orbits(hold_bits, bits,
490354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							  (nchunks + 7) / 8);
4914bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak						tst_resm(TINFO, "\tHold ");
492354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						ft_dumpbits(hold_bits,
493354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							    (nchunks + 7) / 8);
49424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew						tst_exit();
49524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew					}
49624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				}
49724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
49824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			/*
49924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 * Writev it.
50024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 */
501354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (lseek(fd, -xfr, 1) < 0) {
502526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				tst_brkm(TFAIL,
503526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					 NULL,
504354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest[%d]: lseek(1) fail at %x, errno = %d.",
505354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 me, CHUNK(chunk), errno);
50624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
507354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if ((xfr =
508354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			     writev(fd, &val_iovec[0], MAXIOVCNT)) < csize) {
50924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				if (errno == ENOSPC) {
510354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					tst_resm(TFAIL,
511354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 "\tTest[%d]: no space, exiting.",
512354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 me);
51324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew					fsync(fd);
51424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew					tst_exit();
51524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				}
516526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				tst_brkm(TFAIL,
517526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					 NULL,
518354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest[%d]: writev fail at %x xfr %d, errno = %d.",
519354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 me, CHUNK(chunk), xfr, errno);
52024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
52124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if (CHUNK(chunk) + csize > file_max)
52224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				file_max = CHUNK(chunk) + csize;
52324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			/*
52424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 * If hit "misc" interval, do it.
52524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			 */
52624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if (misc_intvl && --whenmisc <= 0) {
527354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
52824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				domisc(me, fd, bits);
52924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				whenmisc = NEXTMISC;
53024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
53124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if (count + collide > 2 * nchunks)
53224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				break;
53324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
53424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
53524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		/*
53624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		 * End of iteration, maybe before doing all chunks.
53724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		 */
53824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
53924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		fsync(fd);
540f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		++misc_cnt[m_fsync];
541af64a878a5f86f98675e1d00691f12f9fa57bd8fvapier		//tst_resm(TINFO, "\tTest{%d} val %d done, count = %d, collide = {%d}",
542354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		//              me, val, count, collide);
543df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		//for (i = 0; i < NMISC; i++)
544354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		//      tst_resm(TINFO, "\t\tTest{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
54524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		++val;
54624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
54724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew}
54824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
54924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew/*
55024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew *	Inject misc syscalls into the thing.
55124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew */
552f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modakstatic void domisc(int me, int fd, char *bits)
55324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew{
554f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	int chunk;
555f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	struct stat sb;
55624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
557f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	if (type > m_fstat)
55824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		type = m_fsync;
559f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
560354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	switch (type) {
56124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	case m_fsync:
56224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (fsync(fd) < 0) {
563526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TFAIL, NULL, "\tTest[%d]: fsync error %d.",
564526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				 me,
565354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 errno);
56624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
56724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		break;
56824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	case m_trunc:
56924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		chunk = rand() % (file_max / csize);
57024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		file_max = CHUNK(chunk);
57124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		last_trunc = file_max;
57224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (tr_flag) {
57324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if (ftruncate(fd, file_max) < 0) {
574526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				tst_brkm(TFAIL,
575526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					 NULL,
576354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest[%d]: ftruncate error %d @ 0x%x.",
577354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 me, errno, file_max);
57824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
57924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			tr_flag = 0;
58024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		} else {
58124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			if (truncate(test_name, file_max) < 0) {
582526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				tst_brkm(TFAIL,
583526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis					 NULL,
584354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "\tTest[%d]: truncate error %d @ 0x%x.",
585354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 me, errno, file_max);
58624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			}
58724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew			tr_flag = 1;
58824e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
589354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		for (; chunk % 8 != 0; chunk++)
590354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			bits[chunk / 8] &= ~(1 << (chunk % 8));
591df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		for (; chunk < nchunks; chunk += 8)
592354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			bits[chunk / 8] = 0;
59324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		break;
59424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	case m_fstat:
59524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (fstat(fd, &sb) < 0) {
596526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TFAIL, NULL, "\tTest[%d]: fstat() error %d.",
597526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				 me,
598354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 errno);
59924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
60024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		if (sb.st_size != file_max) {
601526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis			tst_brkm(TFAIL,
602526fdf8d8ea3b43b73de7cc25eb754f12702c8d2Cyril Hrubis				 NULL, "\tTest[%d]: fstat() mismatch; st_size=%"
603354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 PRIx64 ",file_max=%x.", me,
604354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 (int64_t) sb.st_size, file_max);
60524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		}
60624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew		break;
60724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
608f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
609f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	++misc_cnt[type];
610f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	++type;
61124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew}
61224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
613f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak/*
614f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak * SIGTERM signal handler.
61524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew */
61604f47a16e2a3c646d0b0b1e67ef14bf29342620dsubrata_modakstatic void term(int sig LTP_ATTRIBUTE_UNUSED)
61724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew{
618f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak	int i;
61924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
620af64a878a5f86f98675e1d00691f12f9fa57bd8fvapier	tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
62124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
62224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	/*
62324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * If run by hand we like to have the parent send the signal to
62424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 * the child processes.  This makes life easy.
62524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	 */
62624e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (parent_pid == getpid()) {
627f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		for (i = 0; i < nchild; i++)
628f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak			if (pidlist[i])
62924e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew				kill(pidlist[i], SIGTERM);
630f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak		return;
63124e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	}
63224e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
633af64a878a5f86f98675e1d00691f12f9fa57bd8fvapier	tst_resm(TINFO, "\tunlinking '%s'", test_name);
63424e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew
63524e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	close(fd);
636f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
63724e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	if (unlink(test_name))
638af64a878a5f86f98675e1d00691f12f9fa57bd8fvapier		tst_resm(TBROK, "Unlink of '%s' failed, errno = %d.",
639354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 test_name, errno);
64024e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	else
641af64a878a5f86f98675e1d00691f12f9fa57bd8fvapier		tst_resm(TBROK, "Unlink of '%s' successful.", test_name);
642f17ac84cca81829c4588e59b42495b8aea0fa362subrata_modak
64324e30ab69eb6e1b128ea1eb2dc12df4066c2227erobbiew	tst_exit();
644ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
645