Fix problem where object creation fills flash
[yaffs2.git] / yaffs_guts.c
index d89b87af44eced13f0f7bd0a584581cc579f2914..81ae37a400425ae696818bfa0cdd2180f6e4ba58 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.86 2009-07-28 03:04:54 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);
 
@@ -759,7 +760,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,
@@ -2345,6 +2346,7 @@ static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
                        in = NULL;
                }
 
+               yaffs_UpdateParent(parent);
        }
 
        return in;
@@ -2499,6 +2501,9 @@ int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
                                                existingTarget->objectId);
                        yaffs_UnlinkObject(existingTarget);
                }
+               yaffs_UpdateParent(oldDir);
+               if(newDir != oldDir)
+                       yaffs_UpdateParent(newDir);
 
                return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
        }
@@ -2980,7 +2985,8 @@ static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
        /* 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;
 
@@ -3624,7 +3630,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) {
@@ -3640,7 +3646,7 @@ static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
        if (newChunkId >= 0) {
                yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
 
-               if (prevChunkId >= 0)
+               if (prevChunkId > 0)
                        yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
 
                yaffs_CheckFileSanity(in);
@@ -3726,7 +3732,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));
@@ -3784,13 +3790,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__);
                        }
@@ -5230,6 +5236,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)) {
@@ -6666,6 +6675,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 +6712,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)
 {