yaffs: Change trace flag for partially written blocks
[yaffs2.git] / direct / tests / yaffs_test.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  *
4  * Copyright (C) 2002 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
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <stdlib.h>
24
25 #include "yaffsfs.h"
26
27 #include "yaffs_trace.h"
28
29 #include "nor_stress.h"
30 #include "yaffs_fsx.h"
31
32 void (*ext_fatal)(void);
33
34
35 int random_seed;
36 int simulate_power_failure = 0;
37
38
39 int do_fsx;
40 int do_bash_around;
41
42 int init_test;
43 int do_upgrade;
44 int n_cycles = -1;
45 int yaffs_test_maxMallocs;
46
47 extern int ops_multiplier;
48
49 char mount_point[200];
50
51 #define BASH_HANDLES 20
52 void yaffs_bash_around(const char *mountpt, int n_cycles)
53 {
54         char name[200];
55         char name1[200];
56         int  h[BASH_HANDLES];
57         int i;
58         int op;
59         int pos;
60         int n;
61         int n1;
62         int start_op;
63         int op_max = 99;
64         
65         int cycle = 0;
66         
67         sprintf(name,"%s/bashdir",mountpt);
68         
69         yaffs_mkdir(name,0666);
70
71         for(i = 0; i < BASH_HANDLES; i++)
72                 h[i] = -1;
73                 
74         while(n_cycles){
75                 if(cycle % 100 == 0){
76                         printf("CYCLE %8d mo %2d inodes %d space %d ",cycle,op_max,
77                                 yaffs_inodecount(mountpt),yaffs_freespace(mountpt));
78                         for(i = 0; i < BASH_HANDLES; i++)
79                                 printf("%2d ",h[i]);
80                         printf("\n");
81                 }
82                 cycle++;
83
84                 if(n_cycles > 0)
85                         n_cycles--;
86                 i = rand() % BASH_HANDLES;
87                 op = rand() % op_max;
88                 pos = rand() & 20000000;
89                 n = rand() % 100;
90                 n1 = rand() % 100;
91                 
92                 sprintf(name,"%s/bashdir/xx%d",mountpt,n);
93                 sprintf(name1,"%s/bashdir/xx%d",mountpt,n1);
94
95                 start_op = op;
96                 
97                 op-=1;
98                 if(op < 0){
99                         if(h[i]>= 0){
100                                 yaffs_close(h[i]);
101                                 h[i] = -1;
102                         }
103                         continue;
104                 }
105                 op-=1;
106                 if(op < 0){
107                         if(h[i] < 0)
108                                 h[i] = yaffs_open(name,O_CREAT| O_RDWR, 0666);
109                         continue;
110                 }
111
112                 op-=5;
113                 if(op< 0){
114                         yaffs_lseek(h[i],pos,SEEK_SET);
115                         yaffs_write(h[i],name,n);
116                         continue;
117                 }
118                 op-=1;
119                 if(op < 0){
120                         yaffs_unlink(name);
121                         continue;
122                 }
123                 op-=1;
124                 if(op < 0){
125                         yaffs_rename(name,name1);
126                         continue;
127                 }
128                 op-=1;
129                 if(op < 0){
130                         yaffs_mkdir(name,0666);
131                         continue;
132                 }
133                 op-=1;
134                 if(op < 0){
135                         yaffs_rmdir(name);
136                         continue;
137                 }
138                 
139                 op_max = start_op-op;
140         }
141                 
142         
143 }
144
145 void BadUsage(void)
146 {
147         printf("Usage: yaffs_test [options] mountpoint\n");
148         printf("options\n");
149         printf(" f: do fsx testing\n");
150         printf(" i: initialise for upgrade testing\n");
151         printf(" l: multiply number of operations by 5\n");
152         printf(" m: random max number of mallocs\n");
153         printf(" n nnn: number of cycles\n");
154         printf(" p: simulate power fail testing\n");
155         printf(" s sss: set seed\n");
156         printf(" u: do upgrade test\n");
157         printf(" b: bash-about test\n");
158         exit(2);
159 }
160
161 int sleep_exit = 0;
162 int sleep_time = 0;
163 void test_fatal(void)
164 {
165         printf("fatal yaffs test pid %d sleeping\n",getpid());
166         while(!sleep_exit){
167                 sleep(1);
168                 sleep_time++;
169         }
170         
171         signal(SIGSEGV,SIG_IGN);
172         signal(SIGBUS,SIG_IGN);
173         signal(SIGABRT,SIG_IGN);
174         sleep_exit = 0;
175         
176 }
177
178 void bad_ptr_handler(int sig)
179 {
180         printf("signal %d received\n",sig);
181         test_fatal();
182 }
183
184 int main(int argc, char **argv)
185 {
186         int ch;
187         int random_mallocs=0;
188         ext_fatal = test_fatal;
189
190 #if 1
191         signal(SIGSEGV,bad_ptr_handler);
192         signal(SIGBUS,bad_ptr_handler);
193         signal(SIGABRT,bad_ptr_handler);
194 #endif  
195         while ((ch = getopt(argc,argv, "bfilmn:ps:t:u"))
196                != EOF)
197                 switch (ch) {
198                 case 's':
199                         random_seed = atoi(optarg);
200                         break;
201                 case 'p':
202                         simulate_power_failure =1;
203                         break;
204                 case 'i':
205                         init_test = 1;
206                         break;
207                 case 'b':
208                         do_bash_around = 1;
209                         break;
210                 case 'f':
211                         do_fsx = 1;
212                         break;
213                 case 'l':
214                         ops_multiplier *= 5;
215                         break;
216                 case 'u':
217                         do_upgrade = 1;
218                         break;
219                 case 'm':
220                         random_mallocs=1;
221                         break;
222                 case 'n':
223                         n_cycles = atoi(optarg);
224                         break;
225                 case 't':
226                         yaffs_traceMask = strtol(optarg,NULL,0);
227                         break;
228                 default:
229                         BadUsage();
230                         /* NOTREACHED */
231                 }
232         argc -= optind;
233         argv += optind;
234         
235         if(random_mallocs){
236                 yaffs_test_maxMallocs = 0xFFF & random_seed;
237         }
238         
239         if(argc == 1) {
240                 strcpy(mount_point,argv[0]);
241                 
242                 if(simulate_power_failure)
243                         n_cycles = -1;
244                 printf("Running test %s %s %s %s seed %d cycles %d\n",
245                         do_upgrade ? "fw_upgrade" : "",
246                         init_test ? "initialise":"",
247                         do_fsx ? "fsx" :"",
248                         simulate_power_failure ? "power_fail" : "",
249                         random_seed, n_cycles);
250
251                 yaffs_StartUp();
252                 yaffs_mount(mount_point);
253                 printf("Mount complete\n");
254                         
255                 if(do_upgrade && init_test){
256                         simulate_power_failure = 0;
257                         NorStressTestInitialise(mount_point);
258                 } else if(do_upgrade){
259                         printf("Running stress on %s with seed %d\n",mount_point,random_seed);
260                         NorStressTestRun(mount_point,n_cycles,do_fsx);
261                 } else if(do_fsx){
262                         yaffs_fsx_main(mount_point,n_cycles);
263                 } else if(do_bash_around){
264                         yaffs_bash_around(mount_point,n_cycles);
265                 }else {
266                         printf("No test to run!\n");
267                         BadUsage();
268                 }
269                 yaffs_unmount(mount_point);
270                 
271                 printf("Test run completed!\n");
272         }
273         else
274                 BadUsage();
275         return 0;
276 }
277
278