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