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