Prevent recursive gc
[yaffs2.git] / yaffs_guts.c
1
2 /*
3  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
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.10 2005-07-26 03:05:28 charles Exp $";
18
19 #include "yportenv.h"
20
21 #include "yaffsinterface.h"
22 #include "yaffs_guts.h"
23 #include "yaffs_tagsvalidity.h"
24
25
26 #include "yaffs_tagscompat.h"
27
28 #ifdef CONFIG_YAFFS_WINCE
29 void yfsd_LockYAFFS(BOOL fsLockOnly);
30 void yfsd_UnlockYAFFS(BOOL fsLockOnly);
31 #endif
32
33 #define YAFFS_PASSIVE_GC_CHUNKS 2
34
35 #if 0
36 // Use Steven Hill's ECC struff instead
37 // External functions for ECC on data
38 void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
39 int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
40 #define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc)
41 #define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc)
42 #else
43 #include "yaffs_ecc.h"
44 #endif
45
46 #if 0
47 // countBits is a quick way of counting the number of bits in a byte.
48 // ie. countBits[n] holds the number of 1 bits in a byte with the value n.
49
50 static const char yaffs_countBitsTable[256] =
51 {
52 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
53 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
54 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
55 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
56 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
58 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
59 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
60 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
61 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
62 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
63 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
64 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
65 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
66 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
67 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
68 };
69
70 static int yaffs_CountBits(__u8 x)
71 {
72         int retVal;
73         retVal = yaffs_countBitsTable[x];
74         return retVal;
75 }
76
77 #endif
78
79
80 #if 0
81 // Stuff using yea olde tags
82 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr);
83 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr);
84
85 static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted);
86 static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted);
87 #else
88 #endif
89
90 // NAND access
91
92
93 static Y_INLINE int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags);
94 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *data, yaffs_ExtendedTags *tags);
95 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
96 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state,unsigned *sequenceNumber);
97 // Local prototypes
98 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve);
99 #if 0
100 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev);
101 #endif
102 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan);
103
104 static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type);
105 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj);
106 static int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink);
107 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
108 static int yaffs_CheckStructures(void);
109 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit);
110 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
111
112 static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo);
113
114 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo);
115 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
116
117
118 // Robustification (if it ever comes about...)
119 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND);
120 #if 0
121 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
122 #endif
123 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
124 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags);
125 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND,  const yaffs_ExtendedTags *tags);
126
127 static int  yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND);
128
129 static int yaffs_UnlinkWorker(yaffs_Object *obj);
130 static void yaffs_DestroyObject(yaffs_Object *obj);
131
132 #if 0
133 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize);
134 #endif
135
136 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject);
137
138
139 loff_t yaffs_GetFileSize(yaffs_Object *obj);
140
141
142 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve);
143
144 static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
145
146 #ifdef YAFFS_PARANOID
147 static int yaffs_CheckFileSanity(yaffs_Object *in);
148 #else
149 #define yaffs_CheckFileSanity(in)
150 #endif
151
152 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
153 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
154
155 static int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *buffer, yaffs_ExtendedTags *tags)
156 {
157         chunkInNAND -= dev->chunkOffset;
158         
159         if(dev->readChunkWithTagsFromNAND)
160                 return dev->readChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
161         else
162                 return yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,chunkInNAND,buffer,tags);
163 }
164
165 static Y_INLINE int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_ExtendedTags *tags)
166 {
167         chunkInNAND -= dev->chunkOffset;
168         
169         if(tags)
170         {
171                 tags->sequenceNumber = dev->sequenceNumber;
172                 tags->chunkUsed = 1;
173                 if(!yaffs_ValidateTags(tags))
174                 {
175                         T(YAFFS_TRACE_ERROR,(TSTR("Writing uninitialised tags" TENDSTR)));
176                         YBUG();
177                 }
178                 T(YAFFS_TRACE_WRITE,(TSTR("Writing chunk %d tags %d %d"TENDSTR),chunkInNAND,tags->objectId,tags->chunkId));
179         }
180         else
181         {
182                         T(YAFFS_TRACE_ERROR,(TSTR("Writing with no tags" TENDSTR)));
183                         YBUG();
184         }
185
186         if(dev->writeChunkWithTagsToNAND)
187                 return dev->writeChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
188         else
189                 return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,chunkInNAND,buffer,tags);
190 }
191
192 static Y_INLINE int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
193 {
194         blockNo -= dev->blockOffset;
195         
196         if(dev->markNANDBlockBad)
197                 return dev->markNANDBlockBad(dev,blockNo);
198         else
199                 return yaffs_TagsCompatabilityMarkNANDBlockBad(dev,blockNo);
200 }
201 static Y_INLINE int yaffs_QueryInitialBlockState(yaffs_Device *dev,int blockNo, yaffs_BlockState *state, unsigned *sequenceNumber)
202 {
203         blockNo -= dev->blockOffset;
204         
205         if(dev->queryNANDBlock)
206                 return dev->queryNANDBlock(dev,blockNo,state,sequenceNumber);
207         else
208                 return yaffs_TagsCompatabilityQueryNANDBlock(dev,blockNo,state,sequenceNumber);
209 }
210
211 int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND)
212 {
213         int result;
214         
215         blockInNAND -= dev->blockOffset;
216
217         dev->nBlockErasures++;
218         result = dev->eraseBlockInNAND(dev,blockInNAND);
219
220         if(!result)result = dev->eraseBlockInNAND(dev,blockInNAND); // If at first we don't succeed, try again *once*.
221         return result;
222 }
223
224 static int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
225 {
226         return dev->initialiseNAND(dev);
227 }
228
229
230
231
232 // Temporary buffer manipulations
233
234 static __u8 *yaffs_GetTempBuffer(yaffs_Device *dev,int lineNo)
235 {
236         int i,j;
237         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
238         {
239                 if(dev->tempBuffer[i].line == 0)
240                 {
241                         dev->tempBuffer[i].line = lineNo;
242                         if((i+1) > dev->maxTemp)
243                         {
244                                 dev->maxTemp = i + 1;
245                                 for(j = 0; j <= i; j++)
246                                         dev->tempBuffer[j].maxLine = dev->tempBuffer[j].line;
247                         }       
248                         
249                         return dev->tempBuffer[i].buffer;
250                 }
251         }
252
253         T(YAFFS_TRACE_BUFFERS,(TSTR("Out of temp buffers at line %d, other held by lines:"),lineNo));
254         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
255         {
256                 T(YAFFS_TRACE_BUFFERS,(TSTR(" %d "),dev->tempBuffer[i].line));
257         }
258         T(YAFFS_TRACE_BUFFERS,(TSTR(" "TENDSTR)));
259         
260         dev->unmanagedTempAllocations++;
261         // Get an unmanaged one
262         return YMALLOC(dev->nBytesPerChunk);    
263         
264         
265 }
266
267 static void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo)
268 {
269         int i;
270         for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
271         {
272                 if(dev->tempBuffer[i].buffer == buffer)
273                 {
274                         dev->tempBuffer[i].line = 0;
275                         return;
276                 }
277         }
278         
279         if(buffer)
280         {
281                 // assume it is an unmanaged one.
282                 T(YAFFS_TRACE_BUFFERS,(TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),lineNo));
283                 YFREE(buffer);
284                 dev->unmanagedTempDeallocations++;
285         }
286
287 }
288
289
290 // Chunk bitmap manipulations
291
292 static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
293 {
294         if(blk < dev->internalStartBlock || blk > dev->internalEndBlock)
295         {
296                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk));
297                 YBUG();
298         }
299         return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
300 }
301
302 static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev,int blk)
303 {
304         __u8 *blkBits = yaffs_BlockBits(dev,blk);
305
306          memset(blkBits,0,dev->chunkBitmapStride);
307 }
308
309 static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk)
310 {
311         __u8 *blkBits = yaffs_BlockBits(dev,blk);
312         
313         blkBits[chunk/8] &=  ~ (1<<(chunk & 7));
314 }
315
316 static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk)
317 {
318         __u8 *blkBits = yaffs_BlockBits(dev,blk);
319
320         blkBits[chunk/8] |=   (1<<(chunk & 7));
321 }
322
323 static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk)
324 {
325         __u8 *blkBits = yaffs_BlockBits(dev,blk);
326         return (blkBits[chunk/8] &  (1<<(chunk & 7))) ? 1 :0;
327 }
328
329 static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk)
330 {
331         __u8 *blkBits = yaffs_BlockBits(dev,blk);
332         int i;
333         for(i = 0; i < dev->chunkBitmapStride; i++)
334         {
335                 if(*blkBits) return 1;
336                 blkBits++;
337         }
338         return 0;
339 }
340
341
342 static  Y_INLINE int yaffs_HashFunction(int n)
343 {
344         return (n % YAFFS_NOBJECT_BUCKETS);
345 }
346
347
348 yaffs_Object *yaffs_Root(yaffs_Device *dev)
349 {
350         return dev->rootDir;
351 }
352
353 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
354 {
355         return dev->lostNFoundDir;
356 }
357
358
359
360
361 int yaffs_CheckFF(__u8 *buffer,int nBytes)
362 {
363         //Horrible, slow implementation
364         while(nBytes--)
365         {
366                 if(*buffer != 0xFF) return 0;
367                 buffer++;
368         }
369         return 1;
370 }
371
372 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
373 {
374
375         int retval = YAFFS_OK;
376         __u8 *data = yaffs_GetTempBuffer(dev,__LINE__);
377         yaffs_ExtendedTags tags;
378
379 // NCB          dev->readChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
380         yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND,data,&tags);
381         
382         if(!yaffs_CheckFF(data,dev->nBytesPerChunk) || tags.chunkUsed)
383         {
384                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not erased" TENDSTR),chunkInNAND));
385                 retval = YAFFS_FAIL;
386         }
387
388         yaffs_ReleaseTempBuffer(dev,data,__LINE__);
389         
390         return retval;
391         
392 }
393
394
395
396 #if 1
397 static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_ExtendedTags *tags,int useReserve)
398 {
399         int chunk;
400         
401         int writeOk = 1;
402         int attempts = 0;
403         
404
405         
406         do{
407                 chunk = yaffs_AllocateChunk(dev,useReserve);
408         
409                 if(chunk >= 0)
410                 {
411
412                         // First check this chunk is erased...
413 #ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
414                         writeOk = yaffs_CheckChunkErased(dev,chunk);
415 #endif          
416                         if(!writeOk)
417                         {
418                                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk));
419                         }
420                         else
421                         {
422                                 writeOk =  yaffs_WriteChunkWithTagsToNAND(dev,chunk,data,tags);
423                         }
424                         attempts++;
425
426                         if(writeOk)
427                         {
428                                 // Copy the data into the robustification buffer.
429                                 // NB We do this at the end to prevent duplicates in the case of a write error.
430                                 //Todo
431                                 yaffs_HandleWriteChunkOk(dev,chunk,data,tags);
432                         }
433                         else
434                         {
435                                 yaffs_HandleWriteChunkError(dev,chunk);
436                         }
437                 }
438                 
439         } while(chunk >= 0 && ! writeOk);
440         
441         if(attempts > 1)
442         {
443                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts));
444                 dev->nRetriedWrites+= (attempts - 1);   
445         }
446         
447
448         
449         return chunk;
450 }
451 #endif
452
453
454 ///
455 // Functions for robustisizing
456 //
457 //
458
459 static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND)
460 {
461         
462         yaffs_MarkBlockBad(dev,blockInNAND);
463         
464         yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD;
465
466         dev->nRetiredBlocks++;
467 }
468
469
470 #if 0
471 static int yaffs_RewriteBufferedBlock(yaffs_Device *dev)
472 {
473         dev->doingBufferedBlockRewrite = 1;
474         //
475         //      Remove erased chunks
476         //  Rewrite existing chunks to a new block
477         //      Set current write block to the new block
478         
479         dev->doingBufferedBlockRewrite = 0;
480         
481         return 1;
482 }
483
484
485 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
486 {
487         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
488
489         // Mark the block for retirement
490         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
491         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
492
493
494         //TODO  
495         // Just do a garbage collection on the affected block then retire the block
496         // NB recursion
497 }
498
499
500 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
501 {
502 }
503
504 #endif
505
506 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *tags)
507 {
508 }
509
510 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_ExtendedTags *tags)
511 {
512 }
513
514 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
515 {
516         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
517
518         // Mark the block for retirement
519         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
520         // Delete the chunk
521         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
522 }
523
524
525
526 #if 0
527 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1,int dataSize)
528 {
529
530
531         if( memcmp(d0,d1,dataSize) != 0 ||
532                 s0->tagByte0 != s1->tagByte0 ||
533                 s0->tagByte1 != s1->tagByte1 ||
534                 s0->tagByte2 != s1->tagByte2 ||
535                 s0->tagByte3 != s1->tagByte3 ||
536                 s0->tagByte4 != s1->tagByte4 ||
537                 s0->tagByte5 != s1->tagByte5 ||
538                 s0->tagByte6 != s1->tagByte6 ||
539                 s0->tagByte7 != s1->tagByte7 ||
540                 s0->ecc1[0]  != s1->ecc1[0]  ||
541                 s0->ecc1[1]  != s1->ecc1[1]  ||
542                 s0->ecc1[2]  != s1->ecc1[2]  ||
543                 s0->ecc2[0]  != s1->ecc2[0]  ||
544                 s0->ecc2[1]  != s1->ecc2[1]  ||
545                 s0->ecc2[2]  != s1->ecc2[2] )
546                 {
547                         return 0;
548                 }
549         
550         return 1;
551 }
552 #endif
553
554
555 ///////////////////////// Object management //////////////////
556 // List of spare objects
557 // The list is hooked together using the first pointer
558 // in the object
559
560 // static yaffs_Object *yaffs_freeObjects = NULL;
561
562 // static int yaffs_nFreeObjects;
563
564 // static yaffs_ObjectList *yaffs_allocatedObjectList = NULL;
565
566 // static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS];
567
568
569 static __u16 yaffs_CalcNameSum(const YCHAR *name)
570 {
571         __u16 sum = 0;
572         __u16 i = 1;
573         
574         YUCHAR *bname = (YUCHAR *)name;
575         if(bname)
576         {
577                 while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH))
578                 {
579
580 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
581                         sum += yaffs_toupper(*bname) * i;
582 #else
583                         sum += (*bname) * i;
584 #endif
585                         i++;
586                         bname++;
587                 }
588         }
589         return sum;
590 }
591
592 void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
593 {
594 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
595                                         if(name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
596                                         {
597                                                 yaffs_strcpy(obj->shortName,name);
598                                         }
599                                         else
600                                         {
601                                                 obj->shortName[0]=_Y('\0');
602                                         }
603 #endif
604                                         obj->sum = yaffs_CalcNameSum(name);
605 }
606
607 #if 0
608 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
609 {
610         yaffs_ECCCalculate(data , spare->ecc1);
611         yaffs_ECCCalculate(&data[256] , spare->ecc2);
612 }
613 #endif
614
615
616 ///////////////////////// TNODES ///////////////////////
617
618 // List of spare tnodes
619 // The list is hooked together using the first pointer
620 // in the tnode.
621
622 //static yaffs_Tnode *yaffs_freeTnodes = NULL;
623
624 // static int yaffs_nFreeTnodes;
625
626 //static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL;
627
628
629
630 // yaffs_CreateTnodes creates a bunch more tnodes and
631 // adds them to the tnode free list.
632 // Don't use this function directly
633
634 static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes)
635 {
636     int i;
637     yaffs_Tnode *newTnodes;
638     yaffs_TnodeList *tnl;
639     
640     if(nTnodes < 1) return YAFFS_OK;
641    
642         // make these things
643         
644     newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode));
645    
646     if (!newTnodes)
647     {
648                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR)));
649                 return YAFFS_FAIL;
650     }
651     
652     // Hook them into the free list
653     for(i = 0; i < nTnodes - 1; i++)
654     {
655         newTnodes[i].internal[0] = &newTnodes[i+1];
656 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
657         newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1;
658 #endif
659     }
660         
661         newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
662 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
663         newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1;
664 #endif
665         dev->freeTnodes = newTnodes;
666         dev->nFreeTnodes+= nTnodes;
667         dev->nTnodesCreated += nTnodes;
668
669         // Now add this bunch of tnodes to a list for freeing up.
670         // NB If we can't add this to the management list it isn't fatal
671         // but it just means we can't free this bunch of tnodes later.
672         tnl = YMALLOC(sizeof(yaffs_TnodeList));
673         if(!tnl)
674         {
675                 T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR)));
676                 
677         }
678         else
679         {
680                 tnl->tnodes = newTnodes;
681                 tnl->next = dev->allocatedTnodeList;
682                 dev->allocatedTnodeList = tnl;
683         }
684
685
686         T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR)));
687
688
689         return YAFFS_OK;
690 }
691
692
693 // GetTnode gets us a clean tnode. Tries to make allocate more if we run out
694 static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
695 {
696         yaffs_Tnode *tn = NULL;
697         
698         // If there are none left make more
699         if(!dev->freeTnodes)
700         {
701                 yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES);
702         }
703         
704         if(dev->freeTnodes)
705         {
706                 tn = dev->freeTnodes;
707 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
708         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1)
709                 {
710                         // Hoosterman, this thing looks like it isn't in the list
711                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR)));
712                 }
713 #endif
714                 dev->freeTnodes = dev->freeTnodes->internal[0];
715                 dev->nFreeTnodes--;
716                 // zero out
717                 memset(tn,0,sizeof(yaffs_Tnode));
718         }
719         
720
721         return tn;
722 }
723
724
725 // FreeTnode frees up a tnode and puts it back on the free list
726 static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn)
727 {
728         if(tn)
729         {
730 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
731         if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0)
732                 {
733                         // Hoosterman, this thing looks like it is already in the list
734                                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR)));
735                 }
736                 tn->internal[YAFFS_NTNODES_INTERNAL] = 1;
737 #endif
738                 tn->internal[0] = dev->freeTnodes;
739                 dev->freeTnodes = tn;
740                 dev->nFreeTnodes++;
741         }
742 }
743
744
745 static void yaffs_DeinitialiseTnodes(yaffs_Device*dev)
746 {
747         // Free the list of allocated tnodes
748         yaffs_TnodeList *tmp;
749                 
750         while(dev->allocatedTnodeList)
751         {
752                 tmp =  dev->allocatedTnodeList->next;
753
754                 YFREE(dev->allocatedTnodeList->tnodes);
755                 YFREE(dev->allocatedTnodeList);
756                 dev->allocatedTnodeList = tmp;
757                 
758         }
759         
760         dev->freeTnodes = NULL;
761         dev->nFreeTnodes = 0;
762 }
763
764 static void yaffs_InitialiseTnodes(yaffs_Device*dev)
765 {
766         dev->allocatedTnodeList = NULL;
767         dev->freeTnodes = NULL;
768         dev->nFreeTnodes = 0;
769         dev->nTnodesCreated = 0;
770
771 }
772
773 #if 0
774 void yaffs_TnodeTest(yaffs_Device *dev)
775 {
776
777         int i;
778         int j;
779         yaffs_Tnode *tn[1000];
780         
781         YINFO("Testing TNodes");
782         
783         for(j = 0; j < 50; j++)
784         {
785                 for(i = 0; i < 1000; i++)
786                 {
787                         tn[i] = yaffs_GetTnode(dev);
788                         if(!tn[i])
789                         {
790                                 YALERT("Getting tnode failed");
791                         }
792                 }
793                 for(i = 0; i < 1000; i+=3)
794                 {
795                         yaffs_FreeTnode(dev,tn[i]);
796                         tn[i] = NULL;
797                 }
798                 
799         }
800 }
801 #endif
802
803
804 ////////////////// END OF TNODE MANIPULATION ///////////////////////////
805
806 /////////////// Functions to manipulate the look-up tree (made up of tnodes)
807 // The look up tree is represented by the top tnode and the number of topLevel
808 // in the tree. 0 means only the level 0 tnode is in the tree.
809
810
811 // FindLevel0Tnode finds the level 0 tnode, if one exists.
812 // Used when reading.....
813 static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId)
814 {
815         
816         yaffs_Tnode *tn = fStruct->top;
817         __u32 i;
818         int requiredTallness;   
819         int level = fStruct->topLevel;
820         
821         // Check sane level and chunk Id
822         if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
823         {
824 //              char str[50];
825 //              sprintf(str,"Bad level %d",level);
826 //              YALERT(str);
827                 return NULL;
828         }
829         
830         if(chunkId > YAFFS_MAX_CHUNK_ID)
831         {
832 //              char str[50];
833 //              sprintf(str,"Bad chunkId %d",chunkId);
834 //              YALERT(str);
835                 return NULL;
836         }
837
838         // First check we're tall enough (ie enough topLevel)
839         
840         i = chunkId >> (/*dev->chunkGroupBits  + */YAFFS_TNODES_LEVEL0_BITS);
841         requiredTallness = 0;
842         while(i)
843         {
844                 i >>= YAFFS_TNODES_INTERNAL_BITS;
845                 requiredTallness++;
846         }
847         
848         
849         if(requiredTallness > fStruct->topLevel)
850         {
851                 // Not tall enough, so we can't find it, return NULL.
852                 return NULL;
853         }
854                 
855         
856         // Traverse down to level 0
857         while (level > 0 && tn)
858         {
859             tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
860                                YAFFS_TNODES_INTERNAL_MASK]; 
861                 level--;
862         
863         }
864         
865         return tn;              
866 }
867
868 // AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
869 // This happens in two steps:
870 //  1. If the tree isn't tall enough, then make it taller.
871 //  2. Scan down the tree towards the level 0 tnode adding tnodes if required.
872 //
873 // Used when modifying the tree.
874 //
875 static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId)
876 {
877         
878         yaffs_Tnode *tn; 
879         
880         int requiredTallness;
881         int i;
882         int l;
883         
884         __u32 x;
885                 
886         
887         //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId));
888         
889         // Check sane level and page Id
890         if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
891         {
892 //              char str[50];
893 //              sprintf(str,"Bad level %d",fStruct->topLevel);
894 //              YALERT(str);
895                 return NULL;
896         }
897         
898         if(chunkId > YAFFS_MAX_CHUNK_ID)
899         {
900 //              char str[50];
901 //              sprintf(str,"Bad chunkId %d",chunkId);
902 //              YALERT(str);
903                 return NULL;
904         }
905         
906         // First check we're tall enough (ie enough topLevel)
907         
908         x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS);
909         requiredTallness = 0;
910         while(x)
911         {
912                 x >>= YAFFS_TNODES_INTERNAL_BITS;
913                 requiredTallness++;
914         }
915         
916         //T((TSTR(" required=%d"),requiredTallness));
917         
918         
919         if(requiredTallness > fStruct->topLevel)
920         {
921                 // Not tall enough,gotta make the tree taller
922                 for(i = fStruct->topLevel; i < requiredTallness; i++)
923                 {
924                         //T((TSTR(" add new top")));
925                         
926                         tn = yaffs_GetTnode(dev);
927                         
928                         if(tn)
929                         {
930                                 tn->internal[0] = fStruct->top;
931                                 fStruct->top = tn;
932                         }
933                         else
934                         {
935                                         T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR)));
936                         }
937                 }
938                 
939                 fStruct->topLevel = requiredTallness;
940         }
941         
942         
943         // Traverse down to level 0, adding anything we need
944         
945         l = fStruct->topLevel;
946         tn = fStruct->top;
947         while (l > 0 && tn)
948         {
949                 x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & 
950                                YAFFS_TNODES_INTERNAL_MASK;
951                                
952                 //T((TSTR(" [%d:%d]"),l,i));
953                 
954             if(!tn->internal[x])
955             {
956                 //T((TSTR(" added")));
957                 
958                 tn->internal[x] = yaffs_GetTnode(dev);
959             }
960             
961             tn =        tn->internal[x];
962                 l--;
963         
964         }
965         
966         //TSTR(TENDSTR)));
967         
968         return tn;              
969 }
970
971
972 int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk, yaffs_ExtendedTags *tags, int objectId, int chunkInInode)
973 {
974         int j;
975         
976                                         
977         for(j = 0; theChunk && j < dev->chunkGroupSize; j++)
978         {
979                 if(yaffs_CheckChunkBit(dev,theChunk / dev->nChunksPerBlock,theChunk % dev->nChunksPerBlock))
980                 {
981                         yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL,tags);
982                         if(yaffs_TagsMatch(tags,objectId,chunkInInode))
983                         {
984                                 // found it;
985                                 return theChunk;
986                                         
987                         }
988                 }
989                 theChunk++;
990         }
991         return -1;
992 }
993
994 // DeleteWorker scans backwards through the tnode tree and deletes all the
995 // chunks and tnodes in the file
996 // Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
997
998 static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit)
999 {
1000         int i;
1001         int chunkInInode;
1002         int theChunk;
1003         yaffs_ExtendedTags tags;
1004         int foundChunk;
1005         yaffs_Device *dev = in->myDev;
1006
1007         int allDone = 1;
1008         
1009         
1010         if(tn)
1011         {
1012                 if(level > 0)
1013                 {
1014                 
1015                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1016                         {
1017                             if(tn->internal[i])
1018                         {
1019                                         if(limit && (*limit) < 0)
1020                                         {
1021                                                 allDone = 0;
1022                                         }
1023                                         else
1024                                         {
1025                                                 allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1,
1026                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit);
1027                                         }
1028                                         if(allDone)
1029                                         {
1030                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1031                                         tn->internal[i] = NULL;
1032                                         }
1033                             }
1034                     
1035                         }
1036                         return (allDone) ? 1 : 0;
1037                 }
1038                 else if(level == 0)
1039                 {
1040                         int hitLimit = 0;
1041                         
1042                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--)
1043                         {
1044                             if(tn->level0[i])
1045                         {
1046                                         
1047                                         chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i;
1048                                         
1049                                         theChunk =  tn->level0[i] << dev->chunkGroupBits;
1050                                         
1051                                         foundChunk = yaffs_FindChunkInGroup(dev,theChunk,&tags,in->objectId,chunkInInode);
1052                                         
1053                                         if(foundChunk > 0)
1054                                         {
1055                                                 yaffs_DeleteChunk(dev,foundChunk,1,__LINE__);
1056                                                 in->nDataChunks--;
1057                                                 if(limit)
1058                                                 { 
1059                                                         *limit = *limit-1;
1060                                                         if(*limit <= 0) 
1061                                                         { 
1062                                                                 hitLimit = 1;
1063                                                         }
1064                                                 }
1065                                         
1066                                         }
1067                                         
1068                                 tn->level0[i] = 0;
1069                             }
1070                     
1071                         }
1072                         return (i < 0) ? 1 : 0;
1073
1074                         
1075                 }
1076                 
1077         }
1078         
1079         return 1;
1080         
1081 }
1082
1083
1084 static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
1085 {
1086
1087         yaffs_BlockInfo *theBlock;                                      
1088         
1089         T(YAFFS_TRACE_DELETION,(TSTR("soft delete chunk %d" TENDSTR),chunk));
1090                                                 
1091         theBlock =      yaffs_GetBlockInfo(dev,  chunk/dev->nChunksPerBlock);
1092         if(theBlock)
1093         {
1094                 theBlock->softDeletions++;
1095                 dev->nFreeChunks++;
1096         }
1097 }
1098
1099 // SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
1100 // All soft deleting does is increment the block's softdelete count and pulls the chunk out
1101 // of the tnode.
1102 // THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
1103 //
1104 static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset)
1105 {
1106         int i;
1107         int theChunk;
1108         int allDone = 1;
1109         yaffs_Device *dev = in->myDev;
1110         
1111         
1112         if(tn)
1113         {
1114                 if(level > 0)
1115                 {
1116                 
1117                         for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--)
1118                         {
1119                             if(tn->internal[i])
1120                         {
1121                                                 allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1,
1122                                                                                 (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i);
1123                                         if(allDone)
1124                                         {
1125                                                 yaffs_FreeTnode(dev,tn->internal[i]);
1126                                         tn->internal[i] = NULL;
1127                                         }
1128                                         else
1129                                         {
1130                                                 //Hoosterman... how could this happen.
1131                                         }                           
1132                                 }                   
1133                         }
1134                         return (allDone) ? 1 : 0;
1135                 }
1136                 else if(level == 0)
1137                 {
1138                         
1139                         for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--)
1140                         {
1141                             if(tn->level0[i])
1142                         {
1143                                         // Note this does not find the real chunk, only the chunk group.
1144                                         // We make an assumption that a chunk group is niot larger than a block.
1145                                         theChunk =  (tn->level0[i] << dev->chunkGroupBits);
1146                                         
1147                                         yaffs_SoftDeleteChunk(dev,theChunk);
1148                                 tn->level0[i] = 0;
1149                             }
1150                     
1151                         }
1152                         return 1;
1153                         
1154                 }
1155                 
1156         }
1157         
1158         return 1;
1159                 
1160 }
1161
1162
1163
1164 static void yaffs_SoftDeleteFile(yaffs_Object *obj)
1165 {
1166         if(obj->deleted &&
1167            obj->variantType == YAFFS_OBJECT_TYPE_FILE &&
1168            !obj->softDeleted)
1169         {
1170                 if(obj->nDataChunks <= 0)
1171                 {
1172                                 // Empty file with no duplicate object headers, just delete it immediately
1173                                 yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top);
1174                                 obj->variant.fileVariant.top = NULL;
1175                                 T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId));
1176                                 yaffs_DoGenericObjectDeletion(obj);     
1177                 }
1178                 else
1179                 {
1180                         yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0);
1181                         obj->softDeleted = 1;
1182                 }
1183         }
1184 }
1185
1186
1187
1188
1189
1190 // Pruning removes any part of the file structure tree that is beyond the
1191 // bounds of the file (ie that does not point to chunks).
1192 //
1193 // A file should only get pruned when its size is reduced.
1194 //
1195 // Before pruning, the chunks must be pulled from the tree and the
1196 // level 0 tnode entries must be zeroed out.
1197 // Could also use this for file deletion, but that's probably better handled
1198 // by a special case.
1199
1200 // yaffs_PruneWorker should only be called by yaffs_PruneFileStructure()
1201
1202 static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0)
1203 {
1204         int i;
1205         int hasData;
1206         
1207         if(tn)
1208         {
1209                 hasData = 0;
1210                 
1211                 for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++)
1212                 {
1213                     if(tn->internal[i] && level > 0)
1214                     {
1215                         tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1);
1216                     }
1217                     
1218                     if(tn->internal[i])
1219                     {
1220                         hasData++;
1221                         }
1222                 }
1223                 
1224                 if(hasData == 0 && del0)
1225                 {
1226                         // Free and return NULL
1227                         
1228                         yaffs_FreeTnode(dev,tn);
1229                         tn = NULL;
1230                 }
1231                 
1232         }
1233
1234         return tn;
1235         
1236 }
1237
1238 static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct)
1239 {
1240         int i;
1241         int hasData;
1242         int done = 0;
1243         yaffs_Tnode *tn;
1244         
1245         if(fStruct->topLevel > 0)
1246         {
1247                 fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0);
1248                 
1249                 // Now we have a tree with all the non-zero branches NULL but the height
1250                 // is the same as it was.
1251                 // Let's see if we can trim internal tnodes to shorten the tree.
1252                 // We can do this if only the 0th element in the tnode is in use 
1253                 // (ie all the non-zero are NULL)
1254                 
1255                 while(fStruct->topLevel && !done)
1256                 {
1257                         tn = fStruct->top;
1258                         
1259                         hasData = 0;
1260                         for(i = 1; i <YAFFS_NTNODES_INTERNAL; i++)
1261                         {
1262                                 if(tn->internal[i])
1263                         {
1264                                 hasData++;
1265                                 }
1266                         }
1267                         
1268                         if(!hasData)
1269                         {
1270                                 fStruct->top = tn->internal[0];
1271                                 fStruct->topLevel--;
1272                                 yaffs_FreeTnode(dev,tn);
1273                         }
1274                         else
1275                         {
1276                                 done = 1;
1277                         }
1278                 }
1279         }
1280         
1281         return YAFFS_OK;
1282 }
1283
1284
1285
1286
1287
1288 /////////////////////// End of File Structure functions. /////////////////
1289
1290 // yaffs_CreateFreeObjects creates a bunch more objects and
1291 // adds them to the object free list.
1292 static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
1293 {
1294     int i;
1295     yaffs_Object *newObjects;
1296     yaffs_ObjectList *list;
1297     
1298     if(nObjects < 1) return YAFFS_OK;
1299    
1300         // make these things
1301         
1302     newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
1303    
1304     if (!newObjects)
1305     {
1306                 T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR)));
1307                 return YAFFS_FAIL;
1308     }
1309     
1310     // Hook them into the free list
1311     for(i = 0; i < nObjects - 1; i++)
1312     {
1313         newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]);
1314     }
1315         
1316         newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
1317         dev->freeObjects = newObjects;
1318         dev->nFreeObjects+= nObjects;
1319         dev->nObjectsCreated+= nObjects;
1320         
1321         // Now add this bunch of Objects to a list for freeing up.
1322         
1323         list = YMALLOC(sizeof(yaffs_ObjectList));
1324         if(!list)
1325         {
1326                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR)));
1327         }
1328         else
1329         {
1330                 list->objects = newObjects;
1331                 list->next = dev->allocatedObjectList;
1332                 dev->allocatedObjectList = list;
1333         }
1334         
1335         
1336         
1337         return YAFFS_OK;
1338 }
1339
1340
1341 // AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out
1342 static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
1343 {
1344         yaffs_Object *tn = NULL;
1345         
1346         // If there are none left make more
1347         if(!dev->freeObjects)
1348         {
1349                 yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS);
1350         }
1351         
1352         if(dev->freeObjects)
1353         {
1354                 tn = dev->freeObjects;
1355                 dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next);
1356                 dev->nFreeObjects--;
1357                 
1358                 // Now sweeten it up...
1359         
1360                 memset(tn,0,sizeof(yaffs_Object));
1361                 tn->myDev = dev;
1362                 tn->chunkId = -1;
1363                 tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
1364                 INIT_LIST_HEAD(&(tn->hardLinks));
1365                 INIT_LIST_HEAD(&(tn->hashLink));
1366                 INIT_LIST_HEAD(&tn->siblings);
1367                 
1368                 // Add it to the lost and found directory.
1369                 // NB Can't put root or lostNFound in lostNFound so
1370                 // check if lostNFound exists first
1371                 if(dev->lostNFoundDir)
1372                 {
1373                         yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn);      
1374                 }
1375         }
1376         
1377
1378         return tn;
1379 }
1380
1381 static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode)
1382 {
1383
1384         yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY);              
1385         if(obj)
1386         {
1387                 obj->fake = 1;                  // it is fake so it has no NAND presence...
1388                 obj->renameAllowed= 0;  // ... and we're not allowed to rename it...
1389                 obj->unlinkAllowed= 0;  // ... or unlink it
1390                 obj->deleted = 0;
1391                 obj->unlinked = 0;
1392                 obj->yst_mode = mode;
1393                 obj->myDev = dev;
1394                 obj->chunkId = 0; // Not a valid chunk.
1395         }
1396         
1397         return obj;
1398         
1399 }
1400
1401
1402 static void yaffs_UnhashObject(yaffs_Object *tn)
1403 {
1404         int bucket;
1405         yaffs_Device *dev = tn->myDev;
1406         
1407         
1408         // If it is still linked into the bucket list, free from the list
1409         if(!list_empty(&tn->hashLink))
1410         {
1411                 list_del_init(&tn->hashLink);
1412                 bucket =  yaffs_HashFunction(tn->objectId);
1413                 dev->objectBucket[bucket].count--;
1414         }
1415         
1416 }
1417
1418
1419 // FreeObject frees up a Object and puts it back on the free list
1420 static void yaffs_FreeObject(yaffs_Object *tn)
1421 {
1422
1423         yaffs_Device *dev = tn->myDev;
1424         
1425 #ifdef  __KERNEL__
1426         if(tn->myInode)
1427         {
1428                 // We're still hooked up to a cached inode.
1429                 // Don't delete now, but mark for later deletion
1430                 tn->deferedFree = 1;
1431                 return;
1432         }
1433 #endif
1434         
1435         yaffs_UnhashObject(tn);
1436         
1437         // Link into the free list.
1438         tn->siblings.next = (struct list_head *)(dev->freeObjects);
1439         dev->freeObjects = tn;
1440         dev->nFreeObjects++;
1441 }
1442
1443
1444
1445 #ifdef __KERNEL__
1446
1447 void yaffs_HandleDeferedFree(yaffs_Object *obj)
1448 {
1449         if(obj->deferedFree)
1450         {
1451            yaffs_FreeObject(obj);
1452         }
1453 }
1454
1455 #endif
1456
1457
1458
1459 static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
1460 {
1461         // Free the list of allocated Objects
1462         
1463         yaffs_ObjectList *tmp;
1464         
1465         while( dev->allocatedObjectList)
1466         {
1467                 tmp =  dev->allocatedObjectList->next;
1468                 YFREE(dev->allocatedObjectList->objects);
1469                 YFREE(dev->allocatedObjectList);
1470                 
1471                 dev->allocatedObjectList =  tmp;
1472         }
1473         
1474         dev->freeObjects = NULL;
1475         dev->nFreeObjects = 0;
1476 }
1477
1478 static void yaffs_InitialiseObjects(yaffs_Device *dev)
1479 {
1480         int i;
1481         
1482         dev->allocatedObjectList = NULL;
1483         dev->freeObjects = NULL;
1484         dev->nFreeObjects = 0;
1485         
1486         for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++)
1487         {
1488                 INIT_LIST_HEAD(&dev->objectBucket[i].list);
1489                 dev->objectBucket[i].count = 0; 
1490         }
1491
1492 }
1493
1494
1495
1496
1497
1498
1499 int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
1500 {
1501         static int x = 0;
1502         int i;
1503         int l = 999;
1504         int lowest = 999999;
1505
1506                 
1507         // First let's see if we can find one that's empty.
1508         
1509         for(i = 0; i < 10 && lowest > 0; i++)
1510          {
1511                 x++;
1512                 x %=  YAFFS_NOBJECT_BUCKETS;
1513                 if(dev->objectBucket[x].count < lowest)
1514                 {
1515                         lowest = dev->objectBucket[x].count;
1516                         l = x;
1517                 }
1518                 
1519         }
1520         
1521         // If we didn't find an empty list, then try
1522         // looking a bit further for a short one
1523         
1524         for(i = 0; i < 10 && lowest > 3; i++)
1525          {
1526                 x++;
1527                 x %=  YAFFS_NOBJECT_BUCKETS;
1528                 if(dev->objectBucket[x].count < lowest)
1529                 {
1530                         lowest = dev->objectBucket[x].count;
1531                         l = x;
1532                 }
1533                 
1534         }
1535         
1536         return l;
1537 }
1538
1539 static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
1540 {
1541         int bucket = yaffs_FindNiceObjectBucket(dev);
1542         
1543         // Now find an object value that has not already been taken
1544         // by scanning the list.
1545         
1546         int found = 0;
1547         struct list_head *i;
1548         
1549         __u32 n = (__u32)bucket;
1550
1551         //yaffs_CheckObjectHashSanity();        
1552         
1553         while(!found)
1554         {
1555                 found = 1;
1556                 n +=  YAFFS_NOBJECT_BUCKETS;
1557                 if(1 ||dev->objectBucket[bucket].count > 0)
1558                 {
1559                         list_for_each(i,&dev->objectBucket[bucket].list)
1560                         {
1561                                 // If there is already one in the list
1562                                 if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n)
1563                                 {
1564                                         found = 0;
1565                                 }
1566                         }
1567                 }
1568         }
1569         
1570         //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n);
1571         
1572         return n;       
1573 }
1574
1575 void yaffs_HashObject(yaffs_Object *in)
1576 {
1577         int bucket = yaffs_HashFunction(in->objectId);
1578         yaffs_Device *dev = in->myDev;
1579         
1580         if(!list_empty(&in->hashLink))
1581         {
1582                 //YINFO("!!!");
1583         }
1584
1585         
1586         list_add(&in->hashLink,&dev->objectBucket[bucket].list);
1587         dev->objectBucket[bucket].count++;
1588
1589 }
1590
1591 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number)
1592 {
1593         int bucket = yaffs_HashFunction(number);
1594         struct list_head *i;
1595         yaffs_Object *in;
1596         
1597         list_for_each(i,&dev->objectBucket[bucket].list)
1598         {
1599                 // Look if it is in the list
1600                 if(i)
1601                 {
1602                         in = list_entry(i, yaffs_Object,hashLink);
1603                         if(in->objectId == number)
1604                         {
1605 #ifdef __KERNEL__
1606                                 // Don't tell the VFS about this one if it is defered free
1607                                 if(in->deferedFree)
1608                                   return NULL;
1609 #endif
1610                                   
1611                                 return in;
1612                         }
1613                 }
1614         }
1615         
1616         return NULL;
1617 }
1618
1619
1620
1621 yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type)
1622 {
1623                 
1624         yaffs_Object *theObject;
1625
1626         if(number < 0)
1627         {
1628                 number = yaffs_CreateNewObjectNumber(dev);
1629         }
1630         
1631         theObject = yaffs_AllocateEmptyObject(dev);
1632         
1633         if(theObject)
1634         {
1635                 theObject->fake = 0;
1636                 theObject->renameAllowed = 1;
1637                 theObject->unlinkAllowed = 1;
1638                 theObject->objectId = number;
1639                 yaffs_HashObject(theObject);
1640                 theObject->variantType = type;
1641 #ifdef CONFIG_YAFFS_WINCE
1642                 yfsd_WinFileTimeNow(theObject->win_atime);
1643                 theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0];
1644                 theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1];
1645
1646 #else
1647
1648                 theObject->yst_atime = theObject->yst_mtime = theObject->yst_ctime = Y_CURRENT_TIME;            
1649 #endif
1650                 switch(type)
1651                 {
1652                         case YAFFS_OBJECT_TYPE_FILE: 
1653                                 theObject->variant.fileVariant.fileSize = 0;
1654                                 theObject->variant.fileVariant.scannedFileSize = 0;
1655                                 theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; // max __u32
1656                                 theObject->variant.fileVariant.topLevel = 0;
1657                                 theObject->variant.fileVariant.top  = yaffs_GetTnode(dev);
1658                                 break;
1659                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1660                                 INIT_LIST_HEAD(&theObject->variant.directoryVariant.children);
1661                                 break;
1662                         case YAFFS_OBJECT_TYPE_SYMLINK:
1663                                 // No action required
1664                                 break;
1665                         case YAFFS_OBJECT_TYPE_HARDLINK:
1666                                 // No action required
1667                                 break;
1668                         case YAFFS_OBJECT_TYPE_SPECIAL:
1669                                 // No action required
1670                                 break;
1671                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1672                                 // todo this should not happen
1673                                 break;
1674                 }
1675         }
1676         
1677         return theObject;
1678 }
1679
1680 yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type)
1681 {
1682         yaffs_Object *theObject = NULL;
1683         
1684         if(number > 0)
1685         {
1686                 theObject = yaffs_FindObjectByNumber(dev,number);
1687         }
1688         
1689         if(!theObject)
1690         {
1691                 theObject = yaffs_CreateNewObject(dev,number,type);
1692         }
1693         
1694         return theObject;
1695
1696 }
1697
1698 YCHAR *yaffs_CloneString(const YCHAR *str)
1699 {
1700         YCHAR *newStr = NULL;
1701         
1702         if(str && *str)
1703         {
1704                 newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
1705                 yaffs_strcpy(newStr,str);
1706         }
1707
1708         return newStr;
1709         
1710 }
1711
1712 //
1713 // Mknod (create) a new object.
1714 // equivalentObject only has meaning for a hard link;
1715 // aliasString only has meaning for a sumlink.
1716 // rdev only has meaning for devices (a subset of special objects)
1717 yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type,
1718                                                                  yaffs_Object *parent,
1719                                                                  const YCHAR *name, 
1720                                                                  __u32 mode,
1721                                                                  __u32 uid,
1722                                                                  __u32 gid,
1723                                                                  yaffs_Object *equivalentObject,
1724                                                                  const YCHAR *aliasString,
1725                                                                  __u32 rdev)
1726 {
1727         yaffs_Object *in;
1728
1729         yaffs_Device *dev = parent->myDev;
1730         
1731         // Check if the entry exists. If it does then fail the call since we don't want a dup.
1732         if(yaffs_FindObjectByName(parent,name))
1733         {
1734                 return NULL;
1735         }
1736         
1737         in = yaffs_CreateNewObject(dev,-1,type);
1738         
1739         if(in)
1740         {
1741                 in->chunkId = -1;
1742                 in->valid = 1;
1743                 in->variantType = type;
1744
1745                 in->yst_mode  = mode;
1746                 
1747 #ifdef CONFIG_YAFFS_WINCE
1748                 yfsd_WinFileTimeNow(in->win_atime);
1749                 in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
1750                 in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
1751                 
1752 #else
1753                 in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
1754
1755                 in->yst_rdev  = rdev;
1756                 in->yst_uid   = uid;
1757                 in->yst_gid   = gid;
1758 #endif          
1759                 in->nDataChunks = 0;
1760
1761                 yaffs_SetObjectName(in,name);
1762                 in->dirty = 1;
1763                 
1764                 yaffs_AddObjectToDirectory(parent,in);
1765                 
1766                 in->myDev = parent->myDev;
1767                 
1768                                 
1769                 switch(type)
1770                 {
1771                         case YAFFS_OBJECT_TYPE_SYMLINK:
1772                                 in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString);
1773                                 break;
1774                         case YAFFS_OBJECT_TYPE_HARDLINK:
1775                                 in->variant.hardLinkVariant.equivalentObject = equivalentObject;
1776                                 in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId;
1777                                 list_add(&in->hardLinks,&equivalentObject->hardLinks);
1778                                 break;
1779                         case YAFFS_OBJECT_TYPE_FILE: // do nothing
1780                         case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing
1781                         case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing
1782                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1783                                 break;
1784                 }
1785
1786                 if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */
1787                    yaffs_UpdateObjectHeader(in,name,0,0) < 0)
1788                 {
1789                         // Could not create the object header, fail the creation
1790                         yaffs_DestroyObject(in);
1791                         in = NULL;
1792                 }
1793
1794         }
1795         
1796         return in;
1797 }
1798
1799 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1800 {
1801         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0);
1802 }
1803
1804 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid)
1805 {
1806         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0);
1807 }
1808
1809 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
1810 {
1811         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL,parent,name,mode,uid,gid,NULL,NULL,rdev);
1812 }
1813
1814 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const YCHAR *name, __u32 mode, __u32 uid, __u32 gid,const YCHAR *alias)
1815 {
1816         return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0);
1817 }
1818
1819 // NB yaffs_Link returns the object id of the equivalent object.
1820 yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name, yaffs_Object *equivalentObject)
1821 {
1822         // Get the real object in case we were fed a hard link as an equivalent object
1823         equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
1824         
1825         if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0))
1826         {
1827                 return equivalentObject;
1828         }
1829         else
1830         {
1831                 return NULL;
1832         }
1833         
1834 }
1835
1836
1837 static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const YCHAR *newName,int force)
1838 {
1839         int unlinkOp;
1840         int deleteOp;
1841
1842         if(newDir == NULL)
1843         {
1844                 newDir = obj->parent; // use the old directory
1845         }
1846         
1847         if(newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1848         {
1849                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragendy: yaffs_ChangeObjectName: newDir is not a directory"TENDSTR)));
1850                 YBUG();
1851         }
1852
1853         // TODO: Do we need this different handling for YAFFS2 and YAFFS1??
1854         if(obj->myDev->isYaffs2)
1855         {
1856                 unlinkOp = (newDir == obj->myDev->unlinkedDir);
1857         }
1858         else
1859         {
1860                 unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
1861         }
1862
1863         deleteOp = (newDir == obj->myDev->deletedDir);
1864         
1865         // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since
1866         // duplicate names are allowed.
1867         // Otherwise only proceed if the new name does not exist and if we're putting it into a directory.
1868         if( (unlinkOp|| 
1869                  deleteOp ||
1870                  force || 
1871                  !yaffs_FindObjectByName(newDir,newName))  &&
1872              newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1873         {
1874                 yaffs_SetObjectName(obj,newName);
1875                 obj->dirty = 1;
1876                 
1877                 yaffs_AddObjectToDirectory(newDir,obj);
1878                 
1879                 if(unlinkOp) obj->unlinked = 1;
1880                 
1881                 // If it is a deletion then we mark it as a shrink for gc purposes.
1882                 if(yaffs_UpdateObjectHeader(obj,newName,0,deleteOp) >= 0)
1883                 {
1884                         return YAFFS_OK;
1885                 }
1886         }
1887         
1888         return YAFFS_FAIL;
1889 }
1890
1891
1892
1893 int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName, yaffs_Object *newDir, const YCHAR *newName)
1894 {
1895         yaffs_Object *obj;
1896         int force = 0;
1897         
1898 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
1899         // Special case for case insemsitive systems (eg. WinCE).
1900         // While look-up is case insensitive, the name isn't.
1901         // THerefore we might want to change x.txt to X.txt
1902         if(oldDir == newDir && yaffs_strcmp(oldName,newName) == 0)
1903         {
1904                 force = 1;
1905         }       
1906 #endif
1907         
1908         obj = yaffs_FindObjectByName(oldDir,oldName);
1909         if(obj && obj->renameAllowed)
1910         {
1911                 return yaffs_ChangeObjectName(obj,newDir,newName,force);
1912         }
1913         return YAFFS_FAIL;
1914 }
1915
1916
1917 #if 0
1918
1919 static int yaffs_CheckObjectHashSanity(yaffs_Device *dev)
1920 {
1921         // Scan the buckets and check that the lists 
1922         // have as many members as the count says there are
1923         int bucket;
1924         int countEm;
1925         struct list_head *j;
1926         int ok = YAFFS_OK;
1927         
1928         for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++)
1929         {
1930                 countEm = 0;
1931                 
1932                 list_for_each(j,&dev->objectBucket[bucket].list)
1933                 {
1934                         countEm++;
1935                 }
1936                 
1937                 if(countEm != dev->objectBucket[bucket].count)
1938                 {
1939                         T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR)));
1940                         ok = YAFFS_FAIL;
1941                 }
1942         }
1943
1944         return ok;
1945 }
1946
1947
1948 void yaffs_ObjectTest(yaffs_Device *dev)
1949 {
1950         yaffs_Object *in[1000];
1951         int inNo[1000];
1952         yaffs_Object *inold[1000];
1953         int i;
1954         int j;
1955         
1956         memset(in,0,1000*sizeof(yaffs_Object *));
1957         memset(inold,0,1000*sizeof(yaffs_Object *));
1958         
1959         yaffs_CheckObjectHashSanity(dev);
1960         
1961         for(j = 0; j < 10; j++)
1962         {
1963                 //T(("%d\n",j));
1964                 
1965                 for(i = 0; i < 1000; i++)
1966                 {
1967                         in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE);
1968                         if(!in[i])
1969                         {
1970                                 YINFO("No more inodes");
1971                         }
1972                         else
1973                         {
1974                                 inNo[i] = in[i]->objectId;
1975                         }
1976                 }
1977                 
1978                 for(i = 0; i < 1000; i++)
1979                 {
1980                         if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i])
1981                         {
1982                                 //T(("Differnce in look up test\n"));
1983                         }
1984                         else
1985                         {
1986                                 // T(("Look up ok\n"));
1987                         }
1988                 }
1989                 
1990                 yaffs_CheckObjectHashSanity(dev);
1991         
1992                 for(i = 0; i < 1000; i+=3)
1993                 {
1994                         yaffs_FreeObject(in[i]);        
1995                         in[i] = NULL;
1996                 }
1997                 
1998         
1999                 yaffs_CheckObjectHashSanity(dev);
2000         }
2001                 
2002 }
2003
2004 #endif
2005
2006 /////////////////////////// Block Management and Page Allocation ///////////////////
2007
2008
2009 static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks)
2010 {
2011         dev->allocationBlock = -1; // force it to get a new one
2012         //Todo we're assuming the malloc will pass.
2013         dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
2014         // Set up dynamic blockinfo stuff.
2015         dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8;
2016         dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
2017         if(dev->blockInfo && dev->chunkBits)
2018         {
2019                 memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo));
2020                 memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks);
2021                 return YAFFS_OK;
2022         }
2023         
2024         return YAFFS_FAIL;
2025         
2026 }
2027
2028 static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
2029 {
2030         YFREE(dev->blockInfo);
2031         dev->blockInfo = NULL;
2032         YFREE(dev->chunkBits);
2033         dev->chunkBits = NULL;
2034 }
2035
2036
2037 static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev, yaffs_BlockInfo *bi)
2038 {
2039         int i;
2040         __u32 seq;
2041         yaffs_BlockInfo *b;
2042         
2043         if(!dev->isYaffs2) return 1; // disqualification only applies to yaffs2.
2044         
2045         if(!bi->hasShrinkHeader) return 1; // can gc
2046
2047
2048         // Find the oldest dirty sequence number if we don't know it and save it
2049         // so we don't have to keep recomputing it.
2050         if(!dev->oldestDirtySequence)
2051         {
2052                 seq = dev->sequenceNumber;
2053
2054                 for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2055                 {
2056                         b = yaffs_GetBlockInfo(dev,i);
2057                         if(b->blockState == YAFFS_BLOCK_STATE_FULL &&
2058                         (b->pagesInUse - b->softDeletions )< dev->nChunksPerBlock &&
2059                         b->sequenceNumber < seq)
2060                         {
2061                                 seq = b->sequenceNumber;
2062                         }
2063                 }
2064                 dev->oldestDirtySequence = seq;
2065         }
2066
2067
2068         // Can't do gc of this block if there are any blocks older than this one that have
2069         // discarded pages.
2070         return (bi->sequenceNumber <= dev->oldestDirtySequence);
2071         
2072         
2073         return 1;
2074
2075 }
2076
2077 // FindDiretiestBlock is used to select the dirtiest block (or close enough)
2078 // for garbage collection.
2079 //
2080
2081
2082
2083 static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,int aggressive)
2084 {
2085
2086         int b = dev->currentDirtyChecker;
2087         
2088         int i;
2089         int iterations;
2090         int dirtiest = -1;
2091         int pagesInUse; 
2092         yaffs_BlockInfo *bi;
2093         static int  nonAggressiveSkip = 0;
2094
2095         // If we're doing aggressive GC then we are happy to take a less-dirty block, and
2096         // search harder.
2097         // else (we're doing a leasurely gc), then we only bother to do this if the
2098         // block has only a few pages in use.
2099         
2100
2101         nonAggressiveSkip--;
2102
2103         if(!aggressive &&(nonAggressiveSkip > 0))
2104         {
2105                 return -1;
2106         }
2107
2108         pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
2109
2110         if(aggressive)
2111         {
2112                 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2113         }
2114         else
2115         {
2116                 iterations = dev->internalEndBlock - dev->internalStartBlock + 1;
2117                 iterations = iterations / 16; 
2118                 if(iterations > 200)
2119                 {
2120                         iterations = 200;
2121                 }
2122         }
2123         
2124         for(i = 0; i <= iterations && pagesInUse > 0 ; i++)
2125         {
2126                 b++;
2127                 if ( b < dev->internalStartBlock || b > dev->internalEndBlock)
2128                 {
2129                         b =  dev->internalStartBlock;
2130                 }
2131
2132                 if(b < dev->internalStartBlock || b > dev->internalEndBlock)
2133                 {
2134                         T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b));
2135                         YBUG();
2136                 }
2137                 
2138                 bi = yaffs_GetBlockInfo(dev,b);
2139                 
2140                 if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
2141                    (bi->pagesInUse - bi->softDeletions )< pagesInUse &&
2142                    yaffs_BlockNotDisqualifiedFromGC(dev,bi))
2143                 {
2144                         dirtiest = b;
2145                         pagesInUse = (bi->pagesInUse - bi->softDeletions);
2146                 }
2147         }
2148         
2149         dev->currentDirtyChecker = b;
2150         
2151         if(dirtiest > 0)
2152         {
2153                 T(YAFFS_TRACE_GC,(TSTR("GC Selected block %d with %d free" TENDSTR),dirtiest,dev->nChunksPerBlock - pagesInUse));
2154         }
2155         
2156         dev->oldestDirtySequence = 0; // clear this
2157         
2158         if(dirtiest > 0)
2159         {
2160                 nonAggressiveSkip = 4;
2161         }
2162
2163         return dirtiest;
2164 }
2165
2166
2167 static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo)
2168 {
2169         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo);
2170         
2171         int erasedOk = 0;
2172         
2173         // If the block is still healthy erase it and mark as clean.
2174         // If the block has had a data failure, then retire it.
2175         bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
2176
2177         if(!bi->needsRetiring)
2178         {
2179                 erasedOk = yaffs_EraseBlockInNAND(dev,blockNo);
2180                 if(!erasedOk)
2181                 {
2182                         dev->nErasureFailures++;
2183                         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo));
2184                 }
2185         }
2186
2187         if(erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE))
2188         {
2189                         int i;
2190                         for(i = 0; i < dev->nChunksPerBlock; i++)
2191                         {
2192                                         if(!yaffs_CheckChunkErased(dev,blockNo * dev->nChunksPerBlock + i))
2193                                         {
2194                                                 T(YAFFS_TRACE_ERROR,(TSTR(">>Block %d erasure supposedly OK, but chunk %d not erased" TENDSTR),blockNo,i));
2195                                         }
2196                         }
2197         }
2198         
2199         if( erasedOk )
2200         {
2201                 // Clean it up...
2202                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
2203                 dev->nErasedBlocks++;
2204                 bi->pagesInUse = 0;
2205                 bi->softDeletions = 0;
2206                 bi->hasShrinkHeader=0;
2207                 yaffs_ClearChunkBits(dev,blockNo);
2208         
2209                 T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo));
2210         }
2211         else
2212         {
2213                 dev->nFreeChunks -= dev->nChunksPerBlock; // We lost a block of free space
2214                 
2215                 yaffs_RetireBlock(dev,blockNo);
2216                 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo));
2217         }
2218 }
2219
2220 #if 0
2221 static void yaffs_DumpBlockStats(yaffs_Device *dev)
2222 {
2223         int i,j;
2224         yaffs_BlockInfo *bi;
2225         
2226         for(i= dev->internalStartBlock; i <=dev->internalEndBlock; i++)
2227         {
2228                 bi = yaffs_GetBlockInfo(dev,i);
2229                 T(YAFFS_TRACE_ALLOCATE,(TSTR("%3d state %d shrink %d inuse %d/%d seq %d pages"),i,
2230                 bi->blockState,bi->hasShrinkHeader,bi->pagesInUse,bi->softDeletions,bi->sequenceNumber));       
2231                 
2232                 for(j = 0; j < dev->nChunksPerBlock; j++)
2233                 {
2234                         if(yaffs_CheckChunkBit(dev,i,j))
2235                         {
2236                                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" %d"),j));
2237
2238                         }
2239                 }
2240                 T(YAFFS_TRACE_ALLOCATE,(TSTR(" " TENDSTR)));
2241
2242         }
2243 }
2244 #endif
2245
2246
2247 static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
2248 {
2249         int i;
2250         
2251         yaffs_BlockInfo *bi;
2252         
2253 #if 0
2254         static int j = 0;
2255         j++;
2256         if(j < 0 || j > 100)
2257         {
2258                 j = 0;
2259                 yaffs_DumpBlockStats(dev);
2260         }
2261         
2262 #endif
2263         
2264         if(dev->nErasedBlocks < 1)
2265         {
2266                 // Hoosterman we've got a problem.
2267                 // Can't get space to gc
2268                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
2269
2270                 return -1;
2271         }
2272         
2273         // Find an empty block.
2274         
2275         for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++)
2276         {
2277                 dev->allocationBlockFinder++;
2278                 if(dev->allocationBlockFinder < dev->internalStartBlock || dev->allocationBlockFinder> dev->internalEndBlock) 
2279                 {
2280                         dev->allocationBlockFinder = dev->internalStartBlock;
2281                 }
2282                 
2283                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder);
2284
2285                 if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
2286                 {
2287                         bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
2288                         dev->sequenceNumber++;
2289                         bi->sequenceNumber = dev->sequenceNumber;
2290                         dev->nErasedBlocks--;           
2291                         T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocated block %d, seq  %d, %d left" TENDSTR),dev->allocationBlockFinder,dev->sequenceNumber, dev->nErasedBlocks));      
2292                         return dev->allocationBlockFinder;
2293                 }
2294         }
2295         
2296         
2297         T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs tragedy: no more eraased blocks, but there should have been %d" TENDSTR),dev->nErasedBlocks));
2298         
2299         
2300         
2301
2302         
2303         return -1;      
2304 }
2305
2306
2307 // To determine if we have enough space we just look at the 
2308 // number of erased blocks.
2309
2310 static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev)
2311 {
2312         int reservedChunks = (dev->nReservedBlocks * dev->nChunksPerBlock);
2313         return (dev->nFreeChunks > reservedChunks);
2314 }
2315
2316
2317 static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve)
2318 {
2319         int retVal;
2320         yaffs_BlockInfo *bi;
2321         
2322         if(dev->allocationBlock < 0)
2323         {
2324                 // Get next block to allocate off
2325                 dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
2326                 dev->allocationPage = 0;
2327         }
2328         
2329         if(!useReserve &&  !yaffs_CheckSpaceForAllocation(dev))
2330         {
2331                 // Not enough space to allocate unless we're allowed to use the reserve.
2332                 return -1;
2333         }
2334
2335         if(dev->nErasedBlocks < dev->nReservedBlocks && dev->allocationPage == 0)
2336         {
2337                 T(YAFFS_TRACE_ALLOCATE,(TSTR("Allocating reserve" TENDSTR)));   
2338         }
2339
2340         
2341         // Next page please....
2342         if(dev->allocationBlock >= 0)
2343         {
2344                 bi = yaffs_GetBlockInfo(dev,dev->allocationBlock);
2345                 
2346                 retVal = (dev->allocationBlock * dev->nChunksPerBlock) + 
2347                                   dev->allocationPage;
2348                 bi->pagesInUse++;
2349                 yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage);
2350
2351                 dev->allocationPage++;
2352                 
2353                 dev->nFreeChunks--;
2354                 
2355                 // If the block is full set the state to full
2356                 if(dev->allocationPage >= dev->nChunksPerBlock)
2357                 {
2358                         bi->blockState = YAFFS_BLOCK_STATE_FULL;
2359                         dev->allocationBlock = -1;
2360                 }
2361
2362
2363                 return retVal;
2364                 
2365         }
2366         T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
2367
2368         return -1;      
2369 }
2370
2371
2372
2373
2374 static int yaffs_GetErasedChunks(yaffs_Device *dev)
2375 {
2376                 int n;
2377
2378                 n = dev->nErasedBlocks * dev->nChunksPerBlock;
2379
2380                 if(dev->allocationBlock> 0)
2381                 {
2382                         n += (dev->nChunksPerBlock - dev->allocationPage);
2383                 }
2384
2385                 return n;
2386
2387 }
2388
2389 int  yaffs_GarbageCollectBlock(yaffs_Device *dev,int block)
2390 {
2391         int oldChunk;
2392         int newChunk;
2393         int chunkInBlock;
2394         int markNAND;
2395         int retVal = YAFFS_OK;
2396         int cleanups = 0;
2397         int i;
2398
2399         int chunksBefore = yaffs_GetErasedChunks(dev);
2400         int chunksAfter;
2401
2402         yaffs_ExtendedTags  tags;
2403         
2404         yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block);
2405         
2406         yaffs_Object *object;
2407         
2408         bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
2409
2410         T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR),block,bi->pagesInUse,bi->hasShrinkHeader));
2411         //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits));        
2412         
2413         //yaffs_VerifyFreeChunks(dev);
2414
2415         bi->hasShrinkHeader = 0; // clear the flag so that the block can erase
2416         
2417         dev->nFreeChunks -= bi->softDeletions;  // Take off the number of soft deleted entries because
2418                                                 // they're going to get really deleted during GC.
2419
2420         dev->isDoingGC = 1;
2421
2422         if(!yaffs_StillSomeChunkBits(dev,block))
2423         {
2424                 T(YAFFS_TRACE_TRACING,(TSTR("Collecting block %d that has no chunks in use" TENDSTR),block));
2425                 yaffs_BlockBecameDirty(dev,block);
2426         }
2427         else
2428         {
2429
2430         __u8  *buffer = yaffs_GetTempBuffer(dev,__LINE__);
2431
2432         for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; 
2433             chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block);
2434             chunkInBlock++, oldChunk++ )
2435         {
2436                 if(yaffs_CheckChunkBit(dev,block,chunkInBlock))
2437                 {
2438                         
2439                         // This page is in use and might need to be copied off
2440                         
2441                         markNAND = 1;
2442                         
2443                         //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk));
2444                         
2445                         yaffs_InitialiseTags(&tags);
2446                         
2447                         yaffs_ReadChunkWithTagsFromNAND(dev,oldChunk,buffer, &tags);
2448
2449                         object = yaffs_FindObjectByNumber(dev,tags.objectId);
2450                         
2451                         T(YAFFS_TRACE_GC_DETAIL,(TSTR("Collecting page %d, %d %d %d " TENDSTR),chunkInBlock,tags.objectId,tags.chunkId,tags.byteCount));
2452                         
2453                         if(!object)
2454                         {
2455                                 T(YAFFS_TRACE_ERROR,(TSTR("page %d in gc has no object " TENDSTR),oldChunk));
2456                         }
2457                         
2458                         if(object && object->deleted && tags.chunkId != 0)
2459                         {
2460                                 // Data chunk in a deleted file, throw it away
2461                                 // It's a soft deleted data chunk,
2462                                 // No need to copy this, just forget about it and fix up the
2463                                 // object.
2464                                 
2465                                 //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); 
2466                                 object->nDataChunks--;
2467                                 
2468                                 if(object->nDataChunks <= 0)
2469                                 {
2470                                         // remeber to clean up the object
2471                                         dev->gcCleanupList[cleanups] = tags.objectId;
2472                                         cleanups++;
2473                                 }
2474                                 markNAND = 0;
2475                         }
2476                         else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */)
2477                         {
2478                                 // Deleted object header with no data chunks.
2479                                 // Can be discarded and the file deleted.
2480                                 object->chunkId = 0;
2481                                 yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top);
2482                                 object->variant.fileVariant.top = NULL;
2483                                 yaffs_DoGenericObjectDeletion(object);
2484                                 
2485                         }
2486                         else if(object)
2487                         {
2488                                 // It's either a data chunk in a live file or
2489                                 // an ObjectHeader, so we're interested in it.
2490                                 // NB Need to keep the ObjectHeaders of deleted files
2491                                 // until the whole file has been deleted off
2492                                 tags.serialNumber++;
2493
2494                                 dev->nGCCopies++;
2495
2496                                 newChunk = yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags,1);
2497                         
2498                                 if(newChunk < 0)
2499                                 {
2500                                         retVal =  YAFFS_FAIL;
2501                                 }
2502                                 else
2503                                 {
2504                         
2505                                         // Ok, now fix up the Tnodes etc.
2506                         
2507                                         if(tags.chunkId == 0)
2508                                         {
2509                                                 // It's a header
2510                                                 object->chunkId = newChunk;
2511                                                 object->serial = tags.serialNumber;
2512                                         }
2513                                         else
2514                                         {
2515                                                 // It's a data chunk
2516                                                 yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0);
2517                                         }
2518                                 }
2519                         }
2520                         
2521                         yaffs_DeleteChunk(dev,oldChunk,markNAND,__LINE__);                      
2522                         
2523                 }
2524         }
2525
2526         yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
2527
2528         //yaffs_VerifyFreeChunks(dev);
2529
2530         // Do any required cleanups
2531         for(i = 0; i < cleanups; i++)
2532         {                                               
2533                 // Time to delete the file too
2534                 object =  yaffs_FindObjectByNumber(dev,dev->gcCleanupList[i]);
2535                 if(object)
2536                 {
2537                         yaffs_FreeTnode(dev,object->variant.fileVariant.top);
2538                         object->variant.fileVariant.top = NULL;
2539                         T(YAFFS_TRACE_GC,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId));
2540                         yaffs_DoGenericObjectDeletion(object);
2541                 }
2542
2543         }
2544
2545         }
2546
2547         if(chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev)))
2548         {
2549                         T(YAFFS_TRACE_GC,(TSTR("gc did not increase free chunks before %d after %d" TENDSTR),chunksBefore,chunksAfter));
2550         }
2551         
2552         
2553         dev->isDoingGC = 0;
2554         
2555         //yaffs_VerifyFreeChunks(dev);
2556                         
2557         return YAFFS_OK;
2558 }
2559
2560 #if 0
2561 static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev)
2562 {
2563         // find a file to delete
2564         struct list_head *i;    
2565         yaffs_Object *l;
2566
2567
2568         //Scan the unlinked files looking for one to delete
2569         list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children)
2570         {
2571                 if(i)
2572                 {
2573                         l = list_entry(i, yaffs_Object,siblings);
2574                         if(l->deleted)
2575                         {
2576                                 return l;                       
2577                         }
2578                 }
2579         }       
2580         return NULL;
2581 }
2582
2583
2584 static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev)
2585 {
2586         // This does background deletion on unlinked files.. only deleted ones.
2587         // If we don't have a file we're working on then find one
2588         if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0)
2589         {
2590                 dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev);
2591         }
2592         
2593         // OK, we're working on a file...
2594         if(dev->unlinkedDeletion)
2595         {
2596                 yaffs_Object *obj = dev->unlinkedDeletion;
2597                 int delresult;
2598                 int limit; // Number of chunks to delete in a file.
2599                                    // NB this can be exceeded, but not by much.
2600                                    
2601                 limit = -1;
2602
2603                 delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit);
2604                 
2605                 if(obj->nDataChunks == 0)
2606                 {
2607                         // Done all the deleting of data chunks.
2608                         // Now dump the header and clean up
2609                         yaffs_FreeTnode(dev,obj->variant.fileVariant.top);
2610                         obj->variant.fileVariant.top = NULL;
2611                         yaffs_DoGenericObjectDeletion(obj);
2612                         dev->nDeletedFiles--;
2613                         dev->nUnlinkedFiles--;
2614                         dev->nBackgroundDeletions++;
2615                         dev->unlinkedDeletion = NULL;   
2616                 }
2617         }
2618 }
2619
2620 #endif
2621
2622
2623
2624 // New garbage collector
2625 // If we're very low on erased blocks then we do aggressive garbage collection
2626 // otherwise we do "leasurely" garbage collection.
2627 // Aggressive gc looks further (whole array) and will accept dirtier blocks.
2628 // Passive gc only inspects smaller areas and will only accept cleaner blocks.
2629 //
2630 // The idea is to help clear out space in a more spread-out manner.
2631 // Dunno if it really does anything useful.
2632 //
2633 int yaffs_CheckGarbageCollection(yaffs_Device *dev)
2634 {
2635         int block;
2636         int aggressive; 
2637         int gcOk = YAFFS_OK;
2638         int maxTries = 0;
2639         
2640         //yaffs_VerifyFreeChunks(dev);
2641         
2642         if(dev->isDoingGC)
2643         {
2644                 // Bail out so we don't get recursive gc
2645                 return YAFFS_OK;
2646         }
2647
2648         // This loop should pass the first time.
2649         // We'll only see looping here if the erase of the collected block fails.
2650         
2651         do{
2652                 maxTries++;
2653                 if(dev->nErasedBlocks < dev->nReservedBlocks)
2654                 {
2655                         // We need a block soon...
2656                         aggressive = 1;
2657                 }
2658                 else 
2659                 {
2660                         // We're in no hurry
2661                         aggressive = 0;
2662                 }
2663         
2664                 block = yaffs_FindBlockForGarbageCollection(dev,aggressive);
2665         
2666                 if(block > 0)
2667                 {
2668                         dev->garbageCollections++;
2669                         if(!aggressive)
2670                         {
2671                                 dev->passiveGarbageCollections++;
2672                         }
2673
2674                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive));
2675
2676                         gcOk =  yaffs_GarbageCollectBlock(dev,block);
2677                 }
2678
2679                 if(dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) 
2680                 {
2681                         T(YAFFS_TRACE_GC,(TSTR("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d" TENDSTR),dev->nErasedBlocks,maxTries,block));
2682                 }
2683         } while((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0) && (maxTries < 2));
2684
2685         return aggressive ? gcOk: YAFFS_OK;
2686 }
2687
2688
2689 //////////////////////////// TAGS ///////////////////////////////////////
2690
2691
2692
2693 #if 0
2694
2695 void yaffs_CalcTagsECC(yaffs_Tags *tags)
2696 {
2697         // Calculate an ecc
2698         
2699         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2700         unsigned  i,j;
2701         unsigned  ecc = 0;
2702         unsigned bit = 0;
2703
2704         tags->ecc = 0;
2705         
2706         for(i = 0; i < 8; i++)
2707         {
2708                 for(j = 1; j &0xff; j<<=1)
2709                 {
2710                         bit++;
2711                         if(b[i] & j)
2712                         {
2713                                 ecc ^= bit;
2714                         }
2715                 }
2716         }
2717         
2718         tags->ecc = ecc;
2719         
2720         
2721 }
2722
2723 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
2724 {
2725         unsigned ecc = tags->ecc;
2726         
2727         yaffs_CalcTagsECC(tags);
2728         
2729         ecc ^= tags->ecc;
2730         
2731         if(ecc && ecc <= 64)
2732         {
2733                 // TODO: Handle the failure better. Retire?
2734                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
2735
2736                 ecc--;
2737                                 
2738                 b[ecc / 8] ^= (1 << (ecc & 7));
2739                 
2740                 // Now recvalc the ecc
2741                 yaffs_CalcTagsECC(tags);
2742                 
2743                 return 1; // recovered error
2744         }
2745         else if(ecc)
2746         {
2747                 // Wierd ecc failure value
2748                 // TODO Need to do somethiong here
2749                 return -1; //unrecovered error
2750         }
2751         
2752         return 0;
2753 }
2754
2755 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
2756 {
2757         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2758         
2759         yaffs_CalcTagsECC(tagsPtr);
2760         
2761         sparePtr->tagByte0 = tu->asBytes[0];
2762         sparePtr->tagByte1 = tu->asBytes[1];
2763         sparePtr->tagByte2 = tu->asBytes[2];
2764         sparePtr->tagByte3 = tu->asBytes[3];
2765         sparePtr->tagByte4 = tu->asBytes[4];
2766         sparePtr->tagByte5 = tu->asBytes[5];
2767         sparePtr->tagByte6 = tu->asBytes[6];
2768         sparePtr->tagByte7 = tu->asBytes[7];
2769 }
2770
2771 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
2772 {
2773         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
2774         int result;
2775
2776         tu->asBytes[0]= sparePtr->tagByte0;
2777         tu->asBytes[1]= sparePtr->tagByte1;
2778         tu->asBytes[2]= sparePtr->tagByte2;
2779         tu->asBytes[3]= sparePtr->tagByte3;
2780         tu->asBytes[4]= sparePtr->tagByte4;
2781         tu->asBytes[5]= sparePtr->tagByte5;
2782         tu->asBytes[6]= sparePtr->tagByte6;
2783         tu->asBytes[7]= sparePtr->tagByte7;
2784         
2785         result =  yaffs_CheckECCOnTags(tagsPtr);
2786         if(result> 0)
2787         {
2788                 dev->tagsEccFixed++;
2789         }
2790         else if(result <0)
2791         {
2792                 dev->tagsEccUnfixed++;
2793         }
2794 }
2795
2796 static void yaffs_SpareInitialise(yaffs_Spare *spare)
2797 {
2798         memset(spare,0xFF,sizeof(yaffs_Spare));
2799 }
2800
2801 #endif
2802
2803 #if 0
2804 static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_ExtendedTags *tags, int useReserve)
2805 {
2806         // NB There must be tags, data is optional
2807         // If there is data, then an ECC is calculated on it.
2808         
2809         yaffs_Spare spare;
2810         
2811         if(!tags)
2812         {
2813                 return YAFFS_FAIL;
2814         }
2815         
2816         //yaffs_SpareInitialise(&spare);
2817         
2818         //if(!dev->useNANDECC && buffer)
2819         //{
2820         //      yaffs_CalcECC(buffer,&spare);
2821         //}
2822         
2823         //yaffs_LoadTagsIntoSpare(&spare,tags);
2824         
2825         return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve);
2826         
2827 }
2828 #endif
2829
2830 static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId, int chunkInObject)
2831 {
2832         return  (  tags->chunkId == chunkInObject &&
2833                            tags->objectId == objectId &&
2834                            !tags->chunkDeleted) ? 1 : 0;
2835         
2836 }
2837
2838 /////////////////////////////////////////////////////////////////////////////////////////////////////////
2839
2840
2841 int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2842 {
2843         //Get the Tnode, then get the level 0 offset chunk offset
2844     yaffs_Tnode *tn;     
2845     int theChunk = -1;
2846     yaffs_ExtendedTags localTags;
2847     int retVal = -1;
2848     
2849     yaffs_Device *dev = in->myDev;
2850     
2851     
2852     if(!tags)
2853     {
2854         // Passed a NULL, so use our own tags space
2855         tags = &localTags;
2856     }
2857     
2858     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2859     
2860     if(tn)
2861     {
2862                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2863                 
2864                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2865     }
2866     return retVal;
2867 }
2868
2869 int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_ExtendedTags *tags)
2870 {
2871         //Get the Tnode, then get the level 0 offset chunk offset
2872     yaffs_Tnode *tn;     
2873     int theChunk = -1;
2874     yaffs_ExtendedTags localTags;
2875
2876     yaffs_Device *dev = in->myDev;
2877     int retVal = -1;
2878     
2879     if(!tags)
2880     {
2881         // Passed a NULL, so use our own tags space
2882         tags = &localTags;
2883     }
2884     
2885     tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
2886     
2887     if(tn)
2888     {
2889     
2890                 theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits;
2891                 
2892                 retVal = yaffs_FindChunkInGroup(dev,theChunk,tags,in->objectId,chunkInInode);
2893     
2894                 // Delete the entry in the filestructure (if found)
2895                 if(retVal != -1)
2896                 {
2897                         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0;
2898                 }
2899     }
2900     else
2901     {
2902         //T(("No level 0 found for %d\n", chunkInInode));
2903     }
2904     
2905     if(retVal == -1)
2906     {
2907         //T(("Could not find %d to delete\n",chunkInInode));
2908     }
2909     return retVal;
2910 }
2911
2912
2913 #ifdef YAFFS_PARANOID
2914
2915 static int yaffs_CheckFileSanity(yaffs_Object *in)
2916 {
2917         int chunk;
2918         int nChunks;
2919         int fSize;
2920         int failed = 0;
2921         int objId;
2922         yaffs_Tnode *tn;
2923     yaffs_Tags localTags;
2924     yaffs_Tags *tags = &localTags;
2925     int theChunk;
2926     int chunkDeleted;
2927     
2928         
2929         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2930         {
2931                 //T(("Object not a file\n"));
2932                 return YAFFS_FAIL;
2933         }
2934         
2935         objId = in->objectId;
2936         fSize  = in->variant.fileVariant.fileSize;
2937         nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk;
2938         
2939         for(chunk = 1; chunk <= nChunks; chunk++)
2940         {
2941                 tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk);
2942     
2943                 if(tn)
2944                 {
2945     
2946                         theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits;
2947                         
2948                 if(yaffs_CheckChunkBits(dev,theChunk/dev->nChunksPerBlock,theChunk%dev->nChunksPerBlock))
2949                         {
2950
2951
2952                                 yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted);
2953                                 if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted))
2954                                 {
2955                                         // found it;
2956                                 
2957                                 }
2958                         }
2959                         else
2960                         {
2961                                 //T(("File problem file [%d,%d] NAND %d  tags[%d,%d]\n",
2962                                 //              objId,chunk,theChunk,tags->chunkId,tags->objectId);
2963                                                 
2964                                 failed = 1;
2965                                                         
2966                         }
2967     
2968                 }
2969                 else
2970                 {
2971                         //T(("No level 0 found for %d\n", chunk));
2972                 }
2973         }
2974         
2975         return failed ? YAFFS_FAIL : YAFFS_OK;
2976 }
2977
2978 #endif
2979
2980 static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan)
2981 {
2982         // NB inScan is zero unless scanning. For forward scanning, inScan is > 0; for backward scanning inScan is < 0
2983         yaffs_Tnode *tn;
2984         yaffs_Device *dev = in->myDev;
2985         int existingChunk;
2986         yaffs_ExtendedTags existingTags;
2987         yaffs_ExtendedTags newTags;
2988         unsigned existingSerial, newSerial;
2989         
2990         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
2991         {
2992                 // Just ignore an attempt at putting a chunk into a non-file during scanning
2993                 // If it is not during Scanning then something went wrong!
2994                 if(!inScan)
2995                 {
2996                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy:attempt to put data chunk into a non-file" TENDSTR)));
2997                         YBUG();
2998                 }
2999                 
3000                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3001                 return YAFFS_OK;
3002         }
3003                 
3004         tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode);
3005         if(!tn)
3006         {
3007                 return YAFFS_FAIL;
3008         }
3009
3010         existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK];            
3011         
3012         if(inScan != 0)
3013         {
3014                 // If we're scanning then we need to test for duplicates
3015                 // NB This does not need to be efficient since it should only ever 
3016                 // happen when the power fails during a write, then only one
3017                 // chunk should ever be affected.
3018                 //
3019                 // Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
3020                 // Update: For backward scanning we don't need to re-read tags so this is quite cheap.
3021                 
3022         
3023                 
3024                 if(existingChunk != 0)
3025                 {
3026                         // NB Right now existing chunk will not be real chunkId if the device >= 32MB
3027                         //    thus we have to do a FindChunkInFile to get the real chunk id.
3028                         //
3029                         // We have a duplicate now we need to decide which one to use:
3030                         //
3031                         // Backwards scanning YAFFS2: The old one is what we use, dump the new one.
3032                         // Forward scanning YAFFS2: The new one is what we use, dump the old one.
3033                         // YAFFS1: Get both sets of tags and compare serial numbers.
3034                         //
3035                         //
3036                         
3037                         if(inScan > 0)
3038                         {
3039                                 // Only do this for forward scanning
3040                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunkInNAND, NULL,&newTags);
3041                         
3042                         
3043                                 // Do a proper find
3044                                 existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags);
3045                         }
3046
3047                         if(existingChunk <=0)
3048                         {
3049                                 //Hoosterman - how did this happen?
3050                                 
3051                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR)));
3052
3053                         }
3054
3055                         
3056                         // NB The deleted flags should be false, otherwise the chunks will 
3057                         // not be loaded during a scan
3058                         
3059                         newSerial = newTags.serialNumber;
3060                         existingSerial = existingTags.serialNumber;
3061                         
3062                         if( (inScan > 0) &&
3063                             ( in->myDev->isYaffs2  ||
3064                               existingChunk <= 0 ||
3065                              ((existingSerial+1) & 3) == newSerial))
3066                         {
3067                                 // Forward scanning.                            
3068                                 // Use new
3069                                 // Delete the old one and drop through to update the tnode
3070                                 yaffs_DeleteChunk(dev,existingChunk,1,__LINE__);
3071                         }
3072                         else
3073                         {
3074                                 // Backward scanning or we want to use the existing one
3075                                 // Use existing.
3076                                 // Delete the new one and return early so that the tnode isn't changed
3077                                 yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
3078                                 return YAFFS_OK;
3079                         }
3080                 }
3081
3082         }
3083                 
3084         if(existingChunk == 0)
3085         {
3086                 in->nDataChunks++;
3087         }
3088         
3089         tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits);
3090         
3091         return YAFFS_OK;
3092 }
3093
3094
3095
3096 int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer)
3097 {
3098     int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL);
3099     
3100     if(chunkInNAND >= 0)
3101     {
3102                 return yaffs_ReadChunkWithTagsFromNAND(in->myDev,chunkInNAND,buffer,NULL);
3103         }
3104         else
3105         {
3106                 T(YAFFS_TRACE_NANDACCESS,(TSTR("Chunk %d not found zero instead" TENDSTR),chunkInNAND));
3107
3108                 memset(buffer,0,in->myDev->nBytesPerChunk); // get sane data if you read a hole
3109                 return 0;
3110         }
3111
3112 }
3113
3114
3115 void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND,int lyn)
3116 {
3117         int block;
3118         int page;
3119         yaffs_ExtendedTags tags;
3120         yaffs_BlockInfo *bi;
3121         
3122         if(chunkId <= 0) return;        
3123         
3124         dev->nDeletions++;
3125         block = chunkId / dev->nChunksPerBlock;
3126         page = chunkId % dev->nChunksPerBlock;
3127         
3128         bi = yaffs_GetBlockInfo(dev,block);
3129         
3130         T(YAFFS_TRACE_DELETION,(TSTR("line %d delete of chunk %d" TENDSTR),lyn,chunkId));
3131         
3132         if(markNAND && 
3133           bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
3134           !dev->isYaffs2)
3135         {
3136 //              yaffs_SpareInitialise(&spare);
3137
3138 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
3139
3140                 //read data before write, to ensure correct ecc 
3141                 //if we're using MTD verification under Linux
3142                 yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0);
3143 #endif
3144
3145                 yaffs_InitialiseTags(&tags);
3146
3147                 tags.chunkDeleted = 1;
3148
3149         
3150                 yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags);
3151                 yaffs_HandleUpdateChunk(dev,chunkId,&tags);
3152         }
3153         else
3154         {
3155                         dev->nUnmarkedDeletions++;
3156         }       
3157         
3158         
3159         // Pull out of the management area.
3160         // If the whole block became dirty, this will kick off an erasure.
3161         if(     bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
3162             bi->blockState == YAFFS_BLOCK_STATE_FULL ||     
3163             bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
3164             bi->blockState == YAFFS_BLOCK_STATE_COLLECTING)
3165         {
3166                 dev->nFreeChunks++;
3167
3168                 yaffs_ClearChunkBit(dev,block,page);
3169                 
3170                 bi->pagesInUse--;
3171                 
3172                 if(bi->pagesInUse == 0 &&
3173                    !bi->hasShrinkHeader &&
3174                bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
3175                bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING)
3176             {
3177                 yaffs_BlockBecameDirty(dev,block);
3178             }
3179
3180         }
3181         else
3182         {
3183                 // T(("Bad news deleting chunk %d\n",chunkId));
3184         }
3185         
3186 }
3187
3188
3189
3190
3191 int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve)
3192 {
3193         // Find old chunk Need to do this to get serial number
3194         // Write new one and patch into tree.
3195         // Invalidate old tags.
3196
3197     int prevChunkId;
3198     yaffs_ExtendedTags prevTags;
3199     
3200     int newChunkId;
3201     yaffs_ExtendedTags newTags;
3202
3203     yaffs_Device *dev = in->myDev;    
3204
3205         yaffs_CheckGarbageCollection(dev);
3206
3207         // Get the previous chunk at this location in the file if it exists
3208     prevChunkId  = yaffs_FindChunkInFile(in,chunkInInode,&prevTags);
3209     
3210     // Set up new tags
3211     yaffs_InitialiseTags(&newTags);
3212     
3213         newTags.chunkId = chunkInInode;
3214         newTags.objectId = in->objectId;
3215         newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
3216         newTags.byteCount = nBytes;
3217                 
3218 //      yaffs_CalcTagsECC(&newTags);
3219
3220         newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve);
3221         
3222         if(newChunkId >= 0)
3223         {
3224                 yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0);
3225                 
3226                 
3227                 if(prevChunkId >= 0)
3228                 {
3229                         yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3230         
3231                 }
3232                 
3233                 yaffs_CheckFileSanity(in);
3234         }
3235         return newChunkId;
3236
3237
3238
3239
3240
3241 }
3242
3243
3244 // UpdateObjectHeader updates the header on NAND for an object.
3245 // If name is not NULL, then that new name is used.
3246 //
3247 int yaffs_UpdateObjectHeader(yaffs_Object *in,const YCHAR *name, int force,int isShrink)
3248 {
3249
3250         yaffs_BlockInfo *bi;
3251         
3252         yaffs_Device *dev = in->myDev;
3253         
3254     int prevChunkId;
3255     int retVal = 0;
3256     
3257     int newChunkId;
3258     yaffs_ExtendedTags newTags;
3259     
3260     __u8 *buffer = NULL;
3261     YCHAR oldName[YAFFS_MAX_NAME_LENGTH+1];
3262     
3263    // __u8 bufferOld[YAFFS_BYTES_PER_CHUNK];
3264     
3265     yaffs_ObjectHeader *oh = NULL;
3266     // yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld;
3267
3268     
3269     if(!in->fake || force)
3270     {
3271   
3272                 yaffs_CheckGarbageCollection(dev);              
3273     
3274             buffer = yaffs_GetTempBuffer(in->myDev,__LINE__);
3275             oh  = (yaffs_ObjectHeader *)buffer;
3276     
3277                 prevChunkId = in->chunkId;
3278     
3279                 if(prevChunkId >= 0)
3280                 {
3281                         yaffs_ReadChunkWithTagsFromNAND(dev,prevChunkId,buffer,NULL);
3282                         memcpy(oldName,oh->name,sizeof(oh->name));      
3283                 }
3284
3285                 memset(buffer,0xFF,dev->nBytesPerChunk);
3286
3287                 // Header data
3288                 oh->type = in->variantType;
3289                 
3290                 oh->yst_mode = in->yst_mode;
3291
3292 #ifdef CONFIG_YAFFS_WINCE
3293                 oh->win_atime[0] = in->win_atime[0];
3294                 oh->win_ctime[0] = in->win_ctime[0];
3295                 oh->win_mtime[0] = in->win_mtime[0];
3296                 oh->win_atime[1] = in->win_atime[1];
3297                 oh->win_ctime[1] = in->win_ctime[1];
3298                 oh->win_mtime[1] = in->win_mtime[1];
3299 #else
3300                 oh->yst_uid = in->yst_uid;
3301                 oh->yst_gid = in->yst_gid;
3302                 oh->yst_atime = in->yst_atime;
3303                 oh->yst_mtime = in->yst_mtime;
3304                 oh->yst_ctime = in->yst_ctime;
3305                 oh->yst_rdev = in->yst_rdev;
3306 #endif  
3307                 if(in->parent)
3308                 {
3309                         oh->parentObjectId = in->parent->objectId;
3310                 }
3311                 else
3312                 {
3313                         oh->parentObjectId = 0;
3314                 }
3315                 
3316                 //oh->sum = in->sum;
3317                 if(name && *name)
3318                 {
3319                         memset(oh->name,0,sizeof(oh->name));
3320                         yaffs_strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
3321                 }
3322                 else if(prevChunkId)
3323                 {       
3324                         memcpy(oh->name, oldName,sizeof(oh->name));
3325                 }
3326                 else
3327                 {
3328                         memset(oh->name,0,sizeof(oh->name));    
3329                 }
3330                 
3331                 oh->isShrink = isShrink;
3332         
3333                 switch(in->variantType)
3334                 {
3335                         case YAFFS_OBJECT_TYPE_UNKNOWN:         
3336                                 // Should not happen
3337                                 break;
3338                         case YAFFS_OBJECT_TYPE_FILE:
3339                                 oh->fileSize = (oh->parentObjectId == YAFFS_OBJECTID_DELETED ||
3340                                                 oh->parentObjectId == YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.fileVariant.fileSize;
3341                                 break;
3342                         case YAFFS_OBJECT_TYPE_HARDLINK:
3343                                 oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId;
3344                                 break;
3345                         case YAFFS_OBJECT_TYPE_SPECIAL: 
3346                                 // Do nothing
3347                                 break;
3348                         case YAFFS_OBJECT_TYPE_DIRECTORY:       
3349                                 // Do nothing
3350                                 break;
3351                         case YAFFS_OBJECT_TYPE_SYMLINK:
3352                                 yaffs_strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH);
3353                                 oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
3354                                 break;
3355                 }
3356
3357                 // Tags
3358                 yaffs_InitialiseTags(&newTags);
3359                 in->serial++;
3360                 newTags.chunkId = 0;
3361                 newTags.objectId = in->objectId;
3362                 newTags.serialNumber = in->serial;
3363                 
3364                 // Add extra info for file header
3365                 
3366                 newTags.extraHeaderInfoAvailable = 1;
3367                 newTags.extraParentObjectId = oh->parentObjectId;
3368                 newTags.extraFileLength = oh->fileSize;
3369                 newTags.extraIsShrinkHeader = oh->isShrink;
3370                 newTags.extraEquivalentObjectId = oh->equivalentObjectId;
3371                 newTags.extraObjectType  = in->variantType;
3372                 
3373                 // Create new chunk in NAND
3374                 newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags, (prevChunkId >= 0) ? 1 : 0 );
3375     
3376                 if(newChunkId >= 0)
3377                 {
3378                 
3379                         in->chunkId = newChunkId;       
3380                 
3381                         if(prevChunkId >= 0)
3382                         {
3383                                 yaffs_DeleteChunk(dev,prevChunkId,1,__LINE__);
3384                         }
3385                 
3386                         in->dirty = 0;
3387                         
3388                         // If this was a shrink, then mark the block that the chunk lives on
3389                         if(isShrink)
3390                         {
3391                                 bi = yaffs_GetBlockInfo(in->myDev,newChunkId / in->myDev->nChunksPerBlock);
3392                                 bi->hasShrinkHeader = 1;
3393                         }
3394                         
3395                 }
3396                 
3397                 retVal =  newChunkId;
3398
3399     }
3400     
3401         if(buffer) 
3402                 yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
3403         
3404     return retVal;
3405 }
3406
3407
3408 /////////////////////// Short Operations Cache ////////////////////////////////
3409 //      In many siturations where there is no high level buffering (eg WinCE) a lot of
3410 //      reads might be short sequential reads, and a lot of writes may be short 
3411 //  sequential writes. eg. scanning/writing a jpeg file.
3412 //      In these cases, a short read/write cache can provide a huge perfomance benefit 
3413 //  with dumb-as-a-rock code.
3414 //  There are a limited number (~10) of cache chunks per device so that we don't
3415 //  need a very intelligent search.
3416
3417
3418
3419
3420
3421 static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
3422 {
3423         yaffs_Device *dev = obj->myDev;
3424         int lowest;
3425         int i;
3426         yaffs_ChunkCache *cache;
3427         int chunkWritten = 0;
3428         //int nBytes;
3429         int nCaches = obj->myDev->nShortOpCaches;
3430         
3431         if  (nCaches > 0)
3432         {
3433                 do{
3434                         cache = NULL;
3435                 
3436                         // Find the dirty cache for this object with the lowest chunk id.
3437                         for(i = 0; i < nCaches; i++)
3438                         {
3439                                 if(dev->srCache[i].object == obj &&
3440                                    dev->srCache[i].dirty)
3441                                 {
3442                                         if(!cache ||  dev->srCache[i].chunkId < lowest)
3443                                         {
3444                                                 cache = &dev->srCache[i];
3445                                                 lowest = cache->chunkId;
3446                                         }
3447                                 }
3448                         }
3449                 
3450                         if(cache && !cache->locked)
3451                         {
3452                                 //Write it out and free it up
3453
3454 #if 0
3455                                 nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK);
3456                         
3457                                 if(nBytes > YAFFS_BYTES_PER_CHUNK)
3458                                 {
3459                                         nBytes= YAFFS_BYTES_PER_CHUNK;
3460                                 }
3461 #endif                  
3462                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3463                                                                                                                         cache->chunkId,
3464                                                                                                                         cache->data,
3465                                                                                                                         cache->nBytes,1);
3466
3467                                 cache->dirty = 0;
3468                                 cache->object = NULL;
3469                         }
3470                 
3471                 } while(cache && chunkWritten > 0);
3472         
3473                 if(cache)
3474                 {
3475                         //Hoosterman, disk full while writing cache out.
3476                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
3477
3478                 }
3479         }       
3480                 
3481 }
3482
3483
3484 // Grab us a chunk for use.
3485 // First look for an empty one. 
3486 // Then look for the least recently used non-dirty one.
3487 // Then look for the least recently used dirty one...., flush and look again.
3488 static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
3489 {
3490         int i;
3491         int usage;
3492         int theOne;
3493         
3494         if(dev->nShortOpCaches > 0)
3495         {
3496                 for(i = 0; i < dev->nShortOpCaches; i++)
3497                 {
3498                         if(!dev->srCache[i].object)
3499                         {
3500                                 //T(("Grabbing empty %d\n",i));
3501                                 
3502                                 //printf("Grabbing empty %d\n",i);
3503                         
3504                                 return &dev->srCache[i];
3505                         }
3506                 }
3507                 
3508                 return NULL;
3509         
3510                 theOne = -1; 
3511                 usage = 0; // just to stop the compiler grizzling
3512         
3513                 for(i = 0; i < dev->nShortOpCaches; i++)
3514                 {
3515                         if(!dev->srCache[i].dirty &&
3516                         ((dev->srCache[i].lastUse < usage  && theOne >= 0)|| 
3517                                 theOne < 0))
3518                         {
3519                                 usage = dev->srCache[i].lastUse;
3520                                 theOne = i;
3521                         }
3522                 }
3523         
3524                 //T(("Grabbing non-empty %d\n",theOne));
3525                 
3526                 //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne);
3527                 
3528                 return  theOne >= 0 ?  &dev->srCache[theOne] : NULL;
3529         }
3530         else
3531         {
3532                 return NULL;
3533         }
3534         
3535 }
3536
3537
3538 static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
3539 {
3540         yaffs_ChunkCache *cache;
3541         yaffs_Object *theObj;
3542         int usage;
3543         int i;
3544         int pushout;
3545         
3546         if(dev->nShortOpCaches > 0)
3547         {
3548                 // Try find a non-dirty one...
3549         
3550                 cache = yaffs_GrabChunkCacheWorker(dev);
3551         
3552                 if(!cache)
3553                 {
3554                         // They were all dirty, find the last recently used object and flush
3555                         // its cache, then  find again.
3556                         // NB what's here is not very accurate, we actually flush the object
3557                         // the last recently used page.
3558                         
3559                         // With locking we can't assume we can use entry zero
3560                         
3561                 
3562                         theObj = NULL;
3563                         usage = -1;
3564                         cache = NULL;
3565                         pushout = -1;
3566         
3567                         for(i = 0; i < dev->nShortOpCaches; i++)
3568                         {
3569                                 if( dev->srCache[i].object && 
3570                                         !dev->srCache[i].locked &&
3571                                         (dev->srCache[i].lastUse < usage || !cache))
3572                                 {
3573                                         usage  = dev->srCache[i].lastUse;
3574                                         theObj = dev->srCache[i].object;
3575                                         cache = &dev->srCache[i];
3576                                         pushout = i;
3577                                 }
3578                         }
3579                 
3580                         if(!cache || cache->dirty)
3581                         {
3582                         
3583                                 //printf("Dirty ");
3584                                 yaffs_FlushFilesChunkCache(theObj);
3585                 
3586                                 // Try again
3587                                 cache = yaffs_GrabChunkCacheWorker(dev);
3588                         }
3589                         else
3590                         {
3591                                 //printf(" pushout %d\n",pushout);
3592                         }
3593                         
3594                 }
3595                 return cache;
3596         }
3597         else
3598                 return NULL;
3599
3600 }
3601
3602
3603 // Find a cached chunk
3604 static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId)
3605 {
3606         yaffs_Device *dev = obj->myDev;
3607         int i;
3608         if(dev->nShortOpCaches > 0)
3609         {
3610                 for(i = 0; i < dev->nShortOpCaches; i++)
3611                 {
3612                         if(dev->srCache[i].object == obj && 
3613                         dev->srCache[i].chunkId == chunkId)
3614                         {
3615                                 dev->cacheHits++;
3616                         
3617                                 return &dev->srCache[i];
3618                         }
3619                 }
3620         }
3621         return NULL;
3622 }
3623
3624 // Mark the chunk for the least recently used algorithym
3625 static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite)
3626 {
3627
3628         if(dev->nShortOpCaches > 0)
3629         {
3630                 if( dev->srLastUse < 0 || 
3631                         dev->srLastUse > 100000000)
3632                 {
3633                         // Reset the cache usages
3634                         int i;
3635                         for(i = 1; i < dev->nShortOpCaches; i++)
3636                         {
3637                                 dev->srCache[i].lastUse = 0;
3638                         }
3639                         dev->srLastUse = 0;
3640                 }
3641
3642                 dev->srLastUse++;
3643         
3644                 cache->lastUse = dev->srLastUse;
3645
3646                 if(isAWrite)
3647                 {
3648                         cache->dirty = 1;
3649                 }
3650         }
3651 }
3652
3653 // Invalidate a single cache page.
3654 // Do this when a whole page gets written,
3655 // ie the short cache for this page is no longer valid.
3656 static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
3657 {
3658         if(object->myDev->nShortOpCaches > 0)
3659         {
3660                 yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId);
3661
3662                 if(cache)
3663                 {
3664                         cache->object = NULL;
3665                 }
3666         }
3667 }
3668
3669
3670 // Invalidate all the cache pages associated with this object
3671 // Do this whenever ther file is deleted or resized.
3672 static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
3673 {
3674         int i;
3675         yaffs_Device *dev = in->myDev;
3676         
3677         if(dev->nShortOpCaches > 0)
3678         { 
3679                 // Now invalidate it.
3680                 for(i = 0; i < dev->nShortOpCaches; i++)
3681                 {
3682                         if(dev->srCache[i].object == in)
3683                         {
3684                                 dev->srCache[i].object = NULL;
3685                         }
3686                 }
3687         }
3688 }
3689
3690
3691
3692
3693
3694 ///////////////////////// File read/write ///////////////////////////////
3695 // Read and write have very similar structures.
3696 // In general the read/write has three parts to it
3697 // * An incomplete chunk to start with (if the read/write is not chunk-aligned)
3698 // * Some complete chunks
3699 // * An incomplete chunk to end off with
3700 //
3701 // Curve-balls: the first chunk might also be the last chunk.
3702
3703 int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes)
3704 {
3705         
3706         
3707         int chunk;
3708         int start;
3709         int nToCopy;
3710         int n = nBytes;
3711         int nDone = 0;
3712         yaffs_ChunkCache *cache;
3713         
3714         yaffs_Device *dev;
3715         
3716         dev = in->myDev;
3717         
3718         while(n > 0)
3719         {
3720                 chunk = offset / dev->nBytesPerChunk + 1; // The first chunk is 1
3721                 start = offset % dev->nBytesPerChunk;
3722
3723                 // OK now check for the curveball where the start and end are in
3724                 // the same chunk.      
3725                 if(     (start + n) < dev->nBytesPerChunk)
3726                 {
3727                         nToCopy = n;
3728                 }
3729                 else
3730                 {
3731                         nToCopy = dev->nBytesPerChunk - start;
3732                 }
3733         
3734                 cache = yaffs_FindChunkCache(in,chunk);
3735                 
3736                 // If the chunk is already in the cache or it is less than a whole chunk
3737                 // then use the cache (if there is caching)
3738                 // else bypass the cache.
3739                 if( cache || nToCopy != dev->nBytesPerChunk)
3740                 {
3741                         if(dev->nShortOpCaches > 0)
3742                         {
3743                                 
3744                                 // If we can't find the data in the cache, then load it up.
3745                                 
3746                                 if(!cache)
3747                                 {
3748                                         cache = yaffs_GrabChunkCache(in->myDev);
3749                                         cache->object = in;
3750                                         cache->chunkId = chunk;
3751                                         cache->dirty = 0;
3752                                         cache->locked = 0;
3753                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);
3754                                         cache->nBytes = 0;      
3755                                 }
3756                         
3757                                 yaffs_UseChunkCache(dev,cache,0);
3758
3759                                 cache->locked = 1;
3760
3761 #ifdef CONFIG_YAFFS_WINCE
3762                                 yfsd_UnlockYAFFS(TRUE);
3763 #endif
3764                                 memcpy(buffer,&cache->data[start],nToCopy);
3765
3766 #ifdef CONFIG_YAFFS_WINCE
3767                                 yfsd_LockYAFFS(TRUE);
3768 #endif
3769                                 cache->locked = 0;
3770                         }
3771                         else
3772                         {
3773                                 // Read into the local buffer then copy...
3774                                 
3775                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3776                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);            
3777 #ifdef CONFIG_YAFFS_WINCE
3778                                 yfsd_UnlockYAFFS(TRUE);
3779 #endif
3780                                 memcpy(buffer,&localBuffer[start],nToCopy);
3781
3782 #ifdef CONFIG_YAFFS_WINCE
3783                                 yfsd_LockYAFFS(TRUE);
3784 #endif
3785                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3786                         }
3787
3788                 }
3789                 else
3790                 {
3791 #ifdef CONFIG_YAFFS_WINCE
3792                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3793                         
3794                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3795                         // This is because we otherwise screw up WinCE's memory mapper
3796                         yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3797
3798 #ifdef CONFIG_YAFFS_WINCE
3799                                 yfsd_UnlockYAFFS(TRUE);
3800 #endif
3801                         memcpy(buffer,localBuffer,dev->nBytesPerChunk);
3802
3803 #ifdef CONFIG_YAFFS_WINCE
3804                                 yfsd_LockYAFFS(TRUE);
3805                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3806 #endif
3807
3808 #else
3809                         // A full chunk. Read directly into the supplied buffer.
3810                         yaffs_ReadChunkDataFromObject(in,chunk,buffer);
3811 #endif
3812                 }
3813                 
3814                 n -= nToCopy;
3815                 offset += nToCopy;
3816                 buffer += nToCopy;
3817                 nDone += nToCopy;
3818                 
3819         }
3820         
3821         return nDone;
3822 }
3823
3824
3825
3826 int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes, int writeThrough)
3827 {       
3828         
3829         int chunk;
3830         int start;
3831         int nToCopy;
3832         int n = nBytes;
3833         int nDone = 0;
3834         int nToWriteBack;
3835         int startOfWrite = offset;
3836         int chunkWritten = 0;
3837         int nBytesRead;
3838         
3839         yaffs_Device *dev;
3840         
3841         dev = in->myDev;
3842         
3843         
3844         while(n > 0 && chunkWritten >= 0)
3845         {
3846                 chunk = offset / dev->nBytesPerChunk + 1;
3847                 start = offset % dev->nBytesPerChunk;
3848                 
3849
3850                 // OK now check for the curveball where the start and end are in
3851                 // the same chunk.
3852                 
3853                 if((start + n) < dev->nBytesPerChunk)
3854                 {
3855                         nToCopy = n;
3856                         
3857                         // Now folks, to calculate how many bytes to write back....
3858                         // If we're overwriting and not writing to then end of file then
3859                         // we need to write back as much as was there before.
3860                         
3861                         nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * dev->nBytesPerChunk);
3862                         
3863                         if(nBytesRead > dev->nBytesPerChunk)
3864                         {
3865                                 nBytesRead = dev->nBytesPerChunk;
3866                         }
3867                         
3868                         nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n);
3869                         
3870                 }
3871                 else
3872                 {
3873                         nToCopy = dev->nBytesPerChunk - start;
3874                         nToWriteBack = dev->nBytesPerChunk;
3875                 }
3876         
3877                 if(nToCopy != dev->nBytesPerChunk)
3878                 {
3879                         // An incomplete start or end chunk (or maybe both start and end chunk)
3880                         if(dev->nShortOpCaches > 0)
3881                            {
3882                                 yaffs_ChunkCache *cache;
3883                                 // If we can't find the data in the cache, then load it up.
3884                                 cache = yaffs_FindChunkCache(in,chunk);
3885                                 if(!cache && yaffs_CheckSpaceForAllocation(in->myDev))
3886                                 {
3887                                         cache = yaffs_GrabChunkCache(in->myDev);
3888                                         cache->object = in;
3889                                         cache->chunkId = chunk;
3890                                         cache->dirty = 0;
3891                                         cache->locked = 0;
3892                                         yaffs_ReadChunkDataFromObject(in,chunk,cache->data);            
3893                                 }
3894                         
3895                                 if(cache)
3896                                 {       
3897                                         yaffs_UseChunkCache(dev,cache,1);
3898                                         cache->locked = 1;
3899 #ifdef CONFIG_YAFFS_WINCE
3900                                 yfsd_UnlockYAFFS(TRUE);
3901 #endif
3902         
3903                                         memcpy(&cache->data[start],buffer,nToCopy);
3904
3905 #ifdef CONFIG_YAFFS_WINCE
3906                                 yfsd_LockYAFFS(TRUE);
3907 #endif
3908                                         cache->locked = 0;
3909                                         cache->nBytes = nToWriteBack;
3910                                         
3911                                         if(writeThrough)
3912                                         {
3913                                                 chunkWritten = yaffs_WriteChunkDataToObject(cache->object,
3914                                                                                             cache->chunkId,
3915                                                                                             cache->data,
3916                                                                                             cache->nBytes,1);
3917                                                 cache->dirty = 0;
3918                                         }
3919
3920                                 }
3921                                 else
3922                                 {
3923                                         chunkWritten = -1; // fail the write
3924                                 }
3925                         }
3926                         else
3927                         {
3928                                 // An incomplete start or end chunk (or maybe both start and end chunk)
3929                                 // Read into the local buffer then copy, then copy over and write back.
3930                                 
3931                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3932                 
3933                                 yaffs_ReadChunkDataFromObject(in,chunk,localBuffer);
3934                                 
3935 #ifdef CONFIG_YAFFS_WINCE
3936                                 yfsd_UnlockYAFFS(TRUE);
3937 #endif
3938                                         
3939                                 memcpy(&localBuffer[start],buffer,nToCopy);
3940                         
3941 #ifdef CONFIG_YAFFS_WINCE
3942                                 yfsd_LockYAFFS(TRUE);
3943 #endif
3944                                 chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0);
3945                                 
3946                                 yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3947                         
3948                                 //T(("Write with readback to chunk %d %d  start %d  copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack));
3949                         }
3950                         
3951                 }
3952                 else
3953                 {
3954                         
3955 #ifdef CONFIG_YAFFS_WINCE
3956                         // Under WinCE can't do direct transfer. Need to use a local buffer.
3957                         // This is because we otherwise screw up WinCE's memory mapper
3958                                 __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
3959 #ifdef CONFIG_YAFFS_WINCE
3960                                 yfsd_UnlockYAFFS(TRUE);
3961 #endif
3962                         memcpy(localBuffer,buffer,dev->nBytesPerChunk);
3963 #ifdef CONFIG_YAFFS_WINCE
3964                                 yfsd_LockYAFFS(TRUE);
3965 #endif
3966                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,dev->nBytesPerChunk,0);
3967                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
3968 #else
3969                         // A full chunk. Write directly from the supplied buffer.
3970                         chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,dev->nBytesPerChunk,0);
3971 #endif
3972                         // Since we've overwritten the cached data, we better invalidate it.
3973                         yaffs_InvalidateChunkCache(in,chunk);
3974                         //T(("Write to chunk %d %d\n",chunk,chunkWritten));
3975                 }
3976                 
3977                 if(chunkWritten >= 0)
3978                 {
3979                         n -= nToCopy;
3980                         offset += nToCopy;
3981                         buffer += nToCopy;
3982                         nDone += nToCopy;
3983                 }
3984                 
3985         }
3986         
3987         // Update file object
3988         
3989         if((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
3990         {
3991                 in->variant.fileVariant.fileSize = (startOfWrite + nDone);
3992         }
3993         
3994         in->dirty = 1;
3995         
3996         return nDone;
3997 }
3998
3999 static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
4000 {
4001
4002         yaffs_Device *dev = in->myDev;
4003         int oldFileSize = in->variant.fileVariant.fileSize;
4004
4005         
4006         int lastDel = 1 + (oldFileSize-1)/dev->nBytesPerChunk;
4007                 
4008         int startDel = 1 + (newSize + dev->nBytesPerChunk - 1)/
4009                                                 dev->nBytesPerChunk;
4010         int i;
4011         int chunkId;
4012
4013         // Delete backwards so that we don't end up with holes if
4014         // power is lost part-way through the operation.
4015         for(i = lastDel; i >= startDel; i--)
4016         {
4017                 // NB this could be optimised somewhat,
4018                 // eg. could retrieve the tags and write them without
4019                 // using yaffs_DeleteChunk
4020
4021                 chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL);
4022                 if(chunkId > 0)
4023                 {
4024                         if(chunkId < (dev->internalStartBlock * dev->nChunksPerBlock) || 
4025                        chunkId >= ((dev->internalEndBlock+1) * dev->nChunksPerBlock))
4026                         {
4027                                 T(YAFFS_TRACE_ALWAYS,(TSTR("Found daft chunkId %d for %d"TENDSTR),chunkId,i));
4028                         }
4029                         else
4030                         {
4031                                 in->nDataChunks--;
4032                                 yaffs_DeleteChunk(dev,chunkId,1,__LINE__);
4033                         }
4034                 }
4035         }
4036                 
4037 }
4038
4039 int yaffs_ResizeFile(yaffs_Object *in, int newSize)
4040 {
4041
4042         int oldFileSize = in->variant.fileVariant.fileSize;
4043         int sizeOfPartialChunk;
4044         yaffs_Device *dev = in->myDev;
4045         
4046          sizeOfPartialChunk  = newSize % dev->nBytesPerChunk;
4047                 
4048
4049         yaffs_FlushFilesChunkCache(in); 
4050         yaffs_InvalidateWholeChunkCache(in);
4051
4052         yaffs_CheckGarbageCollection(dev);
4053         
4054         if(in->variantType != YAFFS_OBJECT_TYPE_FILE)
4055         {
4056                 return yaffs_GetFileSize(in);
4057         }
4058         
4059         if(newSize < oldFileSize)
4060         {
4061
4062                 yaffs_PruneResizedChunks(in,newSize);
4063                 
4064                 if(sizeOfPartialChunk != 0)
4065                 {
4066                         int lastChunk = 1+ newSize/dev->nBytesPerChunk;
4067                         __u8 *localBuffer = yaffs_GetTempBuffer(dev,__LINE__);
4068                         
4069                         // Got to read and rewrite the last chunk with its new size and zero pad
4070                         yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer);
4071                         
4072                         memset(localBuffer + sizeOfPartialChunk,0, dev->nBytesPerChunk - sizeOfPartialChunk);
4073                         
4074                         yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfPartialChunk,1);
4075                                 
4076                         yaffs_ReleaseTempBuffer(dev,localBuffer,__LINE__);
4077                 }
4078                 
4079                 in->variant.fileVariant.fileSize = newSize;
4080                 
4081                 yaffs_PruneFileStructure(dev,&in->variant.fileVariant);
4082                 
4083                 // Write a new object header to show we've shrunk the file
4084                 // Do this only if the file is not in the deleted directories.
4085                 if(in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
4086                    in->parent->objectId != YAFFS_OBJECTID_DELETED
4087                   )
4088                 {
4089                         yaffs_UpdateObjectHeader(in,NULL, 0, 1);
4090                 }
4091
4092                 
4093                 return newSize;
4094                 
4095         }
4096         else
4097         {
4098                 return oldFileSize;
4099         }
4100 }
4101
4102
4103 loff_t yaffs_GetFileSize(yaffs_Object *obj)
4104 {
4105         obj = yaffs_GetEquivalentObject(obj);
4106         
4107         switch(obj->variantType)
4108         {
4109                 case YAFFS_OBJECT_TYPE_FILE: 
4110                         return obj->variant.fileVariant.fileSize;
4111                 case YAFFS_OBJECT_TYPE_SYMLINK:
4112                         return yaffs_strlen(obj->variant.symLinkVariant.alias);
4113                 default:
4114                         return 0;
4115         }
4116 }
4117
4118
4119
4120 // yaffs_FlushFile() updates the file's
4121 // objectId in NAND
4122
4123 int yaffs_FlushFile(yaffs_Object *in, int updateTime)
4124 {
4125         int retVal;
4126         if(in->dirty)
4127         {
4128                 //T(("flushing object header\n"));
4129                 
4130                 yaffs_FlushFilesChunkCache(in);
4131                 if(updateTime)
4132                 {
4133 #ifdef CONFIG_YAFFS_WINCE
4134                         yfsd_WinFileTimeNow(in->win_mtime);
4135 #else
4136
4137                         in->yst_mtime = Y_CURRENT_TIME;
4138
4139 #endif
4140                 }
4141
4142                 retVal = (yaffs_UpdateObjectHeader(in,NULL,0,0) >= 0)? YAFFS_OK : YAFFS_FAIL;
4143         }
4144         else
4145         {
4146                 retVal = YAFFS_OK;
4147         }
4148         
4149         return retVal;
4150         
4151 }
4152
4153
4154 static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
4155 {
4156
4157         // First off, invalidate the file's data in the cache, without flushing.
4158         yaffs_InvalidateWholeChunkCache(in);
4159
4160         if(in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir))
4161         {
4162                 // Move to the unlinked directory so we have a record that it was deleted.
4163                 yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4164
4165         }
4166
4167         
4168         yaffs_RemoveObjectFromDirectory(in);
4169         yaffs_DeleteChunk(in->myDev,in->chunkId,1,__LINE__);
4170         in->chunkId = -1;
4171 #if 0
4172 #ifdef __KERNEL__
4173         if(in->myInode)
4174         {
4175                 in->myInode->u.generic_ip = NULL;
4176                 in->myInode = 0;
4177         }
4178 #endif
4179 #endif
4180
4181         yaffs_FreeObject(in);
4182         return YAFFS_OK;
4183
4184 }
4185
4186 // yaffs_DeleteFile deletes the whole file data
4187 // and the inode associated with the file.
4188 // It does not delete the links associated with the file.
4189 static int yaffs_UnlinkFile(yaffs_Object *in)
4190 {
4191
4192 #ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION
4193
4194         // Delete the file data & tnodes
4195
4196          yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL);
4197          
4198
4199         yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4200         
4201         return  yaffs_DoGenericObjectDeletion(in);
4202 #else
4203         int retVal;
4204         int immediateDeletion=0;
4205
4206         if(1)
4207         {
4208                 //in->unlinked = 1;
4209                 //in->myDev->nUnlinkedFiles++;
4210                 //in->renameAllowed = 0;
4211 #ifdef __KERNEL__
4212                 if(!in->myInode)
4213                 {
4214                         immediateDeletion = 1;
4215
4216                 }
4217 #else
4218                 if(in->inUse <= 0)
4219                 {
4220                         immediateDeletion = 1;
4221
4222                 }
4223 #endif
4224                 if(immediateDeletion)
4225                 {
4226                         retVal = yaffs_ChangeObjectName(in, in->myDev->deletedDir,NULL,0);
4227                         T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId));
4228                         in->deleted=1;
4229                         in->myDev->nDeletedFiles++;
4230                         if( 0 && in->myDev->isYaffs2)
4231                         {
4232                                 yaffs_ResizeFile(in,0);
4233                         }
4234                         yaffs_SoftDeleteFile(in);
4235                 }
4236                 else
4237                 {
4238                         retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0);
4239                 }
4240         
4241         }
4242         return retVal;
4243
4244         
4245 #endif
4246 }
4247
4248 int yaffs_DeleteFile(yaffs_Object *in)
4249 {
4250         int retVal = YAFFS_OK;
4251         
4252         if(in->nDataChunks > 0)
4253         {
4254                 // Use soft deletion
4255                 if(!in->unlinked)
4256                 {
4257                         retVal = yaffs_UnlinkFile(in);
4258                 }
4259                 if(retVal == YAFFS_OK && 
4260                 in->unlinked &&
4261                 !in->deleted)
4262                 {
4263                         in->deleted = 1;
4264                         in->myDev->nDeletedFiles++;
4265                         yaffs_SoftDeleteFile(in);
4266                 }
4267                 return in->deleted ? YAFFS_OK : YAFFS_FAIL;     
4268         }
4269         else
4270         {
4271                 // The file has no data chunks so we toss it immediately
4272                 yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top);
4273                 in->variant.fileVariant.top = NULL;
4274                 yaffs_DoGenericObjectDeletion(in);      
4275                 
4276                 return YAFFS_OK;        
4277         }
4278 }
4279
4280 static int yaffs_DeleteDirectory(yaffs_Object *in)
4281 {
4282         //First check that the directory is empty.
4283         if(list_empty(&in->variant.directoryVariant.children))
4284         {
4285                 return  yaffs_DoGenericObjectDeletion(in);
4286         }
4287         
4288         return YAFFS_FAIL;
4289         
4290 }
4291
4292 static int yaffs_DeleteSymLink(yaffs_Object *in)
4293 {
4294         YFREE(in->variant.symLinkVariant.alias);
4295
4296         return  yaffs_DoGenericObjectDeletion(in);
4297 }
4298
4299 static int yaffs_DeleteHardLink(yaffs_Object *in)
4300 {
4301         // remove this hardlink from the list assocaited with the equivalent
4302         // object
4303         list_del(&in->hardLinks);
4304         return  yaffs_DoGenericObjectDeletion(in);      
4305 }
4306
4307
4308 static void yaffs_DestroyObject(yaffs_Object *obj)
4309 {
4310         switch(obj->variantType)
4311         {
4312                 case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break;
4313                 case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break;
4314                 case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break;
4315                 case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break;
4316                 case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break;
4317                 case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen.
4318         }
4319 }
4320
4321
4322 static int yaffs_UnlinkWorker(yaffs_Object *obj)
4323 {
4324
4325         
4326         if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
4327         {
4328                 return  yaffs_DeleteHardLink(obj);
4329         }
4330         else if(!list_empty(&obj->hardLinks))
4331         {       
4332                 // Curve ball: We're unlinking an object that has a hardlink.
4333                 //
4334                 //      This problem arises because we are not strictly following
4335                 //  The Linux link/inode model.
4336                 //
4337                 // We can't really delete the object.
4338                 // Instead, we do the following:
4339                 // - Select a hardlink.
4340                 // - Unhook it from the hard links
4341                 // - Unhook it from its parent directory (so that the rename can work)
4342                 // - Rename the object to the hardlink's name.
4343                 // - Delete the hardlink
4344                 
4345                 
4346                 yaffs_Object *hl;
4347                 int retVal;
4348                 YCHAR name[YAFFS_MAX_NAME_LENGTH+1];
4349                 
4350                 hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks);
4351                 
4352                 list_del_init(&hl->hardLinks);
4353                 list_del_init(&hl->siblings);
4354                 
4355                 yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1);
4356                 
4357                 retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0);
4358                 
4359                 if(retVal == YAFFS_OK)
4360                 {
4361                         retVal = yaffs_DoGenericObjectDeletion(hl);
4362                 }
4363                 return retVal;
4364                                 
4365         }
4366         else
4367         {
4368                 switch(obj->variantType)
4369                 {
4370                         case YAFFS_OBJECT_TYPE_FILE:
4371                                 return yaffs_UnlinkFile(obj);
4372                                 break;
4373                         case YAFFS_OBJECT_TYPE_DIRECTORY:
4374                                 return yaffs_DeleteDirectory(obj);
4375                                 break;
4376                         case YAFFS_OBJECT_TYPE_SYMLINK:
4377                                 return yaffs_DeleteSymLink(obj);
4378                                 break;
4379                         case YAFFS_OBJECT_TYPE_SPECIAL:
4380                                 return yaffs_DoGenericObjectDeletion(obj);
4381                                 break;
4382                         case YAFFS_OBJECT_TYPE_HARDLINK:
4383                         case YAFFS_OBJECT_TYPE_UNKNOWN:
4384                         default:
4385                                 return YAFFS_FAIL;
4386                 }
4387         }
4388 }
4389
4390 int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
4391 {
4392         yaffs_Object *obj;
4393         
4394          obj = yaffs_FindObjectByName(dir,name);
4395          
4396          if(obj && obj->unlinkAllowed)
4397          {
4398                 return yaffs_UnlinkWorker(obj);
4399          }
4400          
4401          return YAFFS_FAIL;
4402         
4403 }
4404
4405 //////////////// Initialisation Scanning /////////////////
4406
4407
4408 #if 0
4409 // For now we use the SmartMedia check.
4410 // We look at the blockStatus byte in the first two chunks
4411 // These must be 0xFF to pass as OK.
4412 // todo: this function needs to be modifyable foir different NAND types
4413 // and different chunk sizes.  Suggest make this into a per-device configurable
4414 // function.
4415 static int yaffs_IsBlockBad(yaffs_Device *dev, int blk)
4416 {
4417         yaffsExtendedTags *tags;
4418         
4419         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&tags,1);
4420 #if 1
4421         if(yaffs_CountBits(spare.blockStatus) < 7)
4422         {
4423                 return 1;
4424         }
4425 #else
4426         if(spare.blockStatus != 0xFF)
4427         {
4428                 return 1;
4429         }
4430 #endif
4431         yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1);
4432
4433 #if 1
4434         if(yaffs_CountBits(spare.blockStatus) < 7)
4435         {
4436                 return 1;
4437         }
4438 #else
4439         if(spare.blockStatus != 0xFF)
4440         {
4441                 return 1;
4442         }
4443 #endif
4444         
4445         return 0;
4446         
4447 }
4448
4449 #endif
4450
4451
4452 typedef struct 
4453 {
4454         int seq;
4455         int block;
4456 } yaffs_BlockIndex;
4457
4458
4459
4460 static int yaffs_Scan(yaffs_Device *dev)
4461 {
4462         yaffs_ExtendedTags tags;
4463         int blk;
4464         int blockIterator;
4465         int startIterator;
4466         int endIterator;
4467         int nBlocksToScan = 0;
4468         
4469         int chunk;
4470         int c;
4471         int deleted;
4472         yaffs_BlockState state;
4473         yaffs_Object *hardList = NULL;
4474         yaffs_Object *hl;
4475         yaffs_BlockInfo *bi;
4476         int sequenceNumber;     
4477         yaffs_ObjectHeader *oh;
4478         yaffs_Object *in;
4479         yaffs_Object *parent;
4480         int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
4481         
4482         __u8 *chunkData;
4483
4484         yaffs_BlockIndex *blockIndex = NULL;
4485
4486         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan starts  intstartblk %d intendblk %d..." TENDSTR),dev->internalStartBlock,dev->internalEndBlock));
4487         
4488         chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4489         
4490         
4491         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4492         
4493         if(dev->isYaffs2)
4494         {
4495                 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));               
4496         }
4497         
4498         
4499         // Scan all the blocks to determine their state
4500         for(blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++)
4501         {
4502                 bi = yaffs_GetBlockInfo(dev,blk);
4503                 yaffs_ClearChunkBits(dev,blk);
4504                 bi->pagesInUse = 0;
4505                 bi->softDeletions = 0;
4506                                 
4507                 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4508                 
4509                 bi->blockState = state;
4510                 bi->sequenceNumber = sequenceNumber;
4511
4512                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block scanning block %d state %d seq %d" TENDSTR),blk,state,sequenceNumber));
4513                 
4514                 if(state == YAFFS_BLOCK_STATE_DEAD)
4515                 {
4516                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4517                 }
4518                 else if(state == YAFFS_BLOCK_STATE_EMPTY)
4519                 {
4520                         T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block empty " TENDSTR)));
4521                         dev->nErasedBlocks++;
4522                         dev->nFreeChunks += dev->nChunksPerBlock;
4523                 }
4524                 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4525                 {
4526                                         
4527                         // Determine the highest sequence number
4528                         if( dev->isYaffs2 &&
4529                             sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
4530                             sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
4531                          {
4532                                 
4533                                 blockIndex[nBlocksToScan].seq = sequenceNumber;
4534                                 blockIndex[nBlocksToScan].block = blk;
4535                                 
4536                                 nBlocksToScan++;
4537
4538                                 if(sequenceNumber >= dev->sequenceNumber)
4539                                 {
4540                                         dev->sequenceNumber = sequenceNumber;
4541                                 }
4542                         }
4543                         else if(dev->isYaffs2)
4544                         {
4545                                 // TODO: Nasty sequence number!
4546                                 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
4547
4548                         }
4549                 }
4550         }
4551         
4552         // Sort the blocks
4553         // Dungy old bubble sort for now...
4554         if(dev->isYaffs2)
4555         {
4556                 yaffs_BlockIndex temp;
4557                 int i;
4558                 int j;
4559                 
4560                 for(i = 0; i < nBlocksToScan; i++)
4561                         for(j = i+1; j < nBlocksToScan; j++)
4562                          if(blockIndex[i].seq > blockIndex[j].seq)
4563                          {
4564                                 temp = blockIndex[j];
4565                                 blockIndex[j] = blockIndex[i];
4566                                 blockIndex[i] = temp;
4567                          }
4568         }
4569         
4570         
4571         // Now scan the blocks looking at the data.
4572         if(dev->isYaffs2)
4573         {
4574                 startIterator = 0;
4575                 endIterator = nBlocksToScan-1;
4576                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("%d blocks to be scanned" TENDSTR),nBlocksToScan));
4577         }
4578         else
4579         {
4580                 startIterator = dev->internalStartBlock;
4581                 endIterator = dev->internalEndBlock;
4582         }
4583         
4584         // For each block....
4585         for(blockIterator = startIterator; blockIterator <= endIterator; blockIterator++)
4586         {
4587         
4588                 if(dev->isYaffs2)
4589                 {
4590                         // get the block to scan in the correct order
4591                         blk = blockIndex[blockIterator].block;
4592                 }
4593                 else
4594                 {
4595                         blk = blockIterator;
4596                 }
4597
4598
4599                 bi = yaffs_GetBlockInfo(dev,blk);
4600                 state = bi->blockState;
4601                 
4602                 deleted = 0;
4603                 
4604                 // For each chunk in each block that needs scanning....
4605                 for(c = 0; c < dev->nChunksPerBlock && 
4606                                    state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++)
4607                 {
4608                         // Read the tags and decide what to do
4609                         chunk = blk * dev->nChunksPerBlock + c;
4610                         
4611                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
4612
4613                         // Let's have a good look at this chunk...
4614         
4615                         
4616                         if(!dev->isYaffs2 && tags.chunkDeleted)
4617                         {
4618                                 // YAFFS1 only...
4619                                 // A deleted chunk
4620                                 deleted++;
4621                                 dev->nFreeChunks ++;
4622                                 //T((" %d %d deleted\n",blk,c));
4623                         }
4624                         else if(!tags.chunkUsed)
4625                         {
4626                                 // An unassigned chunk in the block
4627                                 // This means that either the block is empty or 
4628                                 // this is the one being allocated from
4629                                 
4630                                 if(c == 0)
4631                                 {
4632                                         // We're looking at the first chunk in the block so the block is unused
4633                                         state = YAFFS_BLOCK_STATE_EMPTY;
4634                                         dev->nErasedBlocks++;
4635                                 }
4636                                 else
4637                                 {
4638                                         // this is the block being allocated from
4639                                         T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
4640                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
4641                                         dev->allocationBlock = blk;
4642                                         dev->allocationPage = c;
4643                                         dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
4644                                                                                                           // go forth from here.
4645                                         //Yaffs2 sanity check:
4646                                         // This should be the one with the highest sequence number
4647                                         if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
4648                                         {
4649                                                 T(YAFFS_TRACE_ALWAYS,
4650                                                                 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
4651                                                                 blk,bi->sequenceNumber,dev->sequenceNumber));
4652                                         }
4653                                 }
4654
4655                                 dev->nFreeChunks += (dev->nChunksPerBlock - c);
4656                         }
4657                         else if(tags.chunkId > 0)
4658                         {
4659                                 // chunkId > 0 so it is a data chunk...
4660                                 unsigned int endpos;
4661
4662                                 yaffs_SetChunkBit(dev,blk,c);
4663                                 bi->pagesInUse++;
4664                                                                 
4665                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
4666                                 // PutChunkIntoFile checks for a clash (two data chunks with
4667                                 // the same chunkId).
4668                                 yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1);
4669                                 endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
4670                                 if(in->variantType == YAFFS_OBJECT_TYPE_FILE && in->variant.fileVariant.scannedFileSize <endpos)
4671                                 {
4672                                         in->variant.fileVariant.scannedFileSize = endpos;
4673                                         if(!dev->useHeaderFileSize)
4674                                         {       
4675                                                         in->variant.fileVariant.fileSize = in->variant.fileVariant.scannedFileSize;
4676                                         }
4677
4678                                 }
4679                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
4680                         }
4681                         else
4682                         {
4683                                 // chunkId == 0, so it is an ObjectHeader.
4684                                 // Thus, we read in the object header and make the object
4685                                 yaffs_SetChunkBit(dev,blk,c);
4686                                 bi->pagesInUse++;
4687                                                         
4688                                 yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
4689                                 
4690                                 oh = (yaffs_ObjectHeader *)chunkData;
4691                                 
4692                                 in = yaffs_FindObjectByNumber(dev,tags.objectId);
4693                                 if(in && in->variantType != oh->type)
4694                                 {
4695                                         // This should not happen, but somehow
4696                                         // Wev'e ended up with an objectId that has been reused but not yet 
4697                                         // deleted, and worse still it has changed type. Delete the old object.
4698                                         
4699                                         yaffs_DestroyObject(in);
4700                                         
4701                                         in = 0;
4702                                 }
4703                                 
4704                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
4705                                 
4706                                 if(in->valid)
4707                                 {
4708                                         // We have already filled this one. We have a duplicate and need to resolve it.
4709                                         
4710                                         unsigned existingSerial = in->serial;
4711                                         unsigned newSerial = tags.serialNumber;
4712                                         
4713                                         if( dev->isYaffs2 ||
4714                                             ((existingSerial+1) & 3) == newSerial)
4715                                         {
4716                                                 // Use new one - destroy the exisiting one
4717                                                 yaffs_DeleteChunk(dev,in->chunkId,1,__LINE__);
4718                                                 in->valid = 0;
4719                                         }
4720                                         else
4721                                         {
4722                                                 // Use existing - destroy this one.
4723                                                 yaffs_DeleteChunk(dev,chunk,1,__LINE__);
4724                                         }
4725                                 }
4726                                 
4727                                 if(!in->valid &&
4728                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
4729                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
4730                                 {
4731                                         // We only load some info, don't fiddle with directory structure
4732                                         in->valid = 1;
4733                                         in->variantType = oh->type;
4734         
4735                                         in->yst_mode  = oh->yst_mode;
4736 #ifdef CONFIG_YAFFS_WINCE
4737                                         in->win_atime[0] = oh->win_atime[0];
4738                                         in->win_ctime[0] = oh->win_ctime[0];
4739                                         in->win_mtime[0] = oh->win_mtime[0];
4740                                         in->win_atime[1] = oh->win_atime[1];
4741                                         in->win_ctime[1] = oh->win_ctime[1];
4742                                         in->win_mtime[1] = oh->win_mtime[1];
4743 #else
4744                                         in->yst_uid   = oh->yst_uid;
4745                                         in->yst_gid   = oh->yst_gid;
4746                                         in->yst_atime = oh->yst_atime;
4747                                         in->yst_mtime = oh->yst_mtime;
4748                                         in->yst_ctime = oh->yst_ctime;
4749                                         in->yst_rdev = oh->yst_rdev;
4750 #endif
4751                                         in->chunkId  = chunk;
4752
4753                                 }
4754                                 else if(!in->valid)
4755                                 {
4756                                         // we need to load this info
4757                                 
4758                                         in->valid = 1;
4759                                         in->variantType = oh->type;
4760         
4761                                         in->yst_mode  = oh->yst_mode;
4762 #ifdef CONFIG_YAFFS_WINCE
4763                                         in->win_atime[0] = oh->win_atime[0];
4764                                         in->win_ctime[0] = oh->win_ctime[0];
4765                                         in->win_mtime[0] = oh->win_mtime[0];
4766                                         in->win_atime[1] = oh->win_atime[1];
4767                                         in->win_ctime[1] = oh->win_ctime[1];
4768                                         in->win_mtime[1] = oh->win_mtime[1];
4769 #else
4770                                         in->yst_uid   = oh->yst_uid;
4771                                         in->yst_gid   = oh->yst_gid;
4772                                         in->yst_atime = oh->yst_atime;
4773                                         in->yst_mtime = oh->yst_mtime;
4774                                         in->yst_ctime = oh->yst_ctime;
4775                                         in->yst_rdev = oh->yst_rdev;
4776 #endif
4777                                         in->chunkId  = chunk;
4778
4779                                         yaffs_SetObjectName(in,oh->name);
4780                                         in->dirty = 0;
4781                                                         
4782                                         // directory stuff...
4783                                         // hook up to parent
4784         
4785                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
4786                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
4787                                         {
4788                                                 // Set up as a directory
4789                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
4790                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
4791                                         }
4792                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
4793                                         {
4794                                                 // Hoosterman, another problem....
4795                                                 // We're trying to use a non-directory as a directory
4796
4797                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
4798                                                 parent = dev->lostNFoundDir;
4799                                         }
4800                                 
4801                                         yaffs_AddObjectToDirectory(parent,in);
4802
4803                                         if(0 && (parent == dev->deletedDir ||
4804                                            parent == dev->unlinkedDir))
4805                                         {
4806                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
4807                                                 dev->nDeletedFiles++;
4808                                         }
4809                                 
4810                                         // Note re hardlinks.
4811                                         // Since we might scan a hardlink before its equivalent object is scanned
4812                                         // we put them all in a list.
4813                                         // After scanning is complete, we should have all the objects, so we run through this
4814                                         // list and fix up all the chains.              
4815         
4816                                         switch(in->variantType)
4817                                         {
4818                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
4819                                                         break;
4820                                                 case YAFFS_OBJECT_TYPE_FILE:
4821                                                         if(dev->isYaffs2 && oh->isShrink)
4822                                                         {
4823                                                                 // Prune back the shrunken chunks
4824                                                                 yaffs_PruneResizedChunks(in,oh->fileSize);
4825                                                                 // Mark the block as having a shrinkHeader
4826                                                                 bi->hasShrinkHeader = 1;
4827                                                         }
4828                                                         
4829                                                         if(dev->useHeaderFileSize)
4830                                                 
4831                                                                 in->variant.fileVariant.fileSize = oh->fileSize;
4832                                                                 
4833                                                         break;
4834                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
4835                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
4836                                                         in->hardLinks.next = (struct list_head *)hardList;
4837                                                         hardList = in;
4838                                                         break;
4839                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
4840                                                         break;
4841                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
4842                                                         break;
4843                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
4844                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
4845                                                         break;
4846                                         }
4847
4848                                         if(parent == dev->deletedDir)
4849                                         {
4850                                                 yaffs_DestroyObject(in);
4851                                                 bi->hasShrinkHeader = 1;
4852                                         }
4853                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
4854                                 }
4855                         }
4856                 }
4857                 
4858                 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
4859                 {
4860                         // If we got this far while scanning, then the block is fully allocated.
4861                         state = YAFFS_BLOCK_STATE_FULL; 
4862                 }
4863                 
4864                 bi->blockState = state;
4865                 
4866                 // Now let's see if it was dirty
4867                 if(     bi->pagesInUse == 0 &&
4868                         !bi->hasShrinkHeader &&
4869                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
4870             {
4871                 yaffs_BlockBecameDirty(dev,blk);
4872             }
4873
4874         }
4875         
4876         if(blockIndex)
4877         {
4878                 YFREE(blockIndex);
4879         }
4880         
4881         // Ok, we've done all the scanning.
4882         
4883         // Fix up the hard link chains.
4884         // We should now have scanned all the objects, now it's time to add these 
4885         // hardlinks.
4886         while(hardList)
4887         {
4888                 hl = hardList;
4889                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
4890                 
4891                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
4892                 
4893                 if(in)
4894                 {
4895                         // Add the hardlink pointers
4896                         hl->variant.hardLinkVariant.equivalentObject=in;
4897                         list_add(&hl->hardLinks,&in->hardLinks);
4898                 }
4899                 else
4900                 {
4901                         //Todo Need to report/handle this better.
4902                         // Got a problem... hardlink to a non-existant object
4903                         hl->variant.hardLinkVariant.equivalentObject=NULL;
4904                         INIT_LIST_HEAD(&hl->hardLinks);
4905                         
4906                 }
4907                 
4908         }
4909         
4910         {
4911                 struct list_head *i;    
4912                 struct list_head *n;
4913                         
4914                 yaffs_Object *l;
4915                 // Soft delete all the unlinked files
4916                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
4917                 {
4918                         if(i)
4919                         {
4920                                 l = list_entry(i, yaffs_Object,siblings);
4921                                 yaffs_DestroyObject(l);         
4922                         }
4923                 }       
4924         }
4925         
4926         yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
4927
4928         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_Scan ends" TENDSTR)));
4929
4930         return YAFFS_OK;
4931 }
4932
4933
4934 static int yaffs_ScanBackwards(yaffs_Device *dev)
4935 {
4936         yaffs_ExtendedTags tags;
4937         int blk;
4938         int blockIterator;
4939         int startIterator;
4940         int endIterator;
4941         int nBlocksToScan = 0;
4942         
4943         int chunk;
4944         int c;
4945         int deleted;
4946         yaffs_BlockState state;
4947         yaffs_Object *hardList = NULL;
4948         yaffs_Object *hl;
4949         yaffs_BlockInfo *bi;
4950         int sequenceNumber;     
4951         yaffs_ObjectHeader *oh;
4952         yaffs_Object *in;
4953         yaffs_Object *parent;
4954         int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
4955         
4956         __u8 *chunkData;
4957
4958         yaffs_BlockIndex *blockIndex = NULL;
4959
4960
4961         if(!dev->isYaffs2)
4962         {
4963                 T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
4964                 return YAFFS_FAIL;
4965         }
4966         
4967         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards starts  intstartblk %d intendblk %d..." TENDSTR),dev->internalStartBlock,dev->internalEndBlock));
4968                 
4969         chunkData = yaffs_GetTempBuffer(dev,__LINE__);
4970         
4971         
4972         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
4973         
4974         if(dev->isYaffs2)
4975         {
4976                 blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));               
4977         }
4978         
4979         
4980         // Scan all the blocks to determine their state
4981         for(blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++)
4982         {
4983                 bi = yaffs_GetBlockInfo(dev,blk);
4984                 yaffs_ClearChunkBits(dev,blk);
4985                 bi->pagesInUse = 0;
4986                 bi->softDeletions = 0;
4987                                 
4988                 yaffs_QueryInitialBlockState(dev,blk,&state,&sequenceNumber);
4989                 
4990                 bi->blockState = state;
4991                 bi->sequenceNumber = sequenceNumber;
4992
4993                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block scanning block %d state %d seq %d" TENDSTR),blk,state,sequenceNumber));
4994                 
4995                 if(state == YAFFS_BLOCK_STATE_DEAD)
4996                 {
4997                         T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk));
4998                 }
4999                 else if(state == YAFFS_BLOCK_STATE_EMPTY)
5000                 {
5001                         T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("Block empty " TENDSTR)));
5002                         dev->nErasedBlocks++;
5003                         dev->nFreeChunks += dev->nChunksPerBlock;
5004                 }
5005                 else if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5006                 {
5007                                         
5008                         // Determine the highest sequence number
5009                         if( dev->isYaffs2 &&
5010                             sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
5011                             sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER)
5012                          {
5013                                 
5014                                 blockIndex[nBlocksToScan].seq = sequenceNumber;
5015                                 blockIndex[nBlocksToScan].block = blk;
5016                                 
5017                                 nBlocksToScan++;
5018
5019                                 if(sequenceNumber >= dev->sequenceNumber)
5020                                 {
5021                                         dev->sequenceNumber = sequenceNumber;
5022                                 }
5023                         }
5024                         else if(dev->isYaffs2)
5025                         {
5026                                 // TODO: Nasty sequence number!
5027                                 T(YAFFS_TRACE_SCAN,(TSTR("Block scanning block %d has bad sequence number %d" TENDSTR),blk,sequenceNumber));
5028
5029                         }
5030                 }
5031         }
5032         
5033         // Sort the blocks
5034         // Dungy old bubble sort for now...
5035         if(dev->isYaffs2)
5036         {
5037                 yaffs_BlockIndex temp;
5038                 int i;
5039                 int j;
5040                 
5041                 for(i = 0; i < nBlocksToScan; i++)
5042                         for(j = i+1; j < nBlocksToScan; j++)
5043                          if(blockIndex[i].seq > blockIndex[j].seq)
5044                          {
5045                                 temp = blockIndex[j];
5046                                 blockIndex[j] = blockIndex[i];
5047                                 blockIndex[i] = temp;
5048                          }
5049         }
5050         
5051         
5052         // Now scan the blocks looking at the data.
5053         if(dev->isYaffs2)
5054         {
5055                 startIterator = 0;
5056                 endIterator = nBlocksToScan-1;
5057                 T(YAFFS_TRACE_SCAN_DEBUG,(TSTR("%d blocks to be scanned" TENDSTR),nBlocksToScan));
5058         }
5059
5060         
5061         // For each block.... backwards
5062         for(blockIterator = endIterator; blockIterator >= startIterator; blockIterator--)
5063         {
5064         
5065                 // get the block to scan in the correct order
5066                 blk = blockIndex[blockIterator].block;
5067
5068
5069                 bi = yaffs_GetBlockInfo(dev,blk);
5070                 state = bi->blockState;
5071                 
5072                 deleted = 0;
5073                 
5074                 if( 0 && // Disable since this is redundant.
5075                     state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5076                 {
5077                         // Let's look at the first chunk in the block
5078                         chunk = blk * dev->nChunksPerBlock;
5079                         
5080                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
5081
5082                         // Let's have a good look at this chunk...
5083         
5084                         if(!tags.chunkUsed)
5085                         {
5086                                 // An unassigned chunk in the block
5087                                 // This means that either the block is empty or 
5088                                 // this is the one being allocated from
5089                                 
5090                                 // We're looking at the first chunk in the block so the block is unused
5091                                 state = YAFFS_BLOCK_STATE_EMPTY;
5092                                 dev->nErasedBlocks++;
5093                                 dev->nFreeChunks += dev->nChunksPerBlock;
5094                         }
5095                 
5096                 }
5097                 
5098                 // For each chunk in each block that needs scanning....
5099                 for(c = dev->nChunksPerBlock-1; c >= 0 && 
5100                                   (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
5101                                    state == YAFFS_BLOCK_STATE_ALLOCATING); c--)
5102                 {
5103                         // Scan backwards... 
5104                         // Read the tags and decide what to do
5105                         chunk = blk * dev->nChunksPerBlock + c;
5106                         
5107                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,NULL,&tags);
5108
5109                         // Let's have a good look at this chunk...
5110         
5111                         if(!tags.chunkUsed)
5112                         {
5113                                 // An unassigned chunk in the block
5114                                 // This means that either the block is empty or 
5115                                 // this is the one being allocated from
5116                                 
5117                                 if(c == 0)
5118                                 {
5119                                         // We're looking at the first chunk in the block so the block is unused
5120                                         state = YAFFS_BLOCK_STATE_EMPTY;
5121                                         dev->nErasedBlocks++;
5122                                 }
5123                                 else
5124                                 {
5125                                         // this is the block being allocated from
5126                                         if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5127                                         {
5128                                           T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c));
5129                                         }
5130                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
5131                                         dev->allocationBlock = blk;
5132                                         dev->allocationPage = c;
5133                                         dev->allocationBlockFinder = blk; // Set it to here to encourage the allocator to
5134                                                                                                           // go forth from here.
5135                                         //Yaffs2 sanity check:
5136                                         // This should be the one with the highest sequence number
5137                                         if(dev->isYaffs2 && (dev->sequenceNumber != bi->sequenceNumber))
5138                                         {
5139                                                 T(YAFFS_TRACE_ALWAYS,
5140                                                                 (TSTR("yaffs: Allocation block %d was not highest sequence id: block seq = %d, dev seq = %d" TENDSTR),
5141                                                                 blk,bi->sequenceNumber,dev->sequenceNumber));
5142                                         }
5143                                 }
5144
5145                                 dev->nFreeChunks ++;
5146                         }
5147                         else if(tags.chunkId > 0)
5148                         {
5149                                 // chunkId > 0 so it is a data chunk...
5150                                 unsigned int endpos;
5151                                 
5152                                 __u32 chunkBase = (tags.chunkId - 1)* dev->nBytesPerChunk;
5153
5154                                 yaffs_SetChunkBit(dev,blk,c);
5155                                 bi->pagesInUse++;
5156                                                                 
5157                                 in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE);
5158                                 if(in->variantType == YAFFS_OBJECT_TYPE_FILE &&
5159                                    chunkBase < in->variant.fileVariant.shrinkSize)
5160                                 {
5161                                         // This has not been invalidated by a resize
5162                                         yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,-1);
5163                                 
5164                                 
5165                                         // File size is calculated by looking at the data chunks if we have not 
5166                                         // seen an object header yet. Stop this practice once we find an object header.
5167                                         endpos = (tags.chunkId - 1)* dev->nBytesPerChunk + tags.byteCount;
5168                                         if(!in->valid && // have not got an object header yet
5169                                            in->variant.fileVariant.scannedFileSize <endpos)
5170                                         {
5171                                                 in->variant.fileVariant.scannedFileSize = endpos;
5172                                                 in->variant.fileVariant.fileSize = in->variant.fileVariant.scannedFileSize;
5173                                         }
5174
5175                                 }
5176                                 else
5177                                 {
5178                                         // This chunk has been invalidated by a resize, so delete
5179                                         yaffs_DeleteChunk(dev,chunk,1,__LINE__);
5180                                         
5181                                         
5182                                 }
5183                                 //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));  
5184                         }
5185                         else
5186                         {
5187                                 // chunkId == 0, so it is an ObjectHeader.
5188                                 // Thus, we read in the object header and make the object
5189                                 yaffs_SetChunkBit(dev,blk,c);
5190                                 bi->pagesInUse++;
5191                                 
5192                                 oh = NULL;
5193                                 in = NULL;
5194                                 
5195                                 if(tags.extraHeaderInfoAvailable)
5196                                 {
5197                                         in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,tags.extraObjectType);
5198                                 }
5199
5200                                 
5201                                 if(!in || !in->valid)
5202                                 {
5203                                 
5204                                         // If we don't have  valid info then we need to read the chunk
5205                                         // TODO In future we can probably defer reading the chunk and 
5206                                         // living with invalid data until needed.
5207                                                                 
5208                                         yaffs_ReadChunkWithTagsFromNAND(dev,chunk,chunkData,NULL);
5209                                 
5210                                         oh = (yaffs_ObjectHeader *)chunkData;
5211                                 
5212                                         if(!in)
5213                                            in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type);
5214                                         
5215                                 }
5216                                 
5217                                 if(!in)
5218                                 {
5219                                         // TODO Hoosterman we have a problem!
5220                                         T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: Could not make object for object  %d  at chunk %d during scan" TENDSTR),tags.objectId,chunk));
5221
5222                                 }
5223                                 
5224                                 if(in->valid)
5225                                 {
5226                                         // We have already filled this one. We have a duplicate that will be discarded, but 
5227                                         // we first have to suck out resize info if it is a file.
5228                                         
5229                                         if( (in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
5230                                             ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
5231                                             (tags.extraHeaderInfoAvailable && tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
5232                                            )
5233                                         {
5234                                                 __u32 thisSize = (oh) ? oh->fileSize : tags.extraFileLength;
5235                                                 __u32 parentObjectId = (oh) ? oh->parentObjectId : tags.extraParentObjectId;
5236                                                 unsigned isShrink = (oh)  ? oh->isShrink : tags.extraIsShrinkHeader;
5237                                                 
5238                                                 // If it is deleted (unlinked at start also means deleted)
5239                                                 // we treat the file size as being zeroed at this point.
5240                                                 if(parentObjectId == YAFFS_OBJECTID_DELETED ||
5241                                                    parentObjectId == YAFFS_OBJECTID_UNLINKED)
5242                                                 {
5243                                                         thisSize = 0;
5244                                                         isShrink = 1;
5245                                                 }
5246                                                                                                 
5247                                                 if(in->variant.fileVariant.shrinkSize > thisSize)
5248                                                 {
5249                                                         in->variant.fileVariant.shrinkSize = thisSize;
5250                                                 }
5251                                                 
5252                                                 if(isShrink)
5253                                                 {
5254                                                         bi->hasShrinkHeader = 1;
5255                                                 }
5256                                                 
5257                                         }
5258                                         // Use existing - destroy this one.
5259                                         yaffs_DeleteChunk(dev,chunk,1,__LINE__);
5260                 
5261                                 }
5262                                 
5263                                 if(!in->valid &&
5264                                    (tags.objectId == YAFFS_OBJECTID_ROOT ||
5265                                     tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
5266                                 {
5267                                         // We only load some info, don't fiddle with directory structure
5268                                         in->valid = 1;
5269                                         in->variantType = oh->type;
5270         
5271                                         in->yst_mode  = oh->yst_mode;
5272 #ifdef CONFIG_YAFFS_WINCE
5273                                         in->win_atime[0] = oh->win_atime[0];
5274                                         in->win_ctime[0] = oh->win_ctime[0];
5275                                         in->win_mtime[0] = oh->win_mtime[0];
5276                                         in->win_atime[1] = oh->win_atime[1];
5277                                         in->win_ctime[1] = oh->win_ctime[1];
5278                                         in->win_mtime[1] = oh->win_mtime[1];
5279 #else
5280                                         in->yst_uid   = oh->yst_uid;
5281                                         in->yst_gid   = oh->yst_gid;
5282                                         in->yst_atime = oh->yst_atime;
5283                                         in->yst_mtime = oh->yst_mtime;
5284                                         in->yst_ctime = oh->yst_ctime;
5285                                         in->yst_rdev = oh->yst_rdev;
5286 #endif
5287                                         in->chunkId  = chunk;
5288
5289                                 }
5290                                 else if(!in->valid)
5291                                 {
5292                                         // we need to load this info
5293                                 
5294                                         in->valid = 1;
5295                                         in->variantType = oh->type;
5296         
5297                                         in->yst_mode  = oh->yst_mode;
5298 #ifdef CONFIG_YAFFS_WINCE
5299                                         in->win_atime[0] = oh->win_atime[0];
5300                                         in->win_ctime[0] = oh->win_ctime[0];
5301                                         in->win_mtime[0] = oh->win_mtime[0];
5302                                         in->win_atime[1] = oh->win_atime[1];
5303                                         in->win_ctime[1] = oh->win_ctime[1];
5304                                         in->win_mtime[1] = oh->win_mtime[1];
5305 #else
5306                                         in->yst_uid   = oh->yst_uid;
5307                                         in->yst_gid   = oh->yst_gid;
5308                                         in->yst_atime = oh->yst_atime;
5309                                         in->yst_mtime = oh->yst_mtime;
5310                                         in->yst_ctime = oh->yst_ctime;
5311                                         in->yst_rdev = oh->yst_rdev;
5312 #endif
5313                                         in->chunkId  = chunk;
5314
5315                                         yaffs_SetObjectName(in,oh->name);
5316                                         in->dirty = 0;
5317                                                         
5318                                         // directory stuff...
5319                                         // hook up to parent
5320         
5321                                         parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY);
5322                                         if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN)
5323                                         {
5324                                                 // Set up as a directory
5325                                                 parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY;
5326                                                 INIT_LIST_HEAD(&parent->variant.directoryVariant.children);
5327                                         }
5328                                         else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5329                                         {
5330                                                 // Hoosterman, another problem....
5331                                                 // We're trying to use a non-directory as a directory
5332
5333                                                 T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR)));
5334                                                 parent = dev->lostNFoundDir;
5335                                         }
5336                                 
5337                                         yaffs_AddObjectToDirectory(parent,in);
5338
5339                                         if((parent == dev->deletedDir ||
5340                                            parent == dev->unlinkedDir))
5341                                         {
5342                                                 in->deleted = 1; // If it is unlinked at start up then it wants deleting
5343                                         }
5344                                         
5345                                         if( oh->isShrink)
5346                                         {
5347                                                 // Mark the block as having a shrinkHeader
5348                                                 bi->hasShrinkHeader = 1;
5349                                         }
5350                                         
5351                                 
5352                                         // Note re hardlinks.
5353                                         // Since we might scan a hardlink before its equivalent object is scanned
5354                                         // we put them all in a list.
5355                                         // After scanning is complete, we should have all the objects, so we run through this
5356                                         // list and fix up all the chains.              
5357         
5358                                         switch(in->variantType)
5359                                         {
5360                                                 case YAFFS_OBJECT_TYPE_UNKNOWN:         // Todo got a problem
5361                                                         break;
5362                                                 case YAFFS_OBJECT_TYPE_FILE:
5363
5364                                                         
5365                                                         if(in->variant.fileVariant.scannedFileSize < oh->fileSize)
5366                                                         {
5367                                                                 in->variant.fileVariant.fileSize = oh->fileSize;
5368                                                                 in->variant.fileVariant.scannedFileSize = in->variant.fileVariant.fileSize;
5369                                                         }
5370                                                         
5371                                                                                                         
5372                                                         
5373                                                         if(in->variant.fileVariant.shrinkSize > oh->fileSize)
5374                                                         {
5375                                                                 in->variant.fileVariant.shrinkSize = oh->fileSize;
5376                                                         }
5377                                                                                                         
5378                                                                 
5379                                                         break;
5380                                                 case YAFFS_OBJECT_TYPE_HARDLINK:
5381                                                         in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId;
5382                                                         in->hardLinks.next = (struct list_head *)hardList;
5383                                                         hardList = in;
5384                                                         break;
5385                                                 case YAFFS_OBJECT_TYPE_DIRECTORY:       // Do nothing
5386                                                         break;
5387                                                 case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing
5388                                                         break;
5389                                                 case YAFFS_OBJECT_TYPE_SYMLINK:         // Do nothing
5390                                                         in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias);
5391                                                         break;
5392                                         }
5393
5394 #if 0
5395                                         if(parent == dev->deletedDir)
5396                                         {
5397                                                 yaffs_DestroyObject(in);
5398                                                 bi->hasShrinkHeader = 1;
5399                                         }
5400 #endif
5401                                         //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType));        
5402                                 }
5403                         }
5404                 }
5405                 
5406                 if(state == YAFFS_BLOCK_STATE_NEEDS_SCANNING)
5407                 {
5408                         // If we got this far while scanning, then the block is fully allocated.
5409                         state = YAFFS_BLOCK_STATE_FULL; 
5410                 }
5411                 
5412                 bi->blockState = state;
5413                 
5414                 // Now let's see if it was dirty
5415                 if(     bi->pagesInUse == 0 &&
5416                         !bi->hasShrinkHeader &&
5417                 bi->blockState == YAFFS_BLOCK_STATE_FULL)
5418             {
5419                 yaffs_BlockBecameDirty(dev,blk);
5420             }
5421
5422         }
5423         
5424         if(blockIndex)
5425         {
5426                 YFREE(blockIndex);
5427         }
5428         
5429         // Ok, we've done all the scanning.
5430         
5431         // Fix up the hard link chains.
5432         // We should now have scanned all the objects, now it's time to add these 
5433         // hardlinks.
5434         while(hardList)
5435         {
5436                 hl = hardList;
5437                 hardList = (yaffs_Object *)(hardList->hardLinks.next);
5438                 
5439                 in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId);
5440                 
5441                 if(in)
5442                 {
5443                         // Add the hardlink pointers
5444                         hl->variant.hardLinkVariant.equivalentObject=in;
5445                         list_add(&hl->hardLinks,&in->hardLinks);
5446                 }
5447                 else
5448                 {
5449                         //Todo Need to report/handle this better.
5450                         // Got a problem... hardlink to a non-existant object
5451                         hl->variant.hardLinkVariant.equivalentObject=NULL;
5452                         INIT_LIST_HEAD(&hl->hardLinks);
5453                         
5454                 }
5455                 
5456         }
5457         
5458         {
5459                 struct list_head *i;    
5460                 struct list_head *n;
5461                         
5462                 yaffs_Object *l;
5463                 
5464                 // Soft delete all the unlinked files
5465                 list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children)
5466                 {
5467                         if(i)
5468                         {
5469                                 l = list_entry(i, yaffs_Object,siblings);
5470                                 yaffs_DestroyObject(l);         
5471                         }
5472                 }       
5473                 
5474                 // Soft delete all the deletedDir files
5475                 list_for_each_safe(i,n,&dev->deletedDir->variant.directoryVariant.children)
5476                 {
5477                         if(i)
5478                         {
5479                                 l = list_entry(i, yaffs_Object,siblings);
5480                                 yaffs_DestroyObject(l);         
5481                                 
5482                         }
5483                 }       
5484         }
5485         
5486         yaffs_ReleaseTempBuffer(dev,chunkData,__LINE__);
5487
5488         T(YAFFS_TRACE_SCAN,(TSTR("yaffs_ScanBackwards ends" TENDSTR)));
5489
5490         return YAFFS_OK;
5491 }
5492
5493
5494
5495 ////////////////////////// Directory Functions /////////////////////////
5496
5497
5498 static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj)
5499 {
5500
5501         if(!directory)
5502         {
5503                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: Trying to add an object to a null pointer directory" TENDSTR)));
5504                 YBUG();
5505         }
5506         if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5507         {
5508                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: Trying to add an object to a non-directory" TENDSTR)));
5509                 YBUG();
5510         }
5511
5512                 if(obj->siblings.prev == NULL)
5513         {
5514                 // Not initialised
5515                 INIT_LIST_HEAD(&obj->siblings);
5516                 
5517         }
5518         else if(!list_empty(&obj->siblings))
5519         {
5520                 // If it is holed up somewhere else, un hook it
5521                 list_del_init(&obj->siblings);
5522         }
5523         // Now add it
5524         list_add(&obj->siblings,&directory->variant.directoryVariant.children);
5525         obj->parent = directory;
5526         
5527         if(directory == obj->myDev->unlinkedDir || directory == obj->myDev->deletedDir)
5528         {
5529                 obj->unlinked = 1;
5530                 obj->myDev->nUnlinkedFiles++;
5531                 obj->renameAllowed = 0;
5532         }
5533 }
5534
5535 static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
5536 {
5537         list_del_init(&obj->siblings);
5538         obj->parent = NULL;
5539 }
5540
5541 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const YCHAR *name)
5542 {
5543         int sum;
5544         
5545         struct list_head *i;
5546         YCHAR buffer[YAFFS_MAX_NAME_LENGTH+1];
5547         
5548         yaffs_Object *l;
5549         
5550
5551         if(!directory)
5552         {
5553                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: null pointer directory"TENDSTR)));
5554                 YBUG();
5555         }
5556         if(directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5557         {
5558                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: non-directory"TENDSTR)));
5559                 YBUG();
5560         }
5561
5562         sum = yaffs_CalcNameSum(name);
5563         
5564         list_for_each(i,&directory->variant.directoryVariant.children)
5565         {
5566                 if(i)
5567                 {
5568                         l = list_entry(i, yaffs_Object,siblings);
5569                 
5570                         // Special case for lost-n-found
5571                         if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5572                         {
5573                                 if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0)
5574                                 {
5575                                         return l;
5576                                 }
5577                         }
5578                         else if(yaffs_SumCompare(l->sum, sum)||
5579                                     l->chunkId <= 0) //LostnFound cunk called Objxxx
5580                         {
5581                                 // Do a real check
5582                                 yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH);
5583                                 if(yaffs_strcmp(name,buffer) == 0)
5584                                 {
5585                                         return l;
5586                                 }
5587                         
5588                         }                       
5589                 }
5590         }
5591         
5592         return NULL;
5593 }
5594
5595
5596 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *))
5597 {
5598         struct list_head *i;    
5599         yaffs_Object *l;
5600         
5601
5602         if(!theDir)
5603         {
5604                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: null pointer directory"TENDSTR)));
5605                 YBUG();
5606         }
5607         if(theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
5608         {
5609                 T(YAFFS_TRACE_ALWAYS,(TSTR("tragedy: yaffs_FindObjectByName: non-directory"TENDSTR)));
5610                 YBUG();
5611         }
5612         
5613         list_for_each(i,&theDir->variant.directoryVariant.children)
5614         {
5615                 if(i)
5616                 {
5617                         l = list_entry(i, yaffs_Object,siblings);
5618                         if(l && !fn(l))
5619                         {
5620                                 return YAFFS_FAIL;
5621                         }
5622                 }
5623         }
5624         
5625         return YAFFS_OK;
5626
5627 }
5628
5629
5630 // GetEquivalentObject dereferences any hard links to get to the
5631 // actual object.
5632
5633 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
5634 {
5635         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
5636         {
5637                 // We want the object id of the equivalent object, not this one
5638                 obj = obj->variant.hardLinkVariant.equivalentObject;
5639         }
5640         return obj;
5641
5642 }
5643
5644 int yaffs_GetObjectName(yaffs_Object *obj,YCHAR *name,int buffSize)
5645 {
5646         memset(name,0,buffSize * sizeof(YCHAR));
5647         
5648         if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND)
5649         {
5650                 yaffs_strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1);
5651         }
5652         else if(obj->chunkId <= 0)
5653         {
5654                 YCHAR locName[20];
5655                 // make up a name
5656                 yaffs_sprintf(locName,_Y("%s%d"),YAFFS_LOSTNFOUND_PREFIX,obj->objectId);
5657                 yaffs_strncpy(name,locName,buffSize - 1);
5658
5659         }
5660 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5661         else if(obj->shortName[0])
5662         {
5663                 yaffs_strcpy(name,obj->shortName);
5664         }
5665 #endif
5666         else
5667         {
5668                 __u8 *buffer = yaffs_GetTempBuffer(obj->myDev,__LINE__);
5669                 
5670                 yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5671
5672                 memset(buffer,0,obj->myDev->nBytesPerChunk);
5673         
5674                 if(obj->chunkId >= 0)
5675                 {
5676                         yaffs_ReadChunkWithTagsFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5677                 }
5678                 yaffs_strncpy(name,oh->name,buffSize - 1);
5679
5680                 yaffs_ReleaseTempBuffer(obj->myDev,buffer,__LINE__);
5681         }
5682         
5683         return yaffs_strlen(name);
5684 }
5685
5686 int yaffs_GetObjectFileLength(yaffs_Object *obj)
5687 {
5688         
5689         // Dereference any hard linking
5690         obj = yaffs_GetEquivalentObject(obj);
5691         
5692         if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
5693         {
5694                 return obj->variant.fileVariant.fileSize;
5695         }
5696         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5697         {
5698                 return yaffs_strlen(obj->variant.symLinkVariant.alias);
5699         }
5700         else
5701         {
5702                 // Only a directory should drop through to here
5703                 return obj->myDev->nBytesPerChunk;
5704         }       
5705 }
5706
5707 int yaffs_GetObjectLinkCount(yaffs_Object *obj)
5708 {
5709         int count = 0; 
5710         struct list_head *i;
5711         
5712         if(!obj->unlinked)
5713         {
5714                 count++;        // the object itself
5715         }
5716         list_for_each(i,&obj->hardLinks)
5717         {
5718                 count++;        // add the hard links;
5719         }
5720         return count;
5721         
5722 }
5723
5724
5725 int yaffs_GetObjectInode(yaffs_Object *obj)
5726 {
5727         obj = yaffs_GetEquivalentObject(obj);
5728         
5729         return obj->objectId;
5730 }
5731
5732 unsigned yaffs_GetObjectType(yaffs_Object *obj)
5733 {
5734         obj = yaffs_GetEquivalentObject(obj);
5735         
5736         switch(obj->variantType)
5737         {
5738                 case YAFFS_OBJECT_TYPE_FILE:            return DT_REG; break;
5739                 case YAFFS_OBJECT_TYPE_DIRECTORY:       return DT_DIR; break;
5740                 case YAFFS_OBJECT_TYPE_SYMLINK:         return DT_LNK; break;
5741                 case YAFFS_OBJECT_TYPE_HARDLINK:        return DT_REG; break;
5742                 case YAFFS_OBJECT_TYPE_SPECIAL:         
5743                         if(S_ISFIFO(obj->yst_mode)) return DT_FIFO;
5744                         if(S_ISCHR(obj->yst_mode)) return DT_CHR;
5745                         if(S_ISBLK(obj->yst_mode)) return DT_BLK;
5746                         if(S_ISSOCK(obj->yst_mode)) return DT_SOCK;
5747                 default: return DT_REG; break;
5748         }
5749 }
5750
5751 YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
5752 {
5753         obj = yaffs_GetEquivalentObject(obj);
5754         if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
5755         {
5756                 return yaffs_CloneString(obj->variant.symLinkVariant.alias);
5757         }
5758         else
5759         {
5760                 return yaffs_CloneString(_Y(""));
5761         }
5762 }
5763
5764 #ifndef CONFIG_YAFFS_WINCE
5765
5766 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
5767 {
5768         unsigned int valid = attr->ia_valid;
5769         
5770         if(valid & ATTR_MODE) obj->yst_mode = attr->ia_mode;
5771         if(valid & ATTR_UID) obj->yst_uid = attr->ia_uid;
5772         if(valid & ATTR_GID) obj->yst_gid = attr->ia_gid;
5773         
5774         if(valid & ATTR_ATIME) obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
5775         if(valid & ATTR_CTIME) obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
5776         if(valid & ATTR_MTIME) obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
5777         
5778         if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size);
5779         
5780         yaffs_UpdateObjectHeader(obj,NULL,1,0);
5781         
5782         return YAFFS_OK;
5783         
5784 }
5785 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
5786 {
5787         unsigned int valid = 0;
5788         
5789         attr->ia_mode = obj->yst_mode;  valid |= ATTR_MODE;
5790         attr->ia_uid = obj->yst_uid;            valid |= ATTR_UID;
5791         attr->ia_gid = obj->yst_gid;            valid |= ATTR_GID;
5792         
5793         Y_TIME_CONVERT(attr->ia_atime)= obj->yst_atime; valid |= ATTR_ATIME;
5794         Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;        valid |= ATTR_CTIME;
5795         Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;        valid |= ATTR_MTIME;
5796
5797         attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE;
5798         
5799         attr->ia_valid = valid;
5800         
5801         return YAFFS_OK;
5802         
5803 }
5804
5805 #endif
5806
5807 int yaffs_DumpObject(yaffs_Object *obj)
5808 {
5809 //      __u8 buffer[YAFFS_BYTES_PER_CHUNK];
5810         YCHAR name[257];
5811 //      yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer;
5812
5813 //      memset(buffer,0,YAFFS_BYTES_PER_CHUNK);
5814         
5815 //      if(obj->chunkId >= 0)
5816 //      {
5817 //              yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL);
5818 //      }
5819         
5820         yaffs_GetObjectName(obj,name,256);
5821         
5822         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),
5823                         obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial, 
5824                         obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
5825
5826 #if 0
5827         YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n",
5828                         obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial, 
5829                         obj->sum, obj->chunkId));
5830                 switch(obj->variantType)
5831         {
5832                 case YAFFS_OBJECT_TYPE_FILE: 
5833                         YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize));
5834                         break;
5835                 case YAFFS_OBJECT_TYPE_DIRECTORY:
5836                         YPRINTF((" DIRECTORY\n"));
5837                         break;
5838                 case YAFFS_OBJECT_TYPE_HARDLINK: //todo
5839                 case YAFFS_OBJECT_TYPE_SYMLINK:
5840                 case YAFFS_OBJECT_TYPE_UNKNOWN:
5841                 default:
5842         }
5843 #endif
5844         
5845         return YAFFS_OK;
5846 }
5847
5848
5849 ///////////////////////// Initialisation code ///////////////////////////
5850
5851
5852 int yaffs_CheckDevFunctions(const yaffs_Device *dev)
5853 {
5854
5855         // Common functions, gotta have
5856         if(!dev->eraseBlockInNAND ||
5857            !dev->initialiseNAND) return 0;
5858
5859 #ifdef CONFIG_YAFFS_YAFFS2
5860
5861         // Can use the "with tags" style interface for yaffs1 or yaffs2
5862         if(dev->writeChunkWithTagsToNAND &&
5863            dev->readChunkWithTagsFromNAND &&
5864            !dev->writeChunkToNAND &&
5865            !dev->readChunkFromNAND &&
5866            dev->markNANDBlockBad &&
5867            dev->queryNANDBlock) return 1;
5868 #endif
5869
5870         // Can use the "spare" style interface for yaffs1
5871         if(!dev->isYaffs2 &&
5872            !dev->writeChunkWithTagsToNAND &&
5873            !dev->readChunkWithTagsFromNAND &&
5874            dev->writeChunkToNAND &&
5875            dev->readChunkFromNAND &&
5876            !dev->markNANDBlockBad &&
5877            !dev->queryNANDBlock) return 1;
5878
5879         return 0; // bad
5880 }
5881
5882
5883 int yaffs_GutsInitialise(yaffs_Device *dev)
5884 {
5885         unsigned x;
5886         int bits;
5887         int extraBits;
5888         int nBlocks;
5889
5890         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
5891         // Check stuff that must be set
5892
5893         if(!dev)
5894         {
5895                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Need a device" TENDSTR)));
5896                 return YAFFS_FAIL;
5897         }
5898         
5899         dev->internalStartBlock = dev->startBlock;
5900         dev->internalEndBlock =  dev->endBlock;
5901         dev->blockOffset = 0;
5902         dev->chunkOffset = 0;
5903         dev->nFreeChunks = 0;
5904         
5905         if(dev->startBlock == 0)
5906         {
5907                 dev->internalStartBlock = dev->startBlock + 1;
5908                 dev->internalEndBlock =  dev->endBlock + 1;
5909                 dev->blockOffset = 1;
5910                 dev->chunkOffset = dev->nChunksPerBlock;
5911         }
5912
5913         // Check geometry parameters.
5914
5915         if(     (dev->isYaffs2 && dev->nBytesPerChunk <1024)  ||
5916                 (!dev->isYaffs2 && dev->nBytesPerChunk !=512)  ||
5917                 dev->nChunksPerBlock < 2 ||
5918                 dev->nReservedBlocks < 2 ||
5919                 dev->internalStartBlock <= 0 ||
5920                 dev->internalEndBlock <= 0 ||
5921                 dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
5922           )
5923         {
5924                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s " TENDSTR),
5925                    dev->nBytesPerChunk, dev->isYaffs2 ? "2" : ""));
5926                 return YAFFS_FAIL;
5927         }
5928
5929         if(yaffs_InitialiseNAND(dev) != YAFFS_OK)
5930         {
5931                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
5932                 return YAFFS_FAIL;
5933         }
5934
5935         // Got the right mix of functions?
5936         //
5937         if(!yaffs_CheckDevFunctions(dev))
5938         {
5939                 //Function missing
5940                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device function(s) missing or wrong\n" TENDSTR)));
5941
5942                 return YAFFS_FAIL;
5943         }
5944
5945         // This is really a compilation check.
5946         if(!yaffs_CheckStructures())
5947         {
5948                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
5949                 return YAFFS_FAIL;
5950         }
5951
5952         if(dev->isMounted)
5953         {
5954                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR)));
5955                 return YAFFS_FAIL;
5956         }
5957
5958         //
5959         //
5960         // Finished with most checks. One or two more checks happen later on too.
5961         //
5962
5963         dev->isMounted = 1;
5964
5965
5966         nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5967
5968
5969
5970         // OK now calculate a few things for the device
5971         // Calculate chunkGroupBits.
5972         // We need to find the next power of 2 > than internalEndBlock
5973         
5974         x = dev->nChunksPerBlock * (dev->internalEndBlock+1);
5975         
5976         for(bits = extraBits = 0; x > 1; bits++)
5977         {
5978                 if(x & 1) extraBits++;
5979                 x >>= 1;
5980         }
5981
5982         if(extraBits > 0) bits++;
5983         
5984         
5985         // Level0 Tnodes are 16 bits, so if the bitwidth of the
5986         // chunk range we're using is greater than 16 we need
5987         // to figure out chunk shift and chunkGroupSize
5988         if(bits <= 16) 
5989         {
5990                 dev->chunkGroupBits = 0;
5991         }
5992         else
5993         {
5994                 dev->chunkGroupBits = bits - 16;
5995         }
5996         
5997         dev->chunkGroupSize = 1 << dev->chunkGroupBits;
5998
5999         if(dev->nChunksPerBlock < dev->chunkGroupSize)
6000         {
6001                 // We have a problem because the soft delete won't work if
6002                 // the chunk group size > chunks per block.
6003                 // This can be remedied by using larger "virtual blocks".
6004                 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: chunk group too large\n" TENDSTR)));
6005                 
6006                 return YAFFS_FAIL;
6007         }
6008
6009         
6010         // OK, we've finished verifying the device, lets continue with initialisation
6011         
6012         // More device initialisation
6013         dev->garbageCollections = 0;
6014         dev->passiveGarbageCollections = 0;
6015         dev->currentDirtyChecker = 0;
6016         dev->bufferedBlock = -1;
6017         dev->doingBufferedBlockRewrite = 0;
6018         dev->nDeletedFiles = 0;
6019         dev->nBackgroundDeletions=0;
6020         dev->nUnlinkedFiles = 0;
6021         dev->eccFixed=0;
6022         dev->eccUnfixed=0;
6023         dev->tagsEccFixed=0;
6024         dev->tagsEccUnfixed=0;
6025         dev->nErasureFailures = 0;
6026         dev->nErasedBlocks = 0;
6027         dev->isDoingGC = 0;
6028         
6029         //dev->localBuffer = YMALLOC(dev->nBytesPerChunk);
6030         // Initialise temporary buffers
6031         {
6032                 int i;
6033                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6034                 {
6035                         dev->tempBuffer[i].line = 0; // not in use
6036                         dev->tempBuffer[i].buffer = YMALLOC(dev->nBytesPerChunk);
6037                 }
6038         }
6039         
6040
6041         
6042         yaffs_InitialiseBlocks(dev,nBlocks);
6043         
6044         yaffs_InitialiseTnodes(dev);
6045
6046         yaffs_InitialiseObjects(dev);
6047         
6048         dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
6049         
6050         if(dev->nShortOpCaches > 0)
6051         { 
6052                 int i;
6053                 
6054                 if(dev->nShortOpCaches >  YAFFS_MAX_SHORT_OP_CACHES)
6055                 {
6056                         dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
6057                 }
6058                 
6059                 dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
6060                 
6061                 for(i=0; i < dev->nShortOpCaches; i++)
6062                 {
6063                         dev->srCache[i].object = NULL;
6064                         dev->srCache[i].lastUse = 0;
6065                         dev->srCache[i].dirty = 0;
6066                         dev->srCache[i].data = YMALLOC(dev->nBytesPerChunk);
6067                 }
6068                 dev->srLastUse = 0;
6069         }
6070
6071         dev->cacheHits = 0;
6072         
6073         
6074         // Initialise the unlinked, root and lost and found directories
6075         dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = dev->deletedDir = NULL;
6076         
6077         dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR);
6078         dev->deletedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_DELETED, S_IFDIR);
6079
6080         dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR);
6081         dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR);
6082         yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir);
6083         
6084         if(dev->isYaffs2) 
6085         {
6086                 dev->useHeaderFileSize = 1;
6087         }
6088                 
6089         // Now scan the flash.  
6090         
6091         if(dev->isYaffs2)
6092                 yaffs_ScanBackwards(dev);
6093         else
6094                 yaffs_Scan(dev);
6095         
6096         // Zero out stats
6097         dev->nPageReads = 0;
6098         dev->nPageWrites =  0;
6099         dev->nBlockErasures = 0;
6100         dev->nGCCopies = 0;
6101         dev->nRetriedWrites = 0;
6102
6103         dev->nRetiredBlocks = 0;
6104         
6105         yaffs_VerifyFreeChunks(dev);
6106
6107         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
6108         return YAFFS_OK;
6109                 
6110 }
6111
6112 void yaffs_Deinitialise(yaffs_Device *dev)
6113 {
6114         if(dev->isMounted)
6115         {
6116                 int i;
6117         
6118                 yaffs_DeinitialiseBlocks(dev);
6119                 yaffs_DeinitialiseTnodes(dev);
6120                 yaffs_DeinitialiseObjects(dev);
6121                 if(dev->nShortOpCaches > 0)
6122                 {
6123                                 
6124                         for(i=0; i < dev->nShortOpCaches; i++)
6125                         {
6126                                 YFREE(dev->srCache[i].data);
6127                         }
6128
6129                         YFREE(dev->srCache);
6130                 }
6131
6132                 YFREE(dev->gcCleanupList);
6133
6134                 for(i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6135                 {
6136                         YFREE(dev->tempBuffer[i].buffer);
6137                 }
6138                 
6139                 dev->isMounted = 0;
6140         }
6141         
6142 }
6143
6144 #if 0
6145
6146 int  yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
6147 {
6148         int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS);
6149         
6150         struct list_head *i;    
6151         yaffs_Object *l;
6152         
6153         
6154         // To the free chunks add the chunks that are in the deleted unlinked files.
6155         list_for_each(i,&dev->deletedDir->variant.directoryVariant.children)
6156         {
6157                 l = list_entry(i, yaffs_Object,siblings);
6158                 if(l->deleted)
6159                 {
6160                         nFree++;
6161                         nFree += l->nDataChunks;
6162                 }
6163         }
6164         
6165         
6166         // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree);        
6167
6168         if(nFree < 0) nFree = 0;
6169
6170         return nFree;   
6171         
6172 }
6173
6174 #endif
6175
6176 static int  yaffs_CountFreeChunks(yaffs_Device *dev)
6177 {
6178         int nFree;
6179         int b;
6180
6181         yaffs_BlockInfo *blk;   
6182
6183         
6184         for(nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock; b++)
6185         {
6186                 blk = yaffs_GetBlockInfo(dev,b);
6187                 
6188                 switch(blk->blockState)
6189                 {
6190                         case YAFFS_BLOCK_STATE_EMPTY:
6191                         case YAFFS_BLOCK_STATE_ALLOCATING: 
6192                         case YAFFS_BLOCK_STATE_COLLECTING:
6193                         case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse + blk->softDeletions); break;
6194                         default: break;
6195                 }
6196
6197         }
6198         
6199         return nFree;
6200 }       
6201         
6202
6203
6204 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
6205 {
6206         // This is what we report to the outside world
6207
6208         int nFree;
6209         int nDirtyCacheChunks;
6210                 
6211 #if 1   
6212         nFree = dev->nFreeChunks;
6213 #else
6214         nFree = yaffs_CountFreeChunks(dev);
6215 #endif
6216         
6217         // Now count the number of dirty chunks in the cache and subtract those
6218         
6219         {
6220                 int i;
6221                 for( nDirtyCacheChunks = 0,i = 0; i < dev->nShortOpCaches; i++)
6222                 {
6223                         if(dev->srCache[i].dirty) nDirtyCacheChunks++;
6224                 }
6225         }
6226         
6227         nFree -= nDirtyCacheChunks;
6228         
6229         nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
6230         
6231         if(nFree < 0) nFree = 0;
6232
6233         return nFree;   
6234         
6235 }
6236
6237 static int  yaffs_freeVerificationFailures;
6238
6239 static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
6240 {
6241         int counted = yaffs_CountFreeChunks(dev);
6242         
6243         int difference = dev->nFreeChunks - counted;
6244         
6245         if(difference)
6246         {
6247                 T(YAFFS_TRACE_ALWAYS,(TSTR("Freechunks verification failure %d %d %d" TENDSTR),dev->nFreeChunks,counted,difference)); 
6248                 yaffs_freeVerificationFailures++;       
6249         }
6250 }
6251
6252 /////////////////// YAFFS test code //////////////////////////////////
6253
6254 #define yaffs_CheckStruct(structure,syze, name) \
6255            if(sizeof(structure) != syze) \
6256                { \
6257                  T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \
6258                  return YAFFS_FAIL; \
6259                    }
6260                  
6261                  
6262 static int yaffs_CheckStructures(void)
6263 {
6264 //      yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags")
6265 //      yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion")
6266 //      yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare")
6267 #ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
6268         yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode")
6269 #endif
6270         yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader")
6271         
6272         
6273         return YAFFS_OK;
6274 }
6275
6276 #if 0
6277 void yaffs_GutsTest(yaffs_Device *dev)
6278 {
6279         
6280         if(yaffs_CheckStructures() != YAFFS_OK)
6281         {
6282                 T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR)));
6283                 return;
6284         }
6285         
6286         yaffs_TnodeTest(dev);
6287         yaffs_ObjectTest(dev);  
6288 }
6289 #endif
6290
6291