Fix issue with unlinking hardlinks failing sanity check
[yaffs2.git] / yaffs_guts.c
index d6e4b11a90acff73824a5a2c94c42707b2884b01..d4349c92ad2342c151f9f9755791b5751aae0bb9 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 const char *yaffs_guts_c_version =
-    "$Id: yaffs_guts.c,v 1.101 2009-12-25 01:53:05 charles Exp $";
+    "$Id: yaffs_guts.c,v 1.104 2010-01-05 22:58:15 charles Exp $";
 
 #include "yportenv.h"
 
@@ -1351,6 +1351,7 @@ static void yaffs_DeinitialiseTnodes(yaffs_Device *dev)
 
        dev->freeTnodes = NULL;
        dev->nFreeTnodes = 0;
+       dev->nTnodesCreated = 0;
 }
 
 static void yaffs_InitialiseTnodes(yaffs_Device *dev)
@@ -1807,6 +1808,10 @@ static void yaffs_SoftDeleteFile(yaffs_Object *obj)
  * level 0 tnode entries must be zeroed out.
  * Could also use this for file deletion, but that's probably better handled
  * by a special case.
+ *
+ * This function is recursive. For levels > 0 the function is called again on
+ * any sub-tree. For level == 0 we just check if the sub-tree has data.
+ * If there is no data in a subtree then it is pruned.
  */
 
 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
@@ -1819,25 +1824,25 @@ static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
                hasData = 0;
 
                if(level > 0){
-                       for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
-                               if (tn->internal[i]) {
-                                       tn->internal[i] =
-                                               yaffs_PruneWorker(dev, tn->internal[i],
-                                                       level - 1,
-                                                       (i == 0) ? del0 : 1);
-                               }
-
-                               if (tn->internal[i])
-                                       hasData++;
-                        }
+                       for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
+                               if (tn->internal[i]) {
+                                       tn->internal[i] =
+                                               yaffs_PruneWorker(dev, tn->internal[i],
+                                                       level - 1,
+                                                       (i == 0) ? del0 : 1);
+                               }
+
+                               if (tn->internal[i])
+                                       hasData++;
+                       }
                } else {
-                       int tnodeSize;
-                       __u32 *map = (__u32 *)tn;
-                       tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
+                       int tnodeSize;
+                       __u32 *map = (__u32 *)tn;
+                       tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
 
-                       if (tnodeSize < sizeof(yaffs_Tnode))
-                               tnodeSize = sizeof(yaffs_Tnode);
-                        tnodeSize /= sizeof(__u32);
+                       if (tnodeSize < sizeof(yaffs_Tnode))
+                               tnodeSize = sizeof(yaffs_Tnode);
+                       tnodeSize /= sizeof(__u32);
 
                         for(i = 0; !hasData && i < tnodeSize; i++){
                                 if(map[i])
@@ -2105,6 +2110,7 @@ static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
 
        dev->freeObjects = NULL;
        dev->nFreeObjects = 0;
+       dev->nObjectsCreated = 0;
 }
 
 static void yaffs_InitialiseObjects(yaffs_Device *dev)
@@ -5397,23 +5403,26 @@ static int yaffs_UnlinkWorker(yaffs_Object *obj)
                 * Instead, we do the following:
                 * - Select a hardlink.
                 * - Unhook it from the hard links
-                * - Unhook it from its parent directory (so that the rename can work)
+                * - Move it from its parent directory (so that the rename can work)
                 * - Rename the object to the hardlink's name.
                 * - Delete the hardlink
                 */
 
                yaffs_Object *hl;
+               yaffs_Object *parent;
                int retVal;
                YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
 
                hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
 
+               yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
+               parent = hl->parent;
+
                ylist_del_init(&hl->hardLinks);
-               ylist_del_init(&hl->siblings);
 
-               yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
+               yaffs_AddObjectToDirectory(obj->myDev->unlinkedDir, hl);
 
-               retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
+               retVal = yaffs_ChangeObjectName(obj,parent, name, 0, 0);
 
                if (retVal == YAFFS_OK)
                        retVal = yaffs_DoGenericObjectDeletion(hl);