Add an fsx test, allow fsx to preallocate file space on -x
[xfstests-dev.git] / ltp / fsx.c
1 /*
2  * Copyright (C) 1991, NeXT Computer, Inc.  All Rights Reserverd.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  * 
6  * The contents of this file constitute Original Code as defined in and
7  * are subject to the Apple Public Source License Version 1.1 (the
8  * "License").  You may not use this file except in compliance with the
9  * License.  Please obtain a copy of the License at
10  * http://www.apple.com/publicsource and read it before using this file.
11  * 
12  * This Original Code and all software distributed under the License are
13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17  * License for the specific language governing rights and limitations
18  * under the License.
19  * 
20  * @APPLE_LICENSE_HEADER_END@
21  *
22  *
23  *      File:   fsx.c
24  *      Author: Avadis Tevanian, Jr.
25  *
26  *      File system exerciser. 
27  *
28  *      Rewritten 8/98 by Conrad Minshall.
29  *
30  *      Small changes to work under Linux -- davej.
31  *
32  *      XFS space preallocation changes -- nathans.
33  */
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/param.h>
38 #include <limits.h>
39 #include <time.h>
40 #include <strings.h>
41 #include <sys/file.h>
42 #include <sys/mman.h>
43 #include <limits.h>
44 #include <err.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <stdarg.h>
51 #include <errno.h>
52
53 #include <xfs/libxfs.h>
54
55 #ifndef MAP_FILE
56 # define MAP_FILE 0
57 #endif
58
59 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
60
61 /*
62  *      A log entry is an operation and a bunch of arguments.
63  */
64
65 struct log_entry {
66         int     operation;
67         int     args[3];
68 };
69
70 #define LOGSIZE 1000
71
72 struct log_entry        oplog[LOGSIZE]; /* the log */
73 int                     logptr = 0;     /* current position in log */
74 int                     logcount = 0;   /* total ops */
75
76 /*
77  *      Define operations
78  */
79
80 #define OP_READ         1
81 #define OP_WRITE        2
82 #define OP_TRUNCATE     3
83 #define OP_CLOSEOPEN    4
84 #define OP_MAPREAD      5
85 #define OP_MAPWRITE     6
86 #define OP_SKIPPED      7
87
88 #ifndef PAGE_SIZE
89 #define PAGE_SIZE       getpagesize()
90 #endif
91 #define PAGE_MASK       (PAGE_SIZE - 1)
92
93 char    *original_buf;                  /* a pointer to the original data */
94 char    *good_buf;                      /* a pointer to the correct data */
95 char    *temp_buf;                      /* a pointer to the current data */
96 char    *fname;                         /* name of our test file */
97 int     fd;                             /* fd for our test file */
98
99 off_t           file_size = 0;
100 off_t           biggest = 0;
101 char            state[256];
102 unsigned long   testcalls = 0;          /* calls to function "test" */
103
104 unsigned long   simulatedopcount = 0;   /* -b flag */
105 int     closeprob = 0;                  /* -c flag */
106 int     debug = 0;                      /* -d flag */
107 unsigned long   debugstart = 0;         /* -D flag */
108 unsigned long   maxfilelen = 256 * 1024;        /* -l flag */
109 int     sizechecks = 1;                 /* -n flag disables them */
110 int     maxoplen = 64 * 1024;           /* -o flag */
111 int     quiet = 0;                      /* -q flag */
112 unsigned long progressinterval = 0;     /* -p flag */
113 int     readbdy = 1;                    /* -r flag */
114 int     style = 0;                      /* -s flag */
115 int     prealloc = 0;                   /* -u flag */
116 int     truncbdy = 1;                   /* -t flag */
117 int     writebdy = 1;                   /* -w flag */
118 long    monitorstart = -1;              /* -m flag */
119 long    monitorend = -1;                /* -m flag */
120 int     lite = 0;                       /* -L flag */
121 long    numops = -1;                    /* -N flag */
122 int     randomoplen = 1;                /* -O flag disables it */
123 int     seed = 1;                       /* -S flag */
124 int     mapped_writes = 1;              /* -W flag disables */
125 int     mapped_reads = 1;               /* -R flag disables it */
126 int     fsxgoodfd = 0;
127 FILE *  fsxlogf = NULL;
128 int     badoff = -1;
129 int     closeopen = 0;
130
131
132 void
133 prt(char *fmt, ...)
134 {
135         va_list args;
136
137         va_start(args, fmt);
138         vfprintf(stdout, fmt, args);
139         if (fsxlogf)
140                 vfprintf(fsxlogf, fmt, args);
141         va_end(args);
142 }
143
144 void
145 prterr(char *prefix)
146 {
147         prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
148 }
149
150
151 void
152 log4(int operation, int arg0, int arg1, int arg2)
153 {
154         struct log_entry *le;
155
156         le = &oplog[logptr];
157         le->operation = operation;
158         if (closeopen)
159                 le->operation = ~ le->operation;
160         le->args[0] = arg0;
161         le->args[1] = arg1;
162         le->args[2] = arg2;
163         logptr++;
164         logcount++;
165         if (logptr >= LOGSIZE)
166                 logptr = 0;
167 }
168
169
170 void
171 logdump(void)
172 {
173         int     i, count, down;
174         struct log_entry        *lp;
175
176         prt("LOG DUMP (%d total operations):\n", logcount);
177         if (logcount < LOGSIZE) {
178                 i = 0;
179                 count = logcount;
180         } else {
181                 i = logptr;
182                 count = LOGSIZE;
183         }
184         for ( ; count > 0; count--) {
185                 int opnum;
186
187                 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
188                 prt("%d(%d mod 256): ", opnum, opnum%256);
189                 lp = &oplog[i];
190                 if ((closeopen = lp->operation < 0))
191                         lp->operation = ~ lp->operation;
192                         
193                 switch (lp->operation) {
194                 case OP_MAPREAD:
195                         prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
196                             lp->args[0], lp->args[0] + lp->args[1] - 1,
197                             lp->args[1]);
198                         if (badoff >= lp->args[0] && badoff <
199                                                      lp->args[0] + lp->args[1])
200                                 prt("\t***RRRR***");
201                         break;
202                 case OP_MAPWRITE:
203                         prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
204                             lp->args[0], lp->args[0] + lp->args[1] - 1,
205                             lp->args[1]);
206                         if (badoff >= lp->args[0] && badoff <
207                                                      lp->args[0] + lp->args[1])
208                                 prt("\t******WWWW");
209                         break;
210                 case OP_READ:
211                         prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
212                             lp->args[0], lp->args[0] + lp->args[1] - 1,
213                             lp->args[1]);
214                         if (badoff >= lp->args[0] &&
215                             badoff < lp->args[0] + lp->args[1])
216                                 prt("\t***RRRR***");
217                         break;
218                 case OP_WRITE:
219                         prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
220                             lp->args[0], lp->args[0] + lp->args[1] - 1,
221                             lp->args[1]);
222                         if (lp->args[0] > lp->args[2])
223                                 prt(" HOLE");
224                         else if (lp->args[0] + lp->args[1] > lp->args[2])
225                                 prt(" EXTEND");
226                         if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
227                             badoff < lp->args[0] + lp->args[1])
228                                 prt("\t***WWWW");
229                         break;
230                 case OP_TRUNCATE:
231                         down = lp->args[0] < lp->args[1];
232                         prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
233                             down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
234                         if (badoff >= lp->args[!down] &&
235                             badoff < lp->args[!!down])
236                                 prt("\t******WWWW");
237                         break;
238                 case OP_SKIPPED:
239                         prt("SKIPPED (no operation)");
240                         break;
241                 default:
242                         prt("BOGUS LOG ENTRY (operation code = %d)!",
243                             lp->operation);
244                 }
245                 if (closeopen)
246                         prt("\n\t\tCLOSE/OPEN");
247                 prt("\n");
248                 i++;
249                 if (i == LOGSIZE)
250                         i = 0;
251         }
252 }
253
254
255 void
256 save_buffer(char *buffer, off_t bufferlength, int fd)
257 {
258         off_t ret;
259         ssize_t byteswritten;
260
261         if (fd <= 0 || bufferlength == 0)
262                 return;
263
264         if (bufferlength > SSIZE_MAX) {
265                 prt("fsx flaw: overflow in save_buffer\n");
266                 exit(67);
267         }
268         if (lite) {
269                 off_t size_by_seek = lseek(fd, (off_t)0, L_XTND);
270                 if (size_by_seek == (off_t)-1)
271                         prterr("save_buffer: lseek eof");
272                 else if (bufferlength > size_by_seek) {
273                         fprintf(stderr, "save_buffer: .fsxgood file too short... will save 0x%qx bytes instead of 0x%qx\n", (unsigned long long)size_by_seek,
274                              (unsigned long long)bufferlength);
275                         bufferlength = size_by_seek;
276                 }
277         }
278
279         ret = lseek(fd, (off_t)0, SEEK_SET);
280         if (ret == (off_t)-1)
281                 prterr("save_buffer: lseek 0");
282         
283         byteswritten = write(fd, buffer, (size_t)bufferlength);
284         if (byteswritten != bufferlength) {
285                 if (byteswritten == -1)
286                         prterr("save_buffer write");
287                 else
288                         fprintf(stderr, "save_buffer: short write, 0x%x bytes instead of 0x%qx\n",
289                              (unsigned)byteswritten,
290                              (unsigned long long)bufferlength);
291         }
292 }
293
294
295 void
296 report_failure(int status)
297 {
298         logdump();
299         
300         if (fsxgoodfd) {
301                 if (good_buf) {
302                         save_buffer(good_buf, file_size, fsxgoodfd);
303                         prt("Correct content saved for comparison\n");
304                         prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
305                             fname, fname);
306                 }
307                 close(fsxgoodfd);
308         }
309         exit(status);
310 }
311
312
313 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
314                                         *(((unsigned char *)(cp)) + 1)))
315
316 void
317 check_buffers(unsigned offset, unsigned size)
318 {
319         unsigned char c, t;
320         unsigned i = 0;
321         unsigned n = 0;
322         unsigned op = 0;
323         unsigned bad = 0;
324
325         if (bcmp(good_buf + offset, temp_buf, size) != 0) {
326                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
327                     offset, size);
328                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
329                 while (size > 0) {
330                         c = good_buf[offset];
331                         t = temp_buf[i];
332                         if (c != t) {
333                                 if (n == 0) {
334                                         bad = short_at(&temp_buf[i]);
335                                         prt("0x%5x\t0x%04x\t0x%04x", offset,
336                                             short_at(&good_buf[offset]), bad);
337                                         op = temp_buf[offset & 1 ? i+1 : i];
338                                 }
339                                 n++;
340                                 badoff = offset;
341                         }
342                         offset++;
343                         i++;
344                         size--;
345                 }
346                 if (n) {
347                         prt("\t0x%5x\n", n);
348                         if (bad)
349                                 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
350                         else
351                                 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
352                 } else
353                         prt("????????????????\n");
354                 report_failure(110);
355         }
356 }
357
358
359 void
360 check_size(void)
361 {
362         struct stat     statbuf;
363         off_t   size_by_seek;
364
365         if (fstat(fd, &statbuf)) {
366                 prterr("check_size: fstat");
367                 statbuf.st_size = -1;
368         }
369         size_by_seek = lseek(fd, (off_t)0, L_XTND);
370         if (file_size != statbuf.st_size || file_size != size_by_seek) {
371                 prt("Size error: expected 0x%qx stat 0x%qx seek 0x%qx\n",
372                     (unsigned long long)file_size,
373                     (unsigned long long)statbuf.st_size,
374                     (unsigned long long)size_by_seek);
375                 report_failure(120);
376         }
377 }
378
379
380 void
381 check_trunc_hack(void)
382 {
383         struct stat statbuf;
384
385         ftruncate(fd, (off_t)0);
386         ftruncate(fd, (off_t)100000);
387         fstat(fd, &statbuf);
388         if (statbuf.st_size != (off_t)100000) {
389                 prt("no extend on truncate! not posix!\n");
390                 exit(130);
391         }
392         ftruncate(fd, 0);
393 }
394
395
396 void
397 doread(unsigned offset, unsigned size)
398 {
399         off_t ret;
400         unsigned iret;
401
402         offset -= offset % readbdy;
403         if (size == 0) {
404                 if (!quiet && testcalls > simulatedopcount)
405                         prt("skipping zero size read\n");
406                 log4(OP_SKIPPED, OP_READ, offset, size);
407                 return;
408         }
409         if (size + offset > file_size) {
410                 if (!quiet && testcalls > simulatedopcount)
411                         prt("skipping seek/read past end of file\n");
412                 log4(OP_SKIPPED, OP_READ, offset, size);
413                 return;
414         }
415
416         log4(OP_READ, offset, size, 0);
417
418         if (testcalls <= simulatedopcount)
419                 return;
420
421         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
422                        (debug &&
423                         (monitorstart == -1 ||
424                          (offset + size > monitorstart &&
425                           (monitorend == -1 || offset <= monitorend))))))
426                 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
427                     offset, offset + size - 1, size);
428         ret = lseek(fd, (off_t)offset, SEEK_SET);
429         if (ret == (off_t)-1) {
430                 prterr("doread: lseek");
431                 report_failure(140);
432         }
433         iret = read(fd, temp_buf, size);
434         if (iret != size) {
435                 if (iret == -1)
436                         prterr("doread: read");
437                 else
438                         prt("short read: 0x%x bytes instead of 0x%x\n",
439                             iret, size);
440                 report_failure(141);
441         }
442         check_buffers(offset, size);
443 }
444
445
446 void
447 domapread(unsigned offset, unsigned size)
448 {
449         unsigned pg_offset;
450         unsigned map_size;
451         char    *p;
452
453         offset -= offset % readbdy;
454         if (size == 0) {
455                 if (!quiet && testcalls > simulatedopcount)
456                         prt("skipping zero size read\n");
457                 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
458                 return;
459         }
460         if (size + offset > file_size) {
461                 if (!quiet && testcalls > simulatedopcount)
462                         prt("skipping seek/read past end of file\n");
463                 log4(OP_SKIPPED, OP_MAPREAD, offset, size);
464                 return;
465         }
466
467         log4(OP_MAPREAD, offset, size, 0);
468
469         if (testcalls <= simulatedopcount)
470                 return;
471
472         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
473                        (debug &&
474                         (monitorstart == -1 ||
475                          (offset + size > monitorstart &&
476                           ((monitorend == -1 || offset <= monitorend)))))))
477                 prt("%lu mapread\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
478                     offset, offset + size - 1, size);
479
480         pg_offset = offset & PAGE_MASK;
481         map_size  = pg_offset + size;
482
483         if ((p = (char *)mmap(0, map_size, PROT_READ, MAP_SHARED, fd,
484                               (off_t)(offset - pg_offset))) == (char *)-1) {
485                 prterr("domapread: mmap");
486                 report_failure(190);
487         }
488         memcpy(temp_buf, p + pg_offset, size);
489         if (munmap(p, map_size) != 0) {
490                 prterr("domapread: munmap");
491                 report_failure(191);
492         }
493
494         check_buffers(offset, size);
495 }
496
497
498 void
499 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
500 {
501         while (size--) {
502                 good_buf[offset] = testcalls % 256; 
503                 if (offset % 2)
504                         good_buf[offset] += original_buf[offset];
505                 offset++;
506         }
507 }
508
509
510 void
511 dowrite(unsigned offset, unsigned size)
512 {
513         off_t ret;
514         unsigned iret;
515
516         offset -= offset % writebdy;
517         if (size == 0) {
518                 if (!quiet && testcalls > simulatedopcount)
519                         prt("skipping zero size write\n");
520                 log4(OP_SKIPPED, OP_WRITE, offset, size);
521                 return;
522         }
523
524         log4(OP_WRITE, offset, size, file_size);
525
526         gendata(original_buf, good_buf, offset, size);
527         if (file_size < offset + size) {
528                 if (file_size < offset)
529                         bzero(good_buf + file_size, offset - file_size);
530                 file_size = offset + size;
531                 if (lite) {
532                         fprintf(stderr, "Lite file size bug in fsx!\n");
533                         report_failure(149);
534                 }
535         }
536
537         if (testcalls <= simulatedopcount)
538                 return;
539         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
540                        (debug &&
541                         (monitorstart == -1 ||
542                          (offset + size > monitorstart &&
543                           ((monitorend == -1 || offset <= monitorend)))))))
544                 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
545                     offset, offset + size - 1, size);
546         ret = lseek(fd, (off_t)offset, SEEK_SET);
547         if (ret == (off_t)-1) {
548                 prterr("dowrite: lseek");
549                 report_failure(150);
550         }
551         iret = write(fd, good_buf + offset, size);
552         if (iret != size) {
553                 if (iret == -1)
554                         prterr("dowrite: write");
555                 else
556                         prt("short write: 0x%x bytes instead of 0x%x\n",
557                             iret, size);
558                 report_failure(151);
559         }
560 }
561
562
563 void
564 domapwrite(unsigned offset, unsigned size)
565 {
566         unsigned pg_offset;
567         unsigned map_size;
568         off_t    cur_filesize;
569         char    *p;
570
571         offset -= offset % writebdy;
572         if (size == 0) {
573                 if (!quiet && testcalls > simulatedopcount)
574                         prt("skipping zero size write\n");
575                 log4(OP_SKIPPED, OP_MAPWRITE, offset, size);
576                 return;
577         }
578         cur_filesize = file_size;
579
580         log4(OP_MAPWRITE, offset, size, 0);
581
582         gendata(original_buf, good_buf, offset, size);
583         if (file_size < offset + size) {
584                 if (file_size < offset)
585                         bzero(good_buf + file_size, offset - file_size);
586                 file_size = offset + size;
587                 if (lite) {
588                         fprintf(stderr, "Lite file size bug in fsx!\n");
589                         report_failure(200);
590                 }
591         }
592
593         if (testcalls <= simulatedopcount)
594                 return;
595
596         if (!quiet && ((progressinterval && !(testcalls % progressinterval)) ||
597                        (debug &&
598                         (monitorstart == -1 ||
599                          (offset + size > monitorstart &&
600                           (monitorend == -1 || offset <= monitorend))))))
601                 prt("%lu mapwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
602                     offset, offset + size - 1, size);
603
604         if (file_size > cur_filesize) {
605                 if (ftruncate(fd, file_size) == -1) {
606                         prterr("domapwrite: ftruncate");
607                         exit(201);
608                 }
609         }
610         pg_offset = offset & PAGE_MASK;
611         map_size  = pg_offset + size;
612
613         if ((p = (char *)mmap(0, map_size, PROT_READ | PROT_WRITE,
614                               MAP_FILE | MAP_SHARED, fd,
615                               (off_t)(offset - pg_offset))) == (char *)-1) {
616                 prterr("domapwrite: mmap");
617                 report_failure(202);
618         }
619         memcpy(p + pg_offset, good_buf + offset, size);
620         if (msync(p, map_size, 0) != 0) {
621                 prterr("domapwrite: msync");
622                 report_failure(203);
623         }
624         if (munmap(p, map_size) != 0) {
625                 prterr("domapwrite: munmap");
626                 report_failure(204);
627         }
628 }
629
630
631 void
632 dotruncate(unsigned size)
633 {
634         int oldsize = file_size;
635
636         size -= size % truncbdy;
637         if (size > biggest) {
638                 biggest = size;
639                 if (!quiet && testcalls > simulatedopcount)
640                         prt("truncating to largest ever: 0x%x\n", size);
641         }
642
643         log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
644
645         if (size > file_size)
646                 bzero(good_buf + file_size, size - file_size);
647         file_size = size;
648
649         if (testcalls <= simulatedopcount)
650                 return;
651         
652         if ((progressinterval && !(testcalls % progressinterval)) ||
653             (debug && (monitorstart == -1 || monitorend == -1 ||
654                       size <= monitorend)))
655                 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
656         if (ftruncate(fd, (off_t)size) == -1) {
657                 prt("ftruncate1: %x\n", size);
658                 prterr("dotruncate: ftruncate");
659                 report_failure(160);
660         }
661 }
662
663
664 void
665 writefileimage()
666 {
667         ssize_t iret;
668
669         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
670                 prterr("writefileimage: lseek");
671                 report_failure(171);
672         }
673         iret = write(fd, good_buf, file_size);
674         if ((off_t)iret != file_size) {
675                 if (iret == -1)
676                         prterr("writefileimage: write");
677                 else
678                         prt("short write: 0x%x bytes instead of 0x%qx\n",
679                             iret, (unsigned long long)file_size);
680                 report_failure(172);
681         }
682         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
683                 prt("ftruncate2: %qx\n", (unsigned long long)file_size);
684                 prterr("writefileimage: ftruncate");
685                 report_failure(173);
686         }
687 }
688
689
690 void
691 docloseopen(void)
692
693         if (testcalls <= simulatedopcount)
694                 return;
695
696         if (debug)
697                 prt("%lu close/open\n", testcalls);
698         if (close(fd)) {
699                 prterr("docloseopen: close");
700                 report_failure(180);
701         }
702         fd = open(fname, O_RDWR, 0);
703         if (fd < 0) {
704                 prterr("docloseopen: open");
705                 report_failure(181);
706         }
707 }
708
709
710 void
711 test(void)
712 {
713         unsigned long   offset;
714         unsigned long   size = maxoplen;
715         unsigned long   rv = random();
716         unsigned long   op = rv % (3 + !lite + mapped_writes);
717
718         /* turn off the map read if necessary */
719
720         if (op == 2 && !mapped_reads)
721             op = 0;
722
723         if (simulatedopcount > 0 && testcalls == simulatedopcount)
724                 writefileimage();
725
726         testcalls++;
727
728         if (closeprob)
729                 closeopen = (rv >> 3) < (1 << 28) / closeprob;
730
731         if (debugstart > 0 && testcalls >= debugstart)
732                 debug = 1;
733
734         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
735                 prt("%lu...\n", testcalls);
736
737         /*
738          * READ:        op = 0
739          * WRITE:       op = 1
740          * MAPREAD:     op = 2
741          * TRUNCATE:    op = 3
742          * MAPWRITE:    op = 3 or 4
743          */
744         if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
745                 dotruncate(random() % maxfilelen);
746         else {
747                 if (randomoplen)
748                         size = random() % (maxoplen+1);
749                 if (lite ? 0 : op == 3)
750                         dotruncate(size);
751                 else {
752                         offset = random();
753                         if (op == 1 || op == (lite ? 3 : 4)) {
754                                 offset %= maxfilelen;
755                                 if (offset + size > maxfilelen)
756                                         size = maxfilelen - offset;
757                                 if (op != 1)
758                                         domapwrite(offset, size);
759                                 else
760                                         dowrite(offset, size);
761                         } else {
762                                 if (file_size)
763                                         offset %= file_size;
764                                 else
765                                         offset = 0;
766                                 if (offset + size > file_size)
767                                         size = file_size - offset;
768                                 if (op != 0)
769                                         domapread(offset, size);
770                                 else
771                                         doread(offset, size);
772                         }
773                 }
774         }
775         if (sizechecks && testcalls > simulatedopcount)
776                 check_size();
777         if (closeopen)
778                 docloseopen();
779 }
780
781
782 void
783 cleanup(sig)
784         int     sig;
785 {
786         if (sig)
787                 prt("signal %d\n", sig);
788         prt("testcalls = %lu\n", testcalls);
789         exit(sig);
790 }
791
792
793 void
794 usage(void)
795 {
796         fprintf(stdout, "usage: %s",
797                 "fsx [-dnqLOW] [-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\
798         -b opnum: beginning operation number (default 1)\n\
799         -c P: 1 in P chance of file close+open at each op (default infinity)\n\
800         -d: debug output for all operations\n\
801         -l flen: the upper bound on file size (default 262144)\n\
802         -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
803         -n: no verifications of file size\n\
804         -o oplen: the upper bound on operation size (default 65536)\n\
805         -p progressinterval: debug output at specified operation interval\n\
806         -q: quieter operation\n\
807         -r readbdy: 4096 would make reads page aligned (default 1)\n\
808         -s style: 1 gives smaller truncates (default 0)\n\
809         -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
810         -w writebdy: 4096 would make writes page aligned (default 1)\n\
811         -x: preallocate file space before starting, XFS only (default 0)\n\
812         -D startingop: debug output starting at specified operation\n\
813         -L: fsxLite - no file creations & no file size changes\n\
814         -N numops: total # operations to do (default infinity)\n\
815         -O: use oplen (see -o flag) for every op (default random)\n\
816         -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
817         -S seed: for random # generator (default 1) 0 gets timestamp\n\
818         -W: mapped write operations DISabled\n\
819         -R: read() system calls only (mapped reads disabled)\n\
820         fname: this filename is REQUIRED (no default)\n");
821         exit(90);
822 }
823
824
825 int
826 getnum(char *s, char **e)
827 {
828         int ret = -1;
829
830         *e = (char *) 0;
831         ret = strtol(s, e, 0);
832         if (*e)
833                 switch (**e) {
834                 case 'b':
835                 case 'B':
836                         ret *= 512;
837                         *e = *e + 1;
838                         break;
839                 case 'k':
840                 case 'K':
841                         ret *= 1024;
842                         *e = *e + 1;
843                         break;
844                 case 'm':
845                 case 'M':
846                         ret *= 1024*1024;
847                         *e = *e + 1;
848                         break;
849                 case 'w':
850                 case 'W':
851                         ret *= 4;
852                         *e = *e + 1;
853                         break;
854                 }
855         return (ret);
856 }
857
858
859 int
860 main(int argc, char **argv)
861 {
862         int     i, style, ch;
863         char    *endp;
864         char goodfile[1024];
865         char logfile[1024];
866
867         goodfile[0] = 0;
868         logfile[0] = 0;
869
870         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
871
872         while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:xD:LN:OP:RS:W"))
873                != EOF)
874                 switch (ch) {
875                 case 'b':
876                         simulatedopcount = getnum(optarg, &endp);
877                         if (!quiet)
878                                 fprintf(stdout, "Will begin at operation %ld\n",
879                                         simulatedopcount);
880                         if (simulatedopcount == 0)
881                                 usage();
882                         simulatedopcount -= 1;
883                         break;
884                 case 'c':
885                         closeprob = getnum(optarg, &endp);
886                         if (!quiet)
887                                 fprintf(stdout,
888                                         "Chance of close/open is 1 in %d\n",
889                                         closeprob);
890                         if (closeprob <= 0)
891                                 usage();
892                         break;
893                 case 'd':
894                         debug = 1;
895                         break;
896                 case 'l':
897                         maxfilelen = getnum(optarg, &endp);
898                         if (maxfilelen <= 0)
899                                 usage();
900                         break;
901                 case 'm':
902                         monitorstart = getnum(optarg, &endp);
903                         if (monitorstart < 0)
904                                 usage();
905                         if (!endp || *endp++ != ':')
906                                 usage();
907                         monitorend = getnum(endp, &endp);
908                         if (monitorend < 0)
909                                 usage();
910                         if (monitorend == 0)
911                                 monitorend = -1; /* aka infinity */
912                         debug = 1;
913                 case 'n':
914                         sizechecks = 0;
915                         break;
916                 case 'o':
917                         maxoplen = getnum(optarg, &endp);
918                         if (maxoplen <= 0)
919                                 usage();
920                         break;
921                 case 'p':
922                         progressinterval = getnum(optarg, &endp);
923                         if (progressinterval < 0)
924                                 usage();
925                         break;
926                 case 'q':
927                         quiet = 1;
928                         break;
929                 case 'r':
930                         readbdy = getnum(optarg, &endp);
931                         if (readbdy <= 0)
932                                 usage();
933                         break;
934                 case 's':
935                         style = getnum(optarg, &endp);
936                         if (style < 0 || style > 1)
937                                 usage();
938                         break;
939                 case 't':
940                         truncbdy = getnum(optarg, &endp);
941                         if (truncbdy <= 0)
942                                 usage();
943                         break;
944                 case 'w':
945                         writebdy = getnum(optarg, &endp);
946                         if (writebdy <= 0)
947                                 usage();
948                         break;
949                 case 'x':
950                         prealloc = 1;
951                         break;
952                 case 'D':
953                         debugstart = getnum(optarg, &endp);
954                         if (debugstart < 1)
955                                 usage();
956                         break;
957                 case 'L':
958                         lite = 1;
959                         break;
960                 case 'N':
961                         numops = getnum(optarg, &endp);
962                         if (numops < 0)
963                                 usage();
964                         break;
965                 case 'O':
966                         randomoplen = 0;
967                         break;
968                 case 'P':
969                         strncpy(goodfile, optarg, sizeof(goodfile));
970                         strcat(goodfile, "/");
971                         strncpy(logfile, optarg, sizeof(logfile));
972                         strcat(logfile, "/");
973                         break;
974                 case 'R':
975                         mapped_reads = 0;
976                         break;
977                 case 'S':
978                         seed = getnum(optarg, &endp);
979                         if (seed == 0)
980                                 seed = time(0) % 10000;
981                         if (!quiet)
982                                 fprintf(stdout, "Seed set to %d\n", seed);
983                         if (seed < 0)
984                                 usage();
985                         break;
986                 case 'W':
987                         mapped_writes = 0;
988                         if (!quiet)
989                                 fprintf(stdout, "mapped writes DISABLED\n");
990                         break;
991               
992                 default:
993                         usage();
994                         /* NOTREACHED */
995                 }
996         argc -= optind;
997         argv += optind;
998         if (argc != 1)
999                 usage();
1000         fname = argv[0];
1001
1002
1003         signal(SIGHUP,  cleanup);
1004         signal(SIGINT,  cleanup);
1005         signal(SIGPIPE, cleanup);
1006         signal(SIGALRM, cleanup);
1007         signal(SIGTERM, cleanup);
1008         signal(SIGXCPU, cleanup);
1009         signal(SIGXFSZ, cleanup);
1010         signal(SIGVTALRM,       cleanup);
1011         signal(SIGUSR1, cleanup);
1012         signal(SIGUSR2, cleanup);
1013
1014         initstate(seed, state, 256);
1015         setstate(state);
1016         fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
1017         if (fd < 0) {
1018                 prterr(fname);
1019                 exit(91);
1020         }
1021         if (prealloc) {
1022                 xfs_flock64_t   resv = { 0 };
1023
1024                 if (!platform_test_xfs_fd(fd)) {
1025                         prterr(fname);
1026                         fprintf(stderr, "main: cannot prealloc, non XFS\n");
1027                         exit(96);
1028                 }
1029
1030                 resv.l_len = maxfilelen;
1031                 if ((xfsctl(fname, fd, XFS_IOC_RESVSP, &resv)) < 0) {
1032                         prterr(fname);
1033                         exit(97);
1034                 }
1035         }
1036         strncat(goodfile, fname, 256);
1037         strcat (goodfile, ".fsxgood");
1038         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1039         if (fsxgoodfd < 0) {
1040                 prterr(goodfile);
1041                 exit(92);
1042         }
1043         strncat(logfile, fname, 256);
1044         strcat (logfile, ".fsxlog");
1045         fsxlogf = fopen(logfile, "w");
1046         if (fsxlogf == NULL) {
1047                 prterr(logfile);
1048                 exit(93);
1049         }
1050         if (lite) {
1051                 off_t ret;
1052                 file_size = maxfilelen = lseek(fd, (off_t)0, L_XTND);
1053                 if (file_size == (off_t)-1) {
1054                         prterr(fname);
1055                         fprintf(stderr, "main: lseek eof\n");
1056                         exit(94);
1057                 }
1058                 ret = lseek(fd, (off_t)0, SEEK_SET);
1059                 if (ret == (off_t)-1) {
1060                         prterr(fname);
1061                         fprintf(stderr, "main: lseek 0\n");
1062                         exit(95);
1063                 }
1064         }
1065         original_buf = (char *) malloc(maxfilelen);
1066         for (i = 0; i < maxfilelen; i++)
1067                 original_buf[i] = random() % 256;
1068         good_buf = (char *) malloc(maxfilelen);
1069         bzero(good_buf, maxfilelen);
1070         temp_buf = (char *) malloc(maxoplen);
1071         bzero(temp_buf, maxoplen);
1072         if (lite) {     /* zero entire existing file */
1073                 ssize_t written;
1074
1075                 written = write(fd, good_buf, (size_t)maxfilelen);
1076                 if (written != maxfilelen) {
1077                         if (written == -1) {
1078                                 prterr(fname);
1079                                 fprintf(stderr, "main: error on write\n");
1080                         } else
1081                                 fprintf(stderr, "main: short write, 0x%x bytes instead of 0x%lx\n",
1082                                      (unsigned)written, maxfilelen);
1083                         exit(98);
1084                 }
1085         } else 
1086                 check_trunc_hack();
1087
1088         while (numops == -1 || numops--)
1089                 test();
1090
1091         if (close(fd)) {
1092                 prterr("close");
1093                 report_failure(99);
1094         }
1095         prt("All operations completed A-OK!\n");
1096
1097         exit(0);
1098         return 0;
1099 }