1 #include "nor_stress.h"
12 #define FSX_INIT(mount_pt) do{ if(interleave_fsx) yaffs_fsx_init(mount_pt); } while(0)
13 #define FSX_COMPLETE() do { if(interleave_fsx) yaffs_fsx_complete(); } while (0)
17 if((myrand() & 0x1) == 0){\
24 #define FSX_INIT(mount_point) do { } while(0)
25 #define FSX_COMPLETE() do { } while(0)
26 #define FSX() do { } while(0)
30 static unsigned powerUps;
31 static unsigned cycleStarts;
32 static unsigned cycleEnds;
34 static int interleave_fsx;
36 char fullPathName[100];
37 char fullPowerUpName[100];
38 char fullStartName[100];
39 char fullEndName[100];
40 char fullMainName[100];
41 char fullTempMainName[100];
42 char fullTempCounterName[100];
45 extern int random_seed;
48 random_seed = random_seed * 1103515245 + 12345;
49 return((unsigned)(random_seed/65536) % 32768);
52 void MakeName(char *fullName,const char *prefix, const char *name)
54 strcpy(fullName,prefix);
56 strcat(fullName,name);
60 void MakeFullNames(const char *prefix)
62 MakeName(fullPathName,prefix,"");
63 MakeName(fullPowerUpName,prefix,"powerUps");
64 MakeName(fullStartName,prefix,"starts");
65 MakeName(fullEndName,prefix,"ends");
66 MakeName(fullMainName,prefix,"main");
67 MakeName(fullTempCounterName,prefix,"tmp-counter");
68 MakeName(fullTempMainName,prefix,"tmp-main");
71 static void FatalError(int lineNo)
73 printf("Integrity error %d\n",lineNo);
78 void print_stat(const char *str, struct yaffs_stat *st)
80 printf("%s inode %d\n",str,st->st_ino);
83 static void UpdateCounter(const char *name, unsigned *val, int initialise)
97 inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
99 nread = yaffs_read(inh,x,sizeof(x));
103 if(nread != sizeof(x) ||
105 printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
106 name, inh, x[0], x[1],yaffsfs_GetLastError());
107 FatalError(__LINE__);
115 outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
117 struct yaffs_stat tmpstat, oldstat, tmpfstat;
119 nwritten = yaffs_write(outh,x,sizeof(x));
121 yaffs_fstat(outh,&tmpfstat);
125 printf("About to rename %s to %s\n",fullTempCounterName,name);
126 yaffs_stat(fullTempCounterName,&tmpstat);
127 yaffs_stat(name,&oldstat);
128 print_stat("old stat",&oldstat);
129 print_stat("new stat",&tmpstat);
130 print_stat("new fstat",&tmpfstat);
131 yaffs_rename(fullTempCounterName,name);
135 if(nwritten != sizeof(x)){
136 printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
137 name, inh, x[0], x[1]);
138 FatalError(__LINE__);
144 "## Set counter %s to %u\n"
149 static void dump_directory_tree_worker(const char *dname,int recursive)
156 d = yaffs_opendir(dname);
160 printf("opendir failed\n");
164 while((de = yaffs_readdir(d)) != NULL)
168 strcat(str,de->d_name);
172 printf("%s inode %ld %d obj %x length %d mode %X ",str, de->d_ino, s.st_ino,de->d_dont_use,(int)s.st_size,s.st_mode);\
173 if(de->d_ino != s.st_ino){
174 printf(" \n\n!!!! HEY inode mismatch\n\n");
175 FatalError(__LINE__);
178 switch(s.st_mode & S_IFMT)
180 case S_IFREG: printf("data file"); break;
181 case S_IFDIR: printf("directory"); break;
182 case S_IFLNK: printf("symlink -->");
183 if(yaffs_readlink(str,str,100) < 0)
186 printf("\"%s\"",str);
188 default: printf("unknown"); break;
193 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
194 dump_directory_tree_worker(str,1);
197 FatalError(__LINE__);
206 static void dump_directory_tree(const char *dname)
208 dump_directory_tree_worker(dname,1);
210 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
218 static unsigned xx[XX_SIZE];
220 static int yWriteFile(const char *fname, unsigned sz32)
225 struct yaffs_stat st;
226 unsigned checksum = 0;
230 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
232 printf("Writing file %s inode %d\n",fname, st.st_ino);
237 printf("could not open file %s\n",fname);
244 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
249 for(i = 0; i < XX_SIZE; i++){
255 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
263 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
272 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
278 static int yVerifyFile(const char *fName)
280 unsigned checksum = 0;
283 unsigned recordedSize = 0;
290 printf("Verifying file %s\n",fName);
292 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
295 printf("could not open file %s\n",fName);
299 totalSize = yaffs_lseek(h,0,SEEK_END);
300 yaffs_lseek(h,0,SEEK_SET);
302 r = yaffs_read(h,&sz32,sizeof(sz32));
304 if(r != sizeof(sz32)){
305 printf("reading size failed ... returned %d\n",r);
310 recordedSize = sz32 * sizeof(xx) + 8;
312 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
313 if(totalSize != recordedSize){
314 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
323 r = yaffs_read(h,xx,sizeof(xx));
325 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
329 for(i = 0; i < XX_SIZE; i++)
333 r = yaffs_read(h,xx,sizeof(xx[0]));
334 if(r != sizeof(xx[0])){
335 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
343 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
346 printf("verified ok\n");
353 static void DoUpdateMainFile(void)
357 sz32 = (myrand() % 1000) + 20;
359 result = yWriteFile(fullTempMainName,sz32);
362 FatalError(__LINE__);
363 yaffs_rename(fullTempMainName,fullMainName);
367 static void DoVerifyMainFile(void)
370 result = yVerifyFile(fullMainName);
372 FatalError(__LINE__);
377 void NorStressTestInitialise(const char *prefix)
379 MakeFullNames(prefix);
381 UpdateCounter(fullPowerUpName,&powerUps,1);
382 UpdateCounter(fullStartName,&cycleStarts,1);
383 UpdateCounter(fullEndName,&cycleEnds,1);
384 UpdateCounter(fullPowerUpName,&powerUps,1);
390 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx)
392 interleave_fsx = do_fsx;
393 MakeFullNames(prefix);
396 dump_directory_tree(fullPathName);
398 UpdateCounter(fullPowerUpName,&powerUps,0);
399 dump_directory_tree(fullPathName);
401 while(n_cycles < 0 || n_cycles > 0){
404 UpdateCounter(fullStartName, &cycleStarts,0);
405 dump_directory_tree(fullPathName);
408 dump_directory_tree(fullPathName);
410 UpdateCounter(fullEndName,&cycleEnds,0);
411 dump_directory_tree(fullPathName);