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 */
110 char fname[200]; /* name of our test file */
111 char mount_name[200];
113 int fd; /* fd for our test file */
118 unsigned long testcalls = 0; /* calls to function "test" */
120 unsigned long simulatedopcount = 0; /* -b flag */
121 int 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 long numops = /*-1 */ 10000000; /* -N flag */
137 int randomoplen = 1; /* -O flag disables it */
138 int seed = 1; /* -S flag */
140 int mapped_writes = 0; /* yaffs direct does not support mmapped files */
141 int mapped_reads = 0;
144 FILE * fsxlogf = NULL;
152 printf("fsx wanted to exit with %d\n",x);
161 vwarnc(code, fmt, ap)
166 fprintf(stderr, "fsx: ");
168 vfprintf(stderr, fmt, ap);
169 fprintf(stderr, ": ");
171 fprintf(stderr, "%s\n", strerror(code));
176 warn(const char * fmt, ...)
180 vwarnc(errno, fmt, ap);
191 vfprintf(stdout, fmt, args);
193 vfprintf(fsxlogf, fmt, args);
200 prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
205 log4(int operation, int arg0, int arg1, int arg2)
207 struct log_entry *le;
210 le->operation = operation;
212 le->operation = ~ le->operation;
218 if (logptr >= LOGSIZE)
227 struct log_entry *lp;
229 prt("LOG DUMP (%d total operations):\n", logcount);
230 if (logcount < LOGSIZE) {
237 for ( ; count > 0; count--) {
240 opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
241 prt("%d(%d mod 256): ", opnum, opnum%256);
243 if ((closeopen = lp->operation < 0))
244 lp->operation = ~ lp->operation;
246 switch (lp->operation) {
248 prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)",
249 lp->args[0], lp->args[0] + lp->args[1] - 1,
251 if (badoff >= lp->args[0] && badoff <
252 lp->args[0] + lp->args[1])
256 prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
257 lp->args[0], lp->args[0] + lp->args[1] - 1,
259 if (badoff >= lp->args[0] && badoff <
260 lp->args[0] + lp->args[1])
264 prt("READ\t0x%x thru 0x%x\t(0x%x bytes)",
265 lp->args[0], lp->args[0] + lp->args[1] - 1,
267 if (badoff >= lp->args[0] &&
268 badoff < lp->args[0] + lp->args[1])
272 prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)",
273 lp->args[0], lp->args[0] + lp->args[1] - 1,
275 if (lp->args[0] > lp->args[2])
277 else if (lp->args[0] + lp->args[1] > lp->args[2])
279 if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
280 badoff < lp->args[0] + lp->args[1])
284 down = lp->args[0] < lp->args[1];
285 prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
286 down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
287 if (badoff >= lp->args[!down] &&
288 badoff < lp->args[!!down])
292 prt("SKIPPED (no operation)");
295 prt("BOGUS LOG ENTRY (operation code = %d)!",
299 prt("\n\t\tCLOSE/OPEN");
309 save_buffer(char *buffer, off_t bufferlength, int fd)
312 ssize_t byteswritten;
314 if (fd <= 0 || bufferlength == 0)
317 if (bufferlength > SSIZE_MAX) {
318 prt("fsx flaw: overflow in save_buffer\n");
322 off_t size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
323 if (size_by_seek == (off_t)-1)
324 prterr("save_buffer: lseek eof");
325 else if (bufferlength > size_by_seek) {
326 warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek,
327 (unsigned long long)bufferlength);
328 bufferlength = size_by_seek;
332 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
333 if (ret == (off_t)-1)
334 prterr("save_buffer: lseek 0");
336 byteswritten = yaffs_write(fd, buffer, (size_t)bufferlength);
337 if (byteswritten != bufferlength) {
338 if (byteswritten == -1)
339 prterr("save_buffer write");
341 warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
342 (unsigned)byteswritten,
343 (unsigned long long)bufferlength);
349 report_failure(int status)
355 save_buffer(good_buf, file_size, fsxgoodfd);
356 prt("Correct content saved for comparison\n");
357 prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
362 prt("Exiting with %d\n",status);
367 #define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
368 *(((unsigned char *)(cp)) + 1)))
371 check_buffers(unsigned offset, unsigned size)
379 if (memcmp(good_buf + offset, temp_buf, size) != 0) {
380 prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n",
382 prt("OFFSET\tGOOD\tBAD\tRANGE\n");
384 c = good_buf[offset];
388 bad = short_at(&temp_buf[i]);
389 prt("0x%5x\t0x%04x\t0x%04x", offset,
390 short_at(&good_buf[offset]), bad);
391 op = temp_buf[offset & 1 ? i+1 : i];
403 prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff));
405 prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n");
407 prt("????????????????\n");
416 struct yaffs_stat statbuf;
419 if (yaffs_fstat(fd, &statbuf)) {
420 prterr("check_size: fstat");
421 statbuf.st_size = -1;
423 size_by_seek = yaffs_lseek(fd, (off_t)0, SEEK_END);
424 if (file_size != statbuf.st_size || 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);
435 check_trunc_hack(void)
437 struct yaffs_stat statbuf;
439 yaffs_ftruncate(fd, (off_t)0);
440 yaffs_ftruncate(fd, (off_t)100000);
441 yaffs_fstat(fd, &statbuf);
442 if (statbuf.st_size != (off_t)100000) {
443 prt("no extend on truncate! not posix!\n");
446 yaffs_ftruncate(fd, (off_t)0);
451 doread(unsigned offset, unsigned size)
456 offset -= offset % readbdy;
458 if (!quiet && testcalls > simulatedopcount)
459 prt("skipping zero size read\n");
460 log4(OP_SKIPPED, OP_READ, offset, size);
463 if (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);
470 log4(OP_READ, offset, size, 0);
472 if (testcalls <= simulatedopcount)
475 if (!quiet && ((progressinterval &&
476 testcalls % progressinterval == 0) ||
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 = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
484 if (ret == (off_t)-1) {
485 prterr("doread: lseek");
488 iret = yaffs_read(fd, temp_buf, size);
491 prterr("doread: read");
493 prt("short read: 0x%x bytes instead of 0x%x\n",
497 check_buffers(offset, size);
505 gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
508 good_buf[offset] = testcalls % 256;
510 good_buf[offset] += original_buf[offset];
517 dowrite(unsigned offset, unsigned size)
522 offset -= offset % writebdy;
524 if (!quiet && testcalls > simulatedopcount)
525 prt("skipping zero size write\n");
526 log4(OP_SKIPPED, OP_WRITE, offset, size);
530 log4(OP_WRITE, offset, size, file_size);
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;
538 warn("Lite file size bug in fsx!");
543 if (testcalls <= simulatedopcount)
546 if (!quiet && ((progressinterval &&
547 testcalls % progressinterval == 0) ||
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 = yaffs_lseek(fd, (off_t)offset, SEEK_SET);
555 if (ret == (off_t)-1) {
556 prterr("dowrite: lseek");
559 iret = yaffs_write(fd, good_buf + offset, size);
562 prterr("dowrite: write");
564 prt("short write: 0x%x bytes instead of 0x%x\n",
573 dotruncate(unsigned size)
575 int oldsize = file_size;
577 size -= size % truncbdy;
578 if (size > biggest) {
580 if (!quiet && testcalls > simulatedopcount)
581 prt("truncating to largest ever: 0x%x\n", size);
584 log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
586 if (size > file_size)
587 memset(good_buf + file_size, '\0', size - file_size);
590 if (testcalls <= simulatedopcount)
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 (yaffs_ftruncate(fd, (off_t)size) == -1) {
598 prt("ftruncate1: %x\n", size);
599 prterr("dotruncate: ftruncate");
610 if (yaffs_lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
611 prterr("writefileimage: lseek");
614 iret = yaffs_write(fd, good_buf, file_size);
615 if ((off_t)iret != file_size) {
617 prterr("writefileimage: write");
619 prt("short write: 0x%x bytes instead of 0x%llx\n",
620 iret, (unsigned long long)file_size);
623 if (lite ? 0 : yaffs_ftruncate(fd, file_size) == -1) {
624 prt("ftruncate2: %llx\n", (unsigned long long)file_size);
625 prterr("writefileimage: ftruncate");
634 if (testcalls <= simulatedopcount)
638 prt("%lu close/open\n", testcalls);
639 if (yaffs_close(fd)) {
640 prterr("docloseopen: close");
643 fd = yaffs_open(fname, O_RDWR, 0);
645 prterr("docloseopen: open");
652 yaffs_fsx_do_op(void)
654 unsigned long offset;
655 unsigned long size = maxoplen;
656 unsigned long rv = random();
657 unsigned long op = rv % (3 + !lite + mapped_writes);
659 /* turn off the map read if necessary */
661 if (op == 2 && !mapped_reads)
664 if (simulatedopcount > 0 && testcalls == simulatedopcount)
670 closeopen = (rv >> 3) < (1 << 28) / closeprob;
672 if (debugstart > 0 && testcalls >= debugstart)
675 if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
676 prt("%lu...\n", testcalls);
683 * MAPWRITE: op = 3 or 4
685 if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */
686 dotruncate(random() % maxfilelen);
689 size = random() % (maxoplen+1);
690 if (lite ? 0 : op == 3)
694 if (op == 1 || op == (lite ? 3 : 4)) {
695 offset %= maxfilelen;
696 if (offset + size > maxfilelen)
697 size = maxfilelen - offset;
698 dowrite(offset, size);
704 if (offset + size > file_size)
705 size = file_size - offset;
706 doread(offset, size);
710 if (sizechecks && testcalls > simulatedopcount)
722 prt("signal %d\n", sig);
723 prt("testcalls = %lu\n", testcalls);
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");
758 getnum(char *s, char **e)
763 ret = strtol(s, e, 0);
792 extern int random_seed;
793 extern int simulate_power_failure;
796 int mounted_by_fsx = 0;
799 yaffs_fsx_init(const char *mount_pt)
807 page_size = getpagesize();
808 page_mask = page_size - 1;
810 setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
812 strcpy(mount_name,mount_pt);
813 strcpy(fname,mount_name);
814 strcat(fname,"/fsxdata");
817 signal(SIGHUP, cleanup);
818 signal(SIGINT, cleanup);
819 signal(SIGPIPE, cleanup);
820 signal(SIGALRM, cleanup);
821 signal(SIGTERM, cleanup);
822 signal(SIGXCPU, cleanup);
823 signal(SIGXFSZ, cleanup);
824 signal(SIGVTALRM, cleanup);
825 signal(SIGUSR1, cleanup);
826 signal(SIGUSR2, cleanup);
829 initstate(seed, state, 256);
831 fd = yaffs_open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666);
836 strncat(goodfile, fname, 256);
837 strcat (goodfile, ".fsxgood");
838 fsxgoodfd = yaffs_open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
843 strncat(logfile, "fsx", 256);
844 strcat (logfile, ".fsxlog");
845 fsxlogf = fopen(logfile, "w");
846 if (fsxlogf == NULL) {
852 file_size = maxfilelen = yaffs_lseek(fd, (off_t)0, SEEK_END);
853 if (file_size == (off_t)-1) {
855 warn("main: lseek eof");
858 ret = yaffs_lseek(fd, (off_t)0, SEEK_SET);
859 if (ret == (off_t)-1) {
861 warn("main: lseek 0");
865 original_buf = (char *) malloc(maxfilelen);
866 for (i = 0; i < maxfilelen; i++)
867 original_buf[i] = random() % 256;
868 good_buf = (char *) malloc(maxfilelen);
869 memset(good_buf, '\0', maxfilelen);
870 temp_buf = (char *) malloc(maxoplen);
871 memset(temp_buf, '\0', maxoplen);
872 if (lite) { /* zero entire existing file */
875 written = yaffs_write(fd, good_buf, (size_t)maxfilelen);
876 if (written != maxfilelen) {
879 warn("main: error on write");
881 warn("main: short write, 0x%x bytes instead of 0x%x\n",
882 (unsigned)written, maxfilelen);
892 int yaffs_fsx_complete(void)
894 if (yaffs_close(fd)) {
899 yaffs_close(fsxgoodfd);
901 prt("All operations completed A-OK!\n");
908 yaffs_fsx_main(const char *mount_pt)
910 yaffs_fsx_init(mount_pt);
911 while (numops == -1 || numops--)
913 yaffs_fsx_complete();