xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/ftest/ftest04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) Cyril Hrubis [email protected] 2009
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
17*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
18*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker /*
22*49cdfc7eSAndroid Build Coastguard Worker  * NAME
23*49cdfc7eSAndroid Build Coastguard Worker  *	ftest04.c -- test single file io (tsfio.c by rbk) (ported from SPIE, section2/filesuite/ftest5.c, by Airong Zhang)
24*49cdfc7eSAndroid Build Coastguard Worker  *
25*49cdfc7eSAndroid Build Coastguard Worker  * CALLS
26*49cdfc7eSAndroid Build Coastguard Worker  *	fsync, sync, lseek, read, write
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
30*49cdfc7eSAndroid Build Coastguard Worker  *	Several child processes doing random seeks, read/write
31*49cdfc7eSAndroid Build Coastguard Worker  *	operations on the same file.
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  *
34*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS
35*49cdfc7eSAndroid Build Coastguard Worker  *	Runs a long time with default args - can take others on input
36*49cdfc7eSAndroid Build Coastguard Worker  *	line.  Use with "term mode".
37*49cdfc7eSAndroid Build Coastguard Worker  *
38*49cdfc7eSAndroid Build Coastguard Worker  */
39*49cdfc7eSAndroid Build Coastguard Worker #define _XOPEN_SOURCE 500
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/param.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <sys/file.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
48*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
51*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
52*49cdfc7eSAndroid Build Coastguard Worker #include "libftest.h"
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "ftest04";
55*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
58*49cdfc7eSAndroid Build Coastguard Worker static void runtest(void);
59*49cdfc7eSAndroid Build Coastguard Worker static void dotest(int, int, int);
60*49cdfc7eSAndroid Build Coastguard Worker static void domisc(int, int);
61*49cdfc7eSAndroid Build Coastguard Worker static void term(int sig);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
64*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker #define MAXCHILD	25
67*49cdfc7eSAndroid Build Coastguard Worker #define K_1		1024
68*49cdfc7eSAndroid Build Coastguard Worker #define K_2		2048
69*49cdfc7eSAndroid Build Coastguard Worker #define K_4		4096
70*49cdfc7eSAndroid Build Coastguard Worker #define	MAXIOVCNT	16
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker static int csize;		/* chunk size */
73*49cdfc7eSAndroid Build Coastguard Worker static int iterations;		/* # total iterations */
74*49cdfc7eSAndroid Build Coastguard Worker static int max_size;		/* max file size */
75*49cdfc7eSAndroid Build Coastguard Worker static int misc_intvl;		/* for doing misc things; 0 ==> no */
76*49cdfc7eSAndroid Build Coastguard Worker static int nchild;		/* number of child processes */
77*49cdfc7eSAndroid Build Coastguard Worker static int parent_pid;
78*49cdfc7eSAndroid Build Coastguard Worker static int pidlist[MAXCHILD];
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker static char filename[MAXPATHLEN];
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static int local_flag;
83*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char * av[])84*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char *av[])
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	setup();
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 		runtest();
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 		if (local_flag == PASSED)
97*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Test passed.");
98*49cdfc7eSAndroid Build Coastguard Worker 		else
99*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Test failed.");
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 		/* ??? only one loop ??? */
102*49cdfc7eSAndroid Build Coastguard Worker 		tst_rmdir();
103*49cdfc7eSAndroid Build Coastguard Worker 		tst_exit();
104*49cdfc7eSAndroid Build Coastguard Worker 	}
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)109*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
112*49cdfc7eSAndroid Build Coastguard Worker 	char wdbuf[MAXPATHLEN];
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	parent_pid = getpid();
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	/*
117*49cdfc7eSAndroid Build Coastguard Worker 	 * Make a filename for the test.
118*49cdfc7eSAndroid Build Coastguard Worker 	 */
119*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
120*49cdfc7eSAndroid Build Coastguard Worker 	if (!filename[0])
121*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(filename, "%s/ftest04.%d", getcwd(wdbuf, MAXPATHLEN),
122*49cdfc7eSAndroid Build Coastguard Worker 			getpid());
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(NULL, filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
125*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	/*
128*49cdfc7eSAndroid Build Coastguard Worker 	 * Default values for run conditions.
129*49cdfc7eSAndroid Build Coastguard Worker 	 */
130*49cdfc7eSAndroid Build Coastguard Worker 	iterations = 10;
131*49cdfc7eSAndroid Build Coastguard Worker 	nchild = 5;
132*49cdfc7eSAndroid Build Coastguard Worker 	csize = K_2;		/* should run with 1, 2, and 4 K sizes */
133*49cdfc7eSAndroid Build Coastguard Worker 	max_size = K_1 * K_1;
134*49cdfc7eSAndroid Build Coastguard Worker 	misc_intvl = 10;
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	if (sigset(SIGTERM, term) == SIG_ERR) {
137*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, NULL, "first sigset failed");
138*49cdfc7eSAndroid Build Coastguard Worker 	}
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	local_flag = PASSED;
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker 
runtest(void)143*49cdfc7eSAndroid Build Coastguard Worker static void runtest(void)
144*49cdfc7eSAndroid Build Coastguard Worker {
145*49cdfc7eSAndroid Build Coastguard Worker 	int count, child, fd, i, nwait, status;
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	nwait = 0;
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nchild; i++) {
150*49cdfc7eSAndroid Build Coastguard Worker 		if ((child = fork()) == 0) {
151*49cdfc7eSAndroid Build Coastguard Worker 			fd = SAFE_OPEN(NULL, filename, O_RDWR);
152*49cdfc7eSAndroid Build Coastguard Worker 			dotest(nchild, i, fd);
153*49cdfc7eSAndroid Build Coastguard Worker 			close(fd);
154*49cdfc7eSAndroid Build Coastguard Worker 			tst_exit();
155*49cdfc7eSAndroid Build Coastguard Worker 		}
156*49cdfc7eSAndroid Build Coastguard Worker 		if (child < 0) {
157*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, NULL, "fork failed");
158*49cdfc7eSAndroid Build Coastguard Worker 		} else {
159*49cdfc7eSAndroid Build Coastguard Worker 			pidlist[i] = child;
160*49cdfc7eSAndroid Build Coastguard Worker 			nwait++;
161*49cdfc7eSAndroid Build Coastguard Worker 		}
162*49cdfc7eSAndroid Build Coastguard Worker 	}
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	/*
165*49cdfc7eSAndroid Build Coastguard Worker 	 * Wait for children to finish.
166*49cdfc7eSAndroid Build Coastguard Worker 	 */
167*49cdfc7eSAndroid Build Coastguard Worker 	count = 0;
168*49cdfc7eSAndroid Build Coastguard Worker 	while ((child = wait(&status)) != -1 || errno == EINTR) {
169*49cdfc7eSAndroid Build Coastguard Worker 		if (child > 0) {
170*49cdfc7eSAndroid Build Coastguard Worker 			//tst_resm(TINFO, "\tTest{%d} exited status = 0x%x", child, status);
171*49cdfc7eSAndroid Build Coastguard Worker 			if (status) {
172*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL,
173*49cdfc7eSAndroid Build Coastguard Worker 					 "\tExpected 0 exit status - failed.");
174*49cdfc7eSAndroid Build Coastguard Worker 				local_flag = FAILED;
175*49cdfc7eSAndroid Build Coastguard Worker 			}
176*49cdfc7eSAndroid Build Coastguard Worker 			++count;
177*49cdfc7eSAndroid Build Coastguard Worker 		}
178*49cdfc7eSAndroid Build Coastguard Worker 	}
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	/*
181*49cdfc7eSAndroid Build Coastguard Worker 	 * Should have collected all children.
182*49cdfc7eSAndroid Build Coastguard Worker 	 */
183*49cdfc7eSAndroid Build Coastguard Worker 	if (count != nwait) {
184*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
185*49cdfc7eSAndroid Build Coastguard Worker 			 count);
186*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
187*49cdfc7eSAndroid Build Coastguard Worker 	}
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	unlink(filename);
190*49cdfc7eSAndroid Build Coastguard Worker 	sync();
191*49cdfc7eSAndroid Build Coastguard Worker }
192*49cdfc7eSAndroid Build Coastguard Worker 
193*49cdfc7eSAndroid Build Coastguard Worker /*
194*49cdfc7eSAndroid Build Coastguard Worker  * dotest()
195*49cdfc7eSAndroid Build Coastguard Worker  *	Children execute this.
196*49cdfc7eSAndroid Build Coastguard Worker  *
197*49cdfc7eSAndroid Build Coastguard Worker  * Randomly read/mod/write chunks with known pattern and check.
198*49cdfc7eSAndroid Build Coastguard Worker  * When fill sectors, iterate.
199*49cdfc7eSAndroid Build Coastguard Worker  */
200*49cdfc7eSAndroid Build Coastguard Worker #define	NMISC	2
201*49cdfc7eSAndroid Build Coastguard Worker enum m_type { m_fsync, m_sync };
202*49cdfc7eSAndroid Build Coastguard Worker char *m_str[] = { "fsync", "sync" };
203*49cdfc7eSAndroid Build Coastguard Worker 
204*49cdfc7eSAndroid Build Coastguard Worker int misc_cnt[NMISC];		/* counts # of each kind of misc */
205*49cdfc7eSAndroid Build Coastguard Worker int misc_flag;
206*49cdfc7eSAndroid Build Coastguard Worker int nchunks;
207*49cdfc7eSAndroid Build Coastguard Worker 
208*49cdfc7eSAndroid Build Coastguard Worker #define	CHUNK(i)	(((i) * testers + me) * csize)
209*49cdfc7eSAndroid Build Coastguard Worker #define	NEXTMISC	((rand() % misc_intvl) + 5)
210*49cdfc7eSAndroid Build Coastguard Worker 
dotest(int testers,int me,int fd)211*49cdfc7eSAndroid Build Coastguard Worker static void dotest(int testers, int me, int fd)
212*49cdfc7eSAndroid Build Coastguard Worker {
213*49cdfc7eSAndroid Build Coastguard Worker 	char *bits;
214*49cdfc7eSAndroid Build Coastguard Worker 	char val, val0;
215*49cdfc7eSAndroid Build Coastguard Worker 	int count, collide, chunk, whenmisc, xfr, i;
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	/* Stuff for the readv call */
218*49cdfc7eSAndroid Build Coastguard Worker 	struct iovec r_iovec[MAXIOVCNT];
219*49cdfc7eSAndroid Build Coastguard Worker 	int r_ioveclen;
220*49cdfc7eSAndroid Build Coastguard Worker 
221*49cdfc7eSAndroid Build Coastguard Worker 	/* Stuff for the writev call */
222*49cdfc7eSAndroid Build Coastguard Worker 	struct iovec val0_iovec[MAXIOVCNT];
223*49cdfc7eSAndroid Build Coastguard Worker 	struct iovec val_iovec[MAXIOVCNT];
224*49cdfc7eSAndroid Build Coastguard Worker 	int w_ioveclen;
225*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat;
226*49cdfc7eSAndroid Build Coastguard Worker 
227*49cdfc7eSAndroid Build Coastguard Worker 	nchunks = max_size / (testers * csize);
228*49cdfc7eSAndroid Build Coastguard Worker 	whenmisc = 0;
229*49cdfc7eSAndroid Build Coastguard Worker 
230*49cdfc7eSAndroid Build Coastguard Worker 	if ((bits = malloc((nchunks + 7) / 8)) == NULL) {
231*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, NULL, "\tmalloc failed(bits)");
232*49cdfc7eSAndroid Build Coastguard Worker 	}
233*49cdfc7eSAndroid Build Coastguard Worker 
234*49cdfc7eSAndroid Build Coastguard Worker 	/*Allocate memory for the iovec buffers and init the iovec arrays
235*49cdfc7eSAndroid Build Coastguard Worker 	 */
236*49cdfc7eSAndroid Build Coastguard Worker 	r_ioveclen = w_ioveclen = csize / MAXIOVCNT;
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 	/* Please note that the above statement implies that csize
239*49cdfc7eSAndroid Build Coastguard Worker 	 * be evenly divisible by MAXIOVCNT.
240*49cdfc7eSAndroid Build Coastguard Worker 	 */
241*49cdfc7eSAndroid Build Coastguard Worker 
242*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAXIOVCNT; i++) {
243*49cdfc7eSAndroid Build Coastguard Worker 		if ((r_iovec[i].iov_base = malloc(r_ioveclen)) == NULL) {
244*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, NULL, "\tmalloc failed(r_iovec[])");
245*49cdfc7eSAndroid Build Coastguard Worker 		}
246*49cdfc7eSAndroid Build Coastguard Worker 		r_iovec[i].iov_len = r_ioveclen;
247*49cdfc7eSAndroid Build Coastguard Worker 
248*49cdfc7eSAndroid Build Coastguard Worker 		/* Allocate unused memory areas between all the buffers to
249*49cdfc7eSAndroid Build Coastguard Worker 		 * make things more diffult for the OS.
250*49cdfc7eSAndroid Build Coastguard Worker 		 */
251*49cdfc7eSAndroid Build Coastguard Worker 		if (malloc((i + 1) * 8) == NULL) {
252*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, NULL, "\tmalloc failed");
253*49cdfc7eSAndroid Build Coastguard Worker 		}
254*49cdfc7eSAndroid Build Coastguard Worker 
255*49cdfc7eSAndroid Build Coastguard Worker 		if ((val0_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) {
256*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, NULL, "\tmalloc failed(val0_iovec[])");
257*49cdfc7eSAndroid Build Coastguard Worker 		}
258*49cdfc7eSAndroid Build Coastguard Worker 
259*49cdfc7eSAndroid Build Coastguard Worker 		val0_iovec[i].iov_len = w_ioveclen;
260*49cdfc7eSAndroid Build Coastguard Worker 
261*49cdfc7eSAndroid Build Coastguard Worker 		if (malloc((i + 1) * 8) == NULL) {
262*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, NULL, "\tmalloc failed");
263*49cdfc7eSAndroid Build Coastguard Worker 		}
264*49cdfc7eSAndroid Build Coastguard Worker 
265*49cdfc7eSAndroid Build Coastguard Worker 		if ((val_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) {
266*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, NULL, "\tmalloc failed(iov_base)");
267*49cdfc7eSAndroid Build Coastguard Worker 		}
268*49cdfc7eSAndroid Build Coastguard Worker 
269*49cdfc7eSAndroid Build Coastguard Worker 		val_iovec[i].iov_len = w_ioveclen;
270*49cdfc7eSAndroid Build Coastguard Worker 
271*49cdfc7eSAndroid Build Coastguard Worker 		if (malloc((i + 1) * 8) == NULL) {
272*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, NULL, "\tmalloc failed");
273*49cdfc7eSAndroid Build Coastguard Worker 		}
274*49cdfc7eSAndroid Build Coastguard Worker 	}
275*49cdfc7eSAndroid Build Coastguard Worker 
276*49cdfc7eSAndroid Build Coastguard Worker 	/*
277*49cdfc7eSAndroid Build Coastguard Worker 	 * No init sectors; file-sys makes 0 to start.
278*49cdfc7eSAndroid Build Coastguard Worker 	 */
279*49cdfc7eSAndroid Build Coastguard Worker 	val = (64 / testers) * me + 1;
280*49cdfc7eSAndroid Build Coastguard Worker 	val0 = 0;
281*49cdfc7eSAndroid Build Coastguard Worker 
282*49cdfc7eSAndroid Build Coastguard Worker 	/*
283*49cdfc7eSAndroid Build Coastguard Worker 	 * For each iteration:
284*49cdfc7eSAndroid Build Coastguard Worker 	 *      zap bits array
285*49cdfc7eSAndroid Build Coastguard Worker 	 *      loop:
286*49cdfc7eSAndroid Build Coastguard Worker 	 *              pick random chunk, read it.
287*49cdfc7eSAndroid Build Coastguard Worker 	 *              if corresponding bit off {
288*49cdfc7eSAndroid Build Coastguard Worker 	 *                      verify == 0. (sparse file)
289*49cdfc7eSAndroid Build Coastguard Worker 	 *                      ++count;
290*49cdfc7eSAndroid Build Coastguard Worker 	 *              } else
291*49cdfc7eSAndroid Build Coastguard Worker 	 *                      verify == val.
292*49cdfc7eSAndroid Build Coastguard Worker 	 *              write "val" on it.
293*49cdfc7eSAndroid Build Coastguard Worker 	 *              repeat until count = nchunks.
294*49cdfc7eSAndroid Build Coastguard Worker 	 *      ++val.
295*49cdfc7eSAndroid Build Coastguard Worker 	 */
296*49cdfc7eSAndroid Build Coastguard Worker 	srand(getpid());
297*49cdfc7eSAndroid Build Coastguard Worker 
298*49cdfc7eSAndroid Build Coastguard Worker 	if (misc_intvl)
299*49cdfc7eSAndroid Build Coastguard Worker 		whenmisc = NEXTMISC;
300*49cdfc7eSAndroid Build Coastguard Worker 
301*49cdfc7eSAndroid Build Coastguard Worker 	while (iterations-- > 0) {
302*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < NMISC; i++)
303*49cdfc7eSAndroid Build Coastguard Worker 			misc_cnt[i] = 0;
304*49cdfc7eSAndroid Build Coastguard Worker 		memset(bits, 0, (nchunks + 7) / 8);
305*49cdfc7eSAndroid Build Coastguard Worker 		/* Have to fill the val0 and val iov buffers in a different manner */
306*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < MAXIOVCNT; i++) {
307*49cdfc7eSAndroid Build Coastguard Worker 			memset(val0_iovec[i].iov_base, val0,
308*49cdfc7eSAndroid Build Coastguard Worker 			       val0_iovec[i].iov_len);
309*49cdfc7eSAndroid Build Coastguard Worker 			memset(val_iovec[i].iov_base, val,
310*49cdfc7eSAndroid Build Coastguard Worker 			       val_iovec[i].iov_len);
311*49cdfc7eSAndroid Build Coastguard Worker 
312*49cdfc7eSAndroid Build Coastguard Worker 		}
313*49cdfc7eSAndroid Build Coastguard Worker 		count = 0;
314*49cdfc7eSAndroid Build Coastguard Worker 		collide = 0;
315*49cdfc7eSAndroid Build Coastguard Worker 		while (count < nchunks) {
316*49cdfc7eSAndroid Build Coastguard Worker 			chunk = rand() % nchunks;
317*49cdfc7eSAndroid Build Coastguard Worker 			/*
318*49cdfc7eSAndroid Build Coastguard Worker 			 * Read it.
319*49cdfc7eSAndroid Build Coastguard Worker 			 */
320*49cdfc7eSAndroid Build Coastguard Worker 			if (lseek(fd, CHUNK(chunk), 0) < 0) {
321*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TFAIL,
322*49cdfc7eSAndroid Build Coastguard Worker 					 NULL,
323*49cdfc7eSAndroid Build Coastguard Worker 					 "\tTest[%d]: lseek(0) fail at %x, errno = %d.",
324*49cdfc7eSAndroid Build Coastguard Worker 					 me, CHUNK(chunk), errno);
325*49cdfc7eSAndroid Build Coastguard Worker 			}
326*49cdfc7eSAndroid Build Coastguard Worker 			if ((xfr = readv(fd, &r_iovec[0], MAXIOVCNT)) < 0) {
327*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TFAIL,
328*49cdfc7eSAndroid Build Coastguard Worker 					 NULL,
329*49cdfc7eSAndroid Build Coastguard Worker 					 "\tTest[%d]: readv fail at %x, errno = %d.",
330*49cdfc7eSAndroid Build Coastguard Worker 					 me, CHUNK(chunk), errno);
331*49cdfc7eSAndroid Build Coastguard Worker 			}
332*49cdfc7eSAndroid Build Coastguard Worker 			/*
333*49cdfc7eSAndroid Build Coastguard Worker 			 * If chunk beyond EOF just write on it.
334*49cdfc7eSAndroid Build Coastguard Worker 			 * Else if bit off, haven't seen it yet.
335*49cdfc7eSAndroid Build Coastguard Worker 			 * Else, have.  Verify values.
336*49cdfc7eSAndroid Build Coastguard Worker 			 */
337*49cdfc7eSAndroid Build Coastguard Worker 			if (xfr == 0) {
338*49cdfc7eSAndroid Build Coastguard Worker 				bits[chunk / 8] |= (1 << (chunk % 8));
339*49cdfc7eSAndroid Build Coastguard Worker 			} else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
340*49cdfc7eSAndroid Build Coastguard Worker 				if (xfr != csize) {
341*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TFAIL,
342*49cdfc7eSAndroid Build Coastguard Worker 						 NULL,
343*49cdfc7eSAndroid Build Coastguard Worker 						 "\tTest[%d]: xfr=%d != %d, zero read.",
344*49cdfc7eSAndroid Build Coastguard Worker 						 me, xfr, csize);
345*49cdfc7eSAndroid Build Coastguard Worker 				}
346*49cdfc7eSAndroid Build Coastguard Worker 				for (i = 0; i < MAXIOVCNT; i++) {
347*49cdfc7eSAndroid Build Coastguard Worker 					if (memcmp
348*49cdfc7eSAndroid Build Coastguard Worker 					    (r_iovec[i].iov_base,
349*49cdfc7eSAndroid Build Coastguard Worker 					     val0_iovec[i].iov_base,
350*49cdfc7eSAndroid Build Coastguard Worker 					     r_iovec[i].iov_len)) {
351*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TFAIL,
352*49cdfc7eSAndroid Build Coastguard Worker 							 "\tTest[%d] bad verify @ 0x%x for val %d count %d xfr %d.",
353*49cdfc7eSAndroid Build Coastguard Worker 							 me, CHUNK(chunk), val0,
354*49cdfc7eSAndroid Build Coastguard Worker 							 count, xfr);
355*49cdfc7eSAndroid Build Coastguard Worker 						fstat(fd, &stat);
356*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TINFO,
357*49cdfc7eSAndroid Build Coastguard Worker 							 "\tStat: size=%llx, ino=%x",
358*49cdfc7eSAndroid Build Coastguard Worker 							 stat.st_size, (unsigned)stat.st_ino);
359*49cdfc7eSAndroid Build Coastguard Worker 						ft_dumpiov(&r_iovec[i]);
360*49cdfc7eSAndroid Build Coastguard Worker 						ft_dumpbits(bits,
361*49cdfc7eSAndroid Build Coastguard Worker 							    (nchunks + 7) / 8);
362*49cdfc7eSAndroid Build Coastguard Worker 						tst_exit();
363*49cdfc7eSAndroid Build Coastguard Worker 					}
364*49cdfc7eSAndroid Build Coastguard Worker 				}
365*49cdfc7eSAndroid Build Coastguard Worker 				bits[chunk / 8] |= (1 << (chunk % 8));
366*49cdfc7eSAndroid Build Coastguard Worker 				++count;
367*49cdfc7eSAndroid Build Coastguard Worker 			} else {
368*49cdfc7eSAndroid Build Coastguard Worker 				if (xfr != csize) {
369*49cdfc7eSAndroid Build Coastguard Worker 					tst_brkm(TFAIL,
370*49cdfc7eSAndroid Build Coastguard Worker 						 NULL,
371*49cdfc7eSAndroid Build Coastguard Worker 						 "\tTest[%d]: xfr=%d != %d, val read.",
372*49cdfc7eSAndroid Build Coastguard Worker 						 me, xfr, csize);
373*49cdfc7eSAndroid Build Coastguard Worker 				}
374*49cdfc7eSAndroid Build Coastguard Worker 				++collide;
375*49cdfc7eSAndroid Build Coastguard Worker 				for (i = 0; i < MAXIOVCNT; i++) {
376*49cdfc7eSAndroid Build Coastguard Worker 					if (memcmp
377*49cdfc7eSAndroid Build Coastguard Worker 					    (r_iovec[i].iov_base,
378*49cdfc7eSAndroid Build Coastguard Worker 					     val_iovec[i].iov_base,
379*49cdfc7eSAndroid Build Coastguard Worker 					     r_iovec[i].iov_len)) {
380*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TFAIL,
381*49cdfc7eSAndroid Build Coastguard Worker 							 "\tTest[%d] bad verify @ 0x%x for val %d count %d xfr %d.",
382*49cdfc7eSAndroid Build Coastguard Worker 							 me, CHUNK(chunk), val,
383*49cdfc7eSAndroid Build Coastguard Worker 							 count, xfr);
384*49cdfc7eSAndroid Build Coastguard Worker 						fstat(fd, &stat);
385*49cdfc7eSAndroid Build Coastguard Worker 						tst_resm(TINFO,
386*49cdfc7eSAndroid Build Coastguard Worker 							 "\tStat: size=%llx, ino=%x",
387*49cdfc7eSAndroid Build Coastguard Worker 							 stat.st_size, (unsigned)stat.st_ino);
388*49cdfc7eSAndroid Build Coastguard Worker 						ft_dumpiov(&r_iovec[i]);
389*49cdfc7eSAndroid Build Coastguard Worker 						ft_dumpbits(bits,
390*49cdfc7eSAndroid Build Coastguard Worker 							    (nchunks + 7) / 8);
391*49cdfc7eSAndroid Build Coastguard Worker 						tst_exit();
392*49cdfc7eSAndroid Build Coastguard Worker 					}
393*49cdfc7eSAndroid Build Coastguard Worker 				}
394*49cdfc7eSAndroid Build Coastguard Worker 			}
395*49cdfc7eSAndroid Build Coastguard Worker 			/*
396*49cdfc7eSAndroid Build Coastguard Worker 			 * Write it.
397*49cdfc7eSAndroid Build Coastguard Worker 			 */
398*49cdfc7eSAndroid Build Coastguard Worker 			if (lseek(fd, -xfr, 1) < 0) {
399*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TFAIL,
400*49cdfc7eSAndroid Build Coastguard Worker 					 NULL,
401*49cdfc7eSAndroid Build Coastguard Worker 					 "\tTest[%d]: lseek(1) fail at %x, errno = %d.",
402*49cdfc7eSAndroid Build Coastguard Worker 					 me, CHUNK(chunk), errno);
403*49cdfc7eSAndroid Build Coastguard Worker 			}
404*49cdfc7eSAndroid Build Coastguard Worker 			if ((xfr =
405*49cdfc7eSAndroid Build Coastguard Worker 			     writev(fd, &val_iovec[0], MAXIOVCNT)) < csize) {
406*49cdfc7eSAndroid Build Coastguard Worker 				if (errno == ENOSPC) {
407*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL,
408*49cdfc7eSAndroid Build Coastguard Worker 						 "\tTest[%d]: no space, exiting.",
409*49cdfc7eSAndroid Build Coastguard Worker 						 me);
410*49cdfc7eSAndroid Build Coastguard Worker 					fsync(fd);
411*49cdfc7eSAndroid Build Coastguard Worker 					tst_exit();
412*49cdfc7eSAndroid Build Coastguard Worker 				}
413*49cdfc7eSAndroid Build Coastguard Worker 				tst_brkm(TFAIL,
414*49cdfc7eSAndroid Build Coastguard Worker 					 NULL,
415*49cdfc7eSAndroid Build Coastguard Worker 					 "\tTest[%d]: writev fail at %x xfr %d, errno = %d.",
416*49cdfc7eSAndroid Build Coastguard Worker 					 me, CHUNK(chunk), xfr, errno);
417*49cdfc7eSAndroid Build Coastguard Worker 			}
418*49cdfc7eSAndroid Build Coastguard Worker 			/*
419*49cdfc7eSAndroid Build Coastguard Worker 			 * If hit "misc" interval, do it.
420*49cdfc7eSAndroid Build Coastguard Worker 			 */
421*49cdfc7eSAndroid Build Coastguard Worker 			if (misc_intvl && --whenmisc <= 0) {
422*49cdfc7eSAndroid Build Coastguard Worker 				domisc(me, fd);
423*49cdfc7eSAndroid Build Coastguard Worker 				whenmisc = NEXTMISC;
424*49cdfc7eSAndroid Build Coastguard Worker 			}
425*49cdfc7eSAndroid Build Coastguard Worker 			if (count + collide > 2 * nchunks)
426*49cdfc7eSAndroid Build Coastguard Worker 				break;
427*49cdfc7eSAndroid Build Coastguard Worker 		}
428*49cdfc7eSAndroid Build Coastguard Worker 
429*49cdfc7eSAndroid Build Coastguard Worker 		/*
430*49cdfc7eSAndroid Build Coastguard Worker 		 * End of iteration, maybe before doing all chunks.
431*49cdfc7eSAndroid Build Coastguard Worker 		 */
432*49cdfc7eSAndroid Build Coastguard Worker 
433*49cdfc7eSAndroid Build Coastguard Worker 		if (count < nchunks) {
434*49cdfc7eSAndroid Build Coastguard Worker 			//tst_resm(TINFO, "\tTest{%d} val %d stopping @ %d, collide = {%d}.",
435*49cdfc7eSAndroid Build Coastguard Worker 			//              me, val, count, collide);
436*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < nchunks; i++) {
437*49cdfc7eSAndroid Build Coastguard Worker 				if ((bits[i / 8] & (1 << (i % 8))) == 0) {
438*49cdfc7eSAndroid Build Coastguard Worker 					if (lseek(fd, CHUNK(i), 0) < 0) {
439*49cdfc7eSAndroid Build Coastguard Worker 						tst_brkm(TFAIL,
440*49cdfc7eSAndroid Build Coastguard Worker 							 NULL,
441*49cdfc7eSAndroid Build Coastguard Worker 							 "\tTest[%d]: lseek fail at %x, errno = %d.",
442*49cdfc7eSAndroid Build Coastguard Worker 							 me, CHUNK(i), errno);
443*49cdfc7eSAndroid Build Coastguard Worker 					}
444*49cdfc7eSAndroid Build Coastguard Worker 					if (writev(fd, &val_iovec[0], MAXIOVCNT)
445*49cdfc7eSAndroid Build Coastguard Worker 					    != csize) {
446*49cdfc7eSAndroid Build Coastguard Worker 						tst_brkm(TFAIL,
447*49cdfc7eSAndroid Build Coastguard Worker 							 NULL,
448*49cdfc7eSAndroid Build Coastguard Worker 							 "\tTest[%d]: writev fail at %x, errno = %d.",
449*49cdfc7eSAndroid Build Coastguard Worker 							 me, CHUNK(i), errno);
450*49cdfc7eSAndroid Build Coastguard Worker 					}
451*49cdfc7eSAndroid Build Coastguard Worker 				}
452*49cdfc7eSAndroid Build Coastguard Worker 			}
453*49cdfc7eSAndroid Build Coastguard Worker 		}
454*49cdfc7eSAndroid Build Coastguard Worker 
455*49cdfc7eSAndroid Build Coastguard Worker 		fsync(fd);
456*49cdfc7eSAndroid Build Coastguard Worker 		++misc_cnt[m_fsync];
457*49cdfc7eSAndroid Build Coastguard Worker 		//tst_resm(TINFO, "\tTest[%d] val %d done, count = %d, collide = %d.",
458*49cdfc7eSAndroid Build Coastguard Worker 		//              me, val, count, collide);
459*49cdfc7eSAndroid Build Coastguard Worker 		//for (i = 0; i < NMISC; i++)
460*49cdfc7eSAndroid Build Coastguard Worker 		//      tst_resm(TINFO, "\t\tTest[%d]: %d %s's.", me, misc_cnt[i], m_str[i]);
461*49cdfc7eSAndroid Build Coastguard Worker 		val0 = val++;
462*49cdfc7eSAndroid Build Coastguard Worker 	}
463*49cdfc7eSAndroid Build Coastguard Worker }
464*49cdfc7eSAndroid Build Coastguard Worker 
465*49cdfc7eSAndroid Build Coastguard Worker /*
466*49cdfc7eSAndroid Build Coastguard Worker  * domisc()
467*49cdfc7eSAndroid Build Coastguard Worker  *	Inject misc syscalls into the thing.
468*49cdfc7eSAndroid Build Coastguard Worker  */
domisc(int me,int fd)469*49cdfc7eSAndroid Build Coastguard Worker static void domisc(int me, int fd)
470*49cdfc7eSAndroid Build Coastguard Worker {
471*49cdfc7eSAndroid Build Coastguard Worker 	if (fsync(fd) < 0) {
472*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, NULL, "\tTest[%d]: fsync error %d.", me,
473*49cdfc7eSAndroid Build Coastguard Worker 			 errno);
474*49cdfc7eSAndroid Build Coastguard Worker 	}
475*49cdfc7eSAndroid Build Coastguard Worker 
476*49cdfc7eSAndroid Build Coastguard Worker 	++misc_cnt[1];
477*49cdfc7eSAndroid Build Coastguard Worker }
478*49cdfc7eSAndroid Build Coastguard Worker 
term(int sig LTP_ATTRIBUTE_UNUSED)479*49cdfc7eSAndroid Build Coastguard Worker static void term(int sig LTP_ATTRIBUTE_UNUSED)
480*49cdfc7eSAndroid Build Coastguard Worker {
481*49cdfc7eSAndroid Build Coastguard Worker 	int i;
482*49cdfc7eSAndroid Build Coastguard Worker 
483*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
484*49cdfc7eSAndroid Build Coastguard Worker 
485*49cdfc7eSAndroid Build Coastguard Worker 	if (parent_pid == getpid()) {
486*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < nchild; i++)
487*49cdfc7eSAndroid Build Coastguard Worker 			if (pidlist[i])
488*49cdfc7eSAndroid Build Coastguard Worker 				kill(pidlist[i], SIGTERM);
489*49cdfc7eSAndroid Build Coastguard Worker 	}
490*49cdfc7eSAndroid Build Coastguard Worker 
491*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
492*49cdfc7eSAndroid Build Coastguard Worker }
493