2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 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 * Test code for the "direct" interface.
26 void dumpDir(const char *dname);
30 void copy_in_a_file(char *yaffsName,char *inName)
33 unsigned char buffer[100];
35 inh = open(inName,O_RDONLY);
36 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
38 while((ni = read(inh,buffer,100)) > 0)
40 no = yaffs_write(outh,buffer,ni);
43 printf("problem writing yaffs file\n");
52 void make_a_file(char *yaffsName,char bval,int sizeOfFile)
56 unsigned char buffer[100];
58 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
60 memset(buffer,bval,100);
67 yaffs_write(outh,buffer,i);
69 } while (sizeOfFile > 0);
76 void make_pattern_file(char *fn,int size)
81 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
82 yaffs_lseek(outh,size-1,SEEK_SET);
83 yaffs_write(outh,"A",1);
85 for(i = 0; i < size; i+=256)
88 yaffs_lseek(outh,i,SEEK_SET);
89 yaffs_write(outh,&marker,sizeof(marker));
95 int check_pattern_file(char *fn)
103 h = yaffs_open(fn, O_RDWR,0);
104 size = yaffs_lseek(h,0,SEEK_END);
106 for(i = 0; i < size; i+=256)
108 yaffs_lseek(h,i,SEEK_SET);
109 yaffs_read(h,&marker,sizeof(marker));
113 printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
114 fn,size,i,marker,~i);
125 int dump_file_data(char *fn)
134 h = yaffs_open(fn, O_RDWR,0);
138 while(yaffs_read(h,&b,1)> 0)
155 void dump_file(const char *fn)
161 h = yaffs_open(fn,O_RDONLY,0);
164 printf("*****\nDump file %s does not exist\n",fn);
168 size = yaffs_lseek(h,0,SEEK_SET);
169 printf("*****\nDump file %s size %d\n",fn,size);
170 for(i = 0; i < size; i++)
177 void create_file_of_size(const char *fn,int syze)
184 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
186 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
188 while (iterations > 0)
190 sprintf(xx,"%s %8d",fn,iterations);
191 yaffs_write(h,xx,strlen(xx));
197 void verify_file_of_size(const char *fn,int syze)
206 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
208 h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
210 while (iterations > 0)
212 sprintf(xx,"%s %8d",fn,iterations);
219 printf("=====>>>>> verification of file %s failed near position %d\n",fn,yaffs_lseek(h,0,SEEK_CUR));
226 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
234 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
236 iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
237 while (iterations > 0)
239 yaffs_write(h,fn,strlen(fn));
243 yaffs_truncate(h,reSyze);
245 yaffs_lseek(h,0,SEEK_SET);
246 iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
247 while (iterations > 0)
249 yaffs_write(h,fn,strlen(fn));
257 void do_some_file_stuff(const char *path)
262 sprintf(fn,"%s/%s",path,"f1");
263 create_file_of_size(fn,10000);
265 sprintf(fn,"%s/%s",path,"fdel");
266 create_file_of_size(fn,10000);
269 sprintf(fn,"%s/%s",path,"f2");
271 create_resized_file_of_size(fn,10000,3000,4000);
274 void yaffs_backward_scan_test(const char *path)
282 do_some_file_stuff(path);
284 sprintf(fn,"%s/ddd",path);
288 do_some_file_stuff(fn);
298 void yaffs_device_flush_test(const char *path)
308 do_some_file_stuff(path);
310 // Open and add some data to a few files
311 for(i = 0; i < 10; i++) {
313 sprintf(fn,"%s/ff%d",path,i);
315 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
316 yaffs_write(h,xxzz,2000);
317 yaffs_write(h,xxzz,2000);
326 void short_scan_test(const char *path, int fsize, int niterations)
331 sprintf(fn,"%s/%s",path,"f1");
334 for(i = 0; i < niterations; i++)
336 printf("\n*****************\nIteration %d\n",i);
338 printf("\nmount: Directory look-up of %s\n",path);
340 make_a_file(fn,1,fsize);
347 void scan_pattern_test(const char *path, int fsize, int niterations)
354 sprintf(fn[0],"%s/%s",path,"f0");
355 sprintf(fn[1],"%s/%s",path,"f1");
356 sprintf(fn[2],"%s/%s",path,"f2");
360 for(i = 0; i < niterations; i++)
362 printf("\n*****************\nIteration %d\n",i);
364 printf("\nmount: Directory look-up of %s\n",path);
366 for(j = 0; j < 3; j++)
368 result = dump_file_data(fn[j]);
369 result = check_pattern_file(fn[j]);
370 make_pattern_file(fn[j],fsize);
371 result = dump_file_data(fn[j]);
372 result = check_pattern_file(fn[j]);
378 void fill_disk(char *path,int nfiles)
387 for(n = 0; n < nfiles; n++)
389 sprintf(str,"%s/%d",path,n);
391 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
393 printf("writing file %s handle %d ",str, h);
395 while ((result = yaffs_write(h,xx,600)) == 600)
397 f = yaffs_freespace(path);
399 result = yaffs_close(h);
400 printf(" close %d\n",result);
404 void fill_disk_and_delete(char *path, int nfiles, int ncycles)
410 for(i = 0; i < ncycles; i++)
412 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
413 fill_disk(path,nfiles);
415 for(j = 0; j < nfiles; j++)
417 sprintf(str,"%s/%d",path,j);
418 result = yaffs_unlink(str);
419 printf("unlinking file %s, result %d\n",str,result);
425 void fill_files(char *path,int flags, int maxIterations,int siz)
435 sprintf(str,"%s/%d",path,i);
436 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
441 for(j = 0; j < siz; j++)
443 yaffs_write(h,str,1);
452 } while(h >= 0 && i < maxIterations);
458 sprintf(str,"%s/%d",path,i);
459 printf("unlink %s\n",str);
461 } while(yaffs_unlink(str) >= 0);
465 void leave_unlinked_file(char *path,int maxIterations,int siz)
474 sprintf(str,"%s/%d",path,i);
475 printf("create %s\n",str);
476 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
482 } while(h < 0 && i < maxIterations);
486 for(i = 0; i < siz; i++)
488 yaffs_write(h,str,1);
492 printf("Leaving file %s open\n",str);
496 void dumpDirFollow(const char *dname)
503 d = yaffs_opendir(dname);
507 printf("opendir failed\n");
511 while((de = yaffs_readdir(d)) != NULL)
513 sprintf(str,"%s/%s",dname,de->d_name);
517 printf("%s length %d mode %X ",de->d_name,(int)s.st_size,s.st_mode);
518 switch(s.st_mode & S_IFMT)
520 case S_IFREG: printf("data file"); break;
521 case S_IFDIR: printf("directory"); break;
522 case S_IFLNK: printf("symlink -->");
523 if(yaffs_readlink(str,str,100) < 0)
526 printf("\"%s\"",str);
528 default: printf("unknown"); break;
538 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
543 void dump_directory_tree_worker(const char *dname,int recursive)
550 d = yaffs_opendir(dname);
554 printf("opendir failed\n");
558 while((de = yaffs_readdir(d)) != NULL)
560 sprintf(str,"%s/%s",dname,de->d_name);
564 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);
565 switch(s.st_mode & S_IFMT)
567 case S_IFREG: printf("data file"); break;
568 case S_IFDIR: printf("directory"); break;
569 case S_IFLNK: printf("symlink -->");
570 if(yaffs_readlink(str,str,100) < 0)
573 printf("\"%s\"",str);
575 default: printf("unknown"); break;
580 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
581 dump_directory_tree_worker(str,1);
590 static void dump_directory_tree(const char *dname)
592 dump_directory_tree_worker(dname,1);
594 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
597 void dumpDir(const char *dname)
598 { dump_directory_tree_worker(dname,0);
600 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
604 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
608 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
610 fd = yaffs_open(path,tflags,0);
612 if((fd >= 0) != (expectedResult > 0))
614 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
618 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
627 int long_test(int argc, char *argv[])
638 struct yaffs_stat ystat;
642 yaffs_mount("/boot");
643 yaffs_mount("/data");
644 yaffs_mount("/flash");
647 printf("\nDirectory look-up of /boot\n");
649 printf("\nDirectory look-up of /data\n");
651 printf("\nDirectory look-up of /flash\n");
654 //leave_unlinked_file("/flash",20000,0);
655 //leave_unlinked_file("/data",20000,0);
657 leave_unlinked_file("/ram",20,0);
660 f = yaffs_open("/boot/b1", O_RDONLY,0);
662 printf("open /boot/b1 readonly, f=%d\n",f);
664 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
666 printf("open /boot/b1 O_CREAT, f=%d\n",f);
669 r = yaffs_write(f,"hello",1);
670 printf("write %d attempted to write to a read-only file\n",r);
674 printf("close %d\n",r);
676 f = yaffs_open("/boot/b1", O_RDWR,0);
678 printf("open /boot/b1 O_RDWR,f=%d\n",f);
681 r = yaffs_write(f,"hello",2);
682 printf("write %d attempted to write to a writeable file\n",r);
683 r = yaffs_write(f,"world",3);
684 printf("write %d attempted to write to a writeable file\n",r);
686 r= yaffs_lseek(f,0,SEEK_END);
687 printf("seek end %d\n",r);
689 r = yaffs_read(f,buffer,10);
690 printf("read %d \"%s\"\n",r,buffer);
691 r= yaffs_lseek(f,0,SEEK_SET);
692 printf("seek set %d\n",r);
694 r = yaffs_read(f,buffer,10);
695 printf("read %d \"%s\"\n",r,buffer);
697 r = yaffs_read(f,buffer,10);
698 printf("read %d \"%s\"\n",r,buffer);
700 // Check values reading at end.
701 // A read past end of file should return 0 for 0 bytes read.
703 r= yaffs_lseek(f,0,SEEK_END);
704 r = yaffs_read(f,buffer,10);
705 printf("read at end returned %d\n",r);
706 r= yaffs_lseek(f,500,SEEK_END);
707 r = yaffs_read(f,buffer,10);
708 printf("read past end returned %d\n",r);
712 printf("close %d\n",r);
714 copy_in_a_file("/boot/yyfile","xxx");
716 // Create a file with a long name
718 copy_in_a_file("/boot/file with a long name","xxx");
721 printf("\nDirectory look-up of /boot\n");
725 r = yaffs_stat("/boot/file with a long name",&ystat);
729 r = yaffs_rename("/boot/file with a long name","/boot/r1");
731 printf("\nDirectory look-up of /boot\n");
735 r = yaffs_unlink("/boot/r1");
737 printf("\nDirectory look-up of /boot\n");
742 r = yaffs_mkdir("/boot/directory1",0);
744 printf("\nDirectory look-up of /boot\n");
746 printf("\nDirectory look-up of /boot/directory1\n");
747 dumpDir("/boot/directory1");
749 // add a file to the directory
750 copy_in_a_file("/boot/directory1/file with a long name","xxx");
752 printf("\nDirectory look-up of /boot\n");
754 printf("\nDirectory look-up of /boot/directory1\n");
755 dumpDir("/boot/directory1");
757 // Attempt to delete directory (should fail)
759 r = yaffs_rmdir("/boot/directory1");
761 printf("\nDirectory look-up of /boot\n");
763 printf("\nDirectory look-up of /boot/directory1\n");
764 dumpDir("/boot/directory1");
766 // Delete file first, then rmdir should work
767 r = yaffs_unlink("/boot/directory1/file with a long name");
768 r = yaffs_rmdir("/boot/directory1");
771 printf("\nDirectory look-up of /boot\n");
773 printf("\nDirectory look-up of /boot/directory1\n");
774 dumpDir("/boot/directory1");
777 fill_disk_and_delete("/boot",20,20);
779 printf("\nDirectory look-up of /boot\n");
783 yaffs_symlink("yyfile","/boot/slink");
785 yaffs_readlink("/boot/slink",str,100);
786 printf("symlink alias is %s\n",str);
791 printf("\nDirectory look-up of /boot\n");
793 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
794 dumpDirFollow("/boot");
795 printf("\nDirectory look-up of /boot/directory1\n");
796 dumpDir("/boot/directory1");
798 h = yaffs_open("/boot/slink",O_RDWR,0);
800 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
804 yaffs_unlink("/boot/slink");
807 printf("\nDirectory look-up of /boot\n");
812 yaffs_stat("/boot/yyfile",&ystat);
813 temp_mode = ystat.st_mode;
815 yaffs_chmod("/boot/yyfile",0x55555);
816 printf("\nDirectory look-up of /boot\n");
819 yaffs_chmod("/boot/yyfile",temp_mode);
820 printf("\nDirectory look-up of /boot\n");
823 // Permission checks...
824 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
825 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
826 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
828 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
829 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
830 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
832 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
833 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
834 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
836 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
837 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
838 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
840 yaffs_chmod("/boot/yyfile",temp_mode);
842 //create a zero-length file and unlink it (test for scan bug)
844 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
847 yaffs_unlink("/boot/zlf");
850 yaffs_DumpDevStruct("/boot");
852 fill_disk_and_delete("/boot",20,20);
854 yaffs_DumpDevStruct("/boot");
856 fill_files("/boot",1,10000,0);
857 fill_files("/boot",1,10000,5000);
858 fill_files("/boot",2,10000,0);
859 fill_files("/boot",2,10000,5000);
861 leave_unlinked_file("/data",20000,0);
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);
866 leave_unlinked_file("/data",20000,5000);
868 yaffs_DumpDevStruct("/boot");
869 yaffs_DumpDevStruct("/data");
877 int huge_directory_test_on_path(char *path)
897 struct yaffs_stat ystat;
903 // Create a large number of files
905 for(i = 0; i < 2000; i++)
907 sprintf(str,"%s/%d",path,i);
909 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
915 d = yaffs_opendir(path);
918 while((de = yaffs_readdir(d)) != NULL) {
919 if (total >lastTotal+100*9*1024||(i & 1023)==0){
920 printf("files = %d, total = %d\n",i, total);
924 sprintf(str,"%s/%s",path,de->d_name);
926 switch(s.st_mode & S_IFMT){
928 //printf("data file");
940 int yaffs_scan_test(const char *path)
945 void rename_over_test(const char *mountpt)
951 sprintf(a,"%s/a",mountpt);
952 sprintf(b,"%s/b",mountpt);
956 yaffs_mount(mountpt);
957 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
959 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, 0);
961 yaffs_rename(a,b); // rename over
962 yaffs_rename(b,a); // rename back again (not renaimng over)
963 yaffs_rename(a,b); // rename back again (not renaimng over)
966 yaffs_unmount(mountpt);
970 int resize_stress_test(const char *path)
985 sprintf(aname,"%s%s",path,"/a");
986 sprintf(bname,"%s%s",path,"/b");
988 memset(abuffer,'a',1000);
989 memset(bbuffer,'b',1000);
991 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
992 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
994 printf(" %s %d %s %d\n",aname,a,bname,b);
998 for(j = 0; j < 100; j++)
1000 yaffs_lseek(a,0,SEEK_END);
1003 for(i = 0; i <20000; i++)
1005 //r = yaffs_lseek(b,i,SEEK_SET);
1006 //r = yaffs_write(b,bbuffer,1000);
1011 int syz = yaffs_lseek(a,0,SEEK_END);
1014 if(syz < 0) syz = 0;
1015 yaffs_truncate(a,syz);
1021 r = yaffs_lseek(a,i * 500,SEEK_SET);
1022 r = yaffs_write(a,abuffer,1000);
1034 int resize_stress_test_no_grow_complex(const char *path,int iters)
1049 sprintf(aname,"%s%s",path,"/a");
1050 sprintf(bname,"%s%s",path,"/b");
1052 memset(abuffer,'a',1000);
1053 memset(bbuffer,'b',1000);
1055 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1056 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1058 printf(" %s %d %s %d\n",aname,a,bname,b);
1062 for(j = 0; j < iters; j++)
1064 yaffs_lseek(a,0,SEEK_END);
1067 for(i = 0; i <20000; i++)
1069 //r = yaffs_lseek(b,i,SEEK_SET);
1070 //r = yaffs_write(b,bbuffer,1000);
1075 int syz = yaffs_lseek(a,0,SEEK_END);
1081 if(syz < 0) syz = 0;
1082 yaffs_truncate(a,syz);
1083 syz = yaffs_lseek(a,0,SEEK_END);
1084 printf("shrink to %d\n",syz);
1092 r = yaffs_lseek(a,500,SEEK_END);
1093 r = yaffs_write(a,abuffer,1000);
1099 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1107 int resize_stress_test_no_grow(const char *path,int iters)
1122 sprintf(aname,"%s%s",path,"/a");
1123 sprintf(bname,"%s%s",path,"/b");
1125 memset(abuffer,'a',1000);
1126 memset(bbuffer,'b',1000);
1128 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1129 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1131 printf(" %s %d %s %d\n",aname,a,bname,b);
1135 for(j = 0; j < iters; j++)
1137 yaffs_lseek(a,0,SEEK_END);
1140 for(i = 0; i <20000; i++)
1142 //r = yaffs_lseek(b,i,SEEK_SET);
1143 //r = yaffs_write(b,bbuffer,1000);
1148 int syz = yaffs_lseek(a,0,SEEK_END);
1154 if(syz < 0) syz = 0;
1155 yaffs_truncate(a,syz);
1156 syz = yaffs_lseek(a,0,SEEK_END);
1157 printf("shrink to %d\n",syz);
1165 r = yaffs_lseek(a,-500,SEEK_END);
1166 r = yaffs_write(a,abuffer,1000);
1172 printf("file size is %d\n",yaffs_lseek(a,0,SEEK_END));
1180 int directory_rename_test(void)
1185 yaffs_mount("/ram");
1186 yaffs_mkdir("/ram/a",0);
1187 yaffs_mkdir("/ram/a/b",0);
1188 yaffs_mkdir("/ram/c",0);
1190 printf("\nDirectory look-up of /ram\n");
1193 dumpDir("/ram/a/b");
1195 printf("Do rename (should fail)\n");
1197 r = yaffs_rename("/ram/a","/ram/a/b/d");
1198 printf("\nDirectory look-up of /ram\n");
1201 dumpDir("/ram/a/b");
1203 printf("Do rename (should not fail)\n");
1205 r = yaffs_rename("/ram/c","/ram/a/b/d");
1206 printf("\nDirectory look-up of /ram\n");
1209 dumpDir("/ram/a/b");
1216 int cache_read_test(void)
1220 int sizeOfFiles = 500000;
1225 yaffs_mount("/boot");
1227 make_a_file("/boot/a",'a',sizeOfFiles);
1228 make_a_file("/boot/b",'b',sizeOfFiles);
1230 a = yaffs_open("/boot/a",O_RDONLY,0);
1231 b = yaffs_open("/boot/b",O_RDONLY,0);
1232 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1236 if (i > 100) i = 100;
1238 yaffs_read(a,buffer,i);
1239 yaffs_read(b,buffer,i);
1240 yaffs_write(c,buffer,i);
1241 } while(sizeOfFiles > 0);
1249 int cache_bypass_bug_test(void)
1251 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1252 // resulting in erroneous reads after writes.
1253 // This bug has been fixed.
1260 memset(buffer1,0,sizeof(buffer1));
1261 memset(buffer2,0,sizeof(buffer2));
1265 yaffs_mount("/boot");
1267 // Create a file of 2000 bytes.
1268 make_a_file("/boot/a",'X',2000);
1270 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1272 // Write a short sequence to the file.
1273 // This will go into the cache.
1274 yaffs_lseek(a,0,SEEK_SET);
1275 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1277 // Read a short sequence from the file.
1278 // This will come from the cache.
1279 yaffs_lseek(a,0,SEEK_SET);
1280 yaffs_read(a,buffer1,30);
1282 // Read a page size sequence from the file.
1283 yaffs_lseek(a,0,SEEK_SET);
1284 yaffs_read(a,buffer2,512);
1286 printf("buffer 1 %s\n",buffer1);
1287 printf("buffer 2 %s\n",buffer2);
1289 if(strncmp(buffer1,buffer2,20))
1291 printf("Cache bypass bug detected!!!!!\n");
1299 int free_space_check(void)
1304 yaffs_mount("/boot");
1305 fill_disk("/boot/",2);
1306 f = yaffs_freespace("/boot");
1308 printf("%d free when disk full\n",f);
1312 int truncate_test(void)
1322 yaffs_mount("/boot");
1324 yaffs_unlink("/boot/trunctest");
1326 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1328 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1330 yaffs_truncate(a,3);
1331 l= yaffs_lseek(a,0,SEEK_END);
1333 printf("truncated length is %d\n",l);
1335 yaffs_lseek(a,5,SEEK_SET);
1336 yaffs_write(a,"1",1);
1338 yaffs_lseek(a,0,SEEK_SET);
1340 r = yaffs_read(a,y,10);
1342 printf("read %d bytes:",r);
1344 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1356 void fill_disk_test(const char *mountpt)
1361 for(i = 0; i < 5; i++)
1363 yaffs_mount(mountpt);
1364 fill_disk_and_delete(mountpt,100,i+1);
1365 yaffs_unmount(mountpt);
1372 void lookup_test(const char *mountpt)
1382 struct yaffs_stat s;
1387 yaffs_mount(mountpt);
1389 d = yaffs_opendir(mountpt);
1393 printf("opendir failed\n");
1398 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1400 printf("unlinking %s\n",de->d_name);
1401 yaffs_unlink(de->d_name);
1404 printf("%d files deleted\n",i);
1408 for(i = 0; i < 2000; i++){
1409 sprintf(a,"%s/%d",mountpt,i);
1410 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1415 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1417 printf("%d %s\n",i,de->d_name);
1420 printf("%d files listed\n\n\n",i);
1427 for(i = 0; i < 2000; i++){
1428 sprintf(a,"%s/%d",mountpt,i);
1433 yaffs_unmount(mountpt);
1437 void link_test(const char *mountpt)
1449 sprintf(a,"%s/aaa",mountpt);
1450 sprintf(b,"%s/bbb",mountpt);
1451 sprintf(c,"%s/ccc",mountpt);
1455 yaffs_mount(mountpt);
1458 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1459 for(i = 0; i < 100; i++)
1460 yaffs_write(h,a,100);
1473 yaffs_unmount(mountpt);
1474 yaffs_mount(mountpt);
1476 printf("link test done\n");
1480 void freespace_test(const char *mountpt)
1491 sprintf(a,"%s/aaa",mountpt);
1495 yaffs_mount(mountpt);
1497 f0 = yaffs_freespace(mountpt);
1499 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1501 for(i = 0; i < 100; i++)
1502 yaffs_write(h,a,100);
1506 f1 = yaffs_freespace(mountpt);
1510 f2 = yaffs_freespace(mountpt);
1513 yaffs_unmount(mountpt);
1514 yaffs_mount(mountpt);
1516 f3 = yaffs_freespace(mountpt);
1518 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1523 void simple_rw_test(const char *mountpt)
1532 sprintf(a,"%s/aaa",mountpt);
1536 yaffs_mount(mountpt);
1540 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1542 for(i = 100000;i < 200000; i++){
1543 result = yaffs_write(h,&i,sizeof(i));
1547 printf("write error\n");
1554 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1557 yaffs_lseek(h,0,SEEK_SET);
1559 for(i = 100000; i < 200000; i++){
1560 result = yaffs_read(h,&x,sizeof(x));
1562 if(result != 4 || x != i){
1563 printf("read error %d %x %x\n",i,result,x);
1567 printf("Simple rw test passed\n");
1574 void scan_deleted_files_test(const char *mountpt)
1586 sprintf(sub,"%s/sdir",mountpt);
1589 for(j = 0; j < 10; j++)
1591 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1592 yaffs_mount(mountpt);
1596 p = (j & 0) ? mountpt: sub;
1598 for(i = 0; i < 100; i++)
1600 sprintf(fn,"%s/%d",p,i);
1604 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1605 for(k = 0; k < 1000; k++)
1606 yaffs_write(h,fn,100);
1613 for(i = 0; i < 10; i++)
1615 sprintf(fn,"%s/%d",p,i);
1623 yaffs_unmount(mountpt);
1632 void write_10k(int h)
1635 const char *s="0123456789";
1636 for(i = 0; i < 1000; i++)
1637 yaffs_write(h,s,10);
1640 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1646 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1648 for(i = 0; i < 100000; i+= 10000)
1653 offs = yaffs_lseek(h1,0,SEEK_CUR);
1656 printf("Could not write file\n");
1660 for(i = 0; i < 100000; i+= 10000)
1665 offs = yaffs_lseek(h1,0,SEEK_CUR);
1668 printf("Could not write file\n");
1672 yaffs_unlink(fdel1);
1677 void verify_200k_file(const char *fn)
1682 const char *s="0123456789";
1685 h1 = yaffs_open(fn, O_RDONLY, 0);
1687 for(i = 0; i < 200000 && errCount < 10; i+= 10)
1689 yaffs_read(h1,x,10);
1690 if(strncmp(x,s,10) != 0)
1692 printf("File %s verification failed at %d\n",fn,i);
1697 printf("Too many errors... aborted\n");
1704 void check_resize_gc_bug(const char *mountpt)
1713 sprintf(a,"%s/a",mountpt);
1714 sprintf(b,"%s/b",mountpt);
1715 sprintf(c,"%s/c",mountpt);
1721 yaffs_mount(mountpt);
1725 for(i = 0; i < 50; i++)
1727 printf("A\n");write_200k_file(a,"",c);
1728 printf("B\n");verify_200k_file(a);
1729 printf("C\n");write_200k_file(b,a,c);
1730 printf("D\n");verify_200k_file(b);
1731 yaffs_unmount(mountpt);
1732 yaffs_mount(mountpt);
1733 printf("E\n");verify_200k_file(a);
1734 printf("F\n");verify_200k_file(b);
1740 void multi_mount_test(const char *mountpt,int nmounts)
1750 sprintf(a,"%s/a",mountpt);
1754 for(i = 0; i < nmounts; i++){
1760 static char xx[1000];
1762 printf("############### Iteration %d Start\n",i);
1763 if(1 || i == 0 || i == 5)
1764 yaffs_mount(mountpt);
1766 dump_directory_tree(mountpt);
1771 sprintf(xx,"%s/0",a);
1772 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1774 sprintf(xx,"%s/1",a);
1775 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1777 for(j = 0; j < 200; j++){
1778 yaffs_write(h0,xx,1000);
1779 yaffs_write(h1,xx,1000);
1782 len0 = yaffs_lseek(h0,0,SEEK_END);
1783 len1 = yaffs_lseek(h1,0,SEEK_END);
1785 yaffs_lseek(h0,0,SEEK_SET);
1786 yaffs_lseek(h1,0,SEEK_SET);
1788 for(j = 0; j < 200; j++){
1789 yaffs_read(h0,xx,1000);
1790 yaffs_read(h1,xx,1000);
1794 yaffs_truncate(h0,0);
1798 printf("########### %d\n",i);
1799 dump_directory_tree(mountpt);
1801 if(1 || i == 4 || i == nmounts -1)
1802 yaffs_unmount(mountpt);
1807 void small_mount_test(const char *mountpt,int nmounts)
1823 sprintf(a,"%s/a",mountpt);
1829 for(i = 0; i < nmounts; i++){
1831 static char xx[1000];
1833 printf("############### Iteration %d Start\n",i);
1834 if(1 || i == 0 || i == 5)
1835 yaffs_mount(mountpt);
1837 dump_directory_tree(mountpt);
1841 sprintf(xx,"%s/0",a);
1844 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1845 for(j = 0; j < 130; j++)
1846 yaffs_write(h0,xx,1000);
1850 h0 = yaffs_open(xx,O_RDONLY,0);
1852 sprintf(xx,"%s/1",a);
1853 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1855 while((nread = yaffs_read(h0,xx,1000)) > 0)
1856 yaffs_write(h1,xx,nread);
1859 len0 = yaffs_lseek(h0,0,SEEK_END);
1860 len1 = yaffs_lseek(h1,0,SEEK_END);
1862 yaffs_lseek(h0,0,SEEK_SET);
1863 yaffs_lseek(h1,0,SEEK_SET);
1865 for(j = 0; j < 200; j++){
1866 yaffs_read(h0,xx,1000);
1867 yaffs_read(h1,xx,1000);
1873 printf("########### %d\n",i);
1874 dump_directory_tree(mountpt);
1876 if(1 || i == 4 || i == nmounts -1)
1877 yaffs_unmount(mountpt);
1884 void small_overwrite_test(const char *mountpt,int nmounts)
1900 sprintf(a,"%s/a",mountpt);
1906 for(i = 0; i < nmounts; i++){
1908 static char xx[8000];
1910 printf("############### Iteration %d Start\n",i);
1912 yaffs_mount(mountpt);
1914 dump_directory_tree(mountpt);
1918 sprintf(xx,"%s/0",a);
1919 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1920 sprintf(xx,"%s/1",a);
1921 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
1923 for(j = 0; j < 1000000; j+=1000){
1924 yaffs_truncate(h0,j);
1925 yaffs_lseek(h0,j,SEEK_SET);
1926 yaffs_write(h0,xx,7000);
1927 yaffs_write(h1,xx,7000);
1935 printf("########### %d\n",i);
1936 dump_directory_tree(mountpt);
1939 yaffs_unmount(mountpt);
1944 void yaffs_touch(const char *fn)
1946 yaffs_chmod(fn, S_IREAD | S_IWRITE);
1949 void checkpoint_fill_test(const char *mountpt,int nmounts)
1960 sprintf(a,"%s/a",mountpt);
1967 for(i = 0; i < nmounts; i++){
1968 printf("############### Iteration %d Start\n",i);
1969 yaffs_mount(mountpt);
1970 dump_directory_tree(mountpt);
1973 sprintf(b,"%s/zz",a);
1975 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
1978 while(yaffs_write(h,c,50) == 50){}
1982 for(j = 0; j < 2; j++){
1983 printf("touch %d\n",j);
1985 yaffs_unmount(mountpt);
1986 yaffs_mount(mountpt);
1989 dump_directory_tree(mountpt);
1990 yaffs_unmount(mountpt);
1995 int make_file2(const char *name1, const char *name2,int syz)
2005 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2007 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2009 while(syz > 0 && n > 0){
2010 i = (syz > 2500) ? 2500 : syz;
2011 n = yaffs_write(h1,xx,i);
2012 n = yaffs_write(h2,xx,i);
2021 extern void SetCheckpointReservedBlocks(int n);
2023 void checkpoint_upgrade_test(const char *mountpt,int nmounts)
2035 sprintf(a,"%s/a",mountpt);
2040 printf("Create start condition\n");
2042 SetCheckpointReservedBlocks(0);
2043 yaffs_mount(mountpt);
2045 sprintf(b,"%s/zz",a);
2046 sprintf(c,"%s/xx",a);
2047 make_file2(b,c,2000000);
2048 sprintf(d,"%s/aa",a);
2049 make_file2(d,NULL,500000000);
2050 dump_directory_tree(mountpt);
2052 printf("Umount/mount attempt full\n");
2053 yaffs_unmount(mountpt);
2055 SetCheckpointReservedBlocks(10);
2056 yaffs_mount(mountpt);
2058 printf("unlink small file\n");
2060 dump_directory_tree(mountpt);
2062 printf("Umount/mount attempt\n");
2063 yaffs_unmount(mountpt);
2064 yaffs_mount(mountpt);
2066 for(j = 0; j < 500; j++){
2067 printf("***** touch %d\n",j);
2068 dump_directory_tree(mountpt);
2070 yaffs_unmount(mountpt);
2071 yaffs_mount(mountpt);
2074 for(j = 0; j < 500; j++){
2075 printf("***** touch %d\n",j);
2076 dump_directory_tree(mountpt);
2078 yaffs_unmount(mountpt);
2079 yaffs_mount(mountpt);
2083 void huge_array_test(const char *mountpt,int n)
2095 sprintf(a,"mount point %s",mountpt);
2101 yaffs_mount(mountpt);
2106 printf("\n\n START run\n\n");
2107 while(yaffs_freespace(mountpt) > 25000000){
2108 sprintf(a,"%s/file%d",mountpt,fnum);
2110 printf("create file %s\n",a);
2111 create_file_of_size(a,10000000);
2112 printf("verifying file %s\n",a);
2113 verify_file_of_size(a,10000000);
2116 printf("\n\n verification/deletion\n\n");
2118 for(i = 0; i < fnum; i++){
2119 sprintf(a,"%s/file%d",mountpt,i);
2120 printf("verifying file %s\n",a);
2121 verify_file_of_size(a,10000000);
2122 printf("deleting file %s\n",a);
2125 printf("\n\n done \n\n");
2132 void random_write(int h)
2134 static char buffer[12000];
2137 n = random() & 0x1FFF;
2138 yaffs_write(h,buffer,n);
2141 void random_seek(int h)
2144 n = random() & 0xFFFFF;
2145 yaffs_lseek(h,n,SEEK_SET);
2148 void random_truncate(int h, char * name)
2152 n = random() & 0xFFFFF;
2153 flen = yaffs_lseek(h,0,SEEK_END);
2156 yaffs_truncate(name,n);
2157 yaffs_lseek(h,n,SEEK_SET);
2161 #define NSMALLFILES 10
2162 void random_small_file_test(const char *mountpt,int iterations)
2165 char a[NSMALLFILES][50];
2178 yaffs_mount(mountpt);
2180 for(i = 0; i < NSMALLFILES; i++){
2185 for(n = 0; n < iterations; n++){
2187 for(i = 0; i < NSMALLFILES; i++) {
2190 if(strlen(a[i]) == 0){
2191 sprintf(a[i],"%s/%dx%d",mountpt,n,i);
2192 h[i] = yaffs_open(a,O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2196 printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
2206 random_truncate(h[i],a[i]);
2209 case 5: random_seek(h[i]);
2226 for(i = 0; i < NSMALLFILES; i++)
2229 yaffs_unmount(mountpt);
2234 int main(int argc, char *argv[])
2236 //return long_test(argc,argv);
2238 //return cache_read_test();
2240 //resize_stress_test_no_grow("/flash",20);
2242 //huge_directory_test_on_path("/ram2k");
2244 //yaffs_backward_scan_test("/flash/flash");
2245 // yaffs_device_flush_test("/flash/flash");
2248 //scan_pattern_test("/flash",10000,10);
2249 //short_scan_test("/flash/flash",40000,200);
2250 //small_mount_test("/flash/flash",1000);
2251 //small_overwrite_test("/flash/flash",1000);
2252 //checkpoint_fill_test("/flash/flash",20);
2253 random_small_file_test("/flash/flash",10000);
2254 // huge_array_test("/flash/flash",10);
2259 //long_test_on_path("/ram2k");
2260 // long_test_on_path("/flash");
2261 //simple_rw_test("/flash/flash");
2262 //fill_disk_test("/flash/flash");
2263 // rename_over_test("/flash");
2264 //lookup_test("/flash");
2265 //freespace_test("/flash/flash");
2267 //link_test("/flash/flash");
2272 // cache_bypass_bug_test();
2274 //free_space_check();
2276 //check_resize_gc_bug("/flash");