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