2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 #include "nor_stress.h"
19 #include "yaffs_fsx.h"
26 #define FSX_INIT(mount_pt) do{ if(interleave_fsx) yaffs_fsx_init(mount_pt); } while(0)
27 #define FSX_COMPLETE() do { if(interleave_fsx) yaffs_fsx_complete(); } while (0)
31 if((myrand() & 0x1) == 0){\
38 #define FSX_INIT(mount_point) do { } while(0)
39 #define FSX_COMPLETE() do { } while(0)
40 #define FSX() do { } while(0)
44 void (*ext_fatal)(void) = NULL;
46 static unsigned powerUps;
47 static unsigned cycleStarts;
48 static unsigned cycleEnds;
50 static int interleave_fsx;
52 char fullPathName[100];
53 char fullPowerUpName[100];
54 char fullStartName[100];
55 char fullEndName[100];
56 char fullMainName[100];
57 char fullTempMainName[100];
58 char fullTempCounterName[100];
61 extern int random_seed;
64 random_seed = random_seed * 1103515245 + 12345;
65 return((unsigned)(random_seed/65536) % 32768);
68 void MakeName(char *fullName,const char *prefix, const char *name)
70 strcpy(fullName,prefix);
72 strcat(fullName,name);
76 void MakeFullNames(const char *prefix)
78 MakeName(fullPathName,prefix,"");
79 MakeName(fullPowerUpName,prefix,"powerUps");
80 MakeName(fullStartName,prefix,"starts");
81 MakeName(fullEndName,prefix,"ends");
82 MakeName(fullMainName,prefix,"main");
83 MakeName(fullTempCounterName,prefix,"tmp-counter");
84 MakeName(fullTempMainName,prefix,"tmp-main");
87 static void FatalError(int lineNo)
89 printf("Integrity error %d\n",lineNo);
97 void print_stat(const char *str, struct yaffs_stat *st)
99 printf("%s inode %d\n",str,st->st_ino);
102 static void UpdateCounter(const char *name, unsigned *val, int initialise)
116 inh = yaffs_open(name,O_RDONLY, S_IREAD | S_IWRITE);
118 nread = yaffs_read(inh,x,sizeof(x));
122 if(nread != sizeof(x) ||
124 printf("Error reading counter %s handle %d, x[0] %u x[1] %u last error %d\n",
125 name, inh, x[0], x[1],yaffsfs_GetLastError());
126 FatalError(__LINE__);
134 outh = yaffs_open(fullTempCounterName, O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
137 struct yaffs_stat tmpstat, oldstat, tmpfstat;
139 yaffs_fstat(outh,&tmpfstat);
140 printf("\n\n\n*** Writing file %s inode %d\n",fullTempCounterName,tmpfstat.st_ino);
141 nwritten = yaffs_write(outh,x,sizeof(x));
146 printf("About to rename %s to %s\n",fullTempCounterName,name);
147 yaffs_stat(fullTempCounterName,&tmpstat);
148 yaffs_stat(name,&oldstat);
149 print_stat("old stat",&oldstat);
150 print_stat("new stat",&tmpstat);
151 print_stat("new fstat",&tmpfstat);
152 yaffs_rename(fullTempCounterName,name);
156 if(nwritten != sizeof(x)){
157 printf("Error writing counter %s handle %d, x[0] %u x[1] %u\n",
158 name, inh, x[0], x[1]);
159 FatalError(__LINE__);
165 "## Set counter %s to %u\n"
170 static void dump_directory_tree_worker(const char *dname,int recursive)
178 d = yaffs_opendir(dname);
182 printf("opendir failed\n");
186 while((de = yaffs_readdir(d)) != NULL)
190 strcat(str,de->d_name);
194 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);\
195 if(de->d_ino != s.st_ino){
196 printf(" \n\n!!!! HEY inode mismatch\n\n");
197 error_line = __LINE__;
200 switch(s.st_mode & S_IFMT)
202 case S_IFREG: printf("data file"); break;
203 case S_IFDIR: printf("directory"); break;
204 case S_IFLNK: printf("symlink -->");
205 if(yaffs_readlink(str,str,100) < 0)
208 printf("\"%s\"",str);
210 default: printf("unknown"); break;
215 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
216 dump_directory_tree_worker(str,1);
219 error_line = __LINE__;
224 FatalError(error_line);
231 static void dump_directory_tree(const char *dname)
233 dump_directory_tree_worker(dname,1);
235 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
243 static unsigned xx[XX_SIZE];
245 static int yWriteFile(const char *fname, unsigned sz32)
250 struct yaffs_stat st;
251 unsigned checksum = 0;
255 h = yaffs_open(fname,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
257 printf("\n\n\n**** Open writing file %s inode %d\n",fname, st.st_ino);
262 printf("could not open file %s\n",fname);
269 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
274 for(i = 0; i < XX_SIZE; i++){
280 if((r = yaffs_write(h,xx,sizeof(xx))) != sizeof(xx)){
288 if((r = yaffs_write(h,xx,sizeof(unsigned))) != sizeof(unsigned)){
294 printf("File closed\n");
298 printf("ywrite error at position %d\n",(int)yaffs_lseek(h,0,SEEK_END));
304 static int yVerifyFile(const char *fName)
306 unsigned checksum = 0;
309 unsigned recordedSize = 0;
316 printf("Verifying file %s\n",fName);
318 h = yaffs_open(fName, O_RDONLY,S_IREAD | S_IWRITE);
321 printf("could not open file %s\n",fName);
325 totalSize = yaffs_lseek(h,0,SEEK_END);
326 yaffs_lseek(h,0,SEEK_SET);
328 r = yaffs_read(h,&sz32,sizeof(sz32));
330 if(r != sizeof(sz32)){
331 printf("reading size failed ... returned %d\n",r);
336 recordedSize = sz32 * sizeof(xx) + 8;
338 printf("verify %s: file size is %d, recorded size is %d\n", fName, totalSize, recordedSize);
339 if(totalSize != recordedSize){
340 printf("!!!!!!!!!!!!!!!!!!!!!!!!file size is wrong, should be %d, is %d\n", recordedSize,totalSize);
349 r = yaffs_read(h,xx,sizeof(xx));
351 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
355 for(i = 0; i < XX_SIZE; i++)
359 r = yaffs_read(h,xx,sizeof(xx[0]));
360 if(r != sizeof(xx[0])){
361 printf("!!!!!!!!!!!!!!!!!!!!!!!!!!reading data failed ... returned %d\n",r);
369 printf("!!!!!!!!!!!!!!!!!!!!! checksum failed\n");
372 printf("verified ok\n");
379 static void DoUpdateMainFile(void)
383 sz32 = (myrand() % 1000) + 20;
385 result = yWriteFile(fullTempMainName,sz32);
388 FatalError(__LINE__);
389 printf("Raname file %s to %s\n",fullTempMainName,fullMainName);
390 yaffs_rename(fullTempMainName,fullMainName);
394 static void DoVerifyMainFile(void)
397 result = yVerifyFile(fullMainName);
399 FatalError(__LINE__);
404 void NorStressTestInitialise(const char *prefix)
406 MakeFullNames(prefix);
408 UpdateCounter(fullPowerUpName,&powerUps,1);
409 UpdateCounter(fullStartName,&cycleStarts,1);
410 UpdateCounter(fullEndName,&cycleEnds,1);
411 UpdateCounter(fullPowerUpName,&powerUps,1);
417 void NorStressTestRun(const char *prefix, int n_cycles, int do_fsx)
420 interleave_fsx = do_fsx;
421 MakeFullNames(prefix);
422 dump_directory_tree(fullPathName);
425 dump_directory_tree(fullPathName);
427 UpdateCounter(fullPowerUpName,&powerUps,0);
428 dump_directory_tree(fullPathName);
430 while(n_cycles < 0 || n_cycles > 0){
433 UpdateCounter(fullStartName, &cycleStarts,0);
434 dump_directory_tree(fullPathName);
437 dump_directory_tree(fullPathName);
439 UpdateCounter(fullEndName,&cycleEnds,0);
440 dump_directory_tree(fullPathName);