Fix checkpointing bug
[yaffs2.git] / yaffs_checkptrw.c
index f56b311bbae4a814a4aeb836feccbad3bec214e5..afc7e470ba3f2ac0ae102e2ece981d572412564b 100644 (file)
@@ -13,7 +13,7 @@
  */
 
 const char *yaffs_checkptrw_c_version =
-    "$Id: yaffs_checkptrw.c,v 1.3 2006-05-21 09:39:12 charles Exp $";
+    "$Id: yaffs_checkptrw.c,v 1.6 2006-11-07 23:26:52 charles Exp $";
 
 
 #include "yaffs_checkptrw.h"
@@ -46,7 +46,7 @@ static int yaffs_CheckpointErase(yaffs_Device *dev)
                dev->startBlock,dev->endBlock));
                
        for(i = dev->startBlock; i <= dev->endBlock; i++) {
-               yaffs_BlockInfo *bi = &dev->blockInfo[i];
+               yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
                if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
                        T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
                        if(dev->eraseBlockInNAND(dev,i)){
@@ -77,10 +77,9 @@ static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
           blocksAvailable > 0){
        
                for(i = dev->checkpointNextBlock; i <= dev->endBlock; i++){
-                       yaffs_BlockInfo *bi = &dev->blockInfo[i];
+                       yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
                        if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
                                dev->checkpointNextBlock = i + 1;
-                               dev->nErasedBlocks--;
                                dev->checkpointCurrentBlock = i;
                                T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
                                return;
@@ -136,7 +135,7 @@ int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
                return 0;
                        
        if(!dev->checkpointBuffer)
-               dev->checkpointBuffer = YMALLOC(dev->nBytesPerChunk);
+               dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
        if(!dev->checkpointBuffer)
                return 0;
 
@@ -152,7 +151,7 @@ int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
        
        /* Erase all the blocks in the checkpoint area */
        if(forWriting){
-               memset(dev->checkpointBuffer,0,dev->nBytesPerChunk);
+               memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
                dev->checkpointByteOffset = 0;
                return yaffs_CheckpointErase(dev);
                
@@ -160,7 +159,7 @@ int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
        } else {
                int i;
                /* Set to a value that will kick off a read */
-               dev->checkpointByteOffset = dev->nBytesPerChunk;
+               dev->checkpointByteOffset = dev->nDataBytesPerChunk;
                /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
                 * going to be way more than we need */
                dev->blocksInCheckpoint = 0;
@@ -192,7 +191,14 @@ static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
        tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
        tags.chunkId = dev->checkpointPageSequence + 1;
        tags.sequenceNumber =  YAFFS_SEQUENCE_CHECKPOINT_DATA;
-       tags.byteCount = dev->nBytesPerChunk;
+       tags.byteCount = dev->nDataBytesPerChunk;
+       if(dev->checkpointCurrentChunk == 0){
+               /* First chunk we write for the block? Set block state to
+                  checkpoint */
+               yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
+               bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
+               dev->blocksInCheckpoint++;
+       }
        
        chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
                
@@ -204,7 +210,7 @@ static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
                dev->checkpointCurrentChunk = 0;
                dev->checkpointCurrentBlock = -1;
        }
-       memset(dev->checkpointBuffer,0,dev->nBytesPerChunk);
+       memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
        
        return 1;
 }
@@ -235,7 +241,7 @@ int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
                
                
                if(dev->checkpointByteOffset < 0 ||
-                  dev->checkpointByteOffset >= dev->nBytesPerChunk) 
+                  dev->checkpointByteOffset >= dev->nDataBytesPerChunk) 
                        ok = yaffs_CheckpointFlushBuffer(dev);
 
        }
@@ -261,7 +267,7 @@ int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
        
        
                if(dev->checkpointByteOffset < 0 ||
-                  dev->checkpointByteOffset >= dev->nBytesPerChunk) {
+                  dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
                   
                        if(dev->checkpointCurrentBlock < 0){
                                yaffs_CheckpointFindNextCheckpointBlock(dev);
@@ -315,7 +321,7 @@ int yaffs_CheckpointClose(yaffs_Device *dev)
        } else {
                int i;
                for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
-                       yaffs_BlockInfo *bi = &dev->blockInfo[dev->checkpointBlockList[i]];
+                       yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
                        if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
                                bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
                        else {