Handle unfixed ecc errors better when scanning
[yaffs2.git] / yaffs_guts.c
index a6775a9498cae9e6870a83df509c2fd32fb61b9b..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.63 2008-11-21 02:17:32 charles Exp $";
+    "$Id: yaffs_guts.c,v 1.72 2009-01-16 00:44:45 charles Exp $";
 
 #include "yportenv.h"
 
@@ -406,14 +407,11 @@ static int yaffs_SkipVerification(yaffs_Device *dev)
        return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
 }
 
-#if 0
 static int yaffs_SkipFullVerification(yaffs_Device *dev)
 {
        return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
 }
 
-#endif
-
 static int yaffs_SkipNANDVerification(yaffs_Device *dev)
 {
        return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
@@ -485,9 +483,10 @@ static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int
        yaffs_VerifyBlock(dev,bi,n);
        
        /* After collection the block should be in the erased state */
-       /* TODO: This will need to change if we do partial gc */
+       /* This will need to change if we do partial gc */
        
-       if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
+       if(bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
+          bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
                T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
                        n,bi->blockState));
        }
@@ -2993,17 +2992,17 @@ static int yaffs_GetErasedChunks(yaffs_Device * dev)
 
 }
 
-static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
+static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block, int wholeBlock)
 {
        int oldChunk;
        int newChunk;
-       int chunkInBlock;
        int markNAND;
        int retVal = YAFFS_OK;
        int cleanups = 0;
        int i;
        int isCheckpointBlock;
        int matchingChunk;
+       int maxCopies;
 
        int chunksBefore = yaffs_GetErasedChunks(dev);
        int chunksAfter;
@@ -3019,8 +3018,11 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
        bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
 
        T(YAFFS_TRACE_TRACING,
-         (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
-          bi->pagesInUse, bi->hasShrinkHeader));
+         (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR), 
+         block,
+         bi->pagesInUse,
+         bi->hasShrinkHeader,
+         wholeBlock));
 
        /*yaffs_VerifyFreeChunks(dev); */
 
@@ -3046,14 +3048,20 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
                
                yaffs_VerifyBlock(dev,bi,block);
 
-               for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
+               maxCopies = (wholeBlock) ? dev->nChunksPerBlock : 10;
+               oldChunk = block * dev->nChunksPerBlock + dev->gcChunk;
+               
+               for ( /* init already done */;
                     retVal == YAFFS_OK &&
-                    chunkInBlock < dev->nChunksPerBlock
-                    && yaffs_StillSomeChunkBits(dev, block);
-                    chunkInBlock++, oldChunk++) {
-                       if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
+                    dev->gcChunk < dev->nChunksPerBlock &&
+                    (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)&&
+                    maxCopies > 0;
+                    dev->gcChunk++, oldChunk++) {
+                       if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
 
                                /* This page is in use and might need to be copied off */
+                               
+                               maxCopies--;
 
                                markNAND = 1;
 
@@ -3068,8 +3076,8 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
 
                                T(YAFFS_TRACE_GC_DETAIL,
                                  (TSTR
-                                  ("Collecting page %d, %d %d %d " TENDSTR),
-                                  chunkInBlock, tags.objectId, tags.chunkId,
+                                  ("Collecting chunk in block %d, %d %d %d " TENDSTR),
+                                  dev->gcChunk, tags.objectId, tags.chunkId,
                                   tags.byteCount));
                                   
                                if(object && !yaffs_SkipVerification(dev)){
@@ -3148,7 +3156,6 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
                                                yaffs_ObjectHeader *oh;
                                                oh = (yaffs_ObjectHeader *)buffer;
                                                oh->isShrink = 0;
-                                               oh->shadowsObject = oh->inbandShadowsObject = -1;
                                                tags.extraShadows = 0;
                                                tags.extraIsShrinkHeader = 0;
                                                
@@ -3185,6 +3192,7 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
                }
 
                yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
+               
 
 
                /* Do any required cleanups */
@@ -3219,6 +3227,12 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
                    TENDSTR), chunksBefore, chunksAfter));
        }
 
+       /* If the gc completed then clear the current gcBlock so that we find another. */
+       if(bi->blockState != YAFFS_BLOCK_STATE_COLLECTING){
+               dev->gcBlock = -1;
+               dev->gcChunk = 0;
+       }
+       
        dev->isDoingGC = 0;
 
        return retVal;
@@ -3266,7 +3280,12 @@ static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
                        aggressive = 0;
                }
 
-               block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
+               if(dev->gcBlock <= 0){
+                       dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive);
+                       dev->gcChunk = 0;
+               }
+               
+               block = dev->gcBlock;
 
                if (block > 0) {
                        dev->garbageCollections++;
@@ -3279,7 +3298,7 @@ static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
                           ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
                           dev->nErasedBlocks, aggressive));
 
-                       gcOk = yaffs_GarbageCollectBlock(dev, block);
+                       gcOk = yaffs_GarbageCollectBlock(dev,block,aggressive);
                }
 
                if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
@@ -3288,8 +3307,9 @@ static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
                           ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
                            TENDSTR), dev->nErasedBlocks, maxTries, block));
                }
-       } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
-                && (maxTries < 2));
+       } while ((dev->nErasedBlocks < dev->nReservedBlocks) && 
+                (block > 0) &&
+                (maxTries < 2));
 
        return aggressive ? gcOk : YAFFS_OK;
 }
@@ -4344,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);
@@ -4393,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);
@@ -4828,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;
 
@@ -4858,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;
@@ -4869,6 +4891,9 @@ int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
                        nToWriteBack =
                            (nBytesRead >
                             (start + n)) ? nBytesRead : (start + n);
+                       
+                       if(nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
+                               YBUG();
 
                } else {
                        nToCopy = dev->nDataBytesPerChunk - start;
@@ -5488,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;
@@ -5888,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;
@@ -6250,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 =
@@ -6638,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__);
        
@@ -6690,17 +6698,23 @@ static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
         
         int count = 0;
 
-       if(!obj)
+       if(!obj){
+               T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
                YBUG();
+       }
 
         if(yaffs_SkipVerification(obj->myDev))
                 return;
 
-       if(!obj->parent)
+       if(!obj->parent){
+               T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
                YBUG();
+       }
                
-       if(obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
+       if(obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
+               T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
                YBUG();
+       }
        
         /* Iterate through the objects in each hash entry */
          
@@ -6713,9 +6727,10 @@ static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
                 }
         }
         
-        if(count != 1)
-               YBUG();
-        
+        if(count != 1){
+               T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR),count));
+               YBUG();
+       }
 
 }
 
@@ -6728,20 +6743,24 @@ static void yaffs_VerifyDirectory(yaffs_Object *directory)
        if(!directory)
                YBUG();
 
-        if(yaffs_SkipVerification(directory->myDev))
+        if(yaffs_SkipFullVerification(directory->myDev))
                 return;
 
                
-       if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
+       if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
+               T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR),directory->variantType));
                YBUG();
+       }
        
         /* Iterate through the objects in each hash entry */
          
         ylist_for_each(lh, &directory->variant.directoryVariant.children) {
                if (lh) {
                         listObj = ylist_entry(lh, yaffs_Object, siblings);
-                       if(listObj->parent != directory)
+                       if(listObj->parent != directory){
+                               T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR),listObj->parent));
                                YBUG();
+                       }
                        yaffs_VerifyObjectInDirectory(listObj);
                 }
         }
@@ -6943,9 +6962,18 @@ int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
                yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
        } else if (obj->hdrChunk <= 0) {
                YCHAR locName[20];
+               YCHAR numString[20];
+               YCHAR *x = &numString[19];
+               unsigned v = obj->objectId;
+               numString[19] = 0;
+               while(v>0){
+                       x--;
+                       *x = '0' + (v % 10);
+                       v /= 10;
+               }
                /* make up a name */
-               yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
-                             obj->objectId);
+               yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
+               yaffs_strcat(locName,x);
                yaffs_strncpy(name, locName, buffSize - 1);
 
        }
@@ -7213,6 +7241,8 @@ int yaffs_GutsInitialise(yaffs_Device * dev)
        dev->blockOffset = 0;
        dev->chunkOffset = 0;
        dev->nFreeChunks = 0;
+       
+       dev->gcBlock = -1;
 
        if (dev->startBlock == 0) {
                dev->internalStartBlock = dev->startBlock + 1;
@@ -7459,6 +7489,8 @@ int yaffs_GutsInitialise(yaffs_Device * dev)
                }else
                        if(!yaffs_Scan(dev))
                                init_failed = 1;
+
+               yaffs_StripDeletedObjects(dev);
        }
                
        if(init_failed){