a3fce2cddf2dad153058a3ef9c1c4eb24dc0058d
[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.1 2004-11-03 08:14:07 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 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_DELETE,(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         yaffs_UnhashObject(tn);
1533         
1534         // Link into the free list.
1535         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1536         dev->freeObjects = tn;
1537         dev->nFreeObjects++;
1538 }
1539
1540
1541
1542
1543 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1544 {
1545         // Free the list of allocated Objects
1546         
1547         yaffs_ObjectList *tmp;
1548         
1549         while( dev->allocatedObjectList)
1550         {
1551                 tmp =  dev->allocatedObjectList->next;
1552                 YFREE(dev->allocatedObjectList->objects);
1553                 YFREE(dev->allocatedObjectList);
1554                 
1555                 dev->allocatedObjectList =  tmp;
1556         }
1557         
1558         dev->freeObjects = NULL;
1559         dev->nFreeObjects = 0;
1560 }
1561
1562 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1563 {
1564         int i;
1565         
1566         dev->allocatedObjectList = NULL;
1567         dev->freeObjects = NULL;
1568         dev->nFreeObjects = 0;
1569         
1570         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1571         {
1572                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1573                 dev->objectBucket[i].count = 0; 
1574         }
1575
1576 }
1577
1578
1579
1580
1581
1582
1583 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1584 {
1585         static int x = 0;
1586         int i;
1587         int l = 999;
1588         int lowest = 999999;
1589
1590                 
1591         // First let's see if we can find one that's empty.
1592         
1593         for(i = 0; i < 10 && lowest > 0; i++)
1594          {
1595                 x++;
1596                 x %=  YAFFS_NOBJECT_BUCKETS;
1597                 if(dev->objectBucket[x].count < lowest)
1598                 {
1599                         lowest = dev->objectBucket[x].count;
1600                         l = x;
1601                 }
1602                 
1603         }
1604         
1605         // If we didn't find an empty list, then try
1606         // looking a bit further for a short one
1607         
1608         for(i = 0; i < 10 && lowest > 3; i++)
1609          {
1610                 x++;
1611                 x %=  YAFFS_NOBJECT_BUCKETS;
1612                 if(dev->objectBucket[x].count < lowest)
1613                 {
1614                         lowest = dev->objectBucket[x].count;
1615                         l = x;
1616                 }
1617                 
1618         }
1619         
1620         return l;
1621 }
1622
1623 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1624 {
1625         int bucket = yaffs_FindNiceObjectBucket(dev);
1626         
1627         // Now find an object value that has not already been taken
1628         // by scanning the list.
1629         
1630         int found = 0;
1631         struct list_head *i;
1632         
1633         __u32 n = (__u32)bucket;
1634
1635         //yaffs_CheckObjectHashSanity();        
1636         
1637         while(!found)
1638         {
1639                 found = 1;
1640                 n +=  YAFFS_NOBJECT_BUCKETS;
1641                 if(1 ||dev->objectBucket[bucket].count > 0)
1642                 {
1643                         list_for_each(i,&dev->objectBucket[bucket].list)
1644                         {
1645                                 // If there is already one in the list
1646                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1647                                 {
1648                                         found = 0;
1649                                 }
1650                         }
1651                 }
1652         }
1653         
1654         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1655         
1656         return n;       
1657 }
1658
1659 void yaffs_HashObject(yaffs_Object *in)
1660 {
1661         int bucket = yaffs_HashFunction(in->objectId);
1662         yaffs_Device *dev = in->myDev;
1663         
1664         if(!list_empty(&in->hashLink))
1665         {
1666                 //YINFO("!!!");
1667         }
1668
1669         
1670         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1671         dev->objectBucket[bucket].count++;
1672
1673 }
1674
1675 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1676 {
1677         int bucket = yaffs_HashFunction(number);
1678         struct list_head *i;
1679         yaffs_Object *in;
1680         
1681         list_for_each(i,&dev->objectBucket[bucket].list)
1682         {
1683                 // Look if it is in the list
1684                 if(i)
1685                 {
1686                         in = list_entry(i, yaffs_Object,hashLink);
1687                         if(in->objectId == number)
1688                         {
1689                                 return in;
1690                         }
1691                 }
1692         }
1693         
1694         return NULL;
1695 }
1696
1697
1698
1699 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1700 {
1701                 
1702         yaffs_Object *theObject;
1703
1704         if(number < 0)
1705         {
1706                 number = yaffs_CreateNewObjectNumber(dev);
1707         }
1708         
1709         theObject = yaffs_AllocateEmptyObject(dev);
1710         
1711         if(theObject)
1712         {
1713                 theObject->fake = 0;
1714                 theObject->renameAllowed = 1;
1715                 theObject->unlinkAllowed = 1;
1716                 theObject->objectId = number;
1717                 yaffs_HashObject(theObject);
1718                 theObject->variantType = type;
1719 #ifdef CONFIG_YAFFS_WINCE
1720                 yfsd_WinFileTimeNow(theObject->win_atime);
1721                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1722                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1723
1724 #else
1725
1726 #if defined(CONFIG_KERNEL_2_5)
1727                 theObject->st_atime = theObject->st_mtime =     theObject->st_ctime = CURRENT_TIME.tv_sec;              
1728 #else
1729                 theObject->st_atime = theObject->st_mtime =     theObject->st_ctime = CURRENT_TIME;             
1730 #endif
1731 #endif
1732                 switch(type)
1733                 {
1734                         case YAFFS_OBJECT_TYPE_FILE: 
1735                                 theObject->variant.fileVariant.fileSize = 0;
1736                                 theObject->variant.fileVariant.scannedFileSize = 0;
1737                                 theObject->variant.fileVariant.topLevel = 0;
1738                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1739                                 break;
1740                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1741                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1742                                 break;
1743                         case YAFFS_OBJECT_TYPE_SYMLINK:
1744                                 // No action required
1745                                 break;
1746                         case YAFFS_OBJECT_TYPE_HARDLINK:
1747                                 // No action required
1748                                 break;
1749                         case YAFFS_OBJECT_TYPE_SPECIAL:
1750                                 // No action required
1751                                 break;
1752                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1753                                 // todo this should not happen
1754                                 break;
1755                 }
1756         }
1757         
1758         return theObject;
1759 }
1760
1761 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1762 {
1763         yaffs_Object *theObject = NULL;
1764         
1765         if(number > 0)
1766         {
1767                 theObject = yaffs_FindObjectByNumber(dev,number);
1768         }
1769         
1770         if(!theObject)
1771         {
1772                 theObject = yaffs_CreateNewObject(dev,number,type);
1773         }
1774         
1775         return theObject;
1776
1777 }
1778
1779 YCHAR *yaffs_CloneString(const YCHAR *str)
1780 {
1781         YCHAR *newStr = NULL;
1782         
1783         if(str && *str)
1784         {
1785                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1786                 yaffs_strcpy(newStr,str);
1787         }
1788
1789         return newStr;
1790         
1791 }
1792
1793 //
1794 // Mknod (create) a new object.
1795 // equivalentObject only has meaning for a hard link;
1796 // aliasString only has meaning for a sumlink.
1797 // rdev only has meaning for devices (a subset of special objects)
1798 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1799                                                                  yaffs_Object *parent,
1800                                                                  const YCHAR *name, 
1801                                                                  __u32 mode,
1802                                                                  __u32 uid,
1803                                                                  __u32 gid,
1804                                                                  yaffs_Object *equivalentObject,
1805                                                                  const YCHAR *aliasString,
1806                                                                  __u32 rdev)
1807 {
1808         yaffs_Object *in;
1809
1810         yaffs_Device *dev = parent->myDev;
1811         
1812         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1813         if(yaffs_FindObjectByName(parent,name))
1814         {
1815                 return NULL;
1816         }
1817         
1818         in = yaffs_CreateNewObject(dev,-1,type);
1819         
1820         if(in)
1821         {
1822                 in->chunkId = -1;
1823                 in->valid = 1;
1824                 in->variantType = type;
1825
1826                 in->st_mode  = mode;
1827                 
1828 #ifdef CONFIG_YAFFS_WINCE
1829                 yfsd_WinFileTimeNow(in->win_atime);
1830                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1831                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1832                 
1833 #else
1834 #if defined(CONFIG_KERNEL_2_5)
1835                 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME.tv_sec;
1836 #else
1837                 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
1838 #endif
1839                 in->st_rdev  = rdev;
1840                 in->st_uid   = uid;
1841                 in->st_gid   = gid;
1842 #endif          
1843                 in->nDataChunks = 0;
1844
1845                 yaffs_SetObjectName(in,name);
1846                 in->dirty = 1;
1847                 
1848                 yaffs_AddObjectToDirectory(parent,in);
1849                 
1850                 in->myDev = parent->myDev;
1851                 
1852                                 
1853                 switch(type)
1854                 {
1855                         case YAFFS_OBJECT_TYPE_SYMLINK:
1856                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1857                                 break;
1858                         case YAFFS_OBJECT_TYPE_HARDLINK:
1859                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1860                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1861                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1862                                 break;
1863                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1864                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1865                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1866                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1867                                 break;
1868                 }
1869
1870                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1871                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1872                 {
1873                         // Could not create the object header, fail the creation
1874                         yaffs_DestroyObject(in);
1875                         in = NULL;
1876                 }
1877
1878         }
1879         
1880         return in;
1881 }
1882
1883 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1884 {
1885         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1886 }
1887
1888 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1889 {
1890         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1891 }
1892
1893 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1894 {
1895         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1896 }
1897
1898 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1899 {
1900         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1901 }
1902
1903 // NB yaffs_Link returns the object id of the equivalent object.
1904 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1905 {
1906         // Get the real object in case we were fed a hard link as an equivalent object
1907         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1908         
1909         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1910         {
1911                 return equivalentObject;
1912         }
1913         else
1914         {
1915                 return NULL;
1916         }
1917         
1918 }
1919
1920
1921 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1922 {
1923         int unlinkOp;
1924         int deleteOp;
1925
1926         if(newDir == NULL)
1927         {
1928                 newDir = obj->parent; // use the old directory
1929         }
1930
1931         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1932         if(obj->myDev->isYaffs2)
1933         {
1934                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1935         }
1936         else
1937         {
1938                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1939         }
1940
1941         deleteOp = (newDir == obj->myDev->deletedDir);
1942         
1943         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1944         // duplicate names are allowed.
1945         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1946         if( (unlinkOp|| 
1947                  deleteOp ||
1948                  force || 
1949                  !yaffs_FindObjectByName(newDir,newName))  &&
1950              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1951         {
1952                 yaffs_SetObjectName(obj,newName);
1953                 obj->dirty = 1;
1954                 
1955                 yaffs_AddObjectToDirectory(newDir,obj);
1956                 
1957                 if(unlinkOp) obj->unlinked = 1;
1958                 
1959                 // If it is a dletion then we mark it as a shrink for gc purposes.
1960                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1961                 {
1962                         return YAFFS_OK;
1963                 }
1964         }
1965         
1966         return YAFFS_FAIL;
1967 }
1968
1969
1970
1971 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1972 {
1973         yaffs_Object *obj;
1974         int force = 0;
1975         
1976 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1977         // Special case for WinCE.
1978         // While look-up is case insensitive, the name isn't.
1979         // THerefore we might want to change x.txt to X.txt
1980         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1981         {
1982                 force = 1;
1983         }       
1984 #endif
1985         
1986         obj = yaffs_FindObjectByName(oldDir,oldName);
1987         if(obj && obj->renameAllowed)
1988         {
1989                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1990         }
1991         return YAFFS_FAIL;
1992 }
1993
1994
1995
1996 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1997 {
1998         // Scan the buckets and check that the lists 
1999         // have as many members as the count says there are
2000         int bucket;
2001         int countEm;
2002         struct list_head *j;
2003         int ok = YAFFS_OK;
2004         
2005         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
2006         {
2007                 countEm = 0;
2008                 
2009                 list_for_each(j,&dev->objectBucket[bucket].list)
2010                 {
2011                         countEm++;
2012                 }
2013                 
2014                 if(countEm != dev->objectBucket[bucket].count)
2015                 {
2016                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
2017                         ok = YAFFS_FAIL;
2018                 }
2019         }
2020
2021         return ok;
2022 }
2023
2024 #if 0
2025 void yaffs_ObjectTest(yaffs_Device *dev)
2026 {
2027         yaffs_Object *in[1000];
2028         int inNo[1000];
2029         yaffs_Object *inold[1000];
2030         int i;
2031         int j;
2032         
2033         memset(in,0,1000*sizeof(yaffs_Object *));
2034         memset(inold,0,1000*sizeof(yaffs_Object *));
2035         
2036         yaffs_CheckObjectHashSanity(dev);
2037         
2038         for(j = 0; j < 10; j++)
2039         {
2040                 //T(("%d\n",j));
2041                 
2042                 for(i = 0; i < 1000; i++)
2043                 {
2044                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
2045                         if(!in[i])
2046                         {
2047                                 YINFO("No more inodes");
2048                         }
2049                         else
2050                         {
2051                                 inNo[i] = in[i]->objectId;
2052                         }
2053                 }
2054                 
2055                 for(i = 0; i < 1000; i++)
2056                 {
2057                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
2058                         {
2059                                 //T(("Differnce in look up test\n"));
2060                         }
2061                         else
2062                         {
2063                                 // T(("Look up ok\n"));
2064                         }
2065                 }
2066                 
2067                 yaffs_CheckObjectHashSanity(dev);
2068         
2069                 for(i = 0; i < 1000; i+=3)
2070                 {
2071                         yaffs_FreeObject(in[i]);        
2072                         in[i] = NULL;
2073                 }
2074                 
2075         
2076                 yaffs_CheckObjectHashSanity(dev);
2077         }
2078                 
2079 }
2080
2081 #endif
2082
2083 /////////////////////////// Block Management and Page Allocation ///////////////////
2084
2085
2086 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2087 {
2088         dev->allocationBlock = -1; // force it to get a new one
2089         //Todo we're assuming the malloc will pass.
2090         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2091         // Set up dynamic blockinfo stuff.
2092         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2093         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2094         if(dev->blockInfo && dev->chunkBits)
2095         {
2096                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2097                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2098                 return YAFFS_OK;
2099         }
2100         
2101         return YAFFS_FAIL;
2102         
2103 }
2104
2105 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2106 {
2107         YFREE(dev->blockInfo);
2108         dev->blockInfo = NULL;
2109         YFREE(dev->chunkBits);
2110         dev->chunkBits = NULL;
2111 }
2112
2113
2114 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2115 {
2116         int i;
2117         __u32 seq;
2118         yaffs_BlockInfo *b;
2119         
2120         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2121         
2122         if(!bi->hasShrinkHeader) return 1; // can gc
2123
2124
2125         // Find the oldest dirty sequence number if we don't know it and save it
2126         // so we don't have to keep recomputing it.
2127         if(!dev->oldestDirtySequence)
2128         {
2129                 seq = dev->sequenceNumber;
2130
2131                 for(i = dev->startBlock; i <= dev->endBlock; i++)
2132                 {
2133                         b = yaffs_GetBlockInfo(dev,i);
2134                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2135                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2136                         b->sequenceNumber < seq)
2137                         {
2138                                 seq = b->sequenceNumber;
2139                         }
2140                 }
2141                 dev->oldestDirtySequence = seq;
2142         }
2143
2144
2145         // Can't do gc of this block if there are any blocks older than this one that have
2146         // discarded pages.
2147         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2148         
2149         
2150         return 1;
2151
2152 }
2153
2154 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2155 // for garbage collection.
2156 //
2157
2158
2159
2160 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2161 {
2162
2163         int b = dev->currentDirtyChecker;
2164         
2165         int i;
2166         int iterations;
2167         int dirtiest = -1;
2168         int pagesInUse; 
2169         yaffs_BlockInfo *bi;
2170         static int  nonAggressiveSkip = 0;
2171
2172         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2173         // search harder.
2174         // else (we're doing a leasurely gc), then we only bother to do this if the
2175         // block has only a few pages in use.
2176         
2177
2178         nonAggressiveSkip--;
2179
2180         if(!aggressive &&(nonAggressiveSkip > 0))
2181         {
2182                 return -1;
2183         }
2184
2185         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2186         if(aggressive)
2187         {
2188                 iterations = dev->endBlock - dev->startBlock + 1;
2189         }
2190         else
2191         {
2192                 iterations = dev->endBlock - dev->startBlock + 1;
2193                 iterations = iterations / 16; 
2194                 if(iterations > 200)
2195                 {
2196                         iterations = 200;
2197                 }
2198         }
2199         
2200         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2201         {
2202                 b++;
2203                 if ( b < dev->startBlock || b > dev->endBlock)
2204                 {
2205                         b =  dev->startBlock;
2206                 }
2207
2208                 if(b < dev->startBlock || b > dev->endBlock)
2209                 {
2210                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2211                         YBUG();
2212                 }
2213                 
2214                 bi = yaffs_GetBlockInfo(dev,b);
2215                 
2216                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2217                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2218                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2219                 {
2220                         dirtiest = b;
2221                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2222                 }
2223         }
2224         
2225         dev->currentDirtyChecker = b;
2226         
2227         if(dirtiest > 0)
2228         {
2229                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2230         }
2231         
2232         dev->oldestDirtySequence = 0; // clear this
2233         
2234         if(dirtiest > 0)
2235         {
2236                 nonAggressiveSkip = 4;
2237         }
2238
2239         return dirtiest;
2240 }
2241
2242
2243 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2244 {
2245         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2246         
2247         int erasedOk = 0;
2248         
2249         // If the block is still healthy erase it and mark as clean.
2250         // If the block has had a data failure, then retire it.
2251         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2252
2253         if(!bi->needsRetiring)
2254         {
2255                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2256                 if(!erasedOk)
2257                 {
2258                         dev->nErasureFailures++;
2259                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2260                 }
2261         }
2262
2263         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2264         {
2265                         int i;
2266                         for(i = 0; i < dev->nChunksPerBlock; i++)
2267                         {
2268                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2269                                         {
2270                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2271                                         }
2272                         }
2273         }
2274         
2275         if( erasedOk )
2276         {
2277                 // Clean it up...
2278                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2279                 dev->nErasedBlocks++;
2280                 bi->pagesInUse = 0;
2281                 bi->softDeletions = 0;
2282                 bi->hasShrinkHeader=0;
2283                 yaffs_ClearChunkBits(dev,blockNo);
2284         
2285                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2286         }
2287         else
2288         {
2289                 yaffs_RetireBlock(dev,blockNo);
2290                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2291         }
2292 }
2293
2294 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2295 {
2296         int i,j;
2297         yaffs_BlockInfo *bi;
2298         
2299         for(i= dev->startBlock; i <=dev->endBlock; i++)
2300         {
2301                 bi = yaffs_GetBlockInfo(dev,i);
2302                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2303                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2304                 
2305                 for(j = 0; j < dev->nChunksPerBlock; j++)
2306                 {
2307                         if(yaffs_CheckChunkBit(dev,i,j))
2308                         {
2309                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2310
2311                         }
2312                 }
2313                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2314
2315         }
2316 }
2317
2318
2319 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2320 {
2321         int i;
2322         
2323         yaffs_BlockInfo *bi;
2324         
2325 #if 0
2326         static int j = 0;
2327         j++;
2328         if(j < 0 || j > 100)
2329         {
2330                 j = 0;
2331                 yaffs_DumpBlockStats(dev);
2332         }
2333         
2334 #endif
2335         
2336         if(dev->nErasedBlocks < 1)
2337         {
2338                 // Hoosterman we've got a problem.
2339                 // Can't get space to gc
2340                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2341
2342                 return -1;
2343         }
2344         
2345         // Find an empty block.
2346         
2347         for(i = dev->startBlock; i <= dev->endBlock; i++)
2348         {
2349                 dev->allocationBlockFinder++;
2350                 if(dev->allocationBlockFinder < dev->startBlock || dev->allocationBlockFinder> dev->endBlock) 
2351                 {
2352                         dev->allocationBlockFinder = dev->startBlock;
2353                 }
2354                 
2355                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2356
2357                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2358                 {
2359                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2360                         dev->sequenceNumber++;
2361                         bi->sequenceNumber = dev->sequenceNumber;
2362                         dev->nErasedBlocks--;           
2363                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber));   
2364                         return dev->allocationBlockFinder;
2365                 }
2366         }
2367                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been one" TENDSTR)));
2368
2369         
2370         return -1;      
2371 }
2372
2373
2374
2375 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2376 {
2377         int retVal;
2378         yaffs_BlockInfo *bi;
2379         
2380         if(dev->allocationBlock < 0)
2381         {
2382                 // Get next block to allocate off
2383                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2384                 dev->allocationPage = 0;
2385         }
2386         
2387         if(!useReserve &&  dev->nErasedBlocks </*=*/ dev->nReservedBlocks)
2388         {
2389                 // Not enough space to allocate unless we're allowed to use the reserve.
2390                 return -1;
2391         }
2392
2393         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2394         {
2395                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2396         }
2397
2398         
2399         // Next page please....
2400         if(dev->allocationBlock >= 0)
2401         {
2402                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2403                 
2404                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2405                                   dev->allocationPage;
2406                 bi->pagesInUse++;
2407                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2408
2409                 dev->allocationPage++;
2410                 
2411                 dev->nFreeChunks--;
2412                 
2413                 // If the block is full set the state to full
2414                 if(dev->allocationPage >= dev->nChunksPerBlock)
2415                 {
2416                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2417                         dev->allocationBlock = -1;
2418                 }
2419
2420
2421                 return retVal;
2422                 
2423         }
2424         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2425
2426         return -1;      
2427 }
2428
2429
2430 // To determine if we have enough space we just look at the 
2431 // number of erased blocks.
2432 // The cache is allowed to use reserved blocks.
2433
2434 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2435 {
2436         return (dev->nErasedBlocks >= dev->nReservedBlocks);
2437 }
2438
2439
2440 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2441 {
2442                 int n;
2443
2444                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2445
2446                 if(dev->allocationBlock> 0)
2447                 {
2448                         n += (dev->nChunksPerBlock - dev->allocationPage);
2449                 }
2450
2451                 return n;
2452
2453 }
2454
2455 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2456 {
2457         int oldChunk;
2458         int newChunk;
2459         int chunkInBlock;
2460         int markNAND;
2461         int retVal = YAFFS_OK;
2462         int cleanups = 0;
2463         int i;
2464
2465         int chunksBefore = yaffs_GetErasedChunks(dev);
2466         int chunksAfter;
2467
2468         yaffs_ExtendedTags  tags;
2469         
2470         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2471         
2472         yaffs_Object *object;
2473         
2474         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2475
2476         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2477         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2478
2479         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2480
2481
2482         if(!yaffs_StillSomeChunkBits(dev,block))
2483         {
2484                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2485                 yaffs_BlockBecameDirty(dev,block);
2486         }
2487         else
2488         {
2489
2490                         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2491
2492         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2493             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2494             chunkInBlock++, oldChunk++ )
2495         {
2496                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2497                 {
2498                         
2499                         // This page is in use and might need to be copied off
2500                         
2501                         markNAND = 1;
2502                         
2503                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2504                         
2505                         yaffs_InitialiseTags(&tags);
2506                         
2507                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2508
2509                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2510                         
2511                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2512                         
2513                         if(!object)
2514                         {
2515                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2516                         }
2517                         
2518                         if(object && object->deleted && tags.chunkId != 0)
2519                         {
2520                                 // Data chunk in a deleted file, throw it away
2521                                 // It's a deleted data chunk,
2522                                 // No need to copy this, just forget about it and fix up the
2523                                 // object.
2524                                 
2525                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2526                                 object->nDataChunks--;
2527                                 
2528                                 if(object->nDataChunks <= 0)
2529                                 {
2530                                         // remeber to clean up the object
2531                                         dev->gcCleanupList[cleanups] = tags.objectId;
2532                                         cleanups++;
2533                                 }
2534                                 markNAND = 0;
2535                         }
2536                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2537                         {
2538                                 // Deleted object header with no data chunks.
2539                                 // Can be discarded and the file deleted.
2540                                 object->chunkId = 0;
2541                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2542                                 object->variant.fileVariant.top = NULL;
2543                                 yaffs_DoGenericObjectDeletion(object);
2544                                 
2545                         }
2546                         else if(object)
2547                         {
2548                                 // It's either a data chunk in a live file or
2549                                 // an ObjectHeader, so we're interested in it.
2550                                 // NB Need to keep the ObjectHeaders of deleted files
2551                                 // until the whole file has been deleted off
2552                                 tags.serialNumber++;
2553
2554                                 dev->nGCCopies++;
2555
2556                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2557                         
2558                                 if(newChunk < 0)
2559                                 {
2560                                         retVal =  YAFFS_FAIL;
2561                                 }
2562                                 else
2563                                 {
2564                         
2565                                         // Ok, now fix up the Tnodes etc.
2566                         
2567                                         if(tags.chunkId == 0)
2568                                         {
2569                                                 // It's a header
2570                                                 object->chunkId = newChunk;
2571                                                 object->serial = tags.serialNumber;
2572                                         }
2573                                         else
2574                                         {
2575                                                 // It's a data chunk
2576                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2577                                         }
2578                                 }
2579                         }
2580                         
2581                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2582                         
2583                 }
2584         }
2585
2586         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2587
2588
2589         // Do any required cleanups
2590         for(i = 0; i < cleanups; i++)
2591         {                                               
2592                 // Time to delete the file too
2593                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2594                 if(object)
2595                 {
2596                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2597                         object->variant.fileVariant.top = NULL;
2598                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2599                         yaffs_DoGenericObjectDeletion(object);
2600                 }
2601
2602         }
2603
2604         }
2605
2606         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2607         {
2608                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2609         }
2610                         
2611         return YAFFS_OK;
2612 }
2613
2614 #if 0
2615 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2616 {
2617         // find a file to delete
2618         struct list_head *i;    
2619         yaffs_Object *l;
2620
2621
2622         //Scan the unlinked files looking for one to delete
2623         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2624         {
2625                 if(i)
2626                 {
2627                         l = list_entry(i, yaffs_Object,siblings);
2628                         if(l->deleted)
2629                         {
2630                                 return l;                       
2631                         }
2632                 }
2633         }       
2634         return NULL;
2635 }
2636
2637
2638 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2639 {
2640         // This does background deletion on unlinked files.. only deleted ones.
2641         // If we don't have a file we're working on then find one
2642         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2643         {
2644                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2645         }
2646         
2647         // OK, we're working on a file...
2648         if(dev->unlinkedDeletion)
2649         {
2650                 yaffs_Object *obj = dev->unlinkedDeletion;
2651                 int delresult;
2652                 int limit; // Number of chunks to delete in a file.
2653                                    // NB this can be exceeded, but not by much.
2654                                    
2655                 limit = -1;
2656
2657                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2658                 
2659                 if(obj->nDataChunks == 0)
2660                 {
2661                         // Done all the deleting of data chunks.
2662                         // Now dump the header and clean up
2663                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2664                         obj->variant.fileVariant.top = NULL;
2665                         yaffs_DoGenericObjectDeletion(obj);
2666                         dev->nDeletedFiles--;
2667                         dev->nUnlinkedFiles--;
2668                         dev->nBackgroundDeletions++;
2669                         dev->unlinkedDeletion = NULL;   
2670                 }
2671         }
2672 }
2673
2674 #endif
2675
2676 #if 0
2677 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2678 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2679 {
2680         int block;
2681         int aggressive=0;
2682         
2683         //yaffs_DoUnlinkedFileDeletion(dev);
2684         
2685         if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2686         {
2687                 aggressive = 1;
2688         }               
2689         
2690         if(aggressive)
2691         {
2692                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2693                 
2694                 if(block >= 0)
2695                 {
2696                         dev->garbageCollections++;
2697                         return yaffs_GarbageCollectBlock(dev,block);
2698                 }       
2699                 else
2700                 {
2701                         return YAFFS_FAIL;
2702                 }
2703         }
2704
2705         return YAFFS_OK;
2706 }
2707 #endif
2708
2709 // New garbage collector
2710 // If we're very low on erased blocks then we do aggressive garbage collection
2711 // otherwise we do "leasurely" garbage collection.
2712 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2713 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2714 //
2715 // The idea is to help clear out space in a more spread-out manner.
2716 // Dunno if it really does anything useful.
2717 //
2718 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2719 {
2720         int block;
2721         int aggressive; 
2722         int gcOk;
2723         int maxTries = 0;
2724         
2725         //yaffs_DoUnlinkedFileDeletion(dev);
2726
2727         // This loop should pass the first time.
2728         // We'll only see looping here if the erase of the collected block fails.
2729         
2730         do{
2731                 maxTries++;
2732                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 2))
2733                 {
2734                         // We need a block soon...
2735                         aggressive = 1;
2736                 }
2737                 else 
2738                 {
2739                         // We're in no hurry
2740                         aggressive = 0;
2741                 }
2742         
2743                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2744         
2745                 if(block > 0)
2746                 {
2747                         dev->garbageCollections++;
2748                         if(!aggressive)
2749                         {
2750                                 dev->passiveGarbageCollections++;
2751                         }
2752
2753                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2754
2755                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2756                 }
2757
2758                 if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) 
2759                 {
2760                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2761                 }
2762         } while((dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) && (block > 0) && (maxTries < 5));
2763
2764         return aggressive ? gcOk: YAFFS_OK;
2765 }
2766
2767
2768 //////////////////////////// TAGS ///////////////////////////////////////
2769
2770
2771 void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
2772 {
2773         memset(tags,0,sizeof(yaffs_ExtendedTags));
2774         tags->validMarker0 = 0xAAAAAAAA;
2775         tags->validMarker1 = 0x55555555;
2776 }
2777
2778 static int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
2779 {
2780         return (tags->validMarker0 == 0xAAAAAAAA &&     tags->validMarker1 == 0x55555555);
2781
2782 }
2783
2784
2785 #if 0
2786
2787 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2788 {
2789         // Calculate an ecc
2790         
2791         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2792         unsigned  i,j;
2793         unsigned  ecc = 0;
2794         unsigned bit = 0;
2795
2796         tags->ecc = 0;
2797         
2798         for(i = 0; i < 8; i++)
2799         {
2800                 for(j = 1; j &0xff; j<<=1)
2801                 {
2802                         bit++;
2803                         if(b[i] & j)
2804                         {
2805                                 ecc ^= bit;
2806                         }
2807                 }
2808         }
2809         
2810         tags->ecc = ecc;
2811         
2812         
2813 }
2814
2815 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2816 {
2817         unsigned ecc = tags->ecc;
2818         
2819         yaffs_CalcTagsECC(tags);
2820         
2821         ecc ^= tags->ecc;
2822         
2823         if(ecc && ecc <= 64)
2824         {
2825                 // TODO: Handle the failure better. Retire?
2826                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2827
2828                 ecc--;
2829                                 
2830                 b[ecc / 8] ^= (1 << (ecc & 7));
2831                 
2832                 // Now recvalc the ecc
2833                 yaffs_CalcTagsECC(tags);
2834                 
2835                 return 1; // recovered error
2836         }
2837         else if(ecc)
2838         {
2839                 // Wierd ecc failure value
2840                 // TODO Need to do somethiong here
2841                 return -1; //unrecovered error
2842         }
2843         
2844         return 0;
2845 }
2846
2847 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2848 {
2849         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2850         
2851         yaffs_CalcTagsECC(tagsPtr);
2852         
2853         sparePtr->tagByte0 = tu->asBytes[0];
2854         sparePtr->tagByte1 = tu->asBytes[1];
2855         sparePtr->tagByte2 = tu->asBytes[2];
2856         sparePtr->tagByte3 = tu->asBytes[3];
2857         sparePtr->tagByte4 = tu->asBytes[4];
2858         sparePtr->tagByte5 = tu->asBytes[5];
2859         sparePtr->tagByte6 = tu->asBytes[6];
2860         sparePtr->tagByte7 = tu->asBytes[7];
2861 }
2862
2863 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2864 {
2865         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2866         int result;
2867
2868         tu->asBytes[0]= sparePtr->tagByte0;
2869         tu->asBytes[1]= sparePtr->tagByte1;
2870         tu->asBytes[2]= sparePtr->tagByte2;
2871         tu->asBytes[3]= sparePtr->tagByte3;
2872         tu->asBytes[4]= sparePtr->tagByte4;
2873         tu->asBytes[5]= sparePtr->tagByte5;
2874         tu->asBytes[6]= sparePtr->tagByte6;
2875         tu->asBytes[7]= sparePtr->tagByte7;
2876         
2877         result =  yaffs_CheckECCOnTags(tagsPtr);
2878         if(result> 0)
2879         {
2880                 dev->tagsEccFixed++;
2881         }
2882         else if(result <0)
2883         {
2884                 dev->tagsEccUnfixed++;
2885         }
2886 }
2887
2888 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2889 {
2890         memset(spare,0xFF,sizeof(yaffs_Spare));
2891 }
2892
2893 #endif
2894
2895 #if 0
2896 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2897 {
2898         // NB There must be tags, data is optional
2899         // If there is data, then an ECC is calculated on it.
2900         
2901         yaffs_Spare spare;
2902         
2903         if(!tags)
2904         {
2905                 return YAFFS_FAIL;
2906         }
2907         
2908         //yaffs_SpareInitialise(&spare);
2909         
2910         //if(!dev->useNANDECC && buffer)
2911         //{
2912         //      yaffs_CalcECC(buffer,&spare);
2913         //}
2914         
2915         //yaffs_LoadTagsIntoSpare(&spare,tags);
2916         
2917         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2918         
2919 }
2920 #endif
2921
2922 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2923 {
2924         return  (  tags->chunkId == chunkInObject &&
2925                            tags->objectId == objectId &&
2926                            !tags->chunkDeleted) ? 1 : 0;
2927         
2928 }
2929
2930
2931
2932 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2933 {
2934         //Get the Tnode, then get the level 0 offset chunk offset
2935     yaffs_Tnode *tn;     
2936     int theChunk = -1;
2937     yaffs_ExtendedTags localTags;
2938     int retVal = -1;
2939     
2940     yaffs_Device *dev = in->myDev;
2941     
2942     
2943     if(!tags)
2944     {
2945         // Passed a NULL, so use our own tags space
2946         tags = &localTags;
2947     }
2948     
2949     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2950     
2951     if(tn)
2952     {
2953                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2954                 
2955                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2956     }
2957     return retVal;
2958 }
2959
2960 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2961 {
2962         //Get the Tnode, then get the level 0 offset chunk offset
2963     yaffs_Tnode *tn;     
2964     int theChunk = -1;
2965     yaffs_ExtendedTags localTags;
2966
2967     yaffs_Device *dev = in->myDev;
2968     int retVal = -1;
2969     
2970     if(!tags)
2971     {
2972         // Passed a NULL, so use our own tags space
2973         tags = &localTags;
2974     }
2975     
2976     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2977     
2978     if(tn)
2979     {
2980     
2981                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2982                 
2983                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2984     
2985                 // Delete the entry in the filestructure (if found)
2986                 if(retVal != -1)
2987                 {
2988                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2989                 }
2990     }
2991     else
2992     {
2993         //T(("No level 0 found for %d\n", chunkInInode));
2994     }
2995     
2996     if(retVal == -1)
2997     {
2998         //T(("Could not find %d to delete\n",chunkInInode));
2999     }
3000     return retVal;
3001 }
3002
3003
3004 #ifdef YAFFS_PARANOID
3005
3006 static int yaffs_CheckFileSanity(yaffs_Object *in)
3007 {
3008         int chunk;
3009         int nChunks;
3010         int fSize;
3011         int failed = 0;
3012         int objId;
3013         yaffs_Tnode *tn;
3014     yaffs_Tags localTags;
3015     yaffs_Tags *tags = &localTags;
3016     int theChunk;
3017     int chunkDeleted;
3018     
3019         
3020         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3021         {
3022                 //T(("Object not a file\n"));
3023                 return YAFFS_FAIL;
3024         }
3025         
3026         objId = in->objectId;
3027         fSize  = in->variant.fileVariant.fileSize;
3028         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
3029         
3030         for(chunk = 1; chunk <= nChunks; chunk++)
3031         {
3032                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
3033     
3034                 if(tn)
3035                 {
3036     
3037                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
3038                         
3039                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
3040                         {
3041
3042
3043                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
3044                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
3045                                 {
3046                                         // found it;
3047                                 
3048                                 }
3049                         }
3050                         else
3051                         {
3052                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
3053                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
3054                                                 
3055                                 failed = 1;
3056                                                         
3057                         }
3058     
3059                 }
3060                 else
3061                 {
3062                         //T(("No level 0 found for %d\n", chunk));
3063                 }
3064         }
3065         
3066         return failed ? YAFFS_FAIL : YAFFS_OK;
3067 }
3068
3069 #endif
3070
3071 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
3072 {
3073         yaffs_Tnode *tn;
3074         yaffs_Device *dev = in->myDev;
3075         int existingChunk;
3076         yaffs_ExtendedTags existingTags;
3077         yaffs_ExtendedTags newTags;
3078         unsigned existingSerial, newSerial;
3079                 
3080         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3081         if(!tn)
3082         {
3083                 return YAFFS_FAIL;
3084         }
3085
3086         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
3087         
3088         if(inScan)
3089         {
3090                 // If we're scanning then we need to test for duplicates
3091                 // NB This does not need to be efficient since it should only ever 
3092                 // happen when the power fails during a write, then only one
3093                 // chunk should ever be affected.
3094         
3095                 
3096                 if(existingChunk != 0)
3097                 {
3098                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3099                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3100                         //
3101                         // We have a duplicate now we need to decide which one to use
3102                         // To do this we get both sets of tags and compare serial numbers.
3103                         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3104                         
3105                         
3106                         // Do a proper find
3107                         existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3108
3109                         if(existingChunk <=0)
3110                         {
3111                                 //Hoosterman - how did this happen?
3112                                 
3113                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3114
3115                         }
3116
3117                         
3118                         // NB The deleted flags should be false, otherwise the chunks will 
3119                         // not be loaded during a scan
3120                         
3121                         newSerial = newTags.serialNumber;
3122                         existingSerial = existingTags.serialNumber;
3123                         
3124                         if( in->myDev->isYaffs2 ||
3125                             existingChunk <= 0 ||
3126                             ((existingSerial+1) & 3) == newSerial)
3127                         {
3128                                 // Use new
3129                                 // Delete the old one and drop through to update the tnode
3130                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3131                         }
3132                         else
3133                         {
3134                                 // Use existing.
3135                                 // Delete the new one and return early so that the tnode isn't changed
3136                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3137                                 return YAFFS_OK;
3138                         }
3139                 }
3140
3141         }
3142                 
3143         if(existingChunk == 0)
3144         {
3145                 in->nDataChunks++;
3146         }
3147         
3148         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3149         
3150         return YAFFS_OK;
3151 }
3152
3153
3154
3155 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3156 {
3157     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3158     
3159     if(chunkInNAND >= 0)
3160     {
3161                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3162         }
3163         else
3164         {
3165                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3166
3167                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3168                 return 0;
3169         }
3170
3171 }
3172
3173
3174 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3175 {
3176         int block;
3177         int page;
3178         yaffs_ExtendedTags tags;
3179         yaffs_BlockInfo *bi;
3180         
3181         if(chunkId <= 0) return;        
3182         
3183         dev->nDeletions++;
3184         block = chunkId / dev->nChunksPerBlock;
3185         page = chunkId % dev->nChunksPerBlock;
3186         
3187         bi = yaffs_GetBlockInfo(dev,block);
3188         
3189         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3190         
3191         if(markNAND && 
3192           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3193           !dev->isYaffs2)
3194         {
3195 //              yaffs_SpareInitialise(&spare);
3196
3197 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3198
3199                 //read data before write, to ensure correct ecc 
3200                 //if we're using MTD verification under Linux
3201                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3202 #endif
3203
3204                 yaffs_InitialiseTags(&tags);
3205
3206                 tags.chunkDeleted = 1;
3207
3208         
3209                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3210                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3211         }
3212         else
3213         {
3214                         dev->nUnmarkedDeletions++;
3215         }       
3216         
3217         
3218         // Pull out of the management area.
3219         // If the whole block became dirty, this will kick off an erasure.
3220         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3221             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3222             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3223             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3224         {
3225                 dev->nFreeChunks++;
3226
3227                 yaffs_ClearChunkBit(dev,block,page);
3228                 bi->pagesInUse--;
3229                 
3230                 if(bi->pagesInUse == 0 &&
3231                    !bi->hasShrinkHeader &&
3232                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3233                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3234             {
3235                 yaffs_BlockBecameDirty(dev,block);
3236             }
3237
3238         }
3239         else
3240         {
3241                 // T(("Bad news deleting chunk %d\n",chunkId));
3242         }
3243         
3244 }
3245
3246
3247
3248
3249 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3250 {
3251         // Find old chunk Need to do this to get serial number
3252         // Write new one and patch into tree.
3253         // Invalidate old tags.
3254
3255     int prevChunkId;
3256     yaffs_ExtendedTags prevTags;
3257     
3258     int newChunkId;
3259     yaffs_ExtendedTags newTags;
3260
3261     yaffs_Device *dev = in->myDev;    
3262
3263         yaffs_CheckGarbageCollection(dev);
3264
3265         // Get the previous chunk at this location in the file if it exists
3266     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3267     
3268     // Set up new tags
3269     yaffs_InitialiseTags(&newTags);
3270     
3271         newTags.chunkId = chunkInInode;
3272         newTags.objectId = in->objectId;
3273         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3274         newTags.byteCount = nBytes;
3275                 
3276 //      yaffs_CalcTagsECC(&newTags);
3277
3278         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3279         
3280         if(newChunkId >= 0)
3281         {
3282                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3283                 
3284                 
3285                 if(prevChunkId >= 0)
3286                 {
3287                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3288         
3289                 }
3290                 
3291                 yaffs_CheckFileSanity(in);
3292         }
3293         return newChunkId;
3294
3295
3296
3297
3298
3299 }
3300
3301
3302 // UpdateObjectHeader updates the header on NAND for an object.
3303 // If name is not NULL, then that new name is used.
3304 //
3305 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3306 {
3307
3308         yaffs_BlockInfo *bi;
3309         
3310         yaffs_Device *dev = in->myDev;
3311         
3312     int prevChunkId;
3313     int retVal = 0;
3314     
3315     int newChunkId;
3316     yaffs_ExtendedTags newTags;
3317     
3318     __u8 *buffer = NULL;
3319     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3320     
3321    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3322     
3323     yaffs_ObjectHeader *oh = NULL;
3324     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3325
3326     
3327     if(!in->fake || force)
3328     {
3329   
3330                 yaffs_CheckGarbageCollection(dev);              
3331     
3332             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3333             oh  = (yaffs_ObjectHeader *)buffer;
3334     
3335                 prevChunkId = in->chunkId;
3336     
3337                 if(prevChunkId >= 0)
3338                 {
3339                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3340                         memcpy(oldName,oh->name,sizeof(oh->name));      
3341                 }
3342
3343                 memset(buffer,0xFF,dev->nBytesPerChunk);
3344
3345                 // Header data
3346                 oh->type = in->variantType;
3347                 
3348                 oh->st_mode = in->st_mode;
3349
3350 #ifdef CONFIG_YAFFS_WINCE
3351                 oh->win_atime[0] = in->win_atime[0];
3352                 oh->win_ctime[0] = in->win_ctime[0];
3353                 oh->win_mtime[0] = in->win_mtime[0];
3354                 oh->win_atime[1] = in->win_atime[1];
3355                 oh->win_ctime[1] = in->win_ctime[1];
3356                 oh->win_mtime[1] = in->win_mtime[1];
3357 #else
3358                 oh->st_uid = in->st_uid;
3359                 oh->st_gid = in->st_gid;
3360                 oh->st_atime = in->st_atime;
3361                 oh->st_mtime = in->st_mtime;
3362                 oh->st_ctime = in->st_ctime;
3363                 oh->st_rdev = in->st_rdev;
3364 #endif  
3365                 if(in->parent)
3366                 {
3367                         oh->parentObjectId = in->parent->objectId;
3368                 }
3369                 else
3370                 {
3371                         oh->parentObjectId = 0;
3372                 }
3373                 
3374                 //oh->sum = in->sum;
3375                 if(name && *name)
3376                 {
3377                         memset(oh->name,0,sizeof(oh->name));
3378                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3379                 }
3380                 else if(prevChunkId)
3381                 {       
3382                         memcpy(oh->name, oldName,sizeof(oh->name));
3383                 }
3384                 else
3385                 {
3386                         memset(oh->name,0,sizeof(oh->name));    
3387                 }
3388                 
3389                 oh->isShrink = isShrink;
3390         
3391                 switch(in->variantType)
3392                 {
3393                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3394                                 // Should not happen
3395                                 break;
3396                         case YAFFS_OBJECT_TYPE_FILE:
3397                                 oh->fileSize = in->variant.fileVariant.fileSize;
3398                                 break;
3399                         case YAFFS_OBJECT_TYPE_HARDLINK:
3400                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3401                                 break;
3402                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3403                                 // Do nothing
3404                                 break;
3405                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3406                                 // Do nothing
3407                                 break;
3408                         case YAFFS_OBJECT_TYPE_SYMLINK:
3409                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3410                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3411                                 break;
3412                 }
3413
3414                 // Tags
3415                 yaffs_InitialiseTags(&newTags);
3416                 in->serial++;
3417                 newTags.chunkId = 0;
3418                 newTags.objectId = in->objectId;
3419                 newTags.serialNumber = in->serial;
3420         
3421
3422                 // Create new chunk in NAND
3423                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3424     
3425                 if(newChunkId >= 0)
3426                 {
3427                 
3428                         in->chunkId = newChunkId;       
3429                 
3430                         if(prevChunkId >= 0)
3431                         {
3432                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3433                         }
3434                 
3435                         in->dirty = 0;
3436                         
3437                         // If this was a shrink, then mark the block that the chunk lives on
3438                         if(isShrink)
3439                         {
3440                                 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3441                                 bi->hasShrinkHeader = 1;
3442                         }
3443                         
3444                 }
3445                 
3446                 retVal =  newChunkId;
3447
3448     }
3449     
3450         if(buffer) 
3451                 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3452         
3453     return retVal;
3454 }
3455
3456
3457 /////////////////////// Short Operations Cache ////////////////////////////////
3458 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3459 //      reads might be short sequential reads, and a lot of writes may be short 
3460 //  sequential writes. eg. scanning/writing a jpeg file.
3461 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3462 //  with dumb-as-a-rock code.
3463 //  There are a limited number (~10) of cache chunks per device so that we don't
3464 //  need a very intelligent search.
3465
3466
3467
3468
3469
3470 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3471 {
3472         yaffs_Device *dev = obj->myDev;
3473         int lowest;
3474         int i;
3475         yaffs_ChunkCache *cache;
3476         int chunkWritten;
3477         //int nBytes;
3478         int nCaches = obj->myDev->nShortOpCaches;
3479         
3480         if  (nCaches > 0)
3481         {
3482                 do{
3483                         cache = NULL;
3484                 
3485                         // Find the dirty cache for this object with the lowest chunk id.
3486                         for(i = 0; i < nCaches; i++)
3487                         {
3488                                 if(dev->srCache[i].object == obj &&
3489                                    dev->srCache[i].dirty)
3490                                 {
3491                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3492                                         {
3493                                                 cache = &dev->srCache[i];
3494                                                 lowest = cache->chunkId;
3495                                         }
3496                                 }
3497                         }
3498                 
3499                         if(cache && !cache->locked)
3500                         {
3501                                 //Write it out and free it up
3502
3503 #if 0
3504                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3505                         
3506                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3507                                 {
3508                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3509                                 }
3510 #endif                  
3511                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3512                                                                                                                         cache->chunkId,
3513                                                                                                                         cache->data,
3514                                                                                                                         cache->nBytes,1);
3515
3516                                 cache->dirty = 0;
3517                                 cache->object = NULL;
3518                         }
3519                 
3520                 } while(cache && chunkWritten > 0);
3521         
3522                 if(cache)
3523                 {
3524                         //Hoosterman, disk full while writing cache out.
3525                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3526
3527                 }
3528         }       
3529                 
3530 }
3531
3532
3533 // Grab us a chunk for use.
3534 // First look for an empty one. 
3535 // Then look for the least recently used non-dirty one.
3536 // Then look for the least recently used dirty one...., flush and look again.
3537 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3538 {
3539         int i;
3540         int usage;
3541         int theOne;
3542         
3543         if(dev->nShortOpCaches > 0)
3544         {
3545                 for(i = 0; i < dev->nShortOpCaches; i++)
3546                 {
3547                         if(!dev->srCache[i].object)
3548                         {
3549                                 //T(("Grabbing empty %d\n",i));
3550                                 
3551                                 //printf("Grabbing empty %d\n",i);
3552                         
3553                                 return &dev->srCache[i];
3554                         }
3555                 }
3556                 
3557                 return NULL;
3558         
3559                 theOne = -1; 
3560                 usage = 0; // just to stop the compiler grizzling
3561         
3562                 for(i = 0; i < dev->nShortOpCaches; i++)
3563                 {
3564                         if(!dev->srCache[i].dirty &&
3565                         ((dev->srCache[i].lastUse < usage  && theOne >= 0)|| 
3566                                 theOne < 0))
3567                         {
3568                                 usage = dev->srCache[i].lastUse;
3569                                 theOne = i;
3570                         }
3571                 }
3572         
3573                 //T(("Grabbing non-empty %d\n",theOne));
3574                 
3575                 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3576                 
3577                 return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
3578         }
3579         else
3580         {
3581                 return NULL;
3582         }
3583         
3584 }
3585
3586
3587 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3588 {
3589         yaffs_ChunkCache *cache;
3590         yaffs_Object *theObj;
3591         int usage;
3592         int i;
3593         int pushout;
3594         
3595         if(dev->nShortOpCaches > 0)
3596         {
3597                 // Try find a non-dirty one...
3598         
3599                 cache = yaffs_GrabChunkCacheWorker(dev);
3600         
3601                 if(!cache)
3602                 {
3603                         // They were all dirty, find the last recently used object and flush
3604                         // its cache, then  find again.
3605                         // NB what's here is not very accurate, we actually flush the object
3606                         // the last recently used page.
3607                 
3608                         theObj = NULL;
3609                         usage = 0;
3610                         cache = NULL;
3611                         pushout = 0;
3612         
3613                         for(i = 0; i < dev->nShortOpCaches; i++)
3614                         {
3615                                 if( dev->srCache[i].object && 
3616                                         !dev->srCache[i].locked &&
3617                                         (dev->srCache[i].lastUse < usage || !cache))
3618                                 {
3619                                         usage  = dev->srCache[i].lastUse;
3620                                         theObj = dev->srCache[i].object;
3621                                         cache = &dev->srCache[i];
3622                                         pushout = i;
3623                                 }
3624                         }
3625                 
3626                         if(!cache || cache->dirty)
3627                         {
3628                         
3629                                 //printf("Dirty ");
3630                                 yaffs_FlushFilesChunkCache(theObj);
3631                 
3632                                 // Try again
3633                                 cache = yaffs_GrabChunkCacheWorker(dev);
3634                         }
3635                         else
3636                         {
3637                                 //printf(" pushout %d\n",pushout);
3638                         }
3639                         
3640                 }
3641
3642                 return cache;
3643         }
3644         else
3645                 return NULL;
3646
3647 }
3648
3649
3650 // Find a cached chunk
3651 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3652 {
3653         yaffs_Device *dev = obj->myDev;
3654         int i;
3655         if(dev->nShortOpCaches > 0)
3656         {
3657                 for(i = 0; i < dev->nShortOpCaches; i++)
3658                 {
3659                         if(dev->srCache[i].object == obj && 
3660                         dev->srCache[i].chunkId == chunkId)
3661                         {
3662                                 dev->cacheHits++;
3663                         
3664                                 return &dev->srCache[i];
3665                         }
3666                 }
3667         }
3668         return NULL;
3669 }
3670
3671 // Mark the chunk for the least recently used algorithym
3672 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3673 {
3674
3675         if(dev->nShortOpCaches > 0)
3676         {
3677                 if( dev->srLastUse < 0 || 
3678                         dev->srLastUse > 100000000)
3679                 {
3680                         // Reset the cache usages
3681                         int i;
3682                         for(i = 1; i < dev->nShortOpCaches; i++)
3683                         {
3684                                 dev->srCache[i].lastUse = 0;
3685                         }
3686                         dev->srLastUse = 0;
3687                 }
3688
3689                 dev->srLastUse++;
3690         
3691                 cache->lastUse = dev->srLastUse;
3692
3693                 if(isAWrite)
3694                 {
3695                         cache->dirty = 1;
3696                 }
3697         }
3698 }
3699
3700 // Invalidate a single cache page.
3701 // Do this when a whole page gets written,
3702 // ie the short cache for this page is no longer valid.
3703 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3704 {
3705         if(object->myDev->nShortOpCaches > 0)
3706         {
3707                 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3708
3709                 if(cache)
3710                 {
3711                         cache->object = NULL;
3712                 }
3713         }
3714 }
3715
3716
3717 // Invalidate all the cache pages associated with this object
3718 // Do this whenever ther file is deleted or resized.
3719 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3720 {
3721         int i;
3722         yaffs_Device *dev = in->myDev;
3723         
3724         if(dev->nShortOpCaches > 0)
3725         { 
3726                 // Now invalidate it.
3727                 for(i = 0; i < dev->nShortOpCaches; i++)
3728                 {
3729                         if(dev->srCache[i].object == in)
3730                         {
3731                                 dev->srCache[i].object = NULL;
3732                         }
3733                 }
3734         }
3735 }
3736
3737
3738
3739
3740
3741 ///////////////////////// File read/write ///////////////////////////////
3742 // Read and write have very similar structures.
3743 // In general the read/write has three parts to it
3744 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3745 // * Some complete chunks
3746 // * An incomplete chunk to end off with
3747 //
3748 // Curve-balls: the first chunk might also be the last chunk.
3749
3750 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3751 {
3752         
3753         
3754         int chunk;
3755         int start;
3756         int nToCopy;
3757         int n = nBytes;
3758         int nDone = 0;
3759         yaffs_ChunkCache *cache;
3760         
3761         yaffs_Device *dev;
3762         
3763         dev = in->myDev;
3764         
3765         while(n > 0)
3766         {
3767                 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3768                 start = offset % dev->nBytesPerChunk;
3769
3770                 // OK now check for the curveball where the start and end are in
3771                 // the same chunk.      
3772                 if(     (start + n) < dev->nBytesPerChunk)
3773                 {
3774                         nToCopy = n;
3775                 }
3776                 else
3777                 {
3778                         nToCopy = dev->nBytesPerChunk - start;
3779                 }
3780         
3781                 cache = yaffs_FindChunkCache(in,chunk);
3782                 
3783                 // If the chunk is already in the cache or it is less than a whole chunk
3784                 // then use the cache (if there is caching)
3785                 // else bypass the cache.
3786                 if( cache || nToCopy != dev->nBytesPerChunk)
3787                 {
3788                         if(dev->nShortOpCaches > 0)
3789                         {
3790                                 
3791                                 // If we can't find the data in the cache, then load it up.
3792                                 
3793                                 if(!cache)
3794                                 {
3795                                         cache = yaffs_GrabChunkCache(in->myDev);
3796                                         cache->object = in;
3797                                         cache->chunkId = chunk;
3798                                         cache->dirty = 0;
3799                                         cache->locked = 0;
3800                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3801                                         cache->nBytes = 0;      
3802                                 }
3803                         
3804                                 yaffs_UseChunkCache(dev,cache,0);
3805
3806                                 cache->locked = 1;
3807
3808 #ifdef CONFIG_YAFFS_WINCE
3809                                 yfsd_UnlockYAFFS(TRUE);
3810 #endif
3811                                 memcpy(buffer,&cache->data[start],nToCopy);
3812
3813 #ifdef CONFIG_YAFFS_WINCE
3814                                 yfsd_LockYAFFS(TRUE);
3815 #endif
3816                                 cache->locked = 0;
3817                         }
3818                         else
3819                         {
3820                                 // Read into the local buffer then copy...
3821                                 
3822                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3823                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);            
3824 #ifdef CONFIG_YAFFS_WINCE
3825                                 yfsd_UnlockYAFFS(TRUE);
3826 #endif
3827                                 memcpy(buffer,&localBuffer[start],nToCopy);
3828
3829 #ifdef CONFIG_YAFFS_WINCE
3830                                 yfsd_LockYAFFS(TRUE);
3831 #endif
3832                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3833                         }
3834
3835                 }
3836                 else
3837                 {
3838 #ifdef CONFIG_YAFFS_WINCE
3839                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3840                         
3841                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3842                         // This is because we otherwise screw up WinCE's memory mapper
3843                         yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3844
3845 #ifdef CONFIG_YAFFS_WINCE
3846                                 yfsd_UnlockYAFFS(TRUE);
3847 #endif
3848                         memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3849
3850 #ifdef CONFIG_YAFFS_WINCE
3851                                 yfsd_LockYAFFS(TRUE);
3852                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3853 #endif
3854
3855 #else
3856                         // A full chunk. Read directly into the supplied buffer.
3857                         yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3858 #endif
3859                 }
3860                 
3861                 n -= nToCopy;
3862                 offset += nToCopy;
3863                 buffer += nToCopy;
3864                 nDone += nToCopy;
3865                 
3866         }
3867         
3868         return nDone;
3869 }
3870
3871
3872
3873 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3874 {       
3875         
3876         int chunk;
3877         int start;
3878         int nToCopy;
3879         int n = nBytes;
3880         int nDone = 0;
3881         int nToWriteBack;
3882         int startOfWrite = offset;
3883         int chunkWritten = 0;
3884         int nBytesRead;
3885         
3886         yaffs_Device *dev;
3887         
3888         dev = in->myDev;
3889         
3890         
3891         while(n > 0 && chunkWritten >= 0)
3892         {
3893                 chunk = offset / dev->nBytesPerChunk + 1;
3894                 start = offset % dev->nBytesPerChunk;
3895                 
3896
3897                 // OK now check for the curveball where the start and end are in
3898                 // the same chunk.
3899                 
3900                 if(     (start + n) < dev->nBytesPerChunk)
3901                 {
3902                         nToCopy = n;
3903                         
3904                         // Now folks, to calculate how many bytes to write back....
3905                         // If we're overwriting and not writing to then end of file then
3906                         // we need to write back as much as was there before.
3907                         
3908                         nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3909                         
3910                         if(nBytesRead > dev->nBytesPerChunk)
3911                         {
3912                                 nBytesRead = dev->nBytesPerChunk;
3913                         }
3914                         
3915                         nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3916                         
3917                 }
3918                 else
3919                 {
3920                         nToCopy = dev->nBytesPerChunk - start;
3921                         nToWriteBack = dev->nBytesPerChunk;
3922                 }
3923         
3924                 if(nToCopy != dev->nBytesPerChunk)
3925                 {
3926                         // An incomplete start or end chunk (or maybe both start and end chunk)
3927                         if(dev->nShortOpCaches > 0)
3928                         {
3929                                 yaffs_ChunkCache *cache;
3930                                 // If we can't find the data in the cache, then load it up.
3931                                 cache = yaffs_FindChunkCache(in,chunk);
3932                                 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3933                                 {
3934                                         cache = yaffs_GrabChunkCache(in->myDev);
3935                                         cache->object = in;
3936                                         cache->chunkId = chunk;
3937                                         cache->dirty = 0;
3938                                         cache->locked = 0;
3939                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);            
3940                                 }
3941                         
3942                                 if(cache)
3943                                 {       
3944                                         yaffs_UseChunkCache(dev,cache,1);
3945                                         cache->locked = 1;
3946 #ifdef CONFIG_YAFFS_WINCE
3947                                 yfsd_UnlockYAFFS(TRUE);
3948 #endif
3949         
3950                                         memcpy(&cache->data[start],buffer,nToCopy);
3951
3952 #ifdef CONFIG_YAFFS_WINCE
3953                                 yfsd_LockYAFFS(TRUE);
3954 #endif
3955                                         cache->locked = 0;
3956                                         cache->nBytes = nToWriteBack;
3957                                 }
3958                                 else
3959                                 {
3960                                         chunkWritten = -1; // fail the write
3961                                 }
3962                         }
3963                         else
3964                         {
3965                                 // An incomplete start or end chunk (or maybe both start and end chunk)
3966                                 // Read into the local buffer then copy, then copy over and write back.
3967                                 
3968                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3969                 
3970                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3971                                 
3972 #ifdef CONFIG_YAFFS_WINCE
3973                                 yfsd_UnlockYAFFS(TRUE);
3974 #endif
3975                                         
3976                      &nbs