2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002 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.
25 void dumpDir(const char *dname);
29 void copy_in_a_file(char *yaffsName,char *inName)
32 unsigned char buffer[100];
34 inh = open(inName,O_RDONLY);
35 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
37 while((ni = read(inh,buffer,100)) > 0)
39 no = yaffs_write(outh,buffer,ni);
42 printf("problem writing yaffs file\n");
51 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
55 unsigned char buffer[100];
57 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
59 memset(buffer,bval,100);
66 yaffs_write(outh,buffer,i);
68 } while (sizeOfFile > 0);
75 void make_pattern_file(char *fn,int size)
80 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
81 yaffs_lseek(outh,size-1,SEEK_SET);
82 yaffs_write(outh,"A",1);
84 for(i = 0; i < size; i+=256)
87 yaffs_lseek(outh,i,SEEK_SET);
88 yaffs_write(outh,&marker,sizeof(marker));
94 int check_pattern_file(char *fn)
102 h = yaffs_open(fn, O_RDWR,0);
103 size = yaffs_lseek(h,0,SEEK_END);
105 for(i = 0; i < size; i+=256)
107 yaffs_lseek(h,i,SEEK_SET);
108 yaffs_read(h,&marker,sizeof(marker));
112 printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
113 fn,size,i,marker,~i);
124 int dump_file_data(char *fn)
131 h = yaffs_open(fn, O_RDWR,0);
135 while(yaffs_read(h,&b,1)> 0)
152 void dump_file(const char *fn)
158 h = yaffs_open(fn,O_RDONLY,0);
161 printf("*****\nDump file %s does not exist\n",fn);
165 size = yaffs_lseek(h,0,SEEK_SET);
166 printf("*****\nDump file %s size %d\n",fn,size);
167 for(i = 0; i < size; i++)
174 void create_file_of_size(const char *fn,int syze)
182 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
184 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
186 while (iterations > 0)
188 sprintf(xx,"%s %8d",fn,iterations);
190 result = yaffs_write(h,xx,n);
192 printf("Wrote %d, should have been %d\n",result,n);
198 void verify_file_of_size(const char *fn,int syze)
207 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
209 h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
211 while (iterations > 0)
213 sprintf(xx,"%s %8d",fn,iterations);
216 result = yaffs_read(h,yy,l);
220 printf("=====>>>>> verification of file %s failed near position %lld\n",fn,(long long)yaffs_lseek(h,0,SEEK_CUR));
227 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
233 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
235 iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
236 while (iterations > 0)
238 yaffs_write(h,fn,strlen(fn));
242 yaffs_ftruncate(h,reSyze);
244 yaffs_lseek(h,0,SEEK_SET);
245 iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
246 while (iterations > 0)
248 yaffs_write(h,fn,strlen(fn));
256 void do_some_file_stuff(const char *path)
261 sprintf(fn,"%s/%s",path,"f1");
262 create_file_of_size(fn,10000);
264 sprintf(fn,"%s/%s",path,"fdel");
265 create_file_of_size(fn,10000);
268 sprintf(fn,"%s/%s",path,"f2");
270 create_resized_file_of_size(fn,10000,3000,4000);
273 void yaffs_backward_scan_test(const char *path)
281 do_some_file_stuff(path);
283 sprintf(fn,"%s/ddd",path);
287 do_some_file_stuff(fn);
297 void yaffs_device_flush_test(const char *path)
307 do_some_file_stuff(path);
309 // Open and add some data to a few files
310 for(i = 0; i < 10; i++) {
312 sprintf(fn,"%s/ff%d",path,i);
314 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
315 yaffs_write(h,xxzz,2000);
316 yaffs_write(h,xxzz,2000);
325 void short_scan_test(const char *path, int fsize, int niterations)
330 sprintf(fn,"%s/%s",path,"f1");
333 for(i = 0; i < niterations; i++)
335 printf("\n*****************\nIteration %d\n",i);
337 printf("\nmount: Directory look-up of %s\n",path);
339 make_a_file(fn,1,fsize);
346 void scan_pattern_test(const char *path, int fsize, int niterations)
353 sprintf(fn[0],"%s/%s",path,"f0");
354 sprintf(fn[1],"%s/%s",path,"f1");
355 sprintf(fn[2],"%s/%s",path,"f2");
359 for(i = 0; i < niterations; i++)
361 printf("\n*****************\nIteration %d\n",i);
363 printf("\nmount: Directory look-up of %s\n",path);
365 for(j = 0; j < 3; j++)
367 result = dump_file_data(fn[j]);
368 result = check_pattern_file(fn[j]);
369 make_pattern_file(fn[j],fsize);
370 result = dump_file_data(fn[j]);
371 result = check_pattern_file(fn[j]);
377 void fill_disk(const char *path,int nfiles)
386 for(n = 0; n < nfiles; n++)
388 sprintf(str,"%s/%d",path,n);
390 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
392 printf("writing file %s handle %d ",str, h);
394 while ((result = yaffs_write(h,xx,600)) == 600)
396 f = yaffs_freespace(path);
398 result = yaffs_close(h);
399 printf(" close %d\n",result);
403 void fill_disk_and_delete(const char *path, int nfiles, int ncycles)
409 for(i = 0; i < ncycles; i++)
411 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
412 fill_disk(path,nfiles);
414 for(j = 0; j < nfiles; j++)
416 sprintf(str,"%s/%d",path,j);
417 result = yaffs_unlink(str);
418 printf("unlinking file %s, result %d\n",str,result);
424 void fill_files(char *path,int flags, int maxIterations,int siz)
434 sprintf(str,"%s/%d",path,i);
435 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
440 for(j = 0; j < siz; j++)
442 yaffs_write(h,str,1);
451 } while(h >= 0 && i < maxIterations);
457 sprintf(str,"%s/%d",path,i);
458 printf("unlink %s\n",str);
460 } while(yaffs_unlink(str) >= 0);
464 void leave_unlinked_file(char *path,int maxIterations,int siz)
473 sprintf(str,"%s/%d",path,i);
474 printf("create %s\n",str);
475 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
481 } while(h < 0 && i < maxIterations);
485 for(i = 0; i < siz; i++)
487 yaffs_write(h,str,1);
491 printf("Leaving file %s open\n",str);
495 void dumpDirFollow(const char *dname)
502 d = yaffs_opendir(dname);
506 printf("opendir failed\n");
510 while((de = yaffs_readdir(d)) != NULL)
512 sprintf(str,"%s/%s",dname,de->d_name);
516 printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
517 switch(s.st_mode & S_IFMT)
519 case S_IFREG: printf("data file"); break;
520 case S_IFDIR: printf("directory"); break;
521 case S_IFLNK: printf("symlink -->");
522 if(yaffs_readlink(str,str,100) < 0)
525 printf("\"%s\"",str);
527 default: printf("unknown"); break;
537 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
542 void dump_directory_tree_worker(const char *dname,int recursive)
549 d = yaffs_opendir(dname);
553 printf("opendir failed\n");
557 while((de = yaffs_readdir(d)) != NULL)
559 sprintf(str,"%s/%s",dname,de->d_name);
563 printf("%s inode %d obj %x length %d mode %X ",str,s.st_ino,de->d_dont_use,(int)s.st_size,s.st_mode);
564 switch(s.st_mode & S_IFMT)
566 case S_IFREG: printf("data file"); break;
567 case S_IFDIR: printf("directory"); break;
568 case S_IFLNK: printf("symlink -->");
569 if(yaffs_readlink(str,str,100) < 0)
572 printf("\"%s\"",str);
574 default: printf("unknown"); break;
579 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
580 dump_directory_tree_worker(str,1);
589 static void dump_directory_tree(const char *dname)
591 dump_directory_tree_worker(dname,1);
593 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
596 void dumpDir(const char *dname)
597 { dump_directory_tree_worker(dname,0);
599 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
603 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
607 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
609 fd = yaffs_open(path,tflags,0);
611 if((fd >= 0) != (expectedResult > 0))
613 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
617 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
626 int long_test(int argc, char *argv[])
637 struct yaffs_stat ystat;
641 yaffs_mount("/boot");
642 yaffs_mount("/data");
643 yaffs_mount("/flash");
646 printf("\nDirectory look-up of /boot\n");
648 printf("\nDirectory look-up of /data\n");
650 printf("\nDirectory look-up of /flash\n");
653 //leave_unlinked_file("/flash",20000,0);
654 //leave_unlinked_file("/data",20000,0);
656 leave_unlinked_file("/ram",20,0);
659 f = yaffs_open("/boot/b1", O_RDONLY,0);
661 printf("open /boot/b1 readonly, f=%d\n",f);
663 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
665 printf("open /boot/b1 O_CREAT, f=%d\n",f);
668 r = yaffs_write(f,"hello",1);
669 printf("write %d attempted to write to a read-only file\n",r);
673 printf("close %d\n",r);
675 f = yaffs_open("/boot/b1", O_RDWR,0);
677 printf("open /boot/b1 O_RDWR,f=%d\n",f);
680 r = yaffs_write(f,"hello",2);
681 printf("write %d attempted to write to a writeable file\n",r);
682 r = yaffs_write(f,"world",3);
683 printf("write %d attempted to write to a writeable file\n",r);
685 r= yaffs_lseek(f,0,SEEK_END);
686 printf("seek end %d\n",r);
688 r = yaffs_read(f,buffer,10);
689 printf("read %d \"%s\"\n",r,buffer);
690 r= yaffs_lseek(f,0,SEEK_SET);
691 printf("seek set %d\n",r);
693 r = yaffs_read(f,buffer,10);
694 printf("read %d \"%s\"\n",r,buffer);
696 r = yaffs_read(f,buffer,10);
697 printf("read %d \"%s\"\n",r,buffer);
699 // Check values reading at end.
700 // A read past end of file should return 0 for 0 bytes read.
702 r= yaffs_lseek(f,0,SEEK_END);
703 r = yaffs_read(f,buffer,10);
704 printf("read at end returned %d\n",r);
705 r= yaffs_lseek(f,500,SEEK_END);
706 r = yaffs_read(f,buffer,10);
707 printf("read past end returned %d\n",r);
711 printf("close %d\n",r);
713 copy_in_a_file("/boot/yyfile","xxx");
715 // Create a file with a long name
717 copy_in_a_file("/boot/file with a long name","xxx");
720 printf("\nDirectory look-up of /boot\n");
724 r = yaffs_stat("/boot/file with a long name",&ystat);
728 r = yaffs_rename("/boot/file with a long name","/boot/r1");
730 printf("\nDirectory look-up of /boot\n");
734 r = yaffs_unlink("/boot/r1");
736 printf("\nDirectory look-up of /boot\n");
741 r = yaffs_mkdir("/boot/directory1",0);
743 printf("\nDirectory look-up of /boot\n");
745 printf("\nDirectory look-up of /boot/directory1\n");
746 dumpDir("/boot/directory1");
748 // add a file to the directory
749 copy_in_a_file("/boot/directory1/file with a long name","xxx");
751 printf("\nDirectory look-up of /boot\n");
753 printf("\nDirectory look-up of /boot/directory1\n");
754 dumpDir("/boot/directory1");
756 // Attempt to delete directory (should fail)
758 r = yaffs_rmdir("/boot/directory1");
760 printf("\nDirectory look-up of /boot\n");
762 printf("\nDirectory look-up of /boot/directory1\n");
763 dumpDir("/boot/directory1");
765 // Delete file first, then rmdir should work
766 r = yaffs_unlink("/boot/directory1/file with a long name");
767 r = yaffs_rmdir("/boot/directory1");
770 printf("\nDirectory look-up of /boot\n");
772 printf("\nDirectory look-up of /boot/directory1\n");
773 dumpDir("/boot/directory1");
776 fill_disk_and_delete("/boot",20,20);
778 printf("\nDirectory look-up of /boot\n");
782 yaffs_symlink("yyfile","/boot/slink");
784 yaffs_readlink("/boot/slink",str,100);
785 printf("symlink alias is %s\n",str);
790 printf("\nDirectory look-up of /boot\n");
792 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
793 dumpDirFollow("/boot");
794 printf("\nDirectory look-up of /boot/directory1\n");
795 dumpDir("/boot/directory1");
797 h = yaffs_open("/boot/slink",O_RDWR,0);
799 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
803 yaffs_unlink("/boot/slink");
806 printf("\nDirectory look-up of /boot\n");
811 yaffs_stat("/boot/yyfile",&ystat);
812 temp_mode = ystat.st_mode;
814 yaffs_chmod("/boot/yyfile",0x55555);
815 printf("\nDirectory look-up of /boot\n");
818 yaffs_chmod("/boot/yyfile",temp_mode);
819 printf("\nDirectory look-up of /boot\n");
822 // Permission checks...
823 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
824 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
825 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
827 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
828 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
829 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
831 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
832 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
833 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
835 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
836 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
837 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
839 yaffs_chmod("/boot/yyfile",temp_mode);
841 //create a zero-length file and unlink it (test for scan bug)
843 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
846 yaffs_unlink("/boot/zlf");
849 yaffs_DumpDevStruct("/boot");
851 fill_disk_and_delete("/boot",20,20);
853 yaffs_DumpDevStruct("/boot");
855 fill_files("/boot",1,10000,0);
856 fill_files("/boot",1,10000,5000);
857 fill_files("/boot",2,10000,0);
858 fill_files("/boot",2,10000,5000);
860 leave_unlinked_file("/data",20000,0);
861 leave_unlinked_file("/data",20000,5000);
862 leave_unlinked_file("/data",20000,5000);
863 leave_unlinked_file("/data",20000,5000);
864 leave_unlinked_file("/data",20000,5000);
865 leave_unlinked_file("/data",20000,5000);
867 yaffs_DumpDevStruct("/boot");
868 yaffs_DumpDevStruct("/data");
876 int huge_directory_test_on_path(char *path)
896 // Create a large number of files
898 for(i = 0; i < 2000; i++)
900 sprintf(str,"%s/%d",path,i);
902 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
908 d = yaffs_opendir(path);
911 while((de = yaffs_readdir(d)) != NULL) {
912 if (total >lastTotal+100*9*1024||(i & 1023)==0){
913 printf("files = %d, total = %d\n",i, total);
917 sprintf(str,"%s/%s",path,de->d_name);
919 switch(s.st_mode & S_IFMT){
921 //printf("data file");
933 int yaffs_scan_test(const char *path)
939 void rename_over_test(const char *mountpt)
945 sprintf(a,"%s/a",mountpt);
946 sprintf(b,"%s/b",mountpt);
950 yaffs_mount(mountpt);
951 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
953 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
955 yaffs_rename(a,b); // rename over
956 yaffs_rename(b,a); // rename back again (not renaimng over)
957 yaffs_rename(a,b); // rename back again (not renaimng over)
960 yaffs_unmount(mountpt);
964 int resize_stress_test(const char *path)
979 sprintf(aname,"%s%s",path,"/a");
980 sprintf(bname,"%s%s",path,"/b");
982 memset(abuffer,'a',1000);
983 memset(bbuffer,'b',1000);
985 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
986 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
988 printf(" %s %d %s %d\n",aname,a,bname,b);
992 for(j = 0; j < 100; j++)
994 yaffs_lseek(a,0,SEEK_END);
997 for(i = 0; i <20000; i++)
999 //r = yaffs_lseek(b,i,SEEK_SET);
1000 //r = yaffs_write(b,bbuffer,1000);
1005 int syz = yaffs_lseek(a,0,SEEK_END);
1008 if(syz < 0) syz = 0;
1009 yaffs_ftruncate(a,syz);
1015 r = yaffs_lseek(a,i * 500,SEEK_SET);
1016 r = yaffs_write(a,abuffer,1000);
1028 int resize_stress_test_no_grow_complex(const char *path,int iters)
1044 sprintf(aname,"%s%s",path,"/a");
1045 sprintf(bname,"%s%s",path,"/b");
1047 memset(abuffer,'a',1000);
1048 memset(bbuffer,'b',1000);
1050 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1051 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1053 printf(" %s %d %s %d\n",aname,a,bname,b);
1057 for(j = 0; j < iters; j++)
1059 yaffs_lseek(a,0,SEEK_END);
1062 for(i = 0; i <20000; i++)
1064 //r = yaffs_lseek(b,i,SEEK_SET);
1065 //r = yaffs_write(b,bbuffer,1000);
1070 int syz = yaffs_lseek(a,0,SEEK_END);
1076 if(syz < 0) syz = 0;
1077 yaffs_ftruncate(a,syz);
1078 syz = yaffs_lseek(a,0,SEEK_END);
1079 printf("shrink to %d\n",syz);
1087 r = yaffs_lseek(a,500,SEEK_END);
1088 r = yaffs_write(a,abuffer,1000);
1095 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1103 int resize_stress_test_no_grow(const char *path,int iters)
1118 sprintf(aname,"%s%s",path,"/a");
1119 sprintf(bname,"%s%s",path,"/b");
1121 memset(abuffer,'a',1000);
1122 memset(bbuffer,'b',1000);
1124 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1125 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1127 printf(" %s %d %s %d\n",aname,a,bname,b);
1131 for(j = 0; j < iters; j++)
1133 yaffs_lseek(a,0,SEEK_END);
1136 for(i = 0; i <20000; i++)
1138 //r = yaffs_lseek(b,i,SEEK_SET);
1139 //r = yaffs_write(b,bbuffer,1000);
1144 int syz = yaffs_lseek(a,0,SEEK_END);
1150 if(syz < 0) syz = 0;
1151 yaffs_ftruncate(a,syz);
1152 syz = yaffs_lseek(a,0,SEEK_END);
1153 printf("shrink to %d\n",syz);
1161 r = yaffs_lseek(a,-500,SEEK_END);
1162 r = yaffs_write(a,abuffer,1000);
1168 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1176 int directory_rename_test(void)
1181 yaffs_mount("/ram");
1182 yaffs_mkdir("/ram/a",0);
1183 yaffs_mkdir("/ram/a/b",0);
1184 yaffs_mkdir("/ram/c",0);
1186 printf("\nDirectory look-up of /ram\n");
1189 dumpDir("/ram/a/b");
1191 printf("Do rename (should fail)\n");
1193 r = yaffs_rename("/ram/a","/ram/a/b/d");
1194 printf("\nDirectory look-up of /ram\n");
1197 dumpDir("/ram/a/b");
1199 printf("Do rename (should not fail)\n");
1201 r = yaffs_rename("/ram/c","/ram/a/b/d");
1202 printf("\nDirectory look-up of /ram\n");
1205 dumpDir("/ram/a/b");
1212 int cache_read_test(void)
1216 int sizeOfFiles = 500000;
1221 yaffs_mount("/boot");
1223 make_a_file("/boot/a",'a',sizeOfFiles);
1224 make_a_file("/boot/b",'b',sizeOfFiles);
1226 a = yaffs_open("/boot/a",O_RDONLY,0);
1227 b = yaffs_open("/boot/b",O_RDONLY,0);
1228 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1232 if (i > 100) i = 100;
1234 yaffs_read(a,buffer,i);
1235 yaffs_read(b,buffer,i);
1236 yaffs_write(c,buffer,i);
1237 } while(sizeOfFiles > 0);
1245 int cache_bypass_bug_test(void)
1247 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1248 // resulting in erroneous reads after writes.
1249 // This bug has been fixed.
1255 memset(buffer1,0,sizeof(buffer1));
1256 memset(buffer2,0,sizeof(buffer2));
1260 yaffs_mount("/boot");
1262 // Create a file of 2000 bytes.
1263 make_a_file("/boot/a",'X',2000);
1265 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1267 // Write a short sequence to the file.
1268 // This will go into the cache.
1269 yaffs_lseek(a,0,SEEK_SET);
1270 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1272 // Read a short sequence from the file.
1273 // This will come from the cache.
1274 yaffs_lseek(a,0,SEEK_SET);
1275 yaffs_read(a,buffer1,30);
1277 // Read a page size sequence from the file.
1278 yaffs_lseek(a,0,SEEK_SET);
1279 yaffs_read(a,buffer2,512);
1281 printf("buffer 1 %s\n",buffer1);
1282 printf("buffer 2 %s\n",buffer2);
1284 if(strncmp(buffer1,buffer2,20))
1286 printf("Cache bypass bug detected!!!!!\n");
1294 int free_space_check(void)
1299 yaffs_mount("/boot");
1300 fill_disk("/boot/",2);
1301 f = yaffs_freespace("/boot");
1303 printf("%d free when disk full\n",f);
1307 int truncate_test(void)
1317 yaffs_mount("/boot");
1319 yaffs_unlink("/boot/trunctest");
1321 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1323 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1325 yaffs_ftruncate(a,3);
1326 l= yaffs_lseek(a,0,SEEK_END);
1328 printf("truncated length is %d\n",l);
1330 yaffs_lseek(a,5,SEEK_SET);
1331 yaffs_write(a,"1",1);
1333 yaffs_lseek(a,0,SEEK_SET);
1335 r = yaffs_read(a,y,10);
1337 printf("read %d bytes:",r);
1339 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1351 void fill_disk_test(const char *mountpt)
1356 for(i = 0; i < 5; i++)
1358 yaffs_mount(mountpt);
1359 fill_disk_and_delete(mountpt,100,i+1);
1360 yaffs_unmount(mountpt);
1367 void lookup_test(const char *mountpt)
1379 yaffs_mount(mountpt);
1381 d = yaffs_opendir(mountpt);
1385 printf("opendir failed\n");
1390 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1392 printf("unlinking %s\n",de->d_name);
1393 yaffs_unlink(de->d_name);
1396 printf("%d files deleted\n",i);
1400 for(i = 0; i < 2000; i++){
1401 sprintf(a,"%s/%d",mountpt,i);
1402 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1407 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1409 printf("%d %s\n",i,de->d_name);
1412 printf("%d files listed\n\n\n",i);
1419 for(i = 0; i < 2000; i++){
1420 sprintf(a,"%s/%d",mountpt,i);
1425 yaffs_unmount(mountpt);
1429 void link_test(const char *mountpt)
1437 sprintf(a,"%s/aaa",mountpt);
1438 sprintf(b,"%s/bbb",mountpt);
1439 sprintf(c,"%s/ccc",mountpt);
1443 yaffs_mount(mountpt);
1446 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1447 for(i = 0; i < 100; i++)
1448 yaffs_write(h,a,100);
1461 yaffs_unmount(mountpt);
1462 yaffs_mount(mountpt);
1464 printf("link test done\n");
1468 void freespace_test(const char *mountpt)
1478 sprintf(a,"%s/aaa",mountpt);
1482 yaffs_mount(mountpt);
1484 f0 = yaffs_freespace(mountpt);
1486 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1488 for(i = 0; i < 100; i++)
1489 yaffs_write(h,a,100);
1493 f1 = yaffs_freespace(mountpt);
1497 f2 = yaffs_freespace(mountpt);
1500 yaffs_unmount(mountpt);
1501 yaffs_mount(mountpt);
1503 f3 = yaffs_freespace(mountpt);
1505 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1510 void simple_rw_test(const char *mountpt)
1519 sprintf(a,"%s/aaa",mountpt);
1523 yaffs_mount(mountpt);
1527 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1529 for(i = 100000;i < 200000; i++){
1530 result = yaffs_write(h,&i,sizeof(i));
1534 printf("write error\n");
1541 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1544 yaffs_lseek(h,0,SEEK_SET);
1546 for(i = 100000; i < 200000; i++){
1547 result = yaffs_read(h,&x,sizeof(x));
1549 if(result != 4 || x != i){
1550 printf("read error %d %x %x\n",i,result,x);
1554 printf("Simple rw test passed\n");
1561 void scan_deleted_files_test(const char *mountpt)
1573 sprintf(sub,"%s/sdir",mountpt);
1576 for(j = 0; j < 10; j++)
1578 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1579 yaffs_mount(mountpt);
1583 p = (j & 0) ? mountpt: sub;
1585 for(i = 0; i < 100; i++)
1587 sprintf(fn,"%s/%d",p,i);
1591 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1592 for(k = 0; k < 1000; k++)
1593 yaffs_write(h,fn,100);
1600 for(i = 0; i < 10; i++)
1602 sprintf(fn,"%s/%d",p,i);
1610 yaffs_unmount(mountpt);
1619 void write_10k(int h)
1622 const char *s="0123456789";
1623 for(i = 0; i < 1000; i++)
1624 yaffs_write(h,s,10);
1627 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1633 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1635 for(i = 0; i < 100000; i+= 10000)
1640 offs = yaffs_lseek(h1,0,SEEK_CUR);
1643 printf("Could not write file\n");
1647 for(i = 0; i < 100000; i+= 10000)
1652 offs = yaffs_lseek(h1,0,SEEK_CUR);
1655 printf("Could not write file\n");
1659 yaffs_unlink(fdel1);
1664 void verify_200k_file(const char *fn)
1669 const char *s="0123456789";
1672 h1 = yaffs_open(fn, O_RDONLY, 0);
1674 for(i = 0; i < 200000 && errCount < 10; i+= 10)
1676 yaffs_read(h1,x,10);
1677 if(strncmp(x,s,10) != 0)
1679 printf("File %s verification failed at %d\n",fn,i);
1684 printf("Too many errors... aborted\n");
1691 void check_resize_gc_bug(const char *mountpt)
1700 sprintf(a,"%s/a",mountpt);
1701 sprintf(b,"%s/b",mountpt);
1702 sprintf(c,"%s/c",mountpt);
1708 yaffs_mount(mountpt);
1712 for(i = 0; i < 50; i++)
1714 printf("A\n");write_200k_file(a,"",c);
1715 printf("B\n");verify_200k_file(a);
1716 printf("C\n");write_200k_file(b,a,c);
1717 printf("D\n");verify_200k_file(b);
1718 yaffs_unmount(mountpt);
1719 yaffs_mount(mountpt);
1720 printf("E\n");verify_200k_file(a);
1721 printf("F\n");verify_200k_file(b);
1727 void multi_mount_test(const char *mountpt,int nmounts)
1735 sprintf(a,"%s/a",mountpt);
1739 for(i = 0; i < nmounts; i++){
1745 static char xx[1000];
1747 printf("############### Iteration %d Start\n",i);
1748 if(1 || i == 0 || i == 5)
1749 yaffs_mount(mountpt);
1751 dump_directory_tree(mountpt);
1756 sprintf(xx,"%s/0",a);
1757 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1759 sprintf(xx,"%s/1",a);
1760 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1763 for(j = 0; j < 200; j++){
1764 yaffs_write(h0,xx,1000);
1765 yaffs_write(h1,xx,1000);
1768 while(yaffs_write(h0,xx,1000) > 0){
1770 yaffs_write(h1,xx,1000);
1773 len0 = yaffs_lseek(h0,0,SEEK_END);
1774 len1 = yaffs_lseek(h1,0,SEEK_END);
1776 yaffs_lseek(h0,0,SEEK_SET);
1777 yaffs_lseek(h1,0,SEEK_SET);
1779 for(j = 0; j < 200; j++){
1780 yaffs_read(h0,xx,1000);
1781 yaffs_read(h1,xx,1000);
1785 // yaffs_truncate(h0,0);
1789 printf("########### %d\n",i);
1790 dump_directory_tree(mountpt);
1792 if(1 || i == 4 || i == nmounts -1)
1793 yaffs_unmount(mountpt);
1798 void small_mount_test(const char *mountpt,int nmounts)
1812 sprintf(a,"%s/a",mountpt);
1818 for(i = 0; i < nmounts; i++){
1820 static char xx[1000];
1822 printf("############### Iteration %d Start\n",i);
1823 if(1 || i == 0 || i == 5)
1824 yaffs_mount(mountpt);
1826 dump_directory_tree(mountpt);
1830 sprintf(xx,"%s/0",a);
1833 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1834 for(j = 0; j < 130; j++)
1835 yaffs_write(h0,xx,1000);
1839 h0 = yaffs_open(xx,O_RDONLY,0);
1841 sprintf(xx,"%s/1",a);
1842 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1844 while((nread = yaffs_read(h0,xx,1000)) > 0)
1845 yaffs_write(h1,xx,nread);
1848 len0 = yaffs_lseek(h0,0,SEEK_END);
1849 len1 = yaffs_lseek(h1,0,SEEK_END);
1851 yaffs_lseek(h0,0,SEEK_SET);
1852 yaffs_lseek(h1,0,SEEK_SET);
1854 for(j = 0; j < 200; j++){
1855 yaffs_read(h0,xx,1000);
1856 yaffs_read(h1,xx,1000);
1862 printf("########### %d\n",i);
1863 dump_directory_tree(mountpt);
1865 if(1 || i == 4 || i == nmounts -1)
1866 yaffs_unmount(mountpt);
1873 void small_overwrite_test(const char *mountpt,int nmounts)
1884 sprintf(a,"%s/a",mountpt);
1890 for(i = 0; i < nmounts; i++){
1892 static char xx[8000];
1894 printf("############### Iteration %d Start\n",i);
1896 yaffs_mount(mountpt);
1898 dump_directory_tree(mountpt);
1902 sprintf(xx,"%s/0",a);
1903 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1904 sprintf(xx,"%s/1",a);
1905 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1907 for(j = 0; j < 1000000; j+=1000){
1908 yaffs_ftruncate(h0,j);
1909 yaffs_lseek(h0,j,SEEK_SET);
1910 yaffs_write(h0,xx,7000);
1911 yaffs_write(h1,xx,7000);
1919 printf("########### %d\n",i);
1920 dump_directory_tree(mountpt);
1923 yaffs_unmount(mountpt);
1928 void seek_overwrite_test(const char *mountpt,int nmounts)
1939 sprintf(a,"%s/f",mountpt);
1943 yaffs_mount(mountpt);
1946 for(i = 0; i < nmounts; i++){
1948 h0 = yaffs_open(a, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1950 for(j = 0; j < 100000; j++){
1951 yaffs_lseek(h0,0,SEEK_SET);
1952 yaffs_write(h0,xx,5000);
1953 yaffs_lseek(h0,0x100000,SEEK_SET);
1954 yaffs_write(h0,xx,5000);
1966 void yaffs_touch(const char *fn)
1968 yaffs_chmod(fn, S_IREAD | S_IWRITE);
1971 void checkpoint_fill_test(const char *mountpt,int nmounts)
1982 sprintf(a,"%s/a",mountpt);
1989 for(i = 0; i < nmounts; i++){
1990 printf("############### Iteration %d Start\n",i);
1991 yaffs_mount(mountpt);
1992 dump_directory_tree(mountpt);
1995 sprintf(b,"%s/zz",a);
1997 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
2000 while(yaffs_write(h,c,50) == 50){}
2004 for(j = 0; j < 2; j++){
2005 printf("touch %d\n",j);
2007 yaffs_unmount(mountpt);
2008 yaffs_mount(mountpt);
2011 dump_directory_tree(mountpt);
2012 yaffs_unmount(mountpt);
2017 int make_file2(const char *name1, const char *name2,int syz)
2027 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2029 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2031 while(syz > 0 && n > 0){
2032 i = (syz > 2500) ? 2500 : syz;
2033 n = yaffs_write(h1,xx,i);
2034 n = yaffs_write(h2,xx,i);
2043 extern void SetCheckpointReservedBlocks(int n);
2045 void checkpoint_upgrade_test(const char *mountpt,int nmounts)
2055 sprintf(a,"%s/a",mountpt);
2060 printf("Create start condition\n");
2062 SetCheckpointReservedBlocks(0);
2063 yaffs_mount(mountpt);
2065 sprintf(b,"%s/zz",a);
2066 sprintf(c,"%s/xx",a);
2067 make_file2(b,c,2000000);
2068 sprintf(d,"%s/aa",a);
2069 make_file2(d,NULL,500000000);
2070 dump_directory_tree(mountpt);
2072 printf("Umount/mount attempt full\n");
2073 yaffs_unmount(mountpt);
2075 SetCheckpointReservedBlocks(10);
2076 yaffs_mount(mountpt);
2078 printf("unlink small file\n");
2080 dump_directory_tree(mountpt);
2082 printf("Umount/mount attempt\n");
2083 yaffs_unmount(mountpt);
2084 yaffs_mount(mountpt);
2086 for(j = 0; j < 500; j++){
2087 printf("***** touch %d\n",j);
2088 dump_directory_tree(mountpt);
2090 yaffs_unmount(mountpt);
2091 yaffs_mount(mountpt);
2094 for(j = 0; j < 500; j++){
2095 printf("***** touch %d\n",j);
2096 dump_directory_tree(mountpt);
2098 yaffs_unmount(mountpt);
2099 yaffs_mount(mountpt);
2103 void huge_array_test(const char *mountpt,int n)
2114 sprintf(a,"mount point %s",mountpt);
2120 yaffs_mount(mountpt);
2125 printf("\n\n START run\n\n");
2126 while((space = yaffs_freespace(mountpt)) > 25000000){
2127 sprintf(a,"%s/file%d",mountpt,fnum);
2129 printf("create file %s, free space %d\n",a,space);
2130 create_file_of_size(a,10000000);
2131 printf("verifying file %s\n",a);
2132 verify_file_of_size(a,10000000);
2135 printf("\n\n verification/deletion\n\n");
2137 for(i = 0; i < fnum; i++){
2138 sprintf(a,"%s/file%d",mountpt,i);
2139 printf("verifying file %s\n",a);
2140 verify_file_of_size(a,10000000);
2141 printf("deleting file %s\n",a);
2144 printf("\n\n done \n\n");
2151 void random_write(int h)
2153 static char buffer[12000];
2156 n = random() & 0x1FFF;
2157 yaffs_write(h,buffer,n);
2160 void random_seek(int h)
2163 n = random() & 0xFFFFF;
2164 yaffs_lseek(h,n,SEEK_SET);
2167 void random_truncate(int h, char * name)
2171 n = random() & 0xFFFFF;
2172 flen = yaffs_lseek(h,0,SEEK_END);
2175 yaffs_ftruncate(h,n);
2176 yaffs_lseek(h,n,SEEK_SET);
2180 #define NSMALLFILES 10
2181 void random_small_file_test(const char *mountpt,int iterations)
2184 char a[NSMALLFILES][50];
2195 yaffs_mount(mountpt);
2197 for(i = 0; i < NSMALLFILES; i++){
2202 for(n = 0; n < iterations; n++){
2204 for(i = 0; i < NSMALLFILES; i++) {
2207 if(strlen(a[i]) == 0){
2208 sprintf(a[i],"%s/%dx%d",mountpt,n,i);
2209 h[i] = yaffs_open(a[i],O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2213 printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
2223 random_truncate(h[i],a[i]);
2226 case 5: random_seek(h[i]);
2243 for(i = 0; i < NSMALLFILES; i++)
2246 yaffs_unmount(mountpt);
2251 int main(int argc, char *argv[])
2253 //return long_test(argc,argv);
2255 //return cache_read_test();
2257 resize_stress_test_no_grow("/flash/flash",20);
2259 //huge_directory_test_on_path("/ram2k");
2261 //yaffs_backward_scan_test("/flash/flash");
2262 // yaffs_device_flush_test("/flash/flash");
2265 //scan_pattern_test("/flash",10000,10);
2266 //short_scan_test("/flash/flash",40000,200);
2267 //small_mount_test("/flash/flash",1000);
2268 //small_overwrite_test("/flash/flash",1000);
2269 //seek_overwrite_test("/flash/flash",1000);
2270 //checkpoint_fill_test("/flash/flash",20);
2271 //checkpoint_upgrade_test("/flash/flash",20);
2272 //small_overwrite_test("/flash/flash",1000);
2273 //checkpoint_fill_test("/flash/flash",20);
2274 // random_small_file_test("/flash/flash",10000);
2275 // huge_array_test("/flash/flash",10);
2280 //long_test_on_path("/ram2k");
2281 // long_test_on_path("/flash");
2282 //simple_rw_test("/flash/flash");
2283 //fill_disk_test("/flash/flash");
2284 // rename_over_test("/flash");
2285 //lookup_test("/flash");
2286 //freespace_test("/flash/flash");
2288 //link_test("/flash/flash");
2293 // cache_bypass_bug_test();
2295 //free_space_check();
2297 //check_resize_gc_bug("/flash");