Whamcloud - gitweb
LU-6158 mdt: always shrink_capsule in getxattr_all
[fs/lustre-release.git] / lustre / tests / fsx.c
1 /*
2  * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
3  *
4  * Copyright (c) 2012, Intel Corporation.
5  *
6  * @APPLE_LICENSE_HEADER_START@
7  *
8  * The contents of this file constitute Original Code as defined in and
9  * are subject to the Apple Public Source License Version 1.1 (the
10  * "License").  You may not use this file except in compliance with the
11  * License.  Please obtain a copy of the License at
12  * http://www.apple.com/publicsource and read it before using this file.
13  *
14  * This Original Code and all software distributed under the License are
15  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19  * License for the specific language governing rights and limitations
20  * under the License.
21  *
22  * @APPLE_LICENSE_HEADER_END@
23  *
24  *      File:   fsx.c
25  *      Author: Avadis Tevanian, Jr.
26  *
27  *      File system exerciser.
28  *
29  *      Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
30  *
31  *      Various features from Joe Sokol, Pat Dirks, and Clark Warner.
32  *
33  *      Small changes to work under Linux -- davej@suse.de
34  *
35  *      Sundry porting patches from Guy Harris 12/2001
36  * $FreeBSD: src/tools/regression/fsx/fsx.c,v 1.1 2001/12/20 04:15:57 jkh Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #if defined(_UWIN) || defined(__linux__)
42 # include <sys/param.h>
43 # include <limits.h>
44 # include <time.h>
45 # include <strings.h>
46 # include <sys/time.h>
47 #endif
48 #include <fcntl.h>
49 #include <sys/mman.h>
50 #ifndef MAP_FILE
51 # define MAP_FILE 0
52 #endif
53 #include <limits.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <stdarg.h>
60 #include <errno.h>
61
62 #define NUMPRINTCOLUMNS 32      /* # columns of data to print on each line */
63
64 /*
65  * Each test run will work with one or more separate file descriptors for the
66  * same file.  This allows testing cache coherency across multiple mountpoints
67  * of the same network filesystem on a single client.
68  */
69 struct test_file {
70         char *path;
71         int fd;
72 } *test_files = NULL;
73
74 int num_test_files;
75 enum fd_iteration_policy {
76         FD_SINGLE,
77         FD_ROTATE,
78         FD_RANDOM,
79 };
80 int fd_policy = FD_RANDOM;
81 int fd_last;
82
83 /*
84  *      A log entry is an operation and a bunch of arguments.
85  */
86
87 struct log_entry {
88         int                     operation;
89         int                     args[3];
90         struct timeval          tv;
91         const struct test_file  *tf;
92 };
93
94 #define LOGSIZE 100000
95
96 struct log_entry        oplog[LOGSIZE]; /* the log */
97 int                     logptr = 0;     /* current position in log */
98 int                     logcount = 0;   /* total ops */
99
100 /*
101  *      Define operations
102  */
103
104 #define OP_READ         1
105 #define OP_WRITE        2
106 #define OP_TRUNCATE     3
107 #define OP_CLOSEOPEN    4
108 #define OP_MAPREAD      5
109 #define OP_MAPWRITE     6
110 #define OP_SKIPPED      7
111
112 int page_size;
113 int page_mask;
114
115 char    *original_buf;                  /* a pointer to the original data */
116 char    *good_buf;                      /* a pointer to the correct data */
117 char    *temp_buf;                      /* a pointer to the current data */
118 char    *fname;                         /* name of our test file */
119 char    logfile[1024];                  /* name of our log file */
120 char    goodfile[1024];                 /* name of our test file */
121
122 off_t           file_size = 0;
123 off_t           biggest = 0;
124 char            state[256];
125 unsigned long   testcalls = 0;          /* calls to function "test" */
126
127 long    simulatedopcount = 0;           /* -b flag */
128 int     closeprob = 0;                  /* -c flag */
129 int     debug = 0;                      /* -d flag */
130 long    debugstart = 0;                 /* -D flag */
131 long    maxfilelen = 256 * 1024;        /* -l flag */
132 int     sizechecks = 1;                 /* -n flag disables them */
133 int     maxoplen = 64 * 1024;           /* -o flag */
134 int     quiet = 0;                      /* -q flag */
135 long    progressinterval = 0;           /* -p flag */
136 int     readbdy = 1;                    /* -r flag */
137 int     style = 0;                      /* -s flag */
138 int     truncbdy = 1;                   /* -t flag */
139 int     writebdy = 1;                   /* -w flag */
140 long    monitorstart = -1;              /* -m flag */
141 long    monitorend = -1;                /* -m flag */
142 int     lite = 0;                       /* -L flag */
143 long    numops = -1;                    /* -N flag */
144 int     randomoplen = 1;                /* -O flag disables it */
145 int     seed = 1;                       /* -S flag */
146 int     mapped_writes = 1;              /* -W flag disables */
147 int     mapped_reads = 1;               /* -R flag disables it */
148 int     fsxgoodfd = 0;
149 FILE *  fsxlogf = NULL;
150 int badoff = -1;
151
152
153 void
154 vwarnc(code, fmt, ap)
155         int code;
156         const char *fmt;
157         va_list ap;
158 {
159         fprintf(stderr, "fsx: ");
160         if (fmt != NULL) {
161                 vfprintf(stderr, fmt, ap);
162                 fprintf(stderr, ": ");
163         }
164         fprintf(stderr, "%s\n", strerror(code));
165 }
166
167
168 void
169 warn(const char * fmt, ...)
170 {
171         va_list ap;
172         va_start(ap, fmt);
173         vwarnc(errno, fmt, ap);
174         va_end(ap);
175 }
176
177
178 void
179 __attribute__((format(printf, 1, 2)))
180 prt(char *fmt, ...)
181 {
182         va_list args;
183
184         va_start(args, fmt);
185         vfprintf(stdout, fmt, args);
186         va_end(args);
187
188         if (fsxlogf) {
189                 va_start(args, fmt);
190                 vfprintf(fsxlogf, fmt, args);
191                 va_end(args);
192         }
193 }
194
195 void
196 prterr(char *prefix)
197 {
198         prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
199 }
200
201
202 void
203 log4(int operation, int arg0, int arg1, int arg2, struct timeval *tv,
204         const struct test_file *tf)
205 {
206         struct log_entry *le;
207
208         le = &oplog[logptr];
209         le->operation = operation;
210         le->args[0] = arg0;
211         le->args[1] = arg1;
212         le->args[2] = arg2;
213         le->tv = *tv;
214         le->tf = tf;
215         logptr++;
216         logcount++;
217         if (logptr >= LOGSIZE)
218                 logptr = 0;
219 }
220
221 const char *
222 fill_tf_buf(const struct test_file *tf)
223 {
224         static int max_tf_len;
225         static char tf_buf[32];
226
227         if (fd_policy == FD_SINGLE)
228                 return "";
229
230         if (max_tf_len == 0)
231                 max_tf_len = snprintf(tf_buf, sizeof(tf_buf) - 1,
232                                       "%u", num_test_files - 1);
233
234         sprintf(tf_buf, "[%0*lu]", max_tf_len,
235                 (unsigned long)(tf - test_files));
236
237         return tf_buf;
238 }
239
240 void
241 logdump(void)
242 {
243         int     i, count, down;
244         struct log_entry        *lp;
245
246         prt("LOG DUMP (%d total operations):\n", logcount);
247         if (logcount < LOGSIZE) {
248                 i = 0;
249                 count = logcount;
250         } else {
251                 i = logptr;
252                 count = LOGSIZE;
253         }
254         for ( ; count > 0; count--) {
255                 int opnum;
256
257                 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
258                 lp = &oplog[i];
259                 prt("%d%s: %lu.%06lu ", opnum, fill_tf_buf(lp->tf),
260                     lp->tv.tv_sec, lp->tv.tv_usec);
261
262                 switch (lp->operation) {
263                 case OP_MAPREAD:
264                         prt("MAPREAD  0x%x thru 0x%x (0x%x bytes)",
265                             lp->args[0], lp->args[0] + lp->args[1] - 1,
266                             lp->args[1]);
267                         if (badoff >= lp->args[0] && badoff <
268                                                      lp->args[0] + lp->args[1])
269                                 prt("\t***RRRR***");
270                         break;
271                 case OP_MAPWRITE:
272                         prt("MAPWRITE 0x%x thru 0x%x (0x%x bytes)",
273                             lp->args[0], lp->args[0] + lp->args[1] - 1,
274                             lp->args[1]);
275                         if (badoff >= lp->args[0] && badoff <
276                                                      lp->args[0] + lp->args[1])
277                                 prt("\t******WWWW");
278                         break;
279                 case OP_READ:
280                         prt("READ     0x%x thru 0x%x (0x%x bytes)",
281                             lp->args[0], lp->args[0] + lp->args[1] - 1,
282                             lp->args[1]);
283                         if (badoff >= lp->args[0] &&
284                             badoff < lp->args[0] + lp->args[1])
285                                 prt("\t***RRRR***");
286                         break;
287                 case OP_WRITE:
288                         prt("WRITE    0x%x thru 0x%x (0x%x bytes)",
289                             lp->args[0], lp->args[0] + lp->args[1] - 1,
290                             lp->args[1]);
291                         if (lp->args[0] > lp->args[2])
292                                 prt(" HOLE");
293                         else if (lp->args[0] + lp->args[1] > lp->args[2])
294                                 prt(" EXTEND");
295                         if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
296                             badoff < lp->args[0] + lp->args[1])
297                                 prt("\t***WWWW");
298                         break;
299                 case OP_TRUNCATE:
300                         down = lp->args[0] < lp->args[1];
301                         prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
302                             down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
303                         if (badoff >= lp->args[!down] &&
304                             badoff < lp->args[!!down])
305                                 prt("\t******WWWW");
306                         break;
307                 case OP_CLOSEOPEN:
308                         prt("CLOSE/OPEN");
309                         break;
310                 case OP_SKIPPED:
311                         prt("SKIPPED (no operation)");
312                         break;
313                 default:
314                         prt("BOGUS LOG ENTRY (operation code = %d)!",
315                             lp->operation);
316                 }
317                 prt("\n");
318                 i++;
319                 if (i == LOGSIZE)
320                         i = 0;
321         }
322 }
323
324
325 void
326 save_buffer(char *buffer, off_t bufferlength, int fd)
327 {
328         off_t ret;
329         ssize_t byteswritten;
330
331         if (fd <= 0 || bufferlength == 0)
332                 return;
333
334         if (bufferlength > SSIZE_MAX) {
335                 prt("fsx flaw: overflow in save_buffer\n");
336                 exit(67);
337         }
338         if (lite) {
339                 off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END);
340                 if (size_by_seek == (off_t)-1)
341                         prterr("save_buffer: lseek eof");
342                 else if (bufferlength > size_by_seek) {
343                         warn("save_buffer: .fsxgood file too short... will"
344                                 "save 0x%llx bytes instead of 0x%llx\n",
345                                 (unsigned long long)size_by_seek,
346                                 (unsigned long long)bufferlength);
347                         bufferlength = size_by_seek;
348                 }
349         }
350
351         ret = lseek(fd, (off_t)0, SEEK_SET);
352         if (ret == (off_t)-1)
353                 prterr("save_buffer: lseek 0");
354
355         byteswritten = write(fd, buffer, (size_t)bufferlength);
356         if (byteswritten != bufferlength) {
357                 if (byteswritten == -1)
358                         prterr("save_buffer write");
359                 else
360                         warn("save_buffer: short write, 0x%x bytes instead"
361                                 "of 0x%llx\n",
362                              (unsigned)byteswritten,
363                              (unsigned long long)bufferlength);
364         }
365 }
366
367
368 void
369 report_failure(int status)
370 {
371         logdump();
372
373         if (fsxgoodfd) {
374                 if (good_buf) {
375                         save_buffer(good_buf, file_size, fsxgoodfd);
376                         prt("Correct content saved for comparison\n");
377                         prt("(maybe hexdump \"%s\" vs \"%s\")\n",
378                             fname, goodfile);
379                 }
380                 close(fsxgoodfd);
381         }
382         exit(status);
383 }
384
385
386 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
387                                         *(((unsigned char *)(cp)) + 1)))
388
389 void
390 check_buffers(unsigned offset, unsigned size)
391 {
392         unsigned char c, t;
393         unsigned i = 0;
394         unsigned n = 0;
395         unsigned op = 0;
396         unsigned bad = 0;
397
398         if (memcmp(good_buf + offset, temp_buf, size) != 0) {
399                 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
400                     offset, size);
401                 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
402                 while (size > 0) {
403                         c = good_buf[offset];
404                         t = temp_buf[i];
405                         if (c != t) {
406                                 if (n == 0) {
407                                         bad = short_at(&temp_buf[i]);
408                                         prt("%#07x\t%#06x\t%#06x", offset,
409                                             short_at(&good_buf[offset]), bad);
410                                         op = temp_buf[offset & 1 ? i+1 : i];
411                                 }
412                                 n++;
413                                 badoff = offset;
414                         }
415                         offset++;
416                         i++;
417                         size--;
418                 }
419                 if (n) {
420                         prt("\t%#7x\n", n);
421                         if (bad)
422                                 prt("operation# (mod 256) for the bad data"
423                                         "may be %u\n", ((unsigned)op & 0xff));
424                         else
425                                 prt("operation# (mod 256) for the bad data"
426                                         "unknown, check HOLE and EXTEND ops\n");
427                 } else
428                         prt("????????????????\n");
429                 report_failure(110);
430         }
431 }
432
433 struct test_file *
434 get_tf(void)
435 {
436         unsigned index = 0;
437
438         switch (fd_policy) {
439                 case FD_ROTATE:
440                         index = fd_last++;
441                         break;
442                 case FD_RANDOM:
443                         index = random();
444                         break;
445                 case FD_SINGLE:
446                         index = 0;
447                         break;
448                 default:
449                         prt("unknown policy");
450                         exit(1);
451                         break;
452         }
453         return &test_files[ index % num_test_files ];
454 }
455
456 void
457 assign_fd_policy(char *policy)
458 {
459         if (!strcmp(policy, "random"))
460                 fd_policy = FD_RANDOM;
461         else if (!strcmp(policy, "rotate"))
462                 fd_policy = FD_ROTATE;
463         else {
464                 prt("unknown -I policy: '%s'\n", policy);
465                 exit(1);
466         }
467 }
468
469 int
470 get_fd(void)
471 {
472         struct test_file *tf = get_tf();
473         return tf->fd;
474 }
475
476 static const char *my_basename(const char *path)
477 {
478         char *c = strrchr(path, '/');
479
480         return c ? c++ : path;
481 }
482
483 void
484 open_test_files(char **argv, int argc)
485 {
486         struct test_file *tf;
487         int i;
488
489         num_test_files = argc;
490         if (num_test_files == 1)
491                 fd_policy = FD_SINGLE;
492
493         test_files = calloc(num_test_files, sizeof(*test_files));
494         if (test_files == NULL) {
495                 prterr("reallocating space for test files");
496                 exit(1);
497         }
498
499         for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
500
501                 tf->path = argv[i];
502                 tf->fd = open(tf->path, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC),
503                                 0666);
504                 if (tf->fd < 0) {
505                         prterr(tf->path);
506                         exit(91);
507                 }
508         }
509
510         if (quiet || fd_policy == FD_SINGLE)
511                 return;
512
513         for (i = 0, tf = test_files; i < num_test_files; i++, tf++)
514                 prt("fd %d: %s\n", i, tf->path);
515 }
516
517 void
518 close_test_files(void)
519 {
520         int i;
521         struct test_file *tf;
522
523         for (i = 0, tf = test_files; i < num_test_files; i++, tf++) {
524                 if (close(tf->fd)) {
525                         prterr("close");
526                         report_failure(99);
527                 }
528         }
529 }
530
531
532 void
533 check_size(void)
534 {
535         struct stat     statbuf;
536         off_t   size_by_seek;
537         int fd = get_fd();
538
539         if (fstat(fd, &statbuf)) {
540                 prterr("check_size: fstat");
541                 statbuf.st_size = -1;
542         }
543         size_by_seek = lseek(fd, (off_t)0, SEEK_END);
544         if (file_size != statbuf.st_size || file_size != size_by_seek) {
545                 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
546                     (unsigned long long)file_size,
547                     (unsigned long long)statbuf.st_size,
548                     (unsigned long long)size_by_seek);
549                 report_failure(120);
550         }
551 }
552
553
554 void
555 check_trunc_hack(void)
556 {
557         struct stat statbuf;
558         int fd = get_fd();
559
560         /* should not ignore ftruncate(2)'s return value */
561         if (ftruncate(fd, (off_t)0) < 0) {
562                 prterr("trunc_hack: ftruncate(0)");
563                 exit(1);
564         }
565         if (ftruncate(fd, (off_t)100000) < 0) {
566                 prterr("trunc_hack: ftruncate(100000)");
567                 exit(1);
568         }
569         if (fstat(fd, &statbuf)) {
570                 prterr("trunc_hack: fstat");
571                 statbuf.st_size = -1;
572         }
573         if (statbuf.st_size != (off_t)100000) {
574                 prt("no extend on truncate! not posix!\n");
575                 exit(130);
576         }
577         if (ftruncate(fd, 0) < 0) {
578                 prterr("trunc_hack: ftruncate(0) (2nd call)");
579                 exit(1);
580         }
581 }
582
583 void
584 output_line(struct test_file *tf, int op, unsigned offset,
585                 unsigned size, struct timeval *tv)
586 {
587         char *ops[] = {
588                 [OP_READ] = "read",
589                 [OP_WRITE] = "write",
590                 [OP_TRUNCATE] = "trunc from",
591                 [OP_MAPREAD] = "mapread",
592                 [OP_MAPWRITE] = "mapwrite",
593         };
594
595         /* W. */
596         if (!(!quiet && ((progressinterval &&
597                         testcalls % progressinterval == 0) ||
598                        (debug &&
599                         (monitorstart == -1 ||
600                          (offset + size > monitorstart &&
601                           (monitorend == -1 || offset <= monitorend)))))))
602                 return;
603
604         prt("%06lu%s %lu.%06lu %-10s %#08x %s %#08x\t(0x%x bytes)\n",
605                 testcalls, fill_tf_buf(tf), tv->tv_sec, tv->tv_usec,
606                 ops[op],
607                 offset, op == OP_TRUNCATE ? " to " : "thru",
608                 offset + size - 1, size);
609 }
610
611 void
612 doread(unsigned offset, unsigned size)
613 {
614         struct timeval t;
615         off_t ret;
616         unsigned iret;
617         struct test_file *tf = get_tf();
618         int fd = tf->fd;
619
620         offset -= offset % readbdy;
621         gettimeofday(&t, NULL);
622         if (size == 0) {
623                 if (!quiet && testcalls > simulatedopcount)
624                         prt("skipping zero size read\n");
625                 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
626                 return;
627         }
628         if (size + offset > file_size) {
629                 if (!quiet && testcalls > simulatedopcount)
630                         prt("skipping seek/read past end of file\n");
631                 log4(OP_SKIPPED, OP_READ, offset, size, &t, tf);
632                 return;
633         }
634
635         log4(OP_READ, offset, size, 0, &t, tf);
636
637         if (testcalls <= simulatedopcount)
638                 return;
639
640         output_line(tf, OP_READ, offset, size, &t);
641
642         ret = lseek(fd, (off_t)offset, SEEK_SET);
643         if (ret == (off_t)-1) {
644                 prterr("doread: lseek");
645                 report_failure(140);
646         }
647         iret = read(fd, temp_buf, size);
648         if (!quiet && (debug > 1 &&
649                         (monitorstart == -1 ||
650                          (offset + size > monitorstart &&
651                           (monitorend == -1 || offset <= monitorend))))) {
652                 gettimeofday(&t, NULL);
653                 prt("       %lu.%06lu read done\n", t.tv_sec, t.tv_usec);
654         }
655         if (iret != size) {
656                 if (iret == -1)
657                         prterr("doread: read");
658                 else
659                         prt("short read: 0x%x bytes instead of 0x%x\n",
660                             iret, size);
661                 report_failure(141);
662         }
663         check_buffers(offset, size);
664 }
665
666
667 void
668 domapread(unsigned offset, unsigned size)
669 {
670         struct timeval t;
671         unsigned pg_offset;
672         unsigned map_size;
673         char    *p;
674         struct test_file *tf = get_tf();
675         int fd = tf->fd;
676
677         offset -= offset % readbdy;
678         gettimeofday(&t, NULL);
679         if (size == 0) {
680                 if (!quiet && testcalls > simulatedopcount)
681                         prt("skipping zero size read\n");
682                 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
683                 return;
684         }
685         if (size + offset > file_size) {
686                 if (!quiet && testcalls > simulatedopcount)
687                         prt("skipping seek/read past end of file\n");
688                 log4(OP_SKIPPED, OP_MAPREAD, offset, size, &t, tf);
689                 return;
690         }
691
692         log4(OP_MAPREAD, offset, size, 0, &t, tf);
693
694         if (testcalls <= simulatedopcount)
695                 return;
696
697         output_line(tf, OP_MAPREAD, offset, size, &t);
698
699         pg_offset = offset & page_mask;
700         map_size  = pg_offset + size;
701
702         if ((p = mmap(0, map_size, PROT_READ, MAP_FILE | MAP_SHARED, fd,
703                       (off_t)(offset - pg_offset))) == MAP_FAILED) {
704                 prterr("domapread: mmap");
705                 report_failure(190);
706         }
707         if (!quiet && (debug > 1 &&
708                         (monitorstart == -1 ||
709                          (offset + size > monitorstart &&
710                           (monitorend == -1 || offset <= monitorend))))) {
711                 gettimeofday(&t, NULL);
712                 prt("       %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
713         }
714         memcpy(temp_buf, p + pg_offset, size);
715         if (!quiet && (debug > 1 &&
716                         (monitorstart == -1 ||
717                          (offset + size > monitorstart &&
718                           (monitorend == -1 || offset <= monitorend))))) {
719                 gettimeofday(&t, NULL);
720                 prt("       %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
721         }
722         if (munmap(p, map_size) != 0) {
723                 prterr("domapread: munmap");
724                 report_failure(191);
725         }
726         if (!quiet && (debug > 1 &&
727                         (monitorstart == -1 ||
728                          (offset + size > monitorstart &&
729                           (monitorend == -1 || offset <= monitorend))))) {
730                 gettimeofday(&t, NULL);
731                 prt("       %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
732         }
733
734         check_buffers(offset, size);
735 }
736
737
738 void
739 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
740 {
741         while (size--) {
742                 good_buf[offset] = testcalls % 256;
743                 if (offset % 2)
744                         good_buf[offset] += original_buf[offset];
745                 offset++;
746         }
747 }
748
749
750 void
751 dowrite(unsigned offset, unsigned size)
752 {
753         struct timeval t;
754         off_t ret;
755         unsigned iret;
756         struct test_file *tf = get_tf();
757         int fd = tf->fd;
758
759         offset -= offset % writebdy;
760         gettimeofday(&t, NULL);
761         if (size == 0) {
762                 if (!quiet && testcalls > simulatedopcount)
763                         prt("skipping zero size write\n");
764                 log4(OP_SKIPPED, OP_WRITE, offset, size, &t, tf);
765                 return;
766         }
767
768         log4(OP_WRITE, offset, size, file_size, &t, tf);
769
770         gendata(original_buf, good_buf, offset, size);
771         if (file_size < offset + size) {
772                 if (file_size < offset)
773                         memset(good_buf + file_size, '\0', offset - file_size);
774                 file_size = offset + size;
775                 if (lite) {
776                         warn("Lite file size bug in fsx!");
777                         report_failure(149);
778                 }
779         }
780
781         if (testcalls <= simulatedopcount)
782                 return;
783
784         output_line(tf, OP_WRITE, offset, size, &t);
785
786         ret = lseek(fd, (off_t)offset, SEEK_SET);
787         if (ret == (off_t)-1) {
788                 prterr("dowrite: lseek");
789                 report_failure(150);
790         }
791         iret = write(fd, good_buf + offset, size);
792         if (!quiet && (debug > 1 &&
793                         (monitorstart == -1 ||
794                          (offset + size > monitorstart &&
795                           (monitorend == -1 || offset <= monitorend))))) {
796                 gettimeofday(&t, NULL);
797                 prt("       %lu.%06lu write done\n", t.tv_sec, t.tv_usec);
798         }
799         if (iret != size) {
800                 if (iret == -1)
801                         prterr("dowrite: write");
802                 else
803                         prt("short write: 0x%x bytes instead of 0x%x\n",
804                             iret, size);
805                 report_failure(151);
806         }
807 }
808
809
810 void
811 domapwrite(unsigned offset, unsigned size)
812 {
813         struct timeval t;
814         unsigned pg_offset;
815         unsigned map_size;
816         off_t    cur_filesize;
817         char    *p;
818         struct test_file *tf = get_tf();
819         int fd = tf->fd;
820
821         offset -= offset % writebdy;
822         gettimeofday(&t, NULL);
823         if (size == 0) {
824                 if (!quiet && testcalls > simulatedopcount)
825                         prt("skipping zero size write\n");
826                 log4(OP_SKIPPED, OP_MAPWRITE, offset, size, &t, tf);
827                 return;
828         }
829         cur_filesize = file_size;
830
831         log4(OP_MAPWRITE, offset, size, 0, &t, tf);
832
833         gendata(original_buf, good_buf, offset, size);
834         if (file_size < offset + size) {
835                 if (file_size < offset)
836                         memset(good_buf + file_size, '\0', offset - file_size);
837                 file_size = offset + size;
838                 if (lite) {
839                         warn("Lite file size bug in fsx!");
840                         report_failure(200);
841                 }
842         }
843
844         if (testcalls <= simulatedopcount)
845                 return;
846
847         output_line(tf, OP_MAPWRITE, offset, size, &t);
848
849         if (file_size > cur_filesize) {
850                 if (ftruncate(fd, file_size) == -1) {
851                         prterr("domapwrite: ftruncate");
852                         exit(201);
853                 }
854                 if (!quiet && (debug > 1 &&
855                                (monitorstart == -1 ||
856                                 (offset + size > monitorstart &&
857                                  (monitorend == -1 || offset <= monitorend))))) {
858                         gettimeofday(&t, NULL);
859                         prt("       %lu.%06lu truncate done\n", t.tv_sec, t.tv_usec);
860         }
861         }
862         pg_offset = offset & page_mask;
863         map_size  = pg_offset + size;
864
865         if ((p = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_FILE|MAP_SHARED,
866                       fd, (off_t)(offset - pg_offset))) == MAP_FAILED) {
867                 prterr("domapwrite: mmap");
868                 report_failure(202);
869         }
870         if (!quiet && (debug > 1 &&
871                         (monitorstart == -1 ||
872                          (offset + size > monitorstart &&
873                           (monitorend == -1 || offset <= monitorend))))) {
874                 gettimeofday(&t, NULL);
875                 prt("       %lu.%06lu mmap done\n", t.tv_sec, t.tv_usec);
876         }
877         memcpy(p + pg_offset, good_buf + offset, size);
878         if (!quiet && (debug > 1 &&
879                         (monitorstart == -1 ||
880                          (offset + size > monitorstart &&
881                           (monitorend == -1 || offset <= monitorend))))) {
882                 gettimeofday(&t, NULL);
883                 prt("       %lu.%06lu memcpy done\n", t.tv_sec, t.tv_usec);
884         }
885         if (msync(p, map_size, 0) != 0) {
886                 prterr("domapwrite: msync");
887                 report_failure(203);
888         }
889         if (!quiet && (debug > 1 &&
890                         (monitorstart == -1 ||
891                          (offset + size > monitorstart &&
892                           (monitorend == -1 || offset <= monitorend))))) {
893                 gettimeofday(&t, NULL);
894                 prt("       %lu.%06lu msync done\n", t.tv_sec, t.tv_usec);
895         }
896         if (munmap(p, map_size) != 0) {
897                 prterr("domapwrite: munmap");
898                 report_failure(204);
899         }
900         if (!quiet && (debug > 1 &&
901                         (monitorstart == -1 ||
902                          (offset + size > monitorstart &&
903                           (monitorend == -1 || offset <= monitorend))))) {
904                 gettimeofday(&t, NULL);
905                 prt("       %lu.%06lu munmap done\n", t.tv_sec, t.tv_usec);
906         }
907 }
908
909
910 void
911 dotruncate(unsigned size)
912 {
913         struct timeval t;
914         int oldsize = file_size;
915         struct test_file *tf = get_tf();
916         int fd = tf->fd;
917
918         size -= size % truncbdy;
919         gettimeofday(&t, NULL);
920         if (size > biggest) {
921                 biggest = size;
922                 if (!quiet && testcalls > simulatedopcount)
923                         prt("truncating to largest ever: 0x%x\n", size);
924         }
925
926         log4(OP_TRUNCATE, size, (unsigned)file_size, 0, &t, tf);
927
928         if (size > file_size)
929                 memset(good_buf + file_size, '\0', size - file_size);
930         file_size = size;
931
932         if (testcalls <= simulatedopcount)
933                 return;
934
935         output_line(tf, OP_TRUNCATE, oldsize, size, &t);
936
937         if (ftruncate(fd, (off_t)size) == -1) {
938                 prt("ftruncate1: %x\n", size);
939                 prterr("dotruncate: ftruncate");
940                 report_failure(160);
941         }
942         if (!quiet && debug > 1) {
943                 gettimeofday(&t, NULL);
944                 prt("       %lu.%06lu trunc done\n", t.tv_sec, t.tv_usec);
945         }
946 }
947
948
949 void
950 writefileimage()
951 {
952         ssize_t iret;
953         int fd = get_fd();
954
955         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
956                 prterr("writefileimage: lseek");
957                 report_failure(171);
958         }
959         iret = write(fd, good_buf, file_size);
960         if ((off_t)iret != file_size) {
961                 if (iret == -1)
962                         prterr("writefileimage: write");
963                 else
964                         prt("short write: 0x%lx bytes instead of 0x%llx\n",
965                             (unsigned long)iret,
966                             (unsigned long long)file_size);
967                 report_failure(172);
968         }
969         if (lite ? 0 : ftruncate(fd, file_size) == -1) {
970                 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
971                 prterr("writefileimage: ftruncate");
972                 report_failure(173);
973         }
974 }
975
976
977 void
978 docloseopen(void)
979 {
980         struct timeval t;
981         struct test_file *tf = get_tf();
982
983         if (testcalls <= simulatedopcount)
984                 return;
985
986         gettimeofday(&t, NULL);
987         log4(OP_CLOSEOPEN, file_size, (unsigned)file_size, 0, &t, tf);
988
989         if (debug)
990                 prt("%06lu %lu.%06lu close/open\n", testcalls, t.tv_sec,
991                     t.tv_usec);
992         if (close(tf->fd)) {
993                 prterr("docloseopen: close");
994                 report_failure(180);
995         }
996         if (!quiet && debug > 1) {
997                 gettimeofday(&t, NULL);
998                 prt("       %lu.%06lu close done\n", t.tv_sec, t.tv_usec);
999         }
1000         tf->fd = open(tf->path, O_RDWR, 0);
1001         if (tf->fd < 0) {
1002                 prterr("docloseopen: open");
1003                 report_failure(181);
1004         }
1005         if (!quiet && debug > 1) {
1006                 gettimeofday(&t, NULL);
1007                 prt("       %lu.%06lu open done\n", t.tv_sec, t.tv_usec);
1008         }
1009 }
1010
1011
1012 void
1013 test(void)
1014 {
1015         unsigned long   offset;
1016         unsigned long   size = maxoplen;
1017         unsigned long   rv = random();
1018         unsigned long   op = rv % (3 + !lite + mapped_writes);
1019
1020         /* turn off the map read if necessary */
1021
1022         if (op == 2 && !mapped_reads)
1023             op = 0;
1024
1025         if (simulatedopcount > 0 && testcalls == simulatedopcount)
1026                 writefileimage();
1027
1028         testcalls++;
1029
1030         if (debugstart > 0 && testcalls >= debugstart)
1031                 debug = 1;
1032
1033         if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
1034                 prt("%lu...\n", testcalls);
1035
1036         /*
1037          * READ:        op = 0
1038          * WRITE:       op = 1
1039          * MAPREAD:     op = 2
1040          * TRUNCATE:    op = 3
1041          * MAPWRITE:    op = 3 or 4
1042          */
1043         if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
1044                 dotruncate(random() % maxfilelen);
1045         else {
1046                 if (randomoplen)
1047                         size = random() % (maxoplen+1);
1048                 if (lite ? 0 : op == 3)
1049                         dotruncate(size);
1050                 else {
1051                         offset = random();
1052                         if (op == 1 || op == (lite ? 3 : 4)) {
1053                                 offset %= maxfilelen;
1054                                 if (offset + size > maxfilelen)
1055                                         size = maxfilelen - offset;
1056                                 if (op != 1)
1057                                         domapwrite(offset, size);
1058                                 else
1059                                         dowrite(offset, size);
1060                         } else {
1061                                 if (file_size)
1062                                         offset %= file_size;
1063                                 else
1064                                         offset = 0;
1065                                 if (offset + size > file_size)
1066                                         size = file_size - offset;
1067                                 if (op != 0)
1068                                         domapread(offset, size);
1069                                 else
1070                                         doread(offset, size);
1071                         }
1072                 }
1073         }
1074         if (sizechecks && testcalls > simulatedopcount)
1075                 check_size();
1076         if (closeprob && (rv >> 3) < (1 << 28) / closeprob)
1077                 docloseopen();
1078 }
1079
1080
1081 void
1082 cleanup(sig)
1083         int     sig;
1084 {
1085         if (sig)
1086                 prt("signal %d\n", sig);
1087         prt("testcalls = %lu\n", testcalls);
1088         exit(sig);
1089 }
1090
1091
1092 void
1093 usage(void)
1094 {
1095         fprintf(stdout, "usage: %s",
1096                 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m "
1097 "start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t "
1098 "truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] "
1099 "[ -I random|rotate ] fname [additional paths to fname..]\n"
1100 "       -b opnum: beginning operation number (default 1)\n"
1101 "       -c P: 1 in P chance of file close+open at each op (default infinity)\n"
1102 "       -d: debug output for all operations [-d -d = more debugging]\n"
1103 "       -l flen: the upper bound on file size (default 262144)\n"
1104 "       -m startop:endop: monitor (print debug output) specified byte rang"
1105 "(default 0:infinity)\n"
1106 "       -n: no verifications of file size\n"
1107 "       -o oplen: the upper bound on operation size (default 65536)\n"
1108 "       -p progressinterval: debug output at specified operation interval\n"
1109 "       -q: quieter operation\n"
1110 "       -r readbdy: 4096 would make reads page aligned (default 1)\n"
1111 "       -s style: 1 gives smaller truncates (default 0)\n"
1112 "       -t truncbdy: 4096 would make truncates page aligned (default 1)\n"
1113 "       -w writebdy: 4096 would make writes page aligned (default 1)\n"
1114 "       -D startingop: debug output starting at specified operation\n"
1115 "       -L: fsxLite - no file creations & no file size changes\n"
1116 "       -N numops: total # operations to do (default infinity)\n"
1117 "       -O: use oplen (see -o flag) for every op (default random)\n"
1118 "       -P: save .fsxlog and .fsxgood files in dirpath (default ./)\n"
1119 "       -S seed: for random # generator (default 1) 0 gets timestamp\n"
1120 "       -W: mapped write operations DISabled\n"
1121 "        -R: read() system calls only (mapped reads disabled)\n"
1122 "       -I: When multiple paths to the file are given each operation uses"
1123 "           a different path.  Iterate through them in order with 'rotate'"
1124 "           or chose then at 'random'.  (defaults to random)\n"
1125 "       fname: this filename is REQUIRED (no default)\n");
1126         exit(90);
1127 }
1128
1129
1130 int
1131 getnum(char *s, char **e)
1132 {
1133         int ret = -1;
1134
1135         *e = (char *) 0;
1136         ret = strtol(s, e, 0);
1137         if (*e)
1138                 switch (**e) {
1139                 case 'b':
1140                 case 'B':
1141                         ret *= 512;
1142                         *e = *e + 1;
1143                         break;
1144                 case 'k':
1145                 case 'K':
1146                         ret *= 1024;
1147                         *e = *e + 1;
1148                         break;
1149                 case 'm':
1150                 case 'M':
1151                         ret *= 1024*1024;
1152                         *e = *e + 1;
1153                         break;
1154                 case 'w':
1155                 case 'W':
1156                         ret *= 4;
1157                         *e = *e + 1;
1158                         break;
1159                 }
1160         return (ret);
1161 }
1162
1163 int
1164 main(int argc, char **argv)
1165 {
1166         int     i, style, ch;
1167         char    *endp;
1168         int  dirpath = 0;
1169
1170         goodfile[0] = 0;
1171         logfile[0] = 0;
1172
1173         page_size = getpagesize();
1174         page_mask = page_size - 1;
1175
1176         setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
1177
1178         while ((ch = getopt(argc, argv,
1179                                 "b:c:dl:m:no:p:qr:s:t:w:D:I:LN:OP:RS:W"))
1180                != EOF)
1181                 switch (ch) {
1182                 case 'b':
1183                         simulatedopcount = getnum(optarg, &endp);
1184                         if (!quiet)
1185                                 fprintf(stdout, "Will begin at operation"
1186                                         "%ld\n",
1187                                         simulatedopcount);
1188                         if (simulatedopcount == 0)
1189                                 usage();
1190                         simulatedopcount -= 1;
1191                         break;
1192                 case 'c':
1193                         closeprob = getnum(optarg, &endp);
1194                         if (!quiet)
1195                                 fprintf(stdout,
1196                                         "Chance of close/open is 1 in %d\n",
1197                                         closeprob);
1198                         if (closeprob <= 0)
1199                                 usage();
1200                         break;
1201                 case 'd':
1202                         debug++;
1203                         break;
1204                 case 'l':
1205                         maxfilelen = getnum(optarg, &endp);
1206                         if (maxfilelen <= 0)
1207                                 usage();
1208                         break;
1209                 case 'm':
1210                         monitorstart = getnum(optarg, &endp);
1211                         if (monitorstart < 0)
1212                                 usage();
1213                         if (!endp || *endp++ != ':')
1214                                 usage();
1215                         monitorend = getnum(endp, &endp);
1216                         if (monitorend < 0)
1217                                 usage();
1218                         if (monitorend == 0)
1219                                 monitorend = -1; /* aka infinity */
1220                         debug = 1;
1221                 case 'n':
1222                         sizechecks = 0;
1223                         break;
1224                 case 'o':
1225                         maxoplen = getnum(optarg, &endp);
1226                         if (maxoplen <= 0)
1227                                 usage();
1228                         break;
1229                 case 'p':
1230                         progressinterval = getnum(optarg, &endp);
1231                         if (progressinterval < 0)
1232                                 usage();
1233                         break;
1234                 case 'q':
1235                         quiet = 1;
1236                         break;
1237                 case 'r':
1238                         readbdy = getnum(optarg, &endp);
1239                         if (readbdy <= 0)
1240                                 usage();
1241                         break;
1242                 case 's':
1243                         style = getnum(optarg, &endp);
1244                         if (style < 0 || style > 1)
1245                                 usage();
1246                         break;
1247                 case 't':
1248                         truncbdy = getnum(optarg, &endp);
1249                         if (truncbdy <= 0)
1250                                 usage();
1251                         break;
1252                 case 'w':
1253                         writebdy = getnum(optarg, &endp);
1254                         if (writebdy <= 0)
1255                                 usage();
1256                         break;
1257                 case 'D':
1258                         debugstart = getnum(optarg, &endp);
1259                         if (debugstart < 1)
1260                                 usage();
1261                         break;
1262                 case 'I':
1263                         assign_fd_policy(optarg);
1264                         break;
1265                 case 'L':
1266                         lite = 1;
1267                         break;
1268                 case 'N':
1269                         numops = getnum(optarg, &endp);
1270                         if (numops < 0)
1271                                 usage();
1272                         break;
1273                 case 'O':
1274                         randomoplen = 0;
1275                         break;
1276                 case 'P':
1277                         strncpy(goodfile, optarg, sizeof(goodfile));
1278                         strcat(goodfile, "/");
1279                         strncpy(logfile, optarg, sizeof(logfile));
1280                         strcat(logfile, "/");
1281                         dirpath = 1;
1282                         break;
1283                 case 'R':
1284                         mapped_reads = 0;
1285                         break;
1286                 case 'S':
1287                         seed = getnum(optarg, &endp);
1288                         if (seed == 0)
1289                                 seed = time(0) % 10000;
1290                         if (!quiet)
1291                                 fprintf(stdout, "Seed set to %d\n", seed);
1292                         if (seed < 0)
1293                                 usage();
1294                         break;
1295                 case 'W':
1296                         mapped_writes = 0;
1297                         if (!quiet)
1298                                 fprintf(stdout, "mapped writes DISABLED\n");
1299                         break;
1300
1301                 default:
1302                         usage();
1303                         /* NOTREACHED */
1304                 }
1305         argc -= optind;
1306         argv += optind;
1307         if (argc < 1)
1308                 usage();
1309         fname = argv[0];
1310
1311         signal(SIGHUP,  cleanup);
1312         signal(SIGINT,  cleanup);
1313         signal(SIGPIPE, cleanup);
1314         signal(SIGALRM, cleanup);
1315         signal(SIGTERM, cleanup);
1316         signal(SIGXCPU, cleanup);
1317         signal(SIGXFSZ, cleanup);
1318         signal(SIGVTALRM,       cleanup);
1319         signal(SIGUSR1, cleanup);
1320         signal(SIGUSR2, cleanup);
1321
1322         initstate(seed, state, 256);
1323         setstate(state);
1324
1325         open_test_files(argv, argc);
1326
1327         strncat(goodfile, dirpath ? my_basename(fname) : fname, 256);
1328         strcat (goodfile, ".fsxgood");
1329         fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
1330         if (fsxgoodfd < 0) {
1331                 prterr(goodfile);
1332                 exit(92);
1333         }
1334         strncat(logfile, dirpath ? my_basename(fname) : fname, 256);
1335         strcat (logfile, ".fsxlog");
1336         fsxlogf = fopen(logfile, "w");
1337         if (fsxlogf == NULL) {
1338                 prterr(logfile);
1339                 exit(93);
1340         }
1341         if (lite) {
1342                 off_t ret;
1343                 int fd = get_fd();
1344                 file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END);
1345                 if (file_size == (off_t)-1) {
1346                         prterr(fname);
1347                         warn("main: lseek eof");
1348                         exit(94);
1349                 }
1350                 ret = lseek(fd, (off_t)0, SEEK_SET);
1351                 if (ret == (off_t)-1) {
1352                         prterr(fname);
1353                         warn("main: lseek 0");
1354                         exit(95);
1355                 }
1356         }
1357         original_buf = (char *) malloc(maxfilelen);
1358         for (i = 0; i < maxfilelen; i++)
1359                 original_buf[i] = random() % 256;
1360         good_buf = (char *) malloc(maxfilelen);
1361         memset(good_buf, '\0', maxfilelen);
1362         temp_buf = (char *) malloc(maxoplen);
1363         memset(temp_buf, '\0', maxoplen);
1364         if (lite) {     /* zero entire existing file */
1365                 ssize_t written;
1366                 int fd = get_fd();
1367
1368                 written = write(fd, good_buf, (size_t)maxfilelen);
1369                 if (written != maxfilelen) {
1370                         if (written == -1) {
1371                                 prterr(fname);
1372                                 warn("main: error on write");
1373                         } else
1374                                 warn("main: short write, 0x%x bytes instead"
1375                                         "of 0x%x\n",
1376                                      (unsigned)written, maxfilelen);
1377                         exit(98);
1378                 }
1379         } else
1380                 check_trunc_hack();
1381
1382         while (numops == -1 || numops--)
1383                 test();
1384
1385         close_test_files();
1386         prt("All operations completed A-OK!\n");
1387
1388         exit(0);
1389         return 0;
1390 }