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 void (*ext_fatal)(void) = NULL;
32 static unsigned powerUps;
33 static unsigned cycleStarts;
34 static unsigned cycleEnds;
36 static int interleave_fsx;
38 char fullPathName[100];
39 char fullPowerUpName[100];
40 char fullStartName[100];
41 char fullEndName[100];
42 char fullMainName[100];
43 char fullTempMainName[100];
44 char fullTempCounterName[100];
47 extern int random_seed;
50 random_seed = random_seed * 1103515245 + 12345;
51 return((unsigned)(random_seed/65536) % 32768);
54 void MakeName(char *fullName,const char *prefix, const char *name)
56 strcpy(fullName,prefix);
58 strcat(fullName,name);
62 void MakeFullNames(const char *prefix)
64 MakeName(fullPathName,prefix,"");
65 MakeName(fullPowerUpName,prefix,"powerUps");
66 MakeName(fullStartName,prefix,"starts");
67 MakeName(fullEndName,prefix,"ends");
68 MakeName(fullMainName,prefix,"main");
69 MakeName(fullTempCounterName,prefix,"tmp-counter");
70 MakeName(fullTempMainName,prefix,"tmp-main");
73 static void FatalError(int lineNo)
75 printf("Integrity error %d\n",lineNo);
83 void print_stat(const char *str, struct yaffs_stat *st)
85 printf("%s inode %d\n",str,st->st_ino);
88 static void UpdateCounter(const char *name, unsigned *val, int initialise)
102 inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
104 nread = yaffs_read(inh,x,sizeof(x));
108 if(nread != sizeof(x) ||
110 printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
111 name, inh, x[0], x[1],yaffsfs_GetLastError());
112 FatalError(__LINE__);
120 outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
122 struct yaffs_stat tmpstat, oldstat, tmpfstat;
124 nwritten = yaffs_write(outh,x,sizeof(x));
126 yaffs_fstat(outh,&tmpfstat);
130 printf("About to rename %s to %s\n",fullTempCounterName,name);
131 yaffs_stat(fullTempCounterName,&tmpstat);
132 yaffs_stat(name,&oldstat);
133 print_stat("old stat",&oldstat);
134 print_stat("new stat",&tmpstat);
135 print_stat("new fstat",&tmpfstat);
136 yaffs_rename(fullTempCounterName,name);
140 if(nwritten != sizeof(x)){
141 printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
142 name, inh, x[0], x[1]);
143 FatalError(__LINE__);
149 "## Set counter %s to %u\n"
154 static void dump_directory_tree_worker(const char *dname,int recursive)
161 d = yaffs_opendir(dname);
165 printf("opendir failed\n");
169 while((de = yaffs_readdir(d)) != NULL)
173 strcat(str,de->d_name);
177 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);\
178 if(de->d_ino != s.st_ino){
179 printf(" \n\n!!!! HEY inode mismatch\n\n");
180 FatalError(__LINE__);
183 switch(s.st_mode & S_IFMT)
185 case S_IFREG: printf("data file"); break;
186 case S_IFDIR: printf("directory"); break;
187 case S_IFLNK: printf("symlink -->");
188 if(yaffs_readlink(str,str,100) < 0)
191 printf("\"%s\"",str);
193 default: printf("unknown"); break;
198 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
199 dump_directory_tree_worker(str,1);
202 FatalError(__LINE__);
211 static void dump_directory_tree(const char *dname)
213 dump_directory_tree_worker(dname,1);
215 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
223 static unsigned xx[XX_SIZE];
225 static int yWriteFile(const char *fname, unsigned sz32)
230 struct yaffs_stat st;
231 unsigned checksum = 0;
235 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
237 printf("Writing file %s inode %d\n",fname, st.st_ino);
242 printf("could not open file %s\n",fname);
249 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
254 for(i = 0; i < XX_SIZE; i++){
260 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
268 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
277 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
283 static int yVerifyFile(const char *fName)
285 unsigned checksum = 0;
288 unsigned recordedSize = 0;
295 printf("Verifying file %s\n",fName);
297 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
300 printf("could not open file %s\n",fName);
304 totalSize = yaffs_lseek(h,0,SEEK_END);
305 yaffs_lseek(h,0,SEEK_SET);
307 r = yaffs_read(h,&sz32,sizeof(sz32));
309 if(r != sizeof(sz32)){
310 printf("reading size failed ... returned %d\n",r);
315 recordedSize = sz32 * sizeof(xx) + 8;
317 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
318 if(totalSize != recordedSize){
319 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
328 r = yaffs_read(h,xx,sizeof(xx));
330 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
334 for(i = 0; i < XX_SIZE; i++)
338 r = yaffs_read(h,xx,sizeof(xx[0]));
339 if(r != sizeof(xx[0])){
340 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
348 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
351 printf("verified ok\n");
358 static void DoUpdateMainFile(void)
362 sz32 = (myrand() % 1000) + 20;
364 result = yWriteFile(fullTempMainName,sz32);
367 FatalError(__LINE__);
368 yaffs_rename(fullTempMainName,fullMainName);
372 static void DoVerifyMainFile(void)
375 result = yVerifyFile(fullMainName);
377 FatalError(__LINE__);
382 void NorStressTestInitialise(const char *prefix)
384 MakeFullNames(prefix);
386 UpdateCounter(fullPowerUpName,&powerUps,1);
387 UpdateCounter(fullStartName,&cycleStarts,1);
388 UpdateCounter(fullEndName,&cycleEnds,1);
389 UpdateCounter(fullPowerUpName,&powerUps,1);
395 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx)
397 interleave_fsx = do_fsx;
398 MakeFullNames(prefix);
401 dump_directory_tree(fullPathName);
403 UpdateCounter(fullPowerUpName,&powerUps,0);
404 dump_directory_tree(fullPathName);
406 while(n_cycles < 0 || n_cycles > 0){
409 UpdateCounter(fullStartName, &cycleStarts,0);
410 dump_directory_tree(fullPathName);
413 dump_directory_tree(fullPathName);
415 UpdateCounter(fullEndName,&cycleEnds,0);
416 dump_directory_tree(fullPathName);