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