2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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.
23 #include "yaffs_guts.h" /* Only for dumping device innards */
25 extern int yaffs_trace_mask;
27 void dumpDir(const char *dname);
29 void copy_in_a_file(const char *yaffsName,const 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(const 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 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
186 sprintf(xx,"%s %8d",fn,iteration);
188 result = yaffs_write(h,xx,n);
190 printf("Wrote %d, should have been %d. syze is %d\n",result,n,syze);
199 void verify_file_of_size(const char *fn,int syze)
208 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
210 h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
212 while (iterations > 0)
214 sprintf(xx,"%s %8d",fn,iterations);
217 result = yaffs_read(h,yy,l);
221 printf("=====>>>>> verification of file %s failed near position %lld\n",fn,(long long)yaffs_lseek(h,0,SEEK_CUR));
228 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_ftruncate(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);
295 void null_name_test(const char *path)
303 sprintf(fn,"%s",path);
305 h = yaffs_open(fn,O_CREAT| O_TRUNC| O_RDWR, 0666);
314 void yaffs_device_flush_test(const char *path)
324 do_some_file_stuff(path);
326 // Open and add some data to a few files
327 for(i = 0; i < 10; i++) {
329 sprintf(fn,"%s/ff%d",path,i);
331 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
332 yaffs_write(h,xxzz,2000);
333 yaffs_write(h,xxzz,2000);
342 void short_scan_test(const char *path, int fsize, int niterations)
347 sprintf(fn,"%s/%s",path,"f1");
350 for(i = 0; i < niterations; i++)
352 printf("\n*****************\nIteration %d\n",i);
354 printf("\nmount: Directory look-up of %s\n",path);
356 make_a_file(fn,1,fsize);
363 void scan_pattern_test(const char *path, int fsize, int niterations)
370 sprintf(fn[0],"%s/%s",path,"f0");
371 sprintf(fn[1],"%s/%s",path,"f1");
372 sprintf(fn[2],"%s/%s",path,"f2");
376 for(i = 0; i < niterations; i++)
378 printf("\n*****************\nIteration %d\n",i);
380 printf("\nmount: Directory look-up of %s\n",path);
382 for(j = 0; j < 3; j++)
384 result = dump_file_data(fn[j]);
385 result = check_pattern_file(fn[j]);
386 make_pattern_file(fn[j],fsize);
387 result = dump_file_data(fn[j]);
388 result = check_pattern_file(fn[j]);
394 void fill_disk(const char *path,int nfiles)
404 for(n = 0; n < nfiles; n++)
406 sprintf(str,"%s/%d",path,n);
408 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
410 printf("writing file %s handle %d ",str, h);
412 while ((result = yaffs_write(h,xx,600)) == 600)
414 f = yaffs_freespace(path);
416 result = yaffs_close(h);
417 printf(" close %d\n",result);
421 void fill_disk_and_delete(const char *path, int nfiles, int ncycles)
427 for(i = 0; i < ncycles; i++)
429 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
430 fill_disk(path,nfiles);
432 for(j = 0; j < nfiles; j++)
434 sprintf(str,"%s/%d",path,j);
435 result = yaffs_unlink(str);
436 printf("unlinking file %s, result %d\n",str,result);
442 void fill_files(const char *path,int flags, int maxIterations,int siz)
452 sprintf(str,"%s/%d",path,i);
453 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
457 for(j = 0; j < siz; j++)
459 yaffs_write(h,str,1);
468 } while(h >= 0 && i < maxIterations);
474 sprintf(str,"%s/%d",path,i);
475 printf("unlink %s\n",str);
477 } while(yaffs_unlink(str) >= 0);
481 void leave_unlinked_file(const char *path,int maxIterations,int siz)
490 sprintf(str,"%s/%d",path,i);
491 printf("create %s\n",str);
492 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
498 } while(h < 0 && i < maxIterations);
502 for(i = 0; i < siz; i++)
504 yaffs_write(h,str,1);
508 printf("Leaving file %s open\n",str);
512 void dumpDirFollow(const char *dname)
515 struct yaffs_dirent *de;
519 d = yaffs_opendir(dname);
523 printf("opendir failed\n");
527 while((de = yaffs_readdir(d)) != NULL)
529 sprintf(str,"%s/%s",dname,de->d_name);
533 printf("%s ino %lld length %d mode %X ",de->d_name,(int)s.st_ino,s.st_size,s.st_mode);
534 switch(s.st_mode & S_IFMT)
536 case S_IFREG: printf("data file"); break;
537 case S_IFDIR: printf("directory"); break;
538 case S_IFLNK: printf("symlink -->");
539 if(yaffs_readlink(str,str,100) < 0)
542 printf("\"%s\"",str);
544 default: printf("unknown"); break;
554 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
559 void dump_directory_tree_worker(const char *dname,int recursive)
562 struct yaffs_dirent *de;
566 d = yaffs_opendir(dname);
570 printf("opendir failed\n");
574 while((de = yaffs_readdir(d)) != NULL)
576 sprintf(str,"%s/%s",dname,de->d_name);
580 printf("%s inode %d obj %x length %lld mode %X ",
581 str,s.st_ino,de->d_dont_use, s.st_size,s.st_mode);
582 switch(s.st_mode & S_IFMT)
584 case S_IFREG: printf("data file"); break;
585 case S_IFDIR: printf("directory"); break;
586 case S_IFLNK: printf("symlink -->");
587 if(yaffs_readlink(str,str,100) < 0)
590 printf("\"%s\"",str);
592 default: printf("unknown"); break;
597 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
598 dump_directory_tree_worker(str,1);
607 static void dump_directory_tree(const char *dname)
609 dump_directory_tree_worker(dname,1);
611 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
614 void dump_directory_tree_worker_fd(const char *dname,int recursive)
617 struct yaffs_dirent *de;
621 h = yaffs_open(dname, O_RDONLY, 0);
625 printf("open of dir failed\n");
629 printf("using fd %d\n", h);
631 while((de = yaffs_readdir_fd(h)) != NULL)
633 sprintf(str,"%s/%s",dname,de->d_name);
637 printf("%s inode %d obj %x length %lld mode %X ",
638 str,s.st_ino,de->d_dont_use, s.st_size,s.st_mode);
639 switch(s.st_mode & S_IFMT)
641 case S_IFREG: printf("data file"); break;
642 case S_IFDIR: printf("directory"); break;
643 case S_IFLNK: printf("symlink -->");
644 if(yaffs_readlink(str,str,100) < 0)
647 printf("\"%s\"",str);
649 default: printf("unknown"); break;
654 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
655 dump_directory_tree_worker_fd(str,1);
664 static void dump_directory_tree_fd(const char *dname)
666 dump_directory_tree_worker_fd(dname,1);
668 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
671 void dumpDir(const char *dname)
672 { dump_directory_tree_worker(dname,0);
674 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
678 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
682 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
684 fd = yaffs_open(path,tflags,0);
686 if((fd >= 0) != (expectedResult > 0))
688 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
692 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
701 int long_test(int argc, char *argv[])
712 struct yaffs_stat ystat;
716 yaffs_mount("/boot");
717 yaffs_mount("/data");
718 yaffs_mount("/flash");
721 printf("\nDirectory look-up of /boot\n");
723 printf("\nDirectory look-up of /data\n");
725 printf("\nDirectory look-up of /flash\n");
728 //leave_unlinked_file("/flash",20000,0);
729 //leave_unlinked_file("/data",20000,0);
731 leave_unlinked_file("/ram",20,0);
734 f = yaffs_open("/boot/b1", O_RDONLY,0);
736 printf("open /boot/b1 readonly, f=%d\n",f);
738 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
740 printf("open /boot/b1 O_CREAT, f=%d\n",f);
743 r = yaffs_write(f,"hello",1);
744 printf("write %d attempted to write to a read-only file\n",r);
748 printf("close %d\n",r);
750 f = yaffs_open("/boot/b1", O_RDWR,0);
752 printf("open /boot/b1 O_RDWR,f=%d\n",f);
755 r = yaffs_write(f,"hello",2);
756 printf("write %d attempted to write to a writeable file\n",r);
757 r = yaffs_write(f,"world",3);
758 printf("write %d attempted to write to a writeable file\n",r);
760 r= yaffs_lseek(f,0,SEEK_END);
761 printf("seek end %d\n",r);
763 r = yaffs_read(f,buffer,10);
764 printf("read %d \"%s\"\n",r,buffer);
765 r= yaffs_lseek(f,0,SEEK_SET);
766 printf("seek set %d\n",r);
768 r = yaffs_read(f,buffer,10);
769 printf("read %d \"%s\"\n",r,buffer);
771 r = yaffs_read(f,buffer,10);
772 printf("read %d \"%s\"\n",r,buffer);
774 // Check values reading at end.
775 // A read past end of file should return 0 for 0 bytes read.
777 r= yaffs_lseek(f,0,SEEK_END);
778 r = yaffs_read(f,buffer,10);
779 printf("read at end returned %d\n",r);
780 r= yaffs_lseek(f,500,SEEK_END);
781 r = yaffs_read(f,buffer,10);
782 printf("read past end returned %d\n",r);
786 printf("close %d\n",r);
788 copy_in_a_file("/boot/yyfile","xxx");
790 // Create a file with a long name
792 copy_in_a_file("/boot/file with a long name","xxx");
795 printf("\nDirectory look-up of /boot\n");
799 r = yaffs_lstat("/boot/file with a long name",&ystat);
803 r = yaffs_rename("/boot/file with a long name","/boot/r1");
805 printf("\nDirectory look-up of /boot\n");
809 r = yaffs_unlink("/boot/r1");
811 printf("\nDirectory look-up of /boot\n");
816 r = yaffs_mkdir("/boot/directory1",0);
818 printf("\nDirectory look-up of /boot\n");
820 printf("\nDirectory look-up of /boot/directory1\n");
821 dumpDir("/boot/directory1");
823 // add a file to the directory
824 copy_in_a_file("/boot/directory1/file with a long name","xxx");
826 printf("\nDirectory look-up of /boot\n");
828 printf("\nDirectory look-up of /boot/directory1\n");
829 dumpDir("/boot/directory1");
831 // Attempt to delete directory (should fail)
833 r = yaffs_rmdir("/boot/directory1");
835 printf("\nDirectory look-up of /boot\n");
837 printf("\nDirectory look-up of /boot/directory1\n");
838 dumpDir("/boot/directory1");
840 // Delete file first, then rmdir should work
841 r = yaffs_unlink("/boot/directory1/file with a long name");
842 r = yaffs_rmdir("/boot/directory1");
845 printf("\nDirectory look-up of /boot\n");
847 printf("\nDirectory look-up of /boot/directory1\n");
848 dumpDir("/boot/directory1");
851 fill_disk_and_delete("/boot",20,20);
853 printf("\nDirectory look-up of /boot\n");
857 yaffs_symlink("yyfile","/boot/slink");
859 yaffs_readlink("/boot/slink",str,100);
860 printf("symlink alias is %s\n",str);
865 printf("\nDirectory look-up of /boot\n");
867 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
868 dumpDirFollow("/boot");
869 printf("\nDirectory look-up of /boot/directory1\n");
870 dumpDir("/boot/directory1");
872 h = yaffs_open("/boot/slink",O_RDWR,0);
874 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
878 yaffs_unlink("/boot/slink");
881 printf("\nDirectory look-up of /boot\n");
886 yaffs_lstat("/boot/yyfile",&ystat);
887 temp_mode = ystat.st_mode;
889 yaffs_chmod("/boot/yyfile",0x55555);
890 printf("\nDirectory look-up of /boot\n");
893 yaffs_chmod("/boot/yyfile",temp_mode);
894 printf("\nDirectory look-up of /boot\n");
897 // Permission checks...
898 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
899 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
900 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
902 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
903 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
904 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
906 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
907 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
908 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
910 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
911 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
912 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
914 yaffs_chmod("/boot/yyfile",temp_mode);
916 //create a zero-length file and unlink it (test for scan bug)
918 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
921 yaffs_unlink("/boot/zlf");
924 yaffs_dump_dev("/boot");
926 fill_disk_and_delete("/boot",20,20);
928 yaffs_dump_dev("/boot");
930 fill_files("/boot",1,10000,0);
931 fill_files("/boot",1,10000,5000);
932 fill_files("/boot",2,10000,0);
933 fill_files("/boot",2,10000,5000);
935 leave_unlinked_file("/data",20000,0);
936 leave_unlinked_file("/data",20000,5000);
937 leave_unlinked_file("/data",20000,5000);
938 leave_unlinked_file("/data",20000,5000);
939 leave_unlinked_file("/data",20000,5000);
940 leave_unlinked_file("/data",20000,5000);
942 yaffs_dump_dev("/boot");
943 yaffs_dump_dev("/data");
951 int huge_directory_test_on_path(char *path)
955 struct yaffs_dirent *de;
971 // Create a large number of files
973 for(i = 0; i < 2000; i++)
975 sprintf(str,"%s/%d",path,i);
977 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
983 d = yaffs_opendir(path);
986 while((de = yaffs_readdir(d)) != NULL) {
987 if (total >lastTotal+100*9*1024||(i & 1023)==0){
988 printf("files = %d, total = %d\n",i, total);
992 sprintf(str,"%s/%s",path,de->d_name);
994 switch(s.st_mode & S_IFMT){
996 //printf("data file");
1008 int yaffs_scan_test(const char *path)
1014 void rename_over_test(const char *mountpt)
1021 sprintf(a,"%s/a",mountpt);
1022 sprintf(b,"%s/b",mountpt);
1023 sprintf(c,"%s/c",mountpt);
1027 yaffs_mount(mountpt);
1029 printf("Existing files\n");
1030 dumpDirFollow(mountpt);
1034 i = yaffs_open(c,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1035 printf("File c handle is %d\n",i);
1037 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1039 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1041 yaffs_rename(a,b); // rename over
1042 yaffs_rename(b,a); // rename back again (not renaimng over)
1043 yaffs_rename(a,b); // rename back again (not renaimng over)
1046 yaffs_unmount(mountpt);
1051 int resize_stress_test(const char *path)
1066 sprintf(aname,"%s%s",path,"/a");
1067 sprintf(bname,"%s%s",path,"/b");
1069 memset(abuffer,'a',1000);
1070 memset(bbuffer,'b',1000);
1072 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1073 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1075 printf(" %s %d %s %d\n",aname,a,bname,b);
1079 for(j = 0; j < 100; j++)
1081 yaffs_lseek(a,0,SEEK_END);
1084 for(i = 0; i <20000; i++)
1086 //r = yaffs_lseek(b,i,SEEK_SET);
1087 //r = yaffs_write(b,bbuffer,1000);
1092 int syz = yaffs_lseek(a,0,SEEK_END);
1095 if(syz < 0) syz = 0;
1096 yaffs_ftruncate(a,syz);
1102 r = yaffs_lseek(a,i * 500,SEEK_SET);
1103 r = yaffs_write(a,abuffer,1000);
1115 int overwrite_test(const char *path)
1127 sprintf(aname,"%s%s",path,"/a");
1128 sprintf(bname,"%s%s",path,"/b");
1130 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1131 for(j= 0; j < 500; j++){
1132 yaffs_write(b,bname,100);
1133 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1134 for(i = 0; i < rand() % 20000; i++)
1135 yaffs_write(a,&a,sizeof(a));
1144 int root_perm_remount(const char *path)
1146 struct yaffs_stat s;
1152 yaffs_lstat(path,&s);
1153 printf("root perms after mount %x\n",s.st_mode);
1155 yaffs_chmod(path, 0777);
1157 yaffs_lstat(path,&s);
1158 printf("root perms after setting to 0777 is %x\n",s.st_mode);
1160 yaffs_unmount(path);
1167 int resize_stress_test_no_grow_complex(const char *path,int iters)
1183 sprintf(aname,"%s%s",path,"/a");
1184 sprintf(bname,"%s%s",path,"/b");
1186 memset(abuffer,'a',1000);
1187 memset(bbuffer,'b',1000);
1189 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1190 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1192 printf(" %s %d %s %d\n",aname,a,bname,b);
1196 for(j = 0; j < iters; j++)
1198 yaffs_lseek(a,0,SEEK_END);
1201 for(i = 0; i <20000; i++)
1203 //r = yaffs_lseek(b,i,SEEK_SET);
1204 //r = yaffs_write(b,bbuffer,1000);
1209 int syz = yaffs_lseek(a,0,SEEK_END);
1215 if(syz < 0) syz = 0;
1216 yaffs_ftruncate(a,syz);
1217 syz = yaffs_lseek(a,0,SEEK_END);
1218 printf("shrink to %d\n",syz);
1226 r = yaffs_lseek(a,500,SEEK_END);
1227 r = yaffs_write(a,abuffer,1000);
1234 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1242 int resize_stress_test_no_grow(const char *path,int iters)
1257 sprintf(aname,"%s%s",path,"/a");
1258 sprintf(bname,"%s%s",path,"/b");
1260 memset(abuffer,'a',1000);
1261 memset(bbuffer,'b',1000);
1263 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1264 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1266 printf(" %s %d %s %d\n",aname,a,bname,b);
1270 for(j = 0; j < iters; j++)
1272 yaffs_lseek(a,0,SEEK_END);
1275 for(i = 0; i <20000; i++)
1277 //r = yaffs_lseek(b,i,SEEK_SET);
1278 //r = yaffs_write(b,bbuffer,1000);
1283 int syz = yaffs_lseek(a,0,SEEK_END);
1289 if(syz < 0) syz = 0;
1290 yaffs_ftruncate(a,syz);
1291 syz = yaffs_lseek(a,0,SEEK_END);
1292 printf("shrink to %d\n",syz);
1300 r = yaffs_lseek(a,-500,SEEK_END);
1301 r = yaffs_write(a,abuffer,1000);
1307 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1315 int directory_rename_test(void)
1320 yaffs_mount("/ram");
1321 yaffs_mkdir("/ram/a",0);
1322 yaffs_mkdir("/ram/a/b",0);
1323 yaffs_mkdir("/ram/c",0);
1325 printf("\nDirectory look-up of /ram\n");
1328 dumpDir("/ram/a/b");
1330 printf("Do rename (should fail)\n");
1332 r = yaffs_rename("/ram/a","/ram/a/b/d");
1333 printf("\nDirectory look-up of /ram\n");
1336 dumpDir("/ram/a/b");
1338 printf("Do rename (should not fail)\n");
1340 r = yaffs_rename("/ram/c","/ram/a/b/d");
1341 printf("\nDirectory look-up of /ram\n");
1344 dumpDir("/ram/a/b");
1351 int cache_read_test(void)
1355 int sizeOfFiles = 500000;
1360 yaffs_mount("/boot");
1362 make_a_file("/boot/a",'a',sizeOfFiles);
1363 make_a_file("/boot/b",'b',sizeOfFiles);
1365 a = yaffs_open("/boot/a",O_RDONLY,0);
1366 b = yaffs_open("/boot/b",O_RDONLY,0);
1367 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1371 if (i > 100) i = 100;
1373 yaffs_read(a,buffer,i);
1374 yaffs_read(b,buffer,i);
1375 yaffs_write(c,buffer,i);
1376 } while(sizeOfFiles > 0);
1384 int cache_bypass_bug_test(void)
1386 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1387 // resulting in erroneous reads after writes.
1388 // This bug has been fixed.
1394 memset(buffer1,0,sizeof(buffer1));
1395 memset(buffer2,0,sizeof(buffer2));
1399 yaffs_mount("/boot");
1401 // Create a file of 2000 bytes.
1402 make_a_file("/boot/a",'X',2000);
1404 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1406 // Write a short sequence to the file.
1407 // This will go into the cache.
1408 yaffs_lseek(a,0,SEEK_SET);
1409 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1411 // Read a short sequence from the file.
1412 // This will come from the cache.
1413 yaffs_lseek(a,0,SEEK_SET);
1414 yaffs_read(a,buffer1,30);
1416 // Read a page size sequence from the file.
1417 yaffs_lseek(a,0,SEEK_SET);
1418 yaffs_read(a,buffer2,512);
1420 printf("buffer 1 %s\n",buffer1);
1421 printf("buffer 2 %s\n",buffer2);
1423 if(strncmp(buffer1,buffer2,20))
1425 printf("Cache bypass bug detected!!!!!\n");
1433 int free_space_check(void)
1438 yaffs_mount("/boot");
1439 fill_disk("/boot/",2);
1440 f = yaffs_freespace("/boot");
1442 printf("%d free when disk full\n",f);
1446 int truncate_test(void)
1456 yaffs_mount("/boot");
1458 yaffs_unlink("/boot/trunctest");
1460 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1462 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1464 yaffs_ftruncate(a,3);
1465 l= yaffs_lseek(a,0,SEEK_END);
1467 printf("truncated length is %d\n",l);
1469 yaffs_lseek(a,5,SEEK_SET);
1470 yaffs_write(a,"1",1);
1472 yaffs_lseek(a,0,SEEK_SET);
1474 r = yaffs_read(a,y,10);
1476 printf("read %d bytes:",r);
1478 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1490 void fill_disk_test(const char *mountpt)
1495 for(i = 0; i < 5; i++)
1497 yaffs_mount(mountpt);
1498 fill_disk_and_delete(mountpt,100,i+1);
1499 yaffs_unmount(mountpt);
1505 void fill_files_test(const char *mountpt)
1510 for(i = 0; i < 5; i++)
1512 yaffs_mount(mountpt);
1513 fill_files(mountpt,2,3,100);
1514 yaffs_unmount(mountpt);
1519 void fill_empty_files_test(const char *mountpt)
1528 for(i = 0; i < 5; i++)
1530 yaffs_mount(mountpt);
1531 for(d = 0; result >= 0 && d < 1000; d++){
1532 sprintf(name,"%s/%d",mountpt,d);
1533 result= yaffs_mkdir(name,0);
1534 printf("creating directory %s result %d\n",name,result);
1536 for(f = 0; result >= 0 && f < 100; f++){
1537 sprintf(name,"%s/%d/%d",mountpt,d,f);
1538 result= yaffs_open(name,O_CREAT, 0);
1539 yaffs_close(result);
1540 printf("creating file %s result %d\n",name,result);
1543 yaffs_unmount(mountpt);
1548 void long_name_test(const char *mountpt)
1552 char fullName[1000];
1558 // Make a 256 byte name
1559 memset(name,0,sizeof(name));
1560 for(i = 0; i < 256; i++)
1561 name[i] = '0' + i % 10;
1563 sprintf(fullName,"%s/%s",mountpt,name);
1565 for(i = 0; i < 1; i++)
1567 yaffs_mount(mountpt);
1569 printf("Files at start\n");
1572 printf("Creating file %s\n",fullName);
1574 f = yaffs_open(fullName,O_CREAT | O_RDWR,0);
1577 printf("Result %d\n",f);
1582 printf("Deleting %s\n",fullName);
1583 result = yaffs_unlink(fullName);
1584 printf("Result %d\n",result);
1590 yaffs_unmount(mountpt);
1596 void lookup_test(const char *mountpt)
1604 struct yaffs_dirent *de;
1608 yaffs_mount(mountpt);
1610 d = yaffs_opendir(mountpt);
1614 printf("opendir failed\n");
1619 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1621 printf("unlinking %s\n",de->d_name);
1622 yaffs_unlink(de->d_name);
1625 printf("%d files deleted\n",i);
1629 for(i = 0; i < 2000; i++){
1630 sprintf(a,"%s/%d",mountpt,i);
1631 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1636 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1638 printf("%d %s\n",i,de->d_name);
1641 printf("%d files listed\n\n\n",i);
1648 for(i = 0; i < 2000; i++){
1649 sprintf(a,"%s/%d",mountpt,i);
1654 yaffs_unmount(mountpt);
1658 void link_test0(const char *mountpt)
1666 yaffs_mount(mountpt);
1669 sprintf(namea,"%s/a",mountpt);
1670 sprintf(nameb,"%s/b",mountpt);
1672 printf("mounted\n");
1675 yaffs_unlink(namea);
1676 printf("a unlinked\n");
1679 yaffs_unlink(nameb);
1680 printf("b unlinked\n");
1683 result = yaffs_open(namea,O_CREAT| O_RDWR,0666);
1684 yaffs_close(result);
1685 printf("a created\n");
1688 yaffs_link(namea,nameb);
1691 yaffs_unlink(namea);
1692 printf("a ulinked\n");
1694 yaffs_unlink(nameb);
1695 printf("b unlinked\n");
1698 yaffs_unmount(mountpt);
1702 void link_test1(const char *mountpt)
1710 sprintf(a,"%s/aaa",mountpt);
1711 sprintf(b,"%s/bbb",mountpt);
1712 sprintf(c,"%s/ccc",mountpt);
1716 yaffs_mount(mountpt);
1719 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1720 for(i = 0; i < 100; i++)
1721 yaffs_write(h,a,100);
1734 yaffs_unmount(mountpt);
1735 yaffs_mount(mountpt);
1737 printf("link test done\n");
1740 void handle_test(const char *mountpt)
1747 sprintf(a,"%s/aaa",mountpt);
1751 yaffs_mount(mountpt);
1753 for(cycle = 0; cycle < 5; cycle++){
1754 printf("Start cycle %d\n",cycle);
1757 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1758 printf("%d handle %d\n",i,h);
1768 yaffs_unmount(mountpt);
1771 void freespace_test(const char *mountpt)
1781 sprintf(a,"%s/aaa",mountpt);
1785 yaffs_mount(mountpt);
1787 f0 = yaffs_freespace(mountpt);
1789 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1791 for(i = 0; i < 100; i++)
1792 yaffs_write(h,a,100);
1796 f1 = yaffs_freespace(mountpt);
1800 f2 = yaffs_freespace(mountpt);
1803 yaffs_unmount(mountpt);
1804 yaffs_mount(mountpt);
1806 f3 = yaffs_freespace(mountpt);
1808 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1813 void simple_rw_test(const char *mountpt)
1822 sprintf(a,"%s/aaa",mountpt);
1826 yaffs_mount(mountpt);
1830 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1832 for(i = 100000;i < 200000; i++){
1833 result = yaffs_write(h,&i,sizeof(i));
1837 printf("write error\n");
1844 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1847 yaffs_lseek(h,0,SEEK_SET);
1849 for(i = 100000; i < 200000; i++){
1850 result = yaffs_read(h,&x,sizeof(x));
1852 if(result != 4 || x != i){
1853 printf("read error %d %x %x\n",i,result,x);
1857 printf("Simple rw test passed\n");
1864 void scan_deleted_files_test(const char *mountpt)
1876 sprintf(sub,"%s/sdir",mountpt);
1879 for(j = 0; j < 10; j++)
1881 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1882 yaffs_mount(mountpt);
1886 p = (j & 0) ? mountpt: sub;
1888 for(i = 0; i < 100; i++)
1890 sprintf(fn,"%s/%d",p,i);
1894 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1895 for(k = 0; k < 1000; k++)
1896 yaffs_write(h,fn,100);
1903 for(i = 0; i < 10; i++)
1905 sprintf(fn,"%s/%d",p,i);
1913 yaffs_unmount(mountpt);
1922 void write_10k(int h)
1925 const char *s="0123456789";
1926 for(i = 0; i < 1000; i++)
1927 yaffs_write(h,s,10);
1930 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1936 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1938 for(i = 0; i < 100000; i+= 10000)
1943 offs = yaffs_lseek(h1,0,SEEK_CUR);
1946 printf("Could not write file\n");
1950 for(i = 0; i < 100000; i+= 10000)
1955 offs = yaffs_lseek(h1,0,SEEK_CUR);
1958 printf("Could not write file\n");
1962 yaffs_unlink(fdel1);
1967 void verify_200k_file(const char *fn)
1972 const char *s="0123456789";
1975 h1 = yaffs_open(fn, O_RDONLY, 0);
1977 for(i = 0; i < 200000 && errCount < 10; i+= 10)
1979 yaffs_read(h1,x,10);
1980 if(strncmp(x,s,10) != 0)
1982 printf("File %s verification failed at %d\n",fn,i);
1987 printf("Too many errors... aborted\n");
1994 void check_resize_gc_bug(const char *mountpt)
2003 sprintf(a,"%s/a",mountpt);
2004 sprintf(b,"%s/b",mountpt);
2005 sprintf(c,"%s/c",mountpt);
2011 yaffs_mount(mountpt);
2015 for(i = 0; i < 50; i++)
2017 printf("A\n");write_200k_file(a,"",c);
2018 printf("B\n");verify_200k_file(a);
2019 printf("C\n");write_200k_file(b,a,c);
2020 printf("D\n");verify_200k_file(b);
2021 yaffs_unmount(mountpt);
2022 yaffs_mount(mountpt);
2023 printf("E\n");verify_200k_file(a);
2024 printf("F\n");verify_200k_file(b);
2030 void multi_mount_test(const char *mountpt,int nmounts)
2038 sprintf(a,"%s/a",mountpt);
2042 for(i = 0; i < nmounts; i++){
2048 static char xx[1000];
2050 printf("############### Iteration %d Start\n",i);
2051 if(1 || i == 0 || i == 5)
2052 yaffs_mount(mountpt);
2054 dump_directory_tree(mountpt);
2059 sprintf(xx,"%s/0",a);
2060 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2062 sprintf(xx,"%s/1",a);
2063 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2066 for(j = 0; j < 200; j++){
2067 yaffs_write(h0,xx,1000);
2068 yaffs_write(h1,xx,1000);
2071 while(yaffs_write(h0,xx,1000) > 0){
2073 yaffs_write(h1,xx,1000);
2076 len0 = yaffs_lseek(h0,0,SEEK_END);
2077 len1 = yaffs_lseek(h1,0,SEEK_END);
2079 yaffs_lseek(h0,0,SEEK_SET);
2080 yaffs_lseek(h1,0,SEEK_SET);
2082 for(j = 0; j < 200; j++){
2083 yaffs_read(h0,xx,1000);
2084 yaffs_read(h1,xx,1000);
2088 // yaffs_truncate(h0,0);
2092 printf("########### %d\n",i);
2093 dump_directory_tree(mountpt);
2095 if(1 || i == 4 || i == nmounts -1)
2096 yaffs_unmount(mountpt);
2101 void small_mount_test(const char *mountpt,int nmounts)
2115 sprintf(a,"%s/a",mountpt);
2121 for(i = 0; i < nmounts; i++){
2123 static char xx[1000];
2125 printf("############### Iteration %d Start\n",i);
2126 if(1 || i == 0 || i == 5)
2127 yaffs_mount(mountpt);
2129 dump_directory_tree(mountpt);
2133 sprintf(xx,"%s/0",a);
2136 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2137 for(j = 0; j < 130; j++)
2138 yaffs_write(h0,xx,1000);
2142 h0 = yaffs_open(xx,O_RDONLY,0);
2144 sprintf(xx,"%s/1",a);
2145 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2147 while((nread = yaffs_read(h0,xx,1000)) > 0)
2148 yaffs_write(h1,xx,nread);
2151 len0 = yaffs_lseek(h0,0,SEEK_END);
2152 len1 = yaffs_lseek(h1,0,SEEK_END);
2154 yaffs_lseek(h0,0,SEEK_SET);
2155 yaffs_lseek(h1,0,SEEK_SET);
2157 for(j = 0; j < 200; j++){
2158 yaffs_read(h0,xx,1000);
2159 yaffs_read(h1,xx,1000);
2165 printf("########### %d\n",i);
2166 dump_directory_tree(mountpt);
2168 if(1 || i == 4 || i == nmounts -1)
2169 yaffs_unmount(mountpt);
2176 void small_overwrite_test(const char *mountpt,int nmounts)
2187 sprintf(a,"%s/a",mountpt);
2193 for(i = 0; i < nmounts; i++){
2195 static char xx[8000];
2197 printf("############### Iteration %d Start\n",i);
2199 yaffs_mount(mountpt);
2201 dump_directory_tree(mountpt);
2205 sprintf(xx,"%s/0",a);
2206 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2207 sprintf(xx,"%s/1",a);
2208 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2210 for(j = 0; j < 1000000; j+=1000){
2211 yaffs_ftruncate(h0,j);
2212 yaffs_lseek(h0,j,SEEK_SET);
2213 yaffs_write(h0,xx,7000);
2214 yaffs_write(h1,xx,7000);
2222 printf("########### %d\n",i);
2223 dump_directory_tree(mountpt);
2226 yaffs_unmount(mountpt);
2231 void seek_overwrite_test(const char *mountpt,int nmounts)
2233 static char xx[5000];
2242 sprintf(a,"%s/f",mountpt);
2246 yaffs_mount(mountpt);
2249 for(i = 0; i < nmounts; i++){
2251 h0 = yaffs_open(a, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2253 for(j = 0; j < 100000; j++){
2254 yaffs_lseek(h0,0,SEEK_SET);
2255 yaffs_write(h0,xx,5000);
2256 yaffs_lseek(h0,0x100000,SEEK_SET);
2257 yaffs_write(h0,xx,5000);
2269 void yaffs_touch(const char *fn)
2271 yaffs_chmod(fn, S_IREAD | S_IWRITE);
2274 void checkpoint_fill_test(const char *mountpt,int nmounts)
2285 sprintf(a,"%s/a",mountpt);
2292 for(i = 0; i < nmounts; i++){
2293 printf("############### Iteration %d Start\n",i);
2294 yaffs_mount(mountpt);
2295 dump_directory_tree(mountpt);
2298 sprintf(b,"%s/zz",a);
2300 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
2303 while(yaffs_write(h,c,50) == 50){}
2307 for(j = 0; j < 2; j++){
2308 printf("touch %d\n",j);
2310 yaffs_unmount(mountpt);
2311 yaffs_mount(mountpt);
2314 dump_directory_tree(mountpt);
2315 yaffs_unmount(mountpt);
2320 int make_file2(const char *name1, const char *name2,int syz)
2330 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2332 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2334 while(syz > 0 && n > 0){
2335 i = (syz > 2500) ? 2500 : syz;
2336 n = yaffs_write(h1,xx,i);
2337 n = yaffs_write(h2,xx,i);
2346 extern void SetCheckpointReservedBlocks(int n);
2348 void checkpoint_upgrade_test(const char *mountpt,int nmounts)
2358 sprintf(a,"%s/a",mountpt);
2363 printf("Create start condition\n");
2365 yaffs_mount(mountpt);
2367 sprintf(b,"%s/zz",a);
2368 sprintf(c,"%s/xx",a);
2369 make_file2(b,c,2000000);
2370 sprintf(d,"%s/aa",a);
2371 make_file2(d,NULL,500000000);
2372 dump_directory_tree(mountpt);
2374 printf("Umount/mount attempt full\n");
2375 yaffs_unmount(mountpt);
2377 yaffs_mount(mountpt);
2379 printf("unlink small file\n");
2381 dump_directory_tree(mountpt);
2383 printf("Umount/mount attempt\n");
2384 yaffs_unmount(mountpt);
2385 yaffs_mount(mountpt);
2387 for(j = 0; j < 500; j++){
2388 printf("***** touch %d\n",j);
2389 dump_directory_tree(mountpt);
2391 yaffs_unmount(mountpt);
2392 yaffs_mount(mountpt);
2395 for(j = 0; j < 500; j++){
2396 printf("***** touch %d\n",j);
2397 dump_directory_tree(mountpt);
2399 yaffs_unmount(mountpt);
2400 yaffs_mount(mountpt);
2404 void huge_array_test(const char *mountpt,int n)
2415 sprintf(a,"mount point %s",mountpt);
2421 yaffs_mount(mountpt);
2426 printf("\n\n START run\n\n");
2427 while((space = yaffs_freespace(mountpt)) > 25000000){
2428 sprintf(a,"%s/file%d",mountpt,fnum);
2430 printf("create file %s, free space %d\n",a,space);
2431 create_file_of_size(a,10000000);
2432 printf("verifying file %s\n",a);
2433 verify_file_of_size(a,10000000);
2436 printf("\n\n verification/deletion\n\n");
2438 for(i = 0; i < fnum; i++){
2439 sprintf(a,"%s/file%d",mountpt,i);
2440 printf("verifying file %s\n",a);
2441 verify_file_of_size(a,10000000);
2442 printf("deleting file %s\n",a);
2445 printf("\n\n done \n\n");
2452 void random_write(int h)
2454 static char buffer[12000];
2457 n = random() & 0x1FFF;
2458 yaffs_write(h,buffer,n);
2461 void random_seek(int h)
2464 n = random() & 0xFFFFF;
2465 yaffs_lseek(h,n,SEEK_SET);
2468 void random_truncate(int h, char * name)
2472 n = random() & 0xFFFFF;
2473 flen = yaffs_lseek(h,0,SEEK_END);
2476 yaffs_ftruncate(h,n);
2477 yaffs_lseek(h,n,SEEK_SET);
2481 #define NSMALLFILES 10
2482 void random_small_file_test(const char *mountpt,int iterations)
2485 char a[NSMALLFILES][50];
2496 yaffs_mount(mountpt);
2498 for(i = 0; i < NSMALLFILES; i++){
2503 for(n = 0; n < iterations; n++){
2505 for(i = 0; i < NSMALLFILES; i++) {
2508 if(strlen(a[i]) == 0){
2509 sprintf(a[i],"%s/%dx%d",mountpt,n,i);
2510 h[i] = yaffs_open(a[i],O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2514 printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
2524 random_truncate(h[i],a[i]);
2527 case 5: random_seek(h[i]);
2544 for(i = 0; i < NSMALLFILES; i++)
2547 yaffs_unmount(mountpt);
2550 void rmdir_test(const char *mountpt)
2555 yaffs_mount(mountpt);
2557 strcpy(name,mountpt);
2559 strcat(name,"hello");
2560 yaffs_mkdir(name,0666);
2562 yaffs_unmount(mountpt);
2567 static void print_xattrib_val(const char *path, const char *name)
2572 n = yaffs_getxattr(path,name,buffer,sizeof(buffer));
2574 u8 *b = (u8 *)buffer;
2576 printf("%d bytes:",n);
2580 printf("[%02X]",*b);
2586 printf(" Novalue result %d\n",n);
2589 static void list_xattr(const char *path)
2596 list_len = yaffs_listxattr(path,list,sizeof(list));
2597 printf("xattribs for %s, result is %d\n",path,list_len);
2598 while(n < list_len){
2599 len = strlen(list + n);
2600 printf("\"%s\" value ",list+n);
2601 print_xattrib_val(path,list + n);
2607 void basic_utime_test(const char *mountpt)
2613 struct yaffs_utimbuf utb;
2614 struct yaffs_stat st;
2618 yaffs_mount(mountpt);
2620 strcpy(name,mountpt);
2622 strcat(name,"xfile");
2626 printf("created\n");
2627 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2629 yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
2633 result = yaffs_futime(h,&utb);
2634 printf("futime to a 1000 m 2000 result %d\n",result);
2635 yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
2640 result = yaffs_utime(name, &utb);
2641 printf("utime to a 5000 m 8000 result %d\n",result);
2642 yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
2644 result = yaffs_utime(name, NULL);
2645 printf("utime to NULL result %d\n",result);
2646 yaffs_fstat(h,&st); printf(" times %u %u %u\n",st.yst_atime, st.yst_ctime, st.yst_mtime);
2651 void basic_xattr_test(const char *mountpt)
2660 yaffs_mount(mountpt);
2662 strcpy(name,mountpt);
2664 strcat(name,"xfile");
2667 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2673 printf("Add an attribute\n");
2675 result = yaffs_setxattr(name,"foo",&val1,sizeof(val1),0);
2676 printf("wrote attribute foo: result %d\n",result);
2678 printf("Add an attribute\n");
2680 result = yaffs_setxattr(name,"bar",&val1,sizeof(val1),0);
2681 printf("wrote attribute bar: result %d\n",result);
2684 printf("Get non-existanrt attribute\n");
2685 print_xattrib_val(name,"not here");
2687 printf("Delete non existing attribute\n");
2688 yaffs_removexattr(name,"not here");
2691 printf("Remove foo\n");
2692 yaffs_removexattr(name,"foo");
2695 printf("Remove bar\n");
2696 yaffs_removexattr(name,"bar");
2701 void big_xattr_test(const char *mountpt)
2710 yaffs_mount(mountpt);
2712 strcpy(name,mountpt);
2714 strcat(name,"xfile");
2717 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2723 printf("Add a large attribute\n");
2724 memset(val,0x1,sizeof(val));
2725 result = yaffs_setxattr(name,"aaa",val,200,0);
2726 printf("wrote attribute aaa: result %d\n",result);
2729 printf("Add a large attribute\n");
2730 memset(val,0x2,sizeof(val));
2731 result = yaffs_setxattr(name,"bbb",val,1000,0);
2732 printf("wrote attribute bbb: result %d\n",result);
2735 printf("Replace attribute\n");
2736 memset(val,0x3,sizeof(val));
2737 result = yaffs_setxattr(name,"aaa",val,1000,0);
2738 printf("wrote attribute aaa: result %d\n",result);
2744 void dump_dev_stats(struct yaffs_dev *dev, const char * str)
2747 printf( "space free %d erased %d "
2748 "nand reads %d writes %d erases %d "
2749 "gc all %d passive %d oldestdirty %d blocks %d copies %d \n",
2750 dev->n_free_chunks, dev->n_erased_blocks * dev->param.chunks_per_block,
2751 dev->n_page_reads, dev->n_page_writes, dev->n_erasures,
2752 dev->all_gcs, dev->passive_gc_count, dev->oldest_dirty_gc_count, dev->n_gc_blocks, dev->n_gc_copies);
2755 void test_flash_traffic(const char *mountpt)
2760 struct yaffs_dev *dev;
2762 yaffs_trace_mask = 0;
2766 yaffs_mount(mountpt);
2768 dev = yaffs_getdev(mountpt);
2770 strcpy(name0,mountpt);
2773 strcpy(name1,mountpt);
2776 dump_dev_stats(dev,"start");
2777 create_file_of_size(name0,32 * 1024 * 1024);
2778 dump_dev_stats(dev,"32MB written");
2779 for(i = 0; i < 20; i++)
2780 create_file_of_size(name1,1024 * 1024);
2781 dump_dev_stats(dev,"20x 1MB files written");
2785 void link_follow_test(const char *mountpt)
2793 yaffs_trace_mask = 0;
2797 yaffs_mount(mountpt);
2799 sprintf(fn,"%s/file",mountpt);
2800 sprintf(sn,"%s/sym",mountpt);
2801 sprintf(hn,"%s/hl-sym",mountpt);
2803 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2804 result = yaffs_close(h);
2806 result = yaffs_symlink(fn,sn);
2807 result = yaffs_link(sn,hn);
2809 h =yaffs_open(hn,O_RDWR,0);
2813 void max_files_test(const char *mountpt)
2822 yaffs_trace_mask = 0;
2826 yaffs_mount(mountpt);
2828 for(i = 0; i < 5000; i++) {
2829 sprintf(fn,"%s/file%d", mountpt, i);
2831 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2833 printf("File %s not created\n", fn);
2834 yaffs_write(h,fn,100);
2835 result = yaffs_close(h);
2837 for(i = 0; i < 5; i++){
2838 sprintf(fn,"%s/file%d",mountpt, i);
2842 for(i = 1000; i < 1010; i++){
2843 sprintf(fn,"%s/file%d",mountpt, i);
2844 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2845 yaffs_write(h,fn,100);
2847 printf("File %s not created\n", fn);
2848 result = yaffs_close(h);
2851 yaffs_unmount(mountpt);
2853 //h =yaffs_open(hn,O_RDWR,0);
2856 void case_insensitive_test(const char *mountpt)
2862 struct yaffs_stat s;
2866 yaffs_trace_mask = 0;
2870 yaffs_mount(mountpt);
2871 dump_directory_tree(mountpt);
2873 sprintf(fn,"%s/Abc.Txt",mountpt);
2875 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2877 ret = yaffs_write(h,fn, strlen(fn) + 1);
2879 ret = yaffs_close(h);
2881 dump_directory_tree(mountpt);
2891 h = yaffs_open(fn2, O_RDONLY, 0);
2892 ret = yaffs_read(h, buffer, 100);
2894 if (ret != strlen(fn) + 1 || memcmp(buffer, fn, ret)){
2895 printf("wrong file read\n");
2897 printf("File %s is the same as file %s\n", fn, fn2);
2900 ret = yaffs_stat(fn2, &s);
2902 printf("renaming\n");
2904 ret = yaffs_rename(fn, fn2);
2905 dump_directory_tree(mountpt);
2909 void start_twice(const char *mountpt)
2911 printf("About to do first yaffs_start\n");
2913 printf("started\n");
2914 printf("First mount returns %d\n", yaffs_mount(mountpt));
2915 printf("About to do second yaffs_start\n");
2917 printf("started\n");
2918 printf("Second mount returns %d\n", yaffs_mount(mountpt));
2921 #define N_WRITES 2000
2924 #define BUFFER_N 1100
2925 unsigned xxbuffer[BUFFER_N];
2928 void set_buffer(int n)
2931 for(i = 0; i < BUFFER_N; i++)
2932 xxbuffer[i] = i + n;
2935 void write_big_sparse_file(int h)
2940 int n = sizeof(xxbuffer);
2943 for(i = 0; i < N_WRITES; i++) {
2944 printf("writing at %lld\n", offset);
2946 pos = yaffs_lseek(h, offset, SEEK_SET);
2948 printf("mismatched seek pos %lld offset %lld\n",
2953 wrote = yaffs_write(h, xxbuffer, n);
2956 printf("mismatched write wrote %d n %d\n", wrote, n);
2960 offset += (STRIDE * sizeof(xxbuffer));
2963 yaffs_ftruncate(h, offset);
2970 void verify_big_sparse_file(int h)
2972 unsigned check_buffer[BUFFER_N];
2976 int n = sizeof(check_buffer);
2978 const char * check_type;
2979 int checks_failed = 0;
2980 int checks_passed = 0;
2982 for(i = 0; i < N_WRITES * STRIDE; i++) {
2984 check_type = "zero";
2985 memset(xxbuffer,0, n);
2987 check_type = "buffer";
2988 set_buffer(i/STRIDE);
2990 //printf("%s checking %lld\n", check_type, offset);
2991 pos = yaffs_lseek(h, offset, SEEK_SET);
2993 printf("mismatched seek pos %lld offset %lld\n",
2998 result = yaffs_read(h, check_buffer, n);
3001 printf("mismatched read result %d n %d\n", result, n);
3008 if(memcmp(xxbuffer, check_buffer, n)) {
3011 printf("buffer at %lld mismatches\n", pos);
3012 printf("xxbuffer ");
3013 for(j = 0; j < 20; j++)
3014 printf(" %d",xxbuffer[j]);
3016 printf("check_buffer ");
3017 for(j = 0; j < 20; j++)
3018 printf(" %d",check_buffer[j]);
3026 offset += sizeof(xxbuffer);
3029 printf("%d checks passed, %d checks failed\n", checks_passed, checks_failed);
3034 void large_file_test(const char *mountpt)
3036 char xx_buffer[1000];
3042 yaffs_trace_mask = 0;
3046 yaffs_mount(mountpt);
3047 printf("mounted\n");
3050 sprintf(fullname, "%s/%s", mountpt, "big-test-file");
3052 handle = yaffs_open(fullname, O_RDONLY, 0);
3054 handle = yaffs_open(fullname, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
3057 perror("opening file");
3061 write_big_sparse_file(handle);
3062 verify_big_sparse_file(handle);
3064 yaffs_close(handle);
3066 printf("Job done\n");
3067 yaffs_unmount(mountpt);
3069 yaffs_mount(mountpt);
3070 printf("mounted again\n");
3072 handle = yaffs_open(fullname, O_RDONLY, 0);
3073 verify_big_sparse_file(handle);
3074 yaffs_unmount(mountpt);
3077 yaffs_mount_common(mountpt, 0, 1);
3078 printf("mounted with no checkpt\n");
3080 handle = yaffs_open(fullname, O_RDONLY, 0);
3081 verify_big_sparse_file(handle);
3082 yaffs_unmount(mountpt);
3084 /* Check resize by adding to the end, resizing back and verifying. */
3085 yaffs_mount_common(mountpt, 0, 1);
3086 printf("checking resize\n");
3088 handle = yaffs_open(fullname, O_RDWR, 0);
3090 file_end = yaffs_lseek(handle, 0, SEEK_END);
3091 printf("file_end %lld\n", file_end);
3092 for(i = 0; i < 10000; i++)
3093 yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
3094 yaffs_ftruncate(handle, file_end);
3096 verify_big_sparse_file(handle);
3097 yaffs_unmount(mountpt);
3102 int mk_dir(const char *mp, const char *name)
3104 char full_name[100];
3106 sprintf(full_name, "%s/%s", mp, name);
3108 return yaffs_mkdir(full_name, S_IREAD| S_IWRITE);
3111 int mk_file(const char *mp, const char *name)
3113 char full_name[100];
3116 sprintf(full_name, "%s/%s", mp, name);
3118 h = yaffs_open(full_name, O_RDWR | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
3120 yaffs_write(h, name, strlen(name));
3126 void xx_test(const char *mountpt)
3128 char xx_buffer[1000];
3132 yaffs_format(mountpt,0,0,0);
3134 yaffs_mount(mountpt);
3135 printf("mounted\n");
3138 printf("create files\n");
3140 mk_dir(mountpt, "foo");
3141 mk_file(mountpt, "foo/f1");
3142 mk_file(mountpt, "foo/f2");
3143 mk_file(mountpt, "foo/f3");
3144 mk_file(mountpt, "foo/f4");
3145 dump_directory_tree(mountpt);
3147 printf("unmount and remount\n");
3149 /* Unmount/remount */
3150 yaffs_unmount(mountpt);
3151 yaffs_mount(mountpt);
3152 dump_directory_tree(mountpt);
3155 void yy_test(const char *mountpt)
3157 char xx_buffer[1000];
3161 yaffs_mount(mountpt);
3162 dump_directory_tree(mountpt);
3166 void readdir_test(const char *mountpt)
3168 char xx_buffer[1000];
3173 yaffs_DIR *dirs[100];
3176 yaffs_trace_mask = 0;
3180 yaffs_mount(mountpt);
3182 for(i = 0; i < 100; i++) {
3183 dirs[i] = yaffs_opendir(mountpt);
3184 printf("%2d %p,", i, dirs[i]);
3189 for(i = 0; i < 100; i++) {
3191 yaffs_closedir(dirs[i]);
3195 for(i = 0; i < 100; i++) {
3196 dirs[i] = yaffs_opendir(mountpt);
3197 printf("%2d %p,", i, dirs[i]);
3200 yaffs_unmount(mountpt);
3205 void format_test(const char *mountpt)
3211 ret = yaffs_format(mountpt, 0, 0, 0);
3212 printf("yaffs_format(...,0, 0, 0) of unmounted returned %d\n", ret);
3214 yaffs_mount(mountpt);
3216 ret = yaffs_format(mountpt, 0, 0, 0);
3217 printf("yaffs_format(...,0, 0, 0) of mounted returned %d\n", ret);
3219 ret = yaffs_format(mountpt, 1, 0, 0);
3220 printf("yaffs_format(...,1, 0, 0) of mounted returned %d\n", ret);
3222 ret = yaffs_mount(mountpt);
3223 printf("mount should return 0 returned %d\n", ret);
3225 ret = yaffs_format(mountpt, 1, 0, 1);
3226 printf("yaffs_format(...,1, 0, 1) of mounted returned %d\n", ret);
3228 ret = yaffs_mount(mountpt);
3229 printf("mount should return -1 returned %d\n", ret);
3232 void dir_rename_test(const char *mountpt)
3240 yaffs_mount(mountpt);
3242 sprintf(fname,"%s/file",mountpt);
3243 sprintf(dname,"%s/directory",mountpt);
3245 h = yaffs_open(fname,O_CREAT | O_RDWR | O_TRUNC, 0666);
3248 yaffs_mkdir(dname, 0666);
3250 dump_directory_tree(mountpt);
3252 printf("Try to rename %s to %s\n", fname, dname);
3253 ret = yaffs_rename(fname, dname);
3254 printf("result %d, %d\n", ret, yaffs_get_error());
3256 printf("Try to rename %s to %s\n", dname, fname);
3257 ret = yaffs_rename(dname, fname);
3258 printf("result %d, %d\n", ret, yaffs_get_error());
3264 void dir_fd_test(const char *mountpt)
3272 yaffs_mount(mountpt);
3274 sprintf(name,"%s/directory",mountpt);
3275 yaffs_mkdir(name, 0666);
3276 for(i=0; i < 20; i++) {
3277 sprintf(name,"%s/directory/file%d",mountpt, i);
3279 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3280 yaffs_write(h, name, strlen(name));
3283 sprintf(name,"%s/dddd",mountpt);
3284 yaffs_mkdir(name, 0666);
3285 for(i=0; i < 20; i++) {
3286 sprintf(name,"%s/dddd/filezzz%d",mountpt, i);
3288 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3289 yaffs_write(h, name, strlen(name));
3294 dump_directory_tree(mountpt);
3295 dump_directory_tree_fd(mountpt);
3296 dump_directory_tree_fd(mountpt);
3301 int simulate_power_failure;
3303 int main(int argc, char *argv[])
3305 random_seed = time(NULL);
3306 //return long_test(argc,argv);
3308 //return cache_read_test();
3310 // resize_stress_test_no_grow("/flash/flash",20);
3311 //root_perm_remount("/flash/flash");
3313 //huge_directory_test_on_path("/ram2k");
3315 //yaffs_backward_scan_test("/flash/flash");
3316 // yaffs_device_flush_test("/flash/flash");
3318 //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
3320 //fill_empty_files_test("/nand/");
3321 //resize_stress_test("/nand");
3322 //overwrite_test("/nand");
3324 //long_name_test("/nand");
3325 //link_test0("/nand");
3326 //link_test1("yaffs2");
3327 //scan_pattern_test("/flash",10000,10);
3328 //short_scan_test("/flash/flash",40000,200);
3329 //small_mount_test("/flash/flash",1000);
3330 //small_overwrite_test("/flash/flash",1000);
3331 //seek_overwrite_test("/flash/flash",1000);
3332 //checkpoint_fill_test("/flash/flash",20);
3333 //checkpoint_upgrade_test("/flash/flash",20);
3334 //small_overwrite_test("/flash/flash",1000);
3335 //checkpoint_fill_test("/flash/flash",20);
3336 //random_small_file_test("/flash/flash",10000);
3337 // huge_array_test("/flash/flash",10);
3340 // handle_test("yaffs2/");
3342 //long_test_on_path("/ram2k");
3343 // long_test_on_path("/flash");
3344 //simple_rw_test("/flash/flash");
3345 //fill_disk_test("/flash/flash");
3346 // rename_over_test("/flash");
3347 //lookup_test("/flash");
3348 //freespace_test("/flash/flash");
3350 //link_test("/flash/flash");
3352 // cache_bypass_bug_test();
3354 //free_space_check();
3356 //check_resize_gc_bug("/flash");
3358 //basic_xattr_test("/nand");
3359 //big_xattr_test("/nand");
3361 //null_name_test("yaffs2");
3363 //test_flash_traffic("yaffs2");
3364 // link_follow_test("/nand");
3365 //basic_utime_test("/nand");
3368 //format_test("/nand");
3370 //max_files_test("/nand");
3372 //start_twice("/nand");
3374 //large_file_test("/nand");
3375 //readdir_test("/nand");
3377 //basic_utime_test("/nand");
3378 //case_insensitive_test("/nand");
3381 //dir_rename_test("/nand");
3383 dir_fd_test("/nand");