*** empty log message ***
[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.17 2003-01-17 04:19:08 charles Exp $";
18
19 #include "yportenv.h"
20
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
23
24
25
26
27 #define YAFFS_PASSIVE_GC_CHUNKS 2
28
29 // External functions for ECC on data
30 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
31 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
32
33
34 // countBits is a quick way of counting the number of bits in a byte.
35 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
36
37 static const char yaffs_countBitsTable[256] =
38 {
39 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
40 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
41 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
42 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
43 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
44 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
45 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
46 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
47 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
48 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
49 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
50 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
51 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
52 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
53 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
54 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
55 };
56
57 static int yaffs_CountBits(__u8 x)
58 {
59         int retVal;
60         retVal = yaffs_countBitsTable[x];
61         return retVal;
62 }
63
64
65
66 // Local prototypes
67 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
68 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
69 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
70 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
71
72 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
73 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
74 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force);
75 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND);
76 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
77 static int yaffs_CheckStructures(void);
78 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
79 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
80
81 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
82
83 // Robustification
84 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
85 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
86 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
87 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
88 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
89
90 static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
91
92 static int yaffs_UnlinkWorker(yaffs_Object *obj);
93 static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj);
94
95 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1);
96
97
98
99 loff_t yaffs_GetFileSize(yaffs_Object *obj);
100
101 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
102 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
103
104
105 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
106
107 #ifdef YAFFS_PARANOID
108 static int yaffs_CheckFileSanity(yaffs_Object *in);
109 #else
110 #define yaffs_CheckFileSanity(in)
111 #endif
112
113 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
114 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
115
116 // Chunk bitmap manipulations
117
118 static __inline __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
119 {
120         if(blk < dev->startBlock || blk > dev->endBlock)
121         {
122                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
123                 YBUG();
124         }
125         return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock));
126 }
127
128 static __inline__ void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
129 {
130         __u8 *blkBits = yaffs_BlockBits(dev,blk);
131
132          memset(blkBits,0,dev->chunkBitmapStride);
133 }
134
135 static __inline__ void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
136 {
137         __u8 *blkBits = yaffs_BlockBits(dev,blk);
138         
139         blkBits[chunk/8] &=  ~ (1<<(chunk & 7));
140 }
141
142 static __inline__ void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
143 {
144         __u8 *blkBits = yaffs_BlockBits(dev,blk);
145
146         blkBits[chunk/8] |=   (1<<(chunk & 7));
147 }
148
149 static __inline__ int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
150 {
151         __u8 *blkBits = yaffs_BlockBits(dev,blk);
152         return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;
153 }
154
155 static __inline__ int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
156 {
157         __u8 *blkBits = yaffs_BlockBits(dev,blk);
158         int i;
159         for(i = 0; i < dev->chunkBitmapStride; i++)
160         {
161                 if(*blkBits) return 1;
162                 blkBits++;
163         }
164         return 0;
165 }
166
167 // Function to manipulate block info
168 static  __inline__ yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk)
169 {
170         if(blk < dev->startBlock || blk > dev->endBlock)
171         {
172                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk));
173                 YBUG();
174         }
175         return &dev->blockInfo[blk - dev->startBlock];
176 }
177
178
179 static  __inline__ int yaffs_HashFunction(int n)
180 {
181         return (n % YAFFS_NOBJECT_BUCKETS);
182 }
183
184
185 yaffs_Object *yaffs_Root(yaffs_Device *dev)
186 {
187         return dev->rootDir;
188 }
189
190 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
191 {
192         return dev->lostNFoundDir;
193 }
194
195
196 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
197 {
198         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
199         {
200                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
201                 return YAFFS_FAIL;
202         }
203
204         dev->nPageWrites++;
205         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
206 }
207
208
209
210 int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
211                                                         int chunkInNAND, 
212                                                         __u8 *data, 
213                                                         yaffs_Spare *spare, 
214                                                         int doErrorCorrection)
215 {
216         int retVal;
217         yaffs_Spare localSpare;
218
219         __u8 calcEcc[3];
220         int eccResult1,eccResult2;
221         struct yaffs_NANDSpare nspare;
222
223         dev->nPageReads++;
224         
225         
226
227         
228         if(!spare && data)
229         {
230                 // If we don't have a real spare, then we use a local one.
231                 // Need this for the calculation of the ecc
232                 spare = &localSpare;
233         }
234         
235
236         if(!dev->useNANDECC)
237         {
238                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
239                 if(data && doErrorCorrection)
240                 {
241                         // Do ECC correction
242                         //Todo handle any errors
243                         nand_calculate_ecc(data,calcEcc);
244                         eccResult1 = nand_correct_data (data,spare->ecc1, calcEcc);
245                         nand_calculate_ecc(&data[256],calcEcc);
246                         eccResult2 = nand_correct_data (&data[256],spare->ecc2, calcEcc);
247
248                         if(eccResult1>0)
249                         {
250                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
251                                 dev->eccFixed++;
252                         }
253                         else if(eccResult1<0)
254                         {
255                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
256                                 dev->eccUnfixed++;
257                         }
258
259                         if(eccResult2>0)
260                         {
261                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
262                                 dev->eccFixed++;
263                         }
264                         else if(eccResult2<0)
265                         {
266                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
267                                 dev->eccUnfixed++;
268                         }
269
270                         if(eccResult1 || eccResult2)
271                         {
272                                 // Hoosterman, we had a data problem on this page
273                                 yaffs_HandleReadDataError(dev,chunkInNAND);
274                         }
275                 }
276         }
277         else
278         {
279                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
280                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
281                 if(data && doErrorCorrection)
282                 {
283                         if(nspare.eccres1>0)
284                         {
285                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
286                         }
287                         else if(nspare.eccres1<0)
288                         {
289                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
290                         }
291
292                         if(nspare.eccres2>0)
293                         {
294                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
295                         }
296                         else if(nspare.eccres2<0)
297                         {
298                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
299                         }
300
301                         if(nspare.eccres2 || nspare.eccres2)
302                         {
303                                 // Hoosterman, we had a data problem on this page
304                                 yaffs_HandleReadDataError(dev,chunkInNAND);
305                         }
306
307                 }
308         }
309         return retVal;
310 }
311
312
313 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
314 {
315
316         static int init = 0;
317         static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
318         static __u8 data[YAFFS_BYTES_PER_CHUNK];
319         static __u8 spare[16];
320         
321         
322         dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare);
323         
324         
325         
326         if(!init)
327         {
328                 memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);
329                 init = 1;
330         }
331         
332         if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return  YAFFS_FAIL;
333         if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;
334
335         
336         return YAFFS_OK;
337         
338 }
339
340
341
342 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
343 {
344         dev->nBlockErasures++;
345         return dev->eraseBlockInNAND(dev,blockInNAND);
346 }
347
348 int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
349 {
350         return dev->initialiseNAND(dev);
351 }
352
353 static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve)
354 {
355         int chunk;
356         
357         int writeOk = 1;
358         int attempts = 0;
359         
360         unsigned char rbData[YAFFS_BYTES_PER_CHUNK];
361         yaffs_Spare rbSpare;
362         
363         do{
364                 chunk = yaffs_AllocateChunk(dev,useReserve);
365         
366                 if(chunk >= 0)
367                 {
368
369                         // First check this chunk is erased...
370 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
371                         writeOk = yaffs_CheckChunkErased(dev,chunk);
372 #endif          
373                         if(!writeOk)
374                         {
375                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
376                         }
377                         else
378                         {
379                                 writeOk =  yaffs_WriteChunkToNAND(dev,chunk,data,spare);
380                         }
381                         attempts++;
382                         if(writeOk)
383                         {
384                                 // Readback & verify
385                                 // If verify fails, then delete this chunk and try again
386                                 // To verify we compare everything except the block and 
387                                 // page status bytes.
388                                 // NB We check a raw read without ECC correction applied
389                                 yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0);
390                                 
391 #ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
392                                 if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare))
393                                 {
394                                         // Didn't verify
395                                         T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk));
396
397                                         writeOk = 0;
398                                 }       
399 #endif                          
400                                 
401                         }
402                         if(writeOk)
403                         {
404                                 // Copy the data into the write buffer.
405                                 // NB We do this at the end to prevent duplicates in the case of a write error.
406                                 //Todo
407                                 yaffs_HandleWriteChunkOk(dev,chunk,data,spare);
408                         }
409                         else
410                         {
411                                 yaffs_HandleWriteChunkError(dev,chunk);
412                         }
413                 }
414                 
415         } while(chunk >= 0 && ! writeOk);
416         
417         if(attempts > 1)
418         {
419                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
420                 dev->nRetriedWrites+= (attempts - 1);   
421         }
422         
423         return chunk;
424 }
425
426 ///
427 // Functions for robustisizing
428 //
429 //
430
431 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
432 {
433         // Ding the blockStatus in the first two pages of the block.
434         
435         yaffs_Spare spare;
436
437         memset(&spare, 0xff,sizeof(yaffs_Spare));
438
439         spare.blockStatus = 0;
440         
441         // TODO change this retirement marking for other NAND types
442         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);
443         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);
444         
445         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
446         dev->nRetiredBlocks++;
447 }
448
449
450
451 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
452 {
453         dev->doingBufferedBlockRewrite = 1;
454         //
455         //      Remove erased chunks
456         //  Rewrite existing chunks to a new block
457         //      Set current write block to the new block
458         
459         dev->doingBufferedBlockRewrite = 0;
460         
461         return 1;
462 }
463
464
465 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
466 {
467         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
468
469         // Mark the block for retirement
470         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
471         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
472
473
474         //TODO  
475         // Just do a garbage collection on the affected block then retire the block
476         // NB recursion
477 }
478
479
480 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
481 {
482 }
483
484 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
485 {
486 }
487
488 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
489 {
490 }
491
492 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
493 {
494         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
495
496         // Mark the block for retirement
497         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
498         // Delete the chunk
499         yaffs_DeleteChunk(dev,chunkInNAND,1);
500 }
501
502
503
504
505 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
506 {
507
508
509         if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
510                 s0->tagByte0 != s1->tagByte0 ||
511                 s0->tagByte1 != s1->tagByte1 ||
512                 s0->tagByte2 != s1->tagByte2 ||
513                 s0->tagByte3 != s1->tagByte3 ||
514                 s0->tagByte4 != s1->tagByte4 ||
515                 s0->tagByte5 != s1->tagByte5 ||
516                 s0->tagByte6 != s1->tagByte6 ||
517                 s0->tagByte7 != s1->tagByte7 ||
518                 s0->ecc1[0]  != s1->ecc1[0]  ||
519                 s0->ecc1[1]  != s1->ecc1[1]  ||
520                 s0->ecc1[2]  != s1->ecc1[2]  ||
521                 s0->ecc2[0]  != s1->ecc2[0]  ||
522                 s0->ecc2[1]  != s1->ecc2[1]  ||
523                 s0->ecc2[2]  != s1->ecc2[2] )
524                 {
525                         return 0;
526                 }
527         
528         return 1;
529 }
530
531
532 ///////////////////////// Object management //////////////////
533 // List of spare objects
534 // The list is hooked together using the first pointer
535 // in the object
536
537 // static yaffs_Object *yaffs_freeObjects = NULL;
538
539 // static int yaffs_nFreeObjects;
540
541 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
542
543 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
544
545
546 static __u16 yaffs_CalcNameSum(const char *name)
547 {
548         __u16 sum = 0;
549         __u16 i = 1;
550         
551         __u8 *bname = (__u8 *)name;
552         if(bname)
553         {
554                 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
555                 {
556
557 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
558                         sum += toupper(*bname) * i;
559 #else
560                         sum += (*bname) * i;
561 #endif
562                         i++;
563                         bname++;
564                 }
565         }
566         return sum;
567 }
568
569 void yaffs_SetObjectName(yaffs_Object *obj, const char *name)
570 {
571 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
572                                         if(name && strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
573                                         {
574                                                 strcpy(obj->shortName,name);
575                                         }
576                                         else
577                                         {
578                                                 obj->shortName[0]='\0';
579                                         }
580 #endif
581                                         obj->sum = yaffs_CalcNameSum(name);
582 }
583
584 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
585 {
586         nand_calculate_ecc (data , spare->ecc1);
587         nand_calculate_ecc (&data[256] , spare->ecc2);
588 }
589
590 void yaffs_CalcTagsECC(yaffs_Tags *tags)
591 {
592         // Calculate an ecc
593         
594         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
595         unsigned  i,j;
596         unsigned  ecc = 0;
597         unsigned bit = 0;
598
599         tags->ecc = 0;
600         
601         for(i = 0; i < 8; i++)
602         {
603                 for(j = 1; j &0xff; j<<=1)
604                 {
605                         bit++;
606                         if(b[i] & j)
607                         {
608                                 ecc ^= bit;
609                         }
610                 }
611         }
612         
613         tags->ecc = ecc;
614         
615         
616 }
617
618 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
619 {
620         unsigned ecc = tags->ecc;
621         
622         yaffs_CalcTagsECC(tags);
623         
624         ecc ^= tags->ecc;
625         
626         if(ecc && ecc <= 64)
627         {
628                 // TODO: Handle the failure better. Retire?
629                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
630
631                 ecc--;
632                                 
633                 b[ecc / 8] ^= (1 << (ecc & 7));
634                 
635                 // Now recvalc the ecc
636                 yaffs_CalcTagsECC(tags);
637                 
638                 return 1; // recovered error
639         }
640         else if(ecc)
641         {
642                 // Wierd ecc failure value
643                 // TODO Need to do somethiong here
644                 return -1; //unrecovered error
645         }
646         
647         return 0;
648 }
649
650
651 ///////////////////////// TNODES ///////////////////////
652
653 // List of spare tnodes
654 // The list is hooked together using the first pointer
655 // in the tnode.
656
657 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
658
659 // static int yaffs_nFreeTnodes;
660
661 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
662
663
664
665 // yaffs_CreateTnodes creates a bunch more tnodes and
666 // adds them to the tnode free list.
667 // Don't use this function directly
668
669 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
670 {
671     int i;
672     yaffs_Tnode *newTnodes;
673     yaffs_TnodeList *tnl;
674     
675     if(nTnodes < 1) return YAFFS_OK;
676    
677         // make these things
678         
679     newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
680    
681     if (!newTnodes)
682     {
683                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
684                 return YAFFS_FAIL;
685     }
686     
687     // Hook them into the free list
688     for(i = 0; i < nTnodes - 1; i++)
689     {
690         newTnodes[i].internal[0] = &newTnodes[i+1];
691 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
692         newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
693 #endif
694     }
695         
696         newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
697 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
698         newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
699 #endif
700         dev->freeTnodes = newTnodes;
701         dev->nFreeTnodes+= nTnodes;
702         dev->nTnodesCreated += nTnodes;
703
704         // Now add this bunch of tnodes to a list for freeing up.
705         // NB If we can't add this to the management list it isn't fatal
706         // but it just means we can't free this bunch of tnodes later.
707         tnl = YMALLOC(sizeof(yaffs_TnodeList));
708         if(!tnl)
709         {
710                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
711                 
712         }
713         else
714         {
715                 tnl->tnodes = newTnodes;
716                 tnl->next = dev->allocatedTnodeList;
717                 dev->allocatedTnodeList = tnl;
718         }
719
720
721         T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
722
723
724         return YAFFS_OK;
725 }
726
727
728 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
729 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
730 {
731         yaffs_Tnode *tn = NULL;
732         
733         // If there are none left make more
734         if(!dev->freeTnodes)
735         {
736                 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
737         }
738         
739         if(dev->freeTnodes)
740         {
741                 tn = dev->freeTnodes;
742 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
743         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
744                 {
745                         // Hoosterman, this thing looks like it isn't in the list
746                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
747                 }
748 #endif
749                 dev->freeTnodes = dev->freeTnodes->internal[0];
750                 dev->nFreeTnodes--;
751                 // zero out
752                 memset(tn,0,sizeof(yaffs_Tnode));
753         }
754         
755
756         return tn;
757 }
758
759
760 // FreeTnode frees up a tnode and puts it back on the free list
761 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
762 {
763         if(tn)
764         {
765 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
766         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
767                 {
768                         // Hoosterman, this thing looks like it is already in the list
769                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
770                 }
771                 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
772 #endif
773                 tn->internal[0] = dev->freeTnodes;
774                 dev->freeTnodes = tn;
775                 dev->nFreeTnodes++;
776         }
777 }
778
779
780 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
781 {
782         // Free the list of allocated tnodes
783         
784         while(dev->allocatedTnodeList)
785         {
786                 YFREE(dev->allocatedTnodeList->tnodes);
787                 dev->allocatedTnodeList =  dev->allocatedTnodeList->next;
788         }
789         
790         dev->freeTnodes = NULL;
791         dev->nFreeTnodes = 0;
792 }
793
794 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
795 {
796         dev->allocatedTnodeList = NULL;
797         dev->freeTnodes = NULL;
798         dev->nFreeTnodes = 0;
799         dev->nTnodesCreated = 0;
800
801 }
802
803 #if 0
804 void yaffs_TnodeTest(yaffs_Device *dev)
805 {
806
807         int i;
808         int j;
809         yaffs_Tnode *tn[1000];
810         
811         YINFO("Testing TNodes");
812         
813         for(j = 0; j < 50; j++)
814         {
815                 for(i = 0; i < 1000; i++)
816                 {
817                         tn[i] = yaffs_GetTnode(dev);
818                         if(!tn[i])
819                         {
820                                 YALERT("Getting tnode failed");
821                         }
822                 }
823                 for(i = 0; i < 1000; i+=3)
824                 {
825                         yaffs_FreeTnode(dev,tn[i]);
826                         tn[i] = NULL;
827                 }
828                 
829         }
830 }
831 #endif
832
833
834 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
835
836 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
837 // The look up tree is represented by the top tnode and the number of topLevel
838 // in the tree. 0 means only the level 0 tnode is in the tree.
839
840
841 // FindLevel0Tnode finds the level 0 tnode, if one exists.
842 // Used when reading.....
843 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
844 {
845         
846         yaffs_Tnode *tn = fStruct->top;
847         __u32 i;
848         int requiredTallness;   
849         int level = fStruct->topLevel;
850         
851         // Check sane level and chunk Id
852         if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
853         {
854 //              char str[50];
855 //              sprintf(str,"Bad level %d",level);
856 //              YALERT(str);
857                 return NULL;
858         }
859         
860         if(chunkId > YAFFS_MAX_CHUNK_ID)
861         {
862 //              char str[50];
863 //              sprintf(str,"Bad chunkId %d",chunkId);
864 //              YALERT(str);
865                 return NULL;
866         }
867
868         // First check we're tall enough (ie enough topLevel)
869         
870         i = chunkId >> (/*dev->chunkGroupBits  + */YAFFS_TNODES_LEVEL0_BITS);
871         requiredTallness = 0;
872         while(i)
873         {
874                 i >>= YAFFS_TNODES_INTERNAL_BITS;
875                 requiredTallness++;
876         }
877         
878         
879         if(requiredTallness > fStruct->topLevel)
880         {
881                 // Not tall enough, so we can't find it, return NULL.
882                 return NULL;
883         }
884                 
885         
886         // Traverse down to level 0
887         while (level > 0 && tn)
888         {
889             tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
890                                YAFFS_TNODES_INTERNAL_MASK]; 
891                 level--;
892         
893         }
894         
895         return tn;              
896 }
897
898 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
899 // This happens in two steps:
900 //  1. If the tree isn't tall enough, then make it taller.
901 //  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
902 //
903 // Used when modifying the tree.
904 //
905 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
906 {
907         
908         yaffs_Tnode *tn; 
909         
910         int requiredTallness;
911         int i;
912         int l;
913         
914         __u32 x;
915                 
916         
917         //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
918         
919         // Check sane level and page Id
920         if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
921         {
922 //              char str[50];
923 //              sprintf(str,"Bad level %d",fStruct->topLevel);
924 //              YALERT(str);
925                 return NULL;
926         }
927         
928         if(chunkId > YAFFS_MAX_CHUNK_ID)
929         {
930 //              char str[50];
931 //              sprintf(str,"Bad chunkId %d",chunkId);
932 //              YALERT(str);
933                 return NULL;
934         }
935         
936         // First check we're tall enough (ie enough topLevel)
937         
938         x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
939         requiredTallness = 0;
940         while(x)
941         {
942                 x >>= YAFFS_TNODES_INTERNAL_BITS;
943                 requiredTallness++;
944         }
945         
946         //T((TSTR(" required=%d"),requiredTallness));
947         
948         
949         if(requiredTallness > fStruct->topLevel)
950         {
951                 // Not tall enough,gotta make the tree taller
952                 for(i = fStruct->topLevel; i < requiredTallness; i++)
953                 {
954                         //T((TSTR(" add new top")));
955                         
956                         tn = yaffs_GetTnode(dev);
957                         
958                         if(tn)
959                         {
960                                 tn->internal[0] = fStruct->top;
961                                 fStruct->top = tn;
962                         }
963                         else
964                         {
965                                         T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
966                         }
967                 }
968                 
969                 fStruct->topLevel = requiredTallness;
970         }
971         
972         
973         // Traverse down to level 0, adding anything we need
974         
975         l = fStruct->topLevel;
976         tn = fStruct->top;
977         while (l > 0 && tn)
978         {
979                 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
980                                YAFFS_TNODES_INTERNAL_MASK;
981                                
982                 //T((TSTR(" [%d:%d]"),l,i));
983                 
984             if(!tn->internal[x])
985             {
986                 //T((TSTR(" added")));
987                 
988                 tn->internal[x] = yaffs_GetTnode(dev);
989             }
990             
991             tn =        tn->internal[x];
992                 l--;
993         
994         }
995         
996         //TSTR(TENDSTR)));
997         
998         return tn;              
999 }
1000
1001 // DeleteWorker scans backwards through the tnode tree and deletes all the
1002 // chunks and tnodes in the file
1003 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
1004
1005 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
1006 {
1007         int i;
1008         int chunkInInode;
1009         int theChunk;
1010         yaffs_Tags tags;
1011         int found;
1012         int chunkDeleted;
1013         int allDone = 1;
1014         
1015         
1016         if(tn)
1017         {
1018                 if(level > 0)
1019                 {
1020                 
1021                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1022                         {
1023                             if(tn->internal[i])
1024                         {
1025                                         if(limit && (*limit) < 0)
1026                                         {
1027                                                 allDone = 0;
1028                                         }
1029                                         else
1030                                         {
1031                                                 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1032                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1033                                         }
1034                                         if(allDone)
1035                                         {
1036                                                 yaffs_FreeTnode(in->myDev,tn->internal[i]);
1037                                         tn->internal[i] = NULL;
1038                                         }
1039                             }
1040                     
1041                         }
1042                         return (allDone) ? 1 : 0;
1043                 }
1044                 else if(level == 0)
1045                 {
1046                         int hitLimit = 0;
1047                         
1048                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1049                         {
1050                             if(tn->level0[i])
1051                         {
1052                                         int j;
1053                                         
1054                                         chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1055                                         
1056                                         theChunk =  tn->level0[i] << in->myDev->chunkGroupBits;
1057
1058                                         // Now we need to search for it
1059                                         for(j = 0,found = 0; theChunk && j < in->myDev->chunkGroupSize && !found; j++)
1060                                         {
1061                                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,&tags,&chunkDeleted);
1062                                                 if(yaffs_TagsMatch(&tags,in->objectId,chunkInInode,chunkDeleted))
1063                                                 {
1064                                                         // found it;
1065                                                         found = 1;
1066                                         
1067                                                 }
1068                                                 else
1069                                                 {
1070                                                         theChunk++;
1071                                                 }
1072                                         }
1073                                         
1074                                         if(found)
1075                                         {
1076                                                 yaffs_DeleteChunk(in->myDev,theChunk,1);
1077                                                 in->nDataChunks--;
1078                                                 if(limit)
1079                                                 { 
1080                                                         *limit = *limit-1;
1081                                                         if(limit <= 0) 
1082                                                         { 
1083                                                                 hitLimit = 1;
1084                                                         }
1085                                                 }
1086                                         
1087                                         }
1088                                         
1089                                 tn->level0[i] = 0;
1090                             }
1091                     
1092                         }
1093                         return (i < 0) ? 1 : 0;
1094
1095                         
1096                 }
1097                 
1098         }
1099         
1100         return 1;
1101         
1102 }
1103
1104 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1105 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1106 // of the tnode.
1107 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1108 //
1109 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1110 {
1111         int i;
1112         int chunkInInode;
1113         int theChunk;
1114         yaffs_BlockInfo *theBlock;
1115         yaffs_Tags tags;
1116         int found;
1117         int chunkDeleted;
1118         int allDone = 1;
1119         
1120         
1121         if(tn)
1122         {
1123                 if(level > 0)
1124                 {
1125                 
1126                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1127                         {
1128                             if(tn->internal[i])
1129                         {
1130                                                 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1131                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1132                                         if(allDone)
1133                                         {
1134                                                 yaffs_FreeTnode(in->myDev,tn->internal[i]);
1135                                         tn->internal[i] = NULL;
1136                                         }
1137                                         else
1138                                         {
1139                                                 //Hoosterman... how could this happen.
1140                                         }                           
1141                                 }                   
1142                         }
1143                         return (allDone) ? 1 : 0;
1144                 }
1145                 else if(level == 0)
1146                 {
1147                         
1148                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1149                         {
1150                             if(tn->level0[i])
1151                         {
1152                                         
1153                                         theChunk =  (tn->level0[i] << in->myDev->chunkGroupBits);
1154                                         theBlock =      yaffs_GetBlockInfo(in->myDev,  theChunk/in->myDev->nChunksPerBlock);
1155                                         if(theBlock)
1156                                         {
1157                                                 theBlock->softDeletions++;
1158                                         }
1159                                 tn->level0[i] = 0;
1160                             }
1161                     
1162                         }
1163                         return 1;
1164                         
1165                 }
1166                 
1167         }
1168         
1169         return 1;
1170                 
1171 }
1172
1173
1174
1175 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1176 {
1177         if(obj->deleted &&
1178            obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1179            !obj->softDeleted)
1180         {
1181                 if(obj->nDataChunks <= 0)
1182                 {
1183                                 // Empty file, just delete it immediately
1184                                 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1185                                 obj->variant.fileVariant.top = NULL;
1186                                 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1187                                 yaffs_DoGenericObjectDeletion(obj);     
1188                 }
1189                 else
1190                 {
1191                         yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1192                         obj->softDeleted = 1;
1193                 }
1194         }
1195 }
1196
1197
1198
1199
1200
1201 // Pruning removes any part of the file structure tree that is beyond the
1202 // bounds of the file (ie that does not point to chunks).
1203 //
1204 // A file should only get pruned when its size is reduced.
1205 //
1206 // Before pruning, the chunks must be pulled from the tree and the
1207 // level 0 tnode entries must be zeroed out.
1208 // Could also use this for file deletion, but that's probably better handled
1209 // by a special case.
1210
1211 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1212
1213 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1214 {
1215         int i;
1216         int hasData;
1217         
1218         if(tn)
1219         {
1220                 hasData = 0;
1221                 
1222                 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1223                 {
1224                     if(tn->internal[i] && level > 0)
1225                     {
1226                         tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1227                     }
1228                     
1229                     if(tn->internal[i])
1230                     {
1231                         hasData++;
1232                         }
1233                 }
1234                 
1235                 if(hasData == 0 && del0)
1236                 {
1237                         // Free and return NULL
1238                         
1239                         yaffs_FreeTnode(dev,tn);
1240                         tn = NULL;
1241                 }
1242                 
1243         }
1244
1245         return tn;
1246         
1247 }
1248
1249 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1250 {
1251         int i;
1252         int hasData;
1253         int done = 0;
1254         yaffs_Tnode *tn;
1255         
1256         if(fStruct->topLevel > 0)
1257         {
1258                 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1259                 
1260                 // Now we have a tree with all the non-zero branches NULL but the height
1261                 // is the same as it was.
1262                 // Let's see if we can trim internal tnodes to shorten the tree.
1263                 // We can do this if only the 0th element in the tnode is in use 
1264                 // (ie all the non-zero are NULL)
1265                 
1266                 while(fStruct->topLevel && !done)
1267                 {
1268                         tn = fStruct->top;
1269                         
1270                         hasData = 0;
1271                         for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1272                         {
1273                                 if(tn->internal[i])
1274                         {
1275                                 hasData++;
1276                                 }
1277                         }
1278                         
1279                         if(!hasData)
1280                         {
1281                                 fStruct->top = tn->internal[0];
1282                                 fStruct->topLevel--;
1283                                 yaffs_FreeTnode(dev,tn);
1284                         }
1285                         else
1286                         {
1287                                 done = 1;
1288                         }
1289                 }
1290         }
1291         
1292         return YAFFS_OK;
1293 }
1294
1295
1296
1297
1298
1299 /////////////////////// End of File Structure functions. /////////////////
1300
1301 // yaffs_CreateFreeObjects creates a bunch more objects and
1302 // adds them to the object free list.
1303 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1304 {
1305     int i;
1306     yaffs_Object *newObjects;
1307     yaffs_ObjectList *list;
1308     
1309     if(nObjects < 1) return YAFFS_OK;
1310    
1311         // make these things
1312         
1313     newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1314    
1315     if (!newObjects)
1316     {
1317                 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1318                 return YAFFS_FAIL;
1319     }
1320     
1321     // Hook them into the free list
1322     for(i = 0; i < nObjects - 1; i++)
1323     {
1324         newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1325     }
1326         
1327         newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1328         dev->freeObjects = newObjects;
1329         dev->nFreeObjects+= nObjects;
1330         dev->nObjectsCreated+= nObjects;
1331         
1332         // Now add this bunch of Objects to a list for freeing up.
1333         
1334         list = YMALLOC(sizeof(yaffs_ObjectList));
1335         if(!list)
1336         {
1337                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1338         }
1339         else
1340         {
1341                 list->objects = newObjects;
1342                 list->next = dev->allocatedObjectList;
1343                 dev->allocatedObjectList = list;
1344         }
1345         
1346         
1347         
1348         return YAFFS_OK;
1349 }
1350
1351
1352 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1353 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1354 {
1355         yaffs_Object *tn = NULL;
1356         
1357         // If there are none left make more
1358         if(!dev->freeObjects)
1359         {
1360                 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1361         }
1362         
1363         if(dev->freeObjects)
1364         {
1365                 tn = dev->freeObjects;
1366                 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1367                 dev->nFreeObjects--;
1368                 
1369                 // Now sweeten it up...
1370         
1371                 memset(tn,0,sizeof(yaffs_Object));
1372                 tn->myDev = dev;
1373                 tn->chunkId = -1;
1374                 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1375                 INIT_LIST_HEAD(&(tn->hardLinks));
1376                 INIT_LIST_HEAD(&(tn->hashLink));
1377                 INIT_LIST_HEAD(&tn->siblings);
1378                 
1379                 // Add it to the lost and found directory.
1380                 // NB Can't put root or lostNFound in lostNFound so
1381                 // check if lostNFound exists first
1382                 if(dev->lostNFoundDir)
1383                 {
1384                         yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);      
1385                 }
1386         }
1387         
1388
1389         return tn;
1390 }
1391
1392 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1393 {
1394
1395         yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);              
1396         if(obj)
1397         {
1398                 obj->fake = 1;                  // it is fake so it has no NAND presence...
1399                 obj->renameAllowed= 0;  // ... and we're not allowed to rename it...
1400                 obj->unlinkAllowed= 0;  // ... or unlink it
1401                 obj->deleted = 0;
1402                 obj->unlinked = 0;
1403                 obj->st_mode = mode;
1404                 obj->myDev = dev;
1405                 obj->chunkId = 0; // Not a valid chunk.
1406         }
1407         
1408         return obj;
1409         
1410 }
1411
1412
1413 static void yaffs_UnhashObject(yaffs_Object *tn)
1414 {
1415         int bucket;
1416         yaffs_Device *dev = tn->myDev;
1417         
1418         
1419         // If it is still linked into the bucket list, free from the list
1420         if(!list_empty(&tn->hashLink))
1421         {
1422                 list_del_init(&tn->hashLink);
1423                 bucket =  yaffs_HashFunction(tn->objectId);
1424                 dev->objectBucket[bucket].count--;
1425         }
1426         
1427 }
1428
1429
1430 // FreeObject frees up a Object and puts it back on the free list
1431 static void yaffs_FreeObject(yaffs_Object *tn)
1432 {
1433
1434         yaffs_Device *dev = tn->myDev;
1435         
1436         yaffs_UnhashObject(tn);
1437         
1438         // Link into the free list.
1439         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1440         dev->freeObjects = tn;
1441         dev->nFreeObjects++;
1442 }
1443
1444
1445
1446
1447 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1448 {
1449         // Free the list of allocated Objects
1450         
1451         while( dev->allocatedObjectList)
1452         {
1453                 YFREE(dev->allocatedObjectList->objects);
1454                 dev->allocatedObjectList =  dev->allocatedObjectList->next;
1455         }
1456         
1457         dev->freeObjects = NULL;
1458         dev->nFreeObjects = 0;
1459 }
1460
1461 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1462 {
1463         int i;
1464         
1465         dev->allocatedObjectList = NULL;
1466         dev->freeObjects = NULL;
1467         dev->nFreeObjects = 0;
1468         
1469         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1470         {
1471                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1472                 dev->objectBucket[i].count = 0; 
1473         }
1474
1475 }
1476
1477
1478
1479
1480
1481
1482 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1483 {
1484         static int x = 0;
1485         int i;
1486         int l = 999;
1487         int lowest = 999999;
1488
1489                 
1490         // First let's see if we can find one that's empty.
1491         
1492         for(i = 0; i < 10 && lowest > 0; i++)
1493          {
1494                 x++;
1495                 x %=  YAFFS_NOBJECT_BUCKETS;
1496                 if(dev->objectBucket[x].count < lowest)
1497                 {
1498                         lowest = dev->objectBucket[x].count;
1499                         l = x;
1500                 }
1501                 
1502         }
1503         
1504         // If we didn't find an empty list, then try
1505         // looking a bit further for a short one
1506         
1507         for(i = 0; i < 10 && lowest > 3; i++)
1508          {
1509                 x++;
1510                 x %=  YAFFS_NOBJECT_BUCKETS;
1511                 if(dev->objectBucket[x].count < lowest)
1512                 {
1513                         lowest = dev->objectBucket[x].count;
1514                         l = x;
1515                 }
1516                 
1517         }
1518         
1519         return l;
1520 }
1521
1522 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1523 {
1524         int bucket = yaffs_FindNiceObjectBucket(dev);
1525         
1526         // Now find an object value that has not already been taken
1527         // by scanning the list.
1528         
1529         int found = 0;
1530         struct list_head *i;
1531         
1532         __u32 n = (__u32)bucket;
1533
1534         //yaffs_CheckObjectHashSanity();        
1535         
1536         while(!found)
1537         {
1538                 found = 1;
1539                 n +=  YAFFS_NOBJECT_BUCKETS;
1540                 if(1 ||dev->objectBucket[bucket].count > 0)
1541                 {
1542                         list_for_each(i,&dev->objectBucket[bucket].list)
1543                         {
1544                                 // If there is already one in the list
1545                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1546                                 {
1547                                         found = 0;
1548                                 }
1549                         }
1550                 }
1551         }
1552         
1553         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1554         
1555         return n;       
1556 }
1557
1558 void yaffs_HashObject(yaffs_Object *in)
1559 {
1560         int bucket = yaffs_HashFunction(in->objectId);
1561         yaffs_Device *dev = in->myDev;
1562         
1563         if(!list_empty(&in->hashLink))
1564         {
1565                 //YINFO("!!!");
1566         }
1567
1568         
1569         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1570         dev->objectBucket[bucket].count++;
1571
1572 }
1573
1574 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1575 {
1576         int bucket = yaffs_HashFunction(number);
1577         struct list_head *i;
1578         yaffs_Object *in;
1579         
1580         list_for_each(i,&dev->objectBucket[bucket].list)
1581         {
1582                 // Look if it is in the list
1583                 if(i)
1584                 {
1585                         in = list_entry(i, yaffs_Object,hashLink);
1586                         if(in->objectId == number)
1587                         {
1588                                 return in;
1589                         }
1590                 }
1591         }
1592         
1593         return NULL;
1594 }
1595
1596
1597
1598 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1599 {
1600                 
1601         yaffs_Object *theObject;
1602
1603         if(number < 0)
1604         {
1605                 number = yaffs_CreateNewObjectNumber(dev);
1606         }
1607         
1608         theObject = yaffs_AllocateEmptyObject(dev);
1609         
1610         if(theObject)
1611         {
1612                 theObject->fake = 0;
1613                 theObject->renameAllowed = 1;
1614                 theObject->unlinkAllowed = 1;
1615                 theObject->objectId = number;
1616                 yaffs_HashObject(theObject);
1617                 theObject->variantType = type;
1618 #ifdef CONFIG_YAFFS_WINCE
1619                 yfsd_WinFileTimeNow(theObject->win_atime);
1620                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1621                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1622
1623 #else
1624                 theObject->st_atime = theObject->st_mtime =     theObject->st_ctime = CURRENT_TIME;
1625 #endif
1626                 switch(type)
1627                 {
1628                         case YAFFS_OBJECT_TYPE_FILE: 
1629                                 theObject->variant.fileVariant.fileSize = 0;
1630                                 theObject->variant.fileVariant.scannedFileSize = 0;
1631                                 theObject->variant.fileVariant.topLevel = 0;
1632                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1633                                 break;
1634                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1635                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1636                                 break;
1637                         case YAFFS_OBJECT_TYPE_SYMLINK:
1638                                 // No action required
1639                                 break;
1640                         case YAFFS_OBJECT_TYPE_HARDLINK:
1641                                 // No action required
1642                                 break;
1643                         case YAFFS_OBJECT_TYPE_SPECIAL:
1644                                 // No action required
1645                                 break;
1646                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1647                                 // todo this should not happen
1648                                 break;
1649                 }
1650         }
1651         
1652         return theObject;
1653 }
1654
1655 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1656 {
1657         yaffs_Object *theObject = NULL;
1658         
1659         if(number > 0)
1660         {
1661                 theObject = yaffs_FindObjectByNumber(dev,number);
1662         }
1663         
1664         if(!theObject)
1665         {
1666                 theObject = yaffs_CreateNewObject(dev,number,type);
1667         }
1668         
1669         return theObject;
1670
1671 }
1672
1673 char *yaffs_CloneString(const char *str)
1674 {
1675         char *newStr = NULL;
1676         
1677         if(str && *str)
1678         {
1679                 newStr = YMALLOC(strlen(str) + 1);
1680                 strcpy(newStr,str);
1681         }
1682
1683         return newStr;
1684         
1685 }
1686
1687 //
1688 // Mknod (create) a new object.
1689 // equivalentObject only has meaning for a hard link;
1690 // aliasString only has meaning for a sumlink.
1691 // rdev only has meaning for devices (a subset of special objects)
1692 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1693                                                                  yaffs_Object *parent,
1694                                                                  const char *name, 
1695                                                                  __u32 mode,
1696                                                                  __u32 uid,
1697                                                                  __u32 gid,
1698                                                                  yaffs_Object *equivalentObject,
1699                                                                  const char *aliasString,
1700                                                                  __u32 rdev)
1701 {
1702         yaffs_Object *in;
1703
1704         yaffs_Device *dev = parent->myDev;
1705         
1706         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1707         if(yaffs_FindObjectByName(parent,name))
1708         {
1709                 return NULL;
1710         }
1711         
1712         in = yaffs_CreateNewObject(dev,-1,type);
1713         
1714         if(in)
1715         {
1716                 in->chunkId = -1;
1717                 in->valid = 1;
1718                 in->variantType = type;
1719
1720                 in->st_mode  = mode;
1721                 
1722 #ifdef CONFIG_YAFFS_WINCE
1723                 yfsd_WinFileTimeNow(in->win_atime);
1724                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1725                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[0];
1726                 
1727 #else
1728                 in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME;
1729                 in->st_rdev  = rdev;
1730                 in->st_uid   = uid;
1731                 in->st_gid   = gid;
1732 #endif          
1733                 in->nDataChunks = 0;
1734
1735                 yaffs_SetObjectName(in,name);
1736                 in->dirty = 1;
1737                 
1738                 yaffs_AddObjectToDirectory(parent,in);
1739                 
1740                 in->myDev = parent->myDev;
1741                 
1742                                 
1743                 switch(type)
1744                 {
1745                         case YAFFS_OBJECT_TYPE_SYMLINK:
1746                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1747                                 break;
1748                         case YAFFS_OBJECT_TYPE_HARDLINK:
1749                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1750                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1751                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1752                                 break;
1753                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1754                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1755                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1756                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1757                                 break;
1758                 }
1759
1760                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1761                    yaffs_UpdateObjectHeader(in,name,0) < 0)
1762                 {
1763                         // Could not create the object header, fail the creation
1764                         yaffs_AbortHalfCreatedObject(in);
1765                         in = NULL;
1766                 }
1767
1768         }
1769         
1770         return in;
1771 }
1772
1773 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1774 {
1775         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1776 }
1777
1778 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid)
1779 {
1780         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1781 }
1782
1783 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1784 {
1785         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev);
1786 }
1787
1788 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,const char *alias)
1789 {
1790         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1791 }
1792
1793 // NB yaffs_Link returns the object id of the equivalent object.
1794 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject)
1795 {
1796         // Get the real object in case we were fed a hard link as an equivalent object
1797         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1798         
1799         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1800         {
1801                 return equivalentObject;
1802         }
1803         else
1804         {
1805                 return NULL;
1806         }
1807         
1808 }
1809
1810
1811 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName,int force)
1812 {
1813         int unlinkOp;
1814
1815         if(newDir == NULL)
1816         {
1817                 newDir = obj->parent; // use the old directory
1818         }
1819
1820         unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1821         
1822         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1823         // duplicate names are allowed.
1824         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1825         if( (unlinkOp|| 
1826                  force || 
1827                  !yaffs_FindObjectByName(newDir,newName))  &&
1828              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1829         {
1830                 yaffs_SetObjectName(obj,newName);
1831                 obj->dirty = 1;
1832                 
1833                 yaffs_AddObjectToDirectory(newDir,obj);
1834                 
1835                 if(unlinkOp) obj->unlinked = 1;
1836                 
1837                 
1838                 if(yaffs_UpdateObjectHeader(obj,newName,0) >= 0)
1839                 {
1840                         return YAFFS_OK;
1841                 }
1842         }
1843         
1844         return YAFFS_FAIL;
1845 }
1846
1847
1848
1849 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName)
1850 {
1851         yaffs_Object *obj;
1852         int force = 0;
1853         
1854 #ifdef YAFFS_CASE_INSENSITIVE
1855         // Special case for WinCE.
1856         // While look-up is case insensitive, the name isn't.
1857         // THerefore we might want to change x.txt to X.txt
1858         if(oldDir == newDir && _stricmp(oldName,newName) == 0)
1859         {
1860                 force = 1;
1861         }       
1862 #endif
1863         
1864         obj = yaffs_FindObjectByName(oldDir,oldName);
1865         if(obj && obj->renameAllowed)
1866         {
1867                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1868         }
1869         return YAFFS_FAIL;
1870 }
1871
1872
1873
1874 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1875 {
1876         // Scan the buckets and check that the lists 
1877         // have as many members as the count says there are
1878         int bucket;
1879         int countEm;
1880         struct list_head *j;
1881         int ok = YAFFS_OK;
1882         
1883         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1884         {
1885                 countEm = 0;
1886                 
1887                 list_for_each(j,&dev->objectBucket[bucket].list)
1888                 {
1889                         countEm++;
1890                 }
1891                 
1892                 if(countEm != dev->objectBucket[bucket].count)
1893                 {
1894                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
1895                         ok = YAFFS_FAIL;
1896                 }
1897         }
1898
1899         return ok;
1900 }
1901
1902 #if 0
1903 void yaffs_ObjectTest(yaffs_Device *dev)
1904 {
1905         yaffs_Object *in[1000];
1906         int inNo[1000];
1907         yaffs_Object *inold[1000];
1908         int i;
1909         int j;
1910         
1911         memset(in,0,1000*sizeof(yaffs_Object *));
1912         memset(inold,0,1000*sizeof(yaffs_Object *));
1913         
1914         yaffs_CheckObjectHashSanity(dev);
1915         
1916         for(j = 0; j < 10; j++)
1917         {
1918                 //T(("%d\n",j));
1919                 
1920                 for(i = 0; i < 1000; i++)
1921                 {
1922                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1923                         if(!in[i])
1924                         {
1925                                 YINFO("No more inodes");
1926                         }
1927                         else
1928                         {
1929                                 inNo[i] = in[i]->objectId;
1930                         }
1931                 }
1932                 
1933                 for(i = 0; i < 1000; i++)
1934                 {
1935                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1936                         {
1937                                 //T(("Differnce in look up test\n"));
1938                         }
1939                         else
1940                         {
1941                                 // T(("Look up ok\n"));
1942                         }
1943                 }
1944                 
1945                 yaffs_CheckObjectHashSanity(dev);
1946         
1947                 for(i = 0; i < 1000; i+=3)
1948                 {
1949                         yaffs_FreeObject(in[i]);        
1950                         in[i] = NULL;
1951                 }
1952                 
1953         
1954                 yaffs_CheckObjectHashSanity(dev);
1955         }
1956                 
1957 }
1958
1959 #endif
1960
1961 /////////////////////////// Block Management and Page Allocation ///////////////////
1962
1963
1964 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
1965 {
1966         dev->allocationBlock = -1; // force it to get a new one
1967         //Todo we're assuming the malloc will pass.
1968         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
1969         // Set up dynamic blockinfo stuff.
1970         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
1971         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
1972         if(dev->blockInfo && dev->chunkBits)
1973         {
1974                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
1975                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
1976                 return YAFFS_OK;
1977         }
1978         
1979         return YAFFS_FAIL;
1980         
1981 }
1982
1983 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
1984 {
1985         YFREE(dev->blockInfo);
1986         dev->blockInfo = NULL;
1987         YFREE(dev->chunkBits);
1988         dev->chunkBits = NULL;
1989 }
1990
1991 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
1992 // for garbage collection.
1993
1994 static int yaffs_FindDirtiestBlock(yaffs_Device *dev,int aggressive)
1995 {
1996
1997         int b = dev->currentDirtyChecker;
1998         
1999         int i;
2000         int iterations;
2001         int dirtiest = -1;
2002         int pagesInUse; 
2003         yaffs_BlockInfo *bi;
2004
2005         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2006         // search further.
2007         
2008         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2009         if(aggressive)
2010         {
2011                 iterations = dev->endBlock - dev->startBlock + 1;
2012         }
2013         else
2014         {
2015                 iterations = iterations / 16;
2016                 if(iterations > 200)
2017                 {
2018                         iterations = 200;
2019                 }
2020         }
2021         
2022         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2023         {
2024                 b++;
2025                 if ( b < dev->startBlock || b > dev->endBlock)
2026                 {
2027                         b =  dev->startBlock;
2028                 }
2029
2030                 if(b < dev->startBlock || b > dev->endBlock)
2031                 {
2032                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2033                         YBUG();
2034                 }
2035                 
2036                 bi = yaffs_GetBlockInfo(dev,b);
2037                 
2038                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2039                    (bi->pagesInUse - bi->softDeletions )< pagesInUse)
2040                 {
2041                         dirtiest = b;
2042                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2043                 }
2044         }
2045         
2046         dev->currentDirtyChecker = b;
2047         
2048         return dirtiest;
2049 }
2050
2051
2052 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2053 {
2054         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2055         
2056         int erasedOk = 0;
2057         
2058         // If the block is still healthy erase it and mark as clean.
2059         // If the block has had a data failure, then retire it.
2060         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2061
2062         if(!bi->needsRetiring)
2063         {
2064                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2065                 if(!erasedOk)
2066                 {
2067                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2068                 }
2069         }
2070         
2071         if( erasedOk )
2072         {
2073                 // Clean it up...
2074                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2075                 dev->nErasedBlocks++;
2076                 bi->pagesInUse = 0;
2077                 bi->softDeletions = 0;
2078                 yaffs_ClearChunkBits(dev,blockNo);
2079         
2080                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2081         }
2082         else
2083         {
2084                 yaffs_RetireBlock(dev,blockNo);
2085                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2086         }
2087 }
2088
2089
2090 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2091 {
2092         int i;
2093         
2094         yaffs_BlockInfo *bi;
2095         
2096         if(dev->nErasedBlocks < 1)
2097         {
2098                 // Hoosterman we've got a problem.
2099                 // Can't get space to gc
2100                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during gc" TENDSTR)));
2101
2102                 return -1;
2103         }
2104         
2105         // Find an empty block.
2106         
2107         for(i = dev->startBlock; i <= dev->endBlock; i++)
2108         {
2109                 dev->allocationBlockFinder++;
2110                 if(dev->allocationBlockFinder <dev->startBlock || dev->allocationBlockFinder> dev->endBlock) 
2111                 {
2112                         dev->allocationBlockFinder = dev->startBlock;
2113                 }
2114                 
2115                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2116
2117                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2118                 {
2119                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2120                         dev->nErasedBlocks--;                   
2121                         return dev->allocationBlockFinder;
2122                 }
2123         }
2124         
2125         return -1;      
2126 }
2127
2128
2129
2130 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2131 {
2132         int retVal;
2133         yaffs_BlockInfo *bi;
2134         
2135         if(dev->allocationBlock < 0)
2136         {
2137                 // Get next block to allocate off
2138                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2139                 dev->allocationPage = 0;
2140         }
2141         
2142         if(!useReserve &&  dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS)
2143         {
2144                 // Not enough space to allocate unless we're allowed to use the reserve.
2145                 return -1;
2146         }
2147         
2148         // Next page please....
2149         if(dev->allocationBlock >= 0)
2150         {
2151                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2152                 
2153                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2154                                   dev->allocationPage;
2155                 bi->pagesInUse++;
2156                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2157
2158                 dev->allocationPage++;
2159                 
2160                 dev->nFreeChunks--;
2161                 
2162                 // If the block is full set the state to full
2163                 if(dev->allocationPage >= dev->nChunksPerBlock)
2164                 {
2165                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2166                         dev->allocationBlock = -1;
2167                 }
2168
2169
2170                 return retVal;
2171                 
2172         }
2173         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2174
2175         return -1;      
2176 }
2177
2178 // To determine if we have enough space we just look at the 
2179 // number of erased blocks.
2180 // The cache is allowed to use reserved blocks.
2181
2182 static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev)
2183 {
2184         return (dev->nErasedBlocks >= YAFFS_RESERVED_BLOCKS);
2185 }
2186
2187
2188 static int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2189 {
2190         int oldChunk;
2191         int newChunk;
2192         int chunkInBlock;
2193         int markNAND;
2194         
2195         
2196         yaffs_Spare spare;
2197         yaffs_Tags  tags;
2198         __u8  buffer[YAFFS_BYTES_PER_CHUNK];
2199         
2200 //      yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2201         
2202         yaffs_Object *object;
2203
2204         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2205         
2206         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2207             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2208             chunkInBlock++, oldChunk++ )
2209         {
2210                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2211                 {
2212                         
2213                         // This page is in use and might need to be copied off
2214                         
2215                         markNAND = 1;
2216                         
2217                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2218                         
2219                         yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare,1);
2220                         
2221                         yaffs_GetTagsFromSpare(dev,&spare,&tags);
2222
2223                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2224                         
2225                         if(object && object->deleted && tags.chunkId != 0)
2226                         {
2227                                 // Data chunk in a deleted file, throw it away
2228                                 // It's a deleted data chunk,
2229                                 // No need to copy this, just forget about it and fix up the
2230                                 // object.
2231                                 
2232                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2233                                 object->nDataChunks--;
2234                                 
2235                                 if(object->nDataChunks <= 0)
2236                                 {
2237                                         // Time to delete the file too
2238                                         yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2239                                         object->variant.fileVariant.top = NULL;
2240                                         T(YAFFS_TRACE_TRACING,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2241                                         yaffs_DoGenericObjectDeletion(object);                                  
2242                                 }
2243                                 markNAND = 0;
2244                         }
2245                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2246                         {
2247                                 // Deleted object header with no data chunks.
2248                                 // Can be discarded and the file deleted.
2249                                 object->chunkId = 0;
2250                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2251                                 object->variant.fileVariant.top = NULL;
2252                                 yaffs_DoGenericObjectDeletion(object);
2253                                 
2254                         }
2255                         else if(object)
2256                         {
2257                                 // It's either a data chunk in a live file or
2258                                 // an ObjectHeader, so we're interested in it.
2259                                 // NB Need to keep the ObjectHeaders of deleted files
2260                                 // until the whole file has been deleted off
2261                                 tags.serialNumber++;
2262                                 yaffs_LoadTagsIntoSpare(&spare,&tags);
2263
2264                                 dev->nGCCopies++;
2265
2266                                 newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1);
2267                         
2268                                 if(newChunk < 0)
2269                                 {
2270                                         return YAFFS_FAIL;
2271                                 }
2272                         
2273                                 // Ok, now fix up the Tnodes etc.
2274                         
2275                                 if(tags.chunkId == 0)
2276                                 {
2277                                         // It's a header
2278                                         object->chunkId = newChunk;
2279                                 }
2280                                 else
2281                                 {
2282                                         // It's a data chunk
2283                                         yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2284                                 }
2285                         }
2286                         
2287                         yaffs_DeleteChunk(dev,oldChunk,markNAND);                       
2288                         
2289                 }
2290         }
2291
2292         return YAFFS_OK;
2293 }
2294
2295
2296 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2297 {
2298         // find a file to delete
2299         struct list_head *i;    
2300         yaffs_Object *l;
2301
2302
2303         //Scan the unlinked files looking for one to delete
2304         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2305         {
2306                 if(i)
2307                 {
2308                         l = list_entry(i, yaffs_Object,siblings);
2309                         if(l->deleted)
2310                         {
2311                                 return l;                       
2312                         }
2313                 }
2314         }       
2315         return NULL;
2316 }
2317
2318
2319 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2320 {
2321         // This does background deletion on unlinked files.. only deleted ones.
2322         // If we don't have a file we're working on then find one
2323         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2324         {
2325                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2326         }
2327         
2328         // OK, we're working on a file...
2329         if(dev->unlinkedDeletion)
2330         {
2331                 yaffs_Object *obj = dev->unlinkedDeletion;
2332                 int delresult;
2333                 int limit; // Number of chunks to delete in a file.
2334                                    // NB this can be exceeded, but not by much.
2335                                    
2336                 limit = -1;
2337
2338                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2339                 
2340                 if(obj->nDataChunks == 0)
2341                 {
2342                         // Done all the deleting of data chunks.
2343                         // Now dump the header and clean up
2344                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2345                         obj->variant.fileVariant.top = NULL;
2346                         yaffs_DoGenericObjectDeletion(obj);
2347                         dev->nDeletedFiles--;
2348                         dev->nUnlinkedFiles--;
2349                         dev->nBackgroundDeletions++;
2350                         dev->unlinkedDeletion = NULL;   
2351                 }
2352         }
2353 }
2354
2355
2356 #if 0
2357 #define YAFFS_GARBAGE_COLLECT_LOW_WATER 2
2358 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2359 {
2360         int block;
2361         int aggressive=0;
2362         
2363         //yaffs_DoUnlinkedFileDeletion(dev);
2364         
2365         if(dev->nErasedBlocks <= (YAFFS_RESERVED_BLOCKS + YAFFS_GARBAGE_COLLECT_LOW_WATER))
2366         {
2367                 aggressive = 1;
2368         }               
2369         
2370         if(aggressive)
2371         {
2372                 block = yaffs_FindDirtiestBlock(dev,aggressive);
2373                 
2374                 if(block >= 0)
2375                 {
2376                         dev->garbageCollections++;
2377                         return yaffs_GarbageCollectBlock(dev,block);
2378                 }       
2379                 else
2380                 {
2381                         return YAFFS_FAIL;
2382                 }
2383         }
2384
2385         return YAFFS_OK;
2386 }
2387 #endif
2388
2389 // New garbage collector
2390 // If we're very low on erased blocks then we do aggressive garbage collection
2391 // otherwise we do "passive" garbage collection.
2392 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2393 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2394 //
2395 // The idea is to help clear out space in a more spread-out manner.
2396 // Dunno if it really does anything useful.
2397 //
2398 static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2399 {
2400         int block;
2401         int aggressive=0;
2402         
2403         //yaffs_DoUnlinkedFileDeletion(dev);
2404         
2405         if(dev->nErasedBlocks <= (YAFFS_RESERVED_BLOCKS + 1))
2406         {
2407                 aggressive = 1;
2408         }               
2409         
2410         block = yaffs_FindDirtiestBlock(dev,aggressive);
2411         
2412         if(block >= 0)
2413         {
2414                 dev->garbageCollections++;
2415                 if(!aggressive)
2416                 {
2417                         dev->passiveGarbageCollections++;
2418                 }
2419
2420                 T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2421
2422                 return yaffs_GarbageCollectBlock(dev,block);
2423         }       
2424
2425         return aggressive ? YAFFS_FAIL : YAFFS_OK;
2426 }
2427
2428
2429 //////////////////////////// TAGS ///////////////////////////////////////
2430
2431 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2432 {
2433         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2434         
2435         yaffs_CalcTagsECC(tagsPtr);
2436         
2437         sparePtr->tagByte0 = tu->asBytes[0];
2438         sparePtr->tagByte1 = tu->asBytes[1];
2439         sparePtr->tagByte2 = tu->asBytes[2];
2440         sparePtr->tagByte3 = tu->asBytes[3];
2441         sparePtr->tagByte4 = tu->asBytes[4];
2442         sparePtr->tagByte5 = tu->asBytes[5];
2443         sparePtr->tagByte6 = tu->asBytes[6];
2444         sparePtr->tagByte7 = tu->asBytes[7];
2445 }
2446
2447 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2448 {
2449         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2450         int result;
2451
2452         tu->asBytes[0]= sparePtr->tagByte0;
2453         tu->asBytes[1]= sparePtr->tagByte1;
2454         tu->asBytes[2]= sparePtr->tagByte2;
2455         tu->asBytes[3]= sparePtr->tagByte3;
2456         tu->asBytes[4]= sparePtr->tagByte4;
2457         tu->asBytes[5]= sparePtr->tagByte5;
2458         tu->asBytes[6]= sparePtr->tagByte6;
2459         tu->asBytes[7]= sparePtr->tagByte7;
2460         
2461         result =  yaffs_CheckECCOnTags(tagsPtr);
2462         if(result> 0)
2463         {
2464                 dev->tagsEccFixed++;
2465         }
2466         else if(result <0)
2467         {
2468                 dev->tagsEccUnfixed++;
2469         }
2470 }
2471
2472 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2473 {
2474         memset(spare,0xFF,sizeof(yaffs_Spare));
2475 }
2476
2477 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted)
2478 {
2479         if(tags)
2480         {
2481                 yaffs_Spare spare;
2482                 if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare,1) == YAFFS_OK)
2483                 {
2484                         *chunkDeleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
2485                         yaffs_GetTagsFromSpare(dev,&spare,tags);
2486                         return YAFFS_OK;
2487                 }
2488                 else
2489                 {
2490                         return YAFFS_FAIL;
2491                 }
2492         }
2493         
2494         return YAFFS_OK;
2495 }
2496
2497 #if 0
2498 static int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_Tags *tags)
2499 {
2500         // NB There must be tags, data is optional
2501         // If there is data, then an ECC is calculated on it.
2502         
2503         yaffs_Spare spare;
2504         
2505         if(!tags)
2506         {
2507                 return YAFFS_FAIL;
2508         }
2509         
2510         yaffs_SpareInitialise(&spare);
2511         
2512         if(!dev->useNANDECC && buffer)
2513         {
2514                 yaffs_CalcECC(buffer,&spare);
2515         }
2516         
2517         yaffs_LoadTagsIntoSpare(&spare,tags);
2518         
2519         return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare);
2520         
2521 }
2522 #endif
2523
2524
2525 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_Tags *tags, int useReserve)
2526 {
2527         // NB There must be tags, data is optional
2528         // If there is data, then an ECC is calculated on it.
2529         
2530         yaffs_Spare spare;
2531         
2532         if(!tags)
2533         {
2534                 return YAFFS_FAIL;
2535         }
2536         
2537         yaffs_SpareInitialise(&spare);
2538         
2539         if(!dev->useNANDECC && buffer)
2540         {
2541                 yaffs_CalcECC(buffer,&spare);
2542         }
2543         
2544         yaffs_LoadTagsIntoSpare(&spare,tags);
2545         
2546         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2547         
2548 }
2549
2550 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted)
2551 {
2552         return  (  tags->chunkId == chunkInObject &&
2553                            tags->objectId == objectId &&
2554                            !chunkDeleted) ? 1 : 0;
2555         
2556 }
2557
2558
2559
2560 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
2561 {
2562         //Get the Tnode, then get the level 0 offset chunk offset
2563     yaffs_Tnode *tn;     
2564     int theChunk = -1;
2565     yaffs_Tags localTags;
2566     int i;
2567     int found = 0;
2568     int chunkDeleted;
2569     
2570     yaffs_Device *dev = in->myDev;
2571     
2572     
2573     if(!tags)
2574     {
2575         // Passed a NULL, so use our own tags space
2576         tags = &localTags;
2577     }
2578     
2579     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2580     
2581     if(tn)
2582     {
2583                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2584
2585                 // Now we need to do the shifting etc and search for it
2586                 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
2587                 {
2588                         yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
2589                         if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
2590                         {
2591                                 // found it;
2592                                 found = 1;
2593                         }
2594                         else
2595                         {
2596                                 theChunk++;
2597                         }
2598                 }
2599     }
2600     return found ? theChunk : -1;
2601 }
2602
2603 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags)
2604 {
2605         //Get the Tnode, then get the level 0 offset chunk offset
2606     yaffs_Tnode *tn;     
2607     int theChunk = -1;
2608     yaffs_Tags localTags;
2609     int i;
2610     int found = 0;
2611     yaffs_Device *dev = in->myDev;
2612     int chunkDeleted;
2613     
2614     if(!tags)
2615     {
2616         // Passed a NULL, so use our own tags space
2617         tags = &localTags;
2618     }
2619     
2620     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2621     
2622     if(tn)
2623     {
2624     
2625                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2626     
2627                 // Now we need to do the shifting etc and search for it
2628                 for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++)
2629                 {
2630                         yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted);
2631                         if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted))
2632                         {
2633                                 // found it;
2634                                 found = 1;
2635                         }
2636                         else
2637                         {
2638                                 theChunk++;
2639                         }
2640                 }
2641     
2642                 // Delete the entry in the filestructure
2643                 if(found)
2644                 {
2645                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2646                 }
2647     }
2648     else
2649     {
2650         //T(("No level 0 found for %d\n", chunkInInode));
2651     }
2652     
2653     if(!found)
2654     {
2655         //T(("Could not find %d to delete\n",chunkInInode));
2656     }
2657     return found ? theChunk : -1;
2658 }
2659
2660
2661 #ifdef YAFFS_PARANOID
2662
2663 static int yaffs_CheckFileSanity(yaffs_Object *in)
2664 {
2665         int chunk;
2666         int nChunks;
2667         int fSize;
2668         int failed = 0;
2669         int objId;
2670         yaffs_Tnode *tn;
2671     yaffs_Tags localTags;
2672     yaffs_Tags *tags = &localTags;
2673     int theChunk;
2674     int chunkDeleted;
2675     
2676         
2677         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2678         {
2679                 //T(("Object not a file\n"));
2680                 return YAFFS_FAIL;
2681         }
2682         
2683         objId = in->objectId;
2684         fSize  = in->variant.fileVariant.fileSize;
2685         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
2686         
2687         for(chunk = 1; chunk <= nChunks; chunk++)
2688         {
2689                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
2690     
2691                 if(tn)
2692                 {
2693     
2694                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
2695     
2696
2697                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
2698                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
2699                                 {
2700                                         // found it;
2701                                 
2702                                 }
2703                                 else
2704                                 {
2705                                         //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
2706                                         //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
2707                                                         
2708                                         failed = 1;
2709                                                                 
2710                                 }
2711     
2712                 }
2713                 else
2714                 {
2715                         //T(("No level 0 found for %d\n", chunk));
2716                 }
2717         }
2718         
2719         return failed ? YAFFS_FAIL : YAFFS_OK;
2720 }
2721
2722 #endif
2723
2724 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
2725 {
2726         yaffs_Tnode *tn;
2727         yaffs_Device *dev = in->myDev;
2728         int existingChunk;
2729         yaffs_Tags existingTags;
2730         yaffs_Tags newTags;
2731         unsigned existingSerial, newSerial;
2732         
2733         int newChunkDeleted;
2734         
2735         
2736         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2737         if(!tn)
2738         {
2739                 return YAFFS_FAIL;
2740         }
2741
2742         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
2743         
2744         if(inScan)
2745         {
2746                 // If we're scanning then we need to test for duplicates
2747                 // NB This does not need to be efficient since it should only ever 
2748                 // happen when the power fails during a write, then only one
2749                 // chunk should ever be affected.
2750         
2751                 
2752                 if(existingChunk != 0)
2753                 {
2754                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
2755                         //    thus we have to do a FindChunkInFile to get the real chunk id.
2756                         //
2757                         // We have a duplicate now we need to decide which one to use
2758                         // To do this we get both sets of tags and compare serial numbers.
2759                         yaffs_ReadChunkTagsFromNAND(dev,chunkInNAND, &newTags,&newChunkDeleted);
2760                         
2761                         
2762                         // Do a proper find
2763                         existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
2764
2765                         if(existingChunk <=0)
2766                         {
2767                                 //Hoosterman - how did this happen?
2768                                 
2769                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
2770
2771                         }
2772
2773                         
2774                         // NB The deleted flags should be false, otherwise the chunks will 
2775                         // not be loaded during a scan
2776                         
2777                         newSerial = newTags.serialNumber;
2778                         existingSerial = existingTags.serialNumber;
2779                         
2780                         if( existingChunk <= 0 ||
2781                             ((existingSerial+1) & 3) == newSerial)
2782                         {
2783                                 // Use new
2784                                 // Delete the old one and drop through to update the tnode
2785                                 yaffs_DeleteChunk(dev,existingChunk,1);
2786                         }
2787                         else
2788                         {
2789                                 // Use existing.
2790                                 // Delete the new one and return early so that the tnode isn't changed
2791                                 yaffs_DeleteChunk(dev,chunkInNAND,1);
2792                                 return YAFFS_OK;
2793                         }
2794                 }
2795
2796         }
2797                 
2798         if(existingChunk == 0)
2799         {
2800                 in->nDataChunks++;
2801         }
2802         
2803         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
2804         
2805         return YAFFS_OK;
2806 }
2807
2808
2809
2810 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
2811 {
2812     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
2813     
2814     if(chunkInNAND >= 0)
2815     {
2816                 return yaffs_ReadChunkFromNAND(in->myDev,chunkInNAND,buffer,NULL,1);
2817         }
2818         else
2819         {
2820                 return 0;
2821         }
2822
2823 }
2824
2825
2826 static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND)
2827 {
2828         int block;
2829         int page;
2830         yaffs_Spare spare;
2831         yaffs_BlockInfo *bi;
2832         
2833         if(chunkId <= 0) return;        
2834         
2835         dev->nDeletions++;
2836         block = chunkId / dev->nChunksPerBlock;
2837         page = chunkId % dev->nChunksPerBlock;
2838         
2839         if(markNAND)
2840         {
2841                 yaffs_SpareInitialise(&spare);
2842         
2843                 spare.pageStatus = 0; // To mark it as deleted.
2844
2845         
2846                 yaffs_WriteChunkToNAND(dev,chunkId,NULL,&spare);
2847                 yaffs_HandleUpdateChunk(dev,chunkId,&spare);
2848         }
2849         else
2850         {
2851                         dev->nUnmarkedDeletions++;
2852         }       
2853         
2854         bi = yaffs_GetBlockInfo(dev,block);
2855                         
2856         
2857         // Pull out of the management area.
2858         // If the whole block became dirty, this will kick off an erasure.
2859         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
2860             bi->blockState == YAFFS_BLOCK_STATE_FULL)
2861         {
2862                 dev->nFreeChunks++;
2863
2864                 yaffs_ClearChunkBit(dev,block,page);
2865                 bi->pagesInUse--;
2866                 
2867                 if(bi->pagesInUse == 0 &&
2868                bi->blockState == YAFFS_BLOCK_STATE_FULL)
2869             {
2870                 yaffs_BlockBecameDirty(dev,block);
2871             }
2872
2873         }
2874         else
2875         {
2876                 // T(("Bad news deleting chunk %d\n",chunkId));
2877         }
2878         
2879 }
2880
2881
2882
2883
2884 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
2885 {
2886         // Find old chunk Need to do this to get serial number
2887         // Write new one and patch into tree.
2888         // Invalidate old tags.
2889
2890     int prevChunkId;
2891     yaffs_Tags prevTags;
2892     
2893     int newChunkId;
2894     yaffs_Tags newTags;
2895
2896     yaffs_Device *dev = in->myDev;    
2897
2898         yaffs_CheckGarbageCollection(dev);
2899
2900         // Get the previous chunk at this location in the file if it exists
2901     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
2902     
2903     // Set up new tags
2904         newTags.chunkId = chunkInInode;
2905         newTags.objectId = in->objectId;
2906         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
2907         newTags.byteCount = nBytes;
2908         newTags.unusedStuff = 0xFFFFFFFF;
2909                 
2910         yaffs_CalcTagsECC(&newTags);
2911
2912         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
2913         if(newChunkId >= 0)
2914         {
2915                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
2916                 
2917                 
2918                 if(prevChunkId >= 0)
2919                 {
2920                         yaffs_DeleteChunk(dev,prevChunkId,1);
2921         
2922                 }
2923                 
2924                 yaffs_CheckFileSanity(in);
2925         }
2926         return newChunkId;
2927
2928
2929
2930
2931
2932 }
2933
2934
2935 // UpdateObjectHeader updates the header on NAND for an object.
2936 // If name is not NULL, then that new name is used.
2937 //
2938 int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force)
2939 {
2940
2941         yaffs_Device *dev = in->myDev;
2942         
2943     int prevChunkId;
2944     
2945     int newChunkId;
2946     yaffs_Tags newTags;
2947     __u8 bufferNew[YAFFS_BYTES_PER_CHUNK];
2948     __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
2949     
2950     yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bufferNew;
2951     yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
2952
2953     
2954     if(!in->fake || force)
2955     {
2956   
2957                 yaffs_CheckGarbageCollection(dev);              
2958     
2959                 memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK);
2960     
2961                 prevChunkId = in->chunkId;
2962     
2963                 if(prevChunkId >= 0)
2964                 {
2965                         yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL,1);      
2966                 }
2967
2968                 // Header data
2969                 oh->type = in->variantType;
2970                 
2971                 oh->st_mode = in->st_mode;
2972
2973 #ifdef CONFIG_YAFFS_WINCE
2974                 oh->win_atime[0] = in->win_atime[0];
2975                 oh->win_ctime[0] = in->win_ctime[0];
2976                 oh->win_mtime[0] = in->win_mtime[0];
2977                 oh->win_atime[1] = in->win_atime[1];
2978                 oh->win_ctime[1] = in->win_ctime[1];
2979                 oh->win_mtime[1] = in->win_mtime[1];
2980 #else
2981                 oh->st_uid = in->st_uid;
2982                 oh->st_gid = in->st_gid;
2983                 oh->st_atime = in->st_atime;
2984                 oh->st_mtime = in->st_mtime;
2985                 oh->st_ctime = in->st_ctime;
2986                 oh->st_rdev = in->st_rdev;
2987 #endif  
2988                 if(in->parent)
2989                 {
2990                         oh->parentObjectId = in->parent->objectId;
2991                 }
2992                 else
2993                 {
2994                         oh->parentObjectId = 0;
2995                 }
2996                 
2997                 //oh->sum = in->sum;
2998                 if(name && *name)
2999                 {
3000                         memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);
3001                         strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3002                 }
3003                 else if(prevChunkId)
3004                 {       
3005                         memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1);
3006                 }
3007                 else
3008                 {
3009                         memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1);   
3010                 }
3011         
3012                 switch(in->variantType)
3013                 {
3014                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3015                                 // Should not happen
3016                                 break;
3017                         case YAFFS_OBJECT_TYPE_FILE:
3018                                 oh->fileSize = in->variant.fileVariant.fileSize;
3019                                 break;
3020                         case YAFFS_OBJECT_TYPE_HARDLINK:
3021                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3022                                 break;
3023                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3024                                 // Do nothing
3025                                 break;
3026                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3027                                 // Do nothing
3028                                 break;
3029                         case YAFFS_OBJECT_TYPE_SYMLINK:
3030                                 strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3031                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3032                                 break;
3033                 }
3034
3035                 // Tags
3036                 in->serial++;
3037                 newTags.chunkId = 0;
3038                 newTags.objectId = in->objectId;
3039                 newTags.serialNumber = in->serial;
3040                 newTags.byteCount =   0xFFFFFFFF;
3041                 newTags.unusedStuff = 0xFFFFFFFF;
3042         
3043                 yaffs_CalcTagsECC(&newTags);
3044         
3045
3046                 // Create new chunk in NAND
3047                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3048     
3049                 if(newChunkId >= 0)
3050                 {
3051                 
3052                         in->chunkId = newChunkId;               
3053                 
3054                         if(prevChunkId >= 0)
3055                         {
3056                                 yaffs_DeleteChunk(dev,prevChunkId,1);
3057                         }
3058                 
3059                         in->dirty = 0;
3060                 }
3061                 
3062                 return newChunkId;
3063
3064     }
3065     return 0;
3066 }
3067
3068
3069 /////////////////////// Short Operations Cache ////////////////////////////////
3070 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3071 //      reads might be short sequential reads, and a lot of writes may be short 
3072 //  sequential writes. eg. scanning/writing a jpeg file.
3073 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3074 //  with dumb-as-a-rock code.
3075 //  There are a limited number (~10) of cache chunks per device so that we don't
3076 //  need a very intelligent search.
3077
3078
3079
3080
3081
3082 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3083 {
3084         yaffs_Device *dev = obj->myDev;
3085         int lowest;
3086         int i;
3087         yaffs_ChunkCache *cache;
3088         int chunkWritten;
3089         int nBytes;
3090         int nCaches = obj->myDev->nShortOpCaches;
3091         
3092         if  (nCaches > 0)
3093         {
3094                 do{
3095                         cache = NULL;
3096                 
3097                         // Find the dirty cache for this object with the lowest chunk id.
3098                         for(i = 0; i < nCaches; i++)
3099                         {
3100                                 if(dev->srCache[i].object == obj &&
3101                                 dev->srCache[i].dirty)
3102                                 {
3103                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3104                                         {
3105                                                 cache = &dev->srCache[i];
3106                                                 lowest = cache->chunkId;
3107                                         }
3108                                 }
3109                         }
3110                 
3111                         if(cache)
3112                         {
3113                                 //Write it out
3114
3115 #if 0
3116                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3117                         
3118                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3119                                 {
3120                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3121                                 }
3122 #endif                  
3123                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3124                                                                                                                         cache->chunkId,
3125                                                                                                                         cache->data,
3126                                                                                                                         cache->nBytes,1);
3127
3128                                 cache->dirty = 0;
3129                         }
3130                 
3131                 } while(cache && chunkWritten > 0);
3132         
3133                 if(cache)
3134                 {
3135                         //Hoosterman, disk full while writing cache out.
3136                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3137
3138                 }
3139         }       
3140                 
3141 }
3142
3143
3144 // Grab us a chunk for use.
3145 // First look for an empty one. 
3146 // Then look for the least recently used non-dirty one.
3147 // Then look for the least recently used dirty one...., flush and look again.
3148 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3149 {
3150         int i;
3151         int usage;
3152         int theOne;
3153         
3154         if(dev->nShortOpCaches > 0)
3155         {
3156                 for(i = 0; i < dev->nShortOpCaches; i++)
3157                 {
3158                         if(!dev->srCache[i].object)
3159                         {
3160                                 //T(("Grabbing empty %d\n",i));
3161                         
3162                                 return &dev->srCache[i];
3163                         }
3164                 }
3165         
3166                 theOne = -1; 
3167                 usage = 0; // just to stop the compiler grizzling
3168         
3169                 for(i = 0; i < dev->nShortOpCaches; i++)
3170                 {
3171                         if(!dev->srCache[i].dirty &&
3172                         ((dev->srCache[i].lastUse < usage  && theOne >= 0)|| 
3173                                 theOne < 0))
3174                         {
3175                                 usage = dev->srCache[i].lastUse;
3176                                 theOne = i;
3177                         }
3178                 }
3179         
3180                 //T(("Grabbing non-empty %d\n",theOne));
3181                 return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
3182         }
3183         else
3184         {
3185                 return NULL;
3186         }
3187         
3188 }
3189
3190
3191 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3192 {
3193         yaffs_ChunkCache *cache;
3194         yaffs_Object *theObj;
3195         int usage;
3196         int i;
3197         
3198         if(dev->nShortOpCaches > 0)
3199         {
3200                 // Try find a non-dirty one...
3201         
3202                 cache = yaffs_GrabChunkCacheWorker(dev);
3203         
3204                 if(!cache)
3205                 {
3206                         // They were all dirty, find the last recently used object and flush
3207                         // its cache, then  find again.
3208                         // NB what's here is not very accurate, we actually flush the object
3209                         // the last recently used page.
3210                 
3211                         theObj = dev->srCache[0].object;
3212                         usage = dev->srCache[0].lastUse;
3213         
3214                         for(i = 1; i < dev->nShortOpCaches; i++)
3215                         {
3216                                 if( dev->srCache[i].object && 
3217                                         dev->srCache[i].lastUse < usage)
3218                                 {
3219                                         usage  = dev->srCache[i].lastUse;
3220                                         theObj = dev->srCache[i].object;
3221                                 }
3222                         }
3223                 
3224                         yaffs_FlushFilesChunkCache(theObj);
3225                 
3226                         // Try again
3227                         cache = yaffs_GrabChunkCacheWorker(dev);
3228                 }
3229         
3230                 return cache;
3231         }
3232         else
3233                 return NULL;
3234
3235 }
3236
3237
3238 // Find a cached chunk
3239 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3240 {
3241         yaffs_Device *dev = obj->myDev;
3242         int i;
3243         if(dev->nShortOpCaches > 0)
3244         {
3245                 for(i = 0; i < dev->nShortOpCaches; i++)
3246                 {
3247                         if(dev->srCache[i].object == obj && 
3248                         dev->srCache[i].chunkId == chunkId)
3249                         {
3250                                 dev->cacheHits++;
3251                         
3252                                 return &dev->srCache[i];
3253                         }
3254                 }
3255         }
3256         return NULL;
3257 }
3258
3259 // Mark the chunk for the least recently used algorithym
3260 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3261 {
3262
3263         if(dev->nShortOpCaches > 0)
3264         {
3265                 if( dev->srLastUse < 0 || 
3266                         dev->srLastUse > 100000000)
3267                 {
3268                         // Reset the cache usages
3269                         int i;
3270                         for(i = 1; i < dev->nShortOpCaches; i++)
3271                         {
3272                                 dev->srCache[i].lastUse = 0;
3273                         }
3274                         dev->srLastUse = 0;
3275                 }
3276
3277                 dev->srLastUse++;
3278         
3279                 cache->lastUse = dev->srLastUse;
3280
3281                 if(isAWrite)
3282                 {
3283                         cache->dirty = 1;
3284                 }
3285         }
3286 }
3287
3288 // Invalidate a single cache page.
3289 // Do this when a whole page gets written,
3290 // ie the short cache for this page is no longer valid.
3291 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3292 {
3293         if(object->myDev->nShortOpCaches > 0)
3294         {
3295                 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3296
3297                 if(cache)
3298                 {
3299                         cache->object = NULL;
3300                 }
3301         }
3302 }
3303
3304
3305 // Invalidate all the cache pages associated with this object
3306 // Do this whenever ther file is deleted or resized.
3307 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3308 {
3309         int i;
3310         yaffs_Device *dev = in->myDev;
3311         
3312         if(dev->nShortOpCaches > 0)
3313         { 
3314                 // Now invalidate it.
3315                 for(i = 0; i < dev->nShortOpCaches; i++)
3316                 {
3317                         if(dev->srCache[i].object == in)
3318                         {
3319                                 dev->srCache[i].object = NULL;
3320                         }
3321                 }
3322         }
3323 }
3324
3325
3326
3327
3328
3329 ///////////////////////// File read/write ///////////////////////////////
3330 // Read and write have very similar structures.
3331 // In general the read/write has three parts to it
3332 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3333 // * Some complete chunks
3334 // * An incomplete chunk to end off with
3335 //
3336 // Curve-balls: the first chunk might also be the last chunk.
3337
3338 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3339 {
3340         
3341         
3342         int chunk;
3343         int start;
3344         int nToCopy;
3345         int n = nBytes;
3346         int nDone = 0;
3347         
3348         yaffs_Device *dev;
3349         
3350         dev = in->myDev;
3351         
3352         while(n > 0)
3353         {
3354                 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; // The first chunk is 1
3355                 start = offset % YAFFS_BYTES_PER_CHUNK;
3356
3357                 // OK now check for the curveball where the start and end are in
3358                 // the same chunk.      
3359                 if(     (start + n) < YAFFS_BYTES_PER_CHUNK)
3360                 {
3361                         nToCopy = n;
3362                 }
3363                 else
3364                 {
3365                         nToCopy = YAFFS_BYTES_PER_CHUNK - start;
3366                 }
3367         
3368                 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
3369                 {
3370                         // An incomplete start or end chunk (or maybe both start and end chunk)
3371                         if(dev->nShortOpCaches > 0)
3372                         {
3373                                 yaffs_ChunkCache *cache;
3374                                 // If we can't find the data in the cache, then load it up.
3375                                 cache = yaffs_FindChunkCache(in,chunk);
3376                                 if(!cache)
3377                                 {
3378                                         cache = yaffs_GrabChunkCache(in->myDev);
3379                                         cache->object = in;
3380                                         cache->chunkId = chunk;
3381                                         cache->dirty = 0;
3382                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3383                                         cache->nBytes = 0;      
3384                                 }
3385                         
3386                                 yaffs_UseChunkCache(dev,cache,0);
3387
3388                                 memcpy(buffer,&cache->data[start],nToCopy);
3389                         }
3390                         else
3391                         {
3392                                 // Read into the local buffer then copy...
3393                                 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);               
3394                                 memcpy(buffer,&dev->localBuffer[start],nToCopy);
3395                         }
3396
3397                 }
3398                 else
3399                 {
3400 #ifdef CONFIG_YAFFS_WINCE
3401                         
3402                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3403                         // This is because we otherwise screw up WinCE's memory mapper
3404                         yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3405                         memcpy(buffer,dev->localBuffer,YAFFS_BYTES_PER_CHUNK);
3406 #else
3407                         // A full chunk. Read directly into the supplied buffer.
3408                         yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3409 #endif
3410                 }
3411                 
3412                 n -= nToCopy;
3413                 offset += nToCopy;
3414                 buffer += nToCopy;
3415                 nDone += nToCopy;
3416                 
3417         }
3418         
3419         return nDone;
3420 }
3421
3422
3423
3424 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes)
3425 {       
3426         
3427         int chunk;
3428         int start;
3429         int nToCopy;
3430         int n = nBytes;
3431         int nDone = 0;
3432         int nToWriteBack;
3433         int startOfWrite = offset;
3434         int chunkWritten = 0;
3435         int nBytesRead;
3436         
3437         yaffs_Device *dev;
3438         
3439         dev = in->myDev;
3440         
3441         
3442         while(n > 0 && chunkWritten >= 0)
3443         {
3444                 chunk = offset / YAFFS_BYTES_PER_CHUNK + 1;
3445                 start = offset % YAFFS_BYTES_PER_CHUNK;
3446                 
3447
3448                 // OK now check for the curveball where the start and end are in
3449                 // the same chunk.
3450                 
3451                 if(     (start + n) < YAFFS_BYTES_PER_CHUNK)
3452                 {
3453                         nToCopy = n;
3454                         
3455                         // Now folks, to calculate how many bytes to write back....
3456                         // If we're overwriting and not writing to then end of file then
3457                         // we need to write back as much as was there before.
3458                         
3459                         nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * YAFFS_BYTES_PER_CHUNK);
3460                         
3461                         if(nBytesRead > YAFFS_BYTES_PER_CHUNK)
3462                         {
3463                                 nBytesRead = YAFFS_BYTES_PER_CHUNK;
3464                         }
3465                         
3466                         nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3467                         
3468                 }
3469                 else
3470                 {
3471                         nToCopy = YAFFS_BYTES_PER_CHUNK - start;
3472                         nToWriteBack = YAFFS_BYTES_PER_CHUNK;
3473                 }
3474         
3475                 if(nToCopy != YAFFS_BYTES_PER_CHUNK)
3476                 {
3477                         // An incomplete start or end chunk (or maybe both start and end chunk)
3478                         if(dev->nShortOpCaches > 0)
3479                         {
3480                                 yaffs_ChunkCache *cache;
3481                                 // If we can't find the data in the cache, then load it up.
3482                                 cache = yaffs_FindChunkCache(in,chunk);
3483                                 if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev))
3484                                 {
3485                                         cache = yaffs_GrabChunkCache(in->myDev);
3486                                         cache->object = in;
3487                                         cache->chunkId = chunk;
3488                                         cache->dirty = 0;
3489                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);            
3490                                 }
3491                         
3492                                 if(cache)
3493                                 {       
3494                                         yaffs_UseChunkCache(dev,cache,1);
3495                                         memcpy(&cache->data[start],buffer,nToCopy);
3496                                         cache->nBytes = nToWriteBack;
3497                                 }
3498                                 else
3499                                 {
3500                                         chunkWritten = -1; // fail the write
3501                                 }
3502                         }
3503                         else
3504                         {
3505                                 // An incomplete start or end chunk (or maybe both start and end chunk)
3506                                 // Read into the local buffer then copy, then copy over and write back.
3507                 
3508                                 yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer);
3509                                 
3510                                 memcpy(&dev->localBuffer[start],buffer,nToCopy);
3511                         
3512                                 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,nToWriteBack,0);
3513                         
3514                                 //T(("Write with readback to chunk %d %d  start %d  copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3515                         }
3516                         
3517                 }
3518                 else
3519                 {
3520                         
3521 #ifdef CONFIG_YAFFS_WINCE
3522                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3523                         // This is because we otherwise screw up WinCE's memory mapper
3524                         memcpy(dev->localBuffer,buffer,YAFFS_BYTES_PER_CHUNK);
3525                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,YAFFS_BYTES_PER_CHUNK,0);
3526 #else
3527                         // A full chunk. Write directly from the supplied buffer.
3528                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0);
3529 #endif
3530                         // Since we've overwritten the cached data, we better invalidate it.
3531                         yaffs_InvalidateChunkCache(in,chunk);
3532                         //T(("Write to chunk %d %d\n",chunk,chunkWritten));
3533                 }
3534                 
3535                 if(chunkWritten >= 0)
3536                 {
3537                         n -= nToCopy;
3538                         offset += nToCopy;
3539                         buffer += nToCopy;
3540                         nDone += nToCopy;
3541                 }
3542                 
3543         }
3544         
3545         // Update file object
3546         
3547         if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
3548         {
3549                 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
3550         }
3551         
3552         in->dirty = 1;
3553         
3554         return nDone;
3555 }
3556
3557
3558 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
3559 {
3560         int i;
3561         int chunkId;
3562         int oldFileSize = in->variant.fileVariant.fileSize;
3563         int sizeOfPartialChunk = newSize % YAFFS_BYTES_PER_CHUNK;
3564         
3565         yaffs_Device *dev = in->myDev;
3566         
3567
3568         yaffs_FlushFilesChunkCache(in); 
3569         yaffs_InvalidateWholeChunkCache(in);
3570         
3571         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
3572         {
3573                 return yaffs_GetFileSize(in);
3574         }
3575         
3576         if(newSize < oldFileSize)
3577         {
3578                 
3579                 int lastDel = 1 + (oldFileSize-1)/YAFFS_BYTES_PER_CHUNK;
3580                 
3581                 int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/
3582                                                         YAFFS_BYTES_PER_CHUNK;
3583
3584                 // Delete backwards so that we don't end up with holes if
3585                 // power is lost part-way through the operation.
3586                 for(i = lastDel; i >= startDel; i--)
3587                 {
3588                         // NB this could be optimised somewhat,
3589                         // eg. could retrieve the tags and write them without
3590                         // using yaffs_DeleteChunk
3591
3592                         chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
3593                         if(chunkId <= 0 || chunkId >= (dev->endBlock * 32))
3594                         {
3595                                 //T(("Found daft chunkId %d for %d\n",chunkId,i));
3596                         }
3597                         else
3598                         {
3599                                 in->nDataChunks--;
3600                                 yaffs_DeleteChunk(dev,chunkId,1);
3601                         }
3602                 }
3603                 
3604                 
3605                 if(sizeOfPartialChunk != 0)
3606                 {
3607                         int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK;
3608                         
3609                         // Got to read and rewrite the last chunk with its new size.
3610                         yaffs_ReadChunkDataFromObject(in,lastChunk,dev->localBuffer);
3611                         
3612                         yaffs_WriteChunkDataToObject(in,lastChunk,dev->localBuffer,sizeOfPartialChunk,1);
3613                                 
3614                 }
3615                 
3616                 in->variant.fileVariant.fileSize = newSize;
3617                 
3618                 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
3619                 
3620                 return newSize;
3621                 
3622         }
3623         else
3624         {
3625                 return oldFileSize;
3626         }
3627 }
3628
3629
3630 loff_t yaffs_GetFileSize(yaffs_Object *obj)
3631 {
3632         obj = yaffs_GetEquivalentObject(obj);
3633         
3634         switch(obj->variantType)
3635         {
3636                 case YAFFS_OBJECT_TYPE_FILE: 
3637                         return obj->variant.fileVariant.fileSize;
3638                 case YAFFS_OBJECT_TYPE_SYMLINK:
3639                         return strlen(obj->variant.symLinkVariant.alias);
3640                 default:
3641                         return 0;
3642         }
3643 }
3644
3645
3646
3647 // yaffs_FlushFile() updates the file's
3648 // objectId in NAND
3649
3650 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
3651 {
3652         int retVal;
3653         if(in->dirty)
3654         {
3655                 //T(("flushing object header\n"));
3656                 
3657                 yaffs_FlushFilesChunkCache(in);
3658                 if(updateTime)
3659                 {
3660 #ifdef CONFIG_YAFFS_WINCE
3661                         yfsd_WinFileTimeNow(in->win_mtime);
3662 #else
3663                         in->st_mtime = CURRENT_TIME;
3664 #endif
3665                 }
3666
3667                 retVal = (yaffs_UpdateObjectHeader(in,NULL,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
3668         }
3669         else
3670         {
3671                 retVal = YAFFS_OK;
3672         }
3673         
3674         return retVal;
3675         
3676 }
3677
3678
3679 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
3680 {
3681
3682         // First off, invalidate the file's data in the cache, without flushing.
3683         yaffs_InvalidateWholeChunkCache(in);
3684         
3685         yaffs_RemoveObjectFromDirectory(in);
3686         yaffs_DeleteChunk(in->myDev,in->chunkId,1);
3687 #ifdef __KERNEL__
3688         if(in->myInode)
3689         {
3690                 in->myInode->u.generic_ip = NULL;
3691                 in->myInode = 0;
3692         }
3693 #endif
3694         yaffs_FreeObject(in);
3695         return YAFFS_OK;
3696
3697 }
3698
3699 // yaffs_DeleteFile deletes the whole file data
3700 // and the inode associated with the file.
3701 // It does not delete the links associated with the file.
3702 static int yaffs_UnlinkFile(yaffs_Object *in)
3703 {
3704
3705 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
3706
3707         // Delete the file data & tnodes
3708
3709          yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
3710          
3711
3712         yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
3713         
3714         return  yaffs_DoGenericObjectDeletion(in);
3715 #else
3716         int retVal;
3717         int immediateDeletion=0;
3718         retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
3719         if(1 || // Ignore the result of the change name. This will only fail
3720                         // if the disk was completely full (an error condition)
3721                         // We ignore the error so we can delete files to recover the disk
3722            retVal == YAFFS_OK)
3723         {
3724                 //in->unlinked = 1;
3725                 //in->myDev->nUnlinkedFiles++;
3726                 //in->renameAllowed = 0;
3727 #ifdef __KERNEL__
3728                 if(!in->myInode)
3729                 {
3730                         immediateDeletion = 1;
3731
3732                 }
3733 #else
3734                 if(in->inUse <= 0)
3735                 {
3736                         immediateDeletion = 1;
3737
3738                 }
3739 #endif
3740                 
3741                 if(immediateDeletion)
3742                 {
3743                         T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
3744                         in->deleted=1;
3745                         in->myDev->nDeletedFiles++;
3746                         yaffs_SoftDeleteFile(in);
3747                 }
3748         
3749         }
3750         return retVal;
3751
3752         
3753 #endif
3754 }
3755
3756 int yaffs_DeleteFile(yaffs_Object *in)
3757 {
3758         int retVal = YAFFS_OK;
3759         
3760         if(in->nDataChunks > 0)
3761         {
3762                 // Use soft deletion
3763                 if(!in->unlinked)
3764                 {
3765                         retVal = yaffs_UnlinkFile(in);
3766                 }
3767                 if(retVal == YAFFS_OK && 
3768                 in->unlinked &&
3769                 !in->deleted)
3770                 {
3771                         in->deleted = 1;
3772                         in->myDev->nDeletedFiles++;
3773                         yaffs_SoftDeleteFile(in);
3774                 }
3775                 return in->deleted ? YAFFS_OK : YAFFS_FAIL;     
3776         }
3777         else
3778         {
3779                 // The file has no data chunks so we toss it immediately
3780                 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
3781                 in->variant.fileVariant.top = NULL;
3782                 yaffs_DoGenericObjectDeletion(in);      
3783                 
3784                 return YAFFS_OK;        
3785         }
3786 }
3787
3788 static int yaffs_DeleteDirectory(yaffs_Object *in)
3789 {
3790         //First check that the directory is empty.
3791         if(list_empty(&in->variant.directoryVariant.children))
3792         {
3793                 return  yaffs_DoGenericObjectDeletion(in);
3794         }
3795         
3796         return YAFFS_FAIL;
3797         
3798 }
3799
3800 static int yaffs_DeleteSymLink(yaffs_Object *in)
3801 {
3802         YFREE(in->variant.symLinkVariant.alias);
3803
3804         return  yaffs_DoGenericObjectDeletion(in);
3805 }
3806
3807 static int yaffs_DeleteHardLink(yaffs_Object *in)
3808 {
3809         // remove this hardlink from the list assocaited with the equivalent
3810         // object
3811         list_del(&in->hardLinks);
3812         return  yaffs_DoGenericObjectDeletion(in);      
3813 }
3814
3815
3816 static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj)
3817 {
3818         switch(obj->variantType)
3819         {
3820                 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
3821                 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
3822                 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
3823                 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
3824                 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
3825                 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
3826         }
3827 }
3828
3829
3830 static int yaffs_UnlinkWorker(yaffs_Object *obj)
3831 {
3832
3833         
3834         if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
3835         {
3836                 return  yaffs_DeleteHardLink(obj);
3837         }
3838         else if(!list_empty(&obj->hardLinks))
3839         {       
3840                 // Curve ball: We're unlinking an object that has a hardlink.
3841                 //
3842                 //      This problem arises because we are not strictly following
3843                 //  The Linux link/inode model.
3844                 //
3845                 // We can't really delete the object.
3846                 // Instead, we do the following:
3847                 // - Select a hardlink.
3848                 // - Unhook it from the hard links
3849                 // - Unhook it from its parent directory (so that the rename can work)
3850                 // - Rename the object to the hardlink's name.
3851                 // - Delete the hardlink
3852                 
3853                 
3854                 yaffs_Object *hl;
3855                 int retVal;
3856                 char name[YAFFS_MAX_NAME_LENGTH+1];
3857                 
3858                 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
3859                 
3860                 list_del_init(&hl->hardLinks);
3861                 list_del_init(&hl->siblings);
3862                 
3863                 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
3864                 
3865                 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
3866                 
3867                 if(retVal == YAFFS_OK)
3868                 {
3869                         retVal = yaffs_DoGenericObjectDeletion(hl);
3870                 }
3871                 return retVal;
3872                                 
3873         }
3874         else
3875         {
3876                 switch(obj->variantType)
3877                 {
3878                         case YAFFS_OBJECT_TYPE_FILE:
3879                                 return yaffs_UnlinkFile(obj);
3880                                 break;
3881                         case YAFFS_OBJECT_TYPE_DIRECTORY:
3882                                 return yaffs_DeleteDirectory(obj);
3883                                 break;
3884                         case YAFFS_OBJECT_TYPE_SYMLINK:
3885                                 return yaffs_DeleteSymLink(obj);
3886                                 break;
3887                         case YAFFS_OBJECT_TYPE_HARDLINK:
3888                         case YAFFS_OBJECT_TYPE_UNKNOWN:
3889                         default:
3890                                 return YAFFS_FAIL;
3891                 }
3892         }
3893 }
3894
3895 int yaffs_Unlink(yaffs_Object *dir, const char *name)
3896 {
3897         yaffs_Object *obj;
3898         
3899          obj = yaffs_FindObjectByName(dir,name);
3900          
3901          if(obj && obj->unlinkAllowed)
3902          {
3903                 return yaffs_UnlinkWorker(obj);
3904          }
3905          
3906          return YAFFS_FAIL;
3907         
3908 }
3909
3910 //////////////// Initialisation Scanning /////////////////
3911
3912
3913
3914 // For now we use the SmartMedia check.
3915 // We look at the blockStatus byte in the first two chunks
3916 // These must be 0xFF to pass as OK.
3917 // todo: this function needs to be modifyable foir different NAND types
3918 // and different chunk sizes.  Suggest make this into a per-device configurable
3919 // function.
3920 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
3921 {
3922         yaffs_Spare spare;
3923         
3924         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&spare,1);
3925 #if 1
3926         if(yaffs_CountBits(spare.blockStatus) < 7)
3927         {
3928                 return 1;
3929         }
3930 #else
3931         if(spare.blockStatus != 0xFF)
3932         {
3933                 return 1;
3934         }
3935 #endif
3936         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
3937
3938 #if 1
3939         if(yaffs_CountBits(spare.blockStatus) < 7)
3940         {
3941                 return 1;
3942         }
3943 #else
3944         if(spare.blockStatus != 0xFF)
3945         {
3946                 return 1;
3947         }
3948 #endif
3949         
3950         return 0;
3951         
3952 }
3953
3954 static int yaffs_Scan(yaffs_Device *dev)
3955 {
3956         yaffs_Spare spare;
3957         yaffs_Tags tags;
3958         int blk;
3959         int chunk;
3960         int c;
3961         int deleted;
3962         yaffs_BlockState state;
3963         yaffs_Object *hardList = NULL;
3964         yaffs_Object *hl;
3965         yaffs_BlockInfo *bi;
3966
3967         
3968         yaffs_ObjectHeader *oh;
3969         yaffs_Object *in;
3970         yaffs_Object *parent;
3971         
3972         __u8 chunkData[YAFFS_BYTES_PER_CHUNK];
3973         
3974         
3975         // Scan all the blocks...
3976         
3977         for(blk = dev->startBlock; blk <= dev->endBlock; blk++)
3978         {
3979                 deleted = 0;
3980                 bi = yaffs_GetBlockInfo(dev,blk);
3981                 yaffs_ClearChunkBits(dev,blk);
3982                 bi->pagesInUse = 0;
3983                 bi->softDeletions = 0;
3984                 state = YAFFS_BLOCK_STATE_SCANNING;
3985                 
3986                 
3987                 if(yaffs_IsBlockBad(dev,blk))
3988                 {
3989                         state = YAFFS_BLOCK_STATE_DEAD;
3990                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
3991                 }
3992                 
3993                 // Read each chunk in the block.
3994                 
3995                 for(c = 0; c < dev->nChunksPerBlock && 
3996                                    state == YAFFS_BLOCK_STATE_SCANNING; c++)
3997                 {
3998                         // Read the spare area and decide what to do
3999                         chunk = blk * dev->nChunksPerBlock + c;
4000                         
4001                         yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare,1);
4002
4003                         
4004                         // This block looks ok, now what's in this chunk?
4005                         yaffs_GetTagsFromSpare(dev,&spare,&tags);
4006                         
4007                         if(yaffs_CountBits(spare.pageStatus) < 6)
4008                         {
4009                                 // A deleted chunk
4010                                 deleted++;
4011                                 dev->nFreeChunks ++;
4012                                 //T((" %d %d deleted\n",blk,c));
4013                         }
4014                         else if(tags.objectId == YAFFS_UNUSED_OBJECT_ID)
4015                         {
4016                                 // An unassigned chunk in the block
4017                                 // This means that either the block is empty or 
4018                                 // this is the one being allocated from
4019                                 
4020                                 if(c == 0)
4021                                 {
4022                                         // the block is unused
4023                                         state = YAFFS_BLOCK_STATE_EMPTY;
4024                                         dev->nErasedBlocks++;
4025                                 }
4026                                 else
4027                                 {
4028                                         // this is the block being allocated from
4029                                         T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4030                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
4031                                         dev->allocationBlock = blk;
4032                                         dev->allocationPage = c;
4033                                 }
4034
4035                                 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4036                         }
4037                         else if(tags.chunkId > 0)
4038                         {
4039                                 int endpos;
4040                                 // A data chunk.
4041                                 yaffs_SetChunkBit(dev,blk,c);
4042                                 bi->pagesInUse++;
4043                                                                 
4044                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4045                                 // PutChunkIntoFIle checks for a clash (two data chunks with
4046                                 // the same chunkId).
4047                                 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4048                                 endpos = (tags.chunkId - 1)* YAFFS_BYTES_PER_CHUNK + tags.byteCount;
4049                                 if(in->variant.fileVariant.scannedFileSize <endpos)
4050                                 {
4051                                         in->variant.fileVariant.scannedFileSize = endpos;
4052 #ifndef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
4053                                                 in->variant.fileVariant.fileSize =      
4054                                                         in->variant.fileVariant.scannedFileSize;
4055 #endif
4056
4057                                 }
4058                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
4059                         }
4060                         else
4061                         {
4062                                 // chunkId == 0, so it is an ObjectHeader.
4063                                 // Thus, we read in the object header and make the object
4064                                 yaffs_SetChunkBit(dev,blk,c);
4065                                 bi->pagesInUse++;
4066                                                         
4067                                 yaffs_ReadChunkFromNAND(dev,chunk,chunkData,NULL,1);
4068                                 
4069                                 oh = (yaffs_ObjectHeader *)chunkData;
4070                                 
4071                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4072                                 
4073                                 if(in->valid)
4074                                 {
4075                                         // We have already filled this one. We have a duplicate and need to resolve it.
4076                                         
4077                                         unsigned existingSerial = in->serial;
4078                                         unsigned newSerial = tags.serialNumber;
4079                                         
4080                                         if(((existingSerial+1) & 3) == newSerial)
4081                                         {
4082                                                 // Use new one - destroy the exisiting one
4083                                                 yaffs_DeleteChunk(dev,in->chunkId,1);
4084                                                 in->valid = 0;
4085                                         }
4086                                         else
4087                                         {
4088                                                 // Use existing - destroy this one.
4089                                                 yaffs_DeleteChunk(dev,chunk,1);
4090                                         }
4091                                 }
4092                                 
4093                                 if(!in->valid &&
4094                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
4095                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4096                                 {
4097                                         // We only load some info, don't fiddle with directory structure
4098                                         in->valid = 1;
4099                                         in->variantType = oh->type;
4100         
4101                                         in->st_mode  = oh->st_mode;
4102 #ifdef CONFIG_YAFFS_WINCE
4103                                         in->win_atime[0] = oh->win_atime[0];
4104                                         in->win_ctime[0] = oh->win_ctime[0];
4105                                         in->win_mtime[0] = oh->win_mtime[0];
4106                                         in->win_atime[1] = oh->win_atime[1];
4107                                         in->win_ctime[1] = oh->win_ctime[1];
4108                                         in->win_mtime[1] = oh->win_mtime[1];
4109 #else
4110                                         in->st_uid   = oh->st_uid;
4111                                         in->st_gid   = oh->st_gid;
4112                                         in->st_atime = oh->st_atime;
4113                                         in->st_mtime = oh->st_mtime;
4114                                         in->st_ctime = oh->st_ctime;
4115                                         in->st_rdev = oh->st_rdev;
4116 #endif
4117                                         in->chunkId  = chunk;
4118
4119                                 }
4120                                 else if(!in->valid)
4121                                 {
4122                                         // we need to load this info
4123                                 
4124                                         in->valid = 1;
4125                                         in->variantType = oh->type;
4126         
4127                                         in->st_mode  = oh->st_mode;
4128 #ifdef CONFIG_YAFFS_WINCE
4129                                         in->win_atime[0] = oh->win_atime[0];
4130                                         in->win_ctime[0] = oh->win_ctime[0];
4131                                         in->win_mtime[0] = oh->win_mtime[0];
4132                                         in->win_atime[1] = oh->win_atime[1];
4133                                         in->win_ctime[1] = oh->win_ctime[1];
4134                                         in->win_mtime[1] = oh->win_mtime[1];
4135 #else
4136                                         in->st_uid   = oh->st_uid;
4137                                         in->st_gid   = oh->st_gid;
4138                                         in->st_atime = oh->st_atime;
4139                                         in->st_mtime = oh->st_mtime;
4140                                         in->st_ctime = oh->st_ctime;
4141                                         in->st_rdev = oh->st_rdev;
4142 #endif
4143                                         in->chunkId  = chunk;
4144
4145                                         yaffs_SetObjectName(in,oh->name);
4146                                         in->dirty = 0;
4147                                                         
4148                                         // directory stuff...
4149                                         // hook up to parent
4150         
4151                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4152                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4153                                         {
4154                                                 // Set up as a directory
4155                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4156                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4157                                         }
4158                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4159                                         {
4160                                                 // Hoosterman, another problem....
4161                                                 // We're trying to use a non-directory as a directory
4162                                                 // Todo ... handle
4163                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan" TENDSTR)));
4164
4165                                         }
4166                                 
4167                                         yaffs_AddObjectToDirectory(parent,in);
4168                                         if(parent == dev->unlinkedDir)
4169                                         {
4170                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4171                                                 dev->nDeletedFiles++;
4172                                         }
4173                                 
4174                                         // Note re hardlinks.
4175                                         // Since we might scan a hardlink before its equivalent object is scanned
4176                                         // we put them all in a list.
4177                                         // After scanning is complete, we should have all the objects, so we run through this
4178                                         // list and fix up all the chains.              
4179         
4180                                         switch(in->variantType)
4181                                         {
4182                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
4183                                                         break;
4184                                                 case YAFFS_OBJECT_TYPE_FILE:
4185 #ifdef CONFIG_YAFFS_USE_HEADER_FILE_SIZE
4186                                                         in->variant.fileVariant.fileSize = oh->fileSize;
4187 #endif
4188                                                         break;
4189                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
4190                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4191                                                         in->hardLinks.next = (struct list_head *)hardList;
4192                                                         hardList = in;
4193                                                         break;
4194                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
4195                                                         break;
4196                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4197                                                         break;
4198                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
4199                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4200                                                         break;
4201                                         }
4202                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
4203                                 }
4204                         }
4205                 }
4206                 
4207                 if(state == YAFFS_BLOCK_STATE_SCANNING)
4208                 {
4209                         // If we got this far while scanning, then the block is fully allocated.
4210                         state = YAFFS_BLOCK_STATE_FULL; 
4211                 }
4212                 
4213                 bi->blockState = state;
4214                 
4215                 // Now let's see if it was dirty
4216                 if(     bi->pagesInUse == 0 &&
4217                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4218             {
4219                 yaffs_BlockBecameDirty(dev,blk);
4220             }
4221
4222         }
4223         
4224         // Fix up the hard link chains.
4225         // We should now have scanned all the objects, now it's time to add these 
4226         // hardlinks.
4227         while(hardList)
4228         {
4229                 hl = hardList;
4230                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4231                 
4232                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4233                 
4234                 if(in)
4235                 {
4236                         // Add the hardlink pointers
4237                         hl->variant.hardLinkVariant.equivalentObject=in;
4238                         list_add(&hl->hardLinks,&in->hardLinks);
4239                 }
4240                 else
4241                 {
4242                         //Todo Need to report/handle this better.
4243                         // Got a problem... hardlink to a non-existant object
4244                         hl->variant.hardLinkVariant.equivalentObject=NULL;
4245                         INIT_LIST_HEAD(&hl->hardLinks);
4246                         
4247                 }
4248                 
4249         }
4250         
4251         {
4252                 struct list_head *i;    
4253                 struct list_head *n;
4254                         
4255                 yaffs_Object *l;
4256                 // Soft delete all the unlinked files
4257                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4258                 {
4259                         if(i)
4260                         {
4261                                 l = list_entry(i, yaffs_Object,siblings);
4262                                 if(l->deleted)
4263                                 {
4264                                         yaffs_SoftDeleteFile(l);                
4265                                 }
4266                         }
4267                 }       
4268         }
4269         
4270         
4271         return YAFFS_OK;
4272 }
4273
4274
4275 ////////////////////////// Directory Functions /////////////////////////
4276
4277
4278 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
4279 {
4280
4281         if(obj->siblings.prev == NULL)
4282         {
4283                 // Not initialised
4284                 INIT_LIST_HEAD(&obj->siblings);
4285                 
4286         }
4287         else if(!list_empty(&obj->siblings))
4288         {
4289                 // If it is holed up somewhere else, un hook it
4290                 list_del_init(&obj->siblings);
4291         }
4292         // Now add it
4293         list_add(&obj->siblings,&directory->variant.directoryVariant.children);
4294         obj->parent = directory;
4295         
4296         if(directory == obj->myDev->unlinkedDir)
4297         {
4298                 obj->unlinked = 1;
4299                 obj->myDev->nUnlinkedFiles++;
4300                 obj->renameAllowed = 0;
4301         }
4302 }
4303
4304 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
4305 {
4306         list_del_init(&obj->siblings);
4307         obj->parent = NULL;
4308 }
4309
4310 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const char *name)
4311 {
4312         int sum;
4313         
4314         struct list_head *i;
4315         char buffer[YAFFS_MAX_NAME_LENGTH+1];
4316         
4317         yaffs_Object *l;
4318         
4319         sum = yaffs_CalcNameSum(name);
4320         
4321         list_for_each(i,&directory->variant.directoryVariant.children)
4322         {
4323                 if(i)
4324                 {
4325                         l = list_entry(i, yaffs_Object,siblings);
4326                 
4327                         // Special case for lost-n-found
4328                         if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4329                         {
4330                                 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
4331                                 {
4332                                         return l;
4333                                 }
4334                         }
4335                         else if(yaffs_SumCompare(l->sum, sum))
4336                         {
4337                                 // Do a real check
4338                                 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
4339                                 if(yaffs_strcmp(name,buffer) == 0)
4340                                 {
4341                                         return l;
4342                                 }
4343                         
4344                         }                       
4345                 }
4346         }
4347         
4348         return NULL;
4349 }
4350
4351
4352 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
4353 {
4354         struct list_head *i;    
4355         yaffs_Object *l;
4356         
4357         
4358         list_for_each(i,&theDir->variant.directoryVariant.children)
4359         {
4360                 if(i)
4361                 {
4362                         l = list_entry(i, yaffs_Object,siblings);
4363                         if(l && !fn(l))
4364                         {
4365                                 return YAFFS_FAIL;
4366                         }
4367                 }
4368         }
4369         
4370         return YAFFS_OK;
4371
4372 }
4373
4374
4375 // GetEquivalentObject dereferences any hard links to get to the
4376 // actual object.
4377
4378 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
4379 {
4380         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4381         {
4382                 // We want the object id of the equivalent object, not this one
4383                 obj = obj->variant.hardLinkVariant.equivalentObject;
4384         }
4385         return obj;
4386
4387 }
4388
4389 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize)
4390 {
4391         memset(name,0,buffSize);
4392         
4393         if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
4394         {
4395                 strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
4396         }
4397         else if(obj->chunkId <= 0)
4398         {
4399                 char locName[20];
4400                 // make up a name
4401                 sprintf(locName,"%s%d",YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
4402                 strncpy(name,locName,buffSize - 1);
4403
4404         }
4405 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4406         else if(obj->shortName[0])
4407         {
4408                 strcpy(name,obj->shortName);
4409         }
4410 #endif
4411         else
4412         {
4413                 __u8 buffer[YAFFS_BYTES_PER_CHUNK];
4414                 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
4415
4416                 memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
4417         
4418                 if(obj->chunkId >= 0)
4419                 {
4420                         yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL,1);
4421                 }
4422                 strncpy(name,oh->name,buffSize - 1);
4423         }
4424         
4425         return strlen(name);
4426 }
4427
4428 int yaffs_GetObjectFileLength(yaffs_Object *obj)
4429 {
4430         
4431         // Dereference any hard linking
4432         obj = yaffs_GetEquivalentObject(obj);
4433         
4434         if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
4435         {
4436                 return obj->variant.fileVariant.fileSize;
4437         }
4438         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
4439         {
4440                 return strlen(obj->variant.symLinkVariant.alias);
4441         }
4442         else
4443         {
4444                 // Only a directory should drop through to here
4445                 return YAFFS_BYTES_PER_CHUNK;
4446         }       
4447 }
4448
4449 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
4450 {
4451         int count = 0; 
4452         struct list_head *i;
4453         
4454         if(!obj->unlinked)
4455         {
4456                 count++;        // the object itself
4457         }
4458         list_for_each(i,&obj->hardLinks)
4459         {
4460                 count++;        // add the hard links;
4461         }
4462         return count;
4463         
4464 }
4465
4466
4467 int yaffs_GetObjectInode(yaffs_Object *obj)
4468 {
4469         obj = yaffs_GetEquivalentObject(obj);
4470         
4471         return obj->objectId;
4472 }
4473
4474 unsigned yaffs_GetObjectType(yaffs_Object *obj)
4475 {
4476         obj = yaffs_GetEquivalentObject(obj);
4477         
4478         switch(obj->variantType)
4479         {
4480                 case YAFFS_OBJECT_TYPE_FILE:            return DT_REG; break;
4481                 case YAFFS_OBJECT_TYPE_DIRECTORY:       return DT_DIR; break;
4482                 case YAFFS_OBJECT_TYPE_SYMLINK:         return DT_LNK; break;
4483                 case YAFFS_OBJECT_TYPE_HARDLINK:        return DT_REG; break;
4484                 case YAFFS_OBJECT_TYPE_SPECIAL:         
4485                         if(S_ISFIFO(obj->st_mode)) return DT_FIFO;
4486                         if(S_ISCHR(obj->st_mode)) return DT_CHR;
4487                         if(S_ISBLK(obj->st_mode)) return DT_BLK;
4488                         if(S_ISSOCK(obj->st_mode)) return DT_SOCK;
4489                 default: return DT_REG; break;
4490         }
4491 }
4492
4493 char *yaffs_GetSymlinkAlias(yaffs_Object *obj)
4494 {
4495         obj = yaffs_GetEquivalentObject(obj);
4496         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
4497         {
4498                 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
4499         }
4500         else
4501         {
4502                 return yaffs_CloneString("");
4503         }
4504 }
4505
4506 #ifndef CONFIG_YAFFS_WINCE
4507
4508 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
4509 {
4510         unsigned int valid = attr->ia_valid;
4511         
4512         if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode;
4513         if(valid & ATTR_UID) obj->st_uid = attr->ia_uid;
4514         if(valid & ATTR_GID) obj->st_gid = attr->ia_gid;
4515         
4516         if(valid & ATTR_ATIME) obj->st_atime = attr->ia_atime;
4517         if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime;
4518         if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime;
4519         
4520         if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
4521         
4522         yaffs_UpdateObjectHeader(obj,NULL,1);
4523         
4524         return YAFFS_OK;
4525         
4526 }
4527 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
4528 {
4529         unsigned int valid = 0;
4530         
4531         attr->ia_mode = obj->st_mode;   valid |= ATTR_MODE;
4532         attr->ia_uid = obj->st_uid;             valid |= ATTR_UID;
4533         attr->ia_gid = obj->st_gid;             valid |= ATTR_GID;
4534         
4535         attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME;
4536         attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME;
4537         attr->ia_mtime = obj->st_mtime; valid |= ATTR_MTIME;
4538         
4539         attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
4540         
4541         attr->ia_valid = valid;
4542         
4543         return YAFFS_OK;
4544         
4545 }
4546
4547 #endif
4548
4549 int yaffs_DumpObject(yaffs_Object *obj)
4550 {
4551 //      __u8 buffer[YAFFS_BYTES_PER_CHUNK];
4552         char name[257];
4553 //      yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
4554
4555 //      memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
4556         
4557 //      if(obj->chunkId >= 0)
4558 //      {
4559 //              yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
4560 //      }
4561         
4562         yaffs_GetObjectName(obj,name,256);
4563         
4564         T(YAFFS_TRACE_ALWAYS,(TSTR("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d type %d size %d\n" TENDSTR),
4565                         obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial, 
4566                         obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
4567
4568 #if 0
4569         YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
4570                         obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial, 
4571                         obj->sum, obj->chunkId));
4572                 switch(obj->variantType)
4573         {
4574                 case YAFFS_OBJECT_TYPE_FILE: 
4575                         YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
4576                         break;
4577                 case YAFFS_OBJECT_TYPE_DIRECTORY:
4578                         YPRINTF((" DIRECTORY\n"));
4579                         break;
4580                 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
4581                 case YAFFS_OBJECT_TYPE_SYMLINK:
4582                 case YAFFS_OBJECT_TYPE_UNKNOWN:
4583                 default:
4584         }
4585 #endif
4586         
4587         return YAFFS_OK;
4588 }
4589
4590
4591 ///////////////////////// Initialisation code ///////////////////////////
4592
4593
4594
4595 int yaffs_GutsInitialise(yaffs_Device *dev)
4596 {
4597         unsigned x;
4598         int bits;
4599         int extraBits;
4600         int nBlocks;
4601
4602
4603         
4604         if(!yaffs_CheckStructures())
4605         {
4606                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
4607                 return YAFFS_FAIL;
4608         }
4609
4610         if(dev->isMounted)
4611         {
4612                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
4613                 return YAFFS_FAIL;
4614         }
4615
4616         dev->isMounted = 1;
4617         
4618         if(dev->startBlock <= 0 ||
4619            (dev->endBlock - dev->startBlock) < 10)
4620         {
4621                 T(YAFFS_TRACE_ALWAYS,(TSTR("startBlock %d or endBlock %d invalid\n" TENDSTR),
4622                                 dev->startBlock, dev->endBlock));
4623                 return YAFFS_FAIL;
4624         }
4625         
4626         nBlocks = dev->endBlock - dev->startBlock + 1;
4627         
4628
4629                 
4630         // OK now calculate a few things for the device
4631         // Calculate chunkGroupBits. 
4632         // We need to find the next power of 2 > than endBlock
4633         
4634         x = dev->nChunksPerBlock * (dev->endBlock+1);
4635         
4636         for(bits = extraBits = 0; x > 1; bits++)
4637         {
4638                 if(x & 1) extraBits++;
4639                 x >>= 1;
4640         }
4641
4642         if(extraBits > 0) bits++;
4643         
4644         
4645         // Level0 Tnodes are 16 bits, so if the bitwidth of the
4646         // chunk range we're using is greater than 16 we need 
4647         // to figure out chunk shift and chunkGroupSize
4648         if(bits <= 16) 
4649         {
4650                 dev->chunkGroupBits = 0;
4651         }
4652         else
4653         {
4654                 dev->chunkGroupBits = bits - 16;
4655         }
4656         dev->chunkGroupSize = 1 << dev->chunkGroupBits;
4657         
4658         // Stuff to be taken out later
4659         dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
4660         dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
4661         
4662         
4663         // More device initialisation
4664         dev->garbageCollections = 0;
4665         dev->passiveGarbageCollections = 0;
4666         dev->currentDirtyChecker = 0;
4667         dev->bufferedBlock = -1;
4668         dev->doingBufferedBlockRewrite = 0;
4669         dev->nDeletedFiles = 0;
4670         dev->nBackgroundDeletions=0;
4671         dev->nUnlinkedFiles = 0;
4672         dev->eccFixed=0;
4673         dev->eccUnfixed=0;
4674         dev->tagsEccFixed=0;
4675         dev->tagsEccUnfixed=0;
4676         
4677         dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
4678         
4679
4680         
4681         
4682         yaffs_InitialiseBlocks(dev,nBlocks);
4683         
4684         yaffs_InitialiseTnodes(dev);
4685
4686         yaffs_InitialiseObjects(dev);
4687         
4688         if(dev->nShortOpCaches > 0)
4689         { 
4690                 int i;
4691                 
4692                 if(dev->nShortOpCaches >  YAFFS_MAX_SHORT_OP_CACHES)
4693                 {
4694                         dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
4695                 }
4696                 
4697                 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
4698                 
4699                 for(i=0; i < dev->nShortOpCaches; i++)
4700                 {
4701                         dev->srCache[i].object = NULL;
4702                         dev->srCache[i].lastUse = 0;
4703                         dev->srCache[i].dirty = 0;
4704                 }
4705                 dev->srLastUse = 0;
4706         }
4707
4708         dev->cacheHits = 0;
4709         
4710         
4711         // Initialise the unlinked, root and lost and found directories
4712         dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = NULL;
4713         
4714         dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
4715
4716         dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
4717         dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
4718         yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
4719         
4720                 
4721         // Now scan the flash.  
4722         yaffs_Scan(dev);
4723         
4724         // Zero out stats
4725         dev->nPageReads = 0;
4726     dev->nPageWrites =  0;
4727         dev->nBlockErasures = 0;
4728         dev->nGCCopies = 0;
4729         dev->nRetriedWrites = 0;
4730         dev->nRetiredBlocks = 0;
4731
4732         
4733         return YAFFS_OK;
4734                 
4735 }
4736
4737 void yaffs_Deinitialise(yaffs_Device *dev)
4738 {
4739         if(dev->isMounted)
4740         {
4741         
4742                 yaffs_DeinitialiseBlocks(dev);
4743                 yaffs_DeinitialiseTnodes(dev);
4744                 yaffs_DeinitialiseObjects(dev);
4745         }
4746         
4747 }
4748
4749 #if 0
4750
4751 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
4752 {
4753         int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
4754         
4755         struct list_head *i;    
4756         yaffs_Object *l;
4757         
4758         
4759         // To the free chunks add the chunks that are in the deleted unlinked files.
4760         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
4761         {
4762                 l = list_entry(i, yaffs_Object,siblings);
4763                 if(l->deleted)
4764                 {
4765                         nFree++;
4766                         nFree += l->nDataChunks;
4767                 }
4768         }
4769         
4770         
4771         printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);   
4772
4773         if(nFree < 0) nFree = 0;
4774
4775         return nFree;   
4776         
4777 }
4778
4779 #endif
4780
4781 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
4782 {
4783         int nFree;
4784         int pending;
4785         int b;
4786         
4787         yaffs_BlockInfo *blk;
4788         
4789         struct list_head *i;    
4790         yaffs_Object *l;
4791         
4792         for(nFree = 0, b = dev->startBlock; b <= dev->endBlock; b++)
4793         {
4794                 blk = yaffs_GetBlockInfo(dev,b);
4795                 
4796                 switch(blk->blockState)
4797                 {
4798                         case YAFFS_BLOCK_STATE_EMPTY:
4799                         case YAFFS_BLOCK_STATE_ALLOCATING: 
4800                         case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break;
4801                         default: break;
4802                 }
4803         }
4804         
4805         
4806         pending = 0;
4807         
4808         // To the free chunks add the chunks that are in the deleted unlinked files.
4809         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
4810         {
4811                 if(i)
4812                 {
4813                         l = list_entry(i, yaffs_Object,siblings);
4814                         if(l->deleted)
4815                         {
4816                                 pending++;
4817                                 pending += l->nDataChunks;
4818                         }
4819                 }
4820         }
4821         
4822         
4823         
4824         //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending);
4825         
4826         if(nFree != dev->nFreeChunks) 
4827         {
4828         //      printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks);
4829         }
4830
4831         nFree += pending;
4832         
4833         if(nFree < 0) nFree = 0;
4834
4835         return nFree;   
4836         
4837 }
4838
4839
4840
4841 /////////////////// YAFFS test code //////////////////////////////////
4842
4843 #define yaffs_CheckStruct(structure,syze, name) \
4844            if(sizeof(structure) != syze) \
4845                { \
4846                  T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
4847                  return YAFFS_FAIL; \
4848                    }
4849                  
4850                  
4851 static int yaffs_CheckStructures(void)
4852 {
4853         yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
4854         yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
4855         yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
4856 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
4857         yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
4858 #endif
4859         yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
4860         
4861         
4862         return YAFFS_OK;
4863 }
4864
4865 #if 0
4866 void yaffs_GutsTest(yaffs_Device *dev)
4867 {
4868         
4869         if(yaffs_CheckStructures() != YAFFS_OK)
4870         {
4871                 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
4872                 return;
4873         }
4874         
4875         yaffs_TnodeTest(dev);
4876         yaffs_ObjectTest(dev);  
4877 }
4878 #endif
4879
4880
4881