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