blob: 2855f46e2a446878fa56e9168942f8705569b40f [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
21/*
22 * NAME
23 * ftest08.c -- test single file io (tsfio.c by rbk) (ported from SPIE,
24 * section2/filesuite/ftest10.c, by Airong Zhang)
25 *
26 * this is the same as ftest5, except that it uses lseek64
27 *
28 * CALLS
29 * fsync, sync, lseek64, read, write
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
robbiew24e30ab2003-01-07 20:53:21 +000031 *
32 * ALGORITHM
33 * Several child processes doing random seeks, read/write
34 * operations on the same file.
35 *
36 *
37 * RESTRICTIONS
38 * Runs a long time with default args - can take others on input
39 * line. Use with "term mode".
subrata_modakbdbaec52009-02-26 12:14:51 +000040 *
robbiew24e30ab2003-01-07 20:53:21 +000041 */
42
43#define _XOPEN_SOURCE 500
subrata_modak04f47a12009-09-18 17:44:08 +000044#define _LARGEFILE64_SOURCE 1
45#include <stdio.h>
robbiew24e30ab2003-01-07 20:53:21 +000046#include <sys/types.h>
47#include <sys/param.h>
48#include <sys/wait.h>
49#include <sys/file.h>
50#include <sys/fcntl.h>
51#include <sys/stat.h>
52#include <sys/uio.h>
vapierf81795e2006-02-15 06:28:58 +000053#include <errno.h>
subrata_modak04f47a12009-09-18 17:44:08 +000054#include <signal.h>
robbiew24e30ab2003-01-07 20:53:21 +000055#include <unistd.h>
rootad99f102010-02-19 01:24:05 -080056#include <inttypes.h>
robbiew24e30ab2003-01-07 20:53:21 +000057#include "test.h"
subrata_modak04f47a12009-09-18 17:44:08 +000058#include "libftest.h"
robbiew24e30ab2003-01-07 20:53:21 +000059
60char *TCID = "ftest08";
61int TST_TOTAL = 1;
robbiew24e30ab2003-01-07 20:53:21 +000062
63#define PASSED 1
64#define FAILED 0
65
subrata_modak04f47a12009-09-18 17:44:08 +000066#define MAXCHILD 25
robbiew24e30ab2003-01-07 20:53:21 +000067#define K_1 1024
68#define K_2 2048
69#define K_4 4096
70#define MAXIOVCNT 16
71
subrata_modak04f47a12009-09-18 17:44:08 +000072static void init(void);
73static void runtest(void);
74static void dotest(int, int, int);
75static void domisc(int, int);
76static void term(int sig);
77static void cleanup(void);
robbiew24e30ab2003-01-07 20:53:21 +000078
Wanlong Gao354ebb42012-12-07 10:10:04 +080079static int csize; /* chunk size */
80static int iterations; /* # total iterations */
81static off64_t max_size; /* max file size */
82static int misc_intvl; /* for doing misc things; 0 ==> no */
83static int nchild; /* number of child processes */
subrata_modak04f47a12009-09-18 17:44:08 +000084static int parent_pid;
85static int pidlist[MAXCHILD];
robbiew24e30ab2003-01-07 20:53:21 +000086
Wanlong Gao354ebb42012-12-07 10:10:04 +080087static char filename[MAXPATHLEN];
88static int local_flag;
robbiew24e30ab2003-01-07 20:53:21 +000089
subrata_modak04f47a12009-09-18 17:44:08 +000090int main(int ac, char *av[])
robbiew24e30ab2003-01-07 20:53:21 +000091{
Wanlong Gao354ebb42012-12-07 10:10:04 +080092 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020093 const char *msg;
robbiew24e30ab2003-01-07 20:53:21 +000094
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020095 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Cyril Hrubisee7667e2014-09-24 13:14:56 +020096 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew24e30ab2003-01-07 20:53:21 +000097
98 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
robbiew24e30ab2003-01-07 20:53:21 +0000100 local_flag = PASSED;
robbiew24e30ab2003-01-07 20:53:21 +0000101 init();
robbiew24e30ab2003-01-07 20:53:21 +0000102 runtest();
103
subrata_modak04f47a12009-09-18 17:44:08 +0000104 if (local_flag == PASSED)
105 tst_resm(TPASS, "Test passed.");
106 else
107 tst_resm(TFAIL, "Test failed.");
108 }
robbiew24e30ab2003-01-07 20:53:21 +0000109
robbiew24e30ab2003-01-07 20:53:21 +0000110 cleanup();
Garrett Cooper7d716be2010-12-19 07:44:35 -0800111 tst_exit();
robbiew24e30ab2003-01-07 20:53:21 +0000112}
113
subrata_modak04f47a12009-09-18 17:44:08 +0000114static void init(void)
robbiew24e30ab2003-01-07 20:53:21 +0000115{
116 int fd;
117 char wdbuf[MAXPATHLEN];
118
119 parent_pid = getpid();
120 tst_tmpdir();
subrata_modak04f47a12009-09-18 17:44:08 +0000121
robbiew24e30ab2003-01-07 20:53:21 +0000122 /*
123 * Make a filename for the test.
124 */
robbiew24e30ab2003-01-07 20:53:21 +0000125 if (!filename[0])
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 sprintf(filename, "%s/ftest08.%d", getcwd(wdbuf, MAXPATHLEN),
127 getpid());
robbiew24e30ab2003-01-07 20:53:21 +0000128
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
subrata_modak04f47a12009-09-18 17:44:08 +0000130
robbiew24e30ab2003-01-07 20:53:21 +0000131 if (fd < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100132 tst_brkm(TBROK, NULL, "Error %d creating file %s", errno,
133 filename);
robbiew24e30ab2003-01-07 20:53:21 +0000134 }
subrata_modak04f47a12009-09-18 17:44:08 +0000135
robbiew24e30ab2003-01-07 20:53:21 +0000136 close(fd);
137
138 /*
139 * Default values for run conditions.
140 */
robbiew24e30ab2003-01-07 20:53:21 +0000141 iterations = 10;
142 nchild = 5;
143 csize = K_2; /* should run with 1, 2, and 4 K sizes */
144 max_size = K_1 * K_1;
145 misc_intvl = 10;
146
subrata_modak04f47a12009-09-18 17:44:08 +0000147 if (sigset(SIGTERM, term) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 tst_brkm(TBROK | TERRNO, NULL, "first sigset failed");
robbiew24e30ab2003-01-07 20:53:21 +0000149 }
150
151}
152
subrata_modak04f47a12009-09-18 17:44:08 +0000153static void runtest(void)
robbiew24e30ab2003-01-07 20:53:21 +0000154{
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800155 int child, count, fd, i, nwait, status;
156
157 nwait = 0;
robbiew24e30ab2003-01-07 20:53:21 +0000158
subrata_modak04f47a12009-09-18 17:44:08 +0000159 for (i = 0; i < nchild; i++) {
robbiew24e30ab2003-01-07 20:53:21 +0000160
subrata_modak04f47a12009-09-18 17:44:08 +0000161 if ((child = fork()) == 0) {
robbiew24e30ab2003-01-07 20:53:21 +0000162 fd = open(filename, O_RDWR);
163 if (fd < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100164 tst_brkm(TFAIL,
165 NULL,
166 "\tTest[%d]: error %d openning %s.",
167 i,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 errno, filename);
robbiew24e30ab2003-01-07 20:53:21 +0000169 }
subrata_modak04f47a12009-09-18 17:44:08 +0000170 dotest(nchild, i, fd);
subrata_modak9320b112008-08-27 11:26:51 +0000171 close(fd);
subrata_modak04f47a12009-09-18 17:44:08 +0000172 tst_exit();
robbiew24e30ab2003-01-07 20:53:21 +0000173 }
subrata_modak04f47a12009-09-18 17:44:08 +0000174
robbiew24e30ab2003-01-07 20:53:21 +0000175 if (child < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800176 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
robbiew24e30ab2003-01-07 20:53:21 +0000177 } else {
178 pidlist[i] = child;
179 nwait++;
180 }
181 }
182
183 /*
184 * Wait for children to finish.
185 */
robbiew24e30ab2003-01-07 20:53:21 +0000186 count = 0;
subrata_modak04f47a12009-09-18 17:44:08 +0000187 while ((child = wait(&status)) != -1 || errno == EINTR) {
188 if (child > 0) {
vapieraf64a872006-02-15 06:47:36 +0000189 //tst_resm(TINFO, "\tTest{%d} exited status = 0x%x", child, status);
robbiew24e30ab2003-01-07 20:53:21 +0000190 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 tst_resm(TFAIL,
192 "\tExpected 0 exit status - failed.");
robbiew24e30ab2003-01-07 20:53:21 +0000193 local_flag = FAILED;
194 }
195 ++count;
196 }
197 }
198
199 /*
200 * Should have collected all children.
201 */
robbiew24e30ab2003-01-07 20:53:21 +0000202 if (count != nwait) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
204 count);
robbiew24e30ab2003-01-07 20:53:21 +0000205 local_flag = FAILED;
206 }
207
208 unlink(filename);
subrata_modak04f47a12009-09-18 17:44:08 +0000209 sync();
robbiew24e30ab2003-01-07 20:53:21 +0000210}
211
212/*
213 * dotest()
214 * Children execute this.
215 *
216 * Randomly read/mod/write chunks with known pattern and check.
217 * When fill sectors, iterate.
218 */
robbiew24e30ab2003-01-07 20:53:21 +0000219#define NMISC 2
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220enum m_type { m_fsync, m_sync };
221char *m_str[] = { "fsync", "sync" };
robbiew24e30ab2003-01-07 20:53:21 +0000222
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223int misc_cnt[NMISC]; /* counts # of each kind of misc */
224int misc_flag;
225int nchunks;
robbiew24e30ab2003-01-07 20:53:21 +0000226
227#define CHUNK(i) ((((off64_t)i) * testers + me) * csize)
228#define NEXTMISC ((rand() % misc_intvl) + 5)
229
subrata_modak04f47a12009-09-18 17:44:08 +0000230static void dotest(int testers, int me, int fd)
robbiew24e30ab2003-01-07 20:53:21 +0000231{
Cyril Hrubis82ddd5f2013-06-04 19:33:07 +0200232 char *bits;
subrata_modak04f47a12009-09-18 17:44:08 +0000233 char val, val0;
234 int count, collide, chunk, whenmisc, xfr, i;
robbiew24e30ab2003-01-07 20:53:21 +0000235
236 /* Stuff for the readv call */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 struct iovec r_iovec[MAXIOVCNT];
238 int r_ioveclen;
robbiew24e30ab2003-01-07 20:53:21 +0000239
240 /* Stuff for the writev call */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800241 struct iovec val0_iovec[MAXIOVCNT];
242 struct iovec val_iovec[MAXIOVCNT];
243 int w_ioveclen;
robbiew24e30ab2003-01-07 20:53:21 +0000244
245 nchunks = max_size / (testers * csize);
Garrett Cooperf01306b2010-11-05 11:14:08 -0700246 whenmisc = 0;
subrata_modak04f47a12009-09-18 17:44:08 +0000247
Wanlong Gao354ebb42012-12-07 10:10:04 +0800248 if ((bits = malloc((nchunks + 7) / 8)) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100249 tst_brkm(TBROK, NULL, "\tmalloc failed(bits)");
robbiew24e30ab2003-01-07 20:53:21 +0000250 }
subrata_modak04f47a12009-09-18 17:44:08 +0000251
subrata_modak04f47a12009-09-18 17:44:08 +0000252 /* Allocate memory for the iovec buffers and init the iovec arrays */
robbiew24e30ab2003-01-07 20:53:21 +0000253 r_ioveclen = w_ioveclen = csize / MAXIOVCNT;
254
subrata_modak04f47a12009-09-18 17:44:08 +0000255 /* Please note that the above statement implies that csize
256 * be evenly divisible by MAXIOVCNT.
257 */
robbiew24e30ab2003-01-07 20:53:21 +0000258 for (i = 0; i < MAXIOVCNT; i++) {
subrata_modak04f47a12009-09-18 17:44:08 +0000259 if ((r_iovec[i].iov_base = malloc(r_ioveclen)) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100260 tst_brkm(TBROK, NULL, "\tmalloc failed(iov_base)");
robbiew24e30ab2003-01-07 20:53:21 +0000261 }
262 r_iovec[i].iov_len = r_ioveclen;
263
264 /* Allocate unused memory areas between all the buffers to
265 * make things more diffult for the OS.
266 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800267 if (malloc((i + 1) * 8) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100268 tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)");
robbiew24e30ab2003-01-07 20:53:21 +0000269 }
subrata_modak04f47a12009-09-18 17:44:08 +0000270
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800271 if ((val0_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100272 tst_brkm(TBROK, NULL, "\tmalloc failed(val0_iovec)");
robbiew24e30ab2003-01-07 20:53:21 +0000273 }
subrata_modak04f47a12009-09-18 17:44:08 +0000274
robbiew24e30ab2003-01-07 20:53:21 +0000275 val0_iovec[i].iov_len = w_ioveclen;
Garrett Cooper2c282152010-12-16 00:55:50 -0800276
Wanlong Gao354ebb42012-12-07 10:10:04 +0800277 if (malloc((i + 1) * 8) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100278 tst_brkm(TBROK, NULL, "\tmalloc failed((i+1)*8)");
robbiew24e30ab2003-01-07 20:53:21 +0000279 }
subrata_modak04f47a12009-09-18 17:44:08 +0000280
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800281 if ((val_iovec[i].iov_base = malloc(w_ioveclen)) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100282 tst_brkm(TBROK, NULL, "\tmalloc failed(iov_base)");
robbiew24e30ab2003-01-07 20:53:21 +0000283 }
284 val_iovec[i].iov_len = w_ioveclen;
285
Wanlong Gao354ebb42012-12-07 10:10:04 +0800286 if (malloc((i + 1) * 8) == NULL) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100287 tst_brkm(TBROK, NULL, "\tmalloc failed(((i+1)*8)");
robbiew24e30ab2003-01-07 20:53:21 +0000288 }
289 }
290
291 /*
292 * No init sectors; file-sys makes 0 to start.
293 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800294 val = (64 / testers) * me + 1;
robbiew24e30ab2003-01-07 20:53:21 +0000295 val0 = 0;
296
297 /*
298 * For each iteration:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800299 * zap bits array
300 * loop:
301 * pick random chunk, read it.
302 * if corresponding bit off {
303 * verify == 0. (sparse file)
304 * ++count;
305 * } else
306 * verify == val.
307 * write "val" on it.
308 * repeat until count = nchunks.
309 * ++val.
robbiew24e30ab2003-01-07 20:53:21 +0000310 */
robbiew24e30ab2003-01-07 20:53:21 +0000311 srand(getpid());
subrata_modak04f47a12009-09-18 17:44:08 +0000312
313 if (misc_intvl)
314 whenmisc = NEXTMISC;
315
316 while (iterations-- > 0) {
317 for (i = 0; i < NMISC; i++)
robbiew24e30ab2003-01-07 20:53:21 +0000318 misc_cnt[i] = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800319 memset(bits, 0, (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000320 /* Have to fill the val0 and val iov buffers in a different manner
321 */
subrata_modak04f47a12009-09-18 17:44:08 +0000322 for (i = 0; i < MAXIOVCNT; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323 memset(val0_iovec[i].iov_base, val0,
324 val0_iovec[i].iov_len);
325 memset(val_iovec[i].iov_base, val,
326 val_iovec[i].iov_len);
robbiew24e30ab2003-01-07 20:53:21 +0000327
328 }
subrata_modak04f47a12009-09-18 17:44:08 +0000329
robbiew24e30ab2003-01-07 20:53:21 +0000330 count = 0;
331 collide = 0;
subrata_modak04f47a12009-09-18 17:44:08 +0000332
333 while (count < nchunks) {
robbiew24e30ab2003-01-07 20:53:21 +0000334 chunk = rand() % nchunks;
335 /*
336 * Read it.
337 */
subrata_modak04f47a12009-09-18 17:44:08 +0000338 if (lseek64(fd, CHUNK(chunk), 0) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100339 tst_brkm(TFAIL,
340 NULL, "\tTest[%d]: lseek64(0) fail at %"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800341 PRIx64 "x, errno = %d.", me,
342 CHUNK(chunk), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000343 }
344 if ((xfr = readv(fd, &r_iovec[0], MAXIOVCNT)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100345 tst_brkm(TFAIL,
346 NULL, "\tTest[%d]: readv fail at %" PRIx64
Wanlong Gao354ebb42012-12-07 10:10:04 +0800347 "x, errno = %d.", me, CHUNK(chunk),
348 errno);
robbiew24e30ab2003-01-07 20:53:21 +0000349 }
350 /*
351 * If chunk beyond EOF just write on it.
352 * Else if bit off, haven't seen it yet.
353 * Else, have. Verify values.
354 */
355 if (xfr == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800356 bits[chunk / 8] |= (1 << (chunk % 8));
357 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
robbiew24e30ab2003-01-07 20:53:21 +0000358 if (xfr != csize) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100359 tst_brkm(TFAIL,
360 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800361 "\tTest[%d]: xfr=%d != %d, zero read.",
362 me, xfr, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000363 }
subrata_modak04f47a12009-09-18 17:44:08 +0000364 for (i = 0; i < MAXIOVCNT; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800365 if (memcmp
366 (r_iovec[i].iov_base,
367 val0_iovec[i].iov_base,
368 r_iovec[i].iov_len)) {
369 tst_resm(TFAIL,
370 "\tTest[%d] bad verify @ 0x%"
371 PRIx64
372 " for val %d count %d xfr %d.",
373 me, CHUNK(chunk), val0,
374 count, xfr);
subrata_modak04f47a12009-09-18 17:44:08 +0000375 ft_dumpiov(&r_iovec[i]);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800376 ft_dumpbits(bits,
377 (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000378 tst_exit();
379 }
380 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800381 bits[chunk / 8] |= (1 << (chunk % 8));
robbiew24e30ab2003-01-07 20:53:21 +0000382 ++count;
383 } else {
384 if (xfr != csize) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100385 tst_brkm(TFAIL,
386 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800387 "\tTest[%d]: xfr=%d != %d, val read.",
388 me, xfr, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000389 }
390 ++collide;
subrata_modak04f47a12009-09-18 17:44:08 +0000391 for (i = 0; i < MAXIOVCNT; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800392 if (memcmp
393 (r_iovec[i].iov_base,
394 val_iovec[i].iov_base,
395 r_iovec[i].iov_len)) {
396 tst_resm(TFAIL,
397 "\tTest[%d] bad verify @ 0x%"
398 PRIx64
399 " for val %d count %d xfr %d.",
400 me, CHUNK(chunk), val,
401 count, xfr);
subrata_modak04f47a12009-09-18 17:44:08 +0000402 ft_dumpiov(&r_iovec[i]);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800403 ft_dumpbits(bits,
404 (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000405 tst_exit();
406 }
407 }
408 }
409 /*
410 * Write it.
411 */
subrata_modak04f47a12009-09-18 17:44:08 +0000412 if (lseek64(fd, -xfr, 1) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100413 tst_brkm(TFAIL,
414 NULL, "\tTest[%d]: lseek64(1) fail at %"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800415 PRIx64 ", errno = %d.", me,
416 CHUNK(chunk), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000417 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800418 if ((xfr =
419 writev(fd, &val_iovec[0], MAXIOVCNT)) < csize) {
robbiew24e30ab2003-01-07 20:53:21 +0000420 if (errno == ENOSPC) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800421 tst_resm(TFAIL,
422 "\tTest[%d]: no space, exiting.",
423 me);
robbiew24e30ab2003-01-07 20:53:21 +0000424 fsync(fd);
425 tst_exit();
426 }
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100427 tst_brkm(TFAIL,
428 NULL, "\tTest[%d]: writev fail at %" PRIx64
Wanlong Gao354ebb42012-12-07 10:10:04 +0800429 "x xfr %d, errno = %d.", me,
430 CHUNK(chunk), xfr, errno);
robbiew24e30ab2003-01-07 20:53:21 +0000431 }
432 /*
433 * If hit "misc" interval, do it.
434 */
435 if (misc_intvl && --whenmisc <= 0) {
subrata_modak04f47a12009-09-18 17:44:08 +0000436 domisc(me, fd);
robbiew24e30ab2003-01-07 20:53:21 +0000437 whenmisc = NEXTMISC;
438 }
439 if (count + collide > 2 * nchunks)
440 break;
441 }
442
443 /*
444 * End of iteration, maybe before doing all chunks.
445 */
446
447 if (count < nchunks) {
vapieraf64a872006-02-15 06:47:36 +0000448 //tst_resm(TINFO, "\tTest{%d} val %d stopping @ %d, collide = {%d}.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800449 // me, val, count, collide);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800450 for (i = 0; i < nchunks; i++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800451 if ((bits[i / 8] & (1 << (i % 8))) == 0) {
452 if (lseek64(fd, CHUNK(i), 0) <
453 (off64_t) 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100454 tst_brkm(TFAIL,
455 NULL, "\tTest[%d]: lseek64 fail at %"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800456 PRIx64
457 "x, errno = %d.", me,
458 CHUNK(i), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000459 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800460 if (writev(fd, &val_iovec[0], MAXIOVCNT)
461 != csize) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100462 tst_brkm(TFAIL,
463 NULL, "\tTest[%d]: writev fail at %"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800464 PRIx64
465 "x, errno = %d.", me,
466 CHUNK(i), errno);
robbiew24e30ab2003-01-07 20:53:21 +0000467 }
468 }
469 }
470 }
471
472 fsync(fd);
subrata_modak04f47a12009-09-18 17:44:08 +0000473 ++misc_cnt[m_fsync];
vapieraf64a872006-02-15 06:47:36 +0000474 //tst_resm(TINFO, "\tTest[%d] val %d done, count = %d, collide = %d.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800475 // me, val, count, collide);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800476 //for (i = 0; i < NMISC; i++)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800477 // tst_resm(TINFO, "\t\tTest[%d]: %d %s's.", me, misc_cnt[i], m_str[i]);
robbiew24e30ab2003-01-07 20:53:21 +0000478 val0 = val++;
479 }
robbiew24e30ab2003-01-07 20:53:21 +0000480}
481
482/*
483 * domisc()
484 * Inject misc syscalls into the thing.
485 */
subrata_modak04f47a12009-09-18 17:44:08 +0000486static void domisc(int me, int fd)
robbiew24e30ab2003-01-07 20:53:21 +0000487{
subrata_modak04f47a12009-09-18 17:44:08 +0000488 enum m_type type;
robbiew24e30ab2003-01-07 20:53:21 +0000489
490 if (misc_flag) {
491 type = m_fsync;
492 misc_flag = 0;
493 } else {
494 type = m_sync;;
495 misc_flag = 1;
496 }
subrata_modak04f47a12009-09-18 17:44:08 +0000497
Wanlong Gao354ebb42012-12-07 10:10:04 +0800498 switch (type) {
499 case m_fsync:
500 if (fsync(fd) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100501 tst_brkm(TFAIL, NULL, "\tTest[%d]: fsync error %d.",
502 me,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800503 errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800504 }
robbiew24e30ab2003-01-07 20:53:21 +0000505 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800506 case m_sync:
507 sync();
robbiew24e30ab2003-01-07 20:53:21 +0000508 break;
509 }
subrata_modak04f47a12009-09-18 17:44:08 +0000510
511 ++misc_cnt[type];
robbiew24e30ab2003-01-07 20:53:21 +0000512}
513
subrata_modak04f47a12009-09-18 17:44:08 +0000514static void term(int sig LTP_ATTRIBUTE_UNUSED)
robbiew24e30ab2003-01-07 20:53:21 +0000515{
subrata_modak04f47a12009-09-18 17:44:08 +0000516 int i;
robbiew24e30ab2003-01-07 20:53:21 +0000517
vapieraf64a872006-02-15 06:47:36 +0000518 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
robbiew24e30ab2003-01-07 20:53:21 +0000519
520 if (parent_pid == getpid()) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800521 for (i = 0; i < nchild; i++)
subrata_modak04f47a12009-09-18 17:44:08 +0000522 if (pidlist[i])
robbiew24e30ab2003-01-07 20:53:21 +0000523 kill(pidlist[i], SIGTERM);
subrata_modak04f47a12009-09-18 17:44:08 +0000524 return;
robbiew24e30ab2003-01-07 20:53:21 +0000525 }
526
527 tst_exit();
robbiew24e30ab2003-01-07 20:53:21 +0000528}
529
subrata_modak04f47a12009-09-18 17:44:08 +0000530void cleanup(void)
robbiew24e30ab2003-01-07 20:53:21 +0000531{
robbiew24e30ab2003-01-07 20:53:21 +0000532
Wanlong Gao354ebb42012-12-07 10:10:04 +0800533 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700534}