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