blob: 2b7ad1397a90615f85d7642e7030b5e2ce2c4842 [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 * ftest05.c -- test file I/O (ported from SPIE, section2/filesuite/ftest6.c, by Airong Zhang)
24 *
25 * this is the same as ftest1, except that it uses lseek64
26 *
27 * CALLS
28 * lseek64, read, write
29 * truncate, ftruncate, fsync, sync, fstat
30 *
31 * ALGORITHM
32 * A bitmap is used to map pieces of a file.
33 * Loop: pick a random piece of the file
34 * if we haven't seen it before make sure it is zero,
35 * write pattern
36 * if we have seen it before make sure correct pattern.
37 *
38 * This was originally written by rbk - was program tfio.c
39 * Modified by dale to integrate with test suites.
40 *
41 * RESTRICTIONS
42 * Runs a long time with default args - can take others on input
43 * line. Use with "term mode".
44 * If run on vax the ftruncate will not be random - will always go to
45 * start of file. NOTE: produces a very high load average!!
46 *
47 * CAUTION!!
48 * If a file is supplied to this program with the "-f" option
49 * it will be removed with a system("rm -rf filename") call.
subrata_modakbdbaec52009-02-26 12:14:51 +000050 *
robbiew24e30ab2003-01-07 20:53:21 +000051 */
52
53#define _XOPEN_SOURCE 500
subrata_modak04f47a12009-09-18 17:44:08 +000054#define _LARGEFILE64_SOURCE 1
55#include <stdio.h>
robbiew24e30ab2003-01-07 20:53:21 +000056#include <sys/types.h>
57#include <sys/wait.h>
58#include <sys/stat.h>
vapierf81795e2006-02-15 06:28:58 +000059#include <errno.h>
robbiew24e30ab2003-01-07 20:53:21 +000060#include <fcntl.h>
subrata_modak04f47a12009-09-18 17:44:08 +000061#include <signal.h>
robbiew24e30ab2003-01-07 20:53:21 +000062#include <unistd.h>
subrata_modak04f47a12009-09-18 17:44:08 +000063#include <inttypes.h>
robbiew24e30ab2003-01-07 20:53:21 +000064#include "test.h"
subrata_modak04f47a12009-09-18 17:44:08 +000065#include "libftest.h"
robbiew24e30ab2003-01-07 20:53:21 +000066
67char *TCID = "ftest05";
68int TST_TOTAL = 1;
robbiew24e30ab2003-01-07 20:53:21 +000069
subrata_modak04f47a12009-09-18 17:44:08 +000070static void setup(void);
71static void runtest();
72static void dotest(int, int, int);
Wanlong Gao354ebb42012-12-07 10:10:04 +080073static void domisc(int, int, char *);
subrata_modak04f47a12009-09-18 17:44:08 +000074static void term(int sig);
75static void cleanup(void);
robbiew24e30ab2003-01-07 20:53:21 +000076
77#define PASSED 1
78#define FAILED 0
79
subrata_modak04f47a12009-09-18 17:44:08 +000080#define MAXCHILD 25
robbiew24e30ab2003-01-07 20:53:21 +000081#define K_1 1024
82#define K_2 2048
83#define K_4 4096
84
Wanlong Gao354ebb42012-12-07 10:10:04 +080085static int csize; /* chunk size */
86static int iterations; /* # total iterations */
87static off64_t max_size; /* max file size */
88static int misc_intvl; /* for doing misc things; 0 ==> no */
89static int nchild; /* how many children */
90static int fd; /* file descriptor used by child */
subrata_modak04f47a12009-09-18 17:44:08 +000091static int parent_pid;
92static int pidlist[MAXCHILD];
Wanlong Gao354ebb42012-12-07 10:10:04 +080093static char test_name[2]; /* childs test directory name */
robbiew24e30ab2003-01-07 20:53:21 +000094
Wanlong Gao354ebb42012-12-07 10:10:04 +080095static char fuss[MAXPATHLEN]; /* directory to do this in */
96static char homedir[MAXPATHLEN]; /* where we started */
robbiew24e30ab2003-01-07 20:53:21 +000097
subrata_modak04f47a12009-09-18 17:44:08 +000098static int local_flag;
robbiew24e30ab2003-01-07 20:53:21 +000099
subrata_modak04f47a12009-09-18 17:44:08 +0000100int main(int ac, char *av[])
robbiew24e30ab2003-01-07 20:53:21 +0000101{
subrata_modak04f47a12009-09-18 17:44:08 +0000102 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200103 const char *msg;
robbiew24e30ab2003-01-07 20:53:21 +0000104
subrata_modak04f47a12009-09-18 17:44:08 +0000105 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Cyril Hrubisee7667e2014-09-24 13:14:56 +0200106 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew24e30ab2003-01-07 20:53:21 +0000107
subrata_modak04f47a12009-09-18 17:44:08 +0000108 setup();
robbiew24e30ab2003-01-07 20:53:21 +0000109
110 local_flag = PASSED;
111
subrata_modak04f47a12009-09-18 17:44:08 +0000112 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew24e30ab2003-01-07 20:53:21 +0000113
subrata_modak04f47a12009-09-18 17:44:08 +0000114 runtest();
robbiew24e30ab2003-01-07 20:53:21 +0000115
subrata_modak04f47a12009-09-18 17:44:08 +0000116 if (local_flag == PASSED)
117 tst_resm(TPASS, "Test passed.");
118 else
119 tst_resm(TFAIL, "Test failed.");
120 }
121
robbiew24e30ab2003-01-07 20:53:21 +0000122 cleanup();
Garrett Cooperf01306b2010-11-05 11:14:08 -0700123 return 1;
robbiew24e30ab2003-01-07 20:53:21 +0000124}
robbiew24e30ab2003-01-07 20:53:21 +0000125
subrata_modak04f47a12009-09-18 17:44:08 +0000126static void setup(void)
robbiew24e30ab2003-01-07 20:53:21 +0000127{
robbiew24e30ab2003-01-07 20:53:21 +0000128 /*
129 * Make a directory to do this in; ignore error if already exists.
130 * Save starting directory.
131 */
132 tst_tmpdir();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 getcwd(homedir, sizeof(homedir));
robbiew24e30ab2003-01-07 20:53:21 +0000134 parent_pid = getpid();
135
136 if (!fuss[0])
137 sprintf(fuss, "./ftest05.%d", getpid());
138
139 mkdir(fuss, 0755);
140
141 if (chdir(fuss) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 tst_brkm(TBROK | TERRNO, NULL, "\tCan't chdir(%s)", fuss);
robbiew24e30ab2003-01-07 20:53:21 +0000143 }
144
robbiew24e30ab2003-01-07 20:53:21 +0000145 /*
146 * Default values for run conditions.
147 */
robbiew24e30ab2003-01-07 20:53:21 +0000148 iterations = 10;
149 nchild = 5;
150 csize = K_2; /* should run with 1, 2, and 4 K sizes */
151 max_size = K_1 * K_1;
152 misc_intvl = 10;
153
subrata_modak04f47a12009-09-18 17:44:08 +0000154 if (sigset(SIGTERM, term) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 tst_brkm(TBROK | TERRNO, NULL,
156 "sigset (signo = SIGTERM) failed");
robbiew24e30ab2003-01-07 20:53:21 +0000157 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000158
robbiew24e30ab2003-01-07 20:53:21 +0000159 local_flag = PASSED;
robbiew24e30ab2003-01-07 20:53:21 +0000160}
161
subrata_modak04f47a12009-09-18 17:44:08 +0000162static void runtest(void)
robbiew24e30ab2003-01-07 20:53:21 +0000163{
Garrett Cooperf01306b2010-11-05 11:14:08 -0700164 int child, count, i, nwait, pid, status;
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800165
166 nwait = 0;
robbiew24e30ab2003-01-07 20:53:21 +0000167
subrata_modak04f47a12009-09-18 17:44:08 +0000168 for (i = 0; i < nchild; i++) {
robbiew24e30ab2003-01-07 20:53:21 +0000169 test_name[0] = 'a' + i;
170 test_name[1] = '\0';
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 fd = open(test_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
subrata_modak04f47a12009-09-18 17:44:08 +0000172
robbiew24e30ab2003-01-07 20:53:21 +0000173 if (fd < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_brkm(TBROK | TERRNO, NULL,
175 "\tError creating %s/%s.", fuss, test_name);
robbiew24e30ab2003-01-07 20:53:21 +0000176 }
subrata_modak04f47a12009-09-18 17:44:08 +0000177
178 if ((child = fork()) == 0) {
179 dotest(nchild, i, fd);
180 tst_exit();
robbiew24e30ab2003-01-07 20:53:21 +0000181 }
subrata_modak04f47a12009-09-18 17:44:08 +0000182
robbiew24e30ab2003-01-07 20:53:21 +0000183 close(fd);
subrata_modak04f47a12009-09-18 17:44:08 +0000184
robbiew24e30ab2003-01-07 20:53:21 +0000185 if (child < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800186 tst_brkm(TBROK | TERRNO, NULL, "fork failed");
robbiew24e30ab2003-01-07 20:53:21 +0000187 } else {
188 pidlist[i] = child;
189 nwait++;
190 }
191 }
192
193 /*
194 * Wait for children to finish.
195 */
robbiew24e30ab2003-01-07 20:53:21 +0000196 count = 0;
subrata_modak04f47a12009-09-18 17:44:08 +0000197 while (1) {
198 if ((child = wait(&status)) >= 0) {
199 if (status != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200 tst_resm(TFAIL,
201 "\tTest{%d} failed, expected 0 exit.",
202 child);
subrata_modak04f47a12009-09-18 17:44:08 +0000203 local_flag = FAILED;
204 }
205 ++count;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800206 } else if (errno != EINTR)
207 break;
robbiew24e30ab2003-01-07 20:53:21 +0000208 }
209
210 /*
211 * Should have collected all children.
212 */
robbiew24e30ab2003-01-07 20:53:21 +0000213 if (count != nwait) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 tst_resm(TFAIL, "\tWrong # children waited on, count = %d",
215 count);
robbiew24e30ab2003-01-07 20:53:21 +0000216 local_flag = FAILED;
217 }
218
robbiew24e30ab2003-01-07 20:53:21 +0000219 chdir(homedir);
220 pid = fork();
subrata_modak04f47a12009-09-18 17:44:08 +0000221
robbiew24e30ab2003-01-07 20:53:21 +0000222 if (pid < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223 tst_brkm(TBROK | TERRNO, sync, "fork failed");
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800224 tst_exit();
robbiew24e30ab2003-01-07 20:53:21 +0000225 }
subrata_modak04f47a12009-09-18 17:44:08 +0000226
robbiew24e30ab2003-01-07 20:53:21 +0000227 if (pid == 0) {
robbiew7fdd5142005-02-07 19:39:54 +0000228 execl("/bin/rm", "rm", "-rf", fuss, NULL);
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800229 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000230 }
231
232 wait(&status);
subrata_modak04f47a12009-09-18 17:44:08 +0000233
robbiew24e30ab2003-01-07 20:53:21 +0000234 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 tst_resm(TINFO, "CAUTION - ftest05, '%s' may not be removed",
236 fuss);
robbiew24e30ab2003-01-07 20:53:21 +0000237 }
238
subrata_modak04f47a12009-09-18 17:44:08 +0000239 sync();
robbiew24e30ab2003-01-07 20:53:21 +0000240}
241
242/*
243 * dotest()
244 * Children execute this.
245 *
246 * Randomly read/mod/write chunks with known pattern and check.
247 * When fill sectors, iterate.
248 */
249
250#define NMISC 4
Wanlong Gao354ebb42012-12-07 10:10:04 +0800251enum m_type { m_fsync, m_trunc, m_sync, m_fstat };
252char *m_str[] = { "fsync", "trunc", "sync", "fstat" };
robbiew24e30ab2003-01-07 20:53:21 +0000253
Wanlong Gao354ebb42012-12-07 10:10:04 +0800254int misc_cnt[NMISC]; /* counts # of each kind of misc */
255int file_max; /* file-max size */
256int nchunks;
257int last_trunc = -1;
258int tr_flag;
259enum m_type type = m_fsync;
robbiew24e30ab2003-01-07 20:53:21 +0000260
261#define CHUNK(i) (((off64_t)i) * csize)
262#define NEXTMISC ((rand() % misc_intvl) + 5)
263
subrata_modak04f47a12009-09-18 17:44:08 +0000264static void dotest(int testers, int me, int fd)
robbiew24e30ab2003-01-07 20:53:21 +0000265{
subrata_modak04f47a12009-09-18 17:44:08 +0000266 int i, count, collide, chunk, whenmisc, xfr;
267 char *bits, *hold_bits, *buf, *val_buf, *zero_buf;
268 char val;
robbiew24e30ab2003-01-07 20:53:21 +0000269
270 nchunks = max_size / csize;
subrata_modak04f47a12009-09-18 17:44:08 +0000271
Wanlong Gao354ebb42012-12-07 10:10:04 +0800272 if ((bits = calloc((nchunks + 7) / 8, 1)) == NULL) {
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800273 perror("\tmalloc (bits)");
274 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000275 }
subrata_modak04f47a12009-09-18 17:44:08 +0000276
Wanlong Gao354ebb42012-12-07 10:10:04 +0800277 if ((hold_bits = calloc((nchunks + 7) / 8, 1)) == NULL) {
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800278 perror("\tmalloc (bold_bits)");
279 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000280 }
subrata_modak04f47a12009-09-18 17:44:08 +0000281
282 if ((buf = (calloc(csize, 1))) == NULL) {
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800283 perror("\tmalloc (buf)");
284 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000285 }
subrata_modak04f47a12009-09-18 17:44:08 +0000286
287 if ((val_buf = (calloc(csize, 1))) == NULL) {
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800288 perror("\tmalloc (val_buf)");
289 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000290 }
subrata_modak04f47a12009-09-18 17:44:08 +0000291
292 if ((zero_buf = (calloc(csize, 1))) == NULL) {
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800293 perror("\tmalloc (zero_buf)");
294 exit(1);
robbiew24e30ab2003-01-07 20:53:21 +0000295 }
296
297 /*
298 * No init sectors; allow file to be sparse.
299 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800300 val = (64 / testers) * me + 1;
robbiew24e30ab2003-01-07 20:53:21 +0000301
302 /*
303 * For each iteration:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800304 * zap bits array
305 * loop:
306 * pick random chunk, read it.
307 * if corresponding bit off {
308 * verify == 0. (sparse file)
309 * ++count;
310 * } else
311 * verify == val.
312 * write "val" on it.
313 * repeat until count = nchunks.
314 * ++val.
robbiew24e30ab2003-01-07 20:53:21 +0000315 */
316
317 srand(getpid());
Wanlong Gao354ebb42012-12-07 10:10:04 +0800318 if (misc_intvl)
319 whenmisc = NEXTMISC;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800320 while (iterations-- > 0) {
321 for (i = 0; i < NMISC; i++)
robbiew24e30ab2003-01-07 20:53:21 +0000322 misc_cnt[i] = 0;
323 ftruncate(fd, 0);
324 file_max = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800325 memset(bits, 0, (nchunks + 7) / 8);
326 memset(hold_bits, 0, (nchunks + 7) / 8);
subrata_modak04f47a12009-09-18 17:44:08 +0000327 memset(val_buf, val, csize);
328 memset(zero_buf, 0, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000329 count = 0;
330 collide = 0;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800331 while (count < nchunks) {
robbiew24e30ab2003-01-07 20:53:21 +0000332 chunk = rand() % nchunks;
333 /*
334 * Read it.
335 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800336 if (lseek64(fd, CHUNK(chunk), 0) < (off64_t) 0) {
337 tst_brkm(TFAIL | TERRNO, NULL,
338 "\tTest[%d]: lseek64(0) fail at %Lx",
339 me, CHUNK(chunk));
robbiew24e30ab2003-01-07 20:53:21 +0000340 }
341 if ((xfr = read(fd, buf, csize)) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800342 tst_brkm(TFAIL | TERRNO, NULL,
343 "\tTest[%d]: read fail at %Lx",
344 me, CHUNK(chunk));
robbiew24e30ab2003-01-07 20:53:21 +0000345 }
346 /*
347 * If chunk beyond EOF just write on it.
348 * Else if bit off, haven't seen it yet.
349 * Else, have. Verify values.
350 */
vapieraf64a872006-02-15 06:47:36 +0000351 //printf("%li %d", CHUNK(chunk), file_max );
robbiew24e30ab2003-01-07 20:53:21 +0000352 if (CHUNK(chunk) >= file_max) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800353 bits[chunk / 8] |= (1 << (chunk % 8));
robbiew24e30ab2003-01-07 20:53:21 +0000354 ++count;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800355 } else if ((bits[chunk / 8] & (1 << (chunk % 8))) == 0) {
robbiew24e30ab2003-01-07 20:53:21 +0000356 if (xfr != csize) {
vapieraf64a872006-02-15 06:47:36 +0000357 //tst_resm(TINFO, "\tTest[%d]: xfr=%d != %d, zero read.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800358 // me, xfr, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000359 tst_exit();
360 }
361 if (memcmp(buf, zero_buf, csize)) {
362 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800363 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%x, should be %d.",
364 me, CHUNK(chunk), val, count,
365 xfr, file_max, zero_buf[0]);
366 tst_resm(TINFO,
367 "\tTest[%d]: last_trunc = 0x%x.",
368 me, last_trunc);
robbiew24e30ab2003-01-07 20:53:21 +0000369 sync();
subrata_modak04f47a12009-09-18 17:44:08 +0000370 ft_dumpbuf(buf, csize);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800371 ft_dumpbits(bits, (nchunks + 7) / 8);
372 ft_orbits(hold_bits, bits,
373 (nchunks + 7) / 8);
subrata_modak04f47a12009-09-18 17:44:08 +0000374 tst_resm(TINFO, "\tHold ");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800375 ft_dumpbits(hold_bits,
376 (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000377 tst_exit();
378 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800379 bits[chunk / 8] |= (1 << (chunk % 8));
robbiew24e30ab2003-01-07 20:53:21 +0000380 ++count;
381 } else {
382 if (xfr != csize) {
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800383 tst_brkm(TFAIL, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800384 "\tTest[%d]: xfr=%d != %d, val read.",
385 me, xfr, csize);
robbiew24e30ab2003-01-07 20:53:21 +0000386 }
387 ++collide;
388 if (memcmp(buf, val_buf, csize)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800389 tst_resm(TFAIL,
390 "\tTest[%d] bad verify @ 0x%Lx for val %d count %d xfr %d file_max 0x%x.",
391 me, CHUNK(chunk), val, count,
392 xfr, file_max);
393 tst_resm(TINFO,
394 "\tTest[%d]: last_trunc = 0x%x.",
395 me, last_trunc);
robbiew24e30ab2003-01-07 20:53:21 +0000396 sync();
subrata_modak04f47a12009-09-18 17:44:08 +0000397 ft_dumpbuf(buf, csize);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800398 ft_dumpbits(bits, (nchunks + 7) / 8);
399 ft_orbits(hold_bits, bits,
400 (nchunks + 7) / 8);
subrata_modak04f47a12009-09-18 17:44:08 +0000401 tst_resm(TINFO, "\tHold ");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800402 ft_dumpbits(hold_bits,
403 (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000404 tst_exit();
405 }
406 }
407 /*
408 * Write it.
409 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800410 if (lseek64(fd, -((off64_t) xfr), 1) < (off64_t) 0) {
411 tst_brkm(TFAIL | TERRNO, NULL,
412 "\tTest[%d]: lseek64(1) fail at %Lx",
413 me, CHUNK(chunk));
robbiew24e30ab2003-01-07 20:53:21 +0000414 }
415 if ((xfr = write(fd, val_buf, csize)) < csize) {
416 if (errno == ENOSPC) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800417 tst_resm(TFAIL,
418 "\tTest[%d]: no space, exiting.",
419 me);
robbiew24e30ab2003-01-07 20:53:21 +0000420 fsync(fd);
Garrett Cooper7a73eab2010-11-09 22:45:42 -0800421 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800422 tst_resm(TFAIL | TERRNO,
423 "\tTest[%d]: write fail at %Lx xfr %d",
424 me, CHUNK(chunk), xfr);
robbiew24e30ab2003-01-07 20:53:21 +0000425 }
robbiew24e30ab2003-01-07 20:53:21 +0000426 tst_exit();
427 }
428 if (CHUNK(chunk) + csize > file_max)
429 file_max = CHUNK(chunk) + csize;
430 /*
431 * If hit "misc" interval, do it.
432 */
433 if (misc_intvl && --whenmisc <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800434 ft_orbits(hold_bits, bits, (nchunks + 7) / 8);
robbiew24e30ab2003-01-07 20:53:21 +0000435 domisc(me, fd, bits);
436 whenmisc = NEXTMISC;
437 }
438 if (count + collide > 2 * nchunks)
439 break;
440 }
441
442 /*
443 * End of iteration, maybe before doing all chunks.
444 */
445
446 fsync(fd);
subrata_modak04f47a12009-09-18 17:44:08 +0000447 ++misc_cnt[m_fsync];
vapieraf64a872006-02-15 06:47:36 +0000448 //tst_resm(TINFO, "\tTest{%d} val %d done, count = %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 < NMISC; i++)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800451 // tst_resm(TINFO, "\t\tTest{%d}: {%d} %s's.", me, misc_cnt[i], m_str[i]);
robbiew24e30ab2003-01-07 20:53:21 +0000452 ++val;
453 }
robbiew24e30ab2003-01-07 20:53:21 +0000454}
455
456/*
457 * domisc()
458 * Inject misc syscalls into the thing.
459 */
subrata_modak04f47a12009-09-18 17:44:08 +0000460static void domisc(int me, int fd, char *bits)
robbiew24e30ab2003-01-07 20:53:21 +0000461{
subrata_modak04f47a12009-09-18 17:44:08 +0000462 int chunk;
463 struct stat sb;
robbiew24e30ab2003-01-07 20:53:21 +0000464
subrata_modak04f47a12009-09-18 17:44:08 +0000465 if (type > m_fstat)
robbiew24e30ab2003-01-07 20:53:21 +0000466 type = m_fsync;
subrata_modak04f47a12009-09-18 17:44:08 +0000467
468 switch (type) {
robbiew24e30ab2003-01-07 20:53:21 +0000469 case m_fsync:
470 if (fsync(fd) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800471 tst_resm(TFAIL | TERRNO, "\tTest[%d]: fsync error", me);
robbiew24e30ab2003-01-07 20:53:21 +0000472 }
473 break;
474 case m_trunc:
475 chunk = rand() % (file_max / csize);
476 file_max = CHUNK(chunk);
477 last_trunc = file_max;
478 if (tr_flag) {
479 if (ftruncate(fd, file_max) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800480 tst_brkm(TFAIL | TERRNO, NULL,
481 "\tTest[%d]: ftruncate error @ 0x%x.",
482 me, file_max);
robbiew24e30ab2003-01-07 20:53:21 +0000483 }
484 tr_flag = 0;
485 } else {
486 if (truncate(test_name, file_max) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800487 tst_brkm(TFAIL | TERRNO, NULL,
488 "\tTest[%d]: truncate error @ 0x%x.",
489 me, file_max);
robbiew24e30ab2003-01-07 20:53:21 +0000490 }
491 tr_flag = 1;
492 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800493 for (; chunk % 8 != 0; chunk++)
494 bits[chunk / 8] &= ~(1 << (chunk % 8));
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800495 for (; chunk < nchunks; chunk += 8)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800496 bits[chunk / 8] = 0;
robbiew24e30ab2003-01-07 20:53:21 +0000497 break;
498 case m_sync:
499 sync();
500 break;
501 case m_fstat:
502 if (fstat(fd, &sb) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800503 tst_brkm(TFAIL | TERRNO, NULL,
504 "\tTest[%d]: fstat() error.", me);
robbiew24e30ab2003-01-07 20:53:21 +0000505 }
506 if (sb.st_size != file_max) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800507 tst_brkm(TFAIL, NULL,
508 "\tTest[%d]: fstat() mismatch; st_size=%"
509 PRIx64 ",file_max=%x.", me,
510 (int64_t) sb.st_size, file_max);
robbiew24e30ab2003-01-07 20:53:21 +0000511 }
512 break;
513 }
subrata_modak04f47a12009-09-18 17:44:08 +0000514 ++misc_cnt[type];
515 ++type;
robbiew24e30ab2003-01-07 20:53:21 +0000516}
517
518/* term()
519 *
520 * This is called when a SIGTERM signal arrives.
521 */
subrata_modak04f47a12009-09-18 17:44:08 +0000522static void term(int sig LTP_ATTRIBUTE_UNUSED)
robbiew24e30ab2003-01-07 20:53:21 +0000523{
subrata_modak04f47a12009-09-18 17:44:08 +0000524 int i;
robbiew24e30ab2003-01-07 20:53:21 +0000525
vapieraf64a872006-02-15 06:47:36 +0000526 tst_resm(TINFO, "\tterm -[%d]- got sig term.", getpid());
robbiew24e30ab2003-01-07 20:53:21 +0000527
528 /*
529 * If run by hand we like to have the parent send the signal to
530 * the child processes. This makes life easy.
531 */
robbiew24e30ab2003-01-07 20:53:21 +0000532 if (parent_pid == getpid()) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800533 for (i = 0; i < nchild; i++)
534 if (pidlist[i]) /* avoid embarassment */
robbiew24e30ab2003-01-07 20:53:21 +0000535 kill(pidlist[i], SIGTERM);
subrata_modak04f47a12009-09-18 17:44:08 +0000536 return;
robbiew24e30ab2003-01-07 20:53:21 +0000537 }
538
vapieraf64a872006-02-15 06:47:36 +0000539 tst_resm(TINFO, "\tunlinking '%s'", test_name);
robbiew24e30ab2003-01-07 20:53:21 +0000540
541 close(fd);
subrata_modak04f47a12009-09-18 17:44:08 +0000542
robbiew24e30ab2003-01-07 20:53:21 +0000543 if (unlink(test_name))
vapieraf64a872006-02-15 06:47:36 +0000544 tst_resm(TBROK, "Unlink of '%s' failed, errno = %d.",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800545 test_name, errno);
robbiew24e30ab2003-01-07 20:53:21 +0000546 else
vapieraf64a872006-02-15 06:47:36 +0000547 tst_resm(TINFO, "Unlink of '%s' successful.", test_name);
subrata_modak04f47a12009-09-18 17:44:08 +0000548
robbiew24e30ab2003-01-07 20:53:21 +0000549 tst_exit();
robbiew24e30ab2003-01-07 20:53:21 +0000550}
551
subrata_modak04f47a12009-09-18 17:44:08 +0000552static void cleanup(void)
robbiew24e30ab2003-01-07 20:53:21 +0000553{
robbiew24e30ab2003-01-07 20:53:21 +0000554
Wanlong Gao354ebb42012-12-07 10:10:04 +0800555 tst_rmdir();
556 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700557}