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);
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);
219 printf("result in line %d is %d", __LINE__, result);
223 printf("=====>>>>> verification of file %s failed near position %lld\n",fn,(long long)yaffs_lseek(h,0,SEEK_CUR));
230 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
236 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
238 iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
239 while (iterations > 0)
241 yaffs_write(h,fn,strlen(fn));
245 yaffs_ftruncate(h,reSyze);
247 yaffs_lseek(h,0,SEEK_SET);
248 iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
249 while (iterations > 0)
251 yaffs_write(h,fn,strlen(fn));
259 void do_some_file_stuff(const char *path)
264 sprintf(fn,"%s/%s",path,"f1");
265 create_file_of_size(fn,10000);
267 sprintf(fn,"%s/%s",path,"fdel");
268 create_file_of_size(fn,10000);
271 sprintf(fn,"%s/%s",path,"f2");
273 create_resized_file_of_size(fn,10000,3000,4000);
276 void yaffs_backward_scan_test(const char *path)
284 do_some_file_stuff(path);
286 sprintf(fn,"%s/ddd",path);
290 do_some_file_stuff(fn);
297 void null_name_test(const char *path)
305 sprintf(fn,"%s",path);
307 h = yaffs_open(fn,O_CREAT| O_TRUNC| O_RDWR, 0666);
316 void yaffs_device_flush_test(const char *path)
326 do_some_file_stuff(path);
328 // Open and add some data to a few files
329 for(i = 0; i < 10; i++) {
331 sprintf(fn,"%s/ff%d",path,i);
333 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
334 yaffs_write(h,xxzz,2000);
335 yaffs_write(h,xxzz,2000);
344 void short_scan_test(const char *path, int fsize, int niterations)
349 sprintf(fn,"%s/%s",path,"f1");
352 for(i = 0; i < niterations; i++)
354 printf("\n*****************\nIteration %d\n",i);
356 printf("\nmount: Directory look-up of %s\n",path);
358 make_a_file(fn,1,fsize);
365 void scan_pattern_test(const char *path, int fsize, int niterations)
372 sprintf(fn[0],"%s/%s",path,"f0");
373 sprintf(fn[1],"%s/%s",path,"f1");
374 sprintf(fn[2],"%s/%s",path,"f2");
378 for(i = 0; i < niterations; i++)
380 printf("\n*****************\nIteration %d\n",i);
382 printf("\nmount: Directory look-up of %s\n",path);
384 for(j = 0; j < 3; j++)
386 result = dump_file_data(fn[j]);
387 result = check_pattern_file(fn[j]);
388 make_pattern_file(fn[j],fsize);
389 result = dump_file_data(fn[j]);
390 result = check_pattern_file(fn[j]);
392 printf("result in line %d is %d", __LINE__, result);
398 void fill_disk(const char *path,int nfiles)
408 for(n = 0; n < nfiles; n++)
410 sprintf(str,"%s/%d",path,n);
412 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
414 printf("writing file %s handle %d ",str, h);
416 while ((result = yaffs_write(h,xx,600)) == 600)
418 f = yaffs_freespace(path);
420 printf("last freespace was %d\n", f);
421 result = yaffs_close(h);
422 printf(" close %d\n", result);
426 void fill_disk_and_delete(const char *path, int nfiles, int ncycles)
432 for(i = 0; i < ncycles; i++)
434 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
435 fill_disk(path,nfiles);
437 for(j = 0; j < nfiles; j++)
439 sprintf(str,"%s/%d",path,j);
440 result = yaffs_unlink(str);
441 printf("unlinking file %s, result %d\n", str, result);
447 void fill_files(const char *path,int flags, int maxIterations,int siz)
457 sprintf(str,"%s/%d",path,i);
458 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
462 for(j = 0; j < siz; j++)
464 yaffs_write(h,str,1);
473 } while(h >= 0 && i < maxIterations);
479 sprintf(str,"%s/%d",path,i);
480 printf("unlink %s\n",str);
482 } while(yaffs_unlink(str) >= 0);
486 void leave_unlinked_file(const char *path,int maxIterations,int siz)
495 sprintf(str,"%s/%d",path,i);
496 printf("create %s\n",str);
497 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
503 } while(h < 0 && i < maxIterations);
507 for(i = 0; i < siz; i++)
509 yaffs_write(h,str,1);
513 printf("Leaving file %s open\n",str);
517 void dumpDirFollow(const char *dname)
520 struct yaffs_dirent *de;
524 d = yaffs_opendir(dname);
528 printf("opendir failed\n");
532 while((de = yaffs_readdir(d)) != NULL)
534 sprintf(str,"%s/%s",dname,de->d_name);
538 printf("%s ino %d length %d mode %X ",
539 de->d_name, (int)s.st_ino, (int)s.st_size, s.st_mode);
540 switch(s.st_mode & S_IFMT)
542 case S_IFREG: printf("data file"); break;
543 case S_IFDIR: printf("directory"); break;
544 case S_IFLNK: printf("symlink -->");
545 if(yaffs_readlink(str,str,100) < 0)
548 printf("\"%s\"",str);
550 default: printf("unknown"); break;
560 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
565 void dump_directory_tree_worker(const char *dname,int recursive)
568 struct yaffs_dirent *de;
572 d = yaffs_opendir(dname);
576 printf("opendir failed\n");
580 while((de = yaffs_readdir(d)) != NULL)
582 sprintf(str,"%s/%s",dname,de->d_name);
586 printf("%s inode %d length %d mode %X ",
587 str, s.st_ino, (int)s.st_size, s.st_mode);
588 switch(s.st_mode & S_IFMT)
590 case S_IFREG: printf("data file"); break;
591 case S_IFDIR: printf("directory"); break;
592 case S_IFLNK: printf("symlink -->");
593 if(yaffs_readlink(str,str,100) < 0)
596 printf("\"%s\"",str);
598 default: printf("unknown"); break;
603 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
604 dump_directory_tree_worker(str,1);
613 static void dump_directory_tree(const char *dname)
615 dump_directory_tree_worker(dname,1);
617 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
620 void dump_directory_tree_worker_fd(const char *dname,int recursive)
623 struct yaffs_dirent *de;
627 h = yaffs_open(dname, O_RDONLY, 0);
631 printf("open of dir failed\n");
635 printf("using fd %d\n", h);
637 while((de = yaffs_readdir_fd(h)) != NULL)
639 sprintf(str,"%s/%s",dname,de->d_name);
643 printf("%s inode %d length %d mode %X ",
644 str,s.st_ino, (int)s.st_size, s.st_mode);
645 switch(s.st_mode & S_IFMT)
647 case S_IFREG: printf("data file"); break;
648 case S_IFDIR: printf("directory"); break;
649 case S_IFLNK: printf("symlink -->");
650 if(yaffs_readlink(str,str,100) < 0)
653 printf("\"%s\"",str);
655 default: printf("unknown"); break;
660 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
661 dump_directory_tree_worker_fd(str,1);
670 static void dump_directory_tree_fd(const char *dname)
672 dump_directory_tree_worker_fd(dname,1);
674 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
677 void dumpDir(const char *dname)
678 { dump_directory_tree_worker(dname,0);
680 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
684 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
688 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
690 fd = yaffs_open(path,tflags,0);
692 if((fd >= 0) != (expectedResult > 0))
694 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
698 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
707 int long_test(int argc, char *argv[])
718 struct yaffs_stat ystat;
725 yaffs_mount("/boot");
726 yaffs_mount("/data");
727 yaffs_mount("/flash");
730 printf("\nDirectory look-up of /boot\n");
732 printf("\nDirectory look-up of /data\n");
734 printf("\nDirectory look-up of /flash\n");
737 //leave_unlinked_file("/flash",20000,0);
738 //leave_unlinked_file("/data",20000,0);
740 leave_unlinked_file("/ram",20,0);
743 f = yaffs_open("/boot/b1", O_RDONLY,0);
745 printf("open /boot/b1 readonly, f=%d\n",f);
747 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
749 printf("open /boot/b1 O_CREAT, f=%d\n",f);
752 r = yaffs_write(f,"hello",1);
753 printf("write %d attempted to write to a read-only file\n",r);
757 printf("close %d\n",r);
759 f = yaffs_open("/boot/b1", O_RDWR,0);
761 printf("open /boot/b1 O_RDWR,f=%d\n",f);
764 r = yaffs_write(f,"hello",2);
765 printf("write %d attempted to write to a writeable file\n",r);
766 r = yaffs_write(f,"world",3);
767 printf("write %d attempted to write to a writeable file\n",r);
769 r= yaffs_lseek(f,0,SEEK_END);
770 printf("seek end %d\n",r);
772 r = yaffs_read(f,buffer,10);
773 printf("read %d \"%s\"\n",r,buffer);
774 r= yaffs_lseek(f,0,SEEK_SET);
775 printf("seek set %d\n",r);
777 r = yaffs_read(f,buffer,10);
778 printf("read %d \"%s\"\n",r,buffer);
780 r = yaffs_read(f,buffer,10);
781 printf("read %d \"%s\"\n",r,buffer);
783 // Check values reading at end.
784 // A read past end of file should return 0 for 0 bytes read.
786 r= yaffs_lseek(f,0,SEEK_END);
787 r = yaffs_read(f,buffer,10);
788 printf("read at end returned %d\n",r);
789 r= yaffs_lseek(f,500,SEEK_END);
790 r = yaffs_read(f,buffer,10);
791 printf("read past end returned %d\n",r);
795 printf("close %d\n",r);
797 copy_in_a_file("/boot/yyfile","xxx");
799 // Create a file with a long name
801 copy_in_a_file("/boot/file with a long name","xxx");
804 printf("\nDirectory look-up of /boot\n");
808 r = yaffs_lstat("/boot/file with a long name",&ystat);
812 r = yaffs_rename("/boot/file with a long name","/boot/r1");
814 printf("\nDirectory look-up of /boot\n");
818 r = yaffs_unlink("/boot/r1");
820 printf("\nDirectory look-up of /boot\n");
825 r = yaffs_mkdir("/boot/directory1",0);
827 printf("\nDirectory look-up of /boot\n");
829 printf("\nDirectory look-up of /boot/directory1\n");
830 dumpDir("/boot/directory1");
832 // add a file to the directory
833 copy_in_a_file("/boot/directory1/file with a long name","xxx");
835 printf("\nDirectory look-up of /boot\n");
837 printf("\nDirectory look-up of /boot/directory1\n");
838 dumpDir("/boot/directory1");
840 // Attempt to delete directory (should fail)
842 r = yaffs_rmdir("/boot/directory1");
844 printf("\nDirectory look-up of /boot\n");
846 printf("\nDirectory look-up of /boot/directory1\n");
847 dumpDir("/boot/directory1");
849 // Delete file first, then rmdir should work
850 r = yaffs_unlink("/boot/directory1/file with a long name");
851 r = yaffs_rmdir("/boot/directory1");
854 printf("\nDirectory look-up of /boot\n");
856 printf("\nDirectory look-up of /boot/directory1\n");
857 dumpDir("/boot/directory1");
860 fill_disk_and_delete("/boot",20,20);
862 printf("\nDirectory look-up of /boot\n");
866 yaffs_symlink("yyfile","/boot/slink");
868 yaffs_readlink("/boot/slink",str,100);
869 printf("symlink alias is %s\n",str);
874 printf("\nDirectory look-up of /boot\n");
876 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
877 dumpDirFollow("/boot");
878 printf("\nDirectory look-up of /boot/directory1\n");
879 dumpDir("/boot/directory1");
881 h = yaffs_open("/boot/slink",O_RDWR,0);
883 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
887 yaffs_unlink("/boot/slink");
890 printf("\nDirectory look-up of /boot\n");
895 yaffs_lstat("/boot/yyfile",&ystat);
896 temp_mode = ystat.st_mode;
898 yaffs_chmod("/boot/yyfile",0x55555);
899 printf("\nDirectory look-up of /boot\n");
902 yaffs_chmod("/boot/yyfile",temp_mode);
903 printf("\nDirectory look-up of /boot\n");
906 // Permission checks...
907 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
908 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
909 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
911 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
912 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
913 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
915 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
916 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
917 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
919 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
920 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
921 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
923 yaffs_chmod("/boot/yyfile",temp_mode);
925 //create a zero-length file and unlink it (test for scan bug)
927 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
930 yaffs_unlink("/boot/zlf");
933 yaffs_dump_dev("/boot");
935 fill_disk_and_delete("/boot",20,20);
937 yaffs_dump_dev("/boot");
939 fill_files("/boot",1,10000,0);
940 fill_files("/boot",1,10000,5000);
941 fill_files("/boot",2,10000,0);
942 fill_files("/boot",2,10000,5000);
944 leave_unlinked_file("/data",20000,0);
945 leave_unlinked_file("/data",20000,5000);
946 leave_unlinked_file("/data",20000,5000);
947 leave_unlinked_file("/data",20000,5000);
948 leave_unlinked_file("/data",20000,5000);
949 leave_unlinked_file("/data",20000,5000);
951 yaffs_dump_dev("/boot");
952 yaffs_dump_dev("/data");
960 int huge_directory_test_on_path(char *path)
964 struct yaffs_dirent *de;
980 // Create a large number of files
982 for(i = 0; i < 2000; i++)
984 sprintf(str,"%s/%d",path,i);
986 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
992 d = yaffs_opendir(path);
995 while((de = yaffs_readdir(d)) != NULL) {
996 if (total >lastTotal+100*9*1024||(i & 1023)==0){
997 printf("files = %d, total = %d\n",i, total);
1001 sprintf(str,"%s/%s",path,de->d_name);
1002 yaffs_lstat(str,&s);
1003 switch(s.st_mode & S_IFMT){
1005 //printf("data file");
1017 int yaffs_scan_test(const char *path)
1025 void rename_over_test(const char *mountpt)
1032 sprintf(a,"%s/a",mountpt);
1033 sprintf(b,"%s/b",mountpt);
1034 sprintf(c,"%s/c",mountpt);
1038 yaffs_mount(mountpt);
1040 printf("Existing files\n");
1041 dumpDirFollow(mountpt);
1045 i = yaffs_open(c,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1046 printf("File c handle is %d\n",i);
1048 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1050 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1052 yaffs_rename(a,b); // rename over
1053 yaffs_rename(b,a); // rename back again (not renaimng over)
1054 yaffs_rename(a,b); // rename back again (not renaimng over)
1057 yaffs_unmount(mountpt);
1062 int resize_stress_test(const char *path)
1077 sprintf(aname,"%s%s",path,"/a");
1078 sprintf(bname,"%s%s",path,"/b");
1080 memset(abuffer,'a',1000);
1081 memset(bbuffer,'b',1000);
1083 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1084 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1086 printf(" %s %d %s %d\n",aname,a,bname,b);
1090 for(j = 0; j < 100; j++)
1092 yaffs_lseek(a,0,SEEK_END);
1095 for(i = 0; i <20000; i++)
1097 //r = yaffs_lseek(b,i,SEEK_SET);
1098 //r = yaffs_write(b,bbuffer,1000);
1103 int syz = yaffs_lseek(a,0,SEEK_END);
1106 if(syz < 0) syz = 0;
1107 yaffs_ftruncate(a,syz);
1113 r = yaffs_lseek(a,i * 500,SEEK_SET);
1115 printf("At line %d, r is %d\n", __LINE__, r);
1116 r = yaffs_write(a,abuffer,1000);
1118 printf("At line %d, r is %d\n", __LINE__, r);
1130 int overwrite_test(const char *path)
1142 sprintf(aname,"%s%s",path,"/a");
1143 sprintf(bname,"%s%s",path,"/b");
1145 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1146 for(j= 0; j < 500; j++){
1147 yaffs_write(b,bname,100);
1148 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1149 for(i = 0; i < rand() % 20000; i++)
1150 yaffs_write(a,&a,sizeof(a));
1159 int root_perm_remount(const char *path)
1161 struct yaffs_stat s;
1167 yaffs_lstat(path,&s);
1168 printf("root perms after mount %x\n",s.st_mode);
1170 yaffs_chmod(path, 0777);
1172 yaffs_lstat(path,&s);
1173 printf("root perms after setting to 0777 is %x\n",s.st_mode);
1175 yaffs_unmount(path);
1182 int resize_stress_test_no_grow_complex(const char *path,int iters)
1198 sprintf(aname,"%s%s",path,"/a");
1199 sprintf(bname,"%s%s",path,"/b");
1201 memset(abuffer,'a',1000);
1202 memset(bbuffer,'b',1000);
1204 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1205 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1207 printf(" %s %d %s %d\n",aname,a,bname,b);
1211 for(j = 0; j < iters; j++)
1213 yaffs_lseek(a,0,SEEK_END);
1216 for(i = 0; i <20000; i++)
1218 //r = yaffs_lseek(b,i,SEEK_SET);
1219 //r = yaffs_write(b,bbuffer,1000);
1224 int syz = yaffs_lseek(a,0,SEEK_END);
1230 if(syz < 0) syz = 0;
1231 yaffs_ftruncate(a,syz);
1232 syz = yaffs_lseek(a,0,SEEK_END);
1233 printf("shrink to %d\n",syz);
1241 r = yaffs_lseek(a,500,SEEK_END);
1242 r = yaffs_write(a,abuffer,1000);
1244 printf("At line %d, r is %d\n", __LINE__, r);
1251 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1259 int resize_stress_test_no_grow(const char *path,int iters)
1274 sprintf(aname,"%s%s",path,"/a");
1275 sprintf(bname,"%s%s",path,"/b");
1277 memset(abuffer,'a',1000);
1278 memset(bbuffer,'b',1000);
1280 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1281 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1283 printf(" %s %d %s %d\n",aname,a,bname,b);
1287 for(j = 0; j < iters; j++)
1289 yaffs_lseek(a,0,SEEK_END);
1292 for(i = 0; i <20000; i++)
1294 //r = yaffs_lseek(b,i,SEEK_SET);
1295 //r = yaffs_write(b,bbuffer,1000);
1300 int syz = yaffs_lseek(a,0,SEEK_END);
1306 if(syz < 0) syz = 0;
1307 yaffs_ftruncate(a,syz);
1308 syz = yaffs_lseek(a,0,SEEK_END);
1309 printf("shrink to %d\n",syz);
1317 r = yaffs_lseek(a,-500,SEEK_END);
1318 r = yaffs_write(a,abuffer,1000);
1320 printf("At line %d, r is %d\n", __LINE__, r);
1326 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1334 int directory_rename_test(void)
1339 yaffs_mount("/ram");
1340 yaffs_mkdir("/ram/a",0);
1341 yaffs_mkdir("/ram/a/b",0);
1342 yaffs_mkdir("/ram/c",0);
1344 printf("\nDirectory look-up of /ram\n");
1347 dumpDir("/ram/a/b");
1349 printf("Do rename (should fail)\n");
1351 r = yaffs_rename("/ram/a","/ram/a/b/d");
1353 printf("At line %d, r is %d\n", __LINE__, r);
1355 printf("\nDirectory look-up of /ram\n");
1358 dumpDir("/ram/a/b");
1360 printf("Do rename (should not fail)\n");
1362 r = yaffs_rename("/ram/c","/ram/a/b/d");
1363 printf("\nDirectory look-up of /ram\n");
1366 dumpDir("/ram/a/b");
1373 int cache_read_test(void)
1377 int sizeOfFiles = 500000;
1382 yaffs_mount("/boot");
1384 make_a_file("/boot/a",'a',sizeOfFiles);
1385 make_a_file("/boot/b",'b',sizeOfFiles);
1387 a = yaffs_open("/boot/a",O_RDONLY,0);
1388 b = yaffs_open("/boot/b",O_RDONLY,0);
1389 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1393 if (i > 100) i = 100;
1395 yaffs_read(a,buffer,i);
1396 yaffs_read(b,buffer,i);
1397 yaffs_write(c,buffer,i);
1398 } while(sizeOfFiles > 0);
1406 int cache_bypass_bug_test(void)
1408 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1409 // resulting in erroneous reads after writes.
1410 // This bug has been fixed.
1416 memset(buffer1,0,sizeof(buffer1));
1417 memset(buffer2,0,sizeof(buffer2));
1421 yaffs_mount("/boot");
1423 // Create a file of 2000 bytes.
1424 make_a_file("/boot/a",'X',2000);
1426 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1428 // Write a short sequence to the file.
1429 // This will go into the cache.
1430 yaffs_lseek(a,0,SEEK_SET);
1431 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1433 // Read a short sequence from the file.
1434 // This will come from the cache.
1435 yaffs_lseek(a,0,SEEK_SET);
1436 yaffs_read(a,buffer1,30);
1438 // Read a page size sequence from the file.
1439 yaffs_lseek(a,0,SEEK_SET);
1440 yaffs_read(a,buffer2,512);
1442 printf("buffer 1 %s\n",buffer1);
1443 printf("buffer 2 %s\n",buffer2);
1445 if(strncmp(buffer1,buffer2,20))
1447 printf("Cache bypass bug detected!!!!!\n");
1455 int free_space_check(void)
1460 yaffs_mount("/boot");
1461 fill_disk("/boot/",2);
1462 f = yaffs_freespace("/boot");
1464 printf("%d free when disk full\n",f);
1468 int truncate_test(void)
1478 yaffs_mount("/boot");
1480 yaffs_unlink("/boot/trunctest");
1482 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1484 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1486 yaffs_ftruncate(a,3);
1487 l= yaffs_lseek(a,0,SEEK_END);
1489 printf("truncated length is %d\n",l);
1491 yaffs_lseek(a,5,SEEK_SET);
1492 yaffs_write(a,"1",1);
1494 yaffs_lseek(a,0,SEEK_SET);
1496 r = yaffs_read(a,y,10);
1498 printf("read %d bytes:", r);
1500 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1512 void fill_disk_test(const char *mountpt)
1517 for(i = 0; i < 5; i++)
1519 yaffs_mount(mountpt);
1520 fill_disk_and_delete(mountpt,100,i+1);
1521 yaffs_unmount(mountpt);
1527 void fill_files_test(const char *mountpt)
1532 for(i = 0; i < 5; i++)
1534 yaffs_mount(mountpt);
1535 fill_files(mountpt,2,3,100);
1536 yaffs_unmount(mountpt);
1541 void fill_empty_files_test(const char *mountpt)
1550 for(i = 0; i < 5; i++)
1552 yaffs_mount(mountpt);
1553 for(d = 0; result >= 0 && d < 1000; d++){
1554 sprintf(name,"%s/%d",mountpt,d);
1555 result= yaffs_mkdir(name,0);
1556 printf("creating directory %s result %d\n",name,result);
1558 for(f = 0; result >= 0 && f < 100; f++){
1559 sprintf(name,"%s/%d/%d",mountpt,d,f);
1560 result= yaffs_open(name,O_CREAT, 0);
1561 yaffs_close(result);
1562 printf("creating file %s result %d\n",name,result);
1565 yaffs_unmount(mountpt);
1570 void long_name_test(const char *mountpt)
1574 char fullName[1000];
1580 // Make a 256 byte name
1581 memset(name,0,sizeof(name));
1582 for(i = 0; i < 256; i++)
1583 name[i] = '0' + i % 10;
1585 sprintf(fullName,"%s/%s",mountpt,name);
1587 for(i = 0; i < 1; i++)
1589 yaffs_mount(mountpt);
1591 printf("Files at start\n");
1594 printf("Creating file %s\n",fullName);
1596 f = yaffs_open(fullName,O_CREAT | O_RDWR,0);
1599 printf("Result %d\n",f);
1604 printf("Deleting %s\n",fullName);
1605 result = yaffs_unlink(fullName);
1606 printf("Result %d\n",result);
1612 yaffs_unmount(mountpt);
1618 void lookup_test(const char *mountpt)
1626 struct yaffs_dirent *de;
1630 yaffs_mount(mountpt);
1632 d = yaffs_opendir(mountpt);
1636 printf("opendir failed\n");
1641 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1643 printf("unlinking %s\n",de->d_name);
1644 yaffs_unlink(de->d_name);
1647 printf("%d files deleted\n",i);
1651 for(i = 0; i < 2000; i++){
1652 sprintf(a,"%s/%d",mountpt,i);
1653 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1658 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1660 printf("%d %s\n",i,de->d_name);
1663 printf("%d files listed\n\n\n",i);
1670 for(i = 0; i < 2000; i++){
1671 sprintf(a,"%s/%d",mountpt,i);
1676 yaffs_unmount(mountpt);
1680 void link_test0(const char *mountpt)
1688 yaffs_mount(mountpt);
1691 sprintf(namea,"%s/a",mountpt);
1692 sprintf(nameb,"%s/b",mountpt);
1694 printf("mounted\n");
1697 yaffs_unlink(namea);
1698 printf("a unlinked\n");
1701 yaffs_unlink(nameb);
1702 printf("b unlinked\n");
1705 result = yaffs_open(namea,O_CREAT| O_RDWR,0666);
1706 yaffs_close(result);
1707 printf("a created\n");
1710 yaffs_link(namea,nameb);
1713 yaffs_unlink(namea);
1714 printf("a ulinked\n");
1716 yaffs_unlink(nameb);
1717 printf("b unlinked\n");
1720 yaffs_unmount(mountpt);
1724 void link_test1(const char *mountpt)
1732 sprintf(a,"%s/aaa",mountpt);
1733 sprintf(b,"%s/bbb",mountpt);
1734 sprintf(c,"%s/ccc",mountpt);
1738 yaffs_mount(mountpt);
1741 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1742 for(i = 0; i < 100; i++)
1743 yaffs_write(h,a,100);
1756 yaffs_unmount(mountpt);
1757 yaffs_mount(mountpt);
1759 printf("link test done\n");
1762 void handle_test(const char *mountpt)
1769 sprintf(a,"%s/aaa",mountpt);
1773 yaffs_mount(mountpt);
1775 for(cycle = 0; cycle < 5; cycle++){
1776 printf("Start cycle %d\n",cycle);
1779 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1780 printf("%d handle %d\n",i,h);
1790 yaffs_unmount(mountpt);
1793 void freespace_test(const char *mountpt)
1803 sprintf(a,"%s/aaa",mountpt);
1807 yaffs_mount(mountpt);
1809 f0 = yaffs_freespace(mountpt);
1811 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1813 for(i = 0; i < 100; i++)
1814 yaffs_write(h,a,100);
1818 f1 = yaffs_freespace(mountpt);
1822 f2 = yaffs_freespace(mountpt);
1825 yaffs_unmount(mountpt);
1826 yaffs_mount(mountpt);
1828 f3 = yaffs_freespace(mountpt);
1830 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1835 void simple_rw_test(const char *mountpt)
1844 sprintf(a,"%s/aaa",mountpt);
1848 yaffs_mount(mountpt);
1852 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1854 for(i = 100000;i < 200000; i++){
1855 result = yaffs_write(h,&i,sizeof(i));
1859 printf("write error\n");
1866 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1869 yaffs_lseek(h,0,SEEK_SET);
1871 for(i = 100000; i < 200000; i++){
1872 result = yaffs_read(h,&x,sizeof(x));
1874 if(result != 4 || x != i){
1875 printf("read error %d %x %x\n",i,result,x);
1879 printf("Simple rw test passed\n");
1886 void scan_deleted_files_test(const char *mountpt)
1898 sprintf(sub,"%s/sdir",mountpt);
1901 for(j = 0; j < 10; j++)
1903 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1904 yaffs_mount(mountpt);
1908 p = (j & 0) ? mountpt: sub;
1910 for(i = 0; i < 100; i++)
1912 sprintf(fn,"%s/%d",p,i);
1916 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1917 for(k = 0; k < 1000; k++)
1918 yaffs_write(h,fn,100);
1925 for(i = 0; i < 10; i++)
1927 sprintf(fn,"%s/%d",p,i);
1935 yaffs_unmount(mountpt);
1944 void write_10k(int h)
1947 const char *s="0123456789";
1948 for(i = 0; i < 1000; i++)
1949 yaffs_write(h,s,10);
1952 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1958 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1960 for(i = 0; i < 100000; i+= 10000)
1965 offs = yaffs_lseek(h1,0,SEEK_CUR);
1968 printf("Could not write file\n");
1972 for(i = 0; i < 100000; i+= 10000)
1977 offs = yaffs_lseek(h1,0,SEEK_CUR);
1980 printf("Could not write file\n");
1984 yaffs_unlink(fdel1);
1989 void verify_200k_file(const char *fn)
1994 const char *s="0123456789";
1997 h1 = yaffs_open(fn, O_RDONLY, 0);
1999 for(i = 0; i < 200000 && errCount < 10; i+= 10)
2001 yaffs_read(h1,x,10);
2002 if(strncmp(x,s,10) != 0)
2004 printf("File %s verification failed at %d\n",fn,i);
2009 printf("Too many errors... aborted\n");
2016 void check_resize_gc_bug(const char *mountpt)
2025 sprintf(a,"%s/a",mountpt);
2026 sprintf(b,"%s/b",mountpt);
2027 sprintf(c,"%s/c",mountpt);
2033 yaffs_mount(mountpt);
2037 for(i = 0; i < 50; i++)
2039 printf("A\n");write_200k_file(a,"",c);
2040 printf("B\n");verify_200k_file(a);
2041 printf("C\n");write_200k_file(b,a,c);
2042 printf("D\n");verify_200k_file(b);
2043 yaffs_unmount(mountpt);
2044 yaffs_mount(mountpt);
2045 printf("E\n");verify_200k_file(a);
2046 printf("F\n");verify_200k_file(b);
2052 void multi_mount_test(const char *mountpt,int nmounts)
2060 sprintf(a,"%s/a",mountpt);
2064 for(i = 0; i < nmounts; i++){
2069 printf("############### Iteration %d Start\n",i);
2070 if(1 || i == 0 || i == 5)
2071 yaffs_mount(mountpt);
2073 dump_directory_tree(mountpt);
2078 sprintf(xx,"%s/0",a);
2079 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2081 sprintf(xx,"%s/1",a);
2082 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2085 for(j = 0; j < 200; j++){
2086 yaffs_write(h0,xx,1000);
2087 yaffs_write(h1,xx,1000);
2090 while(yaffs_write(h0,xx,1000) > 0){
2092 yaffs_write(h1,xx,1000);
2095 yaffs_lseek(h0,0,SEEK_END);
2096 yaffs_lseek(h1,0,SEEK_END);
2098 yaffs_lseek(h0,0,SEEK_SET);
2099 yaffs_lseek(h1,0,SEEK_SET);
2101 for(j = 0; j < 200; j++){
2102 yaffs_read(h0,xx,1000);
2103 yaffs_read(h1,xx,1000);
2107 // yaffs_truncate(h0,0);
2111 printf("########### %d\n",i);
2112 dump_directory_tree(mountpt);
2114 if(1 || i == 4 || i == nmounts -1)
2115 yaffs_unmount(mountpt);
2120 void small_mount_test(const char *mountpt,int nmounts)
2132 sprintf(a,"%s/a",mountpt);
2138 for(i = 0; i < nmounts; i++){
2142 printf("############### Iteration %d Start\n",i);
2143 if(1 || i == 0 || i == 5)
2144 yaffs_mount(mountpt);
2146 dump_directory_tree(mountpt);
2150 sprintf(xx,"%s/0",a);
2153 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2154 for(j = 0; j < 130; j++)
2155 yaffs_write(h0,xx,1000);
2159 h0 = yaffs_open(xx,O_RDONLY,0);
2161 sprintf(xx,"%s/1",a);
2162 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2164 while((nread = yaffs_read(h0,xx,1000)) > 0)
2165 yaffs_write(h1,xx,nread);
2168 yaffs_lseek(h0,0,SEEK_END);
2169 yaffs_lseek(h1,0,SEEK_END);
2171 yaffs_lseek(h0,0,SEEK_SET);
2172 yaffs_lseek(h1,0,SEEK_SET);
2174 for(j = 0; j < 200; j++){
2175 yaffs_read(h0,xx,1000);
2176 yaffs_read(h1,xx,1000);
2182 printf("########### %d\n",i);
2183 dump_directory_tree(mountpt);
2185 if(1 || i == 4 || i == nmounts -1)
2186 yaffs_unmount(mountpt);
2193 void small_overwrite_test(const char *mountpt,int nmounts)
2204 sprintf(a,"%s/a",mountpt);
2210 for(i = 0; i < nmounts; i++){
2212 static char xx[8000];
2214 printf("############### Iteration %d Start\n",i);
2216 yaffs_mount(mountpt);
2218 dump_directory_tree(mountpt);
2222 sprintf(xx,"%s/0",a);
2223 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2224 sprintf(xx,"%s/1",a);
2225 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2227 for(j = 0; j < 1000000; j+=1000){
2228 yaffs_ftruncate(h0,j);
2229 yaffs_lseek(h0,j,SEEK_SET);
2230 yaffs_write(h0,xx,7000);
2231 yaffs_write(h1,xx,7000);
2239 printf("########### %d\n",i);
2240 dump_directory_tree(mountpt);
2243 yaffs_unmount(mountpt);
2248 void seek_overwrite_test(const char *mountpt,int nmounts)
2250 static char xx[5000];
2259 sprintf(a,"%s/f",mountpt);
2263 yaffs_mount(mountpt);
2266 for(i = 0; i < nmounts; i++){
2268 h0 = yaffs_open(a, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2270 for(j = 0; j < 100000; j++){
2271 yaffs_lseek(h0,0,SEEK_SET);
2272 yaffs_write(h0,xx,5000);
2273 yaffs_lseek(h0,0x100000,SEEK_SET);
2274 yaffs_write(h0,xx,5000);
2286 void yaffs_touch(const char *fn)
2288 yaffs_chmod(fn, S_IREAD | S_IWRITE);
2291 void checkpoint_fill_test(const char *mountpt,int nmounts)
2302 sprintf(a,"%s/a",mountpt);
2309 for(i = 0; i < nmounts; i++){
2310 printf("############### Iteration %d Start\n",i);
2311 yaffs_mount(mountpt);
2312 dump_directory_tree(mountpt);
2315 sprintf(b,"%s/zz",a);
2317 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
2320 while(yaffs_write(h,c,50) == 50){}
2324 for(j = 0; j < 2; j++){
2325 printf("touch %d\n",j);
2327 yaffs_unmount(mountpt);
2328 yaffs_mount(mountpt);
2331 dump_directory_tree(mountpt);
2332 yaffs_unmount(mountpt);
2337 int make_file2(const char *name1, const char *name2,int syz)
2347 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2349 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2351 while(syz > 0 && n > 0){
2352 i = (syz > 2500) ? 2500 : syz;
2353 n = yaffs_write(h1,xx,i);
2354 n = yaffs_write(h2,xx,i);
2363 extern void SetCheckpointReservedBlocks(int n);
2365 void checkpoint_upgrade_test(const char *mountpt, int nmounts)
2377 sprintf(a,"%s/a",mountpt);
2379 printf("Create start condition\n");
2381 yaffs_mount(mountpt);
2383 sprintf(b,"%s/zz",a);
2384 sprintf(c,"%s/xx",a);
2385 make_file2(b,c,2000000);
2386 sprintf(d,"%s/aa",a);
2387 make_file2(d,NULL,500000000);
2388 dump_directory_tree(mountpt);
2390 printf("Umount/mount attempt full\n");
2391 yaffs_unmount(mountpt);
2393 yaffs_mount(mountpt);
2395 printf("unlink small file\n");
2397 dump_directory_tree(mountpt);
2399 printf("Umount/mount attempt\n");
2400 yaffs_unmount(mountpt);
2401 yaffs_mount(mountpt);
2403 for(j = 0; j < 500; j++){
2404 printf("***** touch %d\n",j);
2405 dump_directory_tree(mountpt);
2407 yaffs_unmount(mountpt);
2408 yaffs_mount(mountpt);
2411 for(j = 0; j < 500; j++){
2412 printf("***** touch %d\n",j);
2413 dump_directory_tree(mountpt);
2415 yaffs_unmount(mountpt);
2416 yaffs_mount(mountpt);
2420 void huge_array_test(const char *mountpt,int n)
2431 sprintf(a,"mount point %s",mountpt);
2437 yaffs_mount(mountpt);
2442 printf("\n\n START run\n\n");
2443 while((space = yaffs_freespace(mountpt)) > 25000000){
2444 sprintf(a,"%s/file%d",mountpt,fnum);
2446 printf("create file %s, free space %d\n",a,space);
2447 create_file_of_size(a,10000000);
2448 printf("verifying file %s\n",a);
2449 verify_file_of_size(a,10000000);
2452 printf("\n\n verification/deletion\n\n");
2454 for(i = 0; i < fnum; i++){
2455 sprintf(a,"%s/file%d",mountpt,i);
2456 printf("verifying file %s\n",a);
2457 verify_file_of_size(a,10000000);
2458 printf("deleting file %s\n",a);
2461 printf("\n\n done \n\n");
2468 void random_write(int h)
2470 static char buffer[12000];
2473 n = random() & 0x1FFF;
2474 yaffs_write(h,buffer,n);
2477 void random_seek(int h)
2480 n = random() & 0xFFFFF;
2481 yaffs_lseek(h,n,SEEK_SET);
2484 void random_truncate(int h, char *name)
2491 n = random() & 0xFFFFF;
2492 flen = yaffs_lseek(h,0,SEEK_END);
2495 yaffs_ftruncate(h,n);
2496 yaffs_lseek(h,n,SEEK_SET);
2500 #define NSMALLFILES 10
2501 void random_small_file_test(const char *mountpt,int iterations)
2504 char a[NSMALLFILES][50];
2515 yaffs_mount(mountpt);
2517 for(i = 0; i < NSMALLFILES; i++){
2522 for(n = 0; n < iterations; n++){
2524 for(i = 0; i < NSMALLFILES; i++) {
2527 if(strlen(a[i]) == 0){
2528 sprintf(a[i],"%s/%dx%d",mountpt,n,i);
2529 h[i] = yaffs_open(a[i],O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2533 printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
2543 random_truncate(h[i],a[i]);
2546 case 5: random_seek(h[i]);
2563 for(i = 0; i < NSMALLFILES; i++)
2566 yaffs_unmount(mountpt);
2569 void rmdir_test(const char *mountpt)
2574 yaffs_mount(mountpt);
2576 strcpy(name,mountpt);
2578 strcat(name,"hello");
2579 yaffs_mkdir(name,0666);
2581 yaffs_unmount(mountpt);
2586 static void print_xattrib_val(const char *path, const char *name)
2591 n = yaffs_getxattr(path,name,buffer,sizeof(buffer));
2593 u8 *b = (u8 *)buffer;
2595 printf("%d bytes:",n);
2599 printf("[%02X]",*b);
2605 printf(" Novalue result %d\n",n);
2608 static void list_xattr(const char *path)
2615 list_len = yaffs_listxattr(path,list,sizeof(list));
2616 printf("xattribs for %s, result is %d\n",path,list_len);
2617 while(n < list_len){
2618 len = strlen(list + n);
2619 printf("\"%s\" value ",list+n);
2620 print_xattrib_val(path,list + n);
2626 void basic_utime_test(const char *mountpt)
2631 struct yaffs_utimbuf utb;
2632 struct yaffs_stat st;
2636 yaffs_mount(mountpt);
2638 strcpy(name,mountpt);
2640 strcat(name,"xfile");
2644 printf("created\n");
2645 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2648 printf(" times %lu %lu %lu\n",
2649 st.yst_atime, st.yst_ctime, st.yst_mtime);
2653 result = yaffs_futime(h,&utb);
2654 printf("futime to a 1000 m 2000 result %d\n",result);
2656 printf(" times %lu %lu %lu\n",
2657 st.yst_atime, st.yst_ctime, st.yst_mtime);
2662 result = yaffs_utime(name, &utb);
2663 printf("utime to a 5000 m 8000 result %d\n",result);
2665 printf(" times %lu %lu %lu\n",
2666 st.yst_atime, st.yst_ctime, st.yst_mtime);
2668 result = yaffs_utime(name, NULL);
2669 printf("utime to NULL result %d\n",result);
2671 printf(" times %lu %lu %lu\n",
2672 st.yst_atime, st.yst_ctime, st.yst_mtime);
2677 void basic_xattr_test(const char *mountpt)
2686 yaffs_mount(mountpt);
2688 strcpy(name,mountpt);
2690 strcat(name,"xfile");
2693 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2699 printf("Add an attribute\n");
2701 result = yaffs_setxattr(name,"foo",&val1,sizeof(val1),0);
2702 printf("wrote attribute foo: result %d\n",result);
2704 printf("Add an attribute\n");
2706 result = yaffs_setxattr(name,"bar",&val1,sizeof(val1),0);
2707 printf("wrote attribute bar: result %d\n",result);
2710 printf("Get non-existanrt attribute\n");
2711 print_xattrib_val(name,"not here");
2713 printf("Delete non existing attribute\n");
2714 yaffs_removexattr(name,"not here");
2717 printf("Remove foo\n");
2718 yaffs_removexattr(name,"foo");
2721 printf("Remove bar\n");
2722 yaffs_removexattr(name,"bar");
2727 void big_xattr_test(const char *mountpt)
2736 yaffs_mount(mountpt);
2738 strcpy(name,mountpt);
2740 strcat(name,"xfile");
2743 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2749 printf("Add a large attribute\n");
2750 memset(val,0x1,sizeof(val));
2751 result = yaffs_setxattr(name,"aaa",val,200,0);
2752 printf("wrote attribute aaa: result %d\n",result);
2755 printf("Add a large attribute\n");
2756 memset(val,0x2,sizeof(val));
2757 result = yaffs_setxattr(name,"bbb",val,1000,0);
2758 printf("wrote attribute bbb: result %d\n",result);
2761 printf("Replace attribute\n");
2762 memset(val,0x3,sizeof(val));
2763 result = yaffs_setxattr(name,"aaa",val,1000,0);
2764 printf("wrote attribute aaa: result %d\n",result);
2770 void dump_dev_stats(struct yaffs_dev *dev, const char * str)
2773 printf( "space free %d erased %d "
2774 "nand reads %d writes %d erases %d "
2775 "gc all %d passive %d oldestdirty %d blocks %d copies %d \n",
2776 dev->n_free_chunks, dev->n_erased_blocks * dev->param.chunks_per_block,
2777 dev->n_page_reads, dev->n_page_writes, dev->n_erasures,
2778 dev->all_gcs, dev->passive_gc_count, dev->oldest_dirty_gc_count, dev->n_gc_blocks, dev->n_gc_copies);
2781 void test_flash_traffic(const char *mountpt)
2786 struct yaffs_dev *dev;
2788 yaffs_trace_mask = 0;
2792 yaffs_mount(mountpt);
2794 dev = yaffs_getdev(mountpt);
2796 strcpy(name0,mountpt);
2799 strcpy(name1,mountpt);
2802 dump_dev_stats(dev,"start");
2803 create_file_of_size(name0,32 * 1024 * 1024);
2804 dump_dev_stats(dev,"32MB written");
2805 for(i = 0; i < 20; i++)
2806 create_file_of_size(name1,1024 * 1024);
2807 dump_dev_stats(dev,"20x 1MB files written");
2811 void link_follow_test(const char *mountpt)
2819 yaffs_trace_mask = 0;
2823 yaffs_mount(mountpt);
2825 sprintf(fn,"%s/file",mountpt);
2826 sprintf(sn,"%s/sym",mountpt);
2827 sprintf(hn,"%s/hl-sym",mountpt);
2829 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2830 result = yaffs_close(h);
2832 result = yaffs_symlink(fn,sn);
2833 result = yaffs_link(sn,hn);
2836 printf("At line %d result is %d\n", __LINE__, result);
2838 h =yaffs_open(hn,O_RDWR,0);
2842 void max_files_test(const char *mountpt)
2850 yaffs_trace_mask = 0;
2854 yaffs_mount(mountpt);
2856 for(i = 0; i < 5000; i++) {
2857 sprintf(fn,"%s/file%d", mountpt, i);
2859 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2861 printf("File %s not created\n", fn);
2862 yaffs_write(h,fn,100);
2863 result = yaffs_close(h);
2865 for(i = 0; i < 5; i++){
2866 sprintf(fn,"%s/file%d",mountpt, i);
2870 for(i = 1000; i < 1010; i++){
2871 sprintf(fn,"%s/file%d",mountpt, i);
2872 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2873 yaffs_write(h,fn,100);
2875 printf("File %s not created\n", fn);
2876 result = yaffs_close(h);
2880 printf("At line %d result is %d\n", __LINE__, result);
2882 yaffs_unmount(mountpt);
2885 void case_insensitive_test(const char *mountpt)
2891 struct yaffs_stat s;
2895 yaffs_trace_mask = 0;
2899 yaffs_mount(mountpt);
2900 dump_directory_tree(mountpt);
2902 sprintf(fn,"%s/Abc.Txt",mountpt);
2904 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2906 ret = yaffs_write(h,fn, strlen(fn) + 1);
2908 ret = yaffs_close(h);
2910 dump_directory_tree(mountpt);
2920 h = yaffs_open(fn2, O_RDONLY, 0);
2921 ret = yaffs_read(h, buffer, 100);
2923 if (ret != (int)(strlen(fn) + 1) || memcmp(buffer, fn, ret)){
2924 printf("wrong file read\n");
2926 printf("File %s is the same as file %s\n", fn, fn2);
2929 ret = yaffs_stat(fn2, &s);
2931 printf("renaming\n");
2933 ret = yaffs_rename(fn, fn2);
2934 dump_directory_tree(mountpt);
2938 void start_twice(const char *mountpt)
2940 printf("About to do first yaffs_start\n");
2942 printf("started\n");
2943 printf("First mount returns %d\n", yaffs_mount(mountpt));
2944 printf("About to do second yaffs_start\n");
2946 printf("started\n");
2947 printf("Second mount returns %d\n", yaffs_mount(mountpt));
2950 #define N_WRITES 2000
2953 #define BUFFER_N 1100
2954 unsigned xxbuffer[BUFFER_N];
2956 void set_buffer(int n)
2959 for(i = 0; i < BUFFER_N; i++)
2960 xxbuffer[i] = i + n;
2963 void write_big_sparse_file(int h)
2968 int n = sizeof(xxbuffer);
2971 for(i = 0; i < N_WRITES; i++) {
2972 printf("writing at %d\n", (int)offset);
2974 pos = yaffs_lseek(h, offset, SEEK_SET);
2976 printf("mismatched seek pos %d offset %d\n",
2977 (int)pos, (int)offset);
2981 wrote = yaffs_write(h, xxbuffer, n);
2984 printf("mismatched write wrote %d n %d\n", wrote, n);
2988 offset += (STRIDE * sizeof(xxbuffer));
2991 yaffs_ftruncate(h, offset);
2995 void verify_big_sparse_file(int h)
2997 unsigned check_buffer[BUFFER_N];
3001 int n = sizeof(check_buffer);
3003 int checks_failed = 0;
3004 int checks_passed = 0;
3006 for(i = 0; i < N_WRITES * STRIDE; i++) {
3008 memset(xxbuffer,0, n);
3010 set_buffer(i/STRIDE);
3012 pos = yaffs_lseek(h, offset, SEEK_SET);
3014 printf("mismatched seek pos %d offset %d\n",
3015 (int)pos, (int)offset);
3019 result = yaffs_read(h, check_buffer, n);
3022 printf("mismatched read result %d n %d\n", result, n);
3026 if(memcmp(xxbuffer, check_buffer, n)) {
3029 printf("buffer at %d mismatches\n", (int)pos);
3030 printf("xxbuffer ");
3031 for(j = 0; j < 20; j++)
3032 printf(" %d",xxbuffer[j]);
3034 printf("check_buffer ");
3035 for(j = 0; j < 20; j++)
3036 printf(" %d",check_buffer[j]);
3044 offset += sizeof(xxbuffer);
3047 printf("%d checks passed, %d checks failed\n", checks_passed, checks_failed);
3052 void large_file_test(const char *mountpt)
3054 char xx_buffer[1000];
3060 yaffs_trace_mask = 0;
3064 yaffs_mount(mountpt);
3065 printf("mounted\n");
3068 sprintf(fullname, "%s/%s", mountpt, "big-test-file");
3070 handle = yaffs_open(fullname, O_RDONLY, 0);
3072 handle = yaffs_open(fullname, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
3075 perror("opening file");
3079 write_big_sparse_file(handle);
3080 verify_big_sparse_file(handle);
3082 yaffs_close(handle);
3084 printf("Job done\n");
3085 yaffs_unmount(mountpt);
3087 yaffs_mount(mountpt);
3088 printf("mounted again\n");
3090 handle = yaffs_open(fullname, O_RDONLY, 0);
3091 verify_big_sparse_file(handle);
3092 yaffs_unmount(mountpt);
3095 yaffs_mount3(mountpt, 0, 1);
3096 printf("mounted with no checkpt\n");
3098 handle = yaffs_open(fullname, O_RDONLY, 0);
3099 verify_big_sparse_file(handle);
3100 yaffs_unmount(mountpt);
3102 /* Check resize by adding to the end, resizing back and verifying. */
3103 yaffs_mount3(mountpt, 0, 1);
3104 printf("checking resize\n");
3106 handle = yaffs_open(fullname, O_RDWR, 0);
3108 file_end = yaffs_lseek(handle, 0, SEEK_END);
3109 printf("file_end %d\n", (int)file_end);
3110 for(i = 0; i < 10000; i++)
3111 yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
3112 yaffs_ftruncate(handle, file_end);
3114 verify_big_sparse_file(handle);
3115 yaffs_unmount(mountpt);
3120 int mk_dir(const char *mp, const char *name)
3122 char full_name[100];
3124 sprintf(full_name, "%s/%s", mp, name);
3126 return yaffs_mkdir(full_name, S_IREAD| S_IWRITE);
3129 int mk_file(const char *mp, const char *name)
3131 char full_name[100];
3134 sprintf(full_name, "%s/%s", mp, name);
3136 h = yaffs_open(full_name, O_RDWR | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
3138 yaffs_write(h, name, strlen(name));
3144 void xx_test(const char *mountpt)
3148 yaffs_format(mountpt,0,0,0);
3150 yaffs_mount(mountpt);
3151 printf("mounted\n");
3154 printf("create files\n");
3156 mk_dir(mountpt, "foo");
3157 mk_file(mountpt, "foo/f1");
3158 mk_file(mountpt, "foo/f2");
3159 mk_file(mountpt, "foo/f3");
3160 mk_file(mountpt, "foo/f4");
3161 dump_directory_tree(mountpt);
3163 printf("unmount and remount\n");
3165 /* Unmount/remount */
3166 yaffs_unmount(mountpt);
3167 yaffs_mount(mountpt);
3168 dump_directory_tree(mountpt);
3171 void yy_test(const char *mountpt)
3175 yaffs_mount(mountpt);
3176 dump_directory_tree(mountpt);
3180 void readdir_test(const char *mountpt)
3183 yaffs_DIR *dirs[100];
3185 yaffs_trace_mask = 0;
3189 yaffs_mount(mountpt);
3191 for(i = 0; i < 100; i++) {
3192 dirs[i] = yaffs_opendir(mountpt);
3193 printf("%2d %p,", i, dirs[i]);
3198 for(i = 0; i < 100; i++) {
3200 yaffs_closedir(dirs[i]);
3204 for(i = 0; i < 100; i++) {
3205 dirs[i] = yaffs_opendir(mountpt);
3206 printf("%2d %p,", i, dirs[i]);
3209 yaffs_unmount(mountpt);
3213 void format_test(const char *mountpt)
3219 ret = yaffs_format(mountpt, 0, 0, 0);
3220 printf("yaffs_format(...,0, 0, 0) of unmounted returned %d."
3221 " Should return 0\n\n\n", ret);
3223 yaffs_mount(mountpt);
3225 ret = yaffs_format(mountpt, 0, 0, 0);
3226 printf("yaffs_format(...,0, 0, 0) of mounted returned %d."
3227 " Should return -1 (busy)\n\n\n", ret);
3229 ret = yaffs_format(mountpt, 1, 0, 0);
3230 printf("yaffs_format(...,1, 0, 0) of mounted returned %d."
3231 " Should return 0.\n\n\n", ret);
3233 ret = yaffs_mount(mountpt);
3234 printf("mount should return 0 returned %d\n\n\n", ret);
3236 ret = yaffs_format(mountpt, 1, 0, 1);
3237 printf("yaffs_format(...,1, 0, 1) of mounted returned %d."
3238 " Should return 0.\n\n\n", ret);
3240 ret = yaffs_mount(mountpt);
3241 printf("mount should return -1 returned %d\n", ret);
3244 void dir_rename_test(const char *mountpt)
3252 yaffs_mount(mountpt);
3254 sprintf(fname,"%s/file",mountpt);
3255 sprintf(dname,"%s/directory",mountpt);
3257 h = yaffs_open(fname,O_CREAT | O_RDWR | O_TRUNC, 0666);
3260 yaffs_mkdir(dname, 0666);
3262 dump_directory_tree(mountpt);
3264 printf("Try to rename %s to %s\n", fname, dname);
3265 ret = yaffs_rename(fname, dname);
3266 printf("result %d, %d\n", ret, yaffs_get_error());
3268 printf("Try to rename %s to %s\n", dname, fname);
3269 ret = yaffs_rename(dname, fname);
3270 printf("result %d, %d\n", ret, yaffs_get_error());
3276 void dir_fd_test(const char *mountpt)
3283 yaffs_mount(mountpt);
3285 sprintf(name,"%s/directory",mountpt);
3286 yaffs_mkdir(name, 0666);
3287 for(i=0; i < 20; i++) {
3288 sprintf(name,"%s/directory/file%d",mountpt, i);
3290 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3291 yaffs_write(h, name, strlen(name));
3294 sprintf(name,"%s/dddd",mountpt);
3295 yaffs_mkdir(name, 0666);
3296 for(i=0; i < 20; i++) {
3297 sprintf(name,"%s/dddd/filezzz%d",mountpt, i);
3299 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3300 yaffs_write(h, name, strlen(name));
3305 dump_directory_tree(mountpt);
3306 dump_directory_tree_fd(mountpt);
3307 dump_directory_tree_fd(mountpt);
3311 void create_delete_many_files_test(const char *mountpt)
3323 yaffs_mount(mountpt);
3325 for(i = 1; i < 2000; i++) {
3326 sprintf(fn,"%s/f%d",mountpt, i);
3327 fsize = (i%10) * 10000 + 20000;
3328 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666);
3330 wrote = yaffs_write(h, buffer, sizeof(buffer));
3331 if (wrote != sizeof(buffer)) {
3332 printf("Writing file %s, only wrote %d bytes\n", fn, wrote);
3343 void find_device_check(void)
3346 yaffs_mount("/nand");
3348 yaffs_mkdir("/nandxxx", 0666);
3349 yaffs_mkdir("/nand/xxx", 0666);
3353 void try_opendir(const char *str)
3357 d = yaffs_opendir(str);
3359 printf("%s --> %p\n", str, d);
3365 void opendir_test(void)
3368 yaffs_mount("/nand/");
3369 yaffs_symlink("x", "/nand/sym");
3370 yaffs_mkdir("/nand/x",0666);
3371 yaffs_mkdir("/nand/y",0666);
3372 yaffs_mkdir("/nand/x/r",0666);
3373 yaffs_mkdir("/nand/x/s",0666);
3374 yaffs_mkdir("/nand/x/t",0666);
3376 try_opendir("nand/x/.");
3377 try_opendir("nand/x/r/..");
3378 try_opendir("nand/x/./");
3379 try_opendir("nand/x/r/../");
3380 try_opendir("/nand/x");
3381 try_opendir("/nand/x/");
3382 try_opendir("nand/x");
3383 try_opendir("nand/sym");
3384 try_opendir("nand/sym/");
3388 void try_rmdir(const char *str)
3392 ret= yaffs_rmdir(str);
3394 printf("rmdir(\"%s\") --> %d, errno %d\n", str, ret, yaffs_get_error());
3398 void rmdir_test2(void)
3402 yaffs_mount("/nand/");
3403 yaffs_mkdir("/nand/z",0666);
3404 try_rmdir("/nand/z");
3405 yaffs_mkdir("/nand/z",0666);
3406 try_rmdir("/nand/z/");
3410 void dump_yaffs_malloc_usage(void)
3413 unsigned high_water;
3415 yaffsfs_get_malloc_values(¤t, &high_water);
3418 "***************************************\n"
3419 "Dump of yaffs malloc usage during run\n"
3420 "Currently allocated : %u bytes\n"
3421 "High water allocated : %u bytes\n"
3422 "Note, this is not accurate on all platforms\n",
3423 current, high_water);
3429 int simulate_power_failure;
3431 int main(int argc, char *argv[])
3434 atexit(dump_yaffs_malloc_usage);
3439 random_seed = time(NULL);
3440 //return long_test(argc,argv);
3442 //return cache_read_test();
3444 // resize_stress_test_no_grow("/flash/flash",20);
3445 //root_perm_remount("/flash/flash");
3447 //huge_directory_test_on_path("/ram2k");
3449 //yaffs_backward_scan_test("/flash/flash");
3450 // yaffs_device_flush_test("/flash/flash");
3452 //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
3454 //fill_empty_files_test("/nand/");
3455 //resize_stress_test("/nand");
3456 //overwrite_test("/nand");
3458 //long_name_test("/nand");
3459 //link_test0("/nand");
3460 //link_test1("yaffs2");
3461 //scan_pattern_test("/flash",10000,10);
3462 short_scan_test("/yflash2",40000,200);
3463 //small_mount_test("/flash/flash",1000);
3464 //small_overwrite_test("/flash/flash",1000);
3465 //seek_overwrite_test("/flash/flash",1000);
3466 //checkpoint_fill_test("/flash/flash",20);
3467 //checkpoint_upgrade_test("/flash/flash",20);
3468 //small_overwrite_test("/flash/flash",1000);
3469 //checkpoint_fill_test("/flash/flash",20);
3470 //random_small_file_test("/flash/flash",10000);
3471 // huge_array_test("/flash/flash",10);
3474 // handle_test("yaffs2/");
3476 //long_test_on_path("/ram2k");
3477 // long_test_on_path("/flash");
3478 //simple_rw_test("/flash/flash");
3479 //fill_disk_test("/nand");
3480 // rename_over_test("/flash");
3481 //lookup_test("/flash");
3482 //freespace_test("/flash/flash");
3484 //link_test("/flash/flash");
3486 // cache_bypass_bug_test();
3488 //free_space_check();
3490 //check_resize_gc_bug("/flash");
3492 //basic_xattr_test("/nand");
3493 //big_xattr_test("/nand");
3495 //null_name_test("yaffs2");
3497 //test_flash_traffic("yaffs2");
3498 // link_follow_test("/nand");
3499 //basic_utime_test("/nand");
3502 //format_test("/nand");
3504 //max_files_test("/nand");
3506 //start_twice("/nand");
3508 //large_file_test("/nand");
3509 //readdir_test("/nand");
3511 //basic_utime_test("/nand");
3512 //case_insensitive_test("/nand");
3515 //dir_rename_test("/nand");
3517 //dir_fd_test("/nand");
3519 //format_test("/nand");
3524 //create_delete_many_files_test("/nand");