Runtime disbale of lazy loading. Fix problem where resize was breading rename shadowing
[yaffs2.git] / direct / yaffs_fileem2k.c
index 59d0c8efa12e3d51b1faf283bb6cdb8dcbec6ca2..398cc0868a65ed5f041523b6700de52ec5ffb183 100644 (file)
@@ -1,26 +1,27 @@
 /*
- * YAFFS: Yet another FFS. A NAND-flash specific file system. 
- * yaffs_ramdisk.c: yaffs ram disk component
+ * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002 Aleph One Ltd.
+ * Copyright (C) 2002-2007 Aleph One Ltd.
+ *   for Toby Churchill Ltd and Brightstar Engineering
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
- *
  */
 
-// This provides a YAFFS nand emulation on a file for emulating 2kB pages.
-// THis is only intended as test code to test persistence etc.
+/*
+ * This provides a YAFFS nand emulation on a file for emulating 2kB pages.
+ * This is only intended as test code to test persistence etc.
+ */
 
-const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.7 2006-10-13 08:52:49 charles Exp $";
+const char *yaffs_flashif2_c_version = "$Id: yaffs_fileem2k.c,v 1.20 2009-10-15 00:45:46 charles Exp $";
 
 
 #include "yportenv.h"
 
-#include "yaffs_flashif.h"
+#include "yaffs_flashif2.h"
 #include "yaffs_guts.h"
 #include "devextras.h"
 
@@ -34,6 +35,8 @@ const char *yaffs_flashif_c_version = "$Id: yaffs_fileem2k.c,v 1.7 2006-10-13 08
 
 
 
+#define REPORT_ERROR 0
+
 typedef struct 
 {
        __u8 data[PAGE_SIZE]; // Data + spare
@@ -48,7 +51,7 @@ typedef struct
 
 
 #define MAX_HANDLES 20
-#define BLOCKS_PER_HANDLE 8000
+#define BLOCKS_PER_HANDLE (32*8)
 
 typedef struct
 {
@@ -60,10 +63,40 @@ 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("  Fail simulated on chunkInNAND %d, at fail point %d\n",
+                               chunkInNAND, failPoint);
+       exit(0);
+  }
+}
+
+
+
+
+
+static __u8 localBuffer[PAGE_SIZE];
 
 static char *NToName(char *buf,int n)
 {
-       sprintf(buf,"emfile%d",n);
+       sprintf(buf,"emfile-2k-%d",n);
        return buf;
 }
 
@@ -98,16 +131,10 @@ static int GetBlockFileHandle(int n)
 static int  CheckInit(void)
 {
        static int initialised = 0;
-       int h;
        int i;
 
-       
-       off_t fSize;
-       off_t requiredSize;
-       int written;
        int blk;
-       
-       yflash_Page p;
+
        
        if(initialised) 
        {
@@ -116,7 +143,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;
@@ -132,62 +163,209 @@ static int  CheckInit(void)
 }
 
 
-int yflash_GetNumberOfBlocks(void)
+int yflash2_GetNumberOfBlocks(void)
 {
        CheckInit();
        
        return filedisk.nBlocks;
 }
 
-int yflash_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_ExtendedTags *tags)
+int yflash2_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
 {
        int written;
        int pos;
        int h;
+       int i;
+       int nRead;
+       int error;
        
+       T(YAFFS_TRACE_MTD,(TSTR("write chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
+
        CheckInit();
        
        
-       
-       if(data)
-       {
+       if(dev->inbandTags){
+               
+               yaffs_PackedTags2TagsPart * pt2tp;
+               pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
+               yaffs_PackTags2TagsPart(pt2tp,tags);
+               
                pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
                h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
-               
+                                                       
                lseek(h,pos,SEEK_SET);
-               written = write(h,data,dev->nDataBytesPerChunk);
+               written = write(h,data,dev->totalBytesPerChunk);
+
                
                if(yaffs_testPartialWrite){
                        close(h);
                        exit(1);
                }
                
-               if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
+               if(written != dev->totalBytesPerChunk) return YAFFS_FAIL;
+
+
        }
        
-       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))];
+       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))];
                
-               lseek(h,pos,SEEK_SET);
-               if( 0 && dev->isYaffs2)
+                       
+                       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);
+
+                       if( 0 && dev->isYaffs2)
+                       {
                        
-                       written = write(h,tags,sizeof(yaffs_ExtendedTags));
-                       if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
+                               written = write(h,tags,sizeof(yaffs_ExtendedTags));
+                               if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
+                       }
+                       else
+                       {
+                               yaffs_PackedTags2 pt;
+                               yaffs_PackTags2(dev,&pt,tags);
+                               __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;
+                       }
                }
-               else
+               
+               //yflash2_MaybePowerFail(chunkInNAND,2);
+               
+               /* Next do the whole write */
+               if(data)
                {
-                       yaffs_PackedTags2 pt;
-                       yaffs_PackTags2(&pt,tags);
+                       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");
+                       
+                       lseek(h,pos,SEEK_SET);
+                       written = write(h,localBuffer,dev->nDataBytesPerChunk);
+               
+                       if(yaffs_testPartialWrite){
+                               close(h);
+                               exit(1);
+                       }
 
-                       written = write(h,&pt,sizeof(pt));
-                       if(written != sizeof(pt)) return YAFFS_FAIL;
+
+                       if(written != dev->nDataBytesPerChunk) return YAFFS_FAIL;
                }
-       }
        
+               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);
 
+                       if( 0 && dev->isYaffs2)
+                       {
+                       
+                               written = write(h,tags,sizeof(yaffs_ExtendedTags));
+                               if(written != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
+                       }
+                       else
+                       {
+                               yaffs_PackedTags2 pt;
+                               yaffs_PackTags2(dev,&pt,tags);
+                               __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];
+                        
+                               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,3);
+       
+       }
        return YAFFS_OK;        
 
 }
@@ -207,75 +385,129 @@ int yaffs_CheckAllFF(const __u8 *ptr, int n)
 static int fail300 = 1;
 static int fail320 = 1;
 
-int yflash_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
+static int failRead10 = 2;
+
+int yflash2_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags)
 {
        int nread;
        int pos;
        int h;
+       int localData = 0;
+       int retval = YAFFS_OK;
+       int nRead;
+       
+       T(YAFFS_TRACE_MTD,(TSTR("read chunk %d data %x tags %x" TENDSTR),chunkInNAND,(unsigned)data, (unsigned)tags));
        
        CheckInit();
        
        
        
-       if(data)
-       {
+       
+       if(dev->inbandTags){
+               /* Got to suck the tags out of the data area */
+               if(!data) {
+                       localData=1;
+                       data = yaffs_GetTempBuffer(dev,__LINE__);
+               }
 
+               
+               yaffs_PackedTags2TagsPart * pt2tp;
+               pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
+
+               
                pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
-               h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
+               h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];
+               
                lseek(h,pos,SEEK_SET);
-               nread = read(h,data,dev->nDataBytesPerChunk);
+
+               nRead = read(h, data,dev->totalBytesPerChunk);
+
+               yaffs_UnpackTags2TagsPart(tags,pt2tp);
                
-               if(nread != dev->nDataBytesPerChunk) return YAFFS_FAIL;
+               if(nread != dev->totalBytesPerChunk)
+                       retval = YAFFS_FAIL;
+                       
+               if(localData)
+                       yaffs_ReleaseTempBuffer(dev,data,__LINE__);
+
+
+
        }
        
-       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);
+       else {
+       
+               if(data)
+               {
 
-               if(0 && dev->isYaffs2)
+                       pos = (chunkInNAND % (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE)) * PAGE_SIZE;
+                       h = filedisk.handle[(chunkInNAND / (PAGES_PER_BLOCK * BLOCKS_PER_HANDLE))];             
+                       lseek(h,pos,SEEK_SET);
+                       nread = read(h,data,dev->nDataBytesPerChunk);
+               
+               
+                       if(nread != dev->nDataBytesPerChunk) 
+                               retval = YAFFS_FAIL;
+               }
+       
+               if(tags)
                {
-                       nread= read(h,tags,sizeof(yaffs_ExtendedTags));
-                       if(nread != sizeof(yaffs_ExtendedTags)) return YAFFS_FAIL;
-                       if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
+                       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);
+
+                       if(0 && dev->isYaffs2)
                        {
-                               yaffs_InitialiseTags(tags);
+                               nread= read(h,tags,sizeof(yaffs_ExtendedTags));
+                               if(nread != sizeof(yaffs_ExtendedTags))
+                                        retval =  YAFFS_FAIL;
+                               if(yaffs_CheckAllFF((__u8 *)tags,sizeof(yaffs_ExtendedTags)))
+                               {
+                                       yaffs_InitialiseTags(tags);
+                               }
+                               else
+                               {
+                                       tags->chunkUsed = 1;
+                               }
                        }
                        else
                        {
-                               tags->chunkUsed = 1;
-                       }
-               }
-               else
-               {
-                       yaffs_PackedTags2 pt;
-                       nread= read(h,&pt,sizeof(pt));
-                       yaffs_UnpackTags2(tags,&pt);
-                       if((chunkInNAND >> 6) == 300) {
-                           if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
-                              tags->eccResult = YAFFS_ECC_RESULT_FIXED;
-                              fail300 = 0;
-                           }
-                           
-                       }
-                       if((chunkInNAND >> 6) == 320) {
-                           if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
-                              tags->eccResult = YAFFS_ECC_RESULT_FIXED;
-                              fail320 = 0;
-                           }
+                               yaffs_PackedTags2 pt;
+                               nread= read(h,&pt,sizeof(pt));
+                               yaffs_UnpackTags2(dev,tags,&pt);
+#ifdef SIMULATE_FAILURES
+                               if((chunkInNAND >> 6) == 100) {
+                                       if(fail300 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
+                                               tags->eccResult = YAFFS_ECC_RESULT_FIXED;
+                                               fail300 = 0;
+                                       }
+                               }
+                               
+                               if((chunkInNAND >> 6) == 110) {
+                                       if(fail320 && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR){
+                                               tags->eccResult = YAFFS_ECC_RESULT_FIXED;
+                                               fail320 = 0;
+                                       }
+                               }
+#endif
+                               if(failRead10>0 && chunkInNAND == 10){
+                                       failRead10--;
+                                       nread = 0;
+                               }
+                       
+                               if(nread != sizeof(pt))
+                                       retval = YAFFS_FAIL;
                        }
-                       if(nread != sizeof(pt)) return YAFFS_FAIL;
                }
        }
        
 
-       return YAFFS_OK;        
+
+       return retval;  
 
 }
 
 
-int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
+int yflash2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
 {
        int written;
        int h;
@@ -296,7 +528,7 @@ int yflash_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
        
 }
 
-int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
+int yflash2_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
 {
 
        int i;
@@ -304,6 +536,8 @@ int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
                
        CheckInit();
        
+       printf("erase block %d\n",blockNumber);
+       
        if(blockNumber == 320)
                fail320 = 1;
        
@@ -335,7 +569,7 @@ int yflash_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
        
 }
 
-int yflash_InitialiseNAND(yaffs_Device *dev)
+int yflash2_InitialiseNAND(yaffs_Device *dev)
 {
        CheckInit();
        
@@ -345,7 +579,7 @@ int yflash_InitialiseNAND(yaffs_Device *dev)
 
 
 
-int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
+int yflash2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber)
 {
        yaffs_ExtendedTags tags;
        int chunkNo;
@@ -354,7 +588,7 @@ int yflash_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_Blo
        
        chunkNo = blockNo * dev->nChunksPerBlock;
        
-       yflash_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
+       yflash2_ReadChunkWithTagsFromNAND(dev,chunkNo,NULL,&tags);
        if(tags.blockBad)
        {
                *state = YAFFS_BLOCK_STATE_DEAD;