blob: bbad230cc410f1f0110c40cf7b63e00d2081486a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
subrata_modak4bb656a2009-02-26 12:02:09 +00003 *
plars865695b2001-08-27 22:15:12 +00004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
subrata_modak4bb656a2009-02-26 12:02:09 +00007 *
plars865695b2001-08-27 22:15:12 +00008 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
subrata_modak4bb656a2009-02-26 12:02:09 +000011 *
plars865695b2001-08-27 22:15:12 +000012 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
plars865695b2001-08-27 22:15:12 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
plars865695b2001-08-27 22:15:12 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
subrata_modak4bb656a2009-02-26 12:02:09 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
plars865695b2001-08-27 22:15:12 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32/*
33 * This program will grow a list of files.
34 * Each file will grow by grow_incr before the same
35 * file grows twice. Each file is open and closed before next file is opened.
36 *
37 * To just verify file contents: growfiles -g 0 -c 1 filename
38 *
39 * See help and prt_examples functions below.
40 *
41 * Basic code layout
subrata_modak4bb656a2009-02-26 12:02:09 +000042 * process cmdline
plars865695b2001-08-27 22:15:12 +000043 * print debug message about options used
44 * setup signal handlers
45 * return control to user (if wanted - default action)
46 * fork number of desired childern (if wanted)
47 * re-exec self (if wanted)
48 * Determine number of files
49 * malloc space or i/o buffer
50 * Loop until stop is set
51 * Determine if hit iteration, time, max errors or num bytes reached
52 * Loop through each file
53 * open file
54 * fstat file - to determine if file if a fifo
55 * prealloc file space (if wanted)
56 * growfile
57 * check last write
58 * check whole file
59 * shrink file
60 * close file
61 * delay (if wanted)
62 * End loop
63 * End loop
64 * remove all files (if wanted)
65 *
66 * Author: Richard Logan
67 *
68 */
69#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000070#include <errno.h>
plars865695b2001-08-27 22:15:12 +000071#include <stdlib.h>
72#include <ctype.h>
plars865695b2001-08-27 22:15:12 +000073#include <sys/types.h>
mridge24227452004-05-17 22:06:02 +000074#include <time.h>
mridge1f61f0d2004-05-18 17:08:11 +000075#include <sys/file.h>
mridge24227452004-05-17 22:06:02 +000076#include <unistd.h>
plars865695b2001-08-27 22:15:12 +000077#include <sys/stat.h>
78#include <sys/time.h>
79#include <sys/param.h>
80#include <sys/signal.h>
mridge1f61f0d2004-05-18 17:08:11 +000081#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000082#include <errno.h>
83#include <string.h>
subrata_modakf17ac842009-09-07 09:06:31 +000084#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000085#include "dataascii.h"
86#include "random_range.h"
87#include "databin.h"
Alex Elder97360e72011-04-01 17:01:26 -050088#include "open_flags.h"
89#include "forker.h"
90#include "file_lock.h"
plars865695b2001-08-27 22:15:12 +000091
92#ifdef CRAY
93#include <sys/panic.h>
94#include <sys/category.h>
95#endif
96
robbiew92b688b2004-03-01 22:36:38 +000097#include "test.h"
98
Wanlong Gao354ebb42012-12-07 10:10:04 +080099int set_sig(void);
100void sig_handler(int sig);
101static void notify_others(void);
102int handle_error(void);
103int cleanup(void);
104void usage(void);
105void help(void);
106void prt_examples(FILE * stream);
107int growfile(int fd, char *file, int grow_incr, char *buf,
108 unsigned long *curr_size_ptr);
109int shrinkfile(int fd, char *filename, int trunc_incr,
110 int trunc_inter, int just_trunc);
111int check_write(int fd, int cf_inter, char *filename, int mode);
112int check_file(int fd, int cf_inter, char *filename, int no_file_check);
113int file_size(int fd);
114int lkfile(int fd, int operation, int lklevel);
Alex Elder97360e72011-04-01 17:01:26 -0500115
116#ifndef linux
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117int pre_alloc(int fd, long size);
Alex Elder97360e72011-04-01 17:01:26 -0500118#endif /* !linux */
119
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120extern int datapidgen(int, char *, int, int);
121extern int datapidchk(int, char *, int, int, char **);
Alex Elder97360e72011-04-01 17:01:26 -0500122
robbiew92b688b2004-03-01 22:36:38 +0000123/* LTP status reporting */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124char *TCID = "growfiles"; /* Default test program identifier. */
125int TST_TOTAL = 1; /* Total number of test cases. */
robbiew92b688b2004-03-01 22:36:38 +0000126
127/* To avoid extensive modifications to the code, use this bodge */
128#define exit(x) myexit(x)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129void myexit(int x)
robbiew92b688b2004-03-01 22:36:38 +0000130{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 if (x)
132 tst_resm(TFAIL, "Test failed");
133 else
134 tst_resm(TPASS, "Test passed");
135 tst_exit();
robbiew92b688b2004-03-01 22:36:38 +0000136}
137
Wanlong Gao354ebb42012-12-07 10:10:04 +0800138#define NEWIO 1 /* Use the tlibio.c functions */
plars865695b2001-08-27 22:15:12 +0000139
140#ifndef NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141#define NEWIO 0 /* specifies to use original iowrite.c */
plars865695b2001-08-27 22:15:12 +0000142 /* functions instead of tlibio.c functions */
143 /* Once it is proven tlibio.c functions work properly, */
144 /* only tlibio.c functions will be used */
145#else
146#include "tlibio.h"
147#endif
148
149#ifndef PATH_MAX
150#define PATH_MAX 1023
151#endif
152
plars865695b2001-08-27 22:15:12 +0000153#define DEF_DIR "."
154#define DEF_FILE "gf"
155
156char *Progname;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157int Debug = 0;
plars865695b2001-08-27 22:15:12 +0000158
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159int Pid = 0;
plars865695b2001-08-27 22:15:12 +0000160
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161int io_type = 0; /* I/O type -sync */
robbiew85b1b032004-04-20 15:13:20 +0000162
163#ifdef O_LARGEFILE
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164int open_flags = O_RDWR | O_CREAT | O_LARGEFILE; /* open flags */
robbiew85b1b032004-04-20 15:13:20 +0000165#else
166#warning O_LARGEFILE is not defined!
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167int open_flags = O_RDWR | O_CREAT; /* open flags */
robbiew85b1b032004-04-20 15:13:20 +0000168#endif
plars865695b2001-08-27 22:15:12 +0000169
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170#define MAX_FC_READ 196608 /* 4096 * 48 - 48 blocks */
plars865695b2001-08-27 22:15:12 +0000171
172#define PATTERN_ASCII 1 /* repeating alphabet letter pattern */
173 /* allows multiple writers and to be checked */
174#define PATTERN_PID 2 /* <pid><words byte offset><pid> */
175 /* Assumes 64 bit word. Only allows single */
176 /* process to write and check */
177/*
178 * 1234567890123456789012345678901234567890123456789012345678901234
179 * ________________________________________________________________
180 * < pid >< offset in file of this word >< pid >
181 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000182
plars865695b2001-08-27 22:15:12 +0000183#define PATTERN_OFFSET 3 /* Like PATTERN_PID but has a fixed number */
184 /* (STATIC_NUM) instead of pid. */
185 /* Allows multiple processes to write/read */
186#define PATTERN_ALT 4 /* alternating bit pattern (i.e. 0x5555555...) */
187#define PATTERN_CHKER 5 /* checkerboard pattern (i.e. 0xff00ff00ff00...) */
188#define PATTERN_CNTING 6 /* counting pattern (i.e. 0 - 07, 0 - 07, ...) */
189#define PATTERN_ONES 7 /* all bits set (i.e. 0xffffffffffffff...) */
190#define PATTERN_ZEROS 8 /* all bits cleared (i.e. 0x000000000...) */
191#define PATTERN_RANDOM 9 /* random integers - can not be checked */
192#define STATIC_NUM 221849 /* used instead of pid when PATTERN_OFFSET */
193
194#define MODE_RAND_SIZE 1 /* random write and trunc */
195#define MODE_RAND_LSEEK 2 /* random lseek before write */
196#define MODE_GROW_BY_LSEEK 4 /* lseek beyond end of file then write a byte */
197#define RANDOM_OPEN 999876 /* if Open_flags set to this value, open flags */
198 /* will be randomly choosen from Open_flags[] */
199#define MODE_FIFO S_IFIFO /* defined in stat.h 0010000 */
200
201int num_files = 0; /* num_auto_files + cmd line files */
202char *filenames; /* pointer to space containing filenames */
203int remove_files = 0; /* if set, cleanup default is not to cleanup */
204int bytes_consumed = 0; /* total bytes consumed, all files */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205int bytes_to_consume = 0; /* non-zero if -B was specified, total bytes */
plars865695b2001-08-27 22:15:12 +0000206int Maxerrs = 100; /* Max number errors before forced exit */
207int Errors = 0; /* number of encountered errors */
208int Upanic_on_error = 0; /* call upanic if error and this variable set */
209
210/* The *_size variables are only used when random iosize option (-r) is used */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211int max_size = 5000;
212int min_size = 1; /* also set in option parsing */
213int mult_size = 1; /* when random iosz, iosz must be mult of mult_size */
plars865695b2001-08-27 22:15:12 +0000214/* the *_lseek variables are only used when radon lseek option (-R) is used */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800215int min_lseek = 0; /* also set in option parsing */
216int max_lseek = -1; /* -1 means size of file */
plars865695b2001-08-27 22:15:12 +0000217#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +0800218int Pattern = PATTERN_OFFSET; /* This pattern is 64 bit word based */
plars865695b2001-08-27 22:15:12 +0000219#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220int Pattern = PATTERN_ASCII;
plars865695b2001-08-27 22:15:12 +0000221#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222int Seed = -1; /* random number seed, < 0 == uninitialized */
223int Nseeds = 0; /* Number of seed specified by the user */
plars865695b2001-08-27 22:15:12 +0000224int *Seeds; /* malloc'ed arrary of ints holding user spec seeds */
225
Wanlong Gao354ebb42012-12-07 10:10:04 +0800226int using_random = 0; /* flag indicating randomization is being used */
227float delaysecs = 0.0; /* delay between iterations (in seconds) */
plars865695b2001-08-27 22:15:12 +0000228int delaytime; /* delay between iterations in clocks/uses */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800229int lockfile = 0; /* if set, do file locking */
plars865695b2001-08-27 22:15:12 +0000230 /* 1 = do file locking around write, trunc */
231 /* and reads. */
232 /* 2 = write lock around all file operations */
233
Wanlong Gao354ebb42012-12-07 10:10:04 +0800234off_t Woffset = 0; /* offset before last write */
235int Grow_incr = 4096; /* sz of last write */
236int Mode = 0; /* bitmask of write/trunc mode */
plars865695b2001-08-27 22:15:12 +0000237 /* also knows if dealing with fifo */
238char *Buffer = NULL; /* buffer used by write and write check */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800239int Alignment = 0; /* if non word multiple, io will not be word aligned */
240int Opid = 0; /* original pid */
plars865695b2001-08-27 22:15:12 +0000241
242int Sync_with_others = 0; /* Flag indicating to stop other if we stop before DONE */
243int Iter_cnt = 0; /* contains current iteration count value */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244char TagName[40]; /* name of this growfiles (see Monster) */
plars865695b2001-08-27 22:15:12 +0000245
246struct fileinfo_t {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800247 char *filename;
248 int fd;
249 int openflags;
250 int mode;
251} Fileinfo;
plars865695b2001-08-27 22:15:12 +0000252
253/*
254 * Define open flags that will be used when '-o random' option is used.
255 * Note: If there is more than one growfiles doing its thing to the same
256 * file, O_TRUNC will cause data mismatches. How you ask?
257 * timing of events, example:
258 * Process one Process two
259 * --------------- -------------
260 * get write lock
261 * fstat file
262 * lseek
263 * generate pattern
subrata_modak4bb656a2009-02-26 12:02:09 +0000264 * open with O_TRUNC
plars865695b2001-08-27 22:15:12 +0000265 * write with wrong pattern
266 * because offset is wrong
267 *
268 * The second process truncated the file after the pattern was
269 * determined, thus the pattern is wrong for the file location.
270 *
271 * There can also be a timing problem with open flag O_APPEND if
272 * file locks are not being used (-l option). Things could happen
273 * between the fstat and the write. Thus, writing the wrong pattern.
274 * If all processes observe the file locks, O_APPEND should be ok
275 * to use.
276 */
subrata_modak4bb656a2009-02-26 12:02:09 +0000277int Open_flags[] = {
plars865695b2001-08-27 22:15:12 +0000278#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +0800279 O_RDWR | O_CREAT,
280 O_RDWR | O_CREAT | O_RAW,
281 O_RDWR | O_CREAT | O_BIG,
282 O_RDWR | O_CREAT | O_APPEND,
283 O_RDWR | O_CREAT | O_NDELAY,
284 O_RDWR | O_CREAT | O_PLACE,
285 O_RDWR | O_CREAT | O_SYNC,
286 O_RDWR | O_CREAT | O_RAW | O_SYNC,
287 O_RDWR | O_CREAT | O_NDELAY | O_SYNC,
288 O_RDWR | O_CREAT | O_NDELAY | O_SYNC | O_BIG,
289 O_RDWR | O_CREAT | O_RAW,
290 O_RDWR | O_CREAT | O_RAW | O_APPEND,
291 O_RDWR | O_CREAT | O_RAW | O_BIG,
292 O_RDWR | O_CREAT | O_RAW | O_APPEND | O_BIG,
plars865695b2001-08-27 22:15:12 +0000293/***
294 * O_WELLFORMED makes -o random require well formed i/o
295 ***/
296#if ALLOW_O_WELLFORMED
297#if O_PARALLEL
Wanlong Gao354ebb42012-12-07 10:10:04 +0800298 O_RDWR | O_CREAT | O_PARALLEL | O_WELLFORMED | O_RAW,
299 O_RDWR | O_CREAT | O_PARALLEL | O_WELLFORMED | O_RAW | O_TRUNC,
plars865695b2001-08-27 22:15:12 +0000300#endif /* O_PARALLEL */
301#endif
302
303#else /* CRAY */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800304 O_RDWR | O_CREAT,
305 O_RDWR | O_CREAT | O_APPEND,
306 O_RDWR | O_CREAT | O_NDELAY,
307 O_RDWR | O_CREAT | O_SYNC,
308 O_RDWR | O_CREAT | O_SYNC | O_NDELAY,
309 O_RDWR | O_CREAT | O_APPEND | O_NDELAY,
plars865695b2001-08-27 22:15:12 +0000310
311#endif /* CRAY */
312};
313
314#define REXEC_INIT 0 /* don't do re-exec of childern */
315#define REXEC_DOIT 1 /* Do re-exec of childern */
316#define REXEC_DONE 2 /* We've already been re-exec'ed */
317
318#ifndef BSIZE
319#ifdef CRAY
320#define BSIZE 1024
321#else
322#define BSIZE 512
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323#endif /* CRAY */
324#endif /* BSIZE */
plars865695b2001-08-27 22:15:12 +0000325
Wanlong Gao354ebb42012-12-07 10:10:04 +0800326#define USECS_PER_SEC 1000000 /* microseconds per second */
plars865695b2001-08-27 22:15:12 +0000327
328/*
329 * Define marcos used when dealing with file locks.
330 */
331#define LKLVL0 1 /* file lock around write/read/trunc */
332#define LKLVL1 2 /* file lock after open to before close */
333
334/*
335 * Define special max lseek values
336 */
337#define LSK_EOF -1 /* set fptr up to EOF */
338#define LSK_EOFPLUSGROW -2 /* set fptr up to EOF + grow - leave whole */
339#define LSK_EOFMINUSGROW -3 /* set fptr up to EOF-grow - no grow */
340
plars865695b2001-08-27 22:15:12 +0000341/***********************************************************************
342 * MAIN
343 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800344int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +0000345{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800346 extern char *optarg; /* used by getopt */
347 extern int optind;
plars865695b2001-08-27 22:15:12 +0000348
Wanlong Gao354ebb42012-12-07 10:10:04 +0800349 int ind;
350 int first_file_ind = 0;
351 int num_auto_files = 0; /* files created by tool */
352 int seq_auto_files = 0; /* auto files created by tool created by tool */
353 char *auto_dir = DEF_DIR;
354 char *auto_file = DEF_FILE;
355 int grow_incr = 4096;
356 int trunc_incr = 4096;
357 int trunc_inter = 0; /* 0 means none, */
358 int unlink_inter = 0; /* 0 means none, 1 means always unlink */
359 int unlink_inter_ran = -1; /* -1 -use unlink_inter, otherwise randomly choose */
360 /* between unlink_inter and unlink_inter_ran */
361 int file_check_inter = 0; /* 0 means never, 1 means always */
362 int write_check_inter = 1; /* 0 means never, 1 means always */
363 int iterations = 1; /* number of increments to be added */
364 int no_file_check = 0; /* if set, no whole file checking will be done */
365 int num;
366 int fd; /* file descriptor */
367 int stop = 0; /* loop stopper if set */
mridgedec5a462005-02-25 15:49:37 +0000368
Wanlong Gao354ebb42012-12-07 10:10:04 +0800369 unsigned long curr_size = 0; /* BUG:14136 (keep track of file size) */
370 const unsigned long ext2_limit = 2147483647; /* BUG:14136 (2GB ext2 filesize limit) */
mridgedec5a462005-02-25 15:49:37 +0000371
Wanlong Gao354ebb42012-12-07 10:10:04 +0800372 int tmp;
373 char chr;
374 int ret;
375 int pre_alloc_space = 0;
plars865695b2001-08-27 22:15:12 +0000376#ifndef linux
Wanlong Gao354ebb42012-12-07 10:10:04 +0800377 long total_grow_value; /* used in pre-allocations */
plars865695b2001-08-27 22:15:12 +0000378#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800379 int backgrnd = 1; /* return control to user */
380 struct stat statbuf;
381 int time_iterval = -1;
382 time_t start_time = 0;
383 char reason[128]; /* reason for loop termination */
384 int num_procs = 1;
385 int forker_mode = 0;
386 int reexec = REXEC_INIT; /* reexec info */
387 char *exec_path = NULL;
plars865695b2001-08-27 22:15:12 +0000388
mridgef7298c72005-12-05 19:14:37 +0000389/*char *strrchr();*/
plars865695b2001-08-27 22:15:12 +0000390
Wanlong Gao354ebb42012-12-07 10:10:04 +0800391 char *filename; /* name of file specified by user */
392 char *cptr; /* temp char pointer */
393 extern int Forker_npids; /* num of forked pid, defined in forker.c */
394 struct timeval tv1;
plars865695b2001-08-27 22:15:12 +0000395
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800396 if (argv[0][0] == '-')
Wanlong Gao354ebb42012-12-07 10:10:04 +0800397 reexec = REXEC_DONE;
plars865695b2001-08-27 22:15:12 +0000398 /*
399 * Determine name of file used to invoke this program
400 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800401 if ((Progname = strrchr(argv[0], '/')) != NULL)
plars865695b2001-08-27 22:15:12 +0000402 Progname++;
403 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800404 Progname = argv[0];
plars865695b2001-08-27 22:15:12 +0000405
406 TagName[0] = '\0';
407
408 /*
409 * Process options
410 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800411 while ((ind = getopt(argc, argv,
412 "hB:C:c:bd:D:e:Ef:g:H:I:i:lL:n:N:O:o:pP:q:wt:r:R:s:S:T:uU:W:xy"))
413 != EOF) {
414 switch (ind) {
plars865695b2001-08-27 22:15:12 +0000415
Wanlong Gao354ebb42012-12-07 10:10:04 +0800416 case 'h':
plars865695b2001-08-27 22:15:12 +0000417 help();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800418 tst_exit();
plars865695b2001-08-27 22:15:12 +0000419
420 case 'B':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800421 switch (sscanf(optarg, "%i%c", &bytes_to_consume, &chr)) {
422 case 1: /* noop */
423 break;
plars865695b2001-08-27 22:15:12 +0000424
425 case 2:
426 if (chr == 'b') {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800427 bytes_to_consume *= BSIZE;
plars865695b2001-08-27 22:15:12 +0000428 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800429 fprintf(stderr,
430 "%s%s: --B option arg invalid\n",
431 Progname, TagName);
432 usage();
433 exit(1);
plars865695b2001-08-27 22:15:12 +0000434 }
435 break;
436
437 default:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800438 fprintf(stderr,
439 "%s%s: --B option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000440 Progname, TagName);
441 usage();
442 exit(1);
443 break;
444 }
445
446 break;
447
Wanlong Gao354ebb42012-12-07 10:10:04 +0800448 case 'E':
plars865695b2001-08-27 22:15:12 +0000449 prt_examples(stdout);
450 exit(0);
451
Wanlong Gao354ebb42012-12-07 10:10:04 +0800452 case 'b': /* batch */
453 backgrnd = 0;
plars865695b2001-08-27 22:15:12 +0000454 break;
455
456 case 'C':
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800457 if (sscanf(optarg, "%i", &write_check_inter) != 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800458 fprintf(stderr,
459 "%s%s: --c option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000460 Progname, TagName);
461 usage();
462 exit(1);
463 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800464 break;
plars865695b2001-08-27 22:15:12 +0000465
466 case 'c':
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800467 if (sscanf(optarg, "%i", &file_check_inter) != 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800468 fprintf(stderr,
469 "%s%s: --c option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000470 Progname, TagName);
471 usage();
472 exit(1);
473 }
474 break;
475
plars865695b2001-08-27 22:15:12 +0000476 case 'd':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800477 auto_dir = optarg;
plars865695b2001-08-27 22:15:12 +0000478#ifdef CRAY
479 unsetenv("TMPDIR"); /* force the use of auto_dir */
480#endif
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800481 if (stat(auto_dir, &statbuf) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800482 if (mkdir(auto_dir, 0777) == -1) {
483 if (errno != EEXIST) {
484 fprintf(stderr,
485 "%s%s: Unable to make dir %s\n",
486 Progname, TagName,
487 auto_dir);
488 exit(1);
489 }
490 }
491 } else {
492 if (!(statbuf.st_mode & S_IFDIR)) {
493 fprintf(stderr,
494 "%s%s: %s already exists and is not a directory\n",
495 Progname, TagName, auto_dir);
496 exit(1);
497 }
plars865695b2001-08-27 22:15:12 +0000498 }
499 break;
500
501 case 'D':
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800502 if (sscanf(optarg, "%i", &Debug) != 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800503 fprintf(stderr,
504 "%s%s: --D option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000505 Progname, TagName);
506 usage();
507 exit(1);
508 }
509 break;
510
511 case 'e':
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800512 if (sscanf(optarg, "%i", &Maxerrs) != 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800513 fprintf(stderr,
514 "%s%s: --e option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000515 Progname, TagName);
516 usage();
517 exit(1);
518 }
519 break;
520
521 case 'f':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800522 auto_file = optarg;
plars865695b2001-08-27 22:15:12 +0000523 break;
524
525 case 'g':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800526 if ((ret = sscanf(optarg, "%i%c", &grow_incr, &chr)) < 1
527 || grow_incr < 0) {
plars865695b2001-08-27 22:15:12 +0000528
Wanlong Gao354ebb42012-12-07 10:10:04 +0800529 fprintf(stderr,
530 "%s%s: --g option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000531 Progname, TagName);
532 usage();
533 exit(1);
534 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800535 if (ret == 2) {
536 if (chr == 'b' || chr == 'B')
plars865695b2001-08-27 22:15:12 +0000537 grow_incr *= 4096;
538 else {
539 fprintf(stderr,
540 "%s%s: --g option arg invalid\n",
541 Progname, TagName);
542 usage();
543 exit(1);
544 }
545 }
546 break;
547
548 case 'H':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800549 if (sscanf(optarg, "%f", &delaysecs) != 1
550 || delaysecs < 0) {
plars865695b2001-08-27 22:15:12 +0000551
Wanlong Gao354ebb42012-12-07 10:10:04 +0800552 fprintf(stderr,
553 "%s%s: --H option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000554 Progname, TagName);
555 usage();
556 exit(1);
557 }
558 break;
559
560 case 'i':
561 if (sscanf(optarg, "%i", &iterations) != 1 ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800562 iterations < 0) {
plars865695b2001-08-27 22:15:12 +0000563
Wanlong Gao354ebb42012-12-07 10:10:04 +0800564 fprintf(stderr,
565 "%s%s: --i option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000566 Progname, TagName);
567 usage();
568 exit(1);
569 }
570 break;
571
572 case 'I':
573#if NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +0800574 if ((io_type = lio_parse_io_arg1(optarg)) == -1) {
575 fprintf(stderr,
576 "%s%s: --I arg is invalid, must be s, p, f, a, l, L or r.\n",
577 Progname, TagName);
578 exit(1);
plars865695b2001-08-27 22:15:12 +0000579 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800580 if (io_type & LIO_RANDOM)
plars865695b2001-08-27 22:15:12 +0000581 using_random++;
582#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800583 if ((io_type = parse_io_arg(optarg)) == -1) {
584 fprintf(stderr,
585 "%s%s: --I arg is invalid, must be s, p, f, a, l, L or r.\n",
586 Progname, TagName);
587 exit(1);
plars865695b2001-08-27 22:15:12 +0000588 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800589 if (io_type == 99) /* hold-over until tlibio.h */
plars865695b2001-08-27 22:15:12 +0000590 using_random++;
591#endif
592 break;
593
594 case 'l':
595 lockfile++;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800596 if (lockfile > 2)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800597 lockfile = 2; /* lockfile can only be 1 or 2 */
plars865695b2001-08-27 22:15:12 +0000598 break;
599
600 case 'L':
601 if (sscanf(optarg, "%i", &time_iterval) != 1 ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800602 time_iterval < 0) {
603 fprintf(stderr,
604 "%s%s: --L option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000605 Progname, TagName);
606 usage();
607 exit(1);
608 }
609 break;
610
611 case 'n':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800612 if (sscanf(optarg, "%i:%i", &num_procs, &forker_mode) <
613 1 || num_procs < 0) {
plars865695b2001-08-27 22:15:12 +0000614
Wanlong Gao354ebb42012-12-07 10:10:04 +0800615 fprintf(stderr,
616 "%s%s: --n option arg invalid\n",
617 Progname, TagName);
618 usage();
619 exit(1);
620 }
plars865695b2001-08-27 22:15:12 +0000621
622 break;
623
624 case 'N':
625 if (sscanf(optarg, "%i", &num_auto_files) != 1 ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800626 num_auto_files < 0) {
plars865695b2001-08-27 22:15:12 +0000627
Wanlong Gao354ebb42012-12-07 10:10:04 +0800628 fprintf(stderr,
629 "%s%s: --N option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000630 Progname, TagName);
631 usage();
632 exit(1);
633 }
634 break;
635
636 case 'O':
637 if (sscanf(optarg, "%i", &Alignment) != 1 ||
Jan Stancek5f070bc2013-04-19 14:00:02 +0200638 Alignment < 0) {
plars865695b2001-08-27 22:15:12 +0000639
Wanlong Gao354ebb42012-12-07 10:10:04 +0800640 fprintf(stderr,
641 "%s%s: --O option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000642 Progname, TagName);
643 usage();
644 exit(1);
645 }
646 break;
647
648 case 'o':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800649 if (strcmp(optarg, "random") == 0) {
650 open_flags = RANDOM_OPEN;
651 using_random++;
plars865695b2001-08-27 22:15:12 +0000652
Wanlong Gao354ebb42012-12-07 10:10:04 +0800653 } else if ((open_flags = parse_open_flags(optarg, NULL))
654 == -1) {
655 fprintf(stderr,
656 "%s%s: --o arg contains invalid flag\n",
657 Progname, TagName);
658 exit(1);
659 }
660 break;
plars865695b2001-08-27 22:15:12 +0000661
Wanlong Gao354ebb42012-12-07 10:10:04 +0800662 case 'p': /* pre allocate space */
plars865695b2001-08-27 22:15:12 +0000663#ifdef linux
664 printf("%s%s: --p is illegal option on linux system\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800665 Progname, TagName);
plars865695b2001-08-27 22:15:12 +0000666 exit(1);
667#else
668 pre_alloc_space++;
669#endif
670 break;
671
672 case 'P':
673#ifdef CRAY
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800674 if (strcmp(optarg, "PANIC") != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800675 fprintf(stderr, "%s%s: --P arg must be PANIC\n",
676 Progname, TagName);
plars865695b2001-08-27 22:15:12 +0000677 exit(1);
678 }
679 Upanic_on_error++;
Wei,Jiangangd3b775b2015-04-24 09:46:38 +0800680 printf("%s%s: Will call upanic after writes\n", Progname, TagName);
plars865695b2001-08-27 22:15:12 +0000681#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800682 printf
683 ("%s%s: --P is illegal option on non-cray system\n",
684 Progname, TagName);
plars865695b2001-08-27 22:15:12 +0000685 exit(1);
686#endif
687 break;
688
689 case 'q': /* file content or pattern */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800690 switch (optarg[0]) {
plars865695b2001-08-27 22:15:12 +0000691 case 'A':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800692 Pattern = PATTERN_ALT;
693 break;
plars865695b2001-08-27 22:15:12 +0000694 case 'a':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800695 Pattern = PATTERN_ASCII;
696 break;
plars865695b2001-08-27 22:15:12 +0000697 case 'p':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800698 Pattern = PATTERN_PID;
699 break;
plars865695b2001-08-27 22:15:12 +0000700 case 'o':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800701 Pattern = PATTERN_OFFSET;
702 break;
plars865695b2001-08-27 22:15:12 +0000703 case 'c':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800704 Pattern = PATTERN_CHKER;
705 break;
plars865695b2001-08-27 22:15:12 +0000706 case 'C':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800707 Pattern = PATTERN_CNTING;
708 break;
plars865695b2001-08-27 22:15:12 +0000709 case 'r':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800710 Pattern = PATTERN_RANDOM;
711 using_random++;
712 break;
plars865695b2001-08-27 22:15:12 +0000713 case 'z':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800714 Pattern = PATTERN_ZEROS;
715 break;
plars865695b2001-08-27 22:15:12 +0000716 case 'O':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800717 Pattern = PATTERN_ONES;
718 break;
plars865695b2001-08-27 22:15:12 +0000719 default:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800720 fprintf(stderr,
721 "%s%s: --C option arg invalid, A, a, p, o, c, C, r, z, or 0\n",
722 Progname, TagName);
723 usage();
724 exit(1);
plars865695b2001-08-27 22:15:12 +0000725 }
726 break;
727
Wanlong Gao354ebb42012-12-07 10:10:04 +0800728 case 'R': /* random lseek before write arg: [min-]max */
729 if (sscanf(optarg, "%i-%i", &min_lseek, &max_lseek) !=
730 2) {
731 min_lseek = 1; /* same as default in define */
732 if (sscanf(optarg, "%i%c", &max_lseek, &chr) !=
733 1) {
734 fprintf(stderr,
735 "%s%s: --R option arg invalid: [min-]max\n",
736 Progname, TagName);
737 exit(1);
738 }
plars865695b2001-08-27 22:15:12 +0000739 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800740 if (max_lseek < LSK_EOFMINUSGROW) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800741 fprintf(stderr,
742 "%s%s: --R option, max_lseek is invalid\n",
743 Progname, TagName);
744 exit(1);
plars865695b2001-08-27 22:15:12 +0000745 }
746 Mode |= MODE_RAND_LSEEK;
747 using_random++;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800748 break;
plars865695b2001-08-27 22:15:12 +0000749
750 case 'r': /* random io size arg: [min-]max[:mult] */
751
752 /* min-max:mult format */
753 if (sscanf(optarg, "%i-%i:%i%c", &min_size, &max_size,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800754 &mult_size, &chr) != 3) {
755 min_size = 1;
756 /* max:mult format */
757 if (sscanf(optarg, "%i:%i%c", &max_size,
758 &mult_size, &chr) != 2) {
759 /* min-max format */
760 if (sscanf(optarg, "%i-%i%c", &min_size,
761 &max_size, &chr) != 2) {
762 min_size = 1;
763 if (sscanf
764 (optarg, "%i%c", &max_size,
765 &chr) != 1) {
766 fprintf(stderr,
767 "%s%s: --r option arg invalid: [min-]max[:mult]\n",
768 Progname,
769 TagName);
770 exit(1);
771 }
772 }
773 }
plars865695b2001-08-27 22:15:12 +0000774 }
775
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800776 if (max_size < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800777 fprintf(stderr,
778 "%s%s: --r option, max_size is invalid\n",
779 Progname, TagName);
780 exit(1);
plars865695b2001-08-27 22:15:12 +0000781 }
782 /*
783 * If min and max are the same, no randomness
784 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800785 if (min_size != max_size) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800786 Mode |= MODE_RAND_SIZE;
787 using_random++;
788 }
789 break;
plars865695b2001-08-27 22:15:12 +0000790
791 case 'S':
792 if (sscanf(optarg, "%i", &seq_auto_files) != 1 ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800793 seq_auto_files < 0) {
plars865695b2001-08-27 22:15:12 +0000794
Wanlong Gao354ebb42012-12-07 10:10:04 +0800795 fprintf(stderr,
796 "%s%s: --S option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000797 Progname, TagName);
798 usage();
799 exit(1);
800 }
801 break;
802
803 case 's': /* format: seed[,seed...] */
Garrett Cooper2c282152010-12-16 00:55:50 -0800804
plars865695b2001-08-27 22:15:12 +0000805 /* count the number of seeds */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800806 cptr = optarg;
807 for (Nseeds = 1; *cptr; Nseeds++) {
808 if ((filename = strchr(cptr, ',')) == NULL)
809 break;
810 cptr = filename;
811 cptr++;
plars865695b2001-08-27 22:15:12 +0000812 }
Cyril Hrubisd218f342014-09-23 13:14:56 +0200813 Seeds = malloc(Nseeds * sizeof(int));
plars865695b2001-08-27 22:15:12 +0000814
815 /*
subrata_modak4bb656a2009-02-26 12:02:09 +0000816 * check that each seed is valid and put them in
plars865695b2001-08-27 22:15:12 +0000817 * the newly malloc'ed Seeds arrary.
818 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800819 filename = cptr = optarg;
820 for (Nseeds = 0; *cptr; Nseeds++) {
821 if ((filename = strchr(cptr, ',')) == NULL) {
822 if (sscanf(cptr, "%i", &Seeds[Nseeds]) <
823 1) {
824 fprintf(stderr,
825 "%s%s: --s option arg %s invalid\n",
826 Progname, TagName,
827 cptr);
828 usage();
829 exit(1);
830 }
831 Nseeds++;
832 break;
plars865695b2001-08-27 22:15:12 +0000833 }
plars865695b2001-08-27 22:15:12 +0000834
Wanlong Gao354ebb42012-12-07 10:10:04 +0800835 *filename = '\0';
836 if (sscanf(cptr, "%i", &Seeds[Nseeds]) < 1) {
837 fprintf(stderr,
838 "%s%s: --s option arg %s invalid\n",
839 Progname, TagName, cptr);
840 usage();
841 exit(1);
842 }
843 *filename = ','; /* restore string */
844 cptr = filename;
845 cptr++;
plars865695b2001-08-27 22:15:12 +0000846 }
847 break;
848
849 case 't':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800850 if ((ret =
851 sscanf(optarg, "%i%c", &trunc_incr, &chr)) < 1
852 || trunc_incr < 0) {
plars865695b2001-08-27 22:15:12 +0000853
Wanlong Gao354ebb42012-12-07 10:10:04 +0800854 fprintf(stderr,
855 "%s%s: --t option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000856 Progname, TagName);
857 usage();
858 exit(1);
859 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800860 if (ret == 2) {
861 if (chr == 'b' || chr == 'B')
plars865695b2001-08-27 22:15:12 +0000862 trunc_incr *= 4096;
863 else {
864 fprintf(stderr,
865 "%s%s: --t option arg invalid\n",
866 Progname, TagName);
867 usage();
868 exit(1);
869 }
870 }
871 break;
872
873 case 'T': /* truncate interval */
874 if (sscanf(optarg, "%i%c", &trunc_inter, &chr) != 1 ||
Wanlong Gao354ebb42012-12-07 10:10:04 +0800875 trunc_inter < 0) {
plars865695b2001-08-27 22:15:12 +0000876
Wanlong Gao354ebb42012-12-07 10:10:04 +0800877 fprintf(stderr,
878 "%s%s: --T option arg invalid\n",
plars865695b2001-08-27 22:15:12 +0000879 Progname, TagName);
880 usage();
881 exit(1);
882 }
883 break;
884
885 case 'u':
886 remove_files++;
887 break;
888
Wanlong Gao354ebb42012-12-07 10:10:04 +0800889 case 'U': /* how often to unlink file */
890 /*
891 * formats:
892 * A-B - randomly pick interval between A and B
893 * X - unlink file every X iteration
894 */
895 if (sscanf(optarg, "%i-%i", &unlink_inter,
896 &unlink_inter_ran) == 2) {
plars865695b2001-08-27 22:15:12 +0000897
Wanlong Gao354ebb42012-12-07 10:10:04 +0800898 if (unlink_inter < 0 || unlink_inter_ran < 0) {
899 fprintf(stderr,
900 "%s%s: --U option arg invalid\n",
901 Progname, TagName);
902 usage();
903 exit(1);
904 }
905 /* ensure unlink_inter contains smaller value */
906 if (unlink_inter > unlink_inter_ran) {
907 tmp = unlink_inter_ran;
908 unlink_inter_ran = unlink_inter;
909 unlink_inter = tmp;
910 }
911 using_random++;
plars865695b2001-08-27 22:15:12 +0000912
Wanlong Gao354ebb42012-12-07 10:10:04 +0800913 } else if (sscanf(optarg, "%i%c", &unlink_inter, &chr)
914 != 1 || unlink_inter < 0) {
plars865695b2001-08-27 22:15:12 +0000915
Wanlong Gao354ebb42012-12-07 10:10:04 +0800916 fprintf(stderr,
917 "%s%s: --U option arg invalid\n",
918 Progname, TagName);
919 usage();
920 exit(1);
921 }
plars865695b2001-08-27 22:15:12 +0000922 break;
923
Wanlong Gao354ebb42012-12-07 10:10:04 +0800924 case 'x':
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800925 if (reexec != REXEC_DONE)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800926 reexec = REXEC_DOIT;
plars865695b2001-08-27 22:15:12 +0000927 break;
928
929 case 'w':
930 Mode |= MODE_GROW_BY_LSEEK;
931 break;
932
933 case 'W':
robbiew92b688b2004-03-01 22:36:38 +0000934 TCID = optarg;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800935 sprintf(TagName, "(%.39s)", optarg);
plars865695b2001-08-27 22:15:12 +0000936 break;
937
938 case 'y':
Wanlong Gao354ebb42012-12-07 10:10:04 +0800939 Sync_with_others = 1;
plars865695b2001-08-27 22:15:12 +0000940 break;
941
942 case '?':
943 usage();
944 exit(1);
945 break;
946 }
947 }
948
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800949 if (Debug == 1) {
plars865695b2001-08-27 22:15:12 +0000950 cptr = getenv("TOUTPUT");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800951 if ((cptr != NULL) && (strcmp(cptr, "NOPASS") == 0)) {
plars865695b2001-08-27 22:15:12 +0000952 Debug = 0;
953 }
954 }
955
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800956 if (Pattern == PATTERN_RANDOM) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800957 no_file_check = 1;
958 if (write_check_inter || file_check_inter)
959 printf
960 ("%s%s: %d Using random pattern - no data checking will be performed!\n",
961 Progname, TagName, getpid());
962 } else if (max_lseek == LSK_EOFPLUSGROW || Mode & MODE_GROW_BY_LSEEK) {
963 no_file_check = 1;
plars865695b2001-08-27 22:15:12 +0000964
Wanlong Gao354ebb42012-12-07 10:10:04 +0800965 if (file_check_inter)
966 printf("%s%s: %d Using random lseek beyond EOF or lseek grow,\n\
967no whole file checking will be performed!\n", Progname, TagName,
968 getpid());
plars865695b2001-08-27 22:15:12 +0000969
970 }
971
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800972 if (Mode & MODE_RAND_SIZE)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800973 grow_incr = max_size;
plars865695b2001-08-27 22:15:12 +0000974
975 set_sig();
976
Wanlong Gao354ebb42012-12-07 10:10:04 +0800977 Opid = getpid();
978 Pid = Opid;
plars865695b2001-08-27 22:15:12 +0000979
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800980 if (backgrnd) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800981 if (Debug > 1)
982 printf
983 ("%s: %d DEBUG2 forking, returning control to the user\n",
984 Progname, Opid);
985 background(Progname); /* give user their prompt back */
plars865695b2001-08-27 22:15:12 +0000986 }
plars865695b2001-08-27 22:15:12 +0000987#if CRAY
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800988 if (Sync_with_others)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800989 setpgrp();
plars865695b2001-08-27 22:15:12 +0000990#endif
991
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800992 if (Debug > 3) {
plars865695b2001-08-27 22:15:12 +0000993#if NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +0800994 lio_set_debug(Debug - 3);
plars865695b2001-08-27 22:15:12 +0000995#else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800996 set_iowrite_debug(Debug - 3);
plars865695b2001-08-27 22:15:12 +0000997#endif
998 }
999
1000 /*
1001 * Print some program information here if debug is turned on to
1002 * level 3 or higher.
1003 */
1004
Wanlong Gao354ebb42012-12-07 10:10:04 +08001005 if (Debug > 2) {
Garrett Cooper2c282152010-12-16 00:55:50 -08001006
Wanlong Gao354ebb42012-12-07 10:10:04 +08001007 if (Mode & MODE_GROW_BY_LSEEK)
1008 printf
1009 ("%s: %d DEBUG lseeking past end of file, writting a \"w\"\n",
1010 Progname, Pid);
1011 else if (Pattern == PATTERN_OFFSET)
1012 printf
1013 ("%s: %d DEBUG3 %d<byteoffset>%d per word pattern multi-writers.\n",
1014 Progname, Pid, STATIC_NUM, STATIC_NUM);
1015 else if (Pattern == PATTERN_PID)
1016 printf
1017 ("%s: %d DEBUG3 <pid><byteoffset><pid> per word pattern - 1 writer\n",
1018 Progname, Pid);
1019 else if (Pattern == PATTERN_ASCII)
1020 printf
1021 ("%s: %d DEBUG3 ascii pattern (vi'able)- allows multiple writers\n",
1022 Progname, Pid);
1023 else if (Pattern == PATTERN_ALT)
1024 printf
1025 ("%s: %d DEBUG3 alt bit pattern - allows multiple writers\n",
1026 Progname, Pid);
1027 else if (Pattern == PATTERN_CHKER)
1028 printf
1029 ("%s: %d DEBUG3 checkerboard pattern - allows multiple writers\n",
1030 Progname, Pid);
1031 else if (Pattern == PATTERN_CNTING)
1032 printf
1033 ("%s: %d DEBUG3 counting pattern - allows multiple writers\n",
1034 Progname, Pid);
1035 else if (Pattern == PATTERN_RANDOM)
1036 printf
1037 ("%s: %d DEBUG3 random integer pattern - no write/file checking\n",
1038 Progname, Pid);
1039 else if (Pattern == PATTERN_ONES)
1040 printf
1041 ("%s: %d DEBUG3 all ones pattern - allows multiple writers\n",
1042 Progname, Pid);
1043 else if (Pattern == PATTERN_ZEROS)
1044 printf
1045 ("%s: %d DEBUG3 all zeros pattern - allows multiple writers\n",
1046 Progname, Pid);
subrata_modakbdbaec52009-02-26 12:14:51 +00001047
plars865695b2001-08-27 22:15:12 +00001048 else
Wanlong Gao354ebb42012-12-07 10:10:04 +08001049 printf("%s: %d DEBUG3 unknown pattern\n",
1050 Progname, Pid);
1051 if (bytes_to_consume)
1052 printf("%s: %d DEBUG3 bytes_to_consume = %d\n",
1053 Progname, Pid, bytes_to_consume);
1054 printf
1055 ("%s: %d DEBUG3 Maxerrs = %d, pre_alloc_space = %d, filelocking = %d\n",
1056 Progname, Pid, Maxerrs, pre_alloc_space, lockfile);
plars865695b2001-08-27 22:15:12 +00001057
Wanlong Gao354ebb42012-12-07 10:10:04 +08001058 printf
1059 ("%s: %d DEBUG3 Debug = %d, remove files in cleanup : %d\n",
1060 Progname, Pid, Debug, remove_files);
plars865695b2001-08-27 22:15:12 +00001061
Wanlong Gao354ebb42012-12-07 10:10:04 +08001062 printf("%s: %d DEBUG3 Mode = %#o\n", Progname, Pid, Mode);
plars865695b2001-08-27 22:15:12 +00001063
Wanlong Gao354ebb42012-12-07 10:10:04 +08001064 if (open_flags == RANDOM_OPEN)
1065 printf
1066 ("%s: %d DEBUG3 open_flags = (random), io_type = %#o\n",
1067 Progname, Pid, io_type);
1068 else
1069 printf
1070 ("%s: %d DEBUG3 open_flags = %#o, io_type = %#o\n",
1071 Progname, Pid, open_flags, io_type);
Garrett Cooper2c282152010-12-16 00:55:50 -08001072
Wanlong Gao354ebb42012-12-07 10:10:04 +08001073 if (Mode & MODE_RAND_SIZE) {
1074 printf
1075 ("%s: %d DEBUG3 random write/trunc: min=%d, max=%d, mult = %d\n",
1076 Progname, Pid, min_size, max_size, mult_size);
1077 } else {
1078 printf("%s: %d DEBUG3 grow_incr = %d\n",
1079 Progname, Pid, grow_incr);
1080 }
1081 if (Mode & MODE_RAND_LSEEK) {
1082 if (max_lseek == LSK_EOF)
1083 printf
1084 ("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile>\n",
1085 Progname, Pid, min_lseek);
1086 else if (max_lseek == LSK_EOFPLUSGROW)
1087 printf
1088 ("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile+iosize>\n",
1089 Progname, Pid, min_lseek);
1090 else if (max_lseek == LSK_EOFMINUSGROW)
1091 printf
1092 ("%s: %d DEBUG3 random lseek: min=%d, max=<endoffile-iosize>\n",
1093 Progname, Pid, min_lseek);
1094 else
1095 printf
1096 ("%s: %d DEBUG3 random lseek: min=%d, max=%d\n",
1097 Progname, Pid, min_lseek, max_lseek);
1098 }
plars865695b2001-08-27 22:15:12 +00001099
Wanlong Gao354ebb42012-12-07 10:10:04 +08001100 printf
1101 ("%s: %d DEBUG3 check write interval = %d, check file interval = %d\n",
1102 Progname, Pid, write_check_inter, file_check_inter);
plars865695b2001-08-27 22:15:12 +00001103
Wanlong Gao354ebb42012-12-07 10:10:04 +08001104 printf("%s: %d DEBUG3 trunc interval = %d, trunc_incr = %d\n",
1105 Progname, Pid, trunc_inter, trunc_incr);
1106
1107 if (no_file_check)
1108 printf
1109 ("%s: %d DEBUG3 no whole file checking will be done\n",
1110 Progname, Pid);
1111
1112 if (unlink_inter_ran == -1) {
1113 printf("%s: %d DEBUG3 unlink_inter = %d\n",
1114 Progname, Pid, unlink_inter);
1115 } else {
1116 printf
1117 ("%s: %d DEBUG3 unlink_inter = %d, unlink_inter_ran = %d\n",
1118 Progname, Pid, unlink_inter, unlink_inter_ran);
1119 }
1120
1121 if (Debug > 8) {
1122 num = sizeof(Open_flags) / sizeof(int);
1123 printf("%s: %d DEBUG9 random open flags values:\n",
1124 Progname, Pid);
1125 for (ind = 0; ind < num; ind++) {
1126 printf("\t%#o\n", Open_flags[ind]);
1127 }
1128 }
1129 }
1130 /* end of DEBUG > 2 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001131 if (Debug > 1 && num_procs > 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001132 printf("%s: %d DEBUG2 about to fork %d more copies\n", Progname,
1133 Opid, num_procs - 1);
plars865695b2001-08-27 22:15:12 +00001134 }
1135
Wanlong Gao354ebb42012-12-07 10:10:04 +08001136 fflush(stdout); /* ensure pending i/o is flushed before forking */
plars865695b2001-08-27 22:15:12 +00001137 fflush(stderr);
1138
1139 forker(num_procs, forker_mode, Progname);
1140
Wanlong Gao354ebb42012-12-07 10:10:04 +08001141 Pid = getpid(); /* reset after the forks */
plars865695b2001-08-27 22:15:12 +00001142 /*
1143 * If user specified random seed(s), get that random seed value.
1144 * get random seed if it was not specified by the user.
1145 * This is done after the forks, because pid is used to get the seed.
Wanlong Gao354ebb42012-12-07 10:10:04 +08001146 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001147 if (Nseeds == 1) {
plars865695b2001-08-27 22:15:12 +00001148 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +08001149 * If only one seed specified, all processes will get that seed.
plars865695b2001-08-27 22:15:12 +00001150 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001151 Seed = Seeds[0];
1152 } else if (Nseeds > 1) {
1153 /*
1154 * More than one seed was specified.
1155 * The original process gets the first seed. Each
1156 * process will be get the next seed in the specified list.
1157 */
1158 if (Opid == Pid) {
1159 Seed = Seeds[0];
1160 } else {
1161 /*
1162 * If user didn't specify enough seeds, use default method.
1163 */
1164 if (Forker_npids >= Nseeds) {
1165 struct timeval ts;
1166 gettimeofday(&ts, NULL);
1167 Seed = ts.tv_sec + Pid; /* default random seed */
1168 } else {
1169 Seed = Seeds[Forker_npids];
1170 }
subrata_modak90d1e3c2008-08-21 13:07:02 +00001171 }
plars865695b2001-08-27 22:15:12 +00001172 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001173 /*
1174 * Generate a random seed based on time and pid.
1175 * It has a good chance of being unique for each pid.
1176 */
subrata_modak90d1e3c2008-08-21 13:07:02 +00001177 struct timeval ts;
Wanlong Gao354ebb42012-12-07 10:10:04 +08001178 gettimeofday(&ts, NULL);
1179 Seed = ts.tv_sec + Pid; /* default random seed */
1180 //Seed=time(0) + Pid; /* default random seed */
Garrett Cooper2c282152010-12-16 00:55:50 -08001181
plars865695b2001-08-27 22:15:12 +00001182 }
1183
1184 random_range_seed(Seed);
1185
Wanlong Gao354ebb42012-12-07 10:10:04 +08001186 if (using_random && Debug > 0)
1187 printf("%s%s: %d DEBUG1 Using random seed of %d\n",
1188 Progname, TagName, Pid, Seed);
plars865695b2001-08-27 22:15:12 +00001189
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001190 if (unlink_inter_ran > 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001191 /*
1192 * Find unlinking file interval. This must be done after
1193 * the seed was set. This allows multiple copies to
1194 * get different intervals.
1195 */
1196 tmp = unlink_inter;
1197 unlink_inter =
1198 (int)random_range(tmp, unlink_inter_ran, 1, NULL);
plars865695b2001-08-27 22:15:12 +00001199
Wanlong Gao354ebb42012-12-07 10:10:04 +08001200 if (Debug > 2)
1201 printf
1202 ("%s: %d DEBUG3 Unlink interval is %d (random %d - %d)\n",
1203 Progname, Pid, unlink_inter, tmp,
1204 unlink_inter_ran);
plars865695b2001-08-27 22:15:12 +00001205 }
1206
1207 /*
1208 * re-exec all childern if reexec is set to REXEC_DOIT.
1209 * This is useful on MPP systems to get the
1210 * child process on another PE.
1211 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001212 if (reexec == REXEC_DOIT && Opid != Pid) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001213 if (exec_path == NULL) {
1214 exec_path = argv[0];
1215 /* Get space for cmd (2 extra, 1 for - and 1 fro NULL */
Cyril Hrubisd218f342014-09-23 13:14:56 +02001216 argv[0] = malloc(strlen(exec_path) + 2);
Wanlong Gao354ebb42012-12-07 10:10:04 +08001217 sprintf(argv[0], "-%s", exec_path);
1218 }
Garrett Cooper2c282152010-12-16 00:55:50 -08001219
Wanlong Gao354ebb42012-12-07 10:10:04 +08001220 if (Debug > 2)
1221 printf("%s: %d DEBUG3 %s/%d: execvp(%s, argv)\n",
1222 Progname, Pid, __FILE__, __LINE__, argv[0]);
plars865695b2001-08-27 22:15:12 +00001223
Wanlong Gao354ebb42012-12-07 10:10:04 +08001224 execvp(argv[0], argv);
1225 }
plars865695b2001-08-27 22:15:12 +00001226
1227 /*** begin filename stuff here *****/
1228 /*
1229 * Determine the number of files to be dealt with
1230 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001231 if (optind == argc) {
plars865695b2001-08-27 22:15:12 +00001232 /*
1233 * no cmd line files, therfore, set
1234 * the default number of auto created files
1235 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001236 if (!num_auto_files && !seq_auto_files)
1237 num_auto_files = 1;
1238 } else {
1239 first_file_ind = optind;
1240 num_files += argc - optind;
plars865695b2001-08-27 22:15:12 +00001241 }
1242
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001243 if (num_auto_files) {
plars865695b2001-08-27 22:15:12 +00001244 num_files += num_auto_files;
1245 }
1246
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001247 if (seq_auto_files) {
plars865695b2001-08-27 22:15:12 +00001248 num_files += seq_auto_files;
1249 }
1250
1251 /*
1252 * get space for file names
1253 */
Cyril Hrubisd218f342014-09-23 13:14:56 +02001254 if ((filenames = malloc(num_files * PATH_MAX)) == NULL) {
plars865695b2001-08-27 22:15:12 +00001255 fprintf(stderr, "%s%s: %d %s/%d: malloc(%d) failed: %s\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08001256 Progname, TagName, Pid, __FILE__, __LINE__,
1257 num_files * PATH_MAX, strerror(errno));
plars865695b2001-08-27 22:15:12 +00001258 exit(1);
1259 }
1260
1261 /*
1262 * fill in filename cmd files then auto files.
1263 */
1264
Wanlong Gao354ebb42012-12-07 10:10:04 +08001265 num = 0;
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001266 if (first_file_ind) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001267 for (ind = first_file_ind; ind < argc; ind++, num++) {
1268 strcpy((char *)filenames + (num * PATH_MAX), argv[ind]);
plars865695b2001-08-27 22:15:12 +00001269 }
1270 }
1271
1272 /*
1273 * construct auto filename and insert them into filenames space
1274 */
Garrett Cooper2c282152010-12-16 00:55:50 -08001275
Wanlong Gao354ebb42012-12-07 10:10:04 +08001276 for (ind = 0; ind < num_auto_files; ind++, num++) {
1277 gettimeofday(&tv1, NULL);
1278 sprintf((char *)filenames + (num * PATH_MAX),
1279 "%s/%s%ld%ld%d.%d", auto_dir, auto_file,
1280 (long)tv1.tv_sec, (long)tv1.tv_usec, rand(), ind);
plars865695b2001-08-27 22:15:12 +00001281 }
1282
1283 /*
1284 * construct auto seq filenames
1285 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001286 for (ind = 1; ind <= seq_auto_files; ind++, num++) {
1287 sprintf((char *)filenames + (num * PATH_MAX), "%s/%s%d",
plars865695b2001-08-27 22:15:12 +00001288 auto_dir, auto_file, ind);
1289 }
1290
1291/**** end filename stuff ****/
1292
Wanlong Gao354ebb42012-12-07 10:10:04 +08001293 if (time_iterval > 0) {
subrata_modak90d1e3c2008-08-21 13:07:02 +00001294 struct timeval ts;
Wanlong Gao354ebb42012-12-07 10:10:04 +08001295 gettimeofday(&ts, NULL);
1296 start_time = ts.tv_sec;
subrata_modak90d1e3c2008-08-21 13:07:02 +00001297 //start_time=time(0);
1298 }
plars865695b2001-08-27 22:15:12 +00001299
1300 /*
1301 * get space for I/O buffer
1302 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001303 if (grow_incr) {
Cyril Hrubisd218f342014-09-23 13:14:56 +02001304 if ((Buffer = malloc(grow_incr + Alignment)) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001305 fprintf(stderr,
1306 "%s%s: %d %s/%d: malloc(%d) failed: %s\n",
1307 Progname, TagName, Pid, __FILE__, __LINE__,
1308 grow_incr, strerror(errno));
plars865695b2001-08-27 22:15:12 +00001309 exit(1);
1310 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001311 if (Alignment)
Wanlong Gao354ebb42012-12-07 10:10:04 +08001312 Buffer = Buffer + Alignment;
plars865695b2001-08-27 22:15:12 +00001313
1314 }
1315
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001316 if (Debug > 2) {
plars865695b2001-08-27 22:15:12 +00001317 printf("%s: %d DEBUG3 num_files = %d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08001318 Progname, Pid, num_files);
plars865695b2001-08-27 22:15:12 +00001319 }
plars865695b2001-08-27 22:15:12 +00001320#ifndef linux
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001321 if (pre_alloc_space) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001322 if (iterations == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001323 fprintf(stderr,
1324 "%s%s: %d %s/%d: can NOT pre-alloc and grow forever\n",
1325 Progname, TagName, Pid, __FILE__, __LINE__);
1326 exit(1);
plars865695b2001-08-27 22:15:12 +00001327 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001328 if (Mode & MODE_RAND_SIZE) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001329 fprintf(stderr,
1330 "%s%s: %d %s/%d: can NOT pre-alloc and do random io size\n",
1331 Progname, TagName, Pid, __FILE__, __LINE__);
1332 exit(1);
plars865695b2001-08-27 22:15:12 +00001333 }
1334
Wanlong Gao354ebb42012-12-07 10:10:04 +08001335 total_grow_value = grow_incr * iterations;
plars865695b2001-08-27 22:15:12 +00001336
1337 /*
subrata_modak4bb656a2009-02-26 12:02:09 +00001338 * attempt to limit
plars865695b2001-08-27 22:15:12 +00001339 */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001340 if (bytes_to_consume && bytes_to_consume < total_grow_value) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001341 total_grow_value = bytes_to_consume;
plars865695b2001-08-27 22:15:12 +00001342 }
1343 }
1344#endif
1345
1346 /*
1347 * If delaying between iterations, get amount time to
1348 * delaysecs in clocks or usecs.
1349 * If on the CRAY, delaytime is in clocks since
1350 * _rtc() will be used, which does not have the overhead
Wanlong Gao354ebb42012-12-07 10:10:04 +08001351 * of gettimeofday(2).
plars865695b2001-08-27 22:15:12 +00001352 */
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001353 if (delaysecs) {
plars865695b2001-08-27 22:15:12 +00001354#if CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08001355 int hz;
1356 hz = sysconf(_SC_CLK_TCK);
1357 delaytime = (int)((float)hz * delaysecs);
plars865695b2001-08-27 22:15:12 +00001358#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08001359 delaytime = (int)((float)USECS_PER_SEC * delaysecs);
plars865695b2001-08-27 22:15:12 +00001360#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +08001361 }
plars865695b2001-08-27 22:15:12 +00001362
1363 /*
1364 * This is the main iteration loop.
1365 * Each iteration, all files can be opened, written to,
subrata_modak4bb656a2009-02-26 12:02:09 +00001366 * read to check the write, check the whole file,
Garrett Cooper2c282152010-12-16 00:55:50 -08001367 * truncated, and closed.
plars865695b2001-08-27 22:15:12 +00001368 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001369 for (Iter_cnt = 1; !stop; Iter_cnt++) {
subrata_modak90d1e3c2008-08-21 13:07:02 +00001370 struct timeval ts;
Wanlong Gao354ebb42012-12-07 10:10:04 +08001371 if (iterations && (Iter_cnt >= iterations + 1)) {
1372 strcpy(reason, "Hit iteration value");
1373 stop = 1;
1374 continue;
plars865695b2001-08-27 22:15:12 +00001375 }
Wanlong Gao354ebb42012-12-07 10:10:04 +08001376 gettimeofday(&ts, NULL);
1377 if ((time_iterval > 0)
1378 && (start_time + time_iterval < ts.tv_sec)) {
plars865695b2001-08-27 22:15:12 +00001379
Wanlong Gao354ebb42012-12-07 10:10:04 +08001380 sprintf(reason, "Hit time value of %d", time_iterval);
1381 stop = 1;
plars865695b2001-08-27 22:15:12 +00001382 continue;
1383 }
1384
Wanlong Gao354ebb42012-12-07 10:10:04 +08001385 if (bytes_to_consume && bytes_consumed >= bytes_to_consume) {
1386 sprintf(reason, "Hit bytes consumed value of %d",
1387 bytes_to_consume);
1388 stop = 1;
1389 continue;
1390 }
plars865695b2001-08-27 22:15:12 +00001391
Wanlong Gao354ebb42012-12-07 10:10:04 +08001392 /*
1393 * This loop will loop through all files.
1394 * Each iteration, a single file can be opened, written to,
1395 * read to check the write, check the whole file,
1396 * truncated, and closed.
1397 */
1398 for (ind = 0; ind < num_files; ind++) {
1399
1400 fflush(stdout);
1401 fflush(stderr);
1402
1403 filename = (char *)filenames + (ind * PATH_MAX);
1404 Fileinfo.filename =
1405 (char *)filenames + (ind * PATH_MAX);
1406
1407 if (open_flags == RANDOM_OPEN) {
1408 ret =
1409 Open_flags[random_range
1410 (0,
1411 sizeof(Open_flags) /
1412 sizeof(int) - 1, 1, NULL)];
1413 }
1414
1415 else
1416 ret = open_flags;
1417
1418 Fileinfo.openflags = ret;
1419
1420 if (Debug > 3) {
1421 printf
1422 ("%s: %d DEBUG3 %s/%d: %d Open filename = %s, open flags = %#o %s\n",
1423 Progname, Pid, __FILE__, __LINE__,
1424 Iter_cnt, filename, ret,
1425 openflags2symbols(ret, ",", 0));
1426 } else if (Debug > 2) {
1427 printf
1428 ("%s: %d DEBUG3 %s/%d: %d filename = %s, open flags = %#o\n",
1429 Progname, Pid, __FILE__, __LINE__,
1430 Iter_cnt, filename, ret);
1431 }
1432
1433 /*
1434 * open file with desired flags.
1435 */
1436 if ((fd = open(filename, ret, 0777)) == -1) {
1437 fprintf(stderr,
1438 "%s%s: %d %s/%d: open(%s, %#o, 0777) returned -1, errno:%d %s\n",
1439 Progname, TagName, Pid, __FILE__,
1440 __LINE__, filename, ret, errno,
1441 strerror(errno));
1442 handle_error();
1443 continue;
1444 }
1445
1446 Fileinfo.fd = fd;
1447
1448 lkfile(fd, LOCK_EX, LKLVL1); /* lock if lockfile is LKLVL1 */
plars865695b2001-08-27 22:15:12 +00001449
1450#ifndef linux
Wanlong Gao354ebb42012-12-07 10:10:04 +08001451 /*
1452 * preallocation is only done once, if specified.
1453 */
1454 if (pre_alloc_space) {
1455 if (pre_alloc(fd, total_grow_value) != 0) {
1456 cleanup();
1457 exit(2);
1458 }
1459 if (Debug > 1) {
1460 printf
1461 ("%s: %d DEBUG2 %s/%d: pre_allocated %ld for file %s\n",
1462 Progname, Pid, __FILE__, __LINE__,
1463 total_grow_value, filename);
1464 }
1465 lkfile(fd, LOCK_UN, LKLVL1); /* release lock */
1466 close(fd);
1467 Iter_cnt = 0; /* reset outside loop to restart from one */
1468 continue;
plars865695b2001-08-27 22:15:12 +00001469 }
plars865695b2001-08-27 22:15:12 +00001470#endif
1471
Wanlong Gao354ebb42012-12-07 10:10:04 +08001472 /*
1473 * grow file by desired amount.
1474 * growfile() will set the Grow_incr variable and
1475 * possiblly update the Mode variable indicating
1476 * if we are dealing with a FIFO file.
1477 */
plars865695b2001-08-27 22:15:12 +00001478
Wanlong Gao354ebb42012-12-07 10:10:04 +08001479 /* BUG:14136 (don't go past ext2's filesize limit) */
1480 curr_size = file_size(fd);
1481 if (curr_size + grow_incr >= ext2_limit) {
1482 lkfile(fd, LOCK_UN, LKLVL1); /* release lock */
1483 close(fd);
1484 sprintf(reason,
1485 "Reached %ld filesize which is almost %ld limit.",
1486 curr_size, ext2_limit);
1487 stop = 1;
1488 continue;
1489 }
1490
1491 if (growfile(fd, filename, grow_incr, Buffer, &curr_size) != 0) { /* BUG:14136 */
1492 handle_error();
1493 lkfile(fd, LOCK_UN, LKLVL1); /* release lock */
1494 close(fd);
1495 continue;
1496 }
1497
1498 /*
1499 * check if last write is not corrupted
1500 */
1501 if (check_write(fd, write_check_inter, filename,
1502 Mode) != 0) {
1503 handle_error();
1504 }
1505
1506 /*
1507 * Check that whole file is not corrupted.
1508 */
1509 if (check_file(fd, file_check_inter, filename,
1510 no_file_check) != 0) {
1511 handle_error();
1512 }
1513
1514 /*
1515 * shrink file by desired amount if it is time
1516 */
1517
1518 if (shrinkfile
1519 (fd, filename, trunc_incr, trunc_inter,
1520 Mode) != 0) {
1521 handle_error();
1522 }
1523
1524 lkfile(fd, LOCK_UN, LKLVL1); /* release lock */
1525
1526 if (Debug > 4)
1527 printf
1528 ("%s: %d DEBUG5 %s/%d: %d Closing file %s fd:%d \n",
1529 Progname, Pid, __FILE__, __LINE__,
1530 Iter_cnt, filename, fd);
mridgedec5a462005-02-25 15:49:37 +00001531 close(fd);
mridgedec5a462005-02-25 15:49:37 +00001532
Wanlong Gao354ebb42012-12-07 10:10:04 +08001533 /*
1534 * Unlink the file if that is desired
1535 */
1536 if (unlink_inter && (Iter_cnt % unlink_inter == 0)) {
plars865695b2001-08-27 22:15:12 +00001537
Wanlong Gao354ebb42012-12-07 10:10:04 +08001538 if (Debug > 4)
1539 printf
1540 ("%s: %d DEBUG5 %s/%d: %d Unlinking file %s\n",
1541 Progname, Pid, __FILE__, __LINE__,
1542 Iter_cnt, filename);
plars865695b2001-08-27 22:15:12 +00001543
Wanlong Gao354ebb42012-12-07 10:10:04 +08001544 unlink(filename);
1545 }
plars865695b2001-08-27 22:15:12 +00001546
Wanlong Gao354ebb42012-12-07 10:10:04 +08001547 /*
1548 * delay while staying active for "delaysecs" seconds.
1549 */
1550 if (delaytime) {
plars865695b2001-08-27 22:15:12 +00001551
Wanlong Gao354ebb42012-12-07 10:10:04 +08001552 int ct, end;
plars865695b2001-08-27 22:15:12 +00001553#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08001554 ct = _rtc();
1555 end = ct + delaytime;
1556 while (ct < end) {
1557 ct = _rtc();
1558 }
plars865695b2001-08-27 22:15:12 +00001559#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08001560 struct timeval curtime;
1561 gettimeofday(&curtime, NULL);
1562 ct = curtime.tv_sec * USECS_PER_SEC +
1563 curtime.tv_usec;
1564 end = ct + delaytime;
1565 while (ct < end) {
plars865695b2001-08-27 22:15:12 +00001566
Wanlong Gao354ebb42012-12-07 10:10:04 +08001567 gettimeofday(&curtime, NULL);
1568 ct = curtime.tv_sec * USECS_PER_SEC +
1569 curtime.tv_usec;
1570 }
plars865695b2001-08-27 22:15:12 +00001571#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +08001572 }
1573 }
plars865695b2001-08-27 22:15:12 +00001574#ifndef linux
Wanlong Gao354ebb42012-12-07 10:10:04 +08001575 /*
1576 * if Iter_cnt == 0, then we pre allocated space to all files
1577 * and we are starting outside loop over. Set pre_alloc_space
1578 * to zero otherwise we get in infinite loop
1579 */
1580 if (Iter_cnt == 0) {
1581 pre_alloc_space = 0;
1582 }
plars865695b2001-08-27 22:15:12 +00001583#endif
1584
Wanlong Gao354ebb42012-12-07 10:10:04 +08001585 } /* end iteration for loop */
plars865695b2001-08-27 22:15:12 +00001586
Wanlong Gao354ebb42012-12-07 10:10:04 +08001587 if (Debug) {
1588 printf("%s%s: %d %s/%d: DONE %d iterations to %d files. %s\n",
1589 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt,
1590 num_files, reason);
plars865695b2001-08-27 22:15:12 +00001591 }
1592 fflush(stdout);
1593 fflush(stderr);
1594
1595 cleanup();
1596
Garrett Cooperdf3eb162010-11-28 22:44:32 -08001597 if (Errors) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001598 if (Debug > 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001599 printf("%s%s: %d DEBUG3 %d error(s) encountered\n",
1600 Progname, TagName, Pid, Errors);
1601 printf
1602 ("%s%s: %d DEBUG3 %s/%d: exiting with value of 1\n",
1603 Progname, TagName, Pid, __FILE__, __LINE__);
plars865695b2001-08-27 22:15:12 +00001604 }
1605 exit(1);
1606 }
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001607 if (Debug > 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001608 printf
1609 ("%s%s: %d DEBUG3 %s/%d: no errors, exiting with value of 0\n",
1610 Progname, TagName, Pid, __FILE__, __LINE__);
1611 }
mridge24227452004-05-17 22:06:02 +00001612
plars865695b2001-08-27 22:15:12 +00001613 exit(0);
Wanlong Gao354ebb42012-12-07 10:10:04 +08001614 tst_exit(); /* to keep compiler happy */
plars865695b2001-08-27 22:15:12 +00001615}
1616
1617/***********************************************************************
1618 *
1619 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001620int set_sig(void)
plars865695b2001-08-27 22:15:12 +00001621{
Alex Elder97360e72011-04-01 17:01:26 -05001622 int sig;
plars865695b2001-08-27 22:15:12 +00001623
Wanlong Gao354ebb42012-12-07 10:10:04 +08001624 /*
1625 * now loop through all signals and set the handlers
1626 */
plars865695b2001-08-27 22:15:12 +00001627
Wanlong Gao354ebb42012-12-07 10:10:04 +08001628 for (sig = 1; sig < NSIG; sig++) {
1629 switch (sig) {
1630 case SIGKILL:
1631 case SIGSTOP:
1632 case SIGCONT:
plars865695b2001-08-27 22:15:12 +00001633#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08001634 case SIGINFO:
1635 case SIGRECOVERY:
plars865695b2001-08-27 22:15:12 +00001636#endif /* CRAY */
1637#ifdef SIGCKPT
Wanlong Gao354ebb42012-12-07 10:10:04 +08001638 case SIGCKPT:
plars865695b2001-08-27 22:15:12 +00001639#endif /* SIGCKPT */
1640#ifdef SIGRESTART
Wanlong Gao354ebb42012-12-07 10:10:04 +08001641 case SIGRESTART:
plars865695b2001-08-27 22:15:12 +00001642#endif /* SIGRESTART */
Khem Rajad33a3d2016-07-21 21:26:51 -07001643 case SIGCHLD:
Wanlong Gao354ebb42012-12-07 10:10:04 +08001644 break;
plars865695b2001-08-27 22:15:12 +00001645
Wanlong Gao354ebb42012-12-07 10:10:04 +08001646 default:
plars865695b2001-08-27 22:15:12 +00001647#ifdef sgi
Wanlong Gao354ebb42012-12-07 10:10:04 +08001648 sigset(sig, sig_handler);
plars865695b2001-08-27 22:15:12 +00001649#else
1650/* linux and cray */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001651 signal(sig, sig_handler);
plars865695b2001-08-27 22:15:12 +00001652#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +08001653 break;
1654 }
1655 } /* endfor */
plars865695b2001-08-27 22:15:12 +00001656
Wanlong Gao354ebb42012-12-07 10:10:04 +08001657 return 0;
plars865695b2001-08-27 22:15:12 +00001658}
1659
1660/***********************************************************************
1661 *
1662 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001663void sig_handler(int sig)
plars865695b2001-08-27 22:15:12 +00001664{
Wanlong Gao354ebb42012-12-07 10:10:04 +08001665 int exit_stat = 2;
plars865695b2001-08-27 22:15:12 +00001666
Wanlong Gao354ebb42012-12-07 10:10:04 +08001667 if (sig == SIGUSR2) {
1668 fprintf(stdout,
1669 "%s%s: %d %s/%d: received SIGUSR2 (%d) - stopping.\n",
1670 Progname, TagName, Pid, __FILE__, __LINE__, sig);
plars865695b2001-08-27 22:15:12 +00001671#ifndef sgi
Wanlong Gao354ebb42012-12-07 10:10:04 +08001672 signal(sig, sig_handler); /* allow us to get this signal more than once */
plars865695b2001-08-27 22:15:12 +00001673#endif
Garrett Cooper2c282152010-12-16 00:55:50 -08001674
Wanlong Gao354ebb42012-12-07 10:10:04 +08001675 } else if (sig == SIGINT) {
1676 /* The user has told us to cleanup, don't pretend it's an error. */
1677 exit_stat = 0;
1678 if (Debug != 0) {
1679 fprintf(stderr,
1680 "%s%s: %d %s/%d: received unexpected signal: %d\n",
1681 Progname, TagName, Pid, __FILE__, __LINE__,
1682 sig);
1683 }
1684 } else {
1685 fprintf(stderr,
1686 "%s%s: %d %s/%d: received unexpected signal: %d\n",
1687 Progname, TagName, Pid, __FILE__, __LINE__, sig);
plars865695b2001-08-27 22:15:12 +00001688 }
plars865695b2001-08-27 22:15:12 +00001689
Wanlong Gao354ebb42012-12-07 10:10:04 +08001690 notify_others();
1691 cleanup();
1692 if (Debug > 2) {
1693 printf("%s%s: %d DEBUG3 %s/%d: Exiting with a value of %d\n",
1694 Progname, TagName, Pid, __FILE__, __LINE__, exit_stat);
1695 }
1696 exit(exit_stat);
plars865695b2001-08-27 22:15:12 +00001697}
1698
1699/***********************************************************************
1700 * this function attempts to send SIGUSR2 to other growfiles processes
1701 * telling them to stop.
Garrett Cooper2c282152010-12-16 00:55:50 -08001702 *
plars865695b2001-08-27 22:15:12 +00001703 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001704static void notify_others(void)
plars865695b2001-08-27 22:15:12 +00001705{
Wanlong Gao354ebb42012-12-07 10:10:04 +08001706 static int send_signals = 0;
1707 int ind;
plars865695b2001-08-27 22:15:12 +00001708
Wanlong Gao354ebb42012-12-07 10:10:04 +08001709 if (Sync_with_others && send_signals == 0) {
plars865695b2001-08-27 22:15:12 +00001710
1711#if CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08001712 send_signals = 1; /* only send signals once */
1713 if (Debug > 1)
1714 printf
1715 ("%s%s: %d DEBUG2 %s/%d: Sending SIGUSR2 to pgrp\n",
1716 Progname, TagName, Pid, __FILE__, __LINE__);
1717 killm(C_PGRP, getpgrp(), SIGUSR2);
plars865695b2001-08-27 22:15:12 +00001718#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08001719 send_signals = 1; /* only send signals once */
plars865695b2001-08-27 22:15:12 +00001720
Wanlong Gao354ebb42012-12-07 10:10:04 +08001721 for (ind = 0; ind < Forker_npids; ind++) {
1722 if (Forker_pids[ind] != Pid)
1723 if (Debug > 1)
1724 printf
1725 ("%s%s: %d DEBUG2 %s/%d: Sending SIGUSR2 to pid %d\n",
1726 Progname, TagName, Pid, __FILE__,
1727 __LINE__, Forker_pids[ind]);
1728 kill(Forker_pids[ind], SIGUSR2);
1729 }
plars865695b2001-08-27 22:15:12 +00001730#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +08001731 }
plars865695b2001-08-27 22:15:12 +00001732
1733}
1734
1735/***********************************************************************
1736 * this function will count the number of errors encountered.
1737 * This function will call upanic if wanted or cleanup and
1738 * and exit is Maxerrs were encountered.
1739 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001740int handle_error(void)
plars865695b2001-08-27 22:15:12 +00001741{
Wanlong Gao354ebb42012-12-07 10:10:04 +08001742 Errors++;
plars865695b2001-08-27 22:15:12 +00001743
1744#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08001745 if (Errors & Upanic_on_error) {
1746 upanic(PA_PANIC);
1747 }
plars865695b2001-08-27 22:15:12 +00001748#endif
1749
Wanlong Gao354ebb42012-12-07 10:10:04 +08001750 if (Maxerrs && Errors >= Maxerrs) {
1751 printf("%s%s: %d %s/%d: %d Hit max errors value of %d\n",
1752 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt,
1753 Maxerrs);
1754 notify_others();
1755 cleanup();
plars865695b2001-08-27 22:15:12 +00001756
Wanlong Gao354ebb42012-12-07 10:10:04 +08001757 if (Debug > 2) {
1758 printf("%s%s: %d DEBUG3 %d error(s) encountered\n",
1759 Progname, TagName, Pid, Errors);
1760 printf
1761 ("%s%s: %d DEBUG3 %s/%d: exiting with value of 1\n",
1762 Progname, TagName, Pid, __FILE__, __LINE__);
1763 }
plars865695b2001-08-27 22:15:12 +00001764
Wanlong Gao354ebb42012-12-07 10:10:04 +08001765 exit(1);
plars865695b2001-08-27 22:15:12 +00001766 }
Wanlong Gao354ebb42012-12-07 10:10:04 +08001767
plars865695b2001-08-27 22:15:12 +00001768 return 0;
1769}
1770
1771/***********************************************************************
1772 *
1773 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001774int cleanup(void)
plars865695b2001-08-27 22:15:12 +00001775{
Wanlong Gao354ebb42012-12-07 10:10:04 +08001776 int ind;
plars865695b2001-08-27 22:15:12 +00001777
Wanlong Gao354ebb42012-12-07 10:10:04 +08001778 if (remove_files) {
1779 if (Debug > 2)
1780 printf("%s: %d DEBUG3 Removing all %d files\n",
1781 Progname, Pid, num_files);
1782 for (ind = 0; ind <= num_files; ind++) {
1783 unlink(filenames + (ind * PATH_MAX));
1784 }
1785 }
1786 if (using_random && Debug > 1)
1787 printf("%s%s: %d DEBUG2 Used random seed: %d\n",
1788 Progname, TagName, Pid, Seed);
1789 return 0;
1790}
plars865695b2001-08-27 22:15:12 +00001791
1792/***********************************************************************
1793 *
1794 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001795void usage(void)
1796{
1797 fprintf(stderr,
1798 "Usage: %s%s [-bhEluy][[-g grow_incr][-i num][-t trunc_incr][-T trunc_inter]\n",
1799 Progname, TagName);
1800 fprintf(stderr,
1801 "[-d auto_dir][-e maxerrs][-f auto_file][-N num_files][-w][-c chk_inter][-D debug]\n");
1802 fprintf(stderr,
1803 "[-s seed][-S seq_auto_files][-p][-P PANIC][-I io_type][-o open_flags][-B maxbytes]\n");
1804 fprintf(stderr,
1805 "[-r iosizes][-R lseeks][-U unlk_inter][-W tagname] [files]\n");
1806
1807 return;
1808
1809} /* end of usage */
1810
1811/***********************************************************************
1812 *
1813 ***********************************************************************/
1814void help(void)
plars865695b2001-08-27 22:15:12 +00001815{
1816 usage();
1817
Wanlong Gao354ebb42012-12-07 10:10:04 +08001818 fprintf(stdout, "\
plars865695b2001-08-27 22:15:12 +00001819 -h Specfied to print this help and exit.\n\
1820 -b Specfied to execute in sync mode.(def async mode)\n\
1821 -B maxbytes Max bytes to consume by all files. growfiles exits when more\n\
1822 than maxbytes have been consumed. (def no chk) If maxbytes ends\n\
1823 with the letter 'b', maxbytes is multiplied by BSIZE\n\
1824 -C write_chk Specifies how often to check the last write (default 1)\n\
1825 -c file_chk Specifies how often to check whole file (default 0)\n\
1826 -d auto_dir Specifies the directory to auto created files. (default .)\n\
1827 -D debug_lvl Specifies the debug level (default 1)\n\
1828 -E Print examples and exit\n\
1829 -e errs The number errors that will terminate this program (def 100)\n\
1830 -f auto_file Specifies the base filename files created. (default \"gf\")\n\
1831 -g grow_incr Specfied to grow by incr for each num. (default 4096)\n\
1832 grow_incr may end in b for blocks\n\
1833 If -r option is used, this option is ignored and size is random\n\
1834 -H delay Amount of time to delay between each file (default 0.0)\n\
1835 -I io_type Specifies io type: s - sync, p - polled async, a - async (def s)\n\
1836 l - listio sync, L - listio async, r - random\n\
1837 -i iteration Specfied to grow each file num times. 0 means forever (default 1)\n\
1838 -l Specfied to do file locking around write/read/trunc\n\
1839 If specified twice, file locking after open to just before close\n\
1840 -L time Specfied to exit after time secs, must be used with -i.\n\
1841 -N num_files Specifies the number of files to be created.\n\
1842 The default is zero if cmd line files.\n\
1843 The default is one if no cmd line files.\n\
1844 -n num_procs Specifies the number of copies of this cmd.\n\
1845 -o op_type Specifies open flages: (def O_RDWR,O_CREAT) op_type can be 'random'\n\
1846 -O offset adjust i/o buffer alignment by offset bytes\n\
1847 -P PANIC Specifies to call upanic on error.\n\
1848 -p Specifies to pre-allocate space\n\
1849 -q pattern pattern can be a - ascii, p - pid with boff, o boff (def)\n\
1850 A - Alternating bits, r - random, O - all ones, z - all zeros,\n\
1851 c - checkboard, C - counting\n\
1852 -R [min-]max random lseek before write and trunc, max of -1 means filesz,\n\
1853 -2 means filesz+grow, -3 filesz-grow. (min def is 0)\n\
1854 -r [min-]max random io write size (min def is 1)\n\
1855 -S seq_auto_files Specifies the number of seqental auto files (default 0)\n\
1856 -s seed[,seed...] Specifies the random number seed (default time(0)+pid)\n\
1857 -t trunc_incr Specfied the amount to shrink file. (default 4096)\n\
1858 trunc_inter may end in b for blocks\n\
1859 If -R option is used, this option is ignored and trunc is random\n\
1860 -T trunc_inter Specfied the how many grows happen before shrink. (default 0)\n\
1861 -u unlink files before exit\n\
1862 -U ui[-ui2] Unlink files each ui iteration (def 0)\n\
1863 -w Specfied to grow via lseek instead of writes.\n\
1864 -W tag-name Who-am-i. My Monster tag name. (used by Monster).\n\
1865 -x Re-exec children before continuing - useful on MPP systems\n\
1866 -y Attempt to sync copies - if one fails it will send sigusr2 to others\n\
1867 Action to each file every iteration is open, write, write check\n\
1868 file check, trunc and closed.\n");
1869
1870 return;
1871}
1872
1873/***********************************************************************
1874 *
1875 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001876void prt_examples(FILE * stream)
plars865695b2001-08-27 22:15:12 +00001877{
1878 /* This example creates 200 files in directory dir1. It writes */
1879 /* 4090 bytes 100 times then truncates 408990 bytes off the file */
1880 /* The file contents are checked every 1000 grow. */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001881 fprintf(stream,
1882 "# run forever: writes of 4090 bytes then on every 100 iterval\n\
plars865695b2001-08-27 22:15:12 +00001883# truncate file by 408990 bytes. Done to 200 files in dir1.\n\
Wanlong Gao354ebb42012-12-07 10:10:04 +08001884%s -i 0 -g 4090 -T 100 -t 408990 -l -C 10 -c 1000 -d dir1 -S 200\n\n",
1885 Progname);
plars865695b2001-08-27 22:15:12 +00001886
1887 /* same as above with 5000 byte grow and a 499990 byte tuncate */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001888 fprintf(stream,
1889 "# same as above with writes of 5000 bytes and truncs of 499990\n\
1890%s -i 0 -g 5000 -T 100 -t 499990 -l -C 10 -c 1000 -d dir2 -S 200\n\n",
1891 Progname);
plars865695b2001-08-27 22:15:12 +00001892
1893 /* This example beats on opens and closes */
Wanlong Gao354ebb42012-12-07 10:10:04 +08001894 fprintf(stream,
1895 "# runs forever: beats on opens and closes of file ocfile - no io\n\
1896%s -i 0 -g 0 -c 0 -C 0 ocfile\n\n",
1897 Progname);
plars865695b2001-08-27 22:15:12 +00001898
Wanlong Gao354ebb42012-12-07 10:10:04 +08001899 fprintf(stream, "# writes 4096 to files until 50 blocks are written\n\
plars865695b2001-08-27 22:15:12 +00001900%s -i 0 -g 4096 -B 50b file1 file2\n\n", Progname);
subrata_modakbdbaec52009-02-26 12:14:51 +00001901
Wanlong Gao354ebb42012-12-07 10:10:04 +08001902 fprintf(stream,
1903 "# write one byte to 750 files in gdir then unlinks them\n\
plars865695b2001-08-27 22:15:12 +00001904%s -g 1 -C 0 -d gdir -u -S 750\n\n", Progname);
1905
Wanlong Gao354ebb42012-12-07 10:10:04 +08001906 fprintf(stream, "# run 30 secs: random iosize, random lseek up to eof\n\
plars865695b2001-08-27 22:15:12 +00001907%s -r 1-5000 -R 0--1 -i 0 -L 30 -C 1 g_rand1 g_rand2\n\n", Progname);
1908
Wanlong Gao354ebb42012-12-07 10:10:04 +08001909 fprintf(stream,
1910 "# run 30 secs: grow by lseek then write single byte, trunc every 10 itervals\n\
1911%s -g 5000 -wlu -i 0 -L 30 -C 1 -T 10 g_sleek1 g_lseek2\n\n",
1912 Progname);
plars865695b2001-08-27 22:15:12 +00001913
Wanlong Gao354ebb42012-12-07 10:10:04 +08001914 fprintf(stream,
1915 "# run forever: 5 copies of random iosize, random lseek to beyond eof,\n\
plars865695b2001-08-27 22:15:12 +00001916# rand io types doing a trunc every 5 iterations, with unlinks.\n\
subrata_modak4bb656a2009-02-26 12:02:09 +00001917%s -i0 -r 1-50000 -R 0--2 -I r -C1 -l -n5 -u -U 100-200 gf_rana gf_ranb\n\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08001918 Progname);
plars865695b2001-08-27 22:15:12 +00001919
Wanlong Gao354ebb42012-12-07 10:10:04 +08001920 fprintf(stream,
1921 "# run forever: 5 copies of random iosize, random lseek to beyond eof,\n\
plars865695b2001-08-27 22:15:12 +00001922# random open flags, rand io types doing a trunc every 10 iterations.\n\
subrata_modak4bb656a2009-02-26 12:02:09 +00001923%s -i0 -r 1-50000 -R 0--2 -o random -I r -C0 -l -T 20 -uU100-200 -n 5 gf_rand1 gf_rand2\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08001924 Progname);
plars865695b2001-08-27 22:15:12 +00001925
plars865695b2001-08-27 22:15:12 +00001926 return;
1927}
1928
1929/***********************************************************************
1930 *
1931 * The file descriptor current offset is assumed to be the end of the
Garrett Cooper2c282152010-12-16 00:55:50 -08001932 * file.
plars865695b2001-08-27 22:15:12 +00001933 * Woffset will be set to the offset before the write.
1934 * Grow_incr will be set to the size of the write or lseek write.
1935 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08001936int /* BUG:14136 */ growfile(int fd, char *file, int grow_incr, char *buf,
1937 unsigned long *curr_size_ptr)
plars865695b2001-08-27 22:15:12 +00001938{
Wanlong Gao354ebb42012-12-07 10:10:04 +08001939 off_t noffset;
1940 int ret;
1941 int cur_offset;
1942 char *errmsg;
1943 off_t fsize; /* current size of file */
1944 int size_grew; /* size the file grew */
1945 struct stat stbuf;
1946 off_t off_tmp = 0;
plars865695b2001-08-27 22:15:12 +00001947
Wanlong Gao354ebb42012-12-07 10:10:04 +08001948 /*
1949 * Do a stat on the open file.
1950 * If the file is a fifo, set the bit in Mode variable.
1951 * This fifo check must be done prior to growfile() returning.
plars865695b2001-08-27 22:15:12 +00001952 * Also get the current size of the file.
Wanlong Gao354ebb42012-12-07 10:10:04 +08001953 */
1954 if (fstat(fd, &stbuf) != -1) {
1955 if (S_ISFIFO(stbuf.st_mode)) {
1956 Fileinfo.mode |= MODE_FIFO;
1957 Mode |= MODE_FIFO;
1958 if (Debug > 3)
1959 printf
1960 ("%s: %d DEBUG4 %s/%d: file is a fifo - no lseek or truncs,\n",
1961 Progname, Pid, __FILE__, __LINE__);
1962 }
1963 fsize = stbuf.st_size;
plars865695b2001-08-27 22:15:12 +00001964
Wanlong Gao354ebb42012-12-07 10:10:04 +08001965 } else {
1966 fprintf(stderr,
1967 "%s%s: %d %s/%d: Unable to fstat(%d, &buf), errno:%d %s\n",
1968 Progname, TagName, Pid, __FILE__, __LINE__, fd, errno,
1969 strerror(errno));
plars865695b2001-08-27 22:15:12 +00001970
Wanlong Gao354ebb42012-12-07 10:10:04 +08001971 return -1;
plars865695b2001-08-27 22:15:12 +00001972 }
1973
Wanlong Gao354ebb42012-12-07 10:10:04 +08001974 if (grow_incr <= 0) { /* don't attempt i/o if grow_incr <= 0 */
plars865695b2001-08-27 22:15:12 +00001975
Wanlong Gao354ebb42012-12-07 10:10:04 +08001976 Grow_incr = grow_incr;
1977 if (Debug > 2)
1978 printf
1979 ("%s: %d DEBUG3 %s/%d: Not attempting to grow, growsize == %d\n",
1980 Progname, Pid, __FILE__, __LINE__, grow_incr);
1981 return grow_incr;
plars865695b2001-08-27 22:15:12 +00001982 }
1983
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08001984 if (Mode & MODE_RAND_SIZE) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08001985 grow_incr =
1986 random_range(min_size, max_size, mult_size, &errmsg);
1987 if (errmsg != NULL) {
1988 fprintf(stderr,
1989 "%s%s: %d %s/%d: random_range() failed - %s\n",
1990 Progname, TagName, Pid, __FILE__, __LINE__,
1991 errmsg);
1992 return -1;
1993 }
1994 Grow_incr = grow_incr;
1995 } else
1996 Grow_incr = grow_incr;
plars865695b2001-08-27 22:15:12 +00001997
Wanlong Gao354ebb42012-12-07 10:10:04 +08001998 if (!(Mode & MODE_FIFO)) {
1999 if ((cur_offset = lseek(fd, 0, SEEK_CUR)) == -1) {
2000 fprintf(stderr, "%s%s: %d %s/%d: tell failed: %s\n",
2001 Progname, TagName, Pid, __FILE__, __LINE__,
2002 strerror(errno));
2003 return -1;
2004 }
2005 }
plars865695b2001-08-27 22:15:12 +00002006
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002007 if (Mode & MODE_GROW_BY_LSEEK) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002008 Woffset = fsize;
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002009 if (Debug > 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002010 printf
2011 ("%s: %d DEBUG3 %s/%d: Current size of file is %ld\n",
2012 Progname, Pid, __FILE__, __LINE__, (long)Woffset);
2013 printf
2014 ("%s: %d DEBUG3 %s/%d: lseeking to %d byte with SEEK_END\n",
2015 Progname, Pid, __FILE__, __LINE__, grow_incr - 1);
plars865695b2001-08-27 22:15:12 +00002016 }
2017
Wanlong Gao354ebb42012-12-07 10:10:04 +08002018 if ((noffset = lseek(fd, grow_incr - 1, SEEK_END)) == -1) {
2019 fprintf(stderr,
2020 "%s%s: %s/%d: lseek(fd, %d, SEEK_END) failed: %s\n",
2021 Progname, TagName, __FILE__, __LINE__,
2022 grow_incr - 1, strerror(errno));
plars865695b2001-08-27 22:15:12 +00002023 return -1;
2024 }
2025
Wanlong Gao354ebb42012-12-07 10:10:04 +08002026 lkfile(fd, LOCK_EX, LKLVL0); /* get exclusive lock */
Garrett Cooper2c282152010-12-16 00:55:50 -08002027
plars865695b2001-08-27 22:15:12 +00002028#if NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +08002029 ret =
2030 lio_write_buffer(fd, io_type, "w", 1, SIGUSR1, &errmsg, 0);
plars865695b2001-08-27 22:15:12 +00002031#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08002032 ret = write_buffer(fd, io_type, "w", 1, 0, &errmsg);
plars865695b2001-08-27 22:15:12 +00002033#endif
2034
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002035 if (ret != 1) {
subrata_modak4bb656a2009-02-26 12:02:09 +00002036 fprintf(stderr, "%s%s: %d %s/%d: %d %s\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08002037 Progname, TagName, Pid, __FILE__, __LINE__,
2038 Iter_cnt, errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002039 if (ret == -ENOSPC) {
plars865695b2001-08-27 22:15:12 +00002040 cleanup();
2041 exit(2);
2042 }
2043 }
2044/***
2045 write(fd, "w", 1);
2046****/
2047
2048 lkfile(fd, LOCK_UN, LKLVL0);
2049
Wanlong Gao354ebb42012-12-07 10:10:04 +08002050 if (Debug > 2)
2051 printf("%s: %d DEBUG3 %s/%d: %d wrote 1 byte to file\n",
2052 Progname, Pid, __FILE__, __LINE__, Iter_cnt);
plars865695b2001-08-27 22:15:12 +00002053
Wanlong Gao354ebb42012-12-07 10:10:04 +08002054 } else { /* end of grow by lseek */
plars865695b2001-08-27 22:15:12 +00002055
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002056 if (Fileinfo.openflags & O_APPEND) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002057 /*
2058 * Deal with special case of the open flag containing O_APPEND.
2059 * If it does, the current offset does not matter since the write
2060 * will be done end of the file.
2061 */
2062 if (Debug > 4)
2063 printf
2064 ("%s: %d DEBUG5 %s/%d: dealing with O_APPEND condition\n",
2065 Progname, Pid, __FILE__, __LINE__);
2066 lkfile(fd, LOCK_EX, LKLVL0); /* get exclusive lock */
plars865695b2001-08-27 22:15:12 +00002067
Wanlong Gao354ebb42012-12-07 10:10:04 +08002068 /*
2069 * do fstat again to get size of the file.
2070 * This is done inside a file lock (if locks are being used).
2071 */
2072 if (fstat(fd, &stbuf) != -1) {
2073 Woffset = stbuf.st_size;
2074 } else {
2075 fprintf(stderr,
2076 "%s%s: %d %s/%d: Unable to fstat(%d, &buf), errno:%d %s\n",
2077 Progname, TagName, Pid, __FILE__,
2078 __LINE__, fd, errno, strerror(errno));
subrata_modakbdbaec52009-02-26 12:14:51 +00002079
Wanlong Gao354ebb42012-12-07 10:10:04 +08002080 lkfile(fd, LOCK_UN, LKLVL0); /* release lock */
2081 return -1;
2082 }
2083 if (Debug > 2)
2084 printf
2085 ("%s: %d DEBUG3 %s/%d: dealing with O_APPEND condition (offset:fsz:%d)\n",
2086 Progname, Pid, __FILE__, __LINE__,
2087 (int)stbuf.st_size);
plars865695b2001-08-27 22:15:12 +00002088
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002089 } else if (Mode & MODE_RAND_LSEEK) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002090 if (max_lseek == LSK_EOF) { /* within file size */
2091 noffset =
2092 random_range(min_lseek, fsize, 1, NULL);
2093 } else if (max_lseek == LSK_EOFPLUSGROW) {
2094 /* max to beyond file size */
2095 noffset =
2096 random_range(min_lseek, fsize + grow_incr,
2097 1, NULL);
2098 } else if (max_lseek == LSK_EOFMINUSGROW) {
2099 /*
2100 * Attempt to not grow the file.
2101 * If the i/o will fit from min_lseek to EOF,
2102 * pick offset to allow it to fit.
2103 * Otherwise, pick the min_lseek offset and grow
2104 * file by smallest amount.
2105 * If min_lseek is != 0, there will be a problem
2106 * with whole file checking if file is ever smaller
2107 * than min_lseek.
2108 */
2109 if (fsize <= min_lseek + grow_incr)
2110 noffset = min_lseek; /* file will still grow */
2111 else
2112 noffset =
2113 random_range(min_lseek,
2114 fsize - grow_incr, 1,
2115 NULL);
2116 } else {
2117 noffset =
2118 random_range(min_lseek, max_lseek, 1, NULL);
2119 }
plars865695b2001-08-27 22:15:12 +00002120
Wanlong Gao354ebb42012-12-07 10:10:04 +08002121 if ((Woffset = lseek(fd, noffset, SEEK_SET)) == -1) {
2122 fprintf(stderr,
2123 "%s%s: %d %s/%d: lseek(%d, %ld, "
2124 "SEEK_SET) l2 failed: %s\n", Progname,
2125 TagName, Pid, __FILE__, __LINE__, fd,
2126 (long)noffset, strerror(errno));
2127 return -1;
2128 } else if (Debug > 2)
2129 printf("%s: %d DEBUG3 %s/%d: lseeked to "
2130 "random offset %ld (fsz:%d)\n",
2131 Progname, Pid, __FILE__, __LINE__,
2132 (long)Woffset, (int)stbuf.st_size);
plars865695b2001-08-27 22:15:12 +00002133
Wanlong Gao354ebb42012-12-07 10:10:04 +08002134 }
plars865695b2001-08-27 22:15:12 +00002135
2136 /*
2137 * lseek to end of file only if not fifo
2138 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08002139 else if (!(Mode & MODE_FIFO)) {
2140 if ((Woffset = lseek(fd, 0, SEEK_END)) == -1) {
2141 fprintf(stderr,
2142 "%s%s: %d %s/%d: lseek(fd, 0, SEEK_END) failed: %s\n",
2143 Progname, TagName, Pid, __FILE__,
2144 __LINE__, strerror(errno));
2145 return -1;
2146 } else if (Debug > 2)
2147 printf("%s: %d DEBUG3 %s/%d: lseeked to "
2148 "end of file, offset %ld\n",
2149 Progname, Pid, __FILE__, __LINE__,
2150 (long)Woffset);
plars865695b2001-08-27 22:15:12 +00002151 }
2152
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002153 if (Pattern == PATTERN_OFFSET)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002154 datapidgen(STATIC_NUM, buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002155 else if (Pattern == PATTERN_PID)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002156 datapidgen(Pid, buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002157 else if (Pattern == PATTERN_ASCII)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002158 dataasciigen(NULL, buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002159 else if (Pattern == PATTERN_RANDOM)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002160 databingen('r', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002161 else if (Pattern == PATTERN_ALT)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002162 databingen('a', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002163 else if (Pattern == PATTERN_CHKER)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002164 databingen('c', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002165 else if (Pattern == PATTERN_CNTING)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002166 databingen('C', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002167 else if (Pattern == PATTERN_ZEROS)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002168 databingen('z', buf, grow_incr, Woffset);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002169 else if (Pattern == PATTERN_ONES)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002170 databingen('o', buf, grow_incr, Woffset);
plars865695b2001-08-27 22:15:12 +00002171 else
Wanlong Gao354ebb42012-12-07 10:10:04 +08002172 dataasciigen(NULL, buf, grow_incr, Woffset);
plars865695b2001-08-27 22:15:12 +00002173
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002174 if (Debug > 2)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002175 printf
2176 ("%s: %d DEBUG3 %s/%d: attempting to write %d bytes\n",
2177 Progname, Pid, __FILE__, __LINE__, grow_incr);
plars865695b2001-08-27 22:15:12 +00002178
Wanlong Gao354ebb42012-12-07 10:10:04 +08002179 lkfile(fd, LOCK_EX, LKLVL0); /* get exclusive lock */
plars865695b2001-08-27 22:15:12 +00002180
2181/*****
2182 ret=write(fd, buf, grow_incr);
2183
Alex Elder85cef832011-04-12 09:06:04 -05002184 off_tmp = tell(fd);
plars865695b2001-08-27 22:15:12 +00002185
subrata_modakbdbaec52009-02-26 12:14:51 +00002186 lkfile(fd, LOCK_UN, LKLVL0);
plars865695b2001-08-27 22:15:12 +00002187
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002188 if (ret != grow_incr) {
plars865695b2001-08-27 22:15:12 +00002189 fprintf(stderr, "%s: %s/%d: write failed: %s\n",
2190 Progname, __FILE__, __LINE__, strerror(errno));
2191 return -1;
2192 }
2193*****/
2194
2195#if NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +08002196 ret = lio_write_buffer(fd, io_type, buf, grow_incr,
2197 SIGUSR1, &errmsg, 0);
plars865695b2001-08-27 22:15:12 +00002198#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08002199 ret = write_buffer(fd, io_type, buf, grow_incr, 0, &errmsg);
plars865695b2001-08-27 22:15:12 +00002200#endif
2201
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002202 if (Mode & MODE_FIFO) {
plars865695b2001-08-27 22:15:12 +00002203 /* If it is a fifo then just pretend the file
2204 * offset is where we think it should be.
2205 */
Alex Elder85cef832011-04-12 09:06:04 -05002206 off_tmp = Woffset + grow_incr;
Wanlong Gao354ebb42012-12-07 10:10:04 +08002207 } else {
2208 if ((off_tmp = lseek(fd, 0, SEEK_CUR)) < 0) { /* get offset after the write */
2209 fprintf(stderr,
2210 "%s%s: %s/%d: tell(2) failed: %d %s\n",
2211 Progname, TagName, __FILE__, __LINE__,
2212 errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +00002213 return -1;
2214 }
2215#if NEWIO
subrata_modak142cfbb2009-04-03 06:58:18 +00002216#if defined(sgi) || defined(__linux__)
plars865695b2001-08-27 22:15:12 +00002217 /* If this is POSIX I/O and it is via aio_{read,write}
2218 * or lio_listio then after completion of the I/O the
2219 * value of the file offset for the file is
2220 * unspecified--which means we cannot trust what
2221 * tell() told us. Fudge it here.
2222 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08002223 if ((io_type & LIO_IO_ASYNC_TYPES)
2224 || (io_type & LIO_RANDOM)) {
Alex Elder85cef832011-04-12 09:06:04 -05002225 if (off_tmp != Woffset + grow_incr) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002226 if (Debug > 5) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002227 printf
2228 ("%s: %d DEBUG6 %s/%d: posix fudge, forcing tmp (%"
2229 PRId64
2230 ") to match Woffset+grow_incr (%"
2231 PRId64 ")\n", Progname,
2232 Pid, __FILE__, __LINE__,
2233 (int64_t) off_tmp,
2234 (int64_t) Woffset +
2235 grow_incr);
plars865695b2001-08-27 22:15:12 +00002236 }
Alex Elder85cef832011-04-12 09:06:04 -05002237 off_tmp = Woffset + grow_incr;
plars865695b2001-08-27 22:15:12 +00002238 }
2239 }
subrata_modak142cfbb2009-04-03 06:58:18 +00002240#endif /* sgi __linux__ */
plars865695b2001-08-27 22:15:12 +00002241#endif
2242 }
Alex Elder85cef832011-04-12 09:06:04 -05002243 *curr_size_ptr = off_tmp; /* BUG:14136 */
plars865695b2001-08-27 22:15:12 +00002244
subrata_modakbdbaec52009-02-26 12:14:51 +00002245 lkfile(fd, LOCK_UN, LKLVL0);
plars865695b2001-08-27 22:15:12 +00002246
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002247 if (ret != grow_incr) {
subrata_modak4bb656a2009-02-26 12:02:09 +00002248 fprintf(stderr, "%s%s: %d %s/%d: %d %s\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08002249 Progname, TagName, Pid, __FILE__, __LINE__,
2250 Iter_cnt, errmsg);
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002251 if (ret == -ENOSPC) {
plars865695b2001-08-27 22:15:12 +00002252 cleanup();
2253 exit(2);
2254 }
2255 return -1;
2256 }
2257
2258 /*
2259 * Check for a condition where the file was truncated just before
subrata_modak4bb656a2009-02-26 12:02:09 +00002260 * the write.
plars865695b2001-08-27 22:15:12 +00002261 */
Alex Elder85cef832011-04-12 09:06:04 -05002262 if (off_tmp != Woffset + grow_incr) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002263 /*
2264 * The offset after the write was not as expected.
2265 * This could be caused by the following:
2266 * - file truncated after the lseek and before the write.
2267 * - the file was written to after fstat and before the write
2268 * and the file was opened with O_APPEND.
2269 *
2270 * The pattern written to the file will be considered corrupted.
2271 */
2272 if (Debug > 0 && lockfile) {
2273 printf("%s%s: %d DEBUG1 %s/%d: offset after "
2274 "write(%ld) not as exp(%ld+%d=%ld)\n",
2275 Progname, TagName, Pid, __FILE__,
2276 __LINE__, (long)off_tmp, (long)Woffset,
2277 grow_incr, (long)(Woffset + grow_incr));
2278 printf
2279 ("%s%s: %d DEBUG1 %s/%d: %d Assuming file "
2280 "changed by another process, resetting "
2281 "offset:%ld (expect pattern mismatch)\n",
2282 Progname, TagName, Pid, __FILE__, __LINE__,
2283 Iter_cnt, (long)(off_tmp - grow_incr));
2284 }
2285 if (Debug > 4) {
2286 printf
2287 ("%s: %d DEBUG5 %s/%d: about to chop Woffset. "
2288 "tmp=%ld, grow_incr=%d, Woffset was %ld\n",
2289 Progname, Pid, __FILE__, __LINE__,
2290 (long)off_tmp, grow_incr, (long)Woffset);
2291 }
2292 Woffset = off_tmp - grow_incr;
2293 if (Woffset < 0)
2294 Woffset = 0;
plars865695b2001-08-27 22:15:12 +00002295 }
2296
Wanlong Gao354ebb42012-12-07 10:10:04 +08002297 } /* end of grow by write */
plars865695b2001-08-27 22:15:12 +00002298
plars865695b2001-08-27 22:15:12 +00002299 /*
2300 * Woffset - holds start of grow (start of write expect in grow by lseek)
2301 * Grow_incr - holds size of grow (write).
2302 * fsize - holds size of file before write
2303 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08002304 size_grew = (Woffset + Grow_incr) - fsize;
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002305 if (Debug > 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002306 if (Mode & MODE_FIFO) {
2307 printf
2308 ("%s: %d DEBUG2 %s/%d: file is fifo, %d wrote %d bytes\n",
2309 Progname, Pid, __FILE__, __LINE__, Grow_incr,
2310 Iter_cnt);
2311 }
plars865695b2001-08-27 22:15:12 +00002312
Wanlong Gao354ebb42012-12-07 10:10:04 +08002313 else if (size_grew > 0)
2314 printf
2315 ("%s: %d DEBUG2 %s/%d: %d wrote %d bytes(off:%ld), "
2316 "grew file by %d bytes\n", Progname, Pid, __FILE__,
2317 __LINE__, Iter_cnt, Grow_incr, (long)Woffset,
2318 size_grew);
2319 else
2320 printf
2321 ("%s: %d DEBUG2 %s/%d: %d wrote %d bytes(off:%ld), "
2322 "did not grow file\n", Progname, Pid, __FILE__,
2323 __LINE__, Iter_cnt, Grow_incr, (long)Woffset);
plars865695b2001-08-27 22:15:12 +00002324 }
2325
Wanlong Gao354ebb42012-12-07 10:10:04 +08002326 bytes_consumed += size_grew;
plars865695b2001-08-27 22:15:12 +00002327 return 0;
2328
Wanlong Gao354ebb42012-12-07 10:10:04 +08002329} /* end of growfile */
plars865695b2001-08-27 22:15:12 +00002330
2331/***********************************************************************
2332 * shrinkfile file by trunc_incr. file can not be made smaller than
2333 * size zero. Therefore, if trunc_incr is larger than file size,
2334 * file will be truncated to zero.
2335 * The file descriptor current offset is assumed to be the end of the
2336 * file.
2337 *
2338 ***********************************************************************/
2339int
Alex Elder97360e72011-04-01 17:01:26 -05002340shrinkfile(int fd, char *filename, int trunc_incr, int trunc_inter,
Wanlong Gao354ebb42012-12-07 10:10:04 +08002341 int just_trunc)
plars865695b2001-08-27 22:15:12 +00002342{
Wanlong Gao354ebb42012-12-07 10:10:04 +08002343 static int shrink_cnt = 0;
2344 int cur_offset;
2345 int new_offset;
2346 int ret;
plars865695b2001-08-27 22:15:12 +00002347#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08002348 int offset;
plars865695b2001-08-27 22:15:12 +00002349#endif
2350
2351 shrink_cnt++;
2352
Wanlong Gao354ebb42012-12-07 10:10:04 +08002353 if (trunc_inter == 0 || (shrink_cnt % trunc_inter != 0)) {
2354 if (Debug > 3)
2355 printf
2356 ("%s: %d DEBUG4 %s/%d: Not shrinking file - not time, iter=%d, cnt=%d\n",
2357 Progname, Pid, __FILE__, __LINE__, trunc_inter,
2358 shrink_cnt);
2359 return 0; /* not this time */
plars865695b2001-08-27 22:15:12 +00002360 }
2361
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002362 if (Mode & MODE_FIFO) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002363 if (Debug > 5)
2364 printf
2365 ("%s: %d DEBUG5 %s/%d: Not attempting to shrink a FIFO\n",
2366 Progname, Pid, __FILE__, __LINE__);
2367 return 0; /* can not truncate fifo */
plars865695b2001-08-27 22:15:12 +00002368 }
2369
2370 lkfile(fd, LOCK_EX, LKLVL0);
2371
Wanlong Gao354ebb42012-12-07 10:10:04 +08002372 if ((cur_offset = lseek(fd, 0, SEEK_CUR)) == -1) {
2373 fprintf(stderr, "%s%s: %d %s/%d: tell(%d) failed: %s\n",
2374 Progname, TagName, Pid, __FILE__, __LINE__, fd,
2375 strerror(errno));
2376 lkfile(fd, LOCK_UN, LKLVL0);
2377 return -1;
plars865695b2001-08-27 22:15:12 +00002378 }
2379
Wanlong Gao354ebb42012-12-07 10:10:04 +08002380 if (Mode & MODE_RAND_LSEEK) {
2381 if (max_lseek <= -1) {
2382 if ((new_offset = file_size(fd)) == -1) {
2383 lkfile(fd, LOCK_UN, LKLVL0);
2384 return -1;
2385 }
2386
2387 if (new_offset < min_lseek)
2388 new_offset = min_lseek;
2389 else
2390 new_offset =
2391 random_range(min_lseek, new_offset, 1,
2392 NULL);
2393 } else {
2394 new_offset =
2395 random_range(min_lseek, max_lseek, 1, NULL);
plars865695b2001-08-27 22:15:12 +00002396 }
Garrett Cooper2c282152010-12-16 00:55:50 -08002397
plars865695b2001-08-27 22:15:12 +00002398#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08002399 if ((offset = lseek(fd, new_offset, SEEK_SET)) == -1) {
2400 fprintf(stderr,
2401 "%s%s: %d %s/%d: lseek(%d, %d, SEEK_SET) l3 failed: %s\n",
2402 Progname, TagName, Pid, __FILE__, __LINE__, fd,
2403 new_offset, strerror(errno));
2404 lkfile(fd, LOCK_UN, LKLVL0);
2405 return -1;
2406 } else if (Debug > 3)
2407 printf
2408 ("%s: %d DEBUG4 %s/%d: lseeked to random offset %d\n",
2409 Progname, Pid, __FILE__, __LINE__, offset);
Garrett Cooper2c282152010-12-16 00:55:50 -08002410
plars865695b2001-08-27 22:15:12 +00002411#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +08002412 }
plars865695b2001-08-27 22:15:12 +00002413
Wanlong Gao354ebb42012-12-07 10:10:04 +08002414 else { /* remove trunc_incr from file */
plars865695b2001-08-27 22:15:12 +00002415
Wanlong Gao354ebb42012-12-07 10:10:04 +08002416 new_offset = cur_offset - trunc_incr;
plars865695b2001-08-27 22:15:12 +00002417
Wanlong Gao354ebb42012-12-07 10:10:04 +08002418 if (new_offset < 0)
2419 new_offset = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +00002420
plars865695b2001-08-27 22:15:12 +00002421#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08002422 if (lseek(fd, new_offset, SEEK_SET) == -1) {
2423 fprintf(stderr,
2424 "%s%s: %d %s/%d: lseek(fd, %d, SEEK_SET) l4 failed: %s\n",
2425 Progname, TagName, Pid, __FILE__, __LINE__,
2426 new_offset, strerror(errno));
2427 lkfile(fd, LOCK_UN, LKLVL0);
2428 return -1;
2429 } else if (Debug > 3)
2430 printf
2431 ("%s: %d DEBUG4 %s/%d: lseeked to offset %d, %d bytes from end\n",
2432 Progname, Pid, __FILE__, __LINE__, new_offset,
2433 trunc_incr);
plars865695b2001-08-27 22:15:12 +00002434#endif
2435 }
2436
plars865695b2001-08-27 22:15:12 +00002437#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08002438 ret = trunc(fd);
plars865695b2001-08-27 22:15:12 +00002439#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08002440 ret = ftruncate(fd, new_offset);
Garrett Cooper53740502010-12-16 00:04:01 -08002441 if (ret == 0 && Debug > 3) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002442 printf
2443 ("%s: %d DEBUG4 %s/%d: ftruncated to offset %d, %d bytes from end\n",
2444 Progname, Pid, __FILE__, __LINE__, new_offset, trunc_incr);
plars865695b2001-08-27 22:15:12 +00002445 }
2446#endif
2447
2448 lkfile(fd, LOCK_UN, LKLVL0);
2449
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002450 if (ret == -1) {
plars865695b2001-08-27 22:15:12 +00002451#ifdef CRAY
2452 fprintf(stderr, "%s%s: %d %s/%d: trunc failed: %s\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08002453 Progname, TagName, Pid, __FILE__, __LINE__,
2454 strerror(errno));
plars865695b2001-08-27 22:15:12 +00002455#else
2456 fprintf(stderr, "%s%s: %d %s/%d: ftruncate failed: %s\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +08002457 Progname, TagName, Pid, __FILE__, __LINE__,
2458 strerror(errno));
plars865695b2001-08-27 22:15:12 +00002459#endif
2460 return -1;
2461 }
2462
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002463 if (Debug > 2) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002464 printf
2465 ("%s: %d DEBUG2 %s/%d: trunc file by %d bytes, to size of = %d bytes\n",
2466 Progname, Pid, __FILE__, __LINE__, cur_offset - new_offset,
2467 new_offset);
plars865695b2001-08-27 22:15:12 +00002468 }
subrata_modakbdbaec52009-02-26 12:14:51 +00002469
Wanlong Gao354ebb42012-12-07 10:10:04 +08002470 bytes_consumed -= (cur_offset - new_offset);
plars865695b2001-08-27 22:15:12 +00002471 return 0;
2472
Wanlong Gao354ebb42012-12-07 10:10:04 +08002473} /* end of shrinkfile */
plars865695b2001-08-27 22:15:12 +00002474
2475/***********************************************************************
2476 *
2477 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08002478int check_write(int fd, int cf_inter, char *filename, int mode)
plars865695b2001-08-27 22:15:12 +00002479{
Wanlong Gao354ebb42012-12-07 10:10:04 +08002480 int fsize;
2481 static int cf_count = 0;
2482 int ret = 0;
2483 int tmp;
2484 char *errmsg;
2485 char *ptr;
plars865695b2001-08-27 22:15:12 +00002486
2487 cf_count++;
2488
Garrett Cooperdf3eb162010-11-28 22:44:32 -08002489 if (cf_inter == 0 || (cf_count % cf_inter != 0)) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002490 if (Debug > 4)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002491 printf
2492 ("%s: %d DEBUG5 %s/%d: no write check, not time iter=%d, cnt=%d\n",
2493 Progname, Pid, __FILE__, __LINE__, cf_inter,
2494 cf_count);
2495 return 0; /* no check done */
2496 }
2497
2498 if (Grow_incr <= 0) {
2499 if (Debug > 3)
2500 printf("%s: %d DEBUG4 %s/%d: No write validation, "
2501 "Grow_incr = %d, offset = %ld\n",
2502 Progname, Pid, __FILE__, __LINE__, Grow_incr,
2503 (long)Woffset);
2504 return 0; /* no check */
2505 }
2506
2507 /*
2508 * Get the shared file lock. We need to hold the lock from before
2509 * we do the stat until after the read.
2510 */
2511 lkfile(fd, LOCK_SH, LKLVL0);
2512
2513 if ((fsize = file_size(fd)) == -1) {
2514 lkfile(fd, LOCK_UN, LKLVL0);
2515 return -1;
2516
2517 } else if (fsize <= Woffset) {
2518 /*
2519 * The file was truncated between write and now.
2520 * The contents of our last write is totally gone, no check.
2521 */
2522 if (Debug > 1)
2523 printf
2524 ("%s%s: %d DEBUG2 %s/%d: %d File size (%d) smaller than "
2525 "where last wrote (%ld)- no write validation\n",
2526 Progname, TagName, Pid, __FILE__, __LINE__,
2527 Iter_cnt, fsize, (long)Woffset);
2528 lkfile(fd, LOCK_UN, LKLVL0);
2529 return 0; /* no validation, but not an error */
2530
2531 } else if (fsize < (Woffset + Grow_incr)) {
2532 /*
2533 * The file was truncated between write and now.
2534 * Part of our last write has been truncated, adjust our Grow_incr
2535 * to reflect this.
2536 */
2537
2538 tmp = Grow_incr;
2539 Grow_incr = fsize - Woffset;
2540
2541 if (Debug > 1) {
2542
2543 printf("%s%s: %d DEBUG2 %s/%d: %d fsz:%d, lost(%d)of "
2544 "wrt(off:%ld, sz:%d), adj=%d\n", Progname,
2545 TagName, Pid, __FILE__, __LINE__, Iter_cnt,
2546 fsize, tmp - Grow_incr, (long)Woffset, tmp,
2547 Grow_incr);
2548 }
2549
2550 }
2551
2552 if (Debug > 2)
2553 printf("%s: %d DEBUG3 %s/%d: about to do write validation, "
2554 "offset = %ld, size = %d\n",
2555 Progname, Pid, __FILE__, __LINE__, (long)Woffset,
2556 Grow_incr);
2557
2558 if (!(mode & MODE_FIFO)) {
2559
2560 if (lseek(fd, Woffset, 0) == -1) {
2561 fprintf(stderr,
2562 "%s%s: %d %s/%d: lseek(fd, %ld, 0) failed: %s\n",
2563 Progname, TagName, Pid, __FILE__, __LINE__,
2564 (long)Woffset, strerror(errno));
2565 }
2566 if (Debug > 3)
2567 printf("%s: %d DEBUG4 %s/%d: lseeked to offset:%ld\n",
2568 Progname, Pid, __FILE__, __LINE__,
2569 (long)Woffset);
2570 }
2571
2572 /*
2573 * Read last writes data
2574 */
2575#if NEWIO
2576 ret =
2577 lio_read_buffer(fd, io_type, Buffer, Grow_incr, SIGUSR1, &errmsg,
2578 0);
2579#else
2580 ret = read_buffer(fd, io_type, Buffer, Grow_incr, 0, &errmsg);
2581#endif
2582
2583 /*
2584 * report the error and debug information before releasing
2585 * the file lock
2586 */
2587 if (ret != Grow_incr) {
2588 fprintf(stderr, "%s%s: %d %s/%d: %d CW %s\n", Progname, TagName,
2589 Pid, __FILE__, __LINE__, Iter_cnt, errmsg);
2590 {
2591 struct stat stbuf;
2592 fstat(fd, &stbuf);
2593 if (Debug > 2)
2594 printf("%s%s: %d DEBUG3 %s/%d: fd:%d, offset:%d, fsize:%d, openflags:%#o\n", Progname, TagName, Pid, __FILE__, __LINE__, fd, (int)lseek(fd, SEEK_CUR, 0), /* FIXME: 64bit/LFS ? */
2595 (int)stbuf.st_size, Fileinfo.openflags);
2596 }
2597
2598 lkfile(fd, LOCK_UN, LKLVL0);
2599 return 1;
2600 }
2601
2602 lkfile(fd, LOCK_UN, LKLVL0);
2603
2604 if (Mode & MODE_GROW_BY_LSEEK) {
2605 /* check that all zeros upto last character */
2606 for (ptr = Buffer; ptr < (Buffer + Grow_incr - 1); ptr++) {
2607 if (*ptr != '\0') {
2608 fprintf(stderr,
2609 "%s%s: %d %s/%d: data mismatch at offset %d, exp:%#o(zerofilled), act:%#o in file %s\n",
2610 Progname, TagName, Pid, __FILE__,
2611 __LINE__,
2612 (int)(Woffset +
2613 (Grow_incr - (Buffer - ptr))), 0,
2614 *ptr, filename);
2615 fflush(stderr);
2616 return 1;
2617 }
2618 }
2619 /* check that the last char is a 'w' */
2620 if (*ptr != 'w') {
2621 fprintf(stderr,
2622 "%s%s: %d %s/%d: data mismatch at offset %d, exp:%#o(zerofilled), act:%#o in file %s\n",
2623 Progname, TagName, Pid, __FILE__, __LINE__,
2624 (int)(Woffset + (Grow_incr - (Buffer - ptr))),
2625 'w', *ptr, filename);
2626 fflush(stderr);
2627 return 1;
2628 }
2629 return 0; /* all is well */
2630
2631 } else if (Pattern == PATTERN_OFFSET)
2632 ret =
2633 datapidchk(STATIC_NUM, Buffer, Grow_incr, Woffset, &errmsg);
2634 else if (Pattern == PATTERN_PID)
2635 ret = datapidchk(Pid, Buffer, Grow_incr, Woffset, &errmsg);
2636 else if (Pattern == PATTERN_ASCII)
2637 ret = dataasciichk(NULL, Buffer, Grow_incr, Woffset, &errmsg);
2638 else if (Pattern == PATTERN_RANDOM) ; /* no check for random */
2639 else if (Pattern == PATTERN_ALT)
2640 ret = databinchk('a', Buffer, Grow_incr, Woffset, &errmsg);
2641 else if (Pattern == PATTERN_CHKER)
2642 ret = databinchk('c', Buffer, Grow_incr, Woffset, &errmsg);
2643 else if (Pattern == PATTERN_CNTING)
2644 ret = databinchk('C', Buffer, Grow_incr, Woffset, &errmsg);
2645 else if (Pattern == PATTERN_ZEROS)
2646 ret = databinchk('z', Buffer, Grow_incr, Woffset, &errmsg);
2647 else if (Pattern == PATTERN_ONES)
2648 ret = databinchk('o', Buffer, Grow_incr, Woffset, &errmsg);
2649 else
2650 ret = dataasciichk(NULL, Buffer, Grow_incr, Woffset, &errmsg);
2651
2652 if (ret >= 0) {
2653 fprintf(stderr, "%s%s: %d %s/%d: %d CW %s in file %s\n",
2654 Progname, TagName, Pid, __FILE__, __LINE__, Iter_cnt,
2655 errmsg, filename);
2656
2657 if (Debug > 0)
2658 printf("%s%s: %d DEBUG1 %s/%d: **fd:%d, lk:%d, "
2659 "offset:%ld, sz:%d open flags:%#o %s\n",
2660 Progname, TagName, Pid, __FILE__, __LINE__, fd,
2661 lockfile, (long)Woffset, Grow_incr,
2662 Fileinfo.openflags,
2663 openflags2symbols(Fileinfo.openflags, ",", 0));
2664
2665 fflush(stderr);
2666 return 1;
2667 }
2668
2669 if (Debug > 6)
2670 printf("%s: %d DEBUG7 %s/%d: No corruption detected on "
2671 "write validation , offset = %ld, size = %d\n",
2672 Progname, Pid, __FILE__, __LINE__, (long)Woffset,
2673 Grow_incr);
2674
2675 return 0; /* all is well */
2676}
2677
2678/***********************************************************************
2679 *
2680 ***********************************************************************/
2681int check_file(int fd, int cf_inter, char *filename, int no_file_check)
2682{
2683 int fsize;
2684 static int cf_count = 0;
2685 char *buf;
2686 int ret;
2687 int ret_val = 0;
2688 int rd_cnt;
2689 int rd_size;
2690 char *errmsg;
2691
2692 cf_count++;
2693
2694 if (cf_inter == 0 || (cf_count % cf_inter != 0)) {
2695 if (Debug > 4)
2696 printf
2697 ("%s: %d DEBUG5 %s/%d: No file check - not time, iter=%d, cnt=%d\n",
2698 Progname, Pid, __FILE__, __LINE__, cf_inter,
2699 cf_count);
2700 return 0; /* no check done */
plars865695b2001-08-27 22:15:12 +00002701 }
2702
2703 /*
2704 * if we can't determine file content, don't bother checking
2705 */
Wanlong Gao354ebb42012-12-07 10:10:04 +08002706 if (no_file_check) {
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002707 if (Debug > 4)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002708 printf
2709 ("%s: %d DEBUG5 %s/%d: No file check, lseek grow or random lseeks\n",
2710 Progname, Pid, __FILE__, __LINE__);
plars865695b2001-08-27 22:15:12 +00002711 return 0;
2712 }
2713
2714 /*
2715 * Lock the file. We need to have the file lock before
2716 * the stat and until after the last read to prevent
2717 * a trunc/truncate from "corrupting" our data.
2718 */
2719 lkfile(fd, LOCK_SH, LKLVL0);
subrata_modakbdbaec52009-02-26 12:14:51 +00002720
Wanlong Gao354ebb42012-12-07 10:10:04 +08002721 if ((fsize = file_size(fd)) == -1) {
2722 lkfile(fd, LOCK_UN, LKLVL0);
2723 return -1;
plars865695b2001-08-27 22:15:12 +00002724 }
2725
Wanlong Gao354ebb42012-12-07 10:10:04 +08002726 if (fsize == 0) {
2727 if (Debug > 2)
2728 printf
2729 ("%s: %d DEBUG3 %s/%d: No file validation, file size == 0\n",
2730 Progname, Pid, __FILE__, __LINE__);
plars865695b2001-08-27 22:15:12 +00002731
Wanlong Gao354ebb42012-12-07 10:10:04 +08002732 lkfile(fd, LOCK_UN, LKLVL0);
2733 return 0;
plars865695b2001-08-27 22:15:12 +00002734 }
2735
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -08002736 if (Debug > 2)
Wanlong Gao354ebb42012-12-07 10:10:04 +08002737 printf("%s: %d DEBUG3 %s/%d: about to do file validation\n",
2738 Progname, Pid, __FILE__, __LINE__);
plars865695b2001-08-27 22:15:12 +00002739
Wanlong Gao354ebb42012-12-07 10:10:04 +08002740 if (fsize > MAX_FC_READ) {
2741 /*
2742 * read the file in MAX_FC_READ chuncks.
2743 */
plars865695b2001-08-27 22:15:12 +00002744
Cyril Hrubisd218f342014-09-23 13:14:56 +02002745 if ((buf = malloc(MAX_FC_READ)) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002746 fprintf(stderr, "%s%s: %s/%d: malloc(%d) failed: %s\n",
2747 Progname, TagName, __FILE__, __LINE__,
2748 MAX_FC_READ, strerror(errno));
2749 lkfile(fd, LOCK_UN, LKLVL0);
2750 return -1;
2751 }
plars865695b2001-08-27 22:15:12 +00002752
Wanlong Gao354ebb42012-12-07 10:10:04 +08002753 lseek(fd, 0, SEEK_SET);
plars865695b2001-08-27 22:15:12 +00002754
Wanlong Gao354ebb42012-12-07 10:10:04 +08002755 lkfile(fd, LOCK_SH, LKLVL0); /* get lock on file before getting file size */
plars865695b2001-08-27 22:15:12 +00002756
Wanlong Gao354ebb42012-12-07 10:10:04 +08002757 rd_cnt = 0;
2758 while (rd_cnt < fsize) {
2759 if (fsize - rd_cnt > MAX_FC_READ)
2760 rd_size = MAX_FC_READ;
2761 else
2762 rd_size = fsize - rd_cnt;
Garrett Cooper2c282152010-12-16 00:55:50 -08002763
plars865695b2001-08-27 22:15:12 +00002764#if NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +08002765 ret = lio_read_buffer(fd, io_type, buf, rd_size,
2766 SIGUSR1, &errmsg, 0);
plars865695b2001-08-27 22:15:12 +00002767#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08002768 ret =
2769 read_buffer(fd, io_type, buf, rd_size, 0, &errmsg);
plars865695b2001-08-27 22:15:12 +00002770#endif
2771
Wanlong Gao354ebb42012-12-07 10:10:04 +08002772 if (ret != rd_size) {
2773 fprintf(stderr, "%s%s: %d %s/%d: %d CFa %s\n",
2774 Progname, TagName, Pid, __FILE__,
2775 __LINE__, Iter_cnt, errmsg);
2776 free(buf);
2777 lkfile(fd, LOCK_UN, LKLVL0);
2778 return -1;
2779 }
plars865695b2001-08-27 22:15:12 +00002780/**
2781 read(fd, buf, rd_size);
2782***/
2783
Wanlong Gao354ebb42012-12-07 10:10:04 +08002784 if (Pattern == PATTERN_OFFSET)
2785 ret =
2786 datapidchk(STATIC_NUM, buf, rd_size, rd_cnt,
2787 &errmsg);
2788 else if (Pattern == PATTERN_PID)
2789 ret =
2790 datapidchk(Pid, buf, rd_size, rd_cnt,
2791 &errmsg);
2792 else if (Pattern == PATTERN_ASCII)
2793 ret =
2794 dataasciichk(NULL, buf, rd_size, rd_cnt,
2795 &errmsg);
2796 else if (Pattern == PATTERN_RANDOM) ; /* no checks for random */
2797 else if (Pattern == PATTERN_ALT)
2798 ret =
2799 databinchk('a', buf, rd_size, rd_cnt,
2800 &errmsg);
2801 else if (Pattern == PATTERN_CHKER)
2802 ret =
2803 databinchk('c', buf, rd_size, rd_cnt,
2804 &errmsg);
2805 else if (Pattern == PATTERN_CNTING)
2806 ret =
2807 databinchk('C', buf, rd_size, rd_cnt,
2808 &errmsg);
2809 else if (Pattern == PATTERN_ZEROS)
2810 ret =
2811 databinchk('z', buf, rd_size, rd_cnt,
2812 &errmsg);
2813 else if (Pattern == PATTERN_ONES)
2814 ret =
2815 databinchk('o', buf, rd_size, rd_cnt,
2816 &errmsg);
2817 else
2818 ret =
2819 dataasciichk(NULL, buf, rd_size, rd_cnt,
2820 &errmsg);
Garrett Cooper2c282152010-12-16 00:55:50 -08002821
Wanlong Gao354ebb42012-12-07 10:10:04 +08002822 if (ret >= 0) {
2823 fprintf(stderr,
2824 "%s%s: %d %s/%d: %d CFp %s in file %s\n",
2825 Progname, TagName, Pid, __FILE__,
2826 __LINE__, Iter_cnt, errmsg, filename);
2827 fflush(stderr);
2828 ret_val = 1;
2829 lkfile(fd, LOCK_UN, LKLVL0);
2830 break;
2831 }
2832 rd_cnt += rd_size;
2833 }
plars865695b2001-08-27 22:15:12 +00002834
Wanlong Gao354ebb42012-12-07 10:10:04 +08002835 lkfile(fd, LOCK_UN, LKLVL0);
plars865695b2001-08-27 22:15:12 +00002836
Wanlong Gao354ebb42012-12-07 10:10:04 +08002837 free(buf);
plars865695b2001-08-27 22:15:12 +00002838
Wanlong Gao354ebb42012-12-07 10:10:04 +08002839 } else {
2840 /*
2841 * Read the whole file in a single read
2842 */
Cyril Hrubisd218f342014-09-23 13:14:56 +02002843 if ((buf = malloc(fsize)) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +08002844 fprintf(stderr, "%s%s: %s/%d: malloc(%d) failed: %s\n",
2845 Progname, TagName, __FILE__, __LINE__, fsize,
2846 strerror(errno));
plars865695b2001-08-27 22:15:12 +00002847 fflush(stderr);
2848 return -1;
Wanlong Gao354ebb42012-12-07 10:10:04 +08002849 }
plars865695b2001-08-27 22:15:12 +00002850
Wanlong Gao354ebb42012-12-07 10:10:04 +08002851 lseek(fd, 0, SEEK_SET);
plars865695b2001-08-27 22:15:12 +00002852
2853/****
2854 read(fd, buf, fsize);
2855****/
2856#if NEWIO
Wanlong Gao354ebb42012-12-07 10:10:04 +08002857 ret =
2858 lio_read_buffer(fd, io_type, buf, fsize, SIGUSR1, &errmsg,
2859 0);
plars865695b2001-08-27 22:15:12 +00002860#else
Wanlong Gao354ebb42012-12-07 10:10:04 +08002861 ret = read_buffer(fd, io_type, buf, fsize, 0, &errmsg);
plars865695b2001-08-27 22:15:12 +00002862#endif
2863
Wanlong Gao354ebb42012-12-07 10:10:04 +08002864 /* unlock the file as soon as we can */
2865 lkfile(fd, LOCK_UN, LKLVL0);
plars865695b2001-08-27 22:15:12 +00002866
Wanlong Gao354ebb42012-12-07 10:10:04 +08002867 if (ret != fsize) {
2868 fprintf(stderr, "%s%s: %d %s/%d: %d CFw %s\n",
2869 Progname, TagName, Pid, __FILE__, __LINE__,
2870 Iter_cnt, errmsg);
2871 ret_val = 1;
2872 } else {
2873 if (Pattern == PATTERN_OFFSET)
2874 ret =
2875 datapidchk(STATIC_NUM, buf, fsize, 0,
2876 &errmsg);
2877 else if (Pattern == PATTERN_PID)
2878 ret = datapidchk(Pid, buf, fsize, 0, &errmsg);
2879 else if (Pattern == PATTERN_ASCII)
2880 ret =
2881 dataasciichk(NULL, buf, fsize, 0, &errmsg);
2882 else if (Pattern == PATTERN_RANDOM) ; /* no check for random */
2883 else if (Pattern == PATTERN_ALT)
2884 ret = databinchk('a', buf, fsize, 0, &errmsg);
2885 else if (Pattern == PATTERN_CHKER)
2886 ret = databinchk('c', buf, fsize, 0, &errmsg);
2887 else if (Pattern == PATTERN_CNTING)
2888 ret = databinchk('C', buf, fsize, 0, &errmsg);
2889 else if (Pattern == PATTERN_ZEROS)
2890 ret = databinchk('z', buf, fsize, 0, &errmsg);
2891 else if (Pattern == PATTERN_ONES)
2892 ret = databinchk('o', buf, fsize, 0, &errmsg);
2893 else
2894 ret =
2895 dataasciichk(NULL, buf, fsize, 0, &errmsg);
plars865695b2001-08-27 22:15:12 +00002896
Wanlong Gao354ebb42012-12-07 10:10:04 +08002897 if (ret >= 0) {
2898 fprintf(stderr,
2899 "%s%s: %d %s/%d: %d CFw %s in file %s\n",
2900 Progname, TagName, Pid, __FILE__,
2901 __LINE__, Iter_cnt, errmsg, filename);
2902 fflush(stderr);
2903 ret_val = 1;
2904 }
plars865695b2001-08-27 22:15:12 +00002905 }
Wanlong Gao354ebb42012-12-07 10:10:04 +08002906 free(buf);
plars865695b2001-08-27 22:15:12 +00002907 }
2908
2909 return ret_val;
2910
Wanlong Gao354ebb42012-12-07 10:10:04 +08002911} /* end of check_file */
plars865695b2001-08-27 22:15:12 +00002912
2913/***********************************************************************
2914 *
2915 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08002916int file_size(int fd)
plars865695b2001-08-27 22:15:12 +00002917{
Wanlong Gao354ebb42012-12-07 10:10:04 +08002918 struct stat sb;
plars865695b2001-08-27 22:15:12 +00002919
Wanlong Gao354ebb42012-12-07 10:10:04 +08002920 if (fstat(fd, &sb) < 0) {
2921 fprintf(stderr,
2922 "%s%s: %d %s/%d: Unable to fstat(%d, &buf), errno:%d %s\n",
2923 Progname, TagName, Pid, __FILE__, __LINE__, fd, errno,
2924 strerror(errno));
2925 return -1;
subrata_modakbdbaec52009-02-26 12:14:51 +00002926
Wanlong Gao354ebb42012-12-07 10:10:04 +08002927 }
plars865695b2001-08-27 22:15:12 +00002928
Wanlong Gao354ebb42012-12-07 10:10:04 +08002929 return sb.st_size;
plars865695b2001-08-27 22:15:12 +00002930}
2931
2932/***********************************************************************
2933 * do file lock/unlock action.
2934 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08002935int lkfile(int fd, int operation, int lklevel)
plars865695b2001-08-27 22:15:12 +00002936{
Wanlong Gao354ebb42012-12-07 10:10:04 +08002937 char *errmsg;
plars865695b2001-08-27 22:15:12 +00002938
Wanlong Gao354ebb42012-12-07 10:10:04 +08002939 if (lockfile == lklevel) {
subrata_modakbdbaec52009-02-26 12:14:51 +00002940
Wanlong Gao354ebb42012-12-07 10:10:04 +08002941 if (Debug > 5) {
2942 switch (operation) {
2943 case LOCK_UN:
2944 printf
2945 ("%s: %d DEBUG6 %s/%d: Attempting to release lock on fd %d\n",
2946 Progname, Pid, __FILE__, __LINE__, fd);
2947 break;
plars865695b2001-08-27 22:15:12 +00002948
Wanlong Gao354ebb42012-12-07 10:10:04 +08002949 case LOCK_SH:
2950 printf
2951 ("%s: %d DEBUG6 %s/%d: Attempting to get read/shared lock on fd %d\n",
2952 Progname, Pid, __FILE__, __LINE__, fd);
2953 break;
plars865695b2001-08-27 22:15:12 +00002954
Wanlong Gao354ebb42012-12-07 10:10:04 +08002955 case LOCK_EX:
2956 printf
2957 ("%s: %d DEBUG6 %s/%d: Attempting to get write/exclusive lock on fd %d\n",
2958 Progname, Pid, __FILE__, __LINE__, fd);
2959 break;
2960 }
2961 }
2962
2963 /*
2964 * Attempt to get/release desired lock.
2965 * file_lock will attempt to do action over and over again until
2966 * either an unretryable error or the action is completed.
2967 */
2968
2969 if (file_lock(fd, operation, &errmsg) != 0) {
2970 printf
2971 ("%s%s: %d %s/%d: Unable to perform lock operation. %s\n",
2972 Progname, TagName, Pid, __FILE__, __LINE__,
2973 errmsg);
2974
2975 /* do we count this as an error? handle_error(); */
2976 return -1;
2977 }
2978
2979 if (Debug > 2) {
2980 switch (operation) {
2981 case LOCK_UN:
2982 printf
2983 ("%s: %d DEBUG3 %s/%d: Released lock on fd %d\n",
2984 Progname, Pid, __FILE__, __LINE__, fd);
2985 break;
2986
2987 case LOCK_SH:
2988 printf
2989 ("%s: %d DEBUG3 %s/%d: Got read/shared lock on fd %d\n",
2990 Progname, Pid, __FILE__, __LINE__, fd);
2991 break;
2992
2993 case LOCK_EX:
2994 printf
2995 ("%s: %d DEBUG3 %s/%d: Got write/exclusive lock on fd %d\n",
2996 Progname, Pid, __FILE__, __LINE__, fd);
2997 break;
2998
2999 default:
3000 printf
3001 ("%s: %d DEBUG3 %s/%d: Completed action %d on fd %d\n",
3002 Progname, Pid, __FILE__, __LINE__,
3003 operation, fd);
3004 break;
3005 }
3006 }
plars865695b2001-08-27 22:15:12 +00003007 }
3008
Wanlong Gao354ebb42012-12-07 10:10:04 +08003009 return 0;
plars865695b2001-08-27 22:15:12 +00003010}
3011
3012#ifndef linux
3013/***********************************************************************
3014 *
3015 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +08003016int pre_alloc(int fd, long size)
plars865695b2001-08-27 22:15:12 +00003017{
3018
3019#ifdef CRAY
Wanlong Gao354ebb42012-12-07 10:10:04 +08003020 long avl;
plars865695b2001-08-27 22:15:12 +00003021
Wanlong Gao354ebb42012-12-07 10:10:04 +08003022 if (ialloc(fd, size, IA_CONT, &avl) == -1) {
3023 fprintf(stderr,
3024 "%s%s %s/%d: Unable to pre-alloc space: ialloc failed: %d %s\n",
3025 Progname, TagName, __FILE__, __LINE__, errno,
3026 strerror(errno));
3027 return -1;
3028 }
plars865695b2001-08-27 22:15:12 +00003029#endif
3030
3031#ifdef sgi
Wanlong Gao354ebb42012-12-07 10:10:04 +08003032 struct flock f;
plars865695b2001-08-27 22:15:12 +00003033
3034 f.l_whence = 0;
3035 f.l_start = 0;
3036 f.l_len = size;
3037
3038 /* non-zeroing reservation */
Wanlong Gao354ebb42012-12-07 10:10:04 +08003039 if (fcntl(fd, F_RESVSP, &f) == -1) {
3040 fprintf(stderr,
3041 "%s%s %s/%d: Unable to pre-alloc space: fcntl(F_RESVSP) failed: %d %s\n",
3042 Progname, TagName, __FILE__, __LINE__, errno,
3043 strerror(errno));
plars865695b2001-08-27 22:15:12 +00003044 return -1;
3045 }
plars865695b2001-08-27 22:15:12 +00003046#endif
3047
3048 return 0;
3049}
Alex Elder97360e72011-04-01 17:01:26 -05003050#endif