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