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>
73 #define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
76 * A log entry is an operation and a bunch of arguments.
86 struct log_entry oplog[LOGSIZE]; /* the log */
87 int logptr = 0; /* current position in log */
88 int logcount = 0; /* total ops */
97 #define OP_CLOSEOPEN 4
105 char *original_buf; /* a pointer to the original data */
106 char *good_buf; /* a pointer to the correct data */
107 char *temp_buf; /* a pointer to the current data */
108 char *fname; /* name of our test file */
109 int fd; /* fd for our test file */
114 unsigned long testcalls = 0; /* calls to function "test" */
116 unsigned long simulatedopcount = 0; /* -b flag */
117 int closeprob = 0; /* -c flag */
118 int debug = 0; /* -d flag */
119 unsigned long debugstart = 0; /* -D flag */
120 unsigned long maxfilelen = 256 * 1024; /* -l flag */
121 int sizechecks = 1; /* -n flag disables them */
122 int maxoplen = 64 * 1024; /* -o flag */
123 int quiet = 0; /* -q flag */
124 unsigned long progressinterval = 0; /* -p flag */
125 int readbdy = 1; /* -r flag */
126 int style = 0; /* -s flag */
127 int truncbdy = 1; /* -t flag */
128 int writebdy = 1; /* -w flag */
129 long monitorstart = -1; /* -m flag */
130 long monitorend = -1; /* -m flag */
131 int lite = 0; /* -L flag */
132 long numops = -1; /* -N flag */
133 int randomoplen = 1; /* -O flag disables it */
134 int seed = 1; /* -S flag */
136 int mapped_writes = 0; /* yaffs direct does not support mmapped files */
137 int mapped_reads = 0;
140 FILE * fsxlogf = NULL;
146 vwarnc(code, fmt, ap)
151 fprintf(stderr, "fsx: ");
153 vfprintf(stderr, fmt, ap);
154 fprintf(stderr, ": ");
156 fprintf(stderr, "%s\n", strerror(code));
161 warn(const char * fmt, ...)
165 vwarnc(errno, fmt, ap);
176 vfprintf(stdout, fmt, args);
178 vfprintf(fsxlogf, fmt, args);
185 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
190 log4(int operation, int arg0, int arg1, int arg2)
192 struct log_entry *le;
195 le->operation = operation;
197 le->operation = ~ le->operation;
203 if (logptr >= LOGSIZE)
212 struct log_entry *lp;
214 prt("LOG DUMP (%d total operations):\n", logcount);
215 if (logcount < LOGSIZE) {
222 for ( ; count > 0; count--) {
225 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
226 prt("%d(%d mod 256): ", opnum, opnum%256);
228 if ((closeopen = lp->operation < 0))
229 lp->operation = ~ lp->operation;
231 switch (lp->operation) {
233 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
234 lp->args[0], lp->args[0] + lp->args[1] - 1,
236 if (badoff >= lp->args[0] && badoff <
237 lp->args[0] + lp->args[1])
241 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
242 lp->args[0], lp->args[0] + lp->args[1] - 1,
244 if (badoff >= lp->args[0] && badoff <
245 lp->args[0] + lp->args[1])
249 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
250 lp->args[0], lp->args[0] + lp->args[1] - 1,
252 if (badoff >= lp->args[0] &&
253 badoff < lp->args[0] + lp->args[1])
257 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
258 lp->args[0], lp->args[0] + lp->args[1] - 1,
260 if (lp->args[0] > lp->args[2])
262 else if (lp->args[0] + lp->args[1] > lp->args[2])
264 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
265 badoff < lp->args[0] + lp->args[1])
269 down = lp->args[0] < lp->args[1];
270 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
271 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
272 if (badoff >= lp->args[!down] &&
273 badoff < lp->args[!!down])
277 prt("SKIPPED (no operation)");
280 prt("BOGUS LOG ENTRY (operation code = %d)!",
284 prt("\n\t\tCLOSE/OPEN");
294 save_buffer(char *buffer, off_t bufferlength, int fd)
297 ssize_t byteswritten;
299 if (fd <= 0 || bufferlength == 0)
302 if (bufferlength > SSIZE_MAX) {
303 prt("fsx flaw: overflow in save_buffer\n");
307 off_t size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
308 if (size_by_seek == (off_t)-1)
309 prterr("save_buffer: lseek eof");
310 else if (bufferlength > size_by_seek) {
311 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
312 (unsigned long long)bufferlength);
313 bufferlength = size_by_seek;
317 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
318 if (ret == (off_t)-1)
319 prterr("save_buffer: lseek 0");
321 byteswritten = yaffs_write(fd, buffer, (size_t)bufferlength);
322 if (byteswritten != bufferlength) {
323 if (byteswritten == -1)
324 prterr("save_buffer write");
326 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
327 (unsigned)byteswritten,
328 (unsigned long long)bufferlength);
334 report_failure(int status)
340 save_buffer(good_buf, file_size, fsxgoodfd);
341 prt("Correct content saved for comparison\n");
342 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
347 prt("Exiting with %d\n",status);
352 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
353 *(((unsigned char *)(cp)) + 1)))
356 check_buffers(unsigned offset, unsigned size)
364 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
365 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
367 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
369 c = good_buf[offset];
373 bad = short_at(&temp_buf[i]);
374 prt("0x%5x\t0x%04x\t0x%04x", offset,
375 short_at(&good_buf[offset]), bad);
376 op = temp_buf[offset & 1 ? i+1 : i];
388 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
390 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
392 prt("????????????????\n");
401 struct yaffs_stat statbuf;
404 if (yaffs_fstat(fd, &statbuf)) {
405 prterr("check_size: fstat");
406 statbuf.st_size = -1;
408 size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
409 if (file_size != statbuf.st_size || file_size != size_by_seek) {
410 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
411 (unsigned long long)file_size,
412 (unsigned long long)statbuf.st_size,
413 (unsigned long long)size_by_seek);
420 check_trunc_hack(void)
422 struct yaffs_stat statbuf;
424 yaffs_truncate(fd, (off_t)0);
425 yaffs_truncate(fd, (off_t)100000);
426 yaffs_fstat(fd, &statbuf);
427 if (statbuf.st_size != (off_t)100000) {
428 prt("no extend on truncate! not posix!\n");
431 yaffs_truncate(fd, (off_t)0);
436 doread(unsigned offset, unsigned size)
441 offset -= offset % readbdy;
443 if (!quiet && testcalls > simulatedopcount)
444 prt("skipping zero size read\n");
445 log4(OP_SKIPPED, OP_READ, offset, size);
448 if (size + offset > file_size) {
449 if (!quiet && testcalls > simulatedopcount)
450 prt("skipping seek/read past end of file\n");
451 log4(OP_SKIPPED, OP_READ, offset, size);
455 log4(OP_READ, offset, size, 0);
457 if (testcalls <= simulatedopcount)
460 if (!quiet && ((progressinterval &&
461 testcalls % progressinterval == 0) ||
463 (monitorstart == -1 ||
464 (offset + size > monitorstart &&
465 (monitorend == -1 || offset <= monitorend))))))
466 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
467 offset, offset + size - 1, size);
468 ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
469 if (ret == (off_t)-1) {
470 prterr("doread: lseek");
473 iret = yaffs_read(fd, temp_buf, size);
476 prterr("doread: read");
478 prt("short read: 0x%x bytes instead of 0x%x\n",
482 check_buffers(offset, size);
490 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
493 good_buf[offset] = testcalls % 256;
495 good_buf[offset] += original_buf[offset];
502 dowrite(unsigned offset, unsigned size)
507 offset -= offset % writebdy;
509 if (!quiet && testcalls > simulatedopcount)
510 prt("skipping zero size write\n");
511 log4(OP_SKIPPED, OP_WRITE, offset, size);
515 log4(OP_WRITE, offset, size, file_size);
517 gendata(original_buf, good_buf, offset, size);
518 if (file_size < offset + size) {
519 if (file_size < offset)
520 memset(good_buf + file_size, '\0', offset - file_size);
521 file_size = offset + size;
523 warn("Lite file size bug in fsx!");
528 if (testcalls <= simulatedopcount)
531 if (!quiet && ((progressinterval &&
532 testcalls % progressinterval == 0) ||
534 (monitorstart == -1 ||
535 (offset + size > monitorstart &&
536 (monitorend == -1 || offset <= monitorend))))))
537 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
538 offset, offset + size - 1, size);
539 ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
540 if (ret == (off_t)-1) {
541 prterr("dowrite: lseek");
544 iret = yaffs_write(fd, good_buf + offset, size);
547 prterr("dowrite: write");
549 prt("short write: 0x%x bytes instead of 0x%x\n",
558 dotruncate(unsigned size)
560 int oldsize = file_size;
562 size -= size % truncbdy;
563 if (size > biggest) {
565 if (!quiet && testcalls > simulatedopcount)
566 prt("truncating to largest ever: 0x%x\n", size);
569 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
571 if (size > file_size)
572 memset(good_buf + file_size, '\0', size - file_size);
575 if (testcalls <= simulatedopcount)
578 if ((progressinterval && testcalls % progressinterval == 0) ||
579 (debug && (monitorstart == -1 || monitorend == -1 ||
580 size <= monitorend)))
581 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
582 if (yaffs_truncate(fd, (off_t)size) == -1) {
583 prt("ftruncate1: %x\n", size);
584 prterr("dotruncate: ftruncate");
595 if (yaffs_lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
596 prterr("writefileimage: lseek");
599 iret = yaffs_write(fd, good_buf, file_size);
600 if ((off_t)iret != file_size) {
602 prterr("writefileimage: write");
604 prt("short write: 0x%x bytes instead of 0x%llx\n",
605 iret, (unsigned long long)file_size);
608 if (lite ? 0 : yaffs_truncate(fd, file_size) == -1) {
609 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
610 prterr("writefileimage: ftruncate");
619 if (testcalls <= simulatedopcount)
623 prt("%lu close/open\n", testcalls);
624 if (yaffs_close(fd)) {
625 prterr("docloseopen: close");
628 fd = yaffs_open(fname, O_RDWR, 0);
630 prterr("docloseopen: open");
639 unsigned long offset;
640 unsigned long size = maxoplen;
641 unsigned long rv = random();
642 unsigned long op = rv % (3 + !lite + mapped_writes);
644 /* turn off the map read if necessary */
646 if (op == 2 && !mapped_reads)
649 if (simulatedopcount > 0 && testcalls == simulatedopcount)
655 closeopen = (rv >> 3) < (1 << 28) / closeprob;
657 if (debugstart > 0 && testcalls >= debugstart)
660 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
661 prt("%lu...\n", testcalls);
668 * MAPWRITE: op = 3 or 4
670 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
671 dotruncate(random() % maxfilelen);
674 size = random() % (maxoplen+1);
675 if (lite ? 0 : op == 3)
679 if (op == 1 || op == (lite ? 3 : 4)) {
680 offset %= maxfilelen;
681 if (offset + size > maxfilelen)
682 size = maxfilelen - offset;
683 dowrite(offset, size);
689 if (offset + size > file_size)
690 size = file_size - offset;
691 doread(offset, size);
695 if (sizechecks && testcalls > simulatedopcount)
707 prt("signal %d\n", sig);
708 prt("testcalls = %lu\n", testcalls);
716 fprintf(stdout, "usage: %s",
717 "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\
718 -b opnum: beginning operation number (default 1)\n\
719 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
720 -d: debug output for all operations\n\
721 -l flen: the upper bound on file size (default 262144)\n\
722 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
723 -n: no verifications of file size\n\
724 -o oplen: the upper bound on operation size (default 65536)\n\
725 -p progressinterval: debug output at specified operation interval\n\
726 -q: quieter operation\n\
727 -r readbdy: 4096 would make reads page aligned (default 1)\n\
728 -s style: 1 gives smaller truncates (default 0)\n\
729 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
730 -w writebdy: 4096 would make writes page aligned (default 1)\n\
731 -D startingop: debug output starting at specified operation\n\
732 -L: fsxLite - no file creations & no file size changes\n\
733 -N numops: total # operations to do (default infinity)\n\
734 -O: use oplen (see -o flag) for every op (default random)\n\
735 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
736 -S seed: for random # generator (default 1) 0 gets timestamp\n\
737 fname: this filename is REQUIRED (no default)\n");
743 getnum(char *s, char **e)
748 ret = strtol(s, e, 0);
777 main(int argc, char **argv)
787 page_size = getpagesize();
788 page_mask = page_size - 1;
790 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
792 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
796 simulatedopcount = getnum(optarg, &endp);
798 fprintf(stdout, "Will begin at operation %ld\n",
800 if (simulatedopcount == 0)
802 simulatedopcount -= 1;
805 closeprob = getnum(optarg, &endp);
808 "Chance of close/open is 1 in %d\n",
817 maxfilelen = getnum(optarg, &endp);
822 monitorstart = getnum(optarg, &endp);
823 if (monitorstart < 0)
825 if (!endp || *endp++ != ':')
827 monitorend = getnum(endp, &endp);
831 monitorend = -1; /* aka infinity */
837 maxoplen = getnum(optarg, &endp);
842 progressinterval = getnum(optarg, &endp);
843 if (progressinterval < 0)
850 readbdy = getnum(optarg, &endp);
855 style = getnum(optarg, &endp);
856 if (style < 0 || style > 1)
860 truncbdy = getnum(optarg, &endp);
865 writebdy = getnum(optarg, &endp);
870 debugstart = getnum(optarg, &endp);
878 numops = getnum(optarg, &endp);
886 strncpy(goodfile, optarg, sizeof(goodfile));
887 strcat(goodfile, "/");
888 strncpy(logfile, optarg, sizeof(logfile));
889 strcat(logfile, "/");
895 seed = getnum(optarg, &endp);
897 seed = time(0) % 10000;
899 fprintf(stdout, "Seed set to %d\n", seed);
906 fprintf(stdout, "mapped writes DISABLED\n");
917 yaffs_mount("/flash/flash");
919 fname = "/flash/flash/fsxdata";
921 signal(SIGHUP, cleanup);
922 signal(SIGINT, cleanup);
923 signal(SIGPIPE, cleanup);
924 signal(SIGALRM, cleanup);
925 signal(SIGTERM, cleanup);
926 signal(SIGXCPU, cleanup);
927 signal(SIGXFSZ, cleanup);
928 signal(SIGVTALRM, cleanup);
929 signal(SIGUSR1, cleanup);
930 signal(SIGUSR2, cleanup);
932 initstate(seed, state, 256);
934 fd = yaffs_open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
939 strncat(goodfile, fname, 256);
940 strcat (goodfile, ".fsxgood");
941 fsxgoodfd = yaffs_open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
946 strncat(logfile, "fsx", 256);
947 strcat (logfile, ".fsxlog");
948 fsxlogf = fopen(logfile, "w");
949 if (fsxlogf == NULL) {
955 file_size = maxfilelen = yaffs_lseek(fd, (off_t)0, SEEK_END);
956 if (file_size == (off_t)-1) {
958 warn("main: lseek eof");
961 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
962 if (ret == (off_t)-1) {
964 warn("main: lseek 0");
968 original_buf = (char *) malloc(maxfilelen);
969 for (i = 0; i < maxfilelen; i++)
970 original_buf[i] = random() % 256;
971 good_buf = (char *) malloc(maxfilelen);
972 memset(good_buf, '\0', maxfilelen);
973 temp_buf = (char *) malloc(maxoplen);
974 memset(temp_buf, '\0', maxoplen);
975 if (lite) { /* zero entire existing file */
978 written = yaffs_write(fd, good_buf, (size_t)maxfilelen);
979 if (written != maxfilelen) {
982 warn("main: error on write");
984 warn("main: short write, 0x%x bytes instead of 0x%x\n",
985 (unsigned)written, maxfilelen);
991 while (numops == -1 || numops--)
994 if (yaffs_close(fd)) {
999 yaffs_close(fsxgoodfd);
1001 yaffs_unmount("flash/flash");
1002 prt("All operations completed A-OK!\n");