yaffs: Add xattrib support
[yaffs2.git] / direct / yaffs_fileem2k.c
index 82a00a255b91b0417c6ac7a79705855f803d8dad..84b7eb80469a0dd2e3e21c1a9ab0bb8395035e98 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002-2007 Aleph One Ltd.
+ * Copyright (C) 2002-2010 Aleph One Ltd.
  *   for Toby Churchill Ltd and Brightstar Engineering
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
  * This is only intended as test code to test persistence etc.
  */
 
-const char *yaffs_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.14 2008-07-02 20:17:41 charles Exp $";
+const char *yaffs_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.24 2010-02-18 01:18:04 charles Exp $";
 
 
 #include "yportenv.h"
+#include "yaffs_trace.h"
 
 #include "yaffs_flashif2.h"
 #include "yaffs_guts.h"
@@ -33,7 +34,6 @@ const char *yaffs_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.14 2008-07-02
 #include "yaffs_fileem2k.h"
 #include "yaffs_packedtags2.h"
 
-//#define SIMULATE_FAILURES
 
 
 #define REPORT_ERROR 0
@@ -52,7 +52,7 @@ typedef struct
 
 
 #define MAX_HANDLES 20
-#define BLOCKS_PER_HANDLE 8000
+#define BLOCKS_PER_HANDLE (32*8)
 
 typedef struct
 {
@@ -64,6 +64,32 @@ static yflash_Device filedisk;
 
 int yaffs_testPartialWrite = 0;
 
+extern int random_seed;
+extern int simulate_power_failure;
+static int initialised = 0;
+static int remaining_ops;
+static int nops_so_far;
+
+int ops_multiplier;
+
+
+static void yflash2_MaybePowerFail(unsigned int chunkInNAND, int failPoint)
+{
+
+   nops_so_far++;
+   
+   
+   remaining_ops--;
+   if(simulate_power_failure &&
+      remaining_ops < 1){
+       printf("Simulated power failure after %d operations\n",nops_so_far);
+       printf("  power failed on chunkInNAND %d, at fail point %d\n",
+                               chunkInNAND, failPoint);
+       exit(0);
+  }
+}
+
+
 
 
 
@@ -71,7 +97,7 @@ static __u8 localBuffer[PAGE_SIZE];
 
 static char *NToName(char *buf,int n)
 {
-       sprintf(buf,"emfile2k%d",n);
+       sprintf(buf,"emfile-2k-%d",n);
        return buf;
 }
 
@@ -118,7 +144,11 @@ static int  CheckInit(void)
 
        initialised = 1;
        
-       memset(dummyBuffer,0xff,sizeof(dummyBuffer));
+
+       srand(random_seed);
+       remaining_ops = (rand() % 1000) * 5;
+       memset(dummyBuffer,0xff,sizeof(dummyBuffer));
+
        
        
        filedisk.nBlocks = SIZE_IN_MB * BLOCKS_PER_MB;
@@ -155,7 +185,7 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
        CheckInit();
        
        
-       if(dev->inbandTags){
+       if(dev->param.inbandTags){
                
                yaffs_PackedTags2TagsPart * pt2tp;
                pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
@@ -165,7 +195,7 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
                h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
                                                        
                lseek(h,pos,SEEK_SET);
-               written = write(h,data,dev->totalBytesPerChunk);
+               written = write(h,data,dev->param.totalBytesPerChunk);
 
                
                if(yaffs_testPartialWrite){
@@ -173,30 +203,110 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
                        exit(1);
                }
                
-               if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
+               if(written != dev->param.totalBytesPerChunk) return YAFFS_FAIL;
 
 
        }
        
        else {
-       
+               /* First do a write of a partial page */
+               int n_partials;
+               int bpos;
+
                if(data)
                {
                        pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
                        h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
                
+                       
+                       memcpy(localBuffer,data, dev->nDataBytesPerChunk);
+                       
+                       n_partials = rand()%20;
+                       
+                       for(i = 0; i < n_partials; i++){
+                               bpos = rand() % dev->nDataBytesPerChunk;
+                               
+                               localBuffer[bpos] |= (1 << (rand() & 7));
+                       }
+                 
+                       if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
+                               printf("nand simulator: data does not match\n");
+                       
+                       lseek(h,pos,SEEK_SET);
+                       written = write(h,localBuffer,dev->nDataBytesPerChunk);
+               
+                       if(yaffs_testPartialWrite){
+                               close(h);
+                               exit(1);
+                       }
+
+
+                       if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
+               }
+               // yflash2_MaybePowerFail(chunkInNAND,1);
+       
+               if(tags)
+               {
+                       pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE + PAGE_DATA_SIZE ;
+                       h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
+               
                        lseek(h,pos,SEEK_SET);
-                       nRead =  read(h, localBuffer,dev->nDataBytesPerChunk);
-                       for(i = error = 0; i < dev->nDataBytesPerChunk && !error; i++){
-                               if(REPORT_ERROR && localBuffer[i] != 0xFF){
-                                       printf("nand simulation: chunk %d data byte %d was %0x2\n",
-                                               chunkInNAND,i,localBuffer[i]);
-                                       error = 1;
+
+                       if( 0 && dev->param.isYaffs2)
+                       {
+                       
+                               written = write(h,tags,sizeof(yaffs_ExtendedTags));
+                               if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
+                       }
+                       else
+                       {
+                               yaffs_PackedTags2 pt;
+                               yaffs_PackTags2(&pt,tags, !dev->param.noTagsECC);
+                               __u8 * ptab = (__u8 *)&pt;
+
+                               nRead = read(h,localBuffer,sizeof(pt));
+                               for(i = error = 0; REPORT_ERROR && i < sizeof(pt) && !error; i++){
+                                       if(localBuffer[i] != 0xFF){
+                                               printf("nand simulation: chunk %d oob byte %d was %0x2\n",
+                                                       chunkInNAND,i,localBuffer[i]);
+                                                       error = 1;
+                                       }
                                }
+               
+                               for(i = 0; i < sizeof(pt); i++)
+                               localBuffer[i] &= ptab[i];
+                               
+                               n_partials = rand()% sizeof(pt);
+                       
+                               for(i = 0; i < n_partials; i++){
+                                       bpos = rand() % sizeof(pt);
+                               
+                                       localBuffer[bpos] |= (1 << (rand() & 7));
+                               }                       
+                        
+                               if(REPORT_ERROR && memcmp(localBuffer,&pt,sizeof(pt)))
+                                       printf("nand sim: tags corruption\n");
+                               
+                               lseek(h,pos,SEEK_SET);
+                       
+                               written = write(h,localBuffer,sizeof(pt));
+                               if(written != sizeof(pt)) return YAFFS_FAIL;
                        }
+               }
+               
+               //yflash2_MaybePowerFail(chunkInNAND,2);
                
-                       for(i = 0; i < dev->nDataBytesPerChunk; i++)
-                       localBuffer[i] &= data[i];
+               /* Next do the whole write */
+               if(data)
+               {
+                       pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
+                       h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
+               
+                       
+                       memset(localBuffer,0xFF, PAGE_SIZE);            
+                       for(i = 0; i < dev->nDataBytesPerChunk; i++){
+                               localBuffer[i] &= data[i];
+                       }
                  
                        if(REPORT_ERROR && memcmp(localBuffer,data,dev->nDataBytesPerChunk))
                                printf("nand simulator: data does not match\n");
@@ -208,14 +318,6 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
                                close(h);
                                exit(1);
                        }
-               
-#ifdef SIMULATE_FAILURES
-                               if((chunkInNAND >> 6) == 100) 
-                               written = 0;
-
-                               if((chunkInNAND >> 6) == 110) 
-                               written = 0;
-#endif
 
 
                        if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
@@ -228,7 +330,7 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
                
                        lseek(h,pos,SEEK_SET);
 
-                       if( 0 && dev->isYaffs2)
+                       if( 0 && dev->param.isYaffs2)
                        {
                        
                                written = write(h,tags,sizeof(yaffs_ExtendedTags));
@@ -237,7 +339,7 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
                        else
                        {
                                yaffs_PackedTags2 pt;
-                               yaffs_PackTags2(&pt,tags);
+                               yaffs_PackTags2(&pt,tags,!dev->param.noTagsECC);
                                __u8 * ptab = (__u8 *)&pt;
 
                                nRead = read(h,localBuffer,sizeof(pt));
@@ -261,6 +363,8 @@ int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u
                                if(written != sizeof(pt)) return YAFFS_FAIL;
                        }
                }
+               
+               yflash2_MaybePowerFail(chunkInNAND,3);
        
        }
        return YAFFS_OK;        
@@ -300,7 +404,7 @@ int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *d
        
        
        
-       if(dev->inbandTags){
+       if(dev->param.inbandTags){
                /* Got to suck the tags out of the data area */
                if(!data) {
                        localData=1;
@@ -317,11 +421,11 @@ int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *d
                
                lseek(h,pos,SEEK_SET);
 
-               nRead = read(h, data,dev->totalBytesPerChunk);
+               nRead = read(h, data,dev->param.totalBytesPerChunk);
 
                yaffs_UnpackTags2TagsPart(tags,pt2tp);
                
-               if(nread != dev->totalBytesPerChunk)
+               if(nread != dev->param.totalBytesPerChunk)
                        retval = YAFFS_FAIL;
                        
                if(localData)
@@ -352,7 +456,7 @@ int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *d
                        h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
                        lseek(h,pos,SEEK_SET);
 
-                       if(0 && dev->isYaffs2)
+                       if(0 && dev->param.isYaffs2)
                        {
                                nread= read(h,tags,sizeof(yaffs_ExtendedTags));
                                if(nread != sizeof(yaffs_ExtendedTags))
@@ -370,7 +474,7 @@ int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *d
                        {
                                yaffs_PackedTags2 pt;
                                nread= read(h,&pt,sizeof(pt));
-                               yaffs_UnpackTags2(tags,&pt);
+                               yaffs_UnpackTags2(tags,&pt, !dev->param.noTagsECC);
 #ifdef SIMULATE_FAILURES
                                if((chunkInNAND >> 6) == 100) {
                                        if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
@@ -415,7 +519,7 @@ int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
        
        memset(&pt,0,sizeof(pt));
        h = filedisk.handle[(blockNo / ( BLOCKS_PER_HANDLE))];
-       lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
+       lseek(h,((blockNo % BLOCKS_PER_HANDLE) * dev->param.nChunksPerBlock) * PAGE_SIZE + PAGE_DATA_SIZE,SEEK_SET);
        written = write(h,&pt,sizeof(pt));
                
        if(written != sizeof(pt)) return YAFFS_FAIL;
@@ -454,8 +558,8 @@ int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
                
 
                h = filedisk.handle[(blockNumber / ( BLOCKS_PER_HANDLE))];
-               lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->nChunksPerBlock) * PAGE_SIZE,SEEK_SET);               
-               for(i = 0; i < dev->nChunksPerBlock; i++)
+               lseek(h,((blockNumber % BLOCKS_PER_HANDLE) * dev->param.nChunksPerBlock) * PAGE_SIZE,SEEK_SET);         
+               for(i = 0; i < dev->param.nChunksPerBlock; i++)
                {
                        write(h,pg,PAGE_SIZE);
                }
@@ -483,7 +587,7 @@ int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_Bl
 
        *sequenceNumber = 0;
        
-       chunkNo = blockNo * dev->nChunksPerBlock;
+       chunkNo = blockNo * dev->param.nChunksPerBlock;
        
        yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
        if(tags.blockBad)