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 */
134 long numops = 5000; /* -N flag */
136 long numops = /*-1 */ 100000; /* -N flag */
138 int randomoplen = 1; /* -O flag disables it */
139 int seed = 1; /* -S flag */
141 int mapped_writes = 0; /* yaffs direct does not support mmapped files */
142 int mapped_reads = 0;
145 FILE * fsxlogf = NULL;
151 vwarnc(code, fmt, ap)
156 fprintf(stderr, "fsx: ");
158 vfprintf(stderr, fmt, ap);
159 fprintf(stderr, ": ");
161 fprintf(stderr, "%s\n", strerror(code));
166 warn(const char * fmt, ...)
170 vwarnc(errno, fmt, ap);
181 vfprintf(stdout, fmt, args);
183 vfprintf(fsxlogf, fmt, args);
190 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
195 log4(int operation, int arg0, int arg1, int arg2)
197 struct log_entry *le;
200 le->operation = operation;
202 le->operation = ~ le->operation;
208 if (logptr >= LOGSIZE)
217 struct log_entry *lp;
219 prt("LOG DUMP (%d total operations):\n", logcount);
220 if (logcount < LOGSIZE) {
227 for ( ; count > 0; count--) {
230 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
231 prt("%d(%d mod 256): ", opnum, opnum%256);
233 if ((closeopen = lp->operation < 0))
234 lp->operation = ~ lp->operation;
236 switch (lp->operation) {
238 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
239 lp->args[0], lp->args[0] + lp->args[1] - 1,
241 if (badoff >= lp->args[0] && badoff <
242 lp->args[0] + lp->args[1])
246 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
247 lp->args[0], lp->args[0] + lp->args[1] - 1,
249 if (badoff >= lp->args[0] && badoff <
250 lp->args[0] + lp->args[1])
254 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
255 lp->args[0], lp->args[0] + lp->args[1] - 1,
257 if (badoff >= lp->args[0] &&
258 badoff < lp->args[0] + lp->args[1])
262 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
263 lp->args[0], lp->args[0] + lp->args[1] - 1,
265 if (lp->args[0] > lp->args[2])
267 else if (lp->args[0] + lp->args[1] > lp->args[2])
269 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
270 badoff < lp->args[0] + lp->args[1])
274 down = lp->args[0] < lp->args[1];
275 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
276 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
277 if (badoff >= lp->args[!down] &&
278 badoff < lp->args[!!down])
282 prt("SKIPPED (no operation)");
285 prt("BOGUS LOG ENTRY (operation code = %d)!",
289 prt("\n\t\tCLOSE/OPEN");
299 save_buffer(char *buffer, off_t bufferlength, int fd)
302 ssize_t byteswritten;
304 if (fd <= 0 || bufferlength == 0)
307 if (bufferlength > SSIZE_MAX) {
308 prt("fsx flaw: overflow in save_buffer\n");
312 off_t size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
313 if (size_by_seek == (off_t)-1)
314 prterr("save_buffer: lseek eof");
315 else if (bufferlength > size_by_seek) {
316 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
317 (unsigned long long)bufferlength);
318 bufferlength = size_by_seek;
322 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
323 if (ret == (off_t)-1)
324 prterr("save_buffer: lseek 0");
326 byteswritten = yaffs_write(fd, buffer, (size_t)bufferlength);
327 if (byteswritten != bufferlength) {
328 if (byteswritten == -1)
329 prterr("save_buffer write");
331 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
332 (unsigned)byteswritten,
333 (unsigned long long)bufferlength);
339 report_failure(int status)
345 save_buffer(good_buf, file_size, fsxgoodfd);
346 prt("Correct content saved for comparison\n");
347 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
352 prt("Exiting with %d\n",status);
357 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
358 *(((unsigned char *)(cp)) + 1)))
361 check_buffers(unsigned offset, unsigned size)
369 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
370 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
372 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
374 c = good_buf[offset];
378 bad = short_at(&temp_buf[i]);
379 prt("0x%5x\t0x%04x\t0x%04x", offset,
380 short_at(&good_buf[offset]), bad);
381 op = temp_buf[offset & 1 ? i+1 : i];
393 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
395 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
397 prt("????????????????\n");
406 struct yaffs_stat statbuf;
409 if (yaffs_fstat(fd, &statbuf)) {
410 prterr("check_size: fstat");
411 statbuf.st_size = -1;
413 size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
414 if (file_size != statbuf.st_size || file_size != size_by_seek) {
415 prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n",
416 (unsigned long long)file_size,
417 (unsigned long long)statbuf.st_size,
418 (unsigned long long)size_by_seek);
425 check_trunc_hack(void)
427 struct yaffs_stat statbuf;
429 yaffs_ftruncate(fd, (off_t)0);
430 yaffs_ftruncate(fd, (off_t)100000);
431 yaffs_fstat(fd, &statbuf);
432 if (statbuf.st_size != (off_t)100000) {
433 prt("no extend on truncate! not posix!\n");
436 yaffs_ftruncate(fd, (off_t)0);
441 doread(unsigned offset, unsigned size)
446 offset -= offset % readbdy;
448 if (!quiet && testcalls > simulatedopcount)
449 prt("skipping zero size read\n");
450 log4(OP_SKIPPED, OP_READ, offset, size);
453 if (size + offset > file_size) {
454 if (!quiet && testcalls > simulatedopcount)
455 prt("skipping seek/read past end of file\n");
456 log4(OP_SKIPPED, OP_READ, offset, size);
460 log4(OP_READ, offset, size, 0);
462 if (testcalls <= simulatedopcount)
465 if (!quiet && ((progressinterval &&
466 testcalls % progressinterval == 0) ||
468 (monitorstart == -1 ||
469 (offset + size > monitorstart &&
470 (monitorend == -1 || offset <= monitorend))))))
471 prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
472 offset, offset + size - 1, size);
473 ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
474 if (ret == (off_t)-1) {
475 prterr("doread: lseek");
478 iret = yaffs_read(fd, temp_buf, size);
481 prterr("doread: read");
483 prt("short read: 0x%x bytes instead of 0x%x\n",
487 check_buffers(offset, size);
495 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
498 good_buf[offset] = testcalls % 256;
500 good_buf[offset] += original_buf[offset];
507 dowrite(unsigned offset, unsigned size)
512 offset -= offset % writebdy;
514 if (!quiet && testcalls > simulatedopcount)
515 prt("skipping zero size write\n");
516 log4(OP_SKIPPED, OP_WRITE, offset, size);
520 log4(OP_WRITE, offset, size, file_size);
522 gendata(original_buf, good_buf, offset, size);
523 if (file_size < offset + size) {
524 if (file_size < offset)
525 memset(good_buf + file_size, '\0', offset - file_size);
526 file_size = offset + size;
528 warn("Lite file size bug in fsx!");
533 if (testcalls <= simulatedopcount)
536 if (!quiet && ((progressinterval &&
537 testcalls % progressinterval == 0) ||
539 (monitorstart == -1 ||
540 (offset + size > monitorstart &&
541 (monitorend == -1 || offset <= monitorend))))))
542 prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
543 offset, offset + size - 1, size);
544 ret = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
545 if (ret == (off_t)-1) {
546 prterr("dowrite: lseek");
549 iret = yaffs_write(fd, good_buf + offset, size);
552 prterr("dowrite: write");
554 prt("short write: 0x%x bytes instead of 0x%x\n",
563 dotruncate(unsigned size)
565 int oldsize = file_size;
567 size -= size % truncbdy;
568 if (size > biggest) {
570 if (!quiet && testcalls > simulatedopcount)
571 prt("truncating to largest ever: 0x%x\n", size);
574 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
576 if (size > file_size)
577 memset(good_buf + file_size, '\0', size - file_size);
580 if (testcalls <= simulatedopcount)
583 if ((progressinterval && testcalls % progressinterval == 0) ||
584 (debug && (monitorstart == -1 || monitorend == -1 ||
585 size <= monitorend)))
586 prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
587 if (yaffs_ftruncate(fd, (off_t)size) == -1) {
588 prt("ftruncate1: %x\n", size);
589 prterr("dotruncate: ftruncate");
600 if (yaffs_lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
601 prterr("writefileimage: lseek");
604 iret = yaffs_write(fd, good_buf, file_size);
605 if ((off_t)iret != file_size) {
607 prterr("writefileimage: write");
609 prt("short write: 0x%x bytes instead of 0x%llx\n",
610 iret, (unsigned long long)file_size);
613 if (lite ? 0 : yaffs_ftruncate(fd, file_size) == -1) {
614 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
615 prterr("writefileimage: ftruncate");
624 if (testcalls <= simulatedopcount)
628 prt("%lu close/open\n", testcalls);
629 if (yaffs_close(fd)) {
630 prterr("docloseopen: close");
633 fd = yaffs_open(fname, O_RDWR, 0);
635 prterr("docloseopen: open");
644 unsigned long offset;
645 unsigned long size = maxoplen;
646 unsigned long rv = random();
647 unsigned long op = rv % (3 + !lite + mapped_writes);
649 /* turn off the map read if necessary */
651 if (op == 2 && !mapped_reads)
654 if (simulatedopcount > 0 && testcalls == simulatedopcount)
660 closeopen = (rv >> 3) < (1 << 28) / closeprob;
662 if (debugstart > 0 && testcalls >= debugstart)
665 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
666 prt("%lu...\n", testcalls);
673 * MAPWRITE: op = 3 or 4
675 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
676 dotruncate(random() % maxfilelen);
679 size = random() % (maxoplen+1);
680 if (lite ? 0 : op == 3)
684 if (op == 1 || op == (lite ? 3 : 4)) {
685 offset %= maxfilelen;
686 if (offset + size > maxfilelen)
687 size = maxfilelen - offset;
688 dowrite(offset, size);
694 if (offset + size > file_size)
695 size = file_size - offset;
696 doread(offset, size);
700 if (sizechecks && testcalls > simulatedopcount)
712 prt("signal %d\n", sig);
713 prt("testcalls = %lu\n", testcalls);
721 fprintf(stdout, "usage: %s",
722 "fsx [-dnqLOW] [-b opnum] [-c Prob] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] fname\n\
723 -b opnum: beginning operation number (default 1)\n\
724 -c P: 1 in P chance of file close+open at each op (default infinity)\n\
725 -d: debug output for all operations\n\
726 -l flen: the upper bound on file size (default 262144)\n\
727 -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
728 -n: no verifications of file size\n\
729 -o oplen: the upper bound on operation size (default 65536)\n\
730 -p progressinterval: debug output at specified operation interval\n\
731 -q: quieter operation\n\
732 -r readbdy: 4096 would make reads page aligned (default 1)\n\
733 -s style: 1 gives smaller truncates (default 0)\n\
734 -t truncbdy: 4096 would make truncates page aligned (default 1)\n\
735 -w writebdy: 4096 would make writes page aligned (default 1)\n\
736 -D startingop: debug output starting at specified operation\n\
737 -L: fsxLite - no file creations & no file size changes\n\
738 -N numops: total # operations to do (default infinity)\n\
739 -O: use oplen (see -o flag) for every op (default random)\n\
740 -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
741 -S seed: for random # generator (default 1) 0 gets timestamp\n\
742 fname: this filename is REQUIRED (no default)\n");
748 getnum(char *s, char **e)
753 ret = strtol(s, e, 0);
781 #define BASE_NAME "/flash/yaffs2"
784 main(int argc, char **argv)
794 page_size = getpagesize();
795 page_mask = page_size - 1;
797 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
799 while ((ch = getopt(argc, argv, "b:c:dl:m:no:p:qr:s:t:w:D:LN:OP:RS:W"))
803 simulatedopcount = getnum(optarg, &endp);
805 fprintf(stdout, "Will begin at operation %ld\n",
807 if (simulatedopcount == 0)
809 simulatedopcount -= 1;
812 closeprob = getnum(optarg, &endp);
815 "Chance of close/open is 1 in %d\n",
824 maxfilelen = getnum(optarg, &endp);
829 monitorstart = getnum(optarg, &endp);
830 if (monitorstart < 0)
832 if (!endp || *endp++ != ':')
834 monitorend = getnum(endp, &endp);
838 monitorend = -1; /* aka infinity */
844 maxoplen = getnum(optarg, &endp);
849 progressinterval = getnum(optarg, &endp);
850 if (progressinterval < 0)
857 readbdy = getnum(optarg, &endp);
862 style = getnum(optarg, &endp);
863 if (style < 0 || style > 1)
867 truncbdy = getnum(optarg, &endp);
872 writebdy = getnum(optarg, &endp);
877 debugstart = getnum(optarg, &endp);
885 numops = getnum(optarg, &endp);
893 strncpy(goodfile, optarg, sizeof(goodfile));
894 strcat(goodfile, "/");
895 strncpy(logfile, optarg, sizeof(logfile));
896 strcat(logfile, "/");
902 seed = getnum(optarg, &endp);
904 seed = time(0) % 10000;
906 fprintf(stdout, "Seed set to %d\n", seed);
913 fprintf(stdout, "mapped writes DISABLED\n");
924 yaffs_mount(BASE_NAME);
926 fname = BASE_NAME "/fsxdata";
928 signal(SIGHUP, cleanup);
929 signal(SIGINT, cleanup);
930 signal(SIGPIPE, cleanup);
931 signal(SIGALRM, cleanup);
932 signal(SIGTERM, cleanup);
933 signal(SIGXCPU, cleanup);
934 signal(SIGXFSZ, cleanup);
935 signal(SIGVTALRM, cleanup);
936 signal(SIGUSR1, cleanup);
937 signal(SIGUSR2, cleanup);
939 initstate(seed, state, 256);
941 fd = yaffs_open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
946 strncat(goodfile, fname, 256);
947 strcat (goodfile, ".fsxgood");
948 fsxgoodfd = yaffs_open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
953 strncat(logfile, "fsx", 256);
954 strcat (logfile, ".fsxlog");
955 fsxlogf = fopen(logfile, "w");
956 if (fsxlogf == NULL) {
962 file_size = maxfilelen = yaffs_lseek(fd, (off_t)0, SEEK_END);
963 if (file_size == (off_t)-1) {
965 warn("main: lseek eof");
968 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
969 if (ret == (off_t)-1) {
971 warn("main: lseek 0");
975 original_buf = (char *) malloc(maxfilelen);
976 for (i = 0; i < maxfilelen; i++)
977 original_buf[i] = random() % 256;
978 good_buf = (char *) malloc(maxfilelen);
979 memset(good_buf, '\0', maxfilelen);
980 temp_buf = (char *) malloc(maxoplen);
981 memset(temp_buf, '\0', maxoplen);
982 if (lite) { /* zero entire existing file */
985 written = yaffs_write(fd, good_buf, (size_t)maxfilelen);
986 if (written != maxfilelen) {
989 warn("main: error on write");
991 warn("main: short write, 0x%x bytes instead of 0x%x\n",
992 (unsigned)written, maxfilelen);
998 while (numops == -1 || numops--)
1001 if (yaffs_close(fd)) {
1006 yaffs_close(fsxgoodfd);
1008 yaffs_unmount(BASE_NAME);
1009 prt("All operations completed A-OK!\n");