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