fsx: Improve operation logging
[xfstests-dev.git] / ltp / fsx.c
1 /*
2  *      Copyright (C) 1991, NeXT Computer, Inc.  All Rights Reserverd.
3  *
4  *      File:   fsx.c
5  *      Author: Avadis Tevanian, Jr.
6  *
7  *      File system exerciser. 
8  *
9  *      Rewritten 8/98 by Conrad Minshall.
10  *
11  *      Small changes to work under Linux -- davej.
12  *
13  *      Checks for mmap last-page zero fill.
14  */
15
16 #include "global.h"
17
18 #include <limits.h>
19 #include <time.h>
20 #include <strings.h>
21 #include <sys/file.h>
22 #include <sys/mman.h>
23 #ifdef HAVE_ERR_H
24 #include <err.h>
25 #endif
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <errno.h>
33 #ifdef AIO
34 #include <libaio.h>
35 #endif
36
37 #ifndef MAP_FILE
38 # define MAP_FILE 0
39 #endif
40
41 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
42
43 /* Operation flags */
44
45 enum opflags { FL_NONE = 0, FL_SKIPPED = 1, FL_CLOSE_OPEN = 2, FL_KEEP_SIZE = 4 };
46
47 /*
48  *      A log entry is an operation and a bunch of arguments.
49  */
50
51 struct log_entry {
52         int     operation;
53         int     args[3];
54         enum opflags flags;
55 };
56
57 #define LOGSIZE 10000
58
59 struct log_entry        oplog[LOGSIZE]; /* the log */
60 int                     logptr = 0;     /* current position in log */
61 int                     logcount = 0;   /* total ops */
62
63 /*
64  * The operation matrix is complex due to conditional execution of different
65  * features. Hence when we come to deciding what operation to run, we need to
66  * be careful in how we select the different operations. The active operations
67  * are mapped to numbers as follows:
68  *
69  *              lite    !lite
70  * READ:        0       0
71  * WRITE:       1       1
72  * MAPREAD:     2       2
73  * MAPWRITE:    3       3
74  * TRUNCATE:    -       4
75  * FALLOCATE:   -       5
76  * PUNCH HOLE:  -       6
77  * ZERO RANGE:  -       7
78  *
79  * When mapped read/writes are disabled, they are simply converted to normal
80  * reads and writes. When fallocate/fpunch calls are disabled, they are
81  * skipped.
82  *
83  * Because of the "lite" version, we also need to have different "maximum
84  * operation" defines to allow the ops to be selected correctly based on the
85  * mode being run.
86  */
87
88 /* common operations */
89 #define OP_READ         0
90 #define OP_WRITE        1
91 #define OP_MAPREAD      2
92 #define OP_MAPWRITE     3
93 #define OP_MAX_LITE     4
94
95 /* !lite operations */
96 #define OP_TRUNCATE             4
97 #define OP_FALLOCATE            5
98 #define OP_PUNCH_HOLE           6
99 #define OP_ZERO_RANGE           7
100 #define OP_COLLAPSE_RANGE       8
101 #define OP_INSERT_RANGE 9
102 #define OP_MAX_FULL             10
103
104 #undef PAGE_SIZE
105 #define PAGE_SIZE       getpagesize()
106 #undef PAGE_MASK
107 #define PAGE_MASK       (PAGE_SIZE - 1)
108
109 char    *original_buf;                  /* a pointer to the original data */
110 char    *good_buf;                      /* a pointer to the correct data */
111 char    *temp_buf;                      /* a pointer to the current data */
112 char    *fname;                         /* name of our test file */
113 int     fd;                             /* fd for our test file */
114
115 blksize_t       block_size = 0;
116 off_t           file_size = 0;
117 off_t           biggest = 0;
118 char            state[256];
119 unsigned long   testcalls = 0;          /* calls to function "test" */
120
121 unsigned long   simulatedopcount = 0;   /* -b flag */
122 int     closeprob = 0;                  /* -c flag */
123 int     debug = 0;                      /* -d flag */
124 unsigned long   debugstart = 0;         /* -D flag */
125 int     flush = 0;                      /* -f flag */
126 int     do_fsync = 0;                   /* -y flag */
127 unsigned long   maxfilelen = 256 * 1024;        /* -l flag */
128 int     sizechecks = 1;                 /* -n flag disables them */
129 int     maxoplen = 64 * 1024;           /* -o flag */
130 int     quiet = 0;                      /* -q flag */
131 unsigned long progressinterval = 0;     /* -p flag */
132 int     readbdy = 1;                    /* -r flag */
133 int     style = 0;                      /* -s flag */
134 int     prealloc = 0;                   /* -x flag */
135 int     truncbdy = 1;                   /* -t flag */
136 int     writebdy = 1;                   /* -w flag */
137 long    monitorstart = -1;              /* -m flag */
138 long    monitorend = -1;                /* -m flag */
139 int     lite = 0;                       /* -L flag */
140 long    numops = -1;                    /* -N flag */
141 int     randomoplen = 1;                /* -O flag disables it */
142 int     seed = 1;                       /* -S flag */
143 int     mapped_writes = 1;              /* -W flag disables */
144 int     fallocate_calls = 1;            /* -F flag disables */
145 int     keep_size_calls = 1;            /* -K flag disables */
146 int     punch_hole_calls = 1;           /* -H flag disables */
147 int     zero_range_calls = 1;           /* -z flag disables */
148 int     collapse_range_calls = 1;       /* -C flag disables */
149 int     insert_range_calls = 1;         /* -I flag disables */
150 int     mapped_reads = 1;               /* -R flag disables it */
151 int     fsxgoodfd = 0;
152 int     o_direct;                       /* -Z */
153 int     aio = 0;
154
155 int page_size;
156 int page_mask;
157 int mmap_mask;
158 #ifdef AIO
159 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset);
160 #define READ 0
161 #define WRITE 1
162 #define fsxread(a,b,c,d)        aio_rw(READ, a,b,c,d)
163 #define fsxwrite(a,b,c,d)       aio_rw(WRITE, a,b,c,d)
164 #else
165 #define fsxread(a,b,c,d)        read(a,b,c)
166 #define fsxwrite(a,b,c,d)       write(a,b,c)
167 #endif
168
169 FILE *  fsxlogf = NULL;
170 int badoff = -1;
171 int closeopen = 0;
172
173 static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
174 {
175         unsigned long ret = (unsigned long)ptr;
176
177         ret = ((ret + align - 1) & ~(align - 1));
178         ret += offset;
179         return (void *)ret;
180 }
181
182 void
183 vwarnc(int code, const char *fmt, va_list ap) {
184   fprintf(stderr, "fsx: ");
185   if (fmt != NULL) {
186         vfprintf(stderr, fmt, ap);
187         fprintf(stderr, ": ");
188   }
189   fprintf(stderr, "%s\n", strerror(code));
190 }
191
192 void
193 warn(const char * fmt, ...)  {
194         va_list ap;
195         va_start(ap, fmt);
196         vwarnc(errno, fmt, ap);
197         va_end(ap);
198 }
199
200 #define BUF_SIZE 1024
201
202 void
203 prt(char *fmt, ...)
204 {
205         va_list args;
206         char buffer[BUF_SIZE];
207
208         va_start(args, fmt);
209         vsnprintf(buffer, BUF_SIZE, fmt, args);
210         va_end(args);
211         fprintf(stdout, buffer);
212         if (fsxlogf)
213                 fprintf(fsxlogf, buffer);
214 }
215
216 void
217 prterr(char *prefix)
218 {
219         prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
220 }
221
222
223 void
224 log4(int operation, int arg0, int arg1, enum opflags flags)
225 {
226         struct log_entry *le;
227
228         le = &oplog[logptr];
229         le->operation = operation;
230         if (closeopen)
231                 flags |= FL_CLOSE_OPEN;
232         le->args[0] = arg0;
233         le->args[1] = arg1;
234         le->args[2] = file_size;
235         le->flags = flags;
236         logptr++;
237         logcount++;
238         if (logptr >= LOGSIZE)
239                 logptr = 0;
240 }
241
242
243 void
244 logdump(void)
245 {
246         int     i, count, down;
247         struct log_entry        *lp;
248
249         prt("LOG DUMP (%d total operations):\n", logcount);
250         if (logcount < LOGSIZE) {
251                 i = 0;
252                 count = logcount;
253         } else {
254                 i = logptr;
255                 count = LOGSIZE;
256         }
257         for ( ; count > 0; count--) {
258                 int opnum;
259
260                 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
261                 prt("%d(%3d mod 256): ", opnum, opnum%256);
262                 lp = &oplog[i];
263
264                 if (lp->flags & FL_SKIPPED) {
265                         prt("SKIPPED (no operation)");
266                         goto skipped;
267                 }
268
269                 switch (lp->operation) {
270                 case OP_MAPREAD:
271                         prt("MAPREAD  0x%x thru 0x%x\t(0x%x bytes)",
272                             lp->args[0], lp->args[0] + lp->args[1] - 1,
273                             lp->args[1]);
274                         if (badoff >= lp->args[0] && badoff <
275                                                      lp->args[0] + lp->args[1])
276                                 prt("\t***RRRR***");
277                         break;
278                 case OP_MAPWRITE:
279                         prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
280                             lp->args[0], lp->args[0] + lp->args[1] - 1,
281                             lp->args[1]);
282                         if (badoff >= lp->args[0] && badoff <
283                                                      lp->args[0] + lp->args[1])
284                                 prt("\t******WWWW");
285                         break;
286                 case OP_READ:
287                         prt("READ     0x%x thru 0x%x\t(0x%x bytes)",
288                             lp->args[0], lp->args[0] + lp->args[1] - 1,
289                             lp->args[1]);
290                         if (badoff >= lp->args[0] &&
291                             badoff < lp->args[0] + lp->args[1])
292                                 prt("\t***RRRR***");
293                         break;
294                 case OP_WRITE:
295                         prt("WRITE    0x%x thru 0x%x\t(0x%x bytes)",
296                             lp->args[0], lp->args[0] + lp->args[1] - 1,
297                             lp->args[1]);
298                         if (lp->args[0] > lp->args[2])
299                                 prt(" HOLE");
300                         else if (lp->args[0] + lp->args[1] > lp->args[2])
301                                 prt(" EXTEND");
302                         if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
303                             badoff < lp->args[0] + lp->args[1])
304                                 prt("\t***WWWW");
305                         break;
306                 case OP_TRUNCATE:
307                         down = lp->args[1] < lp->args[2];
308                         prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
309                             down ? "DOWN" : "UP", lp->args[2], lp->args[1]);
310                         if (badoff >= lp->args[1 + !down] &&
311                             badoff < lp->args[1 + !!down])
312                                 prt("\t******WWWW");
313                         break;
314                 case OP_FALLOCATE:
315                         /* 0: offset 1: length 2: where alloced */
316                         prt("FALLOC   0x%x thru 0x%x\t(0x%x bytes) ",
317                                 lp->args[0], lp->args[0] + lp->args[1],
318                                 lp->args[1]);
319                         if (lp->args[0] + lp->args[1] <= lp->args[2])
320                                 prt("INTERIOR");
321                         else if (lp->flags & FL_KEEP_SIZE)
322                                 prt("PAST_EOF");
323                         else
324                                 prt("EXTENDING");
325                         if (badoff >= lp->args[0] &&
326                             badoff < lp->args[0] + lp->args[1])
327                                 prt("\t******FFFF");
328                         break;
329                 case OP_PUNCH_HOLE:
330                         prt("PUNCH    0x%x thru 0x%x\t(0x%x bytes)",
331                             lp->args[0], lp->args[0] + lp->args[1] - 1,
332                             lp->args[1]);
333                         if (badoff >= lp->args[0] && badoff <
334                                                      lp->args[0] + lp->args[1])
335                                 prt("\t******PPPP");
336                         break;
337                 case OP_ZERO_RANGE:
338                         prt("ZERO     0x%x thru 0x%x\t(0x%x bytes)",
339                             lp->args[0], lp->args[0] + lp->args[1] - 1,
340                             lp->args[1]);
341                         if (badoff >= lp->args[0] && badoff <
342                                                      lp->args[0] + lp->args[1])
343                                 prt("\t******ZZZZ");
344                         break;
345                 case OP_COLLAPSE_RANGE:
346                         prt("COLLAPSE 0x%x thru 0x%x\t(0x%x bytes)",
347                             lp->args[0], lp->args[0] + lp->args[1] - 1,
348                             lp->args[1]);
349                         if (badoff >= lp->args[0] && badoff <
350                                                      lp->args[0] + lp->args[1])
351                                 prt("\t******CCCC");
352                         break;
353                 case OP_INSERT_RANGE:
354                         prt("INSERT 0x%x thru 0x%x\t(0x%x bytes)",
355                             lp->args[0], lp->args[0] + lp->args[1] - 1,
356                             lp->args[1]);
357                         if (badoff >= lp->args[0] && badoff <
358                                                      lp->args[0] + lp->args[1])
359                                 prt("\t******IIII");
360                         break;
361                 default:
362                         prt("BOGUS LOG ENTRY (operation code = %d)!",
363                             lp->operation);
364                 }
365             skipped:
366                 if (lp->flags & FL_CLOSE_OPEN)
367                         prt("\n\t\tCLOSE/OPEN");
368                 prt("\n");
369                 i++;
370                 if (i == LOGSIZE)
371                         i = 0;
372         }
373 }
374
375
376 void
377 save_buffer(char *buffer, off_t bufferlength, int fd)
378 {
379         off_t ret;
380         ssize_t byteswritten;
381
382         if (fd <= 0 || bufferlength == 0)
383                 return;
384
385         if (bufferlength > SSIZE_MAX) {
386                 prt("fsx flaw: overflow in save_buffer\n");
387                 exit(67);
388         }
389         if (lite) {
390                 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
391                 if (size_by_seek == (off_t)-1)
392                         prterr("save_buffer: lseek eof");
393                 else if (bufferlength > size_by_seek) {
394                         warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
395                              (unsigned long long)bufferlength);
396                         bufferlength = size_by_seek;
397                 }
398         }
399
400         ret = lseek(fd, (off_t)0, SEEK_SET);
401         if (ret == (off_t)-1)
402                 prterr("save_buffer: lseek 0");
403         
404         byteswritten = write(fd, buffer, (size_t)bufferlength);
405         if (byteswritten != bufferlength) {
406                 if (byteswritten == -1)
407                         prterr("save_buffer write");
408                 else
409                         warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
410                              (unsigned)byteswritten,
411                              (unsigned long long)bufferlength);
412         }
413 }
414
415
416 void
417 report_failure(int status)
418 {
419         logdump();
420         
421         if (fsxgoodfd) {
422                 if (good_buf) {
423                         save_buffer(good_buf, file_size, fsxgoodfd);
424                         prt("Correct content saved for comparison\n");
425                         prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
426                             fname, fname);
427                 }
428                 close(fsxgoodfd);
429         }
430         exit(status);
431 }
432
433
434 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
435                                         *(((unsigned char *)(cp)) + 1)))
436
437 void
438 check_buffers(unsigned offset, unsigned size)
439 {
440         unsigned char c, t;
441         unsigned i = 0;
442         unsigned n = 0;
443         unsigned op = 0;
444         unsigned bad = 0;
445
446         if (memcmp(good_buf + offset, temp_buf, size) != 0) {
447                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
448                     offset, size, fname);
449                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
450                 while (size > 0) {
451                         c = good_buf[offset];
452                         t = temp_buf[i];
453                         if (c != t) {
454                                 if (n < 16) {
455                                         bad = short_at(&temp_buf[i]);
456                                         prt("0x%05x\t0x%04x\t0x%04x", offset,
457                                             short_at(&good_buf[offset]), bad);
458                                         op = temp_buf[offset & 1 ? i+1 : i];
459                                         prt("\t0x%05x\n", n);
460                                         if (op)
461                                                 prt("operation# (mod 256) for "
462                                                   "the bad data may be %u\n",
463                                                 ((unsigned)op & 0xff));
464                                         else
465                                                 prt("operation# (mod 256) for "
466                                                   "the bad data unknown, check"
467                                                   " HOLE and EXTEND ops\n");
468                                 }
469                                 n++;
470                                 badoff = offset;
471                         }
472                         offset++;
473                         i++;
474                         size--;
475                 }
476                 report_failure(110);
477         }
478 }
479
480
481 void
482 check_size(void)
483 {
484         struct stat     statbuf;
485         off_t   size_by_seek;
486
487         if (fstat(fd, &statbuf)) {
488                 prterr("check_size: fstat");
489                 statbuf.st_size = -1;
490         }
491         size_by_seek = lseek(fd, (off_t)0, SEEK_END);
492         if (file_size != statbuf.st_size || file_size != size_by_seek) {
493                 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
494                     (unsigned long long)file_size,
495                     (unsigned long long)statbuf.st_size,
496                     (unsigned long long)size_by_seek);
497                 report_failure(120);
498         }
499 }
500
501
502 void
503 check_trunc_hack(void)
504 {
505         struct stat statbuf;
506
507         ftruncate(fd, (off_t)0);
508         ftruncate(fd, (off_t)100000);
509         fstat(fd, &statbuf);
510         if (statbuf.st_size != (off_t)100000) {
511                 prt("no extend on truncate! not posix!\n");
512                 exit(130);
513         }
514         ftruncate(fd, 0);
515 }
516
517 void
518 doflush(unsigned offset, unsigned size)
519 {
520         unsigned pg_offset;
521         unsigned map_size;
522         char    *p;
523
524         if (o_direct == O_DIRECT)
525                 return;
526
527         pg_offset = offset & mmap_mask;
528         map_size  = pg_offset + size;
529
530         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
531                               MAP_FILE | MAP_SHARED, fd,
532                               (off_t)(offset - pg_offset))) == (char *)-1) {
533                 prterr("doflush: mmap");
534                 report_failure(202);
535         }
536         if (msync(p, map_size, MS_INVALIDATE) != 0) {
537                 prterr("doflush: msync");
538                 report_failure(203);
539         }
540         if (munmap(p, map_size) != 0) {
541                 prterr("doflush: munmap");
542                 report_failure(204);
543         }
544 }
545
546 void
547 doread(unsigned offset, unsigned size)
548 {
549         off_t ret;
550         unsigned iret;
551
552         offset -= offset % readbdy;
553         if (o_direct)
554                 size -= size % readbdy;
555         if (size == 0) {
556                 if (!quiet && testcalls > simulatedopcount && !o_direct)
557                         prt("skipping zero size read\n");
558                 log4(OP_READ, offset, size, FL_SKIPPED);
559                 return;
560         }
561         if (size + offset > file_size) {
562                 if (!quiet && testcalls > simulatedopcount)
563                         prt("skipping seek/read past end of file\n");
564                 log4(OP_READ, offset, size, FL_SKIPPED);
565                 return;
566         }
567
568         log4(OP_READ, offset, size, FL_NONE);
569
570         if (testcalls <= simulatedopcount)
571                 return;
572
573         if (!quiet &&
574                 ((progressinterval && testcalls % progressinterval == 0)  ||
575                 (debug &&
576                        (monitorstart == -1 ||
577                         (offset + size > monitorstart &&
578                         (monitorend == -1 || offset <= monitorend))))))
579                 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
580                     offset, offset + size - 1, size);
581         ret = lseek(fd, (off_t)offset, SEEK_SET);
582         if (ret == (off_t)-1) {
583                 prterr("doread: lseek");
584                 report_failure(140);
585         }
586         iret = fsxread(fd, temp_buf, size, offset);
587         if (iret != size) {
588                 if (iret == -1)
589                         prterr("doread: read");
590                 else
591                         prt("short read: 0x%x bytes instead of 0x%x\n",
592                             iret, size);
593                 report_failure(141);
594         }
595         check_buffers(offset, size);
596 }
597
598
599 void
600 check_eofpage(char *s, unsigned offset, char *p, int size)
601 {
602         unsigned long last_page, should_be_zero;
603
604         if (offset + size <= (file_size & ~page_mask))
605                 return;
606         /*
607          * we landed in the last page of the file
608          * test to make sure the VM system provided 0's 
609          * beyond the true end of the file mapping
610          * (as required by mmap def in 1996 posix 1003.1)
611          */
612         last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
613
614         for (should_be_zero = last_page + (file_size & page_mask);
615              should_be_zero < last_page + page_size;
616              should_be_zero++)
617                 if (*(char *)should_be_zero) {
618                         prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
619                             s, file_size - 1, should_be_zero & page_mask,
620                             short_at(should_be_zero));
621                         report_failure(205);
622                 }
623 }
624
625
626 void
627 domapread(unsigned offset, unsigned size)
628 {
629         unsigned pg_offset;
630         unsigned map_size;
631         char    *p;
632
633         offset -= offset % readbdy;
634         if (size == 0) {
635                 if (!quiet && testcalls > simulatedopcount)
636                         prt("skipping zero size read\n");
637                 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
638                 return;
639         }
640         if (size + offset > file_size) {
641                 if (!quiet && testcalls > simulatedopcount)
642                         prt("skipping seek/read past end of file\n");
643                 log4(OP_MAPREAD, offset, size, FL_SKIPPED);
644                 return;
645         }
646
647         log4(OP_MAPREAD, offset, size, FL_NONE);
648
649         if (testcalls <= simulatedopcount)
650                 return;
651
652         if (!quiet &&
653                 ((progressinterval && testcalls % progressinterval == 0) ||
654                        (debug &&
655                        (monitorstart == -1 ||
656                         (offset + size > monitorstart &&
657                         (monitorend == -1 || offset <= monitorend))))))
658                 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
659                     offset, offset + size - 1, size);
660
661         pg_offset = offset & PAGE_MASK;
662         map_size  = pg_offset + size;
663
664         if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
665                               (off_t)(offset - pg_offset))) == (char *)-1) {
666                 prterr("domapread: mmap");
667                 report_failure(190);
668         }
669         memcpy(temp_buf, p + pg_offset, size);
670
671         check_eofpage("Read", offset, p, size);
672
673         if (munmap(p, map_size) != 0) {
674                 prterr("domapread: munmap");
675                 report_failure(191);
676         }
677
678         check_buffers(offset, size);
679 }
680
681
682 void
683 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
684 {
685         while (size--) {
686                 good_buf[offset] = testcalls % 256; 
687                 if (offset % 2)
688                         good_buf[offset] += original_buf[offset];
689                 offset++;
690         }
691 }
692
693
694 void
695 dowrite(unsigned offset, unsigned size)
696 {
697         off_t ret;
698         unsigned iret;
699
700         offset -= offset % writebdy;
701         if (o_direct)
702                 size -= size % writebdy;
703         if (size == 0) {
704                 if (!quiet && testcalls > simulatedopcount && !o_direct)
705                         prt("skipping zero size write\n");
706                 log4(OP_WRITE, offset, size, FL_SKIPPED);
707                 return;
708         }
709
710         log4(OP_WRITE, offset, size, FL_NONE);
711
712         gendata(original_buf, good_buf, offset, size);
713         if (file_size < offset + size) {
714                 if (file_size < offset)
715                         memset(good_buf + file_size, '\0', offset - file_size);
716                 file_size = offset + size;
717                 if (lite) {
718                         warn("Lite file size bug in fsx!");
719                         report_failure(149);
720                 }
721         }
722
723         if (testcalls <= simulatedopcount)
724                 return;
725
726         if (!quiet &&
727                 ((progressinterval && testcalls % progressinterval == 0) ||
728                        (debug &&
729                        (monitorstart == -1 ||
730                         (offset + size > monitorstart &&
731                         (monitorend == -1 || offset <= monitorend))))))
732                 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
733                     offset, offset + size - 1, size);
734         ret = lseek(fd, (off_t)offset, SEEK_SET);
735         if (ret == (off_t)-1) {
736                 prterr("dowrite: lseek");
737                 report_failure(150);
738         }
739         iret = fsxwrite(fd, good_buf + offset, size, offset);
740         if (iret != size) {
741                 if (iret == -1)
742                         prterr("dowrite: write");
743                 else
744                         prt("short write: 0x%x bytes instead of 0x%x\n",
745                             iret, size);
746                 report_failure(151);
747         }
748         if (do_fsync) {
749                 if (fsync(fd)) {
750                         prt("fsync() failed: %s\n", strerror(errno));
751                         report_failure(152);
752                 }
753         }
754         if (flush) {
755                 doflush(offset, size);
756         }
757 }
758
759
760 void
761 domapwrite(unsigned offset, unsigned size)
762 {
763         unsigned pg_offset;
764         unsigned map_size;
765         off_t    cur_filesize;
766         char    *p;
767
768         offset -= offset % writebdy;
769         if (size == 0) {
770                 if (!quiet && testcalls > simulatedopcount)
771                         prt("skipping zero size write\n");
772                 log4(OP_MAPWRITE, offset, size, FL_SKIPPED);
773                 return;
774         }
775         cur_filesize = file_size;
776
777         log4(OP_MAPWRITE, offset, size, FL_NONE);
778
779         gendata(original_buf, good_buf, offset, size);
780         if (file_size < offset + size) {
781                 if (file_size < offset)
782                         memset(good_buf + file_size, '\0', offset - file_size);
783                 file_size = offset + size;
784                 if (lite) {
785                         warn("Lite file size bug in fsx!");
786                         report_failure(200);
787                 }
788         }
789
790         if (testcalls <= simulatedopcount)
791                 return;
792
793         if (!quiet &&
794                 ((progressinterval && testcalls % progressinterval == 0) ||
795                        (debug &&
796                        (monitorstart == -1 ||
797                         (offset + size > monitorstart &&
798                         (monitorend == -1 || offset <= monitorend))))))
799                 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
800                     offset, offset + size - 1, size);
801
802         if (file_size > cur_filesize) {
803                 if (ftruncate(fd, file_size) == -1) {
804                         prterr("domapwrite: ftruncate");
805                         exit(201);
806                 }
807         }
808         pg_offset = offset & PAGE_MASK;
809         map_size  = pg_offset + size;
810
811         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
812                               MAP_FILE | MAP_SHARED, fd,
813                               (off_t)(offset - pg_offset))) == (char *)-1) {
814                 prterr("domapwrite: mmap");
815                 report_failure(202);
816         }
817         memcpy(p + pg_offset, good_buf + offset, size);
818         if (msync(p, map_size, MS_SYNC) != 0) {
819                 prterr("domapwrite: msync");
820                 report_failure(203);
821         }
822
823         check_eofpage("Write", offset, p, size);
824
825         if (munmap(p, map_size) != 0) {
826                 prterr("domapwrite: munmap");
827                 report_failure(204);
828         }
829 }
830
831
832 void
833 dotruncate(unsigned size)
834 {
835         int oldsize = file_size;
836
837         size -= size % truncbdy;
838         if (size > biggest) {
839                 biggest = size;
840                 if (!quiet && testcalls > simulatedopcount)
841                         prt("truncating to largest ever: 0x%x\n", size);
842         }
843
844         log4(OP_TRUNCATE, 0, size, FL_NONE);
845
846         if (size > file_size)
847                 memset(good_buf + file_size, '\0', size - file_size);
848         file_size = size;
849
850         if (testcalls <= simulatedopcount)
851                 return;
852         
853         if ((progressinterval && testcalls % progressinterval == 0) ||
854             (debug && (monitorstart == -1 || monitorend == -1 ||
855                       size <= monitorend)))
856                 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
857         if (ftruncate(fd, (off_t)size) == -1) {
858                 prt("ftruncate1: %x\n", size);
859                 prterr("dotruncate: ftruncate");
860                 report_failure(160);
861         }
862 }
863
864 #ifdef FALLOC_FL_PUNCH_HOLE
865 void
866 do_punch_hole(unsigned offset, unsigned length)
867 {
868         unsigned end_offset;
869         int max_offset = 0;
870         int max_len = 0;
871         int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
872
873         if (length == 0) {
874                 if (!quiet && testcalls > simulatedopcount)
875                         prt("skipping zero length punch hole\n");
876                         log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
877                 return;
878         }
879
880         if (file_size <= (loff_t)offset) {
881                 if (!quiet && testcalls > simulatedopcount)
882                         prt("skipping hole punch off the end of the file\n");
883                         log4(OP_PUNCH_HOLE, offset, length, FL_SKIPPED);
884                 return;
885         }
886
887         end_offset = offset + length;
888
889         log4(OP_PUNCH_HOLE, offset, length, FL_NONE);
890
891         if (testcalls <= simulatedopcount)
892                 return;
893
894         if ((progressinterval && testcalls % progressinterval == 0) ||
895             (debug && (monitorstart == -1 || monitorend == -1 ||
896                       end_offset <= monitorend))) {
897                 prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
898                         offset, offset+length, length);
899         }
900         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
901                 prt("%punch hole: %x to %x\n", offset, length);
902                 prterr("do_punch_hole: fallocate");
903                 report_failure(161);
904         }
905
906
907         max_offset = offset < file_size ? offset : file_size;
908         max_len = max_offset + length <= file_size ? length :
909                         file_size - max_offset;
910         memset(good_buf + max_offset, '\0', max_len);
911 }
912
913 #else
914 void
915 do_punch_hole(unsigned offset, unsigned length)
916 {
917         return;
918 }
919 #endif
920
921 #ifdef FALLOC_FL_ZERO_RANGE
922 void
923 do_zero_range(unsigned offset, unsigned length, int keep_size)
924 {
925         unsigned end_offset;
926         int mode = FALLOC_FL_ZERO_RANGE;
927
928         if (length == 0) {
929                 if (!quiet && testcalls > simulatedopcount)
930                         prt("skipping zero length zero range\n");
931                         log4(OP_ZERO_RANGE, offset, length, FL_SKIPPED |
932                              (keep_size ? FL_KEEP_SIZE : FL_NONE));
933                 return;
934         }
935
936         end_offset = keep_size ? 0 : offset + length;
937
938         if (end_offset > biggest) {
939                 biggest = end_offset;
940                 if (!quiet && testcalls > simulatedopcount)
941                         prt("zero_range to largest ever: 0x%x\n", end_offset);
942         }
943
944         /*
945          * last arg matches fallocate string array index in logdump:
946          *      0: allocate past EOF
947          *      1: extending prealloc
948          *      2: interior prealloc
949          */
950         log4(OP_ZERO_RANGE, offset, length,
951              keep_size ? FL_KEEP_SIZE : FL_NONE);
952
953         if (testcalls <= simulatedopcount)
954                 return;
955
956         if ((progressinterval && testcalls % progressinterval == 0) ||
957             (debug && (monitorstart == -1 || monitorend == -1 ||
958                       end_offset <= monitorend))) {
959                 prt("%lu zero\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
960                         offset, offset+length, length);
961         }
962         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
963                 prt("%pzero range: %x to %x\n", offset, length);
964                 prterr("do_zero_range: fallocate");
965                 report_failure(161);
966         }
967
968         memset(good_buf + offset, '\0', length);
969 }
970
971 #else
972 void
973 do_zero_range(unsigned offset, unsigned length, int keep_size)
974 {
975         return;
976 }
977 #endif
978
979 #ifdef FALLOC_FL_COLLAPSE_RANGE
980 void
981 do_collapse_range(unsigned offset, unsigned length)
982 {
983         unsigned end_offset;
984         int mode = FALLOC_FL_COLLAPSE_RANGE;
985
986         if (length == 0) {
987                 if (!quiet && testcalls > simulatedopcount)
988                         prt("skipping zero length collapse range\n");
989                 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
990                 return;
991         }
992
993         end_offset = offset + length;
994         if ((loff_t)end_offset >= file_size) {
995                 if (!quiet && testcalls > simulatedopcount)
996                         prt("skipping collapse range behind EOF\n");
997                 log4(OP_COLLAPSE_RANGE, offset, length, FL_SKIPPED);
998                 return;
999         }
1000
1001         log4(OP_COLLAPSE_RANGE, offset, length, FL_NONE);
1002
1003         if (testcalls <= simulatedopcount)
1004                 return;
1005
1006         if ((progressinterval && testcalls % progressinterval == 0) ||
1007             (debug && (monitorstart == -1 || monitorend == -1 ||
1008                       end_offset <= monitorend))) {
1009                 prt("%lu collapse\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1010                         offset, offset+length, length);
1011         }
1012         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1013                 prt("collapse range: %x to %x\n", offset, length);
1014                 prterr("do_collapse_range: fallocate");
1015                 report_failure(161);
1016         }
1017
1018         memmove(good_buf + offset, good_buf + end_offset,
1019                 file_size - end_offset);
1020         file_size -= length;
1021 }
1022
1023 #else
1024 void
1025 do_collapse_range(unsigned offset, unsigned length)
1026 {
1027         return;
1028 }
1029 #endif
1030
1031 #ifdef FALLOC_FL_INSERT_RANGE
1032 void
1033 do_insert_range(unsigned offset, unsigned length)
1034 {
1035         unsigned end_offset;
1036         int mode = FALLOC_FL_INSERT_RANGE;
1037
1038         if (length == 0) {
1039                 if (!quiet && testcalls > simulatedopcount)
1040                         prt("skipping zero length insert range\n");
1041                 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1042                 return;
1043         }
1044
1045         if ((loff_t)offset >= file_size) {
1046                 if (!quiet && testcalls > simulatedopcount)
1047                         prt("skipping insert range behind EOF\n");
1048                 log4(OP_INSERT_RANGE, offset, length, FL_SKIPPED);
1049                 return;
1050         }
1051
1052         log4(OP_INSERT_RANGE, offset, length, FL_NONE);
1053
1054         if (testcalls <= simulatedopcount)
1055                 return;
1056
1057         end_offset = offset + length;
1058         if ((progressinterval && testcalls % progressinterval == 0) ||
1059             (debug && (monitorstart == -1 || monitorend == -1 ||
1060                       end_offset <= monitorend))) {
1061                 prt("%lu insert\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
1062                         offset, offset+length, length);
1063         }
1064         if (fallocate(fd, mode, (loff_t)offset, (loff_t)length) == -1) {
1065                 prt("insert range: %x to %x\n", offset, length);
1066                 prterr("do_insert_range: fallocate");
1067                 report_failure(161);
1068         }
1069
1070         memmove(good_buf + end_offset, good_buf + offset,
1071                 file_size - offset);
1072         memset(good_buf + offset, '\0', length);
1073         file_size += length;
1074 }
1075
1076 #else
1077 void
1078 do_insert_range(unsigned offset, unsigned length)
1079 {
1080         return;
1081 }
1082 #endif
1083
1084 #ifdef HAVE_LINUX_FALLOC_H
1085 /* fallocate is basically a no-op unless extending, then a lot like a truncate */
1086 void
1087 do_preallocate(unsigned offset, unsigned length, int keep_size)
1088 {
1089         unsigned end_offset;
1090
1091         if (length == 0) {
1092                 if (!quiet && testcalls > simulatedopcount)
1093                         prt("skipping zero length fallocate\n");
1094                 log4(OP_FALLOCATE, offset, length, FL_SKIPPED |
1095                      (keep_size ? FL_KEEP_SIZE : FL_NONE));
1096                 return;
1097         }
1098
1099         end_offset = keep_size ? 0 : offset + length;
1100
1101         if (end_offset > biggest) {
1102                 biggest = end_offset;
1103                 if (!quiet && testcalls > simulatedopcount)
1104                         prt("fallocating to largest ever: 0x%x\n", end_offset);
1105         }
1106
1107         /*
1108          * last arg matches fallocate string array index in logdump:
1109          *      0: allocate past EOF
1110          *      1: extending prealloc
1111          *      2: interior prealloc
1112          */
1113         log4(OP_FALLOCATE, offset, length,
1114              keep_size ? FL_KEEP_SIZE : FL_NONE);
1115
1116         if (end_offset > file_size) {
1117                 memset(good_buf + file_size, '\0', end_offset - file_size);
1118                 file_size = end_offset;
1119         }
1120
1121         if (testcalls <= simulatedopcount)
1122                 return;
1123         
1124         if ((progressinterval && testcalls % progressinterval == 0) ||
1125             (debug && (monitorstart == -1 || monitorend == -1 ||
1126                       end_offset <= monitorend)))
1127                 prt("%lu falloc\tfrom 0x%x to 0x%x (0x%x bytes)\n", testcalls,
1128                                 offset, offset + length, length);
1129         if (fallocate(fd, keep_size ? FALLOC_FL_KEEP_SIZE : 0, (loff_t)offset, (loff_t)length) == -1) {
1130                 prt("fallocate: %x to %x\n", offset, length);
1131                 prterr("do_preallocate: fallocate");
1132                 report_failure(161);
1133         }
1134 }
1135 #else
1136 void
1137 do_preallocate(unsigned offset, unsigned length, int keep_size)
1138 {
1139         return;
1140 }
1141 #endif
1142
1143 void
1144 writefileimage()
1145 {
1146         ssize_t iret;
1147
1148         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
1149                 prterr("writefileimage: lseek");
1150                 report_failure(171);
1151         }
1152         iret = write(fd, good_buf, file_size);
1153         if ((off_t)iret != file_size) {
1154                 if (iret == -1)
1155                         prterr("writefileimage: write");
1156                 else
1157                         prt("short write: 0x%x bytes instead of 0x%llx\n",
1158                             iret, (unsigned long long)file_size);
1159                 report_failure(172);
1160         }
1161         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
1162                 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
1163                 prterr("writefileimage: ftruncate");
1164                 report_failure(173);
1165         }
1166 }
1167
1168
1169 void
1170 docloseopen(void)
1171
1172         if (testcalls <= simulatedopcount)
1173                 return;
1174
1175         if (debug)
1176                 prt("%lu close/open\n", testcalls);
1177         if (close(fd)) {
1178                 prterr("docloseopen: close");
1179                 report_failure(180);
1180         }
1181         fd = open(fname, O_RDWR|o_direct, 0);
1182         if (fd < 0) {
1183                 prterr("docloseopen: open");
1184                 report_failure(181);
1185         }
1186 }
1187
1188
1189 #define TRIM_OFF(off, size)                     \
1190 do {                                            \
1191         if (size)                               \
1192                 (off) %= (size);                \
1193         else                                    \
1194                 (off) = 0;                      \
1195 } while (0)
1196
1197 #define TRIM_LEN(off, len, size)                \
1198 do {                                            \
1199         if ((off) + (len) > (size))             \
1200                 (len) = (size) - (off);         \
1201 } while (0)
1202
1203 #define TRIM_OFF_LEN(off, len, size)            \
1204 do {                                            \
1205         TRIM_OFF(off, size);                    \
1206         TRIM_LEN(off, len, size);               \
1207 } while (0)
1208
1209 void
1210 test(void)
1211 {
1212         unsigned long   offset;
1213         unsigned long   size = maxoplen;
1214         unsigned long   rv = random();
1215         unsigned long   op;
1216         int             keep_size = 0;
1217
1218         if (simulatedopcount > 0 && testcalls == simulatedopcount)
1219                 writefileimage();
1220
1221         testcalls++;
1222
1223         if (closeprob)
1224                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
1225
1226         if (debugstart > 0 && testcalls >= debugstart)
1227                 debug = 1;
1228
1229         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1230                 prt("%lu...\n", testcalls);
1231
1232         offset = random();
1233         if (randomoplen)
1234                 size = random() % (maxoplen + 1);
1235
1236         /* calculate appropriate op to run */
1237         if (lite)
1238                 op = rv % OP_MAX_LITE;
1239         else
1240                 op = rv % OP_MAX_FULL;
1241
1242         switch(op) {
1243         case OP_FALLOCATE:
1244                 if (fallocate_calls && size && keep_size_calls)
1245                         keep_size = random() % 2;
1246                 break;
1247         case OP_ZERO_RANGE:
1248                 if (zero_range_calls && size && keep_size_calls)
1249                         keep_size = random() % 2;
1250                 break;
1251         }
1252
1253         switch (op) {
1254         case OP_MAPREAD:
1255                 if (!mapped_reads)
1256                         op = OP_READ;
1257                 break;
1258         case OP_MAPWRITE:
1259                 if (!mapped_writes)
1260                         op = OP_WRITE;
1261                 break;
1262         case OP_FALLOCATE:
1263                 if (!fallocate_calls) {
1264                         log4(OP_FALLOCATE, offset, size, FL_SKIPPED);
1265                         goto out;
1266                 }
1267                 break;
1268         case OP_PUNCH_HOLE:
1269                 if (!punch_hole_calls) {
1270                         log4(OP_PUNCH_HOLE, offset, size, FL_SKIPPED);
1271                         goto out;
1272                 }
1273                 break;
1274         case OP_ZERO_RANGE:
1275                 if (!zero_range_calls) {
1276                         log4(OP_ZERO_RANGE, offset, size, FL_SKIPPED);
1277                         goto out;
1278                 }
1279                 break;
1280         case OP_COLLAPSE_RANGE:
1281                 if (!collapse_range_calls) {
1282                         log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1283                         goto out;
1284                 }
1285                 break;
1286         case OP_INSERT_RANGE:
1287                 if (!insert_range_calls) {
1288                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1289                         goto out;
1290                 }
1291                 break;
1292         }
1293
1294         switch (op) {
1295         case OP_READ:
1296                 TRIM_OFF_LEN(offset, size, file_size);
1297                 doread(offset, size);
1298                 break;
1299
1300         case OP_WRITE:
1301                 TRIM_OFF_LEN(offset, size, maxfilelen);
1302                 dowrite(offset, size);
1303                 break;
1304
1305         case OP_MAPREAD:
1306                 TRIM_OFF_LEN(offset, size, file_size);
1307                 domapread(offset, size);
1308                 break;
1309
1310         case OP_MAPWRITE:
1311                 TRIM_OFF_LEN(offset, size, maxfilelen);
1312                 domapwrite(offset, size);
1313                 break;
1314
1315         case OP_TRUNCATE:
1316                 if (!style)
1317                         size = random() % maxfilelen;
1318                 dotruncate(size);
1319                 break;
1320
1321         case OP_FALLOCATE:
1322                 TRIM_OFF_LEN(offset, size, maxfilelen);
1323                 do_preallocate(offset, size, keep_size);
1324                 break;
1325
1326         case OP_PUNCH_HOLE:
1327                 TRIM_OFF_LEN(offset, size, file_size);
1328                 do_punch_hole(offset, size);
1329                 break;
1330         case OP_ZERO_RANGE:
1331                 TRIM_OFF_LEN(offset, size, file_size);
1332                 do_zero_range(offset, size, keep_size);
1333                 break;
1334         case OP_COLLAPSE_RANGE:
1335                 TRIM_OFF_LEN(offset, size, file_size - 1);
1336                 offset = offset & ~(block_size - 1);
1337                 size = size & ~(block_size - 1);
1338                 if (size == 0) {
1339                         log4(OP_COLLAPSE_RANGE, offset, size, FL_SKIPPED);
1340                         goto out;
1341                 }
1342                 do_collapse_range(offset, size);
1343                 break;
1344         case OP_INSERT_RANGE:
1345                 TRIM_OFF(offset, file_size);
1346                 TRIM_LEN(file_size, size, maxfilelen);
1347                 offset = offset & ~(block_size - 1);
1348                 size = size & ~(block_size - 1);
1349                 if (size == 0) {
1350                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1351                         goto out;
1352                 }
1353                 if (file_size + size > maxfilelen) {
1354                         log4(OP_INSERT_RANGE, offset, size, FL_SKIPPED);
1355                         goto out;
1356                 }
1357
1358                 do_insert_range(offset, size);
1359                 break;
1360         default:
1361                 prterr("test: unknown operation");
1362                 report_failure(42);
1363                 break;
1364         }
1365
1366 out:
1367         if (sizechecks && testcalls > simulatedopcount)
1368                 check_size();
1369         if (closeopen)
1370                 docloseopen();
1371 }
1372
1373
1374 void
1375 cleanup(sig)
1376         int     sig;
1377 {
1378         if (sig)
1379                 prt("signal %d\n", sig);
1380         prt("testcalls = %lu\n", testcalls);
1381         exit(sig);
1382 }
1383
1384
1385 void
1386 usage(void)
1387 {
1388         fprintf(stdout, "usage: %s",
1389                 "fsx [-dnqxAFLOWZ] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
1390         -b opnum: beginning operation number (default 1)\n\
1391         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
1392         -d: debug output for all operations\n\
1393         -f flush and invalidate cache after I/O\n\
1394         -l flen: the upper bound on file size (default 262144)\n\
1395         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
1396         -n: no verifications of file size\n\
1397         -o oplen: the upper bound on operation size (default 65536)\n\
1398         -p progressinterval: debug output at specified operation interval\n\
1399         -q: quieter operation\n\
1400         -r readbdy: 4096 would make reads page aligned (default 1)\n\
1401         -s style: 1 gives smaller truncates (default 0)\n\
1402         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
1403         -w writebdy: 4096 would make writes page aligned (default 1)\n\
1404         -x: preallocate file space before starting, XFS only (default 0)\n\
1405         -y synchronize changes to a file\n"
1406
1407 #ifdef AIO
1408 "       -A: Use the AIO system calls\n"
1409 #endif
1410 "       -D startingop: debug output starting at specified operation\n"
1411 #ifdef HAVE_LINUX_FALLOC_H
1412 "       -F: Do not use fallocate (preallocation) calls\n"
1413 #endif
1414 #ifdef FALLOC_FL_PUNCH_HOLE
1415 "       -H: Do not use punch hole calls\n"
1416 #endif
1417 #ifdef FALLOC_FL_ZERO_RANGE
1418 "       -z: Do not use zero range calls\n"
1419 #endif
1420 #ifdef FALLOC_FL_COLLAPSE_RANGE
1421 "       -C: Do not use collapse range calls\n"
1422 #endif
1423 #ifdef FALLOC_FL_INSERT_RANGE
1424 "       -I: Do not use insert range calls\n"
1425 #endif
1426 "       -L: fsxLite - no file creations & no file size changes\n\
1427         -N numops: total # operations to do (default infinity)\n\
1428         -O: use oplen (see -o flag) for every op (default random)\n\
1429         -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
1430         -S seed: for random # generator (default 1) 0 gets timestamp\n\
1431         -W: mapped write operations DISabled\n\
1432         -R: read() system calls only (mapped reads disabled)\n\
1433         -Z: O_DIRECT (use -R, -W, -r and -w too)\n\
1434         fname: this filename is REQUIRED (no default)\n");
1435         exit(90);
1436 }
1437
1438
1439 int
1440 getnum(char *s, char **e)
1441 {
1442         int ret;
1443
1444         *e = (char *) 0;
1445         ret = strtol(s, e, 0);
1446         if (*e)
1447                 switch (**e) {
1448                 case 'b':
1449                 case 'B':
1450                         ret *= 512;
1451                         *e = *e + 1;
1452                         break;
1453                 case 'k':
1454                 case 'K':
1455                         ret *= 1024;
1456                         *e = *e + 1;
1457                         break;
1458                 case 'm':
1459                 case 'M':
1460                         ret *= 1024*1024;
1461                         *e = *e + 1;
1462                         break;
1463                 case 'w':
1464                 case 'W':
1465                         ret *= 4;
1466                         *e = *e + 1;
1467                         break;
1468                 }
1469         return (ret);
1470 }
1471
1472 #ifdef AIO
1473
1474 #define QSZ     1024
1475 io_context_t    io_ctx;
1476 struct iocb     iocb;
1477
1478 int aio_setup()
1479 {
1480         int ret;
1481         ret = io_queue_init(QSZ, &io_ctx);
1482         if (ret != 0) {
1483                 fprintf(stderr, "aio_setup: io_queue_init failed: %s\n",
1484                         strerror(ret));
1485                 return(-1);
1486         }
1487         return(0);
1488 }
1489
1490 int
1491 __aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1492 {
1493         struct io_event event;
1494         static struct timespec ts;
1495         struct iocb *iocbs[] = { &iocb };
1496         int ret;
1497         long res;
1498
1499         if (rw == READ) {
1500                 io_prep_pread(&iocb, fd, buf, len, offset);
1501         } else {
1502                 io_prep_pwrite(&iocb, fd, buf, len, offset);
1503         }
1504
1505         ts.tv_sec = 30;
1506         ts.tv_nsec = 0;
1507         ret = io_submit(io_ctx, 1, iocbs);
1508         if (ret != 1) {
1509                 fprintf(stderr, "errcode=%d\n", ret);
1510                 fprintf(stderr, "aio_rw: io_submit failed: %s\n",
1511                                 strerror(ret));
1512                 goto out_error;
1513         }
1514
1515         ret = io_getevents(io_ctx, 1, 1, &event, &ts);
1516         if (ret != 1) {
1517                 if (ret == 0)
1518                         fprintf(stderr, "aio_rw: no events available\n");
1519                 else {
1520                         fprintf(stderr, "errcode=%d\n", -ret);
1521                         fprintf(stderr, "aio_rw: io_getevents failed: %s\n",
1522                                         strerror(-ret));
1523                 }
1524                 goto out_error;
1525         }
1526         if (len != event.res) {
1527                 /*
1528                  * The b0rked libaio defines event.res as unsigned.
1529                  * However the kernel strucuture has it signed,
1530                  * and it's used to pass negated error value.
1531                  * Till the library is fixed use the temp var.
1532                  */
1533                 res = (long)event.res;
1534                 if (res >= 0)
1535                         fprintf(stderr, "bad io length: %lu instead of %u\n",
1536                                         res, len);
1537                 else {
1538                         fprintf(stderr, "errcode=%ld\n", -res);
1539                         fprintf(stderr, "aio_rw: async io failed: %s\n",
1540                                         strerror(-res));
1541                         ret = res;
1542                         goto out_error;
1543                 }
1544
1545         }
1546         return event.res;
1547
1548 out_error:
1549         /*
1550          * The caller expects error return in traditional libc
1551          * convention, i.e. -1 and the errno set to error.
1552          */
1553         errno = -ret;
1554         return -1;
1555 }
1556
1557 int aio_rw(int rw, int fd, char *buf, unsigned len, unsigned offset)
1558 {
1559         int ret;
1560
1561         if (aio) {
1562                 ret = __aio_rw(rw, fd, buf, len, offset);
1563         } else {
1564                 if (rw == READ)
1565                         ret = read(fd, buf, len);
1566                 else
1567                         ret = write(fd, buf, len);
1568         }
1569         return ret;
1570 }
1571
1572 #endif
1573
1574 #define test_fallocate(mode) __test_fallocate(mode, #mode)
1575
1576 int
1577 __test_fallocate(int mode, const char *mode_str)
1578 {
1579 #ifdef HAVE_LINUX_FALLOC_H
1580         int ret = 0;
1581         if (!lite) {
1582                 if (fallocate(fd, mode, 0, 1) && errno == EOPNOTSUPP) {
1583                         if(!quiet)
1584                                 fprintf(stderr,
1585                                         "main: filesystem does not support "
1586                                         "fallocate mode %s, disabling!\n",
1587                                         mode_str);
1588                 } else {
1589                         ret = 1;
1590                         ftruncate(fd, 0);
1591                 }
1592         }
1593         return ret;
1594 #endif
1595 }
1596
1597 int
1598 main(int argc, char **argv)
1599 {
1600         int     i, style, ch;
1601         char    *endp;
1602         char goodfile[1024];
1603         char logfile[1024];
1604         struct stat statbuf;
1605
1606         goodfile[0] = 0;
1607         logfile[0] = 0;
1608
1609         page_size = getpagesize();
1610         page_mask = page_size - 1;
1611         mmap_mask = page_mask;
1612         
1613
1614         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1615
1616         while ((ch = getopt(argc, argv, "b:c:dfl:m:no:p:qr:s:t:w:xyAD:FKHzCILN:OP:RS:WZ"))
1617                != EOF)
1618                 switch (ch) {
1619                 case 'b':
1620                         simulatedopcount = getnum(optarg, &endp);
1621                         if (!quiet)
1622                                 fprintf(stdout, "Will begin at operation %ld\n",
1623                                         simulatedopcount);
1624                         if (simulatedopcount == 0)
1625                                 usage();
1626                         simulatedopcount -= 1;
1627                         break;
1628                 case 'c':
1629                         closeprob = getnum(optarg, &endp);
1630                         if (!quiet)
1631                                 fprintf(stdout,
1632                                         "Chance of close/open is 1 in %d\n",
1633                                         closeprob);
1634                         if (closeprob <= 0)
1635                                 usage();
1636                         break;
1637                 case 'd':
1638                         debug = 1;
1639                         break;
1640                 case 'f':
1641                         flush = 1;
1642                         break;
1643                 case 'l':
1644                         maxfilelen = getnum(optarg, &endp);
1645                         if (maxfilelen <= 0)
1646                                 usage();
1647                         break;
1648                 case 'm':
1649                         monitorstart = getnum(optarg, &endp);
1650                         if (monitorstart < 0)
1651                                 usage();
1652                         if (!endp || *endp++ != ':')
1653                                 usage();
1654                         monitorend = getnum(endp, &endp);
1655                         if (monitorend < 0)
1656                                 usage();
1657                         if (monitorend == 0)
1658                                 monitorend = -1; /* aka infinity */
1659                         debug = 1;
1660                 case 'n':
1661                         sizechecks = 0;
1662                         break;
1663                 case 'o':
1664                         maxoplen = getnum(optarg, &endp);
1665                         if (maxoplen <= 0)
1666                                 usage();
1667                         break;
1668                 case 'p':
1669                         progressinterval = getnum(optarg, &endp);
1670                         if (progressinterval == 0)
1671                                 usage();
1672                         break;
1673                 case 'q':
1674                         quiet = 1;
1675                         break;
1676                 case 'r':
1677                         readbdy = getnum(optarg, &endp);
1678                         if (readbdy <= 0)
1679                                 usage();
1680                         break;
1681                 case 's':
1682                         style = getnum(optarg, &endp);
1683                         if (style < 0 || style > 1)
1684                                 usage();
1685                         break;
1686                 case 't':
1687                         truncbdy = getnum(optarg, &endp);
1688                         if (truncbdy <= 0)
1689                                 usage();
1690                         break;
1691                 case 'w':
1692                         writebdy = getnum(optarg, &endp);
1693                         if (writebdy <= 0)
1694                                 usage();
1695                         break;
1696                 case 'x':
1697                         prealloc = 1;
1698                         break;
1699                 case 'y':
1700                         do_fsync = 1;
1701                         break;
1702                 case 'A':
1703                         aio = 1;
1704                         break;
1705                 case 'D':
1706                         debugstart = getnum(optarg, &endp);
1707                         if (debugstart < 1)
1708                                 usage();
1709                         break;
1710                 case 'F':
1711                         fallocate_calls = 0;
1712                         break;
1713                 case 'K':
1714                         keep_size_calls = 0;
1715                         break;
1716                 case 'H':
1717                         punch_hole_calls = 0;
1718                         break;
1719                 case 'z':
1720                         zero_range_calls = 0;
1721                         break;
1722                 case 'C':
1723                         collapse_range_calls = 0;
1724                         break;
1725                 case 'I':
1726                         insert_range_calls = 0;
1727                         break;
1728                 case 'L':
1729                         lite = 1;
1730                         break;
1731                 case 'N':
1732                         numops = getnum(optarg, &endp);
1733                         if (numops < 0)
1734                                 usage();
1735                         break;
1736                 case 'O':
1737                         randomoplen = 0;
1738                         break;
1739                 case 'P':
1740                         strncpy(goodfile, optarg, sizeof(goodfile));
1741                         strcat(goodfile, "/");
1742                         strncpy(logfile, optarg, sizeof(logfile));
1743                         strcat(logfile, "/");
1744                         break;
1745                 case 'R':
1746                         mapped_reads = 0;
1747                         break;
1748                 case 'S':
1749                         seed = getnum(optarg, &endp);
1750                         if (seed == 0)
1751                                 seed = time(0) % 10000;
1752                         if (!quiet)
1753                                 fprintf(stdout, "Seed set to %d\n", seed);
1754                         if (seed < 0)
1755                                 usage();
1756                         break;
1757                 case 'W':
1758                         mapped_writes = 0;
1759                         if (!quiet)
1760                                 fprintf(stdout, "mapped writes DISABLED\n");
1761                         break;
1762                 case 'Z':
1763                         o_direct = O_DIRECT;
1764                         break;
1765                 default:
1766                         usage();
1767                         /* NOTREACHED */
1768                 }
1769         argc -= optind;
1770         argv += optind;
1771         if (argc != 1)
1772                 usage();
1773         fname = argv[0];
1774
1775         signal(SIGHUP,  cleanup);
1776         signal(SIGINT,  cleanup);
1777         signal(SIGPIPE, cleanup);
1778         signal(SIGALRM, cleanup);
1779         signal(SIGTERM, cleanup);
1780         signal(SIGXCPU, cleanup);
1781         signal(SIGXFSZ, cleanup);
1782         signal(SIGVTALRM,       cleanup);
1783         signal(SIGUSR1, cleanup);
1784         signal(SIGUSR2, cleanup);
1785
1786         initstate(seed, state, 256);
1787         setstate(state);
1788         fd = open(fname,
1789                 O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC)|o_direct, 0666);
1790         if (fd < 0) {
1791                 prterr(fname);
1792                 exit(91);
1793         }
1794         if (fstat(fd, &statbuf)) {
1795                 prterr("check_size: fstat");
1796                 exit(91);
1797         }
1798         block_size = statbuf.st_blksize;
1799 #ifdef XFS
1800         if (prealloc) {
1801                 xfs_flock64_t   resv = { 0 };
1802 #ifdef HAVE_XFS_PLATFORM_DEFS_H
1803                 if (!platform_test_xfs_fd(fd)) {
1804                         prterr(fname);
1805                         fprintf(stderr, "main: cannot prealloc, non XFS\n");
1806                         exit(96);
1807                 }
1808 #endif
1809                 resv.l_len = maxfilelen;
1810                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1811                         prterr(fname);
1812                         exit(97);
1813                 }
1814         }
1815 #endif
1816         strncat(goodfile, fname, 256);
1817         strcat (goodfile, ".fsxgood");
1818         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1819         if (fsxgoodfd < 0) {
1820                 prterr(goodfile);
1821                 exit(92);
1822         }
1823         strncat(logfile, fname, 256);
1824         strcat (logfile, ".fsxlog");
1825         fsxlogf = fopen(logfile, "w");
1826         if (fsxlogf == NULL) {
1827                 prterr(logfile);
1828                 exit(93);
1829         }
1830
1831 #ifdef AIO
1832         if (aio) 
1833                 aio_setup();
1834 #endif
1835
1836         if (lite) {
1837                 off_t ret;
1838                 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1839                 if (file_size == (off_t)-1) {
1840                         prterr(fname);
1841                         warn("main: lseek eof");
1842                         exit(94);
1843                 }
1844                 ret = lseek(fd, (off_t)0, SEEK_SET);
1845                 if (ret == (off_t)-1) {
1846                         prterr(fname);
1847                         warn("main: lseek 0");
1848                         exit(95);
1849                 }
1850         }
1851         original_buf = (char *) malloc(maxfilelen);
1852         for (i = 0; i < maxfilelen; i++)
1853                 original_buf[i] = random() % 256;
1854         good_buf = (char *) malloc(maxfilelen + writebdy);
1855         good_buf = round_ptr_up(good_buf, writebdy, 0);
1856         memset(good_buf, '\0', maxfilelen);
1857         temp_buf = (char *) malloc(maxoplen + readbdy);
1858         temp_buf = round_ptr_up(temp_buf, readbdy, 0);
1859         memset(temp_buf, '\0', maxoplen);
1860         if (lite) {     /* zero entire existing file */
1861                 ssize_t written;
1862
1863                 written = write(fd, good_buf, (size_t)maxfilelen);
1864                 if (written != maxfilelen) {
1865                         if (written == -1) {
1866                                 prterr(fname);
1867                                 warn("main: error on write");
1868                         } else
1869                                 warn("main: short write, 0x%x bytes instead "
1870                                         "of 0x%lx\n",
1871                                         (unsigned)written,
1872                                         maxfilelen);
1873                         exit(98);
1874                 }
1875         } else 
1876                 check_trunc_hack();
1877
1878         if (fallocate_calls)
1879                 fallocate_calls = test_fallocate(0);
1880         if (keep_size_calls)
1881                 keep_size_calls = test_fallocate(FALLOC_FL_KEEP_SIZE);
1882         if (punch_hole_calls)
1883                 punch_hole_calls = test_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
1884         if (zero_range_calls)
1885                 zero_range_calls = test_fallocate(FALLOC_FL_ZERO_RANGE);
1886         if (collapse_range_calls)
1887                 collapse_range_calls = test_fallocate(FALLOC_FL_COLLAPSE_RANGE);
1888         if (insert_range_calls)
1889                 insert_range_calls = test_fallocate(FALLOC_FL_INSERT_RANGE);
1890
1891         while (numops == -1 || numops--)
1892                 test();
1893
1894         if (close(fd)) {
1895                 prterr("close");
1896                 report_failure(99);
1897         }
1898         prt("All %lu operations completed A-OK!\n", testcalls);
1899
1900         exit(0);
1901         return 0;
1902 }