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