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