*** empty log message ***
[yaffs/.git] / yaffs_guts.c
index d82da3fc9bec225fc1975c573552d1d8e73ec4e3..5784d7956cc63d008c4df3045968e0839c90f70a 100644 (file)
  */
  //yaffs_guts.c
 
-const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.24 2003-04-03 17:58:56 charles Exp $";
+const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.25 2003-05-20 22:30:35 charles Exp $";
 
 #include "yportenv.h"
 
 #include "yaffsinterface.h"
 #include "yaffs_guts.h"
 
-
-
-
 #define YAFFS_PASSIVE_GC_CHUNKS 2
 
+#if 0
+// Use Steven Hill's ECC struff instead
 // External functions for ECC on data
 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
-
+#define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
+#define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
+#else
+#include "yaffs_ecc.h"
+#endif
 
 // countBits is a quick way of counting the number of bits in a byte.
 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
@@ -239,10 +242,10 @@ int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
                int eccResult1,eccResult2;
                __u8 calcEcc[3];
                 
-                       nand_calculate_ecc(data,calcEcc);
-                       eccResult1 = nand_correct_data (data,spare->ecc1, calcEcc);
-                       nand_calculate_ecc(&data[256],calcEcc);
-                       eccResult2 = nand_correct_data (&data[256],spare->ecc2, calcEcc);
+                       yaffs_ECCCalculate(data,calcEcc);
+                       eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
+                       yaffs_ECCCalculate(&data[256],calcEcc);
+                       eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
 
                        if(eccResult1>0)
                        {
@@ -582,8 +585,8 @@ void yaffs_SetObjectName(yaffs_Object *obj, const char *name)
 
 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
 {
-       nand_calculate_ecc (data , spare->ecc1);
-       nand_calculate_ecc (&data[256] , spare->ecc2);
+       yaffs_ECCCalculate(data , spare->ecc1);
+       yaffs_ECCCalculate(&data[256] , spare->ecc2);
 }
 
 void yaffs_CalcTagsECC(yaffs_Tags *tags)
@@ -3620,7 +3623,7 @@ int yaffs_ResizeFile(yaffs_Object *in, int newSize)
                        // using yaffs_DeleteChunk
 
                        chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
-                       if(chunkId <= 0 || chunkId >= (dev->endBlock * 32))
+                       if(chunkId < (dev->startBlock * 32) || chunkId >= ((dev->endBlock+1) * 32))
                        {
                                //T(("Found daft chunkId %d for %d\n",chunkId,i));
                        }
@@ -4825,6 +4828,7 @@ int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
        int nFree;
        int pending;
        int b;
+       int nDirtyCacheChunks=0;
        
        yaffs_BlockInfo *blk;
        
@@ -4872,6 +4876,20 @@ int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
 
        nFree += pending;
        
+       // Now count the number of dirty chunks in the cache and subtract those
+       
+       {
+               int i;
+               for(i = 0; i < dev->nShortOpCaches; i++)
+               {
+                       if(dev->srCache[i].dirty) nDirtyCacheChunks++;
+               }
+       }
+       
+       nFree -= nDirtyCacheChunks;
+       
+       nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
+       
        if(nFree < 0) nFree = 0;
 
        return nFree;