New partial block handing to improve robustness
[yaffs2.git] / yaffs_guts.c
index 4359db29f02081d189bed3c7d46d629763459461..4af4c4094a4d348446870e070d79ef62c09d3f95 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 const char *yaffs_guts_c_version =
-    "$Id: yaffs_guts.c,v 1.81 2009-03-06 17:20:51 wookey Exp $";
+    "$Id: yaffs_guts.c,v 1.99 2009-12-14 22:03:05 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);
 
@@ -84,8 +85,6 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj);
 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
                        int chunkInObject);
 
-loff_t yaffs_GetFileSize(yaffs_Object *obj);
-
 static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
                                yaffs_BlockInfo **blockUsedPtr);
 
@@ -114,6 +113,11 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
                                        yaffs_FileStructure *fStruct,
                                        __u32 chunkId);
 
+static void yaffs_SkipRestOfBlock(yaffs_Device *dev);
+static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
+                                       int chunkInNAND,
+                                       const __u8 *data,
+                                       yaffs_ExtendedTags *tags);
 
 /* Function to calculate chunk and offset */
 
@@ -236,9 +240,9 @@ __u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
        T(YAFFS_TRACE_BUFFERS,
          (TSTR("Out of temp buffers at line %d, other held by lines:"),
           lineNo));
-       for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
+       for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
                T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
-       }
+
        T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
 
        /*
@@ -761,7 +765,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,
@@ -920,6 +924,29 @@ static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
 
 }
 
+
+static int yaffs_VerifyChunkWritten(yaffs_Device *dev,
+                                       int chunkInNAND,
+                                       const __u8 *data,
+                                       yaffs_ExtendedTags *tags)
+{
+       int retval = YAFFS_OK;
+       yaffs_ExtendedTags tempTags;
+       __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
+       int result;
+       
+       result = yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,&tempTags);
+       if(memcmp(buffer,data,dev->nDataBytesPerChunk) ||
+               tempTags.objectId != tags->objectId ||
+               tempTags.chunkId  != tags->chunkId ||
+               tempTags.byteCount != tags->byteCount)
+               retval = YAFFS_FAIL;
+
+       yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
+
+       return retval;
+}
+
 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
                                        const __u8 *data,
                                        yaffs_ExtendedTags *tags,
@@ -959,12 +986,11 @@ static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
                 * chunk due to power loss.  This checking policy should
                 * catch that case with very few checks and thus save a
                 * lot of checks that are most likely not needed.
+                *
+                * Mods to the above
+                * If an erase check fails or the write fails we skip the 
+                * rest of the block.
                 */
-               if (bi->gcPrioritise) {
-                       yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
-                       /* try another chunk */
-                       continue;
-               }
 
                /* let's give it a try */
                attempts++;
@@ -976,23 +1002,33 @@ static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
                        erasedOk = yaffs_CheckChunkErased(dev, chunk);
                        if (erasedOk != YAFFS_OK) {
                                T(YAFFS_TRACE_ERROR,
-                               (TSTR ("**>> yaffs chunk %d was not erased"
+                               (TSTR("**>> yaffs chunk %d was not erased"
                                TENDSTR), chunk));
 
-                               /* try another chunk */
+                               /* If not erased, delete this one,
+                                * skip rest of block and
+                                * try another chunk */
+                                yaffs_DeleteChunk(dev,chunk,1,__LINE__);
+                                yaffs_SkipRestOfBlock(dev);
                                continue;
                        }
-                       bi->skipErasedCheck = 1;
                }
 
                writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
                                data, tags);
+
+               if(!bi->skipErasedCheck)
+                       writeOk = yaffs_VerifyChunkWritten(dev, chunk, data, tags);
+
                if (writeOk != YAFFS_OK) {
+                       /* Clean up aborted write, skip to next block and
+                        * try another chunk */
                        yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
-                       /* try another chunk */
                        continue;
                }
 
+               bi->skipErasedCheck = 1;
+
                /* Copy the data into the robustification buffer */
                yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
 
@@ -1102,6 +1138,7 @@ static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
 
        /* Delete the chunk */
        yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
+       yaffs_SkipRestOfBlock(dev);
 }
 
 
@@ -1131,12 +1168,11 @@ static __u16 yaffs_CalcNameSum(const YCHAR *name)
 static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
 {
 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
-       memset(obj->shortName, 0, sizeof (YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
-       if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
+       memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
+       if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
                yaffs_strcpy(obj->shortName, name);
-       } else {
+       else
                obj->shortName[0] = _Y('\0');
-       }
 #endif
        obj->sum = yaffs_CalcNameSum(name);
 }
@@ -1245,9 +1281,8 @@ static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev)
        yaffs_Tnode *tn = NULL;
 
        /* If there are none left make more */
-       if (!dev->freeTnodes) {
+       if (!dev->freeTnodes)
                yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
-       }
 
        if (dev->freeTnodes) {
                tn = dev->freeTnodes;
@@ -1327,7 +1362,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;
@@ -1404,13 +1439,11 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
        int level = fStruct->topLevel;
 
        /* Check sane level and chunk Id */
-       if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
+       if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
                return NULL;
-       }
 
-       if (chunkId > YAFFS_MAX_CHUNK_ID) {
+       if (chunkId > YAFFS_MAX_CHUNK_ID)
                return NULL;
-       }
 
        /* First check we're tall enough (ie enough topLevel) */
 
@@ -1421,10 +1454,8 @@ static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
                requiredTallness++;
        }
 
-       if (requiredTallness > fStruct->topLevel) {
-               /* Not tall enough, so we can't find it, return NULL. */
-               return NULL;
-       }
+       if (requiredTallness > fStruct->topLevel)
+               return NULL; /* Not tall enough, so we can't find it */
 
        /* Traverse down to level 0 */
        while (level > 0 && tn) {
@@ -1464,13 +1495,11 @@ static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
 
 
        /* Check sane level and page Id */
-       if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
+       if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
                return NULL;
-       }
 
-       if (chunkId > YAFFS_MAX_CHUNK_ID) {
+       if (chunkId > YAFFS_MAX_CHUNK_ID)
                return NULL;
-       }
 
        /* First check we're tall enough (ie enough topLevel) */
 
@@ -1491,13 +1520,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 */
@@ -1516,6 +1545,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 */
@@ -1528,6 +1559,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;
                                }
                        }
 
@@ -1554,11 +1587,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++;
@@ -1640,14 +1678,13 @@ static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
                                                in->nDataChunks--;
                                                if (limit) {
                                                        *limit = *limit - 1;
-                                                       if (*limit <= 0) {
+                                                       if (*limit <= 0)
                                                                hitLimit = 1;
-                                                       }
                                                }
 
                                        }
 
-                                       yaffs_PutLevel0Tnode(dev, tn, i, 0);
+                                       yaffs_LoadLevel0Tnode(dev, tn, i, 0);
                                }
 
                        }
@@ -1724,7 +1761,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);
                                }
 
                        }
@@ -1789,9 +1826,8 @@ static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
                                                      (i == 0) ? del0 : 1);
                        }
 
-                       if (tn->internal[i]) {
+                       if (tn->internal[i])
                                hasData++;
-                       }
                }
 
                if (hasData == 0 && del0) {
@@ -1831,9 +1867,8 @@ static int yaffs_PruneFileStructure(yaffs_Device *dev,
 
                        hasData = 0;
                        for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
-                               if (tn->internal[i]) {
+                               if (tn->internal[i])
                                        hasData++;
-                               }
                        }
 
                        if (!hasData) {
@@ -1868,10 +1903,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;
@@ -1907,9 +1946,8 @@ static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
        tn = YMALLOC(sizeof(yaffs_Object));
 #else
        /* If there are none left make more */
-       if (!dev->freeObjects) {
+       if (!dev->freeObjects)
                yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
-       }
 
        if (dev->freeObjects) {
                tn = dev->freeObjects;
@@ -1942,9 +1980,8 @@ static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
                 * NB Can't put root or lostNFound in lostNFound so
                 * check if lostNFound exists first
                 */
-               if (dev->lostNFoundDir) {
+               if (dev->lostNFoundDir)
                        yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
-               }
 
                tn->beingCreated = 0;
        }
@@ -1993,11 +2030,11 @@ static void yaffs_FreeObject(yaffs_Object *tn)
 {
        yaffs_Device *dev = tn->myDev;
 
-#ifdef  __KERNEL__
-       T(YAFFS_TRACE_OS,(TSTR("FreeObject %p inode %p"TENDSTR),tn,tn->myInode));
+#ifdef __KERNEL__
+       T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode));
 #endif
-       
-       if(tn->parent)
+
+       if (tn->parent)
                YBUG();
        if (!ylist_empty(&tn->siblings))
                YBUG();
@@ -2017,6 +2054,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);
@@ -2030,9 +2068,8 @@ static void yaffs_FreeObject(yaffs_Object *tn)
 
 void yaffs_HandleDeferedFree(yaffs_Object *obj)
 {
-       if (obj->deferedFree) {
+       if (obj->deferedFree)
                yaffs_FreeObject(obj);
-       }
 }
 
 #endif
@@ -2177,22 +2214,23 @@ yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
        yaffs_Object *theObject;
        yaffs_Tnode *tn = NULL;
 
-       if (number < 0) {
+       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;
@@ -2244,13 +2282,11 @@ static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
 {
        yaffs_Object *theObject = NULL;
 
-       if (number > 0) {
+       if (number > 0)
                theObject = yaffs_FindObjectByNumber(dev, number);
-       }
 
-       if (!theObject) {
+       if (!theObject)
                theObject = yaffs_CreateNewObject(dev, number, type);
-       }
 
        return theObject;
 
@@ -2260,13 +2296,17 @@ static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
 static YCHAR *yaffs_CloneString(const YCHAR *str)
 {
        YCHAR *newStr = NULL;
+       int len;
 
-       if (str && *str) {
-               newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
-               if (newStr)
-                       yaffs_strcpy(newStr, str);
-       }
+       if (!str)
+               str = _Y("");
 
+       len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
+       newStr = YMALLOC((len + 1) * sizeof(YCHAR));
+       if (newStr){
+               yaffs_strncpy(newStr, str,len);
+               newStr[len] = 0;
+       }
        return newStr;
 
 }
@@ -2274,7 +2314,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)
  */
 
@@ -2293,25 +2333,26 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
        yaffs_Device *dev = parent->myDev;
 
        /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
-       if (yaffs_FindObjectByName(parent, name)) {
+       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;
@@ -2365,6 +2406,7 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
                        in = NULL;
                }
 
+               yaffs_UpdateParent(parent);
        }
 
        return in;
@@ -2424,9 +2466,8 @@ static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
 
        yaffs_Object *existingTarget;
 
-       if (newDir == NULL) {
+       if (newDir == NULL)
                newDir = obj->parent;   /* use the old directory */
-       }
 
        if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
                T(YAFFS_TRACE_ALWAYS,
@@ -2437,12 +2478,11 @@ static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
        }
 
        /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
-       if (obj->myDev->isYaffs2) {
+       if (obj->myDev->isYaffs2)
                unlinkOp = (newDir == obj->myDev->unlinkedDir);
-       } else {
+       else
                unlinkOp = (newDir == obj->myDev->unlinkedDir
                            && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
-       }
 
        deleteOp = (newDir == obj->myDev->deletedDir);
 
@@ -2481,6 +2521,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)
@@ -2488,17 +2530,18 @@ 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.
         * Therefore we might want to change x.txt to X.txt
        */
-       if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
+       if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
                force = 1;
-       }
 #endif
 
-       else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
+       if(yaffs_strnlen(newName,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
                /* ENAMETOOLONG */
                return YAFFS_FAIL;
 
@@ -2516,14 +2559,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;
 }
@@ -2671,31 +2726,28 @@ static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
 
        dev->nonAggressiveSkip--;
 
-       if (!aggressive && (dev->nonAggressiveSkip > 0)) {
+       if (!aggressive && (dev->nonAggressiveSkip > 0))
                return -1;
-       }
 
        if (!prioritised)
                pagesInUse =
                        (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
 
-       if (aggressive) {
+       if (aggressive)
                iterations =
                    dev->internalEndBlock - dev->internalStartBlock + 1;
-       else {
+       else {
                iterations =
                    dev->internalEndBlock - dev->internalStartBlock + 1;
                iterations = iterations / 16;
-               if (iterations > 200) {
+               if (iterations > 200)
                        iterations = 200;
-               }
        }
 
        for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
                b++;
-               if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
+               if (b < dev->internalStartBlock || b > dev->internalEndBlock)
                        b = dev->internalStartBlock;
-               }
 
                if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
                        T(YAFFS_TRACE_ERROR,
@@ -2705,13 +2757,6 @@ static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
 
                bi = yaffs_GetBlockInfo(dev, b);
 
-#if 0
-               if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
-                       dirtiest = b;
-                       pagesInUse = 0;
-               } else
-#endif
-
                if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
                        (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
                                yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
@@ -2966,14 +3011,29 @@ static int yaffs_GetErasedChunks(yaffs_Device *dev)
 
        n = dev->nErasedBlocks * dev->nChunksPerBlock;
 
-       if (dev->allocationBlock > 0) {
+       if (dev->allocationBlock > 0)
                n += (dev->nChunksPerBlock - dev->allocationPage);
-       }
 
        return n;
 
 }
 
+/*
+ * yaffs_SkipRestOfBlock() skips over the rest of the allocation block
+ * if we don't want to write to it.
+ */
+static void yaffs_SkipRestOfBlock(yaffs_Device *dev)
+{
+       if(dev->allocationBlock > 0){
+               yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
+               if(bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING){
+                       bi->blockState = YAFFS_BLOCK_STATE_FULL;
+                       dev->allocationBlock = -1;
+               }
+       }
+}
+
+
 static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
                int wholeBlock)
 {
@@ -2998,7 +3058,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),
@@ -3009,12 +3068,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;
 
@@ -3130,6 +3193,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.
                                                 */
@@ -3138,6 +3202,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);
                                        }
@@ -3269,9 +3336,8 @@ static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
 
                if (block > 0) {
                        dev->garbageCollections++;
-                       if (!aggressive) {
+                       if (!aggressive)
                                dev->passiveGarbageCollections++;
-                       }
 
                        T(YAFFS_TRACE_GC,
                          (TSTR
@@ -3362,16 +3428,10 @@ static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
                                           chunkInInode);
 
                /* Delete the entry in the filestructure (if found) */
-               if (retVal != -1) {
-                       yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0);
-               }
-       } else {
-               /*T(("No level 0 found for %d\n", chunkInInode)); */
+               if (retVal != -1)
+                       yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, 0);
        }
 
-       if (retVal == -1) {
-               /* T(("Could not find %d to delete\n",chunkInInode)); */
-       }
        return retVal;
 }
 
@@ -3390,10 +3450,8 @@ static int yaffs_CheckFileSanity(yaffs_Object *in)
        int theChunk;
        int chunkDeleted;
 
-       if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
-               /* T(("Object not a file\n")); */
+       if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
                return YAFFS_FAIL;
-       }
 
        objId = in->objectId;
        fSize = in->variant.fileVariant.fileSize;
@@ -3441,6 +3499,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;
@@ -3470,9 +3530,13 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
                                        &in->variant.fileVariant,
                                        chunkInInode,
                                        NULL);
-       if (!tn) {
+       if (!tn)
                return YAFFS_FAIL;
-       }
+       
+       if(!chunkInNAND)
+               /* Dummy insert, bail now */
+               return YAFFS_OK;
+               
 
        existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
 
@@ -3551,11 +3615,10 @@ static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
 
        }
 
-       if (existingChunk == 0) {
+       if (existingChunk == 0)
                in->nDataChunks++;
-       }
 
-       yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
+       yaffs_LoadLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
 
        return YAFFS_OK;
 }
@@ -3565,10 +3628,10 @@ static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
 {
        int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
 
-       if (chunkInNAND >= 0) {
+       if (chunkInNAND >= 0)
                return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
                                                buffer, NULL);
-       else {
+       else {
                T(YAFFS_TRACE_NANDACCESS,
                  (TSTR("Chunk %d not found zero instead" TENDSTR),
                   chunkInNAND));
@@ -3589,7 +3652,6 @@ void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
        if (chunkId <= 0)
                return;
 
-
        dev->nDeletions++;
        block = chunkId / dev->nChunksPerBlock;
        page = chunkId % dev->nChunksPerBlock;
@@ -3638,8 +3700,6 @@ void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
                        yaffs_BlockBecameDirty(dev, block);
                }
 
-       } else {
-               /* T(("Bad news deleting chunk %d\n",chunkId)); */
        }
 
 }
@@ -3672,27 +3732,33 @@ 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) {
-         T(YAFFS_TRACE_ERROR,
-         (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
-         YBUG();
+               T(YAFFS_TRACE_ERROR,
+               (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);
        }
        return newChunkId;
@@ -3717,6 +3783,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];
@@ -3768,20 +3835,18 @@ int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
                oh->yst_ctime = in->yst_ctime;
                oh->yst_rdev = in->yst_rdev;
 #endif
-               if (in->parent) {
+               if (in->parent)
                        oh->parentObjectId = in->parent->objectId;
-               } else {
+               else
                        oh->parentObjectId = 0;
-               }
 
                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 {
+               else
                        memset(oh->name, 0, sizeof(oh->name));
-               }
 
                oh->isShrink = isShrink;
 
@@ -3807,8 +3872,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;
@@ -3836,13 +3904,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__);
                        }
@@ -4079,9 +4147,9 @@ static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
                if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
                        /* Reset the cache usages */
                        int i;
-                       for (i = 1; i < dev->nShortOpCaches; i++) {
+                       for (i = 1; i < dev->nShortOpCaches; i++)
                                dev->srCache[i].lastUse = 0;
-                       }
+
                        dev->srLastUse = 0;
                }
 
@@ -4089,9 +4157,8 @@ static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
 
                cache->lastUse = dev->srLastUse;
 
-               if (isAWrite) {
+               if (isAWrite)
                        cache->dirty = 1;
-               }
        }
 }
 
@@ -4104,9 +4171,8 @@ static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
        if (object->myDev->nShortOpCaches > 0) {
                yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
 
-               if (cache) {
+               if (cache)
                        cache->object = NULL;
-               }
        }
 }
 
@@ -4121,9 +4187,8 @@ static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
        if (dev->nShortOpCaches > 0) {
                /* Invalidate it. */
                for (i = 0; i < dev->nShortOpCaches; i++) {
-                       if (dev->srCache[i].object == in) {
+                       if (dev->srCache[i].object == in)
                                dev->srCache[i].object = NULL;
-                       }
                }
        }
 }
@@ -4459,9 +4524,8 @@ static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
 
                                        ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
 
-                                       if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
+                                       if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
                                                ok = yaffs_WriteCheckpointTnodes(obj);
-                                       }
                                }
                        }
                }
@@ -4586,10 +4650,8 @@ static int yaffs_WriteCheckpointData(yaffs_Device *dev)
                ok = yaffs_WriteCheckpointValidityMarker(dev, 0);
        }
 
-       if (ok) {
+       if (ok)
                ok = yaffs_WriteCheckpointSum(dev);
-       }
-
 
        if (!yaffs_CheckpointClose(dev))
                ok = 0;
@@ -4731,11 +4793,10 @@ int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
                /* OK now check for the curveball where the start and end are in
                 * the same chunk.
                 */
-               if ((start + n) < dev->nDataBytesPerChunk) {
+               if ((start + n) < dev->nDataBytesPerChunk)
                        nToCopy = n;
-               } else {
+               else
                        nToCopy = dev->nDataBytesPerChunk - start;
-               }
 
                cache = yaffs_FindChunkCache(in, chunk);
 
@@ -4852,9 +4913,8 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
                        else
                                nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
 
-                       if (nBytesRead > dev->nDataBytesPerChunk) {
+                       if (nBytesRead > dev->nDataBytesPerChunk)
                                nBytesRead = dev->nDataBytesPerChunk;
-                       }
 
                        nToWriteBack =
                            (nBytesRead >
@@ -4973,9 +5033,8 @@ int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
 
        /* Update file object */
 
-       if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
+       if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
                in->variant.fileVariant.fileSize = (startOfWrite + nDone);
-       }
 
        in->dirty = 1;
 
@@ -5042,13 +5101,11 @@ int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
 
        yaffs_CheckGarbageCollection(dev);
 
-       if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
+       if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
                return YAFFS_FAIL;
-       }
 
-       if (newSize == oldFileSize) {
+       if (newSize == oldFileSize)
                return YAFFS_OK;
-       }
 
        if (newSize < oldFileSize) {
 
@@ -5081,29 +5138,34 @@ 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) {
+           in->parent->objectId != YAFFS_OBJECTID_DELETED)
                yaffs_UpdateObjectHeader(in, NULL, 0,
                                         (newSize < oldFileSize) ? 1 : 0, 0);
-       }
 
        return YAFFS_OK;
 }
 
 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_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH);
        default:
                return 0;
        }
@@ -5111,23 +5173,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;
        }
@@ -5168,13 +5234,11 @@ static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
        int immediateDeletion = 0;
 
 #ifdef __KERNEL__
-       if (!in->myInode) {
+       if (!in->myInode)
                immediateDeletion = 1;
-       }
 #else
-       if (in->inUse <= 0) {
+       if (in->inUse <= 0)
                immediateDeletion = 1;
-       }
 #endif
 
        if (immediateDeletion) {
@@ -5186,9 +5250,8 @@ static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
                   in->objectId));
                in->deleted = 1;
                in->myDev->nDeletedFiles++;
-               if (1 || in->myDev->isYaffs2) {
+               if (1 || in->myDev->isYaffs2)
                        yaffs_ResizeFile(in, 0);
-               }
                yaffs_SoftDeleteFile(in);
        } else {
                retVal =
@@ -5211,11 +5274,12 @@ int yaffs_DeleteFile(yaffs_Object *in)
                /* Use soft deletion if there is data in the file.
                 * That won't be the case if it has been resized to zero.
                 */
-               if (!in->unlinked) {
+               if (!in->unlinked)
                        retVal = yaffs_UnlinkFileIfNeeded(in);
-               }
+
                if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
-                       in->deleted = deleted = 1;
+                       in->deleted = 1;
+                       deleted = 1;
                        in->myDev->nDeletedFiles++;
                        yaffs_SoftDeleteFile(in);
                }
@@ -5230,20 +5294,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);
 }
@@ -5277,10 +5347,10 @@ int retVal = -1;
                retVal = yaffs_DoGenericObjectDeletion(obj);
                break;
        case YAFFS_OBJECT_TYPE_UNKNOWN:
-               retVal = 0;
+               retVal = 0;
                break;          /* should not happen. */
        }
-       
+
        return retVal;
 }
 
@@ -5290,21 +5360,22 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj)
        int immediateDeletion = 0;
 
 #ifdef __KERNEL__
-       if (!obj->myInode) {
+       if (!obj->myInode)
                immediateDeletion = 1;
-       }
 #else
-       if (obj->inUse <= 0) {
+       if (obj->inUse <= 0)
                immediateDeletion = 1;
-       }
 #endif
 
-        if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
-                return yaffs_DeleteHardLink(obj);
-        } else if (!ylist_empty(&obj->hardLinks)) {
-                /* Curve ball: We're unlinking an object that has a hardlink.
-                 *
-                 * This problem arises because we are not strictly following
+       if(obj)
+               yaffs_UpdateParent(obj->parent);
+
+       if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
+               return yaffs_DeleteHardLink(obj);
+       } else if (!ylist_empty(&obj->hardLinks)) {
+               /* Curve ball: We're unlinking an object that has a hardlink.
+                *
+                * This problem arises because we are not strictly following
                 * The Linux link/inode model.
                 *
                 * We can't really delete the object.
@@ -5329,12 +5400,12 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj)
 
                retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
 
-               if (retVal == YAFFS_OK) {
+               if (retVal == YAFFS_OK)
                        retVal = yaffs_DoGenericObjectDeletion(hl);
-               }
+
                return retVal;
 
-       } else if(immediateDeletion){
+       } else if (immediateDeletion) {
                switch (obj->variantType) {
                case YAFFS_OBJECT_TYPE_FILE:
                        return yaffs_DeleteFile(obj);
@@ -5353,19 +5424,19 @@ 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);
-       }
 }
 
 
 static int yaffs_UnlinkObject(yaffs_Object *obj)
 {
 
-       if (obj && obj->unlinkAllowed) {
+       if (obj && obj->unlinkAllowed)
                return yaffs_UnlinkWorker(obj);
-       }
 
        return YAFFS_FAIL;
 
@@ -5394,9 +5465,9 @@ 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;
-               }
        }
 
        /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
@@ -5407,6 +5478,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 */
@@ -5499,6 +5571,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;
@@ -5622,7 +5813,7 @@ static int yaffs_Scan(yaffs_Device *dev)
                                        dev->allocationBlock = blk;
                                        dev->allocationPage = c;
                                        dev->allocationBlockFinder = blk;
-                                       /* Set it to here to encourage the allocator to go forth from here. */
+                                       /* Set block finder here to encourage the allocator to go forth from here. */
 
                                }
 
@@ -5868,12 +6059,6 @@ static int yaffs_Scan(yaffs_Device *dev)
                                                break;
                                        }
 
-/*
-                                       if (parent == dev->deletedDir) {
-                                               yaffs_DestroyObject(in);
-                                               bi->hasShrinkHeader = 1;
-                                       }
-*/
                                }
                        }
                }
@@ -5883,6 +6068,12 @@ static int yaffs_Scan(yaffs_Device *dev)
                        state = YAFFS_BLOCK_STATE_FULL;
                }
 
+               if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
+                       /* If the block was partially allocated then treat it as fully allocated.*/
+                       state = YAFFS_BLOCK_STATE_FULL;
+                       dev->allocationBlock = -1;
+               }
+
                bi->blockState = state;
 
                /* Now let's see if it was dirty */
@@ -5915,13 +6106,13 @@ static int yaffs_Scan(yaffs_Device *dev)
                         * then setting the object header to unshadowed.
                         */
                        obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
-                       if(obj)
+                       if (obj)
                                yaffs_DeleteObject(obj);
-       
+
                        obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
-                       if(obj){
+
+                       if (obj)
                                yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
-                       }
 
                        YFREE(fixer);
                }
@@ -5929,9 +6120,8 @@ static int yaffs_Scan(yaffs_Device *dev)
 
        yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
 
-       if (alloc_failed) {
+       if (alloc_failed)
                return YAFFS_FAIL;
-       }
 
        T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
 
@@ -5986,7 +6176,7 @@ static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
                yaffs_SetObjectName(in, oh->name);
 
                if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
-                        in->variant.symLinkVariant.alias =
+                       in->variant.symLinkVariant.alias =
                                                    yaffs_CloneString(oh->alias);
                        if (!in->variant.symLinkVariant.alias)
                                alloc_failed = 1; /* Not returned to caller */
@@ -6105,9 +6295,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
 
                                nBlocksToScan++;
 
-                               if (sequenceNumber >= dev->sequenceNumber) {
+                               if (sequenceNumber >= dev->sequenceNumber)
                                        dev->sequenceNumber = sequenceNumber;
-                               }
                        } else {
                                /* TODO: Nasty sequence number! */
                                T(YAFFS_TRACE_SCAN,
@@ -6227,13 +6416,9 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
                                                        dev->allocationBlockFinder = blk;
                                                } else {
                                                        /* This is a partially written block that is not
-                                                        * the current allocation block. This block must have
-                                                        * had a write failure, so set up for retirement.
+                                                        * the current allocation block.
                                                         */
 
-                                                        /* bi->needsRetiring = 1; ??? TODO */
-                                                        bi->gcPrioritise = 1;
-
                                                         T(YAFFS_TRACE_ALWAYS,
                                                         (TSTR("Partially written block %d detected" TENDSTR),
                                                         blk));
@@ -6326,9 +6511,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 ||
@@ -6412,9 +6595,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
                                                            thisSize;
                                                }
 
-                                               if (isShrink) {
+                                               if (isShrink)
                                                        bi->hasShrinkHeader = 1;
-                                               }
 
                                        }
                                        /* Use existing - destroy this one. */
@@ -6498,6 +6680,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
                                                                           oh->
                                                                           shadowsObject,
                                                                           1);
+                                                       
 
 
                                                yaffs_SetObjectName(in, oh->name);
@@ -6629,6 +6812,7 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
                        state = YAFFS_BLOCK_STATE_FULL;
                }
 
+
                bi->blockState = state;
 
                /* Now let's see if it was dirty */
@@ -6639,6 +6823,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
                }
 
        }
+       
+       yaffs_SkipRestOfBlock(dev);
 
        if (altBlockIndex)
                YFREE_ALT(blockIndex);
@@ -6655,9 +6841,8 @@ static int yaffs_ScanBackwards(yaffs_Device *dev)
 
        yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
 
-       if (alloc_failed) {
+       if (alloc_failed)
                return YAFFS_FAIL;
-       }
 
        T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
 
@@ -6714,7 +6899,7 @@ static void yaffs_VerifyDirectory(yaffs_Object *directory)
 {
        struct ylist_head *lh;
        yaffs_Object *listObj;
-        
+
        if (!directory) {
                YBUG();
                return;
@@ -6742,6 +6927,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)
 {
@@ -6759,11 +6964,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)
 {
@@ -6819,9 +7023,8 @@ yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
 
        yaffs_Object *l;
 
-       if (!name) {
+       if (!name)
                return NULL;
-       }
 
        if (!directory) {
                T(YAFFS_TRACE_ALWAYS,
@@ -6851,18 +7054,16 @@ yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
 
                        /* Special case for lost-n-found */
                        if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
-                               if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
+                               if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
                                        return l;
-                               }
                        } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
                                /* LostnFound chunk called Objxxx
                                 * Do a real check
                                 */
                                yaffs_GetObjectName(l, buffer,
-                                                   YAFFS_MAX_NAME_LENGTH);
-                               if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0) {
+                                                   YAFFS_MAX_NAME_LENGTH + 1);
+                               if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
                                        return l;
-                               }
                        }
                }
        }
@@ -6890,16 +7091,15 @@ int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
                T(YAFFS_TRACE_ALWAYS,
                  (TSTR
                   ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
-                YBUG();
-                return YAFFS_FAIL;
-        }
+               YBUG();
+               return YAFFS_FAIL;
+       }
 
        ylist_for_each(i, &theDir->variant.directoryVariant.children) {
                if (i) {
                        l = ylist_entry(i, yaffs_Object, siblings);
-                       if (l && !fn(l)) {
+                       if (l && !fn(l))
                                return YAFFS_FAIL;
-                       }
                }
        }
 
@@ -6948,9 +7148,8 @@ int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
 
        }
 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
-       else if (obj->shortName[0]) {
-               yaffs_strcpy(name, obj->shortName);
-       }
+       else if (obj->shortName[0])
+               yaffs_strncpy(name, obj->shortName,YAFFS_SHORT_NAME_LENGTH+1);
 #endif
        else {
                int result;
@@ -6966,11 +7165,12 @@ int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
                                                        NULL);
                }
                yaffs_strncpy(name, oh->name, buffSize - 1);
+               name[buffSize-1]=0;
 
                yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
        }
 
-       return yaffs_strlen(name);
+       return yaffs_strnlen(name,buffSize-1);
 }
 
 int yaffs_GetObjectFileLength(yaffs_Object *obj)
@@ -6978,11 +7178,12 @@ int yaffs_GetObjectFileLength(yaffs_Object *obj)
        /* Dereference any hard linking */
        obj = yaffs_GetEquivalentObject(obj);
 
-       if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
+       if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
                return obj->variant.fileVariant.fileSize;
-       }
-       if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
-               return yaffs_strlen(obj->variant.symLinkVariant.alias);
+       if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
+               if(!obj->variant.symLinkVariant.alias)
+                       return 0;
+               return yaffs_strnlen(obj->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
        } else {
                /* Only a directory should drop through to here */
                return obj->myDev->nDataBytesPerChunk;
@@ -6994,12 +7195,12 @@ int yaffs_GetObjectLinkCount(yaffs_Object *obj)
        int count = 0;
        struct ylist_head *i;
 
-       if (!obj->unlinked) {
+       if (!obj->unlinked)
                count++;                /* the object itself */
-       }
-       ylist_for_each(i, &obj->hardLinks) {
+
+       ylist_for_each(i, &obj->hardLinks)
                count++;                /* add the hard links; */
-       }
+
        return count;
 }
 
@@ -7045,11 +7246,10 @@ unsigned yaffs_GetObjectType(yaffs_Object *obj)
 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
 {
        obj = yaffs_GetEquivalentObject(obj);
-       if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
+       if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
                return yaffs_CloneString(obj->variant.symLinkVariant.alias);
-       } else {
+       else
                return yaffs_CloneString(_Y(""));
-       }
 }
 
 #ifndef CONFIG_YAFFS_WINCE
@@ -7113,7 +7313,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
@@ -7369,9 +7569,8 @@ int yaffs_GutsInitialise(yaffs_Device *dev)
                void *buf;
                int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
 
-               if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
+               if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
                        dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
-               }
 
                dev->srCache =  YMALLOC(srCacheBytes);
 
@@ -7400,9 +7599,9 @@ int yaffs_GutsInitialise(yaffs_Device *dev)
                        init_failed = 1;
        }
 
-       if (dev->isYaffs2) {
+       if (dev->isYaffs2)
                dev->useHeaderFileSize = 1;
-       }
+
        if (!init_failed && !yaffs_InitialiseBlocks(dev))
                init_failed = 1;
 
@@ -7455,6 +7654,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) {
@@ -7478,6 +7680,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)));
@@ -7508,10 +7713,8 @@ void yaffs_Deinitialise(yaffs_Device *dev)
 
                YFREE(dev->gcCleanupList);
 
-               for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
+               for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
                        YFREE(dev->tempBuffer[i].buffer);
-               }
-
 
                dev->isMounted = 0;
 
@@ -7555,6 +7758,7 @@ int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
        int nFree;
        int nDirtyCacheChunks;
        int blocksForCheckpoint;
+       int i;
 
 #if 1
        nFree = dev->nFreeChunks;
@@ -7566,12 +7770,9 @@ int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
 
        /* Now count the number of dirty chunks in the cache and subtract those */
 
-       {
-               int i;
-               for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
-                       if (dev->srCache[i].dirty)
-                               nDirtyCacheChunks++;
-               }
+       for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
+               if (dev->srCache[i].dirty)
+                       nDirtyCacheChunks++;
        }
 
        nFree -= nDirtyCacheChunks;
@@ -7619,7 +7820,7 @@ static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
 #define yaffs_CheckStruct(structure, syze, name) \
        do { \
                if (sizeof(structure) != syze) { \
-               T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
+                       T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
                                name, syze, sizeof(structure))); \
                        return YAFFS_FAIL; \
                } \