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