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