*** empty log message ***
[yaffs2.git] / yaffs_guts.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  * yaffs_guts.c  The main guts of YAFFS
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  */
14  //yaffs_guts.c
15
16 const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.3 2004-11-22 03:22:25 charles Exp $";
17
18 #include "yportenv.h"
19
20 #include "yaffsinterface.h"
21 #include "yaffs_guts.h"
22
23 #include "yaffs_tagscompat.h"
24
25 #ifdef CONFIG_YAFFS_WINCE
26 void yfsd_LockYAFFS(BOOL fsLockOnly);
27 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
28 #endif
29
30 #define YAFFS_PASSIVE_GC_CHUNKS 2
31
32 #if 0
33 // Use Steven Hill's ECC struff instead
34 // External functions for ECC on data
35 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
36 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
37 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
38 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
39 #else
40 #include "yaffs_ecc.h"
41 #endif
42
43 // countBits is a quick way of counting the number of bits in a byte.
44 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
45
46 static const char yaffs_countBitsTable[256] =
47 {
48 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
49 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
50 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
52 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
53 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
54 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
55 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
56 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
58 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
59 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
60 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
61 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
62 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
63 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
64 };
65
66 static int yaffs_CountBits(__u8 x)
67 {
68         int retVal;
69         retVal = yaffs_countBitsTable[x];
70         return retVal;
71 }
72
73
74 #if 0
75 // Stuff using yea olde tags
76 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
77 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
78
79 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
80 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
81 #else
82 #endif
83
84 // NAND access
85
86
87 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
88 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
89 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
90 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
91 // Local prototypes
92 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
93 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
94 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
95
96 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
97 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
98 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
99 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
100 static int yaffs_CheckStructures(void);
101 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
102 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
103
104 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
105
106 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
107 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
108
109
110 static int  yaffs_ValidateTags(yaffs_ExtendedTags *tags);
111
112 // Robustification (if it ever comes about...)
113 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
114 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
115 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
116 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
117 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND,  const yaffs_ExtendedTags *tags);
118
119 static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
120
121 static int yaffs_UnlinkWorker(yaffs_Object *obj);
122 static void yaffs_DestroyObject(yaffs_Object *obj);
123
124 #if 0
125 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
126 #endif
127
128 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
129
130
131 loff_t yaffs_GetFileSize(yaffs_Object *obj);
132
133
134 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
135
136 #ifdef YAFFS_PARANOID
137 static int yaffs_CheckFileSanity(yaffs_Object *in);
138 #else
139 #define yaffs_CheckFileSanity(in)
140 #endif
141
142 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
143 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
144
145 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
146 {
147         if(dev->readChunkWithTagsFromNAND)
148                 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
149         else
150                 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
151 }
152
153 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
154 {
155         if(tags)
156         {
157                 tags->sequenceNumber = dev->sequenceNumber;
158                 tags->chunkUsed = 1;
159                 if(!yaffs_ValidateTags(tags))
160                 {
161                         T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
162                         YBUG();
163                 }
164                 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
165         }
166         else
167         {
168                         T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
169                         YBUG();
170         }
171
172         if(dev->writeChunkWithTagsToNAND)
173                 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
174         else
175                 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
176 }
177
178 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
179 {
180         if(dev->markNANDBlockBad)
181                 return dev->markNANDBlockBad(dev,blockNo);
182         else
183                 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
184 }
185 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
186 {
187         if(dev->queryNANDBlock)
188                 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
189         else
190                 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
191 }
192
193 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
194 {
195         int result;
196
197         dev->nBlockErasures++;
198         result = dev->eraseBlockInNAND(dev,blockInNAND);
199
200         if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
201         return result;
202 }
203
204 static int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
205 {
206         return dev->initialiseNAND(dev);
207 }
208
209
210
211
212 // Temporary buffer manipulations
213
214 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
215 {
216         int i,j;
217         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
218         {
219                 if(dev->tempBuffer[i].line == 0)
220                 {
221                         dev->tempBuffer[i].line = lineNo;
222                         if((i+1) > dev->maxTemp)
223                         {
224                                 dev->maxTemp = i + 1;
225                                 for(j = 0; j <= i; j++)
226                                         dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
227                         }       
228                         
229                         return dev->tempBuffer[i].buffer;
230                 }
231         }
232
233         T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
234         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
235         {
236                 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
237         }
238         T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
239         
240         dev->unmanagedTempAllocations++;
241         // Get an unmanaged one
242         return YMALLOC(dev->nBytesPerChunk);    
243         
244         
245 }
246
247 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
248 {
249         int i;
250         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
251         {
252                 if(dev->tempBuffer[i].buffer == buffer)
253                 {
254                         dev->tempBuffer[i].line = 0;
255                         return;
256                 }
257         }
258         
259         if(buffer)
260         {
261                 // assume it is an unmanaged one.
262                 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
263                 YFREE(buffer);
264                 dev->unmanagedTempDeallocations++;
265         }
266
267 }
268
269
270 // Chunk bitmap manipulations
271
272 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
273 {
274         if(blk < dev->startBlock || blk > dev->endBlock)
275         {
276                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
277                 YBUG();
278         }
279         return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock));
280 }
281
282 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
283 {
284         __u8 *blkBits = yaffs_BlockBits(dev,blk);
285
286          memset(blkBits,0,dev->chunkBitmapStride);
287 }
288
289 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
290 {
291         __u8 *blkBits = yaffs_BlockBits(dev,blk);
292         
293         blkBits[chunk/8] &=  ~ (1<<(chunk & 7));
294 }
295
296 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
297 {
298         __u8 *blkBits = yaffs_BlockBits(dev,blk);
299
300         blkBits[chunk/8] |=   (1<<(chunk & 7));
301 }
302
303 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
304 {
305         __u8 *blkBits = yaffs_BlockBits(dev,blk);
306         return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;
307 }
308
309 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
310 {
311         __u8 *blkBits = yaffs_BlockBits(dev,blk);
312         int i;
313         for(i = 0; i < dev->chunkBitmapStride; i++)
314         {
315                 if(*blkBits) return 1;
316                 blkBits++;
317         }
318         return 0;
319 }
320
321 #if 0
322 // Function to manipulate block info
323 static  Y_INLINE yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
324 {
325         if(blk < dev->startBlock || blk > dev->endBlock)
326         {
327                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));
328                 YBUG();
329         }
330         return &dev->blockInfo[blk - dev->startBlock];
331 }
332 #endif
333
334
335 static  Y_INLINE int yaffs_HashFunction(int n)
336 {
337         return (n % YAFFS_NOBJECT_BUCKETS);
338 }
339
340
341 yaffs_Object *yaffs_Root(yaffs_Device *dev)
342 {
343         return dev->rootDir;
344 }
345
346 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
347 {
348         return dev->lostNFoundDir;
349 }
350
351 #if 0
352 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
353 {
354         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
355         {
356                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
357                 return YAFFS_FAIL;
358         }
359
360         dev->nPageWrites++;
361         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
362 }
363
364
365
366 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
367                                                         int chunkInNAND, 
368                                                         __u8 *data, 
369                                                         yaffs_Spare *spare, 
370                                                         int doErrorCorrection)
371 {
372         int retVal;
373         yaffs_Spare localSpare;
374
375         dev->nPageReads++;
376         
377         
378
379         
380         if(!spare && data)
381         {
382                 // If we don't have a real spare, then we use a local one.
383                 // Need this for the calculation of the ecc
384                 spare = &localSpare;
385         }
386         
387
388         if(!dev->useNANDECC)
389         {
390                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
391                 if(data && doErrorCorrection)
392                 {
393                         // Do ECC correction
394                         //Todo handle any errors
395                 int eccResult1,eccResult2;
396                 __u8 calcEcc[3];
397                 
398                         yaffs_ECCCalculate(data,calcEcc);
399                         eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
400                         yaffs_ECCCalculate(&data[256],calcEcc);
401                         eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
402
403                         if(eccResult1>0)
404                         {
405                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
406                                 dev->eccFixed++;
407                         }
408                         else if(eccResult1<0)
409                         {
410                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
411                                 dev->eccUnfixed++;
412                         }
413
414                         if(eccResult2>0)
415                         {
416                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
417                                 dev->eccFixed++;
418                         }
419                         else if(eccResult2<0)
420                         {
421                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
422                                 dev->eccUnfixed++;
423                         }
424
425                         if(eccResult1 || eccResult2)
426                         {
427                                 // Hoosterman, we had a data problem on this page
428                                 yaffs_HandleReadDataError(dev,chunkInNAND);
429                         }
430                 }
431         }
432         else
433         {
434         // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
435         struct yaffs_NANDSpare nspare;
436                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
437                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
438                 if(data && doErrorCorrection)
439                 {
440                         if(nspare.eccres1>0)
441                         {
442                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
443                         }
444                         else if(nspare.eccres1<0)
445                         {
446                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
447                         }
448
449                         if(nspare.eccres2>0)
450                         {
451                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
452                         }
453                         else if(nspare.eccres2<0)
454                         {
455                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
456                         }
457
458                         if(nspare.eccres1 || nspare.eccres2)
459                         {
460                                 // Hoosterman, we had a data problem on this page
461                                 yaffs_HandleReadDataError(dev,chunkInNAND);
462                         }
463
464                 }
465         }
466         return retVal;
467 }
468
469 #endif
470
471
472
473 int yaffs_CheckFF(__u8 *buffer,int nBytes)
474 {
475         //Horrible, slow implementation
476         while(nBytes--)
477         {
478                 if(*buffer != 0xFF) return 0; 
479         }
480         return 1;
481 }
482
483 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
484 {
485
486         int retval = YAFFS_OK;
487         __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
488         yaffs_ExtendedTags tags;
489
490         dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
491         
492         if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
493         {
494                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
495                 retval = YAFFS_FAIL;
496         }
497
498         yaffs_ReleaseTempBuffer(dev,data,__LINE__);
499         
500         return retval;
501         
502 }
503
504
505
506 #if 1
507 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
508 {
509         int chunk;
510         
511         int writeOk = 1;
512         int attempts = 0;
513         
514
515         
516         do{
517                 chunk = yaffs_AllocateChunk(dev,useReserve);
518         
519                 if(chunk >= 0)
520                 {
521
522                         // First check this chunk is erased...
523 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
524                         writeOk = yaffs_CheckChunkErased(dev,chunk);
525 #endif          
526                         if(!writeOk)
527                         {
528                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
529                         }
530                         else
531                         {
532                                 writeOk =  yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
533                         }
534                         attempts++;
535
536                         if(writeOk)
537                         {
538                                 // Copy the data into the robustification buffer.
539                                 // NB We do this at the end to prevent duplicates in the case of a write error.
540                                 //Todo
541                                 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
542                         }
543                         else
544                         {
545                                 yaffs_HandleWriteChunkError(dev,chunk);
546                         }
547                 }
548                 
549         } while(chunk >= 0 && ! writeOk);
550         
551         if(attempts > 1)
552         {
553                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
554                 dev->nRetriedWrites+= (attempts - 1);   
555         }
556         
557
558         
559         return chunk;
560 }
561 #endif
562
563
564 ///
565 // Functions for robustisizing
566 //
567 //
568
569 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
570 {
571         
572         yaffs_MarkBlockBad(dev,blockInNAND);
573         
574         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
575
576         dev->nRetiredBlocks++;
577 }
578
579
580
581 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
582 {
583         dev->doingBufferedBlockRewrite = 1;
584         //
585         //      Remove erased chunks
586         //  Rewrite existing chunks to a new block
587         //      Set current write block to the new block
588         
589         dev->doingBufferedBlockRewrite = 0;
590         
591         return 1;
592 }
593
594
595 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
596 {
597         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
598
599         // Mark the block for retirement
600         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
601         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
602
603
604         //TODO  
605         // Just do a garbage collection on the affected block then retire the block
606         // NB recursion
607 }
608
609
610 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
611 {
612 }
613
614 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
615 {
616 }
617
618 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
619 {
620 }
621
622 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
623 {
624         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
625
626         // Mark the block for retirement
627         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
628         // Delete the chunk
629         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
630 }
631
632
633
634 #if 0
635 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
636 {
637
638
639         if( memcmp(d0,d1,dataSize) != 0 ||
640                 s0->tagByte0 != s1->tagByte0 ||
641                 s0->tagByte1 != s1->tagByte1 ||
642                 s0->tagByte2 != s1->tagByte2 ||
643                 s0->tagByte3 != s1->tagByte3 ||
644                 s0->tagByte4 != s1->tagByte4 ||
645                 s0->tagByte5 != s1->tagByte5 ||
646                 s0->tagByte6 != s1->tagByte6 ||
647                 s0->tagByte7 != s1->tagByte7 ||
648                 s0->ecc1[0]  != s1->ecc1[0]  ||
649                 s0->ecc1[1]  != s1->ecc1[1]  ||
650                 s0->ecc1[2]  != s1->ecc1[2]  ||
651                 s0->ecc2[0]  != s1->ecc2[0]  ||
652                 s0->ecc2[1]  != s1->ecc2[1]  ||
653                 s0->ecc2[2]  != s1->ecc2[2] )
654                 {
655                         return 0;
656                 }
657         
658         return 1;
659 }
660 #endif
661
662
663 ///////////////////////// Object management //////////////////
664 // List of spare objects
665 // The list is hooked together using the first pointer
666 // in the object
667
668 // static yaffs_Object *yaffs_freeObjects = NULL;
669
670 // static int yaffs_nFreeObjects;
671
672 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
673
674 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
675
676
677 static __u16 yaffs_CalcNameSum(const YCHAR *name)
678 {
679         __u16 sum = 0;
680         __u16 i = 1;
681         
682         YUCHAR *bname = (YUCHAR *)name;
683         if(bname)
684         {
685                 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
686                 {
687
688 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
689                         sum += yaffs_toupper(*bname) * i;
690 #else
691                         sum += (*bname) * i;
692 #endif
693                         i++;
694                         bname++;
695                 }
696         }
697         return sum;
698 }
699
700 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
701 {
702 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
703                                         if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
704                                         {
705                                                 yaffs_strcpy(obj->shortName,name);
706                                         }
707                                         else
708                                         {
709                                                 obj->shortName[0]=_Y('\0');
710                                         }
711 #endif
712                                         obj->sum = yaffs_CalcNameSum(name);
713 }
714
715 #if 0
716 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
717 {
718         yaffs_ECCCalculate(data , spare->ecc1);
719         yaffs_ECCCalculate(&data[256] , spare->ecc2);
720 }
721 #endif
722
723
724 ///////////////////////// TNODES ///////////////////////
725
726 // List of spare tnodes
727 // The list is hooked together using the first pointer
728 // in the tnode.
729
730 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
731
732 // static int yaffs_nFreeTnodes;
733
734 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
735
736
737
738 // yaffs_CreateTnodes creates a bunch more tnodes and
739 // adds them to the tnode free list.
740 // Don't use this function directly
741
742 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
743 {
744     int i;
745     yaffs_Tnode *newTnodes;
746     yaffs_TnodeList *tnl;
747     
748     if(nTnodes < 1) return YAFFS_OK;
749    
750         // make these things
751         
752     newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
753    
754     if (!newTnodes)
755     {
756                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
757                 return YAFFS_FAIL;
758     }
759     
760     // Hook them into the free list
761     for(i = 0; i < nTnodes - 1; i++)
762     {
763         newTnodes[i].internal[0] = &newTnodes[i+1];
764 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
765         newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
766 #endif
767     }
768         
769         newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
770 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
771         newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
772 #endif
773         dev->freeTnodes = newTnodes;
774         dev->nFreeTnodes+= nTnodes;
775         dev->nTnodesCreated += nTnodes;
776
777         // Now add this bunch of tnodes to a list for freeing up.
778         // NB If we can't add this to the management list it isn't fatal
779         // but it just means we can't free this bunch of tnodes later.
780         tnl = YMALLOC(sizeof(yaffs_TnodeList));
781         if(!tnl)
782         {
783                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
784                 
785         }
786         else
787         {
788                 tnl->tnodes = newTnodes;
789                 tnl->next = dev->allocatedTnodeList;
790                 dev->allocatedTnodeList = tnl;
791         }
792
793
794         T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
795
796
797         return YAFFS_OK;
798 }
799
800
801 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
802 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
803 {
804         yaffs_Tnode *tn = NULL;
805         
806         // If there are none left make more
807         if(!dev->freeTnodes)
808         {
809                 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
810         }
811         
812         if(dev->freeTnodes)
813         {
814                 tn = dev->freeTnodes;
815 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
816         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
817                 {
818                         // Hoosterman, this thing looks like it isn't in the list
819                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
820                 }
821 #endif
822                 dev->freeTnodes = dev->freeTnodes->internal[0];
823                 dev->nFreeTnodes--;
824                 // zero out
825                 memset(tn,0,sizeof(yaffs_Tnode));
826         }
827         
828
829         return tn;
830 }
831
832
833 // FreeTnode frees up a tnode and puts it back on the free list
834 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
835 {
836         if(tn)
837         {
838 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
839         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
840                 {
841                         // Hoosterman, this thing looks like it is already in the list
842                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
843                 }
844                 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
845 #endif
846                 tn->internal[0] = dev->freeTnodes;
847                 dev->freeTnodes = tn;
848                 dev->nFreeTnodes++;
849         }
850 }
851
852
853 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
854 {
855         // Free the list of allocated tnodes
856         yaffs_TnodeList *tmp;
857                 
858         while(dev->allocatedTnodeList)
859         {
860                 tmp =  dev->allocatedTnodeList->next;
861
862                 YFREE(dev->allocatedTnodeList->tnodes);
863                 YFREE(dev->allocatedTnodeList);
864                 dev->allocatedTnodeList = tmp;
865                 
866         }
867         
868         dev->freeTnodes = NULL;
869         dev->nFreeTnodes = 0;
870 }
871
872 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
873 {
874         dev->allocatedTnodeList = NULL;
875         dev->freeTnodes = NULL;
876         dev->nFreeTnodes = 0;
877         dev->nTnodesCreated = 0;
878
879 }
880
881 #if 0
882 void yaffs_TnodeTest(yaffs_Device *dev)
883 {
884
885         int i;
886         int j;
887         yaffs_Tnode *tn[1000];
888         
889         YINFO("Testing TNodes");
890         
891         for(j = 0; j < 50; j++)
892         {
893                 for(i = 0; i < 1000; i++)
894                 {
895                         tn[i] = yaffs_GetTnode(dev);
896                         if(!tn[i])
897                         {
898                                 YALERT("Getting tnode failed");
899                         }
900                 }
901                 for(i = 0; i < 1000; i+=3)
902                 {
903                         yaffs_FreeTnode(dev,tn[i]);
904                         tn[i] = NULL;
905                 }
906                 
907         }
908 }
909 #endif
910
911
912 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
913
914 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
915 // The look up tree is represented by the top tnode and the number of topLevel
916 // in the tree. 0 means only the level 0 tnode is in the tree.
917
918
919 // FindLevel0Tnode finds the level 0 tnode, if one exists.
920 // Used when reading.....
921 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
922 {
923         
924         yaffs_Tnode *tn = fStruct->top;
925         __u32 i;
926         int requiredTallness;   
927         int level = fStruct->topLevel;
928         
929         // Check sane level and chunk Id
930         if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
931         {
932 //              char str[50];
933 //              sprintf(str,"Bad level %d",level);
934 //              YALERT(str);
935                 return NULL;
936         }
937         
938         if(chunkId > YAFFS_MAX_CHUNK_ID)
939         {
940 //              char str[50];
941 //              sprintf(str,"Bad chunkId %d",chunkId);
942 //              YALERT(str);
943                 return NULL;
944         }
945
946         // First check we're tall enough (ie enough topLevel)
947         
948         i = chunkId >> (/*dev->chunkGroupBits  + */YAFFS_TNODES_LEVEL0_BITS);
949         requiredTallness = 0;
950         while(i)
951         {
952                 i >>= YAFFS_TNODES_INTERNAL_BITS;
953                 requiredTallness++;
954         }
955         
956         
957         if(requiredTallness > fStruct->topLevel)
958         {
959                 // Not tall enough, so we can't find it, return NULL.
960                 return NULL;
961         }
962                 
963         
964         // Traverse down to level 0
965         while (level > 0 && tn)
966         {
967             tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
968                                YAFFS_TNODES_INTERNAL_MASK]; 
969                 level--;
970         
971         }
972         
973         return tn;              
974 }
975
976 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
977 // This happens in two steps:
978 //  1. If the tree isn't tall enough, then make it taller.
979 //  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
980 //
981 // Used when modifying the tree.
982 //
983 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
984 {
985         
986         yaffs_Tnode *tn; 
987         
988         int requiredTallness;
989         int i;
990         int l;
991         
992         __u32 x;
993                 
994         
995         //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
996         
997         // Check sane level and page Id
998         if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
999         {
1000 //              char str[50];
1001 //              sprintf(str,"Bad level %d",fStruct->topLevel);
1002 //              YALERT(str);
1003                 return NULL;
1004         }
1005         
1006         if(chunkId > YAFFS_MAX_CHUNK_ID)
1007         {
1008 //              char str[50];
1009 //              sprintf(str,"Bad chunkId %d",chunkId);
1010 //              YALERT(str);
1011                 return NULL;
1012         }
1013         
1014         // First check we're tall enough (ie enough topLevel)
1015         
1016         x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
1017         requiredTallness = 0;
1018         while(x)
1019         {
1020                 x >>= YAFFS_TNODES_INTERNAL_BITS;
1021                 requiredTallness++;
1022         }
1023         
1024         //T((TSTR(" required=%d"),requiredTallness));
1025         
1026         
1027         if(requiredTallness > fStruct->topLevel)
1028         {
1029                 // Not tall enough,gotta make the tree taller
1030                 for(i = fStruct->topLevel; i < requiredTallness; i++)
1031                 {
1032                         //T((TSTR(" add new top")));
1033                         
1034                         tn = yaffs_GetTnode(dev);
1035                         
1036                         if(tn)
1037                         {
1038                                 tn->internal[0] = fStruct->top;
1039                                 fStruct->top = tn;
1040                         }
1041                         else
1042                         {
1043                                         T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
1044                         }
1045                 }
1046                 
1047                 fStruct->topLevel = requiredTallness;
1048         }
1049         
1050         
1051         // Traverse down to level 0, adding anything we need
1052         
1053         l = fStruct->topLevel;
1054         tn = fStruct->top;
1055         while (l > 0 && tn)
1056         {
1057                 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
1058                                YAFFS_TNODES_INTERNAL_MASK;
1059                                
1060                 //T((TSTR(" [%d:%d]"),l,i));
1061                 
1062             if(!tn->internal[x])
1063             {
1064                 //T((TSTR(" added")));
1065                 
1066                 tn->internal[x] = yaffs_GetTnode(dev);
1067             }
1068             
1069             tn =        tn->internal[x];
1070                 l--;
1071         
1072         }
1073         
1074         //TSTR(TENDSTR)));
1075         
1076         return tn;              
1077 }
1078
1079
1080 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
1081 {
1082         int j;
1083         
1084                                         
1085         for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
1086         {
1087                 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
1088                 {
1089                         yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
1090                         if(yaffs_TagsMatch(tags,objectId,chunkInInode))
1091                         {
1092                                 // found it;
1093                                 return theChunk;
1094                                         
1095                         }
1096                 }
1097                 theChunk++;
1098         }
1099         return -1;
1100 }
1101
1102 // DeleteWorker scans backwards through the tnode tree and deletes all the
1103 // chunks and tnodes in the file
1104 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1105
1106 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1107 {
1108         int i;
1109         int chunkInInode;
1110         int theChunk;
1111         yaffs_ExtendedTags tags;
1112         int foundChunk;
1113         yaffs_Device *dev = in->myDev;
1114
1115         int allDone = 1;
1116         
1117         
1118         if(tn)
1119         {
1120                 if(level > 0)
1121                 {
1122                 
1123                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1124                         {
1125                             if(tn->internal[i])
1126                         {
1127                                         if(limit && (*limit) < 0)
1128                                         {
1129                                                 allDone = 0;
1130                                         }
1131                                         else
1132                                         {
1133                                                 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1134                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1135                                         }
1136                                         if(allDone)
1137                                         {
1138                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1139                                         tn->internal[i] = NULL;
1140                                         }
1141                             }
1142                     
1143                         }
1144                         return (allDone) ? 1 : 0;
1145                 }
1146                 else if(level == 0)
1147                 {
1148                         int hitLimit = 0;
1149                         
1150                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1151                         {
1152                             if(tn->level0[i])
1153                         {
1154                                         
1155                                         chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1156                                         
1157                                         theChunk =  tn->level0[i] << dev->chunkGroupBits;
1158                                         
1159                                         foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1160                                         
1161                                         if(foundChunk > 0)
1162                                         {
1163                                                 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1164                                                 in->nDataChunks--;
1165                                                 if(limit)
1166                                                 { 
1167                                                         *limit = *limit-1;
1168                                                         if(*limit <= 0) 
1169                                                         { 
1170                                                                 hitLimit = 1;
1171                                                         }
1172                                                 }
1173                                         
1174                                         }
1175                                         
1176                                 tn->level0[i] = 0;
1177                             }
1178                     
1179                         }
1180                         return (i < 0) ? 1 : 0;
1181
1182                         
1183                 }
1184                 
1185         }
1186         
1187         return 1;
1188         
1189 }
1190
1191
1192 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1193 {
1194
1195         yaffs_BlockInfo *theBlock;                                      
1196         
1197         T(YAFFS_TRACE_DELETION,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1198                                                 
1199         theBlock =      yaffs_GetBlockInfo(dev,  chunk/dev->nChunksPerBlock);
1200         if(theBlock)
1201         {
1202                 theBlock->softDeletions++;
1203         }
1204 }
1205
1206 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1207 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1208 // of the tnode.
1209 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1210 //
1211 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1212 {
1213         int i;
1214         int theChunk;
1215         int allDone = 1;
1216         yaffs_Device *dev = in->myDev;
1217         
1218         
1219         if(tn)
1220         {
1221                 if(level > 0)
1222                 {
1223                 
1224                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1225                         {
1226                             if(tn->internal[i])
1227                         {
1228                                                 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1229                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1230                                         if(allDone)
1231                                         {
1232                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1233                                         tn->internal[i] = NULL;
1234                                         }
1235                                         else
1236                                         {
1237                                                 //Hoosterman... how could this happen.
1238                                         }                           
1239                                 }                   
1240                         }
1241                         return (allDone) ? 1 : 0;
1242                 }
1243                 else if(level == 0)
1244                 {
1245                         
1246                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1247                         {
1248                             if(tn->level0[i])
1249                         {
1250                                         // Note this does not find the real chunk, only the chunk group.
1251                                         // We make an assumption that a chunk group is niot larger than a block.
1252                                         theChunk =  (tn->level0[i] << dev->chunkGroupBits);
1253                                         
1254                                         yaffs_SoftDeleteChunk(dev,theChunk);
1255                                 tn->level0[i] = 0;
1256                             }
1257                     
1258                         }
1259                         return 1;
1260                         
1261                 }
1262                 
1263         }
1264         
1265         return 1;
1266                 
1267 }
1268
1269
1270
1271 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1272 {
1273         if(obj->deleted &&
1274            obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1275            !obj->softDeleted)
1276         {
1277                 if(obj->nDataChunks <= 0)
1278                 {
1279                                 // Empty file with no duplicate object headers, just delete it immediately
1280                                 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1281                                 obj->variant.fileVariant.top = NULL;
1282                                 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1283                                 yaffs_DoGenericObjectDeletion(obj);     
1284                 }
1285                 else
1286                 {
1287                         yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1288                         obj->softDeleted = 1;
1289                 }
1290         }
1291 }
1292
1293
1294
1295
1296
1297 // Pruning removes any part of the file structure tree that is beyond the
1298 // bounds of the file (ie that does not point to chunks).
1299 //
1300 // A file should only get pruned when its size is reduced.
1301 //
1302 // Before pruning, the chunks must be pulled from the tree and the
1303 // level 0 tnode entries must be zeroed out.
1304 // Could also use this for file deletion, but that's probably better handled
1305 // by a special case.
1306
1307 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1308
1309 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1310 {
1311         int i;
1312         int hasData;
1313         
1314         if(tn)
1315         {
1316                 hasData = 0;
1317                 
1318                 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1319                 {
1320                     if(tn->internal[i] && level > 0)
1321                     {
1322                         tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1323                     }
1324                     
1325                     if(tn->internal[i])
1326                     {
1327                         hasData++;
1328                         }
1329                 }
1330                 
1331                 if(hasData == 0 && del0)
1332                 {
1333                         // Free and return NULL
1334                         
1335                         yaffs_FreeTnode(dev,tn);
1336                         tn = NULL;
1337                 }
1338                 
1339         }
1340
1341         return tn;
1342         
1343 }
1344
1345 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1346 {
1347         int i;
1348         int hasData;
1349         int done = 0;
1350         yaffs_Tnode *tn;
1351         
1352         if(fStruct->topLevel > 0)
1353         {
1354                 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1355                 
1356                 // Now we have a tree with all the non-zero branches NULL but the height
1357                 // is the same as it was.
1358                 // Let's see if we can trim internal tnodes to shorten the tree.
1359                 // We can do this if only the 0th element in the tnode is in use 
1360                 // (ie all the non-zero are NULL)
1361                 
1362                 while(fStruct->topLevel && !done)
1363                 {
1364                         tn = fStruct->top;
1365                         
1366                         hasData = 0;
1367                         for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1368                         {
1369                                 if(tn->internal[i])
1370                         {
1371                                 hasData++;
1372                                 }
1373                         }
1374                         
1375                         if(!hasData)
1376                         {
1377                                 fStruct->top = tn->internal[0];
1378                                 fStruct->topLevel--;
1379                                 yaffs_FreeTnode(dev,tn);
1380                         }
1381                         else
1382                         {
1383                                 done = 1;
1384                         }
1385                 }
1386         }
1387         
1388         return YAFFS_OK;
1389 }
1390
1391
1392
1393
1394
1395 /////////////////////// End of File Structure functions. /////////////////
1396
1397 // yaffs_CreateFreeObjects creates a bunch more objects and
1398 // adds them to the object free list.
1399 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1400 {
1401     int i;
1402     yaffs_Object *newObjects;
1403     yaffs_ObjectList *list;
1404     
1405     if(nObjects < 1) return YAFFS_OK;
1406    
1407         // make these things
1408         
1409     newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1410    
1411     if (!newObjects)
1412     {
1413                 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1414                 return YAFFS_FAIL;
1415     }
1416     
1417     // Hook them into the free list
1418     for(i = 0; i < nObjects - 1; i++)
1419     {
1420         newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1421     }
1422         
1423         newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1424         dev->freeObjects = newObjects;
1425         dev->nFreeObjects+= nObjects;
1426         dev->nObjectsCreated+= nObjects;
1427         
1428         // Now add this bunch of Objects to a list for freeing up.
1429         
1430         list = YMALLOC(sizeof(yaffs_ObjectList));
1431         if(!list)
1432         {
1433                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1434         }
1435         else
1436         {
1437                 list->objects = newObjects;
1438                 list->next = dev->allocatedObjectList;
1439                 dev->allocatedObjectList = list;
1440         }
1441         
1442         
1443         
1444         return YAFFS_OK;
1445 }
1446
1447
1448 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1449 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1450 {
1451         yaffs_Object *tn = NULL;
1452         
1453         // If there are none left make more
1454         if(!dev->freeObjects)
1455         {
1456                 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1457         }
1458         
1459         if(dev->freeObjects)
1460         {
1461                 tn = dev->freeObjects;
1462                 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1463                 dev->nFreeObjects--;
1464                 
1465                 // Now sweeten it up...
1466         
1467                 memset(tn,0,sizeof(yaffs_Object));
1468                 tn->myDev = dev;
1469                 tn->chunkId = -1;
1470                 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1471                 INIT_LIST_HEAD(&(tn->hardLinks));
1472                 INIT_LIST_HEAD(&(tn->hashLink));
1473                 INIT_LIST_HEAD(&tn->siblings);
1474                 
1475                 // Add it to the lost and found directory.
1476                 // NB Can't put root or lostNFound in lostNFound so
1477                 // check if lostNFound exists first
1478                 if(dev->lostNFoundDir)
1479                 {
1480                         yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);      
1481                 }
1482         }
1483         
1484
1485         return tn;
1486 }
1487
1488 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1489 {
1490
1491         yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);              
1492         if(obj)
1493         {
1494                 obj->fake = 1;                  // it is fake so it has no NAND presence...
1495                 obj->renameAllowed= 0;  // ... and we're not allowed to rename it...
1496                 obj->unlinkAllowed= 0;  // ... or unlink it
1497                 obj->deleted = 0;
1498                 obj->unlinked = 0;
1499                 obj->st_mode = mode;
1500                 obj->myDev = dev;
1501                 obj->chunkId = 0; // Not a valid chunk.
1502         }
1503         
1504         return obj;
1505         
1506 }
1507
1508
1509 static void yaffs_UnhashObject(yaffs_Object *tn)
1510 {
1511         int bucket;
1512         yaffs_Device *dev = tn->myDev;
1513         
1514         
1515         // If it is still linked into the bucket list, free from the list
1516         if(!list_empty(&tn->hashLink))
1517         {
1518                 list_del_init(&tn->hashLink);
1519                 bucket =  yaffs_HashFunction(tn->objectId);
1520                 dev->objectBucket[bucket].count--;
1521         }
1522         
1523 }
1524
1525
1526 // FreeObject frees up a Object and puts it back on the free list
1527 static void yaffs_FreeObject(yaffs_Object *tn)
1528 {
1529
1530         yaffs_Device *dev = tn->myDev;
1531         
1532 #ifdef  __KERNEL__
1533         if(tn->myInode)
1534         {
1535                 // We're still hooked up to a cached inode.
1536                 // Don't delete now, but mark for later deletion
1537                 tn->deferedFree = 1;
1538                 return;
1539         }
1540 #endif
1541         
1542         yaffs_UnhashObject(tn);
1543         
1544         // Link into the free list.
1545         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1546         dev->freeObjects = tn;
1547         dev->nFreeObjects++;
1548 }
1549
1550
1551
1552 #ifdef __KERNEL__
1553
1554 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1555 {
1556         if(obj->deferedFree)
1557         {
1558            yaffs_FreeObject(obj);
1559         }
1560 }
1561
1562 #endif
1563
1564
1565
1566 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1567 {
1568         // Free the list of allocated Objects
1569         
1570         yaffs_ObjectList *tmp;
1571         
1572         while( dev->allocatedObjectList)
1573         {
1574                 tmp =  dev->allocatedObjectList->next;
1575                 YFREE(dev->allocatedObjectList->objects);
1576                 YFREE(dev->allocatedObjectList);
1577                 
1578                 dev->allocatedObjectList =  tmp;
1579         }
1580         
1581         dev->freeObjects = NULL;
1582         dev->nFreeObjects = 0;
1583 }
1584
1585 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1586 {
1587         int i;
1588         
1589         dev->allocatedObjectList = NULL;
1590         dev->freeObjects = NULL;
1591         dev->nFreeObjects = 0;
1592         
1593         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1594         {
1595                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1596                 dev->objectBucket[i].count = 0; 
1597         }
1598
1599 }
1600
1601
1602
1603
1604
1605
1606 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1607 {
1608         static int x = 0;
1609         int i;
1610         int l = 999;
1611         int lowest = 999999;
1612
1613                 
1614         // First let's see if we can find one that's empty.
1615         
1616         for(i = 0; i < 10 && lowest > 0; i++)
1617          {
1618                 x++;
1619                 x %=  YAFFS_NOBJECT_BUCKETS;
1620                 if(dev->objectBucket[x].count < lowest)
1621                 {
1622                         lowest = dev->objectBucket[x].count;
1623                         l = x;
1624                 }
1625                 
1626         }
1627         
1628         // If we didn't find an empty list, then try
1629         // looking a bit further for a short one
1630         
1631         for(i = 0; i < 10 && lowest > 3; i++)
1632          {
1633                 x++;
1634                 x %=  YAFFS_NOBJECT_BUCKETS;
1635                 if(dev->objectBucket[x].count < lowest)
1636                 {
1637                         lowest = dev->objectBucket[x].count;
1638                         l = x;
1639                 }
1640                 
1641         }
1642         
1643         return l;
1644 }
1645
1646 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1647 {
1648         int bucket = yaffs_FindNiceObjectBucket(dev);
1649         
1650         // Now find an object value that has not already been taken
1651         // by scanning the list.
1652         
1653         int found = 0;
1654         struct list_head *i;
1655         
1656         __u32 n = (__u32)bucket;
1657
1658         //yaffs_CheckObjectHashSanity();        
1659         
1660         while(!found)
1661         {
1662                 found = 1;
1663                 n +=  YAFFS_NOBJECT_BUCKETS;
1664                 if(1 ||dev->objectBucket[bucket].count > 0)
1665                 {
1666                         list_for_each(i,&dev->objectBucket[bucket].list)
1667                         {
1668                                 // If there is already one in the list
1669                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1670                                 {
1671                                         found = 0;
1672                                 }
1673                         }
1674                 }
1675         }
1676         
1677         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1678         
1679         return n;       
1680 }
1681
1682 void yaffs_HashObject(yaffs_Object *in)
1683 {
1684         int bucket = yaffs_HashFunction(in->objectId);
1685         yaffs_Device *dev = in->myDev;
1686         
1687         if(!list_empty(&in->hashLink))
1688         {
1689                 //YINFO("!!!");
1690         }
1691
1692         
1693         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1694         dev->objectBucket[bucket].count++;
1695
1696 }
1697
1698 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1699 {
1700         int bucket = yaffs_HashFunction(number);
1701         struct list_head *i;
1702         yaffs_Object *in;
1703         
1704         list_for_each(i,&dev->objectBucket[bucket].list)
1705         {
1706                 // Look if it is in the list
1707                 if(i)
1708                 {
1709                         in = list_entry(i, yaffs_Object,hashLink);
1710                         if(in->objectId == number)
1711                         {
1712                                 return in;
1713                         }
1714                 }
1715         }
1716         
1717         return NULL;
1718 }
1719
1720
1721
1722 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1723 {
1724                 
1725         yaffs_Object *theObject;
1726
1727         if(number < 0)
1728         {
1729                 number = yaffs_CreateNewObjectNumber(dev);
1730         }
1731         
1732         theObject = yaffs_AllocateEmptyObject(dev);
1733         
1734         if(theObject)
1735         {
1736                 theObject->fake = 0;
1737                 theObject->renameAllowed = 1;
1738                 theObject->unlinkAllowed = 1;
1739                 theObject->objectId = number;
1740                 yaffs_HashObject(theObject);
1741                 theObject->variantType = type;
1742 #ifdef CONFIG_YAFFS_WINCE
1743                 yfsd_WinFileTimeNow(theObject->win_atime);
1744                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1745                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1746
1747 #else
1748
1749                 theObject->st_atime = theObject->st_mtime = theObject->st_ctime = Y_CURRENT_TIME;               
1750 #endif
1751                 switch(type)
1752                 {
1753                         case YAFFS_OBJECT_TYPE_FILE: 
1754                                 theObject->variant.fileVariant.fileSize = 0;
1755                                 theObject->variant.fileVariant.scannedFileSize = 0;
1756                                 theObject->variant.fileVariant.topLevel = 0;
1757                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1758                                 break;
1759                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1760                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1761                                 break;
1762                         case YAFFS_OBJECT_TYPE_SYMLINK:
1763                                 // No action required
1764                                 break;
1765                         case YAFFS_OBJECT_TYPE_HARDLINK:
1766                                 // No action required
1767                                 break;
1768                         case YAFFS_OBJECT_TYPE_SPECIAL:
1769                                 // No action required
1770                                 break;
1771                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1772                                 // todo this should not happen
1773                                 break;
1774                 }
1775         }
1776         
1777         return theObject;
1778 }
1779
1780 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1781 {
1782         yaffs_Object *theObject = NULL;
1783         
1784         if(number > 0)
1785         {
1786                 theObject = yaffs_FindObjectByNumber(dev,number);
1787         }
1788         
1789         if(!theObject)
1790         {
1791                 theObject = yaffs_CreateNewObject(dev,number,type);
1792         }
1793         
1794         return theObject;
1795
1796 }
1797
1798 YCHAR *yaffs_CloneString(const YCHAR *str)
1799 {
1800         YCHAR *newStr = NULL;
1801         
1802         if(str && *str)
1803         {
1804                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1805                 yaffs_strcpy(newStr,str);
1806         }
1807
1808         return newStr;
1809         
1810 }
1811
1812 //
1813 // Mknod (create) a new object.
1814 // equivalentObject only has meaning for a hard link;
1815 // aliasString only has meaning for a sumlink.
1816 // rdev only has meaning for devices (a subset of special objects)
1817 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1818                                                                  yaffs_Object *parent,
1819                                                                  const YCHAR *name, 
1820                                                                  __u32 mode,
1821                                                                  __u32 uid,
1822                                                                  __u32 gid,
1823                                                                  yaffs_Object *equivalentObject,
1824                                                                  const YCHAR *aliasString,
1825                                                                  __u32 rdev)
1826 {
1827         yaffs_Object *in;
1828
1829         yaffs_Device *dev = parent->myDev;
1830         
1831         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1832         if(yaffs_FindObjectByName(parent,name))
1833         {
1834                 return NULL;
1835         }
1836         
1837         in = yaffs_CreateNewObject(dev,-1,type);
1838         
1839         if(in)
1840         {
1841                 in->chunkId = -1;
1842                 in->valid = 1;
1843                 in->variantType = type;
1844
1845                 in->st_mode  = mode;
1846                 
1847 #ifdef CONFIG_YAFFS_WINCE
1848                 yfsd_WinFileTimeNow(in->win_atime);
1849                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1850                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1851                 
1852 #else
1853                 in->st_atime = in->st_mtime = in->st_ctime = Y_CURRENT_TIME;
1854
1855                 in->st_rdev  = rdev;
1856                 in->st_uid   = uid;
1857                 in->st_gid   = gid;
1858 #endif          
1859                 in->nDataChunks = 0;
1860
1861                 yaffs_SetObjectName(in,name);
1862                 in->dirty = 1;
1863                 
1864                 yaffs_AddObjectToDirectory(parent,in);
1865                 
1866                 in->myDev = parent->myDev;
1867                 
1868                                 
1869                 switch(type)
1870                 {
1871                         case YAFFS_OBJECT_TYPE_SYMLINK:
1872                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1873                                 break;
1874                         case YAFFS_OBJECT_TYPE_HARDLINK:
1875                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1876                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1877                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1878                                 break;
1879                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1880                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1881                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1882                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1883                                 break;
1884                 }
1885
1886                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1887                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1888                 {
1889                         // Could not create the object header, fail the creation
1890                         yaffs_DestroyObject(in);
1891                         in = NULL;
1892                 }
1893
1894         }
1895         
1896         return in;
1897 }
1898
1899 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1900 {
1901         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1902 }
1903
1904 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1905 {
1906         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1907 }
1908
1909 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1910 {
1911         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1912 }
1913
1914 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1915 {
1916         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1917 }
1918
1919 // NB yaffs_Link returns the object id of the equivalent object.
1920 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1921 {
1922         // Get the real object in case we were fed a hard link as an equivalent object
1923         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1924         
1925         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1926         {
1927                 return equivalentObject;
1928         }
1929         else
1930         {
1931                 return NULL;
1932         }
1933         
1934 }
1935
1936
1937 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1938 {
1939         int unlinkOp;
1940         int deleteOp;
1941
1942         if(newDir == NULL)
1943         {
1944                 newDir = obj->parent; // use the old directory
1945         }
1946
1947         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1948         if(obj->myDev->isYaffs2)
1949         {
1950                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1951         }
1952         else
1953         {
1954                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1955         }
1956
1957         deleteOp = (newDir == obj->myDev->deletedDir);
1958         
1959         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1960         // duplicate names are allowed.
1961         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1962         if( (unlinkOp|| 
1963                  deleteOp ||
1964                  force || 
1965                  !yaffs_FindObjectByName(newDir,newName))  &&
1966              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1967         {
1968                 yaffs_SetObjectName(obj,newName);
1969                 obj->dirty = 1;
1970                 
1971                 yaffs_AddObjectToDirectory(newDir,obj);
1972                 
1973                 if(unlinkOp) obj->unlinked = 1;
1974                 
1975                 // If it is a dletion then we mark it as a shrink for gc purposes.
1976                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1977                 {
1978                         return YAFFS_OK;
1979                 }
1980         }
1981         
1982         return YAFFS_FAIL;
1983 }
1984
1985
1986
1987 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1988 {
1989         yaffs_Object *obj;
1990         int force = 0;
1991         
1992 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1993         // Special case for case insemsitive systems (eg. WinCE).
1994         // While look-up is case insensitive, the name isn't.
1995         // THerefore we might want to change x.txt to X.txt
1996         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1997         {
1998                 force = 1;
1999         }       
2000 #endif
2001         
2002         obj = yaffs_FindObjectByName(oldDir,oldName);
2003         if(obj && obj->renameAllowed)
2004         {
2005                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
2006         }
2007         return YAFFS_FAIL;
2008 }
2009
2010
2011
2012 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
2013 {
2014         // Scan the buckets and check that the lists 
2015         // have as many members as the count says there are
2016         int bucket;
2017         int countEm;
2018         struct list_head *j;
2019         int ok = YAFFS_OK;
2020         
2021         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2022         {
2023                 countEm = 0;
2024                 
2025                 list_for_each(j,&dev->objectBucket[bucket].list)
2026                 {
2027                         countEm++;
2028                 }
2029                 
2030                 if(countEm != dev->objectBucket[bucket].count)
2031                 {
2032                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2033                         ok = YAFFS_FAIL;
2034                 }
2035         }
2036
2037         return ok;
2038 }
2039
2040 #if 0
2041 void yaffs_ObjectTest(yaffs_Device *dev)
2042 {
2043         yaffs_Object *in[1000];
2044         int inNo[1000];
2045         yaffs_Object *inold[1000];
2046         int i;
2047         int j;
2048         
2049         memset(in,0,1000*sizeof(yaffs_Object *));
2050         memset(inold,0,1000*sizeof(yaffs_Object *));
2051         
2052         yaffs_CheckObjectHashSanity(dev);
2053         
2054         for(j = 0; j < 10; j++)
2055         {
2056                 //T(("%d\n",j));
2057                 
2058                 for(i = 0; i < 1000; i++)
2059                 {
2060                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2061                         if(!in[i])
2062                         {
2063                                 YINFO("No more inodes");
2064                         }
2065                         else
2066                         {
2067                                 inNo[i] = in[i]->objectId;
2068                         }
2069                 }
2070                 
2071                 for(i = 0; i < 1000; i++)
2072                 {
2073                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2074                         {
2075                                 //T(("Differnce in look up test\n"));
2076                         }
2077                         else
2078                         {
2079                                 // T(("Look up ok\n"));
2080                         }
2081                 }
2082                 
2083                 yaffs_CheckObjectHashSanity(dev);
2084         
2085                 for(i = 0; i < 1000; i+=3)
2086                 {
2087                         yaffs_FreeObject(in[i]);        
2088                         in[i] = NULL;
2089                 }
2090                 
2091         
2092                 yaffs_CheckObjectHashSanity(dev);
2093         }
2094                 
2095 }
2096
2097 #endif
2098
2099 /////////////////////////// Block Management and Page Allocation ///////////////////
2100
2101
2102 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2103 {
2104         dev->allocationBlock = -1; // force it to get a new one
2105         //Todo we're assuming the malloc will pass.
2106         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2107         // Set up dynamic blockinfo stuff.
2108         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2109         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2110         if(dev->blockInfo && dev->chunkBits)
2111         {
2112                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2113                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2114                 return YAFFS_OK;
2115         }
2116         
2117         return YAFFS_FAIL;
2118         
2119 }
2120
2121 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2122 {
2123         YFREE(dev->blockInfo);
2124         dev->blockInfo = NULL;
2125         YFREE(dev->chunkBits);
2126         dev->chunkBits = NULL;
2127 }
2128
2129
2130 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2131 {
2132         int i;
2133         __u32 seq;
2134         yaffs_BlockInfo *b;
2135         
2136         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2137         
2138         if(!bi->hasShrinkHeader) return 1; // can gc
2139
2140
2141         // Find the oldest dirty sequence number if we don't know it and save it
2142         // so we don't have to keep recomputing it.
2143         if(!dev->oldestDirtySequence)
2144         {
2145                 seq = dev->sequenceNumber;
2146
2147                 for(i = dev->startBlock; i <= dev->endBlock; i++)
2148                 {
2149                         b = yaffs_GetBlockInfo(dev,i);
2150                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2151                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2152                         b->sequenceNumber < seq)
2153                         {
2154                                 seq = b->sequenceNumber;
2155                         }
2156                 }
2157                 dev->oldestDirtySequence = seq;
2158         }
2159
2160
2161         // Can't do gc of this block if there are any blocks older than this one that have
2162         // discarded pages.
2163         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2164         
2165         
2166         return 1;
2167
2168 }
2169
2170 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2171 // for garbage collection.
2172 //
2173
2174
2175
2176 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2177 {
2178
2179         int b = dev->currentDirtyChecker;
2180         
2181         int i;
2182         int iterations;
2183         int dirtiest = -1;
2184         int pagesInUse; 
2185         yaffs_BlockInfo *bi;
2186         static int  nonAggressiveSkip = 0;
2187
2188         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2189         // search harder.
2190         // else (we're doing a leasurely gc), then we only bother to do this if the
2191         // block has only a few pages in use.
2192         
2193
2194         nonAggressiveSkip--;
2195
2196         if(!aggressive &&(nonAggressiveSkip > 0))
2197         {
2198                 return -1;
2199         }
2200
2201         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2202         if(aggressive)
2203         {
2204                 iterations = dev->endBlock - dev->startBlock + 1;
2205         }
2206         else
2207         {
2208                 iterations = dev->endBlock - dev->startBlock + 1;
2209                 iterations = iterations / 16; 
2210                 if(iterations > 200)
2211                 {
2212                         iterations = 200;
2213                 }
2214         }
2215         
2216         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2217         {
2218                 b++;
2219                 if ( b < dev->startBlock || b > dev->endBlock)
2220                 {
2221                         b =  dev->startBlock;
2222                 }
2223
2224                 if(b < dev->startBlock || b > dev->endBlock)
2225                 {
2226                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2227                         YBUG();
2228                 }
2229                 
2230                 bi = yaffs_GetBlockInfo(dev,b);
2231                 
2232                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2233                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2234                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2235                 {
2236                         dirtiest = b;
2237                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2238                 }
2239         }
2240         
2241         dev->currentDirtyChecker = b;
2242         
2243         if(dirtiest > 0)
2244         {
2245                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2246         }
2247         
2248         dev->oldestDirtySequence = 0; // clear this
2249         
2250         if(dirtiest > 0)
2251         {
2252                 nonAggressiveSkip = 4;
2253         }
2254
2255         return dirtiest;
2256 }
2257
2258
2259 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2260 {
2261         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2262         
2263         int erasedOk = 0;
2264         
2265         // If the block is still healthy erase it and mark as clean.
2266         // If the block has had a data failure, then retire it.
2267         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2268
2269         if(!bi->needsRetiring)
2270         {
2271                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2272                 if(!erasedOk)
2273                 {
2274                         dev->nErasureFailures++;
2275                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2276                 }
2277         }
2278
2279         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2280         {
2281                         int i;
2282                         for(i = 0; i < dev->nChunksPerBlock; i++)
2283                         {
2284                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2285                                         {
2286                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2287                                         }
2288                         }
2289         }
2290         
2291         if( erasedOk )
2292         {
2293                 // Clean it up...
2294                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2295                 dev->nErasedBlocks++;
2296                 bi->pagesInUse = 0;
2297                 bi->softDeletions = 0;
2298                 bi->hasShrinkHeader=0;
2299                 yaffs_ClearChunkBits(dev,blockNo);
2300         
2301                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2302         }
2303         else
2304         {
2305                 yaffs_RetireBlock(dev,blockNo);
2306                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2307         }
2308 }
2309
2310 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2311 {
2312         int i,j;
2313         yaffs_BlockInfo *bi;
2314         
2315         for(i= dev->startBlock; i <=dev->endBlock; i++)
2316         {
2317                 bi = yaffs_GetBlockInfo(dev,i);
2318                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2319                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2320                 
2321                 for(j = 0; j < dev->nChunksPerBlock; j++)
2322                 {
2323                         if(yaffs_CheckChunkBit(dev,i,j))
2324                         {
2325                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2326
2327                         }
2328                 }
2329                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2330
2331         }
2332 }
2333
2334
2335 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2336 {
2337         int i;
2338         
2339         yaffs_BlockInfo *bi;
2340         
2341 #if 0
2342         static int j = 0;
2343         j++;
2344         if(j < 0 || j > 100)
2345         {
2346                 j = 0;
2347                 yaffs_DumpBlockStats(dev);
2348         }
2349         
2350 #endif
2351         
2352         if(dev->nErasedBlocks < 1)
2353         {
2354                 // Hoosterman we've got a problem.
2355                 // Can't get space to gc
2356                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2357
2358                 return -1;
2359         }
2360         
2361         // Find an empty block.
2362         
2363         for(i = dev->startBlock; i <= dev->endBlock; i++)
2364         {
2365                 dev->allocationBlockFinder++;
2366                 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock) 
2367                 {
2368                         dev->allocationBlockFinder = dev->startBlock;
2369                 }
2370                 
2371                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2372
2373                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2374                 {
2375                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2376                         dev->sequenceNumber++;
2377                         bi->sequenceNumber = dev->sequenceNumber;
2378                         dev->nErasedBlocks--;           
2379                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber));   
2380                         return dev->allocationBlockFinder;
2381                 }
2382         }
2383                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been one" TENDSTR)));
2384
2385         
2386         return -1;      
2387 }
2388
2389
2390
2391 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2392 {
2393         int retVal;
2394         yaffs_BlockInfo *bi;
2395         
2396         if(dev->allocationBlock < 0)
2397         {
2398                 // Get next block to allocate off
2399                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2400                 dev->allocationPage = 0;
2401         }
2402         
2403         if(!useReserve &&  dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2404         {
2405                 // Not enough space to allocate unless we're allowed to use the reserve.
2406                 return -1;
2407         }
2408
2409         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2410         {
2411                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2412         }
2413
2414         
2415         // Next page please....
2416         if(dev->allocationBlock >= 0)
2417         {
2418                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2419                 
2420                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2421                                   dev->allocationPage;
2422                 bi->pagesInUse++;
2423                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2424
2425                 dev->allocationPage++;
2426                 
2427                 dev->nFreeChunks--;
2428                 
2429                 // If the block is full set the state to full
2430                 if(dev->allocationPage >= dev->nChunksPerBlock)
2431                 {
2432                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2433                         dev->allocationBlock = -1;
2434                 }
2435
2436
2437                 return retVal;
2438                 
2439         }
2440         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2441
2442         return -1;      
2443 }
2444
2445
2446 // To determine if we have enough space we just look at the 
2447 // number of erased blocks.
2448 // The cache is allowed to use reserved blocks.
2449
2450 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2451 {
2452         return (dev->nErasedBlocks >= dev->nReservedBlocks);
2453 }
2454
2455
2456 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2457 {
2458                 int n;
2459
2460                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2461
2462                 if(dev->allocationBlock> 0)
2463                 {
2464                         n += (dev->nChunksPerBlock - dev->allocationPage);
2465                 }
2466
2467                 return n;
2468
2469 }
2470
2471 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2472 {
2473         int oldChunk;
2474         int newChunk;
2475         int chunkInBlock;
2476         int markNAND;
2477         int retVal = YAFFS_OK;
2478         int cleanups = 0;
2479         int i;
2480
2481         int chunksBefore = yaffs_GetErasedChunks(dev);
2482         int chunksAfter;
2483
2484         yaffs_ExtendedTags  tags;
2485         
2486         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2487         
2488         yaffs_Object *object;
2489         
2490         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2491
2492         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2493         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2494
2495         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2496
2497
2498         if(!yaffs_StillSomeChunkBits(dev,block))
2499         {
2500                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2501                 yaffs_BlockBecameDirty(dev,block);
2502         }
2503         else
2504         {
2505
2506                         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2507
2508         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2509             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2510             chunkInBlock++, oldChunk++ )
2511         {
2512                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2513                 {
2514                         
2515                         // This page is in use and might need to be copied off
2516                         
2517                         markNAND = 1;
2518                         
2519                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2520                         
2521                         yaffs_InitialiseTags(&tags);
2522                         
2523                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2524
2525                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2526                         
2527                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2528                         
2529                         if(!object)
2530                         {
2531                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2532                         }
2533                         
2534                         if(object && object->deleted && tags.chunkId != 0)
2535                         {
2536                                 // Data chunk in a deleted file, throw it away
2537                                 // It's a deleted data chunk,
2538                                 // No need to copy this, just forget about it and fix up the
2539                                 // object.
2540                                 
2541                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2542                                 object->nDataChunks--;
2543                                 
2544                                 if(object->nDataChunks <= 0)
2545                                 {
2546                                         // remeber to clean up the object
2547                                         dev->gcCleanupList[cleanups] = tags.objectId;
2548                                         cleanups++;
2549                                 }
2550                                 markNAND = 0;
2551                         }
2552                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2553                         {
2554                                 // Deleted object header with no data chunks.
2555                                 // Can be discarded and the file deleted.
2556                                 object->chunkId = 0;
2557                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2558                                 object->variant.fileVariant.top = NULL;
2559                                 yaffs_DoGenericObjectDeletion(object);
2560                                 
2561                         }
2562                         else if(object)
2563                         {
2564                                 // It's either a data chunk in a live file or
2565                                 // an ObjectHeader, so we're interested in it.
2566                                 // NB Need to keep the ObjectHeaders of deleted files
2567                                 // until the whole file has been deleted off
2568                                 tags.serialNumber++;
2569
2570                                 dev->nGCCopies++;
2571
2572                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2573                         
2574                                 if(newChunk < 0)
2575                                 {
2576                                         retVal =  YAFFS_FAIL;
2577                                 }
2578                                 else
2579                                 {
2580                         
2581                                         // Ok, now fix up the Tnodes etc.
2582                         
2583                                         if(tags.chunkId == 0)
2584                                         {
2585                                                 // It's a header
2586                                                 object->chunkId = newChunk;
2587                                                 object->serial = tags.serialNumber;
2588                                         }
2589                                         else
2590                                         {
2591                                                 // It's a data chunk
2592                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2593                                         }
2594                                 }
2595                         }
2596                         
2597                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2598                         
2599                 }
2600         }
2601
2602         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2603
2604
2605         // Do any required cleanups
2606         for(i = 0; i < cleanups; i++)
2607         {                                               
2608                 // Time to delete the file too
2609                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2610                 if(object)
2611                 {
2612                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2613                         object->variant.fileVariant.top = NULL;
2614                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2615                         yaffs_DoGenericObjectDeletion(object);
2616                 }
2617
2618         }
2619
2620         }
2621
2622         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2623         {
2624                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2625         }
2626                         
2627         return YAFFS_OK;
2628 }
2629
2630 #if 0
2631 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2632 {
2633         // find a file to delete
2634         struct list_head *i;    
2635         yaffs_Object *l;
2636
2637
2638         //Scan the unlinked files looking for one to delete
2639         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2640         {
2641                 if(i)
2642                 {
2643                         l = list_entry(i, yaffs_Object,siblings);
2644                         if(l->deleted)
2645                         {
2646                                 return l;                       
2647                         }
2648                 }
2649         }       
2650         return NULL;
2651 }
2652
2653
2654 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2655 {
2656         // This does background deletion on unlinked files.. only deleted ones.
2657         // If we don't have a file we're working on then find one
2658         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2659         {
2660                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2661         }
2662         
2663         // OK, we're working on a file...
2664         if(dev->unlinkedDeletion)
2665         {
2666                 yaffs_Object *obj = dev->unlinkedDeletion;
2667                 int delresult;
2668                 int limit; // Number of chunks to delete in a file.
2669                                    // NB this can be exceeded, but not by much.
2670                                    
2671                 limit = -1;
2672
2673                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2674                 
2675                 if(obj->nDataChunks == 0)
2676                 {
2677                         // Done all the deleting of data chunks.
2678                         // Now dump the header and clean up
2679                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2680                         obj->variant.fileVariant.top = NULL;
2681                         yaffs_DoGenericObjectDeletion(obj);
2682                         dev->nDeletedFiles--;
2683                         dev->nUnlinkedFiles--;
2684                         dev->nBackgroundDeletions++;
2685                         dev->unlinkedDeletion = NULL;   
2686                 }
2687         }
2688 }
2689
2690 #endif
2691
2692 #if 0
2693 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2694 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2695 {
2696         int block;
2697         int aggressive=0;
2698         
2699         //yaffs_DoUnlinkedFileDeletion(dev);
2700         
2701         if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2702         {
2703                 aggressive = 1;
2704         }               
2705         
2706         if(aggressive)
2707         {
2708                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2709                 
2710                 if(block >= 0)
2711                 {
2712                         dev->garbageCollections++;
2713                         return yaffs_GarbageCollectBlock(dev,block);
2714                 }       
2715                 else
2716                 {
2717                         return YAFFS_FAIL;
2718                 }
2719         }
2720
2721         return YAFFS_OK;
2722 }
2723 #endif
2724
2725 // New garbage collector
2726 // If we're very low on erased blocks then we do aggressive garbage collection
2727 // otherwise we do "leasurely" garbage collection.
2728 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2729 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2730 //
2731 // The idea is to help clear out space in a more spread-out manner.
2732 // Dunno if it really does anything useful.
2733 //
2734 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2735 {
2736         int block;
2737         int aggressive; 
2738         int gcOk = YAFFS_OK;
2739         int maxTries = 0;
2740         
2741         //yaffs_DoUnlinkedFileDeletion(dev);
2742
2743         // This loop should pass the first time.
2744         // We'll only see looping here if the erase of the collected block fails.
2745         
2746         do{
2747                 maxTries++;
2748                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2749                 {
2750                         // We need a block soon...
2751                         aggressive = 1;
2752                 }
2753                 else 
2754                 {
2755                         // We're in no hurry
2756                         aggressive = 0;
2757                 }
2758         
2759                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2760         
2761                 if(block > 0)
2762                 {
2763                         dev->garbageCollections++;
2764                         if(!aggressive)
2765                         {
2766                                 dev->passiveGarbageCollections++;
2767                         }
2768
2769                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2770
2771                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2772                 }
2773
2774                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) 
2775                 {
2776                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2777                 }
2778         } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2779
2780         return aggressive ? gcOk: YAFFS_OK;
2781 }
2782
2783
2784 //////////////////////////// TAGS ///////////////////////////////////////
2785
2786
2787 void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
2788 {
2789         memset(tags,0,sizeof(yaffs_ExtendedTags));
2790         tags->validMarker0 = 0xAAAAAAAA;
2791         tags->validMarker1 = 0x55555555;
2792 }
2793
2794 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
2795 {
2796         return (tags->validMarker0 == 0xAAAAAAAA &&     tags->validMarker1 == 0x55555555);
2797
2798 }
2799
2800
2801 #if 0
2802
2803 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2804 {
2805         // Calculate an ecc
2806         
2807         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2808         unsigned  i,j;
2809         unsigned  ecc = 0;
2810         unsigned bit = 0;
2811
2812         tags->ecc = 0;
2813         
2814         for(i = 0; i < 8; i++)
2815         {
2816                 for(j = 1; j &0xff; j<<=1)
2817                 {
2818                         bit++;
2819                         if(b[i] & j)
2820                         {
2821                                 ecc ^= bit;
2822                         }
2823                 }
2824         }
2825         
2826         tags->ecc = ecc;
2827         
2828         
2829 }
2830
2831 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2832 {
2833         unsigned ecc = tags->ecc;
2834         
2835         yaffs_CalcTagsECC(tags);
2836         
2837         ecc ^= tags->ecc;
2838         
2839         if(ecc && ecc <= 64)
2840         {
2841                 // TODO: Handle the failure better. Retire?
2842                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2843
2844                 ecc--;
2845                                 
2846                 b[ecc / 8] ^= (1 << (ecc & 7));
2847                 
2848                 // Now recvalc the ecc
2849                 yaffs_CalcTagsECC(tags);
2850                 
2851                 return 1; // recovered error
2852         }
2853         else if(ecc)
2854         {
2855                 // Wierd ecc failure value
2856                 // TODO Need to do somethiong here
2857                 return -1; //unrecovered error
2858         }
2859         
2860         return 0;
2861 }
2862
2863 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2864 {
2865         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2866         
2867         yaffs_CalcTagsECC(tagsPtr);
2868         
2869         sparePtr->tagByte0 = tu->asBytes[0];
2870         sparePtr->tagByte1 = tu->asBytes[1];
2871         sparePtr->tagByte2 = tu->asBytes[2];
2872         sparePtr->tagByte3 = tu->asBytes[3];
2873         sparePtr->tagByte4 = tu->asBytes[4];
2874         sparePtr->tagByte5 = tu->asBytes[5];
2875         sparePtr->tagByte6 = tu->asBytes[6];
2876         sparePtr->tagByte7 = tu->asBytes[7];
2877 }
2878
2879 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2880 {
2881         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2882         int result;
2883
2884         tu->asBytes[0]= sparePtr->tagByte0;
2885         tu->asBytes[1]= sparePtr->tagByte1;
2886         tu->asBytes[2]= sparePtr->tagByte2;
2887         tu->asBytes[3]= sparePtr->tagByte3;
2888         tu->asBytes[4]= sparePtr->tagByte4;
2889         tu->asBytes[5]= sparePtr->tagByte5;
2890         tu->asBytes[6]= sparePtr->tagByte6;
2891         tu->asBytes[7]= sparePtr->tagByte7;
2892         
2893         result =  yaffs_CheckECCOnTags(tagsPtr);
2894         if(result> 0)
2895         {
2896                 dev->tagsEccFixed++;
2897         }
2898         else if(result <0)
2899         {
2900                 dev->tagsEccUnfixed++;
2901         }
2902 }
2903
2904 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2905 {
2906         memset(spare,0xFF,sizeof(yaffs_Spare));
2907 }
2908
2909 #endif
2910
2911 #if 0
2912 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2913 {
2914         // NB There must be tags, data is optional
2915         // If there is data, then an ECC is calculated on it.
2916         
2917         yaffs_Spare spare;
2918         
2919         if(!tags)
2920         {
2921                 return YAFFS_FAIL;
2922         }
2923         
2924         //yaffs_SpareInitialise(&spare);
2925         
2926         //if(!dev->useNANDECC && buffer)
2927         //{
2928         //      yaffs_CalcECC(buffer,&spare);
2929         //}
2930         
2931         //yaffs_LoadTagsIntoSpare(&spare,tags);
2932         
2933         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2934         
2935 }
2936 #endif
2937
2938 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2939 {
2940         return  (  tags->chunkId == chunkInObject &&
2941                            tags->objectId == objectId &&
2942                            !tags->chunkDeleted) ? 1 : 0;
2943         
2944 }
2945
2946
2947
2948 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2949 {
2950         //Get the Tnode, then get the level 0 offset chunk offset
2951     yaffs_Tnode *tn;     
2952     int theChunk = -1;
2953     yaffs_ExtendedTags localTags;
2954     int retVal = -1;
2955     
2956     yaffs_Device *dev = in->myDev;
2957     
2958     
2959     if(!tags)
2960     {
2961         // Passed a NULL, so use our own tags space
2962         tags = &localTags;
2963     }
2964     
2965     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2966     
2967     if(tn)
2968     {
2969                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2970                 
2971                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2972     }
2973     return retVal;
2974 }
2975
2976 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2977 {
2978         //Get the Tnode, then get the level 0 offset chunk offset
2979     yaffs_Tnode *tn;     
2980     int theChunk = -1;
2981     yaffs_ExtendedTags localTags;
2982
2983     yaffs_Device *dev = in->myDev;
2984     int retVal = -1;
2985     
2986     if(!tags)
2987     {
2988         // Passed a NULL, so use our own tags space
2989         tags = &localTags;
2990     }
2991     
2992     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2993     
2994     if(tn)
2995     {
2996     
2997                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2998                 
2999                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
3000     
3001                 // Delete the entry in the filestructure (if found)
3002                 if(retVal != -1)
3003                 {
3004                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
3005                 }
3006     }
3007     else
3008     {
3009         //T(("No level 0 found for %d\n", chunkInInode));
3010     }
3011     
3012     if(retVal == -1)
3013     {
3014         //T(("Could not find %d to delete\n",chunkInInode));
3015     }
3016     return retVal;
3017 }
3018
3019
3020 #ifdef YAFFS_PARANOID
3021
3022 static int yaffs_CheckFileSanity(yaffs_Object *in)
3023 {
3024         int chunk;
3025         int nChunks;
3026         int fSize;
3027         int failed = 0;
3028         int objId;
3029         yaffs_Tnode *tn;
3030     yaffs_Tags localTags;
3031     yaffs_Tags *tags = &localTags;
3032     int theChunk;
3033     int chunkDeleted;
3034     
3035         
3036         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3037         {
3038                 //T(("Object not a file\n"));
3039                 return YAFFS_FAIL;
3040         }
3041         
3042         objId = in->objectId;
3043         fSize  = in->variant.fileVariant.fileSize;
3044         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3045         
3046         for(chunk = 1; chunk <= nChunks; chunk++)
3047         {
3048                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3049     
3050                 if(tn)
3051                 {
3052     
3053                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3054                         
3055                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3056                         {
3057
3058
3059                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3060                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3061                                 {
3062                                         // found it;
3063                                 
3064                                 }
3065                         }
3066                         else
3067                         {
3068                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
3069                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
3070                                                 
3071                                 failed = 1;
3072                                                         
3073                         }
3074     
3075                 }
3076                 else
3077                 {
3078                         //T(("No level 0 found for %d\n", chunk));
3079                 }
3080         }
3081         
3082         return failed ? YAFFS_FAIL : YAFFS_OK;
3083 }
3084
3085 #endif
3086
3087 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3088 {
3089         yaffs_Tnode *tn;
3090         yaffs_Device *dev = in->myDev;
3091         int existingChunk;
3092         yaffs_ExtendedTags existingTags;
3093         yaffs_ExtendedTags newTags;
3094         unsigned existingSerial, newSerial;
3095                 
3096         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3097         if(!tn)
3098         {
3099                 return YAFFS_FAIL;
3100         }
3101
3102         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
3103         
3104         if(inScan)
3105         {
3106                 // If we're scanning then we need to test for duplicates
3107                 // NB This does not need to be efficient since it should only ever 
3108                 // happen when the power fails during a write, then only one
3109                 // chunk should ever be affected.
3110         
3111                 
3112                 if(existingChunk != 0)
3113                 {
3114                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3115                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3116                         //
3117                         // We have a duplicate now we need to decide which one to use
3118                         // To do this we get both sets of tags and compare serial numbers.
3119                         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3120                         
3121                         
3122                         // Do a proper find
3123                         existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3124
3125                         if(existingChunk <=0)
3126                         {
3127                                 //Hoosterman - how did this happen?
3128                                 
3129                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3130
3131                         }
3132
3133                         
3134                         // NB The deleted flags should be false, otherwise the chunks will 
3135                         // not be loaded during a scan
3136                         
3137                         newSerial = newTags.serialNumber;
3138                         existingSerial = existingTags.serialNumber;
3139                         
3140                         if( in->myDev->isYaffs2 ||
3141                             existingChunk <= 0 ||
3142                             ((existingSerial+1) & 3) == newSerial)
3143                         {
3144                                 // Use new
3145                                 // Delete the old one and drop through to update the tnode
3146                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3147                         }
3148                         else
3149                         {
3150                                 // Use existing.
3151                                 // Delete the new one and return early so that the tnode isn't changed
3152                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3153                                 return YAFFS_OK;
3154                         }
3155                 }
3156
3157         }
3158                 
3159         if(existingChunk == 0)
3160         {
3161                 in->nDataChunks++;
3162         }
3163         
3164         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3165         
3166         return YAFFS_OK;
3167 }
3168
3169
3170
3171 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3172 {
3173     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3174     
3175     if(chunkInNAND >= 0)
3176     {
3177                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3178         }
3179         else
3180         {
3181                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3182
3183                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3184                 return 0;
3185         }
3186
3187 }
3188
3189
3190 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3191 {
3192         int block;
3193         int page;
3194         yaffs_ExtendedTags tags;
3195         yaffs_BlockInfo *bi;
3196         
3197         if(chunkId <= 0) return;        
3198         
3199         dev->nDeletions++;
3200         block = chunkId / dev->nChunksPerBlock;
3201         page = chunkId % dev->nChunksPerBlock;
3202         
3203         bi = yaffs_GetBlockInfo(dev,block);
3204         
3205         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3206         
3207         if(markNAND && 
3208           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3209           !dev->isYaffs2)
3210         {
3211 //              yaffs_SpareInitialise(&spare);
3212
3213 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3214
3215                 //read data before write, to ensure correct ecc 
3216                 //if we're using MTD verification under Linux
3217                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3218 #endif
3219
3220                 yaffs_InitialiseTags(&tags);
3221
3222                 tags.chunkDeleted = 1;
3223
3224         
3225                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3226                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3227         }
3228         else
3229         {
3230                         dev->nUnmarkedDeletions++;
3231         }       
3232         
3233         
3234         // Pull out of the management area.
3235         // If the whole block became dirty, this will kick off an erasure.
3236         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3237             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3238             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3239             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3240         {
3241                 dev->nFreeChunks++;
3242
3243                 yaffs_ClearChunkBit(dev,block,page);
3244                 bi->pagesInUse--;
3245                 
3246                 if(bi->pagesInUse == 0 &&
3247                    !bi->hasShrinkHeader &&
3248                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3249                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3250             {
3251                 yaffs_BlockBecameDirty(dev,block);
3252             }
3253
3254         }
3255         else
3256         {
3257                 // T(("Bad news deleting chunk %d\n",chunkId));
3258         }
3259         
3260 }
3261
3262
3263
3264
3265 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3266 {
3267         // Find old chunk Need to do this to get serial number
3268         // Write new one and patch into tree.
3269         // Invalidate old tags.
3270
3271     int prevChunkId;
3272     yaffs_ExtendedTags prevTags;
3273     
3274     int newChunkId;
3275     yaffs_ExtendedTags newTags;
3276
3277     yaffs_Device *dev = in->myDev;    
3278
3279         yaffs_CheckGarbageCollection(dev);
3280
3281         // Get the previous chunk at this location in the file if it exists
3282     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3283     
3284     // Set up new tags
3285     yaffs_InitialiseTags(&newTags);
3286     
3287         newTags.chunkId = chunkInInode;
3288         newTags.objectId = in->objectId;
3289         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3290         newTags.byteCount = nBytes;
3291                 
3292 //      yaffs_CalcTagsECC(&newTags);
3293
3294         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3295         
3296         if(newChunkId >= 0)
3297         {
3298                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3299                 
3300                 
3301                 if(prevChunkId >= 0)
3302                 {
3303                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3304         
3305                 }
3306                 
3307                 yaffs_CheckFileSanity(in);
3308         }
3309         return newChunkId;
3310
3311
3312
3313
3314
3315 }
3316
3317
3318 // UpdateObjectHeader updates the header on NAND for an object.
3319 // If name is not NULL, then that new name is used.
3320 //
3321 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3322 {
3323
3324         yaffs_BlockInfo *bi;
3325         
3326         yaffs_Device *dev = in->myDev;
3327         
3328     int prevChunkId;
3329     int retVal = 0;
3330     
3331     int newChunkId;
3332     yaffs_ExtendedTags newTags;
3333     
3334     __u8 *buffer = NULL;
3335     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3336     
3337    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3338     
3339     yaffs_ObjectHeader *oh = NULL;
3340     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3341
3342     
3343     if(!in->fake || force)
3344     {
3345   
3346                 yaffs_CheckGarbageCollection(dev);              
3347     
3348             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3349             oh  = (yaffs_ObjectHeader *)buffer;
3350     
3351                 prevChunkId = in->chunkId;
3352     
3353                 if(prevChunkId >= 0)
3354                 {
3355                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3356                         memcpy(oldName,oh->name,sizeof(oh->name));      
3357                 }
3358
3359                 memset(buffer,0xFF,dev->nBytesPerChunk);
3360
3361                 // Header data
3362                 oh->type = in->variantType;
3363                 
3364                 oh->st_mode = in->st_mode;
3365
3366 #ifdef CONFIG_YAFFS_WINCE
3367                 oh->win_atime[0] = in->win_atime[0];
3368                 oh->win_ctime[0] = in->win_ctime[0];
3369                 oh->win_mtime[0] = in->win_mtime[0];
3370                 oh->win_atime[1] = in->win_atime[1];
3371                 oh->win_ctime[1] = in->win_ctime[1];
3372                 oh->win_mtime[1] = in->win_mtime[1];
3373 #else
3374                 oh->st_uid = in->st_uid;
3375                 oh->st_gid = in->st_gid;
3376                 oh->st_atime = in->st_atime;
3377                 oh->st_mtime = in->st_mtime;
3378                 oh->st_ctime = in->st_ctime;
3379                 oh->st_rdev = in->st_rdev;
3380 #endif  
3381                 if(in->parent)
3382                 {
3383                         oh->parentObjectId = in->parent->objectId;
3384                 }
3385                 else
3386                 {
3387                         oh->parentObjectId = 0;
3388                 }
3389                 
3390                 //oh->sum = in->sum;
3391                 if(name && *name)
3392                 {
3393                         memset(oh->name,0,sizeof(oh->name));
3394                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3395                 }
3396                 else if(prevChunkId)
3397                 {       
3398                         memcpy(oh->name, oldName,sizeof(oh->name));
3399                 }
3400                 else
3401                 {
3402                         memset(oh->name,0,sizeof(oh->name));    
3403                 }
3404                 
3405                 oh->isShrink = isShrink;
3406         
3407                 switch(in->variantType)
3408                 {
3409                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3410                                 // Should not happen
3411                                 break;
3412                         case YAFFS_OBJECT_TYPE_FILE:
3413                                 oh->fileSize = in->variant.fileVariant.fileSize;
3414                                 break;
3415                         case YAFFS_OBJECT_TYPE_HARDLINK:
3416                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3417                                 break;
3418                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3419                                 // Do nothing
3420                                 break;
3421                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3422                                 // Do nothing
3423                                 break;
3424                         case YAFFS_OBJECT_TYPE_SYMLINK:
3425                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3426                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3427                                 break;
3428                 }
3429
3430                 // Tags
3431                 yaffs_InitialiseTags(&newTags);
3432                 in->serial++;
3433                 newTags.chunkId = 0;
3434                 newTags.objectId = in->objectId;
3435                 newTags.serialNumber = in->serial;
3436         
3437
3438                 // Create new chunk in NAND
3439                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3440     
3441                 if(newChunkId >= 0)
3442                 {
3443                 
3444                         in->chunkId = newChunkId;       
3445                 
3446                         if(prevChunkId >= 0)
3447                         {
3448                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3449                         }
3450                 
3451                         in->dirty = 0;
3452                         
3453                         // If this was a shrink, then mark the block that the chunk lives on
3454                         if(isShrink)
3455                         {