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