Add extra compiler flags
[yaffs2.git] / direct / tests / nor_stress.c
1 #include "nor_stress.h"
2
3
4 #include "yaffsfs.h"
5 #include "yaffs_fsx.h"
6
7 #include <stdio.h>
8
9
10
11 #if 1
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)
14
15 #define FSX() \
16 do { \
17   if((myrand() & 0x1) == 0){\
18      if(interleave_fsx) \
19         yaffs_fsx_do_op(); \
20   } \
21 } while(0)
22
23 #else
24 #define FSX_INIT(mount_point) do { } while(0)
25 #define FSX_COMPLETE() do { } while(0)
26 #define FSX() do { } while(0)
27 #endif
28
29
30 void (*ext_fatal)(void) = NULL;
31
32 static unsigned powerUps;
33 static unsigned cycleStarts;
34 static unsigned cycleEnds;
35
36 static int interleave_fsx;
37
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];
45
46
47 extern int random_seed;
48
49 int myrand(void) {
50   random_seed = random_seed * 1103515245 + 12345;
51   return((unsigned)(random_seed/65536) % 32768);
52 }
53
54 void MakeName(char *fullName,const char *prefix, const char *name)
55 {
56   strcpy(fullName,prefix);
57   strcat(fullName,"/");
58   strcat(fullName,name);
59 }
60
61
62 void MakeFullNames(const char *prefix)
63 {
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");
71 }
72
73 static void FatalError(int lineNo)
74 {
75   printf("Integrity error %d\n",lineNo);
76   if(ext_fatal)
77         ext_fatal();
78         
79   while(1){
80    sleep(1);
81   }
82 }
83 void print_stat(const char *str, struct yaffs_stat *st)
84 {
85         printf("%s inode %d\n",str,st->st_ino);
86 }
87
88 static void UpdateCounter(const char *name, unsigned *val,  int initialise)
89 {
90   int inh=-1;
91   int outh=-1;
92   unsigned x[2];
93   int nread = 0;
94   int nwritten = 0;
95   
96   x[0] = x[1] = 0;
97   
98   if(initialise){
99     x[0] = 0; 
100     x[1] = 1;
101   } else {
102     inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
103     if(inh >= 0){
104       nread = yaffs_read(inh,x,sizeof(x));
105       yaffs_close(inh);
106     }
107
108     if(nread != sizeof(x) ||
109        x[0] + 1 != x[1]){
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__);
113               
114     }
115     x[0]++;
116     x[1]++;
117   }
118   
119   FSX();
120   outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
121   if(outh >= 0){
122    struct yaffs_stat tmpstat, oldstat, tmpfstat;
123    FSX(); 
124     nwritten = yaffs_write(outh,x,sizeof(x));
125     FSX();
126     yaffs_fstat(outh,&tmpfstat);
127     yaffs_close(outh);
128     FSX();
129
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);
137     FSX();
138   }
139   
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__);
144   }
145   
146   *val = x[0];
147   
148   printf("##\n"
149          "## Set counter %s to %u\n"
150          "##\n", name,x[0]);
151 }
152
153
154 static void dump_directory_tree_worker(const char *dname,int recursive)
155 {
156         yaffs_DIR *d;
157         yaffs_dirent *de;
158         struct yaffs_stat s;
159         char str[1000];
160         int error_line = 0;
161                         
162         d = yaffs_opendir(dname);
163         
164         if(!d)
165         {
166                 printf("opendir failed\n");
167         }
168         else
169         {
170                 while((de = yaffs_readdir(d)) != NULL)
171                 {
172                         strcpy(str,dname);
173                         strcat(str,"/");
174                         strcat(str,de->d_name);
175                         
176                         yaffs_lstat(str,&s);
177                         
178                         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);\
179                         if(de->d_ino != s.st_ino){
180                                 printf(" \n\n!!!! HEY inode mismatch\n\n");
181                                 error_line = __LINE__;
182                         }
183
184                         switch(s.st_mode & S_IFMT)
185                         {
186                                 case S_IFREG: printf("data file"); break;
187                                 case S_IFDIR: printf("directory"); break;
188                                 case S_IFLNK: printf("symlink -->");
189                                                           if(yaffs_readlink(str,str,100) < 0)
190                                                                 printf("no alias");
191                                                           else
192                                                                 printf("\"%s\"",str);    
193                                                           break;
194                                 default: printf("unknown"); break;
195                         }
196                         
197                         printf("\n");
198
199                         if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
200                                 dump_directory_tree_worker(str,1);
201                                 
202                         if(s.st_ino > 10000)
203                           error_line = __LINE__;
204                                                         
205                 }
206                 
207                 if(error_line)
208                         FatalError(error_line);
209                 
210                 yaffs_closedir(d);
211         }
212
213 }
214
215 static void dump_directory_tree(const char *dname)
216 {
217         dump_directory_tree_worker(dname,1);
218         printf("\n");
219         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
220 }
221
222
223
224
225 #define XX_SIZE 500
226
227 static unsigned xx[XX_SIZE];
228
229 static int yWriteFile(const char *fname, unsigned sz32)
230 {
231         int h;
232         int r;
233         int i;
234         struct yaffs_stat st;
235         unsigned checksum = 0;
236         
237
238         FSX();
239         h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
240         yaffs_fstat(h,&st);
241         printf("Writing file %s inode %d\n",fname, st.st_ino);
242         
243         FSX();
244
245         if(h < 0){
246                 printf("could not open file %s\n",fname);
247                 return h;
248         }
249
250         xx[0] = sz32;
251         checksum ^= xx[0];
252
253         if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
254                 goto WRITE_ERROR;
255         }
256         FSX();
257         while(sz32> 0){
258                 for(i = 0; i < XX_SIZE; i++){
259                   xx[i] = sz32 + i;
260                   checksum ^= xx[i];
261                 }
262                 
263                 FSX();
264                 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
265                         goto WRITE_ERROR;
266                 }
267                 sz32--;
268         }
269
270         xx[0] = checksum;
271         FSX();
272         if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
273                 goto WRITE_ERROR;
274         }
275         
276         FSX();
277         yaffs_close(h);
278         return 0;
279
280 WRITE_ERROR:
281         printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
282         yaffs_close(h);
283         return -1;
284         
285 }
286
287 static int yVerifyFile(const char *fName)
288 {
289         unsigned checksum = 0;
290         unsigned totalSize;
291         unsigned sz32;
292         unsigned recordedSize = 0;
293         int r;
294         int h;
295         int i;
296         int retval = 0;
297
298
299         printf("Verifying file %s\n",fName);
300                 
301         h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
302
303         if(h < 0){
304                 printf("could not open file %s\n",fName);
305                 return -1;
306         }
307
308         totalSize = yaffs_lseek(h,0,SEEK_END);
309         yaffs_lseek(h,0,SEEK_SET);
310
311         r = yaffs_read(h,&sz32,sizeof(sz32));
312
313         if(r != sizeof(sz32)){
314                 printf("reading size failed ... returned %d\n",r);
315                 yaffs_close(h);
316                 return -1;
317         }
318         
319         recordedSize = sz32 * sizeof(xx) + 8;
320
321         printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
322         if(totalSize != recordedSize){
323                 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
324                 yaffs_close(h);
325                 return -1;
326         }
327
328         checksum ^= sz32;
329
330
331         while(sz32 > 0){
332                 r = yaffs_read(h,xx,sizeof(xx));
333                 if(r != sizeof(xx)){
334                         printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
335                         yaffs_close(h);
336                         return -1;
337                 }
338                 for(i = 0; i < XX_SIZE; i++)
339                   checksum ^= xx[i];
340                 sz32--;
341         }
342         r = yaffs_read(h,xx,sizeof(xx[0]));
343         if(r != sizeof(xx[0])){
344                 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
345                 yaffs_close(h);
346                 return -1;
347         }
348         
349         checksum ^= xx[0];
350
351         if(checksum != 0){
352                 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
353                 retval = -1;
354         } else
355                 printf("verified ok\n");
356         yaffs_close(h);
357
358         return retval;
359 }
360
361
362 static void DoUpdateMainFile(void)
363 {
364         int result;
365         int sz32;
366         sz32 = (myrand() % 1000)   + 20;
367         
368         result = yWriteFile(fullTempMainName,sz32);
369         FSX();
370         if(result)
371             FatalError(__LINE__);
372         yaffs_rename(fullTempMainName,fullMainName);
373         FSX();
374 }
375
376 static void DoVerifyMainFile(void)
377 {
378         int result;
379         result = yVerifyFile(fullMainName);
380         if(result)
381             FatalError(__LINE__);
382
383 }
384
385
386 void NorStressTestInitialise(const char *prefix)
387 {
388   MakeFullNames(prefix);
389   
390   UpdateCounter(fullPowerUpName,&powerUps,1);
391   UpdateCounter(fullStartName,&cycleStarts,1);
392   UpdateCounter(fullEndName,&cycleEnds,1);
393   UpdateCounter(fullPowerUpName,&powerUps,1);
394   DoUpdateMainFile();
395   DoVerifyMainFile();
396 }
397
398
399 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx)
400 {
401   interleave_fsx = do_fsx;
402   MakeFullNames(prefix);
403   FSX_INIT(prefix);
404     
405   dump_directory_tree(fullPathName);
406   
407   UpdateCounter(fullPowerUpName,&powerUps,0);
408   dump_directory_tree(fullPathName);
409   
410   while(n_cycles < 0 || n_cycles > 0){
411     if(n_cycles > 0)
412       n_cycles--;
413     UpdateCounter(fullStartName, &cycleStarts,0);
414     dump_directory_tree(fullPathName);
415     DoVerifyMainFile();
416     DoUpdateMainFile();
417     dump_directory_tree(fullPathName);
418   
419     UpdateCounter(fullEndName,&cycleEnds,0);
420     dump_directory_tree(fullPathName);
421   }
422   FSX_COMPLETE();
423 }