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