2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
24 #include "yaffs_guts.h" /* Only for dumping device innards */
26 extern int yaffs_trace_mask;
28 void dumpDir(const char *dname);
30 void copy_in_a_file(const char *yaffsName,const char *inName)
33 unsigned char buffer[100];
35 inh = open(inName,O_RDONLY);
36 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
38 while((ni = read(inh,buffer,100)) > 0)
40 no = yaffs_write(outh,buffer,ni);
43 printf("problem writing yaffs file\n");
52 void make_a_file(const char *yaffsName,char bval,int sizeOfFile)
56 unsigned char buffer[100];
58 outh = yaffs_open(yaffsName, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
60 memset(buffer,bval,100);
67 yaffs_write(outh,buffer,i);
69 } while (sizeOfFile > 0);
76 void make_pattern_file(char *fn,int size)
81 outh = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
82 yaffs_lseek(outh,size-1,SEEK_SET);
83 yaffs_write(outh,"A",1);
85 for(i = 0; i < size; i+=256)
88 yaffs_lseek(outh,i,SEEK_SET);
89 yaffs_write(outh,&marker,sizeof(marker));
95 int check_pattern_file(char *fn)
103 h = yaffs_open(fn, O_RDWR,0);
104 size = yaffs_lseek(h,0,SEEK_END);
106 for(i = 0; i < size; i+=256)
108 yaffs_lseek(h,i,SEEK_SET);
109 yaffs_read(h,&marker,sizeof(marker));
113 printf("pattern check failed on file %s, size %d at position %d. Got %x instead of %x\n",
114 fn,size,i,marker,~i);
125 int dump_file_data(char *fn)
132 h = yaffs_open(fn, O_RDWR,0);
136 while(yaffs_read(h,&b,1)> 0)
153 void dump_file(const char *fn)
159 h = yaffs_open(fn,O_RDONLY,0);
162 printf("*****\nDump file %s does not exist\n",fn);
166 size = yaffs_lseek(h,0,SEEK_SET);
167 printf("*****\nDump file %s size %d\n",fn,size);
168 for(i = 0; i < size; i++)
175 void create_file_of_size(const char *fn,int syze)
183 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
187 sprintf(xx,"%s %8d",fn,iteration);
189 result = yaffs_write(h,xx,n);
191 printf("Wrote %d, should have been %d. syze is %d\n",result,n,syze);
200 void verify_file_of_size(const char *fn,int syze)
209 int iterations = (syze + strlen(fn) -1)/ strlen(fn);
211 h = yaffs_open(fn, O_RDONLY, S_IREAD | S_IWRITE);
213 while (iterations > 0)
215 sprintf(xx,"%s %8d",fn,iterations);
218 result = yaffs_read(h,yy,l);
220 printf("result in line %d is %d", __LINE__, result);
224 printf("=====>>>>> verification of file %s failed near position %lld\n",fn,(long long)yaffs_lseek(h,0,SEEK_CUR));
231 void create_resized_file_of_size(const char *fn,int syze1,int reSyze, int syze2)
237 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
239 iterations = (syze1 + strlen(fn) -1)/ strlen(fn);
240 while (iterations > 0)
242 yaffs_write(h,fn,strlen(fn));
246 yaffs_ftruncate(h,reSyze);
248 yaffs_lseek(h,0,SEEK_SET);
249 iterations = (syze2 + strlen(fn) -1)/ strlen(fn);
250 while (iterations > 0)
252 yaffs_write(h,fn,strlen(fn));
260 void do_some_file_stuff(const char *path)
265 sprintf(fn,"%s/%s",path,"f1");
266 create_file_of_size(fn,10000);
268 sprintf(fn,"%s/%s",path,"fdel");
269 create_file_of_size(fn,10000);
272 sprintf(fn,"%s/%s",path,"f2");
274 create_resized_file_of_size(fn,10000,3000,4000);
277 void yaffs_backward_scan_test(const char *path)
285 do_some_file_stuff(path);
287 sprintf(fn,"%s/ddd",path);
291 do_some_file_stuff(fn);
298 void null_name_test(const char *path)
306 sprintf(fn,"%s",path);
308 h = yaffs_open(fn,O_CREAT| O_TRUNC| O_RDWR, 0666);
317 void yaffs_device_flush_test(const char *path)
327 do_some_file_stuff(path);
329 // Open and add some data to a few files
330 for(i = 0; i < 10; i++) {
332 sprintf(fn,"%s/ff%d",path,i);
334 h = yaffs_open(fn, O_CREAT | O_RDWR | O_TRUNC, S_IWRITE | S_IREAD);
335 yaffs_write(h,xxzz,2000);
336 yaffs_write(h,xxzz,2000);
345 void short_scan_test(const char *path, int fsize, int niterations)
350 sprintf(fn,"%s/%s",path,"f1");
353 for(i = 0; i < niterations; i++)
355 printf("\n*****************\nIteration %d\n",i);
357 printf("\nmount: Directory look-up of %s\n",path);
359 make_a_file(fn,1,fsize);
366 void scan_pattern_test(const char *path, int fsize, int niterations)
373 sprintf(fn[0],"%s/%s",path,"f0");
374 sprintf(fn[1],"%s/%s",path,"f1");
375 sprintf(fn[2],"%s/%s",path,"f2");
379 for(i = 0; i < niterations; i++)
381 printf("\n*****************\nIteration %d\n",i);
383 printf("\nmount: Directory look-up of %s\n",path);
385 for(j = 0; j < 3; j++)
387 result = dump_file_data(fn[j]);
388 result = check_pattern_file(fn[j]);
389 make_pattern_file(fn[j],fsize);
390 result = dump_file_data(fn[j]);
391 result = check_pattern_file(fn[j]);
393 printf("result in line %d is %d", __LINE__, result);
399 void fill_disk(const char *path,int nfiles)
409 for(n = 0; n < nfiles; n++)
411 sprintf(str,"%s/%d",path,n);
413 h = yaffs_open(str, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
415 printf("writing file %s handle %d ",str, h);
417 while ((result = yaffs_write(h,xx,600)) == 600)
419 f = yaffs_freespace(path);
421 printf("last freespace was %d\n", f);
422 result = yaffs_close(h);
423 printf(" close %d\n", result);
427 void fill_disk_and_delete(const char *path, int nfiles, int ncycles)
433 for(i = 0; i < ncycles; i++)
435 printf("@@@@@@@@@@@@@@ cycle %d\n",i);
436 fill_disk(path,nfiles);
438 for(j = 0; j < nfiles; j++)
440 sprintf(str,"%s/%d",path,j);
441 result = yaffs_unlink(str);
442 printf("unlinking file %s, result %d\n", str, result);
448 void fill_files(const char *path,int flags, int maxIterations,int siz)
458 sprintf(str,"%s/%d",path,i);
459 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
463 for(j = 0; j < siz; j++)
465 yaffs_write(h,str,1);
474 } while(h >= 0 && i < maxIterations);
480 sprintf(str,"%s/%d",path,i);
481 printf("unlink %s\n",str);
483 } while(yaffs_unlink(str) >= 0);
487 void leave_unlinked_file(const char *path,int maxIterations,int siz)
496 sprintf(str,"%s/%d",path,i);
497 printf("create %s\n",str);
498 h = yaffs_open(str, O_CREAT | O_TRUNC | O_RDWR,S_IREAD | S_IWRITE);
504 } while(h < 0 && i < maxIterations);
508 for(i = 0; i < siz; i++)
510 yaffs_write(h,str,1);
514 printf("Leaving file %s open\n",str);
518 void dumpDirFollow(const char *dname)
521 struct yaffs_dirent *de;
525 d = yaffs_opendir(dname);
529 printf("opendir failed\n");
533 while((de = yaffs_readdir(d)) != NULL)
535 sprintf(str,"%s/%s",dname,de->d_name);
539 printf("%s ino %d length %d mode %X ",
540 de->d_name, (int)s.st_ino, (int)s.st_size, s.st_mode);
541 switch(s.st_mode & S_IFMT)
543 case S_IFREG: printf("data file"); break;
544 case S_IFDIR: printf("directory"); break;
545 case S_IFLNK: printf("symlink -->");
546 if(yaffs_readlink(str,str,100) < 0)
549 printf("\"%s\"",str);
551 default: printf("unknown"); break;
561 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
566 void dump_directory_tree_worker(const char *dname,int recursive)
569 struct yaffs_dirent *de;
573 d = yaffs_opendir(dname);
577 printf("opendir failed\n");
581 while((de = yaffs_readdir(d)) != NULL)
583 sprintf(str,"%s/%s",dname,de->d_name);
587 printf("%s inode %d length %d mode %X ",
588 str, s.st_ino, (int)s.st_size, s.st_mode);
589 switch(s.st_mode & S_IFMT)
591 case S_IFREG: printf("data file"); break;
592 case S_IFDIR: printf("directory"); break;
593 case S_IFLNK: printf("symlink -->");
594 if(yaffs_readlink(str,str,100) < 0)
597 printf("\"%s\"",str);
599 default: printf("unknown"); break;
604 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
605 dump_directory_tree_worker(str,1);
614 static void dump_directory_tree(const char *dname)
616 dump_directory_tree_worker(dname,1);
618 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
621 void dump_directory_tree_worker_fd(const char *dname,int recursive)
624 struct yaffs_dirent *de;
628 h = yaffs_open(dname, O_RDONLY, 0);
632 printf("open of dir failed\n");
636 printf("using fd %d\n", h);
638 while((de = yaffs_readdir_fd(h)) != NULL)
640 sprintf(str,"%s/%s",dname,de->d_name);
644 printf("%s inode %d length %d mode %X ",
645 str,s.st_ino, (int)s.st_size, s.st_mode);
646 switch(s.st_mode & S_IFMT)
648 case S_IFREG: printf("data file"); break;
649 case S_IFDIR: printf("directory"); break;
650 case S_IFLNK: printf("symlink -->");
651 if(yaffs_readlink(str,str,100) < 0)
654 printf("\"%s\"",str);
656 default: printf("unknown"); break;
661 if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
662 dump_directory_tree_worker_fd(str,1);
671 static void dump_directory_tree_fd(const char *dname)
673 dump_directory_tree_worker_fd(dname,1);
675 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
678 void dumpDir(const char *dname)
679 { dump_directory_tree_worker(dname,0);
681 printf("Free space in %s is %d\n\n",dname,(int)yaffs_freespace(dname));
685 static void PermissionsCheck(const char *path, mode_t tmode, int tflags,int expectedResult)
689 if(yaffs_chmod(path,tmode)< 0) printf("chmod failed\n");
691 fd = yaffs_open(path,tflags,0);
693 if((fd >= 0) != (expectedResult > 0))
695 printf("Permissions check %x %x %d failed\n",tmode,tflags,expectedResult);
699 printf("Permissions check %x %x %d OK\n",tmode,tflags,expectedResult);
708 int long_test(int argc, char *argv[])
719 struct yaffs_stat ystat;
726 yaffs_mount("/boot");
727 yaffs_mount("/data");
728 yaffs_mount("/flash");
731 printf("\nDirectory look-up of /boot\n");
733 printf("\nDirectory look-up of /data\n");
735 printf("\nDirectory look-up of /flash\n");
738 //leave_unlinked_file("/flash",20000,0);
739 //leave_unlinked_file("/data",20000,0);
741 leave_unlinked_file("/ram",20,0);
744 f = yaffs_open("/boot/b1", O_RDONLY,0);
746 printf("open /boot/b1 readonly, f=%d\n",f);
748 f = yaffs_open("/boot/b1", O_CREAT,S_IREAD | S_IWRITE);
750 printf("open /boot/b1 O_CREAT, f=%d\n",f);
753 r = yaffs_write(f,"hello",1);
754 printf("write %d attempted to write to a read-only file\n",r);
758 printf("close %d\n",r);
760 f = yaffs_open("/boot/b1", O_RDWR,0);
762 printf("open /boot/b1 O_RDWR,f=%d\n",f);
765 r = yaffs_write(f,"hello",2);
766 printf("write %d attempted to write to a writeable file\n",r);
767 r = yaffs_write(f,"world",3);
768 printf("write %d attempted to write to a writeable file\n",r);
770 r= yaffs_lseek(f,0,SEEK_END);
771 printf("seek end %d\n",r);
773 r = yaffs_read(f,buffer,10);
774 printf("read %d \"%s\"\n",r,buffer);
775 r= yaffs_lseek(f,0,SEEK_SET);
776 printf("seek set %d\n",r);
778 r = yaffs_read(f,buffer,10);
779 printf("read %d \"%s\"\n",r,buffer);
781 r = yaffs_read(f,buffer,10);
782 printf("read %d \"%s\"\n",r,buffer);
784 // Check values reading at end.
785 // A read past end of file should return 0 for 0 bytes read.
787 r= yaffs_lseek(f,0,SEEK_END);
788 r = yaffs_read(f,buffer,10);
789 printf("read at end returned %d\n",r);
790 r= yaffs_lseek(f,500,SEEK_END);
791 r = yaffs_read(f,buffer,10);
792 printf("read past end returned %d\n",r);
796 printf("close %d\n",r);
798 copy_in_a_file("/boot/yyfile","xxx");
800 // Create a file with a long name
802 copy_in_a_file("/boot/file with a long name","xxx");
805 printf("\nDirectory look-up of /boot\n");
809 r = yaffs_lstat("/boot/file with a long name",&ystat);
813 r = yaffs_rename("/boot/file with a long name","/boot/r1");
815 printf("\nDirectory look-up of /boot\n");
819 r = yaffs_unlink("/boot/r1");
821 printf("\nDirectory look-up of /boot\n");
826 r = yaffs_mkdir("/boot/directory1",0);
828 printf("\nDirectory look-up of /boot\n");
830 printf("\nDirectory look-up of /boot/directory1\n");
831 dumpDir("/boot/directory1");
833 // add a file to the directory
834 copy_in_a_file("/boot/directory1/file with a long name","xxx");
836 printf("\nDirectory look-up of /boot\n");
838 printf("\nDirectory look-up of /boot/directory1\n");
839 dumpDir("/boot/directory1");
841 // Attempt to delete directory (should fail)
843 r = yaffs_rmdir("/boot/directory1");
845 printf("\nDirectory look-up of /boot\n");
847 printf("\nDirectory look-up of /boot/directory1\n");
848 dumpDir("/boot/directory1");
850 // Delete file first, then rmdir should work
851 r = yaffs_unlink("/boot/directory1/file with a long name");
852 r = yaffs_rmdir("/boot/directory1");
855 printf("\nDirectory look-up of /boot\n");
857 printf("\nDirectory look-up of /boot/directory1\n");
858 dumpDir("/boot/directory1");
861 fill_disk_and_delete("/boot",20,20);
863 printf("\nDirectory look-up of /boot\n");
867 yaffs_symlink("yyfile","/boot/slink");
869 yaffs_readlink("/boot/slink",str,100);
870 printf("symlink alias is %s\n",str);
875 printf("\nDirectory look-up of /boot\n");
877 printf("\nDirectory look-up of /boot (using stat instead of lstat)\n");
878 dumpDirFollow("/boot");
879 printf("\nDirectory look-up of /boot/directory1\n");
880 dumpDir("/boot/directory1");
882 h = yaffs_open("/boot/slink",O_RDWR,0);
884 printf("file length is %d\n",(int)yaffs_lseek(h,0,SEEK_END));
888 yaffs_unlink("/boot/slink");
891 printf("\nDirectory look-up of /boot\n");
896 yaffs_lstat("/boot/yyfile",&ystat);
897 temp_mode = ystat.st_mode;
899 yaffs_chmod("/boot/yyfile",0x55555);
900 printf("\nDirectory look-up of /boot\n");
903 yaffs_chmod("/boot/yyfile",temp_mode);
904 printf("\nDirectory look-up of /boot\n");
907 // Permission checks...
908 PermissionsCheck("/boot/yyfile",0, O_WRONLY,0);
909 PermissionsCheck("/boot/yyfile",0, O_RDONLY,0);
910 PermissionsCheck("/boot/yyfile",0, O_RDWR,0);
912 PermissionsCheck("/boot/yyfile",S_IREAD, O_WRONLY,0);
913 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDONLY,1);
914 PermissionsCheck("/boot/yyfile",S_IREAD, O_RDWR,0);
916 PermissionsCheck("/boot/yyfile",S_IWRITE, O_WRONLY,1);
917 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDONLY,0);
918 PermissionsCheck("/boot/yyfile",S_IWRITE, O_RDWR,0);
920 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_WRONLY,1);
921 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDONLY,1);
922 PermissionsCheck("/boot/yyfile",S_IREAD | S_IWRITE, O_RDWR,1);
924 yaffs_chmod("/boot/yyfile",temp_mode);
926 //create a zero-length file and unlink it (test for scan bug)
928 h = yaffs_open("/boot/zlf",O_CREAT | O_TRUNC | O_RDWR,0);
931 yaffs_unlink("/boot/zlf");
934 yaffs_dump_dev("/boot");
936 fill_disk_and_delete("/boot",20,20);
938 yaffs_dump_dev("/boot");
940 fill_files("/boot",1,10000,0);
941 fill_files("/boot",1,10000,5000);
942 fill_files("/boot",2,10000,0);
943 fill_files("/boot",2,10000,5000);
945 leave_unlinked_file("/data",20000,0);
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);
950 leave_unlinked_file("/data",20000,5000);
952 yaffs_dump_dev("/boot");
953 yaffs_dump_dev("/data");
961 int huge_directory_test_on_path(char *path)
965 struct yaffs_dirent *de;
981 // Create a large number of files
983 for(i = 0; i < 2000; i++)
985 sprintf(str,"%s/%d",path,i);
987 f = yaffs_open(str,O_CREAT,S_IREAD | S_IWRITE);
993 d = yaffs_opendir(path);
996 while((de = yaffs_readdir(d)) != NULL) {
997 if (total >lastTotal+100*9*1024||(i & 1023)==0){
998 printf("files = %d, total = %d\n",i, total);
1002 sprintf(str,"%s/%s",path,de->d_name);
1003 yaffs_lstat(str,&s);
1004 switch(s.st_mode & S_IFMT){
1006 //printf("data file");
1018 int yaffs_scan_test(const char *path)
1026 void rename_over_test(const char *mountpt)
1033 sprintf(a,"%s/a",mountpt);
1034 sprintf(b,"%s/b",mountpt);
1035 sprintf(c,"%s/c",mountpt);
1039 yaffs_mount(mountpt);
1041 printf("Existing files\n");
1042 dumpDirFollow(mountpt);
1046 i = yaffs_open(c,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1047 printf("File c handle is %d\n",i);
1049 i = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1051 i = yaffs_open(b,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1053 yaffs_rename(a,b); // rename over
1054 yaffs_rename(b,a); // rename back again (not renaimng over)
1055 yaffs_rename(a,b); // rename back again (not renaimng over)
1058 yaffs_unmount(mountpt);
1063 int resize_stress_test(const char *path)
1078 sprintf(aname,"%s%s",path,"/a");
1079 sprintf(bname,"%s%s",path,"/b");
1081 memset(abuffer,'a',1000);
1082 memset(bbuffer,'b',1000);
1084 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1085 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1087 printf(" %s %d %s %d\n",aname,a,bname,b);
1091 for(j = 0; j < 100; j++)
1093 yaffs_lseek(a,0,SEEK_END);
1096 for(i = 0; i <20000; i++)
1098 //r = yaffs_lseek(b,i,SEEK_SET);
1099 //r = yaffs_write(b,bbuffer,1000);
1104 int syz = yaffs_lseek(a,0,SEEK_END);
1107 if(syz < 0) syz = 0;
1108 yaffs_ftruncate(a,syz);
1114 r = yaffs_lseek(a,i * 500,SEEK_SET);
1116 printf("At line %d, r is %d\n", __LINE__, r);
1117 r = yaffs_write(a,abuffer,1000);
1119 printf("At line %d, r is %d\n", __LINE__, r);
1131 int overwrite_test(const char *path)
1143 sprintf(aname,"%s%s",path,"/a");
1144 sprintf(bname,"%s%s",path,"/b");
1146 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1147 for(j= 0; j < 500; j++){
1148 yaffs_write(b,bname,100);
1149 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1150 for(i = 0; i < rand() % 20000; i++)
1151 yaffs_write(a,&a,sizeof(a));
1160 int root_perm_remount(const char *path)
1162 struct yaffs_stat s;
1168 yaffs_lstat(path,&s);
1169 printf("root perms after mount %x\n",s.st_mode);
1171 yaffs_chmod(path, 0777);
1173 yaffs_lstat(path,&s);
1174 printf("root perms after setting to 0777 is %x\n",s.st_mode);
1176 yaffs_unmount(path);
1183 int resize_stress_test_no_grow_complex(const char *path,int iters)
1199 sprintf(aname,"%s%s",path,"/a");
1200 sprintf(bname,"%s%s",path,"/b");
1202 memset(abuffer,'a',1000);
1203 memset(bbuffer,'b',1000);
1205 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1206 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1208 printf(" %s %d %s %d\n",aname,a,bname,b);
1212 for(j = 0; j < iters; j++)
1214 yaffs_lseek(a,0,SEEK_END);
1217 for(i = 0; i <20000; i++)
1219 //r = yaffs_lseek(b,i,SEEK_SET);
1220 //r = yaffs_write(b,bbuffer,1000);
1225 int syz = yaffs_lseek(a,0,SEEK_END);
1231 if(syz < 0) syz = 0;
1232 yaffs_ftruncate(a,syz);
1233 syz = yaffs_lseek(a,0,SEEK_END);
1234 printf("shrink to %d\n",syz);
1242 r = yaffs_lseek(a,500,SEEK_END);
1243 r = yaffs_write(a,abuffer,1000);
1245 printf("At line %d, r is %d\n", __LINE__, r);
1252 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1260 int resize_stress_test_no_grow(const char *path,int iters)
1275 sprintf(aname,"%s%s",path,"/a");
1276 sprintf(bname,"%s%s",path,"/b");
1278 memset(abuffer,'a',1000);
1279 memset(bbuffer,'b',1000);
1281 a = yaffs_open(aname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1282 b = yaffs_open(bname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1284 printf(" %s %d %s %d\n",aname,a,bname,b);
1288 for(j = 0; j < iters; j++)
1290 yaffs_lseek(a,0,SEEK_END);
1293 for(i = 0; i <20000; i++)
1295 //r = yaffs_lseek(b,i,SEEK_SET);
1296 //r = yaffs_write(b,bbuffer,1000);
1301 int syz = yaffs_lseek(a,0,SEEK_END);
1307 if(syz < 0) syz = 0;
1308 yaffs_ftruncate(a,syz);
1309 syz = yaffs_lseek(a,0,SEEK_END);
1310 printf("shrink to %d\n",syz);
1318 r = yaffs_lseek(a,-500,SEEK_END);
1319 r = yaffs_write(a,abuffer,1000);
1321 printf("At line %d, r is %d\n", __LINE__, r);
1327 printf("file size is %lld\n",(long long)yaffs_lseek(a,0,SEEK_END));
1335 int directory_rename_test(void)
1340 yaffs_mount("/ram");
1341 yaffs_mkdir("/ram/a",0);
1342 yaffs_mkdir("/ram/a/b",0);
1343 yaffs_mkdir("/ram/c",0);
1345 printf("\nDirectory look-up of /ram\n");
1348 dumpDir("/ram/a/b");
1350 printf("Do rename (should fail)\n");
1352 r = yaffs_rename("/ram/a","/ram/a/b/d");
1354 printf("At line %d, r is %d\n", __LINE__, r);
1356 printf("\nDirectory look-up of /ram\n");
1359 dumpDir("/ram/a/b");
1361 printf("Do rename (should not fail)\n");
1363 r = yaffs_rename("/ram/c","/ram/a/b/d");
1364 printf("\nDirectory look-up of /ram\n");
1367 dumpDir("/ram/a/b");
1374 int cache_read_test(void)
1378 int sizeOfFiles = 500000;
1383 yaffs_mount("/boot");
1385 make_a_file("/boot/a",'a',sizeOfFiles);
1386 make_a_file("/boot/b",'b',sizeOfFiles);
1388 a = yaffs_open("/boot/a",O_RDONLY,0);
1389 b = yaffs_open("/boot/b",O_RDONLY,0);
1390 c = yaffs_open("/boot/c", O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
1394 if (i > 100) i = 100;
1396 yaffs_read(a,buffer,i);
1397 yaffs_read(b,buffer,i);
1398 yaffs_write(c,buffer,i);
1399 } while(sizeOfFiles > 0);
1407 int cache_bypass_bug_test(void)
1409 // This test reporoduces a bug whereby YAFFS caching *was* buypassed
1410 // resulting in erroneous reads after writes.
1411 // This bug has been fixed.
1417 memset(buffer1,0,sizeof(buffer1));
1418 memset(buffer2,0,sizeof(buffer2));
1422 yaffs_mount("/boot");
1424 // Create a file of 2000 bytes.
1425 make_a_file("/boot/a",'X',2000);
1427 a = yaffs_open("/boot/a",O_RDWR, S_IREAD | S_IWRITE);
1429 // Write a short sequence to the file.
1430 // This will go into the cache.
1431 yaffs_lseek(a,0,SEEK_SET);
1432 yaffs_write(a,"abcdefghijklmnopqrstuvwxyz",20);
1434 // Read a short sequence from the file.
1435 // This will come from the cache.
1436 yaffs_lseek(a,0,SEEK_SET);
1437 yaffs_read(a,buffer1,30);
1439 // Read a page size sequence from the file.
1440 yaffs_lseek(a,0,SEEK_SET);
1441 yaffs_read(a,buffer2,512);
1443 printf("buffer 1 %s\n",buffer1);
1444 printf("buffer 2 %s\n",buffer2);
1446 if(strncmp(buffer1,buffer2,20))
1448 printf("Cache bypass bug detected!!!!!\n");
1456 int free_space_check(void)
1461 yaffs_mount("/boot");
1462 fill_disk("/boot/",2);
1463 f = yaffs_freespace("/boot");
1465 printf("%d free when disk full\n",f);
1469 int truncate_test(void)
1479 yaffs_mount("/boot");
1481 yaffs_unlink("/boot/trunctest");
1483 a = yaffs_open("/boot/trunctest", O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1485 yaffs_write(a,"abcdefghijklmnopqrstuvwzyz",26);
1487 yaffs_ftruncate(a,3);
1488 l= yaffs_lseek(a,0,SEEK_END);
1490 printf("truncated length is %d\n",l);
1492 yaffs_lseek(a,5,SEEK_SET);
1493 yaffs_write(a,"1",1);
1495 yaffs_lseek(a,0,SEEK_SET);
1497 r = yaffs_read(a,y,10);
1499 printf("read %d bytes:", r);
1501 for(i = 0; i < r; i++) printf("[%02X]",y[i]);
1513 void fill_disk_test(const char *mountpt)
1518 for(i = 0; i < 5; i++)
1520 yaffs_mount(mountpt);
1521 fill_disk_and_delete(mountpt,100,i+1);
1522 yaffs_unmount(mountpt);
1528 void fill_files_test(const char *mountpt)
1533 for(i = 0; i < 5; i++)
1535 yaffs_mount(mountpt);
1536 fill_files(mountpt,2,3,100);
1537 yaffs_unmount(mountpt);
1542 void fill_empty_files_test(const char *mountpt)
1551 for(i = 0; i < 5; i++)
1553 yaffs_mount(mountpt);
1554 for(d = 0; result >= 0 && d < 1000; d++){
1555 sprintf(name,"%s/%d",mountpt,d);
1556 result= yaffs_mkdir(name,0);
1557 printf("creating directory %s result %d\n",name,result);
1559 for(f = 0; result >= 0 && f < 100; f++){
1560 sprintf(name,"%s/%d/%d",mountpt,d,f);
1561 result= yaffs_open(name,O_CREAT, 0);
1562 yaffs_close(result);
1563 printf("creating file %s result %d\n",name,result);
1566 yaffs_unmount(mountpt);
1571 void long_name_test(const char *mountpt)
1575 char fullName[1000];
1581 // Make a 256 byte name
1582 memset(name,0,sizeof(name));
1583 for(i = 0; i < 256; i++)
1584 name[i] = '0' + i % 10;
1586 sprintf(fullName,"%s/%s",mountpt,name);
1588 for(i = 0; i < 1; i++)
1590 yaffs_mount(mountpt);
1592 printf("Files at start\n");
1595 printf("Creating file %s\n",fullName);
1597 f = yaffs_open(fullName,O_CREAT | O_RDWR,0);
1600 printf("Result %d\n",f);
1605 printf("Deleting %s\n",fullName);
1606 result = yaffs_unlink(fullName);
1607 printf("Result %d\n",result);
1613 yaffs_unmount(mountpt);
1619 void lookup_test(const char *mountpt)
1627 struct yaffs_dirent *de;
1631 yaffs_mount(mountpt);
1633 d = yaffs_opendir(mountpt);
1637 printf("opendir failed\n");
1642 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1644 printf("unlinking %s\n",de->d_name);
1645 yaffs_unlink(de->d_name);
1648 printf("%d files deleted\n",i);
1652 for(i = 0; i < 2000; i++){
1653 sprintf(a,"%s/%d",mountpt,i);
1654 h = yaffs_open(a,O_CREAT | O_TRUNC | O_RDWR, 0);
1659 for(i = 0; (de = yaffs_readdir(d)) != NULL; i++)
1661 printf("%d %s\n",i,de->d_name);
1664 printf("%d files listed\n\n\n",i);
1671 for(i = 0; i < 2000; i++){
1672 sprintf(a,"%s/%d",mountpt,i);
1677 yaffs_unmount(mountpt);
1681 void link_test0(const char *mountpt)
1689 yaffs_mount(mountpt);
1692 sprintf(namea,"%s/a",mountpt);
1693 sprintf(nameb,"%s/b",mountpt);
1695 printf("mounted\n");
1698 yaffs_unlink(namea);
1699 printf("a unlinked\n");
1702 yaffs_unlink(nameb);
1703 printf("b unlinked\n");
1706 result = yaffs_open(namea,O_CREAT| O_RDWR,0666);
1707 yaffs_close(result);
1708 printf("a created\n");
1711 yaffs_link(namea,nameb);
1714 yaffs_unlink(namea);
1715 printf("a ulinked\n");
1717 yaffs_unlink(nameb);
1718 printf("b unlinked\n");
1721 yaffs_unmount(mountpt);
1725 void link_test1(const char *mountpt)
1733 sprintf(a,"%s/aaa",mountpt);
1734 sprintf(b,"%s/bbb",mountpt);
1735 sprintf(c,"%s/ccc",mountpt);
1739 yaffs_mount(mountpt);
1742 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1743 for(i = 0; i < 100; i++)
1744 yaffs_write(h,a,100);
1757 yaffs_unmount(mountpt);
1758 yaffs_mount(mountpt);
1760 printf("link test done\n");
1763 void handle_test(const char *mountpt)
1770 sprintf(a,"%s/aaa",mountpt);
1774 yaffs_mount(mountpt);
1776 for(cycle = 0; cycle < 5; cycle++){
1777 printf("Start cycle %d\n",cycle);
1780 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1781 printf("%d handle %d\n",i,h);
1791 yaffs_unmount(mountpt);
1794 void freespace_test(const char *mountpt)
1804 sprintf(a,"%s/aaa",mountpt);
1808 yaffs_mount(mountpt);
1810 f0 = yaffs_freespace(mountpt);
1812 h = yaffs_open(a, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1814 for(i = 0; i < 100; i++)
1815 yaffs_write(h,a,100);
1819 f1 = yaffs_freespace(mountpt);
1823 f2 = yaffs_freespace(mountpt);
1826 yaffs_unmount(mountpt);
1827 yaffs_mount(mountpt);
1829 f3 = yaffs_freespace(mountpt);
1831 printf("%d\n%d\n%d\n%d\n",f0, f1,f2,f3);
1836 void simple_rw_test(const char *mountpt)
1845 sprintf(a,"%s/aaa",mountpt);
1849 yaffs_mount(mountpt);
1853 h = yaffs_open(a,O_CREAT| O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1855 for(i = 100000;i < 200000; i++){
1856 result = yaffs_write(h,&i,sizeof(i));
1860 printf("write error\n");
1867 // h = yaffs_open(a,O_RDWR, S_IREAD | S_IWRITE);
1870 yaffs_lseek(h,0,SEEK_SET);
1872 for(i = 100000; i < 200000; i++){
1873 result = yaffs_read(h,&x,sizeof(x));
1875 if(result != 4 || x != i){
1876 printf("read error %d %x %x\n",i,result,x);
1880 printf("Simple rw test passed\n");
1887 void scan_deleted_files_test(const char *mountpt)
1899 sprintf(sub,"%s/sdir",mountpt);
1902 for(j = 0; j < 10; j++)
1904 printf("\n\n>>>>>>> Run %d <<<<<<<<<<<<<\n\n",j);
1905 yaffs_mount(mountpt);
1909 p = (j & 0) ? mountpt: sub;
1911 for(i = 0; i < 100; i++)
1913 sprintf(fn,"%s/%d",p,i);
1917 h = yaffs_open(fn,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1918 for(k = 0; k < 1000; k++)
1919 yaffs_write(h,fn,100);
1926 for(i = 0; i < 10; i++)
1928 sprintf(fn,"%s/%d",p,i);
1936 yaffs_unmount(mountpt);
1945 void write_10k(int h)
1948 const char *s="0123456789";
1949 for(i = 0; i < 1000; i++)
1950 yaffs_write(h,s,10);
1953 void write_200k_file(const char *fn, const char *fdel, const char *fdel1)
1959 h1 = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
1961 for(i = 0; i < 100000; i+= 10000)
1966 offs = yaffs_lseek(h1,0,SEEK_CUR);
1969 printf("Could not write file\n");
1973 for(i = 0; i < 100000; i+= 10000)
1978 offs = yaffs_lseek(h1,0,SEEK_CUR);
1981 printf("Could not write file\n");
1985 yaffs_unlink(fdel1);
1990 void verify_200k_file(const char *fn)
1995 const char *s="0123456789";
1998 h1 = yaffs_open(fn, O_RDONLY, 0);
2000 for(i = 0; i < 200000 && errCount < 10; i+= 10)
2002 yaffs_read(h1,x,10);
2003 if(strncmp(x,s,10) != 0)
2005 printf("File %s verification failed at %d\n",fn,i);
2010 printf("Too many errors... aborted\n");
2017 void check_resize_gc_bug(const char *mountpt)
2026 sprintf(a,"%s/a",mountpt);
2027 sprintf(b,"%s/b",mountpt);
2028 sprintf(c,"%s/c",mountpt);
2034 yaffs_mount(mountpt);
2038 for(i = 0; i < 50; i++)
2040 printf("A\n");write_200k_file(a,"",c);
2041 printf("B\n");verify_200k_file(a);
2042 printf("C\n");write_200k_file(b,a,c);
2043 printf("D\n");verify_200k_file(b);
2044 yaffs_unmount(mountpt);
2045 yaffs_mount(mountpt);
2046 printf("E\n");verify_200k_file(a);
2047 printf("F\n");verify_200k_file(b);
2053 void multi_mount_test(const char *mountpt,int nmounts)
2061 sprintf(a,"%s/a",mountpt);
2065 for(i = 0; i < nmounts; i++){
2070 printf("############### Iteration %d Start\n",i);
2071 if(1 || i == 0 || i == 5)
2072 yaffs_mount(mountpt);
2074 dump_directory_tree(mountpt);
2079 sprintf(xx,"%s/0",a);
2080 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2082 sprintf(xx,"%s/1",a);
2083 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2086 for(j = 0; j < 200; j++){
2087 yaffs_write(h0,xx,1000);
2088 yaffs_write(h1,xx,1000);
2091 while(yaffs_write(h0,xx,1000) > 0){
2093 yaffs_write(h1,xx,1000);
2096 yaffs_lseek(h0,0,SEEK_END);
2097 yaffs_lseek(h1,0,SEEK_END);
2099 yaffs_lseek(h0,0,SEEK_SET);
2100 yaffs_lseek(h1,0,SEEK_SET);
2102 for(j = 0; j < 200; j++){
2103 yaffs_read(h0,xx,1000);
2104 yaffs_read(h1,xx,1000);
2108 // yaffs_truncate(h0,0);
2112 printf("########### %d\n",i);
2113 dump_directory_tree(mountpt);
2115 if(1 || i == 4 || i == nmounts -1)
2116 yaffs_unmount(mountpt);
2121 void small_mount_test(const char *mountpt,int nmounts)
2133 sprintf(a,"%s/a",mountpt);
2139 for(i = 0; i < nmounts; i++){
2143 printf("############### Iteration %d Start\n",i);
2144 if(1 || i == 0 || i == 5)
2145 yaffs_mount(mountpt);
2147 dump_directory_tree(mountpt);
2151 sprintf(xx,"%s/0",a);
2154 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2155 for(j = 0; j < 130; j++)
2156 yaffs_write(h0,xx,1000);
2160 h0 = yaffs_open(xx,O_RDONLY,0);
2162 sprintf(xx,"%s/1",a);
2163 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2165 while((nread = yaffs_read(h0,xx,1000)) > 0)
2166 yaffs_write(h1,xx,nread);
2169 yaffs_lseek(h0,0,SEEK_END);
2170 yaffs_lseek(h1,0,SEEK_END);
2172 yaffs_lseek(h0,0,SEEK_SET);
2173 yaffs_lseek(h1,0,SEEK_SET);
2175 for(j = 0; j < 200; j++){
2176 yaffs_read(h0,xx,1000);
2177 yaffs_read(h1,xx,1000);
2183 printf("########### %d\n",i);
2184 dump_directory_tree(mountpt);
2186 if(1 || i == 4 || i == nmounts -1)
2187 yaffs_unmount(mountpt);
2194 void small_overwrite_test(const char *mountpt,int nmounts)
2205 sprintf(a,"%s/a",mountpt);
2211 for(i = 0; i < nmounts; i++){
2213 static char xx[8000];
2215 printf("############### Iteration %d Start\n",i);
2217 yaffs_mount(mountpt);
2219 dump_directory_tree(mountpt);
2223 sprintf(xx,"%s/0",a);
2224 h0 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2225 sprintf(xx,"%s/1",a);
2226 h1 = yaffs_open(xx, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2228 for(j = 0; j < 1000000; j+=1000){
2229 yaffs_ftruncate(h0,j);
2230 yaffs_lseek(h0,j,SEEK_SET);
2231 yaffs_write(h0,xx,7000);
2232 yaffs_write(h1,xx,7000);
2240 printf("########### %d\n",i);
2241 dump_directory_tree(mountpt);
2244 yaffs_unmount(mountpt);
2249 void seek_overwrite_test(const char *mountpt,int nmounts)
2251 static char xx[5000];
2260 sprintf(a,"%s/f",mountpt);
2264 yaffs_mount(mountpt);
2267 for(i = 0; i < nmounts; i++){
2269 h0 = yaffs_open(a, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2271 for(j = 0; j < 100000; j++){
2272 yaffs_lseek(h0,0,SEEK_SET);
2273 yaffs_write(h0,xx,5000);
2274 yaffs_lseek(h0,0x100000,SEEK_SET);
2275 yaffs_write(h0,xx,5000);
2287 void yaffs_touch(const char *fn)
2289 yaffs_chmod(fn, S_IREAD | S_IWRITE);
2292 void checkpoint_fill_test(const char *mountpt,int nmounts)
2303 sprintf(a,"%s/a",mountpt);
2310 for(i = 0; i < nmounts; i++){
2311 printf("############### Iteration %d Start\n",i);
2312 yaffs_mount(mountpt);
2313 dump_directory_tree(mountpt);
2316 sprintf(b,"%s/zz",a);
2318 h = yaffs_open(b,O_CREAT | O_RDWR,S_IREAD |S_IWRITE);
2321 while(yaffs_write(h,c,50) == 50){}
2325 for(j = 0; j < 2; j++){
2326 printf("touch %d\n",j);
2328 yaffs_unmount(mountpt);
2329 yaffs_mount(mountpt);
2332 dump_directory_tree(mountpt);
2333 yaffs_unmount(mountpt);
2338 int make_file2(const char *name1, const char *name2,int syz)
2348 h1 = yaffs_open(name1,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2350 h2 = yaffs_open(name2,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2352 while(syz > 0 && n > 0){
2353 i = (syz > 2500) ? 2500 : syz;
2354 n = yaffs_write(h1,xx,i);
2355 n = yaffs_write(h2,xx,i);
2364 extern void SetCheckpointReservedBlocks(int n);
2366 void checkpoint_upgrade_test(const char *mountpt, int nmounts)
2378 sprintf(a,"%s/a",mountpt);
2380 printf("Create start condition\n");
2382 yaffs_mount(mountpt);
2384 sprintf(b,"%s/zz",a);
2385 sprintf(c,"%s/xx",a);
2386 make_file2(b,c,2000000);
2387 sprintf(d,"%s/aa",a);
2388 make_file2(d,NULL,500000000);
2389 dump_directory_tree(mountpt);
2391 printf("Umount/mount attempt full\n");
2392 yaffs_unmount(mountpt);
2394 yaffs_mount(mountpt);
2396 printf("unlink small file\n");
2398 dump_directory_tree(mountpt);
2400 printf("Umount/mount attempt\n");
2401 yaffs_unmount(mountpt);
2402 yaffs_mount(mountpt);
2404 for(j = 0; j < 500; j++){
2405 printf("***** touch %d\n",j);
2406 dump_directory_tree(mountpt);
2408 yaffs_unmount(mountpt);
2409 yaffs_mount(mountpt);
2412 for(j = 0; j < 500; j++){
2413 printf("***** touch %d\n",j);
2414 dump_directory_tree(mountpt);
2416 yaffs_unmount(mountpt);
2417 yaffs_mount(mountpt);
2421 void huge_array_test(const char *mountpt,int n)
2432 sprintf(a,"mount point %s",mountpt);
2438 yaffs_mount(mountpt);
2443 printf("\n\n START run\n\n");
2444 while((space = yaffs_freespace(mountpt)) > 25000000){
2445 sprintf(a,"%s/file%d",mountpt,fnum);
2447 printf("create file %s, free space %d\n",a,space);
2448 create_file_of_size(a,10000000);
2449 printf("verifying file %s\n",a);
2450 verify_file_of_size(a,10000000);
2453 printf("\n\n verification/deletion\n\n");
2455 for(i = 0; i < fnum; i++){
2456 sprintf(a,"%s/file%d",mountpt,i);
2457 printf("verifying file %s\n",a);
2458 verify_file_of_size(a,10000000);
2459 printf("deleting file %s\n",a);
2462 printf("\n\n done \n\n");
2469 void random_write(int h)
2471 static char buffer[12000];
2474 n = random() & 0x1FFF;
2475 yaffs_write(h,buffer,n);
2478 void random_seek(int h)
2481 n = random() & 0xFFFFF;
2482 yaffs_lseek(h,n,SEEK_SET);
2485 void random_truncate(int h, char *name)
2492 n = random() & 0xFFFFF;
2493 flen = yaffs_lseek(h,0,SEEK_END);
2496 yaffs_ftruncate(h,n);
2497 yaffs_lseek(h,n,SEEK_SET);
2501 #define NSMALLFILES 10
2502 void random_small_file_test(const char *mountpt,int iterations)
2505 char a[NSMALLFILES][50];
2516 yaffs_mount(mountpt);
2518 for(i = 0; i < NSMALLFILES; i++){
2523 for(n = 0; n < iterations; n++){
2525 for(i = 0; i < NSMALLFILES; i++) {
2528 if(strlen(a[i]) == 0){
2529 sprintf(a[i],"%s/%dx%d",mountpt,n,i);
2530 h[i] = yaffs_open(a[i],O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
2534 printf("Could not open yaffs file %d %d error %d\n",n,i,h[i]);
2544 random_truncate(h[i],a[i]);
2547 case 5: random_seek(h[i]);
2564 for(i = 0; i < NSMALLFILES; i++)
2567 yaffs_unmount(mountpt);
2570 void rmdir_test(const char *mountpt)
2575 yaffs_mount(mountpt);
2577 strcpy(name,mountpt);
2579 strcat(name,"hello");
2580 yaffs_mkdir(name,0666);
2582 yaffs_unmount(mountpt);
2587 static void print_xattrib_val(const char *path, const char *name)
2592 n = yaffs_getxattr(path,name,buffer,sizeof(buffer));
2594 u8 *b = (u8 *)buffer;
2596 printf("%d bytes:",n);
2600 printf("[%02X]",*b);
2606 printf(" Novalue result %d\n",n);
2609 static void list_xattr(const char *path)
2616 list_len = yaffs_listxattr(path,list,sizeof(list));
2617 printf("xattribs for %s, result is %d\n",path,list_len);
2618 while(n < list_len){
2619 len = strlen(list + n);
2620 printf("\"%s\" value ",list+n);
2621 print_xattrib_val(path,list + n);
2627 void basic_utime_test(const char *mountpt)
2632 struct yaffs_utimbuf utb;
2633 struct yaffs_stat st;
2637 yaffs_mount(mountpt);
2639 strcpy(name,mountpt);
2641 strcat(name,"xfile");
2645 printf("created\n");
2646 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2649 printf(" times %lu %lu %lu\n",
2650 st.yst_atime, st.yst_ctime, st.yst_mtime);
2654 result = yaffs_futime(h,&utb);
2655 printf("futime to a 1000 m 2000 result %d\n",result);
2657 printf(" times %lu %lu %lu\n",
2658 st.yst_atime, st.yst_ctime, st.yst_mtime);
2663 result = yaffs_utime(name, &utb);
2664 printf("utime to a 5000 m 8000 result %d\n",result);
2666 printf(" times %lu %lu %lu\n",
2667 st.yst_atime, st.yst_ctime, st.yst_mtime);
2669 result = yaffs_utime(name, NULL);
2670 printf("utime to NULL result %d\n",result);
2672 printf(" times %lu %lu %lu\n",
2673 st.yst_atime, st.yst_ctime, st.yst_mtime);
2678 void basic_xattr_test(const char *mountpt)
2687 yaffs_mount(mountpt);
2689 strcpy(name,mountpt);
2691 strcat(name,"xfile");
2694 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2700 printf("Add an attribute\n");
2702 result = yaffs_setxattr(name,"foo",&val1,sizeof(val1),0);
2703 printf("wrote attribute foo: result %d\n",result);
2705 printf("Add an attribute\n");
2707 result = yaffs_setxattr(name,"bar",&val1,sizeof(val1),0);
2708 printf("wrote attribute bar: result %d\n",result);
2711 printf("Get non-existanrt attribute\n");
2712 print_xattrib_val(name,"not here");
2714 printf("Delete non existing attribute\n");
2715 yaffs_removexattr(name,"not here");
2718 printf("Remove foo\n");
2719 yaffs_removexattr(name,"foo");
2722 printf("Remove bar\n");
2723 yaffs_removexattr(name,"bar");
2728 void big_xattr_test(const char *mountpt)
2737 yaffs_mount(mountpt);
2739 strcpy(name,mountpt);
2741 strcat(name,"xfile");
2744 h = yaffs_open(name,O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2750 printf("Add a large attribute\n");
2751 memset(val,0x1,sizeof(val));
2752 result = yaffs_setxattr(name,"aaa",val,200,0);
2753 printf("wrote attribute aaa: result %d\n",result);
2756 printf("Add a large attribute\n");
2757 memset(val,0x2,sizeof(val));
2758 result = yaffs_setxattr(name,"bbb",val,1000,0);
2759 printf("wrote attribute bbb: result %d\n",result);
2762 printf("Replace attribute\n");
2763 memset(val,0x3,sizeof(val));
2764 result = yaffs_setxattr(name,"aaa",val,1000,0);
2765 printf("wrote attribute aaa: result %d\n",result);
2771 void dump_dev_stats(struct yaffs_dev *dev, const char * str)
2774 printf( "space free %d erased %d "
2775 "nand reads %d writes %d erases %d "
2776 "gc all %d passive %d oldestdirty %d blocks %d copies %d \n",
2777 dev->n_free_chunks, dev->n_erased_blocks * dev->param.chunks_per_block,
2778 dev->n_page_reads, dev->n_page_writes, dev->n_erasures,
2779 dev->all_gcs, dev->passive_gc_count, dev->oldest_dirty_gc_count, dev->n_gc_blocks, dev->n_gc_copies);
2782 void test_flash_traffic(const char *mountpt)
2787 struct yaffs_dev *dev;
2789 yaffs_trace_mask = 0;
2793 yaffs_mount(mountpt);
2795 dev = yaffs_getdev(mountpt);
2797 strcpy(name0,mountpt);
2800 strcpy(name1,mountpt);
2803 dump_dev_stats(dev,"start");
2804 create_file_of_size(name0,32 * 1024 * 1024);
2805 dump_dev_stats(dev,"32MB written");
2806 for(i = 0; i < 20; i++)
2807 create_file_of_size(name1,1024 * 1024);
2808 dump_dev_stats(dev,"20x 1MB files written");
2812 void link_follow_test(const char *mountpt)
2820 yaffs_trace_mask = 0;
2824 yaffs_mount(mountpt);
2826 sprintf(fn,"%s/file",mountpt);
2827 sprintf(sn,"%s/sym",mountpt);
2828 sprintf(hn,"%s/hl-sym",mountpt);
2830 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2831 result = yaffs_close(h);
2833 result = yaffs_symlink(fn,sn);
2834 result = yaffs_link(sn,hn);
2837 printf("At line %d result is %d\n", __LINE__, result);
2839 h =yaffs_open(hn,O_RDWR,0);
2843 void max_files_test(const char *mountpt)
2851 yaffs_trace_mask = 0;
2855 yaffs_mount(mountpt);
2857 for(i = 0; i < 5000; i++) {
2858 sprintf(fn,"%s/file%d", mountpt, i);
2860 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2862 printf("File %s not created\n", fn);
2863 yaffs_write(h,fn,100);
2864 result = yaffs_close(h);
2866 for(i = 0; i < 5; i++){
2867 sprintf(fn,"%s/file%d",mountpt, i);
2871 for(i = 1000; i < 1010; i++){
2872 sprintf(fn,"%s/file%d",mountpt, i);
2873 h = yaffs_open(fn,O_CREAT| O_RDWR, S_IREAD | S_IWRITE);
2874 yaffs_write(h,fn,100);
2876 printf("File %s not created\n", fn);
2877 result = yaffs_close(h);
2881 printf("At line %d result is %d\n", __LINE__, result);
2883 yaffs_unmount(mountpt);
2886 void case_insensitive_test(const char *mountpt)
2892 struct yaffs_stat s;
2896 yaffs_trace_mask = 0;
2900 yaffs_mount(mountpt);
2901 dump_directory_tree(mountpt);
2903 sprintf(fn,"%s/Abc.Txt",mountpt);
2905 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE);
2907 ret = yaffs_write(h,fn, strlen(fn) + 1);
2909 ret = yaffs_close(h);
2911 dump_directory_tree(mountpt);
2921 h = yaffs_open(fn2, O_RDONLY, 0);
2922 ret = yaffs_read(h, buffer, 100);
2924 if (ret != (int)(strlen(fn) + 1) || memcmp(buffer, fn, ret)){
2925 printf("wrong file read\n");
2927 printf("File %s is the same as file %s\n", fn, fn2);
2930 ret = yaffs_stat(fn2, &s);
2932 printf("renaming\n");
2934 ret = yaffs_rename(fn, fn2);
2935 dump_directory_tree(mountpt);
2939 void start_twice(const char *mountpt)
2941 printf("About to do first yaffs_start\n");
2943 printf("started\n");
2944 printf("First mount returns %d\n", yaffs_mount(mountpt));
2945 printf("About to do second yaffs_start\n");
2947 printf("started\n");
2948 printf("Second mount returns %d\n", yaffs_mount(mountpt));
2951 #define N_WRITES 2000
2954 #define BUFFER_N 1100
2955 unsigned xxbuffer[BUFFER_N];
2957 void set_buffer(int n)
2960 for(i = 0; i < BUFFER_N; i++)
2961 xxbuffer[i] = i + n;
2964 void write_big_sparse_file(int h)
2969 int n = sizeof(xxbuffer);
2972 for(i = 0; i < N_WRITES; i++) {
2973 printf("writing at %d\n", (int)offset);
2975 pos = yaffs_lseek(h, offset, SEEK_SET);
2977 printf("mismatched seek pos %d offset %d\n",
2978 (int)pos, (int)offset);
2982 wrote = yaffs_write(h, xxbuffer, n);
2985 printf("mismatched write wrote %d n %d\n", wrote, n);
2989 offset += (STRIDE * sizeof(xxbuffer));
2992 yaffs_ftruncate(h, offset);
2996 void verify_big_sparse_file(int h)
2998 unsigned check_buffer[BUFFER_N];
3002 int n = sizeof(check_buffer);
3004 int checks_failed = 0;
3005 int checks_passed = 0;
3007 for(i = 0; i < N_WRITES * STRIDE; i++) {
3009 memset(xxbuffer,0, n);
3011 set_buffer(i/STRIDE);
3013 pos = yaffs_lseek(h, offset, SEEK_SET);
3015 printf("mismatched seek pos %d offset %d\n",
3016 (int)pos, (int)offset);
3020 result = yaffs_read(h, check_buffer, n);
3023 printf("mismatched read result %d n %d\n", result, n);
3027 if(memcmp(xxbuffer, check_buffer, n)) {
3030 printf("buffer at %d mismatches\n", (int)pos);
3031 printf("xxbuffer ");
3032 for(j = 0; j < 20; j++)
3033 printf(" %d",xxbuffer[j]);
3035 printf("check_buffer ");
3036 for(j = 0; j < 20; j++)
3037 printf(" %d",check_buffer[j]);
3045 offset += sizeof(xxbuffer);
3048 printf("%d checks passed, %d checks failed\n", checks_passed, checks_failed);
3053 void large_file_test(const char *mountpt)
3055 char xx_buffer[1000];
3061 yaffs_trace_mask = 0;
3065 yaffs_mount(mountpt);
3066 printf("mounted\n");
3069 sprintf(fullname, "%s/%s", mountpt, "big-test-file");
3071 handle = yaffs_open(fullname, O_RDONLY, 0);
3073 handle = yaffs_open(fullname, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
3076 perror("opening file");
3080 write_big_sparse_file(handle);
3081 verify_big_sparse_file(handle);
3083 yaffs_close(handle);
3085 printf("Job done\n");
3086 yaffs_unmount(mountpt);
3088 yaffs_mount(mountpt);
3089 printf("mounted again\n");
3091 handle = yaffs_open(fullname, O_RDONLY, 0);
3092 verify_big_sparse_file(handle);
3093 yaffs_unmount(mountpt);
3096 yaffs_mount3(mountpt, 0, 1);
3097 printf("mounted with no checkpt\n");
3099 handle = yaffs_open(fullname, O_RDONLY, 0);
3100 verify_big_sparse_file(handle);
3101 yaffs_unmount(mountpt);
3103 /* Check resize by adding to the end, resizing back and verifying. */
3104 yaffs_mount3(mountpt, 0, 1);
3105 printf("checking resize\n");
3107 handle = yaffs_open(fullname, O_RDWR, 0);
3109 file_end = yaffs_lseek(handle, 0, SEEK_END);
3110 printf("file_end %d\n", (int)file_end);
3111 for(i = 0; i < 10000; i++)
3112 yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
3113 yaffs_ftruncate(handle, file_end);
3115 verify_big_sparse_file(handle);
3116 yaffs_unmount(mountpt);
3121 int mk_dir(const char *mp, const char *name)
3123 char full_name[100];
3125 sprintf(full_name, "%s/%s", mp, name);
3127 return yaffs_mkdir(full_name, S_IREAD| S_IWRITE);
3130 int mk_file(const char *mp, const char *name)
3132 char full_name[100];
3135 sprintf(full_name, "%s/%s", mp, name);
3137 h = yaffs_open(full_name, O_RDWR | O_CREAT | O_TRUNC, S_IREAD| S_IWRITE);
3139 yaffs_write(h, name, strlen(name));
3145 void xx_test(const char *mountpt)
3149 yaffs_format(mountpt,0,0,0);
3151 yaffs_mount(mountpt);
3152 printf("mounted\n");
3155 printf("create files\n");
3157 mk_dir(mountpt, "foo");
3158 mk_file(mountpt, "foo/f1");
3159 mk_file(mountpt, "foo/f2");
3160 mk_file(mountpt, "foo/f3");
3161 mk_file(mountpt, "foo/f4");
3162 dump_directory_tree(mountpt);
3164 printf("unmount and remount\n");
3166 /* Unmount/remount */
3167 yaffs_unmount(mountpt);
3168 yaffs_mount(mountpt);
3169 dump_directory_tree(mountpt);
3172 void yy_test(const char *mountpt)
3176 yaffs_mount(mountpt);
3177 dump_directory_tree(mountpt);
3181 void readdir_test(const char *mountpt)
3184 yaffs_DIR *dirs[100];
3186 yaffs_trace_mask = 0;
3190 yaffs_mount(mountpt);
3192 for(i = 0; i < 100; i++) {
3193 dirs[i] = yaffs_opendir(mountpt);
3194 printf("%2d %p,", i, dirs[i]);
3199 for(i = 0; i < 100; i++) {
3201 yaffs_closedir(dirs[i]);
3205 for(i = 0; i < 100; i++) {
3206 dirs[i] = yaffs_opendir(mountpt);
3207 printf("%2d %p,", i, dirs[i]);
3210 yaffs_unmount(mountpt);
3214 void format_test(const char *mountpt)
3220 ret = yaffs_format(mountpt, 0, 0, 0);
3221 printf("yaffs_format(...,0, 0, 0) of unmounted returned %d."
3222 " Should return 0\n\n\n", ret);
3224 yaffs_mount(mountpt);
3226 ret = yaffs_format(mountpt, 0, 0, 0);
3227 printf("yaffs_format(...,0, 0, 0) of mounted returned %d."
3228 " Should return -1 (busy)\n\n\n", ret);
3230 ret = yaffs_format(mountpt, 1, 0, 0);
3231 printf("yaffs_format(...,1, 0, 0) of mounted returned %d."
3232 " Should return 0.\n\n\n", ret);
3234 ret = yaffs_mount(mountpt);
3235 printf("mount should return 0 returned %d\n\n\n", ret);
3237 ret = yaffs_format(mountpt, 1, 0, 1);
3238 printf("yaffs_format(...,1, 0, 1) of mounted returned %d."
3239 " Should return 0.\n\n\n", ret);
3241 ret = yaffs_mount(mountpt);
3242 printf("mount should return -1 returned %d\n", ret);
3245 void dir_rename_test(const char *mountpt)
3253 yaffs_mount(mountpt);
3255 sprintf(fname,"%s/file",mountpt);
3256 sprintf(dname,"%s/directory",mountpt);
3258 h = yaffs_open(fname,O_CREAT | O_RDWR | O_TRUNC, 0666);
3261 yaffs_mkdir(dname, 0666);
3263 dump_directory_tree(mountpt);
3265 printf("Try to rename %s to %s\n", fname, dname);
3266 ret = yaffs_rename(fname, dname);
3267 printf("result %d, %d\n", ret, yaffs_get_error());
3269 printf("Try to rename %s to %s\n", dname, fname);
3270 ret = yaffs_rename(dname, fname);
3271 printf("result %d, %d\n", ret, yaffs_get_error());
3277 void dir_fd_test(const char *mountpt)
3284 yaffs_mount(mountpt);
3286 sprintf(name,"%s/directory",mountpt);
3287 yaffs_mkdir(name, 0666);
3288 for(i=0; i < 20; i++) {
3289 sprintf(name,"%s/directory/file%d",mountpt, i);
3291 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3292 yaffs_write(h, name, strlen(name));
3295 sprintf(name,"%s/dddd",mountpt);
3296 yaffs_mkdir(name, 0666);
3297 for(i=0; i < 20; i++) {
3298 sprintf(name,"%s/dddd/filezzz%d",mountpt, i);
3300 h = yaffs_open(name, O_CREAT | O_TRUNC | O_RDWR, 0666);
3301 yaffs_write(h, name, strlen(name));
3306 dump_directory_tree(mountpt);
3307 dump_directory_tree_fd(mountpt);
3308 dump_directory_tree_fd(mountpt);
3312 void create_delete_many_files_test(const char *mountpt)
3324 yaffs_mount(mountpt);
3326 for(i = 1; i < 2000; i++) {
3327 sprintf(fn,"%s/f%d",mountpt, i);
3328 fsize = (i%10) * 10000 + 20000;
3329 h = yaffs_open(fn, O_CREAT | O_TRUNC | O_RDWR, 0666);
3331 wrote = yaffs_write(h, buffer, sizeof(buffer));
3332 if (wrote != sizeof(buffer)) {
3333 printf("Writing file %s, only wrote %d bytes\n", fn, wrote);
3344 void find_device_check(void)
3347 yaffs_mount("/nand");
3349 yaffs_mkdir("/nandxxx", 0666);
3350 yaffs_mkdir("/nand/xxx", 0666);
3354 void try_opendir(const char *str)
3358 d = yaffs_opendir(str);
3360 printf("%s --> %p\n", str, d);
3366 void opendir_test(void)
3369 yaffs_mount("/nand/");
3370 yaffs_symlink("x", "/nand/sym");
3371 yaffs_mkdir("/nand/x",0666);
3372 yaffs_mkdir("/nand/y",0666);
3373 yaffs_mkdir("/nand/x/r",0666);
3374 yaffs_mkdir("/nand/x/s",0666);
3375 yaffs_mkdir("/nand/x/t",0666);
3377 try_opendir("nand/x/.");
3378 try_opendir("nand/x/r/..");
3379 try_opendir("nand/x/./");
3380 try_opendir("nand/x/r/../");
3381 try_opendir("/nand/x");
3382 try_opendir("/nand/x/");
3383 try_opendir("nand/x");
3384 try_opendir("nand/sym");
3385 try_opendir("nand/sym/");
3389 void try_rmdir(const char *str)
3393 ret= yaffs_rmdir(str);
3395 printf("rmdir(\"%s\") --> %d, errno %d\n", str, ret, yaffs_get_error());
3399 void rmdir_test2(void)
3403 yaffs_mount("/nand/");
3404 yaffs_mkdir("/nand/z",0666);
3405 try_rmdir("/nand/z");
3406 yaffs_mkdir("/nand/z",0666);
3407 try_rmdir("/nand/z/");
3411 int simulate_power_failure;
3413 int main(int argc, char *argv[])
3418 random_seed = time(NULL);
3419 //return long_test(argc,argv);
3421 //return cache_read_test();
3423 // resize_stress_test_no_grow("/flash/flash",20);
3424 //root_perm_remount("/flash/flash");
3426 //huge_directory_test_on_path("/ram2k");
3428 //yaffs_backward_scan_test("/flash/flash");
3429 // yaffs_device_flush_test("/flash/flash");
3431 //rename_over_test("//////////////////flash///////////////////yaffs1///////////");
3433 //fill_empty_files_test("/nand/");
3434 //resize_stress_test("/nand");
3435 //overwrite_test("/nand");
3437 //long_name_test("/nand");
3438 //link_test0("/nand");
3439 //link_test1("yaffs2");
3440 //scan_pattern_test("/flash",10000,10);
3441 short_scan_test("/yflash2",40000,200);
3442 //small_mount_test("/flash/flash",1000);
3443 //small_overwrite_test("/flash/flash",1000);
3444 //seek_overwrite_test("/flash/flash",1000);
3445 //checkpoint_fill_test("/flash/flash",20);
3446 //checkpoint_upgrade_test("/flash/flash",20);
3447 //small_overwrite_test("/flash/flash",1000);
3448 //checkpoint_fill_test("/flash/flash",20);
3449 //random_small_file_test("/flash/flash",10000);
3450 // huge_array_test("/flash/flash",10);
3453 // handle_test("yaffs2/");
3455 //long_test_on_path("/ram2k");
3456 // long_test_on_path("/flash");
3457 //simple_rw_test("/flash/flash");
3458 //fill_disk_test("/flash/flash");
3459 // rename_over_test("/flash");
3460 //lookup_test("/flash");
3461 //freespace_test("/flash/flash");
3463 //link_test("/flash/flash");
3465 // cache_bypass_bug_test();
3467 //free_space_check();
3469 //check_resize_gc_bug("/flash");
3471 //basic_xattr_test("/nand");
3472 //big_xattr_test("/nand");
3474 //null_name_test("yaffs2");
3476 //test_flash_traffic("yaffs2");
3477 // link_follow_test("/nand");
3478 //basic_utime_test("/nand");
3481 //format_test("/nand");
3483 //max_files_test("/nand");
3485 //start_twice("/nand");
3487 //large_file_test("/nand");
3488 //readdir_test("/nand");
3490 //basic_utime_test("/nand");
3491 //case_insensitive_test("/nand");
3494 //dir_rename_test("/nand");
3496 //dir_fd_test("/nand");
3498 //format_test("/nand");
3503 //create_delete_many_files_test("/nand");