2 * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
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
30 * Author: Avadis Tevanian, Jr.
32 * File system exerciser.
34 * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com
36 * Various features from Joe Sokol, Pat Dirks, and Clark Warner.
38 * Small changes to work under Linux -- davej@suse.de
40 * Sundry porting patches from Guy Harris 12/2001
42 * Checks for mmap last-page zero fill.
44 * Modified heavily by Charles Manning to exercise via the
45 * yaffs direct interface.
49 #include <sys/types.h>
52 # include <sys/param.h>
74 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
77 * A log entry is an operation and a bunch of arguments.
87 struct log_entry oplog[LOGSIZE]; /* the log */
88 int logptr = 0; /* current position in log */
89 int logcount = 0; /* total ops */
98 #define OP_CLOSEOPEN 4
100 #define OP_MAPWRITE 6
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 */
115 unsigned long testcalls = 0; /* calls to function "test" */
117 unsigned long simulatedopcount = 0; /* -b flag */
118 int closeprob = 0; /* -c flag */
119 int debug = 0; /* -d flag */
120 unsigned long debugstart = 0; /* -D flag */
121 unsigned long maxfilelen = 256 * 1024; /* -l flag */
122 int sizechecks = 1; /* -n flag disables them */
123 int maxoplen = 64 * 1024; /* -o flag */
124 int quiet = 0; /* -q flag */
125 unsigned long progressinterval = 0; /* -p flag */
126 int readbdy = 1; /* -r flag */
127 int style = 0; /* -s flag */
128 int truncbdy = 1; /* -t flag */
129 int writebdy = 1; /* -w flag */
130 long monitorstart = -1; /* -m flag */
131 long monitorend = -1; /* -m flag */
132 int lite = 0; /* -L flag */
133 long numops = -1; /* -N flag */
134 int randomoplen = 1; /* -O flag disables it */
135 int seed = 1; /* -S flag */
137 int mapped_writes = 0; /* yaffs direct does not support mmapped files */
138 int mapped_reads = 0;
141 FILE * fsxlogf = NULL;
147 vwarnc(code, fmt, ap)
152 fprintf(stderr, "fsx: ");
154 vfprintf(stderr, fmt, ap);
155 fprintf(stderr, ": ");
157 fprintf(stderr, "%s\n", strerror(code));
162 warn(const char * fmt, ...)
166 vwarnc(errno, fmt, ap);
177 vfprintf(stdout, fmt, args);
179 vfprintf(fsxlogf, fmt, args);
186 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
191 log4(int operation, int arg0, int arg1, int arg2)
193 struct log_entry *le;
196 le->operation = operation;
198 le->operation = ~ le->operation;
204 if (logptr >= LOGSIZE)
213 struct log_entry *lp;
215 prt("LOG DUMP (%d total operations):\n", logcount);
216 if (logcount < LOGSIZE) {
223 for ( ; count > 0; count--) {
226 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
227 prt("%d(%d mod 256): ", opnum, opnum%256);
229 if ((closeopen = lp->operation < 0))
230 lp->operation = ~ lp->operation;
232 switch (lp->operation) {
234 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
235 lp->args[0], lp->args[0] + lp->args[1] - 1,
237 if (badoff >= lp->args[0] && badoff <
238 lp->args[0] + lp->args[1])
242 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
243 lp->args[0], lp->args[0] + lp->args[1] - 1,
245 if (badoff >= lp->args[0] && badoff <
246 lp->args[0] + lp->args[1])
250 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
251 lp->args[0], lp->args[0] + lp->args[1] - 1,
253 if (badoff >= lp->args[0] &&
254 badoff < lp->args[0] + lp->args[1])
258 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
259 lp->args[0], lp->args[0] + lp->args[1] - 1,
261 if (lp->args[0] > lp->args[2])
263 else if (lp->args[0] + lp->args[1] > lp->args[2])
265 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
266 badoff < lp->args[0] + lp->args[1])
270 down = lp->args[0] < lp->args[1];
271 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
272 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
273 if (badoff >= lp->args[!down] &&
274 badoff < lp->args[!!down])
278 prt("SKIPPED (no operation)");
281 prt("BOGUS LOG ENTRY (operation code = %d)!",
285 prt("\n\t\tCLOSE/OPEN");
295 save_buffer(char *buffer, off_t bufferlength, int fd)
298 ssize_t byteswritten;
300 if (fd <= 0 || bufferlength == 0)
303 if (bufferlength > SSIZE_MAX) {
304 prt("fsx flaw: overflow in save_buffer\n");
308 off_t size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
309 if (size_by_seek == (off_t)-1)
310 prterr("save_buffer: lseek eof");
311 else if (bufferlength > size_by_seek) {
312 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
313 (unsigned long long)bufferlength);
314 bufferlength = size_by_seek;
318 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
319 if (ret == (off_t)-1)
320 prterr("save_buffer: lseek 0");
322 byteswritten = yaffs_write(fd, buffer, (size_t)bufferlength);
323 if (byteswritten != bufferlength) {
324 if (byteswritten == -1)
325 prterr("save_buffer write");
327 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
328 (unsigned)byteswritten,
329 (unsigned long long)bufferlength);
335 report_failure(int status)
341 save_buffer(good_buf, file_size, fsxgoodfd);
342 prt("Correct content saved for comparison\n");
343 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
348 prt("Exiting with %d\n",status);
353 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
354 *(((unsigned char *)(cp)) + 1)))
357 check_buffers(unsigned offset, unsigned size)
365 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
366 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
368 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
370 c = good_buf[offset];
374 bad = short_at(&temp_buf[i]);
375 prt("0x%5x\t0x%04x\t0x%04x", offset,
376 short_at(&good_buf[offset]), bad);
377 op = temp_buf[offset & 1 ? i+1 : i];
389 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
391 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
393 prt("????????????????\n");
402 struct yaffs_stat statbuf;
405 if (yaffs_fstat(fd, &statbuf)) {
406 prterr("check_size: fstat");
407 statbuf.st_size = -1;
409 size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
410 if (file_size != statbuf.st_size || file_size != size_by_seek) {
411 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
412 (unsigned long long)file_size,
413 (unsigned long long)statbuf.st_size,
414 (unsigned long long)size_by_seek);
421 check_trunc_hack(void)
423 struct yaffs_stat statbuf;
425 yaffs_ftruncate(fd, (off_t)0);
426 yaffs_ftruncate(fd, (off_t)100000);
427 yaffs_fstat(fd, &statbuf);
428 if (statbuf.st_size != (off_t)100000) {
429 prt("no extend on truncate! not posix!\n");
432 yaffs_ftruncate(fd, (off_t)0);
437 doread(unsigned offset, unsigned size)
442 offset -= offset % readbdy;
444 if (!quiet && testcalls > simulatedopcount)
445 prt("skipping zero size read\n");
446 log4(OP_SKIPPED, OP_READ, offset, size);
449 if (size + offset > file_size) {
450 if (!quiet && testcalls > simulatedopcount)
451 prt("skipping seek/read past end of file\n");
452 log4(OP_SKIPPED, OP_READ, offset, size);
456 log4(OP_READ, offset, size, 0);
458 if (testcalls <= simulatedopcount)
461 if (!quiet && ((progressinterval &&
462 testcalls % progressinterval == 0) ||
464 (monitorstart == -1 ||
465 (offset + size > monitorstart &&
466 (monitorend == -1 || offset <= monitorend))))))
467 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
468 offset, offset + size - 1, size);
469 ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
470 if (ret == (off_t)-1) {
471 prterr("doread: lseek");
474 iret = yaffs_read(fd, temp_buf, size);
477 prterr("doread: read");
479 prt("short read: 0x%x bytes instead of 0x%x\n",
483 check_buffers(offset, size);
491 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
494 good_buf[offset] = testcalls % 256;
496 good_buf[offset] += original_buf[offset];
503 dowrite(unsigned offset, unsigned size)
508 offset -= offset % writebdy;
510 if (!quiet && testcalls > simulatedopcount)
511 prt("skipping zero size write\n");
512 log4(OP_SKIPPED, OP_WRITE, offset, size);
516 log4(OP_WRITE, offset, size, file_size);
518 gendata(original_buf, good_buf, offset, size);
519 if (file_size < offset + size) {
520 if (file_size < offset)
521 memset(good_buf + file_size, '\0', offset - file_size);
522 file_size = offset + size;
524 warn("Lite file size bug in fsx!");
529 if (testcalls <= simulatedopcount)
532 if (!quiet && ((progressinterval &&
533 testcalls % progressinterval == 0) ||
535 (monitorstart == -1 ||
536 (offset + size > monitorstart &&
537 (monitorend == -1 || offset <= monitorend))))))
538 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
539 offset, offset + size - 1, size);
540 ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
541 if (ret == (off_t)-1) {
542 prterr("dowrite: lseek");
545 iret = yaffs_write(fd, good_buf + offset, size);
548 prterr("dowrite: write");
550 prt("short write: 0x%x bytes instead of 0x%x\n",
559 dotruncate(unsigned size)
561 int oldsize = file_size;
563 size -= size % truncbdy;
564 if (size > biggest) {
566 if (!quiet && testcalls > simulatedopcount)
567 prt("truncating to largest ever: 0x%x\n", size);
570 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
572 if (size > file_size)
573 memset(good_buf + file_size, '\0', size - file_size);
576 if (testcalls <= simulatedopcount)
579 if ((progressinterval && testcalls % progressinterval == 0) ||
580 (debug && (monitorstart == -1 || monitorend == -1 ||
581 size <= monitorend)))
582 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
583 if (yaffs_ftruncate(fd, (off_t)size) == -1) {
584 prt("ftruncate1: %x\n", size);
585 prterr("dotruncate: ftruncate");
596 if (yaffs_lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
597 prterr("writefileimage: lseek");
600 iret = yaffs_write(fd, good_buf, file_size);
601 if ((off_t)iret != file_size) {
603 prterr("writefileimage: write");
605 prt("short write: 0x%x bytes instead of 0x%llx\n",
606 iret, (unsigned long long)file_size);
609 if (lite ? 0 : yaffs_ftruncate(fd, file_size) == -1) {
610 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
611 prterr("writefileimage: ftruncate");
620 if (testcalls <= simulatedopcount)
624 prt("%lu close/open\n", testcalls);
625 if (yaffs_close(fd)) {
626 prterr("docloseopen: close");
629 fd = yaffs_open(fname, O_RDWR, 0);
631 prterr("docloseopen: open");
640 unsigned long offset;
641 unsigned long size = maxoplen;
642 unsigned long rv = random();
643 unsigned long op = rv % (3 + !lite + mapped_writes);
645 /* turn off the map read if necessary */
647 if (op == 2 && !mapped_reads)
650 if (simulatedopcount > 0 && testcalls == simulatedopcount)
656 closeopen = (rv >> 3) < (1 << 28) / closeprob;
658 if (debugstart > 0 && testcalls >= debugstart)
661 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
662 prt("%lu...\n", testcalls);
669 * MAPWRITE: op = 3 or 4
671 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
672 dotruncate(random() % maxfilelen);
675 size = random() % (maxoplen+1);
676 if (lite ? 0 : op == 3)
680 if (op == 1 || op == (lite ? 3 : 4)) {
681 offset %= maxfilelen;
682 if (offset + size > maxfilelen)
683 size = maxfilelen - offset;
684 dowrite(offset, size);
690 if (offset + size > file_size)
691 size = file_size - offset;
692 doread(offset, size);
696 if (sizechecks && testcalls > simulatedopcount)
708 prt("signal %d\n", sig);
709 prt("testcalls = %lu\n", testcalls);
717 fprintf(stdout, "usage: %s",
718 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
719 -b opnum: beginning operation number (default 1)\n\
720 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
721 -d: debug output for all operations\n\
722 -l flen: the upper bound on file size (default 262144)\n\
723 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
724 -n: no verifications of file size\n\
725 -o oplen: the upper bound on operation size (default 65536)\n\
726 -p progressinterval: debug output at specified operation interval\n\
727 -q: quieter operation\n\
728 -r readbdy: 4096 would make reads page aligned (default 1)\n\
729 -s style: 1 gives smaller truncates (default 0)\n\
730 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
731 -w writebdy: 4096 would make writes page aligned (default 1)\n\
732 -D startingop: debug output starting at specified operation\n\
733 -L: fsxLite - no file creations & no file size changes\n\
734 -N numops: total # operations to do (default infinity)\n\
735 -O: use oplen (see -o flag) for every op (default random)\n\
736 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
737 -S seed: for random # generator (default 1) 0 gets timestamp\n\
738 fname: this filename is REQUIRED (no default)\n");
744 getnum(char *s, char **e)
749 ret = strtol(s, e, 0);
778 main(int argc, char **argv)
788 page_size = getpagesize();
789 page_mask = page_size - 1;
791 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
793 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
797 simulatedopcount = getnum(optarg, &endp);
799 fprintf(stdout, "Will begin at operation %ld\n",
801 if (simulatedopcount == 0)
803 simulatedopcount -= 1;
806 closeprob = getnum(optarg, &endp);
809 "Chance of close/open is 1 in %d\n",
818 maxfilelen = getnum(optarg, &endp);
823 monitorstart = getnum(optarg, &endp);
824 if (monitorstart < 0)
826 if (!endp || *endp++ != ':')
828 monitorend = getnum(endp, &endp);
832 monitorend = -1; /* aka infinity */
838 maxoplen = getnum(optarg, &endp);
843 progressinterval = getnum(optarg, &endp);
844 if (progressinterval < 0)
851 readbdy = getnum(optarg, &endp);
856 style = getnum(optarg, &endp);
857 if (style < 0 || style > 1)
861 truncbdy = getnum(optarg, &endp);
866 writebdy = getnum(optarg, &endp);
871 debugstart = getnum(optarg, &endp);
879 numops = getnum(optarg, &endp);
887 strncpy(goodfile, optarg, sizeof(goodfile));
888 strcat(goodfile, "/");
889 strncpy(logfile, optarg, sizeof(logfile));
890 strcat(logfile, "/");
896 seed = getnum(optarg, &endp);
898 seed = time(0) % 10000;
900 fprintf(stdout, "Seed set to %d\n", seed);
907 fprintf(stdout, "mapped writes DISABLED\n");
918 yaffs_mount("/flash/flash");
920 fname = "/flash/flash/fsxdata";
922 signal(SIGHUP, cleanup);
923 signal(SIGINT, cleanup);
924 signal(SIGPIPE, cleanup);
925 signal(SIGALRM, cleanup);
926 signal(SIGTERM, cleanup);
927 signal(SIGXCPU, cleanup);
928 signal(SIGXFSZ, cleanup);
929 signal(SIGVTALRM, cleanup);
930 signal(SIGUSR1, cleanup);
931 signal(SIGUSR2, cleanup);
933 initstate(seed, state, 256);
935 fd = yaffs_open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
940 strncat(goodfile, fname, 256);
941 strcat (goodfile, ".fsxgood");
942 fsxgoodfd = yaffs_open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
947 strncat(logfile, "fsx", 256);
948 strcat (logfile, ".fsxlog");
949 fsxlogf = fopen(logfile, "w");
950 if (fsxlogf == NULL) {
956 file_size = maxfilelen = yaffs_lseek(fd, (off_t)0, SEEK_END);
957 if (file_size == (off_t)-1) {
959 warn("main: lseek eof");
962 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
963 if (ret == (off_t)-1) {
965 warn("main: lseek 0");
969 original_buf = (char *) malloc(maxfilelen);
970 for (i = 0; i < maxfilelen; i++)
971 original_buf[i] = random() % 256;
972 good_buf = (char *) malloc(maxfilelen);
973 memset(good_buf, '\0', maxfilelen);
974 temp_buf = (char *) malloc(maxoplen);
975 memset(temp_buf, '\0', maxoplen);
976 if (lite) { /* zero entire existing file */
979 written = yaffs_write(fd, good_buf, (size_t)maxfilelen);
980 if (written != maxfilelen) {
983 warn("main: error on write");
985 warn("main: short write, 0x%x bytes instead of 0x%x\n",
986 (unsigned)written, maxfilelen);
992 while (numops == -1 || numops--)
995 if (yaffs_close(fd)) {
1000 yaffs_close(fsxgoodfd);
1002 yaffs_unmount("flash/flash");
1003 prt("All operations completed A-OK!\n");