+ int hitLimit = 0;
+
+ for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
+ {
+ if(tn->level0[i])
+ {
+ int j;
+
+ chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
+
+ theChunk = tn->level0[i] << in->myDev->chunkGroupBits;
+
+ // Now we need to search for it
+ for(j = 0,found = 0; theChunk && j < in->myDev->chunkGroupSize && !found; j++)
+ {
+ yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,&tags,&chunkDeleted);
+ if(yaffs_TagsMatch(&tags,in->objectId,chunkInInode,chunkDeleted))
+ {
+ // found it;
+ found = 1;
+
+ }
+ else
+ {
+ theChunk++;
+ }
+ }
+
+ if(found)
+ {
+ yaffs_DeleteChunk(in->myDev,theChunk,1);
+ in->nDataChunks--;
+ if(limit)
+ {
+ *limit = *limit-1;
+ if(limit <= 0)
+ {
+ hitLimit = 1;
+ }
+ }
+
+ }
+
+ tn->level0[i] = 0;
+ }
+
+ }
+ return (i < 0) ? 1 : 0;
+
+
+ }
+
+ }
+
+ return 1;
+
+}
+
+// SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
+// All soft deleting does is increment the block's softdelete count and pulls the chunk out
+// of the tnode.
+// THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
+//
+static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
+{
+ int i;
+ int chunkInInode;
+ int theChunk;
+ yaffs_BlockInfo *theBlock;
+ yaffs_Tags tags;
+ int found;
+ int chunkDeleted;
+ int allDone = 1;
+
+
+ if(tn)
+ {
+ if(level > 0)
+ {
+
+ for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
+ {
+ if(tn->internal[i])
+ {
+ allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
+ (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
+ if(allDone)
+ {
+ yaffs_FreeTnode(in->myDev,tn->internal[i]);
+ tn->internal[i] = NULL;
+ }
+ else
+ {
+ //Hoosterman... how could this happen.
+ }
+ }
+ }
+ return (allDone) ? 1 : 0;
+ }
+ else if(level == 0)
+ {
+
+ for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
+ {
+ if(tn->level0[i])
+ {
+
+ theChunk = (tn->level0[i] << in->myDev->chunkGroupBits);
+ theBlock = yaffs_GetBlockInfo(in->myDev, theChunk/in->myDev->nChunksPerBlock);
+ if(theBlock)
+ {
+ theBlock->softDeletions++;
+ }
+ tn->level0[i] = 0;
+ }
+
+ }
+ return 1;
+
+ }
+
+ }
+
+ return 1;
+
+}
+
+
+
+static void yaffs_SoftDeleteFile(yaffs_Object *obj)
+{
+ if(obj->deleted &&
+ obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
+ !obj->softDeleted)
+ {
+ if(obj->nDataChunks <= 0)
+ {
+ // Empty file, just delete it immediately
+ yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
+ obj->variant.fileVariant.top = NULL;
+ T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
+ yaffs_DoGenericObjectDeletion(obj);
+ }
+ else
+ {
+ yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
+ obj->softDeleted = 1;
+ }
+ }
+}
+
+
+
+
+
+// Pruning removes any part of the file structure tree that is beyond the
+// bounds of the file (ie that does not point to chunks).
+//
+// A file should only get pruned when its size is reduced.
+//
+// Before pruning, the chunks must be pulled from the tree and the
+// 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.
+
+// yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
+
+static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
+{
+ int i;
+ int hasData;
+
+ if(tn)
+ {
+ hasData = 0;
+
+ for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
+ {
+ if(tn->internal[i] && level > 0)
+ {
+ tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
+ }
+
+ if(tn->internal[i])
+ {
+ hasData++;
+ }
+ }
+
+ if(hasData == 0 && del0)
+ {
+ // Free and return NULL