2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * 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);
121 int dump_file_data(char *fn)
128 h = yaffs_open(fn, O_RDWR,0);
132 while(yaffs_read(h,&b,1)> 0)
149 void dump_file(const char *fn)
155 h = yaffs_open(fn,O_RDONLY,0);
158 printf("*****\nDump file %s does not exist\n",fn);
162 size = yaffs_lseek(h,0,SEEK_SET);
163 printf("*****\nDump file %s size %d\n",fn,size);
164 for(i = 0; i < size; i++)
171 void create_file_of_size(const char *fn,int syze)
179 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
183 sprintf(xx,"%s %8d",fn,iteration);
185 result = yaffs_write(h,xx,n);
187 printf("Wrote %d, should have been %d. syze is %d\n",result,n,syze);
196 void verify_file_of_size(const char *fn,int syze)
205 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
207 h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
209 while (iterations > 0)
211 sprintf(xx,"%s %8d",fn,iterations);
214 result = yaffs_read(h,yy,l);
216 printf("result in line %d is %d", __LINE__, result);
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);
294 void null_name_test(const char *path)
302 sprintf(fn,"%s",path);
304 h = yaffs_open(fn,O_CREAT| O_TRUNC| O_RDWR, 0666);
313 void yaffs_device_flush_test(const char *path)
323 do_some_file_stuff(path);
325 // Open and add some data to a few files
326 for(i = 0; i < 10; i++) {
328 sprintf(fn,"%s/ff%d",path,i);
330 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
331 yaffs_write(h,xxzz,2000);
332 yaffs_write(h,xxzz,2000);
341 void short_scan_test(const char *path, int fsize, int niterations)
346 sprintf(fn,"%s/%s",path,"f1");
349 for(i = 0; i < niterations; i++)
351 printf("\n*****************\nIteration %d\n",i);
353 printf("\nmount: Directory look-up of %s\n",path);
355 make_a_file(fn,1,fsize);
362 void scan_pattern_test(const char *path, int fsize, int niterations)
369 sprintf(fn[0],"%s/%s",path,"f0");
370 sprintf(fn[1],"%s/%s",path,"f1");
371 sprintf(fn[2],"%s/%s",path,"f2");
375 for(i = 0; i < niterations; i++)
377 printf("\n*****************\nIteration %d\n",i);
379 printf("\nmount: Directory look-up of %s\n",path);
381 for(j = 0; j < 3; j++)
383 result = dump_file_data(fn[j]);
384 result = check_pattern_file(fn[j]);
385 make_pattern_file(fn[j],fsize);
386 result = dump_file_data(fn[j]);
387 result = check_pattern_file(fn[j]);
389 printf("result in line %d is %d", __LINE__, result);
395 void fill_disk(const char *path,int nfiles)
405 for(n = 0; n < nfiles; n++)
407 sprintf(str,"%s/%d",path,n);
409 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
411 printf("writing file %s handle %d ",str, h);
413 while ((result = yaffs_write(h,xx,600)) == 600)
415 f = yaffs_freespace(path);
417 printf("last freespace was %d\n", f);
418 result = yaffs_close(h);
419 printf(" close %d\n", result);
423 void fill_disk_and_delete(const char *path, int nfiles, int ncycles)
429 for(i = 0; i < ncycles; i++)
431 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
432 fill_disk(path,nfiles);
434 for(j = 0; j < nfiles; j++)
436 sprintf(str,"%s/%d",path,j);
437 result = yaffs_unlink(str);
438 printf("unlinking file %s, result %d\n", str, result);
444 void fill_files(const char *path,int flags, int maxIterations,int siz)
454 sprintf(str,"%s/%d",path,i);
455 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
459 for(j = 0; j < siz; j++)
461 yaffs_write(h,str,1);
470 } while(h >= 0 && i < maxIterations);
476 sprintf(str,"%s/%d",path,i);
477 printf("unlink %s\n",str);
479 } while(yaffs_unlink(str) >= 0);
483 void leave_unlinked_file(const char *path,int maxIterations,int siz)
492 sprintf(str,"%s/%d",path,i);
493 printf("create %s\n",str);
494 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
500 } while(h < 0 && i < maxIterations);
504 for(i = 0; i < siz; i++)
506 yaffs_write(h,str,1);
510 printf("Leaving file %s open\n",str);
514 void dumpDirFollow(const char *dname)
517 struct yaffs_dirent *de;
521 d = yaffs_opendir(dname);
525 printf("opendir failed\n");
529 while((de = yaffs_readdir(d)) != NULL)
531 sprintf(str,"%s/%s",dname,de->d_name);
535 printf("%s ino %d length %d mode %X ",
536 de->d_name, (int)s.st_ino, (int)s.st_size, s.st_mode);
537 switch(s.st_mode & S_IFMT)
539 case S_IFREG: printf("data file"); break;
540 case S_IFDIR: printf("directory"); break;
541 case S_IFLNK: printf("symlink -->");
542 if(yaffs_readlink(str,str,100) < 0)
545 printf("\"%s\"",str);
547 default: printf("unknown"); break;
557 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
562 void dump_directory_tree_worker(const char *dname,int recursive)
565 struct yaffs_dirent *de;
569 d = yaffs_opendir(dname);
573 printf("opendir failed\n");
577 while((de = yaffs_readdir(d)) != NULL)
579 sprintf(str,"%s/%s",dname,de->d_name);
583 printf("%s inode %d length %d mode %X ",
584 str, s.st_ino, (int)s.st_size, s.st_mode);
585 switch(s.st_mode & S_IFMT)
587 case S_IFREG: printf("data file"); break;
588 case S_IFDIR: printf("directory"); break;
589 case S_IFLNK: printf("symlink -->");
590 if(yaffs_readlink(str,str,100) < 0)
593 printf("\"%s\"",str);
595 default: printf("unknown"); break;
600 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
601 dump_directory_tree_worker(str,1);
610 static void dump_directory_tree(const char *dname)
612 dump_directory_tree_worker(dname,1);
614 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
617 void dump_directory_tree_worker_fd(const char *dname,int recursive)
620 struct yaffs_dirent *de;
624 h = yaffs_open(dname, O_RDONLY, 0);
628 printf("open of dir failed\n");
632 printf("using fd %d\n", h);
634 while((de = yaffs_readdir_fd(h)) != NULL)
636 sprintf(str,"%s/%s",dname,de->d_name);
640 printf("%s inode %d length %d mode %X ",
641 str,s.st_ino, (int)s.st_size, s.st_mode);
642 switch(s.st_mode & S_IFMT)
644 case S_IFREG: printf("data file"); break;
645 case S_IFDIR: printf("directory"); break;
646 case S_IFLNK: printf("symlink -->");
647 if(yaffs_readlink(str,str,100) < 0)
650 printf("\"%s\"",str);
652 default: printf("unknown"); break;
657 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
658 dump_directory_tree_worker_fd(str,1);
667 static void dump_directory_tree_fd(const char *dname)
669 dump_directory_tree_worker_fd(dname,1);
671 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
674 void dumpDir(const char *dname)
675 { dump_directory_tree_worker(dname,0);
677 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
681 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
685 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
687 fd = yaffs_open(path,tflags,0);
689 if((fd >= 0) != (expectedResult > 0))
691 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
695 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
704 int long_test(int argc, char *argv[])
715 struct yaffs_stat ystat;
722 yaffs_mount("/boot");
723 yaffs_mount("/data");
724 yaffs_mount("/flash");
727 printf("\nDirectory look-up of /boot\n");
729 printf("\nDirectory look-up of /data\n");
731 printf("\nDirectory look-up of /flash\n");
734 //leave_unlinked_file("/flash",20000,0);
735 //leave_unlinked_file("/data",20000,0);
737 leave_unlinked_file("/ram",20,0);
740 f = yaffs_open("/boot/b1", O_RDONLY,0);
742 printf("open /boot/b1 readonly, f=%d\n",f);
744 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
746 printf("open /boot/b1 O_CREAT, f=%d\n",f);
749 r = yaffs_write(f,"hello",1);
750 printf("write %d attempted to write to a read-only file\n",r);
754 printf("close %d\n",r);
756 f = yaffs_open("/boot/b1", O_RDWR,0);
758 printf("open /boot/b1 O_RDWR,f=%d\n",f);
761 r = yaffs_write(f,"hello",2);
762 printf("write %d attempted to write to a writeable file\n",r);
763 r = yaffs_write(f,"world",3);
764 printf("write %d attempted to write to a writeable file\n",r);
766 r= yaffs_lseek(f,0,SEEK_END);
767 printf("seek end %d\n",r);
769 r = yaffs_read(f,buffer,10);
770 printf("read %d \"%s\"\n",r,buffer);
771 r= yaffs_lseek(f,0,SEEK_SET);
772 printf("seek set %d\n",r);
774 r = yaffs_read(f,buffer,10);
775 printf("read %d \"%s\"\n",r,buffer);
777 r = yaffs_read(f,buffer,10);
778 printf("read %d \"%s\"\n",r,buffer);
780 // Check values reading at end.
781 // A read past end of file should return 0 for 0 bytes read.
783 r= yaffs_lseek(f,0,SEEK_END);
784 r = yaffs_read(f,buffer,10);
785 printf("read at end returned %d\n",r);
786 r= yaffs_lseek(f,500,SEEK_END);
787 r = yaffs_read(f,buffer,10);
788 printf("read past end returned %d\n",r);
792 printf("close %d\n",r);
794 copy_in_a_file("/boot/yyfile","xxx");
796 // Create a file with a long name
798 copy_in_a_file("/boot/file with a long name","xxx");
801 printf("\nDirectory look-up of /boot\n");
805 r = yaffs_lstat("/boot/file with a long name",&ystat);
809 r = yaffs_rename("/boot/file with a long name","/boot/r1");
811 printf("\nDirectory look-up of /boot\n");
815 r = yaffs_unlink("/boot/r1");
817 printf("\nDirectory look-up of /boot\n");
822 r = yaffs_mkdir("/boot/directory1",0);
824 printf("\nDirectory look-up of /boot\n");
826 printf("\nDirectory look-up of /boot/directory1\n");
827 dumpDir("/boot/directory1");
829 // add a file to the directory
830 copy_in_a_file("/boot/directory1/file with a long name","xxx");
832 printf("\nDirectory look-up of /boot\n");
834 printf("\nDirectory look-up of /boot/directory1\n");
835 dumpDir("/boot/directory1");
837 // Attempt to delete directory (should fail)
839 r = yaffs_rmdir("/boot/directory1");
841 printf("\nDirectory look-up of /boot\n");
843 printf("\nDirectory look-up of /boot/directory1\n");
844 dumpDir("/boot/directory1");
846 // Delete file first, then rmdir should work
847 r = yaffs_unlink("/boot/directory1/file with a long name");
848 r = yaffs_rmdir("/boot/directory1");
851 printf("\nDirectory look-up of /boot\n");
853 printf("\nDirectory look-up of /boot/directory1\n");
854 dumpDir("/boot/directory1");
857 fill_disk_and_delete("/boot",20,20);
859 printf("\nDirectory look-up of /boot\n");
863 yaffs_symlink("yyfile","/boot/slink");
865 yaffs_readlink("/boot/slink",str,100);
866 printf("symlink alias is %s\n",str);
871 printf("\nDirectory look-up of /boot\n");
873 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
874 dumpDirFollow("/boot");
875 printf("\nDirectory look-up of /boot/directory1\n");
876 dumpDir("/boot/directory1");
878 h = yaffs_open("/boot/slink",O_RDWR,0);
880 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
884 yaffs_unlink("/boot/slink");
887 printf("\nDirectory look-up of /boot\n");
892 yaffs_lstat("/boot/yyfile",&ystat);
893 temp_mode = ystat.st_mode;
895 yaffs_chmod("/boot/yyfile",0x55555);
896 printf("\nDirectory look-up of /boot\n");
899 yaffs_chmod("/boot/yyfile",temp_mode);
900 printf("\nDirectory look-up of /boot\n");
903 // Permission checks...
904 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
905 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
906 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
908 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
909 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
910 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
912 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
913 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
914 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
916 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
917 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
918 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
920 yaffs_chmod("/boot/yyfile",temp_mode);
922 //create a zero-length file and unlink it (test for scan bug)
924 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
927 yaffs_unlink("/boot/zlf");
930 yaffs_dump_dev("/boot");
932 fill_disk_and_delete("/boot",20,20);
934 yaffs_dump_dev("/boot");
936 fill_files("/boot",1,10000,0);
937 fill_files("/boot",1,10000,5000);
938 fill_files("/boot",2,10000,0);
939 fill_files("/boot",2,10000,5000);
941 leave_unlinked_file("/data",20000,0);
942 leave_unlinked_file("/data",20000,5000);
943 leave_unlinked_file("/data",20000,5000);
944 leave_unlinked_file("/data",20000,5000);
945 leave_unlinked_file("/data",20000,5000);
946 leave_unlinked_file("/data",20000,5000);
948 yaffs_dump_dev("/boot");
949 yaffs_dump_dev("/data");
957 int huge_directory_test_on_path(char *path)
961 struct yaffs_dirent *de;
977 // Create a large number of files
979 for(i = 0; i < 2000; i++)
981 sprintf(str,"%s/%d",path,i);
983 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
989 d = yaffs_opendir(path);
992 while((de = yaffs_readdir(d)) != NULL) {
993 if (total >lastTotal+100*9*1024||(i & 1023)==0){
994 printf("files = %d, total = %d\n",i, total);
998 sprintf(str,"%s/%s",path,de->d_name);
1000 switch(s.st_mode & S_IFMT){
1002 //printf("data file");
1014 int yaffs_scan_test(const char *path)
1022 void rename_over_test(const char *mountpt)
1029 sprintf(a,"%s/a",mountpt);
1030 sprintf(b,"%s/b",mountpt);
1031 sprintf(c,"%s/c",mountpt);
1035 yaffs_mount(mountpt);
1037 printf("Existing files\n");
1038 dumpDirFollow(mountpt);
1042 i = yaffs_open(c,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1043 printf("File c handle is %d\n",i);
1045 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1047 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1049 yaffs_rename(a,b); // rename over
1050 yaffs_rename(b,a); // rename back again (not renaimng over)
1051 yaffs_rename(a,b); // rename back again (not renaimng over)
1054 yaffs_unmount(mountpt);
1059 int resize_stress_test(const char *path)
1074 sprintf(aname,"%s%s",path,"/a");
1075 sprintf(bname,"%s%s",path,"/b");
1077 memset(abuffer,'a',1000);
1078 memset(bbuffer,'b',1000);
1080 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1081 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1083 printf(" %s %d %s %d\n",aname,a,bname,b);
1087 for(j = 0; j < 100; j++)
1089 yaffs_lseek(a,0,SEEK_END);
1092 for(i = 0; i <20000; i++)
1094 //r = yaffs_lseek(b,i,SEEK_SET);
1095 //r = yaffs_write(b,bbuffer,1000);
1100 int syz = yaffs_lseek(a,0,SEEK_END);
1103 if(syz < 0) syz = 0;
1104 yaffs_ftruncate(a,syz);
1110 r = yaffs_lseek(a,i * 500,SEEK_SET);
1112 printf("At line %d, r is %d\n", __LINE__, r);
1113 r = yaffs_write(a,abuffer,1000);
1115 printf("At line %d, r is %d\n", __LINE__, r);
1127 int overwrite_test(const char *path)
1139 sprintf(aname,"%s%s",path,"/a");
1140 sprintf(bname,"%s%s",path,"/b");
1142 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1143 for(j= 0; j < 500; j++){
1144 yaffs_write(b,bname,100);
1145 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1146 for(i = 0; i < rand() % 20000; i++)
1147 yaffs_write(a,&a,sizeof(a));
1156 int root_perm_remount(const char *path)
1158 struct yaffs_stat s;
1164 yaffs_lstat(path,&s);
1165 printf("root perms after mount %x\n",s.st_mode);
1167 yaffs_chmod(path, 0777);
1169 yaffs_lstat(path,&s);
1170 printf("root perms after setting to 0777 is %x\n",s.st_mode);
1172 yaffs_unmount(path);
1179 int resize_stress_test_no_grow_complex(const char *path,int iters)
1195 sprintf(aname,"%s%s",path,"/a");
1196 sprintf(bname,"%s%s",path,"/b");
1198 memset(abuffer,'a',1000);
1199 memset(bbuffer,'b',1000);
1201 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1202 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1204 printf(" %s %d %s %d\n",aname,a,bname,b);
1208 for(j = 0; j < iters; j++)
1210 yaffs_lseek(a,0,SEEK_END);
1213 for(i = 0; i <20000; i++)
1215 //r = yaffs_lseek(b,i,SEEK_SET);
1216 //r = yaffs_write(b,bbuffer,1000);
1221 int syz = yaffs_lseek(a,0,SEEK_END);
1227 if(syz < 0) syz = 0;
1228 yaffs_ftruncate(a,syz);
1229 syz = yaffs_lseek(a,0,SEEK_END);
1230 printf("shrink to %d\n",syz);
1238 r = yaffs_lseek(a,500,SEEK_END);
1239 r = yaffs_write(a,abuffer,1000);
1241 printf("At line %d, r is %d\n", __LINE__, r);
1248 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1256 int resize_stress_test_no_grow(const char *path,int iters)
1271 sprintf(aname,"%s%s",path,"/a");
1272 sprintf(bname,"%s%s",path,"/b");
1274 memset(abuffer,'a',1000);
1275 memset(bbuffer,'b',1000);
1277 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1278 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1280 printf(" %s %d %s %d\n",aname,a,bname,b);
1284 for(j = 0; j < iters; j++)
1286 yaffs_lseek(a,0,SEEK_END);
1289 for(i = 0; i <20000; i++)
1291 //r = yaffs_lseek(b,i,SEEK_SET);
1292 //r = yaffs_write(b,bbuffer,1000);
1297 int syz = yaffs_lseek(a,0,SEEK_END);
1303 if(syz < 0) syz = 0;
1304 yaffs_ftruncate(a,syz);
1305 syz = yaffs_lseek(a,0,SEEK_END);
1306 printf("shrink to %d\n",syz);
1314 r = yaffs_lseek(a,-500,SEEK_END);
1315 r = yaffs_write(a,abuffer,1000);
1317 printf("At line %d, r is %d\n", __LINE__, r);
1323 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1331 int directory_rename_test(void)
1336 yaffs_mount("/ram");
1337 yaffs_mkdir("/ram/a",0);
1338 yaffs_mkdir("/ram/a/b",0);
1339 yaffs_mkdir("/ram/c",0);
1341 printf("\nDirectory look-up of /ram\n");
1344 dumpDir("/ram/a/b");
1346 printf("Do rename (should fail)\n");
1348 r = yaffs_rename("/ram/a","/ram/a/b/d");
1350 printf("At line %d, r is %d\n", __LINE__, r);
1352 printf("\nDirectory look-up of /ram\n");
1355 dumpDir("/ram/a/b");
1357 printf("Do rename (should not fail)\n");
1359 r = yaffs_rename("/ram/c","/ram/a/b/d");
1360 printf("\nDirectory look-up of /ram\n");
1363 dumpDir("/ram/a/b");
1370 int cache_read_test(void)
1374 int sizeOfFiles = 500000;
1379 yaffs_mount("/boot");
1381 make_a_file("/boot/a",'a',sizeOfFiles);
1382 make_a_file("/boot/b",'b',sizeOfFiles);
1384 a = yaffs_open("/boot/a",O_RDONLY,0);
1385 b = yaffs_open("/boot/b",O_RDONLY,0);
1386 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1390 if (i > 100) i = 100;
1392 yaffs_read(a,buffer,i);
1393 yaffs_read(b,buffer,i);
1394 yaffs_write(c,buffer,i);
1395 } while(sizeOfFiles > 0);
1403 int cache_bypass_bug_test(void)
1405 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1406 // resulting in erroneous reads after writes.
1407 // This bug has been fixed.
1413 memset(buffer1,0,sizeof(buffer1));
1414 memset(buffer2,0,sizeof(buffer2));
1418 yaffs_mount("/boot");
1420 // Create a file of 2000 bytes.
1421 make_a_file("/boot/a",'X',2000);
1423 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1425 // Write a short sequence to the file.
1426 // This will go into the cache.
1427 yaffs_lseek(a,0,SEEK_SET);
1428 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1430 // Read a short sequence from the file.
1431 // This will come from the cache.
1432 yaffs_lseek(a,0,SEEK_SET);
1433 yaffs_read(a,buffer1,30);
1435 // Read a page size sequence from the file.
1436 yaffs_lseek(a,0,SEEK_SET);
1437 yaffs_read(a,buffer2,512);
1439 printf("buffer 1 %s\n",buffer1);
1440 printf("buffer 2 %s\n",buffer2);
1442 if(strncmp(buffer1,buffer2,20))
1444 printf("Cache bypass bug detected!!!!!\n");
1452 int free_space_check(void)
1457 yaffs_mount("/boot");
1458 fill_disk("/boot/",2);
1459 f = yaffs_freespace("/boot");
1461 printf("%d free when disk full\n",f);
1465 int truncate_test(void)
1475 yaffs_mount("/boot");
1477 yaffs_unlink("/boot/trunctest");
1479 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1481 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1483 yaffs_ftruncate(a,3);
1484 l= yaffs_lseek(a,0,SEEK_END);
1486 printf("truncated length is %d\n",l);
1488 yaffs_lseek(a,5,SEEK_SET);
1489 yaffs_write(a,"1",1);
1491 yaffs_lseek(a,0,SEEK_SET);
1493 r = yaffs_read(a,y,10);
1495 printf("read %d bytes:", r);
1497 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1509 void fill_disk_test(const char *mountpt)
1514 for(i = 0; i < 5; i++)
1516 yaffs_mount(mountpt);
1517 fill_disk_and_delete(mountpt,100,i+1);
1518 yaffs_unmount(mountpt);
1524 void fill_n_file_test(const char *mountpt, int n, int syz)
1531 yaffs_format(mountpt, 0, 0, 0);
1532 yaffs_mount(mountpt);
1535 for(i = 0; i < n; i++)
1540 sprintf(fname, "%s/%d", mountpt, i);
1542 h = yaffs_open(fname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1547 yaffs_write(h, fname, sizeof(fname));
1558 void fill_files_test(const char *mountpt)
1563 for(i = 0; i < 5; i++)
1565 yaffs_mount(mountpt);
1566 fill_files(mountpt,2,3,100);
1567 yaffs_unmount(mountpt);
1572 void fill_empty_files_test(const char *mountpt)
1581 for(i = 0; i < 5; i++)
1583 yaffs_mount(mountpt);
1584 for(d = 0; result >= 0 && d < 1000; d++){
1585 sprintf(name,"%s/%d",mountpt,d);
1586 result= yaffs_mkdir(name,0);
1587 printf("creating directory %s result %d\n",name,result);
1589 for(f = 0; result >= 0 && f < 100; f++){
1590 sprintf(name,"%s/%d/%d",mountpt,d,f);
1591 result= yaffs_open(name,O_CREAT, 0);
1592 yaffs_close(result);
1593 printf("creating file %s result %d\n",name,result);
1596 yaffs_unmount(mountpt);
1601 void long_name_test(const char *mountpt)
1605 char fullName[1000];
1611 // Make a 256 byte name
1612 memset(name,0,sizeof(name));
1613 for(i = 0; i < 256; i++)
1614 name[i] = '0' + i % 10;
1616 sprintf(fullName,"%s/%s",mountpt,name);
1618 for(i = 0; i < 1; i++)
1620 yaffs_mount(mountpt);
1622 printf("Files at start\n");
1625 printf("Creating file %s\n",fullName);
1627 f = yaffs_open(fullName,O_CREAT | O_RDWR,0);
1630 printf("Result %d\n",f);
1635 printf("Deleting %s\n",fullName);
1636 result = yaffs_unlink(fullName);
1637 printf("Result %d\n",result);
1643 yaffs_unmount(mountpt);
1649 void lookup_test(const char *mountpt)
1657 struct yaffs_dirent *de;
1661 yaffs_mount(mountpt);
1663 d = yaffs_opendir(mountpt);
1667 printf("opendir failed\n");
1672 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1674 printf("unlinking %s\n",de->d_name);
1675 yaffs_unlink(de->d_name);
1678 printf("%d files deleted\n",i);
1682 for(i = 0; i < 2000; i++){
1683 sprintf(a,"%s/%d",mountpt,i);
1684 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1689 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1691 printf("%d %s\n",i,de->d_name);
1694 printf("%d files listed\n\n\n",i);
1701 for(i = 0; i < 2000; i++){
1702 sprintf(a,"%s/%d",mountpt,i);
1707 yaffs_unmount(mountpt);
1711 void link_test0(const char *mountpt)
1719 yaffs_mount(mountpt);
1722 sprintf(namea,"%s/a",mountpt);
1723 sprintf(nameb,"%s/b",mountpt);
1725 printf("mounted\n");
1728 yaffs_unlink(namea);
1729 printf("a unlinked\n");
1732 yaffs_unlink(nameb);
1733 printf("b unlinked\n");
1736 result = yaffs_open(namea,O_CREAT| O_RDWR,0666);
1737 yaffs_close(result);
1738 printf("a created\n");
1741 yaffs_link(namea,nameb);
1744 yaffs_unlink(namea);
1745 printf("a ulinked\n");
1747 yaffs_unlink(nameb);
1748 printf("b unlinked\n");
1751 yaffs_unmount(mountpt);
1755 void link_test1(const char *mountpt)
1763 sprintf(a,"%s/aaa",mountpt);
1764 sprintf(b,"%s/bbb",mountpt);
1765 sprintf(c,"%s/ccc",mountpt);
1769 yaffs_mount(mountpt);
1772 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1773 for(i = 0; i < 100; i++)
1774 yaffs_write(h,a,100);
1787 yaffs_unmount(mountpt);
1788 yaffs_mount(mountpt);
1790 printf("link test done\n");
1793 void handle_test(const char *mountpt)
1800 sprintf(a,"%s/aaa",mountpt);
1804 yaffs_mount(mountpt);
1806 for(cycle = 0; cycle < 5; cycle++){
1807 printf("Start cycle %d\n",cycle);
1810 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1811 printf("%d handle %d\n",i,h);
1821 yaffs_unmount(mountpt);
1824 void freespace_test(const char *mountpt)
1834 sprintf(a,"%s/aaa",mountpt);
1838 yaffs_mount(mountpt);
1840 f0 = yaffs_freespace(mountpt);
1842 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1844 for(i = 0; i < 100; i++)
1845 yaffs_write(h,a,100);
1849 f1 = yaffs_freespace(mountpt);
1853 f2 = yaffs_freespace(mountpt);
1856 yaffs_unmount(mountpt);
1857 yaffs_mount(mountpt);
1859 f3 = yaffs_freespace(mountpt);
1861 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1866 void simple_rw_test(const char *mountpt)
1875 sprintf(a,"%s/aaa",mountpt);
1879 yaffs_mount(mountpt);
1883 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1885 for(i = 100000;i < 200000; i++){
1886 result = yaffs_write(h,&i,sizeof(i));
1890 printf("write error\n");
1897 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1900 yaffs_lseek(h,0,SEEK_SET);
1902 for(i = 100000; i < 200000; i++){
1903 result = yaffs_read(h,&x,sizeof(x));
1905 if(result != 4 || x != i){
1906 printf("read error %d %x %x\n",i,result,x);
1910 printf("Simple rw test passed\n");
1917 void scan_deleted_files_test(const char *mountpt)
1929 sprintf(sub,"%s/sdir",mountpt);
1932 for(j = 0; j < 10; j++)
1934 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1935 yaffs_mount(mountpt);
1939 p = (j & 0) ? mountpt: sub;
1941 for(i = 0; i < 100; i++)
1943 sprintf(fn,"%s/%d",p,i);
1947 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1948 for(k = 0; k < 1000; k++)
1949 yaffs_write(h,fn,100);
1956 for(i = 0; i < 10; i++)
1958 sprintf(fn,"%s/%d",p,i);
1966 yaffs_unmount(mountpt);
1975 void write_10k(int h)
1978 const char *s="0123456789";
1979 for(i = 0; i < 1000; i++)
1980 yaffs_write(h,s,10);
1983 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1989 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1991 for(i = 0; i < 100000; i+= 10000)
1996 offs = yaffs_lseek(h1,0,SEEK_CUR);
1999 printf("Could not write file\n");
2003 for(i = 0; i < 100000; i+= 10000)
2008 offs = yaffs_lseek(h1,0,SEEK_CUR);
2011 printf("Could not write file\n");
2015 yaffs_unlink(fdel1);
2020 void verify_200k_file(const char *fn)
2025 const char *s="0123456789";
2028 h1 = yaffs_open(fn, O_RDONLY, 0);
2030 for(i = 0; i < 200000 && errCount < 10; i+= 10)
2032 yaffs_read(h1,x,10);
2033 if(strncmp(x,s,10) != 0)
2035 printf("File %s verification failed at %d\n",fn,i);
2040 printf("Too many errors... aborted\n");
2047 void check_resize_gc_bug(const char *mountpt)
2056 sprintf(a,"%s/a",mountpt);
2057 sprintf(b,"%s/b",mountpt);
2058 sprintf(c,"%s/c",mountpt);
2064 yaffs_mount(mountpt);
2068 for(i = 0; i < 50; i++)
2070 printf("A\n");write_200k_file(a,"",c);
2071 printf("B\n");verify_200k_file(a);
2072 printf("C\n");write_200k_file(b,a,c);
2073 printf("D\n");verify_200k_file(b);
2074 yaffs_unmount(mountpt);
2075 yaffs_mount(mountpt);
2076 printf("E\n");verify_200k_file(a);
2077 printf("F\n");verify_200k_file(b);
2083 void multi_mount_test(const char *mountpt,int nmounts)
2091 sprintf(a,"%s/a",mountpt);
2095 for(i = 0; i < nmounts; i++){
2100 printf("############### Iteration %d Start\n",i);
2101 if(1 || i == 0 || i == 5)
2102 yaffs_mount(mountpt);
2104 dump_directory_tree(mountpt);
2109 sprintf(xx,"%s/0",a);
2110 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2112 sprintf(xx,"%s/1",a);
2113 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2116 for(j = 0; j < 200; j++){
2117 yaffs_write(h0,xx,1000);
2118 yaffs_write(h1,xx,1000);
2121 while(yaffs_write(h0,xx,1000) > 0){
2123 yaffs_write(h1,xx,1000);
2126 yaffs_lseek(h0,0,SEEK_END);
2127 yaffs_lseek(h1,0,SEEK_END);
2129 yaffs_lseek(h0,0,SEEK_SET);
2130 yaffs_lseek(h1,0,SEEK_SET);
2132 for(j = 0; j < 200; j++){
2133 yaffs_read(h0,xx,1000);
2134 yaffs_read(h1,xx,1000);
2138 // yaffs_truncate(h0,0);
2142 printf("########### %d\n",i);
2143 dump_directory_tree(mountpt);
2145 if(1 || i == 4 || i == nmounts -1)
2146 yaffs_unmount(mountpt);
2151 void small_mount_test(const char *mountpt,int nmounts)
2163 sprintf(a,"%s/a",mountpt);
2169 for(i = 0; i < nmounts; i++){
2173 printf("############### Iteration %d Start\n",i);
2174 if(1 || i == 0 || i == 5)
2175 yaffs_mount(mountpt);
2177 dump_directory_tree(mountpt);
2181 sprintf(xx,"%s/0",a);
2184 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2185 for(j = 0; j < 130; j++)
2186 yaffs_write(h0,xx,1000);
2190 h0 = yaffs_open(xx,O_RDONLY,0);
2192 sprintf(xx,"%s/1",a);
2193 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2195 while((nread = yaffs_read(h0,xx,1000)) > 0)
2196 yaffs_write(h1,xx,nread);
2199 yaffs_lseek(h0,0,SEEK_END);
2200 yaffs_lseek(h1,0,SEEK_END);
2202 yaffs_lseek(h0,0,SEEK_SET);
2203 yaffs_lseek(h1,0,SEEK_SET);
2205 for(j = 0; j < 200; j++){
2206 yaffs_read(h0,xx,1000);
2207 yaffs_read(h1,xx,1000);
2213 printf("########### %d\n",i);
2214 dump_directory_tree(mountpt);
2216 if(1 || i == 4 || i == nmounts -1)
2217 yaffs_unmount(mountpt);
2224 void small_overwrite_test(const char *mountpt,int nmounts)
2235 sprintf(a,"%s/a",mountpt);
2241 for(i = 0; i < nmounts; i++){
2243 static char xx[8000];
2245 printf("############### Iteration %d Start\n",i);
2247 yaffs_mount(mountpt);
2249 dump_directory_tree(mountpt);
2253 sprintf(xx,"%s/0",a);
2254 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2255 sprintf(xx,"%s/1",a);
2256 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2258 for(j = 0; j < 1000000; j+=1000){
2259 yaffs_ftruncate(h0,j);
2260 yaffs_lseek(h0,j,SEEK_SET);
2261 yaffs_write(h0,xx,7000);
2262 yaffs_write(h1,xx,7000);
2270 printf("########### %d\n",i);
2271 dump_directory_tree(mountpt);
2274 yaffs_unmount(mountpt);
2279 void seek_overwrite_test(const char *mountpt,int nmounts)
2281 static char xx[5000];
2290 sprintf(a,"%s/f",mountpt);
2294 yaffs_mount(mountpt);
2297 for(i = 0; i < nmounts; i++){
2299 h0 = yaffs_open(a, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2301 for(j = 0; j < 100000; j++){
2302 yaffs_lseek(h0,0,SEEK_SET);
2303 yaffs_write(h0,xx,5000);
2304 yaffs_lseek(h0,0x100000,SEEK_SET);
2305 yaffs_write(h0,xx,5000);
2317 void yaffs_touch(const char *fn)
2319 yaffs_chmod(fn, S_IREAD | S_IWRITE);
2322 void checkpoint_fill_test(const char *mountpt,int nmounts)
2333 sprintf(a,"%s/a",mountpt);
2340 for(i = 0; i < nmounts; i++){
2341 printf("############### Iteration %d Start\n",i);
2342 yaffs_mount(mountpt);
2343 dump_directory_tree(mountpt);
2346 sprintf(b,"%s/zz",a);
2348 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
2351 while(yaffs_write(h,c,50) == 50){}
2355 for(j = 0; j < 2; j++){
2356 printf("touch %d\n",j);
2358 yaffs_unmount(mountpt);
2359 yaffs_mount(mountpt);
2362 dump_directory_tree(mountpt);
2363 yaffs_unmount(mountpt);
2368 int make_file2(const char *name1, const char *name2,int syz)
2378 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2380 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2382 while(syz > 0 && n > 0){
2383 i = (syz > 2500) ? 2500 : syz;
2384 n = yaffs_write(h1,xx,i);
2385 n = yaffs_write(h2,xx,i);
2394 extern void SetCheckpointReservedBlocks(int n);
2396 void checkpoint_upgrade_test(const char *mountpt, int nmounts)
2408 sprintf(a,"%s/a",mountpt);
2410 printf("Create start condition\n");
2412 yaffs_mount(mountpt);
2414 sprintf(b,"%s/zz",a);
2415 sprintf(c,"%s/xx",a);
2416 make_file2(b,c,2000000);
2417 sprintf(d,"%s/aa",a);
2418 make_file2(d,NULL,500000000);
2419 dump_directory_tree(mountpt);
2421 printf("Umount/mount attempt full\n");
2422 yaffs_unmount(mountpt);
2424 yaffs_mount(mountpt);
2426 printf("unlink small file\n");
2428 dump_directory_tree(mountpt);
2430 printf("Umount/mount attempt\n");
2431 yaffs_unmount(mountpt);
2432 yaffs_mount(mountpt);
2434 for(j = 0; j < 500; j++){
2435 printf("***** touch %d\n",j);
2436 dump_directory_tree(mountpt);
2438 yaffs_unmount(mountpt);
2439 yaffs_mount(mountpt);
2442 for(j = 0; j < 500; j++){
2443 printf("***** touch %d\n",j);
2444 dump_directory_tree(mountpt);
2446 yaffs_unmount(mountpt);
2447 yaffs_mount(mountpt);
2451 void huge_array_test(const char *mountpt,int n)
2462 sprintf(a,"mount point %s",mountpt);
2468 yaffs_mount(mountpt);
2473 printf("\n\n START run\n\n");
2474 while((space = yaffs_freespace(mountpt)) > 25000000){
2475 sprintf(a,"%s/file%d",mountpt,fnum);
2477 printf("create file %s, free space %d\n",a,space);
2478 create_file_of_size(a,10000000);
2479 printf("verifying file %s\n",a);
2480 verify_file_of_size(a,10000000);
2483 printf("\n\n verification/deletion\n\n");
2485 for(i = 0; i < fnum; i++){
2486 sprintf(a,"%s/file%d",mountpt,i);
2487 printf("verifying file %s\n",a);
2488 verify_file_of_size(a,10000000);
2489 printf("deleting file %s\n",a);
2492 printf("\n\n done \n\n");
2499 void random_write(int h)
2501 static char buffer[12000];
2504 n = random() & 0x1FFF;
2505 yaffs_write(h,buffer,n);
2508 void random_seek(int h)
2511 n = random() & 0xFFFFF;
2512 yaffs_lseek(h,n,SEEK_SET);
2515 void random_truncate(int h, char *name)
2522 n = random() & 0xFFFFF;
2523 flen = yaffs_lseek(h,0,SEEK_END);
2526 yaffs_ftruncate(h,n);
2527 yaffs_lseek(h,n,SEEK_SET);
2531 #define NSMALLFILES 10
2532 void random_small_file_test(const char *mountpt,int iterations)
2535 char a[NSMALLFILES][50];
2546 yaffs_mount(mountpt);
2548 for(i = 0; i < NSMALLFILES; i++){
2553 for(n = 0; n < iterations; n++){
2555 for(i = 0; i < NSMALLFILES; i++) {
2558 if(strlen(a[i]) == 0){
2559 sprintf(a[i],"%s/%dx%d",mountpt,n,i);
2560 h[i] = yaffs_open(a[i],O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2564 printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
2574 random_truncate(h[i],a[i]);
2577 case 5: random_seek(h[i]);
2594 for(i = 0; i < NSMALLFILES; i++)
2597 yaffs_unmount(mountpt);
2600 void rmdir_test(const char *mountpt)
2605 yaffs_mount(mountpt);
2607 strcpy(name,mountpt);
2609 strcat(name,"hello");
2610 yaffs_mkdir(name,0666);
2612 yaffs_unmount(mountpt);
2617 static void print_xattrib_val(const char *path, const char *name)
2622 n = yaffs_getxattr(path,name,buffer,sizeof(buffer));
2624 u8 *b = (u8 *)buffer;
2626 printf("%d bytes:",n);
2630 printf("[%02X]",*b);
2636 printf(" Novalue result %d\n",n);
2639 static void list_xattr(const char *path)
2646 list_len = yaffs_listxattr(path,list,sizeof(list));
2647 printf("xattribs for %s, result is %d\n",path,list_len);
2648 while(n < list_len){
2649 len = strlen(list + n);
2650 printf("\"%s\" value ",list+n);
2651 print_xattrib_val(path,list + n);
2657 void basic_utime_test(const char *mountpt)
2662 struct yaffs_utimbuf utb;
2663 struct yaffs_stat st;
2667 yaffs_mount(mountpt);
2669 strcpy(name,mountpt);
2671 strcat(name,"xfile");
2675 printf("created\n");
2676 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2679 printf(" times %lu %lu %lu\n",
2680 st.yst_atime, st.yst_ctime, st.yst_mtime);
2684 result = yaffs_futime(h,&utb);
2685 printf("futime to a 1000 m 2000 result %d\n",result);
2687 printf(" times %lu %lu %lu\n",
2688 st.yst_atime, st.yst_ctime, st.yst_mtime);
2693 result = yaffs_utime(name, &utb);
2694 printf("utime to a 5000 m 8000 result %d\n",result);
2696 printf(" times %lu %lu %lu\n",
2697 st.yst_atime, st.yst_ctime, st.yst_mtime);
2699 result = yaffs_utime(name, NULL);
2700 printf("utime to NULL result %d\n",result);
2702 printf(" times %lu %lu %lu\n",
2703 st.yst_atime, st.yst_ctime, st.yst_mtime);
2708 void basic_xattr_test(const char *mountpt)
2717 yaffs_mount(mountpt);
2719 strcpy(name,mountpt);
2721 strcat(name,"xfile");
2724 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2730 printf("Add an attribute\n");
2732 result = yaffs_setxattr(name,"foo",&val1,sizeof(val1),0);
2733 printf("wrote attribute foo: result %d\n",result);
2735 printf("Add an attribute\n");
2737 result = yaffs_setxattr(name,"bar",&val1,sizeof(val1),0);
2738 printf("wrote attribute bar: result %d\n",result);
2741 printf("Get non-existanrt attribute\n");
2742 print_xattrib_val(name,"not here");
2744 printf("Delete non existing attribute\n");
2745 yaffs_removexattr(name,"not here");
2748 printf("Remove foo\n");
2749 yaffs_removexattr(name,"foo");
2752 printf("Remove bar\n");
2753 yaffs_removexattr(name,"bar");
2758 void big_xattr_test(const char *mountpt)
2767 yaffs_mount(mountpt);
2769 strcpy(name,mountpt);
2771 strcat(name,"xfile");
2774 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2780 printf("Add a large attribute\n");
2781 memset(val,0x1,sizeof(val));
2782 result = yaffs_setxattr(name,"aaa",val,200,0);
2783 printf("wrote attribute aaa: result %d\n",result);
2786 printf("Add a large attribute\n");
2787 memset(val,0x2,sizeof(val));
2788 result = yaffs_setxattr(name,"bbb",val,1000,0);
2789 printf("wrote attribute bbb: result %d\n",result);
2792 printf("Replace attribute\n");
2793 memset(val,0x3,sizeof(val));
2794 result = yaffs_setxattr(name,"aaa",val,1000,0);
2795 printf("wrote attribute aaa: result %d\n",result);
2801 void dump_dev_stats(struct yaffs_dev *dev, const char * str)
2804 printf( "space free %d erased %d "
2805 "nand reads %d writes %d erases %d "
2806 "gc all %d passive %d oldestdirty %d blocks %d copies %d \n",
2807 dev->n_free_chunks, dev->n_erased_blocks * dev->param.chunks_per_block,
2808 dev->n_page_reads, dev->n_page_writes, dev->n_erasures,
2809 dev->all_gcs, dev->passive_gc_count, dev->oldest_dirty_gc_count, dev->n_gc_blocks, dev->n_gc_copies);
2812 void test_flash_traffic(const char *mountpt)
2817 struct yaffs_dev *dev;
2819 yaffs_trace_mask = 0;
2823 yaffs_mount(mountpt);
2825 dev = yaffs_getdev(mountpt);
2827 strcpy(name0,mountpt);
2830 strcpy(name1,mountpt);
2833 dump_dev_stats(dev,"start");
2834 create_file_of_size(name0,32 * 1024 * 1024);
2835 dump_dev_stats(dev,"32MB written");
2836 for(i = 0; i < 20; i++)
2837 create_file_of_size(name1,1024 * 1024);
2838 dump_dev_stats(dev,"20x 1MB files written");
2842 void link_follow_test(const char *mountpt)
2850 yaffs_trace_mask = 0;
2854 yaffs_mount(mountpt);
2856 sprintf(fn,"%s/file",mountpt);
2857 sprintf(sn,"%s/sym",mountpt);
2858 sprintf(hn,"%s/hl-sym",mountpt);
2860 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2861 result = yaffs_close(h);
2863 result = yaffs_symlink(fn,sn);
2864 result = yaffs_link(sn,hn);
2867 printf("At line %d result is %d\n", __LINE__, result);
2869 h =yaffs_open(hn,O_RDWR,0);
2873 void max_files_test(const char *mountpt)
2881 yaffs_trace_mask = 0;
2885 yaffs_mount(mountpt);
2887 for(i = 0; i < 5000; i++) {
2888 sprintf(fn,"%s/file%d", mountpt, i);
2890 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2892 printf("File %s not created\n", fn);
2893 yaffs_write(h,fn,100);
2894 result = yaffs_close(h);
2896 for(i = 0; i < 5; i++){
2897 sprintf(fn,"%s/file%d",mountpt, i);
2901 for(i = 1000; i < 1010; i++){
2902 sprintf(fn,"%s/file%d",mountpt, i);
2903 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2904 yaffs_write(h,fn,100);
2906 printf("File %s not created\n", fn);
2907 result = yaffs_close(h);
2911 printf("At line %d result is %d\n", __LINE__, result);
2913 yaffs_unmount(mountpt);
2916 void case_insensitive_test(const char *mountpt)
2922 struct yaffs_stat s;
2926 yaffs_trace_mask = 0;
2930 yaffs_mount(mountpt);
2931 dump_directory_tree(mountpt);
2933 sprintf(fn,"%s/Abc.Txt",mountpt);
2935 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2937 ret = yaffs_write(h,fn, strlen(fn) + 1);
2939 ret = yaffs_close(h);
2941 dump_directory_tree(mountpt);
2951 h = yaffs_open(fn2, O_RDONLY, 0);
2952 ret = yaffs_read(h, buffer, 100);
2954 if (ret != (int)(strlen(fn) + 1) || memcmp(buffer, fn, ret)){
2955 printf("wrong file read\n");
2957 printf("File %s is the same as file %s\n", fn, fn2);
2960 ret = yaffs_stat(fn2, &s);
2962 printf("renaming\n");
2964 ret = yaffs_rename(fn, fn2);
2965 dump_directory_tree(mountpt);
2969 void start_twice(const char *mountpt)
2971 printf("About to do first yaffs_start\n");
2973 printf("started\n");
2974 printf("First mount returns %d\n", yaffs_mount(mountpt));
2975 printf("About to do second yaffs_start\n");
2977 printf("started\n");
2978 printf("Second mount returns %d\n", yaffs_mount(mountpt));
2981 #define N_WRITES 2000
2984 #define BUFFER_N 1100
2985 unsigned xxbuffer[BUFFER_N];
2987 void set_buffer(int n)
2990 for(i = 0; i < BUFFER_N; i++)
2991 xxbuffer[i] = i + n;
2994 void write_big_sparse_file(int h)
2999 int n = sizeof(xxbuffer);
3002 for(i = 0; i < N_WRITES; i++) {
3003 printf("writing at %d\n", (int)offset);
3005 pos = yaffs_lseek(h, offset, SEEK_SET);
3007 printf("mismatched seek pos %d offset %d\n",
3008 (int)pos, (int)offset);
3012 wrote = yaffs_write(h, xxbuffer, n);
3015 printf("mismatched write wrote %d n %d\n", wrote, n);
3019 offset += (STRIDE * sizeof(xxbuffer));
3022 yaffs_ftruncate(h, offset);
3026 void verify_big_sparse_file(int h)
3028 unsigned check_buffer[BUFFER_N];
3032 int n = sizeof(check_buffer);
3034 int checks_failed = 0;
3035 int checks_passed = 0;
3037 for(i = 0; i < N_WRITES * STRIDE; i++) {
3039 memset(xxbuffer,0, n);
3041 set_buffer(i/STRIDE);
3043 pos = yaffs_lseek(h, offset, SEEK_SET);
3045 printf("mismatched seek pos %d offset %d\n",
3046 (int)pos, (int)offset);
3050 result = yaffs_read(h, check_buffer, n);
3053 printf("mismatched read result %d n %d\n", result, n);
3057 if(memcmp(xxbuffer, check_buffer, n)) {
3060 printf("buffer at %d mismatches\n", (int)pos);
3061 printf("xxbuffer ");
3062 for(j = 0; j < 20; j++)
3063 printf(" %d",xxbuffer[j]);
3065 printf("check_buffer ");
3066 for(j = 0; j < 20; j++)
3067 printf(" %d",check_buffer[j]);
3075 offset += sizeof(xxbuffer);
3078 printf("%d checks passed, %d checks failed\n", checks_passed, checks_failed);
3083 void large_file_test(const char *mountpt)
3085 char xx_buffer[1000];
3091 yaffs_trace_mask = 0;
3095 yaffs_mount(mountpt);
3096 printf("mounted\n");
3099 sprintf(fullname, "%s/%s", mountpt, "big-test-file");
3101 handle = yaffs_open(fullname, O_RDONLY, 0);
3103 handle = yaffs_open(fullname, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
3106 perror("opening file");
3110 write_big_sparse_file(handle);
3111 verify_big_sparse_file(handle);
3113 yaffs_close(handle);
3115 printf("Job done\n");
3116 yaffs_unmount(mountpt);
3118 yaffs_mount(mountpt);
3119 printf("mounted again\n");
3121 handle = yaffs_open(fullname, O_RDONLY, 0);
3122 verify_big_sparse_file(handle);
3123 yaffs_unmount(mountpt);
3126 yaffs_mount3(mountpt, 0, 1);
3127 printf("mounted with no checkpt\n");
3129 handle = yaffs_open(fullname, O_RDONLY, 0);
3130 verify_big_sparse_file(handle);
3131 yaffs_unmount(mountpt);
3133 /* Check resize by adding to the end, resizing back and verifying. */
3134 yaffs_mount3(mountpt, 0, 1);
3135 printf("checking resize\n");
3137 handle = yaffs_open(fullname, O_RDWR, 0);
3139 file_end = yaffs_lseek(handle, 0, SEEK_END);
3140 printf("file_end %d\n", (int)file_end);
3141 for(i = 0; i < 10000; i++)
3142 yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
3143 yaffs_ftruncate(handle, file_end);
3145 verify_big_sparse_file(handle);
3146 yaffs_unmount(mountpt);
3151 int mk_dir(const char *mp, const char *name)
3153 char full_name[100];
3155 sprintf(full_name, "%s/%s", mp, name);
3157 return yaffs_mkdir(full_name, S_IREAD| S_IWRITE);
3160 int mk_file(const char *mp, const char *name)
3162 char full_name[100];
3165 sprintf(full_name, "%s/%s", mp, name);
3167 h = yaffs_open(full_name, O_RDWR | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
3169 yaffs_write(h, name, strlen(name));
3175 void xx_test(const char *mountpt)
3179 yaffs_format(mountpt,0,0,0);
3181 yaffs_mount(mountpt);
3182 printf("mounted\n");
3185 printf("create files\n");
3187 mk_dir(mountpt, "foo");
3188 mk_file(mountpt, "foo/f1");
3189 mk_file(mountpt, "foo/f2");
3190 mk_file(mountpt, "foo/f3");
3191 mk_file(mountpt, "foo/f4");
3192 dump_directory_tree(mountpt);
3194 printf("unmount and remount\n");
3196 /* Unmount/remount */
3197 yaffs_unmount(mountpt);
3198 yaffs_mount(mountpt);
3199 dump_directory_tree(mountpt);
3202 void yy_test(const char *mountpt)
3206 yaffs_mount(mountpt);
3207 dump_directory_tree(mountpt);
3211 void readdir_test(const char *mountpt)
3214 yaffs_DIR *dirs[100];
3216 yaffs_trace_mask = 0;
3220 yaffs_mount(mountpt);
3222 for(i = 0; i < 100; i++) {
3223 dirs[i] = yaffs_opendir(mountpt);
3224 printf("%2d %p,", i, dirs[i]);
3229 for(i = 0; i < 100; i++) {
3231 yaffs_closedir(dirs[i]);
3235 for(i = 0; i < 100; i++) {
3236 dirs[i] = yaffs_opendir(mountpt);
3237 printf("%2d %p,", i, dirs[i]);
3240 yaffs_unmount(mountpt);
3244 void format_test(const char *mountpt)
3250 ret = yaffs_format(mountpt, 0, 0, 0);
3251 printf("yaffs_format(...,0, 0, 0) of unmounted returned %d."
3252 " Should return 0\n\n\n", ret);
3254 yaffs_mount(mountpt);
3256 ret = yaffs_format(mountpt, 0, 0, 0);
3257 printf("yaffs_format(...,0, 0, 0) of mounted returned %d."
3258 " Should return -1 (busy)\n\n\n", ret);
3260 ret = yaffs_format(mountpt, 1, 0, 0);
3261 printf("yaffs_format(...,1, 0, 0) of mounted returned %d."
3262 " Should return 0.\n\n\n", ret);
3264 ret = yaffs_mount(mountpt);
3265 printf("mount should return 0 returned %d\n\n\n", ret);
3267 ret = yaffs_format(mountpt, 1, 0, 1);
3268 printf("yaffs_format(...,1, 0, 1) of mounted returned %d."
3269 " Should return 0.\n\n\n", ret);
3271 ret = yaffs_mount(mountpt);
3272 printf("mount should return -1 returned %d\n", ret);
3275 void dir_rename_test(const char *mountpt)
3283 yaffs_mount(mountpt);
3285 sprintf(fname,"%s/file",mountpt);
3286 sprintf(dname,"%s/directory",mountpt);
3288 h = yaffs_open(fname,O_CREAT | O_RDWR | O_TRUNC, 0666);
3291 yaffs_mkdir(dname, 0666);
3293 dump_directory_tree(mountpt);
3295 printf("Try to rename %s to %s\n", fname, dname);
3296 ret = yaffs_rename(fname, dname);
3297 printf("result %d, %d\n", ret, yaffs_get_error());
3299 printf("Try to rename %s to %s\n", dname, fname);
3300 ret = yaffs_rename(dname, fname);
3301 printf("result %d, %d\n", ret, yaffs_get_error());
3307 void dir_fd_test(const char *mountpt)
3314 yaffs_mount(mountpt);
3316 sprintf(name,"%s/directory",mountpt);
3317 yaffs_mkdir(name, 0666);
3318 for(i=0; i < 20; i++) {
3319 sprintf(name,"%s/directory/file%d",mountpt, i);
3321 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3322 yaffs_write(h, name, strlen(name));
3325 sprintf(name,"%s/dddd",mountpt);
3326 yaffs_mkdir(name, 0666);
3327 for(i=0; i < 20; i++) {
3328 sprintf(name,"%s/dddd/filezzz%d",mountpt, i);
3330 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3331 yaffs_write(h, name, strlen(name));
3336 dump_directory_tree(mountpt);
3337 dump_directory_tree_fd(mountpt);
3338 dump_directory_tree_fd(mountpt);
3342 void create_delete_many_files_test(const char *mountpt)
3354 yaffs_mount(mountpt);
3356 for(i = 1; i < 2000; i++) {
3357 sprintf(fn,"%s/f%d",mountpt, i);
3358 fsize = (i%10) * 10000 + 20000;
3359 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666);
3361 wrote = yaffs_write(h, buffer, sizeof(buffer));
3362 if (wrote != sizeof(buffer)) {
3363 printf("Writing file %s, only wrote %d bytes\n", fn, wrote);
3374 void find_device_check(void)
3377 yaffs_mount("/nand");
3379 yaffs_mkdir("/nandxxx", 0666);
3380 yaffs_mkdir("/nand/xxx", 0666);
3384 void try_opendir(const char *str)
3388 d = yaffs_opendir(str);
3390 printf("%s --> %p\n", str, d);
3396 void opendir_test(void)
3399 yaffs_mount("/nand/");
3400 yaffs_symlink("x", "/nand/sym");
3401 yaffs_mkdir("/nand/x",0666);
3402 yaffs_mkdir("/nand/y",0666);
3403 yaffs_mkdir("/nand/x/r",0666);
3404 yaffs_mkdir("/nand/x/s",0666);
3405 yaffs_mkdir("/nand/x/t",0666);
3407 try_opendir("nand/x/.");
3408 try_opendir("nand/x/r/..");
3409 try_opendir("nand/x/./");
3410 try_opendir("nand/x/r/../");
3411 try_opendir("/nand/x");
3412 try_opendir("/nand/x/");
3413 try_opendir("nand/x");
3414 try_opendir("nand/sym");
3415 try_opendir("nand/sym/");
3419 void try_rmdir(const char *str)
3423 ret= yaffs_rmdir(str);
3425 printf("rmdir(\"%s\") --> %d, errno %d\n", str, ret, yaffs_get_error());
3429 void rmdir_test2(void)
3433 yaffs_mount("/nand/");
3434 yaffs_mkdir("/nand/z",0666);
3435 try_rmdir("/nand/z");
3436 yaffs_mkdir("/nand/z",0666);
3437 try_rmdir("/nand/z/");
3441 void dump_yaffs_malloc_usage(void)
3444 unsigned high_water;
3446 yaffsfs_get_malloc_values(¤t, &high_water);
3449 "***************************************\n"
3450 "Dump of yaffs malloc usage during run\n"
3451 "Currently allocated : %u bytes\n"
3452 "High water allocated : %u bytes\n"
3453 "Note, this is not accurate on all platforms\n",
3454 current, high_water);
3460 int simulate_power_failure;
3462 int main(int argc, char *argv[])
3465 //unlink("emfile-nand");
3468 atexit(dump_yaffs_malloc_usage);
3473 random_seed = time(NULL);
3474 //return long_test(argc,argv);
3476 //return cache_read_test();
3478 // resize_stress_test_no_grow("/flash/flash",20);
3479 //root_perm_remount("/flash/flash");
3481 //huge_directory_test_on_path("/ram2k");
3483 //yaffs_backward_scan_test("/flash/flash");
3484 // yaffs_device_flush_test("/flash/flash");
3486 //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
3488 //fill_empty_files_test("/nand/");
3489 //resize_stress_test("/nand");
3490 //overwrite_test("/nand");
3492 //long_name_test("/nand");
3493 //link_test0("/nand");
3494 //link_test1("yaffs2");
3495 //scan_pattern_test("/flash",10000,10);
3496 //short_scan_test("/yflash2",40000,200);
3497 //short_scan_test("/nand128MB",40000,200);
3498 //small_mount_test("/flash/flash",1000);
3499 //small_overwrite_test("/flash/flash",1000);
3500 //seek_overwrite_test("/flash/flash",1000);
3501 //checkpoint_fill_test("/flash/flash",20);
3502 //checkpoint_upgrade_test("/flash/flash",20);
3503 //small_overwrite_test("/flash/flash",1000);
3504 //checkpoint_fill_test("/flash/flash",20);
3505 //random_small_file_test("/flash/flash",10000);
3506 // huge_array_test("/flash/flash",10);
3509 // handle_test("yaffs2/");
3511 //long_test_on_path("/ram2k");
3512 // long_test_on_path("/flash");
3513 //simple_rw_test("/flash/flash");
3514 fill_n_file_test("/nand128MB", 50, 128000000/50);
3515 // rename_over_test("/flash");
3516 //lookup_test("/flash");
3517 //freespace_test("/flash/flash");
3519 //link_test("/flash/flash");
3521 // cache_bypass_bug_test();
3523 //free_space_check();
3525 //check_resize_gc_bug("/flash");
3527 //basic_xattr_test("/nand");
3528 //big_xattr_test("/nand");
3530 //null_name_test("yaffs2");
3532 //test_flash_traffic("yaffs2");
3533 // link_follow_test("/nand");
3534 //basic_utime_test("/nand");
3537 //format_test("/nand");
3539 //max_files_test("/nand");
3541 //start_twice("/nand");
3543 //large_file_test("/nand");
3544 //readdir_test("/nand");
3546 //basic_utime_test("/nand");
3547 //case_insensitive_test("/nand");
3550 //dir_rename_test("/nand");
3552 //dir_fd_test("/nand");
3554 //format_test("/nand");
3559 //create_delete_many_files_test("/nand");