Handle unfixed ecc errors better when scanning
[yaffs2.git] / yaffs_guts.c
index 40d534585ba6082f3cf8ba5a8b1cac10b888eafa..76074cd51d8b689363a17fcb9a944f6d9af5bf0f 100644 (file)
@@ -11,8 +11,9 @@
  * published by the Free Software Foundation.
  */
 
+
 const char *yaffs_guts_c_version =
-    "$Id: yaffs_guts.c,v 1.69 2009-01-04 21:44:23 charles Exp $";
+    "$Id: yaffs_guts.c,v 1.72 2009-01-16 00:44:45 charles Exp $";
 
 #include "yportenv.h"
 
@@ -4363,7 +4364,6 @@ static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
                        }
                } else if (level == 0) {
                        __u32 baseOffset = chunkOffset <<  YAFFS_TNODES_LEVEL0_BITS;
-                       /* printf("write tnode at %d\n",baseOffset); */
                        ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
                        if(ok)
                                ok = (yaffs_CheckpointWrite(dev,tn,tnodeSize) == tnodeSize);
@@ -4412,7 +4412,6 @@ static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
                /* Read level 0 tnode */
                
                
-               /* printf("read  tnode at %d\n",baseChunk); */
                tn = yaffs_GetTnodeRaw(dev);
                if(tn)
                        ok = (yaffs_CheckpointRead(dev,tn,tnodeSize) == tnodeSize);
@@ -4847,7 +4846,8 @@ int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
         int nToWriteBack;
         int startOfWrite = offset;
         int chunkWritten = 0;
-        int nBytesRead;
+        __u32 nBytesRead;
+        __u32 chunkStart;
 
        yaffs_Device *dev;
 
@@ -4877,9 +4877,12 @@ int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
                         * we need to write back as much as was there before.
                         */
 
-                       nBytesRead =
-                           in->variant.fileVariant.fileSize -
-                           ((chunk - 1) * dev->nDataBytesPerChunk);
+                       chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
+
+                       if(chunkStart > in->variant.fileVariant.fileSize)
+                               nBytesRead = 0; /* Past end of file */
+                       else
+                               nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
 
                        if (nBytesRead > dev->nDataBytesPerChunk) {
                                nBytesRead = dev->nDataBytesPerChunk;
@@ -4887,7 +4890,10 @@ int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
 
                        nToWriteBack =
                            (nBytesRead >
-                            ((int)start + n)) ? nBytesRead : (start + n);
+                            (start + n)) ? nBytesRead : (start + n);
+                       
+                       if(nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
+                               YBUG();
 
                } else {
                        nToCopy = dev->nDataBytesPerChunk - start;
@@ -5507,6 +5513,35 @@ struct yaffs_ShadowFixerStruct {
        struct yaffs_ShadowFixerStruct *next;
 };
 
+
+static void yaffs_StripDeletedObjects(yaffs_Device *dev)
+{
+       /*
+       *  Sort out state of unlinked and deleted objects after scanning.
+       */
+       struct ylist_head *i;
+       struct ylist_head *n;
+       yaffs_Object *l;
+
+       /* Soft delete all the unlinked files */
+       ylist_for_each_safe(i, n,
+               &dev->unlinkedDir->variant.directoryVariant.children) {
+               if (i) {
+                       l = ylist_entry(i, yaffs_Object, siblings);
+                       yaffs_DestroyObject(l);
+               }
+       }
+       
+       ylist_for_each_safe(i, n,
+               &dev->deletedDir->variant.directoryVariant.children) {
+               if (i) {
+                       l = ylist_entry(i, yaffs_Object, siblings);
+                       yaffs_DestroyObject(l);
+               }
+       }
+
+}
+
 static int yaffs_Scan(yaffs_Device * dev)
 {
        yaffs_ExtendedTags tags;
@@ -5907,25 +5942,6 @@ static int yaffs_Scan(yaffs_Device * dev)
 
        yaffs_HardlinkFixup(dev,hardList);
        
-       /* Handle the unlinked files. Since they were left in an unlinked state we should
-         * just delete them.
-         */
-        {
-                struct ylist_head *i;
-                struct ylist_head *n;
-
-                yaffs_Object *l;
-                /* Soft delete all the unlinked files */
-                ylist_for_each_safe(i, n,
-                                   &dev->unlinkedDir->variant.directoryVariant.
-                                   children) {
-                        if (i) {
-                                l = ylist_entry(i, yaffs_Object, siblings);
-                                yaffs_DestroyObject(l);
-                        }
-                }
-       }
-
        /* Fix up any shadowed objects */
        {
                struct yaffs_ShadowFixerStruct *fixer;
@@ -6269,7 +6285,11 @@ static int yaffs_ScanBackwards(yaffs_Device * dev)
 
                                dev->nFreeChunks++;
                                
-                       } else if (tags.chunkId > 0) {
+                       } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED){
+                               printf("Error in ECC\n");
+                               /* Don't actually delete because the chunk is not yet set up as being in use */
+                               /* yaffs_DeleteChunk(dev, chunk, 1, __LINE__); */
+                       }else if (tags.chunkId > 0) {
                                /* chunkId > 0 so it is a data chunk... */
                                unsigned int endpos;
                                __u32 chunkBase =
@@ -6657,37 +6677,6 @@ static int yaffs_ScanBackwards(yaffs_Device * dev)
         */
        yaffs_HardlinkFixup(dev,hardList);
        
-       
-       /*
-        *  Sort out state of unlinked and deleted objects.
-        */
-        {
-                struct ylist_head *i;
-                struct ylist_head *n;
-
-                yaffs_Object *l;
-
-                /* Soft delete all the unlinked files */
-                ylist_for_each_safe(i, n,
-                                   &dev->unlinkedDir->variant.directoryVariant.
-                                   children) {
-                        if (i) {
-                                l = ylist_entry(i, yaffs_Object, siblings);
-                                yaffs_DestroyObject(l);
-                        }
-                }
-
-                /* Soft delete all the deletedDir files */
-                ylist_for_each_safe(i, n,
-                                   &dev->deletedDir->variant.directoryVariant.
-                                   children) {
-                        if (i) {
-                                l = ylist_entry(i, yaffs_Object, siblings);
-                                yaffs_DestroyObject(l);
-
-                        }
-               }
-       }
 
        yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
        
@@ -7500,6 +7489,8 @@ int yaffs_GutsInitialise(yaffs_Device * dev)
                }else
                        if(!yaffs_Scan(dev))
                                init_failed = 1;
+
+               yaffs_StripDeletedObjects(dev);
        }
                
        if(init_failed){