Clear shadowing during garbage collection since its work is done
[yaffs2.git] / yaffs_guts.c
index d89b87af44eced13f0f7bd0a584581cc579f2914..9f6ba510e635aed3168a865923868b519bc51482 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 const char *yaffs_guts_c_version =
-    "$Id: yaffs_guts.c,v 1.82 2009-03-09 04:24:17 charles Exp $";
+    "$Id: yaffs_guts.c,v 1.97 2009-12-06 22:53:10 charles Exp $";
 
 #include "yportenv.h"
 
@@ -49,6 +49,7 @@ static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
                                const yaffs_ExtendedTags *tags);
 
 /* Other local prototypes */
+static void yaffs_UpdateParent(yaffs_Object *obj);
 static int yaffs_UnlinkObject(yaffs_Object *obj);
 static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
 
@@ -112,7 +113,6 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
                                        yaffs_FileStructure *fStruct,
                                        __u32 chunkId);
 
-
 /* Function to calculate chunk and offset */
 
 static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
@@ -759,7 +759,7 @@ static void yaffs_VerifyObject(yaffs_Object *obj)
        chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
 
        chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
-       chunkIdOk = chunkInRange || obj->hdrChunk == 0;
+       chunkIdOk = chunkInRange || (obj->hdrChunk == 0);
        chunkValid = chunkInRange &&
                        yaffs_CheckChunkBit(dev,
                                        obj->hdrChunk / dev->nChunksPerBlock,
@@ -1323,7 +1323,7 @@ static void yaffs_InitialiseTnodes(yaffs_Device *dev)
 }
 
 
-void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
+void yaffs_LoadLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
                unsigned val)
 {
        __u32 *map = (__u32 *)tn;
@@ -1481,13 +1481,13 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
                        if (tn) {
                                tn->internal[0] = fStruct->top;
                                fStruct->top = tn;
+                               fStruct->topLevel++;
                        } else {
                                T(YAFFS_TRACE_ERROR,
-                                 (TSTR("yaffs: no more tnodes" TENDSTR)));
+                                       (TSTR("yaffs: no more tnodes" TENDSTR)));
+                               return NULL;
                        }
                }
-
-               fStruct->topLevel = requiredTallness;
        }
 
        /* Traverse down to level 0, adding anything we need */
@@ -1506,6 +1506,8 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
                        if ((l > 1) && !tn->internal[x]) {
                                /* Add missing non-level-zero tnode */
                                tn->internal[x] = yaffs_GetTnode(dev);
+                               if(!tn->internal[x])
+                                       return NULL;
 
                        } else if (l == 1) {
                                /* Looking from level 1 at level 0 */
@@ -1518,6 +1520,8 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
                                } else if (!tn->internal[x]) {
                                        /* Don't have one, none passed in */
                                        tn->internal[x] = yaffs_GetTnode(dev);
+                                       if(!tn->internal[x])
+                                               return NULL;
                                }
                        }
 
@@ -1544,11 +1548,16 @@ static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
        for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
                if (yaffs_CheckChunkBit(dev, theChunk / dev->nChunksPerBlock,
                                theChunk % dev->nChunksPerBlock)) {
-                       yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
-                                                       tags);
-                       if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
-                               /* found it; */
+                       
+                       if(dev->chunkGroupSize == 1)
                                return theChunk;
+                       else {
+                               yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
+                                                               tags);
+                               if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
+                                       /* found it; */
+                                       return theChunk;
+                               }
                        }
                }
                theChunk++;
@@ -1636,7 +1645,7 @@ static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
 
                                        }
 
-                                       yaffs_PutLevel0Tnode(dev, tn, i, 0);
+                                       yaffs_LoadLevel0Tnode(dev, tn, i, 0);
                                }
 
                        }
@@ -1713,7 +1722,7 @@ static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
                                         * a block.
                                         */
                                        yaffs_SoftDeleteChunk(dev, theChunk);
-                                       yaffs_PutLevel0Tnode(dev, tn, i, 0);
+                                       yaffs_LoadLevel0Tnode(dev, tn, i, 0);
                                }
 
                        }
@@ -1855,10 +1864,14 @@ static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
        list = YMALLOC(sizeof(yaffs_ObjectList));
 
        if (!newObjects || !list) {
-               if (newObjects)
+               if (newObjects){
                        YFREE(newObjects);
-               if (list)
+                       newObjects = NULL;
+               }
+               if (list){
                        YFREE(list);
+                       list = NULL;
+               }
                T(YAFFS_TRACE_ALLOCATE,
                  (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
                return YAFFS_FAIL;
@@ -2002,6 +2015,7 @@ static void yaffs_FreeObject(yaffs_Object *tn)
 
 #ifdef VALGRIND_TEST
        YFREE(tn);
+       tn = NULL;
 #else
        /* Link into the free list. */
        tn->siblings.next = (struct ylist_head *)(dev->freeObjects);
@@ -2164,18 +2178,20 @@ yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
        if (number < 0)
                number = yaffs_CreateNewObjectNumber(dev);
 
-       theObject = yaffs_AllocateEmptyObject(dev);
-       if (!theObject)
-               return NULL;
-
        if (type == YAFFS_OBJECT_TYPE_FILE) {
                tn = yaffs_GetTnode(dev);
-               if (!tn) {
-                       yaffs_FreeObject(theObject);
+               if (!tn)
                        return NULL;
-               }
        }
 
+       theObject = yaffs_AllocateEmptyObject(dev);
+       if (!theObject){
+               if(tn)
+                       yaffs_FreeTnode(dev,tn);
+               return NULL;
+       }
+
+
        if (theObject) {
                theObject->fake = 0;
                theObject->renameAllowed = 1;
@@ -2242,11 +2258,12 @@ static YCHAR *yaffs_CloneString(const YCHAR *str)
 {
        YCHAR *newStr = NULL;
 
-       if (str && *str) {
-               newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
-               if (newStr)
-                       yaffs_strcpy(newStr, str);
-       }
+       if (!str)
+               str = _Y("");
+
+       newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
+       if (newStr)
+               yaffs_strcpy(newStr, str);
 
        return newStr;
 
@@ -2255,7 +2272,7 @@ static YCHAR *yaffs_CloneString(const YCHAR *str)
 /*
  * Mknod (create) a new object.
  * equivalentObject only has meaning for a hard link;
- * aliasString only has meaning for a sumlink.
+ * aliasString only has meaning for a symlink.
  * rdev only has meaning for devices (a subset of special objects)
  */
 
@@ -2277,21 +2294,23 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
        if (yaffs_FindObjectByName(parent, name))
                return NULL;
 
-       in = yaffs_CreateNewObject(dev, -1, type);
-
-       if (!in)
-               return YAFFS_FAIL;
-
        if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
                str = yaffs_CloneString(aliasString);
-               if (!str) {
-                       yaffs_FreeObject(in);
+               if (!str)
                        return NULL;
-               }
+       }
+
+       in = yaffs_CreateNewObject(dev, -1, type);
+
+       if (!in){
+               if(str)
+                       YFREE(str);
+               return NULL;
        }
 
 
 
+
        if (in) {
                in->hdrChunk = 0;
                in->valid = 1;
@@ -2345,6 +2364,7 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
                        in = NULL;
                }
 
+               yaffs_UpdateParent(parent);
        }
 
        return in;
@@ -2459,6 +2479,8 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
        yaffs_Object *obj = NULL;
        yaffs_Object *existingTarget = NULL;
        int force = 0;
+       int result;
+       yaffs_Device *dev;
 
 
        if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
@@ -2466,6 +2488,8 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
        if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
                YBUG();
 
+       dev = oldDir->myDev;
+
 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
        /* Special case for case insemsitive systems (eg. WinCE).
         * While look-up is case insensitive, the name isn't.
@@ -2475,7 +2499,7 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
                force = 1;
 #endif
 
-       else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
+       if(yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
                /* ENAMETOOLONG */
                return YAFFS_FAIL;
 
@@ -2493,14 +2517,26 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
                        return YAFFS_FAIL;      /* EEXIST or ENOTEMPTY */
                } else if (existingTarget && existingTarget != obj) {
                        /* Nuke the target first, using shadowing,
-                        * but only if it isn't the same object
+                        * but only if it isn't the same object.
+                        *
+                        * Note we must disable gc otherwise it can mess up the shadowing.
+                        *
                         */
+                       dev->isDoingGC=1;
                        yaffs_ChangeObjectName(obj, newDir, newName, force,
                                                existingTarget->objectId);
+                       existingTarget->isShadowed = 1;
                        yaffs_UnlinkObject(existingTarget);
+                       dev->isDoingGC=0;
                }
 
-               return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
+               result = yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
+
+               yaffs_UpdateParent(oldDir);
+               if(newDir != oldDir)
+                       yaffs_UpdateParent(newDir);
+               
+               return result;
        }
        return YAFFS_FAIL;
 }
@@ -2964,7 +3000,6 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
 
        isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
 
-       bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
 
        T(YAFFS_TRACE_TRACING,
                        (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
@@ -2975,12 +3010,16 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
 
        /*yaffs_VerifyFreeChunks(dev); */
 
+       if(bi->blockState == YAFFS_BLOCK_STATE_FULL)
+               bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
+       
        bi->hasShrinkHeader = 0;        /* clear the flag so that the block can erase */
 
        /* Take off the number of soft deleted entries because
         * they're going to get really deleted during GC.
         */
-       dev->nFreeChunks -= bi->softDeletions;
+       if(dev->gcChunk == 0) /* first time through for this block */
+               dev->nFreeChunks -= bi->softDeletions;
 
        dev->isDoingGC = 1;
 
@@ -3096,6 +3135,7 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
                                        if (tags.chunkId == 0) {
                                                /* It is an object Id,
                                                 * We need to nuke the shrinkheader flags first
+                                                * Also need to clean up shadowing.
                                                 * We no longer want the shrinkHeader flag since its work is done
                                                 * and if it is left in place it will mess up scanning.
                                                 */
@@ -3104,6 +3144,9 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
                                                oh = (yaffs_ObjectHeader *)buffer;
                                                oh->isShrink = 0;
                                                tags.extraIsShrinkHeader = 0;
+                                               oh->shadowsObject = 0;
+                                               oh->inbandShadowsObject = 0;
+                                               tags.extraShadows = 0;
 
                                                yaffs_VerifyObjectHeader(object, oh, &tags, 1);
                                        }
@@ -3328,7 +3371,7 @@ static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
 
                /* Delete the entry in the filestructure (if found) */
                if (retVal != -1)
-                       yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0);
+                       yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, 0);
        }
 
        return retVal;
@@ -3398,6 +3441,8 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
        /* NB inScan is zero unless scanning.
         * For forward scanning, inScan is > 0;
         * for backward scanning inScan is < 0
+        *
+        * chunkInNAND = 0 is a dummy insert to make sure the tnodes are there.
         */
 
        yaffs_Tnode *tn;
@@ -3429,6 +3474,11 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
                                        NULL);
        if (!tn)
                return YAFFS_FAIL;
+       
+       if(!chunkInNAND)
+               /* Dummy insert, bail now */
+               return YAFFS_OK;
+               
 
        existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
 
@@ -3510,7 +3560,7 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
        if (existingChunk == 0)
                in->nDataChunks++;
 
-       yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
+       yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
 
        return YAFFS_OK;
 }
@@ -3624,7 +3674,7 @@ static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
        newTags.chunkId = chunkInInode;
        newTags.objectId = in->objectId;
        newTags.serialNumber =
-           (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
+           (prevChunkId > 0) ? prevTags.serialNumber + 1 : 1;
        newTags.byteCount = nBytes;
 
        if (nBytes < 1 || nBytes > dev->totalBytesPerChunk) {
@@ -3632,15 +3682,23 @@ static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
                (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
                YBUG();
        }
-
+       
+       /*
+        * If there isn't already a chunk there then do a dummy
+        * insert to make sue we have the desired tnode structure.
+        */
+       if(prevChunkId < 1 &&
+               yaffs_PutChunkIntoFile(in, chunkInInode, 0, 0) != YAFFS_OK)
+               return -1;
+               
        newChunkId =
            yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
                                              useReserve);
 
-       if (newChunkId >= 0) {
+       if (newChunkId > 0) {
                yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
 
-               if (prevChunkId >= 0)
+               if (prevChunkId > 0)
                        yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
 
                yaffs_CheckFileSanity(in);
@@ -3667,6 +3725,7 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
        int newChunkId;
        yaffs_ExtendedTags newTags;
        yaffs_ExtendedTags oldTags;
+       YCHAR *alias = NULL;
 
        __u8 *buffer = NULL;
        YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
@@ -3726,7 +3785,7 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
                if (name && *name) {
                        memset(oh->name, 0, sizeof(oh->name));
                        yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
-               } else if (prevChunkId >= 0)
+               } else if (prevChunkId > 0)
                        memcpy(oh->name, oldName, sizeof(oh->name));
                else
                        memset(oh->name, 0, sizeof(oh->name));
@@ -3755,8 +3814,11 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
                        /* Do nothing */
                        break;
                case YAFFS_OBJECT_TYPE_SYMLINK:
+                       alias = in->variant.symLinkVariant.alias;
+                       if(!alias)
+                               alias = _Y("no alias");
                        yaffs_strncpy(oh->alias,
-                                     in->variant.symLinkVariant.alias,
+                                       alias,
                                      YAFFS_MAX_ALIAS_LENGTH);
                        oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
                        break;
@@ -3784,13 +3846,13 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
                /* Create new chunk in NAND */
                newChunkId =
                    yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
-                                                     (prevChunkId >= 0) ? 1 : 0);
+                                                     (prevChunkId > 0) ? 1 : 0);
 
                if (newChunkId >= 0) {
 
                        in->hdrChunk = newChunkId;
 
-                       if (prevChunkId >= 0) {
+                       if (prevChunkId > 0) {
                                yaffs_DeleteChunk(dev, prevChunkId, 1,
                                                  __LINE__);
                        }
@@ -5018,11 +5080,13 @@ int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
        }
 
 
-       /* Write a new object header.
+       /* Write a new object header to reflect the resize.
         * show we've shrunk the file, if need be
-        * Do this only if the file is not in the deleted directories.
+        * Do this only if the file is not in the deleted directories
+        * and is not shadowed.
         */
        if (in->parent &&
+           !in->isShadowed &&
            in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
            in->parent->objectId != YAFFS_OBJECTID_DELETED)
                yaffs_UpdateObjectHeader(in, NULL, 0,
@@ -5033,13 +5097,17 @@ int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
 
 loff_t yaffs_GetFileSize(yaffs_Object *obj)
 {
+       YCHAR *alias = NULL;
        obj = yaffs_GetEquivalentObject(obj);
 
        switch (obj->variantType) {
        case YAFFS_OBJECT_TYPE_FILE:
                return obj->variant.fileVariant.fileSize;
        case YAFFS_OBJECT_TYPE_SYMLINK:
-               return yaffs_strlen(obj->variant.symLinkVariant.alias);
+               alias = obj->variant.symLinkVariant.alias;
+               if(!alias)
+                       return 0;
+               return yaffs_strlen(alias);
        default:
                return 0;
        }
@@ -5047,23 +5115,27 @@ loff_t yaffs_GetFileSize(yaffs_Object *obj)
 
 
 
-int yaffs_FlushFile(yaffs_Object *in, int updateTime)
+int yaffs_FlushFile(yaffs_Object *in, int updateTime, int dataSync)
 {
        int retVal;
        if (in->dirty) {
                yaffs_FlushFilesChunkCache(in);
-               if (updateTime) {
+               if(dataSync) /* Only sync data */
+                       retVal=YAFFS_OK;
+               else {
+                       if (updateTime) {
 #ifdef CONFIG_YAFFS_WINCE
-                       yfsd_WinFileTimeNow(in->win_mtime);
+                               yfsd_WinFileTimeNow(in->win_mtime);
 #else
 
-                       in->yst_mtime = Y_CURRENT_TIME;
+                               in->yst_mtime = Y_CURRENT_TIME;
 
 #endif
-               }
+                       }
 
-               retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
-                       0) ? YAFFS_OK : YAFFS_FAIL;
+                       retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
+                               0) ? YAFFS_OK : YAFFS_FAIL;
+               }
        } else {
                retVal = YAFFS_OK;
        }
@@ -5164,19 +5236,26 @@ int yaffs_DeleteFile(yaffs_Object *in)
        }
 }
 
-static int yaffs_DeleteDirectory(yaffs_Object *in)
+static int yaffs_IsNonEmptyDirectory(yaffs_Object *obj)
 {
-       /* First check that the directory is empty. */
-       if (ylist_empty(&in->variant.directoryVariant.children))
-               return yaffs_DoGenericObjectDeletion(in);
+       return (obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) &&
+               !(ylist_empty(&obj->variant.directoryVariant.children));
+}
 
-       return YAFFS_FAIL;
+static int yaffs_DeleteDirectory(yaffs_Object *obj)
+{
+       /* First check that the directory is empty. */
+       if (yaffs_IsNonEmptyDirectory(obj))
+               return YAFFS_FAIL;
 
+       return yaffs_DoGenericObjectDeletion(obj);
 }
 
 static int yaffs_DeleteSymLink(yaffs_Object *in)
 {
-       YFREE(in->variant.symLinkVariant.alias);
+       if(in->variant.symLinkVariant.alias)
+               YFREE(in->variant.symLinkVariant.alias);
+       in->variant.symLinkVariant.alias=NULL;
 
        return yaffs_DoGenericObjectDeletion(in);
 }
@@ -5230,6 +5309,9 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj)
                immediateDeletion = 1;
 #endif
 
+       if(obj)
+               yaffs_UpdateParent(obj->parent);
+
        if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
                return yaffs_DeleteHardLink(obj);
        } else if (!ylist_empty(&obj->hardLinks)) {
@@ -5284,7 +5366,9 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj)
                default:
                        return YAFFS_FAIL;
                }
-       } else
+       } else if(yaffs_IsNonEmptyDirectory(obj))
+               return YAFFS_FAIL;
+       else
                return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
                                           _Y("unlinked"), 0, 0);
 }
@@ -5323,7 +5407,8 @@ static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
                /* Handle YAFFS2 case (backward scanning)
                 * If the shadowed object exists then ignore.
                 */
-               if (yaffs_FindObjectByNumber(dev, objId))
+               obj = yaffs_FindObjectByNumber(dev, objId);
+               if(obj)
                        return;
        }
 
@@ -5335,6 +5420,7 @@ static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
                                             YAFFS_OBJECT_TYPE_FILE);
        if (!obj)
                return;
+       obj->isShadowed = 1;
        yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
        obj->variant.fileVariant.shrinkSize = 0;
        obj->valid = 1;         /* So that we don't read any other info for this file */
@@ -5427,6 +5513,125 @@ static void yaffs_StripDeletedObjects(yaffs_Device *dev)
 
 }
 
+/*
+ * This code iterates through all the objects making sure that they are rooted.
+ * Any unrooted objects are re-rooted in lost+found.
+ * An object needs to be in one of:
+ * - Directly under deleted, unlinked
+ * - Directly or indirectly under root.
+ *
+ * Note:
+ *  This code assumes that we don't ever change the current relationships between
+ *  directories:
+ *   rootDir->parent == unlinkedDir->parent == deletedDir->parent == NULL
+ *   lostNfound->parent == rootDir
+ *
+ * This fixes the problem where directories might have inadvertently been deleted
+ * leaving the object "hanging" without being rooted in the directory tree.
+ */
+static int yaffs_HasNULLParent(yaffs_Device *dev, yaffs_Object *obj)
+{
+       return (obj == dev->deletedDir ||
+               obj == dev->unlinkedDir||
+               obj == dev->rootDir);
+}
+
+static void yaffs_FixHangingObjects(yaffs_Device *dev)
+{
+       yaffs_Object *obj;
+       yaffs_Object *parent;
+       int i;
+       struct ylist_head *lh;
+       struct ylist_head *n;
+       int depthLimit;
+       int hanging;
+
+
+       /* Iterate through the objects in each hash entry,
+        * looking at each object.
+        * Make sure it is rooted.
+        */
+
+       for (i = 0; i <  YAFFS_NOBJECT_BUCKETS; i++) {
+               ylist_for_each_safe(lh, n, &dev->objectBucket[i].list) {
+                       if (lh) {
+                               obj = ylist_entry(lh, yaffs_Object, hashLink);
+                               parent= obj->parent;
+                               
+                               if(yaffs_HasNULLParent(dev,obj)){
+                                       /* These directories are not hanging */
+                                       hanging = 0;
+                               }
+                               else if(!parent || parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
+                                       hanging = 1;
+                               else if(yaffs_HasNULLParent(dev,parent))
+                                       hanging = 0;
+                               else {
+                                       /*
+                                        * Need to follow the parent chain to see if it is hanging.
+                                        */
+                                       hanging = 0;
+                                       depthLimit=100;
+
+                                       while(parent != dev->rootDir &&
+                                               parent->parent &&
+                                               parent->parent->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
+                                               depthLimit > 0){
+                                               parent = parent->parent;
+                                               depthLimit--;
+                                       }
+                                       if(parent != dev->rootDir)
+                                               hanging = 1;
+                               }
+                               if(hanging){
+                                       T(YAFFS_TRACE_SCAN,
+                                         (TSTR("Hanging object %d moved to lost and found" TENDSTR),
+                                               obj->objectId));
+                                       yaffs_AddObjectToDirectory(dev->lostNFoundDir,obj);
+                               }
+                       }
+               }
+       }
+}
+
+
+/*
+ * Delete directory contents for cleaning up lost and found.
+ */
+static void yaffs_DeleteDirectoryContents(yaffs_Object *dir)
+{
+       yaffs_Object *obj;
+       struct ylist_head *lh;
+       struct ylist_head *n;
+
+       if(dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
+               YBUG();
+       
+       ylist_for_each_safe(lh, n, &dir->variant.directoryVariant.children) {
+               if (lh) {
+                       obj = ylist_entry(lh, yaffs_Object, siblings);
+                       if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
+                               yaffs_DeleteDirectoryContents(obj);
+
+                       T(YAFFS_TRACE_SCAN,
+                               (TSTR("Deleting lost_found object %d" TENDSTR),
+                               obj->objectId));
+
+                       /* Need to use UnlinkObject since Delete would not handle
+                        * hardlinked objects correctly.
+                        */
+                       yaffs_UnlinkObject(obj); 
+               }
+       }
+                       
+}
+
+static void yaffs_EmptyLostAndFound(yaffs_Device *dev)
+{
+       yaffs_DeleteDirectoryContents(dev->lostNFoundDir);
+}
+
 static int yaffs_Scan(yaffs_Device *dev)
 {
        yaffs_ExtendedTags tags;
@@ -6252,9 +6457,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
                                }
 
                                if (!in ||
-#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
-                                   !in->valid ||
-#endif
+                                   (!in->valid && dev->disableLazyLoad) ||
                                    tags.extraShadows ||
                                    (!in->valid &&
                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
@@ -6666,6 +6869,26 @@ static void yaffs_VerifyDirectory(yaffs_Object *directory)
        }
 }
 
+/*
+ *yaffs_UpdateParent() handles fixing a directories mtime and ctime when a new
+ * link (ie. name) is created or deleted in the directory.
+ *
+ * ie.
+ *   create dir/a : update dir's mtime/ctime
+ *   rm dir/a:   update dir's mtime/ctime
+ *   modify dir/a: don't update dir's mtimme/ctime
+ */
+static void yaffs_UpdateParent(yaffs_Object *obj)
+{
+       if(!obj)
+               return;
+
+       obj->dirty = 1;
+       obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME;
+
+       yaffs_UpdateObjectHeader(obj,NULL,0,0,0);
+}
 
 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
 {
@@ -6683,11 +6906,10 @@ static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
 
        ylist_del_init(&obj->siblings);
        obj->parent = NULL;
-
+       
        yaffs_VerifyDirectory(parent);
 }
 
-
 static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
                                        yaffs_Object *obj)
 {
@@ -6781,7 +7003,7 @@ yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
                                 * Do a real check
                                 */
                                yaffs_GetObjectName(l, buffer,
-                                                   YAFFS_MAX_NAME_LENGTH);
+                                                   YAFFS_MAX_NAME_LENGTH + 1);
                                if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
                                        return l;
                        }
@@ -6899,9 +7121,11 @@ int yaffs_GetObjectFileLength(yaffs_Object *obj)
 
        if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
                return obj->variant.fileVariant.fileSize;
-       if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
+       if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
+               if(!obj->variant.symLinkVariant.alias)
+                       return 0;
                return yaffs_strlen(obj->variant.symLinkVariant.alias);
-       else {
+       else {
                /* Only a directory should drop through to here */
                return obj->myDev->nDataBytesPerChunk;
        }
@@ -7030,7 +7254,7 @@ int yaffs_DumpObject(yaffs_Object *obj)
 {
        YCHAR name[257];
 
-       yaffs_GetObjectName(obj, name, 256);
+       yaffs_GetObjectName(obj, name, YAFFS_MAX_NAME_LENGTH + 1);
 
        T(YAFFS_TRACE_ALWAYS,
          (TSTR
@@ -7371,6 +7595,9 @@ int yaffs_GutsInitialise(yaffs_Device *dev)
                                init_failed = 1;
 
                yaffs_StripDeletedObjects(dev);
+               yaffs_FixHangingObjects(dev);
+               if(dev->emptyLostAndFound)
+                       yaffs_EmptyLostAndFound(dev);
        }
 
        if (init_failed) {
@@ -7394,6 +7621,9 @@ int yaffs_GutsInitialise(yaffs_Device *dev)
        yaffs_VerifyFreeChunks(dev);
        yaffs_VerifyBlocks(dev);
 
+       /* Clean up any aborted checkpoint data */
+       if(!dev->isCheckpointed && dev->blocksInCheckpoint > 0)
+               yaffs_InvalidateCheckpoint(dev);
 
        T(YAFFS_TRACE_TRACING,
          (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));