From 7c666cd589631a555da943396a3124a610487b7e Mon Sep 17 00:00:00 2001 From: Charles Manning Date: Tue, 17 Mar 2020 16:45:01 +1300 Subject: [PATCH] Add fsx file test and split up basic tests. Signed-off-by: Charles Manning --- rtems/rtems-y-test/{ => basic-test}/Makefile | 15 +- .../basic-test/yaffs-rtems-basic-test.c | 169 ++++ .../{ => common}/yaffs-rtems-flashsim.c | 12 +- .../{ => common}/yaffs-rtems-flashsim.h | 0 .../yaffs-rtems-test-wrapper.c} | 90 +- rtems/rtems-y-test/fsx/Makefile | 58 ++ rtems/rtems-y-test/fsx/rtems-fsx.c | 938 ++++++++++++++++++ 7 files changed, 1198 insertions(+), 84 deletions(-) rename rtems/rtems-y-test/{ => basic-test}/Makefile (66%) create mode 100644 rtems/rtems-y-test/basic-test/yaffs-rtems-basic-test.c rename rtems/rtems-y-test/{ => common}/yaffs-rtems-flashsim.c (97%) rename rtems/rtems-y-test/{ => common}/yaffs-rtems-flashsim.h (100%) rename rtems/rtems-y-test/{yaffs-rtems-test.c => common/yaffs-rtems-test-wrapper.c} (62%) create mode 100644 rtems/rtems-y-test/fsx/Makefile create mode 100644 rtems/rtems-y-test/fsx/rtems-fsx.c diff --git a/rtems/rtems-y-test/Makefile b/rtems/rtems-y-test/basic-test/Makefile similarity index 66% rename from rtems/rtems-y-test/Makefile rename to rtems/rtems-y-test/basic-test/Makefile index 4018051..9576d69 100644 --- a/rtems/rtems-y-test/Makefile +++ b/rtems/rtems-y-test/basic-test/Makefile @@ -13,7 +13,8 @@ PGM=${ARCH}/$(EXEC) MANAGERS=all # C source names -CSRCS = yaffs-rtems-test.c yaffs-rtems-flashsim.c +CSRCS = yaffs-rtems-basic-test.c +CSRCS += yaffs-rtems-test-wrapper.c yaffs-rtems-flashsim.c COBJS_ = $(CSRCS:.c=.o) COBJS = $(COBJS_:%=${ARCH}/%) @@ -40,6 +41,18 @@ OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS) all: ${ARCH} $(PGM) +#Create symlinks +yaffs-rtems-test-wrapper.c: ../common/yaffs-rtems-test-wrapper.c + ln -sf $^ $@ + +yaffs-rtems-flashsim.c: ../common/yaffs-rtems-flashsim.c + ln -sf $^ $@ + +yaffs-rtems-flashsim.h: ../common/yaffs-rtems-flashsim.h + ln -sf $^ $@ + +$(OBJS): yaffs-rtems-flashsim.h + $(PGM): $(OBJS) $(make-exe) diff --git a/rtems/rtems-y-test/basic-test/yaffs-rtems-basic-test.c b/rtems/rtems-y-test/basic-test/yaffs-rtems-basic-test.c new file mode 100644 index 0000000..27448f2 --- /dev/null +++ b/rtems/rtems-y-test/basic-test/yaffs-rtems-basic-test.c @@ -0,0 +1,169 @@ +/* + * Simple test program -- demonstrating use of IMFS + */ + +#include + +#include +#include +#include +#include + +#include + +#include +#include + + +#define YPATH "/yaffs_mount_pt" + + + + + +void set_uint8_t_buffer(uint8_t *buf, uint32_t n, uint8_t start, uint8_t inc) +{ + while (n) { + *buf = start; + buf++; + start += inc; + n--; + } +} + + +#define FNAME YPATH"/test" +#define DIRNAME YPATH"/dirtest" + +void dump_directory_tree_worker(const char *dname,int recursive) +{ + DIR *d; + struct dirent *de; + struct stat s; + char str[1000]; + + d = opendir(dname); + + if(!d) + { + printf("opendir failed\n"); + } + else + { + while((de = readdir(d)) != NULL) + { + sprintf(str,"%s/%s",dname,de->d_name); + + lstat(str,&s); + + printf("%s inode %d length %d mode %X ", + str, (int)s.st_ino, (int)s.st_size, s.st_mode); + switch(s.st_mode & S_IFMT) + { + case S_IFREG: printf("data file"); break; + case S_IFDIR: printf("directory"); break; + case S_IFLNK: printf("symlink -->"); + if(readlink(str,str,100) < 0) + printf("no alias"); + else + printf("\"%s\"",str); + break; + default: printf("unknown"); break; + } + + printf("\n"); + + if((s.st_mode & S_IFMT) == S_IFDIR && recursive) + dump_directory_tree_worker(str,1); + + } + + closedir(d); + } + +} + +static void dump_directory_tree(const char *dname) +{ + dump_directory_tree_worker(dname,1); +} + + +void dumpDir(const char *dname) +{ dump_directory_tree_worker(dname,0); +} + +int run_the_test(void) +{ + int fd; + int ret; + uint8_t buf[100]; + uint8_t buf2[100]; + + + dump_directory_tree(YPATH); + + ret = mkdir(DIRNAME, 0777); + if (ret < 0) + perror("mkdir "DIRNAME); + + fd = open(FNAME, O_RDWR | O_CREAT | O_TRUNC, 0666); + printf("open %s = %d\n", FNAME, fd); + if (fd < 0) + perror("opening " FNAME); + + set_uint8_t_buffer(buf, sizeof(buf), 0xAA, 1); + + ret = write(fd, buf, sizeof(buf)); + + printf("write returned %d\n", ret); + + if (ret == -1) + perror("writing file"); + + ret = lseek(fd, 0, SEEK_END); + + printf("lseek end ret = %d\n", ret); + + ret = lseek(fd, 0, SEEK_SET); + printf("lseek start ret = %d\n", ret); + + ret = read(fd, buf2, sizeof(buf2)); + + printf("reading file ret = %d\n", ret); + + if (ret == -1) + perror("reading file"); + + + return ret; + +#if 0 + + + fd = open("test1", O_CREAT); + printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) ); + close(fd); + + fd = open("test", O_RDONLY); + if (fd == -1) { + printf("Starting on the wrong foot....\n"); + exit(-1); + } + + printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) ); + + fp = fdopen(fd, "r"); + if (fp == NULL) { + printf("Nothing ever goes my way!\n"); + close(fd); + exit(-1); + } else { + printf("Soon, I will be able to take over the world!\n"); + fgets(str, 200, fp); + printf("%s\n", str); + fclose(fp); + } +#endif +} + diff --git a/rtems/rtems-y-test/yaffs-rtems-flashsim.c b/rtems/rtems-y-test/common/yaffs-rtems-flashsim.c similarity index 97% rename from rtems/rtems-y-test/yaffs-rtems-flashsim.c rename to rtems/rtems-y-test/common/yaffs-rtems-flashsim.c index a49ff44..b6c83f2 100644 --- a/rtems/rtems-y-test/yaffs-rtems-flashsim.c +++ b/rtems/rtems-y-test/common/yaffs-rtems-flashsim.c @@ -4,7 +4,7 @@ #include "yaffs-rtems-flashsim.h" #include -#include "../yaffs_guts.h" +#include "../../yaffs_guts.h" @@ -16,6 +16,12 @@ #define PAGES_PER_BLOCK 64 +#if 0 +#define dout printf +#else +#define dout(...) do { } while(0) +#endif + typedef struct { unsigned char page[PAGES_PER_BLOCK][PAGE_SIZE]; unsigned blockOk; @@ -88,6 +94,7 @@ static int yramsim_rd_chunk (struct yaffs_dev *dev, int pageId, unsigned pageOffset = pageId % PAGES_PER_BLOCK; unsigned char * d; unsigned char *s; + if(blockId >= sim->nBlocks || pageOffset >= PAGES_PER_BLOCK || dataLength >DATA_SIZE || @@ -122,6 +129,9 @@ static int yramsim_wr_chunk (struct yaffs_dev *dev, int pageId, unsigned pageOffset = pageId % PAGES_PER_BLOCK; unsigned char * d; unsigned char *s; + + dout("wr_chunk\n"); + if(blockId >= sim->nBlocks || pageOffset >= PAGES_PER_BLOCK || dataLength >DATA_SIZE || diff --git a/rtems/rtems-y-test/yaffs-rtems-flashsim.h b/rtems/rtems-y-test/common/yaffs-rtems-flashsim.h similarity index 100% rename from rtems/rtems-y-test/yaffs-rtems-flashsim.h rename to rtems/rtems-y-test/common/yaffs-rtems-flashsim.h diff --git a/rtems/rtems-y-test/yaffs-rtems-test.c b/rtems/rtems-y-test/common/yaffs-rtems-test-wrapper.c similarity index 62% rename from rtems/rtems-y-test/yaffs-rtems-test.c rename to rtems/rtems-y-test/common/yaffs-rtems-test-wrapper.c index 8e1c620..0ac7a67 100644 --- a/rtems/rtems-y-test/yaffs-rtems-test.c +++ b/rtems/rtems-y-test/common/yaffs-rtems-test-wrapper.c @@ -14,8 +14,8 @@ #include "yaffs-rtems-flashsim.h" -//#define YPATH "/yaffs_mount_pt/" -#define YPATH "" +#define YPATH "/yaffs_mount_pt" +//#define YPATH "" void yaffs_bug_fn(const char *file_name, int line_no) { @@ -48,92 +48,19 @@ int filesystem_init(const char *mount_target) mount_args.dev = device; if (mount_and_make_target_path(NULL, - NULL /*mount_target */, + mount_target, RTEMS_FILESYSTEM_TYPE_YAFFS, RTEMS_FILESYSTEM_READ_WRITE, &mount_args) < 0) { + perror("mount_and_make"); return errno; } else { + chmod(mount_target, 0777); /* Make partition rw/modifiable */ return 0; } } - -void set_uint8_t_buffer(uint8_t *buf, uint32_t n, uint8_t start, uint8_t inc) -{ - while (n) { - *buf = start; - buf++; - start += inc; - n--; - } -} - -int run_basic_file_test(void) -{ - int fd; - int ret; - uint8_t buf[100]; - uint8_t buf2[100]; - - fd = open(YPATH"/test", O_RDWR | O_CREAT | O_TRUNC, 0666); - printf("open = %d\n", fd); - - set_uint8_t_buffer(buf, sizeof(buf), 0xAA, 1); - - ret = write(fd, buf, sizeof(buf)); - - printf("write returned %d\n", ret); - - if (ret == -1) - perror("writing file"); - - ret = lseek(fd, 0, SEEK_END); - - printf("lseek end ret = %d\n", ret); - - ret = lseek(fd, 0, SEEK_SET); - printf("lseek start ret = %d\n", ret); - - ret = read(fd, buf2, sizeof(buf2)); - - printf("reading file ret = %d\n", ret); - - if (ret == -1) - perror("reading file"); - - - return ret; - -#if 0 - - - fd = open("test1", O_CREAT); - printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) ); - close(fd); - - fd = open("test", O_RDONLY); - if (fd == -1) { - printf("Starting on the wrong foot....\n"); - exit(-1); - } - - printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) ); - - fp = fdopen(fd, "r"); - if (fp == NULL) { - printf("Nothing ever goes my way!\n"); - close(fd); - exit(-1); - } else { - printf("Soon, I will be able to take over the world!\n"); - fgets(str, 200, fp); - printf("%s\n", str); - fclose(fp); - } -#endif -} - +extern int run_the_test(void); rtems_task Init( rtems_task_argument ignored @@ -147,8 +74,7 @@ rtems_task Init( printf("filesystem_init(\"%s\") returned %d\n", YPATH, err); - run_basic_file_test(); - + run_the_test(); exit(0); } @@ -178,7 +104,7 @@ previously-registered handler. #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM -#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6 +#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 32 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE #define CONFIGURE_MAXIMUM_TASKS 1 diff --git a/rtems/rtems-y-test/fsx/Makefile b/rtems/rtems-y-test/fsx/Makefile new file mode 100644 index 0000000..0b35cf0 --- /dev/null +++ b/rtems/rtems-y-test/fsx/Makefile @@ -0,0 +1,58 @@ +# +# Makefile for rtems-fsx.exe +# + +# +# RTEMS_MAKEFILE_PATH is typically set in an environment variable +# + +EXEC=rtems-fsx.exe +PGM=${ARCH}/$(EXEC) + +# optional managers required +MANAGERS=all + +# C source names +CSRCS = rtems-fsx.c +CSRCS += yaffs-rtems-test-wrapper.c yaffs-rtems-flashsim.c +COBJS_ = $(CSRCS:.c=.o) +COBJS = $(COBJS_:%=${ARCH}/%) + +# C++ source names +CXXSRCS = +CXXOBJS_ = $(CXXSRCS:.cc=.o) +CXXOBJS = $(CXXOBJS_:%=${ARCH}/%) + +# AS source names +ASSRCS = +ASOBJS_ = $(ASSRCS:.s=.o) +ASOBJS = $(ASOBJS_:%=${ARCH}/%) + +# Libraries +#LIBS = -lrtemsall -lc -lyaffs2 +LINK_LIBS = -lyaffs2 + +include $(RTEMS_MAKEFILE_PATH)/Makefile.inc + +include $(RTEMS_CUSTOM) +include $(PROJECT_ROOT)/make/leaf.cfg + +OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS) + +all: ${ARCH} $(PGM) + +#Create symlinks +yaffs-rtems-test-wrapper.c: ../common/yaffs-rtems-test-wrapper.c + ln -sf $^ $@ + +yaffs-rtems-flashsim.c: ../common/yaffs-rtems-flashsim.c + ln -sf $^ $@ + +yaffs-rtems-flashsim.h: ../common/yaffs-rtems-flashsim.h + ln -sf $^ $@ + +$(OBJS): yaffs-rtems-flashsim.h + +$(PGM): $(OBJS) + $(make-exe) + diff --git a/rtems/rtems-y-test/fsx/rtems-fsx.c b/rtems/rtems-y-test/fsx/rtems-fsx.c new file mode 100644 index 0000000..5fc5db0 --- /dev/null +++ b/rtems/rtems-y-test/fsx/rtems-fsx.c @@ -0,0 +1,938 @@ +/* + * Copyright (c) 1998-2001 Apple Computer, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * The contents of this file constitute Original Code as defined in and + * are subject to the Apple Public Source License Version 1.2 (the + * "License"). You may not use this file except in compliance with the + * License. Please obtain a copy of the License at + * http://www.apple.com/publicsource and read it before using this file. + * + * This Original Code and all software distributed under the License are + * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the + * License for the specific language governing rights and limitations + * under the License. + * + * @APPLE_LICENSE_HEADER_END@ + * + * WARNING--WARNING--WARNING + * This is not the original fsx.c. It has been modified to run with + * yaffs direct. Seek out the original fsx.c if you want to do anything + * else. + * + * + * + * File: fsx.c + * Author: Avadis Tevanian, Jr. + * + * File system exerciser. + * + * Rewrite and enhancements 1998-2001 Conrad Minshall -- conrad@mac.com + * + * Various features from Joe Sokol, Pat Dirks, and Clark Warner. + * + * Small changes to work under Linux -- davej@suse.de + * + * Sundry porting patches from Guy Harris 12/2001 + * + * Checks for mmap last-page zero fill. + * + * Modified heavily by Charles Manning to exercise via the + * yaffs direct interface. + * + */ + + +#include +#include +#ifdef _UWIN +# include +# include +# include +# include +#endif +#include +#include +#ifndef MAP_FILE +# define MAP_FILE 0 +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */ + +/* + * A log entry is an operation and a bunch of arguments. + */ + +struct log_entry { + int operation; + int args[3]; +}; + +#define LOGSIZE 1000 + +struct log_entry oplog[LOGSIZE]; /* the log */ +int logptr = 0; /* current position in log */ +int logcount = 0; /* total ops */ + +/* + * Define operations + */ + +#define OP_READ 1 +#define OP_WRITE 2 +#define OP_TRUNCATE 3 +#define OP_CLOSEOPEN 4 +#define OP_MAPREAD 5 +#define OP_MAPWRITE 6 +#define OP_SKIPPED 7 + +int page_size; +int page_mask; + +char *original_buf; /* a pointer to the original data */ +char *good_buf; /* a pointer to the correct data */ +char *temp_buf; /* a pointer to the current data */ + +char fname[200]; /* name of our test file */ +char mount_name[200]; + +int fd; /* fd for our test file */ + +off_t file_size = 0; +off_t biggest = 0; +char state[256]; +unsigned long testcalls = 0; /* calls to function "test" */ + +unsigned long simulatedopcount = 0; /* -b flag */ +unsigned closeprob = 0; /* -c flag */ +int debug = 0; /* -d flag */ +unsigned long debugstart = 0; /* -D flag */ +unsigned long maxfilelen = 256 * 1024; /* -l flag */ +int sizechecks = 1; /* -n flag disables them */ +int maxoplen = 64 * 1024; /* -o flag */ +int quiet = 0; /* -q flag */ +unsigned long progressinterval = 0; /* -p flag */ +int readbdy = 1; /* -r flag */ +int style = 0; /* -s flag */ +int truncbdy = 1; /* -t flag */ +int writebdy = 1; /* -w flag */ +long monitorstart = -1; /* -m flag */ +long monitorend = -1; /* -m flag */ +int lite = 0; /* -L flag */ +int randomoplen = 1; /* -O flag disables it */ +int seed = 1; /* -S flag */ + +int mapped_writes = 0; /* yaffs direct does not support mmapped files */ +int mapped_reads = 0; + +int fsxgoodfd = 0; +FILE * fsxlogf = NULL; +int badoff = -1; +int closeopen = 0; + + + +void EXIT(int x) +{ + printf("fsx wanted to exit with %d\n",x); + while(x){} +} + +char goodfile[1024]; +char logfile[1024]; + + +void +vwarnc(code, fmt, ap) + int code; + const char *fmt; + va_list ap; +{ + fprintf(stderr, "fsx: "); + if (fmt != NULL) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + fprintf(stderr, "%s\n", strerror(code)); +} + + +void +warn(const char * fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vwarnc(errno, fmt, ap); + va_end(ap); +} + + +void +prt(char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vfprintf(stdout, fmt, args); + if (fsxlogf) + vfprintf(fsxlogf, fmt, args); + va_end(args); +} + +void +prterr(char *prefix) +{ + prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno)); +} + + +void +log4(int operation, int arg0, int arg1, int arg2) +{ + struct log_entry *le; + + le = &oplog[logptr]; + le->operation = operation; + if (closeopen) + le->operation = ~ le->operation; + le->args[0] = arg0; + le->args[1] = arg1; + le->args[2] = arg2; + logptr++; + logcount++; + if (logptr >= LOGSIZE) + logptr = 0; +} + + +void +logdump(void) +{ + int i, count, down; + struct log_entry *lp; + + prt("LOG DUMP (%d total operations):\n", logcount); + if (logcount < LOGSIZE) { + i = 0; + count = logcount; + } else { + i = logptr; + count = LOGSIZE; + } + for ( ; count > 0; count--) { + int opnum; + + opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE; + prt("%d(%d mod 256): ", opnum, opnum%256); + lp = &oplog[i]; + if ((closeopen = lp->operation < 0)) + lp->operation = ~ lp->operation; + + switch (lp->operation) { + case OP_MAPREAD: + prt("MAPREAD\t0x%x thru 0x%x\t(0x%x bytes)", + lp->args[0], lp->args[0] + lp->args[1] - 1, + lp->args[1]); + if (badoff >= lp->args[0] && badoff < + lp->args[0] + lp->args[1]) + prt("\t***RRRR***"); + break; + case OP_MAPWRITE: + prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)", + lp->args[0], lp->args[0] + lp->args[1] - 1, + lp->args[1]); + if (badoff >= lp->args[0] && badoff < + lp->args[0] + lp->args[1]) + prt("\t******WWWW"); + break; + case OP_READ: + prt("READ\t0x%x thru 0x%x\t(0x%x bytes)", + lp->args[0], lp->args[0] + lp->args[1] - 1, + lp->args[1]); + if (badoff >= lp->args[0] && + badoff < lp->args[0] + lp->args[1]) + prt("\t***RRRR***"); + break; + case OP_WRITE: + prt("WRITE\t0x%x thru 0x%x\t(0x%x bytes)", + lp->args[0], lp->args[0] + lp->args[1] - 1, + lp->args[1]); + if (lp->args[0] > lp->args[2]) + prt(" HOLE"); + else if (lp->args[0] + lp->args[1] > lp->args[2]) + prt(" EXTEND"); + if ((badoff >= lp->args[0] || badoff >=lp->args[2]) && + badoff < lp->args[0] + lp->args[1]) + prt("\t***WWWW"); + break; + case OP_TRUNCATE: + down = lp->args[0] < lp->args[1]; + prt("TRUNCATE %s\tfrom 0x%x to 0x%x", + down ? "DOWN" : "UP", lp->args[1], lp->args[0]); + if (badoff >= lp->args[!down] && + badoff < lp->args[!!down]) + prt("\t******WWWW"); + break; + case OP_SKIPPED: + prt("SKIPPED (no operation)"); + break; + default: + prt("BOGUS LOG ENTRY (operation code = %d)!", + lp->operation); + } + if (closeopen) + prt("\n\t\tCLOSE/OPEN"); + prt("\n"); + i++; + if (i == LOGSIZE) + i = 0; + } +} + + +void +save_buffer(char *buffer, off_t bufferlength, int fd) +{ + off_t ret; + ssize_t byteswritten; + + if (fd <= 0 || bufferlength == 0) + return; + + if (bufferlength > SSIZE_MAX) { + prt("fsx flaw: overflow in save_buffer\n"); + EXIT(67); + } + if (lite) { + off_t size_by_seek = lseek(fd, (off_t)0, SEEK_END); + if (size_by_seek == (off_t)-1) + prterr("save_buffer: lseek eof"); + else if (bufferlength > size_by_seek) { + warn("save_buffer: .fsxgood file too short... will save 0x%llx bytes instead of 0x%llx\n", (unsigned long long)size_by_seek, + (unsigned long long)bufferlength); + bufferlength = size_by_seek; + } + } + + ret = lseek(fd, (off_t)0, SEEK_SET); + if (ret == (off_t)-1) + prterr("save_buffer: lseek 0"); + + byteswritten = write(fd, buffer, (size_t)bufferlength); + if (byteswritten != bufferlength) { + if (byteswritten == -1) + prterr("save_buffer write"); + else + warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n", + (unsigned)byteswritten, + (unsigned long long)bufferlength); + } +} + + +void +report_failure(int status) +{ + logdump(); + + if (fsxgoodfd) { + if (good_buf) { + save_buffer(good_buf, file_size, fsxgoodfd); + prt("Correct content saved for comparison\n"); + prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n", + fname, fname); + } + close(fsxgoodfd); + } + prt("Exiting with %d\n",status); + EXIT(status); +} + + +#define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \ + *(((unsigned char *)(cp)) + 1))) + +void +check_buffers(unsigned offset, unsigned size) +{ + unsigned char c, t; + unsigned i = 0; + unsigned n = 0; + unsigned op = 0; + unsigned bad = 0; + + if (memcmp(good_buf + offset, temp_buf, size) != 0) { + prt("READ BAD DATA: offset = 0x%x, size = 0x%x\n", + offset, size); + prt("OFFSET\tGOOD\tBAD\tRANGE\n"); + while (size > 0) { + c = good_buf[offset]; + t = temp_buf[i]; + if (c != t) { + if (n == 0) { + bad = short_at(&temp_buf[i]); + prt("0x%5x\t0x%04x\t0x%04x", offset, + short_at(&good_buf[offset]), bad); + op = temp_buf[offset & 1 ? i+1 : i]; + } + n++; + badoff = offset; + } + offset++; + i++; + size--; + } + if (n) { + prt("\t0x%5x\n", n); + if (bad) + prt("operation# (mod 256) for the bad data may be %u\n", ((unsigned)op & 0xff)); + else + prt("operation# (mod 256) for the bad data unknown, check HOLE and EXTEND ops\n"); + } else + prt("????????????????\n"); + report_failure(110); + } +} + + +void +check_size(void) +{ + struct stat statbuf; + off_t size_by_seek; + + if (fstat(fd, &statbuf)) { + prterr("check_size: fstat"); + statbuf.st_size = -1; + } + size_by_seek = lseek(fd, (off_t)0, SEEK_END); + if (file_size != statbuf.st_size || + file_size != size_by_seek) { + prt("Size error: expected 0x%llx stat 0x%llx seek 0x%llx\n", + (unsigned long long)file_size, + (unsigned long long)statbuf.st_size, + (unsigned long long)size_by_seek); + report_failure(120); + } +} + + +void +check_trunc_hack(void) +{ + struct stat statbuf; + + ftruncate(fd, (off_t)0); + ftruncate(fd, (off_t)100000); + fstat(fd, &statbuf); + if (statbuf.st_size != (off_t)100000) { + prt("no extend on truncate! not posix!\n"); + EXIT(130); + } + ftruncate(fd, (off_t)0); +} + + +void +doread(unsigned offset, unsigned size) +{ + off_t ret; + unsigned iret; + + offset -= offset % readbdy; + if (size == 0) { + if (!quiet && testcalls > simulatedopcount) + prt("skipping zero size read\n"); + log4(OP_SKIPPED, OP_READ, offset, size); + return; + } + if ((off_t)(size + offset) > file_size) { + if (!quiet && testcalls > simulatedopcount) + prt("skipping seek/read past end of file\n"); + log4(OP_SKIPPED, OP_READ, offset, size); + return; + } + + log4(OP_READ, offset, size, 0); + + if (testcalls <= simulatedopcount) + return; + + if (!quiet && ((progressinterval && + testcalls % progressinterval == 0) || + (debug && + (monitorstart == -1 || + (offset + size > monitorstart && + (monitorend == -1 || offset <= monitorend)))))) + prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, + offset, offset + size - 1, size); + ret = lseek(fd, (off_t)offset, SEEK_SET); + if (ret == (off_t)-1) { + prterr("doread: lseek"); + report_failure(140); + } + iret = read(fd, temp_buf, size); + if (iret != size) { + if (iret == (unsigned)(-1)) + prterr("doread: read"); + else + prt("short read: 0x%x bytes instead of 0x%x\n", + iret, size); + report_failure(141); + } + check_buffers(offset, size); +} + + + + + +void +gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size) +{ + while (size--) { + good_buf[offset] = testcalls % 256; + if (offset % 2) + good_buf[offset] += original_buf[offset]; + offset++; + } +} + + +void +dowrite(unsigned offset, unsigned size) +{ + off_t ret; + unsigned iret; + + offset -= offset % writebdy; + if (size == 0) { + if (!quiet && testcalls > simulatedopcount) + prt("skipping zero size write\n"); + log4(OP_SKIPPED, OP_WRITE, offset, size); + return; + } + + log4(OP_WRITE, offset, size, file_size); + + gendata(original_buf, good_buf, offset, size); + if (file_size < offset + size) { + if (file_size < offset) + memset(good_buf + file_size, '\0', offset - file_size); + file_size = offset + size; + if (lite) { + warn("Lite file size bug in fsx!"); + report_failure(149); + } + } + + if (testcalls <= simulatedopcount) + return; + + if (!quiet && ((progressinterval && + testcalls % progressinterval == 0) || + (debug && + (monitorstart == -1 || + (offset + size > monitorstart && + (monitorend == -1 || offset <= monitorend)))))) + prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls, + offset, offset + size - 1, size); + ret = lseek(fd, (off_t)offset, SEEK_SET); + if (ret == (off_t)-1) { + prterr("dowrite: lseek"); + report_failure(150); + } + iret = write(fd, good_buf + offset, size); + if (iret != size) { + if (iret == (unsigned)(-1)) + prterr("dowrite: write"); + else + prt("short write: 0x%x bytes instead of 0x%x\n", + iret, size); + report_failure(151); + } +} + + + +void +dotruncate(unsigned size) +{ + int oldsize = file_size; + + size -= size % truncbdy; + if (size > biggest) { + biggest = size; + if (!quiet && testcalls > simulatedopcount) + prt("truncating to largest ever: 0x%x\n", size); + } + + log4(OP_TRUNCATE, size, (unsigned)file_size, 0); + + if (size > file_size) + memset(good_buf + file_size, '\0', size - file_size); + file_size = size; + + if (testcalls <= simulatedopcount) + return; + + if ((progressinterval && testcalls % progressinterval == 0) || + (debug && (monitorstart == -1 || monitorend == -1 || + size <= monitorend))) + prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size); + if (ftruncate(fd, (off_t)size) == -1) { + prt("ftruncate1: %x\n", size); + prterr("dotruncate: ftruncate"); + report_failure(160); + } +} + + +void +writefileimage() +{ + ssize_t iret; + + if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { + prterr("writefileimage: lseek"); + report_failure(171); + } + iret = write(fd, good_buf, file_size); + if ((off_t)iret != file_size) { + if (iret == -1) + prterr("writefileimage: write"); + else + prt("short write: 0x%x bytes instead of 0x%llx\n", + iret, (unsigned long long)file_size); + report_failure(172); + } + if (lite ? 0 : ftruncate(fd, file_size) == -1) { + prt("ftruncate2: %llx\n", (unsigned long long)file_size); + prterr("writefileimage: ftruncate"); + report_failure(173); + } +} + + +void +docloseopen(void) +{ + if (testcalls <= simulatedopcount) + return; + + if (debug) + prt("%lu close/open\n", testcalls); + if (close(fd)) { + prterr("docloseopen: close"); + report_failure(180); + } + fd = open(fname, O_RDWR, 0); + if (fd < 0) { + prterr("docloseopen: open"); + report_failure(181); + } +} + + +void +fsx_do_op(void) +{ + unsigned long offset; + unsigned long size = maxoplen; + unsigned long rv = random(); + unsigned long op = rv % (3 + !lite + mapped_writes); + + /* turn off the map read if necessary */ + + if (op == 2 && !mapped_reads) + op = 0; + + if (simulatedopcount > 0 && testcalls == simulatedopcount) + writefileimage(); + + testcalls++; + + if (closeprob) + closeopen = (rv >> 3) < (1U << 28) / closeprob; + + if (debugstart > 0 && testcalls >= debugstart) + debug = 1; + + if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0) + prt("%lu...\n", testcalls); + + /* + * READ: op = 0 + * WRITE: op = 1 + * MAPREAD: op = 2 + * TRUNCATE: op = 3 + * MAPWRITE: op = 3 or 4 + */ + if (lite ? 0 : op == 3 && (style & 1) == 0) /* vanilla truncate? */ + dotruncate(random() % maxfilelen); + else { + if (randomoplen) + size = random() % (maxoplen+1); + if (lite ? 0 : op == 3) + dotruncate(size); + else { + offset = random(); + if (op == 1 || op == (lite ? 3 : 4)) { + offset %= maxfilelen; + if (offset + size > maxfilelen) + size = maxfilelen - offset; + dowrite(offset, size); + } else { + if (file_size) + offset %= file_size; + else + offset = 0; + if ((ssize_t)(offset + size) > file_size) + size = file_size - offset; + doread(offset, size); + } + } + } + if (sizechecks && testcalls > simulatedopcount) + check_size(); + if (closeopen) + docloseopen(); +} + + +void +cleanup(sig) + int sig; +{ + if (sig) + prt("signal %d\n", sig); + prt("testcalls = %lu\n", testcalls); + EXIT(sig); +} + + +void +usage(void) +{ + fprintf(stdout, "usage: %s", + "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\ + -b opnum: beginning operation number (default 1)\n\ + -c P: 1 in P chance of file close+open at each op (default infinity)\n\ + -d: debug output for all operations\n\ + -l flen: the upper bound on file size (default 262144)\n\ + -m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\ + -n: no verifications of file size\n\ + -o oplen: the upper bound on operation size (default 65536)\n\ + -p progressinterval: debug output at specified operation interval\n\ + -q: quieter operation\n\ + -r readbdy: 4096 would make reads page aligned (default 1)\n\ + -s style: 1 gives smaller truncates (default 0)\n\ + -t truncbdy: 4096 would make truncates page aligned (default 1)\n\ + -w writebdy: 4096 would make writes page aligned (default 1)\n\ + -D startingop: debug output starting at specified operation\n\ + -L: fsxLite - no file creations & no file size changes\n\ + -N numops: total # operations to do (default infinity)\n\ + -O: use oplen (see -o flag) for every op (default random)\n\ + -P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\ + -S seed: for random # generator (default 1) 0 gets timestamp\n\ + fname: this filename is REQUIRED (no default)\n"); + EXIT(90); +} + + +int +getnum(char *s, char **e) +{ + int ret = -1; + + *e = (char *) 0; + ret = strtol(s, e, 0); + if (*e) + switch (**e) { + case 'b': + case 'B': + ret *= 512; + *e = *e + 1; + break; + case 'k': + case 'K': + ret *= 1024; + *e = *e + 1; + break; + case 'm': + case 'M': + ret *= 1024*1024; + *e = *e + 1; + break; + case 'w': + case 'W': + ret *= 4; + *e = *e + 1; + break; + } + return (ret); +} + + + +extern int random_seed; +extern int simulate_power_failure; + + +int mounted_by_fsx = 0; + + + +int +fsx_init(const char *mount_pt) +{ + unsigned i; + + goodfile[0] = 0; + logfile[0] = 0; + + page_size = getpagesize(); + page_mask = page_size - 1; + + setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */ + + strcpy(mount_name,mount_pt); + strcpy(fname,mount_name); + strcat(fname,"/fsxdata"); + +#if 0 + signal(SIGHUP, cleanup); + signal(SIGINT, cleanup); + signal(SIGPIPE, cleanup); + signal(SIGALRM, cleanup); + signal(SIGTERM, cleanup); + signal(SIGXCPU, cleanup); + signal(SIGXFSZ, cleanup); + signal(SIGVTALRM, cleanup); + signal(SIGUSR1, cleanup); + signal(SIGUSR2, cleanup); +#endif + +#if 0 + initstate(seed, state, 256); + setstate(state); +#endif + + fd = open(fname, O_RDWR|(lite ? 0 : O_CREAT|O_TRUNC), 0666); + if (fd < 0) { + prterr(fname); + EXIT(91); + } + strncat(goodfile, fname, 256); + strcat (goodfile, ".fsxgood"); + fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666); + if (fsxgoodfd < 0) { + prterr(goodfile); + EXIT(92); + } + strncat(logfile, "fsx", 256); + strcat (logfile, ".fsxlog"); + fsxlogf = fopen(logfile, "w"); + if (fsxlogf == NULL) { + prterr(logfile); + EXIT(93); + } + if (lite) { + off_t ret; + file_size = maxfilelen = lseek(fd, (off_t)0, SEEK_END); + if (file_size == (off_t)-1) { + prterr(fname); + warn("main: lseek eof"); + EXIT(94); + } + ret = lseek(fd, (off_t)0, SEEK_SET); + if (ret == (off_t)-1) { + prterr(fname); + warn("main: lseek 0"); + EXIT(95); + } + } + original_buf = (char *) malloc(maxfilelen); + for (i = 0; i < maxfilelen; i++) + original_buf[i] = random() % 256; + good_buf = (char *) malloc(maxfilelen); + memset(good_buf, '\0', maxfilelen); + temp_buf = (char *) malloc(maxoplen); + memset(temp_buf, '\0', maxoplen); + if (lite) { /* zero entire existing file */ + ssize_t written; + + written = write(fd, good_buf, (size_t)maxfilelen); + if (written != (ssize_t)maxfilelen) { + if (written == -1) { + prterr(fname); + warn("main: error on write"); + } else + warn("main: short write, 0x%x bytes instead of 0x%x\n", + (unsigned)written, maxfilelen); + EXIT(98); + } + } else + check_trunc_hack(); + + printf("fsx_init done\n"); + + return 0; +} + + +int fsx_complete(void) +{ + if (close(fd)) { + prterr("close"); + report_failure(99); + } + + close(fsxgoodfd); + + prt("All operations completed A-OK!\n"); + + EXIT(0); + return 0; +} + +int fsx_main(const char *mount_pt, int numops) +{ + fsx_init(mount_pt); + while (numops == -1 || numops--) + fsx_do_op(); + fsx_complete(); + + return 0; +} + + +#define YPATH "/yaffs_mount_pt" + +#define FSX_TEST_DIR YPATH"/fsx_mount" + +int run_the_test(void) +{ + int ret; + ret = mkdir(FSX_TEST_DIR, 0777); + printf("mkdir returned %d\n", ret); + + if (ret < 0) + perror("mkdir"); + + return fsx_main(FSX_TEST_DIR, 100000); +} -- 2.30.2