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