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