yaffs Get fuzz tester to run further
[yaffs2.git] / direct / tests / nor_stress.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  */
14
15 #include "nor_stress.h"
16
17
18 #include "yaffsfs.h"
19 #include "yaffs_fsx.h"
20
21 #include <stdio.h>
22
23
24 extern int fuzz_test;
25
26 #if 1
27 #define FSX_INIT(mount_pt) do{ if(interleave_fsx) yaffs_fsx_init(mount_pt); } while(0)
28 #define FSX_COMPLETE() do { if(interleave_fsx) yaffs_fsx_complete(); } while (0)
29
30 #define FSX() \
31 do { \
32   if((myrand() & 0x1) == 0){\
33      if(interleave_fsx) \
34         yaffs_fsx_do_op(); \
35   } \
36 } while(0)
37
38 #else
39 #define FSX_INIT(mount_point) do { } while(0)
40 #define FSX_COMPLETE() do { } while(0)
41 #define FSX() do { } while(0)
42 #endif
43
44
45 void (*ext_fatal)(void) = NULL;
46
47 static unsigned powerUps;
48 static unsigned cycleStarts;
49 static unsigned cycleEnds;
50
51 static int interleave_fsx;
52
53 static int no_verification;
54  
55 char fullPathName[100];
56 char fullPowerUpName[100];
57 char fullStartName[100];
58 char fullEndName[100];
59 char fullMainName[100];
60 char fullTempMainName[100];
61 char fullTempCounterName[100];
62
63
64 extern int random_seed;
65
66 int myrand(void) {
67   random_seed = random_seed * 1103515245 + 12345;
68   return((unsigned)(random_seed/65536) % 32768);
69 }
70
71 void MakeName(char *fullName,const char *prefix, const char *name)
72 {
73   strcpy(fullName,prefix);
74   strcat(fullName,"/");
75   strcat(fullName,name);
76 }
77
78
79 void MakeFullNames(const char *prefix)
80 {
81   MakeName(fullPathName,prefix,"");
82   MakeName(fullPowerUpName,prefix,"powerUps");
83   MakeName(fullStartName,prefix,"starts");
84   MakeName(fullEndName,prefix,"ends");
85   MakeName(fullMainName,prefix,"main");
86   MakeName(fullTempCounterName,prefix,"tmp-counter");
87   MakeName(fullTempMainName,prefix,"tmp-main");
88 }
89
90 static void FatalError(int lineNo)
91 {
92   printf("Integrity error %d\n",lineNo);
93
94   if(fuzz_test)
95         return;
96
97   if(ext_fatal)
98         ext_fatal();
99         
100   while(1){
101    sleep(1);
102   }
103 }
104 void print_stat(const char *str, struct yaffs_stat *st)
105 {
106         printf("%s inode %d\n",str,st->st_ino);
107 }
108
109 static void UpdateCounter(const char *name, unsigned *val,  int initialise)
110 {
111   int inh=-1;
112   int outh=-1;
113   unsigned x[2];
114   int nread = 0;
115   int nwritten = 0;
116   
117   x[0] = x[1] = 0;
118   
119   if(initialise){
120     x[0] = 0; 
121     x[1] = 1;
122   } else {
123     inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
124     if(inh >= 0){
125       nread = yaffs_read(inh,x,sizeof(x));
126       yaffs_close(inh);
127     }
128
129     if(!no_verification &&
130       (nread != sizeof(x) ||
131        x[0] + 1 != x[1])){
132       printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
133               name, inh, x[0], x[1],yaffsfs_GetLastError());
134       FatalError(__LINE__);
135               
136     }
137     x[0]++;
138     x[1]++;
139   }
140   
141   FSX();
142   outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
143   
144   if(outh >= 0){
145    struct yaffs_stat tmpstat, oldstat, tmpfstat;
146    FSX(); 
147     yaffs_fstat(outh,&tmpfstat);
148     printf("\n\n\n*** Writing file %s inode %d\n",fullTempCounterName,tmpfstat.st_ino);
149     nwritten = yaffs_write(outh,x,sizeof(x));
150     FSX();
151     yaffs_close(outh);
152     FSX();
153
154     printf("About to rename %s to %s\n",fullTempCounterName,name);
155     yaffs_stat(fullTempCounterName,&tmpstat);
156     yaffs_stat(name,&oldstat);
157     print_stat("old stat",&oldstat);
158     print_stat("new stat",&tmpstat);
159     print_stat("new fstat",&tmpfstat);
160     yaffs_rename(fullTempCounterName,name);
161     FSX();
162   }
163   
164   if(nwritten != sizeof(x)){
165       printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
166               name, inh, x[0], x[1]);
167       FatalError(__LINE__);
168   }
169   
170   *val = x[0];
171   
172   printf("##\n"
173          "## Set counter %s to %u\n"
174          "##\n", name,x[0]);
175 }
176
177
178 static void dump_directory_tree_worker(const char *dname,int recursive)
179 {
180         yaffs_DIR *d;
181         yaffs_dirent *de;
182         struct yaffs_stat s;
183         char str[1000];
184         int error_line = 0;
185                         
186         d = yaffs_opendir(dname);
187         
188         if(!d)
189         {
190                 printf("opendir failed\n");
191         }
192         else
193         {
194                 while((de = yaffs_readdir(d)) != NULL)
195                 {
196                         strcpy(str,dname);
197                         strcat(str,"/");
198                         strcat(str,de->d_name);
199                         
200                         yaffs_lstat(str,&s);
201                         
202                         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);\
203                         if(de->d_ino != s.st_ino){
204                                 printf(" \n\n!!!! HEY inode mismatch\n\n");
205                                 error_line = __LINE__;
206                         }
207
208                         switch(s.st_mode & S_IFMT)
209                         {
210                                 case S_IFREG: printf("data file"); break;
211                                 case S_IFDIR: printf("directory"); break;
212                                 case S_IFLNK: printf("symlink -->");
213                                                           if(yaffs_readlink(str,str,100) < 0)
214                                                                 printf("no alias");
215                                                           else
216                                                                 printf("\"%s\"",str);    
217                                                           break;
218                                 default: printf("unknown"); break;
219                         }
220                         
221                         printf("\n");
222
223                         if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
224                                 dump_directory_tree_worker(str,1);
225                                 
226                         if(s.st_ino > 10000)
227                           error_line = __LINE__;
228                                                         
229                 }
230                 
231                 if(error_line && !no_verification)
232                         FatalError(error_line);
233                 
234                 yaffs_closedir(d);
235         }
236
237 }
238
239 static void dump_directory_tree(const char *dname)
240 {
241         dump_directory_tree_worker(dname,1);
242         printf("\n");
243         printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
244 }
245
246
247
248
249 #define XX_SIZE 500
250
251 static unsigned xx[XX_SIZE];
252
253 static int yWriteFile(const char *fname, unsigned sz32)
254 {
255         int h;
256         int r;
257         int i;
258         struct yaffs_stat st;
259         unsigned checksum = 0;
260         
261
262         FSX();
263         h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
264         yaffs_fstat(h,&st);
265         printf("\n\n\n**** Open writing file %s inode %d\n",fname, st.st_ino);
266         
267         FSX();
268
269         if(h < 0){
270                 printf("could not open file %s\n",fname);
271                 return h;
272         }
273
274         xx[0] = sz32;
275         checksum ^= xx[0];
276
277         if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
278                 goto WRITE_ERROR;
279         }
280         FSX();
281         while(sz32> 0){
282                 for(i = 0; i < XX_SIZE; i++){
283                   xx[i] = sz32 + i;
284                   checksum ^= xx[i];
285                 }
286                 
287                 FSX();
288                 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
289                         goto WRITE_ERROR;
290                 }
291                 sz32--;
292         }
293
294         xx[0] = checksum;
295         FSX();
296         if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
297                 goto WRITE_ERROR;
298         }
299         
300         FSX();
301         yaffs_close(h);
302         printf("File closed\n");
303         return 0;
304
305 WRITE_ERROR:
306         printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
307         yaffs_close(h);
308         return -1;
309         
310 }
311
312 static int yVerifyFile(const char *fName)
313 {
314         unsigned checksum = 0;
315         unsigned totalSize;
316         unsigned sz32;
317         unsigned recordedSize = 0;
318         int r;
319         int h;
320         int i;
321         int retval = 0;
322
323         if(no_verification)
324                 return 0;
325
326         printf("Verifying file %s\n",fName);
327                 
328         h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
329
330         if(h < 0){
331                 printf("could not open file %s\n",fName);
332                 return -1;
333         }
334
335         totalSize = yaffs_lseek(h,0,SEEK_END);
336         yaffs_lseek(h,0,SEEK_SET);
337
338         r = yaffs_read(h,&sz32,sizeof(sz32));
339
340         if(r != sizeof(sz32)){
341                 printf("reading size failed ... returned %d\n",r);
342                 yaffs_close(h);
343                 return -1;
344         }
345         
346         recordedSize = sz32 * sizeof(xx) + 8;
347
348         printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
349         if(totalSize != recordedSize){
350                 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
351                 yaffs_close(h);
352                 return -1;
353         }
354
355         checksum ^= sz32;
356
357
358         while(sz32 > 0){
359                 r = yaffs_read(h,xx,sizeof(xx));
360                 if(r != sizeof(xx)){
361                         printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
362                         yaffs_close(h);
363                         return -1;
364                 }
365                 for(i = 0; i < XX_SIZE; i++)
366                   checksum ^= xx[i];
367                 sz32--;
368         }
369         r = yaffs_read(h,xx,sizeof(xx[0]));
370         if(r != sizeof(xx[0])){
371                 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
372                 yaffs_close(h);
373                 return -1;
374         }
375         
376         checksum ^= xx[0];
377
378         if(checksum != 0){
379                 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
380                 retval = -1;
381         } else
382                 printf("verified ok\n");
383         yaffs_close(h);
384
385         return retval;
386 }
387
388
389 static void DoUpdateMainFile(void)
390 {
391         int result;
392         int sz32;
393         sz32 = (myrand() % 1000)   + 20;
394         
395         result = yWriteFile(fullTempMainName,sz32);
396         FSX();
397         if(!no_verification && result)
398             FatalError(__LINE__);
399         printf("Raname file %s to %s\n",fullTempMainName,fullMainName);
400         yaffs_rename(fullTempMainName,fullMainName);
401         FSX();
402 }
403
404 static void DoVerifyMainFile(void)
405 {
406         int result;
407         if(no_verification)
408                 return;
409         result = yVerifyFile(fullMainName);
410         if(result)
411             FatalError(__LINE__);
412
413 }
414
415
416 void NorStressTestInitialise(const char *prefix)
417 {
418   MakeFullNames(prefix);
419   
420   UpdateCounter(fullPowerUpName,&powerUps,1);
421   UpdateCounter(fullStartName,&cycleStarts,1);
422   UpdateCounter(fullEndName,&cycleEnds,1);
423   UpdateCounter(fullPowerUpName,&powerUps,1);
424   DoUpdateMainFile();
425   DoVerifyMainFile();
426 }
427
428
429 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx, int skip_verification)
430 {
431
432   interleave_fsx = do_fsx;
433   no_verification = skip_verification;
434  
435   MakeFullNames(prefix);
436   dump_directory_tree(fullPathName);
437   FSX_INIT(prefix);
438     
439   dump_directory_tree(fullPathName);
440   
441   UpdateCounter(fullPowerUpName,&powerUps,0);
442   dump_directory_tree(fullPathName);
443   
444   while(n_cycles < 0 || n_cycles > 0){
445     if(n_cycles > 0)
446       n_cycles--;
447     UpdateCounter(fullStartName, &cycleStarts,0);
448     dump_directory_tree(fullPathName);
449     DoVerifyMainFile();
450     DoUpdateMainFile();
451     dump_directory_tree(fullPathName);
452   
453     UpdateCounter(fullEndName,&cycleEnds,0);
454     dump_directory_tree(fullPathName);
455   }
456   FSX_COMPLETE();
457 }