Fix free space reporting as per Mikhail Rileev's patch
[yaffs2.git] / yaffs_guts.c
index b17262beee992cfedb11d4b7031791188fe248ef..e949e319aa2fb1a5b60dad1ae117839c8b10fa93 100644 (file)
@@ -13,7 +13,7 @@
  */
 
 const char *yaffs_guts_c_version =
-    "$Id: yaffs_guts.c,v 1.24 2005-12-07 21:49:18 charles Exp $";
+    "$Id: yaffs_guts.c,v 1.29 2006-02-08 22:29:14 charles Exp $";
 
 #include "yportenv.h"
 
@@ -335,6 +335,7 @@ static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
  
 static Y_INLINE int yaffs_HashFunction(int n)
 {
+       n = abs(n);
        return (n % YAFFS_NOBJECT_BUCKETS);
 }
 
@@ -541,7 +542,7 @@ static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
        int tnodeSize;
        yaffs_Tnode *newTnodes;
        __u8 *mem;
-       yaffs_Tnode *current;
+       yaffs_Tnode *curr;
        yaffs_Tnode *next;
        yaffs_TnodeList *tnl;
 
@@ -554,7 +555,8 @@ static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
 
        /* make these things */
 
-       mem = newTnodes = YMALLOC(nTnodes * tnodeSize);
+       newTnodes = YMALLOC(nTnodes * tnodeSize);
+       mem = (__u8 *)newTnodes;
 
        if (!newTnodes) {
                T(YAFFS_TRACE_ERROR,
@@ -579,13 +581,13 @@ static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
 #else
        /* New hookup for wide tnodes */
        for(i = 0; i < nTnodes -1; i++) {
-               current = (yaffs_Tnode *) &mem[i * tnodeSize];
+               curr = (yaffs_Tnode *) &mem[i * tnodeSize];
                next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
-               current->internal[0] = next;
+               curr->internal[0] = next;
        }
        
-       current = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
-       current->internal[0] = dev->freeTnodes;
+       curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
+       curr->internal[0] = dev->freeTnodes;
        dev->freeTnodes = (yaffs_Tnode *)mem;
 
 #endif
@@ -2345,6 +2347,7 @@ static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
                                   ("yaffs: About to finally delete object %d"
                                    TENDSTR), object->objectId));
                                yaffs_DoGenericObjectDeletion(object);
+                               object->myDev->nDeletedFiles--;
                        }
 
                }
@@ -4436,7 +4439,7 @@ static int yaffs_ScanBackwards(yaffs_Device * dev)
        yaffs_Object *in;
        yaffs_Object *parent;
        int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
-
+       int itsUnlinked;
        __u8 *chunkData;
 
        yaffs_BlockIndex *blockIndex = NULL;
@@ -4846,11 +4849,8 @@ static int yaffs_ScanBackwards(yaffs_Device * dev)
 
                                        yaffs_AddObjectToDirectory(parent, in);
 
-                                       if ((parent == dev->deletedDir ||
-                                            parent == dev->unlinkedDir)) {
-                                            /* If it is unlinked at start up then it wants deleting */
-                                               in->deleted = 1;
-                                       }
+                                       itsUnlinked = (parent == dev->deletedDir) ||
+                                                     (parent == dev->unlinkedDir);
 
                                        if (oh->isShrink) {
                                                /* Mark the block as having a shrinkHeader */
@@ -4893,11 +4893,13 @@ static int yaffs_ScanBackwards(yaffs_Device * dev)
 
                                                break;
                                        case YAFFS_OBJECT_TYPE_HARDLINK:
-                                               in->variant.hardLinkVariant.equivalentObjectId =
+                                               if(!itsUnlinked) {
+                                                 in->variant.hardLinkVariant.equivalentObjectId =
                                                    oh->equivalentObjectId;
-                                               in->hardLinks.next =
+                                                 in->hardLinks.next =
                                                    (struct list_head *) hardList;
-                                               hardList = in;
+                                                 hardList = in;
+                                               }
                                                break;
                                        case YAFFS_OBJECT_TYPE_DIRECTORY:
                                                /* Do nothing */
@@ -5690,6 +5692,8 @@ int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
        nFree = yaffs_CountFreeChunks(dev);
 #endif
 
+       nFree += dev->nDeletedFiles;
+       
        /* Now count the number of dirty chunks in the cache and subtract those */
 
        {