blob: 099609a3112b57bdf82b75be04164babf6d7f1a5 [file] [log] [blame]
robbiew24e30ab2003-01-07 20:53:21 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
subrata_modak04f47a12009-09-18 17:44:08 +00004 * Copyright (c) Cyril Hrubis chrubis@suse.cz 2009
robbiew24e30ab2003-01-07 20:53:21 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiew24e30ab2003-01-07 20:53:21 +000019 */
20
robbiew24e30ab2003-01-07 20:53:21 +000021/*
22 * NAME
23 * ftest01.c -- test file I/O (ported from SPIE section2, filesuite, by Airong Zhang)
24 *
25 * CALLS
26 * lseek, read, write
27 * truncate, ftruncate, fsync, sync, fstat
28 *
29 * ALGORITHM
30 * A bitmap is used to map pieces of a file.
31 * Loop: pick a random piece of the file
32 * if we haven't seen it before make sure it is zero,
33 * write pattern
34 * if we have seen it before make sure correct pattern.
35 *
36 * This was originally written by rbk - was program tfio.c
37 * Modified by dale to integrate with test suites.
38 *
39 * RESTRICTIONS
40 * Runs a long time with default args - can take others on input
41 * line. Use with "term mode".
42 * If run on vax the ftruncate will not be random - will always go to
43 * start of file. NOTE: produces a very high load average!!
44 *
45 * CAUTION!!
46 * If a file is supplied to this program with the "-f" option
47 * it will be removed with a system("rm -rf filename") call.
subrata_modakbdbaec52009-02-26 12:14:51 +000048 *
robbiew24e30ab2003-01-07 20:53:21 +000049 */
50#define _GNU_SOURCE 1
subrata_modakf17ac842009-09-07 09:06:31 +000051#include <stdio.h>
robbiew24e30ab2003-01-07 20:53:21 +000052#include <sys/types.h>
53#include <sys/wait.h>
54#include <sys/stat.h>
vapierf81795e2006-02-15 06:28:58 +000055#include <errno.h>
robbiew24e30ab2003-01-07 20:53:21 +000056#include <fcntl.h>
subrata_modakf17ac842009-09-07 09:06:31 +000057#include <signal.h>
robbiew24e30ab2003-01-07 20:53:21 +000058#include <unistd.h>
subrata_modakf17ac842009-09-07 09:06:31 +000059#include <inttypes.h>
robbiew24e30ab2003-01-07 20:53:21 +000060#include "test.h"
subrata_modak04f47a12009-09-18 17:44:08 +000061#include "libftest.h"
robbiew24e30ab2003-01-07 20:53:21 +000062
63char *TCID = "ftest01";
64int TST_TOTAL = 1;
robbiew24e30ab2003-01-07 20:53:21 +000065
subrata_modakf17ac842009-09-07 09:06:31 +000066static void setup(void);
67static void runtest(void);
68static void dotest(int, int, int);
Wanlong Gao354ebb42012-12-07 10:10:04 +080069static void domisc(int, int, char *);
subrata_modakf17ac842009-09-07 09:06:31 +000070static void cleanup(void);
subrata_modak04f47a12009-09-18 17:44:08 +000071static void term(int sig);
robbiew24e30ab2003-01-07 20:53:21 +000072
73#define PASSED 1
74#define FAILED 0
75
subrata_modakf17ac842009-09-07 09:06:31 +000076#define MAXCHILD 25
robbiew24e30ab2003-01-07 20:53:21 +000077#define K_1 1024
78#define K_2 2048
79#define K_4 4096
80
Wanlong Gao354ebb42012-12-07 10:10:04 +080081static int csize; /* chunk size */
82static int iterations; /* # total iterations */
83static int max_size; /* max file size */
84static int misc_intvl; /* for doing misc things; 0 ==> no */
85static int nchild; /* how many children */
86static int fd; /* file descriptor used by child */
subrata_modak04f47a12009-09-18 17:44:08 +000087static int parent_pid;
88static int pidlist[MAXCHILD];
89static char test_name[2];
robbiew24e30ab2003-01-07 20:53:21 +000090
Wanlong Gao354ebb42012-12-07 10:10:04 +080091static char fuss[MAXPATHLEN]; /* directory to do this in */
92static char homedir[MAXPATHLEN]; /* where we started */
robbiew24e30ab2003-01-07 20:53:21 +000093
subrata_modak04f47a12009-09-18 17:44:08 +000094static int local_flag;
robbiew24e30ab2003-01-07 20:53:21 +000095
subrata_modak04f47a12009-09-18 17:44:08 +000096int main(int ac, char *av[])
robbiew24e30ab2003-01-07 20:53:21 +000097{
subrata_modakf17ac842009-09-07 09:06:31 +000098 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020099 const char *msg;
robbiew24e30ab2003-01-07 20:53:21 +0000100
Garrett Cooper72521cf2010-12-18 06:31:47 -0800101 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Cyril Hrubisee7667e2014-09-24 13:14:56 +0200102 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew24e30ab2003-01-07 20:53:21 +0000103
104 setup();
105
106 for (lc = 0; TEST_LOOPING(lc); lc++) {
107
108 runtest();
109
subrata_modakf17ac842009-09-07 09:06:31 +0000110 if (local_flag == PASSED)
vapieraf64a872006-02-15 06:47:36 +0000111 tst_resm(TPASS, "Test passed.");
subrata_modakf17ac842009-09-07 09:06:31 +0000112 else
vapieraf64a872006-02-15 06:47:36 +0000113 tst_resm(TFAIL, "Test failed.");
subrata_modakf17ac842009-09-07 09:06:31 +0000114 }
115
robbiew24e30ab2003-01-07 20:53:21 +0000116 cleanup();
Garrett Cooper72521cf2010-12-18 06:31:47 -0800117 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
robbiew24e30ab2003-01-07 20:53:21 +0000119}
robbiew24e30ab2003-01-07 20:53:21 +0000120
subrata_modakf17ac842009-09-07 09:06:31 +0000121static void setup(void)
robbiew24e30ab2003-01-07 20:53:21 +0000122{
123
robbiew24e30ab2003-01-07 20:53:21 +0000124 tst_tmpdir();
subrata_modakf17ac842009-09-07 09:06:31 +0000125 getcwd(homedir, sizeof(homedir));
robbiew24e30ab2003-01-07 20:53:21 +0000126 parent_pid = getpid();
127
128 if (!fuss[0])
129 sprintf(fuss, "./ftest1.%d", getpid());
130
131 mkdir(fuss, 0755);
132
Garrett Cooper72521cf2010-12-18 06:31:47 -0800133 if (chdir(fuss) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_brkm(TBROK | TERRNO, NULL, "chdir failed");
robbiew24e30ab2003-01-07 20:53:21 +0000135
subrata_modakf17ac842009-09-07 09:06:31 +0000136 /*
137 * Default values for run conditions.
138 */
robbiew24e30ab2003-01-07 20:53:21 +0000139 iterations = 10;
140 nchild = 5;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 csize = K_2; /* should run with 1, 2, and 4 K sizes */
robbiew24e30ab2003-01-07 20:53:21 +0000142 max_size = K_1 * K_1;
143 misc_intvl = 10;
144
subrata_modakf17ac842009-09-07 09:06:31 +0000145 if (sigset(SIGTERM, term) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 tst_brkm(TBROK | TERRNO, NULL, "sigset failed");
robbiew24e30ab2003-01-07 20:53:21 +0000147 }
148
149 local_flag = PASSED;
150}
151
subrata_modakf17ac842009-09-07 09:06:31 +0000152static void runtest(void)
robbiew24e30ab2003-01-07 20:53:21 +0000153{
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800154 pid_t pid;
155 int i, child, count, nwait, status;
156
157 nwait = 0;
robbiew24e30ab2003-01-07 20:53:21 +0000158
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800159 for (i = 0; i < nchild; i++) {
subrata_modakf17ac842009-09-07 09:06:31 +0000160
robbiew24e30ab2003-01-07 20:53:21 +0000161 test_name[0] = 'a' + i;
162 test_name[1] = '\0';
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 fd = open(test_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
subrata_modakf17ac842009-09-07 09:06:31 +0000164
165 if (fd < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800166 tst_brkm(TBROK | TERRNO, NULL, "Can't create %s/%s",
167 fuss, test_name);
subrata_modakf17ac842009-09-07 09:06:31 +0000168
169 if ((child = fork()) == 0) {
170 dotest(nchild, i, fd);
171 exit(0);
robbiew24e30ab2003-01-07 20:53:21 +0000172 }
subrata_modakf17ac842009-09-07 09:06:31 +0000173
robbiew24e30ab2003-01-07 20:53:21 +0000174 close(fd);
subrata_modakf17ac842009-09-07 09:06:31 +0000175
robbiew24e30ab2003-01-07 20:53:21 +0000176 if (child < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800177 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
robbiew24e30ab2003-01-07 20:53:21 +0000178 } else {
179 pidlist[i] = child;
180 nwait++;
181 }
182 }
183
184 /*
185 * Wait for children to finish.
186 */
robbiew24e30ab2003-01-07 20:53:21 +0000187 count = 0;
subrata_modakf17ac842009-09-07 09:06:31 +0000188 while (1) {
robbiew24e30ab2003-01-07 20:53:21 +0000189 if ((child = wait(&status)) >= 0) {
190 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 tst_resm(TFAIL,
192 "Test{%d} failed, expected 0 exit",
193 child);
robbiew24e30ab2003-01-07 20:53:21 +0000194 local_flag = FAILED;
195 }
196 ++count;
subrata_modakf17ac842009-09-07 09:06:31 +0000197 } else {
robbiew24e30ab2003-01-07 20:53:21 +0000198 if (errno != EINTR)
199 break;
200 }
201 }
202
203 /*
204 * Should have collected all children.
205 */
robbiew24e30ab2003-01-07 20:53:21 +0000206 if (count != nwait) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800207 tst_resm(TFAIL, "Wrong # children waited on, count = %d",
208 count);
robbiew24e30ab2003-01-07 20:53:21 +0000209 local_flag = FAILED;
210 }
211
subrata_modakf17ac842009-09-07 09:06:31 +0000212 if (local_flag == PASSED)
vapieraf64a872006-02-15 06:47:36 +0000213 tst_resm(TPASS, "Test passed in fork and wait.");
subrata_modakf17ac842009-09-07 09:06:31 +0000214 else
vapieraf64a872006-02-15 06:47:36 +0000215 tst_resm(TFAIL, "Test failed in fork and wait.");
robbiew24e30ab2003-01-07 20:53:21 +0000216
217 chdir(homedir);
218 pid = fork();
subrata_modakf17ac842009-09-07 09:06:31 +0000219
robbiew24e30ab2003-01-07 20:53:21 +0000220 if (pid < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800221 tst_brkm(TBROK | TERRNO, sync, "fork failed");
robbiew24e30ab2003-01-07 20:53:21 +0000222 tst_exit();
223 }
subrata_modakf17ac842009-09-07 09:06:31 +0000224
robbiew24e30ab2003-01-07 20:53:21 +0000225 if (pid == 0) {
robbiew7fdd5142005-02-07 19:39:54 +0000226 execl("/bin/rm", "rm", "-rf", fuss, NULL);
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800227 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000228 }
229
230 wait(&status);
robbiew24e30ab2003-01-07 20:53:21 +0000231
subrata_modakf17ac842009-09-07 09:06:31 +0000232 if (status)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800233 tst_resm(TINFO, "CAUTION - ftest1, '%s' may not be removed",
234 fuss);
subrata_modakf17ac842009-09-07 09:06:31 +0000235
236 sync();
robbiew24e30ab2003-01-07 20:53:21 +0000237}
238
239/*
240 * dotest()
241 * Children execute this.
242 *
243 * Randomly read/mod/write chunks with known pattern and check.
244 * When fill sectors, iterate.
245 */
246
247#define NMISC 4
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248enum m_type { m_fsync, m_trunc, m_sync, m_fstat };
249char *m_str[] = { "fsync", "trunc", "sync", "fstat" };
robbiew24e30ab2003-01-07 20:53:21 +0000250
Wanlong Gao354ebb42012-12-07 10:10:04 +0800251int misc_cnt[NMISC]; /* counts # of each kind of misc */
252int file_max; /* file-max size */
253int nchunks;
254int last_trunc = -1;
255int tr_flag;
256enum m_type type = m_fsync;
robbiew24e30ab2003-01-07 20:53:21 +0000257
258#define CHUNK(i) ((i) * csize)
259#define NEXTMISC ((rand() % misc_intvl) + 5)
260
Garrett Cooper72521cf2010-12-18 06:31:47 -0800261/* XXX (garrcoop): should not be using libltp as it runs forked. */
subrata_modakf17ac842009-09-07 09:06:31 +0000262static void dotest(int testers, int me, int fd)
robbiew24e30ab2003-01-07 20:53:21 +0000263{
subrata_modakf17ac842009-09-07 09:06:31 +0000264 char *bits, *hold_bits, *buf, *val_buf, *zero_buf;
265 char val;
266 int count, collide, chunk, whenmisc, xfr, i;
robbiew24e30ab2003-01-07 20:53:21 +0000267
268 nchunks = max_size / csize;
subrata_modakf17ac842009-09-07 09:06:31 +0000269
Wanlong Gao354ebb42012-12-07 10:10:04 +0800270 if ((bits = calloc((nchunks + 7) / 8, 1)) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100271 tst_brkm(TBROK,
272 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800273 "Test broken due to inability of malloc(bits).");
robbiew24e30ab2003-01-07 20:53:21 +0000274 }
subrata_modakf17ac842009-09-07 09:06:31 +0000275
Wanlong Gao354ebb42012-12-07 10:10:04 +0800276 if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100277 tst_brkm(TBROK,
278 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800279 "Test broken due to inability of malloc(hold_bits).");
robbiew24e30ab2003-01-07 20:53:21 +0000280 }
subrata_modakf17ac842009-09-07 09:06:31 +0000281
282 if ((buf = (calloc(csize, 1))) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100283 tst_brkm(TBROK, NULL,
284 "Test broken due to inability of malloc(buf).");
robbiew24e30ab2003-01-07 20:53:21 +0000285 }
subrata_modakf17ac842009-09-07 09:06:31 +0000286
287 if ((val_buf = (calloc(csize, 1))) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100288 tst_brkm(TBROK,
289 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800290 "Test broken due to inability of malloc(val_buf).");
robbiew24e30ab2003-01-07 20:53:21 +0000291 }
subrata_modakf17ac842009-09-07 09:06:31 +0000292
293 if ((zero_buf = (calloc(csize, 1))) == 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100294 tst_brkm(TBROK,
295 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800296 "Test broken due to inability of malloc(zero_buf).");
robbiew24e30ab2003-01-07 20:53:21 +0000297 }
298
299 /*
300 * No init sectors; allow file to be sparse.
301 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800302 val = (64 / testers) * me + 1;
robbiew24e30ab2003-01-07 20:53:21 +0000303
304 /*
305 * For each iteration:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800306 * zap bits array
307 * loop:
308 * pick random chunk, read it.
309 * if corresponding bit off {
310 * verify == 0. (sparse file)
311 * ++count;
312 * } else
313 * verify == val.
314 * write "val" on it.
315 * repeat until count = nchunks.
316 * ++val.
robbiew24e30ab2003-01-07 20:53:21 +0000317 */
robbiew24e30ab2003-01-07 20:53:21 +0000318 srand(getpid());
subrata_modakf17ac842009-09-07 09:06:31 +0000319
320 if (misc_intvl)
321 whenmisc = NEXTMISC;
322
323 while (iterations-- > 0) {
324 for (i = 0; i < NMISC; i++)
robbiew24e30ab2003-01-07 20:53:21 +0000325 misc_cnt[i] = 0;
326 ftruncate(fd, 0);
327 file_max = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800328 memset(bits, 0, (nchunks + 7) / 8);
329 memset(hold_bits, 0, (nchunks + 7) / 8);
subrata_modak04f47a12009-09-18 17:44:08 +0000330 memset(val_buf, val, csize);
331 memset(zero_buf, 0, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000332 count = 0;
333 collide = 0;
subrata_modakf17ac842009-09-07 09:06:31 +0000334 while (count < nchunks) {
robbiew24e30ab2003-01-07 20:53:21 +0000335 chunk = rand() % nchunks;
336 /*
337 * Read it.
338 */
subrata_modakf17ac842009-09-07 09:06:31 +0000339 if (lseek(fd, CHUNK(chunk), 0) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100340 tst_brkm(TFAIL,
341 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800342 "Test[%d]: lseek(0) fail at %x, errno = %d.",
343 me, CHUNK(chunk), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000344 }
345 if ((xfr = read(fd, buf, csize)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100346 tst_brkm(TFAIL,
347 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800348 "Test[%d]: read fail at %x, errno = %d.",
349 me, CHUNK(chunk), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000350 }
351 /*
352 * If chunk beyond EOF just write on it.
353 * Else if bit off, haven't seen it yet.
354 * Else, have. Verify values.
355 */
356 if (CHUNK(chunk) >= file_max) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800357 bits[chunk / 8] |= (1 << (chunk % 8));
robbiew24e30ab2003-01-07 20:53:21 +0000358 ++count;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800359 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
robbiew24e30ab2003-01-07 20:53:21 +0000360 if (xfr != csize) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100361 tst_brkm(TFAIL,
362 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800363 "Test[%d]: xfr=%d != %d, zero read.",
364 me, xfr, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000365 }
366 if (memcmp(buf, zero_buf, csize)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800367 tst_resm(TFAIL,
368 "Test[%d] bad verify @ 0x%x for val %d "
369 "count %d xfr %d file_max 0x%x, should be %d.",
370 me, CHUNK(chunk), val, count,
371 xfr, file_max, zero_buf[0]);
372 tst_resm(TFAIL,
373 "Test[%d]: last_trunc = 0x%x.",
374 me, last_trunc);
robbiew24e30ab2003-01-07 20:53:21 +0000375 sync();
subrata_modak04f47a12009-09-18 17:44:08 +0000376 ft_dumpbuf(buf, csize);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800377 ft_dumpbits(bits, (nchunks + 7) / 8);
378 ft_orbits(hold_bits, bits,
379 (nchunks + 7) / 8);
subrata_modak4bb656a2009-02-26 12:02:09 +0000380 tst_resm(TINFO, "Hold ");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800381 ft_dumpbits(hold_bits,
382 (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000383 tst_exit();
384 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800385 bits[chunk / 8] |= (1 << (chunk % 8));
robbiew24e30ab2003-01-07 20:53:21 +0000386 ++count;
387 } else {
388 if (xfr != csize) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100389 tst_brkm(TFAIL,
390 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800391 "\tTest[%d]: xfr=%d != %d, val read.",
392 me, xfr, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000393 }
394 ++collide;
395 if (memcmp(buf, val_buf, csize)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800396 tst_resm(TFAIL,
397 "Test[%d] bad verify @ 0x%x for val %d "
398 "count %d xfr %d file_max 0x%x.",
399 me, CHUNK(chunk), val, count,
400 xfr, file_max);
401 tst_resm(TFAIL,
402 "Test[%d]: last_trunc = 0x%x.",
403 me, last_trunc);
robbiew24e30ab2003-01-07 20:53:21 +0000404 sync();
subrata_modak04f47a12009-09-18 17:44:08 +0000405 ft_dumpbuf(buf, csize);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800406 ft_dumpbits(bits, (nchunks + 7) / 8);
407 ft_orbits(hold_bits, bits,
408 (nchunks + 7) / 8);
subrata_modak4bb656a2009-02-26 12:02:09 +0000409 tst_resm(TINFO, "Hold ");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800410 ft_dumpbits(hold_bits,
411 (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000412 tst_exit();
413 }
414 }
415 /*
416 * Write it.
417 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800418 if (lseek(fd, -xfr, 1) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100419 tst_brkm(TFAIL,
420 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800421 "Test[%d]: lseek(1) fail at %x, errno = %d.",
422 me, CHUNK(chunk), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000423 }
424 if ((xfr = write(fd, val_buf, csize)) < csize) {
425 if (errno == ENOSPC) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800426 tst_resm(TFAIL,
427 "Test[%d]: no space, exiting.",
428 me);
robbiew24e30ab2003-01-07 20:53:21 +0000429 fsync(fd);
430 tst_exit();
431 }
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100432 tst_brkm(TFAIL,
433 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800434 "Test[%d]: write fail at %x xfr %d, errno = %d.",
435 me, CHUNK(chunk), xfr, errno);
robbiew24e30ab2003-01-07 20:53:21 +0000436 }
437 if (CHUNK(chunk) + csize > file_max)
438 file_max = CHUNK(chunk) + csize;
439 /*
440 * If hit "misc" interval, do it.
441 */
442 if (misc_intvl && --whenmisc <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800443 ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000444 domisc(me, fd, bits);
445 whenmisc = NEXTMISC;
446 }
447 if (count + collide > 2 * nchunks)
448 break;
449 }
450
451 /*
452 * End of iteration, maybe before doing all chunks.
453 */
robbiew24e30ab2003-01-07 20:53:21 +0000454 fsync(fd);
subrata_modakf17ac842009-09-07 09:06:31 +0000455 ++misc_cnt[m_fsync];
vapieraf64a872006-02-15 06:47:36 +0000456 //tst_resm(TINFO, "Test{%d} val %d done, count = %d, collide = {%d}",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800457 // me, val, count, collide);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800458 //for (i = 0; i < NMISC; i++)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800459 // tst_resm(TINFO, "Test{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
robbiew24e30ab2003-01-07 20:53:21 +0000460 ++val;
461 }
robbiew24e30ab2003-01-07 20:53:21 +0000462}
463
464/*
465 * domisc()
466 * Inject misc syscalls into the thing.
467 */
subrata_modakf17ac842009-09-07 09:06:31 +0000468static void domisc(int me, int fd, char *bits)
robbiew24e30ab2003-01-07 20:53:21 +0000469{
subrata_modakf17ac842009-09-07 09:06:31 +0000470 int chunk;
471 struct stat sb;
robbiew24e30ab2003-01-07 20:53:21 +0000472
subrata_modakf17ac842009-09-07 09:06:31 +0000473 if (type > m_fstat)
robbiew24e30ab2003-01-07 20:53:21 +0000474 type = m_fsync;
subrata_modakf17ac842009-09-07 09:06:31 +0000475 switch (type) {
robbiew24e30ab2003-01-07 20:53:21 +0000476 case m_fsync:
477 if (fsync(fd) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800478 tst_brkm(TFAIL | TERRNO, NULL,
479 "Test[%d]: fsync failed.", me);
robbiew24e30ab2003-01-07 20:53:21 +0000480 }
481 break;
482 case m_trunc:
483 chunk = rand() % (file_max / csize);
484 file_max = CHUNK(chunk);
485 last_trunc = file_max;
486 if (tr_flag) {
487 if (ftruncate(fd, file_max) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800488 tst_brkm(TFAIL | TERRNO, NULL,
489 "Test[%d]: ftruncate failed @ 0x%x.",
490 me, file_max);
robbiew24e30ab2003-01-07 20:53:21 +0000491 }
492 tr_flag = 0;
493 } else {
494 if (truncate(test_name, file_max) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800495 tst_brkm(TFAIL | TERRNO, NULL,
496 "Test[%d]: truncate failed @ 0x%x.",
497 me, file_max);
robbiew24e30ab2003-01-07 20:53:21 +0000498 }
499 tr_flag = 1;
500 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800501 for (; chunk % 8 != 0; chunk++)
502 bits[chunk / 8] &= ~(1 << (chunk % 8));
subrata_modakf17ac842009-09-07 09:06:31 +0000503 for (; chunk < nchunks; chunk += 8)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800504 bits[chunk / 8] = 0;
robbiew24e30ab2003-01-07 20:53:21 +0000505 break;
506 case m_sync:
507 sync();
508 break;
509 case m_fstat:
Garrett Cooper7d716be2010-12-19 07:44:35 -0800510 if (fstat(fd, &sb) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800511 tst_brkm(TFAIL | TERRNO, NULL,
512 "\tTest[%d]: fstat failed", me);
Garrett Cooper7d716be2010-12-19 07:44:35 -0800513 if (sb.st_size != file_max)
514 tst_brkm(TFAIL, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800515 "\tTest[%d]: fstat() mismatch; st_size=%lu, "
516 "file_max=%x.", me, sb.st_size, file_max);
robbiew24e30ab2003-01-07 20:53:21 +0000517 break;
518 }
subrata_modakf17ac842009-09-07 09:06:31 +0000519
520 ++misc_cnt[type];
521 ++type;
robbiew24e30ab2003-01-07 20:53:21 +0000522}
523
subrata_modakf17ac842009-09-07 09:06:31 +0000524/*
525 * SIGTERM signal handler.
robbiew24e30ab2003-01-07 20:53:21 +0000526 */
subrata_modak04f47a12009-09-18 17:44:08 +0000527static void term(int sig LTP_ATTRIBUTE_UNUSED)
robbiew24e30ab2003-01-07 20:53:21 +0000528{
subrata_modakf17ac842009-09-07 09:06:31 +0000529 int i;
robbiew24e30ab2003-01-07 20:53:21 +0000530
vapieraf64a872006-02-15 06:47:36 +0000531 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
robbiew24e30ab2003-01-07 20:53:21 +0000532
533 /*
534 * If run by hand we like to have the parent send the signal to
subrata_modakf17ac842009-09-07 09:06:31 +0000535 * the child processes.
robbiew24e30ab2003-01-07 20:53:21 +0000536 */
robbiew24e30ab2003-01-07 20:53:21 +0000537 if (parent_pid == getpid()) {
subrata_modakf17ac842009-09-07 09:06:31 +0000538 for (i = 0; i < nchild; i++)
539 if (pidlist[i])
robbiew24e30ab2003-01-07 20:53:21 +0000540 kill(pidlist[i], SIGTERM);
541 tst_exit();
542 }
543
vapieraf64a872006-02-15 06:47:36 +0000544 tst_resm(TINFO, "\tunlinking '%s'", test_name);
robbiew24e30ab2003-01-07 20:53:21 +0000545
546 close(fd);
subrata_modakf17ac842009-09-07 09:06:31 +0000547
Garrett Cooper7d716be2010-12-19 07:44:35 -0800548 if (unlink(test_name) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800549 tst_resm(TBROK | TERRNO, "unlink failed");
subrata_modakf17ac842009-09-07 09:06:31 +0000550
robbiew24e30ab2003-01-07 20:53:21 +0000551 tst_exit();
552}
553
subrata_modakf17ac842009-09-07 09:06:31 +0000554static void cleanup(void)
robbiew24e30ab2003-01-07 20:53:21 +0000555{
robbiew24e30ab2003-01-07 20:53:21 +0000556
557 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700558}